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, 2008 Free Software Foundation, Inc.
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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;; Window tree functions.
30 (defvar window-size-fixed nil
31 "*Non-nil in a buffer means windows displaying the buffer are fixed-size.
32 If the value is `height', then only the window's height is fixed.
33 If the value is `width', then only the window's width is fixed.
34 Any other non-nil value fixes both the width and the height.
35 Emacs won't change the size of any window displaying that buffer,
36 unless you explicitly change the size, or Emacs has no other choice.")
37 (make-variable-buffer-local 'window-size-fixed
)
39 (defmacro save-selected-window
(&rest body
)
40 "Execute BODY, then select the window that was selected before BODY.
41 The value returned is the value of the last form in BODY.
43 This macro saves and restores the current buffer, since otherwise
44 its normal operation could potentially make a different
45 buffer current. It does not alter the buffer list ordering.
47 This macro saves and restores the selected window, as well as
48 the selected window in each frame. If the previously selected
49 window of some frame is no longer live at the end of BODY, that
50 frame's selected window is left alone. If the selected window is
51 no longer live, then whatever window is selected at the end of
52 BODY remains selected."
53 `(let ((save-selected-window-window (selected-window))
54 ;; It is necessary to save all of these, because calling
55 ;; select-window changes frame-selected-window for whatever
56 ;; frame that window is in.
57 (save-selected-window-alist
58 (mapcar (lambda (frame) (cons frame
(frame-selected-window frame
)))
63 (dolist (elt save-selected-window-alist
)
64 (and (frame-live-p (car elt
))
65 (window-live-p (cdr elt
))
66 (set-frame-selected-window (car elt
) (cdr elt
))))
67 (if (window-live-p save-selected-window-window
)
68 (select-window save-selected-window-window
))))))
70 (defun window-body-height (&optional window
)
71 "Return number of lines in window WINDOW for actual buffer text.
72 This does not include the mode line (if any) or the header line (if any)."
73 (or window
(setq window
(selected-window)))
74 (if (window-minibuffer-p window
)
75 (window-height window
)
76 (with-current-buffer (window-buffer window
)
77 (max 1 (- (window-height window
)
78 (if mode-line-format
1 0)
79 (if header-line-format
1 0))))))
81 (defun one-window-p (&optional nomini all-frames
)
82 "Return non-nil if the selected window is the only window.
83 Optional arg NOMINI non-nil means don't count the minibuffer
84 even if it is active. Otherwise, the minibuffer is counted
87 The optional arg ALL-FRAMES t means count windows on all frames.
88 If it is `visible', count windows on all visible frames.
89 ALL-FRAMES nil or omitted means count only the selected frame,
90 plus the minibuffer it uses (which may be on another frame).
91 ALL-FRAMES 0 means count all windows in all visible or iconified frames.
92 If ALL-FRAMES is anything else, count only the selected frame."
93 (let ((base-window (selected-window)))
94 (if (and nomini
(eq base-window
(minibuffer-window)))
95 (setq base-window
(next-window base-window
)))
97 (next-window base-window
(if nomini
'arg
) all-frames
))))
99 (defun window-current-scroll-bars (&optional window
)
100 "Return the current scroll-bar settings in window WINDOW.
101 Value is a cons (VERTICAL . HORIZONTAL) where VERTICAL specifies the
102 current location of the vertical scroll-bars (left, right, or nil),
103 and HORIZONTAL specifies the current location of the horizontal scroll
104 bars (top, bottom, or nil)."
105 (let ((vert (nth 2 (window-scroll-bars window
)))
107 (when (or (eq vert t
) (eq hor t
))
108 (let ((fcsb (frame-current-scroll-bars
109 (window-frame (or window
(selected-window))))))
111 (setq vert
(car fcsb
)))
113 (setq hor
(cdr fcsb
)))))
116 (defun walk-windows (proc &optional minibuf all-frames
)
117 "Cycle through all visible windows, calling PROC for each one.
118 PROC is called with a window as argument.
120 Optional second arg MINIBUF t means count the minibuffer window even
121 if not active. MINIBUF nil or omitted means count the minibuffer only if
122 it is active. MINIBUF neither t nor nil means not to count the
123 minibuffer even if it is active.
125 Several frames may share a single minibuffer; if the minibuffer
126 counts, all windows on all frames that share that minibuffer count
127 too. Therefore, if you are using a separate minibuffer frame
128 and the minibuffer is active and MINIBUF says it counts,
129 `walk-windows' includes the windows in the frame from which you
130 entered the minibuffer, as well as the minibuffer window.
132 ALL-FRAMES is the optional third argument.
133 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
134 ALL-FRAMES = `visible' means include windows on all visible frames.
135 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
136 ALL-FRAMES = t means include windows on all frames including invisible frames.
137 If ALL-FRAMES is a frame, it means include windows on that frame.
138 Anything else means restrict to the selected frame."
139 ;; If we start from the minibuffer window, don't fail to come back to it.
140 (if (window-minibuffer-p (selected-window))
142 (save-selected-window
143 (if (framep all-frames
)
144 (select-window (frame-first-window all-frames
)))
145 (let* (walk-windows-already-seen
146 (walk-windows-current (selected-window)))
148 (setq walk-windows-current
149 (next-window walk-windows-current minibuf all-frames
))
150 (not (memq walk-windows-current walk-windows-already-seen
)))
151 (setq walk-windows-already-seen
152 (cons walk-windows-current walk-windows-already-seen
))
153 (funcall proc walk-windows-current
)))))
155 (defun get-window-with-predicate (predicate &optional minibuf
157 "Return a window satisfying PREDICATE.
159 This function cycles through all visible windows using `walk-windows',
160 calling PREDICATE on each one. PREDICATE is called with a window as
161 argument. The first window for which PREDICATE returns a non-nil
162 value is returned. If no window satisfies PREDICATE, DEFAULT is
165 Optional second arg MINIBUF t means count the minibuffer window even
166 if not active. MINIBUF nil or omitted means count the minibuffer only if
167 it is active. MINIBUF neither t nor nil means not to count the
168 minibuffer even if it is active.
170 Several frames may share a single minibuffer; if the minibuffer
171 counts, all windows on all frames that share that minibuffer count
172 too. Therefore, if you are using a separate minibuffer frame
173 and the minibuffer is active and MINIBUF says it counts,
174 `walk-windows' includes the windows in the frame from which you
175 entered the minibuffer, as well as the minibuffer window.
177 ALL-FRAMES is the optional third argument.
178 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
179 ALL-FRAMES = `visible' means include windows on all visible frames.
180 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
181 ALL-FRAMES = t means include windows on all frames including invisible frames.
182 If ALL-FRAMES is a frame, it means include windows on that frame.
183 Anything else means restrict to the selected frame."
185 (walk-windows #'(lambda (window)
186 (when (funcall predicate window
)
187 (throw 'found window
)))
191 (defalias 'some-window
'get-window-with-predicate
)
193 ;; This should probably be written in C (i.e., without using `walk-windows').
194 (defun get-buffer-window-list (buffer &optional minibuf frame
)
195 "Return list of all windows displaying BUFFER, or nil if none.
196 BUFFER can be a buffer or a buffer name.
197 See `walk-windows' for the meaning of MINIBUF and FRAME."
198 (let ((buffer (if (bufferp buffer
) buffer
(get-buffer buffer
))) windows
)
199 (walk-windows (function (lambda (window)
200 (if (eq (window-buffer window
) buffer
)
201 (setq windows
(cons window windows
)))))
205 (defun minibuffer-window-active-p (window)
206 "Return t if WINDOW (a minibuffer window) is now active."
207 (eq window
(active-minibuffer-window)))
209 (defun count-windows (&optional minibuf
)
210 "Return the number of visible windows.
211 This counts the windows in the selected frame and (if the minibuffer is
212 to be counted) its minibuffer frame (if that's not the same frame).
213 The optional arg MINIBUF non-nil means count the minibuffer
214 even if it is inactive."
216 (walk-windows (lambda (w) (setq count
(+ count
1)))
220 (defun window-safely-shrinkable-p (&optional window
)
221 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
222 If WINDOW is nil or omitted, it defaults to the currently selected window."
223 (with-selected-window (or window
(selected-window))
224 (let ((edges (window-edges)))
225 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
226 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
230 ;;; `balance-windows' subroutines using `window-tree'
232 ;;; Translate from internal window tree format
234 (defun bw-get-tree (&optional window-or-frame
)
235 "Get a window split tree in our format.
237 WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil,
238 then the whole window split tree for `selected-frame' is returned.
239 If it is a frame, then this is used instead. If it is a window,
240 then the smallest tree containing that window is returned."
241 (when window-or-frame
242 (unless (or (framep window-or-frame
)
243 (windowp window-or-frame
))
244 (error "Not a frame or window: %s" window-or-frame
)))
245 (let ((subtree (bw-find-tree-sub window-or-frame
)))
247 (if (integerp subtree
)
249 (bw-get-tree-1 subtree
)))))
251 (defun bw-get-tree-1 (split)
254 (let ((dir (car split
))
255 (edges (car (cdr split
)))
256 (childs (cdr (cdr split
))))
258 (cons 'dir
(if dir
'ver
'hor
))
259 (cons 'b
(nth 3 edges
))
260 (cons 'r
(nth 2 edges
))
261 (cons 't
(nth 1 edges
))
262 (cons 'l
(nth 0 edges
))
263 (cons 'childs
(mapcar #'bw-get-tree-1 childs
))))))
265 (defun bw-find-tree-sub (window-or-frame &optional get-parent
)
266 (let* ((window (when (windowp window-or-frame
) window-or-frame
))
267 (frame (when (windowp window
) (window-frame window
)))
268 (wt (car (window-tree frame
))))
269 (when (< 1 (length (window-list frame
0)))
271 (bw-find-tree-sub-1 wt window get-parent
)
274 (defun bw-find-tree-sub-1 (tree win
&optional get-parent
)
275 (unless (windowp win
) (error "Not a window: %s" win
))
280 (let ((childs (cdr (cdr tree
)))
283 (while (and childs
(not subtree
))
284 (setq child
(car childs
))
285 (setq childs
(cdr childs
))
286 (when (and child
(listp child
))
287 (setq subtree
(bw-find-tree-sub-1 child win get-parent
))))
288 (if (integerp subtree
)
296 ;;; Window or object edges
300 (if (windowp obj
) (nth 0 (window-edges obj
)) (cdr (assq 'l obj
))))
303 (if (windowp obj
) (nth 1 (window-edges obj
)) (cdr (assq 't obj
))))
306 (if (windowp obj
) (nth 2 (window-edges obj
)) (cdr (assq 'r obj
))))
308 "Bottom edge of OBJ."
309 (if (windowp obj
) (nth 3 (window-edges obj
)) (cdr (assq 'b obj
))))
314 "Return window split tree direction if OBJ.
315 If OBJ is a window return 'both. If it is a window split tree
316 then return its direction."
321 (let ((dir (cdr (assq 'dir obj
))))
322 (unless (memq dir
'(hor ver both
))
323 (error "Can't find dir in %s" obj
))
326 (defun bw-eqdir (obj1 obj2
)
327 "Return t if window split tree directions are equal.
328 OBJ1 and OBJ2 should be either windows or window split trees in
329 our format. The directions returned by `bw-dir' are compared and
330 t is returned if they are `eq' or one of them is 'both."
331 (let ((dir1 (bw-dir obj1
))
332 (dir2 (bw-dir obj2
)))
337 ;;; Building split tree
339 (defun bw-refresh-edges (obj)
340 "Refresh the edge information of OBJ and return OBJ."
341 (unless (windowp obj
)
342 (let ((childs (cdr (assq 'childs obj
)))
348 (when (> ol
(bw-l o
)) (setq ol
(bw-l o
)))
349 (when (> ot
(bw-t o
)) (setq ot
(bw-t o
)))
350 (when (< or
(bw-r o
)) (setq or
(bw-r o
)))
351 (when (< ob
(bw-b o
)) (setq ob
(bw-b o
))))
352 (setq obj
(delq 'l obj
))
353 (setq obj
(delq 't obj
))
354 (setq obj
(delq 'r obj
))
355 (setq obj
(delq 'b obj
))
356 (add-to-list 'obj
(cons 'l ol
))
357 (add-to-list 'obj
(cons 't ot
))
358 (add-to-list 'obj
(cons 'r or
))
359 (add-to-list 'obj
(cons 'b ob
))
365 (defun balance-windows (&optional window-or-frame
)
366 "Make windows the same heights or widths in window split subtrees.
368 When called non-interactively WINDOW-OR-FRAME may be either a
369 window or a frame. It then balances the windows on the implied
370 frame. If the parameter is a window only the corresponding window
371 subtree is balanced."
374 (wt (bw-get-tree window-or-frame
))
379 (windows (window-list nil
0)))
381 (while (not (member last-sizes tried-sizes
))
382 (when last-sizes
(setq tried-sizes
(cons last-sizes tried-sizes
)))
383 (setq last-sizes
(mapcar (lambda (w)
386 (when (eq 'hor
(bw-dir wt
))
387 (setq w
(- (bw-r wt
) (bw-l wt
))))
388 (when (eq 'ver
(bw-dir wt
))
389 (setq h
(- (bw-b wt
) (bw-t wt
))))
390 (bw-balance-sub wt w h
)))))
392 (defun bw-adjust-window (window delta horizontal
)
393 "Wrapper around `adjust-window-trailing-edge' with error checking.
394 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
395 ;; `adjust-window-trailing-edge' may fail if delta is too large.
396 (while (>= (abs delta
) 1)
399 (adjust-window-trailing-edge window delta horizontal
)
402 ;;(message "adjust: %s" (error-message-string err))
403 (setq delta
(/ delta
2))))))
405 (defun bw-balance-sub (wt w h
)
406 (setq wt
(bw-refresh-edges wt
))
407 (unless w
(setq w
(- (bw-r wt
) (bw-l wt
))))
408 (unless h
(setq h
(- (bw-b wt
) (bw-t wt
))))
412 (let ((dw (- w
(- (bw-r wt
) (bw-l wt
)))))
414 (bw-adjust-window wt dw t
))))
416 (let ((dh (- h
(- (bw-b wt
) (bw-t wt
)))))
418 (bw-adjust-window wt dh nil
)))))
419 (let* ((childs (cdr (assq 'childs wt
)))
420 (cw (when w
(/ w
(if (bw-eqdir 'hor wt
) (length childs
) 1))))
421 (ch (when h
(/ h
(if (bw-eqdir 'ver wt
) (length childs
) 1)))))
423 (bw-balance-sub c cw ch
)))))
425 ;;; A different solution to balance-windows
427 (defun window-fixed-size-p (&optional window direction
)
428 "Non-nil if WINDOW cannot be resized in DIRECTION.
429 DIRECTION can be nil (i.e. any), `height' or `width'."
430 (with-current-buffer (window-buffer window
)
431 (let ((fixed (and (boundp 'window-size-fixed
) window-size-fixed
)))
434 (member (cons direction window-size-fixed
)
435 '((height . width
) (width . height
)))))))))
437 (defvar window-area-factor
1
438 "Factor by which the window area should be over-estimated.
439 This is used by `balance-windows-area'.
440 Changing this globally has no effect.")
442 (defun balance-windows-area ()
443 "Make all visible windows the same area (approximately).
444 See also `window-area-factor' to change the relative size of specific buffers."
446 (let* ((unchanged 0) (carry 0) (round 0)
447 ;; Remove fixed-size windows.
448 (wins (delq nil
(mapcar (lambda (win)
449 (if (not (window-fixed-size-p win
)) win
))
450 (window-list nil
'nomini
))))
453 ;; Resizing a window changes the size of surrounding windows in complex
454 ;; ways, so it's difficult to balance them all. The introduction of
455 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
456 ;; very difficult to do. `balance-window' above takes an off-line
457 ;; approach: get the whole window tree, then balance it, then try to
458 ;; adjust the windows so they fit the result.
459 ;; Here, instead, we take a "local optimization" approach, where we just
460 ;; go through all the windows several times until nothing needs to be
461 ;; changed. The main problem with this approach is that it's difficult
462 ;; to make sure it terminates, so we use some heuristic to try and break
463 ;; off infinite loops.
464 ;; After a round without any change, we allow a second, to give a chance
465 ;; to the carry to propagate a minor imbalance from the end back to
467 (while (< unchanged
2)
468 ;; (message "New round")
469 (setq unchanged
(1+ unchanged
) round
(1+ round
))
472 (while (progn (setq next
(next-window next
))
473 (window-fixed-size-p next
)))
474 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
476 (< (car (window-edges win
)) (car (window-edges next
))))
477 (areadiff (/ (- (* (window-height next
) (window-width next
)
478 (buffer-local-value 'window-area-factor
479 (window-buffer next
)))
480 (* (window-height win
) (window-width win
)
481 (buffer-local-value 'window-area-factor
482 (window-buffer win
))))
483 (max (buffer-local-value 'window-area-factor
485 (buffer-local-value 'window-area-factor
486 (window-buffer next
)))))
488 (+ (window-height win
) (window-height next
))
489 (+ (window-width win
) (window-width next
))))
490 (diff (/ areadiff edgesize
)))
492 ;; Maybe diff is actually closer to 1 than to 0.
493 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
494 (when (and (zerop diff
) (not (zerop areadiff
)))
495 (setq diff
(/ (+ areadiff carry
) edgesize
))
496 ;; Change things smoothly.
497 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
499 ;; Make sure negligible differences don't accumulate to
500 ;; become significant.
501 (setq carry
(+ carry areadiff
))
502 (bw-adjust-window win diff horiz
)
504 (let ((change (cons win
(window-edges win
))))
505 ;; If the same change has been seen already for this window,
506 ;; we're most likely in an endless loop, so don't count it as
508 (unless (member change changelog
)
509 (push change changelog
)
510 (setq unchanged
0 carry
0)))))))
511 ;; We've now basically balanced all the windows.
512 ;; But there may be some minor off-by-one imbalance left over,
513 ;; so let's do some fine tuning.
514 ;; (bw-finetune wins)
515 ;; (message "Done in %d rounds" round)
518 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
520 ;; I think this should be the default; I think people will prefer it--rms.
521 (defcustom split-window-keep-point t
522 "*If non-nil, \\[split-window-vertically] keeps the original point \
524 This is often more convenient for editing.
525 If nil, adjust point in each of the two windows to minimize redisplay.
526 This is convenient on slow terminals, but point can move strangely.
528 This option applies only to `split-window-vertically' and
529 functions that call it. `split-window' always keeps the original
530 point in both children."
534 (defun split-window-vertically (&optional arg
)
535 "Split current window into two windows, one above the other.
536 The uppermost window gets ARG lines and the other gets the rest.
537 Negative ARG means select the size of the lowermost window instead.
538 With no argument, split equally or close to it.
539 Both windows display the same buffer now current.
541 If the variable `split-window-keep-point' is non-nil, both new windows
542 will get the same value of point as the current window. This is often
543 more convenient for editing. The upper window is the selected window.
545 Otherwise, we choose window starts so as to minimize the amount of
546 redisplay; this is convenient on slow terminals. The new selected
547 window is the one that the current value of point appears in. The
548 value of point can change if the text around point is hidden by the
551 Regardless of the value of `split-window-keep-point', the upper
552 window is the original one and the return value is the new, lower
555 (let ((old-w (selected-window))
557 (size (and arg
(prefix-numeric-value arg
)))
560 (and size
(< size
0) (setq size
(+ (window-height) size
)))
561 (setq new-w
(split-window nil size
))
562 (or split-window-keep-point
565 (set-buffer (window-buffer))
566 (goto-char (window-start))
567 (setq moved
(vertical-motion (window-height)))
568 (set-window-start new-w
(point))
569 (if (> (point) (window-point new-w
))
570 (set-window-point new-w
(point)))
571 (and (= moved
(window-height))
573 (setq window-full-p t
)
574 (vertical-motion -
1)))
575 (setq bottom
(point)))
578 (set-window-point old-w
(1- bottom
)))
580 (<= (window-start new-w
) old-point
)
582 (set-window-point new-w old-point
)
583 (select-window new-w
)))))
584 (split-window-save-restore-data new-w old-w
)))
586 ;; This is to avoid compiler warnings.
587 (defvar view-return-to-alist
)
589 (defun split-window-save-restore-data (new-w old-w
)
590 (with-current-buffer (window-buffer)
592 (let ((old-info (assq old-w view-return-to-alist
)))
594 (push (cons new-w
(cons (car (cdr old-info
)) t
))
595 view-return-to-alist
))))
598 (defun split-window-horizontally (&optional arg
)
599 "Split current window into two windows side by side.
600 This window becomes the leftmost of the two, and gets ARG columns.
601 Negative ARG means select the size of the rightmost window instead.
602 The argument includes the width of the window's scroll bar; if there
603 are no scroll bars, it includes the width of the divider column
604 to the window's right, if any. No ARG means split equally.
606 The original, leftmost window remains selected.
607 The return value is the new, rightmost window."
609 (let ((old-w (selected-window))
610 (size (and arg
(prefix-numeric-value arg
))))
612 (setq size
(+ (window-width) size
)))
613 (split-window-save-restore-data (split-window nil size t
) old-w
)))
615 (defun split-window-preferred-horizontally (window)
616 "Split WINDOW horizontally or select an appropriate existing window.
617 It is called by `display-buffer' to split windows horizontally
618 when the option `split-window-preferred-function' is set to \"horizontally\".
619 This function tries to match the implementation of vertical splitting
620 in `display-buffer' as close as possible but with the logic of
621 horizontal splitting. It returns a new window or an appropriate
622 existing window if splitting is not eligible."
624 ;; If the largest window is wide enough, eligible for splitting,
625 ;; and the only window, split it horizontally.
627 (not (frame-parameter (window-frame window
) 'unsplittable
))
628 (one-window-p (window-frame window
))
629 (>= (window-width window
) (* 2 window-min-width
)))
630 (split-window window nil t
)
631 ;; Otherwise, if the LRU window is wide enough, eligible for
632 ;; splitting and selected or the only window, split it horizontally.
633 (setq window
(get-lru-window nil t
))
635 (not (frame-parameter (window-frame window
) 'unsplittable
))
636 (or (eq window
(selected-window))
637 (one-window-p (window-frame window
)))
638 (>= (window-width window
) (* 2 window-min-width
)))
639 (split-window window nil t
)
640 ;; Otherwise, if get-lru-window returns nil, try other approaches.
642 (get-lru-window nil nil
)
643 ;; Try visible frames first.
644 (get-buffer-window (current-buffer) 'visible
)
645 (get-largest-window 'visible
)
646 ;; If that didn't work, try iconified frames.
647 (get-buffer-window (current-buffer) 0)
648 (get-largest-window 0)
649 ;; As a last resort, make a new frame.
650 (frame-selected-window (funcall pop-up-frame-function
))))))
653 (defun set-window-text-height (window height
)
654 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
655 This doesn't include the mode-line (or header-line if any) or any
656 partial-height lines in the text display area.
658 If WINDOW is nil, the selected window is used.
660 Note that the current implementation of this function cannot always set
661 the height exactly, but attempts to be conservative, by allocating more
662 lines than are actually needed in the case where some error may be present."
663 (let ((delta (- height
(window-text-height window
))))
664 (unless (zerop delta
)
665 ;; Setting window-min-height to a value like 1 can lead to very
666 ;; bizarre displays because it also allows Emacs to make *other*
667 ;; windows 1-line tall, which means that there's no more space for
669 (let ((window-min-height (min 2 height
))) ;One text line plus a modeline.
670 (if (and window
(not (eq window
(selected-window))))
671 (save-selected-window
672 (select-window window
)
673 (enlarge-window delta
))
674 (enlarge-window delta
))))))
677 (defun enlarge-window-horizontally (arg)
678 "Make current window ARG columns wider."
680 (enlarge-window arg t
))
682 (defun shrink-window-horizontally (arg)
683 "Make current window ARG columns narrower."
685 (shrink-window arg t
))
687 (defun window-buffer-height (window)
688 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
689 (with-current-buffer (window-buffer window
)
691 (count-screen-lines (point-min) (point-max)
692 ;; If buffer ends with a newline, ignore it when
693 ;; counting height unless point is after it.
697 (defun count-screen-lines (&optional beg end count-final-newline window
)
698 "Return the number of screen lines in the region.
699 The number of screen lines may be different from the number of actual lines,
700 due to line breaking, display table, etc.
702 Optional arguments BEG and END default to `point-min' and `point-max'
705 If region ends with a newline, ignore it unless optional third argument
706 COUNT-FINAL-NEWLINE is non-nil.
708 The optional fourth argument WINDOW specifies the window used for obtaining
709 parameters such as width, horizontal scrolling, and so on. The default is
710 to use the selected window's parameters.
712 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
713 regardless of which buffer is displayed in WINDOW. This makes possible to use
714 `count-screen-lines' in any buffer, whether or not it is currently displayed
717 (setq beg
(point-min)))
719 (setq end
(point-max)))
725 (narrow-to-region (min beg end
)
726 (if (and (not count-final-newline
)
727 (= ?
\n (char-before (max beg end
))))
730 (goto-char (point-min))
731 (1+ (vertical-motion (buffer-size) window
))))))
733 (defun fit-window-to-buffer (&optional window max-height min-height
)
734 "Make WINDOW the right height to display its contents exactly.
735 If WINDOW is omitted or nil, it defaults to the selected window.
736 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
737 the window is allowed to be, defaulting to the frame height.
738 If the optional argument MIN-HEIGHT is supplied, it is the minimum
739 height the window is allowed to be, defaulting to `window-min-height'.
741 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
746 (setq window
(selected-window)))
747 (when (null max-height
)
748 (setq max-height
(frame-height (window-frame window
))))
751 ;; Buffer that is displayed in WINDOW
752 (window-buffer window
))
754 ;; The current height of WINDOW
755 (window-height window
))
757 ;; The height necessary to show the buffer displayed by WINDOW
758 ;; (`count-screen-lines' always works on the current buffer).
759 (with-current-buffer buf
760 (+ (count-screen-lines)
761 ;; If the buffer is empty, (count-screen-lines) is
762 ;; zero. But, even in that case, we need one text line
764 (if (= (point-min) (point-max))
766 ;; For non-minibuffers, count the mode-line, if any
767 (if (and (not (window-minibuffer-p window
))
770 ;; Count the header-line, if any
771 (if header-line-format
1 0))))
773 ;; Calculate how much the window height has to change to show
774 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
775 (- (max (min desired-height max-height
)
776 (or min-height window-min-height
))
779 ;; Don't try to redisplay with the cursor at the end
780 ;; on its own line--that would force a scroll and spoil things.
781 (when (with-current-buffer buf
782 (and (eobp) (bolp) (not (bobp))))
783 (set-window-point window
(1- (window-point window
))))
785 (save-selected-window
786 (select-window window
)
788 ;; Adjust WINDOW to the nominally correct size (which may actually
789 ;; be slightly off because of variable height text, etc).
790 (unless (zerop delta
)
791 (enlarge-window delta
))
793 ;; Check if the last line is surely fully visible. If not,
794 ;; enlarge the window.
795 (let ((end (with-current-buffer buf
797 (goto-char (point-max))
798 (when (and (bolp) (not (bobp)))
799 ;; Don't include final newline
802 ;; If line-wrapping is turned off, test the
803 ;; beginning of the last line for visibility
804 ;; instead of the end, as the end of the line
805 ;; could be invisible by virtue of extending past
806 ;; the edge of the window.
809 (set-window-vscroll window
0)
810 (while (and (< desired-height max-height
)
811 (= desired-height
(window-height window
))
812 (not (pos-visible-in-window-p end window
)))
814 (setq desired-height
(1+ desired-height
)))))))
816 (defun shrink-window-if-larger-than-buffer (&optional window
)
817 "Shrink the WINDOW to be as small as possible to display its contents.
818 If WINDOW is omitted or nil, it defaults to the selected window.
819 Do not shrink to less than `window-min-height' lines.
820 Do nothing if the buffer contains more lines than the present window height,
821 or if some of the window's contents are scrolled out of view,
822 or if shrinking this window would also shrink another window,
823 or if the window is the only window of its frame."
826 (setq window
(selected-window)))
827 (let* ((frame (window-frame window
))
828 (mini (frame-parameter frame
'minibuffer
))
829 (edges (window-edges window
)))
830 (if (and (not (eq window
(frame-root-window frame
)))
831 (window-safely-shrinkable-p)
832 (pos-visible-in-window-p (point-min) window
)
833 (not (eq mini
'only
))
835 (let ((mini-window (minibuffer-window frame
)))
836 (or (null mini-window
)
837 (not (eq frame
(window-frame mini-window
)))
839 (nth 1 (window-edges mini-window
)))
841 (frame-parameter frame
'menu-bar-lines
))))))
842 (fit-window-to-buffer window
(window-height window
)))))
844 (defun kill-buffer-and-window ()
845 "Kill the current buffer and delete the selected window."
847 (let ((window-to-delete (selected-window))
848 (buffer-to-kill (current-buffer))
849 (delete-window-hook (lambda ()
855 (add-hook 'kill-buffer-hook delete-window-hook t t
)
856 (if (kill-buffer (current-buffer))
857 ;; If `delete-window' failed before, we rerun it to regenerate
858 ;; the error so it can be seen in the echo area.
859 (when (eq (selected-window) window-to-delete
)
861 ;; If the buffer is not dead for some reason (probably because
862 ;; of a `quit' signal), remove the hook again.
864 (with-current-buffer buffer-to-kill
865 (remove-hook 'kill-buffer-hook delete-window-hook t
))
868 (defun quit-window (&optional kill window
)
869 "Quit the current buffer. Bury it, and maybe delete the selected frame.
870 \(The frame is deleted if it contains a dedicated window for the buffer.)
871 With a prefix argument, kill the buffer instead.
873 Noninteractively, if KILL is non-nil, then kill the current buffer,
876 If WINDOW is non-nil, it specifies a window; we delete that window,
877 and the buffer that is killed or buried is the one in that window."
879 (let ((buffer (window-buffer window
))
880 (frame (window-frame (or window
(selected-window))))
882 (save-selected-window
884 (select-window window
))
888 (save-selected-window
890 (select-window window
))
891 (or (window-minibuffer-p)
892 (window-dedicated-p (selected-window))
893 (switch-to-buffer (other-buffer))))
895 ;; Get rid of the frame, if it has just one dedicated window
896 ;; and other visible frames exist.
897 (and (or (window-minibuffer-p) (window-dedicated-p window
))
898 (delq frame
(visible-frame-list))
900 (if (and (eq default-minibuffer-frame frame
)
901 (= 1 (length (minibuffer-frame-list))))
904 (setq window-handled t
)))
906 ;; Deal with the buffer.
909 (bury-buffer buffer
))
911 ;; Maybe get rid of the window.
912 (and window
(not window-handled
) (not window-solitary
)
913 (delete-window window
))))
915 (defvar recenter-last-op nil
916 "Indicates the last recenter operation performed.
917 Possible values: `top', `middle', `bottom'.")
919 (defun recenter-top-bottom (&optional arg
)
920 "Move current line to window center, top, and bottom, successively.
921 With a prefix argument, this is the same as `recenter':
922 With numeric prefix ARG, move current line to window-line ARG.
923 With plain `C-u', move current line to window center.
925 Otherwise move current line to window center on first call, and to
926 top, middle, or bottom on successive calls.
928 The cycling order is: middle -> top -> bottom.
930 Top and bottom destinations are actually `scroll-conservatively' lines
931 from true window top and bottom."
934 (arg (recenter arg
)) ; Always respect ARG.
935 ((not (eq this-command last-command
))
936 ;; First time - save mode and recenter.
937 (setq recenter-last-op
'middle
)
939 (t ;; repeat: loop through various options.
940 (setq recenter-last-op
941 (cond ((eq recenter-last-op
'middle
)
942 (recenter scroll-conservatively
)
944 ((eq recenter-last-op
'top
)
945 (recenter (1- (- scroll-conservatively
)))
947 ((eq recenter-last-op
'bottom
)
951 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
953 (defvar mouse-autoselect-window-timer nil
954 "Timer used by delayed window autoselection.")
956 (defvar mouse-autoselect-window-position nil
957 "Last mouse position recorded by delayed window autoselection.")
959 (defvar mouse-autoselect-window-window nil
960 "Last window recorded by delayed window autoselection.")
962 (defvar mouse-autoselect-window-state nil
963 "When non-nil, special state of delayed window autoselection.
964 Possible values are `suspend' \(suspend autoselection after a menu or
965 scrollbar interaction\) and `select' \(the next invocation of
966 'handle-select-window' shall select the window immediately\).")
968 (defun mouse-autoselect-window-cancel (&optional force
)
969 "Cancel delayed window autoselection.
970 Optional argument FORCE means cancel unconditionally."
971 (unless (and (not force
)
972 ;; Don't cancel for select-window or select-frame events
973 ;; or when the user drags a scroll bar.
974 (or (memq this-command
975 '(handle-select-window handle-switch-frame
))
976 (and (eq this-command
'scroll-bar-toolkit-scroll
)
977 (memq (nth 4 (event-end last-input-event
))
978 '(handle end-scroll
)))))
979 (setq mouse-autoselect-window-state nil
)
980 (when (timerp mouse-autoselect-window-timer
)
981 (cancel-timer mouse-autoselect-window-timer
))
982 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
984 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
985 "Start delayed window autoselection.
986 MOUSE-POSITION is the last position where the mouse was seen as returned
987 by `mouse-position'. Optional argument WINDOW non-nil denotes the
988 window where the mouse was seen. Optional argument SUSPEND non-nil
989 means suspend autoselection."
990 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
991 (setq mouse-autoselect-window-position mouse-position
)
992 (when window
(setq mouse-autoselect-window-window window
))
993 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
994 ;; Install timer which runs `mouse-autoselect-window-select' after
995 ;; `mouse-autoselect-window' seconds.
996 (setq mouse-autoselect-window-timer
998 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
1000 (defun mouse-autoselect-window-select ()
1001 "Select window with delayed window autoselection.
1002 If the mouse position has stabilized in a non-selected window, select
1003 that window. The minibuffer window is selected only if the minibuffer is
1004 active. This function is run by `mouse-autoselect-window-timer'."
1006 (let* ((mouse-position (mouse-position))
1009 (window-at (cadr mouse-position
) (cddr mouse-position
)
1010 (car mouse-position
))
1013 ((or (menu-or-popup-active-p)
1015 (not (coordinates-in-window-p (cdr mouse-position
) window
))))
1016 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
1017 ;; of WINDOW, temporarily suspend delayed autoselection.
1018 (mouse-autoselect-window-start mouse-position nil t
))
1019 ((eq mouse-autoselect-window-state
'suspend
)
1020 ;; Delayed autoselection was temporarily suspended, reenable it.
1021 (mouse-autoselect-window-start mouse-position
))
1022 ((and window
(not (eq window
(selected-window)))
1023 (or (not (numberp mouse-autoselect-window
))
1024 (and (> mouse-autoselect-window
0)
1025 ;; If `mouse-autoselect-window' is positive, select
1026 ;; window if the window is the same as before.
1027 (eq window mouse-autoselect-window-window
))
1028 ;; Otherwise select window if the mouse is at the same
1029 ;; position as before. Observe that the first test after
1030 ;; starting autoselection usually fails since the value of
1031 ;; `mouse-autoselect-window-position' recorded there is the
1032 ;; position where the mouse has entered the new window and
1033 ;; not necessarily where the mouse has stopped moving.
1034 (equal mouse-position mouse-autoselect-window-position
))
1035 ;; The minibuffer is a candidate window if it's active.
1036 (or (not (window-minibuffer-p window
))
1037 (eq window
(active-minibuffer-window))))
1038 ;; Mouse position has stabilized in non-selected window: Cancel
1039 ;; delayed autoselection and try to select that window.
1040 (mouse-autoselect-window-cancel t
)
1041 ;; Select window where mouse appears unless the selected window is the
1042 ;; minibuffer. Use `unread-command-events' in order to execute pre-
1043 ;; and post-command hooks and trigger idle timers. To avoid delaying
1044 ;; autoselection again, set `mouse-autoselect-window-state'."
1045 (unless (window-minibuffer-p (selected-window))
1046 (setq mouse-autoselect-window-state
'select
)
1047 (setq unread-command-events
1048 (cons (list 'select-window
(list window
))
1049 unread-command-events
))))
1050 ((or (and window
(eq window
(selected-window)))
1051 (not (numberp mouse-autoselect-window
))
1052 (equal mouse-position mouse-autoselect-window-position
))
1053 ;; Mouse position has either stabilized in the selected window or at
1054 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
1055 (mouse-autoselect-window-cancel t
))
1057 ;; Mouse position has not stabilized yet, resume delayed
1059 (mouse-autoselect-window-start mouse-position window
))))
1062 (defun handle-select-window (event)
1063 "Handle select-window events."
1065 (let ((window (posn-window (event-start event
))))
1066 (unless (or (not (window-live-p window
))
1067 ;; Don't switch if we're currently in the minibuffer.
1068 ;; This tries to work around problems where the
1069 ;; minibuffer gets unselected unexpectedly, and where
1070 ;; you then have to move your mouse all the way down to
1071 ;; the minibuffer to select it.
1072 (window-minibuffer-p (selected-window))
1073 ;; Don't switch to minibuffer window unless it's active.
1074 (and (window-minibuffer-p window
)
1075 (not (minibuffer-window-active-p window
)))
1076 ;; Don't switch when autoselection shall be delayed.
1077 (and (numberp mouse-autoselect-window
)
1078 (not (zerop mouse-autoselect-window
))
1079 (not (eq mouse-autoselect-window-state
'select
))
1081 ;; Cancel any delayed autoselection.
1082 (mouse-autoselect-window-cancel t
)
1083 ;; Start delayed autoselection from current mouse position
1085 (mouse-autoselect-window-start (mouse-position) window
)
1086 ;; Executing a command cancels delayed autoselection.
1088 'pre-command-hook
'mouse-autoselect-window-cancel
))))
1089 (when mouse-autoselect-window
1090 ;; Reset state of delayed autoselection.
1091 (setq mouse-autoselect-window-state nil
)
1092 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
1093 (run-hooks 'mouse-leave-buffer-hook
))
1094 (select-window window
))))
1096 (defun delete-other-windows-vertically (&optional window
)
1097 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
1098 This may be a useful alternative binding for \\[delete-other-windows]
1099 if you often split windows horizontally."
1101 (let* ((window (or window
(selected-window)))
1102 (edges (window-edges window
))
1104 (while (not (eq (setq w
(next-window w
1)) window
))
1105 (let ((e (window-edges w
)))
1106 (when (and (= (car e
) (car edges
))
1107 (= (caddr e
) (caddr edges
)))
1109 (mapc 'delete-window delenda
)))
1111 (define-key ctl-x-map
"2" 'split-window-vertically
)
1112 (define-key ctl-x-map
"3" 'split-window-horizontally
)
1113 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
1114 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
1115 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
1116 (define-key ctl-x-map
"+" 'balance-windows
)
1117 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
1119 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
1120 ;;; window.el ends here