[Top][All Lists]

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

[elpa] master ba73f20 27/51: Bind field skip and clear command condition

From: Noam Postavsky
Subject: [elpa] master ba73f20 27/51: Bind field skip and clear command conditionally
Date: Sun, 13 May 2018 13:11:43 -0400 (EDT)

branch: master
commit ba73f20bc305570391a151fa353188803b01f855
Author: Noam Postavsky <address@hidden>
Commit: Noam Postavsky <address@hidden>

    Bind field skip and clear command conditionally
    Rather than embedding the conditionality inside the function body, and
    hardcoding fallback to `delete-char'.  This allows users who want, for
    example, to use backspace for field skipping to simply do
        (define-key yas-minor-mode-map (kbd "DEL")
    * yasnippet.el (yas-skip-and-clear-field, yas-current-field)
    (yas--maybe-clear-field-filter): New command and function, extracted
    from yas-skip-and-clear-or-delete-char.
    (yas-skip-and-clear-or-delete-char): Mark obsolete.
    (yas-maybe-skip-and-clear-field): New constant, for conditional
    (yas-keymap): Bind it to `C-d' instead of
    * yasnippet-tests.el (delete-numberless-inner-snippet-issue-562): Use
    `yas-skip-and-clear-field' instead of
 yasnippet-tests.el |  2 +-
 yasnippet.el       | 47 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/yasnippet-tests.el b/yasnippet-tests.el
index 5751a06..c82d1b0 100644
--- a/yasnippet-tests.el
+++ b/yasnippet-tests.el
@@ -274,7 +274,7 @@ attention to case differences."
     (ert-simulate-command '(yas-next-field-or-maybe-expand))
     (should (looking-at "testblable"))
     (ert-simulate-command '(yas-next-field-or-maybe-expand))
-    (ert-simulate-command '(yas-skip-and-clear-or-delete-char))
+    (ert-simulate-command '(yas-skip-and-clear-field))
     (should (looking-at "ble"))
     (should (null (yas-active-snippets)))))
diff --git a/yasnippet.el b/yasnippet.el
index 7c8905a..94d8632 100644
--- a/yasnippet.el
+++ b/yasnippet.el
@@ -396,13 +396,21 @@ It must be set to nil before loading yasnippet to take 
 ;;; User-visible variables
+(defconst yas-maybe-skip-and-clear-field
+  '(menu-item "" yas-skip-and-clear-field
+              :filter yas--maybe-clear-field-filter)
+  "A conditional key definition.
+This can be used as a key definition in keymaps to bind a key to
+`yas-skip-and-clear-field' only when at the beginning of an
+unmodified snippey field.")
 (defvar yas-keymap  (let ((map (make-sparse-keymap)))
                       (define-key map [(tab)]       
                       (define-key map (kbd "TAB")   
                       (define-key map [(shift tab)] 'yas-prev-field)
                       (define-key map [backtab]     'yas-prev-field)
                       (define-key map (kbd "C-g")   'yas-abort-snippet)
-                      (define-key map (kbd "C-d")   
+                      (define-key map (kbd "C-d")   
   "The active keymap while a snippet expansion is in progress.")
@@ -3547,22 +3555,37 @@ holds the keymap."
     (overlay-put overlay 'yas--snippet snippet)
+(defun yas-current-field ()
+  "Return the currently active field."
+  (and yas--active-field-overlay
+       (overlay-buffer yas--active-field-overlay)
+       (overlay-get yas--active-field-overlay 'yas--field)))
+(defun yas--maybe-clear-field-filter (cmd)
+  "Return CMD if at start of unmodified snippet field.
+Use as a `:filter' argument for a conditional keybinding."
+  (let ((field (yas-current-field)))
+    (when (and field
+               (not (yas--field-modified-p field))
+               (eq (point) (marker-position (yas--field-start field))))
+      cmd)))
+(defun yas-skip-and-clear-field (&optional field)
+  "Clears unmodified FIELD if at field start, skips to next tab."
+  (interactive)
+  (yas--skip-and-clear (or field (yas-current-field)))
+  (yas-next-field 1))
 (defun yas-skip-and-clear-or-delete-char (&optional field)
   "Clears unmodified field if at field start, skips to next tab.
 Otherwise deletes a character normally by calling `delete-char'."
-  (let ((field (or field
-                   (and yas--active-field-overlay
-                        (overlay-buffer yas--active-field-overlay)
-                        (overlay-get yas--active-field-overlay 'yas--field)))))
-    (cond ((and field
-                (not (yas--field-modified-p field))
-                (eq (point) (marker-position (yas--field-start field))))
-           (yas--skip-and-clear field)
-           (yas-next-field 1))
-          (t
-           (call-interactively 'delete-char)))))
+  (declare (obsolete "Bind to `yas-maybe-skip-and-clear-field' instead." 
+  (cond ((yas--maybe-clear-field-filter t)
+         (yas--skip-and-clear (or field (yas-current-field)))
+         (yas-next-field 1))
+        (t (call-interactively 'delete-char))))
 (defun yas--skip-and-clear (field &optional from)
   "Deletes the region of FIELD and sets it's modified state to t.

reply via email to

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