swdev
swdev

Reputation: 5157

In Emacs, can we make one keystroke to do different command?

I want to make one keystroke, say C-F12, to do delete-other-windows or winner-undo. I think it's easy if I already learning Emacs Lisp programming, and set a boolean flag. That is, if previously it run delete-other-window, now it'll run winner-undo.

How do you do that in Emacs Lisp?

Thanks

Upvotes: 1

Views: 171

Answers (3)

jlf
jlf

Reputation: 3539

Here's a solution using the approach taken by Emacs' recenter-top-bottom function:

(defun delete-other-window-or-winner-undo ()
  "call delete-other-window on first invocation and winner-undo on subsequent invocations"
  (interactive)
  (if (eq this-command last-command) 
      (winner-undo)
    (delete-other-windows)))

(global-set-key (kbd "<C-f12>") 'delete-other-window-or-winner-undo)

Upvotes: 1

(defun swdev-toggle-sole-window ()
  (interactive)
  (if (cdr (window-list))
      (delete-other-windows)
    (winner-undo)))
(global-set-key (kbd "<C-f12>") 'swdev-toggle-sole-window)
  1. The first line starts the declaration of a function called swdev-toggle-sole-window, taking no argument.
  2. This function is declared as interactive, i.e. it can be called with M-x or through a key binding.
  3. If the window list contains more than one element, i.e. if there is more than one window, …
  4. … then delete other windows …
  5. … else undo the window deletion.
  6. Bind the function to the key C-f12.

Upvotes: 1

Oleg Pavliv
Oleg Pavliv

Reputation: 21192

Try something like this

(setq c-f12-winner-undo t)

(define-key (current-global-map) [C-f12]
  (lambda() 
    (interactive) 
    (if c-f12-winner-undo 
        (winner-undo)
      (delete-other-windows))
    (setq c-f12-winner-undo (not c-f12-winner-undo))))

Upvotes: 3

Related Questions