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