]> code.delx.au - gnu-emacs/blob - lisp/window.el
Replace "Maintainer: FSF" with the emacs-devel mailing address
[gnu-emacs] / lisp / window.el
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
2
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-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 live window and defaults to the selected one.
5030
5031 Optional argument IGNORE non-nil means ignore minimum window
5032 sizes and fixed size restrictions. IGNORE equal `safe' means
5033 windows can get as small as `window-safe-min-height' and
5034 `window-safe-min-width'."
5035 (setq window-state-put-stale-windows nil)
5036 (setq window (window-normalize-window window t))
5037 (let* ((frame (window-frame window))
5038 (head (car state))
5039 ;; We check here (1) whether the total sizes of root window of
5040 ;; STATE and that of WINDOW are equal so we can avoid
5041 ;; calculating new sizes, and (2) if we do have to resize
5042 ;; whether we can do so without violating size restrictions.
5043 (pixelwise (and (cdr (assq 'pixel-width state))
5044 (cdr (assq 'pixel-height state))))
5045 (totals (or (and pixelwise
5046 (= (window-pixel-width window)
5047 (cdr (assq 'pixel-width state)))
5048 (= (window-pixel-height window)
5049 (cdr (assq 'pixel-height state))))
5050 (and (= (window-total-width window)
5051 (cdr (assq 'total-width state)))
5052 (= (window-total-height window)
5053 (cdr (assq 'total-height state))))))
5054 (min-height (cdr (assq
5055 (if pixelwise 'min-pixel-height 'min-height)
5056 head)))
5057 (min-width (cdr (assq
5058 (if pixelwise 'min-pixel-width 'min-weight)
5059 head))))
5060 (if (and (not totals)
5061 (or (> min-height (window-size window nil pixelwise))
5062 (> min-width (window-size window t pixelwise)))
5063 (or (not ignore)
5064 (and (setq min-height
5065 (cdr (assq
5066 (if pixelwise
5067 'min-pixel-height-ignore
5068 'min-height-ignore)
5069 head)))
5070 (setq min-width
5071 (cdr (assq
5072 (if pixelwise
5073 'min-pixel-width-ignore
5074 'min-width-ignore)
5075 head)))
5076 (or (> min-height
5077 (window-size window nil pixelwise))
5078 (> min-width
5079 (window-size window t pixelwise)))
5080 (or (not (eq ignore 'safe))
5081 (and (setq min-height
5082 (cdr (assq
5083 (if pixelwise
5084 'min-pixel-height-safe
5085 'min-height-safe)
5086 head)))
5087 (setq min-width
5088 (cdr (assq
5089 (if pixelwise
5090 'min-pixel-width-safe
5091 'min-width-safe)
5092 head)))
5093 (or (> min-height
5094 (window-size window nil pixelwise))
5095 (> min-width
5096 (window-size window t pixelwise))))))))
5097 ;; The check above might not catch all errors due to rounding
5098 ;; issues - so IGNORE equal 'safe might not always produce the
5099 ;; minimum possible state. But such configurations hardly make
5100 ;; sense anyway.
5101 (error "Window %s too small to accommodate state" window)
5102 (setq state (cdr state))
5103 (setq window-state-put-list nil)
5104 ;; Work on the windows of a temporary buffer to make sure that
5105 ;; splitting proceeds regardless of any buffer local values of
5106 ;; `window-size-fixed'. Release that buffer after the buffers of
5107 ;; all live windows have been set by `window--state-put-2'.
5108 (with-temp-buffer
5109 (set-window-buffer window (current-buffer))
5110 (window--state-put-1 state window nil totals pixelwise)
5111 (window--state-put-2 ignore pixelwise))
5112 (while window-state-put-stale-windows
5113 (let ((window (pop window-state-put-stale-windows)))
5114 (when (eq (window-deletable-p window) t)
5115 (delete-window window))))
5116 (window--check frame))))
5117 \f
5118 (defun display-buffer-record-window (type window buffer)
5119 "Record information for window used by `display-buffer'.
5120 TYPE specifies the type of the calling operation and must be one
5121 of the symbols 'reuse (when WINDOW existed already and was
5122 reused for displaying BUFFER), 'window (when WINDOW was created
5123 on an already existing frame), or 'frame (when WINDOW was
5124 created on a new frame). WINDOW is the window used for or created
5125 by the `display-buffer' routines. BUFFER is the buffer that
5126 shall be displayed.
5127
5128 This function installs or updates the quit-restore parameter of
5129 WINDOW. The quit-restore parameter is a list of four elements:
5130 The first element is one of the symbols 'window, 'frame, 'same or
5131 'other. The second element is either one of the symbols 'window
5132 or 'frame or a list whose elements are the buffer previously
5133 shown in the window, that buffer's window start and window point,
5134 and the window's height. The third element is the window
5135 selected at the time the parameter was created. The fourth
5136 element is BUFFER."
5137 (cond
5138 ((eq type 'reuse)
5139 (if (eq (window-buffer window) buffer)
5140 ;; WINDOW shows BUFFER already.
5141 (when (consp (window-parameter window 'quit-restore))
5142 ;; If WINDOW has a quit-restore parameter, reset its car.
5143 (setcar (window-parameter window 'quit-restore) 'same))
5144 ;; WINDOW shows another buffer.
5145 (with-current-buffer (window-buffer window)
5146 (set-window-parameter
5147 window 'quit-restore
5148 (list 'other
5149 ;; A quadruple of WINDOW's buffer, start, point and height.
5150 (list (current-buffer) (window-start window)
5151 ;; Preserve window-point-insertion-type (Bug#12588).
5152 (copy-marker
5153 (window-point window) window-point-insertion-type)
5154 (window-total-height window))
5155 (selected-window) buffer)))))
5156 ((eq type 'window)
5157 ;; WINDOW has been created on an existing frame.
5158 (set-window-parameter
5159 window 'quit-restore
5160 (list 'window 'window (selected-window) buffer)))
5161 ((eq type 'frame)
5162 ;; WINDOW has been created on a new frame.
5163 (set-window-parameter
5164 window 'quit-restore
5165 (list 'frame 'frame (selected-window) buffer)))))
5166
5167 (defcustom display-buffer-function nil
5168 "If non-nil, function to call to handle `display-buffer'.
5169 It will receive two args, the buffer and a flag which if non-nil
5170 means that the currently selected window is not acceptable. It
5171 should choose or create a window, display the specified buffer in
5172 it, and return the window.
5173
5174 The specified function should call `display-buffer-record-window'
5175 with corresponding arguments to set up the quit-restore parameter
5176 of the window used."
5177 :type '(choice
5178 (const nil)
5179 (function :tag "function"))
5180 :group 'windows)
5181
5182 (make-obsolete-variable 'display-buffer-function
5183 'display-buffer-alist "24.3")
5184
5185 ;; Eventually, we want to turn this into a defvar; instead of
5186 ;; customizing this, the user should use a `pop-up-frame-parameters'
5187 ;; alist entry in `display-buffer-base-action'.
5188 (defcustom pop-up-frame-alist nil
5189 "Alist of parameters for automatically generated new frames.
5190 If non-nil, the value you specify here is used by the default
5191 `pop-up-frame-function' for the creation of new frames.
5192
5193 Since `pop-up-frame-function' is used by `display-buffer' for
5194 making new frames, any value specified here by default affects
5195 the automatic generation of new frames via `display-buffer' and
5196 all functions based on it. The behavior of `make-frame' is not
5197 affected by this variable."
5198 :type '(repeat (cons :format "%v"
5199 (symbol :tag "Parameter")
5200 (sexp :tag "Value")))
5201 :group 'frames)
5202
5203 (defcustom pop-up-frame-function
5204 (lambda () (make-frame pop-up-frame-alist))
5205 "Function used by `display-buffer' for creating a new frame.
5206 This function is called with no arguments and should return a new
5207 frame. The default value calls `make-frame' with the argument
5208 `pop-up-frame-alist'."
5209 :type 'function
5210 :group 'frames)
5211
5212 (defcustom special-display-buffer-names nil
5213 "List of names of buffers that should be displayed specially.
5214 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5215 its name is in this list, displays the buffer in a way specified
5216 by `special-display-function'. `special-display-popup-frame'
5217 \(the default for `special-display-function') usually displays
5218 the buffer in a separate frame made with the parameters specified
5219 by `special-display-frame-alist'. If `special-display-function'
5220 has been set to some other function, that function is called with
5221 the buffer as first, and nil as second argument.
5222
5223 Alternatively, an element of this list can be specified as
5224 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5225 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5226 `special-display-popup-frame' will interpret such pairs as frame
5227 parameters when it creates a special frame, overriding the
5228 corresponding values from `special-display-frame-alist'.
5229
5230 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5231 `special-display-popup-frame' displays that buffer in the
5232 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5233 it displays that buffer in a window on the selected frame.
5234
5235 If `special-display-function' specifies some other function than
5236 `special-display-popup-frame', that function is called with the
5237 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5238 argument.
5239
5240 Finally, an element of this list can be also specified as
5241 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5242 `special-display-popup-frame' will call FUNCTION with the buffer
5243 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5244 second.
5245
5246 Any alternative function specified here is responsible for
5247 setting up the quit-restore parameter of the window used.
5248
5249 If this variable appears \"not to work\", because you added a
5250 name to it but the corresponding buffer is displayed in the
5251 selected window, look at the values of `same-window-buffer-names'
5252 and `same-window-regexps'. Those variables take precedence over
5253 this one.
5254
5255 See also `special-display-regexps'."
5256 :type '(repeat
5257 (choice :tag "Buffer"
5258 :value ""
5259 (string :format "%v")
5260 (cons :tag "With parameters"
5261 :format "%v"
5262 :value ("" . nil)
5263 (string :format "%v")
5264 (repeat :tag "Parameters"
5265 (cons :format "%v"
5266 (symbol :tag "Parameter")
5267 (sexp :tag "Value"))))
5268 (list :tag "With function"
5269 :format "%v"
5270 :value ("" . nil)
5271 (string :format "%v")
5272 (function :tag "Function")
5273 (repeat :tag "Arguments" (sexp)))))
5274 :group 'windows
5275 :group 'frames)
5276 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
5277 (put 'special-display-buffer-names 'risky-local-variable t)
5278
5279 (defcustom special-display-regexps nil
5280 "List of regexps saying which buffers should be displayed specially.
5281 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5282 any regexp in this list matches its name, displays it specially
5283 using `special-display-function'. `special-display-popup-frame'
5284 \(the default for `special-display-function') usually displays
5285 the buffer in a separate frame made with the parameters specified
5286 by `special-display-frame-alist'. If `special-display-function'
5287 has been set to some other function, that function is called with
5288 the buffer as first, and nil as second argument.
5289
5290 Alternatively, an element of this list can be specified as
5291 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5292 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5293 `special-display-popup-frame' will then interpret these pairs as
5294 frame parameters when creating a special frame for a buffer whose
5295 name matches REGEXP, overriding the corresponding values from
5296 `special-display-frame-alist'.
5297
5298 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5299 `special-display-popup-frame' displays buffers matching REGEXP in
5300 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5301 to display such buffers in a window on the selected frame.
5302
5303 If `special-display-function' specifies some other function than
5304 `special-display-popup-frame', that function is called with the
5305 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5306 as second argument.
5307
5308 Finally, an element of this list can be also specified as
5309 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5310 will then call FUNCTION with the buffer whose name matched
5311 REGEXP as first, and OTHER-ARGS as second argument.
5312
5313 Any alternative function specified here is responsible for
5314 setting up the quit-restore parameter of the window used.
5315
5316 If this variable appears \"not to work\", because you added a
5317 name to it but the corresponding buffer is displayed in the
5318 selected window, look at the values of `same-window-buffer-names'
5319 and `same-window-regexps'. Those variables take precedence over
5320 this one.
5321
5322 See also `special-display-buffer-names'."
5323 :type '(repeat
5324 (choice :tag "Buffer"
5325 :value ""
5326 (regexp :format "%v")
5327 (cons :tag "With parameters"
5328 :format "%v"
5329 :value ("" . nil)
5330 (regexp :format "%v")
5331 (repeat :tag "Parameters"
5332 (cons :format "%v"
5333 (symbol :tag "Parameter")
5334 (sexp :tag "Value"))))
5335 (list :tag "With function"
5336 :format "%v"
5337 :value ("" . nil)
5338 (regexp :format "%v")
5339 (function :tag "Function")
5340 (repeat :tag "Arguments" (sexp)))))
5341 :group 'windows
5342 :group 'frames)
5343 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
5344 (put 'special-display-regexps 'risky-local-variable t)
5345
5346 (defun special-display-p (buffer-name)
5347 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5348 More precisely, return t if `special-display-buffer-names' or
5349 `special-display-regexps' contain a string entry equaling or
5350 matching BUFFER-NAME. If `special-display-buffer-names' or
5351 `special-display-regexps' contain a list entry whose car equals
5352 or matches BUFFER-NAME, the return value is the cdr of that
5353 entry."
5354 (let (tmp)
5355 (cond
5356 ((member buffer-name special-display-buffer-names)
5357 t)
5358 ((setq tmp (assoc buffer-name special-display-buffer-names))
5359 (cdr tmp))
5360 ((catch 'found
5361 (dolist (regexp special-display-regexps)
5362 (cond
5363 ((stringp regexp)
5364 (when (string-match-p regexp buffer-name)
5365 (throw 'found t)))
5366 ((and (consp regexp) (stringp (car regexp))
5367 (string-match-p (car regexp) buffer-name))
5368 (throw 'found (cdr regexp))))))))))
5369
5370 (defcustom special-display-frame-alist
5371 '((height . 14) (width . 80) (unsplittable . t))
5372 "Alist of parameters for special frames.
5373 Special frames are used for buffers whose names are listed in
5374 `special-display-buffer-names' and for buffers whose names match
5375 one of the regular expressions in `special-display-regexps'.
5376
5377 This variable can be set in your init file, like this:
5378
5379 (setq special-display-frame-alist '((width . 80) (height . 20)))
5380
5381 These supersede the values given in `default-frame-alist'."
5382 :type '(repeat (cons :format "%v"
5383 (symbol :tag "Parameter")
5384 (sexp :tag "Value")))
5385 :group 'frames)
5386 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
5387
5388 (defun special-display-popup-frame (buffer &optional args)
5389 "Pop up a frame displaying BUFFER and return its window.
5390 If BUFFER is already displayed in a visible or iconified frame,
5391 raise that frame. Otherwise, display BUFFER in a new frame.
5392
5393 Optional argument ARGS is a list specifying additional
5394 information.
5395
5396 If ARGS is an alist, use it as a list of frame parameters. If
5397 these parameters contain (same-window . t), display BUFFER in
5398 the selected window. If they contain (same-frame . t), display
5399 BUFFER in a window of the selected frame.
5400
5401 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5402 function to do the work. Pass it BUFFER as first argument, and
5403 pass the elements of (cdr ARGS) as the remaining arguments."
5404 (if (and args (symbolp (car args)))
5405 (apply (car args) buffer (cdr args))
5406 (let ((window (get-buffer-window buffer 0)))
5407 (or
5408 ;; If we have a window already, make it visible.
5409 (when window
5410 (let ((frame (window-frame window)))
5411 (make-frame-visible frame)
5412 (raise-frame frame)
5413 (display-buffer-record-window 'reuse window buffer)
5414 window))
5415 ;; Reuse the current window if the user requested it.
5416 (when (cdr (assq 'same-window args))
5417 (condition-case nil
5418 (progn (switch-to-buffer buffer nil t) (selected-window))
5419 (error nil)))
5420 ;; Stay on the same frame if requested.
5421 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
5422 (let* ((pop-up-windows t)
5423 pop-up-frames
5424 special-display-buffer-names special-display-regexps)
5425 (display-buffer buffer)))
5426 ;; If no window yet, make one in a new frame.
5427 (let* ((frame
5428 (with-current-buffer buffer
5429 (make-frame (append args special-display-frame-alist))))
5430 (window (frame-selected-window frame)))
5431 (display-buffer-record-window 'frame window buffer)
5432 (unless (eq buffer (window-buffer window))
5433 (set-window-buffer window buffer)
5434 (set-window-prev-buffers window nil))
5435 (set-window-dedicated-p window t)
5436 window)))))
5437
5438 (defcustom special-display-function 'special-display-popup-frame
5439 "Function to call for displaying special buffers.
5440 This function is called with two arguments - the buffer and,
5441 optionally, a list - and should return a window displaying that
5442 buffer. The default value usually makes a separate frame for the
5443 buffer using `special-display-frame-alist' to specify the frame
5444 parameters. See the definition of `special-display-popup-frame'
5445 for how to specify such a function.
5446
5447 A buffer is special when its name is either listed in
5448 `special-display-buffer-names' or matches a regexp in
5449 `special-display-regexps'.
5450
5451 The specified function should call `display-buffer-record-window'
5452 with corresponding arguments to set up the quit-restore parameter
5453 of the window used."
5454 :type 'function
5455 :group 'frames)
5456 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
5457
5458 (defcustom same-window-buffer-names nil
5459 "List of names of buffers that should appear in the \"same\" window.
5460 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5461 on this list in the selected rather than some other window.
5462
5463 An element of this list can be a cons cell instead of just a
5464 string. In that case, the cell's car must be a string specifying
5465 the buffer name. This is for compatibility with
5466 `special-display-buffer-names'; the cdr of the cons cell is
5467 ignored.
5468
5469 See also `same-window-regexps'."
5470 :type '(repeat (string :format "%v"))
5471 :group 'windows)
5472
5473 (defcustom same-window-regexps nil
5474 "List of regexps saying which buffers should appear in the \"same\" window.
5475 `display-buffer' and `pop-to-buffer' show a buffer whose name
5476 matches a regexp on this list in the selected rather than some
5477 other window.
5478
5479 An element of this list can be a cons cell instead of just a
5480 string. In that case, the cell's car must be a regexp matching
5481 the buffer name. This is for compatibility with
5482 `special-display-regexps'; the cdr of the cons cell is ignored.
5483
5484 See also `same-window-buffer-names'."
5485 :type '(repeat (regexp :format "%v"))
5486 :group 'windows)
5487
5488 (defun same-window-p (buffer-name)
5489 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5490 This function returns non-nil if `display-buffer' or
5491 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5492 selected rather than (as usual) some other window. See
5493 `same-window-buffer-names' and `same-window-regexps'."
5494 (cond
5495 ((not (stringp buffer-name)))
5496 ;; The elements of `same-window-buffer-names' can be buffer
5497 ;; names or cons cells whose cars are buffer names.
5498 ((member buffer-name same-window-buffer-names))
5499 ((assoc buffer-name same-window-buffer-names))
5500 ((catch 'found
5501 (dolist (regexp same-window-regexps)
5502 ;; The elements of `same-window-regexps' can be regexps
5503 ;; or cons cells whose cars are regexps.
5504 (when (or (and (stringp regexp)
5505 (string-match-p regexp buffer-name))
5506 (and (consp regexp) (stringp (car regexp))
5507 (string-match-p (car regexp) buffer-name)))
5508 (throw 'found t)))))))
5509
5510 (defcustom pop-up-frames nil
5511 "Whether `display-buffer' should make a separate frame.
5512 If nil, never make a separate frame.
5513 If the value is `graphic-only', make a separate frame
5514 on graphic displays only.
5515 Any other non-nil value means always make a separate frame."
5516 :type '(choice
5517 (const :tag "Never" nil)
5518 (const :tag "On graphic displays only" graphic-only)
5519 (const :tag "Always" t))
5520 :group 'windows)
5521
5522 (defcustom display-buffer-reuse-frames nil
5523 "Non-nil means `display-buffer' should reuse frames.
5524 If the buffer in question is already displayed in a frame, raise
5525 that frame."
5526 :type 'boolean
5527 :version "21.1"
5528 :group 'windows)
5529
5530 (make-obsolete-variable
5531 'display-buffer-reuse-frames
5532 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5533 "24.3")
5534
5535 (defcustom pop-up-windows t
5536 "Non-nil means `display-buffer' should make a new window."
5537 :type 'boolean
5538 :group 'windows)
5539
5540 (defcustom split-window-preferred-function 'split-window-sensibly
5541 "Function called by `display-buffer' routines to split a window.
5542 This function is called with a window as single argument and is
5543 supposed to split that window and return the new window. If the
5544 window can (or shall) not be split, it is supposed to return nil.
5545 The default is to call the function `split-window-sensibly' which
5546 tries to split the window in a way which seems most suitable.
5547 You can customize the options `split-height-threshold' and/or
5548 `split-width-threshold' in order to have `split-window-sensibly'
5549 prefer either vertical or horizontal splitting.
5550
5551 If you set this to any other function, bear in mind that the
5552 `display-buffer' routines may call this function two times. The
5553 argument of the first call is the largest window on its frame.
5554 If that call fails to return a live window, the function is
5555 called again with the least recently used window as argument. If
5556 that call fails too, `display-buffer' will use an existing window
5557 to display its buffer.
5558
5559 The window selected at the time `display-buffer' was invoked is
5560 still selected when this function is called. Hence you can
5561 compare the window argument with the value of `selected-window'
5562 if you intend to split the selected window instead or if you do
5563 not want to split the selected window."
5564 :type 'function
5565 :version "23.1"
5566 :group 'windows)
5567
5568 (defcustom split-height-threshold 80
5569 "Minimum height for splitting windows sensibly.
5570 If this is an integer, `split-window-sensibly' may split a window
5571 vertically only if it has at least this many lines. If this is
5572 nil, `split-window-sensibly' is not allowed to split a window
5573 vertically. If, however, a window is the only window on its
5574 frame, `split-window-sensibly' may split it vertically
5575 disregarding the value of this variable."
5576 :type '(choice (const nil) (integer :tag "lines"))
5577 :version "23.1"
5578 :group 'windows)
5579
5580 (defcustom split-width-threshold 160
5581 "Minimum width for splitting windows sensibly.
5582 If this is an integer, `split-window-sensibly' may split a window
5583 horizontally only if it has at least this many columns. If this
5584 is nil, `split-window-sensibly' is not allowed to split a window
5585 horizontally."
5586 :type '(choice (const nil) (integer :tag "columns"))
5587 :version "23.1"
5588 :group 'windows)
5589
5590 (defun window-splittable-p (window &optional horizontal)
5591 "Return non-nil if `split-window-sensibly' may split WINDOW.
5592 Optional argument HORIZONTAL nil or omitted means check whether
5593 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5594 non-nil means check whether WINDOW may be split horizontally.
5595
5596 WINDOW may be split vertically when the following conditions
5597 hold:
5598 - `window-size-fixed' is either nil or equals `width' for the
5599 buffer of WINDOW.
5600 - `split-height-threshold' is an integer and WINDOW is at least as
5601 high as `split-height-threshold'.
5602 - When WINDOW is split evenly, the emanating windows are at least
5603 `window-min-height' lines tall and can accommodate at least one
5604 line plus - if WINDOW has one - a mode line.
5605
5606 WINDOW may be split horizontally when the following conditions
5607 hold:
5608 - `window-size-fixed' is either nil or equals `height' for the
5609 buffer of WINDOW.
5610 - `split-width-threshold' is an integer and WINDOW is at least as
5611 wide as `split-width-threshold'.
5612 - When WINDOW is split evenly, the emanating windows are at least
5613 `window-min-width' or two (whichever is larger) columns wide."
5614 (when (window-live-p window)
5615 (with-current-buffer (window-buffer window)
5616 (if horizontal
5617 ;; A window can be split horizontally when its width is not
5618 ;; fixed, it is at least `split-width-threshold' columns wide
5619 ;; and at least twice as wide as `window-min-width' and 2 (the
5620 ;; latter value is hardcoded).
5621 (and (memq window-size-fixed '(nil height))
5622 ;; Testing `window-full-width-p' here hardly makes any
5623 ;; sense nowadays. This can be done more intuitively by
5624 ;; setting up `split-width-threshold' appropriately.
5625 (numberp split-width-threshold)
5626 (>= (window-width window)
5627 (max split-width-threshold
5628 (* 2 (max window-min-width 2)))))
5629 ;; A window can be split vertically when its height is not
5630 ;; fixed, it is at least `split-height-threshold' lines high,
5631 ;; and it is at least twice as high as `window-min-height' and 2
5632 ;; if it has a mode line or 1.
5633 (and (memq window-size-fixed '(nil width))
5634 (numberp split-height-threshold)
5635 (>= (window-height window)
5636 (max split-height-threshold
5637 (* 2 (max window-min-height
5638 (if mode-line-format 2 1))))))))))
5639
5640 (defun split-window-sensibly (&optional window)
5641 "Split WINDOW in a way suitable for `display-buffer'.
5642 WINDOW defaults to the currently selected window.
5643 If `split-height-threshold' specifies an integer, WINDOW is at
5644 least `split-height-threshold' lines tall and can be split
5645 vertically, split WINDOW into two windows one above the other and
5646 return the lower window. Otherwise, if `split-width-threshold'
5647 specifies an integer, WINDOW is at least `split-width-threshold'
5648 columns wide and can be split horizontally, split WINDOW into two
5649 windows side by side and return the window on the right. If this
5650 can't be done either and WINDOW is the only window on its frame,
5651 try to split WINDOW vertically disregarding any value specified
5652 by `split-height-threshold'. If that succeeds, return the lower
5653 window. Return nil otherwise.
5654
5655 By default `display-buffer' routines call this function to split
5656 the largest or least recently used window. To change the default
5657 customize the option `split-window-preferred-function'.
5658
5659 You can enforce this function to not split WINDOW horizontally,
5660 by setting (or binding) the variable `split-width-threshold' to
5661 nil. If, in addition, you set `split-height-threshold' to zero,
5662 chances increase that this function does split WINDOW vertically.
5663
5664 In order to not split WINDOW vertically, set (or bind) the
5665 variable `split-height-threshold' to nil. Additionally, you can
5666 set `split-width-threshold' to zero to make a horizontal split
5667 more likely to occur.
5668
5669 Have a look at the function `window-splittable-p' if you want to
5670 know how `split-window-sensibly' determines whether WINDOW can be
5671 split."
5672 (let ((window (or window (selected-window))))
5673 (or (and (window-splittable-p window)
5674 ;; Split window vertically.
5675 (with-selected-window window
5676 (split-window-below)))
5677 (and (window-splittable-p window t)
5678 ;; Split window horizontally.
5679 (with-selected-window window
5680 (split-window-right)))
5681 (and (eq window (frame-root-window (window-frame window)))
5682 (not (window-minibuffer-p window))
5683 ;; If WINDOW is the only window on its frame and is not the
5684 ;; minibuffer window, try to split it vertically disregarding
5685 ;; the value of `split-height-threshold'.
5686 (let ((split-height-threshold 0))
5687 (when (window-splittable-p window)
5688 (with-selected-window window
5689 (split-window-below))))))))
5690
5691 (defun window--try-to-split-window (window &optional alist)
5692 "Try to split WINDOW.
5693 Return value returned by `split-window-preferred-function' if it
5694 represents a live window, nil otherwise."
5695 (and (window-live-p window)
5696 (not (frame-parameter (window-frame window) 'unsplittable))
5697 (let* ((window-combination-limit
5698 ;; When `window-combination-limit' equals
5699 ;; `display-buffer' or equals `resize-window' and a
5700 ;; `window-height' or `window-width' alist entry are
5701 ;; present, bind it to t so resizing steals space
5702 ;; preferably from the window that was split.
5703 (if (or (eq window-combination-limit 'display-buffer)
5704 (and (eq window-combination-limit 'window-size)
5705 (or (cdr (assq 'window-height alist))
5706 (cdr (assq 'window-width alist)))))
5707 t
5708 window-combination-limit))
5709 (new-window
5710 ;; Since `split-window-preferred-function' might
5711 ;; throw an error use `condition-case'.
5712 (condition-case nil
5713 (funcall split-window-preferred-function window)
5714 (error nil))))
5715 (and (window-live-p new-window) new-window))))
5716
5717 (defun window--frame-usable-p (frame)
5718 "Return FRAME if it can be used to display a buffer."
5719 (when (frame-live-p frame)
5720 (let ((window (frame-root-window frame)))
5721 ;; `frame-root-window' may be an internal window which is considered
5722 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5723 ;; implicitly know that `frame' has a visible window we can use.
5724 (unless (and (window-live-p window)
5725 (or (window-minibuffer-p window)
5726 ;; If the window is soft-dedicated, the frame is usable.
5727 ;; Actually, even if the window is really dedicated,
5728 ;; the frame is still usable by splitting it.
5729 ;; At least Emacs-22 allowed it, and it is desirable
5730 ;; when displaying same-frame windows.
5731 nil ; (eq t (window-dedicated-p window))
5732 ))
5733 frame))))
5734
5735 (defcustom even-window-heights t
5736 "If non-nil `display-buffer' will try to even window heights.
5737 Otherwise `display-buffer' will leave the window configuration
5738 alone. Heights are evened only when `display-buffer' chooses a
5739 window that appears above or below the selected window."
5740 :type 'boolean
5741 :group 'windows)
5742
5743 (defun window--even-window-heights (window)
5744 "Even heights of WINDOW and selected window.
5745 Do this only if these windows are vertically adjacent to each
5746 other, `even-window-heights' is non-nil, and the selected window
5747 is higher than WINDOW."
5748 (when (and even-window-heights
5749 ;; Even iff WINDOW forms a vertical combination with the
5750 ;; selected window, and WINDOW's height exceeds that of the
5751 ;; selected window, see also bug#11880.
5752 (window-combined-p window)
5753 (= (window-child-count (window-parent window)) 2)
5754 (eq (window-parent) (window-parent window))
5755 (> (window-total-height) (window-total-height window)))
5756 ;; Don't throw an error if we can't even window heights for
5757 ;; whatever reason.
5758 (condition-case nil
5759 (enlarge-window
5760 (/ (- (window-total-height window) (window-total-height)) 2))
5761 (error nil))))
5762
5763 (defun window--display-buffer (buffer window type &optional alist dedicated)
5764 "Display BUFFER in WINDOW.
5765 TYPE must be one of the symbols `reuse', `window' or `frame' and
5766 is passed unaltered to `display-buffer-record-window'. ALIST is
5767 the alist argument of `display-buffer'. Set `window-dedicated-p'
5768 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5769 live."
5770 (when (and (buffer-live-p buffer) (window-live-p window))
5771 (display-buffer-record-window type window buffer)
5772 (unless (eq buffer (window-buffer window))
5773 (set-window-dedicated-p window nil)
5774 (set-window-buffer window buffer)
5775 (when dedicated
5776 (set-window-dedicated-p window dedicated))
5777 (when (memq type '(window frame))
5778 (set-window-prev-buffers window nil)))
5779 (let ((parameter (window-parameter window 'quit-restore))
5780 (height (cdr (assq 'window-height alist)))
5781 (width (cdr (assq 'window-width alist)))
5782 (size (cdr (assq 'window-size alist))))
5783 (cond
5784 ((or (eq type 'frame)
5785 (and (eq (car parameter) 'same)
5786 (eq (nth 1 parameter) 'frame)))
5787 ;; Adjust size of frame if asked for.
5788 (cond
5789 ((not size))
5790 ((consp size)
5791 (let ((width (car size))
5792 (height (cdr size))
5793 (frame (window-frame window)))
5794 (when (and (numberp width) (numberp height))
5795 (set-frame-height
5796 frame (+ (frame-height frame)
5797 (- height (window-total-height window))))
5798 (set-frame-width
5799 frame (+ (frame-width frame)
5800 (- width (window-total-width window)))))))
5801 ((functionp size)
5802 (ignore-errors (funcall size window)))))
5803 ((or (eq type 'window)
5804 (and (eq (car parameter) 'same)
5805 (eq (nth 1 parameter) 'window)))
5806 ;; Adjust height of window if asked for.
5807 (cond
5808 ((not height))
5809 ((numberp height)
5810 (let* ((new-height
5811 (if (integerp height)
5812 height
5813 (round
5814 (* (window-total-height (frame-root-window window))
5815 height))))
5816 (delta (- new-height (window-total-height window))))
5817 (when (and (window--resizable-p window delta nil 'safe)
5818 (window-combined-p window))
5819 (window-resize window delta nil 'safe))))
5820 ((functionp height)
5821 (ignore-errors (funcall height window))))
5822 ;; Adjust width of window if asked for.
5823 (cond
5824 ((not width))
5825 ((numberp width)
5826 (let* ((new-width
5827 (if (integerp width)
5828 width
5829 (round
5830 (* (window-total-width (frame-root-window window))
5831 width))))
5832 (delta (- new-width (window-total-width window))))
5833 (when (and (window--resizable-p window delta t 'safe)
5834 (window-combined-p window t))
5835 (window-resize window delta t 'safe))))
5836 ((functionp width)
5837 (ignore-errors (funcall width window)))))))
5838
5839 window))
5840
5841 (defun window--maybe-raise-frame (frame)
5842 (let ((visible (frame-visible-p frame)))
5843 (unless (or (not visible)
5844 ;; Assume the selected frame is already visible enough.
5845 (eq frame (selected-frame))
5846 ;; Assume the frame from which we invoked the
5847 ;; minibuffer is visible.
5848 (and (minibuffer-window-active-p (selected-window))
5849 (eq frame (window-frame (minibuffer-selected-window)))))
5850 (raise-frame frame))))
5851
5852 ;; FIXME: Not implemented.
5853 ;; FIXME: By the way, there could be more levels of dedication:
5854 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5855 ;; the window hasn't been used for something else yet.
5856 ;; - `softly' dedicated only allows reuse when asked explicitly.
5857 ;; - `strongly' never allows reuse.
5858 (defvar display-buffer-mark-dedicated nil
5859 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5860 The actual non-nil value of this variable will be copied to the
5861 `window-dedicated-p' flag.")
5862
5863 (defconst display-buffer--action-function-custom-type
5864 '(choice :tag "Function"
5865 (const :tag "--" ignore) ; default for insertion
5866 (const display-buffer-reuse-window)
5867 (const display-buffer-pop-up-window)
5868 (const display-buffer-same-window)
5869 (const display-buffer-pop-up-frame)
5870 (const display-buffer-in-previous-window)
5871 (const display-buffer-use-some-window)
5872 (function :tag "Other function"))
5873 "Custom type for `display-buffer' action functions.")
5874
5875 (defconst display-buffer--action-custom-type
5876 `(cons :tag "Action"
5877 (choice :tag "Action functions"
5878 ,display-buffer--action-function-custom-type
5879 (repeat
5880 :tag "List of functions"
5881 ,display-buffer--action-function-custom-type))
5882 (alist :tag "Action arguments"
5883 :key-type symbol
5884 :value-type (sexp :tag "Value")))
5885 "Custom type for `display-buffer' actions.")
5886
5887 (defvar display-buffer-overriding-action '(nil . nil)
5888 "Overriding action to perform to display a buffer.
5889 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5890 function or a list of functions. Each function should accept two
5891 arguments: a buffer to display and an alist similar to ALIST.
5892 See `display-buffer' for details.")
5893 (put 'display-buffer-overriding-action 'risky-local-variable t)
5894
5895 (defcustom display-buffer-alist nil
5896 "Alist of conditional actions for `display-buffer'.
5897 This is a list of elements (CONDITION . ACTION), where:
5898
5899 CONDITION is either a regexp matching buffer names, or a
5900 function that takes two arguments - a buffer name and the
5901 ACTION argument of `display-buffer' - and returns a boolean.
5902
5903 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5904 function or a list of functions. Each such function should
5905 accept two arguments: a buffer to display and an alist of the
5906 same form as ALIST. See `display-buffer' for details.
5907
5908 `display-buffer' scans this alist until it either finds a
5909 matching regular expression or the function specified by a
5910 condition returns non-nil. In any of these cases, it adds the
5911 associated action to the list of actions it will try."
5912 :type `(alist :key-type
5913 (choice :tag "Condition"
5914 regexp
5915 (function :tag "Matcher function"))
5916 :value-type ,display-buffer--action-custom-type)
5917 :risky t
5918 :version "24.1"
5919 :group 'windows)
5920
5921 (defcustom display-buffer-base-action '(nil . nil)
5922 "User-specified default action for `display-buffer'.
5923 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5924 function or a list of functions. Each function should accept two
5925 arguments: a buffer to display and an alist similar to ALIST.
5926 See `display-buffer' for details."
5927 :type display-buffer--action-custom-type
5928 :risky t
5929 :version "24.1"
5930 :group 'windows)
5931
5932 (defconst display-buffer-fallback-action
5933 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
5934 display-buffer-reuse-window
5935 display-buffer--maybe-pop-up-frame-or-window
5936 display-buffer-in-previous-window
5937 display-buffer-use-some-window
5938 ;; If all else fails, pop up a new frame.
5939 display-buffer-pop-up-frame))
5940 "Default fallback action for `display-buffer'.
5941 This is the action used by `display-buffer' if no other actions
5942 specified, e.g. by the user options `display-buffer-alist' or
5943 `display-buffer-base-action'. See `display-buffer'.")
5944 (put 'display-buffer-fallback-action 'risky-local-variable t)
5945
5946 (defun display-buffer-assq-regexp (buffer-name alist action)
5947 "Retrieve ALIST entry corresponding to BUFFER-NAME.
5948 ACTION is the action argument passed to `display-buffer'."
5949 (catch 'match
5950 (dolist (entry alist)
5951 (let ((key (car entry)))
5952 (when (or (and (stringp key)
5953 (string-match-p key buffer-name))
5954 (and (functionp key)
5955 (funcall key buffer-name action)))
5956 (throw 'match (cdr entry)))))))
5957
5958 (defvar display-buffer--same-window-action
5959 '(display-buffer-same-window
5960 (inhibit-same-window . nil))
5961 "A `display-buffer' action for displaying in the same window.")
5962 (put 'display-buffer--same-window-action 'risky-local-variable t)
5963
5964 (defvar display-buffer--other-frame-action
5965 '((display-buffer-reuse-window
5966 display-buffer-pop-up-frame)
5967 (reusable-frames . 0)
5968 (inhibit-same-window . t))
5969 "A `display-buffer' action for displaying in another frame.")
5970 (put 'display-buffer--other-frame-action 'risky-local-variable t)
5971
5972 (defun display-buffer (buffer-or-name &optional action frame)
5973 "Display BUFFER-OR-NAME in some window, without selecting it.
5974 BUFFER-OR-NAME must be a buffer or the name of an existing
5975 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5976 or nil if no such window is found.
5977
5978 Optional argument ACTION, if non-nil, should specify a display
5979 action. Its form is described below.
5980
5981 Optional argument FRAME, if non-nil, acts like an additional
5982 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
5983 specifying the frame(s) to search for a window that is already
5984 displaying the buffer. See `display-buffer-reuse-window'
5985
5986 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
5987 where FUNCTION is either a function or a list of functions, and
5988 ALIST is an arbitrary association list (alist).
5989
5990 Each such FUNCTION should accept two arguments: the buffer to
5991 display and an alist. Based on those arguments, it should
5992 display the buffer and return the window. If the caller is
5993 prepared to handle the case of not displaying the buffer
5994 and returning nil from `display-buffer' it should pass
5995 \(allow-no-window . t) as an element of the ALIST.
5996
5997 The `display-buffer' function builds a function list and an alist
5998 by combining the functions and alists specified in
5999 `display-buffer-overriding-action', `display-buffer-alist', the
6000 ACTION argument, `display-buffer-base-action', and
6001 `display-buffer-fallback-action' (in order). Then it calls each
6002 function in the combined function list in turn, passing the
6003 buffer as the first argument and the combined alist as the second
6004 argument, until one of the functions returns non-nil.
6005
6006 If ACTION is nil, the function list and the alist are built using
6007 only the other variables mentioned above.
6008
6009 Available action functions include:
6010 `display-buffer-same-window'
6011 `display-buffer-reuse-window'
6012 `display-buffer-pop-up-frame'
6013 `display-buffer-pop-up-window'
6014 `display-buffer-in-previous-window'
6015 `display-buffer-use-some-window'
6016
6017 Recognized alist entries include:
6018
6019 `inhibit-same-window' -- A non-nil value prevents the same
6020 window from being used for display.
6021
6022 `inhibit-switch-frame' -- A non-nil value prevents any other
6023 frame from being raised or selected,
6024 even if the window is displayed there.
6025
6026 `reusable-frames' -- Value specifies frame(s) to search for a
6027 window that already displays the buffer.
6028 See `display-buffer-reuse-window'.
6029
6030 `pop-up-frame-parameters' -- Value specifies an alist of frame
6031 parameters to give a new frame, if
6032 one is created.
6033
6034 `window-height' -- Value specifies either an integer (the number
6035 of lines of a new window), a floating point number (the
6036 fraction of a new window with respect to the height of the
6037 frame's root window) or a function to be called with one
6038 argument - a new window. The function is supposed to adjust
6039 the height of the window; its return value is ignored.
6040 Suitable functions are `shrink-window-if-larger-than-buffer'
6041 and `fit-window-to-buffer'.
6042
6043 `window-width' -- Value specifies either an integer (the number
6044 of columns of a new window), a floating point number (the
6045 fraction of a new window with respect to the width of the
6046 frame's root window) or a function to be called with one
6047 argument - a new window. The function is supposed to adjust
6048 the width of the window; its return value is ignored.
6049
6050 `allow-no-window' -- A non-nil value indicates readiness for the case
6051 of not displaying the buffer and FUNCTION can safely return
6052 a non-window value to suppress displaying.
6053
6054 The ACTION argument to `display-buffer' can also have a non-nil
6055 and non-list value. This means to display the buffer in a window
6056 other than the selected one, even if it is already displayed in
6057 the selected window. If called interactively with a prefix
6058 argument, ACTION is t."
6059 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6060 (if current-prefix-arg t)))
6061 (let ((buffer (if (bufferp buffer-or-name)
6062 buffer-or-name
6063 (get-buffer buffer-or-name)))
6064 ;; Make sure that when we split windows the old window keeps
6065 ;; point, bug#14829.
6066 (split-window-keep-point t)
6067 ;; Handle the old form of the first argument.
6068 (inhibit-same-window (and action (not (listp action)))))
6069 (unless (listp action) (setq action nil))
6070 (if display-buffer-function
6071 ;; If `display-buffer-function' is defined, let it do the job.
6072 (funcall display-buffer-function buffer inhibit-same-window)
6073 ;; Otherwise, use the defined actions.
6074 (let* ((user-action
6075 (display-buffer-assq-regexp
6076 (buffer-name buffer) display-buffer-alist action))
6077 (special-action (display-buffer--special-action buffer))
6078 ;; Extra actions from the arguments to this function:
6079 (extra-action
6080 (cons nil (append (if inhibit-same-window
6081 '((inhibit-same-window . t)))
6082 (if frame
6083 `((reusable-frames . ,frame))))))
6084 ;; Construct action function list and action alist.
6085 (actions (list display-buffer-overriding-action
6086 user-action special-action action extra-action
6087 display-buffer-base-action
6088 display-buffer-fallback-action))
6089 (functions (apply 'append
6090 (mapcar (lambda (x)
6091 (setq x (car x))
6092 (if (functionp x) (list x) x))
6093 actions)))
6094 (alist (apply 'append (mapcar 'cdr actions)))
6095 window)
6096 (unless (buffer-live-p buffer)
6097 (error "Invalid buffer"))
6098 (while (and functions (not window))
6099 (setq window (funcall (car functions) buffer alist)
6100 functions (cdr functions)))
6101 (and (windowp window) window)))))
6102
6103 (defun display-buffer-other-frame (buffer)
6104 "Display buffer BUFFER preferably in another frame.
6105 This uses the function `display-buffer' as a subroutine; see
6106 its documentation for additional customization information."
6107 (interactive "BDisplay buffer in other frame: ")
6108 (display-buffer buffer display-buffer--other-frame-action t))
6109
6110 ;;; `display-buffer' action functions:
6111
6112 (defun display-buffer-same-window (buffer alist)
6113 "Display BUFFER in the selected window.
6114 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6115 if the selected window is a minibuffer window or is dedicated to
6116 another buffer; in that case, return nil. Otherwise, return the
6117 selected window."
6118 (unless (or (cdr (assq 'inhibit-same-window alist))
6119 (window-minibuffer-p)
6120 (window-dedicated-p))
6121 (window--display-buffer buffer (selected-window) 'reuse alist)))
6122
6123 (defun display-buffer--maybe-same-window (buffer alist)
6124 "Conditionally display BUFFER in the selected window.
6125 If `same-window-p' returns non-nil for BUFFER's name, call
6126 `display-buffer-same-window' and return its value. Otherwise,
6127 return nil."
6128 (and (same-window-p (buffer-name buffer))
6129 (display-buffer-same-window buffer alist)))
6130
6131 (defun display-buffer-reuse-window (buffer alist)
6132 "Return a window that is already displaying BUFFER.
6133 Return nil if no usable window is found.
6134
6135 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6136 window is not eligible for reuse.
6137
6138 If ALIST contains a `reusable-frames' entry, its value determines
6139 which frames to search for a reusable window:
6140 nil -- the selected frame (actually the last non-minibuffer frame)
6141 A frame -- just that frame
6142 `visible' -- all visible frames
6143 0 -- all frames on the current terminal
6144 t -- all frames.
6145
6146 If ALIST contains no `reusable-frames' entry, search just the
6147 selected frame if `display-buffer-reuse-frames' and
6148 `pop-up-frames' are both nil; search all frames on the current
6149 terminal if either of those variables is non-nil.
6150
6151 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6152 event that a window on another frame is chosen, avoid raising
6153 that frame."
6154 (let* ((alist-entry (assq 'reusable-frames alist))
6155 (frames (cond (alist-entry (cdr alist-entry))
6156 ((if (eq pop-up-frames 'graphic-only)
6157 (display-graphic-p)
6158 pop-up-frames)
6159 0)
6160 (display-buffer-reuse-frames 0)
6161 (t (last-nonminibuffer-frame))))
6162 (window (if (and (eq buffer (window-buffer))
6163 (not (cdr (assq 'inhibit-same-window alist))))
6164 (selected-window)
6165 (car (delq (selected-window)
6166 (get-buffer-window-list buffer 'nomini
6167 frames))))))
6168 (when (window-live-p window)
6169 (prog1 (window--display-buffer buffer window 'reuse alist)
6170 (unless (cdr (assq 'inhibit-switch-frame alist))
6171 (window--maybe-raise-frame (window-frame window)))))))
6172
6173 (defun display-buffer--special-action (buffer)
6174 "Return special display action for BUFFER, if any.
6175 If `special-display-p' returns non-nil for BUFFER, return an
6176 appropriate display action involving `special-display-function'.
6177 See `display-buffer' for the format of display actions."
6178 (and special-display-function
6179 ;; `special-display-p' returns either t or a list of frame
6180 ;; parameters to pass to `special-display-function'.
6181 (let ((pars (special-display-p (buffer-name buffer))))
6182 (when pars
6183 (list (list #'display-buffer-reuse-window
6184 `(lambda (buffer _alist)
6185 (funcall special-display-function
6186 buffer ',(if (listp pars) pars)))))))))
6187
6188 (defun display-buffer-pop-up-frame (buffer alist)
6189 "Display BUFFER in a new frame.
6190 This works by calling `pop-up-frame-function'. If successful,
6191 return the window used; otherwise return nil.
6192
6193 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6194 raising the new frame.
6195
6196 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6197 corresponding value is an alist of frame parameters to give the
6198 new frame."
6199 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
6200 (pop-up-frame-alist (append params pop-up-frame-alist))
6201 (fun pop-up-frame-function)
6202 frame window)
6203 (when (and fun
6204 ;; Make BUFFER current so `make-frame' will use it as the
6205 ;; new frame's buffer (Bug#15133).
6206 (with-current-buffer buffer
6207 (setq frame (funcall fun)))
6208 (setq window (frame-selected-window frame)))
6209 (prog1 (window--display-buffer
6210 buffer window 'frame alist display-buffer-mark-dedicated)
6211 (unless (cdr (assq 'inhibit-switch-frame alist))
6212 (window--maybe-raise-frame frame))))))
6213
6214 (defun display-buffer-pop-up-window (buffer alist)
6215 "Display BUFFER by popping up a new window.
6216 The new window is created on the selected frame, or in
6217 `last-nonminibuffer-frame' if no windows can be created there.
6218 If successful, return the new window; otherwise return nil.
6219
6220 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6221 event that the new window is created on another frame, avoid
6222 raising the frame."
6223 (let ((frame (or (window--frame-usable-p (selected-frame))
6224 (window--frame-usable-p (last-nonminibuffer-frame))))
6225 window)
6226 (when (and (or (not (frame-parameter frame 'unsplittable))
6227 ;; If the selected frame cannot be split, look at
6228 ;; `last-nonminibuffer-frame'.
6229 (and (eq frame (selected-frame))
6230 (setq frame (last-nonminibuffer-frame))
6231 (window--frame-usable-p frame)
6232 (not (frame-parameter frame 'unsplittable))))
6233 ;; Attempt to split largest or least recently used window.
6234 (setq window (or (window--try-to-split-window
6235 (get-largest-window frame t) alist)
6236 (window--try-to-split-window
6237 (get-lru-window frame t) alist))))
6238 (prog1 (window--display-buffer
6239 buffer window 'window alist display-buffer-mark-dedicated)
6240 (unless (cdr (assq 'inhibit-switch-frame alist))
6241 (window--maybe-raise-frame (window-frame window)))))))
6242
6243 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
6244 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6245
6246 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6247 text-only terminal), try with `display-buffer-pop-up-frame'.
6248
6249 If that cannot be done, and `pop-up-windows' is non-nil, try
6250 again with `display-buffer-pop-up-window'."
6251 (or (and (if (eq pop-up-frames 'graphic-only)
6252 (display-graphic-p)
6253 pop-up-frames)
6254 (display-buffer-pop-up-frame buffer alist))
6255 (and pop-up-windows
6256 (display-buffer-pop-up-window buffer alist))))
6257
6258 (defun display-buffer-below-selected (buffer alist)
6259 "Try displaying BUFFER in a window below the selected window.
6260 This either splits the selected window or reuses the window below
6261 the selected one."
6262 (let (window)
6263 (or (and (not (frame-parameter nil 'unsplittable))
6264 (let ((split-height-threshold 0)
6265 split-width-threshold)
6266 (setq window (window--try-to-split-window (selected-window) alist)))
6267 (window--display-buffer
6268 buffer window 'window alist display-buffer-mark-dedicated))
6269 (and (setq window (window-in-direction 'below))
6270 (not (window-dedicated-p window))
6271 (window--display-buffer
6272 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6273
6274 (defun display-buffer-at-bottom (buffer alist)
6275 "Try displaying BUFFER in a window at the bottom of the selected frame.
6276 This either splits the window at the bottom of the frame or the
6277 frame's root window, or reuses an existing window at the bottom
6278 of the selected frame."
6279 (let (bottom-window window)
6280 (walk-window-tree
6281 (lambda (window) (setq bottom-window window)) nil nil 'nomini)
6282 (or (and (not (frame-parameter nil 'unsplittable))
6283 (let (split-width-threshold)
6284 (setq window (window--try-to-split-window bottom-window alist)))
6285 (window--display-buffer
6286 buffer window 'window alist display-buffer-mark-dedicated))
6287 (and (not (frame-parameter nil 'unsplittable))
6288 (setq window
6289 (condition-case nil
6290 (split-window (window--major-non-side-window))
6291 (error nil)))
6292 (window--display-buffer
6293 buffer window 'window alist display-buffer-mark-dedicated))
6294 (and (setq window bottom-window)
6295 (not (window-dedicated-p window))
6296 (window--display-buffer
6297 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6298
6299 (defun display-buffer-in-previous-window (buffer alist)
6300 "Display BUFFER in a window previously showing it.
6301 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6302 window is not eligible for reuse.
6303
6304 If ALIST contains a `reusable-frames' entry, its value determines
6305 which frames to search for a reusable window:
6306 nil -- the selected frame (actually the last non-minibuffer frame)
6307 A frame -- just that frame
6308 `visible' -- all visible frames
6309 0 -- all frames on the current terminal
6310 t -- all frames.
6311
6312 If ALIST contains no `reusable-frames' entry, search just the
6313 selected frame if `display-buffer-reuse-frames' and
6314 `pop-up-frames' are both nil; search all frames on the current
6315 terminal if either of those variables is non-nil.
6316
6317 If ALIST has a `previous-window' entry, the window specified by
6318 that entry will override any other window found by the methods
6319 above, even if that window never showed BUFFER before."
6320 (let* ((alist-entry (assq 'reusable-frames alist))
6321 (inhibit-same-window
6322 (cdr (assq 'inhibit-same-window alist)))
6323 (frames (cond
6324 (alist-entry (cdr alist-entry))
6325 ((if (eq pop-up-frames 'graphic-only)
6326 (display-graphic-p)
6327 pop-up-frames)
6328 0)
6329 (display-buffer-reuse-frames 0)
6330 (t (last-nonminibuffer-frame))))
6331 best-window second-best-window window)
6332 ;; Scan windows whether they have shown the buffer recently.
6333 (catch 'best
6334 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
6335 (when (and (assq buffer (window-prev-buffers window))
6336 (not (window-dedicated-p window)))
6337 (if (eq window (selected-window))
6338 (unless inhibit-same-window
6339 (setq second-best-window window))
6340 (setq best-window window)
6341 (throw 'best t)))))
6342 ;; When ALIST has a `previous-window' entry, that entry may override
6343 ;; anything we found so far.
6344 (when (and (setq window (cdr (assq 'previous-window alist)))
6345 (window-live-p window)
6346 (not (window-dedicated-p window)))
6347 (if (eq window (selected-window))
6348 (unless inhibit-same-window
6349 (setq second-best-window window))
6350 (setq best-window window)))
6351 ;; Return best or second best window found.
6352 (when (setq window (or best-window second-best-window))
6353 (window--display-buffer buffer window 'reuse alist))))
6354
6355 (defun display-buffer-use-some-window (buffer alist)
6356 "Display BUFFER in an existing window.
6357 Search for a usable window, set that window to the buffer, and
6358 return the window. If no suitable window is found, return nil.
6359
6360 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6361 event that a window in another frame is chosen, avoid raising
6362 that frame."
6363 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
6364 (frame (or (window--frame-usable-p (selected-frame))
6365 (window--frame-usable-p (last-nonminibuffer-frame))))
6366 (window
6367 ;; Reuse an existing window.
6368 (or (get-lru-window frame nil not-this-window)
6369 (let ((window (get-buffer-window buffer 'visible)))
6370 (unless (and not-this-window
6371 (eq window (selected-window)))
6372 window))
6373 (get-largest-window 'visible nil not-this-window)
6374 (let ((window (get-buffer-window buffer 0)))
6375 (unless (and not-this-window
6376 (eq window (selected-window)))
6377 window))
6378 (get-largest-window 0 nil not-this-window)))
6379 (quit-restore (and (window-live-p window)
6380 (window-parameter window 'quit-restore)))
6381 (quad (nth 1 quit-restore)))
6382 (when (window-live-p window)
6383 ;; If the window was used by `display-buffer' before, try to
6384 ;; resize it to its old height but don't signal an error.
6385 (when (and (listp quad)
6386 (integerp (nth 3 quad))
6387 (/= (nth 3 quad) (window-total-height window)))
6388 (condition-case nil
6389 (window-resize window (- (nth 3 quad) (window-total-height window)))
6390 (error nil)))
6391
6392 (prog1
6393 (window--display-buffer buffer window 'reuse alist)
6394 (window--even-window-heights window)
6395 (unless (cdr (assq 'inhibit-switch-frame alist))
6396 (window--maybe-raise-frame (window-frame window)))))))
6397
6398 (defun display-buffer-no-window (_buffer alist)
6399 "Display BUFFER in no window.
6400 If ALIST has a non-nil `allow-no-window' entry, then don't display
6401 a window at all. This makes possible to override the default action
6402 and avoid displaying the buffer. It is assumed that when the caller
6403 specifies a non-nil `allow-no-window' then it can handle a nil value
6404 returned from `display-buffer' in this case."
6405 (when (cdr (assq 'allow-no-window alist))
6406 'fail))
6407
6408 ;;; Display + selection commands:
6409 (defun pop-to-buffer (buffer &optional action norecord)
6410 "Select buffer BUFFER in some window, preferably a different one.
6411 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6412 is a string not naming an existent buffer, create a buffer with
6413 that name. If BUFFER is nil, choose some other buffer. Return
6414 the buffer.
6415
6416 This uses `display-buffer' as a subroutine. The optional ACTION
6417 argument is passed to `display-buffer' as its ACTION argument.
6418 See `display-buffer' for more information. ACTION is t if called
6419 interactively with a prefix argument, which means to pop to a
6420 window other than the selected one even if the buffer is already
6421 displayed in the selected window.
6422
6423 If the window to show BUFFER is not on the selected
6424 frame, raise that window's frame and give it input focus.
6425
6426 Optional third arg NORECORD non-nil means do not put this buffer
6427 at the front of the list of recently selected ones."
6428 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6429 (if current-prefix-arg t)))
6430 (setq buffer (window-normalize-buffer-to-switch-to buffer))
6431 ;; This should be done by `select-window' below.
6432 ;; (set-buffer buffer)
6433 (let* ((old-frame (selected-frame))
6434 (window (display-buffer buffer action))
6435 (frame (window-frame window)))
6436 ;; If we chose another frame, make sure it gets input focus.
6437 (unless (eq frame old-frame)
6438 (select-frame-set-input-focus frame norecord))
6439 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6440 (select-window window norecord)
6441 buffer))
6442
6443 (defun pop-to-buffer-same-window (buffer &optional norecord)
6444 "Select buffer BUFFER in some window, preferably the same one.
6445 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6446 is a string not naming an existent buffer, create a buffer with
6447 that name. If BUFFER is nil, choose some other buffer. Return
6448 the buffer.
6449
6450 Optional argument NORECORD, if non-nil means do not put this
6451 buffer at the front of the list of recently selected ones.
6452
6453 Unlike `pop-to-buffer', this function prefers using the selected
6454 window over popping up a new window or frame."
6455 (pop-to-buffer buffer display-buffer--same-window-action norecord))
6456
6457 (defun read-buffer-to-switch (prompt)
6458 "Read the name of a buffer to switch to, prompting with PROMPT.
6459 Return the name of the buffer as a string.
6460
6461 This function is intended for the `switch-to-buffer' family of
6462 commands since these need to omit the name of the current buffer
6463 from the list of completions and default values."
6464 (let ((rbts-completion-table (internal-complete-buffer-except)))
6465 (minibuffer-with-setup-hook
6466 (lambda ()
6467 (setq minibuffer-completion-table rbts-completion-table)
6468 ;; Since rbts-completion-table is built dynamically, we
6469 ;; can't just add it to the default value of
6470 ;; icomplete-with-completion-tables, so we add it
6471 ;; here manually.
6472 (if (and (boundp 'icomplete-with-completion-tables)
6473 (listp icomplete-with-completion-tables))
6474 (set (make-local-variable 'icomplete-with-completion-tables)
6475 (cons rbts-completion-table
6476 icomplete-with-completion-tables))))
6477 (read-buffer prompt (other-buffer (current-buffer))
6478 (confirm-nonexistent-file-or-buffer)))))
6479
6480 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6481 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6482 If BUFFER-OR-NAME is nil, return the buffer returned by
6483 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6484 exists, return that buffer. If no such buffer exists, create a
6485 buffer with the name BUFFER-OR-NAME and return that buffer."
6486 (if buffer-or-name
6487 (or (get-buffer buffer-or-name)
6488 (let ((buffer (get-buffer-create buffer-or-name)))
6489 (set-buffer-major-mode buffer)
6490 buffer))
6491 (other-buffer)))
6492
6493 (defcustom switch-to-buffer-preserve-window-point nil
6494 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6495 If this is nil, `switch-to-buffer' displays the buffer at that
6496 buffer's `point'. If this is `already-displayed', it tries to
6497 display the buffer at its previous position in the selected
6498 window, provided the buffer is currently displayed in some other
6499 window on any visible or iconified frame. If this is t, it
6500 unconditionally tries to display the buffer at its previous
6501 position in the selected window.
6502
6503 This variable is ignored if the buffer is already displayed in
6504 the selected window or never appeared in it before, or if
6505 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6506 :type '(choice
6507 (const :tag "Never" nil)
6508 (const :tag "If already displayed elsewhere" already-displayed)
6509 (const :tag "Always" t))
6510 :group 'windows
6511 :version "24.3")
6512
6513 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
6514 "Display buffer BUFFER-OR-NAME in the selected window.
6515
6516 WARNING: This is NOT the way to work on another buffer temporarily
6517 within a Lisp program! Use `set-buffer' instead. That avoids
6518 messing with the window-buffer correspondences.
6519
6520 If the selected window cannot display the specified
6521 buffer (e.g. if it is a minibuffer window or strongly dedicated
6522 to another buffer), call `pop-to-buffer' to select the buffer in
6523 another window.
6524
6525 If called interactively, read the buffer name using the
6526 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6527 determines whether to request confirmation before creating a new
6528 buffer.
6529
6530 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6531 If BUFFER-OR-NAME is a string that does not identify an existing
6532 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6533 nil, switch to the buffer returned by `other-buffer'.
6534
6535 If optional argument NORECORD is non-nil, do not put the buffer
6536 at the front of the buffer list, and do not make the window
6537 displaying it the most recently selected one.
6538
6539 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6540 must be displayed in the selected window; if that is impossible,
6541 signal an error rather than calling `pop-to-buffer'.
6542
6543 The option `switch-to-buffer-preserve-window-point' can be used
6544 to make the buffer appear at its last position in the selected
6545 window.
6546
6547 Return the buffer switched to."
6548 (interactive
6549 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
6550 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
6551 (cond
6552 ;; Don't call set-window-buffer if it's not needed since it
6553 ;; might signal an error (e.g. if the window is dedicated).
6554 ((eq buffer (window-buffer)))
6555 ((window-minibuffer-p)
6556 (if force-same-window
6557 (user-error "Cannot switch buffers in minibuffer window")
6558 (pop-to-buffer buffer norecord)))
6559 ((eq (window-dedicated-p) t)
6560 (if force-same-window
6561 (user-error "Cannot switch buffers in a dedicated window")
6562 (pop-to-buffer buffer norecord)))
6563 (t
6564 (let* ((entry (assq buffer (window-prev-buffers)))
6565 (displayed (and (eq switch-to-buffer-preserve-window-point
6566 'already-displayed)
6567 (get-buffer-window buffer 0))))
6568 (set-window-buffer nil buffer)
6569 (when (and entry
6570 (or (eq switch-to-buffer-preserve-window-point t)
6571 displayed))
6572 ;; Try to restore start and point of buffer in the selected
6573 ;; window (Bug#4041).
6574 (set-window-start (selected-window) (nth 1 entry) t)
6575 (set-window-point nil (nth 2 entry))))))
6576
6577 (unless norecord
6578 (select-window (selected-window)))
6579 (set-buffer buffer)))
6580
6581 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
6582 "Select the buffer specified by BUFFER-OR-NAME in another window.
6583 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6584 nil. Return the buffer switched to.
6585
6586 If called interactively, prompt for the buffer name using the
6587 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6588 determines whether to request confirmation before creating a new
6589 buffer.
6590
6591 If BUFFER-OR-NAME is a string and does not identify an existing
6592 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6593 nil, switch to the buffer returned by `other-buffer'.
6594
6595 Optional second argument NORECORD non-nil means do not put this
6596 buffer at the front of the list of recently selected ones.
6597
6598 This uses the function `display-buffer' as a subroutine; see its
6599 documentation for additional customization information."
6600 (interactive
6601 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6602 (let ((pop-up-windows t))
6603 (pop-to-buffer buffer-or-name t norecord)))
6604
6605 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
6606 "Switch to buffer BUFFER-OR-NAME in another frame.
6607 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6608 nil. Return the buffer switched to.
6609
6610 If called interactively, prompt for the buffer name using the
6611 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6612 determines whether to request confirmation before creating a new
6613 buffer.
6614
6615 If BUFFER-OR-NAME is a string and does not identify an existing
6616 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6617 nil, switch to the buffer returned by `other-buffer'.
6618
6619 Optional second arg NORECORD non-nil means do not put this
6620 buffer at the front of the list of recently selected ones.
6621
6622 This uses the function `display-buffer' as a subroutine; see its
6623 documentation for additional customization information."
6624 (interactive
6625 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6626 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
6627 \f
6628 (defun set-window-text-height (window height)
6629 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6630 WINDOW must be a live window and defaults to the selected one.
6631 HEIGHT doesn't include the mode line or header line, if any, or
6632 any partial-height lines in the text display area.
6633
6634 Note that the current implementation of this function cannot
6635 always set the height exactly, but attempts to be conservative,
6636 by allocating more lines than are actually needed in the case
6637 where some error may be present."
6638 (setq window (window-normalize-window window t))
6639 (let ((delta (- height (window-text-height window))))
6640 (unless (zerop delta)
6641 ;; Setting window-min-height to a value like 1 can lead to very
6642 ;; bizarre displays because it also allows Emacs to make *other*
6643 ;; windows one line tall, which means that there's no more space
6644 ;; for the mode line.
6645 (let ((window-min-height (min 2 height)))
6646 (window-resize window delta)))))
6647
6648 (defun enlarge-window-horizontally (delta)
6649 "Make selected window DELTA columns wider.
6650 Interactively, if no argument is given, make selected window one
6651 column wider."
6652 (interactive "p")
6653 (enlarge-window delta t))
6654
6655 (defun shrink-window-horizontally (delta)
6656 "Make selected window DELTA columns narrower.
6657 Interactively, if no argument is given, make selected window one
6658 column narrower."
6659 (interactive "p")
6660 (shrink-window delta t))
6661
6662 (defun count-screen-lines (&optional beg end count-final-newline window)
6663 "Return the number of screen lines in the region.
6664 The number of screen lines may be different from the number of actual lines,
6665 due to line breaking, display table, etc.
6666
6667 Optional arguments BEG and END default to `point-min' and `point-max'
6668 respectively.
6669
6670 If region ends with a newline, ignore it unless optional third argument
6671 COUNT-FINAL-NEWLINE is non-nil.
6672
6673 The optional fourth argument WINDOW specifies the window used for obtaining
6674 parameters such as width, horizontal scrolling, and so on. The default is
6675 to use the selected window's parameters.
6676
6677 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6678 regardless of which buffer is displayed in WINDOW. This makes possible to use
6679 `count-screen-lines' in any buffer, whether or not it is currently displayed
6680 in some window."
6681 (unless beg
6682 (setq beg (point-min)))
6683 (unless end
6684 (setq end (point-max)))
6685 (if (= beg end)
6686 0
6687 (save-excursion
6688 (save-restriction
6689 (widen)
6690 (narrow-to-region (min beg end)
6691 (if (and (not count-final-newline)
6692 (= ?\n (char-before (max beg end))))
6693 (1- (max beg end))
6694 (max beg end)))
6695 (goto-char (point-min))
6696 (1+ (vertical-motion (buffer-size) window))))))
6697
6698 (defun window-buffer-height (window)
6699 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6700 WINDOW must be a live window and defaults to the selected one."
6701 (setq window (window-normalize-window window t))
6702 (with-current-buffer (window-buffer window)
6703 (max 1
6704 (count-screen-lines (point-min) (point-max)
6705 ;; If buffer ends with a newline, ignore it when
6706 ;; counting height unless point is after it.
6707 (eobp)
6708 window))))
6709
6710 ;;; Resizing windows and frames to fit their contents exactly.
6711 (defcustom fit-window-to-buffer-horizontally nil
6712 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6713 If this is nil, `fit-window-to-buffer' never resizes windows
6714 horizontally. If this is `only', it can resize windows
6715 horizontally only. Any other value means `fit-window-to-buffer'
6716 can resize windows in both dimensions."
6717 :type 'boolean
6718 :version "24.4"
6719 :group 'help)
6720
6721 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6722 ;; counting title bar and outer borders.
6723 (defcustom fit-frame-to-buffer nil
6724 "Non-nil means `fit-frame-to-buffer' can fit a frame to its buffer.
6725 A frame is fit if and only if its root window is a live window
6726 and this option is non-nil. If this is `horizontally', frames
6727 are resized horizontally only. If this is `vertically', frames
6728 are resized vertically only. Any other non-nil value means
6729 frames can be resized in both dimensions. See also
6730 `fit-frame-to-buffer-margins' and `fit-frame-to-buffer-sizes'.
6731
6732 If this is non-nil and a window is the only window of its frame,
6733 `fit-window-to-buffer' will invoke `fit-frame-to-buffer' to fit
6734 the frame to its buffer."
6735 :type 'boolean
6736 :version "24.4"
6737 :group 'help)
6738
6739 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
6740 "Margins around frame for `fit-frame-to-buffer'.
6741 This list specifies the numbers of pixels to be left free on the
6742 left, above, the right, and below a frame that shall be fit to
6743 its buffer. The value specified here can be overridden for a
6744 specific frame by that frame's `fit-frame-to-buffer-margins'
6745 parameter, if present.
6746
6747 This variable controls how fitting a frame to the size of its
6748 buffer coordinates with the size of your display. If you don't
6749 specify a value here, the size of the display's workarea is used.
6750
6751 See also `fit-frame-to-buffer-sizes'."
6752 :version "24.4"
6753 :type '(list
6754 (choice
6755 :tag "Left"
6756 :value nil
6757 :format "%[LeftMargin%] %v "
6758 (const :tag "None" :format "%t" nil)
6759 (integer :tag "Pixels" :size 5))
6760 (choice
6761 :tag "Top"
6762 :value nil
6763 :format "%[TopMargin%] %v "
6764 (const :tag "None" :format "%t" nil)
6765 (integer :tag "Pixels" :size 5))
6766 (choice
6767 :tag "Right"
6768 :value nil
6769 :format "%[RightMargin%] %v "
6770 (const :tag "None" :format "%t" nil)
6771 (integer :tag "Pixels" :size 5))
6772 (choice
6773 :tag "Bottom"
6774 :value nil
6775 :format "%[BottomMargin%] %v "
6776 (const :tag "None" :format "%t" nil)
6777 (integer :tag "Pixels" :size 5)))
6778 :group 'help)
6779
6780 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
6781 "Size boundaries of frame for `fit-frame-to-buffer'.
6782 This list specifies the total maximum and minimum lines and
6783 maximum and minimum columns of the root window of any frame that
6784 shall be fit to its buffer. If any of these values is non-nil,
6785 it overrides the corresponding argument of `fit-frame-to-buffer'.
6786
6787 On window systems where the menubar can wrap, fitting a frame to
6788 its buffer may swallow the last line(s). Specifying an
6789 appropriate minimum width value here can avoid such wrapping.
6790
6791 See also `fit-frame-to-buffer-margins'."
6792 :version "24.4"
6793 :type '(list
6794 (choice
6795 :tag "Maximum Height"
6796 :value nil
6797 :format "%[MaxHeight%] %v "
6798 (const :tag "None" :format "%t" nil)
6799 (integer :tag "Lines" :size 5))
6800 (choice
6801 :tag "Minimum Height"
6802 :value nil
6803 :format "%[MinHeight%] %v "
6804 (const :tag "None" :format "%t" nil)
6805 (integer :tag "Lines" :size 5))
6806 (choice
6807 :tag "Maximum Width"
6808 :value nil
6809 :format "%[MaxWidth%] %v "
6810 (const :tag "None" :format "%t" nil)
6811 (integer :tag "Columns" :size 5))
6812 (choice
6813 :tag "Minimum Width"
6814 :value nil
6815 :format "%[MinWidth%] %v\n"
6816 (const :tag "None" :format "%t" nil)
6817 (integer :tag "Columns" :size 5)))
6818 :group 'help)
6819
6820 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
6821
6822 (defun window--sanitize-margin (margin left right)
6823 "Return MARGIN if it's a number between LEFT and RIGHT."
6824 (when (and (numberp margin)
6825 (<= left (- right margin)) (<= margin right))
6826 margin))
6827
6828 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width)
6829 "Adjust size of FRAME to display the contents of its buffer exactly.
6830 FRAME can be any live frame and defaults to the selected one.
6831 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
6832 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
6833 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
6834 `window-min-height' and `window-min-width' respectively.
6835
6836 The option `fit-frame-to-buffer' controls whether this function
6837 has any effect. New position and size of FRAME are additionally
6838 determined by the options `fit-frame-to-buffer-sizes' and
6839 `fit-frame-to-buffer-margins' or the corresponding parameters of
6840 FRAME."
6841 (interactive)
6842 (unless (and (fboundp 'x-display-pixel-height)
6843 ;; We need the respective sizes now.
6844 (fboundp 'display-monitor-attributes-list))
6845 (user-error "Cannot resize frame in non-graphic Emacs"))
6846 (setq frame (window-normalize-frame frame))
6847 (when (and (window-live-p (frame-root-window frame))
6848 fit-frame-to-buffer
6849 (or (not window-size-fixed)
6850 (and (eq window-size-fixed 'height)
6851 (not (eq fit-frame-to-buffer 'vertically)))
6852 (and (eq window-size-fixed 'width)
6853 (not (eq fit-frame-to-buffer 'horizontally)))))
6854 (with-selected-window (frame-root-window frame)
6855 (let* ((window (frame-root-window frame))
6856 (char-width (frame-char-width))
6857 (char-height (frame-char-height))
6858 (monitor-attributes (car (display-monitor-attributes-list
6859 (frame-parameter frame 'display))))
6860 (geometry (cdr (assq 'geometry monitor-attributes)))
6861 (display-width (- (nth 2 geometry) (nth 0 geometry)))
6862 (display-height (- (nth 3 geometry) (nth 1 geometry)))
6863 (workarea (cdr (assq 'workarea monitor-attributes)))
6864 ;; Handle margins.
6865 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
6866 fit-frame-to-buffer-margins))
6867 (left-margin (if (nth 0 margins)
6868 (or (window--sanitize-margin
6869 (nth 0 margins) 0 display-width)
6870 0)
6871 (nth 0 workarea)))
6872 (top-margin (if (nth 1 margins)
6873 (or (window--sanitize-margin
6874 (nth 1 margins) 0 display-height)
6875 0)
6876 (nth 1 workarea)))
6877 (workarea-width (nth 2 workarea))
6878 (right-margin (if (nth 2 margins)
6879 (- display-width
6880 (or (window--sanitize-margin
6881 (nth 2 margins) left-margin display-width)
6882 0))
6883 (nth 2 workarea)))
6884 (workarea-height (nth 3 workarea))
6885 (bottom-margin (if (nth 3 margins)
6886 (- display-height
6887 (or (window--sanitize-margin
6888 (nth 3 margins) top-margin display-height)
6889 0))
6890 (nth 3 workarea)))
6891 ;; The pixel width of FRAME (which does not include the
6892 ;; window manager's decorations).
6893 (frame-width (frame-pixel-width))
6894 ;; The pixel width of the body of FRAME's root window.
6895 (window-body-width (window-body-width nil t))
6896 ;; The difference in pixels between total and body width of
6897 ;; FRAME's window.
6898 (window-extra-width (- (window-pixel-width) window-body-width))
6899 ;; The difference in pixels between the frame's pixel width
6900 ;; and the window's body width. This is the space we can't
6901 ;; use for fitting.
6902 (extra-width (- frame-width window-body-width))
6903 ;; The maximum width we can use for fitting.
6904 (fit-width (- workarea-width extra-width))
6905 ;; The pixel position of FRAME's left border. We usually
6906 ;; try to leave this alone.
6907 (left
6908 (let ((left (frame-parameter nil 'left)))
6909 (if (consp left)
6910 (funcall (car left) (cadr left))
6911 left)))
6912 ;; The pixel height of FRAME (which does not include title
6913 ;; line, decorations, and sometimes neither the menu nor
6914 ;; the toolbar).
6915 (frame-height (frame-pixel-height))
6916 ;; The pixel height of FRAME's root window (we don't care
6917 ;; about the window's body height since the return value of
6918 ;; `window-text-pixel-size' includes header and mode line).
6919 (window-height (window-pixel-height))
6920 ;; The difference in pixels between the frame's pixel
6921 ;; height and the window's height.
6922 (extra-height (- frame-height window-height))
6923 ;; When tool-bar-mode is enabled and we just created a new
6924 ;; frame, reserve lines for toolbar resizing. Needed
6925 ;; because for reasons unknown to me Emacs (1) reserves one
6926 ;; line for the toolbar when making the initial frame and
6927 ;; toolbars are enabled, and (2) later adds the remaining
6928 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
6929 ;; YMMV when you're on a system that behaves differently.
6930 (toolbar-extra-height
6931 (let ((quit-restore (window-parameter window 'quit-restore))
6932 ;; This may have to change when we allow arbitrary
6933 ;; pixel height toolbars.
6934 (lines (tool-bar-height)))
6935 (* char-height
6936 (if (and quit-restore (eq (car quit-restore) 'frame)
6937 (not (zerop lines)))
6938 (1- lines)
6939 0))))
6940 ;; The pixel position of FRAME's top border.
6941 (top
6942 (let ((top (frame-parameter nil 'top)))
6943 (if (consp top)
6944 (funcall (car top) (cadr top))
6945 top)))
6946 ;; Sanitize minimum and maximum sizes.
6947 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
6948 fit-frame-to-buffer-sizes))
6949 (max-height
6950 (cond
6951 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
6952 ((numberp max-height) (* max-height char-height))
6953 (t display-height)))
6954 (min-height
6955 (cond
6956 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
6957 ((numberp min-height) (* min-height char-height))
6958 (t (* window-min-height char-height))))
6959 (max-width
6960 (cond
6961 ((numberp (nth 2 sizes))
6962 (- (* (nth 2 sizes) char-width) window-extra-width))
6963 ((numberp max-width)
6964 (- (* max-width char-width) window-extra-width))
6965 (t display-height)))
6966 (min-width
6967 (cond
6968 ((numberp (nth 3 sizes))
6969 (- (* (nth 3 sizes) char-width) window-extra-width))
6970 ((numberp min-width)
6971 (- (* min-width char-width) window-extra-width))
6972 (t (* window-min-width char-width))))
6973 ;; Note: Currently, for a new frame the sizes of the header
6974 ;; and mode line may be estimated incorrectly
6975 (value (window-text-pixel-size
6976 nil t t workarea-width workarea-height t))
6977 (width (+ (car value) (window-right-divider-width)))
6978 (height (+ (cdr value) (window-bottom-divider-width))))
6979 ;; Don't change height or width when the window's size is fixed
6980 ;; in either direction.
6981 (cond
6982 ((eq window-size-fixed 'width)
6983 (setq width nil))
6984 ((eq window-size-fixed 'height)
6985 (setq height nil)))
6986 ;; Fit width to constraints.
6987 (when width
6988 (unless frame-resize-pixelwise
6989 ;; Round to character sizes.
6990 (setq width (* (/ (+ width char-width -1) char-width)
6991 char-width)))
6992 ;; Fit to maximum and minimum widths.
6993 (setq width (max (min width max-width) min-width))
6994 ;; Add extra width.
6995 (setq width (+ width extra-width))
6996 ;; Preserve margins.
6997 (let ((right (+ left width)))
6998 (cond
6999 ((> right right-margin)
7000 ;; Move frame to left (we don't know its real width).
7001 (setq left (max left-margin (- left (- right right-margin)))))
7002 ((< left left-margin)
7003 ;; Move frame to right.
7004 (setq left left-margin)))))
7005 ;; Fit height to constraints.
7006 (when height
7007 (unless frame-resize-pixelwise
7008 (setq height (* (/ (+ height char-height -1) char-height)
7009 char-height)))
7010 ;; Fit to maximum and minimum heights.
7011 (setq height (max (min height max-height) min-height))
7012 ;; Add extra height.
7013 (setq height (+ height extra-height))
7014 ;; Preserve margins.
7015 (let ((bottom (+ top height)))
7016 (cond
7017 ((> bottom bottom-margin)
7018 ;; Move frame up (we don't know its real height).
7019 (setq top (max top-margin (- top (- bottom bottom-margin)))))
7020 ((< top top-margin)
7021 ;; Move frame down.
7022 (setq top top-margin)))))
7023 ;; Apply changes.
7024 (set-frame-position frame left top)
7025 ;; Clumsily try to translate our calculations to what
7026 ;; `set-frame-size' wants.
7027 (when width
7028 (setq width (- (+ (frame-text-width) width)
7029 extra-width window-body-width)))
7030 (when height
7031 (setq height (- (+ (frame-text-height) height)
7032 extra-height window-height)))
7033 (set-frame-size
7034 frame
7035 (if width
7036 (if frame-resize-pixelwise
7037 width
7038 (/ width char-width))
7039 (frame-text-width))
7040 (if height
7041 (if frame-resize-pixelwise
7042 height
7043 (/ height char-height))
7044 (frame-text-height))
7045 frame-resize-pixelwise)))))
7046
7047 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width)
7048 "Adjust size of WINDOW to display its buffer's contents exactly.
7049 WINDOW must be a live window and defaults to the selected one.
7050
7051 If WINDOW is part of a vertical combination, adjust WINDOW's
7052 height. The new height is calculated from the number of lines of
7053 the accessible portion of its buffer. The optional argument
7054 MAX-HEIGHT specifies a maximum height and defaults to the height
7055 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7056 minimum height and defaults to `window-min-height'. Both
7057 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include the
7058 mode line and header line, if any.
7059
7060 If WINDOW is part of a horizontal combination and the value of
7061 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7062 WINDOW's height. The new width of WINDOW is calculated from the
7063 maximum length of its buffer's lines that follow the current
7064 start position of WINDOW. The optional argument MAX-WIDTH
7065 specifies a maximum width and defaults to the width of WINDOW's
7066 frame. The optional argument MIN-WIDTH specifies a minimum width
7067 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7068 are specified in columns and include fringes, margins and
7069 scrollbars, if any.
7070
7071 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7072 If WINDOW is its frame's root window, then if the option
7073 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7074 adjust the frame's size.
7075
7076 Note that even if this function makes WINDOW large enough to show
7077 _all_ parts of its buffer you might not see the first part when
7078 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7079 not see the top of its buffer unless WINDOW starts at its minimum
7080 accessible position."
7081 (interactive)
7082 (setq window (window-normalize-window window t))
7083 (if (eq window (frame-root-window window))
7084 (when fit-frame-to-buffer
7085 ;; Fit WINDOW's frame to buffer.
7086 (fit-frame-to-buffer
7087 (window-frame window)
7088 max-height min-height max-width min-width))
7089 (with-selected-window window
7090 (let* ((pixelwise window-resize-pixelwise)
7091 (char-height (frame-char-height))
7092 (char-width (frame-char-width))
7093 (total-height (window-size window nil pixelwise))
7094 (body-height (window-body-height window pixelwise))
7095 (body-width (window-body-width window pixelwise))
7096 (min-height
7097 ;; Sanitize MIN-HEIGHT.
7098 (if (numberp min-height)
7099 ;; Can't get smaller than `window-safe-min-height'.
7100 (max (if pixelwise
7101 (* char-height min-height)
7102 min-height)
7103 (if pixelwise
7104 (window-safe-min-pixel-height window)
7105 window-safe-min-height))
7106 ;; Preserve header and mode line if present.
7107 (max (if pixelwise
7108 (* char-height window-min-height)
7109 window-min-height)
7110 (window-min-size nil nil t pixelwise))))
7111 (max-height
7112 ;; Sanitize MAX-HEIGHT.
7113 (if (numberp max-height)
7114 (min
7115 (+ total-height
7116 (window-max-delta
7117 window nil nil nil nil nil pixelwise))
7118 (if pixelwise
7119 (* char-height max-height)
7120 max-height))
7121 (+ total-height (window-max-delta
7122 window nil nil nil nil nil pixelwise))))
7123 height)
7124 (cond
7125 ;; If WINDOW is vertically combined, try to resize it
7126 ;; vertically.
7127 ((and (not (eq fit-window-to-buffer-horizontally 'only))
7128 (not (window-size-fixed-p window))
7129 (window-combined-p))
7130 ;; Vertically we always want to fit the entire buffer.
7131 ;; WINDOW'S height can't get larger than its frame's pixel
7132 ;; height. Its width remains fixed.
7133 (setq height (+ (cdr (window-text-pixel-size
7134 nil nil t nil (frame-pixel-height) t))
7135 (window-bottom-divider-width)))
7136 ;; Round height.
7137 (unless pixelwise
7138 (setq height (/ (+ height char-height -1) char-height)))
7139 (unless (= height total-height)
7140 (window-resize-no-error
7141 window
7142 (- (max min-height (min max-height height)) total-height)
7143 nil window pixelwise)))
7144 ;; If WINDOW is horizontally combined, try to resize it
7145 ;; horizontally.
7146 ((and fit-window-to-buffer-horizontally
7147 (not (window-size-fixed-p window t))
7148 (window-combined-p nil t))
7149 (let* ((total-width (window-size window nil pixelwise))
7150 (min-width
7151 ;; Sanitize MIN-WIDTH.
7152 (if (numberp min-width)
7153 ;; Can't get smaller than `window-safe-min-width'.
7154 (max (if pixelwise
7155 (* char-width min-width)
7156 min-width)
7157 (if pixelwise
7158 (window-safe-min-pixel-width)
7159 window-safe-min-width))
7160 ;; Preserve fringes, margins, scrollbars if present.
7161 (max (if pixelwise
7162 (* char-width window-min-width)
7163 window-min-width)
7164 (window-min-size nil nil t pixelwise))))
7165 (max-width
7166 ;; Sanitize MAX-WIDTH.
7167 (if (numberp max-width)
7168 (min (+ total-width
7169 (window-max-delta
7170 nil t nil nil nil nil pixelwise))
7171 (if pixelwise
7172 (* char-width max-width)
7173 max-width))
7174 (+ total-width (window-max-delta
7175 nil t nil nil nil nil pixelwise))))
7176 ;; When fitting vertically, assume that WINDOW's start
7177 ;; position remains unaltered. WINDOW can't get wider
7178 ;; than its frame's pixel width, its height remains
7179 ;; unaltered.
7180 (width (+ (car (window-text-pixel-size
7181 nil (window-start) (point-max)
7182 (frame-pixel-width)
7183 ;; Add one char-height to assure that
7184 ;; we're on the safe side. This
7185 ;; overshoots when the first line below
7186 ;; the bottom is wider than the window.
7187 (* body-height
7188 (if pixelwise char-height 1))))
7189 (window-right-divider-width))))
7190 (unless pixelwise
7191 (setq width (/ (+ width char-width -1) char-width)))
7192 (unless (= width body-width)
7193 (window-resize-no-error
7194 window
7195 (- (max min-width
7196 (min max-width
7197 (+ total-width (- width body-width))))
7198 total-width)
7199 t window pixelwise)))))))))
7200
7201 (defun window-safely-shrinkable-p (&optional window)
7202 "Return t if WINDOW can be shrunk without shrinking other windows.
7203 WINDOW defaults to the selected window."
7204 (with-selected-window (or window (selected-window))
7205 (let ((edges (window-edges)))
7206 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
7207 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
7208
7209 (defun shrink-window-if-larger-than-buffer (&optional window)
7210 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7211 More precisely, shrink WINDOW vertically to be as small as
7212 possible, while still showing the full contents of its buffer.
7213 WINDOW must be a live window and defaults to the selected one.
7214
7215 Do not shrink WINDOW to less than `window-min-height' lines. Do
7216 nothing if the buffer contains more lines than the present window
7217 height, or if some of the window's contents are scrolled out of
7218 view, or if shrinking this window would also shrink another
7219 window, or if the window is the only window of its frame.
7220
7221 Return non-nil if the window was shrunk, nil otherwise."
7222 (interactive)
7223 (setq window (window-normalize-window window t))
7224 ;; Make sure that WINDOW is vertically combined and `point-min' is
7225 ;; visible (for whatever reason that's needed). The remaining issues
7226 ;; should be taken care of by `fit-window-to-buffer'.
7227 (when (and (window-combined-p window)
7228 (pos-visible-in-window-p (point-min) window))
7229 (fit-window-to-buffer window (window-total-height window))))
7230 \f
7231 (defun kill-buffer-and-window ()
7232 "Kill the current buffer and delete the selected window."
7233 (interactive)
7234 (let ((window-to-delete (selected-window))
7235 (buffer-to-kill (current-buffer))
7236 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7237 (unwind-protect
7238 (progn
7239 (add-hook 'kill-buffer-hook delete-window-hook t t)
7240 (if (kill-buffer (current-buffer))
7241 ;; If `delete-window' failed before, we rerun it to regenerate
7242 ;; the error so it can be seen in the echo area.
7243 (when (eq (selected-window) window-to-delete)
7244 (delete-window))))
7245 ;; If the buffer is not dead for some reason (probably because
7246 ;; of a `quit' signal), remove the hook again.
7247 (ignore-errors
7248 (with-current-buffer buffer-to-kill
7249 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
7250
7251 \f
7252 (defvar recenter-last-op nil
7253 "Indicates the last recenter operation performed.
7254 Possible values: `top', `middle', `bottom', integer or float numbers.")
7255
7256 (defcustom recenter-positions '(middle top bottom)
7257 "Cycling order for `recenter-top-bottom'.
7258 A list of elements with possible values `top', `middle', `bottom',
7259 integer or float numbers that define the cycling order for
7260 the command `recenter-top-bottom'.
7261
7262 Top and bottom destinations are `scroll-margin' lines from the true
7263 window top and bottom. Middle redraws the frame and centers point
7264 vertically within the window. Integer number moves current line to
7265 the specified absolute window-line. Float number between 0.0 and 1.0
7266 means the percentage of the screen space from the top. The default
7267 cycling order is middle -> top -> bottom."
7268 :type '(repeat (choice
7269 (const :tag "Top" top)
7270 (const :tag "Middle" middle)
7271 (const :tag "Bottom" bottom)
7272 (integer :tag "Line number")
7273 (float :tag "Percentage")))
7274 :version "23.2"
7275 :group 'windows)
7276
7277 (defun recenter-top-bottom (&optional arg)
7278 "Move current buffer line to the specified window line.
7279 With no prefix argument, successive calls place point according
7280 to the cycling order defined by `recenter-positions'.
7281
7282 A prefix argument is handled like `recenter':
7283 With numeric prefix ARG, move current line to window-line ARG.
7284 With plain `C-u', move current line to window center."
7285 (interactive "P")
7286 (cond
7287 (arg (recenter arg)) ; Always respect ARG.
7288 (t
7289 (setq recenter-last-op
7290 (if (eq this-command last-command)
7291 (car (or (cdr (member recenter-last-op recenter-positions))
7292 recenter-positions))
7293 (car recenter-positions)))
7294 (let ((this-scroll-margin
7295 (min (max 0 scroll-margin)
7296 (truncate (/ (window-body-height) 4.0)))))
7297 (cond ((eq recenter-last-op 'middle)
7298 (recenter))
7299 ((eq recenter-last-op 'top)
7300 (recenter this-scroll-margin))
7301 ((eq recenter-last-op 'bottom)
7302 (recenter (- -1 this-scroll-margin)))
7303 ((integerp recenter-last-op)
7304 (recenter recenter-last-op))
7305 ((floatp recenter-last-op)
7306 (recenter (round (* recenter-last-op (window-height))))))))))
7307
7308 (define-key global-map [?\C-l] 'recenter-top-bottom)
7309
7310 (defun move-to-window-line-top-bottom (&optional arg)
7311 "Position point relative to window.
7312
7313 With a prefix argument ARG, acts like `move-to-window-line'.
7314
7315 With no argument, positions point at center of window.
7316 Successive calls position point at positions defined
7317 by `recenter-positions'."
7318 (interactive "P")
7319 (cond
7320 (arg (move-to-window-line arg)) ; Always respect ARG.
7321 (t
7322 (setq recenter-last-op
7323 (if (eq this-command last-command)
7324 (car (or (cdr (member recenter-last-op recenter-positions))
7325 recenter-positions))
7326 (car recenter-positions)))
7327 (let ((this-scroll-margin
7328 (min (max 0 scroll-margin)
7329 (truncate (/ (window-body-height) 4.0)))))
7330 (cond ((eq recenter-last-op 'middle)
7331 (call-interactively 'move-to-window-line))
7332 ((eq recenter-last-op 'top)
7333 (move-to-window-line this-scroll-margin))
7334 ((eq recenter-last-op 'bottom)
7335 (move-to-window-line (- -1 this-scroll-margin)))
7336 ((integerp recenter-last-op)
7337 (move-to-window-line recenter-last-op))
7338 ((floatp recenter-last-op)
7339 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
7340
7341 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
7342 \f
7343 ;;; Scrolling commands.
7344
7345 ;;; Scrolling commands which do not signal errors at top/bottom
7346 ;;; of buffer at first key-press (instead move to top/bottom
7347 ;;; of buffer).
7348
7349 (defcustom scroll-error-top-bottom nil
7350 "Move point to top/bottom of buffer before signaling a scrolling error.
7351 A value of nil means just signal an error if no more scrolling possible.
7352 A value of t means point moves to the beginning or the end of the buffer
7353 \(depending on scrolling direction) when no more scrolling possible.
7354 When point is already on that position, then signal an error."
7355 :type 'boolean
7356 :group 'windows
7357 :version "24.1")
7358
7359 (defun scroll-up-command (&optional arg)
7360 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7361 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7362 scroll window further, move cursor to the bottom line.
7363 When point is already on that position, then signal an error.
7364 A near full screen is `next-screen-context-lines' less than a full screen.
7365 Negative ARG means scroll downward.
7366 If ARG is the atom `-', scroll downward by nearly full screen."
7367 (interactive "^P")
7368 (cond
7369 ((null scroll-error-top-bottom)
7370 (scroll-up arg))
7371 ((eq arg '-)
7372 (scroll-down-command nil))
7373 ((< (prefix-numeric-value arg) 0)
7374 (scroll-down-command (- (prefix-numeric-value arg))))
7375 ((eobp)
7376 (scroll-up arg)) ; signal error
7377 (t
7378 (condition-case nil
7379 (scroll-up arg)
7380 (end-of-buffer
7381 (if arg
7382 ;; When scrolling by ARG lines can't be done,
7383 ;; move by ARG lines instead.
7384 (forward-line arg)
7385 ;; When ARG is nil for full-screen scrolling,
7386 ;; move to the bottom of the buffer.
7387 (goto-char (point-max))))))))
7388
7389 (put 'scroll-up-command 'scroll-command t)
7390
7391 (defun scroll-down-command (&optional arg)
7392 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7393 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7394 scroll window further, move cursor to the top line.
7395 When point is already on that position, then signal an error.
7396 A near full screen is `next-screen-context-lines' less than a full screen.
7397 Negative ARG means scroll upward.
7398 If ARG is the atom `-', scroll upward by nearly full screen."
7399 (interactive "^P")
7400 (cond
7401 ((null scroll-error-top-bottom)
7402 (scroll-down arg))
7403 ((eq arg '-)
7404 (scroll-up-command nil))
7405 ((< (prefix-numeric-value arg) 0)
7406 (scroll-up-command (- (prefix-numeric-value arg))))
7407 ((bobp)
7408 (scroll-down arg)) ; signal error
7409 (t
7410 (condition-case nil
7411 (scroll-down arg)
7412 (beginning-of-buffer
7413 (if arg
7414 ;; When scrolling by ARG lines can't be done,
7415 ;; move by ARG lines instead.
7416 (forward-line (- arg))
7417 ;; When ARG is nil for full-screen scrolling,
7418 ;; move to the top of the buffer.
7419 (goto-char (point-min))))))))
7420
7421 (put 'scroll-down-command 'scroll-command t)
7422
7423 ;;; Scrolling commands which scroll a line instead of full screen.
7424
7425 (defun scroll-up-line (&optional arg)
7426 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7427 If ARG is omitted or nil, scroll upward by one line.
7428 This is different from `scroll-up-command' that scrolls a full screen."
7429 (interactive "p")
7430 (scroll-up (or arg 1)))
7431
7432 (put 'scroll-up-line 'scroll-command t)
7433
7434 (defun scroll-down-line (&optional arg)
7435 "Scroll text of selected window down ARG lines; or one line if no ARG.
7436 If ARG is omitted or nil, scroll down by one line.
7437 This is different from `scroll-down-command' that scrolls a full screen."
7438 (interactive "p")
7439 (scroll-down (or arg 1)))
7440
7441 (put 'scroll-down-line 'scroll-command t)
7442
7443 \f
7444 (defun scroll-other-window-down (&optional lines)
7445 "Scroll the \"other window\" down.
7446 For more details, see the documentation for `scroll-other-window'."
7447 (interactive "P")
7448 (scroll-other-window
7449 ;; Just invert the argument's meaning.
7450 ;; We can do that without knowing which window it will be.
7451 (if (eq lines '-) nil
7452 (if (null lines) '-
7453 (- (prefix-numeric-value lines))))))
7454
7455 (defun beginning-of-buffer-other-window (arg)
7456 "Move point to the beginning of the buffer in the other window.
7457 Leave mark at previous position.
7458 With arg N, put point N/10 of the way from the true beginning."
7459 (interactive "P")
7460 (let ((orig-window (selected-window))
7461 (window (other-window-for-scrolling)))
7462 ;; We use unwind-protect rather than save-window-excursion
7463 ;; because the latter would preserve the things we want to change.
7464 (unwind-protect
7465 (progn
7466 (select-window window)
7467 ;; Set point and mark in that window's buffer.
7468 (with-no-warnings
7469 (beginning-of-buffer arg))
7470 ;; Set point accordingly.
7471 (recenter '(t)))
7472 (select-window orig-window))))
7473
7474 (defun end-of-buffer-other-window (arg)
7475 "Move point to the end of the buffer in the other window.
7476 Leave mark at previous position.
7477 With arg N, put point N/10 of the way from the true end."
7478 (interactive "P")
7479 ;; See beginning-of-buffer-other-window for comments.
7480 (let ((orig-window (selected-window))
7481 (window (other-window-for-scrolling)))
7482 (unwind-protect
7483 (progn
7484 (select-window window)
7485 (with-no-warnings
7486 (end-of-buffer arg))
7487 (recenter '(t)))
7488 (select-window orig-window))))
7489 \f
7490 (defvar mouse-autoselect-window-timer nil
7491 "Timer used by delayed window autoselection.")
7492
7493 (defvar mouse-autoselect-window-position nil
7494 "Last mouse position recorded by delayed window autoselection.")
7495
7496 (defvar mouse-autoselect-window-window nil
7497 "Last window recorded by delayed window autoselection.")
7498
7499 (defvar mouse-autoselect-window-state nil
7500 "When non-nil, special state of delayed window autoselection.
7501 Possible values are `suspend' (suspend autoselection after a menu or
7502 scrollbar interaction) and `select' (the next invocation of
7503 `handle-select-window' shall select the window immediately).")
7504
7505 (defun mouse-autoselect-window-cancel (&optional force)
7506 "Cancel delayed window autoselection.
7507 Optional argument FORCE means cancel unconditionally."
7508 (unless (and (not force)
7509 ;; Don't cancel for select-window or select-frame events
7510 ;; or when the user drags a scroll bar.
7511 (or (memq this-command
7512 '(handle-select-window handle-switch-frame))
7513 (and (eq this-command 'scroll-bar-toolkit-scroll)
7514 (memq (nth 4 (event-end last-input-event))
7515 '(handle end-scroll)))))
7516 (setq mouse-autoselect-window-state nil)
7517 (when (timerp mouse-autoselect-window-timer)
7518 (cancel-timer mouse-autoselect-window-timer))
7519 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
7520
7521 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
7522 "Start delayed window autoselection.
7523 MOUSE-POSITION is the last position where the mouse was seen as returned
7524 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7525 window where the mouse was seen. Optional argument SUSPEND non-nil
7526 means suspend autoselection."
7527 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7528 (setq mouse-autoselect-window-position mouse-position)
7529 (when window (setq mouse-autoselect-window-window window))
7530 (setq mouse-autoselect-window-state (when suspend 'suspend))
7531 ;; Install timer which runs `mouse-autoselect-window-select' after
7532 ;; `mouse-autoselect-window' seconds.
7533 (setq mouse-autoselect-window-timer
7534 (run-at-time
7535 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
7536
7537 (defun mouse-autoselect-window-select ()
7538 "Select window with delayed window autoselection.
7539 If the mouse position has stabilized in a non-selected window, select
7540 that window. The minibuffer window is selected only if the minibuffer
7541 is active. This function is run by `mouse-autoselect-window-timer'."
7542 (ignore-errors
7543 (let* ((mouse-position (mouse-position))
7544 (window
7545 (ignore-errors
7546 (window-at (cadr mouse-position) (cddr mouse-position)
7547 (car mouse-position)))))
7548 (cond
7549 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
7550 (and window
7551 (let ((coords (coordinates-in-window-p
7552 (cdr mouse-position) window)))
7553 (and (not (consp coords))
7554 (not (memq coords '(left-margin right-margin)))))))
7555 ;; A menu / popup dialog is active or the mouse is not on the
7556 ;; text region of WINDOW: Suspend autoselection temporarily.
7557 (mouse-autoselect-window-start mouse-position nil t))
7558 ((eq mouse-autoselect-window-state 'suspend)
7559 ;; Delayed autoselection was temporarily suspended, reenable it.
7560 (mouse-autoselect-window-start mouse-position))
7561 ((and window (not (eq window (selected-window)))
7562 (or (not (numberp mouse-autoselect-window))
7563 (and (> mouse-autoselect-window 0)
7564 ;; If `mouse-autoselect-window' is positive, select
7565 ;; window if the window is the same as before.
7566 (eq window mouse-autoselect-window-window))
7567 ;; Otherwise select window if the mouse is at the same
7568 ;; position as before. Observe that the first test after
7569 ;; starting autoselection usually fails since the value of
7570 ;; `mouse-autoselect-window-position' recorded there is the
7571 ;; position where the mouse has entered the new window and
7572 ;; not necessarily where the mouse has stopped moving.
7573 (equal mouse-position mouse-autoselect-window-position))
7574 ;; The minibuffer is a candidate window if it's active.
7575 (or (not (window-minibuffer-p window))
7576 (eq window (active-minibuffer-window))))
7577 ;; Mouse position has stabilized in non-selected window: Cancel
7578 ;; delayed autoselection and try to select that window.
7579 (mouse-autoselect-window-cancel t)
7580 ;; Select window where mouse appears unless the selected window is the
7581 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7582 ;; and post-command hooks and trigger idle timers. To avoid delaying
7583 ;; autoselection again, set `mouse-autoselect-window-state'."
7584 (unless (window-minibuffer-p)
7585 (setq mouse-autoselect-window-state 'select)
7586 (setq unread-command-events
7587 (cons (list 'select-window (list window))
7588 unread-command-events))))
7589 ((or (and window (eq window (selected-window)))
7590 (not (numberp mouse-autoselect-window))
7591 (equal mouse-position mouse-autoselect-window-position))
7592 ;; Mouse position has either stabilized in the selected window or at
7593 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7594 (mouse-autoselect-window-cancel t))
7595 (t
7596 ;; Mouse position has not stabilized yet, resume delayed
7597 ;; autoselection.
7598 (mouse-autoselect-window-start mouse-position window))))))
7599
7600 (defun handle-select-window (event)
7601 "Handle select-window events."
7602 (interactive "e")
7603 (let ((window (posn-window (event-start event))))
7604 (unless (or (not (window-live-p window))
7605 ;; Don't switch if we're currently in the minibuffer.
7606 ;; This tries to work around problems where the
7607 ;; minibuffer gets unselected unexpectedly, and where
7608 ;; you then have to move your mouse all the way down to
7609 ;; the minibuffer to select it.
7610 (window-minibuffer-p)
7611 ;; Don't switch to minibuffer window unless it's active.
7612 (and (window-minibuffer-p window)
7613 (not (minibuffer-window-active-p window)))
7614 ;; Don't switch when autoselection shall be delayed.
7615 (and (numberp mouse-autoselect-window)
7616 (not (zerop mouse-autoselect-window))
7617 (not (eq mouse-autoselect-window-state 'select))
7618 (progn
7619 ;; Cancel any delayed autoselection.
7620 (mouse-autoselect-window-cancel t)
7621 ;; Start delayed autoselection from current mouse
7622 ;; position and window.
7623 (mouse-autoselect-window-start (mouse-position) window)
7624 ;; Executing a command cancels delayed autoselection.
7625 (add-hook
7626 'pre-command-hook 'mouse-autoselect-window-cancel))))
7627 (when mouse-autoselect-window
7628 ;; Reset state of delayed autoselection.
7629 (setq mouse-autoselect-window-state nil)
7630 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7631 (run-hooks 'mouse-leave-buffer-hook))
7632 ;; Clear echo area.
7633 (message nil)
7634 (select-window window))))
7635
7636 (defun truncated-partial-width-window-p (&optional window)
7637 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7638 WINDOW must be a live window and defaults to the selected one.
7639 Return nil if WINDOW is not a partial-width window
7640 (regardless of the value of `truncate-lines').
7641 Otherwise, consult the value of `truncate-partial-width-windows'
7642 for the buffer shown in WINDOW."
7643 (setq window (window-normalize-window window t))
7644 (unless (window-full-width-p window)
7645 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7646 (window-buffer window))))
7647 (if (integerp t-p-w-w)
7648 (< (window-width window) t-p-w-w)
7649 t-p-w-w))))
7650 \f
7651 ;; Some of these are in tutorial--default-keys, so update that if you
7652 ;; change these.
7653 (define-key ctl-x-map "0" 'delete-window)
7654 (define-key ctl-x-map "1" 'delete-other-windows)
7655 (define-key ctl-x-map "2" 'split-window-below)
7656 (define-key ctl-x-map "3" 'split-window-right)
7657 (define-key ctl-x-map "o" 'other-window)
7658 (define-key ctl-x-map "^" 'enlarge-window)
7659 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
7660 (define-key ctl-x-map "{" 'shrink-window-horizontally)
7661 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7662 (define-key ctl-x-map "+" 'balance-windows)
7663 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7664
7665 ;;; window.el ends here