-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
transpose-frame.el
233 lines (214 loc) · 10.3 KB
/
transpose-frame.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
;;; transpose-frame.el --- Transpose windows arrangement in a frame
;; Copyright (c) 2011 S. Irie
;; Author: S. Irie
;; Keywords: window
;; This program is free software.
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;; Commentary:
;; This program provides some interactive functions which allows users
;; to transpose windows arrangement in currently selected frame:
;;
;; `transpose-frame' ... Swap x-direction and y-direction
;;
;; +------------+------------+ +----------------+--------+
;; | | B | | A | |
;; | A +------------+ | | |
;; | | C | => +--------+-------+ D |
;; +------------+------------+ | B | C | |
;; | D | | | | |
;; +-------------------------+ +--------+-------+--------+
;;
;; `flip-frame' ... Flip vertically
;;
;; +------------+------------+ +------------+------------+
;; | | B | | D |
;; | A +------------+ +------------+------------+
;; | | C | => | | C |
;; +------------+------------+ | A +------------+
;; | D | | | B |
;; +-------------------------+ +------------+------------+
;;
;; `flop-frame' ... Flop horizontally
;;
;; +------------+------------+ +------------+------------+
;; | | B | | B | |
;; | A +------------+ +------------+ A |
;; | | C | => | C | |
;; +------------+------------+ +------------+------------+
;; | D | | D |
;; +-------------------------+ +-------------------------+
;;
;; `rotate-frame' ... Rotate 180 degrees
;;
;; +------------+------------+ +-------------------------+
;; | | B | | D |
;; | A +------------+ +------------+------------+
;; | | C | => | C | |
;; +------------+------------+ +------------+ A |
;; | D | | B | |
;; +-------------------------+ +------------+------------+
;;
;; `rotate-frame-clockwise' ... Rotate 90 degrees clockwise
;;
;; +------------+------------+ +-------+-----------------+
;; | | B | | | A |
;; | A +------------+ | | |
;; | | C | => | D +--------+--------+
;; +------------+------------+ | | B | C |
;; | D | | | | |
;; +-------------------------+ +-------+--------+--------+
;;
;; `rotate-frame-anticlockwise' ... Rotate 90 degrees anti-clockwise
;;
;; +------------+------------+ +--------+--------+-------+
;; | | B | | B | C | |
;; | A +------------+ | | | |
;; | | C | => +--------+--------+ D |
;; +------------+------------+ | A | |
;; | D | | | |
;; +-------------------------+ +-----------------+-------+
;;
;; This program is tested on GNU Emacs 22, 23.
;;; Code:
;;; Internal functions
(defun transpose-frame-get-arrangement (&optional frame subtree)
(let ((tree (or subtree
(car (window-tree frame)))))
(if (windowp tree)
(list (window-buffer tree)
(window-start tree)
(window-point tree)
(window-hscroll tree)
(window-margins tree)
(window-fringes tree)
(window-dedicated-p tree)
tree
(eq tree (frame-selected-window frame)))
(let* ((vertical (car tree))
(edges (cadr tree))
(length (float (if vertical
(- (nth 3 edges) (cadr edges))
(- (nth 2 edges) (car edges))))))
(cons vertical
(mapcar (lambda (subtree)
(cons (transpose-frame-get-arrangement frame subtree)
(/ (let ((edges (if (windowp subtree)
(window-edges subtree)
(cadr subtree))))
(if vertical
(- (nth 3 edges) (cadr edges))
(- (nth 2 edges) (car edges))))
length)))
(cddr tree)))))))
(defun transpose-frame-set-arrangement (config &optional window-or-frame &rest how)
(let ((window (if (windowp window-or-frame)
window-or-frame
(frame-selected-window window-or-frame))))
(unless (windowp window-or-frame)
(delete-other-windows window))
(if (bufferp (car config))
(let ((buffer (pop config)))
(set-window-buffer window buffer)
(set-window-start window (pop config))
(set-window-point window (pop config))
(set-window-hscroll window (pop config))
(set-window-margins window (caar config) (cdr (pop config)))
(apply 'set-window-fringes window (pop config))
(set-window-dedicated-p window (pop config))
(let* ((orig-window (pop config))
(ol-func (lambda (ol)
(when (eq (overlay-get ol 'window) orig-window)
(overlay-put ol 'window window))))
(ol-lists (with-current-buffer buffer
(overlay-lists))))
(mapc ol-func (car ol-lists))
(mapc ol-func (cdr ol-lists)))
(if (car config) (select-window window)))
(let* ((horizontal (if (memq 'transpose how)
(pop config)
(not (pop config))))
(edges (window-edges window))
(length (if horizontal
(- (nth 2 edges) (car edges))
(- (nth 3 edges) (cadr edges)))))
(if (memq (if horizontal 'flop 'flip) how)
(setq config (reverse config)))
(while (cdr config)
(setq window (prog1
(split-window window (round (* length (cdar config)))
horizontal)
(apply 'transpose-frame-set-arrangement
(caar config) window how))
config (cdr config)))
(apply 'transpose-frame-set-arrangement
(caar config) window how)))))
;;; User commands
;;;###autoload
(defun transpose-frame (&optional frame)
"Transpose windows arrangement at FRAME.
Omitting FRAME means currently selected frame."
(interactive)
(transpose-frame-set-arrangement (transpose-frame-get-arrangement frame) frame
'transpose)
(when (called-interactively-p 'any) (recenter)))
;;;###autoload
(defun flip-frame (&optional frame)
"Flip windows arrangement vertically at FRAME.
Omitting FRAME means currently selected frame."
(interactive)
(transpose-frame-set-arrangement (transpose-frame-get-arrangement frame) frame
'flip))
;;;###autoload
(defun flop-frame (&optional frame)
"Flop windows arrangement horizontally at FRAME.
Omitting FRAME means currently selected frame."
(interactive)
(transpose-frame-set-arrangement (transpose-frame-get-arrangement frame) frame
'flop))
;;;###autoload
(defun rotate-frame (&optional frame)
"Rotate windows arrangement 180 degrees at FRAME.
Omitting FRAME means currently selected frame."
(interactive)
(transpose-frame-set-arrangement (transpose-frame-get-arrangement frame) frame
'flip 'flop))
;;;###autoload
(defun rotate-frame-clockwise (&optional frame)
"Rotate windows arrangement 90 degrees clockwise at FRAME.
Omitting FRAME means currently selected frame."
(interactive)
(transpose-frame-set-arrangement (transpose-frame-get-arrangement frame) frame
'transpose 'flop)
(when (called-interactively-p 'any) (recenter)))
;;;###autoload
(defun rotate-frame-anticlockwise (&optional frame)
"Rotate windows arrangement 90 degrees anti-clockwise at FRAME.
Omitting FRAME means currently selected frame."
(interactive)
(transpose-frame-set-arrangement (transpose-frame-get-arrangement frame) frame
'transpose 'flip)
(when (called-interactively-p 'any) (recenter)))
;;; _
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
(provide 'transpose-frame)
;;; transpose-frame.el ends here