Rename :content-type to :format in `create-image'
[bpt/emacs.git] / lisp / net / shr.el
CommitLineData
367f7f81
LMI
1;;; shr.el --- Simple HTML Renderer
2
ab422c4d 3;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
367f7f81
LMI
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; Keywords: html
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;; This package takes a HTML parse tree (as provided by
26;; libxml-parse-html-region) and renders it in the current buffer. It
27;; does not do CSS, JavaScript or anything advanced: It's geared
28;; towards rendering typical short snippets of HTML, like what you'd
29;; find in HTML email and the like.
30
31;;; Code:
32
3d319c8f 33(eval-when-compile (require 'cl))
7545bd25 34(eval-when-compile (require 'url)) ;For url-filename's setf handler.
71e691a5
G
35(require 'browse-url)
36
870409d4
G
37(defgroup shr nil
38 "Simple HTML Renderer"
2bed3f04 39 :version "24.1"
0ebd92a3 40 :group 'hypermedia)
870409d4
G
41
42(defcustom shr-max-image-proportion 0.9
43 "How big pictures displayed are in relation to the window they're in.
44A value of 0.7 means that they are allowed to take up 70% of the
45width and height of the window. If they are larger than this,
46and Emacs supports it, then the images will be rescaled down to
47fit these criteria."
48 :version "24.1"
49 :group 'shr
50 :type 'float)
51
52(defcustom shr-blocked-images nil
53 "Images that have URLs matching this regexp will be blocked."
54 :version "24.1"
55 :group 'shr
a931698a 56 :type '(choice (const nil) regexp))
870409d4 57
924d6997
G
58(defcustom shr-table-horizontal-line nil
59 "Character used to draw horizontal table lines.
60If nil, don't draw horizontal table lines."
d3098750 61 :group 'shr
af1c6c84 62 :type '(choice (const nil) character))
d3098750 63
e37df674 64(defcustom shr-table-vertical-line ?\s
d3098750 65 "Character used to draw vertical table lines."
afba0c4b 66 :group 'shr
030158f3 67 :type 'character)
afba0c4b 68
e37df674 69(defcustom shr-table-corner ?\s
d3098750 70 "Character used to draw table corners."
6b7df8d3 71 :group 'shr
030158f3 72 :type 'character)
6b7df8d3
G
73
74(defcustom shr-hr-line ?-
d3098750 75 "Character used to draw hr lines."
afba0c4b 76 :group 'shr
030158f3 77 :type 'character)
afba0c4b 78
d0e0de31 79(defcustom shr-width fill-column
bb7f5cbc
G
80 "Frame width to use for rendering.
81May either be an integer specifying a fixed width in characters,
82or nil, meaning that the full width of the window should be
83used."
84 :type '(choice (integer :tag "Fixed width in characters")
85 (const :tag "Use the width of the window" nil))
d0e0de31
JD
86 :group 'shr)
87
c74cb344
G
88(defcustom shr-bullet "* "
89 "Bullet used for unordered lists.
90Alternative suggestions are:
91- \" \"
92- \" \""
93 :type 'string
94 :group 'shr)
95
0ebd92a3
LMI
96(defcustom shr-external-browser 'browse-url-default-browser
97 "Function used to launch an external browser."
98 :version "24.4"
99 :group 'shr
100 :type 'function)
101
130e977f
LMI
102(defvar shr-content-function nil
103 "If bound, this should be a function that will return the content.
104This is used for cid: URLs, and the function is called with the
105cid: URL as the argument.")
106
b9bdaf74
KY
107(defvar shr-put-image-function 'shr-put-image
108 "Function called to put image and alt string.")
109
6eee2678
LMI
110(defface shr-strike-through '((t (:strike-through t)))
111 "Font for <s> elements."
112 :group 'shr)
113
f8774e35 114(defface shr-link
7ef1d634 115 '((t (:inherit link)))
df26ce09 116 "Font for link elements."
c2f51e23
G
117 :group 'shr)
118
66627fa9
G
119;;; Internal variables.
120
870409d4
G
121(defvar shr-folding-mode nil)
122(defvar shr-state nil)
123(defvar shr-start nil)
a41c2e6d 124(defvar shr-indentation 0)
130e977f 125(defvar shr-inhibit-images nil)
66627fa9 126(defvar shr-list-mode nil)
3d319c8f 127(defvar shr-content-cache nil)
83ffd571 128(defvar shr-kinsoku-shorten nil)
99e65b2d 129(defvar shr-table-depth 0)
04db63bc 130(defvar shr-stylesheet nil)
dbd5ffad 131(defvar shr-base nil)
728518c3 132(defvar shr-ignore-cache nil)
2644071e 133(defvar shr-external-rendering-functions nil)
c74cb344 134(defvar shr-target-id nil)
be2aa135 135(defvar shr-inhibit-decoration nil)
924d6997 136(defvar shr-table-separator-length 1)
870409d4 137
71e691a5
G
138(defvar shr-map
139 (let ((map (make-sparse-keymap)))
140 (define-key map "a" 'shr-show-alt-text)
141 (define-key map "i" 'shr-browse-image)
89b163db 142 (define-key map "z" 'shr-zoom-image)
7304e4dd
LMI
143 (define-key map [tab] 'shr-next-link)
144 (define-key map [backtab] 'shr-previous-link)
970ad972 145 (define-key map [follow-link] 'mouse-face)
1211de50 146 (define-key map [mouse-2] 'shr-browse-url)
71e691a5 147 (define-key map "I" 'shr-insert-image)
16f74f10 148 (define-key map "w" 'shr-copy-url)
2d756ae0 149 (define-key map "u" 'shr-copy-url)
71e691a5 150 (define-key map "v" 'shr-browse-url)
cdf1fca4 151 (define-key map "o" 'shr-save-contents)
71e691a5
G
152 (define-key map "\r" 'shr-browse-url)
153 map))
154
66627fa9 155;; Public functions and commands.
0143b8a3
GM
156(declare-function libxml-parse-html-region "xml.c"
157 (start end &optional base-url))
66627fa9 158
7b953864
SM
159(defun shr-render-buffer (buffer)
160 "Display the HTML rendering of the current buffer."
161 (interactive (list (current-buffer)))
0143b8a3
GM
162 (or (fboundp 'libxml-parse-html-region)
163 (error "This function requires Emacs to be compiled with libxml2"))
1518e4f0
G
164 (pop-to-buffer "*html*")
165 (erase-buffer)
166 (shr-insert-document
7b953864 167 (with-current-buffer buffer
edd9679c
LMI
168 (libxml-parse-html-region (point-min) (point-max))))
169 (goto-char (point-min)))
1518e4f0 170
eadb6068
IK
171(defun shr-render-region (begin end &optional buffer)
172 "Display the HTML rendering of the region between BEGIN and END."
173 (interactive "r")
174 (unless (fboundp 'libxml-parse-html-region)
175 (error "This function requires Emacs to be compiled with libxml2"))
176 (with-current-buffer (or buffer (current-buffer))
177 (let ((dom (libxml-parse-html-region begin end)))
178 (delete-region begin end)
179 (goto-char begin)
180 (shr-insert-document dom))))
181
7b953864
SM
182(defun shr-visit-file (file)
183 "Parse FILE as an HTML document, and render it in a new buffer."
184 (interactive "fHTML file name: ")
185 (with-temp-buffer
186 (insert-file-contents file)
187 (shr-render-buffer (current-buffer))))
188
66627fa9
G
189;;;###autoload
190(defun shr-insert-document (dom)
9ed5a258
LI
191 "Render the parsed document DOM into the current buffer.
192DOM should be a parse tree as generated by
193`libxml-parse-html-region' or similar."
3d319c8f 194 (setq shr-content-cache nil)
9ed5a258
LI
195 (let ((start (point))
196 (shr-state nil)
bb7f5cbc 197 (shr-start nil)
dbd5ffad 198 (shr-base nil)
4452891d 199 (shr-preliminary-table-render 0)
924d6997 200 (shr-width (or shr-width (1- (window-width)))))
9ed5a258
LI
201 (shr-descend (shr-transform-dom dom))
202 (shr-remove-trailing-whitespace start (point))))
203
204(defun shr-remove-trailing-whitespace (start end)
7c4bbb69
LI
205 (let ((width (window-width)))
206 (save-restriction
207 (narrow-to-region start end)
208 (goto-char start)
209 (while (not (eobp))
210 (end-of-line)
888ab661 211 (when (> (shr-previous-newline-padding-width (current-column)) width)
7c4bbb69
LI
212 (dolist (overlay (overlays-at (point)))
213 (when (overlay-get overlay 'before-string)
214 (overlay-put overlay 'before-string nil))))
215 (forward-line 1)))))
66627fa9
G
216
217(defun shr-copy-url ()
218 "Copy the URL under point to the kill ring.
219If called twice, then try to fetch the URL and see whether it
220redirects somewhere else."
221 (interactive)
222 (let ((url (get-text-property (point) 'shr-url)))
223 (cond
224 ((not url)
225 (message "No URL under point"))
226 ;; Resolve redirected URLs.
227 ((equal url (car kill-ring))
228 (url-retrieve
229 url
230 (lambda (a)
231 (when (and (consp a)
232 (eq (car a) :redirect))
233 (with-temp-buffer
234 (insert (cadr a))
235 (goto-char (point-min))
236 ;; Remove common tracking junk from the URL.
237 (when (re-search-forward ".utm_.*" nil t)
238 (replace-match "" t t))
239 (message "Copied %s" (buffer-string))
038b3495
LI
240 (copy-region-as-kill (point-min) (point-max)))))
241 nil t))
66627fa9
G
242 ;; Copy the URL to the kill ring.
243 (t
244 (with-temp-buffer
245 (insert url)
246 (copy-region-as-kill (point-min) (point-max))
247 (message "Copied %s" url))))))
248
7304e4dd
LMI
249(defun shr-next-link ()
250 "Skip to the next link."
251 (interactive)
be2aa135 252 (let ((skip (text-property-any (point) (point-max) 'help-echo nil)))
7304e4dd 253 (if (not (setq skip (text-property-not-all skip (point-max)
be2aa135 254 'help-echo nil)))
7304e4dd
LMI
255 (message "No next link")
256 (goto-char skip)
257 (message "%s" (get-text-property (point) 'help-echo)))))
258
259(defun shr-previous-link ()
260 "Skip to the previous link."
261 (interactive)
262 (let ((start (point))
263 (found nil))
264 ;; Skip past the current link.
265 (while (and (not (bobp))
be2aa135 266 (get-text-property (point) 'help-echo))
7304e4dd
LMI
267 (forward-char -1))
268 ;; Find the previous link.
269 (while (and (not (bobp))
be2aa135 270 (not (setq found (get-text-property (point) 'help-echo))))
7304e4dd
LMI
271 (forward-char -1))
272 (if (not found)
273 (progn
274 (message "No previous link")
275 (goto-char start))
276 ;; Put point at the start of the link.
277 (while (and (not (bobp))
be2aa135 278 (get-text-property (point) 'help-echo))
7304e4dd
LMI
279 (forward-char -1))
280 (forward-char 1)
281 (message "%s" (get-text-property (point) 'help-echo)))))
282
66627fa9
G
283(defun shr-show-alt-text ()
284 "Show the ALT text of the image under point."
285 (interactive)
286 (let ((text (get-text-property (point) 'shr-alt)))
287 (if (not text)
288 (message "No image under point")
289 (message "%s" text))))
290
2da9c605
G
291(defun shr-browse-image (&optional copy-url)
292 "Browse the image under point.
293If COPY-URL (the prefix if called interactively) is non-nil, copy
294the URL of the image to the kill buffer instead."
295 (interactive "P")
8b6f6573 296 (let ((url (get-text-property (point) 'image-url)))
2da9c605
G
297 (cond
298 ((not url)
299 (message "No image under point"))
300 (copy-url
301 (with-temp-buffer
302 (insert url)
303 (copy-region-as-kill (point-min) (point-max))
304 (message "Copied %s" url)))
305 (t
66627fa9 306 (message "Browsing %s..." url)
2da9c605 307 (browse-url url)))))
66627fa9 308
3d319c8f
LMI
309(defun shr-insert-image ()
310 "Insert the image under point into the buffer."
311 (interactive)
8b6f6573 312 (let ((url (get-text-property (point) 'image-url)))
3d319c8f
LMI
313 (if (not url)
314 (message "No image under point")
315 (message "Inserting %s..." url)
316 (url-retrieve url 'shr-image-fetched
317 (list (current-buffer) (1- (point)) (point-marker))
038b3495 318 t t))))
3d319c8f 319
89b163db
G
320(defun shr-zoom-image ()
321 "Toggle the image size.
322The size will be rotated between the default size, the original
323size, and full-buffer size."
324 (interactive)
325 (let ((url (get-text-property (point) 'image-url))
326 (size (get-text-property (point) 'image-size))
327 (buffer-read-only nil))
328 (if (not url)
329 (message "No image under point")
330 ;; Delete the old picture.
331 (while (get-text-property (point) 'image-url)
332 (forward-char -1))
333 (forward-char 1)
334 (let ((start (point)))
335 (while (get-text-property (point) 'image-url)
336 (forward-char 1))
337 (forward-char -1)
338 (put-text-property start (point) 'display nil)
339 (when (> (- (point) start) 2)
340 (delete-region start (1- (point)))))
341 (message "Inserting %s..." url)
342 (url-retrieve url 'shr-image-fetched
343 (list (current-buffer) (1- (point)) (point-marker)
344 (list (cons 'size
345 (cond ((or (eq size 'default)
346 (null size))
347 'original)
348 ((eq size 'original)
349 'full)
350 ((eq size 'full)
351 'default)))))
352 t))))
353
66627fa9
G
354;;; Utility functions.
355
870409d4
G
356(defun shr-transform-dom (dom)
357 (let ((result (list (pop dom))))
358 (dolist (arg (pop dom))
359 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
360 (cdr arg))
361 result))
362 (dolist (sub dom)
363 (if (stringp sub)
953d41c4 364 (push (cons 'text sub) result)
870409d4
G
365 (push (shr-transform-dom sub) result)))
366 (nreverse result)))
367
870409d4 368(defun shr-descend (dom)
2644071e
LMI
369 (let ((function
370 (or
371 ;; Allow other packages to override (or provide) rendering
372 ;; of elements.
373 (cdr (assq (car dom) shr-external-rendering-functions))
374 (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
ebe79557 375 (style (cdr (assq :style (cdr dom))))
04db63bc 376 (shr-stylesheet shr-stylesheet)
ebe79557 377 (start (point)))
b31b26b4 378 (when style
be2aa135 379 (if (string-match "color\\|display\\|border-collapse" style)
b31b26b4
G
380 (setq shr-stylesheet (nconc (shr-parse-style style)
381 shr-stylesheet))
382 (setq style nil)))
c74cb344
G
383 ;; If we have a display:none, then just ignore this part of the
384 ;; DOM.
385 (unless (equal (cdr (assq 'display shr-stylesheet)) "none")
386 (if (fboundp function)
387 (funcall function (cdr dom))
388 (shr-generic (cdr dom)))
389 (when (and shr-target-id
390 (equal (cdr (assq :id (cdr dom))) shr-target-id))
391 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
392 ;; If style is set, then this node has set the color.
393 (when style
394 (shr-colorize-region start (point)
395 (cdr (assq 'color shr-stylesheet))
396 (cdr (assq 'background-color shr-stylesheet)))))))
870409d4
G
397
398(defun shr-generic (cont)
399 (dolist (sub cont)
400 (cond
953d41c4 401 ((eq (car sub) 'text)
870409d4 402 (shr-insert (cdr sub)))
a41c2e6d 403 ((listp (cdr sub))
870409d4
G
404 (shr-descend sub)))))
405
ed797193
G
406(defmacro shr-char-breakable-p (char)
407 "Return non-nil if a line can be broken before and after CHAR."
408 `(aref fill-find-break-point-function-table ,char))
409(defmacro shr-char-nospace-p (char)
410 "Return non-nil if no space is required before and after CHAR."
411 `(aref fill-nospace-between-words-table ,char))
412
413;; KINSOKU is a Japanese word meaning a rule that should not be violated.
414;; In Emacs, it is a term used for characters, e.g. punctuation marks,
415;; parentheses, and so on, that should not be placed in the beginning
416;; of a line or the end of a line.
417(defmacro shr-char-kinsoku-bol-p (char)
418 "Return non-nil if a line ought not to begin with CHAR."
419 `(aref (char-category-set ,char) ?>))
420(defmacro shr-char-kinsoku-eol-p (char)
421 "Return non-nil if a line ought not to end with CHAR."
422 `(aref (char-category-set ,char) ?<))
423(unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
424 (load "kinsoku" nil t))
425
66627fa9 426(defun shr-insert (text)
6b7df8d3 427 (when (and (eq shr-state 'image)
89b163db 428 (not (bolp))
6b7df8d3 429 (not (string-match "\\`[ \t\n]+\\'" text)))
66627fa9
G
430 (insert "\n")
431 (setq shr-state nil))
432 (cond
433 ((eq shr-folding-mode 'none)
434 (insert text))
435 (t
c38e0c97 436 (when (and (string-match "\\`[ \t\n ]" text)
73db8b08
KY
437 (not (bolp))
438 (not (eq (char-after (1- (point))) ? )))
439 (insert " "))
c38e0c97 440 (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t))
73db8b08
KY
441 (when (and (bolp)
442 (> shr-indentation 0))
443 (shr-indent))
73db8b08 444 ;; No space is needed behind a wide character categorized as
b41c2f65
KY
445 ;; kinsoku-bol, between characters both categorized as nospace,
446 ;; or at the beginning of a line.
73db8b08 447 (let (prev)
48ba8195
KY
448 (when (and (> (current-column) shr-indentation)
449 (eq (preceding-char) ? )
20438017 450 (or (= (line-beginning-position) (1- (point)))
ed797193
G
451 (and (shr-char-breakable-p
452 (setq prev (char-after (- (point) 2))))
453 (shr-char-kinsoku-bol-p prev))
454 (and (shr-char-nospace-p prev)
455 (shr-char-nospace-p (aref elem 0)))))
73db8b08 456 (delete-char -1)))
48ba8195
KY
457 ;; The shr-start is a special variable that is used to pass
458 ;; upwards the first point in the buffer where the text really
459 ;; starts.
460 (unless shr-start
461 (setq shr-start (point)))
73db8b08 462 (insert elem)
e76917e6 463 (setq shr-state nil)
e7102c0a
KY
464 (let (found)
465 (while (and (> (current-column) shr-width)
466 (progn
467 (setq found (shr-find-fill-point))
b40950bf 468 (not (eolp))))
fe98a42f
KY
469 (when (eq (preceding-char) ? )
470 (delete-char -1))
471 (insert "\n")
472 (unless found
e7102c0a
KY
473 ;; No space is needed at the beginning of a line.
474 (when (eq (following-char) ? )
475 (delete-char 1)))
476 (when (> shr-indentation 0)
477 (shr-indent))
478 (end-of-line))
479 (insert " ")))
c38e0c97 480 (unless (string-match "[ \t\r\n ]\\'" text)
73db8b08 481 (delete-char -1)))))
66627fa9 482
6b7df8d3 483(defun shr-find-fill-point ()
83ffd571
KY
484 (when (> (move-to-column shr-width) shr-width)
485 (backward-char 1))
ed797193
G
486 (let ((bp (point))
487 failed)
488 (while (not (or (setq failed (= (current-column) shr-indentation))
489 (eq (preceding-char) ? )
490 (eq (following-char) ? )
491 (shr-char-breakable-p (preceding-char))
492 (shr-char-breakable-p (following-char))
7454326a
G
493 (if (eq (preceding-char) ?')
494 (not (memq (char-after (- (point) 2))
495 (list nil ?\n ? )))
7454326a 496 (and (shr-char-kinsoku-bol-p (preceding-char))
6568edea 497 (shr-char-breakable-p (following-char))
7454326a 498 (not (shr-char-kinsoku-bol-p (following-char)))))
ed797193 499 (shr-char-kinsoku-eol-p (following-char))))
83ffd571 500 (backward-char 1))
ed797193
G
501 (if (and (not (or failed (eolp)))
502 (eq (preceding-char) ?'))
503 (while (not (or (setq failed (eolp))
504 (eq (following-char) ? )
505 (shr-char-breakable-p (following-char))
506 (shr-char-kinsoku-eol-p (following-char))))
507 (forward-char 1)))
83ffd571 508 (if failed
20438017 509 ;; There's no breakable point, so we give it up.
ed797193
G
510 (let (found)
511 (goto-char bp)
512 (unless shr-kinsoku-shorten
513 (while (and (setq found (re-search-forward
514 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
515 (line-end-position) 'move))
516 (eq (preceding-char) ?')))
517 (if (and found (not (match-beginning 1)))
518 (goto-char (match-beginning 0)))))
b40950bf
KY
519 (or
520 (eolp)
ed797193
G
521 ;; Don't put kinsoku-bol characters at the beginning of a line,
522 ;; or kinsoku-eol characters at the end of a line.
523 (cond
524 (shr-kinsoku-shorten
525 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
526 (shr-char-kinsoku-eol-p (preceding-char)))
527 (backward-char 1))
528 (when (setq failed (= (current-column) shr-indentation))
529 ;; There's no breakable point that doesn't violate kinsoku,
530 ;; so we look for the second best position.
531 (while (and (progn
532 (forward-char 1)
533 (<= (current-column) shr-width))
534 (progn
535 (setq bp (point))
536 (shr-char-kinsoku-eol-p (following-char)))))
537 (goto-char bp)))
538 ((shr-char-kinsoku-eol-p (preceding-char))
89b163db
G
539 ;; Find backward the point where kinsoku-eol characters begin.
540 (let ((count 4))
541 (while
542 (progn
543 (backward-char 1)
544 (and (> (setq count (1- count)) 0)
545 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
546 (or (shr-char-kinsoku-eol-p (preceding-char))
547 (shr-char-kinsoku-bol-p (following-char)))))))
548 (if (setq failed (= (current-column) shr-indentation))
549 ;; There's no breakable point that doesn't violate kinsoku,
550 ;; so we go to the second best position.
551 (if (looking-at "\\(\\c<+\\)\\c<")
552 (goto-char (match-end 1))
553 (forward-char 1))))
554 ((shr-char-kinsoku-bol-p (following-char))
555 ;; Find forward the point where kinsoku-bol characters end.
556 (let ((count 4))
557 (while (progn
558 (forward-char 1)
559 (and (>= (setq count (1- count)) 0)
ed797193 560 (shr-char-kinsoku-bol-p (following-char))
89b163db 561 (shr-char-breakable-p (following-char))))))))
ed797193
G
562 (when (eq (following-char) ? )
563 (forward-char 1))))
564 (not failed)))
6b7df8d3 565
c74cb344
G
566(defun shr-parse-base (url)
567 ;; Always chop off anchors.
568 (when (string-match "#.*" url)
569 (setq url (substring url 0 (match-beginning 0))))
570 (let* ((parsed (url-generic-parse-url url))
571 (local (url-filename parsed)))
572 (setf (url-filename parsed) "")
573 ;; Chop off the bit after the last slash.
574 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local)
575 (setq local (match-string 1 local)))
576 ;; Always make the local bit end with a slash.
577 (when (and (not (zerop (length local)))
578 (not (eq (aref local (1- (length local))) ?/)))
579 (setq local (concat local "/")))
580 (list (url-recreate-url parsed)
581 local
582 (url-type parsed)
583 url)))
584
585(defun shr-expand-url (url &optional base)
586 (setq base
587 (if base
588 (shr-parse-base base)
589 ;; Bound by the parser.
590 shr-base))
591 (when (zerop (length url))
592 (setq url nil))
593 (cond ((or (not url)
594 (not base)
595 (string-match "\\`[a-z]*:" url))
596 ;; Absolute URL.
597 (or url (car base)))
598 ((eq (aref url 0) ?/)
599 (if (and (> (length url) 1)
600 (eq (aref url 1) ?/))
601 ;; //host...; just use the protocol
602 (concat (nth 2 base) ":" url)
603 ;; Just use the host name part.
604 (concat (car base) url)))
605 ((eq (aref url 0) ?#)
606 ;; A link to an anchor.
607 (concat (nth 3 base) url))
608 (t
609 ;; Totally relative.
610 (concat (car base) (cadr base) url))))
dbd5ffad 611
66627fa9
G
612(defun shr-ensure-newline ()
613 (unless (zerop (current-column))
614 (insert "\n")))
a41c2e6d
G
615
616(defun shr-ensure-paragraph ()
617 (unless (bobp)
f7aa248a 618 (if (<= (current-column) shr-indentation)
71e691a5
G
619 (unless (save-excursion
620 (forward-line -1)
621 (looking-at " *$"))
a41c2e6d
G
622 (insert "\n"))
623 (if (save-excursion
624 (beginning-of-line)
be2aa135
LMI
625 ;; If the current line is totally blank, and doesn't even
626 ;; have any face properties set, then delete the blank
627 ;; space.
628 (and (looking-at " *$")
629 (not (get-text-property (point) 'face))
630 (not (= (next-single-property-change (point) 'face nil
631 (line-end-position))
632 (line-end-position)))))
89b163db 633 (delete-region (match-beginning 0) (match-end 0))
a41c2e6d
G
634 (insert "\n\n")))))
635
66627fa9 636(defun shr-indent ()
f7aa248a
G
637 (when (> shr-indentation 0)
638 (insert (make-string shr-indentation ? ))))
870409d4 639
a41c2e6d 640(defun shr-fontize-cont (cont &rest types)
870409d4
G
641 (let (shr-start)
642 (shr-generic cont)
a41c2e6d
G
643 (dolist (type types)
644 (shr-add-font (or shr-start (point)) (point) type))))
870409d4 645
7304e4dd
LMI
646;; Add face to the region, but avoid putting the font properties on
647;; blank text at the start of the line, and the newline at the end, to
648;; avoid ugliness.
870409d4 649(defun shr-add-font (start end type)
be2aa135
LMI
650 (unless shr-inhibit-decoration
651 (save-excursion
652 (goto-char start)
653 (while (< (point) end)
654 (when (bolp)
655 (skip-chars-forward " "))
656 (add-face-text-property (point) (min (line-end-position) end) type t)
657 (if (< (line-end-position) end)
658 (forward-line 1)
659 (goto-char end))))))
870409d4 660
8ba8eec5
LMI
661(defun shr-mouse-browse-url (ev)
662 "Browse the URL under the mouse cursor."
663 (interactive "e")
664 (mouse-set-point ev)
665 (shr-browse-url))
666
1211de50 667(defun shr-browse-url (&optional external mouse-event)
bdaa086b
LMI
668 "Browse the URL under point.
669If EXTERNAL, browse the URL using `shr-external-browser'."
1211de50
LMI
670 (interactive (list current-prefix-arg last-nonmenu-event))
671 (mouse-set-point mouse-event)
71e691a5 672 (let ((url (get-text-property (point) 'shr-url)))
181cb5fb
G
673 (cond
674 ((not url)
675 (message "No link under point"))
676 ((string-match "^mailto:" url)
39ddff39 677 (browse-url-mail url))
181cb5fb 678 (t
bdaa086b
LMI
679 (if external
680 (funcall shr-external-browser url)
681 (browse-url url))))))
71e691a5 682
cdf1fca4
LMI
683(defun shr-save-contents (directory)
684 "Save the contents from URL in a file."
685 (interactive "DSave contents of URL to directory: ")
686 (let ((url (get-text-property (point) 'shr-url)))
687 (if (not url)
688 (message "No link under point")
689 (url-retrieve (shr-encode-url url)
038b3495
LI
690 'shr-store-contents (list url directory)
691 nil t))))
cdf1fca4
LMI
692
693(defun shr-store-contents (status url directory)
694 (unless (plist-get status :error)
695 (when (or (search-forward "\n\n" nil t)
696 (search-forward "\r\n\r\n" nil t))
697 (write-region (point) (point-max)
698 (expand-file-name (file-name-nondirectory url)
699 directory)))))
700
89b163db 701(defun shr-image-fetched (status buffer start end &optional flags)
0e2cebe5
LI
702 (let ((image-buffer (current-buffer)))
703 (when (and (buffer-name buffer)
704 (not (plist-get status :error)))
705 (url-store-in-cache image-buffer)
706 (when (or (search-forward "\n\n" nil t)
707 (search-forward "\r\n\r\n" nil t))
21c58ae2 708 (let ((data (shr-parse-image-data)))
0e2cebe5
LI
709 (with-current-buffer buffer
710 (save-excursion
711 (let ((alt (buffer-substring start end))
89b163db 712 (properties (text-properties-at start))
0e2cebe5
LI
713 (inhibit-read-only t))
714 (delete-region start end)
715 (goto-char start)
89b163db
G
716 (funcall shr-put-image-function data alt flags)
717 (while properties
718 (let ((type (pop properties))
719 (value (pop properties)))
720 (unless (memq type '(display image-size))
721 (put-text-property start (point) type value))))))))))
0e2cebe5 722 (kill-buffer image-buffer)))
870409d4 723
2250b351
DE
724(defun shr-image-from-data (data)
725 "Return an image from the data: URI content DATA."
726 (when (string-match
727 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
728 data)
729 (let ((param (match-string 4 data))
730 (payload (url-unhex-string (match-string 5 data))))
731 (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
732 (setq payload (base64-decode-string payload)))
733 payload)))
734
21c58ae2
LMI
735(defun shr-put-image (spec alt &optional flags)
736 "Insert image SPEC with a string ALT. Return image.
737SPEC is either an image data blob, or a list where the first
738element is the data blob and the second element is the content-type."
4abff904 739 (if (display-graphic-p)
89b163db 740 (let* ((size (cdr (assq 'size flags)))
21c58ae2
LMI
741 (data (if (consp spec)
742 (car spec)
743 spec))
744 (content-type (and (consp spec)
745 (cadr spec)))
89b163db
G
746 (start (point))
747 (image (cond
748 ((eq size 'original)
21c58ae2 749 (create-image data nil t :ascent 100
c6b7ccaa 750 :format content-type))
89b163db
G
751 ((eq size 'full)
752 (ignore-errors
21c58ae2 753 (shr-rescale-image data t content-type)))
89b163db
G
754 (t
755 (ignore-errors
21c58ae2 756 (shr-rescale-image data nil content-type))))))
4abff904 757 (when image
c0f9edce
G
758 ;; When inserting big-ish pictures, put them at the
759 ;; beginning of the line.
760 (when (and (> (current-column) 0)
761 (> (car (image-size image t)) 400))
762 (insert "\n"))
89b163db 763 (if (eq size 'original)
7304e4dd 764 (insert-sliced-image image (or alt "*") nil 20 1)
89b163db
G
765 (insert-image image (or alt "*")))
766 (put-text-property start (point) 'image-size size)
dd8620de
GM
767 (when (cond ((fboundp 'image-multi-frame-p)
768 ;; Only animate multi-frame things that specify a
769 ;; delay; eg animated gifs as opposed to
770 ;; multi-page tiffs. FIXME?
771 (cdr (image-multi-frame-p image)))
772 ((fboundp 'image-animated-p)
773 (image-animated-p image)))
c146ad85 774 (image-animate image nil 60)))
b9bdaf74 775 image)
99e65b2d 776 (insert alt)))
870409d4 777
21c58ae2 778(defun shr-rescale-image (data &optional force content-type)
89b163db
G
779 "Rescale DATA, if too big, to fit the current buffer.
780If FORCE, rescale the image anyway."
f3f9606c 781 (if (or (not (fboundp 'imagemagick-types))
2fae38e5 782 (eq (image-type-from-data data) 'gif)
f3f9606c
LMI
783 (not (get-buffer-window (current-buffer))))
784 (create-image data nil t :ascent 100)
785 (let ((edges (window-inside-pixel-edges
786 (get-buffer-window (current-buffer)))))
787 (create-image
788 data 'imagemagick t
789 :ascent 100
790 :max-width (truncate (* shr-max-image-proportion
791 (- (nth 2 edges) (nth 0 edges))))
792 :max-height (truncate (* shr-max-image-proportion
21c58ae2 793 (- (nth 3 edges) (nth 1 edges))))
c6b7ccaa 794 :format content-type))))
870409d4 795
85a45a69
GM
796;; url-cache-extract autoloads url-cache.
797(declare-function url-cache-create-filename "url-cache" (url))
798(autoload 'mm-disable-multibyte "mm-util")
39ddff39 799(autoload 'browse-url-mail "browse-url")
85a45a69 800
870409d4
G
801(defun shr-get-image-data (url)
802 "Get image data for URL.
803Return a string with image data."
804 (with-temp-buffer
805 (mm-disable-multibyte)
71e691a5 806 (when (ignore-errors
ab67634f 807 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
71e691a5
G
808 t)
809 (when (or (search-forward "\n\n" nil t)
810 (search-forward "\r\n\r\n" nil t))
21c58ae2
LMI
811 (shr-parse-image-data)))))
812
813(defun shr-parse-image-data ()
814 (list
815 (buffer-substring (point) (point-max))
816 (save-excursion
817 (save-restriction
818 (narrow-to-region (point-min) (point))
819 (let ((content-type (mail-fetch-field "content-type")))
820 (and content-type
821 (intern content-type obarray)))))))
870409d4 822
40de2c6d
KY
823(defun shr-image-displayer (content-function)
824 "Return a function to display an image.
825CONTENT-FUNCTION is a function to retrieve an image for a cid url that
826is an argument. The function to be returned takes three arguments URL,
53964682 827START, and END. Note that START and END should be markers."
40de2c6d 828 `(lambda (url start end)
f8d8a97b
KY
829 (when url
830 (if (string-match "\\`cid:" url)
831 ,(when content-function
832 `(let ((image (funcall ,content-function
833 (substring url (match-end 0)))))
834 (when image
835 (goto-char start)
b9bdaf74 836 (funcall shr-put-image-function
195b2593 837 image (buffer-substring start end))
e2d0ba98 838 (delete-region (point) end))))
f8d8a97b
KY
839 (url-retrieve url 'shr-image-fetched
840 (list (current-buffer) start end)
038b3495 841 t t)))))
40de2c6d 842
66627fa9
G
843(defun shr-heading (cont &rest types)
844 (shr-ensure-paragraph)
845 (apply #'shr-fontize-cont cont types)
846 (shr-ensure-paragraph))
847
04db63bc 848(defun shr-urlify (start url &optional title)
be2aa135 849 (when (and title (string-match "ctx" title)) (debug))
ca3cf0a5 850 (shr-add-font start (point) 'shr-link)
7304e4dd
LMI
851 (add-text-properties
852 start (point)
853 (list 'shr-url url
be2aa135 854 'help-echo (if title (format "%s (%s)" url title) url)
d50fceab 855 'follow-link t
5a2a7735 856 'mouse-face 'highlight
970ad972 857 'keymap shr-map)))
de635afe
G
858
859(defun shr-encode-url (url)
860 "Encode URL."
861 (browse-url-url-encode-chars url "[)$ ]"))
862
ebe79557
LMI
863(autoload 'shr-color-visible "shr-color")
864(autoload 'shr-color->hexadecimal "shr-color")
144b7b5c
G
865
866(defun shr-color-check (fg bg)
867 "Check that FG is visible on BG.
868Returns (fg bg) with corrected values.
869Returns nil if the colors that would be used are the default
870ones, in case fg and bg are nil."
871 (when (or fg bg)
872 (let ((fixed (cond ((null fg) 'fg)
873 ((null bg) 'bg))))
874 ;; Convert colors to hexadecimal, or set them to default.
875 (let ((fg (or (shr-color->hexadecimal fg)
876 (frame-parameter nil 'foreground-color)))
877 (bg (or (shr-color->hexadecimal bg)
878 (frame-parameter nil 'background-color))))
879 (cond ((eq fixed 'bg)
880 ;; Only return the new fg
881 (list nil (cadr (shr-color-visible bg fg t))))
882 ((eq fixed 'fg)
883 ;; Invert args and results and return only the new bg
884 (list (cadr (shr-color-visible fg bg t)) nil))
885 (t
886 (shr-color-visible bg fg)))))))
887
04db63bc 888(defun shr-colorize-region (start end fg &optional bg)
be2aa135
LMI
889 (when (and (not shr-inhibit-decoration)
890 (or fg bg))
04db63bc 891 (let ((new-colors (shr-color-check fg bg)))
144b7b5c 892 (when new-colors
60568d74 893 (when fg
be2aa135
LMI
894 (add-face-text-property start end
895 (list :foreground (cadr new-colors))
896 t))
04db63bc 897 (when bg
be2aa135
LMI
898 (add-face-text-property start end
899 (list :background (car new-colors))
900 t)))
ec72bf63 901 new-colors)))
04db63bc 902
c5ecc769
G
903(defun shr-expand-newlines (start end color)
904 (save-restriction
d709b79a
LI
905 ;; Skip past all white space at the start and ends.
906 (goto-char start)
907 (skip-chars-forward " \t\n")
908 (beginning-of-line)
909 (setq start (point))
910 (goto-char end)
911 (skip-chars-backward " \t\n")
912 (forward-line 1)
913 (setq end (point))
c5ecc769 914 (narrow-to-region start end)
160ae063 915 (let ((width (shr-buffer-width))
c5ecc769
G
916 column)
917 (goto-char (point-min))
918 (while (not (eobp))
919 (end-of-line)
19e0dbe0
KY
920 (when (and (< (setq column (current-column)) width)
921 (< (setq column (shr-previous-newline-padding-width column))
d709b79a 922 width))
7304e4dd 923 (let ((overlay (make-overlay (point) (1+ (point)))))
c5ecc769 924 (overlay-put overlay 'before-string
d709b79a
LI
925 (concat
926 (mapconcat
927 (lambda (overlay)
14596870
KY
928 (let ((string (plist-get
929 (overlay-properties overlay)
930 'before-string)))
d709b79a
LI
931 (if (not string)
932 ""
933 (overlay-put overlay 'before-string "")
934 string)))
935 (overlays-at (point))
936 "")
19e0dbe0 937 (propertize (make-string (- width column) ? )
d709b79a 938 'face (list :background color))))))
c5ecc769 939 (forward-line 1)))))
04db63bc 940
d709b79a
LI
941(defun shr-previous-newline-padding-width (width)
942 (let ((overlays (overlays-at (point)))
943 (previous-width 0))
944 (if (null overlays)
945 width
946 (dolist (overlay overlays)
947 (setq previous-width
948 (+ previous-width
14596870
KY
949 (length (plist-get (overlay-properties overlay)
950 'before-string)))))
d709b79a
LI
951 (+ width previous-width))))
952
66627fa9
G
953;;; Tag-specific rendering rules.
954
144b7b5c 955(defun shr-tag-body (cont)
04db63bc 956 (let* ((start (point))
af4e5f4c
G
957 (fgcolor (cdr (or (assq :fgcolor cont)
958 (assq :text cont))))
04db63bc 959 (bgcolor (cdr (assq :bgcolor cont)))
b31b26b4
G
960 (shr-stylesheet (list (cons 'color fgcolor)
961 (cons 'background-color bgcolor))))
144b7b5c 962 (shr-generic cont)
04db63bc 963 (shr-colorize-region start (point) fgcolor bgcolor)))
144b7b5c 964
b31b26b4
G
965(defun shr-tag-style (cont)
966 )
967
f73341e2
LMI
968(defun shr-tag-script (cont)
969 )
970
fb1b0ef6
LMI
971(defun shr-tag-comment (cont)
972 )
973
c74cb344
G
974(defun shr-dom-to-xml (dom)
975 "Convert DOM into a string containing the xml representation."
976 (let ((arg " ")
977 (text ""))
978 (dolist (sub (cdr dom))
979 (cond
980 ((listp (cdr sub))
981 (setq text (concat text (shr-dom-to-xml sub))))
982 ((eq (car sub) 'text)
983 (setq text (concat text (cdr sub))))
984 (t
985 (setq arg (concat arg (format "%s=\"%s\" "
986 (substring (symbol-name (car sub)) 1)
987 (cdr sub)))))))
988 (format "<%s%s>%s</%s>"
989 (car dom)
990 (substring arg 0 (1- (length arg)))
991 text
992 (car dom))))
993
65e704b9 994(defun shr-tag-svg (cont)
c74cb344
G
995 (when (image-type-available-p 'svg)
996 (funcall shr-put-image-function
997 (shr-dom-to-xml (cons 'svg cont))
998 "SVG Image")))
65e704b9 999
a3af2929
LMI
1000(defun shr-tag-sup (cont)
1001 (let ((start (point)))
1002 (shr-generic cont)
1003 (put-text-property start (point) 'display '(raise 0.5))))
1004
1005(defun shr-tag-sub (cont)
1006 (let ((start (point)))
1007 (shr-generic cont)
1008 (put-text-property start (point) 'display '(raise -0.5))))
1009
7bafe9bc
LMI
1010(defun shr-tag-label (cont)
1011 (shr-generic cont)
1012 (shr-ensure-paragraph))
1013
66627fa9
G
1014(defun shr-tag-p (cont)
1015 (shr-ensure-paragraph)
f7aa248a 1016 (shr-indent)
66627fa9
G
1017 (shr-generic cont)
1018 (shr-ensure-paragraph))
1019
036d93bc
KY
1020(defun shr-tag-div (cont)
1021 (shr-ensure-newline)
1022 (shr-indent)
1023 (shr-generic cont)
1024 (shr-ensure-newline))
1025
6eee2678
LMI
1026(defun shr-tag-s (cont)
1027 (shr-fontize-cont cont 'shr-strike-through))
1028
55385ebc
JD
1029(defun shr-tag-del (cont)
1030 (shr-fontize-cont cont 'shr-strike-through))
1031
66627fa9
G
1032(defun shr-tag-b (cont)
1033 (shr-fontize-cont cont 'bold))
1034
1035(defun shr-tag-i (cont)
1036 (shr-fontize-cont cont 'italic))
1037
1038(defun shr-tag-em (cont)
087d8265 1039 (shr-fontize-cont cont 'italic))
66627fa9 1040
530f7b67
LMI
1041(defun shr-tag-strong (cont)
1042 (shr-fontize-cont cont 'bold))
1043
66627fa9
G
1044(defun shr-tag-u (cont)
1045 (shr-fontize-cont cont 'underline))
1046
2e76c12c
LMI
1047(defun shr-parse-style (style)
1048 (when style
a2994808
JD
1049 (save-match-data
1050 (when (string-match "\n" style)
1051 (setq style (replace-match " " t t style))))
2e76c12c
LMI
1052 (let ((plist nil))
1053 (dolist (elem (split-string style ";"))
1054 (when elem
1055 (setq elem (split-string elem ":"))
1056 (when (and (car elem)
1057 (cadr elem))
1058 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1059 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
144b7b5c
G
1060 (when (string-match " *!important\\'" value)
1061 (setq value (substring value 0 (match-beginning 0))))
2e76c12c
LMI
1062 (push (cons (intern name obarray)
1063 value)
1064 plist)))))
1065 plist)))
1066
dbd5ffad 1067(defun shr-tag-base (cont)
be2aa135
LMI
1068 (let ((base (cdr (assq :href cont))))
1069 (when base
1070 (setq shr-base (shr-parse-base base))))
266c63b5 1071 (shr-generic cont))
dbd5ffad 1072
66627fa9
G
1073(defun shr-tag-a (cont)
1074 (let ((url (cdr (assq :href cont)))
04db63bc 1075 (title (cdr (assq :title cont)))
66627fa9
G
1076 (start (point))
1077 shr-start)
1078 (shr-generic cont)
be2aa135
LMI
1079 (when (and url
1080 (not shr-inhibit-decoration))
cc21c235 1081 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
de635afe
G
1082
1083(defun shr-tag-object (cont)
99e65b2d
G
1084 (let ((start (point))
1085 url)
1086 (dolist (elem cont)
1087 (when (eq (car elem) 'embed)
1088 (setq url (or url (cdr (assq :src (cdr elem))))))
1089 (when (and (eq (car elem) 'param)
1090 (equal (cdr (assq :name (cdr elem))) "movie"))
1091 (setq url (or url (cdr (assq :value (cdr elem)))))))
de635afe
G
1092 (when url
1093 (shr-insert " [multimedia] ")
dbd5ffad 1094 (shr-urlify start (shr-expand-url url)))
99e65b2d
G
1095 (shr-generic cont)))
1096
1097(defun shr-tag-video (cont)
1098 (let ((image (cdr (assq :poster cont)))
1099 (url (cdr (assq :src cont)))
1100 (start (point)))
1101 (shr-tag-img nil image)
dbd5ffad 1102 (shr-urlify start (shr-expand-url url))))
ab67634f 1103
99e65b2d
G
1104(defun shr-tag-img (cont &optional url)
1105 (when (or url
1106 (and cont
1107 (cdr (assq :src cont))))
68f6bd17
KY
1108 (when (and (> (current-column) 0)
1109 (not (eq shr-state 'image)))
1110 (insert "\n"))
1111 (let ((alt (cdr (assq :alt cont)))
dbd5ffad 1112 (url (shr-expand-url (or url (cdr (assq :src cont))))))
68f6bd17
KY
1113 (let ((start (point-marker)))
1114 (when (zerop (length alt))
953d41c4 1115 (setq alt "*"))
68f6bd17 1116 (cond
99e65b2d
G
1117 ((or (member (cdr (assq :height cont)) '("0" "1"))
1118 (member (cdr (assq :width cont)) '("0" "1")))
1119 ;; Ignore zero-sized or single-pixel images.
1120 )
2250b351
DE
1121 ((and (not shr-inhibit-images)
1122 (string-match "\\`data:" url))
1123 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1124 (if image
1125 (funcall shr-put-image-function image alt)
1126 (insert alt))))
68f6bd17
KY
1127 ((and (not shr-inhibit-images)
1128 (string-match "\\`cid:" url))
1129 (let ((url (substring url (match-end 0)))
1130 image)
1131 (if (or (not shr-content-function)
1132 (not (setq image (funcall shr-content-function url))))
1133 (insert alt)
b9bdaf74 1134 (funcall shr-put-image-function image alt))))
68f6bd17
KY
1135 ((or shr-inhibit-images
1136 (and shr-blocked-images
1137 (string-match shr-blocked-images url)))
1138 (setq shr-start (point))
1139 (let ((shr-state 'space))
b354bc53
KY
1140 (if (> (string-width alt) 8)
1141 (shr-insert (truncate-string-to-width alt 8))
68f6bd17 1142 (shr-insert alt))))
728518c3
LMI
1143 ((and (not shr-ignore-cache)
1144 (url-is-cached (shr-encode-url url)))
b9bdaf74 1145 (funcall shr-put-image-function (shr-get-image-data url) alt))
68f6bd17 1146 (t
64522086 1147 (insert alt " ")
728518c3
LMI
1148 (when (and shr-ignore-cache
1149 (url-is-cached (shr-encode-url url)))
1150 (let ((file (url-cache-create-filename (shr-encode-url url))))
1151 (when (file-exists-p file)
1152 (delete-file file))))
038b3495 1153 (url-queue-retrieve
f3b146e9 1154 (shr-encode-url url) 'shr-image-fetched
64522086 1155 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
038b3495 1156 t t)))
a959fc40
KY
1157 (when (zerop shr-table-depth) ;; We are not in a table.
1158 (put-text-property start (point) 'keymap shr-map)
1159 (put-text-property start (point) 'shr-alt alt)
1160 (put-text-property start (point) 'image-url url)
1161 (put-text-property start (point) 'image-displayer
1162 (shr-image-displayer shr-content-function))
1163 (put-text-property start (point) 'help-echo alt))
68f6bd17 1164 (setq shr-state 'image)))))
66627fa9
G
1165
1166(defun shr-tag-pre (cont)
1167 (let ((shr-folding-mode 'none))
1168 (shr-ensure-newline)
f7aa248a 1169 (shr-indent)
66627fa9
G
1170 (shr-generic cont)
1171 (shr-ensure-newline)))
1172
1173(defun shr-tag-blockquote (cont)
1174 (shr-ensure-paragraph)
f7aa248a 1175 (shr-indent)
66627fa9
G
1176 (let ((shr-indentation (+ shr-indentation 4)))
1177 (shr-generic cont))
1178 (shr-ensure-paragraph))
a41c2e6d 1179
d2aa9780
LMI
1180(defun shr-tag-dl (cont)
1181 (shr-ensure-paragraph)
1182 (shr-generic cont)
1183 (shr-ensure-paragraph))
1184
1185(defun shr-tag-dt (cont)
1186 (shr-ensure-newline)
1187 (shr-generic cont)
1188 (shr-ensure-newline))
1189
1190(defun shr-tag-dd (cont)
1191 (shr-ensure-newline)
1192 (let ((shr-indentation (+ shr-indentation 4)))
1193 (shr-generic cont)))
1194
a41c2e6d
G
1195(defun shr-tag-ul (cont)
1196 (shr-ensure-paragraph)
1197 (let ((shr-list-mode 'ul))
3d319c8f
LMI
1198 (shr-generic cont))
1199 (shr-ensure-paragraph))
a41c2e6d
G
1200
1201(defun shr-tag-ol (cont)
3d319c8f 1202 (shr-ensure-paragraph)
a41c2e6d 1203 (let ((shr-list-mode 1))
3d319c8f
LMI
1204 (shr-generic cont))
1205 (shr-ensure-paragraph))
a41c2e6d
G
1206
1207(defun shr-tag-li (cont)
c74cb344 1208 (shr-ensure-newline)
f7aa248a 1209 (shr-indent)
8028ed5c
LMI
1210 (let* ((bullet
1211 (if (numberp shr-list-mode)
1212 (prog1
1213 (format "%d " shr-list-mode)
1214 (setq shr-list-mode (1+ shr-list-mode)))
c74cb344 1215 shr-bullet))
8028ed5c
LMI
1216 (shr-indentation (+ shr-indentation (length bullet))))
1217 (insert bullet)
1218 (shr-generic cont)))
a41c2e6d
G
1219
1220(defun shr-tag-br (cont)
89b163db
G
1221 (when (and (not (bobp))
1222 ;; Only add a newline if we break the current line, or
1223 ;; the previous line isn't a blank line.
1224 (or (not (bolp))
1225 (and (> (- (point) 2) (point-min))
1226 (not (= (char-after (- (point) 2)) ?\n)))))
f7aa248a
G
1227 (insert "\n")
1228 (shr-indent))
a41c2e6d
G
1229 (shr-generic cont))
1230
308c9d24 1231(defun shr-tag-span (cont)
be2aa135 1232 (shr-generic cont))
308c9d24 1233
a41c2e6d 1234(defun shr-tag-h1 (cont)
e4dbdb09 1235 (shr-heading cont 'bold 'underline))
a41c2e6d
G
1236
1237(defun shr-tag-h2 (cont)
e4dbdb09 1238 (shr-heading cont 'bold))
a41c2e6d
G
1239
1240(defun shr-tag-h3 (cont)
e4dbdb09 1241 (shr-heading cont 'italic))
a41c2e6d
G
1242
1243(defun shr-tag-h4 (cont)
e4dbdb09 1244 (shr-heading cont))
a41c2e6d
G
1245
1246(defun shr-tag-h5 (cont)
e4dbdb09 1247 (shr-heading cont))
a41c2e6d
G
1248
1249(defun shr-tag-h6 (cont)
e4dbdb09 1250 (shr-heading cont))
a41c2e6d 1251
3d319c8f
LMI
1252(defun shr-tag-hr (cont)
1253 (shr-ensure-newline)
6b7df8d3 1254 (insert (make-string shr-width shr-hr-line) "\n"))
3d319c8f 1255
144b7b5c
G
1256(defun shr-tag-title (cont)
1257 (shr-heading cont 'bold 'underline))
1258
1110d53b 1259(defun shr-tag-font (cont)
b31b26b4
G
1260 (let* ((start (point))
1261 (color (cdr (assq :color cont)))
1262 (shr-stylesheet (nconc (list (cons 'color color))
1263 shr-stylesheet)))
1110d53b 1264 (shr-generic cont)
b31b26b4
G
1265 (when color
1266 (shr-colorize-region start (point) color
1267 (cdr (assq 'background-color shr-stylesheet))))))
1110d53b 1268
66627fa9 1269;;; Table rendering algorithm.
a41c2e6d 1270
a0ec382a
LMI
1271;; Table rendering is the only complicated thing here. We do this by
1272;; first counting how many TDs there are in each TR, and registering
1273;; how wide they think they should be ("width=45%", etc). Then we
1274;; render each TD separately (this is done in temporary buffers, so
1275;; that we can use all the rendering machinery as if we were in the
1276;; main buffer). Now we know how much space each TD really takes, so
1277;; we then render everything again with the new widths, and finally
1278;; insert all these boxes into the main buffer.
6c769311 1279(defun shr-tag-table-1 (cont)
71e691a5
G
1280 (setq cont (or (cdr (assq 'tbody cont))
1281 cont))
130e977f 1282 (let* ((shr-inhibit-images t)
99e65b2d 1283 (shr-table-depth (1+ shr-table-depth))
83ffd571 1284 (shr-kinsoku-shorten t)
a0ec382a 1285 ;; Find all suggested widths.
130e977f 1286 (columns (shr-column-specs cont))
a0ec382a 1287 ;; Compute how many characters wide each TD should be.
71e691a5 1288 (suggested-widths (shr-pro-rate-columns columns))
a0ec382a
LMI
1289 ;; Do a "test rendering" to see how big each TD is (this can
1290 ;; be smaller (if there's little text) or bigger (if there's
1291 ;; unbreakable text).
71e691a5 1292 (sketch (shr-make-table cont suggested-widths))
160ae063
LMI
1293 ;; Compute the "natural" width by setting each column to 500
1294 ;; characters and see how wide they really render.
1295 (natural (shr-make-table cont (make-vector (length columns) 500)))
1296 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
030158f3 1297 ;; This probably won't work very well.
83ffd571
KY
1298 (when (> (+ (loop for width across sketch-widths
1299 summing (1+ width))
1300 shr-indentation 1)
030158f3
G
1301 (frame-width))
1302 (setq truncate-lines t))
a0ec382a 1303 ;; Then render the table again with these new "hard" widths.
f462d10a 1304 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
130e977f 1305
6c769311
KY
1306(defun shr-tag-table (cont)
1307 (shr-ensure-paragraph)
1308 (let* ((caption (cdr (assq 'caption cont)))
1309 (header (cdr (assq 'thead cont)))
1310 (body (or (cdr (assq 'tbody cont)) cont))
1311 (footer (cdr (assq 'tfoot cont)))
144b7b5c 1312 (bgcolor (cdr (assq :bgcolor cont)))
60568d74
LMI
1313 (start (point))
1314 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1315 shr-stylesheet))
6c769311
KY
1316 (nheader (if header (shr-max-columns header)))
1317 (nbody (if body (shr-max-columns body)))
1318 (nfooter (if footer (shr-max-columns footer))))
2146e256
LMI
1319 (if (and (not caption)
1320 (not header)
1321 (not (cdr (assq 'tbody cont)))
1322 (not (cdr (assq 'tr cont)))
1323 (not footer))
1324 ;; The table is totally invalid and just contains random junk.
1325 ;; Try to output it anyway.
1326 (shr-generic cont)
1327 ;; It's a real table, so render it.
1328 (shr-tag-table-1
1329 (nconc
1330 (if caption `((tr (td ,@caption))))
1331 (if header
1332 (if footer
1333 ;; hader + body + footer
1334 (if (= nheader nbody)
1335 (if (= nbody nfooter)
1336 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
1337 (nconc `((tr (td (table (tbody ,@header ,@body)))))
1338 (if (= nfooter 1)
1339 footer
1340 `((tr (td (table (tbody ,@footer))))))))
1341 (nconc `((tr (td (table (tbody ,@header)))))
1342 (if (= nbody nfooter)
1343 `((tr (td (table (tbody ,@body ,@footer)))))
1344 (nconc `((tr (td (table (tbody ,@body)))))
1345 (if (= nfooter 1)
1346 footer
1347 `((tr (td (table (tbody ,@footer))))))))))
1348 ;; header + body
3c066373 1349 (if (= nheader nbody)
2146e256
LMI
1350 `((tr (td (table (tbody ,@header ,@body)))))
1351 (if (= nheader 1)
1352 `(,@header (tr (td (table (tbody ,@body)))))
1353 `((tr (td (table (tbody ,@header))))
1354 (tr (td (table (tbody ,@body))))))))
1355 (if footer
1356 ;; body + footer
1357 (if (= nbody nfooter)
1358 `((tr (td (table (tbody ,@body ,@footer)))))
1359 (nconc `((tr (td (table (tbody ,@body)))))
1360 (if (= nfooter 1)
1361 footer
1362 `((tr (td (table (tbody ,@footer))))))))
1363 (if caption
1364 `((tr (td (table (tbody ,@body)))))
1365 body))))))
60568d74
LMI
1366 (when bgcolor
1367 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
f462d10a
LMI
1368 bgcolor))
1369 ;; Finally, insert all the images after the table. The Emacs buffer
1370 ;; model isn't strong enough to allow us to put the images actually
1371 ;; into the tables.
1372 (when (zerop shr-table-depth)
1373 (dolist (elem (shr-find-elements cont 'img))
1374 (shr-tag-img (cdr elem))))))
6c769311 1375
130e977f
LMI
1376(defun shr-find-elements (cont type)
1377 (let (result)
1378 (dolist (elem cont)
1379 (cond ((eq (car elem) type)
1380 (push elem result))
1381 ((consp (cdr elem))
1382 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1383 (nreverse result)))
71e691a5
G
1384
1385(defun shr-insert-table (table widths)
be2aa135
LMI
1386 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1387 "collapse"))
924d6997 1388 (shr-table-separator-length (if collapse 0 1))
be2aa135
LMI
1389 (shr-table-vertical-line (if collapse "" shr-table-vertical-line)))
1390 (unless collapse
1391 (shr-insert-table-ruler widths))
1392 (dolist (row table)
1393 (let ((start (point))
1394 (height (let ((max 0))
1395 (dolist (column row)
1396 (setq max (max max (cadr column))))
1397 max)))
1398 (dotimes (i height)
1399 (shr-indent)
1400 (insert shr-table-vertical-line "\n"))
1401 (dolist (column row)
1402 (goto-char start)
1403 (let ((lines (nth 2 column)))
1404 (dolist (line lines)
1405 (end-of-line)
1406 (insert line shr-table-vertical-line)
1407 (forward-line 1))
1408 ;; Add blank lines at padding at the bottom of the TD,
1409 ;; possibly.
1410 (dotimes (i (- height (length lines)))
1411 (end-of-line)
1412 (let ((start (point)))
1413 (insert (make-string (string-width (car lines)) ? )
1414 shr-table-vertical-line)
1415 (when (nth 4 column)
1416 (shr-add-font start (1- (point))
1417 (list :background (nth 4 column)))))
1418 (forward-line 1)))))
1419 (unless collapse
1420 (shr-insert-table-ruler widths)))))
71e691a5
G
1421
1422(defun shr-insert-table-ruler (widths)
924d6997
G
1423 (when shr-table-horizontal-line
1424 (when (and (bolp)
1425 (> shr-indentation 0))
1426 (shr-indent))
1427 (insert shr-table-corner)
1428 (dotimes (i (length widths))
1429 (insert (make-string (aref widths i) shr-table-horizontal-line)
1430 shr-table-corner))
1431 (insert "\n")))
71e691a5 1432
160ae063 1433(defun shr-table-widths (table natural-table suggested-widths)
a7dcc87b
G
1434 (let* ((length (length suggested-widths))
1435 (widths (make-vector length 0))
1436 (natural-widths (make-vector length 0)))
71e691a5
G
1437 (dolist (row table)
1438 (let ((i 0))
1439 (dolist (column row)
160ae063
LMI
1440 (aset widths i (max (aref widths i) column))
1441 (setq i (1+ i)))))
1442 (dolist (row natural-table)
1443 (let ((i 0))
1444 (dolist (column row)
1445 (aset natural-widths i (max (aref natural-widths i) column))
a7dcc87b 1446 (setq i (1+ i)))))
863b61d6
KY
1447 (let ((extra (- (apply '+ (append suggested-widths nil))
1448 (apply '+ (append widths nil))))
a7dcc87b 1449 (expanded-columns 0))
160ae063
LMI
1450 ;; We have extra, unused space, so divide this space amongst the
1451 ;; columns.
a7dcc87b 1452 (when (> extra 0)
160ae063
LMI
1453 ;; If the natural width is wider than the rendered width, we
1454 ;; want to allow the column to expand.
a7dcc87b 1455 (dotimes (i length)
a7dcc87b
G
1456 (when (> (aref natural-widths i) (aref widths i))
1457 (setq expanded-columns (1+ expanded-columns))))
1458 (dotimes (i length)
1459 (when (> (aref natural-widths i) (aref widths i))
1460 (aset widths i (min
160ae063 1461 (aref natural-widths i)
a7dcc87b
G
1462 (+ (/ extra expanded-columns)
1463 (aref widths i))))))))
71e691a5
G
1464 widths))
1465
1466(defun shr-make-table (cont widths &optional fill)
c74cb344
G
1467 (or (cadr (assoc (list cont widths fill) shr-content-cache))
1468 (let ((data (shr-make-table-1 cont widths fill)))
1469 (push (list (list cont widths fill) data)
1470 shr-content-cache)
1471 data)))
1472
1473(defun shr-make-table-1 (cont widths &optional fill)
be2aa135 1474 (let ((trs nil)
924d6997
G
1475 (shr-inhibit-decoration (not fill))
1476 (rowspans (make-vector (length widths) 0))
1477 width colspan)
71e691a5
G
1478 (dolist (row cont)
1479 (when (eq (car row) 'tr)
a0ec382a
LMI
1480 (let ((tds nil)
1481 (columns (cdr row))
1482 (i 0)
924d6997 1483 (width-column 0)
a0ec382a
LMI
1484 column)
1485 (while (< i (length widths))
924d6997
G
1486 ;; If we previously had a rowspan definition, then that
1487 ;; means that we now have a "missing" td/th element here.
1488 ;; So just insert a dummy, empty one to (sort of) emulate
1489 ;; rowspan.
1490 (setq column
1491 (if (zerop (aref rowspans i))
1492 (pop columns)
1493 (aset rowspans i (1- (aref rowspans i)))
1494 '(td)))
a0ec382a 1495 (when (or (memq (car column) '(td th))
924d6997
G
1496 (not column))
1497 (when (cdr (assq :rowspan (cdr column)))
1498 (aset rowspans i (+ (aref rowspans i)
1499 (1- (string-to-number
1500 (cdr (assq :rowspan (cdr column))))))))
970ad972
G
1501 ;; Sanity check for invalid column-spans.
1502 (when (>= width-column (length widths))
1503 (setq width-column 0))
924d6997
G
1504 (setq width
1505 (if column
1506 (aref widths width-column)
2fae38e5 1507 10))
924d6997
G
1508 (when (and fill
1509 (setq colspan (cdr (assq :colspan (cdr column)))))
2122cb6d
LMI
1510 (setq colspan (min (string-to-number colspan)
1511 ;; The colspan may be wrong, so
1512 ;; truncate it to the length of the
1513 ;; remaining columns.
1514 (- (length widths) i)))
924d6997
G
1515 (dotimes (j (1- colspan))
1516 (if (> (+ i 1 j) (1- (length widths)))
1517 (setq width (aref widths (1- (length widths))))
1518 (setq width (+ width
1519 shr-table-separator-length
1520 (aref widths (+ i 1 j))))))
1521 (setq width-column (+ width-column (1- colspan))))
1522 (when (or column
1523 (not fill))
1524 (push (shr-render-td (cdr column) width fill)
1525 tds))
1526 (setq i (1+ i)
1527 width-column (1+ width-column))))
71e691a5
G
1528 (push (nreverse tds) trs))))
1529 (nreverse trs)))
1530
1531(defun shr-render-td (cont width fill)
04db63bc 1532 (with-temp-buffer
60568d74
LMI
1533 (let ((bgcolor (cdr (assq :bgcolor cont)))
1534 (fgcolor (cdr (assq :fgcolor cont)))
1535 (style (cdr (assq :style cont)))
1536 (shr-stylesheet shr-stylesheet)
7304e4dd 1537 actual-colors)
60568d74
LMI
1538 (when style
1539 (setq style (and (string-match "color" style)
1540 (shr-parse-style style))))
1541 (when bgcolor
1542 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1543 (when fgcolor
1544 (setq style (nconc (list (cons 'color fgcolor)) style)))
1545 (when style
1546 (setq shr-stylesheet (append style shr-stylesheet)))
c74cb344
G
1547 (let ((shr-width width)
1548 (shr-indentation 0))
1549 (shr-descend (cons 'td cont)))
1550 ;; Delete padding at the bottom of the TDs.
1551 (delete-region
1552 (point)
1553 (progn
1554 (skip-chars-backward " \t\n")
1555 (end-of-line)
1556 (point)))
60568d74
LMI
1557 (goto-char (point-min))
1558 (let ((max 0))
1559 (while (not (eobp))
1560 (end-of-line)
1561 (setq max (max max (current-column)))
1562 (forward-line 1))
1563 (when fill
1564 (goto-char (point-min))
1565 ;; If the buffer is totally empty, then put a single blank
1566 ;; line here.
1567 (if (zerop (buffer-size))
1568 (insert (make-string width ? ))
1569 ;; Otherwise, fill the buffer.
544d4594
LMI
1570 (let ((align (cdr (assq :align cont)))
1571 length)
1572 (while (not (eobp))
1573 (end-of-line)
1574 (setq length (- width (current-column)))
1575 (when (> length 0)
1576 (cond
1577 ((equal align "right")
1578 (beginning-of-line)
1579 (insert (make-string length ? )))
1580 ((equal align "center")
1581 (insert (make-string (/ length 2) ? ))
1582 (beginning-of-line)
1583 (insert (make-string (- length (/ length 2)) ? )))
1584 (t
1585 (insert (make-string length ? )))))
1586 (forward-line 1))))
abb97fbb 1587 (when style
ec72bf63
G
1588 (setq actual-colors
1589 (shr-colorize-region
1590 (point-min) (point-max)
1591 (cdr (assq 'color shr-stylesheet))
1592 (cdr (assq 'background-color shr-stylesheet))))))
60568d74
LMI
1593 (if fill
1594 (list max
1595 (count-lines (point-min) (point-max))
1596 (split-string (buffer-string) "\n")
7304e4dd 1597 nil
ec72bf63 1598 (car actual-colors))
2122cb6d 1599 max)))))
a7dcc87b 1600
160ae063 1601(defun shr-buffer-width ()
a7dcc87b 1602 (goto-char (point-min))
160ae063 1603 (let ((max 0))
a7dcc87b
G
1604 (while (not (eobp))
1605 (end-of-line)
160ae063 1606 (setq max (max max (current-column)))
a7dcc87b
G
1607 (forward-line 1))
1608 max))
130e977f 1609
71e691a5
G
1610(defun shr-pro-rate-columns (columns)
1611 (let ((total-percentage 0)
1612 (widths (make-vector (length columns) 0)))
1613 (dotimes (i (length columns))
a7dcc87b 1614 (setq total-percentage (+ total-percentage (aref columns i))))
71e691a5
G
1615 (setq total-percentage (/ 1.0 total-percentage))
1616 (dotimes (i (length columns))
1617 (aset widths i (max (truncate (* (aref columns i)
1618 total-percentage
a7dcc87b 1619 (- shr-width (1+ (length columns)))))
71e691a5
G
1620 10)))
1621 widths))
1622
1623;; Return a summary of the number and shape of the TDs in the table.
1624(defun shr-column-specs (cont)
1625 (let ((columns (make-vector (shr-max-columns cont) 1)))
1626 (dolist (row cont)
1627 (when (eq (car row) 'tr)
1628 (let ((i 0))
1629 (dolist (column (cdr row))
1630 (when (memq (car column) '(td th))
1631 (let ((width (cdr (assq :width (cdr column)))))
1632 (when (and width
5d852256
LMI
1633 (string-match "\\([0-9]+\\)%" width)
1634 (not (zerop (setq width (string-to-number
1635 (match-string 1 width))))))
1636 (aset columns i (/ width 100.0))))
130e977f 1637 (setq i (1+ i)))))))
71e691a5
G
1638 columns))
1639
1640(defun shr-count (cont elem)
1641 (let ((i 0))
1642 (dolist (sub cont)
1643 (when (eq (car sub) elem)
1644 (setq i (1+ i))))
1645 i))
1646
1647(defun shr-max-columns (cont)
1648 (let ((max 0))
1649 (dolist (row cont)
1650 (when (eq (car row) 'tr)
130e977f
LMI
1651 (setq max (max max (+ (shr-count (cdr row) 'td)
1652 (shr-count (cdr row) 'th))))))
71e691a5
G
1653 max))
1654
f3fd95db 1655(provide 'shr)
367f7f81 1656
b4543a28 1657;; Local Variables:
c38e0c97 1658;; coding: utf-8
b4543a28
G
1659;; End:
1660
367f7f81 1661;;; shr.el ends here