1 ;;; icomplete.el --- minibuffer completion incremental feedback
3 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Ken Manheimer <klm@nist.gov>
6 ;; Maintainer: Ken Manheimer <klm@nist.gov>
7 ;; Created: Mar 1993 klm@nist.gov - first release to usenet
8 ;; Keywords: help, abbrev
10 ;; This file is part of GNU Emacs.
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)
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.
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;;; Loading this package implements a more fine-grained minibuffer
29 ;;; completion feedback scheme. Prospective completions are concisely
30 ;;; indicated within the minibuffer itself, with each successive
33 ;;; See 'icomplete-completions' docstring for a description of the
34 ;;; icomplete display format.
36 ;;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
37 ;;; customize icomplete setup for interoperation with other
38 ;;; minibuffer-oriented packages.
40 ;;; To activate icomplete mode, simply load the package. You can
41 ;;; subsequently deactivate it by invoking the function icomplete-mode
42 ;;; with a negative prefix-arg (C-U -1 ESC-x icomplete-mode). Also,
43 ;;; you can prevent activation of the mode during package load by
44 ;;; first setting the variable `icomplete-mode' to nil. Icompletion
45 ;;; can be enabled any time after the package is loaded by invoking
46 ;;; icomplete-mode without a prefix arg.
48 ;;; Thanks to everyone for their suggestions for refinements of this
49 ;;; package. I particularly have to credit Michael Cook, who
50 ;;; implemented an incremental completion style in his 'iswitch'
51 ;;; functions that served as a model for icomplete. Some other
52 ;;; contributors: Noah Freidman (restructuring as minor mode), Colin
53 ;;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and
63 ;;;_* User Customization variables
66 ;;;_ = icomplete-minibuffer-setup-hook
67 (defvar icomplete-minibuffer-setup-hook nil
68 "*Icomplete-specific customization of minibuffer setup.
70 This hook is run during minibuffer setup iff icomplete will be active.
71 It is intended for use in customizing icomplete for interoperation
72 with other packages. For instance:
74 \(add-hook 'icomplete-minibuffer-setup-hook
77 \(make-local-variable 'resize-minibuffer-window-max-height)
78 \(setq resize-minibuffer-window-max-height 3))))
80 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
81 icompletion is occurring.")
83 ;;;_ + Internal Variables
85 (defvar icomplete-mode t
86 "Non-nil enables incremental minibuffer completion, once
87 `\\[icomplete-mode]' function has set things up.")
88 ;;;_ = icomplete-eoinput 1
89 (defvar icomplete-eoinput
1
90 "Point where minibuffer input ends and completion info begins.")
91 (make-variable-buffer-local 'icomplete-eoinput
)
92 ;;;_ = icomplete-pre-command-hook
93 (defvar icomplete-pre-command-hook nil
94 "Incremental-minibuffer-completion pre-command-hook.
96 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
97 Use `icomplete-mode' function to set it up properly for incremental
98 minibuffer completion.")
99 (add-hook 'icomplete-pre-command-hook
'icomplete-tidy
)
100 ;;;_ = icomplete-post-command-hook
101 (defvar icomplete-post-command-hook nil
102 "Incremental-minibuffer-completion post-command-hook.
104 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
105 Use `icomplete-mode' function to set it up properly for incremental
106 minibuffer completion.")
107 (add-hook 'icomplete-post-command-hook
'icomplete-exhibit
)
109 ;;;_ > icomplete-mode (&optional prefix)
111 (defun icomplete-mode (&optional prefix
)
112 "Activate incremental minibuffer completion for this emacs session,
113 or deactivate with negative prefix arg."
115 (or prefix
(setq prefix
0))
117 (setq icomplete-mode t
)
118 ;; The following is not really necessary after first time -
120 (add-hook 'minibuffer-setup-hook
'icomplete-minibuffer-setup
))
121 (t (setq icomplete-mode nil
))))
123 ;;;_ > icomplete-simple-completing-p ()
124 (defun icomplete-simple-completing-p ()
126 "Non-nil if current window is minibuffer that's doing simple completion.
129 the selected window is a minibuffer,
130 and not in the middle of macro execution,
131 and minibuffer-completion-table is not a symbol (which would
132 indicate some non-standard, non-simple completion mechansm,
133 like file-name and other custom-func completions)."
135 (and (window-minibuffer-p (selected-window))
136 (not executing-macro
)
137 (not (symbolp minibuffer-completion-table
))))
139 ;;;_ > icomplete-minibuffer-setup ()
141 (defun icomplete-minibuffer-setup ()
142 "Run in minibuffer on activation to establish incremental completion.
143 Usually run by inclusion in `minibuffer-setup-hook'."
144 (cond ((and icomplete-mode
(icomplete-simple-completing-p))
145 (make-local-hook 'pre-command-hook
)
146 (add-hook 'pre-command-hook
148 (run-hooks 'icomplete-pre-command-hook
)))
150 (make-local-hook 'post-command-hook
)
151 (add-hook 'post-command-hook
153 (run-hooks 'icomplete-post-command-hook
)))
155 (run-hooks 'icomplete-minibuffer-setup-hook
))))
159 ;;;_ > icomplete-tidy ()
160 (defun icomplete-tidy ()
161 "Remove completions display \(if any) prior to new user input.
162 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
163 and `minibuffer-setup-hook'."
164 (if (icomplete-simple-completing-p)
165 (if (and (boundp 'icomplete-eoinput
)
168 (if (> icomplete-eoinput
(point-max))
169 ;; Oops, got rug pulled out from under us - reinit:
170 (setq icomplete-eoinput
(point-max))
171 (let ((buffer-undo-list buffer-undo-list
)) ; prevent entry
172 (delete-region icomplete-eoinput
(point-max))))
174 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
175 (make-local-variable 'icomplete-eoinput
)
176 (setq icomplete-eoinput
1))))
178 ;;;_ > icomplete-exhibit ()
179 (defun icomplete-exhibit ()
180 "Insert icomplete completions display.
181 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
182 and `minibuffer-setup-hook'."
183 (if (icomplete-simple-completing-p)
184 (let ((contents (buffer-substring (point-min)(point-max)))
185 (buffer-undo-list t
))
187 (goto-char (point-max))
188 ; Register the end of input, so we
189 ; know where the extra stuff
190 ; (match-status info) begins:
191 (if (not (boundp 'icomplete-eoinput
))
192 ;; In case it got wiped out by major mode business:
193 (make-local-variable 'icomplete-eoinput
))
194 (setq icomplete-eoinput
(point))
195 ; Insert the match-status information:
196 (if (> (point-max) 1)
198 (icomplete-completions contents
199 minibuffer-completion-table
200 minibuffer-completion-predicate
202 minibuffer-completion-confirm
))))))))
204 ;;;_ > icomplete-completions (name candidates predicate require-match)
205 (defun icomplete-completions (name candidates predicate require-match
)
206 "Identify prospective candidates for minibuffer completion.
208 The display is updated with each minibuffer keystroke during
209 minibuffer completion.
211 Prospective completion suffixes (if any) are displayed, bracketed by
212 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
214 \(...) - a single prospect is identified and matching is enforced,
215 \[...] - a single prospect is identified but matching is optional, or
216 \{...} - multiple prospects, separated by commas, are indicated, and
217 further input is required to distingish a single one.
219 The displays for disambiguous matches have ` [Matched]' appended
220 \(whether complete or not), or ` \[No matches]', if no eligible
223 (let ((comps (all-completions name candidates predicate
))
224 ; "-determined" - only one candidate
225 (open-bracket-determined (if require-match
"(" "["))
226 (close-bracket-determined (if require-match
")" "]"))
227 ;"-prospects" - more than one candidate
228 (open-bracket-prospects "{")
229 (close-bracket-prospects "}")
231 (cond ((null comps
) (format " %sNo matches%s"
232 open-bracket-determined
233 close-bracket-determined
))
234 ((null (cdr comps
)) ;one match
235 (concat (if (and (> (length (car comps
))
237 (concat open-bracket-determined
238 (substring (car comps
) (length name
))
239 close-bracket-determined
)
243 (let* ((most (try-completion name candidates predicate
))
244 (most-len (length most
))
251 (mapcar '(lambda (com)
252 (if (= (length com
) most-len
)
253 ;; Most is one exact match,
254 ;; note that and leave out
255 ;; for later indication:
257 (setq most-is-exact t
)
263 (concat (and (> most-len
(length name
))
264 (concat open-bracket-determined
265 (substring most
(length name
))
266 close-bracket-determined
))
267 open-bracket-prospects
269 (concat "," alternatives
)
271 close-bracket-prospects
))))))
273 ;;;_ + Initialization
274 ;;; If user hasn't setq-default icomplete-mode to nil, then setup for
280 ;;;_* Local emacs vars.
282 ;;;outline-layout: (-2 :)
285 ;;; icomplete.el ends here