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