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