asmeurer
asmeurer

Reputation: 91600

Make a keyboard shortcut do something on two presses

In Emacs, I want to make a keyboard combination do one thing if I press it once, and another thing if I press it twice. Beyond that I don't care (probably alternating would make the most sense). I know how to make something alternate, but the thing is, I want it to reset whenever I do something else, so that pressing the shortcut once, moving the cursor, and then pressing it again does the pressing once action, not the pressing twice action.

Upvotes: 2

Views: 138

Answers (2)

artscan
artscan

Reputation: 2350

You can use last-command, for example

(defun eab/etags-find-or-pop ()
  (interactive)
  (if (memq last-command '(eab/etags-find-or-pop))
      (call-interactively 'pop-tag-mark)
    (call-interactively 'etags-select-find-tag-at-point)))

(global-set-key (kbd "M-.") 'eab/etags-find-or-pop)

If you press M-. fisrt time, it calls one thing, and if M-. M-. (second time) it calls another thing.

Upvotes: 3

Stefan
Stefan

Reputation: 28551

The usual way to test this kind of repetition is with (eq this-command last-command).

Upvotes: 1

Related Questions