org-log-beginning: Fix regression in c0b66bf

* lisp/org.el (org-log-beginning): Re-implement the bugfix attempted
in c0b66bf differently to avoid slurping blank lines when creating a
new logbook drawer.  Add more commentary to the function.

Reported-by: Christopher M. Miles <numbchild@gmail.com>

The reproducer:

* headline1
(point here)
* headline 2

(setq org-log-into-drawer t)

Press [C-c C-z] become bellowing content:

* headline1
:LOGBOOK:
- Note taken on [2024-06-05 Wed 12:49] \\
  kkk
:END:(point here)
* headline 2
This commit is contained in:
Ihor Radchenko 2024-06-05 16:05:31 +02:00
parent 589e1f38c1
commit 223cc31ec7
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 45 additions and 12 deletions

View File

@ -10674,6 +10674,9 @@ narrowing."
(let ((drawer (org-log-into-drawer))) (let ((drawer (org-log-into-drawer)))
(cond (cond
(drawer (drawer
;; This either moves past planning and property drawer, to
;; first line below heading, or to `eob' (if heading is the
;; last heading in buffer without contents).
(org-end-of-meta-data) (org-end-of-meta-data)
(let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$")) (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
(end (if (org-at-heading-p) (point) (end (if (org-at-heading-p) (point)
@ -10690,24 +10693,54 @@ narrowing."
(throw 'exit nil)))) (throw 'exit nil))))
;; No drawer found. Create one, if permitted. ;; No drawer found. Create one, if permitted.
(when create (when create
;; `org-end-of-meta-data' ended up at next heading
;; * Heading to insert darawer<maybe folded>
;; * Another heading
;;
;; Unless current heading is the last heading in buffer ;; Unless current heading is the last heading in buffer
;; and does not have a newline, `org-end-of-meta-data' ;; and does not have a newline, `org-end-of-meta-data'
;; should move us somewhere below the heading. ;; can move us to the next heading.
;; Avoid situation when we insert drawer right before ;; Avoid situation when we insert drawer right before
;; first "*". Otherwise, if the previous heading is ;; first "*". Otherwise, if the heading is folded, we
;; folded, we are inserting after visible newline at ;; are inserting after visible newline at the end of the
;; the end of the fold, thus breaking the fold ;; fold, thus breaking the fold continuity.
;; continuity.
(unless (eobp) (unless (eobp)
(when (org-at-heading-p) (backward-char))) (when (org-at-heading-p) (backward-char)))
(org-fold-core-ignore-modifications (org-fold-core-ignore-modifications
(unless (bolp) (insert-and-inherit "\n")) (let (;; Heading
(let ((beg (point))) ;; <point>
(insert-and-inherit ":" drawer ":\n:END:") ;; Text
(if (eolp) (forward-char) (insert "\n")) (at-blank-line? (looking-at-p "^[ \t]*$"))
(org-indent-region beg (point)) ;; Heading
(org-fold-region (line-end-position -1) (1- (point)) t 'drawer)))) ;; <point>Text
(end-of-line -1)))) (at-beginning-of-non-blank-line?
(and (bolp) (not (eolp)))))
(unless (bolp)
;; Heading<point> (see `backward-char' branch above)
(insert-and-inherit "\n"))
(let ((beg (point)) cbeg)
(insert-and-inherit ":" drawer ":")
(setq cbeg (point))
(insert-and-inherit "\n:END:")
(cond
(at-blank-line?
;; Heading
;; :LOGBOOK:
;; :END:
;;
;; Text
(insert "\n")
(backward-char))
(at-beginning-of-non-blank-line?
;; Heading
;; :LOGBOOK:
;; :END:
;; Text
(insert "\n")
(backward-char)))
(org-indent-region beg (point))
(org-fold-region cbeg (point) t 'drawer)))))
(end-of-line 0))))
(t (t
(org-end-of-meta-data org-log-state-notes-insert-after-drawers) (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
(let ((endpos (point))) (let ((endpos (point)))