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