-
Notifications
You must be signed in to change notification settings - Fork 21
/
edraw-mode.el
268 lines (203 loc) · 8.55 KB
/
edraw-mode.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
;;; edraw-mode.el --- Edraw File Editing Mode -*- lexical-binding: t; -*-
;; Copyright (C) 2021 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <[email protected]>
;; Keywords: Graphics, Drawing, SVG
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; (autoload 'edraw-mode "edraw-mode")
;; (add-to-list 'auto-mode-alist '("\\.edraw\\.svg$" . edraw-mode))
;; NOTE: Set later than other modes for .svg such as image-mode.
;;
;; (autoload 'edraw "edraw-mode" nil t)
;; M-x edraw
;;; Code:
(require 'edraw)
(require 'edraw-import)
(defvar-local edraw-mode-editor nil)
;;;###autoload
(defun edraw-mode ()
"Major mode for editing edraw SVG files.
The following commands are available:
\\{edraw-mode-map}"
(interactive)
(widen)
(let* ((svg (edraw-mode--parse-svg)) ;; May throw an error
;; After SVG parse
(ov (progn
;; Leave current major mode
(major-mode-suspend)
;; Fix source text (If buffer is empty, add dummy text)
;; and set read-only property
(edraw-mode--text-lock)
;; Cover the entire source text
(make-overlay (point-min) (point-max) nil nil t)))
(editor (edraw-editor
:overlay ov
:svg svg
;; Don't use :document-writer to hide "save" from main menu
;;:document-writer 'edraw-mode-write-document
;;:document-writer-accepts-top-level-comments-p t
:menu-filter
(lambda (menu-type items &rest _)
(pcase menu-type
('main-menu
(append
items
`(((edraw-msg "Save") save-buffer)
("xml-mode" xml-mode))))
(_ items))))))
(setq-local edraw-mode-editor editor)
;; Setup Keymap
(edraw-mode-transfer-overlay-keymap-to-local-map editor)
;; Setup modification tracking and data saving
(edraw-add-hook editor 'change 'edraw-mode-on-changed)
(add-hook 'before-save-hook 'edraw-mode-on-before-save nil t)
;; Setup file name tracking and maintain :base-uri
;; @todo If default-directory changes, base-uri should also change.
(edraw-mode-update-base-uri)
(add-hook 'after-set-visited-file-name-hook
'edraw-mode-on-file-name-changed nil t)
;; Setup major mode finalization
(add-hook 'change-major-mode-hook 'edraw-mode-finalize-major-mode nil t)
;; Start major mode
(setq mode-name "Easy Draw")
(setq major-mode 'edraw-mode)
(setq cursor-type nil) ;;Hide cursor
;; Disable context-menu-mode
(setq-local minor-mode-overriding-map-alist
'((context-menu-mode . nil)))
;; Ensure the top left corner of the buffer is displayed
(goto-char (point-min))
(set-window-hscroll (selected-window) 0)
(run-mode-hooks 'edraw-mode-hook)))
(defconst edraw-mode--parse-svg--check-edraw-body t)
(defun edraw-mode--parse-svg ()
(let* ((source (buffer-substring-no-properties (point-min) (point-max)))
(svg (edraw-svg-decode-svg source nil t)))
;; Check SVG
(unless (if (string-empty-p source) ;;@todo check whitespace only?
(null svg)
(and (not (null svg))
(edraw-dom-element-p svg)
(edraw-dom-tag-eq (car (edraw-dom-split-top-nodes svg))
'svg)))
(error "Failed to parse SVG"))
(when (and svg
edraw-mode--parse-svg--check-edraw-body
(not (dom-by-id svg "\\`edraw-body\\'")))
(error "edraw-mode can only be used for SVG generated by edraw. You may use the `edraw-convert-buffer-to-edraw-svg-xml' command at your own risk."))
svg))
;;;; Finalize Major Mode
(defun edraw-mode-finalize-major-mode ()
(edraw-mode-save)
;; Remove editor overlay
(remove-overlays (point-min) (point-max))
;; Remove read only properties
(edraw-mode--text-unlock)
;; Remove local change-major-mode-hook (Not needed?)
(remove-hook 'change-major-mode-hook 'edraw-mode-finalize-major-mode t))
;;;; Text Modification
(defvar edraw-mode--text-empty-p nil
"Non-nil means source text is empty and dummy text is added to
show the overlay using display property.")
(defun edraw-mode--text-lock ()
(with-silent-modifications
;; Empty buffers cannot use display property.
;; So if the current buffer is empty, adds a dummy character.
(setq edraw-mode--text-empty-p (= (point-min) (point-max)))
(when edraw-mode--text-empty-p
(insert "\n"))
(add-text-properties (point-min) (point-max)
(list 'read-only t
'front-sticky '(read-only)))))
(defun edraw-mode--text-unlock ()
(with-silent-modifications
(if edraw-mode--text-empty-p
(progn
(erase-buffer)
(setq edraw-mode--text-empty-p nil))
(remove-list-of-text-properties (point-min) (point-max)
'(read-only front-sticky)))))
(defun edraw-mode--text-replace (new-text)
(unless (string= (if edraw-mode--text-empty-p "" (buffer-string))
new-text)
;; Changed
(let ((inhibit-read-only t))
(if edraw-mode--text-empty-p
;; Do not record undo data for deletion of dummy text
(with-silent-modifications
(erase-buffer))
(erase-buffer))
(insert new-text)
(edraw-mode--text-lock))))
;;;; Keymap
(defvar-local edraw-mode-map nil)
(defun edraw-mode-transfer-overlay-keymap-to-local-map (editor)
(let* ((ov (edraw-overlay editor))
(keymap (overlay-get ov 'keymap)))
;; Transfer overlay's keymap property to local-map.
(setq-local edraw-mode-map keymap)
(use-local-map keymap)
(overlay-put ov 'keymap nil) ;; remove keymap property
;; Track keymap changes.
;;(edraw-add-hook editor 'keymap-change 'edraw-mode-on-keymap-change)
editor))
;; (defun edraw-mode-on-keymap-change (_editor keymap)
;; (setq-local edraw-mode-map keymap)
;; (use-local-map keymap)
;; ;; Return t. EDITOR does not change the overlay's keymap.
;; t)
;;;; Modification Tracking and Data Saving
(defconst edraw-mode-compress-file-p nil) ;;browser does not support svgz
(defun edraw-mode-on-changed (_type)
(set-buffer-modified-p t))
(defun edraw-mode-on-before-save ()
(set-buffer-file-coding-system 'utf-8) ;; SVG must be UTF-8 encoded
(edraw-mode-save))
(defun edraw-mode-write-document (svg)
(edraw-mode--text-replace (edraw-svg-encode svg nil
edraw-mode-compress-file-p)))
(defun edraw-mode-save ()
(let ((editor edraw-mode-editor))
;; Same as edraw-save, but don't use document-writer because it
;; shows a "save" item in the main menu.
(when (and editor
(edraw-modified-p editor))
(let ((doc-svg (edraw-document-svg editor t)))
(edraw-mode-write-document doc-svg)
(edraw-set-modified-p editor nil)))))
;;;; Base URI
(defun edraw-mode-on-file-name-changed ()
(edraw-mode-update-base-uri))
(defun edraw-mode-update-base-uri ()
"Update the :base-uri of the editor image.
:base-uri is the base file name when referring to external
resources such as images.
The default is the buffer file name, but buffers created with the
`edraw' command do not yet have a buffer file name.
Therefore external images will not be displayed until the buffer
is saved with a filename.
This function sets :base-uri to default-directory if the buffer
filename is nil. If non-nil invalidate the :base-uri."
(let ((editor edraw-mode-editor))
(when editor
(edraw-set-base-uri editor
(and (null (buffer-file-name))
(expand-file-name "___unnamed___.svg"))))))
;;;; Edraw Command
;;;###autoload
(defun edraw ()
(interactive)
(pop-to-buffer-same-window (generate-new-buffer "new-image.edraw.svg"))
(edraw-mode))
(provide 'edraw-mode)
;;; edraw-mode.el ends here