21803
|
1 ;;; icomplete.el --- minibuffer completion incremental feedback
|
8871
|
2
|
18060
|
3 ;; Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
|
5147
|
4
|
18029
|
5 ;; Author: Ken Manheimer <klm@python.org>
|
|
6 ;; Maintainer: Ken Manheimer <klm@python.org>
|
13337
|
7 ;; Created: Mar 1993 klm@nist.gov - first release to usenet
|
|
8 ;; Keywords: help, abbrev
|
5147
|
9
|
8871
|
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
|
14169
|
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.
|
8871
|
26
|
5147
|
27 ;;; Commentary:
|
|
28
|
14169
|
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.
|
8871
|
33
|
20764
|
34 ;; See `icomplete-completions' docstring for a description of the
|
14169
|
35 ;; icomplete display format.
|
8871
|
36
|
14169
|
37 ;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
|
|
38 ;; customize icomplete setup for interoperation with other
|
|
39 ;; minibuffer-oriented packages.
|
5147
|
40
|
20764
|
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.
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
49
|
18029
|
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
|
14169
|
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
|
20764
|
59 ;; contributors: Noah Friedman (restructuring as minor mode), Colin
|
18251
|
60 ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
|
8871
|
61
|
14169
|
62 ;; klm.
|
5147
|
63
|
|
64 ;;; Code:
|
|
65
|
8871
|
66 ;;;_* Provide
|
5147
|
67 (provide 'icomplete)
|
|
68
|
20764
|
69
|
|
70 (defgroup icomplete nil
|
|
71 "Show completions dynamically in minibuffer."
|
|
72 :prefix "icomplete-"
|
|
73 :group 'minibuffer)
|
|
74
|
8871
|
75 ;;;_* User Customization variables
|
20764
|
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
|
18029
|
89 "*Completions-computation stall, used only with large-number
|
20764
|
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)
|
8871
|
98
|
20764
|
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
|
8871
|
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
|
18029
|
116 \(add-hook 'icomplete-minibuffer-setup-hook
|
8871
|
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
|
20764
|
123 icompletion is occurring."
|
|
124 :type 'hook
|
|
125 :group 'icomplete)
|
|
126
|
|
127
|
|
128 ;;;_* Initialization
|
5147
|
129
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
130 ;;;_ + Internal Variables
|
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
131 ;;;_ = icomplete-eoinput 1
|
5147
|
132 (defvar icomplete-eoinput 1
|
|
133 "Point where minibuffer input ends and completion info begins.")
|
|
134 (make-variable-buffer-local 'icomplete-eoinput)
|
8871
|
135 ;;;_ = icomplete-pre-command-hook
|
|
136 (defvar icomplete-pre-command-hook nil
|
|
137 "Incremental-minibuffer-completion pre-command-hook.
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
138
|
8871
|
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.
|
5147
|
146
|
8871
|
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)
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
151
|
18029
|
152 (defun icomplete-get-keys (func-name)
|
18251
|
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)
|
18029
|
157 (save-excursion
|
|
158 (let* ((sym (intern func-name))
|
18251
|
159 (buf (other-buffer))
|
|
160 (map (save-excursion (set-buffer buf) (current-local-map)))
|
|
161 (keys (where-is-internal sym map)))
|
18029
|
162 (if keys
|
|
163 (concat "<"
|
|
164 (mapconcat 'key-description
|
|
165 (sort keys
|
|
166 #'(lambda (x y)
|
|
167 (< (length x) (length y))))
|
|
168 ", ")
|
|
169 ">"))))))
|
|
170
|
8871
|
171 ;;;_ > icomplete-mode (&optional prefix)
|
|
172 ;;;###autoload
|
|
173 (defun icomplete-mode (&optional prefix)
|
18251
|
174 "Activate incremental minibuffer completion for this Emacs session.
|
|
175 Deactivates with negative universal argument."
|
8871
|
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))))
|
5147
|
184
|
8871
|
185 ;;;_ > icomplete-simple-completing-p ()
|
|
186 (defun icomplete-simple-completing-p ()
|
|
187 "Non-nil if current window is minibuffer that's doing simple completion.
|
8434
36da3d480ccd
(icomplete-prime-minibuffer): Copy the hook lists after making them local.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
188
|
8871
|
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
|
13989
|
193 indicate some non-standard, non-simple completion mechanism,
|
8871
|
194 like file-name and other custom-func completions)."
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
195
|
8871
|
196 (and (window-minibuffer-p (selected-window))
|
15302
|
197 (not executing-kbd-macro)
|
8871
|
198 (not (symbolp minibuffer-completion-table))))
|
12933
|
199
|
8871
|
200 ;;;_ > icomplete-minibuffer-setup ()
|
|
201 ;;;###autoload
|
|
202 (defun icomplete-minibuffer-setup ()
|
|
203 "Run in minibuffer on activation to establish incremental completion.
|
12933
|
204 Usually run by inclusion in `minibuffer-setup-hook'."
|
8871
|
205 (cond ((and icomplete-mode (icomplete-simple-completing-p))
|
12933
|
206 (make-local-hook 'pre-command-hook)
|
8871
|
207 (add-hook 'pre-command-hook
|
|
208 (function (lambda ()
|
12933
|
209 (run-hooks 'icomplete-pre-command-hook)))
|
|
210 nil t)
|
|
211 (make-local-hook 'post-command-hook)
|
8871
|
212 (add-hook 'post-command-hook
|
|
213 (function (lambda ()
|
12933
|
214 (run-hooks 'icomplete-post-command-hook)))
|
|
215 nil t)
|
8871
|
216 (run-hooks 'icomplete-minibuffer-setup-hook))))
|
12933
|
217
|
8871
|
218 ;;;_* Completion
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
219
|
8871
|
220 ;;;_ > icomplete-tidy ()
|
|
221 (defun icomplete-tidy ()
|
|
222 "Remove completions display \(if any) prior to new user input.
|
12933
|
223 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
|
8871
|
224 and `minibuffer-setup-hook'."
|
|
225 (if (icomplete-simple-completing-p)
|
5147
|
226 (if (and (boundp 'icomplete-eoinput)
|
|
227 icomplete-eoinput)
|
8871
|
228
|
5147
|
229 (if (> icomplete-eoinput (point-max))
|
|
230 ;; Oops, got rug pulled out from under us - reinit:
|
|
231 (setq icomplete-eoinput (point-max))
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
232 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
|
5147
|
233 (delete-region icomplete-eoinput (point-max))))
|
8871
|
234
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
235 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
|
5147
|
236 (make-local-variable 'icomplete-eoinput)
|
|
237 (setq icomplete-eoinput 1))))
|
12933
|
238
|
8871
|
239 ;;;_ > icomplete-exhibit ()
|
|
240 (defun icomplete-exhibit ()
|
|
241 "Insert icomplete completions display.
|
12933
|
242 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
|
8871
|
243 and `minibuffer-setup-hook'."
|
|
244 (if (icomplete-simple-completing-p)
|
5147
|
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:
|
18029
|
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)))
|
5147
|
273 (insert-string
|
8871
|
274 (icomplete-completions contents
|
|
275 minibuffer-completion-table
|
|
276 minibuffer-completion-predicate
|
|
277 (not
|
|
278 minibuffer-completion-confirm))))))))
|
12933
|
279
|
8871
|
280 ;;;_ > icomplete-completions (name candidates predicate require-match)
|
|
281 (defun icomplete-completions (name candidates predicate require-match)
|
5147
|
282 "Identify prospective candidates for minibuffer completion.
|
|
283
|
5375
0e30cf7aa525
(icomplete-pre-command-hook): Reconciled with keyboard macro operation.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
284 The display is updated with each minibuffer keystroke during
|
5147
|
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
|
13989
|
293 further input is required to distinguish a single one.
|
5147
|
294
|
13989
|
295 The displays for unambiguous matches have ` [Matched]' appended
|
12933
|
296 \(whether complete or not), or ` \[No matches]', if no eligible
|
18251
|
297 matches exist. \(Keybindings for uniquely matched commands
|
18029
|
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))
|
5147
|
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 )
|
18029
|
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)))))))
|
5147
|
378
|
20764
|
379 (if icomplete-mode
|
|
380 (icomplete-mode 1))
|
|
381
|
8871
|
382 ;;;_* Local emacs vars.
|
|
383 ;;;Local variables:
|
|
384 ;;;outline-layout: (-2 :)
|
|
385 ;;;End:
|
5147
|
386
|
|
387 ;;; icomplete.el ends here
|