临时覆盖(全局覆盖emacs中的键绑定) [英] Override (globally override key binding in emacs) temporarily

查看:243
本文介绍了临时覆盖(全局覆盖emacs中的键绑定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这里所述的方法:全局覆盖 - 键绑定 - emacs

I use the method stated here : globally-override-key-binding-in-emacs

(defvar my-keys-minor-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-i") 'some-function)
    map)
  "my-keys-minor-mode keymap.")

(define-minor-mode my-keys-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  ;; Define-minor-mode will use `my-keys-minor-mode-map' for us.
  :init-value t
  :lighter " my-keys")

(defun my-keys-keep-on-top (&rest _)
  "Try to ensure that my keybindings always have priority."
  (unless (eq (caar minor-mode-map-alist) 'my-keys-minor-mode)
    (let ((mykeys (assq 'my-keys-minor-mode minor-mode-map-alist)))
      (assq-delete-all 'my-keys-minor-mode minor-mode-map-alist)
      (add-to-list 'minor-mode-map-alist mykeys))))
(add-hook 'after-load-functions #'my-keys-keep-on-top)

(my-keys-minor-mode 1)

如何显式覆盖此?
链接中的方法对我来说不起作用。

How to I explicitly override this? The methods in the link is not working to me.

EDIT2:

问题不仅应用于只读模式。 eval-after-load 不是一个好的方法,因为它将更改为 my-minor-mode-keymap 长期以来。

Issue is not only applied to read only mode. The eval-after-load is not a good method since it makes change to my-minor-mode-keymap in long term.

我再说一遍:

我大部分时间都使用程序模式。假设我想在程序模式下将 C-q 绑定到语法检查。但是,我有时在文本模式中记录笔记。在文本模式下进行语法检查是没有意义的。所以我决定让 Cq 绑定到拼写检查。

I use program mode for most of the time. Suppose I want to have C-q bind to syntax-check in program mode. However, I jot notes in text mode sometime. It makes no sense to have syntax check in text mode. So I decide to have C-q bind to spell-check.

在正常情况下,我会这样做

Under normal circumstances, I would do this

(global-set-key (kbd "C-q") 'syntax-check)
(define-key text-mode-map (kbd "C-q") 'spell-check)

如果我定义我的密钥小调模式地图?
假设我将其放在我的键盘映射中:

How to have such effect if I define my-keys-minor-mode-map? Assume I put this in my keymap:

(define-key 'my-keys-minor-mode-map (kbd "C-q") 'syntax-check)

(define-key text-mode-map(kbdCq)'拼写检查)不起作用,因为我的键盘映射始终优先于其他次模式映射。

(define-key text-mode-map (kbd "C-q") 'spell-check) doesn't work since my keymap always has precedence over other minor mode mapping.

(eval-after-load "text-mode"
  (define-key my-keys-minor-mode-map (kbd "C-q") 'spell-check))

/ p>

nor

(add-hook 'text-mode-map (lambda () 
  (define-key my-keys-minor-mode-map (kbd "C-q") 'spell-check)))

做得好因为他们长期改变了我的键 - 小模式地图。这意味着当我从文本模式切换到程序模式时,我失去了语法检查功能。

do a good job because they change the my-keys-minor-mode-map in long term. That means when I switch back from text mode to program mode, I lose syntax check function.

编辑:

例如,

(define-key my-keys-minor-mode-map (kbd "<return>") 'newline-and-indent)

这个工作99%的时间很好,以防止任何其他小的模式或主要模式来修改这个。

this works 99% of time and is good to prevent any other minor modes or major modes to modify this.

但是,键映射在只读模式下有问题。所以我需要有超级优先功能来重新映射这个键。我想要一些类似

However, the key map has problem in read only mode. So I need to have "super priority" function to remap this key. I want something like

(defun super-priority-map()
  (define-key super-my-keys-minor-mode-map (kbd "<return>") 'something-else))
(add-hook 'some-modes 'super-priority-map)

更重要的是,超级优先级地图应该只要留下指定的模式就起飞。

And more important, the super-priority-map should take off as long as that specified mode is left.

推荐答案

你在谈论只读功能?您不能用键盘映射来覆盖它。没有只读键盘映射捕获所有按键并发出错误。

You're talking about read-only functionality? You can't override that with a keymap. There's no "read-only" keymap capturing all keystrokes and issuing errors.

覆盖只读模式只需切换只读模式

在代码中,设置缓冲区-read-only 变量。

您还可以允许绑定 prohib-read-only 代码中的变量忽略所有只读机制(包括特定文本区域的属性 - 请参阅下面的列表中的第7级和第3级)。

You can also let-bind the inhibit-read-only variable in code to ignore all read-only mechanisms (including properties on specific regions of text -- see levels 7 and 3 in the list below).

只读一边,它完全可能的其他键映射优先于你的小模式地图,但我是倾向于说,对于任何与您的次要模式键盘映射的罕见冲突,您应该直接修改与您自己的冲突的键盘映射。

Read-only aside, it is entirely possible for other keymaps to have precedence over your minor mode map, but I'm inclined to say that for any rare conflicts with your minor mode keymap, you should directly modify the keymap which is conflicting with your own.

您的次要模式键盘映射实际上可以是被许多其他人取代,但你可能不希望在较高的优先级别上工作。

Your minor mode keymap can in fact be superseded by many others, but you probably don't want to be working at a higher level of priority in general.

我推荐rea ding 掌握Emacs中的关键绑定,这是一个非常可读的解释东西有用,我从中扣除了以下键盘优先级列表:

I recommend reading Mastering Key Bindings in Emacs which is a very readable explanation of how this stuff works, and from which I've nabbed the following keymap priority list:


  1. overriding-terminal-local-
  2. 覆盖本地地图为应该覆盖所有的密钥其他本地键盘图。

  3. 键盘映射字符点本地的键盘映射点的char属性位于。这用于诸如yasnippet和自定义对话框中的字段。

  4. emulation-mode-map-alists 用于高级多模式键盘映射管理

  5. minor-mode-overriding-map-alist 用于覆盖主要模式中次要模式使用的键盘映射。 >
  6. minor-mode-map-alist 完全像上面的重写版本,而是为次要模式指定键盘映射的首选方法。 li>
  7. 键盘映射文本属性的点与上面的char属性一样,仅适用于文本属性。

  8. current-local-map 用于缓冲区当前本地地图中定义的键盘图

  9. current-全球地图是Emacs将寻找关键绑定的最后一个地方,它是针对全局的。

  1. overriding-terminal-local-map for terminal-specific key binds.
  2. overriding-local-map for keys that should override all other local keymaps. Be VERY careful if you use this!
  3. Keymap char property at point for keymaps that are local to the character point is at. This is used for stuff like fields in yasnippet and the customize dialog.
  4. emulation-mode-map-alists for advanced multi-mode keymap management
  5. minor-mode-overriding-map-alist for overriding the keymaps used by minor modes in major modes.
  6. minor-mode-map-alist is exactly like the overriding version above, but the preferred means of specifying the keymaps for minor modes.
  7. Keymap text property at point is like the one above for char properties but is for text properties only.
  8. current-local-map for keymaps defined in the buffers’ current local map
  9. current-global-map is the last place Emacs will look for key binds and it is for the global ones.

请注意,您的次要模式正在6级工作,并且可以优先考虑以上所有内容。

Note that your minor mode is working at level 6, and everything above that could take precedence.

解除冲突的典型模式将是是:

A typical pattern to unbind a conflict would be:

(eval-after-load "NAME-OF-LIBRARY"
  '(define-key NAME-OF-KEYMAP (kbd "KEY") nil)) ;; unbind KEY in KEYMAP implemented by LIBRARY

中的KEYMAP中解除绑定KEY最有可能的是,您也可以添加原始命令的替换(非冲突)绑定。

Most likely, you would also add a replacement (non-conflicting) binding for the original command.

这篇关于临时覆盖(全局覆盖emacs中的键绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆