1 ;;; rsz-mini.el --- dynamically resize minibuffer to display entire contents
3 ;;; Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
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.
11 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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.
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.
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
48 ;;; To use this package, put the following in your .emacs:
50 ;;; (autoload 'resize-minibuffer-mode "rsz-mini" nil t)
52 ;;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
58 (defvar resize-minibuffer-mode nil
59 "*If non-`nil', resize the minibuffer so its entire contents are visible.")
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.")
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.")
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.")
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.")
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.")
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.
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.
99 The variable `resize-minibuffer-mode' is set to t or nil depending on
100 whether this mode is active or not.
102 The maximum height to which the minibuffer can grow is controlled by the
103 variable `resize-minibuffer-window-max-height'.
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.
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
117 (or prefix (setq prefix 0))
120 (setq resize-minibuffer-mode t))
122 (setq resize-minibuffer-mode nil))))
124 (defun resize-minibuffer-setup ()
126 (resize-minibuffer-mode
129 (eq 'only (cdr (assq 'minibuffer (frame-parameters)))))
130 (and resize-minibuffer-frame
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))))
137 (make-local-variable 'post-command-hook)
138 (add-hook 'post-command-hook 'resize-minibuffer-window))))))
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.
145 Optional arguments START and END default to point-min and point-max,
147 (or start (setq start (point-min)))
148 (or end (setq end (point-max)))
154 (narrow-to-region start end)
156 (vertical-motion (buffer-size))))))
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
170 (enlarge-window (- lines height)))))
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)))
183 (set-frame-size (selected-frame) (frame-width) lines))
184 ((and resize-minibuffer-frame-exactly
185 (> height (cdr (assq 'height minibuffer-frame-alist)))
187 (set-frame-size (selected-frame) (frame-width) lines)))))
189 ;; Restore the original height of the frame.
190 (defun resize-minibuffer-frame-restore ()
191 (set-frame-size (selected-frame)
193 (cdr (assq 'height minibuffer-frame-alist))))
198 (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup)
200 ;; rsz-mini.el ends here