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