contrib/gaphrase.el
changeset 1253 a4a1e57e8ad7
parent 1252 c87a8290acc4
child 1254 ca3e9ee7e186
equal deleted inserted replaced
1252:c87a8290acc4 1253:a4a1e57e8ad7
     1 ;;; gaphrase.el --- major mode for editing gaphrase dialog files -*- lexical-binding: t -*-
       
     2 
       
     3 ;; Copyright (C) 2019 by Oleksandr Gavenko <gavenkoa@gmail.com>
       
     4 
       
     5 ;; You can do anything with this file without any warranty.
       
     6 
       
     7 ;; Author: Oleksandr Gavenko <gavenkoa@gmail.com>
       
     8 ;; Maintainer: Oleksandr Gavenko <gavenkoa@gmail.com>
       
     9 ;; Created: 2019
       
    10 ;; Version: 0.1
       
    11 ;; Keywords: dict, dictionary
       
    12 
       
    13 ;;; Commentary:
       
    14 ;;
       
    15 ;; Mode can be installed by:
       
    16 ;;
       
    17 ;;   (autoload 'gaphrase-mode "gaphrase")
       
    18 ;;
       
    19 ;; File association can be registered by:
       
    20 ;;
       
    21 ;;   (add-to-list 'auto-mode-alist (cons "\\.gaphrase$" 'gaphrase-mode))
       
    22 
       
    23 ;;; Code:
       
    24 
       
    25 (defvar gaphrase-font-lock-keywords
       
    26   '(("^# [1-9][0-9]*" . font-lock-type-face)
       
    27     ("^## [1-9][0-9]*" . font-lock-warning-face)))
       
    28 
       
    29 (defvar gaphrase-syntax-table
       
    30   (let ((table (make-syntax-table text-mode-syntax-table)))
       
    31     (modify-syntax-entry ?' "w" table)
       
    32     table))
       
    33 
       
    34 (defun gaphrase-next-num ()
       
    35   (save-excursion
       
    36     (goto-char (point-min))
       
    37     (let (beg end num)
       
    38       (catch 'return
       
    39         (when (looking-at "## \\([1-9][0-9]*\\)")
       
    40           (setq beg (match-beginning 1)
       
    41                 end (match-end 1))
       
    42           (setq num (string-to-number (buffer-substring beg end)))
       
    43           (delete-region beg end)
       
    44           (goto-char beg)
       
    45           (setq num (1+ num))
       
    46           (insert (int-to-string num))
       
    47           (throw 'return num))
       
    48         (insert "## 1\n")
       
    49         1))))
       
    50 
       
    51 (defun gaphrase-insert-template ()
       
    52   (interactive)
       
    53   (forward-line 0)
       
    54   (when (looking-at "# ")
       
    55     (forward-line 1))
       
    56   (unless (re-search-forward "^# " (+ (point) 10240) t)
       
    57     (goto-char (point-max)))
       
    58   (forward-line 0)
       
    59   (while (memq (char-before) '(?\  ?\n ?\t))
       
    60     (delete-char -1))
       
    61   (insert "\n# ")
       
    62   (insert (int-to-string (gaphrase-next-num)))
       
    63   (insert "\n\n")
       
    64   (backward-char))
       
    65 
       
    66 
       
    67 (define-derived-mode gaphrase-mode fundamental-mode "gaphrase"
       
    68   (setq font-lock-defaults '(gaphrase-font-lock-keywords))
       
    69   (define-key (current-local-map) [C-return] 'gaphrase-insert-template)
       
    70   (set-syntax-table gaphrase-syntax-table))
       
    71 
       
    72 (provide 'gaphrase)
       
    73 
       
    74 ;;; gaphrase.el ends here