我正在尝试在所有打开的缓冲区内完成tab操作,yasnippet也使用tab键目前我可以有一个或另一个下面的代码是我如何处理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)))))

最佳答案

首先,我试图理解代码的作用,以及您希望它的作用。
你的代码
first checks如果点在the minibuffer中。
如果是,那么它会尝试完成minibuffer
如果(在minibuffer中)无法完成它,它将调用dabbrev expand
否则,如果点not in minibuffer
如果某个区域被标记,它将缩进该区域。
如果no mark is active,则检查该点是否位于end of some word
如果是这样,它检查的是yasnippet可以扩展。
如果yas不能扩展,它称之为dabbrev expand
如果不是,则尝试缩进当前行
这就是你的代码所做的。
您的代码由于yas/expand而失败。如果扩展失败,则不返回此命令。
如果此命令失败,它将检查变量yas/fallback-behavior的状态如果该变量的值与您的情况相同,则失败的yas扩展将调用绑定到变量call-other-command中保留的键的命令。
在您的例子中,这个变量是yas/trigger-key
所以:你在单词的末尾,按TAB键完成它,这会触发交互式的TAB,它调用yas/expand,如果扩展失败,则调用smart-tab的绑定函数,这里是无限循环。
您的问题的解决方案是在这个TAB函数中临时绑定nil to yas/fallback-behavior
以下是修复方法:

(if (looking-at "\\_>")
      (let ((yas/fallback-behavior nil))
        (unless (yas/expand)
          (dabbrev-expand nil)))
    (indent-for-tab-command))

10-05 18:08