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