Function: window-swap-states
window-swap-states is an interactive and byte-compiled function
defined in window.el.gz.
Signature
(window-swap-states &optional WINDOW-1 WINDOW-2 SIZE)
Documentation
Swap the states of live windows WINDOW-1 and WINDOW-2.
WINDOW-1 must specify a live window and defaults to the selected one. WINDOW-2 must specify a live window and defaults to the window following WINDOW-1 in the cyclic ordering of windows, excluding minibuffer windows and including live windows on all visible frames.
Optional argument SIZE non-nil means to try swapping the sizes of
WINDOW-1 and WINDOW-2 as well. A value of height means to swap
heights only, a value of width means to swap widths only, while
t means to swap both widths and heights, if possible. Frames are
not resized by this function.
Probably introduced at or before Emacs version 26.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/window.el.gz
(defun window-swap-states (&optional window-1 window-2 size)
"Swap the states of live windows WINDOW-1 and WINDOW-2.
WINDOW-1 must specify a live window and defaults to the selected
one. WINDOW-2 must specify a live window and defaults to the
window following WINDOW-1 in the cyclic ordering of windows,
excluding minibuffer windows and including live windows on all
visible frames.
Optional argument SIZE non-nil means to try swapping the sizes of
WINDOW-1 and WINDOW-2 as well. A value of `height' means to swap
heights only, a value of `width' means to swap widths only, while
t means to swap both widths and heights, if possible. Frames are
not resized by this function."
(interactive)
(setq window-1 (window-normalize-window window-1 t))
(if window-2
(unless (window-live-p window-2)
(error "%s is not a live window" window-2))
(setq window-2 (next-window window-1 'nomini 'visible)))
(unless (eq window-1 window-2)
(let* ((height (memq size '(t height)))
(width (memq size '(t width)))
(state-1 (window-state-get window-1))
(width-1 (and width (window-text-width window-1 t)))
(height-1 (and height (window-text-height window-1 t)))
(state-2 (window-state-get window-2))
(width-2 (and width (window-text-width window-2 t)))
(height-2 (and height (window-text-height window-2 t)))
old preserved)
;; Swap basic states.
(window-state-put state-1 window-2 t)
(window-state-put state-2 window-1 t)
;; Swap overlays with `window' property.
(with-current-buffer (window-buffer window-1)
(dolist (overlay (overlays-in (point-min) (point-max)))
(let ((window (overlay-get overlay 'window)))
(cond
((not window))
((eq window window-1)
(overlay-put overlay 'window window-2))
((eq window window-2)
(overlay-put overlay 'window window-1))))))
(unless (eq (window-buffer window-1) (window-buffer window-2))
(with-current-buffer (window-buffer window-2)
(dolist (overlay (overlays-in (point-min) (point-max)))
(let ((window (overlay-get overlay 'window)))
(cond
((not window))
((eq window window-1)
(overlay-put overlay 'window window-2))
((eq window window-2)
(overlay-put overlay 'window window-1)))))))
;; Try to swap window sizes.
(when size
(unless (= (setq old (window-text-width window-1 t)) width-2)
(window-resize-no-error window-1 (- width-2 old) t t t))
(unless (= (setq old (window-text-width window-2 t)) width-1)
(setq preserved (window-preserved-size window-1 t))
(window-preserve-size window-1 t t)
(window-resize-no-error window-2 (- width-1 old) t t t)
(window-preserve-size window-1 t preserved))
(unless (= (setq old (window-text-height window-1 t)) height-2)
(window-resize-no-error window-1 (- height-2 old) nil t t))
(unless (= (setq old (window-text-height window-2 t)) height-1)
(setq preserved (window-preserved-size window-1))
(window-preserve-size window-1 nil t)
(window-resize-no-error window-2 (- height-1 old) nil t t)
(window-preserve-size window-1 nil preserved))))))