-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel--windows.el
184 lines (164 loc) · 5.53 KB
/
carousel--windows.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
;;; carousel--windows.el -*- lexical-binding: t; -*-
(eval-when-compile
(require 'carousel--macros)
(require 'ring)
(require 'persp-mode)
(require 'cl-lib)
(require 'evil)
(require 'dash)
)
(defun carousel-reset-columns (&optional arg)
(interactive "p")
(carousel-setup-columns arg t)
)
(defun carousel-setup-columns (&optional arg soft)
" Reset windows using `carousel-column-fn`
if SOFT then don't clear the window ring "
(interactive "pi")
(persp-delete-other-windows)
(mapc (-rpartial #'carousel-claim-window t) (funcall carousel-column-fn arg soft))
(unless soft
;; clear ring
(modify-persp-parameters `((carousel-actual . ,(make-ring 1))))
(carousel-add-current-buffer)
)
(when arg
(carousel-redisplay))
)
(defun carousel-setup-columns-default (&optional arg soft)
;; (arg == 1 -> one row) (else -> two rows, only use top)
;; Clear
(let ((leftmost (selected-window))
centre rightmost)
;; split
(when (and (numberp arg) (< 1 arg)) ;; Split as top 3 columns
(split-window-below))
;; Split as 3 columns
(setq centre (split-window-right))
(select-window centre)
(setq rightmost (split-window-right))
(when (and arg evil-auto-balance-windows)
(balance-windows))
(list leftmost centre rightmost)
)
)
(defun carousel-setup-vertical (&optional arg soft)
(let ((top (selected-window))
middle
bottom)
(setq middle (split-window-below))
(select-window middle)
(setq bottom (split-window-below))
(when (and arg evil-auto-balance-windows)
(balance-windows))
(list top middle bottom)
)
)
(defun carousel-kill-persp-fn (persp)
(with-other-carousel persp
(message "Killing Window Ring")
)
)
(defun carousel-claim-window (&optional wind force)
"Claim a window as one suitable for showing ring buffers. returns the window"
(interactive)
(let ((wind (or wind (selected-window))))
(when-carousel
(set-window-parameter wind 'carousel-claimed
(if force t
(not (window-parameter wind 'carousel-claimed))))
)
wind
)
)
(defun carousel-redisplay (&rest args)
(interactive)
(when (not (or (carousel-empty-p) carousel-pause-auto-redisplay))
(pcase carousel-focus-style
('balanced (carousel--redisplay-balanced))
('newest (carousel--redisplay-newest))
('oldest (carousel--redisplay-oldest))
(_ (message "Unknown redisplay style"))
)
)
(carousel-print-order)
)
(defun carousel--redisplay-balanced ()
(with-carousel
(let* ((largest (carousel--rough-mid (window-at-side-list nil carousel-selector)))
(ring-len (ring-length wr-actual))
(curr-win (window-next-sibling largest))
(index (carousel--newer ring-len wr-focus wr-loop))
)
(message "Windows: Largest: %s (%s) curr: %s (%s)" largest (window-live-p largest) curr-win (window-live-p curr-win))
(message "carousel-state: (%s) %s" ring-len wr-actual)
(set-window-buffer largest (carousel--get wr-actual wr-focus))
(while curr-win
(setq index (if (carousel-set-window curr-win index) (carousel--newer ring-len index wr-loop) index)
curr-win (window-next-sibling curr-win))
)
(setq index (carousel--older ring-len wr-focus wr-loop)
curr-win (window-prev-sibling largest))
(while curr-win
(setq index (if (carousel-set-window curr-win index) (carousel--older ring-len index wr-loop) index)
curr-win (window-prev-sibling curr-win))
)
)
)
)
(defun carousel--redisplay-newest ()
(with-carousel
(let* ((windows (reverse (window-at-side-list nil carousel-selector)))
(multi-windows (< 1 (length windows)))
(ring-len (ring-length wr-actual))
(focus wr-focus)
)
(cond ((and multi-windows (zerop focus))
(set-window-buffer (pop windows) (persp-parameter 'carousel-end)))
((and multi-windows (< 0 focus))
(setq focus (carousel--newer ring-len focus wr-loop)))
)
(while windows
(if (null focus)
(set-window-buffer (pop windows) (persp-parameter 'carousel-start))
(carousel-set-window (pop windows) focus)
)
(setq focus (carousel--older ring-len focus wr-loop))
)
)
)
)
(defun carousel--redisplay-oldest ()
(with-carousel
(let ((windows (window-at-side-list nil carousel-selector))
(ring-len (ring-length wr-actual))
(focus wr-focus)
)
(when (and wr-loop (eq focus ring-len))
(set-window-buffer (pop windows) (persp-parameter 'carousel-start)))
(while windows
(if (carousel-set-window (pop windows) focus)
(setq focus (carousel--newer ring-len focus wr-loop)))
)
)
)
)
(defun carousel-set-window (window index)
(with-carousel
(let ((buff (when index (carousel--get wr-actual index)))
)
(unless (and buff (buffer-live-p buff))
(ring-remove wr-actual index)
(ring-resize wr-actual (ring-length wr-actual))
(setq buff nil)
)
(set-window-buffer window (if buff buff wr-start))
(when (and index (fboundp #'solaire-mode))
(with-current-buffer (window-buffer window)
(solaire-mode (if (zerop (mod index 2)) 1 -1))
)
)
)
)
)
(provide 'carousel--windows)