Use a filling algorithm that should probably work for CJVK text, too.
[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 'char)
60
61 (defcustom shr-table-corner ?+
62   "Character used to draw table corner."
63   :group 'shr
64   :type 'char)
65
66 (defcustom shr-hr-line ?-
67   "Character used to draw hr line."
68   :group 'shr
69   :type 'char)
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         (insert elem)
222         (while (> (current-column) shr-width)
223           (if (not (shr-find-fill-point))
224               (insert "\n")
225             (delete-char 1)
226             (insert "\n")
227             (put-text-property (1- (point)) (point) 'shr-break t)
228             (when (> shr-indentation 0)
229               (shr-indent))
230             (end-of-line)))
231         (insert " "))
232       (unless (string-match "[ \t\n]\\'" text)
233         (delete-char -1))))))
234
235 (defun shr-find-fill-point ()
236   (let ((found nil))
237     (while (and (not found)
238                 (not (bolp)))
239       (when (and (or (eq (preceding-char) ? )
240                      (aref fill-find-break-point-function-table
241                            (preceding-char)))
242                  (<= (current-column) shr-width))
243         (setq found (point)))
244       (backward-char 1))
245     (or found
246         (end-of-line))))
247
248 (defun shr-ensure-newline ()
249   (unless (zerop (current-column))
250     (insert "\n")))
251
252 (defun shr-ensure-paragraph ()
253   (unless (bobp)
254     (if (<= (current-column) shr-indentation)
255         (unless (save-excursion
256                   (forward-line -1)
257                   (looking-at " *$"))
258           (insert "\n"))
259       (if (save-excursion
260             (beginning-of-line)
261             (looking-at " *$"))
262           (insert "\n")
263         (insert "\n\n")))))
264
265 (defun shr-indent ()
266   (when (> shr-indentation 0)
267     (insert (make-string shr-indentation ? ))))
268
269 (defun shr-fontize-cont (cont &rest types)
270   (let (shr-start)
271     (shr-generic cont)
272     (dolist (type types)
273       (shr-add-font (or shr-start (point)) (point) type))))
274
275 (defun shr-add-font (start end type)
276   (let ((overlay (make-overlay start end)))
277     (overlay-put overlay 'face type)))
278
279 (defun shr-browse-url ()
280   "Browse the URL under point."
281   (interactive)
282   (let ((url (get-text-property (point) 'shr-url)))
283     (if (not url)
284         (message "No link under point")
285       (browse-url url))))
286
287 (defun shr-image-fetched (status buffer start end)
288   (when (and (buffer-name buffer)
289              (not (plist-get status :error)))
290     (url-store-in-cache (current-buffer))
291     (when (or (search-forward "\n\n" nil t)
292               (search-forward "\r\n\r\n" nil t))
293       (let ((data (buffer-substring (point) (point-max))))
294         (with-current-buffer buffer
295           (let ((alt (buffer-substring start end))
296                 (inhibit-read-only t))
297             (delete-region start end)
298             (shr-put-image data start alt))))))
299   (kill-buffer (current-buffer)))
300
301 (defun shr-put-image (data point alt)
302   (if (not (display-graphic-p))
303       (insert alt)
304     (let ((image (ignore-errors
305                    (shr-rescale-image data))))
306       (when image
307         (put-image image point alt)))))
308
309 (defun shr-rescale-image (data)
310   (if (or (not (fboundp 'imagemagick-types))
311           (not (get-buffer-window (current-buffer))))
312       (create-image data nil t)
313     (let* ((image (create-image data nil t))
314            (size (image-size image t))
315            (width (car size))
316            (height (cdr size))
317            (edges (window-inside-pixel-edges
318                    (get-buffer-window (current-buffer))))
319            (window-width (truncate (* shr-max-image-proportion
320                                       (- (nth 2 edges) (nth 0 edges)))))
321            (window-height (truncate (* shr-max-image-proportion
322                                        (- (nth 3 edges) (nth 1 edges)))))
323            scaled-image)
324       (when (> height window-height)
325         (setq image (or (create-image data 'imagemagick t
326                                       :height window-height)
327                         image))
328         (setq size (image-size image t)))
329       (when (> (car size) window-width)
330         (setq image (or
331                      (create-image data 'imagemagick t
332                                    :width window-width)
333                      image)))
334       image)))
335
336 (defun shr-get-image-data (url)
337   "Get image data for URL.
338 Return a string with image data."
339   (with-temp-buffer
340     (mm-disable-multibyte)
341     (when (ignore-errors
342             (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
343             t)
344       (when (or (search-forward "\n\n" nil t)
345                 (search-forward "\r\n\r\n" nil t))
346         (buffer-substring (point) (point-max))))))
347
348 (defun shr-heading (cont &rest types)
349   (shr-ensure-paragraph)
350   (apply #'shr-fontize-cont cont types)
351   (shr-ensure-paragraph))
352
353 ;;; Tag-specific rendering rules.
354
355 (defun shr-tag-p (cont)
356   (shr-ensure-paragraph)
357   (shr-indent)
358   (shr-generic cont)
359   (shr-ensure-paragraph))
360
361 (defun shr-tag-b (cont)
362   (shr-fontize-cont cont 'bold))
363
364 (defun shr-tag-i (cont)
365   (shr-fontize-cont cont 'italic))
366
367 (defun shr-tag-em (cont)
368   (shr-fontize-cont cont 'bold))
369
370 (defun shr-tag-u (cont)
371   (shr-fontize-cont cont 'underline))
372
373 (defun shr-tag-s (cont)
374   (shr-fontize-cont cont 'strike-through))
375
376 (defun shr-tag-a (cont)
377   (let ((url (cdr (assq :href cont)))
378         (start (point))
379         shr-start)
380     (shr-generic cont)
381     (widget-convert-button
382      'link (or shr-start start) (point)
383      :help-echo url)
384     (put-text-property (or shr-start start) (point) 'keymap shr-map)
385     (put-text-property (or shr-start start) (point) 'shr-url url)))
386
387 (defun shr-encode-url (url)
388   "Encode URL."
389   (browse-url-url-encode-chars url "[)$ ]"))
390
391 (defun shr-tag-img (cont)
392   (when (and (> (current-column) 0)
393              (not (eq shr-state 'image)))
394     (insert "\n"))
395   (let ((alt (cdr (assq :alt cont)))
396         (url (cdr (assq :src cont)))
397         (width (cdr (assq :width cont))))
398     ;; Only respect align if width specified.
399     (when width
400       ;; Check that width is not larger than max width, otherwise ignore
401       ;; align
402       (let ((max-width (* shr-width (frame-char-width)))
403             (width (string-to-number width)))
404         (when (< width max-width)
405           (let ((align (cdr (assq :align cont))))
406             (cond ((string= align "right")
407                    (insert (propertize
408                             " " 'display
409                             `(space . (:align-to ,(list (- max-width width)))))))
410                   ((string= align "center")
411                    (insert (propertize
412                             " " 'display
413                             `(space . (:balign-to ,(list (- (/ max-width 2) width))))))))))))
414     (let ((start (point-marker)))
415       (when (zerop (length alt))
416         (setq alt "[img]"))
417       (cond
418        ((and (not shr-inhibit-images)
419              (string-match "\\`cid:" url))
420         (let ((url (substring url (match-end 0)))
421               image)
422           (if (or (not shr-content-function)
423                   (not (setq image (funcall shr-content-function url))))
424               (insert alt)
425             (shr-put-image image (point) alt))))
426        ((or shr-inhibit-images
427             (and shr-blocked-images
428                  (string-match shr-blocked-images url)))
429         (setq shr-start (point))
430         (let ((shr-state 'space))
431           (if (> (length alt) 8)
432               (shr-insert (substring alt 0 8))
433             (shr-insert alt))))
434        ((url-is-cached (shr-encode-url url))
435         (shr-put-image (shr-get-image-data url) (point) alt))
436        (t
437         (insert alt)
438         (ignore-errors
439           (url-retrieve (shr-encode-url url) 'shr-image-fetched
440                         (list (current-buffer) start (point-marker))
441                         t))))
442       (insert " ")
443       (put-text-property start (point) 'keymap shr-map)
444       (put-text-property start (point) 'shr-alt alt)
445       (put-text-property start (point) 'shr-image url)
446       (setq shr-state 'image))))
447
448 (defun shr-tag-pre (cont)
449   (let ((shr-folding-mode 'none))
450     (shr-ensure-newline)
451     (shr-indent)
452     (shr-generic cont)
453     (shr-ensure-newline)))
454
455 (defun shr-tag-blockquote (cont)
456   (shr-ensure-paragraph)
457   (shr-indent)
458   (let ((shr-indentation (+ shr-indentation 4)))
459     (shr-generic cont))
460   (shr-ensure-paragraph))
461
462 (defun shr-tag-ul (cont)
463   (shr-ensure-paragraph)
464   (let ((shr-list-mode 'ul))
465     (shr-generic cont))
466   (shr-ensure-paragraph))
467
468 (defun shr-tag-ol (cont)
469   (shr-ensure-paragraph)
470   (let ((shr-list-mode 1))
471     (shr-generic cont))
472   (shr-ensure-paragraph))
473
474 (defun shr-tag-li (cont)
475   (shr-ensure-paragraph)
476   (shr-indent)
477   (let* ((bullet
478           (if (numberp shr-list-mode)
479               (prog1
480                   (format "%d " shr-list-mode)
481                 (setq shr-list-mode (1+ shr-list-mode)))
482             "* "))
483          (shr-indentation (+ shr-indentation (length bullet))))
484     (insert bullet)
485     (shr-generic cont)))
486
487 (defun shr-tag-br (cont)
488   (unless (bobp)
489     (insert "\n")
490     (shr-indent))
491   (shr-generic cont))
492
493 (defun shr-tag-h1 (cont)
494   (shr-heading cont 'bold 'underline))
495
496 (defun shr-tag-h2 (cont)
497   (shr-heading cont 'bold))
498
499 (defun shr-tag-h3 (cont)
500   (shr-heading cont 'italic))
501
502 (defun shr-tag-h4 (cont)
503   (shr-heading cont))
504
505 (defun shr-tag-h5 (cont)
506   (shr-heading cont))
507
508 (defun shr-tag-h6 (cont)
509   (shr-heading cont))
510
511 (defun shr-tag-hr (cont)
512   (shr-ensure-newline)
513   (insert (make-string shr-width shr-hr-line) "\n"))
514
515 ;;; Table rendering algorithm.
516
517 ;; Table rendering is the only complicated thing here.  We do this by
518 ;; first counting how many TDs there are in each TR, and registering
519 ;; how wide they think they should be ("width=45%", etc).  Then we
520 ;; render each TD separately (this is done in temporary buffers, so
521 ;; that we can use all the rendering machinery as if we were in the
522 ;; main buffer).  Now we know how much space each TD really takes, so
523 ;; we then render everything again with the new widths, and finally
524 ;; insert all these boxes into the main buffer.
525 (defun shr-tag-table (cont)
526   (shr-ensure-paragraph)
527   (setq cont (or (cdr (assq 'tbody cont))
528                  cont))
529   (let* ((shr-inhibit-images t)
530          ;; Find all suggested widths.
531          (columns (shr-column-specs cont))
532          ;; Compute how many characters wide each TD should be.
533          (suggested-widths (shr-pro-rate-columns columns))
534          ;; Do a "test rendering" to see how big each TD is (this can
535          ;; be smaller (if there's little text) or bigger (if there's
536          ;; unbreakable text).
537          (sketch (shr-make-table cont suggested-widths))
538          (sketch-widths (shr-table-widths sketch suggested-widths)))
539     ;; Then render the table again with these new "hard" widths.
540     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
541   ;; Finally, insert all the images after the table.  The Emacs buffer
542   ;; model isn't strong enough to allow us to put the images actually
543   ;; into the tables.
544   (dolist (elem (shr-find-elements cont 'img))
545     (shr-tag-img (cdr elem))))
546
547 (defun shr-find-elements (cont type)
548   (let (result)
549     (dolist (elem cont)
550       (cond ((eq (car elem) type)
551              (push elem result))
552             ((consp (cdr elem))
553              (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
554     (nreverse result)))
555
556 (defun shr-insert-table (table widths)
557   (shr-insert-table-ruler widths)
558   (dolist (row table)
559     (let ((start (point))
560           (height (let ((max 0))
561                     (dolist (column row)
562                       (setq max (max max (cadr column))))
563                     max)))
564       (dotimes (i height)
565         (shr-indent)
566         (insert "|\n"))
567       (dolist (column row)
568         (goto-char start)
569         (let ((lines (nth 2 column))
570               (overlay-lines (nth 3 column))
571               overlay overlay-line)
572           (dolist (line lines)
573             (setq overlay-line (pop overlay-lines))
574             (end-of-line)
575             (insert line "|")
576             (dolist (overlay overlay-line)
577               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
578                                      (- (point) (nth 1 overlay) 1)))
579                     (properties (nth 2 overlay)))
580                 (while properties
581                   (overlay-put o (pop properties) (pop properties)))))
582             (forward-line 1))
583           ;; Add blank lines at padding at the bottom of the TD,
584           ;; possibly.
585           (dotimes (i (- height (length lines)))
586             (end-of-line)
587             (insert (make-string (length (car lines)) ? ) "|")
588             (forward-line 1)))))
589     (shr-insert-table-ruler widths)))
590
591 (defun shr-insert-table-ruler (widths)
592   (shr-indent)
593   (insert shr-table-corner)
594   (dotimes (i (length widths))
595     (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
596   (insert "\n"))
597
598 (defun shr-table-widths (table suggested-widths)
599   (let* ((length (length suggested-widths))
600          (widths (make-vector length 0))
601          (natural-widths (make-vector length 0)))
602     (dolist (row table)
603       (let ((i 0))
604         (dolist (column row)
605           (aset widths i (max (aref widths i)
606                               (car column)))
607           (aset natural-widths i (max (aref natural-widths i)
608                                       (cadr column)))
609           (setq i (1+ i)))))
610     (let ((extra (- (apply '+ (append suggested-widths nil))
611                     (apply '+ (append widths nil))))
612           (expanded-columns 0))
613       (when (> extra 0)
614         (dotimes (i length)
615           ;; If the natural width is wider than the rendered width, we
616           ;; want to allow the column to expand.
617           (when (> (aref natural-widths i) (aref widths i))
618             (setq expanded-columns (1+ expanded-columns))))
619         (dotimes (i length)
620           (when (> (aref natural-widths i) (aref widths i))
621             (aset widths i (min
622                             (1+ (aref natural-widths i))
623                             (+ (/ extra expanded-columns)
624                                (aref widths i))))))))
625     widths))
626
627 (defun shr-make-table (cont widths &optional fill)
628   (let ((trs nil))
629     (dolist (row cont)
630       (when (eq (car row) 'tr)
631         (let ((tds nil)
632               (columns (cdr row))
633               (i 0)
634               column)
635           (while (< i (length widths))
636             (setq column (pop columns))
637             (when (or (memq (car column) '(td th))
638                       (null column))
639               (push (shr-render-td (cdr column) (aref widths i) fill)
640                     tds)
641               (setq i (1+ i))))
642           (push (nreverse tds) trs))))
643     (nreverse trs)))
644
645 (defun shr-render-td (cont width fill)
646   (with-temp-buffer
647     (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
648       (if cache
649           (insert cache)
650         (let ((shr-width width)
651               (shr-indentation 0))
652           (shr-generic cont))
653         (delete-region
654          (point)
655          (+ (point)
656             (skip-chars-backward " \t\n")))
657         (push (cons (cons width cont) (buffer-string))
658               shr-content-cache)))
659     (goto-char (point-min))
660     (let ((max 0))
661       (while (not (eobp))
662         (end-of-line)
663         (setq max (max max (current-column)))
664         (forward-line 1))
665       (when fill
666         (goto-char (point-min))
667         ;; If the buffer is totally empty, then put a single blank
668         ;; line here.
669         (if (zerop (buffer-size))
670             (insert (make-string width ? ))
671           ;; Otherwise, fill the buffer.
672           (while (not (eobp))
673             (end-of-line)
674             (when (> (- width (current-column)) 0)
675               (insert (make-string (- width (current-column)) ? )))
676             (forward-line 1))))
677       (if fill
678           (list max
679                 (count-lines (point-min) (point-max))
680                 (split-string (buffer-string) "\n")
681                 (shr-collect-overlays))
682         (list max
683               (shr-natural-width))))))
684
685 (defun shr-natural-width ()
686   (goto-char (point-min))
687   (let ((current 0)
688         (max 0))
689     (while (not (eobp))
690       (end-of-line)
691       (setq current (+ current (current-column)))
692       (unless (get-text-property (point) 'shr-break)
693         (setq max (max max current)
694               current 0))
695       (forward-line 1))
696     max))
697
698 (defun shr-collect-overlays ()
699   (save-excursion
700     (goto-char (point-min))
701     (let ((overlays nil))
702       (while (not (eobp))
703         (push (shr-overlays-in-region (point) (line-end-position))
704               overlays)
705         (forward-line 1))
706       (nreverse overlays))))
707
708 (defun shr-overlays-in-region (start end)
709   (let (result)
710     (dolist (overlay (overlays-in start end))
711       (push (list (if (> start (overlay-start overlay))
712                       (- end start)
713                     (- end (overlay-start overlay)))
714                   (if (< end (overlay-end overlay))
715                       0
716                     (- end (overlay-end overlay)))
717                   (overlay-properties overlay))
718             result))
719     (nreverse result)))
720
721 (defun shr-pro-rate-columns (columns)
722   (let ((total-percentage 0)
723         (widths (make-vector (length columns) 0)))
724     (dotimes (i (length columns))
725       (setq total-percentage (+ total-percentage (aref columns i))))
726     (setq total-percentage (/ 1.0 total-percentage))
727     (dotimes (i (length columns))
728       (aset widths i (max (truncate (* (aref columns i)
729                                        total-percentage
730                                        (- shr-width (1+ (length columns)))))
731                           10)))
732     widths))
733
734 ;; Return a summary of the number and shape of the TDs in the table.
735 (defun shr-column-specs (cont)
736   (let ((columns (make-vector (shr-max-columns cont) 1)))
737     (dolist (row cont)
738       (when (eq (car row) 'tr)
739         (let ((i 0))
740           (dolist (column (cdr row))
741             (when (memq (car column) '(td th))
742               (let ((width (cdr (assq :width (cdr column)))))
743                 (when (and width
744                            (string-match "\\([0-9]+\\)%" width))
745                   (aset columns i
746                         (/ (string-to-number (match-string 1 width))
747                            100.0))))
748               (setq i (1+ i)))))))
749     columns))
750
751 (defun shr-count (cont elem)
752   (let ((i 0))
753     (dolist (sub cont)
754       (when (eq (car sub) elem)
755         (setq i (1+ i))))
756     i))
757
758 (defun shr-max-columns (cont)
759   (let ((max 0))
760     (dolist (row cont)
761       (when (eq (car row) 'tr)
762         (setq max (max max (+ (shr-count (cdr row) 'td)
763                               (shr-count (cdr row) 'th))))))
764     max))
765
766 (provide 'shr)
767
768 ;;; shr.el ends here