Merge from trunk.
[bpt/emacs.git] / lisp / window.el
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
2
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2011
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8 ;; Package: emacs
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
33 (defmacro save-selected-window (&rest body)
34 "Execute BODY, then select the previously selected window.
35 The value returned is the value of the last form in BODY.
36
37 This macro saves and restores the selected window, as well as the
38 selected window in each frame. If the previously selected window
39 is no longer live, then whatever window is selected at the end of
40 BODY remains selected. If the previously selected window of some
41 frame is no longer live at the end of BODY, that frame's selected
42 window is left alone.
43
44 This macro saves and restores the current buffer, since otherwise
45 its normal operation could make a different buffer current. The
46 order of recently selected windows and the buffer list ordering
47 are not altered by this macro (unless they are altered in BODY)."
48 (declare (indent 0) (debug t))
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
66 ;; The following two functions are like `window-next' and `window-prev'
67 ;; but the WINDOW argument is _not_ optional (so they don't substitute
68 ;; the selected window for nil), and they return nil when WINDOW doesn't
69 ;; have a parent (like a frame's root window or a minibuffer window).
70 (defsubst window-right (window)
71 "Return WINDOW's right sibling.
72 Return nil if WINDOW is the root window of its frame. WINDOW can
73 be any window."
74 (and window (window-parent window) (window-next window)))
75
76 (defsubst window-left (window)
77 "Return WINDOW's left sibling.
78 Return nil if WINDOW is the root window of its frame. WINDOW can
79 be any window."
80 (and window (window-parent window) (window-prev window)))
81
82 (defsubst window-child (window)
83 "Return WINDOW's first child window."
84 (or (window-vchild window) (window-hchild window)))
85
86 (defun window-child-count (window)
87 "Return number of WINDOW's child windows."
88 (let ((count 0))
89 (when (and (windowp window) (setq window (window-child window)))
90 (while window
91 (setq count (1+ count))
92 (setq window (window-next window))))
93 count))
94
95 (defun window-last-child (window)
96 "Return last child window of WINDOW."
97 (when (and (windowp window) (setq window (window-child window)))
98 (while (window-next window)
99 (setq window (window-next window))))
100 window)
101
102 (defsubst window-any-p (object)
103 "Return t if OBJECT denotes a live or internal window."
104 (and (windowp object)
105 (or (window-buffer object) (window-child object))
106 t))
107
108 ;; The following four functions should probably go to subr.el.
109 (defsubst normalize-live-buffer (buffer-or-name)
110 "Return buffer specified by BUFFER-OR-NAME.
111 BUFFER-OR-NAME must be either a buffer or a string naming a live
112 buffer and defaults to the current buffer."
113 (cond
114 ((not buffer-or-name)
115 (current-buffer))
116 ((bufferp buffer-or-name)
117 (if (buffer-live-p buffer-or-name)
118 buffer-or-name
119 (error "Buffer %s is not a live buffer" buffer-or-name)))
120 ((get-buffer buffer-or-name))
121 (t
122 (error "No such buffer %s" buffer-or-name))))
123
124 (defsubst normalize-live-frame (frame)
125 "Return frame specified by FRAME.
126 FRAME must be a live frame and defaults to the selected frame."
127 (if frame
128 (if (frame-live-p frame)
129 frame
130 (error "%s is not a live frame" frame))
131 (selected-frame)))
132
133 (defsubst normalize-any-window (window)
134 "Return window specified by WINDOW.
135 WINDOW must be a window that has not been deleted and defaults to
136 the selected window."
137 (if window
138 (if (window-any-p window)
139 window
140 (error "%s is not a window" window))
141 (selected-window)))
142
143 (defsubst normalize-live-window (window)
144 "Return live window specified by WINDOW.
145 WINDOW must be a live window and defaults to the selected one."
146 (if window
147 (if (and (windowp window) (window-buffer window))
148 window
149 (error "%s is not a live window" window))
150 (selected-window)))
151
152 (defvar ignore-window-parameters nil
153 "If non-nil, standard functions ignore window parameters.
154 The functions currently affected by this are `split-window',
155 `delete-window', `delete-other-windows' and `other-window'.
156
157 An application may bind this to a non-nil value around calls to
158 these functions to inhibit processing of window parameters.")
159
160 (defconst window-safe-min-height 1
161 "The absolut minimum number of lines of a window.
162 Anything less might crash Emacs.")
163
164 (defcustom window-min-height 4
165 "The minimum number of lines of any window.
166 The value has to accomodate a mode- or header-line if present. A
167 value less than `window-safe-min-height' is ignored. The value
168 of this variable is honored when windows are resized or split.
169
170 Applications should never rebind this variable. To resize a
171 window to a height less than the one specified here, an
172 application should instead call `resize-window' with a non-nil
173 IGNORE argument. In order to have `split-window' make a window
174 shorter, explictly specify the SIZE argument of that function."
175 :type 'integer
176 :version "24.1"
177 :group 'windows)
178
179 (defconst window-safe-min-width 2
180 "The absolut minimum number of columns of a window.
181 Anything less might crash Emacs.")
182
183 (defcustom window-min-width 10
184 "The minimum number of columns of any window.
185 The value has to accomodate margins, fringes, or scrollbars if
186 present. A value less than `window-safe-min-width' is ignored.
187 The value of this variable is honored when windows are resized or
188 split.
189
190 Applications should never rebind this variable. To resize a
191 window to a width less than the one specified here, an
192 application should instead call `resize-window' with a non-nil
193 IGNORE argument. In order to have `split-window' make a window
194 narrower, explictly specify the SIZE argument of that function."
195 :type 'integer
196 :version "24.1"
197 :group 'windows)
198
199 (defun window-iso-combination-p (&optional window horizontal)
200 "If WINDOW is a vertical combination return WINDOW's first child.
201 WINDOW can be any window and defaults to the selected one.
202 Optional argument HORIZONTAL non-nil means return WINDOW's first
203 child if WINDOW is a horizontal combination."
204 (setq window (normalize-any-window window))
205 (if horizontal
206 (window-hchild window)
207 (window-vchild window)))
208
209 (defsubst window-iso-combined-p (&optional window horizontal)
210 "Return non-nil if and only if WINDOW is vertically combined.
211 WINDOW can be any window and defaults to the selected one.
212 Optional argument HORIZONTAL non-nil means return non-nil if and
213 only if WINDOW is horizontally combined."
214 (setq window (normalize-any-window window))
215 (let ((parent (window-parent window)))
216 (and parent (window-iso-combination-p parent horizontal))))
217
218 (defun window-iso-combinations (&optional window horizontal)
219 "Return largest number of vertically arranged subwindows of WINDOW.
220 WINDOW can be any window and defaults to the selected one.
221 Optional argument HORIZONTAL non-nil means to return the largest
222 number of horizontally arranged subwindows of WINDOW."
223 (setq window (normalize-any-window window))
224 (cond
225 ((window-live-p window)
226 ;; If WINDOW is live, return 1.
227 1)
228 ((window-iso-combination-p window horizontal)
229 ;; If WINDOW is iso-combined, return the sum of the values for all
230 ;; subwindows of WINDOW.
231 (let ((child (window-child window))
232 (count 0))
233 (while child
234 (setq count
235 (+ (window-iso-combinations child horizontal)
236 count))
237 (setq child (window-right child)))
238 count))
239 (t
240 ;; If WINDOW is not iso-combined, return the maximum value of any
241 ;; subwindow of WINDOW.
242 (let ((child (window-child window))
243 (count 1))
244 (while child
245 (setq count
246 (max (window-iso-combinations child horizontal)
247 count))
248 (setq child (window-right child)))
249 count))))
250
251 (defun walk-window-tree-1 (proc walk-window-tree-window any &optional sub-only)
252 "Helper function for `walk-window-tree' and `walk-window-subtree'."
253 (let (walk-window-tree-buffer)
254 (while walk-window-tree-window
255 (setq walk-window-tree-buffer
256 (window-buffer walk-window-tree-window))
257 (when (or walk-window-tree-buffer any)
258 (funcall proc walk-window-tree-window))
259 (unless walk-window-tree-buffer
260 (walk-window-tree-1
261 proc (window-hchild walk-window-tree-window) any)
262 (walk-window-tree-1
263 proc (window-vchild walk-window-tree-window) any))
264 (if sub-only
265 (setq walk-window-tree-window nil)
266 (setq walk-window-tree-window
267 (window-right walk-window-tree-window))))))
268
269 (defun walk-window-tree (proc &optional frame any)
270 "Run function PROC on each live window of FRAME.
271 PROC must be a function with one argument - a window. FRAME must
272 be a live frame and defaults to the selected one. ANY, if
273 non-nil means to run PROC on all live and internal windows of
274 FRAME.
275
276 This function performs a pre-order, depth-first traversal of the
277 window tree. If PROC changes the window tree, the result is
278 unpredictable."
279 (let ((walk-window-tree-frame (normalize-live-frame frame)))
280 (walk-window-tree-1
281 proc (frame-root-window walk-window-tree-frame) any)))
282
283 (defun walk-window-subtree (proc &optional window any)
284 "Run function PROC on each live subwindow of WINDOW.
285 WINDOW defaults to the selected window. PROC must be a function
286 with one argument - a window. ANY, if non-nil means to run PROC
287 on all live and internal subwindows of WINDOW.
288
289 This function performs a pre-order, depth-first traversal of the
290 window tree rooted at WINDOW. If PROC changes that window tree,
291 the result is unpredictable."
292 (setq window (normalize-any-window window))
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.
297 FRAME defaults to the selected frame. Optional argument VALUE
298 non-nil means only return windows whose window-parameter value of
299 PARAMETER equals VALUE \(comparison is done using `equal').
300 Optional argument ANY non-nil means consider internal windows
301 too. Optional argument VALUES non-nil means return a list of cons
302 cells whose car is the value of the parameter and whose cdr is
303 the 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.
319 FRAME defaults to the selected frame. Optional argument VALUE
320 non-nil means only return a window whose window-parameter value
321 for PARAMETER equals VALUE \(comparison is done with `equal').
322 Optional argument ANY non-nil means consider internal windows
323 too."
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.
336 WINDOW can be any window and defaults to the selected one.
337 Return nil if WINDOW is not part of a atomic window."
338 (setq window (normalize-any-window window))
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
345 (defun make-window-atom (window)
346 "Make WINDOW an atomic window.
347 WINDOW 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)
378 (window-atom-check-1 (window-hchild window))
379 (window-atom-check-1 (window-vchild window))))
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.
385 FRAME defaults to the selected frame. If an atomic window is
386 wrongly configured, reset the atomicity of all its subwindows to
387 nil. An atomic window is wrongly configured if it has no
388 subwindows or one of its subwindows is not atomic."
389 (window-atom-check-1 (frame-root-window frame)))
390
391 ;; Side windows.
392 (defvar window-sides '(left top right bottom)
393 "Window sides.")
394
395 (defcustom window-sides-vertical nil
396 "If non-nil, left and right side windows are full height.
397 Otherwise, 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.
404 The value is a list of four elements specifying the number of
405 side window slots on \(in this order) the left, top, right and
406 bottom side of each frame. If an element is a number, this means
407 to display at most that many side windows on the corresponding
408 side. If an element is nil, this means there's no bound on the
409 number 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.
446 FRAME defaults to the selected frame. If the configuration is
447 invalid, reset all window-side parameters to nil.
448
449 A 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.
517 FRAME 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.
524 If the value is `height', then only the window's height is fixed.
525 If the value is `width', then only the window's width is fixed.
526 Any other non-nil value fixes both the width and the height.
527
528 Emacs won't change the size of any window displaying that buffer,
529 unless it has no other choice \(like when deleting a neighboring
530 window).")
531 (make-variable-buffer-local 'window-size-fixed)
532
533 (defsubst window-size-ignore (window ignore)
534 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
535 (if (window-any-p ignore) (eq window ignore) ignore))
536
537 (defun window-min-size (&optional window horizontal ignore)
538 "Return the minimum number of lines of WINDOW.
539 WINDOW can be an arbitrary window and defaults to the selected
540 one. Optional argument HORIZONTAL non-nil means return the
541 minimum number of columns of WINDOW.
542
543 Optional argument IGNORE non-nil means ignore any restrictions
544 imposed by fixed size windows, `window-min-height' or
545 `window-min-width' settings. IGNORE equal `safe' means live
546 windows may get as small as `window-safe-min-height' lines and
547 `window-safe-min-width' columns. IGNORE a window means ignore
548 restrictions for that window only."
549 (window-min-size-1
550 (normalize-any-window window) horizontal ignore))
551
552 (defun window-min-size-1 (window horizontal ignore)
553 "Internal function of `window-min-size'."
554 (let ((sub (window-child window)))
555 (if sub
556 (let ((value 0))
557 ;; WINDOW is an internal window.
558 (if (window-iso-combined-p sub horizontal)
559 ;; The minimum size of an iso-combination is the sum of
560 ;; the minimum sizes of its subwindows.
561 (while sub
562 (setq value (+ value
563 (window-min-size-1 sub horizontal ignore)))
564 (setq sub (window-right sub)))
565 ;; The minimum size of an ortho-combination is the maximum of
566 ;; the minimum sizes of its subwindows.
567 (while sub
568 (setq value (max value
569 (window-min-size-1 sub horizontal ignore)))
570 (setq sub (window-right sub))))
571 value)
572 (with-current-buffer (window-buffer window)
573 (cond
574 ((and (not (window-size-ignore window ignore))
575 (window-size-fixed-p window horizontal))
576 ;; The minimum size of a fixed size window is its size.
577 (window-total-size window horizontal))
578 ((or (eq ignore 'safe) (eq ignore window))
579 ;; If IGNORE equals `safe' or WINDOW return the safe values.
580 (if horizontal window-safe-min-width window-safe-min-height))
581 (horizontal
582 ;; For the minimum width of a window take fringes and
583 ;; scroll-bars into account. This is questionable and should
584 ;; be removed as soon as we are able to split (and resize)
585 ;; windows such that the new (or resized) windows can get a
586 ;; size less than the user-specified `window-min-height' and
587 ;; `window-min-width'.
588 (let ((frame (window-frame window))
589 (fringes (window-fringes window))
590 (scroll-bars (window-scroll-bars window)))
591 (max
592 (+ window-safe-min-width
593 (ceiling (car fringes) (frame-char-width frame))
594 (ceiling (cadr fringes) (frame-char-width frame))
595 (cond
596 ((memq (nth 2 scroll-bars) '(left right))
597 (nth 1 scroll-bars))
598 ((memq (frame-parameter frame 'vertical-scroll-bars)
599 '(left right))
600 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
601 (frame-char-width)))
602 (t 0)))
603 (if (and (not (window-size-ignore window ignore))
604 (numberp window-min-width))
605 window-min-width
606 0))))
607 (t
608 ;; For the minimum height of a window take any mode- or
609 ;; header-line into account.
610 (max (+ window-safe-min-height
611 (if header-line-format 1 0)
612 (if mode-line-format 1 0))
613 (if (and (not (window-size-ignore window ignore))
614 (numberp window-min-height))
615 window-min-height
616 0))))))))
617
618 (defun window-sizable (window delta &optional horizontal ignore)
619 "Return DELTA if DELTA lines can be added to WINDOW.
620 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
621 columns can be added to WINDOW. A return value of zero means
622 that no lines (or columns) can be added to WINDOW.
623
624 This function looks only at WINDOW and its subwindows. The
625 function `window-resizable' looks at other windows as well.
626
627 DELTA positive means WINDOW shall be enlarged by DELTA lines or
628 columns. If WINDOW cannot be enlarged by DELTA lines or columns
629 return the maximum value in the range 0..DELTA by which WINDOW
630 can be enlarged.
631
632 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
633 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
634 return the minimum value in the range DELTA..0 by which WINDOW
635 can be shrunk.
636
637 Optional argument IGNORE non-nil means ignore any restrictions
638 imposed by fixed size windows, `window-min-height' or
639 `window-min-width' settings. IGNORE equal `safe' means live
640 windows may get as small as `window-safe-min-height' lines and
641 `window-safe-min-width' columns. IGNORE any window means ignore
642 restrictions for that window only."
643 (setq window (normalize-any-window window))
644 (cond
645 ((< delta 0)
646 (max (- (window-min-size window horizontal ignore)
647 (window-total-size window horizontal))
648 delta))
649 ((window-size-ignore window ignore)
650 delta)
651 ((> delta 0)
652 (if (window-size-fixed-p window horizontal)
653 0
654 delta))
655 (t 0)))
656
657 (defsubst window-sizable-p (window delta &optional horizontal ignore)
658 "Return t if WINDOW can be resized by DELTA lines.
659 For the meaning of the arguments of this function see the
660 doc-string of `window-sizable'."
661 (setq window (normalize-any-window window))
662 (if (> delta 0)
663 (>= (window-sizable window delta horizontal ignore) delta)
664 (<= (window-sizable window delta horizontal ignore) delta)))
665
666 (defun window-size-fixed-1 (window horizontal)
667 "Internal function for `window-size-fixed-p'."
668 (let ((sub (window-child window)))
669 (catch 'fixed
670 (if sub
671 ;; WINDOW is an internal window.
672 (if (window-iso-combined-p sub horizontal)
673 ;; An iso-combination is fixed size if all its subwindows
674 ;; are fixed-size.
675 (progn
676 (while sub
677 (unless (window-size-fixed-1 sub horizontal)
678 ;; We found a non-fixed-size subwindow, so WINDOW's
679 ;; size is not fixed.
680 (throw 'fixed nil))
681 (setq sub (window-right sub)))
682 ;; All subwindows are fixed-size, so WINDOW's size is
683 ;; fixed.
684 (throw 'fixed t))
685 ;; An ortho-combination is fixed-size if at least one of its
686 ;; subwindows is fixed-size.
687 (while sub
688 (when (window-size-fixed-1 sub horizontal)
689 ;; We found a fixed-size subwindow, so WINDOW's size is
690 ;; fixed.
691 (throw 'fixed t))
692 (setq sub (window-right sub))))
693 ;; WINDOW is a live window.
694 (with-current-buffer (window-buffer window)
695 (if horizontal
696 (memq window-size-fixed '(width t))
697 (memq window-size-fixed '(height t))))))))
698
699 (defun window-size-fixed-p (&optional window horizontal)
700 "Return non-nil if WINDOW's height is fixed.
701 WINDOW can be an arbitrary window and defaults to the selected
702 window. Optional argument HORIZONTAL non-nil means return
703 non-nil if WINDOW's width is fixed.
704
705 If this function returns nil, this does not necessarily mean that
706 WINDOW can be resized in the desired direction. The functions
707 `window-resizable' and `window-resizable-p' will tell that."
708 (window-size-fixed-1
709 (normalize-any-window window) horizontal))
710
711 (defun window-min-delta-1 (window delta &optional horizontal ignore trail noup)
712 "Internal function for `window-min-delta'."
713 (if (not (window-parent window))
714 ;; If we can't go up, return zero.
715 0
716 ;; Else try to find a non-fixed-size sibling of WINDOW.
717 (let* ((parent (window-parent window))
718 (sub (window-child parent)))
719 (catch 'done
720 (if (window-iso-combined-p sub horizontal)
721 ;; In an iso-combination throw DELTA if we find at least one
722 ;; subwindow and that subwindow is either not of fixed-size
723 ;; or we can ignore fixed-sizeness.
724 (let ((skip (eq trail 'after)))
725 (while sub
726 (cond
727 ((eq sub window)
728 (setq skip (eq trail 'before)))
729 (skip)
730 ((and (not (window-size-ignore window ignore))
731 (window-size-fixed-p sub horizontal)))
732 (t
733 ;; We found a non-fixed-size subwindow.
734 (throw 'done delta)))
735 (setq sub (window-right sub))))
736 ;; In an ortho-combination set DELTA to the minimum value by
737 ;; which other subwindows can shrink.
738 (while sub
739 (unless (eq sub window)
740 (setq delta
741 (min delta
742 (- (window-total-size sub horizontal)
743 (window-min-size sub horizontal ignore)))))
744 (setq sub (window-right sub))))
745 (if noup
746 delta
747 (window-min-delta-1 parent delta horizontal ignore trail))))))
748
749 (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
750 "Return number of lines by which WINDOW can be shrunk.
751 WINDOW can be an arbitrary window and defaults to the selected
752 window. Return zero if WINDOW cannot be shrunk.
753
754 Optional argument HORIZONTAL non-nil means return number of
755 columns by which WINDOW can be shrunk.
756
757 Optional argument IGNORE non-nil means ignore any restrictions
758 imposed by fixed size windows, `window-min-height' or
759 `window-min-width' settings. IGNORE a window means ignore
760 restrictions for that window only. IGNORE equal `safe' means
761 live windows may get as small as `window-safe-min-height' lines
762 and `window-safe-min-width' columns.
763
764 Optional argument TRAIL `before' means only windows to the left
765 of or above WINDOW can be enlarged. Optional argument TRAIL
766 `after' means only windows to the right of or below WINDOW can be
767 enlarged.
768
769 Optional argument NOUP non-nil means don't go up in the window
770 tree but try to enlarge windows within WINDOW's combination only.
771
772 Optional argument NODOWN non-nil means don't check whether WINDOW
773 itself \(and its subwindows) can be shrunk; check only whether at
774 least one other windows can be enlarged appropriately."
775 (setq window (normalize-any-window window))
776 (let ((size (window-total-size window horizontal))
777 (minimum (window-min-size window horizontal ignore)))
778 (cond
779 (nodown
780 ;; If NODOWN is t, try to recover the entire size of WINDOW.
781 (window-min-delta-1 window size horizontal ignore trail noup))
782 ((= size minimum)
783 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
784 ;; there's nothing to recover.
785 0)
786 (t
787 ;; Otherwise, try to recover whatever WINDOW is larger than its
788 ;; minimum size.
789 (window-min-delta-1
790 window (- size minimum) horizontal ignore trail noup)))))
791
792 (defun window-max-delta-1 (window delta &optional horizontal ignore trail noup)
793 "Internal function of `window-max-delta'."
794 (if (not (window-parent window))
795 ;; Can't go up. Return DELTA.
796 delta
797 (let* ((parent (window-parent window))
798 (sub (window-child parent)))
799 (catch 'fixed
800 (if (window-iso-combined-p sub horizontal)
801 ;; For an iso-combination calculate how much we can get from
802 ;; other subwindows.
803 (let ((skip (eq trail 'after)))
804 (while sub
805 (cond
806 ((eq sub window)
807 (setq skip (eq trail 'before)))
808 (skip)
809 (t
810 (setq delta
811 (+ delta
812 (- (window-total-size sub horizontal)
813 (window-min-size sub horizontal ignore))))))
814 (setq sub (window-right sub))))
815 ;; For an ortho-combination throw DELTA when at least one
816 ;; subwindow is fixed-size.
817 (while sub
818 (when (and (not (eq sub window))
819 (not (window-size-ignore sub ignore))
820 (window-size-fixed-p sub horizontal))
821 (throw 'fixed delta))
822 (setq sub (window-right sub))))
823 (if noup
824 ;; When NOUP is nil, DELTA is all we can get.
825 delta
826 ;; Else try with parent of WINDOW, passing the DELTA we
827 ;; recovered so far.
828 (window-max-delta-1 parent delta horizontal ignore trail))))))
829
830 (defun window-max-delta (&optional window horizontal ignore trail noup nodown)
831 "Return maximum number of lines WINDOW by which WINDOW can be enlarged.
832 WINDOW can be an arbitrary window and defaults to the selected
833 window. The return value is zero if WINDOW cannot be enlarged.
834
835 Optional argument HORIZONTAL non-nil means return maximum number
836 of columns by which WINDOW can be enlarged.
837
838 Optional argument IGNORE non-nil means ignore any restrictions
839 imposed by fixed size windows, `window-min-height' or
840 `window-min-width' settings. IGNORE a window means ignore
841 restrictions for that window only. IGNORE equal `safe' means
842 live windows may get as small as `window-safe-min-height' lines
843 and `window-safe-min-width' columns.
844
845 Optional argument TRAIL `before' means only windows to the left
846 of or below WINDOW can be shrunk. Optional argument TRAIL
847 `after' means only windows to the right of or above WINDOW can be
848 shrunk.
849
850 Optional argument NOUP non-nil means don't go up in the window
851 tree but try to obtain the entire space from windows within
852 WINDOW's combination.
853
854 Optional argument NODOWN non-nil means do not check whether
855 WINDOW itself \(and its subwindows) can be enlarged; check only
856 whether other windows can be shrunk appropriately."
857 (setq window (normalize-any-window window))
858 (if (and (not (window-size-ignore window ignore))
859 (not nodown) (window-size-fixed-p window horizontal))
860 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
861 ;; size.
862 0
863 ;; WINDOW has no fixed size.
864 (window-max-delta-1 window 0 horizontal ignore trail noup)))
865
866 ;; Make NOUP also inhibit the min-size check.
867 (defun window-resizable (window delta &optional horizontal ignore trail noup nodown)
868 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
869 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
870 can be resized horizontally by DELTA columns. A return value of
871 zero means that WINDOW is not resizable.
872
873 DELTA positive means WINDOW shall be enlarged by DELTA lines or
874 columns. If WINDOW cannot be enlarged by DELTA lines or columns
875 return the maximum value in the range 0..DELTA by which WINDOW
876 can be enlarged.
877
878 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
879 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
880 return the minimum value in the range DELTA..0 that can be used
881 for shrinking WINDOW.
882
883 Optional argument IGNORE non-nil means ignore any restrictions
884 imposed by fixed size windows, `window-min-height' or
885 `window-min-width' settings. IGNORE a window means ignore
886 restrictions for that window only. IGNORE equal `safe' means
887 live windows may get as small as `window-safe-min-height' lines
888 and `window-safe-min-width' columns.
889
890 Optional argument TRAIL `before' means only windows to the left
891 of or below WINDOW can be shrunk. Optional argument TRAIL
892 `after' means only windows to the right of or above WINDOW can be
893 shrunk.
894
895 Optional argument NOUP non-nil means don't go up in the window
896 tree but try to distribute the space among the other windows
897 within WINDOW's combination.
898
899 Optional argument NODOWN non-nil means don't check whether WINDOW
900 and its subwindows can be resized."
901 (setq window (normalize-any-window window))
902 (cond
903 ((< delta 0)
904 (max (- (window-min-delta window horizontal ignore trail noup nodown))
905 delta))
906 ((> delta 0)
907 (min (window-max-delta window horizontal ignore trail noup nodown)
908 delta))
909 (t 0)))
910
911 (defun window-resizable-p (window delta &optional horizontal ignore trail noup nodown)
912 "Return t if WINDOW can be resized vertically by DELTA lines.
913 For the meaning of the arguments of this function see the
914 doc-string of `window-resizable'."
915 (setq window (normalize-any-window window))
916 (if (> delta 0)
917 (>= (window-resizable window delta horizontal ignore trail noup nodown)
918 delta)
919 (<= (window-resizable window delta horizontal ignore trail noup nodown)
920 delta)))
921
922 (defsubst window-total-height (&optional window)
923 "Return the total number of lines of WINDOW.
924 WINDOW can be any window and defaults to the selected one. The
925 return value includes WINDOW's mode line and header line, if any.
926 If WINDOW is internal the return value is the sum of the total
927 number of lines of WINDOW's child windows if these are vertically
928 combined and the height of WINDOW's first child otherwise.
929
930 Note: This function does not take into account the value of
931 `line-spacing' when calculating the number of lines in WINDOW."
932 (window-total-size window))
933
934 ;; Eventually we should make `window-height' obsolete.
935 (defalias 'window-height 'window-total-height)
936
937 ;; See discussion in bug#4543.
938 (defsubst window-full-height-p (&optional window)
939 "Return t if WINDOW is as high as the containing frame.
940 More precisely, return t if and only if the total height of
941 WINDOW equals the total height of the root window of WINDOW's
942 frame. WINDOW can be any window and defaults to the selected
943 one."
944 (setq window (normalize-any-window window))
945 (= (window-total-size window)
946 (window-total-size (frame-root-window window))))
947
948 (defsubst window-total-width (&optional window)
949 "Return the total number of columns of WINDOW.
950 WINDOW can be any window and defaults to the selected one. The
951 return value includes any vertical dividers or scrollbars of
952 WINDOW. If WINDOW is internal, the return value is the sum of
953 the total number of columns of WINDOW's child windows if these
954 are horizontally combined and the width of WINDOW's first child
955 otherwise."
956 (window-total-size window t))
957
958 (defsubst window-full-width-p (&optional window)
959 "Return t if WINDOW is as wide as the containing frame.
960 More precisely, return t if and only if the total width of WINDOW
961 equals the total width of the root window of WINDOW's frame.
962 WINDOW can be any window and defaults to the selected one."
963 (setq window (normalize-any-window window))
964 (= (window-total-size window t)
965 (window-total-size (frame-root-window window) t)))
966
967 (defsubst window-body-height (&optional window)
968 "Return the number of lines of WINDOW's body.
969 WINDOW must be a live window and defaults to the selected one.
970
971 The return value does not include WINDOW's mode line and header
972 line, if any. If a line at the bottom of the window is only
973 partially visible, that line is included in the return value. If
974 you do not want to include a partially visible bottom line in the
975 return value, use `window-text-height' instead."
976 (window-body-size window))
977
978 (defsubst window-body-width (&optional window)
979 "Return the number of columns of WINDOW's body.
980 WINDOW must be a live window and defaults to the selected one.
981
982 The return value does not include any vertical dividers or scroll
983 bars owned by WINDOW. On a window-system the return value does
984 not include the number of columns used for WINDOW's fringes or
985 display margins either."
986 (window-body-size window t))
987
988 ;; Eventually we should make `window-height' obsolete.
989 (defalias 'window-width 'window-body-width)
990
991 (defun window-current-scroll-bars (&optional window)
992 "Return the current scroll bar settings for WINDOW.
993 WINDOW must be a live window and defaults to the selected one.
994
995 The return value is a cons cell (VERTICAL . HORIZONTAL) where
996 VERTICAL specifies the current location of the vertical scroll
997 bars (`left', `right', or nil), and HORIZONTAL specifies the
998 current location of the horizontal scroll bars (`top', `bottom',
999 or nil).
1000
1001 Unlike `window-scroll-bars', this function reports the scroll bar
1002 type actually used, once frame defaults and `scroll-bar-mode' are
1003 taken into account."
1004 (setq window (normalize-live-window window))
1005 (let ((vert (nth 2 (window-scroll-bars window)))
1006 (hor nil))
1007 (when (or (eq vert t) (eq hor t))
1008 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
1009 (if (eq vert t)
1010 (setq vert (car fcsb)))
1011 (if (eq hor t)
1012 (setq hor (cdr fcsb)))))
1013 (cons vert hor)))
1014
1015 (defun walk-windows (proc &optional minibuf all-frames)
1016 "Cycle through all live windows, calling PROC for each one.
1017 PROC must specify a function with a window as its sole argument.
1018 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1019 windows to include in the walk.
1020
1021 MINIBUF t means include the minibuffer window even if the
1022 minibuffer is not active. MINIBUF nil or omitted means include
1023 the minibuffer window only if the minibuffer is active. Any
1024 other value means do not include the minibuffer window even if
1025 the minibuffer is active.
1026
1027 ALL-FRAMES nil or omitted means consider all windows on the
1028 selected frame, plus the minibuffer window if specified by the
1029 MINIBUF argument. If the minibuffer counts, consider all windows
1030 on all frames that share that minibuffer too. The following
1031 non-nil values of ALL-FRAMES have special meanings:
1032
1033 - t means consider all windows on all existing frames.
1034
1035 - `visible' means consider all windows on all visible frames on
1036 the current terminal.
1037
1038 - 0 (the number zero) means consider all windows on all visible
1039 and iconified frames on the current terminal.
1040
1041 - A frame means consider all windows on that frame only.
1042
1043 Anything else means consider all windows on the selected frame
1044 and no others.
1045
1046 This function changes neither the order of recently selected
1047 windows nor the buffer list."
1048 ;; If we start from the minibuffer window, don't fail to come
1049 ;; back to it.
1050 (when (window-minibuffer-p (selected-window))
1051 (setq minibuf t))
1052 ;; Make sure to not mess up the order of recently selected
1053 ;; windows. Use `save-selected-window' and `select-window'
1054 ;; with second argument non-nil for this purpose.
1055 (save-selected-window
1056 (when (framep all-frames)
1057 (select-window (frame-first-window all-frames) 'norecord))
1058 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1059 (funcall proc walk-windows-window))))
1060
1061 (defun window-in-direction-2 (window posn &optional horizontal)
1062 "Support function for `window-in-direction'."
1063 (if horizontal
1064 (let ((top (window-top-line window)))
1065 (if (> top posn)
1066 (- top posn)
1067 (- posn top (window-total-height window))))
1068 (let ((left (window-left-column window)))
1069 (if (> left posn)
1070 (- left posn)
1071 (- posn left (window-total-width window))))))
1072
1073 (defun window-in-direction (direction &optional window ignore)
1074 "Return window in DIRECTION as seen from WINDOW.
1075 DIRECTION must be one of `above', `below', `left' or `right'.
1076 WINDOW must be a live window and defaults to the selected one.
1077 IGNORE, when non-nil means a window can be returned even if its
1078 `no-other-window' parameter is non-nil."
1079 (setq window (normalize-live-window window))
1080 (unless (memq direction '(above below left right))
1081 (error "Wrong direction %s" direction))
1082 (let* ((frame (window-frame window))
1083 (hor (memq direction '(left right)))
1084 (first (if hor
1085 (window-left-column window)
1086 (window-top-line window)))
1087 (last (+ first (if hor
1088 (window-total-width window)
1089 (window-total-height window))))
1090 (posn-cons (nth 6 (posn-at-point (window-point window) window)))
1091 ;; The column / row value of `posn-at-point' can be nil for the
1092 ;; mini-window, guard against that.
1093 (posn (if hor
1094 (+ (or (cdr posn-cons) 1) (window-top-line window))
1095 (+ (or (car posn-cons) 1) (window-left-column window))))
1096 (best-edge
1097 (cond
1098 ((eq direction 'below) (frame-height frame))
1099 ((eq direction 'right) (frame-width frame))
1100 (t -1)))
1101 (best-edge-2 best-edge)
1102 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1103 best best-2 best-diff-2-new)
1104 (walk-window-tree
1105 (lambda (w)
1106 (let* ((w-top (window-top-line w))
1107 (w-left (window-left-column w)))
1108 (cond
1109 ((or (eq window w)
1110 ;; Ignore ourselves.
1111 (and (window-parameter w 'no-other-window)
1112 ;; Ignore W unless IGNORE is non-nil.
1113 (not ignore))))
1114 (hor
1115 (cond
1116 ((and (<= w-top posn)
1117 (< posn (+ w-top (window-total-height w))))
1118 ;; W is to the left or right of WINDOW and covers POSN.
1119 (when (or (and (eq direction 'left)
1120 (<= w-left first) (> w-left best-edge))
1121 (and (eq direction 'right)
1122 (>= w-left last) (< w-left best-edge)))
1123 (setq best-edge w-left)
1124 (setq best w)))
1125 ((and (or (and (eq direction 'left)
1126 (<= (+ w-left (window-total-width w)) first))
1127 (and (eq direction 'right) (<= last w-left)))
1128 ;; W is to the left or right of WINDOW but does not
1129 ;; cover POSN.
1130 (setq best-diff-2-new
1131 (window-in-direction-2 w posn hor))
1132 (or (< best-diff-2-new best-diff-2)
1133 (and (= best-diff-2-new best-diff-2)
1134 (if (eq direction 'left)
1135 (> w-left best-edge-2)
1136 (< w-left best-edge-2)))))
1137 (setq best-edge-2 w-left)
1138 (setq best-diff-2 best-diff-2-new)
1139 (setq best-2 w))))
1140 (t
1141 (cond
1142 ((and (<= w-left posn)
1143 (< posn (+ w-left (window-total-width w))))
1144 ;; W is above or below WINDOW and covers POSN.
1145 (when (or (and (eq direction 'above)
1146 (<= w-top first) (> w-top best-edge))
1147 (and (eq direction 'below)
1148 (>= w-top first) (< w-top best-edge)))
1149 (setq best-edge w-top)
1150 (setq best w)))
1151 ((and (or (and (eq direction 'above)
1152 (<= (+ w-top (window-total-height w)) first))
1153 (and (eq direction 'below) (<= last w-top)))
1154 ;; W is above or below WINDOW but does not cover POSN.
1155 (setq best-diff-2-new
1156 (window-in-direction-2 w posn hor))
1157 (or (< best-diff-2-new best-diff-2)
1158 (and (= best-diff-2-new best-diff-2)
1159 (if (eq direction 'above)
1160 (> w-top best-edge-2)
1161 (< w-top best-edge-2)))))
1162 (setq best-edge-2 w-top)
1163 (setq best-diff-2 best-diff-2-new)
1164 (setq best-2 w)))))))
1165 (window-frame window))
1166 (or best best-2)))
1167
1168 (defun get-window-with-predicate (predicate &optional minibuf
1169 all-frames default)
1170 "Return a live window satisfying PREDICATE.
1171 More precisely, cycle through all windows calling the function
1172 PREDICATE on each one of them with the window as its sole
1173 argument. Return the first window for which PREDICATE returns
1174 non-nil. If no window satisfies PREDICATE, return DEFAULT.
1175
1176 ALL-FRAMES nil or omitted means consider all windows on the selected
1177 frame, plus the minibuffer window if specified by the MINIBUF
1178 argument. If the minibuffer counts, consider all windows on all
1179 frames that share that minibuffer too. The following non-nil
1180 values of ALL-FRAMES have special meanings:
1181
1182 - t means consider all windows on all existing frames.
1183
1184 - `visible' means consider all windows on all visible frames on
1185 the current terminal.
1186
1187 - 0 (the number zero) means consider all windows on all visible
1188 and iconified frames on the current terminal.
1189
1190 - A frame means consider all windows on that frame only.
1191
1192 Anything else means consider all windows on the selected frame
1193 and no others."
1194 (catch 'found
1195 (dolist (window (window-list-1 nil minibuf all-frames))
1196 (when (funcall predicate window)
1197 (throw 'found window)))
1198 default))
1199
1200 (defalias 'some-window 'get-window-with-predicate)
1201
1202 (defun get-lru-window (&optional all-frames dedicated)
1203 "Return the least recently used window on frames specified by ALL-FRAMES.
1204 Return a full-width window if possible. A minibuffer window is
1205 never a candidate. A dedicated window is never a candidate
1206 unless DEDICATED is non-nil, so if all windows are dedicated, the
1207 value is nil. Avoid returning the selected window if possible.
1208
1209 The following non-nil values of the optional argument ALL-FRAMES
1210 have special meanings:
1211
1212 - t means consider all windows on all existing frames.
1213
1214 - `visible' means consider all windows on all visible frames on
1215 the current terminal.
1216
1217 - 0 (the number zero) means consider all windows on all visible
1218 and iconified frames on the current terminal.
1219
1220 - A frame means consider all windows on that frame only.
1221
1222 Any other value of ALL-FRAMES means consider all windows on the
1223 selected frame and no others."
1224 (let (best-window best-time second-best-window second-best-time time)
1225 (dolist (window (window-list-1 nil nil all-frames))
1226 (when (or dedicated (not (window-dedicated-p window)))
1227 (setq time (window-use-time window))
1228 (if (or (eq window (selected-window))
1229 (not (window-full-width-p window)))
1230 (when (or (not second-best-time) (< time second-best-time))
1231 (setq second-best-time time)
1232 (setq second-best-window window))
1233 (when (or (not best-time) (< time best-time))
1234 (setq best-time time)
1235 (setq best-window window)))))
1236 (or best-window second-best-window)))
1237
1238 (defun get-mru-window (&optional all-frames)
1239 "Return the most recently used window on frames specified by ALL-FRAMES.
1240 Do not return a minibuffer window.
1241
1242 The following non-nil values of the optional argument ALL-FRAMES
1243 have special meanings:
1244
1245 - t means consider all windows on all existing frames.
1246
1247 - `visible' means consider all windows on all visible frames on
1248 the current terminal.
1249
1250 - 0 (the number zero) means consider all windows on all visible
1251 and iconified frames on the current terminal.
1252
1253 - A frame means consider all windows on that frame only.
1254
1255 Any other value of ALL-FRAMES means consider all windows on the
1256 selected frame and no others."
1257 (let (best-window best-time time)
1258 (dolist (window (window-list-1 nil nil all-frames))
1259 (setq time (window-use-time window))
1260 (when (or (not best-time) (> time best-time))
1261 (setq best-time time)
1262 (setq best-window window)))
1263 best-window))
1264
1265 (defun get-largest-window (&optional all-frames dedicated)
1266 "Return the largest window on frames specified by ALL-FRAMES.
1267 A minibuffer window is never a candidate. A dedicated window is
1268 never a candidate unless DEDICATED is non-nil, so if all windows
1269 are dedicated, the value is nil.
1270
1271 The following non-nil values of the optional argument ALL-FRAMES
1272 have special meanings:
1273
1274 - t means consider all windows on all existing frames.
1275
1276 - `visible' means consider all windows on all visible frames on
1277 the current terminal.
1278
1279 - 0 (the number zero) means consider all windows on all visible
1280 and iconified frames on the current terminal.
1281
1282 - A frame means consider all windows on that frame only.
1283
1284 Any other value of ALL-FRAMES means consider all windows on the
1285 selected frame and no others."
1286 (let ((best-size 0)
1287 best-window size)
1288 (dolist (window (window-list-1 nil nil all-frames))
1289 (when (or dedicated (not (window-dedicated-p window)))
1290 (setq size (* (window-total-size window)
1291 (window-total-size window t)))
1292 (when (> size best-size)
1293 (setq best-size size)
1294 (setq best-window window))))
1295 best-window))
1296
1297 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1298 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1299 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1300 and defaults to the current buffer.
1301
1302 Any windows showing BUFFER-OR-NAME on the selected frame are listed
1303 first.
1304
1305 MINIBUF t means include the minibuffer window even if the
1306 minibuffer is not active. MINIBUF nil or omitted means include
1307 the minibuffer window only if the minibuffer is active. Any
1308 other value means do not include the minibuffer window even if
1309 the minibuffer is active.
1310
1311 ALL-FRAMES nil or omitted means consider all windows on the
1312 selected frame, plus the minibuffer window if specified by the
1313 MINIBUF argument. If the minibuffer counts, consider all windows
1314 on all frames that share that minibuffer too. The following
1315 non-nil values of ALL-FRAMES have special meanings:
1316
1317 - t means consider all windows on all existing frames.
1318
1319 - `visible' means consider all windows on all visible frames on
1320 the current terminal.
1321
1322 - 0 (the number zero) means consider all windows on all visible
1323 and iconified frames on the current terminal.
1324
1325 - A frame means consider all windows on that frame only.
1326
1327 Anything else means consider all windows on the selected frame
1328 and no others."
1329 (let ((buffer (normalize-live-buffer buffer-or-name))
1330 windows)
1331 (dolist (window (window-list-1 (frame-first-window) minibuf all-frames))
1332 (when (eq (window-buffer window) buffer)
1333 (setq windows (cons window windows))))
1334 (nreverse windows)))
1335
1336 (defun minibuffer-window-active-p (window)
1337 "Return t if WINDOW is the currently active minibuffer window."
1338 (eq window (active-minibuffer-window)))
1339
1340 (defun count-windows (&optional minibuf)
1341 "Return the number of live windows on the selected frame.
1342 The optional argument MINIBUF specifies whether the minibuffer
1343 window shall be counted. See `walk-windows' for the precise
1344 meaning of this argument."
1345 (length (window-list-1 nil minibuf)))
1346 \f
1347 ;;; Resizing windows.
1348 (defun resize-window-reset (&optional frame horizontal)
1349 "Reset resize values for all windows on FRAME.
1350 FRAME defaults to the selected frame.
1351
1352 This function stores the current value of `window-total-size' applied
1353 with argument HORIZONTAL in the new total size of all windows on
1354 FRAME. It also resets the new normal size of each of these
1355 windows."
1356 (resize-window-reset-1
1357 (frame-root-window (normalize-live-frame frame)) horizontal))
1358
1359 (defun resize-window-reset-1 (window horizontal)
1360 "Internal function of `resize-window-reset'."
1361 ;; Register old size in the new total size.
1362 (set-window-new-total window (window-total-size window horizontal))
1363 ;; Reset new normal size.
1364 (set-window-new-normal window)
1365 (when (window-child window)
1366 (resize-window-reset-1 (window-child window) horizontal))
1367 (when (window-right window)
1368 (resize-window-reset-1 (window-right window) horizontal)))
1369
1370 ;; The following routine is used to manually resize the minibuffer
1371 ;; window and is currently used, for example, by ispell.el.
1372 (defun resize-mini-window (window delta)
1373 "Resize minibuffer window WINDOW by DELTA lines.
1374 If WINDOW cannot be resized by DELTA lines make it as large \(or
1375 as small) as possible but don't signal an error."
1376 (when (window-minibuffer-p window)
1377 (let* ((frame (window-frame window))
1378 (root (frame-root-window frame))
1379 (height (window-total-size window))
1380 (min-delta
1381 (- (window-total-size root)
1382 (window-min-size root))))
1383 ;; Sanitize DELTA.
1384 (cond
1385 ((<= (+ height delta) 0)
1386 (setq delta (- (- height 1))))
1387 ((> delta min-delta)
1388 (setq delta min-delta)))
1389
1390 ;; Resize now.
1391 (resize-window-reset frame)
1392 ;; Ideally we should be able to resize just the last subwindow of
1393 ;; root here. See the comment in `resize-root-window-vertically'
1394 ;; for why we do not do that.
1395 (resize-this-window root (- delta) nil nil t)
1396 (set-window-new-total window (+ height delta))
1397 ;; The following routine catches the case where we want to resize
1398 ;; a minibuffer-only frame.
1399 (resize-mini-window-internal window))))
1400
1401 (defun resize-window (window delta &optional horizontal ignore)
1402 "Resize WINDOW vertically by DELTA lines.
1403 WINDOW can be an arbitrary window and defaults to the selected
1404 one. An attempt to resize the root window of a frame will raise
1405 an error though.
1406
1407 DELTA a positive number means WINDOW shall be enlarged by DELTA
1408 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1409 lines.
1410
1411 Optional argument HORIZONTAL non-nil means resize WINDOW
1412 horizontally by DELTA columns. In this case a positive DELTA
1413 means enlarge WINDOW by DELTA columns. DELTA negative means
1414 WINDOW shall be shrunk by -DELTA columns.
1415
1416 Optional argument IGNORE non-nil means ignore any restrictions
1417 imposed by fixed size windows, `window-min-height' or
1418 `window-min-width' settings. IGNORE any window means ignore
1419 restrictions for that window only. IGNORE equal `safe' means
1420 live windows may get as small as `window-safe-min-height' lines
1421 and `window-safe-min-width' columns.
1422
1423 This function resizes other windows proportionally and never
1424 deletes any windows. If you want to move only the low (right)
1425 edge of WINDOW consider using `adjust-window-trailing-edge'
1426 instead."
1427 (setq window (normalize-any-window window))
1428 (let* ((frame (window-frame window))
1429 sibling)
1430 (cond
1431 ((eq window (frame-root-window frame))
1432 (error "Cannot resize the root window of a frame"))
1433 ((window-minibuffer-p window)
1434 (resize-mini-window window delta))
1435 ((window-resizable-p window delta horizontal ignore)
1436 (resize-window-reset frame horizontal)
1437 (resize-this-window window delta horizontal ignore t)
1438 (if (and (not (window-splits window))
1439 (window-iso-combined-p window horizontal)
1440 (setq sibling (or (window-right window) (window-left window)))
1441 (window-sizable-p sibling (- delta) horizontal ignore))
1442 ;; If window-splits returns nil for WINDOW, WINDOW is part of
1443 ;; an iso-combination, and WINDOW's neighboring right or left
1444 ;; sibling can be resized as requested, resize that sibling.
1445 (let ((normal-delta
1446 (/ (float delta)
1447 (window-total-size (window-parent window) horizontal))))
1448 (resize-this-window sibling (- delta) horizontal nil t)
1449 (set-window-new-normal
1450 window (+ (window-normal-size window horizontal)
1451 normal-delta))
1452 (set-window-new-normal
1453 sibling (- (window-normal-size sibling horizontal)
1454 normal-delta)))
1455 ;; Otherwise, resize all other windows in the same combination.
1456 (resize-other-windows window delta horizontal ignore))
1457 (resize-window-apply frame horizontal))
1458 (t
1459 (error "Cannot resize window %s" window)))))
1460
1461 (defsubst resize-subwindows-skip-p (window)
1462 "Return non-nil if WINDOW shall be skipped by resizing routines."
1463 (memq (window-new-normal window) '(ignore stuck skip)))
1464
1465 (defun resize-subwindows-normal (parent horizontal window this-delta &optional trail other-delta)
1466 "Set the new normal height of subwindows of window PARENT.
1467 HORIZONTAL non-nil means set the new normal width of these
1468 windows. WINDOW specifies a subwindow of PARENT that has been
1469 resized by THIS-DELTA lines \(columns).
1470
1471 Optional argument TRAIL either 'before or 'after means set values
1472 for windows before or after WINDOW only. Optional argument
1473 OTHER-DELTA a number specifies that this many lines \(columns)
1474 have been obtained from \(or returned to) an ancestor window of
1475 PARENT in order to resize WINDOW."
1476 (let* ((delta-normal
1477 (if (and (= (- this-delta) (window-total-size window horizontal))
1478 (zerop other-delta))
1479 ;; When WINDOW gets deleted and we can return its entire
1480 ;; space to its siblings, use WINDOW's normal size as the
1481 ;; normal delta.
1482 (- (window-normal-size window horizontal))
1483 ;; In any other case calculate the normal delta from the
1484 ;; relation of THIS-DELTA to the total size of PARENT.
1485 (/ (float this-delta) (window-total-size parent horizontal))))
1486 (sub (window-child parent))
1487 (parent-normal 0.0)
1488 (skip (eq trail 'after)))
1489
1490 ;; Set parent-normal to the sum of the normal sizes of all
1491 ;; subwindows of PARENT that shall be resized, excluding only WINDOW
1492 ;; and any windows specified by the optional TRAIL argument.
1493 (while sub
1494 (cond
1495 ((eq sub window)
1496 (setq skip (eq trail 'before)))
1497 (skip)
1498 (t
1499 (setq parent-normal
1500 (+ parent-normal (window-normal-size sub horizontal)))))
1501 (setq sub (window-right sub)))
1502
1503 ;; Set the new normal size of all subwindows of PARENT from what
1504 ;; they should have contributed for recovering THIS-DELTA lines
1505 ;; (columns).
1506 (setq sub (window-child parent))
1507 (setq skip (eq trail 'after))
1508 (while sub
1509 (cond
1510 ((eq sub window)
1511 (setq skip (eq trail 'before)))
1512 (skip)
1513 (t
1514 (let ((old-normal (window-normal-size sub horizontal)))
1515 (set-window-new-normal
1516 sub (min 1.0 ; Don't get larger than 1.
1517 (max (- old-normal
1518 (* (/ old-normal parent-normal)
1519 delta-normal))
1520 ;; Don't drop below 0.
1521 0.0))))))
1522 (setq sub (window-right sub)))
1523
1524 (when (numberp other-delta)
1525 ;; Set the new normal size of windows from what they should have
1526 ;; contributed for recovering OTHER-DELTA lines (columns).
1527 (setq delta-normal (/ (float (window-total-size parent horizontal))
1528 (+ (window-total-size parent horizontal)
1529 other-delta)))
1530 (setq sub (window-child parent))
1531 (setq skip (eq trail 'after))
1532 (while sub
1533 (cond
1534 ((eq sub window)
1535 (setq skip (eq trail 'before)))
1536 (skip)
1537 (t
1538 (set-window-new-normal
1539 sub (min 1.0 ; Don't get larger than 1.
1540 (max (* (window-new-normal sub) delta-normal)
1541 ;; Don't drop below 0.
1542 0.0)))))
1543 (setq sub (window-right sub))))
1544
1545 ;; Set the new normal size of WINDOW to what is left by the sum of
1546 ;; the normal sizes of its siblings.
1547 (set-window-new-normal
1548 window
1549 (let ((sum 0))
1550 (setq sub (window-child parent))
1551 (while sub
1552 (cond
1553 ((eq sub window))
1554 ((not (numberp (window-new-normal sub)))
1555 (setq sum (+ sum (window-normal-size sub horizontal))))
1556 (t
1557 (setq sum (+ sum (window-new-normal sub)))))
1558 (setq sub (window-right sub)))
1559 ;; Don't get larger than 1 or smaller than 0.
1560 (min 1.0 (max (- 1.0 sum) 0.0))))))
1561
1562 (defun resize-subwindows (parent delta &optional horizontal window ignore trail edge)
1563 "Resize subwindows of window PARENT vertically by DELTA lines.
1564 PARENT must be a vertically combined internal window.
1565
1566 Optional argument HORIZONTAL non-nil means resize subwindows of
1567 PARENT horizontally by DELTA columns. In this case PARENT must
1568 be a horizontally combined internal window.
1569
1570 WINDOW, if specified, must denote a child window of PARENT that
1571 is resized by DELTA lines.
1572
1573 Optional argument IGNORE non-nil means ignore any restrictions
1574 imposed by fixed size windows, `window-min-height' or
1575 `window-min-width' settings. IGNORE equal `safe' means live
1576 windows may get as small as `window-safe-min-height' lines and
1577 `window-safe-min-width' columns. IGNORE any window means ignore
1578 restrictions for that window only.
1579
1580 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1581 of windows that shall be resized. If TRAIL equals `before',
1582 resize only windows on the left or above EDGE. If TRAIL equals
1583 `after', resize only windows on the right or below EDGE. Also,
1584 preferably only resize windows adjacent to EDGE.
1585
1586 Return the symbol `normalized' if new normal sizes have been
1587 already set by this routine."
1588 (let* ((first (window-child parent))
1589 (sub first)
1590 (parent-total (+ (window-total-size parent horizontal) delta))
1591 best-window best-value)
1592
1593 (if (and edge (memq trail '(before after))
1594 (progn
1595 (setq sub first)
1596 (while (and (window-right sub)
1597 (or (and (eq trail 'before)
1598 (not (resize-subwindows-skip-p
1599 (window-right sub))))
1600 (and (eq trail 'after)
1601 (resize-subwindows-skip-p sub))))
1602 (setq sub (window-right sub)))
1603 sub)
1604 (if horizontal
1605 (if (eq trail 'before)
1606 (= (+ (window-left-column sub)
1607 (window-total-size sub t))
1608 edge)
1609 (= (window-left-column sub) edge))
1610 (if (eq trail 'before)
1611 (= (+ (window-top-line sub)
1612 (window-total-size sub))
1613 edge)
1614 (= (window-top-line sub) edge)))
1615 (window-sizable-p sub delta horizontal ignore))
1616 ;; Resize only windows adjacent to EDGE.
1617 (progn
1618 (resize-this-window sub delta horizontal ignore t trail edge)
1619 (if (and window (eq (window-parent sub) parent))
1620 (progn
1621 ;; Assign new normal sizes.
1622 (set-window-new-normal
1623 sub (/ (float (window-new-total sub)) parent-total))
1624 (set-window-new-normal
1625 window (- (window-normal-size window horizontal)
1626 (- (window-new-normal sub)
1627 (window-normal-size sub horizontal)))))
1628 (resize-subwindows-normal parent horizontal sub 0 trail delta))
1629 ;; Return 'normalized to notify `resize-other-windows' that
1630 ;; normal sizes have been already set.
1631 'normalized)
1632 ;; Resize all windows proportionally.
1633 (setq sub first)
1634 (while sub
1635 (cond
1636 ((or (resize-subwindows-skip-p sub)
1637 ;; Ignore windows to skip and fixed-size subwindows - in
1638 ;; the latter case make it a window to skip.
1639 (and (not ignore)
1640 (window-size-fixed-p sub horizontal)
1641 (set-window-new-normal sub 'ignore))))
1642 ((< delta 0)
1643 ;; When shrinking store the number of lines/cols we can get
1644 ;; from this window here together with the total/normal size
1645 ;; factor.
1646 (set-window-new-normal
1647 sub
1648 (cons
1649 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
1650 (window-min-delta sub horizontal ignore trail t) ; t)
1651 (- (/ (float (window-total-size sub horizontal))
1652 parent-total)
1653 (window-normal-size sub horizontal)))))
1654 ((> delta 0)
1655 ;; When enlarging store the total/normal size factor only
1656 (set-window-new-normal
1657 sub
1658 (- (/ (float (window-total-size sub horizontal))
1659 parent-total)
1660 (window-normal-size sub horizontal)))))
1661
1662 (setq sub (window-right sub)))
1663
1664 (cond
1665 ((< delta 0)
1666 ;; Shrink windows by delta.
1667 (setq best-window t)
1668 (while (and best-window (not (zerop delta)))
1669 (setq sub first)
1670 (setq best-window nil)
1671 (setq best-value most-negative-fixnum)
1672 (while sub
1673 (when (and (consp (window-new-normal sub))
1674 (not (zerop (car (window-new-normal sub))))
1675 (> (cdr (window-new-normal sub)) best-value))
1676 (setq best-window sub)
1677 (setq best-value (cdr (window-new-normal sub))))
1678
1679 (setq sub (window-right sub)))
1680
1681 (when best-window
1682 (setq delta (1+ delta)))
1683 (set-window-new-total best-window -1 t)
1684 (set-window-new-normal
1685 best-window
1686 (if (= (car (window-new-normal best-window)) 1)
1687 'skip ; We can't shrink best-window any further.
1688 (cons (1- (car (window-new-normal best-window)))
1689 (- (/ (float (window-new-total best-window))
1690 parent-total)
1691 (window-normal-size best-window horizontal)))))))
1692 ((> delta 0)
1693 ;; Enlarge windows by delta.
1694 (setq best-window t)
1695 (while (and best-window (not (zerop delta)))
1696 (setq sub first)
1697 (setq best-window nil)
1698 (setq best-value most-positive-fixnum)
1699 (while sub
1700 (when (and (numberp (window-new-normal sub))
1701 (< (window-new-normal sub) best-value))
1702 (setq best-window sub)
1703 (setq best-value (window-new-normal sub)))
1704
1705 (setq sub (window-right sub)))
1706
1707 (when best-window
1708 (setq delta (1- delta)))
1709 (set-window-new-total best-window 1 t)
1710 (set-window-new-normal
1711 best-window
1712 (- (/ (float (window-new-total best-window))
1713 parent-total)
1714 (window-normal-size best-window horizontal))))))
1715
1716 (when best-window
1717 (setq sub first)
1718 (while sub
1719 (when (or (consp (window-new-normal sub))
1720 (numberp (window-new-normal sub)))
1721 ;; Reset new normal size fields so `resize-window-apply'
1722 ;; won't use them to apply new sizes.
1723 (set-window-new-normal sub))
1724
1725 (unless (eq (window-new-normal sub) 'ignore)
1726 ;; Resize this subwindow's subwindows (back-engineering
1727 ;; delta from sub's old and new total sizes).
1728 (let ((delta (- (window-new-total sub)
1729 (window-total-size sub horizontal))))
1730 (unless (and (zerop delta) (not trail))
1731 ;; For the TRAIL non-nil case we have to resize SUB
1732 ;; recursively even if it's size does not change.
1733 (resize-this-window
1734 sub delta horizontal ignore nil trail edge))))
1735 (setq sub (window-right sub)))))))
1736
1737 (defun resize-other-windows (window delta &optional horizontal ignore trail edge)
1738 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1739 Optional argument HORIZONTAL non-nil means resize other windows
1740 when WINDOW is resized horizontally by DELTA columns. WINDOW
1741 itself is not resized by this function.
1742
1743 Optional argument IGNORE non-nil means ignore any restrictions
1744 imposed by fixed size windows, `window-min-height' or
1745 `window-min-width' settings. IGNORE equal `safe' means live
1746 windows may get as small as `window-safe-min-height' lines and
1747 `window-safe-min-width' columns. IGNORE any window means ignore
1748 restrictions for that window only.
1749
1750 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1751 of windows that shall be resized. If TRAIL equals `before',
1752 resize only windows on the left or above EDGE. If TRAIL equals
1753 `after', resize only windows on the right or below EDGE. Also,
1754 preferably only resize windows adjacent to EDGE."
1755 (when (window-parent window)
1756 (let* ((parent (window-parent window))
1757 (sub (window-child parent)))
1758 (if (window-iso-combined-p sub horizontal)
1759 ;; In an iso-combination try to extract DELTA from WINDOW's
1760 ;; siblings.
1761 (let ((first sub)
1762 (skip (eq trail 'after))
1763 this-delta other-delta)
1764 ;; Decide which windows shall be left alone.
1765 (while sub
1766 (cond
1767 ((eq sub window)
1768 ;; Make sure WINDOW is left alone when
1769 ;; resizing its siblings.
1770 (set-window-new-normal sub 'ignore)
1771 (setq skip (eq trail 'before)))
1772 (skip
1773 ;; Make sure this sibling is left alone when
1774 ;; resizing its siblings.
1775 (set-window-new-normal sub 'ignore))
1776 ((or (window-size-ignore sub ignore)
1777 (not (window-size-fixed-p sub horizontal)))
1778 ;; Set this-delta to t to signal that we found a sibling
1779 ;; of WINDOW whose size is not fixed.
1780 (setq this-delta t)))
1781
1782 (setq sub (window-right sub)))
1783
1784 ;; Set this-delta to what we can get from WINDOW's siblings.
1785 (if (= (- delta) (window-total-size window horizontal))
1786 ;; A deletion, presumably. We must handle this case
1787 ;; specially since `window-resizable' can't be used.
1788 (if this-delta
1789 ;; There's at least one resizable sibling we can
1790 ;; give WINDOW's size to.
1791 (setq this-delta delta)
1792 ;; No resizable sibling exists.
1793 (setq this-delta 0))
1794 ;; Any other form of resizing.
1795 (setq this-delta
1796 (window-resizable window delta horizontal ignore trail t)))
1797
1798 ;; Set other-delta to what we still have to get from
1799 ;; ancestor windows of parent.
1800 (setq other-delta (- delta this-delta))
1801 (unless (zerop other-delta)
1802 ;; Unless we got everything from WINDOW's siblings, PARENT
1803 ;; must be resized by other-delta lines or columns.
1804 (set-window-new-total parent other-delta 'add))
1805
1806 (if (zerop this-delta)
1807 ;; We haven't got anything from WINDOW's siblings but we
1808 ;; must update the normal sizes to respect other-delta.
1809 (resize-subwindows-normal
1810 parent horizontal window this-delta trail other-delta)
1811 ;; We did get something from WINDOW's siblings which means
1812 ;; we have to resize their subwindows.
1813 (unless (eq (resize-subwindows parent (- this-delta) horizontal
1814 window ignore trail edge)
1815 ;; `resize-subwindows' returning 'normalized,
1816 ;; means it has set the normal sizes already.
1817 'normalized)
1818 ;; Set the normal sizes.
1819 (resize-subwindows-normal
1820 parent horizontal window this-delta trail other-delta))
1821 ;; Set DELTA to what we still have to get from ancestor
1822 ;; windows.
1823 (setq delta other-delta)))
1824
1825 ;; In an ortho-combination all siblings of WINDOW must be
1826 ;; resized by DELTA.
1827 (set-window-new-total parent delta 'add)
1828 (while sub
1829 (unless (eq sub window)
1830 (resize-this-window sub delta horizontal ignore t))
1831 (setq sub (window-right sub))))
1832
1833 (unless (zerop delta)
1834 ;; "Go up."
1835 (resize-other-windows parent delta horizontal ignore trail edge)))))
1836
1837 (defun resize-this-window (window delta &optional horizontal ignore add trail edge)
1838 "Resize WINDOW vertically by DELTA lines.
1839 Optional argument HORIZONTAL non-nil means resize WINDOW
1840 horizontally by DELTA columns.
1841
1842 Optional argument IGNORE non-nil means ignore any restrictions
1843 imposed by fixed size windows, `window-min-height' or
1844 `window-min-width' settings. IGNORE equal `safe' means live
1845 windows may get as small as `window-safe-min-height' lines and
1846 `window-safe-min-width' columns. IGNORE any window means ignore
1847 restrictions for that window only.
1848
1849 Optional argument ADD non-nil means add DELTA to the new total
1850 size of WINDOW.
1851
1852 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1853 of windows that shall be resized. If TRAIL equals `before',
1854 resize only windows on the left or above EDGE. If TRAIL equals
1855 `after', resize only windows on the right or below EDGE. Also,
1856 preferably only resize windows adjacent to EDGE.
1857
1858 This function recursively resizes WINDOW's subwindows to fit the
1859 new size. Make sure that WINDOW is `window-resizable' before
1860 calling this function. Note that this function does not resize
1861 siblings of WINDOW or WINDOW's parent window. You have to
1862 eventually call `resize-window-apply' in order to make resizing
1863 actually take effect."
1864 (when add
1865 ;; Add DELTA to the new total size of WINDOW.
1866 (set-window-new-total window delta t))
1867
1868 (let ((sub (window-child window)))
1869 (cond
1870 ((not sub))
1871 ((window-iso-combined-p sub horizontal)
1872 ;; In an iso-combination resize subwindows according to their
1873 ;; normal sizes.
1874 (resize-subwindows window delta horizontal nil ignore trail edge))
1875 ;; In an ortho-combination resize each subwindow by DELTA.
1876 (t
1877 (while sub
1878 (resize-this-window sub delta horizontal ignore t trail edge)
1879 (setq sub (window-right sub)))))))
1880
1881 (defun resize-root-window (window delta horizontal ignore)
1882 "Resize root window WINDOW vertically by DELTA lines.
1883 HORIZONTAL non-nil means resize root window WINDOW horizontally
1884 by DELTA columns.
1885
1886 IGNORE non-nil means ignore any restrictions imposed by fixed
1887 size windows, `window-min-height' or `window-min-width' settings.
1888
1889 This function is only called by the frame resizing routines. It
1890 resizes windows proportionally and never deletes any windows."
1891 (when (and (windowp window) (numberp delta)
1892 (window-sizable-p window delta horizontal ignore))
1893 (resize-window-reset (window-frame window) horizontal)
1894 (resize-this-window window delta horizontal ignore t)))
1895
1896 (defun resize-root-window-vertically (window delta)
1897 "Resize root window WINDOW vertically by DELTA lines.
1898 If DELTA is less than zero and we can't shrink WINDOW by DELTA
1899 lines, shrink it as much as possible. If DELTA is greater than
1900 zero, this function can resize fixed-size subwindows in order to
1901 recover the necessary lines.
1902
1903 Return the number of lines that were recovered.
1904
1905 This function is only called by the minibuffer window resizing
1906 routines. It resizes windows proportionally and never deletes
1907 any windows."
1908 (when (numberp delta)
1909 (let (ignore)
1910 (cond
1911 ((< delta 0)
1912 (setq delta (window-sizable window delta)))
1913 ((> delta 0)
1914 (unless (window-sizable window delta)
1915 (setq ignore t))))
1916
1917 (resize-window-reset (window-frame window))
1918 ;; Ideally, we would resize just the last window in a combination
1919 ;; but that's not feasible for the following reason: If we grow
1920 ;; the minibuffer window and the last window cannot be shrunk any
1921 ;; more, we shrink another window instead. But if we then shrink
1922 ;; the minibuffer window again, the last window might get enlarged
1923 ;; and the state after shrinking is not the state before growing.
1924 ;; So, in practice, we'd need a history variable to record how to
1925 ;; proceed. But I'm not sure how such a variable could work with
1926 ;; repeated minibuffer window growing steps.
1927 (resize-this-window window delta nil ignore t)
1928 delta)))
1929
1930 (defun adjust-window-trailing-edge (window delta &optional horizontal)
1931 "Move WINDOW's bottom edge by DELTA lines.
1932 Optional argument HORIZONTAL non-nil means move WINDOW's right
1933 edge by DELTA columns. WINDOW defaults to the selected window.
1934
1935 If DELTA is greater zero, then move the edge downwards or to the
1936 right. If DELTA is less than zero, move the edge upwards or to
1937 the left. If the edge can't be moved by DELTA lines or columns,
1938 move it as far as possible in the desired direction."
1939 (setq window (normalize-any-window window))
1940 (let ((frame (window-frame window))
1941 (right window)
1942 left this-delta min-delta max-delta failed)
1943 ;; Find the edge we want to move.
1944 (while (and (or (not (window-iso-combined-p right horizontal))
1945 (not (window-right right)))
1946 (setq right (window-parent right))))
1947 (cond
1948 ((and (not right) (not horizontal) (not resize-mini-windows)
1949 (eq (window-frame (minibuffer-window frame)) frame))
1950 (resize-mini-window (minibuffer-window frame) (- delta)))
1951 ((or (not (setq left right)) (not (setq right (window-right right))))
1952 (if horizontal
1953 (error "No window on the right of this one")
1954 (error "No window below this one")))
1955 (t
1956 ;; Set LEFT to the first resizable window on the left. This step is
1957 ;; needed to handle fixed-size windows.
1958 (while (and left (window-size-fixed-p left horizontal))
1959 (setq left
1960 (or (window-left left)
1961 (progn
1962 (while (and (setq left (window-parent left))
1963 (not (window-iso-combined-p left horizontal))))
1964 (window-left left)))))
1965 (unless left
1966 (if horizontal
1967 (error "No resizable window on the left of this one")
1968 (error "No resizable window above this one")))
1969
1970 ;; Set RIGHT to the first resizable window on the right. This step
1971 ;; is needed to handle fixed-size windows.
1972 (while (and right (window-size-fixed-p right horizontal))
1973 (setq right
1974 (or (window-right right)
1975 (progn
1976 (while (and (setq right (window-parent right))
1977 (not (window-iso-combined-p right horizontal))))
1978 (window-right right)))))
1979 (unless right
1980 (if horizontal
1981 (error "No resizable window on the right of this one")
1982 (error "No resizable window below this one")))
1983
1984 ;; LEFT and RIGHT (which might be both internal windows) are now the
1985 ;; two windows we want to resize.
1986 (cond
1987 ((> delta 0)
1988 (setq max-delta (window-max-delta-1 left 0 horizontal nil 'after))
1989 (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'before))
1990 (when (or (< max-delta delta) (> min-delta (- delta)))
1991 ;; We can't get the whole DELTA - move as far as possible.
1992 (setq delta (min max-delta (- min-delta))))
1993 (unless (zerop delta)
1994 ;; Start resizing.
1995 (resize-window-reset frame horizontal)
1996 ;; Try to enlarge LEFT first.
1997 (setq this-delta (window-resizable left delta horizontal))
1998 (unless (zerop this-delta)
1999 (resize-this-window
2000 left this-delta horizontal nil t 'before
2001 (if horizontal
2002 (+ (window-left-column left) (window-total-size left t))
2003 (+ (window-top-line left) (window-total-size left)))))
2004 ;; Shrink windows on right of LEFT.
2005 (resize-other-windows
2006 left delta horizontal nil 'after
2007 (if horizontal
2008 (window-left-column right)
2009 (window-top-line right)))))
2010 ((< delta 0)
2011 (setq max-delta (window-max-delta-1 right 0 horizontal nil 'before))
2012 (setq min-delta (window-min-delta-1 left delta horizontal nil 'after))
2013 (when (or (< max-delta (- delta)) (> min-delta delta))
2014 ;; We can't get the whole DELTA - move as far as possible.
2015 (setq delta (max (- max-delta) min-delta)))
2016 (unless (zerop delta)
2017 ;; Start resizing.
2018 (resize-window-reset frame horizontal)
2019 ;; Try to enlarge RIGHT.
2020 (setq this-delta (window-resizable right (- delta) horizontal))
2021 (unless (zerop this-delta)
2022 (resize-this-window
2023 right this-delta horizontal nil t 'after
2024 (if horizontal
2025 (window-left-column right)
2026 (window-top-line right))))
2027 ;; Shrink windows on left of RIGHT.
2028 (resize-other-windows
2029 right (- delta) horizontal nil 'before
2030 (if horizontal
2031 (+ (window-left-column left) (window-total-size left t))
2032 (+ (window-top-line left) (window-total-size left)))))))
2033 (unless (zerop delta)
2034 ;; Don't report an error in the standard case.
2035 (unless (resize-window-apply frame horizontal)
2036 ;; But do report an error if applying the changes fails.
2037 (error "Failed adjusting window %s" window)))))))
2038
2039 (defun enlarge-window (delta &optional horizontal)
2040 "Make selected window DELTA lines taller.
2041 Interactively, if no argument is given, make the selected window
2042 one line taller. If optional argument HORIZONTAL is non-nil,
2043 make selected window wider by DELTA columns. If DELTA is
2044 negative, shrink selected window by -DELTA lines or columns.
2045 Return nil."
2046 (interactive "p")
2047 (resize-window (selected-window) delta horizontal))
2048
2049 (defun shrink-window (delta &optional horizontal)
2050 "Make selected window DELTA lines smaller.
2051 Interactively, if no argument is given, make the selected window
2052 one line smaller. If optional argument HORIZONTAL is non-nil,
2053 make selected window narrower by DELTA columns. If DELTA is
2054 negative, enlarge selected window by -DELTA lines or columns.
2055 Return nil."
2056 (interactive "p")
2057 (resize-window (selected-window) (- delta) horizontal))
2058
2059 (defun maximize-window (&optional window)
2060 "Maximize WINDOW.
2061 Make WINDOW as large as possible without deleting any windows.
2062 WINDOW can be any window and defaults to the selected window."
2063 (interactive)
2064 (setq window (normalize-any-window window))
2065 (resize-window window (window-max-delta window))
2066 (resize-window window (window-max-delta window t) t))
2067
2068 (defun minimize-window (&optional window)
2069 "Minimize WINDOW.
2070 Make WINDOW as small as possible without deleting any windows.
2071 WINDOW can be any window and defaults to the selected window."
2072 (interactive)
2073 (setq window (normalize-any-window window))
2074 (resize-window window (- (window-min-delta window)))
2075 (resize-window window (- (window-min-delta window t)) t))
2076 \f
2077 (defsubst frame-root-window-p (window)
2078 "Return non-nil if WINDOW is the root window of its frame."
2079 (eq window (frame-root-window window)))
2080 \f
2081 ;; This should probably return non-nil when the selected window is part
2082 ;; of an atomic window whose root is the frame's root window.
2083 (defun one-window-p (&optional nomini all-frames)
2084 "Return non-nil if the selected window is the only window.
2085 Optional arg NOMINI non-nil means don't count the minibuffer
2086 even if it is active. Otherwise, the minibuffer is counted
2087 when it is active.
2088
2089 Optional argument ALL-FRAMES specifies the set of frames to
2090 consider, see also `next-window'. ALL-FRAMES nil or omitted
2091 means consider windows on the selected frame only, plus the
2092 minibuffer window if specified by the NOMINI argument. If the
2093 minibuffer counts, consider all windows on all frames that share
2094 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2095 with a special meaning are:
2096
2097 - t means consider all windows on all existing frames.
2098
2099 - `visible' means consider all windows on all visible frames on
2100 the current terminal.
2101
2102 - 0 (the number zero) means consider all windows on all visible
2103 and iconified frames on the current terminal.
2104
2105 - A frame means consider all windows on that frame only.
2106
2107 Anything else means consider all windows on the selected frame
2108 and no others."
2109 (let ((base-window (selected-window)))
2110 (if (and nomini (eq base-window (minibuffer-window)))
2111 (setq base-window (next-window base-window)))
2112 (eq base-window
2113 (next-window base-window (if nomini 'arg) all-frames))))
2114 \f
2115 ;;; Deleting windows.
2116 (defun window-deletable-p (&optional window)
2117 "Return t if WINDOW can be safely deleted from its frame.
2118 Return `frame' if deleting WINDOW should delete its frame
2119 instead."
2120 (setq window (normalize-any-window window))
2121 (unless ignore-window-parameters
2122 ;; Handle atomicity.
2123 (when (window-parameter window 'window-atom)
2124 (setq window (window-atom-root window))))
2125 (let ((parent (window-parent window))
2126 (frame (window-frame window))
2127 (dedicated (and (window-buffer window) (window-dedicated-p window)))
2128 (quit-restore (window-parameter window 'quit-restore)))
2129 (cond
2130 ((frame-root-window-p window)
2131 (when (and (or dedicated
2132 (and (eq (car-safe quit-restore) 'new-frame)
2133 (eq (nth 1 quit-restore) (window-buffer window))))
2134 (other-visible-frames-p frame))
2135 ;; WINDOW is the root window of its frame. Return `frame' but
2136 ;; only if WINDOW is (1) either dedicated or quit-restore's car
2137 ;; is new-frame and the window still displays the same buffer
2138 ;; and (2) there are other frames left.
2139 'frame))
2140 ((and (not ignore-window-parameters)
2141 (eq (window-parameter window 'window-side) 'none)
2142 (or (not parent)
2143 (not (eq (window-parameter parent 'window-side) 'none))))
2144 ;; Can't delete last main window.
2145 nil)
2146 (t))))
2147
2148 (defun window-or-subwindow-p (subwindow window)
2149 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2150 (or (eq subwindow window)
2151 (let ((parent (window-parent subwindow)))
2152 (catch 'done
2153 (while parent
2154 (if (eq parent window)
2155 (throw 'done t)
2156 (setq parent (window-parent parent))))))))
2157
2158 (defun delete-window (&optional window)
2159 "Delete WINDOW.
2160 WINDOW can be an arbitrary window and defaults to the selected
2161 one. Return nil.
2162
2163 If the variable `ignore-window-parameters' is non-nil or the
2164 `delete-window' parameter of WINDOW equals t, do not process any
2165 parameters of WINDOW. Otherwise, if the `delete-window'
2166 parameter of WINDOW specifies a function, call that function with
2167 WINDOW as its sole argument and return the value returned by that
2168 function.
2169
2170 Otherwise, if WINDOW is part of an atomic window, call
2171 `delete-window' with the root of the atomic window as its
2172 argument. If WINDOW is the only window on its frame or the last
2173 non-side window, signal an error."
2174 (interactive)
2175 (setq window (normalize-any-window window))
2176 (let* ((frame (window-frame window))
2177 (function (window-parameter window 'delete-window))
2178 (parent (window-parent window))
2179 atom-root)
2180 (window-check frame)
2181 (catch 'done
2182 ;; Handle window parameters.
2183 (cond
2184 ;; Ignore window parameters if `ignore-window-parameters' tells
2185 ;; us so or `delete-window' equals t.
2186 ((or ignore-window-parameters (eq function t)))
2187 ((functionp function)
2188 ;; The `delete-window' parameter specifies the function to call.
2189 ;; If that function is `ignore' nothing is done. It's up to the
2190 ;; function called here to avoid infinite recursion.
2191 (throw 'done (funcall function window)))
2192 ((and (window-parameter window 'window-atom)
2193 (setq atom-root (window-atom-root window))
2194 (not (eq atom-root window)))
2195 (throw 'done (delete-window atom-root)))
2196 ((and (eq (window-parameter window 'window-side) 'none)
2197 (or (not parent)
2198 (not (eq (window-parameter parent 'window-side) 'none))))
2199 (error "Attempt to delete last non-side window"))
2200 ((not parent)
2201 (error "Attempt to delete minibuffer or sole ordinary window")))
2202
2203 (let* ((horizontal (window-hchild parent))
2204 (size (window-total-size window horizontal))
2205 (frame-selected
2206 (window-or-subwindow-p (frame-selected-window frame) window))
2207 ;; Emacs 23 preferably gives WINDOW's space to its left
2208 ;; sibling.
2209 (sibling (or (window-left window) (window-right window))))
2210 (resize-window-reset frame horizontal)
2211 (cond
2212 ((and (not (window-splits window))
2213 sibling (window-sizable-p sibling size))
2214 ;; Resize WINDOW's sibling.
2215 (resize-this-window sibling size horizontal nil t)
2216 (set-window-new-normal
2217 sibling (+ (window-normal-size sibling horizontal)
2218 (window-normal-size window horizontal))))
2219 ((window-resizable-p window (- size) horizontal nil nil nil t)
2220 ;; Can do without resizing fixed-size windows.
2221 (resize-other-windows window (- size) horizontal))
2222 (t
2223 ;; Can't do without resizing fixed-size windows.
2224 (resize-other-windows window (- size) horizontal t)))
2225 ;; Actually delete WINDOW.
2226 (delete-window-internal window)
2227 (when (and frame-selected
2228 (window-parameter
2229 (frame-selected-window frame) 'no-other-window))
2230 ;; `delete-window-internal' has selected a window that should
2231 ;; not be selected, fix this here.
2232 (other-window -1 frame))
2233 (run-window-configuration-change-hook frame)
2234 (window-check frame)
2235 ;; Always return nil.
2236 nil))))
2237
2238 (defun delete-other-windows (&optional window)
2239 "Make WINDOW fill its frame.
2240 WINDOW may be any window and defaults to the selected one.
2241 Return nil.
2242
2243 If the variable `ignore-window-parameters' is non-nil or the
2244 `delete-other-windows' parameter of WINDOW equals t, do not
2245 process any parameters of WINDOW. Otherwise, if the
2246 `delete-other-windows' parameter of WINDOW specifies a function,
2247 call that function with WINDOW as its sole argument and return
2248 the value returned by that function.
2249
2250 Otherwise, if WINDOW is part of an atomic window, call this
2251 function with the root of the atomic window as its argument. If
2252 WINDOW is a non-side window, make WINDOW the only non-side window
2253 on the frame. Side windows are not deleted. If WINDOW is a side
2254 window signal an error."
2255 (interactive)
2256 (setq window (normalize-any-window window))
2257 (let* ((frame (window-frame window))
2258 (function (window-parameter window 'delete-other-windows))
2259 (window-side (window-parameter window 'window-side))
2260 atom-root side-main)
2261 (window-check frame)
2262 (catch 'done
2263 (cond
2264 ;; Ignore window parameters if `ignore-window-parameters' is t or
2265 ;; `delete-other-windows' is t.
2266 ((or ignore-window-parameters (eq function t)))
2267 ((functionp function)
2268 ;; The `delete-other-windows' parameter specifies the function
2269 ;; to call. If the function is `ignore' no windows are deleted.
2270 ;; It's up to the function called to avoid infinite recursion.
2271 (throw 'done (funcall function window)))
2272 ((and (window-parameter window 'window-atom)
2273 (setq atom-root (window-atom-root window))
2274 (not (eq atom-root window)))
2275 (throw 'done (delete-other-windows atom-root)))
2276 ((eq window-side 'none)
2277 ;; Set side-main to the major non-side window.
2278 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2279 ((memq window-side window-sides)
2280 (error "Cannot make side window the only window")))
2281 ;; If WINDOW is the main non-side window, do nothing.
2282 (unless (eq window side-main)
2283 (delete-other-windows-internal window side-main)
2284 (run-window-configuration-change-hook frame)
2285 (window-check frame))
2286 ;; Always return nil.
2287 nil)))
2288 \f
2289 ;;; Splitting windows.
2290 (defsubst window-split-min-size (&optional horizontal)
2291 "Return minimum height of any window when splitting windows.
2292 Optional argument HORIZONTAL non-nil means return minimum width."
2293 (if horizontal
2294 (max window-min-width window-safe-min-width)
2295 (max window-min-height window-safe-min-height)))
2296
2297 (defun split-window (&optional window size side)
2298 "Make a new window adjacent to WINDOW.
2299 WINDOW can be any window and defaults to the selected one.
2300 Return the new window which is always a live window.
2301
2302 Optional argument SIZE a positive number means make WINDOW SIZE
2303 lines or columns tall. If SIZE is negative, make the new window
2304 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
2305 absolute value can be less than `window-min-height' or
2306 `window-min-width'; so this command can make a new window as
2307 small as one line or two columns. SIZE defaults to half of
2308 WINDOW's size. Interactively, SIZE is the prefix argument.
2309
2310 Optional third argument SIDE nil (or `below') specifies that the
2311 new window shall be located below WINDOW. SIDE `above' means the
2312 new window shall be located above WINDOW. In both cases SIZE
2313 specifies the new number of lines for WINDOW \(or the new window
2314 if SIZE is negative) including space reserved for the mode and/or
2315 header line.
2316
2317 SIDE t (or `right') specifies that the new window shall be
2318 located on the right side of WINDOW. SIDE `left' means the new
2319 window shall be located on the left of WINDOW. In both cases
2320 SIZE specifies the new number of columns for WINDOW \(or the new
2321 window provided SIZE is negative) including space reserved for
2322 fringes and the scrollbar or a divider column. Any other non-nil
2323 value for SIDE is currently handled like t (or `right').
2324
2325 If the variable `ignore-window-parameters' is non-nil or the
2326 `split-window' parameter of WINDOW equals t, do not process any
2327 parameters of WINDOW. Otherwise, if the `split-window' parameter
2328 of WINDOW specifies a function, call that function with all three
2329 arguments and return the value returned by that function.
2330
2331 Otherwise, if WINDOW is part of an atomic window, \"split\" the
2332 root of that atomic window. The new window does not become a
2333 member of that atomic window.
2334
2335 If WINDOW is live, properties of the new window like margins and
2336 scrollbars are inherited from WINDOW. If WINDOW is an internal
2337 window, these properties as well as the buffer displayed in the
2338 new window are inherited from the window selected on WINDOW's
2339 frame. The selected window is not changed by this function."
2340 (interactive "i")
2341 (setq window (normalize-any-window window))
2342 (let* ((horizontal (not (memq side '(nil below above))))
2343 (frame (window-frame window))
2344 (parent (window-parent window))
2345 (function (window-parameter window 'split-window))
2346 (window-side (window-parameter window 'window-side))
2347 ;; Rebind `window-nest' since in some cases we may have to
2348 ;; override its value.
2349 (window-nest window-nest)
2350 atom-root)
2351
2352 (window-check frame)
2353 (catch 'done
2354 (cond
2355 ;; Ignore window parameters if either `ignore-window-parameters'
2356 ;; is t or the `split-window' parameter equals t.
2357 ((or ignore-window-parameters (eq function t)))
2358 ((functionp function)
2359 ;; The `split-window' parameter specifies the function to call.
2360 ;; If that function is `ignore', do nothing.
2361 (throw 'done (funcall function window size side)))
2362 ;; If WINDOW is a subwindow of an atomic window, split the root
2363 ;; window of that atomic window instead.
2364 ((and (window-parameter window 'window-atom)
2365 (setq atom-root (window-atom-root window))
2366 (not (eq atom-root window)))
2367 (throw 'done (split-window atom-root size side))))
2368
2369 (when (and window-side
2370 (or (not parent)
2371 (not (window-parameter parent 'window-side))))
2372 ;; WINDOW is a side root window. To make sure that a new parent
2373 ;; window gets created set `window-nest' to t.
2374 (setq window-nest t))
2375
2376 (when (and window-splits size (> size 0))
2377 ;; If `window-splits' is non-nil and SIZE is a non-negative
2378 ;; integer, we cannot reasonably resize other windows. Rather
2379 ;; bind `window-nest' to t to make sure that subsequent window
2380 ;; deletions are handled correctly.
2381 (setq window-nest t))
2382
2383 (let* ((parent-size
2384 ;; `parent-size' is the size of WINDOW's parent, provided
2385 ;; it has one.
2386 (when parent (window-total-size parent horizontal)))
2387 ;; `resize' non-nil means we are supposed to resize other
2388 ;; windows in WINDOW's combination.
2389 (resize
2390 (and window-splits (not window-nest)
2391 ;; Resize makes sense in iso-combinations only.
2392 (window-iso-combined-p window horizontal)))
2393 ;; `old-size' is the current size of WINDOW.
2394 (old-size (window-total-size window horizontal))
2395 ;; `new-size' is the specified or calculated size of the
2396 ;; new window.
2397 (new-size
2398 (cond
2399 ((not size)
2400 (max (window-split-min-size horizontal)
2401 (if resize
2402 ;; When resizing try to give the new window the
2403 ;; average size of a window in its combination.
2404 (min (- parent-size
2405 (window-min-size parent horizontal))
2406 (/ parent-size
2407 (1+ (window-iso-combinations
2408 parent horizontal))))
2409 ;; Else try to give the new window half the size
2410 ;; of WINDOW (plus an eventual odd line).
2411 (+ (/ old-size 2) (% old-size 2)))))
2412 ((>= size 0)
2413 ;; SIZE non-negative specifies the new size of WINDOW.
2414
2415 ;; Note: Specifying a non-negative SIZE is practically
2416 ;; always done as workaround for making the new window
2417 ;; appear above or on the left of the new window (the
2418 ;; ispell window is a typical example of that). In all
2419 ;; these cases the SIDE argument should be set to 'above
2420 ;; or 'left in order to support the 'resize option.
2421 ;; Here we have to nest the windows instead, see above.
2422 (- old-size size))
2423 (t
2424 ;; SIZE negative specifies the size of the new window.
2425 (- size))))
2426 new-parent new-normal)
2427
2428 ;; Check SIZE.
2429 (cond
2430 ((not size)
2431 (cond
2432 (resize
2433 ;; SIZE unspecified, resizing.
2434 (when (and (not (window-sizable-p parent (- new-size) horizontal))
2435 ;; Try again with minimum split size.
2436 (setq new-size
2437 (max new-size (window-split-min-size horizontal)))
2438 (not (window-sizable-p parent (- new-size) horizontal)))
2439 (error "Window %s too small for splitting" parent)))
2440 ((> (+ new-size (window-min-size window horizontal)) old-size)
2441 ;; SIZE unspecified, no resizing.
2442 (error "Window %s too small for splitting" window))))
2443 ((and (>= size 0)
2444 (or (>= size old-size)
2445 (< new-size (if horizontal
2446 window-safe-min-width
2447 window-safe-min-width))))
2448 ;; SIZE specified as new size of old window. If the new size
2449 ;; is larger than the old size or the size of the new window
2450 ;; would be less than the safe minimum, signal an error.
2451 (error "Window %s too small for splitting" window))
2452 (resize
2453 ;; SIZE specified, resizing.
2454 (unless (window-sizable-p parent (- new-size) horizontal)
2455 ;; If we cannot resize the parent give up.
2456 (error "Window %s too small for splitting" parent)))
2457 ((or (< new-size
2458 (if horizontal window-safe-min-width window-safe-min-height))
2459 (< (- old-size new-size)
2460 (if horizontal window-safe-min-width window-safe-min-height)))
2461 ;; SIZE specification violates minimum size restrictions.
2462 (error "Window %s too small for splitting" window)))
2463
2464 (resize-window-reset frame horizontal)
2465
2466 (setq new-parent
2467 ;; Make new-parent non-nil if we need a new parent window;
2468 ;; either because we want to nest or because WINDOW is not
2469 ;; iso-combined.
2470 (or window-nest (not (window-iso-combined-p window horizontal))))
2471 (setq new-normal
2472 ;; Make new-normal the normal size of the new window.
2473 (cond
2474 (size (/ (float new-size) (if new-parent old-size parent-size)))
2475 (new-parent 0.5)
2476 (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal))))
2477 (t (/ (window-normal-size window horizontal) 2.0))))
2478
2479 (if resize
2480 ;; Try to get space from OLD's siblings. We could go "up" and
2481 ;; try getting additional space from surrounding windows but
2482 ;; we won't be able to return space to those windows when we
2483 ;; delete the one we create here. Hence we do not go up.
2484 (progn
2485 (resize-subwindows parent (- new-size) horizontal)
2486 (let* ((normal (- 1.0 new-normal))
2487 (sub (window-child parent)))
2488 (while sub
2489 (set-window-new-normal
2490 sub (* (window-normal-size sub horizontal) normal))
2491 (setq sub (window-right sub)))))
2492 ;; Get entire space from WINDOW.
2493 (set-window-new-total window (- old-size new-size))
2494 (resize-this-window window (- new-size) horizontal)
2495 (set-window-new-normal
2496 window (- (if new-parent 1.0 (window-normal-size window horizontal))
2497 new-normal)))
2498
2499 (let* ((new (split-window-internal window new-size side new-normal)))
2500 ;; Inherit window-side parameters, if any.
2501 (when (and window-side new-parent)
2502 (set-window-parameter (window-parent new) 'window-side window-side)
2503 (set-window-parameter new 'window-side window-side))
2504
2505 (run-window-configuration-change-hook frame)
2506 (window-check frame)
2507 ;; Always return the new window.
2508 new)))))
2509
2510 ;; I think this should be the default; I think people will prefer it--rms.
2511 (defcustom split-window-keep-point t
2512 "If non-nil, \\[split-window-above-each-other] keeps the original point \
2513 in both children.
2514 This is often more convenient for editing.
2515 If nil, adjust point in each of the two windows to minimize redisplay.
2516 This is convenient on slow terminals, but point can move strangely.
2517
2518 This option applies only to `split-window-above-each-other' and
2519 functions that call it. `split-window' always keeps the original
2520 point in both children."
2521 :type 'boolean
2522 :group 'windows)
2523
2524 (defun split-window-above-each-other (&optional size)
2525 "Split selected window into two windows, one above the other.
2526 The upper window gets SIZE lines and the lower one gets the rest.
2527 SIZE negative means the lower window gets -SIZE lines and the
2528 upper one the rest. With no argument, split windows equally or
2529 close to it. Both windows display the same buffer, now current.
2530
2531 If the variable `split-window-keep-point' is non-nil, both new
2532 windows will get the same value of point as the selected window.
2533 This is often more convenient for editing. The upper window is
2534 the selected window.
2535
2536 Otherwise, we choose window starts so as to minimize the amount of
2537 redisplay; this is convenient on slow terminals. The new selected
2538 window is the one that the current value of point appears in. The
2539 value of point can change if the text around point is hidden by the
2540 new mode line.
2541
2542 Regardless of the value of `split-window-keep-point', the upper
2543 window is the original one and the return value is the new, lower
2544 window."
2545 (interactive "P")
2546 (let ((old-window (selected-window))
2547 (old-point (point))
2548 (size (and size (prefix-numeric-value size)))
2549 moved-by-window-height moved new-window bottom)
2550 (when (and size (< size 0) (< (- size) window-min-height))
2551 ;; `split-window' would not signal an error here.
2552 (error "Size of new window too small"))
2553 (setq new-window (split-window nil size))
2554 (unless split-window-keep-point
2555 (with-current-buffer (window-buffer)
2556 (goto-char (window-start))
2557 (setq moved (vertical-motion (window-height)))
2558 (set-window-start new-window (point))
2559 (when (> (point) (window-point new-window))
2560 (set-window-point new-window (point)))
2561 (when (= moved (window-height))
2562 (setq moved-by-window-height t)
2563 (vertical-motion -1))
2564 (setq bottom (point)))
2565 (and moved-by-window-height
2566 (<= bottom (point))
2567 (set-window-point old-window (1- bottom)))
2568 (and moved-by-window-height
2569 (<= (window-start new-window) old-point)
2570 (set-window-point new-window old-point)
2571 (select-window new-window)))
2572 (split-window-save-restore-data new-window old-window)))
2573
2574 (defalias 'split-window-vertically 'split-window-above-each-other)
2575
2576 ;; This is to avoid compiler warnings.
2577 (defvar view-return-to-alist)
2578
2579 (defun split-window-save-restore-data (new-window old-window)
2580 (with-current-buffer (window-buffer)
2581 (when view-mode
2582 (let ((old-info (assq old-window view-return-to-alist)))
2583 (when old-info
2584 (push (cons new-window (cons (car (cdr old-info)) t))
2585 view-return-to-alist))))
2586 new-window))
2587
2588 (defun split-window-side-by-side (&optional size)
2589 "Split selected window into two windows side by side.
2590 The selected window becomes the left one and gets SIZE columns.
2591 SIZE negative means the right window gets -SIZE lines.
2592
2593 SIZE includes the width of the window's scroll bar; if there are
2594 no scroll bars, it includes the width of the divider column to
2595 the window's right, if any. SIZE omitted or nil means split
2596 window equally.
2597
2598 The selected window remains selected. Return the new window."
2599 (interactive "P")
2600 (let ((old-window (selected-window))
2601 (size (and size (prefix-numeric-value size)))
2602 new-window)
2603 (when (and size (< size 0) (< (- size) window-min-width))
2604 ;; `split-window' would not signal an error here.
2605 (error "Size of new window too small"))
2606 (split-window-save-restore-data (split-window nil size t) old-window)))
2607
2608 (defalias 'split-window-horizontally 'split-window-side-by-side)
2609 \f
2610 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2611 ;;; `balance-windows' subroutines using `window-tree'
2612
2613 ;;; Translate from internal window tree format
2614
2615 (defun bw-get-tree (&optional window-or-frame)
2616 "Get a window split tree in our format.
2617
2618 WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil,
2619 then the whole window split tree for `selected-frame' is returned.
2620 If it is a frame, then this is used instead. If it is a window,
2621 then the smallest tree containing that window is returned."
2622 (when window-or-frame
2623 (unless (or (framep window-or-frame)
2624 (windowp window-or-frame))
2625 (error "Not a frame or window: %s" window-or-frame)))
2626 (let ((subtree (bw-find-tree-sub window-or-frame)))
2627 (when subtree
2628 (if (integerp subtree)
2629 nil
2630 (bw-get-tree-1 subtree)))))
2631
2632 (defun bw-get-tree-1 (split)
2633 (if (windowp split)
2634 split
2635 (let ((dir (car split))
2636 (edges (car (cdr split)))
2637 (childs (cdr (cdr split))))
2638 (list
2639 (cons 'dir (if dir 'ver 'hor))
2640 (cons 'b (nth 3 edges))
2641 (cons 'r (nth 2 edges))
2642 (cons 't (nth 1 edges))
2643 (cons 'l (nth 0 edges))
2644 (cons 'childs (mapcar #'bw-get-tree-1 childs))))))
2645
2646 (defun bw-find-tree-sub (window-or-frame &optional get-parent)
2647 (let* ((window (when (windowp window-or-frame) window-or-frame))
2648 (frame (when (windowp window) (window-frame window)))
2649 (wt (car (window-tree frame))))
2650 (when (< 1 (length (window-list frame 0)))
2651 (if window
2652 (bw-find-tree-sub-1 wt window get-parent)
2653 wt))))
2654
2655 (defun bw-find-tree-sub-1 (tree win &optional get-parent)
2656 (unless (windowp win) (error "Not a window: %s" win))
2657 (if (memq win tree)
2658 (if get-parent
2659 get-parent
2660 tree)
2661 (let ((childs (cdr (cdr tree)))
2662 child
2663 subtree)
2664 (while (and childs (not subtree))
2665 (setq child (car childs))
2666 (setq childs (cdr childs))
2667 (when (and child (listp child))
2668 (setq subtree (bw-find-tree-sub-1 child win get-parent))))
2669 (if (integerp subtree)
2670 (progn
2671 (if (= 1 subtree)
2672 tree
2673 (1- subtree)))
2674 subtree
2675 ))))
2676
2677 ;;; Window or object edges
2678
2679 (defun bw-l (obj)
2680 "Left edge of OBJ."
2681 (if (windowp obj) (nth 0 (window-edges obj)) (cdr (assq 'l obj))))
2682 (defun bw-t (obj)
2683 "Top edge of OBJ."
2684 (if (windowp obj) (nth 1 (window-edges obj)) (cdr (assq 't obj))))
2685 (defun bw-r (obj)
2686 "Right edge of OBJ."
2687 (if (windowp obj) (nth 2 (window-edges obj)) (cdr (assq 'r obj))))
2688 (defun bw-b (obj)
2689 "Bottom edge of OBJ."
2690 (if (windowp obj) (nth 3 (window-edges obj)) (cdr (assq 'b obj))))
2691
2692 ;;; Split directions
2693
2694 (defun bw-dir (obj)
2695 "Return window split tree direction if OBJ.
2696 If OBJ is a window return 'both. If it is a window split tree
2697 then return its direction."
2698 (if (symbolp obj)
2699 obj
2700 (if (windowp obj)
2701 'both
2702 (let ((dir (cdr (assq 'dir obj))))
2703 (unless (memq dir '(hor ver both))
2704 (error "Can't find dir in %s" obj))
2705 dir))))
2706
2707 (defun bw-eqdir (obj1 obj2)
2708 "Return t if window split tree directions are equal.
2709 OBJ1 and OBJ2 should be either windows or window split trees in
2710 our format. The directions returned by `bw-dir' are compared and
2711 t is returned if they are `eq' or one of them is 'both."
2712 (let ((dir1 (bw-dir obj1))
2713 (dir2 (bw-dir obj2)))
2714 (or (eq dir1 dir2)
2715 (eq dir1 'both)
2716 (eq dir2 'both))))
2717
2718 ;;; Building split tree
2719
2720 (defun bw-refresh-edges (obj)
2721 "Refresh the edge information of OBJ and return OBJ."
2722 (unless (windowp obj)
2723 (let ((childs (cdr (assq 'childs obj)))
2724 (ol 1000)
2725 (ot 1000)
2726 (or -1)
2727 (ob -1))
2728 (dolist (o childs)
2729 (when (> ol (bw-l o)) (setq ol (bw-l o)))
2730 (when (> ot (bw-t o)) (setq ot (bw-t o)))
2731 (when (< or (bw-r o)) (setq or (bw-r o)))
2732 (when (< ob (bw-b o)) (setq ob (bw-b o))))
2733 (setq obj (delq 'l obj))
2734 (setq obj (delq 't obj))
2735 (setq obj (delq 'r obj))
2736 (setq obj (delq 'b obj))
2737 (add-to-list 'obj (cons 'l ol))
2738 (add-to-list 'obj (cons 't ot))
2739 (add-to-list 'obj (cons 'r or))
2740 (add-to-list 'obj (cons 'b ob))
2741 ))
2742 obj)
2743
2744 ;;; Balance windows
2745
2746 (defun balance-windows (&optional window-or-frame)
2747 "Make windows the same heights or widths in window split subtrees.
2748
2749 When called non-interactively WINDOW-OR-FRAME may be either a
2750 window or a frame. It then balances the windows on the implied
2751 frame. If the parameter is a window only the corresponding window
2752 subtree is balanced."
2753 (interactive)
2754 (let (
2755 (wt (bw-get-tree window-or-frame))
2756 (w)
2757 (h)
2758 (tried-sizes)
2759 (last-sizes)
2760 (windows (window-list nil 0)))
2761 (when wt
2762 (while (not (member last-sizes tried-sizes))
2763 (when last-sizes (setq tried-sizes (cons last-sizes tried-sizes)))
2764 (setq last-sizes (mapcar (lambda (w)
2765 (window-edges w))
2766 windows))
2767 (when (eq 'hor (bw-dir wt))
2768 (setq w (- (bw-r wt) (bw-l wt))))
2769 (when (eq 'ver (bw-dir wt))
2770 (setq h (- (bw-b wt) (bw-t wt))))
2771 (bw-balance-sub wt w h)))))
2772
2773 (defun bw-adjust-window (window delta horizontal)
2774 "Wrapper around `adjust-window-trailing-edge' with error checking.
2775 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
2776 ;; `adjust-window-trailing-edge' may fail if delta is too large.
2777 (while (>= (abs delta) 1)
2778 (condition-case nil
2779 (progn
2780 (adjust-window-trailing-edge window delta horizontal)
2781 (setq delta 0))
2782 (error
2783 ;;(message "adjust: %s" (error-message-string err))
2784 (setq delta (/ delta 2))))))
2785
2786 (defun bw-balance-sub (wt w h)
2787 (setq wt (bw-refresh-edges wt))
2788 (unless w (setq w (- (bw-r wt) (bw-l wt))))
2789 (unless h (setq h (- (bw-b wt) (bw-t wt))))
2790 (if (windowp wt)
2791 (progn
2792 (when w
2793 (let ((dw (- w (- (bw-r wt) (bw-l wt)))))
2794 (when (/= 0 dw)
2795 (bw-adjust-window wt dw t))))
2796 (when h
2797 (let ((dh (- h (- (bw-b wt) (bw-t wt)))))
2798 (when (/= 0 dh)
2799 (bw-adjust-window wt dh nil)))))
2800 (let* ((childs (cdr (assq 'childs wt)))
2801 (cw (when w (/ w (if (bw-eqdir 'hor wt) (length childs) 1))))
2802 (ch (when h (/ h (if (bw-eqdir 'ver wt) (length childs) 1)))))
2803 (dolist (c childs)
2804 (bw-balance-sub c cw ch)))))
2805
2806 (defun window-fixed-size-p (&optional window direction)
2807 "Return t if WINDOW cannot be resized in DIRECTION.
2808 WINDOW defaults to the selected window. DIRECTION can be
2809 nil (i.e. any), `height' or `width'."
2810 (with-current-buffer (window-buffer window)
2811 (when (and (boundp 'window-size-fixed) window-size-fixed)
2812 (not (and direction
2813 (member (cons direction window-size-fixed)
2814 '((height . width) (width . height))))))))
2815
2816 ;;; A different solution to balance-windows.
2817
2818 (defvar window-area-factor 1
2819 "Factor by which the window area should be over-estimated.
2820 This is used by `balance-windows-area'.
2821 Changing this globally has no effect.")
2822 (make-variable-buffer-local 'window-area-factor)
2823
2824 (defun balance-windows-area ()
2825 "Make all visible windows the same area (approximately).
2826 See also `window-area-factor' to change the relative size of
2827 specific buffers."
2828 (interactive)
2829 (let* ((unchanged 0) (carry 0) (round 0)
2830 ;; Remove fixed-size windows.
2831 (wins (delq nil (mapcar (lambda (win)
2832 (if (not (window-fixed-size-p win)) win))
2833 (window-list nil 'nomini))))
2834 (changelog nil)
2835 next)
2836 ;; Resizing a window changes the size of surrounding windows in complex
2837 ;; ways, so it's difficult to balance them all. The introduction of
2838 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
2839 ;; very difficult to do. `balance-window' above takes an off-line
2840 ;; approach: get the whole window tree, then balance it, then try to
2841 ;; adjust the windows so they fit the result.
2842 ;; Here, instead, we take a "local optimization" approach, where we just
2843 ;; go through all the windows several times until nothing needs to be
2844 ;; changed. The main problem with this approach is that it's difficult
2845 ;; to make sure it terminates, so we use some heuristic to try and break
2846 ;; off infinite loops.
2847 ;; After a round without any change, we allow a second, to give a chance
2848 ;; to the carry to propagate a minor imbalance from the end back to
2849 ;; the beginning.
2850 (while (< unchanged 2)
2851 ;; (message "New round")
2852 (setq unchanged (1+ unchanged) round (1+ round))
2853 (dolist (win wins)
2854 (setq next win)
2855 (while (progn (setq next (next-window next))
2856 (window-fixed-size-p next)))
2857 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
2858 (let* ((horiz
2859 (< (car (window-edges win)) (car (window-edges next))))
2860 (areadiff (/ (- (* (window-height next) (window-width next)
2861 (buffer-local-value 'window-area-factor
2862 (window-buffer next)))
2863 (* (window-height win) (window-width win)
2864 (buffer-local-value 'window-area-factor
2865 (window-buffer win))))
2866 (max (buffer-local-value 'window-area-factor
2867 (window-buffer win))
2868 (buffer-local-value 'window-area-factor
2869 (window-buffer next)))))
2870 (edgesize (if horiz
2871 (+ (window-height win) (window-height next))
2872 (+ (window-width win) (window-width next))))
2873 (diff (/ areadiff edgesize)))
2874 (when (zerop diff)
2875 ;; Maybe diff is actually closer to 1 than to 0.
2876 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
2877 (when (and (zerop diff) (not (zerop areadiff)))
2878 (setq diff (/ (+ areadiff carry) edgesize))
2879 ;; Change things smoothly.
2880 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
2881 (if (zerop diff)
2882 ;; Make sure negligible differences don't accumulate to
2883 ;; become significant.
2884 (setq carry (+ carry areadiff))
2885 (bw-adjust-window win diff horiz)
2886 ;; (sit-for 0.5)
2887 (let ((change (cons win (window-edges win))))
2888 ;; If the same change has been seen already for this window,
2889 ;; we're most likely in an endless loop, so don't count it as
2890 ;; a change.
2891 (unless (member change changelog)
2892 (push change changelog)
2893 (setq unchanged 0 carry 0)))))))
2894 ;; We've now basically balanced all the windows.
2895 ;; But there may be some minor off-by-one imbalance left over,
2896 ;; so let's do some fine tuning.
2897 ;; (bw-finetune wins)
2898 ;; (message "Done in %d rounds" round)
2899 ))
2900
2901 \f
2902 (defcustom display-buffer-function nil
2903 "If non-nil, function to call to handle `display-buffer'.
2904 It will receive two args, the buffer and a flag which if non-nil
2905 means that the currently selected window is not acceptable. It
2906 should choose or create a window, display the specified buffer in
2907 it, and return the window.
2908
2909 Commands such as `switch-to-buffer-other-window' and
2910 `find-file-other-window' work using this function."
2911 :type '(choice
2912 (const nil)
2913 (function :tag "function"))
2914 :group 'windows)
2915
2916 (defcustom special-display-buffer-names nil
2917 "List of names of buffers that should be displayed specially.
2918 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
2919 its name is in this list, displays the buffer in a way specified
2920 by `special-display-function'. `special-display-popup-frame'
2921 \(the default for `special-display-function') usually displays
2922 the buffer in a separate frame made with the parameters specified
2923 by `special-display-frame-alist'. If `special-display-function'
2924 has been set to some other function, that function is called with
2925 the buffer as first, and nil as second argument.
2926
2927 Alternatively, an element of this list can be specified as
2928 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
2929 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
2930 `special-display-popup-frame' will interpret such pairs as frame
2931 parameters when it creates a special frame, overriding the
2932 corresponding values from `special-display-frame-alist'.
2933
2934 As a special case, if FRAME-PARAMETERS contains (same-window . t)
2935 `special-display-popup-frame' displays that buffer in the
2936 selected window. If FRAME-PARAMETERS contains (same-frame . t),
2937 it displays that buffer in a window on the selected frame.
2938
2939 If `special-display-function' specifies some other function than
2940 `special-display-popup-frame', that function is called with the
2941 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
2942 argument.
2943
2944 Finally, an element of this list can be also specified as
2945 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
2946 `special-display-popup-frame' will call FUNCTION with the buffer
2947 named BUFFER-NAME as first argument, and OTHER-ARGS as the
2948 second. If `special-display-function' specifies some other
2949 function, that function is called with the buffer named
2950 BUFFER-NAME as first, and the element's cdr as second argument.
2951
2952 If this variable appears \"not to work\", because you added a
2953 name to it but the corresponding buffer is displayed in the
2954 selected window, look at the values of `same-window-buffer-names'
2955 and `same-window-regexps'. Those variables take precedence over
2956 this one.
2957
2958 See also `special-display-regexps'."
2959 :type '(repeat
2960 (choice :tag "Buffer"
2961 :value ""
2962 (string :format "%v")
2963 (cons :tag "With parameters"
2964 :format "%v"
2965 :value ("" . nil)
2966 (string :format "%v")
2967 (repeat :tag "Parameters"
2968 (cons :format "%v"
2969 (symbol :tag "Parameter")
2970 (sexp :tag "Value"))))
2971 (list :tag "With function"
2972 :format "%v"
2973 :value ("" . nil)
2974 (string :format "%v")
2975 (function :tag "Function")
2976 (repeat :tag "Arguments" (sexp)))))
2977 :group 'windows
2978 :group 'frames)
2979
2980 ;;;###autoload
2981 (put 'special-display-buffer-names 'risky-local-variable t)
2982
2983 (defcustom special-display-regexps nil
2984 "List of regexps saying which buffers should be displayed specially.
2985 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
2986 any regexp in this list matches its name, displays it specially
2987 using `special-display-function'. `special-display-popup-frame'
2988 \(the default for `special-display-function') usually displays
2989 the buffer in a separate frame made with the parameters specified
2990 by `special-display-frame-alist'. If `special-display-function'
2991 has been set to some other function, that function is called with
2992 the buffer as first, and nil as second argument.
2993
2994 Alternatively, an element of this list can be specified as
2995 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
2996 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
2997 `special-display-popup-frame' will then interpret these pairs as
2998 frame parameters when creating a special frame for a buffer whose
2999 name matches REGEXP, overriding the corresponding values from
3000 `special-display-frame-alist'.
3001
3002 As a special case, if FRAME-PARAMETERS contains (same-window . t)
3003 `special-display-popup-frame' displays buffers matching REGEXP in
3004 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
3005 to display such buffers in a window on the selected frame.
3006
3007 If `special-display-function' specifies some other function than
3008 `special-display-popup-frame', that function is called with the
3009 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
3010 as second argument.
3011
3012 Finally, an element of this list can be also specified as
3013 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
3014 will then call FUNCTION with the buffer whose name matched
3015 REGEXP as first, and OTHER-ARGS as second argument. If
3016 `special-display-function' specifies some other function, that
3017 function is called with the buffer whose name matched REGEXP
3018 as first, and the element's cdr as second argument.
3019
3020 If this variable appears \"not to work\", because you added a
3021 name to it but the corresponding buffer is displayed in the
3022 selected window, look at the values of `same-window-buffer-names'
3023 and `same-window-regexps'. Those variables take precedence over
3024 this one.
3025
3026 See also `special-display-buffer-names'."
3027 :type '(repeat
3028 (choice :tag "Buffer"
3029 :value ""
3030 (regexp :format "%v")
3031 (cons :tag "With parameters"
3032 :format "%v"
3033 :value ("" . nil)
3034 (regexp :format "%v")
3035 (repeat :tag "Parameters"
3036 (cons :format "%v"
3037 (symbol :tag "Parameter")
3038 (sexp :tag "Value"))))
3039 (list :tag "With function"
3040 :format "%v"
3041 :value ("" . nil)
3042 (regexp :format "%v")
3043 (function :tag "Function")
3044 (repeat :tag "Arguments" (sexp)))))
3045 :group 'windows
3046 :group 'frames)
3047
3048 (defun special-display-p (buffer-name)
3049 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
3050 More precisely, return t if `special-display-buffer-names' or
3051 `special-display-regexps' contain a string entry equaling or
3052 matching BUFFER-NAME. If `special-display-buffer-names' or
3053 `special-display-regexps' contain a list entry whose car equals
3054 or matches BUFFER-NAME, the return value is the cdr of that
3055 entry."
3056 (let (tmp)
3057 (cond
3058 ((not (stringp buffer-name)))
3059 ((member buffer-name special-display-buffer-names)
3060 t)
3061 ((setq tmp (assoc buffer-name special-display-buffer-names))
3062 (cdr tmp))
3063 ((catch 'found
3064 (dolist (regexp special-display-regexps)
3065 (cond
3066 ((stringp regexp)
3067 (when (string-match-p regexp buffer-name)
3068 (throw 'found t)))
3069 ((and (consp regexp) (stringp (car regexp))
3070 (string-match-p (car regexp) buffer-name))
3071 (throw 'found (cdr regexp))))))))))
3072
3073 (defcustom special-display-function 'special-display-popup-frame
3074 "Function to call for displaying special buffers.
3075 This function is called with two arguments - the buffer and,
3076 optionally, a list - and should return a window displaying that
3077 buffer. The default value usually makes a separate frame for the
3078 buffer using `special-display-frame-alist' to specify the frame
3079 parameters. See the definition of `special-display-popup-frame'
3080 for how to specify such a function.
3081
3082 A buffer is special when its name is either listed in
3083 `special-display-buffer-names' or matches a regexp in
3084 `special-display-regexps'."
3085 :type 'function
3086 :group 'frames)
3087
3088 (defcustom same-window-buffer-names nil
3089 "List of names of buffers that should appear in the \"same\" window.
3090 `display-buffer' and `pop-to-buffer' show a buffer whose name is
3091 on this list in the selected rather than some other window.
3092
3093 An element of this list can be a cons cell instead of just a
3094 string. In that case, the cell's car must be a string specifying
3095 the buffer name. This is for compatibility with
3096 `special-display-buffer-names'; the cdr of the cons cell is
3097 ignored.
3098
3099 See also `same-window-regexps'."
3100 :type '(repeat (string :format "%v"))
3101 :group 'windows)
3102
3103 (defcustom same-window-regexps nil
3104 "List of regexps saying which buffers should appear in the \"same\" window.
3105 `display-buffer' and `pop-to-buffer' show a buffer whose name
3106 matches a regexp on this list in the selected rather than some
3107 other window.
3108
3109 An element of this list can be a cons cell instead of just a
3110 string. In that case, the cell's car must be a regexp matching
3111 the buffer name. This is for compatibility with
3112 `special-display-regexps'; the cdr of the cons cell is ignored.
3113
3114 See also `same-window-buffer-names'."
3115 :type '(repeat (regexp :format "%v"))
3116 :group 'windows)
3117
3118 (defun same-window-p (buffer-name)
3119 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
3120 This function returns non-nil if `display-buffer' or
3121 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
3122 selected rather than \(as usual\) some other window. See
3123 `same-window-buffer-names' and `same-window-regexps'."
3124 (cond
3125 ((not (stringp buffer-name)))
3126 ;; The elements of `same-window-buffer-names' can be buffer
3127 ;; names or cons cells whose cars are buffer names.
3128 ((member buffer-name same-window-buffer-names))
3129 ((assoc buffer-name same-window-buffer-names))
3130 ((catch 'found
3131 (dolist (regexp same-window-regexps)
3132 ;; The elements of `same-window-regexps' can be regexps
3133 ;; or cons cells whose cars are regexps.
3134 (when (or (and (stringp regexp)
3135 (string-match regexp buffer-name))
3136 (and (consp regexp) (stringp (car regexp))
3137 (string-match-p (car regexp) buffer-name)))
3138 (throw 'found t)))))))
3139
3140 (defcustom pop-up-frames nil
3141 "Whether `display-buffer' should make a separate frame.
3142 If nil, never make a separate frame.
3143 If the value is `graphic-only', make a separate frame
3144 on graphic displays only.
3145 Any other non-nil value means always make a separate frame."
3146 :type '(choice
3147 (const :tag "Never" nil)
3148 (const :tag "On graphic displays only" graphic-only)
3149 (const :tag "Always" t))
3150 :group 'windows)
3151
3152 (defcustom display-buffer-reuse-frames nil
3153 "Non-nil means `display-buffer' should reuse frames.
3154 If the buffer in question is already displayed in a frame, raise
3155 that frame."
3156 :type 'boolean
3157 :version "21.1"
3158 :group 'windows)
3159
3160 (defcustom pop-up-windows t
3161 "Non-nil means `display-buffer' should make a new window."
3162 :type 'boolean
3163 :group 'windows)
3164
3165 (defcustom split-window-preferred-function 'split-window-sensibly
3166 "Function called by `display-buffer' routines to split a window.
3167 This function is called with a window as single argument and is
3168 supposed to split that window and return the new window. If the
3169 window can (or shall) not be split, it is supposed to return nil.
3170 The default is to call the function `split-window-sensibly' which
3171 tries to split the window in a way which seems most suitable.
3172 You can customize the options `split-height-threshold' and/or
3173 `split-width-threshold' in order to have `split-window-sensibly'
3174 prefer either vertical or horizontal splitting.
3175
3176 If you set this to any other function, bear in mind that the
3177 `display-buffer' routines may call this function two times. The
3178 argument of the first call is the largest window on its frame.
3179 If that call fails to return a live window, the function is
3180 called again with the least recently used window as argument. If
3181 that call fails too, `display-buffer' will use an existing window
3182 to display its buffer.
3183
3184 The window selected at the time `display-buffer' was invoked is
3185 still selected when this function is called. Hence you can
3186 compare the window argument with the value of `selected-window'
3187 if you intend to split the selected window instead or if you do
3188 not want to split the selected window."
3189 :type 'function
3190 :version "23.1"
3191 :group 'windows)
3192
3193 (defcustom split-height-threshold 80
3194 "Minimum height for splitting windows sensibly.
3195 If this is an integer, `split-window-sensibly' may split a window
3196 vertically only if it has at least this many lines. If this is
3197 nil, `split-window-sensibly' is not allowed to split a window
3198 vertically. If, however, a window is the only window on its
3199 frame, `split-window-sensibly' may split it vertically
3200 disregarding the value of this variable."
3201 :type '(choice (const nil) (integer :tag "lines"))
3202 :version "23.1"
3203 :group 'windows)
3204
3205 (defcustom split-width-threshold 160
3206 "Minimum width for splitting windows sensibly.
3207 If this is an integer, `split-window-sensibly' may split a window
3208 horizontally only if it has at least this many columns. If this
3209 is nil, `split-window-sensibly' is not allowed to split a window
3210 horizontally."
3211 :type '(choice (const nil) (integer :tag "columns"))
3212 :version "23.1"
3213 :group 'windows)
3214
3215 (defun window-splittable-p (window &optional horizontal)
3216 "Return non-nil if `split-window-sensibly' may split WINDOW.
3217 Optional argument HORIZONTAL nil or omitted means check whether
3218 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
3219 non-nil means check whether WINDOW may be split horizontally.
3220
3221 WINDOW may be split vertically when the following conditions
3222 hold:
3223 - `window-size-fixed' is either nil or equals `width' for the
3224 buffer of WINDOW.
3225 - `split-height-threshold' is an integer and WINDOW is at least as
3226 high as `split-height-threshold'.
3227 - When WINDOW is split evenly, the emanating windows are at least
3228 `window-min-height' lines tall and can accommodate at least one
3229 line plus - if WINDOW has one - a mode line.
3230
3231 WINDOW may be split horizontally when the following conditions
3232 hold:
3233 - `window-size-fixed' is either nil or equals `height' for the
3234 buffer of WINDOW.
3235 - `split-width-threshold' is an integer and WINDOW is at least as
3236 wide as `split-width-threshold'.
3237 - When WINDOW is split evenly, the emanating windows are at least
3238 `window-min-width' or two (whichever is larger) columns wide."
3239 (when (window-live-p window)
3240 (with-current-buffer (window-buffer window)
3241 (if horizontal
3242 ;; A window can be split horizontally when its width is not
3243 ;; fixed, it is at least `split-width-threshold' columns wide
3244 ;; and at least twice as wide as `window-min-width' and 2 (the
3245 ;; latter value is hardcoded).
3246 (and (memq window-size-fixed '(nil height))
3247 ;; Testing `window-full-width-p' here hardly makes any
3248 ;; sense nowadays. This can be done more intuitively by
3249 ;; setting up `split-width-threshold' appropriately.
3250 (numberp split-width-threshold)
3251 (>= (window-width window)
3252 (max split-width-threshold
3253 (* 2 (max window-min-width 2)))))
3254 ;; A window can be split vertically when its height is not
3255 ;; fixed, it is at least `split-height-threshold' lines high,
3256 ;; and it is at least twice as high as `window-min-height' and 2
3257 ;; if it has a modeline or 1.
3258 (and (memq window-size-fixed '(nil width))
3259 (numberp split-height-threshold)
3260 (>= (window-height window)
3261 (max split-height-threshold
3262 (* 2 (max window-min-height
3263 (if mode-line-format 2 1))))))))))
3264
3265 (defun split-window-sensibly (window)
3266 "Split WINDOW in a way suitable for `display-buffer'.
3267 If `split-height-threshold' specifies an integer, WINDOW is at
3268 least `split-height-threshold' lines tall and can be split
3269 vertically, split WINDOW into two windows one above the other and
3270 return the lower window. Otherwise, if `split-width-threshold'
3271 specifies an integer, WINDOW is at least `split-width-threshold'
3272 columns wide and can be split horizontally, split WINDOW into two
3273 windows side by side and return the window on the right. If this
3274 can't be done either and WINDOW is the only window on its frame,
3275 try to split WINDOW vertically disregarding any value specified
3276 by `split-height-threshold'. If that succeeds, return the lower
3277 window. Return nil otherwise.
3278
3279 By default `display-buffer' routines call this function to split
3280 the largest or least recently used window. To change the default
3281 customize the option `split-window-preferred-function'.
3282
3283 You can enforce this function to not split WINDOW horizontally,
3284 by setting \(or binding) the variable `split-width-threshold' to
3285 nil. If, in addition, you set `split-height-threshold' to zero,
3286 chances increase that this function does split WINDOW vertically.
3287
3288 In order to not split WINDOW vertically, set \(or bind) the
3289 variable `split-height-threshold' to nil. Additionally, you can
3290 set `split-width-threshold' to zero to make a horizontal split
3291 more likely to occur.
3292
3293 Have a look at the function `window-splittable-p' if you want to
3294 know how `split-window-sensibly' determines whether WINDOW can be
3295 split."
3296 (or (and (window-splittable-p window)
3297 ;; Split window vertically.
3298 (with-selected-window window
3299 (split-window-vertically)))
3300 (and (window-splittable-p window t)
3301 ;; Split window horizontally.
3302 (with-selected-window window
3303 (split-window-horizontally)))
3304 (and (eq window (frame-root-window (window-frame window)))
3305 (not (window-minibuffer-p window))
3306 ;; If WINDOW is the only window on its frame and is not the
3307 ;; minibuffer window, try to split it vertically disregarding
3308 ;; the value of `split-height-threshold'.
3309 (let ((split-height-threshold 0))
3310 (when (window-splittable-p window)
3311 (with-selected-window window
3312 (split-window-vertically)))))))
3313
3314 (defun window--try-to-split-window (window)
3315 "Try to split WINDOW.
3316 Return value returned by `split-window-preferred-function' if it
3317 represents a live window, nil otherwise."
3318 (and (window-live-p window)
3319 (not (frame-parameter (window-frame window) 'unsplittable))
3320 (let ((new-window
3321 ;; Since `split-window-preferred-function' might
3322 ;; throw an error use `condition-case'.
3323 (condition-case nil
3324 (funcall split-window-preferred-function window)
3325 (error nil))))
3326 (and (window-live-p new-window) new-window))))
3327
3328 (defun window--frame-usable-p (frame)
3329 "Return FRAME if it can be used to display a buffer."
3330 (when (frame-live-p frame)
3331 (let ((window (frame-root-window frame)))
3332 ;; `frame-root-window' may be an internal window which is considered
3333 ;; "dead" by `window-live-p'. Hence if `window' is not live we
3334 ;; implicitly know that `frame' has a visible window we can use.
3335 (unless (and (window-live-p window)
3336 (or (window-minibuffer-p window)
3337 ;; If the window is soft-dedicated, the frame is usable.
3338 ;; Actually, even if the window is really dedicated,
3339 ;; the frame is still usable by splitting it.
3340 ;; At least Emacs-22 allowed it, and it is desirable
3341 ;; when displaying same-frame windows.
3342 nil ; (eq t (window-dedicated-p window))
3343 ))
3344 frame))))
3345
3346 (defcustom even-window-heights t
3347 "If non-nil `display-buffer' will try to even window heights.
3348 Otherwise `display-buffer' will leave the window configuration
3349 alone. Heights are evened only when `display-buffer' chooses a
3350 window that appears above or below the selected window."
3351 :type 'boolean
3352 :group 'windows)
3353
3354 (defun window--even-window-heights (window)
3355 "Even heights of WINDOW and selected window.
3356 Do this only if these windows are vertically adjacent to each
3357 other, `even-window-heights' is non-nil, and the selected window
3358 is higher than WINDOW."
3359 (when (and even-window-heights
3360 (not (eq window (selected-window)))
3361 ;; Don't resize minibuffer windows.
3362 (not (window-minibuffer-p (selected-window)))
3363 (> (window-height (selected-window)) (window-height window))
3364 (eq (window-frame window) (window-frame (selected-window)))
3365 (let ((sel-edges (window-edges (selected-window)))
3366 (win-edges (window-edges window)))
3367 (and (= (nth 0 sel-edges) (nth 0 win-edges))
3368 (= (nth 2 sel-edges) (nth 2 win-edges))
3369 (or (= (nth 1 sel-edges) (nth 3 win-edges))
3370 (= (nth 3 sel-edges) (nth 1 win-edges))))))
3371 (let ((window-min-height 1))
3372 ;; Don't throw an error if we can't even window heights for
3373 ;; whatever reason.
3374 (condition-case nil
3375 (enlarge-window (/ (- (window-height window) (window-height)) 2))
3376 (error nil)))))
3377
3378 (defun window--display-buffer-1 (window)
3379 "Raise the frame containing WINDOW.
3380 Do not raise the selected frame. Return WINDOW."
3381 (let* ((frame (window-frame window))
3382 (visible (frame-visible-p frame)))
3383 (unless (or (not visible)
3384 ;; Assume the selected frame is already visible enough.
3385 (eq frame (selected-frame))
3386 ;; Assume the frame from which we invoked the minibuffer
3387 ;; is visible.
3388 (and (minibuffer-window-active-p (selected-window))
3389 (eq frame (window-frame (minibuffer-selected-window)))))
3390 (raise-frame frame))
3391 window))
3392
3393 (defun window--display-buffer-2 (buffer window &optional dedicated)
3394 "Display BUFFER in WINDOW and make its frame visible.
3395 Set `window-dedicated-p' to DEDICATED if non-nil.
3396 Return WINDOW."
3397 (when (and (buffer-live-p buffer) (window-live-p window))
3398 (set-window-buffer window buffer)
3399 (when dedicated
3400 (set-window-dedicated-p window dedicated))
3401 (window--display-buffer-1 window)))
3402
3403 (defvar display-buffer-mark-dedicated nil
3404 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
3405 The actual non-nil value of this variable will be copied to the
3406 `window-dedicated-p' flag.")
3407
3408 (defun display-buffer (buffer-or-name &optional not-this-window frame)
3409 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
3410 BUFFER-OR-NAME must be a buffer or the name of an existing
3411 buffer. Return the window chosen to display BUFFER-OR-NAME or
3412 nil if no such window is found.
3413
3414 Optional argument NOT-THIS-WINDOW non-nil means display the
3415 buffer in a window other than the selected one, even if it is
3416 already displayed in the selected window.
3417
3418 Optional argument FRAME specifies which frames to investigate
3419 when the specified buffer is already displayed. If the buffer is
3420 already displayed in some window on one of these frames simply
3421 return that window. Possible values of FRAME are:
3422
3423 `visible' - consider windows on all visible frames on the current
3424 terminal.
3425
3426 0 - consider windows on all visible or iconified frames on the
3427 current terminal.
3428
3429 t - consider windows on all frames.
3430
3431 A specific frame - consider windows on that frame only.
3432
3433 nil - consider windows on the selected frame \(actually the
3434 last non-minibuffer frame\) only. If, however, either
3435 `display-buffer-reuse-frames' or `pop-up-frames' is non-nil
3436 \(non-nil and not graphic-only on a text-only terminal),
3437 consider all visible or iconified frames on the current terminal."
3438 (interactive "BDisplay buffer:\nP")
3439 (let* ((can-use-selected-window
3440 ;; The selected window is usable unless either NOT-THIS-WINDOW
3441 ;; is non-nil, it is dedicated to its buffer, or it is the
3442 ;; `minibuffer-window'.
3443 (not (or not-this-window
3444 (window-dedicated-p (selected-window))
3445 (window-minibuffer-p))))
3446 (buffer (if (bufferp buffer-or-name)
3447 buffer-or-name
3448 (get-buffer buffer-or-name)))
3449 (name-of-buffer (buffer-name buffer))
3450 ;; On text-only terminals do not pop up a new frame when
3451 ;; `pop-up-frames' equals graphic-only.
3452 (use-pop-up-frames (if (eq pop-up-frames 'graphic-only)
3453 (display-graphic-p)
3454 pop-up-frames))
3455 ;; `frame-to-use' is the frame where to show `buffer' - either
3456 ;; the selected frame or the last nonminibuffer frame.
3457 (frame-to-use
3458 (or (window--frame-usable-p (selected-frame))
3459 (window--frame-usable-p (last-nonminibuffer-frame))))
3460 ;; `window-to-use' is the window we use for showing `buffer'.
3461 window-to-use)
3462 (cond
3463 ((not (buffer-live-p buffer))
3464 (error "No such buffer %s" buffer))
3465 (display-buffer-function
3466 ;; Let `display-buffer-function' do the job.
3467 (funcall display-buffer-function buffer not-this-window))
3468 ((and (not not-this-window)
3469 (eq (window-buffer (selected-window)) buffer))
3470 ;; The selected window already displays BUFFER and
3471 ;; `not-this-window' is nil, so use it.
3472 (window--display-buffer-1 (selected-window)))
3473 ((and can-use-selected-window (same-window-p name-of-buffer))
3474 ;; If the buffer's name tells us to use the selected window do so.
3475 (window--display-buffer-2 buffer (selected-window)))
3476 ((let ((frames (or frame
3477 (and (or use-pop-up-frames
3478 display-buffer-reuse-frames
3479 (not (last-nonminibuffer-frame)))
3480 0)
3481 (last-nonminibuffer-frame))))
3482 (setq window-to-use
3483 (catch 'found
3484 ;; Search frames for a window displaying BUFFER. Return
3485 ;; the selected window only if we are allowed to do so.
3486 (dolist (window (get-buffer-window-list buffer 'nomini frames))
3487 (when (or can-use-selected-window
3488 (not (eq (selected-window) window)))
3489 (throw 'found window))))))
3490 ;; The buffer is already displayed in some window; use that.
3491 (window--display-buffer-1 window-to-use))
3492 ((and special-display-function
3493 ;; `special-display-p' returns either t or a list of frame
3494 ;; parameters to pass to `special-display-function'.
3495 (let ((pars (special-display-p name-of-buffer)))
3496 (when pars
3497 (funcall special-display-function
3498 buffer (if (listp pars) pars))))))
3499 ((or use-pop-up-frames (not frame-to-use))
3500 ;; We want or need a new frame.
3501 (let ((win (frame-selected-window (funcall pop-up-frame-function))))
3502 (window--display-buffer-2 buffer win display-buffer-mark-dedicated)))
3503 ((and pop-up-windows
3504 ;; Make a new window.
3505 (or (not (frame-parameter frame-to-use 'unsplittable))
3506 ;; If the selected frame cannot be split look at
3507 ;; `last-nonminibuffer-frame'.
3508 (and (eq frame-to-use (selected-frame))
3509 (setq frame-to-use (last-nonminibuffer-frame))
3510 (window--frame-usable-p frame-to-use)
3511 (not (frame-parameter frame-to-use 'unsplittable))))
3512 ;; Attempt to split largest or least recently used window.
3513 (setq window-to-use
3514 (or (window--try-to-split-window
3515 (get-largest-window frame-to-use t))
3516 (window--try-to-split-window
3517 (get-lru-window frame-to-use t)))))
3518 (window--display-buffer-2 buffer window-to-use
3519 display-buffer-mark-dedicated))
3520 ((let ((window-to-undedicate
3521 ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate
3522 ;; the selected window to its buffer, to avoid that some of
3523 ;; the `get-' routines below choose it. (Bug#1415)
3524 (and not-this-window (not (window-dedicated-p))
3525 (set-window-dedicated-p (selected-window) t)
3526 (selected-window))))
3527 (unwind-protect
3528 (setq window-to-use
3529 ;; Reuse an existing window.
3530 (or (get-lru-window frame-to-use)
3531 (let ((window (get-buffer-window buffer 'visible)))
3532 (unless (and not-this-window
3533 (eq window (selected-window)))
3534 window))
3535 (get-largest-window 'visible)
3536 (let ((window (get-buffer-window buffer 0)))
3537 (unless (and not-this-window
3538 (eq window (selected-window)))
3539 window))
3540 (get-largest-window 0)
3541 (frame-selected-window (funcall pop-up-frame-function))))
3542 (when (window-live-p window-to-undedicate)
3543 ;; Restore dedicated status of selected window.
3544 (set-window-dedicated-p window-to-undedicate nil))))
3545 (window--even-window-heights window-to-use)
3546 (window--display-buffer-2 buffer window-to-use)))))
3547
3548 (defun pop-to-buffer (buffer-or-name &optional other-window norecord)
3549 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
3550 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
3551 nil. If BUFFER-OR-NAME is a string not naming an existent
3552 buffer, create a buffer with that name. If BUFFER-OR-NAME is
3553 nil, choose some other buffer.
3554
3555 If `pop-up-windows' is non-nil, windows can be split to display
3556 the buffer. If optional second arg OTHER-WINDOW is non-nil,
3557 insist on finding another window even if the specified buffer is
3558 already visible in the selected window, and ignore
3559 `same-window-regexps' and `same-window-buffer-names'.
3560
3561 If the window to show BUFFER-OR-NAME is not on the selected
3562 frame, raise that window's frame and give it input focus.
3563
3564 This function returns the buffer it switched to. This uses the
3565 function `display-buffer' as a subroutine; see the documentation
3566 of `display-buffer' for additional customization information.
3567
3568 Optional third arg NORECORD non-nil means do not put this buffer
3569 at the front of the list of recently selected ones."
3570 (let ((buffer
3571 ;; FIXME: This behavior is carried over from the previous C version
3572 ;; of pop-to-buffer, but really we should use just
3573 ;; `get-buffer' here.
3574 (if (null buffer-or-name) (other-buffer (current-buffer))
3575 (or (get-buffer buffer-or-name)
3576 (let ((buf (get-buffer-create buffer-or-name)))
3577 (set-buffer-major-mode buf)
3578 buf))))
3579 (old-frame (selected-frame))
3580 new-window new-frame)
3581 (set-buffer buffer)
3582 (setq new-window (display-buffer buffer other-window))
3583 (select-window new-window norecord)
3584 (setq new-frame (window-frame new-window))
3585 (unless (eq new-frame old-frame)
3586 ;; `display-buffer' has chosen another frame, make sure it gets
3587 ;; input focus and is risen.
3588 (select-frame-set-input-focus new-frame))
3589 buffer))
3590 \f
3591 (defun set-window-text-height (window height)
3592 "Set the height in lines of the text display area of WINDOW to HEIGHT.
3593 HEIGHT doesn't include the mode line or header line, if any, or
3594 any partial-height lines in the text display area.
3595
3596 Note that the current implementation of this function cannot
3597 always set the height exactly, but attempts to be conservative,
3598 by allocating more lines than are actually needed in the case
3599 where some error may be present."
3600 (let ((delta (- height (window-text-height window))))
3601 (unless (zerop delta)
3602 ;; Setting window-min-height to a value like 1 can lead to very
3603 ;; bizarre displays because it also allows Emacs to make *other*
3604 ;; windows 1-line tall, which means that there's no more space for
3605 ;; the modeline.
3606 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
3607 (if (and window (not (eq window (selected-window))))
3608 (save-selected-window
3609 (select-window window 'norecord)
3610 (enlarge-window delta))
3611 (enlarge-window delta))))))
3612
3613 \f
3614 (defun enlarge-window-horizontally (columns)
3615 "Make selected window COLUMNS wider.
3616 Interactively, if no argument is given, make selected window one
3617 column wider."
3618 (interactive "p")
3619 (enlarge-window columns t))
3620
3621 (defun shrink-window-horizontally (columns)
3622 "Make selected window COLUMNS narrower.
3623 Interactively, if no argument is given, make selected window one
3624 column narrower."
3625 (interactive "p")
3626 (shrink-window columns t))
3627
3628 (defun window-buffer-height (window)
3629 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
3630 (with-current-buffer (window-buffer window)
3631 (max 1
3632 (count-screen-lines (point-min) (point-max)
3633 ;; If buffer ends with a newline, ignore it when
3634 ;; counting height unless point is after it.
3635 (eobp)
3636 window))))
3637
3638 (defun count-screen-lines (&optional beg end count-final-newline window)
3639 "Return the number of screen lines in the region.
3640 The number of screen lines may be different from the number of actual lines,
3641 due to line breaking, display table, etc.
3642
3643 Optional arguments BEG and END default to `point-min' and `point-max'
3644 respectively.
3645
3646 If region ends with a newline, ignore it unless optional third argument
3647 COUNT-FINAL-NEWLINE is non-nil.
3648
3649 The optional fourth argument WINDOW specifies the window used for obtaining
3650 parameters such as width, horizontal scrolling, and so on. The default is
3651 to use the selected window's parameters.
3652
3653 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
3654 regardless of which buffer is displayed in WINDOW. This makes possible to use
3655 `count-screen-lines' in any buffer, whether or not it is currently displayed
3656 in some window."
3657 (unless beg
3658 (setq beg (point-min)))
3659 (unless end
3660 (setq end (point-max)))
3661 (if (= beg end)
3662 0
3663 (save-excursion
3664 (save-restriction
3665 (widen)
3666 (narrow-to-region (min beg end)
3667 (if (and (not count-final-newline)
3668 (= ?\n (char-before (max beg end))))
3669 (1- (max beg end))
3670 (max beg end)))
3671 (goto-char (point-min))
3672 (1+ (vertical-motion (buffer-size) window))))))
3673
3674 (defun fit-window-to-buffer (&optional window max-height min-height)
3675 "Adjust height of WINDOW to display its buffer's contents exactly.
3676 WINDOW defaults to the selected window.
3677 Optional argument MAX-HEIGHT specifies the maximum height of the
3678 window and defaults to the maximum permissible height of a window
3679 on WINDOW's frame.
3680 Optional argument MIN-HEIGHT specifies the minimum height of the
3681 window and defaults to `window-min-height'.
3682 Both, MAX-HEIGHT and MIN-HEIGHT are specified in lines and
3683 include the mode line and header line, if any.
3684
3685 Return non-nil if height was orderly adjusted, nil otherwise.
3686
3687 Caution: This function can delete WINDOW and/or other windows
3688 when their height shrinks to less than MIN-HEIGHT."
3689 (interactive)
3690 ;; Do all the work in WINDOW and its buffer and restore the selected
3691 ;; window and the current buffer when we're done.
3692 (let ((old-buffer (current-buffer))
3693 value)
3694 (with-selected-window (or window (setq window (selected-window)))
3695 (set-buffer (window-buffer))
3696 ;; Use `condition-case' to handle any fixed-size windows and other
3697 ;; pitfalls nearby.
3698 (condition-case nil
3699 (let* (;; MIN-HEIGHT must not be less than 1 and defaults to
3700 ;; `window-min-height'.
3701 (min-height (max (or min-height window-min-height) 1))
3702 (max-window-height
3703 ;; Maximum height of any window on this frame.
3704 (min (window-height (frame-root-window)) (frame-height)))
3705 ;; MAX-HEIGHT must not be larger than max-window-height and
3706 ;; defaults to max-window-height.
3707 (max-height
3708 (min (or max-height max-window-height) max-window-height))
3709 (desired-height
3710 ;; The height necessary to show all of WINDOW's buffer,
3711 ;; constrained by MIN-HEIGHT and MAX-HEIGHT.
3712 (max
3713 (min
3714 ;; For an empty buffer `count-screen-lines' returns zero.
3715 ;; Even in that case we need one line for the cursor.
3716 (+ (max (count-screen-lines) 1)
3717 ;; For non-minibuffers count the mode line, if any.
3718 (if (and (not (window-minibuffer-p)) mode-line-format)
3719 1 0)
3720 ;; Count the header line, if any.
3721 (if header-line-format 1 0))
3722 max-height)
3723 min-height))
3724 (delta
3725 ;; How much the window height has to change.
3726 (if (= (window-height) (window-height (frame-root-window)))
3727 ;; Don't try to resize a full-height window.
3728 0
3729 (- desired-height (window-height))))
3730 ;; Do something reasonable so `enlarge-window' can make
3731 ;; windows as small as MIN-HEIGHT.
3732 (window-min-height (min min-height window-min-height)))
3733 ;; Don't try to redisplay with the cursor at the end on its
3734 ;; own line--that would force a scroll and spoil things.
3735 (when (and (eobp) (bolp) (not (bobp)))
3736 (set-window-point window (1- (window-point))))
3737 ;; Adjust WINDOW's height to the nominally correct one
3738 ;; (which may actually be slightly off because of variable
3739 ;; height text, etc).
3740 (unless (zerop delta)
3741 (enlarge-window delta))
3742 ;; `enlarge-window' might have deleted WINDOW, so make sure
3743 ;; WINDOW's still alive for the remainder of this.
3744 ;; Note: Deleting WINDOW is clearly counter-intuitive in
3745 ;; this context, but we can't do much about it given the
3746 ;; current semantics of `enlarge-window'.
3747 (when (window-live-p window)
3748 ;; Check if the last line is surely fully visible. If
3749 ;; not, enlarge the window.
3750 (let ((end (save-excursion
3751 (goto-char (point-max))
3752 (when (and (bolp) (not (bobp)))
3753 ;; Don't include final newline.
3754 (backward-char 1))
3755 (when truncate-lines
3756 ;; If line-wrapping is turned off, test the
3757 ;; beginning of the last line for
3758 ;; visibility instead of the end, as the
3759 ;; end of the line could be invisible by
3760 ;; virtue of extending past the edge of the
3761 ;; window.
3762 (forward-line 0))
3763 (point))))
3764 (set-window-vscroll window 0)
3765 (while (and (< desired-height max-height)
3766 (= desired-height (window-height))
3767 (not (pos-visible-in-window-p end)))
3768 (enlarge-window 1)
3769 (setq desired-height (1+ desired-height))))
3770 ;; Return non-nil only if nothing "bad" happened.
3771 (setq value t)))
3772 (error nil)))
3773 (when (buffer-live-p old-buffer)
3774 (set-buffer old-buffer))
3775 value))
3776
3777 (defun window-safely-shrinkable-p (&optional window)
3778 "Return t if WINDOW can be shrunk without shrinking other windows.
3779 WINDOW defaults to the selected window."
3780 (with-selected-window (or window (selected-window))
3781 (let ((edges (window-edges)))
3782 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
3783 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
3784
3785 (defun shrink-window-if-larger-than-buffer (&optional window)
3786 "Shrink height of WINDOW if its buffer doesn't need so many lines.
3787 More precisely, shrink WINDOW vertically to be as small as
3788 possible, while still showing the full contents of its buffer.
3789 WINDOW defaults to the selected window.
3790
3791 Do not shrink to less than `window-min-height' lines. Do nothing
3792 if the buffer contains more lines than the present window height,
3793 or if some of the window's contents are scrolled out of view, or
3794 if shrinking this window would also shrink another window, or if
3795 the window is the only window of its frame.
3796
3797 Return non-nil if the window was shrunk, nil otherwise."
3798 (interactive)
3799 (when (null window)
3800 (setq window (selected-window)))
3801 (let* ((frame (window-frame window))
3802 (mini (frame-parameter frame 'minibuffer))
3803 (edges (window-edges window)))
3804 (if (and (not (eq window (frame-root-window frame)))
3805 (window-safely-shrinkable-p window)
3806 (pos-visible-in-window-p (point-min) window)
3807 (not (eq mini 'only))
3808 (or (not mini)
3809 (let ((mini-window (minibuffer-window frame)))
3810 (or (null mini-window)
3811 (not (eq frame (window-frame mini-window)))
3812 (< (nth 3 edges)
3813 (nth 1 (window-edges mini-window)))
3814 (> (nth 1 edges)
3815 (frame-parameter frame 'menu-bar-lines))))))
3816 (fit-window-to-buffer window (window-height window)))))
3817
3818 (defun kill-buffer-and-window ()
3819 "Kill the current buffer and delete the selected window."
3820 (interactive)
3821 (let ((window-to-delete (selected-window))
3822 (buffer-to-kill (current-buffer))
3823 (delete-window-hook (lambda ()
3824 (condition-case nil
3825 (delete-window)
3826 (error nil)))))
3827 (unwind-protect
3828 (progn
3829 (add-hook 'kill-buffer-hook delete-window-hook t t)
3830 (if (kill-buffer (current-buffer))
3831 ;; If `delete-window' failed before, we rerun it to regenerate
3832 ;; the error so it can be seen in the echo area.
3833 (when (eq (selected-window) window-to-delete)
3834 (delete-window))))
3835 ;; If the buffer is not dead for some reason (probably because
3836 ;; of a `quit' signal), remove the hook again.
3837 (condition-case nil
3838 (with-current-buffer buffer-to-kill
3839 (remove-hook 'kill-buffer-hook delete-window-hook t))
3840 (error nil)))))
3841
3842 (defun quit-window (&optional kill window)
3843 "Quit WINDOW and bury its buffer.
3844 With a prefix argument, kill the buffer instead. WINDOW defaults
3845 to the selected window.
3846
3847 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
3848 it and, if it's alone on its frame, its frame too. Otherwise, or
3849 if deleting WINDOW fails in any of the preceding cases, display
3850 another buffer in WINDOW using `switch-to-buffer'.
3851
3852 Optional argument KILL non-nil means kill WINDOW's buffer.
3853 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
3854 (interactive "P")
3855 (let ((buffer (window-buffer window)))
3856 (if (or window
3857 (window-minibuffer-p window)
3858 (window-dedicated-p window))
3859 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
3860 ;; try to delete it.
3861 (let* ((window (or window (selected-window)))
3862 (frame (window-frame window)))
3863 (if (eq window (frame-root-window frame))
3864 ;; WINDOW is alone on its frame. `delete-windows-on'
3865 ;; knows how to handle that case.
3866 (delete-windows-on buffer frame)
3867 ;; There are other windows on its frame, delete WINDOW.
3868 (delete-window window)))
3869 ;; Otherwise, switch to another buffer in the selected window.
3870 (switch-to-buffer nil))
3871
3872 ;; Deal with the buffer.
3873 (if kill
3874 (kill-buffer buffer)
3875 (bury-buffer buffer))))
3876
3877 \f
3878 (defvar recenter-last-op nil
3879 "Indicates the last recenter operation performed.
3880 Possible values: `top', `middle', `bottom', integer or float numbers.")
3881
3882 (defcustom recenter-positions '(middle top bottom)
3883 "Cycling order for `recenter-top-bottom'.
3884 A list of elements with possible values `top', `middle', `bottom',
3885 integer or float numbers that define the cycling order for
3886 the command `recenter-top-bottom'.
3887
3888 Top and bottom destinations are `scroll-margin' lines the from true
3889 window top and bottom. Middle redraws the frame and centers point
3890 vertically within the window. Integer number moves current line to
3891 the specified absolute window-line. Float number between 0.0 and 1.0
3892 means the percentage of the screen space from the top. The default
3893 cycling order is middle -> top -> bottom."
3894 :type '(repeat (choice
3895 (const :tag "Top" top)
3896 (const :tag "Middle" middle)
3897 (const :tag "Bottom" bottom)
3898 (integer :tag "Line number")
3899 (float :tag "Percentage")))
3900 :version "23.2"
3901 :group 'windows)
3902
3903 (defun recenter-top-bottom (&optional arg)
3904 "Move current buffer line to the specified window line.
3905 With no prefix argument, successive calls place point according
3906 to the cycling order defined by `recenter-positions'.
3907
3908 A prefix argument is handled like `recenter':
3909 With numeric prefix ARG, move current line to window-line ARG.
3910 With plain `C-u', move current line to window center."
3911 (interactive "P")
3912 (cond
3913 (arg (recenter arg)) ; Always respect ARG.
3914 (t
3915 (setq recenter-last-op
3916 (if (eq this-command last-command)
3917 (car (or (cdr (member recenter-last-op recenter-positions))
3918 recenter-positions))
3919 (car recenter-positions)))
3920 (let ((this-scroll-margin
3921 (min (max 0 scroll-margin)
3922 (truncate (/ (window-body-height) 4.0)))))
3923 (cond ((eq recenter-last-op 'middle)
3924 (recenter))
3925 ((eq recenter-last-op 'top)
3926 (recenter this-scroll-margin))
3927 ((eq recenter-last-op 'bottom)
3928 (recenter (- -1 this-scroll-margin)))
3929 ((integerp recenter-last-op)
3930 (recenter recenter-last-op))
3931 ((floatp recenter-last-op)
3932 (recenter (round (* recenter-last-op (window-height))))))))))
3933
3934 (define-key global-map [?\C-l] 'recenter-top-bottom)
3935
3936 (defun move-to-window-line-top-bottom (&optional arg)
3937 "Position point relative to window.
3938
3939 With a prefix argument ARG, acts like `move-to-window-line'.
3940
3941 With no argument, positions point at center of window.
3942 Successive calls position point at positions defined
3943 by `recenter-positions'."
3944 (interactive "P")
3945 (cond
3946 (arg (move-to-window-line arg)) ; Always respect ARG.
3947 (t
3948 (setq recenter-last-op
3949 (if (eq this-command last-command)
3950 (car (or (cdr (member recenter-last-op recenter-positions))
3951 recenter-positions))
3952 (car recenter-positions)))
3953 (let ((this-scroll-margin
3954 (min (max 0 scroll-margin)
3955 (truncate (/ (window-body-height) 4.0)))))
3956 (cond ((eq recenter-last-op 'middle)
3957 (call-interactively 'move-to-window-line))
3958 ((eq recenter-last-op 'top)
3959 (move-to-window-line this-scroll-margin))
3960 ((eq recenter-last-op 'bottom)
3961 (move-to-window-line (- -1 this-scroll-margin)))
3962 ((integerp recenter-last-op)
3963 (move-to-window-line recenter-last-op))
3964 ((floatp recenter-last-op)
3965 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
3966
3967 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
3968
3969 \f
3970 ;;; Scrolling commands.
3971
3972 ;;; Scrolling commands which does not signal errors at top/bottom
3973 ;;; of buffer at first key-press (instead moves to top/bottom
3974 ;;; of buffer).
3975
3976 (defcustom scroll-error-top-bottom nil
3977 "Move point to top/bottom of buffer before signalling a scrolling error.
3978 A value of nil means just signal an error if no more scrolling possible.
3979 A value of t means point moves to the beginning or the end of the buffer
3980 \(depending on scrolling direction) when no more scrolling possible.
3981 When point is already on that position, then signal an error."
3982 :type 'boolean
3983 :group 'scrolling
3984 :version "24.1")
3985
3986 (defun scroll-up-command (&optional arg)
3987 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
3988 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
3989 scroll window further, move cursor to the bottom line.
3990 When point is already on that position, then signal an error.
3991 A near full screen is `next-screen-context-lines' less than a full screen.
3992 Negative ARG means scroll downward.
3993 If ARG is the atom `-', scroll downward by nearly full screen."
3994 (interactive "^P")
3995 (cond
3996 ((null scroll-error-top-bottom)
3997 (scroll-up arg))
3998 ((eq arg '-)
3999 (scroll-down-command nil))
4000 ((< (prefix-numeric-value arg) 0)
4001 (scroll-down-command (- (prefix-numeric-value arg))))
4002 ((eobp)
4003 (scroll-up arg)) ; signal error
4004 (t
4005 (condition-case nil
4006 (scroll-up arg)
4007 (end-of-buffer
4008 (if arg
4009 ;; When scrolling by ARG lines can't be done,
4010 ;; move by ARG lines instead.
4011 (forward-line arg)
4012 ;; When ARG is nil for full-screen scrolling,
4013 ;; move to the bottom of the buffer.
4014 (goto-char (point-max))))))))
4015
4016 (put 'scroll-up-command 'scroll-command t)
4017
4018 (defun scroll-down-command (&optional arg)
4019 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
4020 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
4021 scroll window further, move cursor to the top line.
4022 When point is already on that position, then signal an error.
4023 A near full screen is `next-screen-context-lines' less than a full screen.
4024 Negative ARG means scroll upward.
4025 If ARG is the atom `-', scroll upward by nearly full screen."
4026 (interactive "^P")
4027 (cond
4028 ((null scroll-error-top-bottom)
4029 (scroll-down arg))
4030 ((eq arg '-)
4031 (scroll-up-command nil))
4032 ((< (prefix-numeric-value arg) 0)
4033 (scroll-up-command (- (prefix-numeric-value arg))))
4034 ((bobp)
4035 (scroll-down arg)) ; signal error
4036 (t
4037 (condition-case nil
4038 (scroll-down arg)
4039 (beginning-of-buffer
4040 (if arg
4041 ;; When scrolling by ARG lines can't be done,
4042 ;; move by ARG lines instead.
4043 (forward-line (- arg))
4044 ;; When ARG is nil for full-screen scrolling,
4045 ;; move to the top of the buffer.
4046 (goto-char (point-min))))))))
4047
4048 (put 'scroll-down-command 'scroll-command t)
4049
4050 ;;; Scrolling commands which scroll a line instead of full screen.
4051
4052 (defun scroll-up-line (&optional arg)
4053 "Scroll text of selected window upward ARG lines; or one line if no ARG.
4054 If ARG is omitted or nil, scroll upward by one line.
4055 This is different from `scroll-up-command' that scrolls a full screen."
4056 (interactive "p")
4057 (scroll-up (or arg 1)))
4058
4059 (put 'scroll-up-line 'scroll-command t)
4060
4061 (defun scroll-down-line (&optional arg)
4062 "Scroll text of selected window down ARG lines; or one line if no ARG.
4063 If ARG is omitted or nil, scroll down by one line.
4064 This is different from `scroll-down-command' that scrolls a full screen."
4065 (interactive "p")
4066 (scroll-down (or arg 1)))
4067
4068 (put 'scroll-down-line 'scroll-command t)
4069
4070 \f
4071 (defun scroll-other-window-down (lines)
4072 "Scroll the \"other window\" down.
4073 For more details, see the documentation for `scroll-other-window'."
4074 (interactive "P")
4075 (scroll-other-window
4076 ;; Just invert the argument's meaning.
4077 ;; We can do that without knowing which window it will be.
4078 (if (eq lines '-) nil
4079 (if (null lines) '-
4080 (- (prefix-numeric-value lines))))))
4081
4082 (defun beginning-of-buffer-other-window (arg)
4083 "Move point to the beginning of the buffer in the other window.
4084 Leave mark at previous position.
4085 With arg N, put point N/10 of the way from the true beginning."
4086 (interactive "P")
4087 (let ((orig-window (selected-window))
4088 (window (other-window-for-scrolling)))
4089 ;; We use unwind-protect rather than save-window-excursion
4090 ;; because the latter would preserve the things we want to change.
4091 (unwind-protect
4092 (progn
4093 (select-window window)
4094 ;; Set point and mark in that window's buffer.
4095 (with-no-warnings
4096 (beginning-of-buffer arg))
4097 ;; Set point accordingly.
4098 (recenter '(t)))
4099 (select-window orig-window))))
4100
4101 (defun end-of-buffer-other-window (arg)
4102 "Move point to the end of the buffer in the other window.
4103 Leave mark at previous position.
4104 With arg N, put point N/10 of the way from the true end."
4105 (interactive "P")
4106 ;; See beginning-of-buffer-other-window for comments.
4107 (let ((orig-window (selected-window))
4108 (window (other-window-for-scrolling)))
4109 (unwind-protect
4110 (progn
4111 (select-window window)
4112 (with-no-warnings
4113 (end-of-buffer arg))
4114 (recenter '(t)))
4115 (select-window orig-window))))
4116
4117 \f
4118 (defvar mouse-autoselect-window-timer nil
4119 "Timer used by delayed window autoselection.")
4120
4121 (defvar mouse-autoselect-window-position nil
4122 "Last mouse position recorded by delayed window autoselection.")
4123
4124 (defvar mouse-autoselect-window-window nil
4125 "Last window recorded by delayed window autoselection.")
4126
4127 (defvar mouse-autoselect-window-state nil
4128 "When non-nil, special state of delayed window autoselection.
4129 Possible values are `suspend' \(suspend autoselection after a menu or
4130 scrollbar interaction\) and `select' \(the next invocation of
4131 'handle-select-window' shall select the window immediately\).")
4132
4133 (defun mouse-autoselect-window-cancel (&optional force)
4134 "Cancel delayed window autoselection.
4135 Optional argument FORCE means cancel unconditionally."
4136 (unless (and (not force)
4137 ;; Don't cancel for select-window or select-frame events
4138 ;; or when the user drags a scroll bar.
4139 (or (memq this-command
4140 '(handle-select-window handle-switch-frame))
4141 (and (eq this-command 'scroll-bar-toolkit-scroll)
4142 (memq (nth 4 (event-end last-input-event))
4143 '(handle end-scroll)))))
4144 (setq mouse-autoselect-window-state nil)
4145 (when (timerp mouse-autoselect-window-timer)
4146 (cancel-timer mouse-autoselect-window-timer))
4147 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
4148
4149 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
4150 "Start delayed window autoselection.
4151 MOUSE-POSITION is the last position where the mouse was seen as returned
4152 by `mouse-position'. Optional argument WINDOW non-nil denotes the
4153 window where the mouse was seen. Optional argument SUSPEND non-nil
4154 means suspend autoselection."
4155 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
4156 (setq mouse-autoselect-window-position mouse-position)
4157 (when window (setq mouse-autoselect-window-window window))
4158 (setq mouse-autoselect-window-state (when suspend 'suspend))
4159 ;; Install timer which runs `mouse-autoselect-window-select' after
4160 ;; `mouse-autoselect-window' seconds.
4161 (setq mouse-autoselect-window-timer
4162 (run-at-time
4163 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
4164
4165 (defun mouse-autoselect-window-select ()
4166 "Select window with delayed window autoselection.
4167 If the mouse position has stabilized in a non-selected window, select
4168 that window. The minibuffer window is selected only if the minibuffer is
4169 active. This function is run by `mouse-autoselect-window-timer'."
4170 (condition-case nil
4171 (let* ((mouse-position (mouse-position))
4172 (window
4173 (condition-case nil
4174 (window-at (cadr mouse-position) (cddr mouse-position)
4175 (car mouse-position))
4176 (error nil))))
4177 (cond
4178 ((or (menu-or-popup-active-p)
4179 (and window
4180 (not (coordinates-in-window-p (cdr mouse-position) window))))
4181 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
4182 ;; of WINDOW, temporarily suspend delayed autoselection.
4183 (mouse-autoselect-window-start mouse-position nil t))
4184 ((eq mouse-autoselect-window-state 'suspend)
4185 ;; Delayed autoselection was temporarily suspended, reenable it.
4186 (mouse-autoselect-window-start mouse-position))
4187 ((and window (not (eq window (selected-window)))
4188 (or (not (numberp mouse-autoselect-window))
4189 (and (> mouse-autoselect-window 0)
4190 ;; If `mouse-autoselect-window' is positive, select
4191 ;; window if the window is the same as before.
4192 (eq window mouse-autoselect-window-window))
4193 ;; Otherwise select window if the mouse is at the same
4194 ;; position as before. Observe that the first test after
4195 ;; starting autoselection usually fails since the value of
4196 ;; `mouse-autoselect-window-position' recorded there is the
4197 ;; position where the mouse has entered the new window and
4198 ;; not necessarily where the mouse has stopped moving.
4199 (equal mouse-position mouse-autoselect-window-position))
4200 ;; The minibuffer is a candidate window if it's active.
4201 (or (not (window-minibuffer-p window))
4202 (eq window (active-minibuffer-window))))
4203 ;; Mouse position has stabilized in non-selected window: Cancel
4204 ;; delayed autoselection and try to select that window.
4205 (mouse-autoselect-window-cancel t)
4206 ;; Select window where mouse appears unless the selected window is the
4207 ;; minibuffer. Use `unread-command-events' in order to execute pre-
4208 ;; and post-command hooks and trigger idle timers. To avoid delaying
4209 ;; autoselection again, set `mouse-autoselect-window-state'."
4210 (unless (window-minibuffer-p (selected-window))
4211 (setq mouse-autoselect-window-state 'select)
4212 (setq unread-command-events
4213 (cons (list 'select-window (list window))
4214 unread-command-events))))
4215 ((or (and window (eq window (selected-window)))
4216 (not (numberp mouse-autoselect-window))
4217 (equal mouse-position mouse-autoselect-window-position))
4218 ;; Mouse position has either stabilized in the selected window or at
4219 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
4220 (mouse-autoselect-window-cancel t))
4221 (t
4222 ;; Mouse position has not stabilized yet, resume delayed
4223 ;; autoselection.
4224 (mouse-autoselect-window-start mouse-position window))))
4225 (error nil)))
4226
4227 (defun handle-select-window (event)
4228 "Handle select-window events."
4229 (interactive "e")
4230 (let ((window (posn-window (event-start event))))
4231 (unless (or (not (window-live-p window))
4232 ;; Don't switch if we're currently in the minibuffer.
4233 ;; This tries to work around problems where the
4234 ;; minibuffer gets unselected unexpectedly, and where
4235 ;; you then have to move your mouse all the way down to
4236 ;; the minibuffer to select it.
4237 (window-minibuffer-p (selected-window))
4238 ;; Don't switch to minibuffer window unless it's active.
4239 (and (window-minibuffer-p window)
4240 (not (minibuffer-window-active-p window)))
4241 ;; Don't switch when autoselection shall be delayed.
4242 (and (numberp mouse-autoselect-window)
4243 (not (zerop mouse-autoselect-window))
4244 (not (eq mouse-autoselect-window-state 'select))
4245 (progn
4246 ;; Cancel any delayed autoselection.
4247 (mouse-autoselect-window-cancel t)
4248 ;; Start delayed autoselection from current mouse
4249 ;; position and window.
4250 (mouse-autoselect-window-start (mouse-position) window)
4251 ;; Executing a command cancels delayed autoselection.
4252 (add-hook
4253 'pre-command-hook 'mouse-autoselect-window-cancel))))
4254 (when mouse-autoselect-window
4255 ;; Reset state of delayed autoselection.
4256 (setq mouse-autoselect-window-state nil)
4257 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
4258 (run-hooks 'mouse-leave-buffer-hook))
4259 (select-window window))))
4260
4261 (defun delete-other-windows-vertically (&optional window)
4262 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4263 This may be a useful alternative binding for \\[delete-other-windows]
4264 if you often split windows horizontally."
4265 (interactive)
4266 (let* ((window (or window (selected-window)))
4267 (edges (window-edges window))
4268 (w window) delenda)
4269 (while (not (eq (setq w (next-window w 1)) window))
4270 (let ((e (window-edges w)))
4271 (when (and (= (car e) (car edges))
4272 (= (caddr e) (caddr edges)))
4273 (push w delenda))))
4274 (mapc 'delete-window delenda)))
4275
4276 (defun truncated-partial-width-window-p (&optional window)
4277 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
4278 WINDOW defaults to the selected window.
4279 Return nil if WINDOW is not a partial-width window
4280 (regardless of the value of `truncate-lines').
4281 Otherwise, consult the value of `truncate-partial-width-windows'
4282 for the buffer shown in WINDOW."
4283 (unless window
4284 (setq window (selected-window)))
4285 (unless (window-full-width-p window)
4286 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
4287 (window-buffer window))))
4288 (if (integerp t-p-w-w)
4289 (< (window-width window) t-p-w-w)
4290 t-p-w-w))))
4291 \f
4292 (define-key ctl-x-map "0" 'delete-window)
4293 (define-key ctl-x-map "1" 'delete-other-windows)
4294 (define-key ctl-x-map "2" 'split-window-above-each-other)
4295 (define-key ctl-x-map "3" 'split-window-side-by-side)
4296 (define-key ctl-x-map "^" 'enlarge-window)
4297 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
4298 (define-key ctl-x-map "{" 'shrink-window-horizontally)
4299 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
4300 (define-key ctl-x-map "+" 'balance-windows)
4301 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
4302
4303 ;;; window.el ends here