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