-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathorg-remoteimg.el
227 lines (211 loc) · 10.2 KB
/
org-remoteimg.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
;;; org-remoteimg.el --- Display remote inline images in org-mode -*- lexical-binding: t -*-
;; Copyright (C) 2023 Dean Gao - MIT License
;; Author: Dean Gao <gao.dean@hotmail.com>
;; Description: Inline display of remote images in org-mode
;; Homepage: https://github.com/gaoDean/org-imgtog
;; Package-Requires: ((emacs "25.1"))
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;; This package displays remote images inline in org-mode with automatic caching.
;; This means you can do [[https://my-file.png]], and have it display inline.
;; The next time you visit the file or fetch the image, it will be instantly
;; fetched from the cache.
;;; Code:
(require 'org)
(require 'org-element)
(require 'url)
(require 'url-cache)
(unless (fboundp 'image-supported-file-p)
;; `image-supported-file-p' isn't available before Emacs 28
;; Add alias to not break Emacs <28.
(defalias 'image-type-from-file-name 'image-supported-file-p))
;; Compatibility definitions for Org-mode < 9.8
(unless (boundp 'org-link-preview-overlays)
(defalias 'org-inline-image-overlays 'org-link-preview-overlays))
(defun org-image-update-overlay (file link &optional data-p refresh)
"Create image overlay for FILE associtated with org-element LINK.
If DATA-P is non-nil FILE is not a file name but a string with the image data.
If REFRESH is non-nil don't download the file but refresh the image.
See also `create-image'.
This function is almost a duplicate of a part of `org-display-inline-images'."
(when (or data-p (file-exists-p file))
(let ((width
;; Apply `org-image-actual-width' specifications.
(cond
((eq org-image-actual-width t) nil)
((listp org-image-actual-width)
(or
;; First try to find a width among
;; attributes associated to the paragraph
;; containing link.
(let ((paragraph
(let ((e link))
(while (and (setq e (org-element-property
:parent e))
(not (eq (org-element-type e)
'paragraph))))
e)))
(when paragraph
(save-excursion
(goto-char (org-element-property :begin paragraph))
(when
(re-search-forward
"^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
(org-element-property
:post-affiliated paragraph)
t)
(string-to-number (match-string 1))))))
;; Otherwise, fall-back to provided number.
(car org-image-actual-width)))
((numberp org-image-actual-width)
org-image-actual-width)))
(old (get-char-property-and-overlay
(org-element-property :begin link)
'org-image-overlay)))
(if (and (car-safe old) refresh)
(image-flush (overlay-get (cdr old) 'display))
(let ((image (create-image file
(and (image-type-available-p 'imagemagick)
width
'imagemagick)
data-p
:width width)))
(when image
(let* ((link
;; If inline image is the description
;; of another link, be sure to
;; consider the latter as the one to
;; apply the overlay on.
(let ((parent
(org-element-property :parent link)))
(if (eq (org-element-type parent) 'link)
parent
link)))
(ov (make-overlay
(org-element-property :begin link)
(progn
(goto-char
(org-element-property :end link))
(skip-chars-backward " \t")
(point)))))
(overlay-put ov 'display image)
(overlay-put ov 'face 'default)
(overlay-put ov 'org-image-overlay t)
(overlay-put
ov 'modification-hooks
(list 'org-display-inline-remove-overlay))
(push ov org-link-preview-overlays)
ov)))))))
(defun org-display-user-inline-images (&optional _include-linked _refresh beg end)
"Like `org-display-inline-images' but for image data links.
_INCLUDE-LINKED and _REFRESH are ignored.
Restrict to region between BEG and END if both are non-nil.
Image data links have a :image-data-fun parameter.
\(See `org-link-set-parameters'.)
The value of the :image-data-fun parameter is a function
taking the PROTOCOL, the LINK, and the DESCRIPTION as arguments.
If that function returns nil the link is not interpreted as image.
Otherwise the return value is the image data string to be displayed.
Note that only bracket links are allowed as image data links
with one of the formats
[[PROTOCOL:LINK]]
or
[[PROTOCOL:LINK][DESCRIPTION]]
are recognized.
Full credit goes to org-yt by Tobias Zawada for this function."
(interactive)
(when (and (called-interactively-p 'any)
(use-region-p))
(setq beg (region-beginning)
end (region-end)))
(when (display-graphic-p)
(org-with-wide-buffer
(goto-char (or beg (point-min)))
(when-let* ((image-data-link-parameters
(cl-loop for link-par-entry in org-link-parameters
with fun
when (setq fun (plist-get (cdr link-par-entry) :image-data-fun))
collect (cons (car link-par-entry) fun)))
(image-data-link-re (regexp-opt (mapcar 'car image-data-link-parameters)))
(re (format "\\[\\[\\(%s\\):\\([^]]+\\)\\]\\(?:\\[\\([^]]+\\)\\]\\)?\\]"
image-data-link-re)))
(while (re-search-forward re end t)
(let* ((protocol (match-string-no-properties 1))
(link (match-string-no-properties 2))
(description (match-string-no-properties 3))
(image-data-link (assoc-string protocol image-data-link-parameters))
(el (save-excursion (goto-char (match-beginning 1)) (org-element-context)))
image-data)
(when el
(setq image-data
(or (let ((old (get-char-property-and-overlay
(org-element-property :begin el)
'org-image-overlay)))
(and old
(car-safe old)
(overlay-get (cdr old) 'display)))
(funcall (cdr image-data-link) protocol link description)))
(when image-data
(let ((ol (org-image-update-overlay image-data el t t)))
(when (and ol description)
(overlay-put ol 'after-string description)))))))))))
(defun org-remoteimg--fetch-image (protocol link _description)
"Synchronously retrieve image from cache or web."
(when (and (image-supported-file-p link)
(not (eq org-display-remote-inline-images 'skip)))
(let* ((cache (eq org-display-remote-inline-images 'cache))
(url-automatic-caching (if cache
t
url-automatic-caching))
(url (concat protocol ":" link))
(silent-output (file-exists-p (url-cache-create-filename url))))
(if-let* ((buf (url-retrieve-synchronously url
silent-output
nil
30)))
(with-current-buffer buf
(goto-char (point-min))
(re-search-forward "\r?\n\r?\n" nil t)
(buffer-substring-no-properties (point) (point-max)))
(error "Download of image \"%s\" failed" link)
nil))))
(defun org-link-preview-http (ov _path link)
"Generate preview OV for LINK in PATH."
(when-let* ((raw-link (org-element-property :raw-link link))
(image-type (image-supported-file-p raw-link))
;; cache?
(image-buffer (org-remoteimg--fetch-image nil raw-link nil)))
(overlay-put ov 'display (create-image image-buffer
;; scale?
;; (and (image-type-available-p 'imagemagick)
;; width
;; 'imagemagick)
;; :width width
image-type
t))
t))
(if (fboundp 'org-display-inline-images)
(progn
(advice-add #'org-display-inline-images :after #'org-display-user-inline-images)
(org-link-set-parameters "http" :image-data-fun #'org-remoteimg--fetch-image)
(org-link-set-parameters "https" :image-data-fun #'org-remoteimg--fetch-image))
(org-link-set-parameters "http" :preview #'org-link-preview-http)
(org-link-set-parameters "https" :preview #'org-link-preview-http))
(provide 'org-remoteimg)
;;; org-remoteimg.el ends here