Port various comments from new VC to reduce the noise in the diff.
[emacs.git] / lisp / window.el
blob26d1bdc9d3f9498f8e693aa34665970fe02d6b6d
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Window tree functions.
30 ;;; Code:
32 (defvar window-size-fixed nil
33 "*Non-nil in a buffer means windows displaying the buffer are fixed-size.
34 If the value is `height', then only the window's height is fixed.
35 If the value is `width', then only the window's width is fixed.
36 Any other non-nil value fixes both the width and the height.
37 Emacs won't change the size of any window displaying that buffer,
38 unless you explicitly change the size, or Emacs has no other choice.")
39 (make-variable-buffer-local 'window-size-fixed)
41 (defmacro save-selected-window (&rest body)
42 "Execute BODY, then select the window that was selected before BODY.
43 The value returned is the value of the last form in BODY.
45 This macro saves and restores the current buffer, since otherwise
46 its normal operation could potentially make a different
47 buffer current. It does not alter the buffer list ordering.
49 This macro saves and restores the selected window, as well as
50 the selected window in each frame. If the previously selected
51 window of some frame is no longer live at the end of BODY, that
52 frame's selected window is left alone. If the selected window is
53 no longer live, then whatever window is selected at the end of
54 BODY remains selected."
55 `(let ((save-selected-window-window (selected-window))
56 ;; It is necessary to save all of these, because calling
57 ;; select-window changes frame-selected-window for whatever
58 ;; frame that window is in.
59 (save-selected-window-alist
60 (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
61 (frame-list))))
62 (save-current-buffer
63 (unwind-protect
64 (progn ,@body)
65 (dolist (elt save-selected-window-alist)
66 (and (frame-live-p (car elt))
67 (window-live-p (cdr elt))
68 (set-frame-selected-window (car elt) (cdr elt))))
69 (if (window-live-p save-selected-window-window)
70 (select-window save-selected-window-window))))))
72 (defun window-body-height (&optional window)
73 "Return number of lines in window WINDOW for actual buffer text.
74 This does not include the mode line (if any) or the header line (if any)."
75 (or window (setq window (selected-window)))
76 (if (window-minibuffer-p window)
77 (window-height window)
78 (with-current-buffer (window-buffer window)
79 (max 1 (- (window-height window)
80 (if mode-line-format 1 0)
81 (if header-line-format 1 0))))))
83 (defun one-window-p (&optional nomini all-frames)
84 "Return non-nil if the selected window is the only window.
85 Optional arg NOMINI non-nil means don't count the minibuffer
86 even if it is active. Otherwise, the minibuffer is counted
87 when it is active.
89 The optional arg ALL-FRAMES t means count windows on all frames.
90 If it is `visible', count windows on all visible frames.
91 ALL-FRAMES nil or omitted means count only the selected frame,
92 plus the minibuffer it uses (which may be on another frame).
93 ALL-FRAMES 0 means count all windows in all visible or iconified frames.
94 If ALL-FRAMES is anything else, count only the selected frame."
95 (let ((base-window (selected-window)))
96 (if (and nomini (eq base-window (minibuffer-window)))
97 (setq base-window (next-window base-window)))
98 (eq base-window
99 (next-window base-window (if nomini 'arg) all-frames))))
101 (defun window-current-scroll-bars (&optional window)
102 "Return the current scroll-bar settings in window WINDOW.
103 Value is a cons (VERTICAL . HORIZONTAL) where VERTICAL specifies the
104 current location of the vertical scroll-bars (left, right, or nil),
105 and HORIZONTAL specifies the current location of the horizontal scroll
106 bars (top, bottom, or nil)."
107 (let ((vert (nth 2 (window-scroll-bars window)))
108 (hor nil))
109 (when (or (eq vert t) (eq hor t))
110 (let ((fcsb (frame-current-scroll-bars
111 (window-frame (or window (selected-window))))))
112 (if (eq vert t)
113 (setq vert (car fcsb)))
114 (if (eq hor t)
115 (setq hor (cdr fcsb)))))
116 (cons vert hor)))
118 (defun walk-windows (proc &optional minibuf all-frames)
119 "Cycle through all visible windows, calling PROC for each one.
120 PROC is called with a window as argument.
122 Optional second arg MINIBUF t means count the minibuffer window even
123 if not active. MINIBUF nil or omitted means count the minibuffer only if
124 it is active. MINIBUF neither t nor nil means not to count the
125 minibuffer even if it is active.
127 Several frames may share a single minibuffer; if the minibuffer
128 counts, all windows on all frames that share that minibuffer count
129 too. Therefore, if you are using a separate minibuffer frame
130 and the minibuffer is active and MINIBUF says it counts,
131 `walk-windows' includes the windows in the frame from which you
132 entered the minibuffer, as well as the minibuffer window.
134 ALL-FRAMES is the optional third argument.
135 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
136 ALL-FRAMES = `visible' means include windows on all visible frames.
137 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
138 ALL-FRAMES = t means include windows on all frames including invisible frames.
139 If ALL-FRAMES is a frame, it means include windows on that frame.
140 Anything else means restrict to the selected frame."
141 ;; If we start from the minibuffer window, don't fail to come back to it.
142 (if (window-minibuffer-p (selected-window))
143 (setq minibuf t))
144 (save-selected-window
145 (if (framep all-frames)
146 (select-window (frame-first-window all-frames)))
147 (let* (walk-windows-already-seen
148 (walk-windows-current (selected-window)))
149 (while (progn
150 (setq walk-windows-current
151 (next-window walk-windows-current minibuf all-frames))
152 (not (memq walk-windows-current walk-windows-already-seen)))
153 (setq walk-windows-already-seen
154 (cons walk-windows-current walk-windows-already-seen))
155 (funcall proc walk-windows-current)))))
157 (defun get-window-with-predicate (predicate &optional minibuf
158 all-frames default)
159 "Return a window satisfying PREDICATE.
161 This function cycles through all visible windows using `walk-windows',
162 calling PREDICATE on each one. PREDICATE is called with a window as
163 argument. The first window for which PREDICATE returns a non-nil
164 value is returned. If no window satisfies PREDICATE, DEFAULT is
165 returned.
167 Optional second arg MINIBUF t means count the minibuffer window even
168 if not active. MINIBUF nil or omitted means count the minibuffer only if
169 it is active. MINIBUF neither t nor nil means not to count the
170 minibuffer even if it is active.
172 Several frames may share a single minibuffer; if the minibuffer
173 counts, all windows on all frames that share that minibuffer count
174 too. Therefore, if you are using a separate minibuffer frame
175 and the minibuffer is active and MINIBUF says it counts,
176 `walk-windows' includes the windows in the frame from which you
177 entered the minibuffer, as well as the minibuffer window.
179 ALL-FRAMES is the optional third argument.
180 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
181 ALL-FRAMES = `visible' means include windows on all visible frames.
182 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
183 ALL-FRAMES = t means include windows on all frames including invisible frames.
184 If ALL-FRAMES is a frame, it means include windows on that frame.
185 Anything else means restrict to the selected frame."
186 (catch 'found
187 (walk-windows #'(lambda (window)
188 (when (funcall predicate window)
189 (throw 'found window)))
190 minibuf all-frames)
191 default))
193 (defalias 'some-window 'get-window-with-predicate)
195 ;; This should probably be written in C (i.e., without using `walk-windows').
196 (defun get-buffer-window-list (buffer &optional minibuf frame)
197 "Return list of all windows displaying BUFFER, or nil if none.
198 BUFFER can be a buffer or a buffer name.
199 See `walk-windows' for the meaning of MINIBUF and FRAME."
200 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
201 (walk-windows (function (lambda (window)
202 (if (eq (window-buffer window) buffer)
203 (setq windows (cons window windows)))))
204 minibuf frame)
205 windows))
207 (defun minibuffer-window-active-p (window)
208 "Return t if WINDOW (a minibuffer window) is now active."
209 (eq window (active-minibuffer-window)))
211 (defun count-windows (&optional minibuf)
212 "Return the number of visible windows.
213 This counts the windows in the selected frame and (if the minibuffer is
214 to be counted) its minibuffer frame (if that's not the same frame).
215 The optional arg MINIBUF non-nil means count the minibuffer
216 even if it is inactive."
217 (let ((count 0))
218 (walk-windows (function (lambda (w)
219 (setq count (+ count 1))))
220 minibuf)
221 count))
223 (defun window-safely-shrinkable-p (&optional window)
224 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
225 If WINDOW is nil or omitted, it defaults to the currently selected window."
226 (with-selected-window (or window (selected-window))
227 (let ((edges (window-edges)))
228 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
229 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 ;;; `balance-windows' subroutines using `window-tree'
235 ;;; Translate from internal window tree format
237 (defun bw-get-tree (&optional window-or-frame)
238 "Get a window split tree in our format.
240 WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil,
241 then the whole window split tree for `selected-frame' is returned.
242 If it is a frame, then this is used instead. If it is a window,
243 then the smallest tree containing that window is returned."
244 (when window-or-frame
245 (unless (or (framep window-or-frame)
246 (windowp window-or-frame))
247 (error "Not a frame or window: %s" window-or-frame)))
248 (let ((subtree (bw-find-tree-sub window-or-frame)))
249 (when subtree
250 (if (integerp subtree)
252 (bw-get-tree-1 subtree)))))
254 (defun bw-get-tree-1 (split)
255 (if (windowp split)
256 split
257 (let ((dir (car split))
258 (edges (car (cdr split)))
259 (childs (cdr (cdr split))))
260 (list
261 (cons 'dir (if dir 'ver 'hor))
262 (cons 'b (nth 3 edges))
263 (cons 'r (nth 2 edges))
264 (cons 't (nth 1 edges))
265 (cons 'l (nth 0 edges))
266 (cons 'childs (mapcar #'bw-get-tree-1 childs))))))
268 (defun bw-find-tree-sub (window-or-frame &optional get-parent)
269 (let* ((window (when (windowp window-or-frame) window-or-frame))
270 (frame (when (windowp window) (window-frame window)))
271 (wt (car (window-tree frame))))
272 (when (< 1 (length (window-list frame 0)))
273 (if window
274 (bw-find-tree-sub-1 wt window get-parent)
275 wt))))
277 (defun bw-find-tree-sub-1 (tree win &optional get-parent)
278 (unless (windowp win) (error "Not a window: %s" win))
279 (if (memq win tree)
280 (if get-parent
281 get-parent
282 tree)
283 (let ((childs (cdr (cdr tree)))
284 child
285 subtree)
286 (while (and childs (not subtree))
287 (setq child (car childs))
288 (setq childs (cdr childs))
289 (when (and child (listp child))
290 (setq subtree (bw-find-tree-sub-1 child win get-parent))))
291 (if (integerp subtree)
292 (progn
293 (if (= 1 subtree)
294 tree
295 (1- subtree)))
296 subtree
297 ))))
299 ;;; Window or object edges
301 (defun bw-l (obj)
302 "Left edge of OBJ."
303 (if (windowp obj) (nth 0 (window-edges obj)) (cdr (assq 'l obj))))
304 (defun bw-t (obj)
305 "Top edge of OBJ."
306 (if (windowp obj) (nth 1 (window-edges obj)) (cdr (assq 't obj))))
307 (defun bw-r (obj)
308 "Right edge of OBJ."
309 (if (windowp obj) (nth 2 (window-edges obj)) (cdr (assq 'r obj))))
310 (defun bw-b (obj)
311 "Bottom edge of OBJ."
312 (if (windowp obj) (nth 3 (window-edges obj)) (cdr (assq 'b obj))))
314 ;;; Split directions
316 (defun bw-dir (obj)
317 "Return window split tree direction if OBJ.
318 If OBJ is a window return 'both. If it is a window split tree
319 then return its direction."
320 (if (symbolp obj)
322 (if (windowp obj)
323 'both
324 (let ((dir (cdr (assq 'dir obj))))
325 (unless (memq dir '(hor ver both))
326 (error "Can't find dir in %s" obj))
327 dir))))
329 (defun bw-eqdir (obj1 obj2)
330 "Return t if window split tree directions are equal.
331 OBJ1 and OBJ2 should be either windows or window split trees in
332 our format. The directions returned by `bw-dir' are compared and
333 t is returned if they are `eq' or one of them is 'both."
334 (let ((dir1 (bw-dir obj1))
335 (dir2 (bw-dir obj2)))
336 (or (eq dir1 dir2)
337 (eq dir1 'both)
338 (eq dir2 'both))))
340 ;;; Building split tree
342 (defun bw-refresh-edges (obj)
343 "Refresh the edge information of OBJ and return OBJ."
344 (unless (windowp obj)
345 (let ((childs (cdr (assq 'childs obj)))
346 (ol 1000)
347 (ot 1000)
348 (or -1)
349 (ob -1))
350 (dolist (o childs)
351 (when (> ol (bw-l o)) (setq ol (bw-l o)))
352 (when (> ot (bw-t o)) (setq ot (bw-t o)))
353 (when (< or (bw-r o)) (setq or (bw-r o)))
354 (when (< ob (bw-b o)) (setq ob (bw-b o))))
355 (setq obj (delq 'l obj))
356 (setq obj (delq 't obj))
357 (setq obj (delq 'r obj))
358 (setq obj (delq 'b obj))
359 (add-to-list 'obj (cons 'l ol))
360 (add-to-list 'obj (cons 't ot))
361 (add-to-list 'obj (cons 'r or))
362 (add-to-list 'obj (cons 'b ob))
364 obj)
366 ;;; Balance windows
368 (defun balance-windows (&optional window-or-frame)
369 "Make windows the same heights or widths in window split subtrees.
371 When called non-interactively WINDOW-OR-FRAME may be either a
372 window or a frame. It then balances the windows on the implied
373 frame. If the parameter is a window only the corresponding window
374 subtree is balanced."
375 (interactive)
376 (let (
377 (wt (bw-get-tree window-or-frame))
380 (tried-sizes)
381 (last-sizes)
382 (windows (window-list nil 0))
383 (counter 0))
384 (when wt
385 (while (not (member last-sizes tried-sizes))
386 (when last-sizes (setq tried-sizes (cons last-sizes tried-sizes)))
387 (setq last-sizes (mapcar (lambda (w)
388 (window-edges w))
389 windows))
390 (when (eq 'hor (bw-dir wt))
391 (setq w (- (bw-r wt) (bw-l wt))))
392 (when (eq 'ver (bw-dir wt))
393 (setq h (- (bw-b wt) (bw-t wt))))
394 (bw-balance-sub wt w h)))))
396 (defun bw-adjust-window (window delta horizontal)
397 "Wrapper around `adjust-window-trailing-edge' with error checking.
398 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
399 ;; `adjust-window-trailing-edge' may fail if delta is too large.
400 (while (>= (abs delta) 1)
401 (condition-case err
402 (progn
403 (adjust-window-trailing-edge window delta horizontal)
404 (setq delta 0))
405 (error
406 ;;(message "adjust: %s" (error-message-string err))
407 (setq delta (/ delta 2))))))
409 (defun bw-balance-sub (wt w h)
410 (setq wt (bw-refresh-edges wt))
411 (unless w (setq w (- (bw-r wt) (bw-l wt))))
412 (unless h (setq h (- (bw-b wt) (bw-t wt))))
413 (if (windowp wt)
414 (progn
415 (when w
416 (let ((dw (- w (- (bw-r wt) (bw-l wt)))))
417 (when (/= 0 dw)
418 (bw-adjust-window wt dw t))))
419 (when h
420 (let ((dh (- h (- (bw-b wt) (bw-t wt)))))
421 (when (/= 0 dh)
422 (bw-adjust-window wt dh nil)))))
423 (let* ((childs (cdr (assq 'childs wt)))
424 (lastchild (car (last childs)))
425 (cw (when w (/ w (if (bw-eqdir 'hor wt) (length childs) 1))))
426 (ch (when h (/ h (if (bw-eqdir 'ver wt) (length childs) 1)))))
427 (dolist (c childs)
428 (bw-balance-sub c cw ch)))))
430 ;;; A different solution to balance-windows
432 (defun window-fixed-size-p (&optional window direction)
433 "Non-nil if WINDOW cannot be resized in DIRECTION.
434 DIRECTION can be nil (i.e. any), `height' or `width'."
435 (with-current-buffer (window-buffer window)
436 (let ((fixed (and (boundp 'window-size-fixed) window-size-fixed)))
437 (when fixed
438 (not (and direction
439 (member (cons direction window-size-fixed)
440 '((height . width) (width . height)))))))))
442 (defvar window-area-factor 1
443 "Factor by which the window area should be over-estimated.
444 This is used by `balance-windows-area'.
445 Changing this globally has no effect.")
447 (defun balance-windows-area ()
448 "Make all visible windows the same area (approximately).
449 See also `window-area-factor' to change the relative size of specific buffers."
450 (interactive)
451 (let* ((unchanged 0) (carry 0) (round 0)
452 ;; Remove fixed-size windows.
453 (wins (delq nil (mapcar (lambda (win)
454 (if (not (window-fixed-size-p win)) win))
455 (window-list nil 'nomini))))
456 (changelog nil)
457 next)
458 ;; Resizing a window changes the size of surrounding windows in complex
459 ;; ways, so it's difficult to balance them all. The introduction of
460 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
461 ;; very difficult to do. `balance-window' above takes an off-line
462 ;; approach: get the whole window tree, then balance it, then try to
463 ;; adjust the windows so they fit the result.
464 ;; Here, instead, we take a "local optimization" approach, where we just
465 ;; go through all the windows several times until nothing needs to be
466 ;; changed. The main problem with this approach is that it's difficult
467 ;; to make sure it terminates, so we use some heuristic to try and break
468 ;; off infinite loops.
469 ;; After a round without any change, we allow a second, to give a chance
470 ;; to the carry to propagate a minor imbalance from the end back to
471 ;; the beginning.
472 (while (< unchanged 2)
473 ;; (message "New round")
474 (setq unchanged (1+ unchanged) round (1+ round))
475 (dolist (win wins)
476 (setq next win)
477 (while (progn (setq next (next-window next))
478 (window-fixed-size-p next)))
479 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
480 (let* ((horiz
481 (< (car (window-edges win)) (car (window-edges next))))
482 (areadiff (/ (- (* (window-height next) (window-width next)
483 (buffer-local-value 'window-area-factor
484 (window-buffer next)))
485 (* (window-height win) (window-width win)
486 (buffer-local-value 'window-area-factor
487 (window-buffer win))))
488 (max (buffer-local-value 'window-area-factor
489 (window-buffer win))
490 (buffer-local-value 'window-area-factor
491 (window-buffer next)))))
492 (edgesize (if horiz
493 (+ (window-height win) (window-height next))
494 (+ (window-width win) (window-width next))))
495 (diff (/ areadiff edgesize)))
496 (when (zerop diff)
497 ;; Maybe diff is actually closer to 1 than to 0.
498 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
499 (when (and (zerop diff) (not (zerop areadiff)))
500 (setq diff (/ (+ areadiff carry) edgesize))
501 ;; Change things smoothly.
502 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
503 (if (zerop diff)
504 ;; Make sure negligible differences don't accumulate to
505 ;; become significant.
506 (setq carry (+ carry areadiff))
507 (bw-adjust-window win diff horiz)
508 ;; (sit-for 0.5)
509 (let ((change (cons win (window-edges win))))
510 ;; If the same change has been seen already for this window,
511 ;; we're most likely in an endless loop, so don't count it as
512 ;; a change.
513 (unless (member change changelog)
514 (push change changelog)
515 (setq unchanged 0 carry 0)))))))
516 ;; We've now basically balanced all the windows.
517 ;; But there may be some minor off-by-one imbalance left over,
518 ;; so let's do some fine tuning.
519 ;; (bw-finetune wins)
520 ;; (message "Done in %d rounds" round)
523 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
525 ;; I think this should be the default; I think people will prefer it--rms.
526 (defcustom split-window-keep-point t
527 "*If non-nil, \\[split-window-vertically] keeps the original point \
528 in both children.
529 This is often more convenient for editing.
530 If nil, adjust point in each of the two windows to minimize redisplay.
531 This is convenient on slow terminals, but point can move strangely.
533 This option applies only to `split-window-vertically' and
534 functions that call it. `split-window' always keeps the original
535 point in both children."
536 :type 'boolean
537 :group 'windows)
539 (defun split-window-vertically (&optional arg)
540 "Split current window into two windows, one above the other.
541 The uppermost window gets ARG lines and the other gets the rest.
542 Negative ARG means select the size of the lowermost window instead.
543 With no argument, split equally or close to it.
544 Both windows display the same buffer now current.
546 If the variable `split-window-keep-point' is non-nil, both new windows
547 will get the same value of point as the current window. This is often
548 more convenient for editing. The upper window is the selected window.
550 Otherwise, we choose window starts so as to minimize the amount of
551 redisplay; this is convenient on slow terminals. The new selected
552 window is the one that the current value of point appears in. The
553 value of point can change if the text around point is hidden by the
554 new mode line.
556 Regardless of the value of `split-window-keep-point', the upper
557 window is the original one and the return value is the new, lower
558 window."
559 (interactive "P")
560 (let ((old-w (selected-window))
561 (old-point (point))
562 (size (and arg (prefix-numeric-value arg)))
563 (window-full-p nil)
564 new-w bottom switch moved)
565 (and size (< size 0) (setq size (+ (window-height) size)))
566 (setq new-w (split-window nil size))
567 (or split-window-keep-point
568 (progn
569 (save-excursion
570 (set-buffer (window-buffer))
571 (goto-char (window-start))
572 (setq moved (vertical-motion (window-height)))
573 (set-window-start new-w (point))
574 (if (> (point) (window-point new-w))
575 (set-window-point new-w (point)))
576 (and (= moved (window-height))
577 (progn
578 (setq window-full-p t)
579 (vertical-motion -1)))
580 (setq bottom (point)))
581 (and window-full-p
582 (<= bottom (point))
583 (set-window-point old-w (1- bottom)))
584 (and window-full-p
585 (<= (window-start new-w) old-point)
586 (progn
587 (set-window-point new-w old-point)
588 (select-window new-w)))))
589 (split-window-save-restore-data new-w old-w)))
591 ;; This is to avoid compiler warnings.
592 (defvar view-return-to-alist)
594 (defun split-window-save-restore-data (new-w old-w)
595 (with-current-buffer (window-buffer)
596 (if view-mode
597 (let ((old-info (assq old-w view-return-to-alist)))
598 (if old-info
599 (push (cons new-w (cons (car (cdr old-info)) t))
600 view-return-to-alist))))
601 new-w))
603 (defun split-window-horizontally (&optional arg)
604 "Split current window into two windows side by side.
605 This window becomes the leftmost of the two, and gets ARG columns.
606 Negative ARG means select the size of the rightmost window instead.
607 The argument includes the width of the window's scroll bar; if there
608 are no scroll bars, it includes the width of the divider column
609 to the window's right, if any. No ARG means split equally.
611 The original, leftmost window remains selected.
612 The return value is the new, rightmost window."
613 (interactive "P")
614 (let ((old-w (selected-window))
615 (size (and arg (prefix-numeric-value arg))))
616 (and size (< size 0)
617 (setq size (+ (window-width) size)))
618 (split-window-save-restore-data (split-window nil size t) old-w)))
621 (defun set-window-text-height (window height)
622 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
623 This doesn't include the mode-line (or header-line if any) or any
624 partial-height lines in the text display area.
626 If WINDOW is nil, the selected window is used.
628 Note that the current implementation of this function cannot always set
629 the height exactly, but attempts to be conservative, by allocating more
630 lines than are actually needed in the case where some error may be present."
631 (let ((delta (- height (window-text-height window))))
632 (unless (zerop delta)
633 ;; Setting window-min-height to a value like 1 can lead to very
634 ;; bizarre displays because it also allows Emacs to make *other*
635 ;; windows 1-line tall, which means that there's no more space for
636 ;; the modeline.
637 (let ((window-min-height (min 2 height))) ;One text line plus a modeline.
638 (if (and window (not (eq window (selected-window))))
639 (save-selected-window
640 (select-window window)
641 (enlarge-window delta))
642 (enlarge-window delta))))))
645 (defun enlarge-window-horizontally (arg)
646 "Make current window ARG columns wider."
647 (interactive "p")
648 (enlarge-window arg t))
650 (defun shrink-window-horizontally (arg)
651 "Make current window ARG columns narrower."
652 (interactive "p")
653 (shrink-window arg t))
655 (defun window-buffer-height (window)
656 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
657 (with-current-buffer (window-buffer window)
658 (max 1
659 (count-screen-lines (point-min) (point-max)
660 ;; If buffer ends with a newline, ignore it when
661 ;; counting height unless point is after it.
662 (eobp)
663 window))))
665 (defun count-screen-lines (&optional beg end count-final-newline window)
666 "Return the number of screen lines in the region.
667 The number of screen lines may be different from the number of actual lines,
668 due to line breaking, display table, etc.
670 Optional arguments BEG and END default to `point-min' and `point-max'
671 respectively.
673 If region ends with a newline, ignore it unless optional third argument
674 COUNT-FINAL-NEWLINE is non-nil.
676 The optional fourth argument WINDOW specifies the window used for obtaining
677 parameters such as width, horizontal scrolling, and so on. The default is
678 to use the selected window's parameters.
680 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
681 regardless of which buffer is displayed in WINDOW. This makes possible to use
682 `count-screen-lines' in any buffer, whether or not it is currently displayed
683 in some window."
684 (unless beg
685 (setq beg (point-min)))
686 (unless end
687 (setq end (point-max)))
688 (if (= beg end)
690 (save-excursion
691 (save-restriction
692 (widen)
693 (narrow-to-region (min beg end)
694 (if (and (not count-final-newline)
695 (= ?\n (char-before (max beg end))))
696 (1- (max beg end))
697 (max beg end)))
698 (goto-char (point-min))
699 (1+ (vertical-motion (buffer-size) window))))))
701 (defun fit-window-to-buffer (&optional window max-height min-height)
702 "Make WINDOW the right height to display its contents exactly.
703 If WINDOW is omitted or nil, it defaults to the selected window.
704 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
705 the window is allowed to be, defaulting to the frame height.
706 If the optional argument MIN-HEIGHT is supplied, it is the minimum
707 height the window is allowed to be, defaulting to `window-min-height'.
709 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
710 header-line."
711 (interactive)
713 (when (null window)
714 (setq window (selected-window)))
715 (when (null max-height)
716 (setq max-height (frame-height (window-frame window))))
718 (let* ((buf
719 ;; Buffer that is displayed in WINDOW
720 (window-buffer window))
721 (window-height
722 ;; The current height of WINDOW
723 (window-height window))
724 (desired-height
725 ;; The height necessary to show the buffer displayed by WINDOW
726 ;; (`count-screen-lines' always works on the current buffer).
727 (with-current-buffer buf
728 (+ (count-screen-lines)
729 ;; If the buffer is empty, (count-screen-lines) is
730 ;; zero. But, even in that case, we need one text line
731 ;; for cursor.
732 (if (= (point-min) (point-max))
733 1 0)
734 ;; For non-minibuffers, count the mode-line, if any
735 (if (and (not (window-minibuffer-p window))
736 mode-line-format)
737 1 0)
738 ;; Count the header-line, if any
739 (if header-line-format 1 0))))
740 (delta
741 ;; Calculate how much the window height has to change to show
742 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
743 (- (max (min desired-height max-height)
744 (or min-height window-min-height))
745 window-height)))
747 ;; Don't try to redisplay with the cursor at the end
748 ;; on its own line--that would force a scroll and spoil things.
749 (when (with-current-buffer buf
750 (and (eobp) (bolp) (not (bobp))))
751 (set-window-point window (1- (window-point window))))
753 (save-selected-window
754 (select-window window)
756 ;; Adjust WINDOW to the nominally correct size (which may actually
757 ;; be slightly off because of variable height text, etc).
758 (unless (zerop delta)
759 (enlarge-window delta))
761 ;; Check if the last line is surely fully visible. If not,
762 ;; enlarge the window.
763 (let ((end (with-current-buffer buf
764 (save-excursion
765 (goto-char (point-max))
766 (when (and (bolp) (not (bobp)))
767 ;; Don't include final newline
768 (backward-char 1))
769 (when truncate-lines
770 ;; If line-wrapping is turned off, test the
771 ;; beginning of the last line for visibility
772 ;; instead of the end, as the end of the line
773 ;; could be invisible by virtue of extending past
774 ;; the edge of the window.
775 (forward-line 0))
776 (point)))))
777 (set-window-vscroll window 0)
778 (while (and (< desired-height max-height)
779 (= desired-height (window-height window))
780 (not (pos-visible-in-window-p end window)))
781 (enlarge-window 1)
782 (setq desired-height (1+ desired-height)))))))
784 (defun shrink-window-if-larger-than-buffer (&optional window)
785 "Shrink the WINDOW to be as small as possible to display its contents.
786 If WINDOW is omitted or nil, it defaults to the selected window.
787 Do not shrink to less than `window-min-height' lines.
788 Do nothing if the buffer contains more lines than the present window height,
789 or if some of the window's contents are scrolled out of view,
790 or if shrinking this window would also shrink another window,
791 or if the window is the only window of its frame."
792 (interactive)
793 (when (null window)
794 (setq window (selected-window)))
795 (let* ((frame (window-frame window))
796 (mini (frame-parameter frame 'minibuffer))
797 (edges (window-edges window)))
798 (if (and (not (eq window (frame-root-window frame)))
799 (window-safely-shrinkable-p)
800 (pos-visible-in-window-p (point-min) window)
801 (not (eq mini 'only))
802 (or (not mini)
803 (let ((mini-window (minibuffer-window frame)))
804 (or (null mini-window)
805 (not (eq frame (window-frame mini-window)))
806 (< (nth 3 edges)
807 (nth 1 (window-edges mini-window)))
808 (> (nth 1 edges)
809 (frame-parameter frame 'menu-bar-lines))))))
810 (fit-window-to-buffer window (window-height window)))))
812 (defun kill-buffer-and-window ()
813 "Kill the current buffer and delete the selected window."
814 (interactive)
815 (let ((window-to-delete (selected-window))
816 (buffer-to-kill (current-buffer))
817 (delete-window-hook (lambda ()
818 (condition-case nil
819 (delete-window)
820 (error nil)))))
821 (unwind-protect
822 (progn
823 (add-hook 'kill-buffer-hook delete-window-hook t t)
824 (if (kill-buffer (current-buffer))
825 ;; If `delete-window' failed before, we rerun it to regenerate
826 ;; the error so it can be seen in the echo area.
827 (when (eq (selected-window) window-to-delete)
828 (delete-window))))
829 ;; If the buffer is not dead for some reason (probably because
830 ;; of a `quit' signal), remove the hook again.
831 (condition-case nil
832 (with-current-buffer buffer-to-kill
833 (remove-hook 'kill-buffer-hook delete-window-hook t))
834 (error nil)))))
836 (defun quit-window (&optional kill window)
837 "Quit the current buffer. Bury it, and maybe delete the selected frame.
838 \(The frame is deleted if it contains a dedicated window for the buffer.)
839 With a prefix argument, kill the buffer instead.
841 Noninteractively, if KILL is non-nil, then kill the current buffer,
842 otherwise bury it.
844 If WINDOW is non-nil, it specifies a window; we delete that window,
845 and the buffer that is killed or buried is the one in that window."
846 (interactive "P")
847 (let ((buffer (window-buffer window))
848 (frame (window-frame (or window (selected-window))))
849 (window-solitary
850 (save-selected-window
851 (if window
852 (select-window window))
853 (one-window-p t)))
854 window-handled)
856 (save-selected-window
857 (if window
858 (select-window window))
859 (or (window-minibuffer-p)
860 (window-dedicated-p (selected-window))
861 (switch-to-buffer (other-buffer))))
863 ;; Get rid of the frame, if it has just one dedicated window
864 ;; and other visible frames exist.
865 (and (or (window-minibuffer-p) (window-dedicated-p window))
866 (delq frame (visible-frame-list))
867 window-solitary
868 (if (and (eq default-minibuffer-frame frame)
869 (= 1 (length (minibuffer-frame-list))))
870 (setq window nil)
871 (delete-frame frame)
872 (setq window-handled t)))
874 ;; Deal with the buffer.
875 (if kill
876 (kill-buffer buffer)
877 (bury-buffer buffer))
879 ;; Maybe get rid of the window.
880 (and window (not window-handled) (not window-solitary)
881 (delete-window window))))
883 (defvar mouse-autoselect-window-timer nil
884 "Timer used by delayed window autoselection.")
886 (defvar mouse-autoselect-window-position nil
887 "Last mouse position recorded by delayed window autoselection.")
889 (defvar mouse-autoselect-window-window nil
890 "Last window recorded by delayed window autoselection.")
892 (defvar mouse-autoselect-window-state nil
893 "When non-nil, special state of delayed window autoselection.
894 Possible values are `suspend' \(suspend autoselection after a menu or
895 scrollbar interaction\) and `select' \(the next invocation of
896 'handle-select-window' shall select the window immediately\).")
898 (defun mouse-autoselect-window-cancel (&optional force)
899 "Cancel delayed window autoselection.
900 Optional argument FORCE means cancel unconditionally."
901 (unless (and (not force)
902 ;; Don't cancel while the user drags a scroll bar.
903 (eq this-command 'scroll-bar-toolkit-scroll)
904 (memq (nth 4 (event-end last-input-event))
905 '(handle end-scroll)))
906 (setq mouse-autoselect-window-state nil)
907 (when (timerp mouse-autoselect-window-timer)
908 (cancel-timer mouse-autoselect-window-timer))
909 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
911 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
912 "Start delayed window autoselection.
913 MOUSE-POSITION is the last position where the mouse was seen as returned
914 by `mouse-position'. Optional argument WINDOW non-nil denotes the
915 window where the mouse was seen. Optional argument SUSPEND non-nil
916 means suspend autoselection."
917 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
918 (setq mouse-autoselect-window-position mouse-position)
919 (when window (setq mouse-autoselect-window-window window))
920 (setq mouse-autoselect-window-state (when suspend 'suspend))
921 ;; Install timer which runs `mouse-autoselect-window-select' after
922 ;; `mouse-autoselect-window' seconds.
923 (setq mouse-autoselect-window-timer
924 (run-at-time
925 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
927 (defun mouse-autoselect-window-select ()
928 "Select window with delayed window autoselection.
929 If the mouse position has stabilized in a non-selected window, select
930 that window. The minibuffer window is selected only if the minibuffer is
931 active. This function is run by `mouse-autoselect-window-timer'."
932 (condition-case nil
933 (let* ((mouse-position (mouse-position))
934 (window
935 (condition-case nil
936 (window-at (cadr mouse-position) (cddr mouse-position)
937 (car mouse-position))
938 (error nil))))
939 (cond
940 ((or (menu-or-popup-active-p)
941 (and window
942 (not (coordinates-in-window-p (cdr mouse-position) window))))
943 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
944 ;; of WINDOW, temporarily suspend delayed autoselection.
945 (mouse-autoselect-window-start mouse-position nil t))
946 ((eq mouse-autoselect-window-state 'suspend)
947 ;; Delayed autoselection was temporarily suspended, reenable it.
948 (mouse-autoselect-window-start mouse-position))
949 ((and window (not (eq window (selected-window)))
950 (or (not (numberp mouse-autoselect-window))
951 (and (> mouse-autoselect-window 0)
952 ;; If `mouse-autoselect-window' is positive, select
953 ;; window if the window is the same as before.
954 (eq window mouse-autoselect-window-window))
955 ;; Otherwise select window if the mouse is at the same
956 ;; position as before. Observe that the first test after
957 ;; starting autoselection usually fails since the value of
958 ;; `mouse-autoselect-window-position' recorded there is the
959 ;; position where the mouse has entered the new window and
960 ;; not necessarily where the mouse has stopped moving.
961 (equal mouse-position mouse-autoselect-window-position))
962 ;; The minibuffer is a candidate window if it's active.
963 (or (not (window-minibuffer-p window))
964 (eq window (active-minibuffer-window))))
965 ;; Mouse position has stabilized in non-selected window: Cancel
966 ;; delayed autoselection and try to select that window.
967 (mouse-autoselect-window-cancel t)
968 ;; Select window where mouse appears unless the selected window is the
969 ;; minibuffer. Use `unread-command-events' in order to execute pre-
970 ;; and post-command hooks and trigger idle timers. To avoid delaying
971 ;; autoselection again, set `mouse-autoselect-window-state'."
972 (unless (window-minibuffer-p (selected-window))
973 (setq mouse-autoselect-window-state 'select)
974 (setq unread-command-events
975 (cons (list 'select-window (list window))
976 unread-command-events))))
977 ((or (and window (eq window (selected-window)))
978 (not (numberp mouse-autoselect-window))
979 (equal mouse-position mouse-autoselect-window-position))
980 ;; Mouse position has either stabilized in the selected window or at
981 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
982 (mouse-autoselect-window-cancel t))
984 ;; Mouse position has not stabilized yet, resume delayed
985 ;; autoselection.
986 (mouse-autoselect-window-start mouse-position window))))
987 (error nil)))
989 (defun handle-select-window (event)
990 "Handle select-window events."
991 (interactive "e")
992 (let ((window (posn-window (event-start event))))
993 (when (and (window-live-p window)
994 ;; Don't switch if we're currently in the minibuffer.
995 ;; This tries to work around problems where the minibuffer gets
996 ;; unselected unexpectedly, and where you then have to move
997 ;; your mouse all the way down to the minibuffer to select it.
998 (not (window-minibuffer-p (selected-window)))
999 ;; Don't switch to a minibuffer window unless it's active.
1000 (or (not (window-minibuffer-p window))
1001 (minibuffer-window-active-p window)))
1002 (unless (and (numberp mouse-autoselect-window)
1003 (not (zerop mouse-autoselect-window))
1004 (not (eq mouse-autoselect-window-state 'select))
1005 (progn
1006 ;; Cancel any delayed autoselection.
1007 (mouse-autoselect-window-cancel t)
1008 ;; Start delayed autoselection from current mouse position
1009 ;; and window.
1010 (mouse-autoselect-window-start (mouse-position) window)
1011 ;; Executing a command cancels delayed autoselection.
1012 (add-hook
1013 'pre-command-hook 'mouse-autoselect-window-cancel)))
1014 ;; Reset state of delayed autoselection.
1015 (setq mouse-autoselect-window-state nil)
1016 (when mouse-autoselect-window
1017 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
1018 (run-hooks 'mouse-leave-buffer-hook))
1019 (select-window window)))))
1021 (define-key ctl-x-map "2" 'split-window-vertically)
1022 (define-key ctl-x-map "3" 'split-window-horizontally)
1023 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
1024 (define-key ctl-x-map "{" 'shrink-window-horizontally)
1025 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
1026 (define-key ctl-x-map "+" 'balance-windows)
1027 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
1029 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
1030 ;;; window.el ends here