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