(shr-insert): Rework the line-breaking algorithm.
[gnus] / lisp / shr.el
index 73011af..6124ff7 100644 (file)
@@ -188,20 +188,8 @@ redirects somewhere else."
          column)
       (when (and (string-match "\\`[ \t\n]" text)
                 (not (bolp)))
-       (insert " ")
-       (setq shr-state 'space))
+       (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")
-           (put-text-property (1- (point)) (point) 'shr-break t))
-          ((not first)
-           (insert " "))))
-       (setq first nil)
        (when (and (bolp)
                   (> shr-indentation 0))
          (shr-indent))
@@ -210,12 +198,17 @@ 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 " "))))))
 
 (defun shr-ensure-newline ()
   (unless (zerop (current-column))