gnus-html.el: Rescale images in article buffers for Emacs versions that support this...
[bpt/emacs.git] / lisp / gnus / gnus-html.el
1 ;;; gnus-html.el --- Quoted-Printable functions
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html, web
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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).
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'mm-decode))
33 (require 'mm-url)
34
35 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
36 "Where Gnus will cache images it downloads from the web."
37 :version "24.1"
38 :group 'gnus-art
39 :type 'directory)
40
41 (defcustom gnus-html-cache-size 500000000
42 "The size of the Gnus image cache."
43 :version "24.1"
44 :group 'gnus-art
45 :type 'integer)
46
47 (defcustom gnus-html-frame-width 70
48 "What width to use when rendering HTML."
49 :version "24.1"
50 :group 'gnus-art
51 :type 'integer)
52
53 (defcustom gnus-blocked-images "."
54 "Images that have URLs matching this regexp will be blocked."
55 :version "24.1"
56 :group 'gnus-art
57 :type 'regexp)
58
59 (defcustom gnus-max-image-proportion 0.7
60 "How big pictures displayed are in relation to the window they're in.
61 A value of 0.7 means that they are allowed to take up 70% of the
62 width and height of the window. If they are larger than this,
63 and Emacs supports it, then the images will be rescaled down to
64 fit these criteria."
65 :version "24.1"
66 :group 'gnus-art
67 :type 'float)
68
69 ;;;###autoload
70 (defun gnus-article-html (handle)
71 (let ((article-buffer (current-buffer)))
72 (save-restriction
73 (narrow-to-region (point) (point))
74 (save-excursion
75 (mm-with-part handle
76 (let* ((coding-system-for-read 'utf-8)
77 (coding-system-for-write 'utf-8)
78 (default-process-coding-system
79 (cons coding-system-for-read coding-system-for-write))
80 (charset (mail-content-type-get (mm-handle-type handle)
81 'charset)))
82 (when (and charset
83 (setq charset (mm-charset-to-coding-system charset))
84 (not (eq charset 'ascii)))
85 (mm-decode-coding-region (point-min) (point-max) charset))
86 (call-process-region (point-min) (point-max)
87 "w3m"
88 nil article-buffer nil
89 "-halfdump"
90 "-no-cookie"
91 "-I" "UTF-8"
92 "-O" "UTF-8"
93 "-o" "ext_halfdump=1"
94 "-o" "pre_conv=1"
95 "-t" (format "%s" tab-width)
96 "-cols" (format "%s" gnus-html-frame-width)
97 "-o" "display_image=off"
98 "-T" "text/html"))))
99 (gnus-html-wash-tags))))
100
101 (defvar gnus-article-mouse-face)
102
103 (defun gnus-html-wash-tags ()
104 (let (tag parameters string start end images url)
105 (mm-url-decode-entities)
106 (goto-char (point-min))
107 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
108 (setq tag (match-string 1)
109 parameters (match-string 2)
110 start (match-beginning 0))
111 (when (plusp (length parameters))
112 (set-text-properties 0 (1- (length parameters)) nil parameters))
113 (delete-region start (point))
114 (when (search-forward (concat "</" tag ">") nil t)
115 (delete-region (match-beginning 0) (match-end 0)))
116 (setq end (point))
117 (cond
118 ;; Fetch and insert a picture.
119 ((equal tag "img_alt")
120 (when (string-match "src=\"\\([^\"]+\\)" parameters)
121 (setq url (match-string 1 parameters))
122 (gnus-message 8 "Fetching image URL %s" url)
123 (if (string-match "^cid:\\(.*\\)" url)
124 ;; URLs with cid: have their content stashed in other
125 ;; parts of the MIME structure, so just insert them
126 ;; immediately.
127 (let ((handle (mm-get-content-id
128 (setq url (match-string 1 url))))
129 image)
130 (when handle
131 (mm-with-part handle
132 (setq image (gnus-create-image (buffer-string)
133 nil t))))
134 (when image
135 (delete-region start end)
136 (gnus-put-image image)))
137 ;; Normal, external URL.
138 (when (or (null gnus-blocked-images)
139 (not (string-match gnus-blocked-images url)))
140 (let ((file (gnus-html-image-id url)))
141 (if (file-exists-p file)
142 ;; It's already cached, so just insert it.
143 (when (gnus-html-put-image file (point))
144 ;; Delete the ALT text.
145 (delete-region start end))
146 ;; We don't have it, so schedule it for fetching
147 ;; asynchronously.
148 (push (list url
149 (set-marker (make-marker) start)
150 (point-marker))
151 images)))))))
152 ;; Add a link.
153 ((equal tag "a")
154 (when (string-match "href=\"\\([^\"]+\\)" parameters)
155 (setq url (match-string 1 parameters))
156 (gnus-message 8 "Fetching link URL %s" url)
157 (gnus-article-add-button start end
158 'browse-url url
159 url)
160 (let ((overlay (gnus-make-overlay start end)))
161 (gnus-overlay-put overlay 'evaporate t)
162 (gnus-overlay-put overlay 'gnus-button-url url)
163 (when gnus-article-mouse-face
164 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
165 ;; The upper-case IMG_ALT is apparently just an artifact that
166 ;; should be deleted.
167 ((equal tag "IMG_ALT")
168 (delete-region start end))
169 ;; Whatever. Just ignore the tag.
170 (t
171 ))
172 (goto-char start))
173 (goto-char (point-min))
174 ;; The output from -halfdump isn't totally regular, so strip
175 ;; off any </pre_int>s that were left over.
176 (while (re-search-forward "</pre_int>" nil t)
177 (replace-match "" t t))
178 (when images
179 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
180
181 (defun gnus-html-schedule-image-fetching (buffer images)
182 (gnus-message 8 "Scheduling image fetching in buffer %s, images %s" buffer images)
183 (let* ((url (caar images))
184 (process (start-process
185 "images" nil "curl"
186 "-s" "--create-dirs"
187 "--location"
188 "--max-time" "60"
189 "-o" (gnus-html-image-id url)
190 url)))
191 (process-kill-without-query process)
192 (set-process-sentinel process 'gnus-html-curl-sentinel)
193 (gnus-set-process-plist process (list 'images images
194 'buffer buffer))))
195
196 (defun gnus-html-image-id (url)
197 (expand-file-name (sha1 url) gnus-html-cache-directory))
198
199 (defun gnus-html-curl-sentinel (process event)
200 (when (string-match "finished" event)
201 (let* ((images (gnus-process-get process 'images))
202 (buffer (gnus-process-get process 'buffer))
203 (spec (pop images))
204 (file (gnus-html-image-id (car spec))))
205 (when (and (buffer-live-p buffer)
206 ;; If the position of the marker is 1, then that
207 ;; means that the text it was in has been deleted;
208 ;; i.e., that the user has selected a different
209 ;; article before the image arrived.
210 (not (= (marker-position (cadr spec)) (point-min))))
211 (with-current-buffer buffer
212 (let ((inhibit-read-only t))
213 (when (gnus-html-put-image file (cadr spec))
214 (delete-region (1+ (cadr spec)) (caddr spec))))))
215 (when images
216 (gnus-html-schedule-image-fetching buffer images)))))
217
218 (defun gnus-html-put-image (file point)
219 (when (display-graphic-p)
220 (let ((image (ignore-errors
221 (gnus-create-image file))))
222 (save-excursion
223 (goto-char point)
224 (if (and image
225 ;; Kludge to avoid displaying 30x30 gif images, which
226 ;; seems to be a signal of a broken image.
227 (not (and (listp image)
228 (eq (plist-get (cdr image) :type) 'gif)
229 (= (car (image-size image t)) 30)
230 (= (cdr (image-size image t)) 30))))
231 (progn
232 (gnus-put-image (gnus-html-rescale-image image))
233 t)
234 (when (fboundp 'find-image)
235 (gnus-put-image (find-image
236 '((:type xpm :file "lock-broken.xpm")))))
237 nil)))))
238
239 (defun gnus-html-rescale-image (image)
240 (if (not (fboundp 'imagemagick-types))
241 image
242 (let* ((width (car (image-size image t)))
243 (height (cdr (image-size image t)))
244 (edges (window-pixel-edges))
245 (window-width (truncate (* gnus-max-image-proportion
246 (- (nth 2 edges) (nth 0 edges)))))
247 (window-height (truncate (* gnus-max-image-proportion
248 (- (nth 3 edges) (nth 1 edges)))))
249 scaled-image)
250 (when (> width window-width)
251 (setq window-height (truncate (* window-height
252 (/ (* 1.0 window-width) width)))))
253 (if (> height window-height)
254 (or (create-image file 'imagemagick nil
255 :height window-height)
256 image)
257 image))))
258
259 (defun gnus-html-prune-cache ()
260 (let ((total-size 0)
261 files)
262 (dolist (file (directory-files gnus-html-cache-directory t nil t))
263 (let ((attributes (file-attributes file)))
264 (unless (nth 0 attributes)
265 (incf total-size (nth 7 attributes))
266 (push (list (time-to-seconds (nth 5 attributes))
267 (nth 7 attributes) file)
268 files))))
269 (when (> total-size gnus-html-cache-size)
270 (setq files (sort files (lambda (f1 f2)
271 (< (car f1) (car f2)))))
272 (dolist (file files)
273 (when (> total-size gnus-html-cache-size)
274 (decf total-size (cadr file))
275 (delete-file (nth 2 file)))))))
276
277 ;;;###autoload
278 (defun gnus-html-prefetch-images (summary)
279 (let (blocked-images urls)
280 (when (buffer-live-p summary)
281 (with-current-buffer summary
282 (setq blocked-images gnus-blocked-images))
283 (save-match-data
284 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
285 (let ((url (match-string 1)))
286 (if (or (null blocked-images)
287 (not (string-match blocked-images url)))
288 (unless (file-exists-p (gnus-html-image-id url))
289 (push url urls)
290 (push (gnus-html-image-id url) urls)
291 (push "-o" urls))
292 (gnus-message 8 "Image URL %s is blocked" url))))
293 (let ((process
294 (apply 'start-process
295 "images" nil "curl"
296 "-s" "--create-dirs"
297 "--location"
298 "--max-time" "60"
299 urls)))
300 (process-kill-without-query process))))))
301
302 (provide 'gnus-html)
303
304 ;;; gnus-html.el ends here