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