window.el 315 KB
Newer Older
1 2
;;; window.el --- GNU Emacs window commands aside from those written in C

3 4
;; Copyright (C) 1985, 1989, 1992-1994, 2000-2014 Free Software
;; Foundation, Inc.
5

6
;; Maintainer: emacs-devel@gnu.org
7
;; Keywords: internal
8
;; Package: emacs
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Window tree functions.

;;; Code:

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
(defun internal--before-save-selected-window ()
  (cons (selected-window)
        ;; We save and restore all frames' selected windows, because
        ;; `select-window' can change the frame-selected-window of
        ;; whatever frame that window is in.  Each text terminal's
        ;; top-frame is preserved by putting it last in the list.
        (apply #'append
               (mapcar (lambda (terminal)
                         (let ((frames (frames-on-display-list terminal))
                               (top-frame (tty-top-frame terminal))
                               alist)
                           (if top-frame
                               (setq frames
                                     (cons top-frame
                                           (delq top-frame frames))))
                           (dolist (f frames)
                             (push (cons f (frame-selected-window f))
                                   alist))
                           alist))
                       (terminal-list)))))

(defun internal--after-save-selected-window (state)
  (dolist (elt (cdr state))
    (and (frame-live-p (car elt))
         (window-live-p (cdr elt))
         (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
  (when (window-live-p (car state))
    (select-window (car state) 'norecord)))

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
(defmacro save-selected-window (&rest body)
  "Execute BODY, then select the previously selected window.
The value returned is the value of the last form in BODY.

This macro saves and restores the selected window, as well as the
selected window in each frame.  If the previously selected window
is no longer live, then whatever window is selected at the end of
BODY remains selected.  If the previously selected window of some
frame is no longer live at the end of BODY, that frame's selected
window is left alone.

This macro saves and restores the current buffer, since otherwise
its normal operation could make a different buffer current.  The
order of recently selected windows and the buffer list ordering
are not altered by this macro (unless they are altered in BODY)."
Stefan Monnier's avatar
Stefan Monnier committed
75
  (declare (indent 0) (debug t))
76
  `(let ((save-selected-window--state (internal--before-save-selected-window)))
77 78 79
     (save-current-buffer
       (unwind-protect
	   (progn ,@body)
80
         (internal--after-save-selected-window save-selected-window--state)))))
81

82 83 84 85 86 87 88 89 90 91 92
(defvar temp-buffer-window-setup-hook nil
  "Normal hook run by `with-temp-buffer-window' before buffer display.
This hook is run by `with-temp-buffer-window' with the buffer to be
displayed current.")

(defvar temp-buffer-window-show-hook nil
  "Normal hook run by `with-temp-buffer-window' after buffer display.
This hook is run by `with-temp-buffer-window' with the buffer
displayed and current and its window selected.")

(defun temp-buffer-window-setup (buffer-or-name)
93
  "Set up temporary buffer specified by BUFFER-OR-NAME.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
Return the buffer."
  (let ((old-dir default-directory)
	(buffer (get-buffer-create buffer-or-name)))
    (with-current-buffer buffer
      (kill-all-local-variables)
      (setq default-directory old-dir)
      (delete-all-overlays)
      (setq buffer-read-only nil)
      (setq buffer-file-name nil)
      (setq buffer-undo-list t)
      (let ((inhibit-read-only t)
	    (inhibit-modification-hooks t))
	(erase-buffer)
	(run-hooks 'temp-buffer-window-setup-hook))
      ;; Return the buffer.
      buffer)))

111
(defun temp-buffer-window-show (buffer &optional action)
112 113 114 115 116 117 118 119
  "Show temporary buffer BUFFER in a window.
Return the window showing BUFFER.  Pass ACTION as action argument
to `display-buffer'."
  (let (window frame)
    (with-current-buffer buffer
      (set-buffer-modified-p nil)
      (setq buffer-read-only t)
      (goto-char (point-min))
120 121 122 123 124 125 126 127 128 129 130 131 132
      (when (let ((window-combination-limit
		   ;; When `window-combination-limit' equals
		   ;; `temp-buffer' or `temp-buffer-resize' and
		   ;; `temp-buffer-resize-mode' is enabled in this
		   ;; buffer bind it to t so resizing steals space
		   ;; preferably from the window that was split.
		   (if (or (eq window-combination-limit 'temp-buffer)
			   (and (eq window-combination-limit
				    'temp-buffer-resize)
				temp-buffer-resize-mode))
		       t
		     window-combination-limit)))
	      (setq window (display-buffer buffer action)))
133 134 135 136 137 138 139 140 141 142 143 144 145
	(setq frame (window-frame window))
	(unless (eq frame (selected-frame))
	  (raise-frame frame))
	(setq minibuffer-scroll-window window)
	(set-window-hscroll window 0)
	(with-selected-window window
	  (run-hooks 'temp-buffer-window-show-hook)
	  (when temp-buffer-resize-mode
	    (resize-temp-buffer-window window)))
	;; Return the window.
	window))))

(defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
146
  "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
147 148 149 150
BUFFER-OR-NAME must specify either a live buffer, or the name of
a buffer (if it does not exist, this macro creates it).

Make the buffer specified by BUFFER-OR-NAME empty before running
151 152 153 154 155
BODY and bind `standard-output' to that buffer, so that output
generated with `prin1' and similar functions in BODY goes into
that buffer.  Do not make that buffer current for running the
forms in BODY.  Use `with-current-buffer-window' instead if you
need to run BODY with that buffer current.
156 157 158 159 160

At the end of BODY, mark the specified buffer unmodified and
read-only, and display it in a window (but do not select it).
The display happens by calling `display-buffer' passing it the
ACTION argument.  If `temp-buffer-resize-mode' is enabled, the
161
corresponding window may be resized automatically.
162 163 164

Return the value returned by BODY, unless QUIT-FUNCTION specifies
a function.  In that case, run that function with two arguments -
165
the window showing the specified buffer and the value returned by
166
BODY - and return the value returned by that function.
167 168 169

If the buffer is displayed on a new frame, the window manager may
decide to select that frame.  In that case, it's usually a good
170 171 172
strategy if QUIT-FUNCTION selects the window showing the buffer
before reading any value from the minibuffer; for example, when
asking a `yes-or-no-p' question.
173

174
This runs the hook `temp-buffer-window-setup-hook' before BODY,
175 176 177
with the specified buffer temporarily current.  It runs the hook
`temp-buffer-window-show-hook' after displaying the buffer, with
that buffer temporarily current, and the window that was used to
178 179
display it temporarily selected.

180
This construct is similar to `with-output-to-temp-buffer' but,
181 182 183 184 185 186 187
neither runs `temp-buffer-setup-hook' which usually puts the
buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
argument replaces this)."
  (declare (debug t))
  (let ((buffer (make-symbol "buffer"))
	(window (make-symbol "window"))
	(value (make-symbol "value")))
Leo Liu's avatar
Leo Liu committed
188 189 190 191 192 193 194 195 196 197 198 199 200
    (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
			 (vaction action)
			 (vquit-function quit-function))
      `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
	      (standard-output ,buffer)
	      ,window ,value)
	 (setq ,value (progn ,@body))
	 (with-current-buffer ,buffer
	   (setq ,window (temp-buffer-window-show ,buffer ,vaction)))

	 (if (functionp ,vquit-function)
	     (funcall ,vquit-function ,window ,value)
	   ,value)))))
201 202 203 204 205 206

(defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
  "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
This construct is like `with-temp-buffer-window' but unlike that
makes the buffer specified by BUFFER-OR-NAME current for running
BODY."
207 208 209 210
  (declare (debug t))
  (let ((buffer (make-symbol "buffer"))
	(window (make-symbol "window"))
	(value (make-symbol "value")))
Leo Liu's avatar
Leo Liu committed
211 212 213 214 215 216 217 218 219 220 221 222 223
    (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
			 (vaction action)
			 (vquit-function quit-function))
      `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
	      (standard-output ,buffer)
	      ,window ,value)
	 (with-current-buffer ,buffer
	   (setq ,value (progn ,@body))
	   (setq ,window (temp-buffer-window-show ,buffer ,vaction)))

	 (if (functionp ,vquit-function)
	     (funcall ,vquit-function ,window ,value)
	   ,value)))))
224 225 226 227 228 229 230 231 232

(defmacro with-displayed-buffer-window (buffer-or-name action quit-function &rest body)
  "Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
This construct is like `with-current-buffer-window' but unlike that
displays the buffer specified by BUFFER-OR-NAME before running BODY."
  (declare (debug t))
  (let ((buffer (make-symbol "buffer"))
	(window (make-symbol "window"))
	(value (make-symbol "value")))
Leo Liu's avatar
Leo Liu committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
			 (vaction action)
			 (vquit-function quit-function))
      `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
	      (standard-output ,buffer)
	      ,window ,value)
	 (with-current-buffer ,buffer
	   (setq ,window (temp-buffer-window-show ,buffer ,vaction)))

	 (let ((inhibit-read-only t)
	       (inhibit-modification-hooks t))
	   (setq ,value (progn ,@body)))

	 (set-window-point ,window (point-min))

	 (when (functionp (cdr (assq 'window-height (cdr ,vaction))))
	   (ignore-errors
	     (funcall (cdr (assq 'window-height (cdr ,vaction))) ,window)))

252 253 254 255 256 257
	 (when (consp (cdr (assq 'preserve-size (cdr ,vaction))))
	   (window-preserve-size
	    ,window t (cadr (assq 'preserve-size (cdr ,vaction))))
	  (window-preserve-size
	    ,window nil (cddr (assq 'preserve-size (cdr ,vaction)))))

Leo Liu's avatar
Leo Liu committed
258 259 260
	 (if (functionp ,vquit-function)
	     (funcall ,vquit-function ,window ,value)
	   ,value)))))
261

262 263 264 265 266
;; The following two functions are like `window-next-sibling' and
;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
;; they don't substitute the selected window for nil), and they return
;; nil when WINDOW doesn't have a parent (like a frame's root window or
;; a minibuffer window).
267
(defun window-right (window)
268 269 270
  "Return WINDOW's right sibling.
Return nil if WINDOW is the root window of its frame.  WINDOW can
be any window."
271
  (and window (window-parent window) (window-next-sibling window)))
272

273
(defun window-left (window)
274 275 276
  "Return WINDOW's left sibling.
Return nil if WINDOW is the root window of its frame.  WINDOW can
be any window."
277
  (and window (window-parent window) (window-prev-sibling window)))
278

279
(defun window-child (window)
280 281
  "Return WINDOW's first child window.
WINDOW can be any window."
282
  (or (window-top-child window) (window-left-child window)))
283 284

(defun window-child-count (window)
285 286
  "Return number of WINDOW's child windows.
WINDOW can be any window."
287 288 289 290
  (let ((count 0))
    (when (and (windowp window) (setq window (window-child window)))
      (while window
	(setq count (1+ count))
291
	(setq window (window-next-sibling window))))
292 293 294
    count))

(defun window-last-child (window)
295 296
  "Return last child window of WINDOW.
WINDOW can be any window."
297
  (when (and (windowp window) (setq window (window-child window)))
298 299
    (while (window-next-sibling window)
      (setq window (window-next-sibling window))))
300 301
  window)

302
(defun window-normalize-buffer (buffer-or-name)
303 304 305 306 307 308 309 310 311 312 313 314 315 316
  "Return buffer specified by BUFFER-OR-NAME.
BUFFER-OR-NAME must be either a buffer or a string naming a live
buffer and defaults to the current buffer."
  (cond
   ((not buffer-or-name)
    (current-buffer))
   ((bufferp buffer-or-name)
    (if (buffer-live-p buffer-or-name)
	buffer-or-name
      (error "Buffer %s is not a live buffer" buffer-or-name)))
   ((get-buffer buffer-or-name))
   (t
    (error "No such buffer %s" buffer-or-name))))

317
(defun window-normalize-frame (frame)
318 319 320 321 322 323 324 325
  "Return frame specified by FRAME.
FRAME must be a live frame and defaults to the selected frame."
  (if frame
      (if (frame-live-p frame)
	  frame
	(error "%s is not a live frame" frame))
    (selected-frame)))

326
(defun window-normalize-window (window &optional live-only)
327 328 329 330
  "Return the window specified by WINDOW.
If WINDOW is nil, return the selected window.  Otherwise, if
WINDOW is a live or an internal window, return WINDOW; if
LIVE-ONLY is non-nil, return WINDOW for a live window only.
331
Otherwise, signal an error."
332 333 334 335 336 337 338 339 340 341 342
  (cond
   ((null window)
    (selected-window))
   (live-only
    (if (window-live-p window)
	window
      (error "%s is not a live window" window)))
   ((window-valid-p window)
    window)
   (t
    (error "%s is not a valid window" window))))
343

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
;; Maybe this should go to frame.el.
(defun frame-char-size (&optional window-or-frame horizontal)
  "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
If WINDOW-OR-FRAME is a live frame, return the value of
`frame-char-height' for that frame.  If WINDOW-OR-FRAME is a
valid window, return the value of `frame-char-height' for that
window's frame.  In any other case, return the value of
`frame-char-height' for the selected frame.

Optional argument HORIZONTAL non-nil means to return the value of
`frame-char-width' for WINDOW-OR-FRAME."
  (let ((frame
	 (cond
	  ((window-valid-p window-or-frame)
	   (window-frame window-or-frame))
	  ((frame-live-p window-or-frame)
	   window-or-frame)
	  (t (selected-frame)))))
    (if horizontal
	(frame-char-width frame)
      (frame-char-height frame))))

366 367 368 369 370 371 372 373
(defvar ignore-window-parameters nil
  "If non-nil, standard functions ignore window parameters.
The functions currently affected by this are `split-window',
`delete-window', `delete-other-windows' and `other-window'.

An application may bind this to a non-nil value around calls to
these functions to inhibit processing of window parameters.")

374
;; This must go to C, finally (or get removed).
375
(defconst window-safe-min-height 1
376
  "The absolute minimum number of lines of any window.
377 378
Anything less might crash Emacs.")

379 380 381 382 383
(defun window-safe-min-pixel-height (&optional window)
  "Return the absolute minimum pixel height of WINDOW."
  (* window-safe-min-height
     (frame-char-size (window-normalize-window window))))

384
(defcustom window-min-height 4
385 386
  "The minimum total height, in lines, of any window.
The value has to accommodate one text line, a mode and header
387 388 389
line, a horizontal scroll bar and a bottom divider, if present.
A value less than `window-safe-min-height' is ignored.  The value
of this variable is honored when windows are resized or split.
390 391 392

Applications should never rebind this variable.  To resize a
window to a height less than the one specified here, an
393
application should instead call `window-resize' with a non-nil
394
IGNORE argument.  In order to have `split-window' make a window
Paul Eggert's avatar
Paul Eggert committed
395
shorter, explicitly specify the SIZE argument of that function."
396 397 398 399
  :type 'integer
  :version "24.1"
  :group 'windows)

400 401 402 403 404 405
(defun window-min-pixel-height (&optional window)
  "Return the minimum pixel height of window WINDOW."
  (* (max window-min-height window-safe-min-height)
     (frame-char-size window)))

;; This must go to C, finally (or get removed).
406
(defconst window-safe-min-width 2
407
  "The absolute minimum number of columns of a window.
408 409
Anything less might crash Emacs.")

410 411 412 413 414
(defun window-safe-min-pixel-width (&optional window)
  "Return the absolute minimum pixel width of WINDOW."
  (* window-safe-min-width
     (frame-char-size (window-normalize-window window) t)))

415
(defcustom window-min-width 10
416 417 418 419 420
  "The minimum total width, in columns, of any window.
The value has to accommodate two text columns as well as margins,
fringes, a scroll bar and a right divider, if present.  A value
less than `window-safe-min-width' is ignored.  The value of this
variable is honored when windows are resized or split.
421 422 423

Applications should never rebind this variable.  To resize a
window to a width less than the one specified here, an
424
application should instead call `window-resize' with a non-nil
425
IGNORE argument.  In order to have `split-window' make a window
Paul Eggert's avatar
Paul Eggert committed
426
narrower, explicitly specify the SIZE argument of that function."
427 428 429 430
  :type 'integer
  :version "24.1"
  :group 'windows)

431 432 433 434 435 436 437 438 439 440 441 442 443
(defun window-min-pixel-width (&optional window)
  "Return the minimum pixel width of window WINDOW."
  (* (max window-min-width window-safe-min-width)
     (frame-char-size window t)))

(defun window-safe-min-pixel-size (&optional window horizontal)
  "Return the absolute minimum pixel height of WINDOW.
Optional argument HORIZONTAL non-nil means return the absolute
minimum pixel width of WINDOW."
  (if horizontal
      (window-safe-min-pixel-width window)
    (window-safe-min-pixel-height window)))

444 445 446 447 448 449 450 451
(defun window-min-pixel-size (&optional window horizontal)
  "Return the minimum pixel height of WINDOW.
Optional argument HORIZONTAL non-nil means return the minimum
pixel width of WINDOW."
  (if horizontal
      (window-min-pixel-width window)
    (window-min-pixel-height window)))

452
(defun window-combined-p (&optional window horizontal)
453
  "Return non-nil if WINDOW has siblings in a given direction.
454
WINDOW must be a valid window and defaults to the selected one.
455

456 457 458 459
HORIZONTAL determines a direction for the window combination.  If
HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
a vertical window combination.  If HORIZONTAL is non-nil, return
non-nil if WINDOW is part of a horizontal window combination."
460
  (setq window (window-normalize-window window))
461
  (let ((parent (window-parent window)))
462 463 464 465
    (and parent
	 (if horizontal
	     (window-left-child parent)
	   (window-top-child parent)))))
466

467 468 469 470 471 472 473 474 475 476
(defun window-combination-p (&optional window horizontal)
  "Return WINDOW's first child if WINDOW is a vertical combination.
WINDOW can be any window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return WINDOW's first
child if WINDOW is a horizontal combination."
  (setq window (window-normalize-window window))
  (if horizontal
      (window-left-child window)
    (window-top-child window)))

477 478
(defun window-combinations (window &optional horizontal)
  "Return largest number of windows vertically arranged within WINDOW.
479
WINDOW must be a valid window and defaults to the selected one.
480
If HORIZONTAL is non-nil, return the largest number of
481
windows horizontally arranged within WINDOW."
482
  (setq window (window-normalize-window window))
483 484 485 486
  (cond
   ((window-live-p window)
    ;; If WINDOW is live, return 1.
    1)
487 488 489
   ((if horizontal
	(window-left-child window)
      (window-top-child window))
490
    ;; If WINDOW is iso-combined, return the sum of the values for all
491
    ;; child windows of WINDOW.
492 493 494 495
    (let ((child (window-child window))
	  (count 0))
      (while child
	(setq count
496
	      (+ (window-combinations child horizontal)
497 498 499 500 501
		 count))
	(setq child (window-right child)))
      count))
   (t
    ;; If WINDOW is not iso-combined, return the maximum value of any
502
    ;; child window of WINDOW.
503 504 505 506
    (let ((child (window-child window))
	  (count 1))
      (while child
	(setq count
507
	      (max (window-combinations child horizontal)
508 509 510 511
		   count))
	(setq child (window-right child)))
      count))))

512
(defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
513 514 515 516 517 518
  "Helper function for `walk-window-tree' and `walk-window-subtree'."
  (let (walk-window-tree-buffer)
    (while walk-window-tree-window
      (setq walk-window-tree-buffer
	    (window-buffer walk-window-tree-window))
      (when (or walk-window-tree-buffer any)
519
	(funcall fun walk-window-tree-window))
520 521
      (unless walk-window-tree-buffer
	(walk-window-tree-1
522
	 fun (window-left-child walk-window-tree-window) any)
523
	(walk-window-tree-1
524
	 fun (window-top-child walk-window-tree-window) any))
525 526 527 528 529
      (if sub-only
	  (setq walk-window-tree-window nil)
	(setq walk-window-tree-window
	      (window-right walk-window-tree-window))))))

530
(defun walk-window-tree (fun &optional frame any minibuf)
531 532
  "Run function FUN on each live window of FRAME.
FUN must be a function with one argument - a window.  FRAME must
533
be a live frame and defaults to the selected one.  ANY, if
534
non-nil, means to run FUN on all live and internal windows of
535 536
FRAME.

537 538 539 540 541 542
Optional argument MINIBUF t means run FUN on FRAME's minibuffer
window even if it isn't active.  MINIBUF nil or omitted means run
FUN on FRAME's minibuffer window only if it's active.  In both
cases the minibuffer window must be part of FRAME.  MINIBUF
neither nil nor t means never run FUN on the minibuffer window.

543
This function performs a pre-order, depth-first traversal of the
544
window tree.  If FUN changes the window tree, the result is
545
unpredictable."
546 547 548 549 550 551 552 553 554 555
  (setq frame (window-normalize-frame frame))
  (walk-window-tree-1 fun (frame-root-window frame) any)
  (when (memq minibuf '(nil t))
    ;; Run FUN on FRAME's minibuffer window if requested.
    (let ((minibuffer-window (minibuffer-window frame)))
      (when (and (window-live-p minibuffer-window)
		 (eq (window-frame minibuffer-window) frame)
		 (or (eq minibuf t)
		     (minibuffer-window-active-p minibuffer-window)))
	(funcall fun minibuffer-window)))))
556

557 558 559 560
(defun walk-window-subtree (fun &optional window any)
  "Run function FUN on the subtree of windows rooted at WINDOW.
WINDOW defaults to the selected window.  FUN must be a function
with one argument - a window.  By default, run FUN only on live
561
windows of the subtree.  If the optional argument ANY is non-nil,
562 563
run FUN on all live and internal windows of the subtree.  If
WINDOW is live, run FUN on WINDOW only.
564 565

This function performs a pre-order, depth-first traversal of the
566
subtree rooted at WINDOW.  If FUN changes that tree, the result
567
is unpredictable."
568
  (setq window (window-normalize-window window))
569
  (walk-window-tree-1 fun window any t))
570

571
(defun window-with-parameter (parameter &optional value frame any minibuf)
572 573 574
  "Return first window on FRAME with PARAMETER non-nil.
FRAME defaults to the selected frame.  Optional argument VALUE
non-nil means only return a window whose window-parameter value
Juanma Barranquero's avatar
Juanma Barranquero committed
575
for PARAMETER equals VALUE (comparison is done with `equal').
576
Optional argument ANY non-nil means consider internal windows
577 578 579 580 581 582 583
too.

Optional argument MINIBUF t means consider FRAME's minibuffer
window even if it isn't active.  MINIBUF nil or omitted means
consider FRAME's minibuffer window only if it's active.  In both
cases the minibuffer window must be part of FRAME.  MINIBUF
neither nil nor t means never consider the minibuffer window."
584
  (let (this-value)
585 586 587 588 589 590
    (catch 'found
      (walk-window-tree
       (lambda (window)
	 (when (and (setq this-value (window-parameter window parameter))
		    (or (not value) (equal value this-value)))
	   (throw 'found window)))
591
       frame any minibuf))))
592 593 594 595

;;; Atomic windows.
(defun window-atom-root (&optional window)
  "Return root of atomic window WINDOW is a part of.
596
WINDOW must be a valid window and defaults to the selected one.
Juanma Barranquero's avatar
Juanma Barranquero committed
597
Return nil if WINDOW is not part of an atomic window."
598
  (setq window (window-normalize-window window))
599 600 601 602 603 604
  (let (root)
    (while (and window (window-parameter window 'window-atom))
      (setq root window)
      (setq window (window-parent window)))
    root))

605
(defun window-make-atom (window)
606 607 608 609 610 611
  "Make WINDOW an atomic window.
WINDOW must be an internal window.  Return WINDOW."
  (if (not (window-child window))
      (error "Window %s is not an internal window" window)
    (walk-window-subtree
     (lambda (window)
612 613
       (unless (window-parameter window 'window-atom)
	 (set-window-parameter window 'window-atom t)))
614 615 616
     window t)
    window))

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
(defun display-buffer-in-atom-window (buffer alist)
  "Display BUFFER in an atomic window.
This function displays BUFFER in a new window that will be
combined with an existing window to form an atomic window.  If
the existing window is already part of an atomic window, add the
new window to that atomic window.  Operations like `split-window'
or `delete-window', when applied to a constituent of an atomic
window, are applied atomically to the root of that atomic window.

ALIST is an association list of symbols and values.  The
following symbols can be used.

`window' specifies the existing window the new window shall be
  combined with.  Use `window-atom-root' to make the new window a
  sibling of an atomic window's root.  If an internal window is
  specified here, all children of that window become part of the
  atomic window too.  If no window is specified, the new window
634 635 636
  becomes a sibling of the selected window.  By default, the
  `window-atom' parameter of the existing window is set to `main'
  provided it is live and was not set before.
637 638 639

`side' denotes the side of the existing window where the new
  window shall be located.  Valid values are `below', `right',
640 641
  `above' and `left'.  The default is `below'.  By default, the
  `window-atom' parameter of the new window is set to this value.
642 643 644

The return value is the new window, nil when creating that window
failed."
645 646 647 648 649 650 651
  (let* ((ignore-window-parameters t)
	 (window-combination-limit t)
	 (window-combination-resize 'atom)
	 (window (cdr (assq 'window alist)))
	 (side (cdr (assq 'side alist)))
	 (atom (when window (window-parameter window 'window-atom)))
	 root new)
652
    (setq window (window-normalize-window window))
653 654
    (setq root (window-atom-root window))
    ;; Split off new window.
655
    (when (setq new (split-window window nil side))
656 657 658 659 660 661 662 663 664 665 666
      (window-make-atom
       (if (and root (not (eq root window)))
	   ;; When WINDOW was part of an atomic window and we did not
	   ;; split its root, root atomic window at old root.
	   root
	 ;; Otherwise, root atomic window at WINDOW's new parent.
	 (window-parent window)))
      ;; Assign `window-atom' parameters, if needed.
      (when (and (not atom) (window-live-p window))
	(set-window-parameter window 'window-atom 'main))
      (set-window-parameter new 'window-atom side)
667 668
      ;; Display BUFFER in NEW and return NEW.
      (window--display-buffer
669
       buffer new 'window alist display-buffer-mark-dedicated))))
670

671 672
(defun window--atom-check-1 (window)
  "Subroutine of `window--atom-check'."
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
  (when window
    (if (window-parameter window 'window-atom)
	(let ((count 0))
	  (when (or (catch 'reset
		      (walk-window-subtree
		       (lambda (window)
			 (if (window-parameter window 'window-atom)
			     (setq count (1+ count))
			   (throw 'reset t)))
		       window t))
		    ;; count >= 1 must hold here.  If there's no other
		    ;; window around dissolve this atomic window.
		    (= count 1))
	    ;; Dissolve atomic window.
	    (walk-window-subtree
	     (lambda (window)
	       (set-window-parameter window 'window-atom nil))
	     window t)))
      ;; Check children.
      (unless (window-buffer window)
693 694
	(window--atom-check-1 (window-left-child window))
	(window--atom-check-1 (window-top-child window))))
695
    ;; Check right sibling
696
    (window--atom-check-1 (window-right window))))
697

698
(defun window--atom-check (&optional frame)
699 700
  "Check atomicity of all windows on FRAME.
FRAME defaults to the selected frame.  If an atomic window is
701 702 703
wrongly configured, reset the atomicity of all its windows on
FRAME to nil.  An atomic window is wrongly configured if it has
no child windows or one of its child windows is not atomic."
704
  (window--atom-check-1 (frame-root-window frame)))
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

;; Side windows.
(defvar window-sides '(left top right bottom)
  "Window sides.")

(defcustom window-sides-vertical nil
  "If non-nil, left and right side windows are full height.
Otherwise, top and bottom side windows are full width."
  :type 'boolean
  :group 'windows
  :version "24.1")

(defcustom window-sides-slots '(nil nil nil nil)
  "Maximum number of side window slots.
The value is a list of four elements specifying the number of
Juanma Barranquero's avatar
Juanma Barranquero committed
720
side window slots on (in this order) the left, top, right and
721 722 723 724
bottom side of each frame.  If an element is a number, this means
to display at most that many side windows on the corresponding
side.  If an element is nil, this means there's no bound on the
number of slots on that side."
725
  :version "24.1"
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
  :risky t
  :type
  '(list
    :value (nil nil nil nil)
    (choice
     :tag "Left"
     :help-echo "Maximum slots of left side window."
     :value nil
     :format "%[Left%] %v\n"
     (const :tag "Unlimited" :format "%t" nil)
     (integer :tag "Number" :value 2 :size 5))
    (choice
     :tag "Top"
     :help-echo "Maximum slots of top side window."
     :value nil
     :format "%[Top%] %v\n"
     (const :tag "Unlimited" :format "%t" nil)
     (integer :tag "Number" :value 3 :size 5))
    (choice
     :tag "Right"
     :help-echo "Maximum slots of right side window."
     :value nil
     :format "%[Right%] %v\n"
     (const :tag "Unlimited" :format "%t" nil)
     (integer :tag "Number" :value 2 :size 5))
    (choice
     :tag "Bottom"
     :help-echo "Maximum slots of bottom side window."
     :value nil
     :format "%[Bottom%] %v\n"
     (const :tag "Unlimited" :format "%t" nil)
     (integer :tag "Number" :value 3 :size 5)))
  :group 'windows)

760 761 762 763 764 765 766 767 768
(defun window--side-window-p (window)
  "Return non-nil if WINDOW is a side window or the parent of one."
  (or (window-parameter window 'window-side)
      (and (window-child window)
	   (or (window-parameter
		(window-child window) 'window-side)
	       (window-parameter
		(window-last-child window) 'window-side)))))

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
(defun window--major-non-side-window (&optional frame)
  "Return the major non-side window of frame FRAME.
The optional argument FRAME must be a live frame and defaults to
the selected one.

If FRAME has at least one side window, the major non-side window
is either an internal non-side window such that all other
non-side windows on FRAME descend from it, or the single live
non-side window of FRAME.  If FRAME has no side windows, return
its root window."
  (let ((frame (window-normalize-frame frame))
	major sibling)
    ;; Set major to the _last_ window found by `walk-window-tree' that
    ;; is not a side window but has a side window as its sibling.
    (walk-window-tree
     (lambda (window)
       (and (not (window-parameter window 'window-side))
	    (or (and (setq sibling (window-prev-sibling window))
		     (window-parameter sibling 'window-side))
		(and (setq sibling (window-next-sibling window))
		     (window-parameter sibling 'window-side)))
	    (setq major window)))
791
     frame t 'nomini)
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
    (or major (frame-root-window frame))))

(defun window--major-side-window (side)
  "Return major side window on SIDE.
SIDE must be one of the symbols `left', `top', `right' or
`bottom'.  Return nil if no such window exists."
  (let ((root (frame-root-window))
	window)
    ;; (1) If a window on the opposite side exists, return that window's
    ;;     sibling.
    ;; (2) If the new window shall span the entire side, return the
    ;;     frame's root window.
    ;; (3) If a window on an orthogonal side exists, return that
    ;;     window's sibling.
    ;; (4) Otherwise return the frame's root window.
    (cond
     ((or (and (eq side 'left)
	       (setq window (window-with-parameter 'window-side 'right nil t)))
	  (and (eq side 'top)
	       (setq window (window-with-parameter 'window-side 'bottom nil t))))
      (window-prev-sibling window))
     ((or (and (eq side 'right)
	       (setq window (window-with-parameter 'window-side 'left nil t)))
	  (and (eq side 'bottom)
	       (setq window (window-with-parameter 'window-side 'top nil t))))
      (window-next-sibling window))
     ((memq side '(left right))
      (cond
       (window-sides-vertical
	root)
       ((setq window (window-with-parameter 'window-side 'top nil t))
	(window-next-sibling window))
       ((setq window (window-with-parameter 'window-side 'bottom nil t))
	(window-prev-sibling window))
       (t root)))
     ((memq side '(top bottom))
      (cond
       ((not window-sides-vertical)
	root)
       ((setq window (window-with-parameter 'window-side 'left nil t))
	(window-next-sibling window))
       ((setq window (window-with-parameter 'window-side 'right nil t))
	(window-prev-sibling window))
       (t root))))))

(defun display-buffer-in-major-side-window (buffer side slot &optional alist)
  "Display BUFFER in a new window on SIDE of the selected frame.
SIDE must be one of `left', `top', `right' or `bottom'.  SLOT
specifies the slot to use.  ALIST is an association list of
symbols and values as passed to `display-buffer-in-side-window'.
This function may be called only if no window on SIDE exists yet.
The new window automatically becomes the \"major\" side window on
SIDE.  Return the new window, nil if its creation window failed."
845
  (let* ((left-or-right (memq side '(left right)))
846 847 848 849 850 851 852 853 854 855
	 (major (window--major-side-window side))
	 (on-side (cond
		   ((eq side 'top) 'above)
		   ((eq side 'bottom) 'below)
		   (t side)))
	 ;; The following two bindings will tell `split-window' to take
	 ;; the space for the new window from `major' and not make a new
	 ;; parent window unless needed.
	 (window-combination-resize 'side)
	 (window-combination-limit nil)
856
	 (new (split-window major nil on-side)))
857 858 859 860 861 862 863 864 865
    (when new
      ;; Initialize `window-side' parameter of new window to SIDE.
      (set-window-parameter new 'window-side side)
      ;; Install `window-slot' parameter of new window.
      (set-window-parameter new 'window-slot slot)
      ;; Install `delete-window' parameter thus making sure that when
      ;; the new window is deleted, a side window on the opposite side
      ;; does not get resized.
      (set-window-parameter new 'delete-window 'delete-side-window)
866 867
      ;; Auto-adjust height/width of new window unless a size has been
      ;; explicitly requested.
Paul Eggert's avatar
Paul Eggert committed
868
      (unless (if left-or-right
869 870 871 872 873 874 875
		  (cdr (assq 'window-width alist))
		(cdr (assq 'window-height alist)))
	(setq alist
	      (cons
	       (cons
		(if left-or-right 'window-width 'window-height)
		(/ (window-total-size (frame-root-window) left-or-right)
876 877
		   ;; By default use a fourth of the size of the frame's
		   ;; root window.
878 879
		   4))
	       alist)))
880
      ;; Install BUFFER in new window and return NEW.
881
      (window--display-buffer buffer new 'window alist 'side))))
882 883 884 885 886 887 888 889 890

(defun delete-side-window (window)
  "Delete side window WINDOW."
  (let ((window-combination-resize
	 (window-parameter (window-parent window) 'window-side))
	(ignore-window-parameters t))
    (delete-window window)))

(defun display-buffer-in-side-window (buffer alist)
891
  "Display BUFFER in a side window of the selected frame.
892
ALIST is an association list of symbols and values.  The
893
following special symbols can be used in ALIST.
894

895 896 897
`side' denotes the side of the frame where the new window shall
  be located.  Valid values are `bottom', `right', `top' and
  `left'.  The default is `bottom'.
898 899 900 901 902 903 904 905

`slot' if non-nil, specifies the window slot where to display
  BUFFER.  A value of zero or nil means use the middle slot on
  the specified side.  A negative value means use a slot
  preceding (that is, above or on the left of) the middle slot.
  A positive value means use a slot following (that is, below or
  on the right of) the middle slot.  The default is zero."
  (let ((side (or (cdr (assq 'side alist)) 'bottom))
906
	(slot (or (cdr (assq 'slot alist)) 0)))
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    (cond
     ((not (memq side '(top bottom left right)))
      (error "Invalid side %s specified" side))
     ((not (numberp slot))
      (error "Invalid slot %s specified" slot)))

    (let* ((major (window-with-parameter 'window-side side nil t))
	   ;; `major' is the major window on SIDE, `windows' the list of
	   ;; life windows on SIDE.
	   (windows
	    (when major
	      (let (windows)
		(walk-window-tree
		 (lambda (window)
		   (when (eq (window-parameter window 'window-side) side)
922 923
		     (setq windows (cons window windows))))
		 nil nil 'nomini)
924 925 926 927 928 929 930 931 932 933
		(nreverse windows))))
	   (slots (when major (max 1 (window-child-count major))))
	   (max-slots
	    (nth (cond
		  ((eq side 'left) 0)
		  ((eq side 'top) 1)
		  ((eq side 'right) 2)
		  ((eq side 'bottom) 3))
		 window-sides-slots))
	   window this-window this-slot prev-window next-window
934
	   best-window best-slot abs-slot)
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

      (cond
       ((and (numberp max-slots) (<= max-slots 0))
	;; No side-slots available on this side.  Don't create an error,
	;; just return nil.
	nil)
       ((not windows)
	;; No major window exists on this side, make one.
	(display-buffer-in-major-side-window buffer side slot alist))
       (t
	;; Scan windows on SIDE.
	(catch 'found
	  (dolist (window windows)
	    (setq this-slot (window-parameter window 'window-slot))
	    (cond
	     ;; The following should not happen and probably be checked
	     ;; by window--side-check.
	     ((not (numberp this-slot)))
	     ((= this-slot slot)
	      ;; A window with a matching slot has been found.
	      (setq this-window window)
	      (throw 'found t))
	     (t
	      ;; Check if this window has a better slot value wrt the
	      ;; slot of the window we want.
	      (setq abs-slot
		    (if (or (and (> this-slot 0) (> slot 0))
			    (and (< this-slot 0) (< slot 0)))
			(abs (- slot this-slot))
		      (+ (abs slot) (abs this-slot))))
	      (unless (and best-slot (<= best-slot abs-slot))
		(setq best-window window)
		(setq best-slot abs-slot))
	      (cond
	       ((<= this-slot slot)
		(setq prev-window window))
	       ((not next-window)
		(setq next-window window)))))))

	;; `this-window' is the first window with the same SLOT.
	;; `prev-window' is the window with the largest slot < SLOT.  A new
	;; window will be created after it.
	;; `next-window' is the window with the smallest slot > SLOT.  A new
	;; window will be created before it.
	;; `best-window' is the window with the smallest absolute difference
	;; of its slot and SLOT.

	;; Note: We dedicate the window used softly to its buffer to
	;; avoid that "other" (non-side) buffer display functions steal
	;; it from us.  This must eventually become customizable via
	;; ALIST (or, better, avoided in the "other" functions).
	(or (and this-window
		 ;; Reuse `this-window'.
988
		 (window--display-buffer buffer this-window 'reuse alist 'side))
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	    (and (or (not max-slots) (< slots max-slots))
		 (or (and next-window
			  ;; Make new window before `next-window'.
			  (let ((next-side
				 (if (memq side '(left right)) 'above 'left))
				(window-combination-resize 'side))
			    (setq window (split-window next-window nil next-side))
			    ;; When the new window is deleted, its space
			    ;; is returned to other side windows.
			    (set-window-parameter
			     window 'delete-window 'delete-side-window)
			    window))
		     (and prev-window
			  ;; Make new window after `prev-window'.
			  (let ((prev-side
				 (if (memq side '(left right)) 'below 'right))
				(window-combination-resize 'side))
			    (setq window (split-window prev-window nil prev-side))
			    ;; When the new window is deleted, its space
			    ;; is returned to other side windows.
			    (set-window-parameter
			     window 'delete-window 'delete-side-window)
			    window)))
		   (set-window-parameter window 'window-slot slot)
1013
		   (window--display-buffer buffer window 'window alist 'side))
1014 1015 1016 1017 1018
	    (and best-window
		 ;; Reuse `best-window'.
		 (progn
		   ;; Give best-window the new slot value.
		   (set-window-parameter best-window 'window-slot slot)
1019 1020
		   (window--display-buffer
		    buffer best-window 'reuse alist 'side)))))))))
1021

1022
(defun window--side-check (&optional frame)
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  "Check the side window configuration of FRAME.
FRAME defaults to the selected frame.

A valid side window configuration preserves the following two
invariants:

- If there exists a window whose window-side parameter is
  non-nil, there must exist at least one live window whose
  window-side parameter is nil.

- If a window W has a non-nil window-side parameter (i) it must
  have a parent window and that parent's window-side parameter
  must be either nil or the same as for W, and (ii) any child
  window of W must have the same window-side parameter as W.

If the configuration is invalid, reset the window-side parameters
of all windows on FRAME to nil."
  (let (left top right bottom none side parent parent-side)
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    (when (or (catch 'reset
		(walk-window-tree
		 (lambda (window)
		   (setq side (window-parameter window 'window-side))
		   (setq parent (window-parent window))
		   (setq parent-side
			 (and parent (window-parameter parent 'window-side)))
		   ;; The following `cond' seems a bit tedious, but I'd
		   ;; rather stick to using just the stack.
		   (cond
		    (parent-side
		     (when (not (eq parent-side side))
		       ;; A parent whose window-side is non-nil must
		       ;; have a child with the same window-side.
		       (throw 'reset t)))
1056 1057 1058 1059
		    ((not side)
		     (when (window-buffer window)
		       ;; Record that we have at least one non-side,
		       ;; live window.
1060
		       (setq none t)))
1061 1062 1063 1064 1065 1066 1067 1068 1069
		    ((if (memq side '(left top))
			 (window-prev-sibling window)
		       (window-next-sibling window))
		     ;; Left and top major side windows must not have a
		     ;; previous sibling, right and bottom major side
		     ;; windows must not have a next sibling.
		     (throw 'reset t))
		    ;; Now check that there's no more than one major
		    ;; window for any of left, top, right and bottom.
1070
		    ((eq side 'left)
1071
		     (if left (throw 'reset t) (setq left t)))
1072
		    ((eq side 'top)
1073
		     (if top (throw 'reset t) (setq top t)))
1074
		    ((eq side 'right)
1075
		     (if right (throw 'reset t) (setq right t)))
1076
		    ((eq side 'bottom)
1077
		     (if bottom (throw 'reset t) (setq bottom t)))
1078
		    (t
1079
		     (throw 'reset t))))
1080
		 frame t 'nomini))
1081 1082 1083
	      ;; If there's a side window, there must be at least one
	      ;; non-side window.
	      (and (or left top right bottom) (not none)))
1084 1085 1086
      (walk-window-tree
       (lambda (window)
	 (set-window-parameter window 'window-side nil))
1087
       frame t 'nomini))))
1088

1089
(defun window--check (&optional frame)
1090 1091
  "Check atomic and side windows on FRAME.
FRAME defaults to the selected frame."
1092 1093
  (window--side-check frame)
  (window--atom-check frame))
1094

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
;; Dumping frame/window contents.
(defun window--dump-window (&optional window erase)
  "Dump WINDOW to buffer *window-frame-dump*.
WINDOW must be a valid window and defaults to the selected one.
Optional argument ERASE non-nil means erase *window-frame-dump*
before writing to it."
  (setq window (window-normalize-window window))
  (with-current-buffer (get-buffer-create "*window-frame-dump*")
    (when erase (erase-buffer))
    (insert
     (format "%s   parent: %s\n" window (window-parent window))
     (format "pixel left: %s   top: %s   size: %s x %s   new: %s\n"
	     (window-pixel-left window) (window-pixel-top window)
	     (window-size window t t) (window-size window nil t)
	     (window-new-pixel window))
     (format "char left: %s   top: %s   size: %s x %s   new: %s\n"
	     (window-left-column window) (window-top-line window)
	     (window-total-size window t) (window-total-size window)
	     (window-new-total window))
     (format "normal: %s x %s   new: %s\n"
	     (window-normal-size window t) (window-normal-size window)
	     (window-new-normal window)))
    (when (window-live-p window)
      (let ((fringes (window-fringes window))
	    (margins (window-margins window)))
	(insert
	 (format "body pixel: %s x %s   char: %s x %s\n"
		 (window-body-width window t) (window-body-height window t)
		 (window-body-width window) (window-body-height window))
	 (format "width left fringe: %s  left margin: %s  right margin: %s\n"
		 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
	 (format "width right fringe: %s  scroll-bar: %s  divider: %s\n"
		 (cadr fringes)
		 (window-scroll-bar-width window)
		 (window-right-divider-width window))
	 (format "height header-line: %s  mode-line: %s  divider: %s\n"
		 (window-header-line-height window)
		 (window-mode-line-height window)
		 (window-bottom-divider-width window)))))
    (insert "\n")))

(defun window--dump-frame (&optional window-or-frame)
  "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
WINDOW-OR-FRAME can be a frame or a window and defaults to the
selected frame.  When WINDOW-OR-FRAME is a window, dump that
window's frame.  The buffer *window-frame-dump* is erased before
dumping to it."
  (let* ((window
	  (cond
	   ((or (not window-or-frame)
		(frame-live-p window-or-frame))
	    (frame-root-window window-or-frame))
	   ((or (window-live-p window-or-frame)
		(window-child window-or-frame))
	    window-or-frame)
	   (t
	    (frame-root-window))))
	 (frame (window-frame window)))
    (with-current-buffer (get-buffer-create "*window-frame-dump*")
      (erase-buffer)
      (insert
       (format "frame pixel: %s x %s   cols/lines: %s x %s   units: %s x %s\n"
	       (frame-pixel-width frame) (frame-pixel-height frame)
	       (frame-total-cols frame) (frame-text-lines frame) ; (frame-total-lines frame)
	       (frame-char-width frame) (frame-char-height frame))
       (format "frame text pixel: %s x %s   cols/lines: %s x %s\n"
	       (frame-text-width frame) (frame-text-height frame)
	       (frame-text-cols frame) (frame-text-lines frame))
1163
       (format "tool: %s  scroll: %s/%s  fringe: %s  border: %s  right: %s  bottom: %s\n\n"
1164 1165 1166
	       (if (fboundp 'tool-bar-height)
		   (tool-bar-height frame t)
		 "0")
1167
	       (frame-scroll-bar-width frame)
1168
	       (frame-scroll-bar-height frame)
1169 1170 1171 1172 1173 1174
	       (frame-fringe-width frame)
	       (frame-border-width frame)
	       (frame-right-divider-width frame)
	       (frame-bottom-divider-width frame)))
      (walk-window-tree 'window--dump-window frame t t))))

1175
;;; Window sizes.
1176
(defun window-total-size (&optional window horizontal round)
1177 1178 1179 1180
  "Return the total height or width of WINDOW.
WINDOW must be a valid window and defaults to the selected one.

If HORIZONTAL is omitted or nil, return the total height of
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
WINDOW, in lines.  If WINDOW is live, its total height includes,
in addition to the height of WINDOW's text, the heights of
WINDOW's mode and header line and a bottom divider, if any.

If HORIZONTAL is non-nil, return the total width of WINDOW, in
columns.  If WINDOW is live, its total width includes, in
addition to the width of WINDOW's text, the widths of WINDOW's
fringes, margins, scroll bars and its right divider, if any.

If WINDOW is internal, return the respective size of the screen
areas spanned by its children.
1192 1193 1194

Optional argument ROUND is handled as for `window-total-height'
and `window-total-width'."
1195
  (if horizontal
1196 1197
      (window-total-width window round)
    (window-total-height window round)))
1198

1199
(defun window-size (&optional window horizontal pixelwise round)
1200 1201 1202 1203 1204 1205 1206 1207
  "Return the height or width of WINDOW.
WINDOW must be a valid window and defaults to the selected one.

If HORIZONTAL is omitted or nil, return the total height of
WINDOW, in lines, like `window-total-height'.  Otherwise return
the total width, in columns, like `window-total-width'.

Optional argument PIXELWISE means return the pixel size of WINDOW
1208 1209 1210 1211 1212
like `window-pixel-height' and `window-pixel-width'.

Optional argument ROUND is ignored if PIXELWISE is non-nil and
handled as for `window-total-height' and `window-total-width'
otherwise."
1213 1214 1215
  (if horizontal
      (if pixelwise
	  (window-pixel-width window)
1216
	(window-total-width window round))
1217 1218
    (if pixelwise
	(window-pixel-height window)
1219
      (window-total-height window round))))
1220

1221 1222 1223 1224 1225 1226 1227
(defvar window-size-fixed nil
  "Non-nil in a buffer means windows displaying the buffer are fixed-size.
If the value is `height', then only the window's height is fixed.
If the value is `width', then only the window's width is fixed.
Any other non-nil value fixes both the width and the height.

Emacs won't change the size of any window displaying that buffer,
Juanma Barranquero's avatar
Juanma Barranquero committed
1228
unless it has no other choice (like when deleting a neighboring
1229 1230 1231
window).")
(make-variable-buffer-local 'window-size-fixed)

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
(defun window--preservable-size (window &optional horizontal)
  "Return height of WINDOW as `window-preserve-size' would preserve it.
Optional argument HORIZONTAL non-nil means to return the width of
WINDOW as `window-preserve-size' would preserve it."
  (if horizontal
      (window-body-width window t)
    (+ (window-body-height window t)
       (window-header-line-height window)
       (window-mode-line-height window))))

(defun window-preserve-size (&optional window horizontal preserve)
  "Preserve height of window WINDOW.
WINDOW must be a live window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means preserve the width of
WINDOW.

PRESERVE t means to preserve the current height/width of WINDOW's
body in frame and window resizing operations whenever possible.
The height/width of WINDOW will change only if Emacs has no other
choice.  Resizing a window whose height/width is preserved never
throws an error.

PRESERVE nil means to stop preserving the height/width of WINDOW,
lifting the respective restraint induced by a previous call of
`window-preserve-size' for WINDOW.  Calling `enlarge-window',
`shrink-window', `split-window' or `fit-window-to-buffer' with
WINDOW as argument also removes the respective restraint.

Other values of PRESERVE are reserved for future use."
  (setq window (window-normalize-window window t))
  (let* ((parameter (window-parameter window 'window-preserved-size))
	 (width (nth 1 parameter))
	 (height (nth 2 parameter)))
    (if horizontal
	(set-window-parameter
	 window 'window-preserved-size
	 (list
	  (window-buffer window)
	  (and preserve (window--preservable-size window t))
	  height))
      (set-window-parameter
       window 'window-preserved-size
       (list
	(window-buffer window)
	width
	(and preserve (window--preservable-size window)))))))

(defun window-preserved-size (&optional window horizontal)
  "Return preserved height of window WINDOW.
WINDOW must be a live window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means to return preserved
width of WINDOW."
  (setq window (window-normalize-window window t))
  (let* ((parameter (window-parameter window 'window-preserved-size))
	 (buffer (nth 0 parameter))
	 (width (nth 1 parameter))
	 (height (nth 2 parameter)))
    (when (eq buffer (window-buffer window))
      (if horizontal width height))))

(defun window--preserve-size (window horizontal)
  "Return non-nil when the height of WINDOW shall be preserved.
Optional argument HORIZONTAL non-nil means to return non-nil when
the width of WINDOW shall be preserved."
  (let ((size (window-preserved-size window horizontal)))
    (and (numberp size)
	 (= size (window--preservable-size window horizontal)))))
1299

1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
(defun window-safe-min-size (&optional window horizontal pixelwise)
  "Return safe minimum size of WINDOW.
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return the minimum
number of columns of WINDOW; otherwise return the minimum number
of WINDOW's lines.

Optional argument PIXELWISE non-nil means return the minimum pixel-size
of WINDOW."
  (setq window (window-normalize-window window))
  (if pixelwise
      (if horizontal
	  (* window-safe-min-width
	     (frame-char-width (window-frame window)))
	(* window-safe-min-height
	   (frame-char-height (window-frame window))))
    (if horizontal window-safe-min-width window-safe-min-height)))

(defun window-min-size (&optional window horizontal ignore pixelwise)
1319
  "Return the minimum size of WINDOW.
1320 1321 1322 1323
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return the minimum
number of columns of WINDOW; otherwise return the minimum number
of WINDOW's lines.
1324

1325 1326 1327
The optional argument IGNORE has the same meaning as for
`window-resizable'.  Optional argument PIXELWISE non-nil means
return the minimum pixel-size of WINDOW."
1328
  (window--min-size-1
1329
   (window-normalize-window window) horizontal ignore pixelwise))
1330

1331 1332 1333 1334 1335 1336 1337 1338
(defun window--min-size-ignore-p (window horizontal ignore)
  "Return non-nil if IGNORE says to ignore height restrictions for WINDOW.
HORIZONTAL non-nil means to return non-nil if IGNORE says to
ignore width restrictions for WINDOW."
  (if (window-valid-p ignore)
      (eq window ignore)
    (not (memq ignore '(nil preserved)))))

1339
(defun window--min-size-1 (window horizontal ignore pixelwise)
1340 1341 1342 1343 1344
  "Internal function of `window-min-size'."
  (let ((sub (window-child window)))
    (if sub
	(let ((value 0))
	  ;; WINDOW is an internal window.
1345
	  (if (window-combined-p sub horizontal)
1346
	      ;; The minimum size of an iso-combination is the sum of
1347
	      ;; the minimum sizes of its child windows.
1348 1349
	      (while sub
		(setq value (+ value
1350 1351
			       (window--min-size-1
				sub horizontal ignore pixelwise)))
1352
		(setq sub (window-right sub)))
1353 1354
	    ;; The minimum size of an ortho-combination is the maximum
	    ;; of the minimum sizes of its child windows.
1355 1356
	    (while sub
	      (setq value (max value
1357 1358
			       (window--min-size-1
				sub horizontal ignore pixelwise)))
1359 1360 1361 1362
	      (setq sub (window-right sub))))
	  value)
      (with-current-buffer (window-buffer window)
	(cond
1363 1364
	 ((window-minibuffer-p window)
	  (if pixelwise (frame-char-height (window-frame window)) 1))
1365
	 ((window-size-fixed-p window horizontal ignore)
1366
	  ;; The minimum size of a fixed size window is its size.
1367
	  (window-size window horizontal pixelwise))
1368 1369
	 ((eq ignore 'safe)
	  ;; If IGNORE equals `safe' return the safe value.