【问题标题】:Insert yasnippet by name按名称插入 yasnippet
【发布时间】:2012-04-18 14:38:07
【问题描述】:

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

似乎相关的唯一命令是yas/insert-snippet,但它只是打开一个包含所有选项的弹出窗口,并且文档没有说明通过指定 sn-p 名称绕过弹出窗口的任何内容。

【问题讨论】:

    标签: emacs yasnippet


    【解决方案1】:

    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)))))
    

    【讨论】:

      【解决方案2】:

      作为 yasn-p 的作者,我认为您宁愿不要依赖 yasn-p 有趣数据结构的内部细节,这些数据结构将来可能会发生变化。我会根据yas/insert-snippetyas/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")
      

      【讨论】:

      • 也许这是一个值得添加到包中的东西?
      • 实际上,我很想听听有关数据结构的信息,尽管我认为这个答案/cmets 不是合适的地方。
      【解决方案3】:

      我刚刚进入 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 发生巨大变化,我的解决方案不太容易被破坏。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-26
        • 2015-02-11
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多