]> code.delx.au - gnu-emacs-elpa/commitdiff
Merge pull request #503 from npostavs/key-syntaxes
authorJoão Távora <joaotavora@gmail.com>
Sun, 10 Aug 2014 23:26:29 +0000 (00:26 +0100)
committerJoão Távora <joaotavora@gmail.com>
Sun, 10 Aug 2014 23:26:29 +0000 (00:26 +0100)
More yas-key-syntaxes functions

yasnippet-tests.el
yasnippet.el

index f40ed25b64644955d6967a49b97192fa56fc3724..4b3a4400bce7dbe9fd5b113688c266552c2b827d 100644 (file)
@@ -322,12 +322,13 @@ TODO: correct this bug!"
     (yas-saving-variables
      (yas-with-snippet-dirs
        '((".emacs.d/snippets"
-          ("text-mode"
+          ("emacs-lisp-mode"
            ("foo-barbaz" . "# condition: yas--foobarbaz\n# --\nOKfoo-barbazOK")
            ("barbaz" . "# condition: yas--barbaz\n# --\nOKbarbazOK")
-           ("baz" . "OKbazOK"))))
+           ("baz" . "OKbazOK")
+           ("'quote" . "OKquoteOK"))))
        (yas-reload-all)
-       (text-mode)
+       (emacs-lisp-mode)
        (yas-minor-mode-on)
        (let ((yas-key-syntaxes '("w" "w_")))
          (let ((yas--barbaz t))
@@ -336,13 +337,22 @@ TODO: correct this bug!"
          (let ((yas--foobarbaz t))
            (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK"))))
          (let ((yas-key-syntaxes
-                (cons #'(lambda ()
+                (cons #'(lambda (_start-point)
                           (unless (looking-back "-")
                             (backward-char)
                             'again))
                       yas-key-syntaxes))
                (yas--foobarbaz t))
-           (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK")))))))))
+           (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK")))))
+       (let ((yas-key-syntaxes '(yas-try-key-from-whitespace)))
+         (yas-should-expand '(("xxx\n'quote" . "xxx\nOKquoteOK")
+                              ("xxx 'quote" . "xxx OKquoteOK"))))
+       (let ((yas-key-syntaxes '(yas-shortest-key-until-whitespace))
+             (yas--foobarbaz t) (yas--barbaz t))
+         (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK")))
+         (setq yas-key-syntaxes '(yas-longest-key-from-whitespace))
+         (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK")
+                              ("foo " . "foo "))))))))
 
 \f
 ;;; Loading
index fc5d36bb153e690904252a86de44cc5f92448f54..ea5fe33c6e888b017f5fc95f4e7552bd663b8053 100644 (file)
@@ -388,21 +388,22 @@ the trigger key itself."
                       map)
   "The active keymap while a snippet expansion is in progress.")
 
-(defvar yas-key-syntaxes (list "w" "w_" "w_." "w_.()" "^ ")
+(defvar yas-key-syntaxes (list "w" "w_" "w_." "w_.()"
+                               #'yas-try-key-from-whitespace)
   "Syntaxes and functions to help look for trigger keys before point.
 
 Each element in this list specifies how to skip buffer positions
 backwards and look for the start of a trigger key.
 
-Each element can be either a string or a functino of no
-arguments. A string element is simply passed to
-`skip-syntax-backward' whereas a function element is called with
-no arguments and should also place point before the original
+Each element can be either a string or a function receiving the
+original point as an argument. A string element is simply passed
+to `skip-syntax-backward' whereas a function element is called
+with no arguments and should also place point before the original
 position.
 
 The string between the resulting buffer position and the original
-point.in the is matched against the trigger keys in the active
-snippet tables.
+point is matched against the trigger keys in the active snippet
+tables.
 
 If no expandable snippets are found, the next element is the list
 is tried, unless a function element returned the symbol `again',
@@ -1238,7 +1239,7 @@ Returns (TEMPLATES START END). This function respects
                (skip-syntax-backward method)
                (setq methods (cdr methods)))
               ((functionp method)
-               (unless (eq (funcall method)
+               (unless (eq (funcall method original)
                            'again)
                  (setq methods (cdr methods))))
               (t
@@ -2725,6 +2726,33 @@ and `kill-buffer' instead."
      groups-hash)))
 
 
+\f
+;;; User convenience functions, for using in `yas-key-syntaxes'
+
+(defun yas-try-key-from-whitespace (_start-point)
+  "As `yas-key-syntaxes' element, look for whitespace delimited key.
+
+A newline will be considered whitespace even if the mode syntax
+marks it as something else (typically comment ender)."
+  (skip-chars-backward "^[:space:]\n"))
+
+(defun yas-shortest-key-until-whitespace (_start-point)
+  "Like `yas-longest-key-from-whitespace' but take the shortest key."
+  (when (/= (skip-chars-backward "^[:space:]\n" (1- (point))) 0)
+    'again))
+
+(defun yas-longest-key-from-whitespace (start-point)
+  "As `yas-key-syntaxes' element, look for longest key between point and whitespace.
+
+A newline will be considered whitespace even if the mode syntax
+marks it as something else (typically comment ender)."
+  (if (= (point) start-point)
+      (yas-try-key-from-whitespace start-point)
+    (forward-char))
+  (unless (<= start-point (1+ (point)))
+    'again))
+
+
 \f
 ;;; User convenience functions, for using in snippet definitions