forked from CeleritasCelery/rune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.el
10738 lines (9797 loc) · 444 KB
/
window.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
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
;;; window.el --- GNU Emacs window commands aside from those written in C -*- lexical-binding:t -*-
;; Copyright (C) 1985-2023 Free Software Foundation, Inc.
;; Maintainer: emacs-devel@gnu.org
;; Keywords: internal
;; Package: emacs
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Window tree functions.
;;; Code:
(defun internal--before-save-selected-window ()
(cons (selected-window)
;; We save and restore all frames' selected windows, because
;; `select-window' can change the frame-selected-window of
;; whatever frame that window is in. Each text terminal's
;; top-frame is preserved by putting it last in the list.
(apply #'append
(mapcar (lambda (terminal)
(let ((frames (frames-on-display-list terminal))
(top-frame (tty-top-frame terminal))
alist)
(if top-frame
(setq frames
(cons top-frame
(delq top-frame frames))))
(dolist (f frames)
(push (cons f (frame-selected-window f))
alist))
alist))
(terminal-list)))))
(defun internal--after-save-selected-window (state)
(dolist (elt (cdr state))
(and (frame-live-p (car elt))
(window-live-p (cdr elt))
(set-frame-selected-window (car elt) (cdr elt) 'norecord)))
(when (window-live-p (car state))
(select-window (car state) 'norecord)))
(defmacro save-selected-window (&rest body)
"Execute BODY, then select the previously selected window.
The value returned is the value of the last form in BODY.
This macro saves and restores the selected window, as well as the
selected window in each frame. If the previously selected window
is no longer live, then whatever window is selected at the end of
BODY remains selected. If the previously selected window of some
frame is no longer live at the end of BODY, that frame's selected
window is left alone.
This macro saves and restores the current buffer, since otherwise
its normal operation could make a different buffer current. The
order of recently selected windows and the buffer list ordering
are not altered by this macro (unless they are altered in BODY)."
(declare (indent 0) (debug t))
`(let ((save-selected-window--state (internal--before-save-selected-window)))
(save-current-buffer
(unwind-protect
(progn ,@body)
(internal--after-save-selected-window save-selected-window--state)))))
(defvar temp-buffer-window-setup-hook nil
"Normal hook run by `with-temp-buffer-window' before buffer display.
This hook is run by `with-temp-buffer-window' with the buffer to be
displayed current.")
(defvar temp-buffer-window-show-hook nil
"Normal hook run by `with-temp-buffer-window' after buffer display.
This hook is run by `with-temp-buffer-window' with the buffer
displayed and current and its window selected.")
(defun temp-buffer-window-setup (buffer-or-name)
"Set up temporary buffer specified by BUFFER-OR-NAME.
Return the buffer."
(let ((old-dir default-directory)
(buffer (get-buffer-create buffer-or-name)))
(with-current-buffer buffer
(kill-all-local-variables)
(setq default-directory old-dir)
(delete-all-overlays)
(setq buffer-read-only nil)
(setq buffer-file-name nil)
(setq buffer-undo-list t)
(let ((inhibit-read-only t)
(inhibit-modification-hooks t))
(erase-buffer)
(run-hooks 'temp-buffer-window-setup-hook))
;; Return the buffer.
buffer)))
;; Defined in help.el.
(defvar resize-temp-buffer-window-inhibit)
(defun temp-buffer-window-show (buffer &optional action)
"Show temporary buffer BUFFER in a window.
Return the window showing BUFFER. Pass ACTION as action argument
to `display-buffer'."
(let (resize-temp-buffer-window-inhibit window)
(with-current-buffer buffer
(set-buffer-modified-p nil)
(setq buffer-read-only t)
(goto-char (point-min))
(when (let ((window-combination-limit
;; When `window-combination-limit' equals
;; `temp-buffer' or `temp-buffer-resize' and
;; `temp-buffer-resize-mode' is enabled in this
;; buffer bind it to t so resizing steals space
;; preferably from the window that was split.
(if (or (eq window-combination-limit 'temp-buffer)
(and (eq window-combination-limit
'temp-buffer-resize)
temp-buffer-resize-mode))
t
window-combination-limit)))
(setq window (display-buffer buffer action)))
;; We used to raise the window's frame here. Do not do that
;; since it would override an `inhibit-switch-frame' entry
;; specified for the action alist used by `display-buffer'.
(setq minibuffer-scroll-window window)
(set-window-hscroll window 0)
(with-selected-window window
(run-hooks 'temp-buffer-window-show-hook)
(when temp-buffer-resize-mode
(resize-temp-buffer-window window)))
;; Return the window.
window))))
(defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
"Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
BUFFER-OR-NAME must specify either a live buffer, or the name of
a buffer (if it does not exist, this macro creates it).
Make the buffer specified by BUFFER-OR-NAME empty before running
BODY and bind `standard-output' to that buffer, so that output
generated with `prin1' and similar functions in BODY goes into
that buffer. Do not make that buffer current for running the
forms in BODY. Use `with-current-buffer-window' instead if you
need to run BODY with that buffer current.
At the end of BODY, mark the specified buffer unmodified and
read-only, and display it in a window (but do not select it).
The display happens by calling `display-buffer' passing it the
ACTION argument. If `temp-buffer-resize-mode' is enabled, the
corresponding window may be resized automatically.
Return the value returned by BODY, unless QUIT-FUNCTION specifies
a function. In that case, run that function with two arguments -
the window showing the specified buffer and the value returned by
BODY - and return the value returned by that function.
If the buffer is displayed on a new frame, the window manager may
decide to select that frame. In that case, it's usually a good
strategy if QUIT-FUNCTION selects the window showing the buffer
before reading any value from the minibuffer; for example, when
asking a `yes-or-no-p' question.
This runs the hook `temp-buffer-window-setup-hook' before BODY,
with the specified buffer temporarily current. It runs the hook
`temp-buffer-window-show-hook' after displaying the buffer, with
that buffer temporarily current, and the window that was used to
display it temporarily selected.
This construct is similar to `with-output-to-temp-buffer' but,
neither runs `temp-buffer-setup-hook' which usually puts the
buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
argument replaces this)."
(declare (debug t) (indent 3))
(let ((buffer (make-symbol "buffer"))
(window (make-symbol "window"))
(value (make-symbol "value")))
(macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
(vaction action)
(vquit-function quit-function))
`(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
(standard-output ,buffer)
,window ,value)
(setq ,value (progn ,@body))
(with-current-buffer ,buffer
(setq ,window (temp-buffer-window-show ,buffer ,vaction)))
(if (functionp ,vquit-function)
(funcall ,vquit-function ,window ,value)
,value)))))
(defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
"Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
This construct is like `with-temp-buffer-window' but unlike that,
makes the buffer specified by BUFFER-OR-NAME current for running
BODY."
(declare (debug t) (indent 3))
(let ((buffer (make-symbol "buffer"))
(window (make-symbol "window"))
(value (make-symbol "value")))
(macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
(vaction action)
(vquit-function quit-function))
`(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
(standard-output ,buffer)
,window ,value)
(with-current-buffer ,buffer
(setq ,value (progn ,@body))
(setq ,window (temp-buffer-window-show ,buffer ,vaction)))
(if (functionp ,vquit-function)
(funcall ,vquit-function ,window ,value)
,value)))))
(defmacro with-displayed-buffer-window (buffer-or-name action quit-function &rest body)
"Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
This construct is like `with-current-buffer-window' but unlike that,
displays the buffer specified by BUFFER-OR-NAME before running BODY."
(declare (debug t) (indent 3)
(obsolete "use `with-current-buffer-window' with action alist entry `body-function'."
"28.1"))
(let ((buffer (make-symbol "buffer"))
(window (make-symbol "window"))
(value (make-symbol "value")))
(macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
(vaction action)
(vquit-function quit-function))
`(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
(standard-output ,buffer)
;; If a 'window-height' entry specifies a function,
;; remember it here in order to call it below but replace
;; the entry so `window--try-to-split-window' will bind
;; `window-combination-limit' to t and the function does
;; not resize any other window but the one we split this
;; one off (Bug#25055, Bug#25179).
(vheight-function
(let ((window-height (assq 'window-height (cdr ,vaction))))
(when (functionp (cdr window-height))
(cdr window-height))))
(vaction-copied
(when vheight-function
(cons (car , vaction)
(cons
'(window-height . t)
(assq-delete-all
'window-height (cdr (copy-sequence ,vaction)))))))
,window ,value)
(with-current-buffer ,buffer
(setq ,window (temp-buffer-window-show
,buffer (or vaction-copied ,vaction))))
(let ((inhibit-read-only t)
(inhibit-modification-hooks t))
(setq ,value (progn ,@body)))
(set-window-point ,window (point-min))
(when vheight-function
(ignore-errors
(set-window-parameter ,window 'preserve-size nil)
(funcall vheight-function ,window)))
(when (consp (cdr (assq 'preserve-size (cdr ,vaction))))
(window-preserve-size
,window t (cadr (assq 'preserve-size (cdr ,vaction))))
(window-preserve-size
,window nil (cddr (assq 'preserve-size (cdr ,vaction)))))
(if (functionp ,vquit-function)
(funcall ,vquit-function ,window ,value)
,value)))))
(defmacro with-window-non-dedicated (window &rest body)
"Evaluate BODY with WINDOW temporarily made non-dedicated.
If WINDOW is nil, use the selected window. Return the value of
the last form in BODY."
(declare (indent 1) (debug t))
(let ((window-dedicated-sym (gensym))
(window-sym (gensym)))
`(let* ((,window-sym (window-normalize-window ,window t))
(,window-dedicated-sym (window-dedicated-p ,window-sym)))
(set-window-dedicated-p ,window-sym nil)
(unwind-protect
(progn ,@body)
;; `window-dedicated-p' returns the value set by
;; `set-window-dedicated-p', which differentiates non-nil and
;; t, so we cannot simply use t here. That's why we use
;; `window-dedicated-sym'.
(set-window-dedicated-p ,window-sym ,window-dedicated-sym)))))
;; The following two functions are like `window-next-sibling' and
;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
;; they don't substitute the selected window for nil), and they return
;; nil when WINDOW doesn't have a parent (like a frame's root window or
;; a minibuffer window).
(defun window-right (window)
"Return WINDOW's right sibling.
Return nil if WINDOW is the root window of its frame. WINDOW can
be any window."
(and window (window-parent window) (window-next-sibling window)))
(defun window-left (window)
"Return WINDOW's left sibling.
Return nil if WINDOW is the root window of its frame. WINDOW can
be any window."
(and window (window-parent window) (window-prev-sibling window)))
(defun window-child (window)
"Return WINDOW's first child window.
WINDOW can be any window."
(or (window-top-child window) (window-left-child window)))
(defun window-child-count (window)
"Return number of WINDOW's child windows.
WINDOW can be any window."
(let ((count 0))
(when (and (windowp window) (setq window (window-child window)))
(while window
(setq count (1+ count))
(setq window (window-next-sibling window))))
count))
(defun window-last-child (window)
"Return last child window of WINDOW.
WINDOW can be any window."
(when (and (windowp window) (setq window (window-child window)))
(while (window-next-sibling window)
(setq window (window-next-sibling window))))
window)
(defun window-normalize-buffer (buffer-or-name)
"Return buffer specified by BUFFER-OR-NAME.
BUFFER-OR-NAME must be a live buffer, a string naming a live
buffer or nil which means to return the current buffer.
This function is commonly used to process the (usually optional)
\"BUFFER-OR-NAME\" argument of window related functions where nil
stands for the current buffer."
(let ((buffer
(cond
((not buffer-or-name)
(current-buffer))
((bufferp buffer-or-name)
buffer-or-name)
((stringp buffer-or-name)
(get-buffer buffer-or-name))
(t
(error "No such buffer %s" buffer-or-name)))))
(if (buffer-live-p buffer)
buffer
(error "No such live buffer %s" buffer-or-name))))
(defun window-normalize-frame (frame)
"Return frame specified by FRAME.
FRAME must be a live frame or nil which means to return the
selected frame.
This function is commonly used to process the (usually optional)
\"FRAME\" argument of window and frame related functions where
nil stands for the selected frame."
(if frame
(if (frame-live-p frame)
frame
(error "%s is not a live frame" frame))
(selected-frame)))
(defun window-normalize-window (window &optional live-only)
"Return window specified by WINDOW.
If WINDOW is nil, return the selected window. Otherwise, if
WINDOW is a live or an internal window, return WINDOW; if
LIVE-ONLY is non-nil, return WINDOW for a live window only.
Otherwise, signal an error.
This function is commonly used to process the (usually optional)
\"WINDOW\" argument of window related functions where nil stands
for the selected window."
(cond
((null window)
(selected-window))
(live-only
(if (window-live-p window)
window
(error "%s is not a live window" window)))
((window-valid-p window)
window)
(t
(error "%s is not a valid window" window))))
;; Maybe this should go to frame.el.
(defun frame-char-size (&optional window-or-frame horizontal)
"Return the value of `frame-char-height' for WINDOW-OR-FRAME.
If WINDOW-OR-FRAME is a live frame, return the value of
`frame-char-height' for that frame. If WINDOW-OR-FRAME is a
valid window, return the value of `frame-char-height' for that
window's frame. In any other case, return the value of
`frame-char-height' for the selected frame.
Optional argument HORIZONTAL non-nil means to return the value of
`frame-char-width' for WINDOW-OR-FRAME."
(let ((frame
(cond
((window-valid-p window-or-frame)
(window-frame window-or-frame))
((frame-live-p window-or-frame)
window-or-frame)
(t (selected-frame)))))
(if horizontal
(frame-char-width frame)
(frame-char-height frame))))
(defvar ignore-window-parameters nil
"If non-nil, standard functions ignore window parameters.
The functions currently affected by this are `split-window',
`delete-window', `delete-other-windows' and `other-window'.
An application may bind this to a non-nil value around calls to
these functions to inhibit processing of window parameters.")
;; This must go to C, finally (or get removed).
(defconst window-safe-min-height 1
"The absolute minimum number of lines of any window.
Anything less might crash Emacs.")
(defun window-safe-min-pixel-height (&optional window)
"Return the absolute minimum pixel height of WINDOW."
(* window-safe-min-height
(frame-char-size (window-normalize-window window))))
(defcustom window-min-height 4
"The minimum total height, in lines, of any window.
The value has to accommodate one text line, a mode and header
line, a horizontal scroll bar and a bottom divider, if present.
A value less than `window-safe-min-height' is ignored. The value
of this variable is honored when windows are resized or split.
Applications should never rebind this variable. To resize a
window to a height less than the one specified here, an
application should instead call `window-resize' with a non-nil
IGNORE argument. In order to have `split-window' make a window
shorter, explicitly specify the SIZE argument of that function."
:type 'natnum
:version "24.1"
:group 'windows)
(defun window-min-pixel-height (&optional window)
"Return the minimum pixel height of window WINDOW."
(* (max (if (window-minibuffer-p window) 1 window-min-height)
window-safe-min-height)
(frame-char-size window)))
;; This must go to C, finally (or get removed).
(defconst window-safe-min-width 2
"The absolute minimum number of columns of a window.
Anything less might crash Emacs.")
(defun window-safe-min-pixel-width (&optional window)
"Return the absolute minimum pixel width of WINDOW."
(* window-safe-min-width
(frame-char-size (window-normalize-window window) t)))
(defcustom window-min-width 10
"The minimum total width, in columns, of any window.
The value has to accommodate two text columns as well as margins,
fringes, a scroll bar and a right divider, if present. A value
less than `window-safe-min-width' is ignored. The value of this
variable is honored when windows are resized or split.
Applications should never rebind this variable. To resize a
window to a width less than the one specified here, an
application should instead call `window-resize' with a non-nil
IGNORE argument. In order to have `split-window' make a window
narrower, explicitly specify the SIZE argument of that function."
:type 'natnum
:version "24.1"
:group 'windows)
(defun window-min-pixel-width (&optional window)
"Return the minimum pixel width of window WINDOW."
(* (max window-min-width window-safe-min-width)
(frame-char-size window t)))
(defun window-safe-min-pixel-size (&optional window horizontal)
"Return the absolute minimum pixel height of WINDOW.
Optional argument HORIZONTAL non-nil means return the absolute
minimum pixel width of WINDOW."
(if horizontal
(window-safe-min-pixel-width window)
(window-safe-min-pixel-height window)))
(defun window-min-pixel-size (&optional window horizontal)
"Return the minimum pixel height of WINDOW.
Optional argument HORIZONTAL non-nil means return the minimum
pixel width of WINDOW."
(if horizontal
(window-min-pixel-width window)
(window-min-pixel-height window)))
(defun window-combined-p (&optional window horizontal)
"Return non-nil if WINDOW has siblings in a given direction.
WINDOW must be a valid window and defaults to the selected one.
HORIZONTAL determines a direction for the window combination. If
HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
a vertical window combination. If HORIZONTAL is non-nil, return
non-nil if WINDOW is part of a horizontal window combination."
(setq window (window-normalize-window window))
(let ((parent (window-parent window)))
(and parent
(if horizontal
(window-left-child parent)
(window-top-child parent)))))
(defun window-combination-p (&optional window horizontal)
"Return WINDOW's first child if WINDOW is a vertical combination.
WINDOW can be any window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return WINDOW's first
child if WINDOW is a horizontal combination."
(setq window (window-normalize-window window))
(if horizontal
(window-left-child window)
(window-top-child window)))
(defun window-combinations (window &optional horizontal ignore-fixed)
"Return largest number of windows vertically arranged within WINDOW.
WINDOW must be a valid window and defaults to the selected one.
If HORIZONTAL is non-nil, return the largest number of
windows horizontally arranged within WINDOW.
Optional argument IGNORE-FIXED, if non-nil, means to ignore
fixed-size windows in the calculation."
(setq window (window-normalize-window window))
(cond
((window-live-p window)
;; If WINDOW is live, return 1.
1)
((if horizontal
(window-left-child window)
(window-top-child window))
;; If WINDOW is iso-combined, return the sum of the values for all
;; child windows of WINDOW.
(let ((child (window-child window))
(count 0))
(while child
(unless (and ignore-fixed (window-size-fixed-p child horizontal))
(setq count
(+ (window-combinations child horizontal ignore-fixed)
count)))
(setq child (window-right child)))
count))
(t
;; If WINDOW is not iso-combined, return the maximum value of any
;; child window of WINDOW.
(let ((child (window-child window))
(count 1))
(while child
(unless (and ignore-fixed (window-size-fixed-p child horizontal))
(setq count
(max (window-combinations child horizontal ignore-fixed)
count)))
(setq child (window-right child)))
count))))
(defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
"Helper function for `walk-window-tree' and `walk-window-subtree'."
(let (walk-window-tree-buffer)
(while walk-window-tree-window
(setq walk-window-tree-buffer
(window-buffer walk-window-tree-window))
(when (or walk-window-tree-buffer any)
(funcall fun walk-window-tree-window))
(unless walk-window-tree-buffer
(walk-window-tree-1
fun (window-left-child walk-window-tree-window) any)
(walk-window-tree-1
fun (window-top-child walk-window-tree-window) any))
(if sub-only
(setq walk-window-tree-window nil)
(setq walk-window-tree-window
(window-right walk-window-tree-window))))))
(defun walk-window-tree (fun &optional frame any minibuf)
"Run function FUN on each live window of FRAME.
FUN must be a function with one argument - a window. FRAME must
be a live frame and defaults to the selected one. ANY, if
non-nil, means to run FUN on all live and internal windows of
FRAME.
Optional argument MINIBUF t means run FUN on FRAME's minibuffer
window even if it isn't active. MINIBUF nil or omitted means run
FUN on FRAME's minibuffer window only if it's active. In either
case the minibuffer window must be part of FRAME. MINIBUF
neither nil nor t means never run FUN on the minibuffer window.
This function performs a pre-order, depth-first traversal of the
window tree. If FUN changes the window tree, the result is
unpredictable."
(let ((root (frame-root-window frame))
(mini (minibuffer-window frame)))
(setq frame (window-normalize-frame frame))
(unless (eq root mini)
(walk-window-tree-1 fun root any))
;; Run FUN on FRAME's minibuffer window if requested.
(when (and (window-live-p mini)
(eq (window-frame mini) frame)
(or (eq minibuf t)
(and (not minibuf)
(minibuffer-window-active-p mini))))
(funcall fun mini))))
(defun walk-window-subtree (fun &optional window any)
"Run function FUN on the subtree of windows rooted at WINDOW.
WINDOW defaults to the selected window. FUN must be a function
with one argument - a window. By default, run FUN only on live
windows of the subtree. If the optional argument ANY is non-nil,
run FUN on all live and internal windows of the subtree. If
WINDOW is live, run FUN on WINDOW only.
This function performs a pre-order, depth-first traversal of the
subtree rooted at WINDOW. If FUN changes that tree, the result
is unpredictable."
(setq window (window-normalize-window window))
(walk-window-tree-1 fun window any t))
(defun window-with-parameter (parameter &optional value frame any minibuf)
"Return first window on FRAME with PARAMETER non-nil.
FRAME defaults to the selected frame. Optional argument VALUE
non-nil means only return a window whose `window-parameter' value
for PARAMETER equals VALUE (comparison is done with `equal').
Optional argument ANY non-nil means consider internal windows
too.
Optional argument MINIBUF t means consider FRAME's minibuffer
window even if it isn't active. MINIBUF nil or omitted means
consider FRAME's minibuffer window only if it's active. In both
cases the minibuffer window must be part of FRAME. MINIBUF
neither nil nor t means never consider the minibuffer window."
(let (this-value)
(catch 'found
(walk-window-tree
(lambda (window)
(when (and (setq this-value (window-parameter window parameter))
(or (not value) (equal value this-value)))
(throw 'found window)))
frame any minibuf))))
;;; Atomic windows.
(defun window-atom-root (&optional window)
"Return root of atomic window WINDOW is a part of.
WINDOW must be a valid window and defaults to the selected one.
Return nil if WINDOW is not part of an atomic window."
(setq window (window-normalize-window window))
(let (root)
(while (and window (window-parameter window 'window-atom))
(setq root window)
(setq window (window-parent window)))
root))
(defun window-make-atom (window)
"Make WINDOW an atomic window.
WINDOW must be an internal window. Return WINDOW."
(if (not (window-child window))
(error "Window %s is not an internal window" window)
(walk-window-subtree
(lambda (window)
(unless (window-parameter window 'window-atom)
(set-window-parameter window 'window-atom t)))
window t)
window))
(defun display-buffer-in-atom-window (buffer alist)
"Display BUFFER in an atomic window.
This function displays BUFFER in a new window that will be
combined with an existing window to form an atomic window. If
the existing window is already part of an atomic window, add the
new window to that atomic window. Operations like `split-window'
or `delete-window', when applied to a constituent of an atomic
window, are applied atomically to the root of that atomic window.
ALIST is an association list of action symbols and values. See
Info node `(elisp) Buffer Display Action Alists' for details of
such alists. The following two symbols have a special meaning:
`window' specifies the existing window the new window shall be
combined with. Use `window-atom-root' to make the new window a
sibling of an atomic window's root. If an internal window is
specified here, all children of that window become part of the
atomic window too. If no window is specified, the new window
becomes a sibling of the selected window. By default, the
`window-atom' parameter of the existing window is set to `main'
provided the window is live and the parameter is not set yet.
`side' denotes the side of the existing window where the new
window shall be located. Valid values are `below', `right',
`above' and `left'. The default is `below'. By default, the
`window-atom' parameter of the new window is set to this value.
The return value is the new window, nil when creating that window
failed.
This is an action function for buffer display, see Info
node `(elisp) Buffer Display Action Functions'. It should be
called only by `display-buffer' or a function directly or
indirectly called by the latter."
(let* ((ignore-window-parameters t)
(window-combination-limit t)
(window-combination-resize 'atom)
(window (cdr (assq 'window alist)))
(side (or (cdr (assq 'side alist)) 'below))
(atom (when window (window-parameter window 'window-atom)))
root new)
(setq window (window-normalize-window window))
(setq root (window-atom-root window))
;; Split off new window.
(when (setq new (split-window-no-error window nil side))
(window-make-atom
(if (and root (not (eq root window)))
;; When WINDOW was part of an atomic window and we did not
;; split its root, root atomic window at old root.
root
;; Otherwise, root atomic window at WINDOW's new parent.
(window-parent window)))
;; Assign `window-atom' parameters, if needed.
(when (and (not atom) (window-live-p window))
(set-window-parameter window 'window-atom 'main))
(set-window-parameter new 'window-atom side)
;; Display BUFFER in NEW and return NEW.
(window--display-buffer buffer new 'window alist))))
(defun window--atom-check-1 (window)
"Subroutine of `window--atom-check'."
(when window
(if (window-parameter window 'window-atom)
(let ((count 0))
(when (or (catch 'reset
(walk-window-subtree
(lambda (window)
(if (window-parameter window 'window-atom)
(setq count (1+ count))
(throw 'reset t)))
window t))
;; count >= 1 must hold here. If there's no other
;; window around dissolve this atomic window.
(= count 1))
;; Dissolve atomic window.
(walk-window-subtree
(lambda (window)
(set-window-parameter window 'window-atom nil))
window t)))
;; Check children.
(unless (window-buffer window)
(window--atom-check-1 (window-left-child window))
(window--atom-check-1 (window-top-child window))))
;; Check right sibling
(window--atom-check-1 (window-right window))))
(defun window--atom-check (&optional frame)
"Check atomicity of all windows on FRAME.
FRAME defaults to the selected frame. If an atomic window is
wrongly configured, reset the atomicity of all its windows on
FRAME to nil. An atomic window is wrongly configured if it has
no child windows or one of its child windows is not atomic."
(window--atom-check-1 (frame-root-window frame)))
;; Side windows.
(defcustom window-sides-vertical nil
"If non-nil, left and right side windows occupy full frame height.
If nil, top and bottom side windows occupy full frame width."
:type 'boolean
:initialize 'custom-initialize-default
:set 'window--sides-verticalize
:group 'windows
:version "26.1")
(defcustom window-sides-reversed nil
"Whether top/bottom side windows appear in reverse order.
When this is nil, side windows on the top and bottom of a frame
are always drawn from left to right with increasing slot values.
When this is t, side windows on the top and bottom of a frame are
always drawn from right to left with increasing slot values.
When this is `bidi', the drawing order is like that for the value
t if the value of `bidi-paragraph-direction' is `right-to-left'
in the buffer most recently shown in the window selected within
the main window area of this frame.
The layout of side windows on the left or right of a frame is not
affected by the value of this variable."
:type
'(choice (const :tag "Never" nil)
(const :tag "Bidi" bidi)
(const :tag "Always" t))
:initialize 'custom-initialize-default
:set 'window--sides-reverse
:group 'windows
:version "26.1")
(defcustom window-sides-slots '(nil nil nil nil)
"Number of available side window slots on each side of a frame.
The value is a list of four elements specifying the maximum
number of side windows that may be created on the left, top,
right and bottom side of any frame.
If an element is a number, `display-buffer-in-side-window' will
refrain from making a new side window if the number of windows on
that side is equal to or exceeds that number. Rather, it will
reuse the window whose `window-slot' value is nearest to the slot
specified via its ALIST argument. If an element is nil, this
means there's no bound on the number of windows on that side."
:version "24.1"
:risky t
:type
'(list
:value (nil nil nil nil)
(choice
:tag "Left"
:help-echo "Maximum number of left side windows."
:value nil
:format "%[Left%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 2 :size 5))
(choice
:tag "Top"
:help-echo "Maximum number of top side windows."
:value nil
:format "%[Top%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 3 :size 5))
(choice
:tag "Right"
:help-echo "Maximum number of right side windows."
:value nil
:format "%[Right%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 2 :size 5))
(choice
:tag "Bottom"
:help-echo "Maximum number of bottom side windows."
:value nil
:format "%[Bottom%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 3 :size 5)))
:group 'windows)
(defvar-local window--sides-shown nil
"Non-nil if this buffer was shown in a side window once.
If this variable is non-nil in a buffer, `switch-to-prev-buffer'
and `switch-to-next-buffer' will refrain from showing this buffer
within the main window area. `display-buffer-in-side-window'
sets this variable automatically.
Killing buffer local variables after showing the buffer in a side
window annihilates any effect provided by this variable.")
(defvar window--sides-inhibit-check nil
"Non-nil means inhibit any checks on side windows.")
(defun window--sides-reverse-on-frame-p (frame)
"Return non-nil when side windows should appear reversed on FRAME.
This uses some heuristics to guess the user's intentions when the
selected window of FRAME is a side window."
(cond
;; Reverse when `window-sides-reversed' is t. Do not reverse when
;; `window-sides-reversed' is nil.
((memq window-sides-reversed '(nil t))
window-sides-reversed)
;; Reverse when FRAME's selected window shows a right-to-left buffer.
((let ((window (frame-selected-window frame)))
(when (and (not (window-parameter window 'window-side))
(or (not (window-minibuffer-p window))
(setq window (minibuffer-selected-window))))
(with-current-buffer (window-buffer window)
(eq bidi-paragraph-direction 'right-to-left)))))
;; Reverse when FRAME's `window-sides-main-selected-window' parameter
;; specifies a live window showing a right-to-left buffer.
((let ((window (frame-parameter
frame 'window-sides-main-selected-window)))
(when (window-live-p window)
(with-current-buffer (window-buffer window)
(eq bidi-paragraph-direction 'right-to-left)))))
;; Reverse when all windows in FRAME's main window show right-to-left
;; buffers.
(t
(catch 'found
(walk-window-subtree
(lambda (window)
(with-current-buffer (window-buffer window)
(when (eq bidi-paragraph-direction 'left-to-right)
(throw 'found nil))))
(window-main-window frame))
t))))
(defun window-main-window (&optional frame)
"Return the main window of specified FRAME.
The optional argument FRAME must be a live frame and defaults to
the selected one.
If FRAME has no side windows, return FRAME's root window.
Otherwise, return either an internal non-side window such that
all other non-side windows on FRAME descend from it, or the
single live non-side window of FRAME."
(let ((frame (window-normalize-frame frame))
main sibling)
;; Set main to the _last_ window found by `walk-window-tree' that
;; is not a side window but has a side window as its sibling.
(walk-window-tree
(lambda (window)
(and (not (window-parameter window 'window-side))
(or (and (setq sibling (window-prev-sibling window))
(window-parameter sibling 'window-side))
(and (setq sibling (window-next-sibling window))
(window-parameter sibling 'window-side)))
(setq main window)))
frame t 'nomini)
(or main (frame-root-window frame))))
(defun window--make-major-side-window-next-to (side)
"Return window to split for making a major side window.
SIDE must be one of the symbols `left', `top', `right' or
`bottom'.
This is an auxiliary function of `window--make-major-side-window'
and must not be called when a window on SIDE exists already."
(let ((root (frame-root-window))
(window--sides-inhibit-check t)
window)
;; (1) If a window on the opposite side exists, return that window's
;; sibling.
;; (2) If the new window shall span the entire side, return the
;; frame's root window.
;; (3) If a window on an orthogonal side exists, return that
;; window's sibling.
;; (4) Otherwise return the frame's root window.
(cond
((or (and (eq side 'left)
(setq window (window-with-parameter 'window-side 'right nil t)))
(and (eq side 'top)
(setq window (window-with-parameter 'window-side 'bottom nil t))))
(window-prev-sibling window))
((or (and (eq side 'right)
(setq window (window-with-parameter 'window-side 'left nil t)))
(and (eq side 'bottom)
(setq window (window-with-parameter 'window-side 'top nil t))))
(window-next-sibling window))
((memq side '(left right))
(cond
(window-sides-vertical
root)
((setq window (window-with-parameter 'window-side 'top nil t))
(window-next-sibling window))
((setq window (window-with-parameter 'window-side 'bottom nil t))
(window-prev-sibling window))
(t root)))
((memq side '(top bottom))
(cond
((not window-sides-vertical)
root)
((setq window (window-with-parameter 'window-side 'left nil t))
(window-next-sibling window))
((setq window (window-with-parameter 'window-side 'right nil t))
(window-prev-sibling window))
(t root))))))
(defun window--make-major-side-window (buffer side slot &optional alist)
"Display BUFFER in a new major side window on the selected frame.
SIDE must be one of `left', `top', `right' or `bottom'. SLOT
specifies the slot to use. ALIST is an association list of
symbols and values as passed to `display-buffer-in-side-window'.
Return the new window, nil if its creation failed.
This is an auxiliary function of `display-buffer-in-side-window'
and may be called only if no window on SIDE exists yet."
(let* ((left-or-right (memq side '(left right)))
(next-to (window--make-major-side-window-next-to side))
(on-side (cond
((eq side 'top) 'above)
((eq side 'bottom) 'below)
(t side)))
(window--sides-inhibit-check t)
;; The following two bindings will tell `split-window' to take
;; the space for the new window from the selected frame's main
;; window and not make a new parent window unless needed.
(window-combination-resize 'side)
(window-combination-limit nil)
(window (split-window-no-error next-to nil on-side))
(alist (if (assq 'dedicated alist)
alist
(cons `(dedicated . ,(or display-buffer-mark-dedicated 'side))
alist))))