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