* shr.el (shr-insert): Don't insert double spaces.
[gnus] / lisp / shr.el
index e0bb868..daafa61 100644 (file)
@@ -30,6 +30,7 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'cl))
 (require 'browse-url)
 
 (defgroup shr nil
@@ -68,6 +69,7 @@ cid: URL as the argument.")
 (defvar shr-indentation 0)
 (defvar shr-inhibit-images nil)
 (defvar shr-list-mode nil)
+(defvar shr-content-cache nil)
 
 (defvar shr-map
   (let ((map (make-sparse-keymap)))
@@ -83,6 +85,7 @@ cid: URL as the argument.")
 
 ;;;###autoload
 (defun shr-insert-document (dom)
+  (setq shr-content-cache nil)
   (let ((shr-state nil)
        (shr-start nil))
     (shr-descend (shr-transform-dom dom))))
@@ -135,6 +138,17 @@ redirects somewhere else."
       (message "Browsing %s..." url)
       (browse-url url))))
 
+(defun shr-insert-image ()
+  "Insert the image under point into the buffer."
+  (interactive)
+  (let ((url (get-text-property (point) 'shr-image)))
+    (if (not url)
+       (message "No image under point")
+      (message "Inserting %s..." url)
+      (url-retrieve url 'shr-image-fetched
+                   (list (current-buffer) (1- (point)) (point-marker))
+                   t))))
+
 ;;; Utility functions.
 
 (defun shr-transform-dom (dom)
@@ -174,20 +188,10 @@ redirects somewhere else."
     (let ((first t)
          column)
       (when (and (string-match "\\`[ \t\n]" text)
-                (not (bolp)))
-       (insert " ")
-       (setq shr-state 'space))
+                (not (bolp))
+                (not (eq (char-after (1- (point))) ? )))
+       (insert " "))
       (dolist (elem (split-string text))
-       (setq column (current-column))
-       (when (> column 0)
-         (cond
-          ((and (or (not first)
-                    (eq shr-state 'space))
-                (> (+ column (length elem) 1) shr-width))
-           (insert "\n"))
-          ((not first)
-           (insert " "))))
-       (setq first nil)
        (when (and (bolp)
                   (> shr-indentation 0))
          (shr-indent))
@@ -196,12 +200,19 @@ redirects somewhere else."
        ;; starts.
        (unless shr-start
          (setq shr-start (point)))
-       (insert elem))
-      (setq shr-state nil)
-      (when (and (string-match "[ \t\n]\\'" text)
-                (not (bolp)))
-       (insert " ")
-       (setq shr-state 'space))))))
+       (insert elem)
+       (when (> (current-column) shr-width)
+         (if (not (search-backward " " (line-beginning-position) t))
+             (insert "\n")
+           (delete-char 1)
+           (insert "\n")
+           (put-text-property (1- (point)) (point) 'shr-break t)
+           (when (> shr-indentation 0)
+             (shr-indent))
+           (end-of-line)))
+       (insert " "))
+      (unless (string-match "[ \t\n]\\'" text)
+       (delete-char -1))))))
 
 (defun shr-ensure-newline ()
   (unless (zerop (current-column))
@@ -395,11 +406,14 @@ Return a string with image data."
 (defun shr-tag-ul (cont)
   (shr-ensure-paragraph)
   (let ((shr-list-mode 'ul))
-    (shr-generic cont)))
+    (shr-generic cont))
+  (shr-ensure-paragraph))
 
 (defun shr-tag-ol (cont)
+  (shr-ensure-paragraph)
   (let ((shr-list-mode 1))
-    (shr-generic cont)))
+    (shr-generic cont))
+  (shr-ensure-paragraph))
 
 (defun shr-tag-li (cont)
   (shr-ensure-newline)
@@ -436,6 +450,10 @@ Return a string with image data."
 (defun shr-tag-h6 (cont)
   (shr-heading cont))
 
+(defun shr-tag-hr (cont)
+  (shr-ensure-newline)
+  (insert (make-string shr-width ?-) "\n"))
+
 ;;; Table rendering algorithm.
 
 ;; Table rendering is the only complicated thing here.  We do this by
@@ -459,7 +477,7 @@ Return a string with image data."
         ;; be smaller (if there's little text) or bigger (if there's
         ;; unbreakable text).
         (sketch (shr-make-table cont suggested-widths))
-        (sketch-widths (shr-table-widths sketch (length suggested-widths))))
+        (sketch-widths (shr-table-widths sketch suggested-widths)))
     ;; Then render the table again with these new "hard" widths.
     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
   ;; Finally, insert all the images after the table.  The Emacs buffer
@@ -490,21 +508,20 @@ Return a string with image data."
        (insert "|\n"))
       (dolist (column row)
        (goto-char start)
-       (let ((lines (split-string (nth 2 column) "\n"))
+       (let ((lines (nth 2 column))
              (overlay-lines (nth 3 column))
              overlay overlay-line)
          (dolist (line lines)
            (setq overlay-line (pop overlay-lines))
-           (when (> (length line) 0)
-             (end-of-line)
-             (insert line "|")
-             (dolist (overlay overlay-line)
-               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
-                                      (- (point) (nth 1 overlay) 1)))
-                     (properties (nth 2 overlay)))
-                 (while properties
-                   (overlay-put o (pop properties) (pop properties)))))
-             (forward-line 1)))
+           (end-of-line)
+           (insert line "|")
+           (dolist (overlay overlay-line)
+             (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
+                                    (- (point) (nth 1 overlay) 1)))
+                   (properties (nth 2 overlay)))
+               (while properties
+                 (overlay-put o (pop properties) (pop properties)))))
+           (forward-line 1))
          ;; Add blank lines at padding at the bottom of the TD,
          ;; possibly.
          (dotimes (i (- height (length lines)))
@@ -520,14 +537,33 @@ Return a string with image data."
     (insert (make-string (aref widths i) ?-) ?+))
   (insert "\n"))
 
-(defun shr-table-widths (table length)
-  (let ((widths (make-vector length 0)))
+(defun shr-table-widths (table suggested-widths)
+  (let* ((length (length suggested-widths))
+        (widths (make-vector length 0))
+        (natural-widths (make-vector length 0)))
     (dolist (row table)
       (let ((i 0))
        (dolist (column row)
          (aset widths i (max (aref widths i)
                              (car column)))
-         (incf i))))
+         (aset natural-widths i (max (aref natural-widths i)
+                                     (cadr column)))
+         (setq i (1+ i)))))
+    (let ((extra (- (apply '+ (append suggested-widths nil))
+                   (apply '+ (append widths nil))))
+         (expanded-columns 0))
+      (when (> extra 0)
+       (dotimes (i length)
+         ;; If the natural width is wider than the rendered width, we
+         ;; want to allow the column to expand.
+         (when (> (aref natural-widths i) (aref widths i))
+           (setq expanded-columns (1+ expanded-columns))))
+       (dotimes (i length)
+         (when (> (aref natural-widths i) (aref widths i))
+           (aset widths i (min
+                           (1+ (aref natural-widths i))
+                           (+ (/ extra expanded-columns)
+                              (aref widths i))))))))
     widths))
 
 (defun shr-make-table (cont widths &optional fill)
@@ -550,11 +586,18 @@ Return a string with image data."
 
 (defun shr-render-td (cont width fill)
   (with-temp-buffer
-    (let ((shr-width width)
-         (shr-indentation 0))
-      (shr-generic cont))
-    (while (re-search-backward "\n *$" nil t)
-      (delete-region (match-beginning 0) (match-end 0)))
+    (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
+      (if cache
+         (insert cache)
+       (let ((shr-width width)
+             (shr-indentation 0))
+         (shr-generic cont))
+       (delete-region
+        (point)
+        (+ (point)
+           (skip-chars-backward " \t\n")))
+       (push (cons (cons width cont) (buffer-string))
+             shr-content-cache)))
     (goto-char (point-min))
     (let ((max 0))
       (while (not (eobp))
@@ -573,11 +616,26 @@ Return a string with image data."
            (when (> (- width (current-column)) 0)
              (insert (make-string (- width (current-column)) ? )))
            (forward-line 1))))
-      (list max
-           (count-lines (point-min) (point-max))
-           (buffer-string)
-           (and fill
-                (shr-collect-overlays))))))
+      (if fill
+         (list max
+               (count-lines (point-min) (point-max))
+               (split-string (buffer-string) "\n")
+               (shr-collect-overlays))
+       (list max
+             (shr-natural-width))))))
+
+(defun shr-natural-width ()
+  (goto-char (point-min))
+  (let ((current 0)
+       (max 0))
+    (while (not (eobp))
+      (end-of-line)
+      (setq current (+ current (current-column)))
+      (unless (get-text-property (point) 'shr-break)
+       (setq max (max max current)
+             current 0))
+      (forward-line 1))
+    max))
 
 (defun shr-collect-overlays ()
   (save-excursion
@@ -606,12 +664,12 @@ Return a string with image data."
   (let ((total-percentage 0)
        (widths (make-vector (length columns) 0)))
     (dotimes (i (length columns))
-      (incf total-percentage (aref columns i)))
+      (setq total-percentage (+ total-percentage (aref columns i))))
     (setq total-percentage (/ 1.0 total-percentage))
     (dotimes (i (length columns))
       (aset widths i (max (truncate (* (aref columns i)
                                       total-percentage
-                                      shr-width))
+                                      (- shr-width (1+ (length columns)))))
                          10)))
     widths))