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