+(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))))))
+
+(defun one-window-p (&optional nomini all-frames)
+ "Return non-nil if the selected window is the only window.
+Optional arg NOMINI non-nil means don't count the minibuffer
+even if it is active. Otherwise, the minibuffer is counted
+when it is active.
+
+The optional arg ALL-FRAMES t means count windows on all frames.
+If it is `visible', count windows on all visible frames.
+ALL-FRAMES nil or omitted means count only the selected frame,
+plus the minibuffer it uses (which may be on another frame).
+ALL-FRAMES 0 means count all windows in all visible or iconified frames.
+If ALL-FRAMES is anything else, count only the selected frame."
+ (let ((base-window (selected-window)))
+ (if (and nomini (eq base-window (minibuffer-window)))
+ (setq base-window (next-window base-window)))
+ (eq base-window
+ (next-window base-window (if nomini 'arg) all-frames))))
+
+(defun window-current-scroll-bars (&optional window)
+ "Return the current scroll-bar settings in window WINDOW.
+Value is a cons (VERTICAL . HORIZONTAL) where VERTICAL specifies the
+current location of the vertical scroll-bars (left, right, or nil),
+and HORIZONTAL specifies the current location of the horizontal scroll
+bars (top, bottom, or nil)."
+ (let ((vert (nth 2 (window-scroll-bars window)))
+ (hor nil))
+ (when (or (eq vert t) (eq hor t))
+ (let ((fcsb (frame-current-scroll-bars
+ (window-frame (or window (selected-window))))))
+ (if (eq vert t)
+ (setq vert (car fcsb)))
+ (if (eq hor t)
+ (setq hor (cdr fcsb)))))
+ (cons vert hor)))
+
+(defun walk-windows (proc &optional minibuf all-frames)
+ "Cycle through all visible windows, calling PROC for each one.
+PROC is called with a window as argument.
+
+Optional second arg MINIBUF t means count the minibuffer window even
+if not active. MINIBUF nil or omitted means count the minibuffer iff
+it is active. MINIBUF neither t nor nil means not to count the
+minibuffer even if it is active.
+
+Several frames may share a single minibuffer; if the minibuffer
+counts, all windows on all frames that share that minibuffer count
+too. Therefore, if you are using a separate minibuffer frame
+and the minibuffer is active and MINIBUF says it counts,
+`walk-windows' includes the windows in the frame from which you
+entered the minibuffer, as well as the minibuffer window.
+
+ALL-FRAMES is the optional third argument.
+ALL-FRAMES nil or omitted means cycle within the frames as specified above.
+ALL-FRAMES = `visible' means include windows on all visible frames.
+ALL-FRAMES = 0 means include windows on all visible and iconified frames.
+ALL-FRAMES = t means include windows on all frames including invisible frames.
+If ALL-FRAMES is a frame, it means include windows on that frame.
+Anything else means restrict to the selected frame."
+ ;; If we start from the minibuffer window, don't fail to come back to it.
+ (if (window-minibuffer-p (selected-window))
+ (setq minibuf t))
+ (save-selected-window
+ (if (framep all-frames)
+ (select-window (frame-first-window all-frames)))
+ (let* (walk-windows-already-seen
+ (walk-windows-current (selected-window)))
+ (while (progn
+ (setq walk-windows-current
+ (next-window walk-windows-current minibuf all-frames))
+ (not (memq walk-windows-current walk-windows-already-seen)))
+ (setq walk-windows-already-seen
+ (cons walk-windows-current walk-windows-already-seen))
+ (funcall proc walk-windows-current)))))
+
+(defun get-window-with-predicate (predicate &optional minibuf
+ all-frames default)
+ "Return a window satisfying PREDICATE.
+
+This function cycles through all visible windows using `walk-windows',
+calling PREDICATE on each one. PREDICATE is called with a window as
+argument. The first window for which PREDICATE returns a non-nil
+value is returned. If no window satisfies PREDICATE, DEFAULT is
+returned.
+
+Optional second arg MINIBUF t means count the minibuffer window even
+if not active. MINIBUF nil or omitted means count the minibuffer iff
+it is active. MINIBUF neither t nor nil means not to count the
+minibuffer even if it is active.
+
+Several frames may share a single minibuffer; if the minibuffer
+counts, all windows on all frames that share that minibuffer count
+too. Therefore, if you are using a separate minibuffer frame
+and the minibuffer is active and MINIBUF says it counts,
+`walk-windows' includes the windows in the frame from which you
+entered the minibuffer, as well as the minibuffer window.
+
+ALL-FRAMES is the optional third argument.
+ALL-FRAMES nil or omitted means cycle within the frames as specified above.
+ALL-FRAMES = `visible' means include windows on all visible frames.
+ALL-FRAMES = 0 means include windows on all visible and iconified frames.
+ALL-FRAMES = t means include windows on all frames including invisible frames.
+If ALL-FRAMES is a frame, it means include windows on that frame.
+Anything else means restrict to the selected frame."
+ (catch 'found
+ (walk-windows #'(lambda (window)
+ (when (funcall predicate window)
+ (throw 'found window)))
+ minibuf all-frames)
+ default))
+
+(defalias 'some-window 'get-window-with-predicate)
+
+(defun minibuffer-window-active-p (window)
+ "Return t if WINDOW (a minibuffer window) is now active."
+ (eq window (active-minibuffer-window)))
+\f