Fix typo.
[bpt/emacs.git] / lisp / icomplete.el
CommitLineData
fa157191 1;;; icomplete.el --- minibuffer completion incremental feedback
239c87a1 2
0d30b337 3;; Copyright (C) 1992, 1993, 1994, 1997, 1999, 2001, 2002, 2003,
409cc4a3 4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
d462ff97 5
1934dbf4
GM
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.
be010748 10;; Keywords: help, abbrev
d462ff97 11
239c87a1 12;; This file is part of GNU Emacs.
d462ff97 13
eb3fa2cf 14;; GNU Emacs is free software: you can redistribute it and/or modify
239c87a1 15;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
d462ff97 18
239c87a1
RS
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.
c89164c5 23
239c87a1 24;; You should have received a copy of the GNU General Public License
eb3fa2cf 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
d462ff97 26
239c87a1 27;;; Commentary:
d462ff97 28
b578f267
EN
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.
33
5e537651 34;; See `icomplete-completions' docstring for a description of the
b578f267
EN
35;; icomplete display format.
36
37;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
38;; customize icomplete setup for interoperation with other
39;; minibuffer-oriented packages.
40
1934dbf4
GM
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.
92f7d003 49
b578f267
EN
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
5e537651 54;; contributors: Noah Friedman (restructuring as minor mode), Colin
1be5a284 55;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
b578f267
EN
56
57;; klm.
c89164c5 58
239c87a1
RS
59;;; Code:
60
61;;;_* Provide
d462ff97
RS
62(provide 'icomplete)
63
5e537651
SE
64
65(defgroup icomplete nil
66 "Show completions dynamically in minibuffer."
67 :prefix "icomplete-"
68 :group 'minibuffer)
69
1934dbf4
GM
70;;;_* User Customization variables
71(defcustom icomplete-prospects-length 80
077aec27 72 "Length of string displaying the prospects."
1934dbf4
GM
73 :type 'integer
74 :group 'icomplete)
75
5e537651 76(defcustom icomplete-compute-delay .3
077aec27
JB
77 "Completions-computation stall, used only with large-number completions.
78See `icomplete-delay-completions-threshold'."
5e537651
SE
79 :type 'number
80 :group 'icomplete)
81
82(defcustom icomplete-delay-completions-threshold 400
077aec27 83 "Pending-completions number over which to apply `icomplete-compute-delay'."
5e537651
SE
84 :type 'integer
85 :group 'icomplete)
86
87(defcustom icomplete-max-delay-chars 3
077aec27 88 "Maximum number of initial chars to apply icomplete compute delay."
5e537651
SE
89 :type 'integer
90 :group 'icomplete)
91
92(defcustom icomplete-show-key-bindings t
077aec27 93 "If non-nil, show key bindings as well as completion for sole matches."
5e537651
SE
94 :type 'boolean
95 :group 'icomplete)
96
97(defcustom icomplete-minibuffer-setup-hook nil
077aec27 98 "Icomplete-specific customization of minibuffer setup.
239c87a1 99
4837b516 100This hook is run during minibuffer setup if icomplete is active.
239c87a1 101It is intended for use in customizing icomplete for interoperation
9c8acefd 102with other features and packages. For instance:
239c87a1 103
92f7d003 104 \(add-hook 'icomplete-minibuffer-setup-hook
239c87a1
RS
105 \(function
106 \(lambda ()
9c8acefd
EZ
107 \(make-local-variable 'max-mini-window-height)
108 \(setq max-mini-window-height 3))))
239c87a1 109
9c8acefd 110will constrain Emacs to a maximum minibuffer height of 3 lines when
5e537651
SE
111icompletion is occurring."
112 :type 'hook
113 :group 'icomplete)
114
115
116;;;_* Initialization
d462ff97 117
c89164c5 118;;;_ + Internal Variables
47fda8fc
SM
119;;;_ = icomplete-eoinput nil
120(defvar icomplete-eoinput nil
d462ff97
RS
121 "Point where minibuffer input ends and completion info begins.")
122(make-variable-buffer-local 'icomplete-eoinput)
239c87a1
RS
123;;;_ = icomplete-pre-command-hook
124(defvar icomplete-pre-command-hook nil
125 "Incremental-minibuffer-completion pre-command-hook.
126
127Is run in minibuffer before user input when `icomplete-mode' is non-nil.
128Use `icomplete-mode' function to set it up properly for incremental
129minibuffer completion.")
130(add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
131;;;_ = icomplete-post-command-hook
132(defvar icomplete-post-command-hook nil
133 "Incremental-minibuffer-completion post-command-hook.
134
135Is run in minibuffer after user input when `icomplete-mode' is non-nil.
136Use `icomplete-mode' function to set it up properly for incremental
137minibuffer completion.")
138(add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
139
92f7d003 140(defun icomplete-get-keys (func-name)
1be5a284
RS
141 "Return strings naming keys bound to `func-name', or nil if none.
142Examines the prior, not current, buffer, presuming that current buffer
143is minibuffer."
144 (if (commandp func-name)
92f7d003
KH
145 (save-excursion
146 (let* ((sym (intern func-name))
78421c58 147 (buf (other-buffer nil t))
4afd234b 148 (keys (with-current-buffer buf (where-is-internal sym))))
92f7d003
KH
149 (if keys
150 (concat "<"
151 (mapconcat 'key-description
152 (sort keys
153 #'(lambda (x y)
154 (< (length x) (length y))))
155 ", ")
156 ">"))))))
397e713b
CY
157;;;_ = icomplete-with-completion-tables
158(defvar icomplete-with-completion-tables '(internal-complete-buffer)
159 "Specialized completion tables with which icomplete should operate.
160
161Icomplete does not operate with any specialized completion tables
162except those on this list.")
92f7d003 163
239c87a1 164;;;_ > icomplete-mode (&optional prefix)
c89164c5 165;;;###autoload
47fda8fc 166(define-minor-mode icomplete-mode
4b404c58 167 "Toggle incremental minibuffer completion for this Emacs session.
4837b516
GM
168With a numeric argument, turn Icomplete mode on if ARG is positive,
169otherwise turn it off."
47fda8fc
SM
170 :global t :group 'icomplete
171 (if icomplete-mode
4b404c58
GM
172 ;; The following is not really necessary after first time -
173 ;; no great loss.
47fda8fc
SM
174 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
175 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
239c87a1
RS
176
177;;;_ > icomplete-simple-completing-p ()
178(defun icomplete-simple-completing-p ()
239c87a1
RS
179 "Non-nil if current window is minibuffer that's doing simple completion.
180
181Conditions are:
182 the selected window is a minibuffer,
183 and not in the middle of macro execution,
47fda8fc 184 and `minibuffer-completion-table' is not a symbol (which would
81276211 185 indicate some non-standard, non-simple completion mechanism,
239c87a1
RS
186 like file-name and other custom-func completions)."
187
188 (and (window-minibuffer-p (selected-window))
efcf38c7 189 (not executing-kbd-macro)
c22e3cd4 190 minibuffer-completion-table
397e713b 191 (or (not (functionp minibuffer-completion-table))
476d2aef 192 (eq icomplete-with-completion-tables t)
397e713b 193 (member minibuffer-completion-table
3f56acf9 194 icomplete-with-completion-tables))))
d183f322 195
239c87a1 196;;;_ > icomplete-minibuffer-setup ()
239c87a1 197(defun icomplete-minibuffer-setup ()
239c87a1 198 "Run in minibuffer on activation to establish incremental completion.
d183f322 199Usually run by inclusion in `minibuffer-setup-hook'."
47fda8fc
SM
200 (when (and icomplete-mode (icomplete-simple-completing-p))
201 (add-hook 'pre-command-hook
202 (lambda () (run-hooks 'icomplete-pre-command-hook))
203 nil t)
204 (add-hook 'post-command-hook
205 (lambda () (run-hooks 'icomplete-post-command-hook))
206 nil t)
207 (run-hooks 'icomplete-minibuffer-setup-hook)))
1934dbf4
GM
208;\f
209
210
239c87a1
RS
211;;;_* Completion
212
213;;;_ > icomplete-tidy ()
214(defun icomplete-tidy ()
215 "Remove completions display \(if any) prior to new user input.
d183f322 216Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
239c87a1 217and `minibuffer-setup-hook'."
fe689fb6 218 (when (and icomplete-mode icomplete-eoinput)
239c87a1 219
47fda8fc 220 (unless (>= icomplete-eoinput (point-max))
7a78ffec
EZ
221 (let ((buffer-undo-list t) ; prevent entry
222 deactivate-mark)
47fda8fc 223 (delete-region icomplete-eoinput (point-max))))
239c87a1 224
47fda8fc
SM
225 ;; Reestablish the safe value.
226 (setq icomplete-eoinput nil)))
d183f322 227
239c87a1 228;;;_ > icomplete-exhibit ()
d462ff97 229(defun icomplete-exhibit ()
239c87a1 230 "Insert icomplete completions display.
d183f322 231Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
239c87a1 232and `minibuffer-setup-hook'."
fe689fb6 233 (when (and icomplete-mode (icomplete-simple-completing-p))
47fda8fc
SM
234 (save-excursion
235 (goto-char (point-max))
236 ;; Register the end of input, so we know where the extra stuff
237 ;; (match-status info) begins:
238 (setq icomplete-eoinput (point))
d462ff97 239 ; Insert the match-status information:
47fda8fc
SM
240 (if (and (> (point-max) (minibuffer-prompt-end))
241 buffer-undo-list ; Wait for some user input.
242 (or
243 ;; Don't bother with delay after certain number of chars:
244 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
245 ;; Don't delay if alternatives number is small enough:
246 (and (sequencep minibuffer-completion-table)
247 (< (length minibuffer-completion-table)
248 icomplete-delay-completions-threshold))
249 ;; Delay - give some grace time for next keystroke, before
250 ;; embarking on computing completions:
251 (sit-for icomplete-compute-delay)))
252 (let ((text (while-no-input
9a84ddd8
RS
253 (list
254 (icomplete-completions
255 (field-string)
256 minibuffer-completion-table
257 minibuffer-completion-predicate
258 (not minibuffer-completion-confirm)))))
7a78ffec
EZ
259 (buffer-undo-list t)
260 deactivate-mark)
9a84ddd8
RS
261 ;; Do nothing if while-no-input was aborted.
262 (if (consp text) (insert (car text))))))))
d183f322 263
239c87a1
RS
264;;;_ > icomplete-completions (name candidates predicate require-match)
265(defun icomplete-completions (name candidates predicate require-match)
d462ff97
RS
266 "Identify prospective candidates for minibuffer completion.
267
c89164c5 268The display is updated with each minibuffer keystroke during
d462ff97
RS
269minibuffer completion.
270
271Prospective completion suffixes (if any) are displayed, bracketed by
272one of \(), \[], or \{} pairs. The choice of brackets is as follows:
273
274 \(...) - a single prospect is identified and matching is enforced,
275 \[...] - a single prospect is identified but matching is optional, or
276 \{...} - multiple prospects, separated by commas, are indicated, and
81276211 277 further input is required to distinguish a single one.
d462ff97 278
81276211 279The displays for unambiguous matches have ` [Matched]' appended
d183f322 280\(whether complete or not), or ` \[No matches]', if no eligible
1be5a284 281matches exist. \(Keybindings for uniquely matched commands
92f7d003
KH
282are exhibited within the square braces.)"
283
476d2aef
SM
284 (let* ((comps (completion-all-completions name candidates predicate
285 (length name)))
286 (last (last comps))
287 (base-size (if (consp last) (prog1 (cdr last) (setcdr last nil))))
288 (open-bracket (if require-match "(" "["))
289 (close-bracket (if require-match ")" "]")))
1934dbf4
GM
290 ;; `concat'/`mapconcat' is the slow part. With the introduction of
291 ;; `icomplete-prospects-length', there is no need for `catch'/`throw'.
476d2aef
SM
292 (if (null comps)
293 (format " %sNo matches%s" open-bracket close-bracket)
294 (let* ((most-try (completion-try-completion
295 name
296 ;; If the `comps' are 0-based, the result should be
297 ;; the same with `comps'.
298 (if base-size candidates comps)
299 predicate
300 (length name)))
301 (most (if (consp most-try) (car most-try) (car comps)))
302 ;; Compare name and most, so we can determine if name is
303 ;; a prefix of most, or something else.
304 (compare (compare-strings name nil nil
305 most nil nil completion-ignore-case))
306 (determ (unless (or (eq t compare) (eq t most-try)
307 (= (setq compare (1- (abs compare)))
308 (length most)))
309 (concat open-bracket
310 (cond
311 ((= compare (length name))
312 ;; Typical case: name is a prefix.
313 (substring most compare))
314 ((< compare 5) most)
315 (t (concat "..." (substring most compare))))
316 close-bracket)))
47fda8fc 317 ;;"-prospects" - more than one candidate
7d94fb17 318 (prospects-len (+ (length determ) 6)) ;; take {,...} into account
476d2aef
SM
319 (prefix-len
320 ;; Find the common prefix among `comps'.
321 (if (eq t (compare-strings (car comps) nil (length most)
322 most nil nil case-fold-search))
323 ;; Common case.
324 (length most)
325 ;; Else, use try-completion.
326 (let ((comps-prefix (try-completion "" comps)))
327 (and (stringp comps-prefix)
328 (length comps-prefix)))))
329
7d94fb17 330 prospects most-is-exact comp limit)
476d2aef 331 (if (or (eq most-try t) (null (cdr comps)))
1934dbf4 332 (setq prospects nil)
7d94fb17 333 (while (and comps (not limit))
476d2aef
SM
334 (setq comp
335 (if prefix-len (substring (car comps) prefix-len) (car comps))
1934dbf4
GM
336 comps (cdr comps))
337 (cond ((string-equal comp "") (setq most-is-exact t))
338 ((member comp prospects))
7d94fb17
JB
339 (t (setq prospects-len (+ (length comp) 1 prospects-len))
340 (if (< prospects-len icomplete-prospects-length)
341 (setq prospects (cons comp prospects))
342 (setq limit t))))))
1934dbf4
GM
343 (if prospects
344 (concat determ
47fda8fc 345 "{"
1934dbf4
GM
346 (and most-is-exact ",")
347 (mapconcat 'identity
348 (sort prospects (function string-lessp))
349 ",")
350 (and comps ",...")
47fda8fc 351 "}")
1934dbf4
GM
352 (concat determ
353 " [Matched"
354 (let ((keys (and icomplete-show-key-bindings
355 (commandp (intern-soft most))
356 (icomplete-get-keys most))))
47fda8fc 357 (if keys (concat "; " keys) ""))
1934dbf4 358 "]"))))))
d462ff97 359
476d2aef
SM
360;;_* Local emacs vars.
361;;Local variables:
362;;allout-layout: (-2 :)
363;;End:
d462ff97 364
47fda8fc 365;; arch-tag: 339ec25a-0741-4eb6-be63-997532e89b0f
d462ff97 366;;; icomplete.el ends here