在 Emacs 中重载键绑定

发布于 2024-12-03 14:47:39 字数 240 浏览 0 评论 0原文

我浏览了许多其他问题和 el 文件,寻找可以修改以满足我的需求的内容,但我遇到了麻烦,所以我找到了专家。

无论如何,是否有一个按键根据光标在行中的位置而表现不同?

更具体地说,如果我位于行的中间,我想将制表符映射到行尾,但如果我的光标位于行的开头,则通常会像制表符一样工作。

到目前为止,我有大括号和引号自动配对,并在其中重新定位光标以用于 C++/Java 等。如果例如函数没有任何参数,我想使用 Tab 键到行尾。

I've looked through a number of other questions and el files looking for something i could modify to suit my needs but I'm having trouble so I came to the experts.

Is there anyway to have a key behave differently depending on where in the line the cursor is?

To be more specific I'd like to map the tab key to go to the end of the line if I'm in the middle of the line but work as a tab normally would if my cursor is positioned at the beginning of the line.

So far I have braces and quotes auto-pairing and re-positioning the cursor within them for C++/Java etc. I'd like to use the tab key to end-of-line if for example a function doesn't have any arguments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-12-10 14:47:39

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

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

Emacs 键绑定后备< /a>

(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.

Behaving differently depending on where point is in the line is the easy bit (see (if (looking-back "^") ...) in the code). "[Working] as a tab normally would" is the harder bit, as that's contextual.

Here's one approach, but I was thinking afterwards that a more robust method would be to define a minor mode with its own binding for TAB and let that function look up the fallback binding dynamically. I wasn't sure how to do that last bit, but there's a solution right here:

Emacs key binding fallback

(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.
¢好甜 2024-12-10 14:47:39

Emacs Wiki 的此页面列出了几个使 TAB 根据不同的情况执行不同操作的软件包(smarttab 等)就上下文而言。您也许可以修改其中之一来执行您想要的操作。

This page of Emacs Wiki lists several packages (smarttab, etc.) which make TAB do different things depending on the context. You can probably modify one of them to do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文