mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 21:13:04 +00:00
* yasnippet.el (yas-buffer-local-condition): Accept functions
(yas-not-string-or-comment-condition): Make it a function. (yas--funcall-condition): Rename from `yas--funcall-condition`, change its calling convention. (yas--template-can-expand-p, yas--describe-pretty-table): Adjust accordingly. (yas--require-template-specific-condition-p): Add support for functions in `yas-buffer-local-condition`. * doc/snippet-expansion.org (The condition system <<condition-system>>): Adjust example to avoid quoted code.
This commit is contained in:
parent
25f5d8808a
commit
1d0966ae34
@ -212,9 +212,10 @@ inside a comment? Set [[sym:yas-buffer-local-condition][=yas-buffer-local-condi
|
|||||||
(add-hook 'python-mode-hook
|
(add-hook 'python-mode-hook
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(setq yas-buffer-local-condition
|
(setq yas-buffer-local-condition
|
||||||
'(if (python-syntax-comment-or-string-p)
|
(lambda ()
|
||||||
|
(if (python-syntax-comment-or-string-p)
|
||||||
'(require-snippet-condition . force-in-comment)
|
'(require-snippet-condition . force-in-comment)
|
||||||
t))))
|
t)))))
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
... and for a snippet that you want to expand in comments, specify a
|
... and for a snippet that you want to expand in comments, specify a
|
||||||
|
34
yasnippet.el
34
yasnippet.el
@ -494,18 +494,19 @@ Attention: This hook is not run when exiting nested/stacked snippet expansion!")
|
|||||||
"Hook run just before expanding a snippet.")
|
"Hook run just before expanding a snippet.")
|
||||||
|
|
||||||
(defconst yas-not-string-or-comment-condition
|
(defconst yas-not-string-or-comment-condition
|
||||||
'(if (let ((ppss (syntax-ppss)))
|
(lambda ()
|
||||||
|
(if (let ((ppss (syntax-ppss)))
|
||||||
(or (nth 3 ppss) (nth 4 ppss)))
|
(or (nth 3 ppss) (nth 4 ppss)))
|
||||||
'(require-snippet-condition . force-in-comment)
|
'(require-snippet-condition . force-in-comment)
|
||||||
t)
|
t))
|
||||||
"Disables snippet expansion in strings and comments.
|
"Disables snippet expansion in strings and comments.
|
||||||
To use, set `yas-buffer-local-condition' to this value.")
|
To use, set `yas-buffer-local-condition' to this value.")
|
||||||
|
|
||||||
(defcustom yas-buffer-local-condition t
|
(defcustom yas-buffer-local-condition t
|
||||||
"Snippet expanding condition.
|
"Snippet expanding condition.
|
||||||
|
|
||||||
This variable is a Lisp form which is evaluated every time a
|
This variable is either a Lisp function (called with no arguments)
|
||||||
snippet expansion is attempted:
|
or a Lisp form. It is evaluated every time a snippet expansion is attempted:
|
||||||
|
|
||||||
* If it evaluates to nil, no snippets can be expanded.
|
* If it evaluates to nil, no snippets can be expanded.
|
||||||
|
|
||||||
@ -546,9 +547,10 @@ conditions.
|
|||||||
(add-hook \\='python-mode-hook
|
(add-hook \\='python-mode-hook
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(setq yas-buffer-local-condition
|
(setq yas-buffer-local-condition
|
||||||
\\='(if (python-syntax-comment-or-string-p)
|
(lambda ()
|
||||||
|
(if (python-syntax-comment-or-string-p)
|
||||||
\\='(require-snippet-condition . force-in-comment)
|
\\='(require-snippet-condition . force-in-comment)
|
||||||
t))))"
|
t)))))"
|
||||||
:type
|
:type
|
||||||
`(choice
|
`(choice
|
||||||
(const :tag "Disable snippet expansion inside strings and comments"
|
(const :tag "Disable snippet expansion inside strings and comments"
|
||||||
@ -1329,14 +1331,15 @@ string and TEMPLATE is a `yas--template' structure."
|
|||||||
|
|
||||||
;;; Filtering/condition logic
|
;;; Filtering/condition logic
|
||||||
|
|
||||||
(defun yas--eval-condition (condition)
|
(defun yas--funcall-condition (fun &rest args)
|
||||||
(condition-case err
|
(condition-case err
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(save-restriction
|
(save-restriction
|
||||||
(save-match-data
|
(save-match-data
|
||||||
(eval condition t))))
|
(apply fun args))))
|
||||||
(error (progn
|
(error (progn
|
||||||
(yas--message 1 "Error in condition evaluation: %s" (error-message-string err))
|
(yas--message 1 "Error in condition evaluation: %s"
|
||||||
|
(error-message-string err))
|
||||||
nil))))
|
nil))))
|
||||||
|
|
||||||
|
|
||||||
@ -1361,8 +1364,12 @@ This function implements the rules described in
|
|||||||
conditions to filter out potential expansions."
|
conditions to filter out potential expansions."
|
||||||
(if (eq 'always yas-buffer-local-condition)
|
(if (eq 'always yas-buffer-local-condition)
|
||||||
'always
|
'always
|
||||||
(let ((local-condition (or (and (consp yas-buffer-local-condition)
|
(let ((local-condition
|
||||||
(yas--eval-condition yas-buffer-local-condition))
|
(or (cond
|
||||||
|
((consp yas-buffer-local-condition)
|
||||||
|
(yas--funcall-condition #'eval yas-buffer-local-condition t))
|
||||||
|
((functionp yas-buffer-local-condition)
|
||||||
|
(yas--funcall-condition yas-buffer-local-condition)))
|
||||||
yas-buffer-local-condition)))
|
yas-buffer-local-condition)))
|
||||||
(when local-condition
|
(when local-condition
|
||||||
(if (eq local-condition t)
|
(if (eq local-condition t)
|
||||||
@ -1375,7 +1382,7 @@ conditions to filter out potential expansions."
|
|||||||
(defun yas--template-can-expand-p (condition requirement)
|
(defun yas--template-can-expand-p (condition requirement)
|
||||||
"Evaluate CONDITION and REQUIREMENT and return a boolean."
|
"Evaluate CONDITION and REQUIREMENT and return a boolean."
|
||||||
(let* ((result (or (null condition)
|
(let* ((result (or (null condition)
|
||||||
(yas--eval-condition condition))))
|
(yas--funcall-condition #'eval condition t))))
|
||||||
(cond ((eq requirement t)
|
(cond ((eq requirement t)
|
||||||
result)
|
result)
|
||||||
(t
|
(t
|
||||||
@ -2935,7 +2942,8 @@ DEBUG is for debugging the YASnippet engine itself."
|
|||||||
(if (and condition
|
(if (and condition
|
||||||
original-buffer)
|
original-buffer)
|
||||||
(with-current-buffer original-buffer
|
(with-current-buffer original-buffer
|
||||||
(if (yas--eval-condition condition)
|
(if (yas--funcall-condition
|
||||||
|
#'eval condition t)
|
||||||
"(y)"
|
"(y)"
|
||||||
"(s)"))
|
"(s)"))
|
||||||
"(a)")))
|
"(a)")))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user