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