-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathedraw-org-edit.el
274 lines (231 loc) · 11.3 KB
/
edraw-org-edit.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
;;; edraw-org-edit.el --- Edit edraw link in Org-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2022 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <misohena@gmail.com>
;; Keywords: Graphics, Drawing, SVG, Editor, Orgmode
;; 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:
;;
;;; Code:
(require 'edraw)
(require 'edraw-org)
(declare-function image-flush "image.c")
;;;; Edit edraw link inline
(defvar edraw-org-enable-modification nil)
;;@todo Use edraw-edit-svg?
;;;###autoload
(defun edraw-org-edit-link (&optional _path _arg)
"Edit the `edraw:' link at point.
_PATH and _ARG are not used. These are passed in when called from
org-link, but since these pieces of information aren't quite
enough, this function parses the link at the current position on
its own."
(interactive)
(require 'edraw)
;; Get link object & link properties
(when-let* ((link-object (edraw-org-link-at-point))
(link-props-place-type (edraw-org-link-object-link-properties
link-object nil)))
(let* ((link-begin (org-element-property :begin link-object))
(link-end (org-element-property :end link-object))
(link-props (nth 0 link-props-place-type)))
;; Make sure the editor overlay doesn't exist yet
(when (edraw-editor-overlays-in link-begin link-end)
(error "Editor already exists"))
;; Hide inline link image if it exists
(when-let* ((image-overlay (edraw-org-link-image-overlay-at link-begin)))
(edraw-org-link-image-set-visible image-overlay nil))
;; Remove mouse-face text property
(edraw-org-link-remove-mouse-face link-begin link-end)
;; Create editor
(let* ((editor-overlay (make-overlay link-begin link-end nil t nil))
(_editor (edraw-editor
:overlay editor-overlay
:svg (edraw-org-link-load-svg link-props t)
:document-writer (edraw-org-link-make-writer
editor-overlay
edraw-org-link-compress-data-p
edraw-org-link-compress-file-p)
:document-writer-accepts-top-level-comments-p t
:menu-filter #'edraw-org-link-editor-menu-filter
:keymap (edraw-org-link-editor-make-keymap
edraw-editor-map))))
;;(overlay-put editor-overlay 'evaporate t)
(overlay-put editor-overlay 'modification-hooks
(list (lambda (_ov _after-p _beg _end &optional _len)
(unless edraw-org-enable-modification
(error "There is an edraw-editor within modification range. Please close the editor"))))))
;; Hook kill buffer
(add-hook 'kill-buffer-query-functions 'edraw-buffer-kill-query nil t)
(message "%s" (substitute-command-keys "\\[edraw-org-link-finish-edit]:Finish Edit, \\[edraw-org-link-cancel-edit]:Cancel Edit")))))
(defun edraw-org-link-load-svg (link-props
&optional accepts-top-level-comments-p)
(if-let* ((data (edraw-org-link-prop-data link-props)))
(edraw-svg-decode-svg data t accepts-top-level-comments-p)
(if-let* ((file (edraw-org-link-prop-file link-props)))
(if (file-exists-p file)
(edraw-svg-read-from-file file accepts-top-level-comments-p)))))
(defun edraw-org-link-make-writer (editor-overlay data-gzip-p file-gzip-p)
(lambda (svg)
(edraw-org-link-save-svg editor-overlay svg data-gzip-p file-gzip-p)))
(defun edraw-org-link-save-svg (editor-overlay svg data-gzip-p file-gzip-p)
;; Move to beginning of editing link
(let ((buffer (overlay-buffer editor-overlay)))
(unless buffer
(error "The editor's overlay has been removed"))
(with-current-buffer buffer
(save-excursion
(goto-char (overlay-start editor-overlay))
;; Get link object & parse properties
(let* ((link-object
(or (edraw-org-link-at-point)
(error "The edraw link currently being edited has been lost")))
(link-begin (org-element-property :begin link-object))
(link-end (org-element-property :end link-object))
(link-props-place-type
(or (edraw-org-link-object-link-properties link-object t)
(error "The type of the editing link is not `edraw:'")))
(link-props (nth 0 link-props-place-type))
(in-description-p (nth 1 link-props-place-type)))
(if-let* ((file-path (edraw-org-link-prop-file link-props)))
;; file
(progn
(edraw-svg-write-to-file svg file-path file-gzip-p) ;;signal an error
;; Update inline image
(image-flush (edraw-org-link-image-create link-props)) ;;update image if overlay already exists
(edraw-org-link-image-update link-begin link-end link-object) ;;create a new overlay if not exists
t)
;;data
(setf (alist-get "data" link-props nil nil #'string=)
(edraw-svg-encode svg t data-gzip-p))
(let ((edraw-org-enable-modification t)) ;; call modification hooks(inhibit-modification-hooks=nil) but allow modification. If inhibit-modification-hooks is t, inline images not updated.
(unless (edraw-org-link-replace-object
link-object ;; LINK-OBJECT is invalid after the call
(concat edraw-org-link-type ":"
(edraw-org-link-props-to-string link-props))
(if in-description-p 'description 'path))
(error "Failed to replace edraw link")))
t))))))
(defun edraw-org-link-editor-menu-filter (menu-type items)
(pcase menu-type
('main-menu
(append
items
'(((edraw-msg "Finish Edit") edraw-org-link-finish-edit)
((edraw-msg "Cancel Edit") edraw-org-link-cancel-edit))))
(_ items)))
(defun edraw-org-link-editor-make-keymap (original-keymap)
(let ((km (make-sparse-keymap)))
(set-keymap-parent km original-keymap)
(define-key km (kbd "C-c C-c") 'edraw-org-link-finish-edit)
(define-key km (kbd "C-c C-k") 'edraw-org-link-cancel-edit)
km))
(defun edraw-org-link-finish-edit (&optional editor)
(interactive)
(let ((editor (or editor (edraw-current-editor))))
(when (or (not (edraw-modified-p editor))
(condition-case err
(edraw-save editor)
(error
(message "Error=%s" (prin1-to-string err))
(yes-or-no-p
(format
(edraw-msg "Failed to save. %s. Discard changes?")
(error-message-string err))))))
(edraw-org-link-close-editor editor))))
(defun edraw-org-link-cancel-edit (&optional editor)
(interactive)
(when-let* ((editor (or editor (edraw-current-editor))))
(when (or (null (edraw-modified-p editor))
(yes-or-no-p (edraw-msg "Discard changes?")))
(edraw-org-link-close-editor editor))))
(defun edraw-org-link-close-editor (editor)
;; show link image
(when-let* ((editor-overlay (oref editor overlay))
(buffer (overlay-buffer editor-overlay)))
(with-current-buffer buffer
(save-excursion
;; Recover inline image
(when-let* ((image-overlay (edraw-org-link-image-overlay-at
(overlay-start editor-overlay))))
(edraw-org-link-image-set-visible image-overlay t))
;; Recover mouse-face
(edraw-org-link-recover-mouse-face (overlay-start editor-overlay)
(overlay-end editor-overlay)))))
;; delete editor overlay
(edraw-close editor))
;;;; Edit regular file link inline
;;;###autoload
(defun edraw-org-edit-regular-file-link ()
"Edit the `file:' link to the .edraw.svg file at point.
There is a `[[edraw:file=somefile.edraw.svg]]' format for embedding
an external SVG file in an Org document, but if you don't want to
use `edraw:' link type and want to use the regular `file:' link type
([[file:somefile.edraw.svg]] format), this function might help."
(interactive)
(let* ((link (or (org-element-context)
(error (edraw-msg "No link at point"))))
(type (org-element-property :type link))
(path (org-element-property :path link))
(beg (org-element-property :begin link))
(end (org-element-property :end link)))
(unless (equal type "file")
(error (edraw-msg "The link at point is not of type `file:'")))
(unless (string-suffix-p ".edraw.svg" path t)
(error (edraw-msg "The extension is not .edraw.svg")))
(when (edraw-editor-overlays-in beg end)
(error "Editor already exists"))
;;Remove org-mode inline image
(edraw-org--remove-org-inline-images beg end)
(edraw-edit-svg (when (file-exists-p path)
(edraw-svg-read-from-file path t))
'edraw-svg
beg end
(lambda (_ok _svg)
;; Restore org-mode inline image
;;@todo Add settings?
(org-display-inline-images nil t beg end))
(lambda (svg)
(edraw-svg-write-to-file svg path nil)
t)
;; Keep file's top-level comments
t)))
(defun edraw-org--remove-org-inline-images (beg end)
(if (version<= "9.6" (org-version))
(with-no-warnings
(org-remove-inline-images beg end)) ;; Can pass BEG and END
;; Can't pass BEG and END in 9.5 or earlier
(dolist (ov (overlays-in beg end))
(when (memq ov org-inline-image-overlays)
(setq org-inline-image-overlays (delq ov org-inline-image-overlays))
(delete-overlay ov)))))
;;;; Link Tools
;;;###autoload
(defun edraw-org-link-copy-contents-at-point ()
"Copies the contents of the link at point.
Copies all shapes inside the data to the clipboard. Copied shapes
can be pasted in the editor or in the shape picker (custom shape list)."
(interactive)
(let* ((link-object (edraw-org-link-at-point))
(props (car (edraw-org-link-object-link-properties link-object nil t)))
(svg (edraw-org-link-load-svg props t)))
(unless link-object
(error (edraw-msg "No link at point")))
(unless svg
(error (edraw-msg "Link at point does not contain valid data")))
(edraw-clipboard-set
'shape-descriptor-list
(cl-loop for node in (dom-children (edraw-get-document-body svg))
for desc = (edraw-shape-descriptor-from-svg-element-without-editor
node)
when desc collect desc))))
(provide 'edraw-org-edit)
;;; edraw-org-edit.el ends here