我想创建一个次要模式(foo-mode),它有自己的键映射(foo-mode-map),但是当用户按下不在(foo-mode-map)中的任何键时,此次要模式应该退出。 我如何绑定 turn-off-foo-mode 到所有其他键?
编辑:这是我根据所选择的答案想出的解决方案。 它也接受数字输入。
(defalias 'foo-electric-delete 'backward-kill-word)
(defun foo-mode-quit (&optional arg)
(interactive)
(let ((global-binding (lookup-key (current-global-map)
(single-key-description last-input-event))))
(unless (eq last-input-event ?\C-g)
(push last-input-event unread-command-events))
(unless (memq global-binding
'(negative-argument digit-argument))
(foo-mode -1))))
(defvar foo-mode-map
(let ((map (make-keymap)))
(set-char-table-range (nth 1 map) t 'foo-mode-quit)
(define-key map "-" 'negative-argument)
(dolist (k (number-sequence ?0 ?9))
(define-key map (char-to-string k) 'digit-argument))
(define-key map [backspace] 'foo-electric-delete)
map))
(define-minor-mode foo-mode
"Toggle Foo mode.
With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode.
When Foo mode is enabled, the control delete key
gobbles all preceding whitespace except the last.
See the command \\[foo-electric-delete]."
;; The initial value.
:init-value nil
;; The indicator for the mode line.
:lighter " Foo"
;; The minor mode bindings.
:keymap foo-mode-map
:group 'foo)
pre-command-hook
对于每个按键来说都是很大的开销;当你按下一个键并按住它时,这非常明显...重复会变得非常缓慢。此外,使用self-insert-command
也可能会出现问题。 - aculichself-insert-command
一起工作的说法,您能详细说明一下吗?(我刚刚测试了一下,它似乎完全正常...) - Lindydancerj
被绑定到next-line
以模拟vi中的导航,那么减速将成为一个问题的例子是;当使用pre-command-hook
时,速度会有明显差异。 - aculich