emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: One character key bindings in editing buffers


From: Stefan Monnier
Subject: Re: One character key bindings in editing buffers
Date: Mon, 30 Jul 2018 13:49:49 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

> The idea is when typing in an editing buffer one very rarely
> types only a single character without others preceding or
> following it, so if only a single character is typed then it can
> execute a command. If other characters are also typed then it
> behaves as usual and inserts that character.

Sounds like a good idea for a package, indeed (no way this can make it
to a default behavior, OTOH).
I just have some comments on the code below.

> (defun char-hotkey ()
>   (interactive)
>   (if (and char-hotkey-mode
>            (not char-hotkey-typing)
>            (sit-for 0.5))
>       (call-interactively (assoc-default last-command-event 
> char-hotkey-commands))
>     (call-interactively 'self-insert-command)))
>
> (defun char-hotkey-wait-for-end ()
>   (setq char-hotkey-typing t)
>   (when (or (not (or (equal this-command 'self-insert-command)
>                      (equal this-command 'char-hotkey)))
>              (sit-for 0.5))
>     (setq char-hotkey-typing nil)))

I recommend you try and avoid sit-for as much as possible (e.g. because
it delays running other post-command-hooks).
E.g. in char-hotkey-wait-for-end, better just set some global var to the
current time, and then in char-hotkey compare that to the current time
to see if 0.5s have elapsed.

> (define-minor-mode char-hotkey-mode
>   "Char hotkey."
>   :lighter " CH"
>
>   (if char-hotkey-mode
>       (progn
>         (dolist (command char-hotkey-commands)
>           (local-set-key (char-to-string (car command)) 'char-hotkey))
>         (add-hook 'post-command-hook 'char-hotkey-wait-for-end nil t))
>
>     (dolist (command char-hotkey-commands)
>       (local-set-key (char-to-string (car command)) 'self-insert-command))
>     (remove-hook 'post-command-hook 'char-hotkey-wait-for-end t)))

Over the years, I've grown to like minor modes to start by
unconditionally turning themselves off (i.e. cleaning up):
E.g.

    (define-minor-mode char-hotkey-mode
      "Char hotkey."
      :lighter " CH"
     
      (dolist (command char-hotkey-commands)
        (local-set-key (char-to-string (car command)) 'self-insert-command))
      (remove-hook 'post-command-hook #'char-hotkey-wait-for-end t)

      (when char-hotkey-mode
        (dolist (command char-hotkey-commands)
          (local-set-key (char-to-string (car command)) 'char-hotkey))
        (add-hook 'post-command-hook #'char-hotkey-wait-for-end nil t)))

Also, I'd recommend you use command remapping here:

    (define-minor-mode char-hotkey-mode
      "Char hotkey."
      :lighter " CH"

      (local-set-key [remap self-insert-command] nil)
      (remove-hook 'post-command-hook #'char-hotkey-wait-for-end t)

      (when char-hotkey-mode
        (local-set-key [remap self-insert-command] 'char-hotkey)
        (add-hook 'post-command-hook #'char-hotkey-wait-for-end nil t)))

Also, you can use the minor-mode map feature, so the key-bindings are
automatically (de)activated:

    (defvar char-hotkey-mode-map
      (let ((map (make-sparse-keymap)))
        (define-key map [remap self-insert-command] 'char-hotkey)
        map))

    (define-minor-mode char-hotkey-mode
      "Char hotkey."
      :lighter " CH"
      (remove-hook 'post-command-hook #'char-hotkey-wait-for-end t)
      (when char-hotkey-mode
        (add-hook 'post-command-hook #'char-hotkey-wait-for-end nil t)))


-- Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]