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 * <point>...
* 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 <manikulin@gmail.com>
Link: https://orgmode.org/list/ubpcoi$f7n$1@ciao.gmane.io
This commit is contained in:
Ihor Radchenko 2023-08-19 10:52:25 +03:00
parent a25d00d5df
commit 52bc95676c
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 20 additions and 3 deletions

View File

@ -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)

View File

@ -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<point>"
(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 "
<point>* 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))))))