我查看了许多其他问题和 el 文件,寻找可以修改以满足我的需要的东西,但我遇到了麻烦,所以我找了专家。
无论如何,根据光标所在行的位置,键的行为是否有所不同?
更具体地说,如果我在行的中间,我想将制表键映射到行尾,但如果我的光标位于行的开头,通常会像制表符一样工作。
到目前为止,我有大括号和引号自动配对并在其中重新定位光标以用于 C++/Java 等。例如,如果函数没有任何参数,我想使用 tab 键来结束行.
根据点在行中的位置而表现不同是简单的一点(参见(if (looking-back "^") ...)
代码)。“[Working] as a tab 通常会”是更难的一点,因为这是上下文相关的。
这是一种方法,但我后来想,一种更健壮的方法是定义一个具有自己的 TAB 绑定的次要模式,并让该函数动态查找回退绑定。我不确定最后该怎么做,但这里有一个解决方案:
(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.
Emacs Wiki 的这个页面列出了几个包(smarttab 等),它们使 TAB 根据上下文执行不同的操作。您可能可以修改其中一个来做您想做的事。