[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 7bce1a6 12/60: Add new function yas-lookup-snippet
From: |
João Távora |
Subject: |
[elpa] master 7bce1a6 12/60: Add new function yas-lookup-snippet |
Date: |
Thu, 21 Jan 2016 22:35:52 +0000 |
branch: master
commit 7bce1a647380cb5152fc17a21948ca94f1789fd6
Author: Noam Postavsky <address@hidden>
Commit: Noam Postavsky <address@hidden>
Add new function yas-lookup-snippet
* yasnippet.el (yas-lookup-snippet): New function.
(yas--get-snippet-tables, yas--modes-to-activate): Add optional mode
parameter.
* yasnippet-tests.el (snippet-lookup): New test for yas-lookup-snippet.
* doc/snippet-expansion.org (Expanding from emacs-lisp code): Mention
yas-lookup-snippet.
---
doc/snippet-expansion.org | 13 ++++++++-----
yasnippet-tests.el | 8 ++++++++
yasnippet.el | 34 ++++++++++++++++++++++++++++------
3 files changed, 44 insertions(+), 11 deletions(-)
diff --git a/doc/snippet-expansion.org b/doc/snippet-expansion.org
index a0fbe09..fdc02cc 100644
--- a/doc/snippet-expansion.org
+++ b/doc/snippet-expansion.org
@@ -104,18 +104,21 @@ prefer.
Sometimes you might want to expand a snippet directly from your own
elisp code. You should call [[sym:yas-expand-snippet][=yas-expand-snippet=]]
instead of
-[[sym:yas-expand][=yas-expand=]] in this case.
+[[sym:yas-expand][=yas-expand=]] in this case.
[[sym:yas-expand-snippet][=yas-expand-snippet=]] takes a string in
+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
-candidates doesn't affect expansion. In fact, expanding from the
-YASnippet menu has the same effect of evaluating the follow code:
+candidates doesn't affect expansion (the condition system does affect
+[[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
(yas-expand-snippet template)
#+END_SRC
-See the internal documentation on
[[sym:yas-expand-snippet][=yas-expand-snippet=]] for more
-information.
+See the internal documentation on
[[sym:yas-expand-snippet][=yas-expand-snippet=]] and
+[[sym:yas-lookup-snippet][=yas-lookup-snippet=]] for more information.
* Controlling expansion
diff --git a/yasnippet-tests.el b/yasnippet-tests.el
index 0f11352..0b14a56 100644
--- a/yasnippet-tests.el
+++ b/yasnippet-tests.el
@@ -433,6 +433,14 @@ TODO: correct this bug!"
("lisp-interaction-mode" ("sc" . "brother from another mother"))))
,@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 ()
"Test basic loading and expansion of snippets"
diff --git a/yasnippet.el b/yasnippet.el
index 305bb95..f1bfe65 100644
--- a/yasnippet.el
+++ b/yasnippet.el
@@ -725,7 +725,7 @@ defined direct keybindings to the command
yas--direct-keymaps))
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'
and friends."
(let (dfs)
@@ -740,8 +740,10 @@ and friends."
(not (memq neighbour explored))
(symbolp neighbour))
append (funcall dfs neighbour explored)))))
- (remove-duplicates (append yas--extra-modes
- (funcall dfs major-mode)))))
+ (remove-duplicates (if mode
+ (funcall dfs mode)
+ (append yas--extra-modes
+ (funcall dfs major-mode))))))
(defvar yas-minor-mode-hook nil
"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))
table))
-(defun yas--get-snippet-tables ()
- "Get snippet tables for current buffer.
+(defun yas--get-snippet-tables (&optional mode)
+ "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
consider is returned by `yas--modes-to-activate'"
(remove nil
(mapcar #'(lambda (name)
(gethash name yas--tables))
- (yas--modes-to-activate))))
+ (yas--modes-to-activate mode))))
(defun yas--menu-keymap-get-create (mode &optional 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)
: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)
"Choose a snippet to expand, pop-up a list of choices according
to `yas-prompt-functions'.
- [elpa] master 0e7ea94 06/60: Replaced 'shadow' with 'override' throughout documentation, (continued)
- [elpa] master 0e7ea94 06/60: Replaced 'shadow' with 'override' throughout documentation, João Távora, 2016/01/21
- [elpa] master 4fd8ab1 02/60: README.mdown (per-buffer): Remove needless lambda, João Távora, 2016/01/21
- [elpa] master d619107 03/60: yasnippet.el (yas-initialize): Mark obsolete, João Távora, 2016/01/21
- [elpa] master 890bd7e 04/60: Merge pull request #569 from npostavs/doc-obsolete, João Távora, 2016/01/21
- [elpa] master 5aebe46 08/60: Closes #549: Adjust `load' verbosity to `yas-verbosity', João Távora, 2016/01/21
- [elpa] master 4692c81 09/60: Fix "invalid function: quote", João Távora, 2016/01/21
- [elpa] master 5ebf347 11/60: Minor doc fixes, João Távora, 2016/01/21
- [elpa] master 197db9f 10/60: Merge pull request #578 from aluaces/patch-1, João Távora, 2016/01/21
- [elpa] master 7f4f6be 07/60: Merge pull request #571 from PhilHudson/master, João Távora, 2016/01/21
- [elpa] master c91a588 14/60: Simplify some code, João Távora, 2016/01/21
- [elpa] master 7bce1a6 12/60: Add new function yas-lookup-snippet,
João Távora <=
- [elpa] master e8934ce 13/60: Merge pull request #596 from npostavs/lookup-snippet, João Távora, 2016/01/21
- [elpa] master 0ada0fc 15/60: Add documentation for snippet compilation, João Távora, 2016/01/21
- [elpa] master 4bd3d98 16/60: Merge pull request #599 from zmwangx/snippet-compilation-doc, João Távora, 2016/01/21
- [elpa] master 2f6cfef 17/60: Closes #601: updated snippets submodule, João Távora, 2016/01/21
- [elpa] master 68b0ab6 20/60: Accept documented snippet list formats, João Távora, 2016/01/21
- [elpa] master cc1c758 18/60: Reorganize snippet construction from list code, João Távora, 2016/01/21
- [elpa] master 01139a2 23/60: * yasnippet.el (yas--define-snippets-2): Use file-name-NONdirectory., João Távora, 2016/01/21
- [elpa] master 8df6a6e 25/60: Fix invalid yas-key-syntaxes element warning, João Távora, 2016/01/21
- [elpa] master 7703a55 24/60: * doc/snippet-organization.org (.yas-skip): Add info., João Távora, 2016/01/21
- [elpa] master 041821a 22/60: * yasnippet-tests.el (string-suffix-p): Define for older Emacsen., João Távora, 2016/01/21