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