2012/09/15: emacs: current-window-configuration and set-window-configuration

...are two simple, yet very useful functions. (current-window-configuration) returns an opaque value describing the current arrangement of buffers in the current frame. (set-window-configuration configuration) restores an arrangement of buffers; here configuration is a value previously obtained from (current-window-configuration).

One application is to maintain short cuts to named buffer configurations, in pretty much the same way you have the 26 named string buffers a to z in vi. These can be used to conveniently switch between your current chats, your calendar, the arrangements of source files useful for each of the projects your currently working on...

Following the idea of vi, I index these named configurations by a single character. This is the code I use.


;; Storing configurations

(defvar my-window-configurations '()
  "assoc list for saved window configurations")

(defun my-store-window-configuration (key)
  (interactive "c")
  (let ((lookup (assoc key my-window-configurations)))
    (if lookup
        (setq my-window-configurations (remove lookup my-window-configurations)))
    (setq my-window-configurations
          (cons (cons key (current-window-configuration))
                my-window-configurations))
    (message  "Stored configuration for key %c" key)))


(defun my-restore-window-configuration (key)
  (interactive "c")
  (let ((lookup (assoc key my-window-configurations)))
    (if lookup
        (set-window-configuration (cdr lookup)))))

(global-set-key (quote [33554452]) (quote my-store-window-configuration)) ;; C-S-t
(global-set-key "\C-t" 'my-restore-window-configuration)
; ... also in viper insert mode
(define-key viper-vi-basic-map "\C-t" 'my-restore-window-configuration)
(define-key viper-insert-basic-map "\C-t" 'my-restore-window-configuration)
download



Cross-referenced by: