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