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