mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-14 05:23:04 +00:00
fix: correctly allow functions as snippet forms
also make define-snippet expand directly to a defun
This commit is contained in:
parent
123f204d20
commit
33c696516a
34
snippet.el
34
snippet.el
@ -57,6 +57,13 @@
|
||||
|
||||
(defvar snippet--form-mirror-sym-idx nil)
|
||||
|
||||
(defun snippet--function-p (form)
|
||||
(or (functionp form)
|
||||
(and (eq 'function (first form))
|
||||
(fboundp (second form)))
|
||||
(and (eq 'quote (first form))
|
||||
(fboundp (second form)))))
|
||||
|
||||
(defun snippet--form-sym-tuples (forms &optional parent-field-sym)
|
||||
"Produce information for composing the snippet expansion function.
|
||||
|
||||
@ -95,11 +102,11 @@ iterated depth-first, resulting in a flattened list."
|
||||
,@(when childrenp
|
||||
(snippet--form-sym-tuples (third form) sym))))
|
||||
((null form) nil)
|
||||
|
||||
((or (stringp form)
|
||||
(symbolp form)
|
||||
(eq (car form) 'lambda))
|
||||
`((ignore ,form ,parent-field-sym))))
|
||||
(snippet--function-p form))
|
||||
`((string-or-function ,form ,parent-field-sym)))
|
||||
(t
|
||||
(error "unknown type of snippet form %s" form)))
|
||||
do (setq adjacent-prev-sym sym)))
|
||||
|
||||
(defun snippet--make-marker-init-forms (tuples)
|
||||
@ -215,7 +222,7 @@ I would need these somewhere in the let* form
|
||||
|
||||
|
||||
(defmacro define-snippet (name _args &rest body)
|
||||
"Define NAME as a snippet.
|
||||
"Define NAME as a snippet-inserting function.
|
||||
|
||||
NAME's function definition is set to a function with no arguments
|
||||
that inserts the fields components at point.
|
||||
@ -255,8 +262,7 @@ can be:
|
||||
(marker-init-forms (snippet--make-marker-init-forms sym-tuples))
|
||||
(init-object-forms (snippet--init-field-and-mirror-forms sym-tuples))
|
||||
(first-field-sym (snippet--first-field-sym sym-tuples)))
|
||||
`(let ((insert-snippet-fn
|
||||
#'(lambda ()
|
||||
`(defun ,name ()
|
||||
(let* (,@(mapcar #'first init-object-forms)
|
||||
,@marker-init-forms)
|
||||
|
||||
@ -265,13 +271,14 @@ can be:
|
||||
,@(loop
|
||||
for (sym form) in sym-tuples
|
||||
collect (cond ((snippet--form-field-p form)
|
||||
`(snippet--insert-field ,sym ,(if (stringp (third form))
|
||||
`(snippet--insert-field ,sym ,(if (stringp
|
||||
(third form))
|
||||
(third form))))
|
||||
((snippet--form-mirror-p form)
|
||||
`(snippet--insert-mirror ,sym))
|
||||
((stringp form)
|
||||
`(insert ,form))
|
||||
((functionp form)
|
||||
((snippet--function-p form)
|
||||
`(insert (funcall ,form)))))
|
||||
|
||||
(setq snippet--field-overlay
|
||||
@ -293,13 +300,12 @@ can be:
|
||||
snippet-field-keymap)
|
||||
(overlay-put snippet--field-overlay
|
||||
'snippet--objects
|
||||
(list ,@(remove 'ignore (mapcar #'first sym-tuples))))
|
||||
(list ,@(remove 'string-or-function
|
||||
(mapcar #'first
|
||||
sym-tuples))))
|
||||
,(if first-field-sym
|
||||
`(snippet--move-to-field ,first-field-sym))
|
||||
(add-hook 'post-command-hook 'snippet--post-command-hook t t)
|
||||
(snippet--post-command-hook)))))
|
||||
(defun ,name ()
|
||||
(funcall insert-snippet-fn)))))
|
||||
(add-hook 'post-command-hook 'snippet--post-command-hook t t)))))
|
||||
|
||||
|
||||
;;; Snippet mechanics
|
||||
|
Loading…
x
Reference in New Issue
Block a user