1 ;;;_. icomplete.el - minibuffer completion incremental feedback
3 ;; Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
5 ;; Author: Ken Manheimer <klm@python.org>
6 ;; Maintainer: Ken Manheimer <klm@python.org>
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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Loading this package implements a more fine-grained minibuffer
30 ;; completion feedback scheme. Prospective completions are concisely
31 ;; indicated within the minibuffer itself, with each successive
34 ;; See 'icomplete-completions' docstring for a description of the
35 ;; icomplete display format.
37 ;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
38 ;; customize icomplete setup for interoperation with other
39 ;; minibuffer-oriented packages.
41 ;; To activate icomplete mode, simply load the package. You can
42 ;; subsequently deactivate it by invoking the function icomplete-mode
43 ;; with a negative prefix-arg (C-U -1 ESC-x icomplete-mode). Also,
44 ;; you can prevent activation of the mode during package load by
45 ;; first setting the variable `icomplete-mode' to nil. Icompletion
46 ;; can be enabled any time after the package is loaded by invoking
47 ;; icomplete-mode without a prefix arg.
49 ;; This version of icomplete runs on Emacs 19.18 and later. (It
50 ;; depends on the incorporation of minibuffer-setup-hook.) The elisp
51 ;; archives, ftp://archive.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive,
52 ;; probably still has a version that works in GNU Emacs v18.
54 ;; Thanks to everyone for their suggestions for refinements of this
55 ;; package. I particularly have to credit Michael Cook, who
56 ;; implemented an incremental completion style in his 'iswitch'
57 ;; functions that served as a model for icomplete. Some other
58 ;; contributors: Noah Freidman (restructuring as minor mode), Colin
59 ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
68 ;;;_* User Customization variables
69 (defvar icomplete-compute-delay
.3
70 "*Completions-computation stall, used only with large-number
71 completions - see `icomplete-delay-completions-threshold'.")
72 (defvar icomplete-delay-completions-threshold
400
73 "*Pending-completions number over which to apply icomplete-compute-delay.")
74 (defvar icomplete-max-delay-chars
3
75 "*Maximum number of initial chars to apply icomplete compute delay.")
78 ;;;_ = icomplete-minibuffer-setup-hook
79 (defvar icomplete-minibuffer-setup-hook nil
80 "*Icomplete-specific customization of minibuffer setup.
82 This hook is run during minibuffer setup iff icomplete will be active.
83 It is intended for use in customizing icomplete for interoperation
84 with other packages. For instance:
86 \(add-hook 'icomplete-minibuffer-setup-hook
89 \(make-local-variable 'resize-minibuffer-window-max-height)
90 \(setq resize-minibuffer-window-max-height 3))))
92 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
93 icompletion is occurring.")
95 ;;;_ + Internal Variables
97 (defvar icomplete-mode t
98 "*Non-nil enables incremental minibuffer completion (see \\[icomplete-mode].")
99 ;;;_ = icomplete-eoinput 1
100 (defvar icomplete-eoinput
1
101 "Point where minibuffer input ends and completion info begins.")
102 (make-variable-buffer-local 'icomplete-eoinput
)
103 ;;;_ = icomplete-pre-command-hook
104 (defvar icomplete-pre-command-hook nil
105 "Incremental-minibuffer-completion pre-command-hook.
107 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
108 Use `icomplete-mode' function to set it up properly for incremental
109 minibuffer completion.")
110 (add-hook 'icomplete-pre-command-hook
'icomplete-tidy
)
111 ;;;_ = icomplete-post-command-hook
112 (defvar icomplete-post-command-hook nil
113 "Incremental-minibuffer-completion post-command-hook.
115 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
116 Use `icomplete-mode' function to set it up properly for incremental
117 minibuffer completion.")
118 (add-hook 'icomplete-post-command-hook
'icomplete-exhibit
)
120 (defvar icomplete-show-key-bindings t
121 "*When non-nil, show key bindings as well as completion for sole matches.")
123 (defun icomplete-get-keys (func-name)
124 "Return strings naming keys bound to `func-name', or nil if none.
125 Examines the prior, not current, buffer, presuming that current buffer
127 (if (commandp func-name
)
129 (let* ((sym (intern func-name
))
131 (map (save-excursion (set-buffer buf
) (current-local-map)))
132 (keys (where-is-internal sym map
)))
135 (mapconcat 'key-description
138 (< (length x
) (length y
))))
142 ;;;_ > icomplete-mode (&optional prefix)
144 (defun icomplete-mode (&optional prefix
)
145 "Activate incremental minibuffer completion for this Emacs session.
146 Deactivates with negative universal argument."
148 (or prefix
(setq prefix
0))
150 (setq icomplete-mode t
)
151 ;; The following is not really necessary after first time -
153 (add-hook 'minibuffer-setup-hook
'icomplete-minibuffer-setup
))
154 (t (setq icomplete-mode nil
))))
156 ;;;_ > icomplete-simple-completing-p ()
157 (defun icomplete-simple-completing-p ()
158 "Non-nil if current window is minibuffer that's doing simple completion.
161 the selected window is a minibuffer,
162 and not in the middle of macro execution,
163 and minibuffer-completion-table is not a symbol (which would
164 indicate some non-standard, non-simple completion mechanism,
165 like file-name and other custom-func completions)."
167 (and (window-minibuffer-p (selected-window))
168 (not executing-kbd-macro
)
169 (not (symbolp minibuffer-completion-table
))))
171 ;;;_ > icomplete-minibuffer-setup ()
173 (defun icomplete-minibuffer-setup ()
174 "Run in minibuffer on activation to establish incremental completion.
175 Usually run by inclusion in `minibuffer-setup-hook'."
176 (cond ((and icomplete-mode
(icomplete-simple-completing-p))
177 (make-local-hook 'pre-command-hook
)
178 (add-hook 'pre-command-hook
180 (run-hooks 'icomplete-pre-command-hook
)))
182 (make-local-hook 'post-command-hook
)
183 (add-hook 'post-command-hook
185 (run-hooks 'icomplete-post-command-hook
)))
187 (run-hooks 'icomplete-minibuffer-setup-hook
))))
191 ;;;_ > icomplete-tidy ()
192 (defun icomplete-tidy ()
193 "Remove completions display \(if any) prior to new user input.
194 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
195 and `minibuffer-setup-hook'."
196 (if (icomplete-simple-completing-p)
197 (if (and (boundp 'icomplete-eoinput
)
200 (if (> icomplete-eoinput
(point-max))
201 ;; Oops, got rug pulled out from under us - reinit:
202 (setq icomplete-eoinput
(point-max))
203 (let ((buffer-undo-list buffer-undo-list
)) ; prevent entry
204 (delete-region icomplete-eoinput
(point-max))))
206 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
207 (make-local-variable 'icomplete-eoinput
)
208 (setq icomplete-eoinput
1))))
210 ;;;_ > icomplete-exhibit ()
211 (defun icomplete-exhibit ()
212 "Insert icomplete completions display.
213 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
214 and `minibuffer-setup-hook'."
215 (if (icomplete-simple-completing-p)
216 (let ((contents (buffer-substring (point-min)(point-max)))
217 (buffer-undo-list t
))
219 (goto-char (point-max))
220 ; Register the end of input, so we
221 ; know where the extra stuff
222 ; (match-status info) begins:
223 (if (not (boundp 'icomplete-eoinput
))
224 ;; In case it got wiped out by major mode business:
225 (make-local-variable 'icomplete-eoinput
))
226 (setq icomplete-eoinput
(point))
227 ; Insert the match-status information:
228 (if (and (> (point-max) 1)
230 ;; Don't bother with delay after certain number of chars:
231 (> (point-max) icomplete-max-delay-chars
)
232 ;; Don't delay if alternatives number is small enough:
233 (if minibuffer-completion-table
234 (cond ((numberp minibuffer-completion-table
)
235 (< minibuffer-completion-table
236 icomplete-delay-completions-threshold
))
237 ((sequencep minibuffer-completion-table
)
238 (< (length minibuffer-completion-table
)
239 icomplete-delay-completions-threshold
))
241 ;; Delay - give some grace time for next keystroke, before
242 ;; embarking on computing completions:
243 (sit-for icomplete-compute-delay
)))
245 (icomplete-completions contents
246 minibuffer-completion-table
247 minibuffer-completion-predicate
249 minibuffer-completion-confirm
))))))))
251 ;;;_ > icomplete-completions (name candidates predicate require-match)
252 (defun icomplete-completions (name candidates predicate require-match
)
253 "Identify prospective candidates for minibuffer completion.
255 The display is updated with each minibuffer keystroke during
256 minibuffer completion.
258 Prospective completion suffixes (if any) are displayed, bracketed by
259 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
261 \(...) - a single prospect is identified and matching is enforced,
262 \[...] - a single prospect is identified but matching is optional, or
263 \{...} - multiple prospects, separated by commas, are indicated, and
264 further input is required to distinguish a single one.
266 The displays for unambiguous matches have ` [Matched]' appended
267 \(whether complete or not), or ` \[No matches]', if no eligible
268 matches exist. \(Keybindings for uniquely matched commands
269 are exhibited within the square braces.)"
271 ;; 'all-completions' doesn't like empty
272 ;; minibuffer-completion-table's (ie: (nil))
273 (if (and (listp candidates
) (null (car candidates
)))
274 (setq candidates nil
))
276 (let ((comps (all-completions name candidates predicate
))
277 ; "-determined" - only one candidate
278 (open-bracket-determined (if require-match
"(" "["))
279 (close-bracket-determined (if require-match
")" "]"))
280 ;"-prospects" - more than one candidate
281 (open-bracket-prospects "{")
282 (close-bracket-prospects "}")
285 (cond ((null comps
) (format " %sNo matches%s"
286 open-bracket-determined
287 close-bracket-determined
))
288 ((null (cdr comps
)) ;one match
289 (concat (if (and (> (length (car comps
))
291 (concat open-bracket-determined
292 (substring (car comps
) (length name
))
293 close-bracket-determined
)
296 (let ((keys (and icomplete-show-key-bindings
297 (commandp (intern-soft (car comps
)))
298 (icomplete-get-keys (car comps
)))))
305 (try-completion name candidates
307 ;; Wrap predicate in impatience - ie,
308 ;; `throw' up when pending input is
309 ;; noticed. Adds some overhead to
310 ;; predicate, but should be worth it.
313 (if (input-pending-p)
317 (most-len (length most
))
321 (apply (function concat
)
324 (if (input-pending-p)
326 (if (= (length com
) most-len
)
327 ;; Most is one exact match,
328 ;; note that and leave out
329 ;; for later indication:
331 (setq most-is-exact t
)
338 (concat (and (> most-len
(length name
))
339 (concat open-bracket-determined
340 (substring most
(length name
))
341 close-bracket-determined
))
342 open-bracket-prospects
344 ;; Add a ',' at the front to indicate "complete but
346 (concat "," alternatives
)
348 close-bracket-prospects
)))))))
350 ;;;_* Local emacs vars.
352 ;;;outline-layout: (-2 :)
355 ;;; icomplete.el ends here