]> code.delx.au - gnu-emacs/blob - lisp/icomplete.el
#
[gnu-emacs] / lisp / icomplete.el
1 ;;; icomplete.el --- minibuffer completion incremental feedback
2
3 ;; Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Ken Manheimer <klm@python.org>
6 ;; Maintainer: Ken Manheimer <klm@python.org>
7 ;; Created: Mar 1993 klm@nist.gov - first release to usenet
8 ;; Keywords: help, abbrev
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
22 ;; You should have received a copy of the GNU General Public License
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.
26
27 ;;; Commentary:
28
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
34 ;; See `icomplete-completions' docstring for a description of the
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
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.
49
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
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
59 ;; contributors: Noah Friedman (restructuring as minor mode), Colin
60 ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
61
62 ;; klm.
63
64 ;;; Code:
65
66 ;;;_* Provide
67 (provide 'icomplete)
68
69
70 (defgroup icomplete nil
71 "Show completions dynamically in minibuffer."
72 :prefix "icomplete-"
73 :group 'minibuffer)
74
75 ;;;_* User Customization variables
76 (defcustom icomplete-mode nil
77 "Toggle incremental minibuffer completion.
78 As text is typed into the minibuffer, prospective completions are indicated
79 in the minibuffer.
80 Setting this variable directly does not take effect;
81 use either \\[customize] or the function `icomplete-mode'."
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
90 "*Completions-computation stall, used only with large-number
91 completions - 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
111 "*Icomplete-specific customization of minibuffer setup.
112
113 This hook is run during minibuffer setup iff icomplete will be active.
114 It is intended for use in customizing icomplete for interoperation
115 with other packages. For instance:
116
117 \(add-hook 'icomplete-minibuffer-setup-hook
118 \(function
119 \(lambda ()
120 \(make-local-variable 'resize-minibuffer-window-max-height)
121 \(setq resize-minibuffer-window-max-height 3))))
122
123 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
124 icompletion is occurring."
125 :type 'hook
126 :group 'icomplete)
127
128
129 ;;;_* Initialization
130
131 ;;;_ + Internal Variables
132 ;;;_ = icomplete-eoinput 1
133 (defvar icomplete-eoinput 1
134 "Point where minibuffer input ends and completion info begins.")
135 (make-variable-buffer-local 'icomplete-eoinput)
136 ;;;_ = icomplete-pre-command-hook
137 (defvar icomplete-pre-command-hook nil
138 "Incremental-minibuffer-completion pre-command-hook.
139
140 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
141 Use `icomplete-mode' function to set it up properly for incremental
142 minibuffer 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
148 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
149 Use `icomplete-mode' function to set it up properly for incremental
150 minibuffer completion.")
151 (add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
152
153 (defun icomplete-get-keys (func-name)
154 "Return strings naming keys bound to `func-name', or nil if none.
155 Examines the prior, not current, buffer, presuming that current buffer
156 is minibuffer."
157 (if (commandp func-name)
158 (save-excursion
159 (let* ((sym (intern func-name))
160 (buf (other-buffer))
161 (map (save-excursion (set-buffer buf) (current-local-map)))
162 (keys (where-is-internal sym map)))
163 (if keys
164 (concat "<"
165 (mapconcat 'key-description
166 (sort keys
167 #'(lambda (x y)
168 (< (length x) (length y))))
169 ", ")
170 ">"))))))
171
172 ;;;_ > icomplete-mode (&optional prefix)
173 ;;;###autoload
174 (defun icomplete-mode (&optional prefix)
175 "Activate incremental minibuffer completion for this Emacs session.
176 Deactivates with negative universal argument."
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 ()
188 "Non-nil if current window is minibuffer that's doing simple completion.
189
190 Conditions 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
194 indicate some non-standard, non-simple completion mechanism,
195 like file-name and other custom-func completions)."
196
197 (and (window-minibuffer-p (selected-window))
198 (not executing-kbd-macro)
199 (not (symbolp minibuffer-completion-table))))
200
201 ;;;_ > icomplete-minibuffer-setup ()
202 ;;;###autoload
203 (defun icomplete-minibuffer-setup ()
204 "Run in minibuffer on activation to establish incremental completion.
205 Usually run by inclusion in `minibuffer-setup-hook'."
206 (cond ((and icomplete-mode (icomplete-simple-completing-p))
207 (make-local-hook 'pre-command-hook)
208 (add-hook 'pre-command-hook
209 (function (lambda ()
210 (run-hooks 'icomplete-pre-command-hook)))
211 nil t)
212 (make-local-hook 'post-command-hook)
213 (add-hook 'post-command-hook
214 (function (lambda ()
215 (run-hooks 'icomplete-post-command-hook)))
216 nil t)
217 (run-hooks 'icomplete-minibuffer-setup-hook))))
218 \f
219 ;;;_* Completion
220
221 ;;;_ > icomplete-tidy ()
222 (defun icomplete-tidy ()
223 "Remove completions display \(if any) prior to new user input.
224 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
225 and `minibuffer-setup-hook'."
226 (if (icomplete-simple-completing-p)
227 (if (and (boundp 'icomplete-eoinput)
228 icomplete-eoinput)
229
230 (if (> icomplete-eoinput (point-max))
231 ;; Oops, got rug pulled out from under us - reinit:
232 (setq icomplete-eoinput (point-max))
233 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
234 (delete-region icomplete-eoinput (point-max))))
235
236 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
237 (make-local-variable 'icomplete-eoinput)
238 (setq icomplete-eoinput 1))))
239
240 ;;;_ > icomplete-exhibit ()
241 (defun icomplete-exhibit ()
242 "Insert icomplete completions display.
243 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
244 and `minibuffer-setup-hook'."
245 (if (icomplete-simple-completing-p)
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:
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)))
274 (insert-string
275 (icomplete-completions contents
276 minibuffer-completion-table
277 minibuffer-completion-predicate
278 (not
279 minibuffer-completion-confirm))))))))
280
281 ;;;_ > icomplete-completions (name candidates predicate require-match)
282 (defun icomplete-completions (name candidates predicate require-match)
283 "Identify prospective candidates for minibuffer completion.
284
285 The display is updated with each minibuffer keystroke during
286 minibuffer completion.
287
288 Prospective completion suffixes (if any) are displayed, bracketed by
289 one 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
294 further input is required to distinguish a single one.
295
296 The displays for unambiguous matches have ` [Matched]' appended
297 \(whether complete or not), or ` \[No matches]', if no eligible
298 matches exist. \(Keybindings for uniquely matched commands
299 are 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))
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 )
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)))))))
379
380 (if icomplete-mode
381 (icomplete-mode 1))
382
383 ;;;_* Local emacs vars.
384 ;;;Local variables:
385 ;;;outline-layout: (-2 :)
386 ;;;End:
387
388 ;;; icomplete.el ends here