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