1 ;;; resize-minibuffer.el --- dynamically resize minibuffer to display entire contents
3 ;; Copyright (C) 1990 Roland McGrath
4 ;; Copyright (C) 1993, 1994 Noah S. Friedman
6 ;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
7 ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
8 ;; Modified for Lucid Emacs By: Peter Stout <pds@cs.cmu.edu>
9 ;; Maintainer: friedman@prep.ai.mit.edu
10 ;; Keywords: minibuffer, window, frames, display
12 ;; This file is part of SXEmacs.
14 ;; SXEmacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; SXEmacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Synched up with: Not synched. Copied from rsz-minibuf.el to
28 ;;; resize-minibuffer.el to avoid current problems where, when there's a
29 ;;; file in dump and a file with the same name in packages, a dumping
30 ;;; XEmacs will find the wrong one!
34 ;; This file has received maintenance by the XEmacs development team.
36 ;; This package allows the entire contents (or as much as possible) of the
37 ;; minibuffer to be visible at once when typing. As the end of a line is
38 ;; reached, the minibuffer will resize itself. When the user is done
39 ;; typing, the minibuffer will return to its original size.
41 ;; In window systems where it is possible to have a frame in which the
42 ;; minibuffer is the only window, the frame itself can be resized. In FSF
43 ;; GNU Emacs 19.22 and earlier, the frame may not be properly returned to
44 ;; its original size after it ceases to be active because
45 ;; `minibuffer-exit-hook' didn't exist until version 19.23.
47 ;; NOTE: The code to resize frames has not been tested under Lucid Emacs,
48 ;; because detached minibuffers are broken.
50 ;; Note that the minibuffer and echo area are not the same! They simply
51 ;; happen to occupy roughly the same place on the frame. Messages put in
52 ;; the echo area will not cause any resizing by this package.
54 ;; This package is considered a minor mode but it doesn't put anything in
55 ;; minor-mode-alist because this mode is specific to the minibuffer, which
58 ;; To use this package, put the following in your .emacs:
60 ;; (autoload 'resize-minibuffer-mode "rsz-minibuf" nil t)
62 ;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
68 (defgroup resize-minibuffer nil
69 "Dynamically resize minibuffer to display entire contents"
73 (defcustom resize-minibuffer-mode nil
74 "*If non-`nil', resize the minibuffer so its entire contents are visible."
77 :group 'resize-minibuffer)
79 (defcustom resize-minibuffer-window-max-height nil
80 "*Maximum size the minibuffer window is allowed to become.
81 If less than 1 or not a number, the limit is the height of the frame in
82 which the active minibuffer window resides."
83 :type '(choice (const nil) integer)
84 :group 'resize-minibuffer)
86 ;; #### Yeah, I know. The relation between the echo area and the
87 ;; minibuffer needs rethinking. It's not really possible to unify them at
89 (defcustom resize-minibuffer-idle-height nil
90 "When minibuffer is idle, crop its window to this height.
91 Must be a positive integer or nil. nil indicates no limit.
92 Effective only when `undisplay-echo-area-function' respects it. One such
93 function is `undisplay-echo-area-resize-window'.")
95 (defcustom resize-minibuffer-window-exactly t
96 "*If non-`nil', make minibuffer exactly the size needed to display all its contents.
97 Otherwise, the minibuffer window can temporarily increase in size but
98 never get smaller while it is active."
100 :group 'resize-minibuffer)
103 (defcustom resize-minibuffer-frame nil
104 "*If non-`nil' and the active minibuffer is the sole window in its frame, allow changing the frame height."
106 :group 'resize-minibuffer)
108 (defcustom resize-minibuffer-frame-max-height nil
109 "*Maximum size the minibuffer frame is allowed to become.
110 If less than 1 or not a number, there is no limit.")
112 (defcustom resize-minibuffer-frame-exactly nil
113 "*If non-`nil', make minibuffer frame exactly the size needed to display all its contents.
114 Otherwise, the minibuffer frame can temporarily increase in size but
115 never get smaller while it is active."
117 :group 'resize-minibuffer)
120 (defun resize-minibuffer-mode (&optional prefix)
121 "Enable or disable resize-minibuffer mode.
122 A negative prefix argument disables this mode. A positive argument or
123 argument of 0 enables it.
125 When this minor mode is enabled, the minibuffer is dynamically resized to
126 contain the entire region of text put in it as you type.
128 The variable `resize-minibuffer-mode' is set to t or nil depending on
129 whether this mode is active or not.
131 The maximum height to which the minibuffer can grow is controlled by the
132 variable `resize-minibuffer-window-max-height'.
134 The variable `resize-minibuffer-window-exactly' determines whether the
135 minibuffer window should ever be shrunk to make it no larger than needed to
136 display its contents.
138 When using a window system, it is possible for a minibuffer to be the sole
139 window in a frame. Since that window is already its maximum size, the only
140 way to make more text visible at once is to increase the size of the frame.
141 The variable `resize-minibuffer-frame' controls whether this should be
142 done. The variables `resize-minibuffer-frame-max-height' and
143 `resize-minibuffer-frame-exactly' are analogous to their window
146 (or prefix (setq prefix 0))
149 (setq resize-minibuffer-mode t))
151 (setq resize-minibuffer-mode nil))))
153 (defun resize-minibuffer-setup ()
155 (resize-minibuffer-mode
157 ((and (not (eq 'tty (console-type)))
158 (eq 'only (plist-get (frame-properties) 'minibuffer)))
159 (and resize-minibuffer-frame
161 (make-local-hook 'minibuffer-exit-hook)
162 (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore
164 (make-local-hook 'post-command-hook)
165 (add-hook 'post-command-hook 'resize-minibuffer-frame nil t)
166 (unless (and-boundp 'icomplete-mode icomplete-mode)
167 (resize-minibuffer-frame)))))
169 (make-local-hook 'post-command-hook)
170 (add-hook 'post-command-hook 'resize-minibuffer-window nil t)
171 (unless (and-boundp 'icomplete-mode icomplete-mode)
172 (resize-minibuffer-window)))))))
174 (defun resize-minibuffer-count-window-lines (&optional start end)
175 "Return number of window lines occupied by text in region.
176 The number of window lines may be greater than the number of actual lines
177 in the buffer if any wrap on the display due to their length.
179 Optional arguments START and END default to point-min and point-max,
181 (or start (setq start (point-min)))
182 (or end (setq end (point-max)))
188 (narrow-to-region start end)
190 (vertical-motion (buffer-size))))))
193 ;; Resize the minibuffer window to contain the minibuffer's contents.
194 ;; The minibuffer must be the current window.
195 (defun resize-minibuffer-window ()
196 (let ((height (window-height))
197 (lines (1+ (resize-minibuffer-count-window-lines))))
198 (and (numberp resize-minibuffer-window-max-height)
199 (> resize-minibuffer-window-max-height 0)
202 resize-minibuffer-window-max-height)))
203 (or (if resize-minibuffer-window-exactly
206 (enlarge-window (- lines height)))))
209 ;; Resize the minibuffer frame to contain the minibuffer's contents.
210 ;; The minibuffer frame must be the current frame.
211 (defun resize-minibuffer-frame ()
212 (let ((height (frame-height))
213 (lines (1+ (resize-minibuffer-count-window-lines))))
214 (and (numberp resize-minibuffer-frame-max-height)
215 (> resize-minibuffer-frame-max-height 0)
218 resize-minibuffer-frame-max-height)))
221 (set-frame-size (selected-frame) (frame-width) lines))
222 ((and resize-minibuffer-frame-exactly
223 (> height (plist-get minibuffer-frame-plist 'height))
225 (set-frame-size (selected-frame) (frame-width) lines)))))
227 ;; Restore the original height of the frame.
228 (defun resize-minibuffer-frame-restore ()
229 (set-frame-size (selected-frame) (frame-width)
230 (plist-get minibuffer-frame-plist 'height)))
233 (provide 'rsz-minibuf)
234 (provide 'resize-minibuffer)
236 (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup)
238 ;;; resize-minibuffer.el ends here