ruby-on-rails - 带有 yasnippets 的 emacs 智能选项卡

标签 ruby-on-rails emacs lisp yasnippet

我正在尝试在所有打开的缓冲区和 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/

相关文章:

emacs - 如何提交软件包的更新?

lisp - 为什么我收到 "atom?"的未绑定(bind)错误

lisp - 测试一个类是否是普通 lisp 中另一个类的子类

ruby-on-rails - 剥离 rails 仅供 api 使用

ruby-on-rails - 使用带有 RSpec 的设计测试 View

ruby-on-rails - 为什么某些 MP4 文件的 MIME 类型是 application/octet-stream 而不是 video/mp4?

emacs - 在 Windows 7 上的 emacs 中抑制左侧连续箭头

ruby-on-rails - 为什么我的 RMagick 渲染的 PDF 看起来这么糟糕?

emacs - 如何在 Emacs 中以文档 View 模式向下滚动页面?

arrays - 如何创建特定类型的通用 lisp (SBCL) 向量以在 usocket 中使用?