Restore window-safely-shrinkable-p but avoid using it in calendar.el.
[emacs.git] / lisp / window.el
blob94ac9143efda9c0fb5a138baec44df8fc81c71ba
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2011
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; Window tree functions.
29 ;;; Code:
31 (eval-when-compile (require 'cl))
33 (defmacro save-selected-window (&rest body)
34 "Execute BODY, then select the previously selected window.
35 The value returned is the value of the last form in BODY.
37 This macro saves and restores the selected window, as well as the
38 selected window in each frame. If the previously selected window
39 is no longer live, then whatever window is selected at the end of
40 BODY remains selected. If the previously selected window of some
41 frame is no longer live at the end of BODY, that frame's selected
42 window is left alone.
44 This macro saves and restores the current buffer, since otherwise
45 its normal operation could make a different buffer current. The
46 order of recently selected windows and the buffer list ordering
47 are not altered by this macro (unless they are altered in BODY)."
48 (declare (indent 0) (debug t))
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))))))
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.
72 Return nil if WINDOW is the root window of its frame. WINDOW can
73 be any window."
74 (and window (window-parent window) (window-next window)))
76 (defsubst window-left (window)
77 "Return WINDOW's left sibling.
78 Return nil if WINDOW is the root window of its frame. WINDOW can
79 be any window."
80 (and window (window-parent window) (window-prev window)))
82 (defsubst window-child (window)
83 "Return WINDOW's first child window."
84 (or (window-vchild window) (window-hchild window)))
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))
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)
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))
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.
111 BUFFER-OR-NAME must be either a buffer or a string naming a live
112 buffer 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))
122 (error "No such buffer %s" buffer-or-name))))
124 (defsubst normalize-live-frame (frame)
125 "Return frame specified by FRAME.
126 FRAME 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)))
133 (defsubst normalize-any-window (window)
134 "Return window specified by WINDOW.
135 WINDOW must be a window that has not been deleted and defaults to
136 the selected window."
137 (if window
138 (if (window-any-p window)
139 window
140 (error "%s is not a window" window))
141 (selected-window)))
143 (defsubst normalize-live-window (window)
144 "Return live window specified by WINDOW.
145 WINDOW 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)))
152 (defvar ignore-window-parameters nil
153 "If non-nil, standard functions ignore window parameters.
154 The functions currently affected by this are `split-window',
155 `delete-window', `delete-other-windows' and `other-window'.
157 An application may bind this to a non-nil value around calls to
158 these functions to inhibit processing of window parameters.")
160 (defconst window-safe-min-height 1
161 "The absolut minimum number of lines of a window.
162 Anything less might crash Emacs.")
164 (defcustom window-min-height 4
165 "The minimum number of lines of any window.
166 The value has to accomodate a mode- or header-line if present. A
167 value less than `window-safe-min-height' is ignored. The value
168 of this variable is honored when windows are resized or split.
170 Applications should never rebind this variable. To resize a
171 window to a height less than the one specified here, an
172 application should instead call `resize-window' with a non-nil
173 IGNORE argument. In order to have `split-window' make a window
174 shorter, explictly specify the SIZE argument of that function."
175 :type 'integer
176 :version "24.1"
177 :group 'windows)
179 (defconst window-safe-min-width 2
180 "The absolut minimum number of columns of a window.
181 Anything less might crash Emacs.")
183 (defcustom window-min-width 10
184 "The minimum number of columns of any window.
185 The value has to accomodate margins, fringes, or scrollbars if
186 present. A value less than `window-safe-min-width' is ignored.
187 The value of this variable is honored when windows are resized or
188 split.
190 Applications should never rebind this variable. To resize a
191 window to a width less than the one specified here, an
192 application should instead call `resize-window' with a non-nil
193 IGNORE argument. In order to have `split-window' make a window
194 narrower, explictly specify the SIZE argument of that function."
195 :type 'integer
196 :version "24.1"
197 :group 'windows)
199 (defun window-iso-combination-p (&optional window horizontal)
200 "If WINDOW is a vertical combination return WINDOW's first child.
201 WINDOW can be any window and defaults to the selected one.
202 Optional argument HORIZONTAL non-nil means return WINDOW's first
203 child if WINDOW is a horizontal combination."
204 (setq window (normalize-any-window window))
205 (if horizontal
206 (window-hchild window)
207 (window-vchild window)))
209 (defsubst window-iso-combined-p (&optional window horizontal)
210 "Return non-nil if and only if WINDOW is vertically combined.
211 WINDOW can be any window and defaults to the selected one.
212 Optional argument HORIZONTAL non-nil means return non-nil if and
213 only 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))))
218 (defun window-iso-combinations (&optional window horizontal)
219 "Return largest number of vertically arranged subwindows of WINDOW.
220 WINDOW can be any window and defaults to the selected one.
221 Optional argument HORIZONTAL non-nil means to return the largest
222 number 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.
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))
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))))
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))))))
269 (defun walk-window-tree (proc &optional frame any)
270 "Run function PROC on each live window of FRAME.
271 PROC must be a function with one argument - a window. FRAME must
272 be a live frame and defaults to the selected one. ANY, if
273 non-nil means to run PROC on all live and internal windows of
274 FRAME.
276 This function performs a pre-order, depth-first traversal of the
277 window tree. If PROC changes the window tree, the result is
278 unpredictable."
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)))
283 (defun walk-window-subtree (proc &optional window any)
284 "Run function PROC on each live subwindow of WINDOW.
285 WINDOW defaults to the selected window. PROC must be a function
286 with one argument - a window. ANY, if non-nil means to run PROC
287 on all live and internal subwindows of WINDOW.
289 This function performs a pre-order, depth-first traversal of the
290 window tree rooted at WINDOW. If PROC changes that window tree,
291 the result is unpredictable."
292 (setq window (normalize-any-window window))
293 (walk-window-tree-1 proc window any t))
295 (defun windows-with-parameter (parameter &optional value frame any values)
296 "Return a list of all windows on FRAME with PARAMETER non-nil.
297 FRAME defaults to the selected frame. Optional argument VALUE
298 non-nil means only return windows whose window-parameter value of
299 PARAMETER equals VALUE \(comparison is done using `equal').
300 Optional argument ANY non-nil means consider internal windows
301 too. Optional argument VALUES non-nil means return a list of cons
302 cells whose car is the value of the parameter and whose cdr is
303 the 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)
315 (nreverse windows)))
317 (defun window-with-parameter (parameter &optional value frame any)
318 "Return first window on FRAME with PARAMETER non-nil.
319 FRAME defaults to the selected frame. Optional argument VALUE
320 non-nil means only return a window whose window-parameter value
321 for PARAMETER equals VALUE \(comparison is done with `equal').
322 Optional argument ANY non-nil means consider internal windows
323 too."
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))))
333 ;;; Atomic windows.
334 (defun window-atom-root (&optional window)
335 "Return root of atomic window WINDOW is a part of.
336 WINDOW can be any window and defaults to the selected one.
337 Return 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))
345 (defun make-window-atom (window)
346 "Make WINDOW an atomic window.
347 WINDOW 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))
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))))
383 (defun window-atom-check (&optional frame)
384 "Check atomicity of all windows on FRAME.
385 FRAME defaults to the selected frame. If an atomic window is
386 wrongly configured, reset the atomicity of all its subwindows to
387 nil. An atomic window is wrongly configured if it has no
388 subwindows or one of its subwindows is not atomic."
389 (window-atom-check-1 (frame-root-window frame)))
391 ;; Side windows.
392 (defvar window-sides '(left top right bottom)
393 "Window sides.")
395 (defcustom window-sides-vertical nil
396 "If non-nil, left and right side windows are full height.
397 Otherwise, top and bottom side windows are full width."
398 :type 'boolean
399 :group 'windows
400 :version "24.1")
402 (defcustom window-sides-slots '(nil nil nil nil)
403 "Maximum number of side window slots.
404 The value is a list of four elements specifying the number of
405 side window slots on \(in this order) the left, top, right and
406 bottom side of each frame. If an element is a number, this means
407 to display at most that many side windows on the corresponding
408 side. If an element is nil, this means there's no bound on the
409 number 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)
444 (defun window-side-check (&optional frame)
445 "Check the window-side parameter of all windows on FRAME.
446 FRAME defaults to the selected frame. If the configuration is
447 invalid, reset all window-side parameters to nil.
449 A valid configuration has to preserve the following invariant:
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.
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))))
515 (defun window-check (&optional frame)
516 "Check atomic and side windows on FRAME.
517 FRAME defaults to the selected frame."
518 (window-side-check frame)
519 (window-atom-check frame))
521 ;;; Window sizes.
522 (defvar window-size-fixed nil
523 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
524 If the value is `height', then only the window's height is fixed.
525 If the value is `width', then only the window's width is fixed.
526 Any other non-nil value fixes both the width and the height.
528 Emacs won't change the size of any window displaying that buffer,
529 unless it has no other choice \(like when deleting a neighboring
530 window).")
531 (make-variable-buffer-local 'window-size-fixed)
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))
537 (defun window-min-size (&optional window horizontal ignore)
538 "Return the minimum number of lines of WINDOW.
539 WINDOW can be an arbitrary window and defaults to the selected
540 one. Optional argument HORIZONTAL non-nil means return the
541 minimum number of columns of WINDOW.
543 Optional argument IGNORE non-nil means ignore any restrictions
544 imposed by fixed size windows, `window-min-height' or
545 `window-min-width' settings. IGNORE equal `safe' means live
546 windows may get as small as `window-safe-min-height' lines and
547 `window-safe-min-width' columns. IGNORE a window means ignore
548 restrictions for that window only."
549 (window-min-size-1
550 (normalize-any-window window) horizontal ignore))
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))))
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))))))))
618 (defun window-sizable (window delta &optional horizontal ignore)
619 "Return DELTA if DELTA lines can be added to WINDOW.
620 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
621 columns can be added to WINDOW. A return value of zero means
622 that no lines (or columns) can be added to WINDOW.
624 This function looks only at WINDOW and its subwindows. The
625 function `window-resizable' looks at other windows as well.
627 DELTA positive means WINDOW shall be enlarged by DELTA lines or
628 columns. If WINDOW cannot be enlarged by DELTA lines or columns
629 return the maximum value in the range 0..DELTA by which WINDOW
630 can be enlarged.
632 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
633 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
634 return the minimum value in the range DELTA..0 by which WINDOW
635 can be shrunk.
637 Optional argument IGNORE non-nil means ignore any restrictions
638 imposed by fixed size windows, `window-min-height' or
639 `window-min-width' settings. IGNORE equal `safe' means live
640 windows may get as small as `window-safe-min-height' lines and
641 `window-safe-min-width' columns. IGNORE any window means ignore
642 restrictions 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)
654 delta))
655 (t 0)))
657 (defsubst window-sizable-p (window delta &optional horizontal ignore)
658 "Return t if WINDOW can be resized by DELTA lines.
659 For the meaning of the arguments of this function see the
660 doc-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)))
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))))))))
699 (defun window-size-fixed-p (&optional window horizontal)
700 "Return non-nil if WINDOW's height is fixed.
701 WINDOW can be an arbitrary window and defaults to the selected
702 window. Optional argument HORIZONTAL non-nil means return
703 non-nil if WINDOW's width is fixed.
705 If this function returns nil, this does not necessarily mean that
706 WINDOW 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))
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.
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)))
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))))))
749 (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
750 "Return number of lines by which WINDOW can be shrunk.
751 WINDOW can be an arbitrary window and defaults to the selected
752 window. Return zero if WINDOW cannot be shrunk.
754 Optional argument HORIZONTAL non-nil means return number of
755 columns by which WINDOW can be shrunk.
757 Optional argument IGNORE non-nil means ignore any restrictions
758 imposed by fixed size windows, `window-min-height' or
759 `window-min-width' settings. IGNORE a window means ignore
760 restrictions for that window only. IGNORE equal `safe' means
761 live windows may get as small as `window-safe-min-height' lines
762 and `window-safe-min-width' columns.
764 Optional argument TRAIL `before' means only windows to the left
765 of or above WINDOW can be enlarged. Optional argument TRAIL
766 `after' means only windows to the right of or below WINDOW can be
767 enlarged.
769 Optional argument NOUP non-nil means don't go up in the window
770 tree but try to enlarge windows within WINDOW's combination only.
772 Optional argument NODOWN non-nil means don't check whether WINDOW
773 itself \(and its subwindows) can be shrunk; check only whether at
774 least 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.
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)))))
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)
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))))))
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.
832 WINDOW can be an arbitrary window and defaults to the selected
833 window. The return value is zero if WINDOW cannot be enlarged.
835 Optional argument HORIZONTAL non-nil means return maximum number
836 of columns by which WINDOW can be enlarged.
838 Optional argument IGNORE non-nil means ignore any restrictions
839 imposed by fixed size windows, `window-min-height' or
840 `window-min-width' settings. IGNORE a window means ignore
841 restrictions for that window only. IGNORE equal `safe' means
842 live windows may get as small as `window-safe-min-height' lines
843 and `window-safe-min-width' columns.
845 Optional argument TRAIL `before' means only windows to the left
846 of or below WINDOW can be shrunk. Optional argument TRAIL
847 `after' means only windows to the right of or above WINDOW can be
848 shrunk.
850 Optional argument NOUP non-nil means don't go up in the window
851 tree but try to obtain the entire space from windows within
852 WINDOW's combination.
854 Optional argument NODOWN non-nil means do not check whether
855 WINDOW itself \(and its subwindows) can be enlarged; check only
856 whether 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.
863 ;; WINDOW has no fixed size.
864 (window-max-delta-1 window 0 horizontal ignore trail noup)))
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.
869 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
870 can be resized horizontally by DELTA columns. A return value of
871 zero means that WINDOW is not resizable.
873 DELTA positive means WINDOW shall be enlarged by DELTA lines or
874 columns. If WINDOW cannot be enlarged by DELTA lines or columns
875 return the maximum value in the range 0..DELTA by which WINDOW
876 can be enlarged.
878 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
879 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
880 return the minimum value in the range DELTA..0 that can be used
881 for shrinking WINDOW.
883 Optional argument IGNORE non-nil means ignore any restrictions
884 imposed by fixed size windows, `window-min-height' or
885 `window-min-width' settings. IGNORE a window means ignore
886 restrictions for that window only. IGNORE equal `safe' means
887 live windows may get as small as `window-safe-min-height' lines
888 and `window-safe-min-width' columns.
890 Optional argument TRAIL `before' means only windows to the left
891 of or below WINDOW can be shrunk. Optional argument TRAIL
892 `after' means only windows to the right of or above WINDOW can be
893 shrunk.
895 Optional argument NOUP non-nil means don't go up in the window
896 tree but try to distribute the space among the other windows
897 within WINDOW's combination.
899 Optional argument NODOWN non-nil means don't check whether WINDOW
900 and 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)))
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.
913 For the meaning of the arguments of this function see the
914 doc-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)))
922 (defsubst window-total-height (&optional window)
923 "Return the total number of lines of WINDOW.
924 WINDOW can be any window and defaults to the selected one. The
925 return value includes WINDOW's mode line and header line, if any.
926 If WINDOW is internal the return value is the sum of the total
927 number of lines of WINDOW's child windows if these are vertically
928 combined and the height of WINDOW's first child otherwise.
930 Note: 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))
934 ;; Eventually we should make `window-height' obsolete.
935 (defalias 'window-height 'window-total-height)
937 ;; See discussion in bug#4543.
938 (defsubst window-full-height-p (&optional window)
939 "Return t if WINDOW is as high as the containing frame.
940 More precisely, return t if and only if the total height of
941 WINDOW equals the total height of the root window of WINDOW's
942 frame. WINDOW can be any window and defaults to the selected
943 one."
944 (setq window (normalize-any-window window))
945 (= (window-total-size window)
946 (window-total-size (frame-root-window window))))
948 (defsubst window-total-width (&optional window)
949 "Return the total number of columns of WINDOW.
950 WINDOW can be any window and defaults to the selected one. The
951 return value includes any vertical dividers or scrollbars of
952 WINDOW. If WINDOW is internal, the return value is the sum of
953 the total number of columns of WINDOW's child windows if these
954 are horizontally combined and the width of WINDOW's first child
955 otherwise."
956 (window-total-size window t))
958 (defsubst window-full-width-p (&optional window)
959 "Return t if WINDOW is as wide as the containing frame.
960 More precisely, return t if and only if the total width of WINDOW
961 equals the total width of the root window of WINDOW's frame.
962 WINDOW 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)))
967 (defsubst window-body-height (&optional window)
968 "Return the number of lines of WINDOW's body.
969 WINDOW must be a live window and defaults to the selected one.
971 The return value does not include WINDOW's mode line and header
972 line, if any. If a line at the bottom of the window is only
973 partially visible, that line is included in the return value. If
974 you do not want to include a partially visible bottom line in the
975 return value, use `window-text-height' instead."
976 (window-body-size window))
978 (defsubst window-body-width (&optional window)
979 "Return the number of columns of WINDOW's body.
980 WINDOW must be a live window and defaults to the selected one.
982 The return value does not include any vertical dividers or scroll
983 bars owned by WINDOW. On a window-system the return value does
984 not include the number of columns used for WINDOW's fringes or
985 display margins either."
986 (window-body-size window t))
988 ;; Eventually we should make `window-height' obsolete.
989 (defalias 'window-width 'window-body-width)
991 (defun window-current-scroll-bars (&optional window)
992 "Return the current scroll bar settings for WINDOW.
993 WINDOW must be a live window and defaults to the selected one.
995 The return value is a cons cell (VERTICAL . HORIZONTAL) where
996 VERTICAL specifies the current location of the vertical scroll
997 bars (`left', `right', or nil), and HORIZONTAL specifies the
998 current location of the horizontal scroll bars (`top', `bottom',
999 or nil).
1001 Unlike `window-scroll-bars', this function reports the scroll bar
1002 type actually used, once frame defaults and `scroll-bar-mode' are
1003 taken into account."
1004 (setq window (normalize-live-window window))
1005 (let ((vert (nth 2 (window-scroll-bars window)))
1006 (hor nil))
1007 (when (or (eq vert t) (eq hor t))
1008 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
1009 (if (eq vert t)
1010 (setq vert (car fcsb)))
1011 (if (eq hor t)
1012 (setq hor (cdr fcsb)))))
1013 (cons vert hor)))
1015 (defun walk-windows (proc &optional minibuf all-frames)
1016 "Cycle through all live windows, calling PROC for each one.
1017 PROC must specify a function with a window as its sole argument.
1018 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1019 windows to include in the walk.
1021 MINIBUF t means include the minibuffer window even if the
1022 minibuffer is not active. MINIBUF nil or omitted means include
1023 the minibuffer window only if the minibuffer is active. Any
1024 other value means do not include the minibuffer window even if
1025 the minibuffer is active.
1027 ALL-FRAMES nil or omitted means consider all windows on the
1028 selected frame, plus the minibuffer window if specified by the
1029 MINIBUF argument. If the minibuffer counts, consider all windows
1030 on all frames that share that minibuffer too. The following
1031 non-nil values of ALL-FRAMES have special meanings:
1033 - t means consider all windows on all existing frames.
1035 - `visible' means consider all windows on all visible frames on
1036 the current terminal.
1038 - 0 (the number zero) means consider all windows on all visible
1039 and iconified frames on the current terminal.
1041 - A frame means consider all windows on that frame only.
1043 Anything else means consider all windows on the selected frame
1044 and no others.
1046 This function changes neither the order of recently selected
1047 windows 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))
1058 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1059 (funcall proc walk-windows-window))))
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))))))
1073 (defun window-in-direction (direction &optional window ignore)
1074 "Return window in DIRECTION as seen from WINDOW.
1075 DIRECTION must be one of `above', `below', `left' or `right'.
1076 WINDOW must be a live window and defaults to the selected one.
1077 IGNORE, 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))))
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)))
1168 (defun get-window-with-predicate (predicate &optional minibuf
1169 all-frames default)
1170 "Return a live window satisfying PREDICATE.
1171 More precisely, cycle through all windows calling the function
1172 PREDICATE on each one of them with the window as its sole
1173 argument. Return the first window for which PREDICATE returns
1174 non-nil. If no window satisfies PREDICATE, return DEFAULT.
1176 ALL-FRAMES nil or omitted means consider all windows on the selected
1177 frame, plus the minibuffer window if specified by the MINIBUF
1178 argument. If the minibuffer counts, consider all windows on all
1179 frames that share that minibuffer too. The following non-nil
1180 values of ALL-FRAMES have special meanings:
1182 - t means consider all windows on all existing frames.
1184 - `visible' means consider all windows on all visible frames on
1185 the current terminal.
1187 - 0 (the number zero) means consider all windows on all visible
1188 and iconified frames on the current terminal.
1190 - A frame means consider all windows on that frame only.
1192 Anything else means consider all windows on the selected frame
1193 and no others."
1194 (catch 'found
1195 (dolist (window (window-list-1 nil minibuf all-frames))
1196 (when (funcall predicate window)
1197 (throw 'found window)))
1198 default))
1200 (defalias 'some-window 'get-window-with-predicate)
1202 (defun get-lru-window (&optional all-frames dedicated)
1203 "Return the least recently used window on frames specified by ALL-FRAMES.
1204 Return a full-width window if possible. A minibuffer window is
1205 never a candidate. A dedicated window is never a candidate
1206 unless DEDICATED is non-nil, so if all windows are dedicated, the
1207 value is nil. Avoid returning the selected window if possible.
1209 The following non-nil values of the optional argument ALL-FRAMES
1210 have special meanings:
1212 - t means consider all windows on all existing frames.
1214 - `visible' means consider all windows on all visible frames on
1215 the current terminal.
1217 - 0 (the number zero) means consider all windows on all visible
1218 and iconified frames on the current terminal.
1220 - A frame means consider all windows on that frame only.
1222 Any other value of ALL-FRAMES means consider all windows on the
1223 selected 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)))
1238 (defun get-mru-window (&optional all-frames)
1239 "Return the most recently used window on frames specified by ALL-FRAMES.
1240 Do not return a minibuffer window.
1242 The following non-nil values of the optional argument ALL-FRAMES
1243 have special meanings:
1245 - t means consider all windows on all existing frames.
1247 - `visible' means consider all windows on all visible frames on
1248 the current terminal.
1250 - 0 (the number zero) means consider all windows on all visible
1251 and iconified frames on the current terminal.
1253 - A frame means consider all windows on that frame only.
1255 Any other value of ALL-FRAMES means consider all windows on the
1256 selected 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))
1265 (defun get-largest-window (&optional all-frames dedicated)
1266 "Return the largest window on frames specified by ALL-FRAMES.
1267 A minibuffer window is never a candidate. A dedicated window is
1268 never a candidate unless DEDICATED is non-nil, so if all windows
1269 are dedicated, the value is nil.
1271 The following non-nil values of the optional argument ALL-FRAMES
1272 have special meanings:
1274 - t means consider all windows on all existing frames.
1276 - `visible' means consider all windows on all visible frames on
1277 the current terminal.
1279 - 0 (the number zero) means consider all windows on all visible
1280 and iconified frames on the current terminal.
1282 - A frame means consider all windows on that frame only.
1284 Any other value of ALL-FRAMES means consider all windows on the
1285 selected 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))
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.
1299 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1300 and defaults to the current buffer.
1302 Any windows showing BUFFER-OR-NAME on the selected frame are listed
1303 first.
1305 MINIBUF t means include the minibuffer window even if the
1306 minibuffer is not active. MINIBUF nil or omitted means include
1307 the minibuffer window only if the minibuffer is active. Any
1308 other value means do not include the minibuffer window even if
1309 the minibuffer is active.
1311 ALL-FRAMES nil or omitted means consider all windows on the
1312 selected frame, plus the minibuffer window if specified by the
1313 MINIBUF argument. If the minibuffer counts, consider all windows
1314 on all frames that share that minibuffer too. The following
1315 non-nil values of ALL-FRAMES have special meanings:
1317 - t means consider all windows on all existing frames.
1319 - `visible' means consider all windows on all visible frames on
1320 the current terminal.
1322 - 0 (the number zero) means consider all windows on all visible
1323 and iconified frames on the current terminal.
1325 - A frame means consider all windows on that frame only.
1327 Anything else means consider all windows on the selected frame
1328 and no others."
1329 (let ((buffer (normalize-live-buffer buffer-or-name))
1330 windows)
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)))
1336 (defun minibuffer-window-active-p (window)
1337 "Return t if WINDOW is the currently active minibuffer window."
1338 (eq window (active-minibuffer-window)))
1340 (defun count-windows (&optional minibuf)
1341 "Return the number of live windows on the selected frame.
1342 The optional argument MINIBUF specifies whether the minibuffer
1343 window shall be counted. See `walk-windows' for the precise
1344 meaning of this argument."
1345 (length (window-list-1 nil minibuf)))
1347 ;;; Resizing windows.
1348 (defun resize-window-reset (&optional frame horizontal)
1349 "Reset resize values for all windows on FRAME.
1350 FRAME defaults to the selected frame.
1352 This function stores the current value of `window-total-size' applied
1353 with argument HORIZONTAL in the new total size of all windows on
1354 FRAME. It also resets the new normal size of each of these
1355 windows."
1356 (resize-window-reset-1
1357 (frame-root-window (normalize-live-frame frame)) horizontal))
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)))
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.
1374 If WINDOW cannot be resized by DELTA lines make it as large \(or
1375 as 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)))
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))))
1401 (defun resize-window (window delta &optional horizontal ignore)
1402 "Resize WINDOW vertically by DELTA lines.
1403 WINDOW can be an arbitrary window and defaults to the selected
1404 one. An attempt to resize the root window of a frame will raise
1405 an error though.
1407 DELTA a positive number means WINDOW shall be enlarged by DELTA
1408 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1409 lines.
1411 Optional argument HORIZONTAL non-nil means resize WINDOW
1412 horizontally by DELTA columns. In this case a positive DELTA
1413 means enlarge WINDOW by DELTA columns. DELTA negative means
1414 WINDOW shall be shrunk by -DELTA columns.
1416 Optional argument IGNORE non-nil means ignore any restrictions
1417 imposed by fixed size windows, `window-min-height' or
1418 `window-min-width' settings. IGNORE any window means ignore
1419 restrictions for that window only. IGNORE equal `safe' means
1420 live windows may get as small as `window-safe-min-height' lines
1421 and `window-safe-min-width' columns.
1423 This function resizes other windows proportionally and never
1424 deletes any windows. If you want to move only the low (right)
1425 edge of WINDOW consider using `adjust-window-trailing-edge'
1426 instead."
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))
1459 (error "Cannot resize window %s" window)))))
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)))
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.
1467 HORIZONTAL non-nil means set the new normal width of these
1468 windows. WINDOW specifies a subwindow of PARENT that has been
1469 resized by THIS-DELTA lines \(columns).
1471 Optional argument TRAIL either 'before or 'after means set values
1472 for windows before or after WINDOW only. Optional argument
1473 OTHER-DELTA a number specifies that this many lines \(columns)
1474 have been obtained from \(or returned to) an ancestor window of
1475 PARENT 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)))
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)
1499 (setq parent-normal
1500 (+ parent-normal (window-normal-size sub horizontal)))))
1501 (setq sub (window-right sub)))
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)
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)))
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)
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))))
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))))
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))))))
1562 (defun resize-subwindows (parent delta &optional horizontal window ignore trail edge)
1563 "Resize subwindows of window PARENT vertically by DELTA lines.
1564 PARENT must be a vertically combined internal window.
1566 Optional argument HORIZONTAL non-nil means resize subwindows of
1567 PARENT horizontally by DELTA columns. In this case PARENT must
1568 be a horizontally combined internal window.
1570 WINDOW, if specified, must denote a child window of PARENT that
1571 is resized by DELTA lines.
1573 Optional argument IGNORE non-nil means ignore any restrictions
1574 imposed by fixed size windows, `window-min-height' or
1575 `window-min-width' settings. IGNORE equal `safe' means live
1576 windows may get as small as `window-safe-min-height' lines and
1577 `window-safe-min-width' columns. IGNORE any window means ignore
1578 restrictions for that window only.
1580 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1581 of windows that shall be resized. If TRAIL equals `before',
1582 resize only windows on the left or above EDGE. If TRAIL equals
1583 `after', resize only windows on the right or below EDGE. Also,
1584 preferably only resize windows adjacent to EDGE.
1586 Return the symbol `normalized' if new normal sizes have been
1587 already 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)
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
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
1658 (- (/ (float (window-total-size sub horizontal))
1659 parent-total)
1660 (window-normal-size sub horizontal)))))
1662 (setq sub (window-right sub)))
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))))
1679 (setq sub (window-right sub)))
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)))
1705 (setq sub (window-right sub)))
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))))))
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))
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)))))))
1737 (defun resize-other-windows (window delta &optional horizontal ignore trail edge)
1738 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1739 Optional argument HORIZONTAL non-nil means resize other windows
1740 when WINDOW is resized horizontally by DELTA columns. WINDOW
1741 itself is not resized by this function.
1743 Optional argument IGNORE non-nil means ignore any restrictions
1744 imposed by fixed size windows, `window-min-height' or
1745 `window-min-width' settings. IGNORE equal `safe' means live
1746 windows may get as small as `window-safe-min-height' lines and
1747 `window-safe-min-width' columns. IGNORE any window means ignore
1748 restrictions for that window only.
1750 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1751 of windows that shall be resized. If TRAIL equals `before',
1752 resize only windows on the left or above EDGE. If TRAIL equals
1753 `after', resize only windows on the right or below EDGE. Also,
1754 preferably 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)))
1782 (setq sub (window-right sub)))
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)))
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))
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)))
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))))
1833 (unless (zerop delta)
1834 ;; "Go up."
1835 (resize-other-windows parent delta horizontal ignore trail edge)))))
1837 (defun resize-this-window (window delta &optional horizontal ignore add trail edge)
1838 "Resize WINDOW vertically by DELTA lines.
1839 Optional argument HORIZONTAL non-nil means resize WINDOW
1840 horizontally by DELTA columns.
1842 Optional argument IGNORE non-nil means ignore any restrictions
1843 imposed by fixed size windows, `window-min-height' or
1844 `window-min-width' settings. IGNORE equal `safe' means live
1845 windows may get as small as `window-safe-min-height' lines and
1846 `window-safe-min-width' columns. IGNORE any window means ignore
1847 restrictions for that window only.
1849 Optional argument ADD non-nil means add DELTA to the new total
1850 size of WINDOW.
1852 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1853 of windows that shall be resized. If TRAIL equals `before',
1854 resize only windows on the left or above EDGE. If TRAIL equals
1855 `after', resize only windows on the right or below EDGE. Also,
1856 preferably only resize windows adjacent to EDGE.
1858 This function recursively resizes WINDOW's subwindows to fit the
1859 new size. Make sure that WINDOW is `window-resizable' before
1860 calling this function. Note that this function does not resize
1861 siblings of WINDOW or WINDOW's parent window. You have to
1862 eventually call `resize-window-apply' in order to make resizing
1863 actually take effect."
1864 (when add
1865 ;; Add DELTA to the new total size of WINDOW.
1866 (set-window-new-total window delta t))
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.
1877 (while sub
1878 (resize-this-window sub delta horizontal ignore t trail edge)
1879 (setq sub (window-right sub)))))))
1881 (defun resize-root-window (window delta horizontal ignore)
1882 "Resize root window WINDOW vertically by DELTA lines.
1883 HORIZONTAL non-nil means resize root window WINDOW horizontally
1884 by DELTA columns.
1886 IGNORE non-nil means ignore any restrictions imposed by fixed
1887 size windows, `window-min-height' or `window-min-width' settings.
1889 This function is only called by the frame resizing routines. It
1890 resizes 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)))
1896 (defun resize-root-window-vertically (window delta)
1897 "Resize root window WINDOW vertically by DELTA lines.
1898 If DELTA is less than zero and we can't shrink WINDOW by DELTA
1899 lines, shrink it as much as possible. If DELTA is greater than
1900 zero, this function can resize fixed-size subwindows in order to
1901 recover the necessary lines.
1903 Return the number of lines that were recovered.
1905 This function is only called by the minibuffer window resizing
1906 routines. It resizes windows proportionally and never deletes
1907 any 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))))
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)))
1930 (defun adjust-window-trailing-edge (window delta &optional horizontal)
1931 "Move WINDOW's bottom edge by DELTA lines.
1932 Optional argument HORIZONTAL non-nil means move WINDOW's right
1933 edge by DELTA columns. WINDOW defaults to the selected window.
1935 If DELTA is greater zero, then move the edge downwards or to the
1936 right. If DELTA is less than zero, move the edge upwards or to
1937 the left. If the edge can't be moved by DELTA lines or columns,
1938 move 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")))
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")))
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")))
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)))))))
2039 (defun enlarge-window (delta &optional horizontal)
2040 "Make selected window DELTA lines taller.
2041 Interactively, if no argument is given, make the selected window
2042 one line taller. If optional argument HORIZONTAL is non-nil,
2043 make selected window wider by DELTA columns. If DELTA is
2044 negative, shrink selected window by -DELTA lines or columns.
2045 Return nil."
2046 (interactive "p")
2047 (resize-window (selected-window) delta horizontal))
2049 (defun shrink-window (delta &optional horizontal)
2050 "Make selected window DELTA lines smaller.
2051 Interactively, if no argument is given, make the selected window
2052 one line smaller. If optional argument HORIZONTAL is non-nil,
2053 make selected window narrower by DELTA columns. If DELTA is
2054 negative, enlarge selected window by -DELTA lines or columns.
2055 Return nil."
2056 (interactive "p")
2057 (resize-window (selected-window) (- delta) horizontal))
2059 (defun maximize-window (&optional window)
2060 "Maximize WINDOW.
2061 Make WINDOW as large as possible without deleting any windows.
2062 WINDOW 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))
2068 (defun minimize-window (&optional window)
2069 "Minimize WINDOW.
2070 Make WINDOW as small as possible without deleting any windows.
2071 WINDOW 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))
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)))
2081 (defun window-tree-1 (window &optional next)
2082 "Return window tree rooted at WINDOW.
2083 Optional argument NEXT non-nil means include windows right
2084 siblings in the return value.
2086 See the documentation of `window-tree' for a description of the
2087 return value."
2088 (let (list)
2089 (while window
2090 (setq list
2091 (cons
2092 (cond
2093 ((window-vchild window)
2094 (cons t (cons (window-edges window)
2095 (window-tree-1 (window-vchild window) t))))
2096 ((window-hchild window)
2097 (cons nil (cons (window-edges window)
2098 (window-tree-1 (window-hchild window) t))))
2099 (t window))
2100 list))
2101 (setq window (when next (window-next window))))
2102 (nreverse list)))
2104 (defun window-tree (&optional frame)
2105 "Return the window tree of frame FRAME.
2106 FRAME must be a live frame and defaults to the selected frame.
2107 The return value is a list of the form (ROOT MINI), where ROOT
2108 represents the window tree of the frame's root window, and MINI
2109 is the frame's minibuffer window.
2111 If the root window is not split, ROOT is the root window itself.
2112 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2113 for a horizontal split, and t for a vertical split. EDGES gives
2114 the combined size and position of the subwindows in the split,
2115 and the rest of the elements are the subwindows in the split.
2116 Each of the subwindows may again be a window or a list
2117 representing a window split, and so on. EDGES is a list \(LEFT
2118 TOP RIGHT BOTTOM) as returned by `window-edges'."
2119 (setq frame (normalize-live-frame frame))
2120 (window-tree-1 (frame-root-window frame) t))
2122 (defun other-window (count &optional all-frames)
2123 "Select another window in cyclic ordering of windows.
2124 COUNT specifies the number of windows to skip, starting with the
2125 selected window, before making the selection. If COUNT is
2126 positive, skip COUNT windows forwards. If COUNT is negative,
2127 skip -COUNT windows backwards. COUNT zero means do not skip any
2128 window, so select the selected window. In an interactive call,
2129 COUNT is the numeric prefix argument. Return nil.
2131 If the `other-window' parameter of WINDOW is a function and
2132 `ignore-window-parameters' is nil, call that function with the
2133 arguments COUNT and ALL-FRAMES.
2135 This function does not select a window whose `no-other-window'
2136 window parameter is non-nil.
2138 This function uses `next-window' for finding the window to
2139 select. The argument ALL-FRAMES has the same meaning as in
2140 `next-window', but the MINIBUF argument of `next-window' is
2141 always effectively nil."
2142 (interactive "p")
2143 (let* ((window (selected-window))
2144 (function (and (not ignore-window-parameters)
2145 (window-parameter window 'other-window)))
2146 old-window old-count)
2147 (if (functionp function)
2148 (funcall function count all-frames)
2149 ;; `next-window' and `previous-window' may return a window we are
2150 ;; not allowed to select. Hence we need an exit strategy in case
2151 ;; all windows are non-selectable.
2152 (catch 'exit
2153 (while (> count 0)
2154 (setq window (next-window window nil all-frames))
2155 (cond
2156 ((eq window old-window)
2157 (when (= count old-count)
2158 ;; Keep out of infinite loops. When COUNT has not changed
2159 ;; since we last looked at `window' we're probably in one.
2160 (throw 'exit nil)))
2161 ((window-parameter window 'no-other-window)
2162 (unless old-window
2163 ;; The first non-selectable window `next-window' got us:
2164 ;; Remember it and the current value of COUNT.
2165 (setq old-window window)
2166 (setq old-count count)))
2168 (setq count (1- count)))))
2169 (while (< count 0)
2170 (setq window (previous-window window nil all-frames))
2171 (cond
2172 ((eq window old-window)
2173 (when (= count old-count)
2174 ;; Keep out of infinite loops. When COUNT has not changed
2175 ;; since we last looked at `window' we're probably in one.
2176 (throw 'exit nil)))
2177 ((window-parameter window 'no-other-window)
2178 (unless old-window
2179 ;; The first non-selectable window `previous-window' got
2180 ;; us: Remember it and the current value of COUNT.
2181 (setq old-window window)
2182 (setq old-count count)))
2184 (setq count (1+ count)))))
2186 (select-window window)
2187 ;; Always return nil.
2188 nil))))
2190 ;; This should probably return non-nil when the selected window is part
2191 ;; of an atomic window whose root is the frame's root window.
2192 (defun one-window-p (&optional nomini all-frames)
2193 "Return non-nil if the selected window is the only window.
2194 Optional arg NOMINI non-nil means don't count the minibuffer
2195 even if it is active. Otherwise, the minibuffer is counted
2196 when it is active.
2198 Optional argument ALL-FRAMES specifies the set of frames to
2199 consider, see also `next-window'. ALL-FRAMES nil or omitted
2200 means consider windows on the selected frame only, plus the
2201 minibuffer window if specified by the NOMINI argument. If the
2202 minibuffer counts, consider all windows on all frames that share
2203 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2204 with a special meaning are:
2206 - t means consider all windows on all existing frames.
2208 - `visible' means consider all windows on all visible frames on
2209 the current terminal.
2211 - 0 (the number zero) means consider all windows on all visible
2212 and iconified frames on the current terminal.
2214 - A frame means consider all windows on that frame only.
2216 Anything else means consider all windows on the selected frame
2217 and no others."
2218 (let ((base-window (selected-window)))
2219 (if (and nomini (eq base-window (minibuffer-window)))
2220 (setq base-window (next-window base-window)))
2221 (eq base-window
2222 (next-window base-window (if nomini 'arg) all-frames))))
2224 ;;; Deleting windows.
2225 (defun window-deletable-p (&optional window)
2226 "Return t if WINDOW can be safely deleted from its frame.
2227 Return `frame' if deleting WINDOW should delete its frame
2228 instead."
2229 (setq window (normalize-any-window window))
2230 (unless ignore-window-parameters
2231 ;; Handle atomicity.
2232 (when (window-parameter window 'window-atom)
2233 (setq window (window-atom-root window))))
2234 (let ((parent (window-parent window))
2235 (frame (window-frame window))
2236 (dedicated (and (window-buffer window) (window-dedicated-p window)))
2237 (quit-restore (window-parameter window 'quit-restore)))
2238 (cond
2239 ((frame-root-window-p window)
2240 (when (and (or dedicated
2241 (and (eq (car-safe quit-restore) 'new-frame)
2242 (eq (nth 1 quit-restore) (window-buffer window))))
2243 (other-visible-frames-p frame))
2244 ;; WINDOW is the root window of its frame. Return `frame' but
2245 ;; only if WINDOW is (1) either dedicated or quit-restore's car
2246 ;; is new-frame and the window still displays the same buffer
2247 ;; and (2) there are other frames left.
2248 'frame))
2249 ((and (not ignore-window-parameters)
2250 (eq (window-parameter window 'window-side) 'none)
2251 (or (not parent)
2252 (not (eq (window-parameter parent 'window-side) 'none))))
2253 ;; Can't delete last main window.
2254 nil)
2255 (t))))
2257 (defun window-or-subwindow-p (subwindow window)
2258 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2259 (or (eq subwindow window)
2260 (let ((parent (window-parent subwindow)))
2261 (catch 'done
2262 (while parent
2263 (if (eq parent window)
2264 (throw 'done t)
2265 (setq parent (window-parent parent))))))))
2267 (defun delete-window (&optional window)
2268 "Delete WINDOW.
2269 WINDOW can be an arbitrary window and defaults to the selected
2270 one. Return nil.
2272 If the variable `ignore-window-parameters' is non-nil or the
2273 `delete-window' parameter of WINDOW equals t, do not process any
2274 parameters of WINDOW. Otherwise, if the `delete-window'
2275 parameter of WINDOW specifies a function, call that function with
2276 WINDOW as its sole argument and return the value returned by that
2277 function.
2279 Otherwise, if WINDOW is part of an atomic window, call
2280 `delete-window' with the root of the atomic window as its
2281 argument. If WINDOW is the only window on its frame or the last
2282 non-side window, signal an error."
2283 (interactive)
2284 (setq window (normalize-any-window window))
2285 (let* ((frame (window-frame window))
2286 (function (window-parameter window 'delete-window))
2287 (parent (window-parent window))
2288 atom-root)
2289 (window-check frame)
2290 (catch 'done
2291 ;; Handle window parameters.
2292 (cond
2293 ;; Ignore window parameters if `ignore-window-parameters' tells
2294 ;; us so or `delete-window' equals t.
2295 ((or ignore-window-parameters (eq function t)))
2296 ((functionp function)
2297 ;; The `delete-window' parameter specifies the function to call.
2298 ;; If that function is `ignore' nothing is done. It's up to the
2299 ;; function called here to avoid infinite recursion.
2300 (throw 'done (funcall function window)))
2301 ((and (window-parameter window 'window-atom)
2302 (setq atom-root (window-atom-root window))
2303 (not (eq atom-root window)))
2304 (throw 'done (delete-window atom-root)))
2305 ((and (eq (window-parameter window 'window-side) 'none)
2306 (or (not parent)
2307 (not (eq (window-parameter parent 'window-side) 'none))))
2308 (error "Attempt to delete last non-side window"))
2309 ((not parent)
2310 (error "Attempt to delete minibuffer or sole ordinary window")))
2312 (let* ((horizontal (window-hchild parent))
2313 (size (window-total-size window horizontal))
2314 (frame-selected
2315 (window-or-subwindow-p (frame-selected-window frame) window))
2316 ;; Emacs 23 preferably gives WINDOW's space to its left
2317 ;; sibling.
2318 (sibling (or (window-left window) (window-right window))))
2319 (resize-window-reset frame horizontal)
2320 (cond
2321 ((and (not (window-splits window))
2322 sibling (window-sizable-p sibling size))
2323 ;; Resize WINDOW's sibling.
2324 (resize-this-window sibling size horizontal nil t)
2325 (set-window-new-normal
2326 sibling (+ (window-normal-size sibling horizontal)
2327 (window-normal-size window horizontal))))
2328 ((window-resizable-p window (- size) horizontal nil nil nil t)
2329 ;; Can do without resizing fixed-size windows.
2330 (resize-other-windows window (- size) horizontal))
2332 ;; Can't do without resizing fixed-size windows.
2333 (resize-other-windows window (- size) horizontal t)))
2334 ;; Actually delete WINDOW.
2335 (delete-window-internal window)
2336 (when (and frame-selected
2337 (window-parameter
2338 (frame-selected-window frame) 'no-other-window))
2339 ;; `delete-window-internal' has selected a window that should
2340 ;; not be selected, fix this here.
2341 (other-window -1 frame))
2342 (run-window-configuration-change-hook frame)
2343 (window-check frame)
2344 ;; Always return nil.
2345 nil))))
2347 (defun delete-other-windows (&optional window)
2348 "Make WINDOW fill its frame.
2349 WINDOW may be any window and defaults to the selected one.
2350 Return nil.
2352 If the variable `ignore-window-parameters' is non-nil or the
2353 `delete-other-windows' parameter of WINDOW equals t, do not
2354 process any parameters of WINDOW. Otherwise, if the
2355 `delete-other-windows' parameter of WINDOW specifies a function,
2356 call that function with WINDOW as its sole argument and return
2357 the value returned by that function.
2359 Otherwise, if WINDOW is part of an atomic window, call this
2360 function with the root of the atomic window as its argument. If
2361 WINDOW is a non-side window, make WINDOW the only non-side window
2362 on the frame. Side windows are not deleted. If WINDOW is a side
2363 window signal an error."
2364 (interactive)
2365 (setq window (normalize-any-window window))
2366 (let* ((frame (window-frame window))
2367 (function (window-parameter window 'delete-other-windows))
2368 (window-side (window-parameter window 'window-side))
2369 atom-root side-main)
2370 (window-check frame)
2371 (catch 'done
2372 (cond
2373 ;; Ignore window parameters if `ignore-window-parameters' is t or
2374 ;; `delete-other-windows' is t.
2375 ((or ignore-window-parameters (eq function t)))
2376 ((functionp function)
2377 ;; The `delete-other-windows' parameter specifies the function
2378 ;; to call. If the function is `ignore' no windows are deleted.
2379 ;; It's up to the function called to avoid infinite recursion.
2380 (throw 'done (funcall function window)))
2381 ((and (window-parameter window 'window-atom)
2382 (setq atom-root (window-atom-root window))
2383 (not (eq atom-root window)))
2384 (throw 'done (delete-other-windows atom-root)))
2385 ((eq window-side 'none)
2386 ;; Set side-main to the major non-side window.
2387 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2388 ((memq window-side window-sides)
2389 (error "Cannot make side window the only window")))
2390 ;; If WINDOW is the main non-side window, do nothing.
2391 (unless (eq window side-main)
2392 (delete-other-windows-internal window side-main)
2393 (run-window-configuration-change-hook frame)
2394 (window-check frame))
2395 ;; Always return nil.
2396 nil)))
2398 (defun delete-other-windows-vertically (&optional window)
2399 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2400 This may be a useful alternative binding for \\[delete-other-windows]
2401 if you often split windows horizontally."
2402 (interactive)
2403 (let* ((window (or window (selected-window)))
2404 (edges (window-edges window))
2405 (w window) delenda)
2406 (while (not (eq (setq w (next-window w 1)) window))
2407 (let ((e (window-edges w)))
2408 (when (and (= (car e) (car edges))
2409 (= (caddr e) (caddr edges)))
2410 (push w delenda))))
2411 (mapc 'delete-window delenda)))
2413 ;;; Windows and buffers.
2415 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2416 ;; for (1) determining which buffer to show in the window when its
2417 ;; buffer shall be buried or killed and (2) which buffer to show for
2418 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2420 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2421 ;; triples. The entries on this list are ordered by the time their
2422 ;; buffer has been removed from the window, the most recently removed
2423 ;; buffer's entry being first. The window-start and window-point
2424 ;; components are `window-start' and `window-point' at the time the
2425 ;; buffer was removed from the window which implies that the entry must
2426 ;; be added when `set-window-buffer' removes the buffer from the window.
2428 ;; `next-buffers' is the list of buffers that have been replaced
2429 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2430 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2431 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2432 ;; reset to nil by any action changing the window's buffer with the
2433 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2434 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2435 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2437 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2438 ;; if such a buffer was killed while the window was hidden within a
2439 ;; window configuration. Such killed buffers get removed whenever
2440 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2442 ;; The following function is called by `set-window-buffer' _before_ it
2443 ;; replaces the buffer of the argument window with the new buffer.
2444 (defun record-window-buffer (&optional window)
2445 "Record WINDOW's buffer.
2446 WINDOW must be a live window and defaults to the selected one."
2447 (let* ((window (normalize-live-window window))
2448 (buffer (window-buffer window))
2449 (entry (assq buffer (window-prev-buffers window))))
2450 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2451 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2452 (set-window-next-buffers window nil)
2454 (when entry
2455 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2456 (set-window-prev-buffers
2457 window (assq-delete-all buffer (window-prev-buffers window))))
2459 ;; Don't record insignificant buffers.
2460 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2461 ;; Add an entry for buffer to WINDOW's previous buffers.
2462 (with-current-buffer buffer
2463 (let ((start (window-start window))
2464 (point (window-point window)))
2465 (setq entry
2466 (cons buffer
2467 (if entry
2468 ;; We have an entry, update marker positions.
2469 (list (set-marker (nth 1 entry) start)
2470 (set-marker (nth 2 entry) point))
2471 ;; Make new markers.
2472 (list (copy-marker start)
2473 (copy-marker point)))))
2475 (set-window-prev-buffers
2476 window (cons entry (window-prev-buffers window))))))))
2478 (defun unrecord-window-buffer (&optional window buffer)
2479 "Unrecord BUFFER in WINDOW.
2480 WINDOW must be a live window and defaults to the selected one.
2481 BUFFER must be a live buffer and defaults to the buffer of
2482 WINDOW."
2483 (let* ((window (normalize-live-window window))
2484 (buffer (or buffer (window-buffer window))))
2485 (set-window-prev-buffers
2486 window (assq-delete-all buffer (window-prev-buffers window)))
2487 (set-window-next-buffers
2488 window (delq buffer (window-next-buffers window)))))
2490 (defun set-window-buffer-start-and-point (window buffer &optional start point)
2491 "Set WINDOW's buffer to BUFFER.
2492 Optional argument START non-nil means set WINDOW's start position
2493 to START. Optional argument POINT non-nil means set WINDOW's
2494 point to POINT. If WINDOW is selected this also sets BUFFER's
2495 `point' to POINT. If WINDOW is selected and the buffer it showed
2496 before was current this also makes BUFFER the current buffer."
2497 (let ((selected (eq window (selected-window)))
2498 (current (eq (window-buffer window) (current-buffer))))
2499 (set-window-buffer window buffer)
2500 (when (and selected current)
2501 (set-buffer buffer))
2502 (when start
2503 (set-window-start window start))
2504 (when point
2505 (if selected
2506 (with-current-buffer buffer
2507 (goto-char point))
2508 (set-window-point window point)))))
2510 (defun switch-to-prev-buffer (&optional window bury-or-kill)
2511 "In WINDOW switch to previous buffer.
2512 WINDOW must be a live window and defaults to the selected one.
2514 Optional argument BURY-OR-KILL non-nil means the buffer currently
2515 shown in WINDOW is about to be buried or killed and consequently
2516 shall not be switched to in future invocations of this command."
2517 (interactive)
2518 (let* ((window (normalize-live-window window))
2519 (old-buffer (window-buffer window))
2520 ;; Save this since it's destroyed by `set-window-buffer'.
2521 (next-buffers (window-next-buffers window))
2522 entry new-buffer killed-buffers deletable visible)
2523 (cond
2524 ;; When BURY-OR-KILL is non-nil, there's no previous buffer for
2525 ;; this window, and we can delete the window (or the frame) do
2526 ;; that.
2527 ((and bury-or-kill
2528 (or (not (window-prev-buffers window))
2529 (and (eq (caar (window-prev-buffers window)) old-buffer)
2530 (not (cdr (car (window-prev-buffers window))))))
2531 (setq deletable (window-deletable-p window)))
2532 (if (eq deletable 'frame)
2533 (delete-frame (window-frame window))
2534 (delete-window window)))
2535 ((window-dedicated-p window)
2536 (error "Window %s is dedicated to buffer %s" window old-buffer)))
2538 (unless deletable
2539 (catch 'found
2540 ;; Scan WINDOW's previous buffers first, skipping entries of next
2541 ;; buffers.
2542 (dolist (entry (window-prev-buffers window))
2543 (when (and (setq new-buffer (car entry))
2544 (or (buffer-live-p new-buffer)
2545 (not (setq killed-buffers
2546 (cons new-buffer killed-buffers))))
2547 (not (eq new-buffer old-buffer))
2548 (or bury-or-kill
2549 (not (memq new-buffer next-buffers))))
2550 (set-window-buffer-start-and-point
2551 window new-buffer (nth 1 entry) (nth 2 entry))
2552 (throw 'found t)))
2553 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2554 ;; entries of next buffers. Note that when we bury or kill a
2555 ;; buffer we don't reverse the global buffer list to avoid showing
2556 ;; a buried buffer instead. Otherwise, we must reverse the global
2557 ;; buffer list in order to make sure that switching to the
2558 ;; previous/next buffer traverse it in opposite directions.
2559 (dolist (buffer (if bury-or-kill
2560 (buffer-list (window-frame window))
2561 (nreverse (buffer-list (window-frame window)))))
2562 (when (and (buffer-live-p buffer)
2563 (not (eq buffer old-buffer))
2564 (not (eq (aref (buffer-name buffer) 0) ?\s))
2565 (or bury-or-kill (not (memq buffer next-buffers))))
2566 (if (get-buffer-window buffer)
2567 ;; Try to avoid showing a buffer visible in some other window.
2568 (setq visible buffer)
2569 (setq new-buffer buffer)
2570 (set-window-buffer-start-and-point window new-buffer)
2571 (throw 'found t))))
2572 (unless bury-or-kill
2573 ;; Scan reverted next buffers last (must not use nreverse
2574 ;; here!).
2575 (dolist (buffer (reverse next-buffers))
2576 ;; Actually, buffer _must_ be live here since otherwise it
2577 ;; would have been caught in the scan of previous buffers.
2578 (when (and (or (buffer-live-p buffer)
2579 (not (setq killed-buffers
2580 (cons buffer killed-buffers))))
2581 (not (eq buffer old-buffer))
2582 (setq entry (assq buffer (window-prev-buffers window))))
2583 (setq new-buffer buffer)
2584 (set-window-buffer-start-and-point
2585 window new-buffer (nth 1 entry) (nth 2 entry))
2586 (throw 'found t))))
2588 ;; Show a buffer visible in another window.
2589 (when visible
2590 (setq new-buffer visible)
2591 (set-window-buffer-start-and-point window new-buffer)))
2593 (if bury-or-kill
2594 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2595 ;; of) next buffers.
2596 (progn
2597 (set-window-prev-buffers
2598 window (assq-delete-all old-buffer (window-prev-buffers window)))
2599 (set-window-next-buffers window (delq old-buffer next-buffers)))
2600 ;; Move `old-buffer' to head of WINDOW's restored list of next
2601 ;; buffers.
2602 (set-window-next-buffers
2603 window (cons old-buffer (delq old-buffer next-buffers)))))
2605 ;; Remove killed buffers from WINDOW's previous and next buffers.
2606 (when killed-buffers
2607 (dolist (buffer killed-buffers)
2608 (set-window-prev-buffers
2609 window (assq-delete-all buffer (window-prev-buffers window)))
2610 (set-window-next-buffers
2611 window (delq buffer (window-next-buffers window)))))
2613 ;; Return new-buffer.
2614 new-buffer))
2616 (defun switch-to-next-buffer (&optional window)
2617 "In WINDOW switch to next buffer.
2618 WINDOW must be a live window and defaults to the selected one."
2619 (interactive)
2620 (let* ((window (normalize-live-window window))
2621 (old-buffer (window-buffer window))
2622 (next-buffers (window-next-buffers window))
2623 new-buffer entry killed-buffers visible)
2624 (when (window-dedicated-p window)
2625 (error "Window %s is dedicated to buffer %s" window old-buffer))
2627 (catch 'found
2628 ;; Scan WINDOW's next buffers first.
2629 (dolist (buffer next-buffers)
2630 (when (and (or (buffer-live-p buffer)
2631 (not (setq killed-buffers
2632 (cons buffer killed-buffers))))
2633 (not (eq buffer old-buffer))
2634 (setq entry (assq buffer (window-prev-buffers window))))
2635 (setq new-buffer buffer)
2636 (set-window-buffer-start-and-point
2637 window new-buffer (nth 1 entry) (nth 2 entry))
2638 (throw 'found t)))
2639 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2640 ;; buffers entries.
2641 (dolist (buffer (buffer-list (window-frame window)))
2642 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2643 (not (eq (aref (buffer-name buffer) 0) ?\s))
2644 (not (assq buffer (window-prev-buffers window))))
2645 (if (get-buffer-window buffer)
2646 ;; Try to avoid showing a buffer visible in some other window.
2647 (setq visible buffer)
2648 (setq new-buffer buffer)
2649 (set-window-buffer-start-and-point window new-buffer)
2650 (throw 'found t))))
2651 ;; Scan WINDOW's reverted previous buffers last (must not use
2652 ;; nreverse here!)
2653 (dolist (entry (reverse (window-prev-buffers window)))
2654 (when (and (setq new-buffer (car entry))
2655 (or (buffer-live-p new-buffer)
2656 (not (setq killed-buffers
2657 (cons new-buffer killed-buffers))))
2658 (not (eq new-buffer old-buffer)))
2659 (set-window-buffer-start-and-point
2660 window new-buffer (nth 1 entry) (nth 2 entry))
2661 (throw 'found t)))
2663 ;; Show a buffer visible in another window.
2664 (when visible
2665 (setq new-buffer visible)
2666 (set-window-buffer-start-and-point window new-buffer)))
2668 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2669 (set-window-next-buffers window (delq new-buffer next-buffers))
2671 ;; Remove killed buffers from WINDOW's previous and next buffers.
2672 (when killed-buffers
2673 (dolist (buffer killed-buffers)
2674 (set-window-prev-buffers
2675 window (assq-delete-all buffer (window-prev-buffers window)))
2676 (set-window-next-buffers
2677 window (delq buffer (window-next-buffers window)))))
2679 ;; Return new-buffer.
2680 new-buffer))
2682 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2683 "Search LIST for a valid buffer to display in FRAME.
2684 Return nil when all buffers in LIST are undesirable for display,
2685 otherwise return the first suitable buffer in LIST.
2687 Buffers not visible in windows are preferred to visible buffers,
2688 unless VISIBLE-OK is non-nil.
2689 If the optional argument FRAME is nil, it defaults to the selected frame.
2690 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2691 ;; This logic is more or less copied from other-buffer.
2692 (setq frame (or frame (selected-frame)))
2693 (let ((pred (frame-parameter frame 'buffer-predicate))
2694 found buf)
2695 (while (and (not found) list)
2696 (setq buf (car list))
2697 (if (and (not (eq buffer buf))
2698 (buffer-live-p buf)
2699 (or (null pred) (funcall pred buf))
2700 (not (eq (aref (buffer-name buf) 0) ?\s))
2701 (or visible-ok (null (get-buffer-window buf 'visible))))
2702 (setq found buf)
2703 (setq list (cdr list))))
2704 (car list)))
2706 (defun last-buffer (&optional buffer visible-ok frame)
2707 "Return the last buffer in FRAME's buffer list.
2708 If BUFFER is the last buffer, return the preceding buffer
2709 instead. Buffers not visible in windows are preferred to visible
2710 buffers, unless optional argument VISIBLE-OK is non-nil.
2711 Optional third argument FRAME nil or omitted means use the
2712 selected frame's buffer list. If no such buffer exists, return
2713 the buffer `*scratch*', creating it if necessary."
2714 (setq frame (or frame (selected-frame)))
2715 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2716 buffer visible-ok frame)
2717 (get-buffer "*scratch*")
2718 (let ((scratch (get-buffer-create "*scratch*")))
2719 (set-buffer-major-mode scratch)
2720 scratch)))
2722 (defun bury-buffer (&optional buffer-or-name)
2723 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2724 There it is the least likely candidate for `other-buffer' to
2725 return; thus, the least likely buffer for \\[switch-to-buffer] to
2726 select by default.
2728 You can specify a buffer name as BUFFER-OR-NAME, or an actual
2729 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2730 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2731 remove the current buffer from the selected window if it is
2732 displayed there."
2733 (interactive)
2734 (let* ((buffer (normalize-live-buffer buffer-or-name)))
2735 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2736 ;; although it's not "here" (call it a feature).
2737 (unrecord-buffer buffer)
2738 ;; Handle case where `buffer-or-name' is nil and the current buffer
2739 ;; is shown in the selected window.
2740 (cond
2741 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2742 ((not (window-dedicated-p))
2743 (switch-to-prev-buffer nil 'bury))
2744 ((frame-root-window-p (selected-window))
2745 (iconify-frame (window-frame (selected-window))))
2746 ((window-deletable-p)
2747 (delete-window)))
2748 ;; Always return nil.
2749 nil))
2751 (defun unbury-buffer ()
2752 "Switch to the last buffer in the buffer list."
2753 (interactive)
2754 (switch-to-buffer (last-buffer)))
2756 (defun next-buffer ()
2757 "In selected window switch to next buffer."
2758 (interactive)
2759 (switch-to-next-buffer))
2761 (defun previous-buffer ()
2762 "In selected window switch to previous buffer."
2763 (interactive)
2764 (switch-to-prev-buffer))
2766 (defun delete-windows-on (&optional buffer-or-name frame)
2767 "Delete all windows showing BUFFER-OR-NAME.
2768 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2769 and defaults to the current buffer.
2771 The following non-nil values of the optional argument FRAME
2772 have special meanings:
2774 - t means consider all windows on the selected frame only.
2776 - `visible' means consider all windows on all visible frames on
2777 the current terminal.
2779 - 0 (the number zero) means consider all windows on all visible
2780 and iconified frames on the current terminal.
2782 - A frame means consider all windows on that frame only.
2784 Any other value of FRAME means consider all windows on all
2785 frames.
2787 When a window showing BUFFER-OR-NAME is dedicated and the only
2788 window of its frame, that frame is deleted when there are other
2789 frames left."
2790 (interactive "BDelete windows on (buffer):\nP")
2791 (let ((buffer (normalize-live-buffer buffer-or-name))
2792 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2793 ;; `window-list-1' based function.
2794 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2795 (dolist (window (window-list-1 nil nil all-frames))
2796 (if (eq (window-buffer window) buffer)
2797 (let ((deletable (window-deletable-p window)))
2798 (cond
2799 ((eq deletable 'frame)
2800 ;; Delete frame.
2801 (delete-frame (window-frame window)))
2802 (deletable
2803 ;; Delete window only.
2804 (delete-window window))
2806 ;; In window switch to previous buffer.
2807 (set-window-dedicated-p window nil)
2808 (switch-to-prev-buffer window 'bury))))
2809 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
2810 (unrecord-window-buffer window buffer)))))
2812 (defun replace-buffer-in-windows (&optional buffer-or-name)
2813 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2814 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2815 and defaults to the current buffer.
2817 When a window showing BUFFER-OR-NAME is either dedicated, or the
2818 window has no previous buffer, that window is deleted. If that
2819 window is the only window on its frame, the frame is deleted too
2820 when there are other frames left. If there are no other frames
2821 left, some other buffer is displayed in that window.
2823 This function removes the buffer denoted by BUFFER-OR-NAME from
2824 all window-local buffer lists."
2825 (let ((buffer (normalize-live-buffer buffer-or-name)))
2826 (dolist (window (window-list-1 nil nil t))
2827 (if (eq (window-buffer window) buffer)
2828 (let ((deletable (window-deletable-p window)))
2829 (cond
2830 ((eq deletable 'frame)
2831 ;; Delete frame.
2832 (delete-frame (window-frame window)))
2833 ((and (window-dedicated-p window) deletable)
2834 ;; Delete window.
2835 (delete-window window))
2837 ;; Switch to another buffer in window.
2838 (set-window-dedicated-p window nil)
2839 (switch-to-prev-buffer window 'kill))))
2840 ;; Unrecord BUFFER in WINDOW.
2841 (unrecord-window-buffer window buffer)))))
2843 (defun quit-restore-window (&optional window kill)
2844 "Quit WINDOW in some way.
2845 WINDOW must be a live window and defaults to the selected window.
2846 Return nil.
2848 According to information stored in WINDOW's `quit-restore' window
2849 parameter either \(1) delete WINDOW and its frame, \(2) delete
2850 WINDOW, \(3) restore the buffer previously displayed in WINDOW,
2851 or \(4) make WINDOW display some other buffer than the present
2852 one. If non-nil, reset `quit-restore' parameter to nil.
2854 Optional argument KILL non-nil means in addition kill WINDOW's
2855 buffer. If KILL is nil, put WINDOW's buffer at the end of the
2856 buffer list. Interactively, KILL is the prefix argument."
2857 (interactive "i\nP")
2858 (setq window (normalize-live-window window))
2859 (let ((buffer (window-buffer window))
2860 (quit-restore (window-parameter window 'quit-restore))
2861 deletable resize)
2862 (cond
2863 ((and (or (and (memq (car-safe quit-restore) '(new-window new-frame))
2864 ;; Check that WINDOW's buffer is still the same.
2865 (eq (window-buffer window) (nth 1 quit-restore)))
2866 (window-dedicated-p window))
2867 (setq deletable (window-deletable-p window)))
2868 ;; WINDOW can be deleted.
2869 (unrecord-buffer buffer)
2870 (if (eq deletable 'frame)
2871 ;; WINDOW's frame can be deleted.
2872 (delete-frame (window-frame window))
2873 ;; Just delete WINDOW.
2874 (delete-window window))
2875 ;; If the previously selected window is still alive, select it.
2876 (when (window-live-p (nth 2 quit-restore))
2877 (select-window (nth 2 quit-restore))))
2878 ((and (buffer-live-p (nth 0 quit-restore))
2879 ;; The buffer currently shown in WINDOW must still be the
2880 ;; buffer shown when its `quit-restore' parameter was created
2881 ;; in the first place.
2882 (eq (window-buffer window) (nth 3 quit-restore)))
2883 (setq resize (with-current-buffer buffer temp-buffer-resize-mode))
2884 ;; Unrecord buffer.
2885 (unrecord-buffer buffer)
2886 (unrecord-window-buffer window buffer)
2887 ;; Display buffer stored in the quit-restore parameter.
2888 (set-window-dedicated-p window nil)
2889 (set-window-buffer window (nth 0 quit-restore))
2890 (set-window-start window (nth 1 quit-restore))
2891 (set-window-point window (nth 2 quit-restore))
2892 (when (and resize (/= (nth 4 quit-restore) (window-total-size window)))
2893 (resize-window
2894 window (- (nth 4 quit-restore) (window-total-size window))))
2895 ;; Reset the quit-restore parameter.
2896 (set-window-parameter window 'quit-restore nil)
2897 (when (window-live-p (nth 5 quit-restore))
2898 (select-window (nth 5 quit-restore))))
2900 ;; Otherwise, show another buffer in WINDOW and reset the
2901 ;; quit-restore parameter.
2902 (set-window-parameter window 'quit-restore nil)
2903 (unrecord-buffer buffer)
2904 (switch-to-prev-buffer window 'bury-or-kill)))
2906 ;; Kill WINDOW's old-buffer if requested
2907 (when kill (kill-buffer buffer))
2908 nil))
2910 ;;; Splitting windows.
2911 (defsubst window-split-min-size (&optional horizontal)
2912 "Return minimum height of any window when splitting windows.
2913 Optional argument HORIZONTAL non-nil means return minimum width."
2914 (if horizontal
2915 (max window-min-width window-safe-min-width)
2916 (max window-min-height window-safe-min-height)))
2918 (defun split-window (&optional window size side)
2919 "Make a new window adjacent to WINDOW.
2920 WINDOW can be any window and defaults to the selected one.
2921 Return the new window which is always a live window.
2923 Optional argument SIZE a positive number means make WINDOW SIZE
2924 lines or columns tall. If SIZE is negative, make the new window
2925 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
2926 absolute value can be less than `window-min-height' or
2927 `window-min-width'; so this command can make a new window as
2928 small as one line or two columns. SIZE defaults to half of
2929 WINDOW's size. Interactively, SIZE is the prefix argument.
2931 Optional third argument SIDE nil (or `below') specifies that the
2932 new window shall be located below WINDOW. SIDE `above' means the
2933 new window shall be located above WINDOW. In both cases SIZE
2934 specifies the new number of lines for WINDOW \(or the new window
2935 if SIZE is negative) including space reserved for the mode and/or
2936 header line.
2938 SIDE t (or `right') specifies that the new window shall be
2939 located on the right side of WINDOW. SIDE `left' means the new
2940 window shall be located on the left of WINDOW. In both cases
2941 SIZE specifies the new number of columns for WINDOW \(or the new
2942 window provided SIZE is negative) including space reserved for
2943 fringes and the scrollbar or a divider column. Any other non-nil
2944 value for SIDE is currently handled like t (or `right').
2946 If the variable `ignore-window-parameters' is non-nil or the
2947 `split-window' parameter of WINDOW equals t, do not process any
2948 parameters of WINDOW. Otherwise, if the `split-window' parameter
2949 of WINDOW specifies a function, call that function with all three
2950 arguments and return the value returned by that function.
2952 Otherwise, if WINDOW is part of an atomic window, \"split\" the
2953 root of that atomic window. The new window does not become a
2954 member of that atomic window.
2956 If WINDOW is live, properties of the new window like margins and
2957 scrollbars are inherited from WINDOW. If WINDOW is an internal
2958 window, these properties as well as the buffer displayed in the
2959 new window are inherited from the window selected on WINDOW's
2960 frame. The selected window is not changed by this function."
2961 (interactive "i")
2962 (setq window (normalize-any-window window))
2963 (let* ((horizontal (not (memq side '(nil below above))))
2964 (frame (window-frame window))
2965 (parent (window-parent window))
2966 (function (window-parameter window 'split-window))
2967 (window-side (window-parameter window 'window-side))
2968 ;; Rebind `window-nest' since in some cases we may have to
2969 ;; override its value.
2970 (window-nest window-nest)
2971 atom-root)
2973 (window-check frame)
2974 (catch 'done
2975 (cond
2976 ;; Ignore window parameters if either `ignore-window-parameters'
2977 ;; is t or the `split-window' parameter equals t.
2978 ((or ignore-window-parameters (eq function t)))
2979 ((functionp function)
2980 ;; The `split-window' parameter specifies the function to call.
2981 ;; If that function is `ignore', do nothing.
2982 (throw 'done (funcall function window size side)))
2983 ;; If WINDOW is a subwindow of an atomic window, split the root
2984 ;; window of that atomic window instead.
2985 ((and (window-parameter window 'window-atom)
2986 (setq atom-root (window-atom-root window))
2987 (not (eq atom-root window)))
2988 (throw 'done (split-window atom-root size side))))
2990 (when (and window-side
2991 (or (not parent)
2992 (not (window-parameter parent 'window-side))))
2993 ;; WINDOW is a side root window. To make sure that a new parent
2994 ;; window gets created set `window-nest' to t.
2995 (setq window-nest t))
2997 (when (and window-splits size (> size 0))
2998 ;; If `window-splits' is non-nil and SIZE is a non-negative
2999 ;; integer, we cannot reasonably resize other windows. Rather
3000 ;; bind `window-nest' to t to make sure that subsequent window
3001 ;; deletions are handled correctly.
3002 (setq window-nest t))
3004 (let* ((parent-size
3005 ;; `parent-size' is the size of WINDOW's parent, provided
3006 ;; it has one.
3007 (when parent (window-total-size parent horizontal)))
3008 ;; `resize' non-nil means we are supposed to resize other
3009 ;; windows in WINDOW's combination.
3010 (resize
3011 (and window-splits (not window-nest)
3012 ;; Resize makes sense in iso-combinations only.
3013 (window-iso-combined-p window horizontal)))
3014 ;; `old-size' is the current size of WINDOW.
3015 (old-size (window-total-size window horizontal))
3016 ;; `new-size' is the specified or calculated size of the
3017 ;; new window.
3018 (new-size
3019 (cond
3020 ((not size)
3021 (max (window-split-min-size horizontal)
3022 (if resize
3023 ;; When resizing try to give the new window the
3024 ;; average size of a window in its combination.
3025 (min (- parent-size
3026 (window-min-size parent horizontal))
3027 (/ parent-size
3028 (1+ (window-iso-combinations
3029 parent horizontal))))
3030 ;; Else try to give the new window half the size
3031 ;; of WINDOW (plus an eventual odd line).
3032 (+ (/ old-size 2) (% old-size 2)))))
3033 ((>= size 0)
3034 ;; SIZE non-negative specifies the new size of WINDOW.
3036 ;; Note: Specifying a non-negative SIZE is practically
3037 ;; always done as workaround for making the new window
3038 ;; appear above or on the left of the new window (the
3039 ;; ispell window is a typical example of that). In all
3040 ;; these cases the SIDE argument should be set to 'above
3041 ;; or 'left in order to support the 'resize option.
3042 ;; Here we have to nest the windows instead, see above.
3043 (- old-size size))
3045 ;; SIZE negative specifies the size of the new window.
3046 (- size))))
3047 new-parent new-normal)
3049 ;; Check SIZE.
3050 (cond
3051 ((not size)
3052 (cond
3053 (resize
3054 ;; SIZE unspecified, resizing.
3055 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3056 ;; Try again with minimum split size.
3057 (setq new-size
3058 (max new-size (window-split-min-size horizontal)))
3059 (not (window-sizable-p parent (- new-size) horizontal)))
3060 (error "Window %s too small for splitting" parent)))
3061 ((> (+ new-size (window-min-size window horizontal)) old-size)
3062 ;; SIZE unspecified, no resizing.
3063 (error "Window %s too small for splitting" window))))
3064 ((and (>= size 0)
3065 (or (>= size old-size)
3066 (< new-size (if horizontal
3067 window-safe-min-width
3068 window-safe-min-width))))
3069 ;; SIZE specified as new size of old window. If the new size
3070 ;; is larger than the old size or the size of the new window
3071 ;; would be less than the safe minimum, signal an error.
3072 (error "Window %s too small for splitting" window))
3073 (resize
3074 ;; SIZE specified, resizing.
3075 (unless (window-sizable-p parent (- new-size) horizontal)
3076 ;; If we cannot resize the parent give up.
3077 (error "Window %s too small for splitting" parent)))
3078 ((or (< new-size
3079 (if horizontal window-safe-min-width window-safe-min-height))
3080 (< (- old-size new-size)
3081 (if horizontal window-safe-min-width window-safe-min-height)))
3082 ;; SIZE specification violates minimum size restrictions.
3083 (error "Window %s too small for splitting" window)))
3085 (resize-window-reset frame horizontal)
3087 (setq new-parent
3088 ;; Make new-parent non-nil if we need a new parent window;
3089 ;; either because we want to nest or because WINDOW is not
3090 ;; iso-combined.
3091 (or window-nest (not (window-iso-combined-p window horizontal))))
3092 (setq new-normal
3093 ;; Make new-normal the normal size of the new window.
3094 (cond
3095 (size (/ (float new-size) (if new-parent old-size parent-size)))
3096 (new-parent 0.5)
3097 (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal))))
3098 (t (/ (window-normal-size window horizontal) 2.0))))
3100 (if resize
3101 ;; Try to get space from OLD's siblings. We could go "up" and
3102 ;; try getting additional space from surrounding windows but
3103 ;; we won't be able to return space to those windows when we
3104 ;; delete the one we create here. Hence we do not go up.
3105 (progn
3106 (resize-subwindows parent (- new-size) horizontal)
3107 (let* ((normal (- 1.0 new-normal))
3108 (sub (window-child parent)))
3109 (while sub
3110 (set-window-new-normal
3111 sub (* (window-normal-size sub horizontal) normal))
3112 (setq sub (window-right sub)))))
3113 ;; Get entire space from WINDOW.
3114 (set-window-new-total window (- old-size new-size))
3115 (resize-this-window window (- new-size) horizontal)
3116 (set-window-new-normal
3117 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3118 new-normal)))
3120 (let* ((new (split-window-internal window new-size side new-normal)))
3121 ;; Inherit window-side parameters, if any.
3122 (when (and window-side new-parent)
3123 (set-window-parameter (window-parent new) 'window-side window-side)
3124 (set-window-parameter new 'window-side window-side))
3126 (run-window-configuration-change-hook frame)
3127 (window-check frame)
3128 ;; Always return the new window.
3129 new)))))
3131 ;; I think this should be the default; I think people will prefer it--rms.
3132 (defcustom split-window-keep-point t
3133 "If non-nil, \\[split-window-above-each-other] keeps the original point \
3134 in both children.
3135 This is often more convenient for editing.
3136 If nil, adjust point in each of the two windows to minimize redisplay.
3137 This is convenient on slow terminals, but point can move strangely.
3139 This option applies only to `split-window-above-each-other' and
3140 functions that call it. `split-window' always keeps the original
3141 point in both children."
3142 :type 'boolean
3143 :group 'windows)
3145 (defun split-window-above-each-other (&optional size)
3146 "Split selected window into two windows, one above the other.
3147 The upper window gets SIZE lines and the lower one gets the rest.
3148 SIZE negative means the lower window gets -SIZE lines and the
3149 upper one the rest. With no argument, split windows equally or
3150 close to it. Both windows display the same buffer, now current.
3152 If the variable `split-window-keep-point' is non-nil, both new
3153 windows will get the same value of point as the selected window.
3154 This is often more convenient for editing. The upper window is
3155 the selected window.
3157 Otherwise, we choose window starts so as to minimize the amount of
3158 redisplay; this is convenient on slow terminals. The new selected
3159 window is the one that the current value of point appears in. The
3160 value of point can change if the text around point is hidden by the
3161 new mode line.
3163 Regardless of the value of `split-window-keep-point', the upper
3164 window is the original one and the return value is the new, lower
3165 window."
3166 (interactive "P")
3167 (let ((old-window (selected-window))
3168 (old-point (point))
3169 (size (and size (prefix-numeric-value size)))
3170 moved-by-window-height moved new-window bottom)
3171 (when (and size (< size 0) (< (- size) window-min-height))
3172 ;; `split-window' would not signal an error here.
3173 (error "Size of new window too small"))
3174 (setq new-window (split-window nil size))
3175 (unless split-window-keep-point
3176 (with-current-buffer (window-buffer)
3177 (goto-char (window-start))
3178 (setq moved (vertical-motion (window-height)))
3179 (set-window-start new-window (point))
3180 (when (> (point) (window-point new-window))
3181 (set-window-point new-window (point)))
3182 (when (= moved (window-height))
3183 (setq moved-by-window-height t)
3184 (vertical-motion -1))
3185 (setq bottom (point)))
3186 (and moved-by-window-height
3187 (<= bottom (point))
3188 (set-window-point old-window (1- bottom)))
3189 (and moved-by-window-height
3190 (<= (window-start new-window) old-point)
3191 (set-window-point new-window old-point)
3192 (select-window new-window)))
3193 ;; Always copy quit-restore parameter in interactive use.
3194 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3195 (when quit-restore
3196 (set-window-parameter new-window 'quit-restore quit-restore)))
3197 new-window))
3199 (defalias 'split-window-vertically 'split-window-above-each-other)
3201 (defun split-window-side-by-side (&optional size)
3202 "Split selected window into two windows side by side.
3203 The selected window becomes the left one and gets SIZE columns.
3204 SIZE negative means the right window gets -SIZE lines.
3206 SIZE includes the width of the window's scroll bar; if there are
3207 no scroll bars, it includes the width of the divider column to
3208 the window's right, if any. SIZE omitted or nil means split
3209 window equally.
3211 The selected window remains selected. Return the new window."
3212 (interactive "P")
3213 (let ((old-window (selected-window))
3214 (size (and size (prefix-numeric-value size)))
3215 new-window)
3216 (when (and size (< size 0) (< (- size) window-min-width))
3217 ;; `split-window' would not signal an error here.
3218 (error "Size of new window too small"))
3219 (setq new-window (split-window nil size t))
3220 ;; Always copy quit-restore parameter in interactive use.
3221 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3222 (when quit-restore
3223 (set-window-parameter new-window 'quit-restore quit-restore)))
3224 new-window))
3226 (defalias 'split-window-horizontally 'split-window-side-by-side)
3228 ;;; Balancing windows.
3230 ;; The following routine uses the recycled code from an old version of
3231 ;; `resize-subwindows'. It's not very pretty, but coding it the way the
3232 ;; new `resize-subwindows' code does would hardly make it any shorter or
3233 ;; more readable (FWIW we'd need three loops - one to calculate the
3234 ;; minimum sizes per window, one to enlarge or shrink windows until the
3235 ;; new parent-size matches, and one where we shrink the largest/enlarge
3236 ;; the smallest window).
3237 (defun balance-windows-2 (window horizontal)
3238 "Subroutine of `balance-windows-1'.
3239 WINDOW must be an iso-combination."
3240 (let* ((first (window-child window))
3241 (sub first)
3242 (number-of-children 0)
3243 (parent-size (window-new-total window))
3244 (total-sum parent-size)
3245 found failed size sub-total sub-delta sub-amount rest)
3246 (while sub
3247 (setq number-of-children (1+ number-of-children))
3248 (when (window-size-fixed-p sub horizontal)
3249 (setq total-sum
3250 (- total-sum (window-total-size sub horizontal)))
3251 (set-window-new-normal sub 'ignore))
3252 (setq sub (window-right sub)))
3254 (setq failed t)
3255 (while (and failed (> number-of-children 0))
3256 (setq size (/ total-sum number-of-children))
3257 (setq failed nil)
3258 (setq sub first)
3259 (while (and sub (not failed))
3260 ;; Ignore subwindows that should be ignored or are stuck.
3261 (unless (resize-subwindows-skip-p sub)
3262 (setq found t)
3263 (setq sub-total (window-total-size sub horizontal))
3264 (setq sub-delta (- size sub-total))
3265 (setq sub-amount
3266 (window-sizable sub sub-delta horizontal))
3267 ;; Register the new total size for this subwindow.
3268 (set-window-new-total sub (+ sub-total sub-amount))
3269 (unless (= sub-amount sub-delta)
3270 (setq total-sum (- total-sum sub-total sub-amount))
3271 (setq number-of-children (1- number-of-children))
3272 ;; We failed and need a new round.
3273 (setq failed t)
3274 (set-window-new-normal sub 'skip)))
3275 (setq sub (window-right sub))))
3277 (setq rest (% total-sum number-of-children))
3278 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3279 ;; (column) until `rest' is zero.
3280 (setq sub first)
3281 (while (and sub (> rest 0))
3282 (unless (resize-subwindows-skip-p window)
3283 (set-window-new-total sub 1 t)
3284 (setq rest (1- rest)))
3285 (setq sub (window-right sub)))
3287 ;; Fix rounding by trying to enlarge stuck windows by one line
3288 ;; (column) until `rest' equals zero.
3289 (setq sub first)
3290 (while (and sub (> rest 0))
3291 (unless (eq (window-new-normal sub) 'ignore)
3292 (set-window-new-total sub 1 t)
3293 (setq rest (1- rest)))
3294 (setq sub (window-right sub)))
3296 (setq sub first)
3297 (while sub
3298 ;; Record new normal sizes.
3299 (set-window-new-normal
3300 sub (/ (if (eq (window-new-normal sub) 'ignore)
3301 (window-total-size sub horizontal)
3302 (window-new-total sub))
3303 (float parent-size)))
3304 ;; Recursively balance each subwindow's subwindows.
3305 (balance-windows-1 sub horizontal)
3306 (setq sub (window-right sub)))))
3308 (defun balance-windows-1 (window &optional horizontal)
3309 "Subroutine of `balance-windows'."
3310 (if (window-child window)
3311 (let ((sub (window-child window)))
3312 (if (window-iso-combined-p sub horizontal)
3313 (balance-windows-2 window horizontal)
3314 (let ((size (window-new-total window)))
3315 (while sub
3316 (set-window-new-total sub size)
3317 (balance-windows-1 sub horizontal)
3318 (setq sub (window-right sub))))))))
3320 (defun balance-windows (&optional window-or-frame)
3321 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3322 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3323 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3324 subwindows of that frame's root window. If WINDOW-OR-FRAME
3325 denots a window, balance the sizes of all subwindows of that
3326 window."
3327 (interactive)
3328 (let* ((window
3329 (cond
3330 ((or (not window-or-frame)
3331 (frame-live-p window-or-frame))
3332 (frame-root-window window-or-frame))
3333 ((or (window-live-p window-or-frame)
3334 (window-child window-or-frame))
3335 window-or-frame)
3337 (error "Not a window or frame %s" window-or-frame))))
3338 (frame (window-frame window)))
3339 ;; Balance vertically.
3340 (resize-window-reset (window-frame window))
3341 (balance-windows-1 window)
3342 (resize-window-apply frame)
3343 ;; Balance horizontally.
3344 (resize-window-reset (window-frame window) t)
3345 (balance-windows-1 window t)
3346 (resize-window-apply frame t)))
3348 (defun window-fixed-size-p (&optional window direction)
3349 "Return t if WINDOW cannot be resized in DIRECTION.
3350 WINDOW defaults to the selected window. DIRECTION can be
3351 nil (i.e. any), `height' or `width'."
3352 (with-current-buffer (window-buffer window)
3353 (when (and (boundp 'window-size-fixed) window-size-fixed)
3354 (not (and direction
3355 (member (cons direction window-size-fixed)
3356 '((height . width) (width . height))))))))
3358 ;;; A different solution to balance-windows.
3359 (defvar window-area-factor 1
3360 "Factor by which the window area should be over-estimated.
3361 This is used by `balance-windows-area'.
3362 Changing this globally has no effect.")
3363 (make-variable-buffer-local 'window-area-factor)
3365 (defun balance-windows-area-adjust (window delta horizontal)
3366 "Wrapper around `resize-window' with error checking.
3367 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3368 ;; `resize-window' may fail if delta is too large.
3369 (while (>= (abs delta) 1)
3370 (condition-case nil
3371 (progn
3372 (resize-window window delta horizontal)
3373 (setq delta 0))
3374 (error
3375 ;;(message "adjust: %s" (error-message-string err))
3376 (setq delta (/ delta 2))))))
3378 (defun balance-windows-area ()
3379 "Make all visible windows the same area (approximately).
3380 See also `window-area-factor' to change the relative size of
3381 specific buffers."
3382 (interactive)
3383 (let* ((unchanged 0) (carry 0) (round 0)
3384 ;; Remove fixed-size windows.
3385 (wins (delq nil (mapcar (lambda (win)
3386 (if (not (window-fixed-size-p win)) win))
3387 (window-list nil 'nomini))))
3388 (changelog nil)
3389 next)
3390 ;; Resizing a window changes the size of surrounding windows in complex
3391 ;; ways, so it's difficult to balance them all. The introduction of
3392 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3393 ;; very difficult to do. `balance-window' above takes an off-line
3394 ;; approach: get the whole window tree, then balance it, then try to
3395 ;; adjust the windows so they fit the result.
3396 ;; Here, instead, we take a "local optimization" approach, where we just
3397 ;; go through all the windows several times until nothing needs to be
3398 ;; changed. The main problem with this approach is that it's difficult
3399 ;; to make sure it terminates, so we use some heuristic to try and break
3400 ;; off infinite loops.
3401 ;; After a round without any change, we allow a second, to give a chance
3402 ;; to the carry to propagate a minor imbalance from the end back to
3403 ;; the beginning.
3404 (while (< unchanged 2)
3405 ;; (message "New round")
3406 (setq unchanged (1+ unchanged) round (1+ round))
3407 (dolist (win wins)
3408 (setq next win)
3409 (while (progn (setq next (next-window next))
3410 (window-fixed-size-p next)))
3411 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3412 (let* ((horiz
3413 (< (car (window-edges win)) (car (window-edges next))))
3414 (areadiff (/ (- (* (window-height next) (window-width next)
3415 (buffer-local-value 'window-area-factor
3416 (window-buffer next)))
3417 (* (window-height win) (window-width win)
3418 (buffer-local-value 'window-area-factor
3419 (window-buffer win))))
3420 (max (buffer-local-value 'window-area-factor
3421 (window-buffer win))
3422 (buffer-local-value 'window-area-factor
3423 (window-buffer next)))))
3424 (edgesize (if horiz
3425 (+ (window-height win) (window-height next))
3426 (+ (window-width win) (window-width next))))
3427 (diff (/ areadiff edgesize)))
3428 (when (zerop diff)
3429 ;; Maybe diff is actually closer to 1 than to 0.
3430 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3431 (when (and (zerop diff) (not (zerop areadiff)))
3432 (setq diff (/ (+ areadiff carry) edgesize))
3433 ;; Change things smoothly.
3434 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3435 (if (zerop diff)
3436 ;; Make sure negligible differences don't accumulate to
3437 ;; become significant.
3438 (setq carry (+ carry areadiff))
3439 ;; This used `adjust-window-trailing-edge' before and uses
3440 ;; `resize-window' now. Error wrapping is still needed.
3441 (balance-windows-area-adjust win diff horiz)
3442 ;; (sit-for 0.5)
3443 (let ((change (cons win (window-edges win))))
3444 ;; If the same change has been seen already for this window,
3445 ;; we're most likely in an endless loop, so don't count it as
3446 ;; a change.
3447 (unless (member change changelog)
3448 (push change changelog)
3449 (setq unchanged 0 carry 0)))))))
3450 ;; We've now basically balanced all the windows.
3451 ;; But there may be some minor off-by-one imbalance left over,
3452 ;; so let's do some fine tuning.
3453 ;; (bw-finetune wins)
3454 ;; (message "Done in %d rounds" round)
3458 (defcustom display-buffer-function nil
3459 "If non-nil, function to call to handle `display-buffer'.
3460 It will receive two args, the buffer and a flag which if non-nil
3461 means that the currently selected window is not acceptable. It
3462 should choose or create a window, display the specified buffer in
3463 it, and return the window.
3465 Commands such as `switch-to-buffer-other-window' and
3466 `find-file-other-window' work using this function."
3467 :type '(choice
3468 (const nil)
3469 (function :tag "function"))
3470 :group 'windows)
3472 (defcustom special-display-buffer-names nil
3473 "List of names of buffers that should be displayed specially.
3474 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3475 its name is in this list, displays the buffer in a way specified
3476 by `special-display-function'. `special-display-popup-frame'
3477 \(the default for `special-display-function') usually displays
3478 the buffer in a separate frame made with the parameters specified
3479 by `special-display-frame-alist'. If `special-display-function'
3480 has been set to some other function, that function is called with
3481 the buffer as first, and nil as second argument.
3483 Alternatively, an element of this list can be specified as
3484 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
3485 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
3486 `special-display-popup-frame' will interpret such pairs as frame
3487 parameters when it creates a special frame, overriding the
3488 corresponding values from `special-display-frame-alist'.
3490 As a special case, if FRAME-PARAMETERS contains (same-window . t)
3491 `special-display-popup-frame' displays that buffer in the
3492 selected window. If FRAME-PARAMETERS contains (same-frame . t),
3493 it displays that buffer in a window on the selected frame.
3495 If `special-display-function' specifies some other function than
3496 `special-display-popup-frame', that function is called with the
3497 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
3498 argument.
3500 Finally, an element of this list can be also specified as
3501 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
3502 `special-display-popup-frame' will call FUNCTION with the buffer
3503 named BUFFER-NAME as first argument, and OTHER-ARGS as the
3504 second. If `special-display-function' specifies some other
3505 function, that function is called with the buffer named
3506 BUFFER-NAME as first, and the element's cdr as second argument.
3508 If this variable appears \"not to work\", because you added a
3509 name to it but the corresponding buffer is displayed in the
3510 selected window, look at the values of `same-window-buffer-names'
3511 and `same-window-regexps'. Those variables take precedence over
3512 this one.
3514 See also `special-display-regexps'."
3515 :type '(repeat
3516 (choice :tag "Buffer"
3517 :value ""
3518 (string :format "%v")
3519 (cons :tag "With parameters"
3520 :format "%v"
3521 :value ("" . nil)
3522 (string :format "%v")
3523 (repeat :tag "Parameters"
3524 (cons :format "%v"
3525 (symbol :tag "Parameter")
3526 (sexp :tag "Value"))))
3527 (list :tag "With function"
3528 :format "%v"
3529 :value ("" . nil)
3530 (string :format "%v")
3531 (function :tag "Function")
3532 (repeat :tag "Arguments" (sexp)))))
3533 :group 'windows
3534 :group 'frames)
3536 ;;;###autoload
3537 (put 'special-display-buffer-names 'risky-local-variable t)
3539 (defcustom special-display-regexps nil
3540 "List of regexps saying which buffers should be displayed specially.
3541 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3542 any regexp in this list matches its name, displays it specially
3543 using `special-display-function'. `special-display-popup-frame'
3544 \(the default for `special-display-function') usually displays
3545 the buffer in a separate frame made with the parameters specified
3546 by `special-display-frame-alist'. If `special-display-function'
3547 has been set to some other function, that function is called with
3548 the buffer as first, and nil as second argument.
3550 Alternatively, an element of this list can be specified as
3551 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
3552 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
3553 `special-display-popup-frame' will then interpret these pairs as
3554 frame parameters when creating a special frame for a buffer whose
3555 name matches REGEXP, overriding the corresponding values from
3556 `special-display-frame-alist'.
3558 As a special case, if FRAME-PARAMETERS contains (same-window . t)
3559 `special-display-popup-frame' displays buffers matching REGEXP in
3560 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
3561 to display such buffers in a window on the selected frame.
3563 If `special-display-function' specifies some other function than
3564 `special-display-popup-frame', that function is called with the
3565 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
3566 as second argument.
3568 Finally, an element of this list can be also specified as
3569 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
3570 will then call FUNCTION with the buffer whose name matched
3571 REGEXP as first, and OTHER-ARGS as second argument. If
3572 `special-display-function' specifies some other function, that
3573 function is called with the buffer whose name matched REGEXP
3574 as first, and the element's cdr as second argument.
3576 If this variable appears \"not to work\", because you added a
3577 name to it but the corresponding buffer is displayed in the
3578 selected window, look at the values of `same-window-buffer-names'
3579 and `same-window-regexps'. Those variables take precedence over
3580 this one.
3582 See also `special-display-buffer-names'."
3583 :type '(repeat
3584 (choice :tag "Buffer"
3585 :value ""
3586 (regexp :format "%v")
3587 (cons :tag "With parameters"
3588 :format "%v"
3589 :value ("" . nil)
3590 (regexp :format "%v")
3591 (repeat :tag "Parameters"
3592 (cons :format "%v"
3593 (symbol :tag "Parameter")
3594 (sexp :tag "Value"))))
3595 (list :tag "With function"
3596 :format "%v"
3597 :value ("" . nil)
3598 (regexp :format "%v")
3599 (function :tag "Function")
3600 (repeat :tag "Arguments" (sexp)))))
3601 :group 'windows
3602 :group 'frames)
3604 (defun special-display-p (buffer-name)
3605 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
3606 More precisely, return t if `special-display-buffer-names' or
3607 `special-display-regexps' contain a string entry equaling or
3608 matching BUFFER-NAME. If `special-display-buffer-names' or
3609 `special-display-regexps' contain a list entry whose car equals
3610 or matches BUFFER-NAME, the return value is the cdr of that
3611 entry."
3612 (let (tmp)
3613 (cond
3614 ((not (stringp buffer-name)))
3615 ((member buffer-name special-display-buffer-names)
3617 ((setq tmp (assoc buffer-name special-display-buffer-names))
3618 (cdr tmp))
3619 ((catch 'found
3620 (dolist (regexp special-display-regexps)
3621 (cond
3622 ((stringp regexp)
3623 (when (string-match-p regexp buffer-name)
3624 (throw 'found t)))
3625 ((and (consp regexp) (stringp (car regexp))
3626 (string-match-p (car regexp) buffer-name))
3627 (throw 'found (cdr regexp))))))))))
3629 (defcustom special-display-function 'special-display-popup-frame
3630 "Function to call for displaying special buffers.
3631 This function is called with two arguments - the buffer and,
3632 optionally, a list - and should return a window displaying that
3633 buffer. The default value usually makes a separate frame for the
3634 buffer using `special-display-frame-alist' to specify the frame
3635 parameters. See the definition of `special-display-popup-frame'
3636 for how to specify such a function.
3638 A buffer is special when its name is either listed in
3639 `special-display-buffer-names' or matches a regexp in
3640 `special-display-regexps'."
3641 :type 'function
3642 :group 'frames)
3644 (defcustom same-window-buffer-names nil
3645 "List of names of buffers that should appear in the \"same\" window.
3646 `display-buffer' and `pop-to-buffer' show a buffer whose name is
3647 on this list in the selected rather than some other window.
3649 An element of this list can be a cons cell instead of just a
3650 string. In that case, the cell's car must be a string specifying
3651 the buffer name. This is for compatibility with
3652 `special-display-buffer-names'; the cdr of the cons cell is
3653 ignored.
3655 See also `same-window-regexps'."
3656 :type '(repeat (string :format "%v"))
3657 :group 'windows)
3659 (defcustom same-window-regexps nil
3660 "List of regexps saying which buffers should appear in the \"same\" window.
3661 `display-buffer' and `pop-to-buffer' show a buffer whose name
3662 matches a regexp on this list in the selected rather than some
3663 other window.
3665 An element of this list can be a cons cell instead of just a
3666 string. In that case, the cell's car must be a regexp matching
3667 the buffer name. This is for compatibility with
3668 `special-display-regexps'; the cdr of the cons cell is ignored.
3670 See also `same-window-buffer-names'."
3671 :type '(repeat (regexp :format "%v"))
3672 :group 'windows)
3674 (defun same-window-p (buffer-name)
3675 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
3676 This function returns non-nil if `display-buffer' or
3677 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
3678 selected rather than \(as usual\) some other window. See
3679 `same-window-buffer-names' and `same-window-regexps'."
3680 (cond
3681 ((not (stringp buffer-name)))
3682 ;; The elements of `same-window-buffer-names' can be buffer
3683 ;; names or cons cells whose cars are buffer names.
3684 ((member buffer-name same-window-buffer-names))
3685 ((assoc buffer-name same-window-buffer-names))
3686 ((catch 'found
3687 (dolist (regexp same-window-regexps)
3688 ;; The elements of `same-window-regexps' can be regexps
3689 ;; or cons cells whose cars are regexps.
3690 (when (or (and (stringp regexp)
3691 (string-match regexp buffer-name))
3692 (and (consp regexp) (stringp (car regexp))
3693 (string-match-p (car regexp) buffer-name)))
3694 (throw 'found t)))))))
3696 (defcustom pop-up-frames nil
3697 "Whether `display-buffer' should make a separate frame.
3698 If nil, never make a separate frame.
3699 If the value is `graphic-only', make a separate frame
3700 on graphic displays only.
3701 Any other non-nil value means always make a separate frame."
3702 :type '(choice
3703 (const :tag "Never" nil)
3704 (const :tag "On graphic displays only" graphic-only)
3705 (const :tag "Always" t))
3706 :group 'windows)
3708 (defcustom display-buffer-reuse-frames nil
3709 "Non-nil means `display-buffer' should reuse frames.
3710 If the buffer in question is already displayed in a frame, raise
3711 that frame."
3712 :type 'boolean
3713 :version "21.1"
3714 :group 'windows)
3716 (defcustom pop-up-windows t
3717 "Non-nil means `display-buffer' should make a new window."
3718 :type 'boolean
3719 :group 'windows)
3721 (defcustom split-window-preferred-function 'split-window-sensibly
3722 "Function called by `display-buffer' routines to split a window.
3723 This function is called with a window as single argument and is
3724 supposed to split that window and return the new window. If the
3725 window can (or shall) not be split, it is supposed to return nil.
3726 The default is to call the function `split-window-sensibly' which
3727 tries to split the window in a way which seems most suitable.
3728 You can customize the options `split-height-threshold' and/or
3729 `split-width-threshold' in order to have `split-window-sensibly'
3730 prefer either vertical or horizontal splitting.
3732 If you set this to any other function, bear in mind that the
3733 `display-buffer' routines may call this function two times. The
3734 argument of the first call is the largest window on its frame.
3735 If that call fails to return a live window, the function is
3736 called again with the least recently used window as argument. If
3737 that call fails too, `display-buffer' will use an existing window
3738 to display its buffer.
3740 The window selected at the time `display-buffer' was invoked is
3741 still selected when this function is called. Hence you can
3742 compare the window argument with the value of `selected-window'
3743 if you intend to split the selected window instead or if you do
3744 not want to split the selected window."
3745 :type 'function
3746 :version "23.1"
3747 :group 'windows)
3749 (defcustom split-height-threshold 80
3750 "Minimum height for splitting windows sensibly.
3751 If this is an integer, `split-window-sensibly' may split a window
3752 vertically only if it has at least this many lines. If this is
3753 nil, `split-window-sensibly' is not allowed to split a window
3754 vertically. If, however, a window is the only window on its
3755 frame, `split-window-sensibly' may split it vertically
3756 disregarding the value of this variable."
3757 :type '(choice (const nil) (integer :tag "lines"))
3758 :version "23.1"
3759 :group 'windows)
3761 (defcustom split-width-threshold 160
3762 "Minimum width for splitting windows sensibly.
3763 If this is an integer, `split-window-sensibly' may split a window
3764 horizontally only if it has at least this many columns. If this
3765 is nil, `split-window-sensibly' is not allowed to split a window
3766 horizontally."
3767 :type '(choice (const nil) (integer :tag "columns"))
3768 :version "23.1"
3769 :group 'windows)
3771 (defun window-splittable-p (window &optional horizontal)
3772 "Return non-nil if `split-window-sensibly' may split WINDOW.
3773 Optional argument HORIZONTAL nil or omitted means check whether
3774 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
3775 non-nil means check whether WINDOW may be split horizontally.
3777 WINDOW may be split vertically when the following conditions
3778 hold:
3779 - `window-size-fixed' is either nil or equals `width' for the
3780 buffer of WINDOW.
3781 - `split-height-threshold' is an integer and WINDOW is at least as
3782 high as `split-height-threshold'.
3783 - When WINDOW is split evenly, the emanating windows are at least
3784 `window-min-height' lines tall and can accommodate at least one
3785 line plus - if WINDOW has one - a mode line.
3787 WINDOW may be split horizontally when the following conditions
3788 hold:
3789 - `window-size-fixed' is either nil or equals `height' for the
3790 buffer of WINDOW.
3791 - `split-width-threshold' is an integer and WINDOW is at least as
3792 wide as `split-width-threshold'.
3793 - When WINDOW is split evenly, the emanating windows are at least
3794 `window-min-width' or two (whichever is larger) columns wide."
3795 (when (window-live-p window)
3796 (with-current-buffer (window-buffer window)
3797 (if horizontal
3798 ;; A window can be split horizontally when its width is not
3799 ;; fixed, it is at least `split-width-threshold' columns wide
3800 ;; and at least twice as wide as `window-min-width' and 2 (the
3801 ;; latter value is hardcoded).
3802 (and (memq window-size-fixed '(nil height))
3803 ;; Testing `window-full-width-p' here hardly makes any
3804 ;; sense nowadays. This can be done more intuitively by
3805 ;; setting up `split-width-threshold' appropriately.
3806 (numberp split-width-threshold)
3807 (>= (window-width window)
3808 (max split-width-threshold
3809 (* 2 (max window-min-width 2)))))
3810 ;; A window can be split vertically when its height is not
3811 ;; fixed, it is at least `split-height-threshold' lines high,
3812 ;; and it is at least twice as high as `window-min-height' and 2
3813 ;; if it has a modeline or 1.
3814 (and (memq window-size-fixed '(nil width))
3815 (numberp split-height-threshold)
3816 (>= (window-height window)
3817 (max split-height-threshold
3818 (* 2 (max window-min-height
3819 (if mode-line-format 2 1))))))))))
3821 (defun split-window-sensibly (window)
3822 "Split WINDOW in a way suitable for `display-buffer'.
3823 If `split-height-threshold' specifies an integer, WINDOW is at
3824 least `split-height-threshold' lines tall and can be split
3825 vertically, split WINDOW into two windows one above the other and
3826 return the lower window. Otherwise, if `split-width-threshold'
3827 specifies an integer, WINDOW is at least `split-width-threshold'
3828 columns wide and can be split horizontally, split WINDOW into two
3829 windows side by side and return the window on the right. If this
3830 can't be done either and WINDOW is the only window on its frame,
3831 try to split WINDOW vertically disregarding any value specified
3832 by `split-height-threshold'. If that succeeds, return the lower
3833 window. Return nil otherwise.
3835 By default `display-buffer' routines call this function to split
3836 the largest or least recently used window. To change the default
3837 customize the option `split-window-preferred-function'.
3839 You can enforce this function to not split WINDOW horizontally,
3840 by setting \(or binding) the variable `split-width-threshold' to
3841 nil. If, in addition, you set `split-height-threshold' to zero,
3842 chances increase that this function does split WINDOW vertically.
3844 In order to not split WINDOW vertically, set \(or bind) the
3845 variable `split-height-threshold' to nil. Additionally, you can
3846 set `split-width-threshold' to zero to make a horizontal split
3847 more likely to occur.
3849 Have a look at the function `window-splittable-p' if you want to
3850 know how `split-window-sensibly' determines whether WINDOW can be
3851 split."
3852 (or (and (window-splittable-p window)
3853 ;; Split window vertically.
3854 (with-selected-window window
3855 (split-window-vertically)))
3856 (and (window-splittable-p window t)
3857 ;; Split window horizontally.
3858 (with-selected-window window
3859 (split-window-horizontally)))
3860 (and (eq window (frame-root-window (window-frame window)))
3861 (not (window-minibuffer-p window))
3862 ;; If WINDOW is the only window on its frame and is not the
3863 ;; minibuffer window, try to split it vertically disregarding
3864 ;; the value of `split-height-threshold'.
3865 (let ((split-height-threshold 0))
3866 (when (window-splittable-p window)
3867 (with-selected-window window
3868 (split-window-vertically)))))))
3870 (defun window--try-to-split-window (window)
3871 "Try to split WINDOW.
3872 Return value returned by `split-window-preferred-function' if it
3873 represents a live window, nil otherwise."
3874 (and (window-live-p window)
3875 (not (frame-parameter (window-frame window) 'unsplittable))
3876 (let ((new-window
3877 ;; Since `split-window-preferred-function' might
3878 ;; throw an error use `condition-case'.
3879 (condition-case nil
3880 (funcall split-window-preferred-function window)
3881 (error nil))))
3882 (and (window-live-p new-window) new-window))))
3884 (defun window--frame-usable-p (frame)
3885 "Return FRAME if it can be used to display a buffer."
3886 (when (frame-live-p frame)
3887 (let ((window (frame-root-window frame)))
3888 ;; `frame-root-window' may be an internal window which is considered
3889 ;; "dead" by `window-live-p'. Hence if `window' is not live we
3890 ;; implicitly know that `frame' has a visible window we can use.
3891 (unless (and (window-live-p window)
3892 (or (window-minibuffer-p window)
3893 ;; If the window is soft-dedicated, the frame is usable.
3894 ;; Actually, even if the window is really dedicated,
3895 ;; the frame is still usable by splitting it.
3896 ;; At least Emacs-22 allowed it, and it is desirable
3897 ;; when displaying same-frame windows.
3898 nil ; (eq t (window-dedicated-p window))
3900 frame))))
3902 (defcustom even-window-heights t
3903 "If non-nil `display-buffer' will try to even window heights.
3904 Otherwise `display-buffer' will leave the window configuration
3905 alone. Heights are evened only when `display-buffer' chooses a
3906 window that appears above or below the selected window."
3907 :type 'boolean
3908 :group 'windows)
3910 (defun window--even-window-heights (window)
3911 "Even heights of WINDOW and selected window.
3912 Do this only if these windows are vertically adjacent to each
3913 other, `even-window-heights' is non-nil, and the selected window
3914 is higher than WINDOW."
3915 (when (and even-window-heights
3916 (not (eq window (selected-window)))
3917 ;; Don't resize minibuffer windows.
3918 (not (window-minibuffer-p (selected-window)))
3919 (> (window-height (selected-window)) (window-height window))
3920 (eq (window-frame window) (window-frame (selected-window)))
3921 (let ((sel-edges (window-edges (selected-window)))
3922 (win-edges (window-edges window)))
3923 (and (= (nth 0 sel-edges) (nth 0 win-edges))
3924 (= (nth 2 sel-edges) (nth 2 win-edges))
3925 (or (= (nth 1 sel-edges) (nth 3 win-edges))
3926 (= (nth 3 sel-edges) (nth 1 win-edges))))))
3927 (let ((window-min-height 1))
3928 ;; Don't throw an error if we can't even window heights for
3929 ;; whatever reason.
3930 (condition-case nil
3931 (enlarge-window (/ (- (window-height window) (window-height)) 2))
3932 (error nil)))))
3934 (defun window--display-buffer-1 (window)
3935 "Raise the frame containing WINDOW.
3936 Do not raise the selected frame. Return WINDOW."
3937 (let* ((frame (window-frame window))
3938 (visible (frame-visible-p frame)))
3939 (unless (or (not visible)
3940 ;; Assume the selected frame is already visible enough.
3941 (eq frame (selected-frame))
3942 ;; Assume the frame from which we invoked the minibuffer
3943 ;; is visible.
3944 (and (minibuffer-window-active-p (selected-window))
3945 (eq frame (window-frame (minibuffer-selected-window)))))
3946 (raise-frame frame))
3947 window))
3949 (defun window--display-buffer-2 (buffer window &optional dedicated)
3950 "Display BUFFER in WINDOW and make its frame visible.
3951 Set `window-dedicated-p' to DEDICATED if non-nil.
3952 Return WINDOW."
3953 (when (and (buffer-live-p buffer) (window-live-p window))
3954 (set-window-buffer window buffer)
3955 (when dedicated
3956 (set-window-dedicated-p window dedicated))
3957 (window--display-buffer-1 window)))
3959 (defvar display-buffer-mark-dedicated nil
3960 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
3961 The actual non-nil value of this variable will be copied to the
3962 `window-dedicated-p' flag.")
3964 (defun display-buffer (buffer-or-name &optional not-this-window frame)
3965 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
3966 BUFFER-OR-NAME must be a buffer or the name of an existing
3967 buffer. Return the window chosen to display BUFFER-OR-NAME or
3968 nil if no such window is found.
3970 Optional argument NOT-THIS-WINDOW non-nil means display the
3971 buffer in a window other than the selected one, even if it is
3972 already displayed in the selected window.
3974 Optional argument FRAME specifies which frames to investigate
3975 when the specified buffer is already displayed. If the buffer is
3976 already displayed in some window on one of these frames simply
3977 return that window. Possible values of FRAME are:
3979 `visible' - consider windows on all visible frames on the current
3980 terminal.
3982 0 - consider windows on all visible or iconified frames on the
3983 current terminal.
3985 t - consider windows on all frames.
3987 A specific frame - consider windows on that frame only.
3989 nil - consider windows on the selected frame \(actually the
3990 last non-minibuffer frame\) only. If, however, either
3991 `display-buffer-reuse-frames' or `pop-up-frames' is non-nil
3992 \(non-nil and not graphic-only on a text-only terminal),
3993 consider all visible or iconified frames on the current terminal."
3994 (interactive "BDisplay buffer:\nP")
3995 (let* ((can-use-selected-window
3996 ;; The selected window is usable unless either NOT-THIS-WINDOW
3997 ;; is non-nil, it is dedicated to its buffer, or it is the
3998 ;; `minibuffer-window'.
3999 (not (or not-this-window
4000 (window-dedicated-p (selected-window))
4001 (window-minibuffer-p))))
4002 (buffer (if (bufferp buffer-or-name)
4003 buffer-or-name
4004 (get-buffer buffer-or-name)))
4005 (name-of-buffer (buffer-name buffer))
4006 ;; On text-only terminals do not pop up a new frame when
4007 ;; `pop-up-frames' equals graphic-only.
4008 (use-pop-up-frames (if (eq pop-up-frames 'graphic-only)
4009 (display-graphic-p)
4010 pop-up-frames))
4011 ;; `frame-to-use' is the frame where to show `buffer' - either
4012 ;; the selected frame or the last nonminibuffer frame.
4013 (frame-to-use
4014 (or (window--frame-usable-p (selected-frame))
4015 (window--frame-usable-p (last-nonminibuffer-frame))))
4016 ;; `window-to-use' is the window we use for showing `buffer'.
4017 window-to-use)
4018 (cond
4019 ((not (buffer-live-p buffer))
4020 (error "No such buffer %s" buffer))
4021 (display-buffer-function
4022 ;; Let `display-buffer-function' do the job.
4023 (funcall display-buffer-function buffer not-this-window))
4024 ((and (not not-this-window)
4025 (eq (window-buffer (selected-window)) buffer))
4026 ;; The selected window already displays BUFFER and
4027 ;; `not-this-window' is nil, so use it.
4028 (window--display-buffer-1 (selected-window)))
4029 ((and can-use-selected-window (same-window-p name-of-buffer))
4030 ;; If the buffer's name tells us to use the selected window do so.
4031 (window--display-buffer-2 buffer (selected-window)))
4032 ((let ((frames (or frame
4033 (and (or use-pop-up-frames
4034 display-buffer-reuse-frames
4035 (not (last-nonminibuffer-frame)))
4037 (last-nonminibuffer-frame))))
4038 (setq window-to-use
4039 (catch 'found
4040 ;; Search frames for a window displaying BUFFER. Return
4041 ;; the selected window only if we are allowed to do so.
4042 (dolist (window (get-buffer-window-list buffer 'nomini frames))
4043 (when (or can-use-selected-window
4044 (not (eq (selected-window) window)))
4045 (throw 'found window))))))
4046 ;; The buffer is already displayed in some window; use that.
4047 (window--display-buffer-1 window-to-use))
4048 ((and special-display-function
4049 ;; `special-display-p' returns either t or a list of frame
4050 ;; parameters to pass to `special-display-function'.
4051 (let ((pars (special-display-p name-of-buffer)))
4052 (when pars
4053 (funcall special-display-function
4054 buffer (if (listp pars) pars))))))
4055 ((or use-pop-up-frames (not frame-to-use))
4056 ;; We want or need a new frame.
4057 (let ((win (frame-selected-window (funcall pop-up-frame-function))))
4058 (window--display-buffer-2 buffer win display-buffer-mark-dedicated)))
4059 ((and pop-up-windows
4060 ;; Make a new window.
4061 (or (not (frame-parameter frame-to-use 'unsplittable))
4062 ;; If the selected frame cannot be split look at
4063 ;; `last-nonminibuffer-frame'.
4064 (and (eq frame-to-use (selected-frame))
4065 (setq frame-to-use (last-nonminibuffer-frame))
4066 (window--frame-usable-p frame-to-use)
4067 (not (frame-parameter frame-to-use 'unsplittable))))
4068 ;; Attempt to split largest or least recently used window.
4069 (setq window-to-use
4070 (or (window--try-to-split-window
4071 (get-largest-window frame-to-use t))
4072 (window--try-to-split-window
4073 (get-lru-window frame-to-use t)))))
4074 (window--display-buffer-2 buffer window-to-use
4075 display-buffer-mark-dedicated))
4076 ((let ((window-to-undedicate
4077 ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate
4078 ;; the selected window to its buffer, to avoid that some of
4079 ;; the `get-' routines below choose it. (Bug#1415)
4080 (and not-this-window (not (window-dedicated-p))
4081 (set-window-dedicated-p (selected-window) t)
4082 (selected-window))))
4083 (unwind-protect
4084 (setq window-to-use
4085 ;; Reuse an existing window.
4086 (or (get-lru-window frame-to-use)
4087 (let ((window (get-buffer-window buffer 'visible)))
4088 (unless (and not-this-window
4089 (eq window (selected-window)))
4090 window))
4091 (get-largest-window 'visible)
4092 (let ((window (get-buffer-window buffer 0)))
4093 (unless (and not-this-window
4094 (eq window (selected-window)))
4095 window))
4096 (get-largest-window 0)
4097 (frame-selected-window (funcall pop-up-frame-function))))
4098 (when (window-live-p window-to-undedicate)
4099 ;; Restore dedicated status of selected window.
4100 (set-window-dedicated-p window-to-undedicate nil))))
4101 (window--even-window-heights window-to-use)
4102 (window--display-buffer-2 buffer window-to-use)))))
4104 (defun display-buffer-other-frame (buffer)
4105 "Display buffer BUFFER in another frame.
4106 This uses the function `display-buffer' as a subroutine; see
4107 its documentation for additional customization information."
4108 (interactive "BDisplay buffer in other frame: ")
4109 (let ((pop-up-frames t)
4110 same-window-buffer-names same-window-regexps
4111 ;;(old-window (selected-window))
4112 new-window)
4113 (setq new-window (display-buffer buffer t))
4114 ;; This may have been here in order to prevent the new frame from hiding
4115 ;; the old frame. But it does more harm than good.
4116 ;; Maybe we should call `raise-window' on the old-frame instead? --Stef
4117 ;;(lower-frame (window-frame new-window))
4119 ;; This may have been here in order to make sure the old-frame gets the
4120 ;; focus. But not only can it cause an annoying flicker, with some
4121 ;; window-managers it just makes the window invisible, with no easy
4122 ;; way to recover it. --Stef
4123 ;;(make-frame-invisible (window-frame old-window))
4124 ;;(make-frame-visible (window-frame old-window))
4127 (defun pop-to-buffer (buffer-or-name &optional other-window norecord)
4128 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
4129 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4130 nil. If BUFFER-OR-NAME is a string not naming an existent
4131 buffer, create a buffer with that name. If BUFFER-OR-NAME is
4132 nil, choose some other buffer.
4134 If `pop-up-windows' is non-nil, windows can be split to display
4135 the buffer. If optional second arg OTHER-WINDOW is non-nil,
4136 insist on finding another window even if the specified buffer is
4137 already visible in the selected window, and ignore
4138 `same-window-regexps' and `same-window-buffer-names'.
4140 If the window to show BUFFER-OR-NAME is not on the selected
4141 frame, raise that window's frame and give it input focus.
4143 This function returns the buffer it switched to. This uses the
4144 function `display-buffer' as a subroutine; see the documentation
4145 of `display-buffer' for additional customization information.
4147 Optional third arg NORECORD non-nil means do not put this buffer
4148 at the front of the list of recently selected ones."
4149 (let ((buffer
4150 ;; FIXME: This behavior is carried over from the previous C version
4151 ;; of pop-to-buffer, but really we should use just
4152 ;; `get-buffer' here.
4153 (if (null buffer-or-name) (other-buffer (current-buffer))
4154 (or (get-buffer buffer-or-name)
4155 (let ((buf (get-buffer-create buffer-or-name)))
4156 (set-buffer-major-mode buf)
4157 buf))))
4158 (old-frame (selected-frame))
4159 new-window new-frame)
4160 (set-buffer buffer)
4161 (setq new-window (display-buffer buffer other-window))
4162 (select-window new-window norecord)
4163 (setq new-frame (window-frame new-window))
4164 (unless (eq new-frame old-frame)
4165 ;; `display-buffer' has chosen another frame, make sure it gets
4166 ;; input focus and is risen.
4167 (select-frame-set-input-focus new-frame))
4168 buffer))
4170 (defun read-buffer-to-switch (prompt)
4171 "Read the name of a buffer to switch to, prompting with PROMPT.
4172 Return the neame of the buffer as a string.
4174 This function is intended for the `switch-to-buffer' family of
4175 commands since these need to omit the name of the current buffer
4176 from the list of completions and default values."
4177 (let ((rbts-completion-table (internal-complete-buffer-except)))
4178 (minibuffer-with-setup-hook
4179 (lambda ()
4180 (setq minibuffer-completion-table rbts-completion-table)
4181 ;; Since rbts-completion-table is built dynamically, we
4182 ;; can't just add it to the default value of
4183 ;; icomplete-with-completion-tables, so we add it
4184 ;; here manually.
4185 (if (and (boundp 'icomplete-with-completion-tables)
4186 (listp icomplete-with-completion-tables))
4187 (set (make-local-variable 'icomplete-with-completion-tables)
4188 (cons rbts-completion-table
4189 icomplete-with-completion-tables))))
4190 (read-buffer prompt (other-buffer (current-buffer))
4191 (confirm-nonexistent-file-or-buffer)))))
4193 (defun normalize-buffer-to-switch-to (buffer-or-name)
4194 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
4195 If BUFFER-OR-NAME is nil, return the buffer returned by
4196 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
4197 exists, return that buffer. If no such buffer exists, create a
4198 buffer with the name BUFFER-OR-NAME and return that buffer."
4199 (if buffer-or-name
4200 (or (get-buffer buffer-or-name)
4201 (let ((buffer (get-buffer-create buffer-or-name)))
4202 (set-buffer-major-mode buffer)
4203 buffer))
4204 (other-buffer)))
4206 (defun switch-to-buffer (buffer-or-name &optional norecord)
4207 "Switch to buffer BUFFER-OR-NAME in the selected window.
4208 If called interactively, prompt for the buffer name using the
4209 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4210 determines whether to request confirmation before creating a new
4211 buffer.
4213 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4214 nil. If BUFFER-OR-NAME is a string that does not identify an
4215 existing buffer, create a buffer with that name. If
4216 BUFFER-OR-NAME is nil, switch to the buffer returned by
4217 `other-buffer'.
4219 Optional argument NORECORD non-nil means do not put the buffer
4220 specified by BUFFER-OR-NAME at the front of the buffer list and
4221 do not make the window displaying it the most recently selected
4222 one. Return the buffer switched to.
4224 This function is intended for interactive use only. Lisp
4225 functions should call `pop-to-buffer-same-window' instead."
4226 (interactive
4227 (list (read-buffer-to-switch "Switch to buffer: ")))
4228 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
4229 (if (and (or (window-minibuffer-p) (eq (window-dedicated-p) t))
4230 (not (eq buffer (window-buffer))))
4231 ;; Cannot switch to another buffer in a minibuffer or strongly
4232 ;; dedicated window that does not show the buffer already. Call
4233 ;; `pop-to-buffer' instead.
4234 (pop-to-buffer buffer nil norecord)
4235 (unless (eq buffer (window-buffer))
4236 ;; I'm not sure why we should NOT call `set-window-buffer' here,
4237 ;; but let's keep things as they are (otherwise we could always
4238 ;; call `pop-to-buffer-same-window' here).
4239 (set-window-buffer nil buffer))
4240 (unless norecord
4241 (select-window (selected-window)))
4242 (set-buffer buffer))))
4244 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
4245 "Select the buffer specified by BUFFER-OR-NAME in another window.
4246 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4247 nil. Return the buffer switched to.
4249 If called interactively, prompt for the buffer name using the
4250 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4251 determines whether to request confirmation before creating a new
4252 buffer.
4254 If BUFFER-OR-NAME is a string and does not identify an existing
4255 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
4256 nil, switch to the buffer returned by `other-buffer'.
4258 Optional second argument NORECORD non-nil means do not put this
4259 buffer at the front of the list of recently selected ones.
4261 This uses the function `display-buffer' as a subroutine; see its
4262 documentation for additional customization information."
4263 (interactive
4264 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
4265 (let ((pop-up-windows t)
4266 same-window-buffer-names same-window-regexps)
4267 (pop-to-buffer buffer-or-name t norecord)))
4269 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
4270 "Switch to buffer BUFFER-OR-NAME in another frame.
4271 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4272 nil. Return the buffer switched to.
4274 If called interactively, prompt for the buffer name using the
4275 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4276 determines whether to request confirmation before creating a new
4277 buffer.
4279 If BUFFER-OR-NAME is a string and does not identify an existing
4280 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
4281 nil, switch to the buffer returned by `other-buffer'.
4283 Optional second arg NORECORD non-nil means do not put this
4284 buffer at the front of the list of recently selected ones.
4286 This uses the function `display-buffer' as a subroutine; see its
4287 documentation for additional customization information."
4288 (interactive
4289 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
4290 (let ((pop-up-frames t)
4291 same-window-buffer-names same-window-regexps)
4292 (pop-to-buffer buffer-or-name t norecord)))
4294 (defun set-window-text-height (window height)
4295 "Set the height in lines of the text display area of WINDOW to HEIGHT.
4296 WINDOW must be a live window. HEIGHT doesn't include the mode
4297 line or header line, if any, or any partial-height lines in the
4298 text display area.
4300 Note that the current implementation of this function cannot
4301 always set the height exactly, but attempts to be conservative,
4302 by allocating more lines than are actually needed in the case
4303 where some error may be present."
4304 (setq window (normalize-live-window window))
4305 (let ((delta (- height (window-text-height window))))
4306 (unless (zerop delta)
4307 ;; Setting window-min-height to a value like 1 can lead to very
4308 ;; bizarre displays because it also allows Emacs to make *other*
4309 ;; windows 1-line tall, which means that there's no more space for
4310 ;; the modeline.
4311 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
4312 (resize-window window delta)))))
4314 (defun enlarge-window-horizontally (delta)
4315 "Make selected window DELTA columns wider.
4316 Interactively, if no argument is given, make selected window one
4317 column wider."
4318 (interactive "p")
4319 (enlarge-window delta t))
4321 (defun shrink-window-horizontally (delta)
4322 "Make selected window DELTA columns narrower.
4323 Interactively, if no argument is given, make selected window one
4324 column narrower."
4325 (interactive "p")
4326 (shrink-window delta t))
4328 (defun count-screen-lines (&optional beg end count-final-newline window)
4329 "Return the number of screen lines in the region.
4330 The number of screen lines may be different from the number of actual lines,
4331 due to line breaking, display table, etc.
4333 Optional arguments BEG and END default to `point-min' and `point-max'
4334 respectively.
4336 If region ends with a newline, ignore it unless optional third argument
4337 COUNT-FINAL-NEWLINE is non-nil.
4339 The optional fourth argument WINDOW specifies the window used for obtaining
4340 parameters such as width, horizontal scrolling, and so on. The default is
4341 to use the selected window's parameters.
4343 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
4344 regardless of which buffer is displayed in WINDOW. This makes possible to use
4345 `count-screen-lines' in any buffer, whether or not it is currently displayed
4346 in some window."
4347 (unless beg
4348 (setq beg (point-min)))
4349 (unless end
4350 (setq end (point-max)))
4351 (if (= beg end)
4353 (save-excursion
4354 (save-restriction
4355 (widen)
4356 (narrow-to-region (min beg end)
4357 (if (and (not count-final-newline)
4358 (= ?\n (char-before (max beg end))))
4359 (1- (max beg end))
4360 (max beg end)))
4361 (goto-char (point-min))
4362 (1+ (vertical-motion (buffer-size) window))))))
4364 (defun window-buffer-height (window)
4365 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
4366 (with-current-buffer (window-buffer window)
4367 (max 1
4368 (count-screen-lines (point-min) (point-max)
4369 ;; If buffer ends with a newline, ignore it when
4370 ;; counting height unless point is after it.
4371 (eobp)
4372 window))))
4374 ;;; Resizing buffers to fit their contents exactly.
4375 (defun fit-window-to-buffer (&optional window max-height min-height override)
4376 "Adjust height of WINDOW to display its buffer's contents exactly.
4377 WINDOW can be any live window and defaults to the selected one.
4379 Optional argument MAX-HEIGHT specifies the maximum height of
4380 WINDOW and defaults to the height of WINDOW's frame. Optional
4381 argument MIN-HEIGHT specifies the minimum height of WINDOW and
4382 defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
4383 are specified in lines and include the mode line and header line,
4384 if any.
4386 Optional argument OVERRIDE non-nil means override restrictions
4387 imposed by `window-min-height' and `window-min-width' on the size
4388 of WINDOW.
4390 Return the number of lines by which WINDOW was enlarged or
4391 shrunk. If an error occurs during resizing, return nil but don't
4392 signal an error.
4394 Note that even if this function makes WINDOW large enough to show
4395 _all_ lines of its buffer you might not see the first lines when
4396 WINDOW was scrolled."
4397 (interactive)
4398 ;; Do all the work in WINDOW and its buffer and restore the selected
4399 ;; window and the current buffer when we're done.
4400 (setq window (normalize-live-window window))
4401 ;; Can't resize a full height or fixed-size window.
4402 (unless (or (window-size-fixed-p window)
4403 (window-full-height-p window))
4404 ;; `with-selected-window' should orderly restore the current buffer.
4405 (with-selected-window window
4406 ;; We are in WINDOW's buffer now.
4407 (let* ( ;; Adjust MIN-HEIGHT.
4408 (min-height
4409 (if override
4410 (window-min-size window nil window)
4411 (max (or min-height window-min-height)
4412 window-safe-min-height)))
4413 (max-window-height
4414 (window-total-size (frame-root-window window)))
4415 ;; Adjust MAX-HEIGHT.
4416 (max-height
4417 (if (or override (not max-height))
4418 max-window-height
4419 (min max-height max-window-height)))
4420 ;; Make `desired-height' the height necessary to show
4421 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
4422 ;; and MAX-HEIGHT.
4423 (desired-height
4424 (max
4425 (min
4426 (+ (count-screen-lines)
4427 ;; For non-minibuffers count the mode line, if any.
4428 (if (and (not (window-minibuffer-p window))
4429 mode-line-format)
4432 ;; Count the header line, if any.
4433 (if header-line-format 1 0))
4434 max-height)
4435 min-height))
4436 (desired-delta
4437 (- desired-height (window-total-size window)))
4438 (delta
4439 (if (> desired-delta 0)
4440 (min desired-delta
4441 (window-max-delta window nil window))
4442 (max desired-delta
4443 (- (window-min-delta window nil window))))))
4444 ;; This `condition-case' shouldn't be necessary, but who knows?
4445 (condition-case nil
4446 (if (zerop delta)
4447 ;; Return zero if DELTA became zero in the proces.
4449 ;; Don't try to redisplay with the cursor at the end on its
4450 ;; own line--that would force a scroll and spoil things.
4451 (when (and (eobp) (bolp) (not (bobp)))
4452 ;; It's silly to put `point' at the end of the previous
4453 ;; line and so maybe force horizontal scrolling.
4454 (set-window-point window (line-beginning-position 0)))
4455 ;; Call `resize-window' with OVERRIDE argument equal WINDOW.
4456 (resize-window window delta nil window)
4457 ;; Check if the last line is surely fully visible. If
4458 ;; not, enlarge the window.
4459 (let ((end (save-excursion
4460 (goto-char (point-max))
4461 (when (and (bolp) (not (bobp)))
4462 ;; Don't include final newline.
4463 (backward-char 1))
4464 (when truncate-lines
4465 ;; If line-wrapping is turned off, test the
4466 ;; beginning of the last line for
4467 ;; visibility instead of the end, as the
4468 ;; end of the line could be invisible by
4469 ;; virtue of extending past the edge of the
4470 ;; window.
4471 (forward-line 0))
4472 (point))))
4473 (set-window-vscroll window 0)
4474 ;; This loop might in some rare pathological cases raise
4475 ;; an error - another reason for the `condition-case'.
4476 (while (and (< desired-height max-height)
4477 (= desired-height (window-total-size))
4478 (not (pos-visible-in-window-p end)))
4479 (resize-window window 1 nil window)
4480 (setq desired-height (1+ desired-height)))))
4481 (error (setq delta nil)))
4482 delta))))
4484 (defun window-safely-shrinkable-p (&optional window)
4485 "Return t if WINDOW can be shrunk without shrinking other windows.
4486 WINDOW defaults to the selected window."
4487 (with-selected-window (or window (selected-window))
4488 (let ((edges (window-edges)))
4489 ;; The following doesn't satisfy the doc-string's claim when
4490 ;; window and previous-/next-window are not part of the same
4491 ;; combination but still share a common edge. Using
4492 ;; `window-iso-combined-p' instead should handle that.
4493 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
4494 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
4495 (make-obsolete
4496 'window-safely-shrinkable-p "use `window-iso-combined-p' instead." "24.1")
4498 (defun shrink-window-if-larger-than-buffer (&optional window)
4499 "Shrink height of WINDOW if its buffer doesn't need so many lines.
4500 More precisely, shrink WINDOW vertically to be as small as
4501 possible, while still showing the full contents of its buffer.
4502 WINDOW defaults to the selected window.
4504 Do not shrink WINDOW to less than `window-min-height' lines. Do
4505 nothing if the buffer contains more lines than the present window
4506 height, or if some of the window's contents are scrolled out of
4507 view, or if shrinking this window would also shrink another
4508 window, or if the window is the only window of its frame.
4510 Return non-nil if the window was shrunk, nil otherwise."
4511 (interactive)
4512 (setq window (normalize-live-window window))
4513 ;; Make sure that WINDOW is vertically combined and `point-min' is
4514 ;; visible (for whatever reason that's needed). The remaining issues
4515 ;; should be taken care of by `fit-window-to-buffer'.
4516 (when (and (window-iso-combined-p window)
4517 (pos-visible-in-window-p (point-min) window))
4518 (fit-window-to-buffer window (window-total-size window))))
4520 (defun kill-buffer-and-window ()
4521 "Kill the current buffer and delete the selected window."
4522 (interactive)
4523 (let ((window-to-delete (selected-window))
4524 (buffer-to-kill (current-buffer))
4525 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
4526 (unwind-protect
4527 (progn
4528 (add-hook 'kill-buffer-hook delete-window-hook t t)
4529 (if (kill-buffer (current-buffer))
4530 ;; If `delete-window' failed before, we rerun it to regenerate
4531 ;; the error so it can be seen in the echo area.
4532 (when (eq (selected-window) window-to-delete)
4533 (delete-window))))
4534 ;; If the buffer is not dead for some reason (probably because
4535 ;; of a `quit' signal), remove the hook again.
4536 (ignore-errors
4537 (with-current-buffer buffer-to-kill
4538 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
4540 (defun quit-window (&optional kill window)
4541 "Quit WINDOW and bury its buffer.
4542 With a prefix argument, kill the buffer instead. WINDOW defaults
4543 to the selected window.
4545 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
4546 it and, if it's alone on its frame, its frame too. Otherwise, or
4547 if deleting WINDOW fails in any of the preceding cases, display
4548 another buffer in WINDOW using `switch-to-buffer'.
4550 Optional argument KILL non-nil means kill WINDOW's buffer.
4551 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
4552 (interactive "P")
4553 (let ((buffer (window-buffer window)))
4554 (if (or window
4555 (window-minibuffer-p window)
4556 (window-dedicated-p window))
4557 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
4558 ;; try to delete it.
4559 (let* ((window (or window (selected-window)))
4560 (frame (window-frame window)))
4561 (if (frame-root-window-p window)
4562 ;; WINDOW is alone on its frame.
4563 (delete-frame frame)
4564 ;; There are other windows on its frame, delete WINDOW.
4565 (delete-window window)))
4566 ;; Otherwise, switch to another buffer in the selected window.
4567 (switch-to-buffer nil))
4569 ;; Deal with the buffer.
4570 (if kill
4571 (kill-buffer buffer)
4572 (bury-buffer buffer))))
4574 (defvar recenter-last-op nil
4575 "Indicates the last recenter operation performed.
4576 Possible values: `top', `middle', `bottom', integer or float numbers.")
4578 (defcustom recenter-positions '(middle top bottom)
4579 "Cycling order for `recenter-top-bottom'.
4580 A list of elements with possible values `top', `middle', `bottom',
4581 integer or float numbers that define the cycling order for
4582 the command `recenter-top-bottom'.
4584 Top and bottom destinations are `scroll-margin' lines the from true
4585 window top and bottom. Middle redraws the frame and centers point
4586 vertically within the window. Integer number moves current line to
4587 the specified absolute window-line. Float number between 0.0 and 1.0
4588 means the percentage of the screen space from the top. The default
4589 cycling order is middle -> top -> bottom."
4590 :type '(repeat (choice
4591 (const :tag "Top" top)
4592 (const :tag "Middle" middle)
4593 (const :tag "Bottom" bottom)
4594 (integer :tag "Line number")
4595 (float :tag "Percentage")))
4596 :version "23.2"
4597 :group 'windows)
4599 (defun recenter-top-bottom (&optional arg)
4600 "Move current buffer line to the specified window line.
4601 With no prefix argument, successive calls place point according
4602 to the cycling order defined by `recenter-positions'.
4604 A prefix argument is handled like `recenter':
4605 With numeric prefix ARG, move current line to window-line ARG.
4606 With plain `C-u', move current line to window center."
4607 (interactive "P")
4608 (cond
4609 (arg (recenter arg)) ; Always respect ARG.
4611 (setq recenter-last-op
4612 (if (eq this-command last-command)
4613 (car (or (cdr (member recenter-last-op recenter-positions))
4614 recenter-positions))
4615 (car recenter-positions)))
4616 (let ((this-scroll-margin
4617 (min (max 0 scroll-margin)
4618 (truncate (/ (window-body-height) 4.0)))))
4619 (cond ((eq recenter-last-op 'middle)
4620 (recenter))
4621 ((eq recenter-last-op 'top)
4622 (recenter this-scroll-margin))
4623 ((eq recenter-last-op 'bottom)
4624 (recenter (- -1 this-scroll-margin)))
4625 ((integerp recenter-last-op)
4626 (recenter recenter-last-op))
4627 ((floatp recenter-last-op)
4628 (recenter (round (* recenter-last-op (window-height))))))))))
4630 (define-key global-map [?\C-l] 'recenter-top-bottom)
4632 (defun move-to-window-line-top-bottom (&optional arg)
4633 "Position point relative to window.
4635 With a prefix argument ARG, acts like `move-to-window-line'.
4637 With no argument, positions point at center of window.
4638 Successive calls position point at positions defined
4639 by `recenter-positions'."
4640 (interactive "P")
4641 (cond
4642 (arg (move-to-window-line arg)) ; Always respect ARG.
4644 (setq recenter-last-op
4645 (if (eq this-command last-command)
4646 (car (or (cdr (member recenter-last-op recenter-positions))
4647 recenter-positions))
4648 (car recenter-positions)))
4649 (let ((this-scroll-margin
4650 (min (max 0 scroll-margin)
4651 (truncate (/ (window-body-height) 4.0)))))
4652 (cond ((eq recenter-last-op 'middle)
4653 (call-interactively 'move-to-window-line))
4654 ((eq recenter-last-op 'top)
4655 (move-to-window-line this-scroll-margin))
4656 ((eq recenter-last-op 'bottom)
4657 (move-to-window-line (- -1 this-scroll-margin)))
4658 ((integerp recenter-last-op)
4659 (move-to-window-line recenter-last-op))
4660 ((floatp recenter-last-op)
4661 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
4663 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
4665 ;;; Scrolling commands.
4667 ;;; Scrolling commands which does not signal errors at top/bottom
4668 ;;; of buffer at first key-press (instead moves to top/bottom
4669 ;;; of buffer).
4671 (defcustom scroll-error-top-bottom nil
4672 "Move point to top/bottom of buffer before signalling a scrolling error.
4673 A value of nil means just signal an error if no more scrolling possible.
4674 A value of t means point moves to the beginning or the end of the buffer
4675 \(depending on scrolling direction) when no more scrolling possible.
4676 When point is already on that position, then signal an error."
4677 :type 'boolean
4678 :group 'scrolling
4679 :version "24.1")
4681 (defun scroll-up-command (&optional arg)
4682 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
4683 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
4684 scroll window further, move cursor to the bottom line.
4685 When point is already on that position, then signal an error.
4686 A near full screen is `next-screen-context-lines' less than a full screen.
4687 Negative ARG means scroll downward.
4688 If ARG is the atom `-', scroll downward by nearly full screen."
4689 (interactive "^P")
4690 (cond
4691 ((null scroll-error-top-bottom)
4692 (scroll-up arg))
4693 ((eq arg '-)
4694 (scroll-down-command nil))
4695 ((< (prefix-numeric-value arg) 0)
4696 (scroll-down-command (- (prefix-numeric-value arg))))
4697 ((eobp)
4698 (scroll-up arg)) ; signal error
4700 (condition-case nil
4701 (scroll-up arg)
4702 (end-of-buffer
4703 (if arg
4704 ;; When scrolling by ARG lines can't be done,
4705 ;; move by ARG lines instead.
4706 (forward-line arg)
4707 ;; When ARG is nil for full-screen scrolling,
4708 ;; move to the bottom of the buffer.
4709 (goto-char (point-max))))))))
4711 (put 'scroll-up-command 'scroll-command t)
4713 (defun scroll-down-command (&optional arg)
4714 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
4715 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
4716 scroll window further, move cursor to the top line.
4717 When point is already on that position, then signal an error.
4718 A near full screen is `next-screen-context-lines' less than a full screen.
4719 Negative ARG means scroll upward.
4720 If ARG is the atom `-', scroll upward by nearly full screen."
4721 (interactive "^P")
4722 (cond
4723 ((null scroll-error-top-bottom)
4724 (scroll-down arg))
4725 ((eq arg '-)
4726 (scroll-up-command nil))
4727 ((< (prefix-numeric-value arg) 0)
4728 (scroll-up-command (- (prefix-numeric-value arg))))
4729 ((bobp)
4730 (scroll-down arg)) ; signal error
4732 (condition-case nil
4733 (scroll-down arg)
4734 (beginning-of-buffer
4735 (if arg
4736 ;; When scrolling by ARG lines can't be done,
4737 ;; move by ARG lines instead.
4738 (forward-line (- arg))
4739 ;; When ARG is nil for full-screen scrolling,
4740 ;; move to the top of the buffer.
4741 (goto-char (point-min))))))))
4743 (put 'scroll-down-command 'scroll-command t)
4745 ;;; Scrolling commands which scroll a line instead of full screen.
4747 (defun scroll-up-line (&optional arg)
4748 "Scroll text of selected window upward ARG lines; or one line if no ARG.
4749 If ARG is omitted or nil, scroll upward by one line.
4750 This is different from `scroll-up-command' that scrolls a full screen."
4751 (interactive "p")
4752 (scroll-up (or arg 1)))
4754 (put 'scroll-up-line 'scroll-command t)
4756 (defun scroll-down-line (&optional arg)
4757 "Scroll text of selected window down ARG lines; or one line if no ARG.
4758 If ARG is omitted or nil, scroll down by one line.
4759 This is different from `scroll-down-command' that scrolls a full screen."
4760 (interactive "p")
4761 (scroll-down (or arg 1)))
4763 (put 'scroll-down-line 'scroll-command t)
4766 (defun scroll-other-window-down (lines)
4767 "Scroll the \"other window\" down.
4768 For more details, see the documentation for `scroll-other-window'."
4769 (interactive "P")
4770 (scroll-other-window
4771 ;; Just invert the argument's meaning.
4772 ;; We can do that without knowing which window it will be.
4773 (if (eq lines '-) nil
4774 (if (null lines) '-
4775 (- (prefix-numeric-value lines))))))
4777 (defun beginning-of-buffer-other-window (arg)
4778 "Move point to the beginning of the buffer in the other window.
4779 Leave mark at previous position.
4780 With arg N, put point N/10 of the way from the true beginning."
4781 (interactive "P")
4782 (let ((orig-window (selected-window))
4783 (window (other-window-for-scrolling)))
4784 ;; We use unwind-protect rather than save-window-excursion
4785 ;; because the latter would preserve the things we want to change.
4786 (unwind-protect
4787 (progn
4788 (select-window window)
4789 ;; Set point and mark in that window's buffer.
4790 (with-no-warnings
4791 (beginning-of-buffer arg))
4792 ;; Set point accordingly.
4793 (recenter '(t)))
4794 (select-window orig-window))))
4796 (defun end-of-buffer-other-window (arg)
4797 "Move point to the end of the buffer in the other window.
4798 Leave mark at previous position.
4799 With arg N, put point N/10 of the way from the true end."
4800 (interactive "P")
4801 ;; See beginning-of-buffer-other-window for comments.
4802 (let ((orig-window (selected-window))
4803 (window (other-window-for-scrolling)))
4804 (unwind-protect
4805 (progn
4806 (select-window window)
4807 (with-no-warnings
4808 (end-of-buffer arg))
4809 (recenter '(t)))
4810 (select-window orig-window))))
4812 (defvar mouse-autoselect-window-timer nil
4813 "Timer used by delayed window autoselection.")
4815 (defvar mouse-autoselect-window-position nil
4816 "Last mouse position recorded by delayed window autoselection.")
4818 (defvar mouse-autoselect-window-window nil
4819 "Last window recorded by delayed window autoselection.")
4821 (defvar mouse-autoselect-window-state nil
4822 "When non-nil, special state of delayed window autoselection.
4823 Possible values are `suspend' \(suspend autoselection after a menu or
4824 scrollbar interaction\) and `select' \(the next invocation of
4825 'handle-select-window' shall select the window immediately\).")
4827 (defun mouse-autoselect-window-cancel (&optional force)
4828 "Cancel delayed window autoselection.
4829 Optional argument FORCE means cancel unconditionally."
4830 (unless (and (not force)
4831 ;; Don't cancel for select-window or select-frame events
4832 ;; or when the user drags a scroll bar.
4833 (or (memq this-command
4834 '(handle-select-window handle-switch-frame))
4835 (and (eq this-command 'scroll-bar-toolkit-scroll)
4836 (memq (nth 4 (event-end last-input-event))
4837 '(handle end-scroll)))))
4838 (setq mouse-autoselect-window-state nil)
4839 (when (timerp mouse-autoselect-window-timer)
4840 (cancel-timer mouse-autoselect-window-timer))
4841 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
4843 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
4844 "Start delayed window autoselection.
4845 MOUSE-POSITION is the last position where the mouse was seen as returned
4846 by `mouse-position'. Optional argument WINDOW non-nil denotes the
4847 window where the mouse was seen. Optional argument SUSPEND non-nil
4848 means suspend autoselection."
4849 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
4850 (setq mouse-autoselect-window-position mouse-position)
4851 (when window (setq mouse-autoselect-window-window window))
4852 (setq mouse-autoselect-window-state (when suspend 'suspend))
4853 ;; Install timer which runs `mouse-autoselect-window-select' after
4854 ;; `mouse-autoselect-window' seconds.
4855 (setq mouse-autoselect-window-timer
4856 (run-at-time
4857 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
4859 (defun mouse-autoselect-window-select ()
4860 "Select window with delayed window autoselection.
4861 If the mouse position has stabilized in a non-selected window, select
4862 that window. The minibuffer window is selected only if the minibuffer is
4863 active. This function is run by `mouse-autoselect-window-timer'."
4864 (ignore-errors
4865 (let* ((mouse-position (mouse-position))
4866 (window
4867 (ignore-errors
4868 (window-at (cadr mouse-position) (cddr mouse-position)
4869 (car mouse-position)))))
4870 (cond
4871 ((or (menu-or-popup-active-p)
4872 (and window
4873 (not (coordinates-in-window-p (cdr mouse-position) window))))
4874 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
4875 ;; of WINDOW, temporarily suspend delayed autoselection.
4876 (mouse-autoselect-window-start mouse-position nil t))
4877 ((eq mouse-autoselect-window-state 'suspend)
4878 ;; Delayed autoselection was temporarily suspended, reenable it.
4879 (mouse-autoselect-window-start mouse-position))
4880 ((and window (not (eq window (selected-window)))
4881 (or (not (numberp mouse-autoselect-window))
4882 (and (> mouse-autoselect-window 0)
4883 ;; If `mouse-autoselect-window' is positive, select
4884 ;; window if the window is the same as before.
4885 (eq window mouse-autoselect-window-window))
4886 ;; Otherwise select window if the mouse is at the same
4887 ;; position as before. Observe that the first test after
4888 ;; starting autoselection usually fails since the value of
4889 ;; `mouse-autoselect-window-position' recorded there is the
4890 ;; position where the mouse has entered the new window and
4891 ;; not necessarily where the mouse has stopped moving.
4892 (equal mouse-position mouse-autoselect-window-position))
4893 ;; The minibuffer is a candidate window if it's active.
4894 (or (not (window-minibuffer-p window))
4895 (eq window (active-minibuffer-window))))
4896 ;; Mouse position has stabilized in non-selected window: Cancel
4897 ;; delayed autoselection and try to select that window.
4898 (mouse-autoselect-window-cancel t)
4899 ;; Select window where mouse appears unless the selected window is the
4900 ;; minibuffer. Use `unread-command-events' in order to execute pre-
4901 ;; and post-command hooks and trigger idle timers. To avoid delaying
4902 ;; autoselection again, set `mouse-autoselect-window-state'."
4903 (unless (window-minibuffer-p (selected-window))
4904 (setq mouse-autoselect-window-state 'select)
4905 (setq unread-command-events
4906 (cons (list 'select-window (list window))
4907 unread-command-events))))
4908 ((or (and window (eq window (selected-window)))
4909 (not (numberp mouse-autoselect-window))
4910 (equal mouse-position mouse-autoselect-window-position))
4911 ;; Mouse position has either stabilized in the selected window or at
4912 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
4913 (mouse-autoselect-window-cancel t))
4915 ;; Mouse position has not stabilized yet, resume delayed
4916 ;; autoselection.
4917 (mouse-autoselect-window-start mouse-position window))))))
4919 (defun handle-select-window (event)
4920 "Handle select-window events."
4921 (interactive "e")
4922 (let ((window (posn-window (event-start event))))
4923 (unless (or (not (window-live-p window))
4924 ;; Don't switch if we're currently in the minibuffer.
4925 ;; This tries to work around problems where the
4926 ;; minibuffer gets unselected unexpectedly, and where
4927 ;; you then have to move your mouse all the way down to
4928 ;; the minibuffer to select it.
4929 (window-minibuffer-p (selected-window))
4930 ;; Don't switch to minibuffer window unless it's active.
4931 (and (window-minibuffer-p window)
4932 (not (minibuffer-window-active-p window)))
4933 ;; Don't switch when autoselection shall be delayed.
4934 (and (numberp mouse-autoselect-window)
4935 (not (zerop mouse-autoselect-window))
4936 (not (eq mouse-autoselect-window-state 'select))
4937 (progn
4938 ;; Cancel any delayed autoselection.
4939 (mouse-autoselect-window-cancel t)
4940 ;; Start delayed autoselection from current mouse
4941 ;; position and window.
4942 (mouse-autoselect-window-start (mouse-position) window)
4943 ;; Executing a command cancels delayed autoselection.
4944 (add-hook
4945 'pre-command-hook 'mouse-autoselect-window-cancel))))
4946 (when mouse-autoselect-window
4947 ;; Reset state of delayed autoselection.
4948 (setq mouse-autoselect-window-state nil)
4949 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
4950 (run-hooks 'mouse-leave-buffer-hook))
4951 (select-window window))))
4953 (defun truncated-partial-width-window-p (&optional window)
4954 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
4955 WINDOW defaults to the selected window.
4956 Return nil if WINDOW is not a partial-width window
4957 (regardless of the value of `truncate-lines').
4958 Otherwise, consult the value of `truncate-partial-width-windows'
4959 for the buffer shown in WINDOW."
4960 (unless window
4961 (setq window (selected-window)))
4962 (unless (window-full-width-p window)
4963 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
4964 (window-buffer window))))
4965 (if (integerp t-p-w-w)
4966 (< (window-width window) t-p-w-w)
4967 t-p-w-w))))
4969 (define-key ctl-x-map "0" 'delete-window)
4970 (define-key ctl-x-map "1" 'delete-other-windows)
4971 (define-key ctl-x-map "2" 'split-window-above-each-other)
4972 (define-key ctl-x-map "3" 'split-window-side-by-side)
4973 (define-key ctl-x-map "o" 'other-window)
4974 (define-key ctl-x-map "^" 'enlarge-window)
4975 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
4976 (define-key ctl-x-map "{" 'shrink-window-horizontally)
4977 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
4978 (define-key ctl-x-map "+" 'balance-windows)
4979 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
4981 ;;; window.el ends here