-
Notifications
You must be signed in to change notification settings - Fork 3
/
chatu.el
414 lines (364 loc) · 13.8 KB
/
chatu.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
;;; chatu.el --- Convert and insert any images to org-mode or markdown buffer -*- lexical-binding: t -*-
;; Copyright (c) 2024 Kimi Ma <[email protected]>
;; Author: Kimi Ma <[email protected]>
;; URL: https://github.com/kimim/chatu
;; Keywords: multimedia convenience
;; Version: 0.1
;; Package-Requires: ((org "9.6.6") (emacs "29.1") (plantuml-mode "1.2.9"))
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Acknowledgements:
;; Inspired a lot by https://github.com/nobiot/org-transclusion from
;; Noboru Ota <[email protected]>
;;; Commentary:
;; When you are drawing with draw.io or plantuml and you want to
;; insert the svg image exported from the file, the operation is
;; nontrivial. This extension provide two commands to reduce the
;; manual work:
;;
;; - `chatu-new' add a chatu line
;; - `chatu-add' convert diagram to svg, and insert it to buffer
;; - `chatu-open' open diagram file at the point
;;
;; When `chatu-mode' is activated,
;; in `org-mode',
;; - "C-c C-o" on chatu line will invoke `chatu-open'
;; - "C-c C-c" on chatu line will invoke `chatu-add'
;;
;; in `markdown-mode'
;; - "C-c C-o" on chatu line will invoke `chatu-open'
;; - "C-c C-c C-c" on chatu line will invoke `chatu-add'
;;; Installation:
;; To enable, add the following:
;;
;; (use-package chatu
;; :custom ((chatu-input-dir "./draws")
;; (chatu-output-dir "./images"))
;; :hook ((org-mode markdown-mode) . chatu-mode))
;;; Customization:
;; `chatu-input-dir': diagram input folder, default is ./draws
;; `chatu-output-dir': default svg output folder, default is ./draws_out
;; `chatu-dir-regex': customize folder regex
;; `chatu-file-regex': customize file name regex
;;; Change log:
;; 2024/03/03
;; * added to elpa package, thanks the review support from @riscy
;;
;; 2024/03/02
;; * add new chatu option `:output-ext' to set output extension.
;;
;; 2024/02/21
;; * add `declare-function' as suggested by riscy.
;;
;; 2024/02/13
;; * Support curl, babashka, clojure, R, LilyPond
;;
;; 2024/02/10
;; * Initial version support draw.io and plantuml
;;; Code:
(defgroup chatu nil
"Insert diagram as svg image."
:group 'tools
:group 'chatu
:prefix "chatu-"
:link '(url-link :tag "Github" "https://github.com/kimim/chatu")
:package-version '("Chatu" . "1.0.0"))
(declare-function markdown-remove-inline-images "ext:markdown-mode.el")
(declare-function markdown-display-inline-images "ext:markdown-mode.el")
(declare-function markdown-follow-thing-at-point "ext:markdown-mode.el")
(declare-function org-redisplay-inline-images "org.el")
(defvar org-ctrl-c-ctrl-c-hook)
(defvar markdown-mode-map)
(defvar org-open-at-point-functions)
(defcustom chatu-input-dir "./draws"
"Define default input folder."
:type 'string)
(defcustom chatu-output-dir "./draws_out"
"Define default output folder."
:type 'string)
(defcustom chatu-file-regex
"\"\\([\u4e00-\u9fa5:~ \\/a-z_\s0-9\\.\\%-]+\\)\""
"Define regex of file. Currently support Chinese characters."
:type 'string)
(defcustom chatu-dir-regex
"\"\\([\u4e00-\u9fa5:~ \\/a-z_\s0-9\\.-]+\\)\""
"Define regex of directry. Currently support Chinese charaters."
:type 'string)
(defun chatu-get-keyword (line)
"Get chatu keyword from string LINE."
(when (and
(string-match
"#\\+\\(\\w+\\): +" line)
(string= "chatu"
(substring-no-properties
(match-string 1 line))))
(list :chatu t)))
(defun chatu-get-type (line)
"Get chatu type from string LINE."
(when (string-match
":\\(\\w+\\) +" line)
(list :type
(substring-no-properties
(match-string 1 line)))))
(defun chatu-get-settings (line)
"Get all chatu settings from string LINE."
(save-match-data
(let ((pos 0)
settings)
(while (string-match "\\(:\\w+\\)\\( +:\\|$\\)" line pos)
(setq settings
(append settings
(list
(intern
(substring-no-properties
(match-string 1 line)))
t)))
(setq pos (match-end 1)))
settings)))
(defun chatu-get-input (line)
"Get chatu input file from string LINE."
(when (string-match
(concat ":\\w* +" chatu-file-regex) line)
(list :input
(substring-no-properties
(match-string 1 line)))))
(defun chatu-get-output (line)
"Get chatu output file from string LINE."
(when (string-match
(concat ":output +" chatu-file-regex) line)
(list :output
(substring-no-properties
(match-string 1 line)))))
(defun chatu-get-output-ext (line)
"Get chatu output file extension from string LINE."
(when (string-match
":output-ext +\\(.+\\)[ \\t\\n]*" line)
(list :output-ext
(substring-no-properties
(match-string 1 line)))))
(defun chatu-get-input-dir (line)
"Get chatu output directory from string LINE."
(when (string-match
(concat ":input-dir +" chatu-dir-regex) line)
(list :input-dir
(substring-no-properties
(match-string 1 line)))))
(defun chatu-get-output-dir (line)
"Get chatu input directory from string LINE."
(when (string-match
(concat ":output-dir +" chatu-dir-regex) line)
(list :output-dir
(substring-no-properties
(match-string 1 line)))))
(defun chatu-get-page (line)
"Get chatu output page from string LINE."
(when (string-match ":page +\\([0-9]*\\)[ \\t\\n]*" line)
(list :page
(substring-no-properties
(match-string 1 line)))))
(defun chatu-get-script (line)
"Get chatu inline script from string LINE."
(when (string-match ":script +\"\\(.*\\)\"[ \\t\\n]*" line)
(list :script
(substring-no-properties
(match-string 1 line)))))
(defvar chatu-keyword-value-functions
'(chatu-get-keyword
chatu-get-settings
chatu-get-type
chatu-get-input
chatu-get-output
chatu-get-page
chatu-get-input-dir
chatu-get-output-dir
chatu-get-script
chatu-get-output-ext))
(defun chatu-normalize-keyword-plist (keyword-plist)
"Normalize KEYWORD-PLIST."
(when (plist-get keyword-plist :chatu)
(let* ((input (plist-get keyword-plist :input))
(input-dir (or (plist-get keyword-plist :input-dir)
;; if input already contains parent folder
;; ignore `chatu-input-dir'
(if (file-name-directory input)
nil
chatu-input-dir)))
(_ (plist-put keyword-plist :input-path
(if input-dir
(concat input-dir "/" input)
input)))
(output-ext (plist-get keyword-plist :output-ext))
(output (plist-get keyword-plist :output))
(output-dir (or (plist-get keyword-plist :output-dir)
;; if output already contains parent folder
;; ignore `chatu-output-dir'
(if (and output (file-name-directory output))
nil
chatu-output-dir)))
(page (plist-get keyword-plist :page))
(output (or output
(if page
(concat (file-name-sans-extension
;; remove input's parent folder
(file-name-base input))
"-" page "." (or output-ext "svg"))
(file-name-with-extension
(file-name-base input)
;; when output-ext is set, use it.
(or output-ext "svg")))))
(_ (plist-put keyword-plist :output-path
(if output-dir
(concat output-dir "/" output)
output))))
keyword-plist)))
(defun chatu-keyword-plist ()
"Get normalized KEYWORD-PLIST from string line."
(let ((plist))
(when-let ((line (buffer-substring (line-beginning-position)
(line-end-position))))
(dolist (fn chatu-keyword-value-functions) plist
(setq plist (append plist (funcall fn line)))))
(chatu-normalize-keyword-plist plist)))
(defun chatu-refresh-image ()
"Refresh image depending on the current major mode."
(cond
((derived-mode-p 'markdown-mode)
(markdown-remove-inline-images)
(markdown-display-inline-images))
((derived-mode-p 'org-mode)
(org-redisplay-inline-images))))
(defun chatu-insert-image (path)
"Insert image string PATH for different major mode."
(cond ((derived-mode-p 'markdown-mode)
(insert "![](" path ")"))
((derived-mode-p 'org-mode)
(insert "[[file:" path "]]"))))
(defun chatu-img-pre ()
"Get image string prefix for different major mode."
(cond ((derived-mode-p 'markdown-mode)
"![")
((derived-mode-p 'org-mode)
"[[")))
(defun chatu-skip-lines ()
"Skip lines after chatu line."
(while (and (derived-mode-p 'org-mode)
(string-prefix-p
"#+" (buffer-substring
(line-beginning-position)
(line-end-position))))
(forward-line)))
;;;###autoload
(defun chatu-new ()
"Insert chatu text line respect to mode."
(interactive)
(insert
(cond ((derived-mode-p 'markdown-mode)
"<!-- #+chatu: :")
((derived-mode-p 'org-mode)
"#+chatu: :"))
(read-string "type: " "drawio")
" \""
(read-string "input name: " "")
"\""
(if (derived-mode-p 'markdown-mode)
" -->"
"\n#+results:"))
(when (derived-mode-p 'org-mode)
(forward-line -1)))
;;;###autoload
(defun chatu-add ()
"Concert diagram to svg and add to buffer."
(interactive)
(save-excursion
(let* ((keyword-plist (chatu-keyword-plist))
(type (downcase
(plist-get keyword-plist :type)))
(script (progn
(require (intern (concat "chatu-" type)))
(funcall (intern (concat "chatu-" type "-script"))
keyword-plist)))
;; ~ is after `shell-quote-argument' is \~, which is not
;; working. remove it.
(script (string-replace "\\~" "~" script))
(result (plist-get keyword-plist :output-path))
(result-dir (file-name-directory result)))
;; ensure output-dir exists.
(when (not (file-exists-p result-dir))
(make-directory result-dir t))
(forward-line)
(chatu-skip-lines)
(let ((process (start-process-shell-command "chatu-buffer" nil script)))
(set-process-sentinel
process `(lambda (process event)
;; refresh image
(chatu-refresh-image))))
(if (string-prefix-p (chatu-img-pre)
(buffer-substring
(line-beginning-position)
(line-end-position)))
;; when it is image link
(kill-whole-line 0)
(progn (beginning-of-line) (open-line 1)))
(chatu-insert-image result))))
;;;###autoload
(defun chatu-open ()
"Open diagram file in Emacs."
(interactive)
(let* ((keyword-plist (chatu-keyword-plist))
(type (downcase
(plist-get keyword-plist :type))))
(require (intern (concat "chatu-" type)))
(funcall (intern (concat "chatu-" type "-open"))
keyword-plist)))
(defun chatu-ctrl-c-ctrl-c ()
"Hook function for `org-ctrl-c-ctrl-c-hook'."
(let ((plist (chatu-keyword-plist)))
(if (plist-get plist :chatu)
(progn
(chatu-add)
t)
nil)))
(defun chatu-ctrl-c-ctrl-o (&optional args)
"Hook function for `org-open-at-point-functions'.
ARGS is ignored, required by `markdown-follow-thing-at-point'."
(ignore args) ;; args are not used.
(let ((plist (chatu-keyword-plist)))
(if (plist-get plist :chatu)
(progn
(chatu-open)
t)
nil)))
;;;###autoload
(define-minor-mode chatu-mode
"Add chatu commands or hooks."
:global nil
:lighter " Chatu"
(if (not chatu-mode)
(cond ((derived-mode-p 'markdown-mode)
(keymap-unset markdown-mode-map "C-c C-c C-c" t)
(advice-remove 'markdown-follow-thing-at-point
#'chatu-ctrl-c-ctrl-o))
((derived-mode-p 'org-mode)
(remove-hook 'org-ctrl-c-ctrl-c-hook #'chatu-ctrl-c-ctrl-c)
(setq org-open-at-point-functions
(delete 'chatu-ctrl-c-ctrl-o org-open-at-point-functions))))
(cond ((derived-mode-p 'markdown-mode)
(keymap-set markdown-mode-map "C-c C-c C-c" 'chatu-add)
(advice-add 'markdown-follow-thing-at-point
:before-until
#'chatu-ctrl-c-ctrl-o))
((derived-mode-p 'org-mode)
(add-hook 'org-ctrl-c-ctrl-c-hook #'chatu-ctrl-c-ctrl-c)
(add-to-list 'org-open-at-point-functions #'chatu-ctrl-c-ctrl-o)))))
(provide 'chatu)
;;; chatu.el ends here