(Man-build-man-command): Use manual-program, not hardcoded name.
[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 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.23.
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 ;;; Note that the minibuffer and echo area are not the same! They simply
41 ;;; happen to occupy roughly the same place on the frame. Messages put in
42 ;;; the echo area will not cause any resizing by this package.
43
44 ;;; This package is considered a minor mode but it doesn't put anything in
45 ;;; minor-mode-alist because this mode is specific to the minibuffer, which
46 ;;; has no mode line.
47
48 ;;; To use this package, put the following in your .emacs:
49 ;;;
50 ;;; (autoload 'resize-minibuffer-mode "rsz-mini" nil t)
51 ;;;
52 ;;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
53
54 ;;; Code:
55
56 \f
57 ;;;###autoload
58 (defvar resize-minibuffer-mode nil
59 "*If non-`nil', resize the minibuffer so its entire contents are visible.")
60
61 ;;;###autoload
62 (defvar resize-minibuffer-window-max-height nil
63 "*Maximum size the minibuffer window is allowed to become.
64 If less than 1 or not a number, the limit is the height of the frame in
65 which the active minibuffer window resides.")
66
67 ;;;###autoload
68 (defvar resize-minibuffer-window-exactly t
69 "*If non-`nil', make minibuffer exactly the size needed to display all its contents.
70 Otherwise, the minibuffer window can temporarily increase in size but
71 never get smaller while it is active.")
72
73 \f
74 ;;;###autoload
75 (defvar resize-minibuffer-frame nil
76 "*If non-`nil' and the active minibuffer is the sole window in its frame, allow changing the frame height.")
77
78 ;;;###autoload
79 (defvar resize-minibuffer-frame-max-height nil
80 "*Maximum size the minibuffer frame is allowed to become.
81 If less than 1 or not a number, there is no limit.")
82
83 ;;;###autoload
84 (defvar resize-minibuffer-frame-exactly nil
85 "*If non-`nil', make minibuffer frame exactly the size needed to display all its contents.
86 Otherwise, the minibuffer frame can temporarily increase in size but
87 never get smaller while it is active.")
88
89 \f
90 ;;;###autoload
91 (defun resize-minibuffer-mode (&optional prefix)
92 "Enable or disable resize-minibuffer mode.
93 A negative prefix argument disables this mode. A positive argument or
94 argument of 0 enables it.
95
96 When this minor mode is enabled, the minibuffer is dynamically resized to
97 contain the entire region of text put in it as you type.
98
99 The variable `resize-minibuffer-mode' is set to t or nil depending on
100 whether this mode is active or not.
101
102 The maximum height to which the minibuffer can grow is controlled by the
103 variable `resize-minibuffer-window-max-height'.
104
105 The variable `resize-minibuffer-window-exactly' determines whether the
106 minibuffer window should ever be shrunk to make it no larger than needed to
107 display its contents.
108
109 When using a window system, it is possible for a minibuffer to tbe the sole
110 window in a frame. Since that window is already its maximum size, the only
111 way to make more text visible at once is to increase the size of the frame.
112 The variable `resize-minibuffer-frame' controls whether this should be
113 done. The variables `resize-minibuffer-frame-max-height' and
114 `resize-minibuffer-frame-exactly' are analogous to their window
115 counterparts."
116 (interactive "p")
117 (or prefix (setq prefix 0))
118 (cond
119 ((>= prefix 0)
120 (setq resize-minibuffer-mode t))
121 (t
122 (setq resize-minibuffer-mode nil))))
123
124 (defun resize-minibuffer-setup ()
125 (cond
126 (resize-minibuffer-mode
127 (cond
128 ((and window-system
129 (eq 'only (cdr (assq 'minibuffer (frame-parameters)))))
130 (and resize-minibuffer-frame
131 (progn
132 (make-local-variable 'minibuffer-exit-hook)
133 (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore)
134 (make-local-variable 'post-command-hook)
135 (add-hook 'post-command-hook 'resize-minibuffer-frame))))
136 (t
137 (make-local-variable 'post-command-hook)
138 (add-hook 'post-command-hook 'resize-minibuffer-window))))))
139
140 (defun resize-minibuffer-count-window-lines (&optional start end)
141 "Return number of window lines occupied by text in region.
142 The number of window lines may be greater than the number of actual lines
143 in the buffer if any wrap on the display due to their length.
144
145 Optional arguments START and END default to point-min and point-max,
146 respectively."
147 (or start (setq start (point-min)))
148 (or end (setq end (point-max)))
149 (if (= start end)
150 0
151 (save-excursion
152 (save-restriction
153 (widen)
154 (narrow-to-region start end)
155 (goto-char start)
156 (vertical-motion (buffer-size))))))
157
158 \f
159 ;; Resize the minibuffer window to contain the minibuffer's contents.
160 ;; The minibuffer must be the current window.
161 (defun resize-minibuffer-window ()
162 (let ((height (window-height))
163 (lines (1+ (resize-minibuffer-count-window-lines))))
164 (and (numberp resize-minibuffer-window-max-height)
165 (> resize-minibuffer-window-max-height 0)
166 (setq lines (min lines resize-minibuffer-window-max-height)))
167 (or (if resize-minibuffer-window-exactly
168 (= lines height)
169 (<= lines height))
170 (enlarge-window (- lines height)))))
171
172 \f
173 ;; Resize the minibuffer frame to contain the minibuffer's contents.
174 ;; The minibuffer frame must be the current frame.
175 (defun resize-minibuffer-frame ()
176 (let ((height (frame-height))
177 (lines (1+ (resize-minibuffer-count-window-lines))))
178 (and (numberp resize-minibuffer-frame-max-height)
179 (> resize-minibuffer-frame-max-height 0)
180 (setq lines (min lines resize-minibuffer-frame-max-height)))
181 (cond
182 ((> lines height)
183 (set-frame-size (selected-frame) (frame-width) lines))
184 ((and resize-minibuffer-frame-exactly
185 (> height (cdr (assq 'height minibuffer-frame-alist)))
186 (< lines height))
187 (set-frame-size (selected-frame) (frame-width) lines)))))
188
189 ;; Restore the original height of the frame.
190 (defun resize-minibuffer-frame-restore ()
191 (set-frame-size (selected-frame)
192 (frame-width)
193 (cdr (assq 'height minibuffer-frame-alist))))
194
195 \f
196 (provide 'rsz-mini)
197
198 (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup)
199
200 ;; rsz-mini.el ends here