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