]> code.delx.au - gnu-emacs/blob - lisp/window.el
First commit to scratch/follow. Make Isearch work with Follow Mode, etc.
[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.
4866 (window--sanitize-window-sizes frame horizontal)
4867
4868 (run-window-configuration-change-hook frame)
4869 (run-window-scroll-functions new)
4870 (window--check frame)
4871 ;; Always return the new window.
4872 new)))))
4873
4874 ;; I think this should be the default; I think people will prefer it--rms.
4875 (defcustom split-window-keep-point t
4876 "If non-nil, \\[split-window-below] preserves point in the new window.
4877 If nil, adjust point in the two windows to minimize redisplay.
4878 This option applies only to `split-window-below' and functions
4879 that call it. The low-level `split-window' function always keeps
4880 the original point in both windows."
4881 :type 'boolean
4882 :group 'windows)
4883
4884 (defun split-window-below (&optional size)
4885 "Split the selected window into two windows, one above the other.
4886 The selected window is above. The newly split-off window is
4887 below and displays the same buffer. Return the new window.
4888
4889 If optional argument SIZE is omitted or nil, both windows get the
4890 same height, or close to it. If SIZE is positive, the upper
4891 \(selected) window gets SIZE lines. If SIZE is negative, the
4892 lower (new) window gets -SIZE lines.
4893
4894 If the variable `split-window-keep-point' is non-nil, both
4895 windows get the same value of point as the selected window.
4896 Otherwise, the window starts are chosen so as to minimize the
4897 amount of redisplay; this is convenient on slow terminals."
4898 (interactive "P")
4899 (let ((old-window (selected-window))
4900 (old-point (window-point))
4901 (size (and size (prefix-numeric-value size)))
4902 moved-by-window-height moved new-window bottom)
4903 (when (and size (< size 0) (< (- size) window-min-height))
4904 ;; `split-window' would not signal an error here.
4905 (error "Size of new window too small"))
4906 (setq new-window (split-window nil size))
4907 (unless split-window-keep-point
4908 (with-current-buffer (window-buffer)
4909 ;; Use `save-excursion' around vertical movements below
4910 ;; (Bug#10971). Note: When the selected window's buffer has a
4911 ;; header line, up to two lines of the buffer may not show up
4912 ;; in the resulting configuration.
4913 (save-excursion
4914 (goto-char (window-start))
4915 (setq moved (vertical-motion (window-height)))
4916 (set-window-start new-window (point))
4917 (when (> (point) (window-point new-window))
4918 (set-window-point new-window (point)))
4919 (when (= moved (window-height))
4920 (setq moved-by-window-height t)
4921 (vertical-motion -1))
4922 (setq bottom (point)))
4923 (and moved-by-window-height
4924 (<= bottom (point))
4925 (set-window-point old-window (1- bottom)))
4926 (and moved-by-window-height
4927 (<= (window-start new-window) old-point)
4928 (set-window-point new-window old-point)
4929 (select-window new-window))))
4930 ;; Always copy quit-restore parameter in interactive use.
4931 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4932 (when quit-restore
4933 (set-window-parameter new-window 'quit-restore quit-restore)))
4934 new-window))
4935
4936 (defalias 'split-window-vertically 'split-window-below)
4937
4938 (defun split-window-right (&optional size)
4939 "Split the selected window into two side-by-side windows.
4940 The selected window is on the left. The newly split-off window
4941 is on the right and displays the same buffer. Return the new
4942 window.
4943
4944 If optional argument SIZE is omitted or nil, both windows get the
4945 same width, or close to it. If SIZE is positive, the left-hand
4946 \(selected) window gets SIZE columns. If SIZE is negative, the
4947 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4948 the width of the window's scroll bar; if there are no scroll
4949 bars, it includes the width of the divider column to the window's
4950 right, if any."
4951 (interactive "P")
4952 (let ((old-window (selected-window))
4953 (size (and size (prefix-numeric-value size)))
4954 new-window)
4955 (when (and size (< size 0) (< (- size) window-min-width))
4956 ;; `split-window' would not signal an error here.
4957 (error "Size of new window too small"))
4958 (setq new-window (split-window nil size t))
4959 ;; Always copy quit-restore parameter in interactive use.
4960 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4961 (when quit-restore
4962 (set-window-parameter new-window 'quit-restore quit-restore)))
4963 new-window))
4964
4965 (defalias 'split-window-horizontally 'split-window-right)
4966 \f
4967 ;;; Balancing windows.
4968
4969 ;; The following routine uses the recycled code from an old version of
4970 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4971 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4972 ;; more readable (FWIW we'd need three loops - one to calculate the
4973 ;; minimum sizes per window, one to enlarge or shrink windows until the
4974 ;; new parent-size matches, and one where we shrink the largest/enlarge
4975 ;; the smallest window).
4976 (defun balance-windows-2 (window horizontal)
4977 "Subroutine of `balance-windows-1'.
4978 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4979 is non-nil)."
4980 (let* ((char-size (if window-resize-pixelwise
4981 1
4982 (frame-char-size window horizontal)))
4983 (first (window-child window))
4984 (sub first)
4985 (number-of-children 0)
4986 (parent-size (window-new-pixel window))
4987 (total-sum parent-size)
4988 failed size sub-total sub-delta sub-amount rest)
4989 (while sub
4990 (setq number-of-children (1+ number-of-children))
4991 (when (window-size-fixed-p sub horizontal)
4992 (setq total-sum
4993 (- total-sum (window-size sub horizontal t)))
4994 (set-window-new-normal sub 'ignore))
4995 (setq sub (window-right sub)))
4996
4997 (setq failed t)
4998 (while (and failed (> number-of-children 0))
4999 (setq size (/ total-sum number-of-children))
5000 (setq failed nil)
5001 (setq sub first)
5002 (while (and sub (not failed))
5003 ;; Ignore child windows that should be ignored or are stuck.
5004 (unless (window--resize-child-windows-skip-p sub)
5005 (setq sub-total (window-size sub horizontal t))
5006 (setq sub-delta (- size sub-total))
5007 (setq sub-amount
5008 (window-sizable sub sub-delta horizontal nil t))
5009 ;; Register the new total size for this child window.
5010 (set-window-new-pixel sub (+ sub-total sub-amount))
5011 (unless (= sub-amount sub-delta)
5012 (setq total-sum (- total-sum sub-total sub-amount))
5013 (setq number-of-children (1- number-of-children))
5014 ;; We failed and need a new round.
5015 (setq failed t)
5016 (set-window-new-normal sub 'skip)))
5017 (setq sub (window-right sub))))
5018
5019 ;; How can we be sure that `number-of-children' is NOT zero here ?
5020 (setq rest (% total-sum number-of-children))
5021 ;; Fix rounding by trying to enlarge non-stuck windows by one line
5022 ;; (column) until `rest' is zero.
5023 (setq sub first)
5024 (while (and sub (> rest 0))
5025 (unless (window--resize-child-windows-skip-p window)
5026 (set-window-new-pixel sub (min rest char-size) t)
5027 (setq rest (- rest char-size)))
5028 (setq sub (window-right sub)))
5029
5030 ;; Fix rounding by trying to enlarge stuck windows by one line
5031 ;; (column) until `rest' equals zero.
5032 (setq sub first)
5033 (while (and sub (> rest 0))
5034 (unless (eq (window-new-normal sub) 'ignore)
5035 (set-window-new-pixel sub (min rest char-size) t)
5036 (setq rest (- rest char-size)))
5037 (setq sub (window-right sub)))
5038
5039 (setq sub first)
5040 (while sub
5041 ;; Record new normal sizes.
5042 (set-window-new-normal
5043 sub (/ (if (eq (window-new-normal sub) 'ignore)
5044 (window-size sub horizontal t)
5045 (window-new-pixel sub))
5046 (float parent-size)))
5047 ;; Recursively balance each window's child windows.
5048 (balance-windows-1 sub horizontal)
5049 (setq sub (window-right sub)))))
5050
5051 (defun balance-windows-1 (window &optional horizontal)
5052 "Subroutine of `balance-windows'."
5053 (if (window-child window)
5054 (let ((sub (window-child window)))
5055 (if (window-combined-p sub horizontal)
5056 (balance-windows-2 window horizontal)
5057 (let ((size (window-new-pixel window)))
5058 (while sub
5059 (set-window-new-pixel sub size)
5060 (balance-windows-1 sub horizontal)
5061 (setq sub (window-right sub))))))))
5062
5063 (defun balance-windows (&optional window-or-frame)
5064 "Balance the sizes of windows of WINDOW-OR-FRAME.
5065 WINDOW-OR-FRAME is optional and defaults to the selected frame.
5066 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
5067 windows of that frame. If WINDOW-OR-FRAME denotes a window,
5068 recursively balance the sizes of all child windows of that
5069 window."
5070 (interactive)
5071 (let* ((window
5072 (cond
5073 ((or (not window-or-frame)
5074 (frame-live-p window-or-frame))
5075 (frame-root-window window-or-frame))
5076 ((or (window-live-p window-or-frame)
5077 (window-child window-or-frame))
5078 window-or-frame)
5079 (t
5080 (error "Not a window or frame %s" window-or-frame))))
5081 (frame (window-frame window)))
5082 ;; Balance vertically.
5083 (window--resize-reset (window-frame window))
5084 (balance-windows-1 window)
5085 (when (window--resize-apply-p frame)
5086 (window-resize-apply frame)
5087 (window--pixel-to-total frame)
5088 (run-window-configuration-change-hook frame))
5089 ;; Balance horizontally.
5090 (window--resize-reset (window-frame window) t)
5091 (balance-windows-1 window t)
5092 (when (window--resize-apply-p frame t)
5093 (window-resize-apply frame t)
5094 (window--pixel-to-total frame t)
5095 (run-window-configuration-change-hook frame))))
5096
5097 (defun window-fixed-size-p (&optional window direction)
5098 "Return t if WINDOW cannot be resized in DIRECTION.
5099 WINDOW defaults to the selected window. DIRECTION can be
5100 nil (i.e. any), `height' or `width'."
5101 (with-current-buffer (window-buffer window)
5102 (when (and (boundp 'window-size-fixed) window-size-fixed)
5103 (not (and direction
5104 (member (cons direction window-size-fixed)
5105 '((height . width) (width . height))))))))
5106
5107 ;;; A different solution to balance-windows.
5108 (defvar window-area-factor 1
5109 "Factor by which the window area should be over-estimated.
5110 This is used by `balance-windows-area'.
5111 Changing this globally has no effect.")
5112 (make-variable-buffer-local 'window-area-factor)
5113
5114 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
5115 "Wrapper around `window-resize' with error checking.
5116 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
5117 ;; `window-resize' may fail if delta is too large.
5118 (while (>= (abs delta) 1)
5119 (condition-case nil
5120 (progn
5121 ;; It was wrong to use `window-resize' here. Somehow
5122 ;; `balance-windows-area' depends on resizing windows
5123 ;; asymmetrically.
5124 (adjust-window-trailing-edge window delta horizontal pixelwise)
5125 (setq delta 0))
5126 (error
5127 ;;(message "adjust: %s" (error-message-string err))
5128 (setq delta (/ delta 2))))))
5129
5130 (defun balance-windows-area ()
5131 "Make all visible windows the same area (approximately).
5132 See also `window-area-factor' to change the relative size of
5133 specific buffers."
5134 (interactive)
5135 (let* ((unchanged 0) (carry 0) (round 0)
5136 ;; Remove fixed-size windows.
5137 (wins (delq nil (mapcar (lambda (win)
5138 (if (not (window-fixed-size-p win)) win))
5139 (window-list nil 'nomini))))
5140 (changelog nil)
5141 (pixelwise window-resize-pixelwise)
5142 next)
5143 ;; Resizing a window changes the size of surrounding windows in complex
5144 ;; ways, so it's difficult to balance them all. The introduction of
5145 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
5146 ;; very difficult to do. `balance-window' above takes an off-line
5147 ;; approach: get the whole window tree, then balance it, then try to
5148 ;; adjust the windows so they fit the result.
5149 ;; Here, instead, we take a "local optimization" approach, where we just
5150 ;; go through all the windows several times until nothing needs to be
5151 ;; changed. The main problem with this approach is that it's difficult
5152 ;; to make sure it terminates, so we use some heuristic to try and break
5153 ;; off infinite loops.
5154 ;; After a round without any change, we allow a second, to give a chance
5155 ;; to the carry to propagate a minor imbalance from the end back to
5156 ;; the beginning.
5157 (while (< unchanged 2)
5158 ;; (message "New round")
5159 (setq unchanged (1+ unchanged) round (1+ round))
5160 (dolist (win wins)
5161 (setq next win)
5162 (while (progn (setq next (next-window next))
5163 (window-fixed-size-p next)))
5164 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5165 (let* ((horiz
5166 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
5167 (areadiff (/ (- (* (window-size next nil pixelwise)
5168 (window-size next t pixelwise)
5169 (buffer-local-value 'window-area-factor
5170 (window-buffer next)))
5171 (* (window-size win nil pixelwise)
5172 (window-size win t pixelwise)
5173 (buffer-local-value 'window-area-factor
5174 (window-buffer win))))
5175 (max (buffer-local-value 'window-area-factor
5176 (window-buffer win))
5177 (buffer-local-value 'window-area-factor
5178 (window-buffer next)))))
5179 (edgesize (if horiz
5180 (+ (window-size win nil pixelwise)
5181 (window-size next nil pixelwise))
5182 (+ (window-size win t pixelwise)
5183 (window-size next t pixelwise))))
5184 (diff (/ areadiff edgesize)))
5185 (when (zerop diff)
5186 ;; Maybe diff is actually closer to 1 than to 0.
5187 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
5188 (when (and (zerop diff) (not (zerop areadiff)))
5189 (setq diff (/ (+ areadiff carry) edgesize))
5190 ;; Change things smoothly.
5191 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
5192 (if (zerop diff)
5193 ;; Make sure negligible differences don't accumulate to
5194 ;; become significant.
5195 (setq carry (+ carry areadiff))
5196 ;; This used `adjust-window-trailing-edge' before and uses
5197 ;; `window-resize' now. Error wrapping is still needed.
5198 (balance-windows-area-adjust win diff horiz pixelwise)
5199 ;; (sit-for 0.5)
5200 (let ((change (cons win (window-pixel-edges win))))
5201 ;; If the same change has been seen already for this window,
5202 ;; we're most likely in an endless loop, so don't count it as
5203 ;; a change.
5204 (unless (member change changelog)
5205 (push change changelog)
5206 (setq unchanged 0 carry 0)))))))
5207 ;; We've now basically balanced all the windows.
5208 ;; But there may be some minor off-by-one imbalance left over,
5209 ;; so let's do some fine tuning.
5210 ;; (bw-finetune wins)
5211 ;; (message "Done in %d rounds" round)
5212 ))
5213
5214 ;;; Window states, how to get them and how to put them in a window.
5215 (defun window--state-get-1 (window &optional writable)
5216 "Helper function for `window-state-get'."
5217 (let* ((type
5218 (cond
5219 ((window-top-child window) 'vc)
5220 ((window-left-child window) 'hc)
5221 (t 'leaf)))
5222 (buffer (window-buffer window))
5223 (selected (eq window (selected-window)))
5224 (head
5225 `(,type
5226 ,@(unless (window-next-sibling window) `((last . t)))
5227 (pixel-width . ,(window-pixel-width window))
5228 (pixel-height . ,(window-pixel-height window))
5229 (total-width . ,(window-total-width window))
5230 (total-height . ,(window-total-height window))
5231 (normal-height . ,(window-normal-size window))
5232 (normal-width . ,(window-normal-size window t))
5233 ,@(unless (window-live-p window)
5234 `((combination-limit . ,(window-combination-limit window))))
5235 ,@(let ((parameters (window-parameters window))
5236 list)
5237 ;; Make copies of those window parameters whose
5238 ;; persistence property is `writable' if WRITABLE is
5239 ;; non-nil and non-nil if WRITABLE is nil.
5240 (dolist (par parameters)
5241 (let ((pers (cdr (assq (car par)
5242 window-persistent-parameters))))
5243 (when (and pers (or (not writable) (eq pers 'writable)))
5244 (setq list (cons (cons (car par) (cdr par)) list)))))
5245 ;; Add `clone-of' parameter if necessary.
5246 (let ((pers (cdr (assq 'clone-of
5247 window-persistent-parameters))))
5248 (when (and pers (or (not writable) (eq pers 'writable))
5249 (not (assq 'clone-of list)))
5250 (setq list (cons (cons 'clone-of window) list))))
5251 (when list
5252 `((parameters . ,list))))
5253 ,@(when buffer
5254 ;; All buffer related things go in here.
5255 (let ((point (window-point window))
5256 (start (window-start window)))
5257 `((buffer
5258 ,(buffer-name buffer)
5259 (selected . ,selected)
5260 (hscroll . ,(window-hscroll window))
5261 (fringes . ,(window-fringes window))
5262 (margins . ,(window-margins window))
5263 (scroll-bars . ,(window-scroll-bars window))
5264 (vscroll . ,(window-vscroll window))
5265 (dedicated . ,(window-dedicated-p window))
5266 (point . ,(if writable point
5267 (copy-marker point
5268 (buffer-local-value
5269 'window-point-insertion-type
5270 buffer))))
5271 (start . ,(if writable start (copy-marker start)))))))))
5272 (tail
5273 (when (memq type '(vc hc))
5274 (let (list)
5275 (setq window (window-child window))
5276 (while window
5277 (setq list (cons (window--state-get-1 window writable) list))
5278 (setq window (window-right window)))
5279 (nreverse list)))))
5280 (append head tail)))
5281
5282 (defun window-state-get (&optional window writable)
5283 "Return state of WINDOW as a Lisp object.
5284 WINDOW can be any window and defaults to the root window of the
5285 selected frame.
5286
5287 Optional argument WRITABLE non-nil means do not use markers for
5288 sampling `window-point' and `window-start'. Together, WRITABLE
5289 and the variable `window-persistent-parameters' specify which
5290 window parameters are saved by this function. WRITABLE should be
5291 non-nil when the return value shall be written to a file and read
5292 back in another session. Otherwise, an application may run into
5293 an `invalid-read-syntax' error while attempting to read back the
5294 value from file.
5295
5296 The return value can be used as argument for `window-state-put'
5297 to put the state recorded here into an arbitrary window. The
5298 value can be also stored on disk and read back in a new session."
5299 (setq window
5300 (if window
5301 (if (window-valid-p window)
5302 window
5303 (error "%s is not a live or internal window" window))
5304 (frame-root-window)))
5305 ;; The return value is a cons whose car specifies some constraints on
5306 ;; the size of WINDOW. The cdr lists the states of the child windows
5307 ;; of WINDOW.
5308 (cons
5309 ;; Frame related things would go into a function, say `frame-state',
5310 ;; calling `window-state-get' to insert the frame's root window.
5311 `((min-height . ,(window-min-size window))
5312 (min-width . ,(window-min-size window t))
5313 (min-height-ignore . ,(window-min-size window nil t))
5314 (min-width-ignore . ,(window-min-size window t t))
5315 (min-height-safe . ,(window-min-size window nil 'safe))
5316 (min-width-safe . ,(window-min-size window t 'safe))
5317 (min-pixel-height . ,(window-min-size window nil nil t))
5318 (min-pixel-width . ,(window-min-size window t nil t))
5319 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5320 (min-pixel-width-ignore . ,(window-min-size window t t t))
5321 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5322 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5323 (window--state-get-1 window writable)))
5324
5325 (defvar window-state-put-list nil
5326 "Helper variable for `window-state-put'.")
5327
5328 (defvar window-state-put-stale-windows nil
5329 "Helper variable for `window-state-put'.")
5330
5331 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5332 "Helper function for `window-state-put'."
5333 (let ((type (car state)))
5334 (setq state (cdr state))
5335 (cond
5336 ((eq type 'leaf)
5337 ;; For a leaf window just add unprocessed entries to
5338 ;; `window-state-put-list'.
5339 (push (cons window state) window-state-put-list))
5340 ((memq type '(vc hc))
5341 (let* ((horizontal (eq type 'hc))
5342 (total (window-size window horizontal pixelwise))
5343 (first t)
5344 size new)
5345 (dolist (item state)
5346 ;; Find the next child window. WINDOW always points to the
5347 ;; real window that we want to fill with what we find here.
5348 (when (memq (car item) '(leaf vc hc))
5349 (if (assq 'last item)
5350 ;; The last child window. Below `window--state-put-1'
5351 ;; will put into it whatever ITEM has in store.
5352 (setq new nil)
5353 ;; Not the last child window, prepare for splitting
5354 ;; WINDOW. SIZE is the new (and final) size of the old
5355 ;; window.
5356 (setq size
5357 (if totals
5358 ;; Use total size.
5359 (if pixelwise
5360 (cdr (assq (if horizontal
5361 'pixel-width
5362 'pixel-height)
5363 item))
5364 (cdr (assq (if horizontal
5365 'total-width
5366 'total-height)
5367 item)))
5368 ;; Use normalized size and round.
5369 (round
5370 (* total
5371 (cdr (assq (if horizontal 'normal-width 'normal-height)
5372 item))))))
5373
5374 ;; Use safe sizes, we try to resize later.
5375 (setq size (max size
5376 (if horizontal
5377 (* window-safe-min-width
5378 (if pixelwise
5379 (frame-char-width (window-frame window))
5380 1))
5381 (* window-safe-min-height
5382 (if pixelwise
5383 (frame-char-height (window-frame window))
5384 1)))))
5385 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5386 (let* ((window-combination-limit
5387 (assq 'combination-limit item)))
5388 ;; We must inherit the combination limit, otherwise
5389 ;; we might mess up handling of atomic and side
5390 ;; window.
5391 (setq new (split-window window size horizontal pixelwise)))
5392 ;; Give up if we can't resize window down to safe sizes.
5393 (error "Cannot resize window %s" window))
5394
5395 (when first
5396 (setq first nil)
5397 ;; When creating the first child window add for parent
5398 ;; unprocessed entries to `window-state-put-list'.
5399 (setq window-state-put-list
5400 (cons (cons (window-parent window) state)
5401 window-state-put-list))))
5402
5403 ;; Now process the current window (either the one we've just
5404 ;; split or the last child of its parent).
5405 (window--state-put-1 item window ignore totals)
5406 ;; Continue with the last window split off.
5407 (setq window new))))))))
5408
5409 (defun window--state-put-2 (ignore pixelwise)
5410 "Helper function for `window-state-put'."
5411 (dolist (item window-state-put-list)
5412 (let ((window (car item))
5413 (combination-limit (cdr (assq 'combination-limit item)))
5414 (parameters (cdr (assq 'parameters item)))
5415 (state (cdr (assq 'buffer item))))
5416 (when combination-limit
5417 (set-window-combination-limit window combination-limit))
5418 ;; Reset window's parameters and assign saved ones (we might want
5419 ;; a `remove-window-parameters' function here).
5420 (dolist (parameter (window-parameters window))
5421 (set-window-parameter window (car parameter) nil))
5422 (when parameters
5423 (dolist (parameter parameters)
5424 (set-window-parameter window (car parameter) (cdr parameter))))
5425 ;; Process buffer related state.
5426 (when state
5427 (let ((buffer (get-buffer (car state))))
5428 (if buffer
5429 (with-current-buffer buffer
5430 (set-window-buffer window buffer)
5431 (set-window-hscroll window (cdr (assq 'hscroll state)))
5432 (apply 'set-window-fringes
5433 (cons window (cdr (assq 'fringes state))))
5434 (let ((margins (cdr (assq 'margins state))))
5435 (set-window-margins window (car margins) (cdr margins)))
5436 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5437 (set-window-scroll-bars
5438 window (car scroll-bars) (nth 2 scroll-bars)
5439 (nth 3 scroll-bars) (nth 5 scroll-bars)))
5440 (set-window-vscroll window (cdr (assq 'vscroll state)))
5441 ;; Adjust vertically.
5442 (if (memq window-size-fixed '(t height))
5443 ;; A fixed height window, try to restore the
5444 ;; original size.
5445 (let ((delta
5446 (- (cdr (assq
5447 (if pixelwise 'pixel-height 'total-height)
5448 item))
5449 (window-size window nil pixelwise)))
5450 window-size-fixed)
5451 (when (window--resizable-p
5452 window delta nil nil nil nil nil pixelwise)
5453 (window-resize window delta nil nil pixelwise)))
5454 ;; Else check whether the window is not high enough.
5455 (let* ((min-size
5456 (window-min-size window nil ignore pixelwise))
5457 (delta
5458 (- min-size (window-size window nil pixelwise))))
5459 (when (and (> delta 0)
5460 (window--resizable-p
5461 window delta nil ignore nil nil nil pixelwise))
5462 (window-resize window delta nil ignore pixelwise))))
5463 ;; Adjust horizontally.
5464 (if (memq window-size-fixed '(t width))
5465 ;; A fixed width window, try to restore the original
5466 ;; size.
5467 (let ((delta
5468 (- (cdr (assq
5469 (if pixelwise 'pixel-width 'total-width)
5470 item))
5471 (window-size window t pixelwise)))
5472 window-size-fixed)
5473 (when (window--resizable-p
5474 window delta nil nil nil nil nil pixelwise)
5475 (window-resize window delta nil nil pixelwise)))
5476 ;; Else check whether the window is not wide enough.
5477 (let* ((min-size (window-min-size window t ignore pixelwise))
5478 (delta (- min-size (window-size window t pixelwise))))
5479 (when (and (> delta 0)
5480 (window--resizable-p
5481 window delta t ignore nil nil nil pixelwise))
5482 (window-resize window delta t ignore pixelwise))))
5483 ;; Set dedicated status.
5484 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5485 ;; Install positions (maybe we should do this after all
5486 ;; windows have been created and sized).
5487 (ignore-errors
5488 (set-window-start window (cdr (assq 'start state)))
5489 (set-window-point window (cdr (assq 'point state))))
5490 ;; Select window if it's the selected one.
5491 (when (cdr (assq 'selected state))
5492 (select-window window)))
5493 ;; We don't want to raise an error in case the buffer does
5494 ;; not exist anymore, so we switch to a previous one and
5495 ;; save the window with the intention of deleting it later
5496 ;; if possible.
5497 (switch-to-prev-buffer window)
5498 (push window window-state-put-stale-windows)))))))
5499
5500 (defun window-state-put (state &optional window ignore)
5501 "Put window state STATE into WINDOW.
5502 STATE should be the state of a window returned by an earlier
5503 invocation of `window-state-get'. Optional argument WINDOW must
5504 specify a valid window and defaults to the selected one. If
5505 WINDOW is not live, replace WINDOW by a live one before putting
5506 STATE into it.
5507
5508 Optional argument IGNORE non-nil means ignore minimum window
5509 sizes and fixed size restrictions. IGNORE equal `safe' means
5510 windows can get as small as `window-safe-min-height' and
5511 `window-safe-min-width'."
5512 (setq window-state-put-stale-windows nil)
5513 (setq window (window-normalize-window window))
5514
5515 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5516 ;; see Bug#16793.
5517 (unless (window-live-p window)
5518 (let ((root (frame-root-window window)))
5519 (if (eq window root)
5520 (setq window (frame-first-window root))
5521 (setq root window)
5522 (setq window (catch 'live
5523 (walk-window-subtree
5524 (lambda (window)
5525 (when (window-live-p window)
5526 (throw 'live window)))
5527 root))))
5528 (delete-other-windows-internal window root)))
5529
5530 (set-window-dedicated-p window nil)
5531
5532 (let* ((frame (window-frame window))
5533 (head (car state))
5534 ;; We check here (1) whether the total sizes of root window of
5535 ;; STATE and that of WINDOW are equal so we can avoid
5536 ;; calculating new sizes, and (2) if we do have to resize
5537 ;; whether we can do so without violating size restrictions.
5538 (pixelwise (and (cdr (assq 'pixel-width state))
5539 (cdr (assq 'pixel-height state))))
5540 (totals (or (and pixelwise
5541 (= (window-pixel-width window)
5542 (cdr (assq 'pixel-width state)))
5543 (= (window-pixel-height window)
5544 (cdr (assq 'pixel-height state))))
5545 (and (= (window-total-width window)
5546 (cdr (assq 'total-width state)))
5547 (= (window-total-height window)
5548 (cdr (assq 'total-height state))))))
5549 (min-height (cdr (assq
5550 (if pixelwise 'min-pixel-height 'min-height)
5551 head)))
5552 (min-width (cdr (assq
5553 (if pixelwise 'min-pixel-width 'min-weight)
5554 head))))
5555 (if (and (not totals)
5556 (or (> min-height (window-size window nil pixelwise))
5557 (> min-width (window-size window t pixelwise)))
5558 (or (not ignore)
5559 (and (setq min-height
5560 (cdr (assq
5561 (if pixelwise
5562 'min-pixel-height-ignore
5563 'min-height-ignore)
5564 head)))
5565 (setq min-width
5566 (cdr (assq
5567 (if pixelwise
5568 'min-pixel-width-ignore
5569 'min-width-ignore)
5570 head)))
5571 (or (> min-height
5572 (window-size window nil pixelwise))
5573 (> min-width
5574 (window-size window t pixelwise)))
5575 (or (not (eq ignore 'safe))
5576 (and (setq min-height
5577 (cdr (assq
5578 (if pixelwise
5579 'min-pixel-height-safe
5580 'min-height-safe)
5581 head)))
5582 (setq min-width
5583 (cdr (assq
5584 (if pixelwise
5585 'min-pixel-width-safe
5586 'min-width-safe)
5587 head)))
5588 (or (> min-height
5589 (window-size window nil pixelwise))
5590 (> min-width
5591 (window-size window t pixelwise))))))))
5592 ;; The check above might not catch all errors due to rounding
5593 ;; issues - so IGNORE equal 'safe might not always produce the
5594 ;; minimum possible state. But such configurations hardly make
5595 ;; sense anyway.
5596 (error "Window %s too small to accommodate state" window)
5597 (setq state (cdr state))
5598 (setq window-state-put-list nil)
5599 ;; Work on the windows of a temporary buffer to make sure that
5600 ;; splitting proceeds regardless of any buffer local values of
5601 ;; `window-size-fixed'. Release that buffer after the buffers of
5602 ;; all live windows have been set by `window--state-put-2'.
5603 (with-temp-buffer
5604 (set-window-buffer window (current-buffer))
5605 (window--state-put-1 state window nil totals pixelwise)
5606 (window--state-put-2 ignore pixelwise))
5607 (while window-state-put-stale-windows
5608 (let ((window (pop window-state-put-stale-windows)))
5609 (when (eq (window-deletable-p window) t)
5610 (delete-window window))))
5611 (window--check frame))))
5612 \f
5613 (defun display-buffer-record-window (type window buffer)
5614 "Record information for window used by `display-buffer'.
5615 TYPE specifies the type of the calling operation and must be one
5616 of the symbols `reuse' (when WINDOW existed already and was
5617 reused for displaying BUFFER), `window' (when WINDOW was created
5618 on an already existing frame), or `frame' (when WINDOW was
5619 created on a new frame). WINDOW is the window used for or created
5620 by the `display-buffer' routines. BUFFER is the buffer that
5621 shall be displayed.
5622
5623 This function installs or updates the quit-restore parameter of
5624 WINDOW. The quit-restore parameter is a list of four elements:
5625 The first element is one of the symbols `window', `frame', `same' or
5626 `other'. The second element is either one of the symbols `window'
5627 or `frame' or a list whose elements are the buffer previously
5628 shown in the window, that buffer's window start and window point,
5629 and the window's height. The third element is the window
5630 selected at the time the parameter was created. The fourth
5631 element is BUFFER."
5632 (cond
5633 ((eq type 'reuse)
5634 (if (eq (window-buffer window) buffer)
5635 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
5636 ;; parameter, if any.
5637 (let ((quit-restore (window-parameter window 'quit-restore)))
5638 (when (consp quit-restore)
5639 (setcar quit-restore 'same)
5640 ;; The selected-window might have changed in
5641 ;; between (Bug#20353).
5642 (unless (or (eq window (selected-window))
5643 (eq window (nth 2 quit-restore)))
5644 (setcar (cddr quit-restore) (selected-window)))))
5645 ;; WINDOW shows another buffer.
5646 (with-current-buffer (window-buffer window)
5647 (set-window-parameter
5648 window 'quit-restore
5649 (list 'other
5650 ;; A quadruple of WINDOW's buffer, start, point and height.
5651 (list (current-buffer) (window-start window)
5652 ;; Preserve window-point-insertion-type (Bug#12588).
5653 (copy-marker
5654 (window-point window) window-point-insertion-type)
5655 (if (window-combined-p window)
5656 (window-total-height window)
5657 (window-total-width window)))
5658 (selected-window) buffer)))))
5659 ((eq type 'window)
5660 ;; WINDOW has been created on an existing frame.
5661 (set-window-parameter
5662 window 'quit-restore
5663 (list 'window 'window (selected-window) buffer)))
5664 ((eq type 'frame)
5665 ;; WINDOW has been created on a new frame.
5666 (set-window-parameter
5667 window 'quit-restore
5668 (list 'frame 'frame (selected-window) buffer)))))
5669
5670 (defcustom display-buffer-function nil
5671 "If non-nil, function to call to handle `display-buffer'.
5672 It will receive two args, the buffer and a flag which if non-nil
5673 means that the currently selected window is not acceptable. It
5674 should choose or create a window, display the specified buffer in
5675 it, and return the window.
5676
5677 The specified function should call `display-buffer-record-window'
5678 with corresponding arguments to set up the quit-restore parameter
5679 of the window used."
5680 :type '(choice
5681 (const nil)
5682 (function :tag "function"))
5683 :group 'windows)
5684
5685 (make-obsolete-variable 'display-buffer-function
5686 'display-buffer-alist "24.3")
5687
5688 ;; Eventually, we want to turn this into a defvar; instead of
5689 ;; customizing this, the user should use a `pop-up-frame-parameters'
5690 ;; alist entry in `display-buffer-base-action'.
5691 (defcustom pop-up-frame-alist nil
5692 "Alist of parameters for automatically generated new frames.
5693 If non-nil, the value you specify here is used by the default
5694 `pop-up-frame-function' for the creation of new frames.
5695
5696 Since `pop-up-frame-function' is used by `display-buffer' for
5697 making new frames, any value specified here by default affects
5698 the automatic generation of new frames via `display-buffer' and
5699 all functions based on it. The behavior of `make-frame' is not
5700 affected by this variable."
5701 :type '(repeat (cons :format "%v"
5702 (symbol :tag "Parameter")
5703 (sexp :tag "Value")))
5704 :group 'frames)
5705
5706 (defcustom pop-up-frame-function
5707 (lambda () (make-frame pop-up-frame-alist))
5708 "Function used by `display-buffer' for creating a new frame.
5709 This function is called with no arguments and should return a new
5710 frame. The default value calls `make-frame' with the argument
5711 `pop-up-frame-alist'."
5712 :type 'function
5713 :group 'frames)
5714
5715 (defcustom special-display-buffer-names nil
5716 "List of names of buffers that should be displayed specially.
5717 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5718 its name is in this list, displays the buffer in a way specified
5719 by `special-display-function'. `special-display-popup-frame'
5720 \(the default for `special-display-function') usually displays
5721 the buffer in a separate frame made with the parameters specified
5722 by `special-display-frame-alist'. If `special-display-function'
5723 has been set to some other function, that function is called with
5724 the buffer as first, and nil as second argument.
5725
5726 Alternatively, an element of this list can be specified as
5727 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5728 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5729 `special-display-popup-frame' will interpret such pairs as frame
5730 parameters when it creates a special frame, overriding the
5731 corresponding values from `special-display-frame-alist'.
5732
5733 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5734 `special-display-popup-frame' displays that buffer in the
5735 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5736 it displays that buffer in a window on the selected frame.
5737
5738 If `special-display-function' specifies some other function than
5739 `special-display-popup-frame', that function is called with the
5740 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5741 argument.
5742
5743 Finally, an element of this list can be also specified as
5744 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5745 `special-display-popup-frame' will call FUNCTION with the buffer
5746 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5747 second.
5748
5749 Any alternative function specified here is responsible for
5750 setting up the quit-restore parameter of the window used.
5751
5752 If this variable appears \"not to work\", because you added a
5753 name to it but the corresponding buffer is displayed in the
5754 selected window, look at the values of `same-window-buffer-names'
5755 and `same-window-regexps'. Those variables take precedence over
5756 this one.
5757
5758 See also `special-display-regexps'."
5759 :type '(repeat
5760 (choice :tag "Buffer"
5761 :value ""
5762 (string :format "%v")
5763 (cons :tag "With parameters"
5764 :format "%v"
5765 :value ("" . nil)
5766 (string :format "%v")
5767 (repeat :tag "Parameters"
5768 (cons :format "%v"
5769 (symbol :tag "Parameter")
5770 (sexp :tag "Value"))))
5771 (list :tag "With function"
5772 :format "%v"
5773 :value ("" . nil)
5774 (string :format "%v")
5775 (function :tag "Function")
5776 (repeat :tag "Arguments" (sexp)))))
5777 :group 'windows
5778 :group 'frames)
5779 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
5780 (put 'special-display-buffer-names 'risky-local-variable t)
5781
5782 (defcustom special-display-regexps nil
5783 "List of regexps saying which buffers should be displayed specially.
5784 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5785 any regexp in this list matches its name, displays it specially
5786 using `special-display-function'. `special-display-popup-frame'
5787 \(the default for `special-display-function') usually displays
5788 the buffer in a separate frame made with the parameters specified
5789 by `special-display-frame-alist'. If `special-display-function'
5790 has been set to some other function, that function is called with
5791 the buffer as first, and nil as second argument.
5792
5793 Alternatively, an element of this list can be specified as
5794 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5795 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5796 `special-display-popup-frame' will then interpret these pairs as
5797 frame parameters when creating a special frame for a buffer whose
5798 name matches REGEXP, overriding the corresponding values from
5799 `special-display-frame-alist'.
5800
5801 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5802 `special-display-popup-frame' displays buffers matching REGEXP in
5803 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5804 to display such buffers in a window on the selected frame.
5805
5806 If `special-display-function' specifies some other function than
5807 `special-display-popup-frame', that function is called with the
5808 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5809 as second argument.
5810
5811 Finally, an element of this list can be also specified as
5812 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5813 will then call FUNCTION with the buffer whose name matched
5814 REGEXP as first, and OTHER-ARGS as second argument.
5815
5816 Any alternative function specified here is responsible for
5817 setting up the quit-restore parameter of the window used.
5818
5819 If this variable appears \"not to work\", because you added a
5820 name to it but the corresponding buffer is displayed in the
5821 selected window, look at the values of `same-window-buffer-names'
5822 and `same-window-regexps'. Those variables take precedence over
5823 this one.
5824
5825 See also `special-display-buffer-names'."
5826 :type '(repeat
5827 (choice :tag "Buffer"
5828 :value ""
5829 (regexp :format "%v")
5830 (cons :tag "With parameters"
5831 :format "%v"
5832 :value ("" . nil)
5833 (regexp :format "%v")
5834 (repeat :tag "Parameters"
5835 (cons :format "%v"
5836 (symbol :tag "Parameter")
5837 (sexp :tag "Value"))))
5838 (list :tag "With function"
5839 :format "%v"
5840 :value ("" . nil)
5841 (regexp :format "%v")
5842 (function :tag "Function")
5843 (repeat :tag "Arguments" (sexp)))))
5844 :group 'windows
5845 :group 'frames)
5846 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
5847 (put 'special-display-regexps 'risky-local-variable t)
5848
5849 (defun special-display-p (buffer-name)
5850 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5851 More precisely, return t if `special-display-buffer-names' or
5852 `special-display-regexps' contain a string entry equaling or
5853 matching BUFFER-NAME. If `special-display-buffer-names' or
5854 `special-display-regexps' contain a list entry whose car equals
5855 or matches BUFFER-NAME, the return value is the cdr of that
5856 entry."
5857 (let (tmp)
5858 (cond
5859 ((member buffer-name special-display-buffer-names)
5860 t)
5861 ((setq tmp (assoc buffer-name special-display-buffer-names))
5862 (cdr tmp))
5863 ((catch 'found
5864 (dolist (regexp special-display-regexps)
5865 (cond
5866 ((stringp regexp)
5867 (when (string-match-p regexp buffer-name)
5868 (throw 'found t)))
5869 ((and (consp regexp) (stringp (car regexp))
5870 (string-match-p (car regexp) buffer-name))
5871 (throw 'found (cdr regexp))))))))))
5872
5873 (defcustom special-display-frame-alist
5874 '((height . 14) (width . 80) (unsplittable . t))
5875 "Alist of parameters for special frames.
5876 Special frames are used for buffers whose names are listed in
5877 `special-display-buffer-names' and for buffers whose names match
5878 one of the regular expressions in `special-display-regexps'.
5879
5880 This variable can be set in your init file, like this:
5881
5882 (setq special-display-frame-alist \\='((width . 80) (height . 20)))
5883
5884 These supersede the values given in `default-frame-alist'."
5885 :type '(repeat (cons :format "%v"
5886 (symbol :tag "Parameter")
5887 (sexp :tag "Value")))
5888 :group 'frames)
5889 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
5890
5891 (defun special-display-popup-frame (buffer &optional args)
5892 "Pop up a frame displaying BUFFER and return its window.
5893 If BUFFER is already displayed in a visible or iconified frame,
5894 raise that frame. Otherwise, display BUFFER in a new frame.
5895
5896 Optional argument ARGS is a list specifying additional
5897 information.
5898
5899 If ARGS is an alist, use it as a list of frame parameters. If
5900 these parameters contain (same-window . t), display BUFFER in
5901 the selected window. If they contain (same-frame . t), display
5902 BUFFER in a window of the selected frame.
5903
5904 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5905 function to do the work. Pass it BUFFER as first argument, and
5906 pass the elements of (cdr ARGS) as the remaining arguments."
5907 (if (and args (symbolp (car args)))
5908 (apply (car args) buffer (cdr args))
5909 (let ((window (get-buffer-window buffer 0)))
5910 (or
5911 ;; If we have a window already, make it visible.
5912 (when window
5913 (let ((frame (window-frame window)))
5914 (make-frame-visible frame)
5915 (raise-frame frame)
5916 (display-buffer-record-window 'reuse window buffer)
5917 window))
5918 ;; Reuse the current window if the user requested it.
5919 (when (cdr (assq 'same-window args))
5920 (condition-case nil
5921 (progn (switch-to-buffer buffer nil t) (selected-window))
5922 (error nil)))
5923 ;; Stay on the same frame if requested.
5924 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
5925 (let* ((pop-up-windows t)
5926 pop-up-frames
5927 special-display-buffer-names special-display-regexps)
5928 (display-buffer buffer)))
5929 ;; If no window yet, make one in a new frame.
5930 (let* ((frame
5931 (with-current-buffer buffer
5932 (make-frame (append args special-display-frame-alist))))
5933 (window (frame-selected-window frame)))
5934 (display-buffer-record-window 'frame window buffer)
5935 (unless (eq buffer (window-buffer window))
5936 (set-window-buffer window buffer)
5937 (set-window-prev-buffers window nil))
5938 (set-window-dedicated-p window t)
5939 window)))))
5940
5941 (defcustom special-display-function 'special-display-popup-frame
5942 "Function to call for displaying special buffers.
5943 This function is called with two arguments - the buffer and,
5944 optionally, a list - and should return a window displaying that
5945 buffer. The default value usually makes a separate frame for the
5946 buffer using `special-display-frame-alist' to specify the frame
5947 parameters. See the definition of `special-display-popup-frame'
5948 for how to specify such a function.
5949
5950 A buffer is special when its name is either listed in
5951 `special-display-buffer-names' or matches a regexp in
5952 `special-display-regexps'.
5953
5954 The specified function should call `display-buffer-record-window'
5955 with corresponding arguments to set up the quit-restore parameter
5956 of the window used."
5957 :type 'function
5958 :group 'frames)
5959 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
5960
5961 (defcustom same-window-buffer-names nil
5962 "List of names of buffers that should appear in the \"same\" window.
5963 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5964 on this list in the selected rather than some other window.
5965
5966 An element of this list can be a cons cell instead of just a
5967 string. In that case, the cell's car must be a string specifying
5968 the buffer name. This is for compatibility with
5969 `special-display-buffer-names'; the cdr of the cons cell is
5970 ignored.
5971
5972 See also `same-window-regexps'."
5973 :type '(repeat (string :format "%v"))
5974 :group 'windows)
5975
5976 (defcustom same-window-regexps nil
5977 "List of regexps saying which buffers should appear in the \"same\" window.
5978 `display-buffer' and `pop-to-buffer' show a buffer whose name
5979 matches a regexp on this list in the selected rather than some
5980 other window.
5981
5982 An element of this list can be a cons cell instead of just a
5983 string. In that case, the cell's car must be a regexp matching
5984 the buffer name. This is for compatibility with
5985 `special-display-regexps'; the cdr of the cons cell is ignored.
5986
5987 See also `same-window-buffer-names'."
5988 :type '(repeat (regexp :format "%v"))
5989 :group 'windows)
5990
5991 (defun same-window-p (buffer-name)
5992 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5993 This function returns non-nil if `display-buffer' or
5994 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5995 selected rather than (as usual) some other window. See
5996 `same-window-buffer-names' and `same-window-regexps'."
5997 (cond
5998 ((not (stringp buffer-name)))
5999 ;; The elements of `same-window-buffer-names' can be buffer
6000 ;; names or cons cells whose cars are buffer names.
6001 ((member buffer-name same-window-buffer-names))
6002 ((assoc buffer-name same-window-buffer-names))
6003 ((catch 'found
6004 (dolist (regexp same-window-regexps)
6005 ;; The elements of `same-window-regexps' can be regexps
6006 ;; or cons cells whose cars are regexps.
6007 (when (or (and (stringp regexp)
6008 (string-match-p regexp buffer-name))
6009 (and (consp regexp) (stringp (car regexp))
6010 (string-match-p (car regexp) buffer-name)))
6011 (throw 'found t)))))))
6012
6013 (defcustom pop-up-frames nil
6014 "Whether `display-buffer' should make a separate frame.
6015 If nil, never make a separate frame.
6016 If the value is `graphic-only', make a separate frame
6017 on graphic displays only.
6018 Any other non-nil value means always make a separate frame."
6019 :type '(choice
6020 (const :tag "Never" nil)
6021 (const :tag "On graphic displays only" graphic-only)
6022 (const :tag "Always" t))
6023 :group 'windows)
6024
6025 (defcustom display-buffer-reuse-frames nil
6026 "Non-nil means `display-buffer' should reuse frames.
6027 If the buffer in question is already displayed in a frame, raise
6028 that frame."
6029 :type 'boolean
6030 :version "21.1"
6031 :group 'windows)
6032
6033 (make-obsolete-variable
6034 'display-buffer-reuse-frames
6035 "use a `reusable-frames' alist entry in `display-buffer-alist'."
6036 "24.3")
6037
6038 (defcustom pop-up-windows t
6039 "Non-nil means `display-buffer' should make a new window."
6040 :type 'boolean
6041 :group 'windows)
6042
6043 (defcustom split-window-preferred-function 'split-window-sensibly
6044 "Function called by `display-buffer' routines to split a window.
6045 This function is called with a window as single argument and is
6046 supposed to split that window and return the new window. If the
6047 window can (or shall) not be split, it is supposed to return nil.
6048 The default is to call the function `split-window-sensibly' which
6049 tries to split the window in a way which seems most suitable.
6050 You can customize the options `split-height-threshold' and/or
6051 `split-width-threshold' in order to have `split-window-sensibly'
6052 prefer either vertical or horizontal splitting.
6053
6054 If you set this to any other function, bear in mind that the
6055 `display-buffer' routines may call this function two times. The
6056 argument of the first call is the largest window on its frame.
6057 If that call fails to return a live window, the function is
6058 called again with the least recently used window as argument. If
6059 that call fails too, `display-buffer' will use an existing window
6060 to display its buffer.
6061
6062 The window selected at the time `display-buffer' was invoked is
6063 still selected when this function is called. Hence you can
6064 compare the window argument with the value of `selected-window'
6065 if you intend to split the selected window instead or if you do
6066 not want to split the selected window."
6067 :type 'function
6068 :version "23.1"
6069 :group 'windows)
6070
6071 (defcustom split-height-threshold 80
6072 "Minimum height for splitting windows sensibly.
6073 If this is an integer, `split-window-sensibly' may split a window
6074 vertically only if it has at least this many lines. If this is
6075 nil, `split-window-sensibly' is not allowed to split a window
6076 vertically. If, however, a window is the only window on its
6077 frame, `split-window-sensibly' may split it vertically
6078 disregarding the value of this variable."
6079 :type '(choice (const nil) (integer :tag "lines"))
6080 :version "23.1"
6081 :group 'windows)
6082
6083 (defcustom split-width-threshold 160
6084 "Minimum width for splitting windows sensibly.
6085 If this is an integer, `split-window-sensibly' may split a window
6086 horizontally only if it has at least this many columns. If this
6087 is nil, `split-window-sensibly' is not allowed to split a window
6088 horizontally."
6089 :type '(choice (const nil) (integer :tag "columns"))
6090 :version "23.1"
6091 :group 'windows)
6092
6093 (defun window-splittable-p (window &optional horizontal)
6094 "Return non-nil if `split-window-sensibly' may split WINDOW.
6095 Optional argument HORIZONTAL nil or omitted means check whether
6096 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6097 non-nil means check whether WINDOW may be split horizontally.
6098
6099 WINDOW may be split vertically when the following conditions
6100 hold:
6101 - `window-size-fixed' is either nil or equals `width' for the
6102 buffer of WINDOW.
6103 - `split-height-threshold' is an integer and WINDOW is at least as
6104 high as `split-height-threshold'.
6105 - When WINDOW is split evenly, the emanating windows are at least
6106 `window-min-height' lines tall and can accommodate at least one
6107 line plus - if WINDOW has one - a mode line.
6108
6109 WINDOW may be split horizontally when the following conditions
6110 hold:
6111 - `window-size-fixed' is either nil or equals `height' for the
6112 buffer of WINDOW.
6113 - `split-width-threshold' is an integer and WINDOW is at least as
6114 wide as `split-width-threshold'.
6115 - When WINDOW is split evenly, the emanating windows are at least
6116 `window-min-width' or two (whichever is larger) columns wide."
6117 (when (and (window-live-p window) (not (window--side-window-p window)))
6118 (with-current-buffer (window-buffer window)
6119 (if horizontal
6120 ;; A window can be split horizontally when its width is not
6121 ;; fixed, it is at least `split-width-threshold' columns wide
6122 ;; and at least twice as wide as `window-min-width' and 2 (the
6123 ;; latter value is hardcoded).
6124 (and (memq window-size-fixed '(nil height))
6125 ;; Testing `window-full-width-p' here hardly makes any
6126 ;; sense nowadays. This can be done more intuitively by
6127 ;; setting up `split-width-threshold' appropriately.
6128 (numberp split-width-threshold)
6129 (>= (window-width window)
6130 (max split-width-threshold
6131 (* 2 (max window-min-width 2)))))
6132 ;; A window can be split vertically when its height is not
6133 ;; fixed, it is at least `split-height-threshold' lines high,
6134 ;; and it is at least twice as high as `window-min-height' and 2
6135 ;; if it has a mode line or 1.
6136 (and (memq window-size-fixed '(nil width))
6137 (numberp split-height-threshold)
6138 (>= (window-height window)
6139 (max split-height-threshold
6140 (* 2 (max window-min-height
6141 (if mode-line-format 2 1))))))))))
6142
6143 (defun split-window-sensibly (&optional window)
6144 "Split WINDOW in a way suitable for `display-buffer'.
6145 WINDOW defaults to the currently selected window.
6146 If `split-height-threshold' specifies an integer, WINDOW is at
6147 least `split-height-threshold' lines tall and can be split
6148 vertically, split WINDOW into two windows one above the other and
6149 return the lower window. Otherwise, if `split-width-threshold'
6150 specifies an integer, WINDOW is at least `split-width-threshold'
6151 columns wide and can be split horizontally, split WINDOW into two
6152 windows side by side and return the window on the right. If this
6153 can't be done either and WINDOW is the only window on its frame,
6154 try to split WINDOW vertically disregarding any value specified
6155 by `split-height-threshold'. If that succeeds, return the lower
6156 window. Return nil otherwise.
6157
6158 By default `display-buffer' routines call this function to split
6159 the largest or least recently used window. To change the default
6160 customize the option `split-window-preferred-function'.
6161
6162 You can enforce this function to not split WINDOW horizontally,
6163 by setting (or binding) the variable `split-width-threshold' to
6164 nil. If, in addition, you set `split-height-threshold' to zero,
6165 chances increase that this function does split WINDOW vertically.
6166
6167 In order to not split WINDOW vertically, set (or bind) the
6168 variable `split-height-threshold' to nil. Additionally, you can
6169 set `split-width-threshold' to zero to make a horizontal split
6170 more likely to occur.
6171
6172 Have a look at the function `window-splittable-p' if you want to
6173 know how `split-window-sensibly' determines whether WINDOW can be
6174 split."
6175 (let ((window (or window (selected-window))))
6176 (or (and (window-splittable-p window)
6177 ;; Split window vertically.
6178 (with-selected-window window
6179 (split-window-below)))
6180 (and (window-splittable-p window t)
6181 ;; Split window horizontally.
6182 (with-selected-window window
6183 (split-window-right)))
6184 (and (eq window (frame-root-window (window-frame window)))
6185 (not (window-minibuffer-p window))
6186 ;; If WINDOW is the only window on its frame and is not the
6187 ;; minibuffer window, try to split it vertically disregarding
6188 ;; the value of `split-height-threshold'.
6189 (let ((split-height-threshold 0))
6190 (when (window-splittable-p window)
6191 (with-selected-window window
6192 (split-window-below))))))))
6193
6194 (defun window--try-to-split-window (window &optional alist)
6195 "Try to split WINDOW.
6196 Return value returned by `split-window-preferred-function' if it
6197 represents a live window, nil otherwise."
6198 (and (window-live-p window)
6199 (not (frame-parameter (window-frame window) 'unsplittable))
6200 (let* ((window-combination-limit
6201 ;; When `window-combination-limit' equals
6202 ;; `display-buffer' or equals `resize-window' and a
6203 ;; `window-height' or `window-width' alist entry are
6204 ;; present, bind it to t so resizing steals space
6205 ;; preferably from the window that was split.
6206 (if (or (eq window-combination-limit 'display-buffer)
6207 (and (eq window-combination-limit 'window-size)
6208 (or (cdr (assq 'window-height alist))
6209 (cdr (assq 'window-width alist)))))
6210 t
6211 window-combination-limit))
6212 (new-window
6213 ;; Since `split-window-preferred-function' might
6214 ;; throw an error use `condition-case'.
6215 (condition-case nil
6216 (funcall split-window-preferred-function window)
6217 (error nil))))
6218 (and (window-live-p new-window) new-window))))
6219
6220 (defun window--frame-usable-p (frame)
6221 "Return FRAME if it can be used to display a buffer."
6222 (when (frame-live-p frame)
6223 (let ((window (frame-root-window frame)))
6224 ;; `frame-root-window' may be an internal window which is considered
6225 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6226 ;; implicitly know that `frame' has a visible window we can use.
6227 (unless (and (window-live-p window)
6228 (or (window-minibuffer-p window)
6229 ;; If the window is soft-dedicated, the frame is usable.
6230 ;; Actually, even if the window is really dedicated,
6231 ;; the frame is still usable by splitting it.
6232 ;; At least Emacs-22 allowed it, and it is desirable
6233 ;; when displaying same-frame windows.
6234 nil ; (eq t (window-dedicated-p window))
6235 ))
6236 frame))))
6237
6238 (defcustom even-window-sizes t
6239 "If non-nil `display-buffer' will try to even window sizes.
6240 Otherwise `display-buffer' will leave the window configuration
6241 alone. Special values are `height-only' to even heights only and
6242 `width-only' to even widths only. Any other value means to even
6243 any of them."
6244 :type '(choice
6245 (const :tag "Never" nil)
6246 (const :tag "Side-by-side windows only" width-only)
6247 (const :tag "Windows above or below only" height-only)
6248 (const :tag "Always" t))
6249 :version "25.1"
6250 :group 'windows)
6251 (defvaralias 'even-window-heights 'even-window-sizes)
6252
6253 (defun window--even-window-sizes (window)
6254 "Even sizes of WINDOW and selected window.
6255 Even only if these windows are the only children of their parent,
6256 `even-window-sizes' has the appropriate value and the selected
6257 window is larger than WINDOW."
6258 (when (and (= (window-child-count (window-parent window)) 2)
6259 (eq (window-parent) (window-parent window)))
6260 (cond
6261 ((and (not (memq even-window-sizes '(nil height-only)))
6262 (window-combined-p window t)
6263 (> (window-total-width) (window-total-width window)))
6264 (condition-case nil
6265 (enlarge-window
6266 (/ (- (window-total-width window) (window-total-width)) 2) t)
6267 (error nil)))
6268 ((and (not (memq even-window-sizes '(nil width-only)))
6269 (window-combined-p window)
6270 (> (window-total-height) (window-total-height window)))
6271 (condition-case nil
6272 (enlarge-window
6273 (/ (- (window-total-height window) (window-total-height)) 2))
6274 (error nil))))))
6275
6276 (defun window--display-buffer (buffer window type &optional alist dedicated)
6277 "Display BUFFER in WINDOW.
6278 TYPE must be one of the symbols `reuse', `window' or `frame' and
6279 is passed unaltered to `display-buffer-record-window'. ALIST is
6280 the alist argument of `display-buffer'. Set `window-dedicated-p'
6281 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6282 live."
6283 (when (and (buffer-live-p buffer) (window-live-p window))
6284 (display-buffer-record-window type window buffer)
6285 (unless (eq buffer (window-buffer window))
6286 (set-window-dedicated-p window nil)
6287 (set-window-buffer window buffer)
6288 (when dedicated
6289 (set-window-dedicated-p window dedicated))
6290 (when (memq type '(window frame))
6291 (set-window-prev-buffers window nil)))
6292 (let ((parameter (window-parameter window 'quit-restore))
6293 (height (cdr (assq 'window-height alist)))
6294 (width (cdr (assq 'window-width alist)))
6295 (size (cdr (assq 'window-size alist)))
6296 (preserve-size (cdr (assq 'preserve-size alist))))
6297 (cond
6298 ((or (eq type 'frame)
6299 (and (eq (car parameter) 'same)
6300 (eq (nth 1 parameter) 'frame)))
6301 ;; Adjust size of frame if asked for.
6302 (cond
6303 ((not size))
6304 ((consp size)
6305 (let ((width (car size))
6306 (height (cdr size))
6307 (frame (window-frame window)))
6308 (when (and (numberp width) (numberp height))
6309 (set-frame-height
6310 frame (+ (frame-height frame)
6311 (- height (window-total-height window))))
6312 (set-frame-width
6313 frame (+ (frame-width frame)
6314 (- width (window-total-width window)))))))
6315 ((functionp size)
6316 (ignore-errors (funcall size window)))))
6317 ((or (eq type 'window)
6318 (and (eq (car parameter) 'same)
6319 (eq (nth 1 parameter) 'window)))
6320 ;; Adjust height of window if asked for.
6321 (cond
6322 ((not height))
6323 ((numberp height)
6324 (let* ((new-height
6325 (if (integerp height)
6326 height
6327 (round
6328 (* (window-total-height (frame-root-window window))
6329 height))))
6330 (delta (- new-height (window-total-height window))))
6331 (when (and (window--resizable-p window delta nil 'safe)
6332 (window-combined-p window))
6333 (window-resize window delta nil 'safe))))
6334 ((functionp height)
6335 (ignore-errors (funcall height window))))
6336 ;; Adjust width of window if asked for.
6337 (cond
6338 ((not width))
6339 ((numberp width)
6340 (let* ((new-width
6341 (if (integerp width)
6342 width
6343 (round
6344 (* (window-total-width (frame-root-window window))
6345 width))))
6346 (delta (- new-width (window-total-width window))))
6347 (when (and (window--resizable-p window delta t 'safe)
6348 (window-combined-p window t))
6349 (window-resize window delta t 'safe))))
6350 ((functionp width)
6351 (ignore-errors (funcall width window))))
6352 ;; Preserve window size if asked for.
6353 (when (consp preserve-size)
6354 (window-preserve-size window t (car preserve-size))
6355 (window-preserve-size window nil (cdr preserve-size))))))
6356
6357 window))
6358
6359 (defun window--maybe-raise-frame (frame)
6360 (let ((visible (frame-visible-p frame)))
6361 (unless (or (not visible)
6362 ;; Assume the selected frame is already visible enough.
6363 (eq frame (selected-frame))
6364 ;; Assume the frame from which we invoked the
6365 ;; minibuffer is visible.
6366 (and (minibuffer-window-active-p (selected-window))
6367 (eq frame (window-frame (minibuffer-selected-window)))))
6368 (raise-frame frame))))
6369
6370 ;; FIXME: Not implemented.
6371 ;; FIXME: By the way, there could be more levels of dedication:
6372 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6373 ;; the window hasn't been used for something else yet.
6374 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6375 ;; - `strongly' never allows reuse.
6376 (defvar display-buffer-mark-dedicated nil
6377 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6378 The actual non-nil value of this variable will be copied to the
6379 `window-dedicated-p' flag.")
6380
6381 (defconst display-buffer--action-function-custom-type
6382 '(choice :tag "Function"
6383 (const :tag "--" ignore) ; default for insertion
6384 (const display-buffer-reuse-window)
6385 (const display-buffer-pop-up-window)
6386 (const display-buffer-same-window)
6387 (const display-buffer-pop-up-frame)
6388 (const display-buffer-below-selected)
6389 (const display-buffer-at-bottom)
6390 (const display-buffer-in-previous-window)
6391 (const display-buffer-use-some-window)
6392 (const display-buffer-use-some-frame)
6393 (function :tag "Other function"))
6394 "Custom type for `display-buffer' action functions.")
6395
6396 (defconst display-buffer--action-custom-type
6397 `(cons :tag "Action"
6398 (choice :tag "Action functions"
6399 ,display-buffer--action-function-custom-type
6400 (repeat
6401 :tag "List of functions"
6402 ,display-buffer--action-function-custom-type))
6403 (alist :tag "Action arguments"
6404 :key-type symbol
6405 :value-type (sexp :tag "Value")))
6406 "Custom type for `display-buffer' actions.")
6407
6408 (defvar display-buffer-overriding-action '(nil . nil)
6409 "Overriding action to perform to display a buffer.
6410 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6411 function or a list of functions. Each function should accept two
6412 arguments: a buffer to display and an alist similar to ALIST.
6413 See `display-buffer' for details.")
6414 (put 'display-buffer-overriding-action 'risky-local-variable t)
6415
6416 (defcustom display-buffer-alist nil
6417 "Alist of conditional actions for `display-buffer'.
6418 This is a list of elements (CONDITION . ACTION), where:
6419
6420 CONDITION is either a regexp matching buffer names, or a
6421 function that takes two arguments - a buffer name and the
6422 ACTION argument of `display-buffer' - and returns a boolean.
6423
6424 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6425 function or a list of functions. Each such function should
6426 accept two arguments: a buffer to display and an alist of the
6427 same form as ALIST. See `display-buffer' for details.
6428
6429 `display-buffer' scans this alist until it either finds a
6430 matching regular expression or the function specified by a
6431 condition returns non-nil. In any of these cases, it adds the
6432 associated action to the list of actions it will try."
6433 :type `(alist :key-type
6434 (choice :tag "Condition"
6435 regexp
6436 (function :tag "Matcher function"))
6437 :value-type ,display-buffer--action-custom-type)
6438 :risky t
6439 :version "24.1"
6440 :group 'windows)
6441
6442 (defcustom display-buffer-base-action '(nil . nil)
6443 "User-specified default action for `display-buffer'.
6444 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6445 function or a list of functions. Each function should accept two
6446 arguments: a buffer to display and an alist similar to ALIST.
6447 See `display-buffer' for details."
6448 :type display-buffer--action-custom-type
6449 :risky t
6450 :version "24.1"
6451 :group 'windows)
6452
6453 (defconst display-buffer-fallback-action
6454 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6455 display-buffer-reuse-window
6456 display-buffer--maybe-pop-up-frame-or-window
6457 display-buffer-in-previous-window
6458 display-buffer-use-some-window
6459 ;; If all else fails, pop up a new frame.
6460 display-buffer-pop-up-frame))
6461 "Default fallback action for `display-buffer'.
6462 This is the action used by `display-buffer' if no other actions
6463 specified, e.g. by the user options `display-buffer-alist' or
6464 `display-buffer-base-action'. See `display-buffer'.")
6465 (put 'display-buffer-fallback-action 'risky-local-variable t)
6466
6467 (defun display-buffer-assq-regexp (buffer-name alist action)
6468 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6469 ACTION is the action argument passed to `display-buffer'."
6470 (catch 'match
6471 (dolist (entry alist)
6472 (let ((key (car entry)))
6473 (when (or (and (stringp key)
6474 (string-match-p key buffer-name))
6475 (and (functionp key)
6476 (funcall key buffer-name action)))
6477 (throw 'match (cdr entry)))))))
6478
6479 (defvar display-buffer--same-window-action
6480 '(display-buffer-same-window
6481 (inhibit-same-window . nil))
6482 "A `display-buffer' action for displaying in the same window.")
6483 (put 'display-buffer--same-window-action 'risky-local-variable t)
6484
6485 (defvar display-buffer--other-frame-action
6486 '((display-buffer-reuse-window
6487 display-buffer-pop-up-frame)
6488 (reusable-frames . 0)
6489 (inhibit-same-window . t))
6490 "A `display-buffer' action for displaying in another frame.")
6491 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6492
6493 (defun display-buffer (buffer-or-name &optional action frame)
6494 "Display BUFFER-OR-NAME in some window, without selecting it.
6495 BUFFER-OR-NAME must be a buffer or the name of an existing
6496 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6497 or nil if no such window is found.
6498
6499 Optional argument ACTION, if non-nil, should specify a display
6500 action. Its form is described below.
6501
6502 Optional argument FRAME, if non-nil, acts like an additional
6503 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6504 specifying the frame(s) to search for a window that is already
6505 displaying the buffer. See `display-buffer-reuse-window'
6506
6507 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6508 where FUNCTION is either a function or a list of functions, and
6509 ALIST is an arbitrary association list (alist).
6510
6511 Each such FUNCTION should accept two arguments: the buffer to
6512 display and an alist. Based on those arguments, it should
6513 display the buffer and return the window. If the caller is
6514 prepared to handle the case of not displaying the buffer
6515 and returning nil from `display-buffer' it should pass
6516 \(allow-no-window . t) as an element of the ALIST.
6517
6518 The `display-buffer' function builds a function list and an alist
6519 by combining the functions and alists specified in
6520 `display-buffer-overriding-action', `display-buffer-alist', the
6521 ACTION argument, `display-buffer-base-action', and
6522 `display-buffer-fallback-action' (in order). Then it calls each
6523 function in the combined function list in turn, passing the
6524 buffer as the first argument and the combined alist as the second
6525 argument, until one of the functions returns non-nil.
6526
6527 If ACTION is nil, the function list and the alist are built using
6528 only the other variables mentioned above.
6529
6530 Available action functions include:
6531 `display-buffer-same-window'
6532 `display-buffer-reuse-window'
6533 `display-buffer-pop-up-frame'
6534 `display-buffer-pop-up-window'
6535 `display-buffer-in-previous-window'
6536 `display-buffer-use-some-window'
6537 `display-buffer-use-some-frame'
6538
6539 Recognized alist entries include:
6540
6541 `inhibit-same-window' -- A non-nil value prevents the same
6542 window from being used for display.
6543
6544 `inhibit-switch-frame' -- A non-nil value prevents any other
6545 frame from being raised or selected,
6546 even if the window is displayed there.
6547
6548 `reusable-frames' -- Value specifies frame(s) to search for a
6549 window that already displays the buffer.
6550 See `display-buffer-reuse-window'.
6551
6552 `pop-up-frame-parameters' -- Value specifies an alist of frame
6553 parameters to give a new frame, if
6554 one is created.
6555
6556 `window-height' -- Value specifies either an integer (the number
6557 of lines of a new window), a floating point number (the
6558 fraction of a new window with respect to the height of the
6559 frame's root window) or a function to be called with one
6560 argument - a new window. The function is supposed to adjust
6561 the height of the window; its return value is ignored.
6562 Suitable functions are `shrink-window-if-larger-than-buffer'
6563 and `fit-window-to-buffer'.
6564
6565 `window-width' -- Value specifies either an integer (the number
6566 of columns of a new window), a floating point number (the
6567 fraction of a new window with respect to the width of the
6568 frame's root window) or a function to be called with one
6569 argument - a new window. The function is supposed to adjust
6570 the width of the window; its return value is ignored.
6571
6572 `allow-no-window' -- A non-nil value indicates readiness for the case
6573 of not displaying the buffer and FUNCTION can safely return
6574 a non-window value to suppress displaying.
6575
6576 `preserve-size' -- Value should be either '(t . nil)' to
6577 preserve the width of the window, '(nil . t)' to preserve its
6578 height or '(t . t)' to preserve both.
6579
6580 The ACTION argument to `display-buffer' can also have a non-nil
6581 and non-list value. This means to display the buffer in a window
6582 other than the selected one, even if it is already displayed in
6583 the selected window. If called interactively with a prefix
6584 argument, ACTION is t."
6585 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6586 (if current-prefix-arg t)))
6587 (let ((buffer (if (bufferp buffer-or-name)
6588 buffer-or-name
6589 (get-buffer buffer-or-name)))
6590 ;; Make sure that when we split windows the old window keeps
6591 ;; point, bug#14829.
6592 (split-window-keep-point t)
6593 ;; Handle the old form of the first argument.
6594 (inhibit-same-window (and action (not (listp action)))))
6595 (unless (listp action) (setq action nil))
6596 (if display-buffer-function
6597 ;; If `display-buffer-function' is defined, let it do the job.
6598 (funcall display-buffer-function buffer inhibit-same-window)
6599 ;; Otherwise, use the defined actions.
6600 (let* ((user-action
6601 (display-buffer-assq-regexp
6602 (buffer-name buffer) display-buffer-alist action))
6603 (special-action (display-buffer--special-action buffer))
6604 ;; Extra actions from the arguments to this function:
6605 (extra-action
6606 (cons nil (append (if inhibit-same-window
6607 '((inhibit-same-window . t)))
6608 (if frame
6609 `((reusable-frames . ,frame))))))
6610 ;; Construct action function list and action alist.
6611 (actions (list display-buffer-overriding-action
6612 user-action special-action action extra-action
6613 display-buffer-base-action
6614 display-buffer-fallback-action))
6615 (functions (apply 'append
6616 (mapcar (lambda (x)
6617 (setq x (car x))
6618 (if (functionp x) (list x) x))
6619 actions)))
6620 (alist (apply 'append (mapcar 'cdr actions)))
6621 window)
6622 (unless (buffer-live-p buffer)
6623 (error "Invalid buffer"))
6624 (while (and functions (not window))
6625 (setq window (funcall (car functions) buffer alist)
6626 functions (cdr functions)))
6627 (and (windowp window) window)))))
6628
6629 (defun display-buffer-other-frame (buffer)
6630 "Display buffer BUFFER preferably in another frame.
6631 This uses the function `display-buffer' as a subroutine; see
6632 its documentation for additional customization information."
6633 (interactive "BDisplay buffer in other frame: ")
6634 (display-buffer buffer display-buffer--other-frame-action t))
6635
6636 ;;; `display-buffer' action functions:
6637
6638 (defun display-buffer-use-some-frame (buffer alist)
6639 "Display BUFFER in an existing frame that meets a predicate
6640 \(by default any frame other than the current frame). If
6641 successful, return the window used; otherwise return nil.
6642
6643 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6644 raising the frame.
6645
6646 If ALIST has a non-nil `frame-predicate' entry, its value is a
6647 function taking one argument (a frame), returning non-nil if the
6648 frame is a candidate; this function replaces the default
6649 predicate.
6650
6651 If ALIST has a non-nil `inhibit-same-window' entry, avoid using
6652 the currently selected window (only useful with a frame-predicate
6653 that allows the selected frame)."
6654 (let* ((predicate (or (cdr (assq 'frame-predicate alist))
6655 (lambda (frame)
6656 (and
6657 (not (eq frame (selected-frame)))
6658 (not (window-dedicated-p
6659 (or
6660 (get-lru-window frame)
6661 (frame-first-window frame)))))
6662 )))
6663 (frame (car (filtered-frame-list predicate)))
6664 (window (and frame (get-lru-window frame nil (cdr (assq 'inhibit-same-window alist))))))
6665 (when window
6666 (prog1
6667 (window--display-buffer
6668 buffer window 'frame alist display-buffer-mark-dedicated)
6669 (unless (cdr (assq 'inhibit-switch-frame alist))
6670 (window--maybe-raise-frame frame))))
6671 ))
6672
6673 (defun display-buffer-same-window (buffer alist)
6674 "Display BUFFER in the selected window.
6675 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6676 if the selected window is a minibuffer window or is dedicated to
6677 another buffer; in that case, return nil. Otherwise, return the
6678 selected window."
6679 (unless (or (cdr (assq 'inhibit-same-window alist))
6680 (window-minibuffer-p)
6681 (window-dedicated-p))
6682 (window--display-buffer buffer (selected-window) 'reuse alist)))
6683
6684 (defun display-buffer--maybe-same-window (buffer alist)
6685 "Conditionally display BUFFER in the selected window.
6686 If `same-window-p' returns non-nil for BUFFER's name, call
6687 `display-buffer-same-window' and return its value. Otherwise,
6688 return nil."
6689 (and (same-window-p (buffer-name buffer))
6690 (display-buffer-same-window buffer alist)))
6691
6692 (defun display-buffer-reuse-window (buffer alist)
6693 "Return a window that is already displaying BUFFER.
6694 Return nil if no usable window is found.
6695
6696 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6697 window is not eligible for reuse.
6698
6699 If ALIST contains a `reusable-frames' entry, its value determines
6700 which frames to search for a reusable window:
6701 nil -- the selected frame (actually the last non-minibuffer frame)
6702 A frame -- just that frame
6703 `visible' -- all visible frames
6704 0 -- all frames on the current terminal
6705 t -- all frames.
6706
6707 If ALIST contains no `reusable-frames' entry, search just the
6708 selected frame if `display-buffer-reuse-frames' and
6709 `pop-up-frames' are both nil; search all frames on the current
6710 terminal if either of those variables is non-nil.
6711
6712 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6713 event that a window on another frame is chosen, avoid raising
6714 that frame."
6715 (let* ((alist-entry (assq 'reusable-frames alist))
6716 (frames (cond (alist-entry (cdr alist-entry))
6717 ((if (eq pop-up-frames 'graphic-only)
6718 (display-graphic-p)
6719 pop-up-frames)
6720 0)
6721 (display-buffer-reuse-frames 0)
6722 (t (last-nonminibuffer-frame))))
6723 (window (if (and (eq buffer (window-buffer))
6724 (not (cdr (assq 'inhibit-same-window alist))))
6725 (selected-window)
6726 (car (delq (selected-window)
6727 (get-buffer-window-list buffer 'nomini
6728 frames))))))
6729 (when (window-live-p window)
6730 (prog1 (window--display-buffer buffer window 'reuse alist)
6731 (unless (cdr (assq 'inhibit-switch-frame alist))
6732 (window--maybe-raise-frame (window-frame window)))))))
6733
6734 (defun display-buffer--special-action (buffer)
6735 "Return special display action for BUFFER, if any.
6736 If `special-display-p' returns non-nil for BUFFER, return an
6737 appropriate display action involving `special-display-function'.
6738 See `display-buffer' for the format of display actions."
6739 (and special-display-function
6740 ;; `special-display-p' returns either t or a list of frame
6741 ;; parameters to pass to `special-display-function'.
6742 (let ((pars (special-display-p (buffer-name buffer))))
6743 (when pars
6744 (list (list #'display-buffer-reuse-window
6745 `(lambda (buffer _alist)
6746 (funcall special-display-function
6747 buffer ',(if (listp pars) pars)))))))))
6748
6749 (defun display-buffer-pop-up-frame (buffer alist)
6750 "Display BUFFER in a new frame.
6751 This works by calling `pop-up-frame-function'. If successful,
6752 return the window used; otherwise return nil.
6753
6754 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6755 raising the new frame.
6756
6757 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6758 corresponding value is an alist of frame parameters to give the
6759 new frame."
6760 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
6761 (pop-up-frame-alist (append params pop-up-frame-alist))
6762 (fun pop-up-frame-function)
6763 frame window)
6764 (when (and fun
6765 ;; Make BUFFER current so `make-frame' will use it as the
6766 ;; new frame's buffer (Bug#15133).
6767 (with-current-buffer buffer
6768 (setq frame (funcall fun)))
6769 (setq window (frame-selected-window frame)))
6770 (prog1 (window--display-buffer
6771 buffer window 'frame alist display-buffer-mark-dedicated)
6772 (unless (cdr (assq 'inhibit-switch-frame alist))
6773 (window--maybe-raise-frame frame))))))
6774
6775 (defun display-buffer-pop-up-window (buffer alist)
6776 "Display BUFFER by popping up a new window.
6777 The new window is created on the selected frame, or in
6778 `last-nonminibuffer-frame' if no windows can be created there.
6779 If successful, return the new window; otherwise return nil.
6780
6781 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6782 event that the new window is created on another frame, avoid
6783 raising the frame."
6784 (let ((frame (or (window--frame-usable-p (selected-frame))
6785 (window--frame-usable-p (last-nonminibuffer-frame))))
6786 window)
6787 (when (and (or (not (frame-parameter frame 'unsplittable))
6788 ;; If the selected frame cannot be split, look at
6789 ;; `last-nonminibuffer-frame'.
6790 (and (eq frame (selected-frame))
6791 (setq frame (last-nonminibuffer-frame))
6792 (window--frame-usable-p frame)
6793 (not (frame-parameter frame 'unsplittable))))
6794 ;; Attempt to split largest or least recently used window.
6795 (setq window (or (window--try-to-split-window
6796 (get-largest-window frame t) alist)
6797 (window--try-to-split-window
6798 (get-lru-window frame t) alist))))
6799 (prog1 (window--display-buffer
6800 buffer window 'window alist display-buffer-mark-dedicated)
6801 (unless (cdr (assq 'inhibit-switch-frame alist))
6802 (window--maybe-raise-frame (window-frame window)))))))
6803
6804 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
6805 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6806
6807 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6808 text-only terminal), try with `display-buffer-pop-up-frame'.
6809
6810 If that cannot be done, and `pop-up-windows' is non-nil, try
6811 again with `display-buffer-pop-up-window'."
6812 (or (and (if (eq pop-up-frames 'graphic-only)
6813 (display-graphic-p)
6814 pop-up-frames)
6815 (display-buffer-pop-up-frame buffer alist))
6816 (and pop-up-windows
6817 (display-buffer-pop-up-window buffer alist))))
6818
6819 (defun display-buffer-below-selected (buffer alist)
6820 "Try displaying BUFFER in a window below the selected window.
6821 This either splits the selected window or reuses the window below
6822 the selected one."
6823 (let (window)
6824 (or (and (setq window (window-in-direction 'below))
6825 (eq buffer (window-buffer window))
6826 (window--display-buffer buffer window 'reuse alist))
6827 (and (not (frame-parameter nil 'unsplittable))
6828 (let ((split-height-threshold 0)
6829 split-width-threshold)
6830 (setq window (window--try-to-split-window (selected-window) alist)))
6831 (window--display-buffer
6832 buffer window 'window alist display-buffer-mark-dedicated))
6833 (and (setq window (window-in-direction 'below))
6834 (not (window-dedicated-p window))
6835 (window--display-buffer
6836 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6837
6838 (defun display-buffer-at-bottom (buffer alist)
6839 "Try displaying BUFFER in a window at the bottom of the selected frame.
6840 This either reuses such a window provided it shows BUFFER
6841 already, splits a window at the bottom of the frame or the
6842 frame's root window, or reuses some window at the bottom of the
6843 selected frame."
6844 (let (bottom-window bottom-window-shows-buffer window)
6845 (walk-window-tree
6846 (lambda (window)
6847 (cond
6848 ((window-in-direction 'below window))
6849 ((and (not bottom-window-shows-buffer)
6850 (eq buffer (window-buffer window)))
6851 (setq bottom-window-shows-buffer t)
6852 (setq bottom-window window))
6853 ((not bottom-window)
6854 (setq bottom-window window)))
6855 nil nil 'nomini))
6856 (or (and bottom-window-shows-buffer
6857 (window--display-buffer
6858 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
6859 (and (not (frame-parameter nil 'unsplittable))
6860 (let (split-width-threshold)
6861 (setq window (window--try-to-split-window bottom-window alist)))
6862 (window--display-buffer
6863 buffer window 'window alist display-buffer-mark-dedicated))
6864 (and (not (frame-parameter nil 'unsplittable))
6865 (setq window
6866 (condition-case nil
6867 (split-window (window--major-non-side-window))
6868 (error nil)))
6869 (window--display-buffer
6870 buffer window 'window alist display-buffer-mark-dedicated))
6871 (and (setq window bottom-window)
6872 (not (window-dedicated-p window))
6873 (window--display-buffer
6874 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6875
6876 (defun display-buffer-in-previous-window (buffer alist)
6877 "Display BUFFER in a window previously showing it.
6878 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6879 window is not eligible for reuse.
6880
6881 If ALIST contains a `reusable-frames' entry, its value determines
6882 which frames to search for a reusable window:
6883 nil -- the selected frame (actually the last non-minibuffer frame)
6884 A frame -- just that frame
6885 `visible' -- all visible frames
6886 0 -- all frames on the current terminal
6887 t -- all frames.
6888
6889 If ALIST contains no `reusable-frames' entry, search just the
6890 selected frame if `display-buffer-reuse-frames' and
6891 `pop-up-frames' are both nil; search all frames on the current
6892 terminal if either of those variables is non-nil.
6893
6894 If ALIST has a `previous-window' entry, the window specified by
6895 that entry will override any other window found by the methods
6896 above, even if that window never showed BUFFER before."
6897 (let* ((alist-entry (assq 'reusable-frames alist))
6898 (inhibit-same-window
6899 (cdr (assq 'inhibit-same-window alist)))
6900 (frames (cond
6901 (alist-entry (cdr alist-entry))
6902 ((if (eq pop-up-frames 'graphic-only)
6903 (display-graphic-p)
6904 pop-up-frames)
6905 0)
6906 (display-buffer-reuse-frames 0)
6907 (t (last-nonminibuffer-frame))))
6908 best-window second-best-window window)
6909 ;; Scan windows whether they have shown the buffer recently.
6910 (catch 'best
6911 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
6912 (when (and (assq buffer (window-prev-buffers window))
6913 (not (window-dedicated-p window)))
6914 (if (eq window (selected-window))
6915 (unless inhibit-same-window
6916 (setq second-best-window window))
6917 (setq best-window window)
6918 (throw 'best t)))))
6919 ;; When ALIST has a `previous-window' entry, that entry may override
6920 ;; anything we found so far.
6921 (when (and (setq window (cdr (assq 'previous-window alist)))
6922 (window-live-p window)
6923 (not (window-dedicated-p window)))
6924 (if (eq window (selected-window))
6925 (unless inhibit-same-window
6926 (setq second-best-window window))
6927 (setq best-window window)))
6928 ;; Return best or second best window found.
6929 (when (setq window (or best-window second-best-window))
6930 (window--display-buffer buffer window 'reuse alist))))
6931
6932 (defun display-buffer-use-some-window (buffer alist)
6933 "Display BUFFER in an existing window.
6934 Search for a usable window, set that window to the buffer, and
6935 return the window. If no suitable window is found, return nil.
6936
6937 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6938 event that a window in another frame is chosen, avoid raising
6939 that frame."
6940 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
6941 (frame (or (window--frame-usable-p (selected-frame))
6942 (window--frame-usable-p (last-nonminibuffer-frame))))
6943 (window
6944 ;; Reuse an existing window.
6945 (or (get-lru-window frame nil not-this-window)
6946 (let ((window (get-buffer-window buffer 'visible)))
6947 (unless (and not-this-window
6948 (eq window (selected-window)))
6949 window))
6950 (get-largest-window 'visible nil not-this-window)
6951 (let ((window (get-buffer-window buffer 0)))
6952 (unless (and not-this-window
6953 (eq window (selected-window)))
6954 window))
6955 (get-largest-window 0 nil not-this-window)))
6956 (quit-restore (and (window-live-p window)
6957 (window-parameter window 'quit-restore)))
6958 (quad (nth 1 quit-restore)))
6959 (when (window-live-p window)
6960 ;; If the window was used by `display-buffer' before, try to
6961 ;; resize it to its old height but don't signal an error.
6962 (when (and (listp quad)
6963 (integerp (nth 3 quad))
6964 (> (nth 3 quad) (window-total-height window)))
6965 (condition-case nil
6966 (window-resize window (- (nth 3 quad) (window-total-height window)))
6967 (error nil)))
6968
6969 (prog1
6970 (window--display-buffer buffer window 'reuse alist)
6971 (window--even-window-sizes window)
6972 (unless (cdr (assq 'inhibit-switch-frame alist))
6973 (window--maybe-raise-frame (window-frame window)))))))
6974
6975 (defun display-buffer-no-window (_buffer alist)
6976 "Display BUFFER in no window.
6977 If ALIST has a non-nil `allow-no-window' entry, then don't display
6978 a window at all. This makes possible to override the default action
6979 and avoid displaying the buffer. It is assumed that when the caller
6980 specifies a non-nil `allow-no-window' then it can handle a nil value
6981 returned from `display-buffer' in this case."
6982 (when (cdr (assq 'allow-no-window alist))
6983 'fail))
6984
6985 ;;; Display + selection commands:
6986 (defun pop-to-buffer (buffer &optional action norecord)
6987 "Select buffer BUFFER in some window, preferably a different one.
6988 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6989 is a string not naming an existent buffer, create a buffer with
6990 that name. If BUFFER is nil, choose some other buffer. Return
6991 the buffer.
6992
6993 This uses `display-buffer' as a subroutine. The optional ACTION
6994 argument is passed to `display-buffer' as its ACTION argument.
6995 See `display-buffer' for more information. ACTION is t if called
6996 interactively with a prefix argument, which means to pop to a
6997 window other than the selected one even if the buffer is already
6998 displayed in the selected window.
6999
7000 If the window to show BUFFER is not on the selected
7001 frame, raise that window's frame and give it input focus.
7002
7003 Optional third arg NORECORD non-nil means do not put this buffer
7004 at the front of the list of recently selected ones."
7005 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
7006 (if current-prefix-arg t)))
7007 (setq buffer (window-normalize-buffer-to-switch-to buffer))
7008 ;; This should be done by `select-window' below.
7009 ;; (set-buffer buffer)
7010 (let* ((old-frame (selected-frame))
7011 (window (display-buffer buffer action))
7012 (frame (window-frame window)))
7013 ;; If we chose another frame, make sure it gets input focus.
7014 (unless (eq frame old-frame)
7015 (select-frame-set-input-focus frame norecord))
7016 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
7017 (select-window window norecord)
7018 buffer))
7019
7020 (defun pop-to-buffer-same-window (buffer &optional norecord)
7021 "Select buffer BUFFER in some window, preferably the same one.
7022 BUFFER may be a buffer, a string (a buffer name), or nil. If it
7023 is a string not naming an existent buffer, create a buffer with
7024 that name. If BUFFER is nil, choose some other buffer. Return
7025 the buffer.
7026
7027 Optional argument NORECORD, if non-nil means do not put this
7028 buffer at the front of the list of recently selected ones.
7029
7030 Unlike `pop-to-buffer', this function prefers using the selected
7031 window over popping up a new window or frame."
7032 (pop-to-buffer buffer display-buffer--same-window-action norecord))
7033
7034 (defun read-buffer-to-switch (prompt)
7035 "Read the name of a buffer to switch to, prompting with PROMPT.
7036 Return the name of the buffer as a string.
7037
7038 This function is intended for the `switch-to-buffer' family of
7039 commands since these need to omit the name of the current buffer
7040 from the list of completions and default values."
7041 (let ((rbts-completion-table (internal-complete-buffer-except)))
7042 (minibuffer-with-setup-hook
7043 (lambda ()
7044 (setq minibuffer-completion-table rbts-completion-table)
7045 ;; Since rbts-completion-table is built dynamically, we
7046 ;; can't just add it to the default value of
7047 ;; icomplete-with-completion-tables, so we add it
7048 ;; here manually.
7049 (if (and (boundp 'icomplete-with-completion-tables)
7050 (listp icomplete-with-completion-tables))
7051 (set (make-local-variable 'icomplete-with-completion-tables)
7052 (cons rbts-completion-table
7053 icomplete-with-completion-tables))))
7054 (read-buffer prompt (other-buffer (current-buffer))
7055 (confirm-nonexistent-file-or-buffer)))))
7056
7057 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
7058 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
7059 If BUFFER-OR-NAME is nil, return the buffer returned by
7060 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
7061 exists, return that buffer. If no such buffer exists, create a
7062 buffer with the name BUFFER-OR-NAME and return that buffer."
7063 (if buffer-or-name
7064 (or (get-buffer buffer-or-name)
7065 (let ((buffer (get-buffer-create buffer-or-name)))
7066 (set-buffer-major-mode buffer)
7067 buffer))
7068 (other-buffer)))
7069
7070 (defcustom switch-to-buffer-preserve-window-point nil
7071 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
7072 If this is nil, `switch-to-buffer' displays the buffer at that
7073 buffer's `point'. If this is `already-displayed', it tries to
7074 display the buffer at its previous position in the selected
7075 window, provided the buffer is currently displayed in some other
7076 window on any visible or iconified frame. If this is t, it
7077 unconditionally tries to display the buffer at its previous
7078 position in the selected window.
7079
7080 This variable is ignored if the buffer is already displayed in
7081 the selected window or never appeared in it before, or if
7082 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
7083 :type '(choice
7084 (const :tag "Never" nil)
7085 (const :tag "If already displayed elsewhere" already-displayed)
7086 (const :tag "Always" t))
7087 :group 'windows
7088 :version "24.3")
7089
7090 (defcustom switch-to-buffer-in-dedicated-window nil
7091 "Allow switching to buffer in strongly dedicated windows.
7092 If non-nil, allow `switch-to-buffer' to proceed when called
7093 interactively and the selected window is strongly dedicated to
7094 its buffer.
7095
7096 The following values are recognized:
7097
7098 nil - disallow switching; signal an error
7099
7100 prompt - prompt user whether to allow switching
7101
7102 pop - perform `pop-to-buffer' instead
7103
7104 t - undedicate selected window and switch
7105
7106 When called non-interactively, `switch-to-buffer' always signals
7107 an error when the selected window is dedicated to its buffer and
7108 FORCE-SAME-WINDOW is non-nil."
7109 :type '(choice
7110 (const :tag "Disallow" nil)
7111 (const :tag "Prompt" prompt)
7112 (const :tag "Pop" pop)
7113 (const :tag "Allow" t))
7114 :group 'windows
7115 :version "25.1")
7116
7117 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
7118 "Display buffer BUFFER-OR-NAME in the selected window.
7119
7120 WARNING: This is NOT the way to work on another buffer temporarily
7121 within a Lisp program! Use `set-buffer' instead. That avoids
7122 messing with the window-buffer correspondences.
7123
7124 If the selected window cannot display the specified buffer
7125 because it is a minibuffer window or strongly dedicated to
7126 another buffer, call `pop-to-buffer' to select the buffer in
7127 another window. In interactive use, if the selected window is
7128 strongly dedicated to its buffer, the value of the option
7129 `switch-to-buffer-in-dedicated-window' specifies how to proceed.
7130
7131 If called interactively, read the buffer name using the
7132 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7133 determines whether to request confirmation before creating a new
7134 buffer.
7135
7136 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7137 If BUFFER-OR-NAME is a string that does not identify an existing
7138 buffer, create a buffer with that name. If BUFFER-OR-NAME is
7139 nil, switch to the buffer returned by `other-buffer'.
7140
7141 If optional argument NORECORD is non-nil, do not put the buffer
7142 at the front of the buffer list, and do not make the window
7143 displaying it the most recently selected one.
7144
7145 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
7146 must be displayed in the selected window when called
7147 non-interactively; if that is impossible, signal an error rather
7148 than calling `pop-to-buffer'.
7149
7150 The option `switch-to-buffer-preserve-window-point' can be used
7151 to make the buffer appear at its last position in the selected
7152 window.
7153
7154 Return the buffer switched to."
7155 (interactive
7156 (let ((force-same-window
7157 (cond
7158 ((window-minibuffer-p) nil)
7159 ((not (eq (window-dedicated-p) t)) 'force-same-window)
7160 ((pcase switch-to-buffer-in-dedicated-window
7161 (`nil (user-error
7162 "Cannot switch buffers in a dedicated window"))
7163 (`prompt
7164 (if (y-or-n-p
7165 (format "Window is dedicated to %s; undedicate it"
7166 (window-buffer)))
7167 (progn
7168 (set-window-dedicated-p nil nil)
7169 'force-same-window)
7170 (user-error
7171 "Cannot switch buffers in a dedicated window")))
7172 (`pop nil)
7173 (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
7174 (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
7175 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
7176 (cond
7177 ;; Don't call set-window-buffer if it's not needed since it
7178 ;; might signal an error (e.g. if the window is dedicated).
7179 ((eq buffer (window-buffer)))
7180 ((window-minibuffer-p)
7181 (if force-same-window
7182 (user-error "Cannot switch buffers in minibuffer window")
7183 (pop-to-buffer buffer norecord)))
7184 ((eq (window-dedicated-p) t)
7185 (if force-same-window
7186 (user-error "Cannot switch buffers in a dedicated window")
7187 (pop-to-buffer buffer norecord)))
7188 (t
7189 (let* ((entry (assq buffer (window-prev-buffers)))
7190 (displayed (and (eq switch-to-buffer-preserve-window-point
7191 'already-displayed)
7192 (get-buffer-window buffer 0))))
7193 (set-window-buffer nil buffer)
7194 (when (and entry
7195 (or (eq switch-to-buffer-preserve-window-point t)
7196 displayed))
7197 ;; Try to restore start and point of buffer in the selected
7198 ;; window (Bug#4041).
7199 (set-window-start (selected-window) (nth 1 entry) t)
7200 (set-window-point nil (nth 2 entry))))))
7201
7202 (unless norecord
7203 (select-window (selected-window)))
7204 (set-buffer buffer)))
7205
7206 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
7207 "Select the buffer specified by BUFFER-OR-NAME in another window.
7208 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7209 nil. Return the buffer switched to.
7210
7211 If called interactively, prompt for the buffer name using the
7212 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7213 determines whether to request confirmation before creating a new
7214 buffer.
7215
7216 If BUFFER-OR-NAME is a string and does not identify an existing
7217 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7218 nil, switch to the buffer returned by `other-buffer'.
7219
7220 Optional second argument NORECORD non-nil means do not put this
7221 buffer at the front of the list of recently selected ones.
7222
7223 This uses the function `display-buffer' as a subroutine; see its
7224 documentation for additional customization information."
7225 (interactive
7226 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
7227 (let ((pop-up-windows t))
7228 (pop-to-buffer buffer-or-name t norecord)))
7229
7230 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
7231 "Switch to buffer BUFFER-OR-NAME in another frame.
7232 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7233 nil. Return the buffer switched to.
7234
7235 If called interactively, prompt for the buffer name using the
7236 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7237 determines whether to request confirmation before creating a new
7238 buffer.
7239
7240 If BUFFER-OR-NAME is a string and does not identify an existing
7241 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7242 nil, switch to the buffer returned by `other-buffer'.
7243
7244 Optional second arg NORECORD non-nil means do not put this
7245 buffer at the front of the list of recently selected ones.
7246
7247 This uses the function `display-buffer' as a subroutine; see its
7248 documentation for additional customization information."
7249 (interactive
7250 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
7251 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
7252 \f
7253 (defun set-window-text-height (window height)
7254 "Set the height in lines of the text display area of WINDOW to HEIGHT.
7255 WINDOW must be a live window and defaults to the selected one.
7256 HEIGHT doesn't include the mode line or header line, if any, or
7257 any partial-height lines in the text display area.
7258
7259 Note that the current implementation of this function cannot
7260 always set the height exactly, but attempts to be conservative,
7261 by allocating more lines than are actually needed in the case
7262 where some error may be present."
7263 (setq window (window-normalize-window window t))
7264 (let ((delta (- height (window-text-height window))))
7265 (unless (zerop delta)
7266 ;; Setting window-min-height to a value like 1 can lead to very
7267 ;; bizarre displays because it also allows Emacs to make *other*
7268 ;; windows one line tall, which means that there's no more space
7269 ;; for the mode line.
7270 (let ((window-min-height (min 2 height)))
7271 (window-resize window delta)))))
7272
7273 (defun enlarge-window-horizontally (delta)
7274 "Make selected window DELTA columns wider.
7275 Interactively, if no argument is given, make selected window one
7276 column wider."
7277 (interactive "p")
7278 (enlarge-window delta t))
7279
7280 (defun shrink-window-horizontally (delta)
7281 "Make selected window DELTA columns narrower.
7282 Interactively, if no argument is given, make selected window one
7283 column narrower."
7284 (interactive "p")
7285 (shrink-window delta t))
7286
7287 (defun count-screen-lines (&optional beg end count-final-newline window)
7288 "Return the number of screen lines in the region.
7289 The number of screen lines may be different from the number of actual lines,
7290 due to line breaking, display table, etc.
7291
7292 Optional arguments BEG and END default to `point-min' and `point-max'
7293 respectively.
7294
7295 If region ends with a newline, ignore it unless optional third argument
7296 COUNT-FINAL-NEWLINE is non-nil.
7297
7298 The optional fourth argument WINDOW specifies the window used for obtaining
7299 parameters such as width, horizontal scrolling, and so on. The default is
7300 to use the selected window's parameters.
7301
7302 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7303 regardless of which buffer is displayed in WINDOW. This makes possible to use
7304 `count-screen-lines' in any buffer, whether or not it is currently displayed
7305 in some window."
7306 (unless beg
7307 (setq beg (point-min)))
7308 (unless end
7309 (setq end (point-max)))
7310 (if (= beg end)
7311 0
7312 (save-excursion
7313 (save-restriction
7314 (widen)
7315 (narrow-to-region (min beg end)
7316 (if (and (not count-final-newline)
7317 (= ?\n (char-before (max beg end))))
7318 (1- (max beg end))
7319 (max beg end)))
7320 (goto-char (point-min))
7321 (1+ (vertical-motion (buffer-size) window))))))
7322
7323 (defun window-buffer-height (window)
7324 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7325 WINDOW must be a live window and defaults to the selected one."
7326 (setq window (window-normalize-window window t))
7327 (with-current-buffer (window-buffer window)
7328 (max 1
7329 (count-screen-lines (point-min) (point-max)
7330 ;; If buffer ends with a newline, ignore it when
7331 ;; counting height unless point is after it.
7332 (eobp)
7333 window))))
7334
7335 ;;; Resizing windows and frames to fit their contents exactly.
7336 (defcustom fit-window-to-buffer-horizontally nil
7337 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7338 If this is nil, `fit-window-to-buffer' never resizes windows
7339 horizontally. If this is `only', it can resize windows
7340 horizontally only. Any other value means `fit-window-to-buffer'
7341 can resize windows in both dimensions."
7342 :type 'boolean
7343 :version "24.4"
7344 :group 'help)
7345
7346 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7347 ;; counting title bar and outer borders.
7348 (defcustom fit-frame-to-buffer nil
7349 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7350 A frame is fit if and only if its root window is a live window
7351 and this option is non-nil. If this is `horizontally', frames
7352 are resized horizontally only. If this is `vertically', frames
7353 are resized vertically only. Any other non-nil value means
7354 frames can be resized in both dimensions."
7355 :type 'boolean
7356 :version "24.4"
7357 :group 'help)
7358
7359 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
7360 "Margins around frame for `fit-frame-to-buffer'.
7361 This specifies the numbers of pixels to be left free on the left,
7362 above, on the right, and below a frame fitted to its buffer. Set
7363 this to avoid obscuring other desktop objects like the taskbar.
7364 The default is nil for each side, which means to not add margins.
7365
7366 The value specified here can be overridden for a specific frame
7367 by that frame's `fit-frame-to-buffer-margins' parameter, if
7368 present. See also `fit-frame-to-buffer-sizes'."
7369 :version "24.4"
7370 :type '(list
7371 (choice
7372 :tag "Left"
7373 :value nil
7374 :format "%[LeftMargin%] %v "
7375 (const :tag "None" :format "%t" nil)
7376 (integer :tag "Pixels" :size 5))
7377 (choice
7378 :tag "Top"
7379 :value nil
7380 :format "%[TopMargin%] %v "
7381 (const :tag "None" :format "%t" nil)
7382 (integer :tag "Pixels" :size 5))
7383 (choice
7384 :tag "Right"
7385 :value nil
7386 :format "%[RightMargin%] %v "
7387 (const :tag "None" :format "%t" nil)
7388 (integer :tag "Pixels" :size 5))
7389 (choice
7390 :tag "Bottom"
7391 :value nil
7392 :format "%[BottomMargin%] %v "
7393 (const :tag "None" :format "%t" nil)
7394 (integer :tag "Pixels" :size 5)))
7395 :group 'help)
7396
7397 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
7398 "Size boundaries of frame for `fit-frame-to-buffer'.
7399 This list specifies the total maximum and minimum lines and
7400 maximum and minimum columns of the root window of any frame that
7401 shall be fit to its buffer. If any of these values is non-nil,
7402 it overrides the corresponding argument of `fit-frame-to-buffer'.
7403
7404 On window systems where the menubar can wrap, fitting a frame to
7405 its buffer may swallow the last line(s). Specifying an
7406 appropriate minimum width value here can avoid such wrapping.
7407
7408 See also `fit-frame-to-buffer-margins'."
7409 :version "24.4"
7410 :type '(list
7411 (choice
7412 :tag "Maximum Height"
7413 :value nil
7414 :format "%[MaxHeight%] %v "
7415 (const :tag "None" :format "%t" nil)
7416 (integer :tag "Lines" :size 5))
7417 (choice
7418 :tag "Minimum Height"
7419 :value nil
7420 :format "%[MinHeight%] %v "
7421 (const :tag "None" :format "%t" nil)
7422 (integer :tag "Lines" :size 5))
7423 (choice
7424 :tag "Maximum Width"
7425 :value nil
7426 :format "%[MaxWidth%] %v "
7427 (const :tag "None" :format "%t" nil)
7428 (integer :tag "Columns" :size 5))
7429 (choice
7430 :tag "Minimum Width"
7431 :value nil
7432 :format "%[MinWidth%] %v\n"
7433 (const :tag "None" :format "%t" nil)
7434 (integer :tag "Columns" :size 5)))
7435 :group 'help)
7436
7437 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7438
7439 (defun window--sanitize-margin (margin left right)
7440 "Return MARGIN if it's a number between LEFT and RIGHT."
7441 (when (and (numberp margin)
7442 (<= left (- right margin)) (<= margin right))
7443 margin))
7444
7445 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
7446
7447 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7448 "Adjust size of FRAME to display the contents of its buffer exactly.
7449 FRAME can be any live frame and defaults to the selected one.
7450 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7451 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7452 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7453 `window-min-height' and `window-min-width' respectively.
7454
7455 If the optional argument ONLY is `vertically', resize the frame
7456 vertically only. If ONLY is `horizontally', resize the frame
7457 horizontally only.
7458
7459 The new position and size of FRAME can be additionally determined
7460 by customizing the options `fit-frame-to-buffer-sizes' and
7461 `fit-frame-to-buffer-margins' or the corresponding parameters of
7462 FRAME."
7463 (interactive)
7464 (unless (and (fboundp 'x-display-pixel-height)
7465 ;; We need the respective sizes now.
7466 (fboundp 'display-monitor-attributes-list))
7467 (user-error "Cannot resize frame in non-graphic Emacs"))
7468 (setq frame (window-normalize-frame frame))
7469 (when (window-live-p (frame-root-window frame))
7470 (with-selected-window (frame-root-window frame)
7471 (let* ((window (frame-root-window frame))
7472 (char-width (frame-char-width))
7473 (char-height (frame-char-height))
7474 (monitor-attributes (car (display-monitor-attributes-list
7475 (frame-parameter frame 'display))))
7476 (geometry (cdr (assq 'geometry monitor-attributes)))
7477 (display-width (- (nth 2 geometry) (nth 0 geometry)))
7478 (display-height (- (nth 3 geometry) (nth 1 geometry)))
7479 (workarea (cdr (assq 'workarea monitor-attributes)))
7480 ;; Handle margins.
7481 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
7482 fit-frame-to-buffer-margins))
7483 (left-margin (if (nth 0 margins)
7484 (or (window--sanitize-margin
7485 (nth 0 margins) 0 display-width)
7486 0)
7487 (nth 0 workarea)))
7488 (top-margin (if (nth 1 margins)
7489 (or (window--sanitize-margin
7490 (nth 1 margins) 0 display-height)
7491 0)
7492 (nth 1 workarea)))
7493 (workarea-width (nth 2 workarea))
7494 (right-margin (if (nth 2 margins)
7495 (- display-width
7496 (or (window--sanitize-margin
7497 (nth 2 margins) left-margin display-width)
7498 0))
7499 (nth 2 workarea)))
7500 (workarea-height (nth 3 workarea))
7501 (bottom-margin (if (nth 3 margins)
7502 (- display-height
7503 (or (window--sanitize-margin
7504 (nth 3 margins) top-margin display-height)
7505 0))
7506 (nth 3 workarea)))
7507 ;; The pixel width of FRAME (which does not include the
7508 ;; window manager's decorations).
7509 (frame-width (frame-pixel-width))
7510 ;; The pixel width of the body of FRAME's root window.
7511 (window-body-width (window-body-width nil t))
7512 ;; The difference in pixels between total and body width of
7513 ;; FRAME's window.
7514 (window-extra-width (- (window-pixel-width) window-body-width))
7515 ;; The difference in pixels between the frame's pixel width
7516 ;; and the window's body width. This is the space we can't
7517 ;; use for fitting.
7518 (extra-width (- frame-width window-body-width))
7519 ;; The maximum width we can use for fitting.
7520 (fit-width (- workarea-width extra-width))
7521 ;; The pixel position of FRAME's left border. We usually
7522 ;; try to leave this alone.
7523 (left
7524 (let ((left (frame-parameter nil 'left)))
7525 (if (consp left)
7526 (funcall (car left) (cadr left))
7527 left)))
7528 ;; The pixel height of FRAME (which does not include title
7529 ;; line, decorations, and sometimes neither the menu nor
7530 ;; the toolbar).
7531 (frame-height (frame-pixel-height))
7532 ;; The pixel height of FRAME's root window (we don't care
7533 ;; about the window's body height since the return value of
7534 ;; `window-text-pixel-size' includes header and mode line).
7535 (window-height (window-pixel-height))
7536 ;; The difference in pixels between the frame's pixel
7537 ;; height and the window's height.
7538 (extra-height (- frame-height window-height))
7539 ;; When tool-bar-mode is enabled and we just created a new
7540 ;; frame, reserve lines for toolbar resizing. Needed
7541 ;; because for reasons unknown to me Emacs (1) reserves one
7542 ;; line for the toolbar when making the initial frame and
7543 ;; toolbars are enabled, and (2) later adds the remaining
7544 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7545 ;; YMMV when you're on a system that behaves differently.
7546 (toolbar-extra-height
7547 (let ((quit-restore (window-parameter window 'quit-restore))
7548 ;; This may have to change when we allow arbitrary
7549 ;; pixel height toolbars.
7550 (lines (tool-bar-height)))
7551 (* char-height
7552 (if (and quit-restore (eq (car quit-restore) 'frame)
7553 (not (zerop lines)))
7554 (1- lines)
7555 0))))
7556 ;; The pixel position of FRAME's top border.
7557 (top
7558 (let ((top (frame-parameter nil 'top)))
7559 (if (consp top)
7560 (funcall (car top) (cadr top))
7561 top)))
7562 ;; Sanitize minimum and maximum sizes.
7563 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
7564 fit-frame-to-buffer-sizes))
7565 (max-height
7566 (cond
7567 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
7568 ((numberp max-height) (* max-height char-height))
7569 (t display-height)))
7570 (min-height
7571 (cond
7572 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
7573 ((numberp min-height) (* min-height char-height))
7574 (t (* window-min-height char-height))))
7575 (max-width
7576 (cond
7577 ((numberp (nth 2 sizes))
7578 (- (* (nth 2 sizes) char-width) window-extra-width))
7579 ((numberp max-width)
7580 (- (* max-width char-width) window-extra-width))
7581 (t display-width)))
7582 (min-width
7583 (cond
7584 ((numberp (nth 3 sizes))
7585 (- (* (nth 3 sizes) char-width) window-extra-width))
7586 ((numberp min-width)
7587 (- (* min-width char-width) window-extra-width))
7588 (t (* window-min-width char-width))))
7589 ;; Note: Currently, for a new frame the sizes of the header
7590 ;; and mode line may be estimated incorrectly
7591 (value (window-text-pixel-size
7592 nil t t workarea-width workarea-height t))
7593 (width (+ (car value) (window-right-divider-width)))
7594 (height
7595 (+ (cdr value)
7596 (window-bottom-divider-width)
7597 (window-scroll-bar-height))))
7598 ;; Don't change height or width when the window's size is fixed
7599 ;; in either direction or ONLY forbids it.
7600 (cond
7601 ((or (eq window-size-fixed 'width) (eq only 'vertically))
7602 (setq width nil))
7603 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
7604 (setq height nil)))
7605 ;; Fit width to constraints.
7606 (when width
7607 (unless frame-resize-pixelwise
7608 ;; Round to character sizes.
7609 (setq width (* (/ (+ width char-width -1) char-width)
7610 char-width)))
7611 ;; Fit to maximum and minimum widths.
7612 (setq width (max (min width max-width) min-width))
7613 ;; Add extra width.
7614 (setq width (+ width extra-width))
7615 ;; Preserve margins.
7616 (let ((right (+ left width)))
7617 (cond
7618 ((> right right-margin)
7619 ;; Move frame to left (we don't know its real width).
7620 (setq left (max left-margin (- left (- right right-margin)))))
7621 ((< left left-margin)
7622 ;; Move frame to right.
7623 (setq left left-margin)))))
7624 ;; Fit height to constraints.
7625 (when height
7626 (unless frame-resize-pixelwise
7627 (setq height (* (/ (+ height char-height -1) char-height)
7628 char-height)))
7629 ;; Fit to maximum and minimum heights.
7630 (setq height (max (min height max-height) min-height))
7631 ;; Add extra height.
7632 (setq height (+ height extra-height))
7633 ;; Preserve margins.
7634 (let ((bottom (+ top height)))
7635 (cond
7636 ((> bottom bottom-margin)
7637 ;; Move frame up (we don't know its real height).
7638 (setq top (max top-margin (- top (- bottom bottom-margin)))))
7639 ((< top top-margin)
7640 ;; Move frame down.
7641 (setq top top-margin)))))
7642 ;; Apply changes.
7643 (set-frame-position frame left top)
7644 ;; Clumsily try to translate our calculations to what
7645 ;; `set-frame-size' wants.
7646 (when width
7647 (setq width (- (+ (frame-text-width) width)
7648 extra-width window-body-width)))
7649 (when height
7650 (setq height (- (+ (frame-text-height) height)
7651 extra-height window-height)))
7652 (set-frame-size
7653 frame
7654 (if width
7655 (if frame-resize-pixelwise
7656 width
7657 (/ width char-width))
7658 (frame-text-width))
7659 (if height
7660 (if frame-resize-pixelwise
7661 height
7662 (/ height char-height))
7663 (frame-text-height))
7664 frame-resize-pixelwise)))))
7665
7666 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
7667 "Adjust size of WINDOW to display its buffer's contents exactly.
7668 WINDOW must be a live window and defaults to the selected one.
7669
7670 If WINDOW is part of a vertical combination, adjust WINDOW's
7671 height. The new height is calculated from the actual height of
7672 the accessible portion of its buffer. The optional argument
7673 MAX-HEIGHT specifies a maximum height and defaults to the height
7674 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7675 minimum height and defaults to `window-min-height'. Both
7676 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7677 and header line and a bottom divider, if any.
7678
7679 If WINDOW is part of a horizontal combination and the value of
7680 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7681 WINDOW's width. The new width of WINDOW is calculated from the
7682 maximum length of its buffer's lines that follow the current
7683 start position of WINDOW. The optional argument MAX-WIDTH
7684 specifies a maximum width and defaults to the width of WINDOW's
7685 frame. The optional argument MIN-WIDTH specifies a minimum width
7686 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7687 are specified in columns and include fringes, margins, a
7688 scrollbar and a vertical divider, if any.
7689
7690 If the optional argument `preserve-size' is non-nil, preserve the
7691 size of WINDOW (see `window-preserve-size').
7692
7693 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7694 If WINDOW is its frame's root window and the option
7695 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7696 adjust the frame's size.
7697
7698 Note that even if this function makes WINDOW large enough to show
7699 _all_ parts of its buffer you might not see the first part when
7700 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7701 not see the top of its buffer unless WINDOW starts at its minimum
7702 accessible position."
7703 (interactive)
7704 (setq window (window-normalize-window window t))
7705 (if (eq window (frame-root-window window))
7706 (when fit-frame-to-buffer
7707 ;; Fit WINDOW's frame to buffer.
7708 (fit-frame-to-buffer
7709 (window-frame window)
7710 max-height min-height max-width min-width
7711 (and (memq fit-frame-to-buffer '(vertically horizontally))
7712 fit-frame-to-buffer)))
7713 (with-selected-window window
7714 (let* ((pixelwise window-resize-pixelwise)
7715 (char-height (frame-char-height))
7716 (char-width (frame-char-width))
7717 (total-height (window-size window nil pixelwise))
7718 (body-height (window-body-height window pixelwise))
7719 (body-width (window-body-width window pixelwise))
7720 (min-height
7721 ;; Sanitize MIN-HEIGHT.
7722 (if (numberp min-height)
7723 ;; Can't get smaller than `window-safe-min-height'.
7724 (max (if pixelwise
7725 (* char-height min-height)
7726 min-height)
7727 (if pixelwise
7728 (window-safe-min-pixel-height window)
7729 window-safe-min-height))
7730 ;; Preserve header and mode line if present.
7731 (max (if pixelwise
7732 (* char-height window-min-height)
7733 window-min-height)
7734 (window-min-size window nil window pixelwise))))
7735 (max-height
7736 ;; Sanitize MAX-HEIGHT.
7737 (if (numberp max-height)
7738 (min
7739 (+ total-height
7740 (window-max-delta
7741 window nil window nil nil nil pixelwise))
7742 (if pixelwise
7743 (* char-height max-height)
7744 max-height))
7745 (+ total-height (window-max-delta
7746 window nil window nil nil nil pixelwise))))
7747 height)
7748 (cond
7749 ;; If WINDOW is vertically combined, try to resize it
7750 ;; vertically.
7751 ((and (not (eq fit-window-to-buffer-horizontally 'only))
7752 (not (window-size-fixed-p window 'preserved))
7753 (window-combined-p))
7754 ;; Vertically we always want to fit the entire buffer.
7755 ;; WINDOW'S height can't get larger than its frame's pixel
7756 ;; height. Its width remains fixed.
7757 (setq height (+ (cdr (window-text-pixel-size
7758 nil nil t nil (frame-pixel-height) t))
7759 (window-scroll-bar-height window)
7760 (window-bottom-divider-width)))
7761 ;; Round height.
7762 (unless pixelwise
7763 (setq height (/ (+ height char-height -1) char-height)))
7764 (unless (= height total-height)
7765 (window-preserve-size window)
7766 (window-resize-no-error
7767 window
7768 (- (max min-height (min max-height height)) total-height)
7769 nil window pixelwise)
7770 (when preserve-size
7771 (window-preserve-size window nil t))))
7772 ;; If WINDOW is horizontally combined, try to resize it
7773 ;; horizontally.
7774 ((and fit-window-to-buffer-horizontally
7775 (not (window-size-fixed-p window t 'preserved))
7776 (window-combined-p nil t))
7777 (let* ((total-width (window-size window t pixelwise))
7778 (min-width
7779 ;; Sanitize MIN-WIDTH.
7780 (if (numberp min-width)
7781 ;; Can't get smaller than `window-safe-min-width'.
7782 (max (if pixelwise
7783 (* char-width min-width)
7784 min-width)
7785 (if pixelwise
7786 (window-safe-min-pixel-width)
7787 window-safe-min-width))
7788 ;; Preserve fringes, margins, scrollbars if present.
7789 (max (if pixelwise
7790 (* char-width window-min-width)
7791 window-min-width)
7792 (window-min-size nil nil window pixelwise))))
7793 (max-width
7794 ;; Sanitize MAX-WIDTH.
7795 (if (numberp max-width)
7796 (min (+ total-width
7797 (window-max-delta
7798 window t window nil nil nil pixelwise))
7799 (if pixelwise
7800 (* char-width max-width)
7801 max-width))
7802 (+ total-width (window-max-delta
7803 window t window nil nil nil pixelwise))))
7804 ;; When fitting horizontally, assume that WINDOW's
7805 ;; start position remains unaltered. WINDOW can't get
7806 ;; wider than its frame's pixel width, its height
7807 ;; remains unaltered.
7808 (width (+ (car (window-text-pixel-size
7809 nil (window-start) (point-max)
7810 (frame-pixel-width)
7811 ;; Add one char-height to assure that
7812 ;; we're on the safe side. This
7813 ;; overshoots when the first line below
7814 ;; the bottom is wider than the window.
7815 (* body-height
7816 (if pixelwise 1 char-height))))
7817 (window-right-divider-width))))
7818 (unless pixelwise
7819 (setq width (/ (+ width char-width -1) char-width)))
7820 (unless (= width body-width)
7821 (window-preserve-size window t)
7822 (window-resize-no-error
7823 window
7824 (- (max min-width
7825 (min max-width
7826 (+ total-width (- width body-width))))
7827 total-width)
7828 t window pixelwise)
7829 (when preserve-size
7830 (window-preserve-size window t t))))))))))
7831
7832 (defun window-safely-shrinkable-p (&optional window)
7833 "Return t if WINDOW can be shrunk without shrinking other windows.
7834 WINDOW defaults to the selected window."
7835 (with-selected-window (or window (selected-window))
7836 (let ((edges (window-edges)))
7837 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
7838 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
7839
7840 (defun shrink-window-if-larger-than-buffer (&optional window)
7841 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7842 More precisely, shrink WINDOW vertically to be as small as
7843 possible, while still showing the full contents of its buffer.
7844 WINDOW must be a live window and defaults to the selected one.
7845
7846 Do not shrink WINDOW to less than `window-min-height' lines. Do
7847 nothing if the buffer contains more lines than the present window
7848 height, or if some of the window's contents are scrolled out of
7849 view, or if shrinking this window would also shrink another
7850 window, or if the window is the only window of its frame.
7851
7852 Return non-nil if the window was shrunk, nil otherwise."
7853 (interactive)
7854 (setq window (window-normalize-window window t))
7855 ;; Make sure that WINDOW is vertically combined and `point-min' is
7856 ;; visible (for whatever reason that's needed). The remaining issues
7857 ;; should be taken care of by `fit-window-to-buffer'.
7858 (when (and (window-combined-p window)
7859 (pos-visible-in-window-p (point-min) window))
7860 (fit-window-to-buffer window (window-total-height window))))
7861 \f
7862 (defun kill-buffer-and-window ()
7863 "Kill the current buffer and delete the selected window."
7864 (interactive)
7865 (let ((window-to-delete (selected-window))
7866 (buffer-to-kill (current-buffer))
7867 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7868 (unwind-protect
7869 (progn
7870 (add-hook 'kill-buffer-hook delete-window-hook t t)
7871 (if (kill-buffer (current-buffer))
7872 ;; If `delete-window' failed before, we rerun it to regenerate
7873 ;; the error so it can be seen in the echo area.
7874 (when (eq (selected-window) window-to-delete)
7875 (delete-window))))
7876 ;; If the buffer is not dead for some reason (probably because
7877 ;; of a `quit' signal), remove the hook again.
7878 (ignore-errors
7879 (with-current-buffer buffer-to-kill
7880 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
7881
7882 \f
7883 (defvar recenter-last-op nil
7884 "Indicates the last recenter operation performed.
7885 Possible values: `top', `middle', `bottom', integer or float numbers.
7886 It can also be nil, which means the first value in `recenter-positions'.")
7887
7888 (defcustom recenter-positions '(middle top bottom)
7889 "Cycling order for `recenter-top-bottom'.
7890 A list of elements with possible values `top', `middle', `bottom',
7891 integer or float numbers that define the cycling order for
7892 the command `recenter-top-bottom'.
7893
7894 Top and bottom destinations are `scroll-margin' lines from the true
7895 window top and bottom. Middle redraws the frame and centers point
7896 vertically within the window. Integer number moves current line to
7897 the specified absolute window-line. Float number between 0.0 and 1.0
7898 means the percentage of the screen space from the top. The default
7899 cycling order is middle -> top -> bottom."
7900 :type '(repeat (choice
7901 (const :tag "Top" top)
7902 (const :tag "Middle" middle)
7903 (const :tag "Bottom" bottom)
7904 (integer :tag "Line number")
7905 (float :tag "Percentage")))
7906 :version "23.2"
7907 :group 'windows)
7908
7909 (defun recenter-top-bottom (&optional arg)
7910 "Move current buffer line to the specified window line.
7911 With no prefix argument, successive calls place point according
7912 to the cycling order defined by `recenter-positions'.
7913
7914 A prefix argument is handled like `recenter':
7915 With numeric prefix ARG, move current line to window-line ARG.
7916 With plain `C-u', move current line to window center."
7917 (interactive "P")
7918 (cond
7919 (arg (recenter arg)) ; Always respect ARG.
7920 (t
7921 (setq recenter-last-op
7922 (if (eq this-command last-command)
7923 (car (or (cdr (member recenter-last-op recenter-positions))
7924 recenter-positions))
7925 (car recenter-positions)))
7926 (let ((this-scroll-margin
7927 (min (max 0 scroll-margin)
7928 (truncate (/ (window-body-height) 4.0)))))
7929 (cond ((eq recenter-last-op 'middle)
7930 (recenter))
7931 ((eq recenter-last-op 'top)
7932 (recenter this-scroll-margin))
7933 ((eq recenter-last-op 'bottom)
7934 (recenter (- -1 this-scroll-margin)))
7935 ((integerp recenter-last-op)
7936 (recenter recenter-last-op))
7937 ((floatp recenter-last-op)
7938 (recenter (round (* recenter-last-op (window-height))))))))))
7939
7940 (define-key global-map [?\C-l] 'recenter-top-bottom)
7941
7942 (defun move-to-window-line-top-bottom (&optional arg)
7943 "Position point relative to window.
7944
7945 With a prefix argument ARG, acts like `move-to-window-line'.
7946
7947 With no argument, positions point at center of window.
7948 Successive calls position point at positions defined
7949 by `recenter-positions'."
7950 (interactive "P")
7951 (cond
7952 (arg (move-to-window-line arg)) ; Always respect ARG.
7953 (t
7954 (setq recenter-last-op
7955 (if (eq this-command last-command)
7956 (car (or (cdr (member recenter-last-op recenter-positions))
7957 recenter-positions))
7958 (car recenter-positions)))
7959 (let ((this-scroll-margin
7960 (min (max 0 scroll-margin)
7961 (truncate (/ (window-body-height) 4.0)))))
7962 (cond ((eq recenter-last-op 'middle)
7963 (call-interactively 'move-to-window-line))
7964 ((eq recenter-last-op 'top)
7965 (move-to-window-line this-scroll-margin))
7966 ((eq recenter-last-op 'bottom)
7967 (move-to-window-line (- -1 this-scroll-margin)))
7968 ((integerp recenter-last-op)
7969 (move-to-window-line recenter-last-op))
7970 ((floatp recenter-last-op)
7971 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
7972
7973 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
7974 \f
7975 ;;; Scrolling commands.
7976
7977 ;;; Scrolling commands which do not signal errors at top/bottom
7978 ;;; of buffer at first key-press (instead move to top/bottom
7979 ;;; of buffer).
7980
7981 (defcustom scroll-error-top-bottom nil
7982 "Move point to top/bottom of buffer before signaling a scrolling error.
7983 A value of nil means just signal an error if no more scrolling possible.
7984 A value of t means point moves to the beginning or the end of the buffer
7985 \(depending on scrolling direction) when no more scrolling possible.
7986 When point is already on that position, then signal an error."
7987 :type 'boolean
7988 :group 'windows
7989 :version "24.1")
7990
7991 (defun scroll-up-command (&optional arg)
7992 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7993 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7994 scroll window further, move cursor to the bottom line.
7995 When point is already on that position, then signal an error.
7996 A near full screen is `next-screen-context-lines' less than a full screen.
7997 Negative ARG means scroll downward.
7998 If ARG is the atom `-', scroll downward by nearly full screen."
7999 (interactive "^P")
8000 (cond
8001 ((null scroll-error-top-bottom)
8002 (scroll-up arg))
8003 ((eq arg '-)
8004 (scroll-down-command nil))
8005 ((< (prefix-numeric-value arg) 0)
8006 (scroll-down-command (- (prefix-numeric-value arg))))
8007 ((eobp)
8008 (scroll-up arg)) ; signal error
8009 (t
8010 (condition-case nil
8011 (scroll-up arg)
8012 (end-of-buffer
8013 (if arg
8014 ;; When scrolling by ARG lines can't be done,
8015 ;; move by ARG lines instead.
8016 (forward-line arg)
8017 ;; When ARG is nil for full-screen scrolling,
8018 ;; move to the bottom of the buffer.
8019 (goto-char (point-max))))))))
8020
8021 (put 'scroll-up-command 'scroll-command t)
8022
8023 (defun scroll-down-command (&optional arg)
8024 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
8025 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
8026 scroll window further, move cursor to the top line.
8027 When point is already on that position, then signal an error.
8028 A near full screen is `next-screen-context-lines' less than a full screen.
8029 Negative ARG means scroll upward.
8030 If ARG is the atom `-', scroll upward by nearly full screen."
8031 (interactive "^P")
8032 (cond
8033 ((null scroll-error-top-bottom)
8034 (scroll-down arg))
8035 ((eq arg '-)
8036 (scroll-up-command nil))
8037 ((< (prefix-numeric-value arg) 0)
8038 (scroll-up-command (- (prefix-numeric-value arg))))
8039 ((bobp)
8040 (scroll-down arg)) ; signal error
8041 (t
8042 (condition-case nil
8043 (scroll-down arg)
8044 (beginning-of-buffer
8045 (if arg
8046 ;; When scrolling by ARG lines can't be done,
8047 ;; move by ARG lines instead.
8048 (forward-line (- arg))
8049 ;; When ARG is nil for full-screen scrolling,
8050 ;; move to the top of the buffer.
8051 (goto-char (point-min))))))))
8052
8053 (put 'scroll-down-command 'scroll-command t)
8054
8055 ;;; Scrolling commands which scroll a line instead of full screen.
8056
8057 (defun scroll-up-line (&optional arg)
8058 "Scroll text of selected window upward ARG lines; or one line if no ARG.
8059 If ARG is omitted or nil, scroll upward by one line.
8060 This is different from `scroll-up-command' that scrolls a full screen."
8061 (interactive "p")
8062 (scroll-up (or arg 1)))
8063
8064 (put 'scroll-up-line 'scroll-command t)
8065
8066 (defun scroll-down-line (&optional arg)
8067 "Scroll text of selected window down ARG lines; or one line if no ARG.
8068 If ARG is omitted or nil, scroll down by one line.
8069 This is different from `scroll-down-command' that scrolls a full screen."
8070 (interactive "p")
8071 (scroll-down (or arg 1)))
8072
8073 (put 'scroll-down-line 'scroll-command t)
8074
8075 \f
8076 (defun scroll-other-window-down (&optional lines)
8077 "Scroll the \"other window\" down.
8078 For more details, see the documentation for `scroll-other-window'."
8079 (interactive "P")
8080 (scroll-other-window
8081 ;; Just invert the argument's meaning.
8082 ;; We can do that without knowing which window it will be.
8083 (if (eq lines '-) nil
8084 (if (null lines) '-
8085 (- (prefix-numeric-value lines))))))
8086
8087 (defun beginning-of-buffer-other-window (arg)
8088 "Move point to the beginning of the buffer in the other window.
8089 Leave mark at previous position.
8090 With arg N, put point N/10 of the way from the true beginning."
8091 (interactive "P")
8092 (let ((orig-window (selected-window))
8093 (window (other-window-for-scrolling)))
8094 ;; We use unwind-protect rather than save-window-excursion
8095 ;; because the latter would preserve the things we want to change.
8096 (unwind-protect
8097 (progn
8098 (select-window window)
8099 ;; Set point and mark in that window's buffer.
8100 (with-no-warnings
8101 (beginning-of-buffer arg))
8102 ;; Set point accordingly.
8103 (recenter '(t)))
8104 (select-window orig-window))))
8105
8106 (defun end-of-buffer-other-window (arg)
8107 "Move point to the end of the buffer in the other window.
8108 Leave mark at previous position.
8109 With arg N, put point N/10 of the way from the true end."
8110 (interactive "P")
8111 ;; See beginning-of-buffer-other-window for comments.
8112 (let ((orig-window (selected-window))
8113 (window (other-window-for-scrolling)))
8114 (unwind-protect
8115 (progn
8116 (select-window window)
8117 (with-no-warnings
8118 (end-of-buffer arg))
8119 (recenter '(t)))
8120 (select-window orig-window))))
8121 \f
8122 (defvar mouse-autoselect-window-timer nil
8123 "Timer used by delayed window autoselection.")
8124
8125 (defvar mouse-autoselect-window-position-1 nil
8126 "First mouse position recorded by delayed window autoselection.")
8127
8128 (defvar mouse-autoselect-window-position nil
8129 "Last mouse position recorded by delayed window autoselection.")
8130
8131 (defvar mouse-autoselect-window-window nil
8132 "Last window recorded by delayed window autoselection.")
8133
8134 (defvar mouse-autoselect-window-state nil
8135 "When non-nil, special state of delayed window autoselection.
8136 Possible values are `suspend' (suspend autoselection after a menu or
8137 scrollbar interaction) and `select' (the next invocation of
8138 `handle-select-window' shall select the window immediately).")
8139
8140 (defun mouse-autoselect-window-cancel (&optional force)
8141 "Cancel delayed window autoselection.
8142 Optional argument FORCE means cancel unconditionally."
8143 (unless (and (not force)
8144 ;; Don't cancel for select-window or select-frame events
8145 ;; or when the user drags a scroll bar.
8146 (or (memq this-command
8147 '(handle-select-window handle-switch-frame))
8148 (and (eq this-command 'scroll-bar-toolkit-scroll)
8149 (memq (nth 4 (event-end last-input-event))
8150 '(handle end-scroll)))))
8151 (setq mouse-autoselect-window-state nil)
8152 (setq mouse-autoselect-window-position-1 nil)
8153 (when (timerp mouse-autoselect-window-timer)
8154 (cancel-timer mouse-autoselect-window-timer))
8155 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
8156
8157 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
8158 "Start delayed window autoselection.
8159 MOUSE-POSITION is the last position where the mouse was seen as returned
8160 by `mouse-position'. Optional argument WINDOW non-nil denotes the
8161 window where the mouse was seen. Optional argument SUSPEND non-nil
8162 means suspend autoselection."
8163 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
8164 (setq mouse-autoselect-window-position mouse-position)
8165 (when window (setq mouse-autoselect-window-window window))
8166 (setq mouse-autoselect-window-state (when suspend 'suspend))
8167 ;; Install timer which runs `mouse-autoselect-window-select' after
8168 ;; `mouse-autoselect-window' seconds.
8169 (setq mouse-autoselect-window-timer
8170 (run-at-time
8171 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
8172
8173 (defun mouse-autoselect-window-select ()
8174 "Select window with delayed window autoselection.
8175 If the mouse position has stabilized in a non-selected window, select
8176 that window. The minibuffer window is selected only if the minibuffer
8177 is active. This function is run by `mouse-autoselect-window-timer'."
8178 (ignore-errors
8179 (let* ((mouse-position (mouse-position))
8180 (window
8181 (ignore-errors
8182 (window-at (cadr mouse-position) (cddr mouse-position)
8183 (car mouse-position)))))
8184 (cond
8185 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
8186 (and window
8187 (let ((coords (coordinates-in-window-p
8188 (cdr mouse-position) window)))
8189 (and (not (consp coords))
8190 (not (memq coords '(left-margin right-margin)))))))
8191 ;; A menu / popup dialog is active or the mouse is not on the
8192 ;; text region of WINDOW: Suspend autoselection temporarily.
8193 (mouse-autoselect-window-start mouse-position nil t))
8194 ((or (eq mouse-autoselect-window-state 'suspend)
8195 ;; When the mouse is at its first recorded position, restart
8196 ;; delayed autoselection. This works around a scenario with
8197 ;; two two-window frames with identical dimensions: select the
8198 ;; first window of the first frame, switch to the second
8199 ;; frame, move the mouse to its second window, minimize the
8200 ;; second frame. Now the second window of the first frame
8201 ;; gets selected although the mouse never really "moved" into
8202 ;; that window.
8203 (and (numberp mouse-autoselect-window)
8204 (equal (mouse-position) mouse-autoselect-window-position-1)))
8205 ;; Delayed autoselection was temporarily suspended, reenable it.
8206 (mouse-autoselect-window-start mouse-position))
8207 ((and window (not (eq window (selected-window)))
8208 (or (not (numberp mouse-autoselect-window))
8209 (and (>= mouse-autoselect-window 0)
8210 ;; If `mouse-autoselect-window' is non-negative,
8211 ;; select window if it's the same as before.
8212 (eq window mouse-autoselect-window-window))
8213 ;; Otherwise select window iff the mouse is at the same
8214 ;; position as before. Observe that the first test
8215 ;; after starting autoselection usually fails since the
8216 ;; value of `mouse-autoselect-window-position' recorded
8217 ;; there is the position where the mouse has entered the
8218 ;; new window and not necessarily where the mouse has
8219 ;; stopped moving.
8220 (equal mouse-position mouse-autoselect-window-position))
8221 ;; The minibuffer is a candidate window if it's active.
8222 (or (not (window-minibuffer-p window))
8223 (eq window (active-minibuffer-window))))
8224 ;; Mouse position has stabilized in non-selected window: Cancel
8225 ;; delayed autoselection and try to select that window.
8226 (mouse-autoselect-window-cancel t)
8227 ;; Select window where mouse appears unless the selected window is the
8228 ;; minibuffer. Use `unread-command-events' in order to execute pre-
8229 ;; and post-command hooks and trigger idle timers. To avoid delaying
8230 ;; autoselection again, set `mouse-autoselect-window-state'."
8231 (unless (window-minibuffer-p)
8232 (setq mouse-autoselect-window-state 'select)
8233 (setq unread-command-events
8234 (cons (list 'select-window (list window))
8235 unread-command-events))))
8236 ((or (and window (eq window (selected-window)))
8237 (not (numberp mouse-autoselect-window))
8238 (equal mouse-position mouse-autoselect-window-position))
8239 ;; Mouse position has either stabilized in the selected window or at
8240 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
8241 (mouse-autoselect-window-cancel t))
8242 (t
8243 ;; Mouse position has not stabilized yet, resume delayed
8244 ;; autoselection.
8245 (mouse-autoselect-window-start mouse-position window))))))
8246
8247 (defun handle-select-window (event)
8248 "Handle select-window events."
8249 (interactive "^e")
8250 (let ((window (posn-window (event-start event))))
8251 (unless (or (not (window-live-p window))
8252 ;; Don't switch if we're currently in the minibuffer.
8253 ;; This tries to work around problems where the
8254 ;; minibuffer gets unselected unexpectedly, and where
8255 ;; you then have to move your mouse all the way down to
8256 ;; the minibuffer to select it.
8257 (window-minibuffer-p)
8258 ;; Don't switch to minibuffer window unless it's active.
8259 (and (window-minibuffer-p window)
8260 (not (minibuffer-window-active-p window)))
8261 ;; Don't switch when autoselection shall be delayed.
8262 (and (numberp mouse-autoselect-window)
8263 (not (eq mouse-autoselect-window-state 'select))
8264 (let ((position (mouse-position)))
8265 ;; Cancel any delayed autoselection.
8266 (mouse-autoselect-window-cancel t)
8267 ;; Start delayed autoselection from current mouse
8268 ;; position and window.
8269 (setq mouse-autoselect-window-position-1 position)
8270 (mouse-autoselect-window-start position window)
8271 ;; Executing a command cancels delayed autoselection.
8272 (add-hook
8273 'pre-command-hook 'mouse-autoselect-window-cancel))))
8274 (when mouse-autoselect-window
8275 ;; Reset state of delayed autoselection.
8276 (setq mouse-autoselect-window-state nil)
8277 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
8278 (run-hooks 'mouse-leave-buffer-hook))
8279 ;; Clear echo area.
8280 (message nil)
8281 (select-window window))))
8282
8283 (defun truncated-partial-width-window-p (&optional window)
8284 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
8285 WINDOW must be a live window and defaults to the selected one.
8286 Return nil if WINDOW is not a partial-width window
8287 (regardless of the value of `truncate-lines').
8288 Otherwise, consult the value of `truncate-partial-width-windows'
8289 for the buffer shown in WINDOW."
8290 (setq window (window-normalize-window window t))
8291 (unless (window-full-width-p window)
8292 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
8293 (window-buffer window))))
8294 (if (integerp t-p-w-w)
8295 (< (window-width window) t-p-w-w)
8296 t-p-w-w))))
8297
8298 \f
8299 ;; Automatically inform subprocesses of changes to window size.
8300
8301 (defcustom window-adjust-process-window-size-function
8302 'window-adjust-process-window-size-smallest
8303 "Control how Emacs chooses inferior process window sizes.
8304 Emacs uses this function to tell processes the space they have
8305 available for displaying their output. After each window
8306 configuration change, Emacs calls the value of
8307 `window-adjust-process-window-size-function' for each process
8308 with a buffer being displayed in at least one window.
8309 This function is responsible for combining the sizes of the
8310 displayed windows and returning a cons (WIDTH . HEIGHT)
8311 describing the width and height with which Emacs will call
8312 `set-process-window-size' for that process. If the function
8313 returns nil, Emacs does not call `set-process-window-size'.
8314
8315 This function is called with the process buffer as the current
8316 buffer and with two arguments: the process and a list of windows
8317 displaying process. Modes can make this variable buffer-local;
8318 additionally, the `adjust-window-size-function' process property
8319 overrides the global or buffer-local value of
8320 `window-adjust-process-window-size-function'."
8321 :type '(choice
8322 (const :tag "Minimum area of any window"
8323 window-adjust-process-window-size-smallest)
8324 (const :tag "Maximum area of any window"
8325 window-adjust-process-window-size-largest)
8326 (const :tag "Do not adjust process window sizes" ignore)
8327 function)
8328 :group 'windows
8329 :version "25.1")
8330
8331 (defun window-adjust-process-window-size (reducer process windows)
8332 "Adjust the process window size of PROCESS.
8333 WINDOWS is a list of windows associated with PROCESS. REDUCER is
8334 a two-argument function used to combine the widths and heights of
8335 the given windows."
8336 (when windows
8337 (let ((width (window-body-width (car windows)))
8338 (height (window-body-height (car windows))))
8339 (dolist (window (cdr windows))
8340 (setf width (funcall reducer width (window-body-width window)))
8341 (setf height (funcall reducer height (window-body-height window))))
8342 (cons width height))))
8343
8344 (defun window-adjust-process-window-size-smallest (process windows)
8345 "Adjust the process window size of PROCESS.
8346 WINDOWS is a list of windows associated with PROCESS. Choose the
8347 smallest area available for displaying PROCESS's output."
8348 (window-adjust-process-window-size #'min process windows))
8349
8350 (defun window-adjust-process-window-size-largest (process windows)
8351 "Adjust the process window size of PROCESS.
8352 WINDOWS is a list of windows associated with PROCESS. Choose the
8353 largest area available for displaying PROCESS's output."
8354 (window-adjust-process-window-size #'max process windows))
8355
8356 (defun window--process-window-list ()
8357 "Return an alist mapping processes to associated windows.
8358 A window is associated with a process if that window is
8359 displaying that processes's buffer."
8360 (let ((processes (process-list))
8361 (process-windows nil))
8362 (walk-windows
8363 (lambda (window)
8364 (let ((buffer (window-buffer window))
8365 (iter processes))
8366 (while (let ((process (car iter)))
8367 (if (and (process-live-p process)
8368 (eq buffer (process-buffer process)))
8369 (let ((procwin (assq process process-windows)))
8370 ;; Add this window to the list of windows
8371 ;; displaying process.
8372 (if procwin
8373 (push window (cdr procwin))
8374 (push (list process window) process-windows))
8375 ;; We found our process for this window, so
8376 ;; stop iterating over the process list.
8377 nil)
8378 (setf iter (cdr iter)))))))
8379 1 t)
8380 process-windows))
8381
8382 (defun window--adjust-process-windows ()
8383 "Update process window sizes to match the current window configuration."
8384 (dolist (procwin (window--process-window-list))
8385 (let ((process (car procwin)))
8386 (with-demoted-errors "Error adjusting window size: %S"
8387 (with-current-buffer (process-buffer process)
8388 (let ((size (funcall
8389 (or (process-get process 'adjust-window-size-function)
8390 window-adjust-process-window-size-function)
8391 process (cdr procwin))))
8392 (when size
8393 (set-process-window-size process (cdr size) (car size)))))))))
8394
8395 (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
8396
8397 \f
8398 ;; Some of these are in tutorial--default-keys, so update that if you
8399 ;; change these.
8400 (define-key ctl-x-map "0" 'delete-window)
8401 (define-key ctl-x-map "1" 'delete-other-windows)
8402 (define-key ctl-x-map "2" 'split-window-below)
8403 (define-key ctl-x-map "3" 'split-window-right)
8404 (define-key ctl-x-map "o" 'other-window)
8405 (define-key ctl-x-map "^" 'enlarge-window)
8406 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
8407 (define-key ctl-x-map "{" 'shrink-window-horizontally)
8408 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
8409 (define-key ctl-x-map "+" 'balance-windows)
8410 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
8411
8412 ;;; window.el ends here