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