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