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