*** empty log message ***
[bpt/emacs.git] / lisp / icomplete.el
... / ...
CommitLineData
1;;; icomplete.el --- minibuffer completion incremental feedback
2
3;; Copyright (C) 1992, 1993, 1994, 1997, 1999, 2001, 2002, 2003,
4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
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
11
12;; This file is part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27;;; Commentary:
28
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
34;; See `icomplete-completions' docstring for a description of the
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
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.
49
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.
56
57;; klm.
58
59;;; Code:
60
61;;;_* Provide
62(provide 'icomplete)
63
64
65(defgroup icomplete nil
66 "Show completions dynamically in minibuffer."
67 :prefix "icomplete-"
68 :group 'minibuffer)
69
70(defvar icomplete-prospects-length 80)
71(make-obsolete-variable
72 'icomplete-prospects-length 'icomplete-prospects-height "23.1")
73
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
82(defcustom icomplete-compute-delay .3
83 "Completions-computation stall, used only with large-number completions.
84See `icomplete-delay-completions-threshold'."
85 :type 'number
86 :group 'icomplete)
87
88(defcustom icomplete-delay-completions-threshold 400
89 "Pending-completions number over which to apply `icomplete-compute-delay'."
90 :type 'integer
91 :group 'icomplete)
92
93(defcustom icomplete-max-delay-chars 3
94 "Maximum number of initial chars to apply icomplete compute delay."
95 :type 'integer
96 :group 'icomplete)
97
98(defcustom icomplete-show-key-bindings t
99 "If non-nil, show key bindings as well as completion for sole matches."
100 :type 'boolean
101 :group 'icomplete)
102
103(defcustom icomplete-minibuffer-setup-hook nil
104 "Icomplete-specific customization of minibuffer setup.
105
106This hook is run during minibuffer setup if icomplete is active.
107It is intended for use in customizing icomplete for interoperation
108with other features and packages. For instance:
109
110 \(add-hook 'icomplete-minibuffer-setup-hook
111 \(function
112 \(lambda ()
113 \(make-local-variable 'max-mini-window-height)
114 \(setq max-mini-window-height 3))))
115
116will constrain Emacs to a maximum minibuffer height of 3 lines when
117icompletion is occurring."
118 :type 'hook
119 :group 'icomplete)
120
121
122;;;_* Initialization
123
124;;;_ + Internal Variables
125;;;_ = icomplete-eoinput nil
126(defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
127 "Overlay used to display the list of completions.")
128
129;;;_ = icomplete-pre-command-hook
130(defvar icomplete-pre-command-hook nil
131 "Incremental-minibuffer-completion pre-command-hook.
132
133Is run in minibuffer before user input when `icomplete-mode' is non-nil.
134Use `icomplete-mode' function to set it up properly for incremental
135minibuffer completion.")
136(add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
137;;;_ = icomplete-post-command-hook
138(defvar icomplete-post-command-hook nil
139 "Incremental-minibuffer-completion post-command-hook.
140
141Is run in minibuffer after user input when `icomplete-mode' is non-nil.
142Use `icomplete-mode' function to set it up properly for incremental
143minibuffer completion.")
144(add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
145
146(defun icomplete-get-keys (func-name)
147 "Return strings naming keys bound to `func-name', or nil if none.
148Examines the prior, not current, buffer, presuming that current buffer
149is minibuffer."
150 (if (commandp func-name)
151 (save-excursion
152 (let* ((sym (intern func-name))
153 (buf (other-buffer nil t))
154 (keys (with-current-buffer buf (where-is-internal sym))))
155 (if keys
156 (concat "<"
157 (mapconcat 'key-description
158 (sort keys
159 #'(lambda (x y)
160 (< (length x) (length y))))
161 ", ")
162 ">"))))))
163;;;_ = icomplete-with-completion-tables
164(defvar icomplete-with-completion-tables '(internal-complete-buffer)
165 "Specialized completion tables with which icomplete should operate.
166
167Icomplete does not operate with any specialized completion tables
168except those on this list.")
169
170;;;_ > icomplete-mode (&optional prefix)
171;;;###autoload
172(define-minor-mode icomplete-mode
173 "Toggle incremental minibuffer completion for this Emacs session.
174With a numeric argument, turn Icomplete mode on if ARG is positive,
175otherwise turn it off."
176 :global t :group 'icomplete
177 (if icomplete-mode
178 ;; The following is not really necessary after first time -
179 ;; no great loss.
180 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
181 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
182
183;;;_ > icomplete-simple-completing-p ()
184(defun icomplete-simple-completing-p ()
185 "Non-nil if current window is minibuffer that's doing simple completion.
186
187Conditions are:
188 the selected window is a minibuffer,
189 and not in the middle of macro execution,
190 and `minibuffer-completion-table' is not a symbol (which would
191 indicate some non-standard, non-simple completion mechanism,
192 like file-name and other custom-func completions)."
193
194 (and (window-minibuffer-p (selected-window))
195 (not executing-kbd-macro)
196 minibuffer-completion-table
197 (or (not (functionp minibuffer-completion-table))
198 (eq icomplete-with-completion-tables t)
199 (member minibuffer-completion-table
200 icomplete-with-completion-tables))))
201
202;;;_ > icomplete-minibuffer-setup ()
203(defun icomplete-minibuffer-setup ()
204 "Run in minibuffer on activation to establish incremental completion.
205Usually run by inclusion in `minibuffer-setup-hook'."
206 (when (and icomplete-mode (icomplete-simple-completing-p))
207 (add-hook 'pre-command-hook
208 (lambda () (run-hooks 'icomplete-pre-command-hook))
209 nil t)
210 (add-hook 'post-command-hook
211 (lambda () (run-hooks 'icomplete-post-command-hook))
212 nil t)
213 (run-hooks 'icomplete-minibuffer-setup-hook)))
214;\f
215
216
217;;;_* Completion
218
219;;;_ > icomplete-tidy ()
220(defun icomplete-tidy ()
221 "Remove completions display \(if any) prior to new user input.
222Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
223and `minibuffer-setup-hook'."
224 (delete-overlay icomplete-overlay))
225
226;;;_ > icomplete-exhibit ()
227(defun icomplete-exhibit ()
228 "Insert icomplete completions display.
229Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
230and `minibuffer-setup-hook'."
231 (when (and icomplete-mode (icomplete-simple-completing-p))
232 (save-excursion
233 (goto-char (point-max))
234 ; Insert the match-status information:
235 (if (and (> (point-max) (minibuffer-prompt-end))
236 buffer-undo-list ; Wait for some user input.
237 (or
238 ;; Don't bother with delay after certain number of chars:
239 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
240 ;; Don't delay if alternatives number is small enough:
241 (and (sequencep minibuffer-completion-table)
242 (< (length minibuffer-completion-table)
243 icomplete-delay-completions-threshold))
244 ;; Delay - give some grace time for next keystroke, before
245 ;; embarking on computing completions:
246 (sit-for icomplete-compute-delay)))
247 (let ((text (while-no-input
248 (icomplete-completions
249 (field-string)
250 minibuffer-completion-table
251 minibuffer-completion-predicate
252 (not minibuffer-completion-confirm))))
253 (buffer-undo-list t)
254 deactivate-mark)
255 ;; Do nothing if while-no-input was aborted.
256 (when (stringp text)
257 (move-overlay icomplete-overlay (point) (point) (current-buffer))
258 ;; The current C cursor code doesn't know to use the overlay's
259 ;; marker's stickiness to figure out whether to place the cursor
260 ;; before or after the string, so let's spoon-feed it the pos.
261 (put-text-property 0 1 'cursor t text)
262 (overlay-put icomplete-overlay 'after-string text)))))))
263
264;;;_ > icomplete-completions (name candidates predicate require-match)
265(defun icomplete-completions (name candidates predicate require-match)
266 "Identify prospective candidates for minibuffer completion.
267
268The display is updated with each minibuffer keystroke during
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
277 further input is required to distinguish a single one.
278
279The displays for unambiguous matches have ` [Matched]' appended
280\(whether complete or not), or ` \[No matches]', if no eligible
281matches exist. \(Keybindings for uniquely matched commands
282are exhibited within the square braces.)"
283
284 (let* ((comps (completion-all-sorted-completions))
285 (last (if (consp comps) (last comps)))
286 (base-size (cdr last))
287 (open-bracket (if require-match "(" "["))
288 (close-bracket (if require-match ")" "]")))
289 ;; `concat'/`mapconcat' is the slow part.
290 (if (not (consp comps))
291 (format " %sNo matches%s" open-bracket close-bracket)
292 (if last (setcdr last nil))
293 (let* ((most-try
294 (if (and base-size (> base-size 0))
295 (completion-try-completion
296 name candidates predicate (length name))
297 ;; If the `comps' are 0-based, the result should be
298 ;; the same with `comps'.
299 (completion-try-completion
300 name comps nil (length name))))
301 (most (if (consp most-try) (car most-try)
302 (if most-try (car comps) "")))
303 ;; Compare name and most, so we can determine if name is
304 ;; a prefix of most, or something else.
305 (compare (compare-strings name nil nil
306 most nil nil completion-ignore-case))
307 (determ (unless (or (eq t compare) (eq t most-try)
308 (= (setq compare (1- (abs compare)))
309 (length most)))
310 (concat open-bracket
311 (cond
312 ((= compare (length name))
313 ;; Typical case: name is a prefix.
314 (substring most compare))
315 ((< compare 5) most)
316 (t (concat "..." (substring most compare))))
317 close-bracket)))
318 ;;"-prospects" - more than one candidate
319 (prospects-len (+ (length determ) 6 ;; take {,...} into account
320 (string-width (buffer-string))))
321 (prospects-max
322 ;; Max total length to use, including the minibuffer content.
323 (* (+ icomplete-max-minibuffer-height
324 ;; If the minibuffer content already uses up more than
325 ;; one line, increase the allowable space accordingly.
326 (/ prospects-len (window-width)))
327 (window-width)))
328 (prefix-len
329 ;; Find the common prefix among `comps'.
330 (if (eq t (compare-strings (car comps) nil (length most)
331 most nil nil completion-ignore-case))
332 ;; Common case.
333 (length most)
334 ;; Else, use try-completion.
335 (let ((comps-prefix (try-completion "" comps)))
336 (and (stringp comps-prefix)
337 (length comps-prefix)))))
338
339 prospects most-is-exact comp limit)
340 (if (eq most-try t) ;; (or (null (cdr comps))
341 (setq prospects nil)
342 (while (and comps (not limit))
343 (setq comp
344 (if prefix-len (substring (car comps) prefix-len) (car comps))
345 comps (cdr comps))
346 (cond ((string-equal comp "") (setq most-is-exact t))
347 ((member comp prospects))
348 (t (setq prospects-len
349 (+ (string-width comp) 1 prospects-len))
350 (if (< prospects-len prospects-max)
351 (push comp prospects)
352 (setq limit t))))))
353 ;; Restore the base-size info, since completion-all-sorted-completions
354 ;; is cached.
355 (if last (setcdr last base-size))
356 (if prospects
357 (concat determ
358 "{"
359 (and most-is-exact ",")
360 (mapconcat 'identity (nreverse prospects) ",")
361 (and limit ",...")
362 "}")
363 (concat determ
364 " [Matched"
365 (let ((keys (and icomplete-show-key-bindings
366 (commandp (intern-soft most))
367 (icomplete-get-keys most))))
368 (if keys (concat "; " keys) ""))
369 "]"))))))
370
371;;_* Local emacs vars.
372;;Local variables:
373;;allout-layout: (-2 :)
374;;End:
375
376;; arch-tag: 339ec25a-0741-4eb6-be63-997532e89b0f
377;;; icomplete.el ends here