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