我想在emacs-lisp中插入一个特定的yasnippet作为函数的一部分。有没有办法做到这一点?
唯一看起来相关的命令是yas/insert-snippet,但它只是打开一个包含所有选项的弹出窗口,并且文档中没有提到通过指定代码片段名称来绕过弹出窗口的任何内容。
发布于 2012-04-18 23:58:07
对于交互式使用,yas/insert-snippet确实只是yas/expand-snippet的一个薄薄的包装器。然而,内部结构是...有意思的。从源代码来看,当我想在elisp-mode中展开"defun“代码段时,下面的代码对我是有效的:
(yas/expand-snippet
(yas/template-content (cdar (mapcan #'(lambda (table)
(yas/fetch table "defun"))
(yas/get-snippet-tables)))))发布于 2012-04-29 03:14:10
作为yasnippet的作者,我认为您最好不要依赖yasnippet有趣的数据结构的内部细节,这些细节在未来可能会发生变化。我会基于yas/insert-snippet和yas/prompt-functions的文档来做这件事:
(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")发布于 2014-04-26 11:48:34
我刚开始使用yasnippet,我想在某些模式下打开一个新文件时自动插入我的一个代码片段。这将我带到了这里,但我生成了一个略有不同的解决方案。提供另一种选择:(" new - shell“是我个人代码片段的名称,用于提供新的shell脚本模板)
(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")))我的解决方案是,如果yasnippet发生巨大变化,我的解决方案不太容易被破坏。
https://stackoverflow.com/questions/10211730
复制相似问题