我正在尝试在所有打开的缓冲区和 yasnippet 中完成制表符以使用制表键。目前我可以选择其中之一。以下代码是我处理 yasnippet 扩展的方式,但由于我不是 lisp 程序员,所以我看不到这里的错误。
如果它无法扩展代码段,我希望它尝试从缓冲区扩展。
;; Auto complete settings / tab settings
;; http://emacsblog.org/2007/03/12/tab-completion-everywhere/ <-- in the comments
(global-set-key [(tab)] 'smart-tab)
(defun smart-tab ()
"This smart tab is minibuffer compliant: it acts as usual in
the minibuffer. Else, if mark is active, indents region. Else if
point is at the end of a symbol, expands it. Else indents the
current line."
(interactive)
(if (minibufferp)
(unless (minibuffer-complete)
(dabbrev-expand nil))
(if mark-active
(indent-region (region-beginning)
(region-end))
(if (looking-at "\\_>")
(unless (yas/expand)
(dabbrev-expand nil))
(indent-for-tab-command)))))
最佳答案
首先,我尝试了解代码的作用,以及您希望它做什么。
你的代码
首先检查
点是否在minibuffer
中。如果是,则尝试完成迷你缓冲区
- 如果(在迷你缓冲区中)无法完成它,它会调用 dabbrev-expand
否则如果点
不在迷你缓冲区中
如果标记了某个区域,则缩进该区域。
如果
没有激活的标记
,它会检查该点是否在某个单词的结尾
如果是这样,它会检查 yasnippet 是否可以扩展。
- 如果 yas 不能扩展,它调用 dabbrev-expand
如果没有,它会尝试缩进当前行
这就是您的代码所做的。
您的代码由于 yas/expand 而失败。如果扩展失败,此命令不会返回。
如果此命令失败,它会检查变量 yas/fallback-behavior
的状态。如果此变量的值为 call-other-command
,如您的情况,失败的 yas 扩展将调用绑定(bind)到变量 yas/trigger-key
中保存的键的命令>.
在您的例子中,这个变量是 TAB
。
所以:你在单词的末尾,你按 TAB 来完成它,这会触发交互式 smart-tab
,它调用 yas/expand ,如果扩展失败,它会调用TAB
的绑定(bind)函数,这里是死循环。
您的问题的解决方案是在此 smart-tab
函数中将 nil 临时绑定(bind)到 yas/fallback-behavior
。
修复方法如下:
(if (looking-at "\\_>")
(let ((yas/fallback-behavior nil))
(unless (yas/expand)
(dabbrev-expand nil)))
(indent-for-tab-command))
关于ruby-on-rails - 带有 yasnippets 的 emacs 智能选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13576156/