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