1 ;;; icomplete.el --- minibuffer completion incremental feedback
3 ;; Copyright (C) 1992-1994, 1997, 1999, 2001-2012
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ken Manheimer <klm@i.am>
7 ;; Maintainer: Ken Manheimer <klm@i.am>
8 ;; Created: Mar 1993 Ken Manheimer, klm@nist.gov - first release to usenet
9 ;; Last update: Ken Manheimer <klm@i.am>, 11/18/1999.
10 ;; Keywords: help, abbrev
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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, load the package and use the
42 ;; `icomplete-mode' function. You can subsequently deactivate it by
43 ;; invoking the function icomplete-mode with a negative prefix-arg
44 ;; (C-U -1 ESC-x icomplete-mode). Also, you can prevent activation of
45 ;; the mode during package load by first setting the variable
46 ;; `icomplete-mode' to nil. Icompletion can be enabled any time after
47 ;; the package is loaded by invoking icomplete-mode without a prefix
50 ;; Thanks to everyone for their suggestions for refinements of this
51 ;; package. I particularly have to credit Michael Cook, who
52 ;; implemented an incremental completion style in his 'iswitch'
53 ;; functions that served as a model for icomplete. Some other
54 ;; contributors: Noah Friedman (restructuring as minor mode), Colin
55 ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
65 (defgroup icomplete nil
66 "Show completions dynamically in minibuffer."
70 (defvar icomplete-prospects-length
80)
71 (make-obsolete-variable
72 'icomplete-prospects-length
'icomplete-prospects-height
"23.1")
74 ;;;_* User Customization variables
75 (defcustom icomplete-prospects-height
76 ;; 20 is an estimated common size for the prompt + minibuffer content, to
77 ;; try to guess the number of lines used up by icomplete-prospects-length.
78 (+ 1 (/ (+ icomplete-prospects-length
20) (window-width)))
79 "Maximum number of lines to use in the minibuffer."
84 (defcustom icomplete-compute-delay
.3
85 "Completions-computation stall, used only with large-number completions.
86 See `icomplete-delay-completions-threshold'."
90 (defcustom icomplete-delay-completions-threshold
400
91 "Pending-completions number over which to apply `icomplete-compute-delay'."
95 (defcustom icomplete-max-delay-chars
3
96 "Maximum number of initial chars to apply icomplete compute delay."
100 (defcustom icomplete-show-key-bindings t
101 "If non-nil, show key bindings as well as completion for sole matches."
105 (defcustom icomplete-minibuffer-setup-hook nil
106 "Icomplete-specific customization of minibuffer setup.
108 This hook is run during minibuffer setup if icomplete is active.
109 It is intended for use in customizing icomplete for interoperation
110 with other features and packages. For instance:
112 \(add-hook 'icomplete-minibuffer-setup-hook
115 \(make-local-variable 'max-mini-window-height)
116 \(setq max-mini-window-height 3))))
118 will constrain Emacs to a maximum minibuffer height of 3 lines when
119 icompletion is occurring."
126 ;;;_ + Internal Variables
127 ;;;_ = icomplete-eoinput nil
128 (defvar icomplete-overlay
(make-overlay (point-min) (point-min) nil t t
)
129 "Overlay used to display the list of completions.")
131 ;;;_ = icomplete-pre-command-hook
132 (defvar icomplete-pre-command-hook nil
133 "Incremental-minibuffer-completion pre-command-hook.
135 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
136 Use `icomplete-mode' function to set it up properly for incremental
137 minibuffer completion.")
138 (add-hook 'icomplete-pre-command-hook
'icomplete-tidy
)
139 ;;;_ = icomplete-post-command-hook
140 (defvar icomplete-post-command-hook nil
141 "Incremental-minibuffer-completion post-command-hook.
143 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
144 Use `icomplete-mode' function to set it up properly for incremental
145 minibuffer completion.")
146 (add-hook 'icomplete-post-command-hook
'icomplete-exhibit
)
148 (defun icomplete-get-keys (func-name)
149 "Return strings naming keys bound to FUNC-NAME, or nil if none.
150 Examines the prior, not current, buffer, presuming that current buffer
152 (when (commandp func-name
)
154 (let* ((sym (intern func-name
))
155 (buf (other-buffer nil t
))
156 (keys (with-current-buffer buf
(where-is-internal sym
))))
159 (mapconcat 'key-description
162 (< (length x
) (length y
))))
165 ;;;_ = icomplete-with-completion-tables
166 (defvar icomplete-with-completion-tables
'(internal-complete-buffer)
167 "Specialized completion tables with which icomplete should operate.
169 Icomplete does not operate with any specialized completion tables
170 except those on this list.")
172 ;;;_ > icomplete-mode (&optional prefix)
174 (define-minor-mode icomplete-mode
175 "Toggle incremental minibuffer completion (Icomplete mode).
176 With a prefix argument ARG, enable Icomplete mode if ARG is
177 positive, and disable it otherwise. If called from Lisp, enable
178 the mode if ARG is omitted or nil."
179 :global t
:group
'icomplete
181 ;; The following is not really necessary after first time -
183 (add-hook 'minibuffer-setup-hook
'icomplete-minibuffer-setup
)
184 (remove-hook 'minibuffer-setup-hook
'icomplete-minibuffer-setup
)))
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 minibuffer-completion-table
200 (or (not (functionp minibuffer-completion-table
))
201 (eq icomplete-with-completion-tables t
)
202 (member minibuffer-completion-table
203 icomplete-with-completion-tables
))))
205 ;;;_ > icomplete-minibuffer-setup ()
206 (defun icomplete-minibuffer-setup ()
207 "Run in minibuffer on activation to establish incremental completion.
208 Usually run by inclusion in `minibuffer-setup-hook'."
209 (when (and icomplete-mode
(icomplete-simple-completing-p))
210 (set (make-local-variable 'completion-show-inline-help
) nil
)
211 (add-hook 'pre-command-hook
212 (lambda () (let ((non-essential t
))
213 (run-hooks 'icomplete-pre-command-hook
)))
215 (add-hook 'post-command-hook
216 (lambda () (let ((non-essential t
)) ;E.g. don't prompt for password!
217 (run-hooks 'icomplete-post-command-hook
)))
219 (run-hooks 'icomplete-minibuffer-setup-hook
)))
225 ;;;_ > icomplete-tidy ()
226 (defun icomplete-tidy ()
227 "Remove completions display \(if any) prior to new user input.
228 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
229 and `minibuffer-setup-hook'."
230 (delete-overlay icomplete-overlay
))
232 ;;;_ > icomplete-exhibit ()
233 (defun icomplete-exhibit ()
234 "Insert icomplete completions display.
235 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
236 and `minibuffer-setup-hook'."
237 (when (and icomplete-mode
(icomplete-simple-completing-p))
239 (goto-char (point-max))
240 ; Insert the match-status information:
241 (if (and (> (point-max) (minibuffer-prompt-end))
242 buffer-undo-list
; Wait for some user input.
244 ;; Don't bother with delay after certain number of chars:
245 (> (- (point) (field-beginning)) icomplete-max-delay-chars
)
246 ;; Don't delay if alternatives number is small enough:
247 (and (sequencep minibuffer-completion-table
)
248 (< (length minibuffer-completion-table
)
249 icomplete-delay-completions-threshold
))
250 ;; Delay - give some grace time for next keystroke, before
251 ;; embarking on computing completions:
252 (sit-for icomplete-compute-delay
)))
253 (let ((text (while-no-input
254 (icomplete-completions
256 minibuffer-completion-table
257 minibuffer-completion-predicate
258 (not minibuffer-completion-confirm
))))
261 ;; Do nothing if while-no-input was aborted.
263 (move-overlay icomplete-overlay
(point) (point) (current-buffer))
264 ;; The current C cursor code doesn't know to use the overlay's
265 ;; marker's stickiness to figure out whether to place the cursor
266 ;; before or after the string, so let's spoon-feed it the pos.
267 (put-text-property 0 1 'cursor t text
)
268 (overlay-put icomplete-overlay
'after-string text
)))))))
270 ;;;_ > icomplete-completions (name candidates predicate require-match)
271 (defun icomplete-completions (name candidates predicate require-match
)
272 "Identify prospective candidates for minibuffer completion.
274 The display is updated with each minibuffer keystroke during
275 minibuffer completion.
277 Prospective completion suffixes (if any) are displayed, bracketed by
278 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
280 \(...) - a single prospect is identified and matching is enforced,
281 \[...] - a single prospect is identified but matching is optional, or
282 \{...} - multiple prospects, separated by commas, are indicated, and
283 further input is required to distinguish a single one.
285 The displays for unambiguous matches have ` [Matched]' appended
286 \(whether complete or not), or ` \[No matches]', if no eligible
287 matches exist. \(Keybindings for uniquely matched commands
288 are exhibited within the square braces.)"
290 (let* ((md (completion--field-metadata (field-beginning)))
291 (comps (completion-all-sorted-completions))
292 (last (if (consp comps
) (last comps
)))
293 (base-size (cdr last
))
294 (open-bracket (if require-match
"(" "["))
295 (close-bracket (if require-match
")" "]")))
296 ;; `concat'/`mapconcat' is the slow part.
297 (if (not (consp comps
))
298 (format " %sNo matches%s" open-bracket close-bracket
)
299 (if last
(setcdr last nil
))
301 (if (and base-size
(> base-size
0))
302 (completion-try-completion
303 name candidates predicate
(length name
) md
)
304 ;; If the `comps' are 0-based, the result should be
305 ;; the same with `comps'.
306 (completion-try-completion
307 name comps nil
(length name
) md
)))
308 (most (if (consp most-try
) (car most-try
)
309 (if most-try
(car comps
) "")))
310 ;; Compare name and most, so we can determine if name is
311 ;; a prefix of most, or something else.
312 (compare (compare-strings name nil nil
313 most nil nil completion-ignore-case
))
314 (determ (unless (or (eq t compare
) (eq t most-try
)
315 (= (setq compare
(1- (abs compare
)))
319 ((= compare
(length name
))
320 ;; Typical case: name is a prefix.
321 (substring most compare
))
323 (t (concat "..." (substring most compare
))))
325 ;;"-prospects" - more than one candidate
326 (prospects-len (+ (length determ
) 6 ;; take {,...} into account
327 (string-width (buffer-string))))
329 ;; Max total length to use, including the minibuffer content.
330 (* (+ icomplete-prospects-height
331 ;; If the minibuffer content already uses up more than
332 ;; one line, increase the allowable space accordingly.
333 (/ prospects-len
(window-width)))
336 ;; Find the common prefix among `comps'.
337 ;; We can't use the optimization below because its assumptions
338 ;; aren't always true, e.g. when completion-cycling (bug#10850):
339 ;; (if (eq t (compare-strings (car comps) nil (length most)
340 ;; most nil nil completion-ignore-case))
343 ;; Else, use try-completion.
344 (let ((comps-prefix (try-completion "" comps
)))
345 (and (stringp comps-prefix
)
346 (length comps-prefix
)))) ;;)
348 prospects most-is-exact comp limit
)
349 (if (eq most-try t
) ;; (or (null (cdr comps))
351 (while (and comps
(not limit
))
353 (if prefix-len
(substring (car comps
) prefix-len
) (car comps
))
355 (cond ((string-equal comp
"") (setq most-is-exact t
))
356 ((member comp prospects
))
357 (t (setq prospects-len
358 (+ (string-width comp
) 1 prospects-len
))
359 (if (< prospects-len prospects-max
)
360 (push comp prospects
)
362 ;; Restore the base-size info, since completion-all-sorted-completions
364 (if last
(setcdr last base-size
))
368 (and most-is-exact
",")
369 (mapconcat 'identity
(nreverse prospects
) ",")
374 (let ((keys (and icomplete-show-key-bindings
375 (commandp (intern-soft most
))
376 (icomplete-get-keys most
))))
377 (if keys
(concat "; " keys
) ""))
380 ;;_* Local emacs vars.
382 ;;allout-layout: (-2 :)
385 ;;; icomplete.el ends here