Add entry to NEWS
[emacs.git] / lisp / window.el
blob245ad23f4147e1179cfa935a5f36f0bd7d158c2a
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2011
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Code:
27 (eval-when-compile (require 'cl))
29 (defmacro save-selected-window (&rest body)
30 "Execute BODY, then select the previously selected window.
31 The value returned is the value of the last form in BODY.
33 This macro saves and restores the selected window, as well as the
34 selected window in each frame. If the previously selected window
35 is no longer live, then whatever window is selected at the end of
36 BODY remains selected. If the previously selected window of some
37 frame is no longer live at the end of BODY, that frame's selected
38 window is left alone.
40 This macro saves and restores the current buffer, since otherwise
41 its normal operation could make a different buffer current. The
42 order of recently selected windows and the buffer list ordering
43 are not altered by this macro (unless they are altered in BODY)."
44 (declare (indent 0) (debug t))
45 `(let ((save-selected-window-window (selected-window))
46 ;; It is necessary to save all of these, because calling
47 ;; select-window changes frame-selected-window for whatever
48 ;; frame that window is in.
49 (save-selected-window-alist
50 (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
51 (frame-list))))
52 (save-current-buffer
53 (unwind-protect
54 (progn ,@body)
55 (dolist (elt save-selected-window-alist)
56 (and (frame-live-p (car elt))
57 (window-live-p (cdr elt))
58 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
59 (when (window-live-p save-selected-window-window)
60 (select-window save-selected-window-window 'norecord))))))
62 ;; The following two functions are like `window-next' and `window-prev'
63 ;; but the WINDOW argument is _not_ optional (so they don't substitute
64 ;; the selected window for nil), and they return nil when WINDOW doesn't
65 ;; have a parent (like a frame's root window or a minibuffer window).
66 (defsubst window-right (window)
67 "Return WINDOW's right sibling.
68 Return nil if WINDOW is the root window of its frame. WINDOW can
69 be any window."
70 (and window (window-parent window) (window-next window)))
72 (defsubst window-left (window)
73 "Return WINDOW's left sibling.
74 Return nil if WINDOW is the root window of its frame. WINDOW can
75 be any window."
76 (and window (window-parent window) (window-prev window)))
78 (defsubst window-child (window)
79 "Return WINDOW's first child window."
80 (or (window-vchild window) (window-hchild window)))
82 (defun window-child-count (window)
83 "Return number of WINDOW's child windows."
84 (let ((count 0))
85 (when (and (windowp window) (setq window (window-child window)))
86 (while window
87 (setq count (1+ count))
88 (setq window (window-next window))))
89 count))
91 (defun window-last-child (window)
92 "Return last child window of WINDOW."
93 (when (and (windowp window) (setq window (window-child window)))
94 (while (window-next window)
95 (setq window (window-next window))))
96 window)
98 (defsubst window-any-p (object)
99 "Return t if OBJECT denotes a live or internal window."
100 (and (windowp object)
101 (or (window-buffer object) (window-child object))
104 ;; The following four functions should probably go to subr.el.
105 (defsubst normalize-live-buffer (buffer-or-name)
106 "Return buffer specified by BUFFER-OR-NAME.
107 BUFFER-OR-NAME must be either a buffer or a string naming a live
108 buffer and defaults to the current buffer."
109 (cond
110 ((not buffer-or-name)
111 (current-buffer))
112 ((bufferp buffer-or-name)
113 (if (buffer-live-p buffer-or-name)
114 buffer-or-name
115 (error "Buffer %s is not a live buffer" buffer-or-name)))
116 ((get-buffer buffer-or-name))
118 (error "No such buffer %s" buffer-or-name))))
120 (defsubst normalize-live-frame (frame)
121 "Return frame specified by FRAME.
122 FRAME must be a live frame and defaults to the selected frame."
123 (if frame
124 (if (frame-live-p frame)
125 frame
126 (error "%s is not a live frame" frame))
127 (selected-frame)))
129 (defsubst normalize-any-window (window)
130 "Return window specified by WINDOW.
131 WINDOW must be a window that has not been deleted and defaults to
132 the selected window."
133 (if window
134 (if (window-any-p window)
135 window
136 (error "%s is not a window" window))
137 (selected-window)))
139 (defsubst normalize-live-window (window)
140 "Return live window specified by WINDOW.
141 WINDOW must be a live window and defaults to the selected one."
142 (if window
143 (if (and (windowp window) (window-buffer window))
144 window
145 (error "%s is not a live window" window))
146 (selected-window)))
148 (defvar ignore-window-parameters nil
149 "If non-nil, standard functions ignore window parameters.
150 The functions currently affected by this are `split-window',
151 `delete-window', `delete-other-windows' and `other-window'.
153 An application may bind this to a non-nil value around calls to
154 these functions to inhibit processing of window parameters.")
156 (defconst window-safe-min-height 1
157 "The absolut minimum number of lines of a window.
158 Anything less might crash Emacs.")
160 (defcustom window-min-height 4
161 "The minimum number of lines of any window.
162 The value has to accomodate a mode- or header-line if present. A
163 value less than `window-safe-min-height' is ignored. The value
164 of this variable is honored when windows are resized or split.
166 Applications should never rebind this variable. To resize a
167 window to a height less than the one specified here, an
168 application should instead call `resize-window' with a non-nil
169 IGNORE argument. In order to have `split-window' make a window
170 shorter, explictly specify the SIZE argument of that function."
171 :type 'integer
172 :version "24.1"
173 :group 'windows)
175 (defconst window-safe-min-width 2
176 "The absolut minimum number of columns of a window.
177 Anything less might crash Emacs.")
179 (defcustom window-min-width 10
180 "The minimum number of columns of any window.
181 The value has to accomodate margins, fringes, or scrollbars if
182 present. A value less than `window-safe-min-width' is ignored.
183 The value of this variable is honored when windows are resized or
184 split.
186 Applications should never rebind this variable. To resize a
187 window to a width less than the one specified here, an
188 application should instead call `resize-window' with a non-nil
189 IGNORE argument. In order to have `split-window' make a window
190 narrower, explictly specify the SIZE argument of that function."
191 :type 'integer
192 :version "24.1"
193 :group 'windows)
195 (defun window-iso-combination-p (&optional window horizontal)
196 "If WINDOW is a vertical combination return WINDOW's first child.
197 WINDOW can be any window and defaults to the selected one.
198 Optional argument HORIZONTAL non-nil means return WINDOW's first
199 child if WINDOW is a horizontal combination."
200 (setq window (normalize-any-window window))
201 (if horizontal
202 (window-hchild window)
203 (window-vchild window)))
205 (defsubst window-iso-combined-p (&optional window horizontal)
206 "Return non-nil if and only if WINDOW is vertically combined.
207 WINDOW can be any window and defaults to the selected one.
208 Optional argument HORIZONTAL non-nil means return non-nil if and
209 only if WINDOW is horizontally combined."
210 (setq window (normalize-any-window window))
211 (let ((parent (window-parent window)))
212 (and parent (window-iso-combination-p parent horizontal))))
214 (defun window-iso-combinations (&optional window horizontal)
215 "Return largest number of vertically arranged subwindows of WINDOW.
216 WINDOW can be any window and defaults to the selected one.
217 Optional argument HORIZONTAL non-nil means to return the largest
218 number of horizontally arranged subwindows of WINDOW."
219 (setq window (normalize-any-window window))
220 (cond
221 ((window-live-p window)
222 ;; If WINDOW is live, return 1.
224 ((window-iso-combination-p window horizontal)
225 ;; If WINDOW is iso-combined, return the sum of the values for all
226 ;; subwindows of WINDOW.
227 (let ((child (window-child window))
228 (count 0))
229 (while child
230 (setq count
231 (+ (window-iso-combinations child horizontal)
232 count))
233 (setq child (window-right child)))
234 count))
236 ;; If WINDOW is not iso-combined, return the maximum value of any
237 ;; subwindow of WINDOW.
238 (let ((child (window-child window))
239 (count 1))
240 (while child
241 (setq count
242 (max (window-iso-combinations child horizontal)
243 count))
244 (setq child (window-right child)))
245 count))))
247 (defun walk-window-tree-1 (proc walk-window-tree-window any &optional sub-only)
248 "Helper function for `walk-window-tree' and `walk-window-subtree'."
249 (let (walk-window-tree-buffer)
250 (while walk-window-tree-window
251 (setq walk-window-tree-buffer
252 (window-buffer walk-window-tree-window))
253 (when (or walk-window-tree-buffer any)
254 (funcall proc walk-window-tree-window))
255 (unless walk-window-tree-buffer
256 (walk-window-tree-1
257 proc (window-hchild walk-window-tree-window) any)
258 (walk-window-tree-1
259 proc (window-vchild walk-window-tree-window) any))
260 (if sub-only
261 (setq walk-window-tree-window nil)
262 (setq walk-window-tree-window
263 (window-right walk-window-tree-window))))))
265 (defun walk-window-tree (proc &optional frame any)
266 "Run function PROC on each live window of FRAME.
267 PROC must be a function with one argument - a window. FRAME must
268 be a live frame and defaults to the selected one. ANY, if
269 non-nil means to run PROC on all live and internal windows of
270 FRAME.
272 This function performs a pre-order, depth-first traversal of the
273 window tree. If PROC changes the window tree, the result is
274 unpredictable."
275 (let ((walk-window-tree-frame (normalize-live-frame frame)))
276 (walk-window-tree-1
277 proc (frame-root-window walk-window-tree-frame) any)))
279 (defun walk-window-subtree (proc &optional window any)
280 "Run function PROC on each live subwindow of WINDOW.
281 WINDOW defaults to the selected window. PROC must be a function
282 with one argument - a window. ANY, if non-nil means to run PROC
283 on all live and internal subwindows of WINDOW.
285 This function performs a pre-order, depth-first traversal of the
286 window tree rooted at WINDOW. If PROC changes that window tree,
287 the result is unpredictable."
288 (setq window (normalize-any-window window))
289 (walk-window-tree-1 proc window any t))
291 (defun windows-with-parameter (parameter &optional value frame any values)
292 "Return a list of all windows on FRAME with PARAMETER non-nil.
293 FRAME defaults to the selected frame. Optional argument VALUE
294 non-nil means only return windows whose window-parameter value of
295 PARAMETER equals VALUE \(comparison is done using `equal').
296 Optional argument ANY non-nil means consider internal windows
297 too. Optional argument VALUES non-nil means return a list of cons
298 cells whose car is the value of the parameter and whose cdr is
299 the window."
300 (let (this-value windows)
301 (walk-window-tree
302 (lambda (window)
303 (when (and (setq this-value (window-parameter window parameter))
304 (or (not value) (or (equal value this-value))))
305 (setq windows
306 (if values
307 (cons (cons this-value window) windows)
308 (cons window windows)))))
309 frame any)
311 (nreverse windows)))
313 (defun window-with-parameter (parameter &optional value frame any)
314 "Return first window on FRAME with PARAMETER non-nil.
315 FRAME defaults to the selected frame. Optional argument VALUE
316 non-nil means only return a window whose window-parameter value
317 for PARAMETER equals VALUE \(comparison is done with `equal').
318 Optional argument ANY non-nil means consider internal windows
319 too."
320 (let (this-value windows)
321 (catch 'found
322 (walk-window-tree
323 (lambda (window)
324 (when (and (setq this-value (window-parameter window parameter))
325 (or (not value) (equal value this-value)))
326 (throw 'found window)))
327 frame any))))
329 ;;; Atomic windows.
330 (defun window-atom-root (&optional window)
331 "Return root of atomic window WINDOW is a part of.
332 WINDOW can be any window and defaults to the selected one.
333 Return nil if WINDOW is not part of a atomic window."
334 (setq window (normalize-any-window window))
335 (let (root)
336 (while (and window (window-parameter window 'window-atom))
337 (setq root window)
338 (setq window (window-parent window)))
339 root))
341 (defun make-window-atom (window)
342 "Make WINDOW an atomic window.
343 WINDOW must be an internal window. Return WINDOW."
344 (if (not (window-child window))
345 (error "Window %s is not an internal window" window)
346 (walk-window-subtree
347 (lambda (window)
348 (set-window-parameter window 'window-atom t))
349 window t)
350 window))
352 (defun window-atom-check-1 (window)
353 "Subroutine of `window-atom-check'."
354 (when window
355 (if (window-parameter window 'window-atom)
356 (let ((count 0))
357 (when (or (catch 'reset
358 (walk-window-subtree
359 (lambda (window)
360 (if (window-parameter window 'window-atom)
361 (setq count (1+ count))
362 (throw 'reset t)))
363 window t))
364 ;; count >= 1 must hold here. If there's no other
365 ;; window around dissovle this atomic window.
366 (= count 1))
367 ;; Dissolve atomic window.
368 (walk-window-subtree
369 (lambda (window)
370 (set-window-parameter window 'window-atom nil))
371 window t)))
372 ;; Check children.
373 (unless (window-buffer window)
374 (window-atom-check-1 (window-hchild window))
375 (window-atom-check-1 (window-vchild window))))
376 ;; Check right sibling
377 (window-atom-check-1 (window-right window))))
379 (defun window-atom-check (&optional frame)
380 "Check atomicity of all windows on FRAME.
381 FRAME defaults to the selected frame. If an atomic window is
382 wrongly configured, reset the atomicity of all its subwindows to
383 nil. An atomic window is wrongly configured if it has no
384 subwindows or one of its subwindows is not atomic."
385 (window-atom-check-1 (frame-root-window frame)))
387 ;; Side windows.
388 (defvar window-sides '(left top right bottom)
389 "Window sides.")
391 (defcustom window-sides-vertical nil
392 "If non-nil, left and right side windows are full height.
393 Otherwise, top and bottom side windows are full width."
394 :type 'boolean
395 :group 'windows
396 :version "24.1")
398 (defcustom window-sides-slots '(nil nil nil nil)
399 "Maximum number of side window slots.
400 The value is a list of four elements specifying the number of
401 side window slots on \(in this order) the left, top, right and
402 bottom side of each frame. If an element is a number, this means
403 to display at most that many side windows on the corresponding
404 side. If an element is nil, this means there's no bound on the
405 number of slots on that side."
406 :risky t
407 :type
408 '(list
409 :value (nil nil nil nil)
410 (choice
411 :tag "Left"
412 :help-echo "Maximum slots of left side window."
413 :value nil
414 :format "%[Left%] %v\n"
415 (const :tag "Unlimited" :format "%t" nil)
416 (integer :tag "Number" :value 2 :size 5))
417 (choice
418 :tag "Top"
419 :help-echo "Maximum slots of top side window."
420 :value nil
421 :format "%[Top%] %v\n"
422 (const :tag "Unlimited" :format "%t" nil)
423 (integer :tag "Number" :value 3 :size 5))
424 (choice
425 :tag "Right"
426 :help-echo "Maximum slots of right side window."
427 :value nil
428 :format "%[Right%] %v\n"
429 (const :tag "Unlimited" :format "%t" nil)
430 (integer :tag "Number" :value 2 :size 5))
431 (choice
432 :tag "Bottom"
433 :help-echo "Maximum slots of bottom side window."
434 :value nil
435 :format "%[Bottom%] %v\n"
436 (const :tag "Unlimited" :format "%t" nil)
437 (integer :tag "Number" :value 3 :size 5)))
438 :group 'windows
439 :group 'display-buffer)
441 (defcustom window-sides-alist nil
442 "Association list for side windows.
443 Each entry of this list must be a list whose first element is
444 either `left', `top', `right' or`bottom'. The second element of
445 each entry specifies the minimum size of windows on that side
446 \(either the default value, a number of lines or columns, or a
447 fraction of the frame size). The third element specifies the
448 desired size of windows on that side \(either the default value,
449 a number of lines or columns, or a fraction of the frame size).
450 The fourth element specifies the upper bound on the number of
451 slots on that side, nil if there's no bound."
452 :risky t
453 :type
454 '(set
455 ;;; :format "%v %t"
456 :inline t
457 ;; Left side window.
458 (list
459 :value (left nil nil nil)
460 :format "Left: %v\n"
461 (const :format "" left)
462 ;; Minimum width of left side windows.
463 (choice
464 :tag "Minimum width"
465 :help-echo "The minimum width of left side windows."
466 :value nil
467 :format "%[Minimum%] %v"
468 (const :tag "Default" :format "%t" nil)
469 (integer :tag "Columns" :value 12 :size 5)
470 (float :tag "Fraction" :value .25 :size 5))
471 ;; Desired width of left side windows.
472 (choice
473 :tag "Desired width"
474 :help-echo "The desired width of left side windows."
475 :value nil
476 :format " %[Desired%] %v"
477 (const :tag "Default" :format "%t" nil)
478 (integer :tag "Columns" :value 12 :size 5)
479 (float :tag "Fraction" :value .25 :size 5))
480 ;; The maximum number of slots of left side windows.
481 (choice
482 :tag "Slots"
483 :help-echo "The maximum number of slots in left side windows."
484 :value nil
485 :format " %[Slots%] %v"
486 (const :tag "Unspecified" :format "%t" nil)
487 (integer :tag "Number" :format "%v" :value 3 :size 5)))
488 ;; Top side windows.
489 (list
490 :value (top nil nil nil)
491 :format "Top: %v\n"
492 (const :format "" top)
493 ;; Minimum height of top windows.
494 (choice
495 :tag "Minimum height"
496 :help-echo "The minimum height of top windows."
497 :value nil
498 :format "%[Minimum%] %v"
499 (const :tag "Default" :format "%t" nil)
500 (integer :tag "Lines" :value 6 :size 5)
501 (float :tag "Fraction" :value .25 :size 5))
502 ;; Desired size of left side windows.
503 (choice
504 :tag "Desired height"
505 :help-echo "The desired height of top windows."
506 :value nil
507 :format " %[Desired%] %v"
508 (const :tag "Default" :format "%t" nil)
509 (integer :tag "Lines" :value 6 :size 5)
510 (float :tag "Fraction" :value .25 :size 5))
511 ;; The maximum number of slots of top windows.
512 (choice
513 :tag "Slots"
514 :help-echo "The maximum number of slots in top windows."
515 :value nil
516 :format " %[Slots%] %v"
517 (const :tag "Unspecified" :format "%t" nil)
518 (integer :tag "Number" :value 3 :format "%v" :size 5)))
519 ;; Right side windows.
520 (list
521 :value (right nil nil nil)
522 :format "Right: %v\n"
523 (const :format "" right)
524 ;; Minimum width of right side windows.
525 (choice
526 :tag "Minimum width"
527 :help-echo "The minimum width of windows on the right."
528 :value nil
529 :format "%[Minimum%] %v"
530 (const :tag "Default" :format "%t" nil)
531 (integer :tag "Columns" :value 12 :size 5)
532 (float :tag "Fraction" :value .25 :size 5))
533 ;; Desired width of right side windows.
534 (choice
535 :tag "Desired width"
536 :help-echo "The desired width of windows on the left."
537 :value nil
538 :format " %[Desired%] %v"
539 (const :tag "Default" :format "%t" nil)
540 (integer :tag "Columns" :value 12 :size 5)
541 (float :tag "Fraction" :value .25 :size 5))
542 ;; The maximum number of slots of right side windows.
543 (choice
544 :tag "Slots"
545 :help-echo "The maximum number of slots in right side windows."
546 :value nil
547 :format " %[Slots%] %v"
548 (const :tag "Unspecified" :format "%t" nil)
549 (integer :tag "Number" :value 3 :format "%v" :size 5)))
550 ;; Bottom side windows.
551 (list
552 :value (bottom nil nil nil)
553 :format "Bottom: %v\n"
554 (const :format "" bottom)
555 ;; Minimum height of bottom windows.
556 (choice
557 :tag "Minimum height"
558 :help-echo "The minimum height of bottom windows."
559 :value nil
560 :format "%[Minimum%] %v"
561 (const :tag "Default" :format "%t" nil)
562 (integer :tag "Lines" :value 6 :size 5)
563 (float :tag "Fraction" :value .25 :size 5))
564 ;; Desired height of bottom windows.
565 (choice
566 :tag "Desired height"
567 :help-echo "The desired height of bottom windows."
568 :value nil
569 :format " %[Desired%] %v"
570 (const :tag "Default" :format "%t" nil)
571 (integer :tag "Lines" :value 6 :size 5)
572 (float :tag "Fraction" :value .25 :size 5))
573 ;; The maximum number of slots of bottom windows.
574 (choice
575 :tag "Slots"
576 :help-echo "The maximum number of slots in botom windows."
577 :value nil
578 :format " %[Slots%] %v"
579 (const :tag "Unspecified" :format "%t" nil)
580 (integer :tag "Number" :value 3 :format "%v" :size 5))))
581 :group 'windows
582 :group 'display-buffer)
584 (defun window-side-check (&optional frame)
585 "Check the window-side parameter of all windows on FRAME.
586 FRAME defaults to the selected frame. If the configuration is
587 invalid, reset all window-side parameters to nil.
589 A valid configuration has to preserve the following invariant:
591 - If a window has a non-nil window-side parameter, it must have a
592 parent window and the parent window's window-side parameter
593 must be either nil or the same as for window.
595 - If windows with non-nil window-side parameters exist, there
596 must be at most one window of each side and non-side with a
597 parent whose window-side parameter is nil and there must be no
598 leaf window whose window-side parameter is nil."
599 (let (normal none left top right bottom
600 side parent parent-side code)
601 (when (or (catch 'reset
602 (walk-window-tree
603 (lambda (window)
604 (setq side (window-parameter window 'window-side))
605 (setq parent (window-parent window))
606 (setq parent-side
607 (and parent (window-parameter parent 'window-side)))
608 ;; The following `cond' seems a bit tedious, but I'd
609 ;; rather stick to using just the stack.
610 (cond
611 (parent-side
612 (when (not (eq parent-side side))
613 ;; A parent whose window-side is non-nil must
614 ;; have a child with the same window-side.
615 (throw 'reset t)))
616 ;; Now check that there's more than one main window
617 ;; for any of none, left, top, right and bottom.
618 ((eq side 'none)
619 (if none
620 (throw 'reset t)
621 (setq none t)))
622 ((eq side 'left)
623 (if left
624 (throw 'reset t)
625 (setq left t)))
626 ((eq side 'top)
627 (if top
628 (throw 'reset t)
629 (setq top t)))
630 ((eq side 'right)
631 (if right
632 (throw 'reset t)
633 (setq right t)))
634 ((eq side 'bottom)
635 (if bottom
636 (throw 'reset t)
637 (setq bottom t)))
638 ((window-buffer window)
639 ;; A leaf window without window-side parameter,
640 ;; record its existence.
641 (setq normal t))))
642 frame t))
643 (if none
644 ;; At least one non-side window exists, so there must
645 ;; be at least one side-window and no normal window.
646 (or (not (or left top right bottom)) normal)
647 ;; No non-side window exists, so there must be no side
648 ;; window either.
649 (or left top right bottom)))
650 (walk-window-tree
651 (lambda (window)
652 (set-window-parameter window 'window-side nil))
653 frame t))))
655 (defun window-check (&optional frame)
656 "Check atomic and side windows on FRAME.
657 FRAME defaults to the selected frame."
658 (window-side-check frame)
659 (window-atom-check frame))
661 ;;; Window sizes.
662 (defvar window-size-fixed nil
663 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
664 If the value is `height', then only the window's height is fixed.
665 If the value is `width', then only the window's width is fixed.
666 Any other non-nil value fixes both the width and the height.
668 Emacs won't change the size of any window displaying that buffer,
669 unless it has no other choice \(like when deleting a neighboring
670 window).")
671 (make-variable-buffer-local 'window-size-fixed)
673 (defsubst window-size-ignore (window ignore)
674 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
675 (if (window-any-p ignore) (eq window ignore) ignore))
677 (defun window-min-size (&optional window horizontal ignore)
678 "Return the minimum number of lines of WINDOW.
679 WINDOW can be an arbitrary window and defaults to the selected
680 one. Optional argument HORIZONTAL non-nil means return the
681 minimum number of columns of WINDOW.
683 Optional argument IGNORE non-nil means ignore any restrictions
684 imposed by fixed size windows, `window-min-height' or
685 `window-min-width' settings. IGNORE equal `safe' means live
686 windows may get as small as `window-safe-min-height' lines and
687 `window-safe-min-width' columns. IGNORE a window means ignore
688 restrictions for that window only."
689 (window-min-size-1
690 (normalize-any-window window) horizontal ignore))
692 (defun window-min-size-1 (window horizontal ignore)
693 "Internal function of `window-min-size'."
694 (let ((sub (window-child window)))
695 (if sub
696 (let ((value 0))
697 ;; WINDOW is an internal window.
698 (if (window-iso-combined-p sub horizontal)
699 ;; The minimum size of an iso-combination is the sum of
700 ;; the minimum sizes of its subwindows.
701 (while sub
702 (setq value (+ value
703 (window-min-size-1 sub horizontal ignore)))
704 (setq sub (window-right sub)))
705 ;; The minimum size of an ortho-combination is the maximum of
706 ;; the minimum sizes of its subwindows.
707 (while sub
708 (setq value (max value
709 (window-min-size-1 sub horizontal ignore)))
710 (setq sub (window-right sub))))
711 value)
712 (with-current-buffer (window-buffer window)
713 (cond
714 ((and (not (window-size-ignore window ignore))
715 (window-size-fixed-p window horizontal))
716 ;; The minimum size of a fixed size window is its size.
717 (window-total-size window horizontal))
718 ((or (eq ignore 'safe) (eq ignore window))
719 ;; If IGNORE equals `safe' or WINDOW return the safe values.
720 (if horizontal window-safe-min-width window-safe-min-height))
721 (horizontal
722 ;; For the minimum width of a window take fringes and
723 ;; scroll-bars into account. This is questionable and should
724 ;; be removed as soon as we are able to split (and resize)
725 ;; windows such that the new (or resized) windows can get a
726 ;; size less than the user-specified `window-min-height' and
727 ;; `window-min-width'.
728 (let ((frame (window-frame window))
729 (fringes (window-fringes window))
730 (scroll-bars (window-scroll-bars window)))
731 (max
732 (+ window-safe-min-width
733 (ceiling (car fringes) (frame-char-width frame))
734 (ceiling (cadr fringes) (frame-char-width frame))
735 (cond
736 ((memq (nth 2 scroll-bars) '(left right))
737 (nth 1 scroll-bars))
738 ((memq (frame-parameter frame 'vertical-scroll-bars)
739 '(left right))
740 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
741 (frame-char-width)))
742 (t 0)))
743 (if (and (not (window-size-ignore window ignore))
744 (numberp window-min-width))
745 window-min-width
746 0))))
748 ;; For the minimum height of a window take any mode- or
749 ;; header-line into account.
750 (max (+ window-safe-min-height
751 (if header-line-format 1 0)
752 (if mode-line-format 1 0))
753 (if (and (not (window-size-ignore window ignore))
754 (numberp window-min-height))
755 window-min-height
756 0))))))))
758 (defun window-sizable (window delta &optional horizontal ignore)
759 "Return DELTA if DELTA lines can be added to WINDOW.
760 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
761 columns can be added to WINDOW. A return value of zero means
762 that no lines (or columns) can be added to WINDOW.
764 This function looks only at WINDOW and its subwindows. The
765 function `window-resizable' looks at other windows as well.
767 DELTA positive means WINDOW shall be enlarged by DELTA lines or
768 columns. If WINDOW cannot be enlarged by DELTA lines or columns
769 return the maximum value in the range 0..DELTA by which WINDOW
770 can be enlarged.
772 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
773 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
774 return the minimum value in the range DELTA..0 by which WINDOW
775 can be shrunk.
777 Optional argument IGNORE non-nil means ignore any restrictions
778 imposed by fixed size windows, `window-min-height' or
779 `window-min-width' settings. IGNORE equal `safe' means live
780 windows may get as small as `window-safe-min-height' lines and
781 `window-safe-min-width' columns. IGNORE any window means ignore
782 restrictions for that window only."
783 (setq window (normalize-any-window window))
784 (cond
785 ((< delta 0)
786 (max (- (window-min-size window horizontal ignore)
787 (window-total-size window horizontal))
788 delta))
789 ((window-size-ignore window ignore)
790 delta)
791 ((> delta 0)
792 (if (window-size-fixed-p window horizontal)
794 delta))
795 (t 0)))
797 (defsubst window-sizable-p (window delta &optional horizontal ignore)
798 "Return t if WINDOW can be resized by DELTA lines.
799 For the meaning of the arguments of this function see the
800 doc-string of `window-sizable'."
801 (setq window (normalize-any-window window))
802 (if (> delta 0)
803 (>= (window-sizable window delta horizontal ignore) delta)
804 (<= (window-sizable window delta horizontal ignore) delta)))
806 (defun window-size-fixed-1 (window horizontal)
807 "Internal function for `window-size-fixed-p'."
808 (let ((sub (window-child window)))
809 (catch 'fixed
810 (if sub
811 ;; WINDOW is an internal window.
812 (if (window-iso-combined-p sub horizontal)
813 ;; An iso-combination is fixed size if all its subwindows
814 ;; are fixed-size.
815 (progn
816 (while sub
817 (unless (window-size-fixed-1 sub horizontal)
818 ;; We found a non-fixed-size subwindow, so WINDOW's
819 ;; size is not fixed.
820 (throw 'fixed nil))
821 (setq sub (window-right sub)))
822 ;; All subwindows are fixed-size, so WINDOW's size is
823 ;; fixed.
824 (throw 'fixed t))
825 ;; An ortho-combination is fixed-size if at least one of its
826 ;; subwindows is fixed-size.
827 (while sub
828 (when (window-size-fixed-1 sub horizontal)
829 ;; We found a fixed-size subwindow, so WINDOW's size is
830 ;; fixed.
831 (throw 'fixed t))
832 (setq sub (window-right sub))))
833 ;; WINDOW is a live window.
834 (with-current-buffer (window-buffer window)
835 (if horizontal
836 (memq window-size-fixed '(width t))
837 (memq window-size-fixed '(height t))))))))
839 (defun window-size-fixed-p (&optional window horizontal)
840 "Return non-nil if WINDOW's height is fixed.
841 WINDOW can be an arbitrary window and defaults to the selected
842 window. Optional argument HORIZONTAL non-nil means return
843 non-nil if WINDOW's width is fixed.
845 If this function returns nil, this does not necessarily mean that
846 WINDOW can be resized in the desired direction. The functions
847 `window-resizable' and `window-resizable-p' will tell that."
848 (window-size-fixed-1
849 (normalize-any-window window) horizontal))
851 (defun window-min-delta-1 (window delta &optional horizontal ignore trail noup)
852 "Internal function for `window-min-delta'."
853 (if (not (window-parent window))
854 ;; If we can't go up, return zero.
856 ;; Else try to find a non-fixed-size sibling of WINDOW.
857 (let* ((parent (window-parent window))
858 (sub (window-child parent)))
859 (catch 'done
860 (if (window-iso-combined-p sub horizontal)
861 ;; In an iso-combination throw DELTA if we find at least one
862 ;; subwindow and that subwindow is either not of fixed-size
863 ;; or we can ignore fixed-sizeness.
864 (let ((skip (eq trail 'after)))
865 (while sub
866 (cond
867 ((eq sub window)
868 (setq skip (eq trail 'before)))
869 (skip)
870 ((and (not (window-size-ignore window ignore))
871 (window-size-fixed-p sub horizontal)))
873 ;; We found a non-fixed-size subwindow.
874 (throw 'done delta)))
875 (setq sub (window-right sub))))
876 ;; In an ortho-combination set DELTA to the minimum value by
877 ;; which other subwindows can shrink.
878 (while sub
879 (unless (eq sub window)
880 (setq delta
881 (min delta
882 (- (window-total-size sub horizontal)
883 (window-min-size sub horizontal ignore)))))
884 (setq sub (window-right sub))))
885 (if noup
886 delta
887 (window-min-delta-1 parent delta horizontal ignore trail))))))
889 (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
890 "Return number of lines by which WINDOW can be shrunk.
891 WINDOW can be an arbitrary window and defaults to the selected
892 window. Return zero if WINDOW cannot be shrunk.
894 Optional argument HORIZONTAL non-nil means return number of
895 columns by which WINDOW can be shrunk.
897 Optional argument IGNORE non-nil means ignore any restrictions
898 imposed by fixed size windows, `window-min-height' or
899 `window-min-width' settings. IGNORE a window means ignore
900 restrictions for that window only. IGNORE equal `safe' means
901 live windows may get as small as `window-safe-min-height' lines
902 and `window-safe-min-width' columns.
904 Optional argument TRAIL `before' means only windows to the left
905 of or above WINDOW can be enlarged. Optional argument TRAIL
906 `after' means only windows to the right of or below WINDOW can be
907 enlarged.
909 Optional argument NOUP non-nil means don't go up in the window
910 tree but try to enlarge windows within WINDOW's combination only.
912 Optional argument NODOWN non-nil means don't check whether WINDOW
913 itself \(and its subwindows) can be shrunk; check only whether at
914 least one other windows can be enlarged appropriately."
915 (setq window (normalize-any-window window))
916 (let ((size (window-total-size window horizontal))
917 (minimum (window-min-size window horizontal ignore)))
918 (cond
919 (nodown
920 ;; If NODOWN is t, try to recover the entire size of WINDOW.
921 (window-min-delta-1 window size horizontal ignore trail noup))
922 ((= size minimum)
923 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
924 ;; there's nothing to recover.
927 ;; Otherwise, try to recover whatever WINDOW is larger than its
928 ;; minimum size.
929 (window-min-delta-1
930 window (- size minimum) horizontal ignore trail noup)))))
932 (defun window-max-delta-1 (window delta &optional horizontal ignore trail noup)
933 "Internal function of `window-max-delta'."
934 (if (not (window-parent window))
935 ;; Can't go up. Return DELTA.
936 delta
937 (let* ((parent (window-parent window))
938 (sub (window-child parent)))
939 (catch 'fixed
940 (if (window-iso-combined-p sub horizontal)
941 ;; For an iso-combination calculate how much we can get from
942 ;; other subwindows.
943 (let ((skip (eq trail 'after)))
944 (while sub
945 (cond
946 ((eq sub window)
947 (setq skip (eq trail 'before)))
948 (skip)
950 (setq delta
951 (+ delta
952 (- (window-total-size sub horizontal)
953 (window-min-size sub horizontal ignore))))))
954 (setq sub (window-right sub))))
955 ;; For an ortho-combination throw DELTA when at least one
956 ;; subwindow is fixed-size.
957 (while sub
958 (when (and (not (eq sub window))
959 (not (window-size-ignore sub ignore))
960 (window-size-fixed-p sub horizontal))
961 (throw 'fixed delta))
962 (setq sub (window-right sub))))
963 (if noup
964 ;; When NOUP is nil, DELTA is all we can get.
965 delta
966 ;; Else try with parent of WINDOW, passing the DELTA we
967 ;; recovered so far.
968 (window-max-delta-1 parent delta horizontal ignore trail))))))
970 (defun window-max-delta (&optional window horizontal ignore trail noup nodown)
971 "Return maximum number of lines WINDOW by which WINDOW can be enlarged.
972 WINDOW can be an arbitrary window and defaults to the selected
973 window. The return value is zero if WINDOW cannot be enlarged.
975 Optional argument HORIZONTAL non-nil means return maximum number
976 of columns by which WINDOW can be enlarged.
978 Optional argument IGNORE non-nil means ignore any restrictions
979 imposed by fixed size windows, `window-min-height' or
980 `window-min-width' settings. IGNORE a window means ignore
981 restrictions for that window only. IGNORE equal `safe' means
982 live windows may get as small as `window-safe-min-height' lines
983 and `window-safe-min-width' columns.
985 Optional argument TRAIL `before' means only windows to the left
986 of or below WINDOW can be shrunk. Optional argument TRAIL
987 `after' means only windows to the right of or above WINDOW can be
988 shrunk.
990 Optional argument NOUP non-nil means don't go up in the window
991 tree but try to obtain the entire space from windows within
992 WINDOW's combination.
994 Optional argument NODOWN non-nil means do not check whether
995 WINDOW itself \(and its subwindows) can be enlarged; check only
996 whether other windows can be shrunk appropriately."
997 (setq window (normalize-any-window window))
998 (if (and (not (window-size-ignore window ignore))
999 (not nodown) (window-size-fixed-p window horizontal))
1000 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1001 ;; size.
1003 ;; WINDOW has no fixed size.
1004 (window-max-delta-1 window 0 horizontal ignore trail noup)))
1006 ;; Make NOUP also inhibit the min-size check.
1007 (defun window-resizable (window delta &optional horizontal ignore trail noup nodown)
1008 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1009 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1010 can be resized horizontally by DELTA columns. A return value of
1011 zero means that WINDOW is not resizable.
1013 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1014 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1015 return the maximum value in the range 0..DELTA by which WINDOW
1016 can be enlarged.
1018 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1019 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1020 return the minimum value in the range DELTA..0 that can be used
1021 for shrinking WINDOW.
1023 Optional argument IGNORE non-nil means ignore any restrictions
1024 imposed by fixed size windows, `window-min-height' or
1025 `window-min-width' settings. IGNORE a window means ignore
1026 restrictions for that window only. IGNORE equal `safe' means
1027 live windows may get as small as `window-safe-min-height' lines
1028 and `window-safe-min-width' columns.
1030 Optional argument TRAIL `before' means only windows to the left
1031 of or below WINDOW can be shrunk. Optional argument TRAIL
1032 `after' means only windows to the right of or above WINDOW can be
1033 shrunk.
1035 Optional argument NOUP non-nil means don't go up in the window
1036 tree but try to distribute the space among the other windows
1037 within WINDOW's combination.
1039 Optional argument NODOWN non-nil means don't check whether WINDOW
1040 and its subwindows can be resized."
1041 (setq window (normalize-any-window window))
1042 (cond
1043 ((< delta 0)
1044 (max (- (window-min-delta window horizontal ignore trail noup nodown))
1045 delta))
1046 ((> delta 0)
1047 (min (window-max-delta window horizontal ignore trail noup nodown)
1048 delta))
1049 (t 0)))
1051 (defun window-resizable-p (window delta &optional horizontal ignore trail noup nodown)
1052 "Return t if WINDOW can be resized vertically by DELTA lines.
1053 For the meaning of the arguments of this function see the
1054 doc-string of `window-resizable'."
1055 (setq window (normalize-any-window window))
1056 (if (> delta 0)
1057 (>= (window-resizable window delta horizontal ignore trail noup nodown)
1058 delta)
1059 (<= (window-resizable window delta horizontal ignore trail noup nodown)
1060 delta)))
1062 (defsubst window-total-height (&optional window)
1063 "Return the total number of lines of WINDOW.
1064 WINDOW can be any window and defaults to the selected one. The
1065 return value includes WINDOW's mode line and header line, if any.
1066 If WINDOW is internal the return value is the sum of the total
1067 number of lines of WINDOW's child windows if these are vertically
1068 combined and the height of WINDOW's first child otherwise.
1070 Note: This function does not take into account the value of
1071 `line-spacing' when calculating the number of lines in WINDOW."
1072 (window-total-size window))
1074 ;; Eventually we should make `window-height' obsolete.
1075 (defalias 'window-height 'window-total-height)
1077 ;; See discussion in bug#4543.
1078 (defsubst window-full-height-p (&optional window)
1079 "Return t if WINDOW is as high as the containing frame.
1080 More precisely, return t if and only if the total height of
1081 WINDOW equals the total height of the root window of WINDOW's
1082 frame. WINDOW can be any window and defaults to the selected
1083 one."
1084 (setq window (normalize-any-window window))
1085 (= (window-total-size window)
1086 (window-total-size (frame-root-window window))))
1088 (defsubst window-total-width (&optional window)
1089 "Return the total number of columns of WINDOW.
1090 WINDOW can be any window and defaults to the selected one. The
1091 return value includes any vertical dividers or scrollbars of
1092 WINDOW. If WINDOW is internal, the return value is the sum of
1093 the total number of columns of WINDOW's child windows if these
1094 are horizontally combined and the width of WINDOW's first child
1095 otherwise."
1096 (window-total-size window t))
1098 (defsubst window-full-width-p (&optional window)
1099 "Return t if WINDOW is as wide as the containing frame.
1100 More precisely, return t if and only if the total width of WINDOW
1101 equals the total width of the root window of WINDOW's frame.
1102 WINDOW can be any window and defaults to the selected one."
1103 (setq window (normalize-any-window window))
1104 (= (window-total-size window t)
1105 (window-total-size (frame-root-window window) t)))
1107 (defsubst window-body-height (&optional window)
1108 "Return the number of lines of WINDOW's body.
1109 WINDOW must be a live window and defaults to the selected one.
1111 The return value does not include WINDOW's mode line and header
1112 line, if any. If a line at the bottom of the window is only
1113 partially visible, that line is included in the return value. If
1114 you do not want to include a partially visible bottom line in the
1115 return value, use `window-text-height' instead."
1116 (window-body-size window))
1118 (defsubst window-body-width (&optional window)
1119 "Return the number of columns of WINDOW's body.
1120 WINDOW must be a live window and defaults to the selected one.
1122 The return value does not include any vertical dividers or scroll
1123 bars owned by WINDOW. On a window-system the return value does
1124 not include the number of columns used for WINDOW's fringes or
1125 display margins either."
1126 (window-body-size window t))
1128 ;; Eventually we should make `window-height' obsolete.
1129 (defalias 'window-width 'window-body-width)
1131 (defun window-current-scroll-bars (&optional window)
1132 "Return the current scroll bar settings for WINDOW.
1133 WINDOW must be a live window and defaults to the selected one.
1135 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1136 VERTICAL specifies the current location of the vertical scroll
1137 bars (`left', `right', or nil), and HORIZONTAL specifies the
1138 current location of the horizontal scroll bars (`top', `bottom',
1139 or nil).
1141 Unlike `window-scroll-bars', this function reports the scroll bar
1142 type actually used, once frame defaults and `scroll-bar-mode' are
1143 taken into account."
1144 (setq window (normalize-live-window window))
1145 (let ((vert (nth 2 (window-scroll-bars window)))
1146 (hor nil))
1147 (when (or (eq vert t) (eq hor t))
1148 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
1149 (if (eq vert t)
1150 (setq vert (car fcsb)))
1151 (if (eq hor t)
1152 (setq hor (cdr fcsb)))))
1153 (cons vert hor)))
1155 (defun walk-windows (proc &optional minibuf all-frames)
1156 "Cycle through all live windows, calling PROC for each one.
1157 PROC must specify a function with a window as its sole argument.
1158 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1159 windows to include in the walk.
1161 MINIBUF t means include the minibuffer window even if the
1162 minibuffer is not active. MINIBUF nil or omitted means include
1163 the minibuffer window only if the minibuffer is active. Any
1164 other value means do not include the minibuffer window even if
1165 the minibuffer is active.
1167 ALL-FRAMES nil or omitted means consider all windows on the
1168 selected frame, plus the minibuffer window if specified by the
1169 MINIBUF argument. If the minibuffer counts, consider all windows
1170 on all frames that share that minibuffer too. The following
1171 non-nil values of ALL-FRAMES have special meanings:
1173 - t means consider all windows on all existing frames.
1175 - `visible' means consider all windows on all visible frames on
1176 the current terminal.
1178 - 0 (the number zero) means consider all windows on all visible
1179 and iconified frames on the current terminal.
1181 - A frame means consider all windows on that frame only.
1183 Anything else means consider all windows on the selected frame
1184 and no others.
1186 This function changes neither the order of recently selected
1187 windows nor the buffer list."
1188 ;; If we start from the minibuffer window, don't fail to come
1189 ;; back to it.
1190 (when (window-minibuffer-p (selected-window))
1191 (setq minibuf t))
1192 ;; Make sure to not mess up the order of recently selected
1193 ;; windows. Use `save-selected-window' and `select-window'
1194 ;; with second argument non-nil for this purpose.
1195 (save-selected-window
1196 (when (framep all-frames)
1197 (select-window (frame-first-window all-frames) 'norecord))
1198 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1199 (funcall proc walk-windows-window))))
1201 (defun window-in-direction-2 (window posn &optional horizontal)
1202 "Support function for `window-in-direction'."
1203 (if horizontal
1204 (let ((top (window-top-line window)))
1205 (if (> top posn)
1206 (- top posn)
1207 (- posn top (window-total-height window))))
1208 (let ((left (window-left-column window)))
1209 (if (> left posn)
1210 (- left posn)
1211 (- posn left (window-total-width window))))))
1213 (defun window-in-direction (direction &optional window ignore)
1214 "Return window in DIRECTION as seen from WINDOW.
1215 DIRECTION must be one of `above', `below', `left' or `right'.
1216 WINDOW must be a live window and defaults to the selected one.
1217 IGNORE, when non-nil means a window can be returned even if its
1218 `no-other-window' parameter is non-nil."
1219 (setq window (normalize-live-window window))
1220 (unless (memq direction '(above below left right))
1221 (error "Wrong direction %s" direction))
1222 (let* ((frame (window-frame window))
1223 (hor (memq direction '(left right)))
1224 (first (if hor
1225 (window-left-column window)
1226 (window-top-line window)))
1227 (last (+ first (if hor
1228 (window-total-width window)
1229 (window-total-height window))))
1230 (posn-cons (nth 6 (posn-at-point (window-point window) window)))
1231 ;; The column / row value of `posn-at-point' can be nil for the
1232 ;; mini-window, guard against that.
1233 (posn (if hor
1234 (+ (or (cdr posn-cons) 1) (window-top-line window))
1235 (+ (or (car posn-cons) 1) (window-left-column window))))
1236 (best-edge
1237 (cond
1238 ((eq direction 'below) (frame-height frame))
1239 ((eq direction 'right) (frame-width frame))
1240 (t -1)))
1241 (best-edge-2 best-edge)
1242 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1243 best best-2 best-diff-2-new)
1244 (walk-window-tree
1245 (lambda (w)
1246 (let* ((w-top (window-top-line w))
1247 (w-left (window-left-column w)))
1248 (cond
1249 ((or (eq window w)
1250 ;; Ignore ourselves.
1251 (and (window-parameter w 'no-other-window)
1252 ;; Ignore W unless IGNORE is non-nil.
1253 (not ignore))))
1254 (hor
1255 (cond
1256 ((and (<= w-top posn)
1257 (< posn (+ w-top (window-total-height w))))
1258 ;; W is to the left or right of WINDOW and covers POSN.
1259 (when (or (and (eq direction 'left)
1260 (<= w-left first) (> w-left best-edge))
1261 (and (eq direction 'right)
1262 (>= w-left last) (< w-left best-edge)))
1263 (setq best-edge w-left)
1264 (setq best w)))
1265 ((and (or (and (eq direction 'left)
1266 (<= (+ w-left (window-total-width w)) first))
1267 (and (eq direction 'right) (<= last w-left)))
1268 ;; W is to the left or right of WINDOW but does not
1269 ;; cover POSN.
1270 (setq best-diff-2-new
1271 (window-in-direction-2 w posn hor))
1272 (or (< best-diff-2-new best-diff-2)
1273 (and (= best-diff-2-new best-diff-2)
1274 (if (eq direction 'left)
1275 (> w-left best-edge-2)
1276 (< w-left best-edge-2)))))
1277 (setq best-edge-2 w-left)
1278 (setq best-diff-2 best-diff-2-new)
1279 (setq best-2 w))))
1281 (cond
1282 ((and (<= w-left posn)
1283 (< posn (+ w-left (window-total-width w))))
1284 ;; W is above or below WINDOW and covers POSN.
1285 (when (or (and (eq direction 'above)
1286 (<= w-top first) (> w-top best-edge))
1287 (and (eq direction 'below)
1288 (>= w-top first) (< w-top best-edge)))
1289 (setq best-edge w-top)
1290 (setq best w)))
1291 ((and (or (and (eq direction 'above)
1292 (<= (+ w-top (window-total-height w)) first))
1293 (and (eq direction 'below) (<= last w-top)))
1294 ;; W is above or below WINDOW but does not cover POSN.
1295 (setq best-diff-2-new
1296 (window-in-direction-2 w posn hor))
1297 (or (< best-diff-2-new best-diff-2)
1298 (and (= best-diff-2-new best-diff-2)
1299 (if (eq direction 'above)
1300 (> w-top best-edge-2)
1301 (< w-top best-edge-2)))))
1302 (setq best-edge-2 w-top)
1303 (setq best-diff-2 best-diff-2-new)
1304 (setq best-2 w)))))))
1305 (window-frame window))
1306 (or best best-2)))
1308 (defun get-window-with-predicate (predicate &optional minibuf
1309 all-frames default)
1310 "Return a live window satisfying PREDICATE.
1311 More precisely, cycle through all windows calling the function
1312 PREDICATE on each one of them with the window as its sole
1313 argument. Return the first window for which PREDICATE returns
1314 non-nil. If no window satisfies PREDICATE, return DEFAULT.
1316 ALL-FRAMES nil or omitted means consider all windows on the selected
1317 frame, plus the minibuffer window if specified by the MINIBUF
1318 argument. If the minibuffer counts, consider all windows on all
1319 frames that share that minibuffer too. The following non-nil
1320 values of ALL-FRAMES have special meanings:
1322 - t means consider all windows on all existing frames.
1324 - `visible' means consider all windows on all visible frames on
1325 the current terminal.
1327 - 0 (the number zero) means consider all windows on all visible
1328 and iconified frames on the current terminal.
1330 - A frame means consider all windows on that frame only.
1332 Anything else means consider all windows on the selected frame
1333 and no others."
1334 (catch 'found
1335 (dolist (window (window-list-1 nil minibuf all-frames))
1336 (when (funcall predicate window)
1337 (throw 'found window)))
1338 default))
1340 (defalias 'some-window 'get-window-with-predicate)
1342 (defun get-lru-window (&optional all-frames dedicated)
1343 "Return the least recently used window on frames specified by ALL-FRAMES.
1344 Return a full-width window if possible. A minibuffer window is
1345 never a candidate. A dedicated window is never a candidate
1346 unless DEDICATED is non-nil, so if all windows are dedicated, the
1347 value is nil. Avoid returning the selected window if possible.
1349 The following non-nil values of the optional argument ALL-FRAMES
1350 have special meanings:
1352 - t means consider all windows on all existing frames.
1354 - `visible' means consider all windows on all visible frames on
1355 the current terminal.
1357 - 0 (the number zero) means consider all windows on all visible
1358 and iconified frames on the current terminal.
1360 - A frame means consider all windows on that frame only.
1362 Any other value of ALL-FRAMES means consider all windows on the
1363 selected frame and no others."
1364 (let (best-window best-time second-best-window second-best-time time)
1365 (dolist (window (window-list-1 nil nil all-frames))
1366 (when (or dedicated (not (window-dedicated-p window)))
1367 (setq time (window-use-time window))
1368 (if (or (eq window (selected-window))
1369 (not (window-full-width-p window)))
1370 (when (or (not second-best-time) (< time second-best-time))
1371 (setq second-best-time time)
1372 (setq second-best-window window))
1373 (when (or (not best-time) (< time best-time))
1374 (setq best-time time)
1375 (setq best-window window)))))
1376 (or best-window second-best-window)))
1378 (defun get-mru-window (&optional all-frames)
1379 "Return the most recently used window on frames specified by ALL-FRAMES.
1380 Do not return a minibuffer window.
1382 The following non-nil values of the optional argument ALL-FRAMES
1383 have special meanings:
1385 - t means consider all windows on all existing frames.
1387 - `visible' means consider all windows on all visible frames on
1388 the current terminal.
1390 - 0 (the number zero) means consider all windows on all visible
1391 and iconified frames on the current terminal.
1393 - A frame means consider all windows on that frame only.
1395 Any other value of ALL-FRAMES means consider all windows on the
1396 selected frame and no others."
1397 (let (best-window best-time time)
1398 (dolist (window (window-list-1 nil nil all-frames))
1399 (setq time (window-use-time window))
1400 (when (or (not best-time) (> time best-time))
1401 (setq best-time time)
1402 (setq best-window window)))
1403 best-window))
1405 (defun get-largest-window (&optional all-frames dedicated)
1406 "Return the largest window on frames specified by ALL-FRAMES.
1407 A minibuffer window is never a candidate. A dedicated window is
1408 never a candidate unless DEDICATED is non-nil, so if all windows
1409 are dedicated, the value is nil.
1411 The following non-nil values of the optional argument ALL-FRAMES
1412 have special meanings:
1414 - t means consider all windows on all existing frames.
1416 - `visible' means consider all windows on all visible frames on
1417 the current terminal.
1419 - 0 (the number zero) means consider all windows on all visible
1420 and iconified frames on the current terminal.
1422 - A frame means consider all windows on that frame only.
1424 Any other value of ALL-FRAMES means consider all windows on the
1425 selected frame and no others."
1426 (let ((best-size 0)
1427 best-window size)
1428 (dolist (window (window-list-1 nil nil all-frames))
1429 (when (or dedicated (not (window-dedicated-p window)))
1430 (setq size (* (window-total-size window)
1431 (window-total-size window t)))
1432 (when (> size best-size)
1433 (setq best-size size)
1434 (setq best-window window))))
1435 best-window))
1437 ;; The following is what `get-buffer-window' would look like if it were
1438 ;; implemented in Elisp. Since this function is needed for dumping,
1439 ;; leave it in C.
1441 ;; (defun get-buffer-window (&optional buffer-or-name all-frames)
1442 ;; "Return a window currently displaying BUFFER-OR-NAME, or nil if none.
1443 ;; BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
1444 ;; the current buffer.
1446 ;; The following non-nil values of the optional argument ALL-FRAMES
1447 ;; have special meanings:
1448 ;; - t means consider all windows on all existing frames.
1449 ;; - `visible' means consider all windows on all visible frames.
1450 ;; - 0 (the number zero) means consider all windows on all visible
1451 ;; and iconified frames.
1452 ;; - A frame means consider all windows on that frame only.
1453 ;; Any other value of ALL-FRAMES means consider all windows on the
1454 ;; selected frame and no others."
1455 ;; (let ((buffer (get-buffer buffer-or-name))
1456 ;; best-window)
1457 ;; (when (bufferp buffer)
1458 ;; (dolist (window (window-list-1 nil t all-frames))
1459 ;; (when (and (eq (window-buffer window) buffer)
1460 ;; ;; The following SHOULD have been handled by
1461 ;; ;; `window-list-1' already ...
1462 ;; (or (not (window-minibuffer-p window))
1463 ;; ;; Don't find any minibuffer window except the
1464 ;; ;; one that is currently in use.
1465 ;; (eq window (minibuffer-window)))
1466 ;; (or (not best-window)
1467 ;; ;; Prefer to return selected window.
1468 ;; (eq window (selected-window))
1469 ;; ;; Prefer windows on selected frame.
1470 ;; (eq (window-frame window) (selected-frame))))
1471 ;; (setq best-window window))))
1472 ;; best-window))
1474 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1475 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1476 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1477 and defaults to the current buffer.
1479 Any windows showing BUFFER-OR-NAME on the selected frame are listed
1480 first.
1482 MINIBUF t means include the minibuffer window even if the
1483 minibuffer is not active. MINIBUF nil or omitted means include
1484 the minibuffer window only if the minibuffer is active. Any
1485 other value means do not include the minibuffer window even if
1486 the minibuffer is active.
1488 ALL-FRAMES nil or omitted means consider all windows on the
1489 selected frame, plus the minibuffer window if specified by the
1490 MINIBUF argument. If the minibuffer counts, consider all windows
1491 on all frames that share that minibuffer too. The following
1492 non-nil values of ALL-FRAMES have special meanings:
1494 - t means consider all windows on all existing frames.
1496 - `visible' means consider all windows on all visible frames on
1497 the current terminal.
1499 - 0 (the number zero) means consider all windows on all visible
1500 and iconified frames on the current terminal.
1502 - A frame means consider all windows on that frame only.
1504 Anything else means consider all windows on the selected frame
1505 and no others."
1506 (let ((buffer (normalize-live-buffer buffer-or-name))
1507 windows)
1508 (dolist (window (window-list-1 (frame-first-window) minibuf all-frames))
1509 (when (eq (window-buffer window) buffer)
1510 (setq windows (cons window windows))))
1511 (nreverse windows)))
1513 (defun minibuffer-window-active-p (window)
1514 "Return t if WINDOW is the currently active minibuffer window."
1515 (eq window (active-minibuffer-window)))
1517 (defun count-windows (&optional minibuf)
1518 "Return the number of live windows on the selected frame.
1519 The optional argument MINIBUF specifies whether the minibuffer
1520 window shall be counted. See `walk-windows' for the precise
1521 meaning of this argument."
1522 (length (window-list-1 nil minibuf)))
1524 ;;; Resizing windows.
1525 (defun resize-window-reset (&optional frame horizontal)
1526 "Reset resize values for all windows on FRAME.
1527 FRAME defaults to the selected frame.
1529 This function stores the current value of `window-total-size' applied
1530 with argument HORIZONTAL in the new total size of all windows on
1531 FRAME. It also resets the new normal size of each of these
1532 windows."
1533 (resize-window-reset-1
1534 (frame-root-window (normalize-live-frame frame)) horizontal))
1536 (defun resize-window-reset-1 (window horizontal)
1537 "Internal function of `resize-window-reset'."
1538 ;; Register old size in the new total size.
1539 (set-window-new-total window (window-total-size window horizontal))
1540 ;; Reset new normal size.
1541 (set-window-new-normal window)
1542 (when (window-child window)
1543 (resize-window-reset-1 (window-child window) horizontal))
1544 (when (window-right window)
1545 (resize-window-reset-1 (window-right window) horizontal)))
1547 ;; The following routine is used to manually resize the minibuffer
1548 ;; window and is currently used, for example, by ispell.el.
1549 (defun resize-mini-window (window delta)
1550 "Resize minibuffer window WINDOW by DELTA lines.
1551 If WINDOW cannot be resized by DELTA lines make it as large \(or
1552 as small) as possible but don't signal an error."
1553 (when (window-minibuffer-p window)
1554 (let* ((frame (window-frame window))
1555 (root (frame-root-window frame))
1556 (height (window-total-size window))
1557 (min-delta
1558 (- (window-total-size root)
1559 (window-min-size root))))
1560 ;; Sanitize DELTA.
1561 (cond
1562 ((<= (+ height delta) 0)
1563 (setq delta (- (- height 1))))
1564 ((> delta min-delta)
1565 (setq delta min-delta)))
1567 ;; Resize now.
1568 (resize-window-reset frame)
1569 ;; Ideally we should be able to resize just the last subwindow of
1570 ;; root here. See the comment in `resize-root-window-vertically'
1571 ;; for why we do not do that.
1572 (resize-this-window root (- delta) nil nil t)
1573 (set-window-new-total window (+ height delta))
1574 ;; The following routine catches the case where we want to resize
1575 ;; a minibuffer-only frame.
1576 (resize-mini-window-internal window))))
1578 (defun resize-window (window delta &optional horizontal ignore)
1579 "Resize WINDOW vertically by DELTA lines.
1580 WINDOW can be an arbitrary window and defaults to the selected
1581 one. An attempt to resize the root window of a frame will raise
1582 an error though.
1584 DELTA a positive number means WINDOW shall be enlarged by DELTA
1585 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1586 lines.
1588 Optional argument HORIZONTAL non-nil means resize WINDOW
1589 horizontally by DELTA columns. In this case a positive DELTA
1590 means enlarge WINDOW by DELTA columns. DELTA negative means
1591 WINDOW shall be shrunk by -DELTA columns.
1593 Optional argument IGNORE non-nil means ignore any restrictions
1594 imposed by fixed size windows, `window-min-height' or
1595 `window-min-width' settings. IGNORE any window means ignore
1596 restrictions for that window only. IGNORE equal `safe' means
1597 live windows may get as small as `window-safe-min-height' lines
1598 and `window-safe-min-width' columns.
1600 This function resizes other windows proportionally and never
1601 deletes any windows. If you want to move only the low (right)
1602 edge of WINDOW consider using `adjust-window-trailing-edge'
1603 instead."
1604 (setq window (normalize-any-window window))
1605 (let* ((frame (window-frame window))
1606 sibling)
1607 (cond
1608 ((eq window (frame-root-window frame))
1609 (error "Cannot resize the root window of a frame"))
1610 ((window-minibuffer-p window)
1611 (resize-mini-window window delta))
1612 ((window-resizable-p window delta horizontal ignore)
1613 (resize-window-reset frame horizontal)
1614 (resize-this-window window delta horizontal ignore t)
1615 (if (and (not (window-splits window))
1616 (window-iso-combined-p window horizontal)
1617 (setq sibling (or (window-right window) (window-left window)))
1618 (window-sizable-p sibling (- delta) horizontal ignore))
1619 ;; If window-splits returns nil for WINDOW, WINDOW is part of
1620 ;; an iso-combination, and WINDOW's neighboring right or left
1621 ;; sibling can be resized as requested, resize that sibling.
1622 (let ((normal-delta
1623 (/ (float delta)
1624 (window-total-size (window-parent window) horizontal))))
1625 (resize-this-window sibling (- delta) horizontal nil t)
1626 (set-window-new-normal
1627 window (+ (window-normal-size window horizontal)
1628 normal-delta))
1629 (set-window-new-normal
1630 sibling (- (window-normal-size sibling horizontal)
1631 normal-delta)))
1632 ;; Otherwise, resize all other windows in the same combination.
1633 (resize-other-windows window delta horizontal ignore))
1634 (resize-window-apply frame horizontal))
1636 (error "Cannot resize window %s" window)))))
1638 (defsubst resize-subwindows-skip-p (window)
1639 "Return non-nil if WINDOW shall be skipped by resizing routines."
1640 (memq (window-new-normal window) '(ignore stuck skip)))
1642 (defun resize-subwindows-normal (parent horizontal window this-delta &optional trail other-delta)
1643 "Set the new normal height of subwindows of window PARENT.
1644 HORIZONTAL non-nil means set the new normal width of these
1645 windows. WINDOW specifies a subwindow of PARENT that has been
1646 resized by THIS-DELTA lines \(columns).
1648 Optional argument TRAIL either 'before or 'after means set values
1649 for windows before or after WINDOW only. Optional argument
1650 OTHER-DELTA a number specifies that this many lines \(columns)
1651 have been obtained from \(or returned to) an ancestor window of
1652 PARENT in order to resize WINDOW."
1653 (let* ((delta-normal
1654 (if (and (= (- this-delta) (window-total-size window horizontal))
1655 (zerop other-delta))
1656 ;; When WINDOW gets deleted and we can return its entire
1657 ;; space to its siblings, use WINDOW's normal size as the
1658 ;; normal delta.
1659 (- (window-normal-size window horizontal))
1660 ;; In any other case calculate the normal delta from the
1661 ;; relation of THIS-DELTA to the total size of PARENT.
1662 (/ (float this-delta) (window-total-size parent horizontal))))
1663 (sub (window-child parent))
1664 (parent-normal 0.0)
1665 (skip (eq trail 'after)))
1667 ;; Set parent-normal to the sum of the normal sizes of all
1668 ;; subwindows of PARENT that shall be resized, excluding only WINDOW
1669 ;; and any windows specified by the optional TRAIL argument.
1670 (while sub
1671 (cond
1672 ((eq sub window)
1673 (setq skip (eq trail 'before)))
1674 (skip)
1676 (setq parent-normal
1677 (+ parent-normal (window-normal-size sub horizontal)))))
1678 (setq sub (window-right sub)))
1680 ;; Set the new normal size of all subwindows of PARENT from what
1681 ;; they should have contributed for recovering THIS-DELTA lines
1682 ;; (columns).
1683 (setq sub (window-child parent))
1684 (setq skip (eq trail 'after))
1685 (while sub
1686 (cond
1687 ((eq sub window)
1688 (setq skip (eq trail 'before)))
1689 (skip)
1691 (let ((old-normal (window-normal-size sub horizontal)))
1692 (set-window-new-normal
1693 sub (min 1.0 ; Don't get larger than 1.
1694 (max (- old-normal
1695 (* (/ old-normal parent-normal)
1696 delta-normal))
1697 ;; Don't drop below 0.
1698 0.0))))))
1699 (setq sub (window-right sub)))
1701 (when (numberp other-delta)
1702 ;; Set the new normal size of windows from what they should have
1703 ;; contributed for recovering OTHER-DELTA lines (columns).
1704 (setq delta-normal (/ (float (window-total-size parent horizontal))
1705 (+ (window-total-size parent horizontal)
1706 other-delta)))
1707 (setq sub (window-child parent))
1708 (setq skip (eq trail 'after))
1709 (while sub
1710 (cond
1711 ((eq sub window)
1712 (setq skip (eq trail 'before)))
1713 (skip)
1715 (set-window-new-normal
1716 sub (min 1.0 ; Don't get larger than 1.
1717 (max (* (window-new-normal sub) delta-normal)
1718 ;; Don't drop below 0.
1719 0.0)))))
1720 (setq sub (window-right sub))))
1722 ;; Set the new normal size of WINDOW to what is left by the sum of
1723 ;; the normal sizes of its siblings.
1724 (set-window-new-normal
1725 window
1726 (let ((sum 0))
1727 (setq sub (window-child parent))
1728 (while sub
1729 (cond
1730 ((eq sub window))
1731 ((not (numberp (window-new-normal sub)))
1732 (setq sum (+ sum (window-normal-size sub horizontal))))
1734 (setq sum (+ sum (window-new-normal sub)))))
1735 (setq sub (window-right sub)))
1736 ;; Don't get larger than 1 or smaller than 0.
1737 (min 1.0 (max (- 1.0 sum) 0.0))))))
1739 (defun resize-subwindows (parent delta &optional horizontal window ignore trail edge)
1740 "Resize subwindows of window PARENT vertically by DELTA lines.
1741 PARENT must be a vertically combined internal window.
1743 Optional argument HORIZONTAL non-nil means resize subwindows of
1744 PARENT horizontally by DELTA columns. In this case PARENT must
1745 be a horizontally combined internal window.
1747 WINDOW, if specified, must denote a child window of PARENT that
1748 is resized by DELTA lines.
1750 Optional argument IGNORE non-nil means ignore any restrictions
1751 imposed by fixed size windows, `window-min-height' or
1752 `window-min-width' settings. IGNORE equal `safe' means live
1753 windows may get as small as `window-safe-min-height' lines and
1754 `window-safe-min-width' columns. IGNORE any window means ignore
1755 restrictions for that window only.
1757 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1758 of windows that shall be resized. If TRAIL equals `before',
1759 resize only windows on the left or above EDGE. If TRAIL equals
1760 `after', resize only windows on the right or below EDGE. Also,
1761 preferably only resize windows adjacent to EDGE.
1763 Return the symbol `normalized' if new normal sizes have been
1764 already set by this routine."
1765 (let* ((first (window-child parent))
1766 (sub first)
1767 (parent-total (+ (window-total-size parent horizontal) delta))
1768 best-window best-value)
1770 (if (and edge (memq trail '(before after))
1771 (progn
1772 (setq sub first)
1773 (while (and (window-right sub)
1774 (or (and (eq trail 'before)
1775 (not (resize-subwindows-skip-p
1776 (window-right sub))))
1777 (and (eq trail 'after)
1778 (resize-subwindows-skip-p sub))))
1779 (setq sub (window-right sub)))
1780 sub)
1781 (if horizontal
1782 (if (eq trail 'before)
1783 (= (+ (window-left-column sub)
1784 (window-total-size sub t))
1785 edge)
1786 (= (window-left-column sub) edge))
1787 (if (eq trail 'before)
1788 (= (+ (window-top-line sub)
1789 (window-total-size sub))
1790 edge)
1791 (= (window-top-line sub) edge)))
1792 (window-sizable-p sub delta horizontal ignore))
1793 ;; Resize only windows adjacent to EDGE.
1794 (progn
1795 (resize-this-window sub delta horizontal ignore t trail edge)
1796 (if (and window (eq (window-parent sub) parent))
1797 (progn
1798 ;; Assign new normal sizes.
1799 (set-window-new-normal
1800 sub (/ (float (window-new-total sub)) parent-total))
1801 (set-window-new-normal
1802 window (- (window-normal-size window horizontal)
1803 (- (window-new-normal sub)
1804 (window-normal-size sub horizontal)))))
1805 (resize-subwindows-normal parent horizontal sub 0 trail delta))
1806 ;; Return 'normalized to notify `resize-other-windows' that
1807 ;; normal sizes have been already set.
1808 'normalized)
1809 ;; Resize all windows proportionally.
1810 (setq sub first)
1811 (while sub
1812 (cond
1813 ((or (resize-subwindows-skip-p sub)
1814 ;; Ignore windows to skip and fixed-size subwindows - in
1815 ;; the latter case make it a window to skip.
1816 (and (not ignore)
1817 (window-size-fixed-p sub horizontal)
1818 (set-window-new-normal sub 'ignore))))
1819 ((< delta 0)
1820 ;; When shrinking store the number of lines/cols we can get
1821 ;; from this window here together with the total/normal size
1822 ;; factor.
1823 (set-window-new-normal
1825 (cons
1826 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
1827 (window-min-delta sub horizontal ignore trail t) ; t)
1828 (- (/ (float (window-total-size sub horizontal))
1829 parent-total)
1830 (window-normal-size sub horizontal)))))
1831 ((> delta 0)
1832 ;; When enlarging store the total/normal size factor only
1833 (set-window-new-normal
1835 (- (/ (float (window-total-size sub horizontal))
1836 parent-total)
1837 (window-normal-size sub horizontal)))))
1839 (setq sub (window-right sub)))
1841 (cond
1842 ((< delta 0)
1843 ;; Shrink windows by delta.
1844 (setq best-window t)
1845 (while (and best-window (not (zerop delta)))
1846 (setq sub first)
1847 (setq best-window nil)
1848 (setq best-value most-negative-fixnum)
1849 (while sub
1850 (when (and (consp (window-new-normal sub))
1851 (not (zerop (car (window-new-normal sub))))
1852 (> (cdr (window-new-normal sub)) best-value))
1853 (setq best-window sub)
1854 (setq best-value (cdr (window-new-normal sub))))
1856 (setq sub (window-right sub)))
1858 (when best-window
1859 (setq delta (1+ delta)))
1860 (set-window-new-total best-window -1 t)
1861 (set-window-new-normal
1862 best-window
1863 (if (= (car (window-new-normal best-window)) 1)
1864 'skip ; We can't shrink best-window any further.
1865 (cons (1- (car (window-new-normal best-window)))
1866 (- (/ (float (window-new-total best-window))
1867 parent-total)
1868 (window-normal-size best-window horizontal)))))))
1869 ((> delta 0)
1870 ;; Enlarge windows by delta.
1871 (setq best-window t)
1872 (while (and best-window (not (zerop delta)))
1873 (setq sub first)
1874 (setq best-window nil)
1875 (setq best-value most-positive-fixnum)
1876 (while sub
1877 (when (and (numberp (window-new-normal sub))
1878 (< (window-new-normal sub) best-value))
1879 (setq best-window sub)
1880 (setq best-value (window-new-normal sub)))
1882 (setq sub (window-right sub)))
1884 (when best-window
1885 (setq delta (1- delta)))
1886 (set-window-new-total best-window 1 t)
1887 (set-window-new-normal
1888 best-window
1889 (- (/ (float (window-new-total best-window))
1890 parent-total)
1891 (window-normal-size best-window horizontal))))))
1893 (when best-window
1894 (setq sub first)
1895 (while sub
1896 (when (or (consp (window-new-normal sub))
1897 (numberp (window-new-normal sub)))
1898 ;; Reset new normal size fields so `resize-window-apply'
1899 ;; won't use them to apply new sizes.
1900 (set-window-new-normal sub))
1902 (unless (eq (window-new-normal sub) 'ignore)
1903 ;; Resize this subwindow's subwindows (back-engineering
1904 ;; delta from sub's old and new total sizes).
1905 (let ((delta (- (window-new-total sub)
1906 (window-total-size sub horizontal))))
1907 (unless (and (zerop delta) (not trail))
1908 ;; For the TRAIL non-nil case we have to resize SUB
1909 ;; recursively even if it's size does not change.
1910 (resize-this-window
1911 sub delta horizontal ignore nil trail edge))))
1912 (setq sub (window-right sub)))))))
1914 (defun resize-other-windows (window delta &optional horizontal ignore trail edge)
1915 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1916 Optional argument HORIZONTAL non-nil means resize other windows
1917 when WINDOW is resized horizontally by DELTA columns. WINDOW
1918 itself is not resized by this function.
1920 Optional argument IGNORE non-nil means ignore any restrictions
1921 imposed by fixed size windows, `window-min-height' or
1922 `window-min-width' settings. IGNORE equal `safe' means live
1923 windows may get as small as `window-safe-min-height' lines and
1924 `window-safe-min-width' columns. IGNORE any window means ignore
1925 restrictions for that window only.
1927 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1928 of windows that shall be resized. If TRAIL equals `before',
1929 resize only windows on the left or above EDGE. If TRAIL equals
1930 `after', resize only windows on the right or below EDGE. Also,
1931 preferably only resize windows adjacent to EDGE."
1932 (when (window-parent window)
1933 (let* ((parent (window-parent window))
1934 (sub (window-child parent)))
1935 (if (window-iso-combined-p sub horizontal)
1936 ;; In an iso-combination try to extract DELTA from WINDOW's
1937 ;; siblings.
1938 (let ((first sub)
1939 (skip (eq trail 'after))
1940 this-delta other-delta)
1941 ;; Decide which windows shall be left alone.
1942 (while sub
1943 (cond
1944 ((eq sub window)
1945 ;; Make sure WINDOW is left alone when
1946 ;; resizing its siblings.
1947 (set-window-new-normal sub 'ignore)
1948 (setq skip (eq trail 'before)))
1949 (skip
1950 ;; Make sure this sibling is left alone when
1951 ;; resizing its siblings.
1952 (set-window-new-normal sub 'ignore))
1953 ((or (window-size-ignore sub ignore)
1954 (not (window-size-fixed-p sub horizontal)))
1955 ;; Set this-delta to t to signal that we found a sibling
1956 ;; of WINDOW whose size is not fixed.
1957 (setq this-delta t)))
1959 (setq sub (window-right sub)))
1961 ;; Set this-delta to what we can get from WINDOW's siblings.
1962 (if (= (- delta) (window-total-size window horizontal))
1963 ;; A deletion, presumably. We must handle this case
1964 ;; specially since `window-resizable' can't be used.
1965 (if this-delta
1966 ;; There's at least one resizable sibling we can
1967 ;; give WINDOW's size to.
1968 (setq this-delta delta)
1969 ;; No resizable sibling exists.
1970 (setq this-delta 0))
1971 ;; Any other form of resizing.
1972 (setq this-delta
1973 (window-resizable window delta horizontal ignore trail t)))
1975 ;; Set other-delta to what we still have to get from
1976 ;; ancestor windows of parent.
1977 (setq other-delta (- delta this-delta))
1978 (unless (zerop other-delta)
1979 ;; Unless we got everything from WINDOW's siblings, PARENT
1980 ;; must be resized by other-delta lines or columns.
1981 (set-window-new-total parent other-delta 'add))
1983 (if (zerop this-delta)
1984 ;; We haven't got anything from WINDOW's siblings but we
1985 ;; must update the normal sizes to respect other-delta.
1986 (resize-subwindows-normal
1987 parent horizontal window this-delta trail other-delta)
1988 ;; We did get something from WINDOW's siblings which means
1989 ;; we have to resize their subwindows.
1990 (unless (eq (resize-subwindows parent (- this-delta) horizontal
1991 window ignore trail edge)
1992 ;; `resize-subwindows' returning 'normalized,
1993 ;; means it has set the normal sizes already.
1994 'normalized)
1995 ;; Set the normal sizes.
1996 (resize-subwindows-normal
1997 parent horizontal window this-delta trail other-delta))
1998 ;; Set DELTA to what we still have to get from ancestor
1999 ;; windows.
2000 (setq delta other-delta)))
2002 ;; In an ortho-combination all siblings of WINDOW must be
2003 ;; resized by DELTA.
2004 (set-window-new-total parent delta 'add)
2005 (while sub
2006 (unless (eq sub window)
2007 (resize-this-window sub delta horizontal ignore t))
2008 (setq sub (window-right sub))))
2010 (unless (zerop delta)
2011 ;; "Go up."
2012 (resize-other-windows parent delta horizontal ignore trail edge)))))
2014 (defun resize-this-window (window delta &optional horizontal ignore add trail edge)
2015 "Resize WINDOW vertically by DELTA lines.
2016 Optional argument HORIZONTAL non-nil means resize WINDOW
2017 horizontally by DELTA columns.
2019 Optional argument IGNORE non-nil means ignore any restrictions
2020 imposed by fixed size windows, `window-min-height' or
2021 `window-min-width' settings. IGNORE equal `safe' means live
2022 windows may get as small as `window-safe-min-height' lines and
2023 `window-safe-min-width' columns. IGNORE any window means ignore
2024 restrictions for that window only.
2026 Optional argument ADD non-nil means add DELTA to the new total
2027 size of WINDOW.
2029 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2030 of windows that shall be resized. If TRAIL equals `before',
2031 resize only windows on the left or above EDGE. If TRAIL equals
2032 `after', resize only windows on the right or below EDGE. Also,
2033 preferably only resize windows adjacent to EDGE.
2035 This function recursively resizes WINDOW's subwindows to fit the
2036 new size. Make sure that WINDOW is `window-resizable' before
2037 calling this function. Note that this function does not resize
2038 siblings of WINDOW or WINDOW's parent window. You have to
2039 eventually call `resize-window-apply' in order to make resizing
2040 actually take effect."
2041 (when add
2042 ;; Add DELTA to the new total size of WINDOW.
2043 (set-window-new-total window delta t))
2045 (let ((sub (window-child window)))
2046 (cond
2047 ((not sub))
2048 ((window-iso-combined-p sub horizontal)
2049 ;; In an iso-combination resize subwindows according to their
2050 ;; normal sizes.
2051 (resize-subwindows window delta horizontal nil ignore trail edge))
2052 ;; In an ortho-combination resize each subwindow by DELTA.
2054 (while sub
2055 (resize-this-window sub delta horizontal ignore t trail edge)
2056 (setq sub (window-right sub)))))))
2058 (defun resize-root-window (window delta horizontal ignore)
2059 "Resize root window WINDOW vertically by DELTA lines.
2060 HORIZONTAL non-nil means resize root window WINDOW horizontally
2061 by DELTA columns.
2063 IGNORE non-nil means ignore any restrictions imposed by fixed
2064 size windows, `window-min-height' or `window-min-width' settings.
2066 This function is only called by the frame resizing routines. It
2067 resizes windows proportionally and never deletes any windows."
2068 (when (and (windowp window) (numberp delta)
2069 (window-sizable-p window delta horizontal ignore))
2070 (resize-window-reset (window-frame window) horizontal)
2071 (resize-this-window window delta horizontal ignore t)))
2073 (defun resize-root-window-vertically (window delta)
2074 "Resize root window WINDOW vertically by DELTA lines.
2075 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2076 lines, shrink it as much as possible. If DELTA is greater than
2077 zero, this function can resize fixed-size subwindows in order to
2078 recover the necessary lines.
2080 Return the number of lines that were recovered.
2082 This function is only called by the minibuffer window resizing
2083 routines. It resizes windows proportionally and never deletes
2084 any windows."
2085 (when (numberp delta)
2086 (let (ignore)
2087 (cond
2088 ((< delta 0)
2089 (setq delta (window-sizable window delta)))
2090 ((> delta 0)
2091 (unless (window-sizable window delta)
2092 (setq ignore t))))
2094 (resize-window-reset (window-frame window))
2095 ;; Ideally, we would resize just the last window in a combination
2096 ;; but that's not feasible for the following reason: If we grow
2097 ;; the minibuffer window and the last window cannot be shrunk any
2098 ;; more, we shrink another window instead. But if we then shrink
2099 ;; the minibuffer window again, the last window might get enlarged
2100 ;; and the state after shrinking is not the state before growing.
2101 ;; So, in practice, we'd need a history variable to record how to
2102 ;; proceed. But I'm not sure how such a variable could work with
2103 ;; repeated minibuffer window growing steps.
2104 (resize-this-window window delta nil ignore t)
2105 delta)))
2107 (defun adjust-window-trailing-edge (window delta &optional horizontal)
2108 "Move WINDOW's bottom edge by DELTA lines.
2109 Optional argument HORIZONTAL non-nil means move WINDOW's right
2110 edge by DELTA columns. WINDOW defaults to the selected window.
2112 If DELTA is greater zero, then move the edge downwards or to the
2113 right. If DELTA is less than zero, move the edge upwards or to
2114 the left. If the edge can't be moved by DELTA lines or columns,
2115 move it as far as possible in the desired direction."
2116 (setq window (normalize-any-window window))
2117 (let ((frame (window-frame window))
2118 (right window)
2119 left this-delta min-delta max-delta failed)
2120 ;; Find the edge we want to move.
2121 (while (and (or (not (window-iso-combined-p right horizontal))
2122 (not (window-right right)))
2123 (setq right (window-parent right))))
2124 (cond
2125 ((and (not right) (not horizontal) (not resize-mini-windows)
2126 (eq (window-frame (minibuffer-window frame)) frame))
2127 (resize-mini-window (minibuffer-window frame) (- delta)))
2128 ((or (not (setq left right)) (not (setq right (window-right right))))
2129 (if horizontal
2130 (error "No window on the right of this one")
2131 (error "No window below this one")))
2133 ;; Set LEFT to the first resizable window on the left. This step is
2134 ;; needed to handle fixed-size windows.
2135 (while (and left (window-size-fixed-p left horizontal))
2136 (setq left
2137 (or (window-left left)
2138 (progn
2139 (while (and (setq left (window-parent left))
2140 (not (window-iso-combined-p left horizontal))))
2141 (window-left left)))))
2142 (unless left
2143 (if horizontal
2144 (error "No resizable window on the left of this one")
2145 (error "No resizable window above this one")))
2147 ;; Set RIGHT to the first resizable window on the right. This step
2148 ;; is needed to handle fixed-size windows.
2149 (while (and right (window-size-fixed-p right horizontal))
2150 (setq right
2151 (or (window-right right)
2152 (progn
2153 (while (and (setq right (window-parent right))
2154 (not (window-iso-combined-p right horizontal))))
2155 (window-right right)))))
2156 (unless right
2157 (if horizontal
2158 (error "No resizable window on the right of this one")
2159 (error "No resizable window below this one")))
2161 ;; LEFT and RIGHT (which might be both internal windows) are now the
2162 ;; two windows we want to resize.
2163 (cond
2164 ((> delta 0)
2165 (setq max-delta (window-max-delta-1 left 0 horizontal nil 'after))
2166 (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'before))
2167 (when (or (< max-delta delta) (> min-delta (- delta)))
2168 ;; We can't get the whole DELTA - move as far as possible.
2169 (setq delta (min max-delta (- min-delta))))
2170 (unless (zerop delta)
2171 ;; Start resizing.
2172 (resize-window-reset frame horizontal)
2173 ;; Try to enlarge LEFT first.
2174 (setq this-delta (window-resizable left delta horizontal))
2175 (unless (zerop this-delta)
2176 (resize-this-window
2177 left this-delta horizontal nil t 'before
2178 (if horizontal
2179 (+ (window-left-column left) (window-total-size left t))
2180 (+ (window-top-line left) (window-total-size left)))))
2181 ;; Shrink windows on right of LEFT.
2182 (resize-other-windows
2183 left delta horizontal nil 'after
2184 (if horizontal
2185 (window-left-column right)
2186 (window-top-line right)))))
2187 ((< delta 0)
2188 (setq max-delta (window-max-delta-1 right 0 horizontal nil 'before))
2189 (setq min-delta (window-min-delta-1 left delta horizontal nil 'after))
2190 (when (or (< max-delta (- delta)) (> min-delta delta))
2191 ;; We can't get the whole DELTA - move as far as possible.
2192 (setq delta (max (- max-delta) min-delta)))
2193 (unless (zerop delta)
2194 ;; Start resizing.
2195 (resize-window-reset frame horizontal)
2196 ;; Try to enlarge RIGHT.
2197 (setq this-delta (window-resizable right (- delta) horizontal))
2198 (unless (zerop this-delta)
2199 (resize-this-window
2200 right this-delta horizontal nil t 'after
2201 (if horizontal
2202 (window-left-column right)
2203 (window-top-line right))))
2204 ;; Shrink windows on left of RIGHT.
2205 (resize-other-windows
2206 right (- delta) horizontal nil 'before
2207 (if horizontal
2208 (+ (window-left-column left) (window-total-size left t))
2209 (+ (window-top-line left) (window-total-size left)))))))
2210 (unless (zerop delta)
2211 ;; Don't report an error in the standard case.
2212 (unless (resize-window-apply frame horizontal)
2213 ;; But do report an error if applying the changes fails.
2214 (error "Failed adjusting window %s" window)))))))
2216 (defun enlarge-window (delta &optional horizontal)
2217 "Make selected window DELTA lines taller.
2218 Interactively, if no argument is given, make the selected window
2219 one line taller. If optional argument HORIZONTAL is non-nil,
2220 make selected window wider by DELTA columns. If DELTA is
2221 negative, shrink selected window by -DELTA lines or columns.
2222 Return nil."
2223 (interactive "p")
2224 (resize-window (selected-window) delta horizontal))
2226 (defun shrink-window (delta &optional horizontal)
2227 "Make selected window DELTA lines smaller.
2228 Interactively, if no argument is given, make the selected window
2229 one line smaller. If optional argument HORIZONTAL is non-nil,
2230 make selected window narrower by DELTA columns. If DELTA is
2231 negative, enlarge selected window by -DELTA lines or columns.
2232 Return nil."
2233 (interactive "p")
2234 (resize-window (selected-window) (- delta) horizontal))
2236 (defun maximize-window (&optional window)
2237 "Maximize WINDOW.
2238 Make WINDOW as large as possible without deleting any windows.
2239 WINDOW can be any window and defaults to the selected window."
2240 (interactive)
2241 (setq window (normalize-any-window window))
2242 (resize-window window (window-max-delta window))
2243 (resize-window window (window-max-delta window t) t))
2245 (defun minimize-window (&optional window)
2246 "Minimize WINDOW.
2247 Make WINDOW as small as possible without deleting any windows.
2248 WINDOW can be any window and defaults to the selected window."
2249 (interactive)
2250 (setq window (normalize-any-window window))
2251 (resize-window window (- (window-min-delta window)))
2252 (resize-window window (- (window-min-delta window t)) t))
2254 (defsubst frame-root-window-p (window)
2255 "Return non-nil if WINDOW is the root window of its frame."
2256 (eq window (frame-root-window window)))
2258 (defun window-tree-1 (window &optional next)
2259 "Return window tree rooted at WINDOW.
2260 Optional argument NEXT non-nil means include windows right
2261 siblings in the return value.
2263 See the documentation of `window-tree' for a description of the
2264 return value."
2265 (let (list)
2266 (while window
2267 (setq list
2268 (cons
2269 (cond
2270 ((window-vchild window)
2271 (cons t (cons (window-edges window)
2272 (window-tree-1 (window-vchild window) t))))
2273 ((window-hchild window)
2274 (cons nil (cons (window-edges window)
2275 (window-tree-1 (window-hchild window) t))))
2276 (t window))
2277 list))
2278 (setq window (when next (window-next window))))
2279 (nreverse list)))
2281 (defun window-tree (&optional frame)
2282 "Return the window tree of frame FRAME.
2283 FRAME must be a live frame and defaults to the selected frame.
2284 The return value is a list of the form (ROOT MINI), where ROOT
2285 represents the window tree of the frame's root window, and MINI
2286 is the frame's minibuffer window.
2288 If the root window is not split, ROOT is the root window itself.
2289 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2290 for a horizontal split, and t for a vertical split. EDGES gives
2291 the combined size and position of the subwindows in the split,
2292 and the rest of the elements are the subwindows in the split.
2293 Each of the subwindows may again be a window or a list
2294 representing a window split, and so on. EDGES is a list \(LEFT
2295 TOP RIGHT BOTTOM) as returned by `window-edges'."
2296 (setq frame (normalize-live-frame frame))
2297 (window-tree-1 (frame-root-window frame) t))
2299 (defun other-window (count &optional all-frames)
2300 "Select another window in cyclic ordering of windows.
2301 COUNT specifies the number of windows to skip, starting with the
2302 selected window, before making the selection. If COUNT is
2303 positive, skip COUNT windows forwards. If COUNT is negative,
2304 skip -COUNT windows backwards. COUNT zero means do not skip any
2305 window, so select the selected window. In an interactive call,
2306 COUNT is the numeric prefix argument. Return nil.
2308 If the `other-window' parameter of WINDOW is a function and
2309 `ignore-window-parameters' is nil, call that function with the
2310 arguments COUNT and ALL-FRAMES.
2312 This function does not select a window whose `no-other-window'
2313 window parameter is non-nil.
2315 This function uses `next-window' for finding the window to
2316 select. The argument ALL-FRAMES has the same meaning as in
2317 `next-window', but the MINIBUF argument of `next-window' is
2318 always effectively nil."
2319 (interactive "p")
2320 (let* ((window (selected-window))
2321 (function (and (not ignore-window-parameters)
2322 (window-parameter window 'other-window)))
2323 old-window old-count)
2324 (if (functionp function)
2325 (funcall function count all-frames)
2326 ;; `next-window' and `previous-window' may return a window we are
2327 ;; not allowed to select. Hence we need an exit strategy in case
2328 ;; all windows are non-selectable.
2329 (catch 'exit
2330 (while (> count 0)
2331 (setq window (next-window window nil all-frames))
2332 (cond
2333 ((eq window old-window)
2334 (when (= count old-count)
2335 ;; Keep out of infinite loops. When COUNT has not changed
2336 ;; since we last looked at `window' we're probably in one.
2337 (throw 'exit nil)))
2338 ((window-parameter window 'no-other-window)
2339 (unless old-window
2340 ;; The first non-selectable window `next-window' got us:
2341 ;; Remember it and the current value of COUNT.
2342 (setq old-window window)
2343 (setq old-count count)))
2345 (setq count (1- count)))))
2346 (while (< count 0)
2347 (setq window (previous-window window nil all-frames))
2348 (cond
2349 ((eq window old-window)
2350 (when (= count old-count)
2351 ;; Keep out of infinite loops. When COUNT has not changed
2352 ;; since we last looked at `window' we're probably in one.
2353 (throw 'exit nil)))
2354 ((window-parameter window 'no-other-window)
2355 (unless old-window
2356 ;; The first non-selectable window `previous-window' got
2357 ;; us: Remember it and the current value of COUNT.
2358 (setq old-window window)
2359 (setq old-count count)))
2361 (setq count (1+ count)))))
2363 (select-window window)
2364 ;; Always return nil.
2365 nil))))
2367 ;; This should probably return non-nil when the selected window is part
2368 ;; of an atomic window whose root is the frame's root window.
2369 (defun one-window-p (&optional nomini all-frames)
2370 "Return non-nil if the selected window is the only window.
2371 Optional arg NOMINI non-nil means don't count the minibuffer
2372 even if it is active. Otherwise, the minibuffer is counted
2373 when it is active.
2375 Optional argument ALL-FRAMES specifies the set of frames to
2376 consider, see also `next-window'. ALL-FRAMES nil or omitted
2377 means consider windows on the selected frame only, plus the
2378 minibuffer window if specified by the NOMINI argument. If the
2379 minibuffer counts, consider all windows on all frames that share
2380 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2381 with a special meaning are:
2383 - t means consider all windows on all existing frames.
2385 - `visible' means consider all windows on all visible frames on
2386 the current terminal.
2388 - 0 (the number zero) means consider all windows on all visible
2389 and iconified frames on the current terminal.
2391 - A frame means consider all windows on that frame only.
2393 Anything else means consider all windows on the selected frame
2394 and no others."
2395 (let ((base-window (selected-window)))
2396 (if (and nomini (eq base-window (minibuffer-window)))
2397 (setq base-window (next-window base-window)))
2398 (eq base-window
2399 (next-window base-window (if nomini 'arg) all-frames))))
2401 ;;; Deleting windows.
2402 (defun window-deletable-p (&optional window)
2403 "Return t if WINDOW can be safely deleted from its frame.
2404 Return `frame' if deleting WINDOW should delete its frame
2405 instead."
2406 (setq window (normalize-any-window window))
2407 (unless ignore-window-parameters
2408 ;; Handle atomicity.
2409 (when (window-parameter window 'window-atom)
2410 (setq window (window-atom-root window))))
2411 (let ((parent (window-parent window))
2412 (frame (window-frame window))
2413 (dedicated (and (window-buffer window) (window-dedicated-p window)))
2414 (quit-restore (window-parameter window 'quit-restore)))
2415 (cond
2416 ((frame-root-window-p window)
2417 (when (and (or dedicated
2418 (and (eq (car-safe quit-restore) 'new-frame)
2419 (eq (nth 1 quit-restore) (window-buffer window))))
2420 (other-visible-frames-p frame))
2421 ;; WINDOW is the root window of its frame. Return `frame' but
2422 ;; only if WINDOW is (1) either dedicated or quit-restore's car
2423 ;; is new-frame and the window still displays the same buffer
2424 ;; and (2) there are other frames left.
2425 'frame))
2426 ((and (not ignore-window-parameters)
2427 (eq (window-parameter window 'window-side) 'none)
2428 (or (not parent)
2429 (not (eq (window-parameter parent 'window-side) 'none))))
2430 ;; Can't delete last main window.
2431 nil)
2432 (t))))
2434 (defun window-or-subwindow-p (subwindow window)
2435 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2436 (or (eq subwindow window)
2437 (let ((parent (window-parent subwindow)))
2438 (catch 'done
2439 (while parent
2440 (if (eq parent window)
2441 (throw 'done t)
2442 (setq parent (window-parent parent))))))))
2444 (defun delete-window (&optional window)
2445 "Delete WINDOW.
2446 WINDOW can be an arbitrary window and defaults to the selected
2447 one. Return nil.
2449 If the variable `ignore-window-parameters' is non-nil or the
2450 `delete-window' parameter of WINDOW equals t, do not process any
2451 parameters of WINDOW. Otherwise, if the `delete-window'
2452 parameter of WINDOW specifies a function, call that function with
2453 WINDOW as its sole argument and return the value returned by that
2454 function.
2456 Otherwise, if WINDOW is part of an atomic window, call
2457 `delete-window' with the root of the atomic window as its
2458 argument. If WINDOW is the only window on its frame or the last
2459 non-side window, signal an error."
2460 (interactive)
2461 (setq window (normalize-any-window window))
2462 (let* ((frame (window-frame window))
2463 (function (window-parameter window 'delete-window))
2464 (parent (window-parent window))
2465 atom-root)
2466 (window-check frame)
2467 (catch 'done
2468 ;; Handle window parameters.
2469 (cond
2470 ;; Ignore window parameters if `ignore-window-parameters' tells
2471 ;; us so or `delete-window' equals t.
2472 ((or ignore-window-parameters (eq function t)))
2473 ((functionp function)
2474 ;; The `delete-window' parameter specifies the function to call.
2475 ;; If that function is `ignore' nothing is done. It's up to the
2476 ;; function called here to avoid infinite recursion.
2477 (throw 'done (funcall function window)))
2478 ((and (window-parameter window 'window-atom)
2479 (setq atom-root (window-atom-root window))
2480 (not (eq atom-root window)))
2481 (throw 'done (delete-window atom-root)))
2482 ((and (eq (window-parameter window 'window-side) 'none)
2483 (or (not parent)
2484 (not (eq (window-parameter parent 'window-side) 'none))))
2485 (error "Attempt to delete last non-side window"))
2486 ((not parent)
2487 (error "Attempt to delete minibuffer or sole ordinary window")))
2489 (let* ((horizontal (window-hchild parent))
2490 (size (window-total-size window horizontal))
2491 (frame-selected
2492 (window-or-subwindow-p (frame-selected-window frame) window))
2493 ;; Emacs 23 preferably gives WINDOW's space to its left
2494 ;; sibling.
2495 (sibling (or (window-left window) (window-right window))))
2496 (resize-window-reset frame horizontal)
2497 (cond
2498 ((and (not (window-splits window))
2499 sibling (window-sizable-p sibling size))
2500 ;; Resize WINDOW's sibling.
2501 (resize-this-window sibling size horizontal nil t)
2502 (set-window-new-normal
2503 sibling (+ (window-normal-size sibling horizontal)
2504 (window-normal-size window horizontal))))
2505 ((window-resizable-p window (- size) horizontal nil nil nil t)
2506 ;; Can do without resizing fixed-size windows.
2507 (resize-other-windows window (- size) horizontal))
2509 ;; Can't do without resizing fixed-size windows.
2510 (resize-other-windows window (- size) horizontal t)))
2511 ;; Actually delete WINDOW.
2512 (delete-window-internal window)
2513 (when (and frame-selected
2514 (window-parameter
2515 (frame-selected-window frame) 'no-other-window))
2516 ;; `delete-window-internal' has selected a window that should
2517 ;; not be selected, fix this here.
2518 (other-window -1 frame))
2519 (run-window-configuration-change-hook frame)
2520 (window-check frame)
2521 ;; Always return nil.
2522 nil))))
2524 (defun delete-other-windows (&optional window)
2525 "Make WINDOW fill its frame.
2526 WINDOW may be any window and defaults to the selected one.
2527 Return nil.
2529 If the variable `ignore-window-parameters' is non-nil or the
2530 `delete-other-windows' parameter of WINDOW equals t, do not
2531 process any parameters of WINDOW. Otherwise, if the
2532 `delete-other-windows' parameter of WINDOW specifies a function,
2533 call that function with WINDOW as its sole argument and return
2534 the value returned by that function.
2536 Otherwise, if WINDOW is part of an atomic window, call this
2537 function with the root of the atomic window as its argument. If
2538 WINDOW is a non-side window, make WINDOW the only non-side window
2539 on the frame. Side windows are not deleted. If WINDOW is a side
2540 window signal an error."
2541 (interactive)
2542 (setq window (normalize-any-window window))
2543 (let* ((frame (window-frame window))
2544 (function (window-parameter window 'delete-other-windows))
2545 (window-side (window-parameter window 'window-side))
2546 atom-root side-main)
2547 (window-check frame)
2548 (catch 'done
2549 (cond
2550 ;; Ignore window parameters if `ignore-window-parameters' is t or
2551 ;; `delete-other-windows' is t.
2552 ((or ignore-window-parameters (eq function t)))
2553 ((functionp function)
2554 ;; The `delete-other-windows' parameter specifies the function
2555 ;; to call. If the function is `ignore' no windows are deleted.
2556 ;; It's up to the function called to avoid infinite recursion.
2557 (throw 'done (funcall function window)))
2558 ((and (window-parameter window 'window-atom)
2559 (setq atom-root (window-atom-root window))
2560 (not (eq atom-root window)))
2561 (throw 'done (delete-other-windows atom-root)))
2562 ((eq window-side 'none)
2563 ;; Set side-main to the major non-side window.
2564 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2565 ((memq window-side window-sides)
2566 (error "Cannot make side window the only window")))
2567 ;; If WINDOW is the main non-side window, do nothing.
2568 (unless (eq window side-main)
2569 (delete-other-windows-internal window side-main)
2570 (run-window-configuration-change-hook frame)
2571 (window-check frame))
2572 ;; Always return nil.
2573 nil)))
2575 (defun delete-other-windows-vertically (&optional window)
2576 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2577 This may be a useful alternative binding for \\[delete-other-windows]
2578 if you often split windows horizontally."
2579 (interactive)
2580 (let* ((window (or window (selected-window)))
2581 (edges (window-edges window))
2582 (w window) delenda)
2583 (while (not (eq (setq w (next-window w 1)) window))
2584 (let ((e (window-edges w)))
2585 (when (and (= (car e) (car edges))
2586 (= (caddr e) (caddr edges)))
2587 (push w delenda))))
2588 (mapc 'delete-window delenda)))
2590 ;;; Windows and buffers.
2592 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2593 ;; for (1) determining which buffer to show in the window when its
2594 ;; buffer shall be buried or killed and (2) which buffer to show for
2595 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2597 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2598 ;; triples. The entries on this list are ordered by the time their
2599 ;; buffer has been removed from the window, the most recently removed
2600 ;; buffer's entry being first. The window-start and window-point
2601 ;; components are `window-start' and `window-point' at the time the
2602 ;; buffer was removed from the window which implies that the entry must
2603 ;; be added when `set-window-buffer' removes the buffer from the window.
2605 ;; `next-buffers' is the list of buffers that have been replaced
2606 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2607 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2608 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2609 ;; reset to nil by any action changing the window's buffer with the
2610 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2611 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2612 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2614 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2615 ;; if such a buffer was killed while the window was hidden within a
2616 ;; window configuration. Such killed buffers get removed whenever
2617 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2619 ;; The following function is called by `set-window-buffer' _before_ it
2620 ;; replaces the buffer of the argument window with the new buffer.
2621 (defun record-window-buffer (&optional window)
2622 "Record WINDOW's buffer.
2623 WINDOW must be a live window and defaults to the selected one."
2624 (let* ((window (normalize-live-window window))
2625 (buffer (window-buffer window))
2626 (entry (assq buffer (window-prev-buffers window))))
2627 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2628 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2629 (set-window-next-buffers window nil)
2631 (when entry
2632 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2633 (set-window-prev-buffers
2634 window (assq-delete-all buffer (window-prev-buffers window))))
2636 ;; Don't record insignificant buffers.
2637 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2638 ;; Add an entry for buffer to WINDOW's previous buffers.
2639 (with-current-buffer buffer
2640 (let ((start (window-start window))
2641 (point (window-point window)))
2642 (setq entry
2643 (cons buffer
2644 (if entry
2645 ;; We have an entry, update marker positions.
2646 (list (set-marker (nth 1 entry) start)
2647 (set-marker (nth 2 entry) point))
2648 ;; Make new markers.
2649 (list (copy-marker start)
2650 (copy-marker point)))))
2652 (set-window-prev-buffers
2653 window (cons entry (window-prev-buffers window))))))))
2655 (defun unrecord-window-buffer (&optional window buffer)
2656 "Unrecord BUFFER in WINDOW.
2657 WINDOW must be a live window and defaults to the selected one.
2658 BUFFER must be a live buffer and defaults to the buffer of
2659 WINDOW."
2660 (let* ((window (normalize-live-window window))
2661 (buffer (or buffer (window-buffer window))))
2662 (set-window-prev-buffers
2663 window (assq-delete-all buffer (window-prev-buffers window)))
2664 (set-window-next-buffers
2665 window (delq buffer (window-next-buffers window)))))
2667 (defun set-window-buffer-start-and-point (window buffer &optional start point)
2668 "Set WINDOW's buffer to BUFFER.
2669 Optional argument START non-nil means set WINDOW's start position
2670 to START. Optional argument POINT non-nil means set WINDOW's
2671 point to POINT. If WINDOW is selected this also sets BUFFER's
2672 `point' to POINT. If WINDOW is selected and the buffer it showed
2673 before was current this also makes BUFFER the current buffer."
2674 (let ((selected (eq window (selected-window)))
2675 (current (eq (window-buffer window) (current-buffer))))
2676 (set-window-buffer window buffer)
2677 (when (and selected current)
2678 (set-buffer buffer))
2679 (when start
2680 (set-window-start window start))
2681 (when point
2682 (if selected
2683 (with-current-buffer buffer
2684 (goto-char point))
2685 (set-window-point window point)))))
2687 (defun switch-to-prev-buffer (&optional window bury-or-kill)
2688 "In WINDOW switch to previous buffer.
2689 WINDOW must be a live window and defaults to the selected one.
2691 Optional argument BURY-OR-KILL non-nil means the buffer currently
2692 shown in WINDOW is about to be buried or killed and consequently
2693 shall not be switched to in future invocations of this command."
2694 (interactive)
2695 (let* ((window (normalize-live-window window))
2696 (old-buffer (window-buffer window))
2697 ;; Save this since it's destroyed by `set-window-buffer'.
2698 (next-buffers (window-next-buffers window))
2699 entry new-buffer killed-buffers deletable visible)
2700 (cond
2701 ;; When BURY-OR-KILL is non-nil, there's no previous buffer for
2702 ;; this window, and we can delete the window (or the frame) do
2703 ;; that.
2704 ((and bury-or-kill
2705 (or (not (window-prev-buffers window))
2706 (and (eq (caar (window-prev-buffers window)) old-buffer)
2707 (not (cdr (car (window-prev-buffers window))))))
2708 (setq deletable (window-deletable-p window)))
2709 (if (eq deletable 'frame)
2710 (delete-frame (window-frame window))
2711 (delete-window window)))
2712 ((window-dedicated-p window)
2713 (error "Window %s is dedicated to buffer %s" window old-buffer)))
2715 (unless deletable
2716 (catch 'found
2717 ;; Scan WINDOW's previous buffers first, skipping entries of next
2718 ;; buffers.
2719 (dolist (entry (window-prev-buffers window))
2720 (when (and (setq new-buffer (car entry))
2721 (or (buffer-live-p new-buffer)
2722 (not (setq killed-buffers
2723 (cons new-buffer killed-buffers))))
2724 (not (eq new-buffer old-buffer))
2725 (or bury-or-kill
2726 (not (memq new-buffer next-buffers))))
2727 (set-window-buffer-start-and-point
2728 window new-buffer (nth 1 entry) (nth 2 entry))
2729 (throw 'found t)))
2730 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2731 ;; entries of next buffers. Note that when we bury or kill a
2732 ;; buffer we don't reverse the global buffer list to avoid showing
2733 ;; a buried buffer instead. Otherwise, we must reverse the global
2734 ;; buffer list in order to make sure that switching to the
2735 ;; previous/next buffer traverse it in opposite directions.
2736 (dolist (buffer (if bury-or-kill
2737 (buffer-list (window-frame window))
2738 (nreverse (buffer-list (window-frame window)))))
2739 (when (and (buffer-live-p buffer)
2740 (not (eq buffer old-buffer))
2741 (not (eq (aref (buffer-name buffer) 0) ?\s))
2742 (or bury-or-kill (not (memq buffer next-buffers))))
2743 (if (get-buffer-window buffer)
2744 ;; Try to avoid showing a buffer visible in some other window.
2745 (setq visible buffer)
2746 (setq new-buffer buffer)
2747 (set-window-buffer-start-and-point window new-buffer)
2748 (throw 'found t))))
2749 (unless bury-or-kill
2750 ;; Scan reverted next buffers last (must not use nreverse
2751 ;; here!).
2752 (dolist (buffer (reverse next-buffers))
2753 ;; Actually, buffer _must_ be live here since otherwise it
2754 ;; would have been caught in the scan of previous buffers.
2755 (when (and (or (buffer-live-p buffer)
2756 (not (setq killed-buffers
2757 (cons buffer killed-buffers))))
2758 (not (eq buffer old-buffer))
2759 (setq entry (assq buffer (window-prev-buffers window))))
2760 (setq new-buffer buffer)
2761 (set-window-buffer-start-and-point
2762 window new-buffer (nth 1 entry) (nth 2 entry))
2763 (throw 'found t))))
2765 ;; Show a buffer visible in another window.
2766 (when visible
2767 (setq new-buffer visible)
2768 (set-window-buffer-start-and-point window new-buffer)))
2770 (if bury-or-kill
2771 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2772 ;; of) next buffers.
2773 (progn
2774 (set-window-prev-buffers
2775 window (assq-delete-all old-buffer (window-prev-buffers window)))
2776 (set-window-next-buffers window (delq old-buffer next-buffers)))
2777 ;; Move `old-buffer' to head of WINDOW's restored list of next
2778 ;; buffers.
2779 (set-window-next-buffers
2780 window (cons old-buffer (delq old-buffer next-buffers)))))
2782 ;; Remove killed buffers from WINDOW's previous and next buffers.
2783 (when killed-buffers
2784 (dolist (buffer killed-buffers)
2785 (set-window-prev-buffers
2786 window (assq-delete-all buffer (window-prev-buffers window)))
2787 (set-window-next-buffers
2788 window (delq buffer (window-next-buffers window)))))
2790 ;; Return new-buffer.
2791 new-buffer))
2793 (defun switch-to-next-buffer (&optional window)
2794 "In WINDOW switch to next buffer.
2795 WINDOW must be a live window and defaults to the selected one."
2796 (interactive)
2797 (let* ((window (normalize-live-window window))
2798 (old-buffer (window-buffer window))
2799 (next-buffers (window-next-buffers window))
2800 new-buffer entry killed-buffers visible)
2801 (when (window-dedicated-p window)
2802 (error "Window %s is dedicated to buffer %s" window old-buffer))
2804 (catch 'found
2805 ;; Scan WINDOW's next buffers first.
2806 (dolist (buffer next-buffers)
2807 (when (and (or (buffer-live-p buffer)
2808 (not (setq killed-buffers
2809 (cons buffer killed-buffers))))
2810 (not (eq buffer old-buffer))
2811 (setq entry (assq buffer (window-prev-buffers window))))
2812 (setq new-buffer buffer)
2813 (set-window-buffer-start-and-point
2814 window new-buffer (nth 1 entry) (nth 2 entry))
2815 (throw 'found t)))
2816 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2817 ;; buffers entries.
2818 (dolist (buffer (buffer-list (window-frame window)))
2819 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2820 (not (eq (aref (buffer-name buffer) 0) ?\s))
2821 (not (assq buffer (window-prev-buffers window))))
2822 (if (get-buffer-window buffer)
2823 ;; Try to avoid showing a buffer visible in some other window.
2824 (setq visible buffer)
2825 (setq new-buffer buffer)
2826 (set-window-buffer-start-and-point window new-buffer)
2827 (throw 'found t))))
2828 ;; Scan WINDOW's reverted previous buffers last (must not use
2829 ;; nreverse here!)
2830 (dolist (entry (reverse (window-prev-buffers window)))
2831 (when (and (setq new-buffer (car entry))
2832 (or (buffer-live-p new-buffer)
2833 (not (setq killed-buffers
2834 (cons new-buffer killed-buffers))))
2835 (not (eq new-buffer old-buffer)))
2836 (set-window-buffer-start-and-point
2837 window new-buffer (nth 1 entry) (nth 2 entry))
2838 (throw 'found t)))
2840 ;; Show a buffer visible in another window.
2841 (when visible
2842 (setq new-buffer visible)
2843 (set-window-buffer-start-and-point window new-buffer)))
2845 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2846 (set-window-next-buffers window (delq new-buffer next-buffers))
2848 ;; Remove killed buffers from WINDOW's previous and next buffers.
2849 (when killed-buffers
2850 (dolist (buffer killed-buffers)
2851 (set-window-prev-buffers
2852 window (assq-delete-all buffer (window-prev-buffers window)))
2853 (set-window-next-buffers
2854 window (delq buffer (window-next-buffers window)))))
2856 ;; Return new-buffer.
2857 new-buffer))
2859 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2860 "Search LIST for a valid buffer to display in FRAME.
2861 Return nil when all buffers in LIST are undesirable for display,
2862 otherwise return the first suitable buffer in LIST.
2864 Buffers not visible in windows are preferred to visible buffers,
2865 unless VISIBLE-OK is non-nil.
2866 If the optional argument FRAME is nil, it defaults to the selected frame.
2867 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2868 ;; This logic is more or less copied from other-buffer.
2869 (setq frame (or frame (selected-frame)))
2870 (let ((pred (frame-parameter frame 'buffer-predicate))
2871 found buf)
2872 (while (and (not found) list)
2873 (setq buf (car list))
2874 (if (and (not (eq buffer buf))
2875 (buffer-live-p buf)
2876 (or (null pred) (funcall pred buf))
2877 (not (eq (aref (buffer-name buf) 0) ?\s))
2878 (or visible-ok (null (get-buffer-window buf 'visible))))
2879 (setq found buf)
2880 (setq list (cdr list))))
2881 (car list)))
2883 (defun last-buffer (&optional buffer visible-ok frame)
2884 "Return the last buffer in FRAME's buffer list.
2885 If BUFFER is the last buffer, return the preceding buffer
2886 instead. Buffers not visible in windows are preferred to visible
2887 buffers, unless optional argument VISIBLE-OK is non-nil.
2888 Optional third argument FRAME nil or omitted means use the
2889 selected frame's buffer list. If no such buffer exists, return
2890 the buffer `*scratch*', creating it if necessary."
2891 (setq frame (or frame (selected-frame)))
2892 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2893 buffer visible-ok frame)
2894 (get-buffer "*scratch*")
2895 (let ((scratch (get-buffer-create "*scratch*")))
2896 (set-buffer-major-mode scratch)
2897 scratch)))
2899 (defun bury-buffer (&optional buffer-or-name)
2900 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2901 There it is the least likely candidate for `other-buffer' to
2902 return; thus, the least likely buffer for \\[switch-to-buffer] to
2903 select by default.
2905 You can specify a buffer name as BUFFER-OR-NAME, or an actual
2906 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2907 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2908 remove the current buffer from the selected window if it is
2909 displayed there."
2910 (interactive)
2911 (let* ((buffer (normalize-live-buffer buffer-or-name)))
2912 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2913 ;; although it's not "here" (call it a feature).
2914 (unrecord-buffer buffer)
2915 ;; Handle case where `buffer-or-name' is nil and the current buffer
2916 ;; is shown in the selected window.
2917 (cond
2918 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2919 ((not (window-dedicated-p))
2920 (switch-to-prev-buffer nil 'bury))
2921 ((frame-root-window-p (selected-window))
2922 (iconify-frame (window-frame (selected-window))))
2923 ((window-deletable-p)
2924 (delete-window)))
2925 ;; Always return nil.
2926 nil))
2928 (defun unbury-buffer ()
2929 "Switch to the last buffer in the buffer list."
2930 (interactive)
2931 (switch-to-buffer (last-buffer)))
2933 (defun next-buffer ()
2934 "In selected window switch to next buffer."
2935 (interactive)
2936 (switch-to-next-buffer))
2938 (defun previous-buffer ()
2939 "In selected window switch to previous buffer."
2940 (interactive)
2941 (switch-to-prev-buffer))
2943 (defun delete-windows-on (&optional buffer-or-name frame)
2944 "Delete all windows showing BUFFER-OR-NAME.
2945 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2946 and defaults to the current buffer.
2948 The following non-nil values of the optional argument FRAME
2949 have special meanings:
2951 - t means consider all windows on the selected frame only.
2953 - `visible' means consider all windows on all visible frames on
2954 the current terminal.
2956 - 0 (the number zero) means consider all windows on all visible
2957 and iconified frames on the current terminal.
2959 - A frame means consider all windows on that frame only.
2961 Any other value of FRAME means consider all windows on all
2962 frames.
2964 When a window showing BUFFER-OR-NAME is dedicated and the only
2965 window of its frame, that frame is deleted when there are other
2966 frames left."
2967 (interactive "BDelete windows on (buffer):\nP")
2968 (let ((buffer (normalize-live-buffer buffer-or-name))
2969 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2970 ;; `window-list-1' based function.
2971 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2972 (dolist (window (window-list-1 nil nil all-frames))
2973 (if (eq (window-buffer window) buffer)
2974 (let ((deletable (window-deletable-p window)))
2975 (cond
2976 ((eq deletable 'frame)
2977 ;; Delete frame.
2978 (delete-frame (window-frame window)))
2979 (deletable
2980 ;; Delete window only.
2981 (delete-window window))
2983 ;; In window switch to previous buffer.
2984 (set-window-dedicated-p window nil)
2985 (switch-to-prev-buffer window 'bury))))
2986 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
2987 (unrecord-window-buffer window buffer)))))
2989 (defun replace-buffer-in-windows (&optional buffer-or-name)
2990 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2991 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2992 and defaults to the current buffer.
2994 When a window showing BUFFER-OR-NAME is either dedicated, or the
2995 window has no previous buffer, that window is deleted. If that
2996 window is the only window on its frame, the frame is deleted too
2997 when there are other frames left. If there are no other frames
2998 left, some other buffer is displayed in that window.
3000 This function removes the buffer denoted by BUFFER-OR-NAME from
3001 all window-local buffer lists."
3002 (let ((buffer (normalize-live-buffer buffer-or-name)))
3003 (dolist (window (window-list-1 nil nil t))
3004 (if (eq (window-buffer window) buffer)
3005 (let ((deletable (window-deletable-p window)))
3006 (cond
3007 ((eq deletable 'frame)
3008 ;; Delete frame.
3009 (delete-frame (window-frame window)))
3010 ((and (window-dedicated-p window) deletable)
3011 ;; Delete window.
3012 (delete-window window))
3014 ;; Switch to another buffer in window.
3015 (set-window-dedicated-p window nil)
3016 (switch-to-prev-buffer window 'kill))))
3017 ;; Unrecord BUFFER in WINDOW.
3018 (unrecord-window-buffer window buffer)))))
3020 (defun quit-restore-window (&optional window kill)
3021 "Quit WINDOW in some way.
3022 WINDOW must be a live window and defaults to the selected window.
3023 Return nil.
3025 According to information stored in WINDOW's `quit-restore' window
3026 parameter either \(1) delete WINDOW and its frame, \(2) delete
3027 WINDOW, \(3) restore the buffer previously displayed in WINDOW,
3028 or \(4) make WINDOW display some other buffer than the present
3029 one. If non-nil, reset `quit-restore' parameter to nil.
3031 Optional argument KILL non-nil means in addition kill WINDOW's
3032 buffer. If KILL is nil, put WINDOW's buffer at the end of the
3033 buffer list. Interactively, KILL is the prefix argument."
3034 (interactive "i\nP")
3035 (setq window (normalize-live-window window))
3036 (let ((buffer (window-buffer window))
3037 (quit-restore (window-parameter window 'quit-restore))
3038 deletable resize)
3039 (cond
3040 ((and (or (and (memq (car-safe quit-restore) '(new-window new-frame))
3041 ;; Check that WINDOW's buffer is still the same.
3042 (eq (window-buffer window) (nth 1 quit-restore)))
3043 (window-dedicated-p window))
3044 (setq deletable (window-deletable-p window)))
3045 ;; WINDOW can be deleted.
3046 (unrecord-buffer buffer)
3047 (if (eq deletable 'frame)
3048 ;; WINDOW's frame can be deleted.
3049 (delete-frame (window-frame window))
3050 ;; Just delete WINDOW.
3051 (delete-window window))
3052 ;; If the previously selected window is still alive, select it.
3053 (when (window-live-p (nth 2 quit-restore))
3054 (select-window (nth 2 quit-restore))))
3055 ((and (buffer-live-p (nth 0 quit-restore))
3056 ;; The buffer currently shown in WINDOW must still be the
3057 ;; buffer shown when its `quit-restore' parameter was created
3058 ;; in the first place.
3059 (eq (window-buffer window) (nth 3 quit-restore)))
3060 (setq resize (with-current-buffer buffer temp-buffer-resize-mode))
3061 ;; Unrecord buffer.
3062 (unrecord-buffer buffer)
3063 (unrecord-window-buffer window buffer)
3064 ;; Display buffer stored in the quit-restore parameter.
3065 (set-window-dedicated-p window nil)
3066 (set-window-buffer window (nth 0 quit-restore))
3067 (set-window-start window (nth 1 quit-restore))
3068 (set-window-point window (nth 2 quit-restore))
3069 (when (and resize (/= (nth 4 quit-restore) (window-total-size window)))
3070 (resize-window
3071 window (- (nth 4 quit-restore) (window-total-size window))))
3072 ;; Reset the quit-restore parameter.
3073 (set-window-parameter window 'quit-restore nil)
3074 (when (window-live-p (nth 5 quit-restore))
3075 (select-window (nth 5 quit-restore))))
3077 ;; Otherwise, show another buffer in WINDOW and reset the
3078 ;; quit-restore parameter.
3079 (set-window-parameter window 'quit-restore nil)
3080 (unrecord-buffer buffer)
3081 (switch-to-prev-buffer window 'bury-or-kill)))
3083 ;; Kill WINDOW's old-buffer if requested
3084 (when kill (kill-buffer buffer))
3085 nil))
3087 ;;; Splitting windows.
3088 (defsubst window-split-min-size (&optional horizontal)
3089 "Return minimum height of any window when splitting windows.
3090 Optional argument HORIZONTAL non-nil means return minimum width."
3091 (if horizontal
3092 (max window-min-width window-safe-min-width)
3093 (max window-min-height window-safe-min-height)))
3095 (defun split-window (&optional window size side)
3096 "Make a new window adjacent to WINDOW.
3097 WINDOW can be any window and defaults to the selected one.
3098 Return the new window which is always a live window.
3100 Optional argument SIZE a positive number means make WINDOW SIZE
3101 lines or columns tall. If SIZE is negative, make the new window
3102 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3103 absolute value can be less than `window-min-height' or
3104 `window-min-width'; so this command can make a new window as
3105 small as one line or two columns. SIZE defaults to half of
3106 WINDOW's size. Interactively, SIZE is the prefix argument.
3108 Optional third argument SIDE nil (or `below') specifies that the
3109 new window shall be located below WINDOW. SIDE `above' means the
3110 new window shall be located above WINDOW. In both cases SIZE
3111 specifies the new number of lines for WINDOW \(or the new window
3112 if SIZE is negative) including space reserved for the mode and/or
3113 header line.
3115 SIDE t (or `right') specifies that the new window shall be
3116 located on the right side of WINDOW. SIDE `left' means the new
3117 window shall be located on the left of WINDOW. In both cases
3118 SIZE specifies the new number of columns for WINDOW \(or the new
3119 window provided SIZE is negative) including space reserved for
3120 fringes and the scrollbar or a divider column. Any other non-nil
3121 value for SIDE is currently handled like t (or `right').
3123 If the variable `ignore-window-parameters' is non-nil or the
3124 `split-window' parameter of WINDOW equals t, do not process any
3125 parameters of WINDOW. Otherwise, if the `split-window' parameter
3126 of WINDOW specifies a function, call that function with all three
3127 arguments and return the value returned by that function.
3129 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3130 root of that atomic window. The new window does not become a
3131 member of that atomic window.
3133 If WINDOW is live, properties of the new window like margins and
3134 scrollbars are inherited from WINDOW. If WINDOW is an internal
3135 window, these properties as well as the buffer displayed in the
3136 new window are inherited from the window selected on WINDOW's
3137 frame. The selected window is not changed by this function."
3138 (interactive "i")
3139 (setq window (normalize-any-window window))
3140 (let* ((horizontal (not (memq side '(nil below above))))
3141 (frame (window-frame window))
3142 (parent (window-parent window))
3143 (function (window-parameter window 'split-window))
3144 (window-side (window-parameter window 'window-side))
3145 ;; Rebind `window-nest' since in some cases we may have to
3146 ;; override its value.
3147 (window-nest window-nest)
3148 atom-root)
3150 (window-check frame)
3151 (catch 'done
3152 (cond
3153 ;; Ignore window parameters if either `ignore-window-parameters'
3154 ;; is t or the `split-window' parameter equals t.
3155 ((or ignore-window-parameters (eq function t)))
3156 ((functionp function)
3157 ;; The `split-window' parameter specifies the function to call.
3158 ;; If that function is `ignore', do nothing.
3159 (throw 'done (funcall function window size side)))
3160 ;; If WINDOW is a subwindow of an atomic window, split the root
3161 ;; window of that atomic window instead.
3162 ((and (window-parameter window 'window-atom)
3163 (setq atom-root (window-atom-root window))
3164 (not (eq atom-root window)))
3165 (throw 'done (split-window atom-root size side))))
3167 (when (and window-side
3168 (or (not parent)
3169 (not (window-parameter parent 'window-side))))
3170 ;; WINDOW is a side root window. To make sure that a new parent
3171 ;; window gets created set `window-nest' to t.
3172 (setq window-nest t))
3174 (when (and window-splits size (> size 0))
3175 ;; If `window-splits' is non-nil and SIZE is a non-negative
3176 ;; integer, we cannot reasonably resize other windows. Rather
3177 ;; bind `window-nest' to t to make sure that subsequent window
3178 ;; deletions are handled correctly.
3179 (setq window-nest t))
3181 (let* ((parent-size
3182 ;; `parent-size' is the size of WINDOW's parent, provided
3183 ;; it has one.
3184 (when parent (window-total-size parent horizontal)))
3185 ;; `resize' non-nil means we are supposed to resize other
3186 ;; windows in WINDOW's combination.
3187 (resize
3188 (and window-splits (not window-nest)
3189 ;; Resize makes sense in iso-combinations only.
3190 (window-iso-combined-p window horizontal)))
3191 ;; `old-size' is the current size of WINDOW.
3192 (old-size (window-total-size window horizontal))
3193 ;; `new-size' is the specified or calculated size of the
3194 ;; new window.
3195 (new-size
3196 (cond
3197 ((not size)
3198 (max (window-split-min-size horizontal)
3199 (if resize
3200 ;; When resizing try to give the new window the
3201 ;; average size of a window in its combination.
3202 (min (- parent-size
3203 (window-min-size parent horizontal))
3204 (/ parent-size
3205 (1+ (window-iso-combinations
3206 parent horizontal))))
3207 ;; Else try to give the new window half the size
3208 ;; of WINDOW (plus an eventual odd line).
3209 (+ (/ old-size 2) (% old-size 2)))))
3210 ((>= size 0)
3211 ;; SIZE non-negative specifies the new size of WINDOW.
3213 ;; Note: Specifying a non-negative SIZE is practically
3214 ;; always done as workaround for making the new window
3215 ;; appear above or on the left of the new window (the
3216 ;; ispell window is a typical example of that). In all
3217 ;; these cases the SIDE argument should be set to 'above
3218 ;; or 'left in order to support the 'resize option.
3219 ;; Here we have to nest the windows instead, see above.
3220 (- old-size size))
3222 ;; SIZE negative specifies the size of the new window.
3223 (- size))))
3224 new-parent new-normal)
3226 ;; Check SIZE.
3227 (cond
3228 ((not size)
3229 (cond
3230 (resize
3231 ;; SIZE unspecified, resizing.
3232 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3233 ;; Try again with minimum split size.
3234 (setq new-size
3235 (max new-size (window-split-min-size horizontal)))
3236 (not (window-sizable-p parent (- new-size) horizontal)))
3237 (error "Window %s too small for splitting" parent)))
3238 ((> (+ new-size (window-min-size window horizontal)) old-size)
3239 ;; SIZE unspecified, no resizing.
3240 (error "Window %s too small for splitting" window))))
3241 ((and (>= size 0)
3242 (or (>= size old-size)
3243 (< new-size (if horizontal
3244 window-safe-min-width
3245 window-safe-min-width))))
3246 ;; SIZE specified as new size of old window. If the new size
3247 ;; is larger than the old size or the size of the new window
3248 ;; would be less than the safe minimum, signal an error.
3249 (error "Window %s too small for splitting" window))
3250 (resize
3251 ;; SIZE specified, resizing.
3252 (unless (window-sizable-p parent (- new-size) horizontal)
3253 ;; If we cannot resize the parent give up.
3254 (error "Window %s too small for splitting" parent)))
3255 ((or (< new-size
3256 (if horizontal window-safe-min-width window-safe-min-height))
3257 (< (- old-size new-size)
3258 (if horizontal window-safe-min-width window-safe-min-height)))
3259 ;; SIZE specification violates minimum size restrictions.
3260 (error "Window %s too small for splitting" window)))
3262 (resize-window-reset frame horizontal)
3264 (setq new-parent
3265 ;; Make new-parent non-nil if we need a new parent window;
3266 ;; either because we want to nest or because WINDOW is not
3267 ;; iso-combined.
3268 (or window-nest (not (window-iso-combined-p window horizontal))))
3269 (setq new-normal
3270 ;; Make new-normal the normal size of the new window.
3271 (cond
3272 (size (/ (float new-size) (if new-parent old-size parent-size)))
3273 (new-parent 0.5)
3274 (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal))))
3275 (t (/ (window-normal-size window horizontal) 2.0))))
3277 (if resize
3278 ;; Try to get space from OLD's siblings. We could go "up" and
3279 ;; try getting additional space from surrounding windows but
3280 ;; we won't be able to return space to those windows when we
3281 ;; delete the one we create here. Hence we do not go up.
3282 (progn
3283 (resize-subwindows parent (- new-size) horizontal)
3284 (let* ((normal (- 1.0 new-normal))
3285 (sub (window-child parent)))
3286 (while sub
3287 (set-window-new-normal
3288 sub (* (window-normal-size sub horizontal) normal))
3289 (setq sub (window-right sub)))))
3290 ;; Get entire space from WINDOW.
3291 (set-window-new-total window (- old-size new-size))
3292 (resize-this-window window (- new-size) horizontal)
3293 (set-window-new-normal
3294 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3295 new-normal)))
3297 (let* ((new (split-window-internal window new-size side new-normal)))
3298 ;; Inherit window-side parameters, if any.
3299 (when (and window-side new-parent)
3300 (set-window-parameter (window-parent new) 'window-side window-side)
3301 (set-window-parameter new 'window-side window-side))
3303 (run-window-configuration-change-hook frame)
3304 (window-check frame)
3305 ;; Always return the new window.
3306 new)))))
3308 ;; I think this should be the default; I think people will prefer it--rms.
3309 (defcustom split-window-keep-point t
3310 "If non-nil, \\[split-window-above-each-other] keeps the original point \
3311 in both children.
3312 This is often more convenient for editing.
3313 If nil, adjust point in each of the two windows to minimize redisplay.
3314 This is convenient on slow terminals, but point can move strangely.
3316 This option applies only to `split-window-above-each-other' and
3317 functions that call it. `split-window' always keeps the original
3318 point in both children."
3319 :type 'boolean
3320 :group 'windows)
3322 (defun split-window-above-each-other (&optional size)
3323 "Split selected window into two windows, one above the other.
3324 The upper window gets SIZE lines and the lower one gets the rest.
3325 SIZE negative means the lower window gets -SIZE lines and the
3326 upper one the rest. With no argument, split windows equally or
3327 close to it. Both windows display the same buffer, now current.
3329 If the variable `split-window-keep-point' is non-nil, both new
3330 windows will get the same value of point as the selected window.
3331 This is often more convenient for editing. The upper window is
3332 the selected window.
3334 Otherwise, we choose window starts so as to minimize the amount of
3335 redisplay; this is convenient on slow terminals. The new selected
3336 window is the one that the current value of point appears in. The
3337 value of point can change if the text around point is hidden by the
3338 new mode line.
3340 Regardless of the value of `split-window-keep-point', the upper
3341 window is the original one and the return value is the new, lower
3342 window."
3343 (interactive "P")
3344 (let ((old-window (selected-window))
3345 (old-point (point))
3346 (size (and size (prefix-numeric-value size)))
3347 moved-by-window-height moved new-window bottom)
3348 (when (and size (< size 0) (< (- size) window-min-height))
3349 ;; `split-window' would not signal an error here.
3350 (error "Size of new window too small"))
3351 (setq new-window (split-window nil size))
3352 (unless split-window-keep-point
3353 (with-current-buffer (window-buffer)
3354 (goto-char (window-start))
3355 (setq moved (vertical-motion (window-height)))
3356 (set-window-start new-window (point))
3357 (when (> (point) (window-point new-window))
3358 (set-window-point new-window (point)))
3359 (when (= moved (window-height))
3360 (setq moved-by-window-height t)
3361 (vertical-motion -1))
3362 (setq bottom (point)))
3363 (and moved-by-window-height
3364 (<= bottom (point))
3365 (set-window-point old-window (1- bottom)))
3366 (and moved-by-window-height
3367 (<= (window-start new-window) old-point)
3368 (set-window-point new-window old-point)
3369 (select-window new-window)))
3370 ;; Always copy quit-restore parameter in interactive use.
3371 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3372 (when quit-restore
3373 (set-window-parameter new-window 'quit-restore quit-restore)))
3374 new-window))
3376 (defalias 'split-window-vertically 'split-window-above-each-other)
3378 (defun split-window-side-by-side (&optional size)
3379 "Split selected window into two windows side by side.
3380 The selected window becomes the left one and gets SIZE columns.
3381 SIZE negative means the right window gets -SIZE lines.
3383 SIZE includes the width of the window's scroll bar; if there are
3384 no scroll bars, it includes the width of the divider column to
3385 the window's right, if any. SIZE omitted or nil means split
3386 window equally.
3388 The selected window remains selected. Return the new window."
3389 (interactive "P")
3390 (let ((old-window (selected-window))
3391 (size (and size (prefix-numeric-value size)))
3392 new-window)
3393 (when (and size (< size 0) (< (- size) window-min-width))
3394 ;; `split-window' would not signal an error here.
3395 (error "Size of new window too small"))
3396 (setq new-window (split-window nil size t))
3397 ;; Always copy quit-restore parameter in interactive use.
3398 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3399 (when quit-restore
3400 (set-window-parameter new-window 'quit-restore quit-restore)))
3401 new-window))
3403 (defalias 'split-window-horizontally 'split-window-side-by-side)
3405 ;;; Balancing windows.
3407 ;; The following routine uses the recycled code from an old version of
3408 ;; `resize-subwindows'. It's not very pretty, but coding it the way the
3409 ;; new `resize-subwindows' code does would hardly make it any shorter or
3410 ;; more readable (FWIW we'd need three loops - one to calculate the
3411 ;; minimum sizes per window, one to enlarge or shrink windows until the
3412 ;; new parent-size matches, and one where we shrink the largest/enlarge
3413 ;; the smallest window).
3414 (defun balance-windows-2 (window horizontal)
3415 "Subroutine of `balance-windows-1'.
3416 WINDOW must be an iso-combination."
3417 (let* ((first (window-child window))
3418 (sub first)
3419 (number-of-children 0)
3420 (parent-size (window-new-total window))
3421 (total-sum parent-size)
3422 found failed size sub-total sub-delta sub-amount rest)
3423 (while sub
3424 (setq number-of-children (1+ number-of-children))
3425 (when (window-size-fixed-p sub horizontal)
3426 (setq total-sum
3427 (- total-sum (window-total-size sub horizontal)))
3428 (set-window-new-normal sub 'ignore))
3429 (setq sub (window-right sub)))
3431 (setq failed t)
3432 (while (and failed (> number-of-children 0))
3433 (setq size (/ total-sum number-of-children))
3434 (setq failed nil)
3435 (setq sub first)
3436 (while (and sub (not failed))
3437 ;; Ignore subwindows that should be ignored or are stuck.
3438 (unless (resize-subwindows-skip-p sub)
3439 (setq found t)
3440 (setq sub-total (window-total-size sub horizontal))
3441 (setq sub-delta (- size sub-total))
3442 (setq sub-amount
3443 (window-sizable sub sub-delta horizontal))
3444 ;; Register the new total size for this subwindow.
3445 (set-window-new-total sub (+ sub-total sub-amount))
3446 (unless (= sub-amount sub-delta)
3447 (setq total-sum (- total-sum sub-total sub-amount))
3448 (setq number-of-children (1- number-of-children))
3449 ;; We failed and need a new round.
3450 (setq failed t)
3451 (set-window-new-normal sub 'skip)))
3452 (setq sub (window-right sub))))
3454 (setq rest (% total-sum number-of-children))
3455 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3456 ;; (column) until `rest' is zero.
3457 (setq sub first)
3458 (while (and sub (> rest 0))
3459 (unless (resize-subwindows-skip-p window)
3460 (set-window-new-total sub 1 t)
3461 (setq rest (1- rest)))
3462 (setq sub (window-right sub)))
3464 ;; Fix rounding by trying to enlarge stuck windows by one line
3465 ;; (column) until `rest' equals zero.
3466 (setq sub first)
3467 (while (and sub (> rest 0))
3468 (unless (eq (window-new-normal sub) 'ignore)
3469 (set-window-new-total sub 1 t)
3470 (setq rest (1- rest)))
3471 (setq sub (window-right sub)))
3473 (setq sub first)
3474 (while sub
3475 ;; Record new normal sizes.
3476 (set-window-new-normal
3477 sub (/ (if (eq (window-new-normal sub) 'ignore)
3478 (window-total-size sub horizontal)
3479 (window-new-total sub))
3480 (float parent-size)))
3481 ;; Recursively balance each subwindow's subwindows.
3482 (balance-windows-1 sub horizontal)
3483 (setq sub (window-right sub)))))
3485 (defun balance-windows-1 (window &optional horizontal)
3486 "Subroutine of `balance-windows'."
3487 (if (window-child window)
3488 (let ((sub (window-child window)))
3489 (if (window-iso-combined-p sub horizontal)
3490 (balance-windows-2 window horizontal)
3491 (let ((size (window-new-total window)))
3492 (while sub
3493 (set-window-new-total sub size)
3494 (balance-windows-1 sub horizontal)
3495 (setq sub (window-right sub))))))))
3497 (defun balance-windows (&optional window-or-frame)
3498 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3499 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3500 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3501 subwindows of that frame's root window. If WINDOW-OR-FRAME
3502 denots a window, balance the sizes of all subwindows of that
3503 window."
3504 (interactive)
3505 (let* ((window
3506 (cond
3507 ((or (not window-or-frame)
3508 (frame-live-p window-or-frame))
3509 (frame-root-window window-or-frame))
3510 ((or (window-live-p window-or-frame)
3511 (window-child window-or-frame))
3512 window-or-frame)
3514 (error "Not a window or frame %s" window-or-frame))))
3515 (frame (window-frame window)))
3516 ;; Balance vertically.
3517 (resize-window-reset (window-frame window))
3518 (balance-windows-1 window)
3519 (resize-window-apply frame)
3520 ;; Balance horizontally.
3521 (resize-window-reset (window-frame window) t)
3522 (balance-windows-1 window t)
3523 (resize-window-apply frame t)))
3525 (defun window-fixed-size-p (&optional window direction)
3526 "Return t if WINDOW cannot be resized in DIRECTION.
3527 WINDOW defaults to the selected window. DIRECTION can be
3528 nil (i.e. any), `height' or `width'."
3529 (with-current-buffer (window-buffer window)
3530 (when (and (boundp 'window-size-fixed) window-size-fixed)
3531 (not (and direction
3532 (member (cons direction window-size-fixed)
3533 '((height . width) (width . height))))))))
3535 ;;; A different solution to balance-windows.
3536 (defvar window-area-factor 1
3537 "Factor by which the window area should be over-estimated.
3538 This is used by `balance-windows-area'.
3539 Changing this globally has no effect.")
3540 (make-variable-buffer-local 'window-area-factor)
3542 (defun balance-windows-area-adjust (window delta horizontal)
3543 "Wrapper around `resize-window' with error checking.
3544 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3545 ;; `resize-window' may fail if delta is too large.
3546 (while (>= (abs delta) 1)
3547 (condition-case nil
3548 (progn
3549 (resize-window window delta horizontal)
3550 (setq delta 0))
3551 (error
3552 ;;(message "adjust: %s" (error-message-string err))
3553 (setq delta (/ delta 2))))))
3555 (defun balance-windows-area ()
3556 "Make all visible windows the same area (approximately).
3557 See also `window-area-factor' to change the relative size of
3558 specific buffers."
3559 (interactive)
3560 (let* ((unchanged 0) (carry 0) (round 0)
3561 ;; Remove fixed-size windows.
3562 (wins (delq nil (mapcar (lambda (win)
3563 (if (not (window-fixed-size-p win)) win))
3564 (window-list nil 'nomini))))
3565 (changelog nil)
3566 next)
3567 ;; Resizing a window changes the size of surrounding windows in complex
3568 ;; ways, so it's difficult to balance them all. The introduction of
3569 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3570 ;; very difficult to do. `balance-window' above takes an off-line
3571 ;; approach: get the whole window tree, then balance it, then try to
3572 ;; adjust the windows so they fit the result.
3573 ;; Here, instead, we take a "local optimization" approach, where we just
3574 ;; go through all the windows several times until nothing needs to be
3575 ;; changed. The main problem with this approach is that it's difficult
3576 ;; to make sure it terminates, so we use some heuristic to try and break
3577 ;; off infinite loops.
3578 ;; After a round without any change, we allow a second, to give a chance
3579 ;; to the carry to propagate a minor imbalance from the end back to
3580 ;; the beginning.
3581 (while (< unchanged 2)
3582 ;; (message "New round")
3583 (setq unchanged (1+ unchanged) round (1+ round))
3584 (dolist (win wins)
3585 (setq next win)
3586 (while (progn (setq next (next-window next))
3587 (window-fixed-size-p next)))
3588 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3589 (let* ((horiz
3590 (< (car (window-edges win)) (car (window-edges next))))
3591 (areadiff (/ (- (* (window-height next) (window-width next)
3592 (buffer-local-value 'window-area-factor
3593 (window-buffer next)))
3594 (* (window-height win) (window-width win)
3595 (buffer-local-value 'window-area-factor
3596 (window-buffer win))))
3597 (max (buffer-local-value 'window-area-factor
3598 (window-buffer win))
3599 (buffer-local-value 'window-area-factor
3600 (window-buffer next)))))
3601 (edgesize (if horiz
3602 (+ (window-height win) (window-height next))
3603 (+ (window-width win) (window-width next))))
3604 (diff (/ areadiff edgesize)))
3605 (when (zerop diff)
3606 ;; Maybe diff is actually closer to 1 than to 0.
3607 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3608 (when (and (zerop diff) (not (zerop areadiff)))
3609 (setq diff (/ (+ areadiff carry) edgesize))
3610 ;; Change things smoothly.
3611 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3612 (if (zerop diff)
3613 ;; Make sure negligible differences don't accumulate to
3614 ;; become significant.
3615 (setq carry (+ carry areadiff))
3616 ;; This used `adjust-window-trailing-edge' before and uses
3617 ;; `resize-window' now. Error wrapping is still needed.
3618 (balance-windows-area-adjust win diff horiz)
3619 ;; (sit-for 0.5)
3620 (let ((change (cons win (window-edges win))))
3621 ;; If the same change has been seen already for this window,
3622 ;; we're most likely in an endless loop, so don't count it as
3623 ;; a change.
3624 (unless (member change changelog)
3625 (push change changelog)
3626 (setq unchanged 0 carry 0)))))))
3627 ;; We've now basically balanced all the windows.
3628 ;; But there may be some minor off-by-one imbalance left over,
3629 ;; so let's do some fine tuning.
3630 ;; (bw-finetune wins)
3631 ;; (message "Done in %d rounds" round)
3634 ;;; Window states, how to get them and how to put them in a window.
3635 (defsubst window-list-no-nils (&rest args)
3636 "Like LIST but do not add nil elements of ARGS."
3637 (delq nil (apply 'list args)))
3639 (defvar window-state-ignored-parameters '(quit-restore)
3640 "List of window parameters ignored by `window-state-get'.")
3642 (defun window-state-get-1 (window &optional markers)
3643 "Helper function for `window-state-get'."
3644 (let* ((type
3645 (cond
3646 ((window-vchild window) 'vc)
3647 ((window-hchild window) 'hc)
3648 (t 'leaf)))
3649 (buffer (window-buffer window))
3650 (selected (eq window (selected-window)))
3651 (head
3652 (window-list-no-nils
3653 type
3654 (unless (window-next window) (cons 'last t))
3655 (cons 'clone-number (window-clone-number window))
3656 (cons 'total-height (window-total-size window))
3657 (cons 'total-width (window-total-size window t))
3658 (cons 'normal-height (window-normal-size window))
3659 (cons 'normal-width (window-normal-size window t))
3660 (cons 'splits (window-splits window))
3661 (cons 'nest (window-nest window))
3662 (let (list)
3663 (dolist (parameter (window-parameters window))
3664 (unless (memq (car parameter)
3665 window-state-ignored-parameters)
3666 (setq list (cons parameter list))))
3667 (when list
3668 (cons 'parameters list)))
3669 (when buffer
3670 ;; All buffer related things go in here - make the buffer
3671 ;; current when retrieving `point' and `mark'.
3672 (with-current-buffer (window-buffer window)
3673 (let ((point (if selected (point) (window-point window)))
3674 (start (window-start window))
3675 (mark (mark)))
3676 (window-list-no-nils
3677 'buffer (buffer-name buffer)
3678 (cons 'selected selected)
3679 (when window-size-fixed (cons 'size-fixed window-size-fixed))
3680 (cons 'hscroll (window-hscroll window))
3681 (cons 'fringes (window-fringes window))
3682 (cons 'margins (window-margins window))
3683 (cons 'scroll-bars (window-scroll-bars window))
3684 (cons 'vscroll (window-vscroll window))
3685 (cons 'dedicated (window-dedicated-p window))
3686 (cons 'point (if markers (copy-marker point) point))
3687 (cons 'start (if markers (copy-marker start) start))
3688 (when mark
3689 (cons 'mark (if markers (copy-marker mark) mark)))))))))
3690 (tail
3691 (when (memq type '(vc hc))
3692 (let (list)
3693 (setq window (window-child window))
3694 (while window
3695 (setq list (cons (window-state-get-1 window markers) list))
3696 (setq window (window-right window)))
3697 (nreverse list)))))
3698 (append head tail)))
3700 (defun window-state-get (&optional window markers)
3701 "Return state of WINDOW as a Lisp object.
3702 WINDOW can be any window and defaults to the root window of the
3703 selected frame.
3705 Optional argument MARKERS non-nil means use markers for sampling
3706 positions like `window-point' or `window-start'. MARKERS should
3707 be non-nil only if the value is used for putting the state back
3708 in the same session (note that markers slow down processing).
3710 The return value can be used as argument for `window-state-put'
3711 to put the state recorded here into an arbitrary window. The
3712 value can be also stored on disk and read back in a new session."
3713 (setq window
3714 (if window
3715 (if (window-any-p window)
3716 window
3717 (error "%s is not a live or internal window" window))
3718 (frame-root-window)))
3719 ;; The return value is a cons whose car specifies some constraints on
3720 ;; the size of WINDOW. The cdr lists the states of the subwindows of
3721 ;; WINDOW.
3722 (cons
3723 ;; Frame related things would go into a function, say `frame-state',
3724 ;; calling `window-state-get' to insert the frame's root window.
3725 (window-list-no-nils
3726 (cons 'min-height (window-min-size window))
3727 (cons 'min-width (window-min-size window t))
3728 (cons 'min-height-ignore (window-min-size window nil t))
3729 (cons 'min-width-ignore (window-min-size window t t))
3730 (cons 'min-height-safe (window-min-size window nil 'safe))
3731 (cons 'min-width-safe (window-min-size window t 'safe))
3732 ;; These are probably not needed.
3733 (when (window-size-fixed-p window) (cons 'fixed-height t))
3734 (when (window-size-fixed-p window t) (cons 'fixed-width t)))
3735 (window-state-get-1 window markers)))
3737 (defvar window-state-put-list nil
3738 "Helper variable for `window-state-put'.")
3740 (defun window-state-put-1 (state &optional window ignore totals)
3741 "Helper function for `window-state-put'."
3742 (let ((type (car state)))
3743 (setq state (cdr state))
3744 (cond
3745 ((eq type 'leaf)
3746 ;; For a leaf window just add unprocessed entries to
3747 ;; `window-state-put-list'.
3748 (setq window-state-put-list
3749 (cons (cons window state) window-state-put-list)))
3750 ((memq type '(vc hc))
3751 (let* ((horizontal (eq type 'hc))
3752 (total (window-total-size window horizontal))
3753 (first t)
3754 size new)
3755 (dolist (item state)
3756 ;; Find the next child window. WINDOW always points to the
3757 ;; real window that we want to fill with what we find here.
3758 (when (memq (car item) '(leaf vc hc))
3759 (if (assq 'last item)
3760 ;; The last child window. Below `window-state-put-1'
3761 ;; will put into it whatever ITEM has in store.
3762 (setq new nil)
3763 ;; Not the last child window, prepare for splitting
3764 ;; WINDOW. SIZE is the new (and final) size of the old
3765 ;; window.
3766 (setq size
3767 (if totals
3768 ;; Use total size.
3769 (cdr (assq (if horizontal 'total-width 'total-height) item))
3770 ;; Use normalized size and round.
3771 (round (* total
3772 (cdr (assq
3773 (if horizontal 'normal-width 'normal-height)
3774 item))))))
3776 ;; Use safe sizes, we try to resize later.
3777 (setq size (max size (if horizontal
3778 window-safe-min-height
3779 window-safe-min-width)))
3781 (if (window-sizable-p window (- size) horizontal 'safe)
3782 (let* ((window-nest (assq 'nest item)))
3783 ;; We must inherit the nesting, otherwise we might mess
3784 ;; up handling of atomic and side window.
3785 (setq new (split-window window size horizontal)))
3786 ;; Give up if we can't resize window down to safe sizes.
3787 (error "Cannot resize window %s" window))
3789 (when first
3790 (setq first nil)
3791 ;; When creating the first child window add for parent
3792 ;; unprocessed entries to `window-state-put-list'.
3793 (setq window-state-put-list
3794 (cons (cons (window-parent window) state)
3795 window-state-put-list))))
3797 ;; Now process the current window (either the one we've just
3798 ;; split or the last child of its parent).
3799 (window-state-put-1 item window ignore totals)
3800 ;; Continue with the last window split off.
3801 (setq window new))))))))
3803 (defun window-state-put-2 (ignore)
3804 "Helper function for `window-state-put'."
3805 (dolist (item window-state-put-list)
3806 (let ((window (car item))
3807 (clone-number (cdr (assq 'clone-number item)))
3808 (splits (cdr (assq 'splits item)))
3809 (nest (cdr (assq 'nest item)))
3810 (parameters (cdr (assq 'parameters item)))
3811 (state (cdr (assq 'buffer item))))
3812 ;; Put in clone-number.
3813 (when clone-number (set-window-clone-number window clone-number))
3814 (when splits (set-window-splits window splits))
3815 (when nest (set-window-nest window nest))
3816 ;; Process parameters.
3817 (when parameters
3818 (dolist (parameter parameters)
3819 (set-window-parameter window (car parameter) (cdr parameter))))
3820 ;; Process buffer related state.
3821 (when state
3822 ;; We don't want to raise an error here so we create a buffer if
3823 ;; there's none.
3824 (set-window-buffer window (get-buffer-create (car state)))
3825 (with-current-buffer (window-buffer window)
3826 (set-window-hscroll window (cdr (assq 'hscroll state)))
3827 (apply 'set-window-fringes
3828 (cons window (cdr (assq 'fringes state))))
3829 (let ((margins (cdr (assq 'margins state))))
3830 (set-window-margins window (car margins) (cdr margins)))
3831 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
3832 (set-window-scroll-bars
3833 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
3834 (set-window-vscroll window (cdr (assq 'vscroll state)))
3835 ;; Adjust vertically.
3836 (if (memq window-size-fixed '(t height))
3837 ;; A fixed height window, try to restore the original size.
3838 (let ((delta (- (cdr (assq 'total-height item))
3839 (window-total-height window)))
3840 window-size-fixed)
3841 (when (window-resizable-p window delta)
3842 (resize-window window delta)))
3843 ;; Else check whether the window is not high enough.
3844 (let* ((min-size (window-min-size window nil ignore))
3845 (delta (- min-size (window-total-size window))))
3846 (when (and (> delta 0)
3847 (window-resizable-p window delta nil ignore))
3848 (resize-window window delta nil ignore))))
3849 ;; Adjust horizontally.
3850 (if (memq window-size-fixed '(t width))
3851 ;; A fixed width window, try to restore the original size.
3852 (let ((delta (- (cdr (assq 'total-width item))
3853 (window-total-width window)))
3854 window-size-fixed)
3855 (when (window-resizable-p window delta)
3856 (resize-window window delta)))
3857 ;; Else check whether the window is not wide enough.
3858 (let* ((min-size (window-min-size window t ignore))
3859 (delta (- min-size (window-total-size window t))))
3860 (when (and (> delta 0)
3861 (window-resizable-p window delta t ignore))
3862 (resize-window window delta t ignore))))
3863 ;; Set dedicated status.
3864 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
3865 ;; Install positions (maybe we should do this after all windows
3866 ;; have been created and sized).
3867 (ignore-errors
3868 (set-window-start window (cdr (assq 'start state)))
3869 (set-window-point window (cdr (assq 'point state)))
3870 ;; I'm not sure whether we should set the mark here, but maybe
3871 ;; it can be used.
3872 (let ((mark (cdr (assq 'mark state))))
3873 (when mark (set-mark mark))))
3874 ;; Select window if it's the selected one.
3875 (when (cdr (assq 'selected state))
3876 (select-window window)))))))
3878 (defun window-state-put (state &optional window ignore)
3879 "Put window state STATE into WINDOW.
3880 STATE should be the state of a window returned by an earlier
3881 invocation of `window-state-get'. Optional argument WINDOW must
3882 specify a live window and defaults to the selected one.
3884 Optional argument IGNORE non-nil means ignore minimum window
3885 sizes and fixed size restrictions. IGNORE equal `safe' means
3886 subwindows can get as small as `window-safe-min-height' and
3887 `window-safe-min-width'."
3888 (setq window (normalize-live-window window))
3889 (let* ((frame (window-frame window))
3890 (head (car state))
3891 ;; We check here (1) whether the total sizes of root window of
3892 ;; STATE and that of WINDOW are equal so we can avoid
3893 ;; calculating new sizes, and (2) if we do have to resize
3894 ;; whether we can do so without violating size restrictions.
3895 (totals
3896 (and (= (window-total-size window)
3897 (cdr (assq 'total-height state)))
3898 (= (window-total-size window t)
3899 (cdr (assq 'total-width state)))))
3900 (min-height (cdr (assq 'min-height head)))
3901 (min-width (cdr (assq 'min-width head)))
3902 window-splits selected)
3903 (if (and (not totals)
3904 (or (> min-height (window-total-size window))
3905 (> min-width (window-total-size window t)))
3906 (or (not ignore)
3907 (and (setq min-height
3908 (cdr (assq 'min-height-ignore head)))
3909 (setq min-width
3910 (cdr (assq 'min-width-ignore head)))
3911 (or (> min-height (window-total-size window))
3912 (> min-width (window-total-size window t)))
3913 (or (not (eq ignore 'safe))
3914 (and (setq min-height
3915 (cdr (assq 'min-height-safe head)))
3916 (setq min-width
3917 (cdr (assq 'min-width-safe head)))
3918 (or (> min-height
3919 (window-total-size window))
3920 (> min-width
3921 (window-total-size window t))))))))
3922 ;; The check above might not catch all errors due to rounding
3923 ;; issues - so IGNORE equal 'safe might not always produce the
3924 ;; minimum possible state. But such configurations hardly make
3925 ;; sense anyway.
3926 (error "Window %s too small to accomodate state" window)
3927 (setq state (cdr state))
3928 (setq window-state-put-list nil)
3929 ;; Work on the windows of a temporary buffer to make sure that
3930 ;; splitting proceeds regardless of any buffer local values of
3931 ;; `window-size-fixed'. Release that buffer after the buffers of
3932 ;; all live windows have been set by `window-state-put-2'.
3933 (with-temp-buffer
3934 (set-window-buffer window (current-buffer))
3935 (window-state-put-1 state window nil totals)
3936 (window-state-put-2 ignore))
3937 (window-check frame))))
3939 ;;; Displaying buffers.
3940 (defgroup display-buffer nil
3941 "Displaying buffers in windows."
3942 :version "24.1"
3943 :group 'windows)
3945 (defconst display-buffer-default-specifiers
3946 '((reuse-window nil same visible)
3947 (pop-up-window (largest . nil) (lru . nil))
3948 (pop-up-frame)
3949 (pop-up-frame-alist
3950 (height . 24) (width . 80) (unsplittable . t))
3951 (reuse-window nil other visible)
3952 (reuse-window-even-sizes . t))
3953 "Buffer display default specifiers.
3954 The value specified here is used when no other specifiers have
3955 been specified by the user or the application. Consult the
3956 documentation of `display-buffer-alist' for a description of
3957 buffer display specifiers.")
3959 (defconst display-buffer-macro-specifiers
3960 '((same-window
3961 ;; Use the same window.
3962 (reuse-window same nil nil))
3963 (same-frame
3964 ;; Avoid other frames.
3965 (reuse-window nil same nil)
3966 (pop-up-window (largest . nil) (lru . nil))
3967 (reuse-window nil other nil))
3968 (other-window
3969 ;; Avoid selected window.
3970 (reuse-window other same visible)
3971 (pop-up-window (largest . nil) (lru . nil))
3972 (pop-up-frame)
3973 (reuse-window other other visible))
3974 (same-frame-other-window
3975 ;; Avoid other frames and selected window.
3976 (reuse-window other same nil)
3977 (pop-up-window (largest . nil) (lru . nil))
3978 (reuse-window other other nil))
3979 (other-frame
3980 ;; Avoid selected frame.
3981 (reuse-window nil same other)
3982 (pop-up-frame)
3983 (reuse-window nil other other))
3984 (default
3985 ;; The default specifiers.
3986 display-buffer-default-specifiers))
3987 "Buffer display macro specifiers.")
3989 (defcustom display-buffer-alist
3990 '((((regexp . ".*"))
3991 reuse-window (reuse-window nil same visible)
3992 pop-up-window
3993 (pop-up-window (largest . nil) (lru . nil))
3994 pop-up-frame
3995 (pop-up-frame)
3996 reuse-window (reuse-window nil other visible)
3997 (reuse-window-even-sizes . t)))
3998 "List associating buffer identifiers with display specifiers.
3999 The car of each element of this list is built from a set of cons
4000 cells called buffer identifiers. `display-buffer' shows a buffer
4001 according to the display specifiers in the element's cdr
4002 \(elements are true lists) if at least one of the identifiers
4003 matches the first or third argument of `display-buffer'. Such a
4004 match occurs in one of the following three cases:
4006 - The car of the buffer identifier is the symbol `name' and its
4007 cdr is a string equalling the name of the buffer specified by
4008 the first \(BUFFER-OR-NAME) argument of `display-buffer'.
4010 - The car is the symbol `regexp' and the cdr is a regular
4011 expression matching the name of the buffer specified by the
4012 first \(BUFFER-OR-NAME) argument of `display-buffer'.
4014 - The car is the symbol `label' and the cdr is a symbol equalling
4015 the third \(LABEL) argument of `display-buffer'.
4017 Display specifiers are either symbols, cons cells, or lists.
4018 Five specifiers have been reserved to indicate the basic method
4019 for displaying the buffer: `reuse-window', `pop-up-window',
4020 `pop-up-frame', `use-side-window', and `fun-with-args'.
4022 A list whose car is the symbol `reuse-window' indicates that an
4023 existing window shall be reused for displaying the buffer. The
4024 second element of this list specifies the window to use and can
4025 be one of the following symbols:
4027 nil stands for any window.
4029 `same' stands for the selected window.
4031 `other' stands for any but the selected window.
4033 The third element specifies whether the buffer shown in a window
4034 that shall be reused must be the same buffer that shall be
4035 displayed or another buffer and can be one of the following:
4037 nil means to not care about the window's buffer.
4039 `same' means the window must show the buffer already.
4041 `other' means the window must not show the buffer yet.
4043 The fourth element specifies the set of frames to search for a
4044 suitable window and can be one of the following:
4046 nil to reuse a window on the selected frame.
4048 `visible' to search visible frames on the current terminal.
4050 `other' stands for any visible frame but the selected one.
4052 0 \(the number zero) to search visible and iconified frames on
4053 the current terminal.
4055 t to search arbitrary frames including invisible ones.
4057 If more than one window fits the constraints imposed by these
4058 elements, the least recently used candidate is chosen. A side
4059 window is never reused unless it already shows the buffer.
4061 The following two specifiers are useful when the method equals
4062 `reuse-window':
4064 - A cons cell whose car is the symbol `reuse-window-even-sizes'
4065 and whose cdr is non-nil means to even out the sizes of a
4066 reused window and the selected window provided they (1) appear
4067 adjacent to each other and (2) the selected window is larger
4068 than the window chosen. If the cdr is nil, this means that the
4069 window sizes are left alone.
4071 - A cons cell whose car is the symbol `reuse-window-dedicated'
4072 and whose cdr is non-nil means that a window can be reused even
4073 if it's weakly dedicated to its buffer. If the cdr is t, a
4074 strongly dedicated window can be reused to show the buffer.
4075 Any other non-nil value means only weakly dedicated windows can
4076 be reused. If the cdr is nil, dedicated windows are not
4077 reused.
4079 This specifier should be used in emergency cases only since
4080 windows are usually made dedicated in order to prevent
4081 `display-buffer' from reusing them.
4083 A list whose car is the symbol `pop-up-window' and whose cdr is
4084 built from cons cells representing window/side tuples indicates
4085 that a new window shall be made for displaying the buffer on the
4086 selected frame.
4088 Window/side tuples are cons cells. The car of such a tuple
4089 identifies the window that shall be split. Possible values are
4090 `largest', `lru', `selected', and `root' to split the largest,
4091 least recently used, selected or root window of the selected
4092 frame.
4094 If the frame has side windows, these values do allow to split
4095 only the selected frame's main window or one of its subwindows.
4096 Setting the car to one of `left', `top', `right' and `bottom'
4097 splits the corresponding side window, provided such a window
4098 exists.
4100 The cdr of each pair specifies on which side of the window to
4101 split the new window shall appear and can be one of `below',
4102 `right', `above', or `left' with the obvious meanings. If the
4103 cdr is nil, the window is split in a fashion suitable for its
4104 current dimensions. If the cdr specifies a function, that
4105 function is called with one argument - the window to split. The
4106 function is supposed to split that window and return the new
4107 window.
4109 `display-buffer' scans these tuples until it can either produce a
4110 suitable window or fails. The default value for
4111 `display-buffer-alist' contains the tuples \(largest . nil) and
4112 \(lru . nil) in order to split the largest window first and, if
4113 that fails, the least recently used one.
4115 The following specifiers are useful if the method specifier is
4116 `pop-up-window'.
4118 - A cons cell whose car is the symbol `pop-up-window-min-height'
4119 specifiies the minimum height of the new window. If the cdr is
4120 an integer number, it specifies the minimum number of lines of
4121 the window. A floating point number gives the minimum fraction
4122 of the window height with respect to the height of the frame's
4123 root window. A new window is created only if it can be made at
4124 least as high as specified by the number. If the cdr is nil,
4125 this means to use the value of `window-min-height'.
4127 - A cons cell whose car is the symbol `pop-up-window-min-width'
4128 specifies the minimum width of the new window. If the cdr is
4129 an integer number, it specifies the minimum number of columns
4130 of the window. A floating point number gives the minimum
4131 fraction of the window width with respect to the width of the
4132 frame's root window. A new window is created only if it can be
4133 made at least as wide as specified by the number. If the cdr
4134 is nil, this means to use the value of `window-min-width'.
4136 - A cons cell whose car is `pop-up-window-set-height' with
4137 the following interpretations for the cdr:
4139 - nil means leave the height of the new window alone.
4141 - A number specifies the desired height of the new window. An
4142 integer number specifies the number of lines of the window.
4143 A floating point number gives the fraction of the window
4144 height with respect to the height of the frame's root window.
4146 - If the cdr specifies a function, that function is called with
4147 one argument - the new window. The function is supposed to
4148 adjust the height of the window; its return value is ignored.
4149 Suitable functions are `shrink-window-if-larger-than-buffer'
4150 and `fit-window-to-buffer'.
4152 - A cons cell whose car equals `pop-up-window-set-width' with
4153 the following interpretations for the cdr:
4155 - nil means leave the width of the new window alone.
4157 - A number specifies the desired width of the new window. An
4158 integer number specifies the number of columns of the window.
4159 A floating point number gives the fraction of the window
4160 width with respect to the width of the frame's root window.
4162 - If the cdr specifies a function, that function is called with
4163 one argument - the new window. The function is supposed to
4164 adjust the width of the window; its return value is ignored.
4166 Observe that specifying `pop-up-window-set-height' or
4167 `pop-up-window-set-width' may override restrictions given by
4168 the `pop-up-window-min-height' and `pop-up-window-min-width'
4169 specifiers.
4171 - A cons cell whose car is `pop-up-window-split-unsplittable' and
4172 whose cdr is non-nil allows to make a new window on an
4173 unsplittable frame. If the cdr is nil, unsplittable frames are
4174 not split. This specifier should be used in special cases only
4175 since frames are usually made unsplittable in order to prevent
4176 `display-buffer' from splitting them.
4178 A list whose car is the symbol `pop-up-frame' specifies that a
4179 new frame shall be made for displaying the buffer. The second
4180 element, if non-nil, allows popping up a new frame on graphic
4181 displays only.
4183 The following specifiers are useful if the method specifier is
4184 `pop-up-frame'.
4186 - A list whose car is the symbol `pop-up-frame-function' together
4187 with a valid function as cdr specifies the function for
4188 creating a new frame. If the cdr is nil, the default function
4189 `make-frame' is called. The function is called with the
4190 parameters and values provided by the specifier described next.
4192 - A list whose car is the symbol `pop-up-frame-alist' followed by
4193 an arbitrary number of frame parameter/value tuples, each given
4194 as a cons cell, specifies the parameters passed to the pop-up
4195 frame function.
4197 A list of three elements whose car is the symbol
4198 `use-side-window' specifies that the buffer shall be displayed in
4199 a side window of the selected frame. The second element denotes
4200 the side of the frame where the window appears or shall be made.
4201 The third element denotes the slot used by the window. If a side
4202 window with the specified slot exists already, that window is
4203 reused. If no such window exists it is created.
4205 The following specifiers are useful in connection with the
4206 `use-side-window' method specifier: `reuse-window-dedicated',
4207 `pop-up-window-min-height', `pop-up-window-min-width',
4208 `pop-up-window-set-height' and `pop-up-window-set-width'.
4210 A list whose car is the symbol `fun-with-args' specifies that the
4211 function specified in the second element of the list is
4212 responsible for displaying the buffer. `display-buffer' calls
4213 this function with the buffer as first argument and the remaining
4214 elements of the list as second argument.
4216 The function should choose or create a window, display the buffer
4217 in it, and return the window. It is also responsible for giving
4218 the variable `display-buffer-window' and the `quit-restore'
4219 parameter of the window used a meaningful value.
4221 Within the body of this function avoid calling `display-buffer'
4222 with the same buffer as argument since this may lead to endless
4223 recursion.
4225 Instead of supplying basic method specifiers, it's sometimes more
4226 convenient to use macro specifiers. They provide some commonly
4227 used display methods but do not support the fine control provided
4228 by the basic method specifiers. Macro specifiers are symbols.
4229 The following macro specifiers are provided:
4231 `same-window' to display the buffer in the selected window.
4233 `same-frame' to display the buffer on the selected frame.
4235 `other-window' to display the buffer in any window but the
4236 selected one.
4238 `same-frame-other-window' as `other-window' but stay on the
4239 selected frame.
4241 `other-frame' to display the buffer on another visible
4242 frame.
4244 `default' to use the default value of `display-buffer-alist'.
4246 One specifier is useful with any method specifier: A list whose
4247 car is the symbol `dedicate' and whose cdr is non-nil will
4248 dedicate the window to its buffer. The following values are
4249 supported:
4251 - nil to not dedicate the window to the buffer.
4253 - `weak' to weakly dedicate the window to the buffer.
4255 - t to strongly dedicate the window to the buffer.
4257 Usually, applications are free to override the specifiers of
4258 `display-buffer-alist' by passing their own specifiers as second
4259 argument of `display-buffer'. For every `display-buffer-alist'
4260 entry you can, however, add a cons cell whose car is the symbol
4261 `override' and whose cdr is non-nil, to explicitly override any
4262 value supplied by the application.
4264 Overriding specifiers supplied by the calling application is, in
4265 general, not advisable. It permits, for example, to change the
4266 semantics of a function like `display-buffer-other-window' by
4267 using the location specifiers `same-window' or `other-frame'."
4268 :risky t
4269 :type
4270 '(repeat
4271 :offset 9
4272 ;; Associations of buffer identifiers and display specifiers.
4273 (list
4274 :format "%v"
4275 ;; Buffer identifiers.
4276 (repeat
4277 :tag "Buffer identifiers"
4278 (choice
4279 :tag "Identifier"
4280 :format "%[%t%] %v" :size 15
4281 (cons
4282 :tag "Name"
4283 :format "%v"
4284 :help-echo "A buffer name."
4285 (const :format "" name)
4286 (string :format "Name: %v\n" :size 32))
4287 (cons
4288 :tag "Regexp"
4289 :format "%v"
4290 :help-echo "A regular expression matching buffer names."
4291 (const :format "" regexp)
4292 (string :format "Regexp: %v\n" :size 32))
4293 (cons
4294 :tag "Label"
4295 :format "%v"
4296 :help-echo "A symbol equalling the buffer display label."
4297 (const :format "" symbol)
4298 (symbol :format "Label: %v\n" :size 32))))
4300 ;; Display specifiers.
4301 (repeat
4302 :offset 9
4303 :tag "Display specifiers"
4304 :inline t
4305 (list
4306 :inline t
4307 :format "%v"
4308 (choice
4309 :tag "Method"
4310 :value (reuse-window
4311 (reuse-window nil same nil)
4312 (reuse-window-even-sizes . t))
4313 :inline t
4314 :help-echo "Method for displaying the buffer."
4315 :format "%[Method%] %v" :size 15
4317 ;; Reuse window specifiers.
4318 (list
4319 :tag "Reuse window"
4320 :value (reuse-window
4321 (reuse-window nil same nil)
4322 (reuse-window-even-sizes . t))
4323 :format "%t\n%v"
4324 :inline t
4325 ;; For customization purposes only.
4326 (const :format "" reuse-window)
4327 (set
4328 :format "%v"
4329 :inline t
4330 ;; The window to reuse.
4331 (list
4332 :format "%v\n"
4333 (const :format "" reuse-window)
4334 ;; The window type.
4335 (choice
4336 :tag "Window"
4337 :help-echo "Window to reuse."
4338 :value nil
4339 :format "%[Window%] %v" :size 15
4340 (const :tag "Any" :format "%t" nil)
4341 (const :tag "Selected only" :format "%t" same)
4342 (const :tag "Any but selected" :format "%t" other))
4343 ;; The window's buffer.
4344 (choice
4345 :tag "Buffer"
4346 :help-echo "Buffer shown by reused window."
4347 :value t
4348 :format " %[Buffer%] %v" :size 15
4349 (const :tag "Any buffer" :format "%t" nil)
4350 (const :tag "Same buffer" :format "%t" same)
4351 (const :tag "Other buffer" :format "%t" other))
4352 ;; The window's frame.
4353 (choice
4354 :help-echo "Frame to search for a window to reuse."
4355 :tag "Frame"
4356 :value nil
4357 :format " %[Frame%] %v" :size 15
4358 (const :tag "Selected frame only" :format "%t" nil)
4359 (const :tag "Visible frames" :format "%t" visible)
4360 (const :tag "Visible but unselected" :format "%t" other)
4361 (const :tag "Visible and iconified" :format "%t" 0)
4362 (const :tag "Any frame" :format "%t" t)))
4363 ;; Whether window sizes should be evened out.
4364 (cons
4365 :format "%v\n"
4366 :tag "Even window sizes"
4367 (const :format "" reuse-window-even-sizes)
4368 (choice
4369 :tag "Even window sizes"
4370 :help-echo "Whether to even sizes of selected and reused window."
4371 :value t
4372 :format "%[Even window sizes%] %v" :size 15
4373 (const :tag "Off" :format "%t" nil)
4374 (const :tag "Even window sizes" :format "%t" t)))
4375 ;; Whether to reuse a dedicated window
4376 (cons
4377 :format "%v\n"
4378 (const :format "" reuse-window-dedicated)
4379 (choice
4380 :tag "Reuse dedicated window" :value nil
4381 :help-echo "Reuse a window even if it is dedicated to its buffer."
4382 :format "%[Reuse dedicated window%] %v" :size 15
4383 (const :tag "Off" :format "%t" nil)
4384 (const :tag "Reuse weakly dedicated windows" :format "%t" weak)
4385 (const :tag "Reuse any dedicated window" :format "%t" t)))))
4387 ;; Pop-up window specifiers.
4388 (list
4389 :tag "Pop-up window"
4390 :value (pop-up-window (pop-up-window (largest . nil) (lru . nil)))
4391 :format "%t\n%v"
4392 :inline t
4393 (const :format "" pop-up-window)
4394 (set
4395 :format "%v"
4396 :inline t
4397 ;; Pop-up window list.
4398 (list
4399 :format "%v"
4400 :value (pop-up-window (largest . nil) (lru . nil))
4401 (const :format "" pop-up-window)
4402 (repeat
4403 :tag "Window / Side tuples"
4404 :inline t
4405 (cons
4406 :format "%v\n"
4407 (choice
4408 :tag "Window"
4409 :help-echo "The window to split."
4410 :value largest
4411 :format "%[Window%] %v"
4412 (const :tag "Largest" :format "%t" largest)
4413 (const :tag "Least recently used" :format "%t" lru)
4414 (const :tag "Selected" :format "%t" selected)
4415 (const :tag "Root" :format "%t" root)
4416 (const :tag "Left" :format "%t" left)
4417 (const :tag "Top" :format "%t" top)
4418 (const :tag "Right" :format "%t" right)
4419 (const :tag "Bottom" :format "%t" bottom))
4420 (choice
4421 :tag "Side"
4422 :help-echo "The position of the new window with respect to the window to split."
4423 :value nil
4424 :format " %[Side%] %v"
4425 (const :tag "Dynamic" :format "%t" nil)
4426 (const :tag "Below" :format "%t" below)
4427 (const :tag "Right" :format "%t" right)
4428 (const :tag "Above" :format "%t" above)
4429 (const :tag "Left" :format "%t" left)
4430 (function
4431 :tag "Function" :format "%v" :size 25)))))
4432 ;; Minimum height of pop-up windows.
4433 (cons
4434 :format "%v\n"
4435 (const :format "" pop-up-window-min-height)
4436 (choice
4437 :help-echo "Minimum height of popped-up window."
4438 :format "%[Minimum height%] %v"
4439 (const :tag "Default" :format "%t" :value nil)
4440 (integer :tag "Number of lines" :value 12 :size 5)
4441 (float :tag "Fraction of frame height" :value .25 :size 5)))
4442 ;; Minimum width of pop-up windows.
4443 (cons
4444 :format "%v\n"
4445 (const :format "" pop-up-window-min-width)
4446 (choice
4447 :help-echo "Minimum width of popped-up window."
4448 :format "%[Minimum width%] %v"
4449 (const :tag "Default" :format "%t" :value nil)
4450 (integer :tag "Number of columns" :value 12 :size 5)
4451 (float :tag "Fraction of frame width" :value .25 :size 5)))
4452 ;; Desired height of pop-up windows.
4453 (cons
4454 :format "%v\n"
4455 (const :format "" pop-up-window-set-height)
4456 (choice
4457 :help-echo "Desired height of popped-up window."
4458 :format "%[Desired height%] %v"
4459 (const :tag "Default" :format "%t" :value nil)
4460 (integer :tag "Number of lines" :value 12 :size 5)
4461 (float :tag "Fraction of frame height" :value .25 :size 5)
4462 (function :tag "Function" :size 25)))
4463 ;; Desired width of pop-up windows.
4464 (cons
4465 :format "%v\n"
4466 (const :format "" pop-up-window-set-width)
4467 (choice
4468 :help-echo "Desired width of popped-up window."
4469 :format "%[Desired width%] %v"
4470 (const :tag "Default" :format "%t" :value nil)
4471 (integer :tag "Number of column" :value 12 :size 5)
4472 (float :tag "Fraction of frame width" :value .25 :size 5)
4473 (function :tag "Function" :size 25)))
4474 ;; Split unsplittable frames.
4475 (cons
4476 :format "%v\n"
4477 (const :format "" pop-up-window-unsplittable)
4478 (choice
4479 :help-echo "Allow popping up a window on \"unsplittable\" frames."
4480 :format "%[Split unsplittable frame%] %v"
4481 (const :tag "Off" :format "%t" nil)
4482 (const :tag "Allow" :format "%t" t)))))
4484 ;; Pop-up frame specifiers.
4485 (list
4486 :tag "Pop-up frame"
4487 :value (pop-up-frame
4488 (pop-up-frame)
4489 (pop-up-frame-alist
4490 (height . 24) (width . 80) (unsplittable . t)))
4491 :format "%t\n%v"
4492 :inline t
4493 (const :format "" pop-up-frame)
4494 (set
4495 :format "%v"
4496 :inline t
4497 ;; Pop-up frame.
4498 (list
4499 :tag "Pop-up a new frame"
4500 :value (pop-up-frame)
4501 :format "%v"
4502 (const :format "" pop-up-frame)
4503 (choice
4504 :tag "Pop-up a new frame"
4505 :help-echo "Whether to pop-up a new frame on a display."
4506 :format "%[Display%] %v\n" :size 15
4507 (const :tag "On any display" :format "%t" nil)
4508 (const :tag "On graphic displays only" :format "%t" t)))
4509 ;; Pop-up frame function
4510 (cons
4511 :format "%v\n"
4512 (const :format "" pop-up-frame-function)
4513 (choice
4514 :tag "Pop-up frame function"
4515 :value nil
4516 :help-echo "Function to use to pop-up a new frame."
4517 :format "%[Function%] %v" :size 15
4518 (const :tag "Default" :format "%t" nil)
4519 (function
4520 :value make-frame
4521 :format "%t: %v"
4522 :size 25)))
4523 ;; Pop-up frame alist.
4524 (list
4525 :format "%v"
4526 (const :format "" pop-up-frame-alist)
4527 (repeat
4528 :tag "Parameter / Value tuples"
4529 :inline t
4530 (cons
4531 :format "%v\n"
4532 (symbol
4533 :tag "Parameter"
4534 :format "Parameter: %v"
4535 :size 16)
4536 (sexp
4537 :tag "Value"
4538 :format " Value: %v"
4539 :size 8))))))
4541 ;; Use side-window specifiers.
4542 (list
4543 :tag "Use side-window"
4544 :value (use-side-window (use-side-window bottom 0))
4545 :format "%t\n%v"
4546 :inline t
4547 ;; For customization purposes only.
4548 (const :format "" use-side-window)
4549 (set
4550 :format "%v"
4551 :inline t
4552 ;; Side and slot.
4553 (list
4554 :format "%v\n"
4555 :value (use-side-window bottom 0)
4556 (const :format "" use-side-window)
4557 ;; The side.
4558 (choice
4559 :tag "Side"
4560 :help-echo "Side of frame."
4561 :value bottom
4562 :format "%[Side%] %v" :size 15
4563 (const :tag "Left" :format "%t" left)
4564 (const :tag "Top" :format "%t" top)
4565 (const :tag "Right" :format "%t" right)
4566 (const :tag "Bottom" :format "%t" bottom))
4567 ;; The slot
4568 (number
4569 :tag "Slot"
4570 :help-echo "The slot (an arbitrary number, where 0 stands for the center slot)."
4571 :value 0
4572 :format " Slot: %v" :size 8))
4573 ;; Whether to reuse a dedicated side window
4574 (cons
4575 :format "%v\n"
4576 (const :format "" reuse-window-dedicated)
4577 (choice
4578 :tag "Reuse dedicated side window" :value nil
4579 :help-echo "Reuse a side window even if it is dedicated to its buffer."
4580 :format "%[Reuse dedicated side window%] %v" :size 15
4581 (const :tag "Off" :format "%t" nil)
4582 (const :tag "Reuse weakly dedicated side windows" :format "%t" weak)
4583 (const :tag "Reuse any dedicated side window" :format "%t" t)))
4584 ;; Minimum height of pop-up side windows.
4585 (cons
4586 :format "%v\n"
4587 (const :format "" pop-up-window-min-height)
4588 (choice
4589 :help-echo "Minimum height of popped-up side window."
4590 :format "%[Minimum height%] %v"
4591 (const :tag "Default" :format "%t" :value nil)
4592 (integer :tag "Number of lines" :value 12 :size 5)
4593 (float :tag "Fraction of frame height" :value .25 :size 5)))
4594 ;; Minimum width of pop-up windows.
4595 (cons
4596 :format "%v\n"
4597 (const :format "" pop-up-window-min-width)
4598 (choice
4599 :help-echo "Minimum width of popped-up side window."
4600 :format "%[Minimum width%] %v"
4601 (const :tag "Default" :format "%t" :value nil)
4602 (integer :tag "Number of columns" :value 12 :size 5)
4603 (float :tag "Fraction of frame width" :value .25 :size 5)))
4604 ;; Desired height of pop-up windows.
4605 (cons
4606 :format "%v\n"
4607 (const :format "" pop-up-window-set-height)
4608 (choice
4609 :help-echo "Desired height of popped-up side window."
4610 :format "%[Desired height%] %v"
4611 (const :tag "Default" :format "%t" :value nil)
4612 (integer :tag "Number of lines" :value 12 :size 5)
4613 (float :tag "Fraction of frame height" :value .25 :size 5)
4614 (function :tag "Function" :size 25)))
4615 ;; Desired width of pop-up windows.
4616 (cons
4617 :format "%v\n"
4618 (const :format "" pop-up-window-set-width)
4619 (choice
4620 :help-echo "Desired width of popped-up side window."
4621 :format "%[Desired width%] %v"
4622 (const :tag "Default" :format "%t" :value nil)
4623 (integer :tag "Number of column" :value 12 :size 5)
4624 (float :tag "Fraction of frame width" :value .25 :size 5)
4625 (function :tag "Function" :size 25)))))
4627 ;; Function with argument specifiers.
4628 (list
4629 :tag "Function with arguments"
4630 :value (fun-with-args (fun-with-args 'ignore))
4631 :format "%t\n%v"
4632 :inline t
4633 ;; For customization purposes only.
4634 (const :format "" fun-with-args)
4635 (set
4636 :format "%v"
4637 :inline t
4638 (list
4639 :format "%v"
4640 :value (fun-with-args 'ignore)
4641 (const :format "" fun-with-args)
4642 (function :tag "Function" :format "%t: %v\n" :size 25)
4643 (list
4644 :format "%v"
4645 (repeat
4646 :tag "Arguments"
4647 :inline t
4648 (sexp
4649 :format "%v\n"
4650 :size 16))))))
4652 ;; Macro specifiers.
4653 (list
4654 :tag "Same frame only"
4655 :format "%t%v"
4656 :inline t
4657 (const :format "\n" same-frame))
4658 (list
4659 :tag "Other window"
4660 :format "%t%v"
4661 :inline t
4662 (const :format "\n" other-window))
4663 (list
4664 :tag "Same frame other window"
4665 :format "%t%v"
4666 :inline t
4667 (const :format "\n" same-frame-other-window))
4668 (list
4669 :tag "Other frame only"
4670 :format "%t%v"
4671 :inline t
4672 (const :format "\n" other-frame))
4673 (list
4674 :tag "Default"
4675 :format "%t%v"
4676 :inline t
4677 (const :format "\n" default)))))
4679 (set
4680 :format "%v"
4681 :inline t
4682 ;; Dedicate window to buffer.
4683 (cons
4684 :format "%v"
4685 (const :format "" dedicate)
4686 (choice
4687 :help-echo "Mark window as dedicated to its buffer."
4688 :format "%[Dedicate window to buffer%] %v\n" :size 15
4689 (const :tag "Off" :format "%t" nil)
4690 (const :tag "Weak" :format "%t" weak)
4691 (const :tag "Strong" :format "%t" t)))
4692 ;; No other window.
4693 (cons
4694 :format "%v"
4695 (const :format "" no-other-window)
4696 (choice
4697 :help-echo "Whether `other-window' shall ignore the window."
4698 :format "%[No other window%] %v\n" :size 15
4699 (const :tag "Off" :format "%t" nil)
4700 (const :tag "Ignore" :format "%t" t)))
4701 ;; Overriding.
4702 (cons
4703 :format "%v\n"
4704 (const :format "" override)
4705 (choice
4706 :help-echo "Override application supplied specifiers."
4707 :format "%[Override%] %v"
4708 (const :tag "Off" :format "%t" nil)
4709 (const :tag "Override" :format "%t" t))))))
4710 :group 'windows
4711 :group 'frames)
4713 (defcustom display-buffer-function nil
4714 "If non-nil, function to call to display a buffer.
4715 `display-buffer' calls this function with two arguments, the
4716 buffer to display and a list of buffer display specifiers, see
4717 `display-buffer-alist'.
4719 The function is supposed to choose or create a window, display
4720 the specified buffer in it, and return the window. It is also
4721 responsible for giving the variable `display-buffer-window' and
4722 the `quit-restore' parameter of the window used a meaningful
4723 value.
4725 The function specified here overrides all specifiers of the
4726 variable `display-buffer-alist' any specifiers passed to
4727 `display-buffer'.
4729 If you call `display-buffer' within the body of the function,
4730 bind the value of `display-buffer-function' to nil around that
4731 call to avoid that the function recursively calls itself."
4732 :type '(choice
4733 (const nil)
4734 (function :tag "Function"))
4735 :group 'display-buffer)
4737 ;; The following is a global variable which is used externally (by
4738 ;; help.el) to (1) know which window was used for displaying a buffer
4739 ;; and (2) whether the window was new or reused.
4740 (defvar display-buffer-window nil
4741 "Window used by `display-buffer' and related information.
4742 After `display-buffer' displays a buffer in some window this
4743 variable is a cons cell whose car denotes the window used to
4744 display the buffer. The cdr is supposed to be one of the symbols
4745 `reuse-buffer-window', `reuse-other-window', `new-window' or
4746 `new-frame'.
4748 If the buffer display location specifier is one of 'same-window,
4749 'same-frame, or 'other-frame, the `display-buffer' routines
4750 assign the value of this variable. If the location specifier is
4751 a function, that function becomes responsible for assigning a
4752 meaningful value to this variable. See the functions
4753 `display-buffer-reuse-window', `display-buffer-pop-up-window' and
4754 `display-buffer-pop-up-frame' for how this can be done.")
4756 (defun display-buffer-even-window-sizes (window specifiers)
4757 "Even sizes of WINDOW and selected window according to SPECIFIERS.
4758 SPECIFIERS must be a list of buffer display specifiers, see the
4759 documentation of `display-buffer-alist' for a description.
4761 Sizes are evened out if and only if WINDOW and the selected
4762 window appear next to each other and the selected window is
4763 larger than WINDOW."
4764 (cond
4765 ((or (not (cdr (assq 'reuse-window-even-sizes specifiers)))
4766 ;; Don't resize minibuffer windows.
4767 (window-minibuffer-p)
4768 ;; WINDOW must be adjacent to the selected one.
4769 (not (or (eq window (window-prev))
4770 (eq window (window-next))))))
4771 ((and (window-iso-combined-p window)
4772 ;; Resize iff the selected window is higher than WINDOW.
4773 (> (window-total-height) (window-total-height window)))
4774 ;; Don't throw an error if we can't even window heights for
4775 ;; whatever reason. In any case, enlarging the selected window
4776 ;; might fail anyway if there are other windows above or below
4777 ;; WINDOW and the selected one. But for a simple two windows
4778 ;; configuration the present behavior is good enough so why care?
4779 (ignore-errors
4780 (resize-window
4781 window (/ (- (window-total-height) (window-total-height window))
4782 2))))
4783 ((and (window-iso-combined-p window t)
4784 ;; Resize iff the selected window is wider than WINDOW.
4785 (> (window-total-width) (window-total-width window)))
4786 ;; Don't throw an error if we can't even window widths, see
4787 ;; comment above.
4788 (ignore-errors
4789 (resize-window
4790 window (/ (- (window-total-width) (window-total-width window))
4791 2) t)))))
4793 (defun display-buffer-set-height (window specifiers)
4794 "Adjust height of WINDOW according to SPECIFIERS.
4795 SPECIFIERS must be a list of buffer display specifiers, see the
4796 documentation of `display-buffer-alist' for a description."
4797 (let ((set-height (cdr (assq 'pop-up-window-set-height specifiers))))
4798 (cond
4799 ((numberp set-height)
4800 (let* ((height (if (integerp set-height)
4801 set-height
4802 (round
4803 (* (window-total-size (frame-root-window window))
4804 set-height))))
4805 (delta (- height (window-total-size window))))
4806 (when (and (window-resizable-p window delta nil 'safe)
4807 (window-iso-combined-p window))
4808 (resize-window window delta nil 'safe))))
4809 ((functionp set-height)
4810 (ignore-errors (funcall set-height window))))))
4812 (defun display-buffer-set-width (window specifiers)
4813 "Adjust width of WINDOW according to SPECIFIERS.
4814 SPECIFIERS must be a list of buffer display specifiers, see the
4815 documentation of `display-buffer-alist' for a description."
4816 (let ((set-width (cdr (assq 'pop-up-window-set-width specifiers))))
4817 (cond
4818 ((numberp set-width)
4819 (let* ((width (if (integerp set-width)
4820 set-width
4821 (round
4822 (* (window-total-size (frame-root-window window) t)
4823 set-width))))
4824 (delta (- width (window-total-size window t))))
4825 (when (and (window-resizable-p window delta t 'safe)
4826 (window-iso-combined-p window t))
4827 (resize-window window delta t 'safe))))
4828 ((functionp set-width)
4829 (ignore-errors (funcall set-width window))))))
4831 ;; We have to work around the deficiency that the command loop does not
4832 ;; preserve the selected window when it is on a frame that hasn't been
4833 ;; raised or given input focus. So we have to (1) select the window
4834 ;; used for displaying a buffer and (2) raise its frame if necessary,
4835 ;; thus defeating one primary principle of `display-buffer' namely to
4836 ;; _not_ select the window chosen for displaying the buffer :-(
4837 (defun display-buffer-select-window (window &optional norecord)
4838 "Select WINDOW and raise its frame if necessary."
4839 (let ((old-frame (selected-frame))
4840 (new-frame (window-frame window)))
4841 ;; Select WINDOW _before_ raising the frame to assure that the mouse
4842 ;; cursor moves into the correct window.
4843 (select-window window norecord)
4844 (unless (eq old-frame new-frame)
4845 (select-frame-set-input-focus new-frame))))
4847 (defun display-buffer-in-window (buffer window specifiers)
4848 "Display BUFFER in WINDOW and raise its frame if needed.
4849 WINDOW must be a live window and defaults to the selected one.
4850 Return WINDOW.
4852 SPECIFIERS must be a list of buffer display specifiers, see the
4853 documentation of `display-buffer-alist' for a description."
4854 (setq buffer (normalize-live-buffer buffer))
4855 (setq window (normalize-live-window window))
4856 (let* ((old-frame (selected-frame))
4857 (new-frame (window-frame window))
4858 (dedicated (cdr (assq 'dedicated specifiers)))
4859 (no-other-window (cdr (assq 'no-other-window specifiers))))
4860 ;; Show BUFFER in WINDOW.
4861 (set-window-dedicated-p window nil)
4862 (set-window-buffer window buffer)
4863 (when dedicated
4864 (set-window-dedicated-p window dedicated))
4865 (when no-other-window
4866 (set-window-parameter window 'no-other-window t))
4867 (unless (eq old-frame new-frame)
4868 (display-buffer-select-window window))
4869 ;; Return window.
4870 window))
4872 (defun display-buffer-reuse-window (buffer method &optional specifiers)
4873 "Display BUFFER in an existing window.
4874 METHOD must be a list in the form of the cdr of a `reuse-window'
4875 buffer display specifier, see `display-buffer-alist' for an
4876 explanation. The first element must specifiy the window to use,
4877 and can be either nil, `same', `other', or a live window. The
4878 second element must specify the window's buffer and can be either
4879 nil, `same', `other', or a live buffer. The third element is the
4880 frame to use - either nil, 0, `visible', `other', t, or a live
4881 frame.
4883 Optional argument SPECIFIERS must be a list of valid display
4884 specifiers. Return the window chosen to display BUFFER, nil if
4885 none was found."
4886 (let* ((method-window (nth 0 method))
4887 (method-buffer (nth 1 method))
4888 (method-frame (nth 2 method))
4889 (reuse-dedicated (assq 'reuse-window-dedicated specifiers))
4890 windows other-frame dedicated time best-window best-time)
4891 (when (eq method-frame 'other)
4892 ;; `other' is not handled by `window-list-1'.
4893 (setq other-frame t)
4894 (setq method-frame t))
4895 (dolist (window (window-list-1 nil 'nomini method-frame))
4896 (let ((window-buffer (window-buffer window)))
4897 (when (and (not (window-minibuffer-p window))
4898 ;; Don't reuse a side window.
4899 (or (not (eq (window-parameter window 'window-side) 'side))
4900 (eq window-buffer buffer))
4901 (or (not method-window)
4902 (and (eq method-window 'same)
4903 (eq window (selected-window)))
4904 (and (eq method-window 'other)
4905 (not (eq window (selected-window))))
4906 ;; Special case for applications that specifiy
4907 ;; the window explicitly.
4908 (eq method-window window))
4909 (or (not method-buffer)
4910 (and (eq method-buffer 'same)
4911 (eq window-buffer buffer))
4912 (and (eq method-buffer 'other)
4913 (not (eq window-buffer buffer)))
4914 ;; Special case for applications that specifiy
4915 ;; the window's buffer explicitly.
4916 (eq method-buffer window-buffer))
4917 (or (not other-frame)
4918 (not (eq (window-frame window) (selected-frame))))
4919 ;; Handle dedicatedness.
4920 (or (eq window-buffer buffer)
4921 ;; The window does not show the same buffer.
4922 (not (setq dedicated (window-dedicated-p window)))
4923 ;; If the window is weakly dedicated to its
4924 ;; buffer, reuse-dedicated must be non-nil.
4925 (and (not (eq dedicated t)) reuse-dedicated)
4926 ;; If the window is strongly dedicated to its
4927 ;; buffer, reuse-dedicated must be t.
4928 (eq reuse-dedicated t)))
4929 (setq windows (cons window windows)))))
4931 (if (eq method-buffer 'same)
4932 ;; When reusing a window on the same buffer use the lru one.
4933 (dolist (window windows)
4934 (setq time (window-use-time window))
4935 (when (or (not best-window) (< time best-time))
4936 (setq best-window window)
4937 (setq best-time time)))
4938 ;; Otherwise, sort windows according to their use-time.
4939 (setq windows
4940 (sort windows
4941 '(lambda (window-1 window-2)
4942 (<= (window-use-time window-1)
4943 (window-use-time window-2)))))
4944 (setq best-window
4945 ;; Try to get a full-width window (this is silly and can
4946 ;; get us to another frame but let's ignore these issues
4947 ;; for the moment).
4948 (catch 'found
4949 (dolist (window windows)
4950 (when (window-full-width-p window)
4951 (throw 'found window)))
4952 ;; If there's no full-width window return the lru window.
4953 (car windows))))
4955 (when best-window
4956 (display-buffer-even-window-sizes best-window specifiers)
4957 ;; Never change the quit-restore parameter of a window here.
4958 (if (eq (window-buffer best-window) buffer)
4959 (setq display-buffer-window
4960 (cons best-window 'reuse-buffer-window))
4961 (setq display-buffer-window
4962 (cons best-window 'reuse-other-window))
4963 (unless (window-parameter best-window 'quit-restore)
4964 ;; Don't overwrite an existing quit-restore entry.
4965 (set-window-parameter
4966 best-window 'quit-restore
4967 (list (window-buffer best-window) (window-start best-window)
4968 (window-point best-window) buffer
4969 (window-total-size best-window) (selected-window)))))
4971 (display-buffer-in-window buffer best-window specifiers))))
4973 (defconst display-buffer-split-specifiers '(largest lru selected root left top right bottom)
4974 "List of symbols identifying window that shall be split.")
4976 (defconst display-buffer-side-specifiers '(below right above left nil)
4977 "List of symbols identifying side of split-off window.")
4979 (defun display-buffer-split-window-1 (window side min-size)
4980 "Subroutine of `display-buffer-split-window'."
4981 (let* ((horizontal (memq side '(left right)))
4982 (parent (window-parent window))
4983 (resize (and window-splits (window-iso-combined-p window horizontal)))
4984 (old-size
4985 ;; We either resize WINDOW or its parent.
4986 (window-total-size (if resize parent window) horizontal))
4987 new-size)
4988 ;; We don't call split-window-vertically/-horizontally any more
4989 ;; here. If for some reason it's needed we can always do so
4990 ;; (provided we give it an optional SIDE argument).
4991 (cond
4992 (resize
4993 ;; When we resize a combination, the new window must be at least
4994 ;; MIN-SIZE large after the split.
4995 (setq new-size
4996 (max min-size
4997 (min (- old-size (window-min-size parent horizontal))
4998 (/ old-size
4999 ;; Try to make the size of the new window
5000 ;; proportional to the number of iso-arranged
5001 ;; windows in the combination.
5002 (1+ (window-iso-combinations parent horizontal))))))
5003 (when (window-sizable-p parent (- new-size) horizontal)
5004 (split-window window (- new-size) side)))
5005 ((window-live-p window)
5006 (setq new-size (/ old-size 2))
5007 ;; When WINDOW is live, the old _and_ the new window must be at
5008 ;; least MIN-SIZE large after the split.
5009 (when (and (>= new-size min-size)
5010 (window-sizable-p window (- new-size) horizontal))
5011 ;; Do an even split to make Stepan happy.
5012 (split-window window nil side)))
5014 ;; When WINDOW is internal, the new window must be at least
5015 ;; MIN-SIZE large after the split.
5016 (setq new-size
5017 (max min-size
5018 (/ old-size
5019 ;; Try to make the size of the new window
5020 ;; proportional to the number of iso-arranged
5021 ;; subwindows of WINDOW.
5022 (1+ (window-iso-combinations window horizontal)))))
5023 (when (window-sizable-p window (- new-size) horizontal)
5024 (split-window window (- new-size) side))))))
5026 (defun display-buffer-split-window (window &optional side specifiers)
5027 "Split WINDOW in a way suitable for `display-buffer'.
5028 Optional argument SIDE must be a side specifier \(one of the
5029 symbols below, right, above, left, or nil). SPECIFIERS must be a
5030 list of buffer display specifiers, see the documentation of
5031 `display-buffer-alist' for a description.
5033 Return the new window, nil if it could not be created."
5034 (let ((min-height (cdr (assq 'pop-up-window-min-height specifiers)))
5035 (min-width (cdr (assq 'pop-up-window-min-width specifiers)))
5036 size)
5037 ;; Normalize min-height and min-width, we might need both.
5038 (setq min-height
5039 ;; If min-height is specified, it can be as small as
5040 ;; `window-safe-min-height'.
5041 (cond
5042 ((and (integerp min-height)
5043 (>= min-height window-safe-min-height))
5044 min-height)
5045 ((and (floatp min-height)
5046 (<= min-height 1)
5047 (let* ((root-height (window-total-height
5048 (frame-root-window
5049 (window-frame window))))
5050 (height (round (* min-height root-height))))
5051 (when (>= height window-safe-min-height)
5052 height))))
5053 (t window-min-height)))
5054 (setq min-width
5055 ;; If min-width is specified, it can be as small as
5056 ;; `window-safe-min-width'.
5057 (cond
5058 ((and (integerp min-width)
5059 (>= min-width window-safe-min-width))
5060 min-width)
5061 ((and (floatp min-width)
5062 (<= min-width 1)
5063 (let* ((root-width (window-total-width
5064 (frame-root-window
5065 (window-frame window))))
5066 (width (round (* min-width root-width))))
5067 (when (>= width window-safe-min-width)
5068 width))))
5069 (t window-min-width)))
5071 (or (and (memq side '(nil above below))
5072 (display-buffer-split-window-1
5073 window (or side 'below) min-height))
5074 ;; If SIDE is nil and vertical splitting failed, we try again
5075 ;; splitting horizontally this time.
5076 (and (memq side '(nil left right))
5077 (display-buffer-split-window-1
5078 window (or side 'right) min-width))
5079 ;; If WINDOW is live and the root window of its frame, try once
5080 ;; more splitting vertically, disregarding the min-height
5081 ;; specifier this time and using `window-min-height' instead.
5082 (and (memq side '(nil above below))
5083 (<= window-min-height min-height)
5084 (window-live-p window)
5085 (eq window (frame-root-window window))
5086 (display-buffer-split-window-1
5087 window (or side 'below) window-min-height)))))
5089 (defun display-buffer-split-atom-window (window &optional side nest specifiers)
5090 "Make WINDOW part of an atomic window."
5091 (let ((ignore-window-parameters t)
5092 (window-nest t)
5093 (selected-window (selected-window))
5094 root new new-parent)
5096 ;; We are in an atomic window.
5097 (when (and (window-parameter window 'window-atom) (not nest))
5098 ;; Split the root window.
5099 (setq window (window-atom-root window)))
5101 (when (setq new (display-buffer-split-window window side specifiers))
5102 (setq new-parent (window-parent window))
5103 ;; WINDOW is or becomes atomic.
5104 (unless (window-parameter window 'window-atom)
5105 (walk-window-subtree
5106 (lambda (window)
5107 (set-window-parameter window 'window-atom t))
5108 window t))
5109 ;; New window and any new parent get their window-atom parameter
5110 ;; set too.
5111 (set-window-parameter new 'window-atom t)
5112 (set-window-parameter new-parent 'window-atom t)
5113 new)))
5115 (defun display-buffer-pop-up-window (buffer methods &optional specifiers)
5116 "Display BUFFER in a new window.
5117 Return the window displaying BUFFER, nil if popping up the window
5118 failed. METHODS must be a list of window/side tuples like those
5119 forming the cdr of the `pop-up-window' buffer display specifier.
5120 As a special case, the car of such a tuple can be also a live
5121 window.
5123 Optional argument SPECIFIERS must be a list of buffer display
5124 specifiers, see the doc-string of `display-buffer-alist' for a
5125 description."
5126 (let* ((frame (display-buffer-frame))
5127 (selected-window (frame-selected-window frame))
5128 window side atomic)
5129 (unless (and (cdr (assq 'unsplittable (frame-parameters frame)))
5130 ;; Don't split an unsplittable frame unless
5131 ;; SPECIFIERS allow it.
5132 (not (cdr (assq 'split-unsplittable-frame specifiers))))
5133 (catch 'done
5134 (dolist (method methods)
5135 (setq window (car method))
5136 (setq side (cdr method))
5137 (and (setq window
5138 (cond
5139 ((eq window 'largest)
5140 (get-largest-window frame t))
5141 ((eq window 'lru)
5142 (get-lru-window frame t))
5143 ((eq window 'selected)
5144 (frame-selected-window frame))
5145 ((eq window 'root)
5146 ;; If there are side windows, split the main
5147 ;; window else the frame root window.
5148 (or (window-with-parameter 'window-side 'none nil t)
5149 (frame-root-window frame)))
5150 ((memq window window-sides)
5151 ;; This should gets us the "root" side
5152 ;; window if there exists more than one.
5153 (window-with-parameter 'window-side window nil t))
5154 ((windowp window)
5155 ;; A window, directly specified.
5156 window)))
5157 ;; The window must be on the selected frame,
5158 (eq (window-frame window) frame)
5159 ;; and must be neither a minibuffer window,
5160 (not (window-minibuffer-p window))
5161 ;; nor a side window.
5162 (not (eq (window-parameter window 'window-side) 'side))
5163 (setq window
5164 (cond
5165 ((memq side display-buffer-side-specifiers)
5166 (if (and (window-buffer window)
5167 (setq atomic (cdr (assq 'atomic specifiers))))
5168 (display-buffer-split-atom-window
5169 window side (eq atomic 'nest) specifiers)
5170 (display-buffer-split-window window side specifiers)))
5171 ((functionp side)
5172 (ignore-errors
5173 ;; Don't pass any specifiers to this function.
5174 (funcall side window)))))
5175 (throw 'done window))))
5177 (when window
5178 ;; Adjust sizes if asked for.
5179 (display-buffer-set-height window specifiers)
5180 (display-buffer-set-width window specifiers)
5181 (set-window-parameter
5182 window 'quit-restore (list 'new-window buffer selected-window))
5183 (setq display-buffer-window (cons window 'new-window))
5184 (display-buffer-in-window buffer window specifiers)
5185 (set-window-prev-buffers window nil)
5186 window))))
5188 (defun display-buffer-pop-up-frame (buffer &optional graphic-only specifiers)
5189 "Make a new frame for displaying BUFFER.
5190 Return the window displaying BUFFER if creating the new frame was
5191 successful, nil otherwise. Optional argument GRAPHIC-ONLY
5192 non-nil means to make a new frame on graphic displays only.
5194 SPECIFIERS must be a list of buffer display specifiers, see the
5195 documentation of `display-buffer-alist' for a description."
5196 (unless (and graphic-only (not (display-graphic-p)))
5197 (let* ((selected-window (selected-window))
5198 (function (or (cdr (assq 'pop-up-frame-function specifiers))
5199 'make-frame))
5200 (parameters
5201 (when (symbolp function)
5202 (cdr (assq 'pop-up-frame-alist specifiers))))
5203 (frame
5204 (if (symbolp function)
5205 (funcall function parameters)
5206 (funcall function))))
5207 (when frame
5208 (let ((window (frame-selected-window frame)))
5209 (set-window-parameter
5210 window 'quit-restore (list 'new-frame buffer selected-window))
5211 (setq display-buffer-window (cons window 'new-frame))
5212 (display-buffer-in-window buffer window specifiers))))))
5214 (defun display-buffer-pop-up-side-window (buffer side slot &optional specifiers)
5215 "Display BUFFER in a new window on SIDE of the selected frame.
5216 SLOT specifies the slot to use. SPECIFIERS must be a list of
5217 buffer display specifiers.
5219 Return the window displaying BUFFER, nil if popping up the window
5220 failed."
5221 (let* ((root (frame-root-window))
5222 (main (window-with-parameter 'window-side 'none nil t))
5223 (left-or-right (memq side '(left right)))
5224 (main-or-root
5225 (if (and main
5226 (or (and left-or-right (not window-sides-vertical))
5227 (and (not left-or-right) window-sides-vertical)))
5228 main
5229 root))
5230 (selected-window (selected-window))
5231 (on-side (cond
5232 ((eq side 'top) 'above)
5233 ((eq side 'bottom) 'below)
5234 (t side)))
5235 (window
5236 (display-buffer-split-window main-or-root on-side specifiers))
5237 fun)
5238 (when window
5239 (unless main
5240 (walk-window-subtree
5241 (lambda (window)
5242 ;; Make all main-or-root subwindows main windows.
5243 (set-window-parameter window 'window-side 'none))
5244 main-or-root t))
5245 ;; Make sure that parent's window-side is nil.
5246 (set-window-parameter (window-parent window) 'window-side nil)
5247 ;; Initialize side.
5248 (set-window-parameter window 'window-side side)
5249 ;; Adjust sizes if asked for.
5250 (display-buffer-set-height window specifiers)
5251 (display-buffer-set-width window specifiers)
5252 ;; Set window parameters.
5253 (set-window-parameter
5254 window 'quit-restore (list 'new-window buffer selected-window))
5255 (setq display-buffer-window (cons window 'new-window))
5256 (set-window-parameter window 'window-slot slot)
5257 (display-buffer-in-window buffer window specifiers)
5258 (set-window-prev-buffers window nil)
5259 window)))
5261 (defun display-buffer-in-side-window (buffer side &optional slot specifiers)
5262 "Display BUFFER in a window on SIDE of the selected frame.
5263 SLOT, if non-nil, specifies the window slot where to display the
5264 BUFFER. SLOT zero or nil means use the central slot on SIDE.
5265 SLOT negative means use a slot preceding the central window.
5266 SLOT positive means use a slot following the central window.
5268 SPECIFIERS must be a list of buffer display specifiers."
5269 (unless (memq side window-sides)
5270 (error "Invalid side %s specified" side))
5271 (let* ((major (window-with-parameter 'window-side side nil t))
5272 ;; `major' is the major window on SIDE, `windows' the life
5273 ;; windows on SIDE.
5274 (windows (when major (windows-with-parameter 'window-side side)))
5275 (slots (when major (window-child-count major)))
5276 (max-slots
5277 (nth (cond
5278 ((eq side 'left) 0)
5279 ((eq side 'top) 1)
5280 ((eq side 'right) 2)
5281 ((eq side 'bottom) 3))
5282 window-sides-slots))
5283 (selected-window (selected-window))
5284 window this-window this-slot prev-window next-window
5285 best-window best-slot abs-slot)
5287 (unless (numberp slot)
5288 (setq slot 0))
5289 (if (not windows)
5290 ;; No suitable side window exists, make one.
5291 (display-buffer-pop-up-side-window buffer side slot specifiers)
5292 ;; Scan windows on SIDE.
5293 (catch 'found
5294 (dolist (window windows)
5295 (setq this-slot (window-parameter window 'window-slot))
5296 (cond
5297 ((not (numberp this-slot)))
5298 ((and (= this-slot slot)
5299 ;; Dedicatedness check.
5300 (or (not (window-dedicated-p window))
5301 (assq 'reuse-window-dedicated specifiers)))
5302 ;; Window with matching SLOT, use it.
5303 (setq this-window window)
5304 (throw 'found t))
5306 (setq abs-slot (abs (- (abs slot) (abs this-slot))))
5307 (unless (and best-slot (<= best-slot abs-slot))
5308 (setq best-window window)
5309 (setq best-slot abs-slot))
5310 (cond
5311 ((<= this-slot slot)
5312 (setq prev-window window))
5313 ((not next-window)
5314 (setq next-window window)))))))
5316 ;; `this-window' is the first window with the same SLOT.
5317 ;; `prev-window' is the window with the largest slot < SLOT. A new
5318 ;; window will be created after it.
5319 ;; `next-window' is the window with the smallest slot > SLOT. A new
5320 ;; window will be created before it.
5321 ;; `best-window' is the window with the smallest absolute difference
5322 ;; of its slot and SLOT.
5323 (or (and this-window
5324 ;; Reuse this window.
5325 (prog1
5326 (setq window this-window)
5327 (if (eq (window-buffer window) buffer)
5328 (setq display-buffer-window
5329 (cons window 'reuse-buffer-window))
5330 (setq display-buffer-window
5331 (cons window 'reuse-other-window))
5332 (unless (window-parameter window 'quit-restore)
5333 ;; Don't overwrite an existing quit-restore entry.
5334 (set-window-parameter
5335 window 'quit-restore
5336 (list (window-buffer window) (window-start window)
5337 (window-point window) buffer
5338 (window-total-size window) (selected-window)))))))
5339 (and (or (not max-slots) (< slots max-slots))
5340 (or (and next-window
5341 ;; Make new window before next-window.
5342 (let ((next-side
5343 (if (memq side '(left right)) 'above 'left)))
5344 (setq window (display-buffer-split-window
5345 next-window next-side specifiers))))
5346 (and prev-window
5347 ;; Make new window after prev-window.
5348 (let ((prev-side
5349 (if (memq side '(left right)) 'below 'right)))
5350 (setq window (display-buffer-split-window
5351 prev-window prev-side specifiers)))))
5352 (progn
5353 (display-buffer-set-height window specifiers)
5354 (display-buffer-set-width window specifiers)
5355 (set-window-parameter
5356 window 'quit-restore
5357 (list 'new-window buffer selected-window))
5358 (setq display-buffer-window (cons window 'new-window))
5359 window))
5360 (and best-window
5361 (setq window best-window)
5362 ;; Reuse best window (the window nearest to SLOT).
5363 (if (eq (window-buffer window) buffer)
5364 (setq display-buffer-window
5365 (cons window 'reuse-buffer-window))
5366 (setq display-buffer-window
5367 (cons window 'reuse-other-window))
5369 (unless (window-parameter window 'quit-restore)
5370 ;; Don't overwrite an existing quit-restore entry.
5371 (set-window-parameter
5372 window 'quit-restore
5373 (list (window-buffer window) (window-start window)
5374 (window-point window) buffer
5375 (window-total-size window) (selected-window)))))
5376 window))
5378 (when window
5379 (unless (window-parameter window 'window-slot)
5380 ;; Don't change exisiting slot value.
5381 (set-window-parameter window 'window-slot slot))
5382 (display-buffer-in-window buffer window specifiers)))))
5384 (defun normalize-buffer-to-display (buffer-or-name)
5385 "Normalize BUFFER-OR-NAME argument for buffer display functions.
5386 If BUFFER-OR-NAME is nil, return the curent buffer. Else, if a
5387 buffer specified by BUFFER-OR-NAME exists, return that buffer.
5388 If no such buffer exists, create a buffer with the name
5389 BUFFER-OR-NAME and return that buffer."
5390 (if buffer-or-name
5391 (or (get-buffer buffer-or-name)
5392 (let ((buffer (get-buffer-create buffer-or-name)))
5393 (set-buffer-major-mode buffer)
5394 buffer))
5395 (current-buffer)))
5397 (defun display-buffer-normalize-specifiers-1 (specifiers)
5398 "Subroutine of `display-buffer-normalize-specifiers'."
5399 (let (normalized)
5400 (cond
5401 ((listp specifiers)
5402 (dolist (specifier specifiers)
5403 (cond
5404 ((consp specifier)
5405 (setq normalized (cons specifier normalized)))
5406 ((symbolp specifier)
5407 ;; Might be a macro specifier, try to expand it (the cdr is a
5408 ;; list and we have to reverse it later, so do it one at a
5409 ;; time).
5410 (let ((entry (assq specifier display-buffer-macro-specifiers)))
5411 (dolist (item (cdr entry))
5412 (setq normalized (cons item normalized)))))))
5413 ;; Reverse list.
5414 (setq normalized (nreverse normalized)))
5415 ;; The two cases below must come from the SPECIFIERS argument of
5416 ;; `display-buffer'.
5417 ((eq specifiers 't)
5418 ;; Historically t means "other window". Eventually we should get
5419 ;; rid of this.
5420 (setq normalized
5421 (cdr (assq 'other-window display-buffer-macro-specifiers))
5422 normalized))
5423 ((symbolp specifiers)
5424 ;; We allow scalar specifiers in calls of `display-buffer'.
5425 (let ((entry (assq specifiers display-buffer-macro-specifiers)))
5426 (when entry (setq normalized (cdr entry))))))
5428 normalized))
5430 (defun display-buffer-normalize-specifiers (buffer-name specifiers label)
5431 "Return normalized specifiers for a buffer matching BUFFER-NAME or LABEL.
5432 BUFFER-NAME must be a string specifying a valid buffer name.
5433 SPECIFIERS and LABEL are the homonymous arguments of
5434 `display-buffer'.
5436 The method for displaying the buffer specified by BUFFER-NAME or
5437 LABEL is established by appending the following four lists of
5438 specifiers:
5440 - The specifiers in `display-buffer-alist' whose buffer
5441 identifier matches BUFFER-NAME or LABEL and whose 'override
5442 component is set.
5444 - SPECIFIERS.
5446 - The specifiers in `display-buffer-alist' whose buffer
5447 identifier matches BUFFER-NAME or LABEL and whose 'override
5448 component is not set.
5450 - `display-buffer-default-specifiers'."
5451 (let (list-1 list-2)
5452 (dolist (entry display-buffer-alist)
5453 (when (and (listp entry)
5454 (catch 'match
5455 (dolist (id (car entry))
5456 (when (consp id)
5457 (let ((type (car id))
5458 (value (cdr id)))
5459 (when (or (and (eq type 'name) (stringp value)
5460 (equal value buffer-name))
5461 (and (eq type 'regexp) (stringp value)
5462 (string-match-p value buffer-name))
5463 (and (eq type 'label) (eq value label)))
5464 (throw 'match t)))))))
5465 (let* ((raw (cdr entry))
5466 (normalized (display-buffer-normalize-specifiers-1 raw)))
5467 (if (assq 'override raw)
5468 (setq list-1
5469 (if list-1
5470 (append list-1 normalized)
5471 normalized))
5472 (setq list-2
5473 (if list-2
5474 (append list-2 normalized)
5475 normalized))))))
5477 (append
5478 list-1
5479 (display-buffer-normalize-specifiers-1 specifiers)
5480 list-2
5481 ;; Append the default specifiers.
5482 display-buffer-default-specifiers)))
5484 ;; Minibuffer-only frames should be documented better. They really
5485 ;; deserve a separate section in the manual. Also
5486 ;; `last-nonminibuffer-frame' is nowhere documented in the manual.
5487 (defun display-buffer-frame (&optional frame)
5488 "Return FRAME if it is live and not a minibuffer-only frame.
5489 Return the value of `last-nonminibuffer-frame' otherwise."
5490 (setq frame (normalize-live-frame frame))
5491 (if (and (frame-live-p frame)
5492 ;; A not very nice way to get that information.
5493 (not (window-minibuffer-p (frame-root-window frame))))
5494 frame
5495 (last-nonminibuffer-frame)))
5497 (defun display-buffer (&optional buffer-or-name specifiers label)
5498 "Make the buffer specified by BUFFER-OR-NAME appear in some window.
5499 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5500 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5501 an existent buffer, create a buffer with that name. If
5502 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5503 Interactively, prompt for the buffer name using the minibuffer.
5505 Return the window chosen to display the buffer or nil if no such
5506 window is found. Do not change the selected window unless the
5507 buffer is shown on a different frame than the selected one.
5509 Optional argument SPECIFIERS must be a list of buffer display
5510 specifiers, see the documentation of `display-buffer-alist' for a
5511 description.
5513 For convenience, SPECIFIERS may also consist of a single buffer
5514 display location specifier or t, where the latter means to
5515 display the buffer in any but the selected window. If SPECIFIERS
5516 is nil or omitted, this means to exclusively use the specifiers
5517 provided by `display-buffer-alist'. If the value of the latter
5518 is nil too, all specifiers are provided by the constant
5519 `display-buffer-default-specifiers'.
5521 As a special case, the `reuse-window' specifier allows to specify
5522 as second element an arbitrary window, as third element an
5523 arbitrary buffer, and as fourth element an arbitrary frame. As
5524 first element of a window/side pair of the `pop-up-window'
5525 specifier you can specifiy an arbitrary window.
5527 The optional third argument LABEL, if non-nil, must be a symbol
5528 specifiying the buffer display label. Applications should set
5529 this when the buffer shall be displayed in some special way but
5530 BUFFER-OR-NAME does not identify the buffer as special. Typical
5531 buffers that fit into this category are those whose names are
5532 derived from the name of the file they are visiting. A user can
5533 override SPECIFIERS by adding an entry to `display-buffer-alist'
5534 whose car contains LABEL and whose cdr specifies the preferred
5535 alternative display method.
5537 The method to display the buffer is derived by combining the
5538 values of `display-buffer-alist' and SPECIFIERS. Highest
5539 priority is given to overriding elements of
5540 `display-buffer-alist'. Next come the elements specified by
5541 SPECIFIERS, followed by the non-overriding elements of
5542 `display-buffer-alist'.
5544 The result must be a list of valid buffer display specifiers. If
5545 `display-buffer-function' is non-nil, call it with the buffer and
5546 this list as arguments."
5547 (interactive "BDisplay buffer:\nP")
5548 (let* ((buffer (normalize-buffer-to-display buffer-or-name))
5549 (buffer-name (buffer-name buffer))
5550 (specifiers
5551 ;; Normalize specifiers.
5552 (display-buffer-normalize-specifiers buffer-name specifiers label))
5553 ;; Don't use a minibuffer frame.
5554 (frame (display-buffer-frame))
5555 ;; `window' is the window we use for showing `buffer'.
5556 window specifier method)
5557 ;; Reset this.
5558 (setq display-buffer-window nil)
5559 (if display-buffer-function
5560 ;; Let `display-buffer-function' do the job.
5561 (funcall display-buffer-function buffer specifiers)
5562 ;; Retrieve the next location specifier while there a specifiers
5563 ;; left and we don't have a valid window.
5564 (while (and specifiers (not (window-live-p window)))
5565 (setq specifier (car specifiers))
5566 (setq specifiers (cdr specifiers))
5567 (setq method (car specifier))
5568 (setq window
5569 (cond
5570 ((eq method 'reuse-window)
5571 (display-buffer-reuse-window
5572 buffer (cdr specifier) specifiers))
5573 ((eq method 'pop-up-window)
5574 (display-buffer-pop-up-window
5575 buffer (cdr specifier) specifiers))
5576 ((eq method 'pop-up-frame)
5577 (display-buffer-pop-up-frame
5578 buffer (cdr specifier) specifiers))
5579 ((eq method 'use-side-window)
5580 (display-buffer-in-side-window
5581 buffer (nth 1 specifier) (nth 2 specifier) specifiers))
5582 ((eq method 'fun-with-args)
5583 (apply (cadr specifier) buffer (cddr specifier))))))
5585 ;; If we don't have a window yet, try a fallback method. All
5586 ;; specifiers have been used up by now.
5587 (or (and (window-live-p window) window)
5588 ;; Try reusing a window showing BUFFER on any visible or
5589 ;; iconfied frame.
5590 (display-buffer-reuse-window buffer '(nil buffer 0))
5591 ;; Try reusing a window not showing BUFFER on any visible or
5592 ;; iconified frame.
5593 (display-buffer-reuse-window buffer '(nil other 0))
5594 ;; Try making a new frame.
5595 (display-buffer-pop-up-frame buffer)
5596 ;; Try using weakly dedicated windows.
5597 (display-buffer-reuse-window
5598 buffer '(nil nil t) '((reuse-window-dedicated . weak)))
5599 ;; Try using strongly dedicated windows.
5600 (display-buffer-reuse-window
5601 buffer '(nil nil t) '((reuse-window-dedicated . t)))))))
5603 (defsubst display-buffer-same-window (&optional buffer-or-name label)
5604 "Display buffer specified by BUFFER-OR-NAME in the selected window.
5605 Another window will be used only if the buffer can't be shown in
5606 the selected window, usually because it is dedicated to another
5607 buffer. Optional argument BUFFER-OR-NAME and LABEL are as for
5608 `display-buffer'."
5609 (interactive "BDisplay buffer in same window:\nP")
5610 (display-buffer buffer-or-name 'same-window label))
5612 (defsubst display-buffer-same-frame (&optional buffer-or-name label)
5613 "Display buffer specified by BUFFER-OR-NAME in a window on the same frame.
5614 Another frame will be used only if there is no other choice.
5615 Optional argument BUFFER-OR-NAME and LABEL are as for
5616 `display-buffer'."
5617 (interactive "BDisplay buffer on same frame:\nP")
5618 (display-buffer buffer-or-name 'same-frame label))
5620 (defsubst display-buffer-other-window (&optional buffer-or-name label)
5621 "Display buffer specified by BUFFER-OR-NAME in another window.
5622 The selected window will be used only if there is no other
5623 choice. Windows on the selected frame are preferred to windows
5624 on other frames. Optional argument BUFFER-OR-NAME and LABEL are as
5625 for `display-buffer'."
5626 (interactive "BDisplay buffer in another window:\nP")
5627 (display-buffer buffer-or-name 'other-window label))
5629 (defun display-buffer-same-frame-other-window (&optional buffer-or-name label)
5630 "Display buffer specified by BUFFER-OR-NAME in another window on the same frame.
5631 The selected window or another frame will be used only if there
5632 is no other choice. Optional argument BUFFER-OR-NAME and LABEL are
5633 as for `display-buffer'."
5634 (interactive "BDisplay buffer in another window on same frame:\nP")
5635 (display-buffer buffer-or-name 'same-frame-other-window label))
5637 (defun pop-to-buffer (&optional buffer-or-name specifiers norecord label)
5638 "Display buffer specified by BUFFER-OR-NAME and select the window used.
5639 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5640 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5641 an existent buffer, create a buffer with that name. If
5642 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5643 Interactively, prompt for the buffer name using the minibuffer.
5645 Optional second argument SPECIFIERS must be a list of buffer
5646 display specifiers, a single location specifier, `t' which means
5647 the latter means to display the buffer in any but the selected
5648 window, or nil which means to exclusively apply the specifiers
5649 customized by the user.
5651 Optional argument NORECORD non-nil means do not put the buffer
5652 specified by BUFFER-OR-NAME at the front of the buffer list and
5653 do not make the window displaying it the most recently selected
5654 one.
5656 The optional argument LABEL, if non-nil, is a symbol specifying the
5657 display purpose. Applications should set this when the buffer
5658 shall be displayed in a special way but BUFFER-OR-NAME does not
5659 identify the buffer as special. Buffers that typically fit into
5660 this category are those whose names have been derived from the
5661 name of the file they are visiting.
5663 Return the buffer specified by BUFFER-OR-NAME or nil if
5664 displaying the buffer failed.
5666 This uses the function `display-buffer' as a subroutine; see the
5667 documentations of `display-buffer' and `display-buffer-alist' for
5668 additional information."
5669 (interactive "BPop to buffer:\nP")
5670 (let ((buffer (normalize-buffer-to-display buffer-or-name))
5671 window)
5672 (set-buffer buffer)
5673 (when (setq window (display-buffer buffer specifiers label))
5674 (select-window window norecord)
5675 buffer)))
5677 (defsubst pop-to-buffer-same-window (&optional buffer-or-name norecord label)
5678 "Pop to buffer specified by BUFFER-OR-NAME in the selected window.
5679 Another window will be used only if the buffer can't be shown in
5680 the selected window, usually because it is dedicated to another
5681 buffer. Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are
5682 as for `pop-to-buffer'."
5683 (interactive "BPop to buffer in selected window:\nP")
5684 (pop-to-buffer buffer-or-name 'same-window norecord label))
5686 (defsubst pop-to-buffer-same-frame (&optional buffer-or-name norecord label)
5687 "Pop to buffer specified by BUFFER-OR-NAME in a window on the selected frame.
5688 Another frame will be used only if there is no other choice.
5689 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5690 `pop-to-buffer'."
5691 (interactive "BPop to buffer on same frame:\nP")
5692 (pop-to-buffer buffer-or-name 'same-frame norecord label))
5694 (defsubst pop-to-buffer-other-window (&optional buffer-or-name norecord label)
5695 "Pop to buffer specified by BUFFER-OR-NAME in another window.
5696 The selected window will be used only if there is no other
5697 choice. Windows on the selected frame are preferred to windows
5698 on other frames. Optional arguments BUFFER-OR-NAME, NORECORD and
5699 LABEL are as for `pop-to-buffer'."
5700 (interactive "BPop to buffer in another window:\nP")
5701 (pop-to-buffer buffer-or-name 'other-window norecord))
5703 (defsubst pop-to-buffer-same-frame-other-window (&optional buffer-or-name norecord label)
5704 "Pop to buffer specified by BUFFER-OR-NAME in another window on the selected frame.
5705 The selected window or another frame will be used only if there
5706 is no other choice. Optional arguments BUFFER-OR-NAME, NORECORD
5707 and LABEL are as for `pop-to-buffer'."
5708 (interactive "BPop to buffer in another window on same frame:\nP")
5709 (pop-to-buffer buffer-or-name 'same-frame-other-window norecord label))
5711 (defsubst pop-to-buffer-other-frame (&optional buffer-or-name norecord label)
5712 "Pop to buffer specified by BUFFER-OR-NAME on another frame.
5713 The selected frame will be used only if there's no other choice.
5714 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5715 `pop-to-buffer'."
5716 (interactive "BPop to buffer on another frame:\nP")
5717 (pop-to-buffer buffer-or-name 'other-frame norecord label))
5719 (defun read-buffer-to-switch (prompt)
5720 "Read the name of a buffer to switch to, prompting with PROMPT.
5721 Return the neame of the buffer as a string.
5723 This function is intended for the `switch-to-buffer' family of
5724 commands since these need to omit the name of the current buffer
5725 from the list of completions and default values."
5726 (let ((rbts-completion-table (internal-complete-buffer-except)))
5727 (minibuffer-with-setup-hook
5728 (lambda ()
5729 (setq minibuffer-completion-table rbts-completion-table)
5730 ;; Since rbts-completion-table is built dynamically, we
5731 ;; can't just add it to the default value of
5732 ;; icomplete-with-completion-tables, so we add it
5733 ;; here manually.
5734 (if (and (boundp 'icomplete-with-completion-tables)
5735 (listp icomplete-with-completion-tables))
5736 (set (make-local-variable 'icomplete-with-completion-tables)
5737 (cons rbts-completion-table
5738 icomplete-with-completion-tables))))
5739 (read-buffer prompt (other-buffer (current-buffer))
5740 (confirm-nonexistent-file-or-buffer)))))
5742 (defun normalize-buffer-to-switch-to (buffer-or-name)
5743 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5744 If BUFFER-OR-NAME is nil, return the buffer returned by
5745 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5746 exists, return that buffer. If no such buffer exists, create a
5747 buffer with the name BUFFER-OR-NAME and return that buffer."
5748 (if buffer-or-name
5749 (or (get-buffer buffer-or-name)
5750 (let ((buffer (get-buffer-create buffer-or-name)))
5751 (set-buffer-major-mode buffer)
5752 buffer))
5753 (other-buffer)))
5755 (defun switch-to-buffer (buffer-or-name &optional norecord)
5756 "Switch to buffer BUFFER-OR-NAME in the selected window.
5757 If called interactively, prompt for the buffer name using the
5758 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5759 determines whether to request confirmation before creating a new
5760 buffer.
5762 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5763 nil. If BUFFER-OR-NAME is a string that does not identify an
5764 existing buffer, create a buffer with that name. If
5765 BUFFER-OR-NAME is nil, switch to the buffer returned by
5766 `other-buffer'.
5768 Optional argument NORECORD non-nil means do not put the buffer
5769 specified by BUFFER-OR-NAME at the front of the buffer list and
5770 do not make the window displaying it the most recently selected
5771 one. Return the buffer switched to.
5773 This function is intended for interactive use only. Lisp
5774 functions should call `pop-to-buffer-same-window' instead."
5775 (interactive
5776 (list (read-buffer-to-switch "Switch to buffer: ")))
5777 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5778 (if (and (or (window-minibuffer-p) (eq (window-dedicated-p) t))
5779 (not (eq buffer (window-buffer))))
5780 ;; Cannot switch to another buffer in a minibuffer or strongly
5781 ;; dedicated window that does not show the buffer already. Call
5782 ;; `pop-to-buffer' instead.
5783 (pop-to-buffer buffer 'same-window norecord)
5784 (unless (eq buffer (window-buffer))
5785 ;; I'm not sure why we should NOT call `set-window-buffer' here,
5786 ;; but let's keep things as they are (otherwise we could always
5787 ;; call `pop-to-buffer-same-window' here).
5788 (set-window-buffer nil buffer))
5789 (unless norecord
5790 (select-window (selected-window)))
5791 (set-buffer buffer))))
5793 (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord)
5794 "Switch to buffer BUFFER-OR-NAME in a window on the selected frame.
5795 Another frame will be used only if there is no other choice.
5796 Optional arguments BUFFER-OR-NAME and NORECORD have the same
5797 meaning as for `switch-to-buffer'.
5799 This function is intended for interactive use only. Lisp
5800 functions should call `pop-to-buffer-same-frame' instead."
5801 (interactive
5802 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5803 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5804 (pop-to-buffer buffer 'same-frame norecord)))
5806 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5807 "Switch to buffer BUFFER-OR-NAME in another window.
5808 The selected window will be used only if there is no other
5809 choice. Windows on the selected frame are preferred to windows
5810 on other frames. Optional arguments BUFFER-OR-NAME and NORECORD
5811 have the same meaning as for `switch-to-buffer'.
5813 This function is intended for interactive use only. Lisp
5814 functions should call `pop-to-buffer-other-window' instead."
5815 (interactive
5816 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5817 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5818 (pop-to-buffer buffer 'other-window norecord)))
5820 (defun switch-to-buffer-other-window-same-frame (buffer-or-name &optional norecord)
5821 "Switch to buffer BUFFER-OR-NAME in another window on the selected frame.
5822 The selected window or another frame will be used only if there
5823 is no other choice. Optional arguments BUFFER-OR-NAME and
5824 NORECORD have the same meaning as for `switch-to-buffer'.
5826 This function is intended for interactive use only. Lisp
5827 functions should call `pop-to-buffer-other-window-same-frame'
5828 instead."
5829 (interactive
5830 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5831 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5832 (pop-to-buffer buffer 'same-frame-other-window norecord)))
5834 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5835 "Switch to buffer BUFFER-OR-NAME on another frame.
5836 The same frame will be used only if there is no other choice.
5837 Optional arguments BUFFER-OR-NAME and NORECORD have the same
5838 meaning as for `switch-to-buffer'.
5840 This function is intended for interactive use only. Lisp
5841 functions should call `pop-to-buffer-other-frame' instead."
5842 (interactive
5843 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5844 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5845 (pop-to-buffer buffer 'other-frame norecord)))
5847 ;;; Obsolete definitions of `display-buffer' below.
5848 (defcustom pop-up-frame-alist nil
5849 "Alist of parameters for automatically generated new frames.
5850 You can set this in your init file; for example,
5852 (setq pop-up-frame-alist '((width . 80) (height . 20)))
5854 If non-nil, the value you specify here is used by the default
5855 `pop-up-frame-function' for the creation of new frames.
5857 Since `pop-up-frame-function' is used by `display-buffer' for
5858 making new frames, any value specified here by default affects
5859 the automatic generation of new frames via `display-buffer' and
5860 all functions based on it. The behavior of `make-frame' is not
5861 affected by this variable."
5862 :type '(repeat (cons :format "%v"
5863 (symbol :tag "Parameter")
5864 (sexp :tag "Value")))
5865 :group 'frames)
5866 (make-obsolete-variable
5867 'pop-up-frame-alist
5868 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
5869 "24.1")
5871 (defcustom pop-up-frame-function
5872 (lambda () (make-frame pop-up-frame-alist))
5873 "Function used by `display-buffer' for creating a new frame.
5874 This function is called with no arguments and should return a new
5875 frame. The default value calls `make-frame' with the argument
5876 `pop-up-frame-alist'."
5877 :type 'function
5878 :group 'frames)
5879 (make-obsolete-variable
5880 'pop-up-frame-function
5881 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
5882 "24.1")
5884 (defcustom special-display-frame-alist
5885 '((height . 14) (width . 80) (unsplittable . t))
5886 "Alist of parameters for special frames.
5887 Special frames are used for buffers whose names are listed in
5888 `special-display-buffer-names' and for buffers whose names match
5889 one of the regular expressions in `special-display-regexps'.
5891 This variable can be set in your init file, like this:
5893 (setq special-display-frame-alist '((width . 80) (height . 20)))
5895 These supersede the values given in `default-frame-alist'."
5896 :type '(repeat (cons :format "%v"
5897 (symbol :tag "Parameter")
5898 (sexp :tag "Value")))
5899 :group 'frames)
5900 (make-obsolete-variable
5901 'special-display-frame-alist
5902 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
5903 "24.1")
5905 (defun special-display-popup-frame (buffer &optional args)
5906 "Display BUFFER in a special frame and return the window chosen.
5907 If BUFFER is already displayed in a visible or iconified frame,
5908 raise that frame. Otherwise, display BUFFER in a way as
5909 specified by optional argument ARGS.
5911 If ARGS is an alist, use it as a list of frame parameters. If
5912 these parameters contain \(same-window . t), display BUFFER in
5913 the selected window. If they contain \(same-frame . t), display
5914 BUFFER in a window on the selected frame.
5916 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5917 function to do the work. Pass it BUFFER as first argument,
5918 and (cdr ARGS) as second."
5919 (if (and args (symbolp (car args)))
5920 (apply (car args) buffer (cdr args))
5921 (let ((window (get-buffer-window buffer 0)))
5923 ;; If we have a window already, make it visible.
5924 (when window
5925 (let ((frame (window-frame window)))
5926 (make-frame-visible frame)
5927 (raise-frame frame)
5928 window))
5929 ;; Reuse the current window if the user requested it.
5930 (when (cdr (assq 'same-window args))
5931 (display-buffer-reuse-window
5932 buffer '(same nil nil) '((reuse-dedicated . 'weak))))
5933 ;; Stay on the same frame if requested.
5934 (when (or (cdr (assq 'same-frame args))
5935 (cdr (assq 'same-window args)))
5936 (or (display-buffer-pop-up-window
5937 buffer '((largest . nil) (lru . nil)))
5938 (display-buffer-reuse-window
5939 buffer '(nil nil nil))))
5940 ;; If no window yet, make one in a new frame.
5941 (let ((frame
5942 (with-current-buffer buffer
5943 (make-frame
5944 (append args (with-no-warnings
5945 special-display-frame-alist))))))
5946 (set-window-buffer (frame-selected-window frame) buffer)
5947 (set-window-dedicated-p (frame-selected-window frame) t)
5948 (frame-selected-window frame))))))
5949 (make-obsolete
5950 'special-display-popup-frame
5951 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
5952 "24.1")
5954 (defcustom special-display-buffer-names nil
5955 "List of names of buffers that should be displayed specially.
5956 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5957 its name is in this list, displays the buffer in a way specified
5958 by `special-display-function'. `special-display-popup-frame'
5959 \(the default for `special-display-function') usually displays
5960 the buffer in a separate frame made with the parameters specified
5961 by `special-display-frame-alist'. If `special-display-function'
5962 has been set to some other function, that function is called with
5963 the buffer as first, and nil as second argument.
5965 Alternatively, an element of this list can be specified as
5966 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5967 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
5968 `special-display-popup-frame' will interpret such pairs as frame
5969 parameters when it creates a special frame, overriding the
5970 corresponding values from `special-display-frame-alist'.
5972 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5973 `special-display-popup-frame' displays that buffer in the
5974 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5975 it displays that buffer in a window on the selected frame.
5977 If `special-display-function' specifies some other function than
5978 `special-display-popup-frame', that function is called with the
5979 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5980 argument.
5982 Finally, an element of this list can be also specified as
5983 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5984 `special-display-popup-frame' will call FUNCTION with the buffer
5985 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5986 second. If `special-display-function' specifies some other
5987 function, that function is called with the buffer named
5988 BUFFER-NAME as first, and the element's cdr as second argument.
5990 If this variable appears \"not to work\", because you added a
5991 name to it but the corresponding buffer is displayed in the
5992 selected window, look at the values of `same-window-buffer-names'
5993 and `same-window-regexps'. Those variables take precedence over
5994 this one.
5996 See also `special-display-regexps'."
5997 :type '(repeat
5998 (choice :tag "Buffer"
5999 :value ""
6000 (string :format "%v")
6001 (cons :tag "With parameters"
6002 :format "%v"
6003 :value ("" . nil)
6004 (string :format "%v")
6005 (repeat :tag "Parameters"
6006 (cons :format "%v"
6007 (symbol :tag "Parameter")
6008 (sexp :tag "Value"))))
6009 (list :tag "With function"
6010 :format "%v"
6011 :value ("" . nil)
6012 (string :format "%v")
6013 (function :tag "Function")
6014 (repeat :tag "Arguments" (sexp)))))
6015 :group 'display-buffer
6016 :group 'frames)
6017 (make-obsolete-variable
6018 'special-display-buffer-names
6019 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6020 "24.1")
6022 ;;;###autoload
6023 (put 'special-display-buffer-names 'risky-local-variable t)
6025 (defcustom special-display-regexps nil
6026 "List of regexps saying which buffers should be displayed specially.
6027 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6028 any regexp in this list matches its name, displays it specially
6029 using `special-display-function'.
6031 The function `special-display-popup-frame' \(the default for
6032 `special-display-function') usually displays the buffer in a
6033 separate frame made with the parameters specified by
6034 `special-display-frame-alist'. If `special-display-function' has
6035 been set to some other function, that function is called with the
6036 buffer as first, and nil as second argument.
6038 Alternatively, an element of this list can be specified as
6039 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
6040 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6041 `special-display-popup-frame' will then interpret these pairs as
6042 frame parameters when creating a special frame for a buffer whose
6043 name matches REGEXP, overriding the corresponding values from
6044 `special-display-frame-alist'.
6046 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6047 `special-display-popup-frame' displays buffers matching REGEXP in
6048 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
6049 to display such buffers in a window on the selected frame.
6051 If `special-display-function' specifies some other function than
6052 `special-display-popup-frame', that function is called with the
6053 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
6054 as second argument.
6056 Finally, an element of this list can be also specified as
6057 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
6058 will then call FUNCTION with the buffer whose name matched
6059 REGEXP as first, and OTHER-ARGS as second argument. If
6060 `special-display-function' specifies some other function, that
6061 function is called with the buffer whose name matched REGEXP
6062 as first, and the element's cdr as second argument.
6064 If this variable appears \"not to work\", because you added a
6065 name to it but the corresponding buffer is displayed in the
6066 selected window, look at the values of `same-window-buffer-names'
6067 and `same-window-regexps'. Those variables take precedence over
6068 this one.
6070 See also `special-display-buffer-names'."
6071 :type '(repeat
6072 (choice :tag "Buffer"
6073 :value ""
6074 (regexp :format "%v")
6075 (cons :tag "With parameters"
6076 :format "%v"
6077 :value ("" . nil)
6078 (regexp :format "%v")
6079 (repeat :tag "Parameters"
6080 (cons :format "%v"
6081 (symbol :tag "Parameter")
6082 (sexp :tag "Value"))))
6083 (list :tag "With function"
6084 :format "%v"
6085 :value ("" . nil)
6086 (regexp :format "%v")
6087 (function :tag "Function")
6088 (repeat :tag "Arguments" (sexp)))))
6089 :group 'display-buffer
6090 :group 'frames)
6091 (make-obsolete-variable
6092 'special-display-regexps
6093 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6094 "24.1")
6096 (defun special-display-p (buffer-name)
6097 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
6098 More precisely, return t if `special-display-buffer-names' or
6099 `special-display-regexps' contain a string entry equaling or
6100 matching BUFFER-NAME. If `special-display-buffer-names' or
6101 `special-display-regexps' contain a list entry whose car equals
6102 or matches BUFFER-NAME, the return value is the cdr of that
6103 entry."
6104 (let ((buffer-names (with-no-warnings special-display-buffer-names))
6105 (regexps (with-no-warnings special-display-regexps))
6106 tmp)
6107 (cond
6108 ((not (stringp buffer-name)))
6109 ((member buffer-name buffer-names)
6111 ((setq tmp (assoc buffer-name buffer-names))
6112 (cdr tmp))
6113 ((catch 'found
6114 (dolist (regexp regexps)
6115 (cond
6116 ((stringp regexp)
6117 (when (string-match-p regexp buffer-name)
6118 (throw 'found t)))
6119 ((and (consp regexp) (stringp (car regexp))
6120 (string-match-p (car regexp) buffer-name))
6121 (throw 'found (cdr regexp))))))))))
6122 (make-obsolete 'special-display-p "pass argument to buffer display function instead." "24.1")
6124 (defcustom special-display-function 'special-display-popup-frame
6125 "Function to call for displaying special buffers.
6126 This function is called with two arguments - the buffer and,
6127 optionally, a list - and should return a window displaying that
6128 buffer. The default value usually makes a separate frame for the
6129 buffer using `special-display-frame-alist' to specify the frame
6130 parameters. See the definition of `special-display-popup-frame'
6131 for how to specify such a function.
6133 A buffer is special when its name is either listed in
6134 `special-display-buffer-names' or matches a regexp in
6135 `special-display-regexps'."
6136 :type 'function
6137 :group 'frames)
6138 (make-obsolete-variable
6139 'special-display-function
6140 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6141 "24.1")
6143 (defcustom same-window-buffer-names nil
6144 "List of names of buffers that should appear in the \"same\" window.
6145 `display-buffer' and `pop-to-buffer' show a buffer whose name is
6146 on this list in the selected rather than some other window.
6148 An element of this list can be a cons cell instead of just a
6149 string. In that case, the cell's car must be a string specifying
6150 the buffer name. This is for compatibility with
6151 `special-display-buffer-names'; the cdr of the cons cell is
6152 ignored.
6154 See also `same-window-regexps'."
6155 :type '(repeat (string :format "%v"))
6156 :group 'display-buffer)
6157 (make-obsolete-variable
6158 'same-window-buffer-names
6159 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6160 "24.1")
6162 (defcustom same-window-regexps nil
6163 "List of regexps saying which buffers should appear in the \"same\" window.
6164 `display-buffer' and `pop-to-buffer' show a buffer whose name
6165 matches a regexp on this list in the selected rather than some
6166 other window.
6168 An element of this list can be a cons cell instead of just a
6169 string. In that case, the cell's car must be a regexp matching
6170 the buffer name. This is for compatibility with
6171 `special-display-regexps'; the cdr of the cons cell is ignored.
6173 See also `same-window-buffer-names'."
6174 :type '(repeat (regexp :format "%v"))
6175 :group 'display-buffer)
6176 (make-obsolete-variable
6177 'same-window-regexps
6178 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6179 "24.1")
6181 (defun same-window-p (buffer-name)
6182 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
6183 This function returns non-nil if `display-buffer' or
6184 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
6185 selected rather than \(as usual\) some other window. See
6186 `same-window-buffer-names' and `same-window-regexps'."
6187 (let ((buffer-names (with-no-warnings special-display-buffer-names))
6188 (regexps (with-no-warnings special-display-regexps)))
6189 (cond
6190 ((not (stringp buffer-name)))
6191 ;; The elements of `same-window-buffer-names' can be buffer
6192 ;; names or cons cells whose cars are buffer names.
6193 ((member buffer-name buffer-names))
6194 ((assoc buffer-name buffer-names))
6195 ((catch 'found
6196 (dolist (regexp regexps)
6197 ;; The elements of `same-window-regexps' can be regexps
6198 ;; or cons cells whose cars are regexps.
6199 (when (or (and (stringp regexp)
6200 (string-match regexp buffer-name))
6201 (and (consp regexp) (stringp (car regexp))
6202 (string-match-p (car regexp) buffer-name)))
6203 (throw 'found t))))))))
6204 (make-obsolete 'same-window-p "pass argument to buffer display function instead." "24.1")
6206 (defcustom pop-up-frames nil
6207 "Whether `display-buffer' should make a separate frame.
6208 If nil, never make a separate frame.
6209 If the value is `graphic-only', make a separate frame
6210 on graphic displays only.
6211 Any other non-nil value means always make a separate frame."
6212 :type '(choice
6213 (const :tag "Never" nil)
6214 (const :tag "On graphic displays only" graphic-only)
6215 (const :tag "Always" t))
6216 :group 'display-buffer
6217 :group 'frames)
6218 (make-obsolete-variable
6219 'pop-up-frames
6220 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6221 "24.1")
6223 (defcustom display-buffer-reuse-frames nil
6224 "Non-nil means `display-buffer' should reuse frames.
6225 If the buffer in question is already displayed in a frame, raise
6226 that frame."
6227 :type 'boolean
6228 :version "21.1"
6229 :group 'display-buffer
6230 :group 'frames)
6231 (make-obsolete-variable
6232 'display-buffer-reuse-frames
6233 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6234 "24.1")
6236 (defcustom pop-up-windows t
6237 "Non-nil means `display-buffer' is allowed to make a new window.
6238 A non-empty list specifies the windows `display-buffer' will
6239 consider for splitting. The following entries are supported
6240 where \"frame\" refers to the frame chosen to display the buffer:
6242 largest ...... largest window
6243 lru .......... least recently used window
6244 selected ..... frame's selected window
6245 root ......... frame's root window
6247 The default value t stands for the list `(largest lru)'. This
6248 means that `display-buffer' will first try to split the largest
6249 window and, if that fails, the least recently used window."
6250 :type '(choice
6251 (const :tag "Disallow" nil)
6252 (const :tag "Allow" t)
6253 (repeat :tag "Preferences"
6254 (choice
6255 (const :tag "Largest" largest)
6256 (const :tag "Least Recently Used" lru)
6257 (const :tag "Selected" selected)
6258 (const :tag "Frame Root Window" root))))
6259 :group 'display-buffer)
6260 (make-obsolete-variable
6261 'pop-up-windows
6262 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6263 "24.1")
6265 (defcustom split-window-preferred-function 'split-window-sensibly
6266 "Function called by `display-buffer' to split a window.
6267 This function is called with a window as single argument and is
6268 supposed to split that window and return the new window. If the
6269 window can (or shall) not be split, it is supposed to return nil.
6271 The default is to call the function `split-window-sensibly' which
6272 tries to split the window in a way which seems most suitable.
6273 You can customize the options `split-height-threshold' and/or
6274 `split-width-threshold' in order to have `split-window-sensibly'
6275 prefer either vertical or horizontal splitting.
6277 If you set this to any other function, bear in mind that
6278 `display-buffer' may call that function repeatedly; the option
6279 `pop-up-windows' controls which windows may become the argument
6280 of this function.
6282 The window selected at the time `display-buffer' was invoked is
6283 still selected when this function is called. Hence you can
6284 compare the window argument with the value of `selected-window'
6285 if you intend to split the selected window instead or if you do
6286 not want to split the selected window."
6287 :type 'function
6288 :version "23.1"
6289 :group 'display-buffer)
6290 (make-obsolete-variable
6291 'split-window-preferred-function
6292 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6293 "24.1")
6295 (defcustom split-height-threshold 80
6296 "Minimum height for splitting a window to display a buffer.
6297 If this is an integer, `display-buffer' can split a window
6298 vertically only if it has at least this many lines. If this is
6299 nil, `display-buffer' does not split windows vertically. If a
6300 window is the only window on its frame, `display-buffer' may
6301 split it vertically disregarding the value of this variable."
6302 :type '(choice (const nil) (integer :tag "lines"))
6303 :version "23.1"
6304 :group 'display-buffer)
6305 (make-obsolete-variable
6306 'split-height-threshold
6307 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6308 "24.1")
6310 (defcustom split-width-threshold 160
6311 "Minimum width for splitting a window to display a buffer.
6312 If this is an integer, `display-buffer' can split a window
6313 horizontally only if it has at least this many columns. If this
6314 is nil, `display-buffer' cannot split windows horizontally."
6315 :type '(choice (const nil) (integer :tag "columns"))
6316 :version "23.1"
6317 :group 'display-buffer)
6318 (make-obsolete-variable
6319 'split-width-threshold
6320 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6321 "24.1")
6323 (defcustom even-window-heights t
6324 "If non-nil `display-buffer' will try to even window heights.
6325 Otherwise `display-buffer' will leave the window configuration
6326 alone. Heights are evened only when `display-buffer' reuses a
6327 window that appears above or below the selected window."
6328 :type 'boolean
6329 :group 'display-buffer)
6330 (make-obsolete-variable
6331 'even-window-heights
6332 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6333 "24.1")
6335 (defvar display-buffer-mark-dedicated nil
6336 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6337 The actual non-nil value of this variable will be copied to the
6338 `window-dedicated-p' flag.")
6339 (make-obsolete-variable
6340 'display-buffer-mark-dedicated
6341 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead."
6342 "24.1")
6344 ;; Functions for converting Emacs 23 buffer display options to buffer
6345 ;; display specifiers.
6346 (defun display-buffer-alist-add (identifiers specifiers &optional no-custom)
6347 "Helper function for `display-buffer-alist-set'."
6348 (unless identifiers
6349 (setq identifiers '((regexp . ".*"))))
6350 (unless (atom specifiers)
6351 (setq specifiers (delq nil specifiers)))
6353 (if no-custom
6354 (setq display-buffer-alist
6355 (cons (cons identifiers specifiers) display-buffer-alist))
6356 (customize-set-variable
6357 'display-buffer-alist
6358 (cons (cons identifiers specifiers) display-buffer-alist))))
6360 (defun display-buffer-alist-set (&optional no-custom add)
6361 "Set `display-buffer-alist' from Emacs 23 buffer display options.
6362 Optional argument NO-CUSTOM nil means use `customize-set-variable'
6363 to set the value of `display-buffer-alist'. NO-CUSTOM non-nil
6364 means to use `setq' instead.
6366 Optional argument ADD nil means to replace the actual value of
6367 `display-buffer-alist' with the value calculated here. ADD
6368 non-nil means prepend the value calculated here to the current
6369 value of `display-buffer-alist'."
6370 (unless add
6371 (if no-custom
6372 (setq display-buffer-alist nil)
6373 (customize-set-variable 'display-buffer-alist nil)))
6375 ;; Disable warnings, there are too many obsolete options here.
6376 (with-no-warnings
6377 ;; `pop-up-windows'
6378 (display-buffer-alist-add
6380 (let ((fun (unless (eq split-window-preferred-function
6381 'split-window-sensibly)
6382 ;; `split-window-sensibly' has been merged into the
6383 ;; `display-buffer-split-window' code as `nil'.
6384 split-window-preferred-function))
6385 (min-height
6386 (if (numberp split-height-threshold)
6387 (/ split-height-threshold 2)
6388 ;; Undocumented hack.
6389 1.0))
6390 (min-width
6391 (if (numberp split-width-threshold)
6392 (/ split-width-threshold 2)
6393 ;; Undocumented hack.
6394 1.0)))
6395 (list
6396 'pop-up-window
6397 (when pop-up-windows
6398 (list
6399 'pop-up-window
6400 (cons 'largest fun)
6401 (cons 'lru fun)))
6402 (cons 'pop-up-window-min-height min-height)
6403 (cons 'pop-up-window-min-width min-width)))
6404 no-custom)
6406 ;; `pop-up-frames'
6407 (display-buffer-alist-add
6409 (list
6410 'pop-up-frame
6411 (when pop-up-frames (list 'pop-up-frame pop-up-frames))
6412 (when pop-up-frame-function
6413 (cons 'pop-up-frame-function pop-up-frame-function))
6414 (when pop-up-frame-alist
6415 (cons 'pop-up-frame-alist pop-up-frame-alist)))
6416 no-custom)
6418 ;; `special-display-buffer-names'
6419 (dolist (entry special-display-buffer-names)
6420 (cond
6421 ((stringp entry)
6422 ;; Plain string.
6423 (display-buffer-alist-add
6424 `((name . ,entry))
6425 (list
6426 'fun-with-args
6427 (list 'fun-with-args special-display-function
6428 special-display-frame-alist))
6429 no-custom))
6430 ((consp entry)
6431 (let ((name (car entry))
6432 (rest (cdr entry)))
6433 (cond
6434 ((functionp (car rest))
6435 ;; A function.
6436 (display-buffer-alist-add
6437 `((name . ,name))
6438 (list
6439 'fun-with-args
6440 ;; Weary.
6441 (list 'fun-with-args (car rest) (cadr rest)))
6442 no-custom))
6443 ((listp rest)
6444 ;; A list of parameters.
6445 (cond
6446 ((assq 'same-window rest)
6447 (display-buffer-alist-add
6448 `((name . ,name))
6449 (list 'reuse-window
6450 (list 'reuse-window 'same)
6451 (list 'reuse-window-dedicated 'weak))
6452 no-custom))
6453 ((assq 'same-frame rest)
6454 (display-buffer-alist-add
6455 `((name . ,name)) (list 'same-frame) no-custom))
6457 (display-buffer-alist-add
6458 `((name . ,name))
6459 (list
6460 'fun-with-args
6461 (list 'fun-with-args special-display-function
6462 special-display-frame-alist))
6463 no-custom)))))))))
6465 ;; `special-display-regexps'
6466 (dolist (entry special-display-regexps)
6467 (cond
6468 ((stringp entry)
6469 ;; Plain string.
6470 (display-buffer-alist-add
6471 `((regexp . ,entry))
6472 (list
6473 'fun-with-args
6474 (list 'fun-with-args special-display-function
6475 special-display-frame-alist))
6476 no-custom))
6477 ((consp entry)
6478 (let ((name (car entry))
6479 (rest (cdr entry)))
6480 (cond
6481 ((functionp (car rest))
6482 ;; A function.
6483 (display-buffer-alist-add
6484 `((name . ,name))
6485 (list
6486 'fun-with-args
6487 ;; Weary.
6488 (list 'fun-with-args (car rest) (cadr rest)))
6489 no-custom))
6490 ((listp rest)
6491 ;; A list of parameters.
6492 (cond
6493 ((assq 'same-window rest)
6494 (display-buffer-alist-add
6495 `((name . ,name))
6496 (list 'reuse-window
6497 (list 'reuse-window 'same)
6498 (list 'reuse-window-dedicated 'weak))
6499 no-custom))
6500 ((assq 'same-frame rest)
6501 (display-buffer-alist-add
6502 `((name . ,name)) (list 'same-frame) no-custom))
6504 (display-buffer-alist-add
6505 `((name . ,name))
6506 (list
6507 'fun-with-args
6508 (list 'fun-with-args special-display-function
6509 special-display-frame-alist))
6510 no-custom)))))))))
6512 ;; `same-window-regexps'
6513 (dolist (entry same-window-regexps)
6514 (cond
6515 ((stringp entry)
6516 (display-buffer-alist-add
6517 `((regexp . ,entry))
6518 (list 'reuse-window (list 'reuse-window 'same))
6519 no-custom))
6520 ((consp entry)
6521 (display-buffer-alist-add
6522 `((regexp . ,(car entry)))
6523 (list 'reuse-window (list 'reuse-window 'same))
6524 no-custom))))
6526 ;; `same-window-buffer-names'
6527 (dolist (entry same-window-buffer-names)
6528 (cond
6529 ((stringp entry)
6530 (display-buffer-alist-add
6531 `((name . ,entry))
6532 (list 'reuse-window (list 'reuse-window 'same))
6533 no-custom))
6534 ((consp entry)
6535 (display-buffer-alist-add
6536 `((name . ,(car entry)))
6537 (list 'reuse-window (list 'reuse-window 'same))
6538 no-custom))))
6540 ;; `reuse-window'
6541 (display-buffer-alist-add
6543 (list
6544 'reuse-window
6545 (list 'reuse-window nil 'same
6546 ;; "0" (all visible and iconified frames) is hardcoded in
6547 ;; Emacs 23.
6548 (if (or display-buffer-reuse-frames pop-up-frames) 0 nil))
6549 (when even-window-heights
6550 (cons 'reuse-window-even-sizes t)))
6551 no-custom))
6553 display-buffer-alist)
6555 (defun set-window-text-height (window height)
6556 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6557 WINDOW must be a live window. HEIGHT doesn't include the mode
6558 line or header line, if any, or any partial-height lines in the
6559 text display area.
6561 Note that the current implementation of this function cannot
6562 always set the height exactly, but attempts to be conservative,
6563 by allocating more lines than are actually needed in the case
6564 where some error may be present."
6565 (setq window (normalize-live-window window))
6566 (let ((delta (- height (window-text-height window))))
6567 (unless (zerop delta)
6568 ;; Setting window-min-height to a value like 1 can lead to very
6569 ;; bizarre displays because it also allows Emacs to make *other*
6570 ;; windows 1-line tall, which means that there's no more space for
6571 ;; the modeline.
6572 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
6573 (resize-window window delta)))))
6575 (defun enlarge-window-horizontally (delta)
6576 "Make selected window DELTA wider.
6577 Interactively, if no argument is given, make selected window one
6578 column wider."
6579 (interactive "p")
6580 (enlarge-window delta t))
6582 (defun shrink-window-horizontally (delta)
6583 "Make selected window DELTA narrower.
6584 Interactively, if no argument is given, make selected window one
6585 column narrower."
6586 (interactive "p")
6587 (shrink-window delta t))
6589 (defun count-screen-lines (&optional beg end count-final-newline window)
6590 "Return the number of screen lines in the region.
6591 The number of screen lines may be different from the number of actual lines,
6592 due to line breaking, display table, etc.
6594 Optional arguments BEG and END default to `point-min' and `point-max'
6595 respectively.
6597 If region ends with a newline, ignore it unless optional third argument
6598 COUNT-FINAL-NEWLINE is non-nil.
6600 The optional fourth argument WINDOW specifies the window used for obtaining
6601 parameters such as width, horizontal scrolling, and so on. The default is
6602 to use the selected window's parameters.
6604 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6605 regardless of which buffer is displayed in WINDOW. This makes possible to use
6606 `count-screen-lines' in any buffer, whether or not it is currently displayed
6607 in some window."
6608 (unless beg
6609 (setq beg (point-min)))
6610 (unless end
6611 (setq end (point-max)))
6612 (if (= beg end)
6614 (save-excursion
6615 (save-restriction
6616 (widen)
6617 (narrow-to-region (min beg end)
6618 (if (and (not count-final-newline)
6619 (= ?\n (char-before (max beg end))))
6620 (1- (max beg end))
6621 (max beg end)))
6622 (goto-char (point-min))
6623 (1+ (vertical-motion (buffer-size) window))))))
6625 (defun window-buffer-height (window)
6626 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
6627 (with-current-buffer (window-buffer window)
6628 (max 1
6629 (count-screen-lines (point-min) (point-max)
6630 ;; If buffer ends with a newline, ignore it when
6631 ;; counting height unless point is after it.
6632 (eobp)
6633 window))))
6635 ;;; Resizing buffers to fit their contents exactly.
6636 (defun fit-window-to-buffer (&optional window max-height min-height override)
6637 "Adjust height of WINDOW to display its buffer's contents exactly.
6638 WINDOW can be any live window and defaults to the selected one.
6640 Optional argument MAX-HEIGHT specifies the maximum height of
6641 WINDOW and defaults to the height of WINDOW's frame. Optional
6642 argument MIN-HEIGHT specifies the minimum height of WINDOW and
6643 defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
6644 are specified in lines and include the mode line and header line,
6645 if any.
6647 Optional argument OVERRIDE non-nil means override restrictions
6648 imposed by `window-min-height' and `window-min-width' on the size
6649 of WINDOW.
6651 Return the number of lines by which WINDOW was enlarged or
6652 shrunk. If an error occurs during resizing, return nil but don't
6653 signal an error.
6655 Note that even if this function makes WINDOW large enough to show
6656 _all_ lines of its buffer you might not see the first lines when
6657 WINDOW was scrolled."
6658 (interactive)
6659 ;; Do all the work in WINDOW and its buffer and restore the selected
6660 ;; window and the current buffer when we're done.
6661 (setq window (normalize-live-window window))
6662 ;; Can't resize a full height or fixed-size window.
6663 (unless (or (window-size-fixed-p window)
6664 (window-full-height-p window))
6665 ;; `with-selected-window' should orderly restore the current buffer.
6666 (with-selected-window window
6667 ;; We are in WINDOW's buffer now.
6668 (let* ( ;; Adjust MIN-HEIGHT.
6669 (min-height
6670 (if override
6671 (window-min-size window nil window)
6672 (max (or min-height window-min-height)
6673 window-safe-min-height)))
6674 (max-window-height
6675 (window-total-size (frame-root-window window)))
6676 ;; Adjust MAX-HEIGHT.
6677 (max-height
6678 (if (or override (not max-height))
6679 max-window-height
6680 (min max-height max-window-height)))
6681 ;; Make `desired-height' the height necessary to show
6682 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
6683 ;; and MAX-HEIGHT.
6684 (desired-height
6685 (max
6686 (min
6687 (+ (count-screen-lines)
6688 ;; For non-minibuffers count the mode line, if any.
6689 (if (and (not (window-minibuffer-p window))
6690 mode-line-format)
6693 ;; Count the header line, if any.
6694 (if header-line-format 1 0))
6695 max-height)
6696 min-height))
6697 (desired-delta
6698 (- desired-height (window-total-size window)))
6699 (delta
6700 (if (> desired-delta 0)
6701 (min desired-delta
6702 (window-max-delta window nil window))
6703 (max desired-delta
6704 (- (window-min-delta window nil window))))))
6705 ;; This `condition-case' shouldn't be necessary, but who knows?
6706 (condition-case nil
6707 (if (zerop delta)
6708 ;; Return zero if DELTA became zero in the proces.
6710 ;; Don't try to redisplay with the cursor at the end on its
6711 ;; own line--that would force a scroll and spoil things.
6712 (when (and (eobp) (bolp) (not (bobp)))
6713 ;; It's silly to put `point' at the end of the previous
6714 ;; line and so maybe force horizontal scrolling.
6715 (set-window-point window (line-beginning-position 0)))
6716 ;; Call `resize-window' with OVERRIDE argument equal WINDOW.
6717 (resize-window window delta nil window)
6718 ;; Check if the last line is surely fully visible. If
6719 ;; not, enlarge the window.
6720 (let ((end (save-excursion
6721 (goto-char (point-max))
6722 (when (and (bolp) (not (bobp)))
6723 ;; Don't include final newline.
6724 (backward-char 1))
6725 (when truncate-lines
6726 ;; If line-wrapping is turned off, test the
6727 ;; beginning of the last line for
6728 ;; visibility instead of the end, as the
6729 ;; end of the line could be invisible by
6730 ;; virtue of extending past the edge of the
6731 ;; window.
6732 (forward-line 0))
6733 (point))))
6734 (set-window-vscroll window 0)
6735 ;; This loop might in some rare pathological cases raise
6736 ;; an error - another reason for the `condition-case'.
6737 (while (and (< desired-height max-height)
6738 (= desired-height (window-total-size))
6739 (not (pos-visible-in-window-p end)))
6740 (resize-window window 1 nil window)
6741 (setq desired-height (1+ desired-height)))))
6742 (error (setq delta nil)))
6743 delta))))
6745 (defun shrink-window-if-larger-than-buffer (&optional window)
6746 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6747 More precisely, shrink WINDOW vertically to be as small as
6748 possible, while still showing the full contents of its buffer.
6749 WINDOW defaults to the selected window.
6751 Do not shrink WINDOW to less than `window-min-height' lines. Do
6752 nothing if the buffer contains more lines than the present window
6753 height, or if some of the window's contents are scrolled out of
6754 view, or if shrinking this window would also shrink another
6755 window, or if the window is the only window of its frame.
6757 Return non-nil if the window was shrunk, nil otherwise."
6758 (interactive)
6759 (setq window (normalize-live-window window))
6760 ;; Make sure that WINDOW is vertically combined and `point-min' is
6761 ;; visible (for whatever reason that's needed). The remaining issues
6762 ;; should be taken care of by `fit-window-to-buffer'.
6763 (when (and (window-iso-combined-p window)
6764 (pos-visible-in-window-p (point-min) window))
6765 (fit-window-to-buffer window (window-total-size window))))
6767 (defun kill-buffer-and-window ()
6768 "Kill the current buffer and delete the selected window."
6769 (interactive)
6770 (let ((window-to-delete (selected-window))
6771 (buffer-to-kill (current-buffer))
6772 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6773 (unwind-protect
6774 (progn
6775 (add-hook 'kill-buffer-hook delete-window-hook t t)
6776 (if (kill-buffer (current-buffer))
6777 ;; If `delete-window' failed before, we rerun it to regenerate
6778 ;; the error so it can be seen in the echo area.
6779 (when (eq (selected-window) window-to-delete)
6780 (delete-window))))
6781 ;; If the buffer is not dead for some reason (probably because
6782 ;; of a `quit' signal), remove the hook again.
6783 (ignore-errors
6784 (with-current-buffer buffer-to-kill
6785 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
6787 (defun quit-window (&optional kill window)
6788 "Quit WINDOW and bury its buffer.
6789 With a prefix argument, kill the buffer instead. WINDOW defaults
6790 to the selected window.
6792 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
6793 it and, if it's alone on its frame, its frame too. Otherwise, or
6794 if deleting WINDOW fails in any of the preceding cases, display
6795 another buffer in WINDOW using `switch-to-buffer'.
6797 Optional argument KILL non-nil means kill WINDOW's buffer.
6798 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
6799 (interactive "P")
6800 (let ((buffer (window-buffer window)))
6801 (if (or window
6802 (window-minibuffer-p window)
6803 (window-dedicated-p window))
6804 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
6805 ;; try to delete it.
6806 (let* ((window (or window (selected-window)))
6807 (frame (window-frame window)))
6808 (if (frame-root-window-p window)
6809 ;; WINDOW is alone on its frame.
6810 (delete-frame frame)
6811 ;; There are other windows on its frame, delete WINDOW.
6812 (delete-window window)))
6813 ;; Otherwise, switch to another buffer in the selected window.
6814 (switch-to-buffer nil))
6816 ;; Deal with the buffer.
6817 (if kill
6818 (kill-buffer buffer)
6819 (bury-buffer buffer))))
6821 (defvar recenter-last-op nil
6822 "Indicates the last recenter operation performed.
6823 Possible values: `top', `middle', `bottom', integer or float numbers.")
6825 (defcustom recenter-positions '(middle top bottom)
6826 "Cycling order for `recenter-top-bottom'.
6827 A list of elements with possible values `top', `middle', `bottom',
6828 integer or float numbers that define the cycling order for
6829 the command `recenter-top-bottom'.
6831 Top and bottom destinations are `scroll-margin' lines the from true
6832 window top and bottom. Middle redraws the frame and centers point
6833 vertically within the window. Integer number moves current line to
6834 the specified absolute window-line. Float number between 0.0 and 1.0
6835 means the percentage of the screen space from the top. The default
6836 cycling order is middle -> top -> bottom."
6837 :type '(repeat (choice
6838 (const :tag "Top" top)
6839 (const :tag "Middle" middle)
6840 (const :tag "Bottom" bottom)
6841 (integer :tag "Line number")
6842 (float :tag "Percentage")))
6843 :version "23.2"
6844 :group 'windows)
6846 (defun recenter-top-bottom (&optional arg)
6847 "Move current buffer line to the specified window line.
6848 With no prefix argument, successive calls place point according
6849 to the cycling order defined by `recenter-positions'.
6851 A prefix argument is handled like `recenter':
6852 With numeric prefix ARG, move current line to window-line ARG.
6853 With plain `C-u', move current line to window center."
6854 (interactive "P")
6855 (cond
6856 (arg (recenter arg)) ; Always respect ARG.
6858 (setq recenter-last-op
6859 (if (eq this-command last-command)
6860 (car (or (cdr (member recenter-last-op recenter-positions))
6861 recenter-positions))
6862 (car recenter-positions)))
6863 (let ((this-scroll-margin
6864 (min (max 0 scroll-margin)
6865 (truncate (/ (window-body-height) 4.0)))))
6866 (cond ((eq recenter-last-op 'middle)
6867 (recenter))
6868 ((eq recenter-last-op 'top)
6869 (recenter this-scroll-margin))
6870 ((eq recenter-last-op 'bottom)
6871 (recenter (- -1 this-scroll-margin)))
6872 ((integerp recenter-last-op)
6873 (recenter recenter-last-op))
6874 ((floatp recenter-last-op)
6875 (recenter (round (* recenter-last-op (window-height))))))))))
6877 (define-key global-map [?\C-l] 'recenter-top-bottom)
6879 (defun move-to-window-line-top-bottom (&optional arg)
6880 "Position point relative to window.
6882 With a prefix argument ARG, acts like `move-to-window-line'.
6884 With no argument, positions point at center of window.
6885 Successive calls position point at positions defined
6886 by `recenter-positions'."
6887 (interactive "P")
6888 (cond
6889 (arg (move-to-window-line arg)) ; Always respect ARG.
6891 (setq recenter-last-op
6892 (if (eq this-command last-command)
6893 (car (or (cdr (member recenter-last-op recenter-positions))
6894 recenter-positions))
6895 (car recenter-positions)))
6896 (let ((this-scroll-margin
6897 (min (max 0 scroll-margin)
6898 (truncate (/ (window-body-height) 4.0)))))
6899 (cond ((eq recenter-last-op 'middle)
6900 (call-interactively 'move-to-window-line))
6901 ((eq recenter-last-op 'top)
6902 (move-to-window-line this-scroll-margin))
6903 ((eq recenter-last-op 'bottom)
6904 (move-to-window-line (- -1 this-scroll-margin)))
6905 ((integerp recenter-last-op)
6906 (move-to-window-line recenter-last-op))
6907 ((floatp recenter-last-op)
6908 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
6910 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
6912 ;;; Scrolling commands.
6914 ;;; Scrolling commands which does not signal errors at top/bottom
6915 ;;; of buffer at first key-press (instead moves to top/bottom
6916 ;;; of buffer).
6918 (defcustom scroll-error-top-bottom nil
6919 "Move point to top/bottom of buffer before signalling a scrolling error.
6920 A value of nil means just signal an error if no more scrolling possible.
6921 A value of t means point moves to the beginning or the end of the buffer
6922 \(depending on scrolling direction) when no more scrolling possible.
6923 When point is already on that position, then signal an error."
6924 :type 'boolean
6925 :group 'scrolling
6926 :version "24.1")
6928 (defun scroll-up-command (&optional arg)
6929 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6930 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6931 scroll window further, move cursor to the bottom line.
6932 When point is already on that position, then signal an error.
6933 A near full screen is `next-screen-context-lines' less than a full screen.
6934 Negative ARG means scroll downward.
6935 If ARG is the atom `-', scroll downward by nearly full screen."
6936 (interactive "^P")
6937 (cond
6938 ((null scroll-error-top-bottom)
6939 (scroll-up arg))
6940 ((eq arg '-)
6941 (scroll-down-command nil))
6942 ((< (prefix-numeric-value arg) 0)
6943 (scroll-down-command (- (prefix-numeric-value arg))))
6944 ((eobp)
6945 (scroll-up arg)) ; signal error
6947 (condition-case nil
6948 (scroll-up arg)
6949 (end-of-buffer
6950 (if arg
6951 ;; When scrolling by ARG lines can't be done,
6952 ;; move by ARG lines instead.
6953 (forward-line arg)
6954 ;; When ARG is nil for full-screen scrolling,
6955 ;; move to the bottom of the buffer.
6956 (goto-char (point-max))))))))
6958 (put 'scroll-up-command 'scroll-command t)
6960 (defun scroll-down-command (&optional arg)
6961 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6962 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6963 scroll window further, move cursor to the top line.
6964 When point is already on that position, then signal an error.
6965 A near full screen is `next-screen-context-lines' less than a full screen.
6966 Negative ARG means scroll upward.
6967 If ARG is the atom `-', scroll upward by nearly full screen."
6968 (interactive "^P")
6969 (cond
6970 ((null scroll-error-top-bottom)
6971 (scroll-down arg))
6972 ((eq arg '-)
6973 (scroll-up-command nil))
6974 ((< (prefix-numeric-value arg) 0)
6975 (scroll-up-command (- (prefix-numeric-value arg))))
6976 ((bobp)
6977 (scroll-down arg)) ; signal error
6979 (condition-case nil
6980 (scroll-down arg)
6981 (beginning-of-buffer
6982 (if arg
6983 ;; When scrolling by ARG lines can't be done,
6984 ;; move by ARG lines instead.
6985 (forward-line (- arg))
6986 ;; When ARG is nil for full-screen scrolling,
6987 ;; move to the top of the buffer.
6988 (goto-char (point-min))))))))
6990 (put 'scroll-down-command 'scroll-command t)
6992 ;;; Scrolling commands which scroll a line instead of full screen.
6994 (defun scroll-up-line (&optional arg)
6995 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6996 If ARG is omitted or nil, scroll upward by one line.
6997 This is different from `scroll-up-command' that scrolls a full screen."
6998 (interactive "p")
6999 (scroll-up (or arg 1)))
7001 (put 'scroll-up-line 'scroll-command t)
7003 (defun scroll-down-line (&optional arg)
7004 "Scroll text of selected window down ARG lines; or one line if no ARG.
7005 If ARG is omitted or nil, scroll down by one line.
7006 This is different from `scroll-down-command' that scrolls a full screen."
7007 (interactive "p")
7008 (scroll-down (or arg 1)))
7010 (put 'scroll-down-line 'scroll-command t)
7013 (defun scroll-other-window-down (lines)
7014 "Scroll the \"other window\" down.
7015 For more details, see the documentation for `scroll-other-window'."
7016 (interactive "P")
7017 (scroll-other-window
7018 ;; Just invert the argument's meaning.
7019 ;; We can do that without knowing which window it will be.
7020 (if (eq lines '-) nil
7021 (if (null lines) '-
7022 (- (prefix-numeric-value lines))))))
7024 (defun beginning-of-buffer-other-window (arg)
7025 "Move point to the beginning of the buffer in the other window.
7026 Leave mark at previous position.
7027 With arg N, put point N/10 of the way from the true beginning."
7028 (interactive "P")
7029 (let ((orig-window (selected-window))
7030 (window (other-window-for-scrolling)))
7031 ;; We use unwind-protect rather than save-window-excursion
7032 ;; because the latter would preserve the things we want to change.
7033 (unwind-protect
7034 (progn
7035 (select-window window)
7036 ;; Set point and mark in that window's buffer.
7037 (with-no-warnings
7038 (beginning-of-buffer arg))
7039 ;; Set point accordingly.
7040 (recenter '(t)))
7041 (select-window orig-window))))
7043 (defun end-of-buffer-other-window (arg)
7044 "Move point to the end of the buffer in the other window.
7045 Leave mark at previous position.
7046 With arg N, put point N/10 of the way from the true end."
7047 (interactive "P")
7048 ;; See beginning-of-buffer-other-window for comments.
7049 (let ((orig-window (selected-window))
7050 (window (other-window-for-scrolling)))
7051 (unwind-protect
7052 (progn
7053 (select-window window)
7054 (with-no-warnings
7055 (end-of-buffer arg))
7056 (recenter '(t)))
7057 (select-window orig-window))))
7059 (defvar mouse-autoselect-window-timer nil
7060 "Timer used by delayed window autoselection.")
7062 (defvar mouse-autoselect-window-position nil
7063 "Last mouse position recorded by delayed window autoselection.")
7065 (defvar mouse-autoselect-window-window nil
7066 "Last window recorded by delayed window autoselection.")
7068 (defvar mouse-autoselect-window-state nil
7069 "When non-nil, special state of delayed window autoselection.
7070 Possible values are `suspend' \(suspend autoselection after a menu or
7071 scrollbar interaction\) and `select' \(the next invocation of
7072 'handle-select-window' shall select the window immediately\).")
7074 (defun mouse-autoselect-window-cancel (&optional force)
7075 "Cancel delayed window autoselection.
7076 Optional argument FORCE means cancel unconditionally."
7077 (unless (and (not force)
7078 ;; Don't cancel for select-window or select-frame events
7079 ;; or when the user drags a scroll bar.
7080 (or (memq this-command
7081 '(handle-select-window handle-switch-frame))
7082 (and (eq this-command 'scroll-bar-toolkit-scroll)
7083 (memq (nth 4 (event-end last-input-event))
7084 '(handle end-scroll)))))
7085 (setq mouse-autoselect-window-state nil)
7086 (when (timerp mouse-autoselect-window-timer)
7087 (cancel-timer mouse-autoselect-window-timer))
7088 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
7090 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
7091 "Start delayed window autoselection.
7092 MOUSE-POSITION is the last position where the mouse was seen as returned
7093 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7094 window where the mouse was seen. Optional argument SUSPEND non-nil
7095 means suspend autoselection."
7096 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7097 (setq mouse-autoselect-window-position mouse-position)
7098 (when window (setq mouse-autoselect-window-window window))
7099 (setq mouse-autoselect-window-state (when suspend 'suspend))
7100 ;; Install timer which runs `mouse-autoselect-window-select' after
7101 ;; `mouse-autoselect-window' seconds.
7102 (setq mouse-autoselect-window-timer
7103 (run-at-time
7104 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
7106 (defun mouse-autoselect-window-select ()
7107 "Select window with delayed window autoselection.
7108 If the mouse position has stabilized in a non-selected window, select
7109 that window. The minibuffer window is selected only if the minibuffer is
7110 active. This function is run by `mouse-autoselect-window-timer'."
7111 (ignore-errors
7112 (let* ((mouse-position (mouse-position))
7113 (window
7114 (ignore-errors
7115 (window-at (cadr mouse-position) (cddr mouse-position)
7116 (car mouse-position)))))
7117 (cond
7118 ((or (menu-or-popup-active-p)
7119 (and window
7120 (not (coordinates-in-window-p (cdr mouse-position) window))))
7121 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
7122 ;; of WINDOW, temporarily suspend delayed autoselection.
7123 (mouse-autoselect-window-start mouse-position nil t))
7124 ((eq mouse-autoselect-window-state 'suspend)
7125 ;; Delayed autoselection was temporarily suspended, reenable it.
7126 (mouse-autoselect-window-start mouse-position))
7127 ((and window (not (eq window (selected-window)))
7128 (or (not (numberp mouse-autoselect-window))
7129 (and (> mouse-autoselect-window 0)
7130 ;; If `mouse-autoselect-window' is positive, select
7131 ;; window if the window is the same as before.
7132 (eq window mouse-autoselect-window-window))
7133 ;; Otherwise select window if the mouse is at the same
7134 ;; position as before. Observe that the first test after
7135 ;; starting autoselection usually fails since the value of
7136 ;; `mouse-autoselect-window-position' recorded there is the
7137 ;; position where the mouse has entered the new window and
7138 ;; not necessarily where the mouse has stopped moving.
7139 (equal mouse-position mouse-autoselect-window-position))
7140 ;; The minibuffer is a candidate window if it's active.
7141 (or (not (window-minibuffer-p window))
7142 (eq window (active-minibuffer-window))))
7143 ;; Mouse position has stabilized in non-selected window: Cancel
7144 ;; delayed autoselection and try to select that window.
7145 (mouse-autoselect-window-cancel t)
7146 ;; Select window where mouse appears unless the selected window is the
7147 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7148 ;; and post-command hooks and trigger idle timers. To avoid delaying
7149 ;; autoselection again, set `mouse-autoselect-window-state'."
7150 (unless (window-minibuffer-p (selected-window))
7151 (setq mouse-autoselect-window-state 'select)
7152 (setq unread-command-events
7153 (cons (list 'select-window (list window))
7154 unread-command-events))))
7155 ((or (and window (eq window (selected-window)))
7156 (not (numberp mouse-autoselect-window))
7157 (equal mouse-position mouse-autoselect-window-position))
7158 ;; Mouse position has either stabilized in the selected window or at
7159 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7160 (mouse-autoselect-window-cancel t))
7162 ;; Mouse position has not stabilized yet, resume delayed
7163 ;; autoselection.
7164 (mouse-autoselect-window-start mouse-position window))))))
7166 (defun handle-select-window (event)
7167 "Handle select-window events."
7168 (interactive "e")
7169 (let ((window (posn-window (event-start event))))
7170 (unless (or (not (window-live-p window))
7171 ;; Don't switch if we're currently in the minibuffer.
7172 ;; This tries to work around problems where the
7173 ;; minibuffer gets unselected unexpectedly, and where
7174 ;; you then have to move your mouse all the way down to
7175 ;; the minibuffer to select it.
7176 (window-minibuffer-p (selected-window))
7177 ;; Don't switch to minibuffer window unless it's active.
7178 (and (window-minibuffer-p window)
7179 (not (minibuffer-window-active-p window)))
7180 ;; Don't switch when autoselection shall be delayed.
7181 (and (numberp mouse-autoselect-window)
7182 (not (zerop mouse-autoselect-window))
7183 (not (eq mouse-autoselect-window-state 'select))
7184 (progn
7185 ;; Cancel any delayed autoselection.
7186 (mouse-autoselect-window-cancel t)
7187 ;; Start delayed autoselection from current mouse
7188 ;; position and window.
7189 (mouse-autoselect-window-start (mouse-position) window)
7190 ;; Executing a command cancels delayed autoselection.
7191 (add-hook
7192 'pre-command-hook 'mouse-autoselect-window-cancel))))
7193 (when mouse-autoselect-window
7194 ;; Reset state of delayed autoselection.
7195 (setq mouse-autoselect-window-state nil)
7196 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7197 (run-hooks 'mouse-leave-buffer-hook))
7198 (select-window window))))
7200 (defun truncated-partial-width-window-p (&optional window)
7201 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7202 WINDOW defaults to the selected window.
7203 Return nil if WINDOW is not a partial-width window
7204 (regardless of the value of `truncate-lines').
7205 Otherwise, consult the value of `truncate-partial-width-windows'
7206 for the buffer shown in WINDOW."
7207 (unless window
7208 (setq window (selected-window)))
7209 (unless (window-full-width-p window)
7210 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7211 (window-buffer window))))
7212 (if (integerp t-p-w-w)
7213 (< (window-width window) t-p-w-w)
7214 t-p-w-w))))
7216 (define-key ctl-x-map "0" 'delete-window)
7217 (define-key ctl-x-map "1" 'delete-other-windows)
7218 (define-key ctl-x-map "2" 'split-window-above-each-other)
7219 (define-key ctl-x-map "3" 'split-window-side-by-side)
7220 (define-key ctl-x-map "9" 'maximize-window)
7221 (define-key ctl-x-map "o" 'other-window)
7222 (define-key ctl-x-map "^" 'enlarge-window)
7223 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
7224 (define-key ctl-x-map "{" 'shrink-window-horizontally)
7225 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7226 (define-key ctl-x-map "+" 'balance-windows)
7227 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7229 ;;; window.el ends here