大多数emacs模式都包含某种激活其功能的前缀.例如,当使用GUD时, "next"是"Cc Cn".在这些模式中,许多模式提供了特殊的缓冲区,其中一个可以使用单个键来激活某些功能(例如,只需"n"或"p"来读取GNUS中的下一个/上一个邮件).
然而,并非所有模式都提供这样的缓冲区,并且重复键入前缀可能是令人厌倦的.是否有一个众所周知的elisp位允许在一段时间内对所有输入进行特殊的前缀键规范?(例如,在击中ESC或其他一些受制裁的密钥之前)
我同意Joe Casadonte的回答,即走法是定义自己的次要(或主要)模式.
话虽这么说,你的问题很有趣.
这是一个解决方案,提示您输入一个键序列,它采用前缀击键并将该键盘映射提升到顶层.
例如,假设以下键映射:
M-g ESC Prefix Command M-g g goto-line M-g n next-error M-g p previous-error
当你运行时M-x semi-modal-minor-mode
,它会提示你进行一些击键.如果输入M-g n
,则设置以下键绑定:
ESC Prefix Command (same as M-g ESC) g goto-line n next-error p previous-error
所以现在n
不会自行插入,而是跳转到下一个错误.请参阅下面的代码.
注意:启用此次要模式时,
将绑定一个禁用次要模式的命令.这是因为键绑定可能会很好地禁用您的Emacs(例如,如果有新的键绑定,该怎么办M-x
).
编辑添加这些想法:次模式变量最初是缓冲本地的,但除非你也使minor-mode-alist变量缓冲区本地(duh),否则这不起作用.但是,你也(可能)不想在迷你缓冲区中使用这些绑定...所以,我不打算测试它b/c它真的取决于你想要什么,但我已经添加了对代码的评论反映这种想法.
无需再费周折:
(defvar semi-modal-minor-mode-keymap (make-sparse-keymap) "keymap holding the prefix key's keymapping, not really used") (defvar semi-modal-minor-mode-disable-key (kbd "") "key to disable the minor mode") (defun semi-modal-minor-mode-disable () "disable the minor mode" (interactive) (semi-modal-minor-mode 0)) (define-minor-mode semi-modal-minor-mode "local minor mode that prompts for a prefix key and promotes that keymap to the toplevel e.g. If there are bindings like the following: M-g ESC Prefix Command M-g g goto-line M-g n next-error M-g p previous-error And you enter 'M-g n' when prompted, then the minor mode keymap has the bindings g -> goto-line n -> next-error p -> previous-error ESC -> Prefix Command (same as M-g ESC) The variable semi-modal-minor-mode-disable-key is bound to disable the minor mode map. This is provided because often the mappings make the keyboard unusable. Use at your own risk." nil " Semi" semi-modal-minor-mode-keymap (make-local-variable 'semi-modal-minor-mode) (make-local-variable 'minor-mode-map-alist) (let ((pair-holding-keymap-to-modify (assq 'semi-modal-minor-mode minor-mode-map-alist))) (setcdr pair-holding-keymap-to-modify (make-sparse-keymap)) (if semi-modal-minor-mode (let (key keymap) ;; all but last (b/c we want a prefix (setq key (substring (read-key-sequence "Enter a full key combination, the prefix will be used: ") 0 -1)) (if (and (not (equal "" key)) (not (equal (kbd "C-g") key)) (let ((semi-modal-minor-mode nil)) (keymapp (setq keymap (key-binding key))))) (progn (setcdr pair-holding-keymap-to-modify (copy-keymap keymap)) (when semi-modal-minor-mode-disable-key (define-key (cdr pair-holding-keymap-to-modify) semi-modal-minor-mode-disable-key 'semi-modal-minor-mode-disable))) (semi-modal-minor-mode 0))))))