(callproc.o, print.o, cmds.o, dispnew.o, fileio.o,
[bpt/emacs.git] / lisp / icomplete.el
CommitLineData
3c14708c 1;;; icomplete.el - minibuffer completion incremental feedback
239c87a1 2
f0009536 3;; Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
d462ff97 4
92f7d003
KH
5;; Author: Ken Manheimer <klm@python.org>
6;; Maintainer: Ken Manheimer <klm@python.org>
be010748
RS
7;; Created: Mar 1993 klm@nist.gov - first release to usenet
8;; Keywords: help, abbrev
d462ff97 9
239c87a1 10;; This file is part of GNU Emacs.
d462ff97 11
239c87a1
RS
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
d462ff97 16
239c87a1
RS
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
c89164c5 21
239c87a1 22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
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
5e537651
SE
41;; To activate icomplete mode, simply add the following to .emacs:
42;; (icomplete-mode)
43;; You can subsequently deactivate it by invoking the function
44;; icomplete-mode with a negative prefix-arg (C-U -1 ESC-x
45;; icomplete-mode). Also, you can prevent activation of the mode
46;; during package load by first setting the variable `icomplete-mode'
47;; to nil. Icompletion can be enabled any time after the package is
48;; loaded by invoking icomplete-mode without a prefix arg.
b578f267 49
92f7d003
KH
50;; This version of icomplete runs on Emacs 19.18 and later. (It
51;; depends on the incorporation of minibuffer-setup-hook.) The elisp
52;; archives, ftp://archive.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive,
53;; probably still has a version that works in GNU Emacs v18.
54
b578f267
EN
55;; Thanks to everyone for their suggestions for refinements of this
56;; package. I particularly have to credit Michael Cook, who
57;; implemented an incremental completion style in his 'iswitch'
58;; functions that served as a model for icomplete. Some other
5e537651 59;; contributors: Noah Friedman (restructuring as minor mode), Colin
1be5a284 60;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
b578f267
EN
61
62;; klm.
c89164c5 63
239c87a1
RS
64;;; Code:
65
66;;;_* Provide
d462ff97
RS
67(provide 'icomplete)
68
5e537651
SE
69
70(defgroup icomplete nil
71 "Show completions dynamically in minibuffer."
72 :prefix "icomplete-"
73 :group 'minibuffer)
74
239c87a1 75;;;_* User Customization variables
5e537651
SE
76(defcustom icomplete-mode nil
77 "*Non-nil enables incremental minibuffer completion.
78As text is typed into the minibuffer, prospective completions are indicated
79in the minibuffer.
80You must modify via \\[customize] for this variable to have an effect."
81 :set (lambda (symbol value)
82 (icomplete-mode (if value 1 -1)))
83 :initialize 'custom-initialize-default
84 :type 'boolean
85 :group 'icomplete
86 :require 'icomplete)
87
88(defcustom icomplete-compute-delay .3
92f7d003 89 "*Completions-computation stall, used only with large-number
5e537651
SE
90completions - see `icomplete-delay-completions-threshold'."
91 :type 'number
92 :group 'icomplete)
93
94(defcustom icomplete-delay-completions-threshold 400
95 "*Pending-completions number over which to apply icomplete-compute-delay."
96 :type 'integer
97 :group 'icomplete)
98
99(defcustom icomplete-max-delay-chars 3
100 "*Maximum number of initial chars to apply icomplete compute delay."
101 :type 'integer
102 :group 'icomplete)
103
104(defcustom icomplete-show-key-bindings t
105 "*If non-nil, show key bindings as well as completion for sole matches."
106 :type 'boolean
107 :group 'icomplete)
108
109(defcustom icomplete-minibuffer-setup-hook nil
239c87a1
RS
110 "*Icomplete-specific customization of minibuffer setup.
111
112This hook is run during minibuffer setup iff icomplete will be active.
113It is intended for use in customizing icomplete for interoperation
114with other packages. For instance:
115
92f7d003 116 \(add-hook 'icomplete-minibuffer-setup-hook
239c87a1
RS
117 \(function
118 \(lambda ()
119 \(make-local-variable 'resize-minibuffer-window-max-height)
120 \(setq resize-minibuffer-window-max-height 3))))
121
122will constrain rsz-mini to a maximum minibuffer height of 3 lines when
5e537651
SE
123icompletion is occurring."
124 :type 'hook
125 :group 'icomplete)
126
127
128;;;_* Initialization
d462ff97 129
c89164c5
RS
130;;;_ + Internal Variables
131;;;_ = icomplete-eoinput 1
d462ff97
RS
132(defvar icomplete-eoinput 1
133 "Point where minibuffer input ends and completion info begins.")
134(make-variable-buffer-local 'icomplete-eoinput)
239c87a1
RS
135;;;_ = icomplete-pre-command-hook
136(defvar icomplete-pre-command-hook nil
137 "Incremental-minibuffer-completion pre-command-hook.
138
139Is run in minibuffer before user input when `icomplete-mode' is non-nil.
140Use `icomplete-mode' function to set it up properly for incremental
141minibuffer completion.")
142(add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
143;;;_ = icomplete-post-command-hook
144(defvar icomplete-post-command-hook nil
145 "Incremental-minibuffer-completion post-command-hook.
146
147Is run in minibuffer after 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-post-command-hook 'icomplete-exhibit)
151
92f7d003 152(defun icomplete-get-keys (func-name)
1be5a284
RS
153 "Return strings naming keys bound to `func-name', or nil if none.
154Examines the prior, not current, buffer, presuming that current buffer
155is minibuffer."
156 (if (commandp func-name)
92f7d003
KH
157 (save-excursion
158 (let* ((sym (intern func-name))
1be5a284
RS
159 (buf (other-buffer))
160 (map (save-excursion (set-buffer buf) (current-local-map)))
161 (keys (where-is-internal sym map)))
92f7d003
KH
162 (if keys
163 (concat "<"
164 (mapconcat 'key-description
165 (sort keys
166 #'(lambda (x y)
167 (< (length x) (length y))))
168 ", ")
169 ">"))))))
170
239c87a1 171;;;_ > icomplete-mode (&optional prefix)
c89164c5 172;;;###autoload
239c87a1 173(defun icomplete-mode (&optional prefix)
1be5a284
RS
174 "Activate incremental minibuffer completion for this Emacs session.
175Deactivates with negative universal argument."
239c87a1
RS
176 (interactive "p")
177 (or prefix (setq prefix 0))
178 (cond ((>= prefix 0)
179 (setq icomplete-mode t)
180 ;; The following is not really necessary after first time -
181 ;; no great loss.
182 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup))
183 (t (setq icomplete-mode nil))))
184
185;;;_ > icomplete-simple-completing-p ()
186(defun icomplete-simple-completing-p ()
239c87a1
RS
187 "Non-nil if current window is minibuffer that's doing simple completion.
188
189Conditions are:
190 the selected window is a minibuffer,
191 and not in the middle of macro execution,
192 and minibuffer-completion-table is not a symbol (which would
81276211 193 indicate some non-standard, non-simple completion mechanism,
239c87a1
RS
194 like file-name and other custom-func completions)."
195
196 (and (window-minibuffer-p (selected-window))
efcf38c7 197 (not executing-kbd-macro)
239c87a1 198 (not (symbolp minibuffer-completion-table))))
d183f322 199
239c87a1
RS
200;;;_ > icomplete-minibuffer-setup ()
201;;;###autoload
202(defun icomplete-minibuffer-setup ()
239c87a1 203 "Run in minibuffer on activation to establish incremental completion.
d183f322 204Usually run by inclusion in `minibuffer-setup-hook'."
239c87a1 205 (cond ((and icomplete-mode (icomplete-simple-completing-p))
d183f322 206 (make-local-hook 'pre-command-hook)
239c87a1
RS
207 (add-hook 'pre-command-hook
208 (function (lambda ()
d183f322
RS
209 (run-hooks 'icomplete-pre-command-hook)))
210 nil t)
211 (make-local-hook 'post-command-hook)
239c87a1
RS
212 (add-hook 'post-command-hook
213 (function (lambda ()
d183f322
RS
214 (run-hooks 'icomplete-post-command-hook)))
215 nil t)
239c87a1 216 (run-hooks 'icomplete-minibuffer-setup-hook))))
d183f322 217\f
239c87a1
RS
218;;;_* Completion
219
220;;;_ > icomplete-tidy ()
221(defun icomplete-tidy ()
222 "Remove completions display \(if any) prior to new user input.
d183f322 223Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
239c87a1
RS
224and `minibuffer-setup-hook'."
225 (if (icomplete-simple-completing-p)
d462ff97
RS
226 (if (and (boundp 'icomplete-eoinput)
227 icomplete-eoinput)
239c87a1 228
d462ff97
RS
229 (if (> icomplete-eoinput (point-max))
230 ;; Oops, got rug pulled out from under us - reinit:
231 (setq icomplete-eoinput (point-max))
c89164c5 232 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
d462ff97 233 (delete-region icomplete-eoinput (point-max))))
239c87a1 234
c89164c5 235 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
d462ff97
RS
236 (make-local-variable 'icomplete-eoinput)
237 (setq icomplete-eoinput 1))))
d183f322 238
239c87a1 239;;;_ > icomplete-exhibit ()
d462ff97 240(defun icomplete-exhibit ()
239c87a1 241 "Insert icomplete completions display.
d183f322 242Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
239c87a1
RS
243and `minibuffer-setup-hook'."
244 (if (icomplete-simple-completing-p)
d462ff97
RS
245 (let ((contents (buffer-substring (point-min)(point-max)))
246 (buffer-undo-list t))
247 (save-excursion
248 (goto-char (point-max))
249 ; Register the end of input, so we
250 ; know where the extra stuff
251 ; (match-status info) begins:
252 (if (not (boundp 'icomplete-eoinput))
253 ;; In case it got wiped out by major mode business:
254 (make-local-variable 'icomplete-eoinput))
255 (setq icomplete-eoinput (point))
256 ; Insert the match-status information:
92f7d003
KH
257 (if (and (> (point-max) 1)
258 (or
259 ;; Don't bother with delay after certain number of chars:
260 (> (point-max) icomplete-max-delay-chars)
261 ;; Don't delay if alternatives number is small enough:
262 (if minibuffer-completion-table
263 (cond ((numberp minibuffer-completion-table)
264 (< minibuffer-completion-table
265 icomplete-delay-completions-threshold))
266 ((sequencep minibuffer-completion-table)
267 (< (length minibuffer-completion-table)
268 icomplete-delay-completions-threshold))
269 ))
270 ;; Delay - give some grace time for next keystroke, before
271 ;; embarking on computing completions:
272 (sit-for icomplete-compute-delay)))
d462ff97 273 (insert-string
239c87a1
RS
274 (icomplete-completions contents
275 minibuffer-completion-table
276 minibuffer-completion-predicate
277 (not
278 minibuffer-completion-confirm))))))))
d183f322 279
239c87a1
RS
280;;;_ > icomplete-completions (name candidates predicate require-match)
281(defun icomplete-completions (name candidates predicate require-match)
d462ff97
RS
282 "Identify prospective candidates for minibuffer completion.
283
c89164c5 284The display is updated with each minibuffer keystroke during
d462ff97
RS
285minibuffer completion.
286
287Prospective completion suffixes (if any) are displayed, bracketed by
288one of \(), \[], or \{} pairs. The choice of brackets is as follows:
289
290 \(...) - a single prospect is identified and matching is enforced,
291 \[...] - a single prospect is identified but matching is optional, or
292 \{...} - multiple prospects, separated by commas, are indicated, and
81276211 293 further input is required to distinguish a single one.
d462ff97 294
81276211 295The displays for unambiguous matches have ` [Matched]' appended
d183f322 296\(whether complete or not), or ` \[No matches]', if no eligible
1be5a284 297matches exist. \(Keybindings for uniquely matched commands
92f7d003
KH
298are exhibited within the square braces.)"
299
300 ;; 'all-completions' doesn't like empty
301 ;; minibuffer-completion-table's (ie: (nil))
302 (if (and (listp candidates) (null (car candidates)))
303 (setq candidates nil))
d462ff97
RS
304
305 (let ((comps (all-completions name candidates predicate))
306 ; "-determined" - only one candidate
307 (open-bracket-determined (if require-match "(" "["))
308 (close-bracket-determined (if require-match ")" "]"))
309 ;"-prospects" - more than one candidate
310 (open-bracket-prospects "{")
311 (close-bracket-prospects "}")
312 )
92f7d003
KH
313 (catch 'input
314 (cond ((null comps) (format " %sNo matches%s"
315 open-bracket-determined
316 close-bracket-determined))
317 ((null (cdr comps)) ;one match
318 (concat (if (and (> (length (car comps))
319 (length name)))
320 (concat open-bracket-determined
321 (substring (car comps) (length name))
322 close-bracket-determined)
323 "")
324 " [Matched"
325 (let ((keys (and icomplete-show-key-bindings
326 (commandp (intern-soft (car comps)))
327 (icomplete-get-keys (car comps)))))
328 (if keys
329 (concat "; " keys)
330 ""))
331 "]"))
332 (t ;multiple matches
333 (let* ((most
334 (try-completion name candidates
335 (and predicate
336 ;; Wrap predicate in impatience - ie,
337 ;; `throw' up when pending input is
338 ;; noticed. Adds some overhead to
339 ;; predicate, but should be worth it.
340 (function
341 (lambda (item)
342 (if (input-pending-p)
343 (throw 'input "")
344 (apply predicate
345 item nil)))))))
346 (most-len (length most))
347 most-is-exact
348 (alternatives
349 (substring
350 (apply (function concat)
351 (mapcar (function
352 (lambda (com)
353 (if (input-pending-p)
354 (throw 'input ""))
355 (if (= (length com) most-len)
356 ;; Most is one exact match,
357 ;; note that and leave out
358 ;; for later indication:
359 (progn
360 (setq most-is-exact t)
361 ())
362 (concat ","
363 (substring com
364 most-len)))))
365 comps))
366 1)))
367 (concat (and (> most-len (length name))
368 (concat open-bracket-determined
369 (substring most (length name))
370 close-bracket-determined))
371 open-bracket-prospects
372 (if most-is-exact
373 ;; Add a ',' at the front to indicate "complete but
374 ;; not unique":
375 (concat "," alternatives)
376 alternatives)
377 close-bracket-prospects)))))))
d462ff97 378
5e537651
SE
379(if icomplete-mode
380 (icomplete-mode 1))
381
239c87a1
RS
382;;;_* Local emacs vars.
383;;;Local variables:
384;;;outline-layout: (-2 :)
385;;;End:
d462ff97
RS
386
387;;; icomplete.el ends here