Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / gnus / gnus-html.el
blob5d07a823f618c2221cd1d4f2148e6ad37aacc2fe
1 ;;; gnus-html.el --- Render HTML in a buffer.
3 ;; Copyright (C) 2010-2018 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html, web
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; The idea is to provide a simple, fast and pretty minimal way to
26 ;; render HTML (including links and images) in a buffer, based on an
27 ;; external HTML renderer (i.e., w3m).
29 ;;; Code:
31 (eval-when-compile (require 'cl))
33 (require 'gnus-art)
34 (eval-when-compile (require 'mm-decode))
36 (require 'mm-url)
37 (require 'url)
38 (require 'url-cache)
39 (require 'xml)
40 (require 'browse-url)
41 (require 'mm-util)
42 (require 'help-fns)
43 (require 'url-queue)
45 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
46 "Time used to determine if we should use images from the cache."
47 :version "24.1"
48 :group 'gnus-art
49 ;; FIXME hardly the friendliest type. The allowed value is actually
50 ;; any time value, but we are assuming no-one cares about USEC and
51 ;; PSEC here. It would be better to eg make it a number of minutes.
52 :type '(list integer integer))
54 (defcustom gnus-html-image-automatic-caching t
55 "Whether automatically cache retrieve images."
56 :version "24.1"
57 :group 'gnus-art
58 :type 'boolean)
60 (defcustom gnus-html-frame-width 70
61 "What width to use when rendering HTML."
62 :version "24.1"
63 :group 'gnus-art
64 :type 'integer)
66 (defcustom gnus-max-image-proportion 0.9
67 "How big pictures displayed are in relation to the window they're in.
68 A value of 0.7 means that they are allowed to take up 70% of the
69 width and height of the window. If they are larger than this,
70 and Emacs supports it, then the images will be rescaled down to
71 fit these criteria."
72 :version "24.1"
73 :group 'gnus-art
74 :type 'float)
76 (defvar gnus-html-image-map
77 (let ((map (make-sparse-keymap)))
78 (define-key map "u" 'gnus-article-copy-string)
79 (define-key map "i" 'gnus-html-insert-image)
80 (define-key map "v" 'gnus-html-browse-url)
81 map))
83 (defvar gnus-html-displayed-image-map
84 (let ((map (make-sparse-keymap)))
85 (define-key map "a" 'gnus-html-show-alt-text)
86 (define-key map "i" 'gnus-html-browse-image)
87 (define-key map "\r" 'gnus-html-browse-url)
88 (define-key map "u" 'gnus-article-copy-string)
89 (define-key map [tab] 'widget-forward)
90 map))
92 (defun gnus-html-encode-url (url)
93 "Encode URL."
94 (browse-url-url-encode-chars url "[)$ ]"))
96 (defun gnus-html-cache-expired (url ttl)
97 "Check if URL is cached for more than TTL."
98 (cond (url-standalone-mode
99 (not (file-exists-p (url-cache-create-filename url))))
100 (t (let ((cache-time (url-is-cached url)))
101 (if cache-time
102 (time-less-p (time-add cache-time ttl) nil)
103 t)))))
105 ;;;###autoload
106 (defun gnus-article-html (&optional handle)
107 (let ((article-buffer (current-buffer)))
108 (unless handle
109 (setq handle (mm-dissect-buffer t)))
110 (save-restriction
111 (narrow-to-region (point) (point))
112 (save-excursion
113 (mm-with-part handle
114 (let* ((coding-system-for-read 'utf-8)
115 (coding-system-for-write 'utf-8)
116 (default-process-coding-system
117 (cons coding-system-for-read coding-system-for-write))
118 (charset (mail-content-type-get (mm-handle-type handle)
119 'charset)))
120 (when (and charset
121 (setq charset (mm-charset-to-coding-system
122 charset nil t))
123 (not (eq charset 'ascii)))
124 (insert (prog1
125 (decode-coding-string (buffer-string) charset)
126 (erase-buffer)
127 (mm-enable-multibyte))))
128 (call-process-region (point-min) (point-max)
129 "w3m"
130 nil article-buffer nil
131 "-halfdump"
132 "-no-cookie"
133 "-I" "UTF-8"
134 "-O" "UTF-8"
135 "-o" "ext_halfdump=1"
136 "-o" "display_ins_del=2"
137 "-o" "pre_conv=1"
138 "-t" (format "%s" tab-width)
139 "-cols" (format "%s" gnus-html-frame-width)
140 "-o" "display_image=on"
141 "-T" "text/html"))))
142 (gnus-html-wash-tags))))
144 (defvar gnus-article-mouse-face)
146 (defun gnus-html-pre-wash ()
147 (goto-char (point-min))
148 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
149 (replace-match "" t t))
150 (goto-char (point-min))
151 (while (re-search-forward "<a name[^\n>]+>" nil t)
152 (replace-match "" t t)))
154 (defun gnus-html-wash-images ()
155 "Run through current buffer and replace img tags by images."
156 (let (tag parameters string start end images url alt-text
157 inhibit-images blocked-images)
158 (if (buffer-live-p gnus-summary-buffer)
159 (with-current-buffer gnus-summary-buffer
160 (setq inhibit-images gnus-inhibit-images
161 blocked-images (gnus-blocked-images)))
162 (setq inhibit-images gnus-inhibit-images
163 blocked-images (gnus-blocked-images)))
164 (goto-char (point-min))
165 ;; Search for all the images first.
166 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
167 (setq parameters (match-string 1)
168 start (match-beginning 0))
169 (delete-region start (point))
170 (when (search-forward "</img_alt>" (line-end-position) t)
171 (delete-region (match-beginning 0) (match-end 0)))
172 (setq end (point))
173 (when (string-match "src=\"\\([^\"]+\\)" parameters)
174 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
175 (setq url (gnus-html-encode-url (match-string 1 parameters))
176 alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
177 parameters)
178 (xml-substitute-special (match-string 2 parameters))))
179 (add-text-properties
180 start end
181 (list 'image-url url
182 'image-displayer `(lambda (url start end)
183 (gnus-html-display-image url start end
184 ,alt-text))
185 'gnus-image (list url start end alt-text)))
186 (widget-convert-button
187 'url-link start (point)
188 :help-echo alt-text
189 :keymap gnus-html-image-map
190 url)
191 (if (string-match "\\`cid:" url)
192 ;; URLs with cid: have their content stashed in other
193 ;; parts of the MIME structure, so just insert them
194 ;; immediately.
195 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
196 (image (when (and handle
197 (not inhibit-images))
198 (gnus-create-image
199 (mm-with-part handle (buffer-string))
200 nil t))))
201 (if image
202 (gnus-add-image
203 'cid
204 (gnus-put-image
205 (gnus-rescale-image
206 image (gnus-html-maximum-image-size))
207 (gnus-string-or (prog1
208 (buffer-substring start end)
209 (delete-region start end))
210 "*")
211 'cid))
212 (widget-convert-button
213 'link start end
214 :action 'gnus-html-insert-image
215 :help-echo url
216 :keymap gnus-html-image-map
217 :button-keymap gnus-html-image-map)))
218 ;; Normal, external URL.
219 (if (or inhibit-images
220 (gnus-html-image-url-blocked-p url blocked-images))
221 (widget-convert-button
222 'link start end
223 :action 'gnus-html-insert-image
224 :help-echo url
225 :keymap gnus-html-image-map
226 :button-keymap gnus-html-image-map)
227 ;; Non-blocked url
228 (let ((width
229 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
230 (string-to-number (match-string 1 parameters))))
231 (height
232 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
233 (string-to-number (match-string 1 parameters)))))
234 ;; Don't fetch images that are really small. They're
235 ;; probably tracking pictures.
236 (when (and (or (null height)
237 (> height 4))
238 (or (null width)
239 (> width 4)))
240 (gnus-html-display-image url start end alt-text)))))))))
242 (defun gnus-html-display-image (url start end &optional alt-text)
243 "Display image at URL on text from START to END.
244 Use ALT-TEXT for the image string."
245 (or alt-text (setq alt-text "*"))
246 (if (string-match "\\`cid:" url)
247 (let ((handle (mm-get-content-id (substring url (match-end 0)))))
248 (when handle
249 (gnus-html-put-image (mm-with-part handle (buffer-string))
250 url alt-text)))
251 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
252 ;; We don't have it, so schedule it for fetching
253 ;; asynchronously.
254 (gnus-html-schedule-image-fetching
255 (current-buffer)
256 (list url alt-text))
257 ;; It's already cached, so just insert it.
258 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
260 (defun gnus-html-wash-tags ()
261 (let (tag parameters string start end images url)
262 (gnus-html-pre-wash)
263 (gnus-html-wash-images)
265 (goto-char (point-min))
266 ;; Then do the other tags.
267 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
268 (setq tag (match-string 1)
269 parameters (match-string 2)
270 start (match-beginning 0))
271 (when (> (length parameters) 0)
272 (set-text-properties 0 (1- (length parameters)) nil parameters))
273 (delete-region start (point))
274 (when (search-forward (concat "</" tag ">") nil t)
275 (delete-region (match-beginning 0) (match-end 0)))
276 (setq end (point))
277 (cond
278 ;; Fetch and insert a picture.
279 ((equal tag "img_alt"))
280 ;; Add a link.
281 ((or (equal tag "a")
282 (equal tag "A"))
283 (when (string-match "href=\"\\([^\"]+\\)" parameters)
284 (setq url (match-string 1 parameters))
285 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
286 (gnus-article-add-button start end
287 'browse-url (mm-url-decode-entities-string url)
288 url)
289 (let ((overlay (make-overlay start end)))
290 (overlay-put overlay 'evaporate t)
291 (overlay-put overlay 'gnus-button-url url)
292 (put-text-property start end 'gnus-string url)
293 (when gnus-article-mouse-face
294 (overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
295 ;; The upper-case IMG_ALT is apparently just an artifact that
296 ;; should be deleted.
297 ((equal tag "IMG_ALT")
298 (delete-region start end))
299 ;; w3m does not normalize the case
300 ((or (equal tag "b")
301 (equal tag "B"))
302 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-bold))
303 ((or (equal tag "u")
304 (equal tag "U"))
305 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
306 ((or (equal tag "i")
307 (equal tag "I"))
308 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-italic))
309 ((or (equal tag "s")
310 (equal tag "S"))
311 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-strikethru))
312 ((or (equal tag "ins")
313 (equal tag "INS"))
314 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
315 ;; Handle different UL types
316 ((equal tag "_SYMBOL")
317 (when (string-match "TYPE=\\(.+\\)" parameters)
318 (let ((type (string-to-number (match-string 1 parameters))))
319 (delete-region start end)
320 (cond ((= type 33) (insert " "))
321 ((= type 34) (insert " "))
322 ((= type 35) (insert " "))
323 ((= type 36) (insert " "))
324 ((= type 37) (insert " "))
325 ((= type 38) (insert " "))
326 ((= type 39) (insert " "))
327 ((= type 40) (insert " "))
328 ((= type 42) (insert " "))
329 ((= type 43) (insert " "))
330 (t (insert " "))))))
331 ;; Whatever. Just ignore the tag.
334 (goto-char start))
335 (goto-char (point-min))
336 ;; The output from -halfdump isn't totally regular, so strip
337 ;; off any </pre_int>s that were left over.
338 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
339 (replace-match "" t t))
340 (mm-url-decode-entities)))
342 (defun gnus-html-insert-image (&rest args)
343 "Fetch and insert the image under point."
344 (interactive)
345 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
347 (defun gnus-html-show-alt-text ()
348 "Show the ALT text of the image under point."
349 (interactive)
350 (message "%s" (get-text-property (point) 'gnus-alt-text)))
352 (defun gnus-html-browse-image ()
353 "Browse the image under point."
354 (interactive)
355 (browse-url (get-text-property (point) 'image-url)))
357 (defun gnus-html-browse-url ()
358 "Browse the image under point."
359 (interactive)
360 (let ((url (get-text-property (point) 'gnus-string)))
361 (cond
362 ((not url)
363 (message "No link under point"))
364 ((string-match "^mailto:" url)
365 (gnus-url-mailto url))
367 (browse-url url)))))
369 (defun gnus-html-schedule-image-fetching (buffer image)
370 "Retrieve IMAGE, and place it into BUFFER on arrival."
371 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
372 buffer image)
373 (url-queue-retrieve (car image)
374 'gnus-html-image-fetched
375 (list buffer image) t t))
377 (defun gnus-html-image-fetched (status buffer image)
378 "Callback function called when image has been fetched."
379 (unless (plist-get status :error)
380 (when (and (or (search-forward "\n\n" nil t)
381 (search-forward "\r\n\r\n" nil t))
382 (not (eobp)))
383 (when gnus-html-image-automatic-caching
384 (url-store-in-cache (current-buffer)))
385 (when (buffer-live-p buffer)
386 (let ((data (buffer-substring (point) (point-max))))
387 (with-current-buffer buffer
388 (let ((inhibit-read-only t))
389 (gnus-html-put-image data (car image) (cadr image))))))))
390 (kill-buffer (current-buffer)))
392 (defun gnus-html-get-image-data (url)
393 "Get image data for URL.
394 Return a string with image data."
395 (with-temp-buffer
396 (mm-disable-multibyte)
397 (url-cache-extract (url-cache-create-filename url))
398 (when (or (search-forward "\n\n" nil t)
399 (search-forward "\r\n\r\n" nil t))
400 (buffer-substring (point) (point-max)))))
402 (defun gnus-html-maximum-image-size ()
403 "Return the maximum size of an image according to `gnus-max-image-proportion'."
404 (let ((edges (window-inside-pixel-edges
405 (get-buffer-window (current-buffer)))))
406 ;; (width . height)
407 (cons
408 ;; Aimed width
409 (truncate
410 (* gnus-max-image-proportion
411 (- (nth 2 edges) (nth 0 edges))))
412 ;; Aimed height
413 (truncate (* gnus-max-image-proportion
414 (- (nth 3 edges) (nth 1 edges)))))))
416 ;; Behind display-graphic-p test.
417 (declare-function image-size "image.c" (spec &optional pixels frame))
419 (defun gnus-html-put-image (data url &optional alt-text)
420 "Put an image with DATA from URL and optional ALT-TEXT."
421 (when (display-graphic-p)
422 (let* ((start (text-property-any (point-min) (point-max)
423 'image-url url))
424 (end (when start
425 (next-single-property-change start 'image-url))))
426 ;; Image found?
427 (when start
428 (let* ((image
429 (ignore-errors
430 (gnus-create-image data nil t)))
431 (size (and image (image-size image t))))
432 (save-excursion
433 (goto-char start)
434 (let ((alt-text (or alt-text
435 (buffer-substring-no-properties start end)))
436 (inhibit-read-only t))
437 (if (and image
438 ;; Kludge to avoid displaying 30x30 gif images, which
439 ;; seems to be a signal of a broken image.
440 (not (and (listp image)
441 (eq (plist-get (cdr image) :type)
442 'gif)
443 (= (car size) 30)
444 (= (cdr size) 30))))
445 ;; Good image, add it!
446 (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
447 (delete-region start end)
448 (gnus-put-image image alt-text 'external)
449 (widget-convert-button
450 'url-link start (point)
451 :help-echo alt-text
452 :keymap gnus-html-displayed-image-map
453 url)
454 (put-text-property start (point) 'gnus-alt-text alt-text)
455 (when url
456 (add-text-properties
457 start (point)
458 `(image-url
459 ,url
460 image-displayer
461 (lambda (url start end)
462 (gnus-html-display-image url start end
463 ,alt-text)))))
464 (gnus-add-image 'external image)
466 ;; Bad image, try to show something else
467 (when (fboundp 'find-image)
468 (delete-region start end)
469 (setq image (find-image
470 '((:type xpm :file "lock-broken.xpm"))))
471 (gnus-put-image image alt-text 'internal)
472 (gnus-add-image 'internal image))
473 nil))))))))
475 (defun gnus-html-image-url-blocked-p (url blocked-images)
476 "Find out if URL is blocked by BLOCKED-IMAGES."
477 (let ((ret (and blocked-images
478 (string-match blocked-images url))))
479 (if ret
480 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
481 url blocked-images)
482 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
483 url blocked-images))
484 ret))
486 ;;;###autoload
487 (defun gnus-html-prefetch-images (summary)
488 (when (buffer-live-p summary)
489 (let (inhibit-images blocked-images)
490 (with-current-buffer summary
491 (setq inhibit-images gnus-inhibit-images
492 blocked-images (gnus-blocked-images)))
493 (save-match-data
494 (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
495 (let ((url (gnus-html-encode-url
496 (mm-url-decode-entities-string (match-string 1)))))
497 (unless (or inhibit-images
498 (gnus-html-image-url-blocked-p url blocked-images))
499 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
500 (gnus-html-schedule-image-fetching nil
501 (list url))))))))))
503 (provide 'gnus-html)
505 ;;; gnus-html.el ends here