* term.el: Fix typos in docstrings.
[emacs.git] / lisp / icomplete.el
blob94822a60a63c07fb115e0f050b32fb8005cb40b5
1 ;;; icomplete.el --- minibuffer completion incremental feedback
3 ;; Copyright (C) 1992, 1993, 1994, 1997, 1999, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 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/>.
27 ;;; Commentary:
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
32 ;; keystroke.
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
48 ;; arg.
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.
57 ;; klm.
59 ;;; Code:
61 ;;;_* Provide
62 (provide 'icomplete)
65 (defgroup icomplete nil
66 "Show completions dynamically in minibuffer."
67 :prefix "icomplete-"
68 :group '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."
80 :type 'integer
81 :group 'icomplete
82 :version "23.1")
84 (defcustom icomplete-compute-delay .3
85 "Completions-computation stall, used only with large-number completions.
86 See `icomplete-delay-completions-threshold'."
87 :type 'number
88 :group 'icomplete)
90 (defcustom icomplete-delay-completions-threshold 400
91 "Pending-completions number over which to apply `icomplete-compute-delay'."
92 :type 'integer
93 :group 'icomplete)
95 (defcustom icomplete-max-delay-chars 3
96 "Maximum number of initial chars to apply icomplete compute delay."
97 :type 'integer
98 :group 'icomplete)
100 (defcustom icomplete-show-key-bindings t
101 "If non-nil, show key bindings as well as completion for sole matches."
102 :type 'boolean
103 :group 'icomplete)
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
113 \(function
114 \(lambda ()
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."
120 :type 'hook
121 :group 'icomplete)
124 ;;;_* Initialization
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
151 is minibuffer."
152 (when (commandp func-name)
153 (save-excursion
154 (let* ((sym (intern func-name))
155 (buf (other-buffer nil t))
156 (keys (with-current-buffer buf (where-is-internal sym))))
157 (when keys
158 (concat "<"
159 (mapconcat 'key-description
160 (sort keys
161 #'(lambda (x y)
162 (< (length x) (length y))))
163 ", ")
164 ">"))))))
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)
173 ;;;###autoload
174 (define-minor-mode icomplete-mode
175 "Toggle incremental minibuffer completion for this Emacs session.
176 With a numeric argument, turn Icomplete mode on if ARG is positive,
177 otherwise turn it off."
178 :global t :group 'icomplete
179 (if icomplete-mode
180 ;; The following is not really necessary after first time -
181 ;; no great loss.
182 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
183 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
185 ;;;_ > icomplete-simple-completing-p ()
186 (defun icomplete-simple-completing-p ()
187 "Non-nil if current window is minibuffer that's doing simple completion.
189 Conditions are:
190 the selected window is a minibuffer,
191 and not in the middle of macro execution,
192 and `minibuffer-completion-table' is not a symbol (which would
193 indicate some non-standard, non-simple completion mechanism,
194 like file-name and other custom-func completions)."
196 (and (window-minibuffer-p (selected-window))
197 (not executing-kbd-macro)
198 minibuffer-completion-table
199 (or (not (functionp minibuffer-completion-table))
200 (eq icomplete-with-completion-tables t)
201 (member minibuffer-completion-table
202 icomplete-with-completion-tables))))
204 ;;;_ > icomplete-minibuffer-setup ()
205 (defun icomplete-minibuffer-setup ()
206 "Run in minibuffer on activation to establish incremental completion.
207 Usually run by inclusion in `minibuffer-setup-hook'."
208 (when (and icomplete-mode (icomplete-simple-completing-p))
209 (add-hook 'pre-command-hook
210 (lambda () (run-hooks 'icomplete-pre-command-hook))
211 nil t)
212 (add-hook 'post-command-hook
213 (lambda () (run-hooks 'icomplete-post-command-hook))
214 nil t)
215 (run-hooks 'icomplete-minibuffer-setup-hook)))
219 ;;;_* Completion
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 (delete-overlay icomplete-overlay))
228 ;;;_ > icomplete-exhibit ()
229 (defun icomplete-exhibit ()
230 "Insert icomplete completions display.
231 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
232 and `minibuffer-setup-hook'."
233 (when (and icomplete-mode (icomplete-simple-completing-p))
234 (save-excursion
235 (goto-char (point-max))
236 ; Insert the match-status information:
237 (if (and (> (point-max) (minibuffer-prompt-end))
238 buffer-undo-list ; Wait for some user input.
240 ;; Don't bother with delay after certain number of chars:
241 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
242 ;; Don't delay if alternatives number is small enough:
243 (and (sequencep minibuffer-completion-table)
244 (< (length minibuffer-completion-table)
245 icomplete-delay-completions-threshold))
246 ;; Delay - give some grace time for next keystroke, before
247 ;; embarking on computing completions:
248 (sit-for icomplete-compute-delay)))
249 (let ((text (while-no-input
250 (icomplete-completions
251 (field-string)
252 minibuffer-completion-table
253 minibuffer-completion-predicate
254 (not minibuffer-completion-confirm))))
255 (buffer-undo-list t)
256 deactivate-mark)
257 ;; Do nothing if while-no-input was aborted.
258 (when (stringp text)
259 (move-overlay icomplete-overlay (point) (point) (current-buffer))
260 ;; The current C cursor code doesn't know to use the overlay's
261 ;; marker's stickiness to figure out whether to place the cursor
262 ;; before or after the string, so let's spoon-feed it the pos.
263 (put-text-property 0 1 'cursor t text)
264 (overlay-put icomplete-overlay 'after-string text)))))))
266 ;;;_ > icomplete-completions (name candidates predicate require-match)
267 (defun icomplete-completions (name candidates predicate require-match)
268 "Identify prospective candidates for minibuffer completion.
270 The display is updated with each minibuffer keystroke during
271 minibuffer completion.
273 Prospective completion suffixes (if any) are displayed, bracketed by
274 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
276 \(...) - a single prospect is identified and matching is enforced,
277 \[...] - a single prospect is identified but matching is optional, or
278 \{...} - multiple prospects, separated by commas, are indicated, and
279 further input is required to distinguish a single one.
281 The displays for unambiguous matches have ` [Matched]' appended
282 \(whether complete or not), or ` \[No matches]', if no eligible
283 matches exist. \(Keybindings for uniquely matched commands
284 are exhibited within the square braces.)"
286 (let* ((comps (completion-all-sorted-completions))
287 (last (if (consp comps) (last comps)))
288 (base-size (cdr last))
289 (open-bracket (if require-match "(" "["))
290 (close-bracket (if require-match ")" "]")))
291 ;; `concat'/`mapconcat' is the slow part.
292 (if (not (consp comps))
293 (format " %sNo matches%s" open-bracket close-bracket)
294 (if last (setcdr last nil))
295 (let* ((most-try
296 (if (and base-size (> base-size 0))
297 (completion-try-completion
298 name candidates predicate (length name))
299 ;; If the `comps' are 0-based, the result should be
300 ;; the same with `comps'.
301 (completion-try-completion
302 name comps nil (length name))))
303 (most (if (consp most-try) (car most-try)
304 (if most-try (car comps) "")))
305 ;; Compare name and most, so we can determine if name is
306 ;; a prefix of most, or something else.
307 (compare (compare-strings name nil nil
308 most nil nil completion-ignore-case))
309 (determ (unless (or (eq t compare) (eq t most-try)
310 (= (setq compare (1- (abs compare)))
311 (length most)))
312 (concat open-bracket
313 (cond
314 ((= compare (length name))
315 ;; Typical case: name is a prefix.
316 (substring most compare))
317 ((< compare 5) most)
318 (t (concat "..." (substring most compare))))
319 close-bracket)))
320 ;;"-prospects" - more than one candidate
321 (prospects-len (+ (length determ) 6 ;; take {,...} into account
322 (string-width (buffer-string))))
323 (prospects-max
324 ;; Max total length to use, including the minibuffer content.
325 (* (+ icomplete-prospects-height
326 ;; If the minibuffer content already uses up more than
327 ;; one line, increase the allowable space accordingly.
328 (/ prospects-len (window-width)))
329 (window-width)))
330 (prefix-len
331 ;; Find the common prefix among `comps'.
332 (if (eq t (compare-strings (car comps) nil (length most)
333 most nil nil completion-ignore-case))
334 ;; Common case.
335 (length most)
336 ;; Else, use try-completion.
337 (let ((comps-prefix (try-completion "" comps)))
338 (and (stringp comps-prefix)
339 (length comps-prefix)))))
341 prospects most-is-exact comp limit)
342 (if (eq most-try t) ;; (or (null (cdr comps))
343 (setq prospects nil)
344 (while (and comps (not limit))
345 (setq comp
346 (if prefix-len (substring (car comps) prefix-len) (car comps))
347 comps (cdr comps))
348 (cond ((string-equal comp "") (setq most-is-exact t))
349 ((member comp prospects))
350 (t (setq prospects-len
351 (+ (string-width comp) 1 prospects-len))
352 (if (< prospects-len prospects-max)
353 (push comp prospects)
354 (setq limit t))))))
355 ;; Restore the base-size info, since completion-all-sorted-completions
356 ;; is cached.
357 (if last (setcdr last base-size))
358 (if prospects
359 (concat determ
361 (and most-is-exact ",")
362 (mapconcat 'identity (nreverse prospects) ",")
363 (and limit ",...")
364 "}")
365 (concat determ
366 " [Matched"
367 (let ((keys (and icomplete-show-key-bindings
368 (commandp (intern-soft most))
369 (icomplete-get-keys most))))
370 (if keys (concat "; " keys) ""))
371 "]"))))))
373 ;;_* Local emacs vars.
374 ;;Local variables:
375 ;;allout-layout: (-2 :)
376 ;;End:
378 ;; arch-tag: 339ec25a-0741-4eb6-be63-997532e89b0f
379 ;;; icomplete.el ends here