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