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