在Emacs中重载密钥绑定

kla*_*add 5 emacs elisp cc-mode

我已经查看了一些其他问题和el文件,寻找我可以修改以满足我的需求但我遇到麻烦所以我来找专家.

无论如何,根据光标在行中的位置,键的行为会有所不同吗?

更具体地说,如果我在行的中间,我想将tab键映射到行的末尾,但如果我的光标位于行的开头,通常会作为制表符工作.

到目前为止,我有大括号和引号自动配对并将光标重新定位在C++/Java等中.我想使用tab键来结束行,例如函数没有任何参数.

phi*_*ils 3

根据点在直线上的位置而采取不同的行为是很容易的(请参见(if (looking-back "^") ...)代码)。“[工作]通常作为选项卡”是比较困难的一点,因为这是上下文相关的。

这是一种方法,但我后来想,一种更强大的方法是定义一个具有自己的 TAB 绑定的次要模式,并让该函数动态查找后备绑定。我不确定最后该怎么做,但这里有一个解决方案:

Emacs 键绑定回退

(defvar my-major-mode-tab-function-alist nil)

(defmacro make-my-tab-function ()
  "Return a major mode-specific function suitable for binding to TAB.
Performs the original TAB behaviour when point is at the beginning of
a line, and moves point to the end of the line otherwise."
  ;; If we have already defined a custom function for this mode,
  ;; return that (otherwise that would be our fall-back function).
  (or (cdr (assq major-mode my-major-mode-tab-function-alist))
      ;; Otherwise find the current binding for this mode, and
      ;; specify it as the fall-back for our custom function.
      (let ((original-tab-function (key-binding (kbd "TAB") t)))
        `(let ((new-tab-function
                (lambda ()
                  (interactive)
                  (if (looking-back "^") ;; point is at bol
                      (,original-tab-function)
                    (move-end-of-line nil)))))
           (add-to-list 'my-major-mode-tab-function-alist
                        (cons ',major-mode new-tab-function))
           new-tab-function))))

(add-hook
 'java-mode-hook
 (lambda () (local-set-key (kbd "TAB") (make-my-tab-function)))
 t) ;; Append, so that we run after the other hooks.
Run Code Online (Sandbox Code Playgroud)