unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* One character key bindings in editing buffers
@ 2018-07-30 15:28 JJ
  2018-07-30 17:49 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: JJ @ 2018-07-30 15:28 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/html, Size: 4294 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: One character key bindings in editing buffers
  2018-07-30 15:28 JJ
@ 2018-07-30 17:49 ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2018-07-30 17:49 UTC (permalink / raw)
  To: emacs-devel

> 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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: One character key bindings in editing buffers
@ 2018-07-30 19:30 J J
  2018-07-30 22:14 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: J J @ 2018-07-30 19:30 UTC (permalink / raw)
  To: emacs-devel


> Sounds like a good idea for a package, indeed (no way this can make it
> to a default behavior, OTOH).

Naturally, it should not be default behavior. It's an advanced
feature, so people should turn it on explicitly, so they know
what's going on.

>
> 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.

Thanks, I didn't know that. For some reason I thought sit-for is
implemented with a callback or something behind the scenes, so
while it waits for input or elapsing time other code can keep on
running during that.


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

Yes, that is one of the points which a proper package should
handle. In theory it's also possible that if you use some
punctuation mark for hotkey then it also already has an electric
or similar binding, not self-insert-command, though it's not
likely with letter hotkeys.

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

Nifty. Makes things simpler.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: One character key bindings in editing buffers
  2018-07-30 19:30 One character key bindings in editing buffers J J
@ 2018-07-30 22:14 ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2018-07-30 22:14 UTC (permalink / raw)
  To: emacs-devel

> Yes, that is one of the points which a proper package should
> handle. In theory it's also possible that if you use some
> punctuation mark for hotkey then it also already has an electric
> or similar binding, not self-insert-command, though it's not
> likely with letter hotkeys.

Electricity for keys should use post-self-insert-hook nowadays, so even
"electric" keys should be bound to `self-insert-command`.

But yes, some packages still use other mechanisms, so you may need to
add more commands like self-insert-command to the remapping.


        Stefan




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-07-30 22:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-30 19:30 One character key bindings in editing buffers J J
2018-07-30 22:14 ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2018-07-30 15:28 JJ
2018-07-30 17:49 ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).