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