Remove ID and CUSTOM_ID properties when inserting checklist template

When the template associated with the item at point contains sub-items itself,
those may have been assigned ID or CUSTOM_ID properties.  Copying the template
would thus duplicate these properties, violating their implicit uniqueness
constraint, so we now remove those properties from the inserted checklist
template.
This commit is contained in:
Daniel - 2023-05-06 16:54:38 +02:00
parent fa56dfdd4d
commit 925cf115be
Signed by: dbo
GPG Key ID: 784AA8DF0CCDF625
1 changed files with 84 additions and 65 deletions

View File

@ -1090,6 +1090,9 @@ inserting the 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.
@ -1147,12 +1150,29 @@ inserting the checklist."
;; Insert template, when avilable.
(let ((template-marker (db/org--find-template)))
(insert "\n\nTemplate:")
(setq point-before-template (point))
(if (not template-marker)
(insert " none.\n")
(db/org-copy-body-from-item-to-point template-marker))))
(db/org-copy-body-from-item-to-point template-marker))
(setq point-after-template (point))))
(org-entry-put (point) "CHECKLIST_INSERTED_P" "t")
(db/org-goto-first-open-checkbox-in-subtree))))
(org-update-statistics-cookies nil)
;; Remove any existing ID properties, as they would be duplicates
;; now. Only do this in the part inserted with template, though, and
;; leave previously existing child items and the item itself as they
;; are.
(save-mark-and-excursion
(set-mark point-before-template)
(goto-char point-after-template)
(org-map-entries #'(lambda ()
(org-entry-delete (point) "ID")
(org-entry-delete (point) "CUSTOM_ID"))
nil
'region))
(db/org-goto-first-open-checkbox-in-subtree)))))
(define-obsolete-function-alias 'db/org-copy-template
'db/org-insert-checklist
@ -1183,6 +1203,7 @@ inserted template."
(unless (number-or-marker-p pom)
(user-error "Argument is neither point nor mark: %s" pom))
(let ((body (save-restriction
(widen)
(save-mark-and-excursion
(let ((template-element (org-with-point-at pom
(org-element-at-point))))
@ -1224,9 +1245,7 @@ inserted template."
(unless (save-mark-and-excursion
(forward-line 1)
(looking-at (rx bol (* space) eol)))
(insert "\n"))
(org-update-statistics-cookies nil)))
(insert "\n"))))
(defun db/org-goto-first-open-checkbox-in-subtree (&optional silent)
"Jump to first open checkbox in the current subtree.