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