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