forked from stumpwm/stumpwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile-group.lisp
1317 lines (1173 loc) · 51.8 KB
/
tile-group.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Copyright (C) 2003-2008 Shawn Betts
;;
;; This file is part of stumpwm.
;;
;; stumpwm 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 2, or (at your option)
;; any later version.
;; stumpwm 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 software; see the file COPYING. If not, see
;; <http://www.gnu.org/licenses/>.
;; Commentary:
;;
;; Frame functions
;;
;; Code:
(in-package #:stumpwm)
(export '(save-frame-excursion))
(defclass tile-group (group)
((frame-tree :accessor tile-group-frame-tree)
(last-frame :initform nil :accessor tile-group-last-frame)
(current-frame :accessor tile-group-current-frame)))
(defmethod initialize-instance :after ((group tile-group) &key &allow-other-keys)
(let* ((heads (copy-heads (group-screen group))))
(setf (tile-group-frame-tree group) heads
(tile-group-current-frame group) (first heads))))
(defmethod group-startup ((group tile-group))
(let* ((window (first (group-tile-windows group)))
(frame (if (typep window 'tile-window)
(window-frame window)
(tile-group-current-frame group))))
(focus-frame group frame)))
(defmethod group-wake-up ((group tile-group))
(focus-frame group (tile-group-current-frame group))
;; doesn't get called by focus-frame
(show-frame-indicator group))
(defmethod group-delete-window ((group tile-group) (window tile-window))
(let ((f (window-frame 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))))
(defmethod group-delete-window ((group tile-group) (window float-window))
(let* ((windows (group-windows group))
(float-w (some (lambda (w) (when (typep w 'float-window) w))
windows))
(tile-w (some (lambda (w) (when (typep w 'tile-window) w))
windows)))
(cond (float-w (group-focus-window group float-w))
(tile-w (frame-raise-window group (window-frame tile-w) tile-w))
(t (no-focus group nil)))))
(defmethod group-add-window ((group tile-group) window &key frame raise &allow-other-keys)
;; This is important to get the frame slot
(if (typep window 'float-window)
(call-next-method)
(progn
(change-class window 'tile-window)
;; Try to put the window in the appropriate frame for the group.
(setf (window-frame window)
(or frame
(when *processing-existing-windows*
(find-frame group (xlib:drawable-x (window-parent window))
(xlib:drawable-y (window-parent window))))
(pick-preferred-frame window)))
(when *processing-existing-windows*
(setf (frame-window (window-frame window)) window))
(when (and frame raise)
(setf (tile-group-current-frame group) frame
(frame-window frame) nil))
(sync-frame-windows group (window-frame window))
(when (null (frame-window (window-frame window)))
(frame-raise-window (window-group window) (window-frame window)
window nil)))))
(defmethod group-current-head ((group tile-group))
(if-let ((current-window (group-current-window group)))
(window-head current-window)
(frame-head group (tile-group-current-frame group))))
(defmethod group-move-request ((group tile-group) (window tile-window) x y relative-to)
(when *honor-window-moves*
(dformat 3 "Window requested new position ~D,~D relative to ~S~%" x y relative-to)
(let* ((pos (if (eq relative-to :parent)
(list
(+ (xlib:drawable-x (window-parent window)) x)
(+ (xlib:drawable-y (window-parent window)) y))
(list x y)))
(frame (apply #'find-frame group pos)))
(when frame
(pull-window window frame)))))
(defmethod group-resize-request ((group tile-group) (window tile-window) width height)
;; it's important to grant the resize request first so that resize
;; increment hints use the proper base size to resize from.
(set-window-geometry window :width width :height height)
(maximize-window window))
(defmethod group-resize-request ((group tile-group) (window float-window) width height)
(float-window-move-resize window :width width :height height))
(defmethod group-move-request ((group tile-group) (window float-window) x y relative-to)
(declare (ignore relative-to))
(float-window-move-resize window :x x :y y))
(defmethod group-raise-request ((group tile-group) window stack-mode)
(when (window-in-current-group-p window)
(case stack-mode
(:map
(maybe-map-window window))
(:above
(maybe-raise-window window)))))
(defmethod group-lost-focus ((group tile-group))
;; If this window had the focus, try to avoid losing it.
(let ((frame (tile-group-current-frame group)))
(setf (frame-window frame)
(first (remove-if 'window-hidden-p (frame-windows group frame))))
(focus-frame group frame)))
(defmethod group-indicate-focus ((group tile-group))
(show-frame-indicator group))
(defmethod group-focus-window ((group tile-group) (win tile-window))
(frame-raise-window group (window-frame win) win))
(defmethod group-focus-window ((group tile-group) (window float-window))
(focus-window window))
(defmethod group-button-press ((group tile-group) x y (where (eql :root)))
(when *root-click-focuses-frame*
(let* ((frame (find-frame group x y)))
(when frame
(focus-frame group frame)
(unless (eq *mouse-focus-policy* :click)
(update-all-mode-lines))))))
(defmethod group-button-press ((group tile-group) x y (where window))
(declare (ignore x y))
(when (typep where 'float-window)
(call-next-method))
(when (eq *mouse-focus-policy* :click)
(focus-all where)
(update-all-mode-lines)))
(defmethod group-root-exposure ((group tile-group))
(show-frame-outline group nil))
(defmethod group-add-head ((group tile-group) head)
(let ((new-frame-num (find-free-frame-number group)))
(setf (tile-group-frame-tree group)
(insert-before (tile-group-frame-tree group)
(copy-frame head)
(head-number head)))
;; Try to put something in the new frame and give it an unused number
(let ((frame (tile-group-frame-head group head)))
(setf (frame-number frame) new-frame-num)
(choose-new-frame-window frame group)
(when (frame-window frame)
(unhide-window (frame-window frame))))))
;; TODO: This method has not been updated for floating windows
(defmethod group-remove-head ((group tile-group) head)
(let ((windows (head-windows group head)))
;; Remove it from the frame tree.
(setf (tile-group-frame-tree group) (delete (tile-group-frame-head group head) (tile-group-frame-tree group)))
;; Just set current frame to whatever.
(let ((frame (first (group-frames group))))
(setf (tile-group-current-frame group) frame
(tile-group-last-frame group) nil)
;; Hide its windows.
(dolist (window windows)
(hide-window window)
(setf (window-frame window) frame))))
;; Try to do something with the orphaned windows
(populate-frames group))
(defmethod group-resize-head ((group tile-group) oh nh)
(resize-tree (tile-group-frame-head group oh) (head-width nh) (head-height nh) (head-x nh) (head-y nh)))
(defmethod group-sync-all-heads ((group tile-group))
(sync-all-frame-windows group))
(defmethod group-sync-head ((group tile-group) head)
(dolist (f (head-frames group head))
(sync-frame-windows group f)))
;;;;;
;; (defun tile-group-frame-head (group head)
;; (let ((index (position head (group-heads group)))
;; (frame-tree (tile-group-frame-tree group)))
;; (when (> index (length frame-tree))
;; (elt frame-tree index))))
(defun group-tile-windows (group)
(only-tile-windows (group-windows group)))
(defun tile-group-frame-head (group head)
(group-sync-all-heads group)
(elt (tile-group-frame-tree group) (position head (group-heads group))))
(defun (setf tile-group-frame-head) (frame group head)
(group-sync-all-heads group)
(setf (elt (tile-group-frame-tree group) (position head (group-heads group))) frame))
(defun populate-frames (group)
"Try to fill empty frames in GROUP with hidden windows"
(dolist (f (group-frames group))
(unless (frame-window f)
(choose-new-frame-window f group)
(when (frame-window f)
(maximize-window (frame-window f))
(unhide-window (frame-window f))))))
(defun frame-by-number (group n)
(unless (eq n nil)
(find n (group-frames group)
:key 'frame-number
:test '=)))
(defun find-frame (group x y)
"Return the frame of GROUP containing the pixel at X Y"
(dolist (f (group-frames group))
(let* ((fy (frame-y f))
(fx (frame-x f))
(fwx (+ fx (frame-width f)))
(fhy (+ fy (frame-height f))))
(when (and
(>= y fy) (<= y fhy)
(>= x fx) (<= x fwx)
(return f))))))
(defun frame-set-x (frame v)
(decf (frame-width frame)
(- v (frame-x frame)))
(setf (frame-x frame) v))
(defun frame-set-y (frame v)
(decf (frame-height frame)
(- v (frame-y frame)))
(setf (frame-y frame) v))
(defun frame-set-r (frame v)
(setf (frame-width frame)
(- v (frame-x frame))))
(defun frame-set-b (frame v)
(setf (frame-height frame)
(- v (frame-y frame))))
(defun frame-r (frame)
(+ (frame-x frame) (frame-width frame)))
(defun frame-b (frame)
(+ (frame-y frame) (frame-height frame)))
(defun frame-display-y (group frame)
"Return a Y for frame that doesn't overlap the mode-line."
(let* ((head (frame-head group frame))
(ml (head-mode-line head))
(head-y (frame-y head))
(rel-frame-y (- (frame-y frame) head-y)))
(if (and ml (not (eq (mode-line-mode ml) :hidden)))
(case (mode-line-position ml)
(:top
(+ head-y
(+ (mode-line-height ml) (round (* rel-frame-y (mode-line-factor ml))))))
(:bottom
(+ head-y
(round (* rel-frame-y (mode-line-factor ml))))))
(frame-y frame))))
(defun frame-display-height (group frame)
"Return a HEIGHT for frame that doesn't overlap the mode-line."
(let* ((head (frame-head group frame))
(ml (head-mode-line head)))
(if (and ml (not (eq (mode-line-mode ml) :hidden)))
(round (* (frame-height frame) (mode-line-factor ml)))
(frame-height frame))))
(defun frame-intersect (f1 f2)
"Return a new frame representing (only) the intersection of F1 and F2. WIDTH and HEIGHT will be <= 0 if there is no overlap"
(let ((r (copy-frame f1)))
(when (> (frame-x f2) (frame-x f1))
(frame-set-x r (frame-x f2)))
(when (< (+ (frame-x f2) (frame-width f2))
(+ (frame-x f1) (frame-width f1)))
(frame-set-r r (frame-r f2)))
(when (> (frame-y f2) (frame-y f1))
(frame-set-y r (frame-y f2)))
(when (< (+ (frame-y f2) (frame-height f2))
(+ (frame-y f1) (frame-height f1)))
(frame-set-b r (frame-b f2)))
(values r)))
(defun frames-overlap-p (f1 f2)
"Returns T if frames F1 and F2 overlap at all"
(check-type f1 frame)
(check-type f2 frame)
(and (and (frame-p f1) (frame-p f2))
(let ((frame (frame-intersect f1 f2)))
(values (and (plusp (frame-width frame))
(plusp (frame-height frame)))))))
(defun frame-raise-window (g f w &optional (focus t))
"Raise the window w in frame f in group g. if FOCUS is
T (default) then also focus the frame."
(let ((oldw (frame-window f)))
;; nothing to do when W is nil
(setf (frame-window f) w)
(unless (and w (eq oldw w))
(if w
(raise-window w)
(mapc 'hide-window (frame-windows g f))))
;; If raising a window in the current frame we must focus it or
;; the group and screen will get out of sync.
(when (or focus
(eq (tile-group-current-frame g) f))
(focus-frame g f))
(when (and w (not (window-modal-p w)))
(raise-modals-of w))))
(defun focus-frame (group f)
(let ((w (frame-window f))
(last (tile-group-current-frame group))
(show-indicator nil))
(setf (tile-group-current-frame group) f)
;; record the last frame to be used in the fother command.
(unless (eq f last)
(setf (tile-group-last-frame group) last)
(run-hook-with-args *focus-frame-hook* f last)
(setf show-indicator t))
(if w
(focus-window w)
(no-focus group (frame-window last)))
(if show-indicator
(show-frame-indicator group)
(show-frame-outline group))))
(defun frame-windows (group f)
(remove-if-not (lambda (w) (eq (window-frame w) f))
(group-tile-windows group)))
(defun frame-sort-windows (group f)
(remove-if-not (lambda (w) (and (typep w 'tile-window)
(eq (window-frame w) f)))
(sort-windows group)))
(defun copy-frame-tree (tree)
"Return a copy of the frame tree."
(cond ((null tree) tree)
((typep tree 'frame)
(copy-structure tree))
(t
(mapcar #'copy-frame-tree tree))))
(defun group-frames (group)
(tree-accum-fn (tile-group-frame-tree group) 'nconc 'list))
(defun head-frames (group head)
(tree-accum-fn (tile-group-frame-head group head) 'nconc 'list))
(defun find-free-frame-number (group)
(find-free-number (mapcar 'frame-number (group-frames group))))
(defun choose-new-frame-window (frame group)
"Find out what window should go in a newly created frame."
(let ((win (case *new-frame-action*
(:last-window (other-hidden-window group))
(t nil))))
(setf (frame-window frame) win)
(when win
(setf (window-frame win) frame))))
(defun split-frame-h (group p ratio)
"Return 2 new frames. The first one stealing P's number and window"
(let* ((w (ratio-or-pixel (frame-width p) ratio))
(h (frame-height p))
(f1 (make-frame :number (frame-number p)
:x (frame-x p)
:y (frame-y p)
:width w
:height h
:window (frame-window p)))
(f2 (make-frame :number (find-free-frame-number group)
:x (+ (frame-x p) w)
:y (frame-y p)
;; gobble up the modulo
:width (- (frame-width p) w)
:height h
:window nil)))
(run-hook-with-args *split-frame-hook* p f1 f2)
(run-hook-with-args *new-frame-hook* f2)
(values f1 f2)))
(defun split-frame-v (group p ratio)
"Return 2 new frames. The first one stealing P's number and window"
(let* ((w (frame-width p))
(h (ratio-or-pixel (frame-height p) ratio))
(f1 (make-frame :number (frame-number p)
:x (frame-x p)
:y (frame-y p)
:width w
:height h
:window (frame-window p)))
(f2 (make-frame :number (find-free-frame-number group)
:x (frame-x p)
:y (+ (frame-y p) h)
:width w
;; gobble up the modulo
:height (- (frame-height p) h)
:window nil)))
(run-hook-with-args *split-frame-hook* p f1 f2)
(run-hook-with-args *new-frame-hook* f2)
(values f1 f2)))
(defun ratio-or-pixel (length ratio)
"Return a ratio of length unless ratio is an integer.
If ratio is an integer return the number of pixel desired."
(if (integerp ratio)
ratio
(truncate (* length ratio))))
(defun funcall-on-leaf (tree leaf fn)
"Return a new tree with LEAF replaced with the result of calling FN on LEAF."
(cond ((atom tree)
(if (eq leaf tree)
(funcall fn leaf)
tree))
(t (mapcar (lambda (sib)
(funcall-on-leaf sib leaf fn))
tree))))
(defun funcall-on-node (tree fn match)
"Call fn on the node where match returns t."
(if (funcall match tree)
(funcall fn tree)
(cond ((atom tree) tree)
(t (mapcar (lambda (sib)
(funcall-on-node sib fn match))
tree)))))
(defun replace-frame-in-tree (tree f &rest frames)
(funcall-on-leaf tree f (lambda (f)
(declare (ignore f))
frames)))
(defun sibling-internal (tree leaf fn)
"helper for next-sibling and prev-sibling."
(cond ((atom tree) nil)
((find leaf tree)
(let* ((rest (cdr (member leaf (funcall fn tree))))
(pick (car (if (null rest) (funcall fn tree) rest))))
(unless (eq pick leaf)
pick)))
(t (find-if (lambda (x)
(sibling-internal x leaf fn))
tree))))
(defun next-sibling (tree leaf)
"Return the sibling of LEAF in TREE."
(sibling-internal tree leaf 'identity))
(defun prev-sibling (tree leaf)
(sibling-internal tree leaf 'reverse))
(defun closest-sibling (tree leaf)
"Return the sibling to the right/below of leaf or left/above if
leaf is the most right/below of its siblings."
(let* ((parent (tree-parent tree leaf))
(lastp (= (position leaf parent) (1- (length parent)))))
(if lastp
(prev-sibling parent leaf)
(next-sibling parent leaf))))
(defun migrate-frame-windows (group src dest)
"Migrate all windows in SRC frame to DEST frame."
(mapc (lambda (w)
(when (eq (window-frame w) src)
(setf (window-frame w) dest)))
(group-tile-windows group)))
(defun tree-accum-fn (tree acc fn)
"Run an accumulator function on fn applied to each leaf"
(cond ((null tree) nil)
((atom tree)
(funcall fn tree))
(t (apply acc (mapcar (lambda (x) (tree-accum-fn x acc fn)) tree)))))
(defun tree-iterate (tree fn)
"Call FN on every leaf in TREE"
(cond ((null tree) nil)
((atom tree)
(funcall fn tree))
(t (mapc (lambda (x) (tree-iterate x fn)) tree))))
(defun tree-x (tree)
(tree-accum-fn tree 'min 'frame-x))
(defun tree-y (tree)
(tree-accum-fn tree 'min 'frame-y))
(defun tree-width (tree)
(cond ((atom tree) (frame-width tree))
((tree-row-split tree)
;; in row splits, all children have the same width, so use the
;; first one.
(tree-width (first tree)))
(t
;; for column splits we add the width of each child
(reduce '+ tree :key 'tree-width))))
(defun tree-height (tree)
(cond ((atom tree) (frame-height tree))
((tree-column-split tree)
;; in row splits, all children have the same width, so use the
;; first one.
(tree-height (first tree)))
(t
;; for column splits we add the width of each child
(reduce '+ tree :key 'tree-height))))
(defun tree-parent (top node)
"Return the list in TOP that contains NODE."
(cond ((atom top) nil)
((find node top) top)
(t (loop for i in top
thereis (tree-parent i node)))))
(defun tree-leaf (top)
"Return a leaf of the tree. Use this when you need a leaf but
you don't care which one."
(tree-accum-fn top
(lambda (&rest siblings)
(car siblings))
#'identity))
(defun tree-row-split (tree)
"Return t if the children of tree are stacked vertically"
(loop for i in (cdr tree)
with head = (car tree)
always (= (tree-x head) (tree-x i))))
(defun tree-column-split (tree)
"Return t if the children of tree are side-by-side"
(loop for i in (cdr tree)
with head = (car tree)
always (= (tree-y head) (tree-y i))))
(defun tree-split-type (tree)
"return :row or :column"
(cond ((tree-column-split tree) :column)
((tree-row-split tree) :row)
(t (error "tree-split-type unknown"))))
(defun offset-tree (tree x y)
"move the screen's frames around."
(tree-iterate tree (lambda (frame)
(incf (frame-x frame) x)
(incf (frame-y frame) y))))
(defun offset-tree-dir (tree amount dir)
(ecase dir
(:left (offset-tree tree (- amount) 0))
(:right (offset-tree tree amount 0))
(:top (offset-tree tree 0 (- amount)))
(:bottom (offset-tree tree 0 amount))))
(defun expand-tree (tree amount dir)
"expand the frames in tree by AMOUNT in DIR direction. DIR can be :top :bottom :left :right"
(labels ((expand-frame (f amount dir)
(ecase dir
(:left (decf (frame-x f) amount)
(incf (frame-width f) amount))
(:right (incf (frame-width f) amount))
(:top (decf (frame-y f) amount)
(incf (frame-height f) amount))
(:bottom (incf (frame-height f) amount)))))
(cond ((null tree) nil)
((atom tree)
(expand-frame tree amount dir))
((or (and (find dir '(:left :right))
(tree-row-split tree))
(and (find dir '(:top :bottom))
(tree-column-split tree)))
(dolist (i tree)
(expand-tree i amount dir)))
(t
(let* ((children (if (find dir '(:left :top))
(reverse tree)
tree))
(sz-fn (if (find dir '(:left :right))
'tree-width
'tree-height))
(total (funcall sz-fn tree))
(amt-list (loop for i in children
for old-sz = (funcall sz-fn i)
collect (floor (* amount old-sz) total)))
(remainder (- amount (apply '+ amt-list)))
(ofs 0))
;; spread the remainder out as evenly as possible
(assert (< remainder (length amt-list)))
(loop for i upfrom 0
while (> remainder 0)
do
(incf (nth i amt-list))
(decf remainder))
;; resize proportionally
(loop for i in children
for amt in amt-list
do
(expand-tree i amt dir)
(offset-tree-dir i ofs dir)
(incf ofs amt)))))))
(defun join-subtrees (tree leaf)
"expand the children of tree to occupy the space of
LEAF. Return tree with leaf removed."
(let* ((others (remove leaf tree))
(newtree (if (= (length others) 1)
(car others)
others))
(split-type (tree-split-type tree))
(dir (if (eq split-type :column) :right :bottom))
(ofsdir (if (eq split-type :column) :left :top))
(amt (if (eq split-type :column)
(tree-width leaf)
(tree-height leaf)))
(after (cdr (member leaf tree))))
;; align all children after the leaf with the edge of the
;; frame before leaf.
(offset-tree-dir after amt ofsdir)
(expand-tree newtree amt dir)
newtree))
(defun resize-tree (tree w h &optional (x (tree-x tree)) (y (tree-y tree)))
"Scale TREE to width W and height H, ignoring aspect. If X and Y are
provided, reposition the TREE as well."
(let* ((tw (tree-width tree))
(th (tree-height tree))
(tx (tree-x tree))
(ty (tree-y tree))
(wf (/ w tw))
(hf (/ h th)))
(tree-iterate tree (lambda (f)
(setf (frame-height f) (round (* (frame-height f) hf))
(frame-y f) (+ (round (* (- (frame-y f) ty) hf)) y)
(frame-width f) (round (* (frame-width f) wf))
(frame-x f) (+ (round (* (- (frame-x f) tx) wf)) x))))
(dformat 4 "resize-tree ~Dx~D -> ~Dx~D~%" tw th (tree-width tree) (tree-height tree))))
(defun remove-frame (tree leaf)
"Return a new tree with LEAF and it's sibling merged into
one."
(cond ((atom tree) tree)
((find leaf tree)
(join-subtrees tree leaf))
(t (mapcar (lambda (sib)
(remove-frame sib leaf))
tree))))
(defun sync-frame-windows (group frame)
"synchronize windows attached to FRAME."
(mapc (lambda (w)
(when (eq (window-frame w) frame)
(dformat 3 "maximizing ~S~%" w)
(maximize-window w)))
(group-tile-windows group)))
(defun sync-all-frame-windows (group)
"synchronize all frames in GROUP."
(let ((tree (tile-group-frame-tree group)))
(tree-iterate tree
(lambda (f)
(sync-frame-windows group f)))))
(defun sync-head-frame-windows (group head)
"synchronize all frames in GROUP and HEAD."
(dolist (f (head-frames group head))
(sync-frame-windows group f)))
(defun offset-frames (group x y)
"move the screen's frames around."
(let ((tree (tile-group-frame-tree group)))
(tree-iterate tree (lambda (frame)
(incf (frame-x frame) x)
(incf (frame-y frame) y)))))
(defun resize-frame (group frame amount dim)
"Resize FRAME by AMOUNT in DIM dimension, DIM can be
either :width or :height"
(check-type group group)
(check-type frame frame)
(check-type amount integer)
;; (check-type dim (member :width :height))
(labels ((max-amount (parent node min dim-fn)
(let ((right-sibling (cadr (member node parent)))
(left-sibling (cadr (member node (reverse parent)))))
(dformat 10 "max ~@{~a~^ ~}~%" parent node min dim-fn right-sibling left-sibling)
(if parent
(cond (right-sibling
(max 0 (- (funcall dim-fn right-sibling) min)))
(left-sibling
(max 0 (- (funcall dim-fn left-sibling) min)))
(t 0))
;; no parent means the frame can't get any bigger.
0))))
(let* ((tree (tile-group-frame-tree group))
(parent (tree-parent tree frame))
(gparent (tree-parent tree parent))
(split-type (tree-split-type parent)))
(dformat 10 "~s ~s parent: ~s ~s width: ~s h: ~s~%" dim amount split-type parent (tree-width parent) (tree-height parent))
;; normalize amount
(let* ((max (ecase dim
(:width
(if (>= (frame-width frame) (frame-width (frame-head group frame)))
0
(if (eq split-type :column)
(max-amount parent frame *min-frame-width* 'tree-width)
(max-amount gparent parent *min-frame-width* 'tree-width))))
(:height
(if (>= (frame-height frame) (frame-height (frame-head group frame)))
0
(if (eq split-type :row)
(max-amount parent frame *min-frame-height* 'tree-height)
(max-amount gparent parent *min-frame-height* 'tree-height))))))
(min (ecase dim
;; Frames taking up the entire HEAD in one
;; dimension can't be resized in that dimension.
(:width
(if (and (eq split-type :row)
(or (null gparent)
(>= (frame-width frame) (frame-width (frame-head group frame)))))
0
(- *min-frame-width* (frame-width frame))))
(:height
(if (and (eq split-type :column)
(or (null gparent)
(>= (frame-height frame) (frame-height (frame-head group frame)))))
0
(- *min-frame-height* (frame-height frame)))))))
(setf amount (max (min amount max) min))
(dformat 10 "bounds ~d ~d ~d~%" amount max min))
;; if FRAME is taking up the whole DIM or if AMOUNT = 0, do nothing
(unless (zerop amount)
(let* ((resize-parent (or (and (eq split-type :column)
(eq dim :height))
(and (eq split-type :row)
(eq dim :width))))
(to-resize (if resize-parent parent frame))
(to-resize-parent (if resize-parent gparent parent))
(lastp (= (position to-resize to-resize-parent) (1- (length to-resize-parent))))
(to-shrink (if lastp
(prev-sibling to-resize-parent to-resize)
(next-sibling to-resize-parent to-resize))))
(expand-tree to-resize amount (ecase dim
(:width (if lastp :left :right))
(:height (if lastp :top :bottom))))
(expand-tree to-shrink (- amount) (ecase dim
(:width (if lastp :right :left))
(:height (if lastp :bottom :top))))
(unless (and *resize-hides-windows* (eq *top-map* *resize-map*))
(tree-iterate to-resize
(lambda (leaf)
(sync-frame-windows group leaf)))
(tree-iterate to-shrink
(lambda (leaf)
(sync-frame-windows group leaf)))))))))
(defun balance-frames-internal (group tree)
"Fully balance all the frames contained in tree."
(labels
((balance (tree x y width height)
(etypecase tree
(frame (balance-frame tree x y width height))
(list (balance-tree tree x y width height))))
(balance-frame (frame x y width height)
(setf (frame-x frame) x
(frame-y frame) y
(frame-width frame) width
(frame-height frame) height)
(sync-frame-windows group frame))
(count-splits (tree split-type)
"Count the number of top-level splits of split-type in tree."
(cond ((frame-p tree) 1)
((eql split-type (tree-split-type tree))
(+ (count-splits (first tree) split-type)
(count-splits (second tree) split-type)))
(t 1)))
(divide-dimension (value first-splits second-splits)
"Divide a width or height between two sides of a binary tree.
Returns two values: the number of pixels to give to the first and
second child, respectively.
For example: (divide-dimension 500 3 2) will divide 500 pixels into 300
for the first 3 splits and 200 for the second 2 splits.
"
;; First divide the two groups as evenly as possible.
(multiple-value-bind (base remainder)
(truncate value (+ first-splits second-splits))
;; We may have up to TOTAL-1 pixels left. Divide those evenly
;; between the two sides. If there's 1 odd pixel left just give it
;; to the first side.
(multiple-value-bind (extra maybe-one-extra)
(truncate remainder 2)
(values (+ (* base first-splits) extra maybe-one-extra)
(+ (* base second-splits) extra)))))
(balance-tree (tree x y width height)
"Balance the binary tree to fit the given dimensions."
(let* ((split-type (tree-split-type tree))
(first-splits (count-splits (first tree) split-type))
(second-splits (count-splits (second tree) split-type)))
(ecase split-type
(:row (multiple-value-bind (top-height bottom-height)
(divide-dimension height first-splits second-splits)
(balance (first tree)
x y
width top-height)
(balance (second tree)
x (+ y top-height)
width bottom-height)))
(:column (multiple-value-bind (left-width right-width)
(divide-dimension width first-splits second-splits)
(balance (first tree)
x y
left-width height)
(balance (second tree)
(+ x left-width) y
right-width height)))))))
(balance tree (tree-x tree) (tree-y tree) (tree-width tree) (tree-height tree))))
(defun split-frame (group 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* ((frame (tile-group-current-frame group))
(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 draw-frame-outline (group f tl br)
"Draw an outline around FRAME."
(let* ((screen (group-screen group))
(win (if (frame-window f) (window-xwin (frame-window f)) (screen-root screen)))
(width (screen-frame-outline-width screen))
(gc (screen-frame-outline-gc screen))
(halfwidth (/ width 2)))
(when (> width 0)
(let ((x (frame-x f))
(y (frame-display-y group f))
(w (frame-width f))
(h (frame-display-height group f)))
(when tl
(xlib:draw-line win gc
x (+ halfwidth y) w 0 t)
(xlib:draw-line win gc
(+ halfwidth x) y 0 h t))
(when br
(xlib:draw-line win gc
(+ x (- w halfwidth)) y 0 h t)
(xlib:draw-line win gc
x (+ y (- h halfwidth)) w 0 t))))))
(defun draw-frame-outlines (group &optional head)
"Draw an outline around all frames in GROUP."
(clear-frame-outlines group)
(dolist (h (if head (list head) (group-heads group)))
(draw-frame-outline group h nil t)
(tree-iterate (tile-group-frame-head group h) (lambda (f)
(draw-frame-outline group f t nil)))))
(defun clear-frame-outlines (group)
"Clear the outlines drawn with DRAW-FRAME-OUTLINES."
(xlib:clear-area (screen-root (group-screen group))))
(defun draw-frame-numbers (group)
"Draw the number of each frame in its corner. Return the list of
windows used to draw the numbers in. The caller must destroy them."
(let ((screen (group-screen group)))
(mapcar (lambda (f)
(let ((w (xlib:create-window
:parent (screen-root screen)
:x (frame-x f) :y (frame-display-y group f) :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 (get-frame-number-translation f)))
(xlib:display-finish-output *display*)
(dformat 3 "mapped ~S~%" (frame-number f))
w))
(group-frames group))))
(defmacro save-frame-excursion (&body body)
"Execute body and then restore the current frame."
(let ((oframe (gensym "OFRAME"))
(ogroup (gensym "OGROUP")))
`(let ((,oframe (tile-group-current-frame (current-group)))
(,ogroup (current-group)))
(unwind-protect (progn ,@body)
(focus-frame ,ogroup ,oframe)))))
;;; Frame commands
(defun split-frame-in-dir (group dir &optional (ratio 1/2))
(let ((f (tile-group-current-frame group)))
(if (split-frame group dir ratio)
(progn
(when (frame-window f)
(update-decoration (frame-window f)))
(show-frame-indicator group))
(message "Cannot split smaller than minimum size."))))
(defcommand (hsplit tile-group) (&optional (ratio "1/2")) (:string)
"Split the current frame into 2 side-by-side frames."
(split-frame-in-dir (current-group) :column (read-from-string ratio)))
(defcommand (vsplit tile-group) (&optional (ratio "1/2")) (:string)
"Split the current frame into 2 frames, one on top of the other."
(split-frame-in-dir (current-group) :row (read-from-string ratio)))
(defun split-frame-eql-parts* (group dir amt)
(when (> amt 1)
(when-let ((new-frame (split-frame group dir (/ (- amt 1) amt))))
(cons new-frame (split-frame-eql-parts* group dir (- amt 1))))))
(defun split-frame-eql-parts (group dir amt)
"Splits frame in equal parts defined by amt."
(assert (> amt 1))
(let ((f (tile-group-current-frame group))
(new-frame-numbers (split-frame-eql-parts* group dir amt)))
(if (= (list-length new-frame-numbers) (- amt 1))
(progn
(when (frame-window f)
(update-decoration (frame-window f)))
(show-frame-indicator group))
(let ((head (frame-head group f)))
(setf (tile-group-frame-head group head)
(reduce (lambda (tree num)
(remove-frame tree
(frame-by-number group num)))
new-frame-numbers
:initial-value (tile-group-frame-head group head)))
(message "Cannot split. Maybe current frame is too small.")))))