From 69f3f80a6e4ba09113842ccdf18dbf2c8c0ffa80 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sat, 12 Nov 2022 16:56:29 +0100 Subject: [PATCH] Allow empty template when inserting checklists to Org item In some cases, the backlinks may be sufficient as checklist. --- site-lisp/db-org.el | 69 +++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 401faf9..6052b6a 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -849,43 +849,14 @@ The template is determined by the TEMPLATE_ID property, which must be an ID referencing the proper template item. If that property is not set, search for the topmost sibling of the current item and see whether its headline is matching -\"^Template.*\"; if so, use its body as template, and barf -otherwise." +\"^Template.*\"; if so, use its body as template." (interactive) (unless (derived-mode-p 'org-mode) (user-error "Not in Org mode, aborting")) - (let (template-pom) - - ;; Check for TEMPLATE_ID property - (when-let ((template-id (org-entry-get (point) "TEMPLATE_ID"))) - (setq template-pom (org-id-find template-id :get-marker)) - (unless template-pom - (warn "TEMPLATE_ID is set, but could not be resolved: %s" - template-id))) - - ;; If no template has been found so far, search for top-most sibling and - ;; whether its headline starts with “Template”; use that when found. - (unless template-pom - (let ((top-most-sibling (condition-case _ - (save-restriction - (save-mark-and-excursion - (outline-up-heading 1 'invisible-ok) - (outline-next-heading) - (point))) - (t nil)))) - (when (and top-most-sibling - (integerp top-most-sibling) ; just to make sure we have a - ; point here - (string-match-p "^Template.*" - (org-entry-get top-most-sibling "ITEM"))) - (setq template-pom top-most-sibling)))) - - (unless template-pom - (user-error "Cannot find template via TEMPLATE_ID property or top-most sibling")) - - (let ((parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil) + ;; Insert relevant backlinks, when available. + (let ((parent-depth (--when-let (org-entry-get (point) "CHECKLIST_BACKLINK_DEPTH" nil) (string-to-number it))) number-of-backlinks point-before-backlinks) @@ -915,8 +886,38 @@ otherwise." (delete-region point-before-backlinks (point)) (insert " none."))) - (insert "\n\nTemplate:\n") - (db/org-copy-body-from-item-to-point template-pom))) + ;; Insert template, when avilable. + (let (template-pom) + + (insert "\n\nTemplate:") + + ;; Check for TEMPLATE_ID property + (when-let ((template-id (org-entry-get (point) "TEMPLATE_ID"))) + (setq template-pom (org-id-find template-id :get-marker)) + (unless template-pom + (warn "TEMPLATE_ID is set, but could not be resolved: %s" + template-id))) + + ;; If no template has been found so far, search for top-most sibling and + ;; whether its headline starts with “Template”; use that when found. + (unless template-pom + (let ((top-most-sibling (condition-case _ + (save-restriction + (save-mark-and-excursion + (outline-up-heading 1 'invisible-ok) + (outline-next-heading) + (point))) + (t nil)))) + (when (and top-most-sibling + (integerp top-most-sibling) ; just to make sure we have a point here + (string-match-p "^Template.*" + (org-entry-get top-most-sibling "ITEM"))) + (setq template-pom top-most-sibling)))) + + (if (not template-pom) + (insert " none.") + (insert "\n") + (db/org-copy-body-from-item-to-point template-pom)))) (define-obsolete-function-alias 'db/org-copy-template 'db/org-insert-checklist