misc/gaphrase.el
changeset 1253 a4a1e57e8ad7
parent 1203 c767b62ec786
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/gaphrase.el	Sun Oct 18 12:25:22 2020 +0300
@@ -0,0 +1,74 @@
+;;; gaphrase.el --- major mode for editing gaphrase 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.1
+;; Keywords: dict, dictionary
+
+;;; Commentary:
+;;
+;; Mode can be installed by:
+;;
+;;   (autoload 'gaphrase-mode "gaphrase")
+;;
+;; File association can be registered by:
+;;
+;;   (add-to-list 'auto-mode-alist (cons "\\.gaphrase$" 'gaphrase-mode))
+
+;;; Code:
+
+(defvar gaphrase-font-lock-keywords
+  '(("^# [1-9][0-9]*" . font-lock-type-face)
+    ("^## [1-9][0-9]*" . font-lock-warning-face)))
+
+(defvar gaphrase-syntax-table
+  (let ((table (make-syntax-table text-mode-syntax-table)))
+    (modify-syntax-entry ?' "w" table)
+    table))
+
+(defun gaphrase-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 gaphrase-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 (gaphrase-next-num)))
+  (insert "\n\n")
+  (backward-char))
+
+
+(define-derived-mode gaphrase-mode fundamental-mode "gaphrase"
+  (setq font-lock-defaults '(gaphrase-font-lock-keywords))
+  (define-key (current-local-map) [C-return] 'gaphrase-insert-template)
+  (set-syntax-table gaphrase-syntax-table))
+
+(provide 'gaphrase)
+
+;;; gaphrase.el ends here