(gnus-summary-refer-thread): Implement a version that uses *-request-thread.
[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         (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                 (> (current-column) shr-indentation))
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      'url-link (or shr-start start) (point)
383      :help-echo url
384      :keymap shr-map
385      url)
386     (put-text-property (or shr-start start) (point) 'shr-url url)))
387
388 (defun shr-encode-url (url)
389   "Encode URL."
390   (browse-url-url-encode-chars url "[)$ ]"))
391
392 (defun shr-tag-img (cont)
393   (when (and (> (current-column) 0)
394              (not (eq shr-state 'image)))
395     (insert "\n"))
396   (let ((alt (cdr (assq :alt cont)))
397         (url (cdr (assq :src cont)))
398         (width (cdr (assq :width cont))))
399     ;; Only respect align if width specified.
400     (when width
401       ;; Check that width is not larger than max width, otherwise ignore
402       ;; align
403       (let ((max-width (* shr-width (frame-char-width)))
404             (width (string-to-number width)))
405         (when (< width max-width)
406           (let ((align (cdr (assq :align cont))))
407             (cond ((string= align "right")
408                    (insert (propertize
409                             " " 'display
410                             `(space . (:align-to ,(list (- max-width width)))))))
411                   ((string= align "center")
412                    (insert (propertize
413                             " " 'display
414                             `(space . (:balign-to ,(list (- (/ max-width 2) width))))))))))))
415     (let ((start (point-marker)))
416       (when (zerop (length alt))
417         (setq alt "[img]"))
418       (cond
419        ((and (not shr-inhibit-images)
420              (string-match "\\`cid:" url))
421         (let ((url (substring url (match-end 0)))
422               image)
423           (if (or (not shr-content-function)
424                   (not (setq image (funcall shr-content-function url))))
425               (insert alt)
426             (shr-put-image image (point) alt))))
427        ((or shr-inhibit-images
428             (and shr-blocked-images
429                  (string-match shr-blocked-images url)))
430         (setq shr-start (point))
431         (let ((shr-state 'space))
432           (if (> (length alt) 8)
433               (shr-insert (substring alt 0 8))
434             (shr-insert alt))))
435        ((url-is-cached (shr-encode-url url))
436         (shr-put-image (shr-get-image-data url) (point) alt))
437        (t
438         (insert alt)
439         (ignore-errors
440           (url-retrieve (shr-encode-url url) 'shr-image-fetched
441                         (list (current-buffer) start (point-marker))
442                         t))))
443       (insert " ")
444       (put-text-property start (point) 'keymap shr-map)
445       (put-text-property start (point) 'shr-alt alt)
446       (put-text-property start (point) 'shr-image url)
447       (setq shr-state 'image))))
448
449 (defun shr-tag-pre (cont)
450   (let ((shr-folding-mode 'none))
451     (shr-ensure-newline)
452     (shr-indent)
453     (shr-generic cont)
454     (shr-ensure-newline)))
455
456 (defun shr-tag-blockquote (cont)
457   (shr-ensure-paragraph)
458   (shr-indent)
459   (let ((shr-indentation (+ shr-indentation 4)))
460     (shr-generic cont))
461   (shr-ensure-paragraph))
462
463 (defun shr-tag-ul (cont)
464   (shr-ensure-paragraph)
465   (let ((shr-list-mode 'ul))
466     (shr-generic cont))
467   (shr-ensure-paragraph))
468
469 (defun shr-tag-ol (cont)
470   (shr-ensure-paragraph)
471   (let ((shr-list-mode 1))
472     (shr-generic cont))
473   (shr-ensure-paragraph))
474
475 (defun shr-tag-li (cont)
476   (shr-ensure-paragraph)
477   (shr-indent)
478   (let* ((bullet
479           (if (numberp shr-list-mode)
480               (prog1
481                   (format "%d " shr-list-mode)
482                 (setq shr-list-mode (1+ shr-list-mode)))
483             "* "))
484          (shr-indentation (+ shr-indentation (length bullet))))
485     (insert bullet)
486     (shr-generic cont)))
487
488 (defun shr-tag-br (cont)
489   (unless (bobp)
490     (insert "\n")
491     (shr-indent))
492   (shr-generic cont))
493
494 (defun shr-tag-h1 (cont)
495   (shr-heading cont 'bold 'underline))
496
497 (defun shr-tag-h2 (cont)
498   (shr-heading cont 'bold))
499
500 (defun shr-tag-h3 (cont)
501   (shr-heading cont 'italic))
502
503 (defun shr-tag-h4 (cont)
504   (shr-heading cont))
505
506 (defun shr-tag-h5 (cont)
507   (shr-heading cont))
508
509 (defun shr-tag-h6 (cont)
510   (shr-heading cont))
511
512 (defun shr-tag-hr (cont)
513   (shr-ensure-newline)
514   (insert (make-string shr-width shr-hr-line) "\n"))
515
516 ;;; Table rendering algorithm.
517
518 ;; Table rendering is the only complicated thing here.  We do this by
519 ;; first counting how many TDs there are in each TR, and registering
520 ;; how wide they think they should be ("width=45%", etc).  Then we
521 ;; render each TD separately (this is done in temporary buffers, so
522 ;; that we can use all the rendering machinery as if we were in the
523 ;; main buffer).  Now we know how much space each TD really takes, so
524 ;; we then render everything again with the new widths, and finally
525 ;; insert all these boxes into the main buffer.
526 (defun shr-tag-table (cont)
527   (shr-ensure-paragraph)
528   (setq cont (or (cdr (assq 'tbody cont))
529                  cont))
530   (let* ((shr-inhibit-images t)
531          ;; Find all suggested widths.
532          (columns (shr-column-specs cont))
533          ;; Compute how many characters wide each TD should be.
534          (suggested-widths (shr-pro-rate-columns columns))
535          ;; Do a "test rendering" to see how big each TD is (this can
536          ;; be smaller (if there's little text) or bigger (if there's
537          ;; unbreakable text).
538          (sketch (shr-make-table cont suggested-widths))
539          (sketch-widths (shr-table-widths sketch suggested-widths)))
540     ;; Then render the table again with these new "hard" widths.
541     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
542   ;; Finally, insert all the images after the table.  The Emacs buffer
543   ;; model isn't strong enough to allow us to put the images actually
544   ;; into the tables.
545   (dolist (elem (shr-find-elements cont 'img))
546     (shr-tag-img (cdr elem))))
547
548 (defun shr-find-elements (cont type)
549   (let (result)
550     (dolist (elem cont)
551       (cond ((eq (car elem) type)
552              (push elem result))
553             ((consp (cdr elem))
554              (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
555     (nreverse result)))
556
557 (defun shr-insert-table (table widths)
558   (shr-insert-table-ruler widths)
559   (dolist (row table)
560     (let ((start (point))
561           (height (let ((max 0))
562                     (dolist (column row)
563                       (setq max (max max (cadr column))))
564                     max)))
565       (dotimes (i height)
566         (shr-indent)
567         (insert "|\n"))
568       (dolist (column row)
569         (goto-char start)
570         (let ((lines (nth 2 column))
571               (overlay-lines (nth 3 column))
572               overlay overlay-line)
573           (dolist (line lines)
574             (setq overlay-line (pop overlay-lines))
575             (end-of-line)
576             (insert line "|")
577             (dolist (overlay overlay-line)
578               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
579                                      (- (point) (nth 1 overlay) 1)))
580                     (properties (nth 2 overlay)))
581                 (while properties
582                   (overlay-put o (pop properties) (pop properties)))))
583             (forward-line 1))
584           ;; Add blank lines at padding at the bottom of the TD,
585           ;; possibly.
586           (dotimes (i (- height (length lines)))
587             (end-of-line)
588             (insert (make-string (length (car lines)) ? ) "|")
589             (forward-line 1)))))
590     (shr-insert-table-ruler widths)))
591
592 (defun shr-insert-table-ruler (widths)
593   (shr-indent)
594   (insert shr-table-corner)
595   (dotimes (i (length widths))
596     (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
597   (insert "\n"))
598
599 (defun shr-table-widths (table suggested-widths)
600   (let* ((length (length suggested-widths))
601          (widths (make-vector length 0))
602          (natural-widths (make-vector length 0)))
603     (dolist (row table)
604       (let ((i 0))
605         (dolist (column row)
606           (aset widths i (max (aref widths i)
607                               (car column)))
608           (aset natural-widths i (max (aref natural-widths i)
609                                       (cadr column)))
610           (setq i (1+ i)))))
611     (let ((extra (- (apply '+ (append suggested-widths nil))
612                     (apply '+ (append widths nil))))
613           (expanded-columns 0))
614       (when (> extra 0)
615         (dotimes (i length)
616           ;; If the natural width is wider than the rendered width, we
617           ;; want to allow the column to expand.
618           (when (> (aref natural-widths i) (aref widths i))
619             (setq expanded-columns (1+ expanded-columns))))
620         (dotimes (i length)
621           (when (> (aref natural-widths i) (aref widths i))
622             (aset widths i (min
623                             (1+ (aref natural-widths i))
624                             (+ (/ extra expanded-columns)
625                                (aref widths i))))))))
626     widths))
627
628 (defun shr-make-table (cont widths &optional fill)
629   (let ((trs nil))
630     (dolist (row cont)
631       (when (eq (car row) 'tr)
632         (let ((tds nil)
633               (columns (cdr row))
634               (i 0)
635               column)
636           (while (< i (length widths))
637             (setq column (pop columns))
638             (when (or (memq (car column) '(td th))
639                       (null column))
640               (push (shr-render-td (cdr column) (aref widths i) fill)
641                     tds)
642               (setq i (1+ i))))
643           (push (nreverse tds) trs))))
644     (nreverse trs)))
645
646 (defun shr-render-td (cont width fill)
647   (with-temp-buffer
648     (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
649       (if cache
650           (insert cache)
651         (let ((shr-width width)
652               (shr-indentation 0))
653           (shr-generic cont))
654         (delete-region
655          (point)
656          (+ (point)
657             (skip-chars-backward " \t\n")))
658         (push (cons (cons width cont) (buffer-string))
659               shr-content-cache)))
660     (goto-char (point-min))
661     (let ((max 0))
662       (while (not (eobp))
663         (end-of-line)
664         (setq max (max max (current-column)))
665         (forward-line 1))
666       (when fill
667         (goto-char (point-min))
668         ;; If the buffer is totally empty, then put a single blank
669         ;; line here.
670         (if (zerop (buffer-size))
671             (insert (make-string width ? ))
672           ;; Otherwise, fill the buffer.
673           (while (not (eobp))
674             (end-of-line)
675             (when (> (- width (current-column)) 0)
676               (insert (make-string (- width (current-column)) ? )))
677             (forward-line 1))))
678       (if fill
679           (list max
680                 (count-lines (point-min) (point-max))
681                 (split-string (buffer-string) "\n")
682                 (shr-collect-overlays))
683         (list max
684               (shr-natural-width))))))
685
686 (defun shr-natural-width ()
687   (goto-char (point-min))
688   (let ((current 0)
689         (max 0))
690     (while (not (eobp))
691       (end-of-line)
692       (setq current (+ current (current-column)))
693       (unless (get-text-property (point) 'shr-break)
694         (setq max (max max current)
695               current 0))
696       (forward-line 1))
697     max))
698
699 (defun shr-collect-overlays ()
700   (save-excursion
701     (goto-char (point-min))
702     (let ((overlays nil))
703       (while (not (eobp))
704         (push (shr-overlays-in-region (point) (line-end-position))
705               overlays)
706         (forward-line 1))
707       (nreverse overlays))))
708
709 (defun shr-overlays-in-region (start end)
710   (let (result)
711     (dolist (overlay (overlays-in start end))
712       (push (list (if (> start (overlay-start overlay))
713                       (- end start)
714                     (- end (overlay-start overlay)))
715                   (if (< end (overlay-end overlay))
716                       0
717                     (- end (overlay-end overlay)))
718                   (overlay-properties overlay))
719             result))
720     (nreverse result)))
721
722 (defun shr-pro-rate-columns (columns)
723   (let ((total-percentage 0)
724         (widths (make-vector (length columns) 0)))
725     (dotimes (i (length columns))
726       (setq total-percentage (+ total-percentage (aref columns i))))
727     (setq total-percentage (/ 1.0 total-percentage))
728     (dotimes (i (length columns))
729       (aset widths i (max (truncate (* (aref columns i)
730                                        total-percentage
731                                        (- shr-width (1+ (length columns)))))
732                           10)))
733     widths))
734
735 ;; Return a summary of the number and shape of the TDs in the table.
736 (defun shr-column-specs (cont)
737   (let ((columns (make-vector (shr-max-columns cont) 1)))
738     (dolist (row cont)
739       (when (eq (car row) 'tr)
740         (let ((i 0))
741           (dolist (column (cdr row))
742             (when (memq (car column) '(td th))
743               (let ((width (cdr (assq :width (cdr column)))))
744                 (when (and width
745                            (string-match "\\([0-9]+\\)%" width))
746                   (aset columns i
747                         (/ (string-to-number (match-string 1 width))
748                            100.0))))
749               (setq i (1+ i)))))))
750     columns))
751
752 (defun shr-count (cont elem)
753   (let ((i 0))
754     (dolist (sub cont)
755       (when (eq (car sub) elem)
756         (setq i (1+ i))))
757     i))
758
759 (defun shr-max-columns (cont)
760   (let ((max 0))
761     (dolist (row cont)
762       (when (eq (car row) 'tr)
763         (setq max (max max (+ (shr-count (cdr row) 'td)
764                               (shr-count (cdr row) 'th))))))
765     max))
766
767 (provide 'shr)
768
769 ;;; shr.el ends here