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