all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: One character key bindings in editing buffers
Date: Mon, 30 Jul 2018 13:49:49 -0400	[thread overview]
Message-ID: <jwv7elck4o7.fsf-monnier+gmane.emacs.devel@gnu.org> (raw)
In-Reply-To: trinity-cc35b176-b05f-40cd-b3bd-a2688baaa96b-1532964506182@3c-app-mailcom-lxa04

> 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	other threads:[~2018-07-30 17:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-30 15:28 One character key bindings in editing buffers JJ
2018-07-30 17:49 ` Stefan Monnier [this message]
  -- strict thread matches above, loose matches on Subject: below --
2018-07-30 19:30 J J
2018-07-30 22:14 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwv7elck4o7.fsf-monnier+gmane.emacs.devel@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.