1 ;;; gadialog.el --- major mode for editing gadialog 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.17 |
|
11 ;; Keywords: dict, dictionary |
|
12 |
|
13 ;;; Commentary: |
|
14 ;; |
|
15 ;; Mode can be installed by: |
|
16 ;; |
|
17 ;; (autoload 'gadialog-mode "gadialog") |
|
18 ;; |
|
19 ;; File association can be registered by: |
|
20 ;; |
|
21 ;; (add-to-list 'auto-mode-alist (cons "\\.gadialog$" 'gadialog-mode)) |
|
22 |
|
23 ;;; Code: |
|
24 |
|
25 (defvar gadialog-font-lock-keywords |
|
26 '(("^# [1-9][0-9]*" . font-lock-type-face) |
|
27 ("^## [1-9][0-9]*" . font-lock-warning-face) |
|
28 ("^- " . font-lock-keyword-face))) |
|
29 |
|
30 (defvar gadialog-syntax-table |
|
31 (let ((table (make-syntax-table text-mode-syntax-table))) |
|
32 (modify-syntax-entry ?' "w" table) |
|
33 table)) |
|
34 |
|
35 (defun gadialog-next-num () |
|
36 (save-excursion |
|
37 (goto-char (point-min)) |
|
38 (let (beg end num) |
|
39 (catch 'return |
|
40 (when (looking-at "## \\([1-9][0-9]*\\)") |
|
41 (setq beg (match-beginning 1) |
|
42 end (match-end 1)) |
|
43 (setq num (string-to-number (buffer-substring beg end))) |
|
44 (delete-region beg end) |
|
45 (goto-char beg) |
|
46 (setq num (1+ num)) |
|
47 (insert (int-to-string num)) |
|
48 (throw 'return num)) |
|
49 (insert "## 1\n") |
|
50 1)))) |
|
51 |
|
52 (defun gadialog-cleaup-whitespaces-forward () |
|
53 (while (memq (char-before) '(?\ ?\n ?\t)) |
|
54 (delete-char -1))) |
|
55 |
|
56 (defun gadialog-insert-template () |
|
57 (interactive) |
|
58 (forward-line 0) |
|
59 (when (looking-at "# ") |
|
60 (forward-line 1)) |
|
61 (unless (re-search-forward "^# " (+ (point) 10240) t) |
|
62 (goto-char (point-max))) |
|
63 (when (eobp) (insert-char ?\n)) |
|
64 (forward-line 0) |
|
65 (gadialog-cleaup-whitespaces-forward) |
|
66 (insert "\n# ") |
|
67 (insert (int-to-string (gadialog-next-num))) |
|
68 (insert "\n- \n") |
|
69 (backward-char)) |
|
70 |
|
71 (defun gadialog-insert-sentence () |
|
72 (interactive) |
|
73 (forward-line 1) |
|
74 (while (not (or (eobp) |
|
75 (memq (char-after) (list ?- ?#)))) |
|
76 (forward-line 1)) |
|
77 (gadialog-cleaup-whitespaces-forward) |
|
78 (insert "\n- \n") |
|
79 (backward-char)) |
|
80 |
|
81 |
|
82 (define-derived-mode gadialog-mode fundamental-mode "gadialog" |
|
83 (setq font-lock-defaults '(gadialog-font-lock-keywords)) |
|
84 (define-key (current-local-map) [C-return] 'gadialog-insert-template) |
|
85 (define-key (current-local-map) [S-return] 'gadialog-insert-sentence) |
|
86 (set-syntax-table gadialog-syntax-table) |
|
87 (setq paragraph-start "[#-]")) |
|
88 |
|
89 (provide 'gadialog) |
|
90 |
|
91 ;;; gadialog.el ends here |
|