首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按名称插入yasnippet

按名称插入yasnippet
EN

Stack Overflow用户
提问于 2012-04-18 22:38:07
回答 3查看 912关注 0票数 6

我想在emacs-lisp中插入一个特定的yasnippet作为函数的一部分。有没有办法做到这一点?

唯一看起来相关的命令是yas/insert-snippet,但它只是打开一个包含所有选项的弹出窗口,并且文档中没有提到通过指定代码片段名称来绕过弹出窗口的任何内容。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-18 23:58:07

对于交互式使用,yas/insert-snippet确实只是yas/expand-snippet的一个薄薄的包装器。然而,内部结构是...有意思的。从源代码来看,当我想在elisp-mode中展开"defun“代码段时,下面的代码对我是有效的:

代码语言:javascript
复制
(yas/expand-snippet
  (yas/template-content (cdar (mapcan #'(lambda (table)
                                          (yas/fetch table "defun"))
                                      (yas/get-snippet-tables)))))
票数 5
EN

Stack Overflow用户

发布于 2012-04-29 03:14:10

作为yasnippet的作者,我认为您最好不要依赖yasnippet有趣的数据结构的内部细节,这些细节在未来可能会发生变化。我会基于yas/insert-snippetyas/prompt-functions的文档来做这件事:

代码语言:javascript
复制
(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")
票数 4
EN

Stack Overflow用户

发布于 2014-04-26 11:48:34

我刚开始使用yasnippet,我想在某些模式下打开一个新文件时自动插入我的一个代码片段。这将我带到了这里,但我生成了一个略有不同的解决方案。提供另一种选择:(" new - shell“是我个人代码片段的名称,用于提供新的shell脚本模板)

代码语言:javascript
复制
(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发生巨大变化,我的解决方案不太容易被破坏。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10211730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档