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