From 6872088c7a531e9104d7599d40ade722823428ad Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Fri, 15 Feb 2019 22:46:49 +0100 Subject: [PATCH] Prevent spurious newline characters when inserting a heading * lisp/org.el (org-insert-heading): Do not insert spurious newline characters. * testing/lisp/test-org.el (test-org/insert-heading): Update tests. Reported-by: Leo Vivier --- lisp/org.el | 7 ++----- testing/lisp/test-org.el | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 2d5486bd3..f07d0a7a9 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -7696,7 +7696,6 @@ unconditionally." (unless (and blank? (org-previous-line-empty-p)) (org-N-empty-lines-before-current (if blank? 1 0))) (insert stars " ") - (when (eobp) (save-excursion (insert "\n"))) ;; When INVISIBLE-OK is non-nil, ensure newly created headline ;; is visible. (unless invisible-ok @@ -7723,13 +7722,11 @@ unconditionally." (end-of-line) (when blank? (insert "\n")) (insert "\n" stars " ") - (when (org-string-nw-p split) (insert split)) - (when (eobp) (save-excursion (insert "\n"))))) + (when (org-string-nw-p split) (insert split)))) (t (end-of-line) (when blank? (insert "\n")) - (insert "\n" stars " ") - (when (eobp) (save-excursion (insert "\n")))))) + (insert "\n" stars " ")))) ;; 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 8a94c4a16..0b43dfdb4 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -1267,13 +1267,13 @@ (buffer-string)))) ;; In the middle of a headline, split it if allowed. (should - (equal "* H\n* 1\n" + (equal "* H\n* 1" (org-test-with-temp-text "* H1" (let ((org-M-RET-may-split-line '((headline . t)))) (org-insert-heading)) (buffer-string)))) (should - (equal "* H1\n* \n" + (equal "* H1\n* " (org-test-with-temp-text "* H1" (let ((org-M-RET-may-split-line '((headline . nil)))) (org-insert-heading)) @@ -1281,19 +1281,19 @@ ;; However, splitting cannot happen on TODO keywords, priorities or ;; tags. (should - (equal "* TODO H1\n* \n" + (equal "* TODO H1\n* " (org-test-with-temp-text "* TODO H1" (let ((org-M-RET-may-split-line '((headline . t)))) (org-insert-heading)) (buffer-string)))) (should - (equal "* [#A] H1\n* \n" + (equal "* [#A] H1\n* " (org-test-with-temp-text "* [#A] H1" (let ((org-M-RET-may-split-line '((headline . t)))) (org-insert-heading)) (buffer-string)))) (should - (equal "* H1 :tag:\n* \n" + (equal "* H1 :tag:\n* " (org-test-with-temp-text "* H1 :tag:" (let ((org-M-RET-may-split-line '((headline . t)))) (org-insert-heading)) @@ -1320,14 +1320,14 @@ ;; point. (should (equal - "* H1\n** H2\n* \n" + "* H1\n** H2\n* " (org-test-with-temp-text "* H1\n** H2" (let ((org-insert-heading-respect-content nil)) (org-insert-heading '(4))) (buffer-string)))) (should (equal - "* H1\n** H2\n* \n" + "* H1\n** H2\n* " (org-test-with-temp-text "* H1\n** H2" (let ((org-insert-heading-respect-content nil)) (org-insert-heading '(4))) @@ -1335,7 +1335,7 @@ ;; When called with two universal arguments, insert a new headline ;; at the end of the grandparent subtree. (should - (equal "* H1\n** H3\n- item\n** H2\n** \n" + (equal "* H1\n** H3\n- item\n** H2\n** " (org-test-with-temp-text "* H1\n** H3\n- item\n** H2" (let ((org-insert-heading-respect-content nil)) (org-insert-heading '(16))) @@ -1343,7 +1343,7 @@ ;; When optional TOP-LEVEL argument is non-nil, always insert ;; a level 1 heading. (should - (equal "* H1\n** H2\n* \n" + (equal "* H1\n** H2\n* " (org-test-with-temp-text "* H1\n** H2" (org-insert-heading nil nil t) (buffer-string)))) @@ -1354,32 +1354,32 @@ (buffer-string)))) ;; Obey `org-blank-before-new-entry'. (should - (equal "* H1\n\n* \n" + (equal "* H1\n\n* " (org-test-with-temp-text "* H1" (let ((org-blank-before-new-entry '((heading . t)))) (org-insert-heading)) (buffer-string)))) (should - (equal "* H1\n* \n" + (equal "* H1\n* " (org-test-with-temp-text "* H1" (let ((org-blank-before-new-entry '((heading . nil)))) (org-insert-heading)) (buffer-string)))) (should - (equal "* H1\n* H2\n* \n" + (equal "* H1\n* H2\n* " (org-test-with-temp-text "* H1\n* H2" (let ((org-blank-before-new-entry '((heading . auto)))) (org-insert-heading)) (buffer-string)))) (should - (equal "* H1\n\n* H2\n\n* \n" + (equal "* H1\n\n* H2\n\n* " (org-test-with-temp-text "* H1\n\n* H2" (let ((org-blank-before-new-entry '((heading . auto)))) (org-insert-heading)) (buffer-string)))) ;; Corner case: correctly insert a headline after an empty one. (should - (equal "* \n* \n" + (equal "* \n* " (org-test-with-temp-text "* " (org-insert-heading) (buffer-string)))) @@ -1405,7 +1405,7 @@ ;; Properly handle empty lines when forcing a headline below current ;; one. (should - (equal "* H1\n\n* H\n\n* \n" + (equal "* H1\n\n* H\n\n* " (org-test-with-temp-text "* H1\n\n* H" (let ((org-blank-before-new-entry '((heading . t)))) (org-insert-heading '(4)) @@ -1421,14 +1421,14 @@ ;; Add headline at the end of the first subtree (should (equal - "* TODO \n" + "* TODO " (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body" (org-insert-todo-heading-respect-content) (buffer-substring-no-properties (line-beginning-position) (point-max))))) ;; In a list, do not create a new item. (should (equal - "* TODO \n" + "* TODO " (org-test-with-temp-text "* H\n- an item\n- another one" (search-forward "an ") (org-insert-todo-heading-respect-content)