author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Mon, 27 Feb 2023 00:55:27 +0200 | |
changeset 1342 | d6413e1d20b0 |
parent 1253 | a4a1e57e8ad7 |
permissions | -rw-r--r-- |
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
1 |
;;; gadialog.el --- major mode for editing gadialog dialog files -*- lexical-binding: t -*- |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
2 |
|
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
3 |
;; Copyright (C) 2019 by Oleksandr Gavenko <gavenkoa@gmail.com> |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
4 |
|
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
5 |
;; You can do anything with this file without any warranty. |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
6 |
|
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
7 |
;; Author: Oleksandr Gavenko <gavenkoa@gmail.com> |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
8 |
;; Maintainer: Oleksandr Gavenko <gavenkoa@gmail.com> |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
9 |
;; Created: 2019 |
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
10 |
;; Version: 0.17 |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
11 |
;; Keywords: dict, dictionary |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
12 |
|
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
13 |
;;; Commentary: |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
14 |
;; |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
15 |
;; Mode can be installed by: |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
16 |
;; |
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
17 |
;; (autoload 'gadialog-mode "gadialog") |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
18 |
;; |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
19 |
;; File association can be registered by: |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
20 |
;; |
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
21 |
;; (add-to-list 'auto-mode-alist (cons "\\.gadialog$" 'gadialog-mode)) |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
22 |
|
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
23 |
;;; Code: |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
24 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
25 |
(defvar gadialog-font-lock-keywords |
1145
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
26 |
'(("^# [1-9][0-9]*" . font-lock-type-face) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
27 |
("^## [1-9][0-9]*" . font-lock-warning-face) |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
28 |
("^- " . font-lock-keyword-face))) |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
29 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
30 |
(defvar gadialog-syntax-table |
1168
24da81e9a023
Use ' as word part when killing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1161
diff
changeset
|
31 |
(let ((table (make-syntax-table text-mode-syntax-table))) |
24da81e9a023
Use ' as word part when killing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1161
diff
changeset
|
32 |
(modify-syntax-entry ?' "w" table) |
24da81e9a023
Use ' as word part when killing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1161
diff
changeset
|
33 |
table)) |
24da81e9a023
Use ' as word part when killing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1161
diff
changeset
|
34 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
35 |
(defun gadialog-next-num () |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
36 |
(save-excursion |
1145
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
37 |
(goto-char (point-min)) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
38 |
(let (beg end num) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
39 |
(catch 'return |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
40 |
(when (looking-at "## \\([1-9][0-9]*\\)") |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
41 |
(setq beg (match-beginning 1) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
42 |
end (match-end 1)) |
1157
41bb8575966d
Fix: string-to-int was removed in v26.1.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1152
diff
changeset
|
43 |
(setq num (string-to-number (buffer-substring beg end))) |
1145
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
44 |
(delete-region beg end) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
45 |
(goto-char beg) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
46 |
(setq num (1+ num)) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
47 |
(insert (int-to-string num)) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
48 |
(throw 'return num)) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
49 |
(insert "## 1\n") |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
50 |
1)))) |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
51 |
|
1248 | 52 |
(defun gadialog-cleaup-whitespaces-forward () |
1217
451296ba92c9
Added cleanup of whitespaces to Emacs's gadialog mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1191
diff
changeset
|
53 |
(while (memq (char-before) '(?\ ?\n ?\t)) |
451296ba92c9
Added cleanup of whitespaces to Emacs's gadialog mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1191
diff
changeset
|
54 |
(delete-char -1))) |
451296ba92c9
Added cleanup of whitespaces to Emacs's gadialog mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1191
diff
changeset
|
55 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
56 |
(defun gadialog-insert-template () |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
57 |
(interactive) |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
58 |
(forward-line 0) |
1145
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
59 |
(when (looking-at "# ") |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
60 |
(forward-line 1)) |
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
61 |
(unless (re-search-forward "^# " (+ (point) 10240) t) |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
62 |
(goto-char (point-max))) |
1249
f2f0db6154f3
Fixed template insertion when on the last line without final newline.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1248
diff
changeset
|
63 |
(when (eobp) (insert-char ?\n)) |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
64 |
(forward-line 0) |
1248 | 65 |
(gadialog-cleaup-whitespaces-forward) |
1145
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
66 |
(insert "\n# ") |
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
67 |
(insert (int-to-string (gadialog-next-num))) |
1145
79b55cca9f44
Updated Emacs gaphrase to support new format with unique ids.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1141
diff
changeset
|
68 |
(insert "\n- \n") |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
69 |
(backward-char)) |
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
70 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
71 |
(defun gadialog-insert-sentence () |
1152
ebaadea64df1
Added key binding to insert new sentence.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1145
diff
changeset
|
72 |
(interactive) |
ebaadea64df1
Added key binding to insert new sentence.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1145
diff
changeset
|
73 |
(forward-line 1) |
ebaadea64df1
Added key binding to insert new sentence.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1145
diff
changeset
|
74 |
(while (not (or (eobp) |
1161
e410e63f74e8
Fixed gaphrase-insert-sentence to handle # boundaries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1157
diff
changeset
|
75 |
(memq (char-after) (list ?- ?#)))) |
1152
ebaadea64df1
Added key binding to insert new sentence.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1145
diff
changeset
|
76 |
(forward-line 1)) |
1248 | 77 |
(gadialog-cleaup-whitespaces-forward) |
1217
451296ba92c9
Added cleanup of whitespaces to Emacs's gadialog mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1191
diff
changeset
|
78 |
(insert "\n- \n") |
451296ba92c9
Added cleanup of whitespaces to Emacs's gadialog mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1191
diff
changeset
|
79 |
(backward-char)) |
1152
ebaadea64df1
Added key binding to insert new sentence.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1145
diff
changeset
|
80 |
|
ebaadea64df1
Added key binding to insert new sentence.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1145
diff
changeset
|
81 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
82 |
(define-derived-mode gadialog-mode fundamental-mode "gadialog" |
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
83 |
(setq font-lock-defaults '(gadialog-font-lock-keywords)) |
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
84 |
(define-key (current-local-map) [C-return] 'gadialog-insert-template) |
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
85 |
(define-key (current-local-map) [S-return] 'gadialog-insert-sentence) |
1218
fb5827e19407
Added support to Emacs gadialog mode for paragraphs for justification.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1217
diff
changeset
|
86 |
(set-syntax-table gadialog-syntax-table) |
fb5827e19407
Added support to Emacs gadialog mode for paragraphs for justification.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1217
diff
changeset
|
87 |
(setq paragraph-start "[#-]")) |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
88 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
89 |
(provide 'gadialog) |
1141
879c410290ea
Added Emacs major mode for editing gaphrase dialog files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
90 |
|
1191
a3a7c8342b1c
Renamed gaphrase => gadialog to use gaphrase name for collocations.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1168
diff
changeset
|
91 |
;;; gadialog.el ends here |