Skip to content

Instantly share code, notes, and snippets.

@osmatsuda
Last active September 30, 2016 00:29
Show Gist options
  • Save osmatsuda/f8cf324a4a28aeffa1b54169d643a966 to your computer and use it in GitHub Desktop.
Save osmatsuda/f8cf324a4a28aeffa1b54169d643a966 to your computer and use it in GitHub Desktop.
(defun swap-window-buffers ()
(interactive)
(let (siblings
(child (window-child (window-parent)))
(src-win (selected-window))
(src-buf (window-buffer))
dest-win
dest-buf)
(message
(catch 'except
(if (or (eq (frame-root-window) src-win)
(not (window-parent)))
(throw 'except "Cannot swap!"))
(while child
(when (window-live-p child)
(setq siblings (cons child siblings)))
(setq child (window-next-sibling child)))
(if (and (> (length siblings) 1)
(eq (car siblings) src-win))
(setq dest-win (car (last siblings)))
(setq dest-win
(catch 'dest
(cl-mapl #'(lambda (tail)
(if (eq (car tail) src-win)
(throw 'dest (cadr tail))))
(reverse siblings))
nil)))
(if (not dest-win)
(throw 'except "Cannot swap!"))
(setq dest-buf (window-buffer dest-win))
(condition-case nil
(mapc #'(lambda (pair)
(set-window-buffer (car pair) (cdr pair) t))
(list (cons dest-win src-buf)
(cons src-win dest-buf)))
(error (throw 'except "Swap failed!")))
(select-window dest-win t)
(format "Swap window-buffers: %s <=> %s"
(buffer-name src-buf) (buffer-name dest-buf))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment