Merge changes made in Gnus trunk.
[bpt/emacs.git] / lisp / gnus / shr.el
CommitLineData
367f7f81
LMI
1;;; shr.el --- Simple HTML Renderer
2
bb7f5cbc 3;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
367f7f81
LMI
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; Keywords: html
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;; This package takes a HTML parse tree (as provided by
26;; libxml-parse-html-region) and renders it in the current buffer. It
27;; does not do CSS, JavaScript or anything advanced: It's geared
28;; towards rendering typical short snippets of HTML, like what you'd
29;; find in HTML email and the like.
30
31;;; Code:
32
3d319c8f 33(eval-when-compile (require 'cl))
71e691a5
G
34(require 'browse-url)
35
870409d4
G
36(defgroup shr nil
37 "Simple HTML Renderer"
38 :group 'mail)
39
40(defcustom shr-max-image-proportion 0.9
41 "How big pictures displayed are in relation to the window they're in.
42A value of 0.7 means that they are allowed to take up 70% of the
43width and height of the window. If they are larger than this,
44and Emacs supports it, then the images will be rescaled down to
45fit these criteria."
46 :version "24.1"
47 :group 'shr
48 :type 'float)
49
50(defcustom shr-blocked-images nil
51 "Images that have URLs matching this regexp will be blocked."
52 :version "24.1"
53 :group 'shr
54 :type 'regexp)
55
d3098750
LMI
56(defcustom shr-table-horizontal-line ?-
57 "Character used to draw horizontal table lines."
58 :group 'shr
59 :type 'character)
60
61(defcustom shr-table-vertical-line ?|
62 "Character used to draw vertical table lines."
afba0c4b 63 :group 'shr
030158f3 64 :type 'character)
afba0c4b
JD
65
66(defcustom shr-table-corner ?+
d3098750 67 "Character used to draw table corners."
6b7df8d3 68 :group 'shr
030158f3 69 :type 'character)
6b7df8d3
G
70
71(defcustom shr-hr-line ?-
d3098750 72 "Character used to draw hr lines."
afba0c4b 73 :group 'shr
030158f3 74 :type 'character)
afba0c4b 75
d0e0de31 76(defcustom shr-width fill-column
bb7f5cbc
G
77 "Frame width to use for rendering.
78May either be an integer specifying a fixed width in characters,
79or nil, meaning that the full width of the window should be
80used."
81 :type '(choice (integer :tag "Fixed width in characters")
82 (const :tag "Use the width of the window" nil))
d0e0de31
JD
83 :group 'shr)
84
c5ecc769
G
85(defface shr-tag-h1 '((t (:bold t :height 2.2)))
86 "Face used for H1 tags."
87 :group 'shr)
88
89(defface shr-tag-h2 '((t (:bold t :height 2.0)))
90 "Face used for H2 tags."
91 :group 'shr)
92
93(defface shr-tag-h3 '((t (:bold t :height 1.8)))
94 "Face used for H3 tags."
95 :group 'shr)
96
97(defface shr-tag-h4 '((t (:bold t :height 1.6)))
98 "Face used for H4 tags."
99 :group 'shr)
100
101(defface shr-tag-h5 '((t (:bold t :height 1.4)))
102 "Face used for H5 tags."
103 :group 'shr)
104
105(defface shr-tag-h6 '((t (:bold t :height 1.2)))
106 "Face used for H6 tags."
107 :group 'shr)
108
130e977f
LMI
109(defvar shr-content-function nil
110 "If bound, this should be a function that will return the content.
111This is used for cid: URLs, and the function is called with the
112cid: URL as the argument.")
113
66627fa9
G
114;;; Internal variables.
115
870409d4
G
116(defvar shr-folding-mode nil)
117(defvar shr-state nil)
118(defvar shr-start nil)
a41c2e6d 119(defvar shr-indentation 0)
130e977f 120(defvar shr-inhibit-images nil)
66627fa9 121(defvar shr-list-mode nil)
3d319c8f 122(defvar shr-content-cache nil)
83ffd571 123(defvar shr-kinsoku-shorten nil)
99e65b2d 124(defvar shr-table-depth 0)
04db63bc 125(defvar shr-stylesheet nil)
870409d4 126
71e691a5
G
127(defvar shr-map
128 (let ((map (make-sparse-keymap)))
129 (define-key map "a" 'shr-show-alt-text)
130 (define-key map "i" 'shr-browse-image)
131 (define-key map "I" 'shr-insert-image)
132 (define-key map "u" 'shr-copy-url)
133 (define-key map "v" 'shr-browse-url)
cdf1fca4 134 (define-key map "o" 'shr-save-contents)
71e691a5
G
135 (define-key map "\r" 'shr-browse-url)
136 map))
137
66627fa9
G
138;; Public functions and commands.
139
140;;;###autoload
141(defun shr-insert-document (dom)
3d319c8f 142 (setq shr-content-cache nil)
66627fa9 143 (let ((shr-state nil)
bb7f5cbc
G
144 (shr-start nil)
145 (shr-width (or shr-width (window-width))))
66627fa9
G
146 (shr-descend (shr-transform-dom dom))))
147
148(defun shr-copy-url ()
149 "Copy the URL under point to the kill ring.
150If called twice, then try to fetch the URL and see whether it
151redirects somewhere else."
152 (interactive)
153 (let ((url (get-text-property (point) 'shr-url)))
154 (cond
155 ((not url)
156 (message "No URL under point"))
157 ;; Resolve redirected URLs.
158 ((equal url (car kill-ring))
159 (url-retrieve
160 url
161 (lambda (a)
162 (when (and (consp a)
163 (eq (car a) :redirect))
164 (with-temp-buffer
165 (insert (cadr a))
166 (goto-char (point-min))
167 ;; Remove common tracking junk from the URL.
168 (when (re-search-forward ".utm_.*" nil t)
169 (replace-match "" t t))
170 (message "Copied %s" (buffer-string))
171 (copy-region-as-kill (point-min) (point-max)))))))
172 ;; Copy the URL to the kill ring.
173 (t
174 (with-temp-buffer
175 (insert url)
176 (copy-region-as-kill (point-min) (point-max))
177 (message "Copied %s" url))))))
178
179(defun shr-show-alt-text ()
180 "Show the ALT text of the image under point."
181 (interactive)
182 (let ((text (get-text-property (point) 'shr-alt)))
183 (if (not text)
184 (message "No image under point")
185 (message "%s" text))))
186
187(defun shr-browse-image ()
188 "Browse the image under point."
189 (interactive)
8b6f6573 190 (let ((url (get-text-property (point) 'image-url)))
66627fa9
G
191 (if (not url)
192 (message "No image under point")
193 (message "Browsing %s..." url)
194 (browse-url url))))
195
3d319c8f
LMI
196(defun shr-insert-image ()
197 "Insert the image under point into the buffer."
198 (interactive)
8b6f6573 199 (let ((url (get-text-property (point) 'image-url)))
3d319c8f
LMI
200 (if (not url)
201 (message "No image under point")
202 (message "Inserting %s..." url)
203 (url-retrieve url 'shr-image-fetched
204 (list (current-buffer) (1- (point)) (point-marker))
205 t))))
206
66627fa9
G
207;;; Utility functions.
208
870409d4
G
209(defun shr-transform-dom (dom)
210 (let ((result (list (pop dom))))
211 (dolist (arg (pop dom))
212 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
213 (cdr arg))
214 result))
215 (dolist (sub dom)
216 (if (stringp sub)
953d41c4 217 (push (cons 'text sub) result)
870409d4
G
218 (push (shr-transform-dom sub) result)))
219 (nreverse result)))
220
870409d4 221(defun shr-descend (dom)
ebe79557
LMI
222 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray))
223 (style (cdr (assq :style (cdr dom))))
04db63bc 224 (shr-stylesheet shr-stylesheet)
ebe79557 225 (start (point)))
b31b26b4
G
226 (when style
227 (if (string-match "color" style)
228 (setq shr-stylesheet (nconc (shr-parse-style style)
229 shr-stylesheet))
230 (setq style nil)))
870409d4
G
231 (if (fboundp function)
232 (funcall function (cdr dom))
ebe79557 233 (shr-generic (cdr dom)))
b31b26b4
G
234 ;; If style is set, then this node has set the color.
235 (when style
236 (shr-colorize-region start (point)
237 (cdr (assq 'color shr-stylesheet))
238 (cdr (assq 'background-color shr-stylesheet))))))
870409d4
G
239
240(defun shr-generic (cont)
241 (dolist (sub cont)
242 (cond
953d41c4 243 ((eq (car sub) 'text)
870409d4 244 (shr-insert (cdr sub)))
a41c2e6d 245 ((listp (cdr sub))
870409d4
G
246 (shr-descend sub)))))
247
ed797193
G
248(defmacro shr-char-breakable-p (char)
249 "Return non-nil if a line can be broken before and after CHAR."
250 `(aref fill-find-break-point-function-table ,char))
251(defmacro shr-char-nospace-p (char)
252 "Return non-nil if no space is required before and after CHAR."
253 `(aref fill-nospace-between-words-table ,char))
254
255;; KINSOKU is a Japanese word meaning a rule that should not be violated.
256;; In Emacs, it is a term used for characters, e.g. punctuation marks,
257;; parentheses, and so on, that should not be placed in the beginning
258;; of a line or the end of a line.
259(defmacro shr-char-kinsoku-bol-p (char)
260 "Return non-nil if a line ought not to begin with CHAR."
261 `(aref (char-category-set ,char) ?>))
262(defmacro shr-char-kinsoku-eol-p (char)
263 "Return non-nil if a line ought not to end with CHAR."
264 `(aref (char-category-set ,char) ?<))
265(unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
266 (load "kinsoku" nil t))
267
66627fa9 268(defun shr-insert (text)
6b7df8d3
G
269 (when (and (eq shr-state 'image)
270 (not (string-match "\\`[ \t\n]+\\'" text)))
66627fa9
G
271 (insert "\n")
272 (setq shr-state nil))
273 (cond
274 ((eq shr-folding-mode 'none)
275 (insert text))
276 (t
73db8b08
KY
277 (when (and (string-match "\\`[ \t\n]" text)
278 (not (bolp))
279 (not (eq (char-after (1- (point))) ? )))
280 (insert " "))
281 (dolist (elem (split-string text))
282 (when (and (bolp)
283 (> shr-indentation 0))
284 (shr-indent))
73db8b08 285 ;; No space is needed behind a wide character categorized as
b41c2f65
KY
286 ;; kinsoku-bol, between characters both categorized as nospace,
287 ;; or at the beginning of a line.
73db8b08 288 (let (prev)
48ba8195
KY
289 (when (and (> (current-column) shr-indentation)
290 (eq (preceding-char) ? )
20438017 291 (or (= (line-beginning-position) (1- (point)))
ed797193
G
292 (and (shr-char-breakable-p
293 (setq prev (char-after (- (point) 2))))
294 (shr-char-kinsoku-bol-p prev))
295 (and (shr-char-nospace-p prev)
296 (shr-char-nospace-p (aref elem 0)))))
73db8b08 297 (delete-char -1)))
48ba8195
KY
298 ;; The shr-start is a special variable that is used to pass
299 ;; upwards the first point in the buffer where the text really
300 ;; starts.
301 (unless shr-start
302 (setq shr-start (point)))
73db8b08 303 (insert elem)
e7102c0a
KY
304 (let (found)
305 (while (and (> (current-column) shr-width)
306 (progn
307 (setq found (shr-find-fill-point))
b40950bf 308 (not (eolp))))
fe98a42f
KY
309 (when (eq (preceding-char) ? )
310 (delete-char -1))
311 (insert "\n")
312 (unless found
e7102c0a
KY
313 (put-text-property (1- (point)) (point) 'shr-break t)
314 ;; No space is needed at the beginning of a line.
315 (when (eq (following-char) ? )
316 (delete-char 1)))
317 (when (> shr-indentation 0)
318 (shr-indent))
319 (end-of-line))
320 (insert " ")))
73db8b08
KY
321 (unless (string-match "[ \t\n]\\'" text)
322 (delete-char -1)))))
66627fa9 323
6b7df8d3 324(defun shr-find-fill-point ()
83ffd571
KY
325 (when (> (move-to-column shr-width) shr-width)
326 (backward-char 1))
ed797193
G
327 (let ((bp (point))
328 failed)
329 (while (not (or (setq failed (= (current-column) shr-indentation))
330 (eq (preceding-char) ? )
331 (eq (following-char) ? )
332 (shr-char-breakable-p (preceding-char))
333 (shr-char-breakable-p (following-char))
7454326a
G
334 (if (eq (preceding-char) ?')
335 (not (memq (char-after (- (point) 2))
336 (list nil ?\n ? )))
7454326a 337 (and (shr-char-kinsoku-bol-p (preceding-char))
6568edea 338 (shr-char-breakable-p (following-char))
7454326a 339 (not (shr-char-kinsoku-bol-p (following-char)))))
ed797193 340 (shr-char-kinsoku-eol-p (following-char))))
83ffd571 341 (backward-char 1))
ed797193
G
342 (if (and (not (or failed (eolp)))
343 (eq (preceding-char) ?'))
344 (while (not (or (setq failed (eolp))
345 (eq (following-char) ? )
346 (shr-char-breakable-p (following-char))
347 (shr-char-kinsoku-eol-p (following-char))))
348 (forward-char 1)))
83ffd571 349 (if failed
20438017 350 ;; There's no breakable point, so we give it up.
ed797193
G
351 (let (found)
352 (goto-char bp)
353 (unless shr-kinsoku-shorten
354 (while (and (setq found (re-search-forward
355 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
356 (line-end-position) 'move))
357 (eq (preceding-char) ?')))
358 (if (and found (not (match-beginning 1)))
359 (goto-char (match-beginning 0)))))
b40950bf
KY
360 (or
361 (eolp)
ed797193
G
362 ;; Don't put kinsoku-bol characters at the beginning of a line,
363 ;; or kinsoku-eol characters at the end of a line.
364 (cond
365 (shr-kinsoku-shorten
366 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
367 (shr-char-kinsoku-eol-p (preceding-char)))
368 (backward-char 1))
369 (when (setq failed (= (current-column) shr-indentation))
370 ;; There's no breakable point that doesn't violate kinsoku,
371 ;; so we look for the second best position.
372 (while (and (progn
373 (forward-char 1)
374 (<= (current-column) shr-width))
375 (progn
376 (setq bp (point))
377 (shr-char-kinsoku-eol-p (following-char)))))
378 (goto-char bp)))
379 ((shr-char-kinsoku-eol-p (preceding-char))
380 (if (shr-char-kinsoku-eol-p (following-char))
381 ;; There are consecutive kinsoku-eol characters.
382 (setq failed t)
383 (let ((count 4))
384 (while
385 (progn
386 (backward-char 1)
387 (and (> (setq count (1- count)) 0)
388 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
389 (or (shr-char-kinsoku-eol-p (preceding-char))
390 (shr-char-kinsoku-bol-p (following-char)))))))
391 (if (setq failed (= (current-column) shr-indentation))
b40950bf 392 ;; There's no breakable point that doesn't violate kinsoku,
ed797193
G
393 ;; so we go to the second best position.
394 (if (looking-at "\\(\\c<+\\)\\c<")
395 (goto-char (match-end 1))
396 (forward-char 1)))))
397 (t
398 (if (shr-char-kinsoku-bol-p (preceding-char))
399 ;; There are consecutive kinsoku-bol characters.
400 (setq failed t)
b40950bf
KY
401 (let ((count 4))
402 (while (and (>= (setq count (1- count)) 0)
ed797193
G
403 (shr-char-kinsoku-bol-p (following-char))
404 (shr-char-breakable-p (following-char)))
405 (forward-char 1))))))
406 (when (eq (following-char) ? )
407 (forward-char 1))))
408 (not failed)))
6b7df8d3 409
66627fa9
G
410(defun shr-ensure-newline ()
411 (unless (zerop (current-column))
412 (insert "\n")))
a41c2e6d
G
413
414(defun shr-ensure-paragraph ()
415 (unless (bobp)
f7aa248a 416 (if (<= (current-column) shr-indentation)
71e691a5
G
417 (unless (save-excursion
418 (forward-line -1)
419 (looking-at " *$"))
a41c2e6d
G
420 (insert "\n"))
421 (if (save-excursion
422 (beginning-of-line)
5d2ef6db 423 (looking-at " *$"))
a41c2e6d
G
424 (insert "\n")
425 (insert "\n\n")))))
426
66627fa9 427(defun shr-indent ()
f7aa248a
G
428 (when (> shr-indentation 0)
429 (insert (make-string shr-indentation ? ))))
870409d4 430
a41c2e6d 431(defun shr-fontize-cont (cont &rest types)
870409d4
G
432 (let (shr-start)
433 (shr-generic cont)
a41c2e6d
G
434 (dolist (type types)
435 (shr-add-font (or shr-start (point)) (point) type))))
870409d4 436
6c85a14f
LMI
437;; Add an overlay in the region, but avoid putting the font properties
438;; on blank text at the start of the line, and the newline at the end,
439;; to avoid ugliness.
870409d4 440(defun shr-add-font (start end type)
6c85a14f
LMI
441 (save-excursion
442 (goto-char start)
443 (while (< (point) end)
444 (when (bolp)
445 (skip-chars-forward " "))
446 (let ((overlay (make-overlay (point) (min (line-end-position) end))))
447 (overlay-put overlay 'face type))
448 (if (< (line-end-position) end)
449 (forward-line 1)
450 (goto-char end)))))
870409d4 451
71e691a5
G
452(defun shr-browse-url ()
453 "Browse the URL under point."
454 (interactive)
455 (let ((url (get-text-property (point) 'shr-url)))
181cb5fb
G
456 (cond
457 ((not url)
458 (message "No link under point"))
459 ((string-match "^mailto:" url)
be3c11b3 460 (browse-url-mailto url))
181cb5fb
G
461 (t
462 (browse-url url)))))
71e691a5 463
cdf1fca4
LMI
464(defun shr-save-contents (directory)
465 "Save the contents from URL in a file."
466 (interactive "DSave contents of URL to directory: ")
467 (let ((url (get-text-property (point) 'shr-url)))
468 (if (not url)
469 (message "No link under point")
470 (url-retrieve (shr-encode-url url)
471 'shr-store-contents (list url directory)))))
472
473(defun shr-store-contents (status url directory)
474 (unless (plist-get status :error)
475 (when (or (search-forward "\n\n" nil t)
476 (search-forward "\r\n\r\n" nil t))
477 (write-region (point) (point-max)
478 (expand-file-name (file-name-nondirectory url)
479 directory)))))
480
870409d4
G
481(defun shr-image-fetched (status buffer start end)
482 (when (and (buffer-name buffer)
483 (not (plist-get status :error)))
484 (url-store-in-cache (current-buffer))
485 (when (or (search-forward "\n\n" nil t)
486 (search-forward "\r\n\r\n" nil t))
487 (let ((data (buffer-substring (point) (point-max))))
488 (with-current-buffer buffer
cb51ba08
LI
489 (save-excursion
490 (let ((alt (buffer-substring start end))
491 (inhibit-read-only t))
492 (delete-region start end)
493 (goto-char start)
494 (shr-put-image data alt)))))))
870409d4
G
495 (kill-buffer (current-buffer)))
496
99e65b2d 497(defun shr-put-image (data alt)
4abff904
JD
498 (if (display-graphic-p)
499 (let ((image (ignore-errors
500 (shr-rescale-image data))))
501 (when image
c0f9edce
G
502 ;; When inserting big-ish pictures, put them at the
503 ;; beginning of the line.
504 (when (and (> (current-column) 0)
505 (> (car (image-size image t)) 400))
506 (insert "\n"))
99e65b2d
G
507 (insert-image image (or alt "*"))))
508 (insert alt)))
870409d4
G
509
510(defun shr-rescale-image (data)
511 (if (or (not (fboundp 'imagemagick-types))
512 (not (get-buffer-window (current-buffer))))
513 (create-image data nil t)
514 (let* ((image (create-image data nil t))
a41c2e6d 515 (size (image-size image t))
870409d4
G
516 (width (car size))
517 (height (cdr size))
518 (edges (window-inside-pixel-edges
519 (get-buffer-window (current-buffer))))
520 (window-width (truncate (* shr-max-image-proportion
521 (- (nth 2 edges) (nth 0 edges)))))
522 (window-height (truncate (* shr-max-image-proportion
523 (- (nth 3 edges) (nth 1 edges)))))
524 scaled-image)
525 (when (> height window-height)
526 (setq image (or (create-image data 'imagemagick t
527 :height window-height)
528 image))
529 (setq size (image-size image t)))
530 (when (> (car size) window-width)
531 (setq image (or
532 (create-image data 'imagemagick t
533 :width window-width)
534 image)))
84d89ede
LMI
535 (when (and (fboundp 'create-animated-image)
536 (eq (image-type data nil t) 'gif))
537 (setq image (create-animated-image data 'gif t)))
870409d4
G
538 image)))
539
85a45a69
GM
540;; url-cache-extract autoloads url-cache.
541(declare-function url-cache-create-filename "url-cache" (url))
542(autoload 'mm-disable-multibyte "mm-util")
be3c11b3 543(autoload 'browse-url-mailto "browse-url")
85a45a69 544
870409d4
G
545(defun shr-get-image-data (url)
546 "Get image data for URL.
547Return a string with image data."
548 (with-temp-buffer
549 (mm-disable-multibyte)
71e691a5 550 (when (ignore-errors
ab67634f 551 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
71e691a5
G
552 t)
553 (when (or (search-forward "\n\n" nil t)
554 (search-forward "\r\n\r\n" nil t))
555 (buffer-substring (point) (point-max))))))
870409d4 556
40de2c6d
KY
557(defun shr-image-displayer (content-function)
558 "Return a function to display an image.
559CONTENT-FUNCTION is a function to retrieve an image for a cid url that
560is an argument. The function to be returned takes three arguments URL,
e2d0ba98 561START, and END. Note that START and END should be merkers."
40de2c6d 562 `(lambda (url start end)
f8d8a97b
KY
563 (when url
564 (if (string-match "\\`cid:" url)
565 ,(when content-function
566 `(let ((image (funcall ,content-function
567 (substring url (match-end 0)))))
568 (when image
569 (goto-char start)
570 (shr-put-image image
e2d0ba98
KY
571 (buffer-substring-no-properties start end))
572 (delete-region (point) end))))
f8d8a97b
KY
573 (url-retrieve url 'shr-image-fetched
574 (list (current-buffer) start end)
575 t)))))
40de2c6d 576
66627fa9
G
577(defun shr-heading (cont &rest types)
578 (shr-ensure-paragraph)
579 (apply #'shr-fontize-cont cont types)
580 (shr-ensure-paragraph))
581
85a45a69
GM
582(autoload 'widget-convert-button "wid-edit")
583
04db63bc 584(defun shr-urlify (start url &optional title)
de635afe
G
585 (widget-convert-button
586 'url-link start (point)
04db63bc 587 :help-echo (if title (format "%s (%s)" url title) url)
de635afe
G
588 :keymap shr-map
589 url)
590 (put-text-property start (point) 'shr-url url))
591
592(defun shr-encode-url (url)
593 "Encode URL."
594 (browse-url-url-encode-chars url "[)$ ]"))
595
ebe79557
LMI
596(autoload 'shr-color-visible "shr-color")
597(autoload 'shr-color->hexadecimal "shr-color")
144b7b5c
G
598
599(defun shr-color-check (fg bg)
600 "Check that FG is visible on BG.
601Returns (fg bg) with corrected values.
602Returns nil if the colors that would be used are the default
603ones, in case fg and bg are nil."
604 (when (or fg bg)
605 (let ((fixed (cond ((null fg) 'fg)
606 ((null bg) 'bg))))
607 ;; Convert colors to hexadecimal, or set them to default.
608 (let ((fg (or (shr-color->hexadecimal fg)
609 (frame-parameter nil 'foreground-color)))
610 (bg (or (shr-color->hexadecimal bg)
611 (frame-parameter nil 'background-color))))
612 (cond ((eq fixed 'bg)
613 ;; Only return the new fg
614 (list nil (cadr (shr-color-visible bg fg t))))
615 ((eq fixed 'fg)
616 ;; Invert args and results and return only the new bg
617 (list (cadr (shr-color-visible fg bg t)) nil))
618 (t
619 (shr-color-visible bg fg)))))))
620
04db63bc 621(defun shr-colorize-region (start end fg &optional bg)
b31b26b4 622 (when (or fg bg)
04db63bc 623 (let ((new-colors (shr-color-check fg bg)))
144b7b5c 624 (when new-colors
60568d74
LMI
625 (when fg
626 (shr-put-color start end :foreground (cadr new-colors)))
04db63bc
G
627 (when bg
628 (shr-put-color start end :background (car new-colors)))))))
629
630;; Put a color in the region, but avoid putting colors on on blank
631;; text at the start of the line, and the newline at the end, to avoid
632;; ugliness. Also, don't overwrite any existing color information,
633;; since this can be called recursively, and we want the "inner" color
634;; to win.
635(defun shr-put-color (start end type color)
636 (save-excursion
637 (goto-char start)
638 (while (< (point) end)
639 (when (bolp)
640 (skip-chars-forward " "))
641 (when (> (line-end-position) (point))
642 (shr-put-color-1 (point) (min (line-end-position) end) type color))
643 (if (< (line-end-position) end)
644 (forward-line 1)
c5ecc769
G
645 (goto-char end)))
646 (when (eq type :background)
647 (shr-expand-newlines start end color))))
648
649(defun shr-expand-newlines (start end color)
650 (save-restriction
651 (narrow-to-region start end)
652 (let ((width (shr-natural-width))
653 column)
654 (goto-char (point-min))
655 (while (not (eobp))
656 (end-of-line)
657 (when (and (< (setq current-column (current-column)) width)
658 (not (overlays-at (point))))
659 (let ((overlay (make-overlay (point) (1+ (point)))))
660 (overlay-put overlay 'before-string
661 (propertize (make-string (- width current-column) ? )
662 'face (list :background color)))))
663 (forward-line 1)))))
04db63bc
G
664
665(defun shr-put-color-1 (start end type color)
666 (let* ((old-props (get-text-property start 'face))
667 (do-put (not (memq type old-props)))
668 change)
669 (while (< start end)
670 (setq change (next-single-property-change start 'face nil end))
671 (when do-put
672 (put-text-property start change 'face
673 (nconc (list type color) old-props)))
674 (setq old-props (get-text-property change 'face))
675 (setq do-put (not (memq type old-props)))
676 (setq start change))
677 (when (and do-put
678 (> end start))
679 (put-text-property start end 'face
680 (nconc (list type color old-props))))))
ebe79557 681
66627fa9
G
682;;; Tag-specific rendering rules.
683
144b7b5c 684(defun shr-tag-body (cont)
04db63bc
G
685 (let* ((start (point))
686 (fgcolor (cdr (assq :fgcolor cont)))
687 (bgcolor (cdr (assq :bgcolor cont)))
b31b26b4
G
688 (shr-stylesheet (list (cons 'color fgcolor)
689 (cons 'background-color bgcolor))))
144b7b5c 690 (shr-generic cont)
04db63bc 691 (shr-colorize-region start (point) fgcolor bgcolor)))
144b7b5c 692
b31b26b4
G
693(defun shr-tag-style (cont)
694 )
695
f73341e2
LMI
696(defun shr-tag-script (cont)
697 )
698
7bafe9bc
LMI
699(defun shr-tag-label (cont)
700 (shr-generic cont)
701 (shr-ensure-paragraph))
702
66627fa9
G
703(defun shr-tag-p (cont)
704 (shr-ensure-paragraph)
f7aa248a 705 (shr-indent)
66627fa9
G
706 (shr-generic cont)
707 (shr-ensure-paragraph))
708
036d93bc
KY
709(defun shr-tag-div (cont)
710 (shr-ensure-newline)
711 (shr-indent)
712 (shr-generic cont)
713 (shr-ensure-newline))
714
66627fa9
G
715(defun shr-tag-b (cont)
716 (shr-fontize-cont cont 'bold))
717
718(defun shr-tag-i (cont)
719 (shr-fontize-cont cont 'italic))
720
721(defun shr-tag-em (cont)
722 (shr-fontize-cont cont 'bold))
723
530f7b67
LMI
724(defun shr-tag-strong (cont)
725 (shr-fontize-cont cont 'bold))
726
66627fa9
G
727(defun shr-tag-u (cont)
728 (shr-fontize-cont cont 'underline))
729
730(defun shr-tag-s (cont)
731 (shr-fontize-cont cont 'strike-through))
732
2e76c12c
LMI
733(defun shr-parse-style (style)
734 (when style
a2994808
JD
735 (save-match-data
736 (when (string-match "\n" style)
737 (setq style (replace-match " " t t style))))
2e76c12c
LMI
738 (let ((plist nil))
739 (dolist (elem (split-string style ";"))
740 (when elem
741 (setq elem (split-string elem ":"))
742 (when (and (car elem)
743 (cadr elem))
744 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
745 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
144b7b5c
G
746 (when (string-match " *!important\\'" value)
747 (setq value (substring value 0 (match-beginning 0))))
2e76c12c
LMI
748 (push (cons (intern name obarray)
749 value)
750 plist)))))
751 plist)))
752
66627fa9
G
753(defun shr-tag-a (cont)
754 (let ((url (cdr (assq :href cont)))
04db63bc 755 (title (cdr (assq :title cont)))
66627fa9
G
756 (start (point))
757 shr-start)
758 (shr-generic cont)
04db63bc 759 (shr-urlify (or shr-start start) url title)))
de635afe
G
760
761(defun shr-tag-object (cont)
99e65b2d
G
762 (let ((start (point))
763 url)
764 (dolist (elem cont)
765 (when (eq (car elem) 'embed)
766 (setq url (or url (cdr (assq :src (cdr elem))))))
767 (when (and (eq (car elem) 'param)
768 (equal (cdr (assq :name (cdr elem))) "movie"))
769 (setq url (or url (cdr (assq :value (cdr elem)))))))
de635afe
G
770 (when url
771 (shr-insert " [multimedia] ")
99e65b2d
G
772 (shr-urlify start url))
773 (shr-generic cont)))
774
775(defun shr-tag-video (cont)
776 (let ((image (cdr (assq :poster cont)))
777 (url (cdr (assq :src cont)))
778 (start (point)))
779 (shr-tag-img nil image)
780 (shr-urlify start url)))
ab67634f 781
99e65b2d
G
782(defun shr-tag-img (cont &optional url)
783 (when (or url
784 (and cont
785 (cdr (assq :src cont))))
68f6bd17
KY
786 (when (and (> (current-column) 0)
787 (not (eq shr-state 'image)))
788 (insert "\n"))
789 (let ((alt (cdr (assq :alt cont)))
99e65b2d 790 (url (or url (cdr (assq :src cont)))))
68f6bd17
KY
791 (let ((start (point-marker)))
792 (when (zerop (length alt))
953d41c4 793 (setq alt "*"))
68f6bd17 794 (cond
99e65b2d
G
795 ((or (member (cdr (assq :height cont)) '("0" "1"))
796 (member (cdr (assq :width cont)) '("0" "1")))
797 ;; Ignore zero-sized or single-pixel images.
798 )
68f6bd17
KY
799 ((and (not shr-inhibit-images)
800 (string-match "\\`cid:" url))
801 (let ((url (substring url (match-end 0)))
802 image)
803 (if (or (not shr-content-function)
804 (not (setq image (funcall shr-content-function url))))
805 (insert alt)
99e65b2d 806 (shr-put-image image alt))))
68f6bd17
KY
807 ((or shr-inhibit-images
808 (and shr-blocked-images
809 (string-match shr-blocked-images url)))
810 (setq shr-start (point))
811 (let ((shr-state 'space))
b354bc53
KY
812 (if (> (string-width alt) 8)
813 (shr-insert (truncate-string-to-width alt 8))
68f6bd17
KY
814 (shr-insert alt))))
815 ((url-is-cached (shr-encode-url url))
99e65b2d 816 (shr-put-image (shr-get-image-data url) alt))
68f6bd17
KY
817 (t
818 (insert alt)
819 (ignore-errors
820 (url-retrieve (shr-encode-url url) 'shr-image-fetched
821 (list (current-buffer) start (point-marker))
822 t))))
68f6bd17
KY
823 (put-text-property start (point) 'keymap shr-map)
824 (put-text-property start (point) 'shr-alt alt)
8b6f6573
LMI
825 (put-text-property start (point) 'image-url url)
826 (put-text-property start (point) 'image-displayer
40de2c6d 827 (shr-image-displayer shr-content-function))
99e65b2d 828 (put-text-property start (point) 'help-echo alt)
68f6bd17 829 (setq shr-state 'image)))))
66627fa9
G
830
831(defun shr-tag-pre (cont)
832 (let ((shr-folding-mode 'none))
833 (shr-ensure-newline)
f7aa248a 834 (shr-indent)
66627fa9
G
835 (shr-generic cont)
836 (shr-ensure-newline)))
837
838(defun shr-tag-blockquote (cont)
839 (shr-ensure-paragraph)
f7aa248a 840 (shr-indent)
66627fa9
G
841 (let ((shr-indentation (+ shr-indentation 4)))
842 (shr-generic cont))
843 (shr-ensure-paragraph))
a41c2e6d
G
844
845(defun shr-tag-ul (cont)
846 (shr-ensure-paragraph)
847 (let ((shr-list-mode 'ul))
3d319c8f
LMI
848 (shr-generic cont))
849 (shr-ensure-paragraph))
a41c2e6d
G
850
851(defun shr-tag-ol (cont)
3d319c8f 852 (shr-ensure-paragraph)
a41c2e6d 853 (let ((shr-list-mode 1))
3d319c8f
LMI
854 (shr-generic cont))
855 (shr-ensure-paragraph))
a41c2e6d
G
856
857(defun shr-tag-li (cont)
f7aa248a
G
858 (shr-ensure-paragraph)
859 (shr-indent)
8028ed5c
LMI
860 (let* ((bullet
861 (if (numberp shr-list-mode)
862 (prog1
863 (format "%d " shr-list-mode)
864 (setq shr-list-mode (1+ shr-list-mode)))
865 "* "))
866 (shr-indentation (+ shr-indentation (length bullet))))
867 (insert bullet)
868 (shr-generic cont)))
a41c2e6d
G
869
870(defun shr-tag-br (cont)
1e463294 871 (unless (bobp)
f7aa248a
G
872 (insert "\n")
873 (shr-indent))
a41c2e6d
G
874 (shr-generic cont))
875
876(defun shr-tag-h1 (cont)
c5ecc769 877 (shr-heading cont 'shr-tag-h1))
a41c2e6d
G
878
879(defun shr-tag-h2 (cont)
c5ecc769 880 (shr-heading cont 'shr-tag-h2))
a41c2e6d
G
881
882(defun shr-tag-h3 (cont)
c5ecc769 883 (shr-heading cont 'shr-tag-h3))
a41c2e6d
G
884
885(defun shr-tag-h4 (cont)
c5ecc769 886 (shr-heading cont 'shr-tag-h4))
a41c2e6d
G
887
888(defun shr-tag-h5 (cont)
c5ecc769 889 (shr-heading cont 'shr-tag-h5))
a41c2e6d
G
890
891(defun shr-tag-h6 (cont)
c5ecc769 892 (shr-heading cont 'shr-tag-h6))
a41c2e6d 893
3d319c8f
LMI
894(defun shr-tag-hr (cont)
895 (shr-ensure-newline)
6b7df8d3 896 (insert (make-string shr-width shr-hr-line) "\n"))
3d319c8f 897
144b7b5c
G
898(defun shr-tag-title (cont)
899 (shr-heading cont 'bold 'underline))
900
1110d53b 901(defun shr-tag-font (cont)
b31b26b4
G
902 (let* ((start (point))
903 (color (cdr (assq :color cont)))
904 (shr-stylesheet (nconc (list (cons 'color color))
905 shr-stylesheet)))
1110d53b 906 (shr-generic cont)
b31b26b4
G
907 (when color
908 (shr-colorize-region start (point) color
909 (cdr (assq 'background-color shr-stylesheet))))))
1110d53b 910
66627fa9 911;;; Table rendering algorithm.
a41c2e6d 912
a0ec382a
LMI
913;; Table rendering is the only complicated thing here. We do this by
914;; first counting how many TDs there are in each TR, and registering
915;; how wide they think they should be ("width=45%", etc). Then we
916;; render each TD separately (this is done in temporary buffers, so
917;; that we can use all the rendering machinery as if we were in the
918;; main buffer). Now we know how much space each TD really takes, so
919;; we then render everything again with the new widths, and finally
920;; insert all these boxes into the main buffer.
6c769311 921(defun shr-tag-table-1 (cont)
71e691a5
G
922 (setq cont (or (cdr (assq 'tbody cont))
923 cont))
130e977f 924 (let* ((shr-inhibit-images t)
99e65b2d 925 (shr-table-depth (1+ shr-table-depth))
83ffd571 926 (shr-kinsoku-shorten t)
a0ec382a 927 ;; Find all suggested widths.
130e977f 928 (columns (shr-column-specs cont))
a0ec382a 929 ;; Compute how many characters wide each TD should be.
71e691a5 930 (suggested-widths (shr-pro-rate-columns columns))
a0ec382a
LMI
931 ;; Do a "test rendering" to see how big each TD is (this can
932 ;; be smaller (if there's little text) or bigger (if there's
933 ;; unbreakable text).
71e691a5 934 (sketch (shr-make-table cont suggested-widths))
a7dcc87b 935 (sketch-widths (shr-table-widths sketch suggested-widths)))
030158f3 936 ;; This probably won't work very well.
83ffd571
KY
937 (when (> (+ (loop for width across sketch-widths
938 summing (1+ width))
939 shr-indentation 1)
030158f3
G
940 (frame-width))
941 (setq truncate-lines t))
a0ec382a 942 ;; Then render the table again with these new "hard" widths.
130e977f 943 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
a0ec382a
LMI
944 ;; Finally, insert all the images after the table. The Emacs buffer
945 ;; model isn't strong enough to allow us to put the images actually
946 ;; into the tables.
99e65b2d
G
947 (when (zerop shr-table-depth)
948 (dolist (elem (shr-find-elements cont 'img))
949 (shr-tag-img (cdr elem)))))
130e977f 950
6c769311
KY
951(defun shr-tag-table (cont)
952 (shr-ensure-paragraph)
953 (let* ((caption (cdr (assq 'caption cont)))
954 (header (cdr (assq 'thead cont)))
955 (body (or (cdr (assq 'tbody cont)) cont))
956 (footer (cdr (assq 'tfoot cont)))
144b7b5c 957 (bgcolor (cdr (assq :bgcolor cont)))
60568d74
LMI
958 (start (point))
959 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
960 shr-stylesheet))
6c769311
KY
961 (nheader (if header (shr-max-columns header)))
962 (nbody (if body (shr-max-columns body)))
963 (nfooter (if footer (shr-max-columns footer))))
964 (shr-tag-table-1
3c066373
KY
965 (nconc
966 (if caption `((tr (td ,@caption))))
967 (if header
968 (if footer
969 ;; hader + body + footer
970 (if (= nheader nbody)
971 (if (= nbody nfooter)
972 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
973 (nconc `((tr (td (table (tbody ,@header ,@body)))))
974 (if (= nfooter 1)
975 footer
976 `((tr (td (table (tbody ,@footer))))))))
977 (nconc `((tr (td (table (tbody ,@header)))))
978 (if (= nbody nfooter)
979 `((tr (td (table (tbody ,@body ,@footer)))))
980 (nconc `((tr (td (table (tbody ,@body)))))
981 (if (= nfooter 1)
982 footer
983 `((tr (td (table (tbody ,@footer))))))))))
984 ;; header + body
985 (if (= nheader nbody)
986 `((tr (td (table (tbody ,@header ,@body)))))
987 (if (= nheader 1)
988 `(,@header (tr (td (table (tbody ,@body)))))
989 `((tr (td (table (tbody ,@header))))
990 (tr (td (table (tbody ,@body))))))))
991 (if footer
992 ;; body + footer
993 (if (= nbody nfooter)
994 `((tr (td (table (tbody ,@body ,@footer)))))
995 (nconc `((tr (td (table (tbody ,@body)))))
2526f423 996 (if (= nfooter 1)
3c066373
KY
997 footer
998 `((tr (td (table (tbody ,@footer))))))))
999 (if caption
1000 `((tr (td (table (tbody ,@body)))))
60568d74
LMI
1001 body)))))
1002 (when bgcolor
1003 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1004 bgcolor))))
6c769311 1005
130e977f
LMI
1006(defun shr-find-elements (cont type)
1007 (let (result)
1008 (dolist (elem cont)
1009 (cond ((eq (car elem) type)
1010 (push elem result))
1011 ((consp (cdr elem))
1012 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1013 (nreverse result)))
71e691a5
G
1014
1015(defun shr-insert-table (table widths)
1016 (shr-insert-table-ruler widths)
1017 (dolist (row table)
1018 (let ((start (point))
1019 (height (let ((max 0))
1020 (dolist (column row)
1021 (setq max (max max (cadr column))))
1022 max)))
1023 (dotimes (i height)
1024 (shr-indent)
d3098750 1025 (insert shr-table-vertical-line "\n"))
71e691a5
G
1026 (dolist (column row)
1027 (goto-char start)
a7dcc87b 1028 (let ((lines (nth 2 column))
130e977f
LMI
1029 (overlay-lines (nth 3 column))
1030 overlay overlay-line)
71e691a5 1031 (dolist (line lines)
130e977f 1032 (setq overlay-line (pop overlay-lines))
3d319c8f 1033 (end-of-line)
d3098750 1034 (insert line shr-table-vertical-line)
3d319c8f
LMI
1035 (dolist (overlay overlay-line)
1036 (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
1037 (- (point) (nth 1 overlay) 1)))
1038 (properties (nth 2 overlay)))
1039 (while properties
1040 (overlay-put o (pop properties) (pop properties)))))
1041 (forward-line 1))
71e691a5
G
1042 ;; Add blank lines at padding at the bottom of the TD,
1043 ;; possibly.
1044 (dotimes (i (- height (length lines)))
1045 (end-of-line)
d3098750
LMI
1046 (insert (make-string (string-width (car lines)) ? )
1047 shr-table-vertical-line)
71e691a5
G
1048 (forward-line 1)))))
1049 (shr-insert-table-ruler widths)))
1050
1051(defun shr-insert-table-ruler (widths)
83ffd571
KY
1052 (when (and (bolp)
1053 (> shr-indentation 0))
1054 (shr-indent))
afba0c4b 1055 (insert shr-table-corner)
71e691a5 1056 (dotimes (i (length widths))
d3098750
LMI
1057 (insert (make-string (aref widths i) shr-table-horizontal-line)
1058 shr-table-corner))
71e691a5
G
1059 (insert "\n"))
1060
a7dcc87b
G
1061(defun shr-table-widths (table suggested-widths)
1062 (let* ((length (length suggested-widths))
1063 (widths (make-vector length 0))
1064 (natural-widths (make-vector length 0)))
71e691a5
G
1065 (dolist (row table)
1066 (let ((i 0))
1067 (dolist (column row)
1068 (aset widths i (max (aref widths i)
1069 (car column)))
a7dcc87b
G
1070 (aset natural-widths i (max (aref natural-widths i)
1071 (cadr column)))
1072 (setq i (1+ i)))))
863b61d6
KY
1073 (let ((extra (- (apply '+ (append suggested-widths nil))
1074 (apply '+ (append widths nil))))
a7dcc87b
G
1075 (expanded-columns 0))
1076 (when (> extra 0)
1077 (dotimes (i length)
1078 ;; If the natural width is wider than the rendered width, we
1079 ;; want to allow the column to expand.
1080 (when (> (aref natural-widths i) (aref widths i))
1081 (setq expanded-columns (1+ expanded-columns))))
1082 (dotimes (i length)
1083 (when (> (aref natural-widths i) (aref widths i))
1084 (aset widths i (min
1085 (1+ (aref natural-widths i))
1086 (+ (/ extra expanded-columns)
1087 (aref widths i))))))))
71e691a5
G
1088 widths))
1089
1090(defun shr-make-table (cont widths &optional fill)
1091 (let ((trs nil))
1092 (dolist (row cont)
1093 (when (eq (car row) 'tr)
a0ec382a
LMI
1094 (let ((tds nil)
1095 (columns (cdr row))
1096 (i 0)
1097 column)
1098 (while (< i (length widths))
1099 (setq column (pop columns))
1100 (when (or (memq (car column) '(td th))
1101 (null column))
71e691a5
G
1102 (push (shr-render-td (cdr column) (aref widths i) fill)
1103 tds)
1104 (setq i (1+ i))))
1105 (push (nreverse tds) trs))))
1106 (nreverse trs)))
1107
1108(defun shr-render-td (cont width fill)
04db63bc 1109 (with-temp-buffer
60568d74
LMI
1110 (let ((bgcolor (cdr (assq :bgcolor cont)))
1111 (fgcolor (cdr (assq :fgcolor cont)))
1112 (style (cdr (assq :style cont)))
1113 (shr-stylesheet shr-stylesheet)
1114 overlays)
1115 (when style
1116 (setq style (and (string-match "color" style)
1117 (shr-parse-style style))))
1118 (when bgcolor
1119 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1120 (when fgcolor
1121 (setq style (nconc (list (cons 'color fgcolor)) style)))
1122 (when style
1123 (setq shr-stylesheet (append style shr-stylesheet)))
1124 (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
1125 (if cache
1126 (progn
1127 (insert (car cache))
1128 (let ((end (length (car cache))))
1129 (dolist (overlay (cadr cache))
1130 (let ((new-overlay
1131 (make-overlay (1+ (- end (nth 0 overlay)))
1132 (1+ (- end (nth 1 overlay)))))
1133 (properties (nth 2 overlay)))
1134 (while properties
1135 (overlay-put new-overlay
1136 (pop properties) (pop properties)))))))
1137 (let ((shr-width width)
1138 (shr-indentation 0))
1139 (shr-descend (cons 'td cont)))
1140 (delete-region
1141 (point)
1142 (+ (point)
1143 (skip-chars-backward " \t\n")))
1144 (push (list (cons width cont) (buffer-string)
1145 (shr-overlays-in-region (point-min) (point-max)))
1146 shr-content-cache)))
1147 (goto-char (point-min))
1148 (let ((max 0))
1149 (while (not (eobp))
1150 (end-of-line)
1151 (setq max (max max (current-column)))
1152 (forward-line 1))
1153 (when fill
1154 (goto-char (point-min))
1155 ;; If the buffer is totally empty, then put a single blank
1156 ;; line here.
1157 (if (zerop (buffer-size))
1158 (insert (make-string width ? ))
1159 ;; Otherwise, fill the buffer.
1160 (while (not (eobp))
1161 (end-of-line)
1162 (when (> (- width (current-column)) 0)
1163 (insert (make-string (- width (current-column)) ? )))
1164 (forward-line 1))))
1165 (when style
1166 (shr-colorize-region
1167 (point-min) (point-max)
1168 (cdr (assq 'color shr-stylesheet))
1169 (cdr (assq 'background-color shr-stylesheet))))
1170 (if fill
1171 (list max
1172 (count-lines (point-min) (point-max))
1173 (split-string (buffer-string) "\n")
1174 (shr-collect-overlays))
04db63bc 1175 (list max
60568d74 1176 (shr-natural-width)))))))
a7dcc87b
G
1177
1178(defun shr-natural-width ()
1179 (goto-char (point-min))
1180 (let ((current 0)
1181 (max 0))
1182 (while (not (eobp))
1183 (end-of-line)
1184 (setq current (+ current (current-column)))
1185 (unless (get-text-property (point) 'shr-break)
1186 (setq max (max max current)
1187 current 0))
1188 (forward-line 1))
1189 max))
130e977f
LMI
1190
1191(defun shr-collect-overlays ()
1192 (save-excursion
1193 (goto-char (point-min))
1194 (let ((overlays nil))
1195 (while (not (eobp))
1196 (push (shr-overlays-in-region (point) (line-end-position))
1197 overlays)
1198 (forward-line 1))
1199 (nreverse overlays))))
1200
1201(defun shr-overlays-in-region (start end)
1202 (let (result)
1203 (dolist (overlay (overlays-in start end))
1204 (push (list (if (> start (overlay-start overlay))
1205 (- end start)
1206 (- end (overlay-start overlay)))
1207 (if (< end (overlay-end overlay))
1208 0
1209 (- end (overlay-end overlay)))
1210 (overlay-properties overlay))
1211 result))
1212 (nreverse result)))
71e691a5
G
1213
1214(defun shr-pro-rate-columns (columns)
1215 (let ((total-percentage 0)
1216 (widths (make-vector (length columns) 0)))
1217 (dotimes (i (length columns))
a7dcc87b 1218 (setq total-percentage (+ total-percentage (aref columns i))))
71e691a5
G
1219 (setq total-percentage (/ 1.0 total-percentage))
1220 (dotimes (i (length columns))
1221 (aset widths i (max (truncate (* (aref columns i)
1222 total-percentage
a7dcc87b 1223 (- shr-width (1+ (length columns)))))
71e691a5
G
1224 10)))
1225 widths))
1226
1227;; Return a summary of the number and shape of the TDs in the table.
1228(defun shr-column-specs (cont)
1229 (let ((columns (make-vector (shr-max-columns cont) 1)))
1230 (dolist (row cont)
1231 (when (eq (car row) 'tr)
1232 (let ((i 0))
1233 (dolist (column (cdr row))
1234 (when (memq (car column) '(td th))
1235 (let ((width (cdr (assq :width (cdr column)))))
1236 (when (and width
1237 (string-match "\\([0-9]+\\)%" width))
1238 (aset columns i
1239 (/ (string-to-number (match-string 1 width))
130e977f
LMI
1240 100.0))))
1241 (setq i (1+ i)))))))
71e691a5
G
1242 columns))
1243
1244(defun shr-count (cont elem)
1245 (let ((i 0))
1246 (dolist (sub cont)
1247 (when (eq (car sub) elem)
1248 (setq i (1+ i))))
1249 i))
1250
1251(defun shr-max-columns (cont)
1252 (let ((max 0))
1253 (dolist (row cont)
1254 (when (eq (car row) 'tr)
130e977f
LMI
1255 (setq max (max max (+ (shr-count (cdr row) 'td)
1256 (shr-count (cdr row) 'th))))))
71e691a5
G
1257 max))
1258
f3fd95db 1259(provide 'shr)
367f7f81
LMI
1260
1261;;; shr.el ends here