forked from sensorflo/adoc-mode
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathadoc-mode-image.el
More file actions
322 lines (283 loc) · 11.1 KB
/
adoc-mode-image.el
File metadata and controls
322 lines (283 loc) · 11.1 KB
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
;;; adoc-mode-image.el --- Image display for adoc-mode -*- lexical-binding: t; -*-
;;
;; Copyright 2022-2026 Bozhidar Batsov <bozhidar@batsov.dev> and adoc-mode contributors
;;
;; This file is not part of GNU Emacs.
;;
;; 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 2, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;; Image display support for adoc-mode: inline image overlays, remote image
;; fetching, and context menus. Most of this code is adapted from
;; `markdown-mode'.
;;; Code:
(require 'cl-lib)
(require 'seq)
(require 'url-parse)
(declare-function imagep "image.c")
(declare-function image-flush "image.c")
;; Defined in adoc-mode.el
(defvar adoc-max-image-size)
(defvar adoc-display-images)
(defvar-local adoc-image-overlays nil
"List of image overlays in the current buffer.")
(defcustom adoc-display-remote-images nil
"If non-nil, download and display remote images.
See also `adoc-image-overlays'.
Only image URLs specified with a protocol listed in
`adoc-remote-image-protocols' are displayed."
:group 'adoc
:type 'boolean
:package-version '(adoc-mode . "0.8.0"))
(defcustom adoc-remote-image-protocols '("https")
"List of protocols to use to download remote images.
See also `adoc-display-remote-images'."
:group 'adoc
:type '(repeat string)
:package-version '(adoc-mode . "0.8.0"))
(defvar adoc--remote-image-cache
(make-hash-table :test 'equal)
"A map from URLs to image paths.")
(defun adoc--get-remote-image (url)
"Retrieve the image path for a given URL."
(or (gethash url adoc--remote-image-cache)
(let ((dl-path (make-temp-file "adoc-mode--image")))
(require 'url)
(url-copy-file url dl-path t)
(puthash url dl-path adoc--remote-image-cache))))
(defconst adoc-re-image "\\<image::?\\([^]]+\\)\\(\\[[^]]*\\]\\)"
"Regexp matching block- and inline-images.")
(defun adoc--resolve-attribute-references (str)
"Resolve AsciiDoc attribute references in STR.
Replaces occurrences of {name} with the value defined by
`:name: value' attribute entries in the current buffer.
References without a matching definition are left unchanged."
(if (not (string-match-p "{" str))
str
(let ((attrs (make-hash-table :test 'equal)))
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(while (re-search-forward
"^:\\([a-zA-Z0-9_][^.\n]*?\\(?:\\..*?\\)?\\):[ \t]+\\(.*?\\)$"
nil t)
(puthash (match-string-no-properties 1)
(match-string-no-properties 2)
attrs))))
(replace-regexp-in-string
"{\\([^}\n]+\\)}"
(lambda (match)
(let ((name (match-string 1 match)))
(gethash name attrs match)))
str t t))))
(defvar adoc-image-overlay-functions nil
"Functions called after the creation of an image overlay.
Each function is called with the created overlay as argument.")
(defvar adoc-image-menu (make-sparse-keymap "Image")
"Common menu of image links and image overlays.")
(defvar adoc-image-link-menu
(let ((map (make-sparse-keymap "Image")))
(set-keymap-parent map adoc-image-menu)
(easy-menu-add-item map nil ["Generate Preview At Point"
(lambda (e) (interactive "e") (adoc-display-image-at e))
t])
map)
"Context menu of image links.")
(defvar adoc-image-link-map
(let ((map (make-sparse-keymap "Image")))
(define-key map (kbd "<down-mouse-3>")
(lambda () (interactive) (popup-menu adoc-image-link-menu)))
map)
"Keymap for image links.")
(fset 'adoc-image-link-map adoc-image-link-map)
(defvar adoc-image-overlay-menu
(let ((map (make-sparse-keymap "Image")))
(set-keymap-parent map adoc-image-menu)
(easy-menu-add-item map nil ["Remove Preview At Point"
(lambda (e) (interactive "e") (adoc-remove-image-overlay-at e))
t])
map)
"Context menu of image overlays.")
(defvar adoc-image-overlay-map
(let ((map (make-sparse-keymap "Image")))
(define-key map (kbd "<down-mouse-3>")
(lambda () (interactive) (popup-menu adoc-image-overlay-menu)))
map)
"Keymap for image overlays.")
(defun adoc-create-image-overlay (file start end)
"Create image overlay with START and END displaying image FILE."
(setq file (adoc--resolve-attribute-references file))
(when (not (zerop (length file)))
(unless (file-exists-p file)
(when adoc-display-remote-images
(let ((url-type (ignore-errors
(downcase (url-type (url-generic-parse-url file))))))
(when (member url-type adoc-remote-image-protocols)
(setq file (adoc--get-remote-image file))))))
(when (file-exists-p file)
(let* ((abspath (if (file-name-absolute-p file)
file
(concat default-directory file)))
(image
(if (and adoc-max-image-size
(image-type-available-p 'imagemagick))
(create-image
abspath 'imagemagick nil
:max-width (car adoc-max-image-size)
:max-height (cdr adoc-max-image-size))
(create-image abspath))))
(when image
(let ((ov (make-overlay start end)))
(overlay-put ov 'adoc-image t)
(overlay-put ov 'display image)
(overlay-put ov 'face 'default)
(overlay-put ov 'keymap adoc-image-overlay-map)
(run-hook-with-args 'adoc-image-overlay-functions ov)))))))
(defun adoc-display-images ()
"Add inline image overlays to image links in the buffer.
This can be toggled with `adoc-toggle-images'
or \\[adoc-toggle-images]."
(interactive)
(unless (display-images-p)
(error "Cannot show images"))
(adoc-remove-images)
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(while (re-search-forward adoc-re-image nil t)
(let ((start (match-beginning 0))
(end (match-end 0))
(file (match-string-no-properties 1)))
(adoc-create-image-overlay file start end)
)))))
(defun adoc-image-overlays (&optional begin end)
"Return list of image overlays in region from BEGIN to END.
BEGIN and END default to the buffer boundaries
ignoring any restrictions."
(save-restriction
(widen)
(unless begin (setq begin (point-min)))
(unless end (setq end (point-max)))
(let (image-overlays)
(dolist (ov (overlays-in begin end))
(when (overlay-get ov 'adoc-image)
(push ov image-overlays)))
image-overlays)))
(defun adoc-remove-images ()
"Remove image overlays from image links in the buffer.
This can be toggled with `adoc-toggle-images'
or \\[adoc-toggle-images]."
(interactive)
(let ((image-list (adoc-image-overlays)))
(when image-list
(dolist (ov image-list)
(delete-overlay ov))
t)))
(defun adoc-toggle-images ()
"Toggle the display of images."
(interactive)
(unless (adoc-remove-images)
(adoc-display-images)
))
(defmacro adoc-with-point-at-event (location &rest body)
"Execute BODY like `progn'.
If LOCATION is an event run BODY in the event's buffer
and set LOCATION to the event position.
If LOCATION is number or marker just run BODY with
LOCATION untouched.
Otherwise set LOCATION to current point."
(declare (indent 1) (debug (symbolp form body)))
(let ((posn (make-symbol "posn")))
`(if (mouse-event-p ,location)
(let* ((,posn (event-start ,location)))
(with-current-buffer (window-buffer (posn-window ,posn))
(setq ,location (posn-point ,posn))
,@body))
(unless (number-or-marker-p ,location)
(setq ,location (point)))
,@body)))
(defun adoc-bounds-of-image-link-at (&optional location)
"Get bounds of image link at LOCATION.
Return \\(START . END) giving the start and the end
positions of the image link found.
If an image link is identified `match-data' is set as follows:
0. whole link inclusive \"image::?\" and attributes
1. image path
2. bracketed attribute list."
(adoc-with-point-at-event location
(save-excursion
(when location (goto-char location))
(setq location (point))
(if (looking-at "[[:alpha:]]*::?\\_<")
(skip-chars-backward "[:alpha:]")
(re-search-backward "\\_<image:" (line-beginning-position) t))
(when (looking-at adoc-re-image)
(cons (match-beginning 0) (match-end 0))))))
(cl-defstruct adoc-image-link
"Data from an image link."
begin end uri begin-uri end-uri begin-attributes end-attributes)
(defun adoc-image-link-at (&optional location)
"Return image link at LOCATION if there is one.
LOCATION can be a buffer position or a mouse event.
It defaults to \\(point)."
(adoc-with-point-at-event location
(when (adoc-bounds-of-image-link-at location)
(make-adoc-image-link
:begin (match-beginning 0)
:end (match-end 0)
:begin-uri (match-beginning 1)
:end-uri (match-end 1)
:begin-attributes (match-beginning 2)
:end-attributes (match-end 2)
:uri (match-string 1)))))
(put 'adoc-image 'bounds-of-thing-at-point #'adoc-bounds-of-image-link-at)
(defun adoc-display-image-at (&optional location)
"Create image overlay at LOCATION.
LOCATION can be a buffer position like `point'
or an event. It defaults to \\(point)."
(interactive "d")
(adoc-with-point-at-event location
(when-let* ((link (adoc-image-link-at location)))
(adoc-create-image-overlay
(adoc-image-link-uri link)
(adoc-image-link-begin link)
(adoc-image-link-end link)
))))
(defun adoc-image-overlay-at (location)
"Get image overlay at LOCATION."
(adoc-with-point-at-event location
(seq-find (lambda (ov) (overlay-get ov 'adoc-image))
(overlays-in (1- location) (1+ location)))))
(defun adoc-remove-image-overlay-at (&optional location flush)
"Delete overlay at LOCATION.
POINT defaults to `point'.
POINT can also be a mouse event.
If FLUSH is non-nil also flush the cache for this image."
(interactive "d")
(adoc-with-point-at-event location
(let ((ov (adoc-image-overlay-at location)))
(when ov
(when-let* ((image (and flush (overlay-get ov 'display)))
((imagep image)))
(image-flush image))
(delete-overlay ov)))))
(provide 'adoc-mode-image)
;; Local Variables:
;; indent-tabs-mode: nil
;; coding: utf-8
;; End:
;;; adoc-mode-image.el ends here