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 MR |
160 | (defconst window-safe-min-height 1 |
161 | "The absolut minimum number of lines of a window. | |
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 MR |
179 | (defconst window-safe-min-width 2 |
180 | "The absolut minimum number of columns of a window. | |
181 | Anything less might crash Emacs.") | |
182 | ||
562dd5e9 MR |
183 | (defcustom window-min-width 10 |
184 | "The minimum number of columns of any window. | |
185 | The value has to accomodate margins, fringes, or scrollbars if | |
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 | ||
1061 | (defun window-in-direction-2 (window posn &optional horizontal) | |
1062 | "Support function for `window-in-direction'." | |
1063 | (if horizontal | |
1064 | (let ((top (window-top-line window))) | |
1065 | (if (> top posn) | |
1066 | (- top posn) | |
1067 | (- posn top (window-total-height window)))) | |
1068 | (let ((left (window-left-column window))) | |
1069 | (if (> left posn) | |
1070 | (- left posn) | |
1071 | (- posn left (window-total-width window)))))) | |
1072 | ||
1073 | (defun window-in-direction (direction &optional window ignore) | |
1074 | "Return window in DIRECTION as seen from WINDOW. | |
1075 | DIRECTION must be one of `above', `below', `left' or `right'. | |
1076 | WINDOW must be a live window and defaults to the selected one. | |
1077 | IGNORE, when non-nil means a window can be returned even if its | |
1078 | `no-other-window' parameter is non-nil." | |
5386012d | 1079 | (setq window (window-normalize-live-window window)) |
387522b2 MR |
1080 | (unless (memq direction '(above below left right)) |
1081 | (error "Wrong direction %s" direction)) | |
1082 | (let* ((frame (window-frame window)) | |
1083 | (hor (memq direction '(left right))) | |
1084 | (first (if hor | |
1085 | (window-left-column window) | |
1086 | (window-top-line window))) | |
1087 | (last (+ first (if hor | |
1088 | (window-total-width window) | |
1089 | (window-total-height window)))) | |
1090 | (posn-cons (nth 6 (posn-at-point (window-point window) window))) | |
1091 | ;; The column / row value of `posn-at-point' can be nil for the | |
1092 | ;; mini-window, guard against that. | |
1093 | (posn (if hor | |
1094 | (+ (or (cdr posn-cons) 1) (window-top-line window)) | |
1095 | (+ (or (car posn-cons) 1) (window-left-column window)))) | |
1096 | (best-edge | |
1097 | (cond | |
1098 | ((eq direction 'below) (frame-height frame)) | |
1099 | ((eq direction 'right) (frame-width frame)) | |
1100 | (t -1))) | |
1101 | (best-edge-2 best-edge) | |
1102 | (best-diff-2 (if hor (frame-height frame) (frame-width frame))) | |
1103 | best best-2 best-diff-2-new) | |
1104 | (walk-window-tree | |
1105 | (lambda (w) | |
1106 | (let* ((w-top (window-top-line w)) | |
1107 | (w-left (window-left-column w))) | |
1108 | (cond | |
1109 | ((or (eq window w) | |
1110 | ;; Ignore ourselves. | |
1111 | (and (window-parameter w 'no-other-window) | |
1112 | ;; Ignore W unless IGNORE is non-nil. | |
1113 | (not ignore)))) | |
1114 | (hor | |
1115 | (cond | |
1116 | ((and (<= w-top posn) | |
1117 | (< posn (+ w-top (window-total-height w)))) | |
1118 | ;; W is to the left or right of WINDOW and covers POSN. | |
1119 | (when (or (and (eq direction 'left) | |
1120 | (<= w-left first) (> w-left best-edge)) | |
1121 | (and (eq direction 'right) | |
1122 | (>= w-left last) (< w-left best-edge))) | |
1123 | (setq best-edge w-left) | |
1124 | (setq best w))) | |
1125 | ((and (or (and (eq direction 'left) | |
1126 | (<= (+ w-left (window-total-width w)) first)) | |
1127 | (and (eq direction 'right) (<= last w-left))) | |
1128 | ;; W is to the left or right of WINDOW but does not | |
1129 | ;; cover POSN. | |
1130 | (setq best-diff-2-new | |
1131 | (window-in-direction-2 w posn hor)) | |
1132 | (or (< best-diff-2-new best-diff-2) | |
1133 | (and (= best-diff-2-new best-diff-2) | |
1134 | (if (eq direction 'left) | |
1135 | (> w-left best-edge-2) | |
1136 | (< w-left best-edge-2))))) | |
1137 | (setq best-edge-2 w-left) | |
1138 | (setq best-diff-2 best-diff-2-new) | |
1139 | (setq best-2 w)))) | |
1140 | (t | |
1141 | (cond | |
1142 | ((and (<= w-left posn) | |
1143 | (< posn (+ w-left (window-total-width w)))) | |
1144 | ;; W is above or below WINDOW and covers POSN. | |
1145 | (when (or (and (eq direction 'above) | |
1146 | (<= w-top first) (> w-top best-edge)) | |
1147 | (and (eq direction 'below) | |
1148 | (>= w-top first) (< w-top best-edge))) | |
1149 | (setq best-edge w-top) | |
1150 | (setq best w))) | |
1151 | ((and (or (and (eq direction 'above) | |
1152 | (<= (+ w-top (window-total-height w)) first)) | |
1153 | (and (eq direction 'below) (<= last w-top))) | |
1154 | ;; W is above or below WINDOW but does not cover POSN. | |
1155 | (setq best-diff-2-new | |
1156 | (window-in-direction-2 w posn hor)) | |
1157 | (or (< best-diff-2-new best-diff-2) | |
1158 | (and (= best-diff-2-new best-diff-2) | |
1159 | (if (eq direction 'above) | |
1160 | (> w-top best-edge-2) | |
1161 | (< w-top best-edge-2))))) | |
1162 | (setq best-edge-2 w-top) | |
1163 | (setq best-diff-2 best-diff-2-new) | |
1164 | (setq best-2 w))))))) | |
1165 | (window-frame window)) | |
1166 | (or best best-2))) | |
3c448ab6 | 1167 | |
4c43d97b | 1168 | (defun get-window-with-predicate (predicate &optional minibuf all-frames default) |
387522b2 MR |
1169 | "Return a live window satisfying PREDICATE. |
1170 | More precisely, cycle through all windows calling the function | |
1171 | PREDICATE on each one of them with the window as its sole | |
1172 | argument. Return the first window for which PREDICATE returns | |
4c43d97b MR |
1173 | non-nil. Windows are scanned starting with the window following |
1174 | the selcted window. If no window satisfies PREDICATE, return | |
1175 | DEFAULT. | |
1176 | ||
1177 | MINIBUF t means include the minibuffer window even if the | |
1178 | minibuffer is not active. MINIBUF nil or omitted means include | |
1179 | the minibuffer window only if the minibuffer is active. Any | |
1180 | other value means do not include the minibuffer window even if | |
1181 | the minibuffer is active. | |
387522b2 MR |
1182 | |
1183 | ALL-FRAMES nil or omitted means consider all windows on the selected | |
1184 | frame, plus the minibuffer window if specified by the MINIBUF | |
1185 | argument. If the minibuffer counts, consider all windows on all | |
1186 | frames that share that minibuffer too. The following non-nil | |
1187 | values of ALL-FRAMES have special meanings: | |
3c448ab6 | 1188 | |
387522b2 MR |
1189 | - t means consider all windows on all existing frames. |
1190 | ||
1191 | - `visible' means consider all windows on all visible frames on | |
1192 | the current terminal. | |
1193 | ||
1194 | - 0 (the number zero) means consider all windows on all visible | |
1195 | and iconified frames on the current terminal. | |
1196 | ||
1197 | - A frame means consider all windows on that frame only. | |
1198 | ||
1199 | Anything else means consider all windows on the selected frame | |
1200 | and no others." | |
3c448ab6 | 1201 | (catch 'found |
4c43d97b MR |
1202 | (dolist (window (window-list-1 |
1203 | (next-window nil minibuf all-frames) | |
1204 | minibuf all-frames)) | |
387522b2 MR |
1205 | (when (funcall predicate window) |
1206 | (throw 'found window))) | |
3c448ab6 MR |
1207 | default)) |
1208 | ||
1209 | (defalias 'some-window 'get-window-with-predicate) | |
1210 | ||
190b47e6 MR |
1211 | (defun get-lru-window (&optional all-frames dedicated) |
1212 | "Return the least recently used window on frames specified by ALL-FRAMES. | |
1213 | Return a full-width window if possible. A minibuffer window is | |
1214 | never a candidate. A dedicated window is never a candidate | |
1215 | unless DEDICATED is non-nil, so if all windows are dedicated, the | |
1216 | value is nil. Avoid returning the selected window if possible. | |
1217 | ||
1218 | The following non-nil values of the optional argument ALL-FRAMES | |
1219 | have special meanings: | |
1220 | ||
1221 | - t means consider all windows on all existing frames. | |
1222 | ||
1223 | - `visible' means consider all windows on all visible frames on | |
1224 | the current terminal. | |
1225 | ||
1226 | - 0 (the number zero) means consider all windows on all visible | |
1227 | and iconified frames on the current terminal. | |
1228 | ||
1229 | - A frame means consider all windows on that frame only. | |
1230 | ||
1231 | Any other value of ALL-FRAMES means consider all windows on the | |
1232 | selected frame and no others." | |
1233 | (let (best-window best-time second-best-window second-best-time time) | |
02cfc6d6 | 1234 | (dolist (window (window-list-1 nil 'nomini all-frames)) |
190b47e6 MR |
1235 | (when (or dedicated (not (window-dedicated-p window))) |
1236 | (setq time (window-use-time window)) | |
1237 | (if (or (eq window (selected-window)) | |
1238 | (not (window-full-width-p window))) | |
1239 | (when (or (not second-best-time) (< time second-best-time)) | |
1240 | (setq second-best-time time) | |
1241 | (setq second-best-window window)) | |
1242 | (when (or (not best-time) (< time best-time)) | |
1243 | (setq best-time time) | |
1244 | (setq best-window window))))) | |
1245 | (or best-window second-best-window))) | |
1246 | ||
387522b2 MR |
1247 | (defun get-mru-window (&optional all-frames) |
1248 | "Return the most recently used window on frames specified by ALL-FRAMES. | |
1249 | Do not return a minibuffer window. | |
1250 | ||
1251 | The following non-nil values of the optional argument ALL-FRAMES | |
1252 | have special meanings: | |
1253 | ||
1254 | - t means consider all windows on all existing frames. | |
1255 | ||
1256 | - `visible' means consider all windows on all visible frames on | |
1257 | the current terminal. | |
1258 | ||
1259 | - 0 (the number zero) means consider all windows on all visible | |
1260 | and iconified frames on the current terminal. | |
1261 | ||
1262 | - A frame means consider all windows on that frame only. | |
1263 | ||
1264 | Any other value of ALL-FRAMES means consider all windows on the | |
1265 | selected frame and no others." | |
1266 | (let (best-window best-time time) | |
02cfc6d6 | 1267 | (dolist (window (window-list-1 nil 'nomini all-frames)) |
387522b2 MR |
1268 | (setq time (window-use-time window)) |
1269 | (when (or (not best-time) (> time best-time)) | |
1270 | (setq best-time time) | |
1271 | (setq best-window window))) | |
1272 | best-window)) | |
1273 | ||
190b47e6 MR |
1274 | (defun get-largest-window (&optional all-frames dedicated) |
1275 | "Return the largest window on frames specified by ALL-FRAMES. | |
1276 | A minibuffer window is never a candidate. A dedicated window is | |
1277 | never a candidate unless DEDICATED is non-nil, so if all windows | |
1278 | are dedicated, the value is nil. | |
1279 | ||
1280 | The following non-nil values of the optional argument ALL-FRAMES | |
1281 | have special meanings: | |
1282 | ||
1283 | - t means consider all windows on all existing frames. | |
1284 | ||
1285 | - `visible' means consider all windows on all visible frames on | |
1286 | the current terminal. | |
1287 | ||
1288 | - 0 (the number zero) means consider all windows on all visible | |
1289 | and iconified frames on the current terminal. | |
1290 | ||
1291 | - A frame means consider all windows on that frame only. | |
1292 | ||
1293 | Any other value of ALL-FRAMES means consider all windows on the | |
1294 | selected frame and no others." | |
1295 | (let ((best-size 0) | |
1296 | best-window size) | |
02cfc6d6 | 1297 | (dolist (window (window-list-1 nil 'nomini all-frames)) |
190b47e6 MR |
1298 | (when (or dedicated (not (window-dedicated-p window))) |
1299 | (setq size (* (window-total-size window) | |
1300 | (window-total-size window t))) | |
1301 | (when (> size best-size) | |
1302 | (setq best-size size) | |
1303 | (setq best-window window)))) | |
1304 | best-window)) | |
1305 | ||
3c448ab6 MR |
1306 | (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames) |
1307 | "Return list of all windows displaying BUFFER-OR-NAME, or nil if none. | |
1308 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer | |
4c43d97b MR |
1309 | and defaults to the current buffer. Windows are scanned starting |
1310 | with the selected window. | |
190b47e6 MR |
1311 | |
1312 | MINIBUF t means include the minibuffer window even if the | |
1313 | minibuffer is not active. MINIBUF nil or omitted means include | |
1314 | the minibuffer window only if the minibuffer is active. Any | |
1315 | other value means do not include the minibuffer window even if | |
1316 | the minibuffer is active. | |
1317 | ||
1318 | ALL-FRAMES nil or omitted means consider all windows on the | |
1319 | selected frame, plus the minibuffer window if specified by the | |
1320 | MINIBUF argument. If the minibuffer counts, consider all windows | |
1321 | on all frames that share that minibuffer too. The following | |
1322 | non-nil values of ALL-FRAMES have special meanings: | |
1323 | ||
1324 | - t means consider all windows on all existing frames. | |
1325 | ||
1326 | - `visible' means consider all windows on all visible frames on | |
1327 | the current terminal. | |
1328 | ||
1329 | - 0 (the number zero) means consider all windows on all visible | |
1330 | and iconified frames on the current terminal. | |
1331 | ||
1332 | - A frame means consider all windows on that frame only. | |
1333 | ||
1334 | Anything else means consider all windows on the selected frame | |
1335 | and no others." | |
5386012d | 1336 | (let ((buffer (window-normalize-buffer buffer-or-name)) |
3c448ab6 | 1337 | windows) |
4c43d97b | 1338 | (dolist (window (window-list-1 (selected-window) minibuf all-frames)) |
190b47e6 MR |
1339 | (when (eq (window-buffer window) buffer) |
1340 | (setq windows (cons window windows)))) | |
1341 | (nreverse windows))) | |
3c448ab6 MR |
1342 | |
1343 | (defun minibuffer-window-active-p (window) | |
1344 | "Return t if WINDOW is the currently active minibuffer window." | |
1345 | (eq window (active-minibuffer-window))) | |
387522b2 | 1346 | |
3c448ab6 | 1347 | (defun count-windows (&optional minibuf) |
387522b2 | 1348 | "Return the number of live windows on the selected frame. |
3c448ab6 MR |
1349 | The optional argument MINIBUF specifies whether the minibuffer |
1350 | window shall be counted. See `walk-windows' for the precise | |
1351 | meaning of this argument." | |
387522b2 | 1352 | (length (window-list-1 nil minibuf))) |
9aab8e0d MR |
1353 | \f |
1354 | ;;; Resizing windows. | |
5386012d | 1355 | (defun window--resize-reset (&optional frame horizontal) |
9aab8e0d MR |
1356 | "Reset resize values for all windows on FRAME. |
1357 | FRAME defaults to the selected frame. | |
1358 | ||
1359 | This function stores the current value of `window-total-size' applied | |
1360 | with argument HORIZONTAL in the new total size of all windows on | |
1361 | FRAME. It also resets the new normal size of each of these | |
1362 | windows." | |
5386012d MR |
1363 | (window--resize-reset-1 |
1364 | (frame-root-window (window-normalize-frame frame)) horizontal)) | |
9aab8e0d | 1365 | |
5386012d MR |
1366 | (defun window--resize-reset-1 (window horizontal) |
1367 | "Internal function of `window--resize-reset'." | |
9aab8e0d MR |
1368 | ;; Register old size in the new total size. |
1369 | (set-window-new-total window (window-total-size window horizontal)) | |
1370 | ;; Reset new normal size. | |
1371 | (set-window-new-normal window) | |
1372 | (when (window-child window) | |
5386012d | 1373 | (window--resize-reset-1 (window-child window) horizontal)) |
9aab8e0d | 1374 | (when (window-right window) |
5386012d | 1375 | (window--resize-reset-1 (window-right window) horizontal))) |
9aab8e0d | 1376 | |
562dd5e9 MR |
1377 | ;; The following routine is used to manually resize the minibuffer |
1378 | ;; window and is currently used, for example, by ispell.el. | |
5386012d | 1379 | (defun window--resize-mini-window (window delta) |
562dd5e9 MR |
1380 | "Resize minibuffer window WINDOW by DELTA lines. |
1381 | If WINDOW cannot be resized by DELTA lines make it as large \(or | |
1382 | as small) as possible but don't signal an error." | |
1383 | (when (window-minibuffer-p window) | |
1384 | (let* ((frame (window-frame window)) | |
1385 | (root (frame-root-window frame)) | |
1386 | (height (window-total-size window)) | |
1387 | (min-delta | |
1388 | (- (window-total-size root) | |
1389 | (window-min-size root)))) | |
1390 | ;; Sanitize DELTA. | |
1391 | (cond | |
1392 | ((<= (+ height delta) 0) | |
1393 | (setq delta (- (- height 1)))) | |
1394 | ((> delta min-delta) | |
1395 | (setq delta min-delta))) | |
1396 | ||
1397 | ;; Resize now. | |
5386012d | 1398 | (window--resize-reset frame) |
562dd5e9 MR |
1399 | ;; Ideally we should be able to resize just the last subwindow of |
1400 | ;; root here. See the comment in `resize-root-window-vertically' | |
1401 | ;; for why we do not do that. | |
5386012d | 1402 | (window--resize-this-window root (- delta) nil nil t) |
562dd5e9 MR |
1403 | (set-window-new-total window (+ height delta)) |
1404 | ;; The following routine catches the case where we want to resize | |
1405 | ;; a minibuffer-only frame. | |
1406 | (resize-mini-window-internal window)))) | |
1407 | ||
d615d6d2 | 1408 | (defun window-resize (window delta &optional horizontal ignore) |
562dd5e9 MR |
1409 | "Resize WINDOW vertically by DELTA lines. |
1410 | WINDOW can be an arbitrary window and defaults to the selected | |
1411 | one. An attempt to resize the root window of a frame will raise | |
1412 | an error though. | |
1413 | ||
1414 | DELTA a positive number means WINDOW shall be enlarged by DELTA | |
1415 | lines. DELTA negative means WINDOW shall be shrunk by -DELTA | |
1416 | lines. | |
1417 | ||
1418 | Optional argument HORIZONTAL non-nil means resize WINDOW | |
1419 | horizontally by DELTA columns. In this case a positive DELTA | |
1420 | means enlarge WINDOW by DELTA columns. DELTA negative means | |
1421 | WINDOW shall be shrunk by -DELTA columns. | |
1422 | ||
1423 | Optional argument IGNORE non-nil means ignore any restrictions | |
1424 | imposed by fixed size windows, `window-min-height' or | |
1425 | `window-min-width' settings. IGNORE any window means ignore | |
1426 | restrictions for that window only. IGNORE equal `safe' means | |
1427 | live windows may get as small as `window-safe-min-height' lines | |
1428 | and `window-safe-min-width' columns. | |
1429 | ||
1430 | This function resizes other windows proportionally and never | |
1431 | deletes any windows. If you want to move only the low (right) | |
1432 | edge of WINDOW consider using `adjust-window-trailing-edge' | |
1433 | instead." | |
5386012d | 1434 | (setq window (window-normalize-any-window window)) |
562dd5e9 MR |
1435 | (let* ((frame (window-frame window)) |
1436 | sibling) | |
1437 | (cond | |
1438 | ((eq window (frame-root-window frame)) | |
1439 | (error "Cannot resize the root window of a frame")) | |
1440 | ((window-minibuffer-p window) | |
5386012d | 1441 | (window--resize-mini-window window delta)) |
562dd5e9 | 1442 | ((window-resizable-p window delta horizontal ignore) |
5386012d MR |
1443 | (window--resize-reset frame horizontal) |
1444 | (window--resize-this-window window delta horizontal ignore t) | |
562dd5e9 MR |
1445 | (if (and (not (window-splits window)) |
1446 | (window-iso-combined-p window horizontal) | |
1447 | (setq sibling (or (window-right window) (window-left window))) | |
1448 | (window-sizable-p sibling (- delta) horizontal ignore)) | |
1449 | ;; If window-splits returns nil for WINDOW, WINDOW is part of | |
1450 | ;; an iso-combination, and WINDOW's neighboring right or left | |
1451 | ;; sibling can be resized as requested, resize that sibling. | |
1452 | (let ((normal-delta | |
1453 | (/ (float delta) | |
1454 | (window-total-size (window-parent window) horizontal)))) | |
5386012d | 1455 | (window--resize-this-window sibling (- delta) horizontal nil t) |
562dd5e9 MR |
1456 | (set-window-new-normal |
1457 | window (+ (window-normal-size window horizontal) | |
1458 | normal-delta)) | |
1459 | (set-window-new-normal | |
1460 | sibling (- (window-normal-size sibling horizontal) | |
1461 | normal-delta))) | |
1462 | ;; Otherwise, resize all other windows in the same combination. | |
5386012d | 1463 | (window--resize-siblings window delta horizontal ignore)) |
d615d6d2 | 1464 | (window-resize-apply frame horizontal)) |
562dd5e9 MR |
1465 | (t |
1466 | (error "Cannot resize window %s" window))))) | |
1467 | ||
5386012d | 1468 | (defsubst window--resize-subwindows-skip-p (window) |
9aab8e0d MR |
1469 | "Return non-nil if WINDOW shall be skipped by resizing routines." |
1470 | (memq (window-new-normal window) '(ignore stuck skip))) | |
1471 | ||
5386012d | 1472 | (defun window--resize-subwindows-normal (parent horizontal window this-delta &optional trail other-delta) |
9aab8e0d MR |
1473 | "Set the new normal height of subwindows of window PARENT. |
1474 | HORIZONTAL non-nil means set the new normal width of these | |
1475 | windows. WINDOW specifies a subwindow of PARENT that has been | |
1476 | resized by THIS-DELTA lines \(columns). | |
1477 | ||
1478 | Optional argument TRAIL either 'before or 'after means set values | |
1479 | for windows before or after WINDOW only. Optional argument | |
1480 | OTHER-DELTA a number specifies that this many lines \(columns) | |
1481 | have been obtained from \(or returned to) an ancestor window of | |
1482 | PARENT in order to resize WINDOW." | |
1483 | (let* ((delta-normal | |
1484 | (if (and (= (- this-delta) (window-total-size window horizontal)) | |
1485 | (zerop other-delta)) | |
1486 | ;; When WINDOW gets deleted and we can return its entire | |
1487 | ;; space to its siblings, use WINDOW's normal size as the | |
1488 | ;; normal delta. | |
1489 | (- (window-normal-size window horizontal)) | |
1490 | ;; In any other case calculate the normal delta from the | |
1491 | ;; relation of THIS-DELTA to the total size of PARENT. | |
1492 | (/ (float this-delta) (window-total-size parent horizontal)))) | |
1493 | (sub (window-child parent)) | |
1494 | (parent-normal 0.0) | |
1495 | (skip (eq trail 'after))) | |
1496 | ||
1497 | ;; Set parent-normal to the sum of the normal sizes of all | |
1498 | ;; subwindows of PARENT that shall be resized, excluding only WINDOW | |
1499 | ;; and any windows specified by the optional TRAIL argument. | |
1500 | (while sub | |
1501 | (cond | |
1502 | ((eq sub window) | |
1503 | (setq skip (eq trail 'before))) | |
1504 | (skip) | |
1505 | (t | |
1506 | (setq parent-normal | |
1507 | (+ parent-normal (window-normal-size sub horizontal))))) | |
1508 | (setq sub (window-right sub))) | |
1509 | ||
1510 | ;; Set the new normal size of all subwindows of PARENT from what | |
1511 | ;; they should have contributed for recovering THIS-DELTA lines | |
1512 | ;; (columns). | |
1513 | (setq sub (window-child parent)) | |
1514 | (setq skip (eq trail 'after)) | |
1515 | (while sub | |
1516 | (cond | |
1517 | ((eq sub window) | |
1518 | (setq skip (eq trail 'before))) | |
1519 | (skip) | |
1520 | (t | |
1521 | (let ((old-normal (window-normal-size sub horizontal))) | |
1522 | (set-window-new-normal | |
1523 | sub (min 1.0 ; Don't get larger than 1. | |
1524 | (max (- old-normal | |
1525 | (* (/ old-normal parent-normal) | |
1526 | delta-normal)) | |
1527 | ;; Don't drop below 0. | |
1528 | 0.0)))))) | |
1529 | (setq sub (window-right sub))) | |
1530 | ||
1531 | (when (numberp other-delta) | |
1532 | ;; Set the new normal size of windows from what they should have | |
1533 | ;; contributed for recovering OTHER-DELTA lines (columns). | |
1534 | (setq delta-normal (/ (float (window-total-size parent horizontal)) | |
1535 | (+ (window-total-size parent horizontal) | |
1536 | other-delta))) | |
1537 | (setq sub (window-child parent)) | |
1538 | (setq skip (eq trail 'after)) | |
1539 | (while sub | |
1540 | (cond | |
1541 | ((eq sub window) | |
1542 | (setq skip (eq trail 'before))) | |
1543 | (skip) | |
1544 | (t | |
1545 | (set-window-new-normal | |
1546 | sub (min 1.0 ; Don't get larger than 1. | |
1547 | (max (* (window-new-normal sub) delta-normal) | |
1548 | ;; Don't drop below 0. | |
1549 | 0.0))))) | |
1550 | (setq sub (window-right sub)))) | |
1551 | ||
1552 | ;; Set the new normal size of WINDOW to what is left by the sum of | |
1553 | ;; the normal sizes of its siblings. | |
1554 | (set-window-new-normal | |
1555 | window | |
1556 | (let ((sum 0)) | |
1557 | (setq sub (window-child parent)) | |
1558 | (while sub | |
1559 | (cond | |
1560 | ((eq sub window)) | |
1561 | ((not (numberp (window-new-normal sub))) | |
1562 | (setq sum (+ sum (window-normal-size sub horizontal)))) | |
1563 | (t | |
1564 | (setq sum (+ sum (window-new-normal sub))))) | |
1565 | (setq sub (window-right sub))) | |
1566 | ;; Don't get larger than 1 or smaller than 0. | |
1567 | (min 1.0 (max (- 1.0 sum) 0.0)))))) | |
1568 | ||
5386012d | 1569 | (defun window--resize-subwindows (parent delta &optional horizontal window ignore trail edge) |
9aab8e0d MR |
1570 | "Resize subwindows of window PARENT vertically by DELTA lines. |
1571 | PARENT must be a vertically combined internal window. | |
1572 | ||
1573 | Optional argument HORIZONTAL non-nil means resize subwindows of | |
1574 | PARENT horizontally by DELTA columns. In this case PARENT must | |
1575 | be a horizontally combined internal window. | |
1576 | ||
1577 | WINDOW, if specified, must denote a child window of PARENT that | |
1578 | is resized by DELTA lines. | |
1579 | ||
1580 | Optional argument IGNORE non-nil means ignore any restrictions | |
1581 | imposed by fixed size windows, `window-min-height' or | |
1582 | `window-min-width' settings. IGNORE equal `safe' means live | |
1583 | windows may get as small as `window-safe-min-height' lines and | |
1584 | `window-safe-min-width' columns. IGNORE any window means ignore | |
1585 | restrictions for that window only. | |
1586 | ||
1587 | Optional arguments TRAIL and EDGE, when non-nil, restrict the set | |
1588 | of windows that shall be resized. If TRAIL equals `before', | |
1589 | resize only windows on the left or above EDGE. If TRAIL equals | |
1590 | `after', resize only windows on the right or below EDGE. Also, | |
1591 | preferably only resize windows adjacent to EDGE. | |
1592 | ||
1593 | Return the symbol `normalized' if new normal sizes have been | |
1594 | already set by this routine." | |
1595 | (let* ((first (window-child parent)) | |
1596 | (sub first) | |
1597 | (parent-total (+ (window-total-size parent horizontal) delta)) | |
1598 | best-window best-value) | |
1599 | ||
1600 | (if (and edge (memq trail '(before after)) | |
1601 | (progn | |
1602 | (setq sub first) | |
1603 | (while (and (window-right sub) | |
1604 | (or (and (eq trail 'before) | |
5386012d | 1605 | (not (window--resize-subwindows-skip-p |
9aab8e0d MR |
1606 | (window-right sub)))) |
1607 | (and (eq trail 'after) | |
5386012d | 1608 | (window--resize-subwindows-skip-p sub)))) |
9aab8e0d MR |
1609 | (setq sub (window-right sub))) |
1610 | sub) | |
1611 | (if horizontal | |
1612 | (if (eq trail 'before) | |
1613 | (= (+ (window-left-column sub) | |
1614 | (window-total-size sub t)) | |
1615 | edge) | |
1616 | (= (window-left-column sub) edge)) | |
1617 | (if (eq trail 'before) | |
1618 | (= (+ (window-top-line sub) | |
1619 | (window-total-size sub)) | |
1620 | edge) | |
1621 | (= (window-top-line sub) edge))) | |
1622 | (window-sizable-p sub delta horizontal ignore)) | |
1623 | ;; Resize only windows adjacent to EDGE. | |
1624 | (progn | |
5386012d MR |
1625 | (window--resize-this-window |
1626 | sub delta horizontal ignore t trail edge) | |
9aab8e0d MR |
1627 | (if (and window (eq (window-parent sub) parent)) |
1628 | (progn | |
1629 | ;; Assign new normal sizes. | |
1630 | (set-window-new-normal | |
1631 | sub (/ (float (window-new-total sub)) parent-total)) | |
1632 | (set-window-new-normal | |
1633 | window (- (window-normal-size window horizontal) | |
1634 | (- (window-new-normal sub) | |
1635 | (window-normal-size sub horizontal))))) | |
5386012d MR |
1636 | (window--resize-subwindows-normal |
1637 | parent horizontal sub 0 trail delta)) | |
1638 | ;; Return 'normalized to notify `window--resize-siblings' that | |
9aab8e0d MR |
1639 | ;; normal sizes have been already set. |
1640 | 'normalized) | |
1641 | ;; Resize all windows proportionally. | |
1642 | (setq sub first) | |
1643 | (while sub | |
1644 | (cond | |
5386012d | 1645 | ((or (window--resize-subwindows-skip-p sub) |
9aab8e0d MR |
1646 | ;; Ignore windows to skip and fixed-size subwindows - in |
1647 | ;; the latter case make it a window to skip. | |
1648 | (and (not ignore) | |
1649 | (window-size-fixed-p sub horizontal) | |
1650 | (set-window-new-normal sub 'ignore)))) | |
1651 | ((< delta 0) | |
1652 | ;; When shrinking store the number of lines/cols we can get | |
1653 | ;; from this window here together with the total/normal size | |
1654 | ;; factor. | |
1655 | (set-window-new-normal | |
1656 | sub | |
1657 | (cons | |
1658 | ;; We used to call this with NODOWN t, "fixed" 2011-05-11. | |
1659 | (window-min-delta sub horizontal ignore trail t) ; t) | |
1660 | (- (/ (float (window-total-size sub horizontal)) | |
1661 | parent-total) | |
1662 | (window-normal-size sub horizontal))))) | |
1663 | ((> delta 0) | |
1664 | ;; When enlarging store the total/normal size factor only | |
1665 | (set-window-new-normal | |
1666 | sub | |
1667 | (- (/ (float (window-total-size sub horizontal)) | |
1668 | parent-total) | |
1669 | (window-normal-size sub horizontal))))) | |
1670 | ||
1671 | (setq sub (window-right sub))) | |
1672 | ||
1673 | (cond | |
1674 | ((< delta 0) | |
1675 | ;; Shrink windows by delta. | |
1676 | (setq best-window t) | |
1677 | (while (and best-window (not (zerop delta))) | |
1678 | (setq sub first) | |
1679 | (setq best-window nil) | |
1680 | (setq best-value most-negative-fixnum) | |
1681 | (while sub | |
1682 | (when (and (consp (window-new-normal sub)) | |
1683 | (not (zerop (car (window-new-normal sub)))) | |
1684 | (> (cdr (window-new-normal sub)) best-value)) | |
1685 | (setq best-window sub) | |
1686 | (setq best-value (cdr (window-new-normal sub)))) | |
1687 | ||
1688 | (setq sub (window-right sub))) | |
1689 | ||
1690 | (when best-window | |
1691 | (setq delta (1+ delta))) | |
1692 | (set-window-new-total best-window -1 t) | |
1693 | (set-window-new-normal | |
1694 | best-window | |
1695 | (if (= (car (window-new-normal best-window)) 1) | |
1696 | 'skip ; We can't shrink best-window any further. | |
1697 | (cons (1- (car (window-new-normal best-window))) | |
1698 | (- (/ (float (window-new-total best-window)) | |
1699 | parent-total) | |
1700 | (window-normal-size best-window horizontal))))))) | |
1701 | ((> delta 0) | |
1702 | ;; Enlarge windows by delta. | |
1703 | (setq best-window t) | |
1704 | (while (and best-window (not (zerop delta))) | |
1705 | (setq sub first) | |
1706 | (setq best-window nil) | |
1707 | (setq best-value most-positive-fixnum) | |
1708 | (while sub | |
1709 | (when (and (numberp (window-new-normal sub)) | |
1710 | (< (window-new-normal sub) best-value)) | |
1711 | (setq best-window sub) | |
1712 | (setq best-value (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 | (- (/ (float (window-new-total best-window)) | |
1722 | parent-total) | |
1723 | (window-normal-size best-window horizontal)))))) | |
1724 | ||
1725 | (when best-window | |
1726 | (setq sub first) | |
1727 | (while sub | |
1728 | (when (or (consp (window-new-normal sub)) | |
1729 | (numberp (window-new-normal sub))) | |
d615d6d2 | 1730 | ;; Reset new normal size fields so `window-resize-apply' |
9aab8e0d MR |
1731 | ;; won't use them to apply new sizes. |
1732 | (set-window-new-normal sub)) | |
1733 | ||
1734 | (unless (eq (window-new-normal sub) 'ignore) | |
1735 | ;; Resize this subwindow's subwindows (back-engineering | |
1736 | ;; delta from sub's old and new total sizes). | |
1737 | (let ((delta (- (window-new-total sub) | |
1738 | (window-total-size sub horizontal)))) | |
1739 | (unless (and (zerop delta) (not trail)) | |
1740 | ;; For the TRAIL non-nil case we have to resize SUB | |
1741 | ;; recursively even if it's size does not change. | |
5386012d | 1742 | (window--resize-this-window |
9aab8e0d MR |
1743 | sub delta horizontal ignore nil trail edge)))) |
1744 | (setq sub (window-right sub))))))) | |
1745 | ||
5386012d | 1746 | (defun window--resize-siblings (window delta &optional horizontal ignore trail edge) |
9aab8e0d MR |
1747 | "Resize other windows when WINDOW is resized vertically by DELTA lines. |
1748 | Optional argument HORIZONTAL non-nil means resize other windows | |
1749 | when WINDOW is resized horizontally by DELTA columns. WINDOW | |
1750 | itself is not resized by this function. | |
1751 | ||
1752 | Optional argument IGNORE non-nil means ignore any restrictions | |
1753 | imposed by fixed size windows, `window-min-height' or | |
1754 | `window-min-width' settings. IGNORE equal `safe' means live | |
1755 | windows may get as small as `window-safe-min-height' lines and | |
1756 | `window-safe-min-width' columns. IGNORE any window means ignore | |
1757 | restrictions for that window only. | |
1758 | ||
1759 | Optional arguments TRAIL and EDGE, when non-nil, refine the set | |
1760 | of windows that shall be resized. If TRAIL equals `before', | |
1761 | resize only windows on the left or above EDGE. If TRAIL equals | |
1762 | `after', resize only windows on the right or below EDGE. Also, | |
1763 | preferably only resize windows adjacent to EDGE." | |
1764 | (when (window-parent window) | |
1765 | (let* ((parent (window-parent window)) | |
1766 | (sub (window-child parent))) | |
1767 | (if (window-iso-combined-p sub horizontal) | |
1768 | ;; In an iso-combination try to extract DELTA from WINDOW's | |
1769 | ;; siblings. | |
1770 | (let ((first sub) | |
1771 | (skip (eq trail 'after)) | |
1772 | this-delta other-delta) | |
1773 | ;; Decide which windows shall be left alone. | |
1774 | (while sub | |
1775 | (cond | |
1776 | ((eq sub window) | |
1777 | ;; Make sure WINDOW is left alone when | |
1778 | ;; resizing its siblings. | |
1779 | (set-window-new-normal sub 'ignore) | |
1780 | (setq skip (eq trail 'before))) | |
1781 | (skip | |
1782 | ;; Make sure this sibling is left alone when | |
1783 | ;; resizing its siblings. | |
1784 | (set-window-new-normal sub 'ignore)) | |
1785 | ((or (window-size-ignore sub ignore) | |
1786 | (not (window-size-fixed-p sub horizontal))) | |
1787 | ;; Set this-delta to t to signal that we found a sibling | |
1788 | ;; of WINDOW whose size is not fixed. | |
1789 | (setq this-delta t))) | |
1790 | ||
1791 | (setq sub (window-right sub))) | |
1792 | ||
1793 | ;; Set this-delta to what we can get from WINDOW's siblings. | |
1794 | (if (= (- delta) (window-total-size window horizontal)) | |
1795 | ;; A deletion, presumably. We must handle this case | |
1796 | ;; specially since `window-resizable' can't be used. | |
1797 | (if this-delta | |
1798 | ;; There's at least one resizable sibling we can | |
1799 | ;; give WINDOW's size to. | |
1800 | (setq this-delta delta) | |
1801 | ;; No resizable sibling exists. | |
1802 | (setq this-delta 0)) | |
1803 | ;; Any other form of resizing. | |
1804 | (setq this-delta | |
1805 | (window-resizable window delta horizontal ignore trail t))) | |
1806 | ||
1807 | ;; Set other-delta to what we still have to get from | |
1808 | ;; ancestor windows of parent. | |
1809 | (setq other-delta (- delta this-delta)) | |
1810 | (unless (zerop other-delta) | |
1811 | ;; Unless we got everything from WINDOW's siblings, PARENT | |
1812 | ;; must be resized by other-delta lines or columns. | |
1813 | (set-window-new-total parent other-delta 'add)) | |
1814 | ||
1815 | (if (zerop this-delta) | |
1816 | ;; We haven't got anything from WINDOW's siblings but we | |
1817 | ;; must update the normal sizes to respect other-delta. | |
5386012d | 1818 | (window--resize-subwindows-normal |
9aab8e0d MR |
1819 | parent horizontal window this-delta trail other-delta) |
1820 | ;; We did get something from WINDOW's siblings which means | |
1821 | ;; we have to resize their subwindows. | |
5386012d MR |
1822 | (unless (eq (window--resize-subwindows |
1823 | parent (- this-delta) horizontal | |
1824 | window ignore trail edge) | |
1825 | ;; If `window--resize-subwindows' returns | |
1826 | ;; 'normalized, this means it has set the | |
1827 | ;; normal sizes already. | |
9aab8e0d MR |
1828 | 'normalized) |
1829 | ;; Set the normal sizes. | |
5386012d | 1830 | (window--resize-subwindows-normal |
9aab8e0d MR |
1831 | parent horizontal window this-delta trail other-delta)) |
1832 | ;; Set DELTA to what we still have to get from ancestor | |
1833 | ;; windows. | |
1834 | (setq delta other-delta))) | |
1835 | ||
1836 | ;; In an ortho-combination all siblings of WINDOW must be | |
1837 | ;; resized by DELTA. | |
1838 | (set-window-new-total parent delta 'add) | |
1839 | (while sub | |
1840 | (unless (eq sub window) | |
5386012d | 1841 | (window--resize-this-window sub delta horizontal ignore t)) |
9aab8e0d MR |
1842 | (setq sub (window-right sub)))) |
1843 | ||
1844 | (unless (zerop delta) | |
1845 | ;; "Go up." | |
5386012d MR |
1846 | (window--resize-siblings |
1847 | parent delta horizontal ignore trail edge))))) | |
9aab8e0d | 1848 | |
5386012d | 1849 | (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge) |
9aab8e0d MR |
1850 | "Resize WINDOW vertically by DELTA lines. |
1851 | Optional argument HORIZONTAL non-nil means resize WINDOW | |
1852 | horizontally by DELTA columns. | |
1853 | ||
1854 | Optional argument IGNORE non-nil means ignore any restrictions | |
1855 | imposed by fixed size windows, `window-min-height' or | |
1856 | `window-min-width' settings. IGNORE equal `safe' means live | |
1857 | windows may get as small as `window-safe-min-height' lines and | |
1858 | `window-safe-min-width' columns. IGNORE any window means ignore | |
1859 | restrictions for that window only. | |
1860 | ||
1861 | Optional argument ADD non-nil means add DELTA to the new total | |
1862 | size of WINDOW. | |
1863 | ||
1864 | Optional arguments TRAIL and EDGE, when non-nil, refine the set | |
1865 | of windows that shall be resized. If TRAIL equals `before', | |
1866 | resize only windows on the left or above EDGE. If TRAIL equals | |
1867 | `after', resize only windows on the right or below EDGE. Also, | |
1868 | preferably only resize windows adjacent to EDGE. | |
1869 | ||
1870 | This function recursively resizes WINDOW's subwindows to fit the | |
1871 | new size. Make sure that WINDOW is `window-resizable' before | |
1872 | calling this function. Note that this function does not resize | |
1873 | siblings of WINDOW or WINDOW's parent window. You have to | |
d615d6d2 | 1874 | eventually call `window-resize-apply' in order to make resizing |
9aab8e0d MR |
1875 | actually take effect." |
1876 | (when add | |
1877 | ;; Add DELTA to the new total size of WINDOW. | |
1878 | (set-window-new-total window delta t)) | |
387522b2 | 1879 | |
9aab8e0d MR |
1880 | (let ((sub (window-child window))) |
1881 | (cond | |
1882 | ((not sub)) | |
1883 | ((window-iso-combined-p sub horizontal) | |
1884 | ;; In an iso-combination resize subwindows according to their | |
1885 | ;; normal sizes. | |
5386012d MR |
1886 | (window--resize-subwindows |
1887 | window delta horizontal nil ignore trail edge)) | |
9aab8e0d MR |
1888 | ;; In an ortho-combination resize each subwindow by DELTA. |
1889 | (t | |
1890 | (while sub | |
5386012d MR |
1891 | (window--resize-this-window |
1892 | sub delta horizontal ignore t trail edge) | |
9aab8e0d MR |
1893 | (setq sub (window-right sub))))))) |
1894 | ||
5386012d | 1895 | (defun window--resize-root-window (window delta horizontal ignore) |
9aab8e0d MR |
1896 | "Resize root window WINDOW vertically by DELTA lines. |
1897 | HORIZONTAL non-nil means resize root window WINDOW horizontally | |
1898 | by DELTA columns. | |
1899 | ||
1900 | IGNORE non-nil means ignore any restrictions imposed by fixed | |
1901 | size windows, `window-min-height' or `window-min-width' settings. | |
1902 | ||
1903 | This function is only called by the frame resizing routines. It | |
1904 | resizes windows proportionally and never deletes any windows." | |
1905 | (when (and (windowp window) (numberp delta) | |
1906 | (window-sizable-p window delta horizontal ignore)) | |
5386012d MR |
1907 | (window--resize-reset (window-frame window) horizontal) |
1908 | (window--resize-this-window window delta horizontal ignore t))) | |
9aab8e0d | 1909 | |
5386012d | 1910 | (defun window--resize-root-window-vertically (window delta) |
9aab8e0d MR |
1911 | "Resize root window WINDOW vertically by DELTA lines. |
1912 | If DELTA is less than zero and we can't shrink WINDOW by DELTA | |
1913 | lines, shrink it as much as possible. If DELTA is greater than | |
1914 | zero, this function can resize fixed-size subwindows in order to | |
1915 | recover the necessary lines. | |
1916 | ||
1917 | Return the number of lines that were recovered. | |
1918 | ||
1919 | This function is only called by the minibuffer window resizing | |
1920 | routines. It resizes windows proportionally and never deletes | |
1921 | any windows." | |
1922 | (when (numberp delta) | |
1923 | (let (ignore) | |
1924 | (cond | |
1925 | ((< delta 0) | |
1926 | (setq delta (window-sizable window delta))) | |
1927 | ((> delta 0) | |
1928 | (unless (window-sizable window delta) | |
1929 | (setq ignore t)))) | |
1930 | ||
5386012d | 1931 | (window--resize-reset (window-frame window)) |
9aab8e0d MR |
1932 | ;; Ideally, we would resize just the last window in a combination |
1933 | ;; but that's not feasible for the following reason: If we grow | |
1934 | ;; the minibuffer window and the last window cannot be shrunk any | |
1935 | ;; more, we shrink another window instead. But if we then shrink | |
1936 | ;; the minibuffer window again, the last window might get enlarged | |
1937 | ;; and the state after shrinking is not the state before growing. | |
1938 | ;; So, in practice, we'd need a history variable to record how to | |
1939 | ;; proceed. But I'm not sure how such a variable could work with | |
1940 | ;; repeated minibuffer window growing steps. | |
5386012d | 1941 | (window--resize-this-window window delta nil ignore t) |
9aab8e0d MR |
1942 | delta))) |
1943 | ||
562dd5e9 MR |
1944 | (defun adjust-window-trailing-edge (window delta &optional horizontal) |
1945 | "Move WINDOW's bottom edge by DELTA lines. | |
1946 | Optional argument HORIZONTAL non-nil means move WINDOW's right | |
1947 | edge by DELTA columns. WINDOW defaults to the selected window. | |
1948 | ||
1949 | If DELTA is greater zero, then move the edge downwards or to the | |
1950 | right. If DELTA is less than zero, move the edge upwards or to | |
1951 | the left. If the edge can't be moved by DELTA lines or columns, | |
1952 | move it as far as possible in the desired direction." | |
5386012d | 1953 | (setq window (window-normalize-any-window window)) |
562dd5e9 MR |
1954 | (let ((frame (window-frame window)) |
1955 | (right window) | |
1956 | left this-delta min-delta max-delta failed) | |
1957 | ;; Find the edge we want to move. | |
1958 | (while (and (or (not (window-iso-combined-p right horizontal)) | |
1959 | (not (window-right right))) | |
1960 | (setq right (window-parent right)))) | |
1961 | (cond | |
1962 | ((and (not right) (not horizontal) (not resize-mini-windows) | |
1963 | (eq (window-frame (minibuffer-window frame)) frame)) | |
5386012d | 1964 | (window--resize-mini-window (minibuffer-window frame) (- delta))) |
562dd5e9 MR |
1965 | ((or (not (setq left right)) (not (setq right (window-right right)))) |
1966 | (if horizontal | |
1967 | (error "No window on the right of this one") | |
1968 | (error "No window below this one"))) | |
1969 | (t | |
1970 | ;; Set LEFT to the first resizable window on the left. This step is | |
1971 | ;; needed to handle fixed-size windows. | |
1972 | (while (and left (window-size-fixed-p left horizontal)) | |
1973 | (setq left | |
1974 | (or (window-left left) | |
1975 | (progn | |
1976 | (while (and (setq left (window-parent left)) | |
1977 | (not (window-iso-combined-p left horizontal)))) | |
1978 | (window-left left))))) | |
1979 | (unless left | |
1980 | (if horizontal | |
1981 | (error "No resizable window on the left of this one") | |
1982 | (error "No resizable window above this one"))) | |
1983 | ||
1984 | ;; Set RIGHT to the first resizable window on the right. This step | |
1985 | ;; is needed to handle fixed-size windows. | |
1986 | (while (and right (window-size-fixed-p right horizontal)) | |
1987 | (setq right | |
1988 | (or (window-right right) | |
1989 | (progn | |
1990 | (while (and (setq right (window-parent right)) | |
1991 | (not (window-iso-combined-p right horizontal)))) | |
1992 | (window-right right))))) | |
1993 | (unless right | |
1994 | (if horizontal | |
1995 | (error "No resizable window on the right of this one") | |
1996 | (error "No resizable window below this one"))) | |
1997 | ||
1998 | ;; LEFT and RIGHT (which might be both internal windows) are now the | |
1999 | ;; two windows we want to resize. | |
2000 | (cond | |
2001 | ((> delta 0) | |
2002 | (setq max-delta (window-max-delta-1 left 0 horizontal nil 'after)) | |
2003 | (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'before)) | |
2004 | (when (or (< max-delta delta) (> min-delta (- delta))) | |
2005 | ;; We can't get the whole DELTA - move as far as possible. | |
2006 | (setq delta (min max-delta (- min-delta)))) | |
2007 | (unless (zerop delta) | |
2008 | ;; Start resizing. | |
5386012d | 2009 | (window--resize-reset frame horizontal) |
562dd5e9 MR |
2010 | ;; Try to enlarge LEFT first. |
2011 | (setq this-delta (window-resizable left delta horizontal)) | |
2012 | (unless (zerop this-delta) | |
5386012d | 2013 | (window--resize-this-window |
562dd5e9 MR |
2014 | left this-delta horizontal nil t 'before |
2015 | (if horizontal | |
2016 | (+ (window-left-column left) (window-total-size left t)) | |
2017 | (+ (window-top-line left) (window-total-size left))))) | |
2018 | ;; Shrink windows on right of LEFT. | |
5386012d | 2019 | (window--resize-siblings |
562dd5e9 MR |
2020 | left delta horizontal nil 'after |
2021 | (if horizontal | |
2022 | (window-left-column right) | |
2023 | (window-top-line right))))) | |
2024 | ((< delta 0) | |
2025 | (setq max-delta (window-max-delta-1 right 0 horizontal nil 'before)) | |
2026 | (setq min-delta (window-min-delta-1 left delta horizontal nil 'after)) | |
2027 | (when (or (< max-delta (- delta)) (> min-delta delta)) | |
2028 | ;; We can't get the whole DELTA - move as far as possible. | |
2029 | (setq delta (max (- max-delta) min-delta))) | |
2030 | (unless (zerop delta) | |
2031 | ;; Start resizing. | |
5386012d | 2032 | (window--resize-reset frame horizontal) |
562dd5e9 MR |
2033 | ;; Try to enlarge RIGHT. |
2034 | (setq this-delta (window-resizable right (- delta) horizontal)) | |
2035 | (unless (zerop this-delta) | |
5386012d | 2036 | (window--resize-this-window |
562dd5e9 MR |
2037 | right this-delta horizontal nil t 'after |
2038 | (if horizontal | |
2039 | (window-left-column right) | |
2040 | (window-top-line right)))) | |
2041 | ;; Shrink windows on left of RIGHT. | |
5386012d | 2042 | (window--resize-siblings |
562dd5e9 MR |
2043 | right (- delta) horizontal nil 'before |
2044 | (if horizontal | |
2045 | (+ (window-left-column left) (window-total-size left t)) | |
2046 | (+ (window-top-line left) (window-total-size left))))))) | |
2047 | (unless (zerop delta) | |
2048 | ;; Don't report an error in the standard case. | |
d615d6d2 | 2049 | (unless (window-resize-apply frame horizontal) |
562dd5e9 MR |
2050 | ;; But do report an error if applying the changes fails. |
2051 | (error "Failed adjusting window %s" window))))))) | |
2052 | ||
2053 | (defun enlarge-window (delta &optional horizontal) | |
2054 | "Make selected window DELTA lines taller. | |
2055 | Interactively, if no argument is given, make the selected window | |
2056 | one line taller. If optional argument HORIZONTAL is non-nil, | |
2057 | make selected window wider by DELTA columns. If DELTA is | |
2058 | negative, shrink selected window by -DELTA lines or columns. | |
2059 | Return nil." | |
2060 | (interactive "p") | |
67222e1d MR |
2061 | (cond |
2062 | ((zerop delta)) | |
2063 | ((window-size-fixed-p nil horizontal) | |
2064 | (error "Selected window has fixed size")) | |
2065 | ((window-resizable-p nil delta horizontal) | |
d615d6d2 | 2066 | (window-resize nil delta horizontal)) |
67222e1d | 2067 | (t |
d615d6d2 | 2068 | (window-resize |
67222e1d MR |
2069 | nil (if (> delta 0) |
2070 | (window-max-delta nil horizontal) | |
2071 | (- (window-min-delta nil horizontal))) | |
2072 | horizontal)))) | |
562dd5e9 MR |
2073 | |
2074 | (defun shrink-window (delta &optional horizontal) | |
2075 | "Make selected window DELTA lines smaller. | |
2076 | Interactively, if no argument is given, make the selected window | |
2077 | one line smaller. If optional argument HORIZONTAL is non-nil, | |
2078 | make selected window narrower by DELTA columns. If DELTA is | |
2079 | negative, enlarge selected window by -DELTA lines or columns. | |
2080 | Return nil." | |
2081 | (interactive "p") | |
67222e1d MR |
2082 | (cond |
2083 | ((zerop delta)) | |
2084 | ((window-size-fixed-p nil horizontal) | |
2085 | (error "Selected window has fixed size")) | |
2086 | ((window-resizable-p nil (- delta) horizontal) | |
d615d6d2 | 2087 | (window-resize nil (- delta) horizontal)) |
67222e1d | 2088 | (t |
d615d6d2 | 2089 | (window-resize |
67222e1d MR |
2090 | nil (if (> delta 0) |
2091 | (- (window-min-delta nil horizontal)) | |
2092 | (window-max-delta nil horizontal)) | |
2093 | horizontal)))) | |
562dd5e9 MR |
2094 | |
2095 | (defun maximize-window (&optional window) | |
2096 | "Maximize WINDOW. | |
2097 | Make WINDOW as large as possible without deleting any windows. | |
2098 | WINDOW can be any window and defaults to the selected window." | |
2099 | (interactive) | |
5386012d | 2100 | (setq window (window-normalize-any-window window)) |
d615d6d2 MR |
2101 | (window-resize window (window-max-delta window)) |
2102 | (window-resize window (window-max-delta window t) t)) | |
562dd5e9 MR |
2103 | |
2104 | (defun minimize-window (&optional window) | |
2105 | "Minimize WINDOW. | |
2106 | Make WINDOW as small as possible without deleting any windows. | |
2107 | WINDOW can be any window and defaults to the selected window." | |
2108 | (interactive) | |
5386012d | 2109 | (setq window (window-normalize-any-window window)) |
d615d6d2 MR |
2110 | (window-resize window (- (window-min-delta window))) |
2111 | (window-resize window (- (window-min-delta window t)) t)) | |
562dd5e9 | 2112 | \f |
9aab8e0d MR |
2113 | (defsubst frame-root-window-p (window) |
2114 | "Return non-nil if WINDOW is the root window of its frame." | |
2115 | (eq window (frame-root-window window))) | |
6198ccd0 MR |
2116 | |
2117 | (defun window-tree-1 (window &optional next) | |
2118 | "Return window tree rooted at WINDOW. | |
2119 | Optional argument NEXT non-nil means include windows right | |
2120 | siblings in the return value. | |
2121 | ||
2122 | See the documentation of `window-tree' for a description of the | |
2123 | return value." | |
2124 | (let (list) | |
2125 | (while window | |
2126 | (setq list | |
2127 | (cons | |
2128 | (cond | |
d68443dc | 2129 | ((window-top-child window) |
6198ccd0 | 2130 | (cons t (cons (window-edges window) |
d68443dc MR |
2131 | (window-tree-1 (window-top-child window) t)))) |
2132 | ((window-left-child window) | |
6198ccd0 | 2133 | (cons nil (cons (window-edges window) |
d68443dc | 2134 | (window-tree-1 (window-left-child window) t)))) |
6198ccd0 MR |
2135 | (t window)) |
2136 | list)) | |
d68443dc | 2137 | (setq window (when next (window-next-sibling window)))) |
6198ccd0 MR |
2138 | (nreverse list))) |
2139 | ||
2140 | (defun window-tree (&optional frame) | |
2141 | "Return the window tree of frame FRAME. | |
2142 | FRAME must be a live frame and defaults to the selected frame. | |
2143 | The return value is a list of the form (ROOT MINI), where ROOT | |
2144 | represents the window tree of the frame's root window, and MINI | |
2145 | is the frame's minibuffer window. | |
2146 | ||
2147 | If the root window is not split, ROOT is the root window itself. | |
2148 | Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil | |
2149 | for a horizontal split, and t for a vertical split. EDGES gives | |
2150 | the combined size and position of the subwindows in the split, | |
2151 | and the rest of the elements are the subwindows in the split. | |
2152 | Each of the subwindows may again be a window or a list | |
2153 | representing a window split, and so on. EDGES is a list \(LEFT | |
2154 | TOP RIGHT BOTTOM) as returned by `window-edges'." | |
5386012d | 2155 | (setq frame (window-normalize-frame frame)) |
6198ccd0 | 2156 | (window-tree-1 (frame-root-window frame) t)) |
9aab8e0d | 2157 | \f |
9397e56f MR |
2158 | (defun other-window (count &optional all-frames) |
2159 | "Select another window in cyclic ordering of windows. | |
2160 | COUNT specifies the number of windows to skip, starting with the | |
2161 | selected window, before making the selection. If COUNT is | |
2162 | positive, skip COUNT windows forwards. If COUNT is negative, | |
2163 | skip -COUNT windows backwards. COUNT zero means do not skip any | |
2164 | window, so select the selected window. In an interactive call, | |
2165 | COUNT is the numeric prefix argument. Return nil. | |
2166 | ||
2167 | If the `other-window' parameter of WINDOW is a function and | |
2168 | `ignore-window-parameters' is nil, call that function with the | |
2169 | arguments COUNT and ALL-FRAMES. | |
2170 | ||
2171 | This function does not select a window whose `no-other-window' | |
2172 | window parameter is non-nil. | |
2173 | ||
2174 | This function uses `next-window' for finding the window to | |
2175 | select. The argument ALL-FRAMES has the same meaning as in | |
2176 | `next-window', but the MINIBUF argument of `next-window' is | |
2177 | always effectively nil." | |
2178 | (interactive "p") | |
2179 | (let* ((window (selected-window)) | |
2180 | (function (and (not ignore-window-parameters) | |
2181 | (window-parameter window 'other-window))) | |
2182 | old-window old-count) | |
2183 | (if (functionp function) | |
2184 | (funcall function count all-frames) | |
2185 | ;; `next-window' and `previous-window' may return a window we are | |
2186 | ;; not allowed to select. Hence we need an exit strategy in case | |
2187 | ;; all windows are non-selectable. | |
2188 | (catch 'exit | |
2189 | (while (> count 0) | |
2190 | (setq window (next-window window nil all-frames)) | |
2191 | (cond | |
2192 | ((eq window old-window) | |
2193 | (when (= count old-count) | |
2194 | ;; Keep out of infinite loops. When COUNT has not changed | |
2195 | ;; since we last looked at `window' we're probably in one. | |
2196 | (throw 'exit nil))) | |
2197 | ((window-parameter window 'no-other-window) | |
2198 | (unless old-window | |
2199 | ;; The first non-selectable window `next-window' got us: | |
2200 | ;; Remember it and the current value of COUNT. | |
2201 | (setq old-window window) | |
2202 | (setq old-count count))) | |
2203 | (t | |
2204 | (setq count (1- count))))) | |
2205 | (while (< count 0) | |
2206 | (setq window (previous-window window nil all-frames)) | |
2207 | (cond | |
2208 | ((eq window old-window) | |
2209 | (when (= count old-count) | |
2210 | ;; Keep out of infinite loops. When COUNT has not changed | |
2211 | ;; since we last looked at `window' we're probably in one. | |
2212 | (throw 'exit nil))) | |
2213 | ((window-parameter window 'no-other-window) | |
2214 | (unless old-window | |
2215 | ;; The first non-selectable window `previous-window' got | |
2216 | ;; us: Remember it and the current value of COUNT. | |
2217 | (setq old-window window) | |
2218 | (setq old-count count))) | |
2219 | (t | |
2220 | (setq count (1+ count))))) | |
2221 | ||
2222 | (select-window window) | |
2223 | ;; Always return nil. | |
2224 | nil)))) | |
2225 | ||
387522b2 MR |
2226 | ;; This should probably return non-nil when the selected window is part |
2227 | ;; of an atomic window whose root is the frame's root window. | |
2228 | (defun one-window-p (&optional nomini all-frames) | |
2229 | "Return non-nil if the selected window is the only window. | |
2230 | Optional arg NOMINI non-nil means don't count the minibuffer | |
2231 | even if it is active. Otherwise, the minibuffer is counted | |
2232 | when it is active. | |
2233 | ||
2234 | Optional argument ALL-FRAMES specifies the set of frames to | |
2235 | consider, see also `next-window'. ALL-FRAMES nil or omitted | |
2236 | means consider windows on the selected frame only, plus the | |
2237 | minibuffer window if specified by the NOMINI argument. If the | |
2238 | minibuffer counts, consider all windows on all frames that share | |
2239 | that minibuffer too. The remaining non-nil values of ALL-FRAMES | |
2240 | with a special meaning are: | |
2241 | ||
2242 | - t means consider all windows on all existing frames. | |
2243 | ||
2244 | - `visible' means consider all windows on all visible frames on | |
2245 | the current terminal. | |
2246 | ||
2247 | - 0 (the number zero) means consider all windows on all visible | |
2248 | and iconified frames on the current terminal. | |
2249 | ||
2250 | - A frame means consider all windows on that frame only. | |
2251 | ||
2252 | Anything else means consider all windows on the selected frame | |
2253 | and no others." | |
2254 | (let ((base-window (selected-window))) | |
2255 | (if (and nomini (eq base-window (minibuffer-window))) | |
2256 | (setq base-window (next-window base-window))) | |
2257 | (eq base-window | |
2258 | (next-window base-window (if nomini 'arg) all-frames)))) | |
3c448ab6 | 2259 | \f |
9aab8e0d | 2260 | ;;; Deleting windows. |
fbf5b3ce MR |
2261 | (defcustom frame-auto-delete 'automatic |
2262 | "If non-nil, quitting a window can delete it's frame. | |
2263 | If this variable is nil, functions that quit a window never | |
2264 | delete the associated frame. If this variable equals the symbol | |
2265 | `automatic', a frame is deleted only if it the window is | |
2266 | dedicated or was created by `display-buffer'. If this variable | |
2267 | is t, a frame can be always deleted, even if it was created by | |
2268 | `make-frame-command'. Other values should not be used. | |
2269 | ||
2270 | Note that a frame will be effectively deleted if and only if | |
2271 | another frame still exists. | |
2272 | ||
2273 | Functions quitting a window and consequently affected by this | |
2274 | variable are `switch-to-prev-buffer', `delete-windows-on', | |
2275 | `replace-buffer-in-windows' and `quit-restore-window'." | |
2276 | :type '(choice | |
2277 | (const :tag "Never" nil) | |
2278 | (const :tag "Automatic" automatic) | |
2279 | (const :tag "Always" t)) | |
2280 | :group 'windows | |
2281 | :group 'frames) | |
2282 | ||
9aab8e0d MR |
2283 | (defun window-deletable-p (&optional window) |
2284 | "Return t if WINDOW can be safely deleted from its frame. | |
2285 | Return `frame' if deleting WINDOW should delete its frame | |
2286 | instead." | |
5386012d | 2287 | (setq window (window-normalize-any-window window)) |
9aab8e0d MR |
2288 | (unless ignore-window-parameters |
2289 | ;; Handle atomicity. | |
2290 | (when (window-parameter window 'window-atom) | |
2291 | (setq window (window-atom-root window)))) | |
2292 | (let ((parent (window-parent window)) | |
2293 | (frame (window-frame window)) | |
2294 | (dedicated (and (window-buffer window) (window-dedicated-p window))) | |
2295 | (quit-restore (window-parameter window 'quit-restore))) | |
2296 | (cond | |
2297 | ((frame-root-window-p window) | |
fbf5b3ce MR |
2298 | (when (and (or (eq frame-auto-delete t) |
2299 | (and (eq frame-auto-delete 'automatic) | |
2300 | (or dedicated | |
2301 | (and (eq (car-safe quit-restore) 'new-frame) | |
2302 | (eq (nth 1 quit-restore) | |
2303 | (window-buffer window)))))) | |
9aab8e0d MR |
2304 | (other-visible-frames-p frame)) |
2305 | ;; WINDOW is the root window of its frame. Return `frame' but | |
2306 | ;; only if WINDOW is (1) either dedicated or quit-restore's car | |
2307 | ;; is new-frame and the window still displays the same buffer | |
2308 | ;; and (2) there are other frames left. | |
2309 | 'frame)) | |
2310 | ((and (not ignore-window-parameters) | |
2311 | (eq (window-parameter window 'window-side) 'none) | |
2312 | (or (not parent) | |
2313 | (not (eq (window-parameter parent 'window-side) 'none)))) | |
2314 | ;; Can't delete last main window. | |
2315 | nil) | |
2316 | (t)))) | |
2317 | ||
2318 | (defun window-or-subwindow-p (subwindow window) | |
2319 | "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW." | |
2320 | (or (eq subwindow window) | |
2321 | (let ((parent (window-parent subwindow))) | |
2322 | (catch 'done | |
2323 | (while parent | |
2324 | (if (eq parent window) | |
2325 | (throw 'done t) | |
2326 | (setq parent (window-parent parent)))))))) | |
2327 | ||
562dd5e9 MR |
2328 | (defun delete-window (&optional window) |
2329 | "Delete WINDOW. | |
2330 | WINDOW can be an arbitrary window and defaults to the selected | |
2331 | one. Return nil. | |
2332 | ||
2333 | If the variable `ignore-window-parameters' is non-nil or the | |
2334 | `delete-window' parameter of WINDOW equals t, do not process any | |
2335 | parameters of WINDOW. Otherwise, if the `delete-window' | |
2336 | parameter of WINDOW specifies a function, call that function with | |
2337 | WINDOW as its sole argument and return the value returned by that | |
2338 | function. | |
2339 | ||
2340 | Otherwise, if WINDOW is part of an atomic window, call | |
2341 | `delete-window' with the root of the atomic window as its | |
2342 | argument. If WINDOW is the only window on its frame or the last | |
2343 | non-side window, signal an error." | |
2344 | (interactive) | |
5386012d | 2345 | (setq window (window-normalize-any-window window)) |
562dd5e9 MR |
2346 | (let* ((frame (window-frame window)) |
2347 | (function (window-parameter window 'delete-window)) | |
2348 | (parent (window-parent window)) | |
2349 | atom-root) | |
2350 | (window-check frame) | |
2351 | (catch 'done | |
2352 | ;; Handle window parameters. | |
2353 | (cond | |
2354 | ;; Ignore window parameters if `ignore-window-parameters' tells | |
2355 | ;; us so or `delete-window' equals t. | |
2356 | ((or ignore-window-parameters (eq function t))) | |
2357 | ((functionp function) | |
2358 | ;; The `delete-window' parameter specifies the function to call. | |
2359 | ;; If that function is `ignore' nothing is done. It's up to the | |
2360 | ;; function called here to avoid infinite recursion. | |
2361 | (throw 'done (funcall function window))) | |
2362 | ((and (window-parameter window 'window-atom) | |
2363 | (setq atom-root (window-atom-root window)) | |
2364 | (not (eq atom-root window))) | |
2365 | (throw 'done (delete-window atom-root))) | |
2366 | ((and (eq (window-parameter window 'window-side) 'none) | |
2367 | (or (not parent) | |
2368 | (not (eq (window-parameter parent 'window-side) 'none)))) | |
2369 | (error "Attempt to delete last non-side window")) | |
2370 | ((not parent) | |
2371 | (error "Attempt to delete minibuffer or sole ordinary window"))) | |
2372 | ||
d68443dc | 2373 | (let* ((horizontal (window-left-child parent)) |
562dd5e9 MR |
2374 | (size (window-total-size window horizontal)) |
2375 | (frame-selected | |
2376 | (window-or-subwindow-p (frame-selected-window frame) window)) | |
2377 | ;; Emacs 23 preferably gives WINDOW's space to its left | |
2378 | ;; sibling. | |
2379 | (sibling (or (window-left window) (window-right window)))) | |
5386012d | 2380 | (window--resize-reset frame horizontal) |
562dd5e9 MR |
2381 | (cond |
2382 | ((and (not (window-splits window)) | |
2383 | sibling (window-sizable-p sibling size)) | |
2384 | ;; Resize WINDOW's sibling. | |
5386012d | 2385 | (window--resize-this-window sibling size horizontal nil t) |
562dd5e9 MR |
2386 | (set-window-new-normal |
2387 | sibling (+ (window-normal-size sibling horizontal) | |
2388 | (window-normal-size window horizontal)))) | |
2389 | ((window-resizable-p window (- size) horizontal nil nil nil t) | |
2390 | ;; Can do without resizing fixed-size windows. | |
5386012d | 2391 | (window--resize-siblings window (- size) horizontal)) |
562dd5e9 MR |
2392 | (t |
2393 | ;; Can't do without resizing fixed-size windows. | |
5386012d | 2394 | (window--resize-siblings window (- size) horizontal t))) |
562dd5e9 MR |
2395 | ;; Actually delete WINDOW. |
2396 | (delete-window-internal window) | |
2397 | (when (and frame-selected | |
2398 | (window-parameter | |
2399 | (frame-selected-window frame) 'no-other-window)) | |
2400 | ;; `delete-window-internal' has selected a window that should | |
2401 | ;; not be selected, fix this here. | |
2402 | (other-window -1 frame)) | |
2403 | (run-window-configuration-change-hook frame) | |
2404 | (window-check frame) | |
2405 | ;; Always return nil. | |
2406 | nil)))) | |
2407 | ||
2408 | (defun delete-other-windows (&optional window) | |
2409 | "Make WINDOW fill its frame. | |
2410 | WINDOW may be any window and defaults to the selected one. | |
2411 | Return nil. | |
2412 | ||
2413 | If the variable `ignore-window-parameters' is non-nil or the | |
2414 | `delete-other-windows' parameter of WINDOW equals t, do not | |
2415 | process any parameters of WINDOW. Otherwise, if the | |
2416 | `delete-other-windows' parameter of WINDOW specifies a function, | |
2417 | call that function with WINDOW as its sole argument and return | |
2418 | the value returned by that function. | |
2419 | ||
2420 | Otherwise, if WINDOW is part of an atomic window, call this | |
2421 | function with the root of the atomic window as its argument. If | |
2422 | WINDOW is a non-side window, make WINDOW the only non-side window | |
2423 | on the frame. Side windows are not deleted. If WINDOW is a side | |
2424 | window signal an error." | |
2425 | (interactive) | |
5386012d | 2426 | (setq window (window-normalize-any-window window)) |
562dd5e9 MR |
2427 | (let* ((frame (window-frame window)) |
2428 | (function (window-parameter window 'delete-other-windows)) | |
2429 | (window-side (window-parameter window 'window-side)) | |
2430 | atom-root side-main) | |
2431 | (window-check frame) | |
2432 | (catch 'done | |
2433 | (cond | |
2434 | ;; Ignore window parameters if `ignore-window-parameters' is t or | |
2435 | ;; `delete-other-windows' is t. | |
2436 | ((or ignore-window-parameters (eq function t))) | |
2437 | ((functionp function) | |
2438 | ;; The `delete-other-windows' parameter specifies the function | |
2439 | ;; to call. If the function is `ignore' no windows are deleted. | |
2440 | ;; It's up to the function called to avoid infinite recursion. | |
2441 | (throw 'done (funcall function window))) | |
2442 | ((and (window-parameter window 'window-atom) | |
2443 | (setq atom-root (window-atom-root window)) | |
2444 | (not (eq atom-root window))) | |
2445 | (throw 'done (delete-other-windows atom-root))) | |
2446 | ((eq window-side 'none) | |
2447 | ;; Set side-main to the major non-side window. | |
2448 | (setq side-main (window-with-parameter 'window-side 'none nil t))) | |
2449 | ((memq window-side window-sides) | |
2450 | (error "Cannot make side window the only window"))) | |
2451 | ;; If WINDOW is the main non-side window, do nothing. | |
2452 | (unless (eq window side-main) | |
2453 | (delete-other-windows-internal window side-main) | |
2454 | (run-window-configuration-change-hook frame) | |
2455 | (window-check frame)) | |
2456 | ;; Always return nil. | |
2457 | nil))) | |
9397e56f MR |
2458 | |
2459 | (defun delete-other-windows-vertically (&optional window) | |
2460 | "Delete the windows in the same column with WINDOW, but not WINDOW itself. | |
2461 | This may be a useful alternative binding for \\[delete-other-windows] | |
2462 | if you often split windows horizontally." | |
2463 | (interactive) | |
2464 | (let* ((window (or window (selected-window))) | |
2465 | (edges (window-edges window)) | |
2466 | (w window) delenda) | |
2467 | (while (not (eq (setq w (next-window w 1)) window)) | |
2468 | (let ((e (window-edges w))) | |
2469 | (when (and (= (car e) (car edges)) | |
2470 | (= (caddr e) (caddr edges))) | |
2471 | (push w delenda)))) | |
2472 | (mapc 'delete-window delenda))) | |
2473 | ||
2474 | ;;; Windows and buffers. | |
2475 | ||
2476 | ;; `prev-buffers' and `next-buffers' are two reserved window slots used | |
2477 | ;; for (1) determining which buffer to show in the window when its | |
2478 | ;; buffer shall be buried or killed and (2) which buffer to show for | |
2479 | ;; `switch-to-prev-buffer' and `switch-to-next-buffer'. | |
2480 | ||
2481 | ;; `prev-buffers' consists of <buffer, window-start, window-point> | |
2482 | ;; triples. The entries on this list are ordered by the time their | |
2483 | ;; buffer has been removed from the window, the most recently removed | |
2484 | ;; buffer's entry being first. The window-start and window-point | |
2485 | ;; components are `window-start' and `window-point' at the time the | |
2486 | ;; buffer was removed from the window which implies that the entry must | |
2487 | ;; be added when `set-window-buffer' removes the buffer from the window. | |
2488 | ||
2489 | ;; `next-buffers' is the list of buffers that have been replaced | |
2490 | ;; recently by `switch-to-prev-buffer'. These buffers are the least | |
2491 | ;; preferred candidates of `switch-to-prev-buffer' and the preferred | |
2492 | ;; candidates of `switch-to-next-buffer' to switch to. This list is | |
2493 | ;; reset to nil by any action changing the window's buffer with the | |
2494 | ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'. | |
2495 | ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it, | |
2496 | ;; `switch-to-next-buffer' pops the last pushed buffer from it. | |
2497 | ||
2498 | ;; Both `prev-buffers' and `next-buffers' may reference killed buffers | |
2499 | ;; if such a buffer was killed while the window was hidden within a | |
2500 | ;; window configuration. Such killed buffers get removed whenever | |
2501 | ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them. | |
2502 | ||
2503 | ;; The following function is called by `set-window-buffer' _before_ it | |
2504 | ;; replaces the buffer of the argument window with the new buffer. | |
2505 | (defun record-window-buffer (&optional window) | |
2506 | "Record WINDOW's buffer. | |
2507 | WINDOW must be a live window and defaults to the selected one." | |
5386012d | 2508 | (let* ((window (window-normalize-live-window window)) |
9397e56f MR |
2509 | (buffer (window-buffer window)) |
2510 | (entry (assq buffer (window-prev-buffers window)))) | |
2511 | ;; Reset WINDOW's next buffers. If needed, they are resurrected by | |
2512 | ;; `switch-to-prev-buffer' and `switch-to-next-buffer'. | |
2513 | (set-window-next-buffers window nil) | |
2514 | ||
2515 | (when entry | |
2516 | ;; Remove all entries for BUFFER from WINDOW's previous buffers. | |
2517 | (set-window-prev-buffers | |
2518 | window (assq-delete-all buffer (window-prev-buffers window)))) | |
2519 | ||
2520 | ;; Don't record insignificant buffers. | |
2521 | (unless (eq (aref (buffer-name buffer) 0) ?\s) | |
2522 | ;; Add an entry for buffer to WINDOW's previous buffers. | |
2523 | (with-current-buffer buffer | |
2524 | (let ((start (window-start window)) | |
2525 | (point (window-point window))) | |
2526 | (setq entry | |
2527 | (cons buffer | |
2528 | (if entry | |
2529 | ;; We have an entry, update marker positions. | |
2530 | (list (set-marker (nth 1 entry) start) | |
2531 | (set-marker (nth 2 entry) point)) | |
2532 | ;; Make new markers. | |
2533 | (list (copy-marker start) | |
2534 | (copy-marker point))))) | |
2535 | ||
2536 | (set-window-prev-buffers | |
2537 | window (cons entry (window-prev-buffers window)))))))) | |
2538 | ||
2539 | (defun unrecord-window-buffer (&optional window buffer) | |
2540 | "Unrecord BUFFER in WINDOW. | |
2541 | WINDOW must be a live window and defaults to the selected one. | |
2542 | BUFFER must be a live buffer and defaults to the buffer of | |
2543 | WINDOW." | |
5386012d | 2544 | (let* ((window (window-normalize-live-window window)) |
9397e56f MR |
2545 | (buffer (or buffer (window-buffer window)))) |
2546 | (set-window-prev-buffers | |
2547 | window (assq-delete-all buffer (window-prev-buffers window))) | |
2548 | (set-window-next-buffers | |
2549 | window (delq buffer (window-next-buffers window))))) | |
2550 | ||
2551 | (defun set-window-buffer-start-and-point (window buffer &optional start point) | |
2552 | "Set WINDOW's buffer to BUFFER. | |
2553 | Optional argument START non-nil means set WINDOW's start position | |
2554 | to START. Optional argument POINT non-nil means set WINDOW's | |
2555 | point to POINT. If WINDOW is selected this also sets BUFFER's | |
2556 | `point' to POINT. If WINDOW is selected and the buffer it showed | |
2557 | before was current this also makes BUFFER the current buffer." | |
2558 | (let ((selected (eq window (selected-window))) | |
2559 | (current (eq (window-buffer window) (current-buffer)))) | |
2560 | (set-window-buffer window buffer) | |
2561 | (when (and selected current) | |
2562 | (set-buffer buffer)) | |
2563 | (when start | |
2564 | (set-window-start window start)) | |
2565 | (when point | |
2566 | (if selected | |
2567 | (with-current-buffer buffer | |
2568 | (goto-char point)) | |
2569 | (set-window-point window point))))) | |
2570 | ||
2571 | (defun switch-to-prev-buffer (&optional window bury-or-kill) | |
2572 | "In WINDOW switch to previous buffer. | |
2573 | WINDOW must be a live window and defaults to the selected one. | |
2574 | ||
2575 | Optional argument BURY-OR-KILL non-nil means the buffer currently | |
2576 | shown in WINDOW is about to be buried or killed and consequently | |
2577 | shall not be switched to in future invocations of this command." | |
2578 | (interactive) | |
5386012d | 2579 | (let* ((window (window-normalize-live-window window)) |
9397e56f MR |
2580 | (old-buffer (window-buffer window)) |
2581 | ;; Save this since it's destroyed by `set-window-buffer'. | |
2582 | (next-buffers (window-next-buffers window)) | |
2583 | entry new-buffer killed-buffers deletable visible) | |
2584 | (cond | |
2585 | ;; When BURY-OR-KILL is non-nil, there's no previous buffer for | |
2586 | ;; this window, and we can delete the window (or the frame) do | |
2587 | ;; that. | |
2588 | ((and bury-or-kill | |
2589 | (or (not (window-prev-buffers window)) | |
2590 | (and (eq (caar (window-prev-buffers window)) old-buffer) | |
2591 | (not (cdr (car (window-prev-buffers window)))))) | |
2592 | (setq deletable (window-deletable-p window))) | |
2593 | (if (eq deletable 'frame) | |
2594 | (delete-frame (window-frame window)) | |
2595 | (delete-window window))) | |
2596 | ((window-dedicated-p window) | |
2597 | (error "Window %s is dedicated to buffer %s" window old-buffer))) | |
2598 | ||
2599 | (unless deletable | |
2600 | (catch 'found | |
2601 | ;; Scan WINDOW's previous buffers first, skipping entries of next | |
2602 | ;; buffers. | |
2603 | (dolist (entry (window-prev-buffers window)) | |
2604 | (when (and (setq new-buffer (car entry)) | |
2605 | (or (buffer-live-p new-buffer) | |
2606 | (not (setq killed-buffers | |
2607 | (cons new-buffer killed-buffers)))) | |
2608 | (not (eq new-buffer old-buffer)) | |
2609 | (or bury-or-kill | |
2610 | (not (memq new-buffer next-buffers)))) | |
2611 | (set-window-buffer-start-and-point | |
2612 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
2613 | (throw 'found t))) | |
2614 | ;; Scan reverted buffer list of WINDOW's frame next, skipping | |
2615 | ;; entries of next buffers. Note that when we bury or kill a | |
2616 | ;; buffer we don't reverse the global buffer list to avoid showing | |
2617 | ;; a buried buffer instead. Otherwise, we must reverse the global | |
2618 | ;; buffer list in order to make sure that switching to the | |
2619 | ;; previous/next buffer traverse it in opposite directions. | |
2620 | (dolist (buffer (if bury-or-kill | |
2621 | (buffer-list (window-frame window)) | |
2622 | (nreverse (buffer-list (window-frame window))))) | |
2623 | (when (and (buffer-live-p buffer) | |
2624 | (not (eq buffer old-buffer)) | |
2625 | (not (eq (aref (buffer-name buffer) 0) ?\s)) | |
2626 | (or bury-or-kill (not (memq buffer next-buffers)))) | |
2627 | (if (get-buffer-window buffer) | |
2628 | ;; Try to avoid showing a buffer visible in some other window. | |
2629 | (setq visible buffer) | |
2630 | (setq new-buffer buffer) | |
2631 | (set-window-buffer-start-and-point window new-buffer) | |
2632 | (throw 'found t)))) | |
2633 | (unless bury-or-kill | |
2634 | ;; Scan reverted next buffers last (must not use nreverse | |
2635 | ;; here!). | |
2636 | (dolist (buffer (reverse next-buffers)) | |
2637 | ;; Actually, buffer _must_ be live here since otherwise it | |
2638 | ;; would have been caught in the scan of previous buffers. | |
2639 | (when (and (or (buffer-live-p buffer) | |
2640 | (not (setq killed-buffers | |
2641 | (cons buffer killed-buffers)))) | |
2642 | (not (eq buffer old-buffer)) | |
2643 | (setq entry (assq buffer (window-prev-buffers window)))) | |
2644 | (setq new-buffer buffer) | |
2645 | (set-window-buffer-start-and-point | |
2646 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
2647 | (throw 'found t)))) | |
2648 | ||
2649 | ;; Show a buffer visible in another window. | |
2650 | (when visible | |
2651 | (setq new-buffer visible) | |
2652 | (set-window-buffer-start-and-point window new-buffer))) | |
2653 | ||
2654 | (if bury-or-kill | |
2655 | ;; Remove `old-buffer' from WINDOW's previous and (restored list | |
2656 | ;; of) next buffers. | |
2657 | (progn | |
2658 | (set-window-prev-buffers | |
2659 | window (assq-delete-all old-buffer (window-prev-buffers window))) | |
2660 | (set-window-next-buffers window (delq old-buffer next-buffers))) | |
2661 | ;; Move `old-buffer' to head of WINDOW's restored list of next | |
2662 | ;; buffers. | |
2663 | (set-window-next-buffers | |
2664 | window (cons old-buffer (delq old-buffer next-buffers))))) | |
2665 | ||
2666 | ;; Remove killed buffers from WINDOW's previous and next buffers. | |
2667 | (when killed-buffers | |
2668 | (dolist (buffer killed-buffers) | |
2669 | (set-window-prev-buffers | |
2670 | window (assq-delete-all buffer (window-prev-buffers window))) | |
2671 | (set-window-next-buffers | |
2672 | window (delq buffer (window-next-buffers window))))) | |
2673 | ||
2674 | ;; Return new-buffer. | |
2675 | new-buffer)) | |
2676 | ||
2677 | (defun switch-to-next-buffer (&optional window) | |
2678 | "In WINDOW switch to next buffer. | |
2679 | WINDOW must be a live window and defaults to the selected one." | |
2680 | (interactive) | |
5386012d | 2681 | (let* ((window (window-normalize-live-window window)) |
9397e56f MR |
2682 | (old-buffer (window-buffer window)) |
2683 | (next-buffers (window-next-buffers window)) | |
2684 | new-buffer entry killed-buffers visible) | |
2685 | (when (window-dedicated-p window) | |
2686 | (error "Window %s is dedicated to buffer %s" window old-buffer)) | |
2687 | ||
2688 | (catch 'found | |
2689 | ;; Scan WINDOW's next buffers first. | |
2690 | (dolist (buffer next-buffers) | |
2691 | (when (and (or (buffer-live-p buffer) | |
2692 | (not (setq killed-buffers | |
2693 | (cons buffer killed-buffers)))) | |
2694 | (not (eq buffer old-buffer)) | |
2695 | (setq entry (assq buffer (window-prev-buffers window)))) | |
2696 | (setq new-buffer buffer) | |
2697 | (set-window-buffer-start-and-point | |
2698 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
2699 | (throw 'found t))) | |
2700 | ;; Scan the buffer list of WINDOW's frame next, skipping previous | |
2701 | ;; buffers entries. | |
2702 | (dolist (buffer (buffer-list (window-frame window))) | |
2703 | (when (and (buffer-live-p buffer) (not (eq buffer old-buffer)) | |
2704 | (not (eq (aref (buffer-name buffer) 0) ?\s)) | |
2705 | (not (assq buffer (window-prev-buffers window)))) | |
2706 | (if (get-buffer-window buffer) | |
2707 | ;; Try to avoid showing a buffer visible in some other window. | |
2708 | (setq visible buffer) | |
2709 | (setq new-buffer buffer) | |
2710 | (set-window-buffer-start-and-point window new-buffer) | |
2711 | (throw 'found t)))) | |
2712 | ;; Scan WINDOW's reverted previous buffers last (must not use | |
2713 | ;; nreverse here!) | |
2714 | (dolist (entry (reverse (window-prev-buffers window))) | |
2715 | (when (and (setq new-buffer (car entry)) | |
2716 | (or (buffer-live-p new-buffer) | |
2717 | (not (setq killed-buffers | |
2718 | (cons new-buffer killed-buffers)))) | |
2719 | (not (eq new-buffer old-buffer))) | |
2720 | (set-window-buffer-start-and-point | |
2721 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
2722 | (throw 'found t))) | |
2723 | ||
2724 | ;; Show a buffer visible in another window. | |
2725 | (when visible | |
2726 | (setq new-buffer visible) | |
2727 | (set-window-buffer-start-and-point window new-buffer))) | |
2728 | ||
2729 | ;; Remove `new-buffer' from and restore WINDOW's next buffers. | |
2730 | (set-window-next-buffers window (delq new-buffer next-buffers)) | |
2731 | ||
2732 | ;; Remove killed buffers from WINDOW's previous and next buffers. | |
2733 | (when killed-buffers | |
2734 | (dolist (buffer killed-buffers) | |
2735 | (set-window-prev-buffers | |
2736 | window (assq-delete-all buffer (window-prev-buffers window))) | |
2737 | (set-window-next-buffers | |
2738 | window (delq buffer (window-next-buffers window))))) | |
2739 | ||
2740 | ;; Return new-buffer. | |
2741 | new-buffer)) | |
2742 | ||
2743 | (defun get-next-valid-buffer (list &optional buffer visible-ok frame) | |
2744 | "Search LIST for a valid buffer to display in FRAME. | |
2745 | Return nil when all buffers in LIST are undesirable for display, | |
2746 | otherwise return the first suitable buffer in LIST. | |
2747 | ||
2748 | Buffers not visible in windows are preferred to visible buffers, | |
2749 | unless VISIBLE-OK is non-nil. | |
2750 | If the optional argument FRAME is nil, it defaults to the selected frame. | |
2751 | If BUFFER is non-nil, ignore occurrences of that buffer in LIST." | |
2752 | ;; This logic is more or less copied from other-buffer. | |
2753 | (setq frame (or frame (selected-frame))) | |
2754 | (let ((pred (frame-parameter frame 'buffer-predicate)) | |
2755 | found buf) | |
2756 | (while (and (not found) list) | |
2757 | (setq buf (car list)) | |
2758 | (if (and (not (eq buffer buf)) | |
2759 | (buffer-live-p buf) | |
2760 | (or (null pred) (funcall pred buf)) | |
2761 | (not (eq (aref (buffer-name buf) 0) ?\s)) | |
2762 | (or visible-ok (null (get-buffer-window buf 'visible)))) | |
2763 | (setq found buf) | |
2764 | (setq list (cdr list)))) | |
2765 | (car list))) | |
2766 | ||
2767 | (defun last-buffer (&optional buffer visible-ok frame) | |
2768 | "Return the last buffer in FRAME's buffer list. | |
2769 | If BUFFER is the last buffer, return the preceding buffer | |
2770 | instead. Buffers not visible in windows are preferred to visible | |
2771 | buffers, unless optional argument VISIBLE-OK is non-nil. | |
2772 | Optional third argument FRAME nil or omitted means use the | |
2773 | selected frame's buffer list. If no such buffer exists, return | |
2774 | the buffer `*scratch*', creating it if necessary." | |
2775 | (setq frame (or frame (selected-frame))) | |
2776 | (or (get-next-valid-buffer (nreverse (buffer-list frame)) | |
2777 | buffer visible-ok frame) | |
2778 | (get-buffer "*scratch*") | |
2779 | (let ((scratch (get-buffer-create "*scratch*"))) | |
2780 | (set-buffer-major-mode scratch) | |
2781 | scratch))) | |
2782 | ||
2783 | (defun bury-buffer (&optional buffer-or-name) | |
2784 | "Put BUFFER-OR-NAME at the end of the list of all buffers. | |
2785 | There it is the least likely candidate for `other-buffer' to | |
2786 | return; thus, the least likely buffer for \\[switch-to-buffer] to | |
2787 | select by default. | |
2788 | ||
2789 | You can specify a buffer name as BUFFER-OR-NAME, or an actual | |
2790 | buffer object. If BUFFER-OR-NAME is nil or omitted, bury the | |
2791 | current buffer. Also, if BUFFER-OR-NAME is nil or omitted, | |
2792 | remove the current buffer from the selected window if it is | |
2793 | displayed there." | |
2794 | (interactive) | |
5386012d | 2795 | (let* ((buffer (window-normalize-buffer buffer-or-name))) |
9397e56f MR |
2796 | ;; If `buffer-or-name' is not on the selected frame we unrecord it |
2797 | ;; although it's not "here" (call it a feature). | |
2798 | (unrecord-buffer buffer) | |
2799 | ;; Handle case where `buffer-or-name' is nil and the current buffer | |
2800 | ;; is shown in the selected window. | |
2801 | (cond | |
2802 | ((or buffer-or-name (not (eq buffer (window-buffer))))) | |
2803 | ((not (window-dedicated-p)) | |
2804 | (switch-to-prev-buffer nil 'bury)) | |
0e61a35f SM |
2805 | ((and (frame-root-window-p (selected-window)) |
2806 | ;; Don't iconify if it's the only frame. | |
2807 | (not (eq (next-frame nil 0) (selected-frame)))) | |
9397e56f MR |
2808 | (iconify-frame (window-frame (selected-window)))) |
2809 | ((window-deletable-p) | |
2810 | (delete-window))) | |
2811 | ;; Always return nil. | |
2812 | nil)) | |
2813 | ||
2814 | (defun unbury-buffer () | |
2815 | "Switch to the last buffer in the buffer list." | |
2816 | (interactive) | |
2817 | (switch-to-buffer (last-buffer))) | |
2818 | ||
2819 | (defun next-buffer () | |
2820 | "In selected window switch to next buffer." | |
2821 | (interactive) | |
2822 | (switch-to-next-buffer)) | |
2823 | ||
2824 | (defun previous-buffer () | |
2825 | "In selected window switch to previous buffer." | |
2826 | (interactive) | |
2827 | (switch-to-prev-buffer)) | |
2828 | ||
2829 | (defun delete-windows-on (&optional buffer-or-name frame) | |
2830 | "Delete all windows showing BUFFER-OR-NAME. | |
2831 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer | |
2832 | and defaults to the current buffer. | |
2833 | ||
2834 | The following non-nil values of the optional argument FRAME | |
2835 | have special meanings: | |
2836 | ||
2837 | - t means consider all windows on the selected frame only. | |
2838 | ||
2839 | - `visible' means consider all windows on all visible frames on | |
2840 | the current terminal. | |
2841 | ||
2842 | - 0 (the number zero) means consider all windows on all visible | |
2843 | and iconified frames on the current terminal. | |
2844 | ||
2845 | - A frame means consider all windows on that frame only. | |
2846 | ||
2847 | Any other value of FRAME means consider all windows on all | |
2848 | frames. | |
2849 | ||
2850 | When a window showing BUFFER-OR-NAME is dedicated and the only | |
2851 | window of its frame, that frame is deleted when there are other | |
2852 | frames left." | |
2853 | (interactive "BDelete windows on (buffer):\nP") | |
5386012d | 2854 | (let ((buffer (window-normalize-buffer buffer-or-name)) |
9397e56f MR |
2855 | ;; Handle the "inverted" meaning of the FRAME argument wrt other |
2856 | ;; `window-list-1' based function. | |
2857 | (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame)))) | |
2858 | (dolist (window (window-list-1 nil nil all-frames)) | |
2859 | (if (eq (window-buffer window) buffer) | |
2860 | (let ((deletable (window-deletable-p window))) | |
2861 | (cond | |
2862 | ((eq deletable 'frame) | |
2863 | ;; Delete frame. | |
2864 | (delete-frame (window-frame window))) | |
2865 | (deletable | |
2866 | ;; Delete window only. | |
2867 | (delete-window window)) | |
2868 | (t | |
2869 | ;; In window switch to previous buffer. | |
2870 | (set-window-dedicated-p window nil) | |
2871 | (switch-to-prev-buffer window 'bury)))) | |
2872 | ;; If a window doesn't show BUFFER, unrecord BUFFER in it. | |
2873 | (unrecord-window-buffer window buffer))))) | |
2874 | ||
2875 | (defun replace-buffer-in-windows (&optional buffer-or-name) | |
2876 | "Replace BUFFER-OR-NAME with some other buffer in all windows showing it. | |
2877 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer | |
2878 | and defaults to the current buffer. | |
2879 | ||
2880 | When a window showing BUFFER-OR-NAME is either dedicated, or the | |
2881 | window has no previous buffer, that window is deleted. If that | |
2882 | window is the only window on its frame, the frame is deleted too | |
2883 | when there are other frames left. If there are no other frames | |
2884 | left, some other buffer is displayed in that window. | |
2885 | ||
2886 | This function removes the buffer denoted by BUFFER-OR-NAME from | |
2887 | all window-local buffer lists." | |
5386012d | 2888 | (let ((buffer (window-normalize-buffer buffer-or-name))) |
9397e56f MR |
2889 | (dolist (window (window-list-1 nil nil t)) |
2890 | (if (eq (window-buffer window) buffer) | |
2891 | (let ((deletable (window-deletable-p window))) | |
2892 | (cond | |
2893 | ((eq deletable 'frame) | |
2894 | ;; Delete frame. | |
2895 | (delete-frame (window-frame window))) | |
2896 | ((and (window-dedicated-p window) deletable) | |
2897 | ;; Delete window. | |
2898 | (delete-window window)) | |
2899 | (t | |
2900 | ;; Switch to another buffer in window. | |
2901 | (set-window-dedicated-p window nil) | |
2902 | (switch-to-prev-buffer window 'kill)))) | |
2903 | ;; Unrecord BUFFER in WINDOW. | |
2904 | (unrecord-window-buffer window buffer))))) | |
2905 | ||
2906 | (defun quit-restore-window (&optional window kill) | |
2907 | "Quit WINDOW in some way. | |
2908 | WINDOW must be a live window and defaults to the selected window. | |
2909 | Return nil. | |
2910 | ||
2911 | According to information stored in WINDOW's `quit-restore' window | |
2912 | parameter either \(1) delete WINDOW and its frame, \(2) delete | |
2913 | WINDOW, \(3) restore the buffer previously displayed in WINDOW, | |
2914 | or \(4) make WINDOW display some other buffer than the present | |
2915 | one. If non-nil, reset `quit-restore' parameter to nil. | |
2916 | ||
2917 | Optional argument KILL non-nil means in addition kill WINDOW's | |
2918 | buffer. If KILL is nil, put WINDOW's buffer at the end of the | |
2919 | buffer list. Interactively, KILL is the prefix argument." | |
2920 | (interactive "i\nP") | |
5386012d | 2921 | (setq window (window-normalize-live-window window)) |
9397e56f MR |
2922 | (let ((buffer (window-buffer window)) |
2923 | (quit-restore (window-parameter window 'quit-restore)) | |
2924 | deletable resize) | |
2925 | (cond | |
2926 | ((and (or (and (memq (car-safe quit-restore) '(new-window new-frame)) | |
2927 | ;; Check that WINDOW's buffer is still the same. | |
2928 | (eq (window-buffer window) (nth 1 quit-restore))) | |
2929 | (window-dedicated-p window)) | |
2930 | (setq deletable (window-deletable-p window))) | |
2931 | ;; WINDOW can be deleted. | |
2932 | (unrecord-buffer buffer) | |
2933 | (if (eq deletable 'frame) | |
2934 | ;; WINDOW's frame can be deleted. | |
2935 | (delete-frame (window-frame window)) | |
2936 | ;; Just delete WINDOW. | |
2937 | (delete-window window)) | |
2938 | ;; If the previously selected window is still alive, select it. | |
2939 | (when (window-live-p (nth 2 quit-restore)) | |
2940 | (select-window (nth 2 quit-restore)))) | |
2941 | ((and (buffer-live-p (nth 0 quit-restore)) | |
2942 | ;; The buffer currently shown in WINDOW must still be the | |
2943 | ;; buffer shown when its `quit-restore' parameter was created | |
2944 | ;; in the first place. | |
2945 | (eq (window-buffer window) (nth 3 quit-restore))) | |
2946 | (setq resize (with-current-buffer buffer temp-buffer-resize-mode)) | |
2947 | ;; Unrecord buffer. | |
2948 | (unrecord-buffer buffer) | |
2949 | (unrecord-window-buffer window buffer) | |
2950 | ;; Display buffer stored in the quit-restore parameter. | |
2951 | (set-window-dedicated-p window nil) | |
2952 | (set-window-buffer window (nth 0 quit-restore)) | |
2953 | (set-window-start window (nth 1 quit-restore)) | |
2954 | (set-window-point window (nth 2 quit-restore)) | |
2955 | (when (and resize (/= (nth 4 quit-restore) (window-total-size window))) | |
d615d6d2 | 2956 | (window-resize |
9397e56f MR |
2957 | window (- (nth 4 quit-restore) (window-total-size window)))) |
2958 | ;; Reset the quit-restore parameter. | |
2959 | (set-window-parameter window 'quit-restore nil) | |
2960 | (when (window-live-p (nth 5 quit-restore)) | |
2961 | (select-window (nth 5 quit-restore)))) | |
2962 | (t | |
2963 | ;; Otherwise, show another buffer in WINDOW and reset the | |
2964 | ;; quit-restore parameter. | |
2965 | (set-window-parameter window 'quit-restore nil) | |
2966 | (unrecord-buffer buffer) | |
2967 | (switch-to-prev-buffer window 'bury-or-kill))) | |
2968 | ||
2969 | ;; Kill WINDOW's old-buffer if requested | |
2970 | (when kill (kill-buffer buffer)) | |
2971 | nil)) | |
562dd5e9 MR |
2972 | \f |
2973 | ;;; Splitting windows. | |
2974 | (defsubst window-split-min-size (&optional horizontal) | |
2975 | "Return minimum height of any window when splitting windows. | |
2976 | Optional argument HORIZONTAL non-nil means return minimum width." | |
2977 | (if horizontal | |
2978 | (max window-min-width window-safe-min-width) | |
2979 | (max window-min-height window-safe-min-height))) | |
2980 | ||
2981 | (defun split-window (&optional window size side) | |
2982 | "Make a new window adjacent to WINDOW. | |
2983 | WINDOW can be any window and defaults to the selected one. | |
2984 | Return the new window which is always a live window. | |
2985 | ||
2986 | Optional argument SIZE a positive number means make WINDOW SIZE | |
2987 | lines or columns tall. If SIZE is negative, make the new window | |
2988 | -SIZE lines or columns tall. If and only if SIZE is non-nil, its | |
2989 | absolute value can be less than `window-min-height' or | |
2990 | `window-min-width'; so this command can make a new window as | |
2991 | small as one line or two columns. SIZE defaults to half of | |
2992 | WINDOW's size. Interactively, SIZE is the prefix argument. | |
2993 | ||
2994 | Optional third argument SIDE nil (or `below') specifies that the | |
2995 | new window shall be located below WINDOW. SIDE `above' means the | |
2996 | new window shall be located above WINDOW. In both cases SIZE | |
2997 | specifies the new number of lines for WINDOW \(or the new window | |
2998 | if SIZE is negative) including space reserved for the mode and/or | |
2999 | header line. | |
3000 | ||
3001 | SIDE t (or `right') specifies that the new window shall be | |
3002 | located on the right side of WINDOW. SIDE `left' means the new | |
3003 | window shall be located on the left of WINDOW. In both cases | |
3004 | SIZE specifies the new number of columns for WINDOW \(or the new | |
3005 | window provided SIZE is negative) including space reserved for | |
3006 | fringes and the scrollbar or a divider column. Any other non-nil | |
3007 | value for SIDE is currently handled like t (or `right'). | |
3008 | ||
3009 | If the variable `ignore-window-parameters' is non-nil or the | |
3010 | `split-window' parameter of WINDOW equals t, do not process any | |
3011 | parameters of WINDOW. Otherwise, if the `split-window' parameter | |
3012 | of WINDOW specifies a function, call that function with all three | |
3013 | arguments and return the value returned by that function. | |
3014 | ||
3015 | Otherwise, if WINDOW is part of an atomic window, \"split\" the | |
3016 | root of that atomic window. The new window does not become a | |
3017 | member of that atomic window. | |
3018 | ||
3019 | If WINDOW is live, properties of the new window like margins and | |
3020 | scrollbars are inherited from WINDOW. If WINDOW is an internal | |
3021 | window, these properties as well as the buffer displayed in the | |
3022 | new window are inherited from the window selected on WINDOW's | |
3023 | frame. The selected window is not changed by this function." | |
3024 | (interactive "i") | |
5386012d | 3025 | (setq window (window-normalize-any-window window)) |
130e3e11 MR |
3026 | (let* ((side (cond |
3027 | ((not side) 'below) | |
3028 | ((memq side '(below above right left)) side) | |
3029 | (t 'right))) | |
3030 | (horizontal (not (memq side '(nil below above)))) | |
562dd5e9 MR |
3031 | (frame (window-frame window)) |
3032 | (parent (window-parent window)) | |
3033 | (function (window-parameter window 'split-window)) | |
3034 | (window-side (window-parameter window 'window-side)) | |
3035 | ;; Rebind `window-nest' since in some cases we may have to | |
3036 | ;; override its value. | |
3037 | (window-nest window-nest) | |
3038 | atom-root) | |
3039 | ||
3040 | (window-check frame) | |
3041 | (catch 'done | |
3042 | (cond | |
3043 | ;; Ignore window parameters if either `ignore-window-parameters' | |
3044 | ;; is t or the `split-window' parameter equals t. | |
3045 | ((or ignore-window-parameters (eq function t))) | |
3046 | ((functionp function) | |
3047 | ;; The `split-window' parameter specifies the function to call. | |
3048 | ;; If that function is `ignore', do nothing. | |
3049 | (throw 'done (funcall function window size side))) | |
3050 | ;; If WINDOW is a subwindow of an atomic window, split the root | |
3051 | ;; window of that atomic window instead. | |
3052 | ((and (window-parameter window 'window-atom) | |
3053 | (setq atom-root (window-atom-root window)) | |
3054 | (not (eq atom-root window))) | |
3055 | (throw 'done (split-window atom-root size side)))) | |
3056 | ||
3057 | (when (and window-side | |
3058 | (or (not parent) | |
3059 | (not (window-parameter parent 'window-side)))) | |
3060 | ;; WINDOW is a side root window. To make sure that a new parent | |
3061 | ;; window gets created set `window-nest' to t. | |
3062 | (setq window-nest t)) | |
3063 | ||
3064 | (when (and window-splits size (> size 0)) | |
3065 | ;; If `window-splits' is non-nil and SIZE is a non-negative | |
3066 | ;; integer, we cannot reasonably resize other windows. Rather | |
3067 | ;; bind `window-nest' to t to make sure that subsequent window | |
3068 | ;; deletions are handled correctly. | |
3069 | (setq window-nest t)) | |
3070 | ||
3071 | (let* ((parent-size | |
3072 | ;; `parent-size' is the size of WINDOW's parent, provided | |
3073 | ;; it has one. | |
3074 | (when parent (window-total-size parent horizontal))) | |
3075 | ;; `resize' non-nil means we are supposed to resize other | |
3076 | ;; windows in WINDOW's combination. | |
3077 | (resize | |
3078 | (and window-splits (not window-nest) | |
3079 | ;; Resize makes sense in iso-combinations only. | |
3080 | (window-iso-combined-p window horizontal))) | |
3081 | ;; `old-size' is the current size of WINDOW. | |
3082 | (old-size (window-total-size window horizontal)) | |
3083 | ;; `new-size' is the specified or calculated size of the | |
3084 | ;; new window. | |
3085 | (new-size | |
3086 | (cond | |
3087 | ((not size) | |
3088 | (max (window-split-min-size horizontal) | |
3089 | (if resize | |
3090 | ;; When resizing try to give the new window the | |
3091 | ;; average size of a window in its combination. | |
3092 | (min (- parent-size | |
3093 | (window-min-size parent horizontal)) | |
3094 | (/ parent-size | |
3095 | (1+ (window-iso-combinations | |
3096 | parent horizontal)))) | |
3097 | ;; Else try to give the new window half the size | |
3098 | ;; of WINDOW (plus an eventual odd line). | |
3099 | (+ (/ old-size 2) (% old-size 2))))) | |
3100 | ((>= size 0) | |
3101 | ;; SIZE non-negative specifies the new size of WINDOW. | |
3102 | ||
3103 | ;; Note: Specifying a non-negative SIZE is practically | |
3104 | ;; always done as workaround for making the new window | |
3105 | ;; appear above or on the left of the new window (the | |
3106 | ;; ispell window is a typical example of that). In all | |
3107 | ;; these cases the SIDE argument should be set to 'above | |
3108 | ;; or 'left in order to support the 'resize option. | |
3109 | ;; Here we have to nest the windows instead, see above. | |
3110 | (- old-size size)) | |
3111 | (t | |
3112 | ;; SIZE negative specifies the size of the new window. | |
3113 | (- size)))) | |
3114 | new-parent new-normal) | |
3115 | ||
3116 | ;; Check SIZE. | |
3117 | (cond | |
3118 | ((not size) | |
3119 | (cond | |
3120 | (resize | |
3121 | ;; SIZE unspecified, resizing. | |
3122 | (when (and (not (window-sizable-p parent (- new-size) horizontal)) | |
3123 | ;; Try again with minimum split size. | |
3124 | (setq new-size | |
3125 | (max new-size (window-split-min-size horizontal))) | |
3126 | (not (window-sizable-p parent (- new-size) horizontal))) | |
3127 | (error "Window %s too small for splitting" parent))) | |
3128 | ((> (+ new-size (window-min-size window horizontal)) old-size) | |
3129 | ;; SIZE unspecified, no resizing. | |
3130 | (error "Window %s too small for splitting" window)))) | |
3131 | ((and (>= size 0) | |
3132 | (or (>= size old-size) | |
3133 | (< new-size (if horizontal | |
3134 | window-safe-min-width | |
3135 | window-safe-min-width)))) | |
3136 | ;; SIZE specified as new size of old window. If the new size | |
3137 | ;; is larger than the old size or the size of the new window | |
3138 | ;; would be less than the safe minimum, signal an error. | |
3139 | (error "Window %s too small for splitting" window)) | |
3140 | (resize | |
3141 | ;; SIZE specified, resizing. | |
3142 | (unless (window-sizable-p parent (- new-size) horizontal) | |
3143 | ;; If we cannot resize the parent give up. | |
3144 | (error "Window %s too small for splitting" parent))) | |
3145 | ((or (< new-size | |
3146 | (if horizontal window-safe-min-width window-safe-min-height)) | |
3147 | (< (- old-size new-size) | |
3148 | (if horizontal window-safe-min-width window-safe-min-height))) | |
3149 | ;; SIZE specification violates minimum size restrictions. | |
3150 | (error "Window %s too small for splitting" window))) | |
3151 | ||
5386012d | 3152 | (window--resize-reset frame horizontal) |
562dd5e9 MR |
3153 | |
3154 | (setq new-parent | |
3155 | ;; Make new-parent non-nil if we need a new parent window; | |
3156 | ;; either because we want to nest or because WINDOW is not | |
3157 | ;; iso-combined. | |
3158 | (or window-nest (not (window-iso-combined-p window horizontal)))) | |
3159 | (setq new-normal | |
3160 | ;; Make new-normal the normal size of the new window. | |
3161 | (cond | |
3162 | (size (/ (float new-size) (if new-parent old-size parent-size))) | |
3163 | (new-parent 0.5) | |
3164 | (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal)))) | |
3165 | (t (/ (window-normal-size window horizontal) 2.0)))) | |
3166 | ||
3167 | (if resize | |
3168 | ;; Try to get space from OLD's siblings. We could go "up" and | |
3169 | ;; try getting additional space from surrounding windows but | |
3170 | ;; we won't be able to return space to those windows when we | |
3171 | ;; delete the one we create here. Hence we do not go up. | |
3172 | (progn | |
5386012d | 3173 | (window--resize-subwindows parent (- new-size) horizontal) |
562dd5e9 MR |
3174 | (let* ((normal (- 1.0 new-normal)) |
3175 | (sub (window-child parent))) | |
3176 | (while sub | |
3177 | (set-window-new-normal | |
3178 | sub (* (window-normal-size sub horizontal) normal)) | |
3179 | (setq sub (window-right sub))))) | |
3180 | ;; Get entire space from WINDOW. | |
3181 | (set-window-new-total window (- old-size new-size)) | |
5386012d | 3182 | (window--resize-this-window window (- new-size) horizontal) |
562dd5e9 MR |
3183 | (set-window-new-normal |
3184 | window (- (if new-parent 1.0 (window-normal-size window horizontal)) | |
3185 | new-normal))) | |
3186 | ||
3187 | (let* ((new (split-window-internal window new-size side new-normal))) | |
3188 | ;; Inherit window-side parameters, if any. | |
3189 | (when (and window-side new-parent) | |
3190 | (set-window-parameter (window-parent new) 'window-side window-side) | |
3191 | (set-window-parameter new 'window-side window-side)) | |
3192 | ||
3193 | (run-window-configuration-change-hook frame) | |
3194 | (window-check frame) | |
3195 | ;; Always return the new window. | |
3196 | new))))) | |
3197 | ||
3198 | ;; I think this should be the default; I think people will prefer it--rms. | |
3199 | (defcustom split-window-keep-point t | |
3200 | "If non-nil, \\[split-window-above-each-other] keeps the original point \ | |
3201 | in both children. | |
3202 | This is often more convenient for editing. | |
3203 | If nil, adjust point in each of the two windows to minimize redisplay. | |
3204 | This is convenient on slow terminals, but point can move strangely. | |
3205 | ||
3206 | This option applies only to `split-window-above-each-other' and | |
3207 | functions that call it. `split-window' always keeps the original | |
3208 | point in both children." | |
3209 | :type 'boolean | |
3210 | :group 'windows) | |
3211 | ||
3212 | (defun split-window-above-each-other (&optional size) | |
3213 | "Split selected window into two windows, one above the other. | |
3214 | The upper window gets SIZE lines and the lower one gets the rest. | |
3215 | SIZE negative means the lower window gets -SIZE lines and the | |
3216 | upper one the rest. With no argument, split windows equally or | |
3217 | close to it. Both windows display the same buffer, now current. | |
3218 | ||
3219 | If the variable `split-window-keep-point' is non-nil, both new | |
3220 | windows will get the same value of point as the selected window. | |
3221 | This is often more convenient for editing. The upper window is | |
3222 | the selected window. | |
3223 | ||
3224 | Otherwise, we choose window starts so as to minimize the amount of | |
3225 | redisplay; this is convenient on slow terminals. The new selected | |
3226 | window is the one that the current value of point appears in. The | |
3227 | value of point can change if the text around point is hidden by the | |
3228 | new mode line. | |
3229 | ||
3230 | Regardless of the value of `split-window-keep-point', the upper | |
3231 | window is the original one and the return value is the new, lower | |
3232 | window." | |
3233 | (interactive "P") | |
3234 | (let ((old-window (selected-window)) | |
3235 | (old-point (point)) | |
3236 | (size (and size (prefix-numeric-value size))) | |
3237 | moved-by-window-height moved new-window bottom) | |
3238 | (when (and size (< size 0) (< (- size) window-min-height)) | |
3239 | ;; `split-window' would not signal an error here. | |
3240 | (error "Size of new window too small")) | |
3241 | (setq new-window (split-window nil size)) | |
3242 | (unless split-window-keep-point | |
3243 | (with-current-buffer (window-buffer) | |
3244 | (goto-char (window-start)) | |
3245 | (setq moved (vertical-motion (window-height))) | |
3246 | (set-window-start new-window (point)) | |
3247 | (when (> (point) (window-point new-window)) | |
3248 | (set-window-point new-window (point))) | |
3249 | (when (= moved (window-height)) | |
3250 | (setq moved-by-window-height t) | |
3251 | (vertical-motion -1)) | |
3252 | (setq bottom (point))) | |
3253 | (and moved-by-window-height | |
3254 | (<= bottom (point)) | |
3255 | (set-window-point old-window (1- bottom))) | |
3256 | (and moved-by-window-height | |
3257 | (<= (window-start new-window) old-point) | |
3258 | (set-window-point new-window old-point) | |
3259 | (select-window new-window))) | |
9397e56f MR |
3260 | ;; Always copy quit-restore parameter in interactive use. |
3261 | (let ((quit-restore (window-parameter old-window 'quit-restore))) | |
3262 | (when quit-restore | |
3263 | (set-window-parameter new-window 'quit-restore quit-restore))) | |
3264 | new-window)) | |
562dd5e9 MR |
3265 | |
3266 | (defalias 'split-window-vertically 'split-window-above-each-other) | |
3267 | ||
562dd5e9 MR |
3268 | (defun split-window-side-by-side (&optional size) |
3269 | "Split selected window into two windows side by side. | |
3270 | The selected window becomes the left one and gets SIZE columns. | |
3271 | SIZE negative means the right window gets -SIZE lines. | |
3272 | ||
3273 | SIZE includes the width of the window's scroll bar; if there are | |
3274 | no scroll bars, it includes the width of the divider column to | |
3275 | the window's right, if any. SIZE omitted or nil means split | |
3276 | window equally. | |
3277 | ||
3278 | The selected window remains selected. Return the new window." | |
3279 | (interactive "P") | |
3280 | (let ((old-window (selected-window)) | |
3281 | (size (and size (prefix-numeric-value size))) | |
3282 | new-window) | |
3283 | (when (and size (< size 0) (< (- size) window-min-width)) | |
3284 | ;; `split-window' would not signal an error here. | |
3285 | (error "Size of new window too small")) | |
9397e56f MR |
3286 | (setq new-window (split-window nil size t)) |
3287 | ;; Always copy quit-restore parameter in interactive use. | |
3288 | (let ((quit-restore (window-parameter old-window 'quit-restore))) | |
3289 | (when quit-restore | |
3290 | (set-window-parameter new-window 'quit-restore quit-restore))) | |
3291 | new-window)) | |
562dd5e9 MR |
3292 | |
3293 | (defalias 'split-window-horizontally 'split-window-side-by-side) | |
3294 | \f | |
6198ccd0 MR |
3295 | ;;; Balancing windows. |
3296 | ||
3297 | ;; The following routine uses the recycled code from an old version of | |
5386012d MR |
3298 | ;; `window--resize-subwindows'. It's not very pretty, but coding it the way the |
3299 | ;; new `window--resize-subwindows' code does would hardly make it any shorter or | |
6198ccd0 MR |
3300 | ;; more readable (FWIW we'd need three loops - one to calculate the |
3301 | ;; minimum sizes per window, one to enlarge or shrink windows until the | |
3302 | ;; new parent-size matches, and one where we shrink the largest/enlarge | |
3303 | ;; the smallest window). | |
3304 | (defun balance-windows-2 (window horizontal) | |
3305 | "Subroutine of `balance-windows-1'. | |
3306 | WINDOW must be an iso-combination." | |
3307 | (let* ((first (window-child window)) | |
3308 | (sub first) | |
3309 | (number-of-children 0) | |
3310 | (parent-size (window-new-total window)) | |
3311 | (total-sum parent-size) | |
3312 | found failed size sub-total sub-delta sub-amount rest) | |
3313 | (while sub | |
3314 | (setq number-of-children (1+ number-of-children)) | |
3315 | (when (window-size-fixed-p sub horizontal) | |
3316 | (setq total-sum | |
3317 | (- total-sum (window-total-size sub horizontal))) | |
3318 | (set-window-new-normal sub 'ignore)) | |
3319 | (setq sub (window-right sub))) | |
3c448ab6 | 3320 | |
6198ccd0 MR |
3321 | (setq failed t) |
3322 | (while (and failed (> number-of-children 0)) | |
3323 | (setq size (/ total-sum number-of-children)) | |
3324 | (setq failed nil) | |
3325 | (setq sub first) | |
3326 | (while (and sub (not failed)) | |
3327 | ;; Ignore subwindows that should be ignored or are stuck. | |
5386012d | 3328 | (unless (window--resize-subwindows-skip-p sub) |
6198ccd0 MR |
3329 | (setq found t) |
3330 | (setq sub-total (window-total-size sub horizontal)) | |
3331 | (setq sub-delta (- size sub-total)) | |
3332 | (setq sub-amount | |
3333 | (window-sizable sub sub-delta horizontal)) | |
3334 | ;; Register the new total size for this subwindow. | |
3335 | (set-window-new-total sub (+ sub-total sub-amount)) | |
3336 | (unless (= sub-amount sub-delta) | |
3337 | (setq total-sum (- total-sum sub-total sub-amount)) | |
3338 | (setq number-of-children (1- number-of-children)) | |
3339 | ;; We failed and need a new round. | |
3340 | (setq failed t) | |
3341 | (set-window-new-normal sub 'skip))) | |
3342 | (setq sub (window-right sub)))) | |
3c448ab6 | 3343 | |
6198ccd0 MR |
3344 | (setq rest (% total-sum number-of-children)) |
3345 | ;; Fix rounding by trying to enlarge non-stuck windows by one line | |
3346 | ;; (column) until `rest' is zero. | |
3347 | (setq sub first) | |
3348 | (while (and sub (> rest 0)) | |
5386012d | 3349 | (unless (window--resize-subwindows-skip-p window) |
6198ccd0 MR |
3350 | (set-window-new-total sub 1 t) |
3351 | (setq rest (1- rest))) | |
3352 | (setq sub (window-right sub))) | |
3c448ab6 | 3353 | |
6198ccd0 MR |
3354 | ;; Fix rounding by trying to enlarge stuck windows by one line |
3355 | ;; (column) until `rest' equals zero. | |
3356 | (setq sub first) | |
3357 | (while (and sub (> rest 0)) | |
3358 | (unless (eq (window-new-normal sub) 'ignore) | |
3359 | (set-window-new-total sub 1 t) | |
3360 | (setq rest (1- rest))) | |
3361 | (setq sub (window-right sub))) | |
3362 | ||
3363 | (setq sub first) | |
3364 | (while sub | |
3365 | ;; Record new normal sizes. | |
3366 | (set-window-new-normal | |
3367 | sub (/ (if (eq (window-new-normal sub) 'ignore) | |
3368 | (window-total-size sub horizontal) | |
3369 | (window-new-total sub)) | |
3370 | (float parent-size))) | |
3371 | ;; Recursively balance each subwindow's subwindows. | |
3372 | (balance-windows-1 sub horizontal) | |
3373 | (setq sub (window-right sub))))) | |
3374 | ||
3375 | (defun balance-windows-1 (window &optional horizontal) | |
3376 | "Subroutine of `balance-windows'." | |
3377 | (if (window-child window) | |
3378 | (let ((sub (window-child window))) | |
3379 | (if (window-iso-combined-p sub horizontal) | |
3380 | (balance-windows-2 window horizontal) | |
3381 | (let ((size (window-new-total window))) | |
3382 | (while sub | |
9173deec | 3383 | (set-window-new-total sub size) |
6198ccd0 MR |
3384 | (balance-windows-1 sub horizontal) |
3385 | (setq sub (window-right sub)))))))) | |
3386 | ||
3387 | (defun balance-windows (&optional window-or-frame) | |
3388 | "Balance the sizes of subwindows of WINDOW-OR-FRAME. | |
3389 | WINDOW-OR-FRAME is optional and defaults to the selected frame. | |
3390 | If WINDOW-OR-FRAME denotes a frame, balance the sizes of all | |
3391 | subwindows of that frame's root window. If WINDOW-OR-FRAME | |
3392 | denots a window, balance the sizes of all subwindows of that | |
3393 | window." | |
3394 | (interactive) | |
3395 | (let* ((window | |
3396 | (cond | |
3397 | ((or (not window-or-frame) | |
3398 | (frame-live-p window-or-frame)) | |
3399 | (frame-root-window window-or-frame)) | |
3400 | ((or (window-live-p window-or-frame) | |
3401 | (window-child window-or-frame)) | |
3402 | window-or-frame) | |
3403 | (t | |
3404 | (error "Not a window or frame %s" window-or-frame)))) | |
3405 | (frame (window-frame window))) | |
3406 | ;; Balance vertically. | |
5386012d | 3407 | (window--resize-reset (window-frame window)) |
6198ccd0 | 3408 | (balance-windows-1 window) |
d615d6d2 | 3409 | (window-resize-apply frame) |
6198ccd0 | 3410 | ;; Balance horizontally. |
5386012d | 3411 | (window--resize-reset (window-frame window) t) |
6198ccd0 | 3412 | (balance-windows-1 window t) |
d615d6d2 | 3413 | (window-resize-apply frame t))) |
3c448ab6 MR |
3414 | |
3415 | (defun window-fixed-size-p (&optional window direction) | |
3416 | "Return t if WINDOW cannot be resized in DIRECTION. | |
3417 | WINDOW defaults to the selected window. DIRECTION can be | |
3418 | nil (i.e. any), `height' or `width'." | |
3419 | (with-current-buffer (window-buffer window) | |
3420 | (when (and (boundp 'window-size-fixed) window-size-fixed) | |
3421 | (not (and direction | |
3422 | (member (cons direction window-size-fixed) | |
3423 | '((height . width) (width . height)))))))) | |
3424 | ||
3425 | ;;; A different solution to balance-windows. | |
3c448ab6 MR |
3426 | (defvar window-area-factor 1 |
3427 | "Factor by which the window area should be over-estimated. | |
3428 | This is used by `balance-windows-area'. | |
3429 | Changing this globally has no effect.") | |
3430 | (make-variable-buffer-local 'window-area-factor) | |
3431 | ||
6198ccd0 | 3432 | (defun balance-windows-area-adjust (window delta horizontal) |
d615d6d2 | 3433 | "Wrapper around `window-resize' with error checking. |
6198ccd0 | 3434 | Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function." |
d615d6d2 | 3435 | ;; `window-resize' may fail if delta is too large. |
6198ccd0 MR |
3436 | (while (>= (abs delta) 1) |
3437 | (condition-case nil | |
3438 | (progn | |
d615d6d2 | 3439 | (window-resize window delta horizontal) |
6198ccd0 MR |
3440 | (setq delta 0)) |
3441 | (error | |
3442 | ;;(message "adjust: %s" (error-message-string err)) | |
3443 | (setq delta (/ delta 2)))))) | |
3444 | ||
3c448ab6 MR |
3445 | (defun balance-windows-area () |
3446 | "Make all visible windows the same area (approximately). | |
3447 | See also `window-area-factor' to change the relative size of | |
3448 | specific buffers." | |
3449 | (interactive) | |
3450 | (let* ((unchanged 0) (carry 0) (round 0) | |
3451 | ;; Remove fixed-size windows. | |
3452 | (wins (delq nil (mapcar (lambda (win) | |
3453 | (if (not (window-fixed-size-p win)) win)) | |
3454 | (window-list nil 'nomini)))) | |
3455 | (changelog nil) | |
3456 | next) | |
3457 | ;; Resizing a window changes the size of surrounding windows in complex | |
3458 | ;; ways, so it's difficult to balance them all. The introduction of | |
3459 | ;; `adjust-window-trailing-edge' made it a bit easier, but it is still | |
3460 | ;; very difficult to do. `balance-window' above takes an off-line | |
3461 | ;; approach: get the whole window tree, then balance it, then try to | |
3462 | ;; adjust the windows so they fit the result. | |
3463 | ;; Here, instead, we take a "local optimization" approach, where we just | |
3464 | ;; go through all the windows several times until nothing needs to be | |
3465 | ;; changed. The main problem with this approach is that it's difficult | |
3466 | ;; to make sure it terminates, so we use some heuristic to try and break | |
3467 | ;; off infinite loops. | |
3468 | ;; After a round without any change, we allow a second, to give a chance | |
3469 | ;; to the carry to propagate a minor imbalance from the end back to | |
3470 | ;; the beginning. | |
3471 | (while (< unchanged 2) | |
3472 | ;; (message "New round") | |
3473 | (setq unchanged (1+ unchanged) round (1+ round)) | |
3474 | (dolist (win wins) | |
3475 | (setq next win) | |
3476 | (while (progn (setq next (next-window next)) | |
3477 | (window-fixed-size-p next))) | |
3478 | ;; (assert (eq next (or (cadr (member win wins)) (car wins)))) | |
3479 | (let* ((horiz | |
3480 | (< (car (window-edges win)) (car (window-edges next)))) | |
3481 | (areadiff (/ (- (* (window-height next) (window-width next) | |
3482 | (buffer-local-value 'window-area-factor | |
3483 | (window-buffer next))) | |
3484 | (* (window-height win) (window-width win) | |
3485 | (buffer-local-value 'window-area-factor | |
3486 | (window-buffer win)))) | |
3487 | (max (buffer-local-value 'window-area-factor | |
3488 | (window-buffer win)) | |
3489 | (buffer-local-value 'window-area-factor | |
3490 | (window-buffer next))))) | |
3491 | (edgesize (if horiz | |
3492 | (+ (window-height win) (window-height next)) | |
3493 | (+ (window-width win) (window-width next)))) | |
3494 | (diff (/ areadiff edgesize))) | |
3495 | (when (zerop diff) | |
3496 | ;; Maybe diff is actually closer to 1 than to 0. | |
3497 | (setq diff (/ (* 3 areadiff) (* 2 edgesize)))) | |
3498 | (when (and (zerop diff) (not (zerop areadiff))) | |
3499 | (setq diff (/ (+ areadiff carry) edgesize)) | |
3500 | ;; Change things smoothly. | |
3501 | (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2)))) | |
3502 | (if (zerop diff) | |
3503 | ;; Make sure negligible differences don't accumulate to | |
3504 | ;; become significant. | |
3505 | (setq carry (+ carry areadiff)) | |
6198ccd0 | 3506 | ;; This used `adjust-window-trailing-edge' before and uses |
d615d6d2 | 3507 | ;; `window-resize' now. Error wrapping is still needed. |
6198ccd0 | 3508 | (balance-windows-area-adjust win diff horiz) |
3c448ab6 MR |
3509 | ;; (sit-for 0.5) |
3510 | (let ((change (cons win (window-edges win)))) | |
3511 | ;; If the same change has been seen already for this window, | |
3512 | ;; we're most likely in an endless loop, so don't count it as | |
3513 | ;; a change. | |
3514 | (unless (member change changelog) | |
3515 | (push change changelog) | |
3516 | (setq unchanged 0 carry 0))))))) | |
3517 | ;; We've now basically balanced all the windows. | |
3518 | ;; But there may be some minor off-by-one imbalance left over, | |
3519 | ;; so let's do some fine tuning. | |
3520 | ;; (bw-finetune wins) | |
3521 | ;; (message "Done in %d rounds" round) | |
3522 | )) | |
9d89fec7 MR |
3523 | |
3524 | ;;; Window states, how to get them and how to put them in a window. | |
3525 | (defsubst window-list-no-nils (&rest args) | |
3526 | "Like LIST but do not add nil elements of ARGS." | |
3527 | (delq nil (apply 'list args))) | |
3528 | ||
3529 | (defvar window-state-ignored-parameters '(quit-restore) | |
3530 | "List of window parameters ignored by `window-state-get'.") | |
3531 | ||
3532 | (defun window-state-get-1 (window &optional markers) | |
3533 | "Helper function for `window-state-get'." | |
3534 | (let* ((type | |
3535 | (cond | |
d68443dc MR |
3536 | ((window-top-child window) 'vc) |
3537 | ((window-left-child window) 'hc) | |
9d89fec7 MR |
3538 | (t 'leaf))) |
3539 | (buffer (window-buffer window)) | |
3540 | (selected (eq window (selected-window))) | |
3541 | (head | |
3542 | (window-list-no-nils | |
3543 | type | |
d68443dc | 3544 | (unless (window-next-sibling window) (cons 'last t)) |
9d89fec7 MR |
3545 | (cons 'total-height (window-total-size window)) |
3546 | (cons 'total-width (window-total-size window t)) | |
3547 | (cons 'normal-height (window-normal-size window)) | |
3548 | (cons 'normal-width (window-normal-size window t)) | |
3549 | (cons 'splits (window-splits window)) | |
3550 | (cons 'nest (window-nest window)) | |
3551 | (let (list) | |
3552 | (dolist (parameter (window-parameters window)) | |
3553 | (unless (memq (car parameter) | |
3554 | window-state-ignored-parameters) | |
3555 | (setq list (cons parameter list)))) | |
28545e04 MR |
3556 | (unless (window-parameter window 'clone-of) |
3557 | ;; Make a clone-of parameter. | |
3558 | (setq list (cons (cons 'clone-of window) list))) | |
9d89fec7 MR |
3559 | (when list |
3560 | (cons 'parameters list))) | |
3561 | (when buffer | |
3562 | ;; All buffer related things go in here - make the buffer | |
3563 | ;; current when retrieving `point' and `mark'. | |
3564 | (with-current-buffer (window-buffer window) | |
3565 | (let ((point (if selected (point) (window-point window))) | |
3566 | (start (window-start window)) | |
3567 | (mark (mark))) | |
3568 | (window-list-no-nils | |
3569 | 'buffer (buffer-name buffer) | |
3570 | (cons 'selected selected) | |
3571 | (when window-size-fixed (cons 'size-fixed window-size-fixed)) | |
3572 | (cons 'hscroll (window-hscroll window)) | |
3573 | (cons 'fringes (window-fringes window)) | |
3574 | (cons 'margins (window-margins window)) | |
3575 | (cons 'scroll-bars (window-scroll-bars window)) | |
3576 | (cons 'vscroll (window-vscroll window)) | |
3577 | (cons 'dedicated (window-dedicated-p window)) | |
3578 | (cons 'point (if markers (copy-marker point) point)) | |
3579 | (cons 'start (if markers (copy-marker start) start)) | |
3580 | (when mark | |
3581 | (cons 'mark (if markers (copy-marker mark) mark))))))))) | |
3582 | (tail | |
3583 | (when (memq type '(vc hc)) | |
3584 | (let (list) | |
3585 | (setq window (window-child window)) | |
3586 | (while window | |
3587 | (setq list (cons (window-state-get-1 window markers) list)) | |
3588 | (setq window (window-right window))) | |
3589 | (nreverse list))))) | |
3590 | (append head tail))) | |
3591 | ||
3592 | (defun window-state-get (&optional window markers) | |
3593 | "Return state of WINDOW as a Lisp object. | |
3594 | WINDOW can be any window and defaults to the root window of the | |
3595 | selected frame. | |
3596 | ||
3597 | Optional argument MARKERS non-nil means use markers for sampling | |
3598 | positions like `window-point' or `window-start'. MARKERS should | |
3599 | be non-nil only if the value is used for putting the state back | |
3600 | in the same session (note that markers slow down processing). | |
3601 | ||
3602 | The return value can be used as argument for `window-state-put' | |
3603 | to put the state recorded here into an arbitrary window. The | |
3604 | value can be also stored on disk and read back in a new session." | |
3605 | (setq window | |
3606 | (if window | |
3607 | (if (window-any-p window) | |
3608 | window | |
3609 | (error "%s is not a live or internal window" window)) | |
3610 | (frame-root-window))) | |
3611 | ;; The return value is a cons whose car specifies some constraints on | |
3612 | ;; the size of WINDOW. The cdr lists the states of the subwindows of | |
3613 | ;; WINDOW. | |
3614 | (cons | |
3615 | ;; Frame related things would go into a function, say `frame-state', | |
3616 | ;; calling `window-state-get' to insert the frame's root window. | |
3617 | (window-list-no-nils | |
3618 | (cons 'min-height (window-min-size window)) | |
3619 | (cons 'min-width (window-min-size window t)) | |
3620 | (cons 'min-height-ignore (window-min-size window nil t)) | |
3621 | (cons 'min-width-ignore (window-min-size window t t)) | |
3622 | (cons 'min-height-safe (window-min-size window nil 'safe)) | |
3623 | (cons 'min-width-safe (window-min-size window t 'safe)) | |
3624 | ;; These are probably not needed. | |
3625 | (when (window-size-fixed-p window) (cons 'fixed-height t)) | |
3626 | (when (window-size-fixed-p window t) (cons 'fixed-width t))) | |
3627 | (window-state-get-1 window markers))) | |
3628 | ||
3629 | (defvar window-state-put-list nil | |
3630 | "Helper variable for `window-state-put'.") | |
3631 | ||
3632 | (defun window-state-put-1 (state &optional window ignore totals) | |
3633 | "Helper function for `window-state-put'." | |
3634 | (let ((type (car state))) | |
3635 | (setq state (cdr state)) | |
3636 | (cond | |
3637 | ((eq type 'leaf) | |
3638 | ;; For a leaf window just add unprocessed entries to | |
3639 | ;; `window-state-put-list'. | |
3640 | (setq window-state-put-list | |
3641 | (cons (cons window state) window-state-put-list))) | |
3642 | ((memq type '(vc hc)) | |
3643 | (let* ((horizontal (eq type 'hc)) | |
3644 | (total (window-total-size window horizontal)) | |
3645 | (first t) | |
3646 | size new) | |
3647 | (dolist (item state) | |
3648 | ;; Find the next child window. WINDOW always points to the | |
3649 | ;; real window that we want to fill with what we find here. | |
3650 | (when (memq (car item) '(leaf vc hc)) | |
3651 | (if (assq 'last item) | |
3652 | ;; The last child window. Below `window-state-put-1' | |
3653 | ;; will put into it whatever ITEM has in store. | |
3654 | (setq new nil) | |
3655 | ;; Not the last child window, prepare for splitting | |
3656 | ;; WINDOW. SIZE is the new (and final) size of the old | |
3657 | ;; window. | |
3658 | (setq size | |
3659 | (if totals | |
3660 | ;; Use total size. | |
3661 | (cdr (assq (if horizontal 'total-width 'total-height) item)) | |
3662 | ;; Use normalized size and round. | |
3663 | (round (* total | |
3664 | (cdr (assq | |
3665 | (if horizontal 'normal-width 'normal-height) | |
3666 | item)))))) | |
3667 | ||
3668 | ;; Use safe sizes, we try to resize later. | |
3669 | (setq size (max size (if horizontal | |
3670 | window-safe-min-height | |
3671 | window-safe-min-width))) | |
3672 | ||
3673 | (if (window-sizable-p window (- size) horizontal 'safe) | |
3674 | (let* ((window-nest (assq 'nest item))) | |
3675 | ;; We must inherit the nesting, otherwise we might mess | |
3676 | ;; up handling of atomic and side window. | |
3677 | (setq new (split-window window size horizontal))) | |
3678 | ;; Give up if we can't resize window down to safe sizes. | |
3679 | (error "Cannot resize window %s" window)) | |
3680 | ||
3681 | (when first | |
3682 | (setq first nil) | |
3683 | ;; When creating the first child window add for parent | |
3684 | ;; unprocessed entries to `window-state-put-list'. | |
3685 | (setq window-state-put-list | |
3686 | (cons (cons (window-parent window) state) | |
3687 | window-state-put-list)))) | |
3688 | ||
3689 | ;; Now process the current window (either the one we've just | |
3690 | ;; split or the last child of its parent). | |
3691 | (window-state-put-1 item window ignore totals) | |
3692 | ;; Continue with the last window split off. | |
3693 | (setq window new)))))))) | |
3694 | ||
3695 | (defun window-state-put-2 (ignore) | |
3696 | "Helper function for `window-state-put'." | |
3697 | (dolist (item window-state-put-list) | |
3698 | (let ((window (car item)) | |
9d89fec7 MR |
3699 | (splits (cdr (assq 'splits item))) |
3700 | (nest (cdr (assq 'nest item))) | |
3701 | (parameters (cdr (assq 'parameters item))) | |
3702 | (state (cdr (assq 'buffer item)))) | |
9d89fec7 MR |
3703 | (when splits (set-window-splits window splits)) |
3704 | (when nest (set-window-nest window nest)) | |
3705 | ;; Process parameters. | |
3706 | (when parameters | |
3707 | (dolist (parameter parameters) | |
3708 | (set-window-parameter window (car parameter) (cdr parameter)))) | |
3709 | ;; Process buffer related state. | |
3710 | (when state | |
3711 | ;; We don't want to raise an error here so we create a buffer if | |
3712 | ;; there's none. | |
3713 | (set-window-buffer window (get-buffer-create (car state))) | |
3714 | (with-current-buffer (window-buffer window) | |
3715 | (set-window-hscroll window (cdr (assq 'hscroll state))) | |
3716 | (apply 'set-window-fringes | |
3717 | (cons window (cdr (assq 'fringes state)))) | |
3718 | (let ((margins (cdr (assq 'margins state)))) | |
3719 | (set-window-margins window (car margins) (cdr margins))) | |
3720 | (let ((scroll-bars (cdr (assq 'scroll-bars state)))) | |
3721 | (set-window-scroll-bars | |
3722 | window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars))) | |
3723 | (set-window-vscroll window (cdr (assq 'vscroll state))) | |
3724 | ;; Adjust vertically. | |
3725 | (if (memq window-size-fixed '(t height)) | |
3726 | ;; A fixed height window, try to restore the original size. | |
3727 | (let ((delta (- (cdr (assq 'total-height item)) | |
3728 | (window-total-height window))) | |
3729 | window-size-fixed) | |
3730 | (when (window-resizable-p window delta) | |
d615d6d2 | 3731 | (window-resize window delta))) |
9d89fec7 MR |
3732 | ;; Else check whether the window is not high enough. |
3733 | (let* ((min-size (window-min-size window nil ignore)) | |
3734 | (delta (- min-size (window-total-size window)))) | |
3735 | (when (and (> delta 0) | |
3736 | (window-resizable-p window delta nil ignore)) | |
d615d6d2 | 3737 | (window-resize window delta nil ignore)))) |
9d89fec7 MR |
3738 | ;; Adjust horizontally. |
3739 | (if (memq window-size-fixed '(t width)) | |
3740 | ;; A fixed width window, try to restore the original size. | |
3741 | (let ((delta (- (cdr (assq 'total-width item)) | |
3742 | (window-total-width window))) | |
3743 | window-size-fixed) | |
3744 | (when (window-resizable-p window delta) | |
d615d6d2 | 3745 | (window-resize window delta))) |
9d89fec7 MR |
3746 | ;; Else check whether the window is not wide enough. |
3747 | (let* ((min-size (window-min-size window t ignore)) | |
3748 | (delta (- min-size (window-total-size window t)))) | |
3749 | (when (and (> delta 0) | |
3750 | (window-resizable-p window delta t ignore)) | |
d615d6d2 | 3751 | (window-resize window delta t ignore)))) |
9d89fec7 MR |
3752 | ;; Set dedicated status. |
3753 | (set-window-dedicated-p window (cdr (assq 'dedicated state))) | |
3754 | ;; Install positions (maybe we should do this after all windows | |
3755 | ;; have been created and sized). | |
3756 | (ignore-errors | |
3757 | (set-window-start window (cdr (assq 'start state))) | |
3758 | (set-window-point window (cdr (assq 'point state))) | |
3759 | ;; I'm not sure whether we should set the mark here, but maybe | |
3760 | ;; it can be used. | |
3761 | (let ((mark (cdr (assq 'mark state)))) | |
3762 | (when mark (set-mark mark)))) | |
3763 | ;; Select window if it's the selected one. | |
3764 | (when (cdr (assq 'selected state)) | |
3765 | (select-window window))))))) | |
3766 | ||
3767 | (defun window-state-put (state &optional window ignore) | |
3768 | "Put window state STATE into WINDOW. | |
3769 | STATE should be the state of a window returned by an earlier | |
3770 | invocation of `window-state-get'. Optional argument WINDOW must | |
3771 | specify a live window and defaults to the selected one. | |
3772 | ||
3773 | Optional argument IGNORE non-nil means ignore minimum window | |
3774 | sizes and fixed size restrictions. IGNORE equal `safe' means | |
3775 | subwindows can get as small as `window-safe-min-height' and | |
3776 | `window-safe-min-width'." | |
5386012d | 3777 | (setq window (window-normalize-live-window window)) |
9d89fec7 MR |
3778 | (let* ((frame (window-frame window)) |
3779 | (head (car state)) | |
3780 | ;; We check here (1) whether the total sizes of root window of | |
3781 | ;; STATE and that of WINDOW are equal so we can avoid | |
3782 | ;; calculating new sizes, and (2) if we do have to resize | |
3783 | ;; whether we can do so without violating size restrictions. | |
3784 | (totals | |
3785 | (and (= (window-total-size window) | |
3786 | (cdr (assq 'total-height state))) | |
3787 | (= (window-total-size window t) | |
3788 | (cdr (assq 'total-width state))))) | |
3789 | (min-height (cdr (assq 'min-height head))) | |
3790 | (min-width (cdr (assq 'min-width head))) | |
3791 | window-splits selected) | |
3792 | (if (and (not totals) | |
3793 | (or (> min-height (window-total-size window)) | |
3794 | (> min-width (window-total-size window t))) | |
3795 | (or (not ignore) | |
3796 | (and (setq min-height | |
3797 | (cdr (assq 'min-height-ignore head))) | |
3798 | (setq min-width | |
3799 | (cdr (assq 'min-width-ignore head))) | |
3800 | (or (> min-height (window-total-size window)) | |
3801 | (> min-width (window-total-size window t))) | |
3802 | (or (not (eq ignore 'safe)) | |
3803 | (and (setq min-height | |
3804 | (cdr (assq 'min-height-safe head))) | |
3805 | (setq min-width | |
3806 | (cdr (assq 'min-width-safe head))) | |
3807 | (or (> min-height | |
3808 | (window-total-size window)) | |
3809 | (> min-width | |
3810 | (window-total-size window t)))))))) | |
3811 | ;; The check above might not catch all errors due to rounding | |
3812 | ;; issues - so IGNORE equal 'safe might not always produce the | |
3813 | ;; minimum possible state. But such configurations hardly make | |
3814 | ;; sense anyway. | |
3815 | (error "Window %s too small to accomodate state" window) | |
3816 | (setq state (cdr state)) | |
3817 | (setq window-state-put-list nil) | |
3818 | ;; Work on the windows of a temporary buffer to make sure that | |
3819 | ;; splitting proceeds regardless of any buffer local values of | |
3820 | ;; `window-size-fixed'. Release that buffer after the buffers of | |
3821 | ;; all live windows have been set by `window-state-put-2'. | |
3822 | (with-temp-buffer | |
3823 | (set-window-buffer window (current-buffer)) | |
3824 | (window-state-put-1 state window nil totals) | |
3825 | (window-state-put-2 ignore)) | |
3826 | (window-check frame)))) | |
9481c002 | 3827 | \f |
9b9c9e3a | 3828 | ;;; Displaying buffers. |
9481c002 MR |
3829 | (defconst display-buffer-default-specifiers |
3830 | '((reuse-window nil same visible) | |
3831 | (pop-up-window (largest . nil) (lru . nil)) | |
be3fb2b8 MR |
3832 | (pop-up-window-min-height . 40) |
3833 | (pop-up-window-min-width . 80) | |
3834 | (reuse-window other nil nil) | |
9481c002 | 3835 | (reuse-window nil other visible) |
be3fb2b8 | 3836 | (reuse-window nil nil t) |
9481c002 MR |
3837 | (reuse-window-even-sizes . t)) |
3838 | "Buffer display default specifiers. | |
3839 | The value specified here is used when no other specifiers have | |
3840 | been specified by the user or the application. Consult the | |
3841 | documentation of `display-buffer-alist' for a description of | |
3842 | buffer display specifiers.") | |
3843 | ||
3844 | (defconst display-buffer-macro-specifiers | |
3845 | '((same-window | |
3846 | ;; Use the same window. | |
3847 | (reuse-window same nil nil)) | |
3848 | (same-frame | |
3849 | ;; Avoid other frames. | |
3850 | (reuse-window nil same nil) | |
3851 | (pop-up-window (largest . nil) (lru . nil)) | |
3852 | (reuse-window nil other nil)) | |
25aef8b8 MR |
3853 | ;; (other-window |
3854 | ;; ;; Avoid selected window. | |
3855 | ;; (reuse-window other same visible) | |
3856 | ;; (pop-up-window (largest . nil) (lru . nil)) | |
3857 | ;; (reuse-window other other visible)) | |
9481c002 MR |
3858 | (same-frame-other-window |
3859 | ;; Avoid other frames and selected window. | |
3860 | (reuse-window other same nil) | |
3861 | (pop-up-window (largest . nil) (lru . nil)) | |
3862 | (reuse-window other other nil)) | |
3863 | (other-frame | |
3864 | ;; Avoid selected frame. | |
3865 | (reuse-window nil same other) | |
3866 | (pop-up-frame) | |
3867 | (reuse-window nil other other)) | |
3868 | (default | |
3869 | ;; The default specifiers. | |
3870 | display-buffer-default-specifiers)) | |
3871 | "Buffer display macro specifiers.") | |
3872 | ||
3873 | (defcustom display-buffer-alist | |
3874 | '((((regexp . ".*")) | |
327c8fb1 MR |
3875 | ;; Reuse window showing same buffer on same frame. |
3876 | reuse-window (reuse-window nil same nil) | |
25aef8b8 | 3877 | ;; Pop up window. |
9481c002 | 3878 | pop-up-window |
25aef8b8 | 3879 | ;; Split largest or lru window. |
9481c002 | 3880 | (pop-up-window (largest . nil) (lru . nil)) |
25aef8b8 MR |
3881 | (pop-up-window-min-height . 40) ; split-height-threshold / 2 |
3882 | (pop-up-window-min-width . 80) ; split-width-threshold / 2 | |
3883 | ;; Reuse any but selected window on same frame. | |
3884 | reuse-window (reuse-window other nil nil) | |
9481c002 MR |
3885 | (reuse-window-even-sizes . t))) |
3886 | "List associating buffer identifiers with display specifiers. | |
3887 | The car of each element of this list is built from a set of cons | |
3888 | cells called buffer identifiers. `display-buffer' shows a buffer | |
3889 | according to the display specifiers in the element's cdr | |
3890 | \(elements are true lists) if at least one of the identifiers | |
3891 | matches the first or third argument of `display-buffer'. Such a | |
3892 | match occurs in one of the following three cases: | |
3893 | ||
3894 | - The car of the buffer identifier is the symbol `name' and its | |
3895 | cdr is a string equalling the name of the buffer specified by | |
3896 | the first \(BUFFER-OR-NAME) argument of `display-buffer'. | |
3897 | ||
3898 | - The car is the symbol `regexp' and the cdr is a regular | |
3899 | expression matching the name of the buffer specified by the | |
3900 | first \(BUFFER-OR-NAME) argument of `display-buffer'. | |
3901 | ||
3902 | - The car is the symbol `label' and the cdr is a symbol equalling | |
3903 | the third \(LABEL) argument of `display-buffer'. | |
3904 | ||
3905 | Display specifiers are either symbols, cons cells, or lists. | |
3906 | Five specifiers have been reserved to indicate the basic method | |
3907 | for displaying the buffer: `reuse-window', `pop-up-window', | |
5386012d | 3908 | `pop-up-frame', `use-side-window', and `function'. |
9481c002 MR |
3909 | |
3910 | A list whose car is the symbol `reuse-window' indicates that an | |
3911 | existing window shall be reused for displaying the buffer. The | |
3912 | second element of this list specifies the window to use and can | |
3913 | be one of the following symbols: | |
3914 | ||
3915 | nil stands for any window. | |
3916 | ||
3917 | `same' stands for the selected window. | |
3918 | ||
3919 | `other' stands for any but the selected window. | |
3920 | ||
3921 | The third element specifies whether the buffer shown in a window | |
3922 | that shall be reused must be the same buffer that shall be | |
3923 | displayed or another buffer and can be one of the following: | |
3924 | ||
3925 | nil means to not care about the window's buffer. | |
3926 | ||
3927 | `same' means the window must show the buffer already. | |
3928 | ||
3929 | `other' means the window must not show the buffer yet. | |
3930 | ||
3931 | The fourth element specifies the set of frames to search for a | |
3932 | suitable window and can be one of the following: | |
3933 | ||
3934 | nil to reuse a window on the selected frame. | |
3935 | ||
3936 | `visible' to search visible frames on the current terminal. | |
3937 | ||
3938 | `other' stands for any visible frame but the selected one. | |
3939 | ||
3940 | 0 \(the number zero) to search visible and iconified frames on | |
3941 | the current terminal. | |
3942 | ||
3943 | t to search arbitrary frames including invisible ones. | |
3944 | ||
3945 | If more than one window fits the constraints imposed by these | |
3946 | elements, the least recently used candidate is chosen. A side | |
3947 | window is never reused unless it already shows the buffer. | |
3948 | ||
3949 | The following two specifiers are useful when the method equals | |
3950 | `reuse-window': | |
3951 | ||
3952 | - A cons cell whose car is the symbol `reuse-window-even-sizes' | |
3953 | and whose cdr is non-nil means to even out the sizes of a | |
3954 | reused window and the selected window provided they (1) appear | |
3955 | adjacent to each other and (2) the selected window is larger | |
3956 | than the window chosen. If the cdr is nil, this means that the | |
3957 | window sizes are left alone. | |
3958 | ||
3959 | - A cons cell whose car is the symbol `reuse-window-dedicated' | |
3960 | and whose cdr is non-nil means that a window can be reused even | |
3961 | if it's weakly dedicated to its buffer. If the cdr is t, a | |
3962 | strongly dedicated window can be reused to show the buffer. | |
3963 | Any other non-nil value means only weakly dedicated windows can | |
3964 | be reused. If the cdr is nil, dedicated windows are not | |
3965 | reused. | |
3966 | ||
3967 | This specifier should be used in emergency cases only since | |
3968 | windows are usually made dedicated in order to prevent | |
3969 | `display-buffer' from reusing them. | |
3970 | ||
3971 | A list whose car is the symbol `pop-up-window' and whose cdr is | |
3972 | built from cons cells representing window/side tuples indicates | |
3973 | that a new window shall be made for displaying the buffer on the | |
3974 | selected frame. | |
3975 | ||
3976 | Window/side tuples are cons cells. The car of such a tuple | |
3977 | identifies the window that shall be split. Possible values are | |
3978 | `largest', `lru', `selected', and `root' to split the largest, | |
3979 | least recently used, selected or root window of the selected | |
3980 | frame. | |
3981 | ||
3982 | If the frame has side windows, these values do allow to split | |
3983 | only the selected frame's main window or one of its subwindows. | |
3984 | Setting the car to one of `left', `top', `right' and `bottom' | |
3985 | splits the corresponding side window, provided such a window | |
3986 | exists. | |
3987 | ||
3988 | The cdr of each pair specifies on which side of the window to | |
3989 | split the new window shall appear and can be one of `below', | |
3990 | `right', `above', or `left' with the obvious meanings. If the | |
3991 | cdr is nil, the window is split in a fashion suitable for its | |
3992 | current dimensions. If the cdr specifies a function, that | |
3993 | function is called with one argument - the window to split. The | |
3994 | function is supposed to split that window and return the new | |
3995 | window. | |
3996 | ||
3997 | `display-buffer' scans these tuples until it can either produce a | |
3998 | suitable window or fails. The default value for | |
3999 | `display-buffer-alist' contains the tuples \(largest . nil) and | |
4000 | \(lru . nil) in order to split the largest window first and, if | |
4001 | that fails, the least recently used one. | |
4002 | ||
4003 | The following specifiers are useful if the method specifier is | |
4004 | `pop-up-window'. | |
4005 | ||
4006 | - A cons cell whose car is the symbol `pop-up-window-min-height' | |
4007 | specifiies the minimum height of the new window. If the cdr is | |
4008 | an integer number, it specifies the minimum number of lines of | |
4009 | the window. A floating point number gives the minimum fraction | |
4010 | of the window height with respect to the height of the frame's | |
4011 | root window. A new window is created only if it can be made at | |
4012 | least as high as specified by the number. If the cdr is nil, | |
4013 | this means to use the value of `window-min-height'. | |
4014 | ||
4015 | - A cons cell whose car is the symbol `pop-up-window-min-width' | |
4016 | specifies the minimum width of the new window. If the cdr is | |
4017 | an integer number, it specifies the minimum number of columns | |
4018 | of the window. A floating point number gives the minimum | |
4019 | fraction of the window width with respect to the width of the | |
4020 | frame's root window. A new window is created only if it can be | |
4021 | made at least as wide as specified by the number. If the cdr | |
4022 | is nil, this means to use the value of `window-min-width'. | |
4023 | ||
4024 | - A cons cell whose car is `pop-up-window-set-height' with | |
4025 | the following interpretations for the cdr: | |
4026 | ||
4027 | - nil means leave the height of the new window alone. | |
4028 | ||
4029 | - A number specifies the desired height of the new window. An | |
4030 | integer number specifies the number of lines of the window. | |
4031 | A floating point number gives the fraction of the window | |
4032 | height with respect to the height of the frame's root window. | |
4033 | ||
4034 | - If the cdr specifies a function, that function is called with | |
4035 | one argument - the new window. The function is supposed to | |
4036 | adjust the height of the window; its return value is ignored. | |
4037 | Suitable functions are `shrink-window-if-larger-than-buffer' | |
4038 | and `fit-window-to-buffer'. | |
4039 | ||
4040 | - A cons cell whose car equals `pop-up-window-set-width' with | |
4041 | the following interpretations for the cdr: | |
4042 | ||
4043 | - nil means leave the width of the new window alone. | |
4044 | ||
4045 | - A number specifies the desired width of the new window. An | |
4046 | integer number specifies the number of columns of the window. | |
4047 | A floating point number gives the fraction of the window | |
4048 | width with respect to the width of the frame's root window. | |
4049 | ||
4050 | - If the cdr specifies a function, that function is called with | |
4051 | one argument - the new window. The function is supposed to | |
4052 | adjust the width of the window; its return value is ignored. | |
4053 | ||
4054 | Observe that specifying `pop-up-window-set-height' or | |
4055 | `pop-up-window-set-width' may override restrictions given by | |
4056 | the `pop-up-window-min-height' and `pop-up-window-min-width' | |
4057 | specifiers. | |
4058 | ||
4059 | - A cons cell whose car is `pop-up-window-split-unsplittable' and | |
4060 | whose cdr is non-nil allows to make a new window on an | |
4061 | unsplittable frame. If the cdr is nil, unsplittable frames are | |
4062 | not split. This specifier should be used in special cases only | |
4063 | since frames are usually made unsplittable in order to prevent | |
4064 | `display-buffer' from splitting them. | |
4065 | ||
4066 | A list whose car is the symbol `pop-up-frame' specifies that a | |
4067 | new frame shall be made for displaying the buffer. The second | |
4068 | element, if non-nil, allows popping up a new frame on graphic | |
4069 | displays only. | |
4070 | ||
4071 | The following specifiers are useful if the method specifier is | |
4072 | `pop-up-frame'. | |
4073 | ||
4074 | - A list whose car is the symbol `pop-up-frame-function' together | |
4075 | with a valid function as cdr specifies the function for | |
4076 | creating a new frame. If the cdr is nil, the default function | |
4077 | `make-frame' is called. The function is called with the | |
4078 | parameters and values provided by the specifier described next. | |
4079 | ||
4080 | - A list whose car is the symbol `pop-up-frame-alist' followed by | |
4081 | an arbitrary number of frame parameter/value tuples, each given | |
4082 | as a cons cell, specifies the parameters passed to the pop-up | |
4083 | frame function. | |
4084 | ||
4085 | A list of three elements whose car is the symbol | |
4086 | `use-side-window' specifies that the buffer shall be displayed in | |
4087 | a side window of the selected frame. The second element denotes | |
4088 | the side of the frame where the window appears or shall be made. | |
4089 | The third element denotes the slot used by the window. If a side | |
4090 | window with the specified slot exists already, that window is | |
4091 | reused. If no such window exists it is created. | |
4092 | ||
4093 | The following specifiers are useful in connection with the | |
4094 | `use-side-window' method specifier: `reuse-window-dedicated', | |
4095 | `pop-up-window-min-height', `pop-up-window-min-width', | |
4096 | `pop-up-window-set-height' and `pop-up-window-set-width'. | |
4097 | ||
5386012d | 4098 | A list whose car is the symbol `function' specifies that the |
9481c002 MR |
4099 | function specified in the second element of the list is |
4100 | responsible for displaying the buffer. `display-buffer' calls | |
4101 | this function with the buffer as first argument and the remaining | |
28545e04 | 4102 | elements of the list as the second. |
9481c002 MR |
4103 | |
4104 | The function should choose or create a window, display the buffer | |
4105 | in it, and return the window. It is also responsible for giving | |
4106 | the variable `display-buffer-window' and the `quit-restore' | |
4107 | parameter of the window used a meaningful value. | |
4108 | ||
4109 | Within the body of this function avoid calling `display-buffer' | |
4110 | with the same buffer as argument since this may lead to endless | |
4111 | recursion. | |
4112 | ||
4113 | Instead of supplying basic method specifiers, it's sometimes more | |
4114 | convenient to use macro specifiers. They provide some commonly | |
4115 | used display methods but do not support the fine control provided | |
4116 | by the basic method specifiers. Macro specifiers are symbols. | |
4117 | The following macro specifiers are provided: | |
4118 | ||
4119 | `same-window' to display the buffer in the selected window. | |
4120 | ||
4121 | `same-frame' to display the buffer on the selected frame. | |
4122 | ||
4123 | `other-window' to display the buffer in any window but the | |
4124 | selected one. | |
4125 | ||
4126 | `same-frame-other-window' as `other-window' but stay on the | |
4127 | selected frame. | |
4128 | ||
4129 | `other-frame' to display the buffer on another visible | |
4130 | frame. | |
4131 | ||
4132 | `default' to use the default value of `display-buffer-alist'. | |
4133 | ||
4134 | One specifier is useful with any method specifier: A list whose | |
4135 | car is the symbol `dedicate' and whose cdr is non-nil will | |
4136 | dedicate the window to its buffer. The following values are | |
4137 | supported: | |
4138 | ||
4139 | - nil to not dedicate the window to the buffer. | |
4140 | ||
4141 | - `weak' to weakly dedicate the window to the buffer. | |
4142 | ||
4143 | - t to strongly dedicate the window to the buffer. | |
4144 | ||
9be74fbb MR |
4145 | A cons cell whose car is `other-window-means-other-frame' and |
4146 | whose cdr is non-nil means that you want calls of | |
4147 | `display-buffer' with the second argument t or the symbol | |
4148 | `other-window' to display the buffer in another frame. This | |
4149 | means, for example, that you prefer functions like | |
4150 | `find-file-other-window' or `switch-to-buffer-other-window' to | |
4151 | make a new frame instead of a new window on the selected frame. | |
4152 | ||
9481c002 MR |
4153 | Usually, applications are free to override the specifiers of |
4154 | `display-buffer-alist' by passing their own specifiers as second | |
4155 | argument of `display-buffer'. For every `display-buffer-alist' | |
4156 | entry you can, however, add a cons cell whose car is the symbol | |
4157 | `override' and whose cdr is non-nil, to explicitly override any | |
4158 | value supplied by the application. | |
4159 | ||
4160 | Overriding specifiers supplied by the calling application is, in | |
4161 | general, not advisable. It permits, for example, to change the | |
4162 | semantics of a function like `display-buffer-other-window' by | |
4163 | using the location specifiers `same-window' or `other-frame'." | |
4164 | :risky t | |
4165 | :type | |
4166 | '(repeat | |
4167 | :offset 9 | |
4168 | ;; Associations of buffer identifiers and display specifiers. | |
4169 | (list | |
4170 | :format "%v" | |
4171 | ;; Buffer identifiers. | |
4172 | (repeat | |
4173 | :tag "Buffer identifiers" | |
4174 | (choice | |
4175 | :tag "Identifier" | |
4176 | :format "%[%t%] %v" :size 15 | |
4177 | (cons | |
4178 | :tag "Name" | |
4179 | :format "%v" | |
4180 | :help-echo "A buffer name." | |
4181 | (const :format "" name) | |
4182 | (string :format "Name: %v\n" :size 32)) | |
4183 | (cons | |
4184 | :tag "Regexp" | |
4185 | :format "%v" | |
4186 | :help-echo "A regular expression matching buffer names." | |
4187 | (const :format "" regexp) | |
4188 | (string :format "Regexp: %v\n" :size 32)) | |
4189 | (cons | |
4190 | :tag "Label" | |
4191 | :format "%v" | |
4192 | :help-echo "A symbol equalling the buffer display label." | |
d615d6d2 | 4193 | (const :format "" label) |
9481c002 MR |
4194 | (symbol :format "Label: %v\n" :size 32)))) |
4195 | ||
4196 | ;; Display specifiers. | |
4197 | (repeat | |
4198 | :offset 9 | |
4199 | :tag "Display specifiers" | |
4200 | :inline t | |
4201 | (list | |
4202 | :inline t | |
4203 | :format "%v" | |
4204 | (choice | |
4205 | :tag "Method" | |
4206 | :value (reuse-window | |
4207 | (reuse-window nil same nil) | |
4208 | (reuse-window-even-sizes . t)) | |
4209 | :inline t | |
4210 | :help-echo "Method for displaying the buffer." | |
4211 | :format "%[Method%] %v" :size 15 | |
4212 | ||
4213 | ;; Reuse window specifiers. | |
4214 | (list | |
4215 | :tag "Reuse window" | |
4216 | :value (reuse-window | |
4217 | (reuse-window nil same nil) | |
4218 | (reuse-window-even-sizes . t)) | |
4219 | :format "%t\n%v" | |
4220 | :inline t | |
4221 | ;; For customization purposes only. | |
4222 | (const :format "" reuse-window) | |
4223 | (set | |
4224 | :format "%v" | |
4225 | :inline t | |
4226 | ;; The window to reuse. | |
4227 | (list | |
4228 | :format "%v\n" | |
4229 | (const :format "" reuse-window) | |
4230 | ;; The window type. | |
4231 | (choice | |
4232 | :tag "Window" | |
4233 | :help-echo "Window to reuse." | |
4234 | :value nil | |
4235 | :format "%[Window%] %v" :size 15 | |
5f45cca5 MR |
4236 | (const :tag "Any window" :format "%t" nil) |
4237 | (const :tag "Same window" :format "%t" same) | |
4238 | (const :tag "Other window" :format "%t" other)) | |
9481c002 MR |
4239 | ;; The window's buffer. |
4240 | (choice | |
4241 | :tag "Buffer" | |
4242 | :help-echo "Buffer shown by reused window." | |
4243 | :value t | |
4244 | :format " %[Buffer%] %v" :size 15 | |
4245 | (const :tag "Any buffer" :format "%t" nil) | |
4246 | (const :tag "Same buffer" :format "%t" same) | |
4247 | (const :tag "Other buffer" :format "%t" other)) | |
4248 | ;; The window's frame. | |
4249 | (choice | |
5f45cca5 | 4250 | :help-echo "Frames to search for a window to reuse." |
9481c002 MR |
4251 | :tag "Frame" |
4252 | :value nil | |
4253 | :format " %[Frame%] %v" :size 15 | |
5f45cca5 | 4254 | (const :tag "Same frame only" :format "%t" nil) |
9481c002 | 4255 | (const :tag "Visible frames" :format "%t" visible) |
5f45cca5 MR |
4256 | (const :tag "Any other visible frame" :format "%t" other) |
4257 | (const :tag "Visible and iconified frames" :format "%t" 0) | |
4258 | (const :tag "All frames" :format "%t" t))) | |
9481c002 MR |
4259 | ;; Whether window sizes should be evened out. |
4260 | (cons | |
4261 | :format "%v\n" | |
4262 | :tag "Even window sizes" | |
4263 | (const :format "" reuse-window-even-sizes) | |
4264 | (choice | |
4265 | :tag "Even window sizes" | |
4266 | :help-echo "Whether to even sizes of selected and reused window." | |
4267 | :value t | |
4268 | :format "%[Even window sizes%] %v" :size 15 | |
4269 | (const :tag "Off" :format "%t" nil) | |
4270 | (const :tag "Even window sizes" :format "%t" t))) | |
4271 | ;; Whether to reuse a dedicated window | |
4272 | (cons | |
4273 | :format "%v\n" | |
4274 | (const :format "" reuse-window-dedicated) | |
4275 | (choice | |
4276 | :tag "Reuse dedicated window" :value nil | |
4277 | :help-echo "Reuse a window even if it is dedicated to its buffer." | |
4278 | :format "%[Reuse dedicated window%] %v" :size 15 | |
4279 | (const :tag "Off" :format "%t" nil) | |
4280 | (const :tag "Reuse weakly dedicated windows" :format "%t" weak) | |
4281 | (const :tag "Reuse any dedicated window" :format "%t" t))))) | |
4282 | ||
4283 | ;; Pop-up window specifiers. | |
4284 | (list | |
4285 | :tag "Pop-up window" | |
4286 | :value (pop-up-window (pop-up-window (largest . nil) (lru . nil))) | |
4287 | :format "%t\n%v" | |
4288 | :inline t | |
4289 | (const :format "" pop-up-window) | |
4290 | (set | |
4291 | :format "%v" | |
4292 | :inline t | |
4293 | ;; Pop-up window list. | |
4294 | (list | |
4295 | :format "%v" | |
4296 | :value (pop-up-window (largest . nil) (lru . nil)) | |
4297 | (const :format "" pop-up-window) | |
4298 | (repeat | |
4299 | :tag "Window / Side tuples" | |
4300 | :inline t | |
4301 | (cons | |
4302 | :format "%v\n" | |
4303 | (choice | |
4304 | :tag "Window" | |
4305 | :help-echo "The window to split." | |
4306 | :value largest | |
4307 | :format "%[Window%] %v" | |
4308 | (const :tag "Largest" :format "%t" largest) | |
4309 | (const :tag "Least recently used" :format "%t" lru) | |
4310 | (const :tag "Selected" :format "%t" selected) | |
4311 | (const :tag "Root" :format "%t" root) | |
4312 | (const :tag "Left" :format "%t" left) | |
4313 | (const :tag "Top" :format "%t" top) | |
4314 | (const :tag "Right" :format "%t" right) | |
4315 | (const :tag "Bottom" :format "%t" bottom)) | |
4316 | (choice | |
4317 | :tag "Side" | |
4318 | :help-echo "The position of the new window with respect to the window to split." | |
4319 | :value nil | |
4320 | :format " %[Side%] %v" | |
4321 | (const :tag "Dynamic" :format "%t" nil) | |
4322 | (const :tag "Below" :format "%t" below) | |
4323 | (const :tag "Right" :format "%t" right) | |
4324 | (const :tag "Above" :format "%t" above) | |
4325 | (const :tag "Left" :format "%t" left) | |
4326 | (function | |
4327 | :tag "Function" :format "%v" :size 25))))) | |
4328 | ;; Minimum height of pop-up windows. | |
4329 | (cons | |
4330 | :format "%v\n" | |
4331 | (const :format "" pop-up-window-min-height) | |
4332 | (choice | |
4333 | :help-echo "Minimum height of popped-up window." | |
4334 | :format "%[Minimum height%] %v" | |
4335 | (const :tag "Default" :format "%t" :value nil) | |
4336 | (integer :tag "Number of lines" :value 12 :size 5) | |
4337 | (float :tag "Fraction of frame height" :value .25 :size 5))) | |
4338 | ;; Minimum width of pop-up windows. | |
4339 | (cons | |
4340 | :format "%v\n" | |
4341 | (const :format "" pop-up-window-min-width) | |
4342 | (choice | |
4343 | :help-echo "Minimum width of popped-up window." | |
4344 | :format "%[Minimum width%] %v" | |
4345 | (const :tag "Default" :format "%t" :value nil) | |
4346 | (integer :tag "Number of columns" :value 12 :size 5) | |
4347 | (float :tag "Fraction of frame width" :value .25 :size 5))) | |
4348 | ;; Desired height of pop-up windows. | |
4349 | (cons | |
4350 | :format "%v\n" | |
4351 | (const :format "" pop-up-window-set-height) | |
4352 | (choice | |
4353 | :help-echo "Desired height of popped-up window." | |
4354 | :format "%[Desired height%] %v" | |
4355 | (const :tag "Default" :format "%t" :value nil) | |
4356 | (integer :tag "Number of lines" :value 12 :size 5) | |
4357 | (float :tag "Fraction of frame height" :value .25 :size 5) | |
4358 | (function :tag "Function" :size 25))) | |
4359 | ;; Desired width of pop-up windows. | |
4360 | (cons | |
4361 | :format "%v\n" | |
4362 | (const :format "" pop-up-window-set-width) | |
4363 | (choice | |
4364 | :help-echo "Desired width of popped-up window." | |
4365 | :format "%[Desired width%] %v" | |
4366 | (const :tag "Default" :format "%t" :value nil) | |
4367 | (integer :tag "Number of column" :value 12 :size 5) | |
4368 | (float :tag "Fraction of frame width" :value .25 :size 5) | |
4369 | (function :tag "Function" :size 25))) | |
4370 | ;; Split unsplittable frames. | |
4371 | (cons | |
4372 | :format "%v\n" | |
4373 | (const :format "" pop-up-window-unsplittable) | |
4374 | (choice | |
4375 | :help-echo "Allow popping up a window on \"unsplittable\" frames." | |
4376 | :format "%[Split unsplittable frame%] %v" | |
4377 | (const :tag "Off" :format "%t" nil) | |
4378 | (const :tag "Allow" :format "%t" t))))) | |
4379 | ||
4380 | ;; Pop-up frame specifiers. | |
4381 | (list | |
4382 | :tag "Pop-up frame" | |
4383 | :value (pop-up-frame | |
8998d1b3 | 4384 | (pop-up-frame)) |
9481c002 MR |
4385 | :format "%t\n%v" |
4386 | :inline t | |
4387 | (const :format "" pop-up-frame) | |
4388 | (set | |
4389 | :format "%v" | |
4390 | :inline t | |
4391 | ;; Pop-up frame. | |
4392 | (list | |
4393 | :tag "Pop-up a new frame" | |
4394 | :value (pop-up-frame) | |
4395 | :format "%v" | |
4396 | (const :format "" pop-up-frame) | |
4397 | (choice | |
4398 | :tag "Pop-up a new frame" | |
4399 | :help-echo "Whether to pop-up a new frame on a display." | |
4400 | :format "%[Display%] %v\n" :size 15 | |
4401 | (const :tag "On any display" :format "%t" nil) | |
4402 | (const :tag "On graphic displays only" :format "%t" t))) | |
4403 | ;; Pop-up frame function. | |
4404 | (cons | |
4405 | :format "%v\n" | |
4406 | (const :format "" pop-up-frame-function) | |
4407 | (choice | |
4408 | :tag "Pop-up frame function" | |
4409 | :value nil | |
4410 | :help-echo "Function to use to pop-up a new frame." | |
4411 | :format "%[Function%] %v" :size 15 | |
4412 | (const :tag "Default" :format "%t" nil) | |
4413 | (function | |
4414 | :value make-frame | |
4415 | :format "%t: %v" | |
4416 | :size 25))) | |
4417 | ;; Pop-up frame alist. | |
4418 | (list | |
4419 | :format "%v" | |
4420 | (const :format "" pop-up-frame-alist) | |
4421 | (repeat | |
4422 | :tag "Parameter / Value tuples" | |
4423 | :inline t | |
4424 | (cons | |
4425 | :format "%v\n" | |
4426 | (symbol | |
4427 | :tag "Parameter" | |
4428 | :format "Parameter: %v" | |
4429 | :size 16) | |
4430 | (sexp | |
4431 | :tag "Value" | |
4432 | :format " Value: %v" | |
4433 | :size 8)))))) | |
4434 | ||
4435 | ;; Use side-window specifiers. | |
4436 | (list | |
4437 | :tag "Use side-window" | |
4438 | :value (use-side-window (use-side-window bottom 0)) | |
4439 | :format "%t\n%v" | |
4440 | :inline t | |
4441 | ;; For customization purposes only. | |
4442 | (const :format "" use-side-window) | |
4443 | (set | |
4444 | :format "%v" | |
4445 | :inline t | |
4446 | ;; Side and slot. | |
4447 | (list | |
4448 | :format "%v\n" | |
4449 | :value (use-side-window bottom 0) | |
4450 | (const :format "" use-side-window) | |
4451 | ;; The side. | |
4452 | (choice | |
4453 | :tag "Side" | |
4454 | :help-echo "Side of frame." | |
4455 | :value bottom | |
4456 | :format "%[Side%] %v" :size 15 | |
4457 | (const :tag "Left" :format "%t" left) | |
4458 | (const :tag "Top" :format "%t" top) | |
4459 | (const :tag "Right" :format "%t" right) | |
4460 | (const :tag "Bottom" :format "%t" bottom)) | |
4461 | ;; The slot | |
4462 | (number | |
4463 | :tag "Slot" | |
4464 | :help-echo "The slot (an arbitrary number, where 0 stands for the center slot)." | |
4465 | :value 0 | |
4466 | :format " Slot: %v" :size 8)) | |
4467 | ;; Whether to reuse a dedicated side window | |
4468 | (cons | |
4469 | :format "%v\n" | |
4470 | (const :format "" reuse-window-dedicated) | |
4471 | (choice | |
4472 | :tag "Reuse dedicated side window" :value nil | |
4473 | :help-echo "Reuse a side window even if it is dedicated to its buffer." | |
4474 | :format "%[Reuse dedicated side window%] %v" :size 15 | |
4475 | (const :tag "Off" :format "%t" nil) | |
4476 | (const :tag "Reuse weakly dedicated side windows" :format "%t" weak) | |
4477 | (const :tag "Reuse any dedicated side window" :format "%t" t))) | |
4478 | ;; Minimum height of pop-up side windows. | |
4479 | (cons | |
4480 | :format "%v\n" | |
4481 | (const :format "" pop-up-window-min-height) | |
4482 | (choice | |
4483 | :help-echo "Minimum height of popped-up side window." | |
4484 | :format "%[Minimum height%] %v" | |
4485 | (const :tag "Default" :format "%t" :value nil) | |
4486 | (integer :tag "Number of lines" :value 12 :size 5) | |
4487 | (float :tag "Fraction of frame height" :value .25 :size 5))) | |
4488 | ;; Minimum width of pop-up windows. | |
4489 | (cons | |
4490 | :format "%v\n" | |
4491 | (const :format "" pop-up-window-min-width) | |
4492 | (choice | |
4493 | :help-echo "Minimum width of popped-up side window." | |
4494 | :format "%[Minimum width%] %v" | |
4495 | (const :tag "Default" :format "%t" :value nil) | |
4496 | (integer :tag "Number of columns" :value 12 :size 5) | |
4497 | (float :tag "Fraction of frame width" :value .25 :size 5))) | |
4498 | ;; Desired height of pop-up windows. | |
4499 | (cons | |
4500 | :format "%v\n" | |
4501 | (const :format "" pop-up-window-set-height) | |
4502 | (choice | |
4503 | :help-echo "Desired height of popped-up side window." | |
4504 | :format "%[Desired height%] %v" | |
4505 | (const :tag "Default" :format "%t" :value nil) | |
4506 | (integer :tag "Number of lines" :value 12 :size 5) | |
4507 | (float :tag "Fraction of frame height" :value .25 :size 5) | |
4508 | (function :tag "Function" :size 25))) | |
4509 | ;; Desired width of pop-up windows. | |
4510 | (cons | |
4511 | :format "%v\n" | |
4512 | (const :format "" pop-up-window-set-width) | |
4513 | (choice | |
4514 | :help-echo "Desired width of popped-up side window." | |
4515 | :format "%[Desired width%] %v" | |
4516 | (const :tag "Default" :format "%t" :value nil) | |
4517 | (integer :tag "Number of column" :value 12 :size 5) | |
4518 | (float :tag "Fraction of frame width" :value .25 :size 5) | |
4519 | (function :tag "Function" :size 25))))) | |
4520 | ||
4521 | ;; Function with argument specifiers. | |
4522 | (list | |
4523 | :tag "Function with arguments" | |
5386012d | 4524 | :value (function (function 'ignore)) |
9481c002 MR |
4525 | :format "%t\n%v" |
4526 | :inline t | |
4527 | ;; For customization purposes only. | |
5386012d | 4528 | (const :format "" function) |
9481c002 MR |
4529 | (set |
4530 | :format "%v" | |
4531 | :inline t | |
4532 | (list | |
4533 | :format "%v" | |
5386012d MR |
4534 | :value (function 'ignore) |
4535 | (const :format "" function) | |
9481c002 MR |
4536 | (function :tag "Function" :format "%t: %v\n" :size 25) |
4537 | (list | |
4538 | :format "%v" | |
4539 | (repeat | |
4540 | :tag "Arguments" | |
4541 | :inline t | |
4542 | (sexp | |
4543 | :format "%v\n" | |
4544 | :size 16)))))) | |
4545 | ||
4546 | ;; Macro specifiers. | |
4547 | (list | |
355f2e07 MR |
4548 | :tag "Same window" |
4549 | :format "%t%v" | |
4550 | :inline t | |
4551 | (const :format "\n" same-window)) | |
4552 | (list | |
4553 | :tag "Same frame" | |
9481c002 MR |
4554 | :format "%t%v" |
4555 | :inline t | |
4556 | (const :format "\n" same-frame)) | |
4557 | (list | |
4558 | :tag "Other window" | |
4559 | :format "%t%v" | |
4560 | :inline t | |
4561 | (const :format "\n" other-window)) | |
4562 | (list | |
4563 | :tag "Same frame other window" | |
4564 | :format "%t%v" | |
4565 | :inline t | |
4566 | (const :format "\n" same-frame-other-window)) | |
4567 | (list | |
355f2e07 | 4568 | :tag "Other frame" |
9481c002 MR |
4569 | :format "%t%v" |
4570 | :inline t | |
4571 | (const :format "\n" other-frame)) | |
4572 | (list | |
4573 | :tag "Default" | |
4574 | :format "%t%v" | |
4575 | :inline t | |
4576 | (const :format "\n" default))))) | |
4577 | ||
4578 | (set | |
4579 | :format "%v" | |
4580 | :inline t | |
4581 | ;; Dedicate window to buffer. | |
4582 | (cons | |
4583 | :format "%v" | |
4584 | (const :format "" dedicate) | |
4585 | (choice | |
4586 | :help-echo "Mark window as dedicated to its buffer." | |
4587 | :format "%[Dedicate window to buffer%] %v\n" :size 15 | |
4588 | (const :tag "Off" :format "%t" nil) | |
4589 | (const :tag "Weak" :format "%t" weak) | |
4590 | (const :tag "Strong" :format "%t" t))) | |
4591 | ;; No other window. | |
4592 | (cons | |
4593 | :format "%v" | |
4594 | (const :format "" no-other-window) | |
4595 | (choice | |
4596 | :help-echo "Whether `other-window' shall ignore the window." | |
4597 | :format "%[No other window%] %v\n" :size 15 | |
4598 | (const :tag "Off" :format "%t" nil) | |
4599 | (const :tag "Ignore" :format "%t" t))) | |
25aef8b8 MR |
4600 | ;; Other window means other frame. |
4601 | (cons | |
4602 | :format "%v" | |
4603 | (const :format "" other-window-means-other-frame) | |
4604 | (choice | |
4605 | :help-echo "Whether other window means same or other frame." | |
5f45cca5 MR |
4606 | :format "%[Other window means other frame%] %v\n" :size 15 |
4607 | (const :tag "Off" :format "%t" nil) | |
4608 | (const :tag "On" :format "%t" t))) | |
9481c002 MR |
4609 | ;; Overriding. |
4610 | (cons | |
4611 | :format "%v\n" | |
4612 | (const :format "" override) | |
4613 | (choice | |
4614 | :help-echo "Override application supplied specifiers." | |
4615 | :format "%[Override%] %v" | |
4616 | (const :tag "Off" :format "%t" nil) | |
4617 | (const :tag "Override" :format "%t" t)))))) | |
4618 | :group 'windows | |
4619 | :group 'frames) | |
4620 | ||
3c448ab6 | 4621 | (defcustom display-buffer-function nil |
9481c002 MR |
4622 | "If non-nil, function to call to display a buffer. |
4623 | `display-buffer' calls this function with two arguments, the | |
4624 | buffer to display and a list of buffer display specifiers, see | |
4625 | `display-buffer-alist'. | |
4626 | ||
4627 | The function is supposed to choose or create a window, display | |
4628 | the specified buffer in it, and return the window. It is also | |
4629 | responsible for giving the variable `display-buffer-window' and | |
4630 | the `quit-restore' parameter of the window used a meaningful | |
4631 | value. | |
4632 | ||
4633 | The function specified here overrides all specifiers of the | |
4634 | variable `display-buffer-alist' any specifiers passed to | |
4635 | `display-buffer'. | |
4636 | ||
4637 | If you call `display-buffer' within the body of the function, | |
4638 | bind the value of `display-buffer-function' to nil around that | |
4639 | call to avoid that the function recursively calls itself." | |
3c448ab6 MR |
4640 | :type '(choice |
4641 | (const nil) | |
9481c002 MR |
4642 | (function :tag "Function")) |
4643 | :group 'windows) | |
4644 | ||
4645 | ;; The following is a global variable which is used externally (by | |
4646 | ;; help.el) to (1) know which window was used for displaying a buffer | |
4647 | ;; and (2) whether the window was new or reused. | |
4648 | (defvar display-buffer-window nil | |
4649 | "Window used by `display-buffer' and related information. | |
4650 | After `display-buffer' displays a buffer in some window this | |
4651 | variable is a cons cell whose car denotes the window used to | |
4652 | display the buffer. The cdr is supposed to be one of the symbols | |
4653 | `reuse-buffer-window', `reuse-other-window', `new-window' or | |
4654 | `new-frame'. | |
4655 | ||
4656 | If the buffer display location specifier is one of 'same-window, | |
4657 | 'same-frame, or 'other-frame, the `display-buffer' routines | |
4658 | assign the value of this variable. If the location specifier is | |
4659 | a function, that function becomes responsible for assigning a | |
4660 | meaningful value to this variable. See the functions | |
4661 | `display-buffer-reuse-window', `display-buffer-pop-up-window' and | |
4662 | `display-buffer-pop-up-frame' for how this can be done.") | |
4663 | ||
4664 | (defun display-buffer-even-window-sizes (window specifiers) | |
4665 | "Even sizes of WINDOW and selected window according to SPECIFIERS. | |
4666 | SPECIFIERS must be a list of buffer display specifiers, see the | |
4667 | documentation of `display-buffer-alist' for a description. | |
4668 | ||
4669 | Sizes are evened out if and only if WINDOW and the selected | |
4670 | window appear next to each other and the selected window is | |
4671 | larger than WINDOW." | |
4672 | (cond | |
4673 | ((or (not (cdr (assq 'reuse-window-even-sizes specifiers))) | |
4674 | ;; Don't resize minibuffer windows. | |
4675 | (window-minibuffer-p) | |
4676 | ;; WINDOW must be adjacent to the selected one. | |
d68443dc MR |
4677 | (not (or (eq window (window-prev-sibling)) |
4678 | (eq window (window-next-sibling)))))) | |
9481c002 MR |
4679 | ((and (window-iso-combined-p window) |
4680 | ;; Resize iff the selected window is higher than WINDOW. | |
4681 | (> (window-total-height) (window-total-height window))) | |
4682 | ;; Don't throw an error if we can't even window heights for | |
4683 | ;; whatever reason. In any case, enlarging the selected window | |
4684 | ;; might fail anyway if there are other windows above or below | |
4685 | ;; WINDOW and the selected one. But for a simple two windows | |
4686 | ;; configuration the present behavior is good enough so why care? | |
4687 | (ignore-errors | |
d615d6d2 | 4688 | (window-resize |
9481c002 MR |
4689 | window (/ (- (window-total-height) (window-total-height window)) |
4690 | 2)))) | |
4691 | ((and (window-iso-combined-p window t) | |
4692 | ;; Resize iff the selected window is wider than WINDOW. | |
4693 | (> (window-total-width) (window-total-width window))) | |
4694 | ;; Don't throw an error if we can't even window widths, see | |
4695 | ;; comment above. | |
4696 | (ignore-errors | |
d615d6d2 | 4697 | (window-resize |
9481c002 MR |
4698 | window (/ (- (window-total-width) (window-total-width window)) |
4699 | 2) t))))) | |
4700 | ||
4701 | (defun display-buffer-set-height (window specifiers) | |
4702 | "Adjust height of WINDOW according to SPECIFIERS. | |
4703 | SPECIFIERS must be a list of buffer display specifiers, see the | |
4704 | documentation of `display-buffer-alist' for a description." | |
4705 | (let ((set-height (cdr (assq 'pop-up-window-set-height specifiers)))) | |
4706 | (cond | |
4707 | ((numberp set-height) | |
4708 | (let* ((height (if (integerp set-height) | |
4709 | set-height | |
4710 | (round | |
4711 | (* (window-total-size (frame-root-window window)) | |
4712 | set-height)))) | |
4713 | (delta (- height (window-total-size window)))) | |
4714 | (when (and (window-resizable-p window delta nil 'safe) | |
4715 | (window-iso-combined-p window)) | |
d615d6d2 | 4716 | (window-resize window delta nil 'safe)))) |
9481c002 MR |
4717 | ((functionp set-height) |
4718 | (ignore-errors (funcall set-height window)))))) | |
4719 | ||
4720 | (defun display-buffer-set-width (window specifiers) | |
4721 | "Adjust width of WINDOW according to SPECIFIERS. | |
4722 | SPECIFIERS must be a list of buffer display specifiers, see the | |
4723 | documentation of `display-buffer-alist' for a description." | |
4724 | (let ((set-width (cdr (assq 'pop-up-window-set-width specifiers)))) | |
4725 | (cond | |
4726 | ((numberp set-width) | |
4727 | (let* ((width (if (integerp set-width) | |
4728 | set-width | |
4729 | (round | |
4730 | (* (window-total-size (frame-root-window window) t) | |
4731 | set-width)))) | |
4732 | (delta (- width (window-total-size window t)))) | |
4733 | (when (and (window-resizable-p window delta t 'safe) | |
4734 | (window-iso-combined-p window t)) | |
d615d6d2 | 4735 | (window-resize window delta t 'safe)))) |
9481c002 MR |
4736 | ((functionp set-width) |
4737 | (ignore-errors (funcall set-width window)))))) | |
4738 | ||
9481c002 MR |
4739 | (defun display-buffer-in-window (buffer window specifiers) |
4740 | "Display BUFFER in WINDOW and raise its frame if needed. | |
4741 | WINDOW must be a live window and defaults to the selected one. | |
4742 | Return WINDOW. | |
4743 | ||
4744 | SPECIFIERS must be a list of buffer display specifiers, see the | |
4745 | documentation of `display-buffer-alist' for a description." | |
5386012d MR |
4746 | (setq buffer (window-normalize-buffer buffer)) |
4747 | (setq window (window-normalize-live-window window)) | |
9481c002 MR |
4748 | (let* ((old-frame (selected-frame)) |
4749 | (new-frame (window-frame window)) | |
4750 | (dedicated (cdr (assq 'dedicated specifiers))) | |
4751 | (no-other-window (cdr (assq 'no-other-window specifiers)))) | |
4752 | ;; Show BUFFER in WINDOW. | |
355f2e07 MR |
4753 | (unless (eq buffer (window-buffer window)) |
4754 | ;; If we show another buffer in WINDOW, undedicate it first. | |
4755 | (set-window-dedicated-p window nil)) | |
9481c002 MR |
4756 | (set-window-buffer window buffer) |
4757 | (when dedicated | |
4758 | (set-window-dedicated-p window dedicated)) | |
4759 | (when no-other-window | |
4760 | (set-window-parameter window 'no-other-window t)) | |
327c8fb1 MR |
4761 | (unless (or (eq old-frame new-frame) |
4762 | (not (frame-visible-p new-frame)) | |
4763 | ;; Assume the selected frame is already visible enough. | |
4764 | (eq new-frame (selected-frame)) | |
4765 | ;; Assume the frame from which we invoked the minibuffer | |
4766 | ;; is visible. | |
4767 | (and (minibuffer-window-active-p (selected-window)) | |
4768 | (eq new-frame | |
4769 | (window-frame (minibuffer-selected-window))))) | |
4770 | (raise-frame new-frame)) | |
9481c002 MR |
4771 | ;; Return window. |
4772 | window)) | |
4773 | ||
4774 | (defun display-buffer-reuse-window (buffer method &optional specifiers) | |
4775 | "Display BUFFER in an existing window. | |
4776 | METHOD must be a list in the form of the cdr of a `reuse-window' | |
4777 | buffer display specifier, see `display-buffer-alist' for an | |
4778 | explanation. The first element must specifiy the window to use, | |
4779 | and can be either nil, `same', `other', or a live window. The | |
4780 | second element must specify the window's buffer and can be either | |
4781 | nil, `same', `other', or a live buffer. The third element is the | |
4782 | frame to use - either nil, 0, `visible', `other', t, or a live | |
4783 | frame. | |
4784 | ||
4785 | Optional argument SPECIFIERS must be a list of valid display | |
4786 | specifiers. Return the window chosen to display BUFFER, nil if | |
4787 | none was found." | |
4788 | (let* ((method-window (nth 0 method)) | |
4789 | (method-buffer (nth 1 method)) | |
4790 | (method-frame (nth 2 method)) | |
d615d6d2 | 4791 | (reuse-dedicated (cdr (assq 'reuse-window-dedicated specifiers))) |
9481c002 MR |
4792 | windows other-frame dedicated time best-window best-time) |
4793 | (when (eq method-frame 'other) | |
4794 | ;; `other' is not handled by `window-list-1'. | |
4795 | (setq other-frame t) | |
4796 | (setq method-frame t)) | |
4797 | (dolist (window (window-list-1 nil 'nomini method-frame)) | |
4798 | (let ((window-buffer (window-buffer window))) | |
4799 | (when (and (not (window-minibuffer-p window)) | |
4800 | ;; Don't reuse a side window. | |
4801 | (or (not (eq (window-parameter window 'window-side) 'side)) | |
4802 | (eq window-buffer buffer)) | |
4803 | (or (not method-window) | |
4804 | (and (eq method-window 'same) | |
4805 | (eq window (selected-window))) | |
4806 | (and (eq method-window 'other) | |
4807 | (not (eq window (selected-window)))) | |
4808 | ;; Special case for applications that specifiy | |
4809 | ;; the window explicitly. | |
4810 | (eq method-window window)) | |
4811 | (or (not method-buffer) | |
4812 | (and (eq method-buffer 'same) | |
4813 | (eq window-buffer buffer)) | |
4814 | (and (eq method-buffer 'other) | |
4815 | (not (eq window-buffer buffer))) | |
4816 | ;; Special case for applications that specifiy | |
4817 | ;; the window's buffer explicitly. | |
4818 | (eq method-buffer window-buffer)) | |
4819 | (or (not other-frame) | |
4820 | (not (eq (window-frame window) (selected-frame)))) | |
4821 | ;; Handle dedicatedness. | |
4822 | (or (eq window-buffer buffer) | |
4823 | ;; The window does not show the same buffer. | |
4824 | (not (setq dedicated (window-dedicated-p window))) | |
4825 | ;; If the window is weakly dedicated to its | |
4826 | ;; buffer, reuse-dedicated must be non-nil. | |
4827 | (and (not (eq dedicated t)) reuse-dedicated) | |
4828 | ;; If the window is strongly dedicated to its | |
4829 | ;; buffer, reuse-dedicated must be t. | |
4830 | (eq reuse-dedicated t))) | |
4831 | (setq windows (cons window windows))))) | |
4832 | ||
4833 | (if (eq method-buffer 'same) | |
4834 | ;; When reusing a window on the same buffer use the lru one. | |
4835 | (dolist (window windows) | |
4836 | (setq time (window-use-time window)) | |
4837 | (when (or (not best-window) (< time best-time)) | |
4838 | (setq best-window window) | |
4839 | (setq best-time time))) | |
4840 | ;; Otherwise, sort windows according to their use-time. | |
4841 | (setq windows | |
4842 | (sort windows | |
4843 | #'(lambda (window-1 window-2) | |
4844 | (<= (window-use-time window-1) | |
4845 | (window-use-time window-2))))) | |
4846 | (setq best-window | |
4847 | ;; Try to get a full-width window (this is silly and can | |
4848 | ;; get us to another frame but let's ignore these issues | |
4849 | ;; for the moment). | |
4850 | (catch 'found | |
4851 | (dolist (window windows) | |
4852 | (when (window-full-width-p window) | |
4853 | (throw 'found window))) | |
4854 | ;; If there's no full-width window return the lru window. | |
4855 | (car windows)))) | |
4856 | ||
4857 | (when best-window | |
4858 | (display-buffer-even-window-sizes best-window specifiers) | |
4859 | ;; Never change the quit-restore parameter of a window here. | |
4860 | (if (eq (window-buffer best-window) buffer) | |
4861 | (setq display-buffer-window | |
4862 | (cons best-window 'reuse-buffer-window)) | |
4863 | (setq display-buffer-window | |
4864 | (cons best-window 'reuse-other-window)) | |
4865 | (unless (window-parameter best-window 'quit-restore) | |
4866 | ;; Don't overwrite an existing quit-restore entry. | |
4867 | (set-window-parameter | |
4868 | best-window 'quit-restore | |
4869 | (list (window-buffer best-window) (window-start best-window) | |
4870 | (window-point best-window) buffer | |
4871 | (window-total-size best-window) (selected-window))))) | |
4872 | ||
4873 | (display-buffer-in-window buffer best-window specifiers)))) | |
4874 | ||
4875 | (defconst display-buffer-split-specifiers '(largest lru selected root left top right bottom) | |
4876 | "List of symbols identifying window that shall be split.") | |
4877 | ||
4878 | (defconst display-buffer-side-specifiers '(below right above left nil) | |
4879 | "List of symbols identifying side of split-off window.") | |
4880 | ||
4881 | (defun display-buffer-split-window-1 (window side min-size) | |
4882 | "Subroutine of `display-buffer-split-window'." | |
4883 | (let* ((horizontal (memq side '(left right))) | |
4884 | (parent (window-parent window)) | |
4885 | (resize (and window-splits (window-iso-combined-p window horizontal))) | |
4886 | (old-size | |
4887 | ;; We either resize WINDOW or its parent. | |
4888 | (window-total-size (if resize parent window) horizontal)) | |
4889 | new-size) | |
4890 | ;; We don't call split-window-vertically/-horizontally any more | |
4891 | ;; here. If for some reason it's needed we can always do so | |
4892 | ;; (provided we give it an optional SIDE argument). | |
4893 | (cond | |
4894 | (resize | |
4895 | ;; When we resize a combination, the new window must be at least | |
4896 | ;; MIN-SIZE large after the split. | |
4897 | (setq new-size | |
4898 | (max min-size | |
4899 | (min (- old-size (window-min-size parent horizontal)) | |
4900 | (/ old-size | |
4901 | ;; Try to make the size of the new window | |
4902 | ;; proportional to the number of iso-arranged | |
4903 | ;; windows in the combination. | |
4904 | (1+ (window-iso-combinations parent horizontal)))))) | |
4905 | (when (window-sizable-p parent (- new-size) horizontal) | |
4906 | (split-window window (- new-size) side))) | |
4907 | ((window-live-p window) | |
4908 | (setq new-size (/ old-size 2)) | |
4909 | ;; When WINDOW is live, the old _and_ the new window must be at | |
4910 | ;; least MIN-SIZE large after the split. | |
4911 | (when (and (>= new-size min-size) | |
4912 | (window-sizable-p window (- new-size) horizontal)) | |
4913 | ;; Do an even split to make Stepan happy. | |
4914 | (split-window window nil side))) | |
4915 | (t | |
4916 | ;; When WINDOW is internal, the new window must be at least | |
4917 | ;; MIN-SIZE large after the split. | |
4918 | (setq new-size | |
4919 | (max min-size | |
4920 | (/ old-size | |
4921 | ;; Try to make the size of the new window | |
4922 | ;; proportional to the number of iso-arranged | |
4923 | ;; subwindows of WINDOW. | |
4924 | (1+ (window-iso-combinations window horizontal))))) | |
4925 | (when (window-sizable-p window (- new-size) horizontal) | |
4926 | (split-window window (- new-size) side)))))) | |
4927 | ||
4928 | (defun display-buffer-split-window (window &optional side specifiers) | |
4929 | "Split WINDOW in a way suitable for `display-buffer'. | |
4930 | Optional argument SIDE must be a side specifier \(one of the | |
4931 | symbols below, right, above, left, or nil). SPECIFIERS must be a | |
4932 | list of buffer display specifiers, see the documentation of | |
4933 | `display-buffer-alist' for a description. | |
4934 | ||
4935 | Return the new window, nil if it could not be created." | |
4936 | (let ((min-height (cdr (assq 'pop-up-window-min-height specifiers))) | |
4937 | (min-width (cdr (assq 'pop-up-window-min-width specifiers))) | |
4938 | size) | |
4939 | ;; Normalize min-height and min-width, we might need both. | |
4940 | (setq min-height | |
4941 | ;; If min-height is specified, it can be as small as | |
4942 | ;; `window-safe-min-height'. | |
4943 | (cond | |
4944 | ((and (integerp min-height) | |
4945 | (>= min-height window-safe-min-height)) | |
4946 | min-height) | |
4947 | ((and (floatp min-height) | |
4948 | (<= min-height 1) | |
4949 | (let* ((root-height (window-total-height | |
4950 | (frame-root-window | |
4951 | (window-frame window)))) | |
4952 | (height (round (* min-height root-height)))) | |
4953 | (when (>= height window-safe-min-height) | |
4954 | height)))) | |
4955 | (t window-min-height))) | |
4956 | (setq min-width | |
4957 | ;; If min-width is specified, it can be as small as | |
4958 | ;; `window-safe-min-width'. | |
4959 | (cond | |
4960 | ((and (integerp min-width) | |
4961 | (>= min-width window-safe-min-width)) | |
4962 | min-width) | |
4963 | ((and (floatp min-width) | |
4964 | (<= min-width 1) | |
4965 | (let* ((root-width (window-total-width | |
4966 | (frame-root-window | |
4967 | (window-frame window)))) | |
4968 | (width (round (* min-width root-width)))) | |
4969 | (when (>= width window-safe-min-width) | |
4970 | width)))) | |
4971 | (t window-min-width))) | |
4972 | ||
4973 | (or (and (memq side '(nil above below)) | |
4974 | (display-buffer-split-window-1 | |
4975 | window (or side 'below) min-height)) | |
4976 | ;; If SIDE is nil and vertical splitting failed, we try again | |
4977 | ;; splitting horizontally this time. | |
4978 | (and (memq side '(nil left right)) | |
4979 | (display-buffer-split-window-1 | |
4980 | window (or side 'right) min-width)) | |
4981 | ;; If WINDOW is live and the root window of its frame, try once | |
4982 | ;; more splitting vertically, disregarding the min-height | |
4983 | ;; specifier this time and using `window-min-height' instead. | |
4984 | (and (memq side '(nil above below)) | |
4985 | (<= window-min-height min-height) | |
4986 | (window-live-p window) | |
4987 | (eq window (frame-root-window window)) | |
4988 | (display-buffer-split-window-1 | |
4989 | window (or side 'below) window-min-height))))) | |
4990 | ||
4991 | (defun display-buffer-split-atom-window (window &optional side nest specifiers) | |
4992 | "Make WINDOW part of an atomic window." | |
4993 | (let ((ignore-window-parameters t) | |
4994 | (window-nest t) | |
4995 | (selected-window (selected-window)) | |
4996 | root new new-parent) | |
4997 | ||
4998 | ;; We are in an atomic window. | |
4999 | (when (and (window-parameter window 'window-atom) (not nest)) | |
5000 | ;; Split the root window. | |
5001 | (setq window (window-atom-root window))) | |
5002 | ||
5003 | (when (setq new (display-buffer-split-window window side specifiers)) | |
5004 | (setq new-parent (window-parent window)) | |
5005 | ;; WINDOW is or becomes atomic. | |
5006 | (unless (window-parameter window 'window-atom) | |
5007 | (walk-window-subtree | |
5008 | (lambda (window) | |
5009 | (set-window-parameter window 'window-atom t)) | |
5010 | window t)) | |
5011 | ;; New window and any new parent get their window-atom parameter | |
5012 | ;; set too. | |
5013 | (set-window-parameter new 'window-atom t) | |
5014 | (set-window-parameter new-parent 'window-atom t) | |
5015 | new))) | |
5016 | ||
5017 | (defun display-buffer-pop-up-window (buffer methods &optional specifiers) | |
5018 | "Display BUFFER in a new window. | |
5019 | Return the window displaying BUFFER, nil if popping up the window | |
5020 | failed. METHODS must be a list of window/side tuples like those | |
5021 | forming the cdr of the `pop-up-window' buffer display specifier. | |
5022 | As a special case, the car of such a tuple can be also a live | |
5023 | window. | |
5024 | ||
5025 | Optional argument SPECIFIERS must be a list of buffer display | |
5026 | specifiers, see the doc-string of `display-buffer-alist' for a | |
5027 | description." | |
5028 | (let* ((frame (display-buffer-frame)) | |
5029 | (selected-window (frame-selected-window frame)) | |
02cfc6d6 | 5030 | cand window side atomic) |
9481c002 MR |
5031 | (unless (and (cdr (assq 'unsplittable (frame-parameters frame))) |
5032 | ;; Don't split an unsplittable frame unless | |
5033 | ;; SPECIFIERS allow it. | |
5034 | (not (cdr (assq 'split-unsplittable-frame specifiers)))) | |
5035 | (catch 'done | |
5036 | (dolist (method methods) | |
02cfc6d6 | 5037 | (setq cand (car method)) |
9481c002 | 5038 | (setq side (cdr method)) |
02cfc6d6 MR |
5039 | (setq window |
5040 | (cond | |
5041 | ((eq cand 'largest) | |
9173deec | 5042 | ;; The largest window. |
02cfc6d6 MR |
5043 | (get-largest-window frame t)) |
5044 | ((eq cand 'lru) | |
5045 | ;; The least recently used window. | |
5046 | (get-lru-window frame t)) | |
5047 | ((eq cand 'selected) | |
5048 | ;; The selected window. | |
5049 | (frame-selected-window frame)) | |
5050 | ((eq cand 'root) | |
5051 | ;; If there are side windows, split the main window | |
5052 | ;; else the frame's root window. | |
5053 | (or (window-with-parameter 'window-side 'none nil t) | |
5054 | (frame-root-window frame))) | |
5055 | ((memq cand window-sides) | |
5056 | ;; This should gets us the "root" side window if there | |
5057 | ;; exists more than one window on that side. | |
5058 | (window-with-parameter 'window-side cand nil t)) | |
5059 | ((windowp cand) | |
5060 | ;; A window, directly specified. | |
5061 | cand))) | |
5062 | ||
5063 | (when (and (window-live-p window) | |
5064 | ;; The window must be on the correct frame, | |
5065 | (eq (window-frame window) frame) | |
5066 | ;; and must be neither a minibuffer window | |
5067 | (not (window-minibuffer-p window)) | |
5068 | ;; nor a side window. | |
5069 | (not (eq (window-parameter window 'window-side) 'side))) | |
5070 | (setq window | |
5071 | (cond | |
5072 | ((memq side display-buffer-side-specifiers) | |
5073 | (if (and (window-buffer window) | |
5074 | (setq atomic (cdr (assq 'atomic specifiers)))) | |
5075 | (display-buffer-split-atom-window | |
5076 | window side (eq atomic 'nest) specifiers) | |
5077 | (display-buffer-split-window window side specifiers))) | |
5078 | ((functionp side) | |
5079 | (ignore-errors | |
5080 | ;; Don't pass any specifiers to this function. | |
5081 | (funcall side window))))) | |
5082 | ||
5083 | (when window | |
5084 | ;; Adjust sizes if asked for. | |
5085 | (display-buffer-set-height window specifiers) | |
5086 | (display-buffer-set-width window specifiers) | |
5087 | (set-window-parameter | |
5088 | window 'quit-restore (list 'new-window buffer selected-window)) | |
5089 | (setq display-buffer-window (cons window 'new-window)) | |
5090 | (display-buffer-in-window buffer window specifiers) | |
5091 | (set-window-prev-buffers window nil) | |
5092 | (throw 'done window)))))))) | |
9481c002 MR |
5093 | |
5094 | (defun display-buffer-pop-up-frame (buffer &optional graphic-only specifiers) | |
5095 | "Make a new frame for displaying BUFFER. | |
5096 | Return the window displaying BUFFER if creating the new frame was | |
5097 | successful, nil otherwise. Optional argument GRAPHIC-ONLY | |
5098 | non-nil means to make a new frame on graphic displays only. | |
5099 | ||
5100 | SPECIFIERS must be a list of buffer display specifiers, see the | |
5101 | documentation of `display-buffer-alist' for a description." | |
9c2755e9 MR |
5102 | (unless (or (and graphic-only (not (display-graphic-p))) |
5103 | noninteractive) | |
9481c002 MR |
5104 | (let* ((selected-window (selected-window)) |
5105 | (function (or (cdr (assq 'pop-up-frame-function specifiers)) | |
5106 | 'make-frame)) | |
5107 | (parameters | |
5108 | (when (symbolp function) | |
5109 | (cdr (assq 'pop-up-frame-alist specifiers)))) | |
5110 | (frame | |
5111 | (if (symbolp function) | |
5112 | (funcall function parameters) | |
5113 | (funcall function)))) | |
5114 | (when frame | |
5115 | (let ((window (frame-selected-window frame))) | |
5116 | (set-window-parameter | |
5117 | window 'quit-restore (list 'new-frame buffer selected-window)) | |
5118 | (setq display-buffer-window (cons window 'new-frame)) | |
5119 | (display-buffer-in-window buffer window specifiers)))))) | |
5120 | ||
5121 | (defun display-buffer-pop-up-side-window (buffer side slot &optional specifiers) | |
5122 | "Display BUFFER in a new window on SIDE of the selected frame. | |
5123 | SLOT specifies the slot to use. SPECIFIERS must be a list of | |
5124 | buffer display specifiers. | |
5125 | ||
5126 | Return the window displaying BUFFER, nil if popping up the window | |
5127 | failed." | |
5128 | (let* ((root (frame-root-window)) | |
5129 | (main (window-with-parameter 'window-side 'none nil t)) | |
5130 | (left-or-right (memq side '(left right))) | |
5131 | (main-or-root | |
5132 | (if (and main | |
5133 | (or (and left-or-right (not window-sides-vertical)) | |
5134 | (and (not left-or-right) window-sides-vertical))) | |
5135 | main | |
5136 | root)) | |
5137 | (selected-window (selected-window)) | |
5138 | (on-side (cond | |
5139 | ((eq side 'top) 'above) | |
5140 | ((eq side 'bottom) 'below) | |
5141 | (t side))) | |
5142 | (window | |
5143 | (display-buffer-split-window main-or-root on-side specifiers)) | |
5144 | fun) | |
5145 | (when window | |
5146 | (unless main | |
5147 | (walk-window-subtree | |
5148 | (lambda (window) | |
5149 | ;; Make all main-or-root subwindows main windows. | |
5150 | (set-window-parameter window 'window-side 'none)) | |
5151 | main-or-root t)) | |
5152 | ;; Make sure that parent's window-side is nil. | |
5153 | (set-window-parameter (window-parent window) 'window-side nil) | |
5154 | ;; Initialize side. | |
5155 | (set-window-parameter window 'window-side side) | |
5156 | ;; Adjust sizes if asked for. | |
5157 | (display-buffer-set-height window specifiers) | |
5158 | (display-buffer-set-width window specifiers) | |
5159 | ;; Set window parameters. | |
5160 | (set-window-parameter | |
5161 | window 'quit-restore (list 'new-window buffer selected-window)) | |
5162 | (setq display-buffer-window (cons window 'new-window)) | |
5163 | (set-window-parameter window 'window-slot slot) | |
5164 | (display-buffer-in-window buffer window specifiers) | |
5165 | (set-window-prev-buffers window nil) | |
5166 | window))) | |
5167 | ||
5168 | (defun display-buffer-in-side-window (buffer side &optional slot specifiers) | |
5169 | "Display BUFFER in a window on SIDE of the selected frame. | |
5170 | SLOT, if non-nil, specifies the window slot where to display the | |
5171 | BUFFER. SLOT zero or nil means use the central slot on SIDE. | |
5172 | SLOT negative means use a slot preceding the central window. | |
5173 | SLOT positive means use a slot following the central window. | |
5174 | ||
5175 | SPECIFIERS must be a list of buffer display specifiers." | |
5176 | (unless (memq side window-sides) | |
5177 | (error "Invalid side %s specified" side)) | |
5178 | (let* ((major (window-with-parameter 'window-side side nil t)) | |
5179 | ;; `major' is the major window on SIDE, `windows' the life | |
5180 | ;; windows on SIDE. | |
5181 | (windows (when major (windows-with-parameter 'window-side side))) | |
5f45cca5 | 5182 | (reuse-dedicated (cdr (assq 'reuse-window-dedicated specifiers))) |
9481c002 MR |
5183 | (slots (when major (window-child-count major))) |
5184 | (max-slots | |
5185 | (nth (cond | |
5186 | ((eq side 'left) 0) | |
5187 | ((eq side 'top) 1) | |
5188 | ((eq side 'right) 2) | |
5189 | ((eq side 'bottom) 3)) | |
5190 | window-sides-slots)) | |
5191 | (selected-window (selected-window)) | |
5192 | window this-window this-slot prev-window next-window | |
5f45cca5 | 5193 | best-window best-slot abs-slot dedicated) |
9481c002 MR |
5194 | |
5195 | (unless (numberp slot) | |
5196 | (setq slot 0)) | |
5197 | (if (not windows) | |
5198 | ;; No suitable side window exists, make one. | |
5199 | (display-buffer-pop-up-side-window buffer side slot specifiers) | |
5200 | ;; Scan windows on SIDE. | |
5201 | (catch 'found | |
5202 | (dolist (window windows) | |
5203 | (setq this-slot (window-parameter window 'window-slot)) | |
5204 | (cond | |
5205 | ((not (numberp this-slot))) | |
5206 | ((and (= this-slot slot) | |
5207 | ;; Dedicatedness check. | |
5f45cca5 MR |
5208 | (or (not (setq dedicated (window-dedicated-p window))) |
5209 | ;; If the window is weakly dedicated to its | |
5210 | ;; buffer, reuse-dedicated must be non-nil. | |
5211 | (and (not (eq dedicated t)) reuse-dedicated) | |
5212 | ;; If the window is strongly dedicated to its | |
5213 | ;; buffer, reuse-dedicated must be t. | |
5214 | (eq reuse-dedicated t))) | |
9481c002 MR |
5215 | ;; Window with matching SLOT, use it. |
5216 | (setq this-window window) | |
5217 | (throw 'found t)) | |
5218 | (t | |
5219 | (setq abs-slot (abs (- (abs slot) (abs this-slot)))) | |
5220 | (unless (and best-slot (<= best-slot abs-slot)) | |
5221 | (setq best-window window) | |
5222 | (setq best-slot abs-slot)) | |
5223 | (cond | |
5224 | ((<= this-slot slot) | |
5225 | (setq prev-window window)) | |
5226 | ((not next-window) | |
5227 | (setq next-window window))))))) | |
5228 | ||
5229 | ;; `this-window' is the first window with the same SLOT. | |
5230 | ;; `prev-window' is the window with the largest slot < SLOT. A new | |
5231 | ;; window will be created after it. | |
5232 | ;; `next-window' is the window with the smallest slot > SLOT. A new | |
5233 | ;; window will be created before it. | |
5234 | ;; `best-window' is the window with the smallest absolute difference | |
5235 | ;; of its slot and SLOT. | |
5236 | (or (and this-window | |
5237 | ;; Reuse this window. | |
5238 | (prog1 | |
5239 | (setq window this-window) | |
5240 | (if (eq (window-buffer window) buffer) | |
5241 | (setq display-buffer-window | |
5242 | (cons window 'reuse-buffer-window)) | |
5243 | (setq display-buffer-window | |
5244 | (cons window 'reuse-other-window)) | |
5245 | (unless (window-parameter window 'quit-restore) | |
5246 | ;; Don't overwrite an existing quit-restore entry. | |
5247 | (set-window-parameter | |
5248 | window 'quit-restore | |
5249 | (list (window-buffer window) (window-start window) | |
5250 | (window-point window) buffer | |
5251 | (window-total-size window) (selected-window))))))) | |
5252 | (and (or (not max-slots) (< slots max-slots)) | |
5253 | (or (and next-window | |
5254 | ;; Make new window before next-window. | |
5255 | (let ((next-side | |
5256 | (if (memq side '(left right)) 'above 'left))) | |
5257 | (setq window (display-buffer-split-window | |
5258 | next-window next-side specifiers)))) | |
5259 | (and prev-window | |
5260 | ;; Make new window after prev-window. | |
5261 | (let ((prev-side | |
5262 | (if (memq side '(left right)) 'below 'right))) | |
5263 | (setq window (display-buffer-split-window | |
5264 | prev-window prev-side specifiers))))) | |
5265 | (progn | |
5266 | (display-buffer-set-height window specifiers) | |
5267 | (display-buffer-set-width window specifiers) | |
5268 | (set-window-parameter | |
5269 | window 'quit-restore | |
5270 | (list 'new-window buffer selected-window)) | |
5271 | (setq display-buffer-window (cons window 'new-window)) | |
5272 | window)) | |
5273 | (and best-window | |
5274 | (setq window best-window) | |
5275 | ;; Reuse best window (the window nearest to SLOT). | |
5276 | (if (eq (window-buffer window) buffer) | |
5277 | (setq display-buffer-window | |
5278 | (cons window 'reuse-buffer-window)) | |
5279 | (setq display-buffer-window | |
5280 | (cons window 'reuse-other-window)) | |
5281 | ||
5282 | (unless (window-parameter window 'quit-restore) | |
5283 | ;; Don't overwrite an existing quit-restore entry. | |
5284 | (set-window-parameter | |
5285 | window 'quit-restore | |
5286 | (list (window-buffer window) (window-start window) | |
5287 | (window-point window) buffer | |
5288 | (window-total-size window) (selected-window))))) | |
5289 | window)) | |
5290 | ||
5291 | (when window | |
5292 | (unless (window-parameter window 'window-slot) | |
5293 | ;; Don't change exisiting slot value. | |
5294 | (set-window-parameter window 'window-slot slot)) | |
5295 | (display-buffer-in-window buffer window specifiers))))) | |
5296 | ||
5386012d | 5297 | (defun window-normalize-buffer-to-display (buffer-or-name) |
fbf5b3ce MR |
5298 | "Normalize BUFFER-OR-NAME argument for buffer display functions. |
5299 | If BUFFER-OR-NAME is nil, return the curent buffer. Else, if a | |
5300 | buffer specified by BUFFER-OR-NAME exists, return that buffer. | |
5301 | If no such buffer exists, create a buffer with the name | |
5302 | BUFFER-OR-NAME and return that buffer." | |
5303 | (if buffer-or-name | |
5304 | (or (get-buffer buffer-or-name) | |
5305 | (let ((buffer (get-buffer-create buffer-or-name))) | |
5306 | (set-buffer-major-mode buffer) | |
5307 | buffer)) | |
5308 | (current-buffer))) | |
5309 | ||
25aef8b8 MR |
5310 | (defun display-buffer-other-window-means-other-frame (buffer-or-name &optional label) |
5311 | "Return non-nil if BUFFER shall be preferably displayed in another frame. | |
5312 | BUFFER must be a live buffer or the name of a live buffer. | |
5313 | ||
5314 | Return nil if BUFFER shall be preferably displayed in another | |
5315 | window on the selected frame. Return non-nil if BUFFER shall be | |
5316 | preferably displayed in a window on any but the selected frame. | |
5317 | ||
5318 | Optional argument LABEL is like the same argument of | |
5319 | `display-buffer'. | |
5320 | ||
5321 | The calculation of the return value is exclusively based on the | |
5322 | user preferences expressed in `display-buffer-alist'." | |
5386012d | 5323 | (let* ((buffer (window-normalize-buffer buffer-or-name)) |
fbf5b3ce | 5324 | (list (display-buffer-normalize-alist (buffer-name buffer) label)) |
25aef8b8 MR |
5325 | (value (assq 'other-window-means-other-frame |
5326 | (or (car list) (cdr list))))) | |
5327 | (when value (cdr value)))) | |
5328 | ||
355f2e07 MR |
5329 | (defun display-buffer-normalize-arguments (buffer-name specifiers label other-frame) |
5330 | "Normalize second and third argument of `display-buffer'. | |
fbf5b3ce | 5331 | BUFFER-NAME is the name of the buffer that shall be displayed, |
355f2e07 MR |
5332 | SPECIFIERS is the second argument of `display-buffer'. LABEL is |
5333 | the same argument of `display-buffer'. OTHER-FRAME non-nil means | |
5334 | use other-frame for other-window." | |
5335 | (let (normalized entry specifier pars) | |
5336 | (setq specifier | |
5337 | (cond | |
5338 | ((not specifiers) | |
5339 | nil) | |
5340 | ((listp specifiers) | |
5341 | ;; If SPECIFIERS is a list, we assume it is a list of specifiers. | |
5342 | (dolist (specifier specifiers) | |
5343 | (cond | |
5344 | ((consp specifier) | |
5345 | (setq normalized (cons specifier normalized))) | |
5346 | ((eq specifier 'other-window) | |
5347 | ;; `other-window' must be treated separately. | |
5348 | (let ((entry (assq (if other-frame | |
5349 | 'other-frame | |
5350 | 'same-frame-other-window) | |
5351 | display-buffer-macro-specifiers))) | |
5352 | (dolist (item (cdr entry)) | |
5353 | (setq normalized (cons item normalized))))) | |
5354 | ((symbolp specifier) | |
5355 | ;; Might be a macro specifier, try to expand it (the cdr is a | |
5356 | ;; list and we have to reverse it later, so do it one at a | |
5357 | ;; time). | |
5358 | (let ((entry (assq specifier display-buffer-macro-specifiers))) | |
5359 | (dolist (item (cdr entry)) | |
5360 | (setq normalized (cons item normalized))))))) | |
5361 | ;; Reverse list. | |
5362 | (nreverse normalized)) | |
5363 | ((setq entry (assq specifiers display-buffer-macro-specifiers)) | |
5364 | ;; A macro specifier. | |
5365 | (cdr entry)) | |
5366 | ((or other-frame (with-no-warnings pop-up-frames)) | |
5367 | ;; `special-display-p' group. | |
5368 | (if (and (with-no-warnings special-display-function) | |
5369 | ;; `special-display-p' returns either t or a list | |
5370 | ;; of frame parameters to pass to | |
5371 | ;; `special-display-function'. | |
5372 | (setq pars (with-no-warnings | |
5373 | (special-display-p buffer-name)))) | |
5386012d | 5374 | (list (list 'function |
355f2e07 MR |
5375 | (with-no-warnings special-display-function) |
5376 | (when (listp pars) pars))) | |
5377 | ;; Pop up another frame. | |
5378 | (cddr (assq 'other-frame display-buffer-macro-specifiers)))) | |
5379 | (t | |
5380 | ;; In any other case pop up a new window. | |
5381 | (cdr (assq 'same-frame-other-window | |
5382 | display-buffer-macro-specifiers))))) | |
5383 | ||
5384 | ;; Handle the old meaning of the LABEL argument of `display-buffer'. | |
9481c002 | 5385 | (cond |
355f2e07 MR |
5386 | ((or (memq label '(visible 0 t)) (frame-live-p label)) |
5387 | ;; LABEL must be one of visible (and visible frame), 0 (any | |
5388 | ;; visible or iconfied frame), t (any frame), or a live frame. | |
5389 | (cons `(reuse-window nil same ,label) specifier)) | |
5390 | ((or other-frame | |
5391 | (with-no-warnings pop-up-frames) | |
5392 | (with-no-warnings display-buffer-reuse-frames)) | |
5393 | (cons '(reuse-window nil same 0) specifier)) | |
9b9c9e3a | 5394 | (t |
355f2e07 | 5395 | specifier)))) |
9481c002 | 5396 | |
fbf5b3ce | 5397 | (defun display-buffer-normalize-options (buffer-or-name) |
9481c002 MR |
5398 | "Subroutine of `display-buffer-normalize-specifiers'. |
5399 | BUFFER-OR-NAME is the buffer to display. This routine provides a | |
5400 | compatibility layer for the now obsolete Emacs 23 buffer display | |
5401 | options." | |
327c8fb1 | 5402 | (with-no-warnings |
5386012d | 5403 | (let* ((buffer (window-normalize-buffer buffer-or-name)) |
327c8fb1 MR |
5404 | (buffer-name (buffer-name buffer)) |
5405 | (use-pop-up-frames | |
5406 | (or (and (eq pop-up-frames 'graphic-only) | |
5407 | (display-graphic-p)) | |
5408 | pop-up-frames)) | |
5409 | specifiers) | |
d1067961 MR |
5410 | ;; `even-window-heights', unless nil or unset. |
5411 | (unless (memq even-window-heights '(nil unset)) | |
5412 | (setq specifiers | |
5413 | (cons (cons 'reuse-window-even-sizes t) specifiers))) | |
5414 | ||
9481c002 | 5415 | ;; `display-buffer-mark-dedicated' |
d1067961 | 5416 | (when display-buffer-mark-dedicated |
9481c002 MR |
5417 | (setq specifiers |
5418 | (cons (cons 'dedicate display-buffer-mark-dedicated) | |
5419 | specifiers))) | |
5420 | ||
5421 | ;; `pop-up-window' group. Anything is added here iff | |
5422 | ;; `pop-up-windows' is neither nil nor unset. | |
5423 | (let ((pop-up-window (not (memq pop-up-windows '(nil unset)))) | |
5424 | (fun (unless (eq split-window-preferred-function | |
5425 | 'split-window-sensibly) | |
5426 | split-window-preferred-function)) | |
5427 | (min-height (if (numberp split-height-threshold) | |
5428 | (/ split-height-threshold 2) | |
5429 | 1.0)) | |
5430 | (min-width (if (numberp split-width-threshold) | |
5431 | (/ split-width-threshold 2) | |
5432 | 1.0))) | |
be3fb2b8 MR |
5433 | ;; Create an entry only if a default value was changed. |
5434 | (when (or pop-up-window | |
5435 | (not (equal split-height-threshold 80)) | |
5436 | (not (equal split-width-threshold 160))) | |
5437 | ;; `reuse-window' (needed as fallback when popping up the new | |
5438 | ;; window fails). | |
9481c002 | 5439 | (setq specifiers |
be3fb2b8 | 5440 | (cons (list 'reuse-window 'other nil nil) |
9481c002 MR |
5441 | specifiers)) |
5442 | ;; `split-width-threshold' | |
5443 | (setq specifiers | |
5444 | (cons (cons 'pop-up-window-min-width min-width) | |
5445 | specifiers)) | |
be3fb2b8 MR |
5446 | ;; `split-height-threshold' |
5447 | (setq specifiers | |
5448 | (cons (cons 'pop-up-window-min-height min-height) | |
5449 | specifiers)) | |
9481c002 MR |
5450 | ;; `pop-up-window' |
5451 | (setq specifiers | |
5452 | (cons (list 'pop-up-window | |
5453 | (cons 'largest fun) (cons 'lru fun)) | |
5454 | specifiers)))) | |
5455 | ||
327c8fb1 MR |
5456 | ;; `pop-up-frame' group. |
5457 | (when use-pop-up-frames | |
9481c002 MR |
5458 | ;; `pop-up-frame-function'. If `pop-up-frame-function' uses the |
5459 | ;; now obsolete `pop-up-frame-alist' it will continue to do so. | |
5460 | (setq specifiers | |
5461 | (cons (cons 'pop-up-frame-function pop-up-frame-function) | |
5462 | specifiers)) | |
5463 | ;; `pop-up-frame' | |
5464 | (setq specifiers | |
327c8fb1 MR |
5465 | (cons (list 'pop-up-frame t) specifiers))) |
5466 | ||
5467 | ;; `pop-up-windows' and `use-pop-up-frames' both nil means means | |
5468 | ;; we are supposed to reuse any window on the same frame (unless | |
5469 | ;; we find one showing the same buffer already). | |
5470 | (unless (or pop-up-windows use-pop-up-frames) | |
5471 | ;; `reuse-window' showing any buffer on same frame. | |
5472 | (setq specifiers | |
5473 | (cons (list 'reuse-window nil nil nil) | |
5474 | specifiers))) | |
9481c002 | 5475 | |
9a028c23 MR |
5476 | ;; `special-display-p' group. |
5477 | (when special-display-function | |
5478 | ;; `special-display-p' returns either t or a list of frame | |
5479 | ;; parameters to pass to `special-display-function'. | |
5480 | (let ((pars (special-display-p buffer-name))) | |
5481 | (when pars | |
5482 | (setq specifiers | |
5386012d | 5483 | (cons (list 'function special-display-function |
9a028c23 MR |
5484 | (when (listp pars) pars)) |
5485 | specifiers))))) | |
5486 | ||
9e9de014 MR |
5487 | ;; `pop-up-frames', `display-buffer-reuse-frames' means search for |
5488 | ;; a window showing the buffer on some visible or iconfied frame. | |
5489 | ;; `last-nonminibuffer-frame' set and not the same frame means | |
5490 | ;; search that frame. | |
bd168c06 MR |
5491 | (let ((frames (or (and (or use-pop-up-frames |
5492 | display-buffer-reuse-frames | |
5493 | (not (last-nonminibuffer-frame))) | |
327c8fb1 | 5494 | ;; All visible or iconfied frames. |
bd168c06 | 5495 | 0) |
9e9de014 | 5496 | ;; Same frame. |
bd168c06 | 5497 | (last-nonminibuffer-frame)))) |
327c8fb1 MR |
5498 | (when frames |
5499 | (setq specifiers | |
5500 | (cons (list 'reuse-window 'other 'same frames) | |
5501 | specifiers)))) | |
5502 | ||
d1067961 MR |
5503 | ;; `same-window-p' group. |
5504 | (when (same-window-p buffer-name) | |
5505 | ;; Try to reuse the same (selected) window. | |
5506 | (setq specifiers | |
5507 | (cons (list 'reuse-window 'same nil nil) | |
5508 | specifiers))) | |
9481c002 | 5509 | |
d1067961 | 5510 | ;; Prepend "reuse window on same frame if showing the buffer |
327c8fb1 MR |
5511 | ;; already" specifier. It will be overriden by the application |
5512 | ;; supplied 'other-window specifier. | |
d1067961 MR |
5513 | (setq specifiers (cons (list 'reuse-window nil 'same nil) |
5514 | specifiers)) | |
9481c002 MR |
5515 | |
5516 | specifiers))) | |
5517 | ||
fbf5b3ce MR |
5518 | (defun display-buffer-normalize-alist-1 (specifiers label) |
5519 | "Subroutine of `display-buffer-normalize-alist'. | |
5520 | SPECIFIERS is a list of buffer display specfiers. LABEL is the | |
5521 | same argument of `display-buffer'." | |
5522 | (let (normalized entry) | |
5523 | (cond | |
5524 | ((not specifiers) | |
5525 | nil) | |
5526 | ((listp specifiers) | |
5527 | ;; If SPECIFIERS is a list, we assume it is a list of specifiers. | |
5528 | (dolist (specifier specifiers) | |
5529 | (cond | |
5530 | ((consp specifier) | |
5531 | (setq normalized (cons specifier normalized))) | |
5532 | ((symbolp specifier) | |
5533 | ;; Might be a macro specifier, try to expand it (the cdr is a | |
5534 | ;; list and we have to reverse it later, so do it one at a | |
5535 | ;; time). | |
5536 | (let ((entry (assq specifier display-buffer-macro-specifiers))) | |
5537 | (dolist (item (cdr entry)) | |
5538 | (setq normalized (cons item normalized))))))) | |
5539 | ;; Reverse list. | |
5540 | (nreverse normalized)) | |
5541 | ((setq entry (assq specifiers display-buffer-macro-specifiers)) | |
5542 | ;; A macro specifier. | |
5543 | (cdr entry))))) | |
5544 | ||
5545 | (defun display-buffer-normalize-alist (buffer-name label) | |
5546 | "Normalize `display-buffer-alist'. | |
5547 | BUFFER-NAME must be the name of the buffer that shall be displayed. | |
5548 | LABEL the corresponding argument of `display-buffer'." | |
9481c002 MR |
5549 | (let (list-1 list-2) |
5550 | (dolist (entry display-buffer-alist) | |
5551 | (when (and (listp entry) | |
5552 | (catch 'match | |
5553 | (dolist (id (car entry)) | |
5554 | (when (consp id) | |
5555 | (let ((type (car id)) | |
5556 | (value (cdr id))) | |
5557 | (when (or (and (eq type 'name) (stringp value) | |
5558 | (equal value buffer-name)) | |
5559 | (and (eq type 'regexp) (stringp value) | |
5560 | (string-match-p value buffer-name)) | |
5561 | (and (eq type 'label) (eq value label))) | |
5562 | (throw 'match t))))))) | |
fbf5b3ce | 5563 | (let* ((specifiers (cdr entry)) |
25aef8b8 | 5564 | (normalized |
fbf5b3ce | 5565 | (display-buffer-normalize-alist-1 specifiers label))) |
5f45cca5 | 5566 | (if (cdr (assq 'override specifiers)) |
9481c002 MR |
5567 | (setq list-1 |
5568 | (if list-1 | |
5569 | (append list-1 normalized) | |
5570 | normalized)) | |
5571 | (setq list-2 | |
5572 | (if list-2 | |
5573 | (append list-2 normalized) | |
5574 | normalized)))))) | |
5575 | ||
25aef8b8 MR |
5576 | (cons list-1 list-2))) |
5577 | ||
fbf5b3ce MR |
5578 | (defvar display-buffer-normalize-options-inhibit nil |
5579 | "If non-nil, `display-buffer' doesn't process obsolete options.") | |
5580 | ||
25aef8b8 MR |
5581 | (defun display-buffer-normalize-specifiers (buffer-name specifiers label) |
5582 | "Return normalized specifiers for a buffer matching BUFFER-NAME or LABEL. | |
5583 | BUFFER-NAME must be a string specifying a valid buffer name. | |
5584 | SPECIFIERS and LABEL are the homonymous arguments of | |
5585 | `display-buffer'. | |
5586 | ||
5587 | The method for displaying the buffer specified by BUFFER-NAME or | |
5588 | LABEL is established by appending the following four lists of | |
5589 | specifiers: | |
5590 | ||
5591 | - The specifiers in `display-buffer-alist' whose buffer | |
5592 | identifier matches BUFFER-NAME or LABEL and whose 'override | |
5593 | component is set. | |
5594 | ||
5595 | - SPECIFIERS. | |
5596 | ||
5597 | - The specifiers in `display-buffer-alist' whose buffer | |
5598 | identifier matches BUFFER-NAME or LABEL and whose 'override | |
5599 | component is not set. | |
5600 | ||
5601 | - `display-buffer-default-specifiers'." | |
fbf5b3ce | 5602 | (let* ((list (display-buffer-normalize-alist buffer-name label)) |
5f45cca5 MR |
5603 | (other-frame (cdr (assq 'other-window-means-other-frame |
5604 | (or (car list) (cdr list)))))) | |
9481c002 MR |
5605 | (append |
5606 | ;; Overriding user specifiers. | |
25aef8b8 | 5607 | (car list) |
9481c002 | 5608 | ;; Application specifiers. |
355f2e07 | 5609 | (display-buffer-normalize-arguments |
fbf5b3ce | 5610 | buffer-name specifiers label other-frame) |
9481c002 | 5611 | ;; Emacs 23 compatibility specifiers. |
fbf5b3ce MR |
5612 | (unless display-buffer-normalize-options-inhibit |
5613 | (display-buffer-normalize-options buffer-name)) | |
9481c002 | 5614 | ;; Non-overriding user specifiers. |
25aef8b8 | 5615 | (cdr list) |
9481c002 MR |
5616 | ;; Default specifiers. |
5617 | display-buffer-default-specifiers))) | |
5618 | ||
5619 | ;; Minibuffer-only frames should be documented better. They really | |
5620 | ;; deserve a separate section in the manual. Also | |
5621 | ;; `last-nonminibuffer-frame' is nowhere documented in the manual. | |
5622 | (defun display-buffer-frame (&optional frame) | |
5623 | "Return FRAME if it is live and not a minibuffer-only frame. | |
5624 | Return the value of `last-nonminibuffer-frame' otherwise." | |
5386012d | 5625 | (setq frame (window-normalize-frame frame)) |
9481c002 MR |
5626 | (if (and (frame-live-p frame) |
5627 | ;; A not very nice way to get that information. | |
5628 | (not (window-minibuffer-p (frame-root-window frame)))) | |
5629 | frame | |
5630 | (last-nonminibuffer-frame))) | |
5631 | ||
5632 | (defun display-buffer (&optional buffer-or-name specifiers label) | |
5633 | "Make the buffer specified by BUFFER-OR-NAME appear in some window. | |
5634 | Optional argument BUFFER-OR-NAME may be a buffer, a string \(a | |
5635 | buffer name), or nil. If BUFFER-OR-NAME is a string not naming | |
5636 | an existent buffer, create a buffer with that name. If | |
5637 | BUFFER-OR-NAME is nil or omitted, display the current buffer. | |
5638 | Interactively, prompt for the buffer name using the minibuffer. | |
5639 | ||
5640 | Return the window chosen to display the buffer or nil if no such | |
5641 | window is found. Do not change the selected window unless the | |
5642 | buffer is shown on a different frame than the selected one. | |
5643 | ||
5644 | Optional argument SPECIFIERS must be a list of buffer display | |
5645 | specifiers, see the documentation of `display-buffer-alist' for a | |
5646 | description. | |
5647 | ||
5648 | For convenience, SPECIFIERS may also consist of a single buffer | |
5649 | display location specifier or t, where the latter means to | |
5650 | display the buffer in any but the selected window. If SPECIFIERS | |
5651 | is nil or omitted, this means to exclusively use the specifiers | |
5652 | provided by `display-buffer-alist'. If the value of the latter | |
5653 | is nil too, all specifiers are provided by the constant | |
5654 | `display-buffer-default-specifiers'. | |
5655 | ||
5656 | As a special case, the `reuse-window' specifier allows to specify | |
5657 | as second element an arbitrary window, as third element an | |
5658 | arbitrary buffer, and as fourth element an arbitrary frame. As | |
5659 | first element of a window/side pair of the `pop-up-window' | |
5660 | specifier you can specifiy an arbitrary window. | |
5661 | ||
5662 | The optional third argument LABEL, if non-nil, must be a symbol | |
5663 | specifiying the buffer display label. Applications should set | |
5664 | this when the buffer shall be displayed in some special way but | |
5665 | BUFFER-OR-NAME does not identify the buffer as special. Typical | |
5666 | buffers that fit into this category are those whose names are | |
5667 | derived from the name of the file they are visiting. A user can | |
5668 | override SPECIFIERS by adding an entry to `display-buffer-alist' | |
5669 | whose car contains LABEL and whose cdr specifies the preferred | |
5670 | alternative display method. | |
5671 | ||
355f2e07 MR |
5672 | The following values of LABEL have a special meaning and allow to |
5673 | specify the set of frames to investigate when the buffer already | |
5674 | appears in a window: | |
5675 | ||
5676 | `visible' - the set of visible frames. | |
5677 | ||
5678 | 0 - the set of visible or iconified frames. | |
5679 | ||
5680 | t - the set of all frames. | |
5681 | ||
5682 | A live frame - the set containing that frame as its only element. | |
5683 | ||
5684 | If the buffer is already displayed in a window on a frame in the | |
5685 | specified set, return that window. | |
5686 | ||
9481c002 MR |
5687 | The method to display the buffer is derived by combining the |
5688 | values of `display-buffer-alist' and SPECIFIERS. Highest | |
5689 | priority is given to overriding elements of | |
5690 | `display-buffer-alist'. Next come the elements specified by | |
5691 | SPECIFIERS, followed by the non-overriding elements of | |
5692 | `display-buffer-alist'. | |
5693 | ||
5694 | The result must be a list of valid buffer display specifiers. If | |
5695 | `display-buffer-function' is non-nil, call it with the buffer and | |
5696 | this list as arguments." | |
5697 | (interactive "BDisplay buffer:\nP") | |
5386012d | 5698 | (let* ((buffer (window-normalize-buffer-to-display buffer-or-name)) |
9481c002 | 5699 | (buffer-name (buffer-name buffer)) |
02cfc6d6 | 5700 | (normalized |
9481c002 MR |
5701 | ;; Normalize specifiers. |
5702 | (display-buffer-normalize-specifiers buffer-name specifiers label)) | |
5703 | ;; Don't use a minibuffer frame. | |
5704 | (frame (display-buffer-frame)) | |
5705 | ;; `window' is the window we use for showing `buffer'. | |
5706 | window specifier method) | |
5707 | ;; Reset this. | |
5708 | (setq display-buffer-window nil) | |
5709 | (if display-buffer-function | |
5710 | ;; Let `display-buffer-function' do the job. | |
5711 | (funcall display-buffer-function buffer specifiers) | |
5712 | ;; Retrieve the next location specifier while there a specifiers | |
5713 | ;; left and we don't have a valid window. | |
02cfc6d6 MR |
5714 | (while (and normalized (not (window-live-p window))) |
5715 | (setq specifier (car normalized)) | |
5716 | (setq normalized (cdr normalized)) | |
9481c002 MR |
5717 | (setq method (car specifier)) |
5718 | (setq window | |
5719 | (cond | |
5720 | ((eq method 'reuse-window) | |
5721 | (display-buffer-reuse-window | |
02cfc6d6 | 5722 | buffer (cdr specifier) normalized)) |
9481c002 MR |
5723 | ((eq method 'pop-up-window) |
5724 | (display-buffer-pop-up-window | |
02cfc6d6 | 5725 | buffer (cdr specifier) normalized)) |
9481c002 MR |
5726 | ((eq method 'pop-up-frame) |
5727 | (display-buffer-pop-up-frame | |
02cfc6d6 | 5728 | buffer (cdr specifier) normalized)) |
9481c002 MR |
5729 | ((eq method 'use-side-window) |
5730 | (display-buffer-in-side-window | |
02cfc6d6 | 5731 | buffer (nth 1 specifier) (nth 2 specifier) normalized)) |
5386012d | 5732 | ((eq method 'function) |
0bf4ba9a | 5733 | (funcall (nth 1 specifier) buffer (nth 2 specifier)))))) |
9481c002 MR |
5734 | |
5735 | ;; If we don't have a window yet, try a fallback method. All | |
5736 | ;; specifiers have been used up by now. | |
5737 | (or (and (window-live-p window) window) | |
5738 | ;; Try reusing a window showing BUFFER on any visible or | |
5739 | ;; iconfied frame. | |
355f2e07 | 5740 | (display-buffer-reuse-window buffer `(nil ,buffer 0)) |
9481c002 MR |
5741 | ;; Try reusing a window not showing BUFFER on any visible or |
5742 | ;; iconified frame. | |
5743 | (display-buffer-reuse-window buffer '(nil other 0)) | |
355f2e07 MR |
5744 | ;; Eli says it's better to never try making a new frame. |
5745 | ;; (display-buffer-pop-up-frame buffer) | |
baa1c9ab | 5746 | ;; Try using a weakly dedicated window. |
9481c002 MR |
5747 | (display-buffer-reuse-window |
5748 | buffer '(nil nil t) '((reuse-window-dedicated . weak))) | |
baa1c9ab | 5749 | ;; Try using a strongly dedicated window. |
9481c002 MR |
5750 | (display-buffer-reuse-window |
5751 | buffer '(nil nil t) '((reuse-window-dedicated . t))))))) | |
5752 | ||
9173deec | 5753 | (defsubst display-buffer-same-window (&optional buffer-or-name label) |
9481c002 MR |
5754 | "Display buffer specified by BUFFER-OR-NAME in the selected window. |
5755 | Another window will be used only if the buffer can't be shown in | |
5756 | the selected window, usually because it is dedicated to another | |
5757 | buffer. Optional argument BUFFER-OR-NAME and LABEL are as for | |
5758 | `display-buffer'." | |
5759 | (interactive "BDisplay buffer in same window:\nP") | |
5760 | (display-buffer buffer-or-name 'same-window label)) | |
5761 | ||
9173deec | 5762 | (defsubst display-buffer-same-frame (&optional buffer-or-name label) |
9481c002 MR |
5763 | "Display buffer specified by BUFFER-OR-NAME in a window on the same frame. |
5764 | Another frame will be used only if there is no other choice. | |
5765 | Optional argument BUFFER-OR-NAME and LABEL are as for | |
5766 | `display-buffer'." | |
5767 | (interactive "BDisplay buffer on same frame:\nP") | |
5768 | (display-buffer buffer-or-name 'same-frame label)) | |
5769 | ||
9173deec | 5770 | (defsubst display-buffer-other-window (&optional buffer-or-name label) |
9481c002 MR |
5771 | "Display buffer specified by BUFFER-OR-NAME in another window. |
5772 | The selected window will be used only if there is no other | |
5773 | choice. Windows on the selected frame are preferred to windows | |
5774 | on other frames. Optional argument BUFFER-OR-NAME and LABEL are as | |
5775 | for `display-buffer'." | |
5776 | (interactive "BDisplay buffer in another window:\nP") | |
5777 | (display-buffer buffer-or-name 'other-window label)) | |
5778 | ||
9173deec | 5779 | (defun display-buffer-same-frame-other-window (&optional buffer-or-name label) |
9481c002 MR |
5780 | "Display buffer specified by BUFFER-OR-NAME in another window on the same frame. |
5781 | The selected window or another frame will be used only if there | |
5782 | is no other choice. Optional argument BUFFER-OR-NAME and LABEL are | |
5783 | as for `display-buffer'." | |
5784 | (interactive "BDisplay buffer in another window on same frame:\nP") | |
5785 | (display-buffer buffer-or-name 'same-frame-other-window label)) | |
5786 | ||
5787 | (defun display-buffer-other-frame (&optional buffer-or-name label) | |
5788 | "Display buffer specified by BUFFER-OR-NAME on another frame. | |
5789 | The selected frame will be used only if there is no other choice. | |
5790 | Optional argument BUFFER-OR-NAME and LABEL are as for | |
5791 | `display-buffer'. | |
5792 | ||
5793 | If this command uses another frame, it will also select that frame." | |
5794 | (interactive "BDisplay buffer in other frame: ") | |
5795 | (display-buffer buffer-or-name 'other-frame label)) | |
5796 | ||
5797 | (defun pop-to-buffer (&optional buffer-or-name specifiers norecord label) | |
5798 | "Display buffer specified by BUFFER-OR-NAME and select the window used. | |
5799 | Optional argument BUFFER-OR-NAME may be a buffer, a string \(a | |
5800 | buffer name), or nil. If BUFFER-OR-NAME is a string not naming | |
5801 | an existent buffer, create a buffer with that name. If | |
5802 | BUFFER-OR-NAME is nil or omitted, display the current buffer. | |
5803 | Interactively, prompt for the buffer name using the minibuffer. | |
5804 | ||
5805 | Optional second argument SPECIFIERS must be a list of buffer | |
5806 | display specifiers, a single location specifier, `t' which means | |
5807 | the latter means to display the buffer in any but the selected | |
5808 | window, or nil which means to exclusively apply the specifiers | |
5809 | customized by the user. | |
5810 | ||
5811 | Optional argument NORECORD non-nil means do not put the buffer | |
5812 | specified by BUFFER-OR-NAME at the front of the buffer list and | |
5813 | do not make the window displaying it the most recently selected | |
5814 | one. | |
5815 | ||
5816 | The optional argument LABEL, if non-nil, is a symbol specifying the | |
5817 | display purpose. Applications should set this when the buffer | |
5818 | shall be displayed in a special way but BUFFER-OR-NAME does not | |
5819 | identify the buffer as special. Buffers that typically fit into | |
5820 | this category are those whose names have been derived from the | |
5821 | name of the file they are visiting. | |
5822 | ||
5823 | Return the buffer specified by BUFFER-OR-NAME or nil if | |
5824 | displaying the buffer failed. | |
5825 | ||
5826 | This uses the function `display-buffer' as a subroutine; see the | |
5827 | documentations of `display-buffer' and `display-buffer-alist' for | |
5828 | additional information." | |
5829 | (interactive "BPop to buffer:\nP") | |
5386012d | 5830 | (let ((buffer (window-normalize-buffer-to-display buffer-or-name)) |
327c8fb1 MR |
5831 | (old-window (selected-window)) |
5832 | (old-frame (selected-frame)) | |
5833 | new-window new-frame) | |
9481c002 | 5834 | (set-buffer buffer) |
327c8fb1 MR |
5835 | (setq new-window (display-buffer buffer specifiers label)) |
5836 | (unless (eq new-window old-window) | |
5837 | ;; `display-buffer' has chosen another window, select it. | |
5838 | (select-window new-window norecord) | |
5839 | (setq new-frame (window-frame new-window)) | |
5840 | (unless (eq new-frame old-frame) | |
5841 | ;; `display-buffer' has chosen another frame, make sure it gets | |
5842 | ;; input focus and is risen. | |
5843 | (select-frame-set-input-focus new-frame))) | |
5844 | ||
5845 | buffer)) | |
9481c002 MR |
5846 | |
5847 | (defsubst pop-to-buffer-same-window (&optional buffer-or-name norecord label) | |
5848 | "Pop to buffer specified by BUFFER-OR-NAME in the selected window. | |
5849 | Another window will be used only if the buffer can't be shown in | |
5850 | the selected window, usually because it is dedicated to another | |
5851 | buffer. Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are | |
5852 | as for `pop-to-buffer'." | |
5853 | (interactive "BPop to buffer in selected window:\nP") | |
5854 | (pop-to-buffer buffer-or-name 'same-window norecord label)) | |
5855 | ||
9173deec | 5856 | (defsubst pop-to-buffer-same-frame (&optional buffer-or-name norecord label) |
9481c002 MR |
5857 | "Pop to buffer specified by BUFFER-OR-NAME in a window on the selected frame. |
5858 | Another frame will be used only if there is no other choice. | |
5859 | Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for | |
5860 | `pop-to-buffer'." | |
5861 | (interactive "BPop to buffer on same frame:\nP") | |
5862 | (pop-to-buffer buffer-or-name 'same-frame norecord label)) | |
5863 | ||
5864 | (defsubst pop-to-buffer-other-window (&optional buffer-or-name norecord label) | |
5865 | "Pop to buffer specified by BUFFER-OR-NAME in another window. | |
5866 | The selected window will be used only if there is no other | |
5867 | choice. Windows on the selected frame are preferred to windows | |
5868 | on other frames. Optional arguments BUFFER-OR-NAME, NORECORD and | |
5869 | LABEL are as for `pop-to-buffer'." | |
5870 | (interactive "BPop to buffer in another window:\nP") | |
5871 | (pop-to-buffer buffer-or-name 'other-window norecord)) | |
5872 | ||
9173deec | 5873 | (defsubst pop-to-buffer-same-frame-other-window (&optional buffer-or-name norecord label) |
9481c002 MR |
5874 | "Pop to buffer specified by BUFFER-OR-NAME in another window on the selected frame. |
5875 | The selected window or another frame will be used only if there | |
5876 | is no other choice. Optional arguments BUFFER-OR-NAME, NORECORD | |
5877 | and LABEL are as for `pop-to-buffer'." | |
5878 | (interactive "BPop to buffer in another window on same frame:\nP") | |
5879 | (pop-to-buffer buffer-or-name 'same-frame-other-window norecord label)) | |
5880 | ||
5881 | (defsubst pop-to-buffer-other-frame (&optional buffer-or-name norecord label) | |
5882 | "Pop to buffer specified by BUFFER-OR-NAME on another frame. | |
5883 | The selected frame will be used only if there's no other choice. | |
5884 | Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for | |
5885 | `pop-to-buffer'." | |
5886 | (interactive "BPop to buffer on another frame:\nP") | |
5887 | (pop-to-buffer buffer-or-name 'other-frame norecord label)) | |
5888 | ||
5889 | (defun read-buffer-to-switch (prompt) | |
5890 | "Read the name of a buffer to switch to, prompting with PROMPT. | |
5891 | Return the neame of the buffer as a string. | |
5892 | ||
5893 | This function is intended for the `switch-to-buffer' family of | |
5894 | commands since these need to omit the name of the current buffer | |
5895 | from the list of completions and default values." | |
5896 | (let ((rbts-completion-table (internal-complete-buffer-except))) | |
5897 | (minibuffer-with-setup-hook | |
5898 | (lambda () | |
5899 | (setq minibuffer-completion-table rbts-completion-table) | |
5900 | ;; Since rbts-completion-table is built dynamically, we | |
5901 | ;; can't just add it to the default value of | |
5902 | ;; icomplete-with-completion-tables, so we add it | |
5903 | ;; here manually. | |
5904 | (if (and (boundp 'icomplete-with-completion-tables) | |
5905 | (listp icomplete-with-completion-tables)) | |
5906 | (set (make-local-variable 'icomplete-with-completion-tables) | |
5907 | (cons rbts-completion-table | |
5908 | icomplete-with-completion-tables)))) | |
5909 | (read-buffer prompt (other-buffer (current-buffer)) | |
5910 | (confirm-nonexistent-file-or-buffer))))) | |
5911 | ||
5386012d | 5912 | (defun window-normalize-buffer-to-switch-to (buffer-or-name) |
9481c002 MR |
5913 | "Normalize BUFFER-OR-NAME argument of buffer switching functions. |
5914 | If BUFFER-OR-NAME is nil, return the buffer returned by | |
5915 | `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME | |
5916 | exists, return that buffer. If no such buffer exists, create a | |
5917 | buffer with the name BUFFER-OR-NAME and return that buffer." | |
5918 | (if buffer-or-name | |
5919 | (or (get-buffer buffer-or-name) | |
5920 | (let ((buffer (get-buffer-create buffer-or-name))) | |
5921 | (set-buffer-major-mode buffer) | |
5922 | buffer)) | |
5923 | (other-buffer))) | |
5924 | ||
5925 | (defun switch-to-buffer (buffer-or-name &optional norecord) | |
5926 | "Switch to buffer BUFFER-OR-NAME in the selected window. | |
5927 | If called interactively, prompt for the buffer name using the | |
5928 | minibuffer. The variable `confirm-nonexistent-file-or-buffer' | |
5929 | determines whether to request confirmation before creating a new | |
5930 | buffer. | |
5931 | ||
5932 | BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or | |
5933 | nil. If BUFFER-OR-NAME is a string that does not identify an | |
5934 | existing buffer, create a buffer with that name. If | |
5935 | BUFFER-OR-NAME is nil, switch to the buffer returned by | |
5936 | `other-buffer'. | |
5937 | ||
5938 | Optional argument NORECORD non-nil means do not put the buffer | |
5939 | specified by BUFFER-OR-NAME at the front of the buffer list and | |
5940 | do not make the window displaying it the most recently selected | |
5941 | one. Return the buffer switched to. | |
5942 | ||
5943 | This function is intended for interactive use only. Lisp | |
5944 | functions should call `pop-to-buffer-same-window' instead." | |
5945 | (interactive | |
5946 | (list (read-buffer-to-switch "Switch to buffer: "))) | |
5386012d | 5947 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) |
0e61a35f SM |
5948 | (cond |
5949 | ;; Don't call set-window-buffer if it's not needed since it | |
5950 | ;; might signal an error (e.g. if the window is dedicated). | |
5951 | ((eq buffer (window-buffer)) nil) | |
5952 | ((window-minibuffer-p) | |
5953 | (error "Cannot switch buffers in minibuffer window")) | |
5954 | ((eq (window-dedicated-p) t) | |
5955 | (error "Cannot switch buffers in a dedicated window")) | |
5956 | (t (set-window-buffer nil buffer))) | |
5957 | (unless norecord | |
5958 | (select-window (selected-window))) | |
5959 | (set-buffer buffer))) | |
9481c002 MR |
5960 | |
5961 | (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord) | |
5962 | "Switch to buffer BUFFER-OR-NAME in a window on the selected frame. | |
5963 | Another frame will be used only if there is no other choice. | |
981d5c09 MR |
5964 | Arguments BUFFER-OR-NAME and NORECORD have the same meaning as |
5965 | for `switch-to-buffer'. | |
9481c002 MR |
5966 | |
5967 | This function is intended for interactive use only. Lisp | |
5968 | functions should call `pop-to-buffer-same-frame' instead." | |
5969 | (interactive | |
5970 | (list (read-buffer-to-switch "Switch to buffer in other window: "))) | |
5386012d | 5971 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) |
9481c002 MR |
5972 | (pop-to-buffer buffer 'same-frame norecord))) |
5973 | ||
5974 | (defun switch-to-buffer-other-window (buffer-or-name &optional norecord) | |
5975 | "Switch to buffer BUFFER-OR-NAME in another window. | |
5976 | The selected window will be used only if there is no other | |
5977 | choice. Windows on the selected frame are preferred to windows | |
981d5c09 MR |
5978 | on other frames. Arguments BUFFER-OR-NAME and NORECORD have the |
5979 | same meaning as for `switch-to-buffer'. | |
9481c002 MR |
5980 | |
5981 | This function is intended for interactive use only. Lisp | |
5982 | functions should call `pop-to-buffer-other-window' instead." | |
5983 | (interactive | |
5984 | (list (read-buffer-to-switch "Switch to buffer in other window: "))) | |
5386012d | 5985 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) |
9481c002 MR |
5986 | (pop-to-buffer buffer 'other-window norecord))) |
5987 | ||
5988 | (defun switch-to-buffer-other-window-same-frame (buffer-or-name &optional norecord) | |
5989 | "Switch to buffer BUFFER-OR-NAME in another window on the selected frame. | |
5990 | The selected window or another frame will be used only if there | |
981d5c09 MR |
5991 | is no other choice. Arguments BUFFER-OR-NAME and NORECORD have |
5992 | the same meaning as for `switch-to-buffer'. | |
9481c002 MR |
5993 | |
5994 | This function is intended for interactive use only. Lisp | |
5995 | functions should call `pop-to-buffer-other-window-same-frame' | |
5996 | instead." | |
5997 | (interactive | |
5998 | (list (read-buffer-to-switch "Switch to buffer in other window: "))) | |
5386012d | 5999 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) |
9481c002 MR |
6000 | (pop-to-buffer buffer 'same-frame-other-window norecord))) |
6001 | ||
6002 | (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord) | |
6003 | "Switch to buffer BUFFER-OR-NAME on another frame. | |
6004 | The same frame will be used only if there is no other choice. | |
981d5c09 MR |
6005 | Arguments BUFFER-OR-NAME and NORECORD have the same meaning |
6006 | as for `switch-to-buffer'. | |
9481c002 MR |
6007 | |
6008 | This function is intended for interactive use only. Lisp | |
6009 | functions should call `pop-to-buffer-other-frame' instead." | |
6010 | (interactive | |
6011 | (list (read-buffer-to-switch "Switch to buffer in other frame: "))) | |
5386012d | 6012 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) |
9481c002 MR |
6013 | (pop-to-buffer buffer 'other-frame norecord))) |
6014 | ||
6015 | ;;; Obsolete definitions of `display-buffer' below. | |
6016 | (defcustom same-window-buffer-names nil | |
6017 | "List of names of buffers that should appear in the \"same\" window. | |
6018 | `display-buffer' and `pop-to-buffer' show a buffer whose name is | |
6019 | on this list in the selected rather than some other window. | |
6020 | ||
6021 | An element of this list can be a cons cell instead of just a | |
6022 | string. In that case, the cell's car must be a string specifying | |
6023 | the buffer name. This is for compatibility with | |
6024 | `special-display-buffer-names'; the cdr of the cons cell is | |
6025 | ignored. | |
6026 | ||
6027 | See also `same-window-regexps'." | |
6028 | :type '(repeat (string :format "%v")) | |
6029 | :group 'windows) | |
0bf4ba9a MR |
6030 | ;; (make-obsolete-variable |
6031 | ;; 'same-window-buffer-names | |
6032 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 MR |
6033 | |
6034 | (defcustom same-window-regexps nil | |
6035 | "List of regexps saying which buffers should appear in the \"same\" window. | |
6036 | `display-buffer' and `pop-to-buffer' show a buffer whose name | |
6037 | matches a regexp on this list in the selected rather than some | |
6038 | other window. | |
6039 | ||
6040 | An element of this list can be a cons cell instead of just a | |
6041 | string. In that case, the cell's car must be a regexp matching | |
6042 | the buffer name. This is for compatibility with | |
6043 | `special-display-regexps'; the cdr of the cons cell is ignored. | |
6044 | ||
6045 | See also `same-window-buffer-names'." | |
6046 | :type '(repeat (regexp :format "%v")) | |
3c448ab6 | 6047 | :group 'windows) |
0bf4ba9a MR |
6048 | ;; (make-obsolete-variable |
6049 | ;; 'same-window-regexps | |
6050 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 MR |
6051 | |
6052 | (defun same-window-p (buffer-name) | |
6053 | "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window. | |
6054 | This function returns non-nil if `display-buffer' or | |
6055 | `pop-to-buffer' would show a buffer named BUFFER-NAME in the | |
6056 | selected rather than \(as usual\) some other window. See | |
6057 | `same-window-buffer-names' and `same-window-regexps'." | |
dbad4f69 MR |
6058 | (let ((buffer-names (with-no-warnings same-window-buffer-names)) |
6059 | (regexps (with-no-warnings same-window-regexps))) | |
9481c002 MR |
6060 | (cond |
6061 | ((not (stringp buffer-name))) | |
6062 | ;; The elements of `same-window-buffer-names' can be buffer | |
6063 | ;; names or cons cells whose cars are buffer names. | |
6064 | ((member buffer-name buffer-names)) | |
6065 | ((assoc buffer-name buffer-names)) | |
6066 | ((catch 'found | |
6067 | (dolist (regexp regexps) | |
6068 | ;; The elements of `same-window-regexps' can be regexps | |
6069 | ;; or cons cells whose cars are regexps. | |
6070 | (when (or (and (stringp regexp) | |
6071 | (string-match regexp buffer-name)) | |
6072 | (and (consp regexp) (stringp (car regexp)) | |
6073 | (string-match-p (car regexp) buffer-name))) | |
6074 | (throw 'found t)))))))) | |
0bf4ba9a MR |
6075 | ;; (make-obsolete |
6076 | ;; 'same-window-p "pass argument to buffer display function instead." "24.1") | |
9481c002 MR |
6077 | |
6078 | (defcustom special-display-frame-alist | |
6079 | '((height . 14) (width . 80) (unsplittable . t)) | |
6080 | "Alist of parameters for special frames. | |
6081 | Special frames are used for buffers whose names are listed in | |
6082 | `special-display-buffer-names' and for buffers whose names match | |
6083 | one of the regular expressions in `special-display-regexps'. | |
6084 | ||
6085 | This variable can be set in your init file, like this: | |
6086 | ||
6087 | (setq special-display-frame-alist '((width . 80) (height . 20))) | |
6088 | ||
6089 | These supersede the values given in `default-frame-alist'." | |
6090 | :type '(repeat (cons :format "%v" | |
6091 | (symbol :tag "Parameter") | |
6092 | (sexp :tag "Value"))) | |
6093 | :group 'frames) | |
0bf4ba9a MR |
6094 | ;; (make-obsolete-variable |
6095 | ;; 'special-display-frame-alist | |
6096 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 MR |
6097 | |
6098 | (defun special-display-popup-frame (buffer &optional args) | |
6099 | "Display BUFFER in a special frame and return the window chosen. | |
6100 | If BUFFER is already displayed in a visible or iconified frame, | |
6101 | raise that frame. Otherwise, display BUFFER in a way as | |
6102 | specified by optional argument ARGS. | |
6103 | ||
6104 | If ARGS is an alist, use it as a list of frame parameters. If | |
6105 | these parameters contain \(same-window . t), display BUFFER in | |
6106 | the selected window. If they contain \(same-frame . t), display | |
6107 | BUFFER in a window on the selected frame. | |
6108 | ||
6109 | If ARGS is a list whose car is a symbol, use (car ARGS) as a | |
6110 | function to do the work. Pass it BUFFER as first argument, | |
322b7dab | 6111 | and (cdr ARGS) as the rest of the arguments." |
9481c002 MR |
6112 | (if (and args (symbolp (car args))) |
6113 | (apply (car args) buffer (cdr args)) | |
6114 | (let ((window (get-buffer-window buffer 0))) | |
6115 | (or | |
6116 | ;; If we have a window already, make it visible. | |
6117 | (when window | |
6118 | (let ((frame (window-frame window))) | |
6119 | (make-frame-visible frame) | |
6120 | (raise-frame frame) | |
6121 | window)) | |
6122 | ;; Reuse the current window if the user requested it. | |
6123 | (when (cdr (assq 'same-window args)) | |
6124 | (display-buffer-reuse-window | |
be3fb2b8 | 6125 | buffer '(same nil nil) '((reuse-dedicated . weak)))) |
9481c002 MR |
6126 | ;; Stay on the same frame if requested. |
6127 | (when (or (cdr (assq 'same-frame args)) | |
6128 | (cdr (assq 'same-window args))) | |
6129 | (or (display-buffer-pop-up-window | |
6130 | buffer '((largest . nil) (lru . nil))) | |
6131 | (display-buffer-reuse-window | |
6132 | buffer '(nil nil nil)))) | |
6133 | ;; If no window yet, make one in a new frame. | |
6134 | (let ((frame | |
6135 | (with-current-buffer buffer | |
6136 | (make-frame | |
6137 | (append args (with-no-warnings | |
6138 | special-display-frame-alist)))))) | |
6139 | (set-window-buffer (frame-selected-window frame) buffer) | |
6140 | (set-window-dedicated-p (frame-selected-window frame) t) | |
6141 | (frame-selected-window frame)))))) | |
0bf4ba9a MR |
6142 | ;; (make-obsolete |
6143 | ;; 'special-display-popup-frame | |
6144 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 MR |
6145 | |
6146 | (defcustom special-display-function 'special-display-popup-frame | |
6147 | "Function to call for displaying special buffers. | |
6148 | This function is called with two arguments - the buffer and, | |
6149 | optionally, a list - and should return a window displaying that | |
6150 | buffer. The default value usually makes a separate frame for the | |
6151 | buffer using `special-display-frame-alist' to specify the frame | |
6152 | parameters. See the definition of `special-display-popup-frame' | |
6153 | for how to specify such a function. | |
6154 | ||
6155 | A buffer is special when its name is either listed in | |
6156 | `special-display-buffer-names' or matches a regexp in | |
6157 | `special-display-regexps'." | |
6158 | :type 'function | |
6159 | :group 'windows | |
6160 | :group 'frames) | |
0bf4ba9a MR |
6161 | ;; (make-obsolete-variable |
6162 | ;; 'special-display-function | |
6163 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6164 | |
56f31926 MR |
6165 | (defcustom special-display-buffer-names nil |
6166 | "List of names of buffers that should be displayed specially. | |
6167 | Displaying a buffer with `display-buffer' or `pop-to-buffer', if | |
6168 | its name is in this list, displays the buffer in a way specified | |
6169 | by `special-display-function'. `special-display-popup-frame' | |
6170 | \(the default for `special-display-function') usually displays | |
6171 | the buffer in a separate frame made with the parameters specified | |
6172 | by `special-display-frame-alist'. If `special-display-function' | |
6173 | has been set to some other function, that function is called with | |
6174 | the buffer as first, and nil as second argument. | |
6175 | ||
6176 | Alternatively, an element of this list can be specified as | |
6177 | \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer | |
6178 | name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs. | |
6179 | `special-display-popup-frame' will interpret such pairs as frame | |
6180 | parameters when it creates a special frame, overriding the | |
6181 | corresponding values from `special-display-frame-alist'. | |
6182 | ||
6183 | As a special case, if FRAME-PARAMETERS contains (same-window . t) | |
6184 | `special-display-popup-frame' displays that buffer in the | |
6185 | selected window. If FRAME-PARAMETERS contains (same-frame . t), | |
6186 | it displays that buffer in a window on the selected frame. | |
6187 | ||
6188 | If `special-display-function' specifies some other function than | |
6189 | `special-display-popup-frame', that function is called with the | |
6190 | buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second | |
6191 | argument. | |
6192 | ||
6193 | Finally, an element of this list can be also specified as | |
6194 | \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case, | |
6195 | `special-display-popup-frame' will call FUNCTION with the buffer | |
6196 | named BUFFER-NAME as first argument, and OTHER-ARGS as the | |
6197 | second. If `special-display-function' specifies some other | |
6198 | function, that function is called with the buffer named | |
6199 | BUFFER-NAME as first, and the element's cdr as second argument. | |
6200 | ||
6201 | If this variable appears \"not to work\", because you added a | |
6202 | name to it but the corresponding buffer is displayed in the | |
6203 | selected window, look at the values of `same-window-buffer-names' | |
6204 | and `same-window-regexps'. Those variables take precedence over | |
6205 | this one. | |
6206 | ||
6207 | See also `special-display-regexps'." | |
6208 | :type '(repeat | |
6209 | (choice :tag "Buffer" | |
6210 | :value "" | |
6211 | (string :format "%v") | |
6212 | (cons :tag "With parameters" | |
6213 | :format "%v" | |
6214 | :value ("" . nil) | |
6215 | (string :format "%v") | |
6216 | (repeat :tag "Parameters" | |
6217 | (cons :format "%v" | |
6218 | (symbol :tag "Parameter") | |
6219 | (sexp :tag "Value")))) | |
6220 | (list :tag "With function" | |
6221 | :format "%v" | |
6222 | :value ("" . nil) | |
6223 | (string :format "%v") | |
6224 | (function :tag "Function") | |
6225 | (repeat :tag "Arguments" (sexp))))) | |
6226 | :group 'windows | |
6227 | :group 'frames) | |
0bf4ba9a MR |
6228 | ;; (make-obsolete-variable |
6229 | ;; 'special-display-buffer-names | |
6230 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
56f31926 | 6231 | |
ac549fa5 GM |
6232 | ;;;###autoload |
6233 | (put 'special-display-buffer-names 'risky-local-variable t) | |
6234 | ||
56f31926 MR |
6235 | (defcustom special-display-regexps nil |
6236 | "List of regexps saying which buffers should be displayed specially. | |
6237 | Displaying a buffer with `display-buffer' or `pop-to-buffer', if | |
6238 | any regexp in this list matches its name, displays it specially | |
6239 | using `special-display-function'. `special-display-popup-frame' | |
6240 | \(the default for `special-display-function') usually displays | |
6241 | the buffer in a separate frame made with the parameters specified | |
6242 | by `special-display-frame-alist'. If `special-display-function' | |
6243 | has been set to some other function, that function is called with | |
6244 | the buffer as first, and nil as second argument. | |
6245 | ||
6246 | Alternatively, an element of this list can be specified as | |
6247 | \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and | |
6248 | FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs. | |
6249 | `special-display-popup-frame' will then interpret these pairs as | |
6250 | frame parameters when creating a special frame for a buffer whose | |
6251 | name matches REGEXP, overriding the corresponding values from | |
6252 | `special-display-frame-alist'. | |
6253 | ||
6254 | As a special case, if FRAME-PARAMETERS contains (same-window . t) | |
6255 | `special-display-popup-frame' displays buffers matching REGEXP in | |
6256 | the selected window. \(same-frame . t) in FRAME-PARAMETERS means | |
6257 | to display such buffers in a window on the selected frame. | |
6258 | ||
6259 | If `special-display-function' specifies some other function than | |
6260 | `special-display-popup-frame', that function is called with the | |
6261 | buffer whose name matched REGEXP as first, and FRAME-PARAMETERS | |
6262 | as second argument. | |
6263 | ||
6264 | Finally, an element of this list can be also specified as | |
6265 | \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame' | |
6266 | will then call FUNCTION with the buffer whose name matched | |
6267 | REGEXP as first, and OTHER-ARGS as second argument. If | |
6268 | `special-display-function' specifies some other function, that | |
6269 | function is called with the buffer whose name matched REGEXP | |
6270 | as first, and the element's cdr as second argument. | |
6271 | ||
6272 | If this variable appears \"not to work\", because you added a | |
6273 | name to it but the corresponding buffer is displayed in the | |
6274 | selected window, look at the values of `same-window-buffer-names' | |
6275 | and `same-window-regexps'. Those variables take precedence over | |
6276 | this one. | |
6277 | ||
6278 | See also `special-display-buffer-names'." | |
6279 | :type '(repeat | |
6280 | (choice :tag "Buffer" | |
6281 | :value "" | |
6282 | (regexp :format "%v") | |
6283 | (cons :tag "With parameters" | |
6284 | :format "%v" | |
6285 | :value ("" . nil) | |
6286 | (regexp :format "%v") | |
6287 | (repeat :tag "Parameters" | |
6288 | (cons :format "%v" | |
6289 | (symbol :tag "Parameter") | |
6290 | (sexp :tag "Value")))) | |
6291 | (list :tag "With function" | |
6292 | :format "%v" | |
6293 | :value ("" . nil) | |
6294 | (regexp :format "%v") | |
6295 | (function :tag "Function") | |
6296 | (repeat :tag "Arguments" (sexp))))) | |
6297 | :group 'windows | |
6298 | :group 'frames) | |
0bf4ba9a MR |
6299 | ;; (make-obsolete-variable |
6300 | ;; 'special-display-regexps | |
6301 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
56f31926 | 6302 | |
3c448ab6 MR |
6303 | (defun special-display-p (buffer-name) |
6304 | "Return non-nil if a buffer named BUFFER-NAME gets a special frame. | |
56f31926 MR |
6305 | More precisely, return t if `special-display-buffer-names' or |
6306 | `special-display-regexps' contain a string entry equaling or | |
6307 | matching BUFFER-NAME. If `special-display-buffer-names' or | |
6308 | `special-display-regexps' contain a list entry whose car equals | |
6309 | or matches BUFFER-NAME, the return value is the cdr of that | |
6310 | entry." | |
9481c002 MR |
6311 | (let ((buffer-names (with-no-warnings special-display-buffer-names)) |
6312 | (regexps (with-no-warnings special-display-regexps)) | |
6313 | tmp) | |
98722073 MR |
6314 | (cond |
6315 | ((not (stringp buffer-name))) | |
9481c002 | 6316 | ((member buffer-name buffer-names) |
98722073 | 6317 | t) |
9481c002 | 6318 | ((setq tmp (assoc buffer-name buffer-names)) |
98722073 MR |
6319 | (cdr tmp)) |
6320 | ((catch 'found | |
9481c002 | 6321 | (dolist (regexp regexps) |
98722073 MR |
6322 | (cond |
6323 | ((stringp regexp) | |
6324 | (when (string-match-p regexp buffer-name) | |
6325 | (throw 'found t))) | |
6326 | ((and (consp regexp) (stringp (car regexp)) | |
6327 | (string-match-p (car regexp) buffer-name)) | |
6328 | (throw 'found (cdr regexp)))))))))) | |
0bf4ba9a MR |
6329 | ;; (make-obsolete |
6330 | ;; 'special-display-p | |
6331 | ;; "pass argument to buffer display function instead." "24.1") | |
9481c002 MR |
6332 | |
6333 | (defcustom pop-up-frame-alist nil | |
6334 | "Alist of parameters for automatically generated new frames. | |
6335 | You can set this in your init file; for example, | |
6336 | ||
6337 | (setq pop-up-frame-alist '((width . 80) (height . 20))) | |
6338 | ||
6339 | If non-nil, the value you specify here is used by the default | |
6340 | `pop-up-frame-function' for the creation of new frames. | |
6341 | ||
6342 | Since `pop-up-frame-function' is used by `display-buffer' for | |
6343 | making new frames, any value specified here by default affects | |
6344 | the automatic generation of new frames via `display-buffer' and | |
6345 | all functions based on it. The behavior of `make-frame' is not | |
6346 | affected by this variable." | |
6347 | :type '(repeat (cons :format "%v" | |
6348 | (symbol :tag "Parameter") | |
6349 | (sexp :tag "Value"))) | |
6350 | :group 'frames) | |
0bf4ba9a MR |
6351 | ;; (make-obsolete-variable |
6352 | ;; 'pop-up-frame-alist | |
6353 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 MR |
6354 | |
6355 | (defcustom pop-up-frame-function | |
6356 | (lambda () (make-frame pop-up-frame-alist)) | |
6357 | "Function used by `display-buffer' for creating a new frame. | |
6358 | This function is called with no arguments and should return a new | |
6359 | frame. The default value calls `make-frame' with the argument | |
6360 | `pop-up-frame-alist'." | |
3c448ab6 MR |
6361 | :type 'function |
6362 | :group 'frames) | |
0bf4ba9a MR |
6363 | ;; (make-obsolete-variable |
6364 | ;; 'pop-up-frame-function | |
6365 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6366 | |
d1067961 | 6367 | (defcustom pop-up-frames nil |
3c448ab6 | 6368 | "Whether `display-buffer' should make a separate frame. |
d1f18ec0 | 6369 | If nil, never make a separate frame. |
3c448ab6 MR |
6370 | If the value is `graphic-only', make a separate frame |
6371 | on graphic displays only. | |
6372 | Any other non-nil value means always make a separate frame." | |
6373 | :type '(choice | |
6374 | (const :tag "Never" nil) | |
6375 | (const :tag "On graphic displays only" graphic-only) | |
6376 | (const :tag "Always" t)) | |
9481c002 MR |
6377 | :group 'windows |
6378 | :group 'frames) | |
0bf4ba9a MR |
6379 | ;; (make-obsolete-variable |
6380 | ;; 'pop-up-frames | |
6381 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6382 | |
d1067961 | 6383 | (defcustom display-buffer-reuse-frames nil |
9481c002 | 6384 | "Set and non-nil means `display-buffer' should reuse frames. |
3c448ab6 MR |
6385 | If the buffer in question is already displayed in a frame, raise |
6386 | that frame." | |
6387 | :type 'boolean | |
d1067961 | 6388 | :version "21.1" |
9481c002 MR |
6389 | :group 'windows |
6390 | :group 'frames) | |
0bf4ba9a MR |
6391 | ;; (make-obsolete-variable |
6392 | ;; 'display-buffer-reuse-frames | |
6393 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6394 | |
9481c002 MR |
6395 | (defcustom pop-up-windows 'unset ; t |
6396 | "Set and non-nil means `display-buffer' should make a new window." | |
3c448ab6 | 6397 | :type 'boolean |
9481c002 | 6398 | :version "24.1" |
3c448ab6 | 6399 | :group 'windows) |
0bf4ba9a MR |
6400 | ;; (make-obsolete-variable |
6401 | ;; 'pop-up-windows | |
6402 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6403 | |
8b10a2d1 | 6404 | (defcustom split-window-preferred-function 'split-window-sensibly |
9481c002 | 6405 | "Function called by `display-buffer' to split a window. |
8b10a2d1 MR |
6406 | This function is called with a window as single argument and is |
6407 | supposed to split that window and return the new window. If the | |
6408 | window can (or shall) not be split, it is supposed to return nil. | |
9481c002 | 6409 | |
8b10a2d1 MR |
6410 | The default is to call the function `split-window-sensibly' which |
6411 | tries to split the window in a way which seems most suitable. | |
6412 | You can customize the options `split-height-threshold' and/or | |
6413 | `split-width-threshold' in order to have `split-window-sensibly' | |
6414 | prefer either vertical or horizontal splitting. | |
6415 | ||
9481c002 MR |
6416 | If you set this to any other function, bear in mind that |
6417 | `display-buffer' may call that function repeatedly; the option | |
6418 | `pop-up-windows' controls which windows may become the argument | |
6419 | of this function. | |
8b10a2d1 MR |
6420 | |
6421 | The window selected at the time `display-buffer' was invoked is | |
6422 | still selected when this function is called. Hence you can | |
6423 | compare the window argument with the value of `selected-window' | |
6424 | if you intend to split the selected window instead or if you do | |
6425 | not want to split the selected window." | |
6426 | :type 'function | |
3c448ab6 MR |
6427 | :version "23.1" |
6428 | :group 'windows) | |
0bf4ba9a MR |
6429 | ;; (make-obsolete-variable |
6430 | ;; 'split-window-preferred-function | |
6431 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6432 | |
8b10a2d1 | 6433 | (defcustom split-height-threshold 80 |
9481c002 MR |
6434 | "Minimum height for splitting a window to display a buffer. |
6435 | If this is an integer, `display-buffer' can split a window | |
8b10a2d1 | 6436 | vertically only if it has at least this many lines. If this is |
9481c002 MR |
6437 | nil, `display-buffer' does not split windows vertically. If a |
6438 | window is the only window on its frame, `display-buffer' may | |
6439 | split it vertically disregarding the value of this variable." | |
8b10a2d1 | 6440 | :type '(choice (const nil) (integer :tag "lines")) |
3c448ab6 MR |
6441 | :version "23.1" |
6442 | :group 'windows) | |
0bf4ba9a MR |
6443 | ;; (make-obsolete-variable |
6444 | ;; 'split-height-threshold | |
6445 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6446 | |
8b10a2d1 | 6447 | (defcustom split-width-threshold 160 |
9481c002 MR |
6448 | "Minimum width for splitting a window to display a buffer. |
6449 | If this is an integer, `display-buffer' can split a window | |
8b10a2d1 | 6450 | horizontally only if it has at least this many columns. If this |
9481c002 | 6451 | is nil, `display-buffer' cannot split windows horizontally." |
8b10a2d1 | 6452 | :type '(choice (const nil) (integer :tag "columns")) |
3c448ab6 MR |
6453 | :version "23.1" |
6454 | :group 'windows) | |
0bf4ba9a MR |
6455 | ;; (make-obsolete-variable |
6456 | ;; 'split-width-threshold | |
6457 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 | 6458 | |
d1067961 MR |
6459 | (defcustom even-window-heights 'unset ; t |
6460 | "If set and non-nil `display-buffer' will try to even window heights. | |
9481c002 MR |
6461 | Otherwise `display-buffer' will leave the window configuration |
6462 | alone. Heights are evened only when `display-buffer' reuses a | |
6463 | window that appears above or below the selected window." | |
6464 | :type 'boolean | |
d1067961 | 6465 | :version "24.1" |
9481c002 | 6466 | :group 'windows) |
0bf4ba9a MR |
6467 | ;; (make-obsolete-variable |
6468 | ;; 'even-window-heights | |
6469 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 | 6470 | |
d1067961 MR |
6471 | (defvar display-buffer-mark-dedicated nil |
6472 | "Non-nil means `display-buffer' marks the windows it creates as dedicated. | |
9481c002 MR |
6473 | The actual non-nil value of this variable will be copied to the |
6474 | `window-dedicated-p' flag.") | |
0bf4ba9a MR |
6475 | ;; (make-obsolete-variable |
6476 | ;; 'display-buffer-mark-dedicated | |
6477 | ;; "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6478 | |
8b10a2d1 MR |
6479 | (defun window-splittable-p (window &optional horizontal) |
6480 | "Return non-nil if `split-window-sensibly' may split WINDOW. | |
6481 | Optional argument HORIZONTAL nil or omitted means check whether | |
6482 | `split-window-sensibly' may split WINDOW vertically. HORIZONTAL | |
6483 | non-nil means check whether WINDOW may be split horizontally. | |
3c448ab6 | 6484 | |
8b10a2d1 | 6485 | WINDOW may be split vertically when the following conditions |
3c448ab6 | 6486 | hold: |
3c448ab6 MR |
6487 | - `window-size-fixed' is either nil or equals `width' for the |
6488 | buffer of WINDOW. | |
8b10a2d1 | 6489 | - `split-height-threshold' is an integer and WINDOW is at least as |
3c448ab6 | 6490 | high as `split-height-threshold'. |
3c448ab6 MR |
6491 | - When WINDOW is split evenly, the emanating windows are at least |
6492 | `window-min-height' lines tall and can accommodate at least one | |
6493 | line plus - if WINDOW has one - a mode line. | |
6494 | ||
8b10a2d1 | 6495 | WINDOW may be split horizontally when the following conditions |
3c448ab6 | 6496 | hold: |
3c448ab6 MR |
6497 | - `window-size-fixed' is either nil or equals `height' for the |
6498 | buffer of WINDOW. | |
8b10a2d1 | 6499 | - `split-width-threshold' is an integer and WINDOW is at least as |
3c448ab6 | 6500 | wide as `split-width-threshold'. |
3c448ab6 MR |
6501 | - When WINDOW is split evenly, the emanating windows are at least |
6502 | `window-min-width' or two (whichever is larger) columns wide." | |
6503 | (when (window-live-p window) | |
6504 | (with-current-buffer (window-buffer window) | |
6505 | (if horizontal | |
6506 | ;; A window can be split horizontally when its width is not | |
6507 | ;; fixed, it is at least `split-width-threshold' columns wide | |
6508 | ;; and at least twice as wide as `window-min-width' and 2 (the | |
6509 | ;; latter value is hardcoded). | |
6510 | (and (memq window-size-fixed '(nil height)) | |
6511 | ;; Testing `window-full-width-p' here hardly makes any | |
6512 | ;; sense nowadays. This can be done more intuitively by | |
6513 | ;; setting up `split-width-threshold' appropriately. | |
6514 | (numberp split-width-threshold) | |
6515 | (>= (window-width window) | |
6516 | (max split-width-threshold | |
6517 | (* 2 (max window-min-width 2))))) | |
6518 | ;; A window can be split vertically when its height is not | |
6519 | ;; fixed, it is at least `split-height-threshold' lines high, | |
6520 | ;; and it is at least twice as high as `window-min-height' and 2 | |
6521 | ;; if it has a modeline or 1. | |
6522 | (and (memq window-size-fixed '(nil width)) | |
6523 | (numberp split-height-threshold) | |
6524 | (>= (window-height window) | |
6525 | (max split-height-threshold | |
6526 | (* 2 (max window-min-height | |
6527 | (if mode-line-format 2 1)))))))))) | |
0bf4ba9a MR |
6528 | ;; (make-obsolete |
6529 | ;; 'window-splittable-p "use 2nd arg of `display-buffer' instead." "24.1") | |
3c448ab6 | 6530 | |
8b10a2d1 MR |
6531 | (defun split-window-sensibly (window) |
6532 | "Split WINDOW in a way suitable for `display-buffer'. | |
6533 | If `split-height-threshold' specifies an integer, WINDOW is at | |
6534 | least `split-height-threshold' lines tall and can be split | |
6535 | vertically, split WINDOW into two windows one above the other and | |
6536 | return the lower window. Otherwise, if `split-width-threshold' | |
6537 | specifies an integer, WINDOW is at least `split-width-threshold' | |
6538 | columns wide and can be split horizontally, split WINDOW into two | |
6539 | windows side by side and return the window on the right. If this | |
6540 | can't be done either and WINDOW is the only window on its frame, | |
6541 | try to split WINDOW vertically disregarding any value specified | |
6542 | by `split-height-threshold'. If that succeeds, return the lower | |
6543 | window. Return nil otherwise. | |
6544 | ||
6545 | By default `display-buffer' routines call this function to split | |
6546 | the largest or least recently used window. To change the default | |
6547 | customize the option `split-window-preferred-function'. | |
6548 | ||
6549 | You can enforce this function to not split WINDOW horizontally, | |
6550 | by setting \(or binding) the variable `split-width-threshold' to | |
6551 | nil. If, in addition, you set `split-height-threshold' to zero, | |
6552 | chances increase that this function does split WINDOW vertically. | |
6553 | ||
6554 | In order to not split WINDOW vertically, set \(or bind) the | |
6555 | variable `split-height-threshold' to nil. Additionally, you can | |
6556 | set `split-width-threshold' to zero to make a horizontal split | |
6557 | more likely to occur. | |
6558 | ||
6559 | Have a look at the function `window-splittable-p' if you want to | |
6560 | know how `split-window-sensibly' determines whether WINDOW can be | |
6561 | split." | |
9481c002 | 6562 | (or (and (with-no-warnings (window-splittable-p window)) |
8b10a2d1 MR |
6563 | ;; Split window vertically. |
6564 | (with-selected-window window | |
6565 | (split-window-vertically))) | |
9481c002 | 6566 | (and (with-no-warnings (window-splittable-p window t)) |
8b10a2d1 MR |
6567 | ;; Split window horizontally. |
6568 | (with-selected-window window | |
6569 | (split-window-horizontally))) | |
6570 | (and (eq window (frame-root-window (window-frame window))) | |
6571 | (not (window-minibuffer-p window)) | |
6572 | ;; If WINDOW is the only window on its frame and is not the | |
6573 | ;; minibuffer window, try to split it vertically disregarding | |
6574 | ;; the value of `split-height-threshold'. | |
6575 | (let ((split-height-threshold 0)) | |
9481c002 | 6576 | (when (with-no-warnings (window-splittable-p window)) |
8b10a2d1 MR |
6577 | (with-selected-window window |
6578 | (split-window-vertically))))))) | |
0bf4ba9a MR |
6579 | ;; (make-obsolete |
6580 | ;; 'split-window-sensibly "use 2nd arg of `display-buffer' instead." "24.1") | |
9481c002 MR |
6581 | |
6582 | ;; Functions for converting Emacs 23 buffer display options to buffer | |
6583 | ;; display specifiers. | |
6584 | (defun display-buffer-alist-add (identifiers specifiers &optional no-custom) | |
6585 | "Helper function for `display-buffer-alist-set'." | |
6586 | (unless identifiers | |
6587 | (setq identifiers '((regexp . ".*")))) | |
6588 | (unless (atom specifiers) | |
6589 | (setq specifiers (delq nil specifiers))) | |
6590 | ||
6591 | (if no-custom | |
6592 | (setq display-buffer-alist | |
6593 | (cons (cons identifiers specifiers) display-buffer-alist)) | |
6594 | (customize-set-variable | |
6595 | 'display-buffer-alist | |
6596 | (cons (cons identifiers specifiers) display-buffer-alist)))) | |
6597 | ||
6598 | (defun display-buffer-alist-set (&optional no-custom add) | |
6599 | "Set `display-buffer-alist' from Emacs 23 buffer display options. | |
6600 | Optional argument NO-CUSTOM nil means use `customize-set-variable' | |
6601 | to set the value of `display-buffer-alist'. NO-CUSTOM non-nil | |
6602 | means to use `setq' instead. | |
6603 | ||
6604 | Optional argument ADD nil means to replace the actual value of | |
6605 | `display-buffer-alist' with the value calculated here. ADD | |
6606 | non-nil means prepend the value calculated here to the current | |
6607 | value of `display-buffer-alist'." | |
6608 | (unless add | |
6609 | (if no-custom | |
6610 | (setq display-buffer-alist nil) | |
6611 | (customize-set-variable 'display-buffer-alist nil))) | |
6612 | ||
6613 | ;; Disable warnings, there are too many obsolete options here. | |
6614 | (with-no-warnings | |
6615 | ;; `pop-up-windows' | |
6616 | (display-buffer-alist-add | |
6617 | nil | |
6618 | (let ((fun (unless (eq split-window-preferred-function | |
6619 | 'split-window-sensibly) | |
6620 | ;; `split-window-sensibly' has been merged into the | |
6621 | ;; `display-buffer-split-window' code as `nil'. | |
6622 | split-window-preferred-function)) | |
6623 | (min-height | |
6624 | (if (numberp split-height-threshold) | |
6625 | (/ split-height-threshold 2) | |
6626 | ;; Undocumented hack. | |
6627 | 1.0)) | |
6628 | (min-width | |
6629 | (if (numberp split-width-threshold) | |
6630 | (/ split-width-threshold 2) | |
6631 | ;; Undocumented hack. | |
6632 | 1.0))) | |
6633 | (list | |
6634 | 'pop-up-window | |
6635 | (when pop-up-windows ; unset qualifies as t | |
6636 | (list | |
6637 | 'pop-up-window | |
6638 | (cons 'largest fun) | |
6639 | (cons 'lru fun))) | |
6640 | (cons 'pop-up-window-min-height min-height) | |
6641 | (cons 'pop-up-window-min-width min-width))) | |
6642 | no-custom) | |
6643 | ||
6644 | ;; `pop-up-frames' | |
6645 | (display-buffer-alist-add | |
6646 | nil | |
6647 | (list | |
6648 | 'pop-up-frame | |
d1067961 | 6649 | (when pop-up-frames |
9481c002 MR |
6650 | (list 'pop-up-frame pop-up-frames)) |
6651 | (when pop-up-frame-function | |
6652 | (cons 'pop-up-frame-function pop-up-frame-function)) | |
6653 | (when pop-up-frame-alist | |
6654 | (cons 'pop-up-frame-alist pop-up-frame-alist))) | |
6655 | no-custom) | |
6656 | ||
6657 | ;; `special-display-regexps' | |
6658 | (dolist (entry special-display-regexps) | |
6659 | (cond | |
6660 | ((stringp entry) | |
6661 | ;; Plain string. | |
6662 | (display-buffer-alist-add | |
6663 | `((regexp . ,entry)) | |
6664 | (list | |
5386012d MR |
6665 | 'function |
6666 | (list 'function special-display-function | |
9481c002 MR |
6667 | special-display-frame-alist)) |
6668 | no-custom)) | |
6669 | ((consp entry) | |
6670 | (let ((name (car entry)) | |
6671 | (rest (cdr entry))) | |
6672 | (cond | |
6673 | ((functionp (car rest)) | |
6674 | ;; A function. | |
6675 | (display-buffer-alist-add | |
6676 | `((name . ,name)) | |
6677 | (list | |
5386012d | 6678 | 'function |
9481c002 | 6679 | ;; Weary. |
5386012d | 6680 | (list 'function (car rest) (cadr rest))) |
9481c002 MR |
6681 | no-custom)) |
6682 | ((listp rest) | |
6683 | ;; A list of parameters. | |
6684 | (cond | |
6685 | ((assq 'same-window rest) | |
6686 | (display-buffer-alist-add | |
6687 | `((name . ,name)) | |
6688 | (list 'reuse-window | |
6689 | (list 'reuse-window 'same) | |
6690 | (list 'reuse-window-dedicated 'weak)) | |
6691 | no-custom)) | |
6692 | ((assq 'same-frame rest) | |
6693 | (display-buffer-alist-add | |
6694 | `((name . ,name)) (list 'same-frame) no-custom)) | |
6695 | (t | |
6696 | (display-buffer-alist-add | |
6697 | `((name . ,name)) | |
6698 | (list | |
5386012d MR |
6699 | 'function |
6700 | (list 'function special-display-function | |
9481c002 MR |
6701 | special-display-frame-alist)) |
6702 | no-custom))))))))) | |
6703 | ||
6704 | ;; `special-display-buffer-names' | |
6705 | (dolist (entry special-display-buffer-names) | |
6706 | (cond | |
6707 | ((stringp entry) | |
6708 | ;; Plain string. | |
6709 | (display-buffer-alist-add | |
6710 | `((name . ,entry)) | |
6711 | (list | |
5386012d MR |
6712 | 'function |
6713 | (list 'function special-display-function | |
9481c002 MR |
6714 | special-display-frame-alist)) |
6715 | no-custom)) | |
6716 | ((consp entry) | |
6717 | (let ((name (car entry)) | |
6718 | (rest (cdr entry))) | |
6719 | (cond | |
6720 | ((functionp (car rest)) | |
6721 | ;; A function. | |
6722 | (display-buffer-alist-add | |
6723 | `((name . ,name)) | |
6724 | (list | |
5386012d | 6725 | 'function |
9481c002 | 6726 | ;; Weary. |
5386012d | 6727 | (list 'function (car rest) (cadr rest))) |
9481c002 MR |
6728 | no-custom)) |
6729 | ((listp rest) | |
6730 | ;; A list of parameters. | |
6731 | (cond | |
6732 | ((assq 'same-window rest) | |
6733 | (display-buffer-alist-add | |
6734 | `((name . ,name)) | |
6735 | (list 'reuse-window | |
6736 | (list 'reuse-window 'same) | |
6737 | (list 'reuse-window-dedicated 'weak)) | |
6738 | no-custom)) | |
6739 | ((assq 'same-frame rest) | |
6740 | (display-buffer-alist-add | |
6741 | `((name . ,name)) (list 'same-frame) no-custom)) | |
6742 | (t | |
6743 | (display-buffer-alist-add | |
6744 | `((name . ,name)) | |
6745 | (list | |
5386012d MR |
6746 | 'function |
6747 | (list 'function special-display-function | |
9481c002 MR |
6748 | special-display-frame-alist)) |
6749 | no-custom))))))))) | |
6750 | ||
6751 | ;; `same-window-regexps' | |
6752 | (dolist (entry same-window-regexps) | |
6753 | (cond | |
6754 | ((stringp entry) | |
6755 | (display-buffer-alist-add | |
6756 | `((regexp . ,entry)) | |
6757 | (list 'reuse-window (list 'reuse-window 'same)) | |
6758 | no-custom)) | |
6759 | ((consp entry) | |
6760 | (display-buffer-alist-add | |
6761 | `((regexp . ,(car entry))) | |
6762 | (list 'reuse-window (list 'reuse-window 'same)) | |
6763 | no-custom)))) | |
6764 | ||
6765 | ;; `same-window-buffer-names' | |
6766 | (dolist (entry same-window-buffer-names) | |
6767 | (cond | |
6768 | ((stringp entry) | |
6769 | (display-buffer-alist-add | |
6770 | `((name . ,entry)) | |
6771 | (list 'reuse-window (list 'reuse-window 'same)) | |
6772 | no-custom)) | |
6773 | ((consp entry) | |
6774 | (display-buffer-alist-add | |
6775 | `((name . ,(car entry))) | |
6776 | (list 'reuse-window (list 'reuse-window 'same)) | |
6777 | no-custom)))) | |
6778 | ||
6779 | ;; `reuse-window' | |
6780 | (display-buffer-alist-add | |
6781 | nil | |
6782 | (list | |
6783 | 'reuse-window | |
6784 | (list 'reuse-window nil 'same | |
d1067961 | 6785 | (when (or display-buffer-reuse-frames pop-up-frames) |
9481c002 MR |
6786 | ;; "0" (all visible and iconified frames) is hardcoded in |
6787 | ;; Emacs 23. | |
355f2e07 | 6788 | 0)) |
d1067961 | 6789 | (unless (memq even-window-heights '(nil unset)) |
9481c002 MR |
6790 | (cons 'reuse-window-even-sizes t))) |
6791 | no-custom) | |
6792 | ||
6793 | ;; `display-buffer-mark-dedicated' | |
d1067961 | 6794 | (when display-buffer-mark-dedicated |
9481c002 MR |
6795 | (display-buffer-alist-add |
6796 | nil | |
6797 | (list | |
6798 | (cons 'dedicated display-buffer-mark-dedicated)) | |
6799 | no-custom))) | |
6800 | ||
6801 | display-buffer-alist) | |
3c448ab6 MR |
6802 | \f |
6803 | (defun set-window-text-height (window height) | |
9992ea0c | 6804 | "Set the height in lines of the text display area of WINDOW to HEIGHT. |
6198ccd0 MR |
6805 | WINDOW must be a live window. HEIGHT doesn't include the mode |
6806 | line or header line, if any, or any partial-height lines in the | |
6807 | text display area. | |
3c448ab6 MR |
6808 | |
6809 | Note that the current implementation of this function cannot | |
6810 | always set the height exactly, but attempts to be conservative, | |
6811 | by allocating more lines than are actually needed in the case | |
6812 | where some error may be present." | |
5386012d | 6813 | (setq window (window-normalize-live-window window)) |
3c448ab6 MR |
6814 | (let ((delta (- height (window-text-height window)))) |
6815 | (unless (zerop delta) | |
6816 | ;; Setting window-min-height to a value like 1 can lead to very | |
6817 | ;; bizarre displays because it also allows Emacs to make *other* | |
6818 | ;; windows 1-line tall, which means that there's no more space for | |
6819 | ;; the modeline. | |
6820 | (let ((window-min-height (min 2 height))) ; One text line plus a modeline. | |
d615d6d2 | 6821 | (window-resize window delta))))) |
3c448ab6 | 6822 | |
6198ccd0 MR |
6823 | (defun enlarge-window-horizontally (delta) |
6824 | "Make selected window DELTA columns wider. | |
3c448ab6 MR |
6825 | Interactively, if no argument is given, make selected window one |
6826 | column wider." | |
6827 | (interactive "p") | |
6198ccd0 | 6828 | (enlarge-window delta t)) |
3c448ab6 | 6829 | |
6198ccd0 MR |
6830 | (defun shrink-window-horizontally (delta) |
6831 | "Make selected window DELTA columns narrower. | |
3c448ab6 MR |
6832 | Interactively, if no argument is given, make selected window one |
6833 | column narrower." | |
6834 | (interactive "p") | |
6198ccd0 | 6835 | (shrink-window delta t)) |
3c448ab6 MR |
6836 | |
6837 | (defun count-screen-lines (&optional beg end count-final-newline window) | |
6838 | "Return the number of screen lines in the region. | |
6839 | The number of screen lines may be different from the number of actual lines, | |
6840 | due to line breaking, display table, etc. | |
6841 | ||
6842 | Optional arguments BEG and END default to `point-min' and `point-max' | |
6843 | respectively. | |
6844 | ||
6845 | If region ends with a newline, ignore it unless optional third argument | |
6846 | COUNT-FINAL-NEWLINE is non-nil. | |
6847 | ||
6848 | The optional fourth argument WINDOW specifies the window used for obtaining | |
6849 | parameters such as width, horizontal scrolling, and so on. The default is | |
6850 | to use the selected window's parameters. | |
6851 | ||
6852 | Like `vertical-motion', `count-screen-lines' always uses the current buffer, | |
6853 | regardless of which buffer is displayed in WINDOW. This makes possible to use | |
6854 | `count-screen-lines' in any buffer, whether or not it is currently displayed | |
6855 | in some window." | |
6856 | (unless beg | |
6857 | (setq beg (point-min))) | |
6858 | (unless end | |
6859 | (setq end (point-max))) | |
6860 | (if (= beg end) | |
6861 | 0 | |
6862 | (save-excursion | |
6863 | (save-restriction | |
6864 | (widen) | |
6865 | (narrow-to-region (min beg end) | |
6866 | (if (and (not count-final-newline) | |
6867 | (= ?\n (char-before (max beg end)))) | |
6868 | (1- (max beg end)) | |
6869 | (max beg end))) | |
6870 | (goto-char (point-min)) | |
6871 | (1+ (vertical-motion (buffer-size) window)))))) | |
6872 | ||
6198ccd0 MR |
6873 | (defun window-buffer-height (window) |
6874 | "Return the height (in screen lines) of the buffer that WINDOW is displaying." | |
6875 | (with-current-buffer (window-buffer window) | |
6876 | (max 1 | |
6877 | (count-screen-lines (point-min) (point-max) | |
6878 | ;; If buffer ends with a newline, ignore it when | |
6879 | ;; counting height unless point is after it. | |
6880 | (eobp) | |
6881 | window)))) | |
6882 | ||
6883 | ;;; Resizing buffers to fit their contents exactly. | |
6884 | (defun fit-window-to-buffer (&optional window max-height min-height override) | |
3c448ab6 | 6885 | "Adjust height of WINDOW to display its buffer's contents exactly. |
6198ccd0 MR |
6886 | WINDOW can be any live window and defaults to the selected one. |
6887 | ||
6888 | Optional argument MAX-HEIGHT specifies the maximum height of | |
6889 | WINDOW and defaults to the height of WINDOW's frame. Optional | |
6890 | argument MIN-HEIGHT specifies the minimum height of WINDOW and | |
6891 | defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT | |
6892 | are specified in lines and include the mode line and header line, | |
6893 | if any. | |
6894 | ||
6895 | Optional argument OVERRIDE non-nil means override restrictions | |
6896 | imposed by `window-min-height' and `window-min-width' on the size | |
6897 | of WINDOW. | |
6898 | ||
6899 | Return the number of lines by which WINDOW was enlarged or | |
6900 | shrunk. If an error occurs during resizing, return nil but don't | |
6901 | signal an error. | |
6902 | ||
6903 | Note that even if this function makes WINDOW large enough to show | |
6904 | _all_ lines of its buffer you might not see the first lines when | |
6905 | WINDOW was scrolled." | |
f7baca20 MR |
6906 | (interactive) |
6907 | ;; Do all the work in WINDOW and its buffer and restore the selected | |
6908 | ;; window and the current buffer when we're done. | |
5386012d | 6909 | (setq window (window-normalize-live-window window)) |
6198ccd0 | 6910 | ;; Can't resize a full height or fixed-size window. |
9173deec | 6911 | (unless (or (window-size-fixed-p window) |
6198ccd0 MR |
6912 | (window-full-height-p window)) |
6913 | ;; `with-selected-window' should orderly restore the current buffer. | |
6914 | (with-selected-window window | |
6915 | ;; We are in WINDOW's buffer now. | |
6916 | (let* ( ;; Adjust MIN-HEIGHT. | |
6917 | (min-height | |
6918 | (if override | |
6919 | (window-min-size window nil window) | |
6920 | (max (or min-height window-min-height) | |
6921 | window-safe-min-height))) | |
6922 | (max-window-height | |
6923 | (window-total-size (frame-root-window window))) | |
6924 | ;; Adjust MAX-HEIGHT. | |
6925 | (max-height | |
6926 | (if (or override (not max-height)) | |
6927 | max-window-height | |
6928 | (min max-height max-window-height))) | |
6929 | ;; Make `desired-height' the height necessary to show | |
6930 | ;; all of WINDOW's buffer, constrained by MIN-HEIGHT | |
6931 | ;; and MAX-HEIGHT. | |
6932 | (desired-height | |
6933 | (max | |
6934 | (min | |
6935 | (+ (count-screen-lines) | |
6936 | ;; For non-minibuffers count the mode line, if any. | |
6937 | (if (and (not (window-minibuffer-p window)) | |
6938 | mode-line-format) | |
6939 | 1 | |
6940 | 0) | |
6941 | ;; Count the header line, if any. | |
6942 | (if header-line-format 1 0)) | |
6943 | max-height) | |
6944 | min-height)) | |
6945 | (desired-delta | |
6946 | (- desired-height (window-total-size window))) | |
6947 | (delta | |
6948 | (if (> desired-delta 0) | |
6949 | (min desired-delta | |
6950 | (window-max-delta window nil window)) | |
6951 | (max desired-delta | |
6952 | (- (window-min-delta window nil window)))))) | |
6953 | ;; This `condition-case' shouldn't be necessary, but who knows? | |
6954 | (condition-case nil | |
6955 | (if (zerop delta) | |
6956 | ;; Return zero if DELTA became zero in the proces. | |
6957 | 0 | |
6958 | ;; Don't try to redisplay with the cursor at the end on its | |
6959 | ;; own line--that would force a scroll and spoil things. | |
6960 | (when (and (eobp) (bolp) (not (bobp))) | |
6961 | ;; It's silly to put `point' at the end of the previous | |
6962 | ;; line and so maybe force horizontal scrolling. | |
6963 | (set-window-point window (line-beginning-position 0))) | |
d615d6d2 MR |
6964 | ;; Call `window-resize' with OVERRIDE argument equal WINDOW. |
6965 | (window-resize window delta nil window) | |
f7baca20 MR |
6966 | ;; Check if the last line is surely fully visible. If |
6967 | ;; not, enlarge the window. | |
6968 | (let ((end (save-excursion | |
6969 | (goto-char (point-max)) | |
6970 | (when (and (bolp) (not (bobp))) | |
6971 | ;; Don't include final newline. | |
6972 | (backward-char 1)) | |
6973 | (when truncate-lines | |
6974 | ;; If line-wrapping is turned off, test the | |
6975 | ;; beginning of the last line for | |
6976 | ;; visibility instead of the end, as the | |
6977 | ;; end of the line could be invisible by | |
6978 | ;; virtue of extending past the edge of the | |
6979 | ;; window. | |
6980 | (forward-line 0)) | |
6981 | (point)))) | |
6982 | (set-window-vscroll window 0) | |
6198ccd0 MR |
6983 | ;; This loop might in some rare pathological cases raise |
6984 | ;; an error - another reason for the `condition-case'. | |
f7baca20 | 6985 | (while (and (< desired-height max-height) |
6198ccd0 | 6986 | (= desired-height (window-total-size)) |
f7baca20 | 6987 | (not (pos-visible-in-window-p end))) |
d615d6d2 | 6988 | (window-resize window 1 nil window) |
6198ccd0 MR |
6989 | (setq desired-height (1+ desired-height))))) |
6990 | (error (setq delta nil))) | |
6991 | delta)))) | |
3c448ab6 | 6992 | |
39cffb44 MR |
6993 | (defun window-safely-shrinkable-p (&optional window) |
6994 | "Return t if WINDOW can be shrunk without shrinking other windows. | |
6995 | WINDOW defaults to the selected window." | |
6996 | (with-selected-window (or window (selected-window)) | |
6997 | (let ((edges (window-edges))) | |
6998 | ;; The following doesn't satisfy the doc-string's claim when | |
6999 | ;; window and previous-/next-window are not part of the same | |
7000 | ;; combination but still share a common edge. Using | |
7001 | ;; `window-iso-combined-p' instead should handle that. | |
7002 | (or (= (nth 2 edges) (nth 2 (window-edges (previous-window)))) | |
7003 | (= (nth 0 edges) (nth 0 (window-edges (next-window)))))))) | |
0bf4ba9a MR |
7004 | ;; (make-obsolete |
7005 | ;; 'window-safely-shrinkable-p "use `window-iso-combined-p' instead." "24.1") | |
39cffb44 | 7006 | |
3c448ab6 MR |
7007 | (defun shrink-window-if-larger-than-buffer (&optional window) |
7008 | "Shrink height of WINDOW if its buffer doesn't need so many lines. | |
7009 | More precisely, shrink WINDOW vertically to be as small as | |
7010 | possible, while still showing the full contents of its buffer. | |
7011 | WINDOW defaults to the selected window. | |
7012 | ||
6198ccd0 MR |
7013 | Do not shrink WINDOW to less than `window-min-height' lines. Do |
7014 | nothing if the buffer contains more lines than the present window | |
7015 | height, or if some of the window's contents are scrolled out of | |
7016 | view, or if shrinking this window would also shrink another | |
7017 | window, or if the window is the only window of its frame. | |
3c448ab6 MR |
7018 | |
7019 | Return non-nil if the window was shrunk, nil otherwise." | |
7020 | (interactive) | |
5386012d | 7021 | (setq window (window-normalize-live-window window)) |
6198ccd0 MR |
7022 | ;; Make sure that WINDOW is vertically combined and `point-min' is |
7023 | ;; visible (for whatever reason that's needed). The remaining issues | |
7024 | ;; should be taken care of by `fit-window-to-buffer'. | |
7025 | (when (and (window-iso-combined-p window) | |
7026 | (pos-visible-in-window-p (point-min) window)) | |
7027 | (fit-window-to-buffer window (window-total-size window)))) | |
7028 | \f | |
3c448ab6 MR |
7029 | (defun kill-buffer-and-window () |
7030 | "Kill the current buffer and delete the selected window." | |
7031 | (interactive) | |
7032 | (let ((window-to-delete (selected-window)) | |
7033 | (buffer-to-kill (current-buffer)) | |
6198ccd0 | 7034 | (delete-window-hook (lambda () (ignore-errors (delete-window))))) |
3c448ab6 MR |
7035 | (unwind-protect |
7036 | (progn | |
7037 | (add-hook 'kill-buffer-hook delete-window-hook t t) | |
7038 | (if (kill-buffer (current-buffer)) | |
7039 | ;; If `delete-window' failed before, we rerun it to regenerate | |
7040 | ;; the error so it can be seen in the echo area. | |
7041 | (when (eq (selected-window) window-to-delete) | |
7042 | (delete-window)))) | |
7043 | ;; If the buffer is not dead for some reason (probably because | |
7044 | ;; of a `quit' signal), remove the hook again. | |
6198ccd0 MR |
7045 | (ignore-errors |
7046 | (with-current-buffer buffer-to-kill | |
7047 | (remove-hook 'kill-buffer-hook delete-window-hook t)))))) | |
3c448ab6 MR |
7048 | |
7049 | (defun quit-window (&optional kill window) | |
7050 | "Quit WINDOW and bury its buffer. | |
7051 | With a prefix argument, kill the buffer instead. WINDOW defaults | |
7052 | to the selected window. | |
7053 | ||
7054 | If WINDOW is non-nil, dedicated, or a minibuffer window, delete | |
7055 | it and, if it's alone on its frame, its frame too. Otherwise, or | |
7056 | if deleting WINDOW fails in any of the preceding cases, display | |
7057 | another buffer in WINDOW using `switch-to-buffer'. | |
7058 | ||
7059 | Optional argument KILL non-nil means kill WINDOW's buffer. | |
7060 | Otherwise, bury WINDOW's buffer, see `bury-buffer'." | |
7061 | (interactive "P") | |
7062 | (let ((buffer (window-buffer window))) | |
7063 | (if (or window | |
7064 | (window-minibuffer-p window) | |
7065 | (window-dedicated-p window)) | |
7066 | ;; WINDOW is either non-nil, a minibuffer window, or dedicated; | |
7067 | ;; try to delete it. | |
a0c859f0 MR |
7068 | (let* ((window (or window (selected-window))) |
7069 | (frame (window-frame window))) | |
6198ccd0 MR |
7070 | (if (frame-root-window-p window) |
7071 | ;; WINDOW is alone on its frame. | |
7072 | (delete-frame frame) | |
3c448ab6 MR |
7073 | ;; There are other windows on its frame, delete WINDOW. |
7074 | (delete-window window))) | |
7075 | ;; Otherwise, switch to another buffer in the selected window. | |
7076 | (switch-to-buffer nil)) | |
7077 | ||
7078 | ;; Deal with the buffer. | |
7079 | (if kill | |
7080 | (kill-buffer buffer) | |
7081 | (bury-buffer buffer)))) | |
74f806a1 | 7082 | \f |
3c448ab6 MR |
7083 | (defvar recenter-last-op nil |
7084 | "Indicates the last recenter operation performed. | |
0116abbd JL |
7085 | Possible values: `top', `middle', `bottom', integer or float numbers.") |
7086 | ||
7087 | (defcustom recenter-positions '(middle top bottom) | |
7088 | "Cycling order for `recenter-top-bottom'. | |
7089 | A list of elements with possible values `top', `middle', `bottom', | |
7090 | integer or float numbers that define the cycling order for | |
7091 | the command `recenter-top-bottom'. | |
7092 | ||
7093 | Top and bottom destinations are `scroll-margin' lines the from true | |
7094 | window top and bottom. Middle redraws the frame and centers point | |
7095 | vertically within the window. Integer number moves current line to | |
7096 | the specified absolute window-line. Float number between 0.0 and 1.0 | |
7097 | means the percentage of the screen space from the top. The default | |
7098 | cycling order is middle -> top -> bottom." | |
7099 | :type '(repeat (choice | |
7100 | (const :tag "Top" top) | |
7101 | (const :tag "Middle" middle) | |
7102 | (const :tag "Bottom" bottom) | |
7103 | (integer :tag "Line number") | |
7104 | (float :tag "Percentage"))) | |
7105 | :version "23.2" | |
7106 | :group 'windows) | |
3c448ab6 MR |
7107 | |
7108 | (defun recenter-top-bottom (&optional arg) | |
0116abbd JL |
7109 | "Move current buffer line to the specified window line. |
7110 | With no prefix argument, successive calls place point according | |
7111 | to the cycling order defined by `recenter-positions'. | |
3c448ab6 MR |
7112 | |
7113 | A prefix argument is handled like `recenter': | |
7114 | With numeric prefix ARG, move current line to window-line ARG. | |
0116abbd | 7115 | With plain `C-u', move current line to window center." |
3c448ab6 MR |
7116 | (interactive "P") |
7117 | (cond | |
0116abbd | 7118 | (arg (recenter arg)) ; Always respect ARG. |
3c448ab6 | 7119 | (t |
0116abbd JL |
7120 | (setq recenter-last-op |
7121 | (if (eq this-command last-command) | |
7122 | (car (or (cdr (member recenter-last-op recenter-positions)) | |
7123 | recenter-positions)) | |
7124 | (car recenter-positions))) | |
3c448ab6 MR |
7125 | (let ((this-scroll-margin |
7126 | (min (max 0 scroll-margin) | |
7127 | (truncate (/ (window-body-height) 4.0))))) | |
7128 | (cond ((eq recenter-last-op 'middle) | |
0116abbd | 7129 | (recenter)) |
3c448ab6 | 7130 | ((eq recenter-last-op 'top) |
0116abbd JL |
7131 | (recenter this-scroll-margin)) |
7132 | ((eq recenter-last-op 'bottom) | |
7133 | (recenter (- -1 this-scroll-margin))) | |
7134 | ((integerp recenter-last-op) | |
7135 | (recenter recenter-last-op)) | |
7136 | ((floatp recenter-last-op) | |
7137 | (recenter (round (* recenter-last-op (window-height)))))))))) | |
3c448ab6 MR |
7138 | |
7139 | (define-key global-map [?\C-l] 'recenter-top-bottom) | |
216349f8 | 7140 | |
216349f8 SM |
7141 | (defun move-to-window-line-top-bottom (&optional arg) |
7142 | "Position point relative to window. | |
7143 | ||
0f202d5d | 7144 | With a prefix argument ARG, acts like `move-to-window-line'. |
216349f8 SM |
7145 | |
7146 | With no argument, positions point at center of window. | |
0116abbd JL |
7147 | Successive calls position point at positions defined |
7148 | by `recenter-positions'." | |
216349f8 SM |
7149 | (interactive "P") |
7150 | (cond | |
0116abbd | 7151 | (arg (move-to-window-line arg)) ; Always respect ARG. |
216349f8 | 7152 | (t |
0116abbd JL |
7153 | (setq recenter-last-op |
7154 | (if (eq this-command last-command) | |
7155 | (car (or (cdr (member recenter-last-op recenter-positions)) | |
7156 | recenter-positions)) | |
7157 | (car recenter-positions))) | |
216349f8 SM |
7158 | (let ((this-scroll-margin |
7159 | (min (max 0 scroll-margin) | |
7160 | (truncate (/ (window-body-height) 4.0))))) | |
0f202d5d | 7161 | (cond ((eq recenter-last-op 'middle) |
0116abbd | 7162 | (call-interactively 'move-to-window-line)) |
0f202d5d | 7163 | ((eq recenter-last-op 'top) |
0116abbd JL |
7164 | (move-to-window-line this-scroll-margin)) |
7165 | ((eq recenter-last-op 'bottom) | |
7166 | (move-to-window-line (- -1 this-scroll-margin))) | |
7167 | ((integerp recenter-last-op) | |
7168 | (move-to-window-line recenter-last-op)) | |
7169 | ((floatp recenter-last-op) | |
7170 | (move-to-window-line (round (* recenter-last-op (window-height)))))))))) | |
216349f8 SM |
7171 | |
7172 | (define-key global-map [?\M-r] 'move-to-window-line-top-bottom) | |
3c448ab6 | 7173 | \f |
74f806a1 JL |
7174 | ;;; Scrolling commands. |
7175 | ||
7176 | ;;; Scrolling commands which does not signal errors at top/bottom | |
7177 | ;;; of buffer at first key-press (instead moves to top/bottom | |
7178 | ;;; of buffer). | |
7179 | ||
7180 | (defcustom scroll-error-top-bottom nil | |
7181 | "Move point to top/bottom of buffer before signalling a scrolling error. | |
7182 | A value of nil means just signal an error if no more scrolling possible. | |
7183 | A value of t means point moves to the beginning or the end of the buffer | |
7184 | \(depending on scrolling direction) when no more scrolling possible. | |
7185 | When point is already on that position, then signal an error." | |
7186 | :type 'boolean | |
7187 | :group 'scrolling | |
7188 | :version "24.1") | |
7189 | ||
7190 | (defun scroll-up-command (&optional arg) | |
7191 | "Scroll text of selected window upward ARG lines; or near full screen if no ARG. | |
7192 | If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot | |
7193 | scroll window further, move cursor to the bottom line. | |
7194 | When point is already on that position, then signal an error. | |
7195 | A near full screen is `next-screen-context-lines' less than a full screen. | |
7196 | Negative ARG means scroll downward. | |
7197 | If ARG is the atom `-', scroll downward by nearly full screen." | |
7198 | (interactive "^P") | |
7199 | (cond | |
7200 | ((null scroll-error-top-bottom) | |
7201 | (scroll-up arg)) | |
7202 | ((eq arg '-) | |
7203 | (scroll-down-command nil)) | |
7204 | ((< (prefix-numeric-value arg) 0) | |
7205 | (scroll-down-command (- (prefix-numeric-value arg)))) | |
7206 | ((eobp) | |
7207 | (scroll-up arg)) ; signal error | |
7208 | (t | |
7209 | (condition-case nil | |
7210 | (scroll-up arg) | |
7211 | (end-of-buffer | |
7212 | (if arg | |
7213 | ;; When scrolling by ARG lines can't be done, | |
7214 | ;; move by ARG lines instead. | |
7215 | (forward-line arg) | |
7216 | ;; When ARG is nil for full-screen scrolling, | |
7217 | ;; move to the bottom of the buffer. | |
7218 | (goto-char (point-max)))))))) | |
7219 | ||
7220 | (put 'scroll-up-command 'scroll-command t) | |
7221 | ||
7222 | (defun scroll-down-command (&optional arg) | |
7223 | "Scroll text of selected window down ARG lines; or near full screen if no ARG. | |
7224 | If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot | |
7225 | scroll window further, move cursor to the top line. | |
7226 | When point is already on that position, then signal an error. | |
7227 | A near full screen is `next-screen-context-lines' less than a full screen. | |
7228 | Negative ARG means scroll upward. | |
7229 | If ARG is the atom `-', scroll upward by nearly full screen." | |
7230 | (interactive "^P") | |
7231 | (cond | |
7232 | ((null scroll-error-top-bottom) | |
7233 | (scroll-down arg)) | |
7234 | ((eq arg '-) | |
7235 | (scroll-up-command nil)) | |
7236 | ((< (prefix-numeric-value arg) 0) | |
7237 | (scroll-up-command (- (prefix-numeric-value arg)))) | |
7238 | ((bobp) | |
7239 | (scroll-down arg)) ; signal error | |
7240 | (t | |
7241 | (condition-case nil | |
7242 | (scroll-down arg) | |
7243 | (beginning-of-buffer | |
7244 | (if arg | |
7245 | ;; When scrolling by ARG lines can't be done, | |
7246 | ;; move by ARG lines instead. | |
7247 | (forward-line (- arg)) | |
7248 | ;; When ARG is nil for full-screen scrolling, | |
7249 | ;; move to the top of the buffer. | |
7250 | (goto-char (point-min)))))))) | |
7251 | ||
7252 | (put 'scroll-down-command 'scroll-command t) | |
7253 | ||
7254 | ;;; Scrolling commands which scroll a line instead of full screen. | |
7255 | ||
7256 | (defun scroll-up-line (&optional arg) | |
7257 | "Scroll text of selected window upward ARG lines; or one line if no ARG. | |
7258 | If ARG is omitted or nil, scroll upward by one line. | |
7259 | This is different from `scroll-up-command' that scrolls a full screen." | |
7260 | (interactive "p") | |
7261 | (scroll-up (or arg 1))) | |
7262 | ||
7263 | (put 'scroll-up-line 'scroll-command t) | |
7264 | ||
7265 | (defun scroll-down-line (&optional arg) | |
7266 | "Scroll text of selected window down ARG lines; or one line if no ARG. | |
7267 | If ARG is omitted or nil, scroll down by one line. | |
7268 | This is different from `scroll-down-command' that scrolls a full screen." | |
7269 | (interactive "p") | |
7270 | (scroll-down (or arg 1))) | |
7271 | ||
7272 | (put 'scroll-down-line 'scroll-command t) | |
7273 | ||
7274 | \f | |
7275 | (defun scroll-other-window-down (lines) | |
7276 | "Scroll the \"other window\" down. | |
7277 | For more details, see the documentation for `scroll-other-window'." | |
7278 | (interactive "P") | |
7279 | (scroll-other-window | |
7280 | ;; Just invert the argument's meaning. | |
7281 | ;; We can do that without knowing which window it will be. | |
7282 | (if (eq lines '-) nil | |
7283 | (if (null lines) '- | |
7284 | (- (prefix-numeric-value lines)))))) | |
7285 | ||
7286 | (defun beginning-of-buffer-other-window (arg) | |
7287 | "Move point to the beginning of the buffer in the other window. | |
7288 | Leave mark at previous position. | |
7289 | With arg N, put point N/10 of the way from the true beginning." | |
7290 | (interactive "P") | |
7291 | (let ((orig-window (selected-window)) | |
7292 | (window (other-window-for-scrolling))) | |
7293 | ;; We use unwind-protect rather than save-window-excursion | |
7294 | ;; because the latter would preserve the things we want to change. | |
7295 | (unwind-protect | |
7296 | (progn | |
7297 | (select-window window) | |
7298 | ;; Set point and mark in that window's buffer. | |
7299 | (with-no-warnings | |
7300 | (beginning-of-buffer arg)) | |
7301 | ;; Set point accordingly. | |
7302 | (recenter '(t))) | |
7303 | (select-window orig-window)))) | |
7304 | ||
7305 | (defun end-of-buffer-other-window (arg) | |
7306 | "Move point to the end of the buffer in the other window. | |
7307 | Leave mark at previous position. | |
7308 | With arg N, put point N/10 of the way from the true end." | |
7309 | (interactive "P") | |
7310 | ;; See beginning-of-buffer-other-window for comments. | |
7311 | (let ((orig-window (selected-window)) | |
7312 | (window (other-window-for-scrolling))) | |
7313 | (unwind-protect | |
7314 | (progn | |
7315 | (select-window window) | |
7316 | (with-no-warnings | |
7317 | (end-of-buffer arg)) | |
7318 | (recenter '(t))) | |
7319 | (select-window orig-window)))) | |
74f806a1 | 7320 | \f |
3c448ab6 MR |
7321 | (defvar mouse-autoselect-window-timer nil |
7322 | "Timer used by delayed window autoselection.") | |
7323 | ||
7324 | (defvar mouse-autoselect-window-position nil | |
7325 | "Last mouse position recorded by delayed window autoselection.") | |
7326 | ||
7327 | (defvar mouse-autoselect-window-window nil | |
7328 | "Last window recorded by delayed window autoselection.") | |
7329 | ||
7330 | (defvar mouse-autoselect-window-state nil | |
7331 | "When non-nil, special state of delayed window autoselection. | |
7332 | Possible values are `suspend' \(suspend autoselection after a menu or | |
7333 | scrollbar interaction\) and `select' \(the next invocation of | |
7334 | 'handle-select-window' shall select the window immediately\).") | |
7335 | ||
7336 | (defun mouse-autoselect-window-cancel (&optional force) | |
7337 | "Cancel delayed window autoselection. | |
7338 | Optional argument FORCE means cancel unconditionally." | |
7339 | (unless (and (not force) | |
7340 | ;; Don't cancel for select-window or select-frame events | |
7341 | ;; or when the user drags a scroll bar. | |
7342 | (or (memq this-command | |
7343 | '(handle-select-window handle-switch-frame)) | |
7344 | (and (eq this-command 'scroll-bar-toolkit-scroll) | |
7345 | (memq (nth 4 (event-end last-input-event)) | |
7346 | '(handle end-scroll))))) | |
7347 | (setq mouse-autoselect-window-state nil) | |
7348 | (when (timerp mouse-autoselect-window-timer) | |
7349 | (cancel-timer mouse-autoselect-window-timer)) | |
7350 | (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel))) | |
7351 | ||
7352 | (defun mouse-autoselect-window-start (mouse-position &optional window suspend) | |
7353 | "Start delayed window autoselection. | |
7354 | MOUSE-POSITION is the last position where the mouse was seen as returned | |
7355 | by `mouse-position'. Optional argument WINDOW non-nil denotes the | |
7356 | window where the mouse was seen. Optional argument SUSPEND non-nil | |
7357 | means suspend autoselection." | |
7358 | ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND. | |
7359 | (setq mouse-autoselect-window-position mouse-position) | |
7360 | (when window (setq mouse-autoselect-window-window window)) | |
7361 | (setq mouse-autoselect-window-state (when suspend 'suspend)) | |
7362 | ;; Install timer which runs `mouse-autoselect-window-select' after | |
7363 | ;; `mouse-autoselect-window' seconds. | |
7364 | (setq mouse-autoselect-window-timer | |
7365 | (run-at-time | |
7366 | (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select))) | |
7367 | ||
7368 | (defun mouse-autoselect-window-select () | |
7369 | "Select window with delayed window autoselection. | |
7370 | If the mouse position has stabilized in a non-selected window, select | |
7371 | that window. The minibuffer window is selected only if the minibuffer is | |
7372 | active. This function is run by `mouse-autoselect-window-timer'." | |
6198ccd0 MR |
7373 | (ignore-errors |
7374 | (let* ((mouse-position (mouse-position)) | |
7375 | (window | |
7376 | (ignore-errors | |
7377 | (window-at (cadr mouse-position) (cddr mouse-position) | |
7378 | (car mouse-position))))) | |
7379 | (cond | |
7380 | ((or (menu-or-popup-active-p) | |
7381 | (and window | |
7382 | (not (coordinates-in-window-p (cdr mouse-position) window)))) | |
7383 | ;; A menu / popup dialog is active or the mouse is on the scroll-bar | |
7384 | ;; of WINDOW, temporarily suspend delayed autoselection. | |
7385 | (mouse-autoselect-window-start mouse-position nil t)) | |
7386 | ((eq mouse-autoselect-window-state 'suspend) | |
7387 | ;; Delayed autoselection was temporarily suspended, reenable it. | |
7388 | (mouse-autoselect-window-start mouse-position)) | |
7389 | ((and window (not (eq window (selected-window))) | |
7390 | (or (not (numberp mouse-autoselect-window)) | |
7391 | (and (> mouse-autoselect-window 0) | |
7392 | ;; If `mouse-autoselect-window' is positive, select | |
7393 | ;; window if the window is the same as before. | |
7394 | (eq window mouse-autoselect-window-window)) | |
7395 | ;; Otherwise select window if the mouse is at the same | |
7396 | ;; position as before. Observe that the first test after | |
7397 | ;; starting autoselection usually fails since the value of | |
7398 | ;; `mouse-autoselect-window-position' recorded there is the | |
7399 | ;; position where the mouse has entered the new window and | |
7400 | ;; not necessarily where the mouse has stopped moving. | |
7401 | (equal mouse-position mouse-autoselect-window-position)) | |
7402 | ;; The minibuffer is a candidate window if it's active. | |
7403 | (or (not (window-minibuffer-p window)) | |
7404 | (eq window (active-minibuffer-window)))) | |
7405 | ;; Mouse position has stabilized in non-selected window: Cancel | |
7406 | ;; delayed autoselection and try to select that window. | |
7407 | (mouse-autoselect-window-cancel t) | |
7408 | ;; Select window where mouse appears unless the selected window is the | |
7409 | ;; minibuffer. Use `unread-command-events' in order to execute pre- | |
7410 | ;; and post-command hooks and trigger idle timers. To avoid delaying | |
7411 | ;; autoselection again, set `mouse-autoselect-window-state'." | |
7412 | (unless (window-minibuffer-p (selected-window)) | |
7413 | (setq mouse-autoselect-window-state 'select) | |
7414 | (setq unread-command-events | |
7415 | (cons (list 'select-window (list window)) | |
7416 | unread-command-events)))) | |
7417 | ((or (and window (eq window (selected-window))) | |
7418 | (not (numberp mouse-autoselect-window)) | |
7419 | (equal mouse-position mouse-autoselect-window-position)) | |
7420 | ;; Mouse position has either stabilized in the selected window or at | |
7421 | ;; `mouse-autoselect-window-position': Cancel delayed autoselection. | |
7422 | (mouse-autoselect-window-cancel t)) | |
7423 | (t | |
7424 | ;; Mouse position has not stabilized yet, resume delayed | |
7425 | ;; autoselection. | |
7426 | (mouse-autoselect-window-start mouse-position window)))))) | |
3c448ab6 MR |
7427 | |
7428 | (defun handle-select-window (event) | |
7429 | "Handle select-window events." | |
7430 | (interactive "e") | |
7431 | (let ((window (posn-window (event-start event)))) | |
7432 | (unless (or (not (window-live-p window)) | |
7433 | ;; Don't switch if we're currently in the minibuffer. | |
7434 | ;; This tries to work around problems where the | |
7435 | ;; minibuffer gets unselected unexpectedly, and where | |
7436 | ;; you then have to move your mouse all the way down to | |
7437 | ;; the minibuffer to select it. | |
7438 | (window-minibuffer-p (selected-window)) | |
7439 | ;; Don't switch to minibuffer window unless it's active. | |
7440 | (and (window-minibuffer-p window) | |
7441 | (not (minibuffer-window-active-p window))) | |
7442 | ;; Don't switch when autoselection shall be delayed. | |
7443 | (and (numberp mouse-autoselect-window) | |
7444 | (not (zerop mouse-autoselect-window)) | |
7445 | (not (eq mouse-autoselect-window-state 'select)) | |
7446 | (progn | |
7447 | ;; Cancel any delayed autoselection. | |
7448 | (mouse-autoselect-window-cancel t) | |
7449 | ;; Start delayed autoselection from current mouse | |
7450 | ;; position and window. | |
7451 | (mouse-autoselect-window-start (mouse-position) window) | |
7452 | ;; Executing a command cancels delayed autoselection. | |
7453 | (add-hook | |
7454 | 'pre-command-hook 'mouse-autoselect-window-cancel)))) | |
7455 | (when mouse-autoselect-window | |
7456 | ;; Reset state of delayed autoselection. | |
7457 | (setq mouse-autoselect-window-state nil) | |
7458 | ;; Run `mouse-leave-buffer-hook' when autoselecting window. | |
7459 | (run-hooks 'mouse-leave-buffer-hook)) | |
7460 | (select-window window)))) | |
7461 | ||
3c448ab6 MR |
7462 | (defun truncated-partial-width-window-p (&optional window) |
7463 | "Return non-nil if lines in WINDOW are specifically truncated due to its width. | |
7464 | WINDOW defaults to the selected window. | |
7465 | Return nil if WINDOW is not a partial-width window | |
7466 | (regardless of the value of `truncate-lines'). | |
7467 | Otherwise, consult the value of `truncate-partial-width-windows' | |
7468 | for the buffer shown in WINDOW." | |
7469 | (unless window | |
7470 | (setq window (selected-window))) | |
7471 | (unless (window-full-width-p window) | |
7472 | (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows | |
7473 | (window-buffer window)))) | |
7474 | (if (integerp t-p-w-w) | |
7475 | (< (window-width window) t-p-w-w) | |
7476 | t-p-w-w)))) | |
562dd5e9 MR |
7477 | \f |
7478 | (define-key ctl-x-map "0" 'delete-window) | |
7479 | (define-key ctl-x-map "1" 'delete-other-windows) | |
7480 | (define-key ctl-x-map "2" 'split-window-above-each-other) | |
7481 | (define-key ctl-x-map "3" 'split-window-side-by-side) | |
9397e56f | 7482 | (define-key ctl-x-map "o" 'other-window) |
562dd5e9 | 7483 | (define-key ctl-x-map "^" 'enlarge-window) |
3c448ab6 MR |
7484 | (define-key ctl-x-map "}" 'enlarge-window-horizontally) |
7485 | (define-key ctl-x-map "{" 'shrink-window-horizontally) | |
7486 | (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer) | |
7487 | (define-key ctl-x-map "+" 'balance-windows) | |
7488 | (define-key ctl-x-4-map "0" 'kill-buffer-and-window) | |
7489 | ||
3c448ab6 | 7490 | ;;; window.el ends here |