为Clojure模式添加新功能到TAB

时间:2013-01-31 18:46:50

标签: emacs clojure tabs

我知道TAB希望在Emacs中变得聪明。然而,根据我的口味,它并不够智能。而且因为它“聪明”,所以扩展似乎很复杂。

我希望TAB在Clojure模式下完全按照行为行事, excepts 当我在defn, defmacro的第一个括号时,它位于第0列

当它在那里时,我希望它从 hs-minor-mode 中调用 hs-toggle-hiding

然而,我正试图让它发挥作用。

我首先尝试修改分配给TAB的快捷方式,以便在仅在Clojure模式下,它首先调用我的函数,然后调用 indent-for-tab-command 但更改TAB捷径似乎非常复杂。而且由于Emacs已经计划了模式可以在lisp-indent-function中注册其TAB函数的情况,我希望修改 clojure-indent-function ,其中说:

(defun clojure-indent-function (indent-point state)
  "This function is the normal value of the variable `lisp-indent-function'.

但是当光标位于函数中时,此函数显然只被称为。当光标位于第一个'(',例如,'(defn ...“)。

如何在Clojure模式下和指向第0列时在括号中让TAB调用hs-toggle-hiding

我不希望这会影响组织模式或任何其他模式。只是Clojure模式。

2 个答案:

答案 0 :(得分:3)

一般的答案是:

(eval-after-load 'clojure-mode
  '(define-key clojure-mode-map [tab] 'my-tab-command))

正如你所描述的那样定义:

(defun my-tab-command (&optional arg)
  (interactive "P")
  (if (and (zerop (current-column)) (eq (char-after) ?\())
      (hs-toggle-hiding)
    (indent-for-tab-command arg)))

答案 1 :(得分:3)

clojure-indent-functionlisp-indent-function的实现,应该缩进,而是计算缩进。它可以随时被任何对可能的缩进感兴趣的代码调用,因此我们当然不希望将我们想要的 TAB 行为挂钩到这个地方。

鉴于 TAB 的智能性可能不是您想要的有趣点,重新绑定TAB以将我们的逻辑放在 all <之前可能会更好/ em>可能的聪明:

(defun clojure-hs-tab (arg)
  (interactive "P")
  (if (and (<= (current-column) 1)
           (save-excursion
             (beginning-of-line)
             (looking-at "\(")))
      (hs-toggle-hiding)
    (indent-for-tab-command arg)))

(define-key clojure-mode-map (kbd "TAB") 'clojure-hs-tab)    

我冒昧地修改你的要求并允许第1列, 因为那是hs-toggle-hide在隐藏后放置点的地方。难道你不想用第二个 TAB 按键取消隐藏吗?

下一级“标签智能”是indent-line-function 变量。这是在完成或缩进标签时调用的内容 决定缩进而不是完成。有一个强者 不在此处使用它的原因:可以调用indent-line-function 缩进区域多次。即使我们决定我们想要 仅覆盖缩进和完成的缩进行为 TAB ,建议indent-for-tab-command更好 (建议全球并检查major-mode只做我们想做的事 在那种模式下)。