lisp/gnus/eww.el (eww-tag-input): Implement submit buttons
[emacs/old-mirror.git] / lisp / gnus / shr.el
blobd9e267e52885f77d28ce5644e1d0f9827c21ee00
1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; 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.
31 ;;; Code:
33 (eval-when-compile (require 'cl))
34 (require 'browse-url)
36 (defgroup shr nil
37 "Simple HTML Renderer"
38 :version "24.1"
39 :group 'mail)
41 (defcustom shr-max-image-proportion 0.9
42 "How big pictures displayed are in relation to the window they're in.
43 A value of 0.7 means that they are allowed to take up 70% of the
44 width and height of the window. If they are larger than this,
45 and Emacs supports it, then the images will be rescaled down to
46 fit these criteria."
47 :version "24.1"
48 :group 'shr
49 :type 'float)
51 (defcustom shr-blocked-images nil
52 "Images that have URLs matching this regexp will be blocked."
53 :version "24.1"
54 :group 'shr
55 :type '(choice (const nil) regexp))
57 (defcustom shr-table-horizontal-line ?\s
58 "Character used to draw horizontal table lines."
59 :group 'shr
60 :type 'character)
62 (defcustom shr-table-vertical-line ?\s
63 "Character used to draw vertical table lines."
64 :group 'shr
65 :type 'character)
67 (defcustom shr-table-corner ?\s
68 "Character used to draw table corners."
69 :group 'shr
70 :type 'character)
72 (defcustom shr-hr-line ?-
73 "Character used to draw hr lines."
74 :group 'shr
75 :type 'character)
77 (defcustom shr-width fill-column
78 "Frame width to use for rendering.
79 May either be an integer specifying a fixed width in characters,
80 or nil, meaning that the full width of the window should be
81 used."
82 :type '(choice (integer :tag "Fixed width in characters")
83 (const :tag "Use the width of the window" nil))
84 :group 'shr)
86 (defvar shr-content-function nil
87 "If bound, this should be a function that will return the content.
88 This is used for cid: URLs, and the function is called with the
89 cid: URL as the argument.")
91 (defvar shr-put-image-function 'shr-put-image
92 "Function called to put image and alt string.")
94 (defface shr-strike-through '((t (:strike-through t)))
95 "Font for <s> elements."
96 :group 'shr)
98 (defface shr-link
99 '((t (:inherit link)))
100 "Font for link elements."
101 :group 'shr)
103 ;;; Internal variables.
105 (defvar shr-folding-mode nil)
106 (defvar shr-state nil)
107 (defvar shr-start nil)
108 (defvar shr-indentation 0)
109 (defvar shr-inhibit-images nil)
110 (defvar shr-list-mode nil)
111 (defvar shr-content-cache nil)
112 (defvar shr-kinsoku-shorten nil)
113 (defvar shr-table-depth 0)
114 (defvar shr-stylesheet nil)
115 (defvar shr-base nil)
116 (defvar shr-ignore-cache nil)
117 (defvar shr-external-rendering-functions nil)
118 (defvar shr-final-table-render nil)
120 (defvar shr-map
121 (let ((map (make-sparse-keymap)))
122 (define-key map "a" 'shr-show-alt-text)
123 (define-key map "i" 'shr-browse-image)
124 (define-key map "z" 'shr-zoom-image)
125 (define-key map "I" 'shr-insert-image)
126 (define-key map "u" 'shr-copy-url)
127 (define-key map "v" 'shr-browse-url)
128 (define-key map "o" 'shr-save-contents)
129 (define-key map "\r" 'shr-browse-url)
130 map))
132 ;; Public functions and commands.
134 (defun shr-render-buffer (buffer)
135 "Display the HTML rendering of the current buffer."
136 (interactive (list (current-buffer)))
137 (pop-to-buffer "*html*")
138 (erase-buffer)
139 (shr-insert-document
140 (with-current-buffer buffer
141 (libxml-parse-html-region (point-min) (point-max))))
142 (goto-char (point-min)))
144 (defun shr-visit-file (file)
145 "Parse FILE as an HTML document, and render it in a new buffer."
146 (interactive "fHTML file name: ")
147 (with-temp-buffer
148 (insert-file-contents file)
149 (shr-render-buffer (current-buffer))))
151 ;;;###autoload
152 (defun shr-insert-document (dom)
153 "Render the parsed document DOM into the current buffer.
154 DOM should be a parse tree as generated by
155 `libxml-parse-html-region' or similar."
156 (setq shr-content-cache nil)
157 (let ((start (point))
158 (shr-state nil)
159 (shr-start nil)
160 (shr-base nil)
161 (shr-width (or shr-width (window-width))))
162 (shr-descend (shr-transform-dom dom))
163 (shr-remove-trailing-whitespace start (point))))
165 (defun shr-remove-trailing-whitespace (start end)
166 (let ((width (window-width)))
167 (save-restriction
168 (narrow-to-region start end)
169 (goto-char start)
170 (while (not (eobp))
171 (end-of-line)
172 (when (> (shr-previous-newline-padding-width (current-column)) width)
173 (dolist (overlay (overlays-at (point)))
174 (when (overlay-get overlay 'before-string)
175 (overlay-put overlay 'before-string nil))))
176 (forward-line 1)))))
178 (defun shr-copy-url ()
179 "Copy the URL under point to the kill ring.
180 If called twice, then try to fetch the URL and see whether it
181 redirects somewhere else."
182 (interactive)
183 (let ((url (get-text-property (point) 'shr-url)))
184 (cond
185 ((not url)
186 (message "No URL under point"))
187 ;; Resolve redirected URLs.
188 ((equal url (car kill-ring))
189 (url-retrieve
191 (lambda (a)
192 (when (and (consp a)
193 (eq (car a) :redirect))
194 (with-temp-buffer
195 (insert (cadr a))
196 (goto-char (point-min))
197 ;; Remove common tracking junk from the URL.
198 (when (re-search-forward ".utm_.*" nil t)
199 (replace-match "" t t))
200 (message "Copied %s" (buffer-string))
201 (copy-region-as-kill (point-min) (point-max)))))
202 nil t))
203 ;; Copy the URL to the kill ring.
205 (with-temp-buffer
206 (insert url)
207 (copy-region-as-kill (point-min) (point-max))
208 (message "Copied %s" url))))))
210 (defun shr-show-alt-text ()
211 "Show the ALT text of the image under point."
212 (interactive)
213 (let ((text (get-text-property (point) 'shr-alt)))
214 (if (not text)
215 (message "No image under point")
216 (message "%s" text))))
218 (defun shr-browse-image (&optional copy-url)
219 "Browse the image under point.
220 If COPY-URL (the prefix if called interactively) is non-nil, copy
221 the URL of the image to the kill buffer instead."
222 (interactive "P")
223 (let ((url (get-text-property (point) 'image-url)))
224 (cond
225 ((not url)
226 (message "No image under point"))
227 (copy-url
228 (with-temp-buffer
229 (insert url)
230 (copy-region-as-kill (point-min) (point-max))
231 (message "Copied %s" url)))
233 (message "Browsing %s..." url)
234 (browse-url url)))))
236 (defun shr-insert-image ()
237 "Insert the image under point into the buffer."
238 (interactive)
239 (let ((url (get-text-property (point) 'image-url)))
240 (if (not url)
241 (message "No image under point")
242 (message "Inserting %s..." url)
243 (url-retrieve url 'shr-image-fetched
244 (list (current-buffer) (1- (point)) (point-marker))
245 t t))))
247 (defun shr-zoom-image ()
248 "Toggle the image size.
249 The size will be rotated between the default size, the original
250 size, and full-buffer size."
251 (interactive)
252 (let ((url (get-text-property (point) 'image-url))
253 (size (get-text-property (point) 'image-size))
254 (buffer-read-only nil))
255 (if (not url)
256 (message "No image under point")
257 ;; Delete the old picture.
258 (while (get-text-property (point) 'image-url)
259 (forward-char -1))
260 (forward-char 1)
261 (let ((start (point)))
262 (while (get-text-property (point) 'image-url)
263 (forward-char 1))
264 (forward-char -1)
265 (put-text-property start (point) 'display nil)
266 (when (> (- (point) start) 2)
267 (delete-region start (1- (point)))))
268 (message "Inserting %s..." url)
269 (url-retrieve url 'shr-image-fetched
270 (list (current-buffer) (1- (point)) (point-marker)
271 (list (cons 'size
272 (cond ((or (eq size 'default)
273 (null size))
274 'original)
275 ((eq size 'original)
276 'full)
277 ((eq size 'full)
278 'default)))))
279 t))))
281 ;;; Utility functions.
283 (defun shr-transform-dom (dom)
284 (let ((result (list (pop dom))))
285 (dolist (arg (pop dom))
286 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
287 (cdr arg))
288 result))
289 (dolist (sub dom)
290 (if (stringp sub)
291 (push (cons 'text sub) result)
292 (push (shr-transform-dom sub) result)))
293 (nreverse result)))
295 (defun shr-descend (dom)
296 (let ((function
298 ;; Allow other packages to override (or provide) rendering
299 ;; of elements.
300 (cdr (assq (car dom) shr-external-rendering-functions))
301 (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
302 (style (cdr (assq :style (cdr dom))))
303 (shr-stylesheet shr-stylesheet)
304 (start (point)))
305 (when style
306 (if (string-match "color" style)
307 (setq shr-stylesheet (nconc (shr-parse-style style)
308 shr-stylesheet))
309 (setq style nil)))
310 (if (fboundp function)
311 (funcall function (cdr dom))
312 (shr-generic (cdr dom)))
313 ;; If style is set, then this node has set the color.
314 (when style
315 (shr-colorize-region start (point)
316 (cdr (assq 'color shr-stylesheet))
317 (cdr (assq 'background-color shr-stylesheet))))))
319 (defun shr-generic (cont)
320 (dolist (sub cont)
321 (cond
322 ((eq (car sub) 'text)
323 (shr-insert (cdr sub)))
324 ((listp (cdr sub))
325 (shr-descend sub)))))
327 (defmacro shr-char-breakable-p (char)
328 "Return non-nil if a line can be broken before and after CHAR."
329 `(aref fill-find-break-point-function-table ,char))
330 (defmacro shr-char-nospace-p (char)
331 "Return non-nil if no space is required before and after CHAR."
332 `(aref fill-nospace-between-words-table ,char))
334 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
335 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
336 ;; parentheses, and so on, that should not be placed in the beginning
337 ;; of a line or the end of a line.
338 (defmacro shr-char-kinsoku-bol-p (char)
339 "Return non-nil if a line ought not to begin with CHAR."
340 `(aref (char-category-set ,char) ?>))
341 (defmacro shr-char-kinsoku-eol-p (char)
342 "Return non-nil if a line ought not to end with CHAR."
343 `(aref (char-category-set ,char) ?<))
344 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
345 (load "kinsoku" nil t))
347 (defun shr-insert (text)
348 (when (and (eq shr-state 'image)
349 (not (bolp))
350 (not (string-match "\\`[ \t\n]+\\'" text)))
351 (insert "\n")
352 (setq shr-state nil))
353 (cond
354 ((eq shr-folding-mode 'none)
355 (insert text))
357 (when (and (string-match "\\`[ \t\n ]" text)
358 (not (bolp))
359 (not (eq (char-after (1- (point))) ? )))
360 (insert " "))
361 (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t))
362 (when (and (bolp)
363 (> shr-indentation 0))
364 (shr-indent))
365 ;; No space is needed behind a wide character categorized as
366 ;; kinsoku-bol, between characters both categorized as nospace,
367 ;; or at the beginning of a line.
368 (let (prev)
369 (when (and (> (current-column) shr-indentation)
370 (eq (preceding-char) ? )
371 (or (= (line-beginning-position) (1- (point)))
372 (and (shr-char-breakable-p
373 (setq prev (char-after (- (point) 2))))
374 (shr-char-kinsoku-bol-p prev))
375 (and (shr-char-nospace-p prev)
376 (shr-char-nospace-p (aref elem 0)))))
377 (delete-char -1)))
378 ;; The shr-start is a special variable that is used to pass
379 ;; upwards the first point in the buffer where the text really
380 ;; starts.
381 (unless shr-start
382 (setq shr-start (point)))
383 (insert elem)
384 (setq shr-state nil)
385 (let (found)
386 (while (and (> (current-column) shr-width)
387 (progn
388 (setq found (shr-find-fill-point))
389 (not (eolp))))
390 (when (eq (preceding-char) ? )
391 (delete-char -1))
392 (insert "\n")
393 (unless found
394 ;; No space is needed at the beginning of a line.
395 (when (eq (following-char) ? )
396 (delete-char 1)))
397 (when (> shr-indentation 0)
398 (shr-indent))
399 (end-of-line))
400 (insert " ")))
401 (unless (string-match "[ \t\r\n ]\\'" text)
402 (delete-char -1)))))
404 (defun shr-find-fill-point ()
405 (when (> (move-to-column shr-width) shr-width)
406 (backward-char 1))
407 (let ((bp (point))
408 failed)
409 (while (not (or (setq failed (= (current-column) shr-indentation))
410 (eq (preceding-char) ? )
411 (eq (following-char) ? )
412 (shr-char-breakable-p (preceding-char))
413 (shr-char-breakable-p (following-char))
414 (if (eq (preceding-char) ?')
415 (not (memq (char-after (- (point) 2))
416 (list nil ?\n ? )))
417 (and (shr-char-kinsoku-bol-p (preceding-char))
418 (shr-char-breakable-p (following-char))
419 (not (shr-char-kinsoku-bol-p (following-char)))))
420 (shr-char-kinsoku-eol-p (following-char))))
421 (backward-char 1))
422 (if (and (not (or failed (eolp)))
423 (eq (preceding-char) ?'))
424 (while (not (or (setq failed (eolp))
425 (eq (following-char) ? )
426 (shr-char-breakable-p (following-char))
427 (shr-char-kinsoku-eol-p (following-char))))
428 (forward-char 1)))
429 (if failed
430 ;; There's no breakable point, so we give it up.
431 (let (found)
432 (goto-char bp)
433 (unless shr-kinsoku-shorten
434 (while (and (setq found (re-search-forward
435 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
436 (line-end-position) 'move))
437 (eq (preceding-char) ?')))
438 (if (and found (not (match-beginning 1)))
439 (goto-char (match-beginning 0)))))
441 (eolp)
442 ;; Don't put kinsoku-bol characters at the beginning of a line,
443 ;; or kinsoku-eol characters at the end of a line.
444 (cond
445 (shr-kinsoku-shorten
446 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
447 (shr-char-kinsoku-eol-p (preceding-char)))
448 (backward-char 1))
449 (when (setq failed (= (current-column) shr-indentation))
450 ;; There's no breakable point that doesn't violate kinsoku,
451 ;; so we look for the second best position.
452 (while (and (progn
453 (forward-char 1)
454 (<= (current-column) shr-width))
455 (progn
456 (setq bp (point))
457 (shr-char-kinsoku-eol-p (following-char)))))
458 (goto-char bp)))
459 ((shr-char-kinsoku-eol-p (preceding-char))
460 ;; Find backward the point where kinsoku-eol characters begin.
461 (let ((count 4))
462 (while
463 (progn
464 (backward-char 1)
465 (and (> (setq count (1- count)) 0)
466 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
467 (or (shr-char-kinsoku-eol-p (preceding-char))
468 (shr-char-kinsoku-bol-p (following-char)))))))
469 (if (setq failed (= (current-column) shr-indentation))
470 ;; There's no breakable point that doesn't violate kinsoku,
471 ;; so we go to the second best position.
472 (if (looking-at "\\(\\c<+\\)\\c<")
473 (goto-char (match-end 1))
474 (forward-char 1))))
475 ((shr-char-kinsoku-bol-p (following-char))
476 ;; Find forward the point where kinsoku-bol characters end.
477 (let ((count 4))
478 (while (progn
479 (forward-char 1)
480 (and (>= (setq count (1- count)) 0)
481 (shr-char-kinsoku-bol-p (following-char))
482 (shr-char-breakable-p (following-char))))))))
483 (when (eq (following-char) ? )
484 (forward-char 1))))
485 (not failed)))
487 (defun shr-expand-url (url)
488 (if (or (not url)
489 (string-match "\\`[a-z]*:" url)
490 (not shr-base))
491 ;; Absolute URL.
493 (let ((base shr-base))
494 ;; Chop off query string.
495 (when (string-match "^\\([^?]+\\)[?]" base)
496 (setq base (match-string 1 base)))
497 (cond
498 ((and (string-match "\\`//" url)
499 (string-match "\\`[a-z]*:" base))
500 (concat (match-string 0 base) url))
501 ((and (not (string-match "/\\'" base))
502 (not (string-match "\\`/" url)))
503 (concat base "/" url))
504 ((and (string-match "\\`/" url)
505 (string-match "\\(\\`[^:]*://[^/]+\\)/" base))
506 (concat (match-string 1 base) url))
508 (concat base url))))))
510 (defun shr-ensure-newline ()
511 (unless (zerop (current-column))
512 (insert "\n")))
514 (defun shr-ensure-paragraph ()
515 (unless (bobp)
516 (if (<= (current-column) shr-indentation)
517 (unless (save-excursion
518 (forward-line -1)
519 (looking-at " *$"))
520 (insert "\n"))
521 (if (save-excursion
522 (beginning-of-line)
523 (looking-at " *$"))
524 (delete-region (match-beginning 0) (match-end 0))
525 (insert "\n\n")))))
527 (defun shr-indent ()
528 (when (> shr-indentation 0)
529 (insert (make-string shr-indentation ? ))))
531 (defun shr-fontize-cont (cont &rest types)
532 (let (shr-start)
533 (shr-generic cont)
534 (dolist (type types)
535 (shr-add-font (or shr-start (point)) (point) type))))
537 (defun shr-make-overlay (beg end &optional buffer front-advance rear-advance)
538 (let ((overlay (make-overlay beg end buffer front-advance rear-advance)))
539 (overlay-put overlay 'evaporate t)
540 overlay))
542 ;; Add an overlay in the region, but avoid putting the font properties
543 ;; on blank text at the start of the line, and the newline at the end,
544 ;; to avoid ugliness.
545 (defun shr-add-font (start end type)
546 (save-excursion
547 (goto-char start)
548 (while (< (point) end)
549 (when (bolp)
550 (skip-chars-forward " "))
551 (let ((overlay (shr-make-overlay (point) (min (line-end-position) end))))
552 (overlay-put overlay 'face type))
553 (if (< (line-end-position) end)
554 (forward-line 1)
555 (goto-char end)))))
557 (defun shr-browse-url ()
558 "Browse the URL under point."
559 (interactive)
560 (let ((url (get-text-property (point) 'shr-url)))
561 (cond
562 ((not url)
563 (message "No link under point"))
564 ((string-match "^mailto:" url)
565 (browse-url-mail url))
567 (browse-url url)))))
569 (defun shr-save-contents (directory)
570 "Save the contents from URL in a file."
571 (interactive "DSave contents of URL to directory: ")
572 (let ((url (get-text-property (point) 'shr-url)))
573 (if (not url)
574 (message "No link under point")
575 (url-retrieve (shr-encode-url url)
576 'shr-store-contents (list url directory)
577 nil t))))
579 (defun shr-store-contents (status url directory)
580 (unless (plist-get status :error)
581 (when (or (search-forward "\n\n" nil t)
582 (search-forward "\r\n\r\n" nil t))
583 (write-region (point) (point-max)
584 (expand-file-name (file-name-nondirectory url)
585 directory)))))
587 (defun shr-image-fetched (status buffer start end &optional flags)
588 (let ((image-buffer (current-buffer)))
589 (when (and (buffer-name buffer)
590 (not (plist-get status :error)))
591 (url-store-in-cache image-buffer)
592 (when (or (search-forward "\n\n" nil t)
593 (search-forward "\r\n\r\n" nil t))
594 (let ((data (buffer-substring (point) (point-max))))
595 (with-current-buffer buffer
596 (save-excursion
597 (let ((alt (buffer-substring start end))
598 (properties (text-properties-at start))
599 (inhibit-read-only t))
600 (delete-region start end)
601 (goto-char start)
602 (funcall shr-put-image-function data alt flags)
603 (while properties
604 (let ((type (pop properties))
605 (value (pop properties)))
606 (unless (memq type '(display image-size))
607 (put-text-property start (point) type value))))))))))
608 (kill-buffer image-buffer)))
610 (defun shr-image-from-data (data)
611 "Return an image from the data: URI content DATA."
612 (when (string-match
613 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
614 data)
615 (let ((param (match-string 4 data))
616 (payload (url-unhex-string (match-string 5 data))))
617 (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
618 (setq payload (base64-decode-string payload)))
619 payload)))
621 (defun shr-put-image (data alt &optional flags)
622 "Put image DATA with a string ALT. Return image."
623 (if (display-graphic-p)
624 (let* ((size (cdr (assq 'size flags)))
625 (start (point))
626 (image (cond
627 ((eq size 'original)
628 (create-image data nil t :ascent 100))
629 ((eq size 'full)
630 (ignore-errors
631 (shr-rescale-image data t)))
633 (ignore-errors
634 (shr-rescale-image data))))))
635 (when image
636 ;; When inserting big-ish pictures, put them at the
637 ;; beginning of the line.
638 (when (and (> (current-column) 0)
639 (> (car (image-size image t)) 400))
640 (insert "\n"))
641 (if (eq size 'original)
642 (let ((overlays (overlays-at (point))))
643 (insert-sliced-image image (or alt "*") nil 20 1)
644 (dolist (overlay overlays)
645 (overlay-put overlay 'face 'default)))
646 (insert-image image (or alt "*")))
647 (put-text-property start (point) 'image-size size)
648 (when (cond ((fboundp 'image-multi-frame-p)
649 ;; Only animate multi-frame things that specify a
650 ;; delay; eg animated gifs as opposed to
651 ;; multi-page tiffs. FIXME?
652 (cdr (image-multi-frame-p image)))
653 ((fboundp 'image-animated-p)
654 (image-animated-p image)))
655 (image-animate image nil 60)))
656 image)
657 (insert alt)))
659 (defun shr-rescale-image (data &optional force)
660 "Rescale DATA, if too big, to fit the current buffer.
661 If FORCE, rescale the image anyway."
662 (let ((image (create-image data nil t :ascent 100)))
663 (if (or (not (fboundp 'imagemagick-types))
664 (not (get-buffer-window (current-buffer))))
665 image
666 (let* ((size (image-size image t))
667 (width (car size))
668 (height (cdr size))
669 (edges (window-inside-pixel-edges
670 (get-buffer-window (current-buffer))))
671 (window-width (truncate (* shr-max-image-proportion
672 (- (nth 2 edges) (nth 0 edges)))))
673 (window-height (truncate (* shr-max-image-proportion
674 (- (nth 3 edges) (nth 1 edges)))))
675 scaled-image)
676 (when (or force
677 (> height window-height))
678 (setq image (or (create-image data 'imagemagick t
679 :height window-height
680 :ascent 100)
681 image))
682 (setq size (image-size image t)))
683 (when (> (car size) window-width)
684 (setq image (or
685 (create-image data 'imagemagick t
686 :width window-width
687 :ascent 100)
688 image)))
689 image))))
691 ;; url-cache-extract autoloads url-cache.
692 (declare-function url-cache-create-filename "url-cache" (url))
693 (autoload 'mm-disable-multibyte "mm-util")
694 (autoload 'browse-url-mail "browse-url")
696 (defun shr-get-image-data (url)
697 "Get image data for URL.
698 Return a string with image data."
699 (with-temp-buffer
700 (mm-disable-multibyte)
701 (when (ignore-errors
702 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
704 (when (or (search-forward "\n\n" nil t)
705 (search-forward "\r\n\r\n" nil t))
706 (buffer-substring (point) (point-max))))))
708 (defun shr-image-displayer (content-function)
709 "Return a function to display an image.
710 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
711 is an argument. The function to be returned takes three arguments URL,
712 START, and END. Note that START and END should be markers."
713 `(lambda (url start end)
714 (when url
715 (if (string-match "\\`cid:" url)
716 ,(when content-function
717 `(let ((image (funcall ,content-function
718 (substring url (match-end 0)))))
719 (when image
720 (goto-char start)
721 (funcall shr-put-image-function
722 image (buffer-substring start end))
723 (delete-region (point) end))))
724 (url-retrieve url 'shr-image-fetched
725 (list (current-buffer) start end)
726 t t)))))
728 (defun shr-heading (cont &rest types)
729 (shr-ensure-paragraph)
730 (apply #'shr-fontize-cont cont types)
731 (shr-ensure-paragraph))
733 (autoload 'widget-convert-button "wid-edit")
735 (defun shr-urlify (start url &optional title)
736 (widget-convert-button
737 'url-link start (point)
738 :help-echo (if title (format "%s (%s)" url title) url)
739 :keymap shr-map
740 url)
741 (shr-add-font start (point) 'shr-link)
742 (put-text-property start (point) 'shr-url url))
744 (defun shr-encode-url (url)
745 "Encode URL."
746 (browse-url-url-encode-chars url "[)$ ]"))
748 (autoload 'shr-color-visible "shr-color")
749 (autoload 'shr-color->hexadecimal "shr-color")
751 (defun shr-color-check (fg bg)
752 "Check that FG is visible on BG.
753 Returns (fg bg) with corrected values.
754 Returns nil if the colors that would be used are the default
755 ones, in case fg and bg are nil."
756 (when (or fg bg)
757 (let ((fixed (cond ((null fg) 'fg)
758 ((null bg) 'bg))))
759 ;; Convert colors to hexadecimal, or set them to default.
760 (let ((fg (or (shr-color->hexadecimal fg)
761 (frame-parameter nil 'foreground-color)))
762 (bg (or (shr-color->hexadecimal bg)
763 (frame-parameter nil 'background-color))))
764 (cond ((eq fixed 'bg)
765 ;; Only return the new fg
766 (list nil (cadr (shr-color-visible bg fg t))))
767 ((eq fixed 'fg)
768 ;; Invert args and results and return only the new bg
769 (list (cadr (shr-color-visible fg bg t)) nil))
771 (shr-color-visible bg fg)))))))
773 (defun shr-colorize-region (start end fg &optional bg)
774 (when (or fg bg)
775 (let ((new-colors (shr-color-check fg bg)))
776 (when new-colors
777 (when fg
778 (shr-put-color start end :foreground (cadr new-colors)))
779 (when bg
780 (shr-put-color start end :background (car new-colors))))
781 new-colors)))
783 ;; Put a color in the region, but avoid putting colors on blank
784 ;; text at the start of the line, and the newline at the end, to avoid
785 ;; ugliness. Also, don't overwrite any existing color information,
786 ;; since this can be called recursively, and we want the "inner" color
787 ;; to win.
788 (defun shr-put-color (start end type color)
789 (save-excursion
790 (goto-char start)
791 (while (< (point) end)
792 (when (and (bolp)
793 (not (eq type :background)))
794 (skip-chars-forward " "))
795 (when (> (line-end-position) (point))
796 (shr-put-color-1 (point) (min (line-end-position) end) type color))
797 (if (< (line-end-position) end)
798 (forward-line 1)
799 (goto-char end)))
800 (when (and (eq type :background)
801 (= shr-table-depth 0))
802 (shr-expand-newlines start end color))))
804 (defun shr-expand-newlines (start end color)
805 (save-restriction
806 ;; Skip past all white space at the start and ends.
807 (goto-char start)
808 (skip-chars-forward " \t\n")
809 (beginning-of-line)
810 (setq start (point))
811 (goto-char end)
812 (skip-chars-backward " \t\n")
813 (forward-line 1)
814 (setq end (point))
815 (narrow-to-region start end)
816 (let ((width (shr-buffer-width))
817 column)
818 (goto-char (point-min))
819 (while (not (eobp))
820 (end-of-line)
821 (when (and (< (setq column (current-column)) width)
822 (< (setq column (shr-previous-newline-padding-width column))
823 width))
824 (let ((overlay (shr-make-overlay (point) (1+ (point)))))
825 (overlay-put overlay 'before-string
826 (concat
827 (mapconcat
828 (lambda (overlay)
829 (let ((string (plist-get
830 (overlay-properties overlay)
831 'before-string)))
832 (if (not string)
834 (overlay-put overlay 'before-string "")
835 string)))
836 (overlays-at (point))
838 (propertize (make-string (- width column) ? )
839 'face (list :background color))))))
840 (forward-line 1)))))
842 (defun shr-previous-newline-padding-width (width)
843 (let ((overlays (overlays-at (point)))
844 (previous-width 0))
845 (if (null overlays)
846 width
847 (dolist (overlay overlays)
848 (setq previous-width
849 (+ previous-width
850 (length (plist-get (overlay-properties overlay)
851 'before-string)))))
852 (+ width previous-width))))
854 (defun shr-put-color-1 (start end type color)
855 (let* ((old-props (get-text-property start 'face))
856 (do-put (and (listp old-props)
857 (not (memq type old-props))))
858 change)
859 (while (< start end)
860 (setq change (next-single-property-change start 'face nil end))
861 (when do-put
862 (put-text-property start change 'face
863 (nconc (list type color) old-props)))
864 (setq old-props (get-text-property change 'face))
865 (setq do-put (and (listp old-props)
866 (not (memq type old-props))))
867 (setq start change))
868 (when (and do-put
869 (> end start))
870 (put-text-property start end 'face
871 (nconc (list type color old-props))))))
873 ;;; Tag-specific rendering rules.
875 (defun shr-tag-body (cont)
876 (let* ((start (point))
877 (fgcolor (cdr (or (assq :fgcolor cont)
878 (assq :text cont))))
879 (bgcolor (cdr (assq :bgcolor cont)))
880 (shr-stylesheet (list (cons 'color fgcolor)
881 (cons 'background-color bgcolor))))
882 (shr-generic cont)
883 (shr-colorize-region start (point) fgcolor bgcolor)))
885 (defun shr-tag-style (cont)
888 (defun shr-tag-script (cont)
891 (defun shr-tag-comment (cont)
894 (defun shr-tag-sup (cont)
895 (let ((start (point)))
896 (shr-generic cont)
897 (put-text-property start (point) 'display '(raise 0.5))))
899 (defun shr-tag-sub (cont)
900 (let ((start (point)))
901 (shr-generic cont)
902 (put-text-property start (point) 'display '(raise -0.5))))
904 (defun shr-tag-label (cont)
905 (shr-generic cont)
906 (shr-ensure-paragraph))
908 (defun shr-tag-p (cont)
909 (shr-ensure-paragraph)
910 (shr-indent)
911 (shr-generic cont)
912 (shr-ensure-paragraph))
914 (defun shr-tag-div (cont)
915 (shr-ensure-newline)
916 (shr-indent)
917 (shr-generic cont)
918 (shr-ensure-newline))
920 (defun shr-tag-s (cont)
921 (shr-fontize-cont cont 'shr-strike-through))
923 (defun shr-tag-del (cont)
924 (shr-fontize-cont cont 'shr-strike-through))
926 (defun shr-tag-b (cont)
927 (shr-fontize-cont cont 'bold))
929 (defun shr-tag-i (cont)
930 (shr-fontize-cont cont 'italic))
932 (defun shr-tag-em (cont)
933 (shr-fontize-cont cont 'italic))
935 (defun shr-tag-strong (cont)
936 (shr-fontize-cont cont 'bold))
938 (defun shr-tag-u (cont)
939 (shr-fontize-cont cont 'underline))
941 (defun shr-parse-style (style)
942 (when style
943 (save-match-data
944 (when (string-match "\n" style)
945 (setq style (replace-match " " t t style))))
946 (let ((plist nil))
947 (dolist (elem (split-string style ";"))
948 (when elem
949 (setq elem (split-string elem ":"))
950 (when (and (car elem)
951 (cadr elem))
952 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
953 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
954 (when (string-match " *!important\\'" value)
955 (setq value (substring value 0 (match-beginning 0))))
956 (push (cons (intern name obarray)
957 value)
958 plist)))))
959 plist)))
961 (defun shr-tag-base (cont)
962 (setq shr-base (cdr (assq :href cont)))
963 (shr-generic cont))
965 (defun shr-tag-a (cont)
966 (let ((url (cdr (assq :href cont)))
967 (title (cdr (assq :title cont)))
968 (start (point))
969 shr-start)
970 (shr-generic cont)
971 (when url
972 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
974 (defun shr-tag-object (cont)
975 (let ((start (point))
976 url)
977 (dolist (elem cont)
978 (when (eq (car elem) 'embed)
979 (setq url (or url (cdr (assq :src (cdr elem))))))
980 (when (and (eq (car elem) 'param)
981 (equal (cdr (assq :name (cdr elem))) "movie"))
982 (setq url (or url (cdr (assq :value (cdr elem)))))))
983 (when url
984 (shr-insert " [multimedia] ")
985 (shr-urlify start (shr-expand-url url)))
986 (shr-generic cont)))
988 (defun shr-tag-video (cont)
989 (let ((image (cdr (assq :poster cont)))
990 (url (cdr (assq :src cont)))
991 (start (point)))
992 (shr-tag-img nil image)
993 (shr-urlify start (shr-expand-url url))))
995 (defun shr-tag-img (cont &optional url)
996 (when (or url
997 (and cont
998 (cdr (assq :src cont))))
999 (when (and (> (current-column) 0)
1000 (not (eq shr-state 'image)))
1001 (insert "\n"))
1002 (let ((alt (cdr (assq :alt cont)))
1003 (url (shr-expand-url (or url (cdr (assq :src cont))))))
1004 (let ((start (point-marker)))
1005 (when (zerop (length alt))
1006 (setq alt "*"))
1007 (cond
1008 ((or (member (cdr (assq :height cont)) '("0" "1"))
1009 (member (cdr (assq :width cont)) '("0" "1")))
1010 ;; Ignore zero-sized or single-pixel images.
1012 ((and (not shr-inhibit-images)
1013 (string-match "\\`data:" url))
1014 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1015 (if image
1016 (funcall shr-put-image-function image alt)
1017 (insert alt))))
1018 ((and (not shr-inhibit-images)
1019 (string-match "\\`cid:" url))
1020 (let ((url (substring url (match-end 0)))
1021 image)
1022 (if (or (not shr-content-function)
1023 (not (setq image (funcall shr-content-function url))))
1024 (insert alt)
1025 (funcall shr-put-image-function image alt))))
1026 ((or shr-inhibit-images
1027 (and shr-blocked-images
1028 (string-match shr-blocked-images url)))
1029 (setq shr-start (point))
1030 (let ((shr-state 'space))
1031 (if (> (string-width alt) 8)
1032 (shr-insert (truncate-string-to-width alt 8))
1033 (shr-insert alt))))
1034 ((and (not shr-ignore-cache)
1035 (url-is-cached (shr-encode-url url)))
1036 (funcall shr-put-image-function (shr-get-image-data url) alt))
1038 (insert alt " ")
1039 (when (and shr-ignore-cache
1040 (url-is-cached (shr-encode-url url)))
1041 (let ((file (url-cache-create-filename (shr-encode-url url))))
1042 (when (file-exists-p file)
1043 (delete-file file))))
1044 (url-queue-retrieve
1045 (shr-encode-url url) 'shr-image-fetched
1046 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1047 t t)))
1048 (when (zerop shr-table-depth) ;; We are not in a table.
1049 (put-text-property start (point) 'keymap shr-map)
1050 (put-text-property start (point) 'shr-alt alt)
1051 (put-text-property start (point) 'image-url url)
1052 (put-text-property start (point) 'image-displayer
1053 (shr-image-displayer shr-content-function))
1054 (put-text-property start (point) 'help-echo alt))
1055 (setq shr-state 'image)))))
1057 (defun shr-tag-pre (cont)
1058 (let ((shr-folding-mode 'none))
1059 (shr-ensure-newline)
1060 (shr-indent)
1061 (shr-generic cont)
1062 (shr-ensure-newline)))
1064 (defun shr-tag-blockquote (cont)
1065 (shr-ensure-paragraph)
1066 (shr-indent)
1067 (let ((shr-indentation (+ shr-indentation 4)))
1068 (shr-generic cont))
1069 (shr-ensure-paragraph))
1071 (defun shr-tag-ul (cont)
1072 (shr-ensure-paragraph)
1073 (let ((shr-list-mode 'ul))
1074 (shr-generic cont))
1075 (shr-ensure-paragraph))
1077 (defun shr-tag-ol (cont)
1078 (shr-ensure-paragraph)
1079 (let ((shr-list-mode 1))
1080 (shr-generic cont))
1081 (shr-ensure-paragraph))
1083 (defun shr-tag-li (cont)
1084 (shr-ensure-paragraph)
1085 (shr-indent)
1086 (let* ((bullet
1087 (if (numberp shr-list-mode)
1088 (prog1
1089 (format "%d " shr-list-mode)
1090 (setq shr-list-mode (1+ shr-list-mode)))
1091 "* "))
1092 (shr-indentation (+ shr-indentation (length bullet))))
1093 (insert bullet)
1094 (shr-generic cont)))
1096 (defun shr-tag-br (cont)
1097 (when (and (not (bobp))
1098 ;; Only add a newline if we break the current line, or
1099 ;; the previous line isn't a blank line.
1100 (or (not (bolp))
1101 (and (> (- (point) 2) (point-min))
1102 (not (= (char-after (- (point) 2)) ?\n)))))
1103 (insert "\n")
1104 (shr-indent))
1105 (shr-generic cont))
1107 (defun shr-tag-span (cont)
1108 (let ((title (cdr (assq :title cont))))
1109 (shr-generic cont)
1110 (when title
1111 (when shr-start
1112 (let ((overlay (shr-make-overlay shr-start (point))))
1113 (overlay-put overlay 'help-echo title))))))
1115 (defun shr-tag-h1 (cont)
1116 (shr-heading cont 'bold 'underline))
1118 (defun shr-tag-h2 (cont)
1119 (shr-heading cont 'bold))
1121 (defun shr-tag-h3 (cont)
1122 (shr-heading cont 'italic))
1124 (defun shr-tag-h4 (cont)
1125 (shr-heading cont))
1127 (defun shr-tag-h5 (cont)
1128 (shr-heading cont))
1130 (defun shr-tag-h6 (cont)
1131 (shr-heading cont))
1133 (defun shr-tag-hr (cont)
1134 (shr-ensure-newline)
1135 (insert (make-string shr-width shr-hr-line) "\n"))
1137 (defun shr-tag-title (cont)
1138 (shr-heading cont 'bold 'underline))
1140 (defun shr-tag-font (cont)
1141 (let* ((start (point))
1142 (color (cdr (assq :color cont)))
1143 (shr-stylesheet (nconc (list (cons 'color color))
1144 shr-stylesheet)))
1145 (shr-generic cont)
1146 (when color
1147 (shr-colorize-region start (point) color
1148 (cdr (assq 'background-color shr-stylesheet))))))
1150 ;;; Table rendering algorithm.
1152 ;; Table rendering is the only complicated thing here. We do this by
1153 ;; first counting how many TDs there are in each TR, and registering
1154 ;; how wide they think they should be ("width=45%", etc). Then we
1155 ;; render each TD separately (this is done in temporary buffers, so
1156 ;; that we can use all the rendering machinery as if we were in the
1157 ;; main buffer). Now we know how much space each TD really takes, so
1158 ;; we then render everything again with the new widths, and finally
1159 ;; insert all these boxes into the main buffer.
1160 (defun shr-tag-table-1 (cont)
1161 (setq cont (or (cdr (assq 'tbody cont))
1162 cont))
1163 (let* ((shr-inhibit-images t)
1164 (shr-table-depth (1+ shr-table-depth))
1165 (shr-kinsoku-shorten t)
1166 ;; Find all suggested widths.
1167 (columns (shr-column-specs cont))
1168 ;; Compute how many characters wide each TD should be.
1169 (suggested-widths (shr-pro-rate-columns columns))
1170 ;; Do a "test rendering" to see how big each TD is (this can
1171 ;; be smaller (if there's little text) or bigger (if there's
1172 ;; unbreakable text).
1173 (sketch (shr-make-table cont suggested-widths))
1174 ;; Compute the "natural" width by setting each column to 500
1175 ;; characters and see how wide they really render.
1176 (natural (shr-make-table cont (make-vector (length columns) 500)))
1177 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1178 ;; This probably won't work very well.
1179 (when (> (+ (loop for width across sketch-widths
1180 summing (1+ width))
1181 shr-indentation 1)
1182 (frame-width))
1183 (setq truncate-lines t))
1184 ;; Then render the table again with these new "hard" widths.
1185 (let ((shr-final-table-render t))
1186 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
1187 ;; Finally, insert all the images after the table. The Emacs buffer
1188 ;; model isn't strong enough to allow us to put the images actually
1189 ;; into the tables.
1190 (when (zerop shr-table-depth)
1191 (dolist (elem (shr-find-elements cont 'img))
1192 (shr-tag-img (cdr elem)))))
1194 (defun shr-tag-table (cont)
1195 (shr-ensure-paragraph)
1196 (let* ((caption (cdr (assq 'caption cont)))
1197 (header (cdr (assq 'thead cont)))
1198 (body (or (cdr (assq 'tbody cont)) cont))
1199 (footer (cdr (assq 'tfoot cont)))
1200 (bgcolor (cdr (assq :bgcolor cont)))
1201 (start (point))
1202 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1203 shr-stylesheet))
1204 (nheader (if header (shr-max-columns header)))
1205 (nbody (if body (shr-max-columns body)))
1206 (nfooter (if footer (shr-max-columns footer))))
1207 (if (and (not caption)
1208 (not header)
1209 (not (cdr (assq 'tbody cont)))
1210 (not (cdr (assq 'tr cont)))
1211 (not footer))
1212 ;; The table is totally invalid and just contains random junk.
1213 ;; Try to output it anyway.
1214 (shr-generic cont)
1215 ;; It's a real table, so render it.
1216 (shr-tag-table-1
1217 (nconc
1218 (if caption `((tr (td ,@caption))))
1219 (if header
1220 (if footer
1221 ;; hader + body + footer
1222 (if (= nheader nbody)
1223 (if (= nbody nfooter)
1224 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
1225 (nconc `((tr (td (table (tbody ,@header ,@body)))))
1226 (if (= nfooter 1)
1227 footer
1228 `((tr (td (table (tbody ,@footer))))))))
1229 (nconc `((tr (td (table (tbody ,@header)))))
1230 (if (= nbody nfooter)
1231 `((tr (td (table (tbody ,@body ,@footer)))))
1232 (nconc `((tr (td (table (tbody ,@body)))))
1233 (if (= nfooter 1)
1234 footer
1235 `((tr (td (table (tbody ,@footer))))))))))
1236 ;; header + body
1237 (if (= nheader nbody)
1238 `((tr (td (table (tbody ,@header ,@body)))))
1239 (if (= nheader 1)
1240 `(,@header (tr (td (table (tbody ,@body)))))
1241 `((tr (td (table (tbody ,@header))))
1242 (tr (td (table (tbody ,@body))))))))
1243 (if footer
1244 ;; body + footer
1245 (if (= nbody nfooter)
1246 `((tr (td (table (tbody ,@body ,@footer)))))
1247 (nconc `((tr (td (table (tbody ,@body)))))
1248 (if (= nfooter 1)
1249 footer
1250 `((tr (td (table (tbody ,@footer))))))))
1251 (if caption
1252 `((tr (td (table (tbody ,@body)))))
1253 body))))))
1254 (when bgcolor
1255 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1256 bgcolor))))
1258 (defun shr-find-elements (cont type)
1259 (let (result)
1260 (dolist (elem cont)
1261 (cond ((eq (car elem) type)
1262 (push elem result))
1263 ((consp (cdr elem))
1264 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1265 (nreverse result)))
1267 (defun shr-insert-table (table widths)
1268 (shr-insert-table-ruler widths)
1269 (dolist (row table)
1270 (let ((start (point))
1271 (height (let ((max 0))
1272 (dolist (column row)
1273 (setq max (max max (cadr column))))
1274 max)))
1275 (dotimes (i height)
1276 (shr-indent)
1277 (insert shr-table-vertical-line "\n"))
1278 (dolist (column row)
1279 (goto-char start)
1280 (let ((lines (nth 2 column))
1281 (overlay-lines (nth 3 column))
1282 overlay overlay-line)
1283 (dolist (line lines)
1284 (setq overlay-line (pop overlay-lines))
1285 (end-of-line)
1286 (insert line shr-table-vertical-line)
1287 (dolist (overlay overlay-line)
1288 (let ((o (shr-make-overlay (- (point) (nth 0 overlay) 1)
1289 (- (point) (nth 1 overlay) 1)))
1290 (properties (nth 2 overlay)))
1291 (while properties
1292 (overlay-put o (pop properties) (pop properties)))))
1293 (forward-line 1))
1294 ;; Add blank lines at padding at the bottom of the TD,
1295 ;; possibly.
1296 (dotimes (i (- height (length lines)))
1297 (end-of-line)
1298 (let ((start (point)))
1299 (insert (make-string (string-width (car lines)) ? )
1300 shr-table-vertical-line)
1301 (when (nth 4 column)
1302 (shr-put-color start (1- (point)) :background (nth 4 column))))
1303 (forward-line 1)))))
1304 (shr-insert-table-ruler widths)))
1306 (defun shr-insert-table-ruler (widths)
1307 (when (and (bolp)
1308 (> shr-indentation 0))
1309 (shr-indent))
1310 (insert shr-table-corner)
1311 (dotimes (i (length widths))
1312 (insert (make-string (aref widths i) shr-table-horizontal-line)
1313 shr-table-corner))
1314 (insert "\n"))
1316 (defun shr-table-widths (table natural-table suggested-widths)
1317 (let* ((length (length suggested-widths))
1318 (widths (make-vector length 0))
1319 (natural-widths (make-vector length 0)))
1320 (dolist (row table)
1321 (let ((i 0))
1322 (dolist (column row)
1323 (aset widths i (max (aref widths i) column))
1324 (setq i (1+ i)))))
1325 (dolist (row natural-table)
1326 (let ((i 0))
1327 (dolist (column row)
1328 (aset natural-widths i (max (aref natural-widths i) column))
1329 (setq i (1+ i)))))
1330 (let ((extra (- (apply '+ (append suggested-widths nil))
1331 (apply '+ (append widths nil))))
1332 (expanded-columns 0))
1333 ;; We have extra, unused space, so divide this space amongst the
1334 ;; columns.
1335 (when (> extra 0)
1336 ;; If the natural width is wider than the rendered width, we
1337 ;; want to allow the column to expand.
1338 (dotimes (i length)
1339 (when (> (aref natural-widths i) (aref widths i))
1340 (setq expanded-columns (1+ expanded-columns))))
1341 (dotimes (i length)
1342 (when (> (aref natural-widths i) (aref widths i))
1343 (aset widths i (min
1344 (aref natural-widths i)
1345 (+ (/ extra expanded-columns)
1346 (aref widths i))))))))
1347 widths))
1349 (defun shr-make-table (cont widths &optional fill)
1350 (let ((trs nil))
1351 (dolist (row cont)
1352 (when (eq (car row) 'tr)
1353 (let ((tds nil)
1354 (columns (cdr row))
1355 (i 0)
1356 column)
1357 (while (< i (length widths))
1358 (setq column (pop columns))
1359 (when (or (memq (car column) '(td th))
1360 (null column))
1361 (push (shr-render-td (cdr column) (aref widths i) fill)
1362 tds)
1363 (setq i (1+ i))))
1364 (push (nreverse tds) trs))))
1365 (nreverse trs)))
1367 (defun shr-render-td (cont width fill)
1368 (with-temp-buffer
1369 (let ((bgcolor (cdr (assq :bgcolor cont)))
1370 (fgcolor (cdr (assq :fgcolor cont)))
1371 (style (cdr (assq :style cont)))
1372 (shr-stylesheet shr-stylesheet)
1373 overlays actual-colors)
1374 (when style
1375 (setq style (and (string-match "color" style)
1376 (shr-parse-style style))))
1377 (when bgcolor
1378 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1379 (when fgcolor
1380 (setq style (nconc (list (cons 'color fgcolor)) style)))
1381 (when style
1382 (setq shr-stylesheet (append style shr-stylesheet)))
1383 (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
1384 (if cache
1385 (progn
1386 (insert (car cache))
1387 (let ((end (length (car cache))))
1388 (dolist (overlay (cadr cache))
1389 (let ((new-overlay
1390 (shr-make-overlay (1+ (- end (nth 0 overlay)))
1391 (1+ (- end (nth 1 overlay)))))
1392 (properties (nth 2 overlay)))
1393 (while properties
1394 (overlay-put new-overlay
1395 (pop properties) (pop properties)))))))
1396 (let ((shr-width width)
1397 (shr-indentation 0))
1398 (shr-descend (cons 'td cont)))
1399 ;; Delete padding at the bottom of the TDs.
1400 (delete-region
1401 (point)
1402 (progn
1403 (skip-chars-backward " \t\n")
1404 (end-of-line)
1405 (point)))
1406 (push (list (cons width cont) (buffer-string)
1407 (shr-overlays-in-region (point-min) (point-max)))
1408 shr-content-cache)))
1409 (goto-char (point-min))
1410 (let ((max 0))
1411 (while (not (eobp))
1412 (end-of-line)
1413 (setq max (max max (current-column)))
1414 (forward-line 1))
1415 (when fill
1416 (goto-char (point-min))
1417 ;; If the buffer is totally empty, then put a single blank
1418 ;; line here.
1419 (if (zerop (buffer-size))
1420 (insert (make-string width ? ))
1421 ;; Otherwise, fill the buffer.
1422 (while (not (eobp))
1423 (end-of-line)
1424 (when (> (- width (current-column)) 0)
1425 (insert (make-string (- width (current-column)) ? )))
1426 (forward-line 1)))
1427 (when style
1428 (setq actual-colors
1429 (shr-colorize-region
1430 (point-min) (point-max)
1431 (cdr (assq 'color shr-stylesheet))
1432 (cdr (assq 'background-color shr-stylesheet))))))
1433 (if fill
1434 (list max
1435 (count-lines (point-min) (point-max))
1436 (split-string (buffer-string) "\n")
1437 (shr-collect-overlays)
1438 (car actual-colors))
1439 max)))))
1441 (defun shr-buffer-width ()
1442 (goto-char (point-min))
1443 (let ((max 0))
1444 (while (not (eobp))
1445 (end-of-line)
1446 (setq max (max max (current-column)))
1447 (forward-line 1))
1448 max))
1450 (defun shr-collect-overlays ()
1451 (save-excursion
1452 (goto-char (point-min))
1453 (let ((overlays nil))
1454 (while (not (eobp))
1455 (push (shr-overlays-in-region (point) (line-end-position))
1456 overlays)
1457 (forward-line 1))
1458 (nreverse overlays))))
1460 (defun shr-overlays-in-region (start end)
1461 (let (result)
1462 (dolist (overlay (overlays-in start end))
1463 (push (list (if (> start (overlay-start overlay))
1464 (- end start)
1465 (- end (overlay-start overlay)))
1466 (if (< end (overlay-end overlay))
1468 (- end (overlay-end overlay)))
1469 (overlay-properties overlay))
1470 result))
1471 (nreverse result)))
1473 (defun shr-pro-rate-columns (columns)
1474 (let ((total-percentage 0)
1475 (widths (make-vector (length columns) 0)))
1476 (dotimes (i (length columns))
1477 (setq total-percentage (+ total-percentage (aref columns i))))
1478 (setq total-percentage (/ 1.0 total-percentage))
1479 (dotimes (i (length columns))
1480 (aset widths i (max (truncate (* (aref columns i)
1481 total-percentage
1482 (- shr-width (1+ (length columns)))))
1483 10)))
1484 widths))
1486 ;; Return a summary of the number and shape of the TDs in the table.
1487 (defun shr-column-specs (cont)
1488 (let ((columns (make-vector (shr-max-columns cont) 1)))
1489 (dolist (row cont)
1490 (when (eq (car row) 'tr)
1491 (let ((i 0))
1492 (dolist (column (cdr row))
1493 (when (memq (car column) '(td th))
1494 (let ((width (cdr (assq :width (cdr column)))))
1495 (when (and width
1496 (string-match "\\([0-9]+\\)%" width)
1497 (not (zerop (setq width (string-to-number
1498 (match-string 1 width))))))
1499 (aset columns i (/ width 100.0))))
1500 (setq i (1+ i)))))))
1501 columns))
1503 (defun shr-count (cont elem)
1504 (let ((i 0))
1505 (dolist (sub cont)
1506 (when (eq (car sub) elem)
1507 (setq i (1+ i))))
1510 (defun shr-max-columns (cont)
1511 (let ((max 0))
1512 (dolist (row cont)
1513 (when (eq (car row) 'tr)
1514 (setq max (max max (+ (shr-count (cdr row) 'td)
1515 (shr-count (cdr row) 'th))))))
1516 max))
1518 (provide 'shr)
1520 ;; Local Variables:
1521 ;; coding: utf-8
1522 ;; End:
1524 ;;; shr.el ends here