Merge from emacs-24; up to 2012-12-19T19:51:40Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / icomplete.el
CommitLineData
fa157191 1;;; icomplete.el --- minibuffer completion incremental feedback
239c87a1 2
ab422c4d
PE
3;; Copyright (C) 1992-1994, 1997, 1999, 2001-2013 Free Software
4;; 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
c2d0b538
SM
70(defvar icomplete-prospects-length 80)
71(make-obsolete-variable
72 'icomplete-prospects-length 'icomplete-prospects-height "23.1")
73
cc37e70f
J
74(defcustom icomplete-separator " | "
75 "String used by icomplete to separate alternatives in the minibuffer."
76 :type 'string
fb3bf6ce 77 :version "24.4")
cc37e70f 78
5cbfe5b9
J
79(defcustom icomplete-hide-common-prefix t
80 "When non-nil, hide common prefix from completion candidates.
81When nil, show candidates in full."
82 :type 'boolean
83 :version "24.4"
84 :group 'icomplete)
85
86(defface icomplete-first-match '((t :weight bold))
87 "Face used by icomplete for highlighting first match."
88 :version "24.4"
89 :group 'icomplete)
90
1934dbf4 91;;;_* User Customization variables
c2d0b538
SM
92(defcustom icomplete-prospects-height
93 ;; 20 is an estimated common size for the prompt + minibuffer content, to
94 ;; try to guess the number of lines used up by icomplete-prospects-length.
95 (+ 1 (/ (+ icomplete-prospects-length 20) (window-width)))
96 "Maximum number of lines to use in the minibuffer."
dfb4dab1 97 :type 'integer
af09cfd7
JB
98 :group 'icomplete
99 :version "23.1")
1934dbf4 100
5e537651 101(defcustom icomplete-compute-delay .3
077aec27
JB
102 "Completions-computation stall, used only with large-number completions.
103See `icomplete-delay-completions-threshold'."
5e537651
SE
104 :type 'number
105 :group 'icomplete)
106
107(defcustom icomplete-delay-completions-threshold 400
077aec27 108 "Pending-completions number over which to apply `icomplete-compute-delay'."
5e537651
SE
109 :type 'integer
110 :group 'icomplete)
111
112(defcustom icomplete-max-delay-chars 3
077aec27 113 "Maximum number of initial chars to apply icomplete compute delay."
5e537651
SE
114 :type 'integer
115 :group 'icomplete)
116
5e537651 117(defcustom icomplete-minibuffer-setup-hook nil
077aec27 118 "Icomplete-specific customization of minibuffer setup.
239c87a1 119
4837b516 120This hook is run during minibuffer setup if icomplete is active.
239c87a1 121It is intended for use in customizing icomplete for interoperation
9c8acefd 122with other features and packages. For instance:
239c87a1 123
92f7d003 124 \(add-hook 'icomplete-minibuffer-setup-hook
239c87a1
RS
125 \(function
126 \(lambda ()
9c8acefd
EZ
127 \(make-local-variable 'max-mini-window-height)
128 \(setq max-mini-window-height 3))))
239c87a1 129
9c8acefd 130will constrain Emacs to a maximum minibuffer height of 3 lines when
5e537651
SE
131icompletion is occurring."
132 :type 'hook
133 :group 'icomplete)
134
135
136;;;_* Initialization
d462ff97 137
c89164c5 138;;;_ + Internal Variables
47fda8fc 139;;;_ = icomplete-eoinput nil
31d4b748
SM
140(defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
141 "Overlay used to display the list of completions.")
142
239c87a1
RS
143;;;_ = icomplete-pre-command-hook
144(defvar icomplete-pre-command-hook nil
145 "Incremental-minibuffer-completion pre-command-hook.
146
147Is run in minibuffer before user input when `icomplete-mode' is non-nil.
148Use `icomplete-mode' function to set it up properly for incremental
149minibuffer completion.")
150(add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
151;;;_ = icomplete-post-command-hook
152(defvar icomplete-post-command-hook nil
153 "Incremental-minibuffer-completion post-command-hook.
154
155Is run in minibuffer after user input when `icomplete-mode' is non-nil.
156Use `icomplete-mode' function to set it up properly for incremental
157minibuffer completion.")
158(add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
159
397e713b
CY
160;;;_ = icomplete-with-completion-tables
161(defvar icomplete-with-completion-tables '(internal-complete-buffer)
162 "Specialized completion tables with which icomplete should operate.
163
164Icomplete does not operate with any specialized completion tables
165except those on this list.")
92f7d003 166
cc37e70f
J
167(defvar icomplete-minibuffer-map
168 (let ((map (make-sparse-keymap)))
169 (define-key map [?\M-\t] 'minibuffer-force-complete)
170 (define-key map [?\C-j] 'minibuffer-force-complete-and-exit)
171 (define-key map [?\C-s] 'icomplete-forward-completions)
172 (define-key map [?\C-r] 'icomplete-backward-completions)
173 map))
174
175(defun icomplete-forward-completions ()
176 "Step forward completions by one entry.
177Second entry becomes the first and can be selected with
178`minibuffer-force-complete-and-exit'."
179 (interactive)
180 (let* ((comps (completion-all-sorted-completions))
181 (last (last comps)))
6130b96a
J
182 (when comps
183 (setcdr last (cons (car comps) (cdr last)))
184 (completion--cache-all-sorted-completions (cdr comps)))))
cc37e70f
J
185
186(defun icomplete-backward-completions ()
187 "Step backward completions by one entry.
188Last entry becomes the first and can be selected with
189`minibuffer-force-complete-and-exit'."
190 (interactive)
191 (let* ((comps (completion-all-sorted-completions))
192 (last-but-one (last comps 2))
193 (last (cdr last-but-one)))
6130b96a 194 (when (consp last) ; At least two elements in comps
cc37e70f
J
195 (setcdr last-but-one (cdr last))
196 (push (car last) comps)
197 (completion--cache-all-sorted-completions comps))))
198
239c87a1 199;;;_ > icomplete-mode (&optional prefix)
c89164c5 200;;;###autoload
47fda8fc 201(define-minor-mode icomplete-mode
06e21633
CY
202 "Toggle incremental minibuffer completion (Icomplete mode).
203With a prefix argument ARG, enable Icomplete mode if ARG is
204positive, and disable it otherwise. If called from Lisp, enable
205the mode if ARG is omitted or nil."
47fda8fc
SM
206 :global t :group 'icomplete
207 (if icomplete-mode
4b404c58
GM
208 ;; The following is not really necessary after first time -
209 ;; no great loss.
2176854d
JB
210 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
211 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
239c87a1
RS
212
213;;;_ > icomplete-simple-completing-p ()
214(defun icomplete-simple-completing-p ()
239c87a1
RS
215 "Non-nil if current window is minibuffer that's doing simple completion.
216
217Conditions are:
218 the selected window is a minibuffer,
219 and not in the middle of macro execution,
47fda8fc 220 and `minibuffer-completion-table' is not a symbol (which would
81276211 221 indicate some non-standard, non-simple completion mechanism,
239c87a1
RS
222 like file-name and other custom-func completions)."
223
224 (and (window-minibuffer-p (selected-window))
efcf38c7 225 (not executing-kbd-macro)
c22e3cd4 226 minibuffer-completion-table
397e713b 227 (or (not (functionp minibuffer-completion-table))
476d2aef 228 (eq icomplete-with-completion-tables t)
397e713b 229 (member minibuffer-completion-table
3f56acf9 230 icomplete-with-completion-tables))))
d183f322 231
239c87a1 232;;;_ > icomplete-minibuffer-setup ()
239c87a1 233(defun icomplete-minibuffer-setup ()
239c87a1 234 "Run in minibuffer on activation to establish incremental completion.
d183f322 235Usually run by inclusion in `minibuffer-setup-hook'."
47fda8fc 236 (when (and icomplete-mode (icomplete-simple-completing-p))
2176854d 237 (set (make-local-variable 'completion-show-inline-help) nil)
cc37e70f
J
238 (use-local-map (make-composed-keymap icomplete-minibuffer-map
239 (current-local-map)))
47fda8fc 240 (add-hook 'pre-command-hook
f143bfe3
SM
241 (lambda () (let ((non-essential t))
242 (run-hooks 'icomplete-pre-command-hook)))
47fda8fc
SM
243 nil t)
244 (add-hook 'post-command-hook
f143bfe3
SM
245 (lambda () (let ((non-essential t)) ;E.g. don't prompt for password!
246 (run-hooks 'icomplete-post-command-hook)))
47fda8fc
SM
247 nil t)
248 (run-hooks 'icomplete-minibuffer-setup-hook)))
1934dbf4
GM
249;\f
250
251
239c87a1
RS
252;;;_* Completion
253
254;;;_ > icomplete-tidy ()
255(defun icomplete-tidy ()
256 "Remove completions display \(if any) prior to new user input.
d183f322 257Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
239c87a1 258and `minibuffer-setup-hook'."
31d4b748 259 (delete-overlay icomplete-overlay))
d183f322 260
239c87a1 261;;;_ > icomplete-exhibit ()
d462ff97 262(defun icomplete-exhibit ()
239c87a1 263 "Insert icomplete completions display.
d183f322 264Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
239c87a1 265and `minibuffer-setup-hook'."
fe689fb6 266 (when (and icomplete-mode (icomplete-simple-completing-p))
47fda8fc
SM
267 (save-excursion
268 (goto-char (point-max))
d462ff97 269 ; Insert the match-status information:
47fda8fc 270 (if (and (> (point-max) (minibuffer-prompt-end))
cc37e70f
J
271 buffer-undo-list ; Wait for some user input.
272 (or
273 ;; Don't bother with delay after certain number of chars:
274 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
275 ;; Don't delay if the completions are known.
276 completion-all-sorted-completions
277 ;; Don't delay if alternatives number is small enough:
278 (and (sequencep minibuffer-completion-table)
279 (< (length minibuffer-completion-table)
280 icomplete-delay-completions-threshold))
281 ;; Delay - give some grace time for next keystroke, before
47fda8fc
SM
282 ;; embarking on computing completions:
283 (sit-for icomplete-compute-delay)))
284 (let ((text (while-no-input
cc37e70f
J
285 (icomplete-completions
286 (field-string)
287 minibuffer-completion-table
288 minibuffer-completion-predicate
31d4b748 289 (not minibuffer-completion-confirm))))
7a78ffec
EZ
290 (buffer-undo-list t)
291 deactivate-mark)
9a84ddd8 292 ;; Do nothing if while-no-input was aborted.
cc37e70f 293 (when (stringp text)
31d4b748
SM
294 (move-overlay icomplete-overlay (point) (point) (current-buffer))
295 ;; The current C cursor code doesn't know to use the overlay's
296 ;; marker's stickiness to figure out whether to place the cursor
297 ;; before or after the string, so let's spoon-feed it the pos.
298 (put-text-property 0 1 'cursor t text)
299 (overlay-put icomplete-overlay 'after-string text)))))))
d183f322 300
239c87a1
RS
301;;;_ > icomplete-completions (name candidates predicate require-match)
302(defun icomplete-completions (name candidates predicate require-match)
d462ff97
RS
303 "Identify prospective candidates for minibuffer completion.
304
c89164c5 305The display is updated with each minibuffer keystroke during
d462ff97
RS
306minibuffer completion.
307
308Prospective completion suffixes (if any) are displayed, bracketed by
309one of \(), \[], or \{} pairs. The choice of brackets is as follows:
310
311 \(...) - a single prospect is identified and matching is enforced,
312 \[...] - a single prospect is identified but matching is optional, or
313 \{...} - multiple prospects, separated by commas, are indicated, and
81276211 314 further input is required to distinguish a single one.
d462ff97 315
81276211 316The displays for unambiguous matches have ` [Matched]' appended
d183f322 317\(whether complete or not), or ` \[No matches]', if no eligible
1be5a284 318matches exist. \(Keybindings for uniquely matched commands
92f7d003
KH
319are exhibited within the square braces.)"
320
f143bfe3 321 (let* ((md (completion--field-metadata (field-beginning)))
acd1f317 322 (comps (completion-all-sorted-completions))
31d4b748
SM
323 (last (if (consp comps) (last comps)))
324 (base-size (cdr last))
476d2aef
SM
325 (open-bracket (if require-match "(" "["))
326 (close-bracket (if require-match ")" "]")))
c2d0b538 327 ;; `concat'/`mapconcat' is the slow part.
31d4b748 328 (if (not (consp comps))
476d2aef 329 (format " %sNo matches%s" open-bracket close-bracket)
31d4b748 330 (if last (setcdr last nil))
be91065f
SM
331 (let* ((most-try
332 (if (and base-size (> base-size 0))
333 (completion-try-completion
620c53a6 334 name candidates predicate (length name) md)
be91065f
SM
335 ;; If the `comps' are 0-based, the result should be
336 ;; the same with `comps'.
337 (completion-try-completion
620c53a6 338 name comps nil (length name) md)))
31d4b748
SM
339 (most (if (consp most-try) (car most-try)
340 (if most-try (car comps) "")))
476d2aef
SM
341 ;; Compare name and most, so we can determine if name is
342 ;; a prefix of most, or something else.
343 (compare (compare-strings name nil nil
344 most nil nil completion-ignore-case))
345 (determ (unless (or (eq t compare) (eq t most-try)
346 (= (setq compare (1- (abs compare)))
347 (length most)))
348 (concat open-bracket
349 (cond
350 ((= compare (length name))
351 ;; Typical case: name is a prefix.
352 (substring most compare))
2387610d
SM
353 ;; Don't bother truncating if it doesn't gain
354 ;; us at least 2 columns.
355 ((< compare 3) most)
356 (t (concat "…" (substring most compare))))
476d2aef 357 close-bracket)))
47fda8fc 358 ;;"-prospects" - more than one candidate
5cbfe5b9
J
359 (prospects-len (+ (string-width
360 (or determ (concat open-bracket close-bracket)))
bdd9367d
WD
361 (string-width icomplete-separator)
362 3 ;; take {…} into account
363 (string-width (buffer-string))))
c2d0b538
SM
364 (prospects-max
365 ;; Max total length to use, including the minibuffer content.
29f313d7 366 (* (+ icomplete-prospects-height
c2d0b538
SM
367 ;; If the minibuffer content already uses up more than
368 ;; one line, increase the allowable space accordingly.
369 (/ prospects-len (window-width)))
370 (window-width)))
5cbfe5b9
J
371 (prefix (when icomplete-hide-common-prefix
372 (try-completion "" comps)))
476d2aef
SM
373 (prefix-len
374 ;; Find the common prefix among `comps'.
8f0fde21
SM
375 ;; We can't use the optimization below because its assumptions
376 ;; aren't always true, e.g. when completion-cycling (bug#10850):
377 ;; (if (eq t (compare-strings (car comps) nil (length most)
378 ;; most nil nil completion-ignore-case))
379 ;; ;; Common case.
380 ;; (length most)
381 ;; Else, use try-completion.
5cbfe5b9
J
382 (and (stringp prefix) (length prefix))) ;;)
383 prospects comp limit)
31d4b748 384 (if (eq most-try t) ;; (or (null (cdr comps))
1934dbf4 385 (setq prospects nil)
5cbfe5b9
J
386 (when (member name comps)
387 ;; NAME is complete but not unique. This scenario poses
388 ;; following UI issues:
389 ;;
390 ;; - When `icomplete-hide-common-prefix' is non-nil, NAME
391 ;; is stripped empty. This would make the entry
392 ;; inconspicuous.
393 ;;
394 ;; - Due to sorting of completions, NAME may not be the
395 ;; first of the prospects and could be hidden deep in
396 ;; the displayed string.
397 ;;
398 ;; - Because of `icomplete-prospects-height' , NAME may
399 ;; not even be displayed to the user.
400 ;;
401 ;; To circumvent all the above problems, provide a visual
402 ;; cue to the user via an "empty string" in the try
403 ;; completion field.
404 (setq determ (concat open-bracket "" close-bracket)))
405 ;; Compute prospects for display.
7d94fb17 406 (while (and comps (not limit))
476d2aef 407 (setq comp
8f0fde21 408 (if prefix-len (substring (car comps) prefix-len) (car comps))
1934dbf4 409 comps (cdr comps))
5cbfe5b9 410 (setq prospects-len
bdd9367d
WD
411 (+ (string-width comp)
412 (string-width icomplete-separator)
413 prospects-len))
c2d0b538 414 (if (< prospects-len prospects-max)
31d4b748 415 (push comp prospects)
5cbfe5b9
J
416 (setq limit t))))
417 (setq prospects (nreverse prospects))
418 ;; Decorate first of the prospects.
419 (when prospects
420 (let ((first (copy-sequence (pop prospects))))
421 (put-text-property 0 (length first)
422 'face 'icomplete-first-match first)
423 (push first prospects)))
31d4b748
SM
424 ;; Restore the base-size info, since completion-all-sorted-completions
425 ;; is cached.
426 (if last (setcdr last base-size))
1934dbf4
GM
427 (if prospects
428 (concat determ
47fda8fc 429 "{"
5cbfe5b9 430 (mapconcat 'identity prospects icomplete-separator)
cc37e70f 431 (and limit (concat icomplete-separator "…"))
47fda8fc 432 "}")
cc37e70f 433 (concat determ " [Matched]"))))))
d462ff97 434
476d2aef
SM
435;;_* Local emacs vars.
436;;Local variables:
437;;allout-layout: (-2 :)
438;;End:
d462ff97
RS
439
440;;; icomplete.el ends here