Commit | Line | Data |
---|---|---|
3c448ab6 MR |
1 | ;;; window.el --- GNU Emacs window commands aside from those written in C |
2 | ||
ba318903 PE |
3 | ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2014 Free Software |
4 | ;; Foundation, Inc. | |
3c448ab6 MR |
5 | |
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: internal | |
bd78fa1d | 8 | ;; Package: emacs |
3c448ab6 MR |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Window tree functions. | |
28 | ||
29 | ;;; Code: | |
30 | ||
662a9d0e SM |
31 | (defun internal--before-save-selected-window () |
32 | (cons (selected-window) | |
33 | ;; We save and restore all frames' selected windows, because | |
34 | ;; `select-window' can change the frame-selected-window of | |
35 | ;; whatever frame that window is in. Each text terminal's | |
36 | ;; top-frame is preserved by putting it last in the list. | |
37 | (apply #'append | |
38 | (mapcar (lambda (terminal) | |
39 | (let ((frames (frames-on-display-list terminal)) | |
40 | (top-frame (tty-top-frame terminal)) | |
41 | alist) | |
42 | (if top-frame | |
43 | (setq frames | |
44 | (cons top-frame | |
45 | (delq top-frame frames)))) | |
46 | (dolist (f frames) | |
47 | (push (cons f (frame-selected-window f)) | |
48 | alist)) | |
49 | alist)) | |
50 | (terminal-list))))) | |
51 | ||
52 | (defun internal--after-save-selected-window (state) | |
53 | (dolist (elt (cdr state)) | |
54 | (and (frame-live-p (car elt)) | |
55 | (window-live-p (cdr elt)) | |
56 | (set-frame-selected-window (car elt) (cdr elt) 'norecord))) | |
57 | (when (window-live-p (car state)) | |
58 | (select-window (car state) 'norecord))) | |
59 | ||
3c448ab6 MR |
60 | (defmacro save-selected-window (&rest body) |
61 | "Execute BODY, then select the previously selected window. | |
62 | The value returned is the value of the last form in BODY. | |
63 | ||
64 | This macro saves and restores the selected window, as well as the | |
65 | selected window in each frame. If the previously selected window | |
66 | is no longer live, then whatever window is selected at the end of | |
67 | BODY remains selected. If the previously selected window of some | |
68 | frame is no longer live at the end of BODY, that frame's selected | |
69 | window is left alone. | |
70 | ||
71 | This macro saves and restores the current buffer, since otherwise | |
72 | its normal operation could make a different buffer current. The | |
73 | order of recently selected windows and the buffer list ordering | |
74 | are not altered by this macro (unless they are altered in BODY)." | |
f291fe60 | 75 | (declare (indent 0) (debug t)) |
662a9d0e | 76 | `(let ((save-selected-window--state (internal--before-save-selected-window))) |
3c448ab6 MR |
77 | (save-current-buffer |
78 | (unwind-protect | |
79 | (progn ,@body) | |
662a9d0e | 80 | (internal--after-save-selected-window save-selected-window--state))))) |
3c448ab6 | 81 | |
c5e28e39 MR |
82 | (defvar temp-buffer-window-setup-hook nil |
83 | "Normal hook run by `with-temp-buffer-window' before buffer display. | |
84 | This hook is run by `with-temp-buffer-window' with the buffer to be | |
85 | displayed current.") | |
86 | ||
87 | (defvar temp-buffer-window-show-hook nil | |
88 | "Normal hook run by `with-temp-buffer-window' after buffer display. | |
89 | This hook is run by `with-temp-buffer-window' with the buffer | |
90 | displayed and current and its window selected.") | |
91 | ||
92 | (defun temp-buffer-window-setup (buffer-or-name) | |
42019c2e | 93 | "Set up temporary buffer specified by BUFFER-OR-NAME. |
c5e28e39 MR |
94 | Return the buffer." |
95 | (let ((old-dir default-directory) | |
96 | (buffer (get-buffer-create buffer-or-name))) | |
97 | (with-current-buffer buffer | |
98 | (kill-all-local-variables) | |
99 | (setq default-directory old-dir) | |
100 | (delete-all-overlays) | |
101 | (setq buffer-read-only nil) | |
102 | (setq buffer-file-name nil) | |
103 | (setq buffer-undo-list t) | |
104 | (let ((inhibit-read-only t) | |
105 | (inhibit-modification-hooks t)) | |
106 | (erase-buffer) | |
107 | (run-hooks 'temp-buffer-window-setup-hook)) | |
108 | ;; Return the buffer. | |
109 | buffer))) | |
110 | ||
111 | (defun temp-buffer-window-show (&optional buffer action) | |
112 | "Show temporary buffer BUFFER in a window. | |
113 | Return the window showing BUFFER. Pass ACTION as action argument | |
114 | to `display-buffer'." | |
115 | (let (window frame) | |
116 | (with-current-buffer buffer | |
117 | (set-buffer-modified-p nil) | |
118 | (setq buffer-read-only t) | |
119 | (goto-char (point-min)) | |
8e17c9ba MR |
120 | (when (let ((window-combination-limit |
121 | ;; When `window-combination-limit' equals | |
122 | ;; `temp-buffer' or `temp-buffer-resize' and | |
123 | ;; `temp-buffer-resize-mode' is enabled in this | |
124 | ;; buffer bind it to t so resizing steals space | |
125 | ;; preferably from the window that was split. | |
126 | (if (or (eq window-combination-limit 'temp-buffer) | |
127 | (and (eq window-combination-limit | |
128 | 'temp-buffer-resize) | |
129 | temp-buffer-resize-mode)) | |
130 | t | |
131 | window-combination-limit))) | |
132 | (setq window (display-buffer buffer action))) | |
c5e28e39 MR |
133 | (setq frame (window-frame window)) |
134 | (unless (eq frame (selected-frame)) | |
135 | (raise-frame frame)) | |
136 | (setq minibuffer-scroll-window window) | |
137 | (set-window-hscroll window 0) | |
138 | (with-selected-window window | |
139 | (run-hooks 'temp-buffer-window-show-hook) | |
140 | (when temp-buffer-resize-mode | |
141 | (resize-temp-buffer-window window))) | |
142 | ;; Return the window. | |
143 | window)))) | |
144 | ||
95f0501e | 145 | ;; Doc is very similar to with-output-to-temp-buffer. |
c5e28e39 | 146 | (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body) |
95f0501e | 147 | "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer. |
38785e75 GM |
148 | BUFFER-OR-NAME must specify either a live buffer, or the name of a |
149 | buffer (if it does not exist, this macro creates it). | |
c5e28e39 | 150 | |
95f0501e GM |
151 | This construct makes buffer BUFFER-OR-NAME empty before running BODY. |
152 | It does not make the buffer current for BODY. | |
153 | Instead it binds `standard-output' to that buffer, so that output | |
154 | generated with `prin1' and similar functions in BODY goes into | |
155 | the buffer. | |
c5e28e39 | 156 | |
95f0501e GM |
157 | At the end of BODY, this marks the specified buffer unmodified and |
158 | read-only, and displays it in a window (but does not select it, or make | |
159 | the buffer current). The display happens by calling `display-buffer' | |
160 | with the ACTION argument. If `temp-buffer-resize-mode' is enabled, | |
161 | the relevant window shrinks automatically. | |
c5e28e39 | 162 | |
95f0501e GM |
163 | This returns the value returned by BODY, unless QUIT-FUNCTION specifies |
164 | a function. In that case, it runs the function with two arguments - | |
c5e28e39 | 165 | the window showing the specified buffer and the value returned by |
38785e75 | 166 | BODY - and returns the value returned by that function. |
c5e28e39 MR |
167 | |
168 | If the buffer is displayed on a new frame, the window manager may | |
169 | decide to select that frame. In that case, it's usually a good | |
65463c40 GM |
170 | strategy if QUIT-FUNCTION selects the window showing the buffer |
171 | before reading any value from the minibuffer; for example, when | |
172 | asking a `yes-or-no-p' question. | |
38785e75 | 173 | |
95f0501e GM |
174 | This runs the hook `temp-buffer-window-setup-hook' before BODY, |
175 | with the specified buffer temporarily current. It runs the | |
176 | hook `temp-buffer-window-show-hook' after displaying the buffer, | |
177 | with that buffer temporarily current, and the window that was used to | |
178 | display it temporarily selected. | |
179 | ||
180 | This construct is similar to `with-output-to-temp-buffer', but | |
181 | runs different hooks. In particular, it does not run | |
182 | `temp-buffer-setup-hook', which usually puts the buffer in Help mode. | |
183 | Also, it does not call `temp-buffer-show-function' (the ACTION | |
184 | argument replaces this)." | |
c5e28e39 MR |
185 | (declare (debug t)) |
186 | (let ((buffer (make-symbol "buffer")) | |
187 | (window (make-symbol "window")) | |
188 | (value (make-symbol "value"))) | |
189 | `(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name)) | |
190 | (standard-output ,buffer) | |
191 | ,window ,value) | |
192 | (with-current-buffer ,buffer | |
193 | (setq ,value (progn ,@body)) | |
194 | (setq ,window (temp-buffer-window-show ,buffer ,action))) | |
195 | ||
196 | (if (functionp ,quit-function) | |
197 | (funcall ,quit-function ,window ,value) | |
198 | ,value)))) | |
199 | ||
d68443dc MR |
200 | ;; The following two functions are like `window-next-sibling' and |
201 | ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so | |
202 | ;; they don't substitute the selected window for nil), and they return | |
203 | ;; nil when WINDOW doesn't have a parent (like a frame's root window or | |
204 | ;; a minibuffer window). | |
4b0d61e3 | 205 | (defun window-right (window) |
85cc1f11 MR |
206 | "Return WINDOW's right sibling. |
207 | Return nil if WINDOW is the root window of its frame. WINDOW can | |
208 | be any window." | |
d68443dc | 209 | (and window (window-parent window) (window-next-sibling window))) |
85cc1f11 | 210 | |
4b0d61e3 | 211 | (defun window-left (window) |
85cc1f11 MR |
212 | "Return WINDOW's left sibling. |
213 | Return nil if WINDOW is the root window of its frame. WINDOW can | |
214 | be any window." | |
d68443dc | 215 | (and window (window-parent window) (window-prev-sibling window))) |
85cc1f11 | 216 | |
4b0d61e3 | 217 | (defun window-child (window) |
85c2386b MR |
218 | "Return WINDOW's first child window. |
219 | WINDOW can be any window." | |
d68443dc | 220 | (or (window-top-child window) (window-left-child window))) |
85cc1f11 MR |
221 | |
222 | (defun window-child-count (window) | |
85c2386b MR |
223 | "Return number of WINDOW's child windows. |
224 | WINDOW can be any window." | |
85cc1f11 MR |
225 | (let ((count 0)) |
226 | (when (and (windowp window) (setq window (window-child window))) | |
227 | (while window | |
228 | (setq count (1+ count)) | |
d68443dc | 229 | (setq window (window-next-sibling window)))) |
85cc1f11 MR |
230 | count)) |
231 | ||
232 | (defun window-last-child (window) | |
85c2386b MR |
233 | "Return last child window of WINDOW. |
234 | WINDOW can be any window." | |
85cc1f11 | 235 | (when (and (windowp window) (setq window (window-child window))) |
d68443dc MR |
236 | (while (window-next-sibling window) |
237 | (setq window (window-next-sibling window)))) | |
85cc1f11 MR |
238 | window) |
239 | ||
4b0d61e3 | 240 | (defun window-normalize-buffer (buffer-or-name) |
85cc1f11 MR |
241 | "Return buffer specified by BUFFER-OR-NAME. |
242 | BUFFER-OR-NAME must be either a buffer or a string naming a live | |
243 | buffer and defaults to the current buffer." | |
244 | (cond | |
245 | ((not buffer-or-name) | |
246 | (current-buffer)) | |
247 | ((bufferp buffer-or-name) | |
248 | (if (buffer-live-p buffer-or-name) | |
249 | buffer-or-name | |
250 | (error "Buffer %s is not a live buffer" buffer-or-name))) | |
251 | ((get-buffer buffer-or-name)) | |
252 | (t | |
253 | (error "No such buffer %s" buffer-or-name)))) | |
254 | ||
4b0d61e3 | 255 | (defun window-normalize-frame (frame) |
85cc1f11 MR |
256 | "Return frame specified by FRAME. |
257 | FRAME must be a live frame and defaults to the selected frame." | |
258 | (if frame | |
259 | (if (frame-live-p frame) | |
260 | frame | |
261 | (error "%s is not a live frame" frame)) | |
262 | (selected-frame))) | |
263 | ||
4b0d61e3 | 264 | (defun window-normalize-window (window &optional live-only) |
85c2386b MR |
265 | "Return the window specified by WINDOW. |
266 | If WINDOW is nil, return the selected window. Otherwise, if | |
267 | WINDOW is a live or an internal window, return WINDOW; if | |
268 | LIVE-ONLY is non-nil, return WINDOW for a live window only. | |
447f16b8 | 269 | Otherwise, signal an error." |
85c2386b MR |
270 | (cond |
271 | ((null window) | |
272 | (selected-window)) | |
273 | (live-only | |
274 | (if (window-live-p window) | |
275 | window | |
276 | (error "%s is not a live window" window))) | |
277 | ((window-valid-p window) | |
278 | window) | |
279 | (t | |
280 | (error "%s is not a valid window" window)))) | |
85cc1f11 | 281 | |
880e6158 MR |
282 | ;; Maybe this should go to frame.el. |
283 | (defun frame-char-size (&optional window-or-frame horizontal) | |
284 | "Return the value of `frame-char-height' for WINDOW-OR-FRAME. | |
285 | If WINDOW-OR-FRAME is a live frame, return the value of | |
286 | `frame-char-height' for that frame. If WINDOW-OR-FRAME is a | |
287 | valid window, return the value of `frame-char-height' for that | |
288 | window's frame. In any other case, return the value of | |
289 | `frame-char-height' for the selected frame. | |
290 | ||
291 | Optional argument HORIZONTAL non-nil means to return the value of | |
292 | `frame-char-width' for WINDOW-OR-FRAME." | |
293 | (let ((frame | |
294 | (cond | |
295 | ((window-valid-p window-or-frame) | |
296 | (window-frame window-or-frame)) | |
297 | ((frame-live-p window-or-frame) | |
298 | window-or-frame) | |
299 | (t (selected-frame))))) | |
300 | (if horizontal | |
301 | (frame-char-width frame) | |
302 | (frame-char-height frame)))) | |
303 | ||
85cc1f11 MR |
304 | (defvar ignore-window-parameters nil |
305 | "If non-nil, standard functions ignore window parameters. | |
306 | The functions currently affected by this are `split-window', | |
307 | `delete-window', `delete-other-windows' and `other-window'. | |
308 | ||
309 | An application may bind this to a non-nil value around calls to | |
310 | these functions to inhibit processing of window parameters.") | |
311 | ||
880e6158 | 312 | ;; This must go to C, finally (or get removed). |
a1511caf | 313 | (defconst window-safe-min-height 1 |
880e6158 | 314 | "The absolute minimum number of lines of any window. |
a1511caf MR |
315 | Anything less might crash Emacs.") |
316 | ||
880e6158 MR |
317 | (defun window-safe-min-pixel-height (&optional window) |
318 | "Return the absolute minimum pixel height of WINDOW." | |
319 | (* window-safe-min-height | |
320 | (frame-char-size (window-normalize-window window)))) | |
321 | ||
562dd5e9 MR |
322 | (defcustom window-min-height 4 |
323 | "The minimum number of lines of any window. | |
9173deec JB |
324 | The value has to accommodate a mode- or header-line if present. |
325 | A value less than `window-safe-min-height' is ignored. The value | |
562dd5e9 MR |
326 | of this variable is honored when windows are resized or split. |
327 | ||
328 | Applications should never rebind this variable. To resize a | |
329 | window to a height less than the one specified here, an | |
d615d6d2 | 330 | application should instead call `window-resize' with a non-nil |
562dd5e9 | 331 | IGNORE argument. In order to have `split-window' make a window |
e4769531 | 332 | shorter, explicitly specify the SIZE argument of that function." |
562dd5e9 MR |
333 | :type 'integer |
334 | :version "24.1" | |
335 | :group 'windows) | |
336 | ||
880e6158 MR |
337 | (defun window-min-pixel-height (&optional window) |
338 | "Return the minimum pixel height of window WINDOW." | |
339 | (* (max window-min-height window-safe-min-height) | |
340 | (frame-char-size window))) | |
341 | ||
342 | ;; This must go to C, finally (or get removed). | |
a1511caf | 343 | (defconst window-safe-min-width 2 |
8da11505 | 344 | "The absolute minimum number of columns of a window. |
a1511caf MR |
345 | Anything less might crash Emacs.") |
346 | ||
880e6158 MR |
347 | (defun window-safe-min-pixel-width (&optional window) |
348 | "Return the absolute minimum pixel width of WINDOW." | |
349 | (* window-safe-min-width | |
350 | (frame-char-size (window-normalize-window window) t))) | |
351 | ||
562dd5e9 MR |
352 | (defcustom window-min-width 10 |
353 | "The minimum number of columns of any window. | |
a91adc7e | 354 | The value has to accommodate margins, fringes, or scrollbars if |
562dd5e9 MR |
355 | present. A value less than `window-safe-min-width' is ignored. |
356 | The value of this variable is honored when windows are resized or | |
357 | split. | |
358 | ||
359 | Applications should never rebind this variable. To resize a | |
360 | window to a width less than the one specified here, an | |
d615d6d2 | 361 | application should instead call `window-resize' with a non-nil |
562dd5e9 | 362 | IGNORE argument. In order to have `split-window' make a window |
e4769531 | 363 | narrower, explicitly specify the SIZE argument of that function." |
562dd5e9 MR |
364 | :type 'integer |
365 | :version "24.1" | |
366 | :group 'windows) | |
367 | ||
880e6158 MR |
368 | (defun window-min-pixel-width (&optional window) |
369 | "Return the minimum pixel width of window WINDOW." | |
370 | (* (max window-min-width window-safe-min-width) | |
371 | (frame-char-size window t))) | |
372 | ||
373 | (defun window-safe-min-pixel-size (&optional window horizontal) | |
374 | "Return the absolute minimum pixel height of WINDOW. | |
375 | Optional argument HORIZONTAL non-nil means return the absolute | |
376 | minimum pixel width of WINDOW." | |
377 | (if horizontal | |
378 | (window-safe-min-pixel-width window) | |
379 | (window-safe-min-pixel-height window))) | |
380 | ||
4b0d61e3 | 381 | (defun window-combined-p (&optional window horizontal) |
49745b39 | 382 | "Return non-nil if WINDOW has siblings in a given direction. |
85c2386b | 383 | WINDOW must be a valid window and defaults to the selected one. |
49745b39 | 384 | |
880e6158 MR |
385 | HORIZONTAL determines a direction for the window combination. If |
386 | HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of | |
387 | a vertical window combination. If HORIZONTAL is non-nil, return | |
388 | non-nil if WINDOW is part of a horizontal window combination." | |
447f16b8 | 389 | (setq window (window-normalize-window window)) |
85cc1f11 | 390 | (let ((parent (window-parent window))) |
49745b39 CY |
391 | (and parent |
392 | (if horizontal | |
393 | (window-left-child parent) | |
394 | (window-top-child parent))))) | |
85cc1f11 | 395 | |
880e6158 MR |
396 | (defun window-combination-p (&optional window horizontal) |
397 | "Return WINDOW's first child if WINDOW is a vertical combination. | |
398 | WINDOW can be any window and defaults to the selected one. | |
399 | Optional argument HORIZONTAL non-nil means return WINDOW's first | |
400 | child if WINDOW is a horizontal combination." | |
401 | (setq window (window-normalize-window window)) | |
402 | (if horizontal | |
403 | (window-left-child window) | |
404 | (window-top-child window))) | |
405 | ||
be7f5545 MR |
406 | (defun window-combinations (window &optional horizontal) |
407 | "Return largest number of windows vertically arranged within WINDOW. | |
85c2386b | 408 | WINDOW must be a valid window and defaults to the selected one. |
49745b39 | 409 | If HORIZONTAL is non-nil, return the largest number of |
be7f5545 | 410 | windows horizontally arranged within WINDOW." |
447f16b8 | 411 | (setq window (window-normalize-window window)) |
85cc1f11 MR |
412 | (cond |
413 | ((window-live-p window) | |
414 | ;; If WINDOW is live, return 1. | |
415 | 1) | |
49745b39 CY |
416 | ((if horizontal |
417 | (window-left-child window) | |
418 | (window-top-child window)) | |
85cc1f11 | 419 | ;; If WINDOW is iso-combined, return the sum of the values for all |
be7f5545 | 420 | ;; child windows of WINDOW. |
85cc1f11 MR |
421 | (let ((child (window-child window)) |
422 | (count 0)) | |
423 | (while child | |
424 | (setq count | |
3d8daefe | 425 | (+ (window-combinations child horizontal) |
85cc1f11 MR |
426 | count)) |
427 | (setq child (window-right child))) | |
428 | count)) | |
429 | (t | |
430 | ;; If WINDOW is not iso-combined, return the maximum value of any | |
be7f5545 | 431 | ;; child window of WINDOW. |
85cc1f11 MR |
432 | (let ((child (window-child window)) |
433 | (count 1)) | |
434 | (while child | |
435 | (setq count | |
3d8daefe | 436 | (max (window-combinations child horizontal) |
85cc1f11 MR |
437 | count)) |
438 | (setq child (window-right child))) | |
439 | count)))) | |
440 | ||
c7635a97 | 441 | (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only) |
85cc1f11 MR |
442 | "Helper function for `walk-window-tree' and `walk-window-subtree'." |
443 | (let (walk-window-tree-buffer) | |
444 | (while walk-window-tree-window | |
445 | (setq walk-window-tree-buffer | |
446 | (window-buffer walk-window-tree-window)) | |
447 | (when (or walk-window-tree-buffer any) | |
c7635a97 | 448 | (funcall fun walk-window-tree-window)) |
85cc1f11 MR |
449 | (unless walk-window-tree-buffer |
450 | (walk-window-tree-1 | |
c7635a97 | 451 | fun (window-left-child walk-window-tree-window) any) |
85cc1f11 | 452 | (walk-window-tree-1 |
c7635a97 | 453 | fun (window-top-child walk-window-tree-window) any)) |
85cc1f11 MR |
454 | (if sub-only |
455 | (setq walk-window-tree-window nil) | |
456 | (setq walk-window-tree-window | |
457 | (window-right walk-window-tree-window)))))) | |
458 | ||
ea95074e | 459 | (defun walk-window-tree (fun &optional frame any minibuf) |
c7635a97 CY |
460 | "Run function FUN on each live window of FRAME. |
461 | FUN must be a function with one argument - a window. FRAME must | |
85cc1f11 | 462 | be a live frame and defaults to the selected one. ANY, if |
ea95074e | 463 | non-nil, means to run FUN on all live and internal windows of |
85cc1f11 MR |
464 | FRAME. |
465 | ||
ea95074e MR |
466 | Optional argument MINIBUF t means run FUN on FRAME's minibuffer |
467 | window even if it isn't active. MINIBUF nil or omitted means run | |
468 | FUN on FRAME's minibuffer window only if it's active. In both | |
469 | cases the minibuffer window must be part of FRAME. MINIBUF | |
470 | neither nil nor t means never run FUN on the minibuffer window. | |
471 | ||
85cc1f11 | 472 | This function performs a pre-order, depth-first traversal of the |
c7635a97 | 473 | window tree. If FUN changes the window tree, the result is |
85cc1f11 | 474 | unpredictable." |
ea95074e MR |
475 | (setq frame (window-normalize-frame frame)) |
476 | (walk-window-tree-1 fun (frame-root-window frame) any) | |
477 | (when (memq minibuf '(nil t)) | |
478 | ;; Run FUN on FRAME's minibuffer window if requested. | |
479 | (let ((minibuffer-window (minibuffer-window frame))) | |
480 | (when (and (window-live-p minibuffer-window) | |
481 | (eq (window-frame minibuffer-window) frame) | |
482 | (or (eq minibuf t) | |
483 | (minibuffer-window-active-p minibuffer-window))) | |
484 | (funcall fun minibuffer-window))))) | |
85cc1f11 | 485 | |
c7635a97 CY |
486 | (defun walk-window-subtree (fun &optional window any) |
487 | "Run function FUN on the subtree of windows rooted at WINDOW. | |
488 | WINDOW defaults to the selected window. FUN must be a function | |
489 | with one argument - a window. By default, run FUN only on live | |
be7f5545 | 490 | windows of the subtree. If the optional argument ANY is non-nil, |
c7635a97 CY |
491 | run FUN on all live and internal windows of the subtree. If |
492 | WINDOW is live, run FUN on WINDOW only. | |
85cc1f11 MR |
493 | |
494 | This function performs a pre-order, depth-first traversal of the | |
c7635a97 | 495 | subtree rooted at WINDOW. If FUN changes that tree, the result |
be7f5545 | 496 | is unpredictable." |
447f16b8 | 497 | (setq window (window-normalize-window window)) |
c7635a97 | 498 | (walk-window-tree-1 fun window any t)) |
85cc1f11 | 499 | |
ea95074e | 500 | (defun window-with-parameter (parameter &optional value frame any minibuf) |
85cc1f11 MR |
501 | "Return first window on FRAME with PARAMETER non-nil. |
502 | FRAME defaults to the selected frame. Optional argument VALUE | |
503 | non-nil means only return a window whose window-parameter value | |
382c953b | 504 | for PARAMETER equals VALUE (comparison is done with `equal'). |
85cc1f11 | 505 | Optional argument ANY non-nil means consider internal windows |
ea95074e MR |
506 | too. |
507 | ||
508 | Optional argument MINIBUF t means consider FRAME's minibuffer | |
509 | window even if it isn't active. MINIBUF nil or omitted means | |
510 | consider FRAME's minibuffer window only if it's active. In both | |
511 | cases the minibuffer window must be part of FRAME. MINIBUF | |
512 | neither nil nor t means never consider the minibuffer window." | |
cb882333 | 513 | (let (this-value) |
85cc1f11 MR |
514 | (catch 'found |
515 | (walk-window-tree | |
516 | (lambda (window) | |
517 | (when (and (setq this-value (window-parameter window parameter)) | |
518 | (or (not value) (equal value this-value))) | |
519 | (throw 'found window))) | |
ea95074e | 520 | frame any minibuf)))) |
85cc1f11 MR |
521 | |
522 | ;;; Atomic windows. | |
523 | (defun window-atom-root (&optional window) | |
524 | "Return root of atomic window WINDOW is a part of. | |
85c2386b | 525 | WINDOW must be a valid window and defaults to the selected one. |
382c953b | 526 | Return nil if WINDOW is not part of an atomic window." |
447f16b8 | 527 | (setq window (window-normalize-window window)) |
85cc1f11 MR |
528 | (let (root) |
529 | (while (and window (window-parameter window 'window-atom)) | |
530 | (setq root window) | |
531 | (setq window (window-parent window))) | |
532 | root)) | |
533 | ||
5386012d | 534 | (defun window-make-atom (window) |
85cc1f11 MR |
535 | "Make WINDOW an atomic window. |
536 | WINDOW must be an internal window. Return WINDOW." | |
537 | (if (not (window-child window)) | |
538 | (error "Window %s is not an internal window" window) | |
539 | (walk-window-subtree | |
540 | (lambda (window) | |
c660a885 MR |
541 | (unless (window-parameter window 'window-atom) |
542 | (set-window-parameter window 'window-atom t))) | |
85cc1f11 MR |
543 | window t) |
544 | window)) | |
545 | ||
caceae25 MR |
546 | (defun display-buffer-in-atom-window (buffer alist) |
547 | "Display BUFFER in an atomic window. | |
548 | This function displays BUFFER in a new window that will be | |
549 | combined with an existing window to form an atomic window. If | |
550 | the existing window is already part of an atomic window, add the | |
551 | new window to that atomic window. Operations like `split-window' | |
552 | or `delete-window', when applied to a constituent of an atomic | |
553 | window, are applied atomically to the root of that atomic window. | |
554 | ||
555 | ALIST is an association list of symbols and values. The | |
556 | following symbols can be used. | |
557 | ||
558 | `window' specifies the existing window the new window shall be | |
559 | combined with. Use `window-atom-root' to make the new window a | |
560 | sibling of an atomic window's root. If an internal window is | |
561 | specified here, all children of that window become part of the | |
562 | atomic window too. If no window is specified, the new window | |
c660a885 MR |
563 | becomes a sibling of the selected window. By default, the |
564 | `window-atom' parameter of the existing window is set to `main' | |
565 | provided it is live and was not set before. | |
caceae25 MR |
566 | |
567 | `side' denotes the side of the existing window where the new | |
568 | window shall be located. Valid values are `below', `right', | |
c660a885 MR |
569 | `above' and `left'. The default is `below'. By default, the |
570 | `window-atom' parameter of the new window is set to this value. | |
caceae25 MR |
571 | |
572 | The return value is the new window, nil when creating that window | |
573 | failed." | |
c660a885 MR |
574 | (let* ((ignore-window-parameters t) |
575 | (window-combination-limit t) | |
576 | (window-combination-resize 'atom) | |
577 | (window (cdr (assq 'window alist))) | |
578 | (side (cdr (assq 'side alist))) | |
579 | (atom (when window (window-parameter window 'window-atom))) | |
580 | root new) | |
caceae25 | 581 | (setq window (window-normalize-window window)) |
c660a885 MR |
582 | (setq root (window-atom-root window)) |
583 | ;; Split off new window. | |
caceae25 | 584 | (when (setq new (split-window window nil side)) |
c660a885 MR |
585 | (window-make-atom |
586 | (if (and root (not (eq root window))) | |
587 | ;; When WINDOW was part of an atomic window and we did not | |
588 | ;; split its root, root atomic window at old root. | |
589 | root | |
590 | ;; Otherwise, root atomic window at WINDOW's new parent. | |
591 | (window-parent window))) | |
592 | ;; Assign `window-atom' parameters, if needed. | |
593 | (when (and (not atom) (window-live-p window)) | |
594 | (set-window-parameter window 'window-atom 'main)) | |
595 | (set-window-parameter new 'window-atom side) | |
caceae25 MR |
596 | ;; Display BUFFER in NEW and return NEW. |
597 | (window--display-buffer | |
5938d519 | 598 | buffer new 'window alist display-buffer-mark-dedicated)))) |
caceae25 | 599 | |
54f9154c MR |
600 | (defun window--atom-check-1 (window) |
601 | "Subroutine of `window--atom-check'." | |
85cc1f11 MR |
602 | (when window |
603 | (if (window-parameter window 'window-atom) | |
604 | (let ((count 0)) | |
605 | (when (or (catch 'reset | |
606 | (walk-window-subtree | |
607 | (lambda (window) | |
608 | (if (window-parameter window 'window-atom) | |
609 | (setq count (1+ count)) | |
610 | (throw 'reset t))) | |
611 | window t)) | |
612 | ;; count >= 1 must hold here. If there's no other | |
613 | ;; window around dissolve this atomic window. | |
614 | (= count 1)) | |
615 | ;; Dissolve atomic window. | |
616 | (walk-window-subtree | |
617 | (lambda (window) | |
618 | (set-window-parameter window 'window-atom nil)) | |
619 | window t))) | |
620 | ;; Check children. | |
621 | (unless (window-buffer window) | |
54f9154c MR |
622 | (window--atom-check-1 (window-left-child window)) |
623 | (window--atom-check-1 (window-top-child window)))) | |
85cc1f11 | 624 | ;; Check right sibling |
54f9154c | 625 | (window--atom-check-1 (window-right window)))) |
85cc1f11 | 626 | |
54f9154c | 627 | (defun window--atom-check (&optional frame) |
85cc1f11 MR |
628 | "Check atomicity of all windows on FRAME. |
629 | FRAME defaults to the selected frame. If an atomic window is | |
be7f5545 MR |
630 | wrongly configured, reset the atomicity of all its windows on |
631 | FRAME to nil. An atomic window is wrongly configured if it has | |
632 | no child windows or one of its child windows is not atomic." | |
54f9154c | 633 | (window--atom-check-1 (frame-root-window frame))) |
85cc1f11 MR |
634 | |
635 | ;; Side windows. | |
636 | (defvar window-sides '(left top right bottom) | |
637 | "Window sides.") | |
638 | ||
639 | (defcustom window-sides-vertical nil | |
640 | "If non-nil, left and right side windows are full height. | |
641 | Otherwise, top and bottom side windows are full width." | |
642 | :type 'boolean | |
643 | :group 'windows | |
644 | :version "24.1") | |
645 | ||
646 | (defcustom window-sides-slots '(nil nil nil nil) | |
647 | "Maximum number of side window slots. | |
648 | The value is a list of four elements specifying the number of | |
382c953b | 649 | side window slots on (in this order) the left, top, right and |
85cc1f11 MR |
650 | bottom side of each frame. If an element is a number, this means |
651 | to display at most that many side windows on the corresponding | |
652 | side. If an element is nil, this means there's no bound on the | |
653 | number of slots on that side." | |
2bed3f04 | 654 | :version "24.1" |
85cc1f11 MR |
655 | :risky t |
656 | :type | |
657 | '(list | |
658 | :value (nil nil nil nil) | |
659 | (choice | |
660 | :tag "Left" | |
661 | :help-echo "Maximum slots of left side window." | |
662 | :value nil | |
663 | :format "%[Left%] %v\n" | |
664 | (const :tag "Unlimited" :format "%t" nil) | |
665 | (integer :tag "Number" :value 2 :size 5)) | |
666 | (choice | |
667 | :tag "Top" | |
668 | :help-echo "Maximum slots of top side window." | |
669 | :value nil | |
670 | :format "%[Top%] %v\n" | |
671 | (const :tag "Unlimited" :format "%t" nil) | |
672 | (integer :tag "Number" :value 3 :size 5)) | |
673 | (choice | |
674 | :tag "Right" | |
675 | :help-echo "Maximum slots of right side window." | |
676 | :value nil | |
677 | :format "%[Right%] %v\n" | |
678 | (const :tag "Unlimited" :format "%t" nil) | |
679 | (integer :tag "Number" :value 2 :size 5)) | |
680 | (choice | |
681 | :tag "Bottom" | |
682 | :help-echo "Maximum slots of bottom side window." | |
683 | :value nil | |
684 | :format "%[Bottom%] %v\n" | |
685 | (const :tag "Unlimited" :format "%t" nil) | |
686 | (integer :tag "Number" :value 3 :size 5))) | |
687 | :group 'windows) | |
688 | ||
caceae25 MR |
689 | (defun window--major-non-side-window (&optional frame) |
690 | "Return the major non-side window of frame FRAME. | |
691 | The optional argument FRAME must be a live frame and defaults to | |
692 | the selected one. | |
693 | ||
694 | If FRAME has at least one side window, the major non-side window | |
695 | is either an internal non-side window such that all other | |
696 | non-side windows on FRAME descend from it, or the single live | |
697 | non-side window of FRAME. If FRAME has no side windows, return | |
698 | its root window." | |
699 | (let ((frame (window-normalize-frame frame)) | |
700 | major sibling) | |
701 | ;; Set major to the _last_ window found by `walk-window-tree' that | |
702 | ;; is not a side window but has a side window as its sibling. | |
703 | (walk-window-tree | |
704 | (lambda (window) | |
705 | (and (not (window-parameter window 'window-side)) | |
706 | (or (and (setq sibling (window-prev-sibling window)) | |
707 | (window-parameter sibling 'window-side)) | |
708 | (and (setq sibling (window-next-sibling window)) | |
709 | (window-parameter sibling 'window-side))) | |
710 | (setq major window))) | |
c660a885 | 711 | frame t 'nomini) |
caceae25 MR |
712 | (or major (frame-root-window frame)))) |
713 | ||
714 | (defun window--major-side-window (side) | |
715 | "Return major side window on SIDE. | |
716 | SIDE must be one of the symbols `left', `top', `right' or | |
717 | `bottom'. Return nil if no such window exists." | |
718 | (let ((root (frame-root-window)) | |
719 | window) | |
720 | ;; (1) If a window on the opposite side exists, return that window's | |
721 | ;; sibling. | |
722 | ;; (2) If the new window shall span the entire side, return the | |
723 | ;; frame's root window. | |
724 | ;; (3) If a window on an orthogonal side exists, return that | |
725 | ;; window's sibling. | |
726 | ;; (4) Otherwise return the frame's root window. | |
727 | (cond | |
728 | ((or (and (eq side 'left) | |
729 | (setq window (window-with-parameter 'window-side 'right nil t))) | |
730 | (and (eq side 'top) | |
731 | (setq window (window-with-parameter 'window-side 'bottom nil t)))) | |
732 | (window-prev-sibling window)) | |
733 | ((or (and (eq side 'right) | |
734 | (setq window (window-with-parameter 'window-side 'left nil t))) | |
735 | (and (eq side 'bottom) | |
736 | (setq window (window-with-parameter 'window-side 'top nil t)))) | |
737 | (window-next-sibling window)) | |
738 | ((memq side '(left right)) | |
739 | (cond | |
740 | (window-sides-vertical | |
741 | root) | |
742 | ((setq window (window-with-parameter 'window-side 'top nil t)) | |
743 | (window-next-sibling window)) | |
744 | ((setq window (window-with-parameter 'window-side 'bottom nil t)) | |
745 | (window-prev-sibling window)) | |
746 | (t root))) | |
747 | ((memq side '(top bottom)) | |
748 | (cond | |
749 | ((not window-sides-vertical) | |
750 | root) | |
751 | ((setq window (window-with-parameter 'window-side 'left nil t)) | |
752 | (window-next-sibling window)) | |
753 | ((setq window (window-with-parameter 'window-side 'right nil t)) | |
754 | (window-prev-sibling window)) | |
755 | (t root)))))) | |
756 | ||
757 | (defun display-buffer-in-major-side-window (buffer side slot &optional alist) | |
758 | "Display BUFFER in a new window on SIDE of the selected frame. | |
759 | SIDE must be one of `left', `top', `right' or `bottom'. SLOT | |
760 | specifies the slot to use. ALIST is an association list of | |
761 | symbols and values as passed to `display-buffer-in-side-window'. | |
762 | This function may be called only if no window on SIDE exists yet. | |
763 | The new window automatically becomes the \"major\" side window on | |
764 | SIDE. Return the new window, nil if its creation window failed." | |
9d3aa82c | 765 | (let* ((left-or-right (memq side '(left right))) |
caceae25 | 766 | (major (window--major-side-window side)) |
caceae25 MR |
767 | (on-side (cond |
768 | ((eq side 'top) 'above) | |
769 | ((eq side 'bottom) 'below) | |
770 | (t side))) | |
771 | ;; The following two bindings will tell `split-window' to take | |
772 | ;; the space for the new window from `major' and not make a new | |
773 | ;; parent window unless needed. | |
774 | (window-combination-resize 'side) | |
775 | (window-combination-limit nil) | |
9d3aa82c | 776 | (new (split-window major nil on-side))) |
caceae25 MR |
777 | (when new |
778 | ;; Initialize `window-side' parameter of new window to SIDE. | |
779 | (set-window-parameter new 'window-side side) | |
780 | ;; Install `window-slot' parameter of new window. | |
781 | (set-window-parameter new 'window-slot slot) | |
782 | ;; Install `delete-window' parameter thus making sure that when | |
783 | ;; the new window is deleted, a side window on the opposite side | |
784 | ;; does not get resized. | |
785 | (set-window-parameter new 'delete-window 'delete-side-window) | |
5938d519 MR |
786 | ;; Auto-adjust height/width of new window unless a size has been |
787 | ;; explicitly requested. | |
735135f9 | 788 | (unless (if left-or-right |
5938d519 MR |
789 | (cdr (assq 'window-width alist)) |
790 | (cdr (assq 'window-height alist))) | |
791 | (setq alist | |
792 | (cons | |
793 | (cons | |
794 | (if left-or-right 'window-width 'window-height) | |
795 | (/ (window-total-size (frame-root-window) left-or-right) | |
880e6158 MR |
796 | ;; By default use a fourth of the size of the frame's |
797 | ;; root window. | |
5938d519 MR |
798 | 4)) |
799 | alist))) | |
caceae25 | 800 | ;; Install BUFFER in new window and return NEW. |
5938d519 | 801 | (window--display-buffer buffer new 'window alist 'side)))) |
caceae25 MR |
802 | |
803 | (defun delete-side-window (window) | |
804 | "Delete side window WINDOW." | |
805 | (let ((window-combination-resize | |
806 | (window-parameter (window-parent window) 'window-side)) | |
807 | (ignore-window-parameters t)) | |
808 | (delete-window window))) | |
809 | ||
810 | (defun display-buffer-in-side-window (buffer alist) | |
3c29190f | 811 | "Display BUFFER in a side window of the selected frame. |
caceae25 | 812 | ALIST is an association list of symbols and values. The |
3c29190f | 813 | following special symbols can be used in ALIST. |
caceae25 | 814 | |
3c29190f MR |
815 | `side' denotes the side of the frame where the new window shall |
816 | be located. Valid values are `bottom', `right', `top' and | |
817 | `left'. The default is `bottom'. | |
caceae25 MR |
818 | |
819 | `slot' if non-nil, specifies the window slot where to display | |
820 | BUFFER. A value of zero or nil means use the middle slot on | |
821 | the specified side. A negative value means use a slot | |
822 | preceding (that is, above or on the left of) the middle slot. | |
823 | A positive value means use a slot following (that is, below or | |
824 | on the right of) the middle slot. The default is zero." | |
825 | (let ((side (or (cdr (assq 'side alist)) 'bottom)) | |
9d3aa82c | 826 | (slot (or (cdr (assq 'slot alist)) 0))) |
caceae25 MR |
827 | (cond |
828 | ((not (memq side '(top bottom left right))) | |
829 | (error "Invalid side %s specified" side)) | |
830 | ((not (numberp slot)) | |
831 | (error "Invalid slot %s specified" slot))) | |
832 | ||
833 | (let* ((major (window-with-parameter 'window-side side nil t)) | |
834 | ;; `major' is the major window on SIDE, `windows' the list of | |
835 | ;; life windows on SIDE. | |
836 | (windows | |
837 | (when major | |
838 | (let (windows) | |
839 | (walk-window-tree | |
840 | (lambda (window) | |
841 | (when (eq (window-parameter window 'window-side) side) | |
c660a885 MR |
842 | (setq windows (cons window windows)))) |
843 | nil nil 'nomini) | |
caceae25 MR |
844 | (nreverse windows)))) |
845 | (slots (when major (max 1 (window-child-count major)))) | |
846 | (max-slots | |
847 | (nth (cond | |
848 | ((eq side 'left) 0) | |
849 | ((eq side 'top) 1) | |
850 | ((eq side 'right) 2) | |
851 | ((eq side 'bottom) 3)) | |
852 | window-sides-slots)) | |
caceae25 | 853 | window this-window this-slot prev-window next-window |
9d3aa82c | 854 | best-window best-slot abs-slot) |
caceae25 MR |
855 | |
856 | (cond | |
857 | ((and (numberp max-slots) (<= max-slots 0)) | |
858 | ;; No side-slots available on this side. Don't create an error, | |
859 | ;; just return nil. | |
860 | nil) | |
861 | ((not windows) | |
862 | ;; No major window exists on this side, make one. | |
863 | (display-buffer-in-major-side-window buffer side slot alist)) | |
864 | (t | |
865 | ;; Scan windows on SIDE. | |
866 | (catch 'found | |
867 | (dolist (window windows) | |
868 | (setq this-slot (window-parameter window 'window-slot)) | |
869 | (cond | |
870 | ;; The following should not happen and probably be checked | |
871 | ;; by window--side-check. | |
872 | ((not (numberp this-slot))) | |
873 | ((= this-slot slot) | |
874 | ;; A window with a matching slot has been found. | |
875 | (setq this-window window) | |
876 | (throw 'found t)) | |
877 | (t | |
878 | ;; Check if this window has a better slot value wrt the | |
879 | ;; slot of the window we want. | |
880 | (setq abs-slot | |
881 | (if (or (and (> this-slot 0) (> slot 0)) | |
882 | (and (< this-slot 0) (< slot 0))) | |
883 | (abs (- slot this-slot)) | |
884 | (+ (abs slot) (abs this-slot)))) | |
885 | (unless (and best-slot (<= best-slot abs-slot)) | |
886 | (setq best-window window) | |
887 | (setq best-slot abs-slot)) | |
888 | (cond | |
889 | ((<= this-slot slot) | |
890 | (setq prev-window window)) | |
891 | ((not next-window) | |
892 | (setq next-window window))))))) | |
893 | ||
894 | ;; `this-window' is the first window with the same SLOT. | |
895 | ;; `prev-window' is the window with the largest slot < SLOT. A new | |
896 | ;; window will be created after it. | |
897 | ;; `next-window' is the window with the smallest slot > SLOT. A new | |
898 | ;; window will be created before it. | |
899 | ;; `best-window' is the window with the smallest absolute difference | |
900 | ;; of its slot and SLOT. | |
901 | ||
902 | ;; Note: We dedicate the window used softly to its buffer to | |
903 | ;; avoid that "other" (non-side) buffer display functions steal | |
904 | ;; it from us. This must eventually become customizable via | |
905 | ;; ALIST (or, better, avoided in the "other" functions). | |
906 | (or (and this-window | |
907 | ;; Reuse `this-window'. | |
5938d519 | 908 | (window--display-buffer buffer this-window 'reuse alist 'side)) |
caceae25 MR |
909 | (and (or (not max-slots) (< slots max-slots)) |
910 | (or (and next-window | |
911 | ;; Make new window before `next-window'. | |
912 | (let ((next-side | |
913 | (if (memq side '(left right)) 'above 'left)) | |
914 | (window-combination-resize 'side)) | |
915 | (setq window (split-window next-window nil next-side)) | |
916 | ;; When the new window is deleted, its space | |
917 | ;; is returned to other side windows. | |
918 | (set-window-parameter | |
919 | window 'delete-window 'delete-side-window) | |
920 | window)) | |
921 | (and prev-window | |
922 | ;; Make new window after `prev-window'. | |
923 | (let ((prev-side | |
924 | (if (memq side '(left right)) 'below 'right)) | |
925 | (window-combination-resize 'side)) | |
926 | (setq window (split-window prev-window nil prev-side)) | |
927 | ;; When the new window is deleted, its space | |
928 | ;; is returned to other side windows. | |
929 | (set-window-parameter | |
930 | window 'delete-window 'delete-side-window) | |
931 | window))) | |
932 | (set-window-parameter window 'window-slot slot) | |
5938d519 | 933 | (window--display-buffer buffer window 'window alist 'side)) |
caceae25 MR |
934 | (and best-window |
935 | ;; Reuse `best-window'. | |
936 | (progn | |
937 | ;; Give best-window the new slot value. | |
938 | (set-window-parameter best-window 'window-slot slot) | |
5938d519 MR |
939 | (window--display-buffer |
940 | buffer best-window 'reuse alist 'side))))))))) | |
caceae25 | 941 | |
54f9154c | 942 | (defun window--side-check (&optional frame) |
caceae25 MR |
943 | "Check the side window configuration of FRAME. |
944 | FRAME defaults to the selected frame. | |
945 | ||
946 | A valid side window configuration preserves the following two | |
947 | invariants: | |
948 | ||
949 | - If there exists a window whose window-side parameter is | |
950 | non-nil, there must exist at least one live window whose | |
951 | window-side parameter is nil. | |
952 | ||
953 | - If a window W has a non-nil window-side parameter (i) it must | |
954 | have a parent window and that parent's window-side parameter | |
955 | must be either nil or the same as for W, and (ii) any child | |
956 | window of W must have the same window-side parameter as W. | |
957 | ||
958 | If the configuration is invalid, reset the window-side parameters | |
959 | of all windows on FRAME to nil." | |
960 | (let (left top right bottom none side parent parent-side) | |
85cc1f11 MR |
961 | (when (or (catch 'reset |
962 | (walk-window-tree | |
963 | (lambda (window) | |
964 | (setq side (window-parameter window 'window-side)) | |
965 | (setq parent (window-parent window)) | |
966 | (setq parent-side | |
967 | (and parent (window-parameter parent 'window-side))) | |
968 | ;; The following `cond' seems a bit tedious, but I'd | |
969 | ;; rather stick to using just the stack. | |
970 | (cond | |
971 | (parent-side | |
972 | (when (not (eq parent-side side)) | |
973 | ;; A parent whose window-side is non-nil must | |
974 | ;; have a child with the same window-side. | |
975 | (throw 'reset t))) | |
caceae25 MR |
976 | ((not side) |
977 | (when (window-buffer window) | |
978 | ;; Record that we have at least one non-side, | |
979 | ;; live window. | |
85cc1f11 | 980 | (setq none t))) |
caceae25 MR |
981 | ((if (memq side '(left top)) |
982 | (window-prev-sibling window) | |
983 | (window-next-sibling window)) | |
984 | ;; Left and top major side windows must not have a | |
985 | ;; previous sibling, right and bottom major side | |
986 | ;; windows must not have a next sibling. | |
987 | (throw 'reset t)) | |
988 | ;; Now check that there's no more than one major | |
989 | ;; window for any of left, top, right and bottom. | |
85cc1f11 | 990 | ((eq side 'left) |
caceae25 | 991 | (if left (throw 'reset t) (setq left t))) |
85cc1f11 | 992 | ((eq side 'top) |
caceae25 | 993 | (if top (throw 'reset t) (setq top t))) |
85cc1f11 | 994 | ((eq side 'right) |
caceae25 | 995 | (if right (throw 'reset t) (setq right t))) |
85cc1f11 | 996 | ((eq side 'bottom) |
caceae25 | 997 | (if bottom (throw 'reset t) (setq bottom t))) |
42019c2e | 998 | (t |
caceae25 | 999 | (throw 'reset t)))) |
c660a885 | 1000 | frame t 'nomini)) |
caceae25 MR |
1001 | ;; If there's a side window, there must be at least one |
1002 | ;; non-side window. | |
1003 | (and (or left top right bottom) (not none))) | |
85cc1f11 MR |
1004 | (walk-window-tree |
1005 | (lambda (window) | |
1006 | (set-window-parameter window 'window-side nil)) | |
c660a885 | 1007 | frame t 'nomini)))) |
85cc1f11 | 1008 | |
54f9154c | 1009 | (defun window--check (&optional frame) |
85cc1f11 MR |
1010 | "Check atomic and side windows on FRAME. |
1011 | FRAME defaults to the selected frame." | |
54f9154c MR |
1012 | (window--side-check frame) |
1013 | (window--atom-check frame)) | |
85cc1f11 MR |
1014 | |
1015 | ;;; Window sizes. | |
880e6158 MR |
1016 | (defun window-total-size (&optional window horizontal) |
1017 | "Return the total height or width of WINDOW. | |
1018 | WINDOW must be a valid window and defaults to the selected one. | |
1019 | ||
1020 | If HORIZONTAL is omitted or nil, return the total height of | |
1021 | WINDOW, in lines, like `window-total-height'. Otherwise return | |
1022 | the total width, in columns, like `window-total-width'." | |
1023 | (if horizontal | |
1024 | (window-total-width window) | |
1025 | (window-total-height window))) | |
1026 | ||
1027 | (defun window-size (&optional window horizontal pixelwise) | |
1028 | "Return the height or width of WINDOW. | |
1029 | WINDOW must be a valid window and defaults to the selected one. | |
1030 | ||
1031 | If HORIZONTAL is omitted or nil, return the total height of | |
1032 | WINDOW, in lines, like `window-total-height'. Otherwise return | |
1033 | the total width, in columns, like `window-total-width'. | |
1034 | ||
1035 | Optional argument PIXELWISE means return the pixel size of WINDOW | |
1036 | like `window-pixel-height' and `window-pixel-width'." | |
1037 | (if horizontal | |
1038 | (if pixelwise | |
1039 | (window-pixel-width window) | |
1040 | (window-total-width window)) | |
1041 | (if pixelwise | |
1042 | (window-pixel-height window) | |
1043 | (window-total-height window)))) | |
1044 | ||
85cc1f11 MR |
1045 | (defvar window-size-fixed nil |
1046 | "Non-nil in a buffer means windows displaying the buffer are fixed-size. | |
1047 | If the value is `height', then only the window's height is fixed. | |
1048 | If the value is `width', then only the window's width is fixed. | |
1049 | Any other non-nil value fixes both the width and the height. | |
1050 | ||
1051 | Emacs won't change the size of any window displaying that buffer, | |
382c953b | 1052 | unless it has no other choice (like when deleting a neighboring |
85cc1f11 MR |
1053 | window).") |
1054 | (make-variable-buffer-local 'window-size-fixed) | |
1055 | ||
842e3a93 | 1056 | (defun window--size-ignore-p (window ignore) |
a1511caf | 1057 | "Return non-nil if IGNORE says to ignore size restrictions for WINDOW." |
24300f5f | 1058 | (if (window-valid-p ignore) (eq window ignore) ignore)) |
a1511caf | 1059 | |
880e6158 MR |
1060 | (defun window-safe-min-size (&optional window horizontal pixelwise) |
1061 | "Return safe minimum size of WINDOW. | |
1062 | WINDOW must be a valid window and defaults to the selected one. | |
1063 | Optional argument HORIZONTAL non-nil means return the minimum | |
1064 | number of columns of WINDOW; otherwise return the minimum number | |
1065 | of WINDOW's lines. | |
1066 | ||
1067 | Optional argument PIXELWISE non-nil means return the minimum pixel-size | |
1068 | of WINDOW." | |
1069 | (setq window (window-normalize-window window)) | |
1070 | (if pixelwise | |
1071 | (if horizontal | |
1072 | (* window-safe-min-width | |
1073 | (frame-char-width (window-frame window))) | |
1074 | (* window-safe-min-height | |
1075 | (frame-char-height (window-frame window)))) | |
1076 | (if horizontal window-safe-min-width window-safe-min-height))) | |
1077 | ||
1078 | (defun window-min-size (&optional window horizontal ignore pixelwise) | |
2116e93c | 1079 | "Return the minimum size of WINDOW. |
85c2386b MR |
1080 | WINDOW must be a valid window and defaults to the selected one. |
1081 | Optional argument HORIZONTAL non-nil means return the minimum | |
1082 | number of columns of WINDOW; otherwise return the minimum number | |
1083 | of WINDOW's lines. | |
a1511caf | 1084 | |
2116e93c | 1085 | Optional argument IGNORE, if non-nil, means ignore restrictions |
a1511caf | 1086 | imposed by fixed size windows, `window-min-height' or |
2116e93c | 1087 | `window-min-width' settings. If IGNORE equals `safe', live |
a1511caf | 1088 | windows may get as small as `window-safe-min-height' lines and |
2116e93c EZ |
1089 | `window-safe-min-width' columns. If IGNORE is a window, ignore |
1090 | restrictions for that window only. Any other non-nil value | |
880e6158 MR |
1091 | means ignore all of the above restrictions for all windows. |
1092 | ||
1093 | Optional argument PIXELWISE non-nil means return the minimum pixel-size | |
1094 | of WINDOW." | |
c56cad4a | 1095 | (window--min-size-1 |
880e6158 | 1096 | (window-normalize-window window) horizontal ignore pixelwise)) |
a1511caf | 1097 | |
880e6158 | 1098 | (defun window--min-size-1 (window horizontal ignore pixelwise) |
a1511caf MR |
1099 | "Internal function of `window-min-size'." |
1100 | (let ((sub (window-child window))) | |
1101 | (if sub | |
1102 | (let ((value 0)) | |
1103 | ;; WINDOW is an internal window. | |
3d8daefe | 1104 | (if (window-combined-p sub horizontal) |
a1511caf | 1105 | ;; The minimum size of an iso-combination is the sum of |
be7f5545 | 1106 | ;; the minimum sizes of its child windows. |
a1511caf MR |
1107 | (while sub |
1108 | (setq value (+ value | |
880e6158 MR |
1109 | (window--min-size-1 |
1110 | sub horizontal ignore pixelwise))) | |
a1511caf | 1111 | (setq sub (window-right sub))) |
c660a885 MR |
1112 | ;; The minimum size of an ortho-combination is the maximum |
1113 | ;; of the minimum sizes of its child windows. | |
a1511caf MR |
1114 | (while sub |
1115 | (setq value (max value | |
880e6158 MR |
1116 | (window--min-size-1 |
1117 | sub horizontal ignore pixelwise))) | |
a1511caf MR |
1118 | (setq sub (window-right sub)))) |
1119 | value) | |
1120 | (with-current-buffer (window-buffer window) | |
1121 | (cond | |
842e3a93 | 1122 | ((and (not (window--size-ignore-p window ignore)) |
a1511caf MR |
1123 | (window-size-fixed-p window horizontal)) |
1124 | ;; The minimum size of a fixed size window is its size. | |
880e6158 | 1125 | (window-size window horizontal pixelwise)) |
a1511caf MR |
1126 | ((or (eq ignore 'safe) (eq ignore window)) |
1127 | ;; If IGNORE equals `safe' or WINDOW return the safe values. | |
880e6158 | 1128 | (window-safe-min-size window horizontal pixelwise)) |
a1511caf MR |
1129 | (horizontal |
1130 | ;; For the minimum width of a window take fringes and | |
1131 | ;; scroll-bars into account. This is questionable and should | |
1132 | ;; be removed as soon as we are able to split (and resize) | |
1133 | ;; windows such that the new (or resized) windows can get a | |
1134 | ;; size less than the user-specified `window-min-height' and | |
1135 | ;; `window-min-width'. | |
1136 | (let ((frame (window-frame window)) | |
1137 | (fringes (window-fringes window)) | |
1138 | (scroll-bars (window-scroll-bars window))) | |
880e6158 MR |
1139 | (if pixelwise |
1140 | (max | |
1141 | (+ (window-safe-min-size window t t) | |
1142 | (car fringes) (cadr fringes) | |
1143 | (cond | |
1144 | ((memq (nth 2 scroll-bars) '(left right)) | |
1145 | (nth 1 scroll-bars)) | |
1146 | ((memq (frame-parameter frame 'vertical-scroll-bars) | |
1147 | '(left right)) | |
1148 | (frame-parameter frame 'scroll-bar-width)) | |
1149 | (t 0))) | |
1150 | (if (window--size-ignore-p window ignore) | |
1151 | 0 | |
1152 | (window-min-pixel-width))) | |
1153 | (max | |
1154 | (+ window-safe-min-width | |
1155 | (ceiling (car fringes) (frame-char-width frame)) | |
1156 | (ceiling (cadr fringes) (frame-char-width frame)) | |
1157 | (cond | |
1158 | ((memq (nth 2 scroll-bars) '(left right)) | |
1159 | (nth 1 scroll-bars)) | |
a1511caf MR |
1160 | ((memq (frame-parameter frame 'vertical-scroll-bars) |
1161 | '(left right)) | |
1162 | (ceiling (or (frame-parameter frame 'scroll-bar-width) 14) | |
1163 | (frame-char-width))) | |
1164 | (t 0))) | |
880e6158 MR |
1165 | (if (window--size-ignore-p window ignore) |
1166 | 0 | |
1167 | window-min-width))))) | |
1168 | (pixelwise | |
f224e500 | 1169 | (max |
880e6158 MR |
1170 | (+ (window-safe-min-size window nil t) |
1171 | (window-header-line-height window) | |
1172 | (window-mode-line-height window)) | |
1173 | (if (window--size-ignore-p window ignore) | |
1174 | 0 | |
1175 | (window-min-pixel-height)))) | |
a1511caf MR |
1176 | (t |
1177 | ;; For the minimum height of a window take any mode- or | |
1178 | ;; header-line into account. | |
1179 | (max (+ window-safe-min-height | |
1180 | (if header-line-format 1 0) | |
1181 | (if mode-line-format 1 0)) | |
880e6158 MR |
1182 | (if (window--size-ignore-p window ignore) |
1183 | 0 | |
1184 | window-min-height)))))))) | |
a1511caf | 1185 | |
880e6158 | 1186 | (defun window-sizable (window delta &optional horizontal ignore pixelwise) |
a1511caf | 1187 | "Return DELTA if DELTA lines can be added to WINDOW. |
85c2386b | 1188 | WINDOW must be a valid window and defaults to the selected one. |
a1511caf MR |
1189 | Optional argument HORIZONTAL non-nil means return DELTA if DELTA |
1190 | columns can be added to WINDOW. A return value of zero means | |
1191 | that no lines (or columns) can be added to WINDOW. | |
1192 | ||
be7f5545 MR |
1193 | This function looks only at WINDOW and, recursively, its child |
1194 | windows. The function `window-resizable' looks at other windows | |
1195 | as well. | |
a1511caf MR |
1196 | |
1197 | DELTA positive means WINDOW shall be enlarged by DELTA lines or | |
1198 | columns. If WINDOW cannot be enlarged by DELTA lines or columns | |
1199 | return the maximum value in the range 0..DELTA by which WINDOW | |
1200 | can be enlarged. | |
1201 | ||
1202 | DELTA negative means WINDOW shall be shrunk by -DELTA lines or | |
1203 | columns. If WINDOW cannot be shrunk by -DELTA lines or columns, | |
1204 | return the minimum value in the range DELTA..0 by which WINDOW | |
1205 | can be shrunk. | |
1206 | ||
2116e93c | 1207 | Optional argument IGNORE non-nil means ignore restrictions |
a1511caf | 1208 | imposed by fixed size windows, `window-min-height' or |
2116e93c | 1209 | `window-min-width' settings. If IGNORE equals `safe', live |
a1511caf | 1210 | windows may get as small as `window-safe-min-height' lines and |
2116e93c EZ |
1211 | `window-safe-min-width' columns. If IGNORE is a window, ignore |
1212 | restrictions for that window only. Any other non-nil value means | |
880e6158 MR |
1213 | ignore all of the above restrictions for all windows. |
1214 | ||
1215 | Optional argument PIXELWISE non-nil means interpret DELTA as | |
1216 | pixels." | |
447f16b8 | 1217 | (setq window (window-normalize-window window)) |
a1511caf MR |
1218 | (cond |
1219 | ((< delta 0) | |
880e6158 MR |
1220 | (max (- (window-min-size window horizontal ignore pixelwise) |
1221 | (window-size window horizontal pixelwise)) | |
a1511caf | 1222 | delta)) |
842e3a93 | 1223 | ((window--size-ignore-p window ignore) |
a1511caf MR |
1224 | delta) |
1225 | ((> delta 0) | |
1226 | (if (window-size-fixed-p window horizontal) | |
1227 | 0 | |
1228 | delta)) | |
1229 | (t 0))) | |
1230 | ||
880e6158 | 1231 | (defun window-sizable-p (window delta &optional horizontal ignore pixelwise) |
a1511caf | 1232 | "Return t if WINDOW can be resized by DELTA lines. |
85c2386b | 1233 | WINDOW must be a valid window and defaults to the selected one. |
a1511caf MR |
1234 | For the meaning of the arguments of this function see the |
1235 | doc-string of `window-sizable'." | |
447f16b8 | 1236 | (setq window (window-normalize-window window)) |
a1511caf | 1237 | (if (> delta 0) |
880e6158 MR |
1238 | (>= (window-sizable window delta horizontal ignore pixelwise) |
1239 | delta) | |
1240 | (<= (window-sizable window delta horizontal ignore pixelwise) | |
1241 | delta))) | |
a1511caf | 1242 | |
5e92ca23 | 1243 | (defun window--size-fixed-1 (window horizontal) |
a1511caf MR |
1244 | "Internal function for `window-size-fixed-p'." |
1245 | (let ((sub (window-child window))) | |
1246 | (catch 'fixed | |
1247 | (if sub | |
1248 | ;; WINDOW is an internal window. | |
3d8daefe | 1249 | (if (window-combined-p sub horizontal) |
be7f5545 MR |
1250 | ;; An iso-combination is fixed size if all its child |
1251 | ;; windows are fixed-size. | |
a1511caf MR |
1252 | (progn |
1253 | (while sub | |
5e92ca23 | 1254 | (unless (window--size-fixed-1 sub horizontal) |
be7f5545 MR |
1255 | ;; We found a non-fixed-size child window, so |
1256 | ;; WINDOW's size is not fixed. | |
a1511caf MR |
1257 | (throw 'fixed nil)) |
1258 | (setq sub (window-right sub))) | |
be7f5545 | 1259 | ;; All child windows are fixed-size, so WINDOW's size is |
a1511caf MR |
1260 | ;; fixed. |
1261 | (throw 'fixed t)) | |
1262 | ;; An ortho-combination is fixed-size if at least one of its | |
be7f5545 | 1263 | ;; child windows is fixed-size. |
a1511caf | 1264 | (while sub |
5e92ca23 | 1265 | (when (window--size-fixed-1 sub horizontal) |
be7f5545 MR |
1266 | ;; We found a fixed-size child window, so WINDOW's size |
1267 | ;; is fixed. | |
a1511caf MR |
1268 | (throw 'fixed t)) |
1269 | (setq sub (window-right sub)))) | |
1270 | ;; WINDOW is a live window. | |
1271 | (with-current-buffer (window-buffer window) | |
1272 | (if horizontal | |
1273 | (memq window-size-fixed '(width t)) | |
1274 | (memq window-size-fixed '(height t)))))))) | |
1275 | ||
1276 | (defun window-size-fixed-p (&optional window horizontal) | |
1277 | "Return non-nil if WINDOW's height is fixed. | |
85c2386b MR |
1278 | WINDOW must be a valid window and defaults to the selected one. |
1279 | Optional argument HORIZONTAL non-nil means return non-nil if | |
1280 | WINDOW's width is fixed. | |
a1511caf MR |
1281 | |
1282 | If this function returns nil, this does not necessarily mean that | |
2cffd681 MR |
1283 | WINDOW can be resized in the desired direction. The function |
1284 | `window-resizable' can tell that." | |
5e92ca23 | 1285 | (window--size-fixed-1 |
447f16b8 | 1286 | (window-normalize-window window) horizontal)) |
a1511caf | 1287 | |
880e6158 | 1288 | (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise) |
a1511caf MR |
1289 | "Internal function for `window-min-delta'." |
1290 | (if (not (window-parent window)) | |
1291 | ;; If we can't go up, return zero. | |
1292 | 0 | |
1293 | ;; Else try to find a non-fixed-size sibling of WINDOW. | |
1294 | (let* ((parent (window-parent window)) | |
1295 | (sub (window-child parent))) | |
1296 | (catch 'done | |
3d8daefe | 1297 | (if (window-combined-p sub horizontal) |
a1511caf | 1298 | ;; In an iso-combination throw DELTA if we find at least one |
be7f5545 MR |
1299 | ;; child window and that window is either not fixed-size or |
1300 | ;; we can ignore fixed-sizeness. | |
a1511caf MR |
1301 | (let ((skip (eq trail 'after))) |
1302 | (while sub | |
1303 | (cond | |
1304 | ((eq sub window) | |
1305 | (setq skip (eq trail 'before))) | |
1306 | (skip) | |
842e3a93 | 1307 | ((and (not (window--size-ignore-p window ignore)) |
a1511caf MR |
1308 | (window-size-fixed-p sub horizontal))) |
1309 | (t | |
be7f5545 | 1310 | ;; We found a non-fixed-size child window. |
a1511caf MR |
1311 | (throw 'done delta))) |
1312 | (setq sub (window-right sub)))) | |
1313 | ;; In an ortho-combination set DELTA to the minimum value by | |
be7f5545 | 1314 | ;; which other child windows can shrink. |
a1511caf MR |
1315 | (while sub |
1316 | (unless (eq sub window) | |
1317 | (setq delta | |
1318 | (min delta | |
880e6158 MR |
1319 | (- (window-size sub horizontal pixelwise) |
1320 | (window-min-size | |
1321 | sub horizontal ignore pixelwise))))) | |
a1511caf MR |
1322 | (setq sub (window-right sub)))) |
1323 | (if noup | |
1324 | delta | |
880e6158 MR |
1325 | (window--min-delta-1 |
1326 | parent delta horizontal ignore trail nil pixelwise)))))) | |
a1511caf | 1327 | |
880e6158 | 1328 | (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise) |
a1511caf | 1329 | "Return number of lines by which WINDOW can be shrunk. |
85c2386b MR |
1330 | WINDOW must be a valid window and defaults to the selected one. |
1331 | Return zero if WINDOW cannot be shrunk. | |
a1511caf MR |
1332 | |
1333 | Optional argument HORIZONTAL non-nil means return number of | |
1334 | columns by which WINDOW can be shrunk. | |
1335 | ||
2116e93c | 1336 | Optional argument IGNORE non-nil means ignore restrictions |
a1511caf | 1337 | imposed by fixed size windows, `window-min-height' or |
2116e93c EZ |
1338 | `window-min-width' settings. If IGNORE is a window, ignore |
1339 | restrictions for that window only. If IGNORE equals `safe', | |
a1511caf | 1340 | live windows may get as small as `window-safe-min-height' lines |
2116e93c EZ |
1341 | and `window-safe-min-width' columns. Any other non-nil value |
1342 | means ignore all of the above restrictions for all windows. | |
a1511caf | 1343 | |
2116e93c EZ |
1344 | Optional argument TRAIL restricts the windows that can be enlarged. |
1345 | If its value is `before', only windows to the left of or above WINDOW | |
1346 | can be enlarged. If it is `after', only windows to the right of or | |
1347 | below WINDOW can be enlarged. | |
a1511caf MR |
1348 | |
1349 | Optional argument NOUP non-nil means don't go up in the window | |
2116e93c | 1350 | tree, but try to enlarge windows within WINDOW's combination only. |
a1511caf MR |
1351 | |
1352 | Optional argument NODOWN non-nil means don't check whether WINDOW | |
382c953b | 1353 | itself (and its child windows) can be shrunk; check only whether |
880e6158 MR |
1354 | at least one other window can be enlarged appropriately. |
1355 | ||
1356 | Optional argument PIXELWISE non-nil means return number of pixels | |
1357 | by which WINDOW can be shrunk." | |
447f16b8 | 1358 | (setq window (window-normalize-window window)) |
880e6158 MR |
1359 | (let ((size (window-size window horizontal pixelwise)) |
1360 | (minimum (window-min-size window horizontal ignore pixelwise))) | |
a1511caf MR |
1361 | (cond |
1362 | (nodown | |
1363 | ;; If NODOWN is t, try to recover the entire size of WINDOW. | |
880e6158 MR |
1364 | (window--min-delta-1 |
1365 | window size horizontal ignore trail noup pixelwise)) | |
1366 | ((<= size minimum) | |
a1511caf MR |
1367 | ;; If NODOWN is nil and WINDOW's size is already at its minimum, |
1368 | ;; there's nothing to recover. | |
1369 | 0) | |
1370 | (t | |
1371 | ;; Otherwise, try to recover whatever WINDOW is larger than its | |
1372 | ;; minimum size. | |
c56cad4a | 1373 | (window--min-delta-1 |
880e6158 | 1374 | window (- size minimum) horizontal ignore trail noup pixelwise))))) |
a1511caf | 1375 | |
880e6158 | 1376 | (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise) |
a1511caf MR |
1377 | "Internal function of `window-max-delta'." |
1378 | (if (not (window-parent window)) | |
1379 | ;; Can't go up. Return DELTA. | |
1380 | delta | |
1381 | (let* ((parent (window-parent window)) | |
1382 | (sub (window-child parent))) | |
1383 | (catch 'fixed | |
3d8daefe | 1384 | (if (window-combined-p sub horizontal) |
a1511caf | 1385 | ;; For an iso-combination calculate how much we can get from |
be7f5545 | 1386 | ;; other child windows. |
a1511caf MR |
1387 | (let ((skip (eq trail 'after))) |
1388 | (while sub | |
1389 | (cond | |
1390 | ((eq sub window) | |
1391 | (setq skip (eq trail 'before))) | |
1392 | (skip) | |
1393 | (t | |
1394 | (setq delta | |
1395 | (+ delta | |
880e6158 MR |
1396 | (- (window-size sub horizontal pixelwise) |
1397 | (window-min-size | |
1398 | sub horizontal ignore pixelwise)))))) | |
a1511caf MR |
1399 | (setq sub (window-right sub)))) |
1400 | ;; For an ortho-combination throw DELTA when at least one | |
be7f5545 | 1401 | ;; child window is fixed-size. |
a1511caf MR |
1402 | (while sub |
1403 | (when (and (not (eq sub window)) | |
842e3a93 | 1404 | (not (window--size-ignore-p sub ignore)) |
a1511caf MR |
1405 | (window-size-fixed-p sub horizontal)) |
1406 | (throw 'fixed delta)) | |
1407 | (setq sub (window-right sub)))) | |
1408 | (if noup | |
1409 | ;; When NOUP is nil, DELTA is all we can get. | |
1410 | delta | |
1411 | ;; Else try with parent of WINDOW, passing the DELTA we | |
1412 | ;; recovered so far. | |
880e6158 MR |
1413 | (window--max-delta-1 |
1414 | parent delta horizontal ignore trail nil pixelwise)))))) | |
a1511caf | 1415 | |
880e6158 | 1416 | (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise) |
2116e93c | 1417 | "Return maximum number of lines by which WINDOW can be enlarged. |
85c2386b MR |
1418 | WINDOW must be a valid window and defaults to the selected one. |
1419 | The return value is zero if WINDOW cannot be enlarged. | |
a1511caf MR |
1420 | |
1421 | Optional argument HORIZONTAL non-nil means return maximum number | |
1422 | of columns by which WINDOW can be enlarged. | |
1423 | ||
2116e93c | 1424 | Optional argument IGNORE non-nil means ignore restrictions |
a1511caf | 1425 | imposed by fixed size windows, `window-min-height' or |
2116e93c EZ |
1426 | `window-min-width' settings. If IGNORE is a window, ignore |
1427 | restrictions for that window only. If IGNORE equals `safe', | |
a1511caf | 1428 | live windows may get as small as `window-safe-min-height' lines |
2116e93c EZ |
1429 | and `window-safe-min-width' columns. Any other non-nil value means |
1430 | ignore all of the above restrictions for all windows. | |
a1511caf | 1431 | |
2116e93c EZ |
1432 | Optional argument TRAIL restricts the windows that can be enlarged. |
1433 | If its value is `before', only windows to the left of or above WINDOW | |
1434 | can be enlarged. If it is `after', only windows to the right of or | |
1435 | below WINDOW can be enlarged. | |
a1511caf MR |
1436 | |
1437 | Optional argument NOUP non-nil means don't go up in the window | |
1438 | tree but try to obtain the entire space from windows within | |
1439 | WINDOW's combination. | |
1440 | ||
1441 | Optional argument NODOWN non-nil means do not check whether | |
382c953b | 1442 | WINDOW itself (and its child windows) can be enlarged; check |
880e6158 MR |
1443 | only whether other windows can be shrunk appropriately. |
1444 | ||
1445 | Optional argument PIXELWISE non-nil means return number of | |
1446 | pixels by which WINDOW can be enlarged." | |
447f16b8 | 1447 | (setq window (window-normalize-window window)) |
842e3a93 | 1448 | (if (and (not (window--size-ignore-p window ignore)) |
a1511caf MR |
1449 | (not nodown) (window-size-fixed-p window horizontal)) |
1450 | ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed | |
1451 | ;; size. | |
1452 | 0 | |
1453 | ;; WINDOW has no fixed size. | |
880e6158 | 1454 | (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise))) |
a1511caf MR |
1455 | |
1456 | ;; Make NOUP also inhibit the min-size check. | |
880e6158 | 1457 | (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise) |
a1511caf | 1458 | "Return DELTA if WINDOW can be resized vertically by DELTA lines. |
85c2386b | 1459 | WINDOW must be a valid window and defaults to the selected one. |
a1511caf MR |
1460 | Optional argument HORIZONTAL non-nil means return DELTA if WINDOW |
1461 | can be resized horizontally by DELTA columns. A return value of | |
1462 | zero means that WINDOW is not resizable. | |
1463 | ||
1464 | DELTA positive means WINDOW shall be enlarged by DELTA lines or | |
2cffd681 | 1465 | columns. If WINDOW cannot be enlarged by DELTA lines or columns, |
a1511caf MR |
1466 | return the maximum value in the range 0..DELTA by which WINDOW |
1467 | can be enlarged. | |
1468 | ||
1469 | DELTA negative means WINDOW shall be shrunk by -DELTA lines or | |
1470 | columns. If WINDOW cannot be shrunk by -DELTA lines or columns, | |
1471 | return the minimum value in the range DELTA..0 that can be used | |
1472 | for shrinking WINDOW. | |
1473 | ||
2116e93c | 1474 | Optional argument IGNORE non-nil means ignore restrictions |
a1511caf | 1475 | imposed by fixed size windows, `window-min-height' or |
2116e93c EZ |
1476 | `window-min-width' settings. If IGNORE is a window, ignore |
1477 | restrictions for that window only. If IGNORE equals `safe', | |
a1511caf | 1478 | live windows may get as small as `window-safe-min-height' lines |
2116e93c EZ |
1479 | and `window-safe-min-width' columns. Any other non-nil value |
1480 | means ignore all of the above restrictions for all windows. | |
a1511caf MR |
1481 | |
1482 | Optional argument TRAIL `before' means only windows to the left | |
1483 | of or below WINDOW can be shrunk. Optional argument TRAIL | |
1484 | `after' means only windows to the right of or above WINDOW can be | |
1485 | shrunk. | |
1486 | ||
1487 | Optional argument NOUP non-nil means don't go up in the window | |
2cffd681 MR |
1488 | tree but check only whether space can be obtained from (or given |
1489 | to) WINDOW's siblings. | |
a1511caf | 1490 | |
2cffd681 MR |
1491 | Optional argument NODOWN non-nil means don't go down in the |
1492 | window tree. This means do not check whether resizing would | |
880e6158 MR |
1493 | violate size restrictions of WINDOW or its child windows. |
1494 | ||
1495 | Optional argument PIXELWISE non-nil means interpret DELTA as | |
1496 | number of pixels." | |
447f16b8 | 1497 | (setq window (window-normalize-window window)) |
a1511caf MR |
1498 | (cond |
1499 | ((< delta 0) | |
880e6158 MR |
1500 | (max (- (window-min-delta |
1501 | window horizontal ignore trail noup nodown pixelwise)) | |
a1511caf MR |
1502 | delta)) |
1503 | ((> delta 0) | |
880e6158 MR |
1504 | (min (window-max-delta |
1505 | window horizontal ignore trail noup nodown pixelwise) | |
a1511caf MR |
1506 | delta)) |
1507 | (t 0))) | |
1508 | ||
880e6158 | 1509 | (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise) |
a1511caf | 1510 | "Return t if WINDOW can be resized vertically by DELTA lines. |
85c2386b | 1511 | WINDOW must be a valid window and defaults to the selected one. |
a1511caf | 1512 | For the meaning of the arguments of this function see the |
880e6158 MR |
1513 | doc-string of `window--resizable'. |
1514 | ||
1515 | Optional argument PIXELWISE non-nil means interpret DELTA as | |
1516 | pixels." | |
447f16b8 | 1517 | (setq window (window-normalize-window window)) |
a1511caf | 1518 | (if (> delta 0) |
880e6158 MR |
1519 | (>= (window--resizable |
1520 | window delta horizontal ignore trail noup nodown pixelwise) | |
a1511caf | 1521 | delta) |
880e6158 MR |
1522 | (<= (window--resizable |
1523 | window delta horizontal ignore trail noup nodown pixelwise) | |
a1511caf MR |
1524 | delta))) |
1525 | ||
880e6158 | 1526 | (defun window-resizable (window delta &optional horizontal ignore pixelwise) |
2cffd681 | 1527 | "Return DELTA if WINDOW can be resized vertically by DELTA lines. |
85c2386b | 1528 | WINDOW must be a valid window and defaults to the selected one. |
2cffd681 MR |
1529 | Optional argument HORIZONTAL non-nil means return DELTA if WINDOW |
1530 | can be resized horizontally by DELTA columns. A return value of | |
1531 | zero means that WINDOW is not resizable. | |
1532 | ||
1533 | DELTA positive means WINDOW shall be enlarged by DELTA lines or | |
1534 | columns. If WINDOW cannot be enlarged by DELTA lines or columns | |
1535 | return the maximum value in the range 0..DELTA by which WINDOW | |
1536 | can be enlarged. | |
1537 | ||
1538 | DELTA negative means WINDOW shall be shrunk by -DELTA lines or | |
1539 | columns. If WINDOW cannot be shrunk by -DELTA lines or columns, | |
1540 | return the minimum value in the range DELTA..0 that can be used | |
1541 | for shrinking WINDOW. | |
1542 | ||
2116e93c | 1543 | Optional argument IGNORE non-nil means ignore restrictions |
2cffd681 | 1544 | imposed by fixed size windows, `window-min-height' or |
2116e93c EZ |
1545 | `window-min-width' settings. If IGNORE is a window, ignore |
1546 | restrictions for that window only. If IGNORE equals `safe', | |
2cffd681 | 1547 | live windows may get as small as `window-safe-min-height' lines |
2116e93c | 1548 | and `window-safe-min-width' columns. Any other non-nil value |
880e6158 MR |
1549 | means ignore all of the above restrictions for all windows. |
1550 | ||
1551 | Optional argument PIXELWISE non-nil means interpret DELTA as | |
1552 | pixels." | |
2cffd681 | 1553 | (setq window (window-normalize-window window)) |
880e6158 | 1554 | (window--resizable window delta horizontal ignore nil nil nil pixelwise)) |
2cffd681 | 1555 | |
880e6158 MR |
1556 | (defun window-resizable-p (window delta &optional horizontal ignore pixelwise) |
1557 | "Return t if WINDOW can be resized vertically by DELTA lines. | |
85c2386b | 1558 | WINDOW must be a valid window and defaults to the selected one. |
880e6158 MR |
1559 | For the meaning of the arguments of this function see the |
1560 | doc-string of `window-resizable'." | |
1561 | (setq window (window-normalize-window window)) | |
1562 | (if (> delta 0) | |
1563 | (>= (window--resizable | |
1564 | window delta horizontal ignore nil nil nil pixelwise) | |
1565 | delta) | |
1566 | (<= (window--resizable | |
1567 | window delta horizontal ignore nil nil nil pixelwise) | |
1568 | delta))) | |
105216ed | 1569 | |
880e6158 | 1570 | ;; Aliases of functions defined in window.c. |
f3d1777e | 1571 | (defalias 'window-height 'window-total-height) |
880e6158 MR |
1572 | (defalias 'window-width 'window-body-width) |
1573 | ||
1574 | ;; Eventually the following two should work pixelwise. | |
f3d1777e | 1575 | |
ccafbf06 | 1576 | ;; See discussion in bug#4543. |
4b0d61e3 | 1577 | (defun window-full-height-p (&optional window) |
2116e93c | 1578 | "Return t if WINDOW is as high as its containing frame. |
a1511caf MR |
1579 | More precisely, return t if and only if the total height of |
1580 | WINDOW equals the total height of the root window of WINDOW's | |
85c2386b MR |
1581 | frame. WINDOW must be a valid window and defaults to the |
1582 | selected one." | |
447f16b8 | 1583 | (setq window (window-normalize-window window)) |
880e6158 MR |
1584 | (= (window-pixel-height window) |
1585 | (window-pixel-height (frame-root-window window)))) | |
a1511caf | 1586 | |
4b0d61e3 | 1587 | (defun window-full-width-p (&optional window) |
2116e93c | 1588 | "Return t if WINDOW is as wide as its containing frame. |
a1511caf MR |
1589 | More precisely, return t if and only if the total width of WINDOW |
1590 | equals the total width of the root window of WINDOW's frame. | |
85c2386b | 1591 | WINDOW must be a valid window and defaults to the selected one." |
447f16b8 | 1592 | (setq window (window-normalize-window window)) |
880e6158 MR |
1593 | (= (window-pixel-width window) |
1594 | (window-pixel-width (frame-root-window window)))) | |
a1511caf | 1595 | |
105216ed CY |
1596 | (defun window-body-size (&optional window horizontal) |
1597 | "Return the height or width of WINDOW's text area. | |
85c2386b | 1598 | WINDOW must be a live window and defaults to the selected one. |
105216ed CY |
1599 | |
1600 | If HORIZONTAL is omitted or nil, return the height of the text | |
1601 | area, like `window-body-height'. Otherwise, return the width of | |
1602 | the text area, like `window-body-width'." | |
1603 | (if horizontal | |
1604 | (window-body-width window) | |
1605 | (window-body-height window))) | |
02c6f098 | 1606 | |
3c448ab6 MR |
1607 | (defun window-current-scroll-bars (&optional window) |
1608 | "Return the current scroll bar settings for WINDOW. | |
387522b2 | 1609 | WINDOW must be a live window and defaults to the selected one. |
3c448ab6 MR |
1610 | |
1611 | The return value is a cons cell (VERTICAL . HORIZONTAL) where | |
1612 | VERTICAL specifies the current location of the vertical scroll | |
1613 | bars (`left', `right', or nil), and HORIZONTAL specifies the | |
1614 | current location of the horizontal scroll bars (`top', `bottom', | |
1615 | or nil). | |
1616 | ||
1617 | Unlike `window-scroll-bars', this function reports the scroll bar | |
1618 | type actually used, once frame defaults and `scroll-bar-mode' are | |
1619 | taken into account." | |
447f16b8 | 1620 | (setq window (window-normalize-window window t)) |
3c448ab6 MR |
1621 | (let ((vert (nth 2 (window-scroll-bars window))) |
1622 | (hor nil)) | |
1623 | (when (or (eq vert t) (eq hor t)) | |
387522b2 | 1624 | (let ((fcsb (frame-current-scroll-bars (window-frame window)))) |
3c448ab6 MR |
1625 | (if (eq vert t) |
1626 | (setq vert (car fcsb))) | |
1627 | (if (eq hor t) | |
1628 | (setq hor (cdr fcsb))))) | |
1629 | (cons vert hor))) | |
1630 | ||
c7635a97 CY |
1631 | (defun walk-windows (fun &optional minibuf all-frames) |
1632 | "Cycle through all live windows, calling FUN for each one. | |
1633 | FUN must specify a function with a window as its sole argument. | |
3c448ab6 | 1634 | The optional arguments MINIBUF and ALL-FRAMES specify the set of |
387522b2 | 1635 | windows to include in the walk. |
3c448ab6 MR |
1636 | |
1637 | MINIBUF t means include the minibuffer window even if the | |
1638 | minibuffer is not active. MINIBUF nil or omitted means include | |
1639 | the minibuffer window only if the minibuffer is active. Any | |
1640 | other value means do not include the minibuffer window even if | |
1641 | the minibuffer is active. | |
1642 | ||
387522b2 MR |
1643 | ALL-FRAMES nil or omitted means consider all windows on the |
1644 | selected frame, plus the minibuffer window if specified by the | |
1645 | MINIBUF argument. If the minibuffer counts, consider all windows | |
1646 | on all frames that share that minibuffer too. The following | |
1647 | non-nil values of ALL-FRAMES have special meanings: | |
1648 | ||
1649 | - t means consider all windows on all existing frames. | |
1650 | ||
1651 | - `visible' means consider all windows on all visible frames on | |
1652 | the current terminal. | |
1653 | ||
1654 | - 0 (the number zero) means consider all windows on all visible | |
1655 | and iconified frames on the current terminal. | |
1656 | ||
1657 | - A frame means consider all windows on that frame only. | |
1658 | ||
1659 | Anything else means consider all windows on the selected frame | |
1660 | and no others. | |
3c448ab6 MR |
1661 | |
1662 | This function changes neither the order of recently selected | |
1663 | windows nor the buffer list." | |
1664 | ;; If we start from the minibuffer window, don't fail to come | |
1665 | ;; back to it. | |
290d5b58 | 1666 | (when (window-minibuffer-p) |
3c448ab6 MR |
1667 | (setq minibuf t)) |
1668 | ;; Make sure to not mess up the order of recently selected | |
1669 | ;; windows. Use `save-selected-window' and `select-window' | |
1670 | ;; with second argument non-nil for this purpose. | |
1671 | (save-selected-window | |
1672 | (when (framep all-frames) | |
1673 | (select-window (frame-first-window all-frames) 'norecord)) | |
387522b2 | 1674 | (dolist (walk-windows-window (window-list-1 nil minibuf all-frames)) |
c7635a97 | 1675 | (funcall fun walk-windows-window)))) |
387522b2 | 1676 | |
e07b9a6d MR |
1677 | (defun window-at-side-p (&optional window side) |
1678 | "Return t if WINDOW is at SIDE of its containing frame. | |
85c2386b MR |
1679 | WINDOW must be a valid window and defaults to the selected one. |
1680 | SIDE can be any of the symbols `left', `top', `right' or | |
1681 | `bottom'. The default value nil is handled like `bottom'." | |
447f16b8 | 1682 | (setq window (window-normalize-window window)) |
e07b9a6d MR |
1683 | (let ((edge |
1684 | (cond | |
1685 | ((eq side 'left) 0) | |
1686 | ((eq side 'top) 1) | |
1687 | ((eq side 'right) 2) | |
1688 | ((memq side '(bottom nil)) 3)))) | |
880e6158 MR |
1689 | (= (nth edge (window-pixel-edges window)) |
1690 | (nth edge (window-pixel-edges (frame-root-window window)))))) | |
e07b9a6d | 1691 | |
54f9154c | 1692 | (defun window-at-side-list (&optional frame side) |
e07b9a6d MR |
1693 | "Return list of all windows on SIDE of FRAME. |
1694 | FRAME must be a live frame and defaults to the selected frame. | |
1695 | SIDE can be any of the symbols `left', `top', `right' or | |
1696 | `bottom'. The default value nil is handled like `bottom'." | |
1697 | (setq frame (window-normalize-frame frame)) | |
1698 | (let (windows) | |
1699 | (walk-window-tree | |
1700 | (lambda (window) | |
1701 | (when (window-at-side-p window side) | |
1702 | (setq windows (cons window windows)))) | |
ea95074e | 1703 | frame nil 'nomini) |
e07b9a6d MR |
1704 | (nreverse windows))) |
1705 | ||
5e92ca23 | 1706 | (defun window--in-direction-2 (window posn &optional horizontal) |
387522b2 MR |
1707 | "Support function for `window-in-direction'." |
1708 | (if horizontal | |
880e6158 | 1709 | (let ((top (window-pixel-top window))) |
387522b2 MR |
1710 | (if (> top posn) |
1711 | (- top posn) | |
880e6158 MR |
1712 | (- posn top (window-pixel-height window)))) |
1713 | (let ((left (window-pixel-left window))) | |
387522b2 MR |
1714 | (if (> left posn) |
1715 | (- left posn) | |
880e6158 | 1716 | (- posn left (window-pixel-width window)))))) |
387522b2 | 1717 | |
ea95074e MR |
1718 | ;; Predecessors to the below have been devised by Julian Assange in |
1719 | ;; change-windows-intuitively.el and Hovav Shacham in windmove.el. | |
1720 | ;; Neither of these allow to selectively ignore specific windows | |
1721 | ;; (windows whose `no-other-window' parameter is non-nil) as targets of | |
1722 | ;; the movement. | |
71e6691e | 1723 | (defun window-in-direction (direction &optional window ignore sign wrap mini) |
387522b2 | 1724 | "Return window in DIRECTION as seen from WINDOW. |
ea95074e MR |
1725 | More precisely, return the nearest window in direction DIRECTION |
1726 | as seen from the position of `window-point' in window WINDOW. | |
387522b2 MR |
1727 | DIRECTION must be one of `above', `below', `left' or `right'. |
1728 | WINDOW must be a live window and defaults to the selected one. | |
ea95074e MR |
1729 | |
1730 | Do not return a window whose `no-other-window' parameter is | |
1731 | non-nil. If the nearest window's `no-other-window' parameter is | |
1732 | non-nil, try to find another window in the indicated direction. | |
1733 | If, however, the optional argument IGNORE is non-nil, return that | |
1734 | window even if its `no-other-window' parameter is non-nil. | |
1735 | ||
71e6691e MR |
1736 | Optional argument SIGN a negative number means to use the right |
1737 | or bottom edge of WINDOW as reference position instead of | |
1738 | `window-point'. SIGN a positive number means to use the left or | |
1739 | top edge of WINDOW as reference position. | |
1740 | ||
1741 | Optional argument WRAP non-nil means to wrap DIRECTION around | |
1742 | frame borders. This means to return for a WINDOW a the top of | |
1743 | the frame and DIRECTION `above' to return the minibuffer window | |
1744 | if the frame has one, and a window at the bottom of the frame | |
1745 | otherwise. | |
1746 | ||
1747 | Optional argument MINI nil means to return the minibuffer window | |
1748 | if and only if it is currently active. MINI non-nil means to | |
1749 | return the minibuffer window even when it's not active. However, | |
1750 | if WRAP non-nil, always act as if MINI were nil. | |
1751 | ||
ea95074e | 1752 | Return nil if no suitable window can be found." |
447f16b8 | 1753 | (setq window (window-normalize-window window t)) |
387522b2 MR |
1754 | (unless (memq direction '(above below left right)) |
1755 | (error "Wrong direction %s" direction)) | |
1756 | (let* ((frame (window-frame window)) | |
1757 | (hor (memq direction '(left right))) | |
1758 | (first (if hor | |
880e6158 MR |
1759 | (window-pixel-left window) |
1760 | (window-pixel-top window))) | |
1761 | (last (+ first (window-size window hor t))) | |
387522b2 MR |
1762 | ;; The column / row value of `posn-at-point' can be nil for the |
1763 | ;; mini-window, guard against that. | |
71e6691e MR |
1764 | (posn |
1765 | (cond | |
1766 | ((and (numberp sign) (< sign 0)) | |
1767 | (if hor | |
1768 | (1- (+ (window-pixel-top window) (window-pixel-height window))) | |
1769 | (1- (+ (window-pixel-left window) (window-pixel-width window))))) | |
1770 | ((and (numberp sign) (> sign 0)) | |
1771 | (if hor | |
1772 | (window-pixel-top window) | |
1773 | (window-pixel-left window))) | |
1774 | ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window)))) | |
1775 | (if hor | |
1776 | (+ (or (cdr posn-cons) 1) (window-pixel-top window)) | |
1777 | (+ (or (car posn-cons) 1) (window-pixel-left window))))))) | |
387522b2 MR |
1778 | (best-edge |
1779 | (cond | |
880e6158 MR |
1780 | ((eq direction 'below) (frame-pixel-height frame)) |
1781 | ((eq direction 'right) (frame-pixel-width frame)) | |
387522b2 MR |
1782 | (t -1))) |
1783 | (best-edge-2 best-edge) | |
880e6158 | 1784 | (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame))) |
387522b2 MR |
1785 | best best-2 best-diff-2-new) |
1786 | (walk-window-tree | |
1787 | (lambda (w) | |
880e6158 MR |
1788 | (let* ((w-top (window-pixel-top w)) |
1789 | (w-left (window-pixel-left w))) | |
387522b2 MR |
1790 | (cond |
1791 | ((or (eq window w) | |
1792 | ;; Ignore ourselves. | |
1793 | (and (window-parameter w 'no-other-window) | |
1794 | ;; Ignore W unless IGNORE is non-nil. | |
1795 | (not ignore)))) | |
1796 | (hor | |
1797 | (cond | |
1798 | ((and (<= w-top posn) | |
880e6158 | 1799 | (< posn (+ w-top (window-pixel-height w)))) |
387522b2 MR |
1800 | ;; W is to the left or right of WINDOW and covers POSN. |
1801 | (when (or (and (eq direction 'left) | |
71e6691e MR |
1802 | (or (and (<= w-left first) (> w-left best-edge)) |
1803 | (and wrap | |
1804 | (window-at-side-p window 'left) | |
1805 | (window-at-side-p w 'right)))) | |
387522b2 | 1806 | (and (eq direction 'right) |
71e6691e MR |
1807 | (or (and (>= w-left last) (< w-left best-edge)) |
1808 | (and wrap | |
1809 | (window-at-side-p window 'right) | |
1810 | (window-at-side-p w 'left))))) | |
387522b2 MR |
1811 | (setq best-edge w-left) |
1812 | (setq best w))) | |
1813 | ((and (or (and (eq direction 'left) | |
880e6158 | 1814 | (<= (+ w-left (window-pixel-width w)) first)) |
387522b2 MR |
1815 | (and (eq direction 'right) (<= last w-left))) |
1816 | ;; W is to the left or right of WINDOW but does not | |
1817 | ;; cover POSN. | |
1818 | (setq best-diff-2-new | |
5e92ca23 | 1819 | (window--in-direction-2 w posn hor)) |
387522b2 MR |
1820 | (or (< best-diff-2-new best-diff-2) |
1821 | (and (= best-diff-2-new best-diff-2) | |
1822 | (if (eq direction 'left) | |
1823 | (> w-left best-edge-2) | |
1824 | (< w-left best-edge-2))))) | |
1825 | (setq best-edge-2 w-left) | |
1826 | (setq best-diff-2 best-diff-2-new) | |
1827 | (setq best-2 w)))) | |
71e6691e MR |
1828 | ((and (<= w-left posn) |
1829 | (< posn (+ w-left (window-pixel-width w)))) | |
1830 | ;; W is above or below WINDOW and covers POSN. | |
1831 | (when (or (and (eq direction 'above) | |
1832 | (or (and (<= w-top first) (> w-top best-edge)) | |
1833 | (and wrap | |
1834 | (window-at-side-p window 'top) | |
1835 | (if (active-minibuffer-window) | |
1836 | (minibuffer-window-active-p w) | |
1837 | (window-at-side-p w 'bottom))))) | |
1838 | (and (eq direction 'below) | |
1839 | (or (and (>= w-top first) (< w-top best-edge)) | |
1840 | (and wrap | |
1841 | (if (active-minibuffer-window) | |
1842 | (minibuffer-window-active-p window) | |
1843 | (window-at-side-p window 'bottom)) | |
1844 | (window-at-side-p w 'top))))) | |
1845 | (setq best-edge w-top) | |
1846 | (setq best w))) | |
1847 | ((and (or (and (eq direction 'above) | |
1848 | (<= (+ w-top (window-pixel-height w)) first)) | |
1849 | (and (eq direction 'below) (<= last w-top))) | |
1850 | ;; W is above or below WINDOW but does not cover POSN. | |
1851 | (setq best-diff-2-new | |
1852 | (window--in-direction-2 w posn hor)) | |
1853 | (or (< best-diff-2-new best-diff-2) | |
1854 | (and (= best-diff-2-new best-diff-2) | |
1855 | (if (eq direction 'above) | |
1856 | (> w-top best-edge-2) | |
1857 | (< w-top best-edge-2))))) | |
1858 | (setq best-edge-2 w-top) | |
1859 | (setq best-diff-2 best-diff-2-new) | |
1860 | (setq best-2 w))))) | |
1861 | frame nil (and mini t)) | |
387522b2 | 1862 | (or best best-2))) |
3c448ab6 | 1863 | |
4c43d97b | 1864 | (defun get-window-with-predicate (predicate &optional minibuf all-frames default) |
387522b2 MR |
1865 | "Return a live window satisfying PREDICATE. |
1866 | More precisely, cycle through all windows calling the function | |
1867 | PREDICATE on each one of them with the window as its sole | |
1868 | argument. Return the first window for which PREDICATE returns | |
4c43d97b | 1869 | non-nil. Windows are scanned starting with the window following |
c80e3b4a | 1870 | the selected window. If no window satisfies PREDICATE, return |
4c43d97b MR |
1871 | DEFAULT. |
1872 | ||
1873 | MINIBUF t means include the minibuffer window even if the | |
1874 | minibuffer is not active. MINIBUF nil or omitted means include | |
1875 | the minibuffer window only if the minibuffer is active. Any | |
1876 | other value means do not include the minibuffer window even if | |
1877 | the minibuffer is active. | |
387522b2 MR |
1878 | |
1879 | ALL-FRAMES nil or omitted means consider all windows on the selected | |
1880 | frame, plus the minibuffer window if specified by the MINIBUF | |
1881 | argument. If the minibuffer counts, consider all windows on all | |
1882 | frames that share that minibuffer too. The following non-nil | |
1883 | values of ALL-FRAMES have special meanings: | |
3c448ab6 | 1884 | |
387522b2 MR |
1885 | - t means consider all windows on all existing frames. |
1886 | ||
1887 | - `visible' means consider all windows on all visible frames on | |
1888 | the current terminal. | |
1889 | ||
1890 | - 0 (the number zero) means consider all windows on all visible | |
1891 | and iconified frames on the current terminal. | |
1892 | ||
1893 | - A frame means consider all windows on that frame only. | |
1894 | ||
1895 | Anything else means consider all windows on the selected frame | |
1896 | and no others." | |
3c448ab6 | 1897 | (catch 'found |
4c43d97b MR |
1898 | (dolist (window (window-list-1 |
1899 | (next-window nil minibuf all-frames) | |
1900 | minibuf all-frames)) | |
387522b2 MR |
1901 | (when (funcall predicate window) |
1902 | (throw 'found window))) | |
3c448ab6 MR |
1903 | default)) |
1904 | ||
1905 | (defalias 'some-window 'get-window-with-predicate) | |
1906 | ||
51a5f9d8 | 1907 | (defun get-lru-window (&optional all-frames dedicated not-selected) |
190b47e6 MR |
1908 | "Return the least recently used window on frames specified by ALL-FRAMES. |
1909 | Return a full-width window if possible. A minibuffer window is | |
1910 | never a candidate. A dedicated window is never a candidate | |
1911 | unless DEDICATED is non-nil, so if all windows are dedicated, the | |
1912 | value is nil. Avoid returning the selected window if possible. | |
51a5f9d8 MR |
1913 | Optional argument NOT-SELECTED non-nil means never return the |
1914 | selected window. | |
190b47e6 MR |
1915 | |
1916 | The following non-nil values of the optional argument ALL-FRAMES | |
1917 | have special meanings: | |
1918 | ||
1919 | - t means consider all windows on all existing frames. | |
1920 | ||
1921 | - `visible' means consider all windows on all visible frames on | |
1922 | the current terminal. | |
1923 | ||
1924 | - 0 (the number zero) means consider all windows on all visible | |
1925 | and iconified frames on the current terminal. | |
1926 | ||
1927 | - A frame means consider all windows on that frame only. | |
1928 | ||
1929 | Any other value of ALL-FRAMES means consider all windows on the | |
1930 | selected frame and no others." | |
1931 | (let (best-window best-time second-best-window second-best-time time) | |
02cfc6d6 | 1932 | (dolist (window (window-list-1 nil 'nomini all-frames)) |
51a5f9d8 MR |
1933 | (when (and (or dedicated (not (window-dedicated-p window))) |
1934 | (or (not not-selected) (not (eq window (selected-window))))) | |
190b47e6 MR |
1935 | (setq time (window-use-time window)) |
1936 | (if (or (eq window (selected-window)) | |
1937 | (not (window-full-width-p window))) | |
1938 | (when (or (not second-best-time) (< time second-best-time)) | |
1939 | (setq second-best-time time) | |
1940 | (setq second-best-window window)) | |
1941 | (when (or (not best-time) (< time best-time)) | |
1942 | (setq best-time time) | |
1943 | (setq best-window window))))) | |
1944 | (or best-window second-best-window))) | |
1945 | ||
51a5f9d8 | 1946 | (defun get-mru-window (&optional all-frames dedicated not-selected) |
387522b2 | 1947 | "Return the most recently used window on frames specified by ALL-FRAMES. |
51a5f9d8 MR |
1948 | A minibuffer window is never a candidate. A dedicated window is |
1949 | never a candidate unless DEDICATED is non-nil, so if all windows | |
1950 | are dedicated, the value is nil. Optional argument NOT-SELECTED | |
1951 | non-nil means never return the selected window. | |
387522b2 MR |
1952 | |
1953 | The following non-nil values of the optional argument ALL-FRAMES | |
1954 | have special meanings: | |
1955 | ||
1956 | - t means consider all windows on all existing frames. | |
1957 | ||
1958 | - `visible' means consider all windows on all visible frames on | |
1959 | the current terminal. | |
1960 | ||
1961 | - 0 (the number zero) means consider all windows on all visible | |
1962 | and iconified frames on the current terminal. | |
1963 | ||
1964 | - A frame means consider all windows on that frame only. | |
1965 | ||
1966 | Any other value of ALL-FRAMES means consider all windows on the | |
1967 | selected frame and no others." | |
1968 | (let (best-window best-time time) | |
02cfc6d6 | 1969 | (dolist (window (window-list-1 nil 'nomini all-frames)) |
387522b2 | 1970 | (setq time (window-use-time window)) |
51a5f9d8 MR |
1971 | (when (and (or dedicated (not (window-dedicated-p window))) |
1972 | (or (not not-selected) (not (eq window (selected-window)))) | |
1973 | (or (not best-time) (> time best-time))) | |
387522b2 MR |
1974 | (setq best-time time) |
1975 | (setq best-window window))) | |
1976 | best-window)) | |
1977 | ||
51a5f9d8 | 1978 | (defun get-largest-window (&optional all-frames dedicated not-selected) |
190b47e6 MR |
1979 | "Return the largest window on frames specified by ALL-FRAMES. |
1980 | A minibuffer window is never a candidate. A dedicated window is | |
1981 | never a candidate unless DEDICATED is non-nil, so if all windows | |
51a5f9d8 MR |
1982 | are dedicated, the value is nil. Optional argument NOT-SELECTED |
1983 | non-nil means never return the selected window. | |
190b47e6 MR |
1984 | |
1985 | The following non-nil values of the optional argument ALL-FRAMES | |
1986 | have special meanings: | |
1987 | ||
1988 | - t means consider all windows on all existing frames. | |
1989 | ||
1990 | - `visible' means consider all windows on all visible frames on | |
1991 | the current terminal. | |
1992 | ||
1993 | - 0 (the number zero) means consider all windows on all visible | |
1994 | and iconified frames on the current terminal. | |
1995 | ||
1996 | - A frame means consider all windows on that frame only. | |
1997 | ||
1998 | Any other value of ALL-FRAMES means consider all windows on the | |
1999 | selected frame and no others." | |
2000 | (let ((best-size 0) | |
2001 | best-window size) | |
02cfc6d6 | 2002 | (dolist (window (window-list-1 nil 'nomini all-frames)) |
51a5f9d8 MR |
2003 | (when (and (or dedicated (not (window-dedicated-p window))) |
2004 | (or (not not-selected) (not (eq window (selected-window))))) | |
880e6158 MR |
2005 | (setq size (* (window-pixel-height window) |
2006 | (window-pixel-width window))) | |
190b47e6 MR |
2007 | (when (> size best-size) |
2008 | (setq best-size size) | |
2009 | (setq best-window window)))) | |
2010 | best-window)) | |
2011 | ||
3c448ab6 MR |
2012 | (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames) |
2013 | "Return list of all windows displaying BUFFER-OR-NAME, or nil if none. | |
2014 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer | |
4c43d97b MR |
2015 | and defaults to the current buffer. Windows are scanned starting |
2016 | with the selected window. | |
190b47e6 MR |
2017 | |
2018 | MINIBUF t means include the minibuffer window even if the | |
2019 | minibuffer is not active. MINIBUF nil or omitted means include | |
2020 | the minibuffer window only if the minibuffer is active. Any | |
2021 | other value means do not include the minibuffer window even if | |
2022 | the minibuffer is active. | |
2023 | ||
2024 | ALL-FRAMES nil or omitted means consider all windows on the | |
2025 | selected frame, plus the minibuffer window if specified by the | |
2026 | MINIBUF argument. If the minibuffer counts, consider all windows | |
2027 | on all frames that share that minibuffer too. The following | |
2028 | non-nil values of ALL-FRAMES have special meanings: | |
2029 | ||
2030 | - t means consider all windows on all existing frames. | |
2031 | ||
2032 | - `visible' means consider all windows on all visible frames on | |
2033 | the current terminal. | |
2034 | ||
2035 | - 0 (the number zero) means consider all windows on all visible | |
2036 | and iconified frames on the current terminal. | |
2037 | ||
2038 | - A frame means consider all windows on that frame only. | |
2039 | ||
2040 | Anything else means consider all windows on the selected frame | |
2041 | and no others." | |
5386012d | 2042 | (let ((buffer (window-normalize-buffer buffer-or-name)) |
3c448ab6 | 2043 | windows) |
4c43d97b | 2044 | (dolist (window (window-list-1 (selected-window) minibuf all-frames)) |
190b47e6 MR |
2045 | (when (eq (window-buffer window) buffer) |
2046 | (setq windows (cons window windows)))) | |
2047 | (nreverse windows))) | |
3c448ab6 MR |
2048 | |
2049 | (defun minibuffer-window-active-p (window) | |
2050 | "Return t if WINDOW is the currently active minibuffer window." | |
2051 | (eq window (active-minibuffer-window))) | |
387522b2 | 2052 | |
3c448ab6 | 2053 | (defun count-windows (&optional minibuf) |
387522b2 | 2054 | "Return the number of live windows on the selected frame. |
3c448ab6 MR |
2055 | The optional argument MINIBUF specifies whether the minibuffer |
2056 | window shall be counted. See `walk-windows' for the precise | |
2057 | meaning of this argument." | |
387522b2 | 2058 | (length (window-list-1 nil minibuf))) |
9aab8e0d MR |
2059 | \f |
2060 | ;;; Resizing windows. | |
880e6158 MR |
2061 | (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe) |
2062 | "For WINDOW convert SIZE lines to pixels. | |
2063 | SIZE is supposed to specify a height of WINDOW in terms of text | |
2064 | lines. The return value is the number of pixels specifying that | |
2065 | height. | |
2066 | ||
2067 | WINDOW must be a valid window. Optional argument HORIZONTAL | |
2068 | non-nil means convert SIZE columns to pixels. | |
2069 | ||
2070 | Optional argument PIXELWISE non-nil means SIZE already specifies | |
2071 | pixels but may have to be adjusted to a multiple of the character | |
2072 | size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil | |
2073 | means round to the nearest multiple of the character size of | |
2074 | WINDOW's frame if the option `window-resize-pixelwise' is nil." | |
2075 | (setq window (window-normalize-window window)) | |
2076 | (let ((char-size (frame-char-size window horizontal))) | |
2077 | (if pixelwise | |
2078 | (if (and round-maybe (not window-resize-pixelwise)) | |
2079 | (* (round size char-size) char-size) | |
2080 | size) | |
2081 | (* size char-size)))) | |
2082 | ||
880e6158 MR |
2083 | (defun window--pixel-to-total-1 (window horizontal char-size) |
2084 | "Subroutine of `window--pixel-to-total'." | |
2085 | (let ((child (window-child window))) | |
2086 | (if (window-combination-p window horizontal) | |
2087 | ;; In an iso-combination distribute sizes proportionally. | |
2088 | (let ((remainder (window-new-total window)) | |
c44de18d | 2089 | size best-child rem best-rem) |
880e6158 MR |
2090 | ;; Initialize total sizes to each child's floor. |
2091 | (while child | |
c44de18d | 2092 | (setq size (max (/ (window-size child horizontal t) char-size) 1)) |
880e6158 MR |
2093 | (set-window-new-total child size) |
2094 | (setq remainder (- remainder size)) | |
2095 | (setq child (window-next-sibling child))) | |
2096 | ;; Distribute remainder. | |
2097 | (while (> remainder 0) | |
2098 | (setq child (window-last-child window)) | |
2099 | (setq best-child nil) | |
c44de18d | 2100 | (setq best-rem 0) |
880e6158 | 2101 | (while child |
c44de18d MR |
2102 | (when (and (<= (window-new-total child) |
2103 | (/ (window-size child horizontal t) char-size)) | |
2104 | (> (setq rem (% (window-size child horizontal t) | |
2105 | char-size)) | |
2106 | best-rem)) | |
2107 | (setq best-child child) | |
2108 | (setq best-rem rem)) | |
880e6158 MR |
2109 | (setq child (window-prev-sibling child))) |
2110 | ;; We MUST have a best-child here. | |
2111 | (set-window-new-total best-child 1 t) | |
2112 | (setq remainder (1- remainder))) | |
2113 | ;; Recurse. | |
2114 | (setq child (window-child window)) | |
2115 | (while child | |
2116 | (window--pixel-to-total-1 child horizontal char-size) | |
2117 | (setq child (window-next-sibling child)))) | |
2118 | ;; In an ortho-combination assign new sizes directly. | |
2119 | (let ((size (window-new-total window))) | |
2120 | (while child | |
2121 | (set-window-new-total child size) | |
2122 | (window--pixel-to-total-1 child horizontal char-size) | |
2123 | (setq child (window-next-sibling child))))))) | |
2124 | ||
2125 | (defun window--pixel-to-total (&optional frame horizontal) | |
2126 | "On FRAME assign new total window heights from pixel heights. | |
2127 | FRAME must be a live frame and defaults to the selected frame. | |
2128 | ||
2129 | Optional argument HORIZONTAL non-nil means assign new total | |
2130 | window widths from pixel widths." | |
2131 | (setq frame (window-normalize-frame frame)) | |
c44de18d MR |
2132 | (let* ((char-size (frame-char-size frame horizontal)) |
2133 | (root (frame-root-window)) | |
2134 | (root-size (window-size root horizontal t)) | |
2135 | ;; We have to care about the minibuffer window only if it | |
2136 | ;; appears together with the root window on this frame. | |
2137 | (mini (let ((mini (minibuffer-window frame))) | |
2138 | (and (eq (window-frame mini) frame) | |
2139 | (not (eq mini root)) mini))) | |
2140 | (mini-size (and mini (window-size mini horizontal t)))) | |
2141 | ;; We round the line/column sizes of windows here to the nearest | |
2142 | ;; integer. In some cases this can make windows appear _larger_ | |
2143 | ;; than the containing frame (line/column-wise) because the latter's | |
2144 | ;; sizes are not (yet) rounded. We might eventually fix that. | |
2145 | (if (and mini (not horizontal)) | |
2146 | (let (lines) | |
2147 | (set-window-new-total root (max (/ root-size char-size) 1)) | |
2148 | (set-window-new-total mini (max (/ mini-size char-size) 1)) | |
2149 | (setq lines (- (round (+ root-size mini-size) char-size) | |
2150 | (+ (window-new-total root) (window-new-total mini)))) | |
2151 | (while (> lines 0) | |
2152 | (if (>= (% root-size (window-new-total root)) | |
2153 | (% mini-size (window-new-total mini))) | |
2154 | (set-window-new-total root 1 t) | |
2155 | (set-window-new-total mini 1 t)) | |
2156 | (setq lines (1- lines)))) | |
2157 | (set-window-new-total root (round root-size char-size)) | |
2158 | (when mini | |
2159 | ;; This is taken in the horizontal case only. | |
2160 | (set-window-new-total mini (round mini-size char-size)))) | |
880e6158 | 2161 | (unless (window-buffer root) |
c44de18d MR |
2162 | (window--pixel-to-total-1 root horizontal char-size)) |
2163 | ;; Apply the new sizes. | |
2164 | (window-resize-apply-total frame horizontal))) | |
880e6158 | 2165 | |
5386012d | 2166 | (defun window--resize-reset (&optional frame horizontal) |
9aab8e0d MR |
2167 | "Reset resize values for all windows on FRAME. |
2168 | FRAME defaults to the selected frame. | |
2169 | ||
880e6158 | 2170 | This function stores the current value of `window-size' applied |
9aab8e0d MR |
2171 | with argument HORIZONTAL in the new total size of all windows on |
2172 | FRAME. It also resets the new normal size of each of these | |
2173 | windows." | |
5386012d MR |
2174 | (window--resize-reset-1 |
2175 | (frame-root-window (window-normalize-frame frame)) horizontal)) | |
9aab8e0d | 2176 | |
5386012d MR |
2177 | (defun window--resize-reset-1 (window horizontal) |
2178 | "Internal function of `window--resize-reset'." | |
9aab8e0d | 2179 | ;; Register old size in the new total size. |
880e6158 MR |
2180 | (set-window-new-pixel window (window-size window horizontal t)) |
2181 | (set-window-new-total window (window-size window horizontal)) | |
9aab8e0d MR |
2182 | ;; Reset new normal size. |
2183 | (set-window-new-normal window) | |
2184 | (when (window-child window) | |
5386012d | 2185 | (window--resize-reset-1 (window-child window) horizontal)) |
9aab8e0d | 2186 | (when (window-right window) |
5386012d | 2187 | (window--resize-reset-1 (window-right window) horizontal))) |
9aab8e0d | 2188 | |
562dd5e9 MR |
2189 | ;; The following routine is used to manually resize the minibuffer |
2190 | ;; window and is currently used, for example, by ispell.el. | |
5386012d | 2191 | (defun window--resize-mini-window (window delta) |
880e6158 MR |
2192 | "Resize minibuffer window WINDOW by DELTA pixels. |
2193 | If WINDOW cannot be resized by DELTA pixels make it as large (or | |
2116e93c | 2194 | as small) as possible, but don't signal an error." |
562dd5e9 MR |
2195 | (when (window-minibuffer-p window) |
2196 | (let* ((frame (window-frame window)) | |
2197 | (root (frame-root-window frame)) | |
880e6158 | 2198 | (height (window-pixel-height window)) |
562dd5e9 | 2199 | (min-delta |
880e6158 MR |
2200 | (- (window-pixel-height root) |
2201 | (window-min-size root nil nil t)))) | |
562dd5e9 MR |
2202 | ;; Sanitize DELTA. |
2203 | (cond | |
2204 | ((<= (+ height delta) 0) | |
880e6158 | 2205 | (setq delta (- (frame-char-height (window-frame window)) height))) |
562dd5e9 MR |
2206 | ((> delta min-delta) |
2207 | (setq delta min-delta))) | |
2208 | ||
880e6158 MR |
2209 | (unless (zerop delta) |
2210 | ;; Resize now. | |
2211 | (window--resize-reset frame) | |
2212 | ;; Ideally we should be able to resize just the last child of root | |
2213 | ;; here. See the comment in `resize-root-window-vertically' for | |
2214 | ;; why we do not do that. | |
2215 | (window--resize-this-window root (- delta) nil nil t) | |
2216 | (set-window-new-pixel window (+ height delta)) | |
2217 | ;; The following routine catches the case where we want to resize | |
2218 | ;; a minibuffer-only frame. | |
2219 | (when (resize-mini-window-internal window) | |
2220 | (window--pixel-to-total frame) | |
2221 | (run-window-configuration-change-hook frame)))))) | |
2222 | ||
2223 | (defun window--resize-apply-p (frame &optional horizontal) | |
2224 | "Return t when a window on FRAME shall be resized vertically. | |
2225 | Optional argument HORIZONTAL non-nil means return t when a window | |
2226 | shall be resized horizontally." | |
2227 | (catch 'apply | |
2228 | (walk-window-tree | |
2229 | (lambda (window) | |
2230 | (unless (= (window-new-pixel window) | |
2231 | (window-size window horizontal t)) | |
2232 | (throw 'apply t))) | |
2233 | frame t) | |
2234 | nil)) | |
2235 | ||
2236 | (defun window-resize (window delta &optional horizontal ignore pixelwise) | |
562dd5e9 MR |
2237 | "Resize WINDOW vertically by DELTA lines. |
2238 | WINDOW can be an arbitrary window and defaults to the selected | |
2239 | one. An attempt to resize the root window of a frame will raise | |
2240 | an error though. | |
2241 | ||
2242 | DELTA a positive number means WINDOW shall be enlarged by DELTA | |
2243 | lines. DELTA negative means WINDOW shall be shrunk by -DELTA | |
2244 | lines. | |
2245 | ||
2246 | Optional argument HORIZONTAL non-nil means resize WINDOW | |
2247 | horizontally by DELTA columns. In this case a positive DELTA | |
2248 | means enlarge WINDOW by DELTA columns. DELTA negative means | |
2249 | WINDOW shall be shrunk by -DELTA columns. | |
2250 | ||
2116e93c | 2251 | Optional argument IGNORE non-nil means ignore restrictions |
562dd5e9 | 2252 | imposed by fixed size windows, `window-min-height' or |
2116e93c EZ |
2253 | `window-min-width' settings. If IGNORE is a window, ignore |
2254 | restrictions for that window only. If IGNORE equals `safe', | |
562dd5e9 | 2255 | live windows may get as small as `window-safe-min-height' lines |
2116e93c EZ |
2256 | and `window-safe-min-width' columns. Any other non-nil value |
2257 | means ignore all of the above restrictions for all windows. | |
562dd5e9 | 2258 | |
880e6158 MR |
2259 | Optional argument PIXELWISE non-nil means resize WINDOW by DELTA |
2260 | pixels. | |
2261 | ||
562dd5e9 MR |
2262 | This function resizes other windows proportionally and never |
2263 | deletes any windows. If you want to move only the low (right) | |
2264 | edge of WINDOW consider using `adjust-window-trailing-edge' | |
2265 | instead." | |
447f16b8 | 2266 | (setq window (window-normalize-window window)) |
562dd5e9 | 2267 | (let* ((frame (window-frame window)) |
feeb6f53 | 2268 | (minibuffer-window (minibuffer-window frame)) |
562dd5e9 | 2269 | sibling) |
880e6158 MR |
2270 | (setq delta (window--size-to-pixel |
2271 | window delta horizontal pixelwise t)) | |
562dd5e9 MR |
2272 | (cond |
2273 | ((eq window (frame-root-window frame)) | |
2274 | (error "Cannot resize the root window of a frame")) | |
2275 | ((window-minibuffer-p window) | |
41cfe0cb MR |
2276 | (if horizontal |
2277 | (error "Cannot resize minibuffer window horizontally") | |
2278 | (window--resize-mini-window window delta))) | |
feeb6f53 MR |
2279 | ((and (not horizontal) |
2280 | (window-full-height-p window) | |
2281 | (eq (window-frame minibuffer-window) frame) | |
2282 | (or (not resize-mini-windows) | |
2283 | (eq minibuffer-window (active-minibuffer-window)))) | |
2284 | ;; If WINDOW is full height and either `resize-mini-windows' is | |
2285 | ;; nil or the minibuffer window is active, resize the minibuffer | |
2286 | ;; window. | |
2287 | (window--resize-mini-window minibuffer-window (- delta))) | |
880e6158 MR |
2288 | ((window--resizable-p |
2289 | window delta horizontal ignore nil nil nil t) | |
5386012d MR |
2290 | (window--resize-reset frame horizontal) |
2291 | (window--resize-this-window window delta horizontal ignore t) | |
a0c2d0ae | 2292 | (if (and (not window-combination-resize) |
3d8daefe | 2293 | (window-combined-p window horizontal) |
562dd5e9 | 2294 | (setq sibling (or (window-right window) (window-left window))) |
880e6158 MR |
2295 | (window-sizable-p |
2296 | sibling (- delta) horizontal ignore t)) | |
a0c2d0ae | 2297 | ;; If window-combination-resize is nil, WINDOW is part of an |
89d61221 | 2298 | ;; iso-combination, and WINDOW's neighboring right or left |
562dd5e9 MR |
2299 | ;; sibling can be resized as requested, resize that sibling. |
2300 | (let ((normal-delta | |
2301 | (/ (float delta) | |
880e6158 | 2302 | (window-size (window-parent window) horizontal t)))) |
5386012d | 2303 | (window--resize-this-window sibling (- delta) horizontal nil t) |
562dd5e9 MR |
2304 | (set-window-new-normal |
2305 | window (+ (window-normal-size window horizontal) | |
2306 | normal-delta)) | |
2307 | (set-window-new-normal | |
2308 | sibling (- (window-normal-size sibling horizontal) | |
2309 | normal-delta))) | |
2310 | ;; Otherwise, resize all other windows in the same combination. | |
5386012d | 2311 | (window--resize-siblings window delta horizontal ignore)) |
880e6158 MR |
2312 | (when (window--resize-apply-p frame horizontal) |
2313 | (window-resize-apply frame horizontal) | |
2314 | (window--pixel-to-total frame horizontal) | |
2315 | (run-window-configuration-change-hook frame))) | |
562dd5e9 MR |
2316 | (t |
2317 | (error "Cannot resize window %s" window))))) | |
2318 | ||
880e6158 | 2319 | (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise) |
27fcfe31 MR |
2320 | "Resize WINDOW vertically if it is resizable by DELTA lines. |
2321 | This function is like `window-resize' but does not signal an | |
2322 | error when WINDOW cannot be resized. For the meaning of the | |
880e6158 MR |
2323 | optional arguments see the documentation of `window-resize'. |
2324 | ||
2325 | Optional argument PIXELWISE non-nil means interpret DELTA as | |
2326 | pixels." | |
2327 | (when (window--resizable-p | |
2328 | window delta horizontal ignore nil nil nil pixelwise) | |
2329 | (window-resize window delta horizontal ignore pixelwise))) | |
27fcfe31 | 2330 | |
4b0d61e3 | 2331 | (defun window--resize-child-windows-skip-p (window) |
9aab8e0d MR |
2332 | "Return non-nil if WINDOW shall be skipped by resizing routines." |
2333 | (memq (window-new-normal window) '(ignore stuck skip))) | |
2334 | ||
be7f5545 MR |
2335 | (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta) |
2336 | "Recursively set new normal height of child windows of window PARENT. | |
9aab8e0d | 2337 | HORIZONTAL non-nil means set the new normal width of these |
be7f5545 | 2338 | windows. WINDOW specifies a child window of PARENT that has been |
382c953b | 2339 | resized by THIS-DELTA lines (columns). |
9aab8e0d | 2340 | |
2116e93c EZ |
2341 | Optional argument TRAIL either `before' or `after' means set values |
2342 | only for windows before or after WINDOW. Optional argument | |
2343 | OTHER-DELTA, a number, specifies that this many lines (columns) | |
382c953b | 2344 | have been obtained from (or returned to) an ancestor window of |
9aab8e0d MR |
2345 | PARENT in order to resize WINDOW." |
2346 | (let* ((delta-normal | |
880e6158 MR |
2347 | (if (and (= (- this-delta) |
2348 | (window-size window horizontal t)) | |
9aab8e0d MR |
2349 | (zerop other-delta)) |
2350 | ;; When WINDOW gets deleted and we can return its entire | |
2351 | ;; space to its siblings, use WINDOW's normal size as the | |
2352 | ;; normal delta. | |
2353 | (- (window-normal-size window horizontal)) | |
2354 | ;; In any other case calculate the normal delta from the | |
2355 | ;; relation of THIS-DELTA to the total size of PARENT. | |
880e6158 MR |
2356 | (/ (float this-delta) |
2357 | (window-size parent horizontal t)))) | |
9aab8e0d MR |
2358 | (sub (window-child parent)) |
2359 | (parent-normal 0.0) | |
2360 | (skip (eq trail 'after))) | |
2361 | ||
be7f5545 MR |
2362 | ;; Set parent-normal to the sum of the normal sizes of all child |
2363 | ;; windows of PARENT that shall be resized, excluding only WINDOW | |
9aab8e0d MR |
2364 | ;; and any windows specified by the optional TRAIL argument. |
2365 | (while sub | |
2366 | (cond | |
2367 | ((eq sub window) | |
2368 | (setq skip (eq trail 'before))) | |
2369 | (skip) | |
2370 | (t | |
2371 | (setq parent-normal | |
2372 | (+ parent-normal (window-normal-size sub horizontal))))) | |
2373 | (setq sub (window-right sub))) | |
2374 | ||
be7f5545 | 2375 | ;; Set the new normal size of all child windows of PARENT from what |
9aab8e0d MR |
2376 | ;; they should have contributed for recovering THIS-DELTA lines |
2377 | ;; (columns). | |
2378 | (setq sub (window-child parent)) | |
2379 | (setq skip (eq trail 'after)) | |
2380 | (while sub | |
2381 | (cond | |
2382 | ((eq sub window) | |
2383 | (setq skip (eq trail 'before))) | |
2384 | (skip) | |
2385 | (t | |
2386 | (let ((old-normal (window-normal-size sub horizontal))) | |
2387 | (set-window-new-normal | |
2388 | sub (min 1.0 ; Don't get larger than 1. | |
2389 | (max (- old-normal | |
2390 | (* (/ old-normal parent-normal) | |
2391 | delta-normal)) | |
2392 | ;; Don't drop below 0. | |
2393 | 0.0)))))) | |
2394 | (setq sub (window-right sub))) | |
2395 | ||
2396 | (when (numberp other-delta) | |
2397 | ;; Set the new normal size of windows from what they should have | |
2398 | ;; contributed for recovering OTHER-DELTA lines (columns). | |
880e6158 MR |
2399 | (setq delta-normal (/ (float (window-size parent horizontal t)) |
2400 | (+ (window-size parent horizontal t) | |
9aab8e0d MR |
2401 | other-delta))) |
2402 | (setq sub (window-child parent)) | |
2403 | (setq skip (eq trail 'after)) | |
2404 | (while sub | |
2405 | (cond | |
2406 | ((eq sub window) | |
2407 | (setq skip (eq trail 'before))) | |
2408 | (skip) | |
2409 | (t | |
2410 | (set-window-new-normal | |
2411 | sub (min 1.0 ; Don't get larger than 1. | |
2412 | (max (* (window-new-normal sub) delta-normal) | |
2413 | ;; Don't drop below 0. | |
2414 | 0.0))))) | |
2415 | (setq sub (window-right sub)))) | |
2416 | ||
2417 | ;; Set the new normal size of WINDOW to what is left by the sum of | |
2418 | ;; the normal sizes of its siblings. | |
2419 | (set-window-new-normal | |
2420 | window | |
2421 | (let ((sum 0)) | |
2422 | (setq sub (window-child parent)) | |
2423 | (while sub | |
2424 | (cond | |
2425 | ((eq sub window)) | |
2426 | ((not (numberp (window-new-normal sub))) | |
2427 | (setq sum (+ sum (window-normal-size sub horizontal)))) | |
2428 | (t | |
2429 | (setq sum (+ sum (window-new-normal sub))))) | |
2430 | (setq sub (window-right sub))) | |
2431 | ;; Don't get larger than 1 or smaller than 0. | |
2432 | (min 1.0 (max (- 1.0 sum) 0.0)))))) | |
2433 | ||
880e6158 MR |
2434 | (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size) |
2435 | "Resize child windows of window PARENT vertically by DELTA pixels. | |
9aab8e0d MR |
2436 | PARENT must be a vertically combined internal window. |
2437 | ||
880e6158 MR |
2438 | Optional argument HORIZONTAL non-nil means resize child windows |
2439 | of PARENT horizontally by DELTA pixels. In this case PARENT must | |
9aab8e0d MR |
2440 | be a horizontally combined internal window. |
2441 | ||
2442 | WINDOW, if specified, must denote a child window of PARENT that | |
880e6158 | 2443 | is resized by DELTA pixels. |
9aab8e0d | 2444 | |
2116e93c | 2445 | Optional argument IGNORE non-nil means ignore restrictions |
9aab8e0d | 2446 | imposed by fixed size windows, `window-min-height' or |
2116e93c | 2447 | `window-min-width' settings. If IGNORE equals `safe', live |
9aab8e0d | 2448 | windows may get as small as `window-safe-min-height' lines and |
2116e93c EZ |
2449 | `window-safe-min-width' columns. If IGNORE is a window, ignore |
2450 | restrictions for that window only. Any other non-nil value means | |
2451 | ignore all of the above restrictions for all windows. | |
9aab8e0d MR |
2452 | |
2453 | Optional arguments TRAIL and EDGE, when non-nil, restrict the set | |
2454 | of windows that shall be resized. If TRAIL equals `before', | |
2455 | resize only windows on the left or above EDGE. If TRAIL equals | |
2456 | `after', resize only windows on the right or below EDGE. Also, | |
2457 | preferably only resize windows adjacent to EDGE. | |
2458 | ||
880e6158 MR |
2459 | If the optional argument CHAR-SIZE is a positive integer, it specifies |
2460 | the number of pixels by which windows are incrementally resized. | |
2461 | If CHAR-SIZE is nil, this means to use the value of | |
2462 | `frame-char-height' or `frame-char-width' of WINDOW's frame. | |
2463 | ||
9aab8e0d MR |
2464 | Return the symbol `normalized' if new normal sizes have been |
2465 | already set by this routine." | |
2466 | (let* ((first (window-child parent)) | |
9c52dd5a | 2467 | (last (window-last-child parent)) |
880e6158 MR |
2468 | (parent-total (+ (window-size parent horizontal t) |
2469 | delta)) | |
2470 | (char-size (or char-size | |
2471 | (and window-resize-pixelwise 1) | |
2472 | (frame-char-size window horizontal))) | |
2473 | sub best-window best-value best-delta) | |
9aab8e0d MR |
2474 | |
2475 | (if (and edge (memq trail '(before after)) | |
2476 | (progn | |
2477 | (setq sub first) | |
2478 | (while (and (window-right sub) | |
2479 | (or (and (eq trail 'before) | |
be7f5545 | 2480 | (not (window--resize-child-windows-skip-p |
9aab8e0d MR |
2481 | (window-right sub)))) |
2482 | (and (eq trail 'after) | |
be7f5545 | 2483 | (window--resize-child-windows-skip-p sub)))) |
9aab8e0d MR |
2484 | (setq sub (window-right sub))) |
2485 | sub) | |
2486 | (if horizontal | |
2487 | (if (eq trail 'before) | |
880e6158 | 2488 | (= (+ (window-pixel-left sub) (window-pixel-width sub)) |
9aab8e0d | 2489 | edge) |
880e6158 | 2490 | (= (window-pixel-left sub) edge)) |
9aab8e0d | 2491 | (if (eq trail 'before) |
880e6158 | 2492 | (= (+ (window-pixel-top sub) (window-pixel-height sub)) |
9aab8e0d | 2493 | edge) |
880e6158 MR |
2494 | (= (window-pixel-top sub) edge))) |
2495 | (window-sizable-p sub delta horizontal ignore t)) | |
9aab8e0d MR |
2496 | ;; Resize only windows adjacent to EDGE. |
2497 | (progn | |
5386012d MR |
2498 | (window--resize-this-window |
2499 | sub delta horizontal ignore t trail edge) | |
9aab8e0d MR |
2500 | (if (and window (eq (window-parent sub) parent)) |
2501 | (progn | |
2502 | ;; Assign new normal sizes. | |
2503 | (set-window-new-normal | |
880e6158 | 2504 | sub (/ (float (window-new-pixel sub)) parent-total)) |
9aab8e0d MR |
2505 | (set-window-new-normal |
2506 | window (- (window-normal-size window horizontal) | |
2507 | (- (window-new-normal sub) | |
2508 | (window-normal-size sub horizontal))))) | |
be7f5545 | 2509 | (window--resize-child-windows-normal |
5386012d MR |
2510 | parent horizontal sub 0 trail delta)) |
2511 | ;; Return 'normalized to notify `window--resize-siblings' that | |
9aab8e0d MR |
2512 | ;; normal sizes have been already set. |
2513 | 'normalized) | |
2514 | ;; Resize all windows proportionally. | |
9c52dd5a | 2515 | (setq sub last) |
9aab8e0d MR |
2516 | (while sub |
2517 | (cond | |
be7f5545 MR |
2518 | ((or (window--resize-child-windows-skip-p sub) |
2519 | ;; Ignore windows to skip and fixed-size child windows - | |
2520 | ;; in the latter case make it a window to skip. | |
9aab8e0d MR |
2521 | (and (not ignore) |
2522 | (window-size-fixed-p sub horizontal) | |
2523 | (set-window-new-normal sub 'ignore)))) | |
2524 | ((< delta 0) | |
2525 | ;; When shrinking store the number of lines/cols we can get | |
2526 | ;; from this window here together with the total/normal size | |
2527 | ;; factor. | |
2528 | (set-window-new-normal | |
2529 | sub | |
2530 | (cons | |
2531 | ;; We used to call this with NODOWN t, "fixed" 2011-05-11. | |
880e6158 MR |
2532 | (window-min-delta sub horizontal ignore trail t nil t) |
2533 | (- (/ (float (window-size sub horizontal t)) | |
9aab8e0d MR |
2534 | parent-total) |
2535 | (window-normal-size sub horizontal))))) | |
2536 | ((> delta 0) | |
2537 | ;; When enlarging store the total/normal size factor only | |
2538 | (set-window-new-normal | |
2539 | sub | |
880e6158 | 2540 | (- (/ (float (window-size sub horizontal t)) |
9aab8e0d MR |
2541 | parent-total) |
2542 | (window-normal-size sub horizontal))))) | |
2543 | ||
9c52dd5a | 2544 | (setq sub (window-left sub))) |
9aab8e0d MR |
2545 | |
2546 | (cond | |
2547 | ((< delta 0) | |
2548 | ;; Shrink windows by delta. | |
2549 | (setq best-window t) | |
2550 | (while (and best-window (not (zerop delta))) | |
9c52dd5a | 2551 | (setq sub last) |
9aab8e0d MR |
2552 | (setq best-window nil) |
2553 | (setq best-value most-negative-fixnum) | |
2554 | (while sub | |
2555 | (when (and (consp (window-new-normal sub)) | |
2556 | (not (zerop (car (window-new-normal sub)))) | |
2557 | (> (cdr (window-new-normal sub)) best-value)) | |
2558 | (setq best-window sub) | |
2559 | (setq best-value (cdr (window-new-normal sub)))) | |
2560 | ||
9c52dd5a | 2561 | (setq sub (window-left sub))) |
9aab8e0d MR |
2562 | |
2563 | (when best-window | |
880e6158 MR |
2564 | (setq best-delta (min (car (window-new-normal best-window)) |
2565 | char-size (- delta))) | |
2566 | (setq delta (+ delta best-delta)) | |
2567 | (set-window-new-pixel best-window (- best-delta) t) | |
2568 | (set-window-new-normal | |
2569 | best-window | |
2570 | (if (= (car (window-new-normal best-window)) best-delta) | |
2571 | 'skip ; We can't shrink best-window any further. | |
2572 | (cons (1- (car (window-new-normal best-window))) | |
2573 | (- (/ (float (window-new-pixel best-window)) | |
2574 | parent-total) | |
2575 | (window-normal-size best-window horizontal)))))))) | |
9aab8e0d MR |
2576 | ((> delta 0) |
2577 | ;; Enlarge windows by delta. | |
2578 | (setq best-window t) | |
2579 | (while (and best-window (not (zerop delta))) | |
9c52dd5a | 2580 | (setq sub last) |
9aab8e0d MR |
2581 | (setq best-window nil) |
2582 | (setq best-value most-positive-fixnum) | |
2583 | (while sub | |
2584 | (when (and (numberp (window-new-normal sub)) | |
2585 | (< (window-new-normal sub) best-value)) | |
2586 | (setq best-window sub) | |
2587 | (setq best-value (window-new-normal sub))) | |
2588 | ||
9c52dd5a | 2589 | (setq sub (window-left sub))) |
9aab8e0d MR |
2590 | |
2591 | (when best-window | |
880e6158 MR |
2592 | (setq best-delta (min delta char-size)) |
2593 | (setq delta (- delta best-delta)) | |
2594 | (set-window-new-pixel best-window best-delta t) | |
2595 | (set-window-new-normal | |
2596 | best-window | |
2597 | (- (/ (float (window-new-pixel best-window)) | |
2598 | parent-total) | |
2599 | (window-normal-size best-window horizontal))))))) | |
9aab8e0d MR |
2600 | |
2601 | (when best-window | |
9c52dd5a | 2602 | (setq sub last) |
9aab8e0d MR |
2603 | (while sub |
2604 | (when (or (consp (window-new-normal sub)) | |
2605 | (numberp (window-new-normal sub))) | |
d615d6d2 | 2606 | ;; Reset new normal size fields so `window-resize-apply' |
9aab8e0d MR |
2607 | ;; won't use them to apply new sizes. |
2608 | (set-window-new-normal sub)) | |
2609 | ||
2610 | (unless (eq (window-new-normal sub) 'ignore) | |
be7f5545 | 2611 | ;; Resize this window's child windows (back-engineering |
9aab8e0d | 2612 | ;; delta from sub's old and new total sizes). |
880e6158 MR |
2613 | (let ((delta (- (window-new-pixel sub) |
2614 | (window-size sub horizontal t)))) | |
9aab8e0d MR |
2615 | (unless (and (zerop delta) (not trail)) |
2616 | ;; For the TRAIL non-nil case we have to resize SUB | |
2617 | ;; recursively even if it's size does not change. | |
5386012d | 2618 | (window--resize-this-window |
9aab8e0d | 2619 | sub delta horizontal ignore nil trail edge)))) |
9c52dd5a | 2620 | (setq sub (window-left sub))))))) |
9aab8e0d | 2621 | |
880e6158 MR |
2622 | (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size) |
2623 | "Resize other windows when WINDOW is resized vertically by DELTA pixels. | |
9aab8e0d | 2624 | Optional argument HORIZONTAL non-nil means resize other windows |
880e6158 | 2625 | when WINDOW is resized horizontally by DELTA pixels. WINDOW |
9aab8e0d MR |
2626 | itself is not resized by this function. |
2627 | ||
2116e93c | 2628 | Optional argument IGNORE non-nil means ignore restrictions |
9aab8e0d | 2629 | imposed by fixed size windows, `window-min-height' or |
2116e93c | 2630 | `window-min-width' settings. If IGNORE equals `safe', live |
9aab8e0d | 2631 | windows may get as small as `window-safe-min-height' lines and |
2116e93c EZ |
2632 | `window-safe-min-width' columns. If IGNORE is a window, ignore |
2633 | restrictions for that window only. Any other non-nil value means | |
2634 | ignore all of the above restrictions for all windows. | |
9aab8e0d MR |
2635 | |
2636 | Optional arguments TRAIL and EDGE, when non-nil, refine the set | |
2637 | of windows that shall be resized. If TRAIL equals `before', | |
2638 | resize only windows on the left or above EDGE. If TRAIL equals | |
2639 | `after', resize only windows on the right or below EDGE. Also, | |
2640 | preferably only resize windows adjacent to EDGE." | |
2641 | (when (window-parent window) | |
2642 | (let* ((parent (window-parent window)) | |
2643 | (sub (window-child parent))) | |
3d8daefe | 2644 | (if (window-combined-p sub horizontal) |
9aab8e0d MR |
2645 | ;; In an iso-combination try to extract DELTA from WINDOW's |
2646 | ;; siblings. | |
cb882333 | 2647 | (let ((skip (eq trail 'after)) |
9aab8e0d MR |
2648 | this-delta other-delta) |
2649 | ;; Decide which windows shall be left alone. | |
2650 | (while sub | |
2651 | (cond | |
2652 | ((eq sub window) | |
2653 | ;; Make sure WINDOW is left alone when | |
2654 | ;; resizing its siblings. | |
2655 | (set-window-new-normal sub 'ignore) | |
2656 | (setq skip (eq trail 'before))) | |
2657 | (skip | |
2658 | ;; Make sure this sibling is left alone when | |
2659 | ;; resizing its siblings. | |
2660 | (set-window-new-normal sub 'ignore)) | |
842e3a93 | 2661 | ((or (window--size-ignore-p sub ignore) |
9aab8e0d MR |
2662 | (not (window-size-fixed-p sub horizontal))) |
2663 | ;; Set this-delta to t to signal that we found a sibling | |
2664 | ;; of WINDOW whose size is not fixed. | |
2665 | (setq this-delta t))) | |
2666 | ||
2667 | (setq sub (window-right sub))) | |
2668 | ||
2669 | ;; Set this-delta to what we can get from WINDOW's siblings. | |
880e6158 | 2670 | (if (= (- delta) (window-size window horizontal t)) |
9aab8e0d | 2671 | ;; A deletion, presumably. We must handle this case |
2cffd681 | 2672 | ;; specially since `window--resizable' can't be used. |
9aab8e0d MR |
2673 | (if this-delta |
2674 | ;; There's at least one resizable sibling we can | |
2675 | ;; give WINDOW's size to. | |
2676 | (setq this-delta delta) | |
2677 | ;; No resizable sibling exists. | |
2678 | (setq this-delta 0)) | |
2679 | ;; Any other form of resizing. | |
2680 | (setq this-delta | |
880e6158 MR |
2681 | (window--resizable |
2682 | window delta horizontal ignore trail t nil t))) | |
9aab8e0d MR |
2683 | |
2684 | ;; Set other-delta to what we still have to get from | |
2685 | ;; ancestor windows of parent. | |
2686 | (setq other-delta (- delta this-delta)) | |
2687 | (unless (zerop other-delta) | |
2688 | ;; Unless we got everything from WINDOW's siblings, PARENT | |
2689 | ;; must be resized by other-delta lines or columns. | |
880e6158 | 2690 | (set-window-new-pixel parent other-delta 'add)) |
9aab8e0d MR |
2691 | |
2692 | (if (zerop this-delta) | |
2693 | ;; We haven't got anything from WINDOW's siblings but we | |
2694 | ;; must update the normal sizes to respect other-delta. | |
be7f5545 | 2695 | (window--resize-child-windows-normal |
9aab8e0d MR |
2696 | parent horizontal window this-delta trail other-delta) |
2697 | ;; We did get something from WINDOW's siblings which means | |
be7f5545 MR |
2698 | ;; we have to resize their child windows. |
2699 | (unless (eq (window--resize-child-windows | |
5386012d | 2700 | parent (- this-delta) horizontal |
880e6158 | 2701 | window ignore trail edge char-size) |
be7f5545 | 2702 | ;; If `window--resize-child-windows' returns |
5386012d MR |
2703 | ;; 'normalized, this means it has set the |
2704 | ;; normal sizes already. | |
9aab8e0d MR |
2705 | 'normalized) |
2706 | ;; Set the normal sizes. | |
be7f5545 | 2707 | (window--resize-child-windows-normal |
9aab8e0d MR |
2708 | parent horizontal window this-delta trail other-delta)) |
2709 | ;; Set DELTA to what we still have to get from ancestor | |
2710 | ;; windows. | |
2711 | (setq delta other-delta))) | |
2712 | ||
2713 | ;; In an ortho-combination all siblings of WINDOW must be | |
2714 | ;; resized by DELTA. | |
880e6158 | 2715 | (set-window-new-pixel parent delta 'add) |
9aab8e0d MR |
2716 | (while sub |
2717 | (unless (eq sub window) | |
880e6158 MR |
2718 | (window--resize-this-window |
2719 | sub delta horizontal ignore t)) | |
9aab8e0d MR |
2720 | (setq sub (window-right sub)))) |
2721 | ||
2722 | (unless (zerop delta) | |
2723 | ;; "Go up." | |
5386012d | 2724 | (window--resize-siblings |
880e6158 | 2725 | parent delta horizontal ignore trail edge char-size))))) |
9aab8e0d | 2726 | |
880e6158 MR |
2727 | (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size) |
2728 | "Resize WINDOW vertically by DELTA pixels. | |
9aab8e0d | 2729 | Optional argument HORIZONTAL non-nil means resize WINDOW |
880e6158 | 2730 | horizontally by DELTA pixels. |
9aab8e0d | 2731 | |
2116e93c | 2732 | Optional argument IGNORE non-nil means ignore restrictions |
9aab8e0d | 2733 | imposed by fixed size windows, `window-min-height' or |
2116e93c | 2734 | `window-min-width' settings. If IGNORE equals `safe', live |
9aab8e0d | 2735 | windows may get as small as `window-safe-min-height' lines and |
2116e93c EZ |
2736 | `window-safe-min-width' columns. If IGNORE is a window, ignore |
2737 | restrictions for that window only. Any other non-nil value | |
2738 | means ignore all of the above restrictions for all windows. | |
9aab8e0d MR |
2739 | |
2740 | Optional argument ADD non-nil means add DELTA to the new total | |
2741 | size of WINDOW. | |
2742 | ||
2743 | Optional arguments TRAIL and EDGE, when non-nil, refine the set | |
2744 | of windows that shall be resized. If TRAIL equals `before', | |
2745 | resize only windows on the left or above EDGE. If TRAIL equals | |
2746 | `after', resize only windows on the right or below EDGE. Also, | |
2747 | preferably only resize windows adjacent to EDGE. | |
2748 | ||
880e6158 MR |
2749 | If the optional argument CHAR-SIZE is a positive integer, it specifies |
2750 | the number of pixels by which windows are incrementally resized. | |
2751 | If CHAR-SIZE is nil, this means to use the value of | |
2752 | `frame-char-height' or `frame-char-width' of WINDOW's frame. | |
2753 | ||
be7f5545 | 2754 | This function recursively resizes WINDOW's child windows to fit the |
2cffd681 | 2755 | new size. Make sure that WINDOW is `window--resizable' before |
9aab8e0d MR |
2756 | calling this function. Note that this function does not resize |
2757 | siblings of WINDOW or WINDOW's parent window. You have to | |
d615d6d2 | 2758 | eventually call `window-resize-apply' in order to make resizing |
9aab8e0d MR |
2759 | actually take effect." |
2760 | (when add | |
2761 | ;; Add DELTA to the new total size of WINDOW. | |
880e6158 | 2762 | (set-window-new-pixel window delta t)) |
387522b2 | 2763 | |
9aab8e0d MR |
2764 | (let ((sub (window-child window))) |
2765 | (cond | |
2766 | ((not sub)) | |
3d8daefe | 2767 | ((window-combined-p sub horizontal) |
be7f5545 | 2768 | ;; In an iso-combination resize child windows according to their |
9aab8e0d | 2769 | ;; normal sizes. |
be7f5545 | 2770 | (window--resize-child-windows |
880e6158 | 2771 | window delta horizontal nil ignore trail edge char-size)) |
be7f5545 | 2772 | ;; In an ortho-combination resize each child window by DELTA. |
9aab8e0d MR |
2773 | (t |
2774 | (while sub | |
5386012d | 2775 | (window--resize-this-window |
880e6158 | 2776 | sub delta horizontal ignore t trail edge char-size) |
9aab8e0d MR |
2777 | (setq sub (window-right sub))))))) |
2778 | ||
880e6158 | 2779 | (defun window--resize-root-window (window delta horizontal ignore pixelwise) |
9aab8e0d MR |
2780 | "Resize root window WINDOW vertically by DELTA lines. |
2781 | HORIZONTAL non-nil means resize root window WINDOW horizontally | |
2782 | by DELTA columns. | |
2783 | ||
2784 | IGNORE non-nil means ignore any restrictions imposed by fixed | |
2785 | size windows, `window-min-height' or `window-min-width' settings. | |
2786 | ||
2787 | This function is only called by the frame resizing routines. It | |
2788 | resizes windows proportionally and never deletes any windows." | |
880e6158 MR |
2789 | (when (and (windowp window) (numberp delta)) |
2790 | (let ((pixel-delta | |
2791 | (if pixelwise | |
2792 | delta | |
2793 | (window--size-to-pixel window delta horizontal)))) | |
2794 | (when (window-sizable-p window pixel-delta horizontal ignore t) | |
2795 | (window--resize-reset (window-frame window) horizontal) | |
2796 | (window--resize-this-window | |
2797 | window pixel-delta horizontal ignore t))))) | |
9aab8e0d | 2798 | |
880e6158 | 2799 | (defun window--resize-root-window-vertically (window delta pixelwise) |
9aab8e0d MR |
2800 | "Resize root window WINDOW vertically by DELTA lines. |
2801 | If DELTA is less than zero and we can't shrink WINDOW by DELTA | |
2802 | lines, shrink it as much as possible. If DELTA is greater than | |
be7f5545 | 2803 | zero, this function can resize fixed-size windows in order to |
880e6158 MR |
2804 | recover the necessary lines. Return the number of lines that |
2805 | were recovered. | |
9aab8e0d | 2806 | |
f224e500 | 2807 | Third argument PIXELWISE non-nil means to interpret DELTA as |
880e6158 | 2808 | pixels and return the number of pixels that were recovered. |
9aab8e0d | 2809 | |
880e6158 MR |
2810 | This function is called by the minibuffer window resizing |
2811 | routines." | |
2812 | (let* ((frame (window-frame window)) | |
2813 | (pixel-delta | |
2814 | (cond | |
2815 | (pixelwise | |
2816 | delta) | |
2817 | ((numberp delta) | |
2818 | (* (frame-char-height frame) delta)) | |
2819 | (t 0))) | |
2820 | ignore) | |
9c52dd5a | 2821 | (cond |
880e6158 MR |
2822 | ((zerop pixel-delta)) |
2823 | ((< pixel-delta 0) | |
2824 | (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise)) | |
9c52dd5a MR |
2825 | (window--resize-reset frame) |
2826 | ;; When shrinking the root window, emulate an edge drag in order | |
2827 | ;; to not resize other windows if we can avoid it (Bug#12419). | |
2828 | (window--resize-this-window | |
880e6158 MR |
2829 | window pixel-delta nil ignore t 'before |
2830 | (+ (window-pixel-top window) (window-pixel-height window))) | |
9c52dd5a MR |
2831 | ;; Don't record new normal sizes to make sure that shrinking back |
2832 | ;; proportionally works as intended. | |
2833 | (walk-window-tree | |
2834 | (lambda (window) (set-window-new-normal window 'ignore)) frame t)) | |
880e6158 | 2835 | ((> pixel-delta 0) |
9c52dd5a | 2836 | (window--resize-reset frame) |
880e6158 | 2837 | (unless (window-sizable window pixel-delta nil nil pixelwise) |
9c52dd5a MR |
2838 | (setq ignore t)) |
2839 | ;; When growing the root window, resize proportionally. This | |
2840 | ;; should give windows back their original sizes (hopefully). | |
880e6158 MR |
2841 | (window--resize-this-window |
2842 | window pixel-delta nil ignore t))) | |
9c52dd5a | 2843 | ;; Return the possibly adjusted DELTA. |
880e6158 MR |
2844 | (if pixelwise |
2845 | pixel-delta | |
2846 | (/ pixel-delta (frame-char-height frame))))) | |
9aab8e0d | 2847 | |
880e6158 | 2848 | (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise) |
562dd5e9 MR |
2849 | "Move WINDOW's bottom edge by DELTA lines. |
2850 | Optional argument HORIZONTAL non-nil means move WINDOW's right | |
85c2386b MR |
2851 | edge by DELTA columns. WINDOW must be a valid window and |
2852 | defaults to the selected one. | |
562dd5e9 | 2853 | |
880e6158 MR |
2854 | Optional argument PIXELWISE non-nil means interpret DELTA as |
2855 | number of pixels. | |
2856 | ||
2116e93c | 2857 | If DELTA is greater than zero, move the edge downwards or to the |
562dd5e9 MR |
2858 | right. If DELTA is less than zero, move the edge upwards or to |
2859 | the left. If the edge can't be moved by DELTA lines or columns, | |
2860 | move it as far as possible in the desired direction." | |
447f16b8 | 2861 | (setq window (window-normalize-window window)) |
41cfe0cb MR |
2862 | (let* ((frame (window-frame window)) |
2863 | (minibuffer-window (minibuffer-window frame)) | |
2864 | (right window) | |
2865 | left this-delta min-delta max-delta) | |
880e6158 MR |
2866 | |
2867 | (unless pixelwise | |
2868 | (setq pixelwise t) | |
2869 | (setq delta (* delta (frame-char-size window horizontal)))) | |
2870 | ||
562dd5e9 | 2871 | ;; Find the edge we want to move. |
3d8daefe | 2872 | (while (and (or (not (window-combined-p right horizontal)) |
562dd5e9 MR |
2873 | (not (window-right right))) |
2874 | (setq right (window-parent right)))) | |
2875 | (cond | |
41cfe0cb MR |
2876 | ((and (not right) (not horizontal) |
2877 | ;; Resize the minibuffer window if it's on the same frame as | |
2878 | ;; and immediately below WINDOW and it's either active or | |
2879 | ;; `resize-mini-windows' is nil. | |
2880 | (eq (window-frame minibuffer-window) frame) | |
880e6158 MR |
2881 | (= (nth 1 (window-pixel-edges minibuffer-window)) |
2882 | (nth 3 (window-pixel-edges window))) | |
41cfe0cb MR |
2883 | (or (not resize-mini-windows) |
2884 | (eq minibuffer-window (active-minibuffer-window)))) | |
2885 | (window--resize-mini-window minibuffer-window (- delta))) | |
562dd5e9 MR |
2886 | ((or (not (setq left right)) (not (setq right (window-right right)))) |
2887 | (if horizontal | |
2888 | (error "No window on the right of this one") | |
2889 | (error "No window below this one"))) | |
2890 | (t | |
2891 | ;; Set LEFT to the first resizable window on the left. This step is | |
2892 | ;; needed to handle fixed-size windows. | |
2893 | (while (and left (window-size-fixed-p left horizontal)) | |
2894 | (setq left | |
2895 | (or (window-left left) | |
2896 | (progn | |
2897 | (while (and (setq left (window-parent left)) | |
3d8daefe | 2898 | (not (window-combined-p left horizontal)))) |
562dd5e9 MR |
2899 | (window-left left))))) |
2900 | (unless left | |
2901 | (if horizontal | |
2902 | (error "No resizable window on the left of this one") | |
2903 | (error "No resizable window above this one"))) | |
2904 | ||
2905 | ;; Set RIGHT to the first resizable window on the right. This step | |
2906 | ;; is needed to handle fixed-size windows. | |
2907 | (while (and right (window-size-fixed-p right horizontal)) | |
2908 | (setq right | |
2909 | (or (window-right right) | |
2910 | (progn | |
2911 | (while (and (setq right (window-parent right)) | |
3d8daefe | 2912 | (not (window-combined-p right horizontal)))) |
562dd5e9 MR |
2913 | (window-right right))))) |
2914 | (unless right | |
2915 | (if horizontal | |
2916 | (error "No resizable window on the right of this one") | |
2917 | (error "No resizable window below this one"))) | |
2918 | ||
2919 | ;; LEFT and RIGHT (which might be both internal windows) are now the | |
2920 | ;; two windows we want to resize. | |
2921 | (cond | |
2922 | ((> delta 0) | |
880e6158 MR |
2923 | (setq max-delta |
2924 | (window--max-delta-1 | |
2925 | left 0 horizontal nil 'after nil pixelwise)) | |
2926 | (setq min-delta | |
2927 | (window--min-delta-1 | |
2928 | right (- delta) horizontal nil 'before nil pixelwise)) | |
562dd5e9 MR |
2929 | (when (or (< max-delta delta) (> min-delta (- delta))) |
2930 | ;; We can't get the whole DELTA - move as far as possible. | |
2931 | (setq delta (min max-delta (- min-delta)))) | |
2932 | (unless (zerop delta) | |
2933 | ;; Start resizing. | |
5386012d | 2934 | (window--resize-reset frame horizontal) |
562dd5e9 | 2935 | ;; Try to enlarge LEFT first. |
880e6158 MR |
2936 | (setq this-delta (window--resizable |
2937 | left delta horizontal nil nil nil nil pixelwise)) | |
562dd5e9 | 2938 | (unless (zerop this-delta) |
5386012d | 2939 | (window--resize-this-window |
562dd5e9 MR |
2940 | left this-delta horizontal nil t 'before |
2941 | (if horizontal | |
880e6158 MR |
2942 | (+ (window-pixel-left left) (window-pixel-width left)) |
2943 | (+ (window-pixel-top left) (window-pixel-height left))))) | |
562dd5e9 | 2944 | ;; Shrink windows on right of LEFT. |
5386012d | 2945 | (window--resize-siblings |
562dd5e9 MR |
2946 | left delta horizontal nil 'after |
2947 | (if horizontal | |
880e6158 MR |
2948 | (window-pixel-left right) |
2949 | (window-pixel-top right))))) | |
562dd5e9 | 2950 | ((< delta 0) |
880e6158 MR |
2951 | (setq max-delta |
2952 | (window--max-delta-1 | |
2953 | right 0 horizontal nil 'before nil pixelwise)) | |
2954 | (setq min-delta | |
2955 | (window--min-delta-1 | |
2956 | left delta horizontal nil 'after nil pixelwise)) | |
562dd5e9 MR |
2957 | (when (or (< max-delta (- delta)) (> min-delta delta)) |
2958 | ;; We can't get the whole DELTA - move as far as possible. | |
2959 | (setq delta (max (- max-delta) min-delta))) | |
2960 | (unless (zerop delta) | |
2961 | ;; Start resizing. | |
5386012d | 2962 | (window--resize-reset frame horizontal) |
562dd5e9 | 2963 | ;; Try to enlarge RIGHT. |
880e6158 MR |
2964 | (setq this-delta |
2965 | (window--resizable | |
2966 | right (- delta) horizontal nil nil nil nil pixelwise)) | |
562dd5e9 | 2967 | (unless (zerop this-delta) |
5386012d | 2968 | (window--resize-this-window |
562dd5e9 MR |
2969 | right this-delta horizontal nil t 'after |
2970 | (if horizontal | |
880e6158 MR |
2971 | (window-pixel-left right) |
2972 | (window-pixel-top right)))) | |
562dd5e9 | 2973 | ;; Shrink windows on left of RIGHT. |
5386012d | 2974 | (window--resize-siblings |
562dd5e9 MR |
2975 | right (- delta) horizontal nil 'before |
2976 | (if horizontal | |
880e6158 MR |
2977 | (+ (window-pixel-left left) (window-pixel-width left)) |
2978 | (+ (window-pixel-top left) (window-pixel-height left))))))) | |
562dd5e9 MR |
2979 | (unless (zerop delta) |
2980 | ;; Don't report an error in the standard case. | |
880e6158 MR |
2981 | (when (window--resize-apply-p frame horizontal) |
2982 | (if (window-resize-apply frame horizontal) | |
2983 | (progn | |
2984 | (window--pixel-to-total frame horizontal) | |
2985 | (run-window-configuration-change-hook frame)) | |
2986 | ;; But do report an error if applying the changes fails. | |
2987 | (error "Failed adjusting window %s" window)))))))) | |
562dd5e9 MR |
2988 | |
2989 | (defun enlarge-window (delta &optional horizontal) | |
2116e93c | 2990 | "Make the selected window DELTA lines taller. |
562dd5e9 MR |
2991 | Interactively, if no argument is given, make the selected window |
2992 | one line taller. If optional argument HORIZONTAL is non-nil, | |
2993 | make selected window wider by DELTA columns. If DELTA is | |
0ff7851c | 2994 | negative, shrink selected window by -DELTA lines or columns." |
562dd5e9 | 2995 | (interactive "p") |
feeb6f53 MR |
2996 | (let ((minibuffer-window (minibuffer-window))) |
2997 | (cond | |
2998 | ((zerop delta)) | |
2999 | ((window-size-fixed-p nil horizontal) | |
3000 | (error "Selected window has fixed size")) | |
3001 | ((window-minibuffer-p) | |
3002 | (if horizontal | |
3003 | (error "Cannot resize minibuffer window horizontally") | |
3004 | (window--resize-mini-window (selected-window) delta))) | |
3005 | ((and (not horizontal) | |
3006 | (window-full-height-p) | |
3007 | (eq (window-frame minibuffer-window) (selected-frame)) | |
3008 | (not resize-mini-windows)) | |
3009 | ;; If the selected window is full height and `resize-mini-windows' | |
3010 | ;; is nil, resize the minibuffer window. | |
3011 | (window--resize-mini-window minibuffer-window (- delta))) | |
880e6158 | 3012 | ((window--resizable-p nil delta horizontal) |
feeb6f53 MR |
3013 | (window-resize nil delta horizontal)) |
3014 | (t | |
3015 | (window-resize | |
3016 | nil (if (> delta 0) | |
3017 | (window-max-delta nil horizontal) | |
3018 | (- (window-min-delta nil horizontal))) | |
3019 | horizontal))))) | |
562dd5e9 MR |
3020 | |
3021 | (defun shrink-window (delta &optional horizontal) | |
2116e93c | 3022 | "Make the selected window DELTA lines smaller. |
562dd5e9 MR |
3023 | Interactively, if no argument is given, make the selected window |
3024 | one line smaller. If optional argument HORIZONTAL is non-nil, | |
3025 | make selected window narrower by DELTA columns. If DELTA is | |
3026 | negative, enlarge selected window by -DELTA lines or columns. | |
0ff7851c | 3027 | Also see the `window-min-height' variable." |
562dd5e9 | 3028 | (interactive "p") |
feeb6f53 MR |
3029 | (let ((minibuffer-window (minibuffer-window))) |
3030 | (cond | |
3031 | ((zerop delta)) | |
3032 | ((window-size-fixed-p nil horizontal) | |
3033 | (error "Selected window has fixed size")) | |
3034 | ((window-minibuffer-p) | |
3035 | (if horizontal | |
3036 | (error "Cannot resize minibuffer window horizontally") | |
3037 | (window--resize-mini-window (selected-window) (- delta)))) | |
3038 | ((and (not horizontal) | |
3039 | (window-full-height-p) | |
3040 | (eq (window-frame minibuffer-window) (selected-frame)) | |
3041 | (not resize-mini-windows)) | |
3042 | ;; If the selected window is full height and `resize-mini-windows' | |
3043 | ;; is nil, resize the minibuffer window. | |
3044 | (window--resize-mini-window minibuffer-window delta)) | |
880e6158 | 3045 | ((window--resizable-p nil (- delta) horizontal) |
feeb6f53 MR |
3046 | (window-resize nil (- delta) horizontal)) |
3047 | (t | |
3048 | (window-resize | |
3049 | nil (if (> delta 0) | |
3050 | (- (window-min-delta nil horizontal)) | |
3051 | (window-max-delta nil horizontal)) | |
3052 | horizontal))))) | |
562dd5e9 | 3053 | |
2d6b6005 | 3054 | (defun maximize-window (&optional window) |
562dd5e9 MR |
3055 | "Maximize WINDOW. |
3056 | Make WINDOW as large as possible without deleting any windows. | |
880e6158 MR |
3057 | WINDOW must be a valid window and defaults to the selected one. |
3058 | ||
3059 | If the option `window-resize-pixelwise' is non-nil maximize | |
3060 | WINDOW pixelwise." | |
562dd5e9 | 3061 | (interactive) |
447f16b8 | 3062 | (setq window (window-normalize-window window)) |
880e6158 MR |
3063 | (window-resize |
3064 | window (window-max-delta window nil nil nil nil nil window-resize-pixelwise) | |
3065 | nil nil window-resize-pixelwise) | |
3066 | (window-resize | |
3067 | window (window-max-delta window t nil nil nil nil window-resize-pixelwise) | |
3068 | t nil window-resize-pixelwise)) | |
3069 | ||
2d6b6005 | 3070 | (defun minimize-window (&optional window) |
562dd5e9 MR |
3071 | "Minimize WINDOW. |
3072 | Make WINDOW as small as possible without deleting any windows. | |
880e6158 MR |
3073 | WINDOW must be a valid window and defaults to the selected one. |
3074 | ||
3075 | If the option `window-resize-pixelwise' is non-nil minimize | |
3076 | WINDOW pixelwise." | |
562dd5e9 | 3077 | (interactive) |
447f16b8 | 3078 | (setq window (window-normalize-window window)) |
880e6158 MR |
3079 | (window-resize |
3080 | window | |
3081 | (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise)) | |
3082 | nil nil window-resize-pixelwise) | |
3083 | (window-resize | |
3084 | window | |
3085 | (- (window-min-delta window t nil nil nil nil window-resize-pixelwise)) | |
3086 | t nil window-resize-pixelwise)) | |
562dd5e9 | 3087 | \f |
4b0d61e3 | 3088 | (defun frame-root-window-p (window) |
9aab8e0d MR |
3089 | "Return non-nil if WINDOW is the root window of its frame." |
3090 | (eq window (frame-root-window window))) | |
6198ccd0 | 3091 | |
5e92ca23 MR |
3092 | (defun window--subtree (window &optional next) |
3093 | "Return window subtree rooted at WINDOW. | |
3094 | Optional argument NEXT non-nil means include WINDOW's right | |
6198ccd0 MR |
3095 | siblings in the return value. |
3096 | ||
3097 | See the documentation of `window-tree' for a description of the | |
3098 | return value." | |
3099 | (let (list) | |
3100 | (while window | |
3101 | (setq list | |
3102 | (cons | |
3103 | (cond | |
d68443dc | 3104 | ((window-top-child window) |
6198ccd0 | 3105 | (cons t (cons (window-edges window) |
5e92ca23 | 3106 | (window--subtree (window-top-child window) t)))) |
d68443dc | 3107 | ((window-left-child window) |
6198ccd0 | 3108 | (cons nil (cons (window-edges window) |
5e92ca23 | 3109 | (window--subtree (window-left-child window) t)))) |
6198ccd0 MR |
3110 | (t window)) |
3111 | list)) | |
d68443dc | 3112 | (setq window (when next (window-next-sibling window)))) |
6198ccd0 MR |
3113 | (nreverse list))) |
3114 | ||
3115 | (defun window-tree (&optional frame) | |
3116 | "Return the window tree of frame FRAME. | |
3117 | FRAME must be a live frame and defaults to the selected frame. | |
3118 | The return value is a list of the form (ROOT MINI), where ROOT | |
3119 | represents the window tree of the frame's root window, and MINI | |
3120 | is the frame's minibuffer window. | |
3121 | ||
3122 | If the root window is not split, ROOT is the root window itself. | |
3123 | Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil | |
3124 | for a horizontal split, and t for a vertical split. EDGES gives | |
be7f5545 MR |
3125 | the combined size and position of the child windows in the split, |
3126 | and the rest of the elements are the child windows in the split. | |
3127 | Each of the child windows may again be a window or a list | |
382c953b | 3128 | representing a window split, and so on. EDGES is a list (LEFT |
6198ccd0 | 3129 | TOP RIGHT BOTTOM) as returned by `window-edges'." |
5386012d | 3130 | (setq frame (window-normalize-frame frame)) |
5e92ca23 | 3131 | (window--subtree (frame-root-window frame) t)) |
9aab8e0d | 3132 | \f |
9397e56f MR |
3133 | (defun other-window (count &optional all-frames) |
3134 | "Select another window in cyclic ordering of windows. | |
3135 | COUNT specifies the number of windows to skip, starting with the | |
3136 | selected window, before making the selection. If COUNT is | |
3137 | positive, skip COUNT windows forwards. If COUNT is negative, | |
3138 | skip -COUNT windows backwards. COUNT zero means do not skip any | |
3139 | window, so select the selected window. In an interactive call, | |
3140 | COUNT is the numeric prefix argument. Return nil. | |
3141 | ||
9a9e9ef0 MR |
3142 | If the `other-window' parameter of the selected window is a |
3143 | function and `ignore-window-parameters' is nil, call that | |
3144 | function with the arguments COUNT and ALL-FRAMES. | |
9397e56f MR |
3145 | |
3146 | This function does not select a window whose `no-other-window' | |
3147 | window parameter is non-nil. | |
3148 | ||
3149 | This function uses `next-window' for finding the window to | |
3150 | select. The argument ALL-FRAMES has the same meaning as in | |
3151 | `next-window', but the MINIBUF argument of `next-window' is | |
3152 | always effectively nil." | |
3153 | (interactive "p") | |
3154 | (let* ((window (selected-window)) | |
3155 | (function (and (not ignore-window-parameters) | |
3156 | (window-parameter window 'other-window))) | |
3157 | old-window old-count) | |
3158 | (if (functionp function) | |
3159 | (funcall function count all-frames) | |
3160 | ;; `next-window' and `previous-window' may return a window we are | |
3161 | ;; not allowed to select. Hence we need an exit strategy in case | |
3162 | ;; all windows are non-selectable. | |
3163 | (catch 'exit | |
3164 | (while (> count 0) | |
3165 | (setq window (next-window window nil all-frames)) | |
3166 | (cond | |
3167 | ((eq window old-window) | |
3168 | (when (= count old-count) | |
3169 | ;; Keep out of infinite loops. When COUNT has not changed | |
3170 | ;; since we last looked at `window' we're probably in one. | |
3171 | (throw 'exit nil))) | |
3172 | ((window-parameter window 'no-other-window) | |
3173 | (unless old-window | |
3174 | ;; The first non-selectable window `next-window' got us: | |
3175 | ;; Remember it and the current value of COUNT. | |
3176 | (setq old-window window) | |
3177 | (setq old-count count))) | |
3178 | (t | |
3179 | (setq count (1- count))))) | |
3180 | (while (< count 0) | |
3181 | (setq window (previous-window window nil all-frames)) | |
3182 | (cond | |
3183 | ((eq window old-window) | |
3184 | (when (= count old-count) | |
3185 | ;; Keep out of infinite loops. When COUNT has not changed | |
3186 | ;; since we last looked at `window' we're probably in one. | |
3187 | (throw 'exit nil))) | |
3188 | ((window-parameter window 'no-other-window) | |
3189 | (unless old-window | |
3190 | ;; The first non-selectable window `previous-window' got | |
3191 | ;; us: Remember it and the current value of COUNT. | |
3192 | (setq old-window window) | |
3193 | (setq old-count count))) | |
3194 | (t | |
3195 | (setq count (1+ count))))) | |
3196 | ||
3197 | (select-window window) | |
3198 | ;; Always return nil. | |
3199 | nil)))) | |
3200 | ||
387522b2 MR |
3201 | ;; This should probably return non-nil when the selected window is part |
3202 | ;; of an atomic window whose root is the frame's root window. | |
3203 | (defun one-window-p (&optional nomini all-frames) | |
3204 | "Return non-nil if the selected window is the only window. | |
3205 | Optional arg NOMINI non-nil means don't count the minibuffer | |
3206 | even if it is active. Otherwise, the minibuffer is counted | |
3207 | when it is active. | |
3208 | ||
3209 | Optional argument ALL-FRAMES specifies the set of frames to | |
3210 | consider, see also `next-window'. ALL-FRAMES nil or omitted | |
3211 | means consider windows on the selected frame only, plus the | |
3212 | minibuffer window if specified by the NOMINI argument. If the | |
3213 | minibuffer counts, consider all windows on all frames that share | |
3214 | that minibuffer too. The remaining non-nil values of ALL-FRAMES | |
3215 | with a special meaning are: | |
3216 | ||
3217 | - t means consider all windows on all existing frames. | |
3218 | ||
3219 | - `visible' means consider all windows on all visible frames on | |
3220 | the current terminal. | |
3221 | ||
3222 | - 0 (the number zero) means consider all windows on all visible | |
3223 | and iconified frames on the current terminal. | |
3224 | ||
3225 | - A frame means consider all windows on that frame only. | |
3226 | ||
3227 | Anything else means consider all windows on the selected frame | |
3228 | and no others." | |
3229 | (let ((base-window (selected-window))) | |
3230 | (if (and nomini (eq base-window (minibuffer-window))) | |
3231 | (setq base-window (next-window base-window))) | |
3232 | (eq base-window | |
3233 | (next-window base-window (if nomini 'arg) all-frames)))) | |
3c448ab6 | 3234 | \f |
9aab8e0d | 3235 | ;;; Deleting windows. |
1b36ed6a | 3236 | (defun window-deletable-p (&optional window) |
9aab8e0d | 3237 | "Return t if WINDOW can be safely deleted from its frame. |
85c2386b | 3238 | WINDOW must be a valid window and defaults to the selected one. |
c660a885 | 3239 | Return 'frame if deleting WINDOW should also delete its frame." |
447f16b8 | 3240 | (setq window (window-normalize-window window)) |
8b0874b5 | 3241 | |
c660a885 | 3242 | (unless (or ignore-window-parameters |
f224e500 | 3243 | (eq (window-parameter window 'delete-window) t)) |
9aab8e0d MR |
3244 | ;; Handle atomicity. |
3245 | (when (window-parameter window 'window-atom) | |
3246 | (setq window (window-atom-root window)))) | |
8b0874b5 | 3247 | |
caceae25 | 3248 | (let ((frame (window-frame window))) |
9aab8e0d MR |
3249 | (cond |
3250 | ((frame-root-window-p window) | |
85e9c04b | 3251 | ;; WINDOW's frame can be deleted only if there are other frames |
46b7967e TN |
3252 | ;; on the same terminal, and it does not contain the active |
3253 | ;; minibuffer. | |
3254 | (unless (or (eq frame (next-frame frame 0)) | |
c660a885 MR |
3255 | ;; We can delete our frame only if no other frame |
3256 | ;; currently uses our minibuffer window. | |
3257 | (catch 'other | |
3258 | (dolist (other (frame-list)) | |
3259 | (when (and (not (eq other frame)) | |
3260 | (eq (window-frame (minibuffer-window other)) | |
3261 | frame)) | |
3262 | (throw 'other t)))) | |
46b7967e TN |
3263 | (let ((minibuf (active-minibuffer-window))) |
3264 | (and minibuf (eq frame (window-frame minibuf))))) | |
85e9c04b | 3265 | 'frame)) |
1b36ed6a | 3266 | ((or ignore-window-parameters |
caceae25 MR |
3267 | (not (eq window (window--major-non-side-window frame)))) |
3268 | ;; WINDOW can be deleted unless it is the major non-side window of | |
3269 | ;; its frame. | |
1f3c99ca | 3270 | t)))) |
9aab8e0d | 3271 | |
be7f5545 MR |
3272 | (defun window--in-subtree-p (window root) |
3273 | "Return t if WINDOW is either ROOT or a member of ROOT's subtree." | |
3274 | (or (eq window root) | |
3275 | (let ((parent (window-parent window))) | |
9aab8e0d MR |
3276 | (catch 'done |
3277 | (while parent | |
be7f5545 | 3278 | (if (eq parent root) |
9aab8e0d MR |
3279 | (throw 'done t) |
3280 | (setq parent (window-parent parent)))))))) | |
3281 | ||
562dd5e9 MR |
3282 | (defun delete-window (&optional window) |
3283 | "Delete WINDOW. | |
85c2386b MR |
3284 | WINDOW must be a valid window and defaults to the selected one. |
3285 | Return nil. | |
562dd5e9 MR |
3286 | |
3287 | If the variable `ignore-window-parameters' is non-nil or the | |
3288 | `delete-window' parameter of WINDOW equals t, do not process any | |
3289 | parameters of WINDOW. Otherwise, if the `delete-window' | |
3290 | parameter of WINDOW specifies a function, call that function with | |
3291 | WINDOW as its sole argument and return the value returned by that | |
3292 | function. | |
3293 | ||
3294 | Otherwise, if WINDOW is part of an atomic window, call | |
3295 | `delete-window' with the root of the atomic window as its | |
85c2386b MR |
3296 | argument. Signal an error if WINDOW is either the only window on |
3297 | its frame, the last non-side window, or part of an atomic window | |
3298 | that is its frame's root window." | |
562dd5e9 | 3299 | (interactive) |
447f16b8 | 3300 | (setq window (window-normalize-window window)) |
562dd5e9 MR |
3301 | (let* ((frame (window-frame window)) |
3302 | (function (window-parameter window 'delete-window)) | |
3303 | (parent (window-parent window)) | |
3304 | atom-root) | |
54f9154c | 3305 | (window--check frame) |
562dd5e9 MR |
3306 | (catch 'done |
3307 | ;; Handle window parameters. | |
3308 | (cond | |
3309 | ;; Ignore window parameters if `ignore-window-parameters' tells | |
3310 | ;; us so or `delete-window' equals t. | |
3311 | ((or ignore-window-parameters (eq function t))) | |
3312 | ((functionp function) | |
3313 | ;; The `delete-window' parameter specifies the function to call. | |
3314 | ;; If that function is `ignore' nothing is done. It's up to the | |
3315 | ;; function called here to avoid infinite recursion. | |
3316 | (throw 'done (funcall function window))) | |
3317 | ((and (window-parameter window 'window-atom) | |
3318 | (setq atom-root (window-atom-root window)) | |
3319 | (not (eq atom-root window))) | |
caceae25 MR |
3320 | (if (eq atom-root (frame-root-window frame)) |
3321 | (error "Root of atomic window is root window of its frame") | |
3322 | (throw 'done (delete-window atom-root)))) | |
562dd5e9 | 3323 | ((not parent) |
caceae25 MR |
3324 | (error "Attempt to delete minibuffer or sole ordinary window")) |
3325 | ((eq window (window--major-non-side-window frame)) | |
3326 | (error "Attempt to delete last non-side window"))) | |
562dd5e9 | 3327 | |
d68443dc | 3328 | (let* ((horizontal (window-left-child parent)) |
880e6158 | 3329 | (size (window-size window horizontal t)) |
562dd5e9 | 3330 | (frame-selected |
be7f5545 | 3331 | (window--in-subtree-p (frame-selected-window frame) window)) |
562dd5e9 MR |
3332 | ;; Emacs 23 preferably gives WINDOW's space to its left |
3333 | ;; sibling. | |
3334 | (sibling (or (window-left window) (window-right window)))) | |
5386012d | 3335 | (window--resize-reset frame horizontal) |
562dd5e9 | 3336 | (cond |
a0c2d0ae | 3337 | ((and (not window-combination-resize) |
880e6158 | 3338 | sibling (window-sizable-p sibling size horizontal nil t)) |
562dd5e9 | 3339 | ;; Resize WINDOW's sibling. |
5386012d | 3340 | (window--resize-this-window sibling size horizontal nil t) |
562dd5e9 MR |
3341 | (set-window-new-normal |
3342 | sibling (+ (window-normal-size sibling horizontal) | |
3343 | (window-normal-size window horizontal)))) | |
880e6158 | 3344 | ((window--resizable-p window (- size) horizontal nil nil nil t t) |
562dd5e9 | 3345 | ;; Can do without resizing fixed-size windows. |
5386012d | 3346 | (window--resize-siblings window (- size) horizontal)) |
562dd5e9 MR |
3347 | (t |
3348 | ;; Can't do without resizing fixed-size windows. | |
5386012d | 3349 | (window--resize-siblings window (- size) horizontal t))) |
562dd5e9 MR |
3350 | ;; Actually delete WINDOW. |
3351 | (delete-window-internal window) | |
880e6158 | 3352 | (window--pixel-to-total frame horizontal) |
562dd5e9 MR |
3353 | (when (and frame-selected |
3354 | (window-parameter | |
3355 | (frame-selected-window frame) 'no-other-window)) | |
3356 | ;; `delete-window-internal' has selected a window that should | |
3357 | ;; not be selected, fix this here. | |
3358 | (other-window -1 frame)) | |
3359 | (run-window-configuration-change-hook frame) | |
54f9154c | 3360 | (window--check frame) |
562dd5e9 MR |
3361 | ;; Always return nil. |
3362 | nil)))) | |
3363 | ||
3364 | (defun delete-other-windows (&optional window) | |
3365 | "Make WINDOW fill its frame. | |
85c2386b | 3366 | WINDOW must be a valid window and defaults to the selected one. |
562dd5e9 MR |
3367 | Return nil. |
3368 | ||
3369 | If the variable `ignore-window-parameters' is non-nil or the | |
3370 | `delete-other-windows' parameter of WINDOW equals t, do not | |
3371 | process any parameters of WINDOW. Otherwise, if the | |
3372 | `delete-other-windows' parameter of WINDOW specifies a function, | |
3373 | call that function with WINDOW as its sole argument and return | |
3374 | the value returned by that function. | |
3375 | ||
3376 | Otherwise, if WINDOW is part of an atomic window, call this | |
3377 | function with the root of the atomic window as its argument. If | |
3378 | WINDOW is a non-side window, make WINDOW the only non-side window | |
382c953b | 3379 | on the frame. Side windows are not deleted. If WINDOW is a side |
562dd5e9 MR |
3380 | window signal an error." |
3381 | (interactive) | |
447f16b8 | 3382 | (setq window (window-normalize-window window)) |
562dd5e9 MR |
3383 | (let* ((frame (window-frame window)) |
3384 | (function (window-parameter window 'delete-other-windows)) | |
3385 | (window-side (window-parameter window 'window-side)) | |
3386 | atom-root side-main) | |
54f9154c | 3387 | (window--check frame) |
562dd5e9 MR |
3388 | (catch 'done |
3389 | (cond | |
3390 | ;; Ignore window parameters if `ignore-window-parameters' is t or | |
3391 | ;; `delete-other-windows' is t. | |
3392 | ((or ignore-window-parameters (eq function t))) | |
3393 | ((functionp function) | |
3394 | ;; The `delete-other-windows' parameter specifies the function | |
3395 | ;; to call. If the function is `ignore' no windows are deleted. | |
3396 | ;; It's up to the function called to avoid infinite recursion. | |
3397 | (throw 'done (funcall function window))) | |
3398 | ((and (window-parameter window 'window-atom) | |
3399 | (setq atom-root (window-atom-root window)) | |
3400 | (not (eq atom-root window))) | |
caceae25 MR |
3401 | (if (eq atom-root (frame-root-window frame)) |
3402 | (error "Root of atomic window is root window of its frame") | |
3403 | (throw 'done (delete-other-windows atom-root)))) | |
562dd5e9 | 3404 | ((memq window-side window-sides) |
caceae25 MR |
3405 | (error "Cannot make side window the only window")) |
3406 | ((and (window-minibuffer-p window) | |
3407 | (not (eq window (frame-root-window window)))) | |
3408 | (error "Can't expand minibuffer to full frame"))) | |
3409 | ||
3410 | ;; If WINDOW is the major non-side window, do nothing. | |
3411 | (if (window-with-parameter 'window-side) | |
3412 | (setq side-main (window--major-non-side-window frame)) | |
3413 | (setq side-main (frame-root-window frame))) | |
562dd5e9 MR |
3414 | (unless (eq window side-main) |
3415 | (delete-other-windows-internal window side-main) | |
3416 | (run-window-configuration-change-hook frame) | |
54f9154c | 3417 | (window--check frame)) |
562dd5e9 MR |
3418 | ;; Always return nil. |
3419 | nil))) | |
9397e56f MR |
3420 | |
3421 | (defun delete-other-windows-vertically (&optional window) | |
3422 | "Delete the windows in the same column with WINDOW, but not WINDOW itself. | |
3423 | This may be a useful alternative binding for \\[delete-other-windows] | |
3424 | if you often split windows horizontally." | |
3425 | (interactive) | |
3426 | (let* ((window (or window (selected-window))) | |
3427 | (edges (window-edges window)) | |
3428 | (w window) delenda) | |
3429 | (while (not (eq (setq w (next-window w 1)) window)) | |
3430 | (let ((e (window-edges w))) | |
3431 | (when (and (= (car e) (car edges)) | |
36cec983 | 3432 | (= (nth 2 e) (nth 2 edges))) |
9397e56f MR |
3433 | (push w delenda)))) |
3434 | (mapc 'delete-window delenda))) | |
3435 | ||
3436 | ;;; Windows and buffers. | |
3437 | ||
3438 | ;; `prev-buffers' and `next-buffers' are two reserved window slots used | |
3439 | ;; for (1) determining which buffer to show in the window when its | |
3440 | ;; buffer shall be buried or killed and (2) which buffer to show for | |
3441 | ;; `switch-to-prev-buffer' and `switch-to-next-buffer'. | |
3442 | ||
3443 | ;; `prev-buffers' consists of <buffer, window-start, window-point> | |
3444 | ;; triples. The entries on this list are ordered by the time their | |
3445 | ;; buffer has been removed from the window, the most recently removed | |
3446 | ;; buffer's entry being first. The window-start and window-point | |
3447 | ;; components are `window-start' and `window-point' at the time the | |
3448 | ;; buffer was removed from the window which implies that the entry must | |
3449 | ;; be added when `set-window-buffer' removes the buffer from the window. | |
3450 | ||
3451 | ;; `next-buffers' is the list of buffers that have been replaced | |
3452 | ;; recently by `switch-to-prev-buffer'. These buffers are the least | |
3453 | ;; preferred candidates of `switch-to-prev-buffer' and the preferred | |
3454 | ;; candidates of `switch-to-next-buffer' to switch to. This list is | |
3455 | ;; reset to nil by any action changing the window's buffer with the | |
3456 | ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'. | |
3457 | ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it, | |
3458 | ;; `switch-to-next-buffer' pops the last pushed buffer from it. | |
3459 | ||
3460 | ;; Both `prev-buffers' and `next-buffers' may reference killed buffers | |
3461 | ;; if such a buffer was killed while the window was hidden within a | |
3462 | ;; window configuration. Such killed buffers get removed whenever | |
3463 | ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them. | |
3464 | ||
3465 | ;; The following function is called by `set-window-buffer' _before_ it | |
3466 | ;; replaces the buffer of the argument window with the new buffer. | |
3467 | (defun record-window-buffer (&optional window) | |
3468 | "Record WINDOW's buffer. | |
3469 | WINDOW must be a live window and defaults to the selected one." | |
447f16b8 | 3470 | (let* ((window (window-normalize-window window t)) |
9397e56f MR |
3471 | (buffer (window-buffer window)) |
3472 | (entry (assq buffer (window-prev-buffers window)))) | |
3473 | ;; Reset WINDOW's next buffers. If needed, they are resurrected by | |
3474 | ;; `switch-to-prev-buffer' and `switch-to-next-buffer'. | |
3475 | (set-window-next-buffers window nil) | |
3476 | ||
3477 | (when entry | |
3478 | ;; Remove all entries for BUFFER from WINDOW's previous buffers. | |
3479 | (set-window-prev-buffers | |
3480 | window (assq-delete-all buffer (window-prev-buffers window)))) | |
3481 | ||
3482 | ;; Don't record insignificant buffers. | |
3483 | (unless (eq (aref (buffer-name buffer) 0) ?\s) | |
3484 | ;; Add an entry for buffer to WINDOW's previous buffers. | |
3485 | (with-current-buffer buffer | |
3486 | (let ((start (window-start window)) | |
5481664a | 3487 | (point (window-point window))) |
9397e56f MR |
3488 | (setq entry |
3489 | (cons buffer | |
3490 | (if entry | |
3491 | ;; We have an entry, update marker positions. | |
3492 | (list (set-marker (nth 1 entry) start) | |
3493 | (set-marker (nth 2 entry) point)) | |
3494 | ;; Make new markers. | |
3495 | (list (copy-marker start) | |
92346275 MR |
3496 | (copy-marker |
3497 | ;; Preserve window-point-insertion-type | |
3498 | ;; (Bug#12588). | |
3499 | point window-point-insertion-type))))) | |
9397e56f | 3500 | (set-window-prev-buffers |
c660a885 MR |
3501 | window (cons entry (window-prev-buffers window))))) |
3502 | ||
3503 | (run-hooks 'buffer-list-update-hook)))) | |
9397e56f MR |
3504 | |
3505 | (defun unrecord-window-buffer (&optional window buffer) | |
3506 | "Unrecord BUFFER in WINDOW. | |
3507 | WINDOW must be a live window and defaults to the selected one. | |
3508 | BUFFER must be a live buffer and defaults to the buffer of | |
3509 | WINDOW." | |
447f16b8 | 3510 | (let* ((window (window-normalize-window window t)) |
9397e56f MR |
3511 | (buffer (or buffer (window-buffer window)))) |
3512 | (set-window-prev-buffers | |
3513 | window (assq-delete-all buffer (window-prev-buffers window))) | |
3514 | (set-window-next-buffers | |
3515 | window (delq buffer (window-next-buffers window))))) | |
3516 | ||
3517 | (defun set-window-buffer-start-and-point (window buffer &optional start point) | |
3518 | "Set WINDOW's buffer to BUFFER. | |
85c2386b | 3519 | WINDOW must be a live window and defaults to the selected one. |
9397e56f MR |
3520 | Optional argument START non-nil means set WINDOW's start position |
3521 | to START. Optional argument POINT non-nil means set WINDOW's | |
3522 | point to POINT. If WINDOW is selected this also sets BUFFER's | |
3523 | `point' to POINT. If WINDOW is selected and the buffer it showed | |
3524 | before was current this also makes BUFFER the current buffer." | |
85c2386b | 3525 | (setq window (window-normalize-window window t)) |
9397e56f MR |
3526 | (let ((selected (eq window (selected-window))) |
3527 | (current (eq (window-buffer window) (current-buffer)))) | |
3528 | (set-window-buffer window buffer) | |
3529 | (when (and selected current) | |
3530 | (set-buffer buffer)) | |
3531 | (when start | |
cf4eacfd MR |
3532 | ;; Don't force window-start here (even if POINT is nil). |
3533 | (set-window-start window start t)) | |
9397e56f | 3534 | (when point |
5481664a | 3535 | (set-window-point window point)))) |
9397e56f | 3536 | |
dcb6e7b3 MR |
3537 | (defcustom switch-to-visible-buffer t |
3538 | "If non-nil, allow switching to an already visible buffer. | |
3539 | If this variable is non-nil, `switch-to-prev-buffer' and | |
3540 | `switch-to-next-buffer' may switch to an already visible buffer | |
43bcfda6 MR |
3541 | provided the buffer was shown before in the window specified as |
3542 | argument to those functions. If this variable is nil, | |
3543 | `switch-to-prev-buffer' and `switch-to-next-buffer' always try to | |
3544 | avoid switching to a buffer that is already visible in another | |
3545 | window on the same frame." | |
dcb6e7b3 MR |
3546 | :type 'boolean |
3547 | :version "24.1" | |
3548 | :group 'windows) | |
3549 | ||
9397e56f MR |
3550 | (defun switch-to-prev-buffer (&optional window bury-or-kill) |
3551 | "In WINDOW switch to previous buffer. | |
3552 | WINDOW must be a live window and defaults to the selected one. | |
502e3f89 MR |
3553 | Return the buffer switched to, nil if no suitable buffer could be |
3554 | found. | |
9397e56f MR |
3555 | |
3556 | Optional argument BURY-OR-KILL non-nil means the buffer currently | |
3557 | shown in WINDOW is about to be buried or killed and consequently | |
78dd6ab1 MR |
3558 | shall not be switched to in future invocations of this command. |
3559 | ||
3560 | As a special case, if BURY-OR-KILL equals `append', this means to | |
3561 | move the buffer to the end of WINDOW's previous buffers list so a | |
3562 | future invocation of `switch-to-prev-buffer' less likely switches | |
3563 | to it." | |
9397e56f | 3564 | (interactive) |
447f16b8 | 3565 | (let* ((window (window-normalize-window window t)) |
dcb6e7b3 | 3566 | (frame (window-frame window)) |
9397e56f MR |
3567 | (old-buffer (window-buffer window)) |
3568 | ;; Save this since it's destroyed by `set-window-buffer'. | |
3569 | (next-buffers (window-next-buffers window)) | |
862382df | 3570 | (pred (frame-parameter frame 'buffer-predicate)) |
78dd6ab1 MR |
3571 | entry new-buffer killed-buffers visible) |
3572 | (when (window-minibuffer-p window) | |
3573 | ;; Don't switch in minibuffer window. | |
3574 | (unless (setq window (minibuffer-selected-window)) | |
3575 | (error "Window %s is a minibuffer window" window))) | |
3576 | ||
1b36ed6a | 3577 | (when (window-dedicated-p window) |
78dd6ab1 | 3578 | ;; Don't switch in dedicated window. |
1b36ed6a MR |
3579 | (error "Window %s is dedicated to buffer %s" window old-buffer)) |
3580 | ||
3581 | (catch 'found | |
3582 | ;; Scan WINDOW's previous buffers first, skipping entries of next | |
3583 | ;; buffers. | |
3584 | (dolist (entry (window-prev-buffers window)) | |
78dd6ab1 MR |
3585 | (when (and (setq new-buffer (car entry)) |
3586 | (or (buffer-live-p new-buffer) | |
1b36ed6a | 3587 | (not (setq killed-buffers |
78dd6ab1 MR |
3588 | (cons new-buffer killed-buffers)))) |
3589 | (not (eq new-buffer old-buffer)) | |
3590 | (or (null pred) (funcall pred new-buffer)) | |
3591 | ;; When BURY-OR-KILL is nil, avoid switching to a | |
3592 | ;; buffer in WINDOW's next buffers list. | |
3593 | (or bury-or-kill (not (memq new-buffer next-buffers)))) | |
dcb6e7b3 | 3594 | (if (and (not switch-to-visible-buffer) |
78dd6ab1 MR |
3595 | (get-buffer-window new-buffer frame)) |
3596 | ;; Try to avoid showing a buffer visible in some other | |
3597 | ;; window. | |
3598 | (setq visible new-buffer) | |
502e3f89 MR |
3599 | (set-window-buffer-start-and-point |
3600 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
3601 | (throw 'found t)))) | |
1b36ed6a MR |
3602 | ;; Scan reverted buffer list of WINDOW's frame next, skipping |
3603 | ;; entries of next buffers. Note that when we bury or kill a | |
3604 | ;; buffer we don't reverse the global buffer list to avoid showing | |
3605 | ;; a buried buffer instead. Otherwise, we must reverse the global | |
3606 | ;; buffer list in order to make sure that switching to the | |
3607 | ;; previous/next buffer traverse it in opposite directions. | |
3608 | (dolist (buffer (if bury-or-kill | |
dcb6e7b3 MR |
3609 | (buffer-list frame) |
3610 | (nreverse (buffer-list frame)))) | |
1b36ed6a MR |
3611 | (when (and (buffer-live-p buffer) |
3612 | (not (eq buffer old-buffer)) | |
862382df | 3613 | (or (null pred) (funcall pred buffer)) |
78dd6ab1 | 3614 | (not (eq (aref (buffer-name buffer) 0) ?\s)) |
1b36ed6a | 3615 | (or bury-or-kill (not (memq buffer next-buffers)))) |
dcb6e7b3 | 3616 | (if (get-buffer-window buffer frame) |
1b36ed6a | 3617 | ;; Try to avoid showing a buffer visible in some other window. |
dcb6e7b3 MR |
3618 | (unless visible |
3619 | (setq visible buffer)) | |
1b36ed6a MR |
3620 | (setq new-buffer buffer) |
3621 | (set-window-buffer-start-and-point window new-buffer) | |
3622 | (throw 'found t)))) | |
3623 | (unless bury-or-kill | |
3624 | ;; Scan reverted next buffers last (must not use nreverse | |
3625 | ;; here!). | |
3626 | (dolist (buffer (reverse next-buffers)) | |
3627 | ;; Actually, buffer _must_ be live here since otherwise it | |
3628 | ;; would have been caught in the scan of previous buffers. | |
3629 | (when (and (or (buffer-live-p buffer) | |
9397e56f | 3630 | (not (setq killed-buffers |
1b36ed6a | 3631 | (cons buffer killed-buffers)))) |
9397e56f | 3632 | (not (eq buffer old-buffer)) |
862382df | 3633 | (or (null pred) (funcall pred buffer)) |
1b36ed6a | 3634 | (setq entry (assq buffer (window-prev-buffers window)))) |
9397e56f | 3635 | (setq new-buffer buffer) |
1b36ed6a MR |
3636 | (set-window-buffer-start-and-point |
3637 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
9397e56f | 3638 | (throw 'found t)))) |
1b36ed6a MR |
3639 | |
3640 | ;; Show a buffer visible in another window. | |
3641 | (when visible | |
3642 | (setq new-buffer visible) | |
3643 | (set-window-buffer-start-and-point window new-buffer))) | |
3644 | ||
3645 | (if bury-or-kill | |
78dd6ab1 MR |
3646 | (let ((entry (and (eq bury-or-kill 'append) |
3647 | (assq old-buffer (window-prev-buffers window))))) | |
3648 | ;; Remove `old-buffer' from WINDOW's previous and (restored list | |
3649 | ;; of) next buffers. | |
1b36ed6a MR |
3650 | (set-window-prev-buffers |
3651 | window (assq-delete-all old-buffer (window-prev-buffers window))) | |
78dd6ab1 MR |
3652 | (set-window-next-buffers window (delq old-buffer next-buffers)) |
3653 | (when entry | |
3654 | ;; Append old-buffer's entry to list of WINDOW's previous | |
3655 | ;; buffers so it's less likely to get switched to soon but | |
3656 | ;; `display-buffer-in-previous-window' can nevertheless find | |
3657 | ;; it. | |
3658 | (set-window-prev-buffers | |
3659 | window (append (window-prev-buffers window) (list entry))))) | |
1b36ed6a MR |
3660 | ;; Move `old-buffer' to head of WINDOW's restored list of next |
3661 | ;; buffers. | |
3662 | (set-window-next-buffers | |
3663 | window (cons old-buffer (delq old-buffer next-buffers)))) | |
9397e56f MR |
3664 | |
3665 | ;; Remove killed buffers from WINDOW's previous and next buffers. | |
3666 | (when killed-buffers | |
3667 | (dolist (buffer killed-buffers) | |
3668 | (set-window-prev-buffers | |
3669 | window (assq-delete-all buffer (window-prev-buffers window))) | |
3670 | (set-window-next-buffers | |
3671 | window (delq buffer (window-next-buffers window))))) | |
3672 | ||
3673 | ;; Return new-buffer. | |
3674 | new-buffer)) | |
3675 | ||
3676 | (defun switch-to-next-buffer (&optional window) | |
3677 | "In WINDOW switch to next buffer. | |
502e3f89 MR |
3678 | WINDOW must be a live window and defaults to the selected one. |
3679 | Return the buffer switched to, nil if no suitable buffer could be | |
3680 | found." | |
9397e56f | 3681 | (interactive) |
447f16b8 | 3682 | (let* ((window (window-normalize-window window t)) |
dcb6e7b3 | 3683 | (frame (window-frame window)) |
9397e56f MR |
3684 | (old-buffer (window-buffer window)) |
3685 | (next-buffers (window-next-buffers window)) | |
862382df | 3686 | (pred (frame-parameter frame 'buffer-predicate)) |
78dd6ab1 MR |
3687 | new-buffer entry killed-buffers visible) |
3688 | (when (window-minibuffer-p window) | |
3689 | ;; Don't switch in minibuffer window. | |
3690 | (unless (setq window (minibuffer-selected-window)) | |
3691 | (error "Window %s is a minibuffer window" window))) | |
3692 | ||
9397e56f | 3693 | (when (window-dedicated-p window) |
78dd6ab1 | 3694 | ;; Don't switch in dedicated window. |
9397e56f MR |
3695 | (error "Window %s is dedicated to buffer %s" window old-buffer)) |
3696 | ||
3697 | (catch 'found | |
3698 | ;; Scan WINDOW's next buffers first. | |
3699 | (dolist (buffer next-buffers) | |
3700 | (when (and (or (buffer-live-p buffer) | |
3701 | (not (setq killed-buffers | |
3702 | (cons buffer killed-buffers)))) | |
3703 | (not (eq buffer old-buffer)) | |
862382df | 3704 | (or (null pred) (funcall pred buffer)) |
9397e56f MR |
3705 | (setq entry (assq buffer (window-prev-buffers window)))) |
3706 | (setq new-buffer buffer) | |
3707 | (set-window-buffer-start-and-point | |
3708 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
3709 | (throw 'found t))) | |
3710 | ;; Scan the buffer list of WINDOW's frame next, skipping previous | |
3711 | ;; buffers entries. | |
dcb6e7b3 | 3712 | (dolist (buffer (buffer-list frame)) |
78dd6ab1 MR |
3713 | (when (and (buffer-live-p buffer) |
3714 | (not (eq buffer old-buffer)) | |
862382df | 3715 | (or (null pred) (funcall pred buffer)) |
78dd6ab1 | 3716 | (not (eq (aref (buffer-name buffer) 0) ?\s)) |
9397e56f | 3717 | (not (assq buffer (window-prev-buffers window)))) |
dcb6e7b3 | 3718 | (if (get-buffer-window buffer frame) |
9397e56f MR |
3719 | ;; Try to avoid showing a buffer visible in some other window. |
3720 | (setq visible buffer) | |
3721 | (setq new-buffer buffer) | |
3722 | (set-window-buffer-start-and-point window new-buffer) | |
3723 | (throw 'found t)))) | |
3724 | ;; Scan WINDOW's reverted previous buffers last (must not use | |
3725 | ;; nreverse here!) | |
3726 | (dolist (entry (reverse (window-prev-buffers window))) | |
78dd6ab1 MR |
3727 | (when (and (setq new-buffer (car entry)) |
3728 | (or (buffer-live-p new-buffer) | |
9397e56f | 3729 | (not (setq killed-buffers |
78dd6ab1 MR |
3730 | (cons new-buffer killed-buffers)))) |
3731 | (not (eq new-buffer old-buffer)) | |
3732 | (or (null pred) (funcall pred new-buffer))) | |
dcb6e7b3 | 3733 | (if (and (not switch-to-visible-buffer) |
78dd6ab1 | 3734 | (get-buffer-window new-buffer frame)) |
dcb6e7b3 MR |
3735 | ;; Try to avoid showing a buffer visible in some other window. |
3736 | (unless visible | |
78dd6ab1 | 3737 | (setq visible new-buffer)) |
dcb6e7b3 MR |
3738 | (set-window-buffer-start-and-point |
3739 | window new-buffer (nth 1 entry) (nth 2 entry)) | |
3740 | (throw 'found t)))) | |
9397e56f MR |
3741 | |
3742 | ;; Show a buffer visible in another window. | |
3743 | (when visible | |
3744 | (setq new-buffer visible) | |
3745 | (set-window-buffer-start-and-point window new-buffer))) | |
3746 | ||
3747 | ;; Remove `new-buffer' from and restore WINDOW's next buffers. | |
3748 | (set-window-next-buffers window (delq new-buffer next-buffers)) | |
3749 | ||
3750 | ;; Remove killed buffers from WINDOW's previous and next buffers. | |
3751 | (when killed-buffers | |
3752 | (dolist (buffer killed-buffers) | |
3753 | (set-window-prev-buffers | |
3754 | window (assq-delete-all buffer (window-prev-buffers window))) | |
3755 | (set-window-next-buffers | |
3756 | window (delq buffer (window-next-buffers window))))) | |
3757 | ||
3758 | ;; Return new-buffer. | |
3759 | new-buffer)) | |
3760 | ||
3761 | (defun get-next-valid-buffer (list &optional buffer visible-ok frame) | |
3762 | "Search LIST for a valid buffer to display in FRAME. | |
3763 | Return nil when all buffers in LIST are undesirable for display, | |
3764 | otherwise return the first suitable buffer in LIST. | |
3765 | ||
3766 | Buffers not visible in windows are preferred to visible buffers, | |
3767 | unless VISIBLE-OK is non-nil. | |
3768 | If the optional argument FRAME is nil, it defaults to the selected frame. | |
3769 | If BUFFER is non-nil, ignore occurrences of that buffer in LIST." | |
3770 | ;; This logic is more or less copied from other-buffer. | |
3771 | (setq frame (or frame (selected-frame))) | |
3772 | (let ((pred (frame-parameter frame 'buffer-predicate)) | |
3773 | found buf) | |
3774 | (while (and (not found) list) | |
3775 | (setq buf (car list)) | |
3776 | (if (and (not (eq buffer buf)) | |
3777 | (buffer-live-p buf) | |
3778 | (or (null pred) (funcall pred buf)) | |
3779 | (not (eq (aref (buffer-name buf) 0) ?\s)) | |
3780 | (or visible-ok (null (get-buffer-window buf 'visible)))) | |
3781 | (setq found buf) | |
3782 | (setq list (cdr list)))) | |
3783 | (car list))) | |
3784 | ||
3785 | (defun last-buffer (&optional buffer visible-ok frame) | |
3786 | "Return the last buffer in FRAME's buffer list. | |
3787 | If BUFFER is the last buffer, return the preceding buffer | |
3788 | instead. Buffers not visible in windows are preferred to visible | |
3789 | buffers, unless optional argument VISIBLE-OK is non-nil. | |
3790 | Optional third argument FRAME nil or omitted means use the | |
3791 | selected frame's buffer list. If no such buffer exists, return | |
3792 | the buffer `*scratch*', creating it if necessary." | |
3793 | (setq frame (or frame (selected-frame))) | |
3794 | (or (get-next-valid-buffer (nreverse (buffer-list frame)) | |
3795 | buffer visible-ok frame) | |
3796 | (get-buffer "*scratch*") | |
3797 | (let ((scratch (get-buffer-create "*scratch*"))) | |
3798 | (set-buffer-major-mode scratch) | |
3799 | scratch))) | |
3800 | ||
5a4cf282 MR |
3801 | (defcustom frame-auto-hide-function #'iconify-frame |
3802 | "Function called to automatically hide frames. | |
3803 | The function is called with one argument - a frame. | |
3804 | ||
3805 | Functions affected by this option are those that bury a buffer | |
3806 | shown in a separate frame like `quit-window' and `bury-buffer'." | |
3807 | :type '(choice (const :tag "Iconify" iconify-frame) | |
3808 | (const :tag "Delete" delete-frame) | |
3809 | (const :tag "Do nothing" ignore) | |
3810 | function) | |
3811 | :group 'windows | |
49677495 MR |
3812 | :group 'frames |
3813 | :version "24.1") | |
0e2070b5 MR |
3814 | |
3815 | (defun window--delete (&optional window dedicated-only kill) | |
3816 | "Delete WINDOW if possible. | |
3817 | WINDOW must be a live window and defaults to the selected one. | |
3818 | Optional argument DEDICATED-ONLY non-nil means to delete WINDOW | |
3819 | only if it's dedicated to its buffer. Optional argument KILL | |
3820 | means the buffer shown in window will be killed. Return non-nil | |
c557cd6b | 3821 | if WINDOW gets deleted or its frame is auto-hidden." |
447f16b8 | 3822 | (setq window (window-normalize-window window t)) |
0e2070b5 | 3823 | (unless (and dedicated-only (not (window-dedicated-p window))) |
cb882333 | 3824 | (let ((deletable (window-deletable-p window))) |
0e2070b5 MR |
3825 | (cond |
3826 | ((eq deletable 'frame) | |
3827 | (let ((frame (window-frame window))) | |
c557cd6b MR |
3828 | (cond |
3829 | (kill | |
3830 | (delete-frame frame)) | |
3831 | ((functionp frame-auto-hide-function) | |
3832 | (funcall frame-auto-hide-function frame)))) | |
0e2070b5 MR |
3833 | 'frame) |
3834 | (deletable | |
3835 | (delete-window window) | |
3836 | t))))) | |
3837 | ||
9397e56f MR |
3838 | (defun bury-buffer (&optional buffer-or-name) |
3839 | "Put BUFFER-OR-NAME at the end of the list of all buffers. | |
3840 | There it is the least likely candidate for `other-buffer' to | |
3841 | return; thus, the least likely buffer for \\[switch-to-buffer] to | |
3842 | select by default. | |
3843 | ||
3844 | You can specify a buffer name as BUFFER-OR-NAME, or an actual | |
3845 | buffer object. If BUFFER-OR-NAME is nil or omitted, bury the | |
3846 | current buffer. Also, if BUFFER-OR-NAME is nil or omitted, | |
3847 | remove the current buffer from the selected window if it is | |
3848 | displayed there." | |
3849 | (interactive) | |
5386012d | 3850 | (let* ((buffer (window-normalize-buffer buffer-or-name))) |
9397e56f MR |
3851 | ;; If `buffer-or-name' is not on the selected frame we unrecord it |
3852 | ;; although it's not "here" (call it a feature). | |
e4ed06f1 | 3853 | (bury-buffer-internal buffer) |
9397e56f MR |
3854 | ;; Handle case where `buffer-or-name' is nil and the current buffer |
3855 | ;; is shown in the selected window. | |
3856 | (cond | |
3857 | ((or buffer-or-name (not (eq buffer (window-buffer))))) | |
0e2070b5 MR |
3858 | ((window--delete nil t)) |
3859 | (t | |
3860 | ;; Switch to another buffer in window. | |
3861 | (set-window-dedicated-p nil nil) | |
1885e5b8 | 3862 | (switch-to-prev-buffer nil 'bury))) |
1b36ed6a | 3863 | |
9397e56f MR |
3864 | ;; Always return nil. |
3865 | nil)) | |
3866 | ||
3867 | (defun unbury-buffer () | |
3868 | "Switch to the last buffer in the buffer list." | |
3869 | (interactive) | |
3870 | (switch-to-buffer (last-buffer))) | |
3871 | ||
3872 | (defun next-buffer () | |
3873 | "In selected window switch to next buffer." | |
3874 | (interactive) | |
85c2386b MR |
3875 | (cond |
3876 | ((window-minibuffer-p) | |
3877 | (error "Cannot switch buffers in minibuffer window")) | |
3878 | ((eq (window-dedicated-p) t) | |
3879 | (error "Window is strongly dedicated to its buffer")) | |
3880 | (t | |
3881 | (switch-to-next-buffer)))) | |
9397e56f MR |
3882 | |
3883 | (defun previous-buffer () | |
3884 | "In selected window switch to previous buffer." | |
3885 | (interactive) | |
85c2386b MR |
3886 | (cond |
3887 | ((window-minibuffer-p) | |
3888 | (error "Cannot switch buffers in minibuffer window")) | |
3889 | ((eq (window-dedicated-p) t) | |
3890 | (error "Window is strongly dedicated to its buffer")) | |
3891 | (t | |
3892 | (switch-to-prev-buffer)))) | |
9397e56f MR |
3893 | |
3894 | (defun delete-windows-on (&optional buffer-or-name frame) | |
3895 | "Delete all windows showing BUFFER-OR-NAME. | |
3896 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer | |
3897 | and defaults to the current buffer. | |
3898 | ||
3899 | The following non-nil values of the optional argument FRAME | |
3900 | have special meanings: | |
3901 | ||
3902 | - t means consider all windows on the selected frame only. | |
3903 | ||
3904 | - `visible' means consider all windows on all visible frames on | |
3905 | the current terminal. | |
3906 | ||
3907 | - 0 (the number zero) means consider all windows on all visible | |
3908 | and iconified frames on the current terminal. | |
3909 | ||
3910 | - A frame means consider all windows on that frame only. | |
3911 | ||
3912 | Any other value of FRAME means consider all windows on all | |
3913 | frames. | |
3914 | ||
3915 | When a window showing BUFFER-OR-NAME is dedicated and the only | |
3916 | window of its frame, that frame is deleted when there are other | |
3917 | frames left." | |
3918 | (interactive "BDelete windows on (buffer):\nP") | |
5386012d | 3919 | (let ((buffer (window-normalize-buffer buffer-or-name)) |
9397e56f MR |
3920 | ;; Handle the "inverted" meaning of the FRAME argument wrt other |
3921 | ;; `window-list-1' based function. | |
3922 | (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame)))) | |
3923 | (dolist (window (window-list-1 nil nil all-frames)) | |
3924 | (if (eq (window-buffer window) buffer) | |
1b36ed6a | 3925 | (let ((deletable (window-deletable-p window))) |
9397e56f | 3926 | (cond |
1b36ed6a MR |
3927 | ((and (eq deletable 'frame) (window-dedicated-p window)) |
3928 | ;; Delete frame if and only if window is dedicated. | |
9397e56f | 3929 | (delete-frame (window-frame window))) |
1b36ed6a MR |
3930 | ((eq deletable t) |
3931 | ;; Delete window. | |
9397e56f MR |
3932 | (delete-window window)) |
3933 | (t | |
3934 | ;; In window switch to previous buffer. | |
3935 | (set-window-dedicated-p window nil) | |
3936 | (switch-to-prev-buffer window 'bury)))) | |
3937 | ;; If a window doesn't show BUFFER, unrecord BUFFER in it. | |
3938 | (unrecord-window-buffer window buffer))))) | |
3939 | ||
3940 | (defun replace-buffer-in-windows (&optional buffer-or-name) | |
3941 | "Replace BUFFER-OR-NAME with some other buffer in all windows showing it. | |
3942 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer | |
3943 | and defaults to the current buffer. | |
3944 | ||
0e2070b5 MR |
3945 | When a window showing BUFFER-OR-NAME is dedicated, that window is |
3946 | deleted. If that window is the only window on its frame, the | |
3947 | frame is deleted too when there are other frames left. If there | |
3948 | are no other frames left, some other buffer is displayed in that | |
3949 | window. | |
9397e56f MR |
3950 | |
3951 | This function removes the buffer denoted by BUFFER-OR-NAME from | |
3952 | all window-local buffer lists." | |
24901d61 | 3953 | (interactive "bBuffer to replace: ") |
5386012d | 3954 | (let ((buffer (window-normalize-buffer buffer-or-name))) |
9397e56f MR |
3955 | (dolist (window (window-list-1 nil nil t)) |
3956 | (if (eq (window-buffer window) buffer) | |
0e2070b5 MR |
3957 | (unless (window--delete window t t) |
3958 | ;; Switch to another buffer in window. | |
3959 | (set-window-dedicated-p window nil) | |
3960 | (switch-to-prev-buffer window 'kill)) | |
9397e56f MR |
3961 | ;; Unrecord BUFFER in WINDOW. |
3962 | (unrecord-window-buffer window buffer))))) | |
3963 | ||
78dd6ab1 MR |
3964 | (defun quit-restore-window (&optional window bury-or-kill) |
3965 | "Quit WINDOW and deal with its buffer. | |
cf4eacfd | 3966 | WINDOW must be a live window and defaults to the selected one. |
9397e56f MR |
3967 | |
3968 | According to information stored in WINDOW's `quit-restore' window | |
382c953b JB |
3969 | parameter either (1) delete WINDOW and its frame, (2) delete |
3970 | WINDOW, (3) restore the buffer previously displayed in WINDOW, | |
3971 | or (4) make WINDOW display some other buffer than the present | |
78dd6ab1 MR |
3972 | one. If non-nil, reset `quit-restore' parameter to nil. |
3973 | ||
3974 | Optional second argument BURY-OR-KILL tells how to proceed with | |
3975 | the buffer of WINDOW. The following values are handled: | |
3976 | ||
3977 | `nil' means to not handle the buffer in a particular way. This | |
3978 | means that if WINDOW is not deleted by this function, invoking | |
3979 | `switch-to-prev-buffer' will usually show the buffer again. | |
3980 | ||
3981 | `append' means that if WINDOW is not deleted, move its buffer to | |
3982 | the end of WINDOW's previous buffers so it's less likely that a | |
3983 | future invocation of `switch-to-prev-buffer' will switch to it. | |
3984 | Also, move the buffer to the end of the frame's buffer list. | |
3985 | ||
3986 | `bury' means that if WINDOW is not deleted, remove its buffer | |
3987 | from WINDOW'S list of previous buffers. Also, move the buffer | |
3988 | to the end of the frame's buffer list. This value provides the | |
3989 | most reliable remedy to not have `switch-to-prev-buffer' switch | |
3990 | to this buffer again without killing the buffer. | |
3991 | ||
3992 | `kill' means to kill WINDOW's buffer." | |
447f16b8 | 3993 | (setq window (window-normalize-window window t)) |
cf4eacfd MR |
3994 | (let* ((buffer (window-buffer window)) |
3995 | (quit-restore (window-parameter window 'quit-restore)) | |
3996 | (prev-buffer | |
3997 | (let* ((prev-buffers (window-prev-buffers window)) | |
3998 | (prev-buffer (caar prev-buffers))) | |
3999 | (and (or (not (eq prev-buffer buffer)) | |
4000 | (and (cdr prev-buffers) | |
4001 | (not (eq (setq prev-buffer (cadr prev-buffers)) | |
4002 | buffer)))) | |
4003 | prev-buffer))) | |
78dd6ab1 | 4004 | quad entry) |
9397e56f | 4005 | (cond |
cf4eacfd | 4006 | ((and (not prev-buffer) |
218e997a MR |
4007 | (or (eq (nth 1 quit-restore) 'frame) |
4008 | (and (eq (nth 1 quit-restore) 'window) | |
4009 | ;; If the window has been created on an existing | |
4010 | ;; frame and ended up as the sole window on that | |
4011 | ;; frame, do not delete it (Bug#12764). | |
4012 | (not (eq window (frame-root-window window))))) | |
0e2070b5 MR |
4013 | (eq (nth 3 quit-restore) buffer) |
4014 | ;; Delete WINDOW if possible. | |
78dd6ab1 | 4015 | (window--delete window nil (eq bury-or-kill 'kill))) |
9397e56f MR |
4016 | ;; If the previously selected window is still alive, select it. |
4017 | (when (window-live-p (nth 2 quit-restore)) | |
4018 | (select-window (nth 2 quit-restore)))) | |
cf4eacfd MR |
4019 | ((and (listp (setq quad (nth 1 quit-restore))) |
4020 | (buffer-live-p (car quad)) | |
4021 | (eq (nth 3 quit-restore) buffer)) | |
4022 | ;; Show another buffer stored in quit-restore parameter. | |
78dd6ab1 | 4023 | (when (and (integerp (nth 3 quad)) |
880e6158 | 4024 | (/= (nth 3 quad) (window-total-height window))) |
cf4eacfd MR |
4025 | ;; Try to resize WINDOW to its old height but don't signal an |
4026 | ;; error. | |
4027 | (condition-case nil | |
880e6158 | 4028 | (window-resize window (- (nth 3 quad) (window-total-height window))) |
cf4eacfd | 4029 | (error nil))) |
78dd6ab1 | 4030 | (set-window-dedicated-p window nil) |
1885e5b8 | 4031 | ;; Restore WINDOW's previous buffer, start and point position. |
cf4eacfd MR |
4032 | (set-window-buffer-start-and-point |
4033 | window (nth 0 quad) (nth 1 quad) (nth 2 quad)) | |
78dd6ab1 MR |
4034 | ;; Deal with the buffer we just removed from WINDOW. |
4035 | (setq entry (and (eq bury-or-kill 'append) | |
4036 | (assq buffer (window-prev-buffers window)))) | |
4037 | (when bury-or-kill | |
4038 | ;; Remove buffer from WINDOW's previous and next buffers. | |
4039 | (set-window-prev-buffers | |
4040 | window (assq-delete-all buffer (window-prev-buffers window))) | |
4041 | (set-window-next-buffers | |
4042 | window (delq buffer (window-next-buffers window)))) | |
4043 | (when entry | |
4044 | ;; Append old buffer's entry to list of WINDOW's previous | |
4045 | ;; buffers so it's less likely to get switched to soon but | |
4046 | ;; `display-buffer-in-previous-window' can nevertheless find it. | |
4047 | (set-window-prev-buffers | |
4048 | window (append (window-prev-buffers window) (list entry)))) | |
9397e56f MR |
4049 | ;; Reset the quit-restore parameter. |
4050 | (set-window-parameter window 'quit-restore nil) | |
cf4eacfd MR |
4051 | ;; Select old window. |
4052 | (when (window-live-p (nth 2 quit-restore)) | |
4053 | (select-window (nth 2 quit-restore)))) | |
9397e56f | 4054 | (t |
cf4eacfd MR |
4055 | ;; Show some other buffer in WINDOW and reset the quit-restore |
4056 | ;; parameter. | |
9397e56f | 4057 | (set-window-parameter window 'quit-restore nil) |
b4d72fcf MR |
4058 | ;; Make sure that WINDOW is no more dedicated. |
4059 | (set-window-dedicated-p window nil) | |
78dd6ab1 MR |
4060 | (switch-to-prev-buffer window bury-or-kill))) |
4061 | ||
4062 | ;; Deal with the buffer. | |
4063 | (cond | |
4064 | ((not (buffer-live-p buffer))) | |
4065 | ((eq bury-or-kill 'kill) | |
4066 | (kill-buffer buffer)) | |
4067 | (bury-or-kill | |
4068 | (bury-buffer-internal buffer))))) | |
9397e56f | 4069 | |
78dd6ab1 MR |
4070 | (defun quit-window (&optional kill window) |
4071 | "Quit WINDOW and bury its buffer. | |
4072 | WINDOW must be a live window and defaults to the selected one. | |
4073 | With prefix argument KILL non-nil, kill the buffer instead of | |
4074 | burying it. | |
4075 | ||
4076 | According to information stored in WINDOW's `quit-restore' window | |
4077 | parameter either (1) delete WINDOW and its frame, (2) delete | |
4078 | WINDOW, (3) restore the buffer previously displayed in WINDOW, | |
4079 | or (4) make WINDOW display some other buffer than the present | |
4080 | one. If non-nil, reset `quit-restore' parameter to nil." | |
4081 | (interactive "P") | |
4082 | (quit-restore-window window (if kill 'kill 'bury))) | |
366ca7f3 MR |
4083 | |
4084 | (defun quit-windows-on (&optional buffer-or-name kill frame) | |
4085 | "Quit all windows showing BUFFER-OR-NAME. | |
4086 | BUFFER-OR-NAME may be a buffer or the name of an existing buffer | |
4087 | and defaults to the current buffer. Optional argument KILL | |
4088 | non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury | |
4089 | BUFFER-OR-NAME. Optional argument FRAME is handled as by | |
4090 | `delete-windows-on'. | |
4091 | ||
4092 | This function calls `quit-window' on all candidate windows | |
4093 | showing BUFFER-OR-NAME." | |
4094 | (interactive "BQuit windows on (buffer):\nP") | |
4095 | (let ((buffer (window-normalize-buffer buffer-or-name)) | |
4096 | ;; Handle the "inverted" meaning of the FRAME argument wrt other | |
4097 | ;; `window-list-1' based function. | |
4098 | (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame)))) | |
4099 | (dolist (window (window-list-1 nil nil all-frames)) | |
4100 | (if (eq (window-buffer window) buffer) | |
4101 | (quit-window kill window) | |
4102 | ;; If a window doesn't show BUFFER, unrecord BUFFER in it. | |
4103 | (unrecord-window-buffer window buffer))))) | |
562dd5e9 MR |
4104 | \f |
4105 | ;;; Splitting windows. | |
880e6158 | 4106 | (defun window-split-min-size (&optional horizontal pixelwise) |
562dd5e9 MR |
4107 | "Return minimum height of any window when splitting windows. |
4108 | Optional argument HORIZONTAL non-nil means return minimum width." | |
880e6158 MR |
4109 | (cond |
4110 | (pixelwise | |
4111 | (if horizontal | |
4112 | (window-min-pixel-width) | |
4113 | (window-min-pixel-height))) | |
4114 | (horizontal | |
4115 | (max window-min-width window-safe-min-width)) | |
4116 | (t | |
4117 | (max window-min-height window-safe-min-height)))) | |
562dd5e9 | 4118 | |
880e6158 | 4119 | (defun split-window (&optional window size side pixelwise) |
562dd5e9 | 4120 | "Make a new window adjacent to WINDOW. |
85c2386b | 4121 | WINDOW must be a valid window and defaults to the selected one. |
562dd5e9 MR |
4122 | Return the new window which is always a live window. |
4123 | ||
4124 | Optional argument SIZE a positive number means make WINDOW SIZE | |
4125 | lines or columns tall. If SIZE is negative, make the new window | |
4126 | -SIZE lines or columns tall. If and only if SIZE is non-nil, its | |
4127 | absolute value can be less than `window-min-height' or | |
4128 | `window-min-width'; so this command can make a new window as | |
4129 | small as one line or two columns. SIZE defaults to half of | |
0aa3616e | 4130 | WINDOW's size. |
562dd5e9 MR |
4131 | |
4132 | Optional third argument SIDE nil (or `below') specifies that the | |
4133 | new window shall be located below WINDOW. SIDE `above' means the | |
4134 | new window shall be located above WINDOW. In both cases SIZE | |
382c953b | 4135 | specifies the new number of lines for WINDOW (or the new window |
562dd5e9 MR |
4136 | if SIZE is negative) including space reserved for the mode and/or |
4137 | header line. | |
4138 | ||
4139 | SIDE t (or `right') specifies that the new window shall be | |
4140 | located on the right side of WINDOW. SIDE `left' means the new | |
4141 | window shall be located on the left of WINDOW. In both cases | |
382c953b | 4142 | SIZE specifies the new number of columns for WINDOW (or the new |
562dd5e9 MR |
4143 | window provided SIZE is negative) including space reserved for |
4144 | fringes and the scrollbar or a divider column. Any other non-nil | |
4145 | value for SIDE is currently handled like t (or `right'). | |
4146 | ||
f224e500 | 4147 | PIXELWISE, if non-nil, means to interpret SIZE pixelwise. |
880e6158 | 4148 | |
562dd5e9 MR |
4149 | If the variable `ignore-window-parameters' is non-nil or the |
4150 | `split-window' parameter of WINDOW equals t, do not process any | |
4151 | parameters of WINDOW. Otherwise, if the `split-window' parameter | |
4152 | of WINDOW specifies a function, call that function with all three | |
4153 | arguments and return the value returned by that function. | |
4154 | ||
4155 | Otherwise, if WINDOW is part of an atomic window, \"split\" the | |
4156 | root of that atomic window. The new window does not become a | |
4157 | member of that atomic window. | |
4158 | ||
4159 | If WINDOW is live, properties of the new window like margins and | |
4160 | scrollbars are inherited from WINDOW. If WINDOW is an internal | |
4161 | window, these properties as well as the buffer displayed in the | |
4162 | new window are inherited from the window selected on WINDOW's | |
4163 | frame. The selected window is not changed by this function." | |
447f16b8 | 4164 | (setq window (window-normalize-window window)) |
130e3e11 MR |
4165 | (let* ((side (cond |
4166 | ((not side) 'below) | |
4167 | ((memq side '(below above right left)) side) | |
4168 | (t 'right))) | |
caceae25 | 4169 | (horizontal (not (memq side '(below above)))) |
562dd5e9 MR |
4170 | (frame (window-frame window)) |
4171 | (parent (window-parent window)) | |
4172 | (function (window-parameter window 'split-window)) | |
4173 | (window-side (window-parameter window 'window-side)) | |
8e17c9ba MR |
4174 | ;; Rebind the following two variables since in some cases we |
4175 | ;; have to override their value. | |
b6f67890 | 4176 | (window-combination-limit window-combination-limit) |
caceae25 | 4177 | (window-combination-resize window-combination-resize) |
880e6158 MR |
4178 | (char-size (frame-char-size window horizontal)) |
4179 | (pixel-size | |
4180 | (when (numberp size) | |
4181 | (window--size-to-pixel window size horizontal pixelwise t))) | |
562dd5e9 | 4182 | atom-root) |
54f9154c | 4183 | (window--check frame) |
562dd5e9 MR |
4184 | (catch 'done |
4185 | (cond | |
4186 | ;; Ignore window parameters if either `ignore-window-parameters' | |
4187 | ;; is t or the `split-window' parameter equals t. | |
4188 | ((or ignore-window-parameters (eq function t))) | |
4189 | ((functionp function) | |
4190 | ;; The `split-window' parameter specifies the function to call. | |
4191 | ;; If that function is `ignore', do nothing. | |
4192 | (throw 'done (funcall function window size side))) | |
be7f5545 MR |
4193 | ;; If WINDOW is part of an atomic window, split the root window |
4194 | ;; of that atomic window instead. | |
562dd5e9 MR |
4195 | ((and (window-parameter window 'window-atom) |
4196 | (setq atom-root (window-atom-root window)) | |
4197 | (not (eq atom-root window))) | |
880e6158 | 4198 | (throw 'done (split-window atom-root size side pixelwise))) |
caceae25 MR |
4199 | ;; If WINDOW is a side window or its first or last child is a |
4200 | ;; side window, throw an error unless `window-combination-resize' | |
4201 | ;; equals 'side. | |
4202 | ((and (not (eq window-combination-resize 'side)) | |
4203 | (or (window-parameter window 'window-side) | |
4204 | (and (window-child window) | |
4205 | (or (window-parameter | |
4206 | (window-child window) 'window-side) | |
4207 | (window-parameter | |
4208 | (window-last-child window) 'window-side))))) | |
4209 | (error "Cannot split side window or parent of side window")) | |
4210 | ;; If `window-combination-resize' is 'side and window has a side | |
4211 | ;; window sibling, bind `window-combination-limit' to t. | |
4212 | ((and (not (eq window-combination-resize 'side)) | |
4213 | (or (and (window-prev-sibling window) | |
4214 | (window-parameter | |
4215 | (window-prev-sibling window) 'window-side)) | |
4216 | (and (window-next-sibling window) | |
4217 | (window-parameter | |
4218 | (window-next-sibling window) 'window-side)))) | |
4219 | (setq window-combination-limit t))) | |
4220 | ||
4221 | ;; If `window-combination-resize' is t and SIZE is non-negative, | |
4222 | ;; bind `window-combination-limit' to t. | |
880e6158 MR |
4223 | (when (and (eq window-combination-resize t) |
4224 | pixel-size (> pixel-size 0)) | |
b6f67890 | 4225 | (setq window-combination-limit t)) |
562dd5e9 | 4226 | |
880e6158 MR |
4227 | (let* ((parent-pixel-size |
4228 | ;; `parent-pixel-size' is the pixel size of WINDOW's | |
4229 | ;; parent, provided it has one. | |
4230 | (when parent (window-size parent horizontal t))) | |
562dd5e9 MR |
4231 | ;; `resize' non-nil means we are supposed to resize other |
4232 | ;; windows in WINDOW's combination. | |
4233 | (resize | |
caceae25 MR |
4234 | (and window-combination-resize |
4235 | (or (window-parameter window 'window-side) | |
4236 | (not (eq window-combination-resize 'side))) | |
8e17c9ba | 4237 | (not (eq window-combination-limit t)) |
562dd5e9 | 4238 | ;; Resize makes sense in iso-combinations only. |
3d8daefe | 4239 | (window-combined-p window horizontal))) |
880e6158 MR |
4240 | ;; `old-pixel-size' is the current pixel size of WINDOW. |
4241 | (old-pixel-size (window-size window horizontal t)) | |
562dd5e9 MR |
4242 | ;; `new-size' is the specified or calculated size of the |
4243 | ;; new window. | |
2d6b6005 | 4244 | new-pixel-size new-parent new-normal) |
880e6158 MR |
4245 | (cond |
4246 | ((not pixel-size) | |
4247 | (setq new-pixel-size | |
4248 | (if resize | |
4249 | ;; When resizing try to give the new window the | |
4250 | ;; average size of a window in its combination. | |
4251 | (min (- parent-pixel-size | |
4252 | (window-min-size parent horizontal nil t)) | |
4253 | (/ parent-pixel-size | |
4254 | (1+ (window-combinations parent horizontal)))) | |
4255 | ;; Else try to give the new window half the size | |
4256 | ;; of WINDOW (plus an eventual odd pixel). | |
4257 | (/ old-pixel-size 2))) | |
4258 | (unless window-resize-pixelwise | |
4259 | ;; Round to nearest char-size multiple. | |
4260 | (setq new-pixel-size | |
4261 | (* char-size (round new-pixel-size char-size))))) | |
4262 | ((>= pixel-size 0) | |
4263 | ;; SIZE non-negative specifies the new size of WINDOW. | |
4264 | ||
4265 | ;; Note: Specifying a non-negative SIZE is practically | |
4266 | ;; always done as workaround for making the new window | |
4267 | ;; appear above or on the left of the new window (the | |
4268 | ;; ispell window is a typical example of that). In all | |
4269 | ;; these cases the SIDE argument should be set to 'above | |
4270 | ;; or 'left in order to support the 'resize option. | |
4271 | ;; Here we have to nest the windows instead, see above. | |
4272 | (setq new-pixel-size (- old-pixel-size pixel-size))) | |
4273 | (t | |
4274 | ;; SIZE negative specifies the size of the new window. | |
4275 | (setq new-pixel-size (- pixel-size)))) | |
562dd5e9 MR |
4276 | |
4277 | ;; Check SIZE. | |
4278 | (cond | |
880e6158 | 4279 | ((not pixel-size) |
562dd5e9 MR |
4280 | (cond |
4281 | (resize | |
4282 | ;; SIZE unspecified, resizing. | |
880e6158 MR |
4283 | (when (and (not (window-sizable-p |
4284 | parent (- new-pixel-size) horizontal nil t)) | |
562dd5e9 | 4285 | ;; Try again with minimum split size. |
880e6158 MR |
4286 | (setq new-pixel-size |
4287 | (max new-pixel-size | |
4288 | (window-split-min-size horizontal t))) | |
4289 | (not (window-sizable-p | |
4290 | parent (- new-pixel-size) horizontal nil t))) | |
4291 | (error "Window %s too small for splitting 1" parent))) | |
4292 | ((> (+ new-pixel-size (window-min-size window horizontal nil t)) | |
4293 | old-pixel-size) | |
562dd5e9 | 4294 | ;; SIZE unspecified, no resizing. |
880e6158 MR |
4295 | (error "Window %s too small for splitting 2" window)))) |
4296 | ((and (>= pixel-size 0) | |
4297 | (or (>= pixel-size old-pixel-size) | |
4298 | (< new-pixel-size | |
4299 | (window-safe-min-pixel-size window horizontal)))) | |
562dd5e9 MR |
4300 | ;; SIZE specified as new size of old window. If the new size |
4301 | ;; is larger than the old size or the size of the new window | |
4302 | ;; would be less than the safe minimum, signal an error. | |
880e6158 | 4303 | (error "Window %s too small for splitting 3" window)) |
562dd5e9 MR |
4304 | (resize |
4305 | ;; SIZE specified, resizing. | |
880e6158 MR |
4306 | (unless (window-sizable-p |
4307 | parent (- new-pixel-size) horizontal nil t) | |
562dd5e9 | 4308 | ;; If we cannot resize the parent give up. |
880e6158 MR |
4309 | (error "Window %s too small for splitting 4" parent))) |
4310 | ((or (< new-pixel-size | |
4311 | (window-safe-min-pixel-size window horizontal)) | |
4312 | (< (- old-pixel-size new-pixel-size) | |
4313 | (window-safe-min-pixel-size window horizontal))) | |
562dd5e9 | 4314 | ;; SIZE specification violates minimum size restrictions. |
880e6158 | 4315 | (error "Window %s too small for splitting 5" window))) |
562dd5e9 | 4316 | |
5386012d | 4317 | (window--resize-reset frame horizontal) |
562dd5e9 MR |
4318 | |
4319 | (setq new-parent | |
4320 | ;; Make new-parent non-nil if we need a new parent window; | |
4321 | ;; either because we want to nest or because WINDOW is not | |
4322 | ;; iso-combined. | |
8e17c9ba | 4323 | (or (eq window-combination-limit t) |
b6f67890 | 4324 | (not (window-combined-p window horizontal)))) |
562dd5e9 MR |
4325 | (setq new-normal |
4326 | ;; Make new-normal the normal size of the new window. | |
4327 | (cond | |
880e6158 MR |
4328 | (pixel-size (/ (float new-pixel-size) |
4329 | (if new-parent old-pixel-size parent-pixel-size))) | |
562dd5e9 | 4330 | (new-parent 0.5) |
3d8daefe | 4331 | (resize (/ 1.0 (1+ (window-combinations parent horizontal)))) |
562dd5e9 MR |
4332 | (t (/ (window-normal-size window horizontal) 2.0)))) |
4333 | ||
4334 | (if resize | |
4335 | ;; Try to get space from OLD's siblings. We could go "up" and | |
4336 | ;; try getting additional space from surrounding windows but | |
4337 | ;; we won't be able to return space to those windows when we | |
4338 | ;; delete the one we create here. Hence we do not go up. | |
4339 | (progn | |
880e6158 MR |
4340 | (window--resize-child-windows |
4341 | parent (- new-pixel-size) horizontal) | |
562dd5e9 MR |
4342 | (let* ((normal (- 1.0 new-normal)) |
4343 | (sub (window-child parent))) | |
4344 | (while sub | |
4345 | (set-window-new-normal | |
4346 | sub (* (window-normal-size sub horizontal) normal)) | |
4347 | (setq sub (window-right sub))))) | |
4348 | ;; Get entire space from WINDOW. | |
880e6158 MR |
4349 | (set-window-new-pixel |
4350 | window (- old-pixel-size new-pixel-size)) | |
4351 | ;; (set-window-new-pixel window (- old-pixel-size new-pixel-size)) | |
4352 | ;; (set-window-new-total | |
4353 | ;; window (- old-size new-size)) | |
4354 | (window--resize-this-window window (- new-pixel-size) horizontal) | |
562dd5e9 MR |
4355 | (set-window-new-normal |
4356 | window (- (if new-parent 1.0 (window-normal-size window horizontal)) | |
4357 | new-normal))) | |
4358 | ||
880e6158 MR |
4359 | (let* ((new (split-window-internal window new-pixel-size side new-normal))) |
4360 | (window--pixel-to-total frame horizontal) | |
caceae25 | 4361 | ;; Assign window-side parameters, if any. |
c660a885 MR |
4362 | (cond |
4363 | ((eq window-combination-resize 'side) | |
caceae25 MR |
4364 | (let ((window-side |
4365 | (cond | |
4366 | (window-side window-side) | |
4367 | ((eq side 'above) 'top) | |
4368 | ((eq side 'below) 'bottom) | |
4369 | (t side)))) | |
4370 | ;; We made a new side window. | |
4371 | (set-window-parameter new 'window-side window-side) | |
4372 | (when (and new-parent (window-parameter window 'window-side)) | |
4373 | ;; We've been splitting a side root window. Give the | |
4374 | ;; new parent the same window-side parameter. | |
4375 | (set-window-parameter | |
4376 | (window-parent new) 'window-side window-side)))) | |
c660a885 MR |
4377 | ((eq window-combination-resize 'atom) |
4378 | ;; Make sure `window--check-frame' won't destroy an existing | |
4379 | ;; atomic window in case the new window gets nested inside. | |
4380 | (unless (window-parameter window 'window-atom) | |
4381 | (set-window-parameter window 'window-atom t)) | |
4382 | (when new-parent | |
4383 | (set-window-parameter (window-parent new) 'window-atom t)) | |
4384 | (set-window-parameter new 'window-atom t))) | |
562dd5e9 MR |
4385 | |
4386 | (run-window-configuration-change-hook frame) | |
880e6158 | 4387 | (run-window-scroll-functions new) |
54f9154c | 4388 | (window--check frame) |
562dd5e9 MR |
4389 | ;; Always return the new window. |
4390 | new))))) | |
4391 | ||
4392 | ;; I think this should be the default; I think people will prefer it--rms. | |
4393 | (defcustom split-window-keep-point t | |
2d197ffb CY |
4394 | "If non-nil, \\[split-window-below] preserves point in the new window. |
4395 | If nil, adjust point in the two windows to minimize redisplay. | |
4396 | This option applies only to `split-window-below' and functions | |
4397 | that call it. The low-level `split-window' function always keeps | |
4398 | the original point in both windows." | |
562dd5e9 MR |
4399 | :type 'boolean |
4400 | :group 'windows) | |
4401 | ||
2d197ffb CY |
4402 | (defun split-window-below (&optional size) |
4403 | "Split the selected window into two windows, one above the other. | |
4404 | The selected window is above. The newly split-off window is | |
4405 | below, and displays the same buffer. Return the new window. | |
4406 | ||
4407 | If optional argument SIZE is omitted or nil, both windows get the | |
4408 | same height, or close to it. If SIZE is positive, the upper | |
4409 | \(selected) window gets SIZE lines. If SIZE is negative, the | |
4410 | lower (new) window gets -SIZE lines. | |
4411 | ||
4412 | If the variable `split-window-keep-point' is non-nil, both | |
4413 | windows get the same value of point as the selected window. | |
4414 | Otherwise, the window starts are chosen so as to minimize the | |
4415 | amount of redisplay; this is convenient on slow terminals." | |
562dd5e9 MR |
4416 | (interactive "P") |
4417 | (let ((old-window (selected-window)) | |
5481664a | 4418 | (old-point (window-point)) |
562dd5e9 MR |
4419 | (size (and size (prefix-numeric-value size))) |
4420 | moved-by-window-height moved new-window bottom) | |
4421 | (when (and size (< size 0) (< (- size) window-min-height)) | |
4422 | ;; `split-window' would not signal an error here. | |
4423 | (error "Size of new window too small")) | |
4424 | (setq new-window (split-window nil size)) | |
4425 | (unless split-window-keep-point | |
4426 | (with-current-buffer (window-buffer) | |
c491fa41 MR |
4427 | ;; Use `save-excursion' around vertical movements below |
4428 | ;; (Bug#10971). Note: When the selected window's buffer has a | |
4429 | ;; header line, up to two lines of the buffer may not show up | |
4430 | ;; in the resulting configuration. | |
4431 | (save-excursion | |
4432 | (goto-char (window-start)) | |
4433 | (setq moved (vertical-motion (window-height))) | |
4434 | (set-window-start new-window (point)) | |
4435 | (when (> (point) (window-point new-window)) | |
4436 | (set-window-point new-window (point))) | |
4437 | (when (= moved (window-height)) | |
4438 | (setq moved-by-window-height t) | |
4439 | (vertical-motion -1)) | |
4440 | (setq bottom (point))) | |
4441 | (and moved-by-window-height | |
4442 | (<= bottom (point)) | |
5481664a | 4443 | (set-window-point old-window (1- bottom))) |
c491fa41 MR |
4444 | (and moved-by-window-height |
4445 | (<= (window-start new-window) old-point) | |
4446 | (set-window-point new-window old-point) | |
4447 | (select-window new-window)))) | |
9397e56f MR |
4448 | ;; Always copy quit-restore parameter in interactive use. |
4449 | (let ((quit-restore (window-parameter old-window 'quit-restore))) | |
4450 | (when quit-restore | |
4451 | (set-window-parameter new-window 'quit-restore quit-restore))) | |
4452 | new-window)) | |
562dd5e9 | 4453 | |
2d197ffb | 4454 | (defalias 'split-window-vertically 'split-window-below) |
562dd5e9 | 4455 | |
2d197ffb CY |
4456 | (defun split-window-right (&optional size) |
4457 | "Split the selected window into two side-by-side windows. | |
4458 | The selected window is on the left. The newly split-off window | |
4459 | is on the right, and displays the same buffer. Return the new | |
4460 | window. | |
562dd5e9 | 4461 | |
2d197ffb CY |
4462 | If optional argument SIZE is omitted or nil, both windows get the |
4463 | same width, or close to it. If SIZE is positive, the left-hand | |
4464 | \(selected) window gets SIZE columns. If SIZE is negative, the | |
4465 | right-hand (new) window gets -SIZE columns. Here, SIZE includes | |
4466 | the width of the window's scroll bar; if there are no scroll | |
4467 | bars, it includes the width of the divider column to the window's | |
4468 | right, if any." | |
562dd5e9 MR |
4469 | (interactive "P") |
4470 | (let ((old-window (selected-window)) | |
4471 | (size (and size (prefix-numeric-value size))) | |
4472 | new-window) | |
4473 | (when (and size (< size 0) (< (- size) window-min-width)) | |
4474 | ;; `split-window' would not signal an error here. | |
4475 | (error "Size of new window too small")) | |
9397e56f MR |
4476 | (setq new-window (split-window nil size t)) |
4477 | ;; Always copy quit-restore parameter in interactive use. | |
4478 | (let ((quit-restore (window-parameter old-window 'quit-restore))) | |
4479 | (when quit-restore | |
4480 | (set-window-parameter new-window 'quit-restore quit-restore))) | |
4481 | new-window)) | |
562dd5e9 | 4482 | |
2d197ffb | 4483 | (defalias 'split-window-horizontally 'split-window-right) |
562dd5e9 | 4484 | \f |
6198ccd0 MR |
4485 | ;;; Balancing windows. |
4486 | ||
4487 | ;; The following routine uses the recycled code from an old version of | |
be7f5545 MR |
4488 | ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the |
4489 | ;; new `window--resize-child-windows' code does would hardly make it any shorter or | |
6198ccd0 MR |
4490 | ;; more readable (FWIW we'd need three loops - one to calculate the |
4491 | ;; minimum sizes per window, one to enlarge or shrink windows until the | |
4492 | ;; new parent-size matches, and one where we shrink the largest/enlarge | |
4493 | ;; the smallest window). | |
4494 | (defun balance-windows-2 (window horizontal) | |
4495 | "Subroutine of `balance-windows-1'. | |
3d8daefe | 4496 | WINDOW must be a vertical combination (horizontal if HORIZONTAL |
85c2386b | 4497 | is non-nil)." |
880e6158 MR |
4498 | (let* ((char-size (if window-resize-pixelwise |
4499 | 1 | |
4500 | (frame-char-size window horizontal))) | |
4501 | (first (window-child window)) | |
6198ccd0 MR |
4502 | (sub first) |
4503 | (number-of-children 0) | |
880e6158 | 4504 | (parent-size (window-new-pixel window)) |
6198ccd0 | 4505 | (total-sum parent-size) |
cb882333 | 4506 | failed size sub-total sub-delta sub-amount rest) |
6198ccd0 MR |
4507 | (while sub |
4508 | (setq number-of-children (1+ number-of-children)) | |
4509 | (when (window-size-fixed-p sub horizontal) | |
4510 | (setq total-sum | |
880e6158 | 4511 | (- total-sum (window-size sub horizontal t))) |
6198ccd0 MR |
4512 | (set-window-new-normal sub 'ignore)) |
4513 | (setq sub (window-right sub))) | |
3c448ab6 | 4514 | |
6198ccd0 MR |
4515 | (setq failed t) |
4516 | (while (and failed (> number-of-children 0)) | |
4517 | (setq size (/ total-sum number-of-children)) | |
4518 | (setq failed nil) | |
4519 | (setq sub first) | |
4520 | (while (and sub (not failed)) | |
be7f5545 MR |
4521 | ;; Ignore child windows that should be ignored or are stuck. |
4522 | (unless (window--resize-child-windows-skip-p sub) | |
880e6158 | 4523 | (setq sub-total (window-size sub horizontal t)) |
6198ccd0 MR |
4524 | (setq sub-delta (- size sub-total)) |
4525 | (setq sub-amount | |
880e6158 | 4526 | (window-sizable sub sub-delta horizontal nil t)) |
be7f5545 | 4527 | ;; Register the new total size for this child window. |
880e6158 | 4528 | (set-window-new-pixel sub (+ sub-total sub-amount)) |
6198ccd0 MR |
4529 | (unless (= sub-amount sub-delta) |
4530 | (setq total-sum (- total-sum sub-total sub-amount)) | |
4531 | (setq number-of-children (1- number-of-children)) | |
4532 | ;; We failed and need a new round. | |
4533 | (setq failed t) | |
4534 | (set-window-new-normal sub 'skip))) | |
4535 | (setq sub (window-right sub)))) | |
3c448ab6 | 4536 | |
880e6158 | 4537 | ;; How can we be sure that `number-of-children' is NOT zero here ? |
6198ccd0 MR |
4538 | (setq rest (% total-sum number-of-children)) |
4539 | ;; Fix rounding by trying to enlarge non-stuck windows by one line | |
4540 | ;; (column) until `rest' is zero. | |
4541 | (setq sub first) | |
4542 | (while (and sub (> rest 0)) | |
be7f5545 | 4543 | (unless (window--resize-child-windows-skip-p window) |
880e6158 MR |
4544 | (set-window-new-pixel sub char-size t) |
4545 | (setq rest (- rest char-size))) | |
6198ccd0 | 4546 | (setq sub (window-right sub))) |
3c448ab6 | 4547 | |
6198ccd0 MR |
4548 | ;; Fix rounding by trying to enlarge stuck windows by one line |
4549 | ;; (column) until `rest' equals zero. | |
4550 | (setq sub first) | |
4551 | (while (and sub (> rest 0)) | |
4552 | (unless (eq (window-new-normal sub) 'ignore) | |
880e6158 MR |
4553 | (set-window-new-pixel sub char-size t) |
4554 | (setq rest (- rest char-size))) | |
6198ccd0 MR |
4555 | (setq sub (window-right sub))) |
4556 | ||
4557 | (setq sub first) | |
4558 | (while sub | |
4559 | ;; Record new normal sizes. | |
4560 | (set-window-new-normal | |
4561 | sub (/ (if (eq (window-new-normal sub) 'ignore) | |
880e6158 MR |
4562 | (window-size sub horizontal t) |
4563 | (window-new-pixel sub)) | |
6198ccd0 | 4564 | (float parent-size))) |
be7f5545 | 4565 | ;; Recursively balance each window's child windows. |
6198ccd0 MR |
4566 | (balance-windows-1 sub horizontal) |
4567 | (setq sub (window-right sub))))) | |
4568 | ||
4569 | (defun balance-windows-1 (window &optional horizontal) | |
4570 | "Subroutine of `balance-windows'." | |
4571 | (if (window-child window) | |
4572 | (let ((sub (window-child window))) | |
3d8daefe | 4573 | (if (window-combined-p sub horizontal) |
6198ccd0 | 4574 | (balance-windows-2 window horizontal) |
880e6158 | 4575 | (let ((size (window-new-pixel window))) |
6198ccd0 | 4576 | (while sub |
880e6158 | 4577 | (set-window-new-pixel sub size) |
6198ccd0 MR |
4578 | (balance-windows-1 sub horizontal) |
4579 | (setq sub (window-right sub)))))))) | |
4580 | ||
4581 | (defun balance-windows (&optional window-or-frame) | |
be7f5545 | 4582 | "Balance the sizes of windows of WINDOW-OR-FRAME. |
6198ccd0 MR |
4583 | WINDOW-OR-FRAME is optional and defaults to the selected frame. |
4584 | If WINDOW-OR-FRAME denotes a frame, balance the sizes of all | |
4c36be58 | 4585 | windows of that frame. If WINDOW-OR-FRAME denotes a window, |
be7f5545 | 4586 | recursively balance the sizes of all child windows of that |
6198ccd0 MR |
4587 | window." |
4588 | (interactive) | |
4589 | (let* ((window | |
4590 | (cond | |
4591 | ((or (not window-or-frame) | |
4592 | (frame-live-p window-or-frame)) | |
4593 | (frame-root-window window-or-frame)) | |
4594 | ((or (window-live-p window-or-frame) | |
4595 | (window-child window-or-frame)) | |
4596 | window-or-frame) | |
4597 | (t | |
4598 | (error "Not a window or frame %s" window-or-frame)))) | |
4599 | (frame (window-frame window))) | |
4600 | ;; Balance vertically. | |
5386012d | 4601 | (window--resize-reset (window-frame window)) |
6198ccd0 | 4602 | (balance-windows-1 window) |
880e6158 MR |
4603 | (when (window--resize-apply-p frame) |
4604 | (window-resize-apply frame) | |
4605 | (window--pixel-to-total frame) | |
4606 | (run-window-configuration-change-hook frame)) | |
6198ccd0 | 4607 | ;; Balance horizontally. |
5386012d | 4608 | (window--resize-reset (window-frame window) t) |
6198ccd0 | 4609 | (balance-windows-1 window t) |
880e6158 MR |
4610 | (when (window--resize-apply-p frame t) |
4611 | (window-resize-apply frame) | |
4612 | (window--pixel-to-total frame t) | |
4613 | (run-window-configuration-change-hook frame)))) | |
3c448ab6 MR |
4614 | |
4615 | (defun window-fixed-size-p (&optional window direction) | |
4616 | "Return t if WINDOW cannot be resized in DIRECTION. | |
4617 | WINDOW defaults to the selected window. DIRECTION can be | |
4618 | nil (i.e. any), `height' or `width'." | |
4619 | (with-current-buffer (window-buffer window) | |
4620 | (when (and (boundp 'window-size-fixed) window-size-fixed) | |
4621 | (not (and direction | |
4622 | (member (cons direction window-size-fixed) | |
4623 | '((height . width) (width . height)))))))) | |
4624 | ||
4625 | ;;; A different solution to balance-windows. | |
3c448ab6 MR |
4626 | (defvar window-area-factor 1 |
4627 | "Factor by which the window area should be over-estimated. | |
4628 | This is used by `balance-windows-area'. | |
4629 | Changing this globally has no effect.") | |
4630 | (make-variable-buffer-local 'window-area-factor) | |
4631 | ||
880e6158 | 4632 | (defun balance-windows-area-adjust (window delta horizontal pixelwise) |
d615d6d2 | 4633 | "Wrapper around `window-resize' with error checking. |
6198ccd0 | 4634 | Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function." |
d615d6d2 | 4635 | ;; `window-resize' may fail if delta is too large. |
6198ccd0 MR |
4636 | (while (>= (abs delta) 1) |
4637 | (condition-case nil | |
4638 | (progn | |
880e6158 MR |
4639 | ;; It was wrong to use `window-resize' here. Somehow |
4640 | ;; `balance-windows-area' depends on resizing windows | |
4641 | ;; asymmetrically. | |
4642 | (adjust-window-trailing-edge window delta horizontal pixelwise) | |
6198ccd0 MR |
4643 | (setq delta 0)) |
4644 | (error | |
4645 | ;;(message "adjust: %s" (error-message-string err)) | |
4646 | (setq delta (/ delta 2)))))) | |
4647 | ||
3c448ab6 MR |
4648 | (defun balance-windows-area () |
4649 | "Make all visible windows the same area (approximately). | |
4650 | See also `window-area-factor' to change the relative size of | |
4651 | specific buffers." | |
4652 | (interactive) | |
4653 | (let* ((unchanged 0) (carry 0) (round 0) | |
4654 | ;; Remove fixed-size windows. | |
4655 | (wins (delq nil (mapcar (lambda (win) | |
4656 | (if (not (window-fixed-size-p win)) win)) | |
4657 | (window-list nil 'nomini)))) | |
4658 | (changelog nil) | |
880e6158 MR |
4659 | (pixelwise window-resize-pixelwise) |
4660 | next) | |
3c448ab6 MR |
4661 | ;; Resizing a window changes the size of surrounding windows in complex |
4662 | ;; ways, so it's difficult to balance them all. The introduction of | |
4663 | ;; `adjust-window-trailing-edge' made it a bit easier, but it is still | |
4664 | ;; very difficult to do. `balance-window' above takes an off-line | |
4665 | ;; approach: get the whole window tree, then balance it, then try to | |
4666 | ;; adjust the windows so they fit the result. | |
4667 | ;; Here, instead, we take a "local optimization" approach, where we just | |
4668 | ;; go through all the windows several times until nothing needs to be | |
4669 | ;; changed. The main problem with this approach is that it's difficult | |
4670 | ;; to make sure it terminates, so we use some heuristic to try and break | |
4671 | ;; off infinite loops. | |
4672 | ;; After a round without any change, we allow a second, to give a chance | |
4673 | ;; to the carry to propagate a minor imbalance from the end back to | |
4674 | ;; the beginning. | |
4675 | (while (< unchanged 2) | |
4676 | ;; (message "New round") | |
4677 | (setq unchanged (1+ unchanged) round (1+ round)) | |
4678 | (dolist (win wins) | |
4679 | (setq next win) | |
4680 | (while (progn (setq next (next-window next)) | |
4681 | (window-fixed-size-p next))) | |
4682 | ;; (assert (eq next (or (cadr (member win wins)) (car wins)))) | |
4683 | (let* ((horiz | |
880e6158 | 4684 | (< (car (window-pixel-edges win)) (car (window-pixel-edges next)))) |
18cee9ec MR |
4685 | (areadiff (/ (- (* (window-size next nil pixelwise) |
4686 | (window-size next t pixelwise) | |
3c448ab6 MR |
4687 | (buffer-local-value 'window-area-factor |
4688 | (window-buffer next))) | |
18cee9ec MR |
4689 | (* (window-size win nil pixelwise) |
4690 | (window-size win t pixelwise) | |
3c448ab6 MR |
4691 | (buffer-local-value 'window-area-factor |
4692 | (window-buffer win)))) | |
4693 | (max (buffer-local-value 'window-area-factor | |
4694 | (window-buffer win)) | |
4695 | (buffer-local-value 'window-area-factor | |
4696 | (window-buffer next))))) | |
4697 | (edgesize (if horiz | |
18cee9ec MR |
4698 | (+ (window-size win nil pixelwise) |
4699 | (window-size next nil pixelwise)) | |
4700 | (+ (window-size win t pixelwise) | |
4701 | (window-size next t pixelwise)))) | |
3c448ab6 MR |
4702 | (diff (/ areadiff edgesize))) |
4703 | (when (zerop diff) | |
4704 | ;; Maybe diff is actually closer to 1 than to 0. | |
4705 | (setq diff (/ (* 3 areadiff) (* 2 edgesize)))) | |
4706 | (when (and (zerop diff) (not (zerop areadiff))) | |
4707 | (setq diff (/ (+ areadiff carry) edgesize)) | |
4708 | ;; Change things smoothly. | |
4709 | (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2)))) | |
4710 | (if (zerop diff) | |
4711 | ;; Make sure negligible differences don't accumulate to | |
4712 | ;; become significant. | |
4713 | (setq carry (+ carry areadiff)) | |
6198ccd0 | 4714 | ;; This used `adjust-window-trailing-edge' before and uses |
d615d6d2 | 4715 | ;; `window-resize' now. Error wrapping is still needed. |
880e6158 | 4716 | (balance-windows-area-adjust win diff horiz pixelwise) |
3c448ab6 | 4717 | ;; (sit-for 0.5) |
880e6158 | 4718 | (let ((change (cons win (window-pixel-edges win)))) |
3c448ab6 MR |
4719 | ;; If the same change has been seen already for this window, |
4720 | ;; we're most likely in an endless loop, so don't count it as | |
4721 | ;; a change. | |
4722 | (unless (member change changelog) | |
4723 | (push change changelog) | |
4724 | (setq unchanged 0 carry 0))))))) | |
4725 | ;; We've now basically balanced all the windows. | |
4726 | ;; But there may be some minor off-by-one imbalance left over, | |
4727 | ;; so let's do some fine tuning. | |
4728 | ;; (bw-finetune wins) | |
4729 | ;; (message "Done in %d rounds" round) | |
4730 | )) | |
9d89fec7 MR |
4731 | |
4732 | ;;; Window states, how to get them and how to put them in a window. | |
34a02f46 | 4733 | (defun window--state-get-1 (window &optional writable) |
9d89fec7 MR |
4734 | "Helper function for `window-state-get'." |
4735 | (let* ((type | |
4736 | (cond | |
d68443dc MR |
4737 | ((window-top-child window) 'vc) |
4738 | ((window-left-child window) 'hc) | |
9d89fec7 MR |
4739 | (t 'leaf))) |
4740 | (buffer (window-buffer window)) | |
4741 | (selected (eq window (selected-window))) | |
4742 | (head | |
4b0d61e3 SM |
4743 | `(,type |
4744 | ,@(unless (window-next-sibling window) `((last . t))) | |
880e6158 MR |
4745 | (pixel-width . ,(window-pixel-width window)) |
4746 | (pixel-height . ,(window-pixel-height window)) | |
4747 | (total-width . ,(window-total-width window)) | |
4748 | (total-height . ,(window-total-height window)) | |
4b0d61e3 SM |
4749 | (normal-height . ,(window-normal-size window)) |
4750 | (normal-width . ,(window-normal-size window t)) | |
8bbdea0f | 4751 | ,@(unless (window-live-p window) |
ab5340fe | 4752 | `((combination-limit . ,(window-combination-limit window)))) |
34a02f46 MR |
4753 | ,@(let ((parameters (window-parameters window)) |
4754 | list) | |
4755 | ;; Make copies of those window parameters whose | |
4756 | ;; persistence property is `writable' if WRITABLE is | |
4757 | ;; non-nil and non-nil if WRITABLE is nil. | |
4758 | (dolist (par parameters) | |
4759 | (let ((pers (cdr (assq (car par) | |
4760 | window-persistent-parameters)))) | |
4761 | (when (and pers (or (not writable) (eq pers 'writable))) | |
4762 | (setq list (cons (cons (car par) (cdr par)) list))))) | |
4763 | ;; Add `clone-of' parameter if necessary. | |
4764 | (let ((pers (cdr (assq 'clone-of | |
4765 | window-persistent-parameters)))) | |
4766 | (when (and pers (or (not writable) (eq pers 'writable)) | |
4767 | (not (assq 'clone-of list))) | |
4768 | (setq list (cons (cons 'clone-of window) list)))) | |
4b0d61e3 SM |
4769 | (when list |
4770 | `((parameters . ,list)))) | |
4771 | ,@(when buffer | |
1edf595d | 4772 | ;; All buffer related things go in here. |
5481664a | 4773 | (let ((point (window-point window)) |
1edf595d MR |
4774 | (start (window-start window))) |
4775 | `((buffer | |
4776 | ,(buffer-name buffer) | |
4777 | (selected . ,selected) | |
4778 | (hscroll . ,(window-hscroll window)) | |
4779 | (fringes . ,(window-fringes window)) | |
4780 | (margins . ,(window-margins window)) | |
4781 | (scroll-bars . ,(window-scroll-bars window)) | |
4782 | (vscroll . ,(window-vscroll window)) | |
4783 | (dedicated . ,(window-dedicated-p window)) | |
de8c03dc SM |
4784 | (point . ,(if writable point |
4785 | (copy-marker point | |
4786 | (buffer-local-value | |
4787 | 'window-point-insertion-type | |
4788 | buffer)))) | |
1edf595d | 4789 | (start . ,(if writable start (copy-marker start))))))))) |
9d89fec7 MR |
4790 | (tail |
4791 | (when (memq type '(vc hc)) | |
4792 | (let (list) | |
4793 | (setq window (window-child window)) | |
4794 | (while window | |
34a02f46 | 4795 | (setq list (cons (window--state-get-1 window writable) list)) |
9d89fec7 MR |
4796 | (setq window (window-right window))) |
4797 | (nreverse list))))) | |
4798 | (append head tail))) | |
4799 | ||
34a02f46 | 4800 | (defun window-state-get (&optional window writable) |
9d89fec7 MR |
4801 | "Return state of WINDOW as a Lisp object. |
4802 | WINDOW can be any window and defaults to the root window of the | |
4803 | selected frame. | |
4804 | ||
34a02f46 MR |
4805 | Optional argument WRITABLE non-nil means do not use markers for |
4806 | sampling `window-point' and `window-start'. Together, WRITABLE | |
4807 | and the variable `window-persistent-parameters' specify which | |
4808 | window parameters are saved by this function. WRITABLE should be | |
4809 | non-nil when the return value shall be written to a file and read | |
4810 | back in another session. Otherwise, an application may run into | |
4811 | an `invalid-read-syntax' error while attempting to read back the | |
4812 | value from file. | |
9d89fec7 MR |
4813 | |
4814 | The return value can be used as argument for `window-state-put' | |
4815 | to put the state recorded here into an arbitrary window. The | |
4816 | value can be also stored on disk and read back in a new session." | |
4817 | (setq window | |
4818 | (if window | |
24300f5f | 4819 | (if (window-valid-p window) |
9d89fec7 MR |
4820 | window |
4821 | (error "%s is not a live or internal window" window)) | |
4822 | (frame-root-window))) | |
4823 | ;; The return value is a cons whose car specifies some constraints on | |
be7f5545 MR |
4824 | ;; the size of WINDOW. The cdr lists the states of the child windows |
4825 | ;; of WINDOW. | |
9d89fec7 MR |
4826 | (cons |
4827 | ;; Frame related things would go into a function, say `frame-state', | |
4828 | ;; calling `window-state-get' to insert the frame's root window. | |
4b0d61e3 SM |
4829 | `((min-height . ,(window-min-size window)) |
4830 | (min-width . ,(window-min-size window t)) | |
4831 | (min-height-ignore . ,(window-min-size window nil t)) | |
4832 | (min-width-ignore . ,(window-min-size window t t)) | |
4833 | (min-height-safe . ,(window-min-size window nil 'safe)) | |
880e6158 MR |
4834 | (min-width-safe . ,(window-min-size window t 'safe)) |
4835 | (min-pixel-height . ,(window-min-size window nil nil t)) | |
4836 | (min-pixel-width . ,(window-min-size window t nil t)) | |
4837 | (min-pixel-height-ignore . ,(window-min-size window nil t t)) | |
4838 | (min-pixel-width-ignore . ,(window-min-size window t t t)) | |
4839 | (min-pixel-height-safe . ,(window-min-size window nil 'safe t)) | |
4840 | (min-pixel-width-safe . ,(window-min-size window t 'safe t))) | |
34a02f46 | 4841 | (window--state-get-1 window writable))) |
9d89fec7 MR |
4842 | |
4843 | (defvar window-state-put-list nil | |
4844 | "Helper variable for `window-state-put'.") | |
4845 | ||
34ada5f4 MR |
4846 | (defvar window-state-put-stale-windows nil |
4847 | "Helper variable for `window-state-put'.") | |
4848 | ||
880e6158 | 4849 | (defun window--state-put-1 (state &optional window ignore totals pixelwise) |
9d89fec7 MR |
4850 | "Helper function for `window-state-put'." |
4851 | (let ((type (car state))) | |
4852 | (setq state (cdr state)) | |
4853 | (cond | |
4854 | ((eq type 'leaf) | |
4855 | ;; For a leaf window just add unprocessed entries to | |
4856 | ;; `window-state-put-list'. | |
c7635a97 | 4857 | (push (cons window state) window-state-put-list)) |
9d89fec7 MR |
4858 | ((memq type '(vc hc)) |
4859 | (let* ((horizontal (eq type 'hc)) | |
880e6158 | 4860 | (total (window-size window horizontal pixelwise)) |
9d89fec7 MR |
4861 | (first t) |
4862 | size new) | |
4863 | (dolist (item state) | |
4864 | ;; Find the next child window. WINDOW always points to the | |
4865 | ;; real window that we want to fill with what we find here. | |
4866 | (when (memq (car item) '(leaf vc hc)) | |
4867 | (if (assq 'last item) | |
c56cad4a | 4868 | ;; The last child window. Below `window--state-put-1' |
9d89fec7 MR |
4869 | ;; will put into it whatever ITEM has in store. |
4870 | (setq new nil) | |
4871 | ;; Not the last child window, prepare for splitting | |
4872 | ;; WINDOW. SIZE is the new (and final) size of the old | |
4873 | ;; window. | |
4874 | (setq size | |
4875 | (if totals | |
4876 | ;; Use total size. | |
880e6158 MR |
4877 | (if pixelwise |
4878 | (cdr (assq (if horizontal | |
4879 | 'pixel-width | |
4880 | 'pixel-height) | |
4881 | item)) | |
4882 | (cdr (assq (if horizontal | |
4883 | 'total-width | |
4884 | 'total-height) | |
4885 | item))) | |
9d89fec7 | 4886 | ;; Use normalized size and round. |
880e6158 MR |
4887 | (round |
4888 | (* total | |
4889 | (cdr (assq (if horizontal 'normal-width 'normal-height) | |
4890 | item)))))) | |
9d89fec7 MR |
4891 | |
4892 | ;; Use safe sizes, we try to resize later. | |
880e6158 MR |
4893 | (setq size (max size |
4894 | (if horizontal | |
4895 | (* window-safe-min-width | |
4896 | (if pixelwise | |
4897 | (frame-char-width (window-frame window)) | |
4898 | 1)) | |
4899 | (* window-safe-min-height | |
4900 | (if pixelwise | |
4901 | (frame-char-height (window-frame window)) | |
4902 | 1))))) | |
4903 | (if (window-sizable-p window (- size) horizontal 'safe pixelwise) | |
b6f67890 MR |
4904 | (let* ((window-combination-limit |
4905 | (assq 'combination-limit item))) | |
301b181a | 4906 | ;; We must inherit the combination limit, otherwise |
b6f67890 MR |
4907 | ;; we might mess up handling of atomic and side |
4908 | ;; window. | |
880e6158 | 4909 | (setq new (split-window window size horizontal pixelwise))) |
9d89fec7 MR |
4910 | ;; Give up if we can't resize window down to safe sizes. |
4911 | (error "Cannot resize window %s" window)) | |
4912 | ||
4913 | (when first | |
4914 | (setq first nil) | |
4915 | ;; When creating the first child window add for parent | |
4916 | ;; unprocessed entries to `window-state-put-list'. | |
4917 | (setq window-state-put-list | |
4918 | (cons (cons (window-parent window) state) | |
4919 | window-state-put-list)))) | |
4920 | ||
4921 | ;; Now process the current window (either the one we've just | |
4922 | ;; split or the last child of its parent). | |
c56cad4a | 4923 | (window--state-put-1 item window ignore totals) |
9d89fec7 MR |
4924 | ;; Continue with the last window split off. |
4925 | (setq window new)))))))) | |
4926 | ||
880e6158 | 4927 | (defun window--state-put-2 (ignore pixelwise) |
9d89fec7 MR |
4928 | "Helper function for `window-state-put'." |
4929 | (dolist (item window-state-put-list) | |
4930 | (let ((window (car item)) | |
b6f67890 | 4931 | (combination-limit (cdr (assq 'combination-limit item))) |
9d89fec7 MR |
4932 | (parameters (cdr (assq 'parameters item))) |
4933 | (state (cdr (assq 'buffer item)))) | |
b6f67890 MR |
4934 | (when combination-limit |
4935 | (set-window-combination-limit window combination-limit)) | |
34a02f46 MR |
4936 | ;; Reset window's parameters and assign saved ones (we might want |
4937 | ;; a `remove-window-parameters' function here). | |
4938 | (dolist (parameter (window-parameters window)) | |
4939 | (set-window-parameter window (car parameter) nil)) | |
9d89fec7 MR |
4940 | (when parameters |
4941 | (dolist (parameter parameters) | |
34a02f46 | 4942 | (set-window-parameter window (car parameter) (cdr parameter)))) |
9d89fec7 MR |
4943 | ;; Process buffer related state. |
4944 | (when state | |
34ada5f4 MR |
4945 | (let ((buffer (get-buffer (car state)))) |
4946 | (if buffer | |
c089653d MR |
4947 | (with-current-buffer buffer |
4948 | (set-window-buffer window buffer) | |
4949 | (set-window-hscroll window (cdr (assq 'hscroll state))) | |
4950 | (apply 'set-window-fringes | |
4951 | (cons window (cdr (assq 'fringes state)))) | |
4952 | (let ((margins (cdr (assq 'margins state)))) | |
4953 | (set-window-margins window (car margins) (cdr margins))) | |
4954 | (let ((scroll-bars (cdr (assq 'scroll-bars state)))) | |
4955 | (set-window-scroll-bars | |
4956 | window (car scroll-bars) (nth 2 scroll-bars) | |
4957 | (nth 3 scroll-bars))) | |
4958 | (set-window-vscroll window (cdr (assq 'vscroll state))) | |
4959 | ;; Adjust vertically. | |
4960 | (if (memq window-size-fixed '(t height)) | |
4961 | ;; A fixed height window, try to restore the | |
4962 | ;; original size. | |
880e6158 MR |
4963 | (let ((delta |
4964 | (- (cdr (assq | |
4965 | (if pixelwise 'pixel-height 'total-height) | |
4966 | item)) | |
4967 | (window-size window nil pixelwise))) | |
c089653d | 4968 | window-size-fixed) |
880e6158 MR |
4969 | (when (window--resizable-p |
4970 | window delta nil nil nil nil nil pixelwise) | |
4971 | (window-resize window delta nil nil pixelwise))) | |
c089653d | 4972 | ;; Else check whether the window is not high enough. |
880e6158 MR |
4973 | (let* ((min-size |
4974 | (window-min-size window nil ignore pixelwise)) | |
4975 | (delta | |
4976 | (- min-size (window-size window nil pixelwise)))) | |
c089653d | 4977 | (when (and (> delta 0) |
880e6158 MR |
4978 | (window--resizable-p |
4979 | window delta nil ignore nil nil nil pixelwise)) | |
4980 | (window-resize window delta nil ignore pixelwise)))) | |
c089653d MR |
4981 | ;; Adjust horizontally. |
4982 | (if (memq window-size-fixed '(t width)) | |
4983 | ;; A fixed width window, try to restore the original | |
4984 | ;; size. | |
880e6158 MR |
4985 | (let ((delta |
4986 | (- (cdr (assq | |
4987 | (if pixelwise 'pixel-width 'total-width) | |
4988 | item)) | |
4989 | (window-size window t pixelwise))) | |
c089653d | 4990 | window-size-fixed) |
880e6158 MR |
4991 | (when (window--resizable-p |
4992 | window delta nil nil nil nil nil pixelwise) | |
4993 | (window-resize window delta nil nil pixelwise))) | |
c089653d | 4994 | ;; Else check whether the window is not wide enough. |
880e6158 MR |
4995 | (let* ((min-size (window-min-size window t ignore pixelwise)) |
4996 | (delta (- min-size (window-size window t pixelwise)))) | |
c089653d | 4997 | (when (and (> delta 0) |
880e6158 MR |
4998 | (window--resizable-p |
4999 | window delta t ignore nil nil nil pixelwise)) | |
5000 | (window-resize window delta t ignore pixelwise)))) | |
c089653d MR |
5001 | ;; Set dedicated status. |
5002 | (set-window-dedicated-p window (cdr (assq 'dedicated state))) | |
5003 | ;; Install positions (maybe we should do this after all | |
5004 | ;; windows have been created and sized). | |
5005 | (ignore-errors | |
5006 | (set-window-start window (cdr (assq 'start state))) | |
5007 | (set-window-point window (cdr (assq 'point state)))) | |
5008 | ;; Select window if it's the selected one. | |
5009 | (when (cdr (assq 'selected state)) | |
5010 | (select-window window))) | |
5011 | ;; We don't want to raise an error in case the buffer does | |
5012 | ;; not exist anymore, so we switch to a previous one and | |
5013 | ;; save the window with the intention of deleting it later | |
5014 | ;; if possible. | |
34ada5f4 | 5015 | (switch-to-prev-buffer window) |
c089653d | 5016 | (push window window-state-put-stale-windows))))))) |
9d89fec7 MR |
5017 | |
5018 | (defun window-state-put (state &optional window ignore) | |
5019 | "Put window state STATE into WINDOW. | |
5020 | STATE should be the state of a window returned by an earlier | |
5021 | invocation of `window-state-get'. Optional argument WINDOW must | |
5022 | specify a live window and defaults to the selected one. | |
5023 | ||
5024 | Optional argument IGNORE non-nil means ignore minimum window | |
5025 | sizes and fixed size restrictions. IGNORE equal `safe' means | |
be7f5545 | 5026 | windows can get as small as `window-safe-min-height' and |
9d89fec7 | 5027 | `window-safe-min-width'." |
34ada5f4 | 5028 | (setq window-state-put-stale-windows nil) |
447f16b8 | 5029 | (setq window (window-normalize-window window t)) |
9d89fec7 MR |
5030 | (let* ((frame (window-frame window)) |
5031 | (head (car state)) | |
5032 | ;; We check here (1) whether the total sizes of root window of | |
5033 | ;; STATE and that of WINDOW are equal so we can avoid | |
5034 | ;; calculating new sizes, and (2) if we do have to resize | |
5035 | ;; whether we can do so without violating size restrictions. | |
880e6158 MR |
5036 | (pixelwise (and (cdr (assq 'pixel-width state)) |
5037 | (cdr (assq 'pixel-height state)))) | |
5038 | (totals (or (and pixelwise | |
5039 | (= (window-pixel-width window) | |
5040 | (cdr (assq 'pixel-width state))) | |
5041 | (= (window-pixel-height window) | |
5042 | (cdr (assq 'pixel-height state)))) | |
5043 | (and (= (window-total-width window) | |
5044 | (cdr (assq 'total-width state))) | |
5045 | (= (window-total-height window) | |
5046 | (cdr (assq 'total-height state)))))) | |
5047 | (min-height (cdr (assq | |
5048 | (if pixelwise 'min-pixel-height 'min-height) | |
5049 | head))) | |
5050 | (min-width (cdr (assq | |
5051 | (if pixelwise 'min-pixel-width 'min-weight) | |
5052 | head)))) | |
9d89fec7 | 5053 | (if (and (not totals) |
880e6158 MR |
5054 | (or (> min-height (window-size window nil pixelwise)) |
5055 | (> min-width (window-size window t pixelwise))) | |
9d89fec7 MR |
5056 | (or (not ignore) |
5057 | (and (setq min-height | |
880e6158 MR |
5058 | (cdr (assq |
5059 | (if pixelwise | |
5060 | 'min-pixel-height-ignore | |
5061 | 'min-height-ignore) | |
5062 | head))) | |
9d89fec7 | 5063 | (setq min-width |
880e6158 MR |
5064 | (cdr (assq |
5065 | (if pixelwise | |
5066 | 'min-pixel-width-ignore | |
5067 | 'min-width-ignore) | |
5068 | head))) | |
5069 | (or (> min-height | |
5070 | (window-size window nil pixelwise)) | |
5071 | (> min-width | |
5072 | (window-size window t pixelwise))) | |
9d89fec7 MR |
5073 | (or (not (eq ignore 'safe)) |
5074 | (and (setq min-height | |
880e6158 MR |
5075 | (cdr (assq |
5076 | (if pixelwise | |
5077 | 'min-pixel-height-safe | |
5078 | 'min-height-safe) | |
5079 | head))) | |
9d89fec7 | 5080 | (setq min-width |
880e6158 MR |
5081 | (cdr (assq |
5082 | (if pixelwise | |
5083 | 'min-pixel-width-safe | |
5084 | 'min-width-safe) | |
5085 | head))) | |
9d89fec7 | 5086 | (or (> min-height |
880e6158 | 5087 | (window-size window nil pixelwise)) |
9d89fec7 | 5088 | (> min-width |
880e6158 | 5089 | (window-size window t pixelwise)))))))) |
9d89fec7 MR |
5090 | ;; The check above might not catch all errors due to rounding |
5091 | ;; issues - so IGNORE equal 'safe might not always produce the | |
5092 | ;; minimum possible state. But such configurations hardly make | |
5093 | ;; sense anyway. | |
a91adc7e | 5094 | (error "Window %s too small to accommodate state" window) |
9d89fec7 MR |
5095 | (setq state (cdr state)) |
5096 | (setq window-state-put-list nil) | |
5097 | ;; Work on the windows of a temporary buffer to make sure that | |
5098 | ;; splitting proceeds regardless of any buffer local values of | |
5099 | ;; `window-size-fixed'. Release that buffer after the buffers of | |
c56cad4a | 5100 | ;; all live windows have been set by `window--state-put-2'. |
9d89fec7 MR |
5101 | (with-temp-buffer |
5102 | (set-window-buffer window (current-buffer)) | |
880e6158 MR |
5103 | (window--state-put-1 state window nil totals pixelwise) |
5104 | (window--state-put-2 ignore pixelwise)) | |
34ada5f4 MR |
5105 | (while window-state-put-stale-windows |
5106 | (let ((window (pop window-state-put-stale-windows))) | |
5107 | (when (eq (window-deletable-p window) t) | |
5108 | (delete-window window)))) | |
54f9154c | 5109 | (window--check frame)))) |
9481c002 | 5110 | \f |
f818cd2a MR |
5111 | (defun display-buffer-record-window (type window buffer) |
5112 | "Record information for window used by `display-buffer'. | |
cf4eacfd | 5113 | TYPE specifies the type of the calling operation and must be one |
382c953b JB |
5114 | of the symbols 'reuse (when WINDOW existed already and was |
5115 | reused for displaying BUFFER), 'window (when WINDOW was created | |
5116 | on an already existing frame), or 'frame (when WINDOW was | |
5117 | created on a new frame). WINDOW is the window used for or created | |
cf4eacfd MR |
5118 | by the `display-buffer' routines. BUFFER is the buffer that |
5119 | shall be displayed. | |
5120 | ||
5121 | This function installs or updates the quit-restore parameter of | |
5122 | WINDOW. The quit-restore parameter is a list of four elements: | |
5123 | The first element is one of the symbols 'window, 'frame, 'same or | |
5124 | 'other. The second element is either one of the symbols 'window | |
5125 | or 'frame or a list whose elements are the buffer previously | |
5126 | shown in the window, that buffer's window start and window point, | |
5127 | and the window's height. The third element is the window | |
5128 | selected at the time the parameter was created. The fourth | |
5129 | element is BUFFER." | |
f818cd2a | 5130 | (cond |
cf4eacfd MR |
5131 | ((eq type 'reuse) |
5132 | (if (eq (window-buffer window) buffer) | |
5133 | ;; WINDOW shows BUFFER already. | |
5134 | (when (consp (window-parameter window 'quit-restore)) | |
5135 | ;; If WINDOW has a quit-restore parameter, reset its car. | |
5136 | (setcar (window-parameter window 'quit-restore) 'same)) | |
5137 | ;; WINDOW shows another buffer. | |
92346275 MR |
5138 | (with-current-buffer (window-buffer window) |
5139 | (set-window-parameter | |
5140 | window 'quit-restore | |
5141 | (list 'other | |
5142 | ;; A quadruple of WINDOW's buffer, start, point and height. | |
5143 | (list (current-buffer) (window-start window) | |
5144 | ;; Preserve window-point-insertion-type (Bug#12588). | |
5145 | (copy-marker | |
5146 | (window-point window) window-point-insertion-type) | |
880e6158 | 5147 | (window-total-height window)) |
92346275 | 5148 | (selected-window) buffer))))) |
cf4eacfd MR |
5149 | ((eq type 'window) |
5150 | ;; WINDOW has been created on an existing frame. | |
f818cd2a | 5151 | (set-window-parameter |
cf4eacfd MR |
5152 | window 'quit-restore |
5153 | (list 'window 'window (selected-window) buffer))) | |
5154 | ((eq type 'frame) | |
5155 | ;; WINDOW has been created on a new frame. | |
f818cd2a | 5156 | (set-window-parameter |
cf4eacfd MR |
5157 | window 'quit-restore |
5158 | (list 'frame 'frame (selected-window) buffer))))) | |
9481c002 | 5159 | |
f818cd2a MR |
5160 | (defcustom display-buffer-function nil |
5161 | "If non-nil, function to call to handle `display-buffer'. | |
5162 | It will receive two args, the buffer and a flag which if non-nil | |
5163 | means that the currently selected window is not acceptable. It | |
5164 | should choose or create a window, display the specified buffer in | |
5165 | it, and return the window. | |
5166 | ||
e1c2c6f2 MR |
5167 | The specified function should call `display-buffer-record-window' |
5168 | with corresponding arguments to set up the quit-restore parameter | |
5169 | of the window used." | |
f818cd2a MR |
5170 | :type '(choice |
5171 | (const nil) | |
5172 | (function :tag "function")) | |
3c448ab6 | 5173 | :group 'windows) |
9481c002 | 5174 | |
71ce58e7 CY |
5175 | (make-obsolete-variable 'display-buffer-function |
5176 | 'display-buffer-alist "24.3") | |
5177 | ||
d97af5a0 CY |
5178 | ;; Eventually, we want to turn this into a defvar; instead of |
5179 | ;; customizing this, the user should use a `pop-up-frame-parameters' | |
5180 | ;; alist entry in `display-buffer-base-action'. | |
f818cd2a MR |
5181 | (defcustom pop-up-frame-alist nil |
5182 | "Alist of parameters for automatically generated new frames. | |
f818cd2a MR |
5183 | If non-nil, the value you specify here is used by the default |
5184 | `pop-up-frame-function' for the creation of new frames. | |
9481c002 | 5185 | |
f818cd2a MR |
5186 | Since `pop-up-frame-function' is used by `display-buffer' for |
5187 | making new frames, any value specified here by default affects | |
5188 | the automatic generation of new frames via `display-buffer' and | |
5189 | all functions based on it. The behavior of `make-frame' is not | |
5190 | affected by this variable." | |
9481c002 | 5191 | :type '(repeat (cons :format "%v" |
f818cd2a MR |
5192 | (symbol :tag "Parameter") |
5193 | (sexp :tag "Value"))) | |
9481c002 | 5194 | :group 'frames) |
9481c002 | 5195 | |
f818cd2a MR |
5196 | (defcustom pop-up-frame-function |
5197 | (lambda () (make-frame pop-up-frame-alist)) | |
5198 | "Function used by `display-buffer' for creating a new frame. | |
5199 | This function is called with no arguments and should return a new | |
5200 | frame. The default value calls `make-frame' with the argument | |
5201 | `pop-up-frame-alist'." | |
9481c002 | 5202 | :type 'function |
9481c002 | 5203 | :group 'frames) |
3c448ab6 | 5204 | |
56f31926 MR |
5205 | (defcustom special-display-buffer-names nil |
5206 | "List of names of buffers that should be displayed specially. | |
5207 | Displaying a buffer with `display-buffer' or `pop-to-buffer', if | |
5208 | its name is in this list, displays the buffer in a way specified | |
5209 | by `special-display-function'. `special-display-popup-frame' | |
5210 | \(the default for `special-display-function') usually displays | |
5211 | the buffer in a separate frame made with the parameters specified | |
5212 | by `special-display-frame-alist'. If `special-display-function' | |
5213 | has been set to some other function, that function is called with | |
5214 | the buffer as first, and nil as second argument. | |
5215 | ||
5216 | Alternatively, an element of this list can be specified as | |
5217 | \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer | |
382c953b | 5218 | name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs. |
56f31926 MR |
5219 | `special-display-popup-frame' will interpret such pairs as frame |
5220 | parameters when it creates a special frame, overriding the | |
5221 | corresponding values from `special-display-frame-alist'. | |
5222 | ||
5223 | As a special case, if FRAME-PARAMETERS contains (same-window . t) | |
5224 | `special-display-popup-frame' displays that buffer in the | |
5225 | selected window. If FRAME-PARAMETERS contains (same-frame . t), | |
5226 | it displays that buffer in a window on the selected frame. | |
5227 | ||
5228 | If `special-display-function' specifies some other function than | |
5229 | `special-display-popup-frame', that function is called with the | |
5230 | buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second | |
5231 | argument. | |
5232 | ||
5233 | Finally, an element of this list can be also specified as | |
5234 | \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case, | |
5235 | `special-display-popup-frame' will call FUNCTION with the buffer | |
5236 | named BUFFER-NAME as first argument, and OTHER-ARGS as the | |
0563dae9 MR |
5237 | second. |
5238 | ||
5239 | Any alternative function specified here is responsible for | |
5240 | setting up the quit-restore parameter of the window used. | |
56f31926 MR |
5241 | |
5242 | If this variable appears \"not to work\", because you added a | |
5243 | name to it but the corresponding buffer is displayed in the | |
5244 | selected window, look at the values of `same-window-buffer-names' | |
5245 | and `same-window-regexps'. Those variables take precedence over | |
5246 | this one. | |
5247 | ||
5248 | See also `special-display-regexps'." | |
5249 | :type '(repeat | |
5250 | (choice :tag "Buffer" | |
5251 | :value "" | |
5252 | (string :format "%v") | |
5253 | (cons :tag "With parameters" | |
5254 | :format "%v" | |
5255 | :value ("" . nil) | |
5256 | (string :format "%v") | |
5257 | (repeat :tag "Parameters" | |
5258 | (cons :format "%v" | |
5259 | (symbol :tag "Parameter") | |
5260 | (sexp :tag "Value")))) | |
5261 | (list :tag "With function" | |
5262 | :format "%v" | |
5263 | :value ("" . nil) | |
5264 | (string :format "%v") | |
5265 | (function :tag "Function") | |
5266 | (repeat :tag "Arguments" (sexp))))) | |
5267 | :group 'windows | |
5268 | :group 'frames) | |
77f1f99c | 5269 | (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3") |
ac549fa5 GM |
5270 | (put 'special-display-buffer-names 'risky-local-variable t) |
5271 | ||
56f31926 MR |
5272 | (defcustom special-display-regexps nil |
5273 | "List of regexps saying which buffers should be displayed specially. | |
5274 | Displaying a buffer with `display-buffer' or `pop-to-buffer', if | |
5275 | any regexp in this list matches its name, displays it specially | |
5276 | using `special-display-function'. `special-display-popup-frame' | |
5277 | \(the default for `special-display-function') usually displays | |
5278 | the buffer in a separate frame made with the parameters specified | |
5279 | by `special-display-frame-alist'. If `special-display-function' | |
5280 | has been set to some other function, that function is called with | |
5281 | the buffer as first, and nil as second argument. | |
5282 | ||
5283 | Alternatively, an element of this list can be specified as | |
5284 | \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and | |
5285 | FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs. | |
5286 | `special-display-popup-frame' will then interpret these pairs as | |
5287 | frame parameters when creating a special frame for a buffer whose | |
5288 | name matches REGEXP, overriding the corresponding values from | |
5289 | `special-display-frame-alist'. | |
5290 | ||
5291 | As a special case, if FRAME-PARAMETERS contains (same-window . t) | |
5292 | `special-display-popup-frame' displays buffers matching REGEXP in | |
382c953b | 5293 | the selected window. (same-frame . t) in FRAME-PARAMETERS means |
56f31926 MR |
5294 | to display such buffers in a window on the selected frame. |
5295 | ||
5296 | If `special-display-function' specifies some other function than | |
5297 | `special-display-popup-frame', that function is called with the | |
5298 | buffer whose name matched REGEXP as first, and FRAME-PARAMETERS | |
5299 | as second argument. | |
5300 | ||
5301 | Finally, an element of this list can be also specified as | |
5302 | \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame' | |
5303 | will then call FUNCTION with the buffer whose name matched | |
0563dae9 MR |
5304 | REGEXP as first, and OTHER-ARGS as second argument. |
5305 | ||
5306 | Any alternative function specified here is responsible for | |
5307 | setting up the quit-restore parameter of the window used. | |
56f31926 MR |
5308 | |
5309 | If this variable appears \"not to work\", because you added a | |
5310 | name to it but the corresponding buffer is displayed in the | |
5311 | selected window, look at the values of `same-window-buffer-names' | |
5312 | and `same-window-regexps'. Those variables take precedence over | |
5313 | this one. | |
5314 | ||
5315 | See also `special-display-buffer-names'." | |
5316 | :type '(repeat | |
5317 | (choice :tag "Buffer" | |
5318 | :value "" | |
5319 | (regexp :format "%v") | |
5320 | (cons :tag "With parameters" | |
5321 | :format "%v" | |
5322 | :value ("" . nil) | |
5323 | (regexp :format "%v") | |
5324 | (repeat :tag "Parameters" | |
5325 | (cons :format "%v" | |
5326 | (symbol :tag "Parameter") | |
5327 | (sexp :tag "Value")))) | |
5328 | (list :tag "With function" | |
5329 | :format "%v" | |
5330 | :value ("" . nil) | |
5331 | (regexp :format "%v") | |
5332 | (function :tag "Function") | |
5333 | (repeat :tag "Arguments" (sexp))))) | |
5334 | :group 'windows | |
5335 | :group 'frames) | |
77f1f99c CY |
5336 | (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3") |
5337 | (put 'special-display-regexps 'risky-local-variable t) | |
56f31926 | 5338 | |
3c448ab6 MR |
5339 | (defun special-display-p (buffer-name) |
5340 | "Return non-nil if a buffer named BUFFER-NAME gets a special frame. | |
56f31926 MR |
5341 | More precisely, return t if `special-display-buffer-names' or |
5342 | `special-display-regexps' contain a string entry equaling or | |
5343 | matching BUFFER-NAME. If `special-display-buffer-names' or | |
5344 | `special-display-regexps' contain a list entry whose car equals | |
5345 | or matches BUFFER-NAME, the return value is the cdr of that | |
5346 | entry." | |
f818cd2a | 5347 | (let (tmp) |
98722073 | 5348 | (cond |
f818cd2a | 5349 | ((member buffer-name special-display-buffer-names) |
98722073 | 5350 | t) |
f818cd2a | 5351 | ((setq tmp (assoc buffer-name special-display-buffer-names)) |
98722073 MR |
5352 | (cdr tmp)) |
5353 | ((catch 'found | |
f818cd2a | 5354 | (dolist (regexp special-display-regexps) |
98722073 MR |
5355 | (cond |
5356 | ((stringp regexp) | |
5357 | (when (string-match-p regexp buffer-name) | |
5358 | (throw 'found t))) | |
5359 | ((and (consp regexp) (stringp (car regexp)) | |
5360 | (string-match-p (car regexp) buffer-name)) | |
5361 | (throw 'found (cdr regexp)))))))))) | |
9481c002 | 5362 | |
f818cd2a MR |
5363 | (defcustom special-display-frame-alist |
5364 | '((height . 14) (width . 80) (unsplittable . t)) | |
5365 | "Alist of parameters for special frames. | |
5366 | Special frames are used for buffers whose names are listed in | |
5367 | `special-display-buffer-names' and for buffers whose names match | |
5368 | one of the regular expressions in `special-display-regexps'. | |
9481c002 | 5369 | |
f818cd2a | 5370 | This variable can be set in your init file, like this: |
9481c002 | 5371 | |
f818cd2a MR |
5372 | (setq special-display-frame-alist '((width . 80) (height . 20))) |
5373 | ||
5374 | These supersede the values given in `default-frame-alist'." | |
5375 | :type '(repeat (cons :format "%v" | |
5376 | (symbol :tag "Parameter") | |
5377 | (sexp :tag "Value"))) | |
5378 | :group 'frames) | |
77f1f99c | 5379 | (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3") |
f818cd2a MR |
5380 | |
5381 | (defun special-display-popup-frame (buffer &optional args) | |
31cd32c9 | 5382 | "Pop up a frame displaying BUFFER and return its window. |
f818cd2a MR |
5383 | If BUFFER is already displayed in a visible or iconified frame, |
5384 | raise that frame. Otherwise, display BUFFER in a new frame. | |
5385 | ||
5386 | Optional argument ARGS is a list specifying additional | |
5387 | information. | |
5388 | ||
5389 | If ARGS is an alist, use it as a list of frame parameters. If | |
382c953b JB |
5390 | these parameters contain (same-window . t), display BUFFER in |
5391 | the selected window. If they contain (same-frame . t), display | |
f818cd2a MR |
5392 | BUFFER in a window of the selected frame. |
5393 | ||
5394 | If ARGS is a list whose car is a symbol, use (car ARGS) as a | |
fdc2806d CY |
5395 | function to do the work. Pass it BUFFER as first argument, and |
5396 | pass the elements of (cdr ARGS) as the remaining arguments." | |
f818cd2a MR |
5397 | (if (and args (symbolp (car args))) |
5398 | (apply (car args) buffer (cdr args)) | |
5399 | (let ((window (get-buffer-window buffer 0))) | |
5400 | (or | |
5401 | ;; If we have a window already, make it visible. | |
5402 | (when window | |
5403 | (let ((frame (window-frame window))) | |
5404 | (make-frame-visible frame) | |
5405 | (raise-frame frame) | |
cf4eacfd | 5406 | (display-buffer-record-window 'reuse window buffer) |
f818cd2a MR |
5407 | window)) |
5408 | ;; Reuse the current window if the user requested it. | |
5409 | (when (cdr (assq 'same-window args)) | |
5410 | (condition-case nil | |
5411 | (progn (switch-to-buffer buffer nil t) (selected-window)) | |
5412 | (error nil))) | |
5413 | ;; Stay on the same frame if requested. | |
5414 | (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args))) | |
5415 | (let* ((pop-up-windows t) | |
5416 | pop-up-frames | |
5417 | special-display-buffer-names special-display-regexps) | |
5418 | (display-buffer buffer))) | |
5419 | ;; If no window yet, make one in a new frame. | |
e75852fd MR |
5420 | (let* ((frame |
5421 | (with-current-buffer buffer | |
5422 | (make-frame (append args special-display-frame-alist)))) | |
5423 | (window (frame-selected-window frame))) | |
5424 | (display-buffer-record-window 'frame window buffer) | |
c5e28e39 MR |
5425 | (unless (eq buffer (window-buffer window)) |
5426 | (set-window-buffer window buffer) | |
5427 | (set-window-prev-buffers window nil)) | |
e75852fd MR |
5428 | (set-window-dedicated-p window t) |
5429 | window))))) | |
f818cd2a MR |
5430 | |
5431 | (defcustom special-display-function 'special-display-popup-frame | |
5432 | "Function to call for displaying special buffers. | |
5433 | This function is called with two arguments - the buffer and, | |
5434 | optionally, a list - and should return a window displaying that | |
5435 | buffer. The default value usually makes a separate frame for the | |
5436 | buffer using `special-display-frame-alist' to specify the frame | |
5437 | parameters. See the definition of `special-display-popup-frame' | |
5438 | for how to specify such a function. | |
5439 | ||
5440 | A buffer is special when its name is either listed in | |
5441 | `special-display-buffer-names' or matches a regexp in | |
5442 | `special-display-regexps'. | |
5443 | ||
e1c2c6f2 MR |
5444 | The specified function should call `display-buffer-record-window' |
5445 | with corresponding arguments to set up the quit-restore parameter | |
5446 | of the window used." | |
f818cd2a MR |
5447 | :type 'function |
5448 | :group 'frames) | |
77f1f99c | 5449 | (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3") |
f818cd2a MR |
5450 | |
5451 | (defcustom same-window-buffer-names nil | |
5452 | "List of names of buffers that should appear in the \"same\" window. | |
5453 | `display-buffer' and `pop-to-buffer' show a buffer whose name is | |
5454 | on this list in the selected rather than some other window. | |
5455 | ||
5456 | An element of this list can be a cons cell instead of just a | |
5457 | string. In that case, the cell's car must be a string specifying | |
5458 | the buffer name. This is for compatibility with | |
5459 | `special-display-buffer-names'; the cdr of the cons cell is | |
5460 | ignored. | |
5461 | ||
5462 | See also `same-window-regexps'." | |
5463 | :type '(repeat (string :format "%v")) | |
5464 | :group 'windows) | |
5465 | ||
5466 | (defcustom same-window-regexps nil | |
5467 | "List of regexps saying which buffers should appear in the \"same\" window. | |
5468 | `display-buffer' and `pop-to-buffer' show a buffer whose name | |
5469 | matches a regexp on this list in the selected rather than some | |
5470 | other window. | |
5471 | ||
5472 | An element of this list can be a cons cell instead of just a | |
5473 | string. In that case, the cell's car must be a regexp matching | |
5474 | the buffer name. This is for compatibility with | |
5475 | `special-display-regexps'; the cdr of the cons cell is ignored. | |
9481c002 | 5476 | |
f818cd2a MR |
5477 | See also `same-window-buffer-names'." |
5478 | :type '(repeat (regexp :format "%v")) | |
5479 | :group 'windows) | |
9481c002 | 5480 | |
f818cd2a MR |
5481 | (defun same-window-p (buffer-name) |
5482 | "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window. | |
5483 | This function returns non-nil if `display-buffer' or | |
5484 | `pop-to-buffer' would show a buffer named BUFFER-NAME in the | |
382c953b | 5485 | selected rather than (as usual) some other window. See |
f818cd2a MR |
5486 | `same-window-buffer-names' and `same-window-regexps'." |
5487 | (cond | |
5488 | ((not (stringp buffer-name))) | |
5489 | ;; The elements of `same-window-buffer-names' can be buffer | |
5490 | ;; names or cons cells whose cars are buffer names. | |
5491 | ((member buffer-name same-window-buffer-names)) | |
5492 | ((assoc buffer-name same-window-buffer-names)) | |
5493 | ((catch 'found | |
5494 | (dolist (regexp same-window-regexps) | |
5495 | ;; The elements of `same-window-regexps' can be regexps | |
5496 | ;; or cons cells whose cars are regexps. | |
5497 | (when (or (and (stringp regexp) | |
cb882333 | 5498 | (string-match-p regexp buffer-name)) |
f818cd2a MR |
5499 | (and (consp regexp) (stringp (car regexp)) |
5500 | (string-match-p (car regexp) buffer-name))) | |
5501 | (throw 'found t))))))) | |
3c448ab6 | 5502 | |
d1067961 | 5503 | (defcustom pop-up-frames nil |
3c448ab6 | 5504 | "Whether `display-buffer' should make a separate frame. |
d1f18ec0 | 5505 | If nil, never make a separate frame. |
3c448ab6 MR |
5506 | If the value is `graphic-only', make a separate frame |
5507 | on graphic displays only. | |
5508 | Any other non-nil value means always make a separate frame." | |
5509 | :type '(choice | |
5510 | (const :tag "Never" nil) | |
5511 | (const :tag "On graphic displays only" graphic-only) | |
5512 | (const :tag "Always" t)) | |
f818cd2a | 5513 | :group 'windows) |
3c448ab6 | 5514 | |
d1067961 | 5515 | (defcustom display-buffer-reuse-frames nil |
f818cd2a | 5516 | "Non-nil means `display-buffer' should reuse frames. |
3c448ab6 MR |
5517 | If the buffer in question is already displayed in a frame, raise |
5518 | that frame." | |
5519 | :type 'boolean | |
d1067961 | 5520 | :version "21.1" |
f818cd2a | 5521 | :group 'windows) |
24777832 CY |
5522 | |
5523 | (make-obsolete-variable | |
5524 | 'display-buffer-reuse-frames | |
5525 | "use a `reusable-frames' alist entry in `display-buffer-alist'." | |
5526 | "24.3") | |
3c448ab6 | 5527 | |
4dc2a129 MR |
5528 | (defcustom pop-up-windows t |
5529 | "Non-nil means `display-buffer' should make a new window." | |
3c448ab6 MR |
5530 | :type 'boolean |
5531 | :group 'windows) | |
5532 | ||
8b10a2d1 | 5533 | (defcustom split-window-preferred-function 'split-window-sensibly |
f818cd2a | 5534 | "Function called by `display-buffer' routines to split a window. |
8b10a2d1 MR |
5535 | This function is called with a window as single argument and is |
5536 | supposed to split that window and return the new window. If the | |
5537 | window can (or shall) not be split, it is supposed to return nil. | |
5538 | The default is to call the function `split-window-sensibly' which | |
5539 | tries to split the window in a way which seems most suitable. | |
5540 | You can customize the options `split-height-threshold' and/or | |
5541 | `split-width-threshold' in order to have `split-window-sensibly' | |
5542 | prefer either vertical or horizontal splitting. | |
5543 | ||
f818cd2a MR |
5544 | If you set this to any other function, bear in mind that the |
5545 | `display-buffer' routines may call this function two times. The | |
5546 | argument of the first call is the largest window on its frame. | |
5547 | If that call fails to return a live window, the function is | |
5548 | called again with the least recently used window as argument. If | |
5549 | that call fails too, `display-buffer' will use an existing window | |
5550 | to display its buffer. | |
8b10a2d1 MR |
5551 | |
5552 | The window selected at the time `display-buffer' was invoked is | |
5553 | still selected when this function is called. Hence you can | |
5554 | compare the window argument with the value of `selected-window' | |
5555 | if you intend to split the selected window instead or if you do | |
5556 | not want to split the selected window." | |
5557 | :type 'function | |
3c448ab6 MR |
5558 | :version "23.1" |
5559 | :group 'windows) | |
5560 | ||
8b10a2d1 | 5561 | (defcustom split-height-threshold 80 |
f818cd2a MR |
5562 | "Minimum height for splitting windows sensibly. |
5563 | If this is an integer, `split-window-sensibly' may split a window | |
8b10a2d1 | 5564 | vertically only if it has at least this many lines. If this is |
f818cd2a MR |
5565 | nil, `split-window-sensibly' is not allowed to split a window |
5566 | vertically. If, however, a window is the only window on its | |
5567 | frame, `split-window-sensibly' may split it vertically | |
5568 | disregarding the value of this variable." | |
8b10a2d1 | 5569 | :type '(choice (const nil) (integer :tag "lines")) |
3c448ab6 MR |
5570 | :version "23.1" |
5571 | :group 'windows) | |
5572 | ||
8b10a2d1 | 5573 | (defcustom split-width-threshold 160 |
f818cd2a MR |
5574 | "Minimum width for splitting windows sensibly. |
5575 | If this is an integer, `split-window-sensibly' may split a window | |
8b10a2d1 | 5576 | horizontally only if it has at least this many columns. If this |
f818cd2a MR |
5577 | is nil, `split-window-sensibly' is not allowed to split a window |
5578 | horizontally." | |
8b10a2d1 | 5579 | :type '(choice (const nil) (integer :tag "columns")) |
3c448ab6 MR |
5580 | :version "23.1" |
5581 | :group 'windows) | |
9481c002 | 5582 | |
8b10a2d1 MR |
5583 | (defun window-splittable-p (window &optional horizontal) |
5584 | "Return non-nil if `split-window-sensibly' may split WINDOW. | |
5585 | Optional argument HORIZONTAL nil or omitted means check whether | |
5586 | `split-window-sensibly' may split WINDOW vertically. HORIZONTAL | |
5587 | non-nil means check whether WINDOW may be split horizontally. | |
3c448ab6 | 5588 | |
8b10a2d1 | 5589 | WINDOW may be split vertically when the following conditions |
3c448ab6 | 5590 | hold: |
3c448ab6 MR |
5591 | - `window-size-fixed' is either nil or equals `width' for the |
5592 | buffer of WINDOW. | |
8b10a2d1 | 5593 | - `split-height-threshold' is an integer and WINDOW is at least as |
3c448ab6 | 5594 | high as `split-height-threshold'. |
3c448ab6 MR |
5595 | - When WINDOW is split evenly, the emanating windows are at least |
5596 | `window-min-height' lines tall and can accommodate at least one | |
5597 | line plus - if WINDOW has one - a mode line. | |
5598 | ||
8b10a2d1 | 5599 | WINDOW may be split horizontally when the following conditions |
3c448ab6 | 5600 | hold: |
3c448ab6 MR |
5601 | - `window-size-fixed' is either nil or equals `height' for the |
5602 | buffer of WINDOW. | |
8b10a2d1 | 5603 | - `split-width-threshold' is an integer and WINDOW is at least as |
3c448ab6 | 5604 | wide as `split-width-threshold'. |
3c448ab6 MR |
5605 | - When WINDOW is split evenly, the emanating windows are at least |
5606 | `window-min-width' or two (whichever is larger) columns wide." | |
5607 | (when (window-live-p window) | |
5608 | (with-current-buffer (window-buffer window) | |
5609 | (if horizontal | |
5610 | ;; A window can be split horizontally when its width is not | |
5611 | ;; fixed, it is at least `split-width-threshold' columns wide | |
5612 | ;; and at least twice as wide as `window-min-width' and 2 (the | |
5613 | ;; latter value is hardcoded). | |
5614 | (and (memq window-size-fixed '(nil height)) | |
5615 | ;; Testing `window-full-width-p' here hardly makes any | |
5616 | ;; sense nowadays. This can be done more intuitively by | |
5617 | ;; setting up `split-width-threshold' appropriately. | |
5618 | (numberp split-width-threshold) | |
5619 | (>= (window-width window) | |
5620 | (max split-width-threshold | |
5621 | (* 2 (max window-min-width 2))))) | |
5622 | ;; A window can be split vertically when its height is not | |
5623 | ;; fixed, it is at least `split-height-threshold' lines high, | |
5624 | ;; and it is at least twice as high as `window-min-height' and 2 | |
37269466 | 5625 | ;; if it has a mode line or 1. |
3c448ab6 MR |
5626 | (and (memq window-size-fixed '(nil width)) |
5627 | (numberp split-height-threshold) | |
5628 | (>= (window-height window) | |
5629 | (max split-height-threshold | |
5630 | (* 2 (max window-min-height | |
5631 | (if mode-line-format 2 1)))))))))) | |
5632 | ||
2dc2a609 | 5633 | (defun split-window-sensibly (&optional window) |
8b10a2d1 | 5634 | "Split WINDOW in a way suitable for `display-buffer'. |
2dc2a609 | 5635 | WINDOW defaults to the currently selected window. |
8b10a2d1 MR |
5636 | If `split-height-threshold' specifies an integer, WINDOW is at |
5637 | least `split-height-threshold' lines tall and can be split | |
5638 | vertically, split WINDOW into two windows one above the other and | |
5639 | return the lower window. Otherwise, if `split-width-threshold' | |
5640 | specifies an integer, WINDOW is at least `split-width-threshold' | |
5641 | columns wide and can be split horizontally, split WINDOW into two | |
5642 | windows side by side and return the window on the right. If this | |
5643 | can't be done either and WINDOW is the only window on its frame, | |
5644 | try to split WINDOW vertically disregarding any value specified | |
5645 | by `split-height-threshold'. If that succeeds, return the lower | |
5646 | window. Return nil otherwise. | |
5647 | ||
5648 | By default `display-buffer' routines call this function to split | |
5649 | the largest or least recently used window. To change the default | |
5650 | customize the option `split-window-preferred-function'. | |
5651 | ||
5652 | You can enforce this function to not split WINDOW horizontally, | |
382c953b | 5653 | by setting (or binding) the variable `split-width-threshold' to |
8b10a2d1 MR |
5654 | nil. If, in addition, you set `split-height-threshold' to zero, |
5655 | chances increase that this function does split WINDOW vertically. | |
5656 | ||
382c953b | 5657 | In order to not split WINDOW vertically, set (or bind) the |
8b10a2d1 MR |
5658 | variable `split-height-threshold' to nil. Additionally, you can |
5659 | set `split-width-threshold' to zero to make a horizontal split | |
5660 | more likely to occur. | |
5661 | ||
5662 | Have a look at the function `window-splittable-p' if you want to | |
5663 | know how `split-window-sensibly' determines whether WINDOW can be | |
5664 | split." | |
2dc2a609 TH |
5665 | (let ((window (or window (selected-window)))) |
5666 | (or (and (window-splittable-p window) | |
5667 | ;; Split window vertically. | |
5668 | (with-selected-window window | |
5669 | (split-window-below))) | |
5670 | (and (window-splittable-p window t) | |
5671 | ;; Split window horizontally. | |
5672 | (with-selected-window window | |
5673 | (split-window-right))) | |
5674 | (and (eq window (frame-root-window (window-frame window))) | |
5675 | (not (window-minibuffer-p window)) | |
5676 | ;; If WINDOW is the only window on its frame and is not the | |
5677 | ;; minibuffer window, try to split it vertically disregarding | |
5678 | ;; the value of `split-height-threshold'. | |
5679 | (let ((split-height-threshold 0)) | |
5680 | (when (window-splittable-p window) | |
5681 | (with-selected-window window | |
5682 | (split-window-below)))))))) | |
9481c002 | 5683 | |
5938d519 | 5684 | (defun window--try-to-split-window (window &optional alist) |
f818cd2a MR |
5685 | "Try to split WINDOW. |
5686 | Return value returned by `split-window-preferred-function' if it | |
5687 | represents a live window, nil otherwise." | |
5688 | (and (window-live-p window) | |
5689 | (not (frame-parameter (window-frame window) 'unsplittable)) | |
8e17c9ba MR |
5690 | (let* ((window-combination-limit |
5691 | ;; When `window-combination-limit' equals | |
5938d519 MR |
5692 | ;; `display-buffer' or equals `resize-window' and a |
5693 | ;; `window-height' or `window-width' alist entry are | |
5694 | ;; present, bind it to t so resizing steals space | |
5695 | ;; preferably from the window that was split. | |
5696 | (if (or (eq window-combination-limit 'display-buffer) | |
5697 | (and (eq window-combination-limit 'window-size) | |
5698 | (or (cdr (assq 'window-height alist)) | |
5699 | (cdr (assq 'window-width alist))))) | |
8e17c9ba MR |
5700 | t |
5701 | window-combination-limit)) | |
5702 | (new-window | |
5703 | ;; Since `split-window-preferred-function' might | |
5704 | ;; throw an error use `condition-case'. | |
5705 | (condition-case nil | |
5706 | (funcall split-window-preferred-function window) | |
5707 | (error nil)))) | |
f818cd2a MR |
5708 | (and (window-live-p new-window) new-window)))) |
5709 | ||
5710 | (defun window--frame-usable-p (frame) | |
5711 | "Return FRAME if it can be used to display a buffer." | |
5712 | (when (frame-live-p frame) | |
5713 | (let ((window (frame-root-window frame))) | |
5714 | ;; `frame-root-window' may be an internal window which is considered | |
5715 | ;; "dead" by `window-live-p'. Hence if `window' is not live we | |
5716 | ;; implicitly know that `frame' has a visible window we can use. | |
5717 | (unless (and (window-live-p window) | |
5718 | (or (window-minibuffer-p window) | |
5719 | ;; If the window is soft-dedicated, the frame is usable. | |
5720 | ;; Actually, even if the window is really dedicated, | |
5721 | ;; the frame is still usable by splitting it. | |
5722 | ;; At least Emacs-22 allowed it, and it is desirable | |
5723 | ;; when displaying same-frame windows. | |
5724 | nil ; (eq t (window-dedicated-p window)) | |
5725 | )) | |
5726 | frame)))) | |
5727 | ||
5728 | (defcustom even-window-heights t | |
5729 | "If non-nil `display-buffer' will try to even window heights. | |
5730 | Otherwise `display-buffer' will leave the window configuration | |
5731 | alone. Heights are evened only when `display-buffer' chooses a | |
5732 | window that appears above or below the selected window." | |
5733 | :type 'boolean | |
5734 | :group 'windows) | |
5735 | ||
5736 | (defun window--even-window-heights (window) | |
5737 | "Even heights of WINDOW and selected window. | |
5738 | Do this only if these windows are vertically adjacent to each | |
5739 | other, `even-window-heights' is non-nil, and the selected window | |
5740 | is higher than WINDOW." | |
5741 | (when (and even-window-heights | |
9aba119d MR |
5742 | ;; Even iff WINDOW forms a vertical combination with the |
5743 | ;; selected window, and WINDOW's height exceeds that of the | |
5744 | ;; selected window, see also bug#11880. | |
5745 | (window-combined-p window) | |
5746 | (= (window-child-count (window-parent window)) 2) | |
5747 | (eq (window-parent) (window-parent window)) | |
5748 | (> (window-total-height) (window-total-height window))) | |
5749 | ;; Don't throw an error if we can't even window heights for | |
5750 | ;; whatever reason. | |
5751 | (condition-case nil | |
5752 | (enlarge-window | |
5753 | (/ (- (window-total-height window) (window-total-height)) 2)) | |
5754 | (error nil)))) | |
f818cd2a | 5755 | |
5938d519 | 5756 | (defun window--display-buffer (buffer window type &optional alist dedicated) |
0ff7851c | 5757 | "Display BUFFER in WINDOW. |
51a5f9d8 | 5758 | TYPE must be one of the symbols `reuse', `window' or `frame' and |
0ff7851c MR |
5759 | is passed unaltered to `display-buffer-record-window'. ALIST is |
5760 | the alist argument of `display-buffer'. Set `window-dedicated-p' | |
5761 | to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are | |
5762 | live." | |
f818cd2a | 5763 | (when (and (buffer-live-p buffer) (window-live-p window)) |
caceae25 | 5764 | (display-buffer-record-window type window buffer) |
90749b53 CY |
5765 | (unless (eq buffer (window-buffer window)) |
5766 | (set-window-dedicated-p window nil) | |
90749b53 CY |
5767 | (set-window-buffer window buffer) |
5768 | (when dedicated | |
5769 | (set-window-dedicated-p window dedicated)) | |
5770 | (when (memq type '(window frame)) | |
5771 | (set-window-prev-buffers window nil))) | |
2d6b6005 | 5772 | (let ((parameter (window-parameter window 'quit-restore)) |
5938d519 | 5773 | (height (cdr (assq 'window-height alist))) |
880e6158 MR |
5774 | (width (cdr (assq 'window-width alist))) |
5775 | (size (cdr (assq 'window-size alist)))) | |
5776 | (cond | |
5777 | ((or (eq type 'frame) | |
5778 | (and (eq (car parameter) 'same) | |
5779 | (eq (nth 1 parameter) 'frame))) | |
5780 | ;; Adjust size of frame if asked for. | |
5781 | (cond | |
5782 | ((not size)) | |
5783 | ((consp size) | |
5784 | (let ((width (car size)) | |
5785 | (height (cdr size)) | |
2d6b6005 | 5786 | (frame (window-frame window))) |
880e6158 MR |
5787 | (when (and (numberp width) (numberp height)) |
5788 | (set-frame-height | |
5789 | frame (+ (frame-height frame) | |
5790 | (- height (window-total-height window)))) | |
5791 | (set-frame-width | |
5792 | frame (+ (frame-width frame) | |
5793 | (- width (window-total-width window))))))) | |
5794 | ((functionp size) | |
5795 | (ignore-errors (funcall size window))))) | |
5796 | ((or (eq type 'window) | |
5797 | (and (eq (car parameter) 'same) | |
5798 | (eq (nth 1 parameter) 'window))) | |
0ff7851c | 5799 | ;; Adjust height of window if asked for. |
5938d519 MR |
5800 | (cond |
5801 | ((not height)) | |
5802 | ((numberp height) | |
5803 | (let* ((new-height | |
5804 | (if (integerp height) | |
5805 | height | |
5806 | (round | |
880e6158 | 5807 | (* (window-total-height (frame-root-window window)) |
5938d519 | 5808 | height)))) |
880e6158 MR |
5809 | (delta (- new-height (window-total-height window)))) |
5810 | (when (and (window--resizable-p window delta nil 'safe) | |
0ff7851c MR |
5811 | (window-combined-p window)) |
5812 | (window-resize window delta nil 'safe)))) | |
5938d519 MR |
5813 | ((functionp height) |
5814 | (ignore-errors (funcall height window)))) | |
0ff7851c | 5815 | ;; Adjust width of window if asked for. |
5938d519 MR |
5816 | (cond |
5817 | ((not width)) | |
5818 | ((numberp width) | |
5819 | (let* ((new-width | |
5820 | (if (integerp width) | |
5821 | width | |
5822 | (round | |
880e6158 | 5823 | (* (window-total-width (frame-root-window window)) |
5938d519 | 5824 | width)))) |
880e6158 MR |
5825 | (delta (- new-width (window-total-width window)))) |
5826 | (when (and (window--resizable-p window delta t 'safe) | |
0ff7851c MR |
5827 | (window-combined-p window t)) |
5828 | (window-resize window delta t 'safe)))) | |
5938d519 | 5829 | ((functionp width) |
880e6158 | 5830 | (ignore-errors (funcall width window))))))) |
0ff7851c | 5831 | |
90749b53 CY |
5832 | window)) |
5833 | ||
5834 | (defun window--maybe-raise-frame (frame) | |
5835 | (let ((visible (frame-visible-p frame))) | |
5836 | (unless (or (not visible) | |
5837 | ;; Assume the selected frame is already visible enough. | |
5838 | (eq frame (selected-frame)) | |
5839 | ;; Assume the frame from which we invoked the | |
5840 | ;; minibuffer is visible. | |
5841 | (and (minibuffer-window-active-p (selected-window)) | |
5842 | (eq frame (window-frame (minibuffer-selected-window))))) | |
5843 | (raise-frame frame)))) | |
f818cd2a | 5844 | |
24510c22 SM |
5845 | ;; FIXME: Not implemented. |
5846 | ;; FIXME: By the way, there could be more levels of dedication: | |
5847 | ;; - `barely' dedicated doesn't prevent reuse of the window, only records that | |
5848 | ;; the window hasn't been used for something else yet. | |
5849 | ;; - `softly' dedicated only allows reuse when asked explicitly. | |
5850 | ;; - `strongly' never allows reuse. | |
f818cd2a MR |
5851 | (defvar display-buffer-mark-dedicated nil |
5852 | "If non-nil, `display-buffer' marks the windows it creates as dedicated. | |
5853 | The actual non-nil value of this variable will be copied to the | |
5854 | `window-dedicated-p' flag.") | |
5855 | ||
fa5660f9 CY |
5856 | (defconst display-buffer--action-function-custom-type |
5857 | '(choice :tag "Function" | |
5858 | (const :tag "--" ignore) ; default for insertion | |
fa5660f9 | 5859 | (const display-buffer-reuse-window) |
d9558cad | 5860 | (const display-buffer-pop-up-window) |
fa5660f9 CY |
5861 | (const display-buffer-same-window) |
5862 | (const display-buffer-pop-up-frame) | |
880e6158 | 5863 | (const display-buffer-in-previous-window) |
fa5660f9 CY |
5864 | (const display-buffer-use-some-window) |
5865 | (function :tag "Other function")) | |
5866 | "Custom type for `display-buffer' action functions.") | |
5867 | ||
5868 | (defconst display-buffer--action-custom-type | |
5869 | `(cons :tag "Action" | |
5870 | (choice :tag "Action functions" | |
5871 | ,display-buffer--action-function-custom-type | |
5872 | (repeat | |
5873 | :tag "List of functions" | |
5874 | ,display-buffer--action-function-custom-type)) | |
5875 | (alist :tag "Action arguments" | |
5876 | :key-type symbol | |
5877 | :value-type (sexp :tag "Value"))) | |
5878 | "Custom type for `display-buffer' actions.") | |
5879 | ||
cbb0f9ab CY |
5880 | (defvar display-buffer-overriding-action '(nil . nil) |
5881 | "Overriding action to perform to display a buffer. | |
5882 | It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a | |
382c953b | 5883 | function or a list of functions. Each function should accept two |
cbb0f9ab CY |
5884 | arguments: a buffer to display and an alist similar to ALIST. |
5885 | See `display-buffer' for details.") | |
5886 | (put 'display-buffer-overriding-action 'risky-local-variable t) | |
5887 | ||
fa5660f9 | 5888 | (defcustom display-buffer-alist nil |
89894cd8 CY |
5889 | "Alist of conditional actions for `display-buffer'. |
5890 | This is a list of elements (CONDITION . ACTION), where: | |
5891 | ||
0ff7851c MR |
5892 | CONDITION is either a regexp matching buffer names, or a |
5893 | function that takes two arguments - a buffer name and the | |
5894 | ACTION argument of `display-buffer' - and returns a boolean. | |
89894cd8 | 5895 | |
8319e0bf CY |
5896 | ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a |
5897 | function or a list of functions. Each such function should | |
382c953b | 5898 | accept two arguments: a buffer to display and an alist of the |
f0cfa5fe | 5899 | same form as ALIST. See `display-buffer' for details. |
0ff7851c MR |
5900 | |
5901 | `display-buffer' scans this alist until it either finds a | |
5902 | matching regular expression or the function specified by a | |
9139632a JL |
5903 | condition returns non-nil. In any of these cases, it adds the |
5904 | associated action to the list of actions it will try." | |
fa5660f9 CY |
5905 | :type `(alist :key-type |
5906 | (choice :tag "Condition" | |
5907 | regexp | |
5908 | (function :tag "Matcher function")) | |
5909 | :value-type ,display-buffer--action-custom-type) | |
5910 | :risky t | |
5911 | :version "24.1" | |
5912 | :group 'windows) | |
89894cd8 | 5913 | |
cbb0f9ab CY |
5914 | (defcustom display-buffer-base-action '(nil . nil) |
5915 | "User-specified default action for `display-buffer'. | |
8319e0bf | 5916 | It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a |
382c953b | 5917 | function or a list of functions. Each function should accept two |
fa5660f9 CY |
5918 | arguments: a buffer to display and an alist similar to ALIST. |
5919 | See `display-buffer' for details." | |
5920 | :type display-buffer--action-custom-type | |
5921 | :risky t | |
5922 | :version "24.1" | |
5923 | :group 'windows) | |
89894cd8 | 5924 | |
cbb0f9ab | 5925 | (defconst display-buffer-fallback-action |
0a9f9ab5 | 5926 | '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant? |
cbb0f9ab | 5927 | display-buffer-reuse-window |
cbb0f9ab | 5928 | display-buffer--maybe-pop-up-frame-or-window |
880e6158 | 5929 | display-buffer-in-previous-window |
cbb0f9ab CY |
5930 | display-buffer-use-some-window |
5931 | ;; If all else fails, pop up a new frame. | |
5932 | display-buffer-pop-up-frame)) | |
5933 | "Default fallback action for `display-buffer'. | |
5934 | This is the action used by `display-buffer' if no other actions | |
5935 | specified, e.g. by the user options `display-buffer-alist' or | |
5936 | `display-buffer-base-action'. See `display-buffer'.") | |
5937 | (put 'display-buffer-fallback-action 'risky-local-variable t) | |
f818cd2a | 5938 | |
0ff7851c MR |
5939 | (defun display-buffer-assq-regexp (buffer-name alist action) |
5940 | "Retrieve ALIST entry corresponding to BUFFER-NAME. | |
5941 | ACTION is the action argument passed to `display-buffer'." | |
f818cd2a MR |
5942 | (catch 'match |
5943 | (dolist (entry alist) | |
cb882333 | 5944 | (let ((key (car entry))) |
f818cd2a MR |
5945 | (when (or (and (stringp key) |
5946 | (string-match-p key buffer-name)) | |
0ff7851c MR |
5947 | (and (functionp key) |
5948 | (funcall key buffer-name action))) | |
f818cd2a MR |
5949 | (throw 'match (cdr entry))))))) |
5950 | ||
8319e0bf CY |
5951 | (defvar display-buffer--same-window-action |
5952 | '(display-buffer-same-window | |
5953 | (inhibit-same-window . nil)) | |
5954 | "A `display-buffer' action for displaying in the same window.") | |
5955 | (put 'display-buffer--same-window-action 'risky-local-variable t) | |
5956 | ||
5957 | (defvar display-buffer--other-frame-action | |
5958 | '((display-buffer-reuse-window | |
8319e0bf CY |
5959 | display-buffer-pop-up-frame) |
5960 | (reusable-frames . 0) | |
5961 | (inhibit-same-window . t)) | |
5962 | "A `display-buffer' action for displaying in another frame.") | |
5963 | (put 'display-buffer--other-frame-action 'risky-local-variable t) | |
5964 | ||
d45ba96b | 5965 | (defun display-buffer (buffer-or-name &optional action frame) |
3199b96f | 5966 | "Display BUFFER-OR-NAME in some window, without selecting it. |
89894cd8 CY |
5967 | BUFFER-OR-NAME must be a buffer or the name of an existing |
5968 | buffer. Return the window chosen for displaying BUFFER-OR-NAME, | |
5969 | or nil if no such window is found. | |
5970 | ||
7b9abf24 EZ |
5971 | Optional argument ACTION, if non-nil, should specify a display |
5972 | action. Its form is described below. | |
5973 | ||
5974 | Optional argument FRAME, if non-nil, acts like an additional | |
5975 | ALIST entry (reusable-frames . FRAME) to the action list of ACTION, | |
5976 | specifying the frame(s) to search for a window that is already | |
5977 | displaying the buffer. See `display-buffer-reuse-window' | |
5978 | ||
5979 | If ACTION is non-nil, it should have the form (FUNCTION . ALIST), | |
5980 | where FUNCTION is either a function or a list of functions, and | |
d4bd55e7 CY |
5981 | ALIST is an arbitrary association list (alist). |
5982 | ||
5983 | Each such FUNCTION should accept two arguments: the buffer to | |
f130cb76 LL |
5984 | display and an alist. Based on those arguments, it should |
5985 | display the buffer and return the window. If the caller is | |
9139632a JL |
5986 | prepared to handle the case of not displaying the buffer |
5987 | and returning nil from `display-buffer' it should pass | |
5988 | \(allow-no-window . t) as an element of the ALIST. | |
89894cd8 | 5989 | |
cbb0f9ab | 5990 | The `display-buffer' function builds a function list and an alist |
d4bd55e7 CY |
5991 | by combining the functions and alists specified in |
5992 | `display-buffer-overriding-action', `display-buffer-alist', the | |
5993 | ACTION argument, `display-buffer-base-action', and | |
5994 | `display-buffer-fallback-action' (in order). Then it calls each | |
5995 | function in the combined function list in turn, passing the | |
cbb0f9ab CY |
5996 | buffer as the first argument and the combined alist as the second |
5997 | argument, until one of the functions returns non-nil. | |
8319e0bf | 5998 | |
7b9abf24 EZ |
5999 | If ACTION is nil, the function list and the alist are built using |
6000 | only the other variables mentioned above. | |
6001 | ||
8319e0bf CY |
6002 | Available action functions include: |
6003 | `display-buffer-same-window' | |
8319e0bf CY |
6004 | `display-buffer-reuse-window' |
6005 | `display-buffer-pop-up-frame' | |
6006 | `display-buffer-pop-up-window' | |
880e6158 | 6007 | `display-buffer-in-previous-window' |
8319e0bf CY |
6008 | `display-buffer-use-some-window' |
6009 | ||
6010 | Recognized alist entries include: | |
6011 | ||
6012 | `inhibit-same-window' -- A non-nil value prevents the same | |
6013 | window from being used for display. | |
6014 | ||
90749b53 CY |
6015 | `inhibit-switch-frame' -- A non-nil value prevents any other |
6016 | frame from being raised or selected, | |
6017 | even if the window is displayed there. | |
6018 | ||
8319e0bf CY |
6019 | `reusable-frames' -- Value specifies frame(s) to search for a |
6020 | window that already displays the buffer. | |
6021 | See `display-buffer-reuse-window'. | |
2a7bdc1a | 6022 | |
d97af5a0 CY |
6023 | `pop-up-frame-parameters' -- Value specifies an alist of frame |
6024 | parameters to give a new frame, if | |
6025 | one is created. | |
6026 | ||
df171c23 MR |
6027 | `window-height' -- Value specifies either an integer (the number |
6028 | of lines of a new window), a floating point number (the | |
6029 | fraction of a new window with respect to the height of the | |
6030 | frame's root window) or a function to be called with one | |
6031 | argument - a new window. The function is supposed to adjust | |
6032 | the height of the window; its return value is ignored. | |
6033 | Suitable functions are `shrink-window-if-larger-than-buffer' | |
6034 | and `fit-window-to-buffer'. | |
6035 | ||
6036 | `window-width' -- Value specifies either an integer (the number | |
6037 | of columns of a new window), a floating point number (the | |
6038 | fraction of a new window with respect to the width of the | |
6039 | frame's root window) or a function to be called with one | |
6040 | argument - a new window. The function is supposed to adjust | |
6041 | the width of the window; its return value is ignored. | |
6042 | ||
9139632a JL |
6043 | `allow-no-window' -- A non-nil value indicates readiness for the case |
6044 | of not displaying the buffer and FUNCTION can safely return | |
6045 | a non-window value to suppress displaying. | |
6046 | ||
2a7bdc1a CY |
6047 | The ACTION argument to `display-buffer' can also have a non-nil |
6048 | and non-list value. This means to display the buffer in a window | |
6049 | other than the selected one, even if it is already displayed in | |
6050 | the selected window. If called interactively with a prefix | |
7b9abf24 | 6051 | argument, ACTION is t." |
c3313451 CY |
6052 | (interactive (list (read-buffer "Display buffer: " (other-buffer)) |
6053 | (if current-prefix-arg t))) | |
d45ba96b MR |
6054 | (let ((buffer (if (bufferp buffer-or-name) |
6055 | buffer-or-name | |
6056 | (get-buffer buffer-or-name))) | |
f47ad11b MR |
6057 | ;; Make sure that when we split windows the old window keeps |
6058 | ;; point, bug#14829. | |
6059 | (split-window-keep-point t) | |
89894cd8 CY |
6060 | ;; Handle the old form of the first argument. |
6061 | (inhibit-same-window (and action (not (listp action))))) | |
6062 | (unless (listp action) (setq action nil)) | |
6063 | (if display-buffer-function | |
6064 | ;; If `display-buffer-function' is defined, let it do the job. | |
6065 | (funcall display-buffer-function buffer inhibit-same-window) | |
6066 | ;; Otherwise, use the defined actions. | |
6067 | (let* ((user-action | |
0ff7851c MR |
6068 | (display-buffer-assq-regexp |
6069 | (buffer-name buffer) display-buffer-alist action)) | |
0a9f9ab5 | 6070 | (special-action (display-buffer--special-action buffer)) |
89894cd8 CY |
6071 | ;; Extra actions from the arguments to this function: |
6072 | (extra-action | |
6073 | (cons nil (append (if inhibit-same-window | |
6074 | '((inhibit-same-window . t))) | |
6075 | (if frame | |
8319e0bf | 6076 | `((reusable-frames . ,frame)))))) |
89894cd8 CY |
6077 | ;; Construct action function list and action alist. |
6078 | (actions (list display-buffer-overriding-action | |
0a9f9ab5 | 6079 | user-action special-action action extra-action |
cbb0f9ab CY |
6080 | display-buffer-base-action |
6081 | display-buffer-fallback-action)) | |
89894cd8 CY |
6082 | (functions (apply 'append |
6083 | (mapcar (lambda (x) | |
6084 | (setq x (car x)) | |
c3313451 | 6085 | (if (functionp x) (list x) x)) |
89894cd8 CY |
6086 | actions))) |
6087 | (alist (apply 'append (mapcar 'cdr actions))) | |
6088 | window) | |
6089 | (unless (buffer-live-p buffer) | |
6090 | (error "Invalid buffer")) | |
6091 | (while (and functions (not window)) | |
6092 | (setq window (funcall (car functions) buffer alist) | |
6093 | functions (cdr functions))) | |
f130cb76 | 6094 | (and (windowp window) window))))) |
f818cd2a MR |
6095 | |
6096 | (defun display-buffer-other-frame (buffer) | |
e6f759f9 | 6097 | "Display buffer BUFFER preferably in another frame. |
f818cd2a MR |
6098 | This uses the function `display-buffer' as a subroutine; see |
6099 | its documentation for additional customization information." | |
6100 | (interactive "BDisplay buffer in other frame: ") | |
8319e0bf | 6101 | (display-buffer buffer display-buffer--other-frame-action t)) |
f818cd2a | 6102 | |
89894cd8 | 6103 | ;;; `display-buffer' action functions: |
437014c8 | 6104 | |
89894cd8 | 6105 | (defun display-buffer-same-window (buffer alist) |
8319e0bf CY |
6106 | "Display BUFFER in the selected window. |
6107 | This fails if ALIST has a non-nil `inhibit-same-window' entry, or | |
6108 | if the selected window is a minibuffer window or is dedicated to | |
6109 | another buffer; in that case, return nil. Otherwise, return the | |
6110 | selected window." | |
89894cd8 CY |
6111 | (unless (or (cdr (assq 'inhibit-same-window alist)) |
6112 | (window-minibuffer-p) | |
6113 | (window-dedicated-p)) | |
5938d519 | 6114 | (window--display-buffer buffer (selected-window) 'reuse alist))) |
89894cd8 | 6115 | |
0d3ff375 | 6116 | (defun display-buffer--maybe-same-window (buffer alist) |
8319e0bf CY |
6117 | "Conditionally display BUFFER in the selected window. |
6118 | If `same-window-p' returns non-nil for BUFFER's name, call | |
6119 | `display-buffer-same-window' and return its value. Otherwise, | |
6120 | return nil." | |
89894cd8 CY |
6121 | (and (same-window-p (buffer-name buffer)) |
6122 | (display-buffer-same-window buffer alist))) | |
6123 | ||
6124 | (defun display-buffer-reuse-window (buffer alist) | |
6125 | "Return a window that is already displaying BUFFER. | |
8319e0bf CY |
6126 | Return nil if no usable window is found. |
6127 | ||
6128 | If ALIST has a non-nil `inhibit-same-window' entry, the selected | |
6129 | window is not eligible for reuse. | |
6130 | ||
6131 | If ALIST contains a `reusable-frames' entry, its value determines | |
6132 | which frames to search for a reusable window: | |
6133 | nil -- the selected frame (actually the last non-minibuffer frame) | |
6134 | A frame -- just that frame | |
6135 | `visible' -- all visible frames | |
6136 | 0 -- all frames on the current terminal | |
6137 | t -- all frames. | |
6138 | ||
6139 | If ALIST contains no `reusable-frames' entry, search just the | |
6140 | selected frame if `display-buffer-reuse-frames' and | |
6141 | `pop-up-frames' are both nil; search all frames on the current | |
90749b53 CY |
6142 | terminal if either of those variables is non-nil. |
6143 | ||
6144 | If ALIST has a non-nil `inhibit-switch-frame' entry, then in the | |
6145 | event that a window on another frame is chosen, avoid raising | |
6146 | that frame." | |
8319e0bf CY |
6147 | (let* ((alist-entry (assq 'reusable-frames alist)) |
6148 | (frames (cond (alist-entry (cdr alist-entry)) | |
6149 | ((if (eq pop-up-frames 'graphic-only) | |
6150 | (display-graphic-p) | |
6151 | pop-up-frames) | |
6152 | 0) | |
6153 | (display-buffer-reuse-frames 0) | |
6154 | (t (last-nonminibuffer-frame)))) | |
6155 | (window (if (and (eq buffer (window-buffer)) | |
6156 | (not (cdr (assq 'inhibit-same-window alist)))) | |
6157 | (selected-window) | |
6158 | (car (delq (selected-window) | |
6159 | (get-buffer-window-list buffer 'nomini | |
6160 | frames)))))) | |
90749b53 | 6161 | (when (window-live-p window) |
5938d519 | 6162 | (prog1 (window--display-buffer buffer window 'reuse alist) |
90749b53 CY |
6163 | (unless (cdr (assq 'inhibit-switch-frame alist)) |
6164 | (window--maybe-raise-frame (window-frame window))))))) | |
89894cd8 | 6165 | |
0a9f9ab5 | 6166 | (defun display-buffer--special-action (buffer) |
4ad3bc2a CY |
6167 | "Return special display action for BUFFER, if any. |
6168 | If `special-display-p' returns non-nil for BUFFER, return an | |
6169 | appropriate display action involving `special-display-function'. | |
6170 | See `display-buffer' for the format of display actions." | |
8319e0bf CY |
6171 | (and special-display-function |
6172 | ;; `special-display-p' returns either t or a list of frame | |
6173 | ;; parameters to pass to `special-display-function'. | |
6174 | (let ((pars (special-display-p (buffer-name buffer)))) | |
6175 | (when pars | |
0a9f9ab5 SM |
6176 | (list (list #'display-buffer-reuse-window |
6177 | `(lambda (buffer _alist) | |
6178 | (funcall special-display-function | |
6179 | buffer ',(if (listp pars) pars))))))))) | |
8319e0bf | 6180 | |
90749b53 | 6181 | (defun display-buffer-pop-up-frame (buffer alist) |
89894cd8 | 6182 | "Display BUFFER in a new frame. |
8319e0bf | 6183 | This works by calling `pop-up-frame-function'. If successful, |
90749b53 CY |
6184 | return the window used; otherwise return nil. |
6185 | ||
6186 | If ALIST has a non-nil `inhibit-switch-frame' entry, avoid | |
d97af5a0 CY |
6187 | raising the new frame. |
6188 | ||
6189 | If ALIST has a non-nil `pop-up-frame-parameters' entry, the | |
6190 | corresponding value is an alist of frame parameters to give the | |
6191 | new frame." | |
6192 | (let* ((params (cdr (assq 'pop-up-frame-parameters alist))) | |
6193 | (pop-up-frame-alist (append params pop-up-frame-alist)) | |
6194 | (fun pop-up-frame-function) | |
6195 | frame window) | |
89894cd8 | 6196 | (when (and fun |
7fd5f65e MR |
6197 | ;; Make BUFFER current so `make-frame' will use it as the |
6198 | ;; new frame's buffer (Bug#15133). | |
6199 | (with-current-buffer buffer | |
6200 | (setq frame (funcall fun))) | |
89894cd8 | 6201 | (setq window (frame-selected-window frame))) |
5938d519 MR |
6202 | (prog1 (window--display-buffer |
6203 | buffer window 'frame alist display-buffer-mark-dedicated) | |
90749b53 CY |
6204 | (unless (cdr (assq 'inhibit-switch-frame alist)) |
6205 | (window--maybe-raise-frame frame)))))) | |
89894cd8 | 6206 | |
90749b53 | 6207 | (defun display-buffer-pop-up-window (buffer alist) |
89894cd8 CY |
6208 | "Display BUFFER by popping up a new window. |
6209 | The new window is created on the selected frame, or in | |
6210 | `last-nonminibuffer-frame' if no windows can be created there. | |
90749b53 CY |
6211 | If successful, return the new window; otherwise return nil. |
6212 | ||
6213 | If ALIST has a non-nil `inhibit-switch-frame' entry, then in the | |
6214 | event that the new window is created on another frame, avoid | |
6215 | raising the frame." | |
89894cd8 CY |
6216 | (let ((frame (or (window--frame-usable-p (selected-frame)) |
6217 | (window--frame-usable-p (last-nonminibuffer-frame)))) | |
6218 | window) | |
6219 | (when (and (or (not (frame-parameter frame 'unsplittable)) | |
6220 | ;; If the selected frame cannot be split, look at | |
6221 | ;; `last-nonminibuffer-frame'. | |
6222 | (and (eq frame (selected-frame)) | |
6223 | (setq frame (last-nonminibuffer-frame)) | |
6224 | (window--frame-usable-p frame) | |
6225 | (not (frame-parameter frame 'unsplittable)))) | |
6226 | ;; Attempt to split largest or least recently used window. | |
6227 | (setq window (or (window--try-to-split-window | |
5938d519 | 6228 | (get-largest-window frame t) alist) |
89894cd8 | 6229 | (window--try-to-split-window |
5938d519 MR |
6230 | (get-lru-window frame t) alist)))) |
6231 | (prog1 (window--display-buffer | |
6232 | buffer window 'window alist display-buffer-mark-dedicated) | |
90749b53 CY |
6233 | (unless (cdr (assq 'inhibit-switch-frame alist)) |
6234 | (window--maybe-raise-frame (window-frame window))))))) | |
89894cd8 | 6235 | |
8319e0bf CY |
6236 | (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist) |
6237 | "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'. | |
6238 | ||
6239 | If `pop-up-frames' is non-nil (and not `graphic-only' on a | |
6240 | text-only terminal), try with `display-buffer-pop-up-frame'. | |
6241 | ||
6242 | If that cannot be done, and `pop-up-windows' is non-nil, try | |
6243 | again with `display-buffer-pop-up-window'." | |
6244 | (or (and (if (eq pop-up-frames 'graphic-only) | |
6245 | (display-graphic-p) | |
6246 | pop-up-frames) | |
6247 | (display-buffer-pop-up-frame buffer alist)) | |
6248 | (and pop-up-windows | |
6249 | (display-buffer-pop-up-window buffer alist)))) | |
89894cd8 | 6250 | |
5938d519 | 6251 | (defun display-buffer-below-selected (buffer alist) |
78dd6ab1 MR |
6252 | "Try displaying BUFFER in a window below the selected window. |
6253 | This either splits the selected window or reuses the window below | |
6254 | the selected one." | |
6255 | (let (window) | |
6256 | (or (and (not (frame-parameter nil 'unsplittable)) | |
d144ef06 MR |
6257 | (let ((split-height-threshold 0) |
6258 | split-width-threshold) | |
f93cc74f | 6259 | (setq window (window--try-to-split-window (selected-window) alist))) |
78dd6ab1 | 6260 | (window--display-buffer |
5938d519 | 6261 | buffer window 'window alist display-buffer-mark-dedicated)) |
78dd6ab1 MR |
6262 | (and (setq window (window-in-direction 'below)) |
6263 | (not (window-dedicated-p window)) | |
6264 | (window--display-buffer | |
5938d519 | 6265 | buffer window 'reuse alist display-buffer-mark-dedicated))))) |
8e17c9ba | 6266 | |
5938d519 | 6267 | (defun display-buffer-at-bottom (buffer alist) |
735135f9 | 6268 | "Try displaying BUFFER in a window at the bottom of the selected frame. |
8e17c9ba MR |
6269 | This either splits the window at the bottom of the frame or the |
6270 | frame's root window, or reuses an existing window at the bottom | |
6271 | of the selected frame." | |
6272 | (let (bottom-window window) | |
c660a885 MR |
6273 | (walk-window-tree |
6274 | (lambda (window) (setq bottom-window window)) nil nil 'nomini) | |
8e17c9ba | 6275 | (or (and (not (frame-parameter nil 'unsplittable)) |
00139435 MR |
6276 | (let (split-width-threshold) |
6277 | (setq window (window--try-to-split-window bottom-window alist))) | |
8e17c9ba | 6278 | (window--display-buffer |
5938d519 | 6279 | buffer window 'window alist display-buffer-mark-dedicated)) |
8e17c9ba MR |
6280 | (and (not (frame-parameter nil 'unsplittable)) |
6281 | (setq window | |
6282 | (condition-case nil | |
c660a885 | 6283 | (split-window (window--major-non-side-window)) |
8e17c9ba MR |
6284 | (error nil))) |
6285 | (window--display-buffer | |
5938d519 | 6286 | buffer window 'window alist display-buffer-mark-dedicated)) |
8e17c9ba MR |
6287 | (and (setq window bottom-window) |
6288 | (not (window-dedicated-p window)) | |
6289 | (window--display-buffer | |
5938d519 | 6290 | buffer window 'reuse alist display-buffer-mark-dedicated))))) |
78dd6ab1 | 6291 | |
fa2bcf43 MR |
6292 | (defun display-buffer-in-previous-window (buffer alist) |
6293 | "Display BUFFER in a window previously showing it. | |
6294 | If ALIST has a non-nil `inhibit-same-window' entry, the selected | |
6295 | window is not eligible for reuse. | |
6296 | ||
6297 | If ALIST contains a `reusable-frames' entry, its value determines | |
6298 | which frames to search for a reusable window: | |
6299 | nil -- the selected frame (actually the last non-minibuffer frame) | |
6300 | A frame -- just that frame | |
6301 | `visible' -- all visible frames | |
6302 | 0 -- all frames on the current terminal | |
6303 | t -- all frames. | |
6304 | ||
6305 | If ALIST contains no `reusable-frames' entry, search just the | |
6306 | selected frame if `display-buffer-reuse-frames' and | |
6307 | `pop-up-frames' are both nil; search all frames on the current | |
6308 | terminal if either of those variables is non-nil. | |
6309 | ||
6310 | If ALIST has a `previous-window' entry, the window specified by | |
6311 | that entry will override any other window found by the methods | |
6312 | above, even if that window never showed BUFFER before." | |
6313 | (let* ((alist-entry (assq 'reusable-frames alist)) | |
6314 | (inhibit-same-window | |
6315 | (cdr (assq 'inhibit-same-window alist))) | |
6316 | (frames (cond | |
6317 | (alist-entry (cdr alist-entry)) | |
6318 | ((if (eq pop-up-frames 'graphic-only) | |
6319 | (display-graphic-p) | |
6320 | pop-up-frames) | |
6321 | 0) | |
6322 | (display-buffer-reuse-frames 0) | |
6323 | (t (last-nonminibuffer-frame)))) | |
9d3aa82c | 6324 | best-window second-best-window window) |
fa2bcf43 MR |
6325 | ;; Scan windows whether they have shown the buffer recently. |
6326 | (catch 'best | |
6327 | (dolist (window (window-list-1 (frame-first-window) 'nomini frames)) | |
6328 | (when (and (assq buffer (window-prev-buffers window)) | |
6329 | (not (window-dedicated-p window))) | |
6330 | (if (eq window (selected-window)) | |
6331 | (unless inhibit-same-window | |
6332 | (setq second-best-window window)) | |
6333 | (setq best-window window) | |
6334 | (throw 'best t))))) | |
6335 | ;; When ALIST has a `previous-window' entry, that entry may override | |
6336 | ;; anything we found so far. | |
6337 | (when (and (setq window (cdr (assq 'previous-window alist))) | |
6338 | (window-live-p window) | |
6339 | (not (window-dedicated-p window))) | |
6340 | (if (eq window (selected-window)) | |
6341 | (unless inhibit-same-window | |
6342 | (setq second-best-window window)) | |
6343 | (setq best-window window))) | |
6344 | ;; Return best or second best window found. | |
6345 | (when (setq window (or best-window second-best-window)) | |
5938d519 | 6346 | (window--display-buffer buffer window 'reuse alist)))) |
fa2bcf43 | 6347 | |
89894cd8 CY |
6348 | (defun display-buffer-use-some-window (buffer alist) |
6349 | "Display BUFFER in an existing window. | |
6350 | Search for a usable window, set that window to the buffer, and | |
90749b53 CY |
6351 | return the window. If no suitable window is found, return nil. |
6352 | ||
6353 | If ALIST has a non-nil `inhibit-switch-frame' entry, then in the | |
6354 | event that a window in another frame is chosen, avoid raising | |
6355 | that frame." | |
89894cd8 | 6356 | (let* ((not-this-window (cdr (assq 'inhibit-same-window alist))) |
89894cd8 CY |
6357 | (frame (or (window--frame-usable-p (selected-frame)) |
6358 | (window--frame-usable-p (last-nonminibuffer-frame)))) | |
51a5f9d8 MR |
6359 | (window |
6360 | ;; Reuse an existing window. | |
6361 | (or (get-lru-window frame nil not-this-window) | |
6362 | (let ((window (get-buffer-window buffer 'visible))) | |
6363 | (unless (and not-this-window | |
6364 | (eq window (selected-window))) | |
6365 | window)) | |
6366 | (get-largest-window 'visible nil not-this-window) | |
6367 | (let ((window (get-buffer-window buffer 0))) | |
6368 | (unless (and not-this-window | |
6369 | (eq window (selected-window))) | |
6370 | window)) | |
880e6158 MR |
6371 | (get-largest-window 0 nil not-this-window))) |
6372 | (quit-restore (and (window-live-p window) | |
6373 | (window-parameter window 'quit-restore))) | |
6374 | (quad (nth 1 quit-restore))) | |
90749b53 | 6375 | (when (window-live-p window) |
880e6158 MR |
6376 | ;; If the window was used by `display-buffer' before, try to |
6377 | ;; resize it to its old height but don't signal an error. | |
6378 | (when (and (listp quad) | |
6379 | (integerp (nth 3 quad)) | |
6380 | (/= (nth 3 quad) (window-total-height window))) | |
6381 | (condition-case nil | |
6382 | (window-resize window (- (nth 3 quad) (window-total-height window))) | |
6383 | (error nil))) | |
6384 | ||
9aba119d | 6385 | (prog1 |
5938d519 | 6386 | (window--display-buffer buffer window 'reuse alist) |
9aba119d | 6387 | (window--even-window-heights window) |
90749b53 CY |
6388 | (unless (cdr (assq 'inhibit-switch-frame alist)) |
6389 | (window--maybe-raise-frame (window-frame window))))))) | |
437014c8 | 6390 | |
2d6b6005 | 6391 | (defun display-buffer-no-window (_buffer alist) |
9139632a JL |
6392 | "Display BUFFER in no window. |
6393 | If ALIST has a non-nil `allow-no-window' entry, then don't display | |
6394 | a window at all. This makes possible to override the default action | |
6395 | and avoid displaying the buffer. It is assumed that when the caller | |
6396 | specifies a non-nil `allow-no-window' then it can handle a nil value | |
6397 | returned from `display-buffer' in this case." | |
6398 | (when (cdr (assq 'allow-no-window alist)) | |
6399 | 'fail)) | |
6400 | ||
437014c8 | 6401 | ;;; Display + selection commands: |
c3313451 CY |
6402 | (defun pop-to-buffer (buffer &optional action norecord) |
6403 | "Select buffer BUFFER in some window, preferably a different one. | |
6404 | BUFFER may be a buffer, a string (a buffer name), or nil. If it | |
6405 | is a string not naming an existent buffer, create a buffer with | |
6406 | that name. If BUFFER is nil, choose some other buffer. Return | |
6407 | the buffer. | |
6408 | ||
6409 | This uses `display-buffer' as a subroutine. The optional ACTION | |
6410 | argument is passed to `display-buffer' as its ACTION argument. | |
6411 | See `display-buffer' for more information. ACTION is t if called | |
6412 | interactively with a prefix argument, which means to pop to a | |
6413 | window other than the selected one even if the buffer is already | |
6414 | displayed in the selected window. | |
6415 | ||
6416 | If the window to show BUFFER is not on the selected | |
f818cd2a MR |
6417 | frame, raise that window's frame and give it input focus. |
6418 | ||
f818cd2a MR |
6419 | Optional third arg NORECORD non-nil means do not put this buffer |
6420 | at the front of the list of recently selected ones." | |
c3313451 CY |
6421 | (interactive (list (read-buffer "Pop to buffer: " (other-buffer)) |
6422 | (if current-prefix-arg t))) | |
8319e0bf | 6423 | (setq buffer (window-normalize-buffer-to-switch-to buffer)) |
c660a885 MR |
6424 | ;; This should be done by `select-window' below. |
6425 | ;; (set-buffer buffer) | |
cb882333 | 6426 | (let* ((old-frame (selected-frame)) |
8319e0bf | 6427 | (window (display-buffer buffer action)) |
c3313451 | 6428 | (frame (window-frame window))) |
97adfb97 CY |
6429 | ;; If we chose another frame, make sure it gets input focus. |
6430 | (unless (eq frame old-frame) | |
c3313451 | 6431 | (select-frame-set-input-focus frame norecord)) |
97adfb97 CY |
6432 | ;; Make sure new window is selected (Bug#8615), (Bug#6954). |
6433 | (select-window window norecord) | |
c3313451 | 6434 | buffer)) |
f818cd2a | 6435 | |
72258fe5 CY |
6436 | (defun pop-to-buffer-same-window (buffer &optional norecord) |
6437 | "Select buffer BUFFER in some window, preferably the same one. | |
72258fe5 CY |
6438 | BUFFER may be a buffer, a string (a buffer name), or nil. If it |
6439 | is a string not naming an existent buffer, create a buffer with | |
6440 | that name. If BUFFER is nil, choose some other buffer. Return | |
6441 | the buffer. | |
6442 | ||
6c8413fc MR |
6443 | Optional argument NORECORD, if non-nil means do not put this |
6444 | buffer at the front of the list of recently selected ones. | |
6445 | ||
6446 | Unlike `pop-to-buffer', this function prefers using the selected | |
6447 | window over popping up a new window or frame." | |
24510c22 | 6448 | (pop-to-buffer buffer display-buffer--same-window-action norecord)) |
72258fe5 | 6449 | |
f818cd2a MR |
6450 | (defun read-buffer-to-switch (prompt) |
6451 | "Read the name of a buffer to switch to, prompting with PROMPT. | |
e1dbe924 | 6452 | Return the name of the buffer as a string. |
f818cd2a MR |
6453 | |
6454 | This function is intended for the `switch-to-buffer' family of | |
6455 | commands since these need to omit the name of the current buffer | |
6456 | from the list of completions and default values." | |
6457 | (let ((rbts-completion-table (internal-complete-buffer-except))) | |
6458 | (minibuffer-with-setup-hook | |
6459 | (lambda () | |
6460 | (setq minibuffer-completion-table rbts-completion-table) | |
6461 | ;; Since rbts-completion-table is built dynamically, we | |
6462 | ;; can't just add it to the default value of | |
6463 | ;; icomplete-with-completion-tables, so we add it | |
6464 | ;; here manually. | |
6465 | (if (and (boundp 'icomplete-with-completion-tables) | |
6466 | (listp icomplete-with-completion-tables)) | |
6467 | (set (make-local-variable 'icomplete-with-completion-tables) | |
6468 | (cons rbts-completion-table | |
6469 | icomplete-with-completion-tables)))) | |
6470 | (read-buffer prompt (other-buffer (current-buffer)) | |
6471 | (confirm-nonexistent-file-or-buffer))))) | |
6472 | ||
6473 | (defun window-normalize-buffer-to-switch-to (buffer-or-name) | |
6474 | "Normalize BUFFER-OR-NAME argument of buffer switching functions. | |
6475 | If BUFFER-OR-NAME is nil, return the buffer returned by | |
6476 | `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME | |
6477 | exists, return that buffer. If no such buffer exists, create a | |
6478 | buffer with the name BUFFER-OR-NAME and return that buffer." | |
6479 | (if buffer-or-name | |
6480 | (or (get-buffer buffer-or-name) | |
6481 | (let ((buffer (get-buffer-create buffer-or-name))) | |
6482 | (set-buffer-major-mode buffer) | |
6483 | buffer)) | |
6484 | (other-buffer))) | |
6485 | ||
9d7f027b MR |
6486 | (defcustom switch-to-buffer-preserve-window-point nil |
6487 | "If non-nil, `switch-to-buffer' tries to preserve `window-point'. | |
6488 | If this is nil, `switch-to-buffer' displays the buffer at that | |
6489 | buffer's `point'. If this is `already-displayed', it tries to | |
791ef5f8 | 6490 | display the buffer at its previous position in the selected |
9d7f027b MR |
6491 | window, provided the buffer is currently displayed in some other |
6492 | window on any visible or iconified frame. If this is t, it | |
6493 | unconditionally tries to display the buffer at its previous | |
6494 | position in the selected window. | |
6495 | ||
43bcfda6 MR |
6496 | This variable is ignored if the buffer is already displayed in |
6497 | the selected window or never appeared in it before, or if | |
9d7f027b MR |
6498 | `switch-to-buffer' calls `pop-to-buffer' to display the buffer." |
6499 | :type '(choice | |
6500 | (const :tag "Never" nil) | |
6501 | (const :tag "If already displayed elsewhere" already-displayed) | |
6502 | (const :tag "Always" t)) | |
6503 | :group 'windows | |
6504 | :version "24.3") | |
6505 | ||
f818cd2a | 6506 | (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window) |
d1c0cddf SM |
6507 | "Display buffer BUFFER-OR-NAME in the selected window. |
6508 | ||
6509 | WARNING: This is NOT the way to work on another buffer temporarily | |
6510 | within a Lisp program! Use `set-buffer' instead. That avoids | |
6511 | messing with the window-buffer correspondences. | |
6512 | ||
cee2e90d CY |
6513 | If the selected window cannot display the specified |
6514 | buffer (e.g. if it is a minibuffer window or strongly dedicated | |
6515 | to another buffer), call `pop-to-buffer' to select the buffer in | |
6516 | another window. | |
6517 | ||
6518 | If called interactively, read the buffer name using the | |
f818cd2a MR |
6519 | minibuffer. The variable `confirm-nonexistent-file-or-buffer' |
6520 | determines whether to request confirmation before creating a new | |
6521 | buffer. | |
6522 | ||
cee2e90d CY |
6523 | BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil. |
6524 | If BUFFER-OR-NAME is a string that does not identify an existing | |
6525 | buffer, create a buffer with that name. If BUFFER-OR-NAME is | |
6526 | nil, switch to the buffer returned by `other-buffer'. | |
6527 | ||
6528 | If optional argument NORECORD is non-nil, do not put the buffer | |
6529 | at the front of the buffer list, and do not make the window | |
6530 | displaying it the most recently selected one. | |
6531 | ||
6532 | If optional argument FORCE-SAME-WINDOW is non-nil, the buffer | |
6533 | must be displayed in the selected window; if that is impossible, | |
6534 | signal an error rather than calling `pop-to-buffer'. | |
f818cd2a | 6535 | |
9d7f027b MR |
6536 | The option `switch-to-buffer-preserve-window-point' can be used |
6537 | to make the buffer appear at its last position in the selected | |
6538 | window. | |
6539 | ||
f818cd2a MR |
6540 | Return the buffer switched to." |
6541 | (interactive | |
acc825c5 | 6542 | (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window)) |
f818cd2a | 6543 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) |
24510c22 SM |
6544 | (cond |
6545 | ;; Don't call set-window-buffer if it's not needed since it | |
6546 | ;; might signal an error (e.g. if the window is dedicated). | |
6547 | ((eq buffer (window-buffer))) | |
6548 | ((window-minibuffer-p) | |
6549 | (if force-same-window | |
71873e2b | 6550 | (user-error "Cannot switch buffers in minibuffer window") |
24510c22 SM |
6551 | (pop-to-buffer buffer norecord))) |
6552 | ((eq (window-dedicated-p) t) | |
6553 | (if force-same-window | |
71873e2b | 6554 | (user-error "Cannot switch buffers in a dedicated window") |
24510c22 | 6555 | (pop-to-buffer buffer norecord))) |
9d7f027b MR |
6556 | (t |
6557 | (let* ((entry (assq buffer (window-prev-buffers))) | |
6558 | (displayed (and (eq switch-to-buffer-preserve-window-point | |
6559 | 'already-displayed) | |
6560 | (get-buffer-window buffer 0)))) | |
6561 | (set-window-buffer nil buffer) | |
6562 | (when (and entry | |
6563 | (or (eq switch-to-buffer-preserve-window-point t) | |
6564 | displayed)) | |
6565 | ;; Try to restore start and point of buffer in the selected | |
6566 | ;; window (Bug#4041). | |
6567 | (set-window-start (selected-window) (nth 1 entry) t) | |
6568 | (set-window-point nil (nth 2 entry)))))) | |
24510c22 SM |
6569 | |
6570 | (unless norecord | |
6571 | (select-window (selected-window))) | |
6572 | (set-buffer buffer))) | |
f818cd2a MR |
6573 | |
6574 | (defun switch-to-buffer-other-window (buffer-or-name &optional norecord) | |
6575 | "Select the buffer specified by BUFFER-OR-NAME in another window. | |
382c953b | 6576 | BUFFER-OR-NAME may be a buffer, a string (a buffer name), or |
f818cd2a MR |
6577 | nil. Return the buffer switched to. |
6578 | ||
6579 | If called interactively, prompt for the buffer name using the | |
6580 | minibuffer. The variable `confirm-nonexistent-file-or-buffer' | |
6581 | determines whether to request confirmation before creating a new | |
6582 | buffer. | |
6583 | ||
6584 | If BUFFER-OR-NAME is a string and does not identify an existing | |
6585 | buffer, create a new buffer with that name. If BUFFER-OR-NAME is | |
6586 | nil, switch to the buffer returned by `other-buffer'. | |
6587 | ||
6588 | Optional second argument NORECORD non-nil means do not put this | |
6589 | buffer at the front of the list of recently selected ones. | |
6590 | ||
6591 | This uses the function `display-buffer' as a subroutine; see its | |
6592 | documentation for additional customization information." | |
6593 | (interactive | |
6594 | (list (read-buffer-to-switch "Switch to buffer in other window: "))) | |
8319e0bf CY |
6595 | (let ((pop-up-windows t)) |
6596 | (pop-to-buffer buffer-or-name t norecord))) | |
f818cd2a MR |
6597 | |
6598 | (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord) | |
6599 | "Switch to buffer BUFFER-OR-NAME in another frame. | |
382c953b | 6600 | BUFFER-OR-NAME may be a buffer, a string (a buffer name), or |
f818cd2a MR |
6601 | nil. Return the buffer switched to. |
6602 | ||
6603 | If called interactively, prompt for the buffer name using the | |
6604 | minibuffer. The variable `confirm-nonexistent-file-or-buffer' | |
6605 | determines whether to request confirmation before creating a new | |
6606 | buffer. | |
6607 | ||
6608 | If BUFFER-OR-NAME is a string and does not identify an existing | |
6609 | buffer, create a new buffer with that name. If BUFFER-OR-NAME is | |
6610 | nil, switch to the buffer returned by `other-buffer'. | |
6611 | ||
6612 | Optional second arg NORECORD non-nil means do not put this | |
6613 | buffer at the front of the list of recently selected ones. | |
6614 | ||
6615 | This uses the function `display-buffer' as a subroutine; see its | |
6616 | documentation for additional customization information." | |
6617 | (interactive | |
6618 | (list (read-buffer-to-switch "Switch to buffer in other frame: "))) | |
8319e0bf | 6619 | (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord)) |
3c448ab6 MR |
6620 | \f |
6621 | (defun set-window-text-height (window height) | |
9992ea0c | 6622 | "Set the height in lines of the text display area of WINDOW to HEIGHT. |
85c2386b MR |
6623 | WINDOW must be a live window and defaults to the selected one. |
6624 | HEIGHT doesn't include the mode line or header line, if any, or | |
6625 | any partial-height lines in the text display area. | |
3c448ab6 MR |
6626 | |
6627 | Note that the current implementation of this function cannot | |
6628 | always set the height exactly, but attempts to be conservative, | |
6629 | by allocating more lines than are actually needed in the case | |
6630 | where some error may be present." | |
447f16b8 | 6631 | (setq window (window-normalize-window window t)) |
3c448ab6 MR |
6632 | (let ((delta (- height (window-text-height window)))) |
6633 | (unless (zerop delta) | |
6634 | ;; Setting window-min-height to a value like 1 can lead to very | |
6635 | ;; bizarre displays because it also allows Emacs to make *other* | |
37269466 CY |
6636 | ;; windows one line tall, which means that there's no more space |
6637 | ;; for the mode line. | |
6638 | (let ((window-min-height (min 2 height))) | |
d615d6d2 | 6639 | (window-resize window delta))))) |
3c448ab6 | 6640 | |
6198ccd0 MR |
6641 | (defun enlarge-window-horizontally (delta) |
6642 | "Make selected window DELTA columns wider. | |
3c448ab6 MR |
6643 | Interactively, if no argument is given, make selected window one |
6644 | column wider." | |
6645 | (interactive "p") | |
6198ccd0 | 6646 | (enlarge-window delta t)) |
3c448ab6 | 6647 | |
6198ccd0 MR |
6648 | (defun shrink-window-horizontally (delta) |
6649 | "Make selected window DELTA columns narrower. | |
3c448ab6 MR |
6650 | Interactively, if no argument is given, make selected window one |
6651 | column narrower." | |
6652 | (interactive "p") | |
6198ccd0 | 6653 | (shrink-window delta t)) |
3c448ab6 MR |
6654 | |
6655 | (defun count-screen-lines (&optional beg end count-final-newline window) | |
6656 | "Return the number of screen lines in the region. | |
6657 | The number of screen lines may be different from the number of actual lines, | |
6658 | due to line breaking, display table, etc. | |
6659 | ||
6660 | Optional arguments BEG and END default to `point-min' and `point-max' | |
6661 | respectively. | |
6662 | ||
6663 | If region ends with a newline, ignore it unless optional third argument | |
6664 | COUNT-FINAL-NEWLINE is non-nil. | |
6665 | ||
6666 | The optional fourth argument WINDOW specifies the window used for obtaining | |
6667 | parameters such as width, horizontal scrolling, and so on. The default is | |
6668 | to use the selected window's parameters. | |
6669 | ||
6670 | Like `vertical-motion', `count-screen-lines' always uses the current buffer, | |
6671 | regardless of which buffer is displayed in WINDOW. This makes possible to use | |
6672 | `count-screen-lines' in any buffer, whether or not it is currently displayed | |
6673 | in some window." | |
6674 | (unless beg | |
6675 | (setq beg (point-min))) | |
6676 | (unless end | |
6677 | (setq end (point-max))) | |
6678 | (if (= beg end) | |
6679 | 0 | |
6680 | (save-excursion | |
6681 | (save-restriction | |
6682 | (widen) | |
6683 | (narrow-to-region (min beg end) | |
6684 | (if (and (not count-final-newline) | |
6685 | (= ?\n (char-before (max beg end)))) | |
6686 | (1- (max beg end)) | |
6687 | (max beg end))) | |
6688 | (goto-char (point-min)) | |
6689 | (1+ (vertical-motion (buffer-size) window)))))) | |
6690 | ||
6198ccd0 | 6691 | (defun window-buffer-height (window) |
85c2386b MR |
6692 | "Return the height (in screen lines) of the buffer that WINDOW is displaying. |
6693 | WINDOW must be a live window and defaults to the selected one." | |
6694 | (setq window (window-normalize-window window t)) | |
6198ccd0 MR |
6695 | (with-current-buffer (window-buffer window) |
6696 | (max 1 | |
6697 | (count-screen-lines (point-min) (point-max) | |
6698 | ;; If buffer ends with a newline, ignore it when | |
6699 | ;; counting height unless point is after it. | |
6700 | (eobp) | |
6701 | window)))) | |
6702 | ||
880e6158 MR |
6703 | ;;; Resizing windows and frames to fit their contents exactly. |
6704 | (defcustom fit-window-to-buffer-horizontally nil | |
6705 | "Non-nil means `fit-window-to-buffer' can resize windows horizontally. | |
6706 | If this is nil, `fit-window-to-buffer' never resizes windows | |
6707 | horizontally. If this is `only', it can resize windows | |
6708 | horizontally only. Any other value means `fit-window-to-buffer' | |
6709 | can resize windows in both dimensions." | |
6710 | :type 'boolean | |
6711 | :version "24.4" | |
6712 | :group 'help) | |
6713 | ||
6714 | ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes | |
6715 | ;; counting title bar and outer borders. | |
5938d519 | 6716 | (defcustom fit-frame-to-buffer nil |
880e6158 MR |
6717 | "Non-nil means `fit-frame-to-buffer' can fit a frame to its buffer. |
6718 | A frame is fit if and only if its root window is a live window | |
6719 | and this option is non-nil. If this is `horizontally', frames | |
6720 | are resized horizontally only. If this is `vertically', frames | |
6721 | are resized vertically only. Any other non-nil value means | |
6722 | frames can be resized in both dimensions. See also | |
6723 | `fit-frame-to-buffer-margins' and `fit-frame-to-buffer-sizes'. | |
6724 | ||
6725 | If this is non-nil and a window is the only window of its frame, | |
6726 | `fit-window-to-buffer' will invoke `fit-frame-to-buffer' to fit | |
6727 | the frame to its buffer." | |
5938d519 | 6728 | :type 'boolean |
880e6158 | 6729 | :version "24.4" |
5938d519 MR |
6730 | :group 'help) |
6731 | ||
880e6158 MR |
6732 | (defcustom fit-frame-to-buffer-margins '(nil nil nil nil) |
6733 | "Margins around frame for `fit-frame-to-buffer'. | |
6734 | This list specifies the numbers of pixels to be left free on the | |
6735 | left, above, the right, and below a frame that shall be fit to | |
6736 | its buffer. The value specified here can be overridden for a | |
6737 | specific frame by that frame's `fit-frame-to-buffer-margins' | |
6738 | parameter, if present. | |
6739 | ||
6740 | This variable controls how fitting a frame to the size of its | |
6741 | buffer coordinates with the size of your display. If you don't | |
6742 | specify a value here, the size of the display's workarea is used. | |
6743 | ||
6744 | See also `fit-frame-to-buffer-sizes'." | |
6745 | :version "24.4" | |
6746 | :type '(list | |
6747 | (choice | |
6748 | :tag "Left" | |
6749 | :value nil | |
6750 | :format "%[LeftMargin%] %v " | |
6751 | (const :tag "None" :format "%t" nil) | |
6752 | (integer :tag "Pixels" :size 5)) | |
6753 | (choice | |
6754 | :tag "Top" | |
6755 | :value nil | |
6756 | :format "%[TopMargin%] %v " | |
6757 | (const :tag "None" :format "%t" nil) | |
6758 | (integer :tag "Pixels" :size 5)) | |
6759 | (choice | |
6760 | :tag "Right" | |
6761 | :value nil | |
6762 | :format "%[RightMargin%] %v " | |
6763 | (const :tag "None" :format "%t" nil) | |
6764 | (integer :tag "Pixels" :size 5)) | |
6765 | (choice | |
6766 | :tag "Bottom" | |
6767 | :value nil | |
6768 | :format "%[BottomMargin%] %v " | |
6769 | (const :tag "None" :format "%t" nil) | |
6770 | (integer :tag "Pixels" :size 5))) | |
6771 | :group 'help) | |
6772 | ||
6773 | (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil) | |
6774 | "Size boundaries of frame for `fit-frame-to-buffer'. | |
6775 | This list specifies the total maximum and minimum lines and | |
6776 | maximum and minimum columns of the root window of any frame that | |
6777 | shall be fit to its buffer. If any of these values is non-nil, | |
6778 | it overrides the corresponding argument of `fit-frame-to-buffer'. | |
6779 | ||
6780 | On window systems where the menubar can wrap, fitting a frame to | |
6781 | its buffer may swallow the last line(s). Specifying an | |
6782 | appropriate minimum width value here can avoid such wrapping. | |
6783 | ||
6784 | See also `fit-frame-to-buffer-margins'." | |
6785 | :version "24.4" | |
6786 | :type '(list | |
6787 | (choice | |
6788 | :tag "Maximum Height" | |
6789 | :value nil | |
6790 | :format "%[MaxHeight%] %v " | |
6791 | (const :tag "None" :format "%t" nil) | |
6792 | (integer :tag "Lines" :size 5)) | |
6793 | (choice | |
6794 | :tag "Minimum Height" | |
6795 | :value nil | |
6796 | :format "%[MinHeight%] %v " | |
6797 | (const :tag "None" :format "%t" nil) | |
6798 | (integer :tag "Lines" :size 5)) | |
6799 | (choice | |
6800 | :tag "Maximum Width" | |
6801 | :value nil | |
6802 | :format "%[MaxWidth%] %v " | |
6803 | (const :tag "None" :format "%t" nil) | |
6804 | (integer :tag "Columns" :size 5)) | |
6805 | (choice | |
6806 | :tag "Minimum Width" | |
6807 | :value nil | |
6808 | :format "%[MinWidth%] %v\n" | |
6809 | (const :tag "None" :format "%t" nil) | |
6810 | (integer :tag "Columns" :size 5))) | |
6811 | :group 'help) | |
5938d519 | 6812 | |
e740f9d2 GM |
6813 | (declare-function x-display-pixel-height "xfns.c" (&optional terminal)) |
6814 | (declare-function tool-bar-lines-needed "xdisp.c" (&optional frame)) | |
6815 | ||
880e6158 MR |
6816 | (defun window--sanitize-margin (margin left right) |
6817 | "Return MARGIN if it's a number between LEFT and RIGHT." | |
6818 | (when (and (numberp margin) | |
6819 | (<= left (- right margin)) (<= margin right)) | |
6820 | margin)) | |
5938d519 | 6821 | |
880e6158 MR |
6822 | (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width) |
6823 | "Adjust size of FRAME to display the contents of its buffer exactly. | |
6824 | FRAME can be any live frame and defaults to the selected one. | |
6825 | Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT, | |
6826 | MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of | |
6827 | FRAME's root window. | |
6828 | ||
6829 | The option `fit-frame-to-buffer' controls whether this function | |
6830 | has any effect. New position and size of FRAME are additionally | |
6831 | determined by the options `fit-frame-to-buffer-sizes' and | |
6832 | `fit-frame-to-buffer-margins' or the corresponding parameters of | |
6833 | FRAME." | |
5938d519 | 6834 | (interactive) |
e740f9d2 GM |
6835 | (or (fboundp 'x-display-pixel-height) |
6836 | (user-error "Cannot resize frame in non-graphic Emacs")) | |
5938d519 | 6837 | (setq frame (window-normalize-frame frame)) |
880e6158 MR |
6838 | (when (and (window-live-p (frame-root-window frame)) |
6839 | fit-frame-to-buffer | |
6840 | (or (not window-size-fixed) | |
6841 | (and (eq window-size-fixed 'height) | |
6842 | (not (eq fit-frame-to-buffer 'vertically))) | |
6843 | (and (eq window-size-fixed 'width) | |
6844 | (not (eq fit-frame-to-buffer 'horizontally))))) | |
6845 | (with-selected-window (frame-root-window frame) | |
6846 | (let* ((window (frame-root-window frame)) | |
6847 | (char-width (frame-char-width)) | |
6848 | (char-height (frame-char-height)) | |
6849 | (monitor-attributes (car (display-monitor-attributes-list | |
6850 | (frame-parameter frame 'display)))) | |
6851 | (geometry (cdr (assq 'geometry monitor-attributes))) | |
6852 | (display-width (- (nth 2 geometry) (nth 0 geometry))) | |
6853 | (display-height (- (nth 3 geometry) (nth 1 geometry))) | |
6854 | (workarea (cdr (assq 'workarea monitor-attributes))) | |
6855 | ;; Handle margins. | |
6856 | (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins) | |
6857 | fit-frame-to-buffer-margins)) | |
6858 | (left-margin (or (window--sanitize-margin | |
6859 | (nth 0 margins) 0 display-width) | |
6860 | (nth 0 workarea))) | |
6861 | (top-margin (or (window--sanitize-margin | |
6862 | (nth 1 margins) 0 display-height) | |
6863 | (nth 1 workarea))) | |
6864 | (workarea-width (nth 2 workarea)) | |
6865 | (right-margin (or (window--sanitize-margin | |
6866 | (nth 2 margins) left-margin display-width) | |
6867 | (+ left-margin workarea-width))) | |
6868 | (workarea-height (nth 3 workarea)) | |
6869 | (bottom-margin (or (window--sanitize-margin | |
6870 | (nth 3 margins) top-margin display-height) | |
6871 | (+ top-margin workarea-height))) | |
6872 | ;; The pixel width of FRAME (which does not include the | |
6873 | ;; window manager's decorations). | |
6874 | (frame-width (frame-pixel-width)) | |
6875 | ;; The pixel width of the body of FRAME's root window. | |
6876 | (window-body-width (window-body-width nil t)) | |
6877 | ;; The difference in pixels between total and body width of | |
6878 | ;; FRAME's window. | |
6879 | (window-extra-width (- (window-pixel-width) window-body-width)) | |
6880 | ;; The difference in pixels between the frame's pixel width | |
6881 | ;; and the window's body width. This is the space we can't | |
6882 | ;; use for fitting. | |
6883 | (extra-width (- frame-width window-body-width)) | |
6884 | ;; The maximum width we can use for fitting. | |
6885 | (fit-width (- workarea-width extra-width)) | |
6886 | ;; The pixel position of FRAME's left border. We usually | |
6887 | ;; try to leave this alone. | |
6888 | (left | |
6889 | (let ((left (frame-parameter nil 'left))) | |
6890 | (if (consp left) | |
6891 | (funcall (car left) (cadr left)) | |
6892 | left))) | |
6893 | ;; The pixel height of FRAME (which does not include title | |
6894 | ;; line, decorations, and sometimes neither the menu nor | |
6895 | ;; the toolbar). | |
6896 | (frame-height (frame-pixel-height)) | |
6897 | ;; The pixel height of FRAME's root window (we don't care | |
6898 | ;; about the window's body height since the return value of | |
6899 | ;; `window-text-pixel-size' includes header and mode line). | |
6900 | (window-height (window-pixel-height)) | |
6901 | ;; The difference in pixels between the frame's pixel | |
6902 | ;; height and the window's height. | |
6903 | (extra-height (- frame-height window-height)) | |
6904 | ;; When tool-bar-mode is enabled and we just created a new | |
6905 | ;; frame, reserve lines for toolbar resizing. Needed | |
6906 | ;; because for reasons unknown to me Emacs (1) reserves one | |
6907 | ;; line for the toolbar when making the initial frame and | |
6908 | ;; toolbars are enabled, and (2) later adds the remaining | |
6909 | ;; lines needed. Our code runs IN BETWEEN (1) and (2). | |
6910 | ;; YMMV when you're on a system that behaves differently. | |
6911 | (toolbar-extra-height | |
6912 | (let ((quit-restore (window-parameter window 'quit-restore)) | |
6913 | ;; This may have to change when we allow arbitrary | |
6914 | ;; pixel height toolbars. | |
6915 | (lines (tool-bar-height))) | |
6916 | (* char-height | |
6917 | (if (and quit-restore (eq (car quit-restore) 'frame) | |
6918 | (not (zerop lines))) | |
6919 | (1- lines) | |
6920 | 0)))) | |
6921 | ;; The maximum height we can use for fitting. | |
6922 | (fit-height | |
6923 | (- workarea-height extra-height toolbar-extra-height)) | |
6924 | ;; The pixel position of FRAME's top border. We usually | |
6925 | ;; try to leave this alone. | |
6926 | (top | |
6927 | (let ((top (frame-parameter nil 'top))) | |
6928 | (if (consp top) | |
6929 | (funcall (car top) (cadr top)) | |
6930 | top))) | |
6931 | ;; Sanitize minimum and maximum sizes. | |
6932 | (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes) | |
6933 | fit-frame-to-buffer-sizes)) | |
6934 | (max-height | |
6935 | (cond | |
6936 | ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height)) | |
6937 | ((numberp max-height) (* max-height char-height)))) | |
6938 | (min-height | |
6939 | (cond | |
6940 | ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height)) | |
6941 | ((numberp min-height) (* min-height char-height)))) | |
6942 | (max-width | |
6943 | (cond | |
6944 | ((numberp (nth 2 sizes)) | |
6945 | (- (* (nth 2 sizes) char-width) window-extra-width)) | |
6946 | ((numberp max-width) | |
6947 | (- (* max-width char-width) window-extra-width)))) | |
6948 | (min-width | |
6949 | (cond | |
6950 | ((numberp (nth 3 sizes)) | |
6951 | (- (* (nth 3 sizes) char-width) window-extra-width)) | |
6952 | ((numberp min-width) | |
6953 | (- (* min-width char-width) window-extra-width)))) | |
6954 | ;; Note: Currently, for a new frame the sizes of the header | |
6955 | ;; and mode line may be estimated incorrectly | |
6956 | (value (window-text-pixel-size | |
6957 | nil t t workarea-width workarea-height t)) | |
6958 | (width (+ (car value) (window-right-divider-width))) | |
6959 | (height (+ (cdr value) (window-bottom-divider-width))) | |
6960 | remainder) | |
6961 | (unless window-resize-pixelwise | |
6962 | ;; Round sizes to character sizes. | |
6963 | (setq remainder (% width char-width)) | |
6964 | (unless (zerop remainder) | |
6965 | (setq width (+ width (- char-width remainder)))) | |
6966 | (setq remainder (% height char-height)) | |
6967 | (setq height (+ height (- char-height remainder)))) | |
6968 | ;; Now make sure that we don't get larger than our rounded | |
6969 | ;; maximum lines and columns. | |
6970 | (when (> width fit-width) | |
6971 | (setq width (- fit-width (% fit-width char-width)))) | |
6972 | (when (> height fit-height) | |
6973 | (setq height (- fit-height (% fit-height char-height)))) | |
6974 | ;; Don't change height or width when the window's size is fixed | |
6975 | ;; in either direction. | |
5938d519 | 6976 | (cond |
880e6158 MR |
6977 | ((eq window-size-fixed 'height) |
6978 | (setq height nil)) | |
6979 | ((eq window-size-fixed 'width) | |
6980 | (setq height nil))) | |
6981 | (when width | |
6982 | ;; Fit to maximum and minimum widths. | |
6983 | (when max-width | |
6984 | (setq width (min width max-width))) | |
6985 | (when min-width | |
6986 | (setq width (max width min-width))) | |
6987 | ;; Add extra width. | |
6988 | (setq width (+ width extra-width)) | |
6989 | ;; Preserve right margin. | |
6990 | (let ((right (+ left width extra-width)) | |
6991 | (max-right (- workarea-width right-margin))) | |
6992 | (cond | |
6993 | ((> right max-right) | |
6994 | ;; Move FRAME to left. | |
6995 | (setq left (max 0 (- left (- right max-right))))) | |
6996 | ((< left left-margin) | |
6997 | ;; Move frame to right. | |
6998 | (setq left left-margin))))) | |
6999 | (when height | |
7000 | ;; Fit to maximum and minimum heights. | |
7001 | (when max-height | |
7002 | (setq height (min height max-height))) | |
7003 | (when min-height | |
7004 | (setq height (max height min-height))) | |
7005 | ;; Add extra height. | |
7006 | (setq height (+ height extra-height)) | |
7007 | ;; Preserve bottom and top margins. | |
7008 | (let ((bottom (+ top height extra-height)) | |
7009 | (max-bottom (- workarea-height bottom-margin))) | |
7010 | (cond | |
7011 | ((> bottom max-bottom) | |
7012 | ;; Move FRAME to left. | |
7013 | (setq top (max 0 (- top (- bottom max-bottom))))) | |
7014 | ((< top top-margin) | |
7015 | ;; Move frame down. | |
7016 | (setq top top-margin))))) | |
7017 | ;; Apply changes. | |
7018 | (set-frame-position frame left top) | |
7019 | ;; Clumsily try to translate our calculations to what | |
7020 | ;; `set-frame-size' wants. | |
7021 | (when width | |
7022 | (setq width (- (+ (frame-text-width) width) | |
7023 | extra-width window-body-width))) | |
7024 | (when height | |
7025 | (setq height (- (+ (frame-text-height) height) | |
7026 | extra-height window-height))) | |
7027 | (set-frame-size | |
7028 | frame | |
7029 | (if width | |
7030 | (if window-resize-pixelwise | |
7031 | width | |
7032 | (/ width char-width)) | |
7033 | (frame-text-width)) | |
7034 | (if height | |
7035 | (if window-resize-pixelwise | |
7036 | height | |
7037 | (/ height char-height)) | |
7038 | (frame-text-height)) | |
7039 | window-resize-pixelwise))))) | |
7040 | ||
7041 | (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width) | |
7042 | "Adjust size of WINDOW to display its buffer's contents exactly. | |
85c2386b | 7043 | WINDOW must be a live window and defaults to the selected one. |
6198ccd0 | 7044 | |
880e6158 MR |
7045 | If WINDOW is part of a vertical combination, adjust WINDOW's |
7046 | height. The new height is calculated from the number of lines of | |
7047 | the accessible portion of its buffer. The optional argument | |
7048 | MAX-HEIGHT specifies a maximum height and defaults to the height | |
7049 | of WINDOW's frame. The optional argument MIN-HEIGHT specifies a | |
7050 | minimum height and defaults to `window-min-height'. Both | |
7051 | MAX-HEIGHT and MIN-HEIGHT are specified in lines and include the | |
7052 | mode line and header line, if any. | |
7053 | ||
7054 | If WINDOW is part of a horizontal combination and the value of | |
7055 | the option `fit-window-to-buffer-horizontally' is non-nil, adjust | |
7056 | WINDOW's height. The new width of WINDOW is calculated from the | |
7057 | maximum length of its buffer's lines that follow the current | |
7058 | start position of WINDOW. The optional argument MAX-WIDTH | |
7059 | specifies a maximum width and defaults to the width of WINDOW's | |
7060 | frame. The optional argument MIN-WIDTH specifies a minimum width | |
7061 | and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH | |
7062 | are specified in columns and include fringes, margins and | |
7063 | scrollbars, if any. | |
7064 | ||
7065 | Fit pixelwise if the option `window-resize-pixelwise' is non-nil. | |
7066 | If WINDOW is its frame's root window, then if the option | |
7067 | `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to | |
7068 | adjust the frame's size. | |
6198ccd0 MR |
7069 | |
7070 | Note that even if this function makes WINDOW large enough to show | |
880e6158 MR |
7071 | _all_ parts of its buffer you might not see the first part when |
7072 | WINDOW was scrolled. If WINDOW is resized horizontally, you will | |
7073 | not see the top of its buffer unless WINDOW starts at its minimum | |
7074 | accessible position." | |
f7baca20 | 7075 | (interactive) |
447f16b8 | 7076 | (setq window (window-normalize-window window t)) |
880e6158 MR |
7077 | (if (eq window (frame-root-window window)) |
7078 | (when fit-frame-to-buffer | |
7079 | ;; Fit WINDOW's frame to buffer. | |
7080 | (fit-frame-to-buffer | |
7081 | (window-frame window) | |
7082 | max-height min-height max-width min-width)) | |
6198ccd0 | 7083 | (with-selected-window window |
880e6158 | 7084 | (let* ((pixelwise window-resize-pixelwise) |
880e6158 MR |
7085 | (char-height (frame-char-height)) |
7086 | (char-width (frame-char-width)) | |
880e6158 MR |
7087 | (total-height (window-size window nil pixelwise)) |
7088 | (body-height (window-body-height window pixelwise)) | |
7089 | (body-width (window-body-width window pixelwise)) | |
6198ccd0 | 7090 | (min-height |
880e6158 | 7091 | ;; Sanitize MIN-HEIGHT. |
c5e28e39 MR |
7092 | (if (numberp min-height) |
7093 | ;; Can't get smaller than `window-safe-min-height'. | |
880e6158 MR |
7094 | (max (if pixelwise |
7095 | (* char-height min-height) | |
7096 | min-height) | |
7097 | (if pixelwise | |
7098 | (window-safe-min-pixel-height window) | |
7099 | window-safe-min-height)) | |
c5e28e39 | 7100 | ;; Preserve header and mode line if present. |
880e6158 | 7101 | (window-min-size nil nil t pixelwise))) |
6198ccd0 | 7102 | (max-height |
880e6158 | 7103 | ;; Sanitize MAX-HEIGHT. |
c5e28e39 | 7104 | (if (numberp max-height) |
880e6158 MR |
7105 | (min |
7106 | (+ total-height | |
7107 | (window-max-delta | |
7108 | window nil nil nil nil nil pixelwise)) | |
7109 | (if pixelwise | |
7110 | (* char-height max-height) | |
7111 | max-height)) | |
7112 | (+ total-height (window-max-delta | |
7113 | window nil nil nil nil nil pixelwise)))) | |
7114 | height) | |
7115 | (cond | |
7116 | ;; If WINDOW is vertically combined, try to resize it | |
7117 | ;; vertically. | |
7118 | ((and (not (eq fit-window-to-buffer-horizontally 'only)) | |
7119 | (not (window-size-fixed-p window)) | |
7120 | (window-combined-p)) | |
7121 | ;; Vertically we always want to fit the entire buffer. | |
7122 | ;; WINDOW'S height can't get larger than its frame's pixel | |
7123 | ;; height. Its width remains fixed. | |
7124 | (setq height (+ (cdr (window-text-pixel-size | |
7125 | nil nil t nil (frame-pixel-height) t)) | |
7126 | (window-bottom-divider-width))) | |
7127 | ;; Round height. | |
7128 | (unless pixelwise | |
7129 | (setq height (+ (/ height char-height) | |
7130 | (if (zerop (% height char-height)) 0 1)))) | |
7131 | (unless (= height total-height) | |
7132 | (window-resize-no-error | |
7133 | window | |
7134 | (- (max min-height (min max-height height)) total-height) | |
7135 | nil window pixelwise))) | |
7136 | ;; If WINDOW is horizontally combined, try to resize it | |
7137 | ;; horizontally. | |
7138 | ((and fit-window-to-buffer-horizontally | |
7139 | (not (window-size-fixed-p window t)) | |
7140 | (window-combined-p nil t)) | |
2d6b6005 | 7141 | (let* ((total-width (window-size window nil pixelwise)) |
880e6158 MR |
7142 | (min-width |
7143 | ;; Sanitize MIN-WIDTH. | |
7144 | (if (numberp min-width) | |
7145 | ;; Can't get smaller than `window-safe-min-width'. | |
7146 | (max (if pixelwise | |
7147 | (* char-width min-width) | |
7148 | min-width) | |
7149 | (if pixelwise | |
7150 | (window-safe-min-pixel-width) | |
7151 | window-safe-min-width)) | |
f224e500 | 7152 | ;; Preserve fringes, margins, scrollbars if present. |
880e6158 MR |
7153 | (window-min-size nil nil t pixelwise))) |
7154 | (max-width | |
7155 | ;; Sanitize MAX-WIDTH. | |
7156 | (if (numberp max-width) | |
7157 | (min (+ total-width | |
7158 | (window-max-delta | |
7159 | nil t nil nil nil nil pixelwise)) | |
7160 | (if pixelwise | |
7161 | (* char-width max-width) | |
7162 | max-width)) | |
7163 | (+ total-width (window-max-delta | |
7164 | nil t nil nil nil nil pixelwise)))) | |
7165 | ;; When fitting vertically, assume that WINDOW's start | |
7166 | ;; position remains unaltered. WINDOW can't get wider | |
7167 | ;; than its frame's pixel width, its height remains | |
7168 | ;; unaltered. | |
7169 | (width (+ (car (window-text-pixel-size | |
7170 | nil (window-start) (point-max) | |
7171 | (frame-pixel-width) | |
7172 | ;; Add one char-height to assure that | |
7173 | ;; we're on the safe side. This | |
7174 | ;; overshoots when the first line below | |
7175 | ;; the bottom is wider than the window. | |
7176 | (* body-height | |
7177 | (if pixelwise char-height 1)))) | |
7178 | (window-right-divider-width)))) | |
7179 | (unless pixelwise | |
7180 | (setq width (+ (/ width char-width) | |
7181 | (if (zerop (% width char-width)) 0 1)))) | |
7182 | (unless (= width body-width) | |
7183 | (window-resize-no-error | |
7184 | window | |
7185 | (- (max min-width | |
7186 | (min max-width | |
7187 | (+ total-width (- width body-width)))) | |
7188 | total-width) | |
7189 | t window pixelwise))))))))) | |
ef654460 | 7190 | |
39cffb44 MR |
7191 | (defun window-safely-shrinkable-p (&optional window) |
7192 | "Return t if WINDOW can be shrunk without shrinking other windows. | |
7193 | WINDOW defaults to the selected window." | |
7194 | (with-selected-window (or window (selected-window)) | |
7195 | (let ((edges (window-edges))) | |
39cffb44 MR |
7196 | (or (= (nth 2 edges) (nth 2 (window-edges (previous-window)))) |
7197 | (= (nth 0 edges) (nth 0 (window-edges (next-window)))))))) | |
39cffb44 | 7198 | |
3c448ab6 MR |
7199 | (defun shrink-window-if-larger-than-buffer (&optional window) |
7200 | "Shrink height of WINDOW if its buffer doesn't need so many lines. | |
7201 | More precisely, shrink WINDOW vertically to be as small as | |
7202 | possible, while still showing the full contents of its buffer. | |
85c2386b | 7203 | WINDOW must be a live window and defaults to the selected one. |
3c448ab6 | 7204 | |
6198ccd0 MR |
7205 | Do not shrink WINDOW to less than `window-min-height' lines. Do |
7206 | nothing if the buffer contains more lines than the present window | |
7207 | height, or if some of the window's contents are scrolled out of | |
7208 | view, or if shrinking this window would also shrink another | |
7209 | window, or if the window is the only window of its frame. | |
3c448ab6 MR |
7210 | |
7211 | Return non-nil if the window was shrunk, nil otherwise." | |
7212 | (interactive) | |
447f16b8 | 7213 | (setq window (window-normalize-window window t)) |
6198ccd0 MR |
7214 | ;; Make sure that WINDOW is vertically combined and `point-min' is |
7215 | ;; visible (for whatever reason that's needed). The remaining issues | |
7216 | ;; should be taken care of by `fit-window-to-buffer'. | |
3d8daefe | 7217 | (when (and (window-combined-p window) |
6198ccd0 | 7218 | (pos-visible-in-window-p (point-min) window)) |
880e6158 | 7219 | (fit-window-to-buffer window (window-total-height window)))) |
6198ccd0 | 7220 | \f |
3c448ab6 MR |
7221 | (defun kill-buffer-and-window () |
7222 | "Kill the current buffer and delete the selected window." | |
7223 | (interactive) | |
7224 | (let ((window-to-delete (selected-window)) | |
7225 | (buffer-to-kill (current-buffer)) | |
6198ccd0 | 7226 | (delete-window-hook (lambda () (ignore-errors (delete-window))))) |
3c448ab6 MR |
7227 | (unwind-protect |
7228 | (progn | |
7229 | (add-hook 'kill-buffer-hook delete-window-hook t t) | |
7230 | (if (kill-buffer (current-buffer)) | |
7231 | ;; If `delete-window' failed before, we rerun it to regenerate | |
7232 | ;; the error so it can be seen in the echo area. | |
7233 | (when (eq (selected-window) window-to-delete) | |
7234 | (delete-window)))) | |
7235 | ;; If the buffer is not dead for some reason (probably because | |
7236 | ;; of a `quit' signal), remove the hook again. | |
6198ccd0 MR |
7237 | (ignore-errors |
7238 | (with-current-buffer buffer-to-kill | |
7239 | (remove-hook 'kill-buffer-hook delete-window-hook t)))))) | |
3c448ab6 | 7240 | |
74f806a1 | 7241 | \f |
3c448ab6 MR |
7242 | (defvar recenter-last-op nil |
7243 | "Indicates the last recenter operation performed. | |
0116abbd JL |
7244 | Possible values: `top', `middle', `bottom', integer or float numbers.") |
7245 | ||
7246 | (defcustom recenter-positions '(middle top bottom) | |
7247 | "Cycling order for `recenter-top-bottom'. | |
7248 | A list of elements with possible values `top', `middle', `bottom', | |
7249 | integer or float numbers that define the cycling order for | |
7250 | the command `recenter-top-bottom'. | |
7251 | ||
382c953b | 7252 | Top and bottom destinations are `scroll-margin' lines from the true |
0116abbd JL |
7253 | window top and bottom. Middle redraws the frame and centers point |
7254 | vertically within the window. Integer number moves current line to | |
7255 | the specified absolute window-line. Float number between 0.0 and 1.0 | |
7256 | means the percentage of the screen space from the top. The default | |
7257 | cycling order is middle -> top -> bottom." | |
7258 | :type '(repeat (choice | |
7259 | (const :tag "Top" top) | |
7260 | (const :tag "Middle" middle) | |
7261 | (const :tag "Bottom" bottom) | |
7262 | (integer :tag "Line number") | |
7263 | (float :tag "Percentage"))) | |
7264 | :version "23.2" | |
7265 | :group 'windows) | |
3c448ab6 MR |
7266 | |
7267 | (defun recenter-top-bottom (&optional arg) | |
0116abbd JL |
7268 | "Move current buffer line to the specified window line. |
7269 | With no prefix argument, successive calls place point according | |
7270 | to the cycling order defined by `recenter-positions'. | |
3c448ab6 MR |
7271 | |
7272 | A prefix argument is handled like `recenter': | |
7273 | With numeric prefix ARG, move current line to window-line ARG. | |
0116abbd | 7274 | With plain `C-u', move current line to window center." |
3c448ab6 MR |
7275 | (interactive "P") |
7276 | (cond | |
0116abbd | 7277 | (arg (recenter arg)) ; Always respect ARG. |
3c448ab6 | 7278 | (t |
0116abbd JL |
7279 | (setq recenter-last-op |
7280 | (if (eq this-command last-command) | |
7281 | (car (or (cdr (member recenter-last-op recenter-positions)) | |
7282 | recenter-positions)) | |
7283 | (car recenter-positions))) | |
3c448ab6 MR |
7284 | (let ((this-scroll-margin |
7285 | (min (max 0 scroll-margin) | |
7286 | (truncate (/ (window-body-height) 4.0))))) | |
7287 | (cond ((eq recenter-last-op 'middle) | |
0116abbd | 7288 | (recenter)) |
3c448ab6 | 7289 | ((eq recenter-last-op 'top) |
0116abbd JL |
7290 | (recenter this-scroll-margin)) |
7291 | ((eq recenter-last-op 'bottom) | |
7292 | (recenter (- -1 this-scroll-margin))) | |
7293 | ((integerp recenter-last-op) | |
7294 | (recenter recenter-last-op)) | |
7295 | ((floatp recenter-last-op) | |
7296 | (recenter (round (* recenter-last-op (window-height)))))))))) | |
3c448ab6 MR |
7297 | |
7298 | (define-key global-map [?\C-l] 'recenter-top-bottom) | |
216349f8 | 7299 | |
216349f8 SM |
7300 | (defun move-to-window-line-top-bottom (&optional arg) |
7301 | "Position point relative to window. | |
7302 | ||
0f202d5d | 7303 | With a prefix argument ARG, acts like `move-to-window-line'. |
216349f8 SM |
7304 | |
7305 | With no argument, positions point at center of window. | |
0116abbd JL |
7306 | Successive calls position point at positions defined |
7307 | by `recenter-positions'." | |
216349f8 SM |
7308 | (interactive "P") |
7309 | (cond | |
0116abbd | 7310 | (arg (move-to-window-line arg)) ; Always respect ARG. |
216349f8 | 7311 | (t |
0116abbd JL |
7312 | (setq recenter-last-op |
7313 | (if (eq this-command last-command) | |
7314 | (car (or (cdr (member recenter-last-op recenter-positions)) | |
7315 | recenter-positions)) | |
7316 | (car recenter-positions))) | |
216349f8 SM |
7317 | (let ((this-scroll-margin |
7318 | (min (max 0 scroll-margin) | |
7319 | (truncate (/ (window-body-height) 4.0))))) | |
0f202d5d | 7320 | (cond ((eq recenter-last-op 'middle) |
0116abbd | 7321 | (call-interactively 'move-to-window-line)) |
0f202d5d | 7322 | ((eq recenter-last-op 'top) |
0116abbd JL |
7323 | (move-to-window-line this-scroll-margin)) |
7324 | ((eq recenter-last-op 'bottom) | |
7325 | (move-to-window-line (- -1 this-scroll-margin))) | |
7326 | ((integerp recenter-last-op) | |
7327 | (move-to-window-line recenter-last-op)) | |
7328 | ((floatp recenter-last-op) | |
7329 | (move-to-window-line (round (* recenter-last-op (window-height)))))))))) | |
216349f8 SM |
7330 | |
7331 | (define-key global-map [?\M-r] 'move-to-window-line-top-bottom) | |
3c448ab6 | 7332 | \f |
74f806a1 JL |
7333 | ;;; Scrolling commands. |
7334 | ||
0a9f9ab5 SM |
7335 | ;;; Scrolling commands which do not signal errors at top/bottom |
7336 | ;;; of buffer at first key-press (instead move to top/bottom | |
74f806a1 JL |
7337 | ;;; of buffer). |
7338 | ||
7339 | (defcustom scroll-error-top-bottom nil | |
8350f087 | 7340 | "Move point to top/bottom of buffer before signaling a scrolling error. |
74f806a1 JL |
7341 | A value of nil means just signal an error if no more scrolling possible. |
7342 | A value of t means point moves to the beginning or the end of the buffer | |
7343 | \(depending on scrolling direction) when no more scrolling possible. | |
7344 | When point is already on that position, then signal an error." | |
7345 | :type 'boolean | |
60efac0f | 7346 | :group 'windows |
74f806a1 JL |
7347 | :version "24.1") |
7348 | ||
7349 | (defun scroll-up-command (&optional arg) | |
7350 | "Scroll text of selected window upward ARG lines; or near full screen if no ARG. | |
7351 | If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot | |
7352 | scroll window further, move cursor to the bottom line. | |
7353 | When point is already on that position, then signal an error. | |
7354 | A near full screen is `next-screen-context-lines' less than a full screen. | |
7355 | Negative ARG means scroll downward. | |
7356 | If ARG is the atom `-', scroll downward by nearly full screen." | |
7357 | (interactive "^P") | |
7358 | (cond | |
7359 | ((null scroll-error-top-bottom) | |
7360 | (scroll-up arg)) | |
7361 | ((eq arg '-) | |
7362 | (scroll-down-command nil)) | |
7363 | ((< (prefix-numeric-value arg) 0) | |
7364 | (scroll-down-command (- (prefix-numeric-value arg)))) | |
7365 | ((eobp) | |
7366 | (scroll-up arg)) ; signal error | |
7367 | (t | |
7368 | (condition-case nil | |
7369 | (scroll-up arg) | |
7370 | (end-of-buffer | |
7371 | (if arg | |
7372 | ;; When scrolling by ARG lines can't be done, | |
7373 | ;; move by ARG lines instead. | |
7374 | (forward-line arg) | |
7375 | ;; When ARG is nil for full-screen scrolling, | |
7376 | ;; move to the bottom of the buffer. | |
7377 | (goto-char (point-max)))))))) | |
7378 | ||
7379 | (put 'scroll-up-command 'scroll-command t) | |
7380 | ||
7381 | (defun scroll-down-command (&optional arg) | |
7382 | "Scroll text of selected window down ARG lines; or near full screen if no ARG. | |
7383 | If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot | |
7384 | scroll window further, move cursor to the top line. | |
7385 | When point is already on that position, then signal an error. | |
7386 | A near full screen is `next-screen-context-lines' less than a full screen. | |
7387 | Negative ARG means scroll upward. | |
7388 | If ARG is the atom `-', scroll upward by nearly full screen." | |
7389 | (interactive "^P") | |
7390 | (cond | |
7391 | ((null scroll-error-top-bottom) | |
7392 | (scroll-down arg)) | |
7393 | ((eq arg '-) | |
7394 | (scroll-up-command nil)) | |
7395 | ((< (prefix-numeric-value arg) 0) | |
7396 | (scroll-up-command (- (prefix-numeric-value arg)))) | |
7397 | ((bobp) | |
7398 | (scroll-down arg)) ; signal error | |
7399 | (t | |
7400 | (condition-case nil | |
7401 | (scroll-down arg) | |
7402 | (beginning-of-buffer | |
7403 | (if arg | |
7404 | ;; When scrolling by ARG lines can't be done, | |
7405 | ;; move by ARG lines instead. | |
7406 | (forward-line (- arg)) | |
7407 | ;; When ARG is nil for full-screen scrolling, | |
7408 | ;; move to the top of the buffer. | |
7409 | (goto-char (point-min)))))))) | |
7410 | ||
7411 | (put 'scroll-down-command 'scroll-command t) | |
7412 | ||
7413 | ;;; Scrolling commands which scroll a line instead of full screen. | |
7414 | ||
7415 | (defun scroll-up-line (&optional arg) | |
7416 | "Scroll text of selected window upward ARG lines; or one line if no ARG. | |
7417 | If ARG is omitted or nil, scroll upward by one line. | |
7418 | This is different from `scroll-up-command' that scrolls a full screen." | |
7419 | (interactive "p") | |
7420 | (scroll-up (or arg 1))) | |
7421 | ||
7422 | (put 'scroll-up-line 'scroll-command t) | |
7423 | ||
7424 | (defun scroll-down-line (&optional arg) | |
7425 | "Scroll text of selected window down ARG lines; or one line if no ARG. | |
7426 | If ARG is omitted or nil, scroll down by one line. | |
7427 | This is different from `scroll-down-command' that scrolls a full screen." | |
7428 | (interactive "p") | |
7429 | (scroll-down (or arg 1))) | |
7430 | ||
7431 | (put 'scroll-down-line 'scroll-command t) | |
7432 | ||
7433 | \f | |
011474aa | 7434 | (defun scroll-other-window-down (&optional lines) |
74f806a1 JL |
7435 | "Scroll the \"other window\" down. |
7436 | For more details, see the documentation for `scroll-other-window'." | |
7437 | (interactive "P") | |
7438 | (scroll-other-window | |
7439 | ;; Just invert the argument's meaning. | |
7440 | ;; We can do that without knowing which window it will be. | |
7441 | (if (eq lines '-) nil | |
7442 | (if (null lines) '- | |
7443 | (- (prefix-numeric-value lines)))))) | |
7444 | ||
7445 | (defun beginning-of-buffer-other-window (arg) | |
7446 | "Move point to the beginning of the buffer in the other window. | |
7447 | Leave mark at previous position. | |
7448 | With arg N, put point N/10 of the way from the true beginning." | |
7449 | (interactive "P") | |
7450 | (let ((orig-window (selected-window)) | |
7451 | (window (other-window-for-scrolling))) | |
7452 | ;; We use unwind-protect rather than save-window-excursion | |
7453 | ;; because the latter would preserve the things we want to change. | |
7454 | (unwind-protect | |
7455 | (progn | |
7456 | (select-window window) | |
7457 | ;; Set point and mark in that window's buffer. | |
7458 | (with-no-warnings | |
7459 | (beginning-of-buffer arg)) | |
7460 | ;; Set point accordingly. | |
7461 | (recenter '(t))) | |
7462 | (select-window orig-window)))) | |
7463 | ||
7464 | (defun end-of-buffer-other-window (arg) | |
7465 | "Move point to the end of the buffer in the other window. | |
7466 | Leave mark at previous position. | |
7467 | With arg N, put point N/10 of the way from the true end." | |
7468 | (interactive "P") | |
7469 | ;; See beginning-of-buffer-other-window for comments. | |
7470 | (let ((orig-window (selected-window)) | |
7471 | (window (other-window-for-scrolling))) | |
7472 | (unwind-protect | |
7473 | (progn | |
7474 | (select-window window) | |
7475 | (with-no-warnings | |
7476 | (end-of-buffer arg)) | |
7477 | (recenter '(t))) | |
7478 | (select-window orig-window)))) | |
74f806a1 | 7479 | \f |
3c448ab6 MR |
7480 | (defvar mouse-autoselect-window-timer nil |
7481 | "Timer used by delayed window autoselection.") | |
7482 | ||
7483 | (defvar mouse-autoselect-window-position nil | |
7484 | "Last mouse position recorded by delayed window autoselection.") | |
7485 | ||
7486 | (defvar mouse-autoselect-window-window nil | |
7487 | "Last window recorded by delayed window autoselection.") | |
7488 | ||
7489 | (defvar mouse-autoselect-window-state nil | |
7490 | "When non-nil, special state of delayed window autoselection. | |
382c953b JB |
7491 | Possible values are `suspend' (suspend autoselection after a menu or |
7492 | scrollbar interaction) and `select' (the next invocation of | |
7493 | `handle-select-window' shall select the window immediately).") | |
3c448ab6 MR |
7494 | |
7495 | (defun mouse-autoselect-window-cancel (&optional force) | |
7496 | "Cancel delayed window autoselection. | |
7497 | Optional argument FORCE means cancel unconditionally." | |
7498 | (unless (and (not force) | |
7499 | ;; Don't cancel for select-window or select-frame events | |
7500 | ;; or when the user drags a scroll bar. | |
7501 | (or (memq this-command | |
7502 | '(handle-select-window handle-switch-frame)) | |
7503 | (and (eq this-command 'scroll-bar-toolkit-scroll) | |
7504 | (memq (nth 4 (event-end last-input-event)) | |
7505 | '(handle end-scroll))))) | |
7506 | (setq mouse-autoselect-window-state nil) | |
7507 | (when (timerp mouse-autoselect-window-timer) | |
7508 | (cancel-timer mouse-autoselect-window-timer)) | |
7509 | (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel))) | |
7510 | ||
7511 | (defun mouse-autoselect-window-start (mouse-position &optional window suspend) | |
7512 | "Start delayed window autoselection. | |
7513 | MOUSE-POSITION is the last position where the mouse was seen as returned | |
7514 | by `mouse-position'. Optional argument WINDOW non-nil denotes the | |
7515 | window where the mouse was seen. Optional argument SUSPEND non-nil | |
7516 | means suspend autoselection." | |
7517 | ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND. | |
7518 | (setq mouse-autoselect-window-position mouse-position) | |
7519 | (when window (setq mouse-autoselect-window-window window)) | |
7520 | (setq mouse-autoselect-window-state (when suspend 'suspend)) | |
7521 | ;; Install timer which runs `mouse-autoselect-window-select' after | |
7522 | ;; `mouse-autoselect-window' seconds. | |
7523 | (setq mouse-autoselect-window-timer | |
7524 | (run-at-time | |
7525 | (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select))) | |
7526 | ||
7527 | (defun mouse-autoselect-window-select () | |
7528 | "Select window with delayed window autoselection. | |
7529 | If the mouse position has stabilized in a non-selected window, select | |
382c953b JB |
7530 | that window. The minibuffer window is selected only if the minibuffer |
7531 | is active. This function is run by `mouse-autoselect-window-timer'." | |
6198ccd0 MR |
7532 | (ignore-errors |
7533 | (let* ((mouse-position (mouse-position)) | |
7534 | (window | |
7535 | (ignore-errors | |
7536 | (window-at (cadr mouse-position) (cddr mouse-position) | |
7537 | (car mouse-position))))) | |
7538 | (cond | |
e740f9d2 | 7539 | ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p)) |
6198ccd0 | 7540 | (and window |
3dfc5cd6 MR |
7541 | (let ((coords (coordinates-in-window-p |
7542 | (cdr mouse-position) window))) | |
7543 | (and (not (consp coords)) | |
7544 | (not (memq coords '(left-margin right-margin))))))) | |
c660a885 MR |
7545 | ;; A menu / popup dialog is active or the mouse is not on the |
7546 | ;; text region of WINDOW: Suspend autoselection temporarily. | |
6198ccd0 MR |
7547 | (mouse-autoselect-window-start mouse-position nil t)) |
7548 | ((eq mouse-autoselect-window-state 'suspend) | |
7549 | ;; Delayed autoselection was temporarily suspended, reenable it. | |
7550 | (mouse-autoselect-window-start mouse-position)) | |
7551 | ((and window (not (eq window (selected-window))) | |
7552 | (or (not (numberp mouse-autoselect-window)) | |
7553 | (and (> mouse-autoselect-window 0) | |
7554 | ;; If `mouse-autoselect-window' is positive, select | |
7555 | ;; window if the window is the same as before. | |
7556 | (eq window mouse-autoselect-window-window)) | |
7557 | ;; Otherwise select window if the mouse is at the same | |
7558 | ;; position as before. Observe that the first test after | |
7559 | ;; starting autoselection usually fails since the value of | |
7560 | ;; `mouse-autoselect-window-position' recorded there is the | |
7561 | ;; position where the mouse has entered the new window and | |
7562 | ;; not necessarily where the mouse has stopped moving. | |
7563 | (equal mouse-position mouse-autoselect-window-position)) | |
7564 | ;; The minibuffer is a candidate window if it's active. | |
7565 | (or (not (window-minibuffer-p window)) | |
7566 | (eq window (active-minibuffer-window)))) | |
7567 | ;; Mouse position has stabilized in non-selected window: Cancel | |
7568 | ;; delayed autoselection and try to select that window. | |
7569 | (mouse-autoselect-window-cancel t) | |
7570 | ;; Select window where mouse appears unless the selected window is the | |
7571 | ;; minibuffer. Use `unread-command-events' in order to execute pre- | |
7572 | ;; and post-command hooks and trigger idle timers. To avoid delaying | |
7573 | ;; autoselection again, set `mouse-autoselect-window-state'." | |
290d5b58 | 7574 | (unless (window-minibuffer-p) |
6198ccd0 MR |
7575 | (setq mouse-autoselect-window-state 'select) |
7576 | (setq unread-command-events | |
7577 | (cons (list 'select-window (list window)) | |
7578 | unread-command-events)))) | |
7579 | ((or (and window (eq window (selected-window))) | |
7580 | (not (numberp mouse-autoselect-window)) | |
7581 | (equal mouse-position mouse-autoselect-window-position)) | |
7582 | ;; Mouse position has either stabilized in the selected window or at | |
7583 | ;; `mouse-autoselect-window-position': Cancel delayed autoselection. | |
7584 | (mouse-autoselect-window-cancel t)) | |
7585 | (t | |
7586 | ;; Mouse position has not stabilized yet, resume delayed | |
7587 | ;; autoselection. | |
7588 | (mouse-autoselect-window-start mouse-position window)))))) | |
3c448ab6 MR |
7589 | |
7590 | (defun handle-select-window (event) | |
7591 | "Handle select-window events." | |
7592 | (interactive "e") | |
7593 | (let ((window (posn-window (event-start event)))) | |
7594 | (unless (or (not (window-live-p window)) | |
7595 | ;; Don't switch if we're currently in the minibuffer. | |
7596 | ;; This tries to work around problems where the | |
7597 | ;; minibuffer gets unselected unexpectedly, and where | |
7598 | ;; you then have to move your mouse all the way down to | |
7599 | ;; the minibuffer to select it. | |
290d5b58 | 7600 | (window-minibuffer-p) |
3c448ab6 MR |
7601 | ;; Don't switch to minibuffer window unless it's active. |
7602 | (and (window-minibuffer-p window) | |
7603 | (not (minibuffer-window-active-p window))) | |
7604 | ;; Don't switch when autoselection shall be delayed. | |
7605 | (and (numberp mouse-autoselect-window) | |
7606 | (not (zerop mouse-autoselect-window)) | |
7607 | (not (eq mouse-autoselect-window-state 'select)) | |
7608 | (progn | |
7609 | ;; Cancel any delayed autoselection. | |
7610 | (mouse-autoselect-window-cancel t) | |
7611 | ;; Start delayed autoselection from current mouse | |
7612 | ;; position and window. | |
7613 | (mouse-autoselect-window-start (mouse-position) window) | |
7614 | ;; Executing a command cancels delayed autoselection. | |
7615 | (add-hook | |
7616 | 'pre-command-hook 'mouse-autoselect-window-cancel)))) | |
7617 | (when mouse-autoselect-window | |
7618 | ;; Reset state of delayed autoselection. | |
7619 | (setq mouse-autoselect-window-state nil) | |
7620 | ;; Run `mouse-leave-buffer-hook' when autoselecting window. | |
7621 | (run-hooks 'mouse-leave-buffer-hook)) | |
b1bac16e MR |
7622 | ;; Clear echo area. |
7623 | (message nil) | |
3c448ab6 MR |
7624 | (select-window window)))) |
7625 | ||
3c448ab6 MR |
7626 | (defun truncated-partial-width-window-p (&optional window) |
7627 | "Return non-nil if lines in WINDOW are specifically truncated due to its width. | |
85c2386b | 7628 | WINDOW must be a live window and defaults to the selected one. |
3c448ab6 MR |
7629 | Return nil if WINDOW is not a partial-width window |
7630 | (regardless of the value of `truncate-lines'). | |
7631 | Otherwise, consult the value of `truncate-partial-width-windows' | |
7632 | for the buffer shown in WINDOW." | |
85c2386b | 7633 | (setq window (window-normalize-window window t)) |
3c448ab6 MR |
7634 | (unless (window-full-width-p window) |
7635 | (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows | |
7636 | (window-buffer window)))) | |
7637 | (if (integerp t-p-w-w) | |
7638 | (< (window-width window) t-p-w-w) | |
7639 | t-p-w-w)))) | |
562dd5e9 | 7640 | \f |
59ee0542 GM |
7641 | ;; Some of these are in tutorial--default-keys, so update that if you |
7642 | ;; change these. | |
562dd5e9 MR |
7643 | (define-key ctl-x-map "0" 'delete-window) |
7644 | (define-key ctl-x-map "1" 'delete-other-windows) | |
2d197ffb CY |
7645 | (define-key ctl-x-map "2" 'split-window-below) |
7646 | (define-key ctl-x-map "3" 'split-window-right) | |
9397e56f | 7647 | (define-key ctl-x-map "o" 'other-window) |
562dd5e9 | 7648 | (define-key ctl-x-map "^" 'enlarge-window) |
3c448ab6 MR |
7649 | (define-key ctl-x-map "}" 'enlarge-window-horizontally) |
7650 | (define-key ctl-x-map "{" 'shrink-window-horizontally) | |
7651 | (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer) | |
7652 | (define-key ctl-x-map "+" 'balance-windows) | |
7653 | (define-key ctl-x-4-map "0" 'kill-buffer-and-window) | |
7654 | ||
3c448ab6 | 7655 | ;;; window.el ends here |