Do not print “Template” in checklist when no other items exist

I tend to remove “Template” anyway when it's the only block inserted as
checklist, so let's do this automatically.

This commit also includes a minor refactoring of moving around variable
declarations to be closer to the code that actually uses them.
This commit is contained in:
Daniel Borchmann 2024-12-20 10:50:41 +01:00
parent 3a397139f2
commit 60463a01e3
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625

View File

@ -1438,12 +1438,6 @@ inserting the checklist."
(t ;; Default action: insert complete checklist. (t ;; Default action: insert complete checklist.
(let (point-before-template
point-after-template)
;; Let's remember where we are, so that latter on CHECKLIST_INSERTED_P
;; will be inserted at the original heading (where we are now) and not
;; at possible new subtrees coming from the template.
(save-mark-and-excursion (save-mark-and-excursion
;; Checklists are inserted directly before first child, if existent, or ;; Checklists are inserted directly before first child, if existent, or
@ -1462,8 +1456,13 @@ inserting the checklist."
(looking-at (rx bol (* space) eol))) (looking-at (rx bol (* space) eol)))
(insert "\n")) (insert "\n"))
;; Insert actual checklist consisting of concurrent dates, relevant backlinks, and a template
(let (concurrent-dates
number-of-backlinks)
;; Insert links to concurrent DATEs, if any ;; Insert links to concurrent DATEs, if any
(when-let ((concurrent-dates (org-ql-query :from (org-agenda-files)
(setq concurrent-dates (org-ql-query :from (org-agenda-files)
:select '(cons :select '(cons
(org-entry-get (point) "ITEM") (org-entry-get (point) "ITEM")
(org-id-get-create)) (org-id-get-create))
@ -1472,20 +1471,19 @@ inserting the checklist."
(not (done)) (not (done))
;; XXX: calling `ts-now' twice might be stupid ;; XXX: calling `ts-now' twice might be stupid
(ts-active :from ,(ts-now)) (ts-active :from ,(ts-now))
(ts-active :to ,(ts-now)))))) (ts-active :to ,(ts-now)))))
(when concurrent-dates
(insert "Concurrent DATEs:\n") (insert "Concurrent DATEs:\n")
(dolist (date concurrent-dates) (dolist (date concurrent-dates)
(insert "- " (org-link-make-string (format "id:%s" (cdr date)) (car date)) "\n")) (insert "- " (org-link-make-string (format "id:%s" (cdr date)) (car date)) "\n"))
(insert "\n")) (insert "\n"))
;; Insert relevant backlinks, when available. ;; Insert relevant backlinks
(let ((parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil)
(string-to-number it)))
number-of-backlinks
point-before-backlinks)
;; Store where we are so we can delete the checklist in case it's empty. (let ((point-before-backlinks (point))
(setq point-before-backlinks (point)) (parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil)
(string-to-number it))))
(insert (format "Relevant backlinks (%s):\n\n" (insert (format "Relevant backlinks (%s):\n\n"
(if parent-depth (if parent-depth
@ -1510,17 +1508,19 @@ inserting the checklist."
(delete-region point-before-backlinks (point)) (delete-region point-before-backlinks (point))
(insert "\n\n"))) (insert "\n\n")))
;; Insert template, when avilable. ;; And finally insert a template
(let ((template-marker (db/org--find-template)))
(insert "Template:")
(setq point-before-template (point))
(if (not template-marker)
(insert " none.\n")
(db/org-copy-body-from-item-to-point template-marker))
(setq point-after-template (point))))
(org-entry-put (point) "CHECKLIST_INSERTED_P" "t") (when-let ((template-marker (db/org--find-template)))
(org-update-statistics-cookies nil) (let (point-before-template
point-after-template)
(setq point-before-template (point))
(unless (and (zerop number-of-backlinks)
(null concurrent-dates))
;; When there are no backlinks, there is no need to print “Template” as a
;; separator
(insert "Template:"))
(db/org-copy-body-from-item-to-point template-marker)
(setq point-after-template (point))
;; Remove any existing ID properties, as they would be duplicates ;; Remove any existing ID properties, as they would be duplicates
;; now. Only do this in the part inserted with template, though, and ;; now. Only do this in the part inserted with template, though, and
@ -1533,9 +1533,12 @@ inserting the checklist."
(org-entry-delete (point) "ID") (org-entry-delete (point) "ID")
(org-entry-delete (point) "CUSTOM_ID")) (org-entry-delete (point) "CUSTOM_ID"))
nil nil
'region)) 'region))))))
(db/org-goto-first-open-checkbox-in-headline))))) (org-entry-put (point) "CHECKLIST_INSERTED_P" "t")
(org-update-statistics-cookies nil)
(db/org-goto-first-open-checkbox-in-headline))))
(define-obsolete-function-alias 'db/org-copy-template (define-obsolete-function-alias 'db/org-copy-template
'db/org-insert-checklist 'db/org-insert-checklist