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