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