From 03f23b11fb60de6f47943ccba9cc23ef13013b85 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Tue, 1 Sep 2020 16:23:28 +0200 Subject: [PATCH] Copy complete template for periodic tasks Previously, we only copied the last element in the subtree, assuming that this encompasses all of the content of the subtree. However, this is not true, and thus we have to do something more elaborate. Now, starting from the end of the subtree, we go up all elements in the subtree until we reach either the headline or a drawer. Everything in between is copied as template to the current location. --- site-lisp/db-org.el | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 77ca8f5..44aa083 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -535,15 +535,20 @@ drawers, will be copied to point." (unless (string-equal (org-element-property :title template-element) "Template") (user-error "Template must be first headline in periodic task.")) - ;; XXX: trying to get the contents of the current item, without any - ;; drawers, by going to the end of the template item and marking the - ;; element at point, which, incidentally, seems to be the content we are - ;; looking for; this feels hackish, there must be a better way to do it. - (goto-char (org-element-property :contents-end template-element)) - (org-mark-element) - (string-trim-right - (buffer-substring-no-properties (region-beginning) - (region-end)))))))) + ;; Starting from the end of the last element in the + ;; subtree, we go up until we find a drawer or a + ;; headline; everything in between is considered to be the template + (let ((content-end (org-element-property :contents-end template-element)) + content-begin current-element) + (goto-char content-end) + (while (progn + (setq current-element (org-element-at-point)) + (not (memq (org-element-type current-element) + '(drawer property-drawer headline)))) + (setq content-begin (org-element-property :begin current-element)) + (goto-char (1- content-begin))) + (string-trim-right + (buffer-substring-no-properties content-begin content-end)))))))) (insert template) (org-update-statistics-cookies nil)))