Setup auto-fill-chars.
[bpt/emacs.git] / lisp / rsz-mini.el
1 ;;; rsz-mini.el --- dynamically resize minibuffer to display entire contents
2
3 ;; Copyright (C) 1990, 1993-1995, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Roland McGrath <roland@gnu.org>
7 ;; Maintainer: Noah Friedman <friedman@splode.com>
8 ;; Keywords: minibuffer, window, frame, display
9
10 ;; $Id: rsz-mini.el,v 1.22 1998/09/11 01:36:54 friedman Exp $
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
38 ;; 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 (defgroup resize-minibuffer nil
61 "Dynamically resize minibuffer to display entire contents"
62 :group 'frames)
63
64 ;;;###autoload
65 (defcustom resize-minibuffer-mode nil
66 "*If non-`nil', resize the minibuffer so its entire contents are visible.
67 You must modify via \\[customize] for this variable to have an effect."
68 :set (lambda (symbol value)
69 (resize-minibuffer-mode (if value 1 -1)))
70 :initialize 'custom-initialize-default
71 :type 'boolean
72 :group 'resize-minibuffer
73 :require 'rsz-mini)
74
75 ;;;###autoload
76 (defcustom resize-minibuffer-window-max-height nil
77 "*Maximum size the minibuffer window is allowed to become.
78 If less than 1 or not a number, the limit is the height of the frame in
79 which the active minibuffer window resides."
80 :type '(choice (const nil) integer)
81 :group 'resize-minibuffer)
82
83 ;;;###autoload
84 (defcustom resize-minibuffer-window-exactly t
85 "*Allow making minibuffer exactly the size to display all its contents.
86 If `nil', the minibuffer window can temporarily increase in size but
87 never get smaller while it is active. Any other value allows exact
88 resizing."
89 :type 'boolean
90 :group 'resize-minibuffer)
91
92 ;;;###autoload
93 (defcustom resize-minibuffer-frame nil
94 "*Allow changing the frame height of minibuffer frames.
95 If non-`nil' and the active minibuffer is the sole window in its frame,
96 allow changing the frame height."
97 :type 'boolean
98 :group 'resize-minibuffer)
99
100 ;;;###autoload
101 (defcustom resize-minibuffer-frame-max-height nil
102 "*Maximum size the minibuffer frame is allowed to become.
103 If less than 1 or not a number, there is no limit.")
104
105 ;;;###autoload
106 (defcustom resize-minibuffer-frame-exactly t
107 "*Allow making minibuffer frame exactly the size to display all its contents.
108 If `nil', the minibuffer frame can temporarily increase in size but
109 never get smaller while it is active. Any other value allows exact
110 resizing."
111 :type 'boolean
112 :group 'resize-minibuffer)
113
114 ;; Variable used to store the height of the minibuffer frame
115 ;; on entry, so it can be restored on exit. It is made local before it is
116 ;; modified. Do not use it yourself.
117 (defvar resize-minibuffer-frame-original-height nil)
118
119 \f
120 ;;;###autoload
121 (defun resize-minibuffer-mode (&optional prefix)
122 "Toggle resize-minibuffer mode.
123 With argument, enable resize-minibuffer mode if and only if argument
124 is positive.
125
126 When this minor mode is enabled, the minibuffer is dynamically resized to
127 contain the entire region of text put in it as you type.
128
129 The variable `resize-minibuffer-mode' is set to t or nil depending on
130 whether this mode is active or not.
131
132 The maximum height to which the minibuffer can grow is controlled by the
133 variable `resize-minibuffer-window-max-height'.
134
135 The variable `resize-minibuffer-window-exactly' determines whether the
136 minibuffer window should ever be shrunk to make it no larger than needed to
137 display its contents.
138
139 When using a window system, it is possible for a minibuffer to be the sole
140 window in a frame. Since that window is already its maximum size, the only
141 way to make more text visible at once is to increase the size of the frame.
142 The variable `resize-minibuffer-frame' controls whether this should be
143 done. The variables `resize-minibuffer-frame-max-height' and
144 `resize-minibuffer-frame-exactly' are analogous to their window
145 counterparts."
146 (interactive "P")
147 (setq resize-minibuffer-mode
148 (if prefix
149 (> (prefix-numeric-value prefix) 0)
150 (not resize-minibuffer-mode)))
151 (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup))
152
153 (defun resize-minibuffer-setup ()
154 (cond
155 (resize-minibuffer-mode
156 (cond
157 ((and window-system
158 (eq 'only (cdr (assq 'minibuffer (frame-parameters)))))
159 ;; Checking for resize-minibuffer-frame is done outside the cond
160 ;; predicate because that should always be t if this is a minibuffer
161 ;; frame; it just shouldn't do anything if this flag is nil.
162 (and resize-minibuffer-frame
163 (progn
164 ;; Can't trust the height stored in minibuffer-frame-alist
165 ;; since the frame can be resized by the window manager and
166 ;; that variable isn't updated.
167 (make-local-variable 'resize-minibuffer-frame-original-height)
168 (setq resize-minibuffer-frame-original-height (frame-height))
169
170 (make-local-hook 'post-command-hook)
171 (add-hook 'post-command-hook 'resize-minibuffer-frame 'append t)
172
173 (make-local-hook 'minibuffer-exit-hook)
174 (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore
175 nil t)
176
177 (resize-minibuffer-frame))))
178 (t
179 (make-local-variable 'post-command-hook)
180 ;; Copy this because add-hook modifies the list structure.
181 (setq post-command-hook (copy-sequence post-command-hook))
182 (add-hook 'post-command-hook 'resize-minibuffer-window 'append)
183
184 (make-local-variable 'minibuffer-exit-hook)
185 (add-hook 'minibuffer-exit-hook 'resize-minibuffer-window-restore)
186
187 (resize-minibuffer-window))))))
188
189 (defun resize-minibuffer-count-window-lines (&optional start end)
190 "Return number of window lines occupied by text in region.
191 The number of window lines may be greater than the number of actual lines
192 in the buffer if any wrap on the display due to their length.
193
194 Optional arguments START and END default to point-min and point-max,
195 respectively."
196 (or start (setq start (point-min)))
197 (or end (setq end (point-max)))
198 (if (= start end)
199 0
200 (save-excursion
201 (save-restriction
202 (widen)
203 (narrow-to-region start end)
204 (goto-char start)
205 (vertical-motion (buffer-size))))))
206
207 \f
208 ;; Resize the minibuffer window to contain the minibuffer's contents.
209 (defun resize-minibuffer-window ()
210 (and (eq (selected-window) (minibuffer-window))
211 (let ((height (window-height))
212 (lines (1+ (resize-minibuffer-count-window-lines))))
213 (and (numberp resize-minibuffer-window-max-height)
214 (> resize-minibuffer-window-max-height 0)
215 (setq lines (min lines resize-minibuffer-window-max-height)))
216 (or (if resize-minibuffer-window-exactly
217 (= lines height)
218 (<= lines height))
219 (enlarge-window (- lines height))))))
220
221 ;; This resizes the minibuffer back to one line as soon as it is exited
222 ;; (e.g. when the user hits RET). This way, subsequent messages put in the
223 ;; echo area aren't cluttered with leftover minibuffer text.
224 ;; It should be called by minibuffer-exit-hook.
225 ;;
226 ;; Note that because it calls sit-for to force a screen update, strange
227 ;; things may happen in the minibuffer, such as unexpanded partial
228 ;; completions by complete.el showing their completion.
229 ;; If this bothers you, just redefine this function to do nothing, in, say,
230 ;; your after-load-alist. Perhaps there should be an option variable,
231 ;; but I don't know if there's really any demand for it.
232 ;; (Clobbering this definition is harmless because eventually emacs restores
233 ;; its idea of the minibuffer window size when the minibuffer isn't in use
234 ;; anyway; this is just a kludge because of the timing for that update).
235 (defun resize-minibuffer-window-restore ()
236 (cond
237 ((not (eq (minibuffer-window) (selected-window))))
238 ((> (window-height) 1)
239 (enlarge-window (- 1 (window-height)))
240 (sit-for 0))))
241
242 \f
243 ;; Resize the minibuffer frame to contain the minibuffer's contents.
244 ;; The minibuffer frame must be the current frame.
245 (defun resize-minibuffer-frame ()
246 (let ((height (frame-height))
247 (lines (1+ (resize-minibuffer-count-window-lines))))
248 (and (numberp resize-minibuffer-frame-max-height)
249 (> resize-minibuffer-frame-max-height 0)
250 (setq lines (min lines resize-minibuffer-frame-max-height)))
251 (cond
252 ((> lines height)
253 (set-frame-size (window-frame (minibuffer-window)) (frame-width) lines))
254 ((and resize-minibuffer-frame-exactly
255 (> height resize-minibuffer-frame-original-height)
256 (< lines height))
257 (set-frame-size (window-frame (minibuffer-window))
258 (frame-width) lines)))))
259
260 ;; Restore the original height of the frame.
261 ;; resize-minibuffer-frame-original-height is set in
262 ;; resize-minibuffer-setup.
263 (defun resize-minibuffer-frame-restore ()
264 (set-frame-size (window-frame (minibuffer-window))
265 (frame-width)
266 resize-minibuffer-frame-original-height))
267
268 (if resize-minibuffer-mode
269 (resize-minibuffer-mode 1))
270
271 (provide 'rsz-mini)
272
273 ;; rsz-mini.el ends here