Ensure `CHECKLIST_INSERTED_P` will be inserted at original heading

When a checklist template contains headings on its own, the
`CHECKLIST_INSERTED_P` property until now would be inserted at the last heading
in this template, instead of at the heading where the template is supposed to be
inserted in the first place.  Fixed this.
This commit is contained in:
Daniel - 2023-05-06 15:30:16 +02:00
parent 7db9f8d56c
commit fa56dfdd4d
Signed by: dbo
GPG Key ID: 784AA8DF0CCDF625
1 changed files with 53 additions and 48 deletions

View File

@ -1090,61 +1090,66 @@ inserting the checklist."
(t ;; Default action: insert complete checklist. (t ;; Default action: insert complete checklist.
;; Checklists are inserted directly before first child, if existent, or ;; Let's remember where we are, so that latter on CHECKLIST_INSERTED_P
;; at end of subtree ;; will be inserted at the original heading (where we are now) and not
(org-show-entry) ;; at possible new subtrees coming from the template.
(or (org-goto-first-child) (save-mark-and-excursion
(org-end-of-subtree 'invisible-ok 'to-heading))
;; Move back from heading, unless we are at the end of the buffer
(when (org-at-heading-p)
;; Go to end of line before heading
(forward-char -1))
;; Insert relevant backlinks, when available. ;; Checklists are inserted directly before first child, if existent, or
(let ((parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil) ;; at end of subtree
(string-to-number it))) (org-show-entry)
number-of-backlinks (or (org-goto-first-child)
point-before-backlinks) (org-end-of-subtree 'invisible-ok 'to-heading))
;; Move back from heading, unless we are at the end of the buffer
(when (org-at-heading-p)
;; Go to end of line before heading
(forward-char -1))
;; Insert blank line, but only if the previous line is not blank ;; Insert relevant backlinks, when available.
;; already. (let ((parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil)
(unless (save-mark-and-excursion (string-to-number it)))
(forward-line -1) number-of-backlinks
(looking-at (rx bol (* space) eol))) point-before-backlinks)
(insert "\n"))
(insert (format "Relevant backlinks (%s):\n\n" ;; Insert blank line, but only if the previous line is not blank
(if parent-depth ;; already.
(format "parent-depth %d" parent-depth) (unless (save-mark-and-excursion
"all parents"))) (forward-line -1)
(looking-at (rx bol (* space) eol)))
(insert "\n"))
;; Store where we are (minus the two newlines) so we can delete the (insert (format "Relevant backlinks (%s):\n\n"
;; checklist in case it's empty. (if parent-depth
(setq point-before-backlinks (- (point) 2)) (format "parent-depth %d" parent-depth)
"all parents")))
(setq number-of-backlinks ;; Store where we are (minus the two newlines) so we can delete the
(org-dblock-write:db/org-backlinks (list ;; checklist in case it's empty.
:org-ql-match '(and (setq point-before-backlinks (- (point) 2))
(not (done))
(not (ltags "TEMPLATE"))
(not (scheduled :from 1))
(not (property "CHECKLIST_NO_BACKLINK" "t" :inherit nil)))
:parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil)
(string-to-number it))
:archive nil)))
;; When no backlinks have been found, remove the empty table head and just (setq number-of-backlinks
;; print "none". (org-dblock-write:db/org-backlinks (list
(when (zerop number-of-backlinks) :org-ql-match '(and
(delete-region point-before-backlinks (point)) (not (done))
(insert " none."))) (not (ltags "TEMPLATE"))
(not (scheduled :from 1))
(not (property "CHECKLIST_NO_BACKLINK" "t" :inherit nil)))
:parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil)
(string-to-number it))
:archive nil)))
;; Insert template, when avilable. ;; When no backlinks have been found, remove the empty table head and just
(let ((template-marker (db/org--find-template))) ;; print "none".
(insert "\n\nTemplate:") (when (zerop number-of-backlinks)
(if (not template-marker) (delete-region point-before-backlinks (point))
(insert " none.\n") (insert " none.")))
(db/org-copy-body-from-item-to-point template-marker)))
;; Insert template, when avilable.
(let ((template-marker (db/org--find-template)))
(insert "\n\nTemplate:")
(if (not template-marker)
(insert " none.\n")
(db/org-copy-body-from-item-to-point template-marker))))
(org-entry-put (point) "CHECKLIST_INSERTED_P" "t") (org-entry-put (point) "CHECKLIST_INSERTED_P" "t")
(db/org-goto-first-open-checkbox-in-subtree)))) (db/org-goto-first-open-checkbox-in-subtree))))