Fix incorrect restriction handling when inserting templates

We must widen the current restriction when searching for the template item, not
when copying the body from it.  In the latter case, `org-with-point-at` will
handle the necessary widening.
This commit is contained in:
Daniel Borchmann 2023-05-06 18:03:14 +02:00
parent 925cf115be
commit 01b5e1b8db
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625

View File

@ -1002,19 +1002,20 @@ determined."
;; If no template has been found so far, search for top-most sibling and ;; If no template has been found so far, search for top-most sibling and
;; whether its headline starts with “Template”; use that when found. ;; whether its headline starts with “Template”; use that when found.
(unless template-marker (unless template-marker
(let ((top-most-sibling (condition-case _ (save-excursion
(save-restriction (widen)
(let ((top-most-sibling (condition-case _
(save-mark-and-excursion (save-mark-and-excursion
(outline-up-heading 1 'invisible-ok) (outline-up-heading 1 'invisible-ok)
(outline-next-heading) (outline-next-heading)
(point))) (point))
(t nil)))) (t nil))))
(when (and top-most-sibling (when (and top-most-sibling
(integerp top-most-sibling) ; just to make sure we have a point here (integerp top-most-sibling) ; just to make sure we have a point here
(string-match-p "^Template.*" (string-match-p "^Template.*"
(org-entry-get top-most-sibling "ITEM"))) (org-entry-get top-most-sibling "ITEM")))
(setq template-marker (org-with-point-at top-most-sibling (setq template-marker (org-with-point-at top-most-sibling
(point-marker)))))) (point-marker)))))))
;; Return `template-marker', which is either `nil' or a marker. ;; Return `template-marker', which is either `nil' or a marker.
template-marker)) template-marker))
@ -1202,31 +1203,29 @@ inserted template."
(interactive (list (db/org-get-location t))) (interactive (list (db/org-get-location t)))
(unless (number-or-marker-p pom) (unless (number-or-marker-p pom)
(user-error "Argument is neither point nor mark: %s" pom)) (user-error "Argument is neither point nor mark: %s" pom))
(let ((body (save-restriction (let ((body (save-mark-and-excursion
(widen) (let ((template-element (org-with-point-at pom
(save-mark-and-excursion (org-element-at-point))))
(let ((template-element (org-with-point-at pom (with-current-buffer (if (markerp pom) (marker-buffer pom) (current-buffer))
(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)))))))))
(cond (cond
;; Open next line if the current line is not blank ;; Open next line if the current line is not blank