From 5d10b5dfee3ee3d1c5c199e8c9acfb9c9b26172c Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Sun, 10 Aug 2014 13:07:43 -0400 Subject: [PATCH] Add yas-longest-key-from-whitespace Example yas-key-syntaxes function. * yasnippet.el (yas-longest-key-from-whitespace): new function. * yasnippet-tests.el (complicated-yas-key-syntaxes): test it. --- yasnippet-tests.el | 4 +++- yasnippet.el | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/yasnippet-tests.el b/yasnippet-tests.el index 12ca61528..d090c511d 100644 --- a/yasnippet-tests.el +++ b/yasnippet-tests.el @@ -349,7 +349,9 @@ TODO: correct this bug!" ("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")))))))) + (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK"))) + (setq yas-key-syntaxes '(yas-longest-key-from-whitespace)) + (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK")))))))) ;;; Loading diff --git a/yasnippet.el b/yasnippet.el index 93eceb24a..dcf6bf057 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -2746,6 +2746,19 @@ element of `yas-key-syntaxes'." (when (/= (skip-chars-backward "^[:space:]\n" (1- (point))) 0) 'again)) +(defun yas-longest-key-from-whitespace (start-point) + "Go back to nearest whitespace, then go forward and return `again' until at START-POINT. + +A newline will be considered whitespace even if the mode syntax +marks it as something else (typically comment ender). Use as +element of `yas-key-syntaxes'." + (if (= (point) start-point) + (yas-try-key-from-whitespace start-point) + (forward-char)) + (unless (= original (1+ (point))) + 'again)) + + ;;; User convenience functions, for using in snippet definitions -- 2.39.2