fix: widen buffer when checking yasnippet condition

In certain circumstances, `yasnippet` will narrow the buffer to its own
region while expanding, which prevents certain snippet conditions from
working properly.
This commit is contained in:
Kiana Sheibani 2026-02-14 18:47:30 -05:00
parent 1ab7ecb9c9
commit 48fc692361
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -3364,20 +3364,24 @@ Writing header arguments for source code blocks is confusing, so let's automate
#+begin_src emacs-lisp
(defun ~/yas-org-src-header-p ()
"Determine whether `point' is within a src-block header or header-args."
(let ((context (org-element-context)))
(pcase (org-element-type context)
('src-block (< (point) ; before code part of the src-block
(save-excursion
(goto-char (org-element-property :begin context))
(forward-line 1)
(point))))
('inline-src-block (< (point) ; before code part of the inline-src-block
(save-excursion
(goto-char (org-element-property :begin context))
(search-forward "]{")
(point))))
('keyword (string-match-p "^header-args"
(org-element-property :value context))))))
;; Temporarily widen buffer
;; This avoids issues when checking during nested expansion,
;; since yasnippet narrows to the current field in that case
(org-with-wide-buffer
(let ((context (org-element-context)))
(pcase (org-element-type context)
('src-block (< (point) ; before code part of the src-block
(progn
(goto-char (org-element-property :begin context))
(forward-line 1)
(point))))
('inline-src-block (< (point) ; before code part of the inline-src-block
(progn
(goto-char (org-element-property :begin context))
(search-forward "]{")
(point))))
('keyword (string-match-p "^header-args"
(org-element-property :value context)))))))
(defun ~/yas-org-src-lang ()
"Try to find the current language of the src/header at `point'.