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