help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Single-use keybindings?


From: Eric Abrahamsen
Subject: Re: Single-use keybindings?
Date: Sat, 22 Jul 2017 18:22:20 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Drew Adams <drew.adams@oracle.com> writes:

>> A few times I've found myself wanting the ability to set a single-use
>> keybinding: the next time the user hits "q" (or what have you), run this
>> command, then reset "q" to whatever it was before.
>> 
>> This feels hacky, obviously, but more importantly it feels like I'm
>> not using the right tools, or misusing what I've got. Has anyone else
>> wanted to do this? Any better solutions? Maybe I should be using a minor
>> mode that turns itself off after use, or...
>
> See `set-transient-map'.  From (elisp) `Controlling Active Maps':

There's the new version, which I'm feeling much better about. Thanks
again.

(defun ebdb-mua-in-ebdb-buffer ()
  "From an MUA, temporarily move point to the corresponding EBDB buffer.

All further operations will take place within the EBDB buffer as
per normal, with the exception that \"q\" will return point to
where it was in the MUA, rather than quitting the EBDB buffer."
  (interactive)
  (let* ((buf (get-buffer (ebdb-make-buffer-name)))
         (w-conf (current-window-configuration))
         (w-win (selected-window))
         (w-point (window-point))
         (e-win (or (window-live-p (get-buffer-window buf))
                    (ebdb-pop-up-window buf t (ebdb-popup-window))))
         (key-m (make-sparse-keymap)))
    (define-key key-m (kbd "q")
      (lambda ()
        (interactive)
        (when (window-live-p w-win)
          (set-window-configuration w-conf)
          (goto-char w-point))))
    (select-window e-win t)
    (set-transient-map key-m)))




reply via email to

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