+ ;; Do all the work in WINDOW and its buffer and restore the selected
+ ;; window and the current buffer when we're done.
+ (let ((old-buffer (current-buffer))
+ value)
+ (with-selected-window (or window (setq window (selected-window)))
+ (set-buffer (window-buffer))
+ ;; Use `condition-case' to handle any fixed-size windows and other
+ ;; pitfalls nearby.
+ (condition-case nil
+ (let* (;; MIN-HEIGHT must not be less than 1 and defaults to
+ ;; `window-min-height'.
+ (min-height (max (or min-height window-min-height) 1))
+ (max-window-height
+ ;; Maximum height of any window on this frame.
+ (min (window-height (frame-root-window)) (frame-height)))
+ ;; MAX-HEIGHT must not be larger than max-window-height and
+ ;; defaults to max-window-height.
+ (max-height
+ (min (or max-height max-window-height) max-window-height))
+ (desired-height
+ ;; The height necessary to show all of WINDOW's buffer,
+ ;; constrained by MIN-HEIGHT and MAX-HEIGHT.
+ (max
+ (min
+ ;; For an empty buffer `count-screen-lines' returns zero.
+ ;; Even in that case we need one line for the cursor.
+ (+ (max (count-screen-lines) 1)
+ ;; For non-minibuffers count the mode line, if any.
+ (if (and (not (window-minibuffer-p)) mode-line-format)
+ 1 0)
+ ;; Count the header line, if any.
+ (if header-line-format 1 0))
+ max-height)
+ min-height))
+ (delta
+ ;; How much the window height has to change.
+ (if (= (window-height) (window-height (frame-root-window)))
+ ;; Don't try to resize a full-height window.
+ 0
+ (- desired-height (window-height))))
+ ;; Do something reasonable so `enlarge-window' can make
+ ;; windows as small as MIN-HEIGHT.
+ (window-min-height (min min-height window-min-height)))
+ ;; Don't try to redisplay with the cursor at the end on its
+ ;; own line--that would force a scroll and spoil things.
+ (when (and (eobp) (bolp) (not (bobp)))
+ (set-window-point window (1- (window-point))))
+ ;; Adjust WINDOW's height to the nominally correct one
+ ;; (which may actually be slightly off because of variable
+ ;; height text, etc).
+ (unless (zerop delta)
+ (enlarge-window delta))
+ ;; `enlarge-window' might have deleted WINDOW, so make sure
+ ;; WINDOW's still alive for the remainder of this.
+ ;; Note: Deleting WINDOW is clearly counter-intuitive in
+ ;; this context, but we can't do much about it given the
+ ;; current semantics of `enlarge-window'.
+ (when (window-live-p window)
+ ;; Check if the last line is surely fully visible. If
+ ;; not, enlarge the window.
+ (let ((end (save-excursion
+ (goto-char (point-max))
+ (when (and (bolp) (not (bobp)))
+ ;; Don't include final newline.
+ (backward-char 1))
+ (when truncate-lines
+ ;; If line-wrapping is turned off, test the
+ ;; beginning of the last line for
+ ;; visibility instead of the end, as the
+ ;; end of the line could be invisible by
+ ;; virtue of extending past the edge of the
+ ;; window.
+ (forward-line 0))
+ (point))))
+ (set-window-vscroll window 0)
+ (while (and (< desired-height max-height)
+ (= desired-height (window-height))
+ (not (pos-visible-in-window-p end)))
+ (enlarge-window 1)
+ (setq desired-height (1+ desired-height))))
+ ;; Return non-nil only if nothing "bad" happened.
+ (setq value t)))
+ (error nil)))
+ (when (buffer-live-p old-buffer)
+ (set-buffer old-buffer))
+ value))