]> code.delx.au - gnu-emacs/commitdiff
Remove trailing blank lines in shr
authorLars Magne Ingebrigtsen <larsi@gnus.org>
Sun, 20 Mar 2016 11:52:28 +0000 (12:52 +0100)
committerLars Magne Ingebrigtsen <larsi@gnus.org>
Sun, 20 Mar 2016 11:52:28 +0000 (12:52 +0100)
* lisp/net/shr.el (shr--remove-blank-lines-at-the-end): New function.
(shr-insert-document): Use it to remove trailing blank lines
at the end of documents, since these never seem to be very useful.

lisp/net/shr.el

index 3877719d7a762de777b9aaa09db49932f9de621b..6bb6898188d54c187a77480b16a39700934afbad 100644 (file)
@@ -274,10 +274,24 @@ DOM should be a parse tree as generated by
     (set-window-hscroll nil 0)
     (shr-descend dom)
     (shr-fill-lines start (point))
+    (shr--remove-blank-lines-at-the-end start (point))
     (shr-remove-trailing-whitespace start (point))
     (when shr-warning
       (message "%s" shr-warning))))
 
+(defun shr--remove-blank-lines-at-the-end (start end)
+  (save-restriction
+    (save-excursion
+      (current-buffer)
+      (narrow-to-region start end)
+      (goto-char end)
+      (when (and (re-search-backward "[^ \n]" nil t)
+                 (not (eobp)))
+        (forward-char 1)
+        (delete-region (point) (point-max))
+        (unless (bolp)
+          (insert "\n"))))))
+
 (defun shr-remove-trailing-whitespace (start end)
   (let ((width (window-width)))
     (save-restriction