简体   繁体   English

Emacs Emacs 23和Emacs 24之间的键绑定更改

[英]Emacs keybinding changes between Emacs 23 and Emacs 24

I recently upgraded to Emacs24, and a number of my custom keybindings broke as a result of it. 我最近升级到了Emacs24,由于它,我的一些自定义键绑定被破坏了。

According to the fine manual, it is possible to make Emacs stop conflating function keys with their ASCII control codes (eg, it is possible to have Cm and RET bound to different things, or Ci and TAB , and so on). 根据精细的手册,可以使Emacs停止将功能键与其ASCII控制代码混淆(例如,可以将CmRET绑定到不同的东西,或CiTAB ,等等)。 This has always been a large pet peeve of mine with Emacs, that such valuable "first level" keyboard shortcuts are wasted on things for which I already have dedicated keys on my keyboard. 这一直是我用Emacs的一个大小的烦恼,这种有价值的“第一级”键盘快捷键浪费在我已经在键盘上有专用键的东西上。 I want to bind them to different things, in my case, to 'modernize' the keybindings by mimicking gedit. 在我的例子中,我希望将它们绑定到不同的东西,通过模仿gedit来“现代化”键绑定。 In Emacs23, this was working beautifully: 在Emacs23中,这很漂亮:

(global-set-key (kbd "C-i") 'goto-line)
(global-set-key (kbd "C-m") 'comment-or-uncomment-region)
(global-set-key (kbd "C-d") 'kill-whole-line)

;; Fix some stuff broken by the above
(global-set-key [delete] 'delete-char)
(global-set-key (kbd "TAB") 'indent-for-tab-command)
(global-set-key (kbd "RET") 'newline)

Then, I upgraded to Emacs24 and it broke, kinda. 然后,我升级到Emacs24,它破了,有点。 It still "works" in the sense that Cm certainly does one thing, and RET does another, but the problem is that the return key no longer behaves properly in terminal mode or in the minibuffer. 它仍然“工作”,因为Cm肯定会做一件事,而RET会做另一件事,但问题是返回键在终端模式或迷你缓冲器中不再正常运行。 Instead of activating the command I've just typed, in both cases, the return key simply moves the cursor down to the next line and I'm left with no way of activating the commands I type in to either the minibuffer or the terminal. 在两种情况下,返回键只是将光标向下移动到下一行,而不是激活我输入的命令而不是激活我输入迷你缓冲区或终端的命令。

Ironically, Emacs24 introduced a lot of changes to the behavior of deleting, and in the process they decoupled Cd from DEL so that it's actually now safe to bind Cd to something without needing to bind DEL back to the expected behavior, so it would be great if I could achieve similar "it just works" behavior for my return key, while Cm is bound to something else. 具有讽刺意味的是,Emacs24对删除行为进行了很多更改,并且在此过程中它们将CdDEL分离,因此实际上现在可以安全地将Cd绑定到某些东西而无需将DEL绑定回预期的行为,因此它会很棒如果我可以为我的返回键实现类似的“它只是工作”行为,而Cm则绑定到别的东西。

So, I can envision two possible solutions to this problem. 所以,我可以设想两个可能的解决方案来解决这个问题。 One might look like this: 一个可能看起来像这样:

(global-set-key (kbd "C-m") 'comment-or-uncomment-region)
(global-set-key (kbd "RET") 'do-what-i-expect-the-return-key-to-do-in-any-mode)

OR, something like this would be even nicer: 或者,这样的事情会更好:

(setq decouple-ascii-control-codes-from-function-keys t)

But I'm not aware of any such variable or function that would help me out in this scenario. 但我不知道任何这样的变量或函数可以帮助我在这种情况下。

I've made several unsuccessful attempts at using mode-hooks to restore the correct bindings in terminal and minibuffer modes, but I just can't seem to get anything to work. 我已经做了几次尝试使用模式挂钩在终端和迷你缓冲模式下恢复正确绑定的尝试失败,但我似乎无法得到任何工作。 Help! 救命!

Thanks. 谢谢。

This seems to work: 这似乎有效:

(add-hook 'find-file-hook
          (lambda ()
            (local-set-key (kbd "C-m") 'comment-or-uncomment-region)
            (local-set-key (kbd "<return>") 'newline-and-indent)))

The idea here is that instead of tinkering with the return key globally (which is what breaks the terminal and minibuffer buffers), we only set these keybindings on a per-buffer basis, except that we do it unconditionally for all buffers that represent files on disk. 这里的想法是,不是全局修改返回键(这是破坏终端和迷你缓冲区缓冲区),我们只在每个缓冲区的基础上设置这些键绑定,除了我们无条件地为表示文件的所有缓冲区。磁盘。

It's a little bit inefficient, having to run every time I open a file, but it's nice insofar as I don't have to think of every possible mode to "fix", it simply doesn't break terminal/minibuffer/etc modes in the first place. 它有点低效,每次打开文件时都要运行,但是因为我不必考虑每种可能的“修复”模式,所以它很好,它根本不会破坏终端/迷你缓冲/等模式第一名。

The way these "sister keys" are handled by default in Emacs is to redirect (via function-key-map) the special keys (like tab , and return ) to their ASCII equivalent, and then only add key-bindings to the ASCII version. 默认情况下,在Emacs中处理这些“姐妹键”的方法是将特殊键(如tabreturn )重定向(通过function-key-map)到它们的ASCII等价物,然后只将键绑定添加到ASCII版本。 So you can easily add new meanings to the non-ASCII version with something like 因此,您可以轻松地为非ASCII版本添加新的含义

(global-set-key [return] 'my-new-command)

but in your case you want to do the reverse which is to let return behave as before while changing Cm . 但在你的情况下,你想要做的相反是让return以前一样的行为,同时改变Cm The most reliable way I can think of to do that (reliable in the sense that it should work with most major/minor modes bindings) is to remap Cm early and unconditionally to some new event, as in: 我能想到的最可靠的方法(在大多数主要/次要模式绑定应该可行的意义上可靠)是将Cm早期和无条件地重新映射到某个新事件,如:

(define-key input-decode-map [?\C-m] [C-m])
(define-key input-decode-map [?\C-i] [C-i])

this will not affect the handling of return and tab since input-decode-map is applied before function-key-map , ie before those keys are turned into ASCII control keys. 这不会影响returntab的处理,因为input-decode-map是在function-key-map之前应用的,即在将这些键转换为ASCII控制键之前。 So you can then do: 所以你可以这样做:

(global-set-key [C-m] 'my-new-command)
(global-set-key [C-i] 'my-newer-command)

A downside is that this will not only apply to bindings for Ci but also to bindings for Cc Ci which will now only work as Cc TAB (which will sometimes be just fine, but might occasionally be less mnemonic). 一个缺点是,这不仅适用于Ci绑定,也适用于Cc Ci绑定, Cc Ci现在只能用作Cc TAB (有时候会很好,但偶尔可能会少于助记符)。

Another downside is that if there is a binding for tab , then tab won't be useable to reach a Ci binding. 另一个缺点是,如果tab有绑定,则tab不可用于达到Ci绑定。 But we can fix those two problems by adding the following: 但我们可以通过添加以下内容来解决这两个问题:

(define-key function-key-map [C-i] [?\C-i])
(define-key function-key-map [C-m] [?\C-m])

which will turn the new Ci event back into a normal Ci in case where there is no binding that uses the new event. 如果没有使用新事件的绑定,它会将新的Ci事件重新转换为普通Ci

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM