* lisp/progmodes/python.el:
[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)
924d6997 199 (shr-width (or shr-width (1- (window-width)))))
9ed5a258
LI
200 (shr-descend (shr-transform-dom dom))
201 (shr-remove-trailing-whitespace start (point))))
202
203(defun shr-remove-trailing-whitespace (start end)
7c4bbb69
LI
204 (let ((width (window-width)))
205 (save-restriction
206 (narrow-to-region start end)
207 (goto-char start)
208 (while (not (eobp))
209 (end-of-line)
888ab661 210 (when (> (shr-previous-newline-padding-width (current-column)) width)
7c4bbb69
LI
211 (dolist (overlay (overlays-at (point)))
212 (when (overlay-get overlay 'before-string)
213 (overlay-put overlay 'before-string nil))))
214 (forward-line 1)))))
66627fa9
G
215
216(defun shr-copy-url ()
217 "Copy the URL under point to the kill ring.
218If called twice, then try to fetch the URL and see whether it
219redirects somewhere else."
220 (interactive)
221 (let ((url (get-text-property (point) 'shr-url)))
222 (cond
223 ((not url)
224 (message "No URL under point"))
225 ;; Resolve redirected URLs.
226 ((equal url (car kill-ring))
227 (url-retrieve
228 url
229 (lambda (a)
230 (when (and (consp a)
231 (eq (car a) :redirect))
232 (with-temp-buffer
233 (insert (cadr a))
234 (goto-char (point-min))
235 ;; Remove common tracking junk from the URL.
236 (when (re-search-forward ".utm_.*" nil t)
237 (replace-match "" t t))
238 (message "Copied %s" (buffer-string))
038b3495
LI
239 (copy-region-as-kill (point-min) (point-max)))))
240 nil t))
66627fa9
G
241 ;; Copy the URL to the kill ring.
242 (t
243 (with-temp-buffer
244 (insert url)
245 (copy-region-as-kill (point-min) (point-max))
246 (message "Copied %s" url))))))
247
7304e4dd
LMI
248(defun shr-next-link ()
249 "Skip to the next link."
250 (interactive)
be2aa135 251 (let ((skip (text-property-any (point) (point-max) 'help-echo nil)))
7304e4dd 252 (if (not (setq skip (text-property-not-all skip (point-max)
be2aa135 253 'help-echo nil)))
7304e4dd
LMI
254 (message "No next link")
255 (goto-char skip)
256 (message "%s" (get-text-property (point) 'help-echo)))))
257
258(defun shr-previous-link ()
259 "Skip to the previous link."
260 (interactive)
261 (let ((start (point))
262 (found nil))
263 ;; Skip past the current link.
264 (while (and (not (bobp))
be2aa135 265 (get-text-property (point) 'help-echo))
7304e4dd
LMI
266 (forward-char -1))
267 ;; Find the previous link.
268 (while (and (not (bobp))
be2aa135 269 (not (setq found (get-text-property (point) 'help-echo))))
7304e4dd
LMI
270 (forward-char -1))
271 (if (not found)
272 (progn
273 (message "No previous link")
274 (goto-char start))
275 ;; Put point at the start of the link.
276 (while (and (not (bobp))
be2aa135 277 (get-text-property (point) 'help-echo))
7304e4dd
LMI
278 (forward-char -1))
279 (forward-char 1)
280 (message "%s" (get-text-property (point) 'help-echo)))))
281
66627fa9
G
282(defun shr-show-alt-text ()
283 "Show the ALT text of the image under point."
284 (interactive)
285 (let ((text (get-text-property (point) 'shr-alt)))
286 (if (not text)
287 (message "No image under point")
288 (message "%s" text))))
289
2da9c605
G
290(defun shr-browse-image (&optional copy-url)
291 "Browse the image under point.
292If COPY-URL (the prefix if called interactively) is non-nil, copy
293the URL of the image to the kill buffer instead."
294 (interactive "P")
8b6f6573 295 (let ((url (get-text-property (point) 'image-url)))
2da9c605
G
296 (cond
297 ((not url)
298 (message "No image under point"))
299 (copy-url
300 (with-temp-buffer
301 (insert url)
302 (copy-region-as-kill (point-min) (point-max))
303 (message "Copied %s" url)))
304 (t
66627fa9 305 (message "Browsing %s..." url)
2da9c605 306 (browse-url url)))))
66627fa9 307
3d319c8f
LMI
308(defun shr-insert-image ()
309 "Insert the image under point into the buffer."
310 (interactive)
8b6f6573 311 (let ((url (get-text-property (point) 'image-url)))
3d319c8f
LMI
312 (if (not url)
313 (message "No image under point")
314 (message "Inserting %s..." url)
315 (url-retrieve url 'shr-image-fetched
316 (list (current-buffer) (1- (point)) (point-marker))
038b3495 317 t t))))
3d319c8f 318
89b163db
G
319(defun shr-zoom-image ()
320 "Toggle the image size.
321The size will be rotated between the default size, the original
322size, and full-buffer size."
323 (interactive)
324 (let ((url (get-text-property (point) 'image-url))
325 (size (get-text-property (point) 'image-size))
326 (buffer-read-only nil))
327 (if (not url)
328 (message "No image under point")
329 ;; Delete the old picture.
330 (while (get-text-property (point) 'image-url)
331 (forward-char -1))
332 (forward-char 1)
333 (let ((start (point)))
334 (while (get-text-property (point) 'image-url)
335 (forward-char 1))
336 (forward-char -1)
337 (put-text-property start (point) 'display nil)
338 (when (> (- (point) start) 2)
339 (delete-region start (1- (point)))))
340 (message "Inserting %s..." url)
341 (url-retrieve url 'shr-image-fetched
342 (list (current-buffer) (1- (point)) (point-marker)
343 (list (cons 'size
344 (cond ((or (eq size 'default)
345 (null size))
346 'original)
347 ((eq size 'original)
348 'full)
349 ((eq size 'full)
350 'default)))))
351 t))))
352
66627fa9
G
353;;; Utility functions.
354
870409d4
G
355(defun shr-transform-dom (dom)
356 (let ((result (list (pop dom))))
357 (dolist (arg (pop dom))
358 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
359 (cdr arg))
360 result))
361 (dolist (sub dom)
362 (if (stringp sub)
953d41c4 363 (push (cons 'text sub) result)
870409d4
G
364 (push (shr-transform-dom sub) result)))
365 (nreverse result)))
366
870409d4 367(defun shr-descend (dom)
2644071e
LMI
368 (let ((function
369 (or
370 ;; Allow other packages to override (or provide) rendering
371 ;; of elements.
372 (cdr (assq (car dom) shr-external-rendering-functions))
373 (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
ebe79557 374 (style (cdr (assq :style (cdr dom))))
04db63bc 375 (shr-stylesheet shr-stylesheet)
ebe79557 376 (start (point)))
b31b26b4 377 (when style
be2aa135 378 (if (string-match "color\\|display\\|border-collapse" style)
b31b26b4
G
379 (setq shr-stylesheet (nconc (shr-parse-style style)
380 shr-stylesheet))
381 (setq style nil)))
e9145444 382 ;; If we have a display:none, then just ignore this part of the DOM.
c74cb344
G
383 (unless (equal (cdr (assq 'display shr-stylesheet)) "none")
384 (if (fboundp function)
385 (funcall function (cdr dom))
386 (shr-generic (cdr dom)))
387 (when (and shr-target-id
388 (equal (cdr (assq :id (cdr dom))) shr-target-id))
389 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
390 ;; If style is set, then this node has set the color.
391 (when style
392 (shr-colorize-region start (point)
393 (cdr (assq 'color shr-stylesheet))
394 (cdr (assq 'background-color shr-stylesheet)))))))
870409d4
G
395
396(defun shr-generic (cont)
397 (dolist (sub cont)
398 (cond
953d41c4 399 ((eq (car sub) 'text)
870409d4 400 (shr-insert (cdr sub)))
a41c2e6d 401 ((listp (cdr sub))
870409d4
G
402 (shr-descend sub)))))
403
ed797193
G
404(defmacro shr-char-breakable-p (char)
405 "Return non-nil if a line can be broken before and after CHAR."
406 `(aref fill-find-break-point-function-table ,char))
407(defmacro shr-char-nospace-p (char)
408 "Return non-nil if no space is required before and after CHAR."
409 `(aref fill-nospace-between-words-table ,char))
410
411;; KINSOKU is a Japanese word meaning a rule that should not be violated.
412;; In Emacs, it is a term used for characters, e.g. punctuation marks,
413;; parentheses, and so on, that should not be placed in the beginning
414;; of a line or the end of a line.
415(defmacro shr-char-kinsoku-bol-p (char)
416 "Return non-nil if a line ought not to begin with CHAR."
417 `(aref (char-category-set ,char) ?>))
418(defmacro shr-char-kinsoku-eol-p (char)
419 "Return non-nil if a line ought not to end with CHAR."
420 `(aref (char-category-set ,char) ?<))
421(unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
422 (load "kinsoku" nil t))
423
66627fa9 424(defun shr-insert (text)
6b7df8d3 425 (when (and (eq shr-state 'image)
89b163db 426 (not (bolp))
6b7df8d3 427 (not (string-match "\\`[ \t\n]+\\'" text)))
66627fa9
G
428 (insert "\n")
429 (setq shr-state nil))
430 (cond
431 ((eq shr-folding-mode 'none)
432 (insert text))
433 (t
c38e0c97 434 (when (and (string-match "\\`[ \t\n ]" text)
73db8b08
KY
435 (not (bolp))
436 (not (eq (char-after (1- (point))) ? )))
437 (insert " "))
c38e0c97 438 (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t))
73db8b08
KY
439 (when (and (bolp)
440 (> shr-indentation 0))
441 (shr-indent))
73db8b08 442 ;; No space is needed behind a wide character categorized as
b41c2f65
KY
443 ;; kinsoku-bol, between characters both categorized as nospace,
444 ;; or at the beginning of a line.
73db8b08 445 (let (prev)
48ba8195
KY
446 (when (and (> (current-column) shr-indentation)
447 (eq (preceding-char) ? )
20438017 448 (or (= (line-beginning-position) (1- (point)))
ed797193
G
449 (and (shr-char-breakable-p
450 (setq prev (char-after (- (point) 2))))
451 (shr-char-kinsoku-bol-p prev))
452 (and (shr-char-nospace-p prev)
453 (shr-char-nospace-p (aref elem 0)))))
73db8b08 454 (delete-char -1)))
48ba8195
KY
455 ;; The shr-start is a special variable that is used to pass
456 ;; upwards the first point in the buffer where the text really
457 ;; starts.
458 (unless shr-start
459 (setq shr-start (point)))
73db8b08 460 (insert elem)
e76917e6 461 (setq shr-state nil)
e7102c0a
KY
462 (let (found)
463 (while (and (> (current-column) shr-width)
cf91cee7 464 (> shr-width 0)
e7102c0a
KY
465 (progn
466 (setq found (shr-find-fill-point))
b40950bf 467 (not (eolp))))
fe98a42f
KY
468 (when (eq (preceding-char) ? )
469 (delete-char -1))
470 (insert "\n")
471 (unless found
e7102c0a
KY
472 ;; No space is needed at the beginning of a line.
473 (when (eq (following-char) ? )
474 (delete-char 1)))
475 (when (> shr-indentation 0)
476 (shr-indent))
477 (end-of-line))
478 (insert " ")))
c38e0c97 479 (unless (string-match "[ \t\r\n ]\\'" text)
73db8b08 480 (delete-char -1)))))
66627fa9 481
6b7df8d3 482(defun shr-find-fill-point ()
83ffd571
KY
483 (when (> (move-to-column shr-width) shr-width)
484 (backward-char 1))
ed797193
G
485 (let ((bp (point))
486 failed)
487 (while (not (or (setq failed (= (current-column) shr-indentation))
488 (eq (preceding-char) ? )
489 (eq (following-char) ? )
490 (shr-char-breakable-p (preceding-char))
491 (shr-char-breakable-p (following-char))
7454326a
G
492 (if (eq (preceding-char) ?')
493 (not (memq (char-after (- (point) 2))
494 (list nil ?\n ? )))
7454326a 495 (and (shr-char-kinsoku-bol-p (preceding-char))
6568edea 496 (shr-char-breakable-p (following-char))
7454326a 497 (not (shr-char-kinsoku-bol-p (following-char)))))
ed797193 498 (shr-char-kinsoku-eol-p (following-char))))
83ffd571 499 (backward-char 1))
ed797193
G
500 (if (and (not (or failed (eolp)))
501 (eq (preceding-char) ?'))
502 (while (not (or (setq failed (eolp))
503 (eq (following-char) ? )
504 (shr-char-breakable-p (following-char))
505 (shr-char-kinsoku-eol-p (following-char))))
506 (forward-char 1)))
83ffd571 507 (if failed
20438017 508 ;; There's no breakable point, so we give it up.
ed797193
G
509 (let (found)
510 (goto-char bp)
511 (unless shr-kinsoku-shorten
512 (while (and (setq found (re-search-forward
513 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
514 (line-end-position) 'move))
515 (eq (preceding-char) ?')))
516 (if (and found (not (match-beginning 1)))
517 (goto-char (match-beginning 0)))))
b40950bf
KY
518 (or
519 (eolp)
ed797193
G
520 ;; Don't put kinsoku-bol characters at the beginning of a line,
521 ;; or kinsoku-eol characters at the end of a line.
522 (cond
523 (shr-kinsoku-shorten
524 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
525 (shr-char-kinsoku-eol-p (preceding-char)))
526 (backward-char 1))
527 (when (setq failed (= (current-column) shr-indentation))
528 ;; There's no breakable point that doesn't violate kinsoku,
529 ;; so we look for the second best position.
530 (while (and (progn
531 (forward-char 1)
532 (<= (current-column) shr-width))
533 (progn
534 (setq bp (point))
535 (shr-char-kinsoku-eol-p (following-char)))))
536 (goto-char bp)))
537 ((shr-char-kinsoku-eol-p (preceding-char))
89b163db
G
538 ;; Find backward the point where kinsoku-eol characters begin.
539 (let ((count 4))
540 (while
541 (progn
542 (backward-char 1)
543 (and (> (setq count (1- count)) 0)
544 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
545 (or (shr-char-kinsoku-eol-p (preceding-char))
546 (shr-char-kinsoku-bol-p (following-char)))))))
547 (if (setq failed (= (current-column) shr-indentation))
548 ;; There's no breakable point that doesn't violate kinsoku,
549 ;; so we go to the second best position.
550 (if (looking-at "\\(\\c<+\\)\\c<")
551 (goto-char (match-end 1))
552 (forward-char 1))))
99e4926f
LI
553 ((and (shr-char-kinsoku-bol-p (following-char))
554 (not (eq (following-char) ?')))
89b163db
G
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
986ed135
GM
735;; Behind display-graphic-p test.
736(declare-function image-size "image.c" (spec &optional pixels frame))
737(declare-function image-animate "image" (image &optional index limit))
738
21c58ae2
LMI
739(defun shr-put-image (spec alt &optional flags)
740 "Insert image SPEC with a string ALT. Return image.
741SPEC is either an image data blob, or a list where the first
742element is the data blob and the second element is the content-type."
4abff904 743 (if (display-graphic-p)
89b163db 744 (let* ((size (cdr (assq 'size flags)))
21c58ae2
LMI
745 (data (if (consp spec)
746 (car spec)
747 spec))
748 (content-type (and (consp spec)
749 (cadr spec)))
89b163db
G
750 (start (point))
751 (image (cond
752 ((eq size 'original)
21c58ae2 753 (create-image data nil t :ascent 100
c6b7ccaa 754 :format content-type))
89b163db
G
755 ((eq size 'full)
756 (ignore-errors
e9145444 757 (shr-rescale-image data content-type)))
89b163db
G
758 (t
759 (ignore-errors
e9145444 760 (shr-rescale-image data content-type))))))
4abff904 761 (when image
c0f9edce
G
762 ;; When inserting big-ish pictures, put them at the
763 ;; beginning of the line.
764 (when (and (> (current-column) 0)
765 (> (car (image-size image t)) 400))
766 (insert "\n"))
89b163db 767 (if (eq size 'original)
7304e4dd 768 (insert-sliced-image image (or alt "*") nil 20 1)
89b163db
G
769 (insert-image image (or alt "*")))
770 (put-text-property start (point) 'image-size size)
dd8620de
GM
771 (when (cond ((fboundp 'image-multi-frame-p)
772 ;; Only animate multi-frame things that specify a
773 ;; delay; eg animated gifs as opposed to
774 ;; multi-page tiffs. FIXME?
775 (cdr (image-multi-frame-p image)))
776 ((fboundp 'image-animated-p)
777 (image-animated-p image)))
c146ad85 778 (image-animate image nil 60)))
b9bdaf74 779 image)
99e65b2d 780 (insert alt)))
870409d4 781
e9145444
SM
782(defun shr-rescale-image (data &optional content-type)
783 "Rescale DATA, if too big, to fit the current buffer."
784 (if (not (and (fboundp 'imagemagick-types)
785 (get-buffer-window (current-buffer))))
f3f9606c
LMI
786 (create-image data nil t :ascent 100)
787 (let ((edges (window-inside-pixel-edges
788 (get-buffer-window (current-buffer)))))
789 (create-image
790 data 'imagemagick t
791 :ascent 100
792 :max-width (truncate (* shr-max-image-proportion
793 (- (nth 2 edges) (nth 0 edges))))
794 :max-height (truncate (* shr-max-image-proportion
21c58ae2 795 (- (nth 3 edges) (nth 1 edges))))
c6b7ccaa 796 :format content-type))))
870409d4 797
85a45a69
GM
798;; url-cache-extract autoloads url-cache.
799(declare-function url-cache-create-filename "url-cache" (url))
800(autoload 'mm-disable-multibyte "mm-util")
39ddff39 801(autoload 'browse-url-mail "browse-url")
85a45a69 802
870409d4
G
803(defun shr-get-image-data (url)
804 "Get image data for URL.
805Return a string with image data."
806 (with-temp-buffer
807 (mm-disable-multibyte)
71e691a5 808 (when (ignore-errors
ab67634f 809 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
71e691a5
G
810 t)
811 (when (or (search-forward "\n\n" nil t)
812 (search-forward "\r\n\r\n" nil t))
21c58ae2
LMI
813 (shr-parse-image-data)))))
814
815(defun shr-parse-image-data ()
816 (list
817 (buffer-substring (point) (point-max))
818 (save-excursion
819 (save-restriction
820 (narrow-to-region (point-min) (point))
821 (let ((content-type (mail-fetch-field "content-type")))
822 (and content-type
823 (intern content-type obarray)))))))
870409d4 824
40de2c6d
KY
825(defun shr-image-displayer (content-function)
826 "Return a function to display an image.
827CONTENT-FUNCTION is a function to retrieve an image for a cid url that
828is an argument. The function to be returned takes three arguments URL,
53964682 829START, and END. Note that START and END should be markers."
40de2c6d 830 `(lambda (url start end)
f8d8a97b
KY
831 (when url
832 (if (string-match "\\`cid:" url)
833 ,(when content-function
834 `(let ((image (funcall ,content-function
835 (substring url (match-end 0)))))
836 (when image
837 (goto-char start)
b9bdaf74 838 (funcall shr-put-image-function
195b2593 839 image (buffer-substring start end))
e2d0ba98 840 (delete-region (point) end))))
f8d8a97b
KY
841 (url-retrieve url 'shr-image-fetched
842 (list (current-buffer) start end)
038b3495 843 t t)))))
40de2c6d 844
66627fa9
G
845(defun shr-heading (cont &rest types)
846 (shr-ensure-paragraph)
847 (apply #'shr-fontize-cont cont types)
848 (shr-ensure-paragraph))
849
04db63bc 850(defun shr-urlify (start url &optional title)
ca3cf0a5 851 (shr-add-font start (point) 'shr-link)
7304e4dd
LMI
852 (add-text-properties
853 start (point)
854 (list 'shr-url url
be2aa135 855 'help-echo (if title (format "%s (%s)" url title) url)
d50fceab 856 'follow-link t
5a2a7735 857 'mouse-face 'highlight
970ad972 858 'keymap shr-map)))
de635afe
G
859
860(defun shr-encode-url (url)
861 "Encode URL."
862 (browse-url-url-encode-chars url "[)$ ]"))
863
ebe79557
LMI
864(autoload 'shr-color-visible "shr-color")
865(autoload 'shr-color->hexadecimal "shr-color")
144b7b5c
G
866
867(defun shr-color-check (fg bg)
868 "Check that FG is visible on BG.
869Returns (fg bg) with corrected values.
870Returns nil if the colors that would be used are the default
871ones, in case fg and bg are nil."
872 (when (or fg bg)
873 (let ((fixed (cond ((null fg) 'fg)
874 ((null bg) 'bg))))
875 ;; Convert colors to hexadecimal, or set them to default.
876 (let ((fg (or (shr-color->hexadecimal fg)
877 (frame-parameter nil 'foreground-color)))
878 (bg (or (shr-color->hexadecimal bg)
879 (frame-parameter nil 'background-color))))
880 (cond ((eq fixed 'bg)
881 ;; Only return the new fg
882 (list nil (cadr (shr-color-visible bg fg t))))
883 ((eq fixed 'fg)
884 ;; Invert args and results and return only the new bg
885 (list (cadr (shr-color-visible fg bg t)) nil))
886 (t
887 (shr-color-visible bg fg)))))))
888
04db63bc 889(defun shr-colorize-region (start end fg &optional bg)
be2aa135
LMI
890 (when (and (not shr-inhibit-decoration)
891 (or fg bg))
04db63bc 892 (let ((new-colors (shr-color-check fg bg)))
144b7b5c 893 (when new-colors
60568d74 894 (when fg
be2aa135
LMI
895 (add-face-text-property start end
896 (list :foreground (cadr new-colors))
897 t))
04db63bc 898 (when bg
be2aa135
LMI
899 (add-face-text-property start end
900 (list :background (car new-colors))
901 t)))
ec72bf63 902 new-colors)))
04db63bc 903
c5ecc769
G
904(defun shr-expand-newlines (start end color)
905 (save-restriction
d709b79a
LI
906 ;; Skip past all white space at the start and ends.
907 (goto-char start)
908 (skip-chars-forward " \t\n")
909 (beginning-of-line)
910 (setq start (point))
911 (goto-char end)
912 (skip-chars-backward " \t\n")
913 (forward-line 1)
914 (setq end (point))
c5ecc769 915 (narrow-to-region start end)
160ae063 916 (let ((width (shr-buffer-width))
c5ecc769
G
917 column)
918 (goto-char (point-min))
919 (while (not (eobp))
920 (end-of-line)
19e0dbe0
KY
921 (when (and (< (setq column (current-column)) width)
922 (< (setq column (shr-previous-newline-padding-width column))
d709b79a 923 width))
7304e4dd 924 (let ((overlay (make-overlay (point) (1+ (point)))))
c5ecc769 925 (overlay-put overlay 'before-string
d709b79a
LI
926 (concat
927 (mapconcat
928 (lambda (overlay)
14596870
KY
929 (let ((string (plist-get
930 (overlay-properties overlay)
931 'before-string)))
d709b79a
LI
932 (if (not string)
933 ""
934 (overlay-put overlay 'before-string "")
935 string)))
936 (overlays-at (point))
937 "")
19e0dbe0 938 (propertize (make-string (- width column) ? )
d709b79a 939 'face (list :background color))))))
c5ecc769 940 (forward-line 1)))))
04db63bc 941
d709b79a
LI
942(defun shr-previous-newline-padding-width (width)
943 (let ((overlays (overlays-at (point)))
944 (previous-width 0))
945 (if (null overlays)
946 width
947 (dolist (overlay overlays)
948 (setq previous-width
949 (+ previous-width
14596870
KY
950 (length (plist-get (overlay-properties overlay)
951 'before-string)))))
d709b79a
LI
952 (+ width previous-width))))
953
66627fa9
G
954;;; Tag-specific rendering rules.
955
144b7b5c 956(defun shr-tag-body (cont)
04db63bc 957 (let* ((start (point))
af4e5f4c
G
958 (fgcolor (cdr (or (assq :fgcolor cont)
959 (assq :text cont))))
04db63bc 960 (bgcolor (cdr (assq :bgcolor cont)))
b31b26b4
G
961 (shr-stylesheet (list (cons 'color fgcolor)
962 (cons 'background-color bgcolor))))
144b7b5c 963 (shr-generic cont)
04db63bc 964 (shr-colorize-region start (point) fgcolor bgcolor)))
144b7b5c 965
e9145444 966(defun shr-tag-style (_cont)
b31b26b4
G
967 )
968
e9145444 969(defun shr-tag-script (_cont)
f73341e2
LMI
970 )
971
e9145444 972(defun shr-tag-comment (_cont)
fb1b0ef6
LMI
973 )
974
c74cb344
G
975(defun shr-dom-to-xml (dom)
976 "Convert DOM into a string containing the xml representation."
977 (let ((arg " ")
978 (text ""))
979 (dolist (sub (cdr dom))
980 (cond
981 ((listp (cdr sub))
982 (setq text (concat text (shr-dom-to-xml sub))))
983 ((eq (car sub) 'text)
984 (setq text (concat text (cdr sub))))
985 (t
986 (setq arg (concat arg (format "%s=\"%s\" "
987 (substring (symbol-name (car sub)) 1)
988 (cdr sub)))))))
989 (format "<%s%s>%s</%s>"
990 (car dom)
991 (substring arg 0 (1- (length arg)))
992 text
993 (car dom))))
994
65e704b9 995(defun shr-tag-svg (cont)
c74cb344
G
996 (when (image-type-available-p 'svg)
997 (funcall shr-put-image-function
998 (shr-dom-to-xml (cons 'svg cont))
999 "SVG Image")))
65e704b9 1000
a3af2929
LMI
1001(defun shr-tag-sup (cont)
1002 (let ((start (point)))
1003 (shr-generic cont)
1004 (put-text-property start (point) 'display '(raise 0.5))))
1005
1006(defun shr-tag-sub (cont)
1007 (let ((start (point)))
1008 (shr-generic cont)
1009 (put-text-property start (point) 'display '(raise -0.5))))
1010
7bafe9bc
LMI
1011(defun shr-tag-label (cont)
1012 (shr-generic cont)
1013 (shr-ensure-paragraph))
1014
66627fa9
G
1015(defun shr-tag-p (cont)
1016 (shr-ensure-paragraph)
f7aa248a 1017 (shr-indent)
66627fa9
G
1018 (shr-generic cont)
1019 (shr-ensure-paragraph))
1020
036d93bc
KY
1021(defun shr-tag-div (cont)
1022 (shr-ensure-newline)
1023 (shr-indent)
1024 (shr-generic cont)
1025 (shr-ensure-newline))
1026
6eee2678
LMI
1027(defun shr-tag-s (cont)
1028 (shr-fontize-cont cont 'shr-strike-through))
1029
55385ebc
JD
1030(defun shr-tag-del (cont)
1031 (shr-fontize-cont cont 'shr-strike-through))
1032
66627fa9
G
1033(defun shr-tag-b (cont)
1034 (shr-fontize-cont cont 'bold))
1035
1036(defun shr-tag-i (cont)
1037 (shr-fontize-cont cont 'italic))
1038
1039(defun shr-tag-em (cont)
087d8265 1040 (shr-fontize-cont cont 'italic))
66627fa9 1041
530f7b67
LMI
1042(defun shr-tag-strong (cont)
1043 (shr-fontize-cont cont 'bold))
1044
66627fa9
G
1045(defun shr-tag-u (cont)
1046 (shr-fontize-cont cont 'underline))
1047
2e76c12c
LMI
1048(defun shr-parse-style (style)
1049 (when style
a2994808
JD
1050 (save-match-data
1051 (when (string-match "\n" style)
1052 (setq style (replace-match " " t t style))))
2e76c12c
LMI
1053 (let ((plist nil))
1054 (dolist (elem (split-string style ";"))
1055 (when elem
1056 (setq elem (split-string elem ":"))
1057 (when (and (car elem)
1058 (cadr elem))
1059 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1060 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
144b7b5c
G
1061 (when (string-match " *!important\\'" value)
1062 (setq value (substring value 0 (match-beginning 0))))
2e76c12c
LMI
1063 (push (cons (intern name obarray)
1064 value)
1065 plist)))))
1066 plist)))
1067
dbd5ffad 1068(defun shr-tag-base (cont)
be2aa135
LMI
1069 (let ((base (cdr (assq :href cont))))
1070 (when base
1071 (setq shr-base (shr-parse-base base))))
266c63b5 1072 (shr-generic cont))
dbd5ffad 1073
66627fa9
G
1074(defun shr-tag-a (cont)
1075 (let ((url (cdr (assq :href cont)))
04db63bc 1076 (title (cdr (assq :title cont)))
66627fa9
G
1077 (start (point))
1078 shr-start)
1079 (shr-generic cont)
1c4b1e61 1080 (when (and shr-target-id
e9145444 1081 (equal (cdr (assq :name cont)) shr-target-id))
1c4b1e61
LMI
1082 ;; We have a zero-length <a name="foo"> element, so just
1083 ;; insert... something.
1084 (when (= start (point))
1085 (shr-ensure-newline)
1086 (insert " "))
1087 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
be2aa135
LMI
1088 (when (and url
1089 (not shr-inhibit-decoration))
cc21c235 1090 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
de635afe
G
1091
1092(defun shr-tag-object (cont)
99e65b2d
G
1093 (let ((start (point))
1094 url)
1095 (dolist (elem cont)
1096 (when (eq (car elem) 'embed)
1097 (setq url (or url (cdr (assq :src (cdr elem))))))
1098 (when (and (eq (car elem) 'param)
1099 (equal (cdr (assq :name (cdr elem))) "movie"))
1100 (setq url (or url (cdr (assq :value (cdr elem)))))))
de635afe
G
1101 (when url
1102 (shr-insert " [multimedia] ")
dbd5ffad 1103 (shr-urlify start (shr-expand-url url)))
99e65b2d
G
1104 (shr-generic cont)))
1105
ad9a773c
RS
1106(defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
1107 ("ogv" . 1.0)
1108 ("ogg" . 1.0)
1109 ("opus" . 1.0)
1110 ("flac" . 0.9)
1111 ("wav" . 0.5))
1112 "Preferences for media types.
1113The key element should be a regexp matched against the type of the source or
1114url if no type is specified. The value should be a float in the range 0.0 to
11151.0. Media elements with higher value are preferred."
1116 :version "24.4"
1117 :group 'shr
1118 :type '(alist :key-type regexp :value-type float))
1119
1120(defun shr--get-media-pref (elem)
1121 "Determine the preference for ELEM.
1122The preference is a float determined from `shr-prefer-media-type'."
1123 (let ((type (cdr (assq :type elem)))
1124 (p 0.0))
1125 (unless type
1126 (setq type (cdr (assq :src elem))))
1127 (when type
1128 (dolist (pref shr-prefer-media-type-alist)
1129 (when (and
1130 (> (cdr pref) p)
1131 (string-match-p (car pref) type))
1132 (setq p (cdr pref)))))
1133 p))
1134
1135(defun shr--extract-best-source (cont &optional url pref)
1136 "Extract the best `:src' property from <source> blocks in CONT."
1137 (setq pref (or pref -1.0))
1138 (let (new-pref)
1139 (dolist (elem cont)
073d5cfd
LMI
1140 (when (and (eq (car elem) 'source)
1141 (< pref
1142 (setq new-pref
1143 (shr--get-media-pref elem))))
1144 (setq pref new-pref
1145 url (cdr (assq :src elem)))
f224e500 1146 ;; libxml's html parser isn't HTML5 compliant and non terminated
ad9a773c
RS
1147 ;; source tags might end up as children. So recursion it is...
1148 (dolist (child (cdr elem))
073d5cfd 1149 (when (eq (car child) 'source)
ad9a773c
RS
1150 (let ((ret (shr--extract-best-source (list child) url pref)))
1151 (when (< pref (cdr ret))
1152 (setq url (car ret)
1153 pref (cdr ret)))))))))
1154 (cons url pref))
1155
99e65b2d
G
1156(defun shr-tag-video (cont)
1157 (let ((image (cdr (assq :poster cont)))
ad9a773c
RS
1158 (url (cdr (assq :src cont)))
1159 (start (point)))
1160 (unless url
1161 (setq url (car (shr--extract-best-source cont))))
177948a5
RS
1162 (if image
1163 (shr-tag-img nil image)
1164 (shr-insert " [video] "))
dbd5ffad 1165 (shr-urlify start (shr-expand-url url))))
ab67634f 1166
eb2dd24d
RS
1167(defun shr-tag-audio (cont)
1168 (let ((url (cdr (assq :src cont)))
1169 (start (point)))
ad9a773c
RS
1170 (unless url
1171 (setq url (car (shr--extract-best-source cont))))
eb2dd24d
RS
1172 (shr-insert " [audio] ")
1173 (shr-urlify start (shr-expand-url url))))
1174
99e65b2d
G
1175(defun shr-tag-img (cont &optional url)
1176 (when (or url
1177 (and cont
e330b646 1178 (> (length (cdr (assq :src cont))) 0)))
68f6bd17
KY
1179 (when (and (> (current-column) 0)
1180 (not (eq shr-state 'image)))
1181 (insert "\n"))
1182 (let ((alt (cdr (assq :alt cont)))
dbd5ffad 1183 (url (shr-expand-url (or url (cdr (assq :src cont))))))
68f6bd17
KY
1184 (let ((start (point-marker)))
1185 (when (zerop (length alt))
953d41c4 1186 (setq alt "*"))
68f6bd17 1187 (cond
99e65b2d
G
1188 ((or (member (cdr (assq :height cont)) '("0" "1"))
1189 (member (cdr (assq :width cont)) '("0" "1")))
1190 ;; Ignore zero-sized or single-pixel images.
1191 )
2250b351
DE
1192 ((and (not shr-inhibit-images)
1193 (string-match "\\`data:" url))
1194 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1195 (if image
1196 (funcall shr-put-image-function image alt)
1197 (insert alt))))
68f6bd17
KY
1198 ((and (not shr-inhibit-images)
1199 (string-match "\\`cid:" url))
1200 (let ((url (substring url (match-end 0)))
1201 image)
1202 (if (or (not shr-content-function)
1203 (not (setq image (funcall shr-content-function url))))
1204 (insert alt)
b9bdaf74 1205 (funcall shr-put-image-function image alt))))
68f6bd17
KY
1206 ((or shr-inhibit-images
1207 (and shr-blocked-images
1208 (string-match shr-blocked-images url)))
1209 (setq shr-start (point))
1210 (let ((shr-state 'space))
b354bc53
KY
1211 (if (> (string-width alt) 8)
1212 (shr-insert (truncate-string-to-width alt 8))
68f6bd17 1213 (shr-insert alt))))
728518c3
LMI
1214 ((and (not shr-ignore-cache)
1215 (url-is-cached (shr-encode-url url)))
b9bdaf74 1216 (funcall shr-put-image-function (shr-get-image-data url) alt))
68f6bd17 1217 (t
64522086 1218 (insert alt " ")
728518c3
LMI
1219 (when (and shr-ignore-cache
1220 (url-is-cached (shr-encode-url url)))
1221 (let ((file (url-cache-create-filename (shr-encode-url url))))
1222 (when (file-exists-p file)
1223 (delete-file file))))
038b3495 1224 (url-queue-retrieve
f3b146e9 1225 (shr-encode-url url) 'shr-image-fetched
64522086 1226 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
038b3495 1227 t t)))
a959fc40
KY
1228 (when (zerop shr-table-depth) ;; We are not in a table.
1229 (put-text-property start (point) 'keymap shr-map)
1230 (put-text-property start (point) 'shr-alt alt)
1231 (put-text-property start (point) 'image-url url)
1232 (put-text-property start (point) 'image-displayer
1233 (shr-image-displayer shr-content-function))
1234 (put-text-property start (point) 'help-echo alt))
68f6bd17 1235 (setq shr-state 'image)))))
66627fa9
G
1236
1237(defun shr-tag-pre (cont)
1238 (let ((shr-folding-mode 'none))
1239 (shr-ensure-newline)
f7aa248a 1240 (shr-indent)
66627fa9
G
1241 (shr-generic cont)
1242 (shr-ensure-newline)))
1243
1244(defun shr-tag-blockquote (cont)
1245 (shr-ensure-paragraph)
f7aa248a 1246 (shr-indent)
66627fa9
G
1247 (let ((shr-indentation (+ shr-indentation 4)))
1248 (shr-generic cont))
1249 (shr-ensure-paragraph))
a41c2e6d 1250
d2aa9780
LMI
1251(defun shr-tag-dl (cont)
1252 (shr-ensure-paragraph)
1253 (shr-generic cont)
1254 (shr-ensure-paragraph))
1255
1256(defun shr-tag-dt (cont)
1257 (shr-ensure-newline)
1258 (shr-generic cont)
1259 (shr-ensure-newline))
1260
1261(defun shr-tag-dd (cont)
1262 (shr-ensure-newline)
1263 (let ((shr-indentation (+ shr-indentation 4)))
1264 (shr-generic cont)))
1265
a41c2e6d
G
1266(defun shr-tag-ul (cont)
1267 (shr-ensure-paragraph)
1268 (let ((shr-list-mode 'ul))
3d319c8f
LMI
1269 (shr-generic cont))
1270 (shr-ensure-paragraph))
a41c2e6d
G
1271
1272(defun shr-tag-ol (cont)
3d319c8f 1273 (shr-ensure-paragraph)
a41c2e6d 1274 (let ((shr-list-mode 1))
3d319c8f
LMI
1275 (shr-generic cont))
1276 (shr-ensure-paragraph))
a41c2e6d
G
1277
1278(defun shr-tag-li (cont)
c74cb344 1279 (shr-ensure-newline)
f7aa248a 1280 (shr-indent)
8028ed5c
LMI
1281 (let* ((bullet
1282 (if (numberp shr-list-mode)
1283 (prog1
1284 (format "%d " shr-list-mode)
1285 (setq shr-list-mode (1+ shr-list-mode)))
c74cb344 1286 shr-bullet))
8028ed5c
LMI
1287 (shr-indentation (+ shr-indentation (length bullet))))
1288 (insert bullet)
1289 (shr-generic cont)))
a41c2e6d
G
1290
1291(defun shr-tag-br (cont)
89b163db
G
1292 (when (and (not (bobp))
1293 ;; Only add a newline if we break the current line, or
1294 ;; the previous line isn't a blank line.
1295 (or (not (bolp))
1296 (and (> (- (point) 2) (point-min))
1297 (not (= (char-after (- (point) 2)) ?\n)))))
f7aa248a
G
1298 (insert "\n")
1299 (shr-indent))
a41c2e6d
G
1300 (shr-generic cont))
1301
308c9d24 1302(defun shr-tag-span (cont)
be2aa135 1303 (shr-generic cont))
308c9d24 1304
a41c2e6d 1305(defun shr-tag-h1 (cont)
e4dbdb09 1306 (shr-heading cont 'bold 'underline))
a41c2e6d
G
1307
1308(defun shr-tag-h2 (cont)
e4dbdb09 1309 (shr-heading cont 'bold))
a41c2e6d
G
1310
1311(defun shr-tag-h3 (cont)
e4dbdb09 1312 (shr-heading cont 'italic))
a41c2e6d
G
1313
1314(defun shr-tag-h4 (cont)
e4dbdb09 1315 (shr-heading cont))
a41c2e6d
G
1316
1317(defun shr-tag-h5 (cont)
e4dbdb09 1318 (shr-heading cont))
a41c2e6d
G
1319
1320(defun shr-tag-h6 (cont)
e4dbdb09 1321 (shr-heading cont))
a41c2e6d 1322
e9145444 1323(defun shr-tag-hr (_cont)
3d319c8f 1324 (shr-ensure-newline)
6b7df8d3 1325 (insert (make-string shr-width shr-hr-line) "\n"))
3d319c8f 1326
144b7b5c
G
1327(defun shr-tag-title (cont)
1328 (shr-heading cont 'bold 'underline))
1329
1110d53b 1330(defun shr-tag-font (cont)
b31b26b4
G
1331 (let* ((start (point))
1332 (color (cdr (assq :color cont)))
1333 (shr-stylesheet (nconc (list (cons 'color color))
1334 shr-stylesheet)))
1110d53b 1335 (shr-generic cont)
b31b26b4
G
1336 (when color
1337 (shr-colorize-region start (point) color
1338 (cdr (assq 'background-color shr-stylesheet))))))
1110d53b 1339
66627fa9 1340;;; Table rendering algorithm.
a41c2e6d 1341
a0ec382a
LMI
1342;; Table rendering is the only complicated thing here. We do this by
1343;; first counting how many TDs there are in each TR, and registering
1344;; how wide they think they should be ("width=45%", etc). Then we
1345;; render each TD separately (this is done in temporary buffers, so
1346;; that we can use all the rendering machinery as if we were in the
1347;; main buffer). Now we know how much space each TD really takes, so
1348;; we then render everything again with the new widths, and finally
1349;; insert all these boxes into the main buffer.
6c769311 1350(defun shr-tag-table-1 (cont)
71e691a5
G
1351 (setq cont (or (cdr (assq 'tbody cont))
1352 cont))
130e977f 1353 (let* ((shr-inhibit-images t)
99e65b2d 1354 (shr-table-depth (1+ shr-table-depth))
83ffd571 1355 (shr-kinsoku-shorten t)
a0ec382a 1356 ;; Find all suggested widths.
130e977f 1357 (columns (shr-column-specs cont))
a0ec382a 1358 ;; Compute how many characters wide each TD should be.
71e691a5 1359 (suggested-widths (shr-pro-rate-columns columns))
a0ec382a
LMI
1360 ;; Do a "test rendering" to see how big each TD is (this can
1361 ;; be smaller (if there's little text) or bigger (if there's
1362 ;; unbreakable text).
71e691a5 1363 (sketch (shr-make-table cont suggested-widths))
160ae063
LMI
1364 ;; Compute the "natural" width by setting each column to 500
1365 ;; characters and see how wide they really render.
1366 (natural (shr-make-table cont (make-vector (length columns) 500)))
1367 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
030158f3 1368 ;; This probably won't work very well.
83ffd571
KY
1369 (when (> (+ (loop for width across sketch-widths
1370 summing (1+ width))
1371 shr-indentation 1)
030158f3
G
1372 (frame-width))
1373 (setq truncate-lines t))
a0ec382a 1374 ;; Then render the table again with these new "hard" widths.
f462d10a 1375 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
130e977f 1376
6c769311
KY
1377(defun shr-tag-table (cont)
1378 (shr-ensure-paragraph)
1379 (let* ((caption (cdr (assq 'caption cont)))
1380 (header (cdr (assq 'thead cont)))
1381 (body (or (cdr (assq 'tbody cont)) cont))
1382 (footer (cdr (assq 'tfoot cont)))
144b7b5c 1383 (bgcolor (cdr (assq :bgcolor cont)))
60568d74
LMI
1384 (start (point))
1385 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1386 shr-stylesheet))
6c769311
KY
1387 (nheader (if header (shr-max-columns header)))
1388 (nbody (if body (shr-max-columns body)))
1389 (nfooter (if footer (shr-max-columns footer))))
2146e256
LMI
1390 (if (and (not caption)
1391 (not header)
1392 (not (cdr (assq 'tbody cont)))
1393 (not (cdr (assq 'tr cont)))
1394 (not footer))
1395 ;; The table is totally invalid and just contains random junk.
1396 ;; Try to output it anyway.
1397 (shr-generic cont)
1398 ;; It's a real table, so render it.
1399 (shr-tag-table-1
1400 (nconc
1401 (if caption `((tr (td ,@caption))))
1402 (if header
1403 (if footer
8feacce0 1404 ;; header + body + footer
2146e256
LMI
1405 (if (= nheader nbody)
1406 (if (= nbody nfooter)
1407 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
1408 (nconc `((tr (td (table (tbody ,@header ,@body)))))
1409 (if (= nfooter 1)
1410 footer
1411 `((tr (td (table (tbody ,@footer))))))))
1412 (nconc `((tr (td (table (tbody ,@header)))))
1413 (if (= nbody nfooter)
1414 `((tr (td (table (tbody ,@body ,@footer)))))
1415 (nconc `((tr (td (table (tbody ,@body)))))
1416 (if (= nfooter 1)
1417 footer
1418 `((tr (td (table (tbody ,@footer))))))))))
1419 ;; header + body
3c066373 1420 (if (= nheader nbody)
2146e256
LMI
1421 `((tr (td (table (tbody ,@header ,@body)))))
1422 (if (= nheader 1)
1423 `(,@header (tr (td (table (tbody ,@body)))))
1424 `((tr (td (table (tbody ,@header))))
1425 (tr (td (table (tbody ,@body))))))))
1426 (if footer
1427 ;; body + footer
1428 (if (= nbody nfooter)
1429 `((tr (td (table (tbody ,@body ,@footer)))))
1430 (nconc `((tr (td (table (tbody ,@body)))))
1431 (if (= nfooter 1)
1432 footer
1433 `((tr (td (table (tbody ,@footer))))))))
1434 (if caption
1435 `((tr (td (table (tbody ,@body)))))
1436 body))))))
60568d74
LMI
1437 (when bgcolor
1438 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
f462d10a
LMI
1439 bgcolor))
1440 ;; Finally, insert all the images after the table. The Emacs buffer
1441 ;; model isn't strong enough to allow us to put the images actually
1442 ;; into the tables.
1443 (when (zerop shr-table-depth)
1444 (dolist (elem (shr-find-elements cont 'img))
1445 (shr-tag-img (cdr elem))))))
6c769311 1446
130e977f
LMI
1447(defun shr-find-elements (cont type)
1448 (let (result)
1449 (dolist (elem cont)
1450 (cond ((eq (car elem) type)
1451 (push elem result))
1452 ((consp (cdr elem))
1453 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1454 (nreverse result)))
71e691a5
G
1455
1456(defun shr-insert-table (table widths)
be2aa135
LMI
1457 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1458 "collapse"))
924d6997 1459 (shr-table-separator-length (if collapse 0 1))
be2aa135
LMI
1460 (shr-table-vertical-line (if collapse "" shr-table-vertical-line)))
1461 (unless collapse
1462 (shr-insert-table-ruler widths))
1463 (dolist (row table)
1464 (let ((start (point))
1465 (height (let ((max 0))
1466 (dolist (column row)
1467 (setq max (max max (cadr column))))
1468 max)))
1469 (dotimes (i height)
1470 (shr-indent)
1471 (insert shr-table-vertical-line "\n"))
1472 (dolist (column row)
1473 (goto-char start)
1474 (let ((lines (nth 2 column)))
1475 (dolist (line lines)
1476 (end-of-line)
1477 (insert line shr-table-vertical-line)
1478 (forward-line 1))
1479 ;; Add blank lines at padding at the bottom of the TD,
1480 ;; possibly.
1481 (dotimes (i (- height (length lines)))
1482 (end-of-line)
1483 (let ((start (point)))
1484 (insert (make-string (string-width (car lines)) ? )
1485 shr-table-vertical-line)
1486 (when (nth 4 column)
1487 (shr-add-font start (1- (point))
1488 (list :background (nth 4 column)))))
1489 (forward-line 1)))))
1490 (unless collapse
1491 (shr-insert-table-ruler widths)))))
71e691a5
G
1492
1493(defun shr-insert-table-ruler (widths)
924d6997
G
1494 (when shr-table-horizontal-line
1495 (when (and (bolp)
1496 (> shr-indentation 0))
1497 (shr-indent))
1498 (insert shr-table-corner)
1499 (dotimes (i (length widths))
1500 (insert (make-string (aref widths i) shr-table-horizontal-line)
1501 shr-table-corner))
1502 (insert "\n")))
71e691a5 1503
160ae063 1504(defun shr-table-widths (table natural-table suggested-widths)
a7dcc87b
G
1505 (let* ((length (length suggested-widths))
1506 (widths (make-vector length 0))
1507 (natural-widths (make-vector length 0)))
71e691a5
G
1508 (dolist (row table)
1509 (let ((i 0))
1510 (dolist (column row)
160ae063
LMI
1511 (aset widths i (max (aref widths i) column))
1512 (setq i (1+ i)))))
1513 (dolist (row natural-table)
1514 (let ((i 0))
1515 (dolist (column row)
1516 (aset natural-widths i (max (aref natural-widths i) column))
a7dcc87b 1517 (setq i (1+ i)))))
863b61d6
KY
1518 (let ((extra (- (apply '+ (append suggested-widths nil))
1519 (apply '+ (append widths nil))))
a7dcc87b 1520 (expanded-columns 0))
160ae063
LMI
1521 ;; We have extra, unused space, so divide this space amongst the
1522 ;; columns.
a7dcc87b 1523 (when (> extra 0)
160ae063
LMI
1524 ;; If the natural width is wider than the rendered width, we
1525 ;; want to allow the column to expand.
a7dcc87b 1526 (dotimes (i length)
a7dcc87b
G
1527 (when (> (aref natural-widths i) (aref widths i))
1528 (setq expanded-columns (1+ expanded-columns))))
1529 (dotimes (i length)
1530 (when (> (aref natural-widths i) (aref widths i))
1531 (aset widths i (min
160ae063 1532 (aref natural-widths i)
a7dcc87b
G
1533 (+ (/ extra expanded-columns)
1534 (aref widths i))))))))
71e691a5
G
1535 widths))
1536
1537(defun shr-make-table (cont widths &optional fill)
c74cb344
G
1538 (or (cadr (assoc (list cont widths fill) shr-content-cache))
1539 (let ((data (shr-make-table-1 cont widths fill)))
1540 (push (list (list cont widths fill) data)
1541 shr-content-cache)
1542 data)))
1543
1544(defun shr-make-table-1 (cont widths &optional fill)
be2aa135 1545 (let ((trs nil)
924d6997
G
1546 (shr-inhibit-decoration (not fill))
1547 (rowspans (make-vector (length widths) 0))
1548 width colspan)
71e691a5
G
1549 (dolist (row cont)
1550 (when (eq (car row) 'tr)
a0ec382a
LMI
1551 (let ((tds nil)
1552 (columns (cdr row))
1553 (i 0)
924d6997 1554 (width-column 0)
a0ec382a
LMI
1555 column)
1556 (while (< i (length widths))
924d6997
G
1557 ;; If we previously had a rowspan definition, then that
1558 ;; means that we now have a "missing" td/th element here.
1559 ;; So just insert a dummy, empty one to (sort of) emulate
1560 ;; rowspan.
1561 (setq column
1562 (if (zerop (aref rowspans i))
1563 (pop columns)
1564 (aset rowspans i (1- (aref rowspans i)))
1565 '(td)))
a0ec382a 1566 (when (or (memq (car column) '(td th))
924d6997
G
1567 (not column))
1568 (when (cdr (assq :rowspan (cdr column)))
1569 (aset rowspans i (+ (aref rowspans i)
1570 (1- (string-to-number
1571 (cdr (assq :rowspan (cdr column))))))))
970ad972
G
1572 ;; Sanity check for invalid column-spans.
1573 (when (>= width-column (length widths))
1574 (setq width-column 0))
924d6997
G
1575 (setq width
1576 (if column
1577 (aref widths width-column)
2fae38e5 1578 10))
924d6997
G
1579 (when (and fill
1580 (setq colspan (cdr (assq :colspan (cdr column)))))
2122cb6d
LMI
1581 (setq colspan (min (string-to-number colspan)
1582 ;; The colspan may be wrong, so
1583 ;; truncate it to the length of the
1584 ;; remaining columns.
1585 (- (length widths) i)))
924d6997
G
1586 (dotimes (j (1- colspan))
1587 (if (> (+ i 1 j) (1- (length widths)))
1588 (setq width (aref widths (1- (length widths))))
1589 (setq width (+ width
1590 shr-table-separator-length
1591 (aref widths (+ i 1 j))))))
1592 (setq width-column (+ width-column (1- colspan))))
1593 (when (or column
1594 (not fill))
1595 (push (shr-render-td (cdr column) width fill)
1596 tds))
1597 (setq i (1+ i)
1598 width-column (1+ width-column))))
71e691a5
G
1599 (push (nreverse tds) trs))))
1600 (nreverse trs)))
1601
1602(defun shr-render-td (cont width fill)
04db63bc 1603 (with-temp-buffer
60568d74
LMI
1604 (let ((bgcolor (cdr (assq :bgcolor cont)))
1605 (fgcolor (cdr (assq :fgcolor cont)))
1606 (style (cdr (assq :style cont)))
1607 (shr-stylesheet shr-stylesheet)
7304e4dd 1608 actual-colors)
60568d74
LMI
1609 (when style
1610 (setq style (and (string-match "color" style)
1611 (shr-parse-style style))))
1612 (when bgcolor
1613 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1614 (when fgcolor
1615 (setq style (nconc (list (cons 'color fgcolor)) style)))
1616 (when style
1617 (setq shr-stylesheet (append style shr-stylesheet)))
c74cb344
G
1618 (let ((shr-width width)
1619 (shr-indentation 0))
1620 (shr-descend (cons 'td cont)))
1621 ;; Delete padding at the bottom of the TDs.
1622 (delete-region
1623 (point)
1624 (progn
1625 (skip-chars-backward " \t\n")
1626 (end-of-line)
1627 (point)))
60568d74
LMI
1628 (goto-char (point-min))
1629 (let ((max 0))
1630 (while (not (eobp))
1631 (end-of-line)
1632 (setq max (max max (current-column)))
1633 (forward-line 1))
1634 (when fill
1635 (goto-char (point-min))
1636 ;; If the buffer is totally empty, then put a single blank
1637 ;; line here.
1638 (if (zerop (buffer-size))
1639 (insert (make-string width ? ))
1640 ;; Otherwise, fill the buffer.
544d4594
LMI
1641 (let ((align (cdr (assq :align cont)))
1642 length)
1643 (while (not (eobp))
1644 (end-of-line)
1645 (setq length (- width (current-column)))
1646 (when (> length 0)
1647 (cond
1648 ((equal align "right")
1649 (beginning-of-line)
1650 (insert (make-string length ? )))
1651 ((equal align "center")
1652 (insert (make-string (/ length 2) ? ))
1653 (beginning-of-line)
1654 (insert (make-string (- length (/ length 2)) ? )))
1655 (t
1656 (insert (make-string length ? )))))
1657 (forward-line 1))))
abb97fbb 1658 (when style
ec72bf63
G
1659 (setq actual-colors
1660 (shr-colorize-region
1661 (point-min) (point-max)
1662 (cdr (assq 'color shr-stylesheet))
1663 (cdr (assq 'background-color shr-stylesheet))))))
60568d74
LMI
1664 (if fill
1665 (list max
1666 (count-lines (point-min) (point-max))
1667 (split-string (buffer-string) "\n")
7304e4dd 1668 nil
ec72bf63 1669 (car actual-colors))
2122cb6d 1670 max)))))
a7dcc87b 1671
160ae063 1672(defun shr-buffer-width ()
a7dcc87b 1673 (goto-char (point-min))
160ae063 1674 (let ((max 0))
a7dcc87b
G
1675 (while (not (eobp))
1676 (end-of-line)
160ae063 1677 (setq max (max max (current-column)))
a7dcc87b
G
1678 (forward-line 1))
1679 max))
130e977f 1680
71e691a5
G
1681(defun shr-pro-rate-columns (columns)
1682 (let ((total-percentage 0)
1683 (widths (make-vector (length columns) 0)))
1684 (dotimes (i (length columns))
a7dcc87b 1685 (setq total-percentage (+ total-percentage (aref columns i))))
71e691a5
G
1686 (setq total-percentage (/ 1.0 total-percentage))
1687 (dotimes (i (length columns))
1688 (aset widths i (max (truncate (* (aref columns i)
1689 total-percentage
a7dcc87b 1690 (- shr-width (1+ (length columns)))))
71e691a5
G
1691 10)))
1692 widths))
1693
1694;; Return a summary of the number and shape of the TDs in the table.
1695(defun shr-column-specs (cont)
1696 (let ((columns (make-vector (shr-max-columns cont) 1)))
1697 (dolist (row cont)
1698 (when (eq (car row) 'tr)
1699 (let ((i 0))
1700 (dolist (column (cdr row))
1701 (when (memq (car column) '(td th))
1702 (let ((width (cdr (assq :width (cdr column)))))
1703 (when (and width
5d852256
LMI
1704 (string-match "\\([0-9]+\\)%" width)
1705 (not (zerop (setq width (string-to-number
1706 (match-string 1 width))))))
1707 (aset columns i (/ width 100.0))))
130e977f 1708 (setq i (1+ i)))))))
71e691a5
G
1709 columns))
1710
1711(defun shr-count (cont elem)
1712 (let ((i 0))
1713 (dolist (sub cont)
1714 (when (eq (car sub) elem)
1715 (setq i (1+ i))))
1716 i))
1717
1718(defun shr-max-columns (cont)
1719 (let ((max 0))
1720 (dolist (row cont)
1721 (when (eq (car row) 'tr)
130e977f
LMI
1722 (setq max (max max (+ (shr-count (cdr row) 'td)
1723 (shr-count (cdr row) 'th))))))
71e691a5
G
1724 max))
1725
f3fd95db 1726(provide 'shr)
367f7f81 1727
b4543a28 1728;; Local Variables:
c38e0c97 1729;; coding: utf-8
b4543a28
G
1730;; End:
1731
367f7f81 1732;;; shr.el ends here