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