]> code.delx.au - gnu-emacs/blob - lisp/window.el
* window.el (display-buffer-function, special-display-p)
[gnu-emacs] / 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
444 (defun balance-windows-area ()
445 "Make all visible windows the same area (approximately).
446 See also `window-area-factor' to change the relative size of specific buffers."
447 (interactive)
448 (let* ((unchanged 0) (carry 0) (round 0)
449 ;; Remove fixed-size windows.
450 (wins (delq nil (mapcar (lambda (win)
451 (if (not (window-fixed-size-p win)) win))
452 (window-list nil 'nomini))))
453 (changelog nil)
454 next)
455 ;; Resizing a window changes the size of surrounding windows in complex
456 ;; ways, so it's difficult to balance them all. The introduction of
457 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
458 ;; very difficult to do. `balance-window' above takes an off-line
459 ;; approach: get the whole window tree, then balance it, then try to
460 ;; adjust the windows so they fit the result.
461 ;; Here, instead, we take a "local optimization" approach, where we just
462 ;; go through all the windows several times until nothing needs to be
463 ;; changed. The main problem with this approach is that it's difficult
464 ;; to make sure it terminates, so we use some heuristic to try and break
465 ;; off infinite loops.
466 ;; After a round without any change, we allow a second, to give a chance
467 ;; to the carry to propagate a minor imbalance from the end back to
468 ;; the beginning.
469 (while (< unchanged 2)
470 ;; (message "New round")
471 (setq unchanged (1+ unchanged) round (1+ round))
472 (dolist (win wins)
473 (setq next win)
474 (while (progn (setq next (next-window next))
475 (window-fixed-size-p next)))
476 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
477 (let* ((horiz
478 (< (car (window-edges win)) (car (window-edges next))))
479 (areadiff (/ (- (* (window-height next) (window-width next)
480 (buffer-local-value 'window-area-factor
481 (window-buffer next)))
482 (* (window-height win) (window-width win)
483 (buffer-local-value 'window-area-factor
484 (window-buffer win))))
485 (max (buffer-local-value 'window-area-factor
486 (window-buffer win))
487 (buffer-local-value 'window-area-factor
488 (window-buffer next)))))
489 (edgesize (if horiz
490 (+ (window-height win) (window-height next))
491 (+ (window-width win) (window-width next))))
492 (diff (/ areadiff edgesize)))
493 (when (zerop diff)
494 ;; Maybe diff is actually closer to 1 than to 0.
495 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
496 (when (and (zerop diff) (not (zerop areadiff)))
497 (setq diff (/ (+ areadiff carry) edgesize))
498 ;; Change things smoothly.
499 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
500 (if (zerop diff)
501 ;; Make sure negligible differences don't accumulate to
502 ;; become significant.
503 (setq carry (+ carry areadiff))
504 (bw-adjust-window win diff horiz)
505 ;; (sit-for 0.5)
506 (let ((change (cons win (window-edges win))))
507 ;; If the same change has been seen already for this window,
508 ;; we're most likely in an endless loop, so don't count it as
509 ;; a change.
510 (unless (member change changelog)
511 (push change changelog)
512 (setq unchanged 0 carry 0)))))))
513 ;; We've now basically balanced all the windows.
514 ;; But there may be some minor off-by-one imbalance left over,
515 ;; so let's do some fine tuning.
516 ;; (bw-finetune wins)
517 ;; (message "Done in %d rounds" round)
518 ))
519
520 \f
521 (defcustom display-buffer-function nil
522 "If non-nil, function to call to handle `display-buffer'.
523 It will receive two args, the buffer and a flag which if non-nil
524 means that the currently selected window is not acceptable. It
525 should choose or create a window, display the specified buffer in
526 it, and return the window.
527
528 Commands such as `switch-to-buffer-other-window' and
529 `find-file-other-window' work using this function."
530 :type '(choice
531 (const nil)
532 (function :tag "function"))
533 :group 'windows)
534
535 (defun special-display-p (buffer-name)
536 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
537 If the value is t, `display-buffer' or `pop-to-buffer' would
538 create a special frame for that buffer using the default frame
539 parameters.
540
541 If the value is a list, it is a list of frame parameters that
542 would be used to make a frame for that buffer. The variables
543 `special-display-buffer-names' and `special-display-regexps'
544 control this."
545 (cond
546 ((not (stringp buffer-name)))
547 ;; Make sure to return t in the following two cases.
548 ((member buffer-name special-display-buffer-names) t)
549 ((assoc buffer-name special-display-buffer-names) t)
550 ((catch 'found
551 (dolist (regexp special-display-regexps)
552 (cond
553 ((stringp regexp)
554 (when (string-match-p regexp buffer-name)
555 (throw 'found t)))
556 ((and (consp regexp) (stringp (car regexp))
557 (string-match-p (car regexp) buffer-name))
558 (throw 'found (cdr regexp)))))))))
559
560 (defcustom special-display-buffer-names nil
561 "List of buffer names that should have their own special frames.
562 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
563 its name is in this list, makes a special frame for it using
564 `special-display-function'. See also `special-display-regexps'.
565
566 An element of the list can be a list instead of just a string.
567 There are two ways to use a list as an element:
568 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
569 In the first case, the FRAME-PARAMETERS are pairs of the form
570 \(PARAMETER . VALUE); these parameter values are used to create
571 the frame. In the second case, FUNCTION is called with BUFFER as
572 the first argument, followed by the OTHER-ARGS--it can display
573 BUFFER in any way it likes. All this is done by the function
574 found in `special-display-function'.
575
576 If the specified frame parameters include (same-buffer . t), the
577 buffer is displayed in the currently selected window. Otherwise, if
578 they include (same-frame . t), the buffer is displayed in a new window
579 in the currently selected frame.
580
581 If this variable appears \"not to work\", because you add a name to it
582 but that buffer still appears in the selected window, look at the
583 values of `same-window-buffer-names' and `same-window-regexps'.
584 Those variables take precedence over this one."
585 :type '(repeat (choice :tag "Buffer"
586 :value ""
587 (string :format "%v")
588 (cons :tag "With attributes"
589 :format "%v"
590 :value ("" . nil)
591 (string :format "%v")
592 (repeat :tag "Attributes"
593 (cons :format "%v"
594 (symbol :tag "Parameter")
595 (sexp :tag "Value"))))))
596 :group 'frames)
597
598 (defcustom special-display-regexps nil
599 "List of regexps saying which buffers should have their own special frames.
600 When displaying a buffer with `display-buffer' or
601 `pop-to-buffer', if any regexp in this list matches the buffer
602 name, it makes a special frame for the buffer by calling
603 `special-display-function'.
604
605 An element of the list can be a list instead of just a string.
606 There are two ways to use a list as an element:
607 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
608 In the first case, the FRAME-PARAMETERS are pairs of the form
609 \(PARAMETER . VALUE); these parameter values are used to create
610 the frame. In the second case, FUNCTION is called with BUFFER as
611 the first argument, followed by the OTHER-ARGS--it can display
612 the buffer in any way it likes. All this is done by the function
613 found in `special-display-function'.
614
615 If the specified frame parameters include (same-buffer . t), the
616 buffer is displayed in the currently selected window. Otherwise,
617 if they include (same-frame . t), the buffer is displayed in a
618 new window in the currently selected frame.
619
620 If this variable appears \"not to work\", because you add a
621 regexp to it but the matching buffers still appear in the
622 selected window, look at the values of `same-window-buffer-names'
623 and `same-window-regexps'. Those variables take precedence over
624 this one."
625 :type '(repeat (choice :tag "Buffer"
626 :value ""
627 (regexp :format "%v")
628 (cons :tag "With attributes"
629 :format "%v"
630 :value ("" . nil)
631 (regexp :format "%v")
632 (repeat :tag "Attributes"
633 (cons :format "%v"
634 (symbol :tag "Parameter")
635 (sexp :tag "Value"))))))
636 :group 'frames)
637
638 (defcustom special-display-function 'special-display-popup-frame
639 "Function to call to make a new frame for a special buffer.
640 It is called with two arguments, the buffer and optional buffer
641 specific data, and should return a window displaying that buffer.
642 The default value normally makes a separate frame for the buffer,
643 using `special-display-frame-alist' to specify the frame
644 parameters.
645
646 But if the buffer specific data includes (same-buffer . t) then
647 the buffer is displayed in the current selected window.
648 Otherwise if it includes (same-frame . t) then the buffer is
649 displayed in a new window in the currently selected frame.
650
651 A buffer is special if it is listed in
652 `special-display-buffer-names' or matches a regexp in
653 `special-display-regexps'."
654 :type 'function
655 :group 'frames)
656
657 (defun same-window-p (buffer-name)
658 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
659 This function returns non-nil if `display-buffer' or
660 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
661 selected rather than \(as usual\) some other window. See
662 `same-window-buffer-names' and `same-window-regexps'."
663 (cond
664 ((not (stringp buffer-name)))
665 ;; The elements of `same-window-buffer-names' can be buffer
666 ;; names or cons cells whose cars are buffer names.
667 ((member buffer-name same-window-buffer-names))
668 ((assoc buffer-name same-window-buffer-names))
669 ((catch 'found
670 (dolist (regexp same-window-regexps)
671 ;; The elements of `same-window-regexps' can be regexps
672 ;; or cons cells whose cars are regexps.
673 (when (or (and (stringp regexp)
674 (string-match regexp buffer-name))
675 (and (consp regexp) (stringp (car regexp))
676 (string-match-p (car regexp) buffer-name)))
677 (throw 'found t)))))))
678
679 (defcustom same-window-buffer-names nil
680 "List of names of buffers that should appear in the \"same\" window.
681 `display-buffer' and `pop-to-buffer' show a buffer whose name is
682 on this list in the selected rather than some other window.
683
684 An element of this list can be a cons cell instead of just a
685 string. In that case the car must be a string specifying the
686 buffer name. This is for compatibility with
687 `special-display-buffer-names'; the cdr of the cons cell is
688 ignored.
689
690 See also `same-window-regexps'."
691 :type '(repeat (string :format "%v"))
692 :group 'windows)
693
694 (defcustom same-window-regexps nil
695 "List of regexps saying which buffers should appear in the \"same\" window.
696 `display-buffer' and `pop-to-buffer' show a buffer whose name
697 matches a regexp on this list in the selected rather than some
698 other window.
699
700 An element of this list can be a cons cell instead of just a
701 string. In that case the car must be a string, which specifies
702 the buffer name. This is for compatibility with
703 `special-display-buffer-names'; the cdr of the cons cell is
704 ignored.
705
706 See also `same-window-buffer-names'."
707 :type '(repeat (regexp :format "%v"))
708 :group 'windows)
709
710 (defcustom pop-up-frames nil
711 "Non-nil means `display-buffer' should make a separate frame."
712 :type 'boolean
713 :group 'windows)
714
715 (defcustom display-buffer-reuse-frames nil
716 "Non-nil means `display-buffer' should reuse frames.
717 If the buffer in question is already displayed in a frame, raise
718 that frame."
719 :type 'boolean
720 :version "21.1"
721 :group 'windows)
722
723 (defcustom pop-up-frame-function nil
724 "Function to call to handle automatic new frame creation.
725 It is called with no arguments and should return a newly created frame.
726
727 A typical value might be
728
729 `(lambda () (new-frame pop-up-frame-alist))'
730
731 where `pop-up-frame-alist' would hold the default frame
732 parameters."
733 :type '(choice
734 (const nil)
735 (function :tag "function"))
736 :group 'windows)
737
738 (defcustom pop-up-windows t
739 "Non-nil means `display-buffer' should make a new window."
740 :type 'boolean
741 :group 'windows)
742
743 (defcustom split-window-preferred-function nil
744 "How `display-buffer' shall split windows.
745 Choices are `Vertically', `Horizontally', and `Sensibly' where
746 the latter attempts to split wide windows horizontally, narrow
747 ones vertically. Alternatively, you can set this to a function
748 called with a window as single argument to split that window in
749 two and return the new window."
750 :type '(choice
751 (const :tag "Vertically" nil)
752 (const :tag "Horizontally" horizontally)
753 (const :tag "Sensibly" sensibly)
754 (function :tag "Function"))
755 :version "23.1"
756 :group 'windows)
757
758 (defcustom split-height-threshold 80
759 "Minimum height of window to be split vertically by `display-buffer'.
760 If there is only one window, it can be split regardless of this."
761 :type 'number
762 :version "23.1"
763 :group 'windows)
764
765 (defcustom split-width-threshold 160
766 "Minimum width of window to be split horizontally by `display-buffer'.
767 If there is only one window, it can be split regardless of this."
768 :type 'number
769 :version "23.1"
770 :group 'windows)
771
772 (defun window--splittable-p (window &optional horizontal)
773 "Return non-nil if window WINDOW can be split evenly.
774 Optional argument HORIZONTAL non-nil means check whether WINDOW
775 can be split horizontally.
776
777 WINDOW can be split vertically when the following conditions
778 hold:
779
780 - `window-size-fixed' is either nil or equals `width' for the buffer of
781 WINDOW.
782
783 - WINDOW is at least as high as `split-height-threshold' or it is
784 the only window on its frame.
785
786 - When WINDOW is split evenly, the emanating windows are at least
787 `window-min-height' lines tall and can accomodate at least one
788 line plus - if WINDOW has one - a modeline.
789
790 WINDOW can be split horizontally when the following conditions
791 hold:
792
793 - `window-size-fixed' is either nil or equals `height' for the
794 buffer of WINDOW.
795
796 - WINDOW is at least as wide as `split-width-threshold'.
797
798 - When WINDOW is split evenly, the emanating windows are at least
799 `window-min-width' or two (whichever is larger) columns wide."
800 (when (window-live-p window)
801 (with-selected-window window
802 (if horizontal
803 ;; A window can be split horizontally when its width is not
804 ;; fixed, it is at least `split-width-threshold' columns wide
805 ;; and at least twice as wide as `window-min-width' and 2 (the
806 ;; latter value is hardcoded).
807 (and (memq window-size-fixed '(nil height))
808 (>= (window-width window)
809 (max split-width-threshold
810 (* 2 (max window-min-width 2)))))
811 ;; A window can be split vertically when its height is not
812 ;; fixed, it is at least `split-height-threshold' lines high or
813 ;; the only window on its frame, and it is at least twice as
814 ;; high as `window-min-height' and 2 if it has a modeline or 1.
815 (and (memq window-size-fixed '(nil width))
816 (>= (window-height window)
817 (max (if (one-window-p 'nomini) 0 split-height-threshold)
818 (* 2 (max window-min-height
819 (if mode-line-format 2 1))))))))))
820
821 (defun window--try-to-split-window (window)
822 "Split window WINDOW if it is splittable.
823 See `split-window-preferred-function' for how WINDOW shall be
824 split. See `window--splittable-p' for how to determine whether a
825 window is splittable. If WINDOW can be split, return the value
826 returned by `split-window' or `split-window-preferred-function'."
827 (when (and (window-live-p window)
828 ;; Testing `window-full-width-p' here hardly makes any
829 ;; sense nowadays. This can be done more intuitively by
830 ;; setting up `split-width-threshold' appropriately.
831 (not (frame-parameter (window-frame window) 'unsplittable)))
832 (or (and (not split-window-preferred-function)
833 (window--splittable-p window)
834 (split-window window))
835 (and (eq split-window-preferred-function 'horizontally)
836 (window--splittable-p window t)
837 (split-window window nil t))
838 (and (eq split-window-preferred-function 'sensibly)
839 ;; The following naive aspect-ratio test should become
840 ;; more sensible.
841 (or (and (> (window-width window) (window-height window))
842 (window--splittable-p window t)
843 (split-window window nil t))
844 (and (window--splittable-p window)
845 (split-window window))))
846 (and (functionp split-window-preferred-function)
847 (funcall split-window-preferred-function window)))))
848
849 (defun window--frame-usable-p (frame)
850 "Return frame FRAME if it can be used to display another buffer."
851 (let ((window (frame-root-window frame)))
852 (when (or (not (window-live-p window))
853 (and (not (window-minibuffer-p window))
854 (not (window-dedicated-p window))))
855 frame)))
856
857 (defcustom even-window-heights t
858 "If non-nil `display-buffer' will try to even window heights.
859 Otherwise `display-buffer' will leave the window configuration
860 alone. Heights are evened only when `display-buffer' chooses a
861 window that appears above or below the selected window."
862 :type 'boolean
863 :group 'windows)
864
865 (defun window--even-window-heights (window)
866 "Even heights of window WINDOW and selected window.
867 Do this only if these windows are vertically adjacent to each
868 other and `even-window-heights' is non-nil."
869 (when (and even-window-heights
870 (not (eq window (selected-window)))
871 ;; Don't resize minibuffer windows.
872 (not (window-minibuffer-p (selected-window)))
873 (/= (window-height (selected-window)) (window-height window))
874 (eq (window-frame window) (window-frame (selected-window)))
875 (let ((sel-edges (window-edges (selected-window)))
876 (win-edges (window-edges window)))
877 (and (= (nth 0 sel-edges) (nth 0 win-edges))
878 (= (nth 2 sel-edges) (nth 2 win-edges))
879 (or (= (nth 1 sel-edges) (nth 3 win-edges))
880 (= (nth 3 sel-edges) (nth 1 win-edges))))))
881 (let ((window-min-height 1))
882 ;; Don't throw an error if we can't even window heights for
883 ;; whatever reason.
884 (condition-case nil
885 (enlarge-window (/ (- (window-height window) (window-height)) 2))
886 (error nil)))))
887
888 (defun window--display-buffer-1 (window)
889 "Deiconify the frame containing the window WINDOW.
890 Do not deiconify the selected frame. Return WINDOW."
891 (let* ((frame (window-frame window))
892 (visible (frame-visible-p frame)))
893 (unless (or (not visible)
894 ;; Assume the selected frame is already visible enough.
895 (eq frame (selected-frame))
896 ;; Assume the frame from which we invoked the minibuffer
897 ;; is visible.
898 (and (minibuffer-window-active-p (selected-window))
899 (eq frame (window-frame (minibuffer-selected-window)))))
900 (when (eq visible 'icon)
901 (make-frame-visible frame))
902 (raise-frame frame))
903 window))
904
905 (defun window--display-buffer-2 (buffer window)
906 "Display buffer BUFFER in window WINDOW and make its frame visible.
907 Return WINDOW."
908 (when (and (buffer-live-p buffer) (window-live-p window))
909 (set-window-buffer window buffer)
910 (window--display-buffer-1 window)))
911
912 (defun display-buffer (buffer-or-name &optional not-this-window frame)
913 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
914 BUFFER-OR-NAME must be a buffer or the name of an existing
915 buffer. Return the window chosen to display BUFFER-OR-NAME or
916 nil is no such window is found.
917
918 Optional argument NOT-THIS-WINDOW non-nil means display the
919 buffer in a window other than the selected one, even if it is
920 already displayed in the selected window.
921
922 Optional argument FRAME specifies which frames to investigate
923 when the specified buffer is already displayed. If the buffer is
924 already displayed in some window on one of these frames simply
925 return that window. Possible values of FRAME are:
926
927 `visible' - consider windows on all visible frames.
928
929 0 - consider windows on all visible or iconified frames.
930
931 `t' - consider windows on all frames.
932
933 A specific frame - consider windows on that frame only.
934
935 `nil' - consider windows on the selected frame \(actually the
936 last non-minibuffer frame\) only. If, however, either
937 `display-buffer-reuse-frames' or `pop-up-frames' is non-nil,
938 consider all visible or iconified frames."
939 (interactive "BDisplay buffer:\nP")
940 (let* ((can-use-selected-window
941 ;; The selected window is usable unless either NOT-THIS-WINDOW
942 ;; is non-nil, it is dedicated to its buffer, or it is the
943 ;; `minibuffer-window'.
944 (not (or not-this-window
945 (window-dedicated-p (selected-window))
946 (window-minibuffer-p))))
947 (buffer (if (bufferp buffer-or-name)
948 buffer-or-name
949 (get-buffer buffer-or-name)))
950 (name-of-buffer (buffer-name buffer))
951 ;; `frame-to-use' is the frame where to show `buffer' - either
952 ;; the selected frame or the last nonminibuffer frame.
953 (frame-to-use
954 (or (window--frame-usable-p (selected-frame))
955 (window--frame-usable-p (last-nonminibuffer-frame))))
956 ;; `window-to-use' is the window we use for showing `buffer'.
957 window-to-use)
958 (cond
959 ((not (buffer-live-p buffer))
960 (error "No such buffer %s" buffer))
961 (display-buffer-function
962 ;; Let `display-buffer-function' do the job.
963 (funcall display-buffer-function buffer not-this-window))
964 ((and (not not-this-window)
965 (eq (window-buffer (selected-window)) buffer))
966 ;; The selected window already displays BUFFER and
967 ;; `not-this-window' is nil, so use it.
968 (window--display-buffer-1 (selected-window)))
969 ((and can-use-selected-window (same-window-p name-of-buffer))
970 ;; If the buffer's name tells us to use the selected window do so.
971 (window--display-buffer-2 buffer (selected-window)))
972 ((let ((frames (or frame
973 (and (or pop-up-frames display-buffer-reuse-frames
974 (not (last-nonminibuffer-frame)))
975 0)
976 (last-nonminibuffer-frame))))
977 (and (setq window-to-use (get-buffer-window buffer frames))
978 (or can-use-selected-window
979 (not (eq (selected-window) window-to-use)))))
980 ;; If the buffer is already displayed in some window use that.
981 (window--display-buffer-1 window-to-use))
982 ((and special-display-function
983 ;; `special-display-p' returns either t or a list of frame
984 ;; parameters to pass to `special-display-function'.
985 (let ((pars (special-display-p name-of-buffer)))
986 (when pars
987 (funcall special-display-function
988 (if (eq pars t) buffer pars))))))
989 ((or pop-up-frames (not frame-to-use))
990 ;; We want or need a new frame.
991 (window--display-buffer-2
992 buffer (frame-selected-window (funcall pop-up-frame-function))))
993 ((and pop-up-windows
994 ;; Make a new window.
995 (or (not (frame-parameter frame-to-use 'unsplittable))
996 ;; If the selected frame cannot be split look at
997 ;; `last-nonminibuffer-frame'.
998 (and (eq frame-to-use (selected-frame))
999 (setq frame-to-use (last-nonminibuffer-frame))
1000 (window--frame-usable-p frame-to-use)
1001 (not (frame-parameter frame-to-use 'unsplittable))))
1002 ;; Attempt to split largest or most recently used window.
1003 (setq window-to-use
1004 (or (window--try-to-split-window
1005 (get-largest-window frame-to-use t))
1006 (window--try-to-split-window
1007 (get-lru-window frame-to-use t))))
1008 (window--display-buffer-2 buffer window-to-use)))
1009 ((setq window-to-use
1010 ;; Reuse an existing window.
1011 (or (get-buffer-window buffer 'visible)
1012 (get-largest-window 'visible nil)
1013 (get-buffer-window buffer 0)
1014 (get-largest-window 0 nil)
1015 (frame-selected-window (funcall pop-up-frame-function))
1016 (get-lru-window t t)))
1017 (window--even-window-heights window-to-use)
1018 (window--display-buffer-2 buffer window-to-use)))))
1019
1020 (defun pop-to-buffer (buffer-or-name &optional other-window norecord)
1021 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
1022 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
1023 nil. If BUFFER-OR-NAME is a string not naming an existent
1024 buffer, create a buffer with that name. If BUFFER-OR-NAME is
1025 nil, choose some other buffer.
1026
1027 If `pop-up-windows' is non-nil, windows can be split to display
1028 the buffer. If optional second arg OTHER-WINDOW is non-nil,
1029 insist on finding another window even if the specified buffer is
1030 already visible in the selected window, and ignore
1031 `same-window-regexps' and `same-window-buffer-names'.
1032
1033 This function returns the buffer it switched to. This uses the
1034 function `display-buffer' as a subroutine; see the documentation
1035 of `display-buffer' for additional customization information.
1036
1037 Optional third arg NORECORD non-nil means do not put this buffer
1038 at the front of the list of recently selected ones."
1039 (let ((buffer
1040 ;; FIXME: This behavior is carried over from the previous C version
1041 ;; of pop-to-buffer, but really we should use just
1042 ;; `get-buffer' here.
1043 (if (null buffer-or-name) (other-buffer (current-buffer))
1044 (or (get-buffer buffer-or-name)
1045 (let ((buf (get-buffer-create buffer-or-name)))
1046 (set-buffer-major-mode buf)
1047 buf)))))
1048 (set-buffer buffer)
1049 (select-window (display-buffer buffer other-window) norecord)
1050 buffer))
1051
1052 ;; I think this should be the default; I think people will prefer it--rms.
1053 (defcustom split-window-keep-point t
1054 "*If non-nil, \\[split-window-vertically] keeps the original point \
1055 in both children.
1056 This is often more convenient for editing.
1057 If nil, adjust point in each of the two windows to minimize redisplay.
1058 This is convenient on slow terminals, but point can move strangely.
1059
1060 This option applies only to `split-window-vertically' and
1061 functions that call it. `split-window' always keeps the original
1062 point in both children."
1063 :type 'boolean
1064 :group 'windows)
1065
1066 (defun split-window-vertically (&optional arg)
1067 "Split current window into two windows, one above the other.
1068 The uppermost window gets ARG lines and the other gets the rest.
1069 Negative ARG means select the size of the lowermost window instead.
1070 With no argument, split equally or close to it.
1071 Both windows display the same buffer now current.
1072
1073 If the variable `split-window-keep-point' is non-nil, both new windows
1074 will get the same value of point as the current window. This is often
1075 more convenient for editing. The upper window is the selected window.
1076
1077 Otherwise, we choose window starts so as to minimize the amount of
1078 redisplay; this is convenient on slow terminals. The new selected
1079 window is the one that the current value of point appears in. The
1080 value of point can change if the text around point is hidden by the
1081 new mode line.
1082
1083 Regardless of the value of `split-window-keep-point', the upper
1084 window is the original one and the return value is the new, lower
1085 window."
1086 (interactive "P")
1087 (let ((old-w (selected-window))
1088 (old-point (point))
1089 (size (and arg (prefix-numeric-value arg)))
1090 (window-full-p nil)
1091 new-w bottom moved)
1092 (and size (< size 0) (setq size (+ (window-height) size)))
1093 (setq new-w (split-window nil size))
1094 (or split-window-keep-point
1095 (progn
1096 (save-excursion
1097 (set-buffer (window-buffer))
1098 (goto-char (window-start))
1099 (setq moved (vertical-motion (window-height)))
1100 (set-window-start new-w (point))
1101 (if (> (point) (window-point new-w))
1102 (set-window-point new-w (point)))
1103 (and (= moved (window-height))
1104 (progn
1105 (setq window-full-p t)
1106 (vertical-motion -1)))
1107 (setq bottom (point)))
1108 (and window-full-p
1109 (<= bottom (point))
1110 (set-window-point old-w (1- bottom)))
1111 (and window-full-p
1112 (<= (window-start new-w) old-point)
1113 (progn
1114 (set-window-point new-w old-point)
1115 (select-window new-w)))))
1116 (split-window-save-restore-data new-w old-w)))
1117
1118 ;; This is to avoid compiler warnings.
1119 (defvar view-return-to-alist)
1120
1121 (defun split-window-save-restore-data (new-w old-w)
1122 (with-current-buffer (window-buffer)
1123 (if view-mode
1124 (let ((old-info (assq old-w view-return-to-alist)))
1125 (if old-info
1126 (push (cons new-w (cons (car (cdr old-info)) t))
1127 view-return-to-alist))))
1128 new-w))
1129
1130 (defun split-window-horizontally (&optional arg)
1131 "Split current window into two windows side by side.
1132 This window becomes the leftmost of the two, and gets ARG columns.
1133 Negative ARG means select the size of the rightmost window instead.
1134 The argument includes the width of the window's scroll bar; if there
1135 are no scroll bars, it includes the width of the divider column
1136 to the window's right, if any. No ARG means split equally.
1137
1138 The original, leftmost window remains selected.
1139 The return value is the new, rightmost window."
1140 (interactive "P")
1141 (let ((old-w (selected-window))
1142 (size (and arg (prefix-numeric-value arg))))
1143 (and size (< size 0)
1144 (setq size (+ (window-width) size)))
1145 (split-window-save-restore-data (split-window nil size t) old-w)))
1146
1147 \f
1148 (defun set-window-text-height (window height)
1149 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
1150 This doesn't include the mode-line (or header-line if any) or any
1151 partial-height lines in the text display area.
1152
1153 If WINDOW is nil, the selected window is used.
1154
1155 Note that the current implementation of this function cannot always set
1156 the height exactly, but attempts to be conservative, by allocating more
1157 lines than are actually needed in the case where some error may be present."
1158 (let ((delta (- height (window-text-height window))))
1159 (unless (zerop delta)
1160 ;; Setting window-min-height to a value like 1 can lead to very
1161 ;; bizarre displays because it also allows Emacs to make *other*
1162 ;; windows 1-line tall, which means that there's no more space for
1163 ;; the modeline.
1164 (let ((window-min-height (min 2 height))) ;One text line plus a modeline.
1165 (if (and window (not (eq window (selected-window))))
1166 (save-selected-window
1167 (select-window window)
1168 (enlarge-window delta))
1169 (enlarge-window delta))))))
1170
1171 \f
1172 (defun enlarge-window-horizontally (arg)
1173 "Make current window ARG columns wider."
1174 (interactive "p")
1175 (enlarge-window arg t))
1176
1177 (defun shrink-window-horizontally (arg)
1178 "Make current window ARG columns narrower."
1179 (interactive "p")
1180 (shrink-window arg t))
1181
1182 (defun window-buffer-height (window)
1183 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
1184 (with-current-buffer (window-buffer window)
1185 (max 1
1186 (count-screen-lines (point-min) (point-max)
1187 ;; If buffer ends with a newline, ignore it when
1188 ;; counting height unless point is after it.
1189 (eobp)
1190 window))))
1191
1192 (defun count-screen-lines (&optional beg end count-final-newline window)
1193 "Return the number of screen lines in the region.
1194 The number of screen lines may be different from the number of actual lines,
1195 due to line breaking, display table, etc.
1196
1197 Optional arguments BEG and END default to `point-min' and `point-max'
1198 respectively.
1199
1200 If region ends with a newline, ignore it unless optional third argument
1201 COUNT-FINAL-NEWLINE is non-nil.
1202
1203 The optional fourth argument WINDOW specifies the window used for obtaining
1204 parameters such as width, horizontal scrolling, and so on. The default is
1205 to use the selected window's parameters.
1206
1207 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
1208 regardless of which buffer is displayed in WINDOW. This makes possible to use
1209 `count-screen-lines' in any buffer, whether or not it is currently displayed
1210 in some window."
1211 (unless beg
1212 (setq beg (point-min)))
1213 (unless end
1214 (setq end (point-max)))
1215 (if (= beg end)
1216 0
1217 (save-excursion
1218 (save-restriction
1219 (widen)
1220 (narrow-to-region (min beg end)
1221 (if (and (not count-final-newline)
1222 (= ?\n (char-before (max beg end))))
1223 (1- (max beg end))
1224 (max beg end)))
1225 (goto-char (point-min))
1226 (1+ (vertical-motion (buffer-size) window))))))
1227
1228 (defun fit-window-to-buffer (&optional window max-height min-height)
1229 "Make WINDOW the right height to display its contents exactly.
1230 If WINDOW is omitted or nil, it defaults to the selected window.
1231 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
1232 the window is allowed to be, defaulting to the frame height.
1233 If the optional argument MIN-HEIGHT is supplied, it is the minimum
1234 height the window is allowed to be, defaulting to `window-min-height'.
1235
1236 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
1237 header-line."
1238 (interactive)
1239
1240 (when (null window)
1241 (setq window (selected-window)))
1242 (when (null max-height)
1243 (setq max-height (frame-height (window-frame window))))
1244
1245 (let* ((buf
1246 ;; Buffer that is displayed in WINDOW
1247 (window-buffer window))
1248 (window-height
1249 ;; The current height of WINDOW
1250 (window-height window))
1251 (desired-height
1252 ;; The height necessary to show the buffer displayed by WINDOW
1253 ;; (`count-screen-lines' always works on the current buffer).
1254 (with-current-buffer buf
1255 (+ (count-screen-lines)
1256 ;; If the buffer is empty, (count-screen-lines) is
1257 ;; zero. But, even in that case, we need one text line
1258 ;; for cursor.
1259 (if (= (point-min) (point-max))
1260 1 0)
1261 ;; For non-minibuffers, count the mode-line, if any
1262 (if (and (not (window-minibuffer-p window))
1263 mode-line-format)
1264 1 0)
1265 ;; Count the header-line, if any
1266 (if header-line-format 1 0))))
1267 (delta
1268 ;; Calculate how much the window height has to change to show
1269 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
1270 (- (max (min desired-height max-height)
1271 (or min-height window-min-height))
1272 window-height)))
1273
1274 ;; Don't try to redisplay with the cursor at the end
1275 ;; on its own line--that would force a scroll and spoil things.
1276 (when (with-current-buffer buf
1277 (and (eobp) (bolp) (not (bobp))))
1278 (set-window-point window (1- (window-point window))))
1279
1280 (save-selected-window
1281 (select-window window)
1282
1283 ;; Adjust WINDOW to the nominally correct size (which may actually
1284 ;; be slightly off because of variable height text, etc).
1285 (unless (zerop delta)
1286 (enlarge-window delta))
1287
1288 ;; Check if the last line is surely fully visible. If not,
1289 ;; enlarge the window.
1290 (let ((end (with-current-buffer buf
1291 (save-excursion
1292 (goto-char (point-max))
1293 (when (and (bolp) (not (bobp)))
1294 ;; Don't include final newline
1295 (backward-char 1))
1296 (when truncate-lines
1297 ;; If line-wrapping is turned off, test the
1298 ;; beginning of the last line for visibility
1299 ;; instead of the end, as the end of the line
1300 ;; could be invisible by virtue of extending past
1301 ;; the edge of the window.
1302 (forward-line 0))
1303 (point)))))
1304 (set-window-vscroll window 0)
1305 (while (and (< desired-height max-height)
1306 (= desired-height (window-height window))
1307 (not (pos-visible-in-window-p end window)))
1308 (enlarge-window 1)
1309 (setq desired-height (1+ desired-height)))))))
1310
1311 (defun shrink-window-if-larger-than-buffer (&optional window)
1312 "Shrink the WINDOW to be as small as possible to display its contents.
1313 If WINDOW is omitted or nil, it defaults to the selected window.
1314 Do not shrink to less than `window-min-height' lines.
1315 Do nothing if the buffer contains more lines than the present window height,
1316 or if some of the window's contents are scrolled out of view,
1317 or if shrinking this window would also shrink another window,
1318 or if the window is the only window of its frame."
1319 (interactive)
1320 (when (null window)
1321 (setq window (selected-window)))
1322 (let* ((frame (window-frame window))
1323 (mini (frame-parameter frame 'minibuffer))
1324 (edges (window-edges window)))
1325 (if (and (not (eq window (frame-root-window frame)))
1326 (window-safely-shrinkable-p)
1327 (pos-visible-in-window-p (point-min) window)
1328 (not (eq mini 'only))
1329 (or (not mini)
1330 (let ((mini-window (minibuffer-window frame)))
1331 (or (null mini-window)
1332 (not (eq frame (window-frame mini-window)))
1333 (< (nth 3 edges)
1334 (nth 1 (window-edges mini-window)))
1335 (> (nth 1 edges)
1336 (frame-parameter frame 'menu-bar-lines))))))
1337 (fit-window-to-buffer window (window-height window)))))
1338
1339 (defun kill-buffer-and-window ()
1340 "Kill the current buffer and delete the selected window."
1341 (interactive)
1342 (let ((window-to-delete (selected-window))
1343 (buffer-to-kill (current-buffer))
1344 (delete-window-hook (lambda ()
1345 (condition-case nil
1346 (delete-window)
1347 (error nil)))))
1348 (unwind-protect
1349 (progn
1350 (add-hook 'kill-buffer-hook delete-window-hook t t)
1351 (if (kill-buffer (current-buffer))
1352 ;; If `delete-window' failed before, we rerun it to regenerate
1353 ;; the error so it can be seen in the echo area.
1354 (when (eq (selected-window) window-to-delete)
1355 (delete-window))))
1356 ;; If the buffer is not dead for some reason (probably because
1357 ;; of a `quit' signal), remove the hook again.
1358 (condition-case nil
1359 (with-current-buffer buffer-to-kill
1360 (remove-hook 'kill-buffer-hook delete-window-hook t))
1361 (error nil)))))
1362
1363 (defun quit-window (&optional kill window)
1364 "Quit the current buffer. Bury it, and maybe delete the selected frame.
1365 \(The frame is deleted if it contains a dedicated window for the buffer.)
1366 With a prefix argument, kill the buffer instead.
1367
1368 Noninteractively, if KILL is non-nil, then kill the current buffer,
1369 otherwise bury it.
1370
1371 If WINDOW is non-nil, it specifies a window; we delete that window,
1372 and the buffer that is killed or buried is the one in that window."
1373 (interactive "P")
1374 (let ((buffer (window-buffer window))
1375 (frame (window-frame (or window (selected-window))))
1376 (window-solitary
1377 (save-selected-window
1378 (if window
1379 (select-window window))
1380 (one-window-p t)))
1381 window-handled)
1382
1383 (save-selected-window
1384 (if window
1385 (select-window window))
1386 (or (window-minibuffer-p)
1387 (window-dedicated-p (selected-window))
1388 (switch-to-buffer (other-buffer))))
1389
1390 ;; Get rid of the frame, if it has just one dedicated window
1391 ;; and other visible frames exist.
1392 (and (or (window-minibuffer-p) (window-dedicated-p window))
1393 (delq frame (visible-frame-list))
1394 window-solitary
1395 (if (and (eq default-minibuffer-frame frame)
1396 (= 1 (length (minibuffer-frame-list))))
1397 (setq window nil)
1398 (delete-frame frame)
1399 (setq window-handled t)))
1400
1401 ;; Deal with the buffer.
1402 (if kill
1403 (kill-buffer buffer)
1404 (bury-buffer buffer))
1405
1406 ;; Maybe get rid of the window.
1407 (and window (not window-handled) (not window-solitary)
1408 (delete-window window))))
1409
1410 (defvar recenter-last-op nil
1411 "Indicates the last recenter operation performed.
1412 Possible values: `top', `middle', `bottom'.")
1413
1414 (defun recenter-top-bottom (&optional arg)
1415 "Move current line to window center, top, and bottom, successively.
1416 With a prefix argument, this is the same as `recenter':
1417 With numeric prefix ARG, move current line to window-line ARG.
1418 With plain `C-u', move current line to window center.
1419
1420 Otherwise move current line to window center on first call, and to
1421 top, middle, or bottom on successive calls.
1422
1423 The cycling order is: middle -> top -> bottom.
1424
1425 Top and bottom destinations are actually `scroll-conservatively' lines
1426 from true window top and bottom."
1427 (interactive "P")
1428 (cond
1429 (arg (recenter arg)) ; Always respect ARG.
1430 ((not (eq this-command last-command))
1431 ;; First time - save mode and recenter.
1432 (setq recenter-last-op 'middle)
1433 (recenter))
1434 (t ;; repeat: loop through various options.
1435 (setq recenter-last-op
1436 (cond ((eq recenter-last-op 'middle)
1437 (recenter scroll-conservatively)
1438 'top)
1439 ((eq recenter-last-op 'top)
1440 (recenter (1- (- scroll-conservatively)))
1441 'bottom)
1442 ((eq recenter-last-op 'bottom)
1443 (recenter)
1444 'middle))))))
1445
1446 (define-key global-map [?\C-l] 'recenter-top-bottom)
1447 \f
1448 (defvar mouse-autoselect-window-timer nil
1449 "Timer used by delayed window autoselection.")
1450
1451 (defvar mouse-autoselect-window-position nil
1452 "Last mouse position recorded by delayed window autoselection.")
1453
1454 (defvar mouse-autoselect-window-window nil
1455 "Last window recorded by delayed window autoselection.")
1456
1457 (defvar mouse-autoselect-window-state nil
1458 "When non-nil, special state of delayed window autoselection.
1459 Possible values are `suspend' \(suspend autoselection after a menu or
1460 scrollbar interaction\) and `select' \(the next invocation of
1461 'handle-select-window' shall select the window immediately\).")
1462
1463 (defun mouse-autoselect-window-cancel (&optional force)
1464 "Cancel delayed window autoselection.
1465 Optional argument FORCE means cancel unconditionally."
1466 (unless (and (not force)
1467 ;; Don't cancel for select-window or select-frame events
1468 ;; or when the user drags a scroll bar.
1469 (or (memq this-command
1470 '(handle-select-window handle-switch-frame))
1471 (and (eq this-command 'scroll-bar-toolkit-scroll)
1472 (memq (nth 4 (event-end last-input-event))
1473 '(handle end-scroll)))))
1474 (setq mouse-autoselect-window-state nil)
1475 (when (timerp mouse-autoselect-window-timer)
1476 (cancel-timer mouse-autoselect-window-timer))
1477 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
1478
1479 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
1480 "Start delayed window autoselection.
1481 MOUSE-POSITION is the last position where the mouse was seen as returned
1482 by `mouse-position'. Optional argument WINDOW non-nil denotes the
1483 window where the mouse was seen. Optional argument SUSPEND non-nil
1484 means suspend autoselection."
1485 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
1486 (setq mouse-autoselect-window-position mouse-position)
1487 (when window (setq mouse-autoselect-window-window window))
1488 (setq mouse-autoselect-window-state (when suspend 'suspend))
1489 ;; Install timer which runs `mouse-autoselect-window-select' after
1490 ;; `mouse-autoselect-window' seconds.
1491 (setq mouse-autoselect-window-timer
1492 (run-at-time
1493 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
1494
1495 (defun mouse-autoselect-window-select ()
1496 "Select window with delayed window autoselection.
1497 If the mouse position has stabilized in a non-selected window, select
1498 that window. The minibuffer window is selected only if the minibuffer is
1499 active. This function is run by `mouse-autoselect-window-timer'."
1500 (condition-case nil
1501 (let* ((mouse-position (mouse-position))
1502 (window
1503 (condition-case nil
1504 (window-at (cadr mouse-position) (cddr mouse-position)
1505 (car mouse-position))
1506 (error nil))))
1507 (cond
1508 ((or (menu-or-popup-active-p)
1509 (and window
1510 (not (coordinates-in-window-p (cdr mouse-position) window))))
1511 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
1512 ;; of WINDOW, temporarily suspend delayed autoselection.
1513 (mouse-autoselect-window-start mouse-position nil t))
1514 ((eq mouse-autoselect-window-state 'suspend)
1515 ;; Delayed autoselection was temporarily suspended, reenable it.
1516 (mouse-autoselect-window-start mouse-position))
1517 ((and window (not (eq window (selected-window)))
1518 (or (not (numberp mouse-autoselect-window))
1519 (and (> mouse-autoselect-window 0)
1520 ;; If `mouse-autoselect-window' is positive, select
1521 ;; window if the window is the same as before.
1522 (eq window mouse-autoselect-window-window))
1523 ;; Otherwise select window if the mouse is at the same
1524 ;; position as before. Observe that the first test after
1525 ;; starting autoselection usually fails since the value of
1526 ;; `mouse-autoselect-window-position' recorded there is the
1527 ;; position where the mouse has entered the new window and
1528 ;; not necessarily where the mouse has stopped moving.
1529 (equal mouse-position mouse-autoselect-window-position))
1530 ;; The minibuffer is a candidate window if it's active.
1531 (or (not (window-minibuffer-p window))
1532 (eq window (active-minibuffer-window))))
1533 ;; Mouse position has stabilized in non-selected window: Cancel
1534 ;; delayed autoselection and try to select that window.
1535 (mouse-autoselect-window-cancel t)
1536 ;; Select window where mouse appears unless the selected window is the
1537 ;; minibuffer. Use `unread-command-events' in order to execute pre-
1538 ;; and post-command hooks and trigger idle timers. To avoid delaying
1539 ;; autoselection again, set `mouse-autoselect-window-state'."
1540 (unless (window-minibuffer-p (selected-window))
1541 (setq mouse-autoselect-window-state 'select)
1542 (setq unread-command-events
1543 (cons (list 'select-window (list window))
1544 unread-command-events))))
1545 ((or (and window (eq window (selected-window)))
1546 (not (numberp mouse-autoselect-window))
1547 (equal mouse-position mouse-autoselect-window-position))
1548 ;; Mouse position has either stabilized in the selected window or at
1549 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
1550 (mouse-autoselect-window-cancel t))
1551 (t
1552 ;; Mouse position has not stabilized yet, resume delayed
1553 ;; autoselection.
1554 (mouse-autoselect-window-start mouse-position window))))
1555 (error nil)))
1556
1557 (defun handle-select-window (event)
1558 "Handle select-window events."
1559 (interactive "e")
1560 (let ((window (posn-window (event-start event))))
1561 (unless (or (not (window-live-p window))
1562 ;; Don't switch if we're currently in the minibuffer.
1563 ;; This tries to work around problems where the
1564 ;; minibuffer gets unselected unexpectedly, and where
1565 ;; you then have to move your mouse all the way down to
1566 ;; the minibuffer to select it.
1567 (window-minibuffer-p (selected-window))
1568 ;; Don't switch to minibuffer window unless it's active.
1569 (and (window-minibuffer-p window)
1570 (not (minibuffer-window-active-p window)))
1571 ;; Don't switch when autoselection shall be delayed.
1572 (and (numberp mouse-autoselect-window)
1573 (not (zerop mouse-autoselect-window))
1574 (not (eq mouse-autoselect-window-state 'select))
1575 (progn
1576 ;; Cancel any delayed autoselection.
1577 (mouse-autoselect-window-cancel t)
1578 ;; Start delayed autoselection from current mouse position
1579 ;; and window.
1580 (mouse-autoselect-window-start (mouse-position) window)
1581 ;; Executing a command cancels delayed autoselection.
1582 (add-hook
1583 'pre-command-hook 'mouse-autoselect-window-cancel))))
1584 (when mouse-autoselect-window
1585 ;; Reset state of delayed autoselection.
1586 (setq mouse-autoselect-window-state nil)
1587 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
1588 (run-hooks 'mouse-leave-buffer-hook))
1589 (select-window window))))
1590
1591 (defun delete-other-windows-vertically (&optional window)
1592 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
1593 This may be a useful alternative binding for \\[delete-other-windows]
1594 if you often split windows horizontally."
1595 (interactive)
1596 (let* ((window (or window (selected-window)))
1597 (edges (window-edges window))
1598 (w window) delenda)
1599 (while (not (eq (setq w (next-window w 1)) window))
1600 (let ((e (window-edges w)))
1601 (when (and (= (car e) (car edges))
1602 (= (caddr e) (caddr edges)))
1603 (push w delenda))))
1604 (mapc 'delete-window delenda)))
1605
1606 (define-key ctl-x-map "2" 'split-window-vertically)
1607 (define-key ctl-x-map "3" 'split-window-horizontally)
1608 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
1609 (define-key ctl-x-map "{" 'shrink-window-horizontally)
1610 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
1611 (define-key ctl-x-map "+" 'balance-windows)
1612 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
1613
1614 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
1615 ;;; window.el ends here