term/internal.el (IT-display-table-setup): Don't overstep
[bpt/emacs.git] / lisp / icomplete.el
CommitLineData
7922d62e 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 76(defcustom icomplete-mode nil
b1c501a9 77 "Toggle incremental minibuffer completion.
5e537651
SE
78As text is typed into the minibuffer, prospective completions are indicated
79in the minibuffer.
73a3ea5a
DL
80Setting this variable directly does not take effect;
81use either \\[customize] or the function `icomplete-mode'."
5e537651
SE
82 :set (lambda (symbol value)
83 (icomplete-mode (if value 1 -1)))
84 :initialize 'custom-initialize-default
85 :type 'boolean
86 :group 'icomplete
87 :require 'icomplete)
88
89(defcustom icomplete-compute-delay .3
92f7d003 90 "*Completions-computation stall, used only with large-number
5e537651
SE
91completions - see `icomplete-delay-completions-threshold'."
92 :type 'number
93 :group 'icomplete)
94
95(defcustom icomplete-delay-completions-threshold 400
96 "*Pending-completions number over which to apply icomplete-compute-delay."
97 :type 'integer
98 :group 'icomplete)
99
100(defcustom icomplete-max-delay-chars 3
101 "*Maximum number of initial chars to apply icomplete compute delay."
102 :type 'integer
103 :group 'icomplete)
104
105(defcustom icomplete-show-key-bindings t
106 "*If non-nil, show key bindings as well as completion for sole matches."
107 :type 'boolean
108 :group 'icomplete)
109
110(defcustom icomplete-minibuffer-setup-hook nil
239c87a1
RS
111 "*Icomplete-specific customization of minibuffer setup.
112
113This hook is run during minibuffer setup iff icomplete will be active.
114It is intended for use in customizing icomplete for interoperation
115with other packages. For instance:
116
92f7d003 117 \(add-hook 'icomplete-minibuffer-setup-hook
239c87a1
RS
118 \(function
119 \(lambda ()
120 \(make-local-variable 'resize-minibuffer-window-max-height)
121 \(setq resize-minibuffer-window-max-height 3))))
122
123will constrain rsz-mini to a maximum minibuffer height of 3 lines when
5e537651
SE
124icompletion is occurring."
125 :type 'hook
126 :group 'icomplete)
127
128
129;;;_* Initialization
d462ff97 130
c89164c5
RS
131;;;_ + Internal Variables
132;;;_ = icomplete-eoinput 1
d462ff97
RS
133(defvar icomplete-eoinput 1
134 "Point where minibuffer input ends and completion info begins.")
135(make-variable-buffer-local 'icomplete-eoinput)
239c87a1
RS
136;;;_ = icomplete-pre-command-hook
137(defvar icomplete-pre-command-hook nil
138 "Incremental-minibuffer-completion pre-command-hook.
139
140Is run in minibuffer before user input when `icomplete-mode' is non-nil.
141Use `icomplete-mode' function to set it up properly for incremental
142minibuffer completion.")
143(add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
144;;;_ = icomplete-post-command-hook
145(defvar icomplete-post-command-hook nil
146 "Incremental-minibuffer-completion post-command-hook.
147
148Is run in minibuffer after user input when `icomplete-mode' is non-nil.
149Use `icomplete-mode' function to set it up properly for incremental
150minibuffer completion.")
151(add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
152
92f7d003 153(defun icomplete-get-keys (func-name)
1be5a284
RS
154 "Return strings naming keys bound to `func-name', or nil if none.
155Examines the prior, not current, buffer, presuming that current buffer
156is minibuffer."
157 (if (commandp func-name)
92f7d003
KH
158 (save-excursion
159 (let* ((sym (intern func-name))
1be5a284
RS
160 (buf (other-buffer))
161 (map (save-excursion (set-buffer buf) (current-local-map)))
162 (keys (where-is-internal sym map)))
92f7d003
KH
163 (if keys
164 (concat "<"
165 (mapconcat 'key-description
166 (sort keys
167 #'(lambda (x y)
168 (< (length x) (length y))))
169 ", ")
170 ">"))))))
171
239c87a1 172;;;_ > icomplete-mode (&optional prefix)
c89164c5 173;;;###autoload
239c87a1 174(defun icomplete-mode (&optional prefix)
1be5a284
RS
175 "Activate incremental minibuffer completion for this Emacs session.
176Deactivates with negative universal argument."
239c87a1
RS
177 (interactive "p")
178 (or prefix (setq prefix 0))
179 (cond ((>= prefix 0)
180 (setq icomplete-mode t)
181 ;; The following is not really necessary after first time -
182 ;; no great loss.
183 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup))
184 (t (setq icomplete-mode nil))))
185
186;;;_ > icomplete-simple-completing-p ()
187(defun icomplete-simple-completing-p ()
239c87a1
RS
188 "Non-nil if current window is minibuffer that's doing simple completion.
189
190Conditions are:
191 the selected window is a minibuffer,
192 and not in the middle of macro execution,
193 and minibuffer-completion-table is not a symbol (which would
81276211 194 indicate some non-standard, non-simple completion mechanism,
239c87a1
RS
195 like file-name and other custom-func completions)."
196
197 (and (window-minibuffer-p (selected-window))
efcf38c7 198 (not executing-kbd-macro)
239c87a1 199 (not (symbolp minibuffer-completion-table))))
d183f322 200
239c87a1
RS
201;;;_ > icomplete-minibuffer-setup ()
202;;;###autoload
203(defun icomplete-minibuffer-setup ()
239c87a1 204 "Run in minibuffer on activation to establish incremental completion.
d183f322 205Usually run by inclusion in `minibuffer-setup-hook'."
239c87a1 206 (cond ((and icomplete-mode (icomplete-simple-completing-p))
d183f322 207 (make-local-hook 'pre-command-hook)
239c87a1
RS
208 (add-hook 'pre-command-hook
209 (function (lambda ()
d183f322
RS
210 (run-hooks 'icomplete-pre-command-hook)))
211 nil t)
212 (make-local-hook 'post-command-hook)
239c87a1
RS
213 (add-hook 'post-command-hook
214 (function (lambda ()
d183f322
RS
215 (run-hooks 'icomplete-post-command-hook)))
216 nil t)
239c87a1 217 (run-hooks 'icomplete-minibuffer-setup-hook))))
d183f322 218\f
239c87a1
RS
219;;;_* Completion
220
221;;;_ > icomplete-tidy ()
222(defun icomplete-tidy ()
223 "Remove completions display \(if any) prior to new user input.
d183f322 224Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
239c87a1
RS
225and `minibuffer-setup-hook'."
226 (if (icomplete-simple-completing-p)
d462ff97
RS
227 (if (and (boundp 'icomplete-eoinput)
228 icomplete-eoinput)
239c87a1 229
d462ff97
RS
230 (if (> icomplete-eoinput (point-max))
231 ;; Oops, got rug pulled out from under us - reinit:
232 (setq icomplete-eoinput (point-max))
c89164c5 233 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
d462ff97 234 (delete-region icomplete-eoinput (point-max))))
239c87a1 235
c89164c5 236 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
d462ff97
RS
237 (make-local-variable 'icomplete-eoinput)
238 (setq icomplete-eoinput 1))))
d183f322 239
239c87a1 240;;;_ > icomplete-exhibit ()
d462ff97 241(defun icomplete-exhibit ()
239c87a1 242 "Insert icomplete completions display.
d183f322 243Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
239c87a1
RS
244and `minibuffer-setup-hook'."
245 (if (icomplete-simple-completing-p)
d462ff97
RS
246 (let ((contents (buffer-substring (point-min)(point-max)))
247 (buffer-undo-list t))
248 (save-excursion
249 (goto-char (point-max))
250 ; Register the end of input, so we
251 ; know where the extra stuff
252 ; (match-status info) begins:
253 (if (not (boundp 'icomplete-eoinput))
254 ;; In case it got wiped out by major mode business:
255 (make-local-variable 'icomplete-eoinput))
256 (setq icomplete-eoinput (point))
257 ; Insert the match-status information:
92f7d003
KH
258 (if (and (> (point-max) 1)
259 (or
260 ;; Don't bother with delay after certain number of chars:
261 (> (point-max) icomplete-max-delay-chars)
262 ;; Don't delay if alternatives number is small enough:
263 (if minibuffer-completion-table
264 (cond ((numberp minibuffer-completion-table)
265 (< minibuffer-completion-table
266 icomplete-delay-completions-threshold))
267 ((sequencep minibuffer-completion-table)
268 (< (length minibuffer-completion-table)
269 icomplete-delay-completions-threshold))
270 ))
271 ;; Delay - give some grace time for next keystroke, before
272 ;; embarking on computing completions:
273 (sit-for icomplete-compute-delay)))
d462ff97 274 (insert-string
239c87a1
RS
275 (icomplete-completions contents
276 minibuffer-completion-table
277 minibuffer-completion-predicate
278 (not
279 minibuffer-completion-confirm))))))))
d183f322 280
239c87a1
RS
281;;;_ > icomplete-completions (name candidates predicate require-match)
282(defun icomplete-completions (name candidates predicate require-match)
d462ff97
RS
283 "Identify prospective candidates for minibuffer completion.
284
c89164c5 285The display is updated with each minibuffer keystroke during
d462ff97
RS
286minibuffer completion.
287
288Prospective completion suffixes (if any) are displayed, bracketed by
289one of \(), \[], or \{} pairs. The choice of brackets is as follows:
290
291 \(...) - a single prospect is identified and matching is enforced,
292 \[...] - a single prospect is identified but matching is optional, or
293 \{...} - multiple prospects, separated by commas, are indicated, and
81276211 294 further input is required to distinguish a single one.
d462ff97 295
81276211 296The displays for unambiguous matches have ` [Matched]' appended
d183f322 297\(whether complete or not), or ` \[No matches]', if no eligible
1be5a284 298matches exist. \(Keybindings for uniquely matched commands
92f7d003
KH
299are exhibited within the square braces.)"
300
301 ;; 'all-completions' doesn't like empty
302 ;; minibuffer-completion-table's (ie: (nil))
303 (if (and (listp candidates) (null (car candidates)))
304 (setq candidates nil))
d462ff97
RS
305
306 (let ((comps (all-completions name candidates predicate))
307 ; "-determined" - only one candidate
308 (open-bracket-determined (if require-match "(" "["))
309 (close-bracket-determined (if require-match ")" "]"))
310 ;"-prospects" - more than one candidate
311 (open-bracket-prospects "{")
312 (close-bracket-prospects "}")
313 )
92f7d003
KH
314 (catch 'input
315 (cond ((null comps) (format " %sNo matches%s"
316 open-bracket-determined
317 close-bracket-determined))
318 ((null (cdr comps)) ;one match
319 (concat (if (and (> (length (car comps))
320 (length name)))
321 (concat open-bracket-determined
322 (substring (car comps) (length name))
323 close-bracket-determined)
324 "")
325 " [Matched"
326 (let ((keys (and icomplete-show-key-bindings
327 (commandp (intern-soft (car comps)))
328 (icomplete-get-keys (car comps)))))
329 (if keys
330 (concat "; " keys)
331 ""))
332 "]"))
333 (t ;multiple matches
334 (let* ((most
335 (try-completion name candidates
336 (and predicate
337 ;; Wrap predicate in impatience - ie,
338 ;; `throw' up when pending input is
339 ;; noticed. Adds some overhead to
340 ;; predicate, but should be worth it.
341 (function
342 (lambda (item)
343 (if (input-pending-p)
344 (throw 'input "")
345 (apply predicate
346 item nil)))))))
347 (most-len (length most))
348 most-is-exact
349 (alternatives
350 (substring
351 (apply (function concat)
352 (mapcar (function
353 (lambda (com)
354 (if (input-pending-p)
355 (throw 'input ""))
356 (if (= (length com) most-len)
357 ;; Most is one exact match,
358 ;; note that and leave out
359 ;; for later indication:
360 (progn
361 (setq most-is-exact t)
362 ())
363 (concat ","
364 (substring com
365 most-len)))))
366 comps))
367 1)))
368 (concat (and (> most-len (length name))
369 (concat open-bracket-determined
370 (substring most (length name))
371 close-bracket-determined))
372 open-bracket-prospects
373 (if most-is-exact
374 ;; Add a ',' at the front to indicate "complete but
375 ;; not unique":
376 (concat "," alternatives)
377 alternatives)
378 close-bracket-prospects)))))))
d462ff97 379
5e537651
SE
380(if icomplete-mode
381 (icomplete-mode 1))
382
239c87a1
RS
383;;;_* Local emacs vars.
384;;;Local variables:
385;;;outline-layout: (-2 :)
386;;;End:
d462ff97
RS
387
388;;; icomplete.el ends here