Merge pull request #596 from npostavs/lookup-snippet

Add yas-lookup-snippet
This commit is contained in:
João Távora 2015-08-03 19:24:27 +01:00
commit e8934ce816
4 changed files with 48 additions and 17 deletions

View File

@ -102,21 +102,23 @@ prefer.
** Expanding from emacs-lisp code ** Expanding from emacs-lisp code
Sometimes you might want to expand a snippet directly from you own Sometimes you might want to expand a snippet directly from your own
elisp code. You should call elisp code. You should call [[sym:yas-expand-snippet][=yas-expand-snippet=]] instead of
[[sym:yas-expand-snippet][=yas-expand-snippet=]] instead of [[sym:yas-expand][=yas-expand=]] in this case. [[sym:yas-expand-snippet][=yas-expand-snippet=]] takes a string in
[[sym:yas-expand][=yas-expand=]] in this case. snippet template syntax, if you want to expand an existing snippet you
can use [[sym:yas-lookup-snippet][=yas-lookup-snippet=]] to find its contents by name.
As with expanding from the menubar, the condition system and multiple As with expanding from the menubar, the condition system and multiple
candidates doesn't affect expansion. In fact, expanding from the candidates doesn't affect expansion (the condition system does affect
YASnippet menu has the same effect of evaluating the follow code: [[sym:yas-lookup-snippet][=yas-lookup-snippet=]] though). In fact, expanding from the YASnippet
menu has the same effect of evaluating the follow code:
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(yas-expand-snippet template) (yas-expand-snippet template)
#+END_SRC #+END_SRC
See the internal documentation on [[sym:yas-expand-snippet][=yas-expand-snippet=]] for more See the internal documentation on [[sym:yas-expand-snippet][=yas-expand-snippet=]] and
information. [[sym:yas-lookup-snippet][=yas-lookup-snippet=]] for more information.
* Controlling expansion * Controlling expansion

View File

@ -55,13 +55,12 @@ These customizations can also be found in the menu itself, under the
The "Indenting" submenu contains options to control the values of The "Indenting" submenu contains options to control the values of
[[sym:yas-indent-line][=yas-indent-line=]] and [[sym:yas-also-auto-indent-first-line][=yas-also-auto-indent-first-line=]]. See [[sym:yas-indent-line][=yas-indent-line=]] and [[sym:yas-also-auto-indent-first-line][=yas-also-auto-indent-first-line=]]. See
[[./snippet-development.org][Writing snippets]] . [[./snippet-development.org][Writing snippets]].
* Prompting method * Prompting method
The "Prompting method" submenu contains options to control the value of The "Prompting method" submenu contains options to control the value of
[[sym:yas-prompt-functions][=yas-prompt-functions=]]. See [[./snippet-expansion.org][Expanding [[sym:yas-prompt-functions][=yas-prompt-functions=]]. See [[./snippet-expansion.org][Expanding snippets]].
snippets]] .
* Misc * Misc

View File

@ -433,6 +433,14 @@ TODO: correct this bug!"
("lisp-interaction-mode" ("sc" . "brother from another mother")))) ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
,@body)))) ,@body))))
(ert-deftest snippet-lookup ()
"Test `yas-lookup-snippet'."
(yas-with-some-interesting-snippet-dirs
(yas-reload-all 'no-jit)
(should (equal (yas-lookup-snippet "printf" 'c-mode) "printf($1);"))
(should (equal (yas-lookup-snippet "def" 'c-mode) "# define"))
(should-not (yas-lookup-snippet "no such snippet" nil 'noerror))
(should-not (yas-lookup-snippet "printf" 'emacs-lisp-mode 'noerror))))
(ert-deftest basic-jit-loading () (ert-deftest basic-jit-loading ()
"Test basic loading and expansion of snippets" "Test basic loading and expansion of snippets"

View File

@ -725,7 +725,7 @@ defined direct keybindings to the command
yas--direct-keymaps)) yas--direct-keymaps))
yas--tables)) yas--tables))
(defun yas--modes-to-activate () (defun yas--modes-to-activate (&optional mode)
"Compute list of mode symbols that are active for `yas-expand' "Compute list of mode symbols that are active for `yas-expand'
and friends." and friends."
(let (dfs) (let (dfs)
@ -740,8 +740,10 @@ and friends."
(not (memq neighbour explored)) (not (memq neighbour explored))
(symbolp neighbour)) (symbolp neighbour))
append (funcall dfs neighbour explored))))) append (funcall dfs neighbour explored)))))
(remove-duplicates (append yas--extra-modes (remove-duplicates (if mode
(funcall dfs major-mode))))) (funcall dfs mode)
(append yas--extra-modes
(funcall dfs major-mode))))))
(defvar yas-minor-mode-hook nil (defvar yas-minor-mode-hook nil
"Hook run when `yas-minor-mode' is turned on.") "Hook run when `yas-minor-mode' is turned on.")
@ -1333,15 +1335,17 @@ return an expression that when evaluated will issue an error."
yas--direct-keymaps)) yas--direct-keymaps))
table)) table))
(defun yas--get-snippet-tables () (defun yas--get-snippet-tables (&optional mode)
"Get snippet tables for current buffer. "Get snippet tables for MODE.
MODE defaults to the current buffer's `major-mode'.
Return a list of `yas--table' objects. The list of modes to Return a list of `yas--table' objects. The list of modes to
consider is returned by `yas--modes-to-activate'" consider is returned by `yas--modes-to-activate'"
(remove nil (remove nil
(mapcar #'(lambda (name) (mapcar #'(lambda (name)
(gethash name yas--tables)) (gethash name yas--tables))
(yas--modes-to-activate)))) (yas--modes-to-activate mode))))
(defun yas--menu-keymap-get-create (mode &optional parents) (defun yas--menu-keymap-get-create (mode &optional parents)
"Get or create the menu keymap for MODE and its PARENTS. "Get or create the menu keymap for MODE and its PARENTS.
@ -2305,6 +2309,24 @@ Honours `yas-choose-tables-first', `yas-choose-keys-first' and
(remove-duplicates (mapcan #'yas--table-templates tables) (remove-duplicates (mapcan #'yas--table-templates tables)
:test #'equal)))) :test #'equal))))
(defun yas-lookup-snippet (name &optional mode noerror)
"Get the snippet content for the snippet NAME in MODE's tables.
MODE defaults to the current buffer's `major-mode'. If NOERROR
is non-nil, then don't signal an error if there isn't any snippet
called NAME.
Honours `yas-buffer-local-condition'."
(let* ((yas-choose-tables-first nil) ; avoid prompts
(yas-choose-keys-first nil)
(snippet (cl-find name (yas--all-templates
(yas--get-snippet-tables mode))
:key #'yas--template-name :test #'string=)))
(cond
(snippet (yas--template-content snippet))
(noerror nil)
(t (error "No snippet named: %s" name)))))
(defun yas-insert-snippet (&optional no-condition) (defun yas-insert-snippet (&optional no-condition)
"Choose a snippet to expand, pop-up a list of choices according "Choose a snippet to expand, pop-up a list of choices according
to `yas-prompt-functions'. to `yas-prompt-functions'.