Fix template copy mechanism when template is in other buffer

Forgot to switch buffer when marker in other buffer is given.
This commit is contained in:
Daniel - 2022-10-03 10:11:17 +02:00
parent 12b001221c
commit bc3ab8b901
Signed by: dbo
GPG Key ID: 784AA8DF0CCDF625
1 changed files with 20 additions and 19 deletions

View File

@ -924,25 +924,26 @@ it. Adds newline before and after the template."
(save-mark-and-excursion (save-mark-and-excursion
(let ((template-element (org-with-point-at pom (let ((template-element (org-with-point-at pom
(org-element-at-point)))) (org-element-at-point))))
(let ((content-end (org-element-property :contents-end template-element)) (with-current-buffer (if (markerp pom) (marker-buffer pom) (current-buffer))
current-element (let ((content-end (org-element-property :contents-end template-element))
content-begin) current-element
;; Start finding the beginning of the template contents from the top … content-begin)
(goto-char (org-element-property :contents-begin template-element)) ;; Start finding the beginning of the template contents from the top …
;; … but skip any drawers we may find. (goto-char (org-element-property :contents-begin template-element))
(setq current-element (org-element-at-point)) ;; … but skip any drawers we may find.
(while (memq (org-element-type current-element) (setq current-element (org-element-at-point))
'(drawer property-drawer)) (while (memq (org-element-type current-element)
(goto-char (org-element-property :end current-element)) '(drawer property-drawer))
(setq current-element (org-element-at-point))) (goto-char (org-element-property :end current-element))
;; Now we are at the beginning of the contents, let's copy (setq current-element (org-element-at-point)))
;; that, but only if it exists and is not empty. ;; Now we are at the beginning of the contents, let's copy
(setq content-begin (org-element-property :begin current-element)) ;; that, but only if it exists and is not empty.
(unless (and content-begin (setq content-begin (org-element-property :begin current-element))
(< content-begin content-end)) (unless (and content-begin
(user-error "Cannot find content in template, or content is empty")) (< content-begin content-end))
(string-trim-right (user-error "Cannot find content in template, or content is empty"))
(buffer-substring-no-properties content-begin content-end)))))))) (string-trim-right
(buffer-substring-no-properties content-begin content-end)))))))))
(insert "\n") (insert "\n")
(insert body) (insert body)
(insert "\n") (insert "\n")