dot/emacs, el/dot-emacs.el: Add new command for switching window configs.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 1 May 2022 12:37:54 +0000 (13:37 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 1 May 2022 13:12:21 +0000 (14:12 +0100)
Not sure if I'll like this new approach to working, but it seems OK so
far.

dot/emacs
el/dot-emacs.el

index d661617..3967646 100644 (file)
--- a/dot/emacs
+++ b/dot/emacs
   (global-set-key [?\C-c ?w ?k] 'windmove-up)
   (global-set-key [?\C-c ?w ?l] 'windmove-right)
   (global-set-key [?\C-c ?w ?r] 'winner-redo)
+  (global-set-key [?\C-c ?w ?s] 'mdw-switch-window-configuration)
   (global-set-key [?\C-c ?w ?u] 'winner-undo)
   (global-set-key [?\C-c ?w ?w] 'mdw-set-frame-width)
   (global-set-key [?\C-c ?w up] 'windmove-up)
index bc512b4..8b4f8f8 100644 (file)
@@ -287,6 +287,59 @@ This is sadly necessary because Emacs 26 is broken in this regard."
   (set-frame-parameter frame 'background-color (car colour))
   (set-frame-parameter frame 'foreground-color (cdr colour)))
 
+;; Window configuration switching.
+
+(defvar mdw-current-window-configuration nil
+  "The current window configuration register name, or `nil'.")
+
+(defun mdw-switch-window-configuration (register &optional no-save)
+  "Switch make REGISTER be the new current window configuration.
+If a current window configuration register is established, and
+NO-SAVE is nil, then save the current window configuration to
+that register first.
+
+Signal an error if the new register contains something other than
+a window configuration.  If the register is unset then save the
+current window configuration to it immediately.
+
+With one or three C-u, or an odd numeric prefix argument, set
+NO-SAVE, so the previous window configuration register is left
+unchanged.
+
+With two or three C-u, or a prefix argument which is an odd
+multiple of 2, just clear the record of the current window
+configuration register, so that the next switch doesn't save the
+prevailing configuration."
+  (interactive
+   (let ((arg current-prefix-arg))
+     (list (if (or (and (consp arg) (= (car arg) 16) (= (car arg) 64))
+                  (and (integerp arg) (not (zerop (logand arg 2)))))
+              nil
+            (register-read-with-preview "Switch to window configuration: "))
+          (or (and (consp arg) (= (car arg) 4) (= (car arg) 64))
+              (and (integerp arg) (not (zerop (logand arg 1))))))))
+
+  (let ((current-windows (list (current-window-configuration)
+                              (point-marker)))
+       (register-value (and register (get-register register))))
+    (when (and mdw-current-window-configuration (not no-save))
+      (set-register mdw-current-window-configuration current-windows))
+    (cond ((null register)
+          (setq mdw-current-window-configuration nil))
+         ((not (or (null register-value)
+                   (and (consp register-value)
+                        (window-configuration-p (car register-value))
+                        (integer-or-marker-p (cadr register-value))
+                        (null (caddr register-value)))))
+          (error "Register `%c' is not a window configuration" register))
+         (t
+          (cond ((null register-value)
+                 (set-register register current-windows))
+                (t
+                 (set-window-configuration (car register-value))
+                 (goto-char (cadr register-value))))
+          (setq mdw-current-window-configuration register)))))
+
 ;; Don't raise windows unless I say so.
 
 (defcustom mdw-inhibit-raise-frame nil