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