This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwm.lisp
510 lines (454 loc) · 22.5 KB
/
dwm.lisp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
(in-package :stumpwm)
(defclass dynamic-group (tile-group)
((master-window :initarg :master-window :initform nil
:accessor dynamic-group-master-window)
(window-stack :initarg :window-stack :initform nil
:accessor dynamic-group-window-stack)
(superfluous-window :initform nil
:accessor moving-superfluous-window)))
(defclass dynamic-window (tile-window) ())
;;;;;;;;;;;;;
;;; Utils ;;;
;;;;;;;;;;;;;
(defparameter *maximum-dynamic-group-windows* 7
"the maximum number of windows that can be placed in a dynamic-group before we
attempt to place them in the next dynamic-group (or generate a new one)")
(defcommand gnew-dynamic (name) ((:rest "Group Name: "))
(unless name
(throw 'error :abort))
(add-group (current-screen) name :type 'dynamic-group))
(defcommand gnewbg-dynamic (name) ((:rest "Group Name: "))
(unless name
(throw 'error :abort))
(add-group (current-screen) name :type 'dynamic-group :background t))
(define-condition dynamic-group-too-many-windows (error) ())
(defun dyn-split-frame-in-dir (group dir &optional (ratio 1/2))
(let ((f (tile-group-current-frame group)))
(if (> (length (group-frames group)) *maximum-dynamic-group-windows*)
(progn (message "Too many windows in group")
(error 'dynamic-group-too-many-windows))
(if (or (split-frame group dir ratio))
(progn
(when (frame-window f)
(update-decoration (frame-window f)))
(show-frame-indicator group))
(error 'dynamic-group-too-many-windows)))))
(defun dyn-hsplit (&optional (ratio "1/2"))
(dyn-split-frame-in-dir (current-group) :column (read-from-string ratio)))
(defun dyn-vsplit (&optional (ratio "1/2"))
(dyn-split-frame-in-dir (current-group) :row (read-from-string ratio)))
;;; rework frame splitting to take a frame instead of generating one
(defun dyn-split-frame (group frame how &optional (ratio 1/2))
"Split the current frame into 2 frames. Return new frame number, if
it succeeded. NIL otherwise. RATIO is a fraction of the screen to
allocate to the new split window. If ratio is an integer then the
number of pixels will be used. This can be handy to setup the
desktop when starting."
(check-type how (member :row :column))
(let ((head (frame-head group frame)))
;; don't create frames smaller than the minimum size
(when (or (and (eq how :row)
(>= (frame-height frame) (* *min-frame-height* 2)))
(and (eq how :column)
(>= (frame-width frame) (* *min-frame-width* 2))))
(multiple-value-bind (f1 f2) (funcall (if (eq how :column)
'split-frame-h
'split-frame-v)
group frame ratio)
(setf (tile-group-frame-head group head)
(if (atom (tile-group-frame-head group head))
(list f1 f2)
(funcall-on-node (tile-group-frame-head group head)
(lambda (tree)
(substitute (list f1 f2) frame tree))
(lambda (tree)
(unless (atom tree)
(find frame tree))))))
(migrate-frame-windows group frame f1)
(choose-new-frame-window f2 group)
(if (eq (tile-group-current-frame group)
frame)
(setf (tile-group-current-frame group) f1))
(setf (tile-group-last-frame group) f2)
(sync-frame-windows group f1)
(sync-frame-windows group f2)
;; we also need to show the new window in the other frame
(when (frame-window f2)
(unhide-window (frame-window f2)))
(frame-number f2)))))
(defun dyn-split-frame-in-dir-with-frame (group frame dir &optional (ratio 1/2))
(if (> (length (group-frames group)) *maximum-dynamic-group-windows*)
(progn (message "Too many windows in group")
(error 'dynamic-group-too-many-windows))
(if (or (dyn-split-frame group frame dir ratio))
(progn
(when (frame-window frame)
(update-decoration (frame-window frame)))
(show-frame-indicator group))
(error 'dynamic-group-too-many-windows))))
(defun dyn-hsplit-frame (frame &optional (ratio "1/2"))
(dyn-split-frame-in-dir-with-frame (current-group) frame :column (read-from-string ratio)))
(defun dyn-vsplit-frame (frame &optional (ratio "1/2"))
(dyn-split-frame-in-dir-with-frame (current-group) frame :row (read-from-string ratio)))
(defun find-empty-frames (&optional (group (current-group)))
(loop for frame in (group-frames group)
when (null (frame-windows group frame))
collect frame))
(defun dyn-balance-stack-tree (&optional (group (current-group)))
(let ((tree (tile-group-frame-head group (current-head))))
(when (listp tree)
(balance-frames-internal group (cadr tree)))))
(defcommand dyn-balance () ()
(dyn-balance-stack-tree))
(defun dyn-remove-all-splits (&optional (group (current-group)))
(let* ((frame (tile-group-current-frame group))
(head (frame-head group frame))
(tree (tile-group-frame-head group head)))
(if (atom tree)
(message "Done removing")
(progn (remove-split)
(dyn-remove-all-splits)))))
;;; dynamic stack/master management a la dwm ;;;
(defun dyn-destroy-stack-window (group frame window)
(focus-frame group frame)
(remove-split)
(focus-frame group (frame-by-number group 0))
(dyn-balance-stack-tree group)
(setf (dynamic-group-window-stack group)
(remove window (dynamic-group-window-stack group))))
(defun dyn-pop-from-stack (group)
(let ((new-master (when (dynamic-group-window-stack group)
(pop (dynamic-group-window-stack group)))))
(if (not new-master)
(progn (dyn-remove-all-splits group)
(message "No more windows"))
(let ((new-master-windows-old-frame (window-frame new-master)))
(setf (dynamic-group-master-window group) new-master)
(focus-frame group new-master-windows-old-frame)
(remove-split)
(pull-window new-master (frame-by-number group 0))
(dyn-balance-stack-tree group)))))
(defun dyn-destroy-window (window)
(let ((frame (window-frame window))
(group (window-group window)))
(if (= (frame-number frame) 0) ; then its master frame
(dyn-pop-from-stack group)
(dyn-destroy-stack-window group frame window))))
(defun swap-window-with-main (group window-or-number)
(let* ((stack (dynamic-group-window-stack group))
(win (if (numberp window-or-number)
(member window-or-number stack :key 'window-number)
(member (window-number window-or-number) stack
:key 'window-number)))
(old-master (dynamic-group-master-window group)))
(if win
(progn
(exchange-windows (dynamic-group-master-window group) (car win))
(setf (dynamic-group-master-window group) (car win))
(setf (dynamic-group-window-stack group)
(remove (car win) (dynamic-group-window-stack group)))
(push old-master (dynamic-group-window-stack group))
(focus-all (car win)))
(message "Window not a member of the stack"))))
(defcommand dyn-switch-to-window (number) ((:number "Window Number: "))
(when number
(labels ((match (win)
(= (window-number win) number)))
(let ((win (find-if #'match (group-windows (current-group)))))
(if win
(swap-window-with-main (current-group) win)
(message "No window of number ~S" number))))))
(defun window-number-as-char (window)
(char (prin1-to-string (window-number window)) 0))
(defun draw-window-numbers (group)
"like draw-frame-numbers, but draws the window number of the frames window"
(let ((screen (group-screen group)))
(mapcar (lambda (frame)
(let* ((w (xlib:create-window
:parent (screen-root screen)
:x (frame-x frame) :y (frame-display-y group frame)
:width 1 :height 1
:background (screen-fg-color screen)
:border (screen-border-color screen)
:border-width 1
:event-mask '())))
(xlib:map-window w)
(setf (xlib:window-priority w) :above)
(echo-in-window w (screen-font screen)
(screen-fg-color screen)
(screen-bg-color screen)
(string
(window-number-as-char (frame-window frame))))
(xlib:display-finish-output *display*)
(dformat 3 "mapped ~S~%" (window-number (frame-window frame)))
w))
(group-frames group))))
(defun choose-window-by-number (group)
(let ((wins (progn (draw-frame-outlines group)
(draw-window-numbers group)))
winner num)
(unwind-protect
(multiple-value-bind (has-click ch x y)
(read-one-char-or-click group)
(cond (has-click
(dolist (f (group-frames group))
(when (and (> x (frame-x f)) (> y (frame-y f)))
(if winner
(when (or (> (frame-x f) (frame-x winner))
(> (frame-y f) (frame-y winner)))
(setf winner f))
(setf winner f))))
(ungrab-pointer)
(frame-window winner))
(ch
(setf num (read-from-string (string ch) nil nil))
(dformat 3 "read ~S ~S~%" ch num)
(find num (group-windows group) :test '= :key 'window-number))))
(mapc #'xlib:destroy-window wins)
(clear-frame-outlines group))))
(defun prompt-for-swap-window (group)
(let ((frame (choose-frame-by-number group)))
(when frame
(swap-window-with-main group (window-number (frame-window frame))))))
(defcommand dyn-switch () ()
(prompt-for-swap-window (current-group)))
(defcommand dyn-win-switch () ()
(let ((window (choose-window-by-number (current-group))))
(when window
(swap-window-with-main (current-group) window))))
(defcommand dyn-focus () ()
(if (equal (current-window) (dynamic-group-master-window (current-group)))
(message "Focused window is already master window")
(swap-window-with-main (current-group) (current-window))))
(defun dyn-cycle-windows (direction &optional (group (current-group)))
(check-type group dynamic-group)
(check-type direction (member :up :down))
(let* ((sorted (sort (group-windows group)
(if (eq direction :up) '< '>)
:key 'window-number))
(cycle-to (cadr (member (dynamic-group-master-window group) sorted))))
(when sorted
(swap-window-with-main group (window-number (if cycle-to
cycle-to
(car sorted)))))))
;;; TODO: figure out how to bind C-n/p to dyn-cycle up/down ONLY in dyn groups.
(define-stumpwm-type :up/down (input prompt)
(let* ((values '(("up" :up)
("down" :down)))
(string (argument-pop-or-read input prompt (mapcar 'first values)))
(dir (second (assoc string values :test 'string-equal))))
(or dir
(throw 'error "No matching direction."))))
(defcommand dyn-cycle (direction) ((:up/down "Enter up or down: "))
(dyn-cycle-windows direction))
;;;;;;;;;;;;;;;
;;; Keymaps ;;;
;;;;;;;;;;;;;;;
(defvar *dynamic-group-top-map* nil)
(defvar *dynamic-group-root-map* nil
"Commands specific to a dynamic group context hang from this keymap.
It is available as part of the @dnf{prefix map} when the active group
is a dynamic group.")
(fill-keymap *dynamic-group-top-map*
*escape-key* '*dynamic-group-root-map*)
(fill-keymap *dynamic-group-root-map*
(kbd "n") "dyn-cycle down"
(kbd "p") "dyn-cycle up"
(kbd "s") "dyn-win-switch"
(kbd "RET") "dyn-focus"
(kbd "f") "dyn-switch")
(pushnew '(dynamic-group *dynamic-group-top-map*) *group-top-maps*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Retile in Case of Emergency ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun dyn-emergency-retile ()
(let ((group (current-group)))
(when (typep group 'dynamic-group)
(let* ((master-window (dynamic-group-master-window group))
(other-windows (remove master-window (group-windows group))))
;; (dyn-remove-all-splits group)
(only)
(dyn-hsplit "2/3")
(focus-frame group (frame-by-number group 0))
(pull-window master-window (frame-by-number group 0))
(fnext)
(loop for n from 2 to (length other-windows)
do (dyn-vsplit))
(focus-frame group (frame-by-number group 0))
(dyn-balance-stack-tree group)))))
(defcommand dyn-retile () ()
(dyn-emergency-retile))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; pull overflow windows ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric pull-windows (from-group to-group)
(:documentation "Pull windows from FROM-GROUP to TO-GROUP. Number of windows pulled is dependent upon the type of TO-GROUP."))
(defmethod pull-windows (from-group (to-group tile-group))
(mapc (lambda (w)
(move-window-to-group w to-group))
(group-windows from-group)))
(defmethod pull-windows (from-group (to-group dynamic-group))
(let* ((winlist (group-windows from-group))
(num-wins-to-pull (abs (- *maximum-dynamic-group-windows*
(length (group-windows to-group)))))
(windows-to-pull (if (> (length winlist) num-wins-to-pull)
(subseq winlist 0 num-wins-to-pull)
winlist)))
(mapc (lambda (w)
(move-window-to-group w to-group))
windows-to-pull)))
(defun move-overflow-windows-to-group (group)
(when-let ((from (find-group (current-screen) ".Overflow")))
(pull-windows from group)))
(defun sync-all-frames (group)
(loop for frame in (group-frames group)
do (sync-frame-windows group frame)))
(defcommand dyn-get-overflow-windows () ()
(move-overflow-windows-to-group (current-group))
(sync-all-frames (current-group))
(focus-frame (current-group) (frame-by-number (current-group) 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; group-add-window method ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun open-dynamic-group-p (group)
(< (length (group-frames group)) *maximum-dynamic-group-windows*))
(defun find-open-dynamic-group (&optional (group (current-group))
(screen (current-screen)))
(let ((newgroup (loop for g in (screen-groups screen)
when (and (open-dynamic-group-p g) (not (equal group g)))
return g)))
(if newgroup
newgroup
(progn (gnewbg-dynamic (read-one-line screen "New Dynamic Group Name: "))
(find-open-dynamic-group)))))
(let ((counter 0))
(defun gen-group-name ()
(format nil ".Overflow group ~S" (incf counter))))
(defmethod group-add-window ((group dynamic-group) window &key frame raise &allow-other-keys)
(cond ((typep window 'float-window)
(call-next-method)
(message "Floating windows in dynamic-groups is currently not supported"))
((eq frame :float)
(change-class window 'float-window)
(float-window-align window)
(when raise (group-focus-window group window))
(message "Floating windows in dynamic-groups is currently not supported"))
(t
;; this is undefined behavior i think, but its done in the other
;; group-add-window method so i think its ok, at least on sbcl.
(change-class window 'dynamic-window)
(setf (window-frame window) (frame-by-number group 0))
(case (length (group-windows group))
(1
;; (message "moving from 0 to 1 window")
(setf (dynamic-group-master-window group) window))
(2
;; (message "moving from 1 to 2 windows")
(let ((frame (frame-by-number group 0)))
(dyn-hsplit-frame frame "2/3"))
(let* ((prev-win (dynamic-group-master-window group))
(prev-win-new-frame (car (remove (frame-by-number group 0)
(group-frames group)))))
(push prev-win (dynamic-group-window-stack group))
(setf (window-frame prev-win) prev-win-new-frame
(frame-window prev-win-new-frame) prev-win
(window-frame window) (frame-by-number group 0)
(frame-window (frame-by-number group 0)) window
(dynamic-group-master-window group) window)))
(otherwise
;; (message "We already have the stack set up, so put windows there!")
(let* ((master-frame (frame-by-number group 0))
(old-master (dynamic-group-master-window group))
(frames-no-master (remove master-frame (group-frames group)))
(frame-to-split (car frames-no-master)))
(handler-case
(progn
(dyn-vsplit-frame frame-to-split)
(push (dynamic-group-master-window group)
(dynamic-group-window-stack group))
(setf (dynamic-group-master-window group) window)
(setf (window-frame window) (frame-by-number group 0)
(window-frame old-master)
(or (car (find-empty-frames group))
(error "No Empty Frames in group ~S! Something has gone terribly wrong!" group))
(frame-window (window-frame old-master)) old-master)
(focus-frame group (frame-by-number group 0))
(dyn-balance-stack-tree group))
(dynamic-group-too-many-windows ()
(setf (moving-superfluous-window group) window)
(let ((new-group (gnew (gen-group-name))))
(move-window-to-group window new-group)))))))
(loop for frame in (group-frames group)
do (sync-frame-windows group frame))
(when (null (frame-window (window-frame window)))
(frame-raise-window (window-group window) (window-frame window)
window nil)))))
(defun dyn-find-superfluous-windows (group)
(let ((frames (group-frames group)))
(flatten
(loop for frame in frames
when (> (length (frame-windows group frame)) 1)
collect (remove (frame-window frame) (frame-windows group frame))))))
(defmethod group-delete-window ((group dynamic-group) (window dynamic-window))
(cond ((equal window (moving-superfluous-window group))
(setf (moving-superfluous-window group) nil))
((equal window (dynamic-group-master-window group))
;; (message "group-windows: ~S" (group-windows group))
(let* ((new-master (pop (dynamic-group-window-stack group))))
(if new-master
(let* ((new-masters-old-frame (window-frame new-master))
(master-frame (frame-by-number group 0))
(head (current-head group))
(tree (tile-group-frame-head group head)))
(cond
((not (dynamic-group-window-stack group)) ; we got 1 window left
(setf (window-frame new-master) master-frame
(frame-window master-frame) new-master
(dynamic-group-master-window group) new-master)
(loop for remframe in (remove (frame-by-number group 0)
(group-frames group))
do (setf (tile-group-frame-head group head)
(remove-frame tree remframe)))
(setf (tile-group-current-frame group) master-frame)
(focus-frame group master-frame)
(update-decoration (frame-window master-frame))
(loop for frame in (group-frames group)
do (sync-frame-windows group frame)))
(t ; we have a stack left and a new master window
(setf (tile-group-frame-head group head)
(remove-frame tree new-masters-old-frame)
(window-frame new-master) master-frame
(frame-window master-frame) new-master
(dynamic-group-master-window group) new-master)
(dyn-balance-stack-tree group)
(loop for frame in (group-frames group)
do (sync-frame-windows group frame))
(focus-frame group master-frame))))
(let ((f (window-frame window))) ; taken from tile-group/window
;; maybe pick a new window for the old frame
(when (eq (frame-window f) window)
(frame-raise-window group f
(first (frame-windows group f)) nil))))))
((member window (dynamic-group-window-stack group))
(let* ((new-stack (remove window (dynamic-group-window-stack group)))
(frame-to-remove (window-frame window))
(head (frame-head group frame-to-remove))
(tree (tile-group-frame-head group head)))
(setf (dynamic-group-window-stack group) new-stack)
(cond (new-stack
(setf (tile-group-frame-head group head)
(remove-frame tree frame-to-remove))
(tree-iterate tree (lambda (leaf)
(sync-frame-windows group leaf)))
(focus-frame group (frame-by-number group 0))
(dyn-balance-stack-tree group)
(loop for frame in (group-frames group)
do (sync-frame-windows group frame)))
(t
(setf (tile-group-frame-head group head)
(remove-frame tree frame-to-remove))
(focus-frame group (frame-by-number group 0))
(loop for frame in (group-frames group)
do (sync-frame-windows group frame)))))))
(loop for frame in (group-frames group)
do (sync-frame-windows group frame)))