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