branch: master commit e60e8b55518ede19b995c5b49ba69873ee4dc1d3 Merge: faed106 d982b70 Author: João Távora <joaotav...@gmail.com> Commit: João Távora <joaotav...@gmail.com>
Merge pull request #503 from npostavs/key-syntaxes More yas-key-syntaxes functions --- yasnippet-tests.el | 20 +++++++++++++++----- yasnippet.el | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/yasnippet-tests.el b/yasnippet-tests.el index f40ed25..4b3a440 100644 --- a/yasnippet-tests.el +++ b/yasnippet-tests.el @@ -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 ")))))))) ;;; Loading diff --git a/yasnippet.el b/yasnippet.el index fc5d36b..ea5fe33 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -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 @@ -2726,6 +2727,33 @@ and `kill-buffer' instead." +;;; 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)) + + + ;;; User convenience functions, for using in snippet definitions (defvar yas-modified-p nil