Update FSF's address.
[bpt/emacs.git] / lisp / rsz-mini.el
1 ;;; rsz-mini.el --- dynamically resize minibuffer to display entire contents
2
3 ;; Copyright (C) 1990, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
6 ;; Roland McGrath <roland@prep.ai.mit.edu>
7 ;; Maintainer: friedman@prep.ai.mit.edu
8 ;; Keywords: minibuffer, window, frame, display
9 ;; Status: Known to work in FSF GNU Emacs 19.26 and later.
10 ;; $Id: rsz-mini.el,v 1.10 1996/01/09 23:21:29 kwzh Exp erik $
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs 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 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs 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.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; This package allows the entire contents (or as much as possible) of the
32 ;; minibuffer to be visible at once when typing. As the end of a line is
33 ;; reached, the minibuffer will resize itself. When the user is done
34 ;; typing, the minibuffer will return to its original size.
35
36 ;; In window systems where it is possible to have a frame in which the
37 ;; minibuffer is the only window, the frame itself can be resized. In FSF
38 ;; GNU Emacs 19.22 and earlier, the frame may not be properly returned to
39 ;; its original size after it ceases to be active because
40 ;; `minibuffer-exit-hook' didn't exist until version 19.23.
41 ;;
42 ;; Prior to Emacs 19.26, minibuffer-exit-hook wasn't called after exiting
43 ;; from the minibuffer by hitting the quit char. That meant that the
44 ;; frame size restoration function wasn't being called in that case. In
45 ;; 19.26 or later, minibuffer-exit-hook should be called anyway.
46
47 ;; Note that the minibuffer and echo area are not the same! They simply
48 ;; happen to occupy roughly the same place on the frame. Messages put in
49 ;; the echo area will not cause any resizing by this package.
50
51 ;; This package is considered a minor mode but it doesn't put anything in
52 ;; minor-mode-alist because this mode is specific to the minibuffer, which
53 ;; has no mode line.
54
55 ;; To enable or disable this mode, use M-x resize-minibuffer-mode.
56
57 ;;; Code:
58
59 \f
60 ;;;###autoload
61 (defvar resize-minibuffer-mode nil
62 "*If non-`nil', resize the minibuffer so its entire contents are visible.")
63
64 ;;;###autoload
65 (defvar resize-minibuffer-window-max-height nil
66 "*Maximum size the minibuffer window is allowed to become.
67 If less than 1 or not a number, the limit is the height of the frame in
68 which the active minibuffer window resides.")
69
70 ;;;###autoload
71 (defvar resize-minibuffer-window-exactly t
72 "*Allow making minibuffer exactly the size to display all its contents.
73 If `nil', the minibuffer window can temporarily increase in size but
74 never get smaller while it is active. Any other value allows exact
75 resizing.")
76
77 ;;;###autoload
78 (defvar resize-minibuffer-frame nil
79 "*Allow changing the frame height of minibuffer frames.
80 If non-`nil' and the active minibuffer is the sole window in its frame,
81 allow changing the frame height.")
82
83 ;;;###autoload
84 (defvar resize-minibuffer-frame-max-height nil
85 "*Maximum size the minibuffer frame is allowed to become.
86 If less than 1 or not a number, there is no limit.")
87
88 ;;;###autoload
89 (defvar resize-minibuffer-frame-exactly t
90 "*Allow making minibuffer frame exactly the size to display all its contents.
91 If `nil', the minibuffer frame can temporarily increase in size but
92 never get smaller while it is active. Any other value allows exact
93 resizing.")
94
95 ;; Variable used to store the height of the minibuffer frame
96 ;; on entry, so it can be restored on exit. It is made local before it is
97 ;; modified. Do not use it yourself.
98 (defvar resize-minibuffer-frame-original-height nil)
99
100 \f
101 ;;;###autoload
102 (defun resize-minibuffer-mode (&optional prefix)
103 "Enable or disable resize-minibuffer mode.
104 A negative prefix argument disables this mode. A positive argument or
105 argument of 0 enables it.
106
107 When this minor mode is enabled, the minibuffer is dynamically resized to
108 contain the entire region of text put in it as you type.
109
110 The variable `resize-minibuffer-mode' is set to t or nil depending on
111 whether this mode is active or not.
112
113 The maximum height to which the minibuffer can grow is controlled by the
114 variable `resize-minibuffer-window-max-height'.
115
116 The variable `resize-minibuffer-window-exactly' determines whether the
117 minibuffer window should ever be shrunk to make it no larger than needed to
118 display its contents.
119
120 When using a window system, it is possible for a minibuffer to be the sole
121 window in a frame. Since that window is already its maximum size, the only
122 way to make more text visible at once is to increase the size of the frame.
123 The variable `resize-minibuffer-frame' controls whether this should be
124 done. The variables `resize-minibuffer-frame-max-height' and
125 `resize-minibuffer-frame-exactly' are analogous to their window
126 counterparts."
127 (interactive "p")
128 (or prefix (setq prefix 0))
129 (cond
130 ((>= prefix 0)
131 (setq resize-minibuffer-mode t))
132 (t
133 (setq resize-minibuffer-mode nil))))
134
135 (defun resize-minibuffer-setup ()
136 (cond
137 (resize-minibuffer-mode
138 (cond
139 ((and window-system
140 (eq 'only (cdr (assq 'minibuffer (frame-parameters)))))
141 ;; Checking for resize-minibuffer-frame is done outside the cond
142 ;; predicate because that should always be t if this is a minibuffer
143 ;; frame; it just shouldn't do anything if this flag is nil.
144 (and resize-minibuffer-frame
145 (progn
146 ;; Can't trust the height stored in minibuffer-frame-alist
147 ;; since the frame can be resized by the window manager and
148 ;; that variable isn't updated.
149 (make-local-variable 'resize-minibuffer-frame-original-height)
150 (setq resize-minibuffer-frame-original-height (frame-height))
151
152 (make-local-hook 'post-command-hook)
153 (add-hook 'post-command-hook 'resize-minibuffer-frame 'append t)
154
155 (make-local-hook 'minibuffer-exit-hook)
156 (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore
157 nil t)
158
159 (resize-minibuffer-frame))))
160 (t
161 (make-local-variable 'post-command-hook)
162 ;; Copy this because add-hook modifies the list structure.
163 (setq post-command-hook (copy-sequence post-command-hook))
164 (add-hook 'post-command-hook 'resize-minibuffer-window 'append)
165
166 (make-local-variable 'minibuffer-exit-hook)
167 (add-hook 'minibuffer-exit-hook 'resize-minibuffer-window-restore)
168
169 (resize-minibuffer-window))))))
170
171 (defun resize-minibuffer-count-window-lines (&optional start end)
172 "Return number of window lines occupied by text in region.
173 The number of window lines may be greater than the number of actual lines
174 in the buffer if any wrap on the display due to their length.
175
176 Optional arguments START and END default to point-min and point-max,
177 respectively."
178 (or start (setq start (point-min)))
179 (or end (setq end (point-max)))
180 (if (= start end)
181 0
182 (save-excursion
183 (save-restriction
184 (widen)
185 (narrow-to-region start end)
186 (goto-char start)
187 (vertical-motion (buffer-size))))))
188
189 \f
190 ;; Resize the minibuffer window to contain the minibuffer's contents.
191 (defun resize-minibuffer-window ()
192 (and (eq (selected-window) (minibuffer-window))
193 (let ((height (window-height))
194 (lines (1+ (resize-minibuffer-count-window-lines))))
195 (and (numberp resize-minibuffer-window-max-height)
196 (> resize-minibuffer-window-max-height 0)
197 (setq lines (min lines resize-minibuffer-window-max-height)))
198 (or (if resize-minibuffer-window-exactly
199 (= lines height)
200 (<= lines height))
201 (enlarge-window (- lines height))))))
202
203 ;; This resizes the minibuffer back to one line as soon as it is exited
204 ;; (e.g. when the user hits RET). This way, subsequent messages put in the
205 ;; echo area aren't cluttered with leftover minibuffer text.
206 ;; It should be called by minibuffer-exit-hook.
207 ;;
208 ;; Note that because it calls sit-for to force a screen update, strange
209 ;; things may happen in the minibuffer, such as unexpanded partial
210 ;; completions by complete.el showing their completion.
211 ;; If this bothers you, just redefine this function to do nothing, in, say,
212 ;; your after-load-alist. Perhaps there should be an option variable,
213 ;; but I don't know if there's really any demand for it.
214 ;; (Clobbering this definition is harmless because eventually emacs restores
215 ;; its idea of the minibuffer window size when the minibuffer isn't in use
216 ;; anyway; this is just a kludge because of the timing for that update).
217 (defun resize-minibuffer-window-restore ()
218 (cond
219 ((not (eq (minibuffer-window) (selected-window))))
220 ((> (window-height) 1)
221 (enlarge-window (- 1 (window-height)))
222 (sit-for 0))))
223
224 \f
225 ;; Resize the minibuffer frame to contain the minibuffer's contents.
226 ;; The minibuffer frame must be the current frame.
227 (defun resize-minibuffer-frame ()
228 (let ((height (frame-height))
229 (lines (1+ (resize-minibuffer-count-window-lines))))
230 (and (numberp resize-minibuffer-frame-max-height)
231 (> resize-minibuffer-frame-max-height 0)
232 (setq lines (min lines resize-minibuffer-frame-max-height)))
233 (cond
234 ((> lines height)
235 (set-frame-size (selected-frame) (frame-width) lines))
236 ((and resize-minibuffer-frame-exactly
237 (> height resize-minibuffer-frame-original-height)
238 (< lines height))
239 (set-frame-size (selected-frame) (frame-width) lines)))))
240
241 ;; Restore the original height of the frame.
242 ;; resize-minibuffer-frame-original-height is set in
243 ;; resize-minibuffer-setup.
244 (defun resize-minibuffer-frame-restore ()
245 (set-frame-size (selected-frame)
246 (frame-width)
247 resize-minibuffer-frame-original-height))
248
249 \f
250 (provide 'rsz-mini)
251
252 (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup)
253 (resize-minibuffer-mode)
254
255 ;; rsz-mini.el ends here