Doc changes related to displaying buffers and quitting windows.
[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 before in the window specified as
3095 argument to those functions. If this variable is nil,
3096 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3097 avoid switching to a buffer that is already visible in another
3098 window on the same frame."
3099 :type 'boolean
3100 :version "24.1"
3101 :group 'windows)
3102
3103 (defun switch-to-prev-buffer (&optional window bury-or-kill)
3104 "In WINDOW switch to previous buffer.
3105 WINDOW must be a live window and defaults to the selected one.
3106 Return the buffer switched to, nil if no suitable buffer could be
3107 found.
3108
3109 Optional argument BURY-OR-KILL non-nil means the buffer currently
3110 shown in WINDOW is about to be buried or killed and consequently
3111 shall not be switched to in future invocations of this command.
3112
3113 As a special case, if BURY-OR-KILL equals `append', this means to
3114 move the buffer to the end of WINDOW's previous buffers list so a
3115 future invocation of `switch-to-prev-buffer' less likely switches
3116 to it."
3117 (interactive)
3118 (let* ((window (window-normalize-window window t))
3119 (frame (window-frame window))
3120 (old-buffer (window-buffer window))
3121 ;; Save this since it's destroyed by `set-window-buffer'.
3122 (next-buffers (window-next-buffers window))
3123 (pred (frame-parameter frame 'buffer-predicate))
3124 entry new-buffer killed-buffers visible)
3125 (when (window-minibuffer-p window)
3126 ;; Don't switch in minibuffer window.
3127 (unless (setq window (minibuffer-selected-window))
3128 (error "Window %s is a minibuffer window" window)))
3129
3130 (when (window-dedicated-p window)
3131 ;; Don't switch in dedicated window.
3132 (error "Window %s is dedicated to buffer %s" window old-buffer))
3133
3134 (catch 'found
3135 ;; Scan WINDOW's previous buffers first, skipping entries of next
3136 ;; buffers.
3137 (dolist (entry (window-prev-buffers window))
3138 (when (and (setq new-buffer (car entry))
3139 (or (buffer-live-p new-buffer)
3140 (not (setq killed-buffers
3141 (cons new-buffer killed-buffers))))
3142 (not (eq new-buffer old-buffer))
3143 (or (null pred) (funcall pred new-buffer))
3144 ;; When BURY-OR-KILL is nil, avoid switching to a
3145 ;; buffer in WINDOW's next buffers list.
3146 (or bury-or-kill (not (memq new-buffer next-buffers))))
3147 (if (and (not switch-to-visible-buffer)
3148 (get-buffer-window new-buffer frame))
3149 ;; Try to avoid showing a buffer visible in some other
3150 ;; window.
3151 (setq visible new-buffer)
3152 (set-window-buffer-start-and-point
3153 window new-buffer (nth 1 entry) (nth 2 entry))
3154 (throw 'found t))))
3155 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3156 ;; entries of next buffers. Note that when we bury or kill a
3157 ;; buffer we don't reverse the global buffer list to avoid showing
3158 ;; a buried buffer instead. Otherwise, we must reverse the global
3159 ;; buffer list in order to make sure that switching to the
3160 ;; previous/next buffer traverse it in opposite directions.
3161 (dolist (buffer (if bury-or-kill
3162 (buffer-list frame)
3163 (nreverse (buffer-list frame))))
3164 (when (and (buffer-live-p buffer)
3165 (not (eq buffer old-buffer))
3166 (or (null pred) (funcall pred buffer))
3167 (not (eq (aref (buffer-name buffer) 0) ?\s))
3168 (or bury-or-kill (not (memq buffer next-buffers))))
3169 (if (get-buffer-window buffer frame)
3170 ;; Try to avoid showing a buffer visible in some other window.
3171 (unless visible
3172 (setq visible buffer))
3173 (setq new-buffer buffer)
3174 (set-window-buffer-start-and-point window new-buffer)
3175 (throw 'found t))))
3176 (unless bury-or-kill
3177 ;; Scan reverted next buffers last (must not use nreverse
3178 ;; here!).
3179 (dolist (buffer (reverse next-buffers))
3180 ;; Actually, buffer _must_ be live here since otherwise it
3181 ;; would have been caught in the scan of previous buffers.
3182 (when (and (or (buffer-live-p buffer)
3183 (not (setq killed-buffers
3184 (cons buffer killed-buffers))))
3185 (not (eq buffer old-buffer))
3186 (or (null pred) (funcall pred buffer))
3187 (setq entry (assq buffer (window-prev-buffers window))))
3188 (setq new-buffer buffer)
3189 (set-window-buffer-start-and-point
3190 window new-buffer (nth 1 entry) (nth 2 entry))
3191 (throw 'found t))))
3192
3193 ;; Show a buffer visible in another window.
3194 (when visible
3195 (setq new-buffer visible)
3196 (set-window-buffer-start-and-point window new-buffer)))
3197
3198 (if bury-or-kill
3199 (let ((entry (and (eq bury-or-kill 'append)
3200 (assq old-buffer (window-prev-buffers window)))))
3201 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3202 ;; of) next buffers.
3203 (set-window-prev-buffers
3204 window (assq-delete-all old-buffer (window-prev-buffers window)))
3205 (set-window-next-buffers window (delq old-buffer next-buffers))
3206 (when entry
3207 ;; Append old-buffer's entry to list of WINDOW's previous
3208 ;; buffers so it's less likely to get switched to soon but
3209 ;; `display-buffer-in-previous-window' can nevertheless find
3210 ;; it.
3211 (set-window-prev-buffers
3212 window (append (window-prev-buffers window) (list entry)))))
3213 ;; Move `old-buffer' to head of WINDOW's restored list of next
3214 ;; buffers.
3215 (set-window-next-buffers
3216 window (cons old-buffer (delq old-buffer next-buffers))))
3217
3218 ;; Remove killed buffers from WINDOW's previous and next buffers.
3219 (when killed-buffers
3220 (dolist (buffer killed-buffers)
3221 (set-window-prev-buffers
3222 window (assq-delete-all buffer (window-prev-buffers window)))
3223 (set-window-next-buffers
3224 window (delq buffer (window-next-buffers window)))))
3225
3226 ;; Return new-buffer.
3227 new-buffer))
3228
3229 (defun switch-to-next-buffer (&optional window)
3230 "In WINDOW switch to next buffer.
3231 WINDOW must be a live window and defaults to the selected one.
3232 Return the buffer switched to, nil if no suitable buffer could be
3233 found."
3234 (interactive)
3235 (let* ((window (window-normalize-window window t))
3236 (frame (window-frame window))
3237 (old-buffer (window-buffer window))
3238 (next-buffers (window-next-buffers window))
3239 (pred (frame-parameter frame 'buffer-predicate))
3240 new-buffer entry killed-buffers visible)
3241 (when (window-minibuffer-p window)
3242 ;; Don't switch in minibuffer window.
3243 (unless (setq window (minibuffer-selected-window))
3244 (error "Window %s is a minibuffer window" window)))
3245
3246 (when (window-dedicated-p window)
3247 ;; Don't switch in dedicated window.
3248 (error "Window %s is dedicated to buffer %s" window old-buffer))
3249
3250 (catch 'found
3251 ;; Scan WINDOW's next buffers first.
3252 (dolist (buffer next-buffers)
3253 (when (and (or (buffer-live-p buffer)
3254 (not (setq killed-buffers
3255 (cons buffer killed-buffers))))
3256 (not (eq buffer old-buffer))
3257 (or (null pred) (funcall pred buffer))
3258 (setq entry (assq buffer (window-prev-buffers window))))
3259 (setq new-buffer buffer)
3260 (set-window-buffer-start-and-point
3261 window new-buffer (nth 1 entry) (nth 2 entry))
3262 (throw 'found t)))
3263 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3264 ;; buffers entries.
3265 (dolist (buffer (buffer-list frame))
3266 (when (and (buffer-live-p buffer)
3267 (not (eq buffer old-buffer))
3268 (or (null pred) (funcall pred buffer))
3269 (not (eq (aref (buffer-name buffer) 0) ?\s))
3270 (not (assq buffer (window-prev-buffers window))))
3271 (if (get-buffer-window buffer frame)
3272 ;; Try to avoid showing a buffer visible in some other window.
3273 (setq visible buffer)
3274 (setq new-buffer buffer)
3275 (set-window-buffer-start-and-point window new-buffer)
3276 (throw 'found t))))
3277 ;; Scan WINDOW's reverted previous buffers last (must not use
3278 ;; nreverse here!)
3279 (dolist (entry (reverse (window-prev-buffers window)))
3280 (when (and (setq new-buffer (car entry))
3281 (or (buffer-live-p new-buffer)
3282 (not (setq killed-buffers
3283 (cons new-buffer killed-buffers))))
3284 (not (eq new-buffer old-buffer))
3285 (or (null pred) (funcall pred new-buffer)))
3286 (if (and (not switch-to-visible-buffer)
3287 (get-buffer-window new-buffer frame))
3288 ;; Try to avoid showing a buffer visible in some other window.
3289 (unless visible
3290 (setq visible new-buffer))
3291 (set-window-buffer-start-and-point
3292 window new-buffer (nth 1 entry) (nth 2 entry))
3293 (throw 'found t))))
3294
3295 ;; Show a buffer visible in another window.
3296 (when visible
3297 (setq new-buffer visible)
3298 (set-window-buffer-start-and-point window new-buffer)))
3299
3300 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3301 (set-window-next-buffers window (delq new-buffer next-buffers))
3302
3303 ;; Remove killed buffers from WINDOW's previous and next buffers.
3304 (when killed-buffers
3305 (dolist (buffer killed-buffers)
3306 (set-window-prev-buffers
3307 window (assq-delete-all buffer (window-prev-buffers window)))
3308 (set-window-next-buffers
3309 window (delq buffer (window-next-buffers window)))))
3310
3311 ;; Return new-buffer.
3312 new-buffer))
3313
3314 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
3315 "Search LIST for a valid buffer to display in FRAME.
3316 Return nil when all buffers in LIST are undesirable for display,
3317 otherwise return the first suitable buffer in LIST.
3318
3319 Buffers not visible in windows are preferred to visible buffers,
3320 unless VISIBLE-OK is non-nil.
3321 If the optional argument FRAME is nil, it defaults to the selected frame.
3322 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3323 ;; This logic is more or less copied from other-buffer.
3324 (setq frame (or frame (selected-frame)))
3325 (let ((pred (frame-parameter frame 'buffer-predicate))
3326 found buf)
3327 (while (and (not found) list)
3328 (setq buf (car list))
3329 (if (and (not (eq buffer buf))
3330 (buffer-live-p buf)
3331 (or (null pred) (funcall pred buf))
3332 (not (eq (aref (buffer-name buf) 0) ?\s))
3333 (or visible-ok (null (get-buffer-window buf 'visible))))
3334 (setq found buf)
3335 (setq list (cdr list))))
3336 (car list)))
3337
3338 (defun last-buffer (&optional buffer visible-ok frame)
3339 "Return the last buffer in FRAME's buffer list.
3340 If BUFFER is the last buffer, return the preceding buffer
3341 instead. Buffers not visible in windows are preferred to visible
3342 buffers, unless optional argument VISIBLE-OK is non-nil.
3343 Optional third argument FRAME nil or omitted means use the
3344 selected frame's buffer list. If no such buffer exists, return
3345 the buffer `*scratch*', creating it if necessary."
3346 (setq frame (or frame (selected-frame)))
3347 (or (get-next-valid-buffer (nreverse (buffer-list frame))
3348 buffer visible-ok frame)
3349 (get-buffer "*scratch*")
3350 (let ((scratch (get-buffer-create "*scratch*")))
3351 (set-buffer-major-mode scratch)
3352 scratch)))
3353
3354 (defcustom frame-auto-hide-function #'iconify-frame
3355 "Function called to automatically hide frames.
3356 The function is called with one argument - a frame.
3357
3358 Functions affected by this option are those that bury a buffer
3359 shown in a separate frame like `quit-window' and `bury-buffer'."
3360 :type '(choice (const :tag "Iconify" iconify-frame)
3361 (const :tag "Delete" delete-frame)
3362 (const :tag "Do nothing" ignore)
3363 function)
3364 :group 'windows
3365 :group 'frames
3366 :version "24.1")
3367
3368 (defun window--delete (&optional window dedicated-only kill)
3369 "Delete WINDOW if possible.
3370 WINDOW must be a live window and defaults to the selected one.
3371 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3372 only if it's dedicated to its buffer. Optional argument KILL
3373 means the buffer shown in window will be killed. Return non-nil
3374 if WINDOW gets deleted or its frame is auto-hidden."
3375 (setq window (window-normalize-window window t))
3376 (unless (and dedicated-only (not (window-dedicated-p window)))
3377 (let ((deletable (window-deletable-p window)))
3378 (cond
3379 ((eq deletable 'frame)
3380 (let ((frame (window-frame window)))
3381 (cond
3382 (kill
3383 (delete-frame frame))
3384 ((functionp frame-auto-hide-function)
3385 (funcall frame-auto-hide-function frame))))
3386 'frame)
3387 (deletable
3388 (delete-window window)
3389 t)))))
3390
3391 (defun bury-buffer (&optional buffer-or-name)
3392 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3393 There it is the least likely candidate for `other-buffer' to
3394 return; thus, the least likely buffer for \\[switch-to-buffer] to
3395 select by default.
3396
3397 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3398 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3399 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3400 remove the current buffer from the selected window if it is
3401 displayed there."
3402 (interactive)
3403 (let* ((buffer (window-normalize-buffer buffer-or-name)))
3404 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3405 ;; although it's not "here" (call it a feature).
3406 (bury-buffer-internal buffer)
3407 ;; Handle case where `buffer-or-name' is nil and the current buffer
3408 ;; is shown in the selected window.
3409 (cond
3410 ((or buffer-or-name (not (eq buffer (window-buffer)))))
3411 ((window--delete nil t))
3412 (t
3413 ;; Switch to another buffer in window.
3414 (set-window-dedicated-p nil nil)
3415 (switch-to-prev-buffer nil 'bury)))
3416
3417 ;; Always return nil.
3418 nil))
3419
3420 (defun unbury-buffer ()
3421 "Switch to the last buffer in the buffer list."
3422 (interactive)
3423 (switch-to-buffer (last-buffer)))
3424
3425 (defun next-buffer ()
3426 "In selected window switch to next buffer."
3427 (interactive)
3428 (cond
3429 ((window-minibuffer-p)
3430 (error "Cannot switch buffers in minibuffer window"))
3431 ((eq (window-dedicated-p) t)
3432 (error "Window is strongly dedicated to its buffer"))
3433 (t
3434 (switch-to-next-buffer))))
3435
3436 (defun previous-buffer ()
3437 "In selected window switch to previous buffer."
3438 (interactive)
3439 (cond
3440 ((window-minibuffer-p)
3441 (error "Cannot switch buffers in minibuffer window"))
3442 ((eq (window-dedicated-p) t)
3443 (error "Window is strongly dedicated to its buffer"))
3444 (t
3445 (switch-to-prev-buffer))))
3446
3447 (defun delete-windows-on (&optional buffer-or-name frame)
3448 "Delete all windows showing BUFFER-OR-NAME.
3449 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3450 and defaults to the current buffer.
3451
3452 The following non-nil values of the optional argument FRAME
3453 have special meanings:
3454
3455 - t means consider all windows on the selected frame only.
3456
3457 - `visible' means consider all windows on all visible frames on
3458 the current terminal.
3459
3460 - 0 (the number zero) means consider all windows on all visible
3461 and iconified frames on the current terminal.
3462
3463 - A frame means consider all windows on that frame only.
3464
3465 Any other value of FRAME means consider all windows on all
3466 frames.
3467
3468 When a window showing BUFFER-OR-NAME is dedicated and the only
3469 window of its frame, that frame is deleted when there are other
3470 frames left."
3471 (interactive "BDelete windows on (buffer):\nP")
3472 (let ((buffer (window-normalize-buffer buffer-or-name))
3473 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3474 ;; `window-list-1' based function.
3475 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3476 (dolist (window (window-list-1 nil nil all-frames))
3477 (if (eq (window-buffer window) buffer)
3478 (let ((deletable (window-deletable-p window)))
3479 (cond
3480 ((and (eq deletable 'frame) (window-dedicated-p window))
3481 ;; Delete frame if and only if window is dedicated.
3482 (delete-frame (window-frame window)))
3483 ((eq deletable t)
3484 ;; Delete window.
3485 (delete-window window))
3486 (t
3487 ;; In window switch to previous buffer.
3488 (set-window-dedicated-p window nil)
3489 (switch-to-prev-buffer window 'bury))))
3490 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3491 (unrecord-window-buffer window buffer)))))
3492
3493 (defun replace-buffer-in-windows (&optional buffer-or-name)
3494 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3495 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3496 and defaults to the current buffer.
3497
3498 When a window showing BUFFER-OR-NAME is dedicated, that window is
3499 deleted. If that window is the only window on its frame, the
3500 frame is deleted too when there are other frames left. If there
3501 are no other frames left, some other buffer is displayed in that
3502 window.
3503
3504 This function removes the buffer denoted by BUFFER-OR-NAME from
3505 all window-local buffer lists."
3506 (interactive "bBuffer to replace: ")
3507 (let ((buffer (window-normalize-buffer buffer-or-name)))
3508 (dolist (window (window-list-1 nil nil t))
3509 (if (eq (window-buffer window) buffer)
3510 (unless (window--delete window t t)
3511 ;; Switch to another buffer in window.
3512 (set-window-dedicated-p window nil)
3513 (switch-to-prev-buffer window 'kill))
3514 ;; Unrecord BUFFER in WINDOW.
3515 (unrecord-window-buffer window buffer)))))
3516
3517 (defun quit-restore-window (&optional window bury-or-kill)
3518 "Quit WINDOW and deal with its buffer.
3519 WINDOW must be a live window and defaults to the selected one.
3520
3521 According to information stored in WINDOW's `quit-restore' window
3522 parameter either (1) delete WINDOW and its frame, (2) delete
3523 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3524 or (4) make WINDOW display some other buffer than the present
3525 one. If non-nil, reset `quit-restore' parameter to nil.
3526
3527 Optional second argument BURY-OR-KILL tells how to proceed with
3528 the buffer of WINDOW. The following values are handled:
3529
3530 `nil' means to not handle the buffer in a particular way. This
3531 means that if WINDOW is not deleted by this function, invoking
3532 `switch-to-prev-buffer' will usually show the buffer again.
3533
3534 `append' means that if WINDOW is not deleted, move its buffer to
3535 the end of WINDOW's previous buffers so it's less likely that a
3536 future invocation of `switch-to-prev-buffer' will switch to it.
3537 Also, move the buffer to the end of the frame's buffer list.
3538
3539 `bury' means that if WINDOW is not deleted, remove its buffer
3540 from WINDOW'S list of previous buffers. Also, move the buffer
3541 to the end of the frame's buffer list. This value provides the
3542 most reliable remedy to not have `switch-to-prev-buffer' switch
3543 to this buffer again without killing the buffer.
3544
3545 `kill' means to kill WINDOW's buffer."
3546 (setq window (window-normalize-window window t))
3547 (let* ((buffer (window-buffer window))
3548 (quit-restore (window-parameter window 'quit-restore))
3549 (prev-buffer
3550 (let* ((prev-buffers (window-prev-buffers window))
3551 (prev-buffer (caar prev-buffers)))
3552 (and (or (not (eq prev-buffer buffer))
3553 (and (cdr prev-buffers)
3554 (not (eq (setq prev-buffer (cadr prev-buffers))
3555 buffer))))
3556 prev-buffer)))
3557 quad entry)
3558 (cond
3559 ((and (not prev-buffer)
3560 (or (eq (nth 1 quit-restore) 'frame)
3561 (and (eq (nth 1 quit-restore) 'window)
3562 ;; If the window has been created on an existing
3563 ;; frame and ended up as the sole window on that
3564 ;; frame, do not delete it (Bug#12764).
3565 (not (eq window (frame-root-window window)))))
3566 (eq (nth 3 quit-restore) buffer)
3567 ;; Delete WINDOW if possible.
3568 (window--delete window nil (eq bury-or-kill 'kill)))
3569 ;; If the previously selected window is still alive, select it.
3570 (when (window-live-p (nth 2 quit-restore))
3571 (select-window (nth 2 quit-restore))))
3572 ((and (listp (setq quad (nth 1 quit-restore)))
3573 (buffer-live-p (car quad))
3574 (eq (nth 3 quit-restore) buffer))
3575 ;; Show another buffer stored in quit-restore parameter.
3576 (when (and (integerp (nth 3 quad))
3577 (/= (nth 3 quad) (window-total-size window)))
3578 ;; Try to resize WINDOW to its old height but don't signal an
3579 ;; error.
3580 (condition-case nil
3581 (window-resize window (- (nth 3 quad) (window-total-size window)))
3582 (error nil)))
3583 (set-window-dedicated-p window nil)
3584 ;; Restore WINDOW's previous buffer, start and point position.
3585 (set-window-buffer-start-and-point
3586 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
3587 ;; Deal with the buffer we just removed from WINDOW.
3588 (setq entry (and (eq bury-or-kill 'append)
3589 (assq buffer (window-prev-buffers window))))
3590 (when bury-or-kill
3591 ;; Remove buffer from WINDOW's previous and next buffers.
3592 (set-window-prev-buffers
3593 window (assq-delete-all buffer (window-prev-buffers window)))
3594 (set-window-next-buffers
3595 window (delq buffer (window-next-buffers window))))
3596 (when entry
3597 ;; Append old buffer's entry to list of WINDOW's previous
3598 ;; buffers so it's less likely to get switched to soon but
3599 ;; `display-buffer-in-previous-window' can nevertheless find it.
3600 (set-window-prev-buffers
3601 window (append (window-prev-buffers window) (list entry))))
3602 ;; Reset the quit-restore parameter.
3603 (set-window-parameter window 'quit-restore nil)
3604 ;; Select old window.
3605 (when (window-live-p (nth 2 quit-restore))
3606 (select-window (nth 2 quit-restore))))
3607 (t
3608 ;; Show some other buffer in WINDOW and reset the quit-restore
3609 ;; parameter.
3610 (set-window-parameter window 'quit-restore nil)
3611 ;; Make sure that WINDOW is no more dedicated.
3612 (set-window-dedicated-p window nil)
3613 (switch-to-prev-buffer window bury-or-kill)))
3614
3615 ;; Deal with the buffer.
3616 (cond
3617 ((not (buffer-live-p buffer)))
3618 ((eq bury-or-kill 'kill)
3619 (kill-buffer buffer))
3620 (bury-or-kill
3621 (bury-buffer-internal buffer)))))
3622
3623 (defun quit-window (&optional kill window)
3624 "Quit WINDOW and bury its buffer.
3625 WINDOW must be a live window and defaults to the selected one.
3626 With prefix argument KILL non-nil, kill the buffer instead of
3627 burying it.
3628
3629 According to information stored in WINDOW's `quit-restore' window
3630 parameter either (1) delete WINDOW and its frame, (2) delete
3631 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3632 or (4) make WINDOW display some other buffer than the present
3633 one. If non-nil, reset `quit-restore' parameter to nil."
3634 (interactive "P")
3635 (quit-restore-window window (if kill 'kill 'bury)))
3636
3637 (defun quit-windows-on (&optional buffer-or-name kill frame)
3638 "Quit all windows showing BUFFER-OR-NAME.
3639 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3640 and defaults to the current buffer. Optional argument KILL
3641 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3642 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3643 `delete-windows-on'.
3644
3645 This function calls `quit-window' on all candidate windows
3646 showing BUFFER-OR-NAME."
3647 (interactive "BQuit windows on (buffer):\nP")
3648 (let ((buffer (window-normalize-buffer buffer-or-name))
3649 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3650 ;; `window-list-1' based function.
3651 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3652 (dolist (window (window-list-1 nil nil all-frames))
3653 (if (eq (window-buffer window) buffer)
3654 (quit-window kill window)
3655 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3656 (unrecord-window-buffer window buffer)))))
3657 \f
3658 ;;; Splitting windows.
3659 (defun window-split-min-size (&optional horizontal)
3660 "Return minimum height of any window when splitting windows.
3661 Optional argument HORIZONTAL non-nil means return minimum width."
3662 (if horizontal
3663 (max window-min-width window-safe-min-width)
3664 (max window-min-height window-safe-min-height)))
3665
3666 (defun split-window (&optional window size side)
3667 "Make a new window adjacent to WINDOW.
3668 WINDOW must be a valid window and defaults to the selected one.
3669 Return the new window which is always a live window.
3670
3671 Optional argument SIZE a positive number means make WINDOW SIZE
3672 lines or columns tall. If SIZE is negative, make the new window
3673 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3674 absolute value can be less than `window-min-height' or
3675 `window-min-width'; so this command can make a new window as
3676 small as one line or two columns. SIZE defaults to half of
3677 WINDOW's size. Interactively, SIZE is the prefix argument.
3678
3679 Optional third argument SIDE nil (or `below') specifies that the
3680 new window shall be located below WINDOW. SIDE `above' means the
3681 new window shall be located above WINDOW. In both cases SIZE
3682 specifies the new number of lines for WINDOW (or the new window
3683 if SIZE is negative) including space reserved for the mode and/or
3684 header line.
3685
3686 SIDE t (or `right') specifies that the new window shall be
3687 located on the right side of WINDOW. SIDE `left' means the new
3688 window shall be located on the left of WINDOW. In both cases
3689 SIZE specifies the new number of columns for WINDOW (or the new
3690 window provided SIZE is negative) including space reserved for
3691 fringes and the scrollbar or a divider column. Any other non-nil
3692 value for SIDE is currently handled like t (or `right').
3693
3694 If the variable `ignore-window-parameters' is non-nil or the
3695 `split-window' parameter of WINDOW equals t, do not process any
3696 parameters of WINDOW. Otherwise, if the `split-window' parameter
3697 of WINDOW specifies a function, call that function with all three
3698 arguments and return the value returned by that function.
3699
3700 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3701 root of that atomic window. The new window does not become a
3702 member of that atomic window.
3703
3704 If WINDOW is live, properties of the new window like margins and
3705 scrollbars are inherited from WINDOW. If WINDOW is an internal
3706 window, these properties as well as the buffer displayed in the
3707 new window are inherited from the window selected on WINDOW's
3708 frame. The selected window is not changed by this function."
3709 (interactive "i")
3710 (setq window (window-normalize-window window))
3711 (let* ((side (cond
3712 ((not side) 'below)
3713 ((memq side '(below above right left)) side)
3714 (t 'right)))
3715 (horizontal (not (memq side '(below above))))
3716 (frame (window-frame window))
3717 (parent (window-parent window))
3718 (function (window-parameter window 'split-window))
3719 (window-side (window-parameter window 'window-side))
3720 ;; Rebind the following two variables since in some cases we
3721 ;; have to override their value.
3722 (window-combination-limit window-combination-limit)
3723 (window-combination-resize window-combination-resize)
3724 atom-root)
3725
3726 (window--check frame)
3727 (catch 'done
3728 (cond
3729 ;; Ignore window parameters if either `ignore-window-parameters'
3730 ;; is t or the `split-window' parameter equals t.
3731 ((or ignore-window-parameters (eq function t)))
3732 ((functionp function)
3733 ;; The `split-window' parameter specifies the function to call.
3734 ;; If that function is `ignore', do nothing.
3735 (throw 'done (funcall function window size side)))
3736 ;; If WINDOW is part of an atomic window, split the root window
3737 ;; of that atomic window instead.
3738 ((and (window-parameter window 'window-atom)
3739 (setq atom-root (window-atom-root window))
3740 (not (eq atom-root window)))
3741 (throw 'done (split-window atom-root size side)))
3742 ;; If WINDOW is a side window or its first or last child is a
3743 ;; side window, throw an error unless `window-combination-resize'
3744 ;; equals 'side.
3745 ((and (not (eq window-combination-resize 'side))
3746 (or (window-parameter window 'window-side)
3747 (and (window-child window)
3748 (or (window-parameter
3749 (window-child window) 'window-side)
3750 (window-parameter
3751 (window-last-child window) 'window-side)))))
3752 (error "Cannot split side window or parent of side window"))
3753 ;; If `window-combination-resize' is 'side and window has a side
3754 ;; window sibling, bind `window-combination-limit' to t.
3755 ((and (not (eq window-combination-resize 'side))
3756 (or (and (window-prev-sibling window)
3757 (window-parameter
3758 (window-prev-sibling window) 'window-side))
3759 (and (window-next-sibling window)
3760 (window-parameter
3761 (window-next-sibling window) 'window-side))))
3762 (setq window-combination-limit t)))
3763
3764 ;; If `window-combination-resize' is t and SIZE is non-negative,
3765 ;; bind `window-combination-limit' to t.
3766 (when (and (eq window-combination-resize t) size (> size 0))
3767 (setq window-combination-limit t))
3768
3769 (let* ((parent-size
3770 ;; `parent-size' is the size of WINDOW's parent, provided
3771 ;; it has one.
3772 (when parent (window-total-size parent horizontal)))
3773 ;; `resize' non-nil means we are supposed to resize other
3774 ;; windows in WINDOW's combination.
3775 (resize
3776 (and window-combination-resize
3777 (or (window-parameter window 'window-side)
3778 (not (eq window-combination-resize 'side)))
3779 (not (eq window-combination-limit t))
3780 ;; Resize makes sense in iso-combinations only.
3781 (window-combined-p window horizontal)))
3782 ;; `old-size' is the current size of WINDOW.
3783 (old-size (window-total-size window horizontal))
3784 ;; `new-size' is the specified or calculated size of the
3785 ;; new window.
3786 (new-size
3787 (cond
3788 ((not size)
3789 (max (window-split-min-size horizontal)
3790 (if resize
3791 ;; When resizing try to give the new window the
3792 ;; average size of a window in its combination.
3793 (min (- parent-size
3794 (window-min-size parent horizontal))
3795 (/ parent-size
3796 (1+ (window-combinations
3797 parent horizontal))))
3798 ;; Else try to give the new window half the size
3799 ;; of WINDOW (plus an eventual odd line).
3800 (+ (/ old-size 2) (% old-size 2)))))
3801 ((>= size 0)
3802 ;; SIZE non-negative specifies the new size of WINDOW.
3803
3804 ;; Note: Specifying a non-negative SIZE is practically
3805 ;; always done as workaround for making the new window
3806 ;; appear above or on the left of the new window (the
3807 ;; ispell window is a typical example of that). In all
3808 ;; these cases the SIDE argument should be set to 'above
3809 ;; or 'left in order to support the 'resize option.
3810 ;; Here we have to nest the windows instead, see above.
3811 (- old-size size))
3812 (t
3813 ;; SIZE negative specifies the size of the new window.
3814 (- size))))
3815 new-parent new-normal)
3816
3817 ;; Check SIZE.
3818 (cond
3819 ((not size)
3820 (cond
3821 (resize
3822 ;; SIZE unspecified, resizing.
3823 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3824 ;; Try again with minimum split size.
3825 (setq new-size
3826 (max new-size (window-split-min-size horizontal)))
3827 (not (window-sizable-p parent (- new-size) horizontal)))
3828 (error "Window %s too small for splitting" parent)))
3829 ((> (+ new-size (window-min-size window horizontal)) old-size)
3830 ;; SIZE unspecified, no resizing.
3831 (error "Window %s too small for splitting" window))))
3832 ((and (>= size 0)
3833 (or (>= size old-size)
3834 (< new-size (if horizontal
3835 window-safe-min-width
3836 window-safe-min-width))))
3837 ;; SIZE specified as new size of old window. If the new size
3838 ;; is larger than the old size or the size of the new window
3839 ;; would be less than the safe minimum, signal an error.
3840 (error "Window %s too small for splitting" window))
3841 (resize
3842 ;; SIZE specified, resizing.
3843 (unless (window-sizable-p parent (- new-size) horizontal)
3844 ;; If we cannot resize the parent give up.
3845 (error "Window %s too small for splitting" parent)))
3846 ((or (< new-size
3847 (if horizontal window-safe-min-width window-safe-min-height))
3848 (< (- old-size new-size)
3849 (if horizontal window-safe-min-width window-safe-min-height)))
3850 ;; SIZE specification violates minimum size restrictions.
3851 (error "Window %s too small for splitting" window)))
3852
3853 (window--resize-reset frame horizontal)
3854
3855 (setq new-parent
3856 ;; Make new-parent non-nil if we need a new parent window;
3857 ;; either because we want to nest or because WINDOW is not
3858 ;; iso-combined.
3859 (or (eq window-combination-limit t)
3860 (not (window-combined-p window horizontal))))
3861 (setq new-normal
3862 ;; Make new-normal the normal size of the new window.
3863 (cond
3864 (size (/ (float new-size) (if new-parent old-size parent-size)))
3865 (new-parent 0.5)
3866 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
3867 (t (/ (window-normal-size window horizontal) 2.0))))
3868
3869 (if resize
3870 ;; Try to get space from OLD's siblings. We could go "up" and
3871 ;; try getting additional space from surrounding windows but
3872 ;; we won't be able to return space to those windows when we
3873 ;; delete the one we create here. Hence we do not go up.
3874 (progn
3875 (window--resize-child-windows parent (- new-size) horizontal)
3876 (let* ((normal (- 1.0 new-normal))
3877 (sub (window-child parent)))
3878 (while sub
3879 (set-window-new-normal
3880 sub (* (window-normal-size sub horizontal) normal))
3881 (setq sub (window-right sub)))))
3882 ;; Get entire space from WINDOW.
3883 (set-window-new-total window (- old-size new-size))
3884 (window--resize-this-window window (- new-size) horizontal)
3885 (set-window-new-normal
3886 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3887 new-normal)))
3888
3889 (let* ((new (split-window-internal window new-size side new-normal)))
3890 ;; Assign window-side parameters, if any.
3891 (when (eq window-combination-resize 'side)
3892 (let ((window-side
3893 (cond
3894 (window-side window-side)
3895 ((eq side 'above) 'top)
3896 ((eq side 'below) 'bottom)
3897 (t side))))
3898 ;; We made a new side window.
3899 (set-window-parameter new 'window-side window-side)
3900 (when (and new-parent (window-parameter window 'window-side))
3901 ;; We've been splitting a side root window. Give the
3902 ;; new parent the same window-side parameter.
3903 (set-window-parameter
3904 (window-parent new) 'window-side window-side))))
3905
3906 (run-window-configuration-change-hook frame)
3907 (window--check frame)
3908 ;; Always return the new window.
3909 new)))))
3910
3911 ;; I think this should be the default; I think people will prefer it--rms.
3912 (defcustom split-window-keep-point t
3913 "If non-nil, \\[split-window-below] preserves point in the new window.
3914 If nil, adjust point in the two windows to minimize redisplay.
3915 This option applies only to `split-window-below' and functions
3916 that call it. The low-level `split-window' function always keeps
3917 the original point in both windows."
3918 :type 'boolean
3919 :group 'windows)
3920
3921 (defun split-window-below (&optional size)
3922 "Split the selected window into two windows, one above the other.
3923 The selected window is above. The newly split-off window is
3924 below, and displays the same buffer. Return the new window.
3925
3926 If optional argument SIZE is omitted or nil, both windows get the
3927 same height, or close to it. If SIZE is positive, the upper
3928 \(selected) window gets SIZE lines. If SIZE is negative, the
3929 lower (new) window gets -SIZE lines.
3930
3931 If the variable `split-window-keep-point' is non-nil, both
3932 windows get the same value of point as the selected window.
3933 Otherwise, the window starts are chosen so as to minimize the
3934 amount of redisplay; this is convenient on slow terminals."
3935 (interactive "P")
3936 (let ((old-window (selected-window))
3937 (old-point (window-point))
3938 (size (and size (prefix-numeric-value size)))
3939 moved-by-window-height moved new-window bottom)
3940 (when (and size (< size 0) (< (- size) window-min-height))
3941 ;; `split-window' would not signal an error here.
3942 (error "Size of new window too small"))
3943 (setq new-window (split-window nil size))
3944 (unless split-window-keep-point
3945 (with-current-buffer (window-buffer)
3946 ;; Use `save-excursion' around vertical movements below
3947 ;; (Bug#10971). Note: When the selected window's buffer has a
3948 ;; header line, up to two lines of the buffer may not show up
3949 ;; in the resulting configuration.
3950 (save-excursion
3951 (goto-char (window-start))
3952 (setq moved (vertical-motion (window-height)))
3953 (set-window-start new-window (point))
3954 (when (> (point) (window-point new-window))
3955 (set-window-point new-window (point)))
3956 (when (= moved (window-height))
3957 (setq moved-by-window-height t)
3958 (vertical-motion -1))
3959 (setq bottom (point)))
3960 (and moved-by-window-height
3961 (<= bottom (point))
3962 (set-window-point old-window (1- bottom)))
3963 (and moved-by-window-height
3964 (<= (window-start new-window) old-point)
3965 (set-window-point new-window old-point)
3966 (select-window new-window))))
3967 ;; Always copy quit-restore parameter in interactive use.
3968 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3969 (when quit-restore
3970 (set-window-parameter new-window 'quit-restore quit-restore)))
3971 new-window))
3972
3973 (defalias 'split-window-vertically 'split-window-below)
3974
3975 (defun split-window-right (&optional size)
3976 "Split the selected window into two side-by-side windows.
3977 The selected window is on the left. The newly split-off window
3978 is on the right, and displays the same buffer. Return the new
3979 window.
3980
3981 If optional argument SIZE is omitted or nil, both windows get the
3982 same width, or close to it. If SIZE is positive, the left-hand
3983 \(selected) window gets SIZE columns. If SIZE is negative, the
3984 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3985 the width of the window's scroll bar; if there are no scroll
3986 bars, it includes the width of the divider column to the window's
3987 right, if any."
3988 (interactive "P")
3989 (let ((old-window (selected-window))
3990 (size (and size (prefix-numeric-value size)))
3991 new-window)
3992 (when (and size (< size 0) (< (- size) window-min-width))
3993 ;; `split-window' would not signal an error here.
3994 (error "Size of new window too small"))
3995 (setq new-window (split-window nil size t))
3996 ;; Always copy quit-restore parameter in interactive use.
3997 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3998 (when quit-restore
3999 (set-window-parameter new-window 'quit-restore quit-restore)))
4000 new-window))
4001
4002 (defalias 'split-window-horizontally 'split-window-right)
4003 \f
4004 ;;; Balancing windows.
4005
4006 ;; The following routine uses the recycled code from an old version of
4007 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4008 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4009 ;; more readable (FWIW we'd need three loops - one to calculate the
4010 ;; minimum sizes per window, one to enlarge or shrink windows until the
4011 ;; new parent-size matches, and one where we shrink the largest/enlarge
4012 ;; the smallest window).
4013 (defun balance-windows-2 (window horizontal)
4014 "Subroutine of `balance-windows-1'.
4015 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4016 is non-nil)."
4017 (let* ((first (window-child window))
4018 (sub first)
4019 (number-of-children 0)
4020 (parent-size (window-new-total window))
4021 (total-sum parent-size)
4022 failed size sub-total sub-delta sub-amount rest)
4023 (while sub
4024 (setq number-of-children (1+ number-of-children))
4025 (when (window-size-fixed-p sub horizontal)
4026 (setq total-sum
4027 (- total-sum (window-total-size sub horizontal)))
4028 (set-window-new-normal sub 'ignore))
4029 (setq sub (window-right sub)))
4030
4031 (setq failed t)
4032 (while (and failed (> number-of-children 0))
4033 (setq size (/ total-sum number-of-children))
4034 (setq failed nil)
4035 (setq sub first)
4036 (while (and sub (not failed))
4037 ;; Ignore child windows that should be ignored or are stuck.
4038 (unless (window--resize-child-windows-skip-p sub)
4039 (setq sub-total (window-total-size sub horizontal))
4040 (setq sub-delta (- size sub-total))
4041 (setq sub-amount
4042 (window-sizable sub sub-delta horizontal))
4043 ;; Register the new total size for this child window.
4044 (set-window-new-total sub (+ sub-total sub-amount))
4045 (unless (= sub-amount sub-delta)
4046 (setq total-sum (- total-sum sub-total sub-amount))
4047 (setq number-of-children (1- number-of-children))
4048 ;; We failed and need a new round.
4049 (setq failed t)
4050 (set-window-new-normal sub 'skip)))
4051 (setq sub (window-right sub))))
4052
4053 (setq rest (% total-sum number-of-children))
4054 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4055 ;; (column) until `rest' is zero.
4056 (setq sub first)
4057 (while (and sub (> rest 0))
4058 (unless (window--resize-child-windows-skip-p window)
4059 (set-window-new-total sub 1 t)
4060 (setq rest (1- rest)))
4061 (setq sub (window-right sub)))
4062
4063 ;; Fix rounding by trying to enlarge stuck windows by one line
4064 ;; (column) until `rest' equals zero.
4065 (setq sub first)
4066 (while (and sub (> rest 0))
4067 (unless (eq (window-new-normal sub) 'ignore)
4068 (set-window-new-total sub 1 t)
4069 (setq rest (1- rest)))
4070 (setq sub (window-right sub)))
4071
4072 (setq sub first)
4073 (while sub
4074 ;; Record new normal sizes.
4075 (set-window-new-normal
4076 sub (/ (if (eq (window-new-normal sub) 'ignore)
4077 (window-total-size sub horizontal)
4078 (window-new-total sub))
4079 (float parent-size)))
4080 ;; Recursively balance each window's child windows.
4081 (balance-windows-1 sub horizontal)
4082 (setq sub (window-right sub)))))
4083
4084 (defun balance-windows-1 (window &optional horizontal)
4085 "Subroutine of `balance-windows'."
4086 (if (window-child window)
4087 (let ((sub (window-child window)))
4088 (if (window-combined-p sub horizontal)
4089 (balance-windows-2 window horizontal)
4090 (let ((size (window-new-total window)))
4091 (while sub
4092 (set-window-new-total sub size)
4093 (balance-windows-1 sub horizontal)
4094 (setq sub (window-right sub))))))))
4095
4096 (defun balance-windows (&optional window-or-frame)
4097 "Balance the sizes of windows of WINDOW-OR-FRAME.
4098 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4099 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4100 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4101 recursively balance the sizes of all child windows of that
4102 window."
4103 (interactive)
4104 (let* ((window
4105 (cond
4106 ((or (not window-or-frame)
4107 (frame-live-p window-or-frame))
4108 (frame-root-window window-or-frame))
4109 ((or (window-live-p window-or-frame)
4110 (window-child window-or-frame))
4111 window-or-frame)
4112 (t
4113 (error "Not a window or frame %s" window-or-frame))))
4114 (frame (window-frame window)))
4115 ;; Balance vertically.
4116 (window--resize-reset (window-frame window))
4117 (balance-windows-1 window)
4118 (window-resize-apply frame)
4119 ;; Balance horizontally.
4120 (window--resize-reset (window-frame window) t)
4121 (balance-windows-1 window t)
4122 (window-resize-apply frame t)))
4123
4124 (defun window-fixed-size-p (&optional window direction)
4125 "Return t if WINDOW cannot be resized in DIRECTION.
4126 WINDOW defaults to the selected window. DIRECTION can be
4127 nil (i.e. any), `height' or `width'."
4128 (with-current-buffer (window-buffer window)
4129 (when (and (boundp 'window-size-fixed) window-size-fixed)
4130 (not (and direction
4131 (member (cons direction window-size-fixed)
4132 '((height . width) (width . height))))))))
4133
4134 ;;; A different solution to balance-windows.
4135 (defvar window-area-factor 1
4136 "Factor by which the window area should be over-estimated.
4137 This is used by `balance-windows-area'.
4138 Changing this globally has no effect.")
4139 (make-variable-buffer-local 'window-area-factor)
4140
4141 (defun balance-windows-area-adjust (window delta horizontal)
4142 "Wrapper around `window-resize' with error checking.
4143 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4144 ;; `window-resize' may fail if delta is too large.
4145 (while (>= (abs delta) 1)
4146 (condition-case nil
4147 (progn
4148 (window-resize window delta horizontal)
4149 (setq delta 0))
4150 (error
4151 ;;(message "adjust: %s" (error-message-string err))
4152 (setq delta (/ delta 2))))))
4153
4154 (defun balance-windows-area ()
4155 "Make all visible windows the same area (approximately).
4156 See also `window-area-factor' to change the relative size of
4157 specific buffers."
4158 (interactive)
4159 (let* ((unchanged 0) (carry 0) (round 0)
4160 ;; Remove fixed-size windows.
4161 (wins (delq nil (mapcar (lambda (win)
4162 (if (not (window-fixed-size-p win)) win))
4163 (window-list nil 'nomini))))
4164 (changelog nil)
4165 next)
4166 ;; Resizing a window changes the size of surrounding windows in complex
4167 ;; ways, so it's difficult to balance them all. The introduction of
4168 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4169 ;; very difficult to do. `balance-window' above takes an off-line
4170 ;; approach: get the whole window tree, then balance it, then try to
4171 ;; adjust the windows so they fit the result.
4172 ;; Here, instead, we take a "local optimization" approach, where we just
4173 ;; go through all the windows several times until nothing needs to be
4174 ;; changed. The main problem with this approach is that it's difficult
4175 ;; to make sure it terminates, so we use some heuristic to try and break
4176 ;; off infinite loops.
4177 ;; After a round without any change, we allow a second, to give a chance
4178 ;; to the carry to propagate a minor imbalance from the end back to
4179 ;; the beginning.
4180 (while (< unchanged 2)
4181 ;; (message "New round")
4182 (setq unchanged (1+ unchanged) round (1+ round))
4183 (dolist (win wins)
4184 (setq next win)
4185 (while (progn (setq next (next-window next))
4186 (window-fixed-size-p next)))
4187 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4188 (let* ((horiz
4189 (< (car (window-edges win)) (car (window-edges next))))
4190 (areadiff (/ (- (* (window-height next) (window-width next)
4191 (buffer-local-value 'window-area-factor
4192 (window-buffer next)))
4193 (* (window-height win) (window-width win)
4194 (buffer-local-value 'window-area-factor
4195 (window-buffer win))))
4196 (max (buffer-local-value 'window-area-factor
4197 (window-buffer win))
4198 (buffer-local-value 'window-area-factor
4199 (window-buffer next)))))
4200 (edgesize (if horiz
4201 (+ (window-height win) (window-height next))
4202 (+ (window-width win) (window-width next))))
4203 (diff (/ areadiff edgesize)))
4204 (when (zerop diff)
4205 ;; Maybe diff is actually closer to 1 than to 0.
4206 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
4207 (when (and (zerop diff) (not (zerop areadiff)))
4208 (setq diff (/ (+ areadiff carry) edgesize))
4209 ;; Change things smoothly.
4210 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
4211 (if (zerop diff)
4212 ;; Make sure negligible differences don't accumulate to
4213 ;; become significant.
4214 (setq carry (+ carry areadiff))
4215 ;; This used `adjust-window-trailing-edge' before and uses
4216 ;; `window-resize' now. Error wrapping is still needed.
4217 (balance-windows-area-adjust win diff horiz)
4218 ;; (sit-for 0.5)
4219 (let ((change (cons win (window-edges win))))
4220 ;; If the same change has been seen already for this window,
4221 ;; we're most likely in an endless loop, so don't count it as
4222 ;; a change.
4223 (unless (member change changelog)
4224 (push change changelog)
4225 (setq unchanged 0 carry 0)))))))
4226 ;; We've now basically balanced all the windows.
4227 ;; But there may be some minor off-by-one imbalance left over,
4228 ;; so let's do some fine tuning.
4229 ;; (bw-finetune wins)
4230 ;; (message "Done in %d rounds" round)
4231 ))
4232
4233 ;;; Window states, how to get them and how to put them in a window.
4234 (defun window--state-get-1 (window &optional writable)
4235 "Helper function for `window-state-get'."
4236 (let* ((type
4237 (cond
4238 ((window-top-child window) 'vc)
4239 ((window-left-child window) 'hc)
4240 (t 'leaf)))
4241 (buffer (window-buffer window))
4242 (selected (eq window (selected-window)))
4243 (head
4244 `(,type
4245 ,@(unless (window-next-sibling window) `((last . t)))
4246 (total-height . ,(window-total-size window))
4247 (total-width . ,(window-total-size window t))
4248 (normal-height . ,(window-normal-size window))
4249 (normal-width . ,(window-normal-size window t))
4250 (combination-limit . ,(window-combination-limit window))
4251 ,@(let ((parameters (window-parameters window))
4252 list)
4253 ;; Make copies of those window parameters whose
4254 ;; persistence property is `writable' if WRITABLE is
4255 ;; non-nil and non-nil if WRITABLE is nil.
4256 (dolist (par parameters)
4257 (let ((pers (cdr (assq (car par)
4258 window-persistent-parameters))))
4259 (when (and pers (or (not writable) (eq pers 'writable)))
4260 (setq list (cons (cons (car par) (cdr par)) list)))))
4261 ;; Add `clone-of' parameter if necessary.
4262 (let ((pers (cdr (assq 'clone-of
4263 window-persistent-parameters))))
4264 (when (and pers (or (not writable) (eq pers 'writable))
4265 (not (assq 'clone-of list)))
4266 (setq list (cons (cons 'clone-of window) list))))
4267 (when list
4268 `((parameters . ,list))))
4269 ,@(when buffer
4270 ;; All buffer related things go in here.
4271 (let ((point (window-point window))
4272 (start (window-start window)))
4273 `((buffer
4274 ,(buffer-name buffer)
4275 (selected . ,selected)
4276 (hscroll . ,(window-hscroll window))
4277 (fringes . ,(window-fringes window))
4278 (margins . ,(window-margins window))
4279 (scroll-bars . ,(window-scroll-bars window))
4280 (vscroll . ,(window-vscroll window))
4281 (dedicated . ,(window-dedicated-p window))
4282 (point . ,(if writable point
4283 (copy-marker point
4284 (buffer-local-value
4285 'window-point-insertion-type
4286 buffer))))
4287 (start . ,(if writable start (copy-marker start)))))))))
4288 (tail
4289 (when (memq type '(vc hc))
4290 (let (list)
4291 (setq window (window-child window))
4292 (while window
4293 (setq list (cons (window--state-get-1 window writable) list))
4294 (setq window (window-right window)))
4295 (nreverse list)))))
4296 (append head tail)))
4297
4298 (defun window-state-get (&optional window writable)
4299 "Return state of WINDOW as a Lisp object.
4300 WINDOW can be any window and defaults to the root window of the
4301 selected frame.
4302
4303 Optional argument WRITABLE non-nil means do not use markers for
4304 sampling `window-point' and `window-start'. Together, WRITABLE
4305 and the variable `window-persistent-parameters' specify which
4306 window parameters are saved by this function. WRITABLE should be
4307 non-nil when the return value shall be written to a file and read
4308 back in another session. Otherwise, an application may run into
4309 an `invalid-read-syntax' error while attempting to read back the
4310 value from file.
4311
4312 The return value can be used as argument for `window-state-put'
4313 to put the state recorded here into an arbitrary window. The
4314 value can be also stored on disk and read back in a new session."
4315 (setq window
4316 (if window
4317 (if (window-valid-p window)
4318 window
4319 (error "%s is not a live or internal window" window))
4320 (frame-root-window)))
4321 ;; The return value is a cons whose car specifies some constraints on
4322 ;; the size of WINDOW. The cdr lists the states of the child windows
4323 ;; of WINDOW.
4324 (cons
4325 ;; Frame related things would go into a function, say `frame-state',
4326 ;; calling `window-state-get' to insert the frame's root window.
4327 `((min-height . ,(window-min-size window))
4328 (min-width . ,(window-min-size window t))
4329 (min-height-ignore . ,(window-min-size window nil t))
4330 (min-width-ignore . ,(window-min-size window t t))
4331 (min-height-safe . ,(window-min-size window nil 'safe))
4332 (min-width-safe . ,(window-min-size window t 'safe)))
4333 (window--state-get-1 window writable)))
4334
4335 (defvar window-state-put-list nil
4336 "Helper variable for `window-state-put'.")
4337
4338 (defun window--state-put-1 (state &optional window ignore totals)
4339 "Helper function for `window-state-put'."
4340 (let ((type (car state)))
4341 (setq state (cdr state))
4342 (cond
4343 ((eq type 'leaf)
4344 ;; For a leaf window just add unprocessed entries to
4345 ;; `window-state-put-list'.
4346 (push (cons window state) window-state-put-list))
4347 ((memq type '(vc hc))
4348 (let* ((horizontal (eq type 'hc))
4349 (total (window-total-size window horizontal))
4350 (first t)
4351 size new)
4352 (dolist (item state)
4353 ;; Find the next child window. WINDOW always points to the
4354 ;; real window that we want to fill with what we find here.
4355 (when (memq (car item) '(leaf vc hc))
4356 (if (assq 'last item)
4357 ;; The last child window. Below `window--state-put-1'
4358 ;; will put into it whatever ITEM has in store.
4359 (setq new nil)
4360 ;; Not the last child window, prepare for splitting
4361 ;; WINDOW. SIZE is the new (and final) size of the old
4362 ;; window.
4363 (setq size
4364 (if totals
4365 ;; Use total size.
4366 (cdr (assq (if horizontal 'total-width 'total-height) item))
4367 ;; Use normalized size and round.
4368 (round (* total
4369 (cdr (assq
4370 (if horizontal 'normal-width 'normal-height)
4371 item))))))
4372
4373 ;; Use safe sizes, we try to resize later.
4374 (setq size (max size (if horizontal
4375 window-safe-min-height
4376 window-safe-min-width)))
4377
4378 (if (window-sizable-p window (- size) horizontal 'safe)
4379 (let* ((window-combination-limit
4380 (assq 'combination-limit item)))
4381 ;; We must inherit the combination limit, otherwise
4382 ;; we might mess up handling of atomic and side
4383 ;; window.
4384 (setq new (split-window window size horizontal)))
4385 ;; Give up if we can't resize window down to safe sizes.
4386 (error "Cannot resize window %s" window))
4387
4388 (when first
4389 (setq first nil)
4390 ;; When creating the first child window add for parent
4391 ;; unprocessed entries to `window-state-put-list'.
4392 (setq window-state-put-list
4393 (cons (cons (window-parent window) state)
4394 window-state-put-list))))
4395
4396 ;; Now process the current window (either the one we've just
4397 ;; split or the last child of its parent).
4398 (window--state-put-1 item window ignore totals)
4399 ;; Continue with the last window split off.
4400 (setq window new))))))))
4401
4402 (defun window--state-put-2 (ignore)
4403 "Helper function for `window-state-put'."
4404 (dolist (item window-state-put-list)
4405 (let ((window (car item))
4406 (combination-limit (cdr (assq 'combination-limit item)))
4407 (parameters (cdr (assq 'parameters item)))
4408 (state (cdr (assq 'buffer item))))
4409 (when combination-limit
4410 (set-window-combination-limit window combination-limit))
4411 ;; Reset window's parameters and assign saved ones (we might want
4412 ;; a `remove-window-parameters' function here).
4413 (dolist (parameter (window-parameters window))
4414 (set-window-parameter window (car parameter) nil))
4415 (when parameters
4416 (dolist (parameter parameters)
4417 (set-window-parameter window (car parameter) (cdr parameter))))
4418 ;; Process buffer related state.
4419 (when state
4420 ;; We don't want to raise an error here so we create a buffer if
4421 ;; there's none.
4422 (set-window-buffer window (get-buffer-create (car state)))
4423 (with-current-buffer (window-buffer window)
4424 (set-window-hscroll window (cdr (assq 'hscroll state)))
4425 (apply 'set-window-fringes
4426 (cons window (cdr (assq 'fringes state))))
4427 (let ((margins (cdr (assq 'margins state))))
4428 (set-window-margins window (car margins) (cdr margins)))
4429 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
4430 (set-window-scroll-bars
4431 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
4432 (set-window-vscroll window (cdr (assq 'vscroll state)))
4433 ;; Adjust vertically.
4434 (if (memq window-size-fixed '(t height))
4435 ;; A fixed height window, try to restore the original size.
4436 (let ((delta (- (cdr (assq 'total-height item))
4437 (window-total-height window)))
4438 window-size-fixed)
4439 (when (window--resizable-p window delta)
4440 (window-resize window delta)))
4441 ;; Else check whether the window is not high enough.
4442 (let* ((min-size (window-min-size window nil ignore))
4443 (delta (- min-size (window-total-size window))))
4444 (when (and (> delta 0)
4445 (window--resizable-p window delta nil ignore))
4446 (window-resize window delta nil ignore))))
4447 ;; Adjust horizontally.
4448 (if (memq window-size-fixed '(t width))
4449 ;; A fixed width window, try to restore the original size.
4450 (let ((delta (- (cdr (assq 'total-width item))
4451 (window-total-width window)))
4452 window-size-fixed)
4453 (when (window--resizable-p window delta)
4454 (window-resize window delta)))
4455 ;; Else check whether the window is not wide enough.
4456 (let* ((min-size (window-min-size window t ignore))
4457 (delta (- min-size (window-total-size window t))))
4458 (when (and (> delta 0)
4459 (window--resizable-p window delta t ignore))
4460 (window-resize window delta t ignore))))
4461 ;; Set dedicated status.
4462 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
4463 ;; Install positions (maybe we should do this after all windows
4464 ;; have been created and sized).
4465 (ignore-errors
4466 (set-window-start window (cdr (assq 'start state)))
4467 (set-window-point window (cdr (assq 'point state))))
4468 ;; Select window if it's the selected one.
4469 (when (cdr (assq 'selected state))
4470 (select-window window)))))))
4471
4472 (defun window-state-put (state &optional window ignore)
4473 "Put window state STATE into WINDOW.
4474 STATE should be the state of a window returned by an earlier
4475 invocation of `window-state-get'. Optional argument WINDOW must
4476 specify a live window and defaults to the selected one.
4477
4478 Optional argument IGNORE non-nil means ignore minimum window
4479 sizes and fixed size restrictions. IGNORE equal `safe' means
4480 windows can get as small as `window-safe-min-height' and
4481 `window-safe-min-width'."
4482 (setq window (window-normalize-window window t))
4483 (let* ((frame (window-frame window))
4484 (head (car state))
4485 ;; We check here (1) whether the total sizes of root window of
4486 ;; STATE and that of WINDOW are equal so we can avoid
4487 ;; calculating new sizes, and (2) if we do have to resize
4488 ;; whether we can do so without violating size restrictions.
4489 (totals
4490 (and (= (window-total-size window)
4491 (cdr (assq 'total-height state)))
4492 (= (window-total-size window t)
4493 (cdr (assq 'total-width state)))))
4494 (min-height (cdr (assq 'min-height head)))
4495 (min-width (cdr (assq 'min-width head))))
4496 (if (and (not totals)
4497 (or (> min-height (window-total-size window))
4498 (> min-width (window-total-size window t)))
4499 (or (not ignore)
4500 (and (setq min-height
4501 (cdr (assq 'min-height-ignore head)))
4502 (setq min-width
4503 (cdr (assq 'min-width-ignore head)))
4504 (or (> min-height (window-total-size window))
4505 (> min-width (window-total-size window t)))
4506 (or (not (eq ignore 'safe))
4507 (and (setq min-height
4508 (cdr (assq 'min-height-safe head)))
4509 (setq min-width
4510 (cdr (assq 'min-width-safe head)))
4511 (or (> min-height
4512 (window-total-size window))
4513 (> min-width
4514 (window-total-size window t))))))))
4515 ;; The check above might not catch all errors due to rounding
4516 ;; issues - so IGNORE equal 'safe might not always produce the
4517 ;; minimum possible state. But such configurations hardly make
4518 ;; sense anyway.
4519 (error "Window %s too small to accommodate state" window)
4520 (setq state (cdr state))
4521 (setq window-state-put-list nil)
4522 ;; Work on the windows of a temporary buffer to make sure that
4523 ;; splitting proceeds regardless of any buffer local values of
4524 ;; `window-size-fixed'. Release that buffer after the buffers of
4525 ;; all live windows have been set by `window--state-put-2'.
4526 (with-temp-buffer
4527 (set-window-buffer window (current-buffer))
4528 (window--state-put-1 state window nil totals)
4529 (window--state-put-2 ignore))
4530 (window--check frame))))
4531 \f
4532 (defun display-buffer-record-window (type window buffer)
4533 "Record information for window used by `display-buffer'.
4534 TYPE specifies the type of the calling operation and must be one
4535 of the symbols 'reuse (when WINDOW existed already and was
4536 reused for displaying BUFFER), 'window (when WINDOW was created
4537 on an already existing frame), or 'frame (when WINDOW was
4538 created on a new frame). WINDOW is the window used for or created
4539 by the `display-buffer' routines. BUFFER is the buffer that
4540 shall be displayed.
4541
4542 This function installs or updates the quit-restore parameter of
4543 WINDOW. The quit-restore parameter is a list of four elements:
4544 The first element is one of the symbols 'window, 'frame, 'same or
4545 'other. The second element is either one of the symbols 'window
4546 or 'frame or a list whose elements are the buffer previously
4547 shown in the window, that buffer's window start and window point,
4548 and the window's height. The third element is the window
4549 selected at the time the parameter was created. The fourth
4550 element is BUFFER."
4551 (cond
4552 ((eq type 'reuse)
4553 (if (eq (window-buffer window) buffer)
4554 ;; WINDOW shows BUFFER already.
4555 (when (consp (window-parameter window 'quit-restore))
4556 ;; If WINDOW has a quit-restore parameter, reset its car.
4557 (setcar (window-parameter window 'quit-restore) 'same))
4558 ;; WINDOW shows another buffer.
4559 (set-window-parameter
4560 window 'quit-restore
4561 (list 'other
4562 ;; A quadruple of WINDOW's buffer, start, point and height.
4563 (list (window-buffer window) (window-start window)
4564 (window-point window) (window-total-size window))
4565 (selected-window) buffer))))
4566 ((eq type 'window)
4567 ;; WINDOW has been created on an existing frame.
4568 (set-window-parameter
4569 window 'quit-restore
4570 (list 'window 'window (selected-window) buffer)))
4571 ((eq type 'frame)
4572 ;; WINDOW has been created on a new frame.
4573 (set-window-parameter
4574 window 'quit-restore
4575 (list 'frame 'frame (selected-window) buffer)))))
4576
4577 (defcustom display-buffer-function nil
4578 "If non-nil, function to call to handle `display-buffer'.
4579 It will receive two args, the buffer and a flag which if non-nil
4580 means that the currently selected window is not acceptable. It
4581 should choose or create a window, display the specified buffer in
4582 it, and return the window.
4583
4584 The specified function should call `display-buffer-record-window'
4585 with corresponding arguments to set up the quit-restore parameter
4586 of the window used."
4587 :type '(choice
4588 (const nil)
4589 (function :tag "function"))
4590 :group 'windows)
4591
4592 (make-obsolete-variable 'display-buffer-function
4593 'display-buffer-alist "24.3")
4594
4595 ;; Eventually, we want to turn this into a defvar; instead of
4596 ;; customizing this, the user should use a `pop-up-frame-parameters'
4597 ;; alist entry in `display-buffer-base-action'.
4598 (defcustom pop-up-frame-alist nil
4599 "Alist of parameters for automatically generated new frames.
4600 If non-nil, the value you specify here is used by the default
4601 `pop-up-frame-function' for the creation of new frames.
4602
4603 Since `pop-up-frame-function' is used by `display-buffer' for
4604 making new frames, any value specified here by default affects
4605 the automatic generation of new frames via `display-buffer' and
4606 all functions based on it. The behavior of `make-frame' is not
4607 affected by this variable."
4608 :type '(repeat (cons :format "%v"
4609 (symbol :tag "Parameter")
4610 (sexp :tag "Value")))
4611 :group 'frames)
4612
4613 (defcustom pop-up-frame-function
4614 (lambda () (make-frame pop-up-frame-alist))
4615 "Function used by `display-buffer' for creating a new frame.
4616 This function is called with no arguments and should return a new
4617 frame. The default value calls `make-frame' with the argument
4618 `pop-up-frame-alist'."
4619 :type 'function
4620 :group 'frames)
4621
4622 (defcustom special-display-buffer-names nil
4623 "List of names of buffers that should be displayed specially.
4624 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4625 its name is in this list, displays the buffer in a way specified
4626 by `special-display-function'. `special-display-popup-frame'
4627 \(the default for `special-display-function') usually displays
4628 the buffer in a separate frame made with the parameters specified
4629 by `special-display-frame-alist'. If `special-display-function'
4630 has been set to some other function, that function is called with
4631 the buffer as first, and nil as second argument.
4632
4633 Alternatively, an element of this list can be specified as
4634 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4635 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4636 `special-display-popup-frame' will interpret such pairs as frame
4637 parameters when it creates a special frame, overriding the
4638 corresponding values from `special-display-frame-alist'.
4639
4640 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4641 `special-display-popup-frame' displays that buffer in the
4642 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4643 it displays that buffer in a window on the selected frame.
4644
4645 If `special-display-function' specifies some other function than
4646 `special-display-popup-frame', that function is called with the
4647 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4648 argument.
4649
4650 Finally, an element of this list can be also specified as
4651 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4652 `special-display-popup-frame' will call FUNCTION with the buffer
4653 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4654 second.
4655
4656 Any alternative function specified here is responsible for
4657 setting up the quit-restore parameter of the window used.
4658
4659 If this variable appears \"not to work\", because you added a
4660 name to it but the corresponding buffer is displayed in the
4661 selected window, look at the values of `same-window-buffer-names'
4662 and `same-window-regexps'. Those variables take precedence over
4663 this one.
4664
4665 See also `special-display-regexps'."
4666 :type '(repeat
4667 (choice :tag "Buffer"
4668 :value ""
4669 (string :format "%v")
4670 (cons :tag "With parameters"
4671 :format "%v"
4672 :value ("" . nil)
4673 (string :format "%v")
4674 (repeat :tag "Parameters"
4675 (cons :format "%v"
4676 (symbol :tag "Parameter")
4677 (sexp :tag "Value"))))
4678 (list :tag "With function"
4679 :format "%v"
4680 :value ("" . nil)
4681 (string :format "%v")
4682 (function :tag "Function")
4683 (repeat :tag "Arguments" (sexp)))))
4684 :group 'windows
4685 :group 'frames)
4686 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
4687 (put 'special-display-buffer-names 'risky-local-variable t)
4688
4689 (defcustom special-display-regexps nil
4690 "List of regexps saying which buffers should be displayed specially.
4691 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4692 any regexp in this list matches its name, displays it specially
4693 using `special-display-function'. `special-display-popup-frame'
4694 \(the default for `special-display-function') usually displays
4695 the buffer in a separate frame made with the parameters specified
4696 by `special-display-frame-alist'. If `special-display-function'
4697 has been set to some other function, that function is called with
4698 the buffer as first, and nil as second argument.
4699
4700 Alternatively, an element of this list can be specified as
4701 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4702 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4703 `special-display-popup-frame' will then interpret these pairs as
4704 frame parameters when creating a special frame for a buffer whose
4705 name matches REGEXP, overriding the corresponding values from
4706 `special-display-frame-alist'.
4707
4708 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4709 `special-display-popup-frame' displays buffers matching REGEXP in
4710 the selected window. (same-frame . t) in FRAME-PARAMETERS means
4711 to display such buffers in a window on the selected frame.
4712
4713 If `special-display-function' specifies some other function than
4714 `special-display-popup-frame', that function is called with the
4715 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4716 as second argument.
4717
4718 Finally, an element of this list can be also specified as
4719 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4720 will then call FUNCTION with the buffer whose name matched
4721 REGEXP as first, and OTHER-ARGS as second argument.
4722
4723 Any alternative function specified here is responsible for
4724 setting up the quit-restore parameter of the window used.
4725
4726 If this variable appears \"not to work\", because you added a
4727 name to it but the corresponding buffer is displayed in the
4728 selected window, look at the values of `same-window-buffer-names'
4729 and `same-window-regexps'. Those variables take precedence over
4730 this one.
4731
4732 See also `special-display-buffer-names'."
4733 :type '(repeat
4734 (choice :tag "Buffer"
4735 :value ""
4736 (regexp :format "%v")
4737 (cons :tag "With parameters"
4738 :format "%v"
4739 :value ("" . nil)
4740 (regexp :format "%v")
4741 (repeat :tag "Parameters"
4742 (cons :format "%v"
4743 (symbol :tag "Parameter")
4744 (sexp :tag "Value"))))
4745 (list :tag "With function"
4746 :format "%v"
4747 :value ("" . nil)
4748 (regexp :format "%v")
4749 (function :tag "Function")
4750 (repeat :tag "Arguments" (sexp)))))
4751 :group 'windows
4752 :group 'frames)
4753 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
4754 (put 'special-display-regexps 'risky-local-variable t)
4755
4756 (defun special-display-p (buffer-name)
4757 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4758 More precisely, return t if `special-display-buffer-names' or
4759 `special-display-regexps' contain a string entry equaling or
4760 matching BUFFER-NAME. If `special-display-buffer-names' or
4761 `special-display-regexps' contain a list entry whose car equals
4762 or matches BUFFER-NAME, the return value is the cdr of that
4763 entry."
4764 (let (tmp)
4765 (cond
4766 ((member buffer-name special-display-buffer-names)
4767 t)
4768 ((setq tmp (assoc buffer-name special-display-buffer-names))
4769 (cdr tmp))
4770 ((catch 'found
4771 (dolist (regexp special-display-regexps)
4772 (cond
4773 ((stringp regexp)
4774 (when (string-match-p regexp buffer-name)
4775 (throw 'found t)))
4776 ((and (consp regexp) (stringp (car regexp))
4777 (string-match-p (car regexp) buffer-name))
4778 (throw 'found (cdr regexp))))))))))
4779
4780 (defcustom special-display-frame-alist
4781 '((height . 14) (width . 80) (unsplittable . t))
4782 "Alist of parameters for special frames.
4783 Special frames are used for buffers whose names are listed in
4784 `special-display-buffer-names' and for buffers whose names match
4785 one of the regular expressions in `special-display-regexps'.
4786
4787 This variable can be set in your init file, like this:
4788
4789 (setq special-display-frame-alist '((width . 80) (height . 20)))
4790
4791 These supersede the values given in `default-frame-alist'."
4792 :type '(repeat (cons :format "%v"
4793 (symbol :tag "Parameter")
4794 (sexp :tag "Value")))
4795 :group 'frames)
4796 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
4797
4798 (defun special-display-popup-frame (buffer &optional args)
4799 "Pop up a frame displaying BUFFER and return its window.
4800 If BUFFER is already displayed in a visible or iconified frame,
4801 raise that frame. Otherwise, display BUFFER in a new frame.
4802
4803 Optional argument ARGS is a list specifying additional
4804 information.
4805
4806 If ARGS is an alist, use it as a list of frame parameters. If
4807 these parameters contain (same-window . t), display BUFFER in
4808 the selected window. If they contain (same-frame . t), display
4809 BUFFER in a window of the selected frame.
4810
4811 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4812 function to do the work. Pass it BUFFER as first argument, and
4813 pass the elements of (cdr ARGS) as the remaining arguments."
4814 (if (and args (symbolp (car args)))
4815 (apply (car args) buffer (cdr args))
4816 (let ((window (get-buffer-window buffer 0)))
4817 (or
4818 ;; If we have a window already, make it visible.
4819 (when window
4820 (let ((frame (window-frame window)))
4821 (make-frame-visible frame)
4822 (raise-frame frame)
4823 (display-buffer-record-window 'reuse window buffer)
4824 window))
4825 ;; Reuse the current window if the user requested it.
4826 (when (cdr (assq 'same-window args))
4827 (condition-case nil
4828 (progn (switch-to-buffer buffer nil t) (selected-window))
4829 (error nil)))
4830 ;; Stay on the same frame if requested.
4831 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
4832 (let* ((pop-up-windows t)
4833 pop-up-frames
4834 special-display-buffer-names special-display-regexps)
4835 (display-buffer buffer)))
4836 ;; If no window yet, make one in a new frame.
4837 (let* ((frame
4838 (with-current-buffer buffer
4839 (make-frame (append args special-display-frame-alist))))
4840 (window (frame-selected-window frame)))
4841 (display-buffer-record-window 'frame window buffer)
4842 (unless (eq buffer (window-buffer window))
4843 (set-window-buffer window buffer)
4844 (set-window-prev-buffers window nil))
4845 (set-window-dedicated-p window t)
4846 window)))))
4847
4848 (defcustom special-display-function 'special-display-popup-frame
4849 "Function to call for displaying special buffers.
4850 This function is called with two arguments - the buffer and,
4851 optionally, a list - and should return a window displaying that
4852 buffer. The default value usually makes a separate frame for the
4853 buffer using `special-display-frame-alist' to specify the frame
4854 parameters. See the definition of `special-display-popup-frame'
4855 for how to specify such a function.
4856
4857 A buffer is special when its name is either listed in
4858 `special-display-buffer-names' or matches a regexp in
4859 `special-display-regexps'.
4860
4861 The specified function should call `display-buffer-record-window'
4862 with corresponding arguments to set up the quit-restore parameter
4863 of the window used."
4864 :type 'function
4865 :group 'frames)
4866 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
4867
4868 (defcustom same-window-buffer-names nil
4869 "List of names of buffers that should appear in the \"same\" window.
4870 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4871 on this list in the selected rather than some other window.
4872
4873 An element of this list can be a cons cell instead of just a
4874 string. In that case, the cell's car must be a string specifying
4875 the buffer name. This is for compatibility with
4876 `special-display-buffer-names'; the cdr of the cons cell is
4877 ignored.
4878
4879 See also `same-window-regexps'."
4880 :type '(repeat (string :format "%v"))
4881 :group 'windows)
4882
4883 (defcustom same-window-regexps nil
4884 "List of regexps saying which buffers should appear in the \"same\" window.
4885 `display-buffer' and `pop-to-buffer' show a buffer whose name
4886 matches a regexp on this list in the selected rather than some
4887 other window.
4888
4889 An element of this list can be a cons cell instead of just a
4890 string. In that case, the cell's car must be a regexp matching
4891 the buffer name. This is for compatibility with
4892 `special-display-regexps'; the cdr of the cons cell is ignored.
4893
4894 See also `same-window-buffer-names'."
4895 :type '(repeat (regexp :format "%v"))
4896 :group 'windows)
4897
4898 (defun same-window-p (buffer-name)
4899 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4900 This function returns non-nil if `display-buffer' or
4901 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4902 selected rather than (as usual) some other window. See
4903 `same-window-buffer-names' and `same-window-regexps'."
4904 (cond
4905 ((not (stringp buffer-name)))
4906 ;; The elements of `same-window-buffer-names' can be buffer
4907 ;; names or cons cells whose cars are buffer names.
4908 ((member buffer-name same-window-buffer-names))
4909 ((assoc buffer-name same-window-buffer-names))
4910 ((catch 'found
4911 (dolist (regexp same-window-regexps)
4912 ;; The elements of `same-window-regexps' can be regexps
4913 ;; or cons cells whose cars are regexps.
4914 (when (or (and (stringp regexp)
4915 (string-match-p regexp buffer-name))
4916 (and (consp regexp) (stringp (car regexp))
4917 (string-match-p (car regexp) buffer-name)))
4918 (throw 'found t)))))))
4919
4920 (defcustom pop-up-frames nil
4921 "Whether `display-buffer' should make a separate frame.
4922 If nil, never make a separate frame.
4923 If the value is `graphic-only', make a separate frame
4924 on graphic displays only.
4925 Any other non-nil value means always make a separate frame."
4926 :type '(choice
4927 (const :tag "Never" nil)
4928 (const :tag "On graphic displays only" graphic-only)
4929 (const :tag "Always" t))
4930 :group 'windows)
4931
4932 (defcustom display-buffer-reuse-frames nil
4933 "Non-nil means `display-buffer' should reuse frames.
4934 If the buffer in question is already displayed in a frame, raise
4935 that frame."
4936 :type 'boolean
4937 :version "21.1"
4938 :group 'windows)
4939
4940 (make-obsolete-variable
4941 'display-buffer-reuse-frames
4942 "use a `reusable-frames' alist entry in `display-buffer-alist'."
4943 "24.3")
4944
4945 (defcustom pop-up-windows t
4946 "Non-nil means `display-buffer' should make a new window."
4947 :type 'boolean
4948 :group 'windows)
4949
4950 (defcustom split-window-preferred-function 'split-window-sensibly
4951 "Function called by `display-buffer' routines to split a window.
4952 This function is called with a window as single argument and is
4953 supposed to split that window and return the new window. If the
4954 window can (or shall) not be split, it is supposed to return nil.
4955 The default is to call the function `split-window-sensibly' which
4956 tries to split the window in a way which seems most suitable.
4957 You can customize the options `split-height-threshold' and/or
4958 `split-width-threshold' in order to have `split-window-sensibly'
4959 prefer either vertical or horizontal splitting.
4960
4961 If you set this to any other function, bear in mind that the
4962 `display-buffer' routines may call this function two times. The
4963 argument of the first call is the largest window on its frame.
4964 If that call fails to return a live window, the function is
4965 called again with the least recently used window as argument. If
4966 that call fails too, `display-buffer' will use an existing window
4967 to display its buffer.
4968
4969 The window selected at the time `display-buffer' was invoked is
4970 still selected when this function is called. Hence you can
4971 compare the window argument with the value of `selected-window'
4972 if you intend to split the selected window instead or if you do
4973 not want to split the selected window."
4974 :type 'function
4975 :version "23.1"
4976 :group 'windows)
4977
4978 (defcustom split-height-threshold 80
4979 "Minimum height for splitting windows sensibly.
4980 If this is an integer, `split-window-sensibly' may split a window
4981 vertically only if it has at least this many lines. If this is
4982 nil, `split-window-sensibly' is not allowed to split a window
4983 vertically. If, however, a window is the only window on its
4984 frame, `split-window-sensibly' may split it vertically
4985 disregarding the value of this variable."
4986 :type '(choice (const nil) (integer :tag "lines"))
4987 :version "23.1"
4988 :group 'windows)
4989
4990 (defcustom split-width-threshold 160
4991 "Minimum width for splitting windows sensibly.
4992 If this is an integer, `split-window-sensibly' may split a window
4993 horizontally only if it has at least this many columns. If this
4994 is nil, `split-window-sensibly' is not allowed to split a window
4995 horizontally."
4996 :type '(choice (const nil) (integer :tag "columns"))
4997 :version "23.1"
4998 :group 'windows)
4999
5000 (defun window-splittable-p (window &optional horizontal)
5001 "Return non-nil if `split-window-sensibly' may split WINDOW.
5002 Optional argument HORIZONTAL nil or omitted means check whether
5003 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5004 non-nil means check whether WINDOW may be split horizontally.
5005
5006 WINDOW may be split vertically when the following conditions
5007 hold:
5008 - `window-size-fixed' is either nil or equals `width' for the
5009 buffer of WINDOW.
5010 - `split-height-threshold' is an integer and WINDOW is at least as
5011 high as `split-height-threshold'.
5012 - When WINDOW is split evenly, the emanating windows are at least
5013 `window-min-height' lines tall and can accommodate at least one
5014 line plus - if WINDOW has one - a mode line.
5015
5016 WINDOW may be split horizontally when the following conditions
5017 hold:
5018 - `window-size-fixed' is either nil or equals `height' for the
5019 buffer of WINDOW.
5020 - `split-width-threshold' is an integer and WINDOW is at least as
5021 wide as `split-width-threshold'.
5022 - When WINDOW is split evenly, the emanating windows are at least
5023 `window-min-width' or two (whichever is larger) columns wide."
5024 (when (window-live-p window)
5025 (with-current-buffer (window-buffer window)
5026 (if horizontal
5027 ;; A window can be split horizontally when its width is not
5028 ;; fixed, it is at least `split-width-threshold' columns wide
5029 ;; and at least twice as wide as `window-min-width' and 2 (the
5030 ;; latter value is hardcoded).
5031 (and (memq window-size-fixed '(nil height))
5032 ;; Testing `window-full-width-p' here hardly makes any
5033 ;; sense nowadays. This can be done more intuitively by
5034 ;; setting up `split-width-threshold' appropriately.
5035 (numberp split-width-threshold)
5036 (>= (window-width window)
5037 (max split-width-threshold
5038 (* 2 (max window-min-width 2)))))
5039 ;; A window can be split vertically when its height is not
5040 ;; fixed, it is at least `split-height-threshold' lines high,
5041 ;; and it is at least twice as high as `window-min-height' and 2
5042 ;; if it has a mode line or 1.
5043 (and (memq window-size-fixed '(nil width))
5044 (numberp split-height-threshold)
5045 (>= (window-height window)
5046 (max split-height-threshold
5047 (* 2 (max window-min-height
5048 (if mode-line-format 2 1))))))))))
5049
5050 (defun split-window-sensibly (&optional window)
5051 "Split WINDOW in a way suitable for `display-buffer'.
5052 WINDOW defaults to the currently selected window.
5053 If `split-height-threshold' specifies an integer, WINDOW is at
5054 least `split-height-threshold' lines tall and can be split
5055 vertically, split WINDOW into two windows one above the other and
5056 return the lower window. Otherwise, if `split-width-threshold'
5057 specifies an integer, WINDOW is at least `split-width-threshold'
5058 columns wide and can be split horizontally, split WINDOW into two
5059 windows side by side and return the window on the right. If this
5060 can't be done either and WINDOW is the only window on its frame,
5061 try to split WINDOW vertically disregarding any value specified
5062 by `split-height-threshold'. If that succeeds, return the lower
5063 window. Return nil otherwise.
5064
5065 By default `display-buffer' routines call this function to split
5066 the largest or least recently used window. To change the default
5067 customize the option `split-window-preferred-function'.
5068
5069 You can enforce this function to not split WINDOW horizontally,
5070 by setting (or binding) the variable `split-width-threshold' to
5071 nil. If, in addition, you set `split-height-threshold' to zero,
5072 chances increase that this function does split WINDOW vertically.
5073
5074 In order to not split WINDOW vertically, set (or bind) the
5075 variable `split-height-threshold' to nil. Additionally, you can
5076 set `split-width-threshold' to zero to make a horizontal split
5077 more likely to occur.
5078
5079 Have a look at the function `window-splittable-p' if you want to
5080 know how `split-window-sensibly' determines whether WINDOW can be
5081 split."
5082 (let ((window (or window (selected-window))))
5083 (or (and (window-splittable-p window)
5084 ;; Split window vertically.
5085 (with-selected-window window
5086 (split-window-below)))
5087 (and (window-splittable-p window t)
5088 ;; Split window horizontally.
5089 (with-selected-window window
5090 (split-window-right)))
5091 (and (eq window (frame-root-window (window-frame window)))
5092 (not (window-minibuffer-p window))
5093 ;; If WINDOW is the only window on its frame and is not the
5094 ;; minibuffer window, try to split it vertically disregarding
5095 ;; the value of `split-height-threshold'.
5096 (let ((split-height-threshold 0))
5097 (when (window-splittable-p window)
5098 (with-selected-window window
5099 (split-window-below))))))))
5100
5101 (defun window--try-to-split-window (window &optional alist)
5102 "Try to split WINDOW.
5103 Return value returned by `split-window-preferred-function' if it
5104 represents a live window, nil otherwise."
5105 (and (window-live-p window)
5106 (not (frame-parameter (window-frame window) 'unsplittable))
5107 (let* ((window-combination-limit
5108 ;; When `window-combination-limit' equals
5109 ;; `display-buffer' or equals `resize-window' and a
5110 ;; `window-height' or `window-width' alist entry are
5111 ;; present, bind it to t so resizing steals space
5112 ;; preferably from the window that was split.
5113 (if (or (eq window-combination-limit 'display-buffer)
5114 (and (eq window-combination-limit 'window-size)
5115 (or (cdr (assq 'window-height alist))
5116 (cdr (assq 'window-width alist)))))
5117 t
5118 window-combination-limit))
5119 (new-window
5120 ;; Since `split-window-preferred-function' might
5121 ;; throw an error use `condition-case'.
5122 (condition-case nil
5123 (funcall split-window-preferred-function window)
5124 (error nil))))
5125 (and (window-live-p new-window) new-window))))
5126
5127 (defun window--frame-usable-p (frame)
5128 "Return FRAME if it can be used to display a buffer."
5129 (when (frame-live-p frame)
5130 (let ((window (frame-root-window frame)))
5131 ;; `frame-root-window' may be an internal window which is considered
5132 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5133 ;; implicitly know that `frame' has a visible window we can use.
5134 (unless (and (window-live-p window)
5135 (or (window-minibuffer-p window)
5136 ;; If the window is soft-dedicated, the frame is usable.
5137 ;; Actually, even if the window is really dedicated,
5138 ;; the frame is still usable by splitting it.
5139 ;; At least Emacs-22 allowed it, and it is desirable
5140 ;; when displaying same-frame windows.
5141 nil ; (eq t (window-dedicated-p window))
5142 ))
5143 frame))))
5144
5145 (defcustom even-window-heights t
5146 "If non-nil `display-buffer' will try to even window heights.
5147 Otherwise `display-buffer' will leave the window configuration
5148 alone. Heights are evened only when `display-buffer' chooses a
5149 window that appears above or below the selected window."
5150 :type 'boolean
5151 :group 'windows)
5152
5153 (defun window--even-window-heights (window)
5154 "Even heights of WINDOW and selected window.
5155 Do this only if these windows are vertically adjacent to each
5156 other, `even-window-heights' is non-nil, and the selected window
5157 is higher than WINDOW."
5158 (when (and even-window-heights
5159 ;; Even iff WINDOW forms a vertical combination with the
5160 ;; selected window, and WINDOW's height exceeds that of the
5161 ;; selected window, see also bug#11880.
5162 (window-combined-p window)
5163 (= (window-child-count (window-parent window)) 2)
5164 (eq (window-parent) (window-parent window))
5165 (> (window-total-height) (window-total-height window)))
5166 ;; Don't throw an error if we can't even window heights for
5167 ;; whatever reason.
5168 (condition-case nil
5169 (enlarge-window
5170 (/ (- (window-total-height window) (window-total-height)) 2))
5171 (error nil))))
5172
5173 (defun window--display-buffer (buffer window type &optional alist dedicated)
5174 "Display BUFFER in WINDOW and make its frame visible.
5175 TYPE must be one of the symbols `reuse', `window' or `frame' and
5176 is passed unaltered to `display-buffer-record-window'. Set
5177 `window-dedicated-p' to DEDICATED if non-nil. Return WINDOW if
5178 BUFFER and WINDOW are live."
5179 (when (and (buffer-live-p buffer) (window-live-p window))
5180 (display-buffer-record-window type window buffer)
5181 (unless (eq buffer (window-buffer window))
5182 (set-window-dedicated-p window nil)
5183 (set-window-buffer window buffer)
5184 (when dedicated
5185 (set-window-dedicated-p window dedicated))
5186 (when (memq type '(window frame))
5187 (set-window-prev-buffers window nil)))
5188 (let ((parameter (window-parameter window 'quit-restore))
5189 (height (cdr (assq 'window-height alist)))
5190 (width (cdr (assq 'window-width alist))))
5191 (when (or (memq type '(window frame))
5192 (and (eq (car parameter) 'same)
5193 (memq (nth 1 parameter) '(window frame))))
5194 ;; Adjust height of new window or frame.
5195 (cond
5196 ((not height))
5197 ((numberp height)
5198 (let* ((new-height
5199 (if (integerp height)
5200 height
5201 (round
5202 (* (window-total-size (frame-root-window window))
5203 height))))
5204 (delta (- new-height (window-total-size window))))
5205 (cond
5206 ((and (window--resizable-p window delta nil 'safe)
5207 (window-combined-p window))
5208 (window-resize window delta nil 'safe))
5209 ((or (eq type 'frame)
5210 (and (eq (car parameter) 'same)
5211 (eq (nth 1 parameter) 'frame)))
5212 (set-frame-height
5213 (window-frame window)
5214 (+ (frame-height (window-frame window)) delta))))))
5215 ((functionp height)
5216 (ignore-errors (funcall height window))))
5217 ;; Adjust width of a window or frame.
5218 (cond
5219 ((not width))
5220 ((numberp width)
5221 (let* ((new-width
5222 (if (integerp width)
5223 width
5224 (round
5225 (* (window-total-size (frame-root-window window) t)
5226 width))))
5227 (delta (- new-width (window-total-size window t))))
5228 (cond
5229 ((and (window--resizable-p window delta t 'safe)
5230 (window-combined-p window t))
5231 (window-resize window delta t 'safe))
5232 ((or (eq type 'frame)
5233 (and (eq (car parameter) 'same)
5234 (eq (nth 1 parameter) 'frame)))
5235 (set-frame-width
5236 (window-frame window)
5237 (+ (frame-width (window-frame window)) delta))))))
5238 ((functionp width)
5239 (ignore-errors (funcall width window))))))
5240 window))
5241
5242 (defun window--maybe-raise-frame (frame)
5243 (let ((visible (frame-visible-p frame)))
5244 (unless (or (not visible)
5245 ;; Assume the selected frame is already visible enough.
5246 (eq frame (selected-frame))
5247 ;; Assume the frame from which we invoked the
5248 ;; minibuffer is visible.
5249 (and (minibuffer-window-active-p (selected-window))
5250 (eq frame (window-frame (minibuffer-selected-window)))))
5251 (raise-frame frame))))
5252
5253 ;; FIXME: Not implemented.
5254 ;; FIXME: By the way, there could be more levels of dedication:
5255 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5256 ;; the window hasn't been used for something else yet.
5257 ;; - `softly' dedicated only allows reuse when asked explicitly.
5258 ;; - `strongly' never allows reuse.
5259 (defvar display-buffer-mark-dedicated nil
5260 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5261 The actual non-nil value of this variable will be copied to the
5262 `window-dedicated-p' flag.")
5263
5264 (defconst display-buffer--action-function-custom-type
5265 '(choice :tag "Function"
5266 (const :tag "--" ignore) ; default for insertion
5267 (const display-buffer-reuse-window)
5268 (const display-buffer-pop-up-window)
5269 (const display-buffer-same-window)
5270 (const display-buffer-pop-up-frame)
5271 (const display-buffer-use-some-window)
5272 (function :tag "Other function"))
5273 "Custom type for `display-buffer' action functions.")
5274
5275 (defconst display-buffer--action-custom-type
5276 `(cons :tag "Action"
5277 (choice :tag "Action functions"
5278 ,display-buffer--action-function-custom-type
5279 (repeat
5280 :tag "List of functions"
5281 ,display-buffer--action-function-custom-type))
5282 (alist :tag "Action arguments"
5283 :key-type symbol
5284 :value-type (sexp :tag "Value")))
5285 "Custom type for `display-buffer' actions.")
5286
5287 (defvar display-buffer-overriding-action '(nil . nil)
5288 "Overriding action to perform to display a buffer.
5289 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5290 function or a list of functions. Each function should accept two
5291 arguments: a buffer to display and an alist similar to ALIST.
5292 See `display-buffer' for details.")
5293 (put 'display-buffer-overriding-action 'risky-local-variable t)
5294
5295 (defcustom display-buffer-alist nil
5296 "Alist of conditional actions for `display-buffer'.
5297 This is a list of elements (CONDITION . ACTION), where:
5298
5299 CONDITION is either a regexp matching buffer names, or a function
5300 that takes a buffer and returns a boolean.
5301
5302 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5303 function or a list of functions. Each such function should
5304 accept two arguments: a buffer to display and an alist of the
5305 same form as ALIST. See `display-buffer' for details."
5306 :type `(alist :key-type
5307 (choice :tag "Condition"
5308 regexp
5309 (function :tag "Matcher function"))
5310 :value-type ,display-buffer--action-custom-type)
5311 :risky t
5312 :version "24.1"
5313 :group 'windows)
5314
5315 (defcustom display-buffer-base-action '(nil . nil)
5316 "User-specified default action for `display-buffer'.
5317 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5318 function or a list of functions. Each function should accept two
5319 arguments: a buffer to display and an alist similar to ALIST.
5320 See `display-buffer' for details."
5321 :type display-buffer--action-custom-type
5322 :risky t
5323 :version "24.1"
5324 :group 'windows)
5325
5326 (defconst display-buffer-fallback-action
5327 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
5328 display-buffer-reuse-window
5329 display-buffer--maybe-pop-up-frame-or-window
5330 display-buffer-use-some-window
5331 ;; If all else fails, pop up a new frame.
5332 display-buffer-pop-up-frame))
5333 "Default fallback action for `display-buffer'.
5334 This is the action used by `display-buffer' if no other actions
5335 specified, e.g. by the user options `display-buffer-alist' or
5336 `display-buffer-base-action'. See `display-buffer'.")
5337 (put 'display-buffer-fallback-action 'risky-local-variable t)
5338
5339 (defun display-buffer-assq-regexp (buffer-name alist)
5340 "Retrieve ALIST entry corresponding to BUFFER-NAME."
5341 (catch 'match
5342 (dolist (entry alist)
5343 (let ((key (car entry)))
5344 (when (or (and (stringp key)
5345 (string-match-p key buffer-name))
5346 (and (symbolp key) (functionp key)
5347 (funcall key buffer-name alist)))
5348 (throw 'match (cdr entry)))))))
5349
5350 (defvar display-buffer--same-window-action
5351 '(display-buffer-same-window
5352 (inhibit-same-window . nil))
5353 "A `display-buffer' action for displaying in the same window.")
5354 (put 'display-buffer--same-window-action 'risky-local-variable t)
5355
5356 (defvar display-buffer--other-frame-action
5357 '((display-buffer-reuse-window
5358 display-buffer-pop-up-frame)
5359 (reusable-frames . 0)
5360 (inhibit-same-window . t))
5361 "A `display-buffer' action for displaying in another frame.")
5362 (put 'display-buffer--other-frame-action 'risky-local-variable t)
5363
5364 (defun display-buffer (buffer-or-name &optional action frame)
5365 "Display BUFFER-OR-NAME in some window, without selecting it.
5366 BUFFER-OR-NAME must be a buffer or the name of an existing
5367 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5368 or nil if no such window is found.
5369
5370 Optional argument ACTION, if non-nil, should specify a display
5371 action. Its form is described below.
5372
5373 Optional argument FRAME, if non-nil, acts like an additional
5374 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
5375 specifying the frame(s) to search for a window that is already
5376 displaying the buffer. See `display-buffer-reuse-window'
5377
5378 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
5379 where FUNCTION is either a function or a list of functions, and
5380 ALIST is an arbitrary association list (alist).
5381
5382 Each such FUNCTION should accept two arguments: the buffer to
5383 display and an alist. Based on those arguments, it should either
5384 display the buffer and return the window, or return nil if unable
5385 to display the buffer.
5386
5387 The `display-buffer' function builds a function list and an alist
5388 by combining the functions and alists specified in
5389 `display-buffer-overriding-action', `display-buffer-alist', the
5390 ACTION argument, `display-buffer-base-action', and
5391 `display-buffer-fallback-action' (in order). Then it calls each
5392 function in the combined function list in turn, passing the
5393 buffer as the first argument and the combined alist as the second
5394 argument, until one of the functions returns non-nil.
5395
5396 If ACTION is nil, the function list and the alist are built using
5397 only the other variables mentioned above.
5398
5399 Available action functions include:
5400 `display-buffer-same-window'
5401 `display-buffer-reuse-window'
5402 `display-buffer-pop-up-frame'
5403 `display-buffer-pop-up-window'
5404 `display-buffer-use-some-window'
5405
5406 Recognized alist entries include:
5407
5408 `inhibit-same-window' -- A non-nil value prevents the same
5409 window from being used for display.
5410
5411 `inhibit-switch-frame' -- A non-nil value prevents any other
5412 frame from being raised or selected,
5413 even if the window is displayed there.
5414
5415 `reusable-frames' -- Value specifies frame(s) to search for a
5416 window that already displays the buffer.
5417 See `display-buffer-reuse-window'.
5418
5419 `pop-up-frame-parameters' -- Value specifies an alist of frame
5420 parameters to give a new frame, if
5421 one is created.
5422
5423 `window-height' -- Value specifies either an integer (the number
5424 of lines of a new window), a floating point number (the
5425 fraction of a new window with respect to the height of the
5426 frame's root window) or a function to be called with one
5427 argument - a new window. The function is supposed to adjust
5428 the height of the window; its return value is ignored.
5429 Suitable functions are `shrink-window-if-larger-than-buffer'
5430 and `fit-window-to-buffer'.
5431
5432 `window-width' -- Value specifies either an integer (the number
5433 of columns of a new window), a floating point number (the
5434 fraction of a new window with respect to the width of the
5435 frame's root window) or a function to be called with one
5436 argument - a new window. The function is supposed to adjust
5437 the width of the window; its return value is ignored.
5438
5439 The ACTION argument to `display-buffer' can also have a non-nil
5440 and non-list value. This means to display the buffer in a window
5441 other than the selected one, even if it is already displayed in
5442 the selected window. If called interactively with a prefix
5443 argument, ACTION is t."
5444 (interactive (list (read-buffer "Display buffer: " (other-buffer))
5445 (if current-prefix-arg t)))
5446 (let ((buffer (if (bufferp buffer-or-name)
5447 buffer-or-name
5448 (get-buffer buffer-or-name)))
5449 ;; Handle the old form of the first argument.
5450 (inhibit-same-window (and action (not (listp action)))))
5451 (unless (listp action) (setq action nil))
5452 (if display-buffer-function
5453 ;; If `display-buffer-function' is defined, let it do the job.
5454 (funcall display-buffer-function buffer inhibit-same-window)
5455 ;; Otherwise, use the defined actions.
5456 (let* ((user-action
5457 (display-buffer-assq-regexp (buffer-name buffer)
5458 display-buffer-alist))
5459 (special-action (display-buffer--special-action buffer))
5460 ;; Extra actions from the arguments to this function:
5461 (extra-action
5462 (cons nil (append (if inhibit-same-window
5463 '((inhibit-same-window . t)))
5464 (if frame
5465 `((reusable-frames . ,frame))))))
5466 ;; Construct action function list and action alist.
5467 (actions (list display-buffer-overriding-action
5468 user-action special-action action extra-action
5469 display-buffer-base-action
5470 display-buffer-fallback-action))
5471 (functions (apply 'append
5472 (mapcar (lambda (x)
5473 (setq x (car x))
5474 (if (functionp x) (list x) x))
5475 actions)))
5476 (alist (apply 'append (mapcar 'cdr actions)))
5477 window)
5478 (unless (buffer-live-p buffer)
5479 (error "Invalid buffer"))
5480 (while (and functions (not window))
5481 (setq window (funcall (car functions) buffer alist)
5482 functions (cdr functions)))
5483 window))))
5484
5485 (defun display-buffer-other-frame (buffer)
5486 "Display buffer BUFFER in another frame.
5487 This uses the function `display-buffer' as a subroutine; see
5488 its documentation for additional customization information."
5489 (interactive "BDisplay buffer in other frame: ")
5490 (display-buffer buffer display-buffer--other-frame-action t))
5491
5492 ;;; `display-buffer' action functions:
5493
5494 (defun display-buffer-same-window (buffer alist)
5495 "Display BUFFER in the selected window.
5496 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
5497 if the selected window is a minibuffer window or is dedicated to
5498 another buffer; in that case, return nil. Otherwise, return the
5499 selected window."
5500 (unless (or (cdr (assq 'inhibit-same-window alist))
5501 (window-minibuffer-p)
5502 (window-dedicated-p))
5503 (window--display-buffer buffer (selected-window) 'reuse alist)))
5504
5505 (defun display-buffer--maybe-same-window (buffer alist)
5506 "Conditionally display BUFFER in the selected window.
5507 If `same-window-p' returns non-nil for BUFFER's name, call
5508 `display-buffer-same-window' and return its value. Otherwise,
5509 return nil."
5510 (and (same-window-p (buffer-name buffer))
5511 (display-buffer-same-window buffer alist)))
5512
5513 (defun display-buffer-reuse-window (buffer alist)
5514 "Return a window that is already displaying BUFFER.
5515 Return nil if no usable window is found.
5516
5517 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5518 window is not eligible for reuse.
5519
5520 If ALIST contains a `reusable-frames' entry, its value determines
5521 which frames to search for a reusable window:
5522 nil -- the selected frame (actually the last non-minibuffer frame)
5523 A frame -- just that frame
5524 `visible' -- all visible frames
5525 0 -- all frames on the current terminal
5526 t -- all frames.
5527
5528 If ALIST contains no `reusable-frames' entry, search just the
5529 selected frame if `display-buffer-reuse-frames' and
5530 `pop-up-frames' are both nil; search all frames on the current
5531 terminal if either of those variables is non-nil.
5532
5533 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5534 event that a window on another frame is chosen, avoid raising
5535 that frame."
5536 (let* ((alist-entry (assq 'reusable-frames alist))
5537 (frames (cond (alist-entry (cdr alist-entry))
5538 ((if (eq pop-up-frames 'graphic-only)
5539 (display-graphic-p)
5540 pop-up-frames)
5541 0)
5542 (display-buffer-reuse-frames 0)
5543 (t (last-nonminibuffer-frame))))
5544 (window (if (and (eq buffer (window-buffer))
5545 (not (cdr (assq 'inhibit-same-window alist))))
5546 (selected-window)
5547 (car (delq (selected-window)
5548 (get-buffer-window-list buffer 'nomini
5549 frames))))))
5550 (when (window-live-p window)
5551 (prog1 (window--display-buffer buffer window 'reuse alist)
5552 (unless (cdr (assq 'inhibit-switch-frame alist))
5553 (window--maybe-raise-frame (window-frame window)))))))
5554
5555 (defun display-buffer--special-action (buffer)
5556 "Return special display action for BUFFER, if any.
5557 If `special-display-p' returns non-nil for BUFFER, return an
5558 appropriate display action involving `special-display-function'.
5559 See `display-buffer' for the format of display actions."
5560 (and special-display-function
5561 ;; `special-display-p' returns either t or a list of frame
5562 ;; parameters to pass to `special-display-function'.
5563 (let ((pars (special-display-p (buffer-name buffer))))
5564 (when pars
5565 (list (list #'display-buffer-reuse-window
5566 `(lambda (buffer _alist)
5567 (funcall special-display-function
5568 buffer ',(if (listp pars) pars)))))))))
5569
5570 (defun display-buffer-pop-up-frame (buffer alist)
5571 "Display BUFFER in a new frame.
5572 This works by calling `pop-up-frame-function'. If successful,
5573 return the window used; otherwise return nil.
5574
5575 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
5576 raising the new frame.
5577
5578 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
5579 corresponding value is an alist of frame parameters to give the
5580 new frame."
5581 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
5582 (pop-up-frame-alist (append params pop-up-frame-alist))
5583 (fun pop-up-frame-function)
5584 frame window)
5585 (when (and fun
5586 (setq frame (funcall fun))
5587 (setq window (frame-selected-window frame)))
5588 (prog1 (window--display-buffer
5589 buffer window 'frame alist display-buffer-mark-dedicated)
5590 (unless (cdr (assq 'inhibit-switch-frame alist))
5591 (window--maybe-raise-frame frame))))))
5592
5593 (defun display-buffer-pop-up-window (buffer alist)
5594 "Display BUFFER by popping up a new window.
5595 The new window is created on the selected frame, or in
5596 `last-nonminibuffer-frame' if no windows can be created there.
5597 If successful, return the new window; otherwise return nil.
5598
5599 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5600 event that the new window is created on another frame, avoid
5601 raising the frame."
5602 (let ((frame (or (window--frame-usable-p (selected-frame))
5603 (window--frame-usable-p (last-nonminibuffer-frame))))
5604 window)
5605 (when (and (or (not (frame-parameter frame 'unsplittable))
5606 ;; If the selected frame cannot be split, look at
5607 ;; `last-nonminibuffer-frame'.
5608 (and (eq frame (selected-frame))
5609 (setq frame (last-nonminibuffer-frame))
5610 (window--frame-usable-p frame)
5611 (not (frame-parameter frame 'unsplittable))))
5612 ;; Attempt to split largest or least recently used window.
5613 (setq window (or (window--try-to-split-window
5614 (get-largest-window frame t) alist)
5615 (window--try-to-split-window
5616 (get-lru-window frame t) alist))))
5617 (prog1 (window--display-buffer
5618 buffer window 'window alist display-buffer-mark-dedicated)
5619 (unless (cdr (assq 'inhibit-switch-frame alist))
5620 (window--maybe-raise-frame (window-frame window)))))))
5621
5622 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
5623 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
5624
5625 If `pop-up-frames' is non-nil (and not `graphic-only' on a
5626 text-only terminal), try with `display-buffer-pop-up-frame'.
5627
5628 If that cannot be done, and `pop-up-windows' is non-nil, try
5629 again with `display-buffer-pop-up-window'."
5630 (or (and (if (eq pop-up-frames 'graphic-only)
5631 (display-graphic-p)
5632 pop-up-frames)
5633 (display-buffer-pop-up-frame buffer alist))
5634 (and pop-up-windows
5635 (display-buffer-pop-up-window buffer alist))))
5636
5637 (defun display-buffer-below-selected (buffer alist)
5638 "Try displaying BUFFER in a window below the selected window.
5639 This either splits the selected window or reuses the window below
5640 the selected one."
5641 (let (window)
5642 (or (and (not (frame-parameter nil 'unsplittable))
5643 (setq window (window--try-to-split-window (selected-window) alist))
5644 (window--display-buffer
5645 buffer window 'window alist display-buffer-mark-dedicated))
5646 (and (setq window (window-in-direction 'below))
5647 (not (window-dedicated-p window))
5648 (window--display-buffer
5649 buffer window 'reuse alist display-buffer-mark-dedicated)))))
5650
5651 (defun display-buffer-at-bottom (buffer alist)
5652 "Try displaying BUFFER in a window at the bottom of the selected frame.
5653 This either splits the window at the bottom of the frame or the
5654 frame's root window, or reuses an existing window at the bottom
5655 of the selected frame."
5656 (let (bottom-window window)
5657 (walk-window-tree (lambda (window) (setq bottom-window window)))
5658 (or (and (not (frame-parameter nil 'unsplittable))
5659 (setq window (window--try-to-split-window bottom-window alist))
5660 (window--display-buffer
5661 buffer window 'window alist display-buffer-mark-dedicated))
5662 (and (not (frame-parameter nil 'unsplittable))
5663 (setq window
5664 (condition-case nil
5665 (split-window (frame-root-window))
5666 (error nil)))
5667 (window--display-buffer
5668 buffer window 'window alist display-buffer-mark-dedicated))
5669 (and (setq window bottom-window)
5670 (not (window-dedicated-p window))
5671 (window--display-buffer
5672 buffer window 'reuse alist display-buffer-mark-dedicated)))))
5673
5674 (defun display-buffer-in-previous-window (buffer alist)
5675 "Display BUFFER in a window previously showing it.
5676 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5677 window is not eligible for reuse.
5678
5679 If ALIST contains a `reusable-frames' entry, its value determines
5680 which frames to search for a reusable window:
5681 nil -- the selected frame (actually the last non-minibuffer frame)
5682 A frame -- just that frame
5683 `visible' -- all visible frames
5684 0 -- all frames on the current terminal
5685 t -- all frames.
5686
5687 If ALIST contains no `reusable-frames' entry, search just the
5688 selected frame if `display-buffer-reuse-frames' and
5689 `pop-up-frames' are both nil; search all frames on the current
5690 terminal if either of those variables is non-nil.
5691
5692 If ALIST has a `previous-window' entry, the window specified by
5693 that entry will override any other window found by the methods
5694 above, even if that window never showed BUFFER before."
5695 (let* ((alist-entry (assq 'reusable-frames alist))
5696 (inhibit-same-window
5697 (cdr (assq 'inhibit-same-window alist)))
5698 (frames (cond
5699 (alist-entry (cdr alist-entry))
5700 ((if (eq pop-up-frames 'graphic-only)
5701 (display-graphic-p)
5702 pop-up-frames)
5703 0)
5704 (display-buffer-reuse-frames 0)
5705 (t (last-nonminibuffer-frame))))
5706 entry best-window second-best-window window)
5707 ;; Scan windows whether they have shown the buffer recently.
5708 (catch 'best
5709 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
5710 (when (and (assq buffer (window-prev-buffers window))
5711 (not (window-dedicated-p window)))
5712 (if (eq window (selected-window))
5713 (unless inhibit-same-window
5714 (setq second-best-window window))
5715 (setq best-window window)
5716 (throw 'best t)))))
5717 ;; When ALIST has a `previous-window' entry, that entry may override
5718 ;; anything we found so far.
5719 (when (and (setq window (cdr (assq 'previous-window alist)))
5720 (window-live-p window)
5721 (not (window-dedicated-p window)))
5722 (if (eq window (selected-window))
5723 (unless inhibit-same-window
5724 (setq second-best-window window))
5725 (setq best-window window)))
5726 ;; Return best or second best window found.
5727 (when (setq window (or best-window second-best-window))
5728 (window--display-buffer buffer window 'reuse alist))))
5729
5730 (defun display-buffer-use-some-window (buffer alist)
5731 "Display BUFFER in an existing window.
5732 Search for a usable window, set that window to the buffer, and
5733 return the window. If no suitable window is found, return nil.
5734
5735 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5736 event that a window in another frame is chosen, avoid raising
5737 that frame."
5738 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
5739 (frame (or (window--frame-usable-p (selected-frame))
5740 (window--frame-usable-p (last-nonminibuffer-frame))))
5741 (window
5742 ;; Reuse an existing window.
5743 (or (get-lru-window frame nil not-this-window)
5744 (let ((window (get-buffer-window buffer 'visible)))
5745 (unless (and not-this-window
5746 (eq window (selected-window)))
5747 window))
5748 (get-largest-window 'visible nil not-this-window)
5749 (let ((window (get-buffer-window buffer 0)))
5750 (unless (and not-this-window
5751 (eq window (selected-window)))
5752 window))
5753 (get-largest-window 0 not-this-window))))
5754 (when (window-live-p window)
5755 (prog1
5756 (window--display-buffer buffer window 'reuse alist)
5757 (window--even-window-heights window)
5758 (unless (cdr (assq 'inhibit-switch-frame alist))
5759 (window--maybe-raise-frame (window-frame window)))))))
5760
5761 ;;; Display + selection commands:
5762 (defun pop-to-buffer (buffer &optional action norecord)
5763 "Select buffer BUFFER in some window, preferably a different one.
5764 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5765 is a string not naming an existent buffer, create a buffer with
5766 that name. If BUFFER is nil, choose some other buffer. Return
5767 the buffer.
5768
5769 This uses `display-buffer' as a subroutine. The optional ACTION
5770 argument is passed to `display-buffer' as its ACTION argument.
5771 See `display-buffer' for more information. ACTION is t if called
5772 interactively with a prefix argument, which means to pop to a
5773 window other than the selected one even if the buffer is already
5774 displayed in the selected window.
5775
5776 If the window to show BUFFER is not on the selected
5777 frame, raise that window's frame and give it input focus.
5778
5779 Optional third arg NORECORD non-nil means do not put this buffer
5780 at the front of the list of recently selected ones."
5781 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
5782 (if current-prefix-arg t)))
5783 (setq buffer (window-normalize-buffer-to-switch-to buffer))
5784 (set-buffer buffer)
5785 (let* ((old-frame (selected-frame))
5786 (window (display-buffer buffer action))
5787 (frame (window-frame window)))
5788 ;; If we chose another frame, make sure it gets input focus.
5789 (unless (eq frame old-frame)
5790 (select-frame-set-input-focus frame norecord))
5791 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
5792 (select-window window norecord)
5793 buffer))
5794
5795 (defun pop-to-buffer-same-window (buffer &optional norecord)
5796 "Select buffer BUFFER in some window, preferably the same one.
5797 This function behaves much like `switch-to-buffer', except it
5798 displays with `special-display-function' if BUFFER has a match in
5799 `special-display-buffer-names' or `special-display-regexps'.
5800
5801 Unlike `pop-to-buffer', this function prefers using the selected
5802 window over popping up a new window or frame.
5803
5804 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5805 is a string not naming an existent buffer, create a buffer with
5806 that name. If BUFFER is nil, choose some other buffer. Return
5807 the buffer.
5808
5809 NORECORD, if non-nil means do not put this buffer at the front of
5810 the list of recently selected ones."
5811 (pop-to-buffer buffer display-buffer--same-window-action norecord))
5812
5813 (defun read-buffer-to-switch (prompt)
5814 "Read the name of a buffer to switch to, prompting with PROMPT.
5815 Return the name of the buffer as a string.
5816
5817 This function is intended for the `switch-to-buffer' family of
5818 commands since these need to omit the name of the current buffer
5819 from the list of completions and default values."
5820 (let ((rbts-completion-table (internal-complete-buffer-except)))
5821 (minibuffer-with-setup-hook
5822 (lambda ()
5823 (setq minibuffer-completion-table rbts-completion-table)
5824 ;; Since rbts-completion-table is built dynamically, we
5825 ;; can't just add it to the default value of
5826 ;; icomplete-with-completion-tables, so we add it
5827 ;; here manually.
5828 (if (and (boundp 'icomplete-with-completion-tables)
5829 (listp icomplete-with-completion-tables))
5830 (set (make-local-variable 'icomplete-with-completion-tables)
5831 (cons rbts-completion-table
5832 icomplete-with-completion-tables))))
5833 (read-buffer prompt (other-buffer (current-buffer))
5834 (confirm-nonexistent-file-or-buffer)))))
5835
5836 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5837 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5838 If BUFFER-OR-NAME is nil, return the buffer returned by
5839 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5840 exists, return that buffer. If no such buffer exists, create a
5841 buffer with the name BUFFER-OR-NAME and return that buffer."
5842 (if buffer-or-name
5843 (or (get-buffer buffer-or-name)
5844 (let ((buffer (get-buffer-create buffer-or-name)))
5845 (set-buffer-major-mode buffer)
5846 buffer))
5847 (other-buffer)))
5848
5849 (defcustom switch-to-buffer-preserve-window-point nil
5850 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
5851 If this is nil, `switch-to-buffer' displays the buffer at that
5852 buffer's `point'. If this is `already-displayed', it tries to
5853 display the buffer at its previous position in the selected
5854 window, provided the buffer is currently displayed in some other
5855 window on any visible or iconified frame. If this is t, it
5856 unconditionally tries to display the buffer at its previous
5857 position in the selected window.
5858
5859 This variable is ignored if the buffer is already displayed in
5860 the selected window or never appeared in it before, or if
5861 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
5862 :type '(choice
5863 (const :tag "Never" nil)
5864 (const :tag "If already displayed elsewhere" already-displayed)
5865 (const :tag "Always" t))
5866 :group 'windows
5867 :version "24.3")
5868
5869 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
5870 "Switch to buffer BUFFER-OR-NAME in the selected window.
5871 If the selected window cannot display the specified
5872 buffer (e.g. if it is a minibuffer window or strongly dedicated
5873 to another buffer), call `pop-to-buffer' to select the buffer in
5874 another window.
5875
5876 If called interactively, read the buffer name using the
5877 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5878 determines whether to request confirmation before creating a new
5879 buffer.
5880
5881 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
5882 If BUFFER-OR-NAME is a string that does not identify an existing
5883 buffer, create a buffer with that name. If BUFFER-OR-NAME is
5884 nil, switch to the buffer returned by `other-buffer'.
5885
5886 If optional argument NORECORD is non-nil, do not put the buffer
5887 at the front of the buffer list, and do not make the window
5888 displaying it the most recently selected one.
5889
5890 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
5891 must be displayed in the selected window; if that is impossible,
5892 signal an error rather than calling `pop-to-buffer'.
5893
5894 The option `switch-to-buffer-preserve-window-point' can be used
5895 to make the buffer appear at its last position in the selected
5896 window.
5897
5898 Return the buffer switched to."
5899 (interactive
5900 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
5901 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
5902 (cond
5903 ;; Don't call set-window-buffer if it's not needed since it
5904 ;; might signal an error (e.g. if the window is dedicated).
5905 ((eq buffer (window-buffer)))
5906 ((window-minibuffer-p)
5907 (if force-same-window
5908 (user-error "Cannot switch buffers in minibuffer window")
5909 (pop-to-buffer buffer norecord)))
5910 ((eq (window-dedicated-p) t)
5911 (if force-same-window
5912 (user-error "Cannot switch buffers in a dedicated window")
5913 (pop-to-buffer buffer norecord)))
5914 (t
5915 (let* ((entry (assq buffer (window-prev-buffers)))
5916 (displayed (and (eq switch-to-buffer-preserve-window-point
5917 'already-displayed)
5918 (get-buffer-window buffer 0))))
5919 (set-window-buffer nil buffer)
5920 (when (and entry
5921 (or (eq switch-to-buffer-preserve-window-point t)
5922 displayed))
5923 ;; Try to restore start and point of buffer in the selected
5924 ;; window (Bug#4041).
5925 (set-window-start (selected-window) (nth 1 entry) t)
5926 (set-window-point nil (nth 2 entry))))))
5927
5928 (unless norecord
5929 (select-window (selected-window)))
5930 (set-buffer buffer)))
5931
5932 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5933 "Select the buffer specified by BUFFER-OR-NAME in another window.
5934 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5935 nil. Return the buffer switched to.
5936
5937 If called interactively, prompt for the buffer name using the
5938 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5939 determines whether to request confirmation before creating a new
5940 buffer.
5941
5942 If BUFFER-OR-NAME is a string and does not identify an existing
5943 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5944 nil, switch to the buffer returned by `other-buffer'.
5945
5946 Optional second argument NORECORD non-nil means do not put this
5947 buffer at the front of the list of recently selected ones.
5948
5949 This uses the function `display-buffer' as a subroutine; see its
5950 documentation for additional customization information."
5951 (interactive
5952 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5953 (let ((pop-up-windows t))
5954 (pop-to-buffer buffer-or-name t norecord)))
5955
5956 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5957 "Switch to buffer BUFFER-OR-NAME in another frame.
5958 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5959 nil. Return the buffer switched to.
5960
5961 If called interactively, prompt for the buffer name using the
5962 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5963 determines whether to request confirmation before creating a new
5964 buffer.
5965
5966 If BUFFER-OR-NAME is a string and does not identify an existing
5967 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5968 nil, switch to the buffer returned by `other-buffer'.
5969
5970 Optional second arg NORECORD non-nil means do not put this
5971 buffer at the front of the list of recently selected ones.
5972
5973 This uses the function `display-buffer' as a subroutine; see its
5974 documentation for additional customization information."
5975 (interactive
5976 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5977 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
5978 \f
5979 (defun set-window-text-height (window height)
5980 "Set the height in lines of the text display area of WINDOW to HEIGHT.
5981 WINDOW must be a live window and defaults to the selected one.
5982 HEIGHT doesn't include the mode line or header line, if any, or
5983 any partial-height lines in the text display area.
5984
5985 Note that the current implementation of this function cannot
5986 always set the height exactly, but attempts to be conservative,
5987 by allocating more lines than are actually needed in the case
5988 where some error may be present."
5989 (setq window (window-normalize-window window t))
5990 (let ((delta (- height (window-text-height window))))
5991 (unless (zerop delta)
5992 ;; Setting window-min-height to a value like 1 can lead to very
5993 ;; bizarre displays because it also allows Emacs to make *other*
5994 ;; windows one line tall, which means that there's no more space
5995 ;; for the mode line.
5996 (let ((window-min-height (min 2 height)))
5997 (window-resize window delta)))))
5998
5999 (defun enlarge-window-horizontally (delta)
6000 "Make selected window DELTA columns wider.
6001 Interactively, if no argument is given, make selected window one
6002 column wider."
6003 (interactive "p")
6004 (enlarge-window delta t))
6005
6006 (defun shrink-window-horizontally (delta)
6007 "Make selected window DELTA columns narrower.
6008 Interactively, if no argument is given, make selected window one
6009 column narrower."
6010 (interactive "p")
6011 (shrink-window delta t))
6012
6013 (defun count-screen-lines (&optional beg end count-final-newline window)
6014 "Return the number of screen lines in the region.
6015 The number of screen lines may be different from the number of actual lines,
6016 due to line breaking, display table, etc.
6017
6018 Optional arguments BEG and END default to `point-min' and `point-max'
6019 respectively.
6020
6021 If region ends with a newline, ignore it unless optional third argument
6022 COUNT-FINAL-NEWLINE is non-nil.
6023
6024 The optional fourth argument WINDOW specifies the window used for obtaining
6025 parameters such as width, horizontal scrolling, and so on. The default is
6026 to use the selected window's parameters.
6027
6028 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6029 regardless of which buffer is displayed in WINDOW. This makes possible to use
6030 `count-screen-lines' in any buffer, whether or not it is currently displayed
6031 in some window."
6032 (unless beg
6033 (setq beg (point-min)))
6034 (unless end
6035 (setq end (point-max)))
6036 (if (= beg end)
6037 0
6038 (save-excursion
6039 (save-restriction
6040 (widen)
6041 (narrow-to-region (min beg end)
6042 (if (and (not count-final-newline)
6043 (= ?\n (char-before (max beg end))))
6044 (1- (max beg end))
6045 (max beg end)))
6046 (goto-char (point-min))
6047 (1+ (vertical-motion (buffer-size) window))))))
6048
6049 (defun window-buffer-height (window)
6050 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6051 WINDOW must be a live window and defaults to the selected one."
6052 (setq window (window-normalize-window window t))
6053 (with-current-buffer (window-buffer window)
6054 (max 1
6055 (count-screen-lines (point-min) (point-max)
6056 ;; If buffer ends with a newline, ignore it when
6057 ;; counting height unless point is after it.
6058 (eobp)
6059 window))))
6060
6061 ;;; Resizing buffers to fit their contents exactly.
6062 (defcustom fit-frame-to-buffer nil
6063 "Non-nil means `fit-window-to-buffer' can resize frames.
6064 A frame can be resized if and only if its root window is a live
6065 window. The height of the root window is subject to the values
6066 of `fit-frame-to-buffer-max-height' and `window-min-height'."
6067 :type 'boolean
6068 :version "24.2"
6069 :group 'help)
6070
6071 (defcustom fit-frame-to-buffer-bottom-margin 4
6072 "Bottom margin for `fit-frame-to-buffer'.
6073 This is the number of lines `fit-frame-to-buffer' leaves free at the
6074 bottom of the display in order to not obscure the system task bar."
6075 :type 'integer
6076 :version "24.2"
6077 :group 'windows)
6078
6079 (defun fit-frame-to-buffer (&optional frame max-height min-height)
6080 "Adjust height of FRAME to display its buffer's contents exactly.
6081 FRAME can be any live frame and defaults to the selected one.
6082
6083 Optional argument MAX-HEIGHT specifies the maximum height of
6084 FRAME and defaults to the height of the display below the current
6085 top line of FRAME minus FIT-FRAME-TO-BUFFER-BOTTOM-MARGIN.
6086 Optional argument MIN-HEIGHT specifies the minimum height of
6087 FRAME."
6088 (interactive)
6089 (setq frame (window-normalize-frame frame))
6090 (let* ((root (frame-root-window frame))
6091 (frame-min-height
6092 (+ (- (frame-height frame) (window-total-size root))
6093 window-min-height))
6094 (frame-top (frame-parameter frame 'top))
6095 (top (if (consp frame-top)
6096 (funcall (car frame-top) (cadr frame-top))
6097 frame-top))
6098 (frame-max-height
6099 (- (/ (- (x-display-pixel-height frame) top)
6100 (frame-char-height frame))
6101 fit-frame-to-buffer-bottom-margin))
6102 (compensate 0)
6103 delta)
6104 (when (and (window-live-p root) (not (window-size-fixed-p root)))
6105 (with-selected-window root
6106 (cond
6107 ((not max-height)
6108 (setq max-height frame-max-height))
6109 ((numberp max-height)
6110 (setq max-height (min max-height frame-max-height)))
6111 (t
6112 (error "%s is an invalid maximum height" max-height)))
6113 (cond
6114 ((not min-height)
6115 (setq min-height frame-min-height))
6116 ((numberp min-height)
6117 (setq min-height (min min-height frame-min-height)))
6118 (t
6119 (error "%s is an invalid minimum height" min-height)))
6120 ;; When tool-bar-mode is enabled and we have just created a new
6121 ;; frame, reserve lines for toolbar resizing. This is needed
6122 ;; because for reasons unknown to me Emacs (1) reserves one line
6123 ;; for the toolbar when making the initial frame and toolbars
6124 ;; are enabled, and (2) later adds the remaining lines needed.
6125 ;; Our code runs IN BETWEEN (1) and (2). YMMV when you're on a
6126 ;; system that behaves differently.
6127 (let ((quit-restore (window-parameter root 'quit-restore))
6128 (lines (tool-bar-lines-needed frame)))
6129 (when (and quit-restore (eq (car quit-restore) 'frame)
6130 (not (zerop lines)))
6131 (setq compensate (1- lines))))
6132 (message "%s" compensate)
6133 (setq delta
6134 ;; Always count a final newline - we don't do any
6135 ;; post-processing, so let's play safe.
6136 (+ (count-screen-lines nil nil t)
6137 (- (window-body-size))
6138 compensate)))
6139 ;; Move away from final newline.
6140 (when (and (eobp) (bolp) (not (bobp)))
6141 (set-window-point root (line-beginning-position 0)))
6142 (set-window-start root (point-min))
6143 (set-window-vscroll root 0)
6144 (condition-case nil
6145 (set-frame-height
6146 frame
6147 (min (max (+ (frame-height frame) delta)
6148 min-height)
6149 max-height))
6150 (error (setq delta nil))))
6151 delta))
6152
6153 (defun fit-window-to-buffer (&optional window max-height min-height)
6154 "Adjust height of WINDOW to display its buffer's contents exactly.
6155 WINDOW must be a live window and defaults to the selected one.
6156
6157 Optional argument MAX-HEIGHT specifies the maximum height of
6158 WINDOW and defaults to the height of WINDOW's frame. Optional
6159 argument MIN-HEIGHT specifies the minimum height of WINDOW and
6160 defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
6161 are specified in lines and include the mode line and header line,
6162 if any.
6163
6164 Return the number of lines by which WINDOW was enlarged or
6165 shrunk. If an error occurs during resizing, return nil but don't
6166 signal an error.
6167
6168 Note that even if this function makes WINDOW large enough to show
6169 _all_ lines of its buffer you might not see the first lines when
6170 WINDOW was scrolled."
6171 (interactive)
6172 (setq window (window-normalize-window window t))
6173 (cond
6174 ((window-size-fixed-p window))
6175 ((window-full-height-p window)
6176 (when fit-frame-to-buffer
6177 (fit-frame-to-buffer (window-frame window))))
6178 (t
6179 (with-selected-window window
6180 (let* ((height (window-total-size))
6181 (min-height
6182 ;; Adjust MIN-HEIGHT.
6183 (if (numberp min-height)
6184 ;; Can't get smaller than `window-safe-min-height'.
6185 (max min-height window-safe-min-height)
6186 ;; Preserve header and mode line if present.
6187 (window-min-size nil nil t)))
6188 (max-height
6189 ;; Adjust MAX-HEIGHT.
6190 (if (numberp max-height)
6191 ;; Can't get larger than height of frame.
6192 (min max-height
6193 (window-total-size (frame-root-window window)))
6194 ;; Don't delete other windows.
6195 (+ height (window-max-delta nil nil window))))
6196 ;; Make `desired-height' the height necessary to show
6197 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
6198 ;; and MAX-HEIGHT.
6199 (desired-height
6200 (max
6201 (min
6202 (+ (count-screen-lines)
6203 ;; For non-minibuffers count the mode line, if any.
6204 (if (and (not (window-minibuffer-p window))
6205 mode-line-format)
6206 1
6207 0)
6208 ;; Count the header line, if any.
6209 (if header-line-format 1 0))
6210 max-height)
6211 min-height))
6212 (desired-delta
6213 (- desired-height (window-total-size window)))
6214 (delta
6215 (if (> desired-delta 0)
6216 (min desired-delta
6217 (window-max-delta window nil window))
6218 (max desired-delta
6219 (- (window-min-delta window nil window))))))
6220 (condition-case nil
6221 (if (zerop delta)
6222 ;; Return zero if DELTA became zero in the process.
6223 0
6224 ;; Don't try to redisplay with the cursor at the end on its
6225 ;; own line--that would force a scroll and spoil things.
6226 (when (and (eobp) (bolp) (not (bobp)))
6227 ;; It's silly to put `point' at the end of the previous
6228 ;; line and so maybe force horizontal scrolling.
6229 (set-window-point window (line-beginning-position 0)))
6230 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
6231 (window-resize window delta nil window)
6232 ;; Check if the last line is surely fully visible. If
6233 ;; not, enlarge the window.
6234 (let ((end (save-excursion
6235 (goto-char (point-max))
6236 (when (and (bolp) (not (bobp)))
6237 ;; Don't include final newline.
6238 (backward-char 1))
6239 (when truncate-lines
6240 ;; If line-wrapping is turned off, test the
6241 ;; beginning of the last line for
6242 ;; visibility instead of the end, as the
6243 ;; end of the line could be invisible by
6244 ;; virtue of extending past the edge of the
6245 ;; window.
6246 (forward-line 0))
6247 (point))))
6248 (set-window-vscroll window 0)
6249 ;; This loop might in some rare pathological cases raise
6250 ;; an error - another reason for the `condition-case'.
6251 (while (and (< desired-height max-height)
6252 (= desired-height (window-total-size))
6253 (not (pos-visible-in-window-p end)))
6254 (window-resize window 1 nil window)
6255 (setq desired-height (1+ desired-height)))))
6256 (error (setq delta nil)))
6257 delta)))))
6258
6259 (defun window-safely-shrinkable-p (&optional window)
6260 "Return t if WINDOW can be shrunk without shrinking other windows.
6261 WINDOW defaults to the selected window."
6262 (with-selected-window (or window (selected-window))
6263 (let ((edges (window-edges)))
6264 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
6265 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
6266
6267 (defun shrink-window-if-larger-than-buffer (&optional window)
6268 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6269 More precisely, shrink WINDOW vertically to be as small as
6270 possible, while still showing the full contents of its buffer.
6271 WINDOW must be a live window and defaults to the selected one.
6272
6273 Do not shrink WINDOW to less than `window-min-height' lines. Do
6274 nothing if the buffer contains more lines than the present window
6275 height, or if some of the window's contents are scrolled out of
6276 view, or if shrinking this window would also shrink another
6277 window, or if the window is the only window of its frame.
6278
6279 Return non-nil if the window was shrunk, nil otherwise."
6280 (interactive)
6281 (setq window (window-normalize-window window t))
6282 ;; Make sure that WINDOW is vertically combined and `point-min' is
6283 ;; visible (for whatever reason that's needed). The remaining issues
6284 ;; should be taken care of by `fit-window-to-buffer'.
6285 (when (and (window-combined-p window)
6286 (pos-visible-in-window-p (point-min) window))
6287 (fit-window-to-buffer window (window-total-size window))))
6288 \f
6289 (defun kill-buffer-and-window ()
6290 "Kill the current buffer and delete the selected window."
6291 (interactive)
6292 (let ((window-to-delete (selected-window))
6293 (buffer-to-kill (current-buffer))
6294 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6295 (unwind-protect
6296 (progn
6297 (add-hook 'kill-buffer-hook delete-window-hook t t)
6298 (if (kill-buffer (current-buffer))
6299 ;; If `delete-window' failed before, we rerun it to regenerate
6300 ;; the error so it can be seen in the echo area.
6301 (when (eq (selected-window) window-to-delete)
6302 (delete-window))))
6303 ;; If the buffer is not dead for some reason (probably because
6304 ;; of a `quit' signal), remove the hook again.
6305 (ignore-errors
6306 (with-current-buffer buffer-to-kill
6307 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
6308
6309 \f
6310 (defvar recenter-last-op nil
6311 "Indicates the last recenter operation performed.
6312 Possible values: `top', `middle', `bottom', integer or float numbers.")
6313
6314 (defcustom recenter-positions '(middle top bottom)
6315 "Cycling order for `recenter-top-bottom'.
6316 A list of elements with possible values `top', `middle', `bottom',
6317 integer or float numbers that define the cycling order for
6318 the command `recenter-top-bottom'.
6319
6320 Top and bottom destinations are `scroll-margin' lines from the true
6321 window top and bottom. Middle redraws the frame and centers point
6322 vertically within the window. Integer number moves current line to
6323 the specified absolute window-line. Float number between 0.0 and 1.0
6324 means the percentage of the screen space from the top. The default
6325 cycling order is middle -> top -> bottom."
6326 :type '(repeat (choice
6327 (const :tag "Top" top)
6328 (const :tag "Middle" middle)
6329 (const :tag "Bottom" bottom)
6330 (integer :tag "Line number")
6331 (float :tag "Percentage")))
6332 :version "23.2"
6333 :group 'windows)
6334
6335 (defun recenter-top-bottom (&optional arg)
6336 "Move current buffer line to the specified window line.
6337 With no prefix argument, successive calls place point according
6338 to the cycling order defined by `recenter-positions'.
6339
6340 A prefix argument is handled like `recenter':
6341 With numeric prefix ARG, move current line to window-line ARG.
6342 With plain `C-u', move current line to window center."
6343 (interactive "P")
6344 (cond
6345 (arg (recenter arg)) ; Always respect ARG.
6346 (t
6347 (setq recenter-last-op
6348 (if (eq this-command last-command)
6349 (car (or (cdr (member recenter-last-op recenter-positions))
6350 recenter-positions))
6351 (car recenter-positions)))
6352 (let ((this-scroll-margin
6353 (min (max 0 scroll-margin)
6354 (truncate (/ (window-body-height) 4.0)))))
6355 (cond ((eq recenter-last-op 'middle)
6356 (recenter))
6357 ((eq recenter-last-op 'top)
6358 (recenter this-scroll-margin))
6359 ((eq recenter-last-op 'bottom)
6360 (recenter (- -1 this-scroll-margin)))
6361 ((integerp recenter-last-op)
6362 (recenter recenter-last-op))
6363 ((floatp recenter-last-op)
6364 (recenter (round (* recenter-last-op (window-height))))))))))
6365
6366 (define-key global-map [?\C-l] 'recenter-top-bottom)
6367
6368 (defun move-to-window-line-top-bottom (&optional arg)
6369 "Position point relative to window.
6370
6371 With a prefix argument ARG, acts like `move-to-window-line'.
6372
6373 With no argument, positions point at center of window.
6374 Successive calls position point at positions defined
6375 by `recenter-positions'."
6376 (interactive "P")
6377 (cond
6378 (arg (move-to-window-line arg)) ; Always respect ARG.
6379 (t
6380 (setq recenter-last-op
6381 (if (eq this-command last-command)
6382 (car (or (cdr (member recenter-last-op recenter-positions))
6383 recenter-positions))
6384 (car recenter-positions)))
6385 (let ((this-scroll-margin
6386 (min (max 0 scroll-margin)
6387 (truncate (/ (window-body-height) 4.0)))))
6388 (cond ((eq recenter-last-op 'middle)
6389 (call-interactively 'move-to-window-line))
6390 ((eq recenter-last-op 'top)
6391 (move-to-window-line this-scroll-margin))
6392 ((eq recenter-last-op 'bottom)
6393 (move-to-window-line (- -1 this-scroll-margin)))
6394 ((integerp recenter-last-op)
6395 (move-to-window-line recenter-last-op))
6396 ((floatp recenter-last-op)
6397 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
6398
6399 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
6400 \f
6401 ;;; Scrolling commands.
6402
6403 ;;; Scrolling commands which do not signal errors at top/bottom
6404 ;;; of buffer at first key-press (instead move to top/bottom
6405 ;;; of buffer).
6406
6407 (defcustom scroll-error-top-bottom nil
6408 "Move point to top/bottom of buffer before signaling a scrolling error.
6409 A value of nil means just signal an error if no more scrolling possible.
6410 A value of t means point moves to the beginning or the end of the buffer
6411 \(depending on scrolling direction) when no more scrolling possible.
6412 When point is already on that position, then signal an error."
6413 :type 'boolean
6414 :group 'windows
6415 :version "24.1")
6416
6417 (defun scroll-up-command (&optional arg)
6418 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6419 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6420 scroll window further, move cursor to the bottom line.
6421 When point is already on that position, then signal an error.
6422 A near full screen is `next-screen-context-lines' less than a full screen.
6423 Negative ARG means scroll downward.
6424 If ARG is the atom `-', scroll downward by nearly full screen."
6425 (interactive "^P")
6426 (cond
6427 ((null scroll-error-top-bottom)
6428 (scroll-up arg))
6429 ((eq arg '-)
6430 (scroll-down-command nil))
6431 ((< (prefix-numeric-value arg) 0)
6432 (scroll-down-command (- (prefix-numeric-value arg))))
6433 ((eobp)
6434 (scroll-up arg)) ; signal error
6435 (t
6436 (condition-case nil
6437 (scroll-up arg)
6438 (end-of-buffer
6439 (if arg
6440 ;; When scrolling by ARG lines can't be done,
6441 ;; move by ARG lines instead.
6442 (forward-line arg)
6443 ;; When ARG is nil for full-screen scrolling,
6444 ;; move to the bottom of the buffer.
6445 (goto-char (point-max))))))))
6446
6447 (put 'scroll-up-command 'scroll-command t)
6448
6449 (defun scroll-down-command (&optional arg)
6450 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6451 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6452 scroll window further, move cursor to the top line.
6453 When point is already on that position, then signal an error.
6454 A near full screen is `next-screen-context-lines' less than a full screen.
6455 Negative ARG means scroll upward.
6456 If ARG is the atom `-', scroll upward by nearly full screen."
6457 (interactive "^P")
6458 (cond
6459 ((null scroll-error-top-bottom)
6460 (scroll-down arg))
6461 ((eq arg '-)
6462 (scroll-up-command nil))
6463 ((< (prefix-numeric-value arg) 0)
6464 (scroll-up-command (- (prefix-numeric-value arg))))
6465 ((bobp)
6466 (scroll-down arg)) ; signal error
6467 (t
6468 (condition-case nil
6469 (scroll-down arg)
6470 (beginning-of-buffer
6471 (if arg
6472 ;; When scrolling by ARG lines can't be done,
6473 ;; move by ARG lines instead.
6474 (forward-line (- arg))
6475 ;; When ARG is nil for full-screen scrolling,
6476 ;; move to the top of the buffer.
6477 (goto-char (point-min))))))))
6478
6479 (put 'scroll-down-command 'scroll-command t)
6480
6481 ;;; Scrolling commands which scroll a line instead of full screen.
6482
6483 (defun scroll-up-line (&optional arg)
6484 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6485 If ARG is omitted or nil, scroll upward by one line.
6486 This is different from `scroll-up-command' that scrolls a full screen."
6487 (interactive "p")
6488 (scroll-up (or arg 1)))
6489
6490 (put 'scroll-up-line 'scroll-command t)
6491
6492 (defun scroll-down-line (&optional arg)
6493 "Scroll text of selected window down ARG lines; or one line if no ARG.
6494 If ARG is omitted or nil, scroll down by one line.
6495 This is different from `scroll-down-command' that scrolls a full screen."
6496 (interactive "p")
6497 (scroll-down (or arg 1)))
6498
6499 (put 'scroll-down-line 'scroll-command t)
6500
6501 \f
6502 (defun scroll-other-window-down (&optional lines)
6503 "Scroll the \"other window\" down.
6504 For more details, see the documentation for `scroll-other-window'."
6505 (interactive "P")
6506 (scroll-other-window
6507 ;; Just invert the argument's meaning.
6508 ;; We can do that without knowing which window it will be.
6509 (if (eq lines '-) nil
6510 (if (null lines) '-
6511 (- (prefix-numeric-value lines))))))
6512
6513 (defun beginning-of-buffer-other-window (arg)
6514 "Move point to the beginning of the buffer in the other window.
6515 Leave mark at previous position.
6516 With arg N, put point N/10 of the way from the true beginning."
6517 (interactive "P")
6518 (let ((orig-window (selected-window))
6519 (window (other-window-for-scrolling)))
6520 ;; We use unwind-protect rather than save-window-excursion
6521 ;; because the latter would preserve the things we want to change.
6522 (unwind-protect
6523 (progn
6524 (select-window window)
6525 ;; Set point and mark in that window's buffer.
6526 (with-no-warnings
6527 (beginning-of-buffer arg))
6528 ;; Set point accordingly.
6529 (recenter '(t)))
6530 (select-window orig-window))))
6531
6532 (defun end-of-buffer-other-window (arg)
6533 "Move point to the end of the buffer in the other window.
6534 Leave mark at previous position.
6535 With arg N, put point N/10 of the way from the true end."
6536 (interactive "P")
6537 ;; See beginning-of-buffer-other-window for comments.
6538 (let ((orig-window (selected-window))
6539 (window (other-window-for-scrolling)))
6540 (unwind-protect
6541 (progn
6542 (select-window window)
6543 (with-no-warnings
6544 (end-of-buffer arg))
6545 (recenter '(t)))
6546 (select-window orig-window))))
6547 \f
6548 (defvar mouse-autoselect-window-timer nil
6549 "Timer used by delayed window autoselection.")
6550
6551 (defvar mouse-autoselect-window-position nil
6552 "Last mouse position recorded by delayed window autoselection.")
6553
6554 (defvar mouse-autoselect-window-window nil
6555 "Last window recorded by delayed window autoselection.")
6556
6557 (defvar mouse-autoselect-window-state nil
6558 "When non-nil, special state of delayed window autoselection.
6559 Possible values are `suspend' (suspend autoselection after a menu or
6560 scrollbar interaction) and `select' (the next invocation of
6561 `handle-select-window' shall select the window immediately).")
6562
6563 (defun mouse-autoselect-window-cancel (&optional force)
6564 "Cancel delayed window autoselection.
6565 Optional argument FORCE means cancel unconditionally."
6566 (unless (and (not force)
6567 ;; Don't cancel for select-window or select-frame events
6568 ;; or when the user drags a scroll bar.
6569 (or (memq this-command
6570 '(handle-select-window handle-switch-frame))
6571 (and (eq this-command 'scroll-bar-toolkit-scroll)
6572 (memq (nth 4 (event-end last-input-event))
6573 '(handle end-scroll)))))
6574 (setq mouse-autoselect-window-state nil)
6575 (when (timerp mouse-autoselect-window-timer)
6576 (cancel-timer mouse-autoselect-window-timer))
6577 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
6578
6579 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
6580 "Start delayed window autoselection.
6581 MOUSE-POSITION is the last position where the mouse was seen as returned
6582 by `mouse-position'. Optional argument WINDOW non-nil denotes the
6583 window where the mouse was seen. Optional argument SUSPEND non-nil
6584 means suspend autoselection."
6585 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
6586 (setq mouse-autoselect-window-position mouse-position)
6587 (when window (setq mouse-autoselect-window-window window))
6588 (setq mouse-autoselect-window-state (when suspend 'suspend))
6589 ;; Install timer which runs `mouse-autoselect-window-select' after
6590 ;; `mouse-autoselect-window' seconds.
6591 (setq mouse-autoselect-window-timer
6592 (run-at-time
6593 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
6594
6595 (defun mouse-autoselect-window-select ()
6596 "Select window with delayed window autoselection.
6597 If the mouse position has stabilized in a non-selected window, select
6598 that window. The minibuffer window is selected only if the minibuffer
6599 is active. This function is run by `mouse-autoselect-window-timer'."
6600 (ignore-errors
6601 (let* ((mouse-position (mouse-position))
6602 (window
6603 (ignore-errors
6604 (window-at (cadr mouse-position) (cddr mouse-position)
6605 (car mouse-position)))))
6606 (cond
6607 ((or (menu-or-popup-active-p)
6608 (and window
6609 (not (coordinates-in-window-p (cdr mouse-position) window))))
6610 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
6611 ;; of WINDOW, temporarily suspend delayed autoselection.
6612 (mouse-autoselect-window-start mouse-position nil t))
6613 ((eq mouse-autoselect-window-state 'suspend)
6614 ;; Delayed autoselection was temporarily suspended, reenable it.
6615 (mouse-autoselect-window-start mouse-position))
6616 ((and window (not (eq window (selected-window)))
6617 (or (not (numberp mouse-autoselect-window))
6618 (and (> mouse-autoselect-window 0)
6619 ;; If `mouse-autoselect-window' is positive, select
6620 ;; window if the window is the same as before.
6621 (eq window mouse-autoselect-window-window))
6622 ;; Otherwise select window if the mouse is at the same
6623 ;; position as before. Observe that the first test after
6624 ;; starting autoselection usually fails since the value of
6625 ;; `mouse-autoselect-window-position' recorded there is the
6626 ;; position where the mouse has entered the new window and
6627 ;; not necessarily where the mouse has stopped moving.
6628 (equal mouse-position mouse-autoselect-window-position))
6629 ;; The minibuffer is a candidate window if it's active.
6630 (or (not (window-minibuffer-p window))
6631 (eq window (active-minibuffer-window))))
6632 ;; Mouse position has stabilized in non-selected window: Cancel
6633 ;; delayed autoselection and try to select that window.
6634 (mouse-autoselect-window-cancel t)
6635 ;; Select window where mouse appears unless the selected window is the
6636 ;; minibuffer. Use `unread-command-events' in order to execute pre-
6637 ;; and post-command hooks and trigger idle timers. To avoid delaying
6638 ;; autoselection again, set `mouse-autoselect-window-state'."
6639 (unless (window-minibuffer-p (selected-window))
6640 (setq mouse-autoselect-window-state 'select)
6641 (setq unread-command-events
6642 (cons (list 'select-window (list window))
6643 unread-command-events))))
6644 ((or (and window (eq window (selected-window)))
6645 (not (numberp mouse-autoselect-window))
6646 (equal mouse-position mouse-autoselect-window-position))
6647 ;; Mouse position has either stabilized in the selected window or at
6648 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
6649 (mouse-autoselect-window-cancel t))
6650 (t
6651 ;; Mouse position has not stabilized yet, resume delayed
6652 ;; autoselection.
6653 (mouse-autoselect-window-start mouse-position window))))))
6654
6655 (defun handle-select-window (event)
6656 "Handle select-window events."
6657 (interactive "e")
6658 (let ((window (posn-window (event-start event))))
6659 (unless (or (not (window-live-p window))
6660 ;; Don't switch if we're currently in the minibuffer.
6661 ;; This tries to work around problems where the
6662 ;; minibuffer gets unselected unexpectedly, and where
6663 ;; you then have to move your mouse all the way down to
6664 ;; the minibuffer to select it.
6665 (window-minibuffer-p (selected-window))
6666 ;; Don't switch to minibuffer window unless it's active.
6667 (and (window-minibuffer-p window)
6668 (not (minibuffer-window-active-p window)))
6669 ;; Don't switch when autoselection shall be delayed.
6670 (and (numberp mouse-autoselect-window)
6671 (not (zerop mouse-autoselect-window))
6672 (not (eq mouse-autoselect-window-state 'select))
6673 (progn
6674 ;; Cancel any delayed autoselection.
6675 (mouse-autoselect-window-cancel t)
6676 ;; Start delayed autoselection from current mouse
6677 ;; position and window.
6678 (mouse-autoselect-window-start (mouse-position) window)
6679 ;; Executing a command cancels delayed autoselection.
6680 (add-hook
6681 'pre-command-hook 'mouse-autoselect-window-cancel))))
6682 (when mouse-autoselect-window
6683 ;; Reset state of delayed autoselection.
6684 (setq mouse-autoselect-window-state nil)
6685 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
6686 (run-hooks 'mouse-leave-buffer-hook))
6687 ;; Clear echo area.
6688 (message nil)
6689 (select-window window))))
6690
6691 (defun truncated-partial-width-window-p (&optional window)
6692 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
6693 WINDOW must be a live window and defaults to the selected one.
6694 Return nil if WINDOW is not a partial-width window
6695 (regardless of the value of `truncate-lines').
6696 Otherwise, consult the value of `truncate-partial-width-windows'
6697 for the buffer shown in WINDOW."
6698 (setq window (window-normalize-window window t))
6699 (unless (window-full-width-p window)
6700 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
6701 (window-buffer window))))
6702 (if (integerp t-p-w-w)
6703 (< (window-width window) t-p-w-w)
6704 t-p-w-w))))
6705 \f
6706 ;; Some of these are in tutorial--default-keys, so update that if you
6707 ;; change these.
6708 (define-key ctl-x-map "0" 'delete-window)
6709 (define-key ctl-x-map "1" 'delete-other-windows)
6710 (define-key ctl-x-map "2" 'split-window-below)
6711 (define-key ctl-x-map "3" 'split-window-right)
6712 (define-key ctl-x-map "o" 'other-window)
6713 (define-key ctl-x-map "^" 'enlarge-window)
6714 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
6715 (define-key ctl-x-map "{" 'shrink-window-horizontally)
6716 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
6717 (define-key ctl-x-map "+" 'balance-windows)
6718 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
6719
6720 ;;; window.el ends here