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