+(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,
+unless you explicitly change the size, or Emacs has no other choice.")
+(make-variable-buffer-local 'window-size-fixed)
+
+(defmacro save-selected-window (&rest body)
+ "Execute BODY, then select the window that was selected before BODY.
+The value returned is the value of the last form in BODY.
+
+This macro saves and restores the current buffer, since otherwise
+its normal operation could potentially make a different
+buffer current. It does not alter the buffer list ordering.
+
+This macro saves and restores the selected window, as well as
+the selected window in each frame. 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. If the selected window is
+no longer live, then whatever window is selected at the end of
+BODY remains selected."
+ `(let ((save-selected-window-window (selected-window))
+ ;; It is necessary to save all of these, because calling
+ ;; select-window changes frame-selected-window for whatever
+ ;; frame that window is in.
+ (save-selected-window-alist
+ (mapcar (lambda (frame) (list frame (frame-selected-window frame)))
+ (frame-list))))
+ (save-current-buffer
+ (unwind-protect
+ (progn ,@body)
+ (dolist (elt save-selected-window-alist)
+ (and (frame-live-p (car elt))
+ (window-live-p (cadr elt))
+ (set-frame-selected-window (car elt) (cadr elt))))
+ (if (window-live-p save-selected-window-window)
+ (select-window save-selected-window-window))))))
+
+(defun window-body-height (&optional window)
+ "Return number of lines in window WINDOW for actual buffer text.
+This does not include the mode line (if any) or the header line (if any)."
+ (or window (setq window (selected-window)))
+ (if (window-minibuffer-p window)
+ (window-height window)
+ (with-current-buffer (window-buffer window)
+ (max 1 (- (window-height window)
+ (if mode-line-format 1 0)
+ (if header-line-format 1 0))))))
+