* net/shr.el (shr-mouse-browse-url): New command and keystroke.
[bpt/emacs.git] / lisp / net / shr.el
1 ;;; shr.el --- Simple HTML Renderer
2
3 ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
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
33 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'url)) ;For url-filename's setf handler.
35 (require 'browse-url)
36
37 (defgroup shr nil
38 "Simple HTML Renderer"
39 :version "24.1"
40 :group 'hypermedia)
41
42 (defcustom shr-max-image-proportion 0.9
43 "How big pictures displayed are in relation to the window they're in.
44 A value of 0.7 means that they are allowed to take up 70% of the
45 width and height of the window. If they are larger than this,
46 and Emacs supports it, then the images will be rescaled down to
47 fit 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
56 :type '(choice (const nil) regexp))
57
58 (defcustom shr-table-horizontal-line nil
59 "Character used to draw horizontal table lines.
60 If nil, don't draw horizontal table lines."
61 :group 'shr
62 :type 'character)
63
64 (defcustom shr-table-vertical-line ?\s
65 "Character used to draw vertical table lines."
66 :group 'shr
67 :type 'character)
68
69 (defcustom shr-table-corner ?\s
70 "Character used to draw table corners."
71 :group 'shr
72 :type 'character)
73
74 (defcustom shr-hr-line ?-
75 "Character used to draw hr lines."
76 :group 'shr
77 :type 'character)
78
79 (defcustom shr-width fill-column
80 "Frame width to use for rendering.
81 May either be an integer specifying a fixed width in characters,
82 or nil, meaning that the full width of the window should be
83 used."
84 :type '(choice (integer :tag "Fixed width in characters")
85 (const :tag "Use the width of the window" nil))
86 :group 'shr)
87
88 (defcustom shr-bullet "* "
89 "Bullet used for unordered lists.
90 Alternative suggestions are:
91 - \" \"
92 - \" \""
93 :type 'string
94 :group 'shr)
95
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
102 (defvar shr-content-function nil
103 "If bound, this should be a function that will return the content.
104 This is used for cid: URLs, and the function is called with the
105 cid: URL as the argument.")
106
107 (defvar shr-put-image-function 'shr-put-image
108 "Function called to put image and alt string.")
109
110 (defface shr-strike-through '((t (:strike-through t)))
111 "Font for <s> elements."
112 :group 'shr)
113
114 (defface shr-link
115 '((t (:inherit link)))
116 "Font for link elements."
117 :group 'shr)
118
119 ;;; Internal variables.
120
121 (defvar shr-folding-mode nil)
122 (defvar shr-state nil)
123 (defvar shr-start nil)
124 (defvar shr-indentation 0)
125 (defvar shr-inhibit-images nil)
126 (defvar shr-list-mode nil)
127 (defvar shr-content-cache nil)
128 (defvar shr-kinsoku-shorten nil)
129 (defvar shr-table-depth 0)
130 (defvar shr-stylesheet nil)
131 (defvar shr-base nil)
132 (defvar shr-ignore-cache nil)
133 (defvar shr-external-rendering-functions nil)
134 (defvar shr-target-id nil)
135 (defvar shr-inhibit-decoration nil)
136 (defvar shr-table-separator-length 1)
137
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)
142 (define-key map "z" 'shr-zoom-image)
143 (define-key map [tab] 'shr-next-link)
144 (define-key map [backtab] 'shr-previous-link)
145 (define-key map [follow-link] 'mouse-face)
146 (define-key map [mouse-2] 'shr-mouse-browse-url)
147 (define-key map "I" 'shr-insert-image)
148 (define-key map "w" 'shr-copy-url)
149 (define-key map "u" 'shr-copy-url)
150 (define-key map "v" 'shr-browse-url)
151 (define-key map "o" 'shr-save-contents)
152 (define-key map "\r" 'shr-browse-url)
153 map))
154
155 ;; Public functions and commands.
156 (declare-function libxml-parse-html-region "xml.c"
157 (start end &optional base-url))
158
159 (defun shr-render-buffer (buffer)
160 "Display the HTML rendering of the current buffer."
161 (interactive (list (current-buffer)))
162 (or (fboundp 'libxml-parse-html-region)
163 (error "This function requires Emacs to be compiled with libxml2"))
164 (pop-to-buffer "*html*")
165 (erase-buffer)
166 (shr-insert-document
167 (with-current-buffer buffer
168 (libxml-parse-html-region (point-min) (point-max))))
169 (goto-char (point-min)))
170
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
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
189 ;;;###autoload
190 (defun shr-insert-document (dom)
191 "Render the parsed document DOM into the current buffer.
192 DOM should be a parse tree as generated by
193 `libxml-parse-html-region' or similar."
194 (setq shr-content-cache nil)
195 (let ((start (point))
196 (shr-state nil)
197 (shr-start nil)
198 (shr-base nil)
199 (shr-preliminary-table-render 0)
200 (shr-width (or shr-width (1- (window-width)))))
201 (shr-descend (shr-transform-dom dom))
202 (shr-remove-trailing-whitespace start (point))))
203
204 (defun shr-remove-trailing-whitespace (start end)
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)
211 (when (> (shr-previous-newline-padding-width (current-column)) width)
212 (dolist (overlay (overlays-at (point)))
213 (when (overlay-get overlay 'before-string)
214 (overlay-put overlay 'before-string nil))))
215 (forward-line 1)))))
216
217 (defun shr-copy-url ()
218 "Copy the URL under point to the kill ring.
219 If called twice, then try to fetch the URL and see whether it
220 redirects 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))
240 (copy-region-as-kill (point-min) (point-max)))))
241 nil t))
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
249 (defun shr-next-link ()
250 "Skip to the next link."
251 (interactive)
252 (let ((skip (text-property-any (point) (point-max) 'help-echo nil)))
253 (if (not (setq skip (text-property-not-all skip (point-max)
254 'help-echo nil)))
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))
266 (get-text-property (point) 'help-echo))
267 (forward-char -1))
268 ;; Find the previous link.
269 (while (and (not (bobp))
270 (not (setq found (get-text-property (point) 'help-echo))))
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))
278 (get-text-property (point) 'help-echo))
279 (forward-char -1))
280 (forward-char 1)
281 (message "%s" (get-text-property (point) 'help-echo)))))
282
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
291 (defun shr-browse-image (&optional copy-url)
292 "Browse the image under point.
293 If COPY-URL (the prefix if called interactively) is non-nil, copy
294 the URL of the image to the kill buffer instead."
295 (interactive "P")
296 (let ((url (get-text-property (point) 'image-url)))
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
306 (message "Browsing %s..." url)
307 (browse-url url)))))
308
309 (defun shr-insert-image ()
310 "Insert the image under point into the buffer."
311 (interactive)
312 (let ((url (get-text-property (point) 'image-url)))
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))
318 t t))))
319
320 (defun shr-zoom-image ()
321 "Toggle the image size.
322 The size will be rotated between the default size, the original
323 size, 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
354 ;;; Utility functions.
355
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)
364 (push (cons 'text sub) result)
365 (push (shr-transform-dom sub) result)))
366 (nreverse result)))
367
368 (defun shr-descend (dom)
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)))
375 (style (cdr (assq :style (cdr dom))))
376 (shr-stylesheet shr-stylesheet)
377 (start (point)))
378 (when style
379 (if (string-match "color\\|display\\|border-collapse" style)
380 (setq shr-stylesheet (nconc (shr-parse-style style)
381 shr-stylesheet))
382 (setq style nil)))
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)))))))
397
398 (defun shr-generic (cont)
399 (dolist (sub cont)
400 (cond
401 ((eq (car sub) 'text)
402 (shr-insert (cdr sub)))
403 ((listp (cdr sub))
404 (shr-descend sub)))))
405
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
426 (defun shr-insert (text)
427 (when (and (eq shr-state 'image)
428 (not (bolp))
429 (not (string-match "\\`[ \t\n]+\\'" text)))
430 (insert "\n")
431 (setq shr-state nil))
432 (cond
433 ((eq shr-folding-mode 'none)
434 (insert text))
435 (t
436 (when (and (string-match "\\`[ \t\n ]" text)
437 (not (bolp))
438 (not (eq (char-after (1- (point))) ? )))
439 (insert " "))
440 (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t))
441 (when (and (bolp)
442 (> shr-indentation 0))
443 (shr-indent))
444 ;; No space is needed behind a wide character categorized as
445 ;; kinsoku-bol, between characters both categorized as nospace,
446 ;; or at the beginning of a line.
447 (let (prev)
448 (when (and (> (current-column) shr-indentation)
449 (eq (preceding-char) ? )
450 (or (= (line-beginning-position) (1- (point)))
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)))))
456 (delete-char -1)))
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)))
462 (insert elem)
463 (setq shr-state nil)
464 (let (found)
465 (while (and (> (current-column) shr-width)
466 (progn
467 (setq found (shr-find-fill-point))
468 (not (eolp))))
469 (when (eq (preceding-char) ? )
470 (delete-char -1))
471 (insert "\n")
472 (unless found
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 " ")))
480 (unless (string-match "[ \t\r\n ]\\'" text)
481 (delete-char -1)))))
482
483 (defun shr-find-fill-point ()
484 (when (> (move-to-column shr-width) shr-width)
485 (backward-char 1))
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))
493 (if (eq (preceding-char) ?')
494 (not (memq (char-after (- (point) 2))
495 (list nil ?\n ? )))
496 (and (shr-char-kinsoku-bol-p (preceding-char))
497 (shr-char-breakable-p (following-char))
498 (not (shr-char-kinsoku-bol-p (following-char)))))
499 (shr-char-kinsoku-eol-p (following-char))))
500 (backward-char 1))
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)))
508 (if failed
509 ;; There's no breakable point, so we give it up.
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)))))
519 (or
520 (eolp)
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))
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)
560 (shr-char-kinsoku-bol-p (following-char))
561 (shr-char-breakable-p (following-char))))))))
562 (when (eq (following-char) ? )
563 (forward-char 1))))
564 (not failed)))
565
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))))
611
612 (defun shr-ensure-newline ()
613 (unless (zerop (current-column))
614 (insert "\n")))
615
616 (defun shr-ensure-paragraph ()
617 (unless (bobp)
618 (if (<= (current-column) shr-indentation)
619 (unless (save-excursion
620 (forward-line -1)
621 (looking-at " *$"))
622 (insert "\n"))
623 (if (save-excursion
624 (beginning-of-line)
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)))))
633 (delete-region (match-beginning 0) (match-end 0))
634 (insert "\n\n")))))
635
636 (defun shr-indent ()
637 (when (> shr-indentation 0)
638 (insert (make-string shr-indentation ? ))))
639
640 (defun shr-fontize-cont (cont &rest types)
641 (let (shr-start)
642 (shr-generic cont)
643 (dolist (type types)
644 (shr-add-font (or shr-start (point)) (point) type))))
645
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.
649 (defun shr-add-font (start end type)
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))))))
660
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
667 (defun shr-browse-url (&optional external)
668 "Browse the URL under point.
669 If EXTERNAL, browse the URL using `shr-external-browser'."
670 (interactive "P")
671 (let ((url (get-text-property (point) 'shr-url)))
672 (cond
673 ((not url)
674 (message "No link under point"))
675 ((string-match "^mailto:" url)
676 (browse-url-mail url))
677 (t
678 (if external
679 (funcall shr-external-browser url)
680 (browse-url url))))))
681
682 (defun shr-save-contents (directory)
683 "Save the contents from URL in a file."
684 (interactive "DSave contents of URL to directory: ")
685 (let ((url (get-text-property (point) 'shr-url)))
686 (if (not url)
687 (message "No link under point")
688 (url-retrieve (shr-encode-url url)
689 'shr-store-contents (list url directory)
690 nil t))))
691
692 (defun shr-store-contents (status url directory)
693 (unless (plist-get status :error)
694 (when (or (search-forward "\n\n" nil t)
695 (search-forward "\r\n\r\n" nil t))
696 (write-region (point) (point-max)
697 (expand-file-name (file-name-nondirectory url)
698 directory)))))
699
700 (defun shr-image-fetched (status buffer start end &optional flags)
701 (let ((image-buffer (current-buffer)))
702 (when (and (buffer-name buffer)
703 (not (plist-get status :error)))
704 (url-store-in-cache image-buffer)
705 (when (or (search-forward "\n\n" nil t)
706 (search-forward "\r\n\r\n" nil t))
707 (let ((data (buffer-substring (point) (point-max))))
708 (with-current-buffer buffer
709 (save-excursion
710 (let ((alt (buffer-substring start end))
711 (properties (text-properties-at start))
712 (inhibit-read-only t))
713 (delete-region start end)
714 (goto-char start)
715 (funcall shr-put-image-function data alt flags)
716 (while properties
717 (let ((type (pop properties))
718 (value (pop properties)))
719 (unless (memq type '(display image-size))
720 (put-text-property start (point) type value))))))))))
721 (kill-buffer image-buffer)))
722
723 (defun shr-image-from-data (data)
724 "Return an image from the data: URI content DATA."
725 (when (string-match
726 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
727 data)
728 (let ((param (match-string 4 data))
729 (payload (url-unhex-string (match-string 5 data))))
730 (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
731 (setq payload (base64-decode-string payload)))
732 payload)))
733
734 (defun shr-put-image (data alt &optional flags)
735 "Put image DATA with a string ALT. Return image."
736 (if (display-graphic-p)
737 (let* ((size (cdr (assq 'size flags)))
738 (start (point))
739 (image (cond
740 ((eq size 'original)
741 (create-image data nil t :ascent 100))
742 ((eq size 'full)
743 (ignore-errors
744 (shr-rescale-image data t)))
745 (t
746 (ignore-errors
747 (shr-rescale-image data))))))
748 (when image
749 ;; When inserting big-ish pictures, put them at the
750 ;; beginning of the line.
751 (when (and (> (current-column) 0)
752 (> (car (image-size image t)) 400))
753 (insert "\n"))
754 (if (eq size 'original)
755 (insert-sliced-image image (or alt "*") nil 20 1)
756 (insert-image image (or alt "*")))
757 (put-text-property start (point) 'image-size size)
758 (when (cond ((fboundp 'image-multi-frame-p)
759 ;; Only animate multi-frame things that specify a
760 ;; delay; eg animated gifs as opposed to
761 ;; multi-page tiffs. FIXME?
762 (cdr (image-multi-frame-p image)))
763 ((fboundp 'image-animated-p)
764 (image-animated-p image)))
765 (image-animate image nil 60)))
766 image)
767 (insert alt)))
768
769 (defun shr-rescale-image (data &optional force)
770 "Rescale DATA, if too big, to fit the current buffer.
771 If FORCE, rescale the image anyway."
772 (if (or (not (fboundp 'imagemagick-types))
773 (eq (image-type-from-data data) 'gif)
774 (not (get-buffer-window (current-buffer))))
775 (create-image data nil t :ascent 100)
776 (let ((edges (window-inside-pixel-edges
777 (get-buffer-window (current-buffer)))))
778 (create-image
779 data 'imagemagick t
780 :ascent 100
781 :max-width (truncate (* shr-max-image-proportion
782 (- (nth 2 edges) (nth 0 edges))))
783 :max-height (truncate (* shr-max-image-proportion
784 (- (nth 3 edges) (nth 1 edges))))))))
785
786 ;; url-cache-extract autoloads url-cache.
787 (declare-function url-cache-create-filename "url-cache" (url))
788 (autoload 'mm-disable-multibyte "mm-util")
789 (autoload 'browse-url-mail "browse-url")
790
791 (defun shr-get-image-data (url)
792 "Get image data for URL.
793 Return a string with image data."
794 (with-temp-buffer
795 (mm-disable-multibyte)
796 (when (ignore-errors
797 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
798 t)
799 (when (or (search-forward "\n\n" nil t)
800 (search-forward "\r\n\r\n" nil t))
801 (buffer-substring (point) (point-max))))))
802
803 (defun shr-image-displayer (content-function)
804 "Return a function to display an image.
805 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
806 is an argument. The function to be returned takes three arguments URL,
807 START, and END. Note that START and END should be markers."
808 `(lambda (url start end)
809 (when url
810 (if (string-match "\\`cid:" url)
811 ,(when content-function
812 `(let ((image (funcall ,content-function
813 (substring url (match-end 0)))))
814 (when image
815 (goto-char start)
816 (funcall shr-put-image-function
817 image (buffer-substring start end))
818 (delete-region (point) end))))
819 (url-retrieve url 'shr-image-fetched
820 (list (current-buffer) start end)
821 t t)))))
822
823 (defun shr-heading (cont &rest types)
824 (shr-ensure-paragraph)
825 (apply #'shr-fontize-cont cont types)
826 (shr-ensure-paragraph))
827
828 (defun shr-urlify (start url &optional title)
829 (when (and title (string-match "ctx" title)) (debug))
830 (shr-add-font start (point) 'shr-link)
831 (add-text-properties
832 start (point)
833 (list 'shr-url url
834 'help-echo (if title (format "%s (%s)" url title) url)
835 'keymap shr-map)))
836
837 (defun shr-encode-url (url)
838 "Encode URL."
839 (browse-url-url-encode-chars url "[)$ ]"))
840
841 (autoload 'shr-color-visible "shr-color")
842 (autoload 'shr-color->hexadecimal "shr-color")
843
844 (defun shr-color-check (fg bg)
845 "Check that FG is visible on BG.
846 Returns (fg bg) with corrected values.
847 Returns nil if the colors that would be used are the default
848 ones, in case fg and bg are nil."
849 (when (or fg bg)
850 (let ((fixed (cond ((null fg) 'fg)
851 ((null bg) 'bg))))
852 ;; Convert colors to hexadecimal, or set them to default.
853 (let ((fg (or (shr-color->hexadecimal fg)
854 (frame-parameter nil 'foreground-color)))
855 (bg (or (shr-color->hexadecimal bg)
856 (frame-parameter nil 'background-color))))
857 (cond ((eq fixed 'bg)
858 ;; Only return the new fg
859 (list nil (cadr (shr-color-visible bg fg t))))
860 ((eq fixed 'fg)
861 ;; Invert args and results and return only the new bg
862 (list (cadr (shr-color-visible fg bg t)) nil))
863 (t
864 (shr-color-visible bg fg)))))))
865
866 (defun shr-colorize-region (start end fg &optional bg)
867 (when (and (not shr-inhibit-decoration)
868 (or fg bg))
869 (let ((new-colors (shr-color-check fg bg)))
870 (when new-colors
871 (when fg
872 (add-face-text-property start end
873 (list :foreground (cadr new-colors))
874 t))
875 (when bg
876 (add-face-text-property start end
877 (list :background (car new-colors))
878 t)))
879 new-colors)))
880
881 (defun shr-expand-newlines (start end color)
882 (save-restriction
883 ;; Skip past all white space at the start and ends.
884 (goto-char start)
885 (skip-chars-forward " \t\n")
886 (beginning-of-line)
887 (setq start (point))
888 (goto-char end)
889 (skip-chars-backward " \t\n")
890 (forward-line 1)
891 (setq end (point))
892 (narrow-to-region start end)
893 (let ((width (shr-buffer-width))
894 column)
895 (goto-char (point-min))
896 (while (not (eobp))
897 (end-of-line)
898 (when (and (< (setq column (current-column)) width)
899 (< (setq column (shr-previous-newline-padding-width column))
900 width))
901 (let ((overlay (make-overlay (point) (1+ (point)))))
902 (overlay-put overlay 'before-string
903 (concat
904 (mapconcat
905 (lambda (overlay)
906 (let ((string (plist-get
907 (overlay-properties overlay)
908 'before-string)))
909 (if (not string)
910 ""
911 (overlay-put overlay 'before-string "")
912 string)))
913 (overlays-at (point))
914 "")
915 (propertize (make-string (- width column) ? )
916 'face (list :background color))))))
917 (forward-line 1)))))
918
919 (defun shr-previous-newline-padding-width (width)
920 (let ((overlays (overlays-at (point)))
921 (previous-width 0))
922 (if (null overlays)
923 width
924 (dolist (overlay overlays)
925 (setq previous-width
926 (+ previous-width
927 (length (plist-get (overlay-properties overlay)
928 'before-string)))))
929 (+ width previous-width))))
930
931 ;;; Tag-specific rendering rules.
932
933 (defun shr-tag-body (cont)
934 (let* ((start (point))
935 (fgcolor (cdr (or (assq :fgcolor cont)
936 (assq :text cont))))
937 (bgcolor (cdr (assq :bgcolor cont)))
938 (shr-stylesheet (list (cons 'color fgcolor)
939 (cons 'background-color bgcolor))))
940 (shr-generic cont)
941 (shr-colorize-region start (point) fgcolor bgcolor)))
942
943 (defun shr-tag-style (cont)
944 )
945
946 (defun shr-tag-script (cont)
947 )
948
949 (defun shr-tag-comment (cont)
950 )
951
952 (defun shr-dom-to-xml (dom)
953 "Convert DOM into a string containing the xml representation."
954 (let ((arg " ")
955 (text ""))
956 (dolist (sub (cdr dom))
957 (cond
958 ((listp (cdr sub))
959 (setq text (concat text (shr-dom-to-xml sub))))
960 ((eq (car sub) 'text)
961 (setq text (concat text (cdr sub))))
962 (t
963 (setq arg (concat arg (format "%s=\"%s\" "
964 (substring (symbol-name (car sub)) 1)
965 (cdr sub)))))))
966 (format "<%s%s>%s</%s>"
967 (car dom)
968 (substring arg 0 (1- (length arg)))
969 text
970 (car dom))))
971
972 (defun shr-tag-svg (cont)
973 (when (image-type-available-p 'svg)
974 (funcall shr-put-image-function
975 (shr-dom-to-xml (cons 'svg cont))
976 "SVG Image")))
977
978 (defun shr-tag-sup (cont)
979 (let ((start (point)))
980 (shr-generic cont)
981 (put-text-property start (point) 'display '(raise 0.5))))
982
983 (defun shr-tag-sub (cont)
984 (let ((start (point)))
985 (shr-generic cont)
986 (put-text-property start (point) 'display '(raise -0.5))))
987
988 (defun shr-tag-label (cont)
989 (shr-generic cont)
990 (shr-ensure-paragraph))
991
992 (defun shr-tag-p (cont)
993 (shr-ensure-paragraph)
994 (shr-indent)
995 (shr-generic cont)
996 (shr-ensure-paragraph))
997
998 (defun shr-tag-div (cont)
999 (shr-ensure-newline)
1000 (shr-indent)
1001 (shr-generic cont)
1002 (shr-ensure-newline))
1003
1004 (defun shr-tag-s (cont)
1005 (shr-fontize-cont cont 'shr-strike-through))
1006
1007 (defun shr-tag-del (cont)
1008 (shr-fontize-cont cont 'shr-strike-through))
1009
1010 (defun shr-tag-b (cont)
1011 (shr-fontize-cont cont 'bold))
1012
1013 (defun shr-tag-i (cont)
1014 (shr-fontize-cont cont 'italic))
1015
1016 (defun shr-tag-em (cont)
1017 (shr-fontize-cont cont 'italic))
1018
1019 (defun shr-tag-strong (cont)
1020 (shr-fontize-cont cont 'bold))
1021
1022 (defun shr-tag-u (cont)
1023 (shr-fontize-cont cont 'underline))
1024
1025 (defun shr-parse-style (style)
1026 (when style
1027 (save-match-data
1028 (when (string-match "\n" style)
1029 (setq style (replace-match " " t t style))))
1030 (let ((plist nil))
1031 (dolist (elem (split-string style ";"))
1032 (when elem
1033 (setq elem (split-string elem ":"))
1034 (when (and (car elem)
1035 (cadr elem))
1036 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1037 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1038 (when (string-match " *!important\\'" value)
1039 (setq value (substring value 0 (match-beginning 0))))
1040 (push (cons (intern name obarray)
1041 value)
1042 plist)))))
1043 plist)))
1044
1045 (defun shr-tag-base (cont)
1046 (let ((base (cdr (assq :href cont))))
1047 (when base
1048 (setq shr-base (shr-parse-base base))))
1049 (shr-generic cont))
1050
1051 (defun shr-tag-a (cont)
1052 (let ((url (cdr (assq :href cont)))
1053 (title (cdr (assq :title cont)))
1054 (start (point))
1055 shr-start)
1056 (shr-generic cont)
1057 (when (and url
1058 (not shr-inhibit-decoration))
1059 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1060
1061 (defun shr-tag-object (cont)
1062 (let ((start (point))
1063 url)
1064 (dolist (elem cont)
1065 (when (eq (car elem) 'embed)
1066 (setq url (or url (cdr (assq :src (cdr elem))))))
1067 (when (and (eq (car elem) 'param)
1068 (equal (cdr (assq :name (cdr elem))) "movie"))
1069 (setq url (or url (cdr (assq :value (cdr elem)))))))
1070 (when url
1071 (shr-insert " [multimedia] ")
1072 (shr-urlify start (shr-expand-url url)))
1073 (shr-generic cont)))
1074
1075 (defun shr-tag-video (cont)
1076 (let ((image (cdr (assq :poster cont)))
1077 (url (cdr (assq :src cont)))
1078 (start (point)))
1079 (shr-tag-img nil image)
1080 (shr-urlify start (shr-expand-url url))))
1081
1082 (defun shr-tag-img (cont &optional url)
1083 (when (or url
1084 (and cont
1085 (cdr (assq :src cont))))
1086 (when (and (> (current-column) 0)
1087 (not (eq shr-state 'image)))
1088 (insert "\n"))
1089 (let ((alt (cdr (assq :alt cont)))
1090 (url (shr-expand-url (or url (cdr (assq :src cont))))))
1091 (let ((start (point-marker)))
1092 (when (zerop (length alt))
1093 (setq alt "*"))
1094 (cond
1095 ((or (member (cdr (assq :height cont)) '("0" "1"))
1096 (member (cdr (assq :width cont)) '("0" "1")))
1097 ;; Ignore zero-sized or single-pixel images.
1098 )
1099 ((and (not shr-inhibit-images)
1100 (string-match "\\`data:" url))
1101 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1102 (if image
1103 (funcall shr-put-image-function image alt)
1104 (insert alt))))
1105 ((and (not shr-inhibit-images)
1106 (string-match "\\`cid:" url))
1107 (let ((url (substring url (match-end 0)))
1108 image)
1109 (if (or (not shr-content-function)
1110 (not (setq image (funcall shr-content-function url))))
1111 (insert alt)
1112 (funcall shr-put-image-function image alt))))
1113 ((or shr-inhibit-images
1114 (and shr-blocked-images
1115 (string-match shr-blocked-images url)))
1116 (setq shr-start (point))
1117 (let ((shr-state 'space))
1118 (if (> (string-width alt) 8)
1119 (shr-insert (truncate-string-to-width alt 8))
1120 (shr-insert alt))))
1121 ((and (not shr-ignore-cache)
1122 (url-is-cached (shr-encode-url url)))
1123 (funcall shr-put-image-function (shr-get-image-data url) alt))
1124 (t
1125 (insert alt " ")
1126 (when (and shr-ignore-cache
1127 (url-is-cached (shr-encode-url url)))
1128 (let ((file (url-cache-create-filename (shr-encode-url url))))
1129 (when (file-exists-p file)
1130 (delete-file file))))
1131 (url-queue-retrieve
1132 (shr-encode-url url) 'shr-image-fetched
1133 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1134 t t)))
1135 (when (zerop shr-table-depth) ;; We are not in a table.
1136 (put-text-property start (point) 'keymap shr-map)
1137 (put-text-property start (point) 'shr-alt alt)
1138 (put-text-property start (point) 'image-url url)
1139 (put-text-property start (point) 'image-displayer
1140 (shr-image-displayer shr-content-function))
1141 (put-text-property start (point) 'help-echo alt))
1142 (setq shr-state 'image)))))
1143
1144 (defun shr-tag-pre (cont)
1145 (let ((shr-folding-mode 'none))
1146 (shr-ensure-newline)
1147 (shr-indent)
1148 (shr-generic cont)
1149 (shr-ensure-newline)))
1150
1151 (defun shr-tag-blockquote (cont)
1152 (shr-ensure-paragraph)
1153 (shr-indent)
1154 (let ((shr-indentation (+ shr-indentation 4)))
1155 (shr-generic cont))
1156 (shr-ensure-paragraph))
1157
1158 (defun shr-tag-dl (cont)
1159 (shr-ensure-paragraph)
1160 (shr-generic cont)
1161 (shr-ensure-paragraph))
1162
1163 (defun shr-tag-dt (cont)
1164 (shr-ensure-newline)
1165 (shr-generic cont)
1166 (shr-ensure-newline))
1167
1168 (defun shr-tag-dd (cont)
1169 (shr-ensure-newline)
1170 (let ((shr-indentation (+ shr-indentation 4)))
1171 (shr-generic cont)))
1172
1173 (defun shr-tag-ul (cont)
1174 (shr-ensure-paragraph)
1175 (let ((shr-list-mode 'ul))
1176 (shr-generic cont))
1177 (shr-ensure-paragraph))
1178
1179 (defun shr-tag-ol (cont)
1180 (shr-ensure-paragraph)
1181 (let ((shr-list-mode 1))
1182 (shr-generic cont))
1183 (shr-ensure-paragraph))
1184
1185 (defun shr-tag-li (cont)
1186 (shr-ensure-newline)
1187 (shr-indent)
1188 (let* ((bullet
1189 (if (numberp shr-list-mode)
1190 (prog1
1191 (format "%d " shr-list-mode)
1192 (setq shr-list-mode (1+ shr-list-mode)))
1193 shr-bullet))
1194 (shr-indentation (+ shr-indentation (length bullet))))
1195 (insert bullet)
1196 (shr-generic cont)))
1197
1198 (defun shr-tag-br (cont)
1199 (when (and (not (bobp))
1200 ;; Only add a newline if we break the current line, or
1201 ;; the previous line isn't a blank line.
1202 (or (not (bolp))
1203 (and (> (- (point) 2) (point-min))
1204 (not (= (char-after (- (point) 2)) ?\n)))))
1205 (insert "\n")
1206 (shr-indent))
1207 (shr-generic cont))
1208
1209 (defun shr-tag-span (cont)
1210 (shr-generic cont))
1211
1212 (defun shr-tag-h1 (cont)
1213 (shr-heading cont 'bold 'underline))
1214
1215 (defun shr-tag-h2 (cont)
1216 (shr-heading cont 'bold))
1217
1218 (defun shr-tag-h3 (cont)
1219 (shr-heading cont 'italic))
1220
1221 (defun shr-tag-h4 (cont)
1222 (shr-heading cont))
1223
1224 (defun shr-tag-h5 (cont)
1225 (shr-heading cont))
1226
1227 (defun shr-tag-h6 (cont)
1228 (shr-heading cont))
1229
1230 (defun shr-tag-hr (cont)
1231 (shr-ensure-newline)
1232 (insert (make-string shr-width shr-hr-line) "\n"))
1233
1234 (defun shr-tag-title (cont)
1235 (shr-heading cont 'bold 'underline))
1236
1237 (defun shr-tag-font (cont)
1238 (let* ((start (point))
1239 (color (cdr (assq :color cont)))
1240 (shr-stylesheet (nconc (list (cons 'color color))
1241 shr-stylesheet)))
1242 (shr-generic cont)
1243 (when color
1244 (shr-colorize-region start (point) color
1245 (cdr (assq 'background-color shr-stylesheet))))))
1246
1247 ;;; Table rendering algorithm.
1248
1249 ;; Table rendering is the only complicated thing here. We do this by
1250 ;; first counting how many TDs there are in each TR, and registering
1251 ;; how wide they think they should be ("width=45%", etc). Then we
1252 ;; render each TD separately (this is done in temporary buffers, so
1253 ;; that we can use all the rendering machinery as if we were in the
1254 ;; main buffer). Now we know how much space each TD really takes, so
1255 ;; we then render everything again with the new widths, and finally
1256 ;; insert all these boxes into the main buffer.
1257 (defun shr-tag-table-1 (cont)
1258 (setq cont (or (cdr (assq 'tbody cont))
1259 cont))
1260 (let* ((shr-inhibit-images t)
1261 (shr-table-depth (1+ shr-table-depth))
1262 (shr-kinsoku-shorten t)
1263 ;; Find all suggested widths.
1264 (columns (shr-column-specs cont))
1265 ;; Compute how many characters wide each TD should be.
1266 (suggested-widths (shr-pro-rate-columns columns))
1267 ;; Do a "test rendering" to see how big each TD is (this can
1268 ;; be smaller (if there's little text) or bigger (if there's
1269 ;; unbreakable text).
1270 (sketch (shr-make-table cont suggested-widths))
1271 ;; Compute the "natural" width by setting each column to 500
1272 ;; characters and see how wide they really render.
1273 (natural (shr-make-table cont (make-vector (length columns) 500)))
1274 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1275 ;; This probably won't work very well.
1276 (when (> (+ (loop for width across sketch-widths
1277 summing (1+ width))
1278 shr-indentation 1)
1279 (frame-width))
1280 (setq truncate-lines t))
1281 ;; Then render the table again with these new "hard" widths.
1282 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
1283
1284 (defun shr-tag-table (cont)
1285 (shr-ensure-paragraph)
1286 (let* ((caption (cdr (assq 'caption cont)))
1287 (header (cdr (assq 'thead cont)))
1288 (body (or (cdr (assq 'tbody cont)) cont))
1289 (footer (cdr (assq 'tfoot cont)))
1290 (bgcolor (cdr (assq :bgcolor cont)))
1291 (start (point))
1292 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1293 shr-stylesheet))
1294 (nheader (if header (shr-max-columns header)))
1295 (nbody (if body (shr-max-columns body)))
1296 (nfooter (if footer (shr-max-columns footer))))
1297 (if (and (not caption)
1298 (not header)
1299 (not (cdr (assq 'tbody cont)))
1300 (not (cdr (assq 'tr cont)))
1301 (not footer))
1302 ;; The table is totally invalid and just contains random junk.
1303 ;; Try to output it anyway.
1304 (shr-generic cont)
1305 ;; It's a real table, so render it.
1306 (shr-tag-table-1
1307 (nconc
1308 (if caption `((tr (td ,@caption))))
1309 (if header
1310 (if footer
1311 ;; hader + body + footer
1312 (if (= nheader nbody)
1313 (if (= nbody nfooter)
1314 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
1315 (nconc `((tr (td (table (tbody ,@header ,@body)))))
1316 (if (= nfooter 1)
1317 footer
1318 `((tr (td (table (tbody ,@footer))))))))
1319 (nconc `((tr (td (table (tbody ,@header)))))
1320 (if (= nbody nfooter)
1321 `((tr (td (table (tbody ,@body ,@footer)))))
1322 (nconc `((tr (td (table (tbody ,@body)))))
1323 (if (= nfooter 1)
1324 footer
1325 `((tr (td (table (tbody ,@footer))))))))))
1326 ;; header + body
1327 (if (= nheader nbody)
1328 `((tr (td (table (tbody ,@header ,@body)))))
1329 (if (= nheader 1)
1330 `(,@header (tr (td (table (tbody ,@body)))))
1331 `((tr (td (table (tbody ,@header))))
1332 (tr (td (table (tbody ,@body))))))))
1333 (if footer
1334 ;; body + footer
1335 (if (= nbody nfooter)
1336 `((tr (td (table (tbody ,@body ,@footer)))))
1337 (nconc `((tr (td (table (tbody ,@body)))))
1338 (if (= nfooter 1)
1339 footer
1340 `((tr (td (table (tbody ,@footer))))))))
1341 (if caption
1342 `((tr (td (table (tbody ,@body)))))
1343 body))))))
1344 (when bgcolor
1345 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1346 bgcolor))
1347 ;; Finally, insert all the images after the table. The Emacs buffer
1348 ;; model isn't strong enough to allow us to put the images actually
1349 ;; into the tables.
1350 (when (zerop shr-table-depth)
1351 (dolist (elem (shr-find-elements cont 'img))
1352 (shr-tag-img (cdr elem))))))
1353
1354 (defun shr-find-elements (cont type)
1355 (let (result)
1356 (dolist (elem cont)
1357 (cond ((eq (car elem) type)
1358 (push elem result))
1359 ((consp (cdr elem))
1360 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1361 (nreverse result)))
1362
1363 (defun shr-insert-table (table widths)
1364 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1365 "collapse"))
1366 (shr-table-separator-length (if collapse 0 1))
1367 (shr-table-vertical-line (if collapse "" shr-table-vertical-line)))
1368 (unless collapse
1369 (shr-insert-table-ruler widths))
1370 (dolist (row table)
1371 (let ((start (point))
1372 (height (let ((max 0))
1373 (dolist (column row)
1374 (setq max (max max (cadr column))))
1375 max)))
1376 (dotimes (i height)
1377 (shr-indent)
1378 (insert shr-table-vertical-line "\n"))
1379 (dolist (column row)
1380 (goto-char start)
1381 (let ((lines (nth 2 column)))
1382 (dolist (line lines)
1383 (end-of-line)
1384 (insert line shr-table-vertical-line)
1385 (forward-line 1))
1386 ;; Add blank lines at padding at the bottom of the TD,
1387 ;; possibly.
1388 (dotimes (i (- height (length lines)))
1389 (end-of-line)
1390 (let ((start (point)))
1391 (insert (make-string (string-width (car lines)) ? )
1392 shr-table-vertical-line)
1393 (when (nth 4 column)
1394 (shr-add-font start (1- (point))
1395 (list :background (nth 4 column)))))
1396 (forward-line 1)))))
1397 (unless collapse
1398 (shr-insert-table-ruler widths)))))
1399
1400 (defun shr-insert-table-ruler (widths)
1401 (when shr-table-horizontal-line
1402 (when (and (bolp)
1403 (> shr-indentation 0))
1404 (shr-indent))
1405 (insert shr-table-corner)
1406 (dotimes (i (length widths))
1407 (insert (make-string (aref widths i) shr-table-horizontal-line)
1408 shr-table-corner))
1409 (insert "\n")))
1410
1411 (defun shr-table-widths (table natural-table suggested-widths)
1412 (let* ((length (length suggested-widths))
1413 (widths (make-vector length 0))
1414 (natural-widths (make-vector length 0)))
1415 (dolist (row table)
1416 (let ((i 0))
1417 (dolist (column row)
1418 (aset widths i (max (aref widths i) column))
1419 (setq i (1+ i)))))
1420 (dolist (row natural-table)
1421 (let ((i 0))
1422 (dolist (column row)
1423 (aset natural-widths i (max (aref natural-widths i) column))
1424 (setq i (1+ i)))))
1425 (let ((extra (- (apply '+ (append suggested-widths nil))
1426 (apply '+ (append widths nil))))
1427 (expanded-columns 0))
1428 ;; We have extra, unused space, so divide this space amongst the
1429 ;; columns.
1430 (when (> extra 0)
1431 ;; If the natural width is wider than the rendered width, we
1432 ;; want to allow the column to expand.
1433 (dotimes (i length)
1434 (when (> (aref natural-widths i) (aref widths i))
1435 (setq expanded-columns (1+ expanded-columns))))
1436 (dotimes (i length)
1437 (when (> (aref natural-widths i) (aref widths i))
1438 (aset widths i (min
1439 (aref natural-widths i)
1440 (+ (/ extra expanded-columns)
1441 (aref widths i))))))))
1442 widths))
1443
1444 (defun shr-make-table (cont widths &optional fill)
1445 (or (cadr (assoc (list cont widths fill) shr-content-cache))
1446 (let ((data (shr-make-table-1 cont widths fill)))
1447 (push (list (list cont widths fill) data)
1448 shr-content-cache)
1449 data)))
1450
1451 (defun shr-make-table-1 (cont widths &optional fill)
1452 (let ((trs nil)
1453 (shr-inhibit-decoration (not fill))
1454 (rowspans (make-vector (length widths) 0))
1455 width colspan)
1456 (dolist (row cont)
1457 (when (eq (car row) 'tr)
1458 (let ((tds nil)
1459 (columns (cdr row))
1460 (i 0)
1461 (width-column 0)
1462 column)
1463 (while (< i (length widths))
1464 ;; If we previously had a rowspan definition, then that
1465 ;; means that we now have a "missing" td/th element here.
1466 ;; So just insert a dummy, empty one to (sort of) emulate
1467 ;; rowspan.
1468 (setq column
1469 (if (zerop (aref rowspans i))
1470 (pop columns)
1471 (aset rowspans i (1- (aref rowspans i)))
1472 '(td)))
1473 (when (or (memq (car column) '(td th))
1474 (not column))
1475 (when (cdr (assq :rowspan (cdr column)))
1476 (aset rowspans i (+ (aref rowspans i)
1477 (1- (string-to-number
1478 (cdr (assq :rowspan (cdr column))))))))
1479 ;; Sanity check for invalid column-spans.
1480 (when (>= width-column (length widths))
1481 (setq width-column 0))
1482 (setq width
1483 (if column
1484 (aref widths width-column)
1485 10))
1486 (when (and fill
1487 (setq colspan (cdr (assq :colspan (cdr column)))))
1488 (setq colspan (string-to-number colspan))
1489 (dotimes (j (1- colspan))
1490 (if (> (+ i 1 j) (1- (length widths)))
1491 (setq width (aref widths (1- (length widths))))
1492 (setq width (+ width
1493 shr-table-separator-length
1494 (aref widths (+ i 1 j))))))
1495 (setq width-column (+ width-column (1- colspan))))
1496 (when (or column
1497 (not fill))
1498 ;; Sanity check for degenerate tables.
1499 (when (zerop width)
1500 (setq width 10))
1501 (push (shr-render-td (cdr column) width fill)
1502 tds))
1503 (setq i (1+ i)
1504 width-column (1+ width-column))))
1505 (push (nreverse tds) trs))))
1506 (nreverse trs)))
1507
1508 (defun shr-render-td (cont width fill)
1509 (when (= width 0) (debug))
1510 (with-temp-buffer
1511 (let ((bgcolor (cdr (assq :bgcolor cont)))
1512 (fgcolor (cdr (assq :fgcolor cont)))
1513 (style (cdr (assq :style cont)))
1514 (shr-stylesheet shr-stylesheet)
1515 actual-colors)
1516 (when style
1517 (setq style (and (string-match "color" style)
1518 (shr-parse-style style))))
1519 (when bgcolor
1520 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1521 (when fgcolor
1522 (setq style (nconc (list (cons 'color fgcolor)) style)))
1523 (when style
1524 (setq shr-stylesheet (append style shr-stylesheet)))
1525 (let ((shr-width width)
1526 (shr-indentation 0))
1527 (shr-descend (cons 'td cont)))
1528 ;; Delete padding at the bottom of the TDs.
1529 (delete-region
1530 (point)
1531 (progn
1532 (skip-chars-backward " \t\n")
1533 (end-of-line)
1534 (point)))
1535 (goto-char (point-min))
1536 (let ((max 0))
1537 (while (not (eobp))
1538 (end-of-line)
1539 (setq max (max max (current-column)))
1540 (forward-line 1))
1541 (when fill
1542 (goto-char (point-min))
1543 ;; If the buffer is totally empty, then put a single blank
1544 ;; line here.
1545 (if (zerop (buffer-size))
1546 (insert (make-string width ? ))
1547 ;; Otherwise, fill the buffer.
1548 (let ((align (cdr (assq :align cont)))
1549 length)
1550 (while (not (eobp))
1551 (end-of-line)
1552 (setq length (- width (current-column)))
1553 (when (> length 0)
1554 (cond
1555 ((equal align "right")
1556 (beginning-of-line)
1557 (insert (make-string length ? )))
1558 ((equal align "center")
1559 (insert (make-string (/ length 2) ? ))
1560 (beginning-of-line)
1561 (insert (make-string (- length (/ length 2)) ? )))
1562 (t
1563 (insert (make-string length ? )))))
1564 (forward-line 1))))
1565 (when style
1566 (setq actual-colors
1567 (shr-colorize-region
1568 (point-min) (point-max)
1569 (cdr (assq 'color shr-stylesheet))
1570 (cdr (assq 'background-color shr-stylesheet))))))
1571 (if fill
1572 (list max
1573 (count-lines (point-min) (point-max))
1574 (split-string (buffer-string) "\n")
1575 nil
1576 (car actual-colors))
1577 max)))))
1578
1579 (defun shr-buffer-width ()
1580 (goto-char (point-min))
1581 (let ((max 0))
1582 (while (not (eobp))
1583 (end-of-line)
1584 (setq max (max max (current-column)))
1585 (forward-line 1))
1586 max))
1587
1588 (defun shr-pro-rate-columns (columns)
1589 (let ((total-percentage 0)
1590 (widths (make-vector (length columns) 0)))
1591 (dotimes (i (length columns))
1592 (setq total-percentage (+ total-percentage (aref columns i))))
1593 (setq total-percentage (/ 1.0 total-percentage))
1594 (dotimes (i (length columns))
1595 (aset widths i (max (truncate (* (aref columns i)
1596 total-percentage
1597 (- shr-width (1+ (length columns)))))
1598 10)))
1599 widths))
1600
1601 ;; Return a summary of the number and shape of the TDs in the table.
1602 (defun shr-column-specs (cont)
1603 (let ((columns (make-vector (shr-max-columns cont) 1)))
1604 (dolist (row cont)
1605 (when (eq (car row) 'tr)
1606 (let ((i 0))
1607 (dolist (column (cdr row))
1608 (when (memq (car column) '(td th))
1609 (let ((width (cdr (assq :width (cdr column)))))
1610 (when (and width
1611 (string-match "\\([0-9]+\\)%" width)
1612 (not (zerop (setq width (string-to-number
1613 (match-string 1 width))))))
1614 (aset columns i (/ width 100.0))))
1615 (setq i (1+ i)))))))
1616 columns))
1617
1618 (defun shr-count (cont elem)
1619 (let ((i 0))
1620 (dolist (sub cont)
1621 (when (eq (car sub) elem)
1622 (setq i (1+ i))))
1623 i))
1624
1625 (defun shr-max-columns (cont)
1626 (let ((max 0))
1627 (dolist (row cont)
1628 (when (eq (car row) 'tr)
1629 (setq max (max max (+ (shr-count (cdr row) 'td)
1630 (shr-count (cdr row) 'th))))))
1631 max))
1632
1633 (provide 'shr)
1634
1635 ;; Local Variables:
1636 ;; coding: utf-8
1637 ;; End:
1638
1639 ;;; shr.el ends here