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