Function: fit-window-to-buffer
fit-window-to-buffer is an interactive and byte-compiled function
defined in window.el.gz.
Signature
(fit-window-to-buffer &optional WINDOW MAX-HEIGHT MIN-HEIGHT MAX-WIDTH MIN-WIDTH PRESERVE-SIZE)
Documentation
Adjust size of WINDOW to display its buffer's contents exactly.
WINDOW must be a live window and defaults to the selected one.
If WINDOW is part of a vertical combination, adjust WINDOW's
height. The new height is calculated from the actual height of
the accessible portion of its buffer. The optional argument
MAX-HEIGHT specifies a maximum height and defaults to the height
of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
minimum height and defaults to window-min-height. Both
MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
and header line and a bottom divider, if any.
If WINDOW is part of a horizontal combination and the value of
the option fit-window-to-buffer-horizontally is non-nil, adjust
WINDOW's width. The new width of WINDOW is calculated from the
maximum length of its buffer's lines that follow the current
start position of WINDOW. The optional argument MAX-WIDTH
specifies a maximum width and defaults to the width of WINDOW's
frame. The optional argument MIN-WIDTH specifies a minimum width
and defaults to window-min-width. Both MAX-WIDTH and MIN-WIDTH
are specified in columns and include fringes, margins, a
scrollbar and a vertical divider, if any.
Optional argument PRESERVE-SIZE non-nil means to preserve the
size of WINDOW (see window-preserve-size).
Fit pixelwise if the option window-resize-pixelwise is non-nil.
If WINDOW is its frame's root window and the option
fit-frame-to-buffer(var)/fit-frame-to-buffer(fun) is non-nil, call fit-frame-to-buffer(var)/fit-frame-to-buffer(fun) to
adjust the frame's size.
Note that even if this function makes WINDOW large enough to show
_all_ parts of its buffer you might not see the first part when
WINDOW was scrolled. If WINDOW is resized horizontally, you will
not see the top of its buffer unless WINDOW starts at its minimum
accessible position.
Probably introduced at or before Emacs version 24.3.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/window.el.gz
(defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
"Adjust size of WINDOW to display its buffer's contents exactly.
WINDOW must be a live window and defaults to the selected one.
If WINDOW is part of a vertical combination, adjust WINDOW's
height. The new height is calculated from the actual height of
the accessible portion of its buffer. The optional argument
MAX-HEIGHT specifies a maximum height and defaults to the height
of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
minimum height and defaults to `window-min-height'. Both
MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
and header line and a bottom divider, if any.
If WINDOW is part of a horizontal combination and the value of
the option `fit-window-to-buffer-horizontally' is non-nil, adjust
WINDOW's width. The new width of WINDOW is calculated from the
maximum length of its buffer's lines that follow the current
start position of WINDOW. The optional argument MAX-WIDTH
specifies a maximum width and defaults to the width of WINDOW's
frame. The optional argument MIN-WIDTH specifies a minimum width
and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
are specified in columns and include fringes, margins, a
scrollbar and a vertical divider, if any.
Optional argument PRESERVE-SIZE non-nil means to preserve the
size of WINDOW (see `window-preserve-size').
Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
If WINDOW is its frame's root window and the option
`fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
adjust the frame's size.
Note that even if this function makes WINDOW large enough to show
_all_ parts of its buffer you might not see the first part when
WINDOW was scrolled. If WINDOW is resized horizontally, you will
not see the top of its buffer unless WINDOW starts at its minimum
accessible position."
(interactive)
(setq window (window-normalize-window window t))
(if (eq window (frame-root-window window))
(when fit-frame-to-buffer
;; Fit WINDOW's frame to buffer.
(fit-frame-to-buffer
(window-frame window)
max-height min-height max-width min-width
(and (memq fit-frame-to-buffer '(vertically horizontally))
fit-frame-to-buffer)))
(let* ((pixelwise window-resize-pixelwise)
(frame (window-frame window))
(char-height (frame-char-height frame)))
(cond
;; If WINDOW is vertically combined, try to resize it
;; vertically.
((and (not (eq fit-window-to-buffer-horizontally 'only))
(not (window-size-fixed-p window 'preserved))
(window-combined-p window))
(let* ((line-height (window-default-line-height window))
(total-height (window-size window nil pixelwise))
(min-height
;; Sanitize MIN-HEIGHT.
(if (numberp min-height)
;; Can't get smaller than `window-safe-min-height'.
(max (if pixelwise
(* line-height min-height)
min-height)
(if pixelwise
(window-safe-min-pixel-height window)
window-safe-min-height))
;; Preserve header and mode line if present.
(max (if pixelwise
(* line-height window-min-height)
window-min-height)
(window-min-size window nil window pixelwise))))
(max-height
;; Sanitize MAX-HEIGHT.
(if (numberp max-height)
(min
(+ total-height
(window-max-delta
window nil window nil t nil pixelwise))
(if pixelwise
(* line-height max-height)
(/ (* line-height max-height) line-height)))
(+ total-height (window-max-delta
window nil window nil t nil pixelwise))))
(height (+ (cdr (window-text-pixel-size
window nil t nil (frame-pixel-height frame) t))
(window-scroll-bar-height window)
(window-bottom-divider-width window))))
;; Vertically we always want to fit the entire buffer.
;; WINDOW'S height can't get larger than its frame's pixel
;; height. Its width remains fixed.
;; Round height.
(unless pixelwise
(setq height (/ (+ height char-height -1) char-height)))
(setq height (max min-height (min max-height height)))
(unless (= height total-height)
(window-preserve-size window)
(window-resize-no-error
window (- height total-height) nil window pixelwise)
(when preserve-size
(window-preserve-size window nil t)))))
;; If WINDOW is horizontally combined, try to resize it
;; horizontally.
((and fit-window-to-buffer-horizontally
(not (window-size-fixed-p window t 'preserved))
(window-combined-p window t))
(let* ((char-width (frame-char-width frame))
(total-width (window-size window t pixelwise))
(min-width
;; Sanitize MIN-WIDTH.
(if (numberp min-width)
;; Can't get smaller than `window-safe-min-width'.
(max (if pixelwise
(* char-width min-width)
min-width)
(if pixelwise
(window-safe-min-pixel-width window)
window-safe-min-width))
;; Preserve fringes, margins, scrollbars if present.
(max (if pixelwise
(* char-width window-min-width)
window-min-width)
(window-min-size window nil window pixelwise))))
(max-width
;; Sanitize MAX-WIDTH.
(if (numberp max-width)
(min (+ total-width
(window-max-delta
window t window nil t nil pixelwise))
(if pixelwise
(* char-width max-width)
max-width))
(+ total-width (window-max-delta
window t window nil t nil pixelwise))))
;; When fitting horizontally, assume that WINDOW's
;; start position remains unaltered. WINDOW can't get
;; wider than its frame's pixel width, its height
;; remains unaltered.
(width (+ (car (window-text-pixel-size
window (window-start window) nil
(frame-pixel-width (window-frame window))
;; Add one line-height to assure that
;; we're on the safe side. This
;; overshoots when the first line below
;; the bottom is wider than the window.
(* (window-body-height window pixelwise)
(if pixelwise 1 char-height))))
(- (* total-width (if pixelwise 1 char-width))
(window-body-width window t)))))
(unless pixelwise
(setq width (/ (+ width char-width -1) char-width)))
(setq width (max min-width (min max-width width)))
(unless (= width total-width)
(window-preserve-size window t)
(window-resize-no-error
window (- width total-width) t window pixelwise)
(when preserve-size
(window-preserve-size window t t)))))))))