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 (eval-when-compile (require 'cl
))
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
)))
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
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
)))
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
)))
109 (when (or (eq vert t
) (eq hor t
))
110 (let ((fcsb (frame-current-scroll-bars
111 (window-frame (or window
(selected-window))))))
113 (setq vert
(car fcsb
)))
115 (setq hor
(cdr fcsb
)))))
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))
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)))
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
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
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."
187 (walk-windows #'(lambda (window)
188 (when (funcall predicate window
)
189 (throw 'found window
)))
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
)))))
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."
218 (walk-windows (lambda (w) (setq count
(+ count
1)))
222 (defun window-safely-shrinkable-p (&optional window
)
223 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
224 If WINDOW is nil or omitted, it defaults to the currently selected window."
225 (with-selected-window (or window
(selected-window))
226 (let ((edges (window-edges)))
227 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
228 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;;; `balance-windows' subroutines using `window-tree'
234 ;;; Translate from internal window tree format
236 (defun bw-get-tree (&optional window-or-frame
)
237 "Get a window split tree in our format.
239 WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil,
240 then the whole window split tree for `selected-frame' is returned.
241 If it is a frame, then this is used instead. If it is a window,
242 then the smallest tree containing that window is returned."
243 (when window-or-frame
244 (unless (or (framep window-or-frame
)
245 (windowp window-or-frame
))
246 (error "Not a frame or window: %s" window-or-frame
)))
247 (let ((subtree (bw-find-tree-sub window-or-frame
)))
249 (if (integerp subtree
)
251 (bw-get-tree-1 subtree
)))))
253 (defun bw-get-tree-1 (split)
256 (let ((dir (car split
))
257 (edges (car (cdr split
)))
258 (childs (cdr (cdr split
))))
260 (cons 'dir
(if dir
'ver
'hor
))
261 (cons 'b
(nth 3 edges
))
262 (cons 'r
(nth 2 edges
))
263 (cons 't
(nth 1 edges
))
264 (cons 'l
(nth 0 edges
))
265 (cons 'childs
(mapcar #'bw-get-tree-1 childs
))))))
267 (defun bw-find-tree-sub (window-or-frame &optional get-parent
)
268 (let* ((window (when (windowp window-or-frame
) window-or-frame
))
269 (frame (when (windowp window
) (window-frame window
)))
270 (wt (car (window-tree frame
))))
271 (when (< 1 (length (window-list frame
0)))
273 (bw-find-tree-sub-1 wt window get-parent
)
276 (defun bw-find-tree-sub-1 (tree win
&optional get-parent
)
277 (unless (windowp win
) (error "Not a window: %s" win
))
282 (let ((childs (cdr (cdr tree
)))
285 (while (and childs
(not subtree
))
286 (setq child
(car childs
))
287 (setq childs
(cdr childs
))
288 (when (and child
(listp child
))
289 (setq subtree
(bw-find-tree-sub-1 child win get-parent
))))
290 (if (integerp subtree
)
298 ;;; Window or object edges
302 (if (windowp obj
) (nth 0 (window-edges obj
)) (cdr (assq 'l obj
))))
305 (if (windowp obj
) (nth 1 (window-edges obj
)) (cdr (assq 't obj
))))
308 (if (windowp obj
) (nth 2 (window-edges obj
)) (cdr (assq 'r obj
))))
310 "Bottom edge of OBJ."
311 (if (windowp obj
) (nth 3 (window-edges obj
)) (cdr (assq 'b obj
))))
316 "Return window split tree direction if OBJ.
317 If OBJ is a window return 'both. If it is a window split tree
318 then return its direction."
323 (let ((dir (cdr (assq 'dir obj
))))
324 (unless (memq dir
'(hor ver both
))
325 (error "Can't find dir in %s" obj
))
328 (defun bw-eqdir (obj1 obj2
)
329 "Return t if window split tree directions are equal.
330 OBJ1 and OBJ2 should be either windows or window split trees in
331 our format. The directions returned by `bw-dir' are compared and
332 t is returned if they are `eq' or one of them is 'both."
333 (let ((dir1 (bw-dir obj1
))
334 (dir2 (bw-dir obj2
)))
339 ;;; Building split tree
341 (defun bw-refresh-edges (obj)
342 "Refresh the edge information of OBJ and return OBJ."
343 (unless (windowp obj
)
344 (let ((childs (cdr (assq 'childs obj
)))
350 (when (> ol
(bw-l o
)) (setq ol
(bw-l o
)))
351 (when (> ot
(bw-t o
)) (setq ot
(bw-t o
)))
352 (when (< or
(bw-r o
)) (setq or
(bw-r o
)))
353 (when (< ob
(bw-b o
)) (setq ob
(bw-b o
))))
354 (setq obj
(delq 'l obj
))
355 (setq obj
(delq 't obj
))
356 (setq obj
(delq 'r obj
))
357 (setq obj
(delq 'b obj
))
358 (add-to-list 'obj
(cons 'l ol
))
359 (add-to-list 'obj
(cons 't ot
))
360 (add-to-list 'obj
(cons 'r or
))
361 (add-to-list 'obj
(cons 'b ob
))
367 (defun balance-windows (&optional window-or-frame
)
368 "Make windows the same heights or widths in window split subtrees.
370 When called non-interactively WINDOW-OR-FRAME may be either a
371 window or a frame. It then balances the windows on the implied
372 frame. If the parameter is a window only the corresponding window
373 subtree is balanced."
376 (wt (bw-get-tree window-or-frame
))
381 (windows (window-list nil
0)))
383 (while (not (member last-sizes tried-sizes
))
384 (when last-sizes
(setq tried-sizes
(cons last-sizes tried-sizes
)))
385 (setq last-sizes
(mapcar (lambda (w)
388 (when (eq 'hor
(bw-dir wt
))
389 (setq w
(- (bw-r wt
) (bw-l wt
))))
390 (when (eq 'ver
(bw-dir wt
))
391 (setq h
(- (bw-b wt
) (bw-t wt
))))
392 (bw-balance-sub wt w h
)))))
394 (defun bw-adjust-window (window delta horizontal
)
395 "Wrapper around `adjust-window-trailing-edge' with error checking.
396 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
397 ;; `adjust-window-trailing-edge' may fail if delta is too large.
398 (while (>= (abs delta
) 1)
401 (adjust-window-trailing-edge window delta horizontal
)
404 ;;(message "adjust: %s" (error-message-string err))
405 (setq delta
(/ delta
2))))))
407 (defun bw-balance-sub (wt w h
)
408 (setq wt
(bw-refresh-edges wt
))
409 (unless w
(setq w
(- (bw-r wt
) (bw-l wt
))))
410 (unless h
(setq h
(- (bw-b wt
) (bw-t wt
))))
414 (let ((dw (- w
(- (bw-r wt
) (bw-l wt
)))))
416 (bw-adjust-window wt dw t
))))
418 (let ((dh (- h
(- (bw-b wt
) (bw-t wt
)))))
420 (bw-adjust-window wt dh nil
)))))
421 (let* ((childs (cdr (assq 'childs wt
)))
422 (cw (when w
(/ w
(if (bw-eqdir 'hor wt
) (length childs
) 1))))
423 (ch (when h
(/ h
(if (bw-eqdir 'ver wt
) (length childs
) 1)))))
425 (bw-balance-sub c cw ch
)))))
427 ;;; A different solution to balance-windows
429 (defun window-fixed-size-p (&optional window direction
)
430 "Non-nil if WINDOW cannot be resized in DIRECTION.
431 DIRECTION can be nil (i.e. any), `height' or `width'."
432 (with-current-buffer (window-buffer window
)
433 (let ((fixed (and (boundp 'window-size-fixed
) window-size-fixed
)))
436 (member (cons direction window-size-fixed
)
437 '((height . width
) (width . height
)))))))))
439 (defvar window-area-factor
1
440 "Factor by which the window area should be over-estimated.
441 This is used by `balance-windows-area'.
442 Changing this globally has no effect.")
444 (defun balance-windows-area ()
445 "Make all visible windows the same area (approximately).
446 See also `window-area-factor' to change the relative size of specific buffers."
448 (let* ((unchanged 0) (carry 0) (round 0)
449 ;; Remove fixed-size windows.
450 (wins (delq nil
(mapcar (lambda (win)
451 (if (not (window-fixed-size-p win
)) win
))
452 (window-list nil
'nomini
))))
455 ;; Resizing a window changes the size of surrounding windows in complex
456 ;; ways, so it's difficult to balance them all. The introduction of
457 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
458 ;; very difficult to do. `balance-window' above takes an off-line
459 ;; approach: get the whole window tree, then balance it, then try to
460 ;; adjust the windows so they fit the result.
461 ;; Here, instead, we take a "local optimization" approach, where we just
462 ;; go through all the windows several times until nothing needs to be
463 ;; changed. The main problem with this approach is that it's difficult
464 ;; to make sure it terminates, so we use some heuristic to try and break
465 ;; off infinite loops.
466 ;; After a round without any change, we allow a second, to give a chance
467 ;; to the carry to propagate a minor imbalance from the end back to
469 (while (< unchanged
2)
470 ;; (message "New round")
471 (setq unchanged
(1+ unchanged
) round
(1+ round
))
474 (while (progn (setq next
(next-window next
))
475 (window-fixed-size-p next
)))
476 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
478 (< (car (window-edges win
)) (car (window-edges next
))))
479 (areadiff (/ (- (* (window-height next
) (window-width next
)
480 (buffer-local-value 'window-area-factor
481 (window-buffer next
)))
482 (* (window-height win
) (window-width win
)
483 (buffer-local-value 'window-area-factor
484 (window-buffer win
))))
485 (max (buffer-local-value 'window-area-factor
487 (buffer-local-value 'window-area-factor
488 (window-buffer next
)))))
490 (+ (window-height win
) (window-height next
))
491 (+ (window-width win
) (window-width next
))))
492 (diff (/ areadiff edgesize
)))
494 ;; Maybe diff is actually closer to 1 than to 0.
495 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
496 (when (and (zerop diff
) (not (zerop areadiff
)))
497 (setq diff
(/ (+ areadiff carry
) edgesize
))
498 ;; Change things smoothly.
499 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
501 ;; Make sure negligible differences don't accumulate to
502 ;; become significant.
503 (setq carry
(+ carry areadiff
))
504 (bw-adjust-window win diff horiz
)
506 (let ((change (cons win
(window-edges win
))))
507 ;; If the same change has been seen already for this window,
508 ;; we're most likely in an endless loop, so don't count it as
510 (unless (member change changelog
)
511 (push change changelog
)
512 (setq unchanged
0 carry
0)))))))
513 ;; We've now basically balanced all the windows.
514 ;; But there may be some minor off-by-one imbalance left over,
515 ;; so let's do some fine tuning.
516 ;; (bw-finetune wins)
517 ;; (message "Done in %d rounds" round)
520 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
522 ;; I think this should be the default; I think people will prefer it--rms.
523 (defcustom split-window-keep-point t
524 "*If non-nil, \\[split-window-vertically] keeps the original point \
526 This is often more convenient for editing.
527 If nil, adjust point in each of the two windows to minimize redisplay.
528 This is convenient on slow terminals, but point can move strangely.
530 This option applies only to `split-window-vertically' and
531 functions that call it. `split-window' always keeps the original
532 point in both children."
536 (defun split-window-vertically (&optional arg
)
537 "Split current window into two windows, one above the other.
538 The uppermost window gets ARG lines and the other gets the rest.
539 Negative ARG means select the size of the lowermost window instead.
540 With no argument, split equally or close to it.
541 Both windows display the same buffer now current.
543 If the variable `split-window-keep-point' is non-nil, both new windows
544 will get the same value of point as the current window. This is often
545 more convenient for editing. The upper window is the selected window.
547 Otherwise, we choose window starts so as to minimize the amount of
548 redisplay; this is convenient on slow terminals. The new selected
549 window is the one that the current value of point appears in. The
550 value of point can change if the text around point is hidden by the
553 Regardless of the value of `split-window-keep-point', the upper
554 window is the original one and the return value is the new, lower
557 (let ((old-w (selected-window))
559 (size (and arg
(prefix-numeric-value arg
)))
562 (and size
(< size
0) (setq size
(+ (window-height) size
)))
563 (setq new-w
(split-window nil size
))
564 (or split-window-keep-point
567 (set-buffer (window-buffer))
568 (goto-char (window-start))
569 (setq moved
(vertical-motion (window-height)))
570 (set-window-start new-w
(point))
571 (if (> (point) (window-point new-w
))
572 (set-window-point new-w
(point)))
573 (and (= moved
(window-height))
575 (setq window-full-p t
)
576 (vertical-motion -
1)))
577 (setq bottom
(point)))
580 (set-window-point old-w
(1- bottom
)))
582 (<= (window-start new-w
) old-point
)
584 (set-window-point new-w old-point
)
585 (select-window new-w
)))))
586 (split-window-save-restore-data new-w old-w
)))
588 ;; This is to avoid compiler warnings.
589 (defvar view-return-to-alist
)
591 (defun split-window-save-restore-data (new-w old-w
)
592 (with-current-buffer (window-buffer)
594 (let ((old-info (assq old-w view-return-to-alist
)))
596 (push (cons new-w
(cons (car (cdr old-info
)) t
))
597 view-return-to-alist
))))
600 (defun split-window-horizontally (&optional arg
)
601 "Split current window into two windows side by side.
602 This window becomes the leftmost of the two, and gets ARG columns.
603 Negative ARG means select the size of the rightmost window instead.
604 The argument includes the width of the window's scroll bar; if there
605 are no scroll bars, it includes the width of the divider column
606 to the window's right, if any. No ARG means split equally.
608 The original, leftmost window remains selected.
609 The return value is the new, rightmost window."
611 (let ((old-w (selected-window))
612 (size (and arg
(prefix-numeric-value arg
))))
614 (setq size
(+ (window-width) size
)))
615 (split-window-save-restore-data (split-window nil size t
) old-w
)))
617 (defun split-window-preferred-horizontally (window)
618 "Split WINDOW horizontally or select an appropriate existing window.
619 It is called by `display-buffer' to split windows horizontally
620 when the option `split-window-preferred-function' is set to \"horizontally\".
621 This function tries to match the implementation of vertical splitting
622 in `display-buffer' as close as possible but with the logic of
623 horizontal splitting. It returns a new window or an appropriate
624 existing window if splitting is not eligible."
626 ;; If the largest window is wide enough, eligible for splitting,
627 ;; and the only window, split it horizontally.
629 (not (frame-parameter (window-frame window
) 'unsplittable
))
630 (one-window-p (window-frame window
))
631 (>= (window-width window
) (* 2 window-min-width
)))
632 (split-window window nil t
)
633 ;; Otherwise, if the LRU window is wide enough, eligible for
634 ;; splitting and selected or the only window, split it horizontally.
635 (setq window
(get-lru-window nil t
))
637 (not (frame-parameter (window-frame window
) 'unsplittable
))
638 (or (eq window
(selected-window))
639 (one-window-p (window-frame window
)))
640 (>= (window-width window
) (* 2 window-min-width
)))
641 (split-window window nil t
)
642 ;; Otherwise, if get-lru-window returns nil, try other approaches.
644 (get-lru-window nil nil
)
645 ;; Try visible frames first.
646 (get-buffer-window (current-buffer) 'visible
)
647 (get-largest-window 'visible
)
648 ;; If that didn't work, try iconified frames.
649 (get-buffer-window (current-buffer) 0)
650 (get-largest-window 0)
651 ;; As a last resort, make a new frame.
652 (frame-selected-window (funcall pop-up-frame-function
))))))
655 (defun set-window-text-height (window height
)
656 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
657 This doesn't include the mode-line (or header-line if any) or any
658 partial-height lines in the text display area.
660 If WINDOW is nil, the selected window is used.
662 Note that the current implementation of this function cannot always set
663 the height exactly, but attempts to be conservative, by allocating more
664 lines than are actually needed in the case where some error may be present."
665 (let ((delta (- height
(window-text-height window
))))
666 (unless (zerop delta
)
667 ;; Setting window-min-height to a value like 1 can lead to very
668 ;; bizarre displays because it also allows Emacs to make *other*
669 ;; windows 1-line tall, which means that there's no more space for
671 (let ((window-min-height (min 2 height
))) ;One text line plus a modeline.
672 (if (and window
(not (eq window
(selected-window))))
673 (save-selected-window
674 (select-window window
)
675 (enlarge-window delta
))
676 (enlarge-window delta
))))))
679 (defun enlarge-window-horizontally (arg)
680 "Make current window ARG columns wider."
682 (enlarge-window arg t
))
684 (defun shrink-window-horizontally (arg)
685 "Make current window ARG columns narrower."
687 (shrink-window arg t
))
689 (defun window-buffer-height (window)
690 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
691 (with-current-buffer (window-buffer window
)
693 (count-screen-lines (point-min) (point-max)
694 ;; If buffer ends with a newline, ignore it when
695 ;; counting height unless point is after it.
699 (defun count-screen-lines (&optional beg end count-final-newline window
)
700 "Return the number of screen lines in the region.
701 The number of screen lines may be different from the number of actual lines,
702 due to line breaking, display table, etc.
704 Optional arguments BEG and END default to `point-min' and `point-max'
707 If region ends with a newline, ignore it unless optional third argument
708 COUNT-FINAL-NEWLINE is non-nil.
710 The optional fourth argument WINDOW specifies the window used for obtaining
711 parameters such as width, horizontal scrolling, and so on. The default is
712 to use the selected window's parameters.
714 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
715 regardless of which buffer is displayed in WINDOW. This makes possible to use
716 `count-screen-lines' in any buffer, whether or not it is currently displayed
719 (setq beg
(point-min)))
721 (setq end
(point-max)))
727 (narrow-to-region (min beg end
)
728 (if (and (not count-final-newline
)
729 (= ?
\n (char-before (max beg end
))))
732 (goto-char (point-min))
733 (1+ (vertical-motion (buffer-size) window
))))))
735 (defun fit-window-to-buffer (&optional window max-height min-height
)
736 "Make WINDOW the right height to display its contents exactly.
737 If WINDOW is omitted or nil, it defaults to the selected window.
738 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
739 the window is allowed to be, defaulting to the frame height.
740 If the optional argument MIN-HEIGHT is supplied, it is the minimum
741 height the window is allowed to be, defaulting to `window-min-height'.
743 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
748 (setq window
(selected-window)))
749 (when (null max-height
)
750 (setq max-height
(frame-height (window-frame window
))))
753 ;; Buffer that is displayed in WINDOW
754 (window-buffer window
))
756 ;; The current height of WINDOW
757 (window-height window
))
759 ;; The height necessary to show the buffer displayed by WINDOW
760 ;; (`count-screen-lines' always works on the current buffer).
761 (with-current-buffer buf
762 (+ (count-screen-lines)
763 ;; If the buffer is empty, (count-screen-lines) is
764 ;; zero. But, even in that case, we need one text line
766 (if (= (point-min) (point-max))
768 ;; For non-minibuffers, count the mode-line, if any
769 (if (and (not (window-minibuffer-p window
))
772 ;; Count the header-line, if any
773 (if header-line-format
1 0))))
775 ;; Calculate how much the window height has to change to show
776 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
777 (- (max (min desired-height max-height
)
778 (or min-height window-min-height
))
781 ;; Don't try to redisplay with the cursor at the end
782 ;; on its own line--that would force a scroll and spoil things.
783 (when (with-current-buffer buf
784 (and (eobp) (bolp) (not (bobp))))
785 (set-window-point window
(1- (window-point window
))))
787 (save-selected-window
788 (select-window window
)
790 ;; Adjust WINDOW to the nominally correct size (which may actually
791 ;; be slightly off because of variable height text, etc).
792 (unless (zerop delta
)
793 (enlarge-window delta
))
795 ;; Check if the last line is surely fully visible. If not,
796 ;; enlarge the window.
797 (let ((end (with-current-buffer buf
799 (goto-char (point-max))
800 (when (and (bolp) (not (bobp)))
801 ;; Don't include final newline
804 ;; If line-wrapping is turned off, test the
805 ;; beginning of the last line for visibility
806 ;; instead of the end, as the end of the line
807 ;; could be invisible by virtue of extending past
808 ;; the edge of the window.
811 (set-window-vscroll window
0)
812 (while (and (< desired-height max-height
)
813 (= desired-height
(window-height window
))
814 (not (pos-visible-in-window-p end window
)))
816 (setq desired-height
(1+ desired-height
)))))))
818 (defun shrink-window-if-larger-than-buffer (&optional window
)
819 "Shrink the WINDOW to be as small as possible to display its contents.
820 If WINDOW is omitted or nil, it defaults to the selected window.
821 Do not shrink to less than `window-min-height' lines.
822 Do nothing if the buffer contains more lines than the present window height,
823 or if some of the window's contents are scrolled out of view,
824 or if shrinking this window would also shrink another window,
825 or if the window is the only window of its frame."
828 (setq window
(selected-window)))
829 (let* ((frame (window-frame window
))
830 (mini (frame-parameter frame
'minibuffer
))
831 (edges (window-edges window
)))
832 (if (and (not (eq window
(frame-root-window frame
)))
833 (window-safely-shrinkable-p)
834 (pos-visible-in-window-p (point-min) window
)
835 (not (eq mini
'only
))
837 (let ((mini-window (minibuffer-window frame
)))
838 (or (null mini-window
)
839 (not (eq frame
(window-frame mini-window
)))
841 (nth 1 (window-edges mini-window
)))
843 (frame-parameter frame
'menu-bar-lines
))))))
844 (fit-window-to-buffer window
(window-height window
)))))
846 (defun kill-buffer-and-window ()
847 "Kill the current buffer and delete the selected window."
849 (let ((window-to-delete (selected-window))
850 (buffer-to-kill (current-buffer))
851 (delete-window-hook (lambda ()
857 (add-hook 'kill-buffer-hook delete-window-hook t t
)
858 (if (kill-buffer (current-buffer))
859 ;; If `delete-window' failed before, we rerun it to regenerate
860 ;; the error so it can be seen in the echo area.
861 (when (eq (selected-window) window-to-delete
)
863 ;; If the buffer is not dead for some reason (probably because
864 ;; of a `quit' signal), remove the hook again.
866 (with-current-buffer buffer-to-kill
867 (remove-hook 'kill-buffer-hook delete-window-hook t
))
870 (defun quit-window (&optional kill window
)
871 "Quit the current buffer. Bury it, and maybe delete the selected frame.
872 \(The frame is deleted if it contains a dedicated window for the buffer.)
873 With a prefix argument, kill the buffer instead.
875 Noninteractively, if KILL is non-nil, then kill the current buffer,
878 If WINDOW is non-nil, it specifies a window; we delete that window,
879 and the buffer that is killed or buried is the one in that window."
881 (let ((buffer (window-buffer window
))
882 (frame (window-frame (or window
(selected-window))))
884 (save-selected-window
886 (select-window window
))
890 (save-selected-window
892 (select-window window
))
893 (or (window-minibuffer-p)
894 (window-dedicated-p (selected-window))
895 (switch-to-buffer (other-buffer))))
897 ;; Get rid of the frame, if it has just one dedicated window
898 ;; and other visible frames exist.
899 (and (or (window-minibuffer-p) (window-dedicated-p window
))
900 (delq frame
(visible-frame-list))
902 (if (and (eq default-minibuffer-frame frame
)
903 (= 1 (length (minibuffer-frame-list))))
906 (setq window-handled t
)))
908 ;; Deal with the buffer.
911 (bury-buffer buffer
))
913 ;; Maybe get rid of the window.
914 (and window
(not window-handled
) (not window-solitary
)
915 (delete-window window
))))
917 (defvar recenter-last-op nil
918 "Indicates the last recenter operation performed.
919 Possible values: `top', `middle', `bottom'.")
921 (defun recenter-top-bottom (&optional arg
)
922 "Move current line to window center, top, and bottom, successively.
923 With a prefix argument, this is the same as `recenter':
924 With numeric prefix ARG, move current line to window-line ARG.
925 With plain `C-u', move current line to window center.
927 Otherwise move current line to window center on first call, and to
928 top, middle, or bottom on successive calls.
930 The cycling order is: middle -> top -> bottom.
932 Top and bottom destinations are actually `scroll-conservatively' lines
933 from true window top and bottom."
936 (arg (recenter arg
)) ; Always respect ARG.
937 ((not (eq this-command last-command
))
938 ;; First time - save mode and recenter.
939 (setq recenter-last-op
'middle
)
941 (t ;; repeat: loop through various options.
942 (setq recenter-last-op
943 (cond ((eq recenter-last-op
'middle
)
944 (recenter scroll-conservatively
)
946 ((eq recenter-last-op
'top
)
947 (recenter (1- (- scroll-conservatively
)))
949 ((eq recenter-last-op
'bottom
)
953 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
955 (defvar mouse-autoselect-window-timer nil
956 "Timer used by delayed window autoselection.")
958 (defvar mouse-autoselect-window-position nil
959 "Last mouse position recorded by delayed window autoselection.")
961 (defvar mouse-autoselect-window-window nil
962 "Last window recorded by delayed window autoselection.")
964 (defvar mouse-autoselect-window-state nil
965 "When non-nil, special state of delayed window autoselection.
966 Possible values are `suspend' \(suspend autoselection after a menu or
967 scrollbar interaction\) and `select' \(the next invocation of
968 'handle-select-window' shall select the window immediately\).")
970 (defun mouse-autoselect-window-cancel (&optional force
)
971 "Cancel delayed window autoselection.
972 Optional argument FORCE means cancel unconditionally."
973 (unless (and (not force
)
974 ;; Don't cancel for select-window or select-frame events
975 ;; or when the user drags a scroll bar.
976 (or (memq this-command
977 '(handle-select-window handle-switch-frame
))
978 (and (eq this-command
'scroll-bar-toolkit-scroll
)
979 (memq (nth 4 (event-end last-input-event
))
980 '(handle end-scroll
)))))
981 (setq mouse-autoselect-window-state nil
)
982 (when (timerp mouse-autoselect-window-timer
)
983 (cancel-timer mouse-autoselect-window-timer
))
984 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
986 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
987 "Start delayed window autoselection.
988 MOUSE-POSITION is the last position where the mouse was seen as returned
989 by `mouse-position'. Optional argument WINDOW non-nil denotes the
990 window where the mouse was seen. Optional argument SUSPEND non-nil
991 means suspend autoselection."
992 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
993 (setq mouse-autoselect-window-position mouse-position
)
994 (when window
(setq mouse-autoselect-window-window window
))
995 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
996 ;; Install timer which runs `mouse-autoselect-window-select' after
997 ;; `mouse-autoselect-window' seconds.
998 (setq mouse-autoselect-window-timer
1000 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
1002 (defun mouse-autoselect-window-select ()
1003 "Select window with delayed window autoselection.
1004 If the mouse position has stabilized in a non-selected window, select
1005 that window. The minibuffer window is selected only if the minibuffer is
1006 active. This function is run by `mouse-autoselect-window-timer'."
1008 (let* ((mouse-position (mouse-position))
1011 (window-at (cadr mouse-position
) (cddr mouse-position
)
1012 (car mouse-position
))
1015 ((or (menu-or-popup-active-p)
1017 (not (coordinates-in-window-p (cdr mouse-position
) window
))))
1018 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
1019 ;; of WINDOW, temporarily suspend delayed autoselection.
1020 (mouse-autoselect-window-start mouse-position nil t
))
1021 ((eq mouse-autoselect-window-state
'suspend
)
1022 ;; Delayed autoselection was temporarily suspended, reenable it.
1023 (mouse-autoselect-window-start mouse-position
))
1024 ((and window
(not (eq window
(selected-window)))
1025 (or (not (numberp mouse-autoselect-window
))
1026 (and (> mouse-autoselect-window
0)
1027 ;; If `mouse-autoselect-window' is positive, select
1028 ;; window if the window is the same as before.
1029 (eq window mouse-autoselect-window-window
))
1030 ;; Otherwise select window if the mouse is at the same
1031 ;; position as before. Observe that the first test after
1032 ;; starting autoselection usually fails since the value of
1033 ;; `mouse-autoselect-window-position' recorded there is the
1034 ;; position where the mouse has entered the new window and
1035 ;; not necessarily where the mouse has stopped moving.
1036 (equal mouse-position mouse-autoselect-window-position
))
1037 ;; The minibuffer is a candidate window if it's active.
1038 (or (not (window-minibuffer-p window
))
1039 (eq window
(active-minibuffer-window))))
1040 ;; Mouse position has stabilized in non-selected window: Cancel
1041 ;; delayed autoselection and try to select that window.
1042 (mouse-autoselect-window-cancel t
)
1043 ;; Select window where mouse appears unless the selected window is the
1044 ;; minibuffer. Use `unread-command-events' in order to execute pre-
1045 ;; and post-command hooks and trigger idle timers. To avoid delaying
1046 ;; autoselection again, set `mouse-autoselect-window-state'."
1047 (unless (window-minibuffer-p (selected-window))
1048 (setq mouse-autoselect-window-state
'select
)
1049 (setq unread-command-events
1050 (cons (list 'select-window
(list window
))
1051 unread-command-events
))))
1052 ((or (and window
(eq window
(selected-window)))
1053 (not (numberp mouse-autoselect-window
))
1054 (equal mouse-position mouse-autoselect-window-position
))
1055 ;; Mouse position has either stabilized in the selected window or at
1056 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
1057 (mouse-autoselect-window-cancel t
))
1059 ;; Mouse position has not stabilized yet, resume delayed
1061 (mouse-autoselect-window-start mouse-position window
))))
1064 (defun handle-select-window (event)
1065 "Handle select-window events."
1067 (let ((window (posn-window (event-start event
))))
1068 (unless (or (not (window-live-p window
))
1069 ;; Don't switch if we're currently in the minibuffer.
1070 ;; This tries to work around problems where the
1071 ;; minibuffer gets unselected unexpectedly, and where
1072 ;; you then have to move your mouse all the way down to
1073 ;; the minibuffer to select it.
1074 (window-minibuffer-p (selected-window))
1075 ;; Don't switch to minibuffer window unless it's active.
1076 (and (window-minibuffer-p window
)
1077 (not (minibuffer-window-active-p window
)))
1078 ;; Don't switch when autoselection shall be delayed.
1079 (and (numberp mouse-autoselect-window
)
1080 (not (zerop mouse-autoselect-window
))
1081 (not (eq mouse-autoselect-window-state
'select
))
1083 ;; Cancel any delayed autoselection.
1084 (mouse-autoselect-window-cancel t
)
1085 ;; Start delayed autoselection from current mouse position
1087 (mouse-autoselect-window-start (mouse-position) window
)
1088 ;; Executing a command cancels delayed autoselection.
1090 'pre-command-hook
'mouse-autoselect-window-cancel
))))
1091 (when mouse-autoselect-window
1092 ;; Reset state of delayed autoselection.
1093 (setq mouse-autoselect-window-state nil
)
1094 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
1095 (run-hooks 'mouse-leave-buffer-hook
))
1096 (select-window window
))))
1098 (defun delete-other-windows-vertically (&optional window
)
1099 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
1100 This may be a useful alternative binding for \\[delete-other-windows]
1101 if you often split windows horizontally."
1103 (let* ((window (or window
(selected-window)))
1104 (edges (window-edges window
))
1106 (while (not (eq (setq w
(next-window w
1)) window
))
1107 (let ((e (window-edges w
)))
1108 (when (and (= (car e
) (car edges
))
1109 (= (caddr e
) (caddr edges
)))
1111 (mapc 'delete-window delenda
)))
1113 (define-key ctl-x-map
"2" 'split-window-vertically
)
1114 (define-key ctl-x-map
"3" 'split-window-horizontally
)
1115 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
1116 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
1117 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
1118 (define-key ctl-x-map
"+" 'balance-windows
)
1119 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
1121 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
1122 ;;; window.el ends here