Allow empty template when inserting checklists to Org item

In some cases, the backlinks may be sufficient as checklist.
This commit is contained in:
Daniel - 2022-11-12 16:56:29 +01:00
parent 06ec5f3d9e
commit 69f3f80a6e
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625
1 changed files with 35 additions and 34 deletions

View File

@ -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