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, 2009 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 previously selected window.
43 The value returned is the value of the last form in BODY.
45 This macro saves and restores the selected window, as well as the
46 selected window in each frame. If the previously selected window
47 is no longer live, then whatever window is selected at the end of
48 BODY remains selected. If the previously selected window of some
49 frame is no longer live at the end of BODY, that frame's selected
52 This macro saves and restores the current buffer, since otherwise
53 its normal operation could make a different buffer current. The
54 order of recently selected windows and the buffer list ordering
55 are not altered by this macro (unless they are altered in BODY)."
56 `(let ((save-selected-window-window (selected-window))
57 ;; It is necessary to save all of these, because calling
58 ;; select-window changes frame-selected-window for whatever
59 ;; frame that window is in.
60 (save-selected-window-alist
61 (mapcar (lambda (frame) (cons frame
(frame-selected-window frame
)))
66 (dolist (elt save-selected-window-alist
)
67 (and (frame-live-p (car elt
))
68 (window-live-p (cdr elt
))
69 (set-frame-selected-window (car elt
) (cdr elt
) 'norecord
)))
70 (when (window-live-p save-selected-window-window
)
71 (select-window save-selected-window-window
'norecord
))))))
73 (defun window-body-height (&optional window
)
74 "Return number of lines in WINDOW available for actual buffer text.
75 WINDOW defaults to the selected window.
77 The return value does not include the mode line or the header
78 line, if any. If a line at the bottom of the window is only
79 partially visible, that line is included in the return value. If
80 you do not want to include a partially visible bottom line in the
81 return value, use `window-text-height' instead."
82 (or window
(setq window
(selected-window)))
83 (if (window-minibuffer-p window
)
84 (window-height window
)
85 (with-current-buffer (window-buffer window
)
86 (max 1 (- (window-height window
)
87 (if mode-line-format
1 0)
88 (if header-line-format
1 0))))))
90 (defun one-window-p (&optional nomini all-frames
)
91 "Return non-nil if the selected window is the only window.
92 Optional arg NOMINI non-nil means don't count the minibuffer
93 even if it is active. Otherwise, the minibuffer is counted
96 The optional arg ALL-FRAMES t means count windows on all frames.
97 If it is `visible', count windows on all visible frames.
98 ALL-FRAMES nil or omitted means count only the selected frame,
99 plus the minibuffer it uses (which may be on another frame).
100 ALL-FRAMES 0 means count all windows in all visible or iconified frames.
101 If ALL-FRAMES is anything else, count only the selected frame."
102 (let ((base-window (selected-window)))
103 (if (and nomini
(eq base-window
(minibuffer-window)))
104 (setq base-window
(next-window base-window
)))
106 (next-window base-window
(if nomini
'arg
) all-frames
))))
108 (defun window-current-scroll-bars (&optional window
)
109 "Return the current scroll bar settings for WINDOW.
110 WINDOW defaults to the selected window.
112 The return value is a cons cell (VERTICAL . HORIZONTAL) where
113 VERTICAL specifies the current location of the vertical scroll
114 bars (`left', `right', or nil), and HORIZONTAL specifies the
115 current location of the horizontal scroll bars (`top', `bottom',
118 Unlike `window-scroll-bars', this function reports the scroll bar
119 type actually used, once frame defaults and `scroll-bar-mode' are
121 (let ((vert (nth 2 (window-scroll-bars window
)))
123 (when (or (eq vert t
) (eq hor t
))
124 (let ((fcsb (frame-current-scroll-bars
125 (window-frame (or window
(selected-window))))))
127 (setq vert
(car fcsb
)))
129 (setq hor
(cdr fcsb
)))))
132 (defun walk-windows (proc &optional minibuf all-frames
)
133 "Cycle through all windows, calling PROC for each one.
134 PROC must specify a function with a window as its sole argument.
135 The optional arguments MINIBUF and ALL-FRAMES specify the set of
136 windows to include in the walk, see also `next-window'.
138 MINIBUF t means include the minibuffer window even if the
139 minibuffer is not active. MINIBUF nil or omitted means include
140 the minibuffer window only if the minibuffer is active. Any
141 other value means do not include the minibuffer window even if
142 the minibuffer is active.
144 Several frames may share a single minibuffer; if the minibuffer
145 is active, all windows on all frames that share that minibuffer
146 are included too. Therefore, if you are using a separate
147 minibuffer frame and the minibuffer is active and MINIBUF says it
148 counts, `walk-windows' includes the windows in the frame from
149 which you entered the minibuffer, as well as the minibuffer
152 ALL-FRAMES nil or omitted means cycle through all windows on
153 WINDOW's frame, plus the minibuffer window if specified by the
154 MINIBUF argument, see above. If the minibuffer counts, cycle
155 through all windows on all frames that share that minibuffer
157 ALL-FRAMES t means cycle through all windows on all existing
159 ALL-FRAMES `visible' means cycle through all windows on all
161 ALL-FRAMES 0 means cycle through all windows on all visible and
163 ALL-FRAMES a frame means cycle through all windows on that frame
165 Anything else means cycle through all windows on WINDOW's frame
168 This function changes neither the order of recently selected
169 windows nor the buffer list."
170 ;; If we start from the minibuffer window, don't fail to come
172 (when (window-minibuffer-p (selected-window))
174 ;; Make sure to not mess up the order of recently selected
175 ;; windows. Use `save-selected-window' and `select-window'
176 ;; with second argument non-nil for this purpose.
177 (save-selected-window
178 (when (framep all-frames
)
179 (select-window (frame-first-window all-frames
) 'norecord
))
180 (let* (walk-windows-already-seen
181 (walk-windows-current (selected-window)))
183 (setq walk-windows-current
184 (next-window walk-windows-current minibuf all-frames
))
185 (not (memq walk-windows-current walk-windows-already-seen
)))
186 (setq walk-windows-already-seen
187 (cons walk-windows-current walk-windows-already-seen
))
188 (funcall proc walk-windows-current
)))))
190 (defun get-window-with-predicate (predicate &optional minibuf
192 "Return a window satisfying PREDICATE.
193 More precisely, cycle through all windows using `walk-windows',
194 calling the function PREDICATE on each one of them with the
195 window as its sole argument. Return the first window for which
196 PREDICATE returns non-nil. If no window satisfies PREDICATE,
199 The optional arguments MINIBUF and ALL-FRAMES specify the set of
200 windows to include. See `walk-windows' for the meaning of these
203 (walk-windows #'(lambda (window)
204 (when (funcall predicate window
)
205 (throw 'found window
)))
209 (defalias 'some-window
'get-window-with-predicate
)
211 ;; This should probably be written in C (i.e., without using `walk-windows').
212 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
213 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
214 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
215 and defaults to the current buffer.
217 The optional arguments MINIBUF and ALL-FRAMES specify the set of
218 windows to consider. See `walk-windows' for the precise meaning
221 ((not buffer-or-name
) (current-buffer))
222 ((bufferp buffer-or-name
) buffer-or-name
)
223 (t (get-buffer buffer-or-name
))))
225 (walk-windows (function (lambda (window)
226 (if (eq (window-buffer window
) buffer
)
227 (setq windows
(cons window windows
)))))
231 (defun minibuffer-window-active-p (window)
232 "Return t if WINDOW is the currently active minibuffer window."
233 (eq window
(active-minibuffer-window)))
235 (defun count-windows (&optional minibuf
)
236 "Return the number of visible windows.
237 The optional argument MINIBUF specifies whether the minibuffer
238 window shall be counted. See `walk-windows' for the precise
239 meaning of this argument."
241 (walk-windows (lambda (w) (setq count
(+ count
1)))
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
246 ;;; `balance-windows' subroutines using `window-tree'
248 ;;; Translate from internal window tree format
250 (defun bw-get-tree (&optional window-or-frame
)
251 "Get a window split tree in our format.
253 WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil,
254 then the whole window split tree for `selected-frame' is returned.
255 If it is a frame, then this is used instead. If it is a window,
256 then the smallest tree containing that window is returned."
257 (when window-or-frame
258 (unless (or (framep window-or-frame
)
259 (windowp window-or-frame
))
260 (error "Not a frame or window: %s" window-or-frame
)))
261 (let ((subtree (bw-find-tree-sub window-or-frame
)))
263 (if (integerp subtree
)
265 (bw-get-tree-1 subtree
)))))
267 (defun bw-get-tree-1 (split)
270 (let ((dir (car split
))
271 (edges (car (cdr split
)))
272 (childs (cdr (cdr split
))))
274 (cons 'dir
(if dir
'ver
'hor
))
275 (cons 'b
(nth 3 edges
))
276 (cons 'r
(nth 2 edges
))
277 (cons 't
(nth 1 edges
))
278 (cons 'l
(nth 0 edges
))
279 (cons 'childs
(mapcar #'bw-get-tree-1 childs
))))))
281 (defun bw-find-tree-sub (window-or-frame &optional get-parent
)
282 (let* ((window (when (windowp window-or-frame
) window-or-frame
))
283 (frame (when (windowp window
) (window-frame window
)))
284 (wt (car (window-tree frame
))))
285 (when (< 1 (length (window-list frame
0)))
287 (bw-find-tree-sub-1 wt window get-parent
)
290 (defun bw-find-tree-sub-1 (tree win
&optional get-parent
)
291 (unless (windowp win
) (error "Not a window: %s" win
))
296 (let ((childs (cdr (cdr tree
)))
299 (while (and childs
(not subtree
))
300 (setq child
(car childs
))
301 (setq childs
(cdr childs
))
302 (when (and child
(listp child
))
303 (setq subtree
(bw-find-tree-sub-1 child win get-parent
))))
304 (if (integerp subtree
)
312 ;;; Window or object edges
316 (if (windowp obj
) (nth 0 (window-edges obj
)) (cdr (assq 'l obj
))))
319 (if (windowp obj
) (nth 1 (window-edges obj
)) (cdr (assq 't obj
))))
322 (if (windowp obj
) (nth 2 (window-edges obj
)) (cdr (assq 'r obj
))))
324 "Bottom edge of OBJ."
325 (if (windowp obj
) (nth 3 (window-edges obj
)) (cdr (assq 'b obj
))))
330 "Return window split tree direction if OBJ.
331 If OBJ is a window return 'both. If it is a window split tree
332 then return its direction."
337 (let ((dir (cdr (assq 'dir obj
))))
338 (unless (memq dir
'(hor ver both
))
339 (error "Can't find dir in %s" obj
))
342 (defun bw-eqdir (obj1 obj2
)
343 "Return t if window split tree directions are equal.
344 OBJ1 and OBJ2 should be either windows or window split trees in
345 our format. The directions returned by `bw-dir' are compared and
346 t is returned if they are `eq' or one of them is 'both."
347 (let ((dir1 (bw-dir obj1
))
348 (dir2 (bw-dir obj2
)))
353 ;;; Building split tree
355 (defun bw-refresh-edges (obj)
356 "Refresh the edge information of OBJ and return OBJ."
357 (unless (windowp obj
)
358 (let ((childs (cdr (assq 'childs obj
)))
364 (when (> ol
(bw-l o
)) (setq ol
(bw-l o
)))
365 (when (> ot
(bw-t o
)) (setq ot
(bw-t o
)))
366 (when (< or
(bw-r o
)) (setq or
(bw-r o
)))
367 (when (< ob
(bw-b o
)) (setq ob
(bw-b o
))))
368 (setq obj
(delq 'l obj
))
369 (setq obj
(delq 't obj
))
370 (setq obj
(delq 'r obj
))
371 (setq obj
(delq 'b obj
))
372 (add-to-list 'obj
(cons 'l ol
))
373 (add-to-list 'obj
(cons 't ot
))
374 (add-to-list 'obj
(cons 'r or
))
375 (add-to-list 'obj
(cons 'b ob
))
381 (defun balance-windows (&optional window-or-frame
)
382 "Make windows the same heights or widths in window split subtrees.
384 When called non-interactively WINDOW-OR-FRAME may be either a
385 window or a frame. It then balances the windows on the implied
386 frame. If the parameter is a window only the corresponding window
387 subtree is balanced."
390 (wt (bw-get-tree window-or-frame
))
395 (windows (window-list nil
0)))
397 (while (not (member last-sizes tried-sizes
))
398 (when last-sizes
(setq tried-sizes
(cons last-sizes tried-sizes
)))
399 (setq last-sizes
(mapcar (lambda (w)
402 (when (eq 'hor
(bw-dir wt
))
403 (setq w
(- (bw-r wt
) (bw-l wt
))))
404 (when (eq 'ver
(bw-dir wt
))
405 (setq h
(- (bw-b wt
) (bw-t wt
))))
406 (bw-balance-sub wt w h
)))))
408 (defun bw-adjust-window (window delta horizontal
)
409 "Wrapper around `adjust-window-trailing-edge' with error checking.
410 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
411 ;; `adjust-window-trailing-edge' may fail if delta is too large.
412 (while (>= (abs delta
) 1)
415 (adjust-window-trailing-edge window delta horizontal
)
418 ;;(message "adjust: %s" (error-message-string err))
419 (setq delta
(/ delta
2))))))
421 (defun bw-balance-sub (wt w h
)
422 (setq wt
(bw-refresh-edges wt
))
423 (unless w
(setq w
(- (bw-r wt
) (bw-l wt
))))
424 (unless h
(setq h
(- (bw-b wt
) (bw-t wt
))))
428 (let ((dw (- w
(- (bw-r wt
) (bw-l wt
)))))
430 (bw-adjust-window wt dw t
))))
432 (let ((dh (- h
(- (bw-b wt
) (bw-t wt
)))))
434 (bw-adjust-window wt dh nil
)))))
435 (let* ((childs (cdr (assq 'childs wt
)))
436 (cw (when w
(/ w
(if (bw-eqdir 'hor wt
) (length childs
) 1))))
437 (ch (when h
(/ h
(if (bw-eqdir 'ver wt
) (length childs
) 1)))))
439 (bw-balance-sub c cw ch
)))))
441 (defun window-fixed-size-p (&optional window direction
)
442 "Return t if WINDOW cannot be resized in DIRECTION.
443 WINDOW defaults to the selected window. DIRECTION can be
444 nil (i.e. any), `height' or `width'."
445 (with-current-buffer (window-buffer window
)
446 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
448 (member (cons direction window-size-fixed
)
449 '((height . width
) (width . height
))))))))
451 ;;; A different solution to balance-windows.
453 (defvar window-area-factor
1
454 "Factor by which the window area should be over-estimated.
455 This is used by `balance-windows-area'.
456 Changing this globally has no effect.")
457 (make-variable-buffer-local 'window-area-factor
)
459 (defun balance-windows-area ()
460 "Make all visible windows the same area (approximately).
461 See also `window-area-factor' to change the relative size of
464 (let* ((unchanged 0) (carry 0) (round 0)
465 ;; Remove fixed-size windows.
466 (wins (delq nil
(mapcar (lambda (win)
467 (if (not (window-fixed-size-p win
)) win
))
468 (window-list nil
'nomini
))))
471 ;; Resizing a window changes the size of surrounding windows in complex
472 ;; ways, so it's difficult to balance them all. The introduction of
473 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
474 ;; very difficult to do. `balance-window' above takes an off-line
475 ;; approach: get the whole window tree, then balance it, then try to
476 ;; adjust the windows so they fit the result.
477 ;; Here, instead, we take a "local optimization" approach, where we just
478 ;; go through all the windows several times until nothing needs to be
479 ;; changed. The main problem with this approach is that it's difficult
480 ;; to make sure it terminates, so we use some heuristic to try and break
481 ;; off infinite loops.
482 ;; After a round without any change, we allow a second, to give a chance
483 ;; to the carry to propagate a minor imbalance from the end back to
485 (while (< unchanged
2)
486 ;; (message "New round")
487 (setq unchanged
(1+ unchanged
) round
(1+ round
))
490 (while (progn (setq next
(next-window next
))
491 (window-fixed-size-p next
)))
492 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
494 (< (car (window-edges win
)) (car (window-edges next
))))
495 (areadiff (/ (- (* (window-height next
) (window-width next
)
496 (buffer-local-value 'window-area-factor
497 (window-buffer next
)))
498 (* (window-height win
) (window-width win
)
499 (buffer-local-value 'window-area-factor
500 (window-buffer win
))))
501 (max (buffer-local-value 'window-area-factor
503 (buffer-local-value 'window-area-factor
504 (window-buffer next
)))))
506 (+ (window-height win
) (window-height next
))
507 (+ (window-width win
) (window-width next
))))
508 (diff (/ areadiff edgesize
)))
510 ;; Maybe diff is actually closer to 1 than to 0.
511 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
512 (when (and (zerop diff
) (not (zerop areadiff
)))
513 (setq diff
(/ (+ areadiff carry
) edgesize
))
514 ;; Change things smoothly.
515 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
517 ;; Make sure negligible differences don't accumulate to
518 ;; become significant.
519 (setq carry
(+ carry areadiff
))
520 (bw-adjust-window win diff horiz
)
522 (let ((change (cons win
(window-edges win
))))
523 ;; If the same change has been seen already for this window,
524 ;; we're most likely in an endless loop, so don't count it as
526 (unless (member change changelog
)
527 (push change changelog
)
528 (setq unchanged
0 carry
0)))))))
529 ;; We've now basically balanced all the windows.
530 ;; But there may be some minor off-by-one imbalance left over,
531 ;; so let's do some fine tuning.
532 ;; (bw-finetune wins)
533 ;; (message "Done in %d rounds" round)
537 (defcustom display-buffer-function nil
538 "If non-nil, function to call to handle `display-buffer'.
539 It will receive two args, the buffer and a flag which if non-nil
540 means that the currently selected window is not acceptable. It
541 should choose or create a window, display the specified buffer in
542 it, and return the window.
544 Commands such as `switch-to-buffer-other-window' and
545 `find-file-other-window' work using this function."
548 (function :tag
"function"))
551 (defcustom special-display-buffer-names nil
552 "List of names of buffers that should be displayed specially.
553 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
554 its name is in this list, displays the buffer in a way specified
555 by `special-display-function'. `special-display-popup-frame'
556 \(the default for `special-display-function') usually displays
557 the buffer in a separate frame made with the parameters specified
558 by `special-display-frame-alist'. If `special-display-function'
559 has been set to some other function, that function is called with
560 the buffer as first, and nil as second argument.
562 Alternatively, an element of this list can be specified as
563 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
564 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
565 `special-display-popup-frame' will interpret such pairs as frame
566 parameters when it creates a special frame, overriding the
567 corresponding values from `special-display-frame-alist'.
569 As a special case, if FRAME-PARAMETERS contains (same-window . t)
570 `special-display-popup-frame' displays that buffer in the
571 selected window. If FRAME-PARAMETERS contains (same-frame . t),
572 it displays that buffer in a window on the selected frame.
574 If `special-display-function' specifies some other function than
575 `special-display-popup-frame', that function is called with the
576 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
579 Finally, an element of this list can be also specified as
580 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
581 `special-display-popup-frame' will call FUNCTION with the buffer
582 named BUFFER-NAME as first argument, and OTHER-ARGS as the
583 second. If `special-display-function' specifies some other
584 function, that function is called with the buffer named
585 BUFFER-NAME as first, and the element's cdr as second argument.
587 If this variable appears \"not to work\", because you added a
588 name to it but the corresponding buffer is displayed in the
589 selected window, look at the values of `same-window-buffer-names'
590 and `same-window-regexps'. Those variables take precedence over
593 See also `special-display-regexps'."
595 (choice :tag
"Buffer"
597 (string :format
"%v")
598 (cons :tag
"With parameters"
601 (string :format
"%v")
602 (repeat :tag
"Parameters"
604 (symbol :tag
"Parameter")
605 (sexp :tag
"Value"))))
606 (list :tag
"With function"
609 (string :format
"%v")
610 (function :tag
"Function")
611 (repeat :tag
"Arguments" (sexp)))))
615 (defcustom special-display-regexps nil
616 "List of regexps saying which buffers should be displayed specially.
617 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
618 any regexp in this list matches its name, displays it specially
619 using `special-display-function'. `special-display-popup-frame'
620 \(the default for `special-display-function') usually displays
621 the buffer in a separate frame made with the parameters specified
622 by `special-display-frame-alist'. If `special-display-function'
623 has been set to some other function, that function is called with
624 the buffer as first, and nil as second argument.
626 Alternatively, an element of this list can be specified as
627 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
628 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
629 `special-display-popup-frame' will then interpret these pairs as
630 frame parameters when creating a special frame for a buffer whose
631 name matches REGEXP, overriding the corresponding values from
632 `special-display-frame-alist'.
634 As a special case, if FRAME-PARAMETERS contains (same-window . t)
635 `special-display-popup-frame' displays buffers matching REGEXP in
636 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
637 to display such buffers in a window on the selected frame.
639 If `special-display-function' specifies some other function than
640 `special-display-popup-frame', that function is called with the
641 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
644 Finally, an element of this list can be also specified as
645 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
646 will then call FUNCTION with the buffer whose name matched
647 REGEXP as first, and OTHER-ARGS as second argument. If
648 `special-display-function' specifies some other function, that
649 function is called with the buffer whose name matched REGEXP
650 as first, and the element's cdr as second argument.
652 If this variable appears \"not to work\", because you added a
653 name to it but the corresponding buffer is displayed in the
654 selected window, look at the values of `same-window-buffer-names'
655 and `same-window-regexps'. Those variables take precedence over
658 See also `special-display-buffer-names'."
660 (choice :tag
"Buffer"
662 (regexp :format
"%v")
663 (cons :tag
"With parameters"
666 (regexp :format
"%v")
667 (repeat :tag
"Parameters"
669 (symbol :tag
"Parameter")
670 (sexp :tag
"Value"))))
671 (list :tag
"With function"
674 (regexp :format
"%v")
675 (function :tag
"Function")
676 (repeat :tag
"Arguments" (sexp)))))
680 (defun special-display-p (buffer-name)
681 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
682 More precisely, return t if `special-display-buffer-names' or
683 `special-display-regexps' contain a string entry equaling or
684 matching BUFFER-NAME. If `special-display-buffer-names' or
685 `special-display-regexps' contain a list entry whose car equals
686 or matches BUFFER-NAME, the return value is the cdr of that
690 ((not (stringp buffer-name
)))
691 ((member buffer-name special-display-buffer-names
)
693 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
696 (dolist (regexp special-display-regexps
)
699 (when (string-match-p regexp buffer-name
)
701 ((and (consp regexp
) (stringp (car regexp
))
702 (string-match-p (car regexp
) buffer-name
))
703 (throw 'found
(cdr regexp
))))))))))
705 (defcustom special-display-function
'special-display-popup-frame
706 "Function to call for displaying special buffers.
707 This function is called with two arguments - the buffer and,
708 optionally, a list - and should return a window displaying that
709 buffer. The default value usually makes a separate frame for the
710 buffer using `special-display-frame-alist' to specify the frame
711 parameters. See the definition of `special-display-popup-frame'
712 for how to specify such a function.
714 A buffer is special when its name is either listed in
715 `special-display-buffer-names' or matches a regexp in
716 `special-display-regexps'."
720 (defcustom same-window-buffer-names nil
721 "List of names of buffers that should appear in the \"same\" window.
722 `display-buffer' and `pop-to-buffer' show a buffer whose name is
723 on this list in the selected rather than some other window.
725 An element of this list can be a cons cell instead of just a
726 string. In that case, the cell's car must be a string specifying
727 the buffer name. This is for compatibility with
728 `special-display-buffer-names'; the cdr of the cons cell is
731 See also `same-window-regexps'."
732 :type
'(repeat (string :format
"%v"))
735 (defcustom same-window-regexps nil
736 "List of regexps saying which buffers should appear in the \"same\" window.
737 `display-buffer' and `pop-to-buffer' show a buffer whose name
738 matches a regexp on this list in the selected rather than some
741 An element of this list can be a cons cell instead of just a
742 string. In that case, the cell's car must be a regexp matching
743 the buffer name. This is for compatibility with
744 `special-display-regexps'; the cdr of the cons cell is ignored.
746 See also `same-window-buffer-names'."
747 :type
'(repeat (regexp :format
"%v"))
750 (defun same-window-p (buffer-name)
751 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
752 This function returns non-nil if `display-buffer' or
753 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
754 selected rather than \(as usual\) some other window. See
755 `same-window-buffer-names' and `same-window-regexps'."
757 ((not (stringp buffer-name
)))
758 ;; The elements of `same-window-buffer-names' can be buffer
759 ;; names or cons cells whose cars are buffer names.
760 ((member buffer-name same-window-buffer-names
))
761 ((assoc buffer-name same-window-buffer-names
))
763 (dolist (regexp same-window-regexps
)
764 ;; The elements of `same-window-regexps' can be regexps
765 ;; or cons cells whose cars are regexps.
766 (when (or (and (stringp regexp
)
767 (string-match regexp buffer-name
))
768 (and (consp regexp
) (stringp (car regexp
))
769 (string-match-p (car regexp
) buffer-name
)))
770 (throw 'found t
)))))))
772 (defcustom pop-up-frames nil
773 "Whether `display-buffer' should make a separate frame.
774 If nil, never make a seperate frame.
775 If the value is `graphic-only', make a separate frame
776 on graphic displays only.
777 Any other non-nil value means always make a separate frame."
779 (const :tag
"Never" nil
)
780 (const :tag
"On graphic displays only" graphic-only
)
781 (const :tag
"Always" t
))
784 (defcustom display-buffer-reuse-frames nil
785 "Non-nil means `display-buffer' should reuse frames.
786 If the buffer in question is already displayed in a frame, raise
792 (defcustom pop-up-windows t
793 "Non-nil means `display-buffer' should make a new window."
797 (defcustom split-height-threshold
80
798 "Minimum height of window to be split vertically.
799 If the value is a number, `display-buffer' can split a window
800 only if it has at least as many lines. If the value is nil,
801 `display-buffer' cannot split a window vertically.
803 If the window is the only window on its frame, `display-buffer'
804 can split it regardless of this value."
805 :type
'(choice (const nil
) (number :tag
"lines"))
809 (defcustom split-width-threshold
160
810 "Minimum width of window to be split horizontally.
811 If the value is a number, `display-buffer' can split a window
812 only if it has at least as many columns. If the value is nil,
813 `display-buffer' cannot split a window horizontally."
814 :type
'(choice (const nil
) (number :tag
"columns"))
818 (defcustom split-window-preferred-function nil
819 "Function used by `display-buffer' to split windows.
820 If non-nil, a function called with a window as single argument
821 supposed to split that window and return the new window. If the
822 function returns nil the window is not split.
824 If nil, `display-buffer' will split the window respecting the
825 values of `split-height-threshold' and `split-width-threshold'."
826 :type
'(choice (const nil
) (function :tag
"Function"))
830 (defun window--splittable-p (window &optional horizontal
)
831 "Return non-nil if WINDOW can be split evenly.
832 Optional argument HORIZONTAL non-nil means check whether WINDOW
833 can be split horizontally.
835 WINDOW can be split vertically when the following conditions
838 - `window-size-fixed' is either nil or equals `width' for the
841 - `split-height-threshold' is a number and WINDOW is at least as
842 high as `split-height-threshold'.
844 - When WINDOW is split evenly, the emanating windows are at least
845 `window-min-height' lines tall and can accommodate at least one
846 line plus - if WINDOW has one - a mode line.
848 WINDOW can be split horizontally when the following conditions
851 - `window-size-fixed' is either nil or equals `height' for the
854 - `split-width-threshold' is a number and WINDOW is at least as
855 wide as `split-width-threshold'.
857 - When WINDOW is split evenly, the emanating windows are at least
858 `window-min-width' or two (whichever is larger) columns wide."
859 (when (window-live-p window
)
860 (with-current-buffer (window-buffer window
)
862 ;; A window can be split horizontally when its width is not
863 ;; fixed, it is at least `split-width-threshold' columns wide
864 ;; and at least twice as wide as `window-min-width' and 2 (the
865 ;; latter value is hardcoded).
866 (and (memq window-size-fixed
'(nil height
))
867 ;; Testing `window-full-width-p' here hardly makes any
868 ;; sense nowadays. This can be done more intuitively by
869 ;; setting up `split-width-threshold' appropriately.
870 (numberp split-width-threshold
)
871 (>= (window-width window
)
872 (max split-width-threshold
873 (* 2 (max window-min-width
2)))))
874 ;; A window can be split vertically when its height is not
875 ;; fixed, it is at least `split-height-threshold' lines high,
876 ;; and it is at least twice as high as `window-min-height' and 2
877 ;; if it has a modeline or 1.
878 (and (memq window-size-fixed
'(nil width
))
879 (numberp split-height-threshold
)
880 (>= (window-height window
)
881 (max split-height-threshold
882 (* 2 (max window-min-height
883 (if mode-line-format
2 1))))))))))
885 (defun window--try-to-split-window (window)
886 "Split WINDOW if it is splittable.
887 See `window--splittable-p' for how to determine whether a window
888 is splittable. If WINDOW can be split, return the value returned
889 by `split-window' (or `split-window-preferred-function')."
890 (when (and (window-live-p window
)
891 (not (frame-parameter (window-frame window
) 'unsplittable
)))
892 (if (functionp split-window-preferred-function
)
893 ;; `split-window-preferred-function' is specified, so use it.
894 (funcall split-window-preferred-function window
)
895 (or (and (window--splittable-p window
)
896 ;; Split window vertically.
897 (split-window window
))
898 (and (window--splittable-p window t
)
899 ;; Split window horizontally.
900 (split-window window nil t
))
901 (and (eq window
(frame-root-window (window-frame window
)))
902 (not (window-minibuffer-p window
))
903 ;; If WINDOW is the only window on its frame and not the
904 ;; minibuffer window, attempt to split it vertically
905 ;; disregarding the value of `split-height-threshold'.
906 (let ((split-height-threshold 0))
907 (and (window--splittable-p window
)
908 (split-window window
))))))))
910 (defun window--frame-usable-p (frame)
911 "Return FRAME if it can be used to display a buffer."
912 (when (frame-live-p frame
)
913 (let ((window (frame-root-window frame
)))
914 ;; `frame-root-window' may be an internal window which is considered
915 ;; "dead" by `window-live-p'. Hence if `window' is not live we
916 ;; implicitly know that `frame' has a visible window we can use.
917 (unless (and (window-live-p window
)
918 (or (window-minibuffer-p window
)
919 ;; If the window is soft-dedicated, the frame is usable.
920 ;; Actually, even if the window is really dedicated,
921 ;; the frame is still usable by splitting it.
922 ;; At least Emacs-22 allowed it, and it is desirable
923 ;; when displaying same-frame windows.
924 nil
; (eq t (window-dedicated-p window))
928 (defcustom even-window-heights t
929 "If non-nil `display-buffer' will try to even window heights.
930 Otherwise `display-buffer' will leave the window configuration
931 alone. Heights are evened only when `display-buffer' chooses a
932 window that appears above or below the selected window."
936 (defun window--even-window-heights (window)
937 "Even heights of WINDOW and selected window.
938 Do this only if these windows are vertically adjacent to each
939 other, `even-window-heights' is non-nil, and the selected window
940 is higher than WINDOW."
941 (when (and even-window-heights
942 (not (eq window
(selected-window)))
943 ;; Don't resize minibuffer windows.
944 (not (window-minibuffer-p (selected-window)))
945 (> (window-height (selected-window)) (window-height window
))
946 (eq (window-frame window
) (window-frame (selected-window)))
947 (let ((sel-edges (window-edges (selected-window)))
948 (win-edges (window-edges window
)))
949 (and (= (nth 0 sel-edges
) (nth 0 win-edges
))
950 (= (nth 2 sel-edges
) (nth 2 win-edges
))
951 (or (= (nth 1 sel-edges
) (nth 3 win-edges
))
952 (= (nth 3 sel-edges
) (nth 1 win-edges
))))))
953 (let ((window-min-height 1))
954 ;; Don't throw an error if we can't even window heights for
957 (enlarge-window (/ (- (window-height window
) (window-height)) 2))
960 (defun window--display-buffer-1 (window)
961 "Raise the frame containing WINDOW.
962 Do not raise the selected frame. Return WINDOW."
963 (let* ((frame (window-frame window
))
964 (visible (frame-visible-p frame
)))
965 (unless (or (not visible
)
966 ;; Assume the selected frame is already visible enough.
967 (eq frame
(selected-frame))
968 ;; Assume the frame from which we invoked the minibuffer
970 (and (minibuffer-window-active-p (selected-window))
971 (eq frame
(window-frame (minibuffer-selected-window)))))
975 (defun window--display-buffer-2 (buffer window
)
976 "Display BUFFER in WINDOW and make its frame visible.
978 (when (and (buffer-live-p buffer
) (window-live-p window
))
979 (set-window-buffer window buffer
)
980 (window--display-buffer-1 window
)))
982 (defun display-buffer (buffer-or-name &optional not-this-window frame
)
983 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
984 BUFFER-OR-NAME must be a buffer or the name of an existing
985 buffer. Return the window chosen to display BUFFER-OR-NAME or
986 nil if no such window is found.
988 Optional argument NOT-THIS-WINDOW non-nil means display the
989 buffer in a window other than the selected one, even if it is
990 already displayed in the selected window.
992 Optional argument FRAME specifies which frames to investigate
993 when the specified buffer is already displayed. If the buffer is
994 already displayed in some window on one of these frames simply
995 return that window. Possible values of FRAME are:
997 `visible' - consider windows on all visible frames.
999 0 - consider windows on all visible or iconified frames.
1001 t - consider windows on all frames.
1003 A specific frame - consider windows on that frame only.
1005 nil - consider windows on the selected frame \(actually the
1006 last non-minibuffer frame\) only. If, however, either
1007 `display-buffer-reuse-frames' or `pop-up-frames' is non-nil
1008 \(non-nil and not graphic-only on a text-only terminal),
1009 consider all visible or iconified frames."
1010 (interactive "BDisplay buffer:\nP")
1011 (let* ((can-use-selected-window
1012 ;; The selected window is usable unless either NOT-THIS-WINDOW
1013 ;; is non-nil, it is dedicated to its buffer, or it is the
1014 ;; `minibuffer-window'.
1015 (not (or not-this-window
1016 (window-dedicated-p (selected-window))
1017 (window-minibuffer-p))))
1018 (buffer (if (bufferp buffer-or-name
)
1020 (get-buffer buffer-or-name
)))
1021 (name-of-buffer (buffer-name buffer
))
1022 ;; On text-only terminals do not pop up a new frame when
1023 ;; `pop-up-frames' equals graphic-only.
1024 (use-pop-up-frames (if (eq pop-up-frames
'graphic-only
)
1027 ;; `frame-to-use' is the frame where to show `buffer' - either
1028 ;; the selected frame or the last nonminibuffer frame.
1030 (or (window--frame-usable-p (selected-frame))
1031 (window--frame-usable-p (last-nonminibuffer-frame))))
1032 ;; `window-to-use' is the window we use for showing `buffer'.
1035 ((not (buffer-live-p buffer
))
1036 (error "No such buffer %s" buffer
))
1037 (display-buffer-function
1038 ;; Let `display-buffer-function' do the job.
1039 (funcall display-buffer-function buffer not-this-window
))
1040 ((and (not not-this-window
)
1041 (eq (window-buffer (selected-window)) buffer
))
1042 ;; The selected window already displays BUFFER and
1043 ;; `not-this-window' is nil, so use it.
1044 (window--display-buffer-1 (selected-window)))
1045 ((and can-use-selected-window
(same-window-p name-of-buffer
))
1046 ;; If the buffer's name tells us to use the selected window do so.
1047 (window--display-buffer-2 buffer
(selected-window)))
1048 ((let ((frames (or frame
1049 (and (or use-pop-up-frames
1050 display-buffer-reuse-frames
1051 (not (last-nonminibuffer-frame)))
1053 (last-nonminibuffer-frame))))
1056 ;; Search frames for a window displaying BUFFER. Return
1057 ;; the selected window only if we are allowed to do so.
1058 (dolist (window (get-buffer-window-list buffer
'nomini frames
))
1059 (when (or can-use-selected-window
1060 (not (eq (selected-window) window
)))
1061 (throw 'found window
))))))
1062 ;; The buffer is already displayed in some window; use that.
1063 (window--display-buffer-1 window-to-use
))
1064 ((and special-display-function
1065 ;; `special-display-p' returns either t or a list of frame
1066 ;; parameters to pass to `special-display-function'.
1067 (let ((pars (special-display-p name-of-buffer
)))
1069 (funcall special-display-function
1070 buffer
(if (listp pars
) pars
))))))
1071 ((or use-pop-up-frames
(not frame-to-use
))
1072 ;; We want or need a new frame.
1073 (window--display-buffer-2
1074 buffer
(frame-selected-window (funcall pop-up-frame-function
))))
1075 ((and pop-up-windows
1076 ;; Make a new window.
1077 (or (not (frame-parameter frame-to-use
'unsplittable
))
1078 ;; If the selected frame cannot be split look at
1079 ;; `last-nonminibuffer-frame'.
1080 (and (eq frame-to-use
(selected-frame))
1081 (setq frame-to-use
(last-nonminibuffer-frame))
1082 (window--frame-usable-p frame-to-use
)
1083 (not (frame-parameter frame-to-use
'unsplittable
))))
1084 ;; Attempt to split largest or least recently used window.
1086 (or (window--try-to-split-window
1087 (get-largest-window frame-to-use t
))
1088 (window--try-to-split-window
1089 (get-lru-window frame-to-use t
))))
1090 (window--display-buffer-2 buffer window-to-use
)))
1091 ((let ((window-to-undedicate
1092 ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate
1093 ;; the selected window to its buffer, to avoid that some of
1094 ;; the `get-' routines below choose it. (Bug#1415)
1095 (and not-this-window
(not (window-dedicated-p))
1096 (set-window-dedicated-p (selected-window) t
)
1097 (selected-window))))
1100 ;; Reuse an existing window.
1101 (or (get-lru-window frame-to-use
)
1102 (let ((window (get-buffer-window buffer
'visible
)))
1103 (unless (and not-this-window
1104 (eq window
(selected-window)))
1106 (get-largest-window 'visible
)
1107 (let ((window (get-buffer-window buffer
0)))
1108 (unless (and not-this-window
1109 (eq window
(selected-window)))
1111 (get-largest-window 0)
1112 (frame-selected-window (funcall pop-up-frame-function
))))
1113 (when (window-live-p window-to-undedicate
)
1114 ;; Restore dedicated status of selected window.
1115 (set-window-dedicated-p window-to-undedicate nil
))))
1116 (window--even-window-heights window-to-use
)
1117 (window--display-buffer-2 buffer window-to-use
)))))
1119 (defun pop-to-buffer (buffer-or-name &optional other-window norecord
)
1120 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
1121 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1122 nil. If BUFFER-OR-NAME is a string not naming an existent
1123 buffer, create a buffer with that name. If BUFFER-OR-NAME is
1124 nil, choose some other buffer.
1126 If `pop-up-windows' is non-nil, windows can be split to display
1127 the buffer. If optional second arg OTHER-WINDOW is non-nil,
1128 insist on finding another window even if the specified buffer is
1129 already visible in the selected window, and ignore
1130 `same-window-regexps' and `same-window-buffer-names'.
1132 If the window to show BUFFER-OR-NAME is not on the selected
1133 frame, raise that window's frame and give it input focus.
1135 This function returns the buffer it switched to. This uses the
1136 function `display-buffer' as a subroutine; see the documentation
1137 of `display-buffer' for additional customization information.
1139 Optional third arg NORECORD non-nil means do not put this buffer
1140 at the front of the list of recently selected ones."
1142 ;; FIXME: This behavior is carried over from the previous C version
1143 ;; of pop-to-buffer, but really we should use just
1144 ;; `get-buffer' here.
1145 (if (null buffer-or-name
) (other-buffer (current-buffer))
1146 (or (get-buffer buffer-or-name
)
1147 (let ((buf (get-buffer-create buffer-or-name
)))
1148 (set-buffer-major-mode buf
)
1150 (old-window (selected-window))
1151 (old-frame (selected-frame))
1152 new-window new-frame
)
1154 (setq new-window
(display-buffer buffer other-window
))
1155 (unless (eq new-window old-window
)
1156 ;; `display-buffer' has chosen another window, select it.
1157 (select-window new-window norecord
)
1158 (setq new-frame
(window-frame new-window
))
1159 (unless (eq new-frame old-frame
)
1160 ;; `display-buffer' has chosen another frame, make sure it gets
1161 ;; input focus and is risen.
1162 (select-frame-set-input-focus new-frame
)))
1165 ;; I think this should be the default; I think people will prefer it--rms.
1166 (defcustom split-window-keep-point t
1167 "If non-nil, \\[split-window-vertically] keeps the original point \
1169 This is often more convenient for editing.
1170 If nil, adjust point in each of the two windows to minimize redisplay.
1171 This is convenient on slow terminals, but point can move strangely.
1173 This option applies only to `split-window-vertically' and
1174 functions that call it. `split-window' always keeps the original
1175 point in both children."
1179 (defun split-window-vertically (&optional size
)
1180 "Split selected window into two windows, one above the other.
1181 The upper window gets SIZE lines and the lower one gets the rest.
1182 SIZE negative means the lower window gets -SIZE lines and the
1183 upper one the rest. With no argument, split windows equally or
1184 close to it. Both windows display the same buffer, now current.
1186 If the variable `split-window-keep-point' is non-nil, both new
1187 windows will get the same value of point as the selected window.
1188 This is often more convenient for editing. The upper window is
1189 the selected window.
1191 Otherwise, we choose window starts so as to minimize the amount of
1192 redisplay; this is convenient on slow terminals. The new selected
1193 window is the one that the current value of point appears in. The
1194 value of point can change if the text around point is hidden by the
1197 Regardless of the value of `split-window-keep-point', the upper
1198 window is the original one and the return value is the new, lower
1201 (let ((old-window (selected-window))
1203 (size (and size
(prefix-numeric-value size
)))
1204 moved-by-window-height moved new-window bottom
)
1205 (and size
(< size
0)
1206 ;; Handle negative SIZE value.
1207 (setq size
(+ (window-height) size
)))
1208 (setq new-window
(split-window nil size
))
1209 (unless split-window-keep-point
1211 (set-buffer (window-buffer))
1212 (goto-char (window-start))
1213 (setq moved
(vertical-motion (window-height)))
1214 (set-window-start new-window
(point))
1215 (when (> (point) (window-point new-window
))
1216 (set-window-point new-window
(point)))
1217 (when (= moved
(window-height))
1218 (setq moved-by-window-height t
)
1219 (vertical-motion -
1))
1220 (setq bottom
(point)))
1221 (and moved-by-window-height
1223 (set-window-point old-window
(1- bottom
)))
1224 (and moved-by-window-height
1225 (<= (window-start new-window
) old-point
)
1226 (set-window-point new-window old-point
)
1227 (select-window new-window
)))
1228 (split-window-save-restore-data new-window old-window
)))
1230 ;; This is to avoid compiler warnings.
1231 (defvar view-return-to-alist
)
1233 (defun split-window-save-restore-data (new-window old-window
)
1234 (with-current-buffer (window-buffer)
1236 (let ((old-info (assq old-window view-return-to-alist
)))
1238 (push (cons new-window
(cons (car (cdr old-info
)) t
))
1239 view-return-to-alist
))))
1242 (defun split-window-horizontally (&optional size
)
1243 "Split selected window into two windows side by side.
1244 The selected window becomes the left one and gets SIZE columns.
1245 SIZE negative means the right window gets -SIZE lines.
1247 SIZE includes the width of the window's scroll bar; if there are
1248 no scroll bars, it includes the width of the divider column to
1249 the window's right, if any. SIZE omitted or nil means split
1252 The selected window remains selected. Return the new window."
1254 (let ((old-window (selected-window))
1255 (size (and size
(prefix-numeric-value size
))))
1256 (and size
(< size
0)
1257 ;; Handle negative SIZE value.
1258 (setq size
(+ (window-width) size
)))
1259 (split-window-save-restore-data (split-window nil size t
) old-window
)))
1262 (defun set-window-text-height (window height
)
1263 "Set the height in lines of the text display area of WINDOW to HEIGHT.
1264 HEIGHT doesn't include the mode line or header line, if any, or
1265 any partial-height lines in the text display area.
1267 Note that the current implementation of this function cannot
1268 always set the height exactly, but attempts to be conservative,
1269 by allocating more lines than are actually needed in the case
1270 where some error may be present."
1271 (let ((delta (- height
(window-text-height window
))))
1272 (unless (zerop delta
)
1273 ;; Setting window-min-height to a value like 1 can lead to very
1274 ;; bizarre displays because it also allows Emacs to make *other*
1275 ;; windows 1-line tall, which means that there's no more space for
1277 (let ((window-min-height (min 2 height
))) ; One text line plus a modeline.
1278 (if (and window
(not (eq window
(selected-window))))
1279 (save-selected-window
1280 (select-window window
'norecord
)
1281 (enlarge-window delta
))
1282 (enlarge-window delta
))))))
1285 (defun enlarge-window-horizontally (columns)
1286 "Make selected window COLUMNS wider.
1287 Interactively, if no argument is given, make selected window one
1290 (enlarge-window columns t
))
1292 (defun shrink-window-horizontally (columns)
1293 "Make selected window COLUMNS narrower.
1294 Interactively, if no argument is given, make selected window one
1297 (shrink-window columns t
))
1299 (defun window-buffer-height (window)
1300 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
1301 (with-current-buffer (window-buffer window
)
1303 (count-screen-lines (point-min) (point-max)
1304 ;; If buffer ends with a newline, ignore it when
1305 ;; counting height unless point is after it.
1309 (defun count-screen-lines (&optional beg end count-final-newline window
)
1310 "Return the number of screen lines in the region.
1311 The number of screen lines may be different from the number of actual lines,
1312 due to line breaking, display table, etc.
1314 Optional arguments BEG and END default to `point-min' and `point-max'
1317 If region ends with a newline, ignore it unless optional third argument
1318 COUNT-FINAL-NEWLINE is non-nil.
1320 The optional fourth argument WINDOW specifies the window used for obtaining
1321 parameters such as width, horizontal scrolling, and so on. The default is
1322 to use the selected window's parameters.
1324 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
1325 regardless of which buffer is displayed in WINDOW. This makes possible to use
1326 `count-screen-lines' in any buffer, whether or not it is currently displayed
1329 (setq beg
(point-min)))
1331 (setq end
(point-max)))
1337 (narrow-to-region (min beg end
)
1338 (if (and (not count-final-newline
)
1339 (= ?
\n (char-before (max beg end
))))
1342 (goto-char (point-min))
1343 (1+ (vertical-motion (buffer-size) window
))))))
1345 (defun fit-window-to-buffer (&optional window max-height min-height
)
1346 "Adjust height of WINDOW to display its buffer's contents exactly.
1347 WINDOW defaults to the selected window.
1348 Optional argument MAX-HEIGHT specifies the maximum height of the
1349 window and defaults to the maximum permissible height of a window
1351 Optional argument MIN-HEIGHT specifies the minimum height of the
1352 window and defaults to `window-min-height'.
1353 Both, MAX-HEIGHT and MIN-HEIGHT are specified in lines and
1354 include the mode line and header line, if any.
1356 Return non-nil if height was orderly adjusted, nil otherwise.
1358 Caution: This function can delete WINDOW and/or other windows
1359 when their height shrinks to less than MIN-HEIGHT."
1361 ;; Do all the work in WINDOW and its buffer and restore the selected
1362 ;; window and the current buffer when we're done.
1363 (let ((old-buffer (current-buffer))
1365 (with-selected-window (or window
(setq window
(selected-window)))
1366 (set-buffer (window-buffer))
1367 ;; Use `condition-case' to handle any fixed-size windows and other
1370 (let* (;; MIN-HEIGHT must not be less than 1 and defaults to
1371 ;; `window-min-height'.
1372 (min-height (max (or min-height window-min-height
) 1))
1374 ;; Maximum height of any window on this frame.
1375 (min (window-height (frame-root-window)) (frame-height)))
1376 ;; MAX-HEIGHT must not be larger than max-window-height and
1377 ;; defaults to max-window-height.
1379 (min (or max-height max-window-height
) max-window-height
))
1381 ;; The height necessary to show all of WINDOW's buffer,
1382 ;; constrained by MIN-HEIGHT and MAX-HEIGHT.
1385 ;; For an empty buffer `count-screen-lines' returns zero.
1386 ;; Even in that case we need one line for the cursor.
1387 (+ (max (count-screen-lines) 1)
1388 ;; For non-minibuffers count the mode line, if any.
1389 (if (and (not (window-minibuffer-p)) mode-line-format
)
1391 ;; Count the header line, if any.
1392 (if header-line-format
1 0))
1396 ;; How much the window height has to change.
1397 (if (= (window-height) (window-height (frame-root-window)))
1398 ;; Don't try to resize a full-height window.
1400 (- desired-height
(window-height))))
1401 ;; Do something reasonable so `enlarge-window' can make
1402 ;; windows as small as MIN-HEIGHT.
1403 (window-min-height (min min-height window-min-height
)))
1404 ;; Don't try to redisplay with the cursor at the end on its
1405 ;; own line--that would force a scroll and spoil things.
1406 (when (and (eobp) (bolp) (not (bobp)))
1407 (set-window-point window
(1- (window-point))))
1408 ;; Adjust WINDOW's height to the nominally correct one
1409 ;; (which may actually be slightly off because of variable
1410 ;; height text, etc).
1411 (unless (zerop delta
)
1412 (enlarge-window delta
))
1413 ;; `enlarge-window' might have deleted WINDOW, so make sure
1414 ;; WINDOW's still alive for the remainder of this.
1415 ;; Note: Deleting WINDOW is clearly counter-intuitive in
1416 ;; this context, but we can't do much about it given the
1417 ;; current semantics of `enlarge-window'.
1418 (when (window-live-p window
)
1419 ;; Check if the last line is surely fully visible. If
1420 ;; not, enlarge the window.
1421 (let ((end (save-excursion
1422 (goto-char (point-max))
1423 (when (and (bolp) (not (bobp)))
1424 ;; Don't include final newline.
1426 (when truncate-lines
1427 ;; If line-wrapping is turned off, test the
1428 ;; beginning of the last line for
1429 ;; visibility instead of the end, as the
1430 ;; end of the line could be invisible by
1431 ;; virtue of extending past the edge of the
1435 (set-window-vscroll window
0)
1436 (while (and (< desired-height max-height
)
1437 (= desired-height
(window-height))
1438 (not (pos-visible-in-window-p end
)))
1440 (setq desired-height
(1+ desired-height
))))
1441 ;; Return non-nil only if nothing "bad" happened.
1444 (when (buffer-live-p old-buffer
)
1445 (set-buffer old-buffer
))
1448 (defun window-safely-shrinkable-p (&optional window
)
1449 "Return t if WINDOW can be shrunk without shrinking other windows.
1450 WINDOW defaults to the selected window."
1451 (with-selected-window (or window
(selected-window))
1452 (let ((edges (window-edges)))
1453 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
1454 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
1456 (defun shrink-window-if-larger-than-buffer (&optional window
)
1457 "Shrink height of WINDOW if its buffer doesn't need so many lines.
1458 More precisely, shrink WINDOW vertically to be as small as
1459 possible, while still showing the full contents of its buffer.
1460 WINDOW defaults to the selected window.
1462 Do not shrink to less than `window-min-height' lines. Do nothing
1463 if the buffer contains more lines than the present window height,
1464 or if some of the window's contents are scrolled out of view, or
1465 if shrinking this window would also shrink another window, or if
1466 the window is the only window of its frame.
1468 Return non-nil if the window was shrunk, nil otherwise."
1471 (setq window
(selected-window)))
1472 (let* ((frame (window-frame window
))
1473 (mini (frame-parameter frame
'minibuffer
))
1474 (edges (window-edges window
)))
1475 (if (and (not (eq window
(frame-root-window frame
)))
1476 (window-safely-shrinkable-p window
)
1477 (pos-visible-in-window-p (point-min) window
)
1478 (not (eq mini
'only
))
1480 (let ((mini-window (minibuffer-window frame
)))
1481 (or (null mini-window
)
1482 (not (eq frame
(window-frame mini-window
)))
1484 (nth 1 (window-edges mini-window
)))
1486 (frame-parameter frame
'menu-bar-lines
))))))
1487 (fit-window-to-buffer window
(window-height window
)))))
1489 (defun kill-buffer-and-window ()
1490 "Kill the current buffer and delete the selected window."
1492 (let ((window-to-delete (selected-window))
1493 (buffer-to-kill (current-buffer))
1494 (delete-window-hook (lambda ()
1500 (add-hook 'kill-buffer-hook delete-window-hook t t
)
1501 (if (kill-buffer (current-buffer))
1502 ;; If `delete-window' failed before, we rerun it to regenerate
1503 ;; the error so it can be seen in the echo area.
1504 (when (eq (selected-window) window-to-delete
)
1506 ;; If the buffer is not dead for some reason (probably because
1507 ;; of a `quit' signal), remove the hook again.
1509 (with-current-buffer buffer-to-kill
1510 (remove-hook 'kill-buffer-hook delete-window-hook t
))
1513 (defun quit-window (&optional kill window
)
1514 "Quit WINDOW and bury its buffer.
1515 With a prefix argument, kill the buffer instead. WINDOW defaults
1516 to the selected window.
1518 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
1519 it and, if it's alone on its frame, its frame too. Otherwise, or
1520 if deleting WINDOW fails in any of the preceding cases, display
1521 another buffer in WINDOW using `switch-to-buffer'.
1523 Optional argument KILL non-nil means kill WINDOW's buffer.
1524 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
1526 (let ((buffer (window-buffer window
)))
1528 (window-minibuffer-p window
)
1529 (window-dedicated-p window
))
1530 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
1531 ;; try to delete it.
1532 (let* ((window (or window
(selected-window)))
1533 (frame (window-frame window
)))
1534 (if (eq window
(frame-root-window frame
))
1535 ;; WINDOW is alone on its frame. `delete-windows-on'
1536 ;; knows how to handle that case.
1537 (delete-windows-on buffer frame
)
1538 ;; There are other windows on its frame, delete WINDOW.
1539 (delete-window window
)))
1540 ;; Otherwise, switch to another buffer in the selected window.
1541 (switch-to-buffer nil
))
1543 ;; Deal with the buffer.
1545 (kill-buffer buffer
)
1546 (bury-buffer buffer
))))
1548 (defvar recenter-last-op nil
1549 "Indicates the last recenter operation performed.
1550 Possible values: `top', `middle', `bottom'.")
1552 (defun recenter-top-bottom (&optional arg
)
1553 "Move current line to window center, top, and bottom, successively.
1554 With no prefix argument, the first call redraws the frame and
1555 centers point vertically within the window. Successive calls
1556 scroll the window, placing point on the top, bottom, and middle
1557 consecutively. The cycling order is middle -> top -> bottom.
1559 A prefix argument is handled like `recenter':
1560 With numeric prefix ARG, move current line to window-line ARG.
1561 With plain `C-u', move current line to window center.
1563 Top and bottom destinations are actually `scroll-margin' lines
1564 the from true window top and bottom."
1567 (arg (recenter arg
)) ; Always respect ARG.
1568 ((or (not (eq this-command last-command
))
1569 (eq recenter-last-op
'bottom
))
1570 (setq recenter-last-op
'middle
)
1573 (let ((this-scroll-margin
1574 (min (max 0 scroll-margin
)
1575 (truncate (/ (window-body-height) 4.0)))))
1576 (cond ((eq recenter-last-op
'middle
)
1577 (setq recenter-last-op
'top
)
1578 (recenter this-scroll-margin
))
1579 ((eq recenter-last-op
'top
)
1580 (setq recenter-last-op
'bottom
)
1581 (recenter (- -
1 this-scroll-margin
))))))))
1583 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
1585 (defvar mouse-autoselect-window-timer nil
1586 "Timer used by delayed window autoselection.")
1588 (defvar mouse-autoselect-window-position nil
1589 "Last mouse position recorded by delayed window autoselection.")
1591 (defvar mouse-autoselect-window-window nil
1592 "Last window recorded by delayed window autoselection.")
1594 (defvar mouse-autoselect-window-state nil
1595 "When non-nil, special state of delayed window autoselection.
1596 Possible values are `suspend' \(suspend autoselection after a menu or
1597 scrollbar interaction\) and `select' \(the next invocation of
1598 'handle-select-window' shall select the window immediately\).")
1600 (defun mouse-autoselect-window-cancel (&optional force
)
1601 "Cancel delayed window autoselection.
1602 Optional argument FORCE means cancel unconditionally."
1603 (unless (and (not force
)
1604 ;; Don't cancel for select-window or select-frame events
1605 ;; or when the user drags a scroll bar.
1606 (or (memq this-command
1607 '(handle-select-window handle-switch-frame
))
1608 (and (eq this-command
'scroll-bar-toolkit-scroll
)
1609 (memq (nth 4 (event-end last-input-event
))
1610 '(handle end-scroll
)))))
1611 (setq mouse-autoselect-window-state nil
)
1612 (when (timerp mouse-autoselect-window-timer
)
1613 (cancel-timer mouse-autoselect-window-timer
))
1614 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
1616 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
1617 "Start delayed window autoselection.
1618 MOUSE-POSITION is the last position where the mouse was seen as returned
1619 by `mouse-position'. Optional argument WINDOW non-nil denotes the
1620 window where the mouse was seen. Optional argument SUSPEND non-nil
1621 means suspend autoselection."
1622 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
1623 (setq mouse-autoselect-window-position mouse-position
)
1624 (when window
(setq mouse-autoselect-window-window window
))
1625 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
1626 ;; Install timer which runs `mouse-autoselect-window-select' after
1627 ;; `mouse-autoselect-window' seconds.
1628 (setq mouse-autoselect-window-timer
1630 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
1632 (defun mouse-autoselect-window-select ()
1633 "Select window with delayed window autoselection.
1634 If the mouse position has stabilized in a non-selected window, select
1635 that window. The minibuffer window is selected only if the minibuffer is
1636 active. This function is run by `mouse-autoselect-window-timer'."
1638 (let* ((mouse-position (mouse-position))
1641 (window-at (cadr mouse-position
) (cddr mouse-position
)
1642 (car mouse-position
))
1645 ((or (menu-or-popup-active-p)
1647 (not (coordinates-in-window-p (cdr mouse-position
) window
))))
1648 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
1649 ;; of WINDOW, temporarily suspend delayed autoselection.
1650 (mouse-autoselect-window-start mouse-position nil t
))
1651 ((eq mouse-autoselect-window-state
'suspend
)
1652 ;; Delayed autoselection was temporarily suspended, reenable it.
1653 (mouse-autoselect-window-start mouse-position
))
1654 ((and window
(not (eq window
(selected-window)))
1655 (or (not (numberp mouse-autoselect-window
))
1656 (and (> mouse-autoselect-window
0)
1657 ;; If `mouse-autoselect-window' is positive, select
1658 ;; window if the window is the same as before.
1659 (eq window mouse-autoselect-window-window
))
1660 ;; Otherwise select window if the mouse is at the same
1661 ;; position as before. Observe that the first test after
1662 ;; starting autoselection usually fails since the value of
1663 ;; `mouse-autoselect-window-position' recorded there is the
1664 ;; position where the mouse has entered the new window and
1665 ;; not necessarily where the mouse has stopped moving.
1666 (equal mouse-position mouse-autoselect-window-position
))
1667 ;; The minibuffer is a candidate window if it's active.
1668 (or (not (window-minibuffer-p window
))
1669 (eq window
(active-minibuffer-window))))
1670 ;; Mouse position has stabilized in non-selected window: Cancel
1671 ;; delayed autoselection and try to select that window.
1672 (mouse-autoselect-window-cancel t
)
1673 ;; Select window where mouse appears unless the selected window is the
1674 ;; minibuffer. Use `unread-command-events' in order to execute pre-
1675 ;; and post-command hooks and trigger idle timers. To avoid delaying
1676 ;; autoselection again, set `mouse-autoselect-window-state'."
1677 (unless (window-minibuffer-p (selected-window))
1678 (setq mouse-autoselect-window-state
'select
)
1679 (setq unread-command-events
1680 (cons (list 'select-window
(list window
))
1681 unread-command-events
))))
1682 ((or (and window
(eq window
(selected-window)))
1683 (not (numberp mouse-autoselect-window
))
1684 (equal mouse-position mouse-autoselect-window-position
))
1685 ;; Mouse position has either stabilized in the selected window or at
1686 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
1687 (mouse-autoselect-window-cancel t
))
1689 ;; Mouse position has not stabilized yet, resume delayed
1691 (mouse-autoselect-window-start mouse-position window
))))
1694 (defun handle-select-window (event)
1695 "Handle select-window events."
1697 (let ((window (posn-window (event-start event
))))
1698 (unless (or (not (window-live-p window
))
1699 ;; Don't switch if we're currently in the minibuffer.
1700 ;; This tries to work around problems where the
1701 ;; minibuffer gets unselected unexpectedly, and where
1702 ;; you then have to move your mouse all the way down to
1703 ;; the minibuffer to select it.
1704 (window-minibuffer-p (selected-window))
1705 ;; Don't switch to minibuffer window unless it's active.
1706 (and (window-minibuffer-p window
)
1707 (not (minibuffer-window-active-p window
)))
1708 ;; Don't switch when autoselection shall be delayed.
1709 (and (numberp mouse-autoselect-window
)
1710 (not (zerop mouse-autoselect-window
))
1711 (not (eq mouse-autoselect-window-state
'select
))
1713 ;; Cancel any delayed autoselection.
1714 (mouse-autoselect-window-cancel t
)
1715 ;; Start delayed autoselection from current mouse
1716 ;; position and window.
1717 (mouse-autoselect-window-start (mouse-position) window
)
1718 ;; Executing a command cancels delayed autoselection.
1720 'pre-command-hook
'mouse-autoselect-window-cancel
))))
1721 (when mouse-autoselect-window
1722 ;; Reset state of delayed autoselection.
1723 (setq mouse-autoselect-window-state nil
)
1724 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
1725 (run-hooks 'mouse-leave-buffer-hook
))
1726 (select-window window
))))
1728 (defun delete-other-windows-vertically (&optional window
)
1729 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
1730 This may be a useful alternative binding for \\[delete-other-windows]
1731 if you often split windows horizontally."
1733 (let* ((window (or window
(selected-window)))
1734 (edges (window-edges window
))
1736 (while (not (eq (setq w
(next-window w
1)) window
))
1737 (let ((e (window-edges w
)))
1738 (when (and (= (car e
) (car edges
))
1739 (= (caddr e
) (caddr edges
)))
1741 (mapc 'delete-window delenda
)))
1743 (defun truncated-partial-width-window-p (&optional window
)
1744 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
1745 WINDOW defaults to the selected window.
1746 Return nil if WINDOW is not a partial-width window
1747 (regardless of the value of `truncate-lines').
1748 Otherwise, consult the value of `truncate-partial-width-windows'
1749 for the buffer shown in WINDOW."
1751 (setq window
(selected-window)))
1752 (unless (window-full-width-p window
)
1753 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
1754 (window-buffer window
))))
1755 (if (integerp t-p-w-w
)
1756 (< (window-width window
) t-p-w-w
)
1759 (define-key ctl-x-map
"2" 'split-window-vertically
)
1760 (define-key ctl-x-map
"3" 'split-window-horizontally
)
1761 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
1762 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
1763 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
1764 (define-key ctl-x-map
"+" 'balance-windows
)
1765 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
1767 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
1768 ;;; window.el ends here