Merge pull request #503 from npostavs/key-syntaxes

More yas-key-syntaxes functions
This commit is contained in:
João Távora 2014-08-11 00:26:29 +01:00
commit e60e8b5551
2 changed files with 51 additions and 13 deletions

View File

@ -322,12 +322,13 @@ TODO: correct this bug!"
(yas-saving-variables (yas-saving-variables
(yas-with-snippet-dirs (yas-with-snippet-dirs
'((".emacs.d/snippets" '((".emacs.d/snippets"
("text-mode" ("emacs-lisp-mode"
("foo-barbaz" . "# condition: yas--foobarbaz\n# --\nOKfoo-barbazOK") ("foo-barbaz" . "# condition: yas--foobarbaz\n# --\nOKfoo-barbazOK")
("barbaz" . "# condition: yas--barbaz\n# --\nOKbarbazOK") ("barbaz" . "# condition: yas--barbaz\n# --\nOKbarbazOK")
("baz" . "OKbazOK")))) ("baz" . "OKbazOK")
("'quote" . "OKquoteOK"))))
(yas-reload-all) (yas-reload-all)
(text-mode) (emacs-lisp-mode)
(yas-minor-mode-on) (yas-minor-mode-on)
(let ((yas-key-syntaxes '("w" "w_"))) (let ((yas-key-syntaxes '("w" "w_")))
(let ((yas--barbaz t)) (let ((yas--barbaz t))
@ -336,13 +337,22 @@ TODO: correct this bug!"
(let ((yas--foobarbaz t)) (let ((yas--foobarbaz t))
(yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK")))) (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK"))))
(let ((yas-key-syntaxes (let ((yas-key-syntaxes
(cons #'(lambda () (cons #'(lambda (_start-point)
(unless (looking-back "-") (unless (looking-back "-")
(backward-char) (backward-char)
'again)) 'again))
yas-key-syntaxes)) yas-key-syntaxes))
(yas--foobarbaz t)) (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 ;;; Loading

View File

@ -388,21 +388,22 @@ the trigger key itself."
map) map)
"The active keymap while a snippet expansion is in progress.") "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. "Syntaxes and functions to help look for trigger keys before point.
Each element in this list specifies how to skip buffer positions Each element in this list specifies how to skip buffer positions
backwards and look for the start of a trigger key. backwards and look for the start of a trigger key.
Each element can be either a string or a functino of no Each element can be either a string or a function receiving the
arguments. A string element is simply passed to original point as an argument. A string element is simply passed
`skip-syntax-backward' whereas a function element is called with to `skip-syntax-backward' whereas a function element is called
no arguments and should also place point before the original with no arguments and should also place point before the original
position. position.
The string between the resulting buffer position and the original The string between the resulting buffer position and the original
point.in the is matched against the trigger keys in the active point is matched against the trigger keys in the active snippet
snippet tables. tables.
If no expandable snippets are found, the next element is the list If no expandable snippets are found, the next element is the list
is tried, unless a function element returned the symbol `again', 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) (skip-syntax-backward method)
(setq methods (cdr methods))) (setq methods (cdr methods)))
((functionp method) ((functionp method)
(unless (eq (funcall method) (unless (eq (funcall method original)
'again) 'again)
(setq methods (cdr methods)))) (setq methods (cdr methods))))
(t (t
@ -2725,6 +2726,33 @@ and `kill-buffer' instead."
groups-hash))) groups-hash)))
;;; 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 ;;; User convenience functions, for using in snippet definitions