window-defuns.el 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. (defun window-toggle-split-direction ()
  2. "Switch window split from horizontally to vertically, or vice versa. i.e.
  3. change right window to bottom, or change bottom window to right."
  4. (interactive)
  5. (require 'windmove)
  6. (let ((done))
  7. (dolist (dirs '((right . down) (down . right)))
  8. (unless done
  9. (let* ((win (selected-window))
  10. (nextdir (car dirs))
  11. (neighbour-dir (cdr dirs))
  12. (next-win (windmove-find-other-window nextdir win))
  13. (neighbour1 (windmove-find-other-window neighbour-dir win))
  14. (neighbour2 (if next-win (with-selected-window next-win
  15. (windmove-find-other-window neighbour-dir next-win)))))
  16. ;;(message "win: %s\nnext-win: %s\nneighbour1: %s\nneighbour2:%s" win next-win neighbour1 neighbour2)
  17. (setq done (and (eq neighbour1 neighbour2)
  18. (not (eq (minibuffer-window) next-win))))
  19. (if done
  20. (let* ((other-buf (window-buffer next-win)))
  21. (delete-window next-win)
  22. (if (eq nextdir 'right)
  23. (split-window-vertically)
  24. (split-window-horizontally))
  25. (set-window-buffer (windmove-find-other-window neighbour-dir) other-buf))))))))
  26. (defun toggle-frame-split ()
  27. "If the frame is split vertically, split it horizontally or vice versa.
  28. Assumes that the frame is only split into two."
  29. (interactive)
  30. (unless (= (length (window-list)) 2) (error "Can only toggle a frame split in two"))
  31. (let ((split-vertically-p (window-combined-p)))
  32. (delete-window) ; closes current window
  33. (if split-vertically-p
  34. (split-window-horizontally)
  35. (split-window-vertically)) ; gives us a split with the other window twice
  36. (switch-to-buffer nil))) ; restore the original window in this part of the frame
  37. (defun toggle-window-split ()
  38. (interactive)
  39. (if (= (count-windows) 2)
  40. (let* ((this-win-buffer (window-buffer))
  41. (next-win-buffer (window-buffer (next-window)))
  42. (this-win-edges (window-edges (selected-window)))
  43. (next-win-edges (window-edges (next-window)))
  44. (this-win-2nd (not (and (<= (car this-win-edges)
  45. (car next-win-edges))
  46. (<= (cadr this-win-edges)
  47. (cadr next-win-edges)))))
  48. (splitter
  49. (if (= (car this-win-edges)
  50. (car (window-edges (next-window))))
  51. 'split-window-horizontally
  52. 'split-window-vertically)))
  53. (delete-other-windows)
  54. (let ((first-win (selected-window)))
  55. (funcall splitter)
  56. (if this-win-2nd (other-window 1))
  57. (set-window-buffer (selected-window) this-win-buffer)
  58. (set-window-buffer (next-window) next-win-buffer)
  59. (select-window first-win)
  60. (if this-win-2nd (other-window 1))))))
  61. (provide 'window-defuns)