Commit | Line | Data |
---|---|---|
12169754 | 1 | ;;; window.el --- GNU Emacs window commands aside from those written in C. |
d46bac56 | 2 | |
f92c5e7d | 3 | ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001 |
c16c855b | 4 | ;; Free Software Foundation, Inc. |
a2535589 | 5 | |
58142744 ER |
6 | ;; Maintainer: FSF |
7 | ||
a2535589 JA |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
492878e4 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
a2535589 JA |
24 | |
25 | ||
fe7a0e7d SM |
26 | ;;; Commentary: |
27 | ||
28 | ;; Window tree functions. | |
29 | ||
30 | ;;; Code: | |
82e6d8cb RS |
31 | |
32 | (defun one-window-p (&optional nomini all-frames) | |
fe7a0e7d | 33 | "Return non-nil if the selected window is the only window (in its frame). |
82e6d8cb RS |
34 | Optional arg NOMINI non-nil means don't count the minibuffer |
35 | even if it is active. | |
36 | ||
37 | The optional arg ALL-FRAMES t means count windows on all frames. | |
38 | If it is `visible', count windows on all visible frames. | |
fe7a0e7d | 39 | ALL-FRAMES nil or omitted means count only the selected frame, |
82e6d8cb RS |
40 | plus the minibuffer it uses (which may be on another frame). |
41 | If ALL-FRAMES is neither nil nor t, count only the selected frame." | |
42 | (let ((base-window (selected-window))) | |
43 | (if (and nomini (eq base-window (minibuffer-window))) | |
44 | (setq base-window (next-window base-window))) | |
45 | (eq base-window | |
46 | (next-window base-window (if nomini 'arg) all-frames)))) | |
47 | ||
48 | (defun walk-windows (proc &optional minibuf all-frames) | |
49 | "Cycle through all visible windows, calling PROC for each one. | |
50 | PROC is called with a window as argument. | |
51 | ||
52 | Optional second arg MINIBUF t means count the minibuffer window even | |
53 | if not active. MINIBUF nil or omitted means count the minibuffer iff | |
54 | it is active. MINIBUF neither t nor nil means not to count the | |
55 | minibuffer even if it is active. | |
56 | ||
57 | Several frames may share a single minibuffer; if the minibuffer | |
58 | counts, all windows on all frames that share that minibuffer count | |
5aa29df8 RS |
59 | too. Therefore, if you are using a separate minibuffer frame |
60 | and the minibuffer is active and MINIBUF says it counts, | |
82e6d8cb | 61 | `walk-windows' includes the windows in the frame from which you |
5aa29df8 | 62 | entered the minibuffer, as well as the minibuffer window. |
82e6d8cb | 63 | |
04ea572f RS |
64 | ALL-FRAMES is the optional third argument. |
65 | ALL-FRAMES nil or omitted means cycle within the frames as specified above. | |
66 | ALL-FRAMES = `visible' means include windows on all visible frames. | |
82e6d8cb | 67 | ALL-FRAMES = 0 means include windows on all visible and iconified frames. |
04ea572f | 68 | ALL-FRAMES = t means include windows on all frames including invisible frames. |
aeb721fe | 69 | If ALL-FRAMES is a frame, it means include windows on that frame. |
c7d031ed | 70 | Anything else means restrict to the selected frame." |
82e6d8cb RS |
71 | ;; If we start from the minibuffer window, don't fail to come back to it. |
72 | (if (window-minibuffer-p (selected-window)) | |
73 | (setq minibuf t)) | |
aeb721fe GM |
74 | (save-selected-window |
75 | (if (framep all-frames) | |
76 | (select-window (frame-first-window all-frames))) | |
d4aff7cc GM |
77 | (let* (walk-windows-already-seen |
78 | (walk-windows-current (selected-window))) | |
aeb721fe GM |
79 | (while (progn |
80 | (setq walk-windows-current | |
81 | (next-window walk-windows-current minibuf all-frames)) | |
d4aff7cc GM |
82 | (not (memq walk-windows-current walk-windows-already-seen))) |
83 | (setq walk-windows-already-seen | |
84 | (cons walk-windows-current walk-windows-already-seen)) | |
85 | (funcall proc walk-windows-current))))) | |
86 | ||
87 | (defun some-window (predicate &optional minibuf all-frames default) | |
88 | "Return a window satisfying PREDICATE. | |
89 | ||
90 | This function cycles through all visible windows using `walk-windows', | |
91 | calling PREDICATE on each one. PREDICATE is called with a window as | |
92 | argument. The first window for which PREDICATE returns a non-nil | |
93 | value is returned. If no window satisfies PREDICATE, DEFAULT is | |
94 | returned. | |
95 | ||
96 | Optional second arg MINIBUF t means count the minibuffer window even | |
97 | if not active. MINIBUF nil or omitted means count the minibuffer iff | |
98 | it is active. MINIBUF neither t nor nil means not to count the | |
99 | minibuffer even if it is active. | |
100 | ||
101 | Several frames may share a single minibuffer; if the minibuffer | |
102 | counts, all windows on all frames that share that minibuffer count | |
103 | too. Therefore, if you are using a separate minibuffer frame | |
104 | and the minibuffer is active and MINIBUF says it counts, | |
105 | `walk-windows' includes the windows in the frame from which you | |
106 | entered the minibuffer, as well as the minibuffer window. | |
107 | ||
108 | ALL-FRAMES is the optional third argument. | |
109 | ALL-FRAMES nil or omitted means cycle within the frames as specified above. | |
110 | ALL-FRAMES = `visible' means include windows on all visible frames. | |
111 | ALL-FRAMES = 0 means include windows on all visible and iconified frames. | |
112 | ALL-FRAMES = t means include windows on all frames including invisible frames. | |
113 | If ALL-FRAMES is a frame, it means include windows on that frame. | |
114 | Anything else means restrict to the selected frame." | |
115 | (catch 'found | |
fe7a0e7d | 116 | (walk-windows #'(lambda (window) |
d4aff7cc GM |
117 | (when (funcall predicate window) |
118 | (throw 'found window))) | |
119 | minibuf all-frames) | |
120 | default)) | |
82e6d8cb RS |
121 | |
122 | (defun minibuffer-window-active-p (window) | |
123 | "Return t if WINDOW (a minibuffer window) is now active." | |
0d624ea4 | 124 | (eq window (active-minibuffer-window))) |
7f77f3c8 KH |
125 | |
126 | (defmacro save-selected-window (&rest body) | |
127 | "Execute BODY, then select the window that was selected before BODY." | |
fe7a0e7d SM |
128 | `(let ((save-selected-window-window (selected-window))) |
129 | (unwind-protect | |
130 | (progn ,@body) | |
131 | (select-window save-selected-window-window)))) | |
82e6d8cb | 132 | \f |
a2535589 | 133 | (defun count-windows (&optional minibuf) |
fe7a0e7d | 134 | "Return the number of visible windows. |
b70b2dd2 KH |
135 | This counts the windows in the selected frame and (if the minibuffer is |
136 | to be counted) its minibuffer frame (if that's not the same frame). | |
137 | The optional arg MINIBUF non-nil means count the minibuffer | |
c6897d8e | 138 | even if it is inactive." |
a2535589 | 139 | (let ((count 0)) |
12169754 | 140 | (walk-windows (function (lambda (w) |
a2535589 JA |
141 | (setq count (+ count 1)))) |
142 | minibuf) | |
143 | count)) | |
144 | ||
fe7a0e7d | 145 | (defun window-safely-shrinkable-p (&optional window) |
119171dd EZ |
146 | "Non-nil if the WINDOW can be shrunk without shrinking other windows. |
147 | If WINDOW is nil or omitted, it defaults to the currently selected window." | |
fe7a0e7d SM |
148 | (save-selected-window |
149 | (when window (select-window window)) | |
150 | (or (and (not (eq window (frame-first-window))) | |
151 | (= (car (window-edges)) | |
152 | (car (window-edges (previous-window))))) | |
153 | (= (car (window-edges)) | |
154 | (car (window-edges (next-window))))))) | |
155 | ||
a2535589 | 156 | (defun balance-windows () |
fe7a0e7d | 157 | "Make all visible windows the same height (approximately)." |
a2535589 | 158 | (interactive) |
3ca9dd8d RS |
159 | (let ((count -1) levels newsizes size |
160 | ;; Don't count the lines that are above the uppermost windows. | |
161 | ;; (These are the menu bar lines, if any.) | |
162 | (mbl (nth 1 (window-edges (frame-first-window (selected-frame)))))) | |
7162c5c4 RS |
163 | ;; Find all the different vpos's at which windows start, |
164 | ;; then count them. But ignore levels that differ by only 1. | |
165 | (save-window-excursion | |
166 | (let (tops (prev-top -2)) | |
167 | (walk-windows (function (lambda (w) | |
168 | (setq tops (cons (nth 1 (window-edges w)) | |
169 | tops)))) | |
170 | 'nomini) | |
171 | (setq tops (sort tops '<)) | |
172 | (while tops | |
173 | (if (> (car tops) (1+ prev-top)) | |
174 | (setq prev-top (car tops) | |
175 | count (1+ count))) | |
176 | (setq levels (cons (cons (car tops) count) levels)) | |
177 | (setq tops (cdr tops))) | |
178 | (setq count (1+ count)))) | |
179 | ;; Subdivide the frame into that many vertical levels. | |
3ca9dd8d | 180 | (setq size (/ (- (frame-height) mbl) count)) |
7162c5c4 RS |
181 | (walk-windows (function |
182 | (lambda (w) | |
183 | (select-window w) | |
184 | (let ((newtop (cdr (assq (nth 1 (window-edges)) | |
185 | levels))) | |
186 | (newbot (or (cdr (assq (+ (window-height) | |
187 | (nth 1 (window-edges))) | |
188 | levels)) | |
189 | count))) | |
190 | (setq newsizes | |
191 | (cons (cons w (* size (- newbot newtop))) | |
ce27e1b0 RS |
192 | newsizes))))) |
193 | 'nomini) | |
a2535589 | 194 | (walk-windows (function (lambda (w) |
7162c5c4 RS |
195 | (select-window w) |
196 | (let ((newsize (cdr (assq w newsizes)))) | |
197 | (enlarge-window (- newsize | |
198 | (window-height)))))) | |
199 | 'nomini))) | |
82e6d8cb | 200 | \f |
69037c38 | 201 | ;;; I think this should be the default; I think people will prefer it--rms. |
30e19aee | 202 | (defcustom split-window-keep-point t |
7162c5c4 RS |
203 | "*If non-nil, split windows keeps the original point in both children. |
204 | This is often more convenient for editing. | |
205 | If nil, adjust point in each of the two windows to minimize redisplay. | |
30e19aee RS |
206 | This is convenient on slow terminals, but point can move strangely." |
207 | :type 'boolean | |
208 | :group 'windows) | |
8e4b71d8 | 209 | |
a2535589 JA |
210 | (defun split-window-vertically (&optional arg) |
211 | "Split current window into two windows, one above the other. | |
c65c1681 | 212 | The uppermost window gets ARG lines and the other gets the rest. |
ab94bf9f | 213 | Negative arg means select the size of the lowermost window instead. |
c65c1681 RS |
214 | With no argument, split equally or close to it. |
215 | Both windows display the same buffer now current. | |
c65c1681 | 216 | |
3e9890d1 | 217 | If the variable `split-window-keep-point' is non-nil, both new windows |
8e4b71d8 JB |
218 | will get the same value of point as the current window. This is often |
219 | more convenient for editing. | |
220 | ||
221 | Otherwise, we chose window starts so as to minimize the amount of | |
222 | redisplay; this is convenient on slow terminals. The new selected | |
223 | window is the one that the current value of point appears in. The | |
224 | value of point can change if the text around point is hidden by the | |
225 | new mode line." | |
a2535589 JA |
226 | (interactive "P") |
227 | (let ((old-w (selected-window)) | |
c65c1681 | 228 | (old-point (point)) |
ab94bf9f | 229 | (size (and arg (prefix-numeric-value arg))) |
4464514e RS |
230 | (window-full-p nil) |
231 | new-w bottom switch moved) | |
ab94bf9f KH |
232 | (and size (< size 0) (setq size (+ (window-height) size))) |
233 | (setq new-w (split-window nil size)) | |
7d7f1f33 | 234 | (or split-window-keep-point |
c65c1681 | 235 | (progn |
8e4b71d8 JB |
236 | (save-excursion |
237 | (set-buffer (window-buffer)) | |
238 | (goto-char (window-start)) | |
4464514e | 239 | (setq moved (vertical-motion (window-height))) |
8e4b71d8 JB |
240 | (set-window-start new-w (point)) |
241 | (if (> (point) (window-point new-w)) | |
242 | (set-window-point new-w (point))) | |
4464514e RS |
243 | (and (= moved (window-height)) |
244 | (progn | |
245 | (setq window-full-p t) | |
246 | (vertical-motion -1))) | |
247 | (setq bottom (point))) | |
248 | (and window-full-p | |
249 | (<= bottom (point)) | |
250 | (set-window-point old-w (1- bottom))) | |
251 | (and window-full-p | |
252 | (<= (window-start new-w) old-point) | |
253 | (progn | |
254 | (set-window-point new-w old-point) | |
255 | (select-window new-w))))) | |
c361280d RS |
256 | (split-window-save-restore-data new-w old-w))) |
257 | ||
3d2f23d9 RS |
258 | ;; This is to avoid compiler warnings. |
259 | (defvar view-return-to-alist) | |
260 | ||
c361280d RS |
261 | (defun split-window-save-restore-data (new-w old-w) |
262 | (save-excursion | |
263 | (set-buffer (window-buffer)) | |
264 | (if view-mode | |
265 | (let ((old-info (assq old-w view-return-to-alist))) | |
266 | (setq view-return-to-alist | |
267 | (cons (cons new-w (cons (and old-info (car (cdr old-info))) t)) | |
268 | view-return-to-alist)))) | |
2ed3f64c | 269 | new-w)) |
a2535589 JA |
270 | |
271 | (defun split-window-horizontally (&optional arg) | |
272 | "Split current window into two windows side by side. | |
0ef2c2f2 KH |
273 | This window becomes the leftmost of the two, and gets ARG columns. |
274 | Negative arg means select the size of the rightmost window instead. | |
dd1455c2 RS |
275 | The argument includes the width of the window's scroll bar; if there |
276 | are no scroll bars, it includes the width of the divider column | |
277 | to the window's right, if any. No arg means split equally." | |
a2535589 | 278 | (interactive "P") |
c361280d RS |
279 | (let ((old-w (selected-window)) |
280 | (size (and arg (prefix-numeric-value arg)))) | |
0ef2c2f2 KH |
281 | (and size (< size 0) |
282 | (setq size (+ (window-width) size))) | |
c361280d | 283 | (split-window-save-restore-data (split-window nil size t) old-w))) |
361691dc MB |
284 | |
285 | \f | |
361691dc MB |
286 | (defun set-window-text-height (window height) |
287 | "Sets the height in lines of the text display area of WINDOW to HEIGHT. | |
288 | This doesn't include the mode-line (or header-line if any) or any | |
289 | partial-height lines in the text display area. | |
290 | ||
291 | If WINDOW is nil, the selected window is used. | |
361691dc MB |
292 | |
293 | Note that the current implementation of this function cannot always set | |
294 | the height exactly, but attempts to be conservative, by allocating more | |
295 | lines than are actually needed in the case where some error may be present." | |
296 | (let ((delta (- height (window-text-height window)))) | |
297 | (unless (zerop delta) | |
38f99c27 MB |
298 | (let ((window-min-height 1)) |
299 | (if (and window (not (eq window (selected-window)))) | |
300 | (save-selected-window | |
301 | (select-window window) | |
302 | (enlarge-window delta)) | |
303 | (enlarge-window delta)))))) | |
361691dc | 304 | |
8075a110 | 305 | \f |
a2535589 JA |
306 | (defun enlarge-window-horizontally (arg) |
307 | "Make current window ARG columns wider." | |
308 | (interactive "p") | |
309 | (enlarge-window arg t)) | |
310 | ||
311 | (defun shrink-window-horizontally (arg) | |
312 | "Make current window ARG columns narrower." | |
313 | (interactive "p") | |
314 | (shrink-window arg t)) | |
315 | ||
3b134005 RS |
316 | (defun window-buffer-height (window) |
317 | "Return the height (in screen lines) of the buffer that WINDOW is displaying." | |
318 | (save-excursion | |
319 | (set-buffer (window-buffer window)) | |
320 | (goto-char (point-min)) | |
321 | (let ((ignore-final-newline | |
322 | ;; If buffer ends with a newline, ignore it when counting height | |
323 | ;; unless point is after it. | |
324 | (and (not (eobp)) (eq ?\n (char-after (1- (point-max))))))) | |
325 | (+ 1 (nth 2 (compute-motion (point-min) | |
326 | '(0 . 0) | |
327 | (- (point-max) (if ignore-final-newline 1 0)) | |
328 | (cons 0 100000000) | |
329 | (window-width window) | |
330 | nil | |
331 | window)))))) | |
332 | ||
c16c855b GM |
333 | (defun count-screen-lines (&optional beg end count-final-newline window) |
334 | "Return the number of screen lines in the region. | |
335 | The number of screen lines may be different from the number of actual lines, | |
336 | due to line breaking, display table, etc. | |
337 | ||
338 | Optional arguments BEG and END default to `point-min' and `point-max' | |
339 | respectively. | |
340 | ||
fe7a0e7d | 341 | If region ends with a newline, ignore it unless optional third argument |
c16c855b GM |
342 | COUNT-FINAL-NEWLINE is non-nil. |
343 | ||
344 | The optional fourth argument WINDOW specifies the window used for obtaining | |
fe7a0e7d | 345 | parameters such as width, horizontal scrolling, and so on. The default is |
c16c855b GM |
346 | to use the selected window's parameters. |
347 | ||
348 | Like `vertical-motion', `count-screen-lines' always uses the current buffer, | |
fe7a0e7d | 349 | regardless of which buffer is displayed in WINDOW. This makes possible to use |
c16c855b GM |
350 | `count-screen-lines' in any buffer, whether or not it is currently displayed |
351 | in some window." | |
352 | (unless beg | |
353 | (setq beg (point-min))) | |
354 | (unless end | |
355 | (setq end (point-max))) | |
356 | (if (= beg end) | |
357 | 0 | |
358 | (save-excursion | |
359 | (save-restriction | |
360 | (widen) | |
361 | (narrow-to-region (min beg end) | |
362 | (if (and (not count-final-newline) | |
363 | (= ?\n (char-before (max beg end)))) | |
364 | (1- (max beg end)) | |
365 | (max beg end))) | |
366 | (goto-char (point-min)) | |
367 | (1+ (vertical-motion (buffer-size) window)))))) | |
368 | ||
65e742bd MB |
369 | (defun fit-window-to-buffer (&optional window max-height min-height) |
370 | "Make WINDOW the right size to display its contents exactly. | |
119171dd | 371 | If WINDOW is omitted or nil, it defaults to the selected window. |
65e742bd MB |
372 | If the optional argument MAX-HEIGHT is supplied, it is the maximum height |
373 | the window is allowed to be, defaulting to the frame height. | |
374 | If the optional argument MIN-HEIGHT is supplied, it is the minimum | |
375 | height the window is allowed to be, defaulting to `window-min-height'. | |
376 | ||
377 | The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or | |
378 | header-line." | |
379 | (interactive) | |
380 | ||
381 | (when (null window) | |
382 | (setq window (selected-window))) | |
3511cde8 MB |
383 | (when (null max-height) |
384 | (setq max-height (frame-height (window-frame window)))) | |
65e742bd | 385 | |
88f0a1eb MB |
386 | (let* ((buf |
387 | ;; Buffer that is displayed in WINDOW | |
388 | (window-buffer window)) | |
389 | (window-height | |
65e742bd MB |
390 | ;; The current height of WINDOW |
391 | (window-height window)) | |
88f0a1eb | 392 | (desired-height |
65e742bd MB |
393 | ;; The height necessary to show the buffer displayed by WINDOW |
394 | ;; (`count-screen-lines' always works on the current buffer). | |
88f0a1eb MB |
395 | (with-current-buffer buf |
396 | (+ (count-screen-lines) | |
b1491763 KH |
397 | ;; If the buffer is empty, (count-screen-lines) is |
398 | ;; zero. But, even in that case, we need one text line | |
399 | ;; for cursor. | |
400 | (if (= (point-min) (point-max)) | |
401 | 1 0) | |
88f0a1eb MB |
402 | ;; For non-minibuffers, count the mode-line, if any |
403 | (if (and (not (window-minibuffer-p window)) | |
404 | mode-line-format) | |
405 | 1 0) | |
406 | ;; Count the header-line, if any | |
407 | (if header-line-format 1 0)))) | |
65e742bd MB |
408 | (delta |
409 | ;; Calculate how much the window height has to change to show | |
88f0a1eb MB |
410 | ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT. |
411 | (- (max (min desired-height max-height) | |
65e742bd MB |
412 | (or min-height window-min-height)) |
413 | window-height)) | |
414 | ;; We do our own height checking, so avoid any restrictions due to | |
415 | ;; window-min-height. | |
416 | (window-min-height 1)) | |
417 | ||
418 | ;; Don't try to redisplay with the cursor at the end | |
419 | ;; on its own line--that would force a scroll and spoil things. | |
88f0a1eb MB |
420 | (when (with-current-buffer buf |
421 | (and (eobp) (bolp) (not (bobp)))) | |
422 | (set-window-point window (1- (window-point window)))) | |
423 | ||
424 | (save-selected-window | |
425 | (select-window window) | |
426 | ||
427 | ;; Adjust WINDOW to the nominally correct size (which may actually | |
428 | ;; be slightly off because of variable height text, etc). | |
429 | (unless (zerop delta) | |
430 | (enlarge-window delta)) | |
431 | ||
432 | ;; Check if the last line is surely fully visible. If not, | |
433 | ;; enlarge the window. | |
434 | (let ((end (with-current-buffer buf | |
435 | (save-excursion | |
436 | (goto-char (point-max)) | |
45450dd5 MB |
437 | (when (and (bolp) (not (bobp))) |
438 | ;; Don't include final newline | |
439 | (backward-char 1)) | |
440 | (when truncate-lines | |
441 | ;; If line-wrapping is turned off, test the | |
442 | ;; beginning of the last line for visibility | |
443 | ;; instead of the end, as the end of the line | |
444 | ;; could be invisible by virtue of extending past | |
445 | ;; the edge of the window. | |
446 | (forward-line 0)) | |
447 | (point))))) | |
88f0a1eb MB |
448 | (set-window-vscroll window 0) |
449 | (while (and (< desired-height max-height) | |
450 | (= desired-height (window-height window)) | |
df0677c3 | 451 | (not (pos-visible-in-window-p end window))) |
88f0a1eb | 452 | (enlarge-window 1) |
edb08287 | 453 | (setq desired-height (1+ desired-height))))))) |
65e742bd | 454 | |
a0900d9f | 455 | (defun shrink-window-if-larger-than-buffer (&optional window) |
d0bee390 | 456 | "Shrink the WINDOW to be as small as possible to display its contents. |
119171dd | 457 | If WINDOW is omitted or nil, it defaults to the selected window. |
30f2e5cc | 458 | Do not shrink to less than `window-min-height' lines. |
d0bee390 | 459 | Do nothing if the buffer contains more lines than the present window height, |
3eb217a0 | 460 | or if some of the window's contents are scrolled out of view, |
fe7a0e7d SM |
461 | or if shrinking this window would also shrink another window. |
462 | or if the window is the only window of its frame. | |
463 | Return non-nil if the window was shrunk." | |
d0bee390 | 464 | (interactive) |
65e742bd MB |
465 | (when (null window) |
466 | (setq window (selected-window))) | |
467 | (let* ((frame (window-frame window)) | |
468 | (mini (frame-parameter frame 'minibuffer)) | |
469 | (edges (window-edges window))) | |
470 | (if (and (not (eq window (frame-root-window frame))) | |
fe7a0e7d | 471 | (window-safely-shrinkable-p) |
65e742bd MB |
472 | (pos-visible-in-window-p (point-min) window) |
473 | (not (eq mini 'only)) | |
474 | (or (not mini) | |
f92c5e7d GM |
475 | (let ((mini-window (minibuffer-window frame))) |
476 | (or (null mini-window) | |
477 | (not (eq frame (window-frame mini-window))) | |
478 | (< (nth 3 edges) | |
479 | (nth 1 (window-edges mini-window))) | |
480 | (> (nth 1 edges) | |
481 | (frame-parameter frame 'menu-bar-lines)))))) | |
65e742bd | 482 | (fit-window-to-buffer window (window-height window))))) |
b691df0c RS |
483 | |
484 | (defun kill-buffer-and-window () | |
485 | "Kill the current buffer and delete the selected window." | |
486 | (interactive) | |
487 | (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name))) | |
488 | (let ((buffer (current-buffer))) | |
489 | (delete-window (selected-window)) | |
490 | (kill-buffer buffer)) | |
491 | (error "Aborted"))) | |
492 | ||
3d2f23d9 RS |
493 | (defun quit-window (&optional kill window) |
494 | "Quit the current buffer. Bury it, and maybe delete the selected frame. | |
495 | \(The frame is deleted if it is contains a dedicated window for the buffer.) | |
496 | With a prefix argument, kill the buffer instead. | |
497 | ||
498 | Noninteractively, if KILL is non-nil, then kill the current buffer, | |
499 | otherwise bury it. | |
500 | ||
501 | If WINDOW is non-nil, it specifies a window; we delete that window, | |
502 | and the buffer that is killed or buried is the one in that window." | |
503 | (interactive "P") | |
504 | (let ((buffer (window-buffer window)) | |
c81845b6 | 505 | (frame (window-frame (or window (selected-window)))) |
3d2f23d9 RS |
506 | (window-solitary |
507 | (save-selected-window | |
508 | (if window | |
509 | (select-window window)) | |
510 | (one-window-p t))) | |
511 | window-handled) | |
512 | ||
513 | (save-selected-window | |
514 | (if window | |
515 | (select-window window)) | |
eed1360c RS |
516 | (or (window-minibuffer-p) |
517 | (window-dedicated-p (selected-window)) | |
518 | (switch-to-buffer (other-buffer)))) | |
3d2f23d9 RS |
519 | |
520 | ;; Get rid of the frame, if it has just one dedicated window | |
521 | ;; and other visible frames exist. | |
eed1360c | 522 | (and (or (window-minibuffer-p) (window-dedicated-p window)) |
3d2f23d9 RS |
523 | (delq frame (visible-frame-list)) |
524 | window-solitary | |
525 | (if (and (eq default-minibuffer-frame frame) | |
526 | (= 1 (length (minibuffer-frame-list)))) | |
527 | (setq window nil) | |
528 | (delete-frame frame) | |
529 | (setq window-handled t))) | |
530 | ||
531 | ;; Deal with the buffer. | |
532 | (if kill | |
533 | (kill-buffer buffer) | |
534 | (bury-buffer buffer)) | |
535 | ||
536 | ;; Maybe get rid of the window. | |
537 | (and window (not window-handled) (not window-solitary) | |
538 | (delete-window window)))) | |
539 | ||
a2535589 | 540 | (define-key ctl-x-map "2" 'split-window-vertically) |
492878e4 | 541 | (define-key ctl-x-map "3" 'split-window-horizontally) |
a2535589 JA |
542 | (define-key ctl-x-map "}" 'enlarge-window-horizontally) |
543 | (define-key ctl-x-map "{" 'shrink-window-horizontally) | |
a0900d9f ER |
544 | (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer) |
545 | (define-key ctl-x-map "+" 'balance-windows) | |
b691df0c | 546 | (define-key ctl-x-4-map "0" 'kill-buffer-and-window) |
76d7458e | 547 | |
fe7a0e7d | 548 | ;;; window.el ends here |