(recenter-top-bottom): Doc fix.
[bpt/emacs.git] / lisp / window.el
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
2
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;; Window tree functions.
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
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)
40
41 (defmacro save-selected-window (&rest body)
42 "Execute BODY, then select the window that was selected before BODY.
43 The value returned is the value of the last form in BODY.
44
45 This macro saves and restores the current buffer, since otherwise
46 its normal operation could potentially make a different
47 buffer current. It does not alter the buffer list ordering.
48
49 This macro saves and restores the selected window, as well as
50 the selected window in each frame. If the previously selected
51 window of some frame is no longer live at the end of BODY, that
52 frame's selected window is left alone. If the selected window is
53 no longer live, then whatever window is selected at the end of
54 BODY remains selected."
55 `(let ((save-selected-window-window (selected-window))
56 ;; It is necessary to save all of these, because calling
57 ;; select-window changes frame-selected-window for whatever
58 ;; frame that window is in.
59 (save-selected-window-alist
60 (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
61 (frame-list))))
62 (save-current-buffer
63 (unwind-protect
64 (progn ,@body)
65 (dolist (elt save-selected-window-alist)
66 (and (frame-live-p (car elt))
67 (window-live-p (cdr elt))
68 (set-frame-selected-window (car elt) (cdr elt))))
69 (if (window-live-p save-selected-window-window)
70 (select-window save-selected-window-window))))))
71
72 (defun window-body-height (&optional window)
73 "Return number of lines in window WINDOW for actual buffer text.
74 This does not include the mode line (if any) or the header line (if any)."
75 (or window (setq window (selected-window)))
76 (if (window-minibuffer-p window)
77 (window-height window)
78 (with-current-buffer (window-buffer window)
79 (max 1 (- (window-height window)
80 (if mode-line-format 1 0)
81 (if header-line-format 1 0))))))
82
83 (defun one-window-p (&optional nomini all-frames)
84 "Return non-nil if the selected window is the only window.
85 Optional arg NOMINI non-nil means don't count the minibuffer
86 even if it is active. Otherwise, the minibuffer is counted
87 when it is active.
88
89 The optional arg ALL-FRAMES t means count windows on all frames.
90 If it is `visible', count windows on all visible frames.
91 ALL-FRAMES nil or omitted means count only the selected frame,
92 plus the minibuffer it uses (which may be on another frame).
93 ALL-FRAMES 0 means count all windows in all visible or iconified frames.
94 If ALL-FRAMES is anything else, count only the selected frame."
95 (let ((base-window (selected-window)))
96 (if (and nomini (eq base-window (minibuffer-window)))
97 (setq base-window (next-window base-window)))
98 (eq base-window
99 (next-window base-window (if nomini 'arg) all-frames))))
100
101 (defun window-current-scroll-bars (&optional window)
102 "Return the current scroll-bar settings in window WINDOW.
103 Value is a cons (VERTICAL . HORIZONTAL) where VERTICAL specifies the
104 current location of the vertical scroll-bars (left, right, or nil),
105 and HORIZONTAL specifies the current location of the horizontal scroll
106 bars (top, bottom, or nil)."
107 (let ((vert (nth 2 (window-scroll-bars window)))
108 (hor nil))
109 (when (or (eq vert t) (eq hor t))
110 (let ((fcsb (frame-current-scroll-bars
111 (window-frame (or window (selected-window))))))
112 (if (eq vert t)
113 (setq vert (car fcsb)))
114 (if (eq hor t)
115 (setq hor (cdr fcsb)))))
116 (cons vert hor)))
117
118 (defun walk-windows (proc &optional minibuf all-frames)
119 "Cycle through all visible windows, calling PROC for each one.
120 PROC is called with a window as argument.
121
122 Optional second arg MINIBUF t means count the minibuffer window even
123 if not active. MINIBUF nil or omitted means count the minibuffer only if
124 it is active. MINIBUF neither t nor nil means not to count the
125 minibuffer even if it is active.
126
127 Several frames may share a single minibuffer; if the minibuffer
128 counts, all windows on all frames that share that minibuffer count
129 too. Therefore, if you are using a separate minibuffer frame
130 and the minibuffer is active and MINIBUF says it counts,
131 `walk-windows' includes the windows in the frame from which you
132 entered the minibuffer, as well as the minibuffer window.
133
134 ALL-FRAMES is the optional third argument.
135 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
136 ALL-FRAMES = `visible' means include windows on all visible frames.
137 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
138 ALL-FRAMES = t means include windows on all frames including invisible frames.
139 If ALL-FRAMES is a frame, it means include windows on that frame.
140 Anything else means restrict to the selected frame."
141 ;; If we start from the minibuffer window, don't fail to come back to it.
142 (if (window-minibuffer-p (selected-window))
143 (setq minibuf t))
144 (save-selected-window
145 (if (framep all-frames)
146 (select-window (frame-first-window all-frames)))
147 (let* (walk-windows-already-seen
148 (walk-windows-current (selected-window)))
149 (while (progn
150 (setq walk-windows-current
151 (next-window walk-windows-current minibuf all-frames))
152 (not (memq walk-windows-current walk-windows-already-seen)))
153 (setq walk-windows-already-seen
154 (cons walk-windows-current walk-windows-already-seen))
155 (funcall proc walk-windows-current)))))
156
157 (defun get-window-with-predicate (predicate &optional minibuf
158 all-frames default)
159 "Return a window satisfying PREDICATE.
160
161 This function cycles through all visible windows using `walk-windows',
162 calling PREDICATE on each one. PREDICATE is called with a window as
163 argument. The first window for which PREDICATE returns a non-nil
164 value is returned. If no window satisfies PREDICATE, DEFAULT is
165 returned.
166
167 Optional second arg MINIBUF t means count the minibuffer window even
168 if not active. MINIBUF nil or omitted means count the minibuffer only if
169 it is active. MINIBUF neither t nor nil means not to count the
170 minibuffer even if it is active.
171
172 Several frames may share a single minibuffer; if the minibuffer
173 counts, all windows on all frames that share that minibuffer count
174 too. Therefore, if you are using a separate minibuffer frame
175 and the minibuffer is active and MINIBUF says it counts,
176 `walk-windows' includes the windows in the frame from which you
177 entered the minibuffer, as well as the minibuffer window.
178
179 ALL-FRAMES is the optional third argument.
180 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
181 ALL-FRAMES = `visible' means include windows on all visible frames.
182 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
183 ALL-FRAMES = t means include windows on all frames including invisible frames.
184 If ALL-FRAMES is a frame, it means include windows on that frame.
185 Anything else means restrict to the selected frame."
186 (catch 'found
187 (walk-windows #'(lambda (window)
188 (when (funcall predicate window)
189 (throw 'found window)))
190 minibuf all-frames)
191 default))
192
193 (defalias 'some-window 'get-window-with-predicate)
194
195 ;; This should probably be written in C (i.e., without using `walk-windows').
196 (defun get-buffer-window-list (buffer &optional minibuf all-frames)
197 "Return list of all windows displaying BUFFER, or nil if none.
198 BUFFER can be a buffer or a buffer name.
199 See `walk-windows' for the meaning of MINIBUF and ALL-FRAMES."
200 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
201 (walk-windows (function (lambda (window)
202 (if (eq (window-buffer window) buffer)
203 (setq windows (cons window windows)))))
204 minibuf all-frames)
205 windows))
206
207 (defun minibuffer-window-active-p (window)
208 "Return t if WINDOW (a minibuffer window) is now active."
209 (eq window (active-minibuffer-window)))
210 \f
211 (defun count-windows (&optional minibuf)
212 "Return the number of visible windows.
213 This counts the windows in the selected frame and (if the minibuffer is
214 to be counted) its minibuffer frame (if that's not the same frame).
215 The optional arg MINIBUF non-nil means count the minibuffer
216 even if it is inactive."
217 (let ((count 0))
218 (walk-windows (lambda (w) (setq count (+ count 1)))
219 minibuf)
220 count))
221
222 (defun window-safely-shrinkable-p (&optional window)
223 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
224 If WINDOW is nil or omitted, it defaults to the currently selected window."
225 (with-selected-window (or window (selected-window))
226 (let ((edges (window-edges)))
227 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
228 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
229
230 \f
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;;; `balance-windows' subroutines using `window-tree'
233
234 ;;; Translate from internal window tree format
235
236 (defun bw-get-tree (&optional window-or-frame)
237 "Get a window split tree in our format.
238
239 WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil,
240 then the whole window split tree for `selected-frame' is returned.
241 If it is a frame, then this is used instead. If it is a window,
242 then the smallest tree containing that window is returned."
243 (when window-or-frame
244 (unless (or (framep window-or-frame)
245 (windowp window-or-frame))
246 (error "Not a frame or window: %s" window-or-frame)))
247 (let ((subtree (bw-find-tree-sub window-or-frame)))
248 (when subtree
249 (if (integerp subtree)
250 nil
251 (bw-get-tree-1 subtree)))))
252
253 (defun bw-get-tree-1 (split)
254 (if (windowp split)
255 split
256 (let ((dir (car split))
257 (edges (car (cdr split)))
258 (childs (cdr (cdr split))))
259 (list
260 (cons 'dir (if dir 'ver 'hor))
261 (cons 'b (nth 3 edges))
262 (cons 'r (nth 2 edges))
263 (cons 't (nth 1 edges))
264 (cons 'l (nth 0 edges))
265 (cons 'childs (mapcar #'bw-get-tree-1 childs))))))
266
267 (defun bw-find-tree-sub (window-or-frame &optional get-parent)
268 (let* ((window (when (windowp window-or-frame) window-or-frame))
269 (frame (when (windowp window) (window-frame window)))
270 (wt (car (window-tree frame))))
271 (when (< 1 (length (window-list frame 0)))
272 (if window
273 (bw-find-tree-sub-1 wt window get-parent)
274 wt))))
275
276 (defun bw-find-tree-sub-1 (tree win &optional get-parent)
277 (unless (windowp win) (error "Not a window: %s" win))
278 (if (memq win tree)
279 (if get-parent
280 get-parent
281 tree)
282 (let ((childs (cdr (cdr tree)))
283 child
284 subtree)
285 (while (and childs (not subtree))
286 (setq child (car childs))
287 (setq childs (cdr childs))
288 (when (and child (listp child))
289 (setq subtree (bw-find-tree-sub-1 child win get-parent))))
290 (if (integerp subtree)
291 (progn
292 (if (= 1 subtree)
293 tree
294 (1- subtree)))
295 subtree
296 ))))
297
298 ;;; Window or object edges
299
300 (defun bw-l (obj)
301 "Left edge of OBJ."
302 (if (windowp obj) (nth 0 (window-edges obj)) (cdr (assq 'l obj))))
303 (defun bw-t (obj)
304 "Top edge of OBJ."
305 (if (windowp obj) (nth 1 (window-edges obj)) (cdr (assq 't obj))))
306 (defun bw-r (obj)
307 "Right edge of OBJ."
308 (if (windowp obj) (nth 2 (window-edges obj)) (cdr (assq 'r obj))))
309 (defun bw-b (obj)
310 "Bottom edge of OBJ."
311 (if (windowp obj) (nth 3 (window-edges obj)) (cdr (assq 'b obj))))
312
313 ;;; Split directions
314
315 (defun bw-dir (obj)
316 "Return window split tree direction if OBJ.
317 If OBJ is a window return 'both. If it is a window split tree
318 then return its direction."
319 (if (symbolp obj)
320 obj
321 (if (windowp obj)
322 'both
323 (let ((dir (cdr (assq 'dir obj))))
324 (unless (memq dir '(hor ver both))
325 (error "Can't find dir in %s" obj))
326 dir))))
327
328 (defun bw-eqdir (obj1 obj2)
329 "Return t if window split tree directions are equal.
330 OBJ1 and OBJ2 should be either windows or window split trees in
331 our format. The directions returned by `bw-dir' are compared and
332 t is returned if they are `eq' or one of them is 'both."
333 (let ((dir1 (bw-dir obj1))
334 (dir2 (bw-dir obj2)))
335 (or (eq dir1 dir2)
336 (eq dir1 'both)
337 (eq dir2 'both))))
338
339 ;;; Building split tree
340
341 (defun bw-refresh-edges (obj)
342 "Refresh the edge information of OBJ and return OBJ."
343 (unless (windowp obj)
344 (let ((childs (cdr (assq 'childs obj)))
345 (ol 1000)
346 (ot 1000)
347 (or -1)
348 (ob -1))
349 (dolist (o childs)
350 (when (> ol (bw-l o)) (setq ol (bw-l o)))
351 (when (> ot (bw-t o)) (setq ot (bw-t o)))
352 (when (< or (bw-r o)) (setq or (bw-r o)))
353 (when (< ob (bw-b o)) (setq ob (bw-b o))))
354 (setq obj (delq 'l obj))
355 (setq obj (delq 't obj))
356 (setq obj (delq 'r obj))
357 (setq obj (delq 'b obj))
358 (add-to-list 'obj (cons 'l ol))
359 (add-to-list 'obj (cons 't ot))
360 (add-to-list 'obj (cons 'r or))
361 (add-to-list 'obj (cons 'b ob))
362 ))
363 obj)
364
365 ;;; Balance windows
366
367 (defun balance-windows (&optional window-or-frame)
368 "Make windows the same heights or widths in window split subtrees.
369
370 When called non-interactively WINDOW-OR-FRAME may be either a
371 window or a frame. It then balances the windows on the implied
372 frame. If the parameter is a window only the corresponding window
373 subtree is balanced."
374 (interactive)
375 (let (
376 (wt (bw-get-tree window-or-frame))
377 (w)
378 (h)
379 (tried-sizes)
380 (last-sizes)
381 (windows (window-list nil 0)))
382 (when wt
383 (while (not (member last-sizes tried-sizes))
384 (when last-sizes (setq tried-sizes (cons last-sizes tried-sizes)))
385 (setq last-sizes (mapcar (lambda (w)
386 (window-edges w))
387 windows))
388 (when (eq 'hor (bw-dir wt))
389 (setq w (- (bw-r wt) (bw-l wt))))
390 (when (eq 'ver (bw-dir wt))
391 (setq h (- (bw-b wt) (bw-t wt))))
392 (bw-balance-sub wt w h)))))
393
394 (defun bw-adjust-window (window delta horizontal)
395 "Wrapper around `adjust-window-trailing-edge' with error checking.
396 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
397 ;; `adjust-window-trailing-edge' may fail if delta is too large.
398 (while (>= (abs delta) 1)
399 (condition-case err
400 (progn
401 (adjust-window-trailing-edge window delta horizontal)
402 (setq delta 0))
403 (error
404 ;;(message "adjust: %s" (error-message-string err))
405 (setq delta (/ delta 2))))))
406
407 (defun bw-balance-sub (wt w h)
408 (setq wt (bw-refresh-edges wt))
409 (unless w (setq w (- (bw-r wt) (bw-l wt))))
410 (unless h (setq h (- (bw-b wt) (bw-t wt))))
411 (if (windowp wt)
412 (progn
413 (when w
414 (let ((dw (- w (- (bw-r wt) (bw-l wt)))))
415 (when (/= 0 dw)
416 (bw-adjust-window wt dw t))))
417 (when h
418 (let ((dh (- h (- (bw-b wt) (bw-t wt)))))
419 (when (/= 0 dh)
420 (bw-adjust-window wt dh nil)))))
421 (let* ((childs (cdr (assq 'childs wt)))
422 (cw (when w (/ w (if (bw-eqdir 'hor wt) (length childs) 1))))
423 (ch (when h (/ h (if (bw-eqdir 'ver wt) (length childs) 1)))))
424 (dolist (c childs)
425 (bw-balance-sub c cw ch)))))
426
427 ;;; A different solution to balance-windows
428
429 (defun window-fixed-size-p (&optional window direction)
430 "Non-nil if WINDOW cannot be resized in DIRECTION.
431 DIRECTION can be nil (i.e. any), `height' or `width'."
432 (with-current-buffer (window-buffer window)
433 (let ((fixed (and (boundp 'window-size-fixed) window-size-fixed)))
434 (when fixed
435 (not (and direction
436 (member (cons direction window-size-fixed)
437 '((height . width) (width . height)))))))))
438
439 (defvar window-area-factor 1
440 "Factor by which the window area should be over-estimated.
441 This is used by `balance-windows-area'.
442 Changing this globally has no effect.")
443 (make-variable-buffer-local 'window-area-factor)
444
445 (defun balance-windows-area ()
446 "Make all visible windows the same area (approximately).
447 See also `window-area-factor' to change the relative size of specific buffers."
448 (interactive)
449 (let* ((unchanged 0) (carry 0) (round 0)
450 ;; Remove fixed-size windows.
451 (wins (delq nil (mapcar (lambda (win)
452 (if (not (window-fixed-size-p win)) win))
453 (window-list nil 'nomini))))
454 (changelog nil)
455 next)
456 ;; Resizing a window changes the size of surrounding windows in complex
457 ;; ways, so it's difficult to balance them all. The introduction of
458 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
459 ;; very difficult to do. `balance-window' above takes an off-line
460 ;; approach: get the whole window tree, then balance it, then try to
461 ;; adjust the windows so they fit the result.
462 ;; Here, instead, we take a "local optimization" approach, where we just
463 ;; go through all the windows several times until nothing needs to be
464 ;; changed. The main problem with this approach is that it's difficult
465 ;; to make sure it terminates, so we use some heuristic to try and break
466 ;; off infinite loops.
467 ;; After a round without any change, we allow a second, to give a chance
468 ;; to the carry to propagate a minor imbalance from the end back to
469 ;; the beginning.
470 (while (< unchanged 2)
471 ;; (message "New round")
472 (setq unchanged (1+ unchanged) round (1+ round))
473 (dolist (win wins)
474 (setq next win)
475 (while (progn (setq next (next-window next))
476 (window-fixed-size-p next)))
477 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
478 (let* ((horiz
479 (< (car (window-edges win)) (car (window-edges next))))
480 (areadiff (/ (- (* (window-height next) (window-width next)
481 (buffer-local-value 'window-area-factor
482 (window-buffer next)))
483 (* (window-height win) (window-width win)
484 (buffer-local-value 'window-area-factor
485 (window-buffer win))))
486 (max (buffer-local-value 'window-area-factor
487 (window-buffer win))
488 (buffer-local-value 'window-area-factor
489 (window-buffer next)))))
490 (edgesize (if horiz
491 (+ (window-height win) (window-height next))
492 (+ (window-width win) (window-width next))))
493 (diff (/ areadiff edgesize)))
494 (when (zerop diff)
495 ;; Maybe diff is actually closer to 1 than to 0.
496 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
497 (when (and (zerop diff) (not (zerop areadiff)))
498 (setq diff (/ (+ areadiff carry) edgesize))
499 ;; Change things smoothly.
500 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
501 (if (zerop diff)
502 ;; Make sure negligible differences don't accumulate to
503 ;; become significant.
504 (setq carry (+ carry areadiff))
505 (bw-adjust-window win diff horiz)
506 ;; (sit-for 0.5)
507 (let ((change (cons win (window-edges win))))
508 ;; If the same change has been seen already for this window,
509 ;; we're most likely in an endless loop, so don't count it as
510 ;; a change.
511 (unless (member change changelog)
512 (push change changelog)
513 (setq unchanged 0 carry 0)))))))
514 ;; We've now basically balanced all the windows.
515 ;; But there may be some minor off-by-one imbalance left over,
516 ;; so let's do some fine tuning.
517 ;; (bw-finetune wins)
518 ;; (message "Done in %d rounds" round)
519 ))
520
521 \f
522 (defcustom display-buffer-function nil
523 "If non-nil, function to call to handle `display-buffer'.
524 It will receive two args, the buffer and a flag which if non-nil
525 means that the currently selected window is not acceptable. It
526 should choose or create a window, display the specified buffer in
527 it, and return the window.
528
529 Commands such as `switch-to-buffer-other-window' and
530 `find-file-other-window' work using this function."
531 :type '(choice
532 (const nil)
533 (function :tag "function"))
534 :group 'windows)
535
536 (defun special-display-p (buffer-name)
537 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
538 If the value is t, `display-buffer' or `pop-to-buffer' would
539 create a special frame for that buffer using the default frame
540 parameters.
541
542 If the value is a list, it is a list of frame parameters that
543 would be used to make a frame for that buffer. The variables
544 `special-display-buffer-names' and `special-display-regexps'
545 control this."
546 (let (tmp)
547 (cond
548 ((not (stringp buffer-name)))
549 ;; Make sure to return t in the following two cases.
550 ((member buffer-name special-display-buffer-names) t)
551 ((setq tmp (assoc buffer-name special-display-buffer-names)) (cdr tmp))
552 ((catch 'found
553 (dolist (regexp special-display-regexps)
554 (cond
555 ((stringp regexp)
556 (when (string-match-p regexp buffer-name)
557 (throw 'found t)))
558 ((and (consp regexp) (stringp (car regexp))
559 (string-match-p (car regexp) buffer-name))
560 (throw 'found (cdr regexp))))))))))
561
562 (defcustom special-display-buffer-names nil
563 "List of buffer names that should have their own special frames.
564 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
565 its name is in this list, makes a special frame for it using
566 `special-display-function'. See also `special-display-regexps'.
567
568 An element of the list can be a list instead of just a string.
569 There are two ways to use a list as an element:
570 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
571 In the first case, the FRAME-PARAMETERS are pairs of the form
572 \(PARAMETER . VALUE); these parameter values are used to create
573 the frame. In the second case, FUNCTION is called with BUFFER as
574 the first argument, followed by the OTHER-ARGS--it can display
575 BUFFER in any way it likes. All this is done by the function
576 found in `special-display-function'.
577
578 If the specified frame parameters include (same-buffer . t), the
579 buffer is displayed in the currently selected window. Otherwise, if
580 they include (same-frame . t), the buffer is displayed in a new window
581 in the currently selected frame.
582
583 If this variable appears \"not to work\", because you add a name to it
584 but that buffer still appears in the selected window, look at the
585 values of `same-window-buffer-names' and `same-window-regexps'.
586 Those variables take precedence over this one."
587 :type '(repeat (choice :tag "Buffer"
588 :value ""
589 (string :format "%v")
590 (cons :tag "With attributes"
591 :format "%v"
592 :value ("" . nil)
593 (string :format "%v")
594 (repeat :tag "Attributes"
595 (cons :format "%v"
596 (symbol :tag "Parameter")
597 (sexp :tag "Value"))))))
598 :group 'frames)
599
600 (defcustom special-display-regexps nil
601 "List of regexps saying which buffers should have their own special frames.
602 When displaying a buffer with `display-buffer' or
603 `pop-to-buffer', if any regexp in this list matches the buffer
604 name, it makes a special frame for the buffer by calling
605 `special-display-function'.
606
607 An element of the list can be a list instead of just a string.
608 There are two ways to use a list as an element:
609 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
610 In the first case, the FRAME-PARAMETERS are pairs of the form
611 \(PARAMETER . VALUE); these parameter values are used to create
612 the frame. In the second case, FUNCTION is called with BUFFER as
613 the first argument, followed by the OTHER-ARGS--it can display
614 the buffer in any way it likes. All this is done by the function
615 found in `special-display-function'.
616
617 If the specified frame parameters include (same-buffer . t), the
618 buffer is displayed in the currently selected window. Otherwise,
619 if they include (same-frame . t), the buffer is displayed in a
620 new window in the currently selected frame.
621
622 If this variable appears \"not to work\", because you add a
623 regexp to it but the matching buffers still appear in the
624 selected window, look at the values of `same-window-buffer-names'
625 and `same-window-regexps'. Those variables take precedence over
626 this one."
627 :type '(repeat (choice :tag "Buffer"
628 :value ""
629 (regexp :format "%v")
630 (cons :tag "With attributes"
631 :format "%v"
632 :value ("" . nil)
633 (regexp :format "%v")
634 (repeat :tag "Attributes"
635 (cons :format "%v"
636 (symbol :tag "Parameter")
637 (sexp :tag "Value"))))))
638 :group 'frames)
639
640 (defcustom special-display-function 'special-display-popup-frame
641 "Function to call to make a new frame for a special buffer.
642 It is called with two arguments, the buffer and optional buffer
643 specific data, and should return a window displaying that buffer.
644 The default value normally makes a separate frame for the buffer,
645 using `special-display-frame-alist' to specify the frame
646 parameters.
647
648 But if the buffer specific data includes (same-buffer . t) then
649 the buffer is displayed in the current selected window.
650 Otherwise if it includes (same-frame . t) then the buffer is
651 displayed in a new window in the currently selected frame.
652
653 A buffer is special if it is listed in
654 `special-display-buffer-names' or matches a regexp in
655 `special-display-regexps'."
656 :type 'function
657 :group 'frames)
658
659 (defun same-window-p (buffer-name)
660 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
661 This function returns non-nil if `display-buffer' or
662 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
663 selected rather than \(as usual\) some other window. See
664 `same-window-buffer-names' and `same-window-regexps'."
665 (cond
666 ((not (stringp buffer-name)))
667 ;; The elements of `same-window-buffer-names' can be buffer
668 ;; names or cons cells whose cars are buffer names.
669 ((member buffer-name same-window-buffer-names))
670 ((assoc buffer-name same-window-buffer-names))
671 ((catch 'found
672 (dolist (regexp same-window-regexps)
673 ;; The elements of `same-window-regexps' can be regexps
674 ;; or cons cells whose cars are regexps.
675 (when (or (and (stringp regexp)
676 (string-match regexp buffer-name))
677 (and (consp regexp) (stringp (car regexp))
678 (string-match-p (car regexp) buffer-name)))
679 (throw 'found t)))))))
680
681 (defcustom same-window-buffer-names nil
682 "List of names of buffers that should appear in the \"same\" window.
683 `display-buffer' and `pop-to-buffer' show a buffer whose name is
684 on this list in the selected rather than some other window.
685
686 An element of this list can be a cons cell instead of just a
687 string. In that case the car must be a string specifying the
688 buffer name. This is for compatibility with
689 `special-display-buffer-names'; the cdr of the cons cell is
690 ignored.
691
692 See also `same-window-regexps'."
693 :type '(repeat (string :format "%v"))
694 :group 'windows)
695
696 (defcustom same-window-regexps nil
697 "List of regexps saying which buffers should appear in the \"same\" window.
698 `display-buffer' and `pop-to-buffer' show a buffer whose name
699 matches a regexp on this list in the selected rather than some
700 other window.
701
702 An element of this list can be a cons cell instead of just a
703 string. In that case the car must be a string, which specifies
704 the buffer name. This is for compatibility with
705 `special-display-buffer-names'; the cdr of the cons cell is
706 ignored.
707
708 See also `same-window-buffer-names'."
709 :type '(repeat (regexp :format "%v"))
710 :group 'windows)
711
712 (defcustom pop-up-frames nil
713 "Non-nil means `display-buffer' should make a separate frame."
714 :type 'boolean
715 :group 'windows)
716
717 (defcustom display-buffer-reuse-frames nil
718 "Non-nil means `display-buffer' should reuse frames.
719 If the buffer in question is already displayed in a frame, raise
720 that frame."
721 :type 'boolean
722 :version "21.1"
723 :group 'windows)
724
725 (defcustom pop-up-frame-function nil
726 "Function to call to handle automatic new frame creation.
727 It is called with no arguments and should return a newly created frame.
728
729 A typical value might be
730
731 `(lambda () (new-frame pop-up-frame-alist))'
732
733 where `pop-up-frame-alist' would hold the default frame
734 parameters."
735 :type '(choice
736 (const nil)
737 (function :tag "function"))
738 :group 'windows)
739
740 (defcustom pop-up-windows t
741 "Non-nil means `display-buffer' should make a new window."
742 :type 'boolean
743 :group 'windows)
744
745 (defcustom split-height-threshold 80
746 "Minimum height of window to be split vertically.
747 If the value is a number, `display-buffer' can split a window
748 only if it has at least as many lines. If the value is nil,
749 `display-buffer' cannot split a window vertically.
750
751 If the window is the only window on its frame, `display-buffer'
752 can split it regardless of this value."
753 :type '(choice (const nil) (number :tag "lines"))
754 :version "23.1"
755 :group 'windows)
756
757 (defcustom split-width-threshold 160
758 "Minimum width of window to be split horizontally.
759 If the value is a number, `display-buffer' can split a window
760 only if it has at least as many columns. If the value is nil,
761 `display-buffer' cannot split a window horizontally."
762 :type '(choice (const nil) (number :tag "columns"))
763 :version "23.1"
764 :group 'windows)
765
766 (defcustom split-window-preferred-function nil
767 "Function used by `display-buffer' to split windows.
768 If non-nil, a function called with a window as single argument
769 supposed to split that window and return the new window. If the
770 function returns nil the window is not split.
771
772 If nil, `display-buffer' will split the window respecting the
773 values of `split-height-threshold' and `split-width-threshold'."
774 :type '(choice (const nil) (function :tag "Function"))
775 :version "23.1"
776 :group 'windows)
777
778 (defun window--splittable-p (window &optional horizontal)
779 "Return non-nil if window WINDOW can be split evenly.
780 Optional argument HORIZONTAL non-nil means check whether WINDOW
781 can be split horizontally.
782
783 WINDOW can be split vertically when the following conditions
784 hold:
785
786 - `window-size-fixed' is either nil or equals `width' for the
787 buffer of WINDOW.
788
789 - `split-height-threshold' is a number and WINDOW is at least as
790 high as `split-height-threshold'.
791
792 - When WINDOW is split evenly, the emanating windows are at least
793 `window-min-height' lines tall and can accommodate at least one
794 line plus - if WINDOW has one - a modeline.
795
796 WINDOW can be split horizontally when the following conditions
797 hold:
798
799 - `window-size-fixed' is either nil or equals `height' for the
800 buffer of WINDOW.
801
802 - `split-width-threshold' is a number and WINDOW is at least as
803 wide as `split-width-threshold'.
804
805 - When WINDOW is split evenly, the emanating windows are at least
806 `window-min-width' or two (whichever is larger) columns wide."
807 (when (window-live-p window)
808 (with-current-buffer (window-buffer window)
809 (if horizontal
810 ;; A window can be split horizontally when its width is not
811 ;; fixed, it is at least `split-width-threshold' columns wide
812 ;; and at least twice as wide as `window-min-width' and 2 (the
813 ;; latter value is hardcoded).
814 (and (memq window-size-fixed '(nil height))
815 ;; Testing `window-full-width-p' here hardly makes any
816 ;; sense nowadays. This can be done more intuitively by
817 ;; setting up `split-width-threshold' appropriately.
818 (numberp split-width-threshold)
819 (>= (window-width window)
820 (max split-width-threshold
821 (* 2 (max window-min-width 2)))))
822 ;; A window can be split vertically when its height is not
823 ;; fixed, it is at least `split-height-threshold' lines high,
824 ;; and it is at least twice as high as `window-min-height' and 2
825 ;; if it has a modeline or 1.
826 (and (memq window-size-fixed '(nil width))
827 (numberp split-height-threshold)
828 (>= (window-height window)
829 (max split-height-threshold
830 (* 2 (max window-min-height
831 (if mode-line-format 2 1))))))))))
832
833 (defun window--try-to-split-window (window)
834 "Split window WINDOW if it is splittable.
835 See `window--splittable-p' for how to determine whether a window
836 is splittable. If WINDOW can be split, return the value returned
837 by `split-window' or `split-window-preferred-function'."
838 (when (and (window-live-p window)
839 (not (frame-parameter (window-frame window) 'unsplittable)))
840 (if (functionp split-window-preferred-function)
841 ;; `split-window-preferred-function' is specified, so use it.
842 (funcall split-window-preferred-function window)
843 (or (and (window--splittable-p window)
844 ;; Split window vertically.
845 (split-window window))
846 (and (window--splittable-p window t)
847 ;; Split window horizontally.
848 (split-window window nil t))
849 (and (eq window (frame-root-window (window-frame window)))
850 (not (window-minibuffer-p window))
851 ;; If WINDOW is the only window on its frame and not the
852 ;; minibuffer window, attempt to split it vertically
853 ;; disregarding the value of `split-height-threshold'.
854 (let ((split-height-threshold 0))
855 (window--splittable-p window)
856 (split-window window)))))))
857
858 (defun window--frame-usable-p (frame)
859 "Return frame FRAME if it can be used to display another buffer."
860 (when (framep frame)
861 (let ((window (frame-root-window frame)))
862 ;; `frame-root-window' may be an internal window which is considered
863 ;; "dead" by `window-live-p'. Hence if `window' is not live we
864 ;; implicitly know that `frame' has a visible window we can use.
865 (when (or (not (window-live-p window))
866 (and (not (window-minibuffer-p window))
867 (not (window-dedicated-p window))))
868 frame))))
869
870 (defcustom even-window-heights t
871 "If non-nil `display-buffer' will try to even window heights.
872 Otherwise `display-buffer' will leave the window configuration
873 alone. Heights are evened only when `display-buffer' chooses a
874 window that appears above or below the selected window."
875 :type 'boolean
876 :group 'windows)
877
878 (defun window--even-window-heights (window)
879 "Even heights of window WINDOW and selected window.
880 Do this only if these windows are vertically adjacent to each
881 other, `even-window-heights' is non-nil, and the selected window
882 is higher than WINDOW."
883 (when (and even-window-heights
884 (not (eq window (selected-window)))
885 ;; Don't resize minibuffer windows.
886 (not (window-minibuffer-p (selected-window)))
887 (> (window-height (selected-window)) (window-height window))
888 (eq (window-frame window) (window-frame (selected-window)))
889 (let ((sel-edges (window-edges (selected-window)))
890 (win-edges (window-edges window)))
891 (and (= (nth 0 sel-edges) (nth 0 win-edges))
892 (= (nth 2 sel-edges) (nth 2 win-edges))
893 (or (= (nth 1 sel-edges) (nth 3 win-edges))
894 (= (nth 3 sel-edges) (nth 1 win-edges))))))
895 (let ((window-min-height 1))
896 ;; Don't throw an error if we can't even window heights for
897 ;; whatever reason.
898 (condition-case nil
899 (enlarge-window (/ (- (window-height window) (window-height)) 2))
900 (error nil)))))
901
902 (defun window--display-buffer-1 (window)
903 "Deiconify the frame containing the window WINDOW.
904 Do not deiconify the selected frame. Return WINDOW."
905 (let* ((frame (window-frame window))
906 (visible (frame-visible-p frame)))
907 (unless (or (not visible)
908 ;; Assume the selected frame is already visible enough.
909 (eq frame (selected-frame))
910 ;; Assume the frame from which we invoked the minibuffer
911 ;; is visible.
912 (and (minibuffer-window-active-p (selected-window))
913 (eq frame (window-frame (minibuffer-selected-window)))))
914 (when (eq visible 'icon)
915 (make-frame-visible frame))
916 (raise-frame frame))
917 window))
918
919 (defun window--display-buffer-2 (buffer window)
920 "Display buffer BUFFER in window WINDOW and make its frame visible.
921 Return WINDOW."
922 (when (and (buffer-live-p buffer) (window-live-p window))
923 (set-window-buffer window buffer)
924 (window--display-buffer-1 window)))
925
926 (defun display-buffer (buffer-or-name &optional not-this-window frame)
927 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
928 BUFFER-OR-NAME must be a buffer or the name of an existing
929 buffer. Return the window chosen to display BUFFER-OR-NAME or
930 nil is no such window is found.
931
932 Optional argument NOT-THIS-WINDOW non-nil means display the
933 buffer in a window other than the selected one, even if it is
934 already displayed in the selected window.
935
936 Optional argument FRAME specifies which frames to investigate
937 when the specified buffer is already displayed. If the buffer is
938 already displayed in some window on one of these frames simply
939 return that window. Possible values of FRAME are:
940
941 `visible' - consider windows on all visible frames.
942
943 0 - consider windows on all visible or iconified frames.
944
945 t - consider windows on all frames.
946
947 A specific frame - consider windows on that frame only.
948
949 nil - consider windows on the selected frame \(actually the
950 last non-minibuffer frame\) only. If, however, either
951 `display-buffer-reuse-frames' or `pop-up-frames' is non-nil,
952 consider all visible or iconified frames."
953 (interactive "BDisplay buffer:\nP")
954 (let* ((can-use-selected-window
955 ;; The selected window is usable unless either NOT-THIS-WINDOW
956 ;; is non-nil, it is dedicated to its buffer, or it is the
957 ;; `minibuffer-window'.
958 (not (or not-this-window
959 (window-dedicated-p (selected-window))
960 (window-minibuffer-p))))
961 (buffer (if (bufferp buffer-or-name)
962 buffer-or-name
963 (get-buffer buffer-or-name)))
964 (name-of-buffer (buffer-name buffer))
965 ;; `frame-to-use' is the frame where to show `buffer' - either
966 ;; the selected frame or the last nonminibuffer frame.
967 (frame-to-use
968 (or (window--frame-usable-p (selected-frame))
969 (window--frame-usable-p (last-nonminibuffer-frame))))
970 ;; `window-to-use' is the window we use for showing `buffer'.
971 window-to-use)
972 (cond
973 ((not (buffer-live-p buffer))
974 (error "No such buffer %s" buffer))
975 (display-buffer-function
976 ;; Let `display-buffer-function' do the job.
977 (funcall display-buffer-function buffer not-this-window))
978 ((and (not not-this-window)
979 (eq (window-buffer (selected-window)) buffer))
980 ;; The selected window already displays BUFFER and
981 ;; `not-this-window' is nil, so use it.
982 (window--display-buffer-1 (selected-window)))
983 ((and can-use-selected-window (same-window-p name-of-buffer))
984 ;; If the buffer's name tells us to use the selected window do so.
985 (window--display-buffer-2 buffer (selected-window)))
986 ((let ((frames (or frame
987 (and (or pop-up-frames display-buffer-reuse-frames
988 (not (last-nonminibuffer-frame)))
989 0)
990 (last-nonminibuffer-frame))))
991 (and (setq window-to-use (get-buffer-window buffer frames))
992 (or can-use-selected-window
993 (not (eq (selected-window) window-to-use)))))
994 ;; If the buffer is already displayed in some window use that.
995 (window--display-buffer-1 window-to-use))
996 ((and special-display-function
997 ;; `special-display-p' returns either t or a list of frame
998 ;; parameters to pass to `special-display-function'.
999 (let ((pars (special-display-p name-of-buffer)))
1000 (when pars
1001 (funcall special-display-function
1002 buffer (if (listp pars) pars))))))
1003 ((or pop-up-frames (not frame-to-use))
1004 ;; We want or need a new frame.
1005 (window--display-buffer-2
1006 buffer (frame-selected-window (funcall pop-up-frame-function))))
1007 ((and pop-up-windows
1008 ;; Make a new window.
1009 (or (not (frame-parameter frame-to-use 'unsplittable))
1010 ;; If the selected frame cannot be split look at
1011 ;; `last-nonminibuffer-frame'.
1012 (and (eq frame-to-use (selected-frame))
1013 (setq frame-to-use (last-nonminibuffer-frame))
1014 (window--frame-usable-p frame-to-use)
1015 (not (frame-parameter frame-to-use 'unsplittable))))
1016 ;; Attempt to split largest or least recently used window.
1017 (setq window-to-use
1018 (or (window--try-to-split-window
1019 (get-largest-window frame-to-use t))
1020 (window--try-to-split-window
1021 (get-lru-window frame-to-use t))))
1022 (window--display-buffer-2 buffer window-to-use)))
1023 ((setq window-to-use
1024 ;; Reuse an existing window.
1025 (or (get-lru-window frame-to-use)
1026 (get-buffer-window buffer 'visible)
1027 (get-largest-window 'visible nil)
1028 (get-buffer-window buffer 0)
1029 (get-largest-window 0 nil)
1030 (frame-selected-window (funcall pop-up-frame-function))))
1031 (window--even-window-heights window-to-use)
1032 (window--display-buffer-2 buffer window-to-use)))))
1033
1034 (defun pop-to-buffer (buffer-or-name &optional other-window norecord)
1035 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
1036 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1037 nil. If BUFFER-OR-NAME is a string not naming an existent
1038 buffer, create a buffer with that name. If BUFFER-OR-NAME is
1039 nil, choose some other buffer.
1040
1041 If `pop-up-windows' is non-nil, windows can be split to display
1042 the buffer. If optional second arg OTHER-WINDOW is non-nil,
1043 insist on finding another window even if the specified buffer is
1044 already visible in the selected window, and ignore
1045 `same-window-regexps' and `same-window-buffer-names'.
1046
1047 This function returns the buffer it switched to. This uses the
1048 function `display-buffer' as a subroutine; see the documentation
1049 of `display-buffer' for additional customization information.
1050
1051 Optional third arg NORECORD non-nil means do not put this buffer
1052 at the front of the list of recently selected ones."
1053 (let ((buffer
1054 ;; FIXME: This behavior is carried over from the previous C version
1055 ;; of pop-to-buffer, but really we should use just
1056 ;; `get-buffer' here.
1057 (if (null buffer-or-name) (other-buffer (current-buffer))
1058 (or (get-buffer buffer-or-name)
1059 (let ((buf (get-buffer-create buffer-or-name)))
1060 (set-buffer-major-mode buf)
1061 buf)))))
1062 (set-buffer buffer)
1063 (select-window (display-buffer buffer other-window) norecord)
1064 buffer))
1065
1066 ;; I think this should be the default; I think people will prefer it--rms.
1067 (defcustom split-window-keep-point t
1068 "If non-nil, \\[split-window-vertically] keeps the original point \
1069 in both children.
1070 This is often more convenient for editing.
1071 If nil, adjust point in each of the two windows to minimize redisplay.
1072 This is convenient on slow terminals, but point can move strangely.
1073
1074 This option applies only to `split-window-vertically' and
1075 functions that call it. `split-window' always keeps the original
1076 point in both children."
1077 :type 'boolean
1078 :group 'windows)
1079
1080 (defun split-window-vertically (&optional arg)
1081 "Split current window into two windows, one above the other.
1082 The uppermost window gets ARG lines and the other gets the rest.
1083 Negative ARG means select the size of the lowermost window instead.
1084 With no argument, split equally or close to it.
1085 Both windows display the same buffer now current.
1086
1087 If the variable `split-window-keep-point' is non-nil, both new windows
1088 will get the same value of point as the current window. This is often
1089 more convenient for editing. The upper window is the selected window.
1090
1091 Otherwise, we choose window starts so as to minimize the amount of
1092 redisplay; this is convenient on slow terminals. The new selected
1093 window is the one that the current value of point appears in. The
1094 value of point can change if the text around point is hidden by the
1095 new mode line.
1096
1097 Regardless of the value of `split-window-keep-point', the upper
1098 window is the original one and the return value is the new, lower
1099 window."
1100 (interactive "P")
1101 (let ((old-w (selected-window))
1102 (old-point (point))
1103 (size (and arg (prefix-numeric-value arg)))
1104 (window-full-p nil)
1105 new-w bottom moved)
1106 (and size (< size 0) (setq size (+ (window-height) size)))
1107 (setq new-w (split-window nil size))
1108 (or split-window-keep-point
1109 (progn
1110 (save-excursion
1111 (set-buffer (window-buffer))
1112 (goto-char (window-start))
1113 (setq moved (vertical-motion (window-height)))
1114 (set-window-start new-w (point))
1115 (if (> (point) (window-point new-w))
1116 (set-window-point new-w (point)))
1117 (and (= moved (window-height))
1118 (progn
1119 (setq window-full-p t)
1120 (vertical-motion -1)))
1121 (setq bottom (point)))
1122 (and window-full-p
1123 (<= bottom (point))
1124 (set-window-point old-w (1- bottom)))
1125 (and window-full-p
1126 (<= (window-start new-w) old-point)
1127 (progn
1128 (set-window-point new-w old-point)
1129 (select-window new-w)))))
1130 (split-window-save-restore-data new-w old-w)))
1131
1132 ;; This is to avoid compiler warnings.
1133 (defvar view-return-to-alist)
1134
1135 (defun split-window-save-restore-data (new-w old-w)
1136 (with-current-buffer (window-buffer)
1137 (if view-mode
1138 (let ((old-info (assq old-w view-return-to-alist)))
1139 (if old-info
1140 (push (cons new-w (cons (car (cdr old-info)) t))
1141 view-return-to-alist))))
1142 new-w))
1143
1144 (defun split-window-horizontally (&optional arg)
1145 "Split current window into two windows side by side.
1146 This window becomes the leftmost of the two, and gets ARG columns.
1147 Negative ARG means select the size of the rightmost window instead.
1148 The argument includes the width of the window's scroll bar; if there
1149 are no scroll bars, it includes the width of the divider column
1150 to the window's right, if any. No ARG means split equally.
1151
1152 The original, leftmost window remains selected.
1153 The return value is the new, rightmost window."
1154 (interactive "P")
1155 (let ((old-w (selected-window))
1156 (size (and arg (prefix-numeric-value arg))))
1157 (and size (< size 0)
1158 (setq size (+ (window-width) size)))
1159 (split-window-save-restore-data (split-window nil size t) old-w)))
1160
1161 \f
1162 (defun set-window-text-height (window height)
1163 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
1164 This doesn't include the mode-line (or header-line if any) or any
1165 partial-height lines in the text display area.
1166
1167 If WINDOW is nil, the selected window is used.
1168
1169 Note that the current implementation of this function cannot always set
1170 the height exactly, but attempts to be conservative, by allocating more
1171 lines than are actually needed in the case where some error may be present."
1172 (let ((delta (- height (window-text-height window))))
1173 (unless (zerop delta)
1174 ;; Setting window-min-height to a value like 1 can lead to very
1175 ;; bizarre displays because it also allows Emacs to make *other*
1176 ;; windows 1-line tall, which means that there's no more space for
1177 ;; the modeline.
1178 (let ((window-min-height (min 2 height))) ;One text line plus a modeline.
1179 (if (and window (not (eq window (selected-window))))
1180 (save-selected-window
1181 (select-window window)
1182 (enlarge-window delta))
1183 (enlarge-window delta))))))
1184
1185 \f
1186 (defun enlarge-window-horizontally (arg)
1187 "Make current window ARG columns wider."
1188 (interactive "p")
1189 (enlarge-window arg t))
1190
1191 (defun shrink-window-horizontally (arg)
1192 "Make current window ARG columns narrower."
1193 (interactive "p")
1194 (shrink-window arg t))
1195
1196 (defun window-buffer-height (window)
1197 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
1198 (with-current-buffer (window-buffer window)
1199 (max 1
1200 (count-screen-lines (point-min) (point-max)
1201 ;; If buffer ends with a newline, ignore it when
1202 ;; counting height unless point is after it.
1203 (eobp)
1204 window))))
1205
1206 (defun count-screen-lines (&optional beg end count-final-newline window)
1207 "Return the number of screen lines in the region.
1208 The number of screen lines may be different from the number of actual lines,
1209 due to line breaking, display table, etc.
1210
1211 Optional arguments BEG and END default to `point-min' and `point-max'
1212 respectively.
1213
1214 If region ends with a newline, ignore it unless optional third argument
1215 COUNT-FINAL-NEWLINE is non-nil.
1216
1217 The optional fourth argument WINDOW specifies the window used for obtaining
1218 parameters such as width, horizontal scrolling, and so on. The default is
1219 to use the selected window's parameters.
1220
1221 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
1222 regardless of which buffer is displayed in WINDOW. This makes possible to use
1223 `count-screen-lines' in any buffer, whether or not it is currently displayed
1224 in some window."
1225 (unless beg
1226 (setq beg (point-min)))
1227 (unless end
1228 (setq end (point-max)))
1229 (if (= beg end)
1230 0
1231 (save-excursion
1232 (save-restriction
1233 (widen)
1234 (narrow-to-region (min beg end)
1235 (if (and (not count-final-newline)
1236 (= ?\n (char-before (max beg end))))
1237 (1- (max beg end))
1238 (max beg end)))
1239 (goto-char (point-min))
1240 (1+ (vertical-motion (buffer-size) window))))))
1241
1242 (defun fit-window-to-buffer (&optional window max-height min-height)
1243 "Make WINDOW the right height to display its contents exactly.
1244 If WINDOW is omitted or nil, it defaults to the selected window.
1245 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
1246 the window is allowed to be, defaulting to the frame height.
1247 If the optional argument MIN-HEIGHT is supplied, it is the minimum
1248 height the window is allowed to be, defaulting to `window-min-height'.
1249
1250 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
1251 header-line."
1252 (interactive)
1253
1254 (when (null window)
1255 (setq window (selected-window)))
1256 (when (null max-height)
1257 (setq max-height (frame-height (window-frame window))))
1258
1259 (let* ((buf
1260 ;; Buffer that is displayed in WINDOW
1261 (window-buffer window))
1262 (window-height
1263 ;; The current height of WINDOW
1264 (window-height window))
1265 (desired-height
1266 ;; The height necessary to show the buffer displayed by WINDOW
1267 ;; (`count-screen-lines' always works on the current buffer).
1268 (with-current-buffer buf
1269 (+ (count-screen-lines)
1270 ;; If the buffer is empty, (count-screen-lines) is
1271 ;; zero. But, even in that case, we need one text line
1272 ;; for cursor.
1273 (if (= (point-min) (point-max))
1274 1 0)
1275 ;; For non-minibuffers, count the mode-line, if any
1276 (if (and (not (window-minibuffer-p window))
1277 mode-line-format)
1278 1 0)
1279 ;; Count the header-line, if any
1280 (if header-line-format 1 0))))
1281 (delta
1282 ;; Calculate how much the window height has to change to show
1283 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
1284 (- (max (min desired-height max-height)
1285 (or min-height window-min-height))
1286 window-height)))
1287
1288 ;; Don't try to redisplay with the cursor at the end
1289 ;; on its own line--that would force a scroll and spoil things.
1290 (when (with-current-buffer buf
1291 (and (eobp) (bolp) (not (bobp))))
1292 (set-window-point window (1- (window-point window))))
1293
1294 (save-selected-window
1295 (select-window window)
1296
1297 ;; Adjust WINDOW to the nominally correct size (which may actually
1298 ;; be slightly off because of variable height text, etc).
1299 (unless (zerop delta)
1300 (enlarge-window delta))
1301
1302 ;; Check if the last line is surely fully visible. If not,
1303 ;; enlarge the window.
1304 (let ((end (with-current-buffer buf
1305 (save-excursion
1306 (goto-char (point-max))
1307 (when (and (bolp) (not (bobp)))
1308 ;; Don't include final newline
1309 (backward-char 1))
1310 (when truncate-lines
1311 ;; If line-wrapping is turned off, test the
1312 ;; beginning of the last line for visibility
1313 ;; instead of the end, as the end of the line
1314 ;; could be invisible by virtue of extending past
1315 ;; the edge of the window.
1316 (forward-line 0))
1317 (point)))))
1318 (set-window-vscroll window 0)
1319 (while (and (< desired-height max-height)
1320 (= desired-height (window-height window))
1321 (not (pos-visible-in-window-p end window)))
1322 (enlarge-window 1)
1323 (setq desired-height (1+ desired-height)))))))
1324
1325 (defun shrink-window-if-larger-than-buffer (&optional window)
1326 "Shrink the WINDOW to be as small as possible to display its contents.
1327 If WINDOW is omitted or nil, it defaults to the selected window.
1328 Do not shrink to less than `window-min-height' lines.
1329 Do nothing if the buffer contains more lines than the present window height,
1330 or if some of the window's contents are scrolled out of view,
1331 or if shrinking this window would also shrink another window,
1332 or if the window is the only window of its frame."
1333 (interactive)
1334 (when (null window)
1335 (setq window (selected-window)))
1336 (let* ((frame (window-frame window))
1337 (mini (frame-parameter frame 'minibuffer))
1338 (edges (window-edges window)))
1339 (if (and (not (eq window (frame-root-window frame)))
1340 (window-safely-shrinkable-p)
1341 (pos-visible-in-window-p (point-min) window)
1342 (not (eq mini 'only))
1343 (or (not mini)
1344 (let ((mini-window (minibuffer-window frame)))
1345 (or (null mini-window)
1346 (not (eq frame (window-frame mini-window)))
1347 (< (nth 3 edges)
1348 (nth 1 (window-edges mini-window)))
1349 (> (nth 1 edges)
1350 (frame-parameter frame 'menu-bar-lines))))))
1351 (fit-window-to-buffer window (window-height window)))))
1352
1353 (defun kill-buffer-and-window ()
1354 "Kill the current buffer and delete the selected window."
1355 (interactive)
1356 (let ((window-to-delete (selected-window))
1357 (buffer-to-kill (current-buffer))
1358 (delete-window-hook (lambda ()
1359 (condition-case nil
1360 (delete-window)
1361 (error nil)))))
1362 (unwind-protect
1363 (progn
1364 (add-hook 'kill-buffer-hook delete-window-hook t t)
1365 (if (kill-buffer (current-buffer))
1366 ;; If `delete-window' failed before, we rerun it to regenerate
1367 ;; the error so it can be seen in the echo area.
1368 (when (eq (selected-window) window-to-delete)
1369 (delete-window))))
1370 ;; If the buffer is not dead for some reason (probably because
1371 ;; of a `quit' signal), remove the hook again.
1372 (condition-case nil
1373 (with-current-buffer buffer-to-kill
1374 (remove-hook 'kill-buffer-hook delete-window-hook t))
1375 (error nil)))))
1376
1377 (defun quit-window (&optional kill window)
1378 "Quit the current buffer. Bury it, and maybe delete the selected frame.
1379 \(The frame is deleted if it contains a dedicated window for the buffer.)
1380 With a prefix argument, kill the buffer instead.
1381
1382 Noninteractively, if KILL is non-nil, then kill the current buffer,
1383 otherwise bury it.
1384
1385 If WINDOW is non-nil, it specifies a window; we delete that window,
1386 and the buffer that is killed or buried is the one in that window."
1387 (interactive "P")
1388 (let ((buffer (window-buffer window))
1389 (frame (window-frame (or window (selected-window))))
1390 (window-solitary
1391 (save-selected-window
1392 (if window
1393 (select-window window))
1394 (one-window-p t)))
1395 window-handled)
1396
1397 (save-selected-window
1398 (if window
1399 (select-window window))
1400 (or (window-minibuffer-p)
1401 (window-dedicated-p (selected-window))
1402 (switch-to-buffer (other-buffer))))
1403
1404 ;; Get rid of the frame, if it has just one dedicated window
1405 ;; and other visible frames exist.
1406 (and (or (window-minibuffer-p) (window-dedicated-p window))
1407 (delq frame (visible-frame-list))
1408 window-solitary
1409 (if (and (eq default-minibuffer-frame frame)
1410 (= 1 (length (minibuffer-frame-list))))
1411 (setq window nil)
1412 (delete-frame frame)
1413 (setq window-handled t)))
1414
1415 ;; Deal with the buffer.
1416 (if kill
1417 (kill-buffer buffer)
1418 (bury-buffer buffer))
1419
1420 ;; Maybe get rid of the window.
1421 (and window (not window-handled) (not window-solitary)
1422 (delete-window window))))
1423
1424 (defvar recenter-last-op nil
1425 "Indicates the last recenter operation performed.
1426 Possible values: `top', `middle', `bottom'.")
1427
1428 (defun recenter-top-bottom (&optional arg)
1429 "Move current line to window center, top, and bottom, successively.
1430 With no prefix argument, the first call redraws the frame and
1431 centers point vertically within the window. Successive calls
1432 scroll the window, placing point on the top, bottom, and middle
1433 consecutively. The cycling order is middle -> top -> bottom.
1434
1435 A prefix argument is handled like `recenter':
1436 With numeric prefix ARG, move current line to window-line ARG.
1437 With plain `C-u', move current line to window center.
1438
1439 Top and bottom destinations are actually `scroll-margin' lines
1440 the from true window top and bottom."
1441 (interactive "P")
1442 (cond
1443 (arg (recenter arg)) ; Always respect ARG.
1444 ((or (not (eq this-command last-command))
1445 (eq recenter-last-op 'bottom))
1446 (setq recenter-last-op 'middle)
1447 (recenter))
1448 (t
1449 (let ((this-scroll-margin
1450 (min (max 0 scroll-margin)
1451 (truncate (/ (window-body-height) 4.0)))))
1452 (cond ((eq recenter-last-op 'middle)
1453 (setq recenter-last-op 'top)
1454 (recenter this-scroll-margin))
1455 ((eq recenter-last-op 'top)
1456 (setq recenter-last-op 'bottom)
1457 (recenter (- -1 this-scroll-margin))))))))
1458
1459 (define-key global-map [?\C-l] 'recenter-top-bottom)
1460 \f
1461 (defvar mouse-autoselect-window-timer nil
1462 "Timer used by delayed window autoselection.")
1463
1464 (defvar mouse-autoselect-window-position nil
1465 "Last mouse position recorded by delayed window autoselection.")
1466
1467 (defvar mouse-autoselect-window-window nil
1468 "Last window recorded by delayed window autoselection.")
1469
1470 (defvar mouse-autoselect-window-state nil
1471 "When non-nil, special state of delayed window autoselection.
1472 Possible values are `suspend' \(suspend autoselection after a menu or
1473 scrollbar interaction\) and `select' \(the next invocation of
1474 'handle-select-window' shall select the window immediately\).")
1475
1476 (defun mouse-autoselect-window-cancel (&optional force)
1477 "Cancel delayed window autoselection.
1478 Optional argument FORCE means cancel unconditionally."
1479 (unless (and (not force)
1480 ;; Don't cancel for select-window or select-frame events
1481 ;; or when the user drags a scroll bar.
1482 (or (memq this-command
1483 '(handle-select-window handle-switch-frame))
1484 (and (eq this-command 'scroll-bar-toolkit-scroll)
1485 (memq (nth 4 (event-end last-input-event))
1486 '(handle end-scroll)))))
1487 (setq mouse-autoselect-window-state nil)
1488 (when (timerp mouse-autoselect-window-timer)
1489 (cancel-timer mouse-autoselect-window-timer))
1490 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
1491
1492 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
1493 "Start delayed window autoselection.
1494 MOUSE-POSITION is the last position where the mouse was seen as returned
1495 by `mouse-position'. Optional argument WINDOW non-nil denotes the
1496 window where the mouse was seen. Optional argument SUSPEND non-nil
1497 means suspend autoselection."
1498 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
1499 (setq mouse-autoselect-window-position mouse-position)
1500 (when window (setq mouse-autoselect-window-window window))
1501 (setq mouse-autoselect-window-state (when suspend 'suspend))
1502 ;; Install timer which runs `mouse-autoselect-window-select' after
1503 ;; `mouse-autoselect-window' seconds.
1504 (setq mouse-autoselect-window-timer
1505 (run-at-time
1506 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
1507
1508 (defun mouse-autoselect-window-select ()
1509 "Select window with delayed window autoselection.
1510 If the mouse position has stabilized in a non-selected window, select
1511 that window. The minibuffer window is selected only if the minibuffer is
1512 active. This function is run by `mouse-autoselect-window-timer'."
1513 (condition-case nil
1514 (let* ((mouse-position (mouse-position))
1515 (window
1516 (condition-case nil
1517 (window-at (cadr mouse-position) (cddr mouse-position)
1518 (car mouse-position))
1519 (error nil))))
1520 (cond
1521 ((or (menu-or-popup-active-p)
1522 (and window
1523 (not (coordinates-in-window-p (cdr mouse-position) window))))
1524 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
1525 ;; of WINDOW, temporarily suspend delayed autoselection.
1526 (mouse-autoselect-window-start mouse-position nil t))
1527 ((eq mouse-autoselect-window-state 'suspend)
1528 ;; Delayed autoselection was temporarily suspended, reenable it.
1529 (mouse-autoselect-window-start mouse-position))
1530 ((and window (not (eq window (selected-window)))
1531 (or (not (numberp mouse-autoselect-window))
1532 (and (> mouse-autoselect-window 0)
1533 ;; If `mouse-autoselect-window' is positive, select
1534 ;; window if the window is the same as before.
1535 (eq window mouse-autoselect-window-window))
1536 ;; Otherwise select window if the mouse is at the same
1537 ;; position as before. Observe that the first test after
1538 ;; starting autoselection usually fails since the value of
1539 ;; `mouse-autoselect-window-position' recorded there is the
1540 ;; position where the mouse has entered the new window and
1541 ;; not necessarily where the mouse has stopped moving.
1542 (equal mouse-position mouse-autoselect-window-position))
1543 ;; The minibuffer is a candidate window if it's active.
1544 (or (not (window-minibuffer-p window))
1545 (eq window (active-minibuffer-window))))
1546 ;; Mouse position has stabilized in non-selected window: Cancel
1547 ;; delayed autoselection and try to select that window.
1548 (mouse-autoselect-window-cancel t)
1549 ;; Select window where mouse appears unless the selected window is the
1550 ;; minibuffer. Use `unread-command-events' in order to execute pre-
1551 ;; and post-command hooks and trigger idle timers. To avoid delaying
1552 ;; autoselection again, set `mouse-autoselect-window-state'."
1553 (unless (window-minibuffer-p (selected-window))
1554 (setq mouse-autoselect-window-state 'select)
1555 (setq unread-command-events
1556 (cons (list 'select-window (list window))
1557 unread-command-events))))
1558 ((or (and window (eq window (selected-window)))
1559 (not (numberp mouse-autoselect-window))
1560 (equal mouse-position mouse-autoselect-window-position))
1561 ;; Mouse position has either stabilized in the selected window or at
1562 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
1563 (mouse-autoselect-window-cancel t))
1564 (t
1565 ;; Mouse position has not stabilized yet, resume delayed
1566 ;; autoselection.
1567 (mouse-autoselect-window-start mouse-position window))))
1568 (error nil)))
1569
1570 (defun handle-select-window (event)
1571 "Handle select-window events."
1572 (interactive "e")
1573 (let ((window (posn-window (event-start event))))
1574 (unless (or (not (window-live-p window))
1575 ;; Don't switch if we're currently in the minibuffer.
1576 ;; This tries to work around problems where the
1577 ;; minibuffer gets unselected unexpectedly, and where
1578 ;; you then have to move your mouse all the way down to
1579 ;; the minibuffer to select it.
1580 (window-minibuffer-p (selected-window))
1581 ;; Don't switch to minibuffer window unless it's active.
1582 (and (window-minibuffer-p window)
1583 (not (minibuffer-window-active-p window)))
1584 ;; Don't switch when autoselection shall be delayed.
1585 (and (numberp mouse-autoselect-window)
1586 (not (zerop mouse-autoselect-window))
1587 (not (eq mouse-autoselect-window-state 'select))
1588 (progn
1589 ;; Cancel any delayed autoselection.
1590 (mouse-autoselect-window-cancel t)
1591 ;; Start delayed autoselection from current mouse
1592 ;; position and window.
1593 (mouse-autoselect-window-start (mouse-position) window)
1594 ;; Executing a command cancels delayed autoselection.
1595 (add-hook
1596 'pre-command-hook 'mouse-autoselect-window-cancel))))
1597 (when mouse-autoselect-window
1598 ;; Reset state of delayed autoselection.
1599 (setq mouse-autoselect-window-state nil)
1600 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
1601 (run-hooks 'mouse-leave-buffer-hook))
1602 (select-window window))))
1603
1604 (defun delete-other-windows-vertically (&optional window)
1605 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
1606 This may be a useful alternative binding for \\[delete-other-windows]
1607 if you often split windows horizontally."
1608 (interactive)
1609 (let* ((window (or window (selected-window)))
1610 (edges (window-edges window))
1611 (w window) delenda)
1612 (while (not (eq (setq w (next-window w 1)) window))
1613 (let ((e (window-edges w)))
1614 (when (and (= (car e) (car edges))
1615 (= (caddr e) (caddr edges)))
1616 (push w delenda))))
1617 (mapc 'delete-window delenda)))
1618
1619 (defun truncated-partial-width-window-p (&optional window)
1620 "Non-nil if lines in WINDOW are specifically truncated due to its width.
1621 This returns nil if WINDOW is not a partial-width window
1622 (regardless of the value of `truncate-lines').
1623 Otherwise, consult the value of `truncate-partial-width-windows'
1624 for the buffer shown in WINDOW.
1625 If WINDOW is nil, use the selected window."
1626 (unless window
1627 (setq window (selected-window)))
1628 (unless (window-full-width-p window)
1629 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
1630 (window-buffer window))))
1631 (if (integerp t-p-w-w)
1632 (< (window-width window) t-p-w-w)
1633 t-p-w-w))))
1634
1635 (define-key ctl-x-map "2" 'split-window-vertically)
1636 (define-key ctl-x-map "3" 'split-window-horizontally)
1637 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
1638 (define-key ctl-x-map "{" 'shrink-window-horizontally)
1639 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
1640 (define-key ctl-x-map "+" 'balance-windows)
1641 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
1642
1643 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
1644 ;;; window.el ends here