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.
This commit is contained in:
Daniel - 2020-09-01 16:23:28 +02:00
parent b9fe0924f2
commit 03f23b11fb
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
1 changed files with 14 additions and 9 deletions

View File

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