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