mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 21:13:04 +00:00
Use expand-env for all snippet evaluations
* yasnippet.el (yas--snippet): New field, expand-env. (yas--letenv): New macro, evaluate body with a given environment. (yas--find-next-field, yas--safely-run-hooks): (yas--on-field-overlay-modification): (yas-expand-snippet, yas--snippet-create): Use it.
This commit is contained in:
parent
5534cab0b7
commit
4ee3835adf
33
yasnippet.el
33
yasnippet.el
@ -2902,10 +2902,11 @@ Use this in primary and mirror transformations to tget."
|
||||
(put 'yas--active-field-overlay 'permanent-local t)
|
||||
(put 'yas--field-protection-overlays 'permanent-local t)
|
||||
|
||||
(cl-defstruct (yas--snippet (:constructor yas--make-snippet ()))
|
||||
(cl-defstruct (yas--snippet (:constructor yas--make-snippet (expand-env)))
|
||||
"A snippet.
|
||||
|
||||
..."
|
||||
expand-env
|
||||
(fields '())
|
||||
(exit nil)
|
||||
(id (yas--snippet-next-id) :read-only t)
|
||||
@ -2954,6 +2955,12 @@ DEPTH is a count of how many nested mirrors can affect this mirror"
|
||||
marker
|
||||
next)
|
||||
|
||||
(defmacro yas--letenv (env &rest body)
|
||||
"Evaluate BODY with bindings from ENV.
|
||||
ENV is a list of elements with the form (VAR FORM)."
|
||||
(declare (debug (form body)) (indent 1))
|
||||
`(eval (cl-list* 'let* ,env ',body)))
|
||||
|
||||
(defun yas--apply-transform (field-or-mirror field &optional empty-on-nil-p)
|
||||
"Calculate transformed string for FIELD-OR-MIRROR from FIELD.
|
||||
|
||||
@ -3102,6 +3109,7 @@ If there's none, exit the snippet."
|
||||
(let* ((snippet (car (yas-active-snippets)))
|
||||
(active-field (overlay-get yas--active-field-overlay 'yas--field))
|
||||
(target-field (yas--find-next-field arg snippet active-field)))
|
||||
(yas--letenv (yas--snippet-expand-env snippet)
|
||||
;; Apply transform to active field.
|
||||
(when active-field
|
||||
(let ((yas-moving-away-p t))
|
||||
@ -3110,7 +3118,7 @@ If there's none, exit the snippet."
|
||||
;; Now actually move...
|
||||
(if target-field
|
||||
(yas--move-to-field snippet target-field)
|
||||
(yas-exit-snippet snippet))))
|
||||
(yas-exit-snippet snippet)))))
|
||||
|
||||
(defun yas--place-overlays (snippet field)
|
||||
"Correctly place overlays for SNIPPET's FIELD."
|
||||
@ -3239,6 +3247,7 @@ If so cleans up the whole snippet up."
|
||||
(snippet-exit-transform))
|
||||
(dolist (snippet snippets)
|
||||
(let ((active-field (yas--snippet-active-field snippet)))
|
||||
(yas--letenv (yas--snippet-expand-env snippet)
|
||||
(setq snippet-exit-transform (yas--snippet-force-exit snippet))
|
||||
(cond ((or snippet-exit-transform
|
||||
(not (and active-field (yas--field-contains-point-p active-field))))
|
||||
@ -3257,7 +3266,7 @@ If so cleans up the whole snippet up."
|
||||
(yas--move-to-field snippet active-field)
|
||||
(yas--update-mirrors snippet)))
|
||||
(t
|
||||
nil))))
|
||||
nil)))))
|
||||
(unless (or (null snippets) snippets-left)
|
||||
(if snippet-exit-transform
|
||||
(yas--eval-lisp-no-saves snippet-exit-transform))
|
||||
@ -3428,6 +3437,7 @@ field start. This hook does nothing if an undo is in progress."
|
||||
(field (overlay-get overlay 'yas--field))
|
||||
(snippet (overlay-get yas--active-field-overlay 'yas--snippet)))
|
||||
(save-match-data
|
||||
(yas--letenv (yas--snippet-expand-env snippet)
|
||||
(when (yas--skip-and-clear-field-p field beg end length)
|
||||
;; We delete text starting from the END of insertion.
|
||||
(yas--skip-and-clear field end))
|
||||
@ -3435,7 +3445,7 @@ field start. This hook does nothing if an undo is in progress."
|
||||
(yas--advance-end-maybe field (overlay-end overlay))
|
||||
(save-excursion
|
||||
(yas--field-update-display field))
|
||||
(yas--update-mirrors snippet)))))
|
||||
(yas--update-mirrors snippet))))))
|
||||
|
||||
;;; Apropos protection overlays:
|
||||
;;
|
||||
@ -3582,13 +3592,9 @@ considered when expanding the snippet."
|
||||
;; insert things that are not valid in the
|
||||
;; major-mode language syntax anyway.
|
||||
(syntax-propertize-function nil))
|
||||
(insert content)
|
||||
(setq snippet
|
||||
(if expand-env
|
||||
(eval `(let* ,expand-env
|
||||
(insert content)
|
||||
(yas--snippet-create start (point))))
|
||||
(insert content)
|
||||
(yas--snippet-create start (point)))))
|
||||
(yas--snippet-create expand-env start (point))))
|
||||
;; Invalidate any syntax-propertizing done while `syntax-propertize-function' was nil
|
||||
(syntax-ppss-flush-cache start))
|
||||
|
||||
@ -3668,13 +3674,14 @@ After revival, push the `yas--take-care-of-redo' in the
|
||||
(push `(apply yas--take-care-of-redo ,beg ,end ,snippet)
|
||||
buffer-undo-list))))
|
||||
|
||||
(defun yas--snippet-create (begin end)
|
||||
(defun yas--snippet-create (expand-env begin end)
|
||||
"Create a snippet from a template inserted at BEGIN to END.
|
||||
|
||||
Returns the newly created snippet."
|
||||
(save-restriction
|
||||
(narrow-to-region begin end)
|
||||
(let ((snippet (yas--make-snippet)))
|
||||
(let ((snippet (yas--make-snippet expand-env)))
|
||||
(yas--letenv expand-env
|
||||
(goto-char begin)
|
||||
(yas--snippet-parse-create snippet)
|
||||
|
||||
@ -3688,7 +3695,7 @@ Returns the newly created snippet."
|
||||
;; Move to end
|
||||
(goto-char (point-max))
|
||||
|
||||
snippet)))
|
||||
snippet))))
|
||||
|
||||
|
||||
;;; Apropos adjacencies and "fom's":
|
||||
|
Loading…
x
Reference in New Issue
Block a user