From 542890cd3f7fc482c7a1c949de6eba46af9fb4e0 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Fri, 17 Feb 2017 21:51:16 +0100 Subject: [PATCH] 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 --- lisp/org.el | 7 +++---- testing/lisp/test-org.el | 10 ++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index ed58ab787..3290a2b96 100644 --- a/lisp/org.el +++ b/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) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 8f5210f70..1083d0d21 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -1331,6 +1331,16 @@ (should (equal "* \n* \n" (org-test-with-temp-text "* " + (org-insert-heading) + (buffer-string)))) + (should + (org-test-with-temp-text "* \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\n" (org-insert-heading) (buffer-string)))))