al-window.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; al-window.el --- Additional functionality for working with windows and frames
  2. ;; Copyright © 2013–2017 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. ;;; Make 2 windows
  17. ;;;###autoload
  18. (defun al/make-2-windows (&optional fun)
  19. "Make 2 windows in the current frame.
  20. FUN is a function for splitting
  21. windows (`split-window-vertically' by default)."
  22. (interactive)
  23. (or fun
  24. (setq fun 'split-window-below))
  25. (if (one-window-p)
  26. (funcall fun)
  27. (let ((cur-buffer (current-buffer)))
  28. (other-window -1)
  29. (delete-other-windows)
  30. (funcall fun)
  31. (switch-to-buffer cur-buffer))))
  32. ;;;###autoload
  33. (defalias 'al/make-vertical-windows 'al/make-2-windows
  34. "Make 2 vertical windows.
  35. If there is only one window, split it.
  36. If there are more windows, show current and previous buffer in new
  37. windows.")
  38. ;;;###autoload
  39. (defun al/make-horizontal-windows ()
  40. "Make 2 horizontal windows.
  41. If there is only one window, split it.
  42. If there are more windows, show current and previous buffer in new
  43. windows."
  44. (interactive)
  45. (al/make-2-windows 'split-window-right))
  46. ;;; Switching windows
  47. (defvar al/other-window-order 1)
  48. ;;;###autoload
  49. (defun al/other-window ()
  50. "Select the previously selected window."
  51. (interactive)
  52. (setq al/other-window-order (- al/other-window-order))
  53. (other-window al/other-window-order))
  54. ;;;###autoload
  55. (defun al/switch-windows ()
  56. "Switch current and previous windows (show switched buffers)."
  57. (interactive)
  58. (unless (one-window-p)
  59. (let ((cur-buffer (current-buffer)))
  60. (other-window -1)
  61. (switch-to-buffer cur-buffer)
  62. (other-window 1)
  63. (switch-to-buffer nil)
  64. (other-window -1))))
  65. ;;;###autoload
  66. (defun al/switch-or-next-window ()
  67. "Switch windows or select the next window.
  68. If there are only 2 windows in the current frame, switch them,
  69. otherwise select the next window."
  70. (interactive)
  71. (if (cddr (window-list)) ; > 2
  72. (other-window 1)
  73. (al/switch-windows)))
  74. ;;;###autoload
  75. (defun al/switch-to-minibuffer ()
  76. "Switch to minibuffer window."
  77. (interactive)
  78. (let ((mb (active-minibuffer-window)))
  79. (if mb
  80. (select-window mb)
  81. (error "Minibuffer is not active"))))
  82. ;;; Frames
  83. ;;;###autoload
  84. (defun al/maximize-frame (&optional current-frame)
  85. "Maximize active frame using 'wmctrl'.
  86. The variable CURRENT-FRAME affects nothing, it is used for
  87. `after-make-frame-functions' (for maximizing new frames)."
  88. (interactive)
  89. (shell-command "wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz"))
  90. ;; Update WINDOWS_NUM property for a stumpwm command, see
  91. ;; <https://github.com/alezost/stumpwm-config/blob/master/utils.lisp>.
  92. ;; Intended to be used with:
  93. ;; (add-hook 'window-configuration-change-hook 'al/set-windows-num-property)
  94. ;;;###autoload
  95. (defun al/set-windows-num-property ()
  96. "Set X window property WINDOWS_NUM to the current number of windows."
  97. (and (display-graphic-p)
  98. (x-change-window-property
  99. "WINDOWS_NUM"
  100. (string (length (window-list)))
  101. nil nil nil t)))
  102. (provide 'al-window)
  103. ;;; al-window.el ends here