5

I want to make a minor-mode (foo-mode) which has its keymap (foo-mode-map), but when the user presses any key not in (foo-mode-map), the minor-mode should quit. How do I bind the turn-off-foo-mode all other keys?

EDIT: here is the solution I came up with based on the chosen answer. It accepts numeric input as well.

(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)
halfer
  • 19,824
  • 17
  • 99
  • 186
event_jr
  • 17,467
  • 4
  • 47
  • 62

4 Answers4

4

One thing you have to decide is if, when the user presses another key, you should simply quit your mode, of if you should run the command bound to the key in question (like in incremental-search).

One way would be to use pre-command-hook to check if this-command is one of your commands, and turn off your mode if that would not be the case.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • Using `pre-command-hook` for this is a lot of overhead for each keystroke; it is very noticeable when you hit a key and hold it down... the repeat slows ways down. Also, there can be problems using this with `self-insert-command`. – aculich Dec 18 '11 at 22:04
  • The slowdown problem can easily be handled by removing the hook when exiting foo-mode. Admittedly, there will be a slowdown while foo-mode runs, but I doubt that really would make a difference, unless it's a really time-critical package (which I seriously doubt). – Lindydancer Dec 18 '11 at 22:36
  • Regarding your claim that this should not work with `self-insert-command`, could you elaborate on this? (I just tested it, and it seems to work just fine....) – Lindydancer Dec 18 '11 at 22:41
  • An example of where the slowdown will be a problem is if `j` were bound to `next-line` to emulate navigation in vi; there is a noticeable difference in speed when using a `pre-command-hook`. – aculich Dec 18 '11 at 22:47
4

Solution 2: You could use set-char-table-range to set all characters in a map. For example:

(defvar foo-mode-map
  (let ((map (make-keymap)))
    (set-char-table-range (nth 1 map) t 'foo-turn-off-foo-mode)
    ...
    map))
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • This will not work properly unless you add `map` as the last expression in the `let` otherwise `foo-mode-map` will get bound to the value of the last expression is; in your example that would be the value returned by `set-char-table-range` or whatever they filled in for `...`. – aculich Dec 18 '11 at 22:07
4

I've included a full working example for creating a minor mode with the kind of behavior you want; the key is to use set-char-table-range on a keymap created by make-keymap which creates a dense keymap with a full char-table; using this on a sparse keymap created with make-sparse-keymap will not work.

(defalias 'foo-electric-delete 'backward-kill-word)

(defun foo-mode-quit (&optional arg)
  (interactive)
  (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 [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)

(defvar major-baz-mode-map '(keymap (t . major-baz-mode-default-function)))

Setting a default binding for a major mode map is easier and I include this example here, but as I noted above, this kind of spare keymap will not work for a minor mode:

(defvar major-baz-mode-map '(keymap (t . major-baz-mode-default-function)))

This is discussed in the documentation in Format of Keymaps where it says:

(t . binding)

This specifies a default key binding; any event not bound by other
elements of the keymap is given binding as its binding. Default
bindings allow a keymap to bind all possible event types without
having to enumerate all of them. A keymap that has a default binding
completely masks any lower-precedence keymap, except for events
explicitly bound to nil (see below).
event_jr
  • 17,467
  • 4
  • 47
  • 62
aculich
  • 14,545
  • 9
  • 64
  • 71
  • I must issue a big warning regarding the example give above. When implementing an Emacs package, it is important to be able to reload a module without overwriting user settings. Normally `defvar` will not overwrite a variable if it already has value. The code above mix `defvar` with explicit calls to modifying functions like `set-char-table-range`. – Lindydancer Dec 18 '11 at 22:32
  • @Lindydancer thanks, you're right. I updated the code to use a `let` instead. – aculich Dec 18 '11 at 22:40
  • You lost a pair of parenthesis in first arg of (let ...) – kuanyui Jun 18 '14 at 21:26
1

Answer 3:

I would say that the selected solution is way too complicated. Starting to manipulate the event queue is not something that you typically would like to do.

Instead, I would suggest a radically different solution. If you leave the mode always on, you could bind keys like backspace to a function that could determine by itself if it should operate on a character-by-character basis or on words.

Below is a simple proof-of-concept. It still has an explicit function to enter word mode, but that could be generalized into functions that both enable word mode and perform some kind of action.

(defun foo-electric-enabled nil
  "Non-nil when foo-mode operate in word mode.")

(defun foo-electric-delete (&optional arg)
  (interactive "p")
  (if (and foo-electric-enabled
           (memq last-command '(kill-region
                                foo-electric-delete
                                foo-enable-word-operations)))
      (call-interactively 'backward-kill-word)
    (setq foo-electric-enabled nil)
    (call-interactively 'backward-delete-char-untabify)))

(defvar foo-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map [backspace] 'foo-electric-delete)
    map))

(defun foo-enable-word-operations ()
  (interactive)
  (setq foo-electric-enabled t))

(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 backspace 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)

The code above can be enhanced, but I leave that as an exercise for the reader:

  • Instead of checking if a function is a member of a literal list, there could be a module-wide list, alternatively, you could use properties on functions that should be treated as though you would like to remain in word mode.

  • Things like the mode-line indicator is always on. By implementing this package as using another minor modes, you could use the second to own the mode-line indicator and to indicate that the package is in word mode, replacing foo-electric-enabled and foo-enable-word-operations.

  • The function foo-electric-delete explicitly calls either backward-kill-word or backword-delete-char-untabify. There are a number of techniques to call whatever was bound to meta backspace and backspace, respectively.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • This is similar to a `post-command-hook` solution, but more convoluted? ;) I actually went with a post-command hook for my use. – event_jr Dec 19 '11 at 16:16
  • It's very different from a `post-command-hook` solution, I would say. To start with, a `post-command-hook` runs after the command, so you can't really change what the command does in any manner. Secondly, you will take a penalty for everything you add to a `post-` or `pre-command-hook`, as every command you issue has to be passed through your hook. Also, a function added to such a hook can't issue an error (when doing so, they are silently removed from the hook). 20 years of Emacs development say taught me to stay away from the `pre-` and `post-command-hook`, unless I really have to use them. – Lindydancer Dec 19 '11 at 16:40