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