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