This repository has been archived on 2022-12-11. You can view files and clone it, but cannot push or open issues or pull requests.
dot-files/.emacs.d/config/window-management.el

89 lines
3.6 KiB
EmacsLisp

;; (C) Copyright Collin J. Doering 2014
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; File: window-management.el
;; Author: Collin J. Doering <collin.doering@rekahsoft.ca>
;; Date: Oct 22, 2014
;; Use this function to create a X11 emacs frame with a static name so it can
;; be treated specially by xmonad (as a scratch-pad in this case). This is expected
;; to be run from command-line like so:
;; emacsclient --eval '(make-frame-with-static-name "emacs-scratch")'
(defun make-frame-with-static-name (given-name)
"Makes a (X11) frame with a unchanging name for the purposes of finding it with
a window manager, and treating it specially."
(select-frame (make-frame `((name . ,given-name)))))
;; Toggles windows split orientation of 2 adjecent windows
;; Thanks to http://www.emacswiki.org/cgi-bin/wiki?ToggleWindowSplit
(defun toggle-window-split ()
(interactive)
(if (= (count-windows) 2)
(let* ((this-win-buffer (window-buffer))
(next-win-buffer (window-buffer (next-window)))
(this-win-edges (window-edges (selected-window)))
(next-win-edges (window-edges (next-window)))
(this-win-2nd (not (and (<= (car this-win-edges)
(car next-win-edges))
(<= (cadr this-win-edges)
(cadr next-win-edges)))))
(splitter
(if (= (car this-win-edges)
(car (window-edges (next-window))))
'split-window-horizontally
'split-window-vertically)))
(delete-other-windows)
(let ((first-win (selected-window)))
(funcall splitter)
(if this-win-2nd (other-window 1))
(set-window-buffer (selected-window) this-win-buffer)
(set-window-buffer (next-window) next-win-buffer)
(select-window first-win)
(if this-win-2nd (other-window 1))))))
;; TODO: Modify the below function to accept the universal argument.
;; Specifically an integer argument (n) where:
;; - if negative denotes backwards rotation repeated |n| times
;; - if positive denotes forwards rotation repeated n times
;; - otherwise, no numerical value is given for the universal argument; ignore.
;; Rotates windows
;; Thanks to http://www.emacswiki.org/emacs/TransposeWindows
(defun rotate-windows ()
"Rotate your windows"
(interactive)
(cond
((not (> (count-windows) 1))
(message "You can't rotate a single window!"))
(t
(let ((i 0)
(num-windows (count-windows)))
(while (< i (- num-windows 1))
(let* ((w1 (elt (window-list) i))
(w2 (elt (window-list) (% (+ i 1) num-windows)))
(b1 (window-buffer w1))
(b2 (window-buffer w2))
(s1 (window-start w1))
(s2 (window-start w2)))
(set-window-buffer w1 b2)
(set-window-buffer w2 b1)
(set-window-start w1 s2)
(set-window-start w2 s1)
(setq i (1+ i))))))))
;; Assign keybinding to toggle split orientation of 2 adjacent windows, and to rotate windows
(define-key ctl-x-4-map "t" 'toggle-window-split)
(global-set-key "\C-cr" 'rotate-windows)