* alloc.c (DEADP): New macro, for porting to GNU/Hurd (Bug#9926).
[bpt/emacs.git] / lisp / window.el
CommitLineData
3c448ab6
MR
1;;; window.el --- GNU Emacs window commands aside from those written in C
2
73b0cd50 3;; Copyright (C) 1985, 1989, 1992-1994, 2000-2011
3689984f 4;; Free Software Foundation, Inc.
3c448ab6
MR
5
6;; Maintainer: FSF
7;; Keywords: internal
bd78fa1d 8;; Package: emacs
3c448ab6
MR
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;; Window tree functions.
28
29;;; Code:
30
31(eval-when-compile (require 'cl))
32
3c448ab6
MR
33(defmacro save-selected-window (&rest body)
34 "Execute BODY, then select the previously selected window.
35The value returned is the value of the last form in BODY.
36
37This macro saves and restores the selected window, as well as the
38selected window in each frame. If the previously selected window
39is no longer live, then whatever window is selected at the end of
40BODY remains selected. If the previously selected window of some
41frame is no longer live at the end of BODY, that frame's selected
42window is left alone.
43
44This macro saves and restores the current buffer, since otherwise
45its normal operation could make a different buffer current. The
46order of recently selected windows and the buffer list ordering
47are not altered by this macro (unless they are altered in BODY)."
f291fe60 48 (declare (indent 0) (debug t))
3c448ab6
MR
49 `(let ((save-selected-window-window (selected-window))
50 ;; It is necessary to save all of these, because calling
51 ;; select-window changes frame-selected-window for whatever
52 ;; frame that window is in.
53 (save-selected-window-alist
54 (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
55 (frame-list))))
56 (save-current-buffer
57 (unwind-protect
58 (progn ,@body)
59 (dolist (elt save-selected-window-alist)
60 (and (frame-live-p (car elt))
61 (window-live-p (cdr elt))
62 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
63 (when (window-live-p save-selected-window-window)
64 (select-window save-selected-window-window 'norecord))))))
65
d68443dc
MR
66;; The following two functions are like `window-next-sibling' and
67;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
68;; they don't substitute the selected window for nil), and they return
69;; nil when WINDOW doesn't have a parent (like a frame's root window or
70;; a minibuffer window).
85cc1f11
MR
71(defsubst window-right (window)
72 "Return WINDOW's right sibling.
73Return nil if WINDOW is the root window of its frame. WINDOW can
74be any window."
d68443dc 75 (and window (window-parent window) (window-next-sibling window)))
85cc1f11
MR
76
77(defsubst window-left (window)
78 "Return WINDOW's left sibling.
79Return nil if WINDOW is the root window of its frame. WINDOW can
80be any window."
d68443dc 81 (and window (window-parent window) (window-prev-sibling window)))
85cc1f11
MR
82
83(defsubst window-child (window)
84 "Return WINDOW's first child window."
d68443dc 85 (or (window-top-child window) (window-left-child window)))
85cc1f11
MR
86
87(defun window-child-count (window)
88 "Return number of WINDOW's child windows."
89 (let ((count 0))
90 (when (and (windowp window) (setq window (window-child window)))
91 (while window
92 (setq count (1+ count))
d68443dc 93 (setq window (window-next-sibling window))))
85cc1f11
MR
94 count))
95
96(defun window-last-child (window)
97 "Return last child window of WINDOW."
98 (when (and (windowp window) (setq window (window-child window)))
d68443dc
MR
99 (while (window-next-sibling window)
100 (setq window (window-next-sibling window))))
85cc1f11
MR
101 window)
102
24300f5f
CY
103(defsubst window-valid-p (object)
104 "Return t if OBJECT denotes a live window or internal window.
105Otherwise, return nil; this includes the case where OBJECT is a
106deleted window."
85cc1f11
MR
107 (and (windowp object)
108 (or (window-buffer object) (window-child object))
109 t))
110
5386012d 111(defsubst window-normalize-buffer (buffer-or-name)
85cc1f11
MR
112 "Return buffer specified by BUFFER-OR-NAME.
113BUFFER-OR-NAME must be either a buffer or a string naming a live
114buffer and defaults to the current buffer."
115 (cond
116 ((not buffer-or-name)
117 (current-buffer))
118 ((bufferp buffer-or-name)
119 (if (buffer-live-p buffer-or-name)
120 buffer-or-name
121 (error "Buffer %s is not a live buffer" buffer-or-name)))
122 ((get-buffer buffer-or-name))
123 (t
124 (error "No such buffer %s" buffer-or-name))))
125
5386012d 126(defsubst window-normalize-frame (frame)
85cc1f11
MR
127 "Return frame specified by FRAME.
128FRAME must be a live frame and defaults to the selected frame."
129 (if frame
130 (if (frame-live-p frame)
131 frame
132 (error "%s is not a live frame" frame))
133 (selected-frame)))
134
447f16b8 135(defsubst window-normalize-window (window &optional live-only)
85cc1f11 136 "Return window specified by WINDOW.
447f16b8
CY
137If WINDOW is nil, return `selected-window'.
138If WINDOW is a live window or internal window, return WINDOW;
139 if LIVE-ONLY is non-nil, return WINDOW for a live window only.
140Otherwise, signal an error."
141 (cond ((null window)
142 (selected-window))
143 (live-only
144 (if (window-live-p window)
145 window
146 (error "%s is not a live window" window)))
147 ((if (window-valid-p window)
148 window
149 (error "%s is not a window" window)))))
85cc1f11
MR
150
151(defvar ignore-window-parameters nil
152 "If non-nil, standard functions ignore window parameters.
153The functions currently affected by this are `split-window',
154`delete-window', `delete-other-windows' and `other-window'.
155
156An application may bind this to a non-nil value around calls to
157these functions to inhibit processing of window parameters.")
158
a1511caf 159(defconst window-safe-min-height 1
8da11505 160 "The absolute minimum number of lines of a window.
a1511caf
MR
161Anything less might crash Emacs.")
162
562dd5e9
MR
163(defcustom window-min-height 4
164 "The minimum number of lines of any window.
9173deec
JB
165The value has to accommodate a mode- or header-line if present.
166A value less than `window-safe-min-height' is ignored. The value
562dd5e9
MR
167of this variable is honored when windows are resized or split.
168
169Applications should never rebind this variable. To resize a
170window to a height less than the one specified here, an
d615d6d2 171application should instead call `window-resize' with a non-nil
562dd5e9
MR
172IGNORE argument. In order to have `split-window' make a window
173shorter, explictly specify the SIZE argument of that function."
174 :type 'integer
175 :version "24.1"
176 :group 'windows)
177
a1511caf 178(defconst window-safe-min-width 2
8da11505 179 "The absolute minimum number of columns of a window.
a1511caf
MR
180Anything less might crash Emacs.")
181
562dd5e9
MR
182(defcustom window-min-width 10
183 "The minimum number of columns of any window.
a91adc7e 184The value has to accommodate margins, fringes, or scrollbars if
562dd5e9
MR
185present. A value less than `window-safe-min-width' is ignored.
186The value of this variable is honored when windows are resized or
187split.
188
189Applications should never rebind this variable. To resize a
190window to a width less than the one specified here, an
d615d6d2 191application should instead call `window-resize' with a non-nil
562dd5e9
MR
192IGNORE argument. In order to have `split-window' make a window
193narrower, explictly specify the SIZE argument of that function."
194 :type 'integer
195 :version "24.1"
196 :group 'windows)
197
3d8daefe 198(defsubst window-combined-p (&optional window horizontal)
49745b39
CY
199 "Return non-nil if WINDOW has siblings in a given direction.
200If WINDOW is omitted or nil, it defaults to the selected window.
201
202HORIZONTAL determines a direction for the window combination.
203If HORIZONTAL is omitted or nil, return non-nil if WINDOW is part
204of a vertical window combination.
205If HORIZONTAL is non-nil, return non-nil if WINDOW is part of a
206horizontal window combination."
447f16b8 207 (setq window (window-normalize-window window))
85cc1f11 208 (let ((parent (window-parent window)))
49745b39
CY
209 (and parent
210 (if horizontal
211 (window-left-child parent)
212 (window-top-child parent)))))
85cc1f11 213
3d8daefe 214(defun window-combinations (&optional window horizontal)
85cc1f11 215 "Return largest number of vertically arranged subwindows of WINDOW.
49745b39
CY
216If WINDOW is omitted or nil, it defaults to the selected window.
217If HORIZONTAL is non-nil, return the largest number of
218horizontally arranged subwindows of WINDOW."
447f16b8 219 (setq window (window-normalize-window window))
85cc1f11
MR
220 (cond
221 ((window-live-p window)
222 ;; If WINDOW is live, return 1.
223 1)
49745b39
CY
224 ((if horizontal
225 (window-left-child window)
226 (window-top-child window))
85cc1f11
MR
227 ;; If WINDOW is iso-combined, return the sum of the values for all
228 ;; subwindows of WINDOW.
229 (let ((child (window-child window))
230 (count 0))
231 (while child
232 (setq count
3d8daefe 233 (+ (window-combinations child horizontal)
85cc1f11
MR
234 count))
235 (setq child (window-right child)))
236 count))
237 (t
238 ;; If WINDOW is not iso-combined, return the maximum value of any
239 ;; subwindow of WINDOW.
240 (let ((child (window-child window))
241 (count 1))
242 (while child
243 (setq count
3d8daefe 244 (max (window-combinations child horizontal)
85cc1f11
MR
245 count))
246 (setq child (window-right child)))
247 count))))
248
249(defun walk-window-tree-1 (proc walk-window-tree-window any &optional sub-only)
250 "Helper function for `walk-window-tree' and `walk-window-subtree'."
251 (let (walk-window-tree-buffer)
252 (while walk-window-tree-window
253 (setq walk-window-tree-buffer
254 (window-buffer walk-window-tree-window))
255 (when (or walk-window-tree-buffer any)
256 (funcall proc walk-window-tree-window))
257 (unless walk-window-tree-buffer
258 (walk-window-tree-1
d68443dc 259 proc (window-left-child walk-window-tree-window) any)
85cc1f11 260 (walk-window-tree-1
d68443dc 261 proc (window-top-child walk-window-tree-window) any))
85cc1f11
MR
262 (if sub-only
263 (setq walk-window-tree-window nil)
264 (setq walk-window-tree-window
265 (window-right walk-window-tree-window))))))
266
267(defun walk-window-tree (proc &optional frame any)
268 "Run function PROC on each live window of FRAME.
269PROC must be a function with one argument - a window. FRAME must
270be a live frame and defaults to the selected one. ANY, if
271non-nil means to run PROC on all live and internal windows of
272FRAME.
273
274This function performs a pre-order, depth-first traversal of the
275window tree. If PROC changes the window tree, the result is
276unpredictable."
5386012d 277 (let ((walk-window-tree-frame (window-normalize-frame frame)))
85cc1f11
MR
278 (walk-window-tree-1
279 proc (frame-root-window walk-window-tree-frame) any)))
280
281(defun walk-window-subtree (proc &optional window any)
282 "Run function PROC on each live subwindow of WINDOW.
283WINDOW defaults to the selected window. PROC must be a function
284with one argument - a window. ANY, if non-nil means to run PROC
285on all live and internal subwindows of WINDOW.
286
287This function performs a pre-order, depth-first traversal of the
288window tree rooted at WINDOW. If PROC changes that window tree,
289the result is unpredictable."
447f16b8 290 (setq window (window-normalize-window window))
85cc1f11
MR
291 (walk-window-tree-1 proc window any t))
292
293(defun windows-with-parameter (parameter &optional value frame any values)
294 "Return a list of all windows on FRAME with PARAMETER non-nil.
295FRAME defaults to the selected frame. Optional argument VALUE
296non-nil means only return windows whose window-parameter value of
297PARAMETER equals VALUE \(comparison is done using `equal').
298Optional argument ANY non-nil means consider internal windows
299too. Optional argument VALUES non-nil means return a list of cons
300cells whose car is the value of the parameter and whose cdr is
301the window."
302 (let (this-value windows)
303 (walk-window-tree
304 (lambda (window)
305 (when (and (setq this-value (window-parameter window parameter))
306 (or (not value) (or (equal value this-value))))
307 (setq windows
308 (if values
309 (cons (cons this-value window) windows)
310 (cons window windows)))))
311 frame any)
312
313 (nreverse windows)))
314
315(defun window-with-parameter (parameter &optional value frame any)
316 "Return first window on FRAME with PARAMETER non-nil.
317FRAME defaults to the selected frame. Optional argument VALUE
318non-nil means only return a window whose window-parameter value
319for PARAMETER equals VALUE \(comparison is done with `equal').
320Optional argument ANY non-nil means consider internal windows
321too."
322 (let (this-value windows)
323 (catch 'found
324 (walk-window-tree
325 (lambda (window)
326 (when (and (setq this-value (window-parameter window parameter))
327 (or (not value) (equal value this-value)))
328 (throw 'found window)))
329 frame any))))
330
331;;; Atomic windows.
332(defun window-atom-root (&optional window)
333 "Return root of atomic window WINDOW is a part of.
334WINDOW can be any window and defaults to the selected one.
335Return nil if WINDOW is not part of a atomic window."
447f16b8 336 (setq window (window-normalize-window window))
85cc1f11
MR
337 (let (root)
338 (while (and window (window-parameter window 'window-atom))
339 (setq root window)
340 (setq window (window-parent window)))
341 root))
342
5386012d 343(defun window-make-atom (window)
85cc1f11
MR
344 "Make WINDOW an atomic window.
345WINDOW must be an internal window. Return WINDOW."
346 (if (not (window-child window))
347 (error "Window %s is not an internal window" window)
348 (walk-window-subtree
349 (lambda (window)
350 (set-window-parameter window 'window-atom t))
351 window t)
352 window))
353
354(defun window-atom-check-1 (window)
355 "Subroutine of `window-atom-check'."
356 (when window
357 (if (window-parameter window 'window-atom)
358 (let ((count 0))
359 (when (or (catch 'reset
360 (walk-window-subtree
361 (lambda (window)
362 (if (window-parameter window 'window-atom)
363 (setq count (1+ count))
364 (throw 'reset t)))
365 window t))
366 ;; count >= 1 must hold here. If there's no other
367 ;; window around dissolve this atomic window.
368 (= count 1))
369 ;; Dissolve atomic window.
370 (walk-window-subtree
371 (lambda (window)
372 (set-window-parameter window 'window-atom nil))
373 window t)))
374 ;; Check children.
375 (unless (window-buffer window)
d68443dc
MR
376 (window-atom-check-1 (window-left-child window))
377 (window-atom-check-1 (window-top-child window))))
85cc1f11
MR
378 ;; Check right sibling
379 (window-atom-check-1 (window-right window))))
380
381(defun window-atom-check (&optional frame)
382 "Check atomicity of all windows on FRAME.
383FRAME defaults to the selected frame. If an atomic window is
384wrongly configured, reset the atomicity of all its subwindows to
385nil. An atomic window is wrongly configured if it has no
386subwindows or one of its subwindows is not atomic."
387 (window-atom-check-1 (frame-root-window frame)))
388
389;; Side windows.
390(defvar window-sides '(left top right bottom)
391 "Window sides.")
392
393(defcustom window-sides-vertical nil
394 "If non-nil, left and right side windows are full height.
395Otherwise, top and bottom side windows are full width."
396 :type 'boolean
397 :group 'windows
398 :version "24.1")
399
400(defcustom window-sides-slots '(nil nil nil nil)
401 "Maximum number of side window slots.
402The value is a list of four elements specifying the number of
403side window slots on \(in this order) the left, top, right and
404bottom side of each frame. If an element is a number, this means
405to display at most that many side windows on the corresponding
406side. If an element is nil, this means there's no bound on the
407number of slots on that side."
408 :risky t
409 :type
410 '(list
411 :value (nil nil nil nil)
412 (choice
413 :tag "Left"
414 :help-echo "Maximum slots of left side window."
415 :value nil
416 :format "%[Left%] %v\n"
417 (const :tag "Unlimited" :format "%t" nil)
418 (integer :tag "Number" :value 2 :size 5))
419 (choice
420 :tag "Top"
421 :help-echo "Maximum slots of top side window."
422 :value nil
423 :format "%[Top%] %v\n"
424 (const :tag "Unlimited" :format "%t" nil)
425 (integer :tag "Number" :value 3 :size 5))
426 (choice
427 :tag "Right"
428 :help-echo "Maximum slots of right side window."
429 :value nil
430 :format "%[Right%] %v\n"
431 (const :tag "Unlimited" :format "%t" nil)
432 (integer :tag "Number" :value 2 :size 5))
433 (choice
434 :tag "Bottom"
435 :help-echo "Maximum slots of bottom side window."
436 :value nil
437 :format "%[Bottom%] %v\n"
438 (const :tag "Unlimited" :format "%t" nil)
439 (integer :tag "Number" :value 3 :size 5)))
440 :group 'windows)
441
442(defun window-side-check (&optional frame)
443 "Check the window-side parameter of all windows on FRAME.
444FRAME defaults to the selected frame. If the configuration is
445invalid, reset all window-side parameters to nil.
446
447A valid configuration has to preserve the following invariant:
448
449- If a window has a non-nil window-side parameter, it must have a
450 parent window and the parent window's window-side parameter
451 must be either nil or the same as for window.
452
453- If windows with non-nil window-side parameters exist, there
454 must be at most one window of each side and non-side with a
455 parent whose window-side parameter is nil and there must be no
456 leaf window whose window-side parameter is nil."
457 (let (normal none left top right bottom
458 side parent parent-side code)
459 (when (or (catch 'reset
460 (walk-window-tree
461 (lambda (window)
462 (setq side (window-parameter window 'window-side))
463 (setq parent (window-parent window))
464 (setq parent-side
465 (and parent (window-parameter parent 'window-side)))
466 ;; The following `cond' seems a bit tedious, but I'd
467 ;; rather stick to using just the stack.
468 (cond
469 (parent-side
470 (when (not (eq parent-side side))
471 ;; A parent whose window-side is non-nil must
472 ;; have a child with the same window-side.
473 (throw 'reset t)))
474 ;; Now check that there's more than one main window
475 ;; for any of none, left, top, right and bottom.
476 ((eq side 'none)
477 (if none
478 (throw 'reset t)
479 (setq none t)))
480 ((eq side 'left)
481 (if left
482 (throw 'reset t)
483 (setq left t)))
484 ((eq side 'top)
485 (if top
486 (throw 'reset t)
487 (setq top t)))
488 ((eq side 'right)
489 (if right
490 (throw 'reset t)
491 (setq right t)))
492 ((eq side 'bottom)
493 (if bottom
494 (throw 'reset t)
495 (setq bottom t)))
496 ((window-buffer window)
497 ;; A leaf window without window-side parameter,
498 ;; record its existence.
499 (setq normal t))))
500 frame t))
501 (if none
502 ;; At least one non-side window exists, so there must
503 ;; be at least one side-window and no normal window.
504 (or (not (or left top right bottom)) normal)
505 ;; No non-side window exists, so there must be no side
506 ;; window either.
507 (or left top right bottom)))
508 (walk-window-tree
509 (lambda (window)
510 (set-window-parameter window 'window-side nil))
511 frame t))))
512
513(defun window-check (&optional frame)
514 "Check atomic and side windows on FRAME.
515FRAME defaults to the selected frame."
516 (window-side-check frame)
517 (window-atom-check frame))
518
519;;; Window sizes.
520(defvar window-size-fixed nil
521 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
522If the value is `height', then only the window's height is fixed.
523If the value is `width', then only the window's width is fixed.
524Any other non-nil value fixes both the width and the height.
525
526Emacs won't change the size of any window displaying that buffer,
527unless it has no other choice \(like when deleting a neighboring
528window).")
529(make-variable-buffer-local 'window-size-fixed)
530
a1511caf
MR
531(defsubst window-size-ignore (window ignore)
532 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
24300f5f 533 (if (window-valid-p ignore) (eq window ignore) ignore))
a1511caf
MR
534
535(defun window-min-size (&optional window horizontal ignore)
536 "Return the minimum number of lines of WINDOW.
537WINDOW can be an arbitrary window and defaults to the selected
538one. Optional argument HORIZONTAL non-nil means return the
539minimum number of columns of WINDOW.
540
541Optional argument IGNORE non-nil means ignore any restrictions
542imposed by fixed size windows, `window-min-height' or
543`window-min-width' settings. IGNORE equal `safe' means live
544windows may get as small as `window-safe-min-height' lines and
545`window-safe-min-width' columns. IGNORE a window means ignore
546restrictions for that window only."
547 (window-min-size-1
447f16b8 548 (window-normalize-window window) horizontal ignore))
a1511caf
MR
549
550(defun window-min-size-1 (window horizontal ignore)
551 "Internal function of `window-min-size'."
552 (let ((sub (window-child window)))
553 (if sub
554 (let ((value 0))
555 ;; WINDOW is an internal window.
3d8daefe 556 (if (window-combined-p sub horizontal)
a1511caf
MR
557 ;; The minimum size of an iso-combination is the sum of
558 ;; the minimum sizes of its subwindows.
559 (while sub
560 (setq value (+ value
561 (window-min-size-1 sub horizontal ignore)))
562 (setq sub (window-right sub)))
563 ;; The minimum size of an ortho-combination is the maximum of
564 ;; the minimum sizes of its subwindows.
565 (while sub
566 (setq value (max value
567 (window-min-size-1 sub horizontal ignore)))
568 (setq sub (window-right sub))))
569 value)
570 (with-current-buffer (window-buffer window)
571 (cond
572 ((and (not (window-size-ignore window ignore))
573 (window-size-fixed-p window horizontal))
574 ;; The minimum size of a fixed size window is its size.
575 (window-total-size window horizontal))
576 ((or (eq ignore 'safe) (eq ignore window))
577 ;; If IGNORE equals `safe' or WINDOW return the safe values.
578 (if horizontal window-safe-min-width window-safe-min-height))
579 (horizontal
580 ;; For the minimum width of a window take fringes and
581 ;; scroll-bars into account. This is questionable and should
582 ;; be removed as soon as we are able to split (and resize)
583 ;; windows such that the new (or resized) windows can get a
584 ;; size less than the user-specified `window-min-height' and
585 ;; `window-min-width'.
586 (let ((frame (window-frame window))
587 (fringes (window-fringes window))
588 (scroll-bars (window-scroll-bars window)))
589 (max
590 (+ window-safe-min-width
591 (ceiling (car fringes) (frame-char-width frame))
592 (ceiling (cadr fringes) (frame-char-width frame))
593 (cond
594 ((memq (nth 2 scroll-bars) '(left right))
595 (nth 1 scroll-bars))
596 ((memq (frame-parameter frame 'vertical-scroll-bars)
597 '(left right))
598 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
599 (frame-char-width)))
600 (t 0)))
601 (if (and (not (window-size-ignore window ignore))
602 (numberp window-min-width))
603 window-min-width
604 0))))
605 (t
606 ;; For the minimum height of a window take any mode- or
607 ;; header-line into account.
608 (max (+ window-safe-min-height
609 (if header-line-format 1 0)
610 (if mode-line-format 1 0))
611 (if (and (not (window-size-ignore window ignore))
612 (numberp window-min-height))
613 window-min-height
614 0))))))))
615
616(defun window-sizable (window delta &optional horizontal ignore)
617 "Return DELTA if DELTA lines can be added to WINDOW.
618Optional argument HORIZONTAL non-nil means return DELTA if DELTA
619columns can be added to WINDOW. A return value of zero means
620that no lines (or columns) can be added to WINDOW.
621
622This function looks only at WINDOW and its subwindows. The
623function `window-resizable' looks at other windows as well.
624
625DELTA positive means WINDOW shall be enlarged by DELTA lines or
626columns. If WINDOW cannot be enlarged by DELTA lines or columns
627return the maximum value in the range 0..DELTA by which WINDOW
628can be enlarged.
629
630DELTA negative means WINDOW shall be shrunk by -DELTA lines or
631columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
632return the minimum value in the range DELTA..0 by which WINDOW
633can be shrunk.
634
635Optional argument IGNORE non-nil means ignore any restrictions
636imposed by fixed size windows, `window-min-height' or
637`window-min-width' settings. IGNORE equal `safe' means live
638windows may get as small as `window-safe-min-height' lines and
639`window-safe-min-width' columns. IGNORE any window means ignore
640restrictions for that window only."
447f16b8 641 (setq window (window-normalize-window window))
a1511caf
MR
642 (cond
643 ((< delta 0)
644 (max (- (window-min-size window horizontal ignore)
645 (window-total-size window horizontal))
646 delta))
647 ((window-size-ignore window ignore)
648 delta)
649 ((> delta 0)
650 (if (window-size-fixed-p window horizontal)
651 0
652 delta))
653 (t 0)))
654
655(defsubst window-sizable-p (window delta &optional horizontal ignore)
656 "Return t if WINDOW can be resized by DELTA lines.
657For the meaning of the arguments of this function see the
658doc-string of `window-sizable'."
447f16b8 659 (setq window (window-normalize-window window))
a1511caf
MR
660 (if (> delta 0)
661 (>= (window-sizable window delta horizontal ignore) delta)
662 (<= (window-sizable window delta horizontal ignore) delta)))
663
664(defun window-size-fixed-1 (window horizontal)
665 "Internal function for `window-size-fixed-p'."
666 (let ((sub (window-child window)))
667 (catch 'fixed
668 (if sub
669 ;; WINDOW is an internal window.
3d8daefe 670 (if (window-combined-p sub horizontal)
a1511caf
MR
671 ;; An iso-combination is fixed size if all its subwindows
672 ;; are fixed-size.
673 (progn
674 (while sub
675 (unless (window-size-fixed-1 sub horizontal)
676 ;; We found a non-fixed-size subwindow, so WINDOW's
677 ;; size is not fixed.
678 (throw 'fixed nil))
679 (setq sub (window-right sub)))
680 ;; All subwindows are fixed-size, so WINDOW's size is
681 ;; fixed.
682 (throw 'fixed t))
683 ;; An ortho-combination is fixed-size if at least one of its
684 ;; subwindows is fixed-size.
685 (while sub
686 (when (window-size-fixed-1 sub horizontal)
687 ;; We found a fixed-size subwindow, so WINDOW's size is
688 ;; fixed.
689 (throw 'fixed t))
690 (setq sub (window-right sub))))
691 ;; WINDOW is a live window.
692 (with-current-buffer (window-buffer window)
693 (if horizontal
694 (memq window-size-fixed '(width t))
695 (memq window-size-fixed '(height t))))))))
696
697(defun window-size-fixed-p (&optional window horizontal)
698 "Return non-nil if WINDOW's height is fixed.
699WINDOW can be an arbitrary window and defaults to the selected
700window. Optional argument HORIZONTAL non-nil means return
701non-nil if WINDOW's width is fixed.
702
703If this function returns nil, this does not necessarily mean that
704WINDOW can be resized in the desired direction. The functions
705`window-resizable' and `window-resizable-p' will tell that."
706 (window-size-fixed-1
447f16b8 707 (window-normalize-window window) horizontal))
a1511caf
MR
708
709(defun window-min-delta-1 (window delta &optional horizontal ignore trail noup)
710 "Internal function for `window-min-delta'."
711 (if (not (window-parent window))
712 ;; If we can't go up, return zero.
713 0
714 ;; Else try to find a non-fixed-size sibling of WINDOW.
715 (let* ((parent (window-parent window))
716 (sub (window-child parent)))
717 (catch 'done
3d8daefe 718 (if (window-combined-p sub horizontal)
a1511caf
MR
719 ;; In an iso-combination throw DELTA if we find at least one
720 ;; subwindow and that subwindow is either not of fixed-size
721 ;; or we can ignore fixed-sizeness.
722 (let ((skip (eq trail 'after)))
723 (while sub
724 (cond
725 ((eq sub window)
726 (setq skip (eq trail 'before)))
727 (skip)
728 ((and (not (window-size-ignore window ignore))
729 (window-size-fixed-p sub horizontal)))
730 (t
731 ;; We found a non-fixed-size subwindow.
732 (throw 'done delta)))
733 (setq sub (window-right sub))))
734 ;; In an ortho-combination set DELTA to the minimum value by
735 ;; which other subwindows can shrink.
736 (while sub
737 (unless (eq sub window)
738 (setq delta
739 (min delta
740 (- (window-total-size sub horizontal)
741 (window-min-size sub horizontal ignore)))))
742 (setq sub (window-right sub))))
743 (if noup
744 delta
745 (window-min-delta-1 parent delta horizontal ignore trail))))))
746
747(defun window-min-delta (&optional window horizontal ignore trail noup nodown)
748 "Return number of lines by which WINDOW can be shrunk.
749WINDOW can be an arbitrary window and defaults to the selected
750window. Return zero if WINDOW cannot be shrunk.
751
752Optional argument HORIZONTAL non-nil means return number of
753columns by which WINDOW can be shrunk.
754
755Optional argument IGNORE non-nil means ignore any restrictions
756imposed by fixed size windows, `window-min-height' or
757`window-min-width' settings. IGNORE a window means ignore
758restrictions for that window only. IGNORE equal `safe' means
759live windows may get as small as `window-safe-min-height' lines
760and `window-safe-min-width' columns.
761
762Optional argument TRAIL `before' means only windows to the left
763of or above WINDOW can be enlarged. Optional argument TRAIL
764`after' means only windows to the right of or below WINDOW can be
765enlarged.
766
767Optional argument NOUP non-nil means don't go up in the window
768tree but try to enlarge windows within WINDOW's combination only.
769
770Optional argument NODOWN non-nil means don't check whether WINDOW
771itself \(and its subwindows) can be shrunk; check only whether at
772least one other windows can be enlarged appropriately."
447f16b8 773 (setq window (window-normalize-window window))
a1511caf
MR
774 (let ((size (window-total-size window horizontal))
775 (minimum (window-min-size window horizontal ignore)))
776 (cond
777 (nodown
778 ;; If NODOWN is t, try to recover the entire size of WINDOW.
779 (window-min-delta-1 window size horizontal ignore trail noup))
780 ((= size minimum)
781 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
782 ;; there's nothing to recover.
783 0)
784 (t
785 ;; Otherwise, try to recover whatever WINDOW is larger than its
786 ;; minimum size.
787 (window-min-delta-1
788 window (- size minimum) horizontal ignore trail noup)))))
789
790(defun window-max-delta-1 (window delta &optional horizontal ignore trail noup)
791 "Internal function of `window-max-delta'."
792 (if (not (window-parent window))
793 ;; Can't go up. Return DELTA.
794 delta
795 (let* ((parent (window-parent window))
796 (sub (window-child parent)))
797 (catch 'fixed
3d8daefe 798 (if (window-combined-p sub horizontal)
a1511caf
MR
799 ;; For an iso-combination calculate how much we can get from
800 ;; other subwindows.
801 (let ((skip (eq trail 'after)))
802 (while sub
803 (cond
804 ((eq sub window)
805 (setq skip (eq trail 'before)))
806 (skip)
807 (t
808 (setq delta
809 (+ delta
810 (- (window-total-size sub horizontal)
811 (window-min-size sub horizontal ignore))))))
812 (setq sub (window-right sub))))
813 ;; For an ortho-combination throw DELTA when at least one
814 ;; subwindow is fixed-size.
815 (while sub
816 (when (and (not (eq sub window))
817 (not (window-size-ignore sub ignore))
818 (window-size-fixed-p sub horizontal))
819 (throw 'fixed delta))
820 (setq sub (window-right sub))))
821 (if noup
822 ;; When NOUP is nil, DELTA is all we can get.
823 delta
824 ;; Else try with parent of WINDOW, passing the DELTA we
825 ;; recovered so far.
826 (window-max-delta-1 parent delta horizontal ignore trail))))))
827
828(defun window-max-delta (&optional window horizontal ignore trail noup nodown)
829 "Return maximum number of lines WINDOW by which WINDOW can be enlarged.
830WINDOW can be an arbitrary window and defaults to the selected
831window. The return value is zero if WINDOW cannot be enlarged.
832
833Optional argument HORIZONTAL non-nil means return maximum number
834of columns by which WINDOW can be enlarged.
835
836Optional argument IGNORE non-nil means ignore any restrictions
837imposed by fixed size windows, `window-min-height' or
838`window-min-width' settings. IGNORE a window means ignore
839restrictions for that window only. IGNORE equal `safe' means
840live windows may get as small as `window-safe-min-height' lines
841and `window-safe-min-width' columns.
842
843Optional argument TRAIL `before' means only windows to the left
844of or below WINDOW can be shrunk. Optional argument TRAIL
845`after' means only windows to the right of or above WINDOW can be
846shrunk.
847
848Optional argument NOUP non-nil means don't go up in the window
849tree but try to obtain the entire space from windows within
850WINDOW's combination.
851
852Optional argument NODOWN non-nil means do not check whether
853WINDOW itself \(and its subwindows) can be enlarged; check only
854whether other windows can be shrunk appropriately."
447f16b8 855 (setq window (window-normalize-window window))
a1511caf
MR
856 (if (and (not (window-size-ignore window ignore))
857 (not nodown) (window-size-fixed-p window horizontal))
858 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
859 ;; size.
860 0
861 ;; WINDOW has no fixed size.
862 (window-max-delta-1 window 0 horizontal ignore trail noup)))
863
864;; Make NOUP also inhibit the min-size check.
865(defun window-resizable (window delta &optional horizontal ignore trail noup nodown)
866 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
867Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
868can be resized horizontally by DELTA columns. A return value of
869zero means that WINDOW is not resizable.
870
871DELTA positive means WINDOW shall be enlarged by DELTA lines or
872columns. If WINDOW cannot be enlarged by DELTA lines or columns
873return the maximum value in the range 0..DELTA by which WINDOW
874can be enlarged.
875
876DELTA negative means WINDOW shall be shrunk by -DELTA lines or
877columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
878return the minimum value in the range DELTA..0 that can be used
879for shrinking WINDOW.
880
881Optional argument IGNORE non-nil means ignore any restrictions
882imposed by fixed size windows, `window-min-height' or
883`window-min-width' settings. IGNORE a window means ignore
884restrictions for that window only. IGNORE equal `safe' means
885live windows may get as small as `window-safe-min-height' lines
886and `window-safe-min-width' columns.
887
888Optional argument TRAIL `before' means only windows to the left
889of or below WINDOW can be shrunk. Optional argument TRAIL
890`after' means only windows to the right of or above WINDOW can be
891shrunk.
892
893Optional argument NOUP non-nil means don't go up in the window
894tree but try to distribute the space among the other windows
895within WINDOW's combination.
896
897Optional argument NODOWN non-nil means don't check whether WINDOW
898and its subwindows can be resized."
447f16b8 899 (setq window (window-normalize-window window))
a1511caf
MR
900 (cond
901 ((< delta 0)
902 (max (- (window-min-delta window horizontal ignore trail noup nodown))
903 delta))
904 ((> delta 0)
905 (min (window-max-delta window horizontal ignore trail noup nodown)
906 delta))
907 (t 0)))
908
909(defun window-resizable-p (window delta &optional horizontal ignore trail noup nodown)
910 "Return t if WINDOW can be resized vertically by DELTA lines.
911For the meaning of the arguments of this function see the
912doc-string of `window-resizable'."
447f16b8 913 (setq window (window-normalize-window window))
a1511caf
MR
914 (if (> delta 0)
915 (>= (window-resizable window delta horizontal ignore trail noup nodown)
916 delta)
917 (<= (window-resizable window delta horizontal ignore trail noup nodown)
918 delta)))
919
920(defsubst window-total-height (&optional window)
921 "Return the total number of lines of WINDOW.
922WINDOW can be any window and defaults to the selected one. The
923return value includes WINDOW's mode line and header line, if any.
924If WINDOW is internal the return value is the sum of the total
925number of lines of WINDOW's child windows if these are vertically
926combined and the height of WINDOW's first child otherwise.
927
928Note: This function does not take into account the value of
929`line-spacing' when calculating the number of lines in WINDOW."
930 (window-total-size window))
3c448ab6 931
f3d1777e
MR
932;; Eventually we should make `window-height' obsolete.
933(defalias 'window-height 'window-total-height)
934
ccafbf06 935;; See discussion in bug#4543.
a1511caf
MR
936(defsubst window-full-height-p (&optional window)
937 "Return t if WINDOW is as high as the containing frame.
938More precisely, return t if and only if the total height of
939WINDOW equals the total height of the root window of WINDOW's
940frame. WINDOW can be any window and defaults to the selected
941one."
447f16b8 942 (setq window (window-normalize-window window))
a1511caf
MR
943 (= (window-total-size window)
944 (window-total-size (frame-root-window window))))
945
946(defsubst window-total-width (&optional window)
947 "Return the total number of columns of WINDOW.
948WINDOW can be any window and defaults to the selected one. The
949return value includes any vertical dividers or scrollbars of
950WINDOW. If WINDOW is internal, the return value is the sum of
951the total number of columns of WINDOW's child windows if these
952are horizontally combined and the width of WINDOW's first child
953otherwise."
954 (window-total-size window t))
955
956(defsubst window-full-width-p (&optional window)
957 "Return t if WINDOW is as wide as the containing frame.
958More precisely, return t if and only if the total width of WINDOW
959equals the total width of the root window of WINDOW's frame.
960WINDOW can be any window and defaults to the selected one."
447f16b8 961 (setq window (window-normalize-window window))
a1511caf
MR
962 (= (window-total-size window t)
963 (window-total-size (frame-root-window window) t)))
964
965(defsubst window-body-height (&optional window)
966 "Return the number of lines of WINDOW's body.
967WINDOW must be a live window and defaults to the selected one.
968
969The return value does not include WINDOW's mode line and header
970line, if any. If a line at the bottom of the window is only
971partially visible, that line is included in the return value. If
972you do not want to include a partially visible bottom line in the
89bd5ee1
EZ
973return value, use `window-text-height' instead.
974
975Note that the return value is measured in canonical units, i.e. for
976the default frame's face. If the window shows some characters with
977non-default face, e.g., if the font of some characters is larger or
978smaller than the default font, the value returned by this function
979will not match the actual number of lines shown in the window. To
980get the actual number of lines, use `posn-at-point'."
a1511caf
MR
981 (window-body-size window))
982
983(defsubst window-body-width (&optional window)
984 "Return the number of columns of WINDOW's body.
985WINDOW must be a live window and defaults to the selected one.
986
987The return value does not include any vertical dividers or scroll
988bars owned by WINDOW. On a window-system the return value does
989not include the number of columns used for WINDOW's fringes or
89bd5ee1
EZ
990display margins either.
991
992Note that the return value is measured in canonical units, i.e. for
993the default frame's face. If the window shows some characters with
994non-default face, e.g., if the font of some characters is larger or
995smaller than the default font, the value returned by this function
996will not match the actual number of characters per line shown in the
997window. To get the actual number of columns, use `posn-at-point'."
a1511caf 998 (window-body-size window t))
02c6f098 999
f3d1777e
MR
1000;; Eventually we should make `window-height' obsolete.
1001(defalias 'window-width 'window-body-width)
1002
3c448ab6
MR
1003(defun window-current-scroll-bars (&optional window)
1004 "Return the current scroll bar settings for WINDOW.
387522b2 1005WINDOW must be a live window and defaults to the selected one.
3c448ab6
MR
1006
1007The return value is a cons cell (VERTICAL . HORIZONTAL) where
1008VERTICAL specifies the current location of the vertical scroll
1009bars (`left', `right', or nil), and HORIZONTAL specifies the
1010current location of the horizontal scroll bars (`top', `bottom',
1011or nil).
1012
1013Unlike `window-scroll-bars', this function reports the scroll bar
1014type actually used, once frame defaults and `scroll-bar-mode' are
1015taken into account."
447f16b8 1016 (setq window (window-normalize-window window t))
3c448ab6
MR
1017 (let ((vert (nth 2 (window-scroll-bars window)))
1018 (hor nil))
1019 (when (or (eq vert t) (eq hor t))
387522b2 1020 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
3c448ab6
MR
1021 (if (eq vert t)
1022 (setq vert (car fcsb)))
1023 (if (eq hor t)
1024 (setq hor (cdr fcsb)))))
1025 (cons vert hor)))
1026
1027(defun walk-windows (proc &optional minibuf all-frames)
387522b2 1028 "Cycle through all live windows, calling PROC for each one.
3c448ab6
MR
1029PROC must specify a function with a window as its sole argument.
1030The optional arguments MINIBUF and ALL-FRAMES specify the set of
387522b2 1031windows to include in the walk.
3c448ab6
MR
1032
1033MINIBUF t means include the minibuffer window even if the
1034minibuffer is not active. MINIBUF nil or omitted means include
1035the minibuffer window only if the minibuffer is active. Any
1036other value means do not include the minibuffer window even if
1037the minibuffer is active.
1038
387522b2
MR
1039ALL-FRAMES nil or omitted means consider all windows on the
1040selected frame, plus the minibuffer window if specified by the
1041MINIBUF argument. If the minibuffer counts, consider all windows
1042on all frames that share that minibuffer too. The following
1043non-nil values of ALL-FRAMES have special meanings:
1044
1045- t means consider all windows on all existing frames.
1046
1047- `visible' means consider all windows on all visible frames on
1048 the current terminal.
1049
1050- 0 (the number zero) means consider all windows on all visible
1051 and iconified frames on the current terminal.
1052
1053- A frame means consider all windows on that frame only.
1054
1055Anything else means consider all windows on the selected frame
1056and no others.
3c448ab6
MR
1057
1058This function changes neither the order of recently selected
1059windows nor the buffer list."
1060 ;; If we start from the minibuffer window, don't fail to come
1061 ;; back to it.
1062 (when (window-minibuffer-p (selected-window))
1063 (setq minibuf t))
1064 ;; Make sure to not mess up the order of recently selected
1065 ;; windows. Use `save-selected-window' and `select-window'
1066 ;; with second argument non-nil for this purpose.
1067 (save-selected-window
1068 (when (framep all-frames)
1069 (select-window (frame-first-window all-frames) 'norecord))
387522b2
MR
1070 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1071 (funcall proc walk-windows-window))))
1072
c96111ea
MR
1073(defun window-point-1 (&optional window)
1074 "Return value of WINDOW's point.
1075WINDOW can be any live window and defaults to the selected one.
1076
1077This function is like `window-point' with one exception: If
1078WINDOW is selected, it returns the value of `point' of WINDOW's
1079buffer regardless of whether that buffer is current or not."
447f16b8 1080 (setq window (window-normalize-window window t))
c96111ea
MR
1081 (if (eq window (selected-window))
1082 (with-current-buffer (window-buffer window)
1083 (point))
1084 (window-point window)))
1085
1086(defun set-window-point-1 (window pos)
1087 "Set value of WINDOW's point to POS.
1088WINDOW can be any live window and defaults to the selected one.
1089
1090This function is like `set-window-point' with one exception: If
1091WINDOW is selected, it moves `point' of WINDOW's buffer to POS
1092regardless of whether that buffer is current or not."
447f16b8 1093 (setq window (window-normalize-window window t))
c96111ea
MR
1094 (if (eq window (selected-window))
1095 (with-current-buffer (window-buffer window)
1096 (goto-char pos))
1097 (set-window-point window pos)))
1098
e07b9a6d
MR
1099(defun window-at-side-p (&optional window side)
1100 "Return t if WINDOW is at SIDE of its containing frame.
1101WINDOW can be any window and defaults to the selected one. SIDE
1102can be any of the symbols `left', `top', `right' or `bottom'.
1103The default value nil is handled like `bottom'."
447f16b8 1104 (setq window (window-normalize-window window))
e07b9a6d
MR
1105 (let ((edge
1106 (cond
1107 ((eq side 'left) 0)
1108 ((eq side 'top) 1)
1109 ((eq side 'right) 2)
1110 ((memq side '(bottom nil)) 3))))
1111 (= (nth edge (window-edges window))
1112 (nth edge (window-edges (frame-root-window window))))))
1113
1114(defun windows-at-side (&optional frame side)
1115 "Return list of all windows on SIDE of FRAME.
1116FRAME must be a live frame and defaults to the selected frame.
1117SIDE can be any of the symbols `left', `top', `right' or
1118`bottom'. The default value nil is handled like `bottom'."
1119 (setq frame (window-normalize-frame frame))
1120 (let (windows)
1121 (walk-window-tree
1122 (lambda (window)
1123 (when (window-at-side-p window side)
1124 (setq windows (cons window windows))))
1125 frame)
1126 (nreverse windows)))
1127
387522b2
MR
1128(defun window-in-direction-2 (window posn &optional horizontal)
1129 "Support function for `window-in-direction'."
1130 (if horizontal
1131 (let ((top (window-top-line window)))
1132 (if (> top posn)
1133 (- top posn)
1134 (- posn top (window-total-height window))))
1135 (let ((left (window-left-column window)))
1136 (if (> left posn)
1137 (- left posn)
1138 (- posn left (window-total-width window))))))
1139
1140(defun window-in-direction (direction &optional window ignore)
1141 "Return window in DIRECTION as seen from WINDOW.
1142DIRECTION must be one of `above', `below', `left' or `right'.
1143WINDOW must be a live window and defaults to the selected one.
1144IGNORE, when non-nil means a window can be returned even if its
1145`no-other-window' parameter is non-nil."
447f16b8 1146 (setq window (window-normalize-window window t))
387522b2
MR
1147 (unless (memq direction '(above below left right))
1148 (error "Wrong direction %s" direction))
1149 (let* ((frame (window-frame window))
1150 (hor (memq direction '(left right)))
1151 (first (if hor
1152 (window-left-column window)
1153 (window-top-line window)))
1154 (last (+ first (if hor
1155 (window-total-width window)
1156 (window-total-height window))))
c96111ea 1157 (posn-cons (nth 6 (posn-at-point (window-point-1 window) window)))
387522b2
MR
1158 ;; The column / row value of `posn-at-point' can be nil for the
1159 ;; mini-window, guard against that.
1160 (posn (if hor
1161 (+ (or (cdr posn-cons) 1) (window-top-line window))
1162 (+ (or (car posn-cons) 1) (window-left-column window))))
1163 (best-edge
1164 (cond
1165 ((eq direction 'below) (frame-height frame))
1166 ((eq direction 'right) (frame-width frame))
1167 (t -1)))
1168 (best-edge-2 best-edge)
1169 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1170 best best-2 best-diff-2-new)
1171 (walk-window-tree
1172 (lambda (w)
1173 (let* ((w-top (window-top-line w))
1174 (w-left (window-left-column w)))
1175 (cond
1176 ((or (eq window w)
1177 ;; Ignore ourselves.
1178 (and (window-parameter w 'no-other-window)
1179 ;; Ignore W unless IGNORE is non-nil.
1180 (not ignore))))
1181 (hor
1182 (cond
1183 ((and (<= w-top posn)
1184 (< posn (+ w-top (window-total-height w))))
1185 ;; W is to the left or right of WINDOW and covers POSN.
1186 (when (or (and (eq direction 'left)
1187 (<= w-left first) (> w-left best-edge))
1188 (and (eq direction 'right)
1189 (>= w-left last) (< w-left best-edge)))
1190 (setq best-edge w-left)
1191 (setq best w)))
1192 ((and (or (and (eq direction 'left)
1193 (<= (+ w-left (window-total-width w)) first))
1194 (and (eq direction 'right) (<= last w-left)))
1195 ;; W is to the left or right of WINDOW but does not
1196 ;; cover POSN.
1197 (setq best-diff-2-new
1198 (window-in-direction-2 w posn hor))
1199 (or (< best-diff-2-new best-diff-2)
1200 (and (= best-diff-2-new best-diff-2)
1201 (if (eq direction 'left)
1202 (> w-left best-edge-2)
1203 (< w-left best-edge-2)))))
1204 (setq best-edge-2 w-left)
1205 (setq best-diff-2 best-diff-2-new)
1206 (setq best-2 w))))
1207 (t
1208 (cond
1209 ((and (<= w-left posn)
1210 (< posn (+ w-left (window-total-width w))))
1211 ;; W is above or below WINDOW and covers POSN.
1212 (when (or (and (eq direction 'above)
1213 (<= w-top first) (> w-top best-edge))
1214 (and (eq direction 'below)
1215 (>= w-top first) (< w-top best-edge)))
1216 (setq best-edge w-top)
1217 (setq best w)))
1218 ((and (or (and (eq direction 'above)
1219 (<= (+ w-top (window-total-height w)) first))
1220 (and (eq direction 'below) (<= last w-top)))
1221 ;; W is above or below WINDOW but does not cover POSN.
1222 (setq best-diff-2-new
1223 (window-in-direction-2 w posn hor))
1224 (or (< best-diff-2-new best-diff-2)
1225 (and (= best-diff-2-new best-diff-2)
1226 (if (eq direction 'above)
1227 (> w-top best-edge-2)
1228 (< w-top best-edge-2)))))
1229 (setq best-edge-2 w-top)
1230 (setq best-diff-2 best-diff-2-new)
1231 (setq best-2 w)))))))
1232 (window-frame window))
1233 (or best best-2)))
3c448ab6 1234
4c43d97b 1235(defun get-window-with-predicate (predicate &optional minibuf all-frames default)
387522b2
MR
1236 "Return a live window satisfying PREDICATE.
1237More precisely, cycle through all windows calling the function
1238PREDICATE on each one of them with the window as its sole
1239argument. Return the first window for which PREDICATE returns
4c43d97b
MR
1240non-nil. Windows are scanned starting with the window following
1241the selcted window. If no window satisfies PREDICATE, return
1242DEFAULT.
1243
1244MINIBUF t means include the minibuffer window even if the
1245minibuffer is not active. MINIBUF nil or omitted means include
1246the minibuffer window only if the minibuffer is active. Any
1247other value means do not include the minibuffer window even if
1248the minibuffer is active.
387522b2
MR
1249
1250ALL-FRAMES nil or omitted means consider all windows on the selected
1251frame, plus the minibuffer window if specified by the MINIBUF
1252argument. If the minibuffer counts, consider all windows on all
1253frames that share that minibuffer too. The following non-nil
1254values of ALL-FRAMES have special meanings:
3c448ab6 1255
387522b2
MR
1256- t means consider all windows on all existing frames.
1257
1258- `visible' means consider all windows on all visible frames on
1259 the current terminal.
1260
1261- 0 (the number zero) means consider all windows on all visible
1262 and iconified frames on the current terminal.
1263
1264- A frame means consider all windows on that frame only.
1265
1266Anything else means consider all windows on the selected frame
1267and no others."
3c448ab6 1268 (catch 'found
4c43d97b
MR
1269 (dolist (window (window-list-1
1270 (next-window nil minibuf all-frames)
1271 minibuf all-frames))
387522b2
MR
1272 (when (funcall predicate window)
1273 (throw 'found window)))
3c448ab6
MR
1274 default))
1275
1276(defalias 'some-window 'get-window-with-predicate)
1277
190b47e6
MR
1278(defun get-lru-window (&optional all-frames dedicated)
1279 "Return the least recently used window on frames specified by ALL-FRAMES.
1280Return a full-width window if possible. A minibuffer window is
1281never a candidate. A dedicated window is never a candidate
1282unless DEDICATED is non-nil, so if all windows are dedicated, the
1283value is nil. Avoid returning the selected window if possible.
1284
1285The following non-nil values of the optional argument ALL-FRAMES
1286have special meanings:
1287
1288- t means consider all windows on all existing frames.
1289
1290- `visible' means consider all windows on all visible frames on
1291 the current terminal.
1292
1293- 0 (the number zero) means consider all windows on all visible
1294 and iconified frames on the current terminal.
1295
1296- A frame means consider all windows on that frame only.
1297
1298Any other value of ALL-FRAMES means consider all windows on the
1299selected frame and no others."
1300 (let (best-window best-time second-best-window second-best-time time)
02cfc6d6 1301 (dolist (window (window-list-1 nil 'nomini all-frames))
190b47e6
MR
1302 (when (or dedicated (not (window-dedicated-p window)))
1303 (setq time (window-use-time window))
1304 (if (or (eq window (selected-window))
1305 (not (window-full-width-p window)))
1306 (when (or (not second-best-time) (< time second-best-time))
1307 (setq second-best-time time)
1308 (setq second-best-window window))
1309 (when (or (not best-time) (< time best-time))
1310 (setq best-time time)
1311 (setq best-window window)))))
1312 (or best-window second-best-window)))
1313
387522b2
MR
1314(defun get-mru-window (&optional all-frames)
1315 "Return the most recently used window on frames specified by ALL-FRAMES.
1316Do not return a minibuffer window.
1317
1318The following non-nil values of the optional argument ALL-FRAMES
1319have special meanings:
1320
1321- t means consider all windows on all existing frames.
1322
1323- `visible' means consider all windows on all visible frames on
1324 the current terminal.
1325
1326- 0 (the number zero) means consider all windows on all visible
1327 and iconified frames on the current terminal.
1328
1329- A frame means consider all windows on that frame only.
1330
1331Any other value of ALL-FRAMES means consider all windows on the
1332selected frame and no others."
1333 (let (best-window best-time time)
02cfc6d6 1334 (dolist (window (window-list-1 nil 'nomini all-frames))
387522b2
MR
1335 (setq time (window-use-time window))
1336 (when (or (not best-time) (> time best-time))
1337 (setq best-time time)
1338 (setq best-window window)))
1339 best-window))
1340
190b47e6
MR
1341(defun get-largest-window (&optional all-frames dedicated)
1342 "Return the largest window on frames specified by ALL-FRAMES.
1343A minibuffer window is never a candidate. A dedicated window is
1344never a candidate unless DEDICATED is non-nil, so if all windows
1345are dedicated, the value is nil.
1346
1347The following non-nil values of the optional argument ALL-FRAMES
1348have special meanings:
1349
1350- t means consider all windows on all existing frames.
1351
1352- `visible' means consider all windows on all visible frames on
1353 the current terminal.
1354
1355- 0 (the number zero) means consider all windows on all visible
1356 and iconified frames on the current terminal.
1357
1358- A frame means consider all windows on that frame only.
1359
1360Any other value of ALL-FRAMES means consider all windows on the
1361selected frame and no others."
1362 (let ((best-size 0)
1363 best-window size)
02cfc6d6 1364 (dolist (window (window-list-1 nil 'nomini all-frames))
190b47e6
MR
1365 (when (or dedicated (not (window-dedicated-p window)))
1366 (setq size (* (window-total-size window)
1367 (window-total-size window t)))
1368 (when (> size best-size)
1369 (setq best-size size)
1370 (setq best-window window))))
1371 best-window))
1372
3c448ab6
MR
1373(defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1374 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1375BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4c43d97b
MR
1376and defaults to the current buffer. Windows are scanned starting
1377with the selected window.
190b47e6
MR
1378
1379MINIBUF t means include the minibuffer window even if the
1380minibuffer is not active. MINIBUF nil or omitted means include
1381the minibuffer window only if the minibuffer is active. Any
1382other value means do not include the minibuffer window even if
1383the minibuffer is active.
1384
1385ALL-FRAMES nil or omitted means consider all windows on the
1386selected frame, plus the minibuffer window if specified by the
1387MINIBUF argument. If the minibuffer counts, consider all windows
1388on all frames that share that minibuffer too. The following
1389non-nil values of ALL-FRAMES have special meanings:
1390
1391- t means consider all windows on all existing frames.
1392
1393- `visible' means consider all windows on all visible frames on
1394 the current terminal.
1395
1396- 0 (the number zero) means consider all windows on all visible
1397 and iconified frames on the current terminal.
1398
1399- A frame means consider all windows on that frame only.
1400
1401Anything else means consider all windows on the selected frame
1402and no others."
5386012d 1403 (let ((buffer (window-normalize-buffer buffer-or-name))
3c448ab6 1404 windows)
4c43d97b 1405 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
190b47e6
MR
1406 (when (eq (window-buffer window) buffer)
1407 (setq windows (cons window windows))))
1408 (nreverse windows)))
3c448ab6
MR
1409
1410(defun minibuffer-window-active-p (window)
1411 "Return t if WINDOW is the currently active minibuffer window."
1412 (eq window (active-minibuffer-window)))
387522b2 1413
3c448ab6 1414(defun count-windows (&optional minibuf)
387522b2 1415 "Return the number of live windows on the selected frame.
3c448ab6
MR
1416The optional argument MINIBUF specifies whether the minibuffer
1417window shall be counted. See `walk-windows' for the precise
1418meaning of this argument."
387522b2 1419 (length (window-list-1 nil minibuf)))
9aab8e0d
MR
1420\f
1421;;; Resizing windows.
5386012d 1422(defun window--resize-reset (&optional frame horizontal)
9aab8e0d
MR
1423 "Reset resize values for all windows on FRAME.
1424FRAME defaults to the selected frame.
1425
1426This function stores the current value of `window-total-size' applied
1427with argument HORIZONTAL in the new total size of all windows on
1428FRAME. It also resets the new normal size of each of these
1429windows."
5386012d
MR
1430 (window--resize-reset-1
1431 (frame-root-window (window-normalize-frame frame)) horizontal))
9aab8e0d 1432
5386012d
MR
1433(defun window--resize-reset-1 (window horizontal)
1434 "Internal function of `window--resize-reset'."
9aab8e0d
MR
1435 ;; Register old size in the new total size.
1436 (set-window-new-total window (window-total-size window horizontal))
1437 ;; Reset new normal size.
1438 (set-window-new-normal window)
1439 (when (window-child window)
5386012d 1440 (window--resize-reset-1 (window-child window) horizontal))
9aab8e0d 1441 (when (window-right window)
5386012d 1442 (window--resize-reset-1 (window-right window) horizontal)))
9aab8e0d 1443
562dd5e9
MR
1444;; The following routine is used to manually resize the minibuffer
1445;; window and is currently used, for example, by ispell.el.
5386012d 1446(defun window--resize-mini-window (window delta)
562dd5e9
MR
1447 "Resize minibuffer window WINDOW by DELTA lines.
1448If WINDOW cannot be resized by DELTA lines make it as large \(or
1449as small) as possible but don't signal an error."
1450 (when (window-minibuffer-p window)
1451 (let* ((frame (window-frame window))
1452 (root (frame-root-window frame))
1453 (height (window-total-size window))
1454 (min-delta
1455 (- (window-total-size root)
1456 (window-min-size root))))
1457 ;; Sanitize DELTA.
1458 (cond
1459 ((<= (+ height delta) 0)
1460 (setq delta (- (- height 1))))
1461 ((> delta min-delta)
1462 (setq delta min-delta)))
1463
1464 ;; Resize now.
5386012d 1465 (window--resize-reset frame)
562dd5e9
MR
1466 ;; Ideally we should be able to resize just the last subwindow of
1467 ;; root here. See the comment in `resize-root-window-vertically'
1468 ;; for why we do not do that.
5386012d 1469 (window--resize-this-window root (- delta) nil nil t)
562dd5e9
MR
1470 (set-window-new-total window (+ height delta))
1471 ;; The following routine catches the case where we want to resize
1472 ;; a minibuffer-only frame.
1473 (resize-mini-window-internal window))))
1474
d615d6d2 1475(defun window-resize (window delta &optional horizontal ignore)
562dd5e9
MR
1476 "Resize WINDOW vertically by DELTA lines.
1477WINDOW can be an arbitrary window and defaults to the selected
1478one. An attempt to resize the root window of a frame will raise
1479an error though.
1480
1481DELTA a positive number means WINDOW shall be enlarged by DELTA
1482lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1483lines.
1484
1485Optional argument HORIZONTAL non-nil means resize WINDOW
1486horizontally by DELTA columns. In this case a positive DELTA
1487means enlarge WINDOW by DELTA columns. DELTA negative means
1488WINDOW shall be shrunk by -DELTA columns.
1489
1490Optional argument IGNORE non-nil means ignore any restrictions
1491imposed by fixed size windows, `window-min-height' or
1492`window-min-width' settings. IGNORE any window means ignore
1493restrictions for that window only. IGNORE equal `safe' means
1494live windows may get as small as `window-safe-min-height' lines
1495and `window-safe-min-width' columns.
1496
1497This function resizes other windows proportionally and never
1498deletes any windows. If you want to move only the low (right)
1499edge of WINDOW consider using `adjust-window-trailing-edge'
1500instead."
447f16b8 1501 (setq window (window-normalize-window window))
562dd5e9
MR
1502 (let* ((frame (window-frame window))
1503 sibling)
1504 (cond
1505 ((eq window (frame-root-window frame))
1506 (error "Cannot resize the root window of a frame"))
1507 ((window-minibuffer-p window)
5386012d 1508 (window--resize-mini-window window delta))
562dd5e9 1509 ((window-resizable-p window delta horizontal ignore)
5386012d
MR
1510 (window--resize-reset frame horizontal)
1511 (window--resize-this-window window delta horizontal ignore t)
562dd5e9 1512 (if (and (not (window-splits window))
3d8daefe 1513 (window-combined-p window horizontal)
562dd5e9
MR
1514 (setq sibling (or (window-right window) (window-left window)))
1515 (window-sizable-p sibling (- delta) horizontal ignore))
1516 ;; If window-splits returns nil for WINDOW, WINDOW is part of
1517 ;; an iso-combination, and WINDOW's neighboring right or left
1518 ;; sibling can be resized as requested, resize that sibling.
1519 (let ((normal-delta
1520 (/ (float delta)
1521 (window-total-size (window-parent window) horizontal))))
5386012d 1522 (window--resize-this-window sibling (- delta) horizontal nil t)
562dd5e9
MR
1523 (set-window-new-normal
1524 window (+ (window-normal-size window horizontal)
1525 normal-delta))
1526 (set-window-new-normal
1527 sibling (- (window-normal-size sibling horizontal)
1528 normal-delta)))
1529 ;; Otherwise, resize all other windows in the same combination.
5386012d 1530 (window--resize-siblings window delta horizontal ignore))
d615d6d2 1531 (window-resize-apply frame horizontal))
562dd5e9
MR
1532 (t
1533 (error "Cannot resize window %s" window)))))
1534
5386012d 1535(defsubst window--resize-subwindows-skip-p (window)
9aab8e0d
MR
1536 "Return non-nil if WINDOW shall be skipped by resizing routines."
1537 (memq (window-new-normal window) '(ignore stuck skip)))
1538
5386012d 1539(defun window--resize-subwindows-normal (parent horizontal window this-delta &optional trail other-delta)
9aab8e0d
MR
1540 "Set the new normal height of subwindows of window PARENT.
1541HORIZONTAL non-nil means set the new normal width of these
1542windows. WINDOW specifies a subwindow of PARENT that has been
1543resized by THIS-DELTA lines \(columns).
1544
1545Optional argument TRAIL either 'before or 'after means set values
1546for windows before or after WINDOW only. Optional argument
1547OTHER-DELTA a number specifies that this many lines \(columns)
1548have been obtained from \(or returned to) an ancestor window of
1549PARENT in order to resize WINDOW."
1550 (let* ((delta-normal
1551 (if (and (= (- this-delta) (window-total-size window horizontal))
1552 (zerop other-delta))
1553 ;; When WINDOW gets deleted and we can return its entire
1554 ;; space to its siblings, use WINDOW's normal size as the
1555 ;; normal delta.
1556 (- (window-normal-size window horizontal))
1557 ;; In any other case calculate the normal delta from the
1558 ;; relation of THIS-DELTA to the total size of PARENT.
1559 (/ (float this-delta) (window-total-size parent horizontal))))
1560 (sub (window-child parent))
1561 (parent-normal 0.0)
1562 (skip (eq trail 'after)))
1563
1564 ;; Set parent-normal to the sum of the normal sizes of all
1565 ;; subwindows of PARENT that shall be resized, excluding only WINDOW
1566 ;; and any windows specified by the optional TRAIL argument.
1567 (while sub
1568 (cond
1569 ((eq sub window)
1570 (setq skip (eq trail 'before)))
1571 (skip)
1572 (t
1573 (setq parent-normal
1574 (+ parent-normal (window-normal-size sub horizontal)))))
1575 (setq sub (window-right sub)))
1576
1577 ;; Set the new normal size of all subwindows of PARENT from what
1578 ;; they should have contributed for recovering THIS-DELTA lines
1579 ;; (columns).
1580 (setq sub (window-child parent))
1581 (setq skip (eq trail 'after))
1582 (while sub
1583 (cond
1584 ((eq sub window)
1585 (setq skip (eq trail 'before)))
1586 (skip)
1587 (t
1588 (let ((old-normal (window-normal-size sub horizontal)))
1589 (set-window-new-normal
1590 sub (min 1.0 ; Don't get larger than 1.
1591 (max (- old-normal
1592 (* (/ old-normal parent-normal)
1593 delta-normal))
1594 ;; Don't drop below 0.
1595 0.0))))))
1596 (setq sub (window-right sub)))
1597
1598 (when (numberp other-delta)
1599 ;; Set the new normal size of windows from what they should have
1600 ;; contributed for recovering OTHER-DELTA lines (columns).
1601 (setq delta-normal (/ (float (window-total-size parent horizontal))
1602 (+ (window-total-size parent horizontal)
1603 other-delta)))
1604 (setq sub (window-child parent))
1605 (setq skip (eq trail 'after))
1606 (while sub
1607 (cond
1608 ((eq sub window)
1609 (setq skip (eq trail 'before)))
1610 (skip)
1611 (t
1612 (set-window-new-normal
1613 sub (min 1.0 ; Don't get larger than 1.
1614 (max (* (window-new-normal sub) delta-normal)
1615 ;; Don't drop below 0.
1616 0.0)))))
1617 (setq sub (window-right sub))))
1618
1619 ;; Set the new normal size of WINDOW to what is left by the sum of
1620 ;; the normal sizes of its siblings.
1621 (set-window-new-normal
1622 window
1623 (let ((sum 0))
1624 (setq sub (window-child parent))
1625 (while sub
1626 (cond
1627 ((eq sub window))
1628 ((not (numberp (window-new-normal sub)))
1629 (setq sum (+ sum (window-normal-size sub horizontal))))
1630 (t
1631 (setq sum (+ sum (window-new-normal sub)))))
1632 (setq sub (window-right sub)))
1633 ;; Don't get larger than 1 or smaller than 0.
1634 (min 1.0 (max (- 1.0 sum) 0.0))))))
1635
5386012d 1636(defun window--resize-subwindows (parent delta &optional horizontal window ignore trail edge)
9aab8e0d
MR
1637 "Resize subwindows of window PARENT vertically by DELTA lines.
1638PARENT must be a vertically combined internal window.
1639
1640Optional argument HORIZONTAL non-nil means resize subwindows of
1641PARENT horizontally by DELTA columns. In this case PARENT must
1642be a horizontally combined internal window.
1643
1644WINDOW, if specified, must denote a child window of PARENT that
1645is resized by DELTA lines.
1646
1647Optional argument IGNORE non-nil means ignore any restrictions
1648imposed by fixed size windows, `window-min-height' or
1649`window-min-width' settings. IGNORE equal `safe' means live
1650windows may get as small as `window-safe-min-height' lines and
1651`window-safe-min-width' columns. IGNORE any window means ignore
1652restrictions for that window only.
1653
1654Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1655of windows that shall be resized. If TRAIL equals `before',
1656resize only windows on the left or above EDGE. If TRAIL equals
1657`after', resize only windows on the right or below EDGE. Also,
1658preferably only resize windows adjacent to EDGE.
1659
1660Return the symbol `normalized' if new normal sizes have been
1661already set by this routine."
1662 (let* ((first (window-child parent))
1663 (sub first)
1664 (parent-total (+ (window-total-size parent horizontal) delta))
1665 best-window best-value)
1666
1667 (if (and edge (memq trail '(before after))
1668 (progn
1669 (setq sub first)
1670 (while (and (window-right sub)
1671 (or (and (eq trail 'before)
5386012d 1672 (not (window--resize-subwindows-skip-p
9aab8e0d
MR
1673 (window-right sub))))
1674 (and (eq trail 'after)
5386012d 1675 (window--resize-subwindows-skip-p sub))))
9aab8e0d
MR
1676 (setq sub (window-right sub)))
1677 sub)
1678 (if horizontal
1679 (if (eq trail 'before)
1680 (= (+ (window-left-column sub)
1681 (window-total-size sub t))
1682 edge)
1683 (= (window-left-column sub) edge))
1684 (if (eq trail 'before)
1685 (= (+ (window-top-line sub)
1686 (window-total-size sub))
1687 edge)
1688 (= (window-top-line sub) edge)))
1689 (window-sizable-p sub delta horizontal ignore))
1690 ;; Resize only windows adjacent to EDGE.
1691 (progn
5386012d
MR
1692 (window--resize-this-window
1693 sub delta horizontal ignore t trail edge)
9aab8e0d
MR
1694 (if (and window (eq (window-parent sub) parent))
1695 (progn
1696 ;; Assign new normal sizes.
1697 (set-window-new-normal
1698 sub (/ (float (window-new-total sub)) parent-total))
1699 (set-window-new-normal
1700 window (- (window-normal-size window horizontal)
1701 (- (window-new-normal sub)
1702 (window-normal-size sub horizontal)))))
5386012d
MR
1703 (window--resize-subwindows-normal
1704 parent horizontal sub 0 trail delta))
1705 ;; Return 'normalized to notify `window--resize-siblings' that
9aab8e0d
MR
1706 ;; normal sizes have been already set.
1707 'normalized)
1708 ;; Resize all windows proportionally.
1709 (setq sub first)
1710 (while sub
1711 (cond
5386012d 1712 ((or (window--resize-subwindows-skip-p sub)
9aab8e0d
MR
1713 ;; Ignore windows to skip and fixed-size subwindows - in
1714 ;; the latter case make it a window to skip.
1715 (and (not ignore)
1716 (window-size-fixed-p sub horizontal)
1717 (set-window-new-normal sub 'ignore))))
1718 ((< delta 0)
1719 ;; When shrinking store the number of lines/cols we can get
1720 ;; from this window here together with the total/normal size
1721 ;; factor.
1722 (set-window-new-normal
1723 sub
1724 (cons
1725 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
1726 (window-min-delta sub horizontal ignore trail t) ; t)
1727 (- (/ (float (window-total-size sub horizontal))
1728 parent-total)
1729 (window-normal-size sub horizontal)))))
1730 ((> delta 0)
1731 ;; When enlarging store the total/normal size factor only
1732 (set-window-new-normal
1733 sub
1734 (- (/ (float (window-total-size sub horizontal))
1735 parent-total)
1736 (window-normal-size sub horizontal)))))
1737
1738 (setq sub (window-right sub)))
1739
1740 (cond
1741 ((< delta 0)
1742 ;; Shrink windows by delta.
1743 (setq best-window t)
1744 (while (and best-window (not (zerop delta)))
1745 (setq sub first)
1746 (setq best-window nil)
1747 (setq best-value most-negative-fixnum)
1748 (while sub
1749 (when (and (consp (window-new-normal sub))
1750 (not (zerop (car (window-new-normal sub))))
1751 (> (cdr (window-new-normal sub)) best-value))
1752 (setq best-window sub)
1753 (setq best-value (cdr (window-new-normal sub))))
1754
1755 (setq sub (window-right sub)))
1756
1757 (when best-window
1758 (setq delta (1+ delta)))
1759 (set-window-new-total best-window -1 t)
1760 (set-window-new-normal
1761 best-window
1762 (if (= (car (window-new-normal best-window)) 1)
1763 'skip ; We can't shrink best-window any further.
1764 (cons (1- (car (window-new-normal best-window)))
1765 (- (/ (float (window-new-total best-window))
1766 parent-total)
1767 (window-normal-size best-window horizontal)))))))
1768 ((> delta 0)
1769 ;; Enlarge windows by delta.
1770 (setq best-window t)
1771 (while (and best-window (not (zerop delta)))
1772 (setq sub first)
1773 (setq best-window nil)
1774 (setq best-value most-positive-fixnum)
1775 (while sub
1776 (when (and (numberp (window-new-normal sub))
1777 (< (window-new-normal sub) best-value))
1778 (setq best-window sub)
1779 (setq best-value (window-new-normal sub)))
1780
1781 (setq sub (window-right sub)))
1782
1783 (when best-window
1784 (setq delta (1- delta)))
1785 (set-window-new-total best-window 1 t)
1786 (set-window-new-normal
1787 best-window
1788 (- (/ (float (window-new-total best-window))
1789 parent-total)
1790 (window-normal-size best-window horizontal))))))
1791
1792 (when best-window
1793 (setq sub first)
1794 (while sub
1795 (when (or (consp (window-new-normal sub))
1796 (numberp (window-new-normal sub)))
d615d6d2 1797 ;; Reset new normal size fields so `window-resize-apply'
9aab8e0d
MR
1798 ;; won't use them to apply new sizes.
1799 (set-window-new-normal sub))
1800
1801 (unless (eq (window-new-normal sub) 'ignore)
1802 ;; Resize this subwindow's subwindows (back-engineering
1803 ;; delta from sub's old and new total sizes).
1804 (let ((delta (- (window-new-total sub)
1805 (window-total-size sub horizontal))))
1806 (unless (and (zerop delta) (not trail))
1807 ;; For the TRAIL non-nil case we have to resize SUB
1808 ;; recursively even if it's size does not change.
5386012d 1809 (window--resize-this-window
9aab8e0d
MR
1810 sub delta horizontal ignore nil trail edge))))
1811 (setq sub (window-right sub)))))))
1812
5386012d 1813(defun window--resize-siblings (window delta &optional horizontal ignore trail edge)
9aab8e0d
MR
1814 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1815Optional argument HORIZONTAL non-nil means resize other windows
1816when WINDOW is resized horizontally by DELTA columns. WINDOW
1817itself is not resized by this function.
1818
1819Optional argument IGNORE non-nil means ignore any restrictions
1820imposed by fixed size windows, `window-min-height' or
1821`window-min-width' settings. IGNORE equal `safe' means live
1822windows may get as small as `window-safe-min-height' lines and
1823`window-safe-min-width' columns. IGNORE any window means ignore
1824restrictions for that window only.
1825
1826Optional arguments TRAIL and EDGE, when non-nil, refine the set
1827of windows that shall be resized. If TRAIL equals `before',
1828resize only windows on the left or above EDGE. If TRAIL equals
1829`after', resize only windows on the right or below EDGE. Also,
1830preferably only resize windows adjacent to EDGE."
1831 (when (window-parent window)
1832 (let* ((parent (window-parent window))
1833 (sub (window-child parent)))
3d8daefe 1834 (if (window-combined-p sub horizontal)
9aab8e0d
MR
1835 ;; In an iso-combination try to extract DELTA from WINDOW's
1836 ;; siblings.
1837 (let ((first sub)
1838 (skip (eq trail 'after))
1839 this-delta other-delta)
1840 ;; Decide which windows shall be left alone.
1841 (while sub
1842 (cond
1843 ((eq sub window)
1844 ;; Make sure WINDOW is left alone when
1845 ;; resizing its siblings.
1846 (set-window-new-normal sub 'ignore)
1847 (setq skip (eq trail 'before)))
1848 (skip
1849 ;; Make sure this sibling is left alone when
1850 ;; resizing its siblings.
1851 (set-window-new-normal sub 'ignore))
1852 ((or (window-size-ignore sub ignore)
1853 (not (window-size-fixed-p sub horizontal)))
1854 ;; Set this-delta to t to signal that we found a sibling
1855 ;; of WINDOW whose size is not fixed.
1856 (setq this-delta t)))
1857
1858 (setq sub (window-right sub)))
1859
1860 ;; Set this-delta to what we can get from WINDOW's siblings.
1861 (if (= (- delta) (window-total-size window horizontal))
1862 ;; A deletion, presumably. We must handle this case
1863 ;; specially since `window-resizable' can't be used.
1864 (if this-delta
1865 ;; There's at least one resizable sibling we can
1866 ;; give WINDOW's size to.
1867 (setq this-delta delta)
1868 ;; No resizable sibling exists.
1869 (setq this-delta 0))
1870 ;; Any other form of resizing.
1871 (setq this-delta
1872 (window-resizable window delta horizontal ignore trail t)))
1873
1874 ;; Set other-delta to what we still have to get from
1875 ;; ancestor windows of parent.
1876 (setq other-delta (- delta this-delta))
1877 (unless (zerop other-delta)
1878 ;; Unless we got everything from WINDOW's siblings, PARENT
1879 ;; must be resized by other-delta lines or columns.
1880 (set-window-new-total parent other-delta 'add))
1881
1882 (if (zerop this-delta)
1883 ;; We haven't got anything from WINDOW's siblings but we
1884 ;; must update the normal sizes to respect other-delta.
5386012d 1885 (window--resize-subwindows-normal
9aab8e0d
MR
1886 parent horizontal window this-delta trail other-delta)
1887 ;; We did get something from WINDOW's siblings which means
1888 ;; we have to resize their subwindows.
5386012d
MR
1889 (unless (eq (window--resize-subwindows
1890 parent (- this-delta) horizontal
1891 window ignore trail edge)
1892 ;; If `window--resize-subwindows' returns
1893 ;; 'normalized, this means it has set the
1894 ;; normal sizes already.
9aab8e0d
MR
1895 'normalized)
1896 ;; Set the normal sizes.
5386012d 1897 (window--resize-subwindows-normal
9aab8e0d
MR
1898 parent horizontal window this-delta trail other-delta))
1899 ;; Set DELTA to what we still have to get from ancestor
1900 ;; windows.
1901 (setq delta other-delta)))
1902
1903 ;; In an ortho-combination all siblings of WINDOW must be
1904 ;; resized by DELTA.
1905 (set-window-new-total parent delta 'add)
1906 (while sub
1907 (unless (eq sub window)
5386012d 1908 (window--resize-this-window sub delta horizontal ignore t))
9aab8e0d
MR
1909 (setq sub (window-right sub))))
1910
1911 (unless (zerop delta)
1912 ;; "Go up."
5386012d
MR
1913 (window--resize-siblings
1914 parent delta horizontal ignore trail edge)))))
9aab8e0d 1915
5386012d 1916(defun window--resize-this-window (window delta &optional horizontal ignore add trail edge)
9aab8e0d
MR
1917 "Resize WINDOW vertically by DELTA lines.
1918Optional argument HORIZONTAL non-nil means resize WINDOW
1919horizontally by DELTA columns.
1920
1921Optional argument IGNORE non-nil means ignore any restrictions
1922imposed by fixed size windows, `window-min-height' or
1923`window-min-width' settings. IGNORE equal `safe' means live
1924windows may get as small as `window-safe-min-height' lines and
1925`window-safe-min-width' columns. IGNORE any window means ignore
1926restrictions for that window only.
1927
1928Optional argument ADD non-nil means add DELTA to the new total
1929size of WINDOW.
1930
1931Optional arguments TRAIL and EDGE, when non-nil, refine the set
1932of windows that shall be resized. If TRAIL equals `before',
1933resize only windows on the left or above EDGE. If TRAIL equals
1934`after', resize only windows on the right or below EDGE. Also,
1935preferably only resize windows adjacent to EDGE.
1936
1937This function recursively resizes WINDOW's subwindows to fit the
1938new size. Make sure that WINDOW is `window-resizable' before
1939calling this function. Note that this function does not resize
1940siblings of WINDOW or WINDOW's parent window. You have to
d615d6d2 1941eventually call `window-resize-apply' in order to make resizing
9aab8e0d
MR
1942actually take effect."
1943 (when add
1944 ;; Add DELTA to the new total size of WINDOW.
1945 (set-window-new-total window delta t))
387522b2 1946
9aab8e0d
MR
1947 (let ((sub (window-child window)))
1948 (cond
1949 ((not sub))
3d8daefe 1950 ((window-combined-p sub horizontal)
9aab8e0d
MR
1951 ;; In an iso-combination resize subwindows according to their
1952 ;; normal sizes.
5386012d
MR
1953 (window--resize-subwindows
1954 window delta horizontal nil ignore trail edge))
9aab8e0d
MR
1955 ;; In an ortho-combination resize each subwindow by DELTA.
1956 (t
1957 (while sub
5386012d
MR
1958 (window--resize-this-window
1959 sub delta horizontal ignore t trail edge)
9aab8e0d
MR
1960 (setq sub (window-right sub)))))))
1961
5386012d 1962(defun window--resize-root-window (window delta horizontal ignore)
9aab8e0d
MR
1963 "Resize root window WINDOW vertically by DELTA lines.
1964HORIZONTAL non-nil means resize root window WINDOW horizontally
1965by DELTA columns.
1966
1967IGNORE non-nil means ignore any restrictions imposed by fixed
1968size windows, `window-min-height' or `window-min-width' settings.
1969
1970This function is only called by the frame resizing routines. It
1971resizes windows proportionally and never deletes any windows."
1972 (when (and (windowp window) (numberp delta)
1973 (window-sizable-p window delta horizontal ignore))
5386012d
MR
1974 (window--resize-reset (window-frame window) horizontal)
1975 (window--resize-this-window window delta horizontal ignore t)))
9aab8e0d 1976
5386012d 1977(defun window--resize-root-window-vertically (window delta)
9aab8e0d
MR
1978 "Resize root window WINDOW vertically by DELTA lines.
1979If DELTA is less than zero and we can't shrink WINDOW by DELTA
1980lines, shrink it as much as possible. If DELTA is greater than
1981zero, this function can resize fixed-size subwindows in order to
1982recover the necessary lines.
1983
1984Return the number of lines that were recovered.
1985
1986This function is only called by the minibuffer window resizing
1987routines. It resizes windows proportionally and never deletes
1988any windows."
1989 (when (numberp delta)
1990 (let (ignore)
1991 (cond
1992 ((< delta 0)
1993 (setq delta (window-sizable window delta)))
1994 ((> delta 0)
1995 (unless (window-sizable window delta)
1996 (setq ignore t))))
1997
5386012d 1998 (window--resize-reset (window-frame window))
9aab8e0d
MR
1999 ;; Ideally, we would resize just the last window in a combination
2000 ;; but that's not feasible for the following reason: If we grow
2001 ;; the minibuffer window and the last window cannot be shrunk any
2002 ;; more, we shrink another window instead. But if we then shrink
2003 ;; the minibuffer window again, the last window might get enlarged
2004 ;; and the state after shrinking is not the state before growing.
2005 ;; So, in practice, we'd need a history variable to record how to
2006 ;; proceed. But I'm not sure how such a variable could work with
2007 ;; repeated minibuffer window growing steps.
5386012d 2008 (window--resize-this-window window delta nil ignore t)
9aab8e0d
MR
2009 delta)))
2010
562dd5e9
MR
2011(defun adjust-window-trailing-edge (window delta &optional horizontal)
2012 "Move WINDOW's bottom edge by DELTA lines.
2013Optional argument HORIZONTAL non-nil means move WINDOW's right
2014edge by DELTA columns. WINDOW defaults to the selected window.
2015
2016If DELTA is greater zero, then move the edge downwards or to the
2017right. If DELTA is less than zero, move the edge upwards or to
2018the left. If the edge can't be moved by DELTA lines or columns,
2019move it as far as possible in the desired direction."
447f16b8 2020 (setq window (window-normalize-window window))
562dd5e9
MR
2021 (let ((frame (window-frame window))
2022 (right window)
2023 left this-delta min-delta max-delta failed)
2024 ;; Find the edge we want to move.
3d8daefe 2025 (while (and (or (not (window-combined-p right horizontal))
562dd5e9
MR
2026 (not (window-right right)))
2027 (setq right (window-parent right))))
2028 (cond
2029 ((and (not right) (not horizontal) (not resize-mini-windows)
2030 (eq (window-frame (minibuffer-window frame)) frame))
5386012d 2031 (window--resize-mini-window (minibuffer-window frame) (- delta)))
562dd5e9
MR
2032 ((or (not (setq left right)) (not (setq right (window-right right))))
2033 (if horizontal
2034 (error "No window on the right of this one")
2035 (error "No window below this one")))
2036 (t
2037 ;; Set LEFT to the first resizable window on the left. This step is
2038 ;; needed to handle fixed-size windows.
2039 (while (and left (window-size-fixed-p left horizontal))
2040 (setq left
2041 (or (window-left left)
2042 (progn
2043 (while (and (setq left (window-parent left))
3d8daefe 2044 (not (window-combined-p left horizontal))))
562dd5e9
MR
2045 (window-left left)))))
2046 (unless left
2047 (if horizontal
2048 (error "No resizable window on the left of this one")
2049 (error "No resizable window above this one")))
2050
2051 ;; Set RIGHT to the first resizable window on the right. This step
2052 ;; is needed to handle fixed-size windows.
2053 (while (and right (window-size-fixed-p right horizontal))
2054 (setq right
2055 (or (window-right right)
2056 (progn
2057 (while (and (setq right (window-parent right))
3d8daefe 2058 (not (window-combined-p right horizontal))))
562dd5e9
MR
2059 (window-right right)))))
2060 (unless right
2061 (if horizontal
2062 (error "No resizable window on the right of this one")
2063 (error "No resizable window below this one")))
2064
2065 ;; LEFT and RIGHT (which might be both internal windows) are now the
2066 ;; two windows we want to resize.
2067 (cond
2068 ((> delta 0)
2069 (setq max-delta (window-max-delta-1 left 0 horizontal nil 'after))
2070 (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'before))
2071 (when (or (< max-delta delta) (> min-delta (- delta)))
2072 ;; We can't get the whole DELTA - move as far as possible.
2073 (setq delta (min max-delta (- min-delta))))
2074 (unless (zerop delta)
2075 ;; Start resizing.
5386012d 2076 (window--resize-reset frame horizontal)
562dd5e9
MR
2077 ;; Try to enlarge LEFT first.
2078 (setq this-delta (window-resizable left delta horizontal))
2079 (unless (zerop this-delta)
5386012d 2080 (window--resize-this-window
562dd5e9
MR
2081 left this-delta horizontal nil t 'before
2082 (if horizontal
2083 (+ (window-left-column left) (window-total-size left t))
2084 (+ (window-top-line left) (window-total-size left)))))
2085 ;; Shrink windows on right of LEFT.
5386012d 2086 (window--resize-siblings
562dd5e9
MR
2087 left delta horizontal nil 'after
2088 (if horizontal
2089 (window-left-column right)
2090 (window-top-line right)))))
2091 ((< delta 0)
2092 (setq max-delta (window-max-delta-1 right 0 horizontal nil 'before))
2093 (setq min-delta (window-min-delta-1 left delta horizontal nil 'after))
2094 (when (or (< max-delta (- delta)) (> min-delta delta))
2095 ;; We can't get the whole DELTA - move as far as possible.
2096 (setq delta (max (- max-delta) min-delta)))
2097 (unless (zerop delta)
2098 ;; Start resizing.
5386012d 2099 (window--resize-reset frame horizontal)
562dd5e9
MR
2100 ;; Try to enlarge RIGHT.
2101 (setq this-delta (window-resizable right (- delta) horizontal))
2102 (unless (zerop this-delta)
5386012d 2103 (window--resize-this-window
562dd5e9
MR
2104 right this-delta horizontal nil t 'after
2105 (if horizontal
2106 (window-left-column right)
2107 (window-top-line right))))
2108 ;; Shrink windows on left of RIGHT.
5386012d 2109 (window--resize-siblings
562dd5e9
MR
2110 right (- delta) horizontal nil 'before
2111 (if horizontal
2112 (+ (window-left-column left) (window-total-size left t))
2113 (+ (window-top-line left) (window-total-size left)))))))
2114 (unless (zerop delta)
2115 ;; Don't report an error in the standard case.
d615d6d2 2116 (unless (window-resize-apply frame horizontal)
562dd5e9
MR
2117 ;; But do report an error if applying the changes fails.
2118 (error "Failed adjusting window %s" window)))))))
2119
2120(defun enlarge-window (delta &optional horizontal)
2121 "Make selected window DELTA lines taller.
2122Interactively, if no argument is given, make the selected window
2123one line taller. If optional argument HORIZONTAL is non-nil,
2124make selected window wider by DELTA columns. If DELTA is
2125negative, shrink selected window by -DELTA lines or columns.
2126Return nil."
2127 (interactive "p")
67222e1d
MR
2128 (cond
2129 ((zerop delta))
2130 ((window-size-fixed-p nil horizontal)
2131 (error "Selected window has fixed size"))
2132 ((window-resizable-p nil delta horizontal)
d615d6d2 2133 (window-resize nil delta horizontal))
67222e1d 2134 (t
d615d6d2 2135 (window-resize
67222e1d
MR
2136 nil (if (> delta 0)
2137 (window-max-delta nil horizontal)
2138 (- (window-min-delta nil horizontal)))
2139 horizontal))))
562dd5e9
MR
2140
2141(defun shrink-window (delta &optional horizontal)
2142 "Make selected window DELTA lines smaller.
2143Interactively, if no argument is given, make the selected window
2144one line smaller. If optional argument HORIZONTAL is non-nil,
2145make selected window narrower by DELTA columns. If DELTA is
2146negative, enlarge selected window by -DELTA lines or columns.
2147Return nil."
2148 (interactive "p")
67222e1d
MR
2149 (cond
2150 ((zerop delta))
2151 ((window-size-fixed-p nil horizontal)
2152 (error "Selected window has fixed size"))
2153 ((window-resizable-p nil (- delta) horizontal)
d615d6d2 2154 (window-resize nil (- delta) horizontal))
67222e1d 2155 (t
d615d6d2 2156 (window-resize
67222e1d
MR
2157 nil (if (> delta 0)
2158 (- (window-min-delta nil horizontal))
2159 (window-max-delta nil horizontal))
2160 horizontal))))
562dd5e9
MR
2161
2162(defun maximize-window (&optional window)
2163 "Maximize WINDOW.
2164Make WINDOW as large as possible without deleting any windows.
2165WINDOW can be any window and defaults to the selected window."
2166 (interactive)
447f16b8 2167 (setq window (window-normalize-window window))
d615d6d2
MR
2168 (window-resize window (window-max-delta window))
2169 (window-resize window (window-max-delta window t) t))
562dd5e9
MR
2170
2171(defun minimize-window (&optional window)
2172 "Minimize WINDOW.
2173Make WINDOW as small as possible without deleting any windows.
2174WINDOW can be any window and defaults to the selected window."
2175 (interactive)
447f16b8 2176 (setq window (window-normalize-window window))
d615d6d2
MR
2177 (window-resize window (- (window-min-delta window)))
2178 (window-resize window (- (window-min-delta window t)) t))
562dd5e9 2179\f
9aab8e0d
MR
2180(defsubst frame-root-window-p (window)
2181 "Return non-nil if WINDOW is the root window of its frame."
2182 (eq window (frame-root-window window)))
6198ccd0
MR
2183
2184(defun window-tree-1 (window &optional next)
2185 "Return window tree rooted at WINDOW.
2186Optional argument NEXT non-nil means include windows right
2187siblings in the return value.
2188
2189See the documentation of `window-tree' for a description of the
2190return value."
2191 (let (list)
2192 (while window
2193 (setq list
2194 (cons
2195 (cond
d68443dc 2196 ((window-top-child window)
6198ccd0 2197 (cons t (cons (window-edges window)
d68443dc
MR
2198 (window-tree-1 (window-top-child window) t))))
2199 ((window-left-child window)
6198ccd0 2200 (cons nil (cons (window-edges window)
d68443dc 2201 (window-tree-1 (window-left-child window) t))))
6198ccd0
MR
2202 (t window))
2203 list))
d68443dc 2204 (setq window (when next (window-next-sibling window))))
6198ccd0
MR
2205 (nreverse list)))
2206
2207(defun window-tree (&optional frame)
2208 "Return the window tree of frame FRAME.
2209FRAME must be a live frame and defaults to the selected frame.
2210The return value is a list of the form (ROOT MINI), where ROOT
2211represents the window tree of the frame's root window, and MINI
2212is the frame's minibuffer window.
2213
2214If the root window is not split, ROOT is the root window itself.
2215Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2216for a horizontal split, and t for a vertical split. EDGES gives
2217the combined size and position of the subwindows in the split,
2218and the rest of the elements are the subwindows in the split.
2219Each of the subwindows may again be a window or a list
2220representing a window split, and so on. EDGES is a list \(LEFT
2221TOP RIGHT BOTTOM) as returned by `window-edges'."
5386012d 2222 (setq frame (window-normalize-frame frame))
6198ccd0 2223 (window-tree-1 (frame-root-window frame) t))
9aab8e0d 2224\f
9397e56f
MR
2225(defun other-window (count &optional all-frames)
2226 "Select another window in cyclic ordering of windows.
2227COUNT specifies the number of windows to skip, starting with the
2228selected window, before making the selection. If COUNT is
2229positive, skip COUNT windows forwards. If COUNT is negative,
2230skip -COUNT windows backwards. COUNT zero means do not skip any
2231window, so select the selected window. In an interactive call,
2232COUNT is the numeric prefix argument. Return nil.
2233
2234If the `other-window' parameter of WINDOW is a function and
2235`ignore-window-parameters' is nil, call that function with the
2236arguments COUNT and ALL-FRAMES.
2237
2238This function does not select a window whose `no-other-window'
2239window parameter is non-nil.
2240
2241This function uses `next-window' for finding the window to
2242select. The argument ALL-FRAMES has the same meaning as in
2243`next-window', but the MINIBUF argument of `next-window' is
2244always effectively nil."
2245 (interactive "p")
2246 (let* ((window (selected-window))
2247 (function (and (not ignore-window-parameters)
2248 (window-parameter window 'other-window)))
2249 old-window old-count)
2250 (if (functionp function)
2251 (funcall function count all-frames)
2252 ;; `next-window' and `previous-window' may return a window we are
2253 ;; not allowed to select. Hence we need an exit strategy in case
2254 ;; all windows are non-selectable.
2255 (catch 'exit
2256 (while (> count 0)
2257 (setq window (next-window window nil all-frames))
2258 (cond
2259 ((eq window old-window)
2260 (when (= count old-count)
2261 ;; Keep out of infinite loops. When COUNT has not changed
2262 ;; since we last looked at `window' we're probably in one.
2263 (throw 'exit nil)))
2264 ((window-parameter window 'no-other-window)
2265 (unless old-window
2266 ;; The first non-selectable window `next-window' got us:
2267 ;; Remember it and the current value of COUNT.
2268 (setq old-window window)
2269 (setq old-count count)))
2270 (t
2271 (setq count (1- count)))))
2272 (while (< count 0)
2273 (setq window (previous-window window nil all-frames))
2274 (cond
2275 ((eq window old-window)
2276 (when (= count old-count)
2277 ;; Keep out of infinite loops. When COUNT has not changed
2278 ;; since we last looked at `window' we're probably in one.
2279 (throw 'exit nil)))
2280 ((window-parameter window 'no-other-window)
2281 (unless old-window
2282 ;; The first non-selectable window `previous-window' got
2283 ;; us: Remember it and the current value of COUNT.
2284 (setq old-window window)
2285 (setq old-count count)))
2286 (t
2287 (setq count (1+ count)))))
2288
2289 (select-window window)
2290 ;; Always return nil.
2291 nil))))
2292
387522b2
MR
2293;; This should probably return non-nil when the selected window is part
2294;; of an atomic window whose root is the frame's root window.
2295(defun one-window-p (&optional nomini all-frames)
2296 "Return non-nil if the selected window is the only window.
2297Optional arg NOMINI non-nil means don't count the minibuffer
2298even if it is active. Otherwise, the minibuffer is counted
2299when it is active.
2300
2301Optional argument ALL-FRAMES specifies the set of frames to
2302consider, see also `next-window'. ALL-FRAMES nil or omitted
2303means consider windows on the selected frame only, plus the
2304minibuffer window if specified by the NOMINI argument. If the
2305minibuffer counts, consider all windows on all frames that share
2306that minibuffer too. The remaining non-nil values of ALL-FRAMES
2307with a special meaning are:
2308
2309- t means consider all windows on all existing frames.
2310
2311- `visible' means consider all windows on all visible frames on
2312 the current terminal.
2313
2314- 0 (the number zero) means consider all windows on all visible
2315 and iconified frames on the current terminal.
2316
2317- A frame means consider all windows on that frame only.
2318
2319Anything else means consider all windows on the selected frame
2320and no others."
2321 (let ((base-window (selected-window)))
2322 (if (and nomini (eq base-window (minibuffer-window)))
2323 (setq base-window (next-window base-window)))
2324 (eq base-window
2325 (next-window base-window (if nomini 'arg) all-frames))))
3c448ab6 2326\f
9aab8e0d 2327;;; Deleting windows.
1b36ed6a 2328(defun window-deletable-p (&optional window)
9aab8e0d 2329 "Return t if WINDOW can be safely deleted from its frame.
e9d90883 2330Return `frame' if deleting WINDOW should also delete its
1b36ed6a 2331frame."
447f16b8 2332 (setq window (window-normalize-window window))
8b0874b5 2333
9aab8e0d
MR
2334 (unless ignore-window-parameters
2335 ;; Handle atomicity.
2336 (when (window-parameter window 'window-atom)
2337 (setq window (window-atom-root window))))
8b0874b5
MR
2338
2339 (let* ((parent (window-parent window))
2340 (frame (window-frame window))
1b36ed6a 2341 (buffer (window-buffer window)))
9aab8e0d
MR
2342 (cond
2343 ((frame-root-window-p window)
85e9c04b
CY
2344 ;; WINDOW's frame can be deleted only if there are other frames
2345 ;; on the same terminal.
2346 (unless (eq frame (next-frame frame 0))
2347 'frame))
1b36ed6a
MR
2348 ((or ignore-window-parameters
2349 (not (eq (window-parameter window 'window-side) 'none))
2350 (and parent (eq (window-parameter parent 'window-side) 'none)))
2351 ;; WINDOW can be deleted unless it is the main window of its
2352 ;; frame.
1f3c99ca 2353 t))))
9aab8e0d
MR
2354
2355(defun window-or-subwindow-p (subwindow window)
2356 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2357 (or (eq subwindow window)
2358 (let ((parent (window-parent subwindow)))
2359 (catch 'done
2360 (while parent
2361 (if (eq parent window)
2362 (throw 'done t)
2363 (setq parent (window-parent parent))))))))
2364
562dd5e9
MR
2365(defun delete-window (&optional window)
2366 "Delete WINDOW.
2367WINDOW can be an arbitrary window and defaults to the selected
2368one. Return nil.
2369
2370If the variable `ignore-window-parameters' is non-nil or the
2371`delete-window' parameter of WINDOW equals t, do not process any
2372parameters of WINDOW. Otherwise, if the `delete-window'
2373parameter of WINDOW specifies a function, call that function with
2374WINDOW as its sole argument and return the value returned by that
2375function.
2376
2377Otherwise, if WINDOW is part of an atomic window, call
2378`delete-window' with the root of the atomic window as its
2379argument. If WINDOW is the only window on its frame or the last
2380non-side window, signal an error."
2381 (interactive)
447f16b8 2382 (setq window (window-normalize-window window))
562dd5e9
MR
2383 (let* ((frame (window-frame window))
2384 (function (window-parameter window 'delete-window))
2385 (parent (window-parent window))
2386 atom-root)
2387 (window-check frame)
2388 (catch 'done
2389 ;; Handle window parameters.
2390 (cond
2391 ;; Ignore window parameters if `ignore-window-parameters' tells
2392 ;; us so or `delete-window' equals t.
2393 ((or ignore-window-parameters (eq function t)))
2394 ((functionp function)
2395 ;; The `delete-window' parameter specifies the function to call.
2396 ;; If that function is `ignore' nothing is done. It's up to the
2397 ;; function called here to avoid infinite recursion.
2398 (throw 'done (funcall function window)))
2399 ((and (window-parameter window 'window-atom)
2400 (setq atom-root (window-atom-root window))
2401 (not (eq atom-root window)))
2402 (throw 'done (delete-window atom-root)))
2403 ((and (eq (window-parameter window 'window-side) 'none)
2404 (or (not parent)
2405 (not (eq (window-parameter parent 'window-side) 'none))))
2406 (error "Attempt to delete last non-side window"))
2407 ((not parent)
2408 (error "Attempt to delete minibuffer or sole ordinary window")))
2409
d68443dc 2410 (let* ((horizontal (window-left-child parent))
562dd5e9
MR
2411 (size (window-total-size window horizontal))
2412 (frame-selected
2413 (window-or-subwindow-p (frame-selected-window frame) window))
2414 ;; Emacs 23 preferably gives WINDOW's space to its left
2415 ;; sibling.
2416 (sibling (or (window-left window) (window-right window))))
5386012d 2417 (window--resize-reset frame horizontal)
562dd5e9
MR
2418 (cond
2419 ((and (not (window-splits window))
2420 sibling (window-sizable-p sibling size))
2421 ;; Resize WINDOW's sibling.
5386012d 2422 (window--resize-this-window sibling size horizontal nil t)
562dd5e9
MR
2423 (set-window-new-normal
2424 sibling (+ (window-normal-size sibling horizontal)
2425 (window-normal-size window horizontal))))
2426 ((window-resizable-p window (- size) horizontal nil nil nil t)
2427 ;; Can do without resizing fixed-size windows.
5386012d 2428 (window--resize-siblings window (- size) horizontal))
562dd5e9
MR
2429 (t
2430 ;; Can't do without resizing fixed-size windows.
5386012d 2431 (window--resize-siblings window (- size) horizontal t)))
562dd5e9
MR
2432 ;; Actually delete WINDOW.
2433 (delete-window-internal window)
2434 (when (and frame-selected
2435 (window-parameter
2436 (frame-selected-window frame) 'no-other-window))
2437 ;; `delete-window-internal' has selected a window that should
2438 ;; not be selected, fix this here.
2439 (other-window -1 frame))
2440 (run-window-configuration-change-hook frame)
2441 (window-check frame)
2442 ;; Always return nil.
2443 nil))))
2444
2445(defun delete-other-windows (&optional window)
2446 "Make WINDOW fill its frame.
2447WINDOW may be any window and defaults to the selected one.
2448Return nil.
2449
2450If the variable `ignore-window-parameters' is non-nil or the
2451`delete-other-windows' parameter of WINDOW equals t, do not
2452process any parameters of WINDOW. Otherwise, if the
2453`delete-other-windows' parameter of WINDOW specifies a function,
2454call that function with WINDOW as its sole argument and return
2455the value returned by that function.
2456
2457Otherwise, if WINDOW is part of an atomic window, call this
2458function with the root of the atomic window as its argument. If
2459WINDOW is a non-side window, make WINDOW the only non-side window
2460on the frame. Side windows are not deleted. If WINDOW is a side
2461window signal an error."
2462 (interactive)
447f16b8 2463 (setq window (window-normalize-window window))
562dd5e9
MR
2464 (let* ((frame (window-frame window))
2465 (function (window-parameter window 'delete-other-windows))
2466 (window-side (window-parameter window 'window-side))
2467 atom-root side-main)
2468 (window-check frame)
2469 (catch 'done
2470 (cond
2471 ;; Ignore window parameters if `ignore-window-parameters' is t or
2472 ;; `delete-other-windows' is t.
2473 ((or ignore-window-parameters (eq function t)))
2474 ((functionp function)
2475 ;; The `delete-other-windows' parameter specifies the function
2476 ;; to call. If the function is `ignore' no windows are deleted.
2477 ;; It's up to the function called to avoid infinite recursion.
2478 (throw 'done (funcall function window)))
2479 ((and (window-parameter window 'window-atom)
2480 (setq atom-root (window-atom-root window))
2481 (not (eq atom-root window)))
2482 (throw 'done (delete-other-windows atom-root)))
2483 ((eq window-side 'none)
2484 ;; Set side-main to the major non-side window.
2485 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2486 ((memq window-side window-sides)
2487 (error "Cannot make side window the only window")))
2488 ;; If WINDOW is the main non-side window, do nothing.
2489 (unless (eq window side-main)
2490 (delete-other-windows-internal window side-main)
2491 (run-window-configuration-change-hook frame)
2492 (window-check frame))
2493 ;; Always return nil.
2494 nil)))
9397e56f
MR
2495
2496(defun delete-other-windows-vertically (&optional window)
2497 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2498This may be a useful alternative binding for \\[delete-other-windows]
2499 if you often split windows horizontally."
2500 (interactive)
2501 (let* ((window (or window (selected-window)))
2502 (edges (window-edges window))
2503 (w window) delenda)
2504 (while (not (eq (setq w (next-window w 1)) window))
2505 (let ((e (window-edges w)))
2506 (when (and (= (car e) (car edges))
2507 (= (caddr e) (caddr edges)))
2508 (push w delenda))))
2509 (mapc 'delete-window delenda)))
2510
2511;;; Windows and buffers.
2512
2513;; `prev-buffers' and `next-buffers' are two reserved window slots used
2514;; for (1) determining which buffer to show in the window when its
2515;; buffer shall be buried or killed and (2) which buffer to show for
2516;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2517
2518;; `prev-buffers' consists of <buffer, window-start, window-point>
2519;; triples. The entries on this list are ordered by the time their
2520;; buffer has been removed from the window, the most recently removed
2521;; buffer's entry being first. The window-start and window-point
2522;; components are `window-start' and `window-point' at the time the
2523;; buffer was removed from the window which implies that the entry must
2524;; be added when `set-window-buffer' removes the buffer from the window.
2525
2526;; `next-buffers' is the list of buffers that have been replaced
2527;; recently by `switch-to-prev-buffer'. These buffers are the least
2528;; preferred candidates of `switch-to-prev-buffer' and the preferred
2529;; candidates of `switch-to-next-buffer' to switch to. This list is
2530;; reset to nil by any action changing the window's buffer with the
2531;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2532;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2533;; `switch-to-next-buffer' pops the last pushed buffer from it.
2534
2535;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2536;; if such a buffer was killed while the window was hidden within a
2537;; window configuration. Such killed buffers get removed whenever
2538;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2539
2540;; The following function is called by `set-window-buffer' _before_ it
2541;; replaces the buffer of the argument window with the new buffer.
2542(defun record-window-buffer (&optional window)
2543 "Record WINDOW's buffer.
2544WINDOW must be a live window and defaults to the selected one."
447f16b8 2545 (let* ((window (window-normalize-window window t))
9397e56f
MR
2546 (buffer (window-buffer window))
2547 (entry (assq buffer (window-prev-buffers window))))
2548 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2549 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2550 (set-window-next-buffers window nil)
2551
2552 (when entry
2553 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2554 (set-window-prev-buffers
2555 window (assq-delete-all buffer (window-prev-buffers window))))
2556
2557 ;; Don't record insignificant buffers.
2558 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2559 ;; Add an entry for buffer to WINDOW's previous buffers.
2560 (with-current-buffer buffer
2561 (let ((start (window-start window))
c96111ea 2562 (point (window-point-1 window)))
9397e56f
MR
2563 (setq entry
2564 (cons buffer
2565 (if entry
2566 ;; We have an entry, update marker positions.
2567 (list (set-marker (nth 1 entry) start)
2568 (set-marker (nth 2 entry) point))
2569 ;; Make new markers.
2570 (list (copy-marker start)
2571 (copy-marker point)))))
2572
2573 (set-window-prev-buffers
2574 window (cons entry (window-prev-buffers window))))))))
2575
2576(defun unrecord-window-buffer (&optional window buffer)
2577 "Unrecord BUFFER in WINDOW.
2578WINDOW must be a live window and defaults to the selected one.
2579BUFFER must be a live buffer and defaults to the buffer of
2580WINDOW."
447f16b8 2581 (let* ((window (window-normalize-window window t))
9397e56f
MR
2582 (buffer (or buffer (window-buffer window))))
2583 (set-window-prev-buffers
2584 window (assq-delete-all buffer (window-prev-buffers window)))
2585 (set-window-next-buffers
2586 window (delq buffer (window-next-buffers window)))))
2587
2588(defun set-window-buffer-start-and-point (window buffer &optional start point)
2589 "Set WINDOW's buffer to BUFFER.
2590Optional argument START non-nil means set WINDOW's start position
2591to START. Optional argument POINT non-nil means set WINDOW's
2592point to POINT. If WINDOW is selected this also sets BUFFER's
2593`point' to POINT. If WINDOW is selected and the buffer it showed
2594before was current this also makes BUFFER the current buffer."
2595 (let ((selected (eq window (selected-window)))
2596 (current (eq (window-buffer window) (current-buffer))))
2597 (set-window-buffer window buffer)
2598 (when (and selected current)
2599 (set-buffer buffer))
2600 (when start
cf4eacfd
MR
2601 ;; Don't force window-start here (even if POINT is nil).
2602 (set-window-start window start t))
9397e56f 2603 (when point
c96111ea 2604 (set-window-point-1 window point))))
9397e56f
MR
2605
2606(defun switch-to-prev-buffer (&optional window bury-or-kill)
2607 "In WINDOW switch to previous buffer.
2608WINDOW must be a live window and defaults to the selected one.
2609
2610Optional argument BURY-OR-KILL non-nil means the buffer currently
2611shown in WINDOW is about to be buried or killed and consequently
2612shall not be switched to in future invocations of this command."
2613 (interactive)
447f16b8 2614 (let* ((window (window-normalize-window window t))
9397e56f
MR
2615 (old-buffer (window-buffer window))
2616 ;; Save this since it's destroyed by `set-window-buffer'.
2617 (next-buffers (window-next-buffers window))
1b36ed6a
MR
2618 entry new-buffer killed-buffers visible)
2619 (when (window-dedicated-p window)
2620 (error "Window %s is dedicated to buffer %s" window old-buffer))
2621
2622 (catch 'found
2623 ;; Scan WINDOW's previous buffers first, skipping entries of next
2624 ;; buffers.
2625 (dolist (entry (window-prev-buffers window))
2626 (when (and (setq new-buffer (car entry))
2627 (or (buffer-live-p new-buffer)
2628 (not (setq killed-buffers
2629 (cons new-buffer killed-buffers))))
2630 (not (eq new-buffer old-buffer))
2631 (or bury-or-kill
2632 (not (memq new-buffer next-buffers))))
2633 (set-window-buffer-start-and-point
2634 window new-buffer (nth 1 entry) (nth 2 entry))
2635 (throw 'found t)))
2636 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2637 ;; entries of next buffers. Note that when we bury or kill a
2638 ;; buffer we don't reverse the global buffer list to avoid showing
2639 ;; a buried buffer instead. Otherwise, we must reverse the global
2640 ;; buffer list in order to make sure that switching to the
2641 ;; previous/next buffer traverse it in opposite directions.
2642 (dolist (buffer (if bury-or-kill
2643 (buffer-list (window-frame window))
2644 (nreverse (buffer-list (window-frame window)))))
2645 (when (and (buffer-live-p buffer)
2646 (not (eq buffer old-buffer))
2647 (not (eq (aref (buffer-name buffer) 0) ?\s))
2648 (or bury-or-kill (not (memq buffer next-buffers))))
2649 (if (get-buffer-window buffer)
2650 ;; Try to avoid showing a buffer visible in some other window.
2651 (setq visible buffer)
2652 (setq new-buffer buffer)
2653 (set-window-buffer-start-and-point window new-buffer)
2654 (throw 'found t))))
2655 (unless bury-or-kill
2656 ;; Scan reverted next buffers last (must not use nreverse
2657 ;; here!).
2658 (dolist (buffer (reverse next-buffers))
2659 ;; Actually, buffer _must_ be live here since otherwise it
2660 ;; would have been caught in the scan of previous buffers.
2661 (when (and (or (buffer-live-p buffer)
9397e56f 2662 (not (setq killed-buffers
1b36ed6a 2663 (cons buffer killed-buffers))))
9397e56f 2664 (not (eq buffer old-buffer))
1b36ed6a 2665 (setq entry (assq buffer (window-prev-buffers window))))
9397e56f 2666 (setq new-buffer buffer)
1b36ed6a
MR
2667 (set-window-buffer-start-and-point
2668 window new-buffer (nth 1 entry) (nth 2 entry))
9397e56f 2669 (throw 'found t))))
1b36ed6a
MR
2670
2671 ;; Show a buffer visible in another window.
2672 (when visible
2673 (setq new-buffer visible)
2674 (set-window-buffer-start-and-point window new-buffer)))
2675
2676 (if bury-or-kill
2677 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2678 ;; of) next buffers.
2679 (progn
2680 (set-window-prev-buffers
2681 window (assq-delete-all old-buffer (window-prev-buffers window)))
2682 (set-window-next-buffers window (delq old-buffer next-buffers)))
2683 ;; Move `old-buffer' to head of WINDOW's restored list of next
2684 ;; buffers.
2685 (set-window-next-buffers
2686 window (cons old-buffer (delq old-buffer next-buffers))))
9397e56f
MR
2687
2688 ;; Remove killed buffers from WINDOW's previous and next buffers.
2689 (when killed-buffers
2690 (dolist (buffer killed-buffers)
2691 (set-window-prev-buffers
2692 window (assq-delete-all buffer (window-prev-buffers window)))
2693 (set-window-next-buffers
2694 window (delq buffer (window-next-buffers window)))))
2695
2696 ;; Return new-buffer.
2697 new-buffer))
2698
2699(defun switch-to-next-buffer (&optional window)
2700 "In WINDOW switch to next buffer.
2701WINDOW must be a live window and defaults to the selected one."
2702 (interactive)
447f16b8 2703 (let* ((window (window-normalize-window window t))
9397e56f
MR
2704 (old-buffer (window-buffer window))
2705 (next-buffers (window-next-buffers window))
2706 new-buffer entry killed-buffers visible)
2707 (when (window-dedicated-p window)
2708 (error "Window %s is dedicated to buffer %s" window old-buffer))
2709
2710 (catch 'found
2711 ;; Scan WINDOW's next buffers first.
2712 (dolist (buffer next-buffers)
2713 (when (and (or (buffer-live-p buffer)
2714 (not (setq killed-buffers
2715 (cons buffer killed-buffers))))
2716 (not (eq buffer old-buffer))
2717 (setq entry (assq buffer (window-prev-buffers window))))
2718 (setq new-buffer buffer)
2719 (set-window-buffer-start-and-point
2720 window new-buffer (nth 1 entry) (nth 2 entry))
2721 (throw 'found t)))
2722 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2723 ;; buffers entries.
2724 (dolist (buffer (buffer-list (window-frame window)))
2725 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2726 (not (eq (aref (buffer-name buffer) 0) ?\s))
2727 (not (assq buffer (window-prev-buffers window))))
2728 (if (get-buffer-window buffer)
2729 ;; Try to avoid showing a buffer visible in some other window.
2730 (setq visible buffer)
2731 (setq new-buffer buffer)
2732 (set-window-buffer-start-and-point window new-buffer)
2733 (throw 'found t))))
2734 ;; Scan WINDOW's reverted previous buffers last (must not use
2735 ;; nreverse here!)
2736 (dolist (entry (reverse (window-prev-buffers window)))
2737 (when (and (setq new-buffer (car entry))
2738 (or (buffer-live-p new-buffer)
2739 (not (setq killed-buffers
2740 (cons new-buffer killed-buffers))))
2741 (not (eq new-buffer old-buffer)))
2742 (set-window-buffer-start-and-point
2743 window new-buffer (nth 1 entry) (nth 2 entry))
2744 (throw 'found t)))
2745
2746 ;; Show a buffer visible in another window.
2747 (when visible
2748 (setq new-buffer visible)
2749 (set-window-buffer-start-and-point window new-buffer)))
2750
2751 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2752 (set-window-next-buffers window (delq new-buffer next-buffers))
2753
2754 ;; Remove killed buffers from WINDOW's previous and next buffers.
2755 (when killed-buffers
2756 (dolist (buffer killed-buffers)
2757 (set-window-prev-buffers
2758 window (assq-delete-all buffer (window-prev-buffers window)))
2759 (set-window-next-buffers
2760 window (delq buffer (window-next-buffers window)))))
2761
2762 ;; Return new-buffer.
2763 new-buffer))
2764
2765(defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2766 "Search LIST for a valid buffer to display in FRAME.
2767Return nil when all buffers in LIST are undesirable for display,
2768otherwise return the first suitable buffer in LIST.
2769
2770Buffers not visible in windows are preferred to visible buffers,
2771unless VISIBLE-OK is non-nil.
2772If the optional argument FRAME is nil, it defaults to the selected frame.
2773If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2774 ;; This logic is more or less copied from other-buffer.
2775 (setq frame (or frame (selected-frame)))
2776 (let ((pred (frame-parameter frame 'buffer-predicate))
2777 found buf)
2778 (while (and (not found) list)
2779 (setq buf (car list))
2780 (if (and (not (eq buffer buf))
2781 (buffer-live-p buf)
2782 (or (null pred) (funcall pred buf))
2783 (not (eq (aref (buffer-name buf) 0) ?\s))
2784 (or visible-ok (null (get-buffer-window buf 'visible))))
2785 (setq found buf)
2786 (setq list (cdr list))))
2787 (car list)))
2788
2789(defun last-buffer (&optional buffer visible-ok frame)
2790 "Return the last buffer in FRAME's buffer list.
2791If BUFFER is the last buffer, return the preceding buffer
2792instead. Buffers not visible in windows are preferred to visible
2793buffers, unless optional argument VISIBLE-OK is non-nil.
2794Optional third argument FRAME nil or omitted means use the
2795selected frame's buffer list. If no such buffer exists, return
2796the buffer `*scratch*', creating it if necessary."
2797 (setq frame (or frame (selected-frame)))
2798 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2799 buffer visible-ok frame)
2800 (get-buffer "*scratch*")
2801 (let ((scratch (get-buffer-create "*scratch*")))
2802 (set-buffer-major-mode scratch)
2803 scratch)))
2804
5a4cf282
MR
2805(defcustom frame-auto-hide-function #'iconify-frame
2806 "Function called to automatically hide frames.
2807The function is called with one argument - a frame.
2808
2809Functions affected by this option are those that bury a buffer
2810shown in a separate frame like `quit-window' and `bury-buffer'."
2811 :type '(choice (const :tag "Iconify" iconify-frame)
2812 (const :tag "Delete" delete-frame)
2813 (const :tag "Do nothing" ignore)
2814 function)
2815 :group 'windows
49677495
MR
2816 :group 'frames
2817 :version "24.1")
0e2070b5
MR
2818
2819(defun window--delete (&optional window dedicated-only kill)
2820 "Delete WINDOW if possible.
2821WINDOW must be a live window and defaults to the selected one.
2822Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
2823only if it's dedicated to its buffer. Optional argument KILL
2824means the buffer shown in window will be killed. Return non-nil
c557cd6b 2825if WINDOW gets deleted or its frame is auto-hidden."
447f16b8 2826 (setq window (window-normalize-window window t))
0e2070b5
MR
2827 (unless (and dedicated-only (not (window-dedicated-p window)))
2828 (let* ((buffer (window-buffer window))
2829 (deletable (window-deletable-p window)))
2830 (cond
2831 ((eq deletable 'frame)
2832 (let ((frame (window-frame window)))
c557cd6b
MR
2833 (cond
2834 (kill
2835 (delete-frame frame))
2836 ((functionp frame-auto-hide-function)
2837 (funcall frame-auto-hide-function frame))))
0e2070b5
MR
2838 'frame)
2839 (deletable
2840 (delete-window window)
2841 t)))))
2842
9397e56f
MR
2843(defun bury-buffer (&optional buffer-or-name)
2844 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2845There it is the least likely candidate for `other-buffer' to
2846return; thus, the least likely buffer for \\[switch-to-buffer] to
2847select by default.
2848
2849You can specify a buffer name as BUFFER-OR-NAME, or an actual
2850buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2851current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2852remove the current buffer from the selected window if it is
2853displayed there."
2854 (interactive)
5386012d 2855 (let* ((buffer (window-normalize-buffer buffer-or-name)))
9397e56f
MR
2856 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2857 ;; although it's not "here" (call it a feature).
e4ed06f1 2858 (bury-buffer-internal buffer)
9397e56f
MR
2859 ;; Handle case where `buffer-or-name' is nil and the current buffer
2860 ;; is shown in the selected window.
2861 (cond
2862 ((or buffer-or-name (not (eq buffer (window-buffer)))))
0e2070b5
MR
2863 ((window--delete nil t))
2864 (t
2865 ;; Switch to another buffer in window.
2866 (set-window-dedicated-p nil nil)
1885e5b8 2867 (switch-to-prev-buffer nil 'bury)))
1b36ed6a 2868
9397e56f
MR
2869 ;; Always return nil.
2870 nil))
2871
2872(defun unbury-buffer ()
2873 "Switch to the last buffer in the buffer list."
2874 (interactive)
2875 (switch-to-buffer (last-buffer)))
2876
2877(defun next-buffer ()
2878 "In selected window switch to next buffer."
2879 (interactive)
e14b388a
CY
2880 (if (window-minibuffer-p)
2881 (error "Cannot switch buffers in minibuffer window"))
9397e56f
MR
2882 (switch-to-next-buffer))
2883
2884(defun previous-buffer ()
2885 "In selected window switch to previous buffer."
2886 (interactive)
e14b388a
CY
2887 (if (window-minibuffer-p)
2888 (error "Cannot switch buffers in minibuffer window"))
9397e56f
MR
2889 (switch-to-prev-buffer))
2890
2891(defun delete-windows-on (&optional buffer-or-name frame)
2892 "Delete all windows showing BUFFER-OR-NAME.
2893BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2894and defaults to the current buffer.
2895
2896The following non-nil values of the optional argument FRAME
2897have special meanings:
2898
2899- t means consider all windows on the selected frame only.
2900
2901- `visible' means consider all windows on all visible frames on
2902 the current terminal.
2903
2904- 0 (the number zero) means consider all windows on all visible
2905 and iconified frames on the current terminal.
2906
2907- A frame means consider all windows on that frame only.
2908
2909Any other value of FRAME means consider all windows on all
2910frames.
2911
2912When a window showing BUFFER-OR-NAME is dedicated and the only
2913window of its frame, that frame is deleted when there are other
2914frames left."
2915 (interactive "BDelete windows on (buffer):\nP")
5386012d 2916 (let ((buffer (window-normalize-buffer buffer-or-name))
9397e56f
MR
2917 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2918 ;; `window-list-1' based function.
2919 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2920 (dolist (window (window-list-1 nil nil all-frames))
2921 (if (eq (window-buffer window) buffer)
1b36ed6a 2922 (let ((deletable (window-deletable-p window)))
9397e56f 2923 (cond
1b36ed6a
MR
2924 ((and (eq deletable 'frame) (window-dedicated-p window))
2925 ;; Delete frame if and only if window is dedicated.
9397e56f 2926 (delete-frame (window-frame window)))
1b36ed6a
MR
2927 ((eq deletable t)
2928 ;; Delete window.
9397e56f
MR
2929 (delete-window window))
2930 (t
2931 ;; In window switch to previous buffer.
2932 (set-window-dedicated-p window nil)
2933 (switch-to-prev-buffer window 'bury))))
2934 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
2935 (unrecord-window-buffer window buffer)))))
2936
2937(defun replace-buffer-in-windows (&optional buffer-or-name)
2938 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2939BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2940and defaults to the current buffer.
2941
0e2070b5
MR
2942When a window showing BUFFER-OR-NAME is dedicated, that window is
2943deleted. If that window is the only window on its frame, the
2944frame is deleted too when there are other frames left. If there
2945are no other frames left, some other buffer is displayed in that
2946window.
9397e56f
MR
2947
2948This function removes the buffer denoted by BUFFER-OR-NAME from
2949all window-local buffer lists."
5386012d 2950 (let ((buffer (window-normalize-buffer buffer-or-name)))
9397e56f
MR
2951 (dolist (window (window-list-1 nil nil t))
2952 (if (eq (window-buffer window) buffer)
0e2070b5
MR
2953 (unless (window--delete window t t)
2954 ;; Switch to another buffer in window.
2955 (set-window-dedicated-p window nil)
2956 (switch-to-prev-buffer window 'kill))
9397e56f
MR
2957 ;; Unrecord BUFFER in WINDOW.
2958 (unrecord-window-buffer window buffer)))))
2959
1ed43b09
CY
2960(defun quit-window (&optional kill window)
2961 "Quit WINDOW and bury its buffer.
cf4eacfd
MR
2962WINDOW must be a live window and defaults to the selected one.
2963With prefix argument KILL non-nil, kill the buffer instead of
2964burying it.
9397e56f
MR
2965
2966According to information stored in WINDOW's `quit-restore' window
2967parameter either \(1) delete WINDOW and its frame, \(2) delete
2968WINDOW, \(3) restore the buffer previously displayed in WINDOW,
2969or \(4) make WINDOW display some other buffer than the present
1ed43b09
CY
2970one. If non-nil, reset `quit-restore' parameter to nil."
2971 (interactive "P")
447f16b8 2972 (setq window (window-normalize-window window t))
cf4eacfd
MR
2973 (let* ((buffer (window-buffer window))
2974 (quit-restore (window-parameter window 'quit-restore))
2975 (prev-buffer
2976 (let* ((prev-buffers (window-prev-buffers window))
2977 (prev-buffer (caar prev-buffers)))
2978 (and (or (not (eq prev-buffer buffer))
2979 (and (cdr prev-buffers)
2980 (not (eq (setq prev-buffer (cadr prev-buffers))
2981 buffer))))
2982 prev-buffer)))
2983 quad resize)
9397e56f 2984 (cond
cf4eacfd 2985 ((and (not prev-buffer)
0e2070b5
MR
2986 (memq (nth 1 quit-restore) '(window frame))
2987 (eq (nth 3 quit-restore) buffer)
2988 ;; Delete WINDOW if possible.
2989 (window--delete window nil kill))
9397e56f
MR
2990 ;; If the previously selected window is still alive, select it.
2991 (when (window-live-p (nth 2 quit-restore))
2992 (select-window (nth 2 quit-restore))))
cf4eacfd
MR
2993 ((and (listp (setq quad (nth 1 quit-restore)))
2994 (buffer-live-p (car quad))
2995 (eq (nth 3 quit-restore) buffer))
2996 ;; Show another buffer stored in quit-restore parameter.
2997 (setq resize (with-current-buffer buffer
2998 (and temp-buffer-resize-mode
2999 (/= (nth 3 quad) (window-total-size window)))))
9397e56f 3000 (set-window-dedicated-p window nil)
cf4eacfd
MR
3001 (when resize
3002 ;; Try to resize WINDOW to its old height but don't signal an
3003 ;; error.
3004 (condition-case nil
3005 (window-resize window (- (nth 3 quad) (window-total-size window)))
3006 (error nil)))
1885e5b8 3007 ;; Restore WINDOW's previous buffer, start and point position.
cf4eacfd
MR
3008 (set-window-buffer-start-and-point
3009 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
1885e5b8
MR
3010 ;; Unrecord WINDOW's buffer here (Bug#9937) to make sure it's not
3011 ;; re-recorded by `set-window-buffer'.
3012 (unrecord-window-buffer window buffer)
9397e56f
MR
3013 ;; Reset the quit-restore parameter.
3014 (set-window-parameter window 'quit-restore nil)
cf4eacfd
MR
3015 ;; Select old window.
3016 (when (window-live-p (nth 2 quit-restore))
3017 (select-window (nth 2 quit-restore))))
9397e56f 3018 (t
cf4eacfd
MR
3019 ;; Show some other buffer in WINDOW and reset the quit-restore
3020 ;; parameter.
9397e56f 3021 (set-window-parameter window 'quit-restore nil)
b4d72fcf
MR
3022 ;; Make sure that WINDOW is no more dedicated.
3023 (set-window-dedicated-p window nil)
9397e56f
MR
3024 (switch-to-prev-buffer window 'bury-or-kill)))
3025
3026 ;; Kill WINDOW's old-buffer if requested
e4ed06f1
CY
3027 (if kill
3028 (kill-buffer buffer)
3029 (bury-buffer-internal buffer))))
366ca7f3
MR
3030
3031(defun quit-windows-on (&optional buffer-or-name kill frame)
3032 "Quit all windows showing BUFFER-OR-NAME.
3033BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3034and defaults to the current buffer. Optional argument KILL
3035non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3036BUFFER-OR-NAME. Optional argument FRAME is handled as by
3037`delete-windows-on'.
3038
3039This function calls `quit-window' on all candidate windows
3040showing BUFFER-OR-NAME."
3041 (interactive "BQuit windows on (buffer):\nP")
3042 (let ((buffer (window-normalize-buffer buffer-or-name))
3043 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3044 ;; `window-list-1' based function.
3045 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3046 (dolist (window (window-list-1 nil nil all-frames))
3047 (if (eq (window-buffer window) buffer)
3048 (quit-window kill window)
3049 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3050 (unrecord-window-buffer window buffer)))))
562dd5e9
MR
3051\f
3052;;; Splitting windows.
3053(defsubst window-split-min-size (&optional horizontal)
3054 "Return minimum height of any window when splitting windows.
3055Optional argument HORIZONTAL non-nil means return minimum width."
3056 (if horizontal
3057 (max window-min-width window-safe-min-width)
3058 (max window-min-height window-safe-min-height)))
3059
3060(defun split-window (&optional window size side)
3061 "Make a new window adjacent to WINDOW.
3062WINDOW can be any window and defaults to the selected one.
3063Return the new window which is always a live window.
3064
3065Optional argument SIZE a positive number means make WINDOW SIZE
3066lines or columns tall. If SIZE is negative, make the new window
3067-SIZE lines or columns tall. If and only if SIZE is non-nil, its
3068absolute value can be less than `window-min-height' or
3069`window-min-width'; so this command can make a new window as
3070small as one line or two columns. SIZE defaults to half of
3071WINDOW's size. Interactively, SIZE is the prefix argument.
3072
3073Optional third argument SIDE nil (or `below') specifies that the
3074new window shall be located below WINDOW. SIDE `above' means the
3075new window shall be located above WINDOW. In both cases SIZE
3076specifies the new number of lines for WINDOW \(or the new window
3077if SIZE is negative) including space reserved for the mode and/or
3078header line.
3079
3080SIDE t (or `right') specifies that the new window shall be
3081located on the right side of WINDOW. SIDE `left' means the new
3082window shall be located on the left of WINDOW. In both cases
3083SIZE specifies the new number of columns for WINDOW \(or the new
3084window provided SIZE is negative) including space reserved for
3085fringes and the scrollbar or a divider column. Any other non-nil
3086value for SIDE is currently handled like t (or `right').
3087
3088If the variable `ignore-window-parameters' is non-nil or the
3089`split-window' parameter of WINDOW equals t, do not process any
3090parameters of WINDOW. Otherwise, if the `split-window' parameter
3091of WINDOW specifies a function, call that function with all three
3092arguments and return the value returned by that function.
3093
3094Otherwise, if WINDOW is part of an atomic window, \"split\" the
3095root of that atomic window. The new window does not become a
3096member of that atomic window.
3097
3098If WINDOW is live, properties of the new window like margins and
3099scrollbars are inherited from WINDOW. If WINDOW is an internal
3100window, these properties as well as the buffer displayed in the
3101new window are inherited from the window selected on WINDOW's
3102frame. The selected window is not changed by this function."
3103 (interactive "i")
447f16b8 3104 (setq window (window-normalize-window window))
130e3e11
MR
3105 (let* ((side (cond
3106 ((not side) 'below)
3107 ((memq side '(below above right left)) side)
3108 (t 'right)))
3109 (horizontal (not (memq side '(nil below above))))
562dd5e9
MR
3110 (frame (window-frame window))
3111 (parent (window-parent window))
3112 (function (window-parameter window 'split-window))
3113 (window-side (window-parameter window 'window-side))
3114 ;; Rebind `window-nest' since in some cases we may have to
3115 ;; override its value.
3116 (window-nest window-nest)
3117 atom-root)
3118
3119 (window-check frame)
3120 (catch 'done
3121 (cond
3122 ;; Ignore window parameters if either `ignore-window-parameters'
3123 ;; is t or the `split-window' parameter equals t.
3124 ((or ignore-window-parameters (eq function t)))
3125 ((functionp function)
3126 ;; The `split-window' parameter specifies the function to call.
3127 ;; If that function is `ignore', do nothing.
3128 (throw 'done (funcall function window size side)))
3129 ;; If WINDOW is a subwindow of an atomic window, split the root
3130 ;; window of that atomic window instead.
3131 ((and (window-parameter window 'window-atom)
3132 (setq atom-root (window-atom-root window))
3133 (not (eq atom-root window)))
3134 (throw 'done (split-window atom-root size side))))
3135
3136 (when (and window-side
3137 (or (not parent)
3138 (not (window-parameter parent 'window-side))))
3139 ;; WINDOW is a side root window. To make sure that a new parent
3140 ;; window gets created set `window-nest' to t.
3141 (setq window-nest t))
3142
3143 (when (and window-splits size (> size 0))
3144 ;; If `window-splits' is non-nil and SIZE is a non-negative
3145 ;; integer, we cannot reasonably resize other windows. Rather
3146 ;; bind `window-nest' to t to make sure that subsequent window
3147 ;; deletions are handled correctly.
3148 (setq window-nest t))
3149
3150 (let* ((parent-size
3151 ;; `parent-size' is the size of WINDOW's parent, provided
3152 ;; it has one.
3153 (when parent (window-total-size parent horizontal)))
3154 ;; `resize' non-nil means we are supposed to resize other
3155 ;; windows in WINDOW's combination.
3156 (resize
3157 (and window-splits (not window-nest)
3158 ;; Resize makes sense in iso-combinations only.
3d8daefe 3159 (window-combined-p window horizontal)))
562dd5e9
MR
3160 ;; `old-size' is the current size of WINDOW.
3161 (old-size (window-total-size window horizontal))
3162 ;; `new-size' is the specified or calculated size of the
3163 ;; new window.
3164 (new-size
3165 (cond
3166 ((not size)
3167 (max (window-split-min-size horizontal)
3168 (if resize
3169 ;; When resizing try to give the new window the
3170 ;; average size of a window in its combination.
3171 (min (- parent-size
3172 (window-min-size parent horizontal))
3173 (/ parent-size
3d8daefe 3174 (1+ (window-combinations
562dd5e9
MR
3175 parent horizontal))))
3176 ;; Else try to give the new window half the size
3177 ;; of WINDOW (plus an eventual odd line).
3178 (+ (/ old-size 2) (% old-size 2)))))
3179 ((>= size 0)
3180 ;; SIZE non-negative specifies the new size of WINDOW.
3181
3182 ;; Note: Specifying a non-negative SIZE is practically
3183 ;; always done as workaround for making the new window
3184 ;; appear above or on the left of the new window (the
3185 ;; ispell window is a typical example of that). In all
3186 ;; these cases the SIDE argument should be set to 'above
3187 ;; or 'left in order to support the 'resize option.
3188 ;; Here we have to nest the windows instead, see above.
3189 (- old-size size))
3190 (t
3191 ;; SIZE negative specifies the size of the new window.
3192 (- size))))
3193 new-parent new-normal)
3194
3195 ;; Check SIZE.
3196 (cond
3197 ((not size)
3198 (cond
3199 (resize
3200 ;; SIZE unspecified, resizing.
3201 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3202 ;; Try again with minimum split size.
3203 (setq new-size
3204 (max new-size (window-split-min-size horizontal)))
3205 (not (window-sizable-p parent (- new-size) horizontal)))
3206 (error "Window %s too small for splitting" parent)))
3207 ((> (+ new-size (window-min-size window horizontal)) old-size)
3208 ;; SIZE unspecified, no resizing.
3209 (error "Window %s too small for splitting" window))))
3210 ((and (>= size 0)
3211 (or (>= size old-size)
3212 (< new-size (if horizontal
3213 window-safe-min-width
3214 window-safe-min-width))))
3215 ;; SIZE specified as new size of old window. If the new size
3216 ;; is larger than the old size or the size of the new window
3217 ;; would be less than the safe minimum, signal an error.
3218 (error "Window %s too small for splitting" window))
3219 (resize
3220 ;; SIZE specified, resizing.
3221 (unless (window-sizable-p parent (- new-size) horizontal)
3222 ;; If we cannot resize the parent give up.
3223 (error "Window %s too small for splitting" parent)))
3224 ((or (< new-size
3225 (if horizontal window-safe-min-width window-safe-min-height))
3226 (< (- old-size new-size)
3227 (if horizontal window-safe-min-width window-safe-min-height)))
3228 ;; SIZE specification violates minimum size restrictions.
3229 (error "Window %s too small for splitting" window)))
3230
5386012d 3231 (window--resize-reset frame horizontal)
562dd5e9
MR
3232
3233 (setq new-parent
3234 ;; Make new-parent non-nil if we need a new parent window;
3235 ;; either because we want to nest or because WINDOW is not
3236 ;; iso-combined.
3d8daefe 3237 (or window-nest (not (window-combined-p window horizontal))))
562dd5e9
MR
3238 (setq new-normal
3239 ;; Make new-normal the normal size of the new window.
3240 (cond
3241 (size (/ (float new-size) (if new-parent old-size parent-size)))
3242 (new-parent 0.5)
3d8daefe 3243 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
562dd5e9
MR
3244 (t (/ (window-normal-size window horizontal) 2.0))))
3245
3246 (if resize
3247 ;; Try to get space from OLD's siblings. We could go "up" and
3248 ;; try getting additional space from surrounding windows but
3249 ;; we won't be able to return space to those windows when we
3250 ;; delete the one we create here. Hence we do not go up.
3251 (progn
5386012d 3252 (window--resize-subwindows parent (- new-size) horizontal)
562dd5e9
MR
3253 (let* ((normal (- 1.0 new-normal))
3254 (sub (window-child parent)))
3255 (while sub
3256 (set-window-new-normal
3257 sub (* (window-normal-size sub horizontal) normal))
3258 (setq sub (window-right sub)))))
3259 ;; Get entire space from WINDOW.
3260 (set-window-new-total window (- old-size new-size))
5386012d 3261 (window--resize-this-window window (- new-size) horizontal)
562dd5e9
MR
3262 (set-window-new-normal
3263 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3264 new-normal)))
3265
3266 (let* ((new (split-window-internal window new-size side new-normal)))
3267 ;; Inherit window-side parameters, if any.
3268 (when (and window-side new-parent)
3269 (set-window-parameter (window-parent new) 'window-side window-side)
3270 (set-window-parameter new 'window-side window-side))
3271
3272 (run-window-configuration-change-hook frame)
3273 (window-check frame)
3274 ;; Always return the new window.
3275 new)))))
3276
3277;; I think this should be the default; I think people will prefer it--rms.
3278(defcustom split-window-keep-point t
2d197ffb
CY
3279 "If non-nil, \\[split-window-below] preserves point in the new window.
3280If nil, adjust point in the two windows to minimize redisplay.
3281This option applies only to `split-window-below' and functions
3282that call it. The low-level `split-window' function always keeps
3283the original point in both windows."
562dd5e9
MR
3284 :type 'boolean
3285 :group 'windows)
3286
2d197ffb
CY
3287(defun split-window-below (&optional size)
3288 "Split the selected window into two windows, one above the other.
3289The selected window is above. The newly split-off window is
3290below, and displays the same buffer. Return the new window.
3291
3292If optional argument SIZE is omitted or nil, both windows get the
3293same height, or close to it. If SIZE is positive, the upper
3294\(selected) window gets SIZE lines. If SIZE is negative, the
3295lower (new) window gets -SIZE lines.
3296
3297If the variable `split-window-keep-point' is non-nil, both
3298windows get the same value of point as the selected window.
3299Otherwise, the window starts are chosen so as to minimize the
3300amount of redisplay; this is convenient on slow terminals."
562dd5e9
MR
3301 (interactive "P")
3302 (let ((old-window (selected-window))
3303 (old-point (point))
3304 (size (and size (prefix-numeric-value size)))
3305 moved-by-window-height moved new-window bottom)
3306 (when (and size (< size 0) (< (- size) window-min-height))
3307 ;; `split-window' would not signal an error here.
3308 (error "Size of new window too small"))
3309 (setq new-window (split-window nil size))
3310 (unless split-window-keep-point
3311 (with-current-buffer (window-buffer)
3312 (goto-char (window-start))
3313 (setq moved (vertical-motion (window-height)))
3314 (set-window-start new-window (point))
3315 (when (> (point) (window-point new-window))
3316 (set-window-point new-window (point)))
3317 (when (= moved (window-height))
3318 (setq moved-by-window-height t)
3319 (vertical-motion -1))
3320 (setq bottom (point)))
3321 (and moved-by-window-height
3322 (<= bottom (point))
3323 (set-window-point old-window (1- bottom)))
3324 (and moved-by-window-height
3325 (<= (window-start new-window) old-point)
3326 (set-window-point new-window old-point)
3327 (select-window new-window)))
9397e56f
MR
3328 ;; Always copy quit-restore parameter in interactive use.
3329 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3330 (when quit-restore
3331 (set-window-parameter new-window 'quit-restore quit-restore)))
3332 new-window))
562dd5e9 3333
2d197ffb 3334(defalias 'split-window-vertically 'split-window-below)
562dd5e9 3335
2d197ffb
CY
3336(defun split-window-right (&optional size)
3337 "Split the selected window into two side-by-side windows.
3338The selected window is on the left. The newly split-off window
3339is on the right, and displays the same buffer. Return the new
3340window.
562dd5e9 3341
2d197ffb
CY
3342If optional argument SIZE is omitted or nil, both windows get the
3343same width, or close to it. If SIZE is positive, the left-hand
3344\(selected) window gets SIZE columns. If SIZE is negative, the
3345right-hand (new) window gets -SIZE columns. Here, SIZE includes
3346the width of the window's scroll bar; if there are no scroll
3347bars, it includes the width of the divider column to the window's
3348right, if any."
562dd5e9
MR
3349 (interactive "P")
3350 (let ((old-window (selected-window))
3351 (size (and size (prefix-numeric-value size)))
3352 new-window)
3353 (when (and size (< size 0) (< (- size) window-min-width))
3354 ;; `split-window' would not signal an error here.
3355 (error "Size of new window too small"))
9397e56f
MR
3356 (setq new-window (split-window nil size t))
3357 ;; Always copy quit-restore parameter in interactive use.
3358 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3359 (when quit-restore
3360 (set-window-parameter new-window 'quit-restore quit-restore)))
3361 new-window))
562dd5e9 3362
2d197ffb 3363(defalias 'split-window-horizontally 'split-window-right)
562dd5e9 3364\f
6198ccd0
MR
3365;;; Balancing windows.
3366
3367;; The following routine uses the recycled code from an old version of
5386012d
MR
3368;; `window--resize-subwindows'. It's not very pretty, but coding it the way the
3369;; new `window--resize-subwindows' code does would hardly make it any shorter or
6198ccd0
MR
3370;; more readable (FWIW we'd need three loops - one to calculate the
3371;; minimum sizes per window, one to enlarge or shrink windows until the
3372;; new parent-size matches, and one where we shrink the largest/enlarge
3373;; the smallest window).
3374(defun balance-windows-2 (window horizontal)
3375 "Subroutine of `balance-windows-1'.
3d8daefe
MR
3376WINDOW must be a vertical combination (horizontal if HORIZONTAL
3377is non-nil."
6198ccd0
MR
3378 (let* ((first (window-child window))
3379 (sub first)
3380 (number-of-children 0)
3381 (parent-size (window-new-total window))
3382 (total-sum parent-size)
3383 found failed size sub-total sub-delta sub-amount rest)
3384 (while sub
3385 (setq number-of-children (1+ number-of-children))
3386 (when (window-size-fixed-p sub horizontal)
3387 (setq total-sum
3388 (- total-sum (window-total-size sub horizontal)))
3389 (set-window-new-normal sub 'ignore))
3390 (setq sub (window-right sub)))
3c448ab6 3391
6198ccd0
MR
3392 (setq failed t)
3393 (while (and failed (> number-of-children 0))
3394 (setq size (/ total-sum number-of-children))
3395 (setq failed nil)
3396 (setq sub first)
3397 (while (and sub (not failed))
3398 ;; Ignore subwindows that should be ignored or are stuck.
5386012d 3399 (unless (window--resize-subwindows-skip-p sub)
6198ccd0
MR
3400 (setq found t)
3401 (setq sub-total (window-total-size sub horizontal))
3402 (setq sub-delta (- size sub-total))
3403 (setq sub-amount
3404 (window-sizable sub sub-delta horizontal))
3405 ;; Register the new total size for this subwindow.
3406 (set-window-new-total sub (+ sub-total sub-amount))
3407 (unless (= sub-amount sub-delta)
3408 (setq total-sum (- total-sum sub-total sub-amount))
3409 (setq number-of-children (1- number-of-children))
3410 ;; We failed and need a new round.
3411 (setq failed t)
3412 (set-window-new-normal sub 'skip)))
3413 (setq sub (window-right sub))))
3c448ab6 3414
6198ccd0
MR
3415 (setq rest (% total-sum number-of-children))
3416 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3417 ;; (column) until `rest' is zero.
3418 (setq sub first)
3419 (while (and sub (> rest 0))
5386012d 3420 (unless (window--resize-subwindows-skip-p window)
6198ccd0
MR
3421 (set-window-new-total sub 1 t)
3422 (setq rest (1- rest)))
3423 (setq sub (window-right sub)))
3c448ab6 3424
6198ccd0
MR
3425 ;; Fix rounding by trying to enlarge stuck windows by one line
3426 ;; (column) until `rest' equals zero.
3427 (setq sub first)
3428 (while (and sub (> rest 0))
3429 (unless (eq (window-new-normal sub) 'ignore)
3430 (set-window-new-total sub 1 t)
3431 (setq rest (1- rest)))
3432 (setq sub (window-right sub)))
3433
3434 (setq sub first)
3435 (while sub
3436 ;; Record new normal sizes.
3437 (set-window-new-normal
3438 sub (/ (if (eq (window-new-normal sub) 'ignore)
3439 (window-total-size sub horizontal)
3440 (window-new-total sub))
3441 (float parent-size)))
3442 ;; Recursively balance each subwindow's subwindows.
3443 (balance-windows-1 sub horizontal)
3444 (setq sub (window-right sub)))))
3445
3446(defun balance-windows-1 (window &optional horizontal)
3447 "Subroutine of `balance-windows'."
3448 (if (window-child window)
3449 (let ((sub (window-child window)))
3d8daefe 3450 (if (window-combined-p sub horizontal)
6198ccd0
MR
3451 (balance-windows-2 window horizontal)
3452 (let ((size (window-new-total window)))
3453 (while sub
9173deec 3454 (set-window-new-total sub size)
6198ccd0
MR
3455 (balance-windows-1 sub horizontal)
3456 (setq sub (window-right sub))))))))
3457
3458(defun balance-windows (&optional window-or-frame)
3459 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3460WINDOW-OR-FRAME is optional and defaults to the selected frame.
3461If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3462subwindows of that frame's root window. If WINDOW-OR-FRAME
3463denots a window, balance the sizes of all subwindows of that
3464window."
3465 (interactive)
3466 (let* ((window
3467 (cond
3468 ((or (not window-or-frame)
3469 (frame-live-p window-or-frame))
3470 (frame-root-window window-or-frame))
3471 ((or (window-live-p window-or-frame)
3472 (window-child window-or-frame))
3473 window-or-frame)
3474 (t
3475 (error "Not a window or frame %s" window-or-frame))))
3476 (frame (window-frame window)))
3477 ;; Balance vertically.
5386012d 3478 (window--resize-reset (window-frame window))
6198ccd0 3479 (balance-windows-1 window)
d615d6d2 3480 (window-resize-apply frame)
6198ccd0 3481 ;; Balance horizontally.
5386012d 3482 (window--resize-reset (window-frame window) t)
6198ccd0 3483 (balance-windows-1 window t)
d615d6d2 3484 (window-resize-apply frame t)))
3c448ab6
MR
3485
3486(defun window-fixed-size-p (&optional window direction)
3487 "Return t if WINDOW cannot be resized in DIRECTION.
3488WINDOW defaults to the selected window. DIRECTION can be
3489nil (i.e. any), `height' or `width'."
3490 (with-current-buffer (window-buffer window)
3491 (when (and (boundp 'window-size-fixed) window-size-fixed)
3492 (not (and direction
3493 (member (cons direction window-size-fixed)
3494 '((height . width) (width . height))))))))
3495
3496;;; A different solution to balance-windows.
3c448ab6
MR
3497(defvar window-area-factor 1
3498 "Factor by which the window area should be over-estimated.
3499This is used by `balance-windows-area'.
3500Changing this globally has no effect.")
3501(make-variable-buffer-local 'window-area-factor)
3502
6198ccd0 3503(defun balance-windows-area-adjust (window delta horizontal)
d615d6d2 3504 "Wrapper around `window-resize' with error checking.
6198ccd0 3505Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
d615d6d2 3506 ;; `window-resize' may fail if delta is too large.
6198ccd0
MR
3507 (while (>= (abs delta) 1)
3508 (condition-case nil
3509 (progn
d615d6d2 3510 (window-resize window delta horizontal)
6198ccd0
MR
3511 (setq delta 0))
3512 (error
3513 ;;(message "adjust: %s" (error-message-string err))
3514 (setq delta (/ delta 2))))))
3515
3c448ab6
MR
3516(defun balance-windows-area ()
3517 "Make all visible windows the same area (approximately).
3518See also `window-area-factor' to change the relative size of
3519specific buffers."
3520 (interactive)
3521 (let* ((unchanged 0) (carry 0) (round 0)
3522 ;; Remove fixed-size windows.
3523 (wins (delq nil (mapcar (lambda (win)
3524 (if (not (window-fixed-size-p win)) win))
3525 (window-list nil 'nomini))))
3526 (changelog nil)
3527 next)
3528 ;; Resizing a window changes the size of surrounding windows in complex
3529 ;; ways, so it's difficult to balance them all. The introduction of
3530 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3531 ;; very difficult to do. `balance-window' above takes an off-line
3532 ;; approach: get the whole window tree, then balance it, then try to
3533 ;; adjust the windows so they fit the result.
3534 ;; Here, instead, we take a "local optimization" approach, where we just
3535 ;; go through all the windows several times until nothing needs to be
3536 ;; changed. The main problem with this approach is that it's difficult
3537 ;; to make sure it terminates, so we use some heuristic to try and break
3538 ;; off infinite loops.
3539 ;; After a round without any change, we allow a second, to give a chance
3540 ;; to the carry to propagate a minor imbalance from the end back to
3541 ;; the beginning.
3542 (while (< unchanged 2)
3543 ;; (message "New round")
3544 (setq unchanged (1+ unchanged) round (1+ round))
3545 (dolist (win wins)
3546 (setq next win)
3547 (while (progn (setq next (next-window next))
3548 (window-fixed-size-p next)))
3549 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3550 (let* ((horiz
3551 (< (car (window-edges win)) (car (window-edges next))))
3552 (areadiff (/ (- (* (window-height next) (window-width next)
3553 (buffer-local-value 'window-area-factor
3554 (window-buffer next)))
3555 (* (window-height win) (window-width win)
3556 (buffer-local-value 'window-area-factor
3557 (window-buffer win))))
3558 (max (buffer-local-value 'window-area-factor
3559 (window-buffer win))
3560 (buffer-local-value 'window-area-factor
3561 (window-buffer next)))))
3562 (edgesize (if horiz
3563 (+ (window-height win) (window-height next))
3564 (+ (window-width win) (window-width next))))
3565 (diff (/ areadiff edgesize)))
3566 (when (zerop diff)
3567 ;; Maybe diff is actually closer to 1 than to 0.
3568 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3569 (when (and (zerop diff) (not (zerop areadiff)))
3570 (setq diff (/ (+ areadiff carry) edgesize))
3571 ;; Change things smoothly.
3572 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3573 (if (zerop diff)
3574 ;; Make sure negligible differences don't accumulate to
3575 ;; become significant.
3576 (setq carry (+ carry areadiff))
6198ccd0 3577 ;; This used `adjust-window-trailing-edge' before and uses
d615d6d2 3578 ;; `window-resize' now. Error wrapping is still needed.
6198ccd0 3579 (balance-windows-area-adjust win diff horiz)
3c448ab6
MR
3580 ;; (sit-for 0.5)
3581 (let ((change (cons win (window-edges win))))
3582 ;; If the same change has been seen already for this window,
3583 ;; we're most likely in an endless loop, so don't count it as
3584 ;; a change.
3585 (unless (member change changelog)
3586 (push change changelog)
3587 (setq unchanged 0 carry 0)))))))
3588 ;; We've now basically balanced all the windows.
3589 ;; But there may be some minor off-by-one imbalance left over,
3590 ;; so let's do some fine tuning.
3591 ;; (bw-finetune wins)
3592 ;; (message "Done in %d rounds" round)
3593 ))
9d89fec7
MR
3594
3595;;; Window states, how to get them and how to put them in a window.
3596(defsubst window-list-no-nils (&rest args)
3597 "Like LIST but do not add nil elements of ARGS."
3598 (delq nil (apply 'list args)))
3599
3600(defvar window-state-ignored-parameters '(quit-restore)
3601 "List of window parameters ignored by `window-state-get'.")
3602
3603(defun window-state-get-1 (window &optional markers)
3604 "Helper function for `window-state-get'."
3605 (let* ((type
3606 (cond
d68443dc
MR
3607 ((window-top-child window) 'vc)
3608 ((window-left-child window) 'hc)
9d89fec7
MR
3609 (t 'leaf)))
3610 (buffer (window-buffer window))
3611 (selected (eq window (selected-window)))
3612 (head
3613 (window-list-no-nils
3614 type
d68443dc 3615 (unless (window-next-sibling window) (cons 'last t))
9d89fec7
MR
3616 (cons 'total-height (window-total-size window))
3617 (cons 'total-width (window-total-size window t))
3618 (cons 'normal-height (window-normal-size window))
3619 (cons 'normal-width (window-normal-size window t))
3620 (cons 'splits (window-splits window))
3621 (cons 'nest (window-nest window))
3622 (let (list)
3623 (dolist (parameter (window-parameters window))
3624 (unless (memq (car parameter)
3625 window-state-ignored-parameters)
3626 (setq list (cons parameter list))))
28545e04
MR
3627 (unless (window-parameter window 'clone-of)
3628 ;; Make a clone-of parameter.
3629 (setq list (cons (cons 'clone-of window) list)))
9d89fec7
MR
3630 (when list
3631 (cons 'parameters list)))
3632 (when buffer
3633 ;; All buffer related things go in here - make the buffer
3634 ;; current when retrieving `point' and `mark'.
3635 (with-current-buffer (window-buffer window)
c96111ea 3636 (let ((point (window-point-1 window))
9d89fec7
MR
3637 (start (window-start window))
3638 (mark (mark)))
3639 (window-list-no-nils
3640 'buffer (buffer-name buffer)
3641 (cons 'selected selected)
3642 (when window-size-fixed (cons 'size-fixed window-size-fixed))
3643 (cons 'hscroll (window-hscroll window))
3644 (cons 'fringes (window-fringes window))
3645 (cons 'margins (window-margins window))
3646 (cons 'scroll-bars (window-scroll-bars window))
3647 (cons 'vscroll (window-vscroll window))
3648 (cons 'dedicated (window-dedicated-p window))
3649 (cons 'point (if markers (copy-marker point) point))
3650 (cons 'start (if markers (copy-marker start) start))
3651 (when mark
3652 (cons 'mark (if markers (copy-marker mark) mark)))))))))
3653 (tail
3654 (when (memq type '(vc hc))
3655 (let (list)
3656 (setq window (window-child window))
3657 (while window
3658 (setq list (cons (window-state-get-1 window markers) list))
3659 (setq window (window-right window)))
3660 (nreverse list)))))
3661 (append head tail)))
3662
3663(defun window-state-get (&optional window markers)
3664 "Return state of WINDOW as a Lisp object.
3665WINDOW can be any window and defaults to the root window of the
3666selected frame.
3667
3668Optional argument MARKERS non-nil means use markers for sampling
3669positions like `window-point' or `window-start'. MARKERS should
3670be non-nil only if the value is used for putting the state back
3671in the same session (note that markers slow down processing).
3672
3673The return value can be used as argument for `window-state-put'
3674to put the state recorded here into an arbitrary window. The
3675value can be also stored on disk and read back in a new session."
3676 (setq window
3677 (if window
24300f5f 3678 (if (window-valid-p window)
9d89fec7
MR
3679 window
3680 (error "%s is not a live or internal window" window))
3681 (frame-root-window)))
3682 ;; The return value is a cons whose car specifies some constraints on
3683 ;; the size of WINDOW. The cdr lists the states of the subwindows of
3684 ;; WINDOW.
3685 (cons
3686 ;; Frame related things would go into a function, say `frame-state',
3687 ;; calling `window-state-get' to insert the frame's root window.
3688 (window-list-no-nils
3689 (cons 'min-height (window-min-size window))
3690 (cons 'min-width (window-min-size window t))
3691 (cons 'min-height-ignore (window-min-size window nil t))
3692 (cons 'min-width-ignore (window-min-size window t t))
3693 (cons 'min-height-safe (window-min-size window nil 'safe))
3694 (cons 'min-width-safe (window-min-size window t 'safe))
3695 ;; These are probably not needed.
3696 (when (window-size-fixed-p window) (cons 'fixed-height t))
3697 (when (window-size-fixed-p window t) (cons 'fixed-width t)))
3698 (window-state-get-1 window markers)))
3699
3700(defvar window-state-put-list nil
3701 "Helper variable for `window-state-put'.")
3702
3703(defun window-state-put-1 (state &optional window ignore totals)
3704 "Helper function for `window-state-put'."
3705 (let ((type (car state)))
3706 (setq state (cdr state))
3707 (cond
3708 ((eq type 'leaf)
3709 ;; For a leaf window just add unprocessed entries to
3710 ;; `window-state-put-list'.
3711 (setq window-state-put-list
3712 (cons (cons window state) window-state-put-list)))
3713 ((memq type '(vc hc))
3714 (let* ((horizontal (eq type 'hc))
3715 (total (window-total-size window horizontal))
3716 (first t)
3717 size new)
3718 (dolist (item state)
3719 ;; Find the next child window. WINDOW always points to the
3720 ;; real window that we want to fill with what we find here.
3721 (when (memq (car item) '(leaf vc hc))
3722 (if (assq 'last item)
3723 ;; The last child window. Below `window-state-put-1'
3724 ;; will put into it whatever ITEM has in store.
3725 (setq new nil)
3726 ;; Not the last child window, prepare for splitting
3727 ;; WINDOW. SIZE is the new (and final) size of the old
3728 ;; window.
3729 (setq size
3730 (if totals
3731 ;; Use total size.
3732 (cdr (assq (if horizontal 'total-width 'total-height) item))
3733 ;; Use normalized size and round.
3734 (round (* total
3735 (cdr (assq
3736 (if horizontal 'normal-width 'normal-height)
3737 item))))))
3738
3739 ;; Use safe sizes, we try to resize later.
3740 (setq size (max size (if horizontal
3741 window-safe-min-height
3742 window-safe-min-width)))
3743
3744 (if (window-sizable-p window (- size) horizontal 'safe)
3745 (let* ((window-nest (assq 'nest item)))
3746 ;; We must inherit the nesting, otherwise we might mess
3747 ;; up handling of atomic and side window.
3748 (setq new (split-window window size horizontal)))
3749 ;; Give up if we can't resize window down to safe sizes.
3750 (error "Cannot resize window %s" window))
3751
3752 (when first
3753 (setq first nil)
3754 ;; When creating the first child window add for parent
3755 ;; unprocessed entries to `window-state-put-list'.
3756 (setq window-state-put-list
3757 (cons (cons (window-parent window) state)
3758 window-state-put-list))))
3759
3760 ;; Now process the current window (either the one we've just
3761 ;; split or the last child of its parent).
3762 (window-state-put-1 item window ignore totals)
3763 ;; Continue with the last window split off.
3764 (setq window new))))))))
3765
3766(defun window-state-put-2 (ignore)
3767 "Helper function for `window-state-put'."
3768 (dolist (item window-state-put-list)
3769 (let ((window (car item))
9d89fec7
MR
3770 (splits (cdr (assq 'splits item)))
3771 (nest (cdr (assq 'nest item)))
3772 (parameters (cdr (assq 'parameters item)))
3773 (state (cdr (assq 'buffer item))))
9d89fec7
MR
3774 (when splits (set-window-splits window splits))
3775 (when nest (set-window-nest window nest))
3776 ;; Process parameters.
3777 (when parameters
3778 (dolist (parameter parameters)
3779 (set-window-parameter window (car parameter) (cdr parameter))))
3780 ;; Process buffer related state.
3781 (when state
3782 ;; We don't want to raise an error here so we create a buffer if
3783 ;; there's none.
3784 (set-window-buffer window (get-buffer-create (car state)))
3785 (with-current-buffer (window-buffer window)
3786 (set-window-hscroll window (cdr (assq 'hscroll state)))
3787 (apply 'set-window-fringes
3788 (cons window (cdr (assq 'fringes state))))
3789 (let ((margins (cdr (assq 'margins state))))
3790 (set-window-margins window (car margins) (cdr margins)))
3791 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
3792 (set-window-scroll-bars
3793 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
3794 (set-window-vscroll window (cdr (assq 'vscroll state)))
3795 ;; Adjust vertically.
3796 (if (memq window-size-fixed '(t height))
3797 ;; A fixed height window, try to restore the original size.
3798 (let ((delta (- (cdr (assq 'total-height item))
3799 (window-total-height window)))
3800 window-size-fixed)
3801 (when (window-resizable-p window delta)
d615d6d2 3802 (window-resize window delta)))
9d89fec7
MR
3803 ;; Else check whether the window is not high enough.
3804 (let* ((min-size (window-min-size window nil ignore))
3805 (delta (- min-size (window-total-size window))))
3806 (when (and (> delta 0)
3807 (window-resizable-p window delta nil ignore))
d615d6d2 3808 (window-resize window delta nil ignore))))
9d89fec7
MR
3809 ;; Adjust horizontally.
3810 (if (memq window-size-fixed '(t width))
3811 ;; A fixed width window, try to restore the original size.
3812 (let ((delta (- (cdr (assq 'total-width item))
3813 (window-total-width window)))
3814 window-size-fixed)
3815 (when (window-resizable-p window delta)
d615d6d2 3816 (window-resize window delta)))
9d89fec7
MR
3817 ;; Else check whether the window is not wide enough.
3818 (let* ((min-size (window-min-size window t ignore))
3819 (delta (- min-size (window-total-size window t))))
3820 (when (and (> delta 0)
3821 (window-resizable-p window delta t ignore))
d615d6d2 3822 (window-resize window delta t ignore))))
9d89fec7
MR
3823 ;; Set dedicated status.
3824 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
3825 ;; Install positions (maybe we should do this after all windows
3826 ;; have been created and sized).
3827 (ignore-errors
3828 (set-window-start window (cdr (assq 'start state)))
3829 (set-window-point window (cdr (assq 'point state)))
3830 ;; I'm not sure whether we should set the mark here, but maybe
3831 ;; it can be used.
3832 (let ((mark (cdr (assq 'mark state))))
3833 (when mark (set-mark mark))))
3834 ;; Select window if it's the selected one.
3835 (when (cdr (assq 'selected state))
3836 (select-window window)))))))
3837
3838(defun window-state-put (state &optional window ignore)
3839 "Put window state STATE into WINDOW.
3840STATE should be the state of a window returned by an earlier
3841invocation of `window-state-get'. Optional argument WINDOW must
3842specify a live window and defaults to the selected one.
3843
3844Optional argument IGNORE non-nil means ignore minimum window
3845sizes and fixed size restrictions. IGNORE equal `safe' means
3846subwindows can get as small as `window-safe-min-height' and
3847`window-safe-min-width'."
447f16b8 3848 (setq window (window-normalize-window window t))
9d89fec7
MR
3849 (let* ((frame (window-frame window))
3850 (head (car state))
3851 ;; We check here (1) whether the total sizes of root window of
3852 ;; STATE and that of WINDOW are equal so we can avoid
3853 ;; calculating new sizes, and (2) if we do have to resize
3854 ;; whether we can do so without violating size restrictions.
3855 (totals
3856 (and (= (window-total-size window)
3857 (cdr (assq 'total-height state)))
3858 (= (window-total-size window t)
3859 (cdr (assq 'total-width state)))))
3860 (min-height (cdr (assq 'min-height head)))
3861 (min-width (cdr (assq 'min-width head)))
3862 window-splits selected)
3863 (if (and (not totals)
3864 (or (> min-height (window-total-size window))
3865 (> min-width (window-total-size window t)))
3866 (or (not ignore)
3867 (and (setq min-height
3868 (cdr (assq 'min-height-ignore head)))
3869 (setq min-width
3870 (cdr (assq 'min-width-ignore head)))
3871 (or (> min-height (window-total-size window))
3872 (> min-width (window-total-size window t)))
3873 (or (not (eq ignore 'safe))
3874 (and (setq min-height
3875 (cdr (assq 'min-height-safe head)))
3876 (setq min-width
3877 (cdr (assq 'min-width-safe head)))
3878 (or (> min-height
3879 (window-total-size window))
3880 (> min-width
3881 (window-total-size window t))))))))
3882 ;; The check above might not catch all errors due to rounding
3883 ;; issues - so IGNORE equal 'safe might not always produce the
3884 ;; minimum possible state. But such configurations hardly make
3885 ;; sense anyway.
a91adc7e 3886 (error "Window %s too small to accommodate state" window)
9d89fec7
MR
3887 (setq state (cdr state))
3888 (setq window-state-put-list nil)
3889 ;; Work on the windows of a temporary buffer to make sure that
3890 ;; splitting proceeds regardless of any buffer local values of
3891 ;; `window-size-fixed'. Release that buffer after the buffers of
3892 ;; all live windows have been set by `window-state-put-2'.
3893 (with-temp-buffer
3894 (set-window-buffer window (current-buffer))
3895 (window-state-put-1 state window nil totals)
3896 (window-state-put-2 ignore))
3897 (window-check frame))))
9481c002 3898\f
f818cd2a
MR
3899(defun display-buffer-record-window (type window buffer)
3900 "Record information for window used by `display-buffer'.
cf4eacfd
MR
3901TYPE specifies the type of the calling operation and must be one
3902of the symbols 'reuse \(when WINDOW existed already and was
3903reused for displaying BUFFER), 'window \(when WINDOW was created
3904on an already existing frame), or 'frame \(when WINDOW was
3905created on a new frame). WINDOW is the window used for or created
3906by the `display-buffer' routines. BUFFER is the buffer that
3907shall be displayed.
3908
3909This function installs or updates the quit-restore parameter of
3910WINDOW. The quit-restore parameter is a list of four elements:
3911The first element is one of the symbols 'window, 'frame, 'same or
3912'other. The second element is either one of the symbols 'window
3913or 'frame or a list whose elements are the buffer previously
3914shown in the window, that buffer's window start and window point,
3915and the window's height. The third element is the window
3916selected at the time the parameter was created. The fourth
3917element is BUFFER."
f818cd2a 3918 (cond
cf4eacfd
MR
3919 ((eq type 'reuse)
3920 (if (eq (window-buffer window) buffer)
3921 ;; WINDOW shows BUFFER already.
3922 (when (consp (window-parameter window 'quit-restore))
3923 ;; If WINDOW has a quit-restore parameter, reset its car.
3924 (setcar (window-parameter window 'quit-restore) 'same))
3925 ;; WINDOW shows another buffer.
9481c002 3926 (set-window-parameter
f818cd2a 3927 window 'quit-restore
cf4eacfd
MR
3928 (list 'other
3929 ;; A quadruple of WINDOW's buffer, start, point and height.
3930 (list (window-buffer window) (window-start window)
c96111ea 3931 (window-point-1 window) (window-total-size window))
cf4eacfd
MR
3932 (selected-window) buffer))))
3933 ((eq type 'window)
3934 ;; WINDOW has been created on an existing frame.
f818cd2a 3935 (set-window-parameter
cf4eacfd
MR
3936 window 'quit-restore
3937 (list 'window 'window (selected-window) buffer)))
3938 ((eq type 'frame)
3939 ;; WINDOW has been created on a new frame.
f818cd2a 3940 (set-window-parameter
cf4eacfd
MR
3941 window 'quit-restore
3942 (list 'frame 'frame (selected-window) buffer)))))
9481c002 3943
f818cd2a
MR
3944(defcustom display-buffer-function nil
3945 "If non-nil, function to call to handle `display-buffer'.
3946It will receive two args, the buffer and a flag which if non-nil
3947means that the currently selected window is not acceptable. It
3948should choose or create a window, display the specified buffer in
3949it, and return the window.
3950
f678e0b6
MR
3951The function specified here is responsible for setting the
3952quit-restore and help-setup parameters of the window used."
f818cd2a
MR
3953 :type '(choice
3954 (const nil)
3955 (function :tag "function"))
3c448ab6 3956 :group 'windows)
9481c002 3957
f818cd2a
MR
3958(defcustom pop-up-frame-alist nil
3959 "Alist of parameters for automatically generated new frames.
3960You can set this in your init file; for example,
9481c002 3961
f818cd2a 3962 (setq pop-up-frame-alist '((width . 80) (height . 20)))
9481c002 3963
f818cd2a
MR
3964If non-nil, the value you specify here is used by the default
3965`pop-up-frame-function' for the creation of new frames.
9481c002 3966
f818cd2a
MR
3967Since `pop-up-frame-function' is used by `display-buffer' for
3968making new frames, any value specified here by default affects
3969the automatic generation of new frames via `display-buffer' and
3970all functions based on it. The behavior of `make-frame' is not
3971affected by this variable."
9481c002 3972 :type '(repeat (cons :format "%v"
f818cd2a
MR
3973 (symbol :tag "Parameter")
3974 (sexp :tag "Value")))
9481c002 3975 :group 'frames)
9481c002 3976
f818cd2a
MR
3977(defcustom pop-up-frame-function
3978 (lambda () (make-frame pop-up-frame-alist))
3979 "Function used by `display-buffer' for creating a new frame.
3980This function is called with no arguments and should return a new
3981frame. The default value calls `make-frame' with the argument
3982`pop-up-frame-alist'."
9481c002 3983 :type 'function
9481c002 3984 :group 'frames)
3c448ab6 3985
56f31926
MR
3986(defcustom special-display-buffer-names nil
3987 "List of names of buffers that should be displayed specially.
3988Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3989its name is in this list, displays the buffer in a way specified
3990by `special-display-function'. `special-display-popup-frame'
3991\(the default for `special-display-function') usually displays
3992the buffer in a separate frame made with the parameters specified
3993by `special-display-frame-alist'. If `special-display-function'
3994has been set to some other function, that function is called with
3995the buffer as first, and nil as second argument.
3996
3997Alternatively, an element of this list can be specified as
3998\(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
3999name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
4000`special-display-popup-frame' will interpret such pairs as frame
4001parameters when it creates a special frame, overriding the
4002corresponding values from `special-display-frame-alist'.
4003
4004As a special case, if FRAME-PARAMETERS contains (same-window . t)
4005`special-display-popup-frame' displays that buffer in the
4006selected window. If FRAME-PARAMETERS contains (same-frame . t),
4007it displays that buffer in a window on the selected frame.
4008
4009If `special-display-function' specifies some other function than
4010`special-display-popup-frame', that function is called with the
4011buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4012argument.
4013
4014Finally, an element of this list can be also specified as
4015\(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4016`special-display-popup-frame' will call FUNCTION with the buffer
4017named BUFFER-NAME as first argument, and OTHER-ARGS as the
0563dae9
MR
4018second.
4019
4020Any alternative function specified here is responsible for
4021setting up the quit-restore parameter of the window used.
56f31926
MR
4022
4023If this variable appears \"not to work\", because you added a
4024name to it but the corresponding buffer is displayed in the
4025selected window, look at the values of `same-window-buffer-names'
4026and `same-window-regexps'. Those variables take precedence over
4027this one.
4028
4029See also `special-display-regexps'."
4030 :type '(repeat
4031 (choice :tag "Buffer"
4032 :value ""
4033 (string :format "%v")
4034 (cons :tag "With parameters"
4035 :format "%v"
4036 :value ("" . nil)
4037 (string :format "%v")
4038 (repeat :tag "Parameters"
4039 (cons :format "%v"
4040 (symbol :tag "Parameter")
4041 (sexp :tag "Value"))))
4042 (list :tag "With function"
4043 :format "%v"
4044 :value ("" . nil)
4045 (string :format "%v")
4046 (function :tag "Function")
4047 (repeat :tag "Arguments" (sexp)))))
4048 :group 'windows
4049 :group 'frames)
4050
ac549fa5
GM
4051;;;###autoload
4052(put 'special-display-buffer-names 'risky-local-variable t)
4053
56f31926
MR
4054(defcustom special-display-regexps nil
4055 "List of regexps saying which buffers should be displayed specially.
4056Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4057any regexp in this list matches its name, displays it specially
4058using `special-display-function'. `special-display-popup-frame'
4059\(the default for `special-display-function') usually displays
4060the buffer in a separate frame made with the parameters specified
4061by `special-display-frame-alist'. If `special-display-function'
4062has been set to some other function, that function is called with
4063the buffer as first, and nil as second argument.
4064
4065Alternatively, an element of this list can be specified as
4066\(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4067FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4068`special-display-popup-frame' will then interpret these pairs as
4069frame parameters when creating a special frame for a buffer whose
4070name matches REGEXP, overriding the corresponding values from
4071`special-display-frame-alist'.
4072
4073As a special case, if FRAME-PARAMETERS contains (same-window . t)
4074`special-display-popup-frame' displays buffers matching REGEXP in
4075the selected window. \(same-frame . t) in FRAME-PARAMETERS means
4076to display such buffers in a window on the selected frame.
4077
4078If `special-display-function' specifies some other function than
4079`special-display-popup-frame', that function is called with the
4080buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4081as second argument.
4082
4083Finally, an element of this list can be also specified as
4084\(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4085will then call FUNCTION with the buffer whose name matched
0563dae9
MR
4086REGEXP as first, and OTHER-ARGS as second argument.
4087
4088Any alternative function specified here is responsible for
4089setting up the quit-restore parameter of the window used.
56f31926
MR
4090
4091If this variable appears \"not to work\", because you added a
4092name to it but the corresponding buffer is displayed in the
4093selected window, look at the values of `same-window-buffer-names'
4094and `same-window-regexps'. Those variables take precedence over
4095this one.
4096
4097See also `special-display-buffer-names'."
4098 :type '(repeat
4099 (choice :tag "Buffer"
4100 :value ""
4101 (regexp :format "%v")
4102 (cons :tag "With parameters"
4103 :format "%v"
4104 :value ("" . nil)
4105 (regexp :format "%v")
4106 (repeat :tag "Parameters"
4107 (cons :format "%v"
4108 (symbol :tag "Parameter")
4109 (sexp :tag "Value"))))
4110 (list :tag "With function"
4111 :format "%v"
4112 :value ("" . nil)
4113 (regexp :format "%v")
4114 (function :tag "Function")
4115 (repeat :tag "Arguments" (sexp)))))
4116 :group 'windows
4117 :group 'frames)
4118
3c448ab6
MR
4119(defun special-display-p (buffer-name)
4120 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
56f31926
MR
4121More precisely, return t if `special-display-buffer-names' or
4122`special-display-regexps' contain a string entry equaling or
4123matching BUFFER-NAME. If `special-display-buffer-names' or
4124`special-display-regexps' contain a list entry whose car equals
4125or matches BUFFER-NAME, the return value is the cdr of that
4126entry."
f818cd2a 4127 (let (tmp)
98722073
MR
4128 (cond
4129 ((not (stringp buffer-name)))
f818cd2a 4130 ((member buffer-name special-display-buffer-names)
98722073 4131 t)
f818cd2a 4132 ((setq tmp (assoc buffer-name special-display-buffer-names))
98722073
MR
4133 (cdr tmp))
4134 ((catch 'found
f818cd2a 4135 (dolist (regexp special-display-regexps)
98722073
MR
4136 (cond
4137 ((stringp regexp)
4138 (when (string-match-p regexp buffer-name)
4139 (throw 'found t)))
4140 ((and (consp regexp) (stringp (car regexp))
4141 (string-match-p (car regexp) buffer-name))
4142 (throw 'found (cdr regexp))))))))))
9481c002 4143
f818cd2a
MR
4144(defcustom special-display-frame-alist
4145 '((height . 14) (width . 80) (unsplittable . t))
4146 "Alist of parameters for special frames.
4147Special frames are used for buffers whose names are listed in
4148`special-display-buffer-names' and for buffers whose names match
4149one of the regular expressions in `special-display-regexps'.
9481c002 4150
f818cd2a 4151This variable can be set in your init file, like this:
9481c002 4152
f818cd2a
MR
4153 (setq special-display-frame-alist '((width . 80) (height . 20)))
4154
4155These supersede the values given in `default-frame-alist'."
4156 :type '(repeat (cons :format "%v"
4157 (symbol :tag "Parameter")
4158 (sexp :tag "Value")))
4159 :group 'frames)
4160
4161(defun special-display-popup-frame (buffer &optional args)
4162 "Display BUFFER and return the window chosen.
4163If BUFFER is already displayed in a visible or iconified frame,
4164raise that frame. Otherwise, display BUFFER in a new frame.
4165
4166Optional argument ARGS is a list specifying additional
4167information.
4168
4169If ARGS is an alist, use it as a list of frame parameters. If
4170these parameters contain \(same-window . t), display BUFFER in
4171the selected window. If they contain \(same-frame . t), display
4172BUFFER in a window of the selected frame.
4173
4174If ARGS is a list whose car is a symbol, use (car ARGS) as a
4175function to do the work. Pass it BUFFER as first argument,
4176and (cdr ARGS) as second."
4177 (if (and args (symbolp (car args)))
4178 (apply (car args) buffer (cdr args))
4179 (let ((window (get-buffer-window buffer 0)))
4180 (or
4181 ;; If we have a window already, make it visible.
4182 (when window
4183 (let ((frame (window-frame window)))
4184 (make-frame-visible frame)
4185 (raise-frame frame)
cf4eacfd 4186 (display-buffer-record-window 'reuse window buffer)
f818cd2a
MR
4187 window))
4188 ;; Reuse the current window if the user requested it.
4189 (when (cdr (assq 'same-window args))
4190 (condition-case nil
4191 (progn (switch-to-buffer buffer nil t) (selected-window))
4192 (error nil)))
4193 ;; Stay on the same frame if requested.
4194 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
4195 (let* ((pop-up-windows t)
4196 pop-up-frames
4197 special-display-buffer-names special-display-regexps)
4198 (display-buffer buffer)))
4199 ;; If no window yet, make one in a new frame.
7f80c86d
MR
4200 (let* ((frame
4201 (with-current-buffer buffer
4202 (make-frame (append args special-display-frame-alist))))
4203 (window (frame-selected-window frame)))
4204 (display-buffer-record-window 'frame window buffer)
4205 (set-window-buffer window buffer)
4206 ;; Reset list of WINDOW's previous buffers to nil.
4207 (set-window-prev-buffers window nil)
4208 (set-window-dedicated-p window t)
4209 window)))))
f818cd2a
MR
4210
4211(defcustom special-display-function 'special-display-popup-frame
4212 "Function to call for displaying special buffers.
4213This function is called with two arguments - the buffer and,
4214optionally, a list - and should return a window displaying that
4215buffer. The default value usually makes a separate frame for the
4216buffer using `special-display-frame-alist' to specify the frame
4217parameters. See the definition of `special-display-popup-frame'
4218for how to specify such a function.
4219
4220A buffer is special when its name is either listed in
4221`special-display-buffer-names' or matches a regexp in
4222`special-display-regexps'.
4223
f678e0b6
MR
4224The function specified here is responsible for setting the
4225quit-restore and help-setup parameters of the window used."
f818cd2a
MR
4226 :type 'function
4227 :group 'frames)
4228
4229(defcustom same-window-buffer-names nil
4230 "List of names of buffers that should appear in the \"same\" window.
4231`display-buffer' and `pop-to-buffer' show a buffer whose name is
4232on this list in the selected rather than some other window.
4233
4234An element of this list can be a cons cell instead of just a
4235string. In that case, the cell's car must be a string specifying
4236the buffer name. This is for compatibility with
4237`special-display-buffer-names'; the cdr of the cons cell is
4238ignored.
4239
4240See also `same-window-regexps'."
4241 :type '(repeat (string :format "%v"))
4242 :group 'windows)
4243
4244(defcustom same-window-regexps nil
4245 "List of regexps saying which buffers should appear in the \"same\" window.
4246`display-buffer' and `pop-to-buffer' show a buffer whose name
4247matches a regexp on this list in the selected rather than some
4248other window.
4249
4250An element of this list can be a cons cell instead of just a
4251string. In that case, the cell's car must be a regexp matching
4252the buffer name. This is for compatibility with
4253`special-display-regexps'; the cdr of the cons cell is ignored.
9481c002 4254
f818cd2a
MR
4255See also `same-window-buffer-names'."
4256 :type '(repeat (regexp :format "%v"))
4257 :group 'windows)
9481c002 4258
f818cd2a
MR
4259(defun same-window-p (buffer-name)
4260 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4261This function returns non-nil if `display-buffer' or
4262`pop-to-buffer' would show a buffer named BUFFER-NAME in the
4263selected rather than \(as usual\) some other window. See
4264`same-window-buffer-names' and `same-window-regexps'."
4265 (cond
4266 ((not (stringp buffer-name)))
4267 ;; The elements of `same-window-buffer-names' can be buffer
4268 ;; names or cons cells whose cars are buffer names.
4269 ((member buffer-name same-window-buffer-names))
4270 ((assoc buffer-name same-window-buffer-names))
4271 ((catch 'found
4272 (dolist (regexp same-window-regexps)
4273 ;; The elements of `same-window-regexps' can be regexps
4274 ;; or cons cells whose cars are regexps.
4275 (when (or (and (stringp regexp)
4276 (string-match regexp buffer-name))
4277 (and (consp regexp) (stringp (car regexp))
4278 (string-match-p (car regexp) buffer-name)))
4279 (throw 'found t)))))))
3c448ab6 4280
d1067961 4281(defcustom pop-up-frames nil
3c448ab6 4282 "Whether `display-buffer' should make a separate frame.
d1f18ec0 4283If nil, never make a separate frame.
3c448ab6
MR
4284If the value is `graphic-only', make a separate frame
4285on graphic displays only.
4286Any other non-nil value means always make a separate frame."
4287 :type '(choice
4288 (const :tag "Never" nil)
4289 (const :tag "On graphic displays only" graphic-only)
4290 (const :tag "Always" t))
f818cd2a 4291 :group 'windows)
3c448ab6 4292
d1067961 4293(defcustom display-buffer-reuse-frames nil
f818cd2a 4294 "Non-nil means `display-buffer' should reuse frames.
3c448ab6
MR
4295If the buffer in question is already displayed in a frame, raise
4296that frame."
4297 :type 'boolean
d1067961 4298 :version "21.1"
f818cd2a 4299 :group 'windows)
3c448ab6 4300
4dc2a129
MR
4301(defcustom pop-up-windows t
4302 "Non-nil means `display-buffer' should make a new window."
3c448ab6
MR
4303 :type 'boolean
4304 :group 'windows)
4305
8b10a2d1 4306(defcustom split-window-preferred-function 'split-window-sensibly
f818cd2a 4307 "Function called by `display-buffer' routines to split a window.
8b10a2d1
MR
4308This function is called with a window as single argument and is
4309supposed to split that window and return the new window. If the
4310window can (or shall) not be split, it is supposed to return nil.
4311The default is to call the function `split-window-sensibly' which
4312tries to split the window in a way which seems most suitable.
4313You can customize the options `split-height-threshold' and/or
4314`split-width-threshold' in order to have `split-window-sensibly'
4315prefer either vertical or horizontal splitting.
4316
f818cd2a
MR
4317If you set this to any other function, bear in mind that the
4318`display-buffer' routines may call this function two times. The
4319argument of the first call is the largest window on its frame.
4320If that call fails to return a live window, the function is
4321called again with the least recently used window as argument. If
4322that call fails too, `display-buffer' will use an existing window
4323to display its buffer.
8b10a2d1
MR
4324
4325The window selected at the time `display-buffer' was invoked is
4326still selected when this function is called. Hence you can
4327compare the window argument with the value of `selected-window'
4328if you intend to split the selected window instead or if you do
4329not want to split the selected window."
4330 :type 'function
3c448ab6
MR
4331 :version "23.1"
4332 :group 'windows)
4333
8b10a2d1 4334(defcustom split-height-threshold 80
f818cd2a
MR
4335 "Minimum height for splitting windows sensibly.
4336If this is an integer, `split-window-sensibly' may split a window
8b10a2d1 4337vertically only if it has at least this many lines. If this is
f818cd2a
MR
4338nil, `split-window-sensibly' is not allowed to split a window
4339vertically. If, however, a window is the only window on its
4340frame, `split-window-sensibly' may split it vertically
4341disregarding the value of this variable."
8b10a2d1 4342 :type '(choice (const nil) (integer :tag "lines"))
3c448ab6
MR
4343 :version "23.1"
4344 :group 'windows)
4345
8b10a2d1 4346(defcustom split-width-threshold 160
f818cd2a
MR
4347 "Minimum width for splitting windows sensibly.
4348If this is an integer, `split-window-sensibly' may split a window
8b10a2d1 4349horizontally only if it has at least this many columns. If this
f818cd2a
MR
4350is nil, `split-window-sensibly' is not allowed to split a window
4351horizontally."
8b10a2d1 4352 :type '(choice (const nil) (integer :tag "columns"))
3c448ab6
MR
4353 :version "23.1"
4354 :group 'windows)
9481c002 4355
8b10a2d1
MR
4356(defun window-splittable-p (window &optional horizontal)
4357 "Return non-nil if `split-window-sensibly' may split WINDOW.
4358Optional argument HORIZONTAL nil or omitted means check whether
4359`split-window-sensibly' may split WINDOW vertically. HORIZONTAL
4360non-nil means check whether WINDOW may be split horizontally.
3c448ab6 4361
8b10a2d1 4362WINDOW may be split vertically when the following conditions
3c448ab6 4363hold:
3c448ab6
MR
4364- `window-size-fixed' is either nil or equals `width' for the
4365 buffer of WINDOW.
8b10a2d1 4366- `split-height-threshold' is an integer and WINDOW is at least as
3c448ab6 4367 high as `split-height-threshold'.
3c448ab6
MR
4368- When WINDOW is split evenly, the emanating windows are at least
4369 `window-min-height' lines tall and can accommodate at least one
4370 line plus - if WINDOW has one - a mode line.
4371
8b10a2d1 4372WINDOW may be split horizontally when the following conditions
3c448ab6 4373hold:
3c448ab6
MR
4374- `window-size-fixed' is either nil or equals `height' for the
4375 buffer of WINDOW.
8b10a2d1 4376- `split-width-threshold' is an integer and WINDOW is at least as
3c448ab6 4377 wide as `split-width-threshold'.
3c448ab6
MR
4378- When WINDOW is split evenly, the emanating windows are at least
4379 `window-min-width' or two (whichever is larger) columns wide."
4380 (when (window-live-p window)
4381 (with-current-buffer (window-buffer window)
4382 (if horizontal
4383 ;; A window can be split horizontally when its width is not
4384 ;; fixed, it is at least `split-width-threshold' columns wide
4385 ;; and at least twice as wide as `window-min-width' and 2 (the
4386 ;; latter value is hardcoded).
4387 (and (memq window-size-fixed '(nil height))
4388 ;; Testing `window-full-width-p' here hardly makes any
4389 ;; sense nowadays. This can be done more intuitively by
4390 ;; setting up `split-width-threshold' appropriately.
4391 (numberp split-width-threshold)
4392 (>= (window-width window)
4393 (max split-width-threshold
4394 (* 2 (max window-min-width 2)))))
4395 ;; A window can be split vertically when its height is not
4396 ;; fixed, it is at least `split-height-threshold' lines high,
4397 ;; and it is at least twice as high as `window-min-height' and 2
4398 ;; if it has a modeline or 1.
4399 (and (memq window-size-fixed '(nil width))
4400 (numberp split-height-threshold)
4401 (>= (window-height window)
4402 (max split-height-threshold
4403 (* 2 (max window-min-height
4404 (if mode-line-format 2 1))))))))))
4405
8b10a2d1
MR
4406(defun split-window-sensibly (window)
4407 "Split WINDOW in a way suitable for `display-buffer'.
4408If `split-height-threshold' specifies an integer, WINDOW is at
4409least `split-height-threshold' lines tall and can be split
4410vertically, split WINDOW into two windows one above the other and
4411return the lower window. Otherwise, if `split-width-threshold'
4412specifies an integer, WINDOW is at least `split-width-threshold'
4413columns wide and can be split horizontally, split WINDOW into two
4414windows side by side and return the window on the right. If this
4415can't be done either and WINDOW is the only window on its frame,
4416try to split WINDOW vertically disregarding any value specified
4417by `split-height-threshold'. If that succeeds, return the lower
4418window. Return nil otherwise.
4419
4420By default `display-buffer' routines call this function to split
4421the largest or least recently used window. To change the default
4422customize the option `split-window-preferred-function'.
4423
4424You can enforce this function to not split WINDOW horizontally,
4425by setting \(or binding) the variable `split-width-threshold' to
4426nil. If, in addition, you set `split-height-threshold' to zero,
4427chances increase that this function does split WINDOW vertically.
4428
4429In order to not split WINDOW vertically, set \(or bind) the
4430variable `split-height-threshold' to nil. Additionally, you can
4431set `split-width-threshold' to zero to make a horizontal split
4432more likely to occur.
4433
4434Have a look at the function `window-splittable-p' if you want to
4435know how `split-window-sensibly' determines whether WINDOW can be
4436split."
f818cd2a 4437 (or (and (window-splittable-p window)
8b10a2d1
MR
4438 ;; Split window vertically.
4439 (with-selected-window window
2d197ffb 4440 (split-window-below)))
f818cd2a 4441 (and (window-splittable-p window t)
8b10a2d1
MR
4442 ;; Split window horizontally.
4443 (with-selected-window window
2d197ffb 4444 (split-window-right)))
8b10a2d1
MR
4445 (and (eq window (frame-root-window (window-frame window)))
4446 (not (window-minibuffer-p window))
4447 ;; If WINDOW is the only window on its frame and is not the
4448 ;; minibuffer window, try to split it vertically disregarding
4449 ;; the value of `split-height-threshold'.
4450 (let ((split-height-threshold 0))
f818cd2a 4451 (when (window-splittable-p window)
8b10a2d1 4452 (with-selected-window window
2d197ffb 4453 (split-window-below)))))))
9481c002 4454
f818cd2a
MR
4455(defun window--try-to-split-window (window)
4456 "Try to split WINDOW.
4457Return value returned by `split-window-preferred-function' if it
4458represents a live window, nil otherwise."
4459 (and (window-live-p window)
4460 (not (frame-parameter (window-frame window) 'unsplittable))
4461 (let ((new-window
4462 ;; Since `split-window-preferred-function' might
4463 ;; throw an error use `condition-case'.
4464 (condition-case nil
4465 (funcall split-window-preferred-function window)
4466 (error nil))))
4467 (and (window-live-p new-window) new-window))))
4468
4469(defun window--frame-usable-p (frame)
4470 "Return FRAME if it can be used to display a buffer."
4471 (when (frame-live-p frame)
4472 (let ((window (frame-root-window frame)))
4473 ;; `frame-root-window' may be an internal window which is considered
4474 ;; "dead" by `window-live-p'. Hence if `window' is not live we
4475 ;; implicitly know that `frame' has a visible window we can use.
4476 (unless (and (window-live-p window)
4477 (or (window-minibuffer-p window)
4478 ;; If the window is soft-dedicated, the frame is usable.
4479 ;; Actually, even if the window is really dedicated,
4480 ;; the frame is still usable by splitting it.
4481 ;; At least Emacs-22 allowed it, and it is desirable
4482 ;; when displaying same-frame windows.
4483 nil ; (eq t (window-dedicated-p window))
4484 ))
4485 frame))))
4486
4487(defcustom even-window-heights t
4488 "If non-nil `display-buffer' will try to even window heights.
4489Otherwise `display-buffer' will leave the window configuration
4490alone. Heights are evened only when `display-buffer' chooses a
4491window that appears above or below the selected window."
4492 :type 'boolean
4493 :group 'windows)
4494
4495(defun window--even-window-heights (window)
4496 "Even heights of WINDOW and selected window.
4497Do this only if these windows are vertically adjacent to each
4498other, `even-window-heights' is non-nil, and the selected window
4499is higher than WINDOW."
4500 (when (and even-window-heights
4501 (not (eq window (selected-window)))
4502 ;; Don't resize minibuffer windows.
4503 (not (window-minibuffer-p (selected-window)))
4504 (> (window-height (selected-window)) (window-height window))
4505 (eq (window-frame window) (window-frame (selected-window)))
4506 (let ((sel-edges (window-edges (selected-window)))
4507 (win-edges (window-edges window)))
4508 (and (= (nth 0 sel-edges) (nth 0 win-edges))
4509 (= (nth 2 sel-edges) (nth 2 win-edges))
4510 (or (= (nth 1 sel-edges) (nth 3 win-edges))
4511 (= (nth 3 sel-edges) (nth 1 win-edges))))))
4512 (let ((window-min-height 1))
4513 ;; Don't throw an error if we can't even window heights for
4514 ;; whatever reason.
4515 (condition-case nil
4516 (enlarge-window (/ (- (window-height window) (window-height)) 2))
4517 (error nil)))))
4518
4519(defun window--display-buffer-1 (window)
4520 "Raise the frame containing WINDOW.
4521Do not raise the selected frame. Return WINDOW."
4522 (let* ((frame (window-frame window))
4523 (visible (frame-visible-p frame)))
4524 (unless (or (not visible)
4525 ;; Assume the selected frame is already visible enough.
4526 (eq frame (selected-frame))
4527 ;; Assume the frame from which we invoked the minibuffer
4528 ;; is visible.
4529 (and (minibuffer-window-active-p (selected-window))
4530 (eq frame (window-frame (minibuffer-selected-window)))))
4531 (raise-frame frame))
4532 window))
4533
4534(defun window--display-buffer-2 (buffer window &optional dedicated)
4535 "Display BUFFER in WINDOW and make its frame visible.
4536Set `window-dedicated-p' to DEDICATED if non-nil.
4537Return WINDOW."
4538 (when (and (buffer-live-p buffer) (window-live-p window))
4539 (set-window-buffer window buffer)
4540 (when dedicated
4541 (set-window-dedicated-p window dedicated))
4542 (window--display-buffer-1 window)))
4543
4544(defvar display-buffer-mark-dedicated nil
4545 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
4546The actual non-nil value of this variable will be copied to the
4547`window-dedicated-p' flag.")
4548
f818cd2a
MR
4549(defun window-normalize-buffer-to-display (buffer-or-name)
4550 "Normalize BUFFER-OR-NAME argument for buffer display functions.
4551If BUFFER-OR-NAME is nil, return the curent buffer. Else, if a
4552buffer specified by BUFFER-OR-NAME exists, return that buffer.
4553If no such buffer exists, create a buffer with the name
4554BUFFER-OR-NAME and return that buffer."
4555 (if buffer-or-name
4556 (or (get-buffer buffer-or-name)
4557 (let ((buffer (get-buffer-create buffer-or-name)))
4558 (set-buffer-major-mode buffer)
4559 buffer))
4560 (current-buffer)))
4561
fa5660f9
CY
4562(defconst display-buffer--action-function-custom-type
4563 '(choice :tag "Function"
4564 (const :tag "--" ignore) ; default for insertion
fa5660f9 4565 (const display-buffer-reuse-window)
fa5660f9
CY
4566 (const display-buffer-use-some-window)
4567 (const display-buffer-same-window)
4568 (const display-buffer-pop-up-frame)
4569 (const display-buffer-use-some-window)
4570 (function :tag "Other function"))
4571 "Custom type for `display-buffer' action functions.")
4572
4573(defconst display-buffer--action-custom-type
4574 `(cons :tag "Action"
4575 (choice :tag "Action functions"
4576 ,display-buffer--action-function-custom-type
4577 (repeat
4578 :tag "List of functions"
4579 ,display-buffer--action-function-custom-type))
4580 (alist :tag "Action arguments"
4581 :key-type symbol
4582 :value-type (sexp :tag "Value")))
4583 "Custom type for `display-buffer' actions.")
4584
cbb0f9ab
CY
4585(defvar display-buffer-overriding-action '(nil . nil)
4586 "Overriding action to perform to display a buffer.
4587It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4588function or a list of functions. Each function should accept 2
4589arguments: a buffer to display and an alist similar to ALIST.
4590See `display-buffer' for details.")
4591(put 'display-buffer-overriding-action 'risky-local-variable t)
4592
fa5660f9 4593(defcustom display-buffer-alist nil
89894cd8
CY
4594 "Alist of conditional actions for `display-buffer'.
4595This is a list of elements (CONDITION . ACTION), where:
4596
4597 CONDITION is either a regexp matching buffer names, or a function
4598 that takes a buffer and returns a boolean.
4599
8319e0bf
CY
4600 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
4601 function or a list of functions. Each such function should
4602 accept 2 arguments: a buffer to display and an alist of the
fa5660f9
CY
4603 same form as ALIST. See `display-buffer' for details."
4604 :type `(alist :key-type
4605 (choice :tag "Condition"
4606 regexp
4607 (function :tag "Matcher function"))
4608 :value-type ,display-buffer--action-custom-type)
4609 :risky t
4610 :version "24.1"
4611 :group 'windows)
89894cd8 4612
cbb0f9ab
CY
4613(defcustom display-buffer-base-action '(nil . nil)
4614 "User-specified default action for `display-buffer'.
8319e0bf
CY
4615It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4616function or a list of functions. Each function should accept 2
fa5660f9
CY
4617arguments: a buffer to display and an alist similar to ALIST.
4618See `display-buffer' for details."
4619 :type display-buffer--action-custom-type
4620 :risky t
4621 :version "24.1"
4622 :group 'windows)
89894cd8 4623
cbb0f9ab
CY
4624(defconst display-buffer-fallback-action
4625 '((display-buffer--maybe-same-window
4626 display-buffer-reuse-window
4627 display-buffer--special
4628 display-buffer--maybe-pop-up-frame-or-window
4629 display-buffer-use-some-window
4630 ;; If all else fails, pop up a new frame.
4631 display-buffer-pop-up-frame))
4632 "Default fallback action for `display-buffer'.
4633This is the action used by `display-buffer' if no other actions
4634specified, e.g. by the user options `display-buffer-alist' or
4635`display-buffer-base-action'. See `display-buffer'.")
4636(put 'display-buffer-fallback-action 'risky-local-variable t)
f818cd2a
MR
4637
4638(defun display-buffer-assq-regexp (buffer-name alist)
4639 "Retrieve ALIST entry corresponding to BUFFER-NAME."
4640 (catch 'match
4641 (dolist (entry alist)
4642 (let ((key (car entry))
4643 (value (cdr entry)))
4644 (when (or (and (stringp key)
4645 (string-match-p key buffer-name))
4646 (and (symbolp key) (functionp key)
4647 (funcall key buffer-name alist)))
4648 (throw 'match (cdr entry)))))))
4649
8319e0bf
CY
4650(defvar display-buffer--same-window-action
4651 '(display-buffer-same-window
4652 (inhibit-same-window . nil))
4653 "A `display-buffer' action for displaying in the same window.")
4654(put 'display-buffer--same-window-action 'risky-local-variable t)
4655
4656(defvar display-buffer--other-frame-action
4657 '((display-buffer-reuse-window
4658 display-buffer--special
4659 display-buffer-pop-up-frame)
4660 (reusable-frames . 0)
4661 (inhibit-same-window . t))
4662 "A `display-buffer' action for displaying in another frame.")
4663(put 'display-buffer--other-frame-action 'risky-local-variable t)
4664
f818cd2a 4665(defun display-buffer (&optional buffer-or-name action frame)
3199b96f 4666 "Display BUFFER-OR-NAME in some window, without selecting it.
89894cd8
CY
4667BUFFER-OR-NAME must be a buffer or the name of an existing
4668buffer. Return the window chosen for displaying BUFFER-OR-NAME,
4669or nil if no such window is found.
4670
4671Optional argument ACTION should have the form (FUNCTION . ALIST).
4672FUNCTION is either a function or a list of functions. Each such
4673function is called with 2 arguments: the buffer to display and an
4674alist. It should either display the buffer and return the
fa5660f9 4675window, or return nil if unable to display the buffer.
89894cd8 4676
cbb0f9ab
CY
4677The `display-buffer' function builds a function list and an alist
4678from `display-buffer-overriding-action', `display-buffer-alist',
4679the ACTION argument, `display-buffer-base-action', and
4680`display-buffer-fallback-action' (in that order). Then it calls
4681each function in the combined function list in turn, passing the
4682buffer as the first argument and the combined alist as the second
4683argument, until one of the functions returns non-nil.
8319e0bf
CY
4684
4685Available action functions include:
4686 `display-buffer-same-window'
8319e0bf
CY
4687 `display-buffer-reuse-window'
4688 `display-buffer-pop-up-frame'
4689 `display-buffer-pop-up-window'
4690 `display-buffer-use-some-window'
4691
4692Recognized alist entries include:
4693
4694 `inhibit-same-window' -- A non-nil value prevents the same
4695 window from being used for display.
4696
4697 `reusable-frames' -- Value specifies frame(s) to search for a
4698 window that already displays the buffer.
4699 See `display-buffer-reuse-window'.
2a7bdc1a
CY
4700
4701The ACTION argument to `display-buffer' can also have a non-nil
4702and non-list value. This means to display the buffer in a window
4703other than the selected one, even if it is already displayed in
4704the selected window. If called interactively with a prefix
4705argument, ACTION is t.
89894cd8 4706
8319e0bf
CY
4707Optional argument FRAME, if non-nil, acts like an additional
4708ALIST entry (reusable-frames . FRAME), specifying the frame(s) to
4709search for a window that is already displaying the buffer. See
4710`display-buffer-reuse-window'."
c3313451
CY
4711 (interactive (list (read-buffer "Display buffer: " (other-buffer))
4712 (if current-prefix-arg t)))
89894cd8
CY
4713 (let ((buffer (window-normalize-buffer-to-display buffer-or-name))
4714 ;; Handle the old form of the first argument.
4715 (inhibit-same-window (and action (not (listp action)))))
4716 (unless (listp action) (setq action nil))
4717 (if display-buffer-function
4718 ;; If `display-buffer-function' is defined, let it do the job.
4719 (funcall display-buffer-function buffer inhibit-same-window)
4720 ;; Otherwise, use the defined actions.
4721 (let* ((user-action
4722 (display-buffer-assq-regexp (buffer-name buffer)
4723 display-buffer-alist))
4724 ;; Extra actions from the arguments to this function:
4725 (extra-action
4726 (cons nil (append (if inhibit-same-window
4727 '((inhibit-same-window . t)))
4728 (if frame
8319e0bf 4729 `((reusable-frames . ,frame))))))
89894cd8
CY
4730 ;; Construct action function list and action alist.
4731 (actions (list display-buffer-overriding-action
4732 user-action action extra-action
cbb0f9ab
CY
4733 display-buffer-base-action
4734 display-buffer-fallback-action))
89894cd8
CY
4735 (functions (apply 'append
4736 (mapcar (lambda (x)
4737 (setq x (car x))
c3313451 4738 (if (functionp x) (list x) x))
89894cd8
CY
4739 actions)))
4740 (alist (apply 'append (mapcar 'cdr actions)))
4741 window)
4742 (unless (buffer-live-p buffer)
4743 (error "Invalid buffer"))
4744 (while (and functions (not window))
4745 (setq window (funcall (car functions) buffer alist)
4746 functions (cdr functions)))
4747 window))))
f818cd2a
MR
4748
4749(defun display-buffer-other-frame (buffer)
4750 "Display buffer BUFFER in another frame.
4751This uses the function `display-buffer' as a subroutine; see
4752its documentation for additional customization information."
4753 (interactive "BDisplay buffer in other frame: ")
8319e0bf 4754 (display-buffer buffer display-buffer--other-frame-action t))
f818cd2a 4755
89894cd8 4756;;; `display-buffer' action functions:
437014c8 4757
89894cd8 4758(defun display-buffer-same-window (buffer alist)
8319e0bf
CY
4759 "Display BUFFER in the selected window.
4760This fails if ALIST has a non-nil `inhibit-same-window' entry, or
4761if the selected window is a minibuffer window or is dedicated to
4762another buffer; in that case, return nil. Otherwise, return the
4763selected window."
89894cd8
CY
4764 (unless (or (cdr (assq 'inhibit-same-window alist))
4765 (window-minibuffer-p)
4766 (window-dedicated-p))
cf4eacfd 4767 (display-buffer-record-window 'reuse (selected-window) buffer)
89894cd8
CY
4768 (window--display-buffer-2 buffer (selected-window))))
4769
0d3ff375 4770(defun display-buffer--maybe-same-window (buffer alist)
8319e0bf
CY
4771 "Conditionally display BUFFER in the selected window.
4772If `same-window-p' returns non-nil for BUFFER's name, call
4773`display-buffer-same-window' and return its value. Otherwise,
4774return nil."
89894cd8
CY
4775 (and (same-window-p (buffer-name buffer))
4776 (display-buffer-same-window buffer alist)))
4777
4778(defun display-buffer-reuse-window (buffer alist)
4779 "Return a window that is already displaying BUFFER.
8319e0bf
CY
4780Return nil if no usable window is found.
4781
4782If ALIST has a non-nil `inhibit-same-window' entry, the selected
4783window is not eligible for reuse.
4784
4785If ALIST contains a `reusable-frames' entry, its value determines
4786which frames to search for a reusable window:
4787 nil -- the selected frame (actually the last non-minibuffer frame)
4788 A frame -- just that frame
4789 `visible' -- all visible frames
4790 0 -- all frames on the current terminal
4791 t -- all frames.
4792
4793If ALIST contains no `reusable-frames' entry, search just the
4794selected frame if `display-buffer-reuse-frames' and
4795`pop-up-frames' are both nil; search all frames on the current
4796terminal if either of those variables is non-nil."
4797 (let* ((alist-entry (assq 'reusable-frames alist))
4798 (frames (cond (alist-entry (cdr alist-entry))
4799 ((if (eq pop-up-frames 'graphic-only)
4800 (display-graphic-p)
4801 pop-up-frames)
4802 0)
4803 (display-buffer-reuse-frames 0)
4804 (t (last-nonminibuffer-frame))))
4805 (window (if (and (eq buffer (window-buffer))
4806 (not (cdr (assq 'inhibit-same-window alist))))
4807 (selected-window)
4808 (car (delq (selected-window)
4809 (get-buffer-window-list buffer 'nomini
4810 frames))))))
89894cd8 4811 (when window
cf4eacfd 4812 (display-buffer-record-window 'reuse window buffer)
89894cd8
CY
4813 (window--display-buffer-1 window))))
4814
8319e0bf
CY
4815(defun display-buffer--special (buffer alist)
4816 "Try to display BUFFER using `special-display-function'.
4817Call `special-display-p' on BUFFER's name, and if that returns
4818non-nil, call `special-display-function' on BUFFER."
4819 (and special-display-function
4820 ;; `special-display-p' returns either t or a list of frame
4821 ;; parameters to pass to `special-display-function'.
4822 (let ((pars (special-display-p (buffer-name buffer))))
4823 (when pars
4824 (funcall special-display-function
4825 buffer (if (listp pars) pars))))))
4826
89894cd8
CY
4827(defun display-buffer-pop-up-frame (buffer alist)
4828 "Display BUFFER in a new frame.
8319e0bf
CY
4829This works by calling `pop-up-frame-function'. If successful,
4830return the window used; otherwise return nil."
89894cd8
CY
4831 (let ((fun pop-up-frame-function)
4832 frame window)
4833 (when (and fun
4834 (setq frame (funcall fun))
4835 (setq window (frame-selected-window frame)))
cf4eacfd 4836 (display-buffer-record-window 'frame window buffer)
0e2070b5 4837 (window--display-buffer-2 buffer window display-buffer-mark-dedicated)
1f3c99ca
MR
4838 ;; Reset list of WINDOW's previous buffers to nil.
4839 (set-window-prev-buffers window nil)
4840 window)))
89894cd8
CY
4841
4842(defun display-buffer-pop-up-window (buffer alist)
4843 "Display BUFFER by popping up a new window.
4844The new window is created on the selected frame, or in
4845`last-nonminibuffer-frame' if no windows can be created there.
4846If sucessful, return the new window; otherwise return nil."
4847 (let ((frame (or (window--frame-usable-p (selected-frame))
4848 (window--frame-usable-p (last-nonminibuffer-frame))))
4849 window)
4850 (when (and (or (not (frame-parameter frame 'unsplittable))
4851 ;; If the selected frame cannot be split, look at
4852 ;; `last-nonminibuffer-frame'.
4853 (and (eq frame (selected-frame))
4854 (setq frame (last-nonminibuffer-frame))
4855 (window--frame-usable-p frame)
4856 (not (frame-parameter frame 'unsplittable))))
4857 ;; Attempt to split largest or least recently used window.
4858 (setq window (or (window--try-to-split-window
4859 (get-largest-window frame t))
4860 (window--try-to-split-window
4861 (get-lru-window frame t)))))
cf4eacfd 4862 (display-buffer-record-window 'window window buffer)
0e2070b5 4863 (window--display-buffer-2 buffer window display-buffer-mark-dedicated)
1f3c99ca
MR
4864 ;; Reset list of WINDOW's previous buffers to nil.
4865 (set-window-prev-buffers window nil)
4866 window)))
89894cd8 4867
8319e0bf
CY
4868(defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
4869 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
4870
4871If `pop-up-frames' is non-nil (and not `graphic-only' on a
4872text-only terminal), try with `display-buffer-pop-up-frame'.
4873
4874If that cannot be done, and `pop-up-windows' is non-nil, try
4875again with `display-buffer-pop-up-window'."
4876 (or (and (if (eq pop-up-frames 'graphic-only)
4877 (display-graphic-p)
4878 pop-up-frames)
4879 (display-buffer-pop-up-frame buffer alist))
4880 (and pop-up-windows
4881 (display-buffer-pop-up-window buffer alist))))
89894cd8
CY
4882
4883(defun display-buffer-use-some-window (buffer alist)
4884 "Display BUFFER in an existing window.
4885Search for a usable window, set that window to the buffer, and
4886return the window. If no suitable window is found, return nil."
4887 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
4888 (window-to-undedicate
4889 ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate the
4890 ;; selected window to its buffer, to prevent any of the
4891 ;; `get-' routines below from choosing it. (Bug#1415)
4892 (and not-this-window (not (window-dedicated-p))
4893 (set-window-dedicated-p (selected-window) t)
4894 (selected-window)))
4895 (frame (or (window--frame-usable-p (selected-frame))
4896 (window--frame-usable-p (last-nonminibuffer-frame))))
8319e0bf 4897 window)
89894cd8
CY
4898 (unwind-protect
4899 (setq window
4900 ;; Reuse an existing window.
4901 (or (get-lru-window frame)
4902 (let ((window (get-buffer-window buffer 'visible)))
4903 (unless (and not-this-window
4904 (eq window (selected-window)))
4905 window))
4906 (get-largest-window 'visible)
4907 (let ((window (get-buffer-window buffer 0)))
4908 (unless (and not-this-window
4909 (eq window (selected-window)))
4910 window))
8319e0bf 4911 (get-largest-window 0)))
89894cd8
CY
4912 (when (window-live-p window-to-undedicate)
4913 ;; Restore dedicated status of selected window.
4914 (set-window-dedicated-p window-to-undedicate nil)))
4915 (when window
cf4eacfd 4916 (display-buffer-record-window 'reuse window buffer)
89894cd8
CY
4917 (window--even-window-heights window)
4918 (window--display-buffer-2 buffer window))))
437014c8
CY
4919
4920;;; Display + selection commands:
4921
c3313451
CY
4922(defun pop-to-buffer (buffer &optional action norecord)
4923 "Select buffer BUFFER in some window, preferably a different one.
4924BUFFER may be a buffer, a string (a buffer name), or nil. If it
4925is a string not naming an existent buffer, create a buffer with
4926that name. If BUFFER is nil, choose some other buffer. Return
4927the buffer.
4928
4929This uses `display-buffer' as a subroutine. The optional ACTION
4930argument is passed to `display-buffer' as its ACTION argument.
4931See `display-buffer' for more information. ACTION is t if called
4932interactively with a prefix argument, which means to pop to a
4933window other than the selected one even if the buffer is already
4934displayed in the selected window.
4935
4936If the window to show BUFFER is not on the selected
f818cd2a
MR
4937frame, raise that window's frame and give it input focus.
4938
f818cd2a
MR
4939Optional third arg NORECORD non-nil means do not put this buffer
4940at the front of the list of recently selected ones."
c3313451
CY
4941 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
4942 (if current-prefix-arg t)))
8319e0bf 4943 (setq buffer (window-normalize-buffer-to-switch-to buffer))
c3313451
CY
4944 (set-buffer buffer)
4945 (let* ((old-window (selected-window))
4946 (old-frame (selected-frame))
8319e0bf 4947 (window (display-buffer buffer action))
c3313451 4948 (frame (window-frame window)))
97adfb97
CY
4949 ;; If we chose another frame, make sure it gets input focus.
4950 (unless (eq frame old-frame)
c3313451 4951 (select-frame-set-input-focus frame norecord))
97adfb97
CY
4952 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
4953 (select-window window norecord)
c3313451 4954 buffer))
f818cd2a 4955
72258fe5
CY
4956(defun pop-to-buffer-same-window (buffer &optional norecord)
4957 "Select buffer BUFFER in some window, preferably the same one.
4958This function behaves much like `switch-to-buffer', except it
4959displays with `special-display-function' if BUFFER has a match in
4960`special-display-buffer-names' or `special-display-regexps'.
4961
4962Unlike `pop-to-buffer', this function prefers using the selected
4963window over popping up a new window or frame.
4964
4965BUFFER may be a buffer, a string (a buffer name), or nil. If it
4966is a string not naming an existent buffer, create a buffer with
4967that name. If BUFFER is nil, choose some other buffer. Return
4968the buffer.
4969
4970NORECORD, if non-nil means do not put this buffer at the front of
4971the list of recently selected ones."
4972 (pop-to-buffer buffer
4973 '((display-buffer--special
4974 display-buffer-same-window)
4975 (inhibit-same-window . nil))
4976 norecord))
4977
f818cd2a
MR
4978(defun read-buffer-to-switch (prompt)
4979 "Read the name of a buffer to switch to, prompting with PROMPT.
4980Return the neame of the buffer as a string.
4981
4982This function is intended for the `switch-to-buffer' family of
4983commands since these need to omit the name of the current buffer
4984from the list of completions and default values."
4985 (let ((rbts-completion-table (internal-complete-buffer-except)))
4986 (minibuffer-with-setup-hook
4987 (lambda ()
4988 (setq minibuffer-completion-table rbts-completion-table)
4989 ;; Since rbts-completion-table is built dynamically, we
4990 ;; can't just add it to the default value of
4991 ;; icomplete-with-completion-tables, so we add it
4992 ;; here manually.
4993 (if (and (boundp 'icomplete-with-completion-tables)
4994 (listp icomplete-with-completion-tables))
4995 (set (make-local-variable 'icomplete-with-completion-tables)
4996 (cons rbts-completion-table
4997 icomplete-with-completion-tables))))
4998 (read-buffer prompt (other-buffer (current-buffer))
4999 (confirm-nonexistent-file-or-buffer)))))
5000
5001(defun window-normalize-buffer-to-switch-to (buffer-or-name)
5002 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5003If BUFFER-OR-NAME is nil, return the buffer returned by
5004`other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5005exists, return that buffer. If no such buffer exists, create a
5006buffer with the name BUFFER-OR-NAME and return that buffer."
5007 (if buffer-or-name
5008 (or (get-buffer buffer-or-name)
5009 (let ((buffer (get-buffer-create buffer-or-name)))
5010 (set-buffer-major-mode buffer)
5011 buffer))
5012 (other-buffer)))
5013
5014(defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
5015 "Switch to buffer BUFFER-OR-NAME in the selected window.
5016If called interactively, prompt for the buffer name using the
5017minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5018determines whether to request confirmation before creating a new
5019buffer.
5020
5021BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5022nil. If BUFFER-OR-NAME is a string that does not identify an
5023existing buffer, create a buffer with that name. If
5024BUFFER-OR-NAME is nil, switch to the buffer returned by
5025`other-buffer'.
5026
5027Optional argument NORECORD non-nil means do not put the buffer
5028specified by BUFFER-OR-NAME at the front of the buffer list and
5029do not make the window displaying it the most recently selected
5030one.
5031
5032If FORCE-SAME-WINDOW is non-nil, BUFFER-OR-NAME must be displayed
94e0933e
CY
5033in the selected window; signal an error if that is
5034impossible (e.g. if the selected window is minibuffer-only). If
235ce86f 5035nil, BUFFER-OR-NAME may be displayed in another window.
f818cd2a
MR
5036
5037Return the buffer switched to."
5038 (interactive
acc825c5 5039 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
f818cd2a
MR
5040 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
5041 (if (null force-same-window)
8319e0bf 5042 (pop-to-buffer buffer display-buffer--same-window-action norecord)
f818cd2a
MR
5043 (cond
5044 ;; Don't call set-window-buffer if it's not needed since it
5045 ;; might signal an error (e.g. if the window is dedicated).
5046 ((eq buffer (window-buffer)))
5047 ((window-minibuffer-p)
5048 (error "Cannot switch buffers in minibuffer window"))
5049 ((eq (window-dedicated-p) t)
5050 (error "Cannot switch buffers in a dedicated window"))
5051 (t (set-window-buffer nil buffer)))
5052
5053 (unless norecord
5054 (select-window (selected-window)))
5055 (set-buffer buffer))))
5056
5057(defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5058 "Select the buffer specified by BUFFER-OR-NAME in another window.
5059BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5060nil. Return the buffer switched to.
5061
5062If called interactively, prompt for the buffer name using the
5063minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5064determines whether to request confirmation before creating a new
5065buffer.
5066
5067If BUFFER-OR-NAME is a string and does not identify an existing
5068buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5069nil, switch to the buffer returned by `other-buffer'.
5070
5071Optional second argument NORECORD non-nil means do not put this
5072buffer at the front of the list of recently selected ones.
5073
5074This uses the function `display-buffer' as a subroutine; see its
5075documentation for additional customization information."
5076 (interactive
5077 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
8319e0bf
CY
5078 (let ((pop-up-windows t))
5079 (pop-to-buffer buffer-or-name t norecord)))
f818cd2a
MR
5080
5081(defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5082 "Switch to buffer BUFFER-OR-NAME in another frame.
5083BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5084nil. Return the buffer switched to.
5085
5086If called interactively, prompt for the buffer name using the
5087minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5088determines whether to request confirmation before creating a new
5089buffer.
5090
5091If BUFFER-OR-NAME is a string and does not identify an existing
5092buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5093nil, switch to the buffer returned by `other-buffer'.
5094
5095Optional second arg NORECORD non-nil means do not put this
5096buffer at the front of the list of recently selected ones.
5097
5098This uses the function `display-buffer' as a subroutine; see its
5099documentation for additional customization information."
5100 (interactive
5101 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
8319e0bf 5102 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
3c448ab6
MR
5103\f
5104(defun set-window-text-height (window height)
9992ea0c 5105 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6198ccd0
MR
5106WINDOW must be a live window. HEIGHT doesn't include the mode
5107line or header line, if any, or any partial-height lines in the
5108text display area.
3c448ab6
MR
5109
5110Note that the current implementation of this function cannot
5111always set the height exactly, but attempts to be conservative,
5112by allocating more lines than are actually needed in the case
5113where some error may be present."
447f16b8 5114 (setq window (window-normalize-window window t))
3c448ab6
MR
5115 (let ((delta (- height (window-text-height window))))
5116 (unless (zerop delta)
5117 ;; Setting window-min-height to a value like 1 can lead to very
5118 ;; bizarre displays because it also allows Emacs to make *other*
5119 ;; windows 1-line tall, which means that there's no more space for
5120 ;; the modeline.
5121 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
d615d6d2 5122 (window-resize window delta)))))
3c448ab6 5123
6198ccd0
MR
5124(defun enlarge-window-horizontally (delta)
5125 "Make selected window DELTA columns wider.
3c448ab6
MR
5126Interactively, if no argument is given, make selected window one
5127column wider."
5128 (interactive "p")
6198ccd0 5129 (enlarge-window delta t))
3c448ab6 5130
6198ccd0
MR
5131(defun shrink-window-horizontally (delta)
5132 "Make selected window DELTA columns narrower.
3c448ab6
MR
5133Interactively, if no argument is given, make selected window one
5134column narrower."
5135 (interactive "p")
6198ccd0 5136 (shrink-window delta t))
3c448ab6
MR
5137
5138(defun count-screen-lines (&optional beg end count-final-newline window)
5139 "Return the number of screen lines in the region.
5140The number of screen lines may be different from the number of actual lines,
5141due to line breaking, display table, etc.
5142
5143Optional arguments BEG and END default to `point-min' and `point-max'
5144respectively.
5145
5146If region ends with a newline, ignore it unless optional third argument
5147COUNT-FINAL-NEWLINE is non-nil.
5148
5149The optional fourth argument WINDOW specifies the window used for obtaining
5150parameters such as width, horizontal scrolling, and so on. The default is
5151to use the selected window's parameters.
5152
5153Like `vertical-motion', `count-screen-lines' always uses the current buffer,
5154regardless of which buffer is displayed in WINDOW. This makes possible to use
5155`count-screen-lines' in any buffer, whether or not it is currently displayed
5156in some window."
5157 (unless beg
5158 (setq beg (point-min)))
5159 (unless end
5160 (setq end (point-max)))
5161 (if (= beg end)
5162 0
5163 (save-excursion
5164 (save-restriction
5165 (widen)
5166 (narrow-to-region (min beg end)
5167 (if (and (not count-final-newline)
5168 (= ?\n (char-before (max beg end))))
5169 (1- (max beg end))
5170 (max beg end)))
5171 (goto-char (point-min))
5172 (1+ (vertical-motion (buffer-size) window))))))
5173
6198ccd0
MR
5174(defun window-buffer-height (window)
5175 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
5176 (with-current-buffer (window-buffer window)
5177 (max 1
5178 (count-screen-lines (point-min) (point-max)
5179 ;; If buffer ends with a newline, ignore it when
5180 ;; counting height unless point is after it.
5181 (eobp)
5182 window))))
5183
5184;;; Resizing buffers to fit their contents exactly.
5185(defun fit-window-to-buffer (&optional window max-height min-height override)
3c448ab6 5186 "Adjust height of WINDOW to display its buffer's contents exactly.
6198ccd0
MR
5187WINDOW can be any live window and defaults to the selected one.
5188
5189Optional argument MAX-HEIGHT specifies the maximum height of
5190WINDOW and defaults to the height of WINDOW's frame. Optional
5191argument MIN-HEIGHT specifies the minimum height of WINDOW and
5192defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
5193are specified in lines and include the mode line and header line,
5194if any.
5195
5196Optional argument OVERRIDE non-nil means override restrictions
5197imposed by `window-min-height' and `window-min-width' on the size
5198of WINDOW.
5199
5200Return the number of lines by which WINDOW was enlarged or
5201shrunk. If an error occurs during resizing, return nil but don't
5202signal an error.
5203
5204Note that even if this function makes WINDOW large enough to show
5205_all_ lines of its buffer you might not see the first lines when
5206WINDOW was scrolled."
f7baca20
MR
5207 (interactive)
5208 ;; Do all the work in WINDOW and its buffer and restore the selected
5209 ;; window and the current buffer when we're done.
447f16b8 5210 (setq window (window-normalize-window window t))
6198ccd0 5211 ;; Can't resize a full height or fixed-size window.
9173deec 5212 (unless (or (window-size-fixed-p window)
6198ccd0
MR
5213 (window-full-height-p window))
5214 ;; `with-selected-window' should orderly restore the current buffer.
5215 (with-selected-window window
5216 ;; We are in WINDOW's buffer now.
938b94c8 5217 (let* (;; Adjust MIN-HEIGHT.
6198ccd0
MR
5218 (min-height
5219 (if override
5220 (window-min-size window nil window)
5221 (max (or min-height window-min-height)
5222 window-safe-min-height)))
5223 (max-window-height
5224 (window-total-size (frame-root-window window)))
5225 ;; Adjust MAX-HEIGHT.
5226 (max-height
5227 (if (or override (not max-height))
5228 max-window-height
5229 (min max-height max-window-height)))
5230 ;; Make `desired-height' the height necessary to show
5231 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
5232 ;; and MAX-HEIGHT.
5233 (desired-height
5234 (max
5235 (min
5236 (+ (count-screen-lines)
5237 ;; For non-minibuffers count the mode line, if any.
5238 (if (and (not (window-minibuffer-p window))
5239 mode-line-format)
5240 1
5241 0)
5242 ;; Count the header line, if any.
5243 (if header-line-format 1 0))
5244 max-height)
5245 min-height))
5246 (desired-delta
5247 (- desired-height (window-total-size window)))
5248 (delta
5249 (if (> desired-delta 0)
5250 (min desired-delta
5251 (window-max-delta window nil window))
5252 (max desired-delta
5253 (- (window-min-delta window nil window))))))
5254 ;; This `condition-case' shouldn't be necessary, but who knows?
5255 (condition-case nil
5256 (if (zerop delta)
5257 ;; Return zero if DELTA became zero in the proces.
5258 0
5259 ;; Don't try to redisplay with the cursor at the end on its
5260 ;; own line--that would force a scroll and spoil things.
5261 (when (and (eobp) (bolp) (not (bobp)))
5262 ;; It's silly to put `point' at the end of the previous
5263 ;; line and so maybe force horizontal scrolling.
5264 (set-window-point window (line-beginning-position 0)))
d615d6d2
MR
5265 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
5266 (window-resize window delta nil window)
f7baca20
MR
5267 ;; Check if the last line is surely fully visible. If
5268 ;; not, enlarge the window.
5269 (let ((end (save-excursion
5270 (goto-char (point-max))
5271 (when (and (bolp) (not (bobp)))
5272 ;; Don't include final newline.
5273 (backward-char 1))
5274 (when truncate-lines
5275 ;; If line-wrapping is turned off, test the
5276 ;; beginning of the last line for
5277 ;; visibility instead of the end, as the
5278 ;; end of the line could be invisible by
5279 ;; virtue of extending past the edge of the
5280 ;; window.
5281 (forward-line 0))
5282 (point))))
5283 (set-window-vscroll window 0)
6198ccd0
MR
5284 ;; This loop might in some rare pathological cases raise
5285 ;; an error - another reason for the `condition-case'.
f7baca20 5286 (while (and (< desired-height max-height)
6198ccd0 5287 (= desired-height (window-total-size))
f7baca20 5288 (not (pos-visible-in-window-p end)))
d615d6d2 5289 (window-resize window 1 nil window)
6198ccd0
MR
5290 (setq desired-height (1+ desired-height)))))
5291 (error (setq delta nil)))
5292 delta))))
3c448ab6 5293
39cffb44
MR
5294(defun window-safely-shrinkable-p (&optional window)
5295 "Return t if WINDOW can be shrunk without shrinking other windows.
5296WINDOW defaults to the selected window."
5297 (with-selected-window (or window (selected-window))
5298 (let ((edges (window-edges)))
39cffb44
MR
5299 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
5300 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
39cffb44 5301
3c448ab6
MR
5302(defun shrink-window-if-larger-than-buffer (&optional window)
5303 "Shrink height of WINDOW if its buffer doesn't need so many lines.
5304More precisely, shrink WINDOW vertically to be as small as
5305possible, while still showing the full contents of its buffer.
5306WINDOW defaults to the selected window.
5307
6198ccd0
MR
5308Do not shrink WINDOW to less than `window-min-height' lines. Do
5309nothing if the buffer contains more lines than the present window
5310height, or if some of the window's contents are scrolled out of
5311view, or if shrinking this window would also shrink another
5312window, or if the window is the only window of its frame.
3c448ab6
MR
5313
5314Return non-nil if the window was shrunk, nil otherwise."
5315 (interactive)
447f16b8 5316 (setq window (window-normalize-window window t))
6198ccd0
MR
5317 ;; Make sure that WINDOW is vertically combined and `point-min' is
5318 ;; visible (for whatever reason that's needed). The remaining issues
5319 ;; should be taken care of by `fit-window-to-buffer'.
3d8daefe 5320 (when (and (window-combined-p window)
6198ccd0
MR
5321 (pos-visible-in-window-p (point-min) window))
5322 (fit-window-to-buffer window (window-total-size window))))
5323\f
3c448ab6
MR
5324(defun kill-buffer-and-window ()
5325 "Kill the current buffer and delete the selected window."
5326 (interactive)
5327 (let ((window-to-delete (selected-window))
5328 (buffer-to-kill (current-buffer))
6198ccd0 5329 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
3c448ab6
MR
5330 (unwind-protect
5331 (progn
5332 (add-hook 'kill-buffer-hook delete-window-hook t t)
5333 (if (kill-buffer (current-buffer))
5334 ;; If `delete-window' failed before, we rerun it to regenerate
5335 ;; the error so it can be seen in the echo area.
5336 (when (eq (selected-window) window-to-delete)
5337 (delete-window))))
5338 ;; If the buffer is not dead for some reason (probably because
5339 ;; of a `quit' signal), remove the hook again.
6198ccd0
MR
5340 (ignore-errors
5341 (with-current-buffer buffer-to-kill
5342 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
3c448ab6 5343
74f806a1 5344\f
3c448ab6
MR
5345(defvar recenter-last-op nil
5346 "Indicates the last recenter operation performed.
0116abbd
JL
5347Possible values: `top', `middle', `bottom', integer or float numbers.")
5348
5349(defcustom recenter-positions '(middle top bottom)
5350 "Cycling order for `recenter-top-bottom'.
5351A list of elements with possible values `top', `middle', `bottom',
5352integer or float numbers that define the cycling order for
5353the command `recenter-top-bottom'.
5354
5355Top and bottom destinations are `scroll-margin' lines the from true
5356window top and bottom. Middle redraws the frame and centers point
5357vertically within the window. Integer number moves current line to
5358the specified absolute window-line. Float number between 0.0 and 1.0
5359means the percentage of the screen space from the top. The default
5360cycling order is middle -> top -> bottom."
5361 :type '(repeat (choice
5362 (const :tag "Top" top)
5363 (const :tag "Middle" middle)
5364 (const :tag "Bottom" bottom)
5365 (integer :tag "Line number")
5366 (float :tag "Percentage")))
5367 :version "23.2"
5368 :group 'windows)
3c448ab6
MR
5369
5370(defun recenter-top-bottom (&optional arg)
0116abbd
JL
5371 "Move current buffer line to the specified window line.
5372With no prefix argument, successive calls place point according
5373to the cycling order defined by `recenter-positions'.
3c448ab6
MR
5374
5375A prefix argument is handled like `recenter':
5376 With numeric prefix ARG, move current line to window-line ARG.
0116abbd 5377 With plain `C-u', move current line to window center."
3c448ab6
MR
5378 (interactive "P")
5379 (cond
0116abbd 5380 (arg (recenter arg)) ; Always respect ARG.
3c448ab6 5381 (t
0116abbd
JL
5382 (setq recenter-last-op
5383 (if (eq this-command last-command)
5384 (car (or (cdr (member recenter-last-op recenter-positions))
5385 recenter-positions))
5386 (car recenter-positions)))
3c448ab6
MR
5387 (let ((this-scroll-margin
5388 (min (max 0 scroll-margin)
5389 (truncate (/ (window-body-height) 4.0)))))
5390 (cond ((eq recenter-last-op 'middle)
0116abbd 5391 (recenter))
3c448ab6 5392 ((eq recenter-last-op 'top)
0116abbd
JL
5393 (recenter this-scroll-margin))
5394 ((eq recenter-last-op 'bottom)
5395 (recenter (- -1 this-scroll-margin)))
5396 ((integerp recenter-last-op)
5397 (recenter recenter-last-op))
5398 ((floatp recenter-last-op)
5399 (recenter (round (* recenter-last-op (window-height))))))))))
3c448ab6
MR
5400
5401(define-key global-map [?\C-l] 'recenter-top-bottom)
216349f8 5402
216349f8
SM
5403(defun move-to-window-line-top-bottom (&optional arg)
5404 "Position point relative to window.
5405
0f202d5d 5406With a prefix argument ARG, acts like `move-to-window-line'.
216349f8
SM
5407
5408With no argument, positions point at center of window.
0116abbd
JL
5409Successive calls position point at positions defined
5410by `recenter-positions'."
216349f8
SM
5411 (interactive "P")
5412 (cond
0116abbd 5413 (arg (move-to-window-line arg)) ; Always respect ARG.
216349f8 5414 (t
0116abbd
JL
5415 (setq recenter-last-op
5416 (if (eq this-command last-command)
5417 (car (or (cdr (member recenter-last-op recenter-positions))
5418 recenter-positions))
5419 (car recenter-positions)))
216349f8
SM
5420 (let ((this-scroll-margin
5421 (min (max 0 scroll-margin)
5422 (truncate (/ (window-body-height) 4.0)))))
0f202d5d 5423 (cond ((eq recenter-last-op 'middle)
0116abbd 5424 (call-interactively 'move-to-window-line))
0f202d5d 5425 ((eq recenter-last-op 'top)
0116abbd
JL
5426 (move-to-window-line this-scroll-margin))
5427 ((eq recenter-last-op 'bottom)
5428 (move-to-window-line (- -1 this-scroll-margin)))
5429 ((integerp recenter-last-op)
5430 (move-to-window-line recenter-last-op))
5431 ((floatp recenter-last-op)
5432 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
216349f8
SM
5433
5434(define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
3c448ab6 5435\f
74f806a1
JL
5436;;; Scrolling commands.
5437
5438;;; Scrolling commands which does not signal errors at top/bottom
5439;;; of buffer at first key-press (instead moves to top/bottom
5440;;; of buffer).
5441
5442(defcustom scroll-error-top-bottom nil
5443 "Move point to top/bottom of buffer before signalling a scrolling error.
5444A value of nil means just signal an error if no more scrolling possible.
5445A value of t means point moves to the beginning or the end of the buffer
5446\(depending on scrolling direction) when no more scrolling possible.
5447When point is already on that position, then signal an error."
5448 :type 'boolean
5449 :group 'scrolling
5450 :version "24.1")
5451
5452(defun scroll-up-command (&optional arg)
5453 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
5454If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
5455scroll window further, move cursor to the bottom line.
5456When point is already on that position, then signal an error.
5457A near full screen is `next-screen-context-lines' less than a full screen.
5458Negative ARG means scroll downward.
5459If ARG is the atom `-', scroll downward by nearly full screen."
5460 (interactive "^P")
5461 (cond
5462 ((null scroll-error-top-bottom)
5463 (scroll-up arg))
5464 ((eq arg '-)
5465 (scroll-down-command nil))
5466 ((< (prefix-numeric-value arg) 0)
5467 (scroll-down-command (- (prefix-numeric-value arg))))
5468 ((eobp)
5469 (scroll-up arg)) ; signal error
5470 (t
5471 (condition-case nil
5472 (scroll-up arg)
5473 (end-of-buffer
5474 (if arg
5475 ;; When scrolling by ARG lines can't be done,
5476 ;; move by ARG lines instead.
5477 (forward-line arg)
5478 ;; When ARG is nil for full-screen scrolling,
5479 ;; move to the bottom of the buffer.
5480 (goto-char (point-max))))))))
5481
5482(put 'scroll-up-command 'scroll-command t)
5483
5484(defun scroll-down-command (&optional arg)
5485 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
5486If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
5487scroll window further, move cursor to the top line.
5488When point is already on that position, then signal an error.
5489A near full screen is `next-screen-context-lines' less than a full screen.
5490Negative ARG means scroll upward.
5491If ARG is the atom `-', scroll upward by nearly full screen."
5492 (interactive "^P")
5493 (cond
5494 ((null scroll-error-top-bottom)
5495 (scroll-down arg))
5496 ((eq arg '-)
5497 (scroll-up-command nil))
5498 ((< (prefix-numeric-value arg) 0)
5499 (scroll-up-command (- (prefix-numeric-value arg))))
5500 ((bobp)
5501 (scroll-down arg)) ; signal error
5502 (t
5503 (condition-case nil
5504 (scroll-down arg)
5505 (beginning-of-buffer
5506 (if arg
5507 ;; When scrolling by ARG lines can't be done,
5508 ;; move by ARG lines instead.
5509 (forward-line (- arg))
5510 ;; When ARG is nil for full-screen scrolling,
5511 ;; move to the top of the buffer.
5512 (goto-char (point-min))))))))
5513
5514(put 'scroll-down-command 'scroll-command t)
5515
5516;;; Scrolling commands which scroll a line instead of full screen.
5517
5518(defun scroll-up-line (&optional arg)
5519 "Scroll text of selected window upward ARG lines; or one line if no ARG.
5520If ARG is omitted or nil, scroll upward by one line.
5521This is different from `scroll-up-command' that scrolls a full screen."
5522 (interactive "p")
5523 (scroll-up (or arg 1)))
5524
5525(put 'scroll-up-line 'scroll-command t)
5526
5527(defun scroll-down-line (&optional arg)
5528 "Scroll text of selected window down ARG lines; or one line if no ARG.
5529If ARG is omitted or nil, scroll down by one line.
5530This is different from `scroll-down-command' that scrolls a full screen."
5531 (interactive "p")
5532 (scroll-down (or arg 1)))
5533
5534(put 'scroll-down-line 'scroll-command t)
5535
5536\f
5537(defun scroll-other-window-down (lines)
5538 "Scroll the \"other window\" down.
5539For more details, see the documentation for `scroll-other-window'."
5540 (interactive "P")
5541 (scroll-other-window
5542 ;; Just invert the argument's meaning.
5543 ;; We can do that without knowing which window it will be.
5544 (if (eq lines '-) nil
5545 (if (null lines) '-
5546 (- (prefix-numeric-value lines))))))
5547
5548(defun beginning-of-buffer-other-window (arg)
5549 "Move point to the beginning of the buffer in the other window.
5550Leave mark at previous position.
5551With arg N, put point N/10 of the way from the true beginning."
5552 (interactive "P")
5553 (let ((orig-window (selected-window))
5554 (window (other-window-for-scrolling)))
5555 ;; We use unwind-protect rather than save-window-excursion
5556 ;; because the latter would preserve the things we want to change.
5557 (unwind-protect
5558 (progn
5559 (select-window window)
5560 ;; Set point and mark in that window's buffer.
5561 (with-no-warnings
5562 (beginning-of-buffer arg))
5563 ;; Set point accordingly.
5564 (recenter '(t)))
5565 (select-window orig-window))))
5566
5567(defun end-of-buffer-other-window (arg)
5568 "Move point to the end of the buffer in the other window.
5569Leave mark at previous position.
5570With arg N, put point N/10 of the way from the true end."
5571 (interactive "P")
5572 ;; See beginning-of-buffer-other-window for comments.
5573 (let ((orig-window (selected-window))
5574 (window (other-window-for-scrolling)))
5575 (unwind-protect
5576 (progn
5577 (select-window window)
5578 (with-no-warnings
5579 (end-of-buffer arg))
5580 (recenter '(t)))
5581 (select-window orig-window))))
74f806a1 5582\f
3c448ab6
MR
5583(defvar mouse-autoselect-window-timer nil
5584 "Timer used by delayed window autoselection.")
5585
5586(defvar mouse-autoselect-window-position nil
5587 "Last mouse position recorded by delayed window autoselection.")
5588
5589(defvar mouse-autoselect-window-window nil
5590 "Last window recorded by delayed window autoselection.")
5591
5592(defvar mouse-autoselect-window-state nil
5593 "When non-nil, special state of delayed window autoselection.
5594Possible values are `suspend' \(suspend autoselection after a menu or
5595scrollbar interaction\) and `select' \(the next invocation of
5596'handle-select-window' shall select the window immediately\).")
5597
5598(defun mouse-autoselect-window-cancel (&optional force)
5599 "Cancel delayed window autoselection.
5600Optional argument FORCE means cancel unconditionally."
5601 (unless (and (not force)
5602 ;; Don't cancel for select-window or select-frame events
5603 ;; or when the user drags a scroll bar.
5604 (or (memq this-command
5605 '(handle-select-window handle-switch-frame))
5606 (and (eq this-command 'scroll-bar-toolkit-scroll)
5607 (memq (nth 4 (event-end last-input-event))
5608 '(handle end-scroll)))))
5609 (setq mouse-autoselect-window-state nil)
5610 (when (timerp mouse-autoselect-window-timer)
5611 (cancel-timer mouse-autoselect-window-timer))
5612 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
5613
5614(defun mouse-autoselect-window-start (mouse-position &optional window suspend)
5615 "Start delayed window autoselection.
5616MOUSE-POSITION is the last position where the mouse was seen as returned
5617by `mouse-position'. Optional argument WINDOW non-nil denotes the
5618window where the mouse was seen. Optional argument SUSPEND non-nil
5619means suspend autoselection."
5620 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
5621 (setq mouse-autoselect-window-position mouse-position)
5622 (when window (setq mouse-autoselect-window-window window))
5623 (setq mouse-autoselect-window-state (when suspend 'suspend))
5624 ;; Install timer which runs `mouse-autoselect-window-select' after
5625 ;; `mouse-autoselect-window' seconds.
5626 (setq mouse-autoselect-window-timer
5627 (run-at-time
5628 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
5629
5630(defun mouse-autoselect-window-select ()
5631 "Select window with delayed window autoselection.
5632If the mouse position has stabilized in a non-selected window, select
5633that window. The minibuffer window is selected only if the minibuffer is
5634active. This function is run by `mouse-autoselect-window-timer'."
6198ccd0
MR
5635 (ignore-errors
5636 (let* ((mouse-position (mouse-position))
5637 (window
5638 (ignore-errors
5639 (window-at (cadr mouse-position) (cddr mouse-position)
5640 (car mouse-position)))))
5641 (cond
5642 ((or (menu-or-popup-active-p)
5643 (and window
5644 (not (coordinates-in-window-p (cdr mouse-position) window))))
5645 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
5646 ;; of WINDOW, temporarily suspend delayed autoselection.
5647 (mouse-autoselect-window-start mouse-position nil t))
5648 ((eq mouse-autoselect-window-state 'suspend)
5649 ;; Delayed autoselection was temporarily suspended, reenable it.
5650 (mouse-autoselect-window-start mouse-position))
5651 ((and window (not (eq window (selected-window)))
5652 (or (not (numberp mouse-autoselect-window))
5653 (and (> mouse-autoselect-window 0)
5654 ;; If `mouse-autoselect-window' is positive, select
5655 ;; window if the window is the same as before.
5656 (eq window mouse-autoselect-window-window))
5657 ;; Otherwise select window if the mouse is at the same
5658 ;; position as before. Observe that the first test after
5659 ;; starting autoselection usually fails since the value of
5660 ;; `mouse-autoselect-window-position' recorded there is the
5661 ;; position where the mouse has entered the new window and
5662 ;; not necessarily where the mouse has stopped moving.
5663 (equal mouse-position mouse-autoselect-window-position))
5664 ;; The minibuffer is a candidate window if it's active.
5665 (or (not (window-minibuffer-p window))
5666 (eq window (active-minibuffer-window))))
5667 ;; Mouse position has stabilized in non-selected window: Cancel
5668 ;; delayed autoselection and try to select that window.
5669 (mouse-autoselect-window-cancel t)
5670 ;; Select window where mouse appears unless the selected window is the
5671 ;; minibuffer. Use `unread-command-events' in order to execute pre-
5672 ;; and post-command hooks and trigger idle timers. To avoid delaying
5673 ;; autoselection again, set `mouse-autoselect-window-state'."
5674 (unless (window-minibuffer-p (selected-window))
5675 (setq mouse-autoselect-window-state 'select)
5676 (setq unread-command-events
5677 (cons (list 'select-window (list window))
5678 unread-command-events))))
5679 ((or (and window (eq window (selected-window)))
5680 (not (numberp mouse-autoselect-window))
5681 (equal mouse-position mouse-autoselect-window-position))
5682 ;; Mouse position has either stabilized in the selected window or at
5683 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
5684 (mouse-autoselect-window-cancel t))
5685 (t
5686 ;; Mouse position has not stabilized yet, resume delayed
5687 ;; autoselection.
5688 (mouse-autoselect-window-start mouse-position window))))))
3c448ab6
MR
5689
5690(defun handle-select-window (event)
5691 "Handle select-window events."
5692 (interactive "e")
5693 (let ((window (posn-window (event-start event))))
5694 (unless (or (not (window-live-p window))
5695 ;; Don't switch if we're currently in the minibuffer.
5696 ;; This tries to work around problems where the
5697 ;; minibuffer gets unselected unexpectedly, and where
5698 ;; you then have to move your mouse all the way down to
5699 ;; the minibuffer to select it.
5700 (window-minibuffer-p (selected-window))
5701 ;; Don't switch to minibuffer window unless it's active.
5702 (and (window-minibuffer-p window)
5703 (not (minibuffer-window-active-p window)))
5704 ;; Don't switch when autoselection shall be delayed.
5705 (and (numberp mouse-autoselect-window)
5706 (not (zerop mouse-autoselect-window))
5707 (not (eq mouse-autoselect-window-state 'select))
5708 (progn
5709 ;; Cancel any delayed autoselection.
5710 (mouse-autoselect-window-cancel t)
5711 ;; Start delayed autoselection from current mouse
5712 ;; position and window.
5713 (mouse-autoselect-window-start (mouse-position) window)
5714 ;; Executing a command cancels delayed autoselection.
5715 (add-hook
5716 'pre-command-hook 'mouse-autoselect-window-cancel))))
5717 (when mouse-autoselect-window
5718 ;; Reset state of delayed autoselection.
5719 (setq mouse-autoselect-window-state nil)
5720 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
5721 (run-hooks 'mouse-leave-buffer-hook))
5722 (select-window window))))
5723
3c448ab6
MR
5724(defun truncated-partial-width-window-p (&optional window)
5725 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
5726WINDOW defaults to the selected window.
5727Return nil if WINDOW is not a partial-width window
5728 (regardless of the value of `truncate-lines').
5729Otherwise, consult the value of `truncate-partial-width-windows'
5730 for the buffer shown in WINDOW."
5731 (unless window
5732 (setq window (selected-window)))
5733 (unless (window-full-width-p window)
5734 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
5735 (window-buffer window))))
5736 (if (integerp t-p-w-w)
5737 (< (window-width window) t-p-w-w)
5738 t-p-w-w))))
562dd5e9 5739\f
59ee0542
GM
5740;; Some of these are in tutorial--default-keys, so update that if you
5741;; change these.
562dd5e9
MR
5742(define-key ctl-x-map "0" 'delete-window)
5743(define-key ctl-x-map "1" 'delete-other-windows)
2d197ffb
CY
5744(define-key ctl-x-map "2" 'split-window-below)
5745(define-key ctl-x-map "3" 'split-window-right)
9397e56f 5746(define-key ctl-x-map "o" 'other-window)
562dd5e9 5747(define-key ctl-x-map "^" 'enlarge-window)
3c448ab6
MR
5748(define-key ctl-x-map "}" 'enlarge-window-horizontally)
5749(define-key ctl-x-map "{" 'shrink-window-horizontally)
5750(define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
5751(define-key ctl-x-map "+" 'balance-windows)
5752(define-key ctl-x-4-map "0" 'kill-buffer-and-window)
5753
3c448ab6 5754;;; window.el ends here