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