org-insert-heading: Prevent 0-blanks after heading when there is blank before

* lisp/org.el (org-insert-heading): When creating a new heading with
blank lines before results in the _next_ heading to have no blank
lines, add them.

Reported-by: gusbrs <gusbrs.2016@gmail.com>
Link: https://orgmode.org/list/877cjl67z6.fsf@localhost
This commit is contained in:
Ihor Radchenko 2024-05-10 12:15:40 +03:00
parent 3bf33c0d7b
commit f64c8a5a5b
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 24 additions and 4 deletions

View File

@ -6409,7 +6409,17 @@ but not a number, insert a level-1 heading."
current-level current-level
;; This `1' is for when before first headline ;; This `1' is for when before first headline
1)) 1))
(stars (make-string num-stars ?*))) (stars (make-string num-stars ?*))
(maybe-add-blank-after
(lambda (blank?)
"Add a blank line before next heading when BLANK? is non-nil.
Assume that point is on the inserted heading."
(save-excursion
(end-of-line)
(unless (eobp)
(forward-char)
(when (and blank? (org-at-heading-p))
(insert "\n")))))))
(cond (cond
((or org-insert-heading-respect-content ((or org-insert-heading-respect-content
(member arg '((4) (16))) (member arg '((4) (16)))
@ -6439,6 +6449,8 @@ but not a number, insert a level-1 heading."
(insert stars " " "\n") (insert stars " " "\n")
;; Move point after stars. ;; Move point after stars.
(backward-char) (backward-char)
;; Retain blank lines before next heading.
(funcall maybe-add-blank-after blank?)
;; When INVISIBLE-OK is non-nil, ensure newly created headline ;; When INVISIBLE-OK is non-nil, ensure newly created headline
;; is visible. ;; is visible.
(unless invisible-ok (unless invisible-ok
@ -6473,23 +6485,31 @@ but not a number, insert a level-1 heading."
(end-of-line) (end-of-line)
(when blank? (insert "\n")) (when blank? (insert "\n"))
(insert "\n" stars " ") (insert "\n" stars " ")
;; Retain blank lines before next heading.
(funcall maybe-add-blank-after blank?)
(when (org-string-nw-p split) (insert split)))) (when (org-string-nw-p split) (insert split))))
(t (t
(end-of-line) (end-of-line)
(when blank? (insert "\n")) (when blank? (insert "\n"))
(insert "\n" stars " ")))) (insert "\n" stars " ")
;; Retain blank lines before next heading.
(funcall maybe-add-blank-after blank?))))
;; On regular text, turn line into a headline or split, if ;; On regular text, turn line into a headline or split, if
;; appropriate. ;; appropriate.
((bolp) ((bolp)
(insert stars " ") (insert stars " ")
(unless (and blank? (org-previous-line-empty-p)) (unless (and blank? (org-previous-line-empty-p))
(org-N-empty-lines-before-current (if blank? 1 0)))) (org-N-empty-lines-before-current (if blank? 1 0)))
;; Retain blank lines before next heading.
(funcall maybe-add-blank-after blank?))
(t (t
(unless (org-get-alist-option org-M-RET-may-split-line 'headline) (unless (org-get-alist-option org-M-RET-may-split-line 'headline)
(end-of-line)) (end-of-line))
(insert "\n" stars " ") (insert "\n" stars " ")
(unless (and blank? (org-previous-line-empty-p)) (unless (and blank? (org-previous-line-empty-p))
(org-N-empty-lines-before-current (if blank? 1 0)))))) (org-N-empty-lines-before-current (if blank? 1 0)))
;; Retain blank lines before next heading.
(funcall maybe-add-blank-after blank?))))
(run-hooks 'org-insert-heading-hook)) (run-hooks 'org-insert-heading-hook))
(defun org-N-empty-lines-before-current (n) (defun org-N-empty-lines-before-current (n)