Browse Source

Fix: Prevent spurious newlines when inserting a new heading

* lisp/org.el (org-insert-heading): Do not insert spurious newline
  characters when inserting a headline.

* testing/lisp/test-org.el (test-org/insert-heading): Add tests.

Reported-by: Kyle Sherman <kylewsherman@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/112205>
Nicolas Goaziou 8 years ago
parent
commit
542890cd3f
2 changed files with 13 additions and 4 deletions
  1. 3 4
      lisp/org.el
  2. 10 0
      testing/lisp/test-org.el

+ 3 - 4
lisp/org.el

@@ -7855,13 +7855,12 @@ unconditionally."
 	       (when blank? (insert "\n"))
 	       (insert "\n" stars " ")
 	       (when (org-string-nw-p split) (insert split))
-	       (insert "\n")
-	       (forward-char -1)))
+	       (when (eobp) (save-excursion (insert "\n")))))
 	    (t
 	     (end-of-line)
 	     (when blank? (insert "\n"))
-	     (insert "\n" stars " \n")
-	     (forward-char -1))))
+	     (insert "\n" stars " ")
+	     (when (eobp) (save-excursion (insert "\n"))))))
      ;; On regular text, turn line into a headline or split, if
      ;; appropriate.
      ((bolp)

+ 10 - 0
testing/lisp/test-org.el

@@ -1331,6 +1331,16 @@
   (should
    (equal "* \n* \n"
 	  (org-test-with-temp-text "* <point>"
+	    (org-insert-heading)
+	    (buffer-string))))
+  (should
+   (org-test-with-temp-text "* <point>\n"
+     (org-insert-heading)
+     (looking-at-p "\n\\'")))
+  ;; Do not insert spurious headlines when inserting a new headline.
+  (should
+   (equal "* H1\n* H2\n* \n"
+	  (org-test-with-temp-text "* H1\n* H2<point>\n"
 	    (org-insert-heading)
 	    (buffer-string)))))