(terminal-init-xterm): Add more key bindings.
[emacs.git] / lisp / icomplete.el
blob6687c13275b11792005539619154cb43c3a164ae
1 ;;; icomplete.el --- minibuffer completion incremental feedback
3 ;; Copyright (C) 1992, 1993, 1994, 1997, 1999, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software 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 2, or (at your option)
17 ;; 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; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; Loading this package implements a more fine-grained minibuffer
32 ;; completion feedback scheme. Prospective completions are concisely
33 ;; indicated within the minibuffer itself, with each successive
34 ;; keystroke.
36 ;; See `icomplete-completions' docstring for a description of the
37 ;; icomplete display format.
39 ;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
40 ;; customize icomplete setup for interoperation with other
41 ;; minibuffer-oriented packages.
43 ;; To activate icomplete mode, load the package and use the
44 ;; `icomplete-mode' function. You can subsequently deactivate it by
45 ;; invoking the function icomplete-mode with a negative prefix-arg
46 ;; (C-U -1 ESC-x icomplete-mode). Also, you can prevent activation of
47 ;; the mode during package load by first setting the variable
48 ;; `icomplete-mode' to nil. Icompletion can be enabled any time after
49 ;; the package is loaded by invoking icomplete-mode without a prefix
50 ;; arg.
52 ;; Thanks to everyone for their suggestions for refinements of this
53 ;; package. I particularly have to credit Michael Cook, who
54 ;; implemented an incremental completion style in his 'iswitch'
55 ;; functions that served as a model for icomplete. Some other
56 ;; contributors: Noah Friedman (restructuring as minor mode), Colin
57 ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
59 ;; klm.
61 ;;; Code:
63 ;;;_* Provide
64 (provide 'icomplete)
67 (defgroup icomplete nil
68 "Show completions dynamically in minibuffer."
69 :prefix "icomplete-"
70 :group 'minibuffer)
72 ;;;_* User Customization variables
73 (defcustom icomplete-prospects-length 80
74 "*Length of string displaying the prospects."
75 :type 'integer
76 :group 'icomplete)
78 (defcustom icomplete-compute-delay .3
79 "*Completions-computation stall, used only with large-number
80 completions - see `icomplete-delay-completions-threshold'."
81 :type 'number
82 :group 'icomplete)
84 (defcustom icomplete-delay-completions-threshold 400
85 "*Pending-completions number over which to apply icomplete-compute-delay."
86 :type 'integer
87 :group 'icomplete)
89 (defcustom icomplete-max-delay-chars 3
90 "*Maximum number of initial chars to apply icomplete compute delay."
91 :type 'integer
92 :group 'icomplete)
94 (defcustom icomplete-show-key-bindings t
95 "*If non-nil, show key bindings as well as completion for sole matches."
96 :type 'boolean
97 :group 'icomplete)
99 (defcustom icomplete-minibuffer-setup-hook nil
100 "*Icomplete-specific customization of minibuffer setup.
102 This hook is run during minibuffer setup iff icomplete will be active.
103 It is intended for use in customizing icomplete for interoperation
104 with other features and packages. For instance:
106 \(add-hook 'icomplete-minibuffer-setup-hook
107 \(function
108 \(lambda ()
109 \(make-local-variable 'max-mini-window-height)
110 \(setq max-mini-window-height 3))))
112 will constrain Emacs to a maximum minibuffer height of 3 lines when
113 icompletion is occurring."
114 :type 'hook
115 :group 'icomplete)
118 ;;;_* Initialization
120 ;;;_ + Internal Variables
121 ;;;_ = icomplete-eoinput nil
122 (defvar icomplete-eoinput nil
123 "Point where minibuffer input ends and completion info begins.")
124 (make-variable-buffer-local 'icomplete-eoinput)
125 ;;;_ = icomplete-pre-command-hook
126 (defvar icomplete-pre-command-hook nil
127 "Incremental-minibuffer-completion pre-command-hook.
129 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
130 Use `icomplete-mode' function to set it up properly for incremental
131 minibuffer completion.")
132 (add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
133 ;;;_ = icomplete-post-command-hook
134 (defvar icomplete-post-command-hook nil
135 "Incremental-minibuffer-completion post-command-hook.
137 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
138 Use `icomplete-mode' function to set it up properly for incremental
139 minibuffer completion.")
140 (add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
142 (defun icomplete-get-keys (func-name)
143 "Return strings naming keys bound to `func-name', or nil if none.
144 Examines the prior, not current, buffer, presuming that current buffer
145 is minibuffer."
146 (if (commandp func-name)
147 (save-excursion
148 (let* ((sym (intern func-name))
149 (buf (other-buffer nil t))
150 (map (save-excursion (set-buffer buf) (current-local-map)))
151 (keys (where-is-internal sym map)))
152 (if keys
153 (concat "<"
154 (mapconcat 'key-description
155 (sort keys
156 #'(lambda (x y)
157 (< (length x) (length y))))
158 ", ")
159 ">"))))))
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 ;;;_ > icomplete-mode (&optional prefix)
168 ;;;###autoload
169 (define-minor-mode icomplete-mode
170 "Toggle incremental minibuffer completion for this Emacs session.
171 With a numeric argument, turn Icomplete mode on iff ARG is positive."
172 :global t :group 'icomplete
173 (if icomplete-mode
174 ;; The following is not really necessary after first time -
175 ;; no great loss.
176 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
177 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
179 ;;;_ > icomplete-simple-completing-p ()
180 (defun icomplete-simple-completing-p ()
181 "Non-nil if current window is minibuffer that's doing simple completion.
183 Conditions are:
184 the selected window is a minibuffer,
185 and not in the middle of macro execution,
186 and `minibuffer-completion-table' is not a symbol (which would
187 indicate some non-standard, non-simple completion mechanism,
188 like file-name and other custom-func completions)."
190 (and (window-minibuffer-p (selected-window))
191 (not executing-kbd-macro)
192 minibuffer-completion-table
193 (or (not (functionp minibuffer-completion-table))
194 (member minibuffer-completion-table
195 icomplete-with-completion-tables))))
197 ;;;_ > icomplete-minibuffer-setup ()
198 (defun icomplete-minibuffer-setup ()
199 "Run in minibuffer on activation to establish incremental completion.
200 Usually run by inclusion in `minibuffer-setup-hook'."
201 (when (and icomplete-mode (icomplete-simple-completing-p))
202 (add-hook 'pre-command-hook
203 (lambda () (run-hooks 'icomplete-pre-command-hook))
204 nil t)
205 (add-hook 'post-command-hook
206 (lambda () (run-hooks 'icomplete-post-command-hook))
207 nil t)
208 (run-hooks 'icomplete-minibuffer-setup-hook)))
212 ;;;_* Completion
214 ;;;_ > icomplete-tidy ()
215 (defun icomplete-tidy ()
216 "Remove completions display \(if any) prior to new user input.
217 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
218 and `minibuffer-setup-hook'."
219 (when icomplete-eoinput
221 (unless (>= icomplete-eoinput (point-max))
222 (let ((buffer-undo-list t)) ; prevent entry
223 (delete-region icomplete-eoinput (point-max))))
225 ;; Reestablish the safe value.
226 (setq icomplete-eoinput nil)))
228 ;;;_ > icomplete-exhibit ()
229 (defun icomplete-exhibit ()
230 "Insert icomplete completions display.
231 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
232 and `minibuffer-setup-hook'."
233 (when (icomplete-simple-completing-p)
234 (save-excursion
235 (goto-char (point-max))
236 ;; Register the end of input, so we know where the extra stuff
237 ;; (match-status info) begins:
238 (setq icomplete-eoinput (point))
239 ; Insert the match-status information:
240 (if (and (> (point-max) (minibuffer-prompt-end))
241 buffer-undo-list ; Wait for some user input.
243 ;; Don't bother with delay after certain number of chars:
244 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
245 ;; Don't delay if alternatives number is small enough:
246 (and (sequencep minibuffer-completion-table)
247 (< (length minibuffer-completion-table)
248 icomplete-delay-completions-threshold))
249 ;; Delay - give some grace time for next keystroke, before
250 ;; embarking on computing completions:
251 (sit-for icomplete-compute-delay)))
252 (let ((text (while-no-input
253 (list
254 (icomplete-completions
255 (field-string)
256 minibuffer-completion-table
257 minibuffer-completion-predicate
258 (not minibuffer-completion-confirm)))))
259 (buffer-undo-list t))
260 ;; Do nothing if while-no-input was aborted.
261 (if (consp text) (insert (car text))))))))
263 ;;;_ > icomplete-completions (name candidates predicate require-match)
264 (defun icomplete-completions (name candidates predicate require-match)
265 "Identify prospective candidates for minibuffer completion.
267 The display is updated with each minibuffer keystroke during
268 minibuffer completion.
270 Prospective completion suffixes (if any) are displayed, bracketed by
271 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
273 \(...) - a single prospect is identified and matching is enforced,
274 \[...] - a single prospect is identified but matching is optional, or
275 \{...} - multiple prospects, separated by commas, are indicated, and
276 further input is required to distinguish a single one.
278 The displays for unambiguous matches have ` [Matched]' appended
279 \(whether complete or not), or ` \[No matches]', if no eligible
280 matches exist. \(Keybindings for uniquely matched commands
281 are exhibited within the square braces.)"
283 ;; 'all-completions' doesn't like empty
284 ;; minibuffer-completion-table's (ie: (nil))
285 (if (and (listp candidates) (null (car candidates)))
286 (setq candidates nil))
288 (let ((comps (all-completions name candidates predicate))
289 ; "-determined" - only one candidate
290 (open-bracket-determined (if require-match "(" "["))
291 (close-bracket-determined (if require-match ")" "]")))
292 ;; `concat'/`mapconcat' is the slow part. With the introduction of
293 ;; `icomplete-prospects-length', there is no need for `catch'/`throw'.
294 (if (null comps) (format " %sNo matches%s"
295 open-bracket-determined
296 close-bracket-determined)
297 (let* ((most-try (try-completion name (mapcar (function list) comps)))
298 (most (if (stringp most-try) most-try (car comps)))
299 (most-len (length most))
300 (determ (and (> most-len (length name))
301 (concat open-bracket-determined
302 (substring most (length name))
303 close-bracket-determined)))
304 ;;"-prospects" - more than one candidate
305 (prospects-len 0)
306 prospects most-is-exact comp)
307 (if (eq most-try t)
308 (setq prospects nil)
309 (while (and comps (< prospects-len icomplete-prospects-length))
310 (setq comp (substring (car comps) most-len)
311 comps (cdr comps))
312 (cond ((string-equal comp "") (setq most-is-exact t))
313 ((member comp prospects))
314 (t (setq prospects (cons comp prospects)
315 prospects-len (+ (length comp) 1 prospects-len))))))
316 (if prospects
317 (concat determ
319 (and most-is-exact ",")
320 (mapconcat 'identity
321 (sort prospects (function string-lessp))
322 ",")
323 (and comps ",...")
324 "}")
325 (concat determ
326 " [Matched"
327 (let ((keys (and icomplete-show-key-bindings
328 (commandp (intern-soft most))
329 (icomplete-get-keys most))))
330 (if keys (concat "; " keys) ""))
331 "]"))))))
333 ;;;_* Local emacs vars.
334 ;;;Local variables:
335 ;;;allout-layout: (-2 :)
336 ;;;End:
338 ;; arch-tag: 339ec25a-0741-4eb6-be63-997532e89b0f
339 ;;; icomplete.el ends here