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