Debug message fix
[sxemacs] / lisp / resize-minibuffer.el
1 ;;; resize-minibuffer.el --- dynamically resize minibuffer to display entire contents
2
3 ;; Copyright (C) 1990 Roland McGrath
4 ;; Copyright (C) 1993, 1994 Noah S. Friedman
5
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
11
12 ;; This file is part of SXEmacs.
13
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.
18
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.
23
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/>.
26
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!
31
32 ;;; Commentary:
33
34 ;; This file has received maintenance by the XEmacs development team.
35
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.
40
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.
46
47 ;; NOTE: The code to resize frames has not been tested under Lucid Emacs,
48 ;; because detached minibuffers are broken.
49
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.
53
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
56 ;; has no modeline.
57
58 ;; To use this package, put the following in your .emacs:
59 ;;
60 ;;     (autoload 'resize-minibuffer-mode "rsz-minibuf" nil t)
61 ;;
62 ;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
63
64 ;;; Code:
65
66 \f
67
68 (defgroup resize-minibuffer nil
69   "Dynamically resize minibuffer to display entire contents"
70   :group 'frames)
71
72
73 (defcustom resize-minibuffer-mode nil
74   "*If non-`nil', resize the minibuffer so its entire contents are visible."
75   :type 'boolean
76   :require 'rsz-minibuf
77   :group 'resize-minibuffer)
78
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)
85
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
88 ;; present. -- sjt
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'.")
94
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."
99   :type 'boolean
100   :group 'resize-minibuffer)
101
102 \f
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."
105   :type 'boolean
106   :group 'resize-minibuffer)
107
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.")
111
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."
116   :type 'boolean
117   :group 'resize-minibuffer)
118
119 \f
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.
124
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.
127
128 The variable `resize-minibuffer-mode' is set to t or nil depending on
129 whether this mode is active or not.
130
131 The maximum height to which the minibuffer can grow is controlled by the
132 variable `resize-minibuffer-window-max-height'.
133
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.
137
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
144 counterparts."
145   (interactive "p")
146   (or prefix (setq prefix 0))
147   (cond
148    ((>= prefix 0)
149     (setq resize-minibuffer-mode t))
150    (t
151     (setq resize-minibuffer-mode nil))))
152
153 (defun resize-minibuffer-setup ()
154   (cond
155    (resize-minibuffer-mode
156     (cond
157      ((and (not (eq 'tty (console-type)))
158            (eq 'only (plist-get (frame-properties) 'minibuffer)))
159       (and resize-minibuffer-frame
160            (progn
161              (make-local-hook 'minibuffer-exit-hook)
162              (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore
163                        nil t)
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)))))
168      (t
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)))))))
173
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.
178
179 Optional arguments START and END default to point-min and point-max,
180 respectively."
181   (or start (setq start (point-min)))
182   (or end   (setq end   (point-max)))
183   (if (= start end)
184       0
185     (save-excursion
186       (save-restriction
187         (widen)
188         (narrow-to-region start end)
189         (goto-char start)
190         (vertical-motion (buffer-size))))))
191
192 \f
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)
200          (setq lines (min
201                       lines
202                       resize-minibuffer-window-max-height)))
203     (or (if resize-minibuffer-window-exactly
204             (= lines height)
205           (<= lines height))
206         (enlarge-window (- lines height)))))
207
208 \f
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)
216          (setq lines (min
217                       lines
218                       resize-minibuffer-frame-max-height)))
219     (cond
220      ((> lines height)
221       (set-frame-size (selected-frame) (frame-width) lines))
222      ((and resize-minibuffer-frame-exactly
223            (> height (plist-get minibuffer-frame-plist 'height))
224            (< lines height))
225       (set-frame-size (selected-frame) (frame-width) lines)))))
226
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)))
231
232 \f
233 (provide 'rsz-minibuf)
234 (provide 'resize-minibuffer)
235
236 (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup)
237
238 ;;; resize-minibuffer.el ends here