- (while (re-search-forward "[ \t\n\r ]+" nil t)
- (replace-match " " t t))
- (goto-char (point-max)))
- ;; We may have removed everything we inserted if if was just
- ;; spaces.
- (unless (= start (point))
- ;; Mark all lines that should possibly be folded afterwards.
- (when bolp
- (shr-mark-fill start))
- (when shr-use-fonts
- (add-face-text-property start (point)
- (or shr-current-font 'variable-pitch)
- t)))))))
+ (goto-char (point-max)))))
+ (t
+ (let ((font-start (point)))
+ (when (and (string-match "\\`[ \t\n\r ]" text)
+ (not (bolp))
+ (not (eq (char-after (1- (point))) ? )))
+ (insert " "))
+ (let ((start (point))
+ (bolp (bolp)))
+ (insert text)
+ (save-restriction
+ (narrow-to-region start (point))
+ (goto-char start)
+ (when (looking-at "[ \t\n\r ]+")
+ (replace-match "" t t))
+ (while (re-search-forward "[ \t\n\r ]+" nil t)
+ (replace-match " " t t))
+ ;; Remove soft hyphens.
+ (goto-char (point-min))
+ (while (search-forward "" nil t)
+ (replace-match "" t t))
+ (goto-char (point-max)))
+ ;; We may have removed everything we inserted if if was just
+ ;; spaces.
+ (unless (= font-start (point))
+ ;; Mark all lines that should possibly be folded afterwards.
+ (when bolp
+ (shr-mark-fill start))
+ (when shr-use-fonts
+ (put-text-property font-start (point)
+ 'face
+ (or shr-current-font 'variable-pitch)))))))))