emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/yasnippet 1d0966ae34: * yasnippet.el (yas-buffer-local-


From: ELPA Syncer
Subject: [elpa] externals/yasnippet 1d0966ae34: * yasnippet.el (yas-buffer-local-condition): Accept functions
Date: Fri, 19 Jan 2024 12:59:09 -0500 (EST)

branch: externals/yasnippet
commit 1d0966ae34f392bcf5d22dc05276d5f9eea88adf
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>

    * 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.
---
 doc/snippet-expansion.org | 11 ++++++-----
 yasnippet.el              | 48 +++++++++++++++++++++++++++--------------------
 2 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/doc/snippet-expansion.org b/doc/snippet-expansion.org
index dca815f8e3..2ff0e45818 100644
--- a/doc/snippet-expansion.org
+++ b/doc/snippet-expansion.org
@@ -210,11 +210,12 @@ inside a comment?  Set 
[[sym:yas-buffer-local-condition][=yas-buffer-local-condi
 
 #+BEGIN_SRC emacs-lisp
   (add-hook 'python-mode-hook
-            (lambda ()
-              (setq yas-buffer-local-condition
-                    '(if (python-syntax-comment-or-string-p)
-                         '(require-snippet-condition . force-in-comment)
-                       t))))
+           (lambda ()
+             (setq yas-buffer-local-condition
+                   (lambda ()
+                     (if (python-syntax-comment-or-string-p)
+                         '(require-snippet-condition . force-in-comment)
+                       t)))))
 #+END_SRC
 
 ... and for a snippet that you want to expand in comments, specify a
diff --git a/yasnippet.el b/yasnippet.el
index 3e42f73c6a..54616d4895 100644
--- a/yasnippet.el
+++ b/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.")
 
 (defconst yas-not-string-or-comment-condition
-  '(if (let ((ppss (syntax-ppss)))
-         (or (nth 3 ppss) (nth 4 ppss)))
-       '(require-snippet-condition . force-in-comment)
-     t)
+  (lambda ()
+    (if (let ((ppss (syntax-ppss)))
+          (or (nth 3 ppss) (nth 4 ppss)))
+        '(require-snippet-condition . force-in-comment)
+      t))
   "Disables snippet expansion in strings and comments.
 To use, set `yas-buffer-local-condition' to this value.")
 
 (defcustom yas-buffer-local-condition t
   "Snippet expanding condition.
 
-This variable is a Lisp form which is evaluated every time a
-snippet expansion is attempted:
+This variable is either a Lisp function (called with no arguments)
+or a Lisp form.  It is evaluated every time a snippet expansion is attempted:
 
     * If it evaluates to nil, no snippets can be expanded.
 
@@ -543,12 +544,13 @@ inside comments, in `python-mode' only, with the 
exception of
 snippets returning the symbol `force-in-comment' in their
 conditions.
 
- (add-hook \\='python-mode-hook
-           (lambda ()
-              (setq yas-buffer-local-condition
-                    \\='(if (python-syntax-comment-or-string-p)
-                         \\='(require-snippet-condition . force-in-comment)
-                       t))))"
+    (add-hook \\='python-mode-hook
+              (lambda ()
+                (setq yas-buffer-local-condition
+                      (lambda ()
+                        (if (python-syntax-comment-or-string-p)
+                            \\='(require-snippet-condition . force-in-comment)
+                          t)))))"
   :type
   `(choice
     (const :tag "Disable snippet expansion inside strings and comments"
@@ -1329,14 +1331,15 @@ string and TEMPLATE is a `yas--template' structure."
 
 ;;; Filtering/condition logic
 
-(defun yas--eval-condition (condition)
+(defun yas--funcall-condition (fun &rest args)
   (condition-case err
       (save-excursion
         (save-restriction
           (save-match-data
-            (eval condition t))))
+            (apply fun args))))
     (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))))
 
 
@@ -1361,9 +1364,13 @@ This function implements the rules described in
 conditions to filter out potential expansions."
   (if (eq 'always yas-buffer-local-condition)
       'always
-    (let ((local-condition (or (and (consp yas-buffer-local-condition)
-                                    (yas--eval-condition 
yas-buffer-local-condition))
-                               yas-buffer-local-condition)))
+    (let ((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)))
       (when local-condition
         (if (eq local-condition t)
             t
@@ -1375,7 +1382,7 @@ conditions to filter out potential expansions."
 (defun yas--template-can-expand-p (condition requirement)
   "Evaluate CONDITION and REQUIREMENT and return a boolean."
   (let* ((result (or (null condition)
-                     (yas--eval-condition condition))))
+                     (yas--funcall-condition #'eval condition t))))
     (cond ((eq requirement t)
            result)
           (t
@@ -2935,7 +2942,8 @@ DEBUG is for debugging the YASnippet engine itself."
                                       (if (and condition
                                                original-buffer)
                                           (with-current-buffer original-buffer
-                                            (if (yas--eval-condition condition)
+                                            (if (yas--funcall-condition
+                                                 #'eval condition t)
                                                 "(y)"
                                               "(s)"))
                                         "(a)")))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]