19

我想使用一个次要模式来重新绑定我绝对想要保留的主要模式键。如何重新绑定密钥而不将其从次要模式映射中全局删除?我知道我可以使用define-key它,但我想保留其他缓冲区/主要模式的绑定。

任何人都可以帮忙吗?

4

3 回答 3

16

做起来有点麻烦。您可以执行以下操作:

(add-hook '<major-mode>-hook
  (lambda ()
    (let ((oldmap (cdr (assoc '<minor-mode> minor-mode-map-alist)))
          (newmap (make-sparse-keymap)))
      (set-keymap-parent newmap oldmap)
      (define-key newmap [<thekeyIwanttohide>] nil)
      (make-local-variable 'minor-mode-overriding-map-alist)
      (push `(<minor-mode> . ,newmap) minor-mode-overriding-map-alist))))
于 2012-10-27T18:13:47.873 回答
14

这是一个处理所有繁琐位的函数。

(defun local-set-minor-mode-key (mode key def)
  "Overrides a minor mode keybinding for the local
   buffer, by creating or altering keymaps stored in buffer-local
   `minor-mode-overriding-map-alist'."
  (let* ((oldmap (cdr (assoc mode minor-mode-map-alist)))
         (newmap (or (cdr (assoc mode minor-mode-overriding-map-alist))
                     (let ((map (make-sparse-keymap)))
                       (set-keymap-parent map oldmap)
                       (push `(,mode . ,map) minor-mode-overriding-map-alist) 
                       map))))
    (define-key newmap key def)))

之后你可以做

(local-set-minor-mode-key '<minor-mode> (kbd "key-to-hide") nil)
于 2013-02-08T09:11:26.493 回答
3

就我而言,company-mode正在覆盖公司完成菜单显示时的cider-repl-mode绑定。完成菜单的键映射是,但没有与之对应的次要模式(用于菜单激活时),因此我无法使用任何现有答案。M-pM-ncompany-active-mapcompany-mode

这是我想出的:

(add-hook 'cider-repl-mode-hook
          (lambda ()
            (make-local-variable 'company-active-map)
            (setq company-active-map (copy-tree company-active-map))
            (define-key company-active-map (kbd "M-p") nil)
            (define-key company-active-map (kbd "M-n") nil)))
于 2016-08-01T23:41:28.777 回答