Make template copy function more robust

Instead of going upward from the end, we now just start from the beginning and
skip all drawers we may encounter.  This should also allow to copy subtrees in
templates, although adjustments to the headline indentations might be necessary
if the template and point are on different levels.
This commit is contained in:
Daniel Borchmann 2021-12-13 21:13:48 +01:00
parent 159c8c0eb6
commit cedaae00e8
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
1 changed files with 16 additions and 11 deletions

View File

@ -603,18 +603,23 @@ query for it."
(let ((template-element (org-with-point-at pom
;; 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 body.
(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)))
;; Start finding the beginning of the template contents from the top …
(goto-char (org-element-property :contents-begin template-element))
;; … but skip any drawers we may find.
(setq current-element (org-element-at-point))
(while (memq (org-element-type current-element)
'(drawer property-drawer))
(goto-char (org-element-property :end current-element))
(setq current-element (org-element-at-point)))
;; Now we are at the beginning of the contents, let's copy
;; that, but only if it exists and is not empty.
(setq content-begin (org-element-property :begin current-element))
(unless (and content-begin
(< content-begin content-end))
(user-error "Cannot find content in template, or content is empty"))
(buffer-substring-no-properties content-begin content-end))))))))
(insert body)