【发布时间】:2012-04-18 14:38:07
【问题描述】:
我想在 emacs-lisp 中插入一个特定的 yasn-p 作为函数的一部分。有没有办法做到这一点?
似乎相关的唯一命令是yas/insert-snippet,但它只是打开一个包含所有选项的弹出窗口,并且文档没有说明通过指定 sn-p 名称绕过弹出窗口的任何内容。
【问题讨论】:
我想在 emacs-lisp 中插入一个特定的 yasn-p 作为函数的一部分。有没有办法做到这一点?
似乎相关的唯一命令是yas/insert-snippet,但它只是打开一个包含所有选项的弹出窗口,并且文档没有说明通过指定 sn-p 名称绕过弹出窗口的任何内容。
【问题讨论】:
yas/insert-snippet 确实只是yas/expand-snippet 的一个薄包装,用于交互使用。然而,内部结构……很有趣。从源代码来看,当我想在 elisp-mode 中扩展“defun”sn-p 时,以下内容对我有用:
(yas/expand-snippet
(yas/template-content (cdar (mapcan #'(lambda (table)
(yas/fetch table "defun"))
(yas/get-snippet-tables)))))
【讨论】:
作为 yasn-p 的作者,我认为您宁愿不要依赖 yasn-p 有趣数据结构的内部细节,这些数据结构将来可能会发生变化。我会根据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")
【讨论】:
我刚刚进入 yasn-p,我想在为某些模式打开一个新文件时自动插入我的一个 sn-ps。这导致我来到这里,但我产生了一个稍微不同的解决方案。提供另一种选择:(“new-shell”是我个人 sn-p 的名称,用于提供新的 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")))
IMO,如果 yasn-p 发生巨大变化,我的解决方案不太容易被破坏。
【讨论】: