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