From 52bc95676c89e2b2460d8a66d44dd41017464b08 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Sat, 19 Aug 2023 10:52:25 +0300 Subject: [PATCH] org-insert-heading: Fix when folded text is kept right at the new heading * lisp/org.el (org-insert-heading): Do not slurp blank lines after previous heading into the contents of the newly added one. These blank lines might be folded and we end up with point before folded spaces * ... * testing/lisp/test-org.el (test-org/insert-heading): Add test. (test-org/insert-todo-heading-respect-content): Do not expect trailing newline after the newly added heading. The test did not intentionally test this particular behavior (other tests do not expect trailing newlines to be _always_ added). Reported-by: Max Nikulin Link: https://orgmode.org/list/ubpcoi$f7n$1@ciao.gmane.io --- lisp/org.el | 5 ++++- testing/lisp/test-org.el | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 02a081339..09805836d 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -6323,7 +6323,10 @@ unconditionally." (if (not level) (outline-next-heading) ;before first headline (org-back-to-heading invisible-ok) (when (equal arg '(16)) (org-up-heading-safe)) - (org-end-of-subtree))) + (org-end-of-subtree invisible-ok 'to-heading))) + ;; At `point-max', if the file does not have ending newline, + ;; create one, so that we are not appending stars at non-empty + ;; line. (unless (bolp) (insert "\n")) (when (and blank? (save-excursion (backward-char) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index db9ad7572..db018b52d 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -1965,7 +1965,21 @@ CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46" (org-test-with-temp-text "* H1\n\n* H" (let ((org-blank-before-new-entry '((heading . t)))) (org-insert-heading '(4)) - (buffer-string)))))) + (buffer-string))))) + ;; Do not include potentially folded empty lines. + (org-test-with-temp-text " +* Sec1 +** SubSec1 + +text + +** SubSec2 + +text +" + (org-content) + (org-insert-heading '(4)) + (should-not (org-fold-folded-p)))) (ert-deftest test-org/insert-todo-heading-respect-content () "Test `org-insert-todo-heading-respect-content' specifications." @@ -1992,7 +2006,7 @@ CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46" ;; Use the same TODO keyword as current heading. (should (equal - "* TODO \n" + "* TODO " (org-test-with-temp-text "* TODO\n** WAITING\n" (org-insert-todo-heading-respect-content) (buffer-substring-no-properties (line-beginning-position) (point-max))))))