style:defaultalt 1alt 2

Mon, 20 Nov 2006 @ 00:23:09

rapid window toggle in emacs

Every so often I encounter an Emacs “quick reference” that lists the functionality of command other-window (C-x o) as toggling between two windows. In fact, this is incorrect as Emacs only implements cyclical window navigation. There is no single shortcut to toggle between two windows; only to cycle through a list of windows. If there are more than two windows in an Emacs session (I typically use four) then there is no simple reflexive key stroke by which the user may return to the previously visited window. To implement real window toggling, identical to the other command in GNU Screen, I use the following code:

;; Redefines "C-x o" to behave like 'other-window' while also
;; remembering the previously selected window.
(defun other-window-history  (&optional x)
  (interactive "P")
  (setq toggle-window-prev (selected-window))
  (if (equal x nil)
      (other-window '1)
    (other-window x)))
(global-set-key (kbd "C-x o") 'other-window-history)


;; By calling other-window-toggle, two windows can be rapidly switched
;; between with a single key sequence, definied below.
(defun other-window-toggle (&optional x)
  (interactive "P")
  (if (not (boundp 'toggle-window-prev))
      (if (equal x nil)
          (other-window-history 1)
        (other-window-history x))
    (let ( (y toggle-window-prev) )
      (setq toggle-window-prev (selected-window))
      (if (equal toggle-window-prev y)
          (if (equal x nil)
              (other-window-history 1)
            (other-window-history x))
        (select-window y)))))
(global-set-key (kbd "C-x C-o") 'other-window-toggle)
(global-set-key (kbd "C-c C-o") 'other-window-toggle)

The source also available at: conf-windows.el.

Other elisp code snippets available at: …/elisp-snippets/.


Posted by timotheus | Permanent link | Filed under: software | Comments (1)
 
© Copyright Timothy Stotts 2002-2009. All rights reserved. ^ top of page
validate:  xhtml  css  rss  atomcontact:  email  gtalk <<< index of blog