* shr.el (shr-tag-img): Don't align images.
[gnus] / lisp / shr.el
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
33 (eval-when-compile (require 'cl))
34 (require 'browse-url)
35
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.
42 A value of 0.7 means that they are allowed to take up 70% of the
43 width and height of the window.  If they are larger than this,
44 and Emacs supports it, then the images will be rescaled down to
45 fit 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
56 (defcustom shr-table-line ?-
57   "Character used to draw table line."
58   :group 'shr
59   :type 'character)
60
61 (defcustom shr-table-corner ?+
62   "Character used to draw table corner."
63   :group 'shr
64   :type 'character)
65
66 (defcustom shr-hr-line ?-
67   "Character used to draw hr line."
68   :group 'shr
69   :type 'character)
70
71 (defcustom shr-width fill-column
72   "Frame width to use for rendering."
73   :type 'integer
74   :group 'shr)
75
76 (defvar shr-content-function nil
77   "If bound, this should be a function that will return the content.
78 This is used for cid: URLs, and the function is called with the
79 cid: URL as the argument.")
80
81 ;;; Internal variables.
82
83 (defvar shr-folding-mode nil)
84 (defvar shr-state nil)
85 (defvar shr-start nil)
86 (defvar shr-indentation 0)
87 (defvar shr-inhibit-images nil)
88 (defvar shr-list-mode nil)
89 (defvar shr-content-cache nil)
90
91 (defvar shr-map
92   (let ((map (make-sparse-keymap)))
93     (define-key map "a" 'shr-show-alt-text)
94     (define-key map "i" 'shr-browse-image)
95     (define-key map "I" 'shr-insert-image)
96     (define-key map "u" 'shr-copy-url)
97     (define-key map "v" 'shr-browse-url)
98     (define-key map "\r" 'shr-browse-url)
99     map))
100
101 ;; Public functions and commands.
102
103 ;;;###autoload
104 (defun shr-insert-document (dom)
105   (setq shr-content-cache nil)
106   (let ((shr-state nil)
107         (shr-start nil))
108     (shr-descend (shr-transform-dom dom))))
109
110 (defun shr-copy-url ()
111   "Copy the URL under point to the kill ring.
112 If called twice, then try to fetch the URL and see whether it
113 redirects somewhere else."
114   (interactive)
115   (let ((url (get-text-property (point) 'shr-url)))
116     (cond
117      ((not url)
118       (message "No URL under point"))
119      ;; Resolve redirected URLs.
120      ((equal url (car kill-ring))
121       (url-retrieve
122        url
123        (lambda (a)
124          (when (and (consp a)
125                     (eq (car a) :redirect))
126            (with-temp-buffer
127              (insert (cadr a))
128              (goto-char (point-min))
129              ;; Remove common tracking junk from the URL.
130              (when (re-search-forward ".utm_.*" nil t)
131                (replace-match "" t t))
132              (message "Copied %s" (buffer-string))
133              (copy-region-as-kill (point-min) (point-max)))))))
134      ;; Copy the URL to the kill ring.
135      (t
136       (with-temp-buffer
137         (insert url)
138         (copy-region-as-kill (point-min) (point-max))
139         (message "Copied %s" url))))))
140
141 (defun shr-show-alt-text ()
142   "Show the ALT text of the image under point."
143   (interactive)
144   (let ((text (get-text-property (point) 'shr-alt)))
145     (if (not text)
146         (message "No image under point")
147       (message "%s" text))))
148
149 (defun shr-browse-image ()
150   "Browse the image under point."
151   (interactive)
152   (let ((url (get-text-property (point) 'shr-image)))
153     (if (not url)
154         (message "No image under point")
155       (message "Browsing %s..." url)
156       (browse-url url))))
157
158 (defun shr-insert-image ()
159   "Insert the image under point into the buffer."
160   (interactive)
161   (let ((url (get-text-property (point) 'shr-image)))
162     (if (not url)
163         (message "No image under point")
164       (message "Inserting %s..." url)
165       (url-retrieve url 'shr-image-fetched
166                     (list (current-buffer) (1- (point)) (point-marker))
167                     t))))
168
169 ;;; Utility functions.
170
171 (defun shr-transform-dom (dom)
172   (let ((result (list (pop dom))))
173     (dolist (arg (pop dom))
174       (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
175                   (cdr arg))
176             result))
177     (dolist (sub dom)
178       (if (stringp sub)
179           (push (cons :text sub) result)
180         (push (shr-transform-dom sub) result)))
181     (nreverse result)))
182
183 (defun shr-descend (dom)
184   (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
185     (if (fboundp function)
186         (funcall function (cdr dom))
187       (shr-generic (cdr dom)))))
188
189 (defun shr-generic (cont)
190   (dolist (sub cont)
191     (cond
192      ((eq (car sub) :text)
193       (shr-insert (cdr sub)))
194      ((listp (cdr sub))
195       (shr-descend sub)))))
196
197 (defun shr-insert (text)
198   (when (and (eq shr-state 'image)
199              (not (string-match "\\`[ \t\n]+\\'" text)))
200     (insert "\n")
201     (setq shr-state nil))
202   (cond
203    ((eq shr-folding-mode 'none)
204     (insert text))
205    (t
206     (let ((first t)
207           column)
208       (when (and (string-match "\\`[ \t\n]" text)
209                  (not (bolp))
210                  (not (eq (char-after (1- (point))) ? )))
211         (insert " "))
212       (dolist (elem (split-string text))
213         (when (and (bolp)
214                    (> shr-indentation 0))
215           (shr-indent))
216         ;; The shr-start is a special variable that is used to pass
217         ;; upwards the first point in the buffer where the text really
218         ;; starts.
219         (unless shr-start
220           (setq shr-start (point)))
221         ;; No space is needed before or after a breakable character or
222         ;; at the beginning of a line.
223         (when (and (eq (preceding-char) ? )
224                    (or (= (line-beginning-position) (1- (point)))
225                        (aref fill-find-break-point-function-table
226                              (char-after (- (point) 2)))
227                        (aref fill-find-break-point-function-table
228                              (aref elem 0))))
229           (delete-char -1))
230         (insert elem)
231         (while (> (current-column) shr-width)
232           (unless (prog1
233                       (shr-find-fill-point)
234                     (when (eq (preceding-char) ? )
235                       (delete-char -1))
236                     (insert "\n"))
237             (put-text-property (1- (point)) (point) 'shr-break t)
238             ;; No space is needed at the beginning of a line.
239             (if (eq (following-char) ? )
240                 (delete-char 1)))
241           (when (> shr-indentation 0)
242             (shr-indent))
243           (end-of-line))
244         (insert " "))
245       (unless (string-match "[ \t\n]\\'" text)
246         (delete-char -1))))))
247
248 (eval-and-compile (autoload 'kinsoku-longer "kinsoku"))
249
250 (defun shr-find-fill-point ()
251   (let ((found nil))
252     (while (and (not found)
253                 (> (current-column) shr-indentation))
254       (when (and (or (eq (preceding-char) ? )
255                      (aref fill-find-break-point-function-table
256                            (preceding-char)))
257                  (<= (current-column) shr-width))
258         (setq found t))
259       (backward-char 1)
260       (when (bolp)
261         ;; There's no breakable point, so we give it up.
262         (end-of-line)
263         (while (aref fill-find-break-point-function-table
264                      (preceding-char))
265           (backward-char 1))
266         (setq found 'failed)))
267     (cond ((eq found t)
268            ;; Don't put kinsoku-bol characters at the beginning of a line.
269            (or (eobp)
270                (kinsoku-longer)
271                (not (aref fill-find-break-point-function-table
272                           (following-char)))
273                (forward-char 1)))
274           (found t)
275           (t
276            (end-of-line)
277            nil))))
278
279 (defun shr-ensure-newline ()
280   (unless (zerop (current-column))
281     (insert "\n")))
282
283 (defun shr-ensure-paragraph ()
284   (unless (bobp)
285     (if (<= (current-column) shr-indentation)
286         (unless (save-excursion
287                   (forward-line -1)
288                   (looking-at " *$"))
289           (insert "\n"))
290       (if (save-excursion
291             (beginning-of-line)
292             (looking-at " *$"))
293           (insert "\n")
294         (insert "\n\n")))))
295
296 (defun shr-indent ()
297   (when (> shr-indentation 0)
298     (insert (make-string shr-indentation ? ))))
299
300 (defun shr-fontize-cont (cont &rest types)
301   (let (shr-start)
302     (shr-generic cont)
303     (dolist (type types)
304       (shr-add-font (or shr-start (point)) (point) type))))
305
306 (defun shr-add-font (start end type)
307   (let ((overlay (make-overlay start end)))
308     (overlay-put overlay 'face type)))
309
310 (defun shr-browse-url ()
311   "Browse the URL under point."
312   (interactive)
313   (let ((url (get-text-property (point) 'shr-url)))
314     (if (not url)
315         (message "No link under point")
316       (browse-url url))))
317
318 (defun shr-image-fetched (status buffer start end)
319   (when (and (buffer-name buffer)
320              (not (plist-get status :error)))
321     (url-store-in-cache (current-buffer))
322     (when (or (search-forward "\n\n" nil t)
323               (search-forward "\r\n\r\n" nil t))
324       (let ((data (buffer-substring (point) (point-max))))
325         (with-current-buffer buffer
326           (let ((alt (buffer-substring start end))
327                 (inhibit-read-only t))
328             (delete-region start end)
329             (shr-put-image data start alt))))))
330   (kill-buffer (current-buffer)))
331
332 (defun shr-put-image (data point alt)
333   (if (not (display-graphic-p))
334       (insert alt)
335     (let ((image (ignore-errors
336                    (shr-rescale-image data))))
337       (when image
338         (put-image image point alt)))))
339
340 (defun shr-rescale-image (data)
341   (if (or (not (fboundp 'imagemagick-types))
342           (not (get-buffer-window (current-buffer))))
343       (create-image data nil t)
344     (let* ((image (create-image data nil t))
345            (size (image-size image t))
346            (width (car size))
347            (height (cdr size))
348            (edges (window-inside-pixel-edges
349                    (get-buffer-window (current-buffer))))
350            (window-width (truncate (* shr-max-image-proportion
351                                       (- (nth 2 edges) (nth 0 edges)))))
352            (window-height (truncate (* shr-max-image-proportion
353                                        (- (nth 3 edges) (nth 1 edges)))))
354            scaled-image)
355       (when (> height window-height)
356         (setq image (or (create-image data 'imagemagick t
357                                       :height window-height)
358                         image))
359         (setq size (image-size image t)))
360       (when (> (car size) window-width)
361         (setq image (or
362                      (create-image data 'imagemagick t
363                                    :width window-width)
364                      image)))
365       image)))
366
367 (defun shr-get-image-data (url)
368   "Get image data for URL.
369 Return a string with image data."
370   (with-temp-buffer
371     (mm-disable-multibyte)
372     (when (ignore-errors
373             (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
374             t)
375       (when (or (search-forward "\n\n" nil t)
376                 (search-forward "\r\n\r\n" nil t))
377         (buffer-substring (point) (point-max))))))
378
379 (defun shr-heading (cont &rest types)
380   (shr-ensure-paragraph)
381   (apply #'shr-fontize-cont cont types)
382   (shr-ensure-paragraph))
383
384 ;;; Tag-specific rendering rules.
385
386 (defun shr-tag-p (cont)
387   (shr-ensure-paragraph)
388   (shr-indent)
389   (shr-generic cont)
390   (shr-ensure-paragraph))
391
392 (defun shr-tag-b (cont)
393   (shr-fontize-cont cont 'bold))
394
395 (defun shr-tag-i (cont)
396   (shr-fontize-cont cont 'italic))
397
398 (defun shr-tag-em (cont)
399   (shr-fontize-cont cont 'bold))
400
401 (defun shr-tag-u (cont)
402   (shr-fontize-cont cont 'underline))
403
404 (defun shr-tag-s (cont)
405   (shr-fontize-cont cont 'strike-through))
406
407 (defun shr-tag-a (cont)
408   (let ((url (cdr (assq :href cont)))
409         (start (point))
410         shr-start)
411     (shr-generic cont)
412     (widget-convert-button
413      'url-link (or shr-start start) (point)
414      :help-echo url
415      :keymap shr-map
416      url)
417     (put-text-property (or shr-start start) (point) 'shr-url url)))
418
419 (defun shr-encode-url (url)
420   "Encode URL."
421   (browse-url-url-encode-chars url "[)$ ]"))
422
423 (defun shr-tag-img (cont)
424   (when cont
425     (when (and (> (current-column) 0)
426                (not (eq shr-state 'image)))
427       (insert "\n"))
428     (let ((alt (cdr (assq :alt cont)))
429           (url (cdr (assq :src cont))))
430       (let ((start (point-marker)))
431         (when (zerop (length alt))
432           (setq alt "[img]"))
433         (cond
434          ((and (not shr-inhibit-images)
435                (string-match "\\`cid:" url))
436           (let ((url (substring url (match-end 0)))
437                 image)
438             (if (or (not shr-content-function)
439                     (not (setq image (funcall shr-content-function url))))
440                 (insert alt)
441               (shr-put-image image (point) alt))))
442          ((or shr-inhibit-images
443               (and shr-blocked-images
444                    (string-match shr-blocked-images url)))
445           (setq shr-start (point))
446           (let ((shr-state 'space))
447             (if (> (length alt) 8)
448                 (shr-insert (substring alt 0 8))
449               (shr-insert alt))))
450          ((url-is-cached (shr-encode-url url))
451           (shr-put-image (shr-get-image-data url) (point) alt))
452          (t
453           (insert alt)
454           (ignore-errors
455             (url-retrieve (shr-encode-url url) 'shr-image-fetched
456                           (list (current-buffer) start (point-marker))
457                           t))))
458         (insert " ")
459         (put-text-property start (point) 'keymap shr-map)
460         (put-text-property start (point) 'shr-alt alt)
461         (put-text-property start (point) 'shr-image url)
462         (setq shr-state 'image)))))
463
464 (defun shr-tag-pre (cont)
465   (let ((shr-folding-mode 'none))
466     (shr-ensure-newline)
467     (shr-indent)
468     (shr-generic cont)
469     (shr-ensure-newline)))
470
471 (defun shr-tag-blockquote (cont)
472   (shr-ensure-paragraph)
473   (shr-indent)
474   (let ((shr-indentation (+ shr-indentation 4)))
475     (shr-generic cont))
476   (shr-ensure-paragraph))
477
478 (defun shr-tag-ul (cont)
479   (shr-ensure-paragraph)
480   (let ((shr-list-mode 'ul))
481     (shr-generic cont))
482   (shr-ensure-paragraph))
483
484 (defun shr-tag-ol (cont)
485   (shr-ensure-paragraph)
486   (let ((shr-list-mode 1))
487     (shr-generic cont))
488   (shr-ensure-paragraph))
489
490 (defun shr-tag-li (cont)
491   (shr-ensure-paragraph)
492   (shr-indent)
493   (let* ((bullet
494           (if (numberp shr-list-mode)
495               (prog1
496                   (format "%d " shr-list-mode)
497                 (setq shr-list-mode (1+ shr-list-mode)))
498             "* "))
499          (shr-indentation (+ shr-indentation (length bullet))))
500     (insert bullet)
501     (shr-generic cont)))
502
503 (defun shr-tag-br (cont)
504   (unless (bobp)
505     (insert "\n")
506     (shr-indent))
507   (shr-generic cont))
508
509 (defun shr-tag-h1 (cont)
510   (shr-heading cont 'bold 'underline))
511
512 (defun shr-tag-h2 (cont)
513   (shr-heading cont 'bold))
514
515 (defun shr-tag-h3 (cont)
516   (shr-heading cont 'italic))
517
518 (defun shr-tag-h4 (cont)
519   (shr-heading cont))
520
521 (defun shr-tag-h5 (cont)
522   (shr-heading cont))
523
524 (defun shr-tag-h6 (cont)
525   (shr-heading cont))
526
527 (defun shr-tag-hr (cont)
528   (shr-ensure-newline)
529   (insert (make-string shr-width shr-hr-line) "\n"))
530
531 ;;; Table rendering algorithm.
532
533 ;; Table rendering is the only complicated thing here.  We do this by
534 ;; first counting how many TDs there are in each TR, and registering
535 ;; how wide they think they should be ("width=45%", etc).  Then we
536 ;; render each TD separately (this is done in temporary buffers, so
537 ;; that we can use all the rendering machinery as if we were in the
538 ;; main buffer).  Now we know how much space each TD really takes, so
539 ;; we then render everything again with the new widths, and finally
540 ;; insert all these boxes into the main buffer.
541 (defun shr-tag-table (cont)
542   (shr-ensure-paragraph)
543   (setq cont (or (cdr (assq 'tbody cont))
544                  cont))
545   (let* ((shr-inhibit-images t)
546          ;; Find all suggested widths.
547          (columns (shr-column-specs cont))
548          ;; Compute how many characters wide each TD should be.
549          (suggested-widths (shr-pro-rate-columns columns))
550          ;; Do a "test rendering" to see how big each TD is (this can
551          ;; be smaller (if there's little text) or bigger (if there's
552          ;; unbreakable text).
553          (sketch (shr-make-table cont suggested-widths))
554          (sketch-widths (shr-table-widths sketch suggested-widths)))
555     ;; This probably won't work very well.
556     (when (> (1+ (loop for width across sketch-widths
557                        summing (1+ width)))
558              (frame-width))
559       (setq truncate-lines t))
560     ;; Then render the table again with these new "hard" widths.
561     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
562   ;; Finally, insert all the images after the table.  The Emacs buffer
563   ;; model isn't strong enough to allow us to put the images actually
564   ;; into the tables.
565   (dolist (elem (shr-find-elements cont 'img))
566     (shr-tag-img (cdr elem))))
567
568 (defun shr-find-elements (cont type)
569   (let (result)
570     (dolist (elem cont)
571       (cond ((eq (car elem) type)
572              (push elem result))
573             ((consp (cdr elem))
574              (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
575     (nreverse result)))
576
577 (defun shr-insert-table (table widths)
578   (shr-insert-table-ruler widths)
579   (dolist (row table)
580     (let ((start (point))
581           (height (let ((max 0))
582                     (dolist (column row)
583                       (setq max (max max (cadr column))))
584                     max)))
585       (dotimes (i height)
586         (shr-indent)
587         (insert "|\n"))
588       (dolist (column row)
589         (goto-char start)
590         (let ((lines (nth 2 column))
591               (overlay-lines (nth 3 column))
592               overlay overlay-line)
593           (dolist (line lines)
594             (setq overlay-line (pop overlay-lines))
595             (end-of-line)
596             (insert line "|")
597             (dolist (overlay overlay-line)
598               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
599                                      (- (point) (nth 1 overlay) 1)))
600                     (properties (nth 2 overlay)))
601                 (while properties
602                   (overlay-put o (pop properties) (pop properties)))))
603             (forward-line 1))
604           ;; Add blank lines at padding at the bottom of the TD,
605           ;; possibly.
606           (dotimes (i (- height (length lines)))
607             (end-of-line)
608             (insert (make-string (length (car lines)) ? ) "|")
609             (forward-line 1)))))
610     (shr-insert-table-ruler widths)))
611
612 (defun shr-insert-table-ruler (widths)
613   (shr-indent)
614   (insert shr-table-corner)
615   (dotimes (i (length widths))
616     (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
617   (insert "\n"))
618
619 (defun shr-table-widths (table suggested-widths)
620   (let* ((length (length suggested-widths))
621          (widths (make-vector length 0))
622          (natural-widths (make-vector length 0)))
623     (dolist (row table)
624       (let ((i 0))
625         (dolist (column row)
626           (aset widths i (max (aref widths i)
627                               (car column)))
628           (aset natural-widths i (max (aref natural-widths i)
629                                       (cadr column)))
630           (setq i (1+ i)))))
631     (let ((extra (- (apply '+ (append suggested-widths nil))
632                     (apply '+ (append widths nil))))
633           (expanded-columns 0))
634       (when (> extra 0)
635         (dotimes (i length)
636           ;; If the natural width is wider than the rendered width, we
637           ;; want to allow the column to expand.
638           (when (> (aref natural-widths i) (aref widths i))
639             (setq expanded-columns (1+ expanded-columns))))
640         (dotimes (i length)
641           (when (> (aref natural-widths i) (aref widths i))
642             (aset widths i (min
643                             (1+ (aref natural-widths i))
644                             (+ (/ extra expanded-columns)
645                                (aref widths i))))))))
646     widths))
647
648 (defun shr-make-table (cont widths &optional fill)
649   (let ((trs nil))
650     (dolist (row cont)
651       (when (eq (car row) 'tr)
652         (let ((tds nil)
653               (columns (cdr row))
654               (i 0)
655               column)
656           (while (< i (length widths))
657             (setq column (pop columns))
658             (when (or (memq (car column) '(td th))
659                       (null column))
660               (push (shr-render-td (cdr column) (aref widths i) fill)
661                     tds)
662               (setq i (1+ i))))
663           (push (nreverse tds) trs))))
664     (nreverse trs)))
665
666 (defun shr-render-td (cont width fill)
667   (with-temp-buffer
668     (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
669       (if cache
670           (insert cache)
671         (let ((shr-width width)
672               (shr-indentation 0))
673           (shr-generic cont))
674         (delete-region
675          (point)
676          (+ (point)
677             (skip-chars-backward " \t\n")))
678         (push (cons (cons width cont) (buffer-string))
679               shr-content-cache)))
680     (goto-char (point-min))
681     (let ((max 0))
682       (while (not (eobp))
683         (end-of-line)
684         (setq max (max max (current-column)))
685         (forward-line 1))
686       (when fill
687         (goto-char (point-min))
688         ;; If the buffer is totally empty, then put a single blank
689         ;; line here.
690         (if (zerop (buffer-size))
691             (insert (make-string width ? ))
692           ;; Otherwise, fill the buffer.
693           (while (not (eobp))
694             (end-of-line)
695             (when (> (- width (current-column)) 0)
696               (insert (make-string (- width (current-column)) ? )))
697             (forward-line 1))))
698       (if fill
699           (list max
700                 (count-lines (point-min) (point-max))
701                 (split-string (buffer-string) "\n")
702                 (shr-collect-overlays))
703         (list max
704               (shr-natural-width))))))
705
706 (defun shr-natural-width ()
707   (goto-char (point-min))
708   (let ((current 0)
709         (max 0))
710     (while (not (eobp))
711       (end-of-line)
712       (setq current (+ current (current-column)))
713       (unless (get-text-property (point) 'shr-break)
714         (setq max (max max current)
715               current 0))
716       (forward-line 1))
717     max))
718
719 (defun shr-collect-overlays ()
720   (save-excursion
721     (goto-char (point-min))
722     (let ((overlays nil))
723       (while (not (eobp))
724         (push (shr-overlays-in-region (point) (line-end-position))
725               overlays)
726         (forward-line 1))
727       (nreverse overlays))))
728
729 (defun shr-overlays-in-region (start end)
730   (let (result)
731     (dolist (overlay (overlays-in start end))
732       (push (list (if (> start (overlay-start overlay))
733                       (- end start)
734                     (- end (overlay-start overlay)))
735                   (if (< end (overlay-end overlay))
736                       0
737                     (- end (overlay-end overlay)))
738                   (overlay-properties overlay))
739             result))
740     (nreverse result)))
741
742 (defun shr-pro-rate-columns (columns)
743   (let ((total-percentage 0)
744         (widths (make-vector (length columns) 0)))
745     (dotimes (i (length columns))
746       (setq total-percentage (+ total-percentage (aref columns i))))
747     (setq total-percentage (/ 1.0 total-percentage))
748     (dotimes (i (length columns))
749       (aset widths i (max (truncate (* (aref columns i)
750                                        total-percentage
751                                        (- shr-width (1+ (length columns)))))
752                           10)))
753     widths))
754
755 ;; Return a summary of the number and shape of the TDs in the table.
756 (defun shr-column-specs (cont)
757   (let ((columns (make-vector (shr-max-columns cont) 1)))
758     (dolist (row cont)
759       (when (eq (car row) 'tr)
760         (let ((i 0))
761           (dolist (column (cdr row))
762             (when (memq (car column) '(td th))
763               (let ((width (cdr (assq :width (cdr column)))))
764                 (when (and width
765                            (string-match "\\([0-9]+\\)%" width))
766                   (aset columns i
767                         (/ (string-to-number (match-string 1 width))
768                            100.0))))
769               (setq i (1+ i)))))))
770     columns))
771
772 (defun shr-count (cont elem)
773   (let ((i 0))
774     (dolist (sub cont)
775       (when (eq (car sub) elem)
776         (setq i (1+ i))))
777     i))
778
779 (defun shr-max-columns (cont)
780   (let ((max 0))
781     (dolist (row cont)
782       (when (eq (car row) 'tr)
783         (setq max (max max (+ (shr-count (cdr row) 'td)
784                               (shr-count (cdr row) 'th))))))
785     max))
786
787 (provide 'shr)
788
789 ;;; shr.el ends here