Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / icomplete.el
blobe3635a6216e264f994f700e2a44cd42584008bc2
1 ;;; icomplete.el --- minibuffer completion incremental feedback
3 ;; Copyright (C) 1992-1994, 1997, 1999, 2001-2014 Free Software
4 ;; 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 (defgroup icomplete nil
62 "Show completions dynamically in minibuffer."
63 :prefix "icomplete-"
64 :group 'minibuffer)
66 (defvar icomplete-prospects-length 80)
67 (make-obsolete-variable
68 'icomplete-prospects-length 'icomplete-prospects-height "23.1")
70 (defcustom icomplete-separator " | "
71 "String used by icomplete to separate alternatives in the minibuffer."
72 :type 'string
73 :version "24.4")
75 (defcustom icomplete-hide-common-prefix t
76 "When non-nil, hide common prefix from completion candidates.
77 When nil, show candidates in full."
78 :type 'boolean
79 :version "24.4")
81 (defcustom icomplete-show-matches-on-no-input nil
82 "When non-nil, show completions when first prompting for input."
83 :type 'boolean
84 :version "24.4")
86 (defface icomplete-first-match '((t :weight bold))
87 "Face used by icomplete for highlighting first match."
88 :version "24.4")
90 ;;;_* User Customization variables
91 (defcustom icomplete-prospects-height
92 ;; 20 is an estimated common size for the prompt + minibuffer content, to
93 ;; try to guess the number of lines used up by icomplete-prospects-length.
94 (+ 1 (/ (+ icomplete-prospects-length 20) (window-width)))
95 "Maximum number of lines to use in the minibuffer."
96 :type 'integer
97 :version "23.1")
99 (defcustom icomplete-compute-delay .3
100 "Completions-computation stall, used only with large-number completions.
101 See `icomplete-delay-completions-threshold'."
102 :type 'number)
104 (defcustom icomplete-delay-completions-threshold 400
105 "Pending-completions number over which to apply `icomplete-compute-delay'."
106 :type 'integer)
108 (defcustom icomplete-max-delay-chars 3
109 "Maximum number of initial chars to apply icomplete compute delay."
110 :type 'integer)
112 (defvar icomplete-in-buffer nil
113 "If non-nil, also use Icomplete when completing in non-mini buffers.")
115 (defcustom icomplete-minibuffer-setup-hook nil
116 "Icomplete-specific customization of minibuffer setup.
118 This hook is run during minibuffer setup if icomplete is active.
119 It is intended for use in customizing icomplete for interoperation
120 with other features and packages. For instance:
122 \(add-hook 'icomplete-minibuffer-setup-hook
123 \(function
124 \(lambda ()
125 \(make-local-variable 'max-mini-window-height)
126 \(setq max-mini-window-height 3))))
128 will constrain Emacs to a maximum minibuffer height of 3 lines when
129 icompletion is occurring."
130 :type 'hook
131 :group 'icomplete)
134 ;;;_* Initialization
136 ;;;_ + Internal Variables
137 ;;;_ = icomplete-eoinput nil
138 (defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
139 "Overlay used to display the list of completions.")
141 (defun icomplete-pre-command-hook ()
142 (let ((non-essential t))
143 (icomplete-tidy)))
145 (defun icomplete-post-command-hook ()
146 (let ((non-essential t)) ;E.g. don't prompt for password!
147 (icomplete-exhibit)))
149 ;;;_ = icomplete-with-completion-tables
150 (defcustom icomplete-with-completion-tables t
151 "Specialized completion tables with which icomplete should operate.
153 Icomplete does not operate with any specialized completion tables
154 except those on this list."
155 :version "24.4"
156 :type '(choice (const :tag "All" t)
157 (repeat function)))
159 (defvar icomplete-minibuffer-map
160 (let ((map (make-sparse-keymap)))
161 (define-key map [?\M-\t] 'minibuffer-force-complete)
162 (define-key map [?\C-j] 'minibuffer-force-complete-and-exit)
163 (define-key map [?\C-.] 'icomplete-forward-completions)
164 (define-key map [?\C-,] 'icomplete-backward-completions)
165 map))
167 (defun icomplete-forward-completions ()
168 "Step forward completions by one entry.
169 Second entry becomes the first and can be selected with
170 `minibuffer-force-complete-and-exit'."
171 (interactive)
172 (let* ((beg (icomplete--field-beg))
173 (end (icomplete--field-end))
174 (comps (completion-all-sorted-completions beg end))
175 (last (last comps)))
176 (when comps
177 (setcdr last (cons (car comps) (cdr last)))
178 (completion--cache-all-sorted-completions beg end (cdr comps)))))
180 (defun icomplete-backward-completions ()
181 "Step backward completions by one entry.
182 Last entry becomes the first and can be selected with
183 `minibuffer-force-complete-and-exit'."
184 (interactive)
185 (let* ((beg (icomplete--field-beg))
186 (end (icomplete--field-end))
187 (comps (completion-all-sorted-completions beg end))
188 (last-but-one (last comps 2))
189 (last (cdr last-but-one)))
190 (when (consp last) ; At least two elements in comps
191 (setcdr last-but-one (cdr last))
192 (push (car last) comps)
193 (completion--cache-all-sorted-completions beg end comps))))
195 ;;;_ > icomplete-mode (&optional prefix)
196 ;;;###autoload
197 (define-minor-mode icomplete-mode
198 "Toggle incremental minibuffer completion (Icomplete mode).
199 With a prefix argument ARG, enable Icomplete mode if ARG is
200 positive, and disable it otherwise. If called from Lisp, enable
201 the mode if ARG is omitted or nil."
202 :global t :group 'icomplete
203 (remove-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)
204 (remove-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup)
205 (when icomplete-mode
206 (when icomplete-in-buffer
207 (add-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup))
208 (add-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)))
210 (defun icomplete--completion-table ()
211 (if (window-minibuffer-p) minibuffer-completion-table
212 (or (nth 2 completion-in-region--data)
213 (message "In %S (w=%S): %S"
214 (current-buffer) (selected-window) (window-minibuffer-p)))))
215 (defun icomplete--completion-predicate ()
216 (if (window-minibuffer-p) minibuffer-completion-predicate
217 (nth 3 completion-in-region--data)))
218 (defun icomplete--field-string ()
219 (if (window-minibuffer-p) (minibuffer-contents)
220 (buffer-substring-no-properties
221 (nth 0 completion-in-region--data)
222 (nth 1 completion-in-region--data))))
223 (defun icomplete--field-beg ()
224 (if (window-minibuffer-p) (minibuffer-prompt-end)
225 (nth 0 completion-in-region--data)))
226 (defun icomplete--field-end ()
227 (if (window-minibuffer-p) (point-max)
228 (nth 1 completion-in-region--data)))
230 ;;;_ > icomplete-simple-completing-p ()
231 (defun icomplete-simple-completing-p ()
232 "Non-nil if current window is minibuffer that's doing simple completion.
234 Conditions are:
235 the selected window is a minibuffer,
236 and not in the middle of macro execution,
237 and the completion table is not a function (which would
238 indicate some non-standard, non-simple completion mechanism,
239 like file-name and other custom-func completions)."
241 (unless executing-kbd-macro
242 (let ((table (icomplete--completion-table)))
243 (and table
244 (or (not (functionp table))
245 (eq icomplete-with-completion-tables t)
246 (member table icomplete-with-completion-tables))))))
248 ;;;_ > icomplete-minibuffer-setup ()
249 (defun icomplete-minibuffer-setup ()
250 "Run in minibuffer on activation to establish incremental completion.
251 Usually run by inclusion in `minibuffer-setup-hook'."
252 (when (and icomplete-mode (icomplete-simple-completing-p))
253 (set (make-local-variable 'completion-show-inline-help) nil)
254 (use-local-map (make-composed-keymap icomplete-minibuffer-map
255 (current-local-map)))
256 (add-hook 'pre-command-hook #'icomplete-pre-command-hook nil t)
257 (add-hook 'post-command-hook #'icomplete-post-command-hook nil t)
258 (run-hooks 'icomplete-minibuffer-setup-hook)
259 (when icomplete-show-matches-on-no-input
260 (icomplete-exhibit))))
262 (defvar icomplete--in-region-buffer nil)
264 (defun icomplete--in-region-setup ()
265 (when (or (not completion-in-region-mode)
266 (and icomplete--in-region-buffer
267 (not (eq icomplete--in-region-buffer (current-buffer)))))
268 (with-current-buffer (or icomplete--in-region-buffer (current-buffer))
269 (setq icomplete--in-region-buffer nil)
270 (delete-overlay icomplete-overlay)
271 (kill-local-variable 'completion-show-inline-help)
272 (remove-hook 'pre-command-hook 'icomplete-pre-command-hook t)
273 (remove-hook 'post-command-hook 'icomplete-post-command-hook t)
274 (message nil)))
275 (when (and completion-in-region-mode
276 icomplete-mode (icomplete-simple-completing-p))
277 (setq icomplete--in-region-buffer (current-buffer))
278 (set (make-local-variable 'completion-show-inline-help) nil)
279 (let ((tem (assq 'completion-in-region-mode
280 minor-mode-overriding-map-alist)))
281 (unless (memq icomplete-minibuffer-map (cdr tem))
282 (setcdr tem (make-composed-keymap icomplete-minibuffer-map
283 (cdr tem)))))
284 (add-hook 'pre-command-hook 'icomplete-pre-command-hook nil t)
285 (add-hook 'post-command-hook 'icomplete-post-command-hook nil t)))
289 ;;;_* Completion
291 ;;;_ > icomplete-tidy ()
292 (defun icomplete-tidy ()
293 "Remove completions display \(if any) prior to new user input.
294 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
295 and `minibuffer-setup-hook'."
296 (delete-overlay icomplete-overlay))
298 ;;;_ > icomplete-exhibit ()
299 (defun icomplete-exhibit ()
300 "Insert icomplete completions display.
301 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
302 and `minibuffer-setup-hook'."
303 (when (and icomplete-mode
304 (icomplete-simple-completing-p)) ;Shouldn't be necessary.
305 (save-excursion
306 (goto-char (point-max))
307 ; Insert the match-status information:
308 (if (and (or icomplete-show-matches-on-no-input
309 (> (icomplete--field-end) (icomplete--field-beg)))
311 ;; Don't bother with delay after certain number of chars:
312 (> (- (point) (icomplete--field-beg))
313 icomplete-max-delay-chars)
314 ;; Don't delay if the completions are known.
315 completion-all-sorted-completions
316 ;; Don't delay if alternatives number is small enough:
317 (and (sequencep (icomplete--completion-table))
318 (< (length (icomplete--completion-table))
319 icomplete-delay-completions-threshold))
320 ;; Delay - give some grace time for next keystroke, before
321 ;; embarking on computing completions:
322 (sit-for icomplete-compute-delay)))
323 (let* ((field-string (icomplete--field-string))
324 (text (while-no-input
325 (icomplete-completions
326 field-string
327 (icomplete--completion-table)
328 (icomplete--completion-predicate)
329 (if (window-minibuffer-p)
330 (not minibuffer-completion-confirm)))))
331 (buffer-undo-list t)
332 deactivate-mark)
333 ;; Do nothing if while-no-input was aborted.
334 (when (stringp text)
335 (move-overlay icomplete-overlay (point) (point) (current-buffer))
336 ;; The current C cursor code doesn't know to use the overlay's
337 ;; marker's stickiness to figure out whether to place the cursor
338 ;; before or after the string, so let's spoon-feed it the pos.
339 (put-text-property 0 1 'cursor t text)
340 (overlay-put icomplete-overlay 'after-string text)))))))
342 ;;;_ > icomplete-completions (name candidates predicate require-match)
343 (defun icomplete-completions (name candidates predicate require-match)
344 "Identify prospective candidates for minibuffer completion.
346 The display is updated with each minibuffer keystroke during
347 minibuffer completion.
349 Prospective completion suffixes (if any) are displayed, bracketed by
350 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
352 \(...) - a single prospect is identified and matching is enforced,
353 \[...] - a single prospect is identified but matching is optional, or
354 \{...} - multiple prospects, separated by commas, are indicated, and
355 further input is required to distinguish a single one.
357 The displays for unambiguous matches have ` [Matched]' appended
358 \(whether complete or not), or ` \[No matches]', if no eligible
359 matches exist. \(Keybindings for uniquely matched commands
360 are exhibited within the square braces.)"
362 (let* ((minibuffer-completion-table candidates)
363 (minibuffer-completion-predicate predicate)
364 (md (completion--field-metadata (icomplete--field-beg)))
365 (comps (completion-all-sorted-completions
366 (icomplete--field-beg) (icomplete--field-end)))
367 (last (if (consp comps) (last comps)))
368 (base-size (cdr last))
369 (open-bracket (if require-match "(" "["))
370 (close-bracket (if require-match ")" "]")))
371 ;; `concat'/`mapconcat' is the slow part.
372 (if (not (consp comps))
373 (progn ;;(debug (format "Candidates=%S field=%S" candidates name))
374 (format " %sNo matches%s" open-bracket close-bracket))
375 (if last (setcdr last nil))
376 (let* ((most-try
377 (if (and base-size (> base-size 0))
378 (completion-try-completion
379 name candidates predicate (length name) md)
380 ;; If the `comps' are 0-based, the result should be
381 ;; the same with `comps'.
382 (completion-try-completion
383 name comps nil (length name) md)))
384 (most (if (consp most-try) (car most-try)
385 (if most-try (car comps) "")))
386 ;; Compare name and most, so we can determine if name is
387 ;; a prefix of most, or something else.
388 (compare (compare-strings name nil nil
389 most nil nil completion-ignore-case))
390 (determ (unless (or (eq t compare) (eq t most-try)
391 (= (setq compare (1- (abs compare)))
392 (length most)))
393 (concat open-bracket
394 (cond
395 ((= compare (length name))
396 ;; Typical case: name is a prefix.
397 (substring most compare))
398 ;; Don't bother truncating if it doesn't gain
399 ;; us at least 2 columns.
400 ((< compare 3) most)
401 (t (concat "…" (substring most compare))))
402 close-bracket)))
403 ;;"-prospects" - more than one candidate
404 (prospects-len (+ (string-width
405 (or determ (concat open-bracket close-bracket)))
406 (string-width icomplete-separator)
407 3 ;; take {…} into account
408 (string-width (buffer-string))))
409 (prospects-max
410 ;; Max total length to use, including the minibuffer content.
411 (* (+ icomplete-prospects-height
412 ;; If the minibuffer content already uses up more than
413 ;; one line, increase the allowable space accordingly.
414 (/ prospects-len (window-width)))
415 (window-width)))
416 ;; Find the common prefix among `comps'.
417 ;; We can't use the optimization below because its assumptions
418 ;; aren't always true, e.g. when completion-cycling (bug#10850):
419 ;; (if (eq t (compare-strings (car comps) nil (length most)
420 ;; most nil nil completion-ignore-case))
421 ;; ;; Common case.
422 ;; (length most)
423 ;; Else, use try-completion.
424 (prefix (when icomplete-hide-common-prefix
425 (try-completion "" comps)))
426 (prefix-len
427 (and (stringp prefix)
428 ;; Only hide the prefix if the corresponding info
429 ;; is already displayed via `most'.
430 (string-prefix-p prefix most t)
431 (length prefix))) ;;)
432 prospects comp limit)
433 (if (or (eq most-try t) (not (consp (cdr comps))))
434 (setq prospects nil)
435 (when (member name comps)
436 ;; NAME is complete but not unique. This scenario poses
437 ;; following UI issues:
439 ;; - When `icomplete-hide-common-prefix' is non-nil, NAME
440 ;; is stripped empty. This would make the entry
441 ;; inconspicuous.
443 ;; - Due to sorting of completions, NAME may not be the
444 ;; first of the prospects and could be hidden deep in
445 ;; the displayed string.
447 ;; - Because of `icomplete-prospects-height' , NAME may
448 ;; not even be displayed to the user.
450 ;; To circumvent all the above problems, provide a visual
451 ;; cue to the user via an "empty string" in the try
452 ;; completion field.
453 (setq determ (concat open-bracket "" close-bracket)))
454 ;; Compute prospects for display.
455 (while (and comps (not limit))
456 (setq comp
457 (if prefix-len (substring (car comps) prefix-len) (car comps))
458 comps (cdr comps))
459 (setq prospects-len
460 (+ (string-width comp)
461 (string-width icomplete-separator)
462 prospects-len))
463 (if (< prospects-len prospects-max)
464 (push comp prospects)
465 (setq limit t))))
466 (setq prospects (nreverse prospects))
467 ;; Decorate first of the prospects.
468 (when prospects
469 (let ((first (copy-sequence (pop prospects))))
470 (put-text-property 0 (length first)
471 'face 'icomplete-first-match first)
472 (push first prospects)))
473 ;; Restore the base-size info, since completion-all-sorted-completions
474 ;; is cached.
475 (if last (setcdr last base-size))
476 (if prospects
477 (concat determ
479 (mapconcat 'identity prospects icomplete-separator)
480 (and limit (concat icomplete-separator "…"))
481 "}")
482 (concat determ " [Matched]"))))))
484 ;;;_* Provide
485 (provide 'icomplete)
487 ;;_* Local emacs vars.
488 ;;Local variables:
489 ;;allout-layout: (-2 :)
490 ;;End:
492 ;;; icomplete.el ends here