|
1 ;;; gadict-mode.el --- major mode for editing gadict dictionary source files |
|
2 |
|
3 ;; Copyright (C) 2016 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: 2016 |
|
10 ;; Keywords: dict, dictionary |
|
11 |
|
12 ;;; Commentary: |
|
13 ;; |
|
14 ;; File association can be registered by: |
|
15 ;; |
|
16 ;; (add-to-list 'auto-mode-alist (cons "\\.gadict$" 'gadict-mode)) |
|
17 |
|
18 ;;; Code: |
|
19 |
|
20 (defvar gadict-font-lock-keywords |
|
21 '( ("^\\(_\\{2\\}\\)\n\n\\(\\w.*\\)$" |
|
22 (1 font-lock-function-name-face) (2 font-lock-keyword-face)) |
|
23 ("^ +\\[[^]\n]+]" . font-lock-type-face) )) |
|
24 |
|
25 (eval-when-compile |
|
26 (defvar font-lock-beg) |
|
27 (defvar font-lock-end)) |
|
28 |
|
29 (defun gadict-font-lock-extend-region () |
|
30 "Look for '__' expression and extend `font-lock-beg' and `font-lock-end'." |
|
31 ;; (message "%d:%d, %d lines" font-lock-beg font-lock-end (count-lines font-lock-beg font-lock-end)) |
|
32 (cond |
|
33 ((and |
|
34 (< (count-lines font-lock-beg font-lock-end) 5) |
|
35 (not (and (<= (point-max) font-lock-end) (<= font-lock-beg (point-min)) ))) |
|
36 (save-excursion |
|
37 (goto-char font-lock-beg) |
|
38 (forward-line -2) |
|
39 (setq font-lock-beg (point)) |
|
40 (goto-char font-lock-end) |
|
41 (forward-line 3) |
|
42 (setq font-lock-end (point)) |
|
43 ) |
|
44 t) |
|
45 (t nil) )) |
|
46 |
|
47 (defun gadict-new-entry () |
|
48 "Insert new entry template." |
|
49 (interactive) |
|
50 (if (re-search-forward "^__" nil t) |
|
51 (beginning-of-line) |
|
52 (goto-char (point-max))) |
|
53 (while (eq (char-before) ?\n) |
|
54 (delete-backward-char 1)) |
|
55 (insert-char ?\n) |
|
56 (insert-char ?_ 2) |
|
57 (insert-char ?\n 3) |
|
58 (backward-char)) |
|
59 |
|
60 (defvar gadict-mode-map (make-sparse-keymap)) |
|
61 (define-key gadict-mode-map [C-return] 'gadict-new-entry) |
|
62 |
|
63 ;;;###autoload |
|
64 (define-derived-mode gadict-mode fundamental-mode "gadict" |
|
65 "Derived mode for editing gadict dictionary source files." |
|
66 (setq font-lock-defaults |
|
67 '(gadict-font-lock-keywords |
|
68 t nil nil nil |
|
69 (font-lock-multiline . t) |
|
70 )) |
|
71 (use-local-map gadict-mode-map) |
|
72 (modify-syntax-entry ?' ".") |
|
73 (modify-syntax-entry ?\" ".") |
|
74 (make-local-variable 'paragraph-separate) |
|
75 (setq paragraph-separate "\\([ \t\f]*\\|_\\{5,\\}\\)$") |
|
76 (make-local-variable 'paragraph-start) |
|
77 (setq paragraph-start paragraph-separate) |
|
78 (add-hook 'font-lock-extend-region-functions 'gadict-font-lock-extend-region t) ) |
|
79 |
|
80 (provide 'gadict) |
|
81 |
|
82 ;;; dict-mode.el ends here |