Emacs 按名称插入yasnippet
我想在emacs lisp中插入一个特定的yasnippet作为函数的一部分。有办法吗Emacs 按名称插入yasnippet,emacs,yasnippet,Emacs,Yasnippet,我想在emacs lisp中插入一个特定的yasnippet作为函数的一部分。有办法吗 看起来唯一相关的命令是yas/insert snippet,但它只是打开一个包含所有选项的弹出窗口,文档中没有说明通过指定代码段名称绕过弹出窗口的任何内容 yas/insert snippet实际上只是交互式使用的yas/expand snippet的薄薄包装。然而,内部结构是。。。有趣。从源代码来看,当我想在elisp模式下展开“defun”代码段时,以下代码确实适用于我: (yas/expand-snip
看起来唯一相关的命令是
yas/insert snippet
,但它只是打开一个包含所有选项的弹出窗口,文档中没有说明通过指定代码段名称绕过弹出窗口的任何内容 yas/insert snippet
实际上只是交互式使用的yas/expand snippet
的薄薄包装。然而,内部结构是。。。有趣。从源代码来看,当我想在elisp模式下展开“defun”代码段时,以下代码确实适用于我:
(yas/expand-snippet
(yas/template-content (cdar (mapcan #'(lambda (table)
(yas/fetch table "defun"))
(yas/get-snippet-tables)))))
作为yasnippet的作者,我认为您最好不要依赖yasnippet有趣的数据结构的内部细节,这些数据结构将来可能会发生变化。我将根据
yas/insert snippet
和yas/prompt函数的文档进行此操作:
(defun yas/insert-by-name (name)
(flet ((dummy-prompt
(prompt choices &optional display-fn)
(declare (ignore prompt))
(or (find name choices :key display-fn :test #'string=)
(throw 'notfound nil))))
(let ((yas/prompt-functions '(dummy-prompt)))
(catch 'notfound
(yas/insert-snippet t)))))
(yas/insert-by-name "defun")
我刚刚进入yasnippet,我想在打开一个新文件时自动插入我的一个代码片段,用于某些模式。这让我想到了这里,但我产生了一个稍微不同的解决方案。提供另一种选择:(“new shell”是我提供新shell脚本模板的个人代码片段的名称)
在我看来,如果yasnippet发生巨大变化,我的解决方案就不太容易被破坏。也许这是一个值得添加到包中的东西?事实上,我很想了解数据结构,尽管我认为这个答案/评论不是合适的地方。
(defun jsm/new-file-snippet (key)
"Call particular yasnippet template for newly created
files. Use by adding a lambda function to the particular mode
hook passing the correct yasnippet key"
(interactive)
(if (= (buffer-size) 0)
(progn
(insert key)
(call-interactively 'yas-expand))))
(add-hook 'sh-mode-hook '(lambda () (jsm/new-file-snippet "new-shell")))