Auto-commit of generated files.
[emacs.git] / lisp / icomplete.el
blob104e33638313e78804c5b9ce93644b80d0d65f01
1 ;;; icomplete.el --- minibuffer completion incremental feedback
3 ;; Copyright (C) 1992-1994, 1997, 1999, 2001-2013 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 ;;;_* Provide
62 (provide 'icomplete)
65 (defgroup icomplete nil
66 "Show completions dynamically in minibuffer."
67 :prefix "icomplete-"
68 :group 'minibuffer)
70 (defvar icomplete-prospects-length 80)
71 (make-obsolete-variable
72 'icomplete-prospects-length 'icomplete-prospects-height "23.1")
74 (defcustom icomplete-separator " | "
75 "String used by icomplete to separate alternatives in the minibuffer."
76 :type 'string
77 :version "24.4")
79 (defcustom icomplete-hide-common-prefix t
80 "When non-nil, hide common prefix from completion candidates.
81 When nil, show candidates in full."
82 :type 'boolean
83 :version "24.4"
84 :group 'icomplete)
86 (defface icomplete-first-match '((t :weight bold))
87 "Face used by icomplete for highlighting first match."
88 :version "24.4"
89 :group 'icomplete)
91 ;;;_* User Customization variables
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."
97 :type 'integer
98 :group 'icomplete
99 :version "23.1")
101 (defcustom icomplete-compute-delay .3
102 "Completions-computation stall, used only with large-number completions.
103 See `icomplete-delay-completions-threshold'."
104 :type 'number
105 :group 'icomplete)
107 (defcustom icomplete-delay-completions-threshold 400
108 "Pending-completions number over which to apply `icomplete-compute-delay'."
109 :type 'integer
110 :group 'icomplete)
112 (defcustom icomplete-max-delay-chars 3
113 "Maximum number of initial chars to apply icomplete compute delay."
114 :type 'integer
115 :group 'icomplete)
117 (defcustom icomplete-minibuffer-setup-hook nil
118 "Icomplete-specific customization of minibuffer setup.
120 This hook is run during minibuffer setup if icomplete is active.
121 It is intended for use in customizing icomplete for interoperation
122 with other features and packages. For instance:
124 \(add-hook 'icomplete-minibuffer-setup-hook
125 \(function
126 \(lambda ()
127 \(make-local-variable 'max-mini-window-height)
128 \(setq max-mini-window-height 3))))
130 will constrain Emacs to a maximum minibuffer height of 3 lines when
131 icompletion is occurring."
132 :type 'hook
133 :group 'icomplete)
136 ;;;_* Initialization
138 ;;;_ + Internal Variables
139 ;;;_ = icomplete-eoinput nil
140 (defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
141 "Overlay used to display the list of completions.")
143 ;;;_ = icomplete-pre-command-hook
144 (defvar icomplete-pre-command-hook nil
145 "Incremental-minibuffer-completion pre-command-hook.
147 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
148 Use `icomplete-mode' function to set it up properly for incremental
149 minibuffer 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.
155 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
156 Use `icomplete-mode' function to set it up properly for incremental
157 minibuffer completion.")
158 (add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
160 ;;;_ = icomplete-with-completion-tables
161 (defvar icomplete-with-completion-tables '(internal-complete-buffer)
162 "Specialized completion tables with which icomplete should operate.
164 Icomplete does not operate with any specialized completion tables
165 except those on this list.")
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-.] 'icomplete-forward-completions)
172 (define-key map [?\C-,] 'icomplete-backward-completions)
173 map))
175 (defun icomplete-forward-completions ()
176 "Step forward completions by one entry.
177 Second 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)))
182 (when comps
183 (setcdr last (cons (car comps) (cdr last)))
184 (completion--cache-all-sorted-completions (cdr comps)))))
186 (defun icomplete-backward-completions ()
187 "Step backward completions by one entry.
188 Last 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)))
194 (when (consp last) ; At least two elements in comps
195 (setcdr last-but-one (cdr last))
196 (push (car last) comps)
197 (completion--cache-all-sorted-completions comps))))
199 ;;;_ > icomplete-mode (&optional prefix)
200 ;;;###autoload
201 (define-minor-mode icomplete-mode
202 "Toggle incremental minibuffer completion (Icomplete mode).
203 With a prefix argument ARG, enable Icomplete mode if ARG is
204 positive, and disable it otherwise. If called from Lisp, enable
205 the mode if ARG is omitted or nil."
206 :global t :group 'icomplete
207 (if icomplete-mode
208 ;; The following is not really necessary after first time -
209 ;; no great loss.
210 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
211 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
213 ;;;_ > icomplete-simple-completing-p ()
214 (defun icomplete-simple-completing-p ()
215 "Non-nil if current window is minibuffer that's doing simple completion.
217 Conditions are:
218 the selected window is a minibuffer,
219 and not in the middle of macro execution,
220 and `minibuffer-completion-table' is not a symbol (which would
221 indicate some non-standard, non-simple completion mechanism,
222 like file-name and other custom-func completions)."
224 (and (window-minibuffer-p)
225 (not executing-kbd-macro)
226 minibuffer-completion-table
227 (or (not (functionp minibuffer-completion-table))
228 (eq icomplete-with-completion-tables t)
229 (member minibuffer-completion-table
230 icomplete-with-completion-tables))))
232 ;;;_ > icomplete-minibuffer-setup ()
233 (defun icomplete-minibuffer-setup ()
234 "Run in minibuffer on activation to establish incremental completion.
235 Usually run by inclusion in `minibuffer-setup-hook'."
236 (when (and icomplete-mode (icomplete-simple-completing-p))
237 (set (make-local-variable 'completion-show-inline-help) nil)
238 (use-local-map (make-composed-keymap icomplete-minibuffer-map
239 (current-local-map)))
240 (add-hook 'pre-command-hook
241 (lambda () (let ((non-essential t))
242 (run-hooks 'icomplete-pre-command-hook)))
243 nil t)
244 (add-hook 'post-command-hook
245 (lambda () (let ((non-essential t)) ;E.g. don't prompt for password!
246 (run-hooks 'icomplete-post-command-hook)))
247 nil t)
248 (run-hooks 'icomplete-minibuffer-setup-hook)))
252 ;;;_* Completion
254 ;;;_ > icomplete-tidy ()
255 (defun icomplete-tidy ()
256 "Remove completions display \(if any) prior to new user input.
257 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
258 and `minibuffer-setup-hook'."
259 (delete-overlay icomplete-overlay))
261 ;;;_ > icomplete-exhibit ()
262 (defun icomplete-exhibit ()
263 "Insert icomplete completions display.
264 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
265 and `minibuffer-setup-hook'."
266 (when (and icomplete-mode (icomplete-simple-completing-p))
267 (save-excursion
268 (goto-char (point-max))
269 ; Insert the match-status information:
270 (if (and (> (point-max) (minibuffer-prompt-end))
271 buffer-undo-list ; Wait for some user input.
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
282 ;; embarking on computing completions:
283 (sit-for icomplete-compute-delay)))
284 (let ((text (while-no-input
285 (icomplete-completions
286 (field-string)
287 minibuffer-completion-table
288 minibuffer-completion-predicate
289 (not minibuffer-completion-confirm))))
290 (buffer-undo-list t)
291 deactivate-mark)
292 ;; Do nothing if while-no-input was aborted.
293 (when (stringp text)
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)))))))
301 ;;;_ > icomplete-completions (name candidates predicate require-match)
302 (defun icomplete-completions (name candidates predicate require-match)
303 "Identify prospective candidates for minibuffer completion.
305 The display is updated with each minibuffer keystroke during
306 minibuffer completion.
308 Prospective completion suffixes (if any) are displayed, bracketed by
309 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
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
314 further input is required to distinguish a single one.
316 The displays for unambiguous matches have ` [Matched]' appended
317 \(whether complete or not), or ` \[No matches]', if no eligible
318 matches exist. \(Keybindings for uniquely matched commands
319 are exhibited within the square braces.)"
321 (let* ((md (completion--field-metadata (field-beginning)))
322 (comps (completion-all-sorted-completions))
323 (last (if (consp comps) (last comps)))
324 (base-size (cdr last))
325 (open-bracket (if require-match "(" "["))
326 (close-bracket (if require-match ")" "]")))
327 ;; `concat'/`mapconcat' is the slow part.
328 (if (not (consp comps))
329 (format " %sNo matches%s" open-bracket close-bracket)
330 (if last (setcdr last nil))
331 (let* ((most-try
332 (if (and base-size (> base-size 0))
333 (completion-try-completion
334 name candidates predicate (length name) md)
335 ;; If the `comps' are 0-based, the result should be
336 ;; the same with `comps'.
337 (completion-try-completion
338 name comps nil (length name) md)))
339 (most (if (consp most-try) (car most-try)
340 (if most-try (car comps) "")))
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))
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))))
357 close-bracket)))
358 ;;"-prospects" - more than one candidate
359 (prospects-len (+ (string-width
360 (or determ (concat open-bracket close-bracket)))
361 (string-width icomplete-separator)
362 3 ;; take {…} into account
363 (string-width (buffer-string))))
364 (prospects-max
365 ;; Max total length to use, including the minibuffer content.
366 (* (+ icomplete-prospects-height
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)))
371 (prefix (when icomplete-hide-common-prefix
372 (try-completion "" comps)))
373 (prefix-len
374 ;; Find the common prefix among `comps'.
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.
382 (and (stringp prefix) (length prefix))) ;;)
383 prospects comp limit)
384 (if (eq most-try t) ;; (or (null (cdr comps))
385 (setq prospects nil)
386 (when (member name comps)
387 ;; NAME is complete but not unique. This scenario poses
388 ;; following UI issues:
390 ;; - When `icomplete-hide-common-prefix' is non-nil, NAME
391 ;; is stripped empty. This would make the entry
392 ;; inconspicuous.
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.
398 ;; - Because of `icomplete-prospects-height' , NAME may
399 ;; not even be displayed to the user.
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.
406 (while (and comps (not limit))
407 (setq comp
408 (if prefix-len (substring (car comps) prefix-len) (car comps))
409 comps (cdr comps))
410 (setq prospects-len
411 (+ (string-width comp)
412 (string-width icomplete-separator)
413 prospects-len))
414 (if (< prospects-len prospects-max)
415 (push comp prospects)
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)))
424 ;; Restore the base-size info, since completion-all-sorted-completions
425 ;; is cached.
426 (if last (setcdr last base-size))
427 (if prospects
428 (concat determ
430 (mapconcat 'identity prospects icomplete-separator)
431 (and limit (concat icomplete-separator "…"))
432 "}")
433 (concat determ " [Matched]"))))))
435 ;;_* Local emacs vars.
436 ;;Local variables:
437 ;;allout-layout: (-2 :)
438 ;;End:
440 ;;; icomplete.el ends here