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