contrib/gadialog.el
changeset 1191 a3a7c8342b1c
parent 1168 24da81e9a023
child 1217 451296ba92c9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/gadialog.el	Mon Feb 10 00:54:18 2020 +0200
@@ -0,0 +1,86 @@
+;;; gadialog.el --- major mode for editing gadialog dialog files -*- lexical-binding: t -*-
+
+;; Copyright (C) 2019 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: 2019
+;; Version: 0.17
+;; Keywords: dict, dictionary
+
+;;; Commentary:
+;;
+;; Mode can be installed by:
+;;
+;;   (autoload 'gadialog-mode "gadialog")
+;;
+;; File association can be registered by:
+;;
+;;   (add-to-list 'auto-mode-alist (cons "\\.gadialog$" 'gadialog-mode))
+
+;;; Code:
+
+(defvar gadialog-font-lock-keywords
+  '(("^# [1-9][0-9]*" . font-lock-type-face)
+    ("^## [1-9][0-9]*" . font-lock-warning-face)
+    ("^- " . font-lock-keyword-face)))
+
+(defvar gadialog-syntax-table
+  (let ((table (make-syntax-table text-mode-syntax-table)))
+    (modify-syntax-entry ?' "w" table)
+    table))
+
+(defun gadialog-next-num ()
+  (save-excursion
+    (goto-char (point-min))
+    (let (beg end num)
+      (catch 'return
+        (when (looking-at "## \\([1-9][0-9]*\\)")
+          (setq beg (match-beginning 1)
+                end (match-end 1))
+          (setq num (string-to-number (buffer-substring beg end)))
+          (delete-region beg end)
+          (goto-char beg)
+          (setq num (1+ num))
+          (insert (int-to-string num))
+          (throw 'return num))
+        (insert "## 1\n")
+        1))))
+
+(defun gadialog-insert-template ()
+  (interactive)
+  (forward-line 0)
+  (when (looking-at "# ")
+    (forward-line 1))
+  (unless (re-search-forward "^# " (+ (point) 10240) t)
+    (goto-char (point-max)))
+  (forward-line 0)
+  (while (memq (char-before) '(?\  ?\n ?\t))
+    (delete-char -1))
+  (insert "\n# ")
+  (insert (int-to-string (gadialog-next-num)))
+  (insert "\n- \n")
+  (backward-char))
+
+(defun gadialog-insert-sentence ()
+  (interactive)
+  (forward-line 1)
+  (while (not (or (eobp)
+                  (memq (char-after) (list ?- ?#))))
+    (forward-line 1))
+  (insert ?\n)
+  (backward-char)
+  (insert "- "))
+
+
+(define-derived-mode gadialog-mode fundamental-mode "gadialog"
+  (setq font-lock-defaults '(gadialog-font-lock-keywords))
+  (define-key (current-local-map) [C-return] 'gadialog-insert-template)
+  (define-key (current-local-map) [S-return] 'gadialog-insert-sentence)
+  (set-syntax-table gadialog-syntax-table))
+
+(provide 'gadialog)
+
+;;; gadialog.el ends here