File name fix, for AUTHORS.
[emacs.git] / lisp / window.el
blobed97c61f43473c05fb43a2a294da98d2693b4287
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.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 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/>.
24 ;;; Commentary:
26 ;; Window tree functions.
28 ;;; Code:
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
50 window is left alone.
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)))
62 (frame-list))))
63 (save-current-buffer
64 (unwind-protect
65 (progn ,@body)
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
94 when it is active.
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)))
105 (eq 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',
116 or nil).
118 Unlike `window-scroll-bars', this function reports the scroll bar
119 type actually used, once frame defaults and `scroll-bar-mode' are
120 taken into account."
121 (let ((vert (nth 2 (window-scroll-bars window)))
122 (hor nil))
123 (when (or (eq vert t) (eq hor t))
124 (let ((fcsb (frame-current-scroll-bars
125 (window-frame (or window (selected-window))))))
126 (if (eq vert t)
127 (setq vert (car fcsb)))
128 (if (eq hor t)
129 (setq hor (cdr fcsb)))))
130 (cons vert hor)))
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
150 window.
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
156 too.
157 ALL-FRAMES t means cycle through all windows on all existing
158 frames.
159 ALL-FRAMES `visible' means cycle through all windows on all
160 visible frames.
161 ALL-FRAMES 0 means cycle through all windows on all visible and
162 iconified frames.
163 ALL-FRAMES a frame means cycle through all windows on that frame
164 only.
165 Anything else means cycle through all windows on WINDOW's frame
166 and no others.
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
171 ;; back to it.
172 (when (window-minibuffer-p (selected-window))
173 (setq minibuf t))
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)))
182 (while (progn
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
191 all-frames default)
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,
197 return DEFAULT.
199 The optional arguments MINIBUF and ALL-FRAMES specify the set of
200 windows to include. See `walk-windows' for the meaning of these
201 arguments."
202 (catch 'found
203 (walk-windows #'(lambda (window)
204 (when (funcall predicate window)
205 (throw 'found window)))
206 minibuf all-frames)
207 default))
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
219 of these arguments."
220 (let ((buffer (cond
221 ((not buffer-or-name) (current-buffer))
222 ((bufferp buffer-or-name) buffer-or-name)
223 (t (get-buffer buffer-or-name))))
224 windows)
225 (walk-windows (function (lambda (window)
226 (if (eq (window-buffer window) buffer)
227 (setq windows (cons window windows)))))
228 minibuf all-frames)
229 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."
240 (let ((count 0))
241 (walk-windows (lambda (w) (setq count (+ count 1)))
242 minibuf)
243 count))
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)))
262 (when subtree
263 (if (integerp subtree)
265 (bw-get-tree-1 subtree)))))
267 (defun bw-get-tree-1 (split)
268 (if (windowp split)
269 split
270 (let ((dir (car split))
271 (edges (car (cdr split)))
272 (childs (cdr (cdr split))))
273 (list
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)))
286 (if window
287 (bw-find-tree-sub-1 wt window get-parent)
288 wt))))
290 (defun bw-find-tree-sub-1 (tree win &optional get-parent)
291 (unless (windowp win) (error "Not a window: %s" win))
292 (if (memq win tree)
293 (if get-parent
294 get-parent
295 tree)
296 (let ((childs (cdr (cdr tree)))
297 child
298 subtree)
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)
305 (progn
306 (if (= 1 subtree)
307 tree
308 (1- subtree)))
309 subtree
310 ))))
312 ;;; Window or object edges
314 (defun bw-l (obj)
315 "Left edge of OBJ."
316 (if (windowp obj) (nth 0 (window-edges obj)) (cdr (assq 'l obj))))
317 (defun bw-t (obj)
318 "Top edge of OBJ."
319 (if (windowp obj) (nth 1 (window-edges obj)) (cdr (assq 't obj))))
320 (defun bw-r (obj)
321 "Right edge of OBJ."
322 (if (windowp obj) (nth 2 (window-edges obj)) (cdr (assq 'r obj))))
323 (defun bw-b (obj)
324 "Bottom edge of OBJ."
325 (if (windowp obj) (nth 3 (window-edges obj)) (cdr (assq 'b obj))))
327 ;;; Split directions
329 (defun bw-dir (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."
333 (if (symbolp obj)
335 (if (windowp obj)
336 'both
337 (let ((dir (cdr (assq 'dir obj))))
338 (unless (memq dir '(hor ver both))
339 (error "Can't find dir in %s" obj))
340 dir))))
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)))
349 (or (eq dir1 dir2)
350 (eq dir1 'both)
351 (eq dir2 'both))))
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)))
359 (ol 1000)
360 (ot 1000)
361 (or -1)
362 (ob -1))
363 (dolist (o childs)
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))
377 obj)
379 ;;; Balance windows
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."
388 (interactive)
389 (let (
390 (wt (bw-get-tree window-or-frame))
393 (tried-sizes)
394 (last-sizes)
395 (windows (window-list nil 0)))
396 (when wt
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)
400 (window-edges w))
401 windows))
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)
413 (condition-case err
414 (progn
415 (adjust-window-trailing-edge window delta horizontal)
416 (setq delta 0))
417 (error
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))))
425 (if (windowp wt)
426 (progn
427 (when w
428 (let ((dw (- w (- (bw-r wt) (bw-l wt)))))
429 (when (/= 0 dw)
430 (bw-adjust-window wt dw t))))
431 (when h
432 (let ((dh (- h (- (bw-b wt) (bw-t wt)))))
433 (when (/= 0 dh)
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)))))
438 (dolist (c childs)
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)
447 (not (and direction
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
462 specific buffers."
463 (interactive)
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))))
469 (changelog nil)
470 next)
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
484 ;; the beginning.
485 (while (< unchanged 2)
486 ;; (message "New round")
487 (setq unchanged (1+ unchanged) round (1+ round))
488 (dolist (win wins)
489 (setq next win)
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))))
493 (let* ((horiz
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
502 (window-buffer win))
503 (buffer-local-value 'window-area-factor
504 (window-buffer next)))))
505 (edgesize (if horiz
506 (+ (window-height win) (window-height next))
507 (+ (window-width win) (window-width next))))
508 (diff (/ areadiff edgesize)))
509 (when (zerop diff)
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))))
516 (if (zerop diff)
517 ;; Make sure negligible differences don't accumulate to
518 ;; become significant.
519 (setq carry (+ carry areadiff))
520 (bw-adjust-window win diff horiz)
521 ;; (sit-for 0.5)
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
525 ;; a change.
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."
546 :type '(choice
547 (const nil)
548 (function :tag "function"))
549 :group 'windows)
551 (defun special-display-p (buffer-name)
552 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
553 If the value is t, `display-buffer' or `pop-to-buffer' would
554 create a special frame for that buffer using the default frame
555 parameters.
557 If the value is a list, it is a list of frame parameters that
558 would be used to make a frame for that buffer. The variables
559 `special-display-buffer-names' and `special-display-regexps'
560 control this."
561 (let (tmp)
562 (cond
563 ((not (stringp buffer-name)))
564 ;; Make sure to return t in the following two cases.
565 ((member buffer-name special-display-buffer-names) t)
566 ((setq tmp (assoc buffer-name special-display-buffer-names)) (cdr tmp))
567 ((catch 'found
568 (dolist (regexp special-display-regexps)
569 (cond
570 ((stringp regexp)
571 (when (string-match-p regexp buffer-name)
572 (throw 'found t)))
573 ((and (consp regexp) (stringp (car regexp))
574 (string-match-p (car regexp) buffer-name))
575 (throw 'found (cdr regexp))))))))))
577 (defcustom special-display-buffer-names nil
578 "List of buffer names that should have their own special frames.
579 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
580 its name is in this list, makes a special frame for it using
581 `special-display-function'. See also `special-display-regexps'.
583 An element of the list can be a list instead of just a string.
584 There are two ways to use a list as an element:
585 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
586 In the first case, the FRAME-PARAMETERS are pairs of the form
587 \(PARAMETER . VALUE); these parameter values are used to create
588 the frame. In the second case, FUNCTION is called with BUFFER as
589 the first argument, followed by the OTHER-ARGS--it can display
590 BUFFER in any way it likes. All this is done by the function
591 found in `special-display-function'.
593 If the specified frame parameters include (same-buffer . t), the
594 buffer is displayed in the currently selected window. Otherwise, if
595 they include (same-frame . t), the buffer is displayed in a new window
596 in the currently selected frame.
598 If this variable appears \"not to work\", because you add a name to it
599 but that buffer still appears in the selected window, look at the
600 values of `same-window-buffer-names' and `same-window-regexps'.
601 Those variables take precedence over this one."
602 :type '(repeat (choice :tag "Buffer"
603 :value ""
604 (string :format "%v")
605 (cons :tag "With attributes"
606 :format "%v"
607 :value ("" . nil)
608 (string :format "%v")
609 (repeat :tag "Attributes"
610 (cons :format "%v"
611 (symbol :tag "Parameter")
612 (sexp :tag "Value"))))))
613 :group 'frames)
615 (defcustom special-display-regexps nil
616 "List of regexps saying which buffers should have their own special frames.
617 When displaying a buffer with `display-buffer' or
618 `pop-to-buffer', if any regexp in this list matches the buffer
619 name, it makes a special frame for the buffer by calling
620 `special-display-function'.
622 An element of the list can be a list instead of just a string.
623 There are two ways to use a list as an element:
624 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
625 In the first case, the FRAME-PARAMETERS are pairs of the form
626 \(PARAMETER . VALUE); these parameter values are used to create
627 the frame. In the second case, FUNCTION is called with BUFFER as
628 the first argument, followed by the OTHER-ARGS--it can display
629 the buffer in any way it likes. All this is done by the function
630 found in `special-display-function'.
632 If the specified frame parameters include (same-buffer . t), the
633 buffer is displayed in the currently selected window. Otherwise,
634 if they include (same-frame . t), the buffer is displayed in a
635 new window in the currently selected frame.
637 If this variable appears \"not to work\", because you add a
638 regexp to it but the matching buffers still appear in the
639 selected window, look at the values of `same-window-buffer-names'
640 and `same-window-regexps'. Those variables take precedence over
641 this one."
642 :type '(repeat (choice :tag "Buffer"
643 :value ""
644 (regexp :format "%v")
645 (cons :tag "With attributes"
646 :format "%v"
647 :value ("" . nil)
648 (regexp :format "%v")
649 (repeat :tag "Attributes"
650 (cons :format "%v"
651 (symbol :tag "Parameter")
652 (sexp :tag "Value"))))))
653 :group 'frames)
655 (defcustom special-display-function 'special-display-popup-frame
656 "Function to call to make a new frame for a special buffer.
657 It is called with two arguments, the buffer and optional buffer
658 specific data, and should return a window displaying that buffer.
659 The default value normally makes a separate frame for the buffer,
660 using `special-display-frame-alist' to specify the frame
661 parameters.
663 But if the buffer specific data includes (same-buffer . t) then
664 the buffer is displayed in the current selected window.
665 Otherwise if it includes (same-frame . t) then the buffer is
666 displayed in a new window in the currently selected frame.
668 A buffer is special if it is listed in
669 `special-display-buffer-names' or matches a regexp in
670 `special-display-regexps'."
671 :type 'function
672 :group 'frames)
674 (defun same-window-p (buffer-name)
675 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
676 This function returns non-nil if `display-buffer' or
677 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
678 selected rather than \(as usual\) some other window. See
679 `same-window-buffer-names' and `same-window-regexps'."
680 (cond
681 ((not (stringp buffer-name)))
682 ;; The elements of `same-window-buffer-names' can be buffer
683 ;; names or cons cells whose cars are buffer names.
684 ((member buffer-name same-window-buffer-names))
685 ((assoc buffer-name same-window-buffer-names))
686 ((catch 'found
687 (dolist (regexp same-window-regexps)
688 ;; The elements of `same-window-regexps' can be regexps
689 ;; or cons cells whose cars are regexps.
690 (when (or (and (stringp regexp)
691 (string-match regexp buffer-name))
692 (and (consp regexp) (stringp (car regexp))
693 (string-match-p (car regexp) buffer-name)))
694 (throw 'found t)))))))
696 (defcustom same-window-buffer-names nil
697 "List of names of buffers that should appear in the \"same\" window.
698 `display-buffer' and `pop-to-buffer' show a buffer whose name is
699 on this list in the selected rather than some other window.
701 An element of this list can be a cons cell instead of just a
702 string. In that case the car must be a string specifying the
703 buffer name. This is for compatibility with
704 `special-display-buffer-names'; the cdr of the cons cell is
705 ignored.
707 See also `same-window-regexps'."
708 :type '(repeat (string :format "%v"))
709 :group 'windows)
711 (defcustom same-window-regexps nil
712 "List of regexps saying which buffers should appear in the \"same\" window.
713 `display-buffer' and `pop-to-buffer' show a buffer whose name
714 matches a regexp on this list in the selected rather than some
715 other window.
717 An element of this list can be a cons cell instead of just a
718 string. In that case the car must be a string, which specifies
719 the buffer name. This is for compatibility with
720 `special-display-buffer-names'; the cdr of the cons cell is
721 ignored.
723 See also `same-window-buffer-names'."
724 :type '(repeat (regexp :format "%v"))
725 :group 'windows)
727 (defcustom pop-up-frames nil
728 "Whether `display-buffer' should make a separate frame.
729 If nil, never make a seperate frame.
730 If the value is `graphic-only', make a separate frame
731 on graphic displays only.
732 Any other non-nil value means always make a separate frame."
733 :type '(choice
734 (const :tag "Never" nil)
735 (const :tag "On graphic displays only" graphic-only)
736 (const :tag "Always" t))
737 :group 'windows)
739 (defcustom display-buffer-reuse-frames nil
740 "Non-nil means `display-buffer' should reuse frames.
741 If the buffer in question is already displayed in a frame, raise
742 that frame."
743 :type 'boolean
744 :version "21.1"
745 :group 'windows)
747 (defcustom pop-up-windows t
748 "Non-nil means `display-buffer' should make a new window."
749 :type 'boolean
750 :group 'windows)
752 (defcustom split-height-threshold 80
753 "Minimum height of window to be split vertically.
754 If the value is a number, `display-buffer' can split a window
755 only if it has at least as many lines. If the value is nil,
756 `display-buffer' cannot split a window vertically.
758 If the window is the only window on its frame, `display-buffer'
759 can split it regardless of this value."
760 :type '(choice (const nil) (number :tag "lines"))
761 :version "23.1"
762 :group 'windows)
764 (defcustom split-width-threshold 160
765 "Minimum width of window to be split horizontally.
766 If the value is a number, `display-buffer' can split a window
767 only if it has at least as many columns. If the value is nil,
768 `display-buffer' cannot split a window horizontally."
769 :type '(choice (const nil) (number :tag "columns"))
770 :version "23.1"
771 :group 'windows)
773 (defcustom split-window-preferred-function nil
774 "Function used by `display-buffer' to split windows.
775 If non-nil, a function called with a window as single argument
776 supposed to split that window and return the new window. If the
777 function returns nil the window is not split.
779 If nil, `display-buffer' will split the window respecting the
780 values of `split-height-threshold' and `split-width-threshold'."
781 :type '(choice (const nil) (function :tag "Function"))
782 :version "23.1"
783 :group 'windows)
785 (defun window--splittable-p (window &optional horizontal)
786 "Return non-nil if WINDOW can be split evenly.
787 Optional argument HORIZONTAL non-nil means check whether WINDOW
788 can be split horizontally.
790 WINDOW can be split vertically when the following conditions
791 hold:
793 - `window-size-fixed' is either nil or equals `width' for the
794 buffer of WINDOW.
796 - `split-height-threshold' is a number and WINDOW is at least as
797 high as `split-height-threshold'.
799 - When WINDOW is split evenly, the emanating windows are at least
800 `window-min-height' lines tall and can accommodate at least one
801 line plus - if WINDOW has one - a mode line.
803 WINDOW can be split horizontally when the following conditions
804 hold:
806 - `window-size-fixed' is either nil or equals `height' for the
807 buffer of WINDOW.
809 - `split-width-threshold' is a number and WINDOW is at least as
810 wide as `split-width-threshold'.
812 - When WINDOW is split evenly, the emanating windows are at least
813 `window-min-width' or two (whichever is larger) columns wide."
814 (when (window-live-p window)
815 (with-current-buffer (window-buffer window)
816 (if horizontal
817 ;; A window can be split horizontally when its width is not
818 ;; fixed, it is at least `split-width-threshold' columns wide
819 ;; and at least twice as wide as `window-min-width' and 2 (the
820 ;; latter value is hardcoded).
821 (and (memq window-size-fixed '(nil height))
822 ;; Testing `window-full-width-p' here hardly makes any
823 ;; sense nowadays. This can be done more intuitively by
824 ;; setting up `split-width-threshold' appropriately.
825 (numberp split-width-threshold)
826 (>= (window-width window)
827 (max split-width-threshold
828 (* 2 (max window-min-width 2)))))
829 ;; A window can be split vertically when its height is not
830 ;; fixed, it is at least `split-height-threshold' lines high,
831 ;; and it is at least twice as high as `window-min-height' and 2
832 ;; if it has a modeline or 1.
833 (and (memq window-size-fixed '(nil width))
834 (numberp split-height-threshold)
835 (>= (window-height window)
836 (max split-height-threshold
837 (* 2 (max window-min-height
838 (if mode-line-format 2 1))))))))))
840 (defun window--try-to-split-window (window)
841 "Split WINDOW if it is splittable.
842 See `window--splittable-p' for how to determine whether a window
843 is splittable. If WINDOW can be split, return the value returned
844 by `split-window' (or `split-window-preferred-function')."
845 (when (and (window-live-p window)
846 (not (frame-parameter (window-frame window) 'unsplittable)))
847 (if (functionp split-window-preferred-function)
848 ;; `split-window-preferred-function' is specified, so use it.
849 (funcall split-window-preferred-function window)
850 (or (and (window--splittable-p window)
851 ;; Split window vertically.
852 (split-window window))
853 (and (window--splittable-p window t)
854 ;; Split window horizontally.
855 (split-window window nil t))
856 (and (eq window (frame-root-window (window-frame window)))
857 (not (window-minibuffer-p window))
858 ;; If WINDOW is the only window on its frame and not the
859 ;; minibuffer window, attempt to split it vertically
860 ;; disregarding the value of `split-height-threshold'.
861 (let ((split-height-threshold 0))
862 (and (window--splittable-p window)
863 (split-window window))))))))
865 (defun window--frame-usable-p (frame)
866 "Return FRAME if it can be used to display a buffer."
867 (when (frame-live-p frame)
868 (let ((window (frame-root-window frame)))
869 ;; `frame-root-window' may be an internal window which is considered
870 ;; "dead" by `window-live-p'. Hence if `window' is not live we
871 ;; implicitly know that `frame' has a visible window we can use.
872 (unless (and (window-live-p window)
873 (or (window-minibuffer-p window)
874 ;; If the window is soft-dedicated, the frame is usable.
875 ;; Actually, even if the window is really dedicated,
876 ;; the frame is still usable by splitting it.
877 ;; At least Emacs-22 allowed it, and it is desirable
878 ;; when displaying same-frame windows.
879 nil ; (eq t (window-dedicated-p window))
881 frame))))
883 (defcustom even-window-heights t
884 "If non-nil `display-buffer' will try to even window heights.
885 Otherwise `display-buffer' will leave the window configuration
886 alone. Heights are evened only when `display-buffer' chooses a
887 window that appears above or below the selected window."
888 :type 'boolean
889 :group 'windows)
891 (defun window--even-window-heights (window)
892 "Even heights of WINDOW and selected window.
893 Do this only if these windows are vertically adjacent to each
894 other, `even-window-heights' is non-nil, and the selected window
895 is higher than WINDOW."
896 (when (and even-window-heights
897 (not (eq window (selected-window)))
898 ;; Don't resize minibuffer windows.
899 (not (window-minibuffer-p (selected-window)))
900 (> (window-height (selected-window)) (window-height window))
901 (eq (window-frame window) (window-frame (selected-window)))
902 (let ((sel-edges (window-edges (selected-window)))
903 (win-edges (window-edges window)))
904 (and (= (nth 0 sel-edges) (nth 0 win-edges))
905 (= (nth 2 sel-edges) (nth 2 win-edges))
906 (or (= (nth 1 sel-edges) (nth 3 win-edges))
907 (= (nth 3 sel-edges) (nth 1 win-edges))))))
908 (let ((window-min-height 1))
909 ;; Don't throw an error if we can't even window heights for
910 ;; whatever reason.
911 (condition-case nil
912 (enlarge-window (/ (- (window-height window) (window-height)) 2))
913 (error nil)))))
915 (defun window--display-buffer-1 (window)
916 "Raise the frame containing WINDOW.
917 Do not raise the selected frame. Return WINDOW."
918 (let* ((frame (window-frame window))
919 (visible (frame-visible-p frame)))
920 (unless (or (not visible)
921 ;; Assume the selected frame is already visible enough.
922 (eq frame (selected-frame))
923 ;; Assume the frame from which we invoked the minibuffer
924 ;; is visible.
925 (and (minibuffer-window-active-p (selected-window))
926 (eq frame (window-frame (minibuffer-selected-window)))))
927 (raise-frame frame))
928 window))
930 (defun window--display-buffer-2 (buffer window)
931 "Display BUFFER in WINDOW and make its frame visible.
932 Return WINDOW."
933 (when (and (buffer-live-p buffer) (window-live-p window))
934 (set-window-buffer window buffer)
935 (window--display-buffer-1 window)))
937 (defun display-buffer (buffer-or-name &optional not-this-window frame)
938 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
939 BUFFER-OR-NAME must be a buffer or the name of an existing
940 buffer. Return the window chosen to display BUFFER-OR-NAME or
941 nil if no such window is found.
943 Optional argument NOT-THIS-WINDOW non-nil means display the
944 buffer in a window other than the selected one, even if it is
945 already displayed in the selected window.
947 Optional argument FRAME specifies which frames to investigate
948 when the specified buffer is already displayed. If the buffer is
949 already displayed in some window on one of these frames simply
950 return that window. Possible values of FRAME are:
952 `visible' - consider windows on all visible frames.
954 0 - consider windows on all visible or iconified frames.
956 t - consider windows on all frames.
958 A specific frame - consider windows on that frame only.
960 nil - consider windows on the selected frame \(actually the
961 last non-minibuffer frame\) only. If, however, either
962 `display-buffer-reuse-frames' or `pop-up-frames' is non-nil
963 \(non-nil and not graphic-only on a text-only terminal),
964 consider all visible or iconified frames."
965 (interactive "BDisplay buffer:\nP")
966 (let* ((can-use-selected-window
967 ;; The selected window is usable unless either NOT-THIS-WINDOW
968 ;; is non-nil, it is dedicated to its buffer, or it is the
969 ;; `minibuffer-window'.
970 (not (or not-this-window
971 (window-dedicated-p (selected-window))
972 (window-minibuffer-p))))
973 (buffer (if (bufferp buffer-or-name)
974 buffer-or-name
975 (get-buffer buffer-or-name)))
976 (name-of-buffer (buffer-name buffer))
977 ;; On text-only terminals do not pop up a new frame when
978 ;; `pop-up-frames' equals graphic-only.
979 (use-pop-up-frames (if (eq pop-up-frames 'graphic-only)
980 (display-graphic-p)
981 pop-up-frames))
982 ;; `frame-to-use' is the frame where to show `buffer' - either
983 ;; the selected frame or the last nonminibuffer frame.
984 (frame-to-use
985 (or (window--frame-usable-p (selected-frame))
986 (window--frame-usable-p (last-nonminibuffer-frame))))
987 ;; `window-to-use' is the window we use for showing `buffer'.
988 window-to-use)
989 (cond
990 ((not (buffer-live-p buffer))
991 (error "No such buffer %s" buffer))
992 (display-buffer-function
993 ;; Let `display-buffer-function' do the job.
994 (funcall display-buffer-function buffer not-this-window))
995 ((and (not not-this-window)
996 (eq (window-buffer (selected-window)) buffer))
997 ;; The selected window already displays BUFFER and
998 ;; `not-this-window' is nil, so use it.
999 (window--display-buffer-1 (selected-window)))
1000 ((and can-use-selected-window (same-window-p name-of-buffer))
1001 ;; If the buffer's name tells us to use the selected window do so.
1002 (window--display-buffer-2 buffer (selected-window)))
1003 ((let ((frames (or frame
1004 (and (or use-pop-up-frames
1005 display-buffer-reuse-frames
1006 (not (last-nonminibuffer-frame)))
1008 (last-nonminibuffer-frame))))
1009 (and (setq window-to-use (get-buffer-window buffer frames))
1010 (or can-use-selected-window
1011 (not (eq (selected-window) window-to-use)))))
1012 ;; If the buffer is already displayed in some window use that.
1013 (window--display-buffer-1 window-to-use))
1014 ((and special-display-function
1015 ;; `special-display-p' returns either t or a list of frame
1016 ;; parameters to pass to `special-display-function'.
1017 (let ((pars (special-display-p name-of-buffer)))
1018 (when pars
1019 (funcall special-display-function
1020 buffer (if (listp pars) pars))))))
1021 ((or use-pop-up-frames (not frame-to-use))
1022 ;; We want or need a new frame.
1023 (window--display-buffer-2
1024 buffer (frame-selected-window (funcall pop-up-frame-function))))
1025 ((and pop-up-windows
1026 ;; Make a new window.
1027 (or (not (frame-parameter frame-to-use 'unsplittable))
1028 ;; If the selected frame cannot be split look at
1029 ;; `last-nonminibuffer-frame'.
1030 (and (eq frame-to-use (selected-frame))
1031 (setq frame-to-use (last-nonminibuffer-frame))
1032 (window--frame-usable-p frame-to-use)
1033 (not (frame-parameter frame-to-use 'unsplittable))))
1034 ;; Attempt to split largest or least recently used window.
1035 (setq window-to-use
1036 (or (window--try-to-split-window
1037 (get-largest-window frame-to-use t))
1038 (window--try-to-split-window
1039 (get-lru-window frame-to-use t))))
1040 (window--display-buffer-2 buffer window-to-use)))
1041 ((let ((window-to-undedicate
1042 ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate
1043 ;; the selected window to its buffer, to avoid that some of
1044 ;; the `get-' routines below choose it. (Bug#1415)
1045 (and not-this-window (not (window-dedicated-p))
1046 (set-window-dedicated-p (selected-window) t)
1047 (selected-window))))
1048 (unwind-protect
1049 (setq window-to-use
1050 ;; Reuse an existing window.
1051 (or (get-lru-window frame-to-use)
1052 (let ((window (get-buffer-window buffer 'visible)))
1053 (unless (and not-this-window
1054 (eq window (selected-window)))
1055 window))
1056 (get-largest-window 'visible)
1057 (let ((window (get-buffer-window buffer 0)))
1058 (unless (and not-this-window
1059 (eq window (selected-window)))
1060 window))
1061 (get-largest-window 0)
1062 (frame-selected-window (funcall pop-up-frame-function))))
1063 (when (window-live-p window-to-undedicate)
1064 ;; Restore dedicated status of selected window.
1065 (set-window-dedicated-p window-to-undedicate nil))))
1066 (window--even-window-heights window-to-use)
1067 (window--display-buffer-2 buffer window-to-use)))))
1069 (defun pop-to-buffer (buffer-or-name &optional other-window norecord)
1070 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
1071 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1072 nil. If BUFFER-OR-NAME is a string not naming an existent
1073 buffer, create a buffer with that name. If BUFFER-OR-NAME is
1074 nil, choose some other buffer.
1076 If `pop-up-windows' is non-nil, windows can be split to display
1077 the buffer. If optional second arg OTHER-WINDOW is non-nil,
1078 insist on finding another window even if the specified buffer is
1079 already visible in the selected window, and ignore
1080 `same-window-regexps' and `same-window-buffer-names'.
1082 If the window to show BUFFER-OR-NAME is not on the selected
1083 frame, raise that window's frame and give it input focus.
1085 This function returns the buffer it switched to. This uses the
1086 function `display-buffer' as a subroutine; see the documentation
1087 of `display-buffer' for additional customization information.
1089 Optional third arg NORECORD non-nil means do not put this buffer
1090 at the front of the list of recently selected ones."
1091 (let ((buffer
1092 ;; FIXME: This behavior is carried over from the previous C version
1093 ;; of pop-to-buffer, but really we should use just
1094 ;; `get-buffer' here.
1095 (if (null buffer-or-name) (other-buffer (current-buffer))
1096 (or (get-buffer buffer-or-name)
1097 (let ((buf (get-buffer-create buffer-or-name)))
1098 (set-buffer-major-mode buf)
1099 buf))))
1100 (old-window (selected-window))
1101 (old-frame (selected-frame))
1102 new-window new-frame)
1103 (set-buffer buffer)
1104 (setq new-window (display-buffer buffer other-window))
1105 (unless (eq new-window old-window)
1106 ;; `display-buffer' has chosen another window, select it.
1107 (select-window new-window norecord)
1108 (setq new-frame (window-frame new-window))
1109 (unless (eq new-frame old-frame)
1110 ;; `display-buffer' has chosen another frame, make sure it gets
1111 ;; input focus and is risen.
1112 (select-frame-set-input-focus new-frame)))
1113 buffer))
1115 ;; I think this should be the default; I think people will prefer it--rms.
1116 (defcustom split-window-keep-point t
1117 "If non-nil, \\[split-window-vertically] keeps the original point \
1118 in both children.
1119 This is often more convenient for editing.
1120 If nil, adjust point in each of the two windows to minimize redisplay.
1121 This is convenient on slow terminals, but point can move strangely.
1123 This option applies only to `split-window-vertically' and
1124 functions that call it. `split-window' always keeps the original
1125 point in both children."
1126 :type 'boolean
1127 :group 'windows)
1129 (defun split-window-vertically (&optional size)
1130 "Split selected window into two windows, one above the other.
1131 The upper window gets SIZE lines and the lower one gets the rest.
1132 SIZE negative means the lower window gets -SIZE lines and the
1133 upper one the rest. With no argument, split windows equally or
1134 close to it. Both windows display the same buffer, now current.
1136 If the variable `split-window-keep-point' is non-nil, both new
1137 windows will get the same value of point as the selected window.
1138 This is often more convenient for editing. The upper window is
1139 the selected window.
1141 Otherwise, we choose window starts so as to minimize the amount of
1142 redisplay; this is convenient on slow terminals. The new selected
1143 window is the one that the current value of point appears in. The
1144 value of point can change if the text around point is hidden by the
1145 new mode line.
1147 Regardless of the value of `split-window-keep-point', the upper
1148 window is the original one and the return value is the new, lower
1149 window."
1150 (interactive "P")
1151 (let ((old-window (selected-window))
1152 (old-point (point))
1153 (size (and size (prefix-numeric-value size)))
1154 moved-by-window-height moved new-window bottom)
1155 (and size (< size 0)
1156 ;; Handle negative SIZE value.
1157 (setq size (+ (window-height) size)))
1158 (setq new-window (split-window nil size))
1159 (unless split-window-keep-point
1160 (save-excursion
1161 (set-buffer (window-buffer))
1162 (goto-char (window-start))
1163 (setq moved (vertical-motion (window-height)))
1164 (set-window-start new-window (point))
1165 (when (> (point) (window-point new-window))
1166 (set-window-point new-window (point)))
1167 (when (= moved (window-height))
1168 (setq moved-by-window-height t)
1169 (vertical-motion -1))
1170 (setq bottom (point)))
1171 (and moved-by-window-height
1172 (<= bottom (point))
1173 (set-window-point old-window (1- bottom)))
1174 (and moved-by-window-height
1175 (<= (window-start new-window) old-point)
1176 (set-window-point new-window old-point)
1177 (select-window new-window)))
1178 (split-window-save-restore-data new-window old-window)))
1180 ;; This is to avoid compiler warnings.
1181 (defvar view-return-to-alist)
1183 (defun split-window-save-restore-data (new-window old-window)
1184 (with-current-buffer (window-buffer)
1185 (when view-mode
1186 (let ((old-info (assq old-window view-return-to-alist)))
1187 (when old-info
1188 (push (cons new-window (cons (car (cdr old-info)) t))
1189 view-return-to-alist))))
1190 new-window))
1192 (defun split-window-horizontally (&optional size)
1193 "Split selected window into two windows side by side.
1194 The selected window becomes the left one and gets SIZE columns.
1195 SIZE negative means the right window gets -SIZE lines.
1197 SIZE includes the width of the window's scroll bar; if there are
1198 no scroll bars, it includes the width of the divider column to
1199 the window's right, if any. SIZE omitted or nil means split
1200 window equally.
1202 The selected window remains selected. Return the new window."
1203 (interactive "P")
1204 (let ((old-window (selected-window))
1205 (size (and size (prefix-numeric-value size))))
1206 (and size (< size 0)
1207 ;; Handle negative SIZE value.
1208 (setq size (+ (window-width) size)))
1209 (split-window-save-restore-data (split-window nil size t) old-window)))
1212 (defun set-window-text-height (window height)
1213 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
1214 HEIGHT doesn't include the mode line or header line, if any, or
1215 any partial-height lines in the text display area.
1217 Note that the current implementation of this function cannot
1218 always set the height exactly, but attempts to be conservative,
1219 by allocating more lines than are actually needed in the case
1220 where some error may be present."
1221 (let ((delta (- height (window-text-height window))))
1222 (unless (zerop delta)
1223 ;; Setting window-min-height to a value like 1 can lead to very
1224 ;; bizarre displays because it also allows Emacs to make *other*
1225 ;; windows 1-line tall, which means that there's no more space for
1226 ;; the modeline.
1227 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
1228 (if (and window (not (eq window (selected-window))))
1229 (save-selected-window
1230 (select-window window 'norecord)
1231 (enlarge-window delta))
1232 (enlarge-window delta))))))
1235 (defun enlarge-window-horizontally (columns)
1236 "Make selected window COLUMNS wider.
1237 Interactively, if no argument is given, make selected window one
1238 column wider."
1239 (interactive "p")
1240 (enlarge-window columns t))
1242 (defun shrink-window-horizontally (columns)
1243 "Make selected window COLUMNS narrower.
1244 Interactively, if no argument is given, make selected window one
1245 column narrower."
1246 (interactive "p")
1247 (shrink-window columns t))
1249 (defun window-buffer-height (window)
1250 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
1251 (with-current-buffer (window-buffer window)
1252 (max 1
1253 (count-screen-lines (point-min) (point-max)
1254 ;; If buffer ends with a newline, ignore it when
1255 ;; counting height unless point is after it.
1256 (eobp)
1257 window))))
1259 (defun count-screen-lines (&optional beg end count-final-newline window)
1260 "Return the number of screen lines in the region.
1261 The number of screen lines may be different from the number of actual lines,
1262 due to line breaking, display table, etc.
1264 Optional arguments BEG and END default to `point-min' and `point-max'
1265 respectively.
1267 If region ends with a newline, ignore it unless optional third argument
1268 COUNT-FINAL-NEWLINE is non-nil.
1270 The optional fourth argument WINDOW specifies the window used for obtaining
1271 parameters such as width, horizontal scrolling, and so on. The default is
1272 to use the selected window's parameters.
1274 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
1275 regardless of which buffer is displayed in WINDOW. This makes possible to use
1276 `count-screen-lines' in any buffer, whether or not it is currently displayed
1277 in some window."
1278 (unless beg
1279 (setq beg (point-min)))
1280 (unless end
1281 (setq end (point-max)))
1282 (if (= beg end)
1284 (save-excursion
1285 (save-restriction
1286 (widen)
1287 (narrow-to-region (min beg end)
1288 (if (and (not count-final-newline)
1289 (= ?\n (char-before (max beg end))))
1290 (1- (max beg end))
1291 (max beg end)))
1292 (goto-char (point-min))
1293 (1+ (vertical-motion (buffer-size) window))))))
1295 (defun fit-window-to-buffer (&optional window max-height min-height)
1296 "Adjust height of WINDOW to display its buffer's contents exactly.
1297 WINDOW defaults to the selected window.
1298 Optional argument MAX-HEIGHT specifies the maximum height of the
1299 window and defaults to the maximum permissible height of a window
1300 on WINDOW's frame.
1301 Optional argument MIN-HEIGHT specifies the minimum height of the
1302 window and defaults to `window-min-height'.
1303 Both, MAX-HEIGHT and MIN-HEIGHT are specified in lines and
1304 include the mode line and header line, if any.
1306 Return non-nil if height was orderly adjusted, nil otherwise.
1308 Caution: This function can delete WINDOW and/or other windows
1309 when their height shrinks to less than MIN-HEIGHT."
1310 (interactive)
1311 ;; Do all the work in WINDOW and its buffer and restore the selected
1312 ;; window and the current buffer when we're done.
1313 (let ((old-buffer (current-buffer))
1314 value)
1315 (with-selected-window (or window (setq window (selected-window)))
1316 (set-buffer (window-buffer))
1317 ;; Use `condition-case' to handle any fixed-size windows and other
1318 ;; pitfalls nearby.
1319 (condition-case nil
1320 (let* (;; MIN-HEIGHT must not be less than 1 and defaults to
1321 ;; `window-min-height'.
1322 (min-height (max (or min-height window-min-height) 1))
1323 (max-window-height
1324 ;; Maximum height of any window on this frame.
1325 (min (window-height (frame-root-window)) (frame-height)))
1326 ;; MAX-HEIGHT must not be larger than max-window-height and
1327 ;; defaults to max-window-height.
1328 (max-height
1329 (min (or max-height max-window-height) max-window-height))
1330 (desired-height
1331 ;; The height necessary to show all of WINDOW's buffer,
1332 ;; constrained by MIN-HEIGHT and MAX-HEIGHT.
1333 (max
1334 (min
1335 ;; For an empty buffer `count-screen-lines' returns zero.
1336 ;; Even in that case we need one line for the cursor.
1337 (+ (max (count-screen-lines) 1)
1338 ;; For non-minibuffers count the mode line, if any.
1339 (if (and (not (window-minibuffer-p)) mode-line-format)
1340 1 0)
1341 ;; Count the header line, if any.
1342 (if header-line-format 1 0))
1343 max-height)
1344 min-height))
1345 (delta
1346 ;; How much the window height has to change.
1347 (if (= (window-height) (window-height (frame-root-window)))
1348 ;; Don't try to resize a full-height window.
1350 (- desired-height (window-height))))
1351 ;; Do something reasonable so `enlarge-window' can make
1352 ;; windows as small as MIN-HEIGHT.
1353 (window-min-height (min min-height window-min-height)))
1354 ;; Don't try to redisplay with the cursor at the end on its
1355 ;; own line--that would force a scroll and spoil things.
1356 (when (and (eobp) (bolp) (not (bobp)))
1357 (set-window-point window (1- (window-point))))
1358 ;; Adjust WINDOW's height to the nominally correct one
1359 ;; (which may actually be slightly off because of variable
1360 ;; height text, etc).
1361 (unless (zerop delta)
1362 (enlarge-window delta))
1363 ;; `enlarge-window' might have deleted WINDOW, so make sure
1364 ;; WINDOW's still alive for the remainder of this.
1365 ;; Note: Deleting WINDOW is clearly counter-intuitive in
1366 ;; this context, but we can't do much about it given the
1367 ;; current semantics of `enlarge-window'.
1368 (when (window-live-p window)
1369 ;; Check if the last line is surely fully visible. If
1370 ;; not, enlarge the window.
1371 (let ((end (save-excursion
1372 (goto-char (point-max))
1373 (when (and (bolp) (not (bobp)))
1374 ;; Don't include final newline.
1375 (backward-char 1))
1376 (when truncate-lines
1377 ;; If line-wrapping is turned off, test the
1378 ;; beginning of the last line for
1379 ;; visibility instead of the end, as the
1380 ;; end of the line could be invisible by
1381 ;; virtue of extending past the edge of the
1382 ;; window.
1383 (forward-line 0))
1384 (point))))
1385 (set-window-vscroll window 0)
1386 (while (and (< desired-height max-height)
1387 (= desired-height (window-height))
1388 (not (pos-visible-in-window-p end)))
1389 (enlarge-window 1)
1390 (setq desired-height (1+ desired-height))))
1391 ;; Return non-nil only if nothing "bad" happened.
1392 (setq value t)))
1393 (error nil)))
1394 (when (buffer-live-p old-buffer)
1395 (set-buffer old-buffer))
1396 value))
1398 (defun window-safely-shrinkable-p (&optional window)
1399 "Return t if WINDOW can be shrunk without shrinking other windows.
1400 WINDOW defaults to the selected window."
1401 (with-selected-window (or window (selected-window))
1402 (let ((edges (window-edges)))
1403 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
1404 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
1406 (defun shrink-window-if-larger-than-buffer (&optional window)
1407 "Shrink height of WINDOW if its buffer doesn't need so many lines.
1408 More precisely, shrink WINDOW vertically to be as small as
1409 possible, while still showing the full contents of its buffer.
1410 WINDOW defaults to the selected window.
1412 Do not shrink to less than `window-min-height' lines. Do nothing
1413 if the buffer contains more lines than the present window height,
1414 or if some of the window's contents are scrolled out of view, or
1415 if shrinking this window would also shrink another window, or if
1416 the window is the only window of its frame.
1418 Return non-nil if the window was shrunk, nil otherwise."
1419 (interactive)
1420 (when (null window)
1421 (setq window (selected-window)))
1422 (let* ((frame (window-frame window))
1423 (mini (frame-parameter frame 'minibuffer))
1424 (edges (window-edges window)))
1425 (if (and (not (eq window (frame-root-window frame)))
1426 (window-safely-shrinkable-p window)
1427 (pos-visible-in-window-p (point-min) window)
1428 (not (eq mini 'only))
1429 (or (not mini)
1430 (let ((mini-window (minibuffer-window frame)))
1431 (or (null mini-window)
1432 (not (eq frame (window-frame mini-window)))
1433 (< (nth 3 edges)
1434 (nth 1 (window-edges mini-window)))
1435 (> (nth 1 edges)
1436 (frame-parameter frame 'menu-bar-lines))))))
1437 (fit-window-to-buffer window (window-height window)))))
1439 (defun kill-buffer-and-window ()
1440 "Kill the current buffer and delete the selected window."
1441 (interactive)
1442 (let ((window-to-delete (selected-window))
1443 (buffer-to-kill (current-buffer))
1444 (delete-window-hook (lambda ()
1445 (condition-case nil
1446 (delete-window)
1447 (error nil)))))
1448 (unwind-protect
1449 (progn
1450 (add-hook 'kill-buffer-hook delete-window-hook t t)
1451 (if (kill-buffer (current-buffer))
1452 ;; If `delete-window' failed before, we rerun it to regenerate
1453 ;; the error so it can be seen in the echo area.
1454 (when (eq (selected-window) window-to-delete)
1455 (delete-window))))
1456 ;; If the buffer is not dead for some reason (probably because
1457 ;; of a `quit' signal), remove the hook again.
1458 (condition-case nil
1459 (with-current-buffer buffer-to-kill
1460 (remove-hook 'kill-buffer-hook delete-window-hook t))
1461 (error nil)))))
1463 (defun quit-window (&optional kill window)
1464 "Quit WINDOW and bury its buffer.
1465 With a prefix argument, kill the buffer instead. WINDOW defaults
1466 to the selected window.
1468 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
1469 it and, if it's alone on its frame, its frame too. Otherwise, or
1470 if deleting WINDOW fails in any of the preceding cases, display
1471 another buffer in WINDOW using `switch-to-buffer'.
1473 Optional argument KILL non-nil means kill WINDOW's buffer.
1474 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
1475 (interactive "P")
1476 (let ((buffer (window-buffer window)))
1477 (if (or window
1478 (window-minibuffer-p window)
1479 (window-dedicated-p window))
1480 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
1481 ;; try to delete it.
1482 (let* ((window (or window (selected-window)))
1483 (frame (window-frame window)))
1484 (if (eq window (frame-root-window frame))
1485 ;; WINDOW is alone on its frame. `delete-windows-on'
1486 ;; knows how to handle that case.
1487 (delete-windows-on buffer frame)
1488 ;; There are other windows on its frame, delete WINDOW.
1489 (delete-window window)))
1490 ;; Otherwise, switch to another buffer in the selected window.
1491 (switch-to-buffer nil))
1493 ;; Deal with the buffer.
1494 (if kill
1495 (kill-buffer buffer)
1496 (bury-buffer buffer))))
1498 (defvar recenter-last-op nil
1499 "Indicates the last recenter operation performed.
1500 Possible values: `top', `middle', `bottom'.")
1502 (defun recenter-top-bottom (&optional arg)
1503 "Move current line to window center, top, and bottom, successively.
1504 With no prefix argument, the first call redraws the frame and
1505 centers point vertically within the window. Successive calls
1506 scroll the window, placing point on the top, bottom, and middle
1507 consecutively. The cycling order is middle -> top -> bottom.
1509 A prefix argument is handled like `recenter':
1510 With numeric prefix ARG, move current line to window-line ARG.
1511 With plain `C-u', move current line to window center.
1513 Top and bottom destinations are actually `scroll-margin' lines
1514 the from true window top and bottom."
1515 (interactive "P")
1516 (cond
1517 (arg (recenter arg)) ; Always respect ARG.
1518 ((or (not (eq this-command last-command))
1519 (eq recenter-last-op 'bottom))
1520 (setq recenter-last-op 'middle)
1521 (recenter))
1523 (let ((this-scroll-margin
1524 (min (max 0 scroll-margin)
1525 (truncate (/ (window-body-height) 4.0)))))
1526 (cond ((eq recenter-last-op 'middle)
1527 (setq recenter-last-op 'top)
1528 (recenter this-scroll-margin))
1529 ((eq recenter-last-op 'top)
1530 (setq recenter-last-op 'bottom)
1531 (recenter (- -1 this-scroll-margin))))))))
1533 (define-key global-map [?\C-l] 'recenter-top-bottom)
1535 (defvar mouse-autoselect-window-timer nil
1536 "Timer used by delayed window autoselection.")
1538 (defvar mouse-autoselect-window-position nil
1539 "Last mouse position recorded by delayed window autoselection.")
1541 (defvar mouse-autoselect-window-window nil
1542 "Last window recorded by delayed window autoselection.")
1544 (defvar mouse-autoselect-window-state nil
1545 "When non-nil, special state of delayed window autoselection.
1546 Possible values are `suspend' \(suspend autoselection after a menu or
1547 scrollbar interaction\) and `select' \(the next invocation of
1548 'handle-select-window' shall select the window immediately\).")
1550 (defun mouse-autoselect-window-cancel (&optional force)
1551 "Cancel delayed window autoselection.
1552 Optional argument FORCE means cancel unconditionally."
1553 (unless (and (not force)
1554 ;; Don't cancel for select-window or select-frame events
1555 ;; or when the user drags a scroll bar.
1556 (or (memq this-command
1557 '(handle-select-window handle-switch-frame))
1558 (and (eq this-command 'scroll-bar-toolkit-scroll)
1559 (memq (nth 4 (event-end last-input-event))
1560 '(handle end-scroll)))))
1561 (setq mouse-autoselect-window-state nil)
1562 (when (timerp mouse-autoselect-window-timer)
1563 (cancel-timer mouse-autoselect-window-timer))
1564 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
1566 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
1567 "Start delayed window autoselection.
1568 MOUSE-POSITION is the last position where the mouse was seen as returned
1569 by `mouse-position'. Optional argument WINDOW non-nil denotes the
1570 window where the mouse was seen. Optional argument SUSPEND non-nil
1571 means suspend autoselection."
1572 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
1573 (setq mouse-autoselect-window-position mouse-position)
1574 (when window (setq mouse-autoselect-window-window window))
1575 (setq mouse-autoselect-window-state (when suspend 'suspend))
1576 ;; Install timer which runs `mouse-autoselect-window-select' after
1577 ;; `mouse-autoselect-window' seconds.
1578 (setq mouse-autoselect-window-timer
1579 (run-at-time
1580 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
1582 (defun mouse-autoselect-window-select ()
1583 "Select window with delayed window autoselection.
1584 If the mouse position has stabilized in a non-selected window, select
1585 that window. The minibuffer window is selected only if the minibuffer is
1586 active. This function is run by `mouse-autoselect-window-timer'."
1587 (condition-case nil
1588 (let* ((mouse-position (mouse-position))
1589 (window
1590 (condition-case nil
1591 (window-at (cadr mouse-position) (cddr mouse-position)
1592 (car mouse-position))
1593 (error nil))))
1594 (cond
1595 ((or (menu-or-popup-active-p)
1596 (and window
1597 (not (coordinates-in-window-p (cdr mouse-position) window))))
1598 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
1599 ;; of WINDOW, temporarily suspend delayed autoselection.
1600 (mouse-autoselect-window-start mouse-position nil t))
1601 ((eq mouse-autoselect-window-state 'suspend)
1602 ;; Delayed autoselection was temporarily suspended, reenable it.
1603 (mouse-autoselect-window-start mouse-position))
1604 ((and window (not (eq window (selected-window)))
1605 (or (not (numberp mouse-autoselect-window))
1606 (and (> mouse-autoselect-window 0)
1607 ;; If `mouse-autoselect-window' is positive, select
1608 ;; window if the window is the same as before.
1609 (eq window mouse-autoselect-window-window))
1610 ;; Otherwise select window if the mouse is at the same
1611 ;; position as before. Observe that the first test after
1612 ;; starting autoselection usually fails since the value of
1613 ;; `mouse-autoselect-window-position' recorded there is the
1614 ;; position where the mouse has entered the new window and
1615 ;; not necessarily where the mouse has stopped moving.
1616 (equal mouse-position mouse-autoselect-window-position))
1617 ;; The minibuffer is a candidate window if it's active.
1618 (or (not (window-minibuffer-p window))
1619 (eq window (active-minibuffer-window))))
1620 ;; Mouse position has stabilized in non-selected window: Cancel
1621 ;; delayed autoselection and try to select that window.
1622 (mouse-autoselect-window-cancel t)
1623 ;; Select window where mouse appears unless the selected window is the
1624 ;; minibuffer. Use `unread-command-events' in order to execute pre-
1625 ;; and post-command hooks and trigger idle timers. To avoid delaying
1626 ;; autoselection again, set `mouse-autoselect-window-state'."
1627 (unless (window-minibuffer-p (selected-window))
1628 (setq mouse-autoselect-window-state 'select)
1629 (setq unread-command-events
1630 (cons (list 'select-window (list window))
1631 unread-command-events))))
1632 ((or (and window (eq window (selected-window)))
1633 (not (numberp mouse-autoselect-window))
1634 (equal mouse-position mouse-autoselect-window-position))
1635 ;; Mouse position has either stabilized in the selected window or at
1636 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
1637 (mouse-autoselect-window-cancel t))
1639 ;; Mouse position has not stabilized yet, resume delayed
1640 ;; autoselection.
1641 (mouse-autoselect-window-start mouse-position window))))
1642 (error nil)))
1644 (defun handle-select-window (event)
1645 "Handle select-window events."
1646 (interactive "e")
1647 (let ((window (posn-window (event-start event))))
1648 (unless (or (not (window-live-p window))
1649 ;; Don't switch if we're currently in the minibuffer.
1650 ;; This tries to work around problems where the
1651 ;; minibuffer gets unselected unexpectedly, and where
1652 ;; you then have to move your mouse all the way down to
1653 ;; the minibuffer to select it.
1654 (window-minibuffer-p (selected-window))
1655 ;; Don't switch to minibuffer window unless it's active.
1656 (and (window-minibuffer-p window)
1657 (not (minibuffer-window-active-p window)))
1658 ;; Don't switch when autoselection shall be delayed.
1659 (and (numberp mouse-autoselect-window)
1660 (not (zerop mouse-autoselect-window))
1661 (not (eq mouse-autoselect-window-state 'select))
1662 (progn
1663 ;; Cancel any delayed autoselection.
1664 (mouse-autoselect-window-cancel t)
1665 ;; Start delayed autoselection from current mouse
1666 ;; position and window.
1667 (mouse-autoselect-window-start (mouse-position) window)
1668 ;; Executing a command cancels delayed autoselection.
1669 (add-hook
1670 'pre-command-hook 'mouse-autoselect-window-cancel))))
1671 (when mouse-autoselect-window
1672 ;; Reset state of delayed autoselection.
1673 (setq mouse-autoselect-window-state nil)
1674 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
1675 (run-hooks 'mouse-leave-buffer-hook))
1676 (select-window window))))
1678 (defun delete-other-windows-vertically (&optional window)
1679 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
1680 This may be a useful alternative binding for \\[delete-other-windows]
1681 if you often split windows horizontally."
1682 (interactive)
1683 (let* ((window (or window (selected-window)))
1684 (edges (window-edges window))
1685 (w window) delenda)
1686 (while (not (eq (setq w (next-window w 1)) window))
1687 (let ((e (window-edges w)))
1688 (when (and (= (car e) (car edges))
1689 (= (caddr e) (caddr edges)))
1690 (push w delenda))))
1691 (mapc 'delete-window delenda)))
1693 (defun truncated-partial-width-window-p (&optional window)
1694 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
1695 WINDOW defaults to the selected window.
1696 Return nil if WINDOW is not a partial-width window
1697 (regardless of the value of `truncate-lines').
1698 Otherwise, consult the value of `truncate-partial-width-windows'
1699 for the buffer shown in WINDOW."
1700 (unless window
1701 (setq window (selected-window)))
1702 (unless (window-full-width-p window)
1703 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
1704 (window-buffer window))))
1705 (if (integerp t-p-w-w)
1706 (< (window-width window) t-p-w-w)
1707 t-p-w-w))))
1709 (define-key ctl-x-map "2" 'split-window-vertically)
1710 (define-key ctl-x-map "3" 'split-window-horizontally)
1711 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
1712 (define-key ctl-x-map "{" 'shrink-window-horizontally)
1713 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
1714 (define-key ctl-x-map "+" 'balance-windows)
1715 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
1717 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
1718 ;;; window.el ends here