(Info-goto-node, Info-search):
[bpt/emacs.git] / lisp / rsz-mini.el
index 8d90e0c..684a55c 100644 (file)
@@ -1,13 +1,13 @@
 ;;; rsz-mini.el --- dynamically resize minibuffer to display entire contents
 
-;;; Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1993, 1994, 1995 Free Software Foundation, Inc.
 
-;;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
-;;;         Roland McGrath <roland@prep.ai.mit.edu>
-;;; Maintainer: friedman@prep.ai.mit.edu
-;;; Keywords: minibuffer, window, frame, display
-;;; Status: Known to work in FSF GNU Emacs 19.26 and later.
-;;; $Id: rsz-mini.el,v 1.4 1994/06/22 22:14:28 friedman Exp friedman $
+;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
+;;         Roland McGrath <roland@prep.ai.mit.edu>
+;; Maintainer: friedman@prep.ai.mit.edu
+;; Keywords: minibuffer, window, frame, display
+;; Status: Known to work in FSF GNU Emacs 19.26 and later.
+;; $Id: rsz-mini.el,v 1.7 1994/07/13 17:19:23 friedman Exp friedman $
 
 ;; This file is part of GNU Emacs.
 
 
 ;;; Commentary:
 
-;;; This package allows the entire contents (or as much as possible) of the
-;;; minibuffer to be visible at once when typing.  As the end of a line is
-;;; reached, the minibuffer will resize itself.  When the user is done
-;;; typing, the minibuffer will return to its original size.
-
-;;; In window systems where it is possible to have a frame in which the
-;;; minibuffer is the only window, the frame itself can be resized.  In FSF
-;;; GNU Emacs 19.22 and earlier, the frame may not be properly returned to
-;;; its original size after it ceases to be active because
-;;; `minibuffer-exit-hook' didn't exist until version 19.23.
-;;;
-;;; Prior to Emacs 19.26, minibuffer-exit-hook wasn't called after exiting
-;;; from the minibuffer by hitting the quit char.  That meant that the
-;;; frame size restoration function wasn't being called in that case.  In
-;;; 19.26 or later, minibuffer-exit-hook should be called anyway.
-
-;;; Note that the minibuffer and echo area are not the same!  They simply
-;;; happen to occupy roughly the same place on the frame.  Messages put in
-;;; the echo area will not cause any resizing by this package.
-
-;;; This package is considered a minor mode but it doesn't put anything in
-;;; minor-mode-alist because this mode is specific to the minibuffer, which
-;;; has no mode line.
-
-;;; To use this package, put the following in your .emacs:
-;;;
-;;;     (autoload 'resize-minibuffer-mode "rsz-mini" nil t)
-;;;
-;;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
-;;; Simply loading this file will also enable it.
+;; This package allows the entire contents (or as much as possible) of the
+;; minibuffer to be visible at once when typing.  As the end of a line is
+;; reached, the minibuffer will resize itself.  When the user is done
+;; typing, the minibuffer will return to its original size.
+
+;; In window systems where it is possible to have a frame in which the
+;; minibuffer is the only window, the frame itself can be resized.  In FSF
+;; GNU Emacs 19.22 and earlier, the frame may not be properly returned to
+;; its original size after it ceases to be active because
+;; `minibuffer-exit-hook' didn't exist until version 19.23.
+;;
+;; Prior to Emacs 19.26, minibuffer-exit-hook wasn't called after exiting
+;; from the minibuffer by hitting the quit char.  That meant that the
+;; frame size restoration function wasn't being called in that case.  In
+;; 19.26 or later, minibuffer-exit-hook should be called anyway.
+
+;; Note that the minibuffer and echo area are not the same!  They simply
+;; happen to occupy roughly the same place on the frame.  Messages put in
+;; the echo area will not cause any resizing by this package.
+
+;; This package is considered a minor mode but it doesn't put anything in
+;; minor-mode-alist because this mode is specific to the minibuffer, which
+;; has no mode line.
+
+;; To use this package, put the following in your .emacs:
+;;
+;;     (autoload 'resize-minibuffer-mode "rsz-mini" nil t)
+;;
+;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
+;; Simply loading this file will also enable it.
 
 ;;; Code:
 
@@ -142,23 +142,36 @@ counterparts."
     (cond
      ((and window-system
            (eq 'only (cdr (assq 'minibuffer (frame-parameters)))))
+      ;; Checking for resize-minibuffer-frame is done outside the cond
+      ;; predicate because that should always be t if this is a minibuffer
+      ;; frame; it just shouldn't do anything if this flag is nil.
       (and resize-minibuffer-frame
            (progn
-             ;; Squirrel away the current height of the frame so we can
-             ;; restore it later.  We do this rather than trusting the
-             ;; value in minibuffer-frame-alist since the frame can be
-             ;; resized by the window manager and that variable isn't updated.
+             ;; Can't trust the height stored in minibuffer-frame-alist
+             ;; since the frame can be resized by the window manager and
+             ;; that variable isn't updated.
              (make-local-variable 'resize-minibuffer-frame-original-height)
              (setq resize-minibuffer-frame-original-height (frame-height))
+
+             (make-local-variable 'post-command-hook)
+            ;; Copy this because add-hook modifies the list structure.
+            (setq post-command-hook (copy-sequence post-command-hook))
+             (add-hook 'post-command-hook 'resize-minibuffer-frame 'append)
+
              (make-local-variable 'minibuffer-exit-hook)
              (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore)
-             (make-local-variable 'post-command-hook)
-             (add-hook 'post-command-hook 'resize-minibuffer-frame 'append))))
+
+             (resize-minibuffer-frame))))
      (t
       (make-local-variable 'post-command-hook)
+      ;; Copy this because add-hook modifies the list structure.
+      (setq post-command-hook (copy-sequence post-command-hook))
       (add-hook 'post-command-hook 'resize-minibuffer-window 'append)
+
       (make-local-variable 'minibuffer-exit-hook)
-      (add-hook 'minibuffer-exit-hook 'resize-minibuffer-window-restore))))))
+      (add-hook 'minibuffer-exit-hook 'resize-minibuffer-window-restore)
+
+      (resize-minibuffer-window))))))
 
 (defun resize-minibuffer-count-window-lines (&optional start end)
   "Return number of window lines occupied by text in region.
@@ -180,17 +193,17 @@ respectively."
 
 \f
 ;; Resize the minibuffer window to contain the minibuffer's contents.
-;; The minibuffer window must be current.
 (defun resize-minibuffer-window ()
-  (let ((height (window-height))
-        (lines (1+ (resize-minibuffer-count-window-lines))))
-    (and (numberp resize-minibuffer-window-max-height)
-         (> resize-minibuffer-window-max-height 0)
-         (setq lines (min lines resize-minibuffer-window-max-height)))
-    (or (if resize-minibuffer-window-exactly
-            (= lines height)
-          (<= lines height))
-        (enlarge-window (- lines height)))))
+  (and (eq (selected-window) (minibuffer-window))
+       (let ((height (window-height))
+             (lines (1+ (resize-minibuffer-count-window-lines))))
+         (and (numberp resize-minibuffer-window-max-height)
+              (> resize-minibuffer-window-max-height 0)
+              (setq lines (min lines resize-minibuffer-window-max-height)))
+         (or (if resize-minibuffer-window-exactly
+                 (= lines height)
+               (<= lines height))
+             (enlarge-window (- lines height))))))
 
 ;; This resizes the minibuffer back to one line as soon as it is exited
 ;; (e.g. when the user hits RET).  This way, subsequent messages put in the
@@ -208,6 +221,7 @@ respectively."
 ;; anyway; this is just a kludge because of the timing for that update).
 (defun resize-minibuffer-window-restore ()
   (cond
+   ((not (eq (minibuffer-window) (selected-window))))
    ((> (window-height) 1)
     (enlarge-window (- 1 (window-height)))
     (sit-for 0))))