]> code.delx.au - gnu-emacs/blob - lisp/icomplete.el
(mark-fortran-subprogram): Activate mark
[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 "*Non-nil enables incremental minibuffer completion.
78 As text is typed into the minibuffer, prospective completions are indicated
79 in the minibuffer.
80 You 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
89 "*Completions-computation stall, used only with large-number
90 completions - 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
110 "*Icomplete-specific customization of minibuffer setup.
111
112 This hook is run during minibuffer setup iff icomplete will be active.
113 It is intended for use in customizing icomplete for interoperation
114 with other packages. For instance:
115
116 \(add-hook 'icomplete-minibuffer-setup-hook
117 \(function
118 \(lambda ()
119 \(make-local-variable 'resize-minibuffer-window-max-height)
120 \(setq resize-minibuffer-window-max-height 3))))
121
122 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
123 icompletion is occurring."
124 :type 'hook
125 :group 'icomplete)
126
127
128 ;;;_* Initialization
129
130 ;;;_ + Internal Variables
131 ;;;_ = icomplete-eoinput 1
132 (defvar icomplete-eoinput 1
133 "Point where minibuffer input ends and completion info begins.")
134 (make-variable-buffer-local 'icomplete-eoinput)
135 ;;;_ = icomplete-pre-command-hook
136 (defvar icomplete-pre-command-hook nil
137 "Incremental-minibuffer-completion pre-command-hook.
138
139 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
140 Use `icomplete-mode' function to set it up properly for incremental
141 minibuffer 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
147 Is run in minibuffer after 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-post-command-hook 'icomplete-exhibit)
151
152 (defun icomplete-get-keys (func-name)
153 "Return strings naming keys bound to `func-name', or nil if none.
154 Examines the prior, not current, buffer, presuming that current buffer
155 is minibuffer."
156 (if (commandp func-name)
157 (save-excursion
158 (let* ((sym (intern func-name))
159 (buf (other-buffer))
160 (map (save-excursion (set-buffer buf) (current-local-map)))
161 (keys (where-is-internal sym map)))
162 (if keys
163 (concat "<"
164 (mapconcat 'key-description
165 (sort keys
166 #'(lambda (x y)
167 (< (length x) (length y))))
168 ", ")
169 ">"))))))
170
171 ;;;_ > icomplete-mode (&optional prefix)
172 ;;;###autoload
173 (defun icomplete-mode (&optional prefix)
174 "Activate incremental minibuffer completion for this Emacs session.
175 Deactivates with negative universal argument."
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 ()
187 "Non-nil if current window is minibuffer that's doing simple completion.
188
189 Conditions 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
193 indicate some non-standard, non-simple completion mechanism,
194 like file-name and other custom-func completions)."
195
196 (and (window-minibuffer-p (selected-window))
197 (not executing-kbd-macro)
198 (not (symbolp minibuffer-completion-table))))
199
200 ;;;_ > icomplete-minibuffer-setup ()
201 ;;;###autoload
202 (defun icomplete-minibuffer-setup ()
203 "Run in minibuffer on activation to establish incremental completion.
204 Usually run by inclusion in `minibuffer-setup-hook'."
205 (cond ((and icomplete-mode (icomplete-simple-completing-p))
206 (make-local-hook 'pre-command-hook)
207 (add-hook 'pre-command-hook
208 (function (lambda ()
209 (run-hooks 'icomplete-pre-command-hook)))
210 nil t)
211 (make-local-hook 'post-command-hook)
212 (add-hook 'post-command-hook
213 (function (lambda ()
214 (run-hooks 'icomplete-post-command-hook)))
215 nil t)
216 (run-hooks 'icomplete-minibuffer-setup-hook))))
217 \f
218 ;;;_* Completion
219
220 ;;;_ > icomplete-tidy ()
221 (defun icomplete-tidy ()
222 "Remove completions display \(if any) prior to new user input.
223 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
224 and `minibuffer-setup-hook'."
225 (if (icomplete-simple-completing-p)
226 (if (and (boundp 'icomplete-eoinput)
227 icomplete-eoinput)
228
229 (if (> icomplete-eoinput (point-max))
230 ;; Oops, got rug pulled out from under us - reinit:
231 (setq icomplete-eoinput (point-max))
232 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
233 (delete-region icomplete-eoinput (point-max))))
234
235 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
236 (make-local-variable 'icomplete-eoinput)
237 (setq icomplete-eoinput 1))))
238
239 ;;;_ > icomplete-exhibit ()
240 (defun icomplete-exhibit ()
241 "Insert icomplete completions display.
242 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
243 and `minibuffer-setup-hook'."
244 (if (icomplete-simple-completing-p)
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:
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)))
273 (insert-string
274 (icomplete-completions contents
275 minibuffer-completion-table
276 minibuffer-completion-predicate
277 (not
278 minibuffer-completion-confirm))))))))
279
280 ;;;_ > icomplete-completions (name candidates predicate require-match)
281 (defun icomplete-completions (name candidates predicate require-match)
282 "Identify prospective candidates for minibuffer completion.
283
284 The display is updated with each minibuffer keystroke during
285 minibuffer completion.
286
287 Prospective completion suffixes (if any) are displayed, bracketed by
288 one 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
293 further input is required to distinguish a single one.
294
295 The displays for unambiguous matches have ` [Matched]' appended
296 \(whether complete or not), or ` \[No matches]', if no eligible
297 matches exist. \(Keybindings for uniquely matched commands
298 are 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))
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 )
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)))))))
378
379 (if icomplete-mode
380 (icomplete-mode 1))
381
382 ;;;_* Local emacs vars.
383 ;;;Local variables:
384 ;;;outline-layout: (-2 :)
385 ;;;End:
386
387 ;;; icomplete.el ends here