Fix bug #7417 with cursor positioning on empty lines.
[bpt/emacs.git] / lisp / gnus / shr.el
CommitLineData
367f7f81
LMI
1;;; shr.el --- Simple HTML Renderer
2
3;; Copyright (C) 2010 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
3d319c8f 33(eval-when-compile (require 'cl))
71e691a5 34(require 'browse-url)
2fe10446
KY
35(unless (aref (char-category-set (make-char 'japanese-jisx0208 33 35)) ?>)
36 (load "kinsoku" nil t))
71e691a5 37
870409d4
G
38(defgroup shr nil
39 "Simple HTML Renderer"
40 :group 'mail)
41
42(defcustom shr-max-image-proportion 0.9
43 "How big pictures displayed are in relation to the window they're in.
44A value of 0.7 means that they are allowed to take up 70% of the
45width and height of the window. If they are larger than this,
46and Emacs supports it, then the images will be rescaled down to
47fit these criteria."
48 :version "24.1"
49 :group 'shr
50 :type 'float)
51
52(defcustom shr-blocked-images nil
53 "Images that have URLs matching this regexp will be blocked."
54 :version "24.1"
55 :group 'shr
56 :type 'regexp)
57
afba0c4b
JD
58(defcustom shr-table-line ?-
59 "Character used to draw table line."
60 :group 'shr
030158f3 61 :type 'character)
afba0c4b
JD
62
63(defcustom shr-table-corner ?+
6b7df8d3
G
64 "Character used to draw table corner."
65 :group 'shr
030158f3 66 :type 'character)
6b7df8d3
G
67
68(defcustom shr-hr-line ?-
69 "Character used to draw hr line."
afba0c4b 70 :group 'shr
030158f3 71 :type 'character)
afba0c4b 72
d0e0de31
JD
73(defcustom shr-width fill-column
74 "Frame width to use for rendering."
75 :type 'integer
76 :group 'shr)
77
130e977f
LMI
78(defvar shr-content-function nil
79 "If bound, this should be a function that will return the content.
80This is used for cid: URLs, and the function is called with the
81cid: URL as the argument.")
82
66627fa9
G
83;;; Internal variables.
84
870409d4
G
85(defvar shr-folding-mode nil)
86(defvar shr-state nil)
87(defvar shr-start nil)
a41c2e6d 88(defvar shr-indentation 0)
130e977f 89(defvar shr-inhibit-images nil)
66627fa9 90(defvar shr-list-mode nil)
3d319c8f 91(defvar shr-content-cache nil)
83ffd571 92(defvar shr-kinsoku-shorten nil)
99e65b2d 93(defvar shr-table-depth 0)
870409d4 94
71e691a5
G
95(defvar shr-map
96 (let ((map (make-sparse-keymap)))
97 (define-key map "a" 'shr-show-alt-text)
98 (define-key map "i" 'shr-browse-image)
99 (define-key map "I" 'shr-insert-image)
100 (define-key map "u" 'shr-copy-url)
101 (define-key map "v" 'shr-browse-url)
cdf1fca4 102 (define-key map "o" 'shr-save-contents)
71e691a5
G
103 (define-key map "\r" 'shr-browse-url)
104 map))
105
66627fa9
G
106;; Public functions and commands.
107
108;;;###autoload
109(defun shr-insert-document (dom)
3d319c8f 110 (setq shr-content-cache nil)
66627fa9
G
111 (let ((shr-state nil)
112 (shr-start nil))
113 (shr-descend (shr-transform-dom dom))))
114
115(defun shr-copy-url ()
116 "Copy the URL under point to the kill ring.
117If called twice, then try to fetch the URL and see whether it
118redirects somewhere else."
119 (interactive)
120 (let ((url (get-text-property (point) 'shr-url)))
121 (cond
122 ((not url)
123 (message "No URL under point"))
124 ;; Resolve redirected URLs.
125 ((equal url (car kill-ring))
126 (url-retrieve
127 url
128 (lambda (a)
129 (when (and (consp a)
130 (eq (car a) :redirect))
131 (with-temp-buffer
132 (insert (cadr a))
133 (goto-char (point-min))
134 ;; Remove common tracking junk from the URL.
135 (when (re-search-forward ".utm_.*" nil t)
136 (replace-match "" t t))
137 (message "Copied %s" (buffer-string))
138 (copy-region-as-kill (point-min) (point-max)))))))
139 ;; Copy the URL to the kill ring.
140 (t
141 (with-temp-buffer
142 (insert url)
143 (copy-region-as-kill (point-min) (point-max))
144 (message "Copied %s" url))))))
145
146(defun shr-show-alt-text ()
147 "Show the ALT text of the image under point."
148 (interactive)
149 (let ((text (get-text-property (point) 'shr-alt)))
150 (if (not text)
151 (message "No image under point")
152 (message "%s" text))))
153
154(defun shr-browse-image ()
155 "Browse the image under point."
156 (interactive)
8b6f6573 157 (let ((url (get-text-property (point) 'image-url)))
66627fa9
G
158 (if (not url)
159 (message "No image under point")
160 (message "Browsing %s..." url)
161 (browse-url url))))
162
3d319c8f
LMI
163(defun shr-insert-image ()
164 "Insert the image under point into the buffer."
165 (interactive)
8b6f6573 166 (let ((url (get-text-property (point) 'image-url)))
3d319c8f
LMI
167 (if (not url)
168 (message "No image under point")
169 (message "Inserting %s..." url)
170 (url-retrieve url 'shr-image-fetched
171 (list (current-buffer) (1- (point)) (point-marker))
172 t))))
173
66627fa9
G
174;;; Utility functions.
175
870409d4
G
176(defun shr-transform-dom (dom)
177 (let ((result (list (pop dom))))
178 (dolist (arg (pop dom))
179 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
180 (cdr arg))
181 result))
182 (dolist (sub dom)
183 (if (stringp sub)
953d41c4 184 (push (cons 'text sub) result)
870409d4
G
185 (push (shr-transform-dom sub) result)))
186 (nreverse result)))
187
870409d4 188(defun shr-descend (dom)
a41c2e6d 189 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
870409d4
G
190 (if (fboundp function)
191 (funcall function (cdr dom))
192 (shr-generic (cdr dom)))))
193
194(defun shr-generic (cont)
195 (dolist (sub cont)
196 (cond
953d41c4 197 ((eq (car sub) 'text)
870409d4 198 (shr-insert (cdr sub)))
a41c2e6d 199 ((listp (cdr sub))
870409d4
G
200 (shr-descend sub)))))
201
66627fa9 202(defun shr-insert (text)
6b7df8d3
G
203 (when (and (eq shr-state 'image)
204 (not (string-match "\\`[ \t\n]+\\'" text)))
66627fa9
G
205 (insert "\n")
206 (setq shr-state nil))
207 (cond
208 ((eq shr-folding-mode 'none)
209 (insert text))
210 (t
73db8b08
KY
211 (when (and (string-match "\\`[ \t\n]" text)
212 (not (bolp))
213 (not (eq (char-after (1- (point))) ? )))
214 (insert " "))
215 (dolist (elem (split-string text))
216 (when (and (bolp)
217 (> shr-indentation 0))
218 (shr-indent))
219 ;; The shr-start is a special variable that is used to pass
220 ;; upwards the first point in the buffer where the text really
221 ;; starts.
222 (unless shr-start
223 (setq shr-start (point)))
224 ;; No space is needed behind a wide character categorized as
b41c2f65
KY
225 ;; kinsoku-bol, between characters both categorized as nospace,
226 ;; or at the beginning of a line.
73db8b08 227 (let (prev)
20438017
KY
228 (when (and (eq (preceding-char) ? )
229 (or (= (line-beginning-position) (1- (point)))
73db8b08
KY
230 (and (aref fill-find-break-point-function-table
231 (setq prev (char-after (- (point) 2))))
232 (aref (char-category-set prev) ?>))
233 (and (aref fill-nospace-between-words-table prev)
234 (aref fill-nospace-between-words-table
235 (aref elem 0)))))
236 (delete-char -1)))
237 (insert elem)
e7102c0a
KY
238 (let (found)
239 (while (and (> (current-column) shr-width)
240 (progn
241 (setq found (shr-find-fill-point))
242 (not (eolp))))
fe98a42f
KY
243 (when (eq (preceding-char) ? )
244 (delete-char -1))
245 (insert "\n")
246 (unless found
e7102c0a
KY
247 (put-text-property (1- (point)) (point) 'shr-break t)
248 ;; No space is needed at the beginning of a line.
249 (when (eq (following-char) ? )
250 (delete-char 1)))
251 (when (> shr-indentation 0)
252 (shr-indent))
253 (end-of-line))
254 (insert " ")))
73db8b08
KY
255 (unless (string-match "[ \t\n]\\'" text)
256 (delete-char -1)))))
66627fa9 257
6b7df8d3 258(defun shr-find-fill-point ()
83ffd571
KY
259 (when (> (move-to-column shr-width) shr-width)
260 (backward-char 1))
261 (let (failed)
262 (while (not
263 (or (setq failed (= (current-column) shr-indentation))
264 (eq (preceding-char) ? )
265 (eq (following-char) ? )
266 (aref fill-find-break-point-function-table (preceding-char))))
267 (backward-char 1))
268 (if failed
20438017 269 ;; There's no breakable point, so we give it up.
83ffd571
KY
270 (progn
271 (end-of-line)
272 (while (aref fill-find-break-point-function-table (preceding-char))
273 (backward-char 1))
274 nil)
275 (or (eolp)
276 ;; Don't put kinsoku-bol characters at the beginning of a line,
277 ;; or kinsoku-eol characters at the end of a line,
278 (let ((count 4))
9f47ba2d
KY
279 (if (or shr-kinsoku-shorten
280 (and (aref (char-category-set (preceding-char)) ?<)
281 (progn
282 (setq count (1- count))
283 (backward-char 1)
284 t)))
83ffd571 285 (while (and
9f47ba2d
KY
286 (>= (setq count (1- count)) 0)
287 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
83ffd571
KY
288 (or (aref (char-category-set (preceding-char)) ?<)
289 (aref (char-category-set (following-char)) ?>)))
290 (backward-char 1))
9f47ba2d 291 (while (and (>= (setq count (1- count)) 0)
389b76fa
G
292 (aref (char-category-set (following-char)) ?>)
293 (aref fill-find-break-point-function-table
294 (following-char)))
83ffd571 295 (forward-char 1)))
b643306f
LMI
296 (when (eq (following-char) ? )
297 (forward-char 1))
83ffd571 298 t)))))
6b7df8d3 299
66627fa9
G
300(defun shr-ensure-newline ()
301 (unless (zerop (current-column))
302 (insert "\n")))
a41c2e6d
G
303
304(defun shr-ensure-paragraph ()
305 (unless (bobp)
f7aa248a 306 (if (<= (current-column) shr-indentation)
71e691a5
G
307 (unless (save-excursion
308 (forward-line -1)
309 (looking-at " *$"))
a41c2e6d
G
310 (insert "\n"))
311 (if (save-excursion
312 (beginning-of-line)
5d2ef6db 313 (looking-at " *$"))
a41c2e6d
G
314 (insert "\n")
315 (insert "\n\n")))))
316
66627fa9 317(defun shr-indent ()
f7aa248a
G
318 (when (> shr-indentation 0)
319 (insert (make-string shr-indentation ? ))))
870409d4 320
a41c2e6d 321(defun shr-fontize-cont (cont &rest types)
870409d4
G
322 (let (shr-start)
323 (shr-generic cont)
a41c2e6d
G
324 (dolist (type types)
325 (shr-add-font (or shr-start (point)) (point) type))))
870409d4 326
6c85a14f
LMI
327;; Add an overlay in the region, but avoid putting the font properties
328;; on blank text at the start of the line, and the newline at the end,
329;; to avoid ugliness.
870409d4 330(defun shr-add-font (start end type)
6c85a14f
LMI
331 (save-excursion
332 (goto-char start)
333 (while (< (point) end)
334 (when (bolp)
335 (skip-chars-forward " "))
336 (let ((overlay (make-overlay (point) (min (line-end-position) end))))
337 (overlay-put overlay 'face type))
338 (if (< (line-end-position) end)
339 (forward-line 1)
340 (goto-char end)))))
870409d4 341
71e691a5
G
342(defun shr-browse-url ()
343 "Browse the URL under point."
344 (interactive)
345 (let ((url (get-text-property (point) 'shr-url)))
181cb5fb
G
346 (cond
347 ((not url)
348 (message "No link under point"))
349 ((string-match "^mailto:" url)
be3c11b3 350 (browse-url-mailto url))
181cb5fb
G
351 (t
352 (browse-url url)))))
71e691a5 353
cdf1fca4
LMI
354(defun shr-save-contents (directory)
355 "Save the contents from URL in a file."
356 (interactive "DSave contents of URL to directory: ")
357 (let ((url (get-text-property (point) 'shr-url)))
358 (if (not url)
359 (message "No link under point")
360 (url-retrieve (shr-encode-url url)
361 'shr-store-contents (list url directory)))))
362
363(defun shr-store-contents (status url directory)
364 (unless (plist-get status :error)
365 (when (or (search-forward "\n\n" nil t)
366 (search-forward "\r\n\r\n" nil t))
367 (write-region (point) (point-max)
368 (expand-file-name (file-name-nondirectory url)
369 directory)))))
370
870409d4
G
371(defun shr-image-fetched (status buffer start end)
372 (when (and (buffer-name buffer)
373 (not (plist-get status :error)))
374 (url-store-in-cache (current-buffer))
375 (when (or (search-forward "\n\n" nil t)
376 (search-forward "\r\n\r\n" nil t))
377 (let ((data (buffer-substring (point) (point-max))))
378 (with-current-buffer buffer
379 (let ((alt (buffer-substring start end))
380 (inhibit-read-only t))
381 (delete-region start end)
99e65b2d
G
382 (goto-char start)
383 (shr-put-image data alt))))))
870409d4
G
384 (kill-buffer (current-buffer)))
385
99e65b2d 386(defun shr-put-image (data alt)
4abff904
JD
387 (if (display-graphic-p)
388 (let ((image (ignore-errors
389 (shr-rescale-image data))))
390 (when image
99e65b2d
G
391 (insert-image image (or alt "*"))))
392 (insert alt)))
870409d4
G
393
394(defun shr-rescale-image (data)
395 (if (or (not (fboundp 'imagemagick-types))
396 (not (get-buffer-window (current-buffer))))
397 (create-image data nil t)
398 (let* ((image (create-image data nil t))
a41c2e6d 399 (size (image-size image t))
870409d4
G
400 (width (car size))
401 (height (cdr size))
402 (edges (window-inside-pixel-edges
403 (get-buffer-window (current-buffer))))
404 (window-width (truncate (* shr-max-image-proportion
405 (- (nth 2 edges) (nth 0 edges)))))
406 (window-height (truncate (* shr-max-image-proportion
407 (- (nth 3 edges) (nth 1 edges)))))
408 scaled-image)
409 (when (> height window-height)
410 (setq image (or (create-image data 'imagemagick t
411 :height window-height)
412 image))
413 (setq size (image-size image t)))
414 (when (> (car size) window-width)
415 (setq image (or
416 (create-image data 'imagemagick t
417 :width window-width)
418 image)))
419 image)))
420
85a45a69
GM
421;; url-cache-extract autoloads url-cache.
422(declare-function url-cache-create-filename "url-cache" (url))
423(autoload 'mm-disable-multibyte "mm-util")
be3c11b3 424(autoload 'browse-url-mailto "browse-url")
85a45a69 425
870409d4
G
426(defun shr-get-image-data (url)
427 "Get image data for URL.
428Return a string with image data."
429 (with-temp-buffer
430 (mm-disable-multibyte)
71e691a5 431 (when (ignore-errors
ab67634f 432 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
71e691a5
G
433 t)
434 (when (or (search-forward "\n\n" nil t)
435 (search-forward "\r\n\r\n" nil t))
436 (buffer-substring (point) (point-max))))))
870409d4 437
40de2c6d
KY
438(defun shr-image-displayer (content-function)
439 "Return a function to display an image.
440CONTENT-FUNCTION is a function to retrieve an image for a cid url that
441is an argument. The function to be returned takes three arguments URL,
442START, and END."
443 `(lambda (url start end)
f8d8a97b
KY
444 (when url
445 (if (string-match "\\`cid:" url)
446 ,(when content-function
447 `(let ((image (funcall ,content-function
448 (substring url (match-end 0)))))
449 (when image
450 (goto-char start)
451 (shr-put-image image
452 (prog1
453 (buffer-substring-no-properties start end)
454 (delete-region start end))))))
455 (url-retrieve url 'shr-image-fetched
456 (list (current-buffer) start end)
457 t)))))
40de2c6d 458
66627fa9
G
459(defun shr-heading (cont &rest types)
460 (shr-ensure-paragraph)
461 (apply #'shr-fontize-cont cont types)
462 (shr-ensure-paragraph))
463
85a45a69
GM
464(autoload 'widget-convert-button "wid-edit")
465
de635afe
G
466(defun shr-urlify (start url)
467 (widget-convert-button
468 'url-link start (point)
469 :help-echo url
470 :keymap shr-map
471 url)
472 (put-text-property start (point) 'shr-url url))
473
474(defun shr-encode-url (url)
475 "Encode URL."
476 (browse-url-url-encode-chars url "[)$ ]"))
477
66627fa9
G
478;;; Tag-specific rendering rules.
479
480(defun shr-tag-p (cont)
481 (shr-ensure-paragraph)
f7aa248a 482 (shr-indent)
66627fa9
G
483 (shr-generic cont)
484 (shr-ensure-paragraph))
485
036d93bc
KY
486(defun shr-tag-div (cont)
487 (shr-ensure-newline)
488 (shr-indent)
489 (shr-generic cont)
490 (shr-ensure-newline))
491
66627fa9
G
492(defun shr-tag-b (cont)
493 (shr-fontize-cont cont 'bold))
494
495(defun shr-tag-i (cont)
496 (shr-fontize-cont cont 'italic))
497
498(defun shr-tag-em (cont)
499 (shr-fontize-cont cont 'bold))
500
530f7b67
LMI
501(defun shr-tag-strong (cont)
502 (shr-fontize-cont cont 'bold))
503
66627fa9
G
504(defun shr-tag-u (cont)
505 (shr-fontize-cont cont 'underline))
506
507(defun shr-tag-s (cont)
508 (shr-fontize-cont cont 'strike-through))
509
2e76c12c
LMI
510(defun shr-parse-style (style)
511 (when style
512 (let ((plist nil))
513 (dolist (elem (split-string style ";"))
514 (when elem
515 (setq elem (split-string elem ":"))
516 (when (and (car elem)
517 (cadr elem))
518 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
519 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
520 (push (cons (intern name obarray)
521 value)
522 plist)))))
523 plist)))
524
66627fa9
G
525(defun shr-tag-a (cont)
526 (let ((url (cdr (assq :href cont)))
527 (start (point))
528 shr-start)
529 (shr-generic cont)
de635afe
G
530 (shr-urlify (or shr-start start) url)))
531
532(defun shr-tag-object (cont)
99e65b2d
G
533 (let ((start (point))
534 url)
535 (dolist (elem cont)
536 (when (eq (car elem) 'embed)
537 (setq url (or url (cdr (assq :src (cdr elem))))))
538 (when (and (eq (car elem) 'param)
539 (equal (cdr (assq :name (cdr elem))) "movie"))
540 (setq url (or url (cdr (assq :value (cdr elem)))))))
de635afe
G
541 (when url
542 (shr-insert " [multimedia] ")
99e65b2d
G
543 (shr-urlify start url))
544 (shr-generic cont)))
545
546(defun shr-tag-video (cont)
547 (let ((image (cdr (assq :poster cont)))
548 (url (cdr (assq :src cont)))
549 (start (point)))
550 (shr-tag-img nil image)
551 (shr-urlify start url)))
ab67634f 552
99e65b2d
G
553(defun shr-tag-img (cont &optional url)
554 (when (or url
555 (and cont
556 (cdr (assq :src cont))))
68f6bd17
KY
557 (when (and (> (current-column) 0)
558 (not (eq shr-state 'image)))
559 (insert "\n"))
560 (let ((alt (cdr (assq :alt cont)))
99e65b2d 561 (url (or url (cdr (assq :src cont)))))
68f6bd17
KY
562 (let ((start (point-marker)))
563 (when (zerop (length alt))
953d41c4 564 (setq alt "*"))
68f6bd17 565 (cond
99e65b2d
G
566 ((or (member (cdr (assq :height cont)) '("0" "1"))
567 (member (cdr (assq :width cont)) '("0" "1")))
568 ;; Ignore zero-sized or single-pixel images.
569 )
68f6bd17
KY
570 ((and (not shr-inhibit-images)
571 (string-match "\\`cid:" url))
572 (let ((url (substring url (match-end 0)))
573 image)
574 (if (or (not shr-content-function)
575 (not (setq image (funcall shr-content-function url))))
576 (insert alt)
99e65b2d 577 (shr-put-image image alt))))
68f6bd17
KY
578 ((or shr-inhibit-images
579 (and shr-blocked-images
580 (string-match shr-blocked-images url)))
581 (setq shr-start (point))
582 (let ((shr-state 'space))
b354bc53
KY
583 (if (> (string-width alt) 8)
584 (shr-insert (truncate-string-to-width alt 8))
68f6bd17
KY
585 (shr-insert alt))))
586 ((url-is-cached (shr-encode-url url))
99e65b2d 587 (shr-put-image (shr-get-image-data url) alt))
68f6bd17
KY
588 (t
589 (insert alt)
590 (ignore-errors
591 (url-retrieve (shr-encode-url url) 'shr-image-fetched
592 (list (current-buffer) start (point-marker))
593 t))))
68f6bd17
KY
594 (put-text-property start (point) 'keymap shr-map)
595 (put-text-property start (point) 'shr-alt alt)
8b6f6573
LMI
596 (put-text-property start (point) 'image-url url)
597 (put-text-property start (point) 'image-displayer
40de2c6d 598 (shr-image-displayer shr-content-function))
99e65b2d 599 (put-text-property start (point) 'help-echo alt)
68f6bd17 600 (setq shr-state 'image)))))
66627fa9
G
601
602(defun shr-tag-pre (cont)
603 (let ((shr-folding-mode 'none))
604 (shr-ensure-newline)
f7aa248a 605 (shr-indent)
66627fa9
G
606 (shr-generic cont)
607 (shr-ensure-newline)))
608
609(defun shr-tag-blockquote (cont)
610 (shr-ensure-paragraph)
f7aa248a 611 (shr-indent)
66627fa9
G
612 (let ((shr-indentation (+ shr-indentation 4)))
613 (shr-generic cont))
614 (shr-ensure-paragraph))
a41c2e6d
G
615
616(defun shr-tag-ul (cont)
617 (shr-ensure-paragraph)
618 (let ((shr-list-mode 'ul))
3d319c8f
LMI
619 (shr-generic cont))
620 (shr-ensure-paragraph))
a41c2e6d
G
621
622(defun shr-tag-ol (cont)
3d319c8f 623 (shr-ensure-paragraph)
a41c2e6d 624 (let ((shr-list-mode 1))
3d319c8f
LMI
625 (shr-generic cont))
626 (shr-ensure-paragraph))
a41c2e6d
G
627
628(defun shr-tag-li (cont)
f7aa248a
G
629 (shr-ensure-paragraph)
630 (shr-indent)
8028ed5c
LMI
631 (let* ((bullet
632 (if (numberp shr-list-mode)
633 (prog1
634 (format "%d " shr-list-mode)
635 (setq shr-list-mode (1+ shr-list-mode)))
636 "* "))
637 (shr-indentation (+ shr-indentation (length bullet))))
638 (insert bullet)
639 (shr-generic cont)))
a41c2e6d
G
640
641(defun shr-tag-br (cont)
1e463294 642 (unless (bobp)
f7aa248a
G
643 (insert "\n")
644 (shr-indent))
a41c2e6d
G
645 (shr-generic cont))
646
647(defun shr-tag-h1 (cont)
648 (shr-heading cont 'bold 'underline))
649
650(defun shr-tag-h2 (cont)
651 (shr-heading cont 'bold))
652
653(defun shr-tag-h3 (cont)
654 (shr-heading cont 'italic))
655
656(defun shr-tag-h4 (cont)
657 (shr-heading cont))
658
659(defun shr-tag-h5 (cont)
660 (shr-heading cont))
661
662(defun shr-tag-h6 (cont)
663 (shr-heading cont))
664
3d319c8f
LMI
665(defun shr-tag-hr (cont)
666 (shr-ensure-newline)
6b7df8d3 667 (insert (make-string shr-width shr-hr-line) "\n"))
3d319c8f 668
66627fa9 669;;; Table rendering algorithm.
a41c2e6d 670
a0ec382a
LMI
671;; Table rendering is the only complicated thing here. We do this by
672;; first counting how many TDs there are in each TR, and registering
673;; how wide they think they should be ("width=45%", etc). Then we
674;; render each TD separately (this is done in temporary buffers, so
675;; that we can use all the rendering machinery as if we were in the
676;; main buffer). Now we know how much space each TD really takes, so
677;; we then render everything again with the new widths, and finally
678;; insert all these boxes into the main buffer.
6c769311 679(defun shr-tag-table-1 (cont)
71e691a5
G
680 (setq cont (or (cdr (assq 'tbody cont))
681 cont))
130e977f 682 (let* ((shr-inhibit-images t)
99e65b2d 683 (shr-table-depth (1+ shr-table-depth))
83ffd571 684 (shr-kinsoku-shorten t)
a0ec382a 685 ;; Find all suggested widths.
130e977f 686 (columns (shr-column-specs cont))
a0ec382a 687 ;; Compute how many characters wide each TD should be.
71e691a5 688 (suggested-widths (shr-pro-rate-columns columns))
a0ec382a
LMI
689 ;; Do a "test rendering" to see how big each TD is (this can
690 ;; be smaller (if there's little text) or bigger (if there's
691 ;; unbreakable text).
71e691a5 692 (sketch (shr-make-table cont suggested-widths))
a7dcc87b 693 (sketch-widths (shr-table-widths sketch suggested-widths)))
030158f3 694 ;; This probably won't work very well.
83ffd571
KY
695 (when (> (+ (loop for width across sketch-widths
696 summing (1+ width))
697 shr-indentation 1)
030158f3
G
698 (frame-width))
699 (setq truncate-lines t))
a0ec382a 700 ;; Then render the table again with these new "hard" widths.
130e977f 701 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
a0ec382a
LMI
702 ;; Finally, insert all the images after the table. The Emacs buffer
703 ;; model isn't strong enough to allow us to put the images actually
704 ;; into the tables.
99e65b2d
G
705 (when (zerop shr-table-depth)
706 (dolist (elem (shr-find-elements cont 'img))
707 (shr-tag-img (cdr elem)))))
130e977f 708
6c769311
KY
709(defun shr-tag-table (cont)
710 (shr-ensure-paragraph)
711 (let* ((caption (cdr (assq 'caption cont)))
712 (header (cdr (assq 'thead cont)))
713 (body (or (cdr (assq 'tbody cont)) cont))
714 (footer (cdr (assq 'tfoot cont)))
715 (nheader (if header (shr-max-columns header)))
716 (nbody (if body (shr-max-columns body)))
717 (nfooter (if footer (shr-max-columns footer))))
718 (shr-tag-table-1
3c066373
KY
719 (nconc
720 (if caption `((tr (td ,@caption))))
721 (if header
722 (if footer
723 ;; hader + body + footer
724 (if (= nheader nbody)
725 (if (= nbody nfooter)
726 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
727 (nconc `((tr (td (table (tbody ,@header ,@body)))))
728 (if (= nfooter 1)
729 footer
730 `((tr (td (table (tbody ,@footer))))))))
731 (nconc `((tr (td (table (tbody ,@header)))))
732 (if (= nbody nfooter)
733 `((tr (td (table (tbody ,@body ,@footer)))))
734 (nconc `((tr (td (table (tbody ,@body)))))
735 (if (= nfooter 1)
736 footer
737 `((tr (td (table (tbody ,@footer))))))))))
738 ;; header + body
739 (if (= nheader nbody)
740 `((tr (td (table (tbody ,@header ,@body)))))
741 (if (= nheader 1)
742 `(,@header (tr (td (table (tbody ,@body)))))
743 `((tr (td (table (tbody ,@header))))
744 (tr (td (table (tbody ,@body))))))))
745 (if footer
746 ;; body + footer
747 (if (= nbody nfooter)
748 `((tr (td (table (tbody ,@body ,@footer)))))
749 (nconc `((tr (td (table (tbody ,@body)))))
2526f423 750 (if (= nfooter 1)
3c066373
KY
751 footer
752 `((tr (td (table (tbody ,@footer))))))))
753 (if caption
754 `((tr (td (table (tbody ,@body)))))
755 body)))))))
6c769311 756
130e977f
LMI
757(defun shr-find-elements (cont type)
758 (let (result)
759 (dolist (elem cont)
760 (cond ((eq (car elem) type)
761 (push elem result))
762 ((consp (cdr elem))
763 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
764 (nreverse result)))
71e691a5
G
765
766(defun shr-insert-table (table widths)
767 (shr-insert-table-ruler widths)
768 (dolist (row table)
769 (let ((start (point))
770 (height (let ((max 0))
771 (dolist (column row)
772 (setq max (max max (cadr column))))
773 max)))
774 (dotimes (i height)
775 (shr-indent)
776 (insert "|\n"))
777 (dolist (column row)
778 (goto-char start)
a7dcc87b 779 (let ((lines (nth 2 column))
130e977f
LMI
780 (overlay-lines (nth 3 column))
781 overlay overlay-line)
71e691a5 782 (dolist (line lines)
130e977f 783 (setq overlay-line (pop overlay-lines))
3d319c8f
LMI
784 (end-of-line)
785 (insert line "|")
786 (dolist (overlay overlay-line)
787 (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
788 (- (point) (nth 1 overlay) 1)))
789 (properties (nth 2 overlay)))
790 (while properties
791 (overlay-put o (pop properties) (pop properties)))))
792 (forward-line 1))
71e691a5
G
793 ;; Add blank lines at padding at the bottom of the TD,
794 ;; possibly.
795 (dotimes (i (- height (length lines)))
796 (end-of-line)
83ffd571 797 (insert (make-string (string-width (car lines)) ? ) "|")
71e691a5
G
798 (forward-line 1)))))
799 (shr-insert-table-ruler widths)))
800
801(defun shr-insert-table-ruler (widths)
83ffd571
KY
802 (when (and (bolp)
803 (> shr-indentation 0))
804 (shr-indent))
afba0c4b 805 (insert shr-table-corner)
71e691a5 806 (dotimes (i (length widths))
afba0c4b 807 (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
71e691a5
G
808 (insert "\n"))
809
a7dcc87b
G
810(defun shr-table-widths (table suggested-widths)
811 (let* ((length (length suggested-widths))
812 (widths (make-vector length 0))
813 (natural-widths (make-vector length 0)))
71e691a5
G
814 (dolist (row table)
815 (let ((i 0))
816 (dolist (column row)
817 (aset widths i (max (aref widths i)
818 (car column)))
a7dcc87b
G
819 (aset natural-widths i (max (aref natural-widths i)
820 (cadr column)))
821 (setq i (1+ i)))))
863b61d6
KY
822 (let ((extra (- (apply '+ (append suggested-widths nil))
823 (apply '+ (append widths nil))))
a7dcc87b
G
824 (expanded-columns 0))
825 (when (> extra 0)
826 (dotimes (i length)
827 ;; If the natural width is wider than the rendered width, we
828 ;; want to allow the column to expand.
829 (when (> (aref natural-widths i) (aref widths i))
830 (setq expanded-columns (1+ expanded-columns))))
831 (dotimes (i length)
832 (when (> (aref natural-widths i) (aref widths i))
833 (aset widths i (min
834 (1+ (aref natural-widths i))
835 (+ (/ extra expanded-columns)
836 (aref widths i))))))))
71e691a5
G
837 widths))
838
839(defun shr-make-table (cont widths &optional fill)
840 (let ((trs nil))
841 (dolist (row cont)
842 (when (eq (car row) 'tr)
a0ec382a
LMI
843 (let ((tds nil)
844 (columns (cdr row))
845 (i 0)
846 column)
847 (while (< i (length widths))
848 (setq column (pop columns))
849 (when (or (memq (car column) '(td th))
850 (null column))
71e691a5
G
851 (push (shr-render-td (cdr column) (aref widths i) fill)
852 tds)
853 (setq i (1+ i))))
854 (push (nreverse tds) trs))))
855 (nreverse trs)))
856
857(defun shr-render-td (cont width fill)
858 (with-temp-buffer
3d319c8f
LMI
859 (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
860 (if cache
861 (insert cache)
862 (let ((shr-width width)
863 (shr-indentation 0))
864 (shr-generic cont))
865 (delete-region
866 (point)
867 (+ (point)
868 (skip-chars-backward " \t\n")))
869 (push (cons (cons width cont) (buffer-string))
870 shr-content-cache)))
71e691a5
G
871 (goto-char (point-min))
872 (let ((max 0))
873 (while (not (eobp))
874 (end-of-line)
875 (setq max (max max (current-column)))
876 (forward-line 1))
877 (when fill
878 (goto-char (point-min))
a0ec382a
LMI
879 ;; If the buffer is totally empty, then put a single blank
880 ;; line here.
881 (if (zerop (buffer-size))
882 (insert (make-string width ? ))
883 ;; Otherwise, fill the buffer.
884 (while (not (eobp))
885 (end-of-line)
886 (when (> (- width (current-column)) 0)
887 (insert (make-string (- width (current-column)) ? )))
888 (forward-line 1))))
a7dcc87b
G
889 (if fill
890 (list max
891 (count-lines (point-min) (point-max))
892 (split-string (buffer-string) "\n")
893 (shr-collect-overlays))
894 (list max
895 (shr-natural-width))))))
896
897(defun shr-natural-width ()
898 (goto-char (point-min))
899 (let ((current 0)
900 (max 0))
901 (while (not (eobp))
902 (end-of-line)
903 (setq current (+ current (current-column)))
904 (unless (get-text-property (point) 'shr-break)
905 (setq max (max max current)
906 current 0))
907 (forward-line 1))
908 max))
130e977f
LMI
909
910(defun shr-collect-overlays ()
911 (save-excursion
912 (goto-char (point-min))
913 (let ((overlays nil))
914 (while (not (eobp))
915 (push (shr-overlays-in-region (point) (line-end-position))
916 overlays)
917 (forward-line 1))
918 (nreverse overlays))))
919
920(defun shr-overlays-in-region (start end)
921 (let (result)
922 (dolist (overlay (overlays-in start end))
923 (push (list (if (> start (overlay-start overlay))
924 (- end start)
925 (- end (overlay-start overlay)))
926 (if (< end (overlay-end overlay))
927 0
928 (- end (overlay-end overlay)))
929 (overlay-properties overlay))
930 result))
931 (nreverse result)))
71e691a5
G
932
933(defun shr-pro-rate-columns (columns)
934 (let ((total-percentage 0)
935 (widths (make-vector (length columns) 0)))
936 (dotimes (i (length columns))
a7dcc87b 937 (setq total-percentage (+ total-percentage (aref columns i))))
71e691a5
G
938 (setq total-percentage (/ 1.0 total-percentage))
939 (dotimes (i (length columns))
940 (aset widths i (max (truncate (* (aref columns i)
941 total-percentage
a7dcc87b 942 (- shr-width (1+ (length columns)))))
71e691a5
G
943 10)))
944 widths))
945
946;; Return a summary of the number and shape of the TDs in the table.
947(defun shr-column-specs (cont)
948 (let ((columns (make-vector (shr-max-columns cont) 1)))
949 (dolist (row cont)
950 (when (eq (car row) 'tr)
951 (let ((i 0))
952 (dolist (column (cdr row))
953 (when (memq (car column) '(td th))
954 (let ((width (cdr (assq :width (cdr column)))))
955 (when (and width
956 (string-match "\\([0-9]+\\)%" width))
957 (aset columns i
958 (/ (string-to-number (match-string 1 width))
130e977f
LMI
959 100.0))))
960 (setq i (1+ i)))))))
71e691a5
G
961 columns))
962
963(defun shr-count (cont elem)
964 (let ((i 0))
965 (dolist (sub cont)
966 (when (eq (car sub) elem)
967 (setq i (1+ i))))
968 i))
969
970(defun shr-max-columns (cont)
971 (let ((max 0))
972 (dolist (row cont)
973 (when (eq (car row) 'tr)
130e977f
LMI
974 (setq max (max max (+ (shr-count (cdr row) 'td)
975 (shr-count (cdr row) 'th))))))
71e691a5
G
976 max))
977
f3fd95db 978(provide 'shr)
367f7f81
LMI
979
980;;; shr.el ends here