contrib/gadict.el
changeset 361 6dee73a3e3cb
parent 335 8732c7c894af
child 362 3d5c70256edf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/gadict.el	Sun Mar 13 23:10:30 2016 +0200
@@ -0,0 +1,82 @@
+;;; gadict-mode.el --- major mode for editing gadict dictionary source files
+
+;; Copyright (C) 2016 by Oleksandr Gavenko <gavenkoa@gmail.com>
+
+;; You can do anything with this file without any warranty.
+
+;; Author: Oleksandr Gavenko <gavenkoa@gmail.com>
+;; Maintainer: Oleksandr Gavenko <gavenkoa@gmail.com>
+;; Created: 2016
+;; Keywords: dict, dictionary
+
+;;; Commentary:
+;;
+;; File association can be registered by:
+;;
+;;   (add-to-list 'auto-mode-alist (cons "\\.gadict$" 'gadict-mode))
+
+;;; Code:
+
+(defvar gadict-font-lock-keywords
+  '( ("^\\(_\\{2\\}\\)\n\n\\(\\w.*\\)$"
+      (1 font-lock-function-name-face) (2 font-lock-keyword-face))
+     ("^ +\\[[^]\n]+]" . font-lock-type-face) ))
+
+(eval-when-compile
+  (defvar font-lock-beg)
+  (defvar font-lock-end))
+
+(defun gadict-font-lock-extend-region ()
+  "Look for '__' expression and extend `font-lock-beg' and `font-lock-end'."
+  ;; (message "%d:%d, %d lines" font-lock-beg font-lock-end (count-lines font-lock-beg font-lock-end))
+  (cond
+   ((and
+     (< (count-lines font-lock-beg font-lock-end) 5)
+     (not (and (<= (point-max) font-lock-end) (<= font-lock-beg (point-min)) )))
+    (save-excursion
+      (goto-char font-lock-beg)
+      (forward-line -2)
+      (setq font-lock-beg (point))
+      (goto-char font-lock-end)
+      (forward-line 3)
+      (setq font-lock-end (point))
+      )
+    t)
+   (t nil) ))
+
+(defun gadict-new-entry ()
+  "Insert new entry template."
+  (interactive)
+  (if (re-search-forward "^__" nil t)
+      (beginning-of-line)
+    (goto-char (point-max)))
+  (while (eq (char-before) ?\n)
+    (delete-backward-char 1))
+  (insert-char ?\n)
+  (insert-char ?_ 2)
+  (insert-char ?\n 3)
+  (backward-char))
+
+(defvar gadict-mode-map (make-sparse-keymap))
+(define-key gadict-mode-map [C-return] 'gadict-new-entry)
+
+;;;###autoload
+(define-derived-mode gadict-mode fundamental-mode "gadict"
+  "Derived mode for editing gadict dictionary source files."
+  (setq font-lock-defaults
+        '(gadict-font-lock-keywords
+          t nil nil nil
+          (font-lock-multiline . t)
+          ))
+  (use-local-map gadict-mode-map)
+  (modify-syntax-entry ?' ".")
+  (modify-syntax-entry ?\" ".")
+  (make-local-variable 'paragraph-separate)
+  (setq paragraph-separate "\\([ \t\f]*\\|_\\{5,\\}\\)$")
+  (make-local-variable 'paragraph-start)
+  (setq paragraph-start paragraph-separate)
+  (add-hook 'font-lock-extend-region-functions 'gadict-font-lock-extend-region t) )
+
+(provide 'gadict)
+
+;;; dict-mode.el ends here