* src/image.c (imagemagick_load_image): Fix type mismatch.
[bpt/emacs.git] / lisp / window.el
CommitLineData
3c448ab6
MR
1;;; window.el --- GNU Emacs window commands aside from those written in C
2
73b0cd50 3;; Copyright (C) 1985, 1989, 1992-1994, 2000-2011
3689984f 4;; Free Software Foundation, Inc.
3c448ab6
MR
5
6;; Maintainer: FSF
7;; Keywords: internal
bd78fa1d 8;; Package: emacs
3c448ab6
MR
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;; Window tree functions.
28
29;;; Code:
30
31(eval-when-compile (require 'cl))
32
3c448ab6
MR
33(defmacro save-selected-window (&rest body)
34 "Execute BODY, then select the previously selected window.
35The value returned is the value of the last form in BODY.
36
37This macro saves and restores the selected window, as well as the
38selected window in each frame. If the previously selected window
39is no longer live, then whatever window is selected at the end of
40BODY remains selected. If the previously selected window of some
41frame is no longer live at the end of BODY, that frame's selected
42window is left alone.
43
44This macro saves and restores the current buffer, since otherwise
45its normal operation could make a different buffer current. The
46order of recently selected windows and the buffer list ordering
47are not altered by this macro (unless they are altered in BODY)."
f291fe60 48 (declare (indent 0) (debug t))
3c448ab6
MR
49 `(let ((save-selected-window-window (selected-window))
50 ;; It is necessary to save all of these, because calling
51 ;; select-window changes frame-selected-window for whatever
52 ;; frame that window is in.
53 (save-selected-window-alist
54 (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
55 (frame-list))))
56 (save-current-buffer
57 (unwind-protect
58 (progn ,@body)
59 (dolist (elt save-selected-window-alist)
60 (and (frame-live-p (car elt))
61 (window-live-p (cdr elt))
62 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
63 (when (window-live-p save-selected-window-window)
64 (select-window save-selected-window-window 'norecord))))))
65
85cc1f11
MR
66;; The following two functions are like `window-next' and `window-prev'
67;; but the WINDOW argument is _not_ optional (so they don't substitute
68;; the selected window for nil), and they return nil when WINDOW doesn't
69;; have a parent (like a frame's root window or a minibuffer window).
70(defsubst window-right (window)
71 "Return WINDOW's right sibling.
72Return nil if WINDOW is the root window of its frame. WINDOW can
73be any window."
74 (and window (window-parent window) (window-next window)))
75
76(defsubst window-left (window)
77 "Return WINDOW's left sibling.
78Return nil if WINDOW is the root window of its frame. WINDOW can
79be any window."
80 (and window (window-parent window) (window-prev window)))
81
82(defsubst window-child (window)
83 "Return WINDOW's first child window."
84 (or (window-vchild window) (window-hchild window)))
85
86(defun window-child-count (window)
87 "Return number of WINDOW's child windows."
88 (let ((count 0))
89 (when (and (windowp window) (setq window (window-child window)))
90 (while window
91 (setq count (1+ count))
92 (setq window (window-next window))))
93 count))
94
95(defun window-last-child (window)
96 "Return last child window of WINDOW."
97 (when (and (windowp window) (setq window (window-child window)))
98 (while (window-next window)
99 (setq window (window-next window))))
100 window)
101
102(defsubst window-any-p (object)
103 "Return t if OBJECT denotes a live or internal window."
104 (and (windowp object)
105 (or (window-buffer object) (window-child object))
106 t))
107
108;; The following four functions should probably go to subr.el.
109(defsubst normalize-live-buffer (buffer-or-name)
110 "Return buffer specified by BUFFER-OR-NAME.
111BUFFER-OR-NAME must be either a buffer or a string naming a live
112buffer and defaults to the current buffer."
113 (cond
114 ((not buffer-or-name)
115 (current-buffer))
116 ((bufferp buffer-or-name)
117 (if (buffer-live-p buffer-or-name)
118 buffer-or-name
119 (error "Buffer %s is not a live buffer" buffer-or-name)))
120 ((get-buffer buffer-or-name))
121 (t
122 (error "No such buffer %s" buffer-or-name))))
123
124(defsubst normalize-live-frame (frame)
125 "Return frame specified by FRAME.
126FRAME must be a live frame and defaults to the selected frame."
127 (if frame
128 (if (frame-live-p frame)
129 frame
130 (error "%s is not a live frame" frame))
131 (selected-frame)))
132
133(defsubst normalize-any-window (window)
134 "Return window specified by WINDOW.
135WINDOW must be a window that has not been deleted and defaults to
136the selected window."
137 (if window
138 (if (window-any-p window)
139 window
140 (error "%s is not a window" window))
141 (selected-window)))
142
143(defsubst normalize-live-window (window)
144 "Return live window specified by WINDOW.
145WINDOW must be a live window and defaults to the selected one."
146 (if window
147 (if (and (windowp window) (window-buffer window))
148 window
149 (error "%s is not a live window" window))
150 (selected-window)))
151
152(defvar ignore-window-parameters nil
153 "If non-nil, standard functions ignore window parameters.
154The functions currently affected by this are `split-window',
155`delete-window', `delete-other-windows' and `other-window'.
156
157An application may bind this to a non-nil value around calls to
158these functions to inhibit processing of window parameters.")
159
a1511caf
MR
160(defconst window-safe-min-height 1
161 "The absolut minimum number of lines of a window.
162Anything less might crash Emacs.")
163
562dd5e9
MR
164(defcustom window-min-height 4
165 "The minimum number of lines of any window.
166The value has to accomodate a mode- or header-line if present. A
167value less than `window-safe-min-height' is ignored. The value
168of this variable is honored when windows are resized or split.
169
170Applications should never rebind this variable. To resize a
171window to a height less than the one specified here, an
172application should instead call `resize-window' with a non-nil
173IGNORE argument. In order to have `split-window' make a window
174shorter, explictly specify the SIZE argument of that function."
175 :type 'integer
176 :version "24.1"
177 :group 'windows)
178
a1511caf
MR
179(defconst window-safe-min-width 2
180 "The absolut minimum number of columns of a window.
181Anything less might crash Emacs.")
182
562dd5e9
MR
183(defcustom window-min-width 10
184 "The minimum number of columns of any window.
185The value has to accomodate margins, fringes, or scrollbars if
186present. A value less than `window-safe-min-width' is ignored.
187The value of this variable is honored when windows are resized or
188split.
189
190Applications should never rebind this variable. To resize a
191window to a width less than the one specified here, an
192application should instead call `resize-window' with a non-nil
193IGNORE argument. In order to have `split-window' make a window
194narrower, explictly specify the SIZE argument of that function."
195 :type 'integer
196 :version "24.1"
197 :group 'windows)
198
85cc1f11
MR
199(defun window-iso-combination-p (&optional window horizontal)
200 "If WINDOW is a vertical combination return WINDOW's first child.
201WINDOW can be any window and defaults to the selected one.
202Optional argument HORIZONTAL non-nil means return WINDOW's first
203child if WINDOW is a horizontal combination."
204 (setq window (normalize-any-window window))
205 (if horizontal
206 (window-hchild window)
207 (window-vchild window)))
208
209(defsubst window-iso-combined-p (&optional window horizontal)
210 "Return non-nil if and only if WINDOW is vertically combined.
211WINDOW can be any window and defaults to the selected one.
212Optional argument HORIZONTAL non-nil means return non-nil if and
213only if WINDOW is horizontally combined."
214 (setq window (normalize-any-window window))
215 (let ((parent (window-parent window)))
216 (and parent (window-iso-combination-p parent horizontal))))
217
218(defun window-iso-combinations (&optional window horizontal)
219 "Return largest number of vertically arranged subwindows of WINDOW.
220WINDOW can be any window and defaults to the selected one.
221Optional argument HORIZONTAL non-nil means to return the largest
222number of horizontally arranged subwindows of WINDOW."
223 (setq window (normalize-any-window window))
224 (cond
225 ((window-live-p window)
226 ;; If WINDOW is live, return 1.
227 1)
228 ((window-iso-combination-p window horizontal)
229 ;; If WINDOW is iso-combined, return the sum of the values for all
230 ;; subwindows of WINDOW.
231 (let ((child (window-child window))
232 (count 0))
233 (while child
234 (setq count
235 (+ (window-iso-combinations child horizontal)
236 count))
237 (setq child (window-right child)))
238 count))
239 (t
240 ;; If WINDOW is not iso-combined, return the maximum value of any
241 ;; subwindow of WINDOW.
242 (let ((child (window-child window))
243 (count 1))
244 (while child
245 (setq count
246 (max (window-iso-combinations child horizontal)
247 count))
248 (setq child (window-right child)))
249 count))))
250
251(defun walk-window-tree-1 (proc walk-window-tree-window any &optional sub-only)
252 "Helper function for `walk-window-tree' and `walk-window-subtree'."
253 (let (walk-window-tree-buffer)
254 (while walk-window-tree-window
255 (setq walk-window-tree-buffer
256 (window-buffer walk-window-tree-window))
257 (when (or walk-window-tree-buffer any)
258 (funcall proc walk-window-tree-window))
259 (unless walk-window-tree-buffer
260 (walk-window-tree-1
261 proc (window-hchild walk-window-tree-window) any)
262 (walk-window-tree-1
263 proc (window-vchild walk-window-tree-window) any))
264 (if sub-only
265 (setq walk-window-tree-window nil)
266 (setq walk-window-tree-window
267 (window-right walk-window-tree-window))))))
268
269(defun walk-window-tree (proc &optional frame any)
270 "Run function PROC on each live window of FRAME.
271PROC must be a function with one argument - a window. FRAME must
272be a live frame and defaults to the selected one. ANY, if
273non-nil means to run PROC on all live and internal windows of
274FRAME.
275
276This function performs a pre-order, depth-first traversal of the
277window tree. If PROC changes the window tree, the result is
278unpredictable."
279 (let ((walk-window-tree-frame (normalize-live-frame frame)))
280 (walk-window-tree-1
281 proc (frame-root-window walk-window-tree-frame) any)))
282
283(defun walk-window-subtree (proc &optional window any)
284 "Run function PROC on each live subwindow of WINDOW.
285WINDOW defaults to the selected window. PROC must be a function
286with one argument - a window. ANY, if non-nil means to run PROC
287on all live and internal subwindows of WINDOW.
288
289This function performs a pre-order, depth-first traversal of the
290window tree rooted at WINDOW. If PROC changes that window tree,
291the result is unpredictable."
292 (setq window (normalize-any-window window))
293 (walk-window-tree-1 proc window any t))
294
295(defun windows-with-parameter (parameter &optional value frame any values)
296 "Return a list of all windows on FRAME with PARAMETER non-nil.
297FRAME defaults to the selected frame. Optional argument VALUE
298non-nil means only return windows whose window-parameter value of
299PARAMETER equals VALUE \(comparison is done using `equal').
300Optional argument ANY non-nil means consider internal windows
301too. Optional argument VALUES non-nil means return a list of cons
302cells whose car is the value of the parameter and whose cdr is
303the window."
304 (let (this-value windows)
305 (walk-window-tree
306 (lambda (window)
307 (when (and (setq this-value (window-parameter window parameter))
308 (or (not value) (or (equal value this-value))))
309 (setq windows
310 (if values
311 (cons (cons this-value window) windows)
312 (cons window windows)))))
313 frame any)
314
315 (nreverse windows)))
316
317(defun window-with-parameter (parameter &optional value frame any)
318 "Return first window on FRAME with PARAMETER non-nil.
319FRAME defaults to the selected frame. Optional argument VALUE
320non-nil means only return a window whose window-parameter value
321for PARAMETER equals VALUE \(comparison is done with `equal').
322Optional argument ANY non-nil means consider internal windows
323too."
324 (let (this-value windows)
325 (catch 'found
326 (walk-window-tree
327 (lambda (window)
328 (when (and (setq this-value (window-parameter window parameter))
329 (or (not value) (equal value this-value)))
330 (throw 'found window)))
331 frame any))))
332
333;;; Atomic windows.
334(defun window-atom-root (&optional window)
335 "Return root of atomic window WINDOW is a part of.
336WINDOW can be any window and defaults to the selected one.
337Return nil if WINDOW is not part of a atomic window."
338 (setq window (normalize-any-window window))
339 (let (root)
340 (while (and window (window-parameter window 'window-atom))
341 (setq root window)
342 (setq window (window-parent window)))
343 root))
344
345(defun make-window-atom (window)
346 "Make WINDOW an atomic window.
347WINDOW must be an internal window. Return WINDOW."
348 (if (not (window-child window))
349 (error "Window %s is not an internal window" window)
350 (walk-window-subtree
351 (lambda (window)
352 (set-window-parameter window 'window-atom t))
353 window t)
354 window))
355
356(defun window-atom-check-1 (window)
357 "Subroutine of `window-atom-check'."
358 (when window
359 (if (window-parameter window 'window-atom)
360 (let ((count 0))
361 (when (or (catch 'reset
362 (walk-window-subtree
363 (lambda (window)
364 (if (window-parameter window 'window-atom)
365 (setq count (1+ count))
366 (throw 'reset t)))
367 window t))
368 ;; count >= 1 must hold here. If there's no other
369 ;; window around dissolve this atomic window.
370 (= count 1))
371 ;; Dissolve atomic window.
372 (walk-window-subtree
373 (lambda (window)
374 (set-window-parameter window 'window-atom nil))
375 window t)))
376 ;; Check children.
377 (unless (window-buffer window)
378 (window-atom-check-1 (window-hchild window))
379 (window-atom-check-1 (window-vchild window))))
380 ;; Check right sibling
381 (window-atom-check-1 (window-right window))))
382
383(defun window-atom-check (&optional frame)
384 "Check atomicity of all windows on FRAME.
385FRAME defaults to the selected frame. If an atomic window is
386wrongly configured, reset the atomicity of all its subwindows to
387nil. An atomic window is wrongly configured if it has no
388subwindows or one of its subwindows is not atomic."
389 (window-atom-check-1 (frame-root-window frame)))
390
391;; Side windows.
392(defvar window-sides '(left top right bottom)
393 "Window sides.")
394
395(defcustom window-sides-vertical nil
396 "If non-nil, left and right side windows are full height.
397Otherwise, top and bottom side windows are full width."
398 :type 'boolean
399 :group 'windows
400 :version "24.1")
401
402(defcustom window-sides-slots '(nil nil nil nil)
403 "Maximum number of side window slots.
404The value is a list of four elements specifying the number of
405side window slots on \(in this order) the left, top, right and
406bottom side of each frame. If an element is a number, this means
407to display at most that many side windows on the corresponding
408side. If an element is nil, this means there's no bound on the
409number of slots on that side."
410 :risky t
411 :type
412 '(list
413 :value (nil nil nil nil)
414 (choice
415 :tag "Left"
416 :help-echo "Maximum slots of left side window."
417 :value nil
418 :format "%[Left%] %v\n"
419 (const :tag "Unlimited" :format "%t" nil)
420 (integer :tag "Number" :value 2 :size 5))
421 (choice
422 :tag "Top"
423 :help-echo "Maximum slots of top side window."
424 :value nil
425 :format "%[Top%] %v\n"
426 (const :tag "Unlimited" :format "%t" nil)
427 (integer :tag "Number" :value 3 :size 5))
428 (choice
429 :tag "Right"
430 :help-echo "Maximum slots of right side window."
431 :value nil
432 :format "%[Right%] %v\n"
433 (const :tag "Unlimited" :format "%t" nil)
434 (integer :tag "Number" :value 2 :size 5))
435 (choice
436 :tag "Bottom"
437 :help-echo "Maximum slots of bottom side window."
438 :value nil
439 :format "%[Bottom%] %v\n"
440 (const :tag "Unlimited" :format "%t" nil)
441 (integer :tag "Number" :value 3 :size 5)))
442 :group 'windows)
443
444(defun window-side-check (&optional frame)
445 "Check the window-side parameter of all windows on FRAME.
446FRAME defaults to the selected frame. If the configuration is
447invalid, reset all window-side parameters to nil.
448
449A valid configuration has to preserve the following invariant:
450
451- If a window has a non-nil window-side parameter, it must have a
452 parent window and the parent window's window-side parameter
453 must be either nil or the same as for window.
454
455- If windows with non-nil window-side parameters exist, there
456 must be at most one window of each side and non-side with a
457 parent whose window-side parameter is nil and there must be no
458 leaf window whose window-side parameter is nil."
459 (let (normal none left top right bottom
460 side parent parent-side code)
461 (when (or (catch 'reset
462 (walk-window-tree
463 (lambda (window)
464 (setq side (window-parameter window 'window-side))
465 (setq parent (window-parent window))
466 (setq parent-side
467 (and parent (window-parameter parent 'window-side)))
468 ;; The following `cond' seems a bit tedious, but I'd
469 ;; rather stick to using just the stack.
470 (cond
471 (parent-side
472 (when (not (eq parent-side side))
473 ;; A parent whose window-side is non-nil must
474 ;; have a child with the same window-side.
475 (throw 'reset t)))
476 ;; Now check that there's more than one main window
477 ;; for any of none, left, top, right and bottom.
478 ((eq side 'none)
479 (if none
480 (throw 'reset t)
481 (setq none t)))
482 ((eq side 'left)
483 (if left
484 (throw 'reset t)
485 (setq left t)))
486 ((eq side 'top)
487 (if top
488 (throw 'reset t)
489 (setq top t)))
490 ((eq side 'right)
491 (if right
492 (throw 'reset t)
493 (setq right t)))
494 ((eq side 'bottom)
495 (if bottom
496 (throw 'reset t)
497 (setq bottom t)))
498 ((window-buffer window)
499 ;; A leaf window without window-side parameter,
500 ;; record its existence.
501 (setq normal t))))
502 frame t))
503 (if none
504 ;; At least one non-side window exists, so there must
505 ;; be at least one side-window and no normal window.
506 (or (not (or left top right bottom)) normal)
507 ;; No non-side window exists, so there must be no side
508 ;; window either.
509 (or left top right bottom)))
510 (walk-window-tree
511 (lambda (window)
512 (set-window-parameter window 'window-side nil))
513 frame t))))
514
515(defun window-check (&optional frame)
516 "Check atomic and side windows on FRAME.
517FRAME defaults to the selected frame."
518 (window-side-check frame)
519 (window-atom-check frame))
520
521;;; Window sizes.
522(defvar window-size-fixed nil
523 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
524If the value is `height', then only the window's height is fixed.
525If the value is `width', then only the window's width is fixed.
526Any other non-nil value fixes both the width and the height.
527
528Emacs won't change the size of any window displaying that buffer,
529unless it has no other choice \(like when deleting a neighboring
530window).")
531(make-variable-buffer-local 'window-size-fixed)
532
a1511caf
MR
533(defsubst window-size-ignore (window ignore)
534 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
535 (if (window-any-p ignore) (eq window ignore) ignore))
536
537(defun window-min-size (&optional window horizontal ignore)
538 "Return the minimum number of lines of WINDOW.
539WINDOW can be an arbitrary window and defaults to the selected
540one. Optional argument HORIZONTAL non-nil means return the
541minimum number of columns of WINDOW.
542
543Optional argument IGNORE non-nil means ignore any restrictions
544imposed by fixed size windows, `window-min-height' or
545`window-min-width' settings. IGNORE equal `safe' means live
546windows may get as small as `window-safe-min-height' lines and
547`window-safe-min-width' columns. IGNORE a window means ignore
548restrictions for that window only."
549 (window-min-size-1
550 (normalize-any-window window) horizontal ignore))
551
552(defun window-min-size-1 (window horizontal ignore)
553 "Internal function of `window-min-size'."
554 (let ((sub (window-child window)))
555 (if sub
556 (let ((value 0))
557 ;; WINDOW is an internal window.
558 (if (window-iso-combined-p sub horizontal)
559 ;; The minimum size of an iso-combination is the sum of
560 ;; the minimum sizes of its subwindows.
561 (while sub
562 (setq value (+ value
563 (window-min-size-1 sub horizontal ignore)))
564 (setq sub (window-right sub)))
565 ;; The minimum size of an ortho-combination is the maximum of
566 ;; the minimum sizes of its subwindows.
567 (while sub
568 (setq value (max value
569 (window-min-size-1 sub horizontal ignore)))
570 (setq sub (window-right sub))))
571 value)
572 (with-current-buffer (window-buffer window)
573 (cond
574 ((and (not (window-size-ignore window ignore))
575 (window-size-fixed-p window horizontal))
576 ;; The minimum size of a fixed size window is its size.
577 (window-total-size window horizontal))
578 ((or (eq ignore 'safe) (eq ignore window))
579 ;; If IGNORE equals `safe' or WINDOW return the safe values.
580 (if horizontal window-safe-min-width window-safe-min-height))
581 (horizontal
582 ;; For the minimum width of a window take fringes and
583 ;; scroll-bars into account. This is questionable and should
584 ;; be removed as soon as we are able to split (and resize)
585 ;; windows such that the new (or resized) windows can get a
586 ;; size less than the user-specified `window-min-height' and
587 ;; `window-min-width'.
588 (let ((frame (window-frame window))
589 (fringes (window-fringes window))
590 (scroll-bars (window-scroll-bars window)))
591 (max
592 (+ window-safe-min-width
593 (ceiling (car fringes) (frame-char-width frame))
594 (ceiling (cadr fringes) (frame-char-width frame))
595 (cond
596 ((memq (nth 2 scroll-bars) '(left right))
597 (nth 1 scroll-bars))
598 ((memq (frame-parameter frame 'vertical-scroll-bars)
599 '(left right))
600 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
601 (frame-char-width)))
602 (t 0)))
603 (if (and (not (window-size-ignore window ignore))
604 (numberp window-min-width))
605 window-min-width
606 0))))
607 (t
608 ;; For the minimum height of a window take any mode- or
609 ;; header-line into account.
610 (max (+ window-safe-min-height
611 (if header-line-format 1 0)
612 (if mode-line-format 1 0))
613 (if (and (not (window-size-ignore window ignore))
614 (numberp window-min-height))
615 window-min-height
616 0))))))))
617
618(defun window-sizable (window delta &optional horizontal ignore)
619 "Return DELTA if DELTA lines can be added to WINDOW.
620Optional argument HORIZONTAL non-nil means return DELTA if DELTA
621columns can be added to WINDOW. A return value of zero means
622that no lines (or columns) can be added to WINDOW.
623
624This function looks only at WINDOW and its subwindows. The
625function `window-resizable' looks at other windows as well.
626
627DELTA positive means WINDOW shall be enlarged by DELTA lines or
628columns. If WINDOW cannot be enlarged by DELTA lines or columns
629return the maximum value in the range 0..DELTA by which WINDOW
630can be enlarged.
631
632DELTA negative means WINDOW shall be shrunk by -DELTA lines or
633columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
634return the minimum value in the range DELTA..0 by which WINDOW
635can be shrunk.
636
637Optional argument IGNORE non-nil means ignore any restrictions
638imposed by fixed size windows, `window-min-height' or
639`window-min-width' settings. IGNORE equal `safe' means live
640windows may get as small as `window-safe-min-height' lines and
641`window-safe-min-width' columns. IGNORE any window means ignore
642restrictions for that window only."
643 (setq window (normalize-any-window window))
644 (cond
645 ((< delta 0)
646 (max (- (window-min-size window horizontal ignore)
647 (window-total-size window horizontal))
648 delta))
649 ((window-size-ignore window ignore)
650 delta)
651 ((> delta 0)
652 (if (window-size-fixed-p window horizontal)
653 0
654 delta))
655 (t 0)))
656
657(defsubst window-sizable-p (window delta &optional horizontal ignore)
658 "Return t if WINDOW can be resized by DELTA lines.
659For the meaning of the arguments of this function see the
660doc-string of `window-sizable'."
661 (setq window (normalize-any-window window))
662 (if (> delta 0)
663 (>= (window-sizable window delta horizontal ignore) delta)
664 (<= (window-sizable window delta horizontal ignore) delta)))
665
666(defun window-size-fixed-1 (window horizontal)
667 "Internal function for `window-size-fixed-p'."
668 (let ((sub (window-child window)))
669 (catch 'fixed
670 (if sub
671 ;; WINDOW is an internal window.
672 (if (window-iso-combined-p sub horizontal)
673 ;; An iso-combination is fixed size if all its subwindows
674 ;; are fixed-size.
675 (progn
676 (while sub
677 (unless (window-size-fixed-1 sub horizontal)
678 ;; We found a non-fixed-size subwindow, so WINDOW's
679 ;; size is not fixed.
680 (throw 'fixed nil))
681 (setq sub (window-right sub)))
682 ;; All subwindows are fixed-size, so WINDOW's size is
683 ;; fixed.
684 (throw 'fixed t))
685 ;; An ortho-combination is fixed-size if at least one of its
686 ;; subwindows is fixed-size.
687 (while sub
688 (when (window-size-fixed-1 sub horizontal)
689 ;; We found a fixed-size subwindow, so WINDOW's size is
690 ;; fixed.
691 (throw 'fixed t))
692 (setq sub (window-right sub))))
693 ;; WINDOW is a live window.
694 (with-current-buffer (window-buffer window)
695 (if horizontal
696 (memq window-size-fixed '(width t))
697 (memq window-size-fixed '(height t))))))))
698
699(defun window-size-fixed-p (&optional window horizontal)
700 "Return non-nil if WINDOW's height is fixed.
701WINDOW can be an arbitrary window and defaults to the selected
702window. Optional argument HORIZONTAL non-nil means return
703non-nil if WINDOW's width is fixed.
704
705If this function returns nil, this does not necessarily mean that
706WINDOW can be resized in the desired direction. The functions
707`window-resizable' and `window-resizable-p' will tell that."
708 (window-size-fixed-1
709 (normalize-any-window window) horizontal))
710
711(defun window-min-delta-1 (window delta &optional horizontal ignore trail noup)
712 "Internal function for `window-min-delta'."
713 (if (not (window-parent window))
714 ;; If we can't go up, return zero.
715 0
716 ;; Else try to find a non-fixed-size sibling of WINDOW.
717 (let* ((parent (window-parent window))
718 (sub (window-child parent)))
719 (catch 'done
720 (if (window-iso-combined-p sub horizontal)
721 ;; In an iso-combination throw DELTA if we find at least one
722 ;; subwindow and that subwindow is either not of fixed-size
723 ;; or we can ignore fixed-sizeness.
724 (let ((skip (eq trail 'after)))
725 (while sub
726 (cond
727 ((eq sub window)
728 (setq skip (eq trail 'before)))
729 (skip)
730 ((and (not (window-size-ignore window ignore))
731 (window-size-fixed-p sub horizontal)))
732 (t
733 ;; We found a non-fixed-size subwindow.
734 (throw 'done delta)))
735 (setq sub (window-right sub))))
736 ;; In an ortho-combination set DELTA to the minimum value by
737 ;; which other subwindows can shrink.
738 (while sub
739 (unless (eq sub window)
740 (setq delta
741 (min delta
742 (- (window-total-size sub horizontal)
743 (window-min-size sub horizontal ignore)))))
744 (setq sub (window-right sub))))
745 (if noup
746 delta
747 (window-min-delta-1 parent delta horizontal ignore trail))))))
748
749(defun window-min-delta (&optional window horizontal ignore trail noup nodown)
750 "Return number of lines by which WINDOW can be shrunk.
751WINDOW can be an arbitrary window and defaults to the selected
752window. Return zero if WINDOW cannot be shrunk.
753
754Optional argument HORIZONTAL non-nil means return number of
755columns by which WINDOW can be shrunk.
756
757Optional argument IGNORE non-nil means ignore any restrictions
758imposed by fixed size windows, `window-min-height' or
759`window-min-width' settings. IGNORE a window means ignore
760restrictions for that window only. IGNORE equal `safe' means
761live windows may get as small as `window-safe-min-height' lines
762and `window-safe-min-width' columns.
763
764Optional argument TRAIL `before' means only windows to the left
765of or above WINDOW can be enlarged. Optional argument TRAIL
766`after' means only windows to the right of or below WINDOW can be
767enlarged.
768
769Optional argument NOUP non-nil means don't go up in the window
770tree but try to enlarge windows within WINDOW's combination only.
771
772Optional argument NODOWN non-nil means don't check whether WINDOW
773itself \(and its subwindows) can be shrunk; check only whether at
774least one other windows can be enlarged appropriately."
775 (setq window (normalize-any-window window))
776 (let ((size (window-total-size window horizontal))
777 (minimum (window-min-size window horizontal ignore)))
778 (cond
779 (nodown
780 ;; If NODOWN is t, try to recover the entire size of WINDOW.
781 (window-min-delta-1 window size horizontal ignore trail noup))
782 ((= size minimum)
783 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
784 ;; there's nothing to recover.
785 0)
786 (t
787 ;; Otherwise, try to recover whatever WINDOW is larger than its
788 ;; minimum size.
789 (window-min-delta-1
790 window (- size minimum) horizontal ignore trail noup)))))
791
792(defun window-max-delta-1 (window delta &optional horizontal ignore trail noup)
793 "Internal function of `window-max-delta'."
794 (if (not (window-parent window))
795 ;; Can't go up. Return DELTA.
796 delta
797 (let* ((parent (window-parent window))
798 (sub (window-child parent)))
799 (catch 'fixed
800 (if (window-iso-combined-p sub horizontal)
801 ;; For an iso-combination calculate how much we can get from
802 ;; other subwindows.
803 (let ((skip (eq trail 'after)))
804 (while sub
805 (cond
806 ((eq sub window)
807 (setq skip (eq trail 'before)))
808 (skip)
809 (t
810 (setq delta
811 (+ delta
812 (- (window-total-size sub horizontal)
813 (window-min-size sub horizontal ignore))))))
814 (setq sub (window-right sub))))
815 ;; For an ortho-combination throw DELTA when at least one
816 ;; subwindow is fixed-size.
817 (while sub
818 (when (and (not (eq sub window))
819 (not (window-size-ignore sub ignore))
820 (window-size-fixed-p sub horizontal))
821 (throw 'fixed delta))
822 (setq sub (window-right sub))))
823 (if noup
824 ;; When NOUP is nil, DELTA is all we can get.
825 delta
826 ;; Else try with parent of WINDOW, passing the DELTA we
827 ;; recovered so far.
828 (window-max-delta-1 parent delta horizontal ignore trail))))))
829
830(defun window-max-delta (&optional window horizontal ignore trail noup nodown)
831 "Return maximum number of lines WINDOW by which WINDOW can be enlarged.
832WINDOW can be an arbitrary window and defaults to the selected
833window. The return value is zero if WINDOW cannot be enlarged.
834
835Optional argument HORIZONTAL non-nil means return maximum number
836of columns by which WINDOW can be enlarged.
837
838Optional argument IGNORE non-nil means ignore any restrictions
839imposed by fixed size windows, `window-min-height' or
840`window-min-width' settings. IGNORE a window means ignore
841restrictions for that window only. IGNORE equal `safe' means
842live windows may get as small as `window-safe-min-height' lines
843and `window-safe-min-width' columns.
844
845Optional argument TRAIL `before' means only windows to the left
846of or below WINDOW can be shrunk. Optional argument TRAIL
847`after' means only windows to the right of or above WINDOW can be
848shrunk.
849
850Optional argument NOUP non-nil means don't go up in the window
851tree but try to obtain the entire space from windows within
852WINDOW's combination.
853
854Optional argument NODOWN non-nil means do not check whether
855WINDOW itself \(and its subwindows) can be enlarged; check only
856whether other windows can be shrunk appropriately."
857 (setq window (normalize-any-window window))
858 (if (and (not (window-size-ignore window ignore))
859 (not nodown) (window-size-fixed-p window horizontal))
860 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
861 ;; size.
862 0
863 ;; WINDOW has no fixed size.
864 (window-max-delta-1 window 0 horizontal ignore trail noup)))
865
866;; Make NOUP also inhibit the min-size check.
867(defun window-resizable (window delta &optional horizontal ignore trail noup nodown)
868 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
869Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
870can be resized horizontally by DELTA columns. A return value of
871zero means that WINDOW is not resizable.
872
873DELTA positive means WINDOW shall be enlarged by DELTA lines or
874columns. If WINDOW cannot be enlarged by DELTA lines or columns
875return the maximum value in the range 0..DELTA by which WINDOW
876can be enlarged.
877
878DELTA negative means WINDOW shall be shrunk by -DELTA lines or
879columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
880return the minimum value in the range DELTA..0 that can be used
881for shrinking WINDOW.
882
883Optional argument IGNORE non-nil means ignore any restrictions
884imposed by fixed size windows, `window-min-height' or
885`window-min-width' settings. IGNORE a window means ignore
886restrictions for that window only. IGNORE equal `safe' means
887live windows may get as small as `window-safe-min-height' lines
888and `window-safe-min-width' columns.
889
890Optional argument TRAIL `before' means only windows to the left
891of or below WINDOW can be shrunk. Optional argument TRAIL
892`after' means only windows to the right of or above WINDOW can be
893shrunk.
894
895Optional argument NOUP non-nil means don't go up in the window
896tree but try to distribute the space among the other windows
897within WINDOW's combination.
898
899Optional argument NODOWN non-nil means don't check whether WINDOW
900and its subwindows can be resized."
901 (setq window (normalize-any-window window))
902 (cond
903 ((< delta 0)
904 (max (- (window-min-delta window horizontal ignore trail noup nodown))
905 delta))
906 ((> delta 0)
907 (min (window-max-delta window horizontal ignore trail noup nodown)
908 delta))
909 (t 0)))
910
911(defun window-resizable-p (window delta &optional horizontal ignore trail noup nodown)
912 "Return t if WINDOW can be resized vertically by DELTA lines.
913For the meaning of the arguments of this function see the
914doc-string of `window-resizable'."
915 (setq window (normalize-any-window window))
916 (if (> delta 0)
917 (>= (window-resizable window delta horizontal ignore trail noup nodown)
918 delta)
919 (<= (window-resizable window delta horizontal ignore trail noup nodown)
920 delta)))
921
922(defsubst window-total-height (&optional window)
923 "Return the total number of lines of WINDOW.
924WINDOW can be any window and defaults to the selected one. The
925return value includes WINDOW's mode line and header line, if any.
926If WINDOW is internal the return value is the sum of the total
927number of lines of WINDOW's child windows if these are vertically
928combined and the height of WINDOW's first child otherwise.
929
930Note: This function does not take into account the value of
931`line-spacing' when calculating the number of lines in WINDOW."
932 (window-total-size window))
3c448ab6 933
f3d1777e
MR
934;; Eventually we should make `window-height' obsolete.
935(defalias 'window-height 'window-total-height)
936
ccafbf06 937;; See discussion in bug#4543.
a1511caf
MR
938(defsubst window-full-height-p (&optional window)
939 "Return t if WINDOW is as high as the containing frame.
940More precisely, return t if and only if the total height of
941WINDOW equals the total height of the root window of WINDOW's
942frame. WINDOW can be any window and defaults to the selected
943one."
944 (setq window (normalize-any-window window))
945 (= (window-total-size window)
946 (window-total-size (frame-root-window window))))
947
948(defsubst window-total-width (&optional window)
949 "Return the total number of columns of WINDOW.
950WINDOW can be any window and defaults to the selected one. The
951return value includes any vertical dividers or scrollbars of
952WINDOW. If WINDOW is internal, the return value is the sum of
953the total number of columns of WINDOW's child windows if these
954are horizontally combined and the width of WINDOW's first child
955otherwise."
956 (window-total-size window t))
957
958(defsubst window-full-width-p (&optional window)
959 "Return t if WINDOW is as wide as the containing frame.
960More precisely, return t if and only if the total width of WINDOW
961equals the total width of the root window of WINDOW's frame.
962WINDOW can be any window and defaults to the selected one."
963 (setq window (normalize-any-window window))
964 (= (window-total-size window t)
965 (window-total-size (frame-root-window window) t)))
966
967(defsubst window-body-height (&optional window)
968 "Return the number of lines of WINDOW's body.
969WINDOW must be a live window and defaults to the selected one.
970
971The return value does not include WINDOW's mode line and header
972line, if any. If a line at the bottom of the window is only
973partially visible, that line is included in the return value. If
974you do not want to include a partially visible bottom line in the
975return value, use `window-text-height' instead."
976 (window-body-size window))
977
978(defsubst window-body-width (&optional window)
979 "Return the number of columns of WINDOW's body.
980WINDOW must be a live window and defaults to the selected one.
981
982The return value does not include any vertical dividers or scroll
983bars owned by WINDOW. On a window-system the return value does
984not include the number of columns used for WINDOW's fringes or
985display margins either."
986 (window-body-size window t))
02c6f098 987
f3d1777e
MR
988;; Eventually we should make `window-height' obsolete.
989(defalias 'window-width 'window-body-width)
990
3c448ab6
MR
991(defun window-current-scroll-bars (&optional window)
992 "Return the current scroll bar settings for WINDOW.
387522b2 993WINDOW must be a live window and defaults to the selected one.
3c448ab6
MR
994
995The return value is a cons cell (VERTICAL . HORIZONTAL) where
996VERTICAL specifies the current location of the vertical scroll
997bars (`left', `right', or nil), and HORIZONTAL specifies the
998current location of the horizontal scroll bars (`top', `bottom',
999or nil).
1000
1001Unlike `window-scroll-bars', this function reports the scroll bar
1002type actually used, once frame defaults and `scroll-bar-mode' are
1003taken into account."
387522b2 1004 (setq window (normalize-live-window window))
3c448ab6
MR
1005 (let ((vert (nth 2 (window-scroll-bars window)))
1006 (hor nil))
1007 (when (or (eq vert t) (eq hor t))
387522b2 1008 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
3c448ab6
MR
1009 (if (eq vert t)
1010 (setq vert (car fcsb)))
1011 (if (eq hor t)
1012 (setq hor (cdr fcsb)))))
1013 (cons vert hor)))
1014
1015(defun walk-windows (proc &optional minibuf all-frames)
387522b2 1016 "Cycle through all live windows, calling PROC for each one.
3c448ab6
MR
1017PROC must specify a function with a window as its sole argument.
1018The optional arguments MINIBUF and ALL-FRAMES specify the set of
387522b2 1019windows to include in the walk.
3c448ab6
MR
1020
1021MINIBUF t means include the minibuffer window even if the
1022minibuffer is not active. MINIBUF nil or omitted means include
1023the minibuffer window only if the minibuffer is active. Any
1024other value means do not include the minibuffer window even if
1025the minibuffer is active.
1026
387522b2
MR
1027ALL-FRAMES nil or omitted means consider all windows on the
1028selected frame, plus the minibuffer window if specified by the
1029MINIBUF argument. If the minibuffer counts, consider all windows
1030on all frames that share that minibuffer too. The following
1031non-nil values of ALL-FRAMES have special meanings:
1032
1033- t means consider all windows on all existing frames.
1034
1035- `visible' means consider all windows on all visible frames on
1036 the current terminal.
1037
1038- 0 (the number zero) means consider all windows on all visible
1039 and iconified frames on the current terminal.
1040
1041- A frame means consider all windows on that frame only.
1042
1043Anything else means consider all windows on the selected frame
1044and no others.
3c448ab6
MR
1045
1046This function changes neither the order of recently selected
1047windows nor the buffer list."
1048 ;; If we start from the minibuffer window, don't fail to come
1049 ;; back to it.
1050 (when (window-minibuffer-p (selected-window))
1051 (setq minibuf t))
1052 ;; Make sure to not mess up the order of recently selected
1053 ;; windows. Use `save-selected-window' and `select-window'
1054 ;; with second argument non-nil for this purpose.
1055 (save-selected-window
1056 (when (framep all-frames)
1057 (select-window (frame-first-window all-frames) 'norecord))
387522b2
MR
1058 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1059 (funcall proc walk-windows-window))))
1060
1061(defun window-in-direction-2 (window posn &optional horizontal)
1062 "Support function for `window-in-direction'."
1063 (if horizontal
1064 (let ((top (window-top-line window)))
1065 (if (> top posn)
1066 (- top posn)
1067 (- posn top (window-total-height window))))
1068 (let ((left (window-left-column window)))
1069 (if (> left posn)
1070 (- left posn)
1071 (- posn left (window-total-width window))))))
1072
1073(defun window-in-direction (direction &optional window ignore)
1074 "Return window in DIRECTION as seen from WINDOW.
1075DIRECTION must be one of `above', `below', `left' or `right'.
1076WINDOW must be a live window and defaults to the selected one.
1077IGNORE, when non-nil means a window can be returned even if its
1078`no-other-window' parameter is non-nil."
1079 (setq window (normalize-live-window window))
1080 (unless (memq direction '(above below left right))
1081 (error "Wrong direction %s" direction))
1082 (let* ((frame (window-frame window))
1083 (hor (memq direction '(left right)))
1084 (first (if hor
1085 (window-left-column window)
1086 (window-top-line window)))
1087 (last (+ first (if hor
1088 (window-total-width window)
1089 (window-total-height window))))
1090 (posn-cons (nth 6 (posn-at-point (window-point window) window)))
1091 ;; The column / row value of `posn-at-point' can be nil for the
1092 ;; mini-window, guard against that.
1093 (posn (if hor
1094 (+ (or (cdr posn-cons) 1) (window-top-line window))
1095 (+ (or (car posn-cons) 1) (window-left-column window))))
1096 (best-edge
1097 (cond
1098 ((eq direction 'below) (frame-height frame))
1099 ((eq direction 'right) (frame-width frame))
1100 (t -1)))
1101 (best-edge-2 best-edge)
1102 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1103 best best-2 best-diff-2-new)
1104 (walk-window-tree
1105 (lambda (w)
1106 (let* ((w-top (window-top-line w))
1107 (w-left (window-left-column w)))
1108 (cond
1109 ((or (eq window w)
1110 ;; Ignore ourselves.
1111 (and (window-parameter w 'no-other-window)
1112 ;; Ignore W unless IGNORE is non-nil.
1113 (not ignore))))
1114 (hor
1115 (cond
1116 ((and (<= w-top posn)
1117 (< posn (+ w-top (window-total-height w))))
1118 ;; W is to the left or right of WINDOW and covers POSN.
1119 (when (or (and (eq direction 'left)
1120 (<= w-left first) (> w-left best-edge))
1121 (and (eq direction 'right)
1122 (>= w-left last) (< w-left best-edge)))
1123 (setq best-edge w-left)
1124 (setq best w)))
1125 ((and (or (and (eq direction 'left)
1126 (<= (+ w-left (window-total-width w)) first))
1127 (and (eq direction 'right) (<= last w-left)))
1128 ;; W is to the left or right of WINDOW but does not
1129 ;; cover POSN.
1130 (setq best-diff-2-new
1131 (window-in-direction-2 w posn hor))
1132 (or (< best-diff-2-new best-diff-2)
1133 (and (= best-diff-2-new best-diff-2)
1134 (if (eq direction 'left)
1135 (> w-left best-edge-2)
1136 (< w-left best-edge-2)))))
1137 (setq best-edge-2 w-left)
1138 (setq best-diff-2 best-diff-2-new)
1139 (setq best-2 w))))
1140 (t
1141 (cond
1142 ((and (<= w-left posn)
1143 (< posn (+ w-left (window-total-width w))))
1144 ;; W is above or below WINDOW and covers POSN.
1145 (when (or (and (eq direction 'above)
1146 (<= w-top first) (> w-top best-edge))
1147 (and (eq direction 'below)
1148 (>= w-top first) (< w-top best-edge)))
1149 (setq best-edge w-top)
1150 (setq best w)))
1151 ((and (or (and (eq direction 'above)
1152 (<= (+ w-top (window-total-height w)) first))
1153 (and (eq direction 'below) (<= last w-top)))
1154 ;; W is above or below WINDOW but does not cover POSN.
1155 (setq best-diff-2-new
1156 (window-in-direction-2 w posn hor))
1157 (or (< best-diff-2-new best-diff-2)
1158 (and (= best-diff-2-new best-diff-2)
1159 (if (eq direction 'above)
1160 (> w-top best-edge-2)
1161 (< w-top best-edge-2)))))
1162 (setq best-edge-2 w-top)
1163 (setq best-diff-2 best-diff-2-new)
1164 (setq best-2 w)))))))
1165 (window-frame window))
1166 (or best best-2)))
3c448ab6
MR
1167
1168(defun get-window-with-predicate (predicate &optional minibuf
1169 all-frames default)
387522b2
MR
1170 "Return a live window satisfying PREDICATE.
1171More precisely, cycle through all windows calling the function
1172PREDICATE on each one of them with the window as its sole
1173argument. Return the first window for which PREDICATE returns
1174non-nil. If no window satisfies PREDICATE, return DEFAULT.
1175
1176ALL-FRAMES nil or omitted means consider all windows on the selected
1177frame, plus the minibuffer window if specified by the MINIBUF
1178argument. If the minibuffer counts, consider all windows on all
1179frames that share that minibuffer too. The following non-nil
1180values of ALL-FRAMES have special meanings:
3c448ab6 1181
387522b2
MR
1182- t means consider all windows on all existing frames.
1183
1184- `visible' means consider all windows on all visible frames on
1185 the current terminal.
1186
1187- 0 (the number zero) means consider all windows on all visible
1188 and iconified frames on the current terminal.
1189
1190- A frame means consider all windows on that frame only.
1191
1192Anything else means consider all windows on the selected frame
1193and no others."
3c448ab6 1194 (catch 'found
387522b2
MR
1195 (dolist (window (window-list-1 nil minibuf all-frames))
1196 (when (funcall predicate window)
1197 (throw 'found window)))
3c448ab6
MR
1198 default))
1199
1200(defalias 'some-window 'get-window-with-predicate)
1201
190b47e6
MR
1202(defun get-lru-window (&optional all-frames dedicated)
1203 "Return the least recently used window on frames specified by ALL-FRAMES.
1204Return a full-width window if possible. A minibuffer window is
1205never a candidate. A dedicated window is never a candidate
1206unless DEDICATED is non-nil, so if all windows are dedicated, the
1207value is nil. Avoid returning the selected window if possible.
1208
1209The following non-nil values of the optional argument ALL-FRAMES
1210have special meanings:
1211
1212- t means consider all windows on all existing frames.
1213
1214- `visible' means consider all windows on all visible frames on
1215 the current terminal.
1216
1217- 0 (the number zero) means consider all windows on all visible
1218 and iconified frames on the current terminal.
1219
1220- A frame means consider all windows on that frame only.
1221
1222Any other value of ALL-FRAMES means consider all windows on the
1223selected frame and no others."
1224 (let (best-window best-time second-best-window second-best-time time)
1225 (dolist (window (window-list-1 nil nil all-frames))
1226 (when (or dedicated (not (window-dedicated-p window)))
1227 (setq time (window-use-time window))
1228 (if (or (eq window (selected-window))
1229 (not (window-full-width-p window)))
1230 (when (or (not second-best-time) (< time second-best-time))
1231 (setq second-best-time time)
1232 (setq second-best-window window))
1233 (when (or (not best-time) (< time best-time))
1234 (setq best-time time)
1235 (setq best-window window)))))
1236 (or best-window second-best-window)))
1237
387522b2
MR
1238(defun get-mru-window (&optional all-frames)
1239 "Return the most recently used window on frames specified by ALL-FRAMES.
1240Do not return a minibuffer window.
1241
1242The following non-nil values of the optional argument ALL-FRAMES
1243have special meanings:
1244
1245- t means consider all windows on all existing frames.
1246
1247- `visible' means consider all windows on all visible frames on
1248 the current terminal.
1249
1250- 0 (the number zero) means consider all windows on all visible
1251 and iconified frames on the current terminal.
1252
1253- A frame means consider all windows on that frame only.
1254
1255Any other value of ALL-FRAMES means consider all windows on the
1256selected frame and no others."
1257 (let (best-window best-time time)
1258 (dolist (window (window-list-1 nil nil all-frames))
1259 (setq time (window-use-time window))
1260 (when (or (not best-time) (> time best-time))
1261 (setq best-time time)
1262 (setq best-window window)))
1263 best-window))
1264
190b47e6
MR
1265(defun get-largest-window (&optional all-frames dedicated)
1266 "Return the largest window on frames specified by ALL-FRAMES.
1267A minibuffer window is never a candidate. A dedicated window is
1268never a candidate unless DEDICATED is non-nil, so if all windows
1269are dedicated, the value is nil.
1270
1271The following non-nil values of the optional argument ALL-FRAMES
1272have special meanings:
1273
1274- t means consider all windows on all existing frames.
1275
1276- `visible' means consider all windows on all visible frames on
1277 the current terminal.
1278
1279- 0 (the number zero) means consider all windows on all visible
1280 and iconified frames on the current terminal.
1281
1282- A frame means consider all windows on that frame only.
1283
1284Any other value of ALL-FRAMES means consider all windows on the
1285selected frame and no others."
1286 (let ((best-size 0)
1287 best-window size)
1288 (dolist (window (window-list-1 nil nil all-frames))
1289 (when (or dedicated (not (window-dedicated-p window)))
1290 (setq size (* (window-total-size window)
1291 (window-total-size window t)))
1292 (when (> size best-size)
1293 (setq best-size size)
1294 (setq best-window window))))
1295 best-window))
1296
3c448ab6
MR
1297(defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1298 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1299BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1300and defaults to the current buffer.
1301
190b47e6
MR
1302Any windows showing BUFFER-OR-NAME on the selected frame are listed
1303first.
1304
1305MINIBUF t means include the minibuffer window even if the
1306minibuffer is not active. MINIBUF nil or omitted means include
1307the minibuffer window only if the minibuffer is active. Any
1308other value means do not include the minibuffer window even if
1309the minibuffer is active.
1310
1311ALL-FRAMES nil or omitted means consider all windows on the
1312selected frame, plus the minibuffer window if specified by the
1313MINIBUF argument. If the minibuffer counts, consider all windows
1314on all frames that share that minibuffer too. The following
1315non-nil values of ALL-FRAMES have special meanings:
1316
1317- t means consider all windows on all existing frames.
1318
1319- `visible' means consider all windows on all visible frames on
1320 the current terminal.
1321
1322- 0 (the number zero) means consider all windows on all visible
1323 and iconified frames on the current terminal.
1324
1325- A frame means consider all windows on that frame only.
1326
1327Anything else means consider all windows on the selected frame
1328and no others."
1329 (let ((buffer (normalize-live-buffer buffer-or-name))
3c448ab6 1330 windows)
190b47e6
MR
1331 (dolist (window (window-list-1 (frame-first-window) minibuf all-frames))
1332 (when (eq (window-buffer window) buffer)
1333 (setq windows (cons window windows))))
1334 (nreverse windows)))
3c448ab6
MR
1335
1336(defun minibuffer-window-active-p (window)
1337 "Return t if WINDOW is the currently active minibuffer window."
1338 (eq window (active-minibuffer-window)))
387522b2 1339
3c448ab6 1340(defun count-windows (&optional minibuf)
387522b2 1341 "Return the number of live windows on the selected frame.
3c448ab6
MR
1342The optional argument MINIBUF specifies whether the minibuffer
1343window shall be counted. See `walk-windows' for the precise
1344meaning of this argument."
387522b2 1345 (length (window-list-1 nil minibuf)))
9aab8e0d
MR
1346\f
1347;;; Resizing windows.
1348(defun resize-window-reset (&optional frame horizontal)
1349 "Reset resize values for all windows on FRAME.
1350FRAME defaults to the selected frame.
1351
1352This function stores the current value of `window-total-size' applied
1353with argument HORIZONTAL in the new total size of all windows on
1354FRAME. It also resets the new normal size of each of these
1355windows."
1356 (resize-window-reset-1
1357 (frame-root-window (normalize-live-frame frame)) horizontal))
1358
1359(defun resize-window-reset-1 (window horizontal)
1360 "Internal function of `resize-window-reset'."
1361 ;; Register old size in the new total size.
1362 (set-window-new-total window (window-total-size window horizontal))
1363 ;; Reset new normal size.
1364 (set-window-new-normal window)
1365 (when (window-child window)
1366 (resize-window-reset-1 (window-child window) horizontal))
1367 (when (window-right window)
1368 (resize-window-reset-1 (window-right window) horizontal)))
1369
562dd5e9
MR
1370;; The following routine is used to manually resize the minibuffer
1371;; window and is currently used, for example, by ispell.el.
1372(defun resize-mini-window (window delta)
1373 "Resize minibuffer window WINDOW by DELTA lines.
1374If WINDOW cannot be resized by DELTA lines make it as large \(or
1375as small) as possible but don't signal an error."
1376 (when (window-minibuffer-p window)
1377 (let* ((frame (window-frame window))
1378 (root (frame-root-window frame))
1379 (height (window-total-size window))
1380 (min-delta
1381 (- (window-total-size root)
1382 (window-min-size root))))
1383 ;; Sanitize DELTA.
1384 (cond
1385 ((<= (+ height delta) 0)
1386 (setq delta (- (- height 1))))
1387 ((> delta min-delta)
1388 (setq delta min-delta)))
1389
1390 ;; Resize now.
1391 (resize-window-reset frame)
1392 ;; Ideally we should be able to resize just the last subwindow of
1393 ;; root here. See the comment in `resize-root-window-vertically'
1394 ;; for why we do not do that.
1395 (resize-this-window root (- delta) nil nil t)
1396 (set-window-new-total window (+ height delta))
1397 ;; The following routine catches the case where we want to resize
1398 ;; a minibuffer-only frame.
1399 (resize-mini-window-internal window))))
1400
1401(defun resize-window (window delta &optional horizontal ignore)
1402 "Resize WINDOW vertically by DELTA lines.
1403WINDOW can be an arbitrary window and defaults to the selected
1404one. An attempt to resize the root window of a frame will raise
1405an error though.
1406
1407DELTA a positive number means WINDOW shall be enlarged by DELTA
1408lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1409lines.
1410
1411Optional argument HORIZONTAL non-nil means resize WINDOW
1412horizontally by DELTA columns. In this case a positive DELTA
1413means enlarge WINDOW by DELTA columns. DELTA negative means
1414WINDOW shall be shrunk by -DELTA columns.
1415
1416Optional argument IGNORE non-nil means ignore any restrictions
1417imposed by fixed size windows, `window-min-height' or
1418`window-min-width' settings. IGNORE any window means ignore
1419restrictions for that window only. IGNORE equal `safe' means
1420live windows may get as small as `window-safe-min-height' lines
1421and `window-safe-min-width' columns.
1422
1423This function resizes other windows proportionally and never
1424deletes any windows. If you want to move only the low (right)
1425edge of WINDOW consider using `adjust-window-trailing-edge'
1426instead."
1427 (setq window (normalize-any-window window))
1428 (let* ((frame (window-frame window))
1429 sibling)
1430 (cond
1431 ((eq window (frame-root-window frame))
1432 (error "Cannot resize the root window of a frame"))
1433 ((window-minibuffer-p window)
1434 (resize-mini-window window delta))
1435 ((window-resizable-p window delta horizontal ignore)
1436 (resize-window-reset frame horizontal)
1437 (resize-this-window window delta horizontal ignore t)
1438 (if (and (not (window-splits window))
1439 (window-iso-combined-p window horizontal)
1440 (setq sibling (or (window-right window) (window-left window)))
1441 (window-sizable-p sibling (- delta) horizontal ignore))
1442 ;; If window-splits returns nil for WINDOW, WINDOW is part of
1443 ;; an iso-combination, and WINDOW's neighboring right or left
1444 ;; sibling can be resized as requested, resize that sibling.
1445 (let ((normal-delta
1446 (/ (float delta)
1447 (window-total-size (window-parent window) horizontal))))
1448 (resize-this-window sibling (- delta) horizontal nil t)
1449 (set-window-new-normal
1450 window (+ (window-normal-size window horizontal)
1451 normal-delta))
1452 (set-window-new-normal
1453 sibling (- (window-normal-size sibling horizontal)
1454 normal-delta)))
1455 ;; Otherwise, resize all other windows in the same combination.
1456 (resize-other-windows window delta horizontal ignore))
1457 (resize-window-apply frame horizontal))
1458 (t
1459 (error "Cannot resize window %s" window)))))
1460
9aab8e0d
MR
1461(defsubst resize-subwindows-skip-p (window)
1462 "Return non-nil if WINDOW shall be skipped by resizing routines."
1463 (memq (window-new-normal window) '(ignore stuck skip)))
1464
1465(defun resize-subwindows-normal (parent horizontal window this-delta &optional trail other-delta)
1466 "Set the new normal height of subwindows of window PARENT.
1467HORIZONTAL non-nil means set the new normal width of these
1468windows. WINDOW specifies a subwindow of PARENT that has been
1469resized by THIS-DELTA lines \(columns).
1470
1471Optional argument TRAIL either 'before or 'after means set values
1472for windows before or after WINDOW only. Optional argument
1473OTHER-DELTA a number specifies that this many lines \(columns)
1474have been obtained from \(or returned to) an ancestor window of
1475PARENT in order to resize WINDOW."
1476 (let* ((delta-normal
1477 (if (and (= (- this-delta) (window-total-size window horizontal))
1478 (zerop other-delta))
1479 ;; When WINDOW gets deleted and we can return its entire
1480 ;; space to its siblings, use WINDOW's normal size as the
1481 ;; normal delta.
1482 (- (window-normal-size window horizontal))
1483 ;; In any other case calculate the normal delta from the
1484 ;; relation of THIS-DELTA to the total size of PARENT.
1485 (/ (float this-delta) (window-total-size parent horizontal))))
1486 (sub (window-child parent))
1487 (parent-normal 0.0)
1488 (skip (eq trail 'after)))
1489
1490 ;; Set parent-normal to the sum of the normal sizes of all
1491 ;; subwindows of PARENT that shall be resized, excluding only WINDOW
1492 ;; and any windows specified by the optional TRAIL argument.
1493 (while sub
1494 (cond
1495 ((eq sub window)
1496 (setq skip (eq trail 'before)))
1497 (skip)
1498 (t
1499 (setq parent-normal
1500 (+ parent-normal (window-normal-size sub horizontal)))))
1501 (setq sub (window-right sub)))
1502
1503 ;; Set the new normal size of all subwindows of PARENT from what
1504 ;; they should have contributed for recovering THIS-DELTA lines
1505 ;; (columns).
1506 (setq sub (window-child parent))
1507 (setq skip (eq trail 'after))
1508 (while sub
1509 (cond
1510 ((eq sub window)
1511 (setq skip (eq trail 'before)))
1512 (skip)
1513 (t
1514 (let ((old-normal (window-normal-size sub horizontal)))
1515 (set-window-new-normal
1516 sub (min 1.0 ; Don't get larger than 1.
1517 (max (- old-normal
1518 (* (/ old-normal parent-normal)
1519 delta-normal))
1520 ;; Don't drop below 0.
1521 0.0))))))
1522 (setq sub (window-right sub)))
1523
1524 (when (numberp other-delta)
1525 ;; Set the new normal size of windows from what they should have
1526 ;; contributed for recovering OTHER-DELTA lines (columns).
1527 (setq delta-normal (/ (float (window-total-size parent horizontal))
1528 (+ (window-total-size parent horizontal)
1529 other-delta)))
1530 (setq sub (window-child parent))
1531 (setq skip (eq trail 'after))
1532 (while sub
1533 (cond
1534 ((eq sub window)
1535 (setq skip (eq trail 'before)))
1536 (skip)
1537 (t
1538 (set-window-new-normal
1539 sub (min 1.0 ; Don't get larger than 1.
1540 (max (* (window-new-normal sub) delta-normal)
1541 ;; Don't drop below 0.
1542 0.0)))))
1543 (setq sub (window-right sub))))
1544
1545 ;; Set the new normal size of WINDOW to what is left by the sum of
1546 ;; the normal sizes of its siblings.
1547 (set-window-new-normal
1548 window
1549 (let ((sum 0))
1550 (setq sub (window-child parent))
1551 (while sub
1552 (cond
1553 ((eq sub window))
1554 ((not (numberp (window-new-normal sub)))
1555 (setq sum (+ sum (window-normal-size sub horizontal))))
1556 (t
1557 (setq sum (+ sum (window-new-normal sub)))))
1558 (setq sub (window-right sub)))
1559 ;; Don't get larger than 1 or smaller than 0.
1560 (min 1.0 (max (- 1.0 sum) 0.0))))))
1561
1562(defun resize-subwindows (parent delta &optional horizontal window ignore trail edge)
1563 "Resize subwindows of window PARENT vertically by DELTA lines.
1564PARENT must be a vertically combined internal window.
1565
1566Optional argument HORIZONTAL non-nil means resize subwindows of
1567PARENT horizontally by DELTA columns. In this case PARENT must
1568be a horizontally combined internal window.
1569
1570WINDOW, if specified, must denote a child window of PARENT that
1571is resized by DELTA lines.
1572
1573Optional argument IGNORE non-nil means ignore any restrictions
1574imposed by fixed size windows, `window-min-height' or
1575`window-min-width' settings. IGNORE equal `safe' means live
1576windows may get as small as `window-safe-min-height' lines and
1577`window-safe-min-width' columns. IGNORE any window means ignore
1578restrictions for that window only.
1579
1580Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1581of windows that shall be resized. If TRAIL equals `before',
1582resize only windows on the left or above EDGE. If TRAIL equals
1583`after', resize only windows on the right or below EDGE. Also,
1584preferably only resize windows adjacent to EDGE.
1585
1586Return the symbol `normalized' if new normal sizes have been
1587already set by this routine."
1588 (let* ((first (window-child parent))
1589 (sub first)
1590 (parent-total (+ (window-total-size parent horizontal) delta))
1591 best-window best-value)
1592
1593 (if (and edge (memq trail '(before after))
1594 (progn
1595 (setq sub first)
1596 (while (and (window-right sub)
1597 (or (and (eq trail 'before)
1598 (not (resize-subwindows-skip-p
1599 (window-right sub))))
1600 (and (eq trail 'after)
1601 (resize-subwindows-skip-p sub))))
1602 (setq sub (window-right sub)))
1603 sub)
1604 (if horizontal
1605 (if (eq trail 'before)
1606 (= (+ (window-left-column sub)
1607 (window-total-size sub t))
1608 edge)
1609 (= (window-left-column sub) edge))
1610 (if (eq trail 'before)
1611 (= (+ (window-top-line sub)
1612 (window-total-size sub))
1613 edge)
1614 (= (window-top-line sub) edge)))
1615 (window-sizable-p sub delta horizontal ignore))
1616 ;; Resize only windows adjacent to EDGE.
1617 (progn
1618 (resize-this-window sub delta horizontal ignore t trail edge)
1619 (if (and window (eq (window-parent sub) parent))
1620 (progn
1621 ;; Assign new normal sizes.
1622 (set-window-new-normal
1623 sub (/ (float (window-new-total sub)) parent-total))
1624 (set-window-new-normal
1625 window (- (window-normal-size window horizontal)
1626 (- (window-new-normal sub)
1627 (window-normal-size sub horizontal)))))
1628 (resize-subwindows-normal parent horizontal sub 0 trail delta))
1629 ;; Return 'normalized to notify `resize-other-windows' that
1630 ;; normal sizes have been already set.
1631 'normalized)
1632 ;; Resize all windows proportionally.
1633 (setq sub first)
1634 (while sub
1635 (cond
1636 ((or (resize-subwindows-skip-p sub)
1637 ;; Ignore windows to skip and fixed-size subwindows - in
1638 ;; the latter case make it a window to skip.
1639 (and (not ignore)
1640 (window-size-fixed-p sub horizontal)
1641 (set-window-new-normal sub 'ignore))))
1642 ((< delta 0)
1643 ;; When shrinking store the number of lines/cols we can get
1644 ;; from this window here together with the total/normal size
1645 ;; factor.
1646 (set-window-new-normal
1647 sub
1648 (cons
1649 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
1650 (window-min-delta sub horizontal ignore trail t) ; t)
1651 (- (/ (float (window-total-size sub horizontal))
1652 parent-total)
1653 (window-normal-size sub horizontal)))))
1654 ((> delta 0)
1655 ;; When enlarging store the total/normal size factor only
1656 (set-window-new-normal
1657 sub
1658 (- (/ (float (window-total-size sub horizontal))
1659 parent-total)
1660 (window-normal-size sub horizontal)))))
1661
1662 (setq sub (window-right sub)))
1663
1664 (cond
1665 ((< delta 0)
1666 ;; Shrink windows by delta.
1667 (setq best-window t)
1668 (while (and best-window (not (zerop delta)))
1669 (setq sub first)
1670 (setq best-window nil)
1671 (setq best-value most-negative-fixnum)
1672 (while sub
1673 (when (and (consp (window-new-normal sub))
1674 (not (zerop (car (window-new-normal sub))))
1675 (> (cdr (window-new-normal sub)) best-value))
1676 (setq best-window sub)
1677 (setq best-value (cdr (window-new-normal sub))))
1678
1679 (setq sub (window-right sub)))
1680
1681 (when best-window
1682 (setq delta (1+ delta)))
1683 (set-window-new-total best-window -1 t)
1684 (set-window-new-normal
1685 best-window
1686 (if (= (car (window-new-normal best-window)) 1)
1687 'skip ; We can't shrink best-window any further.
1688 (cons (1- (car (window-new-normal best-window)))
1689 (- (/ (float (window-new-total best-window))
1690 parent-total)
1691 (window-normal-size best-window horizontal)))))))
1692 ((> delta 0)
1693 ;; Enlarge windows by delta.
1694 (setq best-window t)
1695 (while (and best-window (not (zerop delta)))
1696 (setq sub first)
1697 (setq best-window nil)
1698 (setq best-value most-positive-fixnum)
1699 (while sub
1700 (when (and (numberp (window-new-normal sub))
1701 (< (window-new-normal sub) best-value))
1702 (setq best-window sub)
1703 (setq best-value (window-new-normal sub)))
1704
1705 (setq sub (window-right sub)))
1706
1707 (when best-window
1708 (setq delta (1- delta)))
1709 (set-window-new-total best-window 1 t)
1710 (set-window-new-normal
1711 best-window
1712 (- (/ (float (window-new-total best-window))
1713 parent-total)
1714 (window-normal-size best-window horizontal))))))
1715
1716 (when best-window
1717 (setq sub first)
1718 (while sub
1719 (when (or (consp (window-new-normal sub))
1720 (numberp (window-new-normal sub)))
1721 ;; Reset new normal size fields so `resize-window-apply'
1722 ;; won't use them to apply new sizes.
1723 (set-window-new-normal sub))
1724
1725 (unless (eq (window-new-normal sub) 'ignore)
1726 ;; Resize this subwindow's subwindows (back-engineering
1727 ;; delta from sub's old and new total sizes).
1728 (let ((delta (- (window-new-total sub)
1729 (window-total-size sub horizontal))))
1730 (unless (and (zerop delta) (not trail))
1731 ;; For the TRAIL non-nil case we have to resize SUB
1732 ;; recursively even if it's size does not change.
1733 (resize-this-window
1734 sub delta horizontal ignore nil trail edge))))
1735 (setq sub (window-right sub)))))))
1736
1737(defun resize-other-windows (window delta &optional horizontal ignore trail edge)
1738 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1739Optional argument HORIZONTAL non-nil means resize other windows
1740when WINDOW is resized horizontally by DELTA columns. WINDOW
1741itself is not resized by this function.
1742
1743Optional argument IGNORE non-nil means ignore any restrictions
1744imposed by fixed size windows, `window-min-height' or
1745`window-min-width' settings. IGNORE equal `safe' means live
1746windows may get as small as `window-safe-min-height' lines and
1747`window-safe-min-width' columns. IGNORE any window means ignore
1748restrictions for that window only.
1749
1750Optional arguments TRAIL and EDGE, when non-nil, refine the set
1751of windows that shall be resized. If TRAIL equals `before',
1752resize only windows on the left or above EDGE. If TRAIL equals
1753`after', resize only windows on the right or below EDGE. Also,
1754preferably only resize windows adjacent to EDGE."
1755 (when (window-parent window)
1756 (let* ((parent (window-parent window))
1757 (sub (window-child parent)))
1758 (if (window-iso-combined-p sub horizontal)
1759 ;; In an iso-combination try to extract DELTA from WINDOW's
1760 ;; siblings.
1761 (let ((first sub)
1762 (skip (eq trail 'after))
1763 this-delta other-delta)
1764 ;; Decide which windows shall be left alone.
1765 (while sub
1766 (cond
1767 ((eq sub window)
1768 ;; Make sure WINDOW is left alone when
1769 ;; resizing its siblings.
1770 (set-window-new-normal sub 'ignore)
1771 (setq skip (eq trail 'before)))
1772 (skip
1773 ;; Make sure this sibling is left alone when
1774 ;; resizing its siblings.
1775 (set-window-new-normal sub 'ignore))
1776 ((or (window-size-ignore sub ignore)
1777 (not (window-size-fixed-p sub horizontal)))
1778 ;; Set this-delta to t to signal that we found a sibling
1779 ;; of WINDOW whose size is not fixed.
1780 (setq this-delta t)))
1781
1782 (setq sub (window-right sub)))
1783
1784 ;; Set this-delta to what we can get from WINDOW's siblings.
1785 (if (= (- delta) (window-total-size window horizontal))
1786 ;; A deletion, presumably. We must handle this case
1787 ;; specially since `window-resizable' can't be used.
1788 (if this-delta
1789 ;; There's at least one resizable sibling we can
1790 ;; give WINDOW's size to.
1791 (setq this-delta delta)
1792 ;; No resizable sibling exists.
1793 (setq this-delta 0))
1794 ;; Any other form of resizing.
1795 (setq this-delta
1796 (window-resizable window delta horizontal ignore trail t)))
1797
1798 ;; Set other-delta to what we still have to get from
1799 ;; ancestor windows of parent.
1800 (setq other-delta (- delta this-delta))
1801 (unless (zerop other-delta)
1802 ;; Unless we got everything from WINDOW's siblings, PARENT
1803 ;; must be resized by other-delta lines or columns.
1804 (set-window-new-total parent other-delta 'add))
1805
1806 (if (zerop this-delta)
1807 ;; We haven't got anything from WINDOW's siblings but we
1808 ;; must update the normal sizes to respect other-delta.
1809 (resize-subwindows-normal
1810 parent horizontal window this-delta trail other-delta)
1811 ;; We did get something from WINDOW's siblings which means
1812 ;; we have to resize their subwindows.
1813 (unless (eq (resize-subwindows parent (- this-delta) horizontal
1814 window ignore trail edge)
1815 ;; `resize-subwindows' returning 'normalized,
1816 ;; means it has set the normal sizes already.
1817 'normalized)
1818 ;; Set the normal sizes.
1819 (resize-subwindows-normal
1820 parent horizontal window this-delta trail other-delta))
1821 ;; Set DELTA to what we still have to get from ancestor
1822 ;; windows.
1823 (setq delta other-delta)))
1824
1825 ;; In an ortho-combination all siblings of WINDOW must be
1826 ;; resized by DELTA.
1827 (set-window-new-total parent delta 'add)
1828 (while sub
1829 (unless (eq sub window)
1830 (resize-this-window sub delta horizontal ignore t))
1831 (setq sub (window-right sub))))
1832
1833 (unless (zerop delta)
1834 ;; "Go up."
1835 (resize-other-windows parent delta horizontal ignore trail edge)))))
1836
1837(defun resize-this-window (window delta &optional horizontal ignore add trail edge)
1838 "Resize WINDOW vertically by DELTA lines.
1839Optional argument HORIZONTAL non-nil means resize WINDOW
1840horizontally by DELTA columns.
1841
1842Optional argument IGNORE non-nil means ignore any restrictions
1843imposed by fixed size windows, `window-min-height' or
1844`window-min-width' settings. IGNORE equal `safe' means live
1845windows may get as small as `window-safe-min-height' lines and
1846`window-safe-min-width' columns. IGNORE any window means ignore
1847restrictions for that window only.
1848
1849Optional argument ADD non-nil means add DELTA to the new total
1850size of WINDOW.
1851
1852Optional arguments TRAIL and EDGE, when non-nil, refine the set
1853of windows that shall be resized. If TRAIL equals `before',
1854resize only windows on the left or above EDGE. If TRAIL equals
1855`after', resize only windows on the right or below EDGE. Also,
1856preferably only resize windows adjacent to EDGE.
1857
1858This function recursively resizes WINDOW's subwindows to fit the
1859new size. Make sure that WINDOW is `window-resizable' before
1860calling this function. Note that this function does not resize
1861siblings of WINDOW or WINDOW's parent window. You have to
1862eventually call `resize-window-apply' in order to make resizing
1863actually take effect."
1864 (when add
1865 ;; Add DELTA to the new total size of WINDOW.
1866 (set-window-new-total window delta t))
387522b2 1867
9aab8e0d
MR
1868 (let ((sub (window-child window)))
1869 (cond
1870 ((not sub))
1871 ((window-iso-combined-p sub horizontal)
1872 ;; In an iso-combination resize subwindows according to their
1873 ;; normal sizes.
1874 (resize-subwindows window delta horizontal nil ignore trail edge))
1875 ;; In an ortho-combination resize each subwindow by DELTA.
1876 (t
1877 (while sub
1878 (resize-this-window sub delta horizontal ignore t trail edge)
1879 (setq sub (window-right sub)))))))
1880
1881(defun resize-root-window (window delta horizontal ignore)
1882 "Resize root window WINDOW vertically by DELTA lines.
1883HORIZONTAL non-nil means resize root window WINDOW horizontally
1884by DELTA columns.
1885
1886IGNORE non-nil means ignore any restrictions imposed by fixed
1887size windows, `window-min-height' or `window-min-width' settings.
1888
1889This function is only called by the frame resizing routines. It
1890resizes windows proportionally and never deletes any windows."
1891 (when (and (windowp window) (numberp delta)
1892 (window-sizable-p window delta horizontal ignore))
1893 (resize-window-reset (window-frame window) horizontal)
1894 (resize-this-window window delta horizontal ignore t)))
1895
1896(defun resize-root-window-vertically (window delta)
1897 "Resize root window WINDOW vertically by DELTA lines.
1898If DELTA is less than zero and we can't shrink WINDOW by DELTA
1899lines, shrink it as much as possible. If DELTA is greater than
1900zero, this function can resize fixed-size subwindows in order to
1901recover the necessary lines.
1902
1903Return the number of lines that were recovered.
1904
1905This function is only called by the minibuffer window resizing
1906routines. It resizes windows proportionally and never deletes
1907any windows."
1908 (when (numberp delta)
1909 (let (ignore)
1910 (cond
1911 ((< delta 0)
1912 (setq delta (window-sizable window delta)))
1913 ((> delta 0)
1914 (unless (window-sizable window delta)
1915 (setq ignore t))))
1916
1917 (resize-window-reset (window-frame window))
1918 ;; Ideally, we would resize just the last window in a combination
1919 ;; but that's not feasible for the following reason: If we grow
1920 ;; the minibuffer window and the last window cannot be shrunk any
1921 ;; more, we shrink another window instead. But if we then shrink
1922 ;; the minibuffer window again, the last window might get enlarged
1923 ;; and the state after shrinking is not the state before growing.
1924 ;; So, in practice, we'd need a history variable to record how to
1925 ;; proceed. But I'm not sure how such a variable could work with
1926 ;; repeated minibuffer window growing steps.
1927 (resize-this-window window delta nil ignore t)
1928 delta)))
1929
562dd5e9
MR
1930(defun adjust-window-trailing-edge (window delta &optional horizontal)
1931 "Move WINDOW's bottom edge by DELTA lines.
1932Optional argument HORIZONTAL non-nil means move WINDOW's right
1933edge by DELTA columns. WINDOW defaults to the selected window.
1934
1935If DELTA is greater zero, then move the edge downwards or to the
1936right. If DELTA is less than zero, move the edge upwards or to
1937the left. If the edge can't be moved by DELTA lines or columns,
1938move it as far as possible in the desired direction."
1939 (setq window (normalize-any-window window))
1940 (let ((frame (window-frame window))
1941 (right window)
1942 left this-delta min-delta max-delta failed)
1943 ;; Find the edge we want to move.
1944 (while (and (or (not (window-iso-combined-p right horizontal))
1945 (not (window-right right)))
1946 (setq right (window-parent right))))
1947 (cond
1948 ((and (not right) (not horizontal) (not resize-mini-windows)
1949 (eq (window-frame (minibuffer-window frame)) frame))
1950 (resize-mini-window (minibuffer-window frame) (- delta)))
1951 ((or (not (setq left right)) (not (setq right (window-right right))))
1952 (if horizontal
1953 (error "No window on the right of this one")
1954 (error "No window below this one")))
1955 (t
1956 ;; Set LEFT to the first resizable window on the left. This step is
1957 ;; needed to handle fixed-size windows.
1958 (while (and left (window-size-fixed-p left horizontal))
1959 (setq left
1960 (or (window-left left)
1961 (progn
1962 (while (and (setq left (window-parent left))
1963 (not (window-iso-combined-p left horizontal))))
1964 (window-left left)))))
1965 (unless left
1966 (if horizontal
1967 (error "No resizable window on the left of this one")
1968 (error "No resizable window above this one")))
1969
1970 ;; Set RIGHT to the first resizable window on the right. This step
1971 ;; is needed to handle fixed-size windows.
1972 (while (and right (window-size-fixed-p right horizontal))
1973 (setq right
1974 (or (window-right right)
1975 (progn
1976 (while (and (setq right (window-parent right))
1977 (not (window-iso-combined-p right horizontal))))
1978 (window-right right)))))
1979 (unless right
1980 (if horizontal
1981 (error "No resizable window on the right of this one")
1982 (error "No resizable window below this one")))
1983
1984 ;; LEFT and RIGHT (which might be both internal windows) are now the
1985 ;; two windows we want to resize.
1986 (cond
1987 ((> delta 0)
1988 (setq max-delta (window-max-delta-1 left 0 horizontal nil 'after))
1989 (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'before))
1990 (when (or (< max-delta delta) (> min-delta (- delta)))
1991 ;; We can't get the whole DELTA - move as far as possible.
1992 (setq delta (min max-delta (- min-delta))))
1993 (unless (zerop delta)
1994 ;; Start resizing.
1995 (resize-window-reset frame horizontal)
1996 ;; Try to enlarge LEFT first.
1997 (setq this-delta (window-resizable left delta horizontal))
1998 (unless (zerop this-delta)
1999 (resize-this-window
2000 left this-delta horizontal nil t 'before
2001 (if horizontal
2002 (+ (window-left-column left) (window-total-size left t))
2003 (+ (window-top-line left) (window-total-size left)))))
2004 ;; Shrink windows on right of LEFT.
2005 (resize-other-windows
2006 left delta horizontal nil 'after
2007 (if horizontal
2008 (window-left-column right)
2009 (window-top-line right)))))
2010 ((< delta 0)
2011 (setq max-delta (window-max-delta-1 right 0 horizontal nil 'before))
2012 (setq min-delta (window-min-delta-1 left delta horizontal nil 'after))
2013 (when (or (< max-delta (- delta)) (> min-delta delta))
2014 ;; We can't get the whole DELTA - move as far as possible.
2015 (setq delta (max (- max-delta) min-delta)))
2016 (unless (zerop delta)
2017 ;; Start resizing.
2018 (resize-window-reset frame horizontal)
2019 ;; Try to enlarge RIGHT.
2020 (setq this-delta (window-resizable right (- delta) horizontal))
2021 (unless (zerop this-delta)
2022 (resize-this-window
2023 right this-delta horizontal nil t 'after
2024 (if horizontal
2025 (window-left-column right)
2026 (window-top-line right))))
2027 ;; Shrink windows on left of RIGHT.
2028 (resize-other-windows
2029 right (- delta) horizontal nil 'before
2030 (if horizontal
2031 (+ (window-left-column left) (window-total-size left t))
2032 (+ (window-top-line left) (window-total-size left)))))))
2033 (unless (zerop delta)
2034 ;; Don't report an error in the standard case.
2035 (unless (resize-window-apply frame horizontal)
2036 ;; But do report an error if applying the changes fails.
2037 (error "Failed adjusting window %s" window)))))))
2038
2039(defun enlarge-window (delta &optional horizontal)
2040 "Make selected window DELTA lines taller.
2041Interactively, if no argument is given, make the selected window
2042one line taller. If optional argument HORIZONTAL is non-nil,
2043make selected window wider by DELTA columns. If DELTA is
2044negative, shrink selected window by -DELTA lines or columns.
2045Return nil."
2046 (interactive "p")
2047 (resize-window (selected-window) delta horizontal))
2048
2049(defun shrink-window (delta &optional horizontal)
2050 "Make selected window DELTA lines smaller.
2051Interactively, if no argument is given, make the selected window
2052one line smaller. If optional argument HORIZONTAL is non-nil,
2053make selected window narrower by DELTA columns. If DELTA is
2054negative, enlarge selected window by -DELTA lines or columns.
2055Return nil."
2056 (interactive "p")
2057 (resize-window (selected-window) (- delta) horizontal))
2058
2059(defun maximize-window (&optional window)
2060 "Maximize WINDOW.
2061Make WINDOW as large as possible without deleting any windows.
2062WINDOW can be any window and defaults to the selected window."
2063 (interactive)
2064 (setq window (normalize-any-window window))
2065 (resize-window window (window-max-delta window))
2066 (resize-window window (window-max-delta window t) t))
2067
2068(defun minimize-window (&optional window)
2069 "Minimize WINDOW.
2070Make WINDOW as small as possible without deleting any windows.
2071WINDOW can be any window and defaults to the selected window."
2072 (interactive)
2073 (setq window (normalize-any-window window))
2074 (resize-window window (- (window-min-delta window)))
2075 (resize-window window (- (window-min-delta window t)) t))
2076\f
9aab8e0d
MR
2077(defsubst frame-root-window-p (window)
2078 "Return non-nil if WINDOW is the root window of its frame."
2079 (eq window (frame-root-window window)))
2080\f
9397e56f
MR
2081(defun other-window (count &optional all-frames)
2082 "Select another window in cyclic ordering of windows.
2083COUNT specifies the number of windows to skip, starting with the
2084selected window, before making the selection. If COUNT is
2085positive, skip COUNT windows forwards. If COUNT is negative,
2086skip -COUNT windows backwards. COUNT zero means do not skip any
2087window, so select the selected window. In an interactive call,
2088COUNT is the numeric prefix argument. Return nil.
2089
2090If the `other-window' parameter of WINDOW is a function and
2091`ignore-window-parameters' is nil, call that function with the
2092arguments COUNT and ALL-FRAMES.
2093
2094This function does not select a window whose `no-other-window'
2095window parameter is non-nil.
2096
2097This function uses `next-window' for finding the window to
2098select. The argument ALL-FRAMES has the same meaning as in
2099`next-window', but the MINIBUF argument of `next-window' is
2100always effectively nil."
2101 (interactive "p")
2102 (let* ((window (selected-window))
2103 (function (and (not ignore-window-parameters)
2104 (window-parameter window 'other-window)))
2105 old-window old-count)
2106 (if (functionp function)
2107 (funcall function count all-frames)
2108 ;; `next-window' and `previous-window' may return a window we are
2109 ;; not allowed to select. Hence we need an exit strategy in case
2110 ;; all windows are non-selectable.
2111 (catch 'exit
2112 (while (> count 0)
2113 (setq window (next-window window nil all-frames))
2114 (cond
2115 ((eq window old-window)
2116 (when (= count old-count)
2117 ;; Keep out of infinite loops. When COUNT has not changed
2118 ;; since we last looked at `window' we're probably in one.
2119 (throw 'exit nil)))
2120 ((window-parameter window 'no-other-window)
2121 (unless old-window
2122 ;; The first non-selectable window `next-window' got us:
2123 ;; Remember it and the current value of COUNT.
2124 (setq old-window window)
2125 (setq old-count count)))
2126 (t
2127 (setq count (1- count)))))
2128 (while (< count 0)
2129 (setq window (previous-window window nil all-frames))
2130 (cond
2131 ((eq window old-window)
2132 (when (= count old-count)
2133 ;; Keep out of infinite loops. When COUNT has not changed
2134 ;; since we last looked at `window' we're probably in one.
2135 (throw 'exit nil)))
2136 ((window-parameter window 'no-other-window)
2137 (unless old-window
2138 ;; The first non-selectable window `previous-window' got
2139 ;; us: Remember it and the current value of COUNT.
2140 (setq old-window window)
2141 (setq old-count count)))
2142 (t
2143 (setq count (1+ count)))))
2144
2145 (select-window window)
2146 ;; Always return nil.
2147 nil))))
2148
387522b2
MR
2149;; This should probably return non-nil when the selected window is part
2150;; of an atomic window whose root is the frame's root window.
2151(defun one-window-p (&optional nomini all-frames)
2152 "Return non-nil if the selected window is the only window.
2153Optional arg NOMINI non-nil means don't count the minibuffer
2154even if it is active. Otherwise, the minibuffer is counted
2155when it is active.
2156
2157Optional argument ALL-FRAMES specifies the set of frames to
2158consider, see also `next-window'. ALL-FRAMES nil or omitted
2159means consider windows on the selected frame only, plus the
2160minibuffer window if specified by the NOMINI argument. If the
2161minibuffer counts, consider all windows on all frames that share
2162that minibuffer too. The remaining non-nil values of ALL-FRAMES
2163with a special meaning are:
2164
2165- t means consider all windows on all existing frames.
2166
2167- `visible' means consider all windows on all visible frames on
2168 the current terminal.
2169
2170- 0 (the number zero) means consider all windows on all visible
2171 and iconified frames on the current terminal.
2172
2173- A frame means consider all windows on that frame only.
2174
2175Anything else means consider all windows on the selected frame
2176and no others."
2177 (let ((base-window (selected-window)))
2178 (if (and nomini (eq base-window (minibuffer-window)))
2179 (setq base-window (next-window base-window)))
2180 (eq base-window
2181 (next-window base-window (if nomini 'arg) all-frames))))
3c448ab6 2182\f
9aab8e0d
MR
2183;;; Deleting windows.
2184(defun window-deletable-p (&optional window)
2185 "Return t if WINDOW can be safely deleted from its frame.
2186Return `frame' if deleting WINDOW should delete its frame
2187instead."
2188 (setq window (normalize-any-window window))
2189 (unless ignore-window-parameters
2190 ;; Handle atomicity.
2191 (when (window-parameter window 'window-atom)
2192 (setq window (window-atom-root window))))
2193 (let ((parent (window-parent window))
2194 (frame (window-frame window))
2195 (dedicated (and (window-buffer window) (window-dedicated-p window)))
2196 (quit-restore (window-parameter window 'quit-restore)))
2197 (cond
2198 ((frame-root-window-p window)
2199 (when (and (or dedicated
2200 (and (eq (car-safe quit-restore) 'new-frame)
2201 (eq (nth 1 quit-restore) (window-buffer window))))
2202 (other-visible-frames-p frame))
2203 ;; WINDOW is the root window of its frame. Return `frame' but
2204 ;; only if WINDOW is (1) either dedicated or quit-restore's car
2205 ;; is new-frame and the window still displays the same buffer
2206 ;; and (2) there are other frames left.
2207 'frame))
2208 ((and (not ignore-window-parameters)
2209 (eq (window-parameter window 'window-side) 'none)
2210 (or (not parent)
2211 (not (eq (window-parameter parent 'window-side) 'none))))
2212 ;; Can't delete last main window.
2213 nil)
2214 (t))))
2215
2216(defun window-or-subwindow-p (subwindow window)
2217 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2218 (or (eq subwindow window)
2219 (let ((parent (window-parent subwindow)))
2220 (catch 'done
2221 (while parent
2222 (if (eq parent window)
2223 (throw 'done t)
2224 (setq parent (window-parent parent))))))))
2225
562dd5e9
MR
2226(defun delete-window (&optional window)
2227 "Delete WINDOW.
2228WINDOW can be an arbitrary window and defaults to the selected
2229one. Return nil.
2230
2231If the variable `ignore-window-parameters' is non-nil or the
2232`delete-window' parameter of WINDOW equals t, do not process any
2233parameters of WINDOW. Otherwise, if the `delete-window'
2234parameter of WINDOW specifies a function, call that function with
2235WINDOW as its sole argument and return the value returned by that
2236function.
2237
2238Otherwise, if WINDOW is part of an atomic window, call
2239`delete-window' with the root of the atomic window as its
2240argument. If WINDOW is the only window on its frame or the last
2241non-side window, signal an error."
2242 (interactive)
2243 (setq window (normalize-any-window window))
2244 (let* ((frame (window-frame window))
2245 (function (window-parameter window 'delete-window))
2246 (parent (window-parent window))
2247 atom-root)
2248 (window-check frame)
2249 (catch 'done
2250 ;; Handle window parameters.
2251 (cond
2252 ;; Ignore window parameters if `ignore-window-parameters' tells
2253 ;; us so or `delete-window' equals t.
2254 ((or ignore-window-parameters (eq function t)))
2255 ((functionp function)
2256 ;; The `delete-window' parameter specifies the function to call.
2257 ;; If that function is `ignore' nothing is done. It's up to the
2258 ;; function called here to avoid infinite recursion.
2259 (throw 'done (funcall function window)))
2260 ((and (window-parameter window 'window-atom)
2261 (setq atom-root (window-atom-root window))
2262 (not (eq atom-root window)))
2263 (throw 'done (delete-window atom-root)))
2264 ((and (eq (window-parameter window 'window-side) 'none)
2265 (or (not parent)
2266 (not (eq (window-parameter parent 'window-side) 'none))))
2267 (error "Attempt to delete last non-side window"))
2268 ((not parent)
2269 (error "Attempt to delete minibuffer or sole ordinary window")))
2270
2271 (let* ((horizontal (window-hchild parent))
2272 (size (window-total-size window horizontal))
2273 (frame-selected
2274 (window-or-subwindow-p (frame-selected-window frame) window))
2275 ;; Emacs 23 preferably gives WINDOW's space to its left
2276 ;; sibling.
2277 (sibling (or (window-left window) (window-right window))))
2278 (resize-window-reset frame horizontal)
2279 (cond
2280 ((and (not (window-splits window))
2281 sibling (window-sizable-p sibling size))
2282 ;; Resize WINDOW's sibling.
2283 (resize-this-window sibling size horizontal nil t)
2284 (set-window-new-normal
2285 sibling (+ (window-normal-size sibling horizontal)
2286 (window-normal-size window horizontal))))
2287 ((window-resizable-p window (- size) horizontal nil nil nil t)
2288 ;; Can do without resizing fixed-size windows.
2289 (resize-other-windows window (- size) horizontal))
2290 (t
2291 ;; Can't do without resizing fixed-size windows.
2292 (resize-other-windows window (- size) horizontal t)))
2293 ;; Actually delete WINDOW.
2294 (delete-window-internal window)
2295 (when (and frame-selected
2296 (window-parameter
2297 (frame-selected-window frame) 'no-other-window))
2298 ;; `delete-window-internal' has selected a window that should
2299 ;; not be selected, fix this here.
2300 (other-window -1 frame))
2301 (run-window-configuration-change-hook frame)
2302 (window-check frame)
2303 ;; Always return nil.
2304 nil))))
2305
2306(defun delete-other-windows (&optional window)
2307 "Make WINDOW fill its frame.
2308WINDOW may be any window and defaults to the selected one.
2309Return nil.
2310
2311If the variable `ignore-window-parameters' is non-nil or the
2312`delete-other-windows' parameter of WINDOW equals t, do not
2313process any parameters of WINDOW. Otherwise, if the
2314`delete-other-windows' parameter of WINDOW specifies a function,
2315call that function with WINDOW as its sole argument and return
2316the value returned by that function.
2317
2318Otherwise, if WINDOW is part of an atomic window, call this
2319function with the root of the atomic window as its argument. If
2320WINDOW is a non-side window, make WINDOW the only non-side window
2321on the frame. Side windows are not deleted. If WINDOW is a side
2322window signal an error."
2323 (interactive)
2324 (setq window (normalize-any-window window))
2325 (let* ((frame (window-frame window))
2326 (function (window-parameter window 'delete-other-windows))
2327 (window-side (window-parameter window 'window-side))
2328 atom-root side-main)
2329 (window-check frame)
2330 (catch 'done
2331 (cond
2332 ;; Ignore window parameters if `ignore-window-parameters' is t or
2333 ;; `delete-other-windows' is t.
2334 ((or ignore-window-parameters (eq function t)))
2335 ((functionp function)
2336 ;; The `delete-other-windows' parameter specifies the function
2337 ;; to call. If the function is `ignore' no windows are deleted.
2338 ;; It's up to the function called to avoid infinite recursion.
2339 (throw 'done (funcall function window)))
2340 ((and (window-parameter window 'window-atom)
2341 (setq atom-root (window-atom-root window))
2342 (not (eq atom-root window)))
2343 (throw 'done (delete-other-windows atom-root)))
2344 ((eq window-side 'none)
2345 ;; Set side-main to the major non-side window.
2346 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2347 ((memq window-side window-sides)
2348 (error "Cannot make side window the only window")))
2349 ;; If WINDOW is the main non-side window, do nothing.
2350 (unless (eq window side-main)
2351 (delete-other-windows-internal window side-main)
2352 (run-window-configuration-change-hook frame)
2353 (window-check frame))
2354 ;; Always return nil.
2355 nil)))
9397e56f
MR
2356
2357(defun delete-other-windows-vertically (&optional window)
2358 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2359This may be a useful alternative binding for \\[delete-other-windows]
2360 if you often split windows horizontally."
2361 (interactive)
2362 (let* ((window (or window (selected-window)))
2363 (edges (window-edges window))
2364 (w window) delenda)
2365 (while (not (eq (setq w (next-window w 1)) window))
2366 (let ((e (window-edges w)))
2367 (when (and (= (car e) (car edges))
2368 (= (caddr e) (caddr edges)))
2369 (push w delenda))))
2370 (mapc 'delete-window delenda)))
2371
2372;;; Windows and buffers.
2373
2374;; `prev-buffers' and `next-buffers' are two reserved window slots used
2375;; for (1) determining which buffer to show in the window when its
2376;; buffer shall be buried or killed and (2) which buffer to show for
2377;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2378
2379;; `prev-buffers' consists of <buffer, window-start, window-point>
2380;; triples. The entries on this list are ordered by the time their
2381;; buffer has been removed from the window, the most recently removed
2382;; buffer's entry being first. The window-start and window-point
2383;; components are `window-start' and `window-point' at the time the
2384;; buffer was removed from the window which implies that the entry must
2385;; be added when `set-window-buffer' removes the buffer from the window.
2386
2387;; `next-buffers' is the list of buffers that have been replaced
2388;; recently by `switch-to-prev-buffer'. These buffers are the least
2389;; preferred candidates of `switch-to-prev-buffer' and the preferred
2390;; candidates of `switch-to-next-buffer' to switch to. This list is
2391;; reset to nil by any action changing the window's buffer with the
2392;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2393;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2394;; `switch-to-next-buffer' pops the last pushed buffer from it.
2395
2396;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2397;; if such a buffer was killed while the window was hidden within a
2398;; window configuration. Such killed buffers get removed whenever
2399;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2400
2401;; The following function is called by `set-window-buffer' _before_ it
2402;; replaces the buffer of the argument window with the new buffer.
2403(defun record-window-buffer (&optional window)
2404 "Record WINDOW's buffer.
2405WINDOW must be a live window and defaults to the selected one."
2406 (let* ((window (normalize-live-window window))
2407 (buffer (window-buffer window))
2408 (entry (assq buffer (window-prev-buffers window))))
2409 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2410 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2411 (set-window-next-buffers window nil)
2412
2413 (when entry
2414 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2415 (set-window-prev-buffers
2416 window (assq-delete-all buffer (window-prev-buffers window))))
2417
2418 ;; Don't record insignificant buffers.
2419 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2420 ;; Add an entry for buffer to WINDOW's previous buffers.
2421 (with-current-buffer buffer
2422 (let ((start (window-start window))
2423 (point (window-point window)))
2424 (setq entry
2425 (cons buffer
2426 (if entry
2427 ;; We have an entry, update marker positions.
2428 (list (set-marker (nth 1 entry) start)
2429 (set-marker (nth 2 entry) point))
2430 ;; Make new markers.
2431 (list (copy-marker start)
2432 (copy-marker point)))))
2433
2434 (set-window-prev-buffers
2435 window (cons entry (window-prev-buffers window))))))))
2436
2437(defun unrecord-window-buffer (&optional window buffer)
2438 "Unrecord BUFFER in WINDOW.
2439WINDOW must be a live window and defaults to the selected one.
2440BUFFER must be a live buffer and defaults to the buffer of
2441WINDOW."
2442 (let* ((window (normalize-live-window window))
2443 (buffer (or buffer (window-buffer window))))
2444 (set-window-prev-buffers
2445 window (assq-delete-all buffer (window-prev-buffers window)))
2446 (set-window-next-buffers
2447 window (delq buffer (window-next-buffers window)))))
2448
2449(defun set-window-buffer-start-and-point (window buffer &optional start point)
2450 "Set WINDOW's buffer to BUFFER.
2451Optional argument START non-nil means set WINDOW's start position
2452to START. Optional argument POINT non-nil means set WINDOW's
2453point to POINT. If WINDOW is selected this also sets BUFFER's
2454`point' to POINT. If WINDOW is selected and the buffer it showed
2455before was current this also makes BUFFER the current buffer."
2456 (let ((selected (eq window (selected-window)))
2457 (current (eq (window-buffer window) (current-buffer))))
2458 (set-window-buffer window buffer)
2459 (when (and selected current)
2460 (set-buffer buffer))
2461 (when start
2462 (set-window-start window start))
2463 (when point
2464 (if selected
2465 (with-current-buffer buffer
2466 (goto-char point))
2467 (set-window-point window point)))))
2468
2469(defun switch-to-prev-buffer (&optional window bury-or-kill)
2470 "In WINDOW switch to previous buffer.
2471WINDOW must be a live window and defaults to the selected one.
2472
2473Optional argument BURY-OR-KILL non-nil means the buffer currently
2474shown in WINDOW is about to be buried or killed and consequently
2475shall not be switched to in future invocations of this command."
2476 (interactive)
2477 (let* ((window (normalize-live-window window))
2478 (old-buffer (window-buffer window))
2479 ;; Save this since it's destroyed by `set-window-buffer'.
2480 (next-buffers (window-next-buffers window))
2481 entry new-buffer killed-buffers deletable visible)
2482 (cond
2483 ;; When BURY-OR-KILL is non-nil, there's no previous buffer for
2484 ;; this window, and we can delete the window (or the frame) do
2485 ;; that.
2486 ((and bury-or-kill
2487 (or (not (window-prev-buffers window))
2488 (and (eq (caar (window-prev-buffers window)) old-buffer)
2489 (not (cdr (car (window-prev-buffers window))))))
2490 (setq deletable (window-deletable-p window)))
2491 (if (eq deletable 'frame)
2492 (delete-frame (window-frame window))
2493 (delete-window window)))
2494 ((window-dedicated-p window)
2495 (error "Window %s is dedicated to buffer %s" window old-buffer)))
2496
2497 (unless deletable
2498 (catch 'found
2499 ;; Scan WINDOW's previous buffers first, skipping entries of next
2500 ;; buffers.
2501 (dolist (entry (window-prev-buffers window))
2502 (when (and (setq new-buffer (car entry))
2503 (or (buffer-live-p new-buffer)
2504 (not (setq killed-buffers
2505 (cons new-buffer killed-buffers))))
2506 (not (eq new-buffer old-buffer))
2507 (or bury-or-kill
2508 (not (memq new-buffer next-buffers))))
2509 (set-window-buffer-start-and-point
2510 window new-buffer (nth 1 entry) (nth 2 entry))
2511 (throw 'found t)))
2512 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2513 ;; entries of next buffers. Note that when we bury or kill a
2514 ;; buffer we don't reverse the global buffer list to avoid showing
2515 ;; a buried buffer instead. Otherwise, we must reverse the global
2516 ;; buffer list in order to make sure that switching to the
2517 ;; previous/next buffer traverse it in opposite directions.
2518 (dolist (buffer (if bury-or-kill
2519 (buffer-list (window-frame window))
2520 (nreverse (buffer-list (window-frame window)))))
2521 (when (and (buffer-live-p buffer)
2522 (not (eq buffer old-buffer))
2523 (not (eq (aref (buffer-name buffer) 0) ?\s))
2524 (or bury-or-kill (not (memq buffer next-buffers))))
2525 (if (get-buffer-window buffer)
2526 ;; Try to avoid showing a buffer visible in some other window.
2527 (setq visible buffer)
2528 (setq new-buffer buffer)
2529 (set-window-buffer-start-and-point window new-buffer)
2530 (throw 'found t))))
2531 (unless bury-or-kill
2532 ;; Scan reverted next buffers last (must not use nreverse
2533 ;; here!).
2534 (dolist (buffer (reverse next-buffers))
2535 ;; Actually, buffer _must_ be live here since otherwise it
2536 ;; would have been caught in the scan of previous buffers.
2537 (when (and (or (buffer-live-p buffer)
2538 (not (setq killed-buffers
2539 (cons buffer killed-buffers))))
2540 (not (eq buffer old-buffer))
2541 (setq entry (assq buffer (window-prev-buffers window))))
2542 (setq new-buffer buffer)
2543 (set-window-buffer-start-and-point
2544 window new-buffer (nth 1 entry) (nth 2 entry))
2545 (throw 'found t))))
2546
2547 ;; Show a buffer visible in another window.
2548 (when visible
2549 (setq new-buffer visible)
2550 (set-window-buffer-start-and-point window new-buffer)))
2551
2552 (if bury-or-kill
2553 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2554 ;; of) next buffers.
2555 (progn
2556 (set-window-prev-buffers
2557 window (assq-delete-all old-buffer (window-prev-buffers window)))
2558 (set-window-next-buffers window (delq old-buffer next-buffers)))
2559 ;; Move `old-buffer' to head of WINDOW's restored list of next
2560 ;; buffers.
2561 (set-window-next-buffers
2562 window (cons old-buffer (delq old-buffer next-buffers)))))
2563
2564 ;; Remove killed buffers from WINDOW's previous and next buffers.
2565 (when killed-buffers
2566 (dolist (buffer killed-buffers)
2567 (set-window-prev-buffers
2568 window (assq-delete-all buffer (window-prev-buffers window)))
2569 (set-window-next-buffers
2570 window (delq buffer (window-next-buffers window)))))
2571
2572 ;; Return new-buffer.
2573 new-buffer))
2574
2575(defun switch-to-next-buffer (&optional window)
2576 "In WINDOW switch to next buffer.
2577WINDOW must be a live window and defaults to the selected one."
2578 (interactive)
2579 (let* ((window (normalize-live-window window))
2580 (old-buffer (window-buffer window))
2581 (next-buffers (window-next-buffers window))
2582 new-buffer entry killed-buffers visible)
2583 (when (window-dedicated-p window)
2584 (error "Window %s is dedicated to buffer %s" window old-buffer))
2585
2586 (catch 'found
2587 ;; Scan WINDOW's next buffers first.
2588 (dolist (buffer next-buffers)
2589 (when (and (or (buffer-live-p buffer)
2590 (not (setq killed-buffers
2591 (cons buffer killed-buffers))))
2592 (not (eq buffer old-buffer))
2593 (setq entry (assq buffer (window-prev-buffers window))))
2594 (setq new-buffer buffer)
2595 (set-window-buffer-start-and-point
2596 window new-buffer (nth 1 entry) (nth 2 entry))
2597 (throw 'found t)))
2598 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2599 ;; buffers entries.
2600 (dolist (buffer (buffer-list (window-frame window)))
2601 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2602 (not (eq (aref (buffer-name buffer) 0) ?\s))
2603 (not (assq buffer (window-prev-buffers window))))
2604 (if (get-buffer-window buffer)
2605 ;; Try to avoid showing a buffer visible in some other window.
2606 (setq visible buffer)
2607 (setq new-buffer buffer)
2608 (set-window-buffer-start-and-point window new-buffer)
2609 (throw 'found t))))
2610 ;; Scan WINDOW's reverted previous buffers last (must not use
2611 ;; nreverse here!)
2612 (dolist (entry (reverse (window-prev-buffers window)))
2613 (when (and (setq new-buffer (car entry))
2614 (or (buffer-live-p new-buffer)
2615 (not (setq killed-buffers
2616 (cons new-buffer killed-buffers))))
2617 (not (eq new-buffer old-buffer)))
2618 (set-window-buffer-start-and-point
2619 window new-buffer (nth 1 entry) (nth 2 entry))
2620 (throw 'found t)))
2621
2622 ;; Show a buffer visible in another window.
2623 (when visible
2624 (setq new-buffer visible)
2625 (set-window-buffer-start-and-point window new-buffer)))
2626
2627 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2628 (set-window-next-buffers window (delq new-buffer next-buffers))
2629
2630 ;; Remove killed buffers from WINDOW's previous and next buffers.
2631 (when killed-buffers
2632 (dolist (buffer killed-buffers)
2633 (set-window-prev-buffers
2634 window (assq-delete-all buffer (window-prev-buffers window)))
2635 (set-window-next-buffers
2636 window (delq buffer (window-next-buffers window)))))
2637
2638 ;; Return new-buffer.
2639 new-buffer))
2640
2641(defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2642 "Search LIST for a valid buffer to display in FRAME.
2643Return nil when all buffers in LIST are undesirable for display,
2644otherwise return the first suitable buffer in LIST.
2645
2646Buffers not visible in windows are preferred to visible buffers,
2647unless VISIBLE-OK is non-nil.
2648If the optional argument FRAME is nil, it defaults to the selected frame.
2649If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2650 ;; This logic is more or less copied from other-buffer.
2651 (setq frame (or frame (selected-frame)))
2652 (let ((pred (frame-parameter frame 'buffer-predicate))
2653 found buf)
2654 (while (and (not found) list)
2655 (setq buf (car list))
2656 (if (and (not (eq buffer buf))
2657 (buffer-live-p buf)
2658 (or (null pred) (funcall pred buf))
2659 (not (eq (aref (buffer-name buf) 0) ?\s))
2660 (or visible-ok (null (get-buffer-window buf 'visible))))
2661 (setq found buf)
2662 (setq list (cdr list))))
2663 (car list)))
2664
2665(defun last-buffer (&optional buffer visible-ok frame)
2666 "Return the last buffer in FRAME's buffer list.
2667If BUFFER is the last buffer, return the preceding buffer
2668instead. Buffers not visible in windows are preferred to visible
2669buffers, unless optional argument VISIBLE-OK is non-nil.
2670Optional third argument FRAME nil or omitted means use the
2671selected frame's buffer list. If no such buffer exists, return
2672the buffer `*scratch*', creating it if necessary."
2673 (setq frame (or frame (selected-frame)))
2674 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2675 buffer visible-ok frame)
2676 (get-buffer "*scratch*")
2677 (let ((scratch (get-buffer-create "*scratch*")))
2678 (set-buffer-major-mode scratch)
2679 scratch)))
2680
2681(defun bury-buffer (&optional buffer-or-name)
2682 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2683There it is the least likely candidate for `other-buffer' to
2684return; thus, the least likely buffer for \\[switch-to-buffer] to
2685select by default.
2686
2687You can specify a buffer name as BUFFER-OR-NAME, or an actual
2688buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2689current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2690remove the current buffer from the selected window if it is
2691displayed there."
2692 (interactive)
2693 (let* ((buffer (normalize-live-buffer buffer-or-name)))
2694 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2695 ;; although it's not "here" (call it a feature).
2696 (unrecord-buffer buffer)
2697 ;; Handle case where `buffer-or-name' is nil and the current buffer
2698 ;; is shown in the selected window.
2699 (cond
2700 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2701 ((not (window-dedicated-p))
2702 (switch-to-prev-buffer nil 'bury))
2703 ((frame-root-window-p (selected-window))
2704 (iconify-frame (window-frame (selected-window))))
2705 ((window-deletable-p)
2706 (delete-window)))
2707 ;; Always return nil.
2708 nil))
2709
2710(defun unbury-buffer ()
2711 "Switch to the last buffer in the buffer list."
2712 (interactive)
2713 (switch-to-buffer (last-buffer)))
2714
2715(defun next-buffer ()
2716 "In selected window switch to next buffer."
2717 (interactive)
2718 (switch-to-next-buffer))
2719
2720(defun previous-buffer ()
2721 "In selected window switch to previous buffer."
2722 (interactive)
2723 (switch-to-prev-buffer))
2724
2725(defun delete-windows-on (&optional buffer-or-name frame)
2726 "Delete all windows showing BUFFER-OR-NAME.
2727BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2728and defaults to the current buffer.
2729
2730The following non-nil values of the optional argument FRAME
2731have special meanings:
2732
2733- t means consider all windows on the selected frame only.
2734
2735- `visible' means consider all windows on all visible frames on
2736 the current terminal.
2737
2738- 0 (the number zero) means consider all windows on all visible
2739 and iconified frames on the current terminal.
2740
2741- A frame means consider all windows on that frame only.
2742
2743Any other value of FRAME means consider all windows on all
2744frames.
2745
2746When a window showing BUFFER-OR-NAME is dedicated and the only
2747window of its frame, that frame is deleted when there are other
2748frames left."
2749 (interactive "BDelete windows on (buffer):\nP")
2750 (let ((buffer (normalize-live-buffer buffer-or-name))
2751 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2752 ;; `window-list-1' based function.
2753 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2754 (dolist (window (window-list-1 nil nil all-frames))
2755 (if (eq (window-buffer window) buffer)
2756 (let ((deletable (window-deletable-p window)))
2757 (cond
2758 ((eq deletable 'frame)
2759 ;; Delete frame.
2760 (delete-frame (window-frame window)))
2761 (deletable
2762 ;; Delete window only.
2763 (delete-window window))
2764 (t
2765 ;; In window switch to previous buffer.
2766 (set-window-dedicated-p window nil)
2767 (switch-to-prev-buffer window 'bury))))
2768 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
2769 (unrecord-window-buffer window buffer)))))
2770
2771(defun replace-buffer-in-windows (&optional buffer-or-name)
2772 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2773BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2774and defaults to the current buffer.
2775
2776When a window showing BUFFER-OR-NAME is either dedicated, or the
2777window has no previous buffer, that window is deleted. If that
2778window is the only window on its frame, the frame is deleted too
2779when there are other frames left. If there are no other frames
2780left, some other buffer is displayed in that window.
2781
2782This function removes the buffer denoted by BUFFER-OR-NAME from
2783all window-local buffer lists."
2784 (let ((buffer (normalize-live-buffer buffer-or-name)))
2785 (dolist (window (window-list-1 nil nil t))
2786 (if (eq (window-buffer window) buffer)
2787 (let ((deletable (window-deletable-p window)))
2788 (cond
2789 ((eq deletable 'frame)
2790 ;; Delete frame.
2791 (delete-frame (window-frame window)))
2792 ((and (window-dedicated-p window) deletable)
2793 ;; Delete window.
2794 (delete-window window))
2795 (t
2796 ;; Switch to another buffer in window.
2797 (set-window-dedicated-p window nil)
2798 (switch-to-prev-buffer window 'kill))))
2799 ;; Unrecord BUFFER in WINDOW.
2800 (unrecord-window-buffer window buffer)))))
2801
2802(defun quit-restore-window (&optional window kill)
2803 "Quit WINDOW in some way.
2804WINDOW must be a live window and defaults to the selected window.
2805Return nil.
2806
2807According to information stored in WINDOW's `quit-restore' window
2808parameter either \(1) delete WINDOW and its frame, \(2) delete
2809WINDOW, \(3) restore the buffer previously displayed in WINDOW,
2810or \(4) make WINDOW display some other buffer than the present
2811one. If non-nil, reset `quit-restore' parameter to nil.
2812
2813Optional argument KILL non-nil means in addition kill WINDOW's
2814buffer. If KILL is nil, put WINDOW's buffer at the end of the
2815buffer list. Interactively, KILL is the prefix argument."
2816 (interactive "i\nP")
2817 (setq window (normalize-live-window window))
2818 (let ((buffer (window-buffer window))
2819 (quit-restore (window-parameter window 'quit-restore))
2820 deletable resize)
2821 (cond
2822 ((and (or (and (memq (car-safe quit-restore) '(new-window new-frame))
2823 ;; Check that WINDOW's buffer is still the same.
2824 (eq (window-buffer window) (nth 1 quit-restore)))
2825 (window-dedicated-p window))
2826 (setq deletable (window-deletable-p window)))
2827 ;; WINDOW can be deleted.
2828 (unrecord-buffer buffer)
2829 (if (eq deletable 'frame)
2830 ;; WINDOW's frame can be deleted.
2831 (delete-frame (window-frame window))
2832 ;; Just delete WINDOW.
2833 (delete-window window))
2834 ;; If the previously selected window is still alive, select it.
2835 (when (window-live-p (nth 2 quit-restore))
2836 (select-window (nth 2 quit-restore))))
2837 ((and (buffer-live-p (nth 0 quit-restore))
2838 ;; The buffer currently shown in WINDOW must still be the
2839 ;; buffer shown when its `quit-restore' parameter was created
2840 ;; in the first place.
2841 (eq (window-buffer window) (nth 3 quit-restore)))
2842 (setq resize (with-current-buffer buffer temp-buffer-resize-mode))
2843 ;; Unrecord buffer.
2844 (unrecord-buffer buffer)
2845 (unrecord-window-buffer window buffer)
2846 ;; Display buffer stored in the quit-restore parameter.
2847 (set-window-dedicated-p window nil)
2848 (set-window-buffer window (nth 0 quit-restore))
2849 (set-window-start window (nth 1 quit-restore))
2850 (set-window-point window (nth 2 quit-restore))
2851 (when (and resize (/= (nth 4 quit-restore) (window-total-size window)))
2852 (resize-window
2853 window (- (nth 4 quit-restore) (window-total-size window))))
2854 ;; Reset the quit-restore parameter.
2855 (set-window-parameter window 'quit-restore nil)
2856 (when (window-live-p (nth 5 quit-restore))
2857 (select-window (nth 5 quit-restore))))
2858 (t
2859 ;; Otherwise, show another buffer in WINDOW and reset the
2860 ;; quit-restore parameter.
2861 (set-window-parameter window 'quit-restore nil)
2862 (unrecord-buffer buffer)
2863 (switch-to-prev-buffer window 'bury-or-kill)))
2864
2865 ;; Kill WINDOW's old-buffer if requested
2866 (when kill (kill-buffer buffer))
2867 nil))
562dd5e9
MR
2868\f
2869;;; Splitting windows.
2870(defsubst window-split-min-size (&optional horizontal)
2871 "Return minimum height of any window when splitting windows.
2872Optional argument HORIZONTAL non-nil means return minimum width."
2873 (if horizontal
2874 (max window-min-width window-safe-min-width)
2875 (max window-min-height window-safe-min-height)))
2876
2877(defun split-window (&optional window size side)
2878 "Make a new window adjacent to WINDOW.
2879WINDOW can be any window and defaults to the selected one.
2880Return the new window which is always a live window.
2881
2882Optional argument SIZE a positive number means make WINDOW SIZE
2883lines or columns tall. If SIZE is negative, make the new window
2884-SIZE lines or columns tall. If and only if SIZE is non-nil, its
2885absolute value can be less than `window-min-height' or
2886`window-min-width'; so this command can make a new window as
2887small as one line or two columns. SIZE defaults to half of
2888WINDOW's size. Interactively, SIZE is the prefix argument.
2889
2890Optional third argument SIDE nil (or `below') specifies that the
2891new window shall be located below WINDOW. SIDE `above' means the
2892new window shall be located above WINDOW. In both cases SIZE
2893specifies the new number of lines for WINDOW \(or the new window
2894if SIZE is negative) including space reserved for the mode and/or
2895header line.
2896
2897SIDE t (or `right') specifies that the new window shall be
2898located on the right side of WINDOW. SIDE `left' means the new
2899window shall be located on the left of WINDOW. In both cases
2900SIZE specifies the new number of columns for WINDOW \(or the new
2901window provided SIZE is negative) including space reserved for
2902fringes and the scrollbar or a divider column. Any other non-nil
2903value for SIDE is currently handled like t (or `right').
2904
2905If the variable `ignore-window-parameters' is non-nil or the
2906`split-window' parameter of WINDOW equals t, do not process any
2907parameters of WINDOW. Otherwise, if the `split-window' parameter
2908of WINDOW specifies a function, call that function with all three
2909arguments and return the value returned by that function.
2910
2911Otherwise, if WINDOW is part of an atomic window, \"split\" the
2912root of that atomic window. The new window does not become a
2913member of that atomic window.
2914
2915If WINDOW is live, properties of the new window like margins and
2916scrollbars are inherited from WINDOW. If WINDOW is an internal
2917window, these properties as well as the buffer displayed in the
2918new window are inherited from the window selected on WINDOW's
2919frame. The selected window is not changed by this function."
2920 (interactive "i")
2921 (setq window (normalize-any-window window))
2922 (let* ((horizontal (not (memq side '(nil below above))))
2923 (frame (window-frame window))
2924 (parent (window-parent window))
2925 (function (window-parameter window 'split-window))
2926 (window-side (window-parameter window 'window-side))
2927 ;; Rebind `window-nest' since in some cases we may have to
2928 ;; override its value.
2929 (window-nest window-nest)
2930 atom-root)
2931
2932 (window-check frame)
2933 (catch 'done
2934 (cond
2935 ;; Ignore window parameters if either `ignore-window-parameters'
2936 ;; is t or the `split-window' parameter equals t.
2937 ((or ignore-window-parameters (eq function t)))
2938 ((functionp function)
2939 ;; The `split-window' parameter specifies the function to call.
2940 ;; If that function is `ignore', do nothing.
2941 (throw 'done (funcall function window size side)))
2942 ;; If WINDOW is a subwindow of an atomic window, split the root
2943 ;; window of that atomic window instead.
2944 ((and (window-parameter window 'window-atom)
2945 (setq atom-root (window-atom-root window))
2946 (not (eq atom-root window)))
2947 (throw 'done (split-window atom-root size side))))
2948
2949 (when (and window-side
2950 (or (not parent)
2951 (not (window-parameter parent 'window-side))))
2952 ;; WINDOW is a side root window. To make sure that a new parent
2953 ;; window gets created set `window-nest' to t.
2954 (setq window-nest t))
2955
2956 (when (and window-splits size (> size 0))
2957 ;; If `window-splits' is non-nil and SIZE is a non-negative
2958 ;; integer, we cannot reasonably resize other windows. Rather
2959 ;; bind `window-nest' to t to make sure that subsequent window
2960 ;; deletions are handled correctly.
2961 (setq window-nest t))
2962
2963 (let* ((parent-size
2964 ;; `parent-size' is the size of WINDOW's parent, provided
2965 ;; it has one.
2966 (when parent (window-total-size parent horizontal)))
2967 ;; `resize' non-nil means we are supposed to resize other
2968 ;; windows in WINDOW's combination.
2969 (resize
2970 (and window-splits (not window-nest)
2971 ;; Resize makes sense in iso-combinations only.
2972 (window-iso-combined-p window horizontal)))
2973 ;; `old-size' is the current size of WINDOW.
2974 (old-size (window-total-size window horizontal))
2975 ;; `new-size' is the specified or calculated size of the
2976 ;; new window.
2977 (new-size
2978 (cond
2979 ((not size)
2980 (max (window-split-min-size horizontal)
2981 (if resize
2982 ;; When resizing try to give the new window the
2983 ;; average size of a window in its combination.
2984 (min (- parent-size
2985 (window-min-size parent horizontal))
2986 (/ parent-size
2987 (1+ (window-iso-combinations
2988 parent horizontal))))
2989 ;; Else try to give the new window half the size
2990 ;; of WINDOW (plus an eventual odd line).
2991 (+ (/ old-size 2) (% old-size 2)))))
2992 ((>= size 0)
2993 ;; SIZE non-negative specifies the new size of WINDOW.
2994
2995 ;; Note: Specifying a non-negative SIZE is practically
2996 ;; always done as workaround for making the new window
2997 ;; appear above or on the left of the new window (the
2998 ;; ispell window is a typical example of that). In all
2999 ;; these cases the SIDE argument should be set to 'above
3000 ;; or 'left in order to support the 'resize option.
3001 ;; Here we have to nest the windows instead, see above.
3002 (- old-size size))
3003 (t
3004 ;; SIZE negative specifies the size of the new window.
3005 (- size))))
3006 new-parent new-normal)
3007
3008 ;; Check SIZE.
3009 (cond
3010 ((not size)
3011 (cond
3012 (resize
3013 ;; SIZE unspecified, resizing.
3014 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3015 ;; Try again with minimum split size.
3016 (setq new-size
3017 (max new-size (window-split-min-size horizontal)))
3018 (not (window-sizable-p parent (- new-size) horizontal)))
3019 (error "Window %s too small for splitting" parent)))
3020 ((> (+ new-size (window-min-size window horizontal)) old-size)
3021 ;; SIZE unspecified, no resizing.
3022 (error "Window %s too small for splitting" window))))
3023 ((and (>= size 0)
3024 (or (>= size old-size)
3025 (< new-size (if horizontal
3026 window-safe-min-width
3027 window-safe-min-width))))
3028 ;; SIZE specified as new size of old window. If the new size
3029 ;; is larger than the old size or the size of the new window
3030 ;; would be less than the safe minimum, signal an error.
3031 (error "Window %s too small for splitting" window))
3032 (resize
3033 ;; SIZE specified, resizing.
3034 (unless (window-sizable-p parent (- new-size) horizontal)
3035 ;; If we cannot resize the parent give up.
3036 (error "Window %s too small for splitting" parent)))
3037 ((or (< new-size
3038 (if horizontal window-safe-min-width window-safe-min-height))
3039 (< (- old-size new-size)
3040 (if horizontal window-safe-min-width window-safe-min-height)))
3041 ;; SIZE specification violates minimum size restrictions.
3042 (error "Window %s too small for splitting" window)))
3043
3044 (resize-window-reset frame horizontal)
3045
3046 (setq new-parent
3047 ;; Make new-parent non-nil if we need a new parent window;
3048 ;; either because we want to nest or because WINDOW is not
3049 ;; iso-combined.
3050 (or window-nest (not (window-iso-combined-p window horizontal))))
3051 (setq new-normal
3052 ;; Make new-normal the normal size of the new window.
3053 (cond
3054 (size (/ (float new-size) (if new-parent old-size parent-size)))
3055 (new-parent 0.5)
3056 (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal))))
3057 (t (/ (window-normal-size window horizontal) 2.0))))
3058
3059 (if resize
3060 ;; Try to get space from OLD's siblings. We could go "up" and
3061 ;; try getting additional space from surrounding windows but
3062 ;; we won't be able to return space to those windows when we
3063 ;; delete the one we create here. Hence we do not go up.
3064 (progn
3065 (resize-subwindows parent (- new-size) horizontal)
3066 (let* ((normal (- 1.0 new-normal))
3067 (sub (window-child parent)))
3068 (while sub
3069 (set-window-new-normal
3070 sub (* (window-normal-size sub horizontal) normal))
3071 (setq sub (window-right sub)))))
3072 ;; Get entire space from WINDOW.
3073 (set-window-new-total window (- old-size new-size))
3074 (resize-this-window window (- new-size) horizontal)
3075 (set-window-new-normal
3076 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3077 new-normal)))
3078
3079 (let* ((new (split-window-internal window new-size side new-normal)))
3080 ;; Inherit window-side parameters, if any.
3081 (when (and window-side new-parent)
3082 (set-window-parameter (window-parent new) 'window-side window-side)
3083 (set-window-parameter new 'window-side window-side))
3084
3085 (run-window-configuration-change-hook frame)
3086 (window-check frame)
3087 ;; Always return the new window.
3088 new)))))
3089
3090;; I think this should be the default; I think people will prefer it--rms.
3091(defcustom split-window-keep-point t
3092 "If non-nil, \\[split-window-above-each-other] keeps the original point \
3093in both children.
3094This is often more convenient for editing.
3095If nil, adjust point in each of the two windows to minimize redisplay.
3096This is convenient on slow terminals, but point can move strangely.
3097
3098This option applies only to `split-window-above-each-other' and
3099functions that call it. `split-window' always keeps the original
3100point in both children."
3101 :type 'boolean
3102 :group 'windows)
3103
3104(defun split-window-above-each-other (&optional size)
3105 "Split selected window into two windows, one above the other.
3106The upper window gets SIZE lines and the lower one gets the rest.
3107SIZE negative means the lower window gets -SIZE lines and the
3108upper one the rest. With no argument, split windows equally or
3109close to it. Both windows display the same buffer, now current.
3110
3111If the variable `split-window-keep-point' is non-nil, both new
3112windows will get the same value of point as the selected window.
3113This is often more convenient for editing. The upper window is
3114the selected window.
3115
3116Otherwise, we choose window starts so as to minimize the amount of
3117redisplay; this is convenient on slow terminals. The new selected
3118window is the one that the current value of point appears in. The
3119value of point can change if the text around point is hidden by the
3120new mode line.
3121
3122Regardless of the value of `split-window-keep-point', the upper
3123window is the original one and the return value is the new, lower
3124window."
3125 (interactive "P")
3126 (let ((old-window (selected-window))
3127 (old-point (point))
3128 (size (and size (prefix-numeric-value size)))
3129 moved-by-window-height moved new-window bottom)
3130 (when (and size (< size 0) (< (- size) window-min-height))
3131 ;; `split-window' would not signal an error here.
3132 (error "Size of new window too small"))
3133 (setq new-window (split-window nil size))
3134 (unless split-window-keep-point
3135 (with-current-buffer (window-buffer)
3136 (goto-char (window-start))
3137 (setq moved (vertical-motion (window-height)))
3138 (set-window-start new-window (point))
3139 (when (> (point) (window-point new-window))
3140 (set-window-point new-window (point)))
3141 (when (= moved (window-height))
3142 (setq moved-by-window-height t)
3143 (vertical-motion -1))
3144 (setq bottom (point)))
3145 (and moved-by-window-height
3146 (<= bottom (point))
3147 (set-window-point old-window (1- bottom)))
3148 (and moved-by-window-height
3149 (<= (window-start new-window) old-point)
3150 (set-window-point new-window old-point)
3151 (select-window new-window)))
9397e56f
MR
3152 ;; Always copy quit-restore parameter in interactive use.
3153 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3154 (when quit-restore
3155 (set-window-parameter new-window 'quit-restore quit-restore)))
3156 new-window))
562dd5e9
MR
3157
3158(defalias 'split-window-vertically 'split-window-above-each-other)
3159
562dd5e9
MR
3160(defun split-window-side-by-side (&optional size)
3161 "Split selected window into two windows side by side.
3162The selected window becomes the left one and gets SIZE columns.
3163SIZE negative means the right window gets -SIZE lines.
3164
3165SIZE includes the width of the window's scroll bar; if there are
3166no scroll bars, it includes the width of the divider column to
3167the window's right, if any. SIZE omitted or nil means split
3168window equally.
3169
3170The selected window remains selected. Return the new window."
3171 (interactive "P")
3172 (let ((old-window (selected-window))
3173 (size (and size (prefix-numeric-value size)))
3174 new-window)
3175 (when (and size (< size 0) (< (- size) window-min-width))
3176 ;; `split-window' would not signal an error here.
3177 (error "Size of new window too small"))
9397e56f
MR
3178 (setq new-window (split-window nil size t))
3179 ;; Always copy quit-restore parameter in interactive use.
3180 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3181 (when quit-restore
3182 (set-window-parameter new-window 'quit-restore quit-restore)))
3183 new-window))
562dd5e9
MR
3184
3185(defalias 'split-window-horizontally 'split-window-side-by-side)
3186\f
3c448ab6
MR
3187;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3188;;; `balance-windows' subroutines using `window-tree'
3189
3190;;; Translate from internal window tree format
3191
3192(defun bw-get-tree (&optional window-or-frame)
3193 "Get a window split tree in our format.
3194
3195WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil,
3196then the whole window split tree for `selected-frame' is returned.
3197If it is a frame, then this is used instead. If it is a window,
3198then the smallest tree containing that window is returned."
3199 (when window-or-frame
3200 (unless (or (framep window-or-frame)
3201 (windowp window-or-frame))
3202 (error "Not a frame or window: %s" window-or-frame)))
3203 (let ((subtree (bw-find-tree-sub window-or-frame)))
3204 (when subtree
3205 (if (integerp subtree)
3206 nil
3207 (bw-get-tree-1 subtree)))))
3208
3209(defun bw-get-tree-1 (split)
3210 (if (windowp split)
3211 split
3212 (let ((dir (car split))
3213 (edges (car (cdr split)))
3214 (childs (cdr (cdr split))))
3215 (list
3216 (cons 'dir (if dir 'ver 'hor))
3217 (cons 'b (nth 3 edges))
3218 (cons 'r (nth 2 edges))
3219 (cons 't (nth 1 edges))
3220 (cons 'l (nth 0 edges))
3221 (cons 'childs (mapcar #'bw-get-tree-1 childs))))))
3222
3223(defun bw-find-tree-sub (window-or-frame &optional get-parent)
3224 (let* ((window (when (windowp window-or-frame) window-or-frame))
3225 (frame (when (windowp window) (window-frame window)))
3226 (wt (car (window-tree frame))))
3227 (when (< 1 (length (window-list frame 0)))
3228 (if window
3229 (bw-find-tree-sub-1 wt window get-parent)
3230 wt))))
3231
3232(defun bw-find-tree-sub-1 (tree win &optional get-parent)
3233 (unless (windowp win) (error "Not a window: %s" win))
3234 (if (memq win tree)
3235 (if get-parent
3236 get-parent
3237 tree)
3238 (let ((childs (cdr (cdr tree)))
3239 child
3240 subtree)
3241 (while (and childs (not subtree))
3242 (setq child (car childs))
3243 (setq childs (cdr childs))
3244 (when (and child (listp child))
3245 (setq subtree (bw-find-tree-sub-1 child win get-parent))))
3246 (if (integerp subtree)
3247 (progn
3248 (if (= 1 subtree)
3249 tree
3250 (1- subtree)))
3251 subtree
3252 ))))
3253
3254;;; Window or object edges
3255
3256(defun bw-l (obj)
3257 "Left edge of OBJ."
3258 (if (windowp obj) (nth 0 (window-edges obj)) (cdr (assq 'l obj))))
3259(defun bw-t (obj)
3260 "Top edge of OBJ."
3261 (if (windowp obj) (nth 1 (window-edges obj)) (cdr (assq 't obj))))
3262(defun bw-r (obj)
3263 "Right edge of OBJ."
3264 (if (windowp obj) (nth 2 (window-edges obj)) (cdr (assq 'r obj))))
3265(defun bw-b (obj)
3266 "Bottom edge of OBJ."
3267 (if (windowp obj) (nth 3 (window-edges obj)) (cdr (assq 'b obj))))
3268
3269;;; Split directions
3270
3271(defun bw-dir (obj)
3272 "Return window split tree direction if OBJ.
3273If OBJ is a window return 'both. If it is a window split tree
3274then return its direction."
3275 (if (symbolp obj)
3276 obj
3277 (if (windowp obj)
3278 'both
3279 (let ((dir (cdr (assq 'dir obj))))
3280 (unless (memq dir '(hor ver both))
3281 (error "Can't find dir in %s" obj))
3282 dir))))
3283
3284(defun bw-eqdir (obj1 obj2)
3285 "Return t if window split tree directions are equal.
3286OBJ1 and OBJ2 should be either windows or window split trees in
3287our format. The directions returned by `bw-dir' are compared and
3288t is returned if they are `eq' or one of them is 'both."
3289 (let ((dir1 (bw-dir obj1))
3290 (dir2 (bw-dir obj2)))
3291 (or (eq dir1 dir2)
3292 (eq dir1 'both)
3293 (eq dir2 'both))))
3294
3295;;; Building split tree
3296
3297(defun bw-refresh-edges (obj)
3298 "Refresh the edge information of OBJ and return OBJ."
3299 (unless (windowp obj)
3300 (let ((childs (cdr (assq 'childs obj)))
3301 (ol 1000)
3302 (ot 1000)
3303 (or -1)
3304 (ob -1))
3305 (dolist (o childs)
3306 (when (> ol (bw-l o)) (setq ol (bw-l o)))
3307 (when (> ot (bw-t o)) (setq ot (bw-t o)))
3308 (when (< or (bw-r o)) (setq or (bw-r o)))
3309 (when (< ob (bw-b o)) (setq ob (bw-b o))))
3310 (setq obj (delq 'l obj))
3311 (setq obj (delq 't obj))
3312 (setq obj (delq 'r obj))
3313 (setq obj (delq 'b obj))
3314 (add-to-list 'obj (cons 'l ol))
3315 (add-to-list 'obj (cons 't ot))
3316 (add-to-list 'obj (cons 'r or))
3317 (add-to-list 'obj (cons 'b ob))
3318 ))
3319 obj)
3320
3321;;; Balance windows
3322
3323(defun balance-windows (&optional window-or-frame)
3324 "Make windows the same heights or widths in window split subtrees.
3325
3326When called non-interactively WINDOW-OR-FRAME may be either a
3327window or a frame. It then balances the windows on the implied
3328frame. If the parameter is a window only the corresponding window
3329subtree is balanced."
3330 (interactive)
3331 (let (
3332 (wt (bw-get-tree window-or-frame))
3333 (w)
3334 (h)
3335 (tried-sizes)
3336 (last-sizes)
3337 (windows (window-list nil 0)))
3338 (when wt
3339 (while (not (member last-sizes tried-sizes))
3340 (when last-sizes (setq tried-sizes (cons last-sizes tried-sizes)))
3341 (setq last-sizes (mapcar (lambda (w)
3342 (window-edges w))
3343 windows))
3344 (when (eq 'hor (bw-dir wt))
3345 (setq w (- (bw-r wt) (bw-l wt))))
3346 (when (eq 'ver (bw-dir wt))
3347 (setq h (- (bw-b wt) (bw-t wt))))
3348 (bw-balance-sub wt w h)))))
3349
3350(defun bw-adjust-window (window delta horizontal)
3351 "Wrapper around `adjust-window-trailing-edge' with error checking.
3352Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3353 ;; `adjust-window-trailing-edge' may fail if delta is too large.
3354 (while (>= (abs delta) 1)
06b60517 3355 (condition-case nil
3c448ab6
MR
3356 (progn
3357 (adjust-window-trailing-edge window delta horizontal)
3358 (setq delta 0))
3359 (error
3360 ;;(message "adjust: %s" (error-message-string err))
3361 (setq delta (/ delta 2))))))
3362
3363(defun bw-balance-sub (wt w h)
3364 (setq wt (bw-refresh-edges wt))
3365 (unless w (setq w (- (bw-r wt) (bw-l wt))))
3366 (unless h (setq h (- (bw-b wt) (bw-t wt))))
3367 (if (windowp wt)
3368 (progn
3369 (when w
3370 (let ((dw (- w (- (bw-r wt) (bw-l wt)))))
3371 (when (/= 0 dw)
3372 (bw-adjust-window wt dw t))))
3373 (when h
3374 (let ((dh (- h (- (bw-b wt) (bw-t wt)))))
3375 (when (/= 0 dh)
3376 (bw-adjust-window wt dh nil)))))
3377 (let* ((childs (cdr (assq 'childs wt)))
3378 (cw (when w (/ w (if (bw-eqdir 'hor wt) (length childs) 1))))
3379 (ch (when h (/ h (if (bw-eqdir 'ver wt) (length childs) 1)))))
3380 (dolist (c childs)
3381 (bw-balance-sub c cw ch)))))
3382
3383(defun window-fixed-size-p (&optional window direction)
3384 "Return t if WINDOW cannot be resized in DIRECTION.
3385WINDOW defaults to the selected window. DIRECTION can be
3386nil (i.e. any), `height' or `width'."
3387 (with-current-buffer (window-buffer window)
3388 (when (and (boundp 'window-size-fixed) window-size-fixed)
3389 (not (and direction
3390 (member (cons direction window-size-fixed)
3391 '((height . width) (width . height))))))))
3392
3393;;; A different solution to balance-windows.
3394
3395(defvar window-area-factor 1
3396 "Factor by which the window area should be over-estimated.
3397This is used by `balance-windows-area'.
3398Changing this globally has no effect.")
3399(make-variable-buffer-local 'window-area-factor)
3400
3401(defun balance-windows-area ()
3402 "Make all visible windows the same area (approximately).
3403See also `window-area-factor' to change the relative size of
3404specific buffers."
3405 (interactive)
3406 (let* ((unchanged 0) (carry 0) (round 0)
3407 ;; Remove fixed-size windows.
3408 (wins (delq nil (mapcar (lambda (win)
3409 (if (not (window-fixed-size-p win)) win))
3410 (window-list nil 'nomini))))
3411 (changelog nil)
3412 next)
3413 ;; Resizing a window changes the size of surrounding windows in complex
3414 ;; ways, so it's difficult to balance them all. The introduction of
3415 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3416 ;; very difficult to do. `balance-window' above takes an off-line
3417 ;; approach: get the whole window tree, then balance it, then try to
3418 ;; adjust the windows so they fit the result.
3419 ;; Here, instead, we take a "local optimization" approach, where we just
3420 ;; go through all the windows several times until nothing needs to be
3421 ;; changed. The main problem with this approach is that it's difficult
3422 ;; to make sure it terminates, so we use some heuristic to try and break
3423 ;; off infinite loops.
3424 ;; After a round without any change, we allow a second, to give a chance
3425 ;; to the carry to propagate a minor imbalance from the end back to
3426 ;; the beginning.
3427 (while (< unchanged 2)
3428 ;; (message "New round")
3429 (setq unchanged (1+ unchanged) round (1+ round))
3430 (dolist (win wins)
3431 (setq next win)
3432 (while (progn (setq next (next-window next))
3433 (window-fixed-size-p next)))
3434 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3435 (let* ((horiz
3436 (< (car (window-edges win)) (car (window-edges next))))
3437 (areadiff (/ (- (* (window-height next) (window-width next)
3438 (buffer-local-value 'window-area-factor
3439 (window-buffer next)))
3440 (* (window-height win) (window-width win)
3441 (buffer-local-value 'window-area-factor
3442 (window-buffer win))))
3443 (max (buffer-local-value 'window-area-factor
3444 (window-buffer win))
3445 (buffer-local-value 'window-area-factor
3446 (window-buffer next)))))
3447 (edgesize (if horiz
3448 (+ (window-height win) (window-height next))
3449 (+ (window-width win) (window-width next))))
3450 (diff (/ areadiff edgesize)))
3451 (when (zerop diff)
3452 ;; Maybe diff is actually closer to 1 than to 0.
3453 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3454 (when (and (zerop diff) (not (zerop areadiff)))
3455 (setq diff (/ (+ areadiff carry) edgesize))
3456 ;; Change things smoothly.
3457 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3458 (if (zerop diff)
3459 ;; Make sure negligible differences don't accumulate to
3460 ;; become significant.
3461 (setq carry (+ carry areadiff))
3462 (bw-adjust-window win diff horiz)
3463 ;; (sit-for 0.5)
3464 (let ((change (cons win (window-edges win))))
3465 ;; If the same change has been seen already for this window,
3466 ;; we're most likely in an endless loop, so don't count it as
3467 ;; a change.
3468 (unless (member change changelog)
3469 (push change changelog)
3470 (setq unchanged 0 carry 0)))))))
3471 ;; We've now basically balanced all the windows.
3472 ;; But there may be some minor off-by-one imbalance left over,
3473 ;; so let's do some fine tuning.
3474 ;; (bw-finetune wins)
3475 ;; (message "Done in %d rounds" round)
3476 ))
3477
3478\f
3479(defcustom display-buffer-function nil
3480 "If non-nil, function to call to handle `display-buffer'.
3481It will receive two args, the buffer and a flag which if non-nil
3482means that the currently selected window is not acceptable. It
3483should choose or create a window, display the specified buffer in
3484it, and return the window.
3485
3486Commands such as `switch-to-buffer-other-window' and
3487`find-file-other-window' work using this function."
3488 :type '(choice
3489 (const nil)
3490 (function :tag "function"))
3491 :group 'windows)
3492
56f31926
MR
3493(defcustom special-display-buffer-names nil
3494 "List of names of buffers that should be displayed specially.
3495Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3496its name is in this list, displays the buffer in a way specified
3497by `special-display-function'. `special-display-popup-frame'
3498\(the default for `special-display-function') usually displays
3499the buffer in a separate frame made with the parameters specified
3500by `special-display-frame-alist'. If `special-display-function'
3501has been set to some other function, that function is called with
3502the buffer as first, and nil as second argument.
3503
3504Alternatively, an element of this list can be specified as
3505\(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
3506name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
3507`special-display-popup-frame' will interpret such pairs as frame
3508parameters when it creates a special frame, overriding the
3509corresponding values from `special-display-frame-alist'.
3510
3511As a special case, if FRAME-PARAMETERS contains (same-window . t)
3512`special-display-popup-frame' displays that buffer in the
3513selected window. If FRAME-PARAMETERS contains (same-frame . t),
3514it displays that buffer in a window on the selected frame.
3515
3516If `special-display-function' specifies some other function than
3517`special-display-popup-frame', that function is called with the
3518buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
3519argument.
3520
3521Finally, an element of this list can be also specified as
3522\(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
3523`special-display-popup-frame' will call FUNCTION with the buffer
3524named BUFFER-NAME as first argument, and OTHER-ARGS as the
3525second. If `special-display-function' specifies some other
3526function, that function is called with the buffer named
3527BUFFER-NAME as first, and the element's cdr as second argument.
3528
3529If this variable appears \"not to work\", because you added a
3530name to it but the corresponding buffer is displayed in the
3531selected window, look at the values of `same-window-buffer-names'
3532and `same-window-regexps'. Those variables take precedence over
3533this one.
3534
3535See also `special-display-regexps'."
3536 :type '(repeat
3537 (choice :tag "Buffer"
3538 :value ""
3539 (string :format "%v")
3540 (cons :tag "With parameters"
3541 :format "%v"
3542 :value ("" . nil)
3543 (string :format "%v")
3544 (repeat :tag "Parameters"
3545 (cons :format "%v"
3546 (symbol :tag "Parameter")
3547 (sexp :tag "Value"))))
3548 (list :tag "With function"
3549 :format "%v"
3550 :value ("" . nil)
3551 (string :format "%v")
3552 (function :tag "Function")
3553 (repeat :tag "Arguments" (sexp)))))
3554 :group 'windows
3555 :group 'frames)
3556
ac549fa5
GM
3557;;;###autoload
3558(put 'special-display-buffer-names 'risky-local-variable t)
3559
56f31926
MR
3560(defcustom special-display-regexps nil
3561 "List of regexps saying which buffers should be displayed specially.
3562Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3563any regexp in this list matches its name, displays it specially
3564using `special-display-function'. `special-display-popup-frame'
3565\(the default for `special-display-function') usually displays
3566the buffer in a separate frame made with the parameters specified
3567by `special-display-frame-alist'. If `special-display-function'
3568has been set to some other function, that function is called with
3569the buffer as first, and nil as second argument.
3570
3571Alternatively, an element of this list can be specified as
3572\(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
3573FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
3574`special-display-popup-frame' will then interpret these pairs as
3575frame parameters when creating a special frame for a buffer whose
3576name matches REGEXP, overriding the corresponding values from
3577`special-display-frame-alist'.
3578
3579As a special case, if FRAME-PARAMETERS contains (same-window . t)
3580`special-display-popup-frame' displays buffers matching REGEXP in
3581the selected window. \(same-frame . t) in FRAME-PARAMETERS means
3582to display such buffers in a window on the selected frame.
3583
3584If `special-display-function' specifies some other function than
3585`special-display-popup-frame', that function is called with the
3586buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
3587as second argument.
3588
3589Finally, an element of this list can be also specified as
3590\(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
3591will then call FUNCTION with the buffer whose name matched
3592REGEXP as first, and OTHER-ARGS as second argument. If
3593`special-display-function' specifies some other function, that
3594function is called with the buffer whose name matched REGEXP
3595as first, and the element's cdr as second argument.
3596
3597If this variable appears \"not to work\", because you added a
3598name to it but the corresponding buffer is displayed in the
3599selected window, look at the values of `same-window-buffer-names'
3600and `same-window-regexps'. Those variables take precedence over
3601this one.
3602
3603See also `special-display-buffer-names'."
3604 :type '(repeat
3605 (choice :tag "Buffer"
3606 :value ""
3607 (regexp :format "%v")
3608 (cons :tag "With parameters"
3609 :format "%v"
3610 :value ("" . nil)
3611 (regexp :format "%v")
3612 (repeat :tag "Parameters"
3613 (cons :format "%v"
3614 (symbol :tag "Parameter")
3615 (sexp :tag "Value"))))
3616 (list :tag "With function"
3617 :format "%v"
3618 :value ("" . nil)
3619 (regexp :format "%v")
3620 (function :tag "Function")
3621 (repeat :tag "Arguments" (sexp)))))
3622 :group 'windows
3623 :group 'frames)
3624
3c448ab6
MR
3625(defun special-display-p (buffer-name)
3626 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
56f31926
MR
3627More precisely, return t if `special-display-buffer-names' or
3628`special-display-regexps' contain a string entry equaling or
3629matching BUFFER-NAME. If `special-display-buffer-names' or
3630`special-display-regexps' contain a list entry whose car equals
3631or matches BUFFER-NAME, the return value is the cdr of that
3632entry."
98722073
MR
3633 (let (tmp)
3634 (cond
3635 ((not (stringp buffer-name)))
3636 ((member buffer-name special-display-buffer-names)
3637 t)
3638 ((setq tmp (assoc buffer-name special-display-buffer-names))
3639 (cdr tmp))
3640 ((catch 'found
3641 (dolist (regexp special-display-regexps)
3642 (cond
3643 ((stringp regexp)
3644 (when (string-match-p regexp buffer-name)
3645 (throw 'found t)))
3646 ((and (consp regexp) (stringp (car regexp))
3647 (string-match-p (car regexp) buffer-name))
3648 (throw 'found (cdr regexp))))))))))
3c448ab6
MR
3649
3650(defcustom special-display-function 'special-display-popup-frame
56f31926
MR
3651 "Function to call for displaying special buffers.
3652This function is called with two arguments - the buffer and,
3653optionally, a list - and should return a window displaying that
3654buffer. The default value usually makes a separate frame for the
3655buffer using `special-display-frame-alist' to specify the frame
3656parameters. See the definition of `special-display-popup-frame'
3657for how to specify such a function.
3658
3659A buffer is special when its name is either listed in
3c448ab6
MR
3660`special-display-buffer-names' or matches a regexp in
3661`special-display-regexps'."
3662 :type 'function
3663 :group 'frames)
3664
3c448ab6
MR
3665(defcustom same-window-buffer-names nil
3666 "List of names of buffers that should appear in the \"same\" window.
3667`display-buffer' and `pop-to-buffer' show a buffer whose name is
3668on this list in the selected rather than some other window.
3669
3670An element of this list can be a cons cell instead of just a
56f31926
MR
3671string. In that case, the cell's car must be a string specifying
3672the buffer name. This is for compatibility with
3c448ab6
MR
3673`special-display-buffer-names'; the cdr of the cons cell is
3674ignored.
3675
3676See also `same-window-regexps'."
3677 :type '(repeat (string :format "%v"))
3678 :group 'windows)
3679
3680(defcustom same-window-regexps nil
3681 "List of regexps saying which buffers should appear in the \"same\" window.
3682`display-buffer' and `pop-to-buffer' show a buffer whose name
3683matches a regexp on this list in the selected rather than some
3684other window.
3685
3686An element of this list can be a cons cell instead of just a
56f31926 3687string. In that case, the cell's car must be a regexp matching
3c448ab6 3688the buffer name. This is for compatibility with
56f31926 3689`special-display-regexps'; the cdr of the cons cell is ignored.
3c448ab6
MR
3690
3691See also `same-window-buffer-names'."
3692 :type '(repeat (regexp :format "%v"))
3693 :group 'windows)
3694
56f31926
MR
3695(defun same-window-p (buffer-name)
3696 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
3697This function returns non-nil if `display-buffer' or
3698`pop-to-buffer' would show a buffer named BUFFER-NAME in the
3699selected rather than \(as usual\) some other window. See
3700`same-window-buffer-names' and `same-window-regexps'."
3701 (cond
3702 ((not (stringp buffer-name)))
3703 ;; The elements of `same-window-buffer-names' can be buffer
3704 ;; names or cons cells whose cars are buffer names.
3705 ((member buffer-name same-window-buffer-names))
3706 ((assoc buffer-name same-window-buffer-names))
3707 ((catch 'found
3708 (dolist (regexp same-window-regexps)
3709 ;; The elements of `same-window-regexps' can be regexps
3710 ;; or cons cells whose cars are regexps.
3711 (when (or (and (stringp regexp)
3712 (string-match regexp buffer-name))
3713 (and (consp regexp) (stringp (car regexp))
3714 (string-match-p (car regexp) buffer-name)))
3715 (throw 'found t)))))))
3716
3c448ab6
MR
3717(defcustom pop-up-frames nil
3718 "Whether `display-buffer' should make a separate frame.
d1f18ec0 3719If nil, never make a separate frame.
3c448ab6
MR
3720If the value is `graphic-only', make a separate frame
3721on graphic displays only.
3722Any other non-nil value means always make a separate frame."
3723 :type '(choice
3724 (const :tag "Never" nil)
3725 (const :tag "On graphic displays only" graphic-only)
3726 (const :tag "Always" t))
3727 :group 'windows)
3728
3729(defcustom display-buffer-reuse-frames nil
3730 "Non-nil means `display-buffer' should reuse frames.
3731If the buffer in question is already displayed in a frame, raise
3732that frame."
3733 :type 'boolean
3734 :version "21.1"
3735 :group 'windows)
3736
3737(defcustom pop-up-windows t
3738 "Non-nil means `display-buffer' should make a new window."
3739 :type 'boolean
3740 :group 'windows)
3741
8b10a2d1
MR
3742(defcustom split-window-preferred-function 'split-window-sensibly
3743 "Function called by `display-buffer' routines to split a window.
3744This function is called with a window as single argument and is
3745supposed to split that window and return the new window. If the
3746window can (or shall) not be split, it is supposed to return nil.
3747The default is to call the function `split-window-sensibly' which
3748tries to split the window in a way which seems most suitable.
3749You can customize the options `split-height-threshold' and/or
3750`split-width-threshold' in order to have `split-window-sensibly'
3751prefer either vertical or horizontal splitting.
3752
3753If you set this to any other function, bear in mind that the
3754`display-buffer' routines may call this function two times. The
3755argument of the first call is the largest window on its frame.
3756If that call fails to return a live window, the function is
3757called again with the least recently used window as argument. If
3758that call fails too, `display-buffer' will use an existing window
3759to display its buffer.
3760
3761The window selected at the time `display-buffer' was invoked is
3762still selected when this function is called. Hence you can
3763compare the window argument with the value of `selected-window'
3764if you intend to split the selected window instead or if you do
3765not want to split the selected window."
3766 :type 'function
3c448ab6
MR
3767 :version "23.1"
3768 :group 'windows)
3769
8b10a2d1
MR
3770(defcustom split-height-threshold 80
3771 "Minimum height for splitting windows sensibly.
3772If this is an integer, `split-window-sensibly' may split a window
3773vertically only if it has at least this many lines. If this is
3774nil, `split-window-sensibly' is not allowed to split a window
3775vertically. If, however, a window is the only window on its
3776frame, `split-window-sensibly' may split it vertically
3777disregarding the value of this variable."
3778 :type '(choice (const nil) (integer :tag "lines"))
3c448ab6
MR
3779 :version "23.1"
3780 :group 'windows)
3781
8b10a2d1
MR
3782(defcustom split-width-threshold 160
3783 "Minimum width for splitting windows sensibly.
3784If this is an integer, `split-window-sensibly' may split a window
3785horizontally only if it has at least this many columns. If this
3786is nil, `split-window-sensibly' is not allowed to split a window
3787horizontally."
3788 :type '(choice (const nil) (integer :tag "columns"))
3c448ab6
MR
3789 :version "23.1"
3790 :group 'windows)
3791
8b10a2d1
MR
3792(defun window-splittable-p (window &optional horizontal)
3793 "Return non-nil if `split-window-sensibly' may split WINDOW.
3794Optional argument HORIZONTAL nil or omitted means check whether
3795`split-window-sensibly' may split WINDOW vertically. HORIZONTAL
3796non-nil means check whether WINDOW may be split horizontally.
3c448ab6 3797
8b10a2d1 3798WINDOW may be split vertically when the following conditions
3c448ab6 3799hold:
3c448ab6
MR
3800- `window-size-fixed' is either nil or equals `width' for the
3801 buffer of WINDOW.
8b10a2d1 3802- `split-height-threshold' is an integer and WINDOW is at least as
3c448ab6 3803 high as `split-height-threshold'.
3c448ab6
MR
3804- When WINDOW is split evenly, the emanating windows are at least
3805 `window-min-height' lines tall and can accommodate at least one
3806 line plus - if WINDOW has one - a mode line.
3807
8b10a2d1 3808WINDOW may be split horizontally when the following conditions
3c448ab6 3809hold:
3c448ab6
MR
3810- `window-size-fixed' is either nil or equals `height' for the
3811 buffer of WINDOW.
8b10a2d1 3812- `split-width-threshold' is an integer and WINDOW is at least as
3c448ab6 3813 wide as `split-width-threshold'.
3c448ab6
MR
3814- When WINDOW is split evenly, the emanating windows are at least
3815 `window-min-width' or two (whichever is larger) columns wide."
3816 (when (window-live-p window)
3817 (with-current-buffer (window-buffer window)
3818 (if horizontal
3819 ;; A window can be split horizontally when its width is not
3820 ;; fixed, it is at least `split-width-threshold' columns wide
3821 ;; and at least twice as wide as `window-min-width' and 2 (the
3822 ;; latter value is hardcoded).
3823 (and (memq window-size-fixed '(nil height))
3824 ;; Testing `window-full-width-p' here hardly makes any
3825 ;; sense nowadays. This can be done more intuitively by
3826 ;; setting up `split-width-threshold' appropriately.
3827 (numberp split-width-threshold)
3828 (>= (window-width window)
3829 (max split-width-threshold
3830 (* 2 (max window-min-width 2)))))
3831 ;; A window can be split vertically when its height is not
3832 ;; fixed, it is at least `split-height-threshold' lines high,
3833 ;; and it is at least twice as high as `window-min-height' and 2
3834 ;; if it has a modeline or 1.
3835 (and (memq window-size-fixed '(nil width))
3836 (numberp split-height-threshold)
3837 (>= (window-height window)
3838 (max split-height-threshold
3839 (* 2 (max window-min-height
3840 (if mode-line-format 2 1))))))))))
3841
8b10a2d1
MR
3842(defun split-window-sensibly (window)
3843 "Split WINDOW in a way suitable for `display-buffer'.
3844If `split-height-threshold' specifies an integer, WINDOW is at
3845least `split-height-threshold' lines tall and can be split
3846vertically, split WINDOW into two windows one above the other and
3847return the lower window. Otherwise, if `split-width-threshold'
3848specifies an integer, WINDOW is at least `split-width-threshold'
3849columns wide and can be split horizontally, split WINDOW into two
3850windows side by side and return the window on the right. If this
3851can't be done either and WINDOW is the only window on its frame,
3852try to split WINDOW vertically disregarding any value specified
3853by `split-height-threshold'. If that succeeds, return the lower
3854window. Return nil otherwise.
3855
3856By default `display-buffer' routines call this function to split
3857the largest or least recently used window. To change the default
3858customize the option `split-window-preferred-function'.
3859
3860You can enforce this function to not split WINDOW horizontally,
3861by setting \(or binding) the variable `split-width-threshold' to
3862nil. If, in addition, you set `split-height-threshold' to zero,
3863chances increase that this function does split WINDOW vertically.
3864
3865In order to not split WINDOW vertically, set \(or bind) the
3866variable `split-height-threshold' to nil. Additionally, you can
3867set `split-width-threshold' to zero to make a horizontal split
3868more likely to occur.
3869
3870Have a look at the function `window-splittable-p' if you want to
3871know how `split-window-sensibly' determines whether WINDOW can be
3872split."
3873 (or (and (window-splittable-p window)
3874 ;; Split window vertically.
3875 (with-selected-window window
3876 (split-window-vertically)))
3877 (and (window-splittable-p window t)
3878 ;; Split window horizontally.
3879 (with-selected-window window
3880 (split-window-horizontally)))
3881 (and (eq window (frame-root-window (window-frame window)))
3882 (not (window-minibuffer-p window))
3883 ;; If WINDOW is the only window on its frame and is not the
3884 ;; minibuffer window, try to split it vertically disregarding
3885 ;; the value of `split-height-threshold'.
3886 (let ((split-height-threshold 0))
3887 (when (window-splittable-p window)
3888 (with-selected-window window
3889 (split-window-vertically)))))))
3890
3c448ab6 3891(defun window--try-to-split-window (window)
8b10a2d1
MR
3892 "Try to split WINDOW.
3893Return value returned by `split-window-preferred-function' if it
3894represents a live window, nil otherwise."
3895 (and (window-live-p window)
3896 (not (frame-parameter (window-frame window) 'unsplittable))
3897 (let ((new-window
3898 ;; Since `split-window-preferred-function' might
3899 ;; throw an error use `condition-case'.
3900 (condition-case nil
3901 (funcall split-window-preferred-function window)
3902 (error nil))))
3903 (and (window-live-p new-window) new-window))))
3c448ab6
MR
3904
3905(defun window--frame-usable-p (frame)
3906 "Return FRAME if it can be used to display a buffer."
3907 (when (frame-live-p frame)
3908 (let ((window (frame-root-window frame)))
3909 ;; `frame-root-window' may be an internal window which is considered
3910 ;; "dead" by `window-live-p'. Hence if `window' is not live we
3911 ;; implicitly know that `frame' has a visible window we can use.
4afba819
SM
3912 (unless (and (window-live-p window)
3913 (or (window-minibuffer-p window)
3914 ;; If the window is soft-dedicated, the frame is usable.
064e57de
SM
3915 ;; Actually, even if the window is really dedicated,
3916 ;; the frame is still usable by splitting it.
3917 ;; At least Emacs-22 allowed it, and it is desirable
3918 ;; when displaying same-frame windows.
3919 nil ; (eq t (window-dedicated-p window))
3920 ))
3c448ab6
MR
3921 frame))))
3922
3923(defcustom even-window-heights t
3924 "If non-nil `display-buffer' will try to even window heights.
3925Otherwise `display-buffer' will leave the window configuration
3926alone. Heights are evened only when `display-buffer' chooses a
3927window that appears above or below the selected window."
3928 :type 'boolean
3929 :group 'windows)
3930
3931(defun window--even-window-heights (window)
3932 "Even heights of WINDOW and selected window.
3933Do this only if these windows are vertically adjacent to each
3934other, `even-window-heights' is non-nil, and the selected window
3935is higher than WINDOW."
3936 (when (and even-window-heights
3937 (not (eq window (selected-window)))
3938 ;; Don't resize minibuffer windows.
3939 (not (window-minibuffer-p (selected-window)))
d1f18ec0 3940 (> (window-height (selected-window)) (window-height window))
3c448ab6
MR
3941 (eq (window-frame window) (window-frame (selected-window)))
3942 (let ((sel-edges (window-edges (selected-window)))
3943 (win-edges (window-edges window)))
3944 (and (= (nth 0 sel-edges) (nth 0 win-edges))
3945 (= (nth 2 sel-edges) (nth 2 win-edges))
3946 (or (= (nth 1 sel-edges) (nth 3 win-edges))
3947 (= (nth 3 sel-edges) (nth 1 win-edges))))))
3948 (let ((window-min-height 1))
3949 ;; Don't throw an error if we can't even window heights for
3950 ;; whatever reason.
3951 (condition-case nil
3952 (enlarge-window (/ (- (window-height window) (window-height)) 2))
3953 (error nil)))))
3954
3955(defun window--display-buffer-1 (window)
3956 "Raise the frame containing WINDOW.
3957Do not raise the selected frame. Return WINDOW."
3958 (let* ((frame (window-frame window))
3959 (visible (frame-visible-p frame)))
3960 (unless (or (not visible)
3961 ;; Assume the selected frame is already visible enough.
3962 (eq frame (selected-frame))
3963 ;; Assume the frame from which we invoked the minibuffer
3964 ;; is visible.
3965 (and (minibuffer-window-active-p (selected-window))
3966 (eq frame (window-frame (minibuffer-selected-window)))))
3967 (raise-frame frame))
3968 window))
3969
04ae543a 3970(defun window--display-buffer-2 (buffer window &optional dedicated)
3c448ab6 3971 "Display BUFFER in WINDOW and make its frame visible.
04ae543a 3972Set `window-dedicated-p' to DEDICATED if non-nil.
3c448ab6
MR
3973Return WINDOW."
3974 (when (and (buffer-live-p buffer) (window-live-p window))
3975 (set-window-buffer window buffer)
04ae543a 3976 (when dedicated
782d6e30 3977 (set-window-dedicated-p window dedicated))
3c448ab6
MR
3978 (window--display-buffer-1 window)))
3979
d2c9fc42
SM
3980(defvar display-buffer-mark-dedicated nil
3981 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
3982The actual non-nil value of this variable will be copied to the
3983`window-dedicated-p' flag.")
3984
3c448ab6
MR
3985(defun display-buffer (buffer-or-name &optional not-this-window frame)
3986 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
3987BUFFER-OR-NAME must be a buffer or the name of an existing
3988buffer. Return the window chosen to display BUFFER-OR-NAME or
3989nil if no such window is found.
3990
3991Optional argument NOT-THIS-WINDOW non-nil means display the
3992buffer in a window other than the selected one, even if it is
3993already displayed in the selected window.
3994
3995Optional argument FRAME specifies which frames to investigate
3996when the specified buffer is already displayed. If the buffer is
3997already displayed in some window on one of these frames simply
3998return that window. Possible values of FRAME are:
3999
aa248733
MS
4000`visible' - consider windows on all visible frames on the current
4001terminal.
3c448ab6 4002
aa248733
MS
40030 - consider windows on all visible or iconified frames on the
4004current terminal.
3c448ab6
MR
4005
4006t - consider windows on all frames.
4007
4008A specific frame - consider windows on that frame only.
4009
4010nil - consider windows on the selected frame \(actually the
4011last non-minibuffer frame\) only. If, however, either
4012`display-buffer-reuse-frames' or `pop-up-frames' is non-nil
4013\(non-nil and not graphic-only on a text-only terminal),
aa248733 4014consider all visible or iconified frames on the current terminal."
3c448ab6
MR
4015 (interactive "BDisplay buffer:\nP")
4016 (let* ((can-use-selected-window
4017 ;; The selected window is usable unless either NOT-THIS-WINDOW
4018 ;; is non-nil, it is dedicated to its buffer, or it is the
4019 ;; `minibuffer-window'.
4020 (not (or not-this-window
4021 (window-dedicated-p (selected-window))
4022 (window-minibuffer-p))))
4023 (buffer (if (bufferp buffer-or-name)
4024 buffer-or-name
4025 (get-buffer buffer-or-name)))
4026 (name-of-buffer (buffer-name buffer))
4027 ;; On text-only terminals do not pop up a new frame when
4028 ;; `pop-up-frames' equals graphic-only.
4029 (use-pop-up-frames (if (eq pop-up-frames 'graphic-only)
4030 (display-graphic-p)
4031 pop-up-frames))
4032 ;; `frame-to-use' is the frame where to show `buffer' - either
4033 ;; the selected frame or the last nonminibuffer frame.
4034 (frame-to-use
4035 (or (window--frame-usable-p (selected-frame))
4036 (window--frame-usable-p (last-nonminibuffer-frame))))
4037 ;; `window-to-use' is the window we use for showing `buffer'.
4038 window-to-use)
4039 (cond
4040 ((not (buffer-live-p buffer))
4041 (error "No such buffer %s" buffer))
4042 (display-buffer-function
4043 ;; Let `display-buffer-function' do the job.
4044 (funcall display-buffer-function buffer not-this-window))
4045 ((and (not not-this-window)
4046 (eq (window-buffer (selected-window)) buffer))
4047 ;; The selected window already displays BUFFER and
4048 ;; `not-this-window' is nil, so use it.
4049 (window--display-buffer-1 (selected-window)))
4050 ((and can-use-selected-window (same-window-p name-of-buffer))
4051 ;; If the buffer's name tells us to use the selected window do so.
4052 (window--display-buffer-2 buffer (selected-window)))
4053 ((let ((frames (or frame
4054 (and (or use-pop-up-frames
4055 display-buffer-reuse-frames
4056 (not (last-nonminibuffer-frame)))
4057 0)
4058 (last-nonminibuffer-frame))))
e331bbf3
MR
4059 (setq window-to-use
4060 (catch 'found
29c45500
MR
4061 ;; Search frames for a window displaying BUFFER. Return
4062 ;; the selected window only if we are allowed to do so.
4063 (dolist (window (get-buffer-window-list buffer 'nomini frames))
e331bbf3
MR
4064 (when (or can-use-selected-window
4065 (not (eq (selected-window) window)))
4066 (throw 'found window))))))
4067 ;; The buffer is already displayed in some window; use that.
3c448ab6
MR
4068 (window--display-buffer-1 window-to-use))
4069 ((and special-display-function
4070 ;; `special-display-p' returns either t or a list of frame
4071 ;; parameters to pass to `special-display-function'.
4072 (let ((pars (special-display-p name-of-buffer)))
4073 (when pars
4074 (funcall special-display-function
4075 buffer (if (listp pars) pars))))))
4076 ((or use-pop-up-frames (not frame-to-use))
4077 ;; We want or need a new frame.
d2c9fc42 4078 (let ((win (frame-selected-window (funcall pop-up-frame-function))))
04ae543a 4079 (window--display-buffer-2 buffer win display-buffer-mark-dedicated)))
3c448ab6
MR
4080 ((and pop-up-windows
4081 ;; Make a new window.
4082 (or (not (frame-parameter frame-to-use 'unsplittable))
4083 ;; If the selected frame cannot be split look at
4084 ;; `last-nonminibuffer-frame'.
4085 (and (eq frame-to-use (selected-frame))
4086 (setq frame-to-use (last-nonminibuffer-frame))
4087 (window--frame-usable-p frame-to-use)
4088 (not (frame-parameter frame-to-use 'unsplittable))))
4089 ;; Attempt to split largest or least recently used window.
4090 (setq window-to-use
4091 (or (window--try-to-split-window
4092 (get-largest-window frame-to-use t))
4093 (window--try-to-split-window
d2c9fc42 4094 (get-lru-window frame-to-use t)))))
04ae543a
SM
4095 (window--display-buffer-2 buffer window-to-use
4096 display-buffer-mark-dedicated))
a9d451f0
MR
4097 ((let ((window-to-undedicate
4098 ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate
4099 ;; the selected window to its buffer, to avoid that some of
4100 ;; the `get-' routines below choose it. (Bug#1415)
4101 (and not-this-window (not (window-dedicated-p))
4102 (set-window-dedicated-p (selected-window) t)
4103 (selected-window))))
4104 (unwind-protect
4105 (setq window-to-use
4106 ;; Reuse an existing window.
4107 (or (get-lru-window frame-to-use)
4108 (let ((window (get-buffer-window buffer 'visible)))
4109 (unless (and not-this-window
4110 (eq window (selected-window)))
4111 window))
4112 (get-largest-window 'visible)
4113 (let ((window (get-buffer-window buffer 0)))
4114 (unless (and not-this-window
4115 (eq window (selected-window)))
4116 window))
4117 (get-largest-window 0)
4118 (frame-selected-window (funcall pop-up-frame-function))))
4119 (when (window-live-p window-to-undedicate)
4120 ;; Restore dedicated status of selected window.
4121 (set-window-dedicated-p window-to-undedicate nil))))
3c448ab6
MR
4122 (window--even-window-heights window-to-use)
4123 (window--display-buffer-2 buffer window-to-use)))))
4124
9397e56f
MR
4125(defun display-buffer-other-frame (buffer)
4126 "Display buffer BUFFER in another frame.
4127This uses the function `display-buffer' as a subroutine; see
4128its documentation for additional customization information."
4129 (interactive "BDisplay buffer in other frame: ")
4130 (let ((pop-up-frames t)
4131 same-window-buffer-names same-window-regexps
4132 ;;(old-window (selected-window))
4133 new-window)
4134 (setq new-window (display-buffer buffer t))
4135 ;; This may have been here in order to prevent the new frame from hiding
4136 ;; the old frame. But it does more harm than good.
4137 ;; Maybe we should call `raise-window' on the old-frame instead? --Stef
4138 ;;(lower-frame (window-frame new-window))
4139
4140 ;; This may have been here in order to make sure the old-frame gets the
4141 ;; focus. But not only can it cause an annoying flicker, with some
4142 ;; window-managers it just makes the window invisible, with no easy
4143 ;; way to recover it. --Stef
4144 ;;(make-frame-invisible (window-frame old-window))
4145 ;;(make-frame-visible (window-frame old-window))
4146 ))
4147
3c448ab6
MR
4148(defun pop-to-buffer (buffer-or-name &optional other-window norecord)
4149 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
4150BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4151nil. If BUFFER-OR-NAME is a string not naming an existent
4152buffer, create a buffer with that name. If BUFFER-OR-NAME is
4153nil, choose some other buffer.
4154
4155If `pop-up-windows' is non-nil, windows can be split to display
4156the buffer. If optional second arg OTHER-WINDOW is non-nil,
4157insist on finding another window even if the specified buffer is
4158already visible in the selected window, and ignore
4159`same-window-regexps' and `same-window-buffer-names'.
4160
4161If the window to show BUFFER-OR-NAME is not on the selected
4162frame, raise that window's frame and give it input focus.
4163
4164This function returns the buffer it switched to. This uses the
4165function `display-buffer' as a subroutine; see the documentation
4166of `display-buffer' for additional customization information.
4167
4168Optional third arg NORECORD non-nil means do not put this buffer
4169at the front of the list of recently selected ones."
4170 (let ((buffer
4171 ;; FIXME: This behavior is carried over from the previous C version
4172 ;; of pop-to-buffer, but really we should use just
4173 ;; `get-buffer' here.
4174 (if (null buffer-or-name) (other-buffer (current-buffer))
4175 (or (get-buffer buffer-or-name)
4176 (let ((buf (get-buffer-create buffer-or-name)))
4177 (set-buffer-major-mode buf)
4178 buf))))
3c448ab6
MR
4179 (old-frame (selected-frame))
4180 new-window new-frame)
4181 (set-buffer buffer)
4182 (setq new-window (display-buffer buffer other-window))
13b5221f
MR
4183 (select-window new-window norecord)
4184 (setq new-frame (window-frame new-window))
4185 (unless (eq new-frame old-frame)
4186 ;; `display-buffer' has chosen another frame, make sure it gets
4187 ;; input focus and is risen.
4188 (select-frame-set-input-focus new-frame))
3c448ab6 4189 buffer))
9397e56f
MR
4190
4191(defun read-buffer-to-switch (prompt)
4192 "Read the name of a buffer to switch to, prompting with PROMPT.
4193Return the neame of the buffer as a string.
4194
4195This function is intended for the `switch-to-buffer' family of
4196commands since these need to omit the name of the current buffer
4197from the list of completions and default values."
4198 (let ((rbts-completion-table (internal-complete-buffer-except)))
4199 (minibuffer-with-setup-hook
4200 (lambda ()
4201 (setq minibuffer-completion-table rbts-completion-table)
4202 ;; Since rbts-completion-table is built dynamically, we
4203 ;; can't just add it to the default value of
4204 ;; icomplete-with-completion-tables, so we add it
4205 ;; here manually.
4206 (if (and (boundp 'icomplete-with-completion-tables)
4207 (listp icomplete-with-completion-tables))
4208 (set (make-local-variable 'icomplete-with-completion-tables)
4209 (cons rbts-completion-table
4210 icomplete-with-completion-tables))))
4211 (read-buffer prompt (other-buffer (current-buffer))
4212 (confirm-nonexistent-file-or-buffer)))))
4213
4214(defun normalize-buffer-to-switch-to (buffer-or-name)
4215 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
4216If BUFFER-OR-NAME is nil, return the buffer returned by
4217`other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
4218exists, return that buffer. If no such buffer exists, create a
4219buffer with the name BUFFER-OR-NAME and return that buffer."
4220 (if buffer-or-name
4221 (or (get-buffer buffer-or-name)
4222 (let ((buffer (get-buffer-create buffer-or-name)))
4223 (set-buffer-major-mode buffer)
4224 buffer))
4225 (other-buffer)))
4226
4227(defun switch-to-buffer (buffer-or-name &optional norecord)
4228 "Switch to buffer BUFFER-OR-NAME in the selected window.
4229If called interactively, prompt for the buffer name using the
4230minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4231determines whether to request confirmation before creating a new
4232buffer.
4233
4234BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4235nil. If BUFFER-OR-NAME is a string that does not identify an
4236existing buffer, create a buffer with that name. If
4237BUFFER-OR-NAME is nil, switch to the buffer returned by
4238`other-buffer'.
4239
4240Optional argument NORECORD non-nil means do not put the buffer
4241specified by BUFFER-OR-NAME at the front of the buffer list and
4242do not make the window displaying it the most recently selected
4243one. Return the buffer switched to.
4244
4245This function is intended for interactive use only. Lisp
4246functions should call `pop-to-buffer-same-window' instead."
4247 (interactive
4248 (list (read-buffer-to-switch "Switch to buffer: ")))
4249 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
4250 (if (and (or (window-minibuffer-p) (eq (window-dedicated-p) t))
4251 (not (eq buffer (window-buffer))))
4252 ;; Cannot switch to another buffer in a minibuffer or strongly
4253 ;; dedicated window that does not show the buffer already. Call
4254 ;; `pop-to-buffer' instead.
4255 (pop-to-buffer buffer nil norecord)
4256 (unless (eq buffer (window-buffer))
4257 ;; I'm not sure why we should NOT call `set-window-buffer' here,
4258 ;; but let's keep things as they are (otherwise we could always
4259 ;; call `pop-to-buffer-same-window' here).
4260 (set-window-buffer nil buffer))
4261 (unless norecord
4262 (select-window (selected-window)))
4263 (set-buffer buffer))))
4264
4265(defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
4266 "Select the buffer specified by BUFFER-OR-NAME in another window.
4267BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4268nil. Return the buffer switched to.
4269
4270If called interactively, prompt for the buffer name using the
4271minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4272determines whether to request confirmation before creating a new
4273buffer.
4274
4275If BUFFER-OR-NAME is a string and does not identify an existing
4276buffer, create a new buffer with that name. If BUFFER-OR-NAME is
4277nil, switch to the buffer returned by `other-buffer'.
4278
4279Optional second argument NORECORD non-nil means do not put this
4280buffer at the front of the list of recently selected ones.
4281
4282This uses the function `display-buffer' as a subroutine; see its
4283documentation for additional customization information."
4284 (interactive
4285 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
4286 (let ((pop-up-windows t)
4287 same-window-buffer-names same-window-regexps)
4288 (pop-to-buffer buffer-or-name t norecord)))
4289
4290(defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
4291 "Switch to buffer BUFFER-OR-NAME in another frame.
4292BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4293nil. Return the buffer switched to.
4294
4295If called interactively, prompt for the buffer name using the
4296minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4297determines whether to request confirmation before creating a new
4298buffer.
4299
4300If BUFFER-OR-NAME is a string and does not identify an existing
4301buffer, create a new buffer with that name. If BUFFER-OR-NAME is
4302nil, switch to the buffer returned by `other-buffer'.
4303
4304Optional second arg NORECORD non-nil means do not put this
4305buffer at the front of the list of recently selected ones.
4306
4307This uses the function `display-buffer' as a subroutine; see its
4308documentation for additional customization information."
4309 (interactive
4310 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
4311 (let ((pop-up-frames t)
4312 same-window-buffer-names same-window-regexps)
4313 (pop-to-buffer buffer-or-name t norecord)))
3c448ab6
MR
4314\f
4315(defun set-window-text-height (window height)
9992ea0c 4316 "Set the height in lines of the text display area of WINDOW to HEIGHT.
3c448ab6
MR
4317HEIGHT doesn't include the mode line or header line, if any, or
4318any partial-height lines in the text display area.
4319
4320Note that the current implementation of this function cannot
4321always set the height exactly, but attempts to be conservative,
4322by allocating more lines than are actually needed in the case
4323where some error may be present."
4324 (let ((delta (- height (window-text-height window))))
4325 (unless (zerop delta)
4326 ;; Setting window-min-height to a value like 1 can lead to very
4327 ;; bizarre displays because it also allows Emacs to make *other*
4328 ;; windows 1-line tall, which means that there's no more space for
4329 ;; the modeline.
4330 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
4331 (if (and window (not (eq window (selected-window))))
4332 (save-selected-window
4333 (select-window window 'norecord)
4334 (enlarge-window delta))
4335 (enlarge-window delta))))))
4336
4337\f
4338(defun enlarge-window-horizontally (columns)
4339 "Make selected window COLUMNS wider.
4340Interactively, if no argument is given, make selected window one
4341column wider."
4342 (interactive "p")
4343 (enlarge-window columns t))
4344
4345(defun shrink-window-horizontally (columns)
4346 "Make selected window COLUMNS narrower.
4347Interactively, if no argument is given, make selected window one
4348column narrower."
4349 (interactive "p")
4350 (shrink-window columns t))
4351
4352(defun window-buffer-height (window)
4353 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
4354 (with-current-buffer (window-buffer window)
4355 (max 1
4356 (count-screen-lines (point-min) (point-max)
4357 ;; If buffer ends with a newline, ignore it when
4358 ;; counting height unless point is after it.
4359 (eobp)
4360 window))))
4361
4362(defun count-screen-lines (&optional beg end count-final-newline window)
4363 "Return the number of screen lines in the region.
4364The number of screen lines may be different from the number of actual lines,
4365due to line breaking, display table, etc.
4366
4367Optional arguments BEG and END default to `point-min' and `point-max'
4368respectively.
4369
4370If region ends with a newline, ignore it unless optional third argument
4371COUNT-FINAL-NEWLINE is non-nil.
4372
4373The optional fourth argument WINDOW specifies the window used for obtaining
4374parameters such as width, horizontal scrolling, and so on. The default is
4375to use the selected window's parameters.
4376
4377Like `vertical-motion', `count-screen-lines' always uses the current buffer,
4378regardless of which buffer is displayed in WINDOW. This makes possible to use
4379`count-screen-lines' in any buffer, whether or not it is currently displayed
4380in some window."
4381 (unless beg
4382 (setq beg (point-min)))
4383 (unless end
4384 (setq end (point-max)))
4385 (if (= beg end)
4386 0
4387 (save-excursion
4388 (save-restriction
4389 (widen)
4390 (narrow-to-region (min beg end)
4391 (if (and (not count-final-newline)
4392 (= ?\n (char-before (max beg end))))
4393 (1- (max beg end))
4394 (max beg end)))
4395 (goto-char (point-min))
4396 (1+ (vertical-motion (buffer-size) window))))))
4397
4398(defun fit-window-to-buffer (&optional window max-height min-height)
4399 "Adjust height of WINDOW to display its buffer's contents exactly.
9adf1f06 4400WINDOW defaults to the selected window.
3c448ab6 4401Optional argument MAX-HEIGHT specifies the maximum height of the
f7baca20
MR
4402window and defaults to the maximum permissible height of a window
4403on WINDOW's frame.
3c448ab6
MR
4404Optional argument MIN-HEIGHT specifies the minimum height of the
4405window and defaults to `window-min-height'.
4406Both, MAX-HEIGHT and MIN-HEIGHT are specified in lines and
4407include the mode line and header line, if any.
3c448ab6 4408
9adf1f06
MR
4409Return non-nil if height was orderly adjusted, nil otherwise.
4410
f7baca20
MR
4411Caution: This function can delete WINDOW and/or other windows
4412when their height shrinks to less than MIN-HEIGHT."
4413 (interactive)
4414 ;; Do all the work in WINDOW and its buffer and restore the selected
4415 ;; window and the current buffer when we're done.
9adf1f06
MR
4416 (let ((old-buffer (current-buffer))
4417 value)
f7baca20
MR
4418 (with-selected-window (or window (setq window (selected-window)))
4419 (set-buffer (window-buffer))
4420 ;; Use `condition-case' to handle any fixed-size windows and other
4421 ;; pitfalls nearby.
4422 (condition-case nil
4423 (let* (;; MIN-HEIGHT must not be less than 1 and defaults to
4424 ;; `window-min-height'.
4425 (min-height (max (or min-height window-min-height) 1))
4426 (max-window-height
4427 ;; Maximum height of any window on this frame.
4428 (min (window-height (frame-root-window)) (frame-height)))
4429 ;; MAX-HEIGHT must not be larger than max-window-height and
4430 ;; defaults to max-window-height.
4431 (max-height
4432 (min (or max-height max-window-height) max-window-height))
4433 (desired-height
4434 ;; The height necessary to show all of WINDOW's buffer,
4435 ;; constrained by MIN-HEIGHT and MAX-HEIGHT.
4436 (max
4437 (min
4438 ;; For an empty buffer `count-screen-lines' returns zero.
4439 ;; Even in that case we need one line for the cursor.
4440 (+ (max (count-screen-lines) 1)
4441 ;; For non-minibuffers count the mode line, if any.
4442 (if (and (not (window-minibuffer-p)) mode-line-format)
4443 1 0)
4444 ;; Count the header line, if any.
4445 (if header-line-format 1 0))
4446 max-height)
4447 min-height))
4448 (delta
4449 ;; How much the window height has to change.
4450 (if (= (window-height) (window-height (frame-root-window)))
4451 ;; Don't try to resize a full-height window.
4452 0
4453 (- desired-height (window-height))))
4454 ;; Do something reasonable so `enlarge-window' can make
4455 ;; windows as small as MIN-HEIGHT.
4456 (window-min-height (min min-height window-min-height)))
4457 ;; Don't try to redisplay with the cursor at the end on its
4458 ;; own line--that would force a scroll and spoil things.
4459 (when (and (eobp) (bolp) (not (bobp)))
4460 (set-window-point window (1- (window-point))))
4461 ;; Adjust WINDOW's height to the nominally correct one
4462 ;; (which may actually be slightly off because of variable
4463 ;; height text, etc).
4464 (unless (zerop delta)
4465 (enlarge-window delta))
4466 ;; `enlarge-window' might have deleted WINDOW, so make sure
4467 ;; WINDOW's still alive for the remainder of this.
4468 ;; Note: Deleting WINDOW is clearly counter-intuitive in
4469 ;; this context, but we can't do much about it given the
4470 ;; current semantics of `enlarge-window'.
4471 (when (window-live-p window)
4472 ;; Check if the last line is surely fully visible. If
4473 ;; not, enlarge the window.
4474 (let ((end (save-excursion
4475 (goto-char (point-max))
4476 (when (and (bolp) (not (bobp)))
4477 ;; Don't include final newline.
4478 (backward-char 1))
4479 (when truncate-lines
4480 ;; If line-wrapping is turned off, test the
4481 ;; beginning of the last line for
4482 ;; visibility instead of the end, as the
4483 ;; end of the line could be invisible by
4484 ;; virtue of extending past the edge of the
4485 ;; window.
4486 (forward-line 0))
4487 (point))))
4488 (set-window-vscroll window 0)
4489 (while (and (< desired-height max-height)
4490 (= desired-height (window-height))
4491 (not (pos-visible-in-window-p end)))
4492 (enlarge-window 1)
9adf1f06
MR
4493 (setq desired-height (1+ desired-height))))
4494 ;; Return non-nil only if nothing "bad" happened.
4495 (setq value t)))
f7baca20
MR
4496 (error nil)))
4497 (when (buffer-live-p old-buffer)
9adf1f06
MR
4498 (set-buffer old-buffer))
4499 value))
3c448ab6
MR
4500
4501(defun window-safely-shrinkable-p (&optional window)
4502 "Return t if WINDOW can be shrunk without shrinking other windows.
4503WINDOW defaults to the selected window."
4504 (with-selected-window (or window (selected-window))
4505 (let ((edges (window-edges)))
4506 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
4507 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
4508
4509(defun shrink-window-if-larger-than-buffer (&optional window)
4510 "Shrink height of WINDOW if its buffer doesn't need so many lines.
4511More precisely, shrink WINDOW vertically to be as small as
4512possible, while still showing the full contents of its buffer.
4513WINDOW defaults to the selected window.
4514
4515Do not shrink to less than `window-min-height' lines. Do nothing
4516if the buffer contains more lines than the present window height,
4517or if some of the window's contents are scrolled out of view, or
4518if shrinking this window would also shrink another window, or if
4519the window is the only window of its frame.
4520
4521Return non-nil if the window was shrunk, nil otherwise."
4522 (interactive)
4523 (when (null window)
4524 (setq window (selected-window)))
4525 (let* ((frame (window-frame window))
4526 (mini (frame-parameter frame 'minibuffer))
4527 (edges (window-edges window)))
4528 (if (and (not (eq window (frame-root-window frame)))
4529 (window-safely-shrinkable-p window)
4530 (pos-visible-in-window-p (point-min) window)
4531 (not (eq mini 'only))
4532 (or (not mini)
4533 (let ((mini-window (minibuffer-window frame)))
4534 (or (null mini-window)
4535 (not (eq frame (window-frame mini-window)))
4536 (< (nth 3 edges)
4537 (nth 1 (window-edges mini-window)))
4538 (> (nth 1 edges)
4539 (frame-parameter frame 'menu-bar-lines))))))
4540 (fit-window-to-buffer window (window-height window)))))
4541
4542(defun kill-buffer-and-window ()
4543 "Kill the current buffer and delete the selected window."
4544 (interactive)
4545 (let ((window-to-delete (selected-window))
4546 (buffer-to-kill (current-buffer))
4547 (delete-window-hook (lambda ()
4548 (condition-case nil
4549 (delete-window)
4550 (error nil)))))
4551 (unwind-protect
4552 (progn
4553 (add-hook 'kill-buffer-hook delete-window-hook t t)
4554 (if (kill-buffer (current-buffer))
4555 ;; If `delete-window' failed before, we rerun it to regenerate
4556 ;; the error so it can be seen in the echo area.
4557 (when (eq (selected-window) window-to-delete)
4558 (delete-window))))
4559 ;; If the buffer is not dead for some reason (probably because
4560 ;; of a `quit' signal), remove the hook again.
4561 (condition-case nil
4562 (with-current-buffer buffer-to-kill
4563 (remove-hook 'kill-buffer-hook delete-window-hook t))
4564 (error nil)))))
4565
4566(defun quit-window (&optional kill window)
4567 "Quit WINDOW and bury its buffer.
4568With a prefix argument, kill the buffer instead. WINDOW defaults
4569to the selected window.
4570
4571If WINDOW is non-nil, dedicated, or a minibuffer window, delete
4572it and, if it's alone on its frame, its frame too. Otherwise, or
4573if deleting WINDOW fails in any of the preceding cases, display
4574another buffer in WINDOW using `switch-to-buffer'.
4575
4576Optional argument KILL non-nil means kill WINDOW's buffer.
4577Otherwise, bury WINDOW's buffer, see `bury-buffer'."
4578 (interactive "P")
4579 (let ((buffer (window-buffer window)))
4580 (if (or window
4581 (window-minibuffer-p window)
4582 (window-dedicated-p window))
4583 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
4584 ;; try to delete it.
a0c859f0
MR
4585 (let* ((window (or window (selected-window)))
4586 (frame (window-frame window)))
3c448ab6
MR
4587 (if (eq window (frame-root-window frame))
4588 ;; WINDOW is alone on its frame. `delete-windows-on'
4589 ;; knows how to handle that case.
4590 (delete-windows-on buffer frame)
4591 ;; There are other windows on its frame, delete WINDOW.
4592 (delete-window window)))
4593 ;; Otherwise, switch to another buffer in the selected window.
4594 (switch-to-buffer nil))
4595
4596 ;; Deal with the buffer.
4597 (if kill
4598 (kill-buffer buffer)
4599 (bury-buffer buffer))))
4600
74f806a1 4601\f
3c448ab6
MR
4602(defvar recenter-last-op nil
4603 "Indicates the last recenter operation performed.
0116abbd
JL
4604Possible values: `top', `middle', `bottom', integer or float numbers.")
4605
4606(defcustom recenter-positions '(middle top bottom)
4607 "Cycling order for `recenter-top-bottom'.
4608A list of elements with possible values `top', `middle', `bottom',
4609integer or float numbers that define the cycling order for
4610the command `recenter-top-bottom'.
4611
4612Top and bottom destinations are `scroll-margin' lines the from true
4613window top and bottom. Middle redraws the frame and centers point
4614vertically within the window. Integer number moves current line to
4615the specified absolute window-line. Float number between 0.0 and 1.0
4616means the percentage of the screen space from the top. The default
4617cycling order is middle -> top -> bottom."
4618 :type '(repeat (choice
4619 (const :tag "Top" top)
4620 (const :tag "Middle" middle)
4621 (const :tag "Bottom" bottom)
4622 (integer :tag "Line number")
4623 (float :tag "Percentage")))
4624 :version "23.2"
4625 :group 'windows)
3c448ab6
MR
4626
4627(defun recenter-top-bottom (&optional arg)
0116abbd
JL
4628 "Move current buffer line to the specified window line.
4629With no prefix argument, successive calls place point according
4630to the cycling order defined by `recenter-positions'.
3c448ab6
MR
4631
4632A prefix argument is handled like `recenter':
4633 With numeric prefix ARG, move current line to window-line ARG.
0116abbd 4634 With plain `C-u', move current line to window center."
3c448ab6
MR
4635 (interactive "P")
4636 (cond
0116abbd 4637 (arg (recenter arg)) ; Always respect ARG.
3c448ab6 4638 (t
0116abbd
JL
4639 (setq recenter-last-op
4640 (if (eq this-command last-command)
4641 (car (or (cdr (member recenter-last-op recenter-positions))
4642 recenter-positions))
4643 (car recenter-positions)))
3c448ab6
MR
4644 (let ((this-scroll-margin
4645 (min (max 0 scroll-margin)
4646 (truncate (/ (window-body-height) 4.0)))))
4647 (cond ((eq recenter-last-op 'middle)
0116abbd 4648 (recenter))
3c448ab6 4649 ((eq recenter-last-op 'top)
0116abbd
JL
4650 (recenter this-scroll-margin))
4651 ((eq recenter-last-op 'bottom)
4652 (recenter (- -1 this-scroll-margin)))
4653 ((integerp recenter-last-op)
4654 (recenter recenter-last-op))
4655 ((floatp recenter-last-op)
4656 (recenter (round (* recenter-last-op (window-height))))))))))
3c448ab6
MR
4657
4658(define-key global-map [?\C-l] 'recenter-top-bottom)
216349f8 4659
216349f8
SM
4660(defun move-to-window-line-top-bottom (&optional arg)
4661 "Position point relative to window.
4662
0f202d5d 4663With a prefix argument ARG, acts like `move-to-window-line'.
216349f8
SM
4664
4665With no argument, positions point at center of window.
0116abbd
JL
4666Successive calls position point at positions defined
4667by `recenter-positions'."
216349f8
SM
4668 (interactive "P")
4669 (cond
0116abbd 4670 (arg (move-to-window-line arg)) ; Always respect ARG.
216349f8 4671 (t
0116abbd
JL
4672 (setq recenter-last-op
4673 (if (eq this-command last-command)
4674 (car (or (cdr (member recenter-last-op recenter-positions))
4675 recenter-positions))
4676 (car recenter-positions)))
216349f8
SM
4677 (let ((this-scroll-margin
4678 (min (max 0 scroll-margin)
4679 (truncate (/ (window-body-height) 4.0)))))
0f202d5d 4680 (cond ((eq recenter-last-op 'middle)
0116abbd 4681 (call-interactively 'move-to-window-line))
0f202d5d 4682 ((eq recenter-last-op 'top)
0116abbd
JL
4683 (move-to-window-line this-scroll-margin))
4684 ((eq recenter-last-op 'bottom)
4685 (move-to-window-line (- -1 this-scroll-margin)))
4686 ((integerp recenter-last-op)
4687 (move-to-window-line recenter-last-op))
4688 ((floatp recenter-last-op)
4689 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
216349f8
SM
4690
4691(define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
4692
3c448ab6 4693\f
74f806a1
JL
4694;;; Scrolling commands.
4695
4696;;; Scrolling commands which does not signal errors at top/bottom
4697;;; of buffer at first key-press (instead moves to top/bottom
4698;;; of buffer).
4699
4700(defcustom scroll-error-top-bottom nil
4701 "Move point to top/bottom of buffer before signalling a scrolling error.
4702A value of nil means just signal an error if no more scrolling possible.
4703A value of t means point moves to the beginning or the end of the buffer
4704\(depending on scrolling direction) when no more scrolling possible.
4705When point is already on that position, then signal an error."
4706 :type 'boolean
4707 :group 'scrolling
4708 :version "24.1")
4709
4710(defun scroll-up-command (&optional arg)
4711 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
4712If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
4713scroll window further, move cursor to the bottom line.
4714When point is already on that position, then signal an error.
4715A near full screen is `next-screen-context-lines' less than a full screen.
4716Negative ARG means scroll downward.
4717If ARG is the atom `-', scroll downward by nearly full screen."
4718 (interactive "^P")
4719 (cond
4720 ((null scroll-error-top-bottom)
4721 (scroll-up arg))
4722 ((eq arg '-)
4723 (scroll-down-command nil))
4724 ((< (prefix-numeric-value arg) 0)
4725 (scroll-down-command (- (prefix-numeric-value arg))))
4726 ((eobp)
4727 (scroll-up arg)) ; signal error
4728 (t
4729 (condition-case nil
4730 (scroll-up arg)
4731 (end-of-buffer
4732 (if arg
4733 ;; When scrolling by ARG lines can't be done,
4734 ;; move by ARG lines instead.
4735 (forward-line arg)
4736 ;; When ARG is nil for full-screen scrolling,
4737 ;; move to the bottom of the buffer.
4738 (goto-char (point-max))))))))
4739
4740(put 'scroll-up-command 'scroll-command t)
4741
4742(defun scroll-down-command (&optional arg)
4743 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
4744If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
4745scroll window further, move cursor to the top line.
4746When point is already on that position, then signal an error.
4747A near full screen is `next-screen-context-lines' less than a full screen.
4748Negative ARG means scroll upward.
4749If ARG is the atom `-', scroll upward by nearly full screen."
4750 (interactive "^P")
4751 (cond
4752 ((null scroll-error-top-bottom)
4753 (scroll-down arg))
4754 ((eq arg '-)
4755 (scroll-up-command nil))
4756 ((< (prefix-numeric-value arg) 0)
4757 (scroll-up-command (- (prefix-numeric-value arg))))
4758 ((bobp)
4759 (scroll-down arg)) ; signal error
4760 (t
4761 (condition-case nil
4762 (scroll-down arg)
4763 (beginning-of-buffer
4764 (if arg
4765 ;; When scrolling by ARG lines can't be done,
4766 ;; move by ARG lines instead.
4767 (forward-line (- arg))
4768 ;; When ARG is nil for full-screen scrolling,
4769 ;; move to the top of the buffer.
4770 (goto-char (point-min))))))))
4771
4772(put 'scroll-down-command 'scroll-command t)
4773
4774;;; Scrolling commands which scroll a line instead of full screen.
4775
4776(defun scroll-up-line (&optional arg)
4777 "Scroll text of selected window upward ARG lines; or one line if no ARG.
4778If ARG is omitted or nil, scroll upward by one line.
4779This is different from `scroll-up-command' that scrolls a full screen."
4780 (interactive "p")
4781 (scroll-up (or arg 1)))
4782
4783(put 'scroll-up-line 'scroll-command t)
4784
4785(defun scroll-down-line (&optional arg)
4786 "Scroll text of selected window down ARG lines; or one line if no ARG.
4787If ARG is omitted or nil, scroll down by one line.
4788This is different from `scroll-down-command' that scrolls a full screen."
4789 (interactive "p")
4790 (scroll-down (or arg 1)))
4791
4792(put 'scroll-down-line 'scroll-command t)
4793
4794\f
4795(defun scroll-other-window-down (lines)
4796 "Scroll the \"other window\" down.
4797For more details, see the documentation for `scroll-other-window'."
4798 (interactive "P")
4799 (scroll-other-window
4800 ;; Just invert the argument's meaning.
4801 ;; We can do that without knowing which window it will be.
4802 (if (eq lines '-) nil
4803 (if (null lines) '-
4804 (- (prefix-numeric-value lines))))))
4805
4806(defun beginning-of-buffer-other-window (arg)
4807 "Move point to the beginning of the buffer in the other window.
4808Leave mark at previous position.
4809With arg N, put point N/10 of the way from the true beginning."
4810 (interactive "P")
4811 (let ((orig-window (selected-window))
4812 (window (other-window-for-scrolling)))
4813 ;; We use unwind-protect rather than save-window-excursion
4814 ;; because the latter would preserve the things we want to change.
4815 (unwind-protect
4816 (progn
4817 (select-window window)
4818 ;; Set point and mark in that window's buffer.
4819 (with-no-warnings
4820 (beginning-of-buffer arg))
4821 ;; Set point accordingly.
4822 (recenter '(t)))
4823 (select-window orig-window))))
4824
4825(defun end-of-buffer-other-window (arg)
4826 "Move point to the end of the buffer in the other window.
4827Leave mark at previous position.
4828With arg N, put point N/10 of the way from the true end."
4829 (interactive "P")
4830 ;; See beginning-of-buffer-other-window for comments.
4831 (let ((orig-window (selected-window))
4832 (window (other-window-for-scrolling)))
4833 (unwind-protect
4834 (progn
4835 (select-window window)
4836 (with-no-warnings
4837 (end-of-buffer arg))
4838 (recenter '(t)))
4839 (select-window orig-window))))
4840
4841\f
3c448ab6
MR
4842(defvar mouse-autoselect-window-timer nil
4843 "Timer used by delayed window autoselection.")
4844
4845(defvar mouse-autoselect-window-position nil
4846 "Last mouse position recorded by delayed window autoselection.")
4847
4848(defvar mouse-autoselect-window-window nil
4849 "Last window recorded by delayed window autoselection.")
4850
4851(defvar mouse-autoselect-window-state nil
4852 "When non-nil, special state of delayed window autoselection.
4853Possible values are `suspend' \(suspend autoselection after a menu or
4854scrollbar interaction\) and `select' \(the next invocation of
4855'handle-select-window' shall select the window immediately\).")
4856
4857(defun mouse-autoselect-window-cancel (&optional force)
4858 "Cancel delayed window autoselection.
4859Optional argument FORCE means cancel unconditionally."
4860 (unless (and (not force)
4861 ;; Don't cancel for select-window or select-frame events
4862 ;; or when the user drags a scroll bar.
4863 (or (memq this-command
4864 '(handle-select-window handle-switch-frame))
4865 (and (eq this-command 'scroll-bar-toolkit-scroll)
4866 (memq (nth 4 (event-end last-input-event))
4867 '(handle end-scroll)))))
4868 (setq mouse-autoselect-window-state nil)
4869 (when (timerp mouse-autoselect-window-timer)
4870 (cancel-timer mouse-autoselect-window-timer))
4871 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
4872
4873(defun mouse-autoselect-window-start (mouse-position &optional window suspend)
4874 "Start delayed window autoselection.
4875MOUSE-POSITION is the last position where the mouse was seen as returned
4876by `mouse-position'. Optional argument WINDOW non-nil denotes the
4877window where the mouse was seen. Optional argument SUSPEND non-nil
4878means suspend autoselection."
4879 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
4880 (setq mouse-autoselect-window-position mouse-position)
4881 (when window (setq mouse-autoselect-window-window window))
4882 (setq mouse-autoselect-window-state (when suspend 'suspend))
4883 ;; Install timer which runs `mouse-autoselect-window-select' after
4884 ;; `mouse-autoselect-window' seconds.
4885 (setq mouse-autoselect-window-timer
4886 (run-at-time
4887 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
4888
4889(defun mouse-autoselect-window-select ()
4890 "Select window with delayed window autoselection.
4891If the mouse position has stabilized in a non-selected window, select
4892that window. The minibuffer window is selected only if the minibuffer is
4893active. This function is run by `mouse-autoselect-window-timer'."
4894 (condition-case nil
4895 (let* ((mouse-position (mouse-position))
4896 (window
4897 (condition-case nil
4898 (window-at (cadr mouse-position) (cddr mouse-position)
4899 (car mouse-position))
4900 (error nil))))
4901 (cond
4902 ((or (menu-or-popup-active-p)
4903 (and window
4904 (not (coordinates-in-window-p (cdr mouse-position) window))))
4905 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
4906 ;; of WINDOW, temporarily suspend delayed autoselection.
4907 (mouse-autoselect-window-start mouse-position nil t))
4908 ((eq mouse-autoselect-window-state 'suspend)
4909 ;; Delayed autoselection was temporarily suspended, reenable it.
4910 (mouse-autoselect-window-start mouse-position))
4911 ((and window (not (eq window (selected-window)))
4912 (or (not (numberp mouse-autoselect-window))
4913 (and (> mouse-autoselect-window 0)
4914 ;; If `mouse-autoselect-window' is positive, select
4915 ;; window if the window is the same as before.
4916 (eq window mouse-autoselect-window-window))
4917 ;; Otherwise select window if the mouse is at the same
4918 ;; position as before. Observe that the first test after
4919 ;; starting autoselection usually fails since the value of
4920 ;; `mouse-autoselect-window-position' recorded there is the
4921 ;; position where the mouse has entered the new window and
4922 ;; not necessarily where the mouse has stopped moving.
4923 (equal mouse-position mouse-autoselect-window-position))
4924 ;; The minibuffer is a candidate window if it's active.
4925 (or (not (window-minibuffer-p window))
4926 (eq window (active-minibuffer-window))))
4927 ;; Mouse position has stabilized in non-selected window: Cancel
4928 ;; delayed autoselection and try to select that window.
4929 (mouse-autoselect-window-cancel t)
4930 ;; Select window where mouse appears unless the selected window is the
4931 ;; minibuffer. Use `unread-command-events' in order to execute pre-
4932 ;; and post-command hooks and trigger idle timers. To avoid delaying
4933 ;; autoselection again, set `mouse-autoselect-window-state'."
4934 (unless (window-minibuffer-p (selected-window))
4935 (setq mouse-autoselect-window-state 'select)
4936 (setq unread-command-events
4937 (cons (list 'select-window (list window))
4938 unread-command-events))))
4939 ((or (and window (eq window (selected-window)))
4940 (not (numberp mouse-autoselect-window))
4941 (equal mouse-position mouse-autoselect-window-position))
4942 ;; Mouse position has either stabilized in the selected window or at
4943 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
4944 (mouse-autoselect-window-cancel t))
4945 (t
4946 ;; Mouse position has not stabilized yet, resume delayed
4947 ;; autoselection.
4948 (mouse-autoselect-window-start mouse-position window))))
4949 (error nil)))
4950
4951(defun handle-select-window (event)
4952 "Handle select-window events."
4953 (interactive "e")
4954 (let ((window (posn-window (event-start event))))
4955 (unless (or (not (window-live-p window))
4956 ;; Don't switch if we're currently in the minibuffer.
4957 ;; This tries to work around problems where the
4958 ;; minibuffer gets unselected unexpectedly, and where
4959 ;; you then have to move your mouse all the way down to
4960 ;; the minibuffer to select it.
4961 (window-minibuffer-p (selected-window))
4962 ;; Don't switch to minibuffer window unless it's active.
4963 (and (window-minibuffer-p window)
4964 (not (minibuffer-window-active-p window)))
4965 ;; Don't switch when autoselection shall be delayed.
4966 (and (numberp mouse-autoselect-window)
4967 (not (zerop mouse-autoselect-window))
4968 (not (eq mouse-autoselect-window-state 'select))
4969 (progn
4970 ;; Cancel any delayed autoselection.
4971 (mouse-autoselect-window-cancel t)
4972 ;; Start delayed autoselection from current mouse
4973 ;; position and window.
4974 (mouse-autoselect-window-start (mouse-position) window)
4975 ;; Executing a command cancels delayed autoselection.
4976 (add-hook
4977 'pre-command-hook 'mouse-autoselect-window-cancel))))
4978 (when mouse-autoselect-window
4979 ;; Reset state of delayed autoselection.
4980 (setq mouse-autoselect-window-state nil)
4981 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
4982 (run-hooks 'mouse-leave-buffer-hook))
4983 (select-window window))))
4984
3c448ab6
MR
4985(defun truncated-partial-width-window-p (&optional window)
4986 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
4987WINDOW defaults to the selected window.
4988Return nil if WINDOW is not a partial-width window
4989 (regardless of the value of `truncate-lines').
4990Otherwise, consult the value of `truncate-partial-width-windows'
4991 for the buffer shown in WINDOW."
4992 (unless window
4993 (setq window (selected-window)))
4994 (unless (window-full-width-p window)
4995 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
4996 (window-buffer window))))
4997 (if (integerp t-p-w-w)
4998 (< (window-width window) t-p-w-w)
4999 t-p-w-w))))
562dd5e9
MR
5000\f
5001(define-key ctl-x-map "0" 'delete-window)
5002(define-key ctl-x-map "1" 'delete-other-windows)
5003(define-key ctl-x-map "2" 'split-window-above-each-other)
5004(define-key ctl-x-map "3" 'split-window-side-by-side)
9397e56f 5005(define-key ctl-x-map "o" 'other-window)
562dd5e9 5006(define-key ctl-x-map "^" 'enlarge-window)
3c448ab6
MR
5007(define-key ctl-x-map "}" 'enlarge-window-horizontally)
5008(define-key ctl-x-map "{" 'shrink-window-horizontally)
5009(define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
5010(define-key ctl-x-map "+" 'balance-windows)
5011(define-key ctl-x-4-map "0" 'kill-buffer-and-window)
5012
3c448ab6 5013;;; window.el ends here