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 add the following to .emacs:
43 ;; You can subsequently deactivate it by invoking the function
44 ;; icomplete-mode with a negative prefix-arg (C-U -1 ESC-x
45 ;; icomplete-mode). Also, you can prevent activation of the mode
46 ;; during package load by first setting the variable `icomplete-mode'
47 ;; to nil. Icompletion can be enabled any time after the package is
48 ;; loaded by invoking icomplete-mode without a prefix arg.
50 ;; This version of icomplete runs on Emacs 19.18 and later. (It
51 ;; depends on the incorporation of minibuffer-setup-hook.) The elisp
52 ;; archives, ftp://archive.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive,
53 ;; probably still has a version that works in GNU Emacs v18.
55 ;; Thanks to everyone for their suggestions for refinements of this
56 ;; package. I particularly have to credit Michael Cook, who
57 ;; implemented an incremental completion style in his 'iswitch'
58 ;; functions that served as a model for icomplete. Some other
59 ;; contributors: Noah Friedman (restructuring as minor mode), Colin
60 ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
70 (defgroup icomplete nil
71 "Show completions dynamically in minibuffer."
75 ;;;_* User Customization variables
76 (defcustom icomplete-mode nil
77 "Toggle incremental minibuffer completion.
78 As text is typed into the minibuffer, prospective completions are indicated
80 Setting this variable directly does not take effect;
81 use either \\[customize] or the function `icomplete-mode'."
82 :set
(lambda (symbol value
)
83 (icomplete-mode (if value
1 -
1)))
84 :initialize
'custom-initialize-default
89 (defcustom icomplete-compute-delay
.3
90 "*Completions-computation stall, used only with large-number
91 completions - see `icomplete-delay-completions-threshold'."
95 (defcustom icomplete-delay-completions-threshold
400
96 "*Pending-completions number over which to apply icomplete-compute-delay."
100 (defcustom icomplete-max-delay-chars
3
101 "*Maximum number of initial chars to apply icomplete compute delay."
105 (defcustom icomplete-show-key-bindings t
106 "*If non-nil, show key bindings as well as completion for sole matches."
110 (defcustom icomplete-minibuffer-setup-hook nil
111 "*Icomplete-specific customization of minibuffer setup.
113 This hook is run during minibuffer setup iff icomplete will be active.
114 It is intended for use in customizing icomplete for interoperation
115 with other packages. For instance:
117 \(add-hook 'icomplete-minibuffer-setup-hook
120 \(make-local-variable 'resize-minibuffer-window-max-height)
121 \(setq resize-minibuffer-window-max-height 3))))
123 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
124 icompletion is occurring."
131 ;;;_ + Internal Variables
132 ;;;_ = icomplete-eoinput 1
133 (defvar icomplete-eoinput
1
134 "Point where minibuffer input ends and completion info begins.")
135 (make-variable-buffer-local 'icomplete-eoinput
)
136 ;;;_ = icomplete-pre-command-hook
137 (defvar icomplete-pre-command-hook nil
138 "Incremental-minibuffer-completion pre-command-hook.
140 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
141 Use `icomplete-mode' function to set it up properly for incremental
142 minibuffer completion.")
143 (add-hook 'icomplete-pre-command-hook
'icomplete-tidy
)
144 ;;;_ = icomplete-post-command-hook
145 (defvar icomplete-post-command-hook nil
146 "Incremental-minibuffer-completion post-command-hook.
148 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
149 Use `icomplete-mode' function to set it up properly for incremental
150 minibuffer completion.")
151 (add-hook 'icomplete-post-command-hook
'icomplete-exhibit
)
153 (defun icomplete-get-keys (func-name)
154 "Return strings naming keys bound to `func-name', or nil if none.
155 Examines the prior, not current, buffer, presuming that current buffer
157 (if (commandp func-name
)
159 (let* ((sym (intern func-name
))
161 (map (save-excursion (set-buffer buf
) (current-local-map)))
162 (keys (where-is-internal sym map
)))
165 (mapconcat 'key-description
168 (< (length x
) (length y
))))
172 ;;;_ > icomplete-mode (&optional prefix)
174 (defun icomplete-mode (&optional prefix
)
175 "Activate incremental minibuffer completion for this Emacs session.
176 Deactivates with negative universal argument."
178 (or prefix
(setq prefix
0))
180 (setq icomplete-mode t
)
181 ;; The following is not really necessary after first time -
183 (add-hook 'minibuffer-setup-hook
'icomplete-minibuffer-setup
))
184 (t (setq icomplete-mode nil
))))
186 ;;;_ > icomplete-simple-completing-p ()
187 (defun icomplete-simple-completing-p ()
188 "Non-nil if current window is minibuffer that's doing simple completion.
191 the selected window is a minibuffer,
192 and not in the middle of macro execution,
193 and minibuffer-completion-table is not a symbol (which would
194 indicate some non-standard, non-simple completion mechanism,
195 like file-name and other custom-func completions)."
197 (and (window-minibuffer-p (selected-window))
198 (not executing-kbd-macro
)
199 (not (symbolp minibuffer-completion-table
))))
201 ;;;_ > icomplete-minibuffer-setup ()
203 (defun icomplete-minibuffer-setup ()
204 "Run in minibuffer on activation to establish incremental completion.
205 Usually run by inclusion in `minibuffer-setup-hook'."
206 (cond ((and icomplete-mode
(icomplete-simple-completing-p))
207 (make-local-hook 'pre-command-hook
)
208 (add-hook 'pre-command-hook
210 (run-hooks 'icomplete-pre-command-hook
)))
212 (make-local-hook 'post-command-hook
)
213 (add-hook 'post-command-hook
215 (run-hooks 'icomplete-post-command-hook
)))
217 (run-hooks 'icomplete-minibuffer-setup-hook
))))
221 ;;;_ > icomplete-tidy ()
222 (defun icomplete-tidy ()
223 "Remove completions display \(if any) prior to new user input.
224 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
225 and `minibuffer-setup-hook'."
226 (if (icomplete-simple-completing-p)
227 (if (and (boundp 'icomplete-eoinput
)
230 (if (> icomplete-eoinput
(point-max))
231 ;; Oops, got rug pulled out from under us - reinit:
232 (setq icomplete-eoinput
(point-max))
233 (let ((buffer-undo-list buffer-undo-list
)) ; prevent entry
234 (delete-region icomplete-eoinput
(point-max))))
236 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
237 (make-local-variable 'icomplete-eoinput
)
238 (setq icomplete-eoinput
1))))
240 ;;;_ > icomplete-exhibit ()
241 (defun icomplete-exhibit ()
242 "Insert icomplete completions display.
243 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
244 and `minibuffer-setup-hook'."
245 (if (icomplete-simple-completing-p)
246 (let ((contents (buffer-substring (point-min)(point-max)))
247 (buffer-undo-list t
))
249 (goto-char (point-max))
250 ; Register the end of input, so we
251 ; know where the extra stuff
252 ; (match-status info) begins:
253 (if (not (boundp 'icomplete-eoinput
))
254 ;; In case it got wiped out by major mode business:
255 (make-local-variable 'icomplete-eoinput
))
256 (setq icomplete-eoinput
(point))
257 ; Insert the match-status information:
258 (if (and (> (point-max) 1)
260 ;; Don't bother with delay after certain number of chars:
261 (> (point-max) icomplete-max-delay-chars
)
262 ;; Don't delay if alternatives number is small enough:
263 (if minibuffer-completion-table
264 (cond ((numberp minibuffer-completion-table
)
265 (< minibuffer-completion-table
266 icomplete-delay-completions-threshold
))
267 ((sequencep minibuffer-completion-table
)
268 (< (length minibuffer-completion-table
)
269 icomplete-delay-completions-threshold
))
271 ;; Delay - give some grace time for next keystroke, before
272 ;; embarking on computing completions:
273 (sit-for icomplete-compute-delay
)))
275 (icomplete-completions contents
276 minibuffer-completion-table
277 minibuffer-completion-predicate
279 minibuffer-completion-confirm
))))))))
281 ;;;_ > icomplete-completions (name candidates predicate require-match)
282 (defun icomplete-completions (name candidates predicate require-match
)
283 "Identify prospective candidates for minibuffer completion.
285 The display is updated with each minibuffer keystroke during
286 minibuffer completion.
288 Prospective completion suffixes (if any) are displayed, bracketed by
289 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
291 \(...) - a single prospect is identified and matching is enforced,
292 \[...] - a single prospect is identified but matching is optional, or
293 \{...} - multiple prospects, separated by commas, are indicated, and
294 further input is required to distinguish a single one.
296 The displays for unambiguous matches have ` [Matched]' appended
297 \(whether complete or not), or ` \[No matches]', if no eligible
298 matches exist. \(Keybindings for uniquely matched commands
299 are exhibited within the square braces.)"
301 ;; 'all-completions' doesn't like empty
302 ;; minibuffer-completion-table's (ie: (nil))
303 (if (and (listp candidates
) (null (car candidates
)))
304 (setq candidates nil
))
306 (let ((comps (all-completions name candidates predicate
))
307 ; "-determined" - only one candidate
308 (open-bracket-determined (if require-match
"(" "["))
309 (close-bracket-determined (if require-match
")" "]"))
310 ;"-prospects" - more than one candidate
311 (open-bracket-prospects "{")
312 (close-bracket-prospects "}")
315 (cond ((null comps
) (format " %sNo matches%s"
316 open-bracket-determined
317 close-bracket-determined
))
318 ((null (cdr comps
)) ;one match
319 (concat (if (and (> (length (car comps
))
321 (concat open-bracket-determined
322 (substring (car comps
) (length name
))
323 close-bracket-determined
)
326 (let ((keys (and icomplete-show-key-bindings
327 (commandp (intern-soft (car comps
)))
328 (icomplete-get-keys (car comps
)))))
335 (try-completion name candidates
337 ;; Wrap predicate in impatience - ie,
338 ;; `throw' up when pending input is
339 ;; noticed. Adds some overhead to
340 ;; predicate, but should be worth it.
343 (if (input-pending-p)
347 (most-len (length most
))
351 (apply (function concat
)
354 (if (input-pending-p)
356 (if (= (length com
) most-len
)
357 ;; Most is one exact match,
358 ;; note that and leave out
359 ;; for later indication:
361 (setq most-is-exact t
)
368 (concat (and (> most-len
(length name
))
369 (concat open-bracket-determined
370 (substring most
(length name
))
371 close-bracket-determined
))
372 open-bracket-prospects
374 ;; Add a ',' at the front to indicate "complete but
376 (concat "," alternatives
)
378 close-bracket-prospects
)))))))
383 ;;;_* Local emacs vars.
385 ;;;outline-layout: (-2 :)
388 ;;; icomplete.el ends here