70
|
1 ;;; icomplete.el --- minibuffer completion with incremental feedback
|
0
|
2
|
70
|
3 ;;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
0
|
4
|
70
|
5 ;;; Author: Ken Manheimer <klm@nist.gov>
|
|
6 ;;; Maintainer: Ken Manheimer <klm@nist.gov>
|
74
|
7 ;;; Version: $Id: icomplete.el,v 1.1.1.3 1996/12/21 20:48:12 steve Exp $
|
70
|
8 ;;; Created: Mar 1993 klm@nist.gov - first release to usenet
|
|
9 ;;; Keywords: help, abbrev
|
0
|
10
|
70
|
11 ;;; Hacked for XEmacs: David Hughes 7th September 1995
|
0
|
12
|
70
|
13 ;; This file is part of XEmacs.
|
|
14
|
|
15 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
16 ;; under the terms of the GNU General Public License as published by
|
0
|
17 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
18 ;; any later version.
|
|
19
|
70
|
20 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
23 ;; General Public License for more details.
|
0
|
24
|
|
25 ;; You should have received a copy of the GNU General Public License
|
70
|
26 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
27 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
28 ;; 02111-1307, USA.
|
0
|
29
|
70
|
30 ;;; Synched up with: FSF 19.34.
|
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
|
|
75
|
|
76 ;;;_* Initialization
|
|
77 ;;;_ = icomplete-minibuffer-setup-hook
|
|
78 (defvar icomplete-minibuffer-setup-hook nil
|
|
79 "*Icomplete-specific customization of minibuffer setup.
|
|
80
|
|
81 This hook is run during minibuffer setup iff icomplete will be active.
|
|
82 It is intended for use in customizing icomplete for interoperation
|
|
83 with other packages. For instance:
|
|
84
|
|
85 \(add-hook 'icomplete-minibuffer-setup-hook
|
|
86 \(function
|
|
87 \(lambda ()
|
|
88 \(make-local-variable 'resize-minibuffer-window-max-height)
|
|
89 \(setq resize-minibuffer-window-max-height 3))))
|
|
90
|
|
91 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
|
|
92 icompletion is occurring.")
|
|
93
|
72
|
94 (if (string-match "XEmacs\\|Lucid" emacs-version)
|
|
95 (add-hook 'icomplete-minibuffer-setup-hook 'icomplete-exhibit))
|
|
96
|
0
|
97 ;;;_ + Internal Variables
|
|
98 ;;;_ = icomplete-mode
|
|
99 (defvar icomplete-mode t
|
|
100 "Non-nil enables incremental minibuffer completion, once
|
|
101 `\\[icomplete-mode]' function has set things up.")
|
|
102 ;;;_ = icomplete-eoinput 1
|
|
103 (defvar icomplete-eoinput 1
|
|
104 "Point where minibuffer input ends and completion info begins.")
|
|
105 (make-variable-buffer-local 'icomplete-eoinput)
|
|
106 ;;;_ = icomplete-pre-command-hook
|
|
107 (defvar icomplete-pre-command-hook nil
|
|
108 "Incremental-minibuffer-completion pre-command-hook.
|
|
109
|
|
110 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
|
|
111 Use `icomplete-mode' function to set it up properly for incremental
|
|
112 minibuffer completion.")
|
|
113 (add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
|
|
114 ;;;_ = icomplete-post-command-hook
|
|
115 (defvar icomplete-post-command-hook nil
|
|
116 "Incremental-minibuffer-completion post-command-hook.
|
|
117
|
|
118 Is run in minibuffer after user input when `icomplete-mode' is non-nil.
|
|
119 Use `icomplete-mode' function to set it up properly for incremental
|
|
120 minibuffer completion.")
|
|
121 (add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
|
|
122
|
2
|
123 ;; XEmacs addition
|
70
|
124 (defvar icomplete-show-key-bindings t
|
2
|
125 "When non-nil show key bindings as well as completion when matching
|
70
|
126 a command.")
|
0
|
127
|
70
|
128 ;; XEmacs addition
|
2
|
129 (defun icomplete-get-keys (func-name)
|
70
|
130 "Return the keys `func-name' is bound to as a string."
|
2
|
131 (when (commandp func-name)
|
70
|
132 (let* ((sym (intern func-name))
|
|
133 (keys (where-is-internal sym)))
|
|
134 (concat "<"
|
|
135 (if keys
|
|
136 (mapconcat 'key-description
|
74
|
137 (sort '([next] [kp-next] [(control v)])
|
70
|
138 #'(lambda (x y)
|
|
139 (< (length x) (length y))))
|
|
140 ", ")
|
|
141 "Unbound")
|
|
142 ">"))))
|
0
|
143
|
|
144 ;;;_ > icomplete-mode (&optional prefix)
|
|
145 ;;;###autoload
|
|
146 (defun icomplete-mode (&optional prefix)
|
|
147 "Activate incremental minibuffer completion for this emacs session,
|
|
148 or deactivate with negative prefix arg."
|
|
149 (interactive "p")
|
|
150 (or prefix (setq prefix 0))
|
|
151 (cond ((>= prefix 0)
|
|
152 (setq icomplete-mode t)
|
|
153 ;; The following is not really necessary after first time -
|
|
154 ;; no great loss.
|
|
155 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup))
|
|
156 (t (setq icomplete-mode nil))))
|
|
157
|
|
158 ;;;_ > icomplete-simple-completing-p ()
|
|
159 (defun icomplete-simple-completing-p ()
|
|
160
|
|
161 "Non-nil if current window is minibuffer that's doing simple completion.
|
|
162
|
|
163 Conditions are:
|
|
164 the selected window is a minibuffer,
|
|
165 and not in the middle of macro execution,
|
|
166 and minibuffer-completion-table is not a symbol (which would
|
2
|
167 indicate some non-standard, non-simple completion mechanism,
|
0
|
168 like file-name and other custom-func completions)."
|
|
169
|
|
170 (and (window-minibuffer-p (selected-window))
|
2
|
171 (not executing-kbd-macro)
|
0
|
172 (not (symbolp minibuffer-completion-table))))
|
2
|
173
|
0
|
174 ;;;_ > icomplete-minibuffer-setup ()
|
|
175 ;;;###autoload
|
|
176 (defun icomplete-minibuffer-setup ()
|
|
177 "Run in minibuffer on activation to establish incremental completion.
|
2
|
178 Usually run by inclusion in `minibuffer-setup-hook'."
|
0
|
179 (cond ((and icomplete-mode (icomplete-simple-completing-p))
|
|
180 (make-local-hook 'pre-command-hook)
|
2
|
181 (add-hook 'pre-command-hook
|
|
182 (function (lambda ()
|
|
183 (run-hooks 'icomplete-pre-command-hook)))
|
|
184 nil t)
|
0
|
185 (make-local-hook 'post-command-hook)
|
2
|
186 (add-hook 'post-command-hook
|
|
187 (function (lambda ()
|
|
188 (run-hooks 'icomplete-post-command-hook)))
|
|
189 nil t)
|
0
|
190 (run-hooks 'icomplete-minibuffer-setup-hook))))
|
2
|
191
|
0
|
192 ;;;_* Completion
|
|
193
|
|
194 ;;;_ > icomplete-tidy ()
|
|
195 (defun icomplete-tidy ()
|
|
196 "Remove completions display \(if any) prior to new user input.
|
2
|
197 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
|
0
|
198 and `minibuffer-setup-hook'."
|
|
199 (if (icomplete-simple-completing-p)
|
|
200 (if (and (boundp 'icomplete-eoinput)
|
|
201 icomplete-eoinput)
|
|
202
|
|
203 (if (> icomplete-eoinput (point-max))
|
|
204 ;; Oops, got rug pulled out from under us - reinit:
|
|
205 (setq icomplete-eoinput (point-max))
|
|
206 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
|
|
207 (delete-region icomplete-eoinput (point-max))))
|
|
208
|
|
209 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
|
|
210 (make-local-variable 'icomplete-eoinput)
|
|
211 (setq icomplete-eoinput 1))))
|
2
|
212
|
0
|
213 ;;;_ > icomplete-exhibit ()
|
|
214 (defun icomplete-exhibit ()
|
|
215 "Insert icomplete completions display.
|
2
|
216 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
|
0
|
217 and `minibuffer-setup-hook'."
|
|
218 (if (icomplete-simple-completing-p)
|
2
|
219 (let ((contents (buffer-substring (point-min)(point-max)))
|
|
220 (buffer-undo-list t))
|
0
|
221 (save-excursion
|
|
222 (goto-char (point-max))
|
|
223 ; Register the end of input, so we
|
|
224 ; know where the extra stuff
|
|
225 ; (match-status info) begins:
|
|
226 (if (not (boundp 'icomplete-eoinput))
|
|
227 ;; In case it got wiped out by major mode business:
|
|
228 (make-local-variable 'icomplete-eoinput))
|
|
229 (setq icomplete-eoinput (point))
|
|
230 ; Insert the match-status information:
|
70
|
231 (if (> (point-max) 1)
|
0
|
232 (insert-string
|
|
233 (icomplete-completions contents
|
|
234 minibuffer-completion-table
|
|
235 minibuffer-completion-predicate
|
|
236 (not
|
|
237 minibuffer-completion-confirm))))))))
|
2
|
238
|
0
|
239 ;;;_ > icomplete-completions (name candidates predicate require-match)
|
|
240 (defun icomplete-completions (name candidates predicate require-match)
|
|
241 "Identify prospective candidates for minibuffer completion.
|
|
242
|
|
243 The display is updated with each minibuffer keystroke during
|
|
244 minibuffer completion.
|
|
245
|
|
246 Prospective completion suffixes (if any) are displayed, bracketed by
|
|
247 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
|
|
248
|
|
249 \(...) - a single prospect is identified and matching is enforced,
|
|
250 \[...] - a single prospect is identified but matching is optional, or
|
|
251 \{...} - multiple prospects, separated by commas, are indicated, and
|
2
|
252 further input is required to distinguish a single one.
|
0
|
253
|
2
|
254 The displays for unambiguous matches have ` [Matched]' appended
|
|
255 \(whether complete or not), or ` \[No matches]', if no eligible
|
70
|
256 matches exist."
|
0
|
257
|
|
258 (let ((comps (all-completions name candidates predicate))
|
|
259 ; "-determined" - only one candidate
|
|
260 (open-bracket-determined (if require-match "(" "["))
|
|
261 (close-bracket-determined (if require-match ")" "]"))
|
|
262 ;"-prospects" - more than one candidate
|
|
263 (open-bracket-prospects "{")
|
|
264 (close-bracket-prospects "}")
|
|
265 )
|
70
|
266 (cond ((null comps) (format " %sNo matches%s"
|
|
267 open-bracket-determined
|
|
268 close-bracket-determined))
|
|
269 ((null (cdr comps)) ;one match
|
|
270 (concat (if (and (> (length (car comps))
|
|
271 (length name)))
|
|
272 (concat open-bracket-determined
|
|
273 (substring (car comps) (length name))
|
|
274 close-bracket-determined)
|
|
275 "")
|
|
276 " [Matched]"
|
|
277 ;; XEmacs
|
|
278 (if (and icomplete-show-key-bindings
|
72
|
279 (commandp (intern-soft (car comps))))
|
70
|
280 (icomplete-get-keys (car comps))
|
|
281 "")
|
|
282 ))
|
|
283 (t ;multiple matches
|
|
284 (let* ((most (try-completion name candidates predicate))
|
|
285 (most-len (length most))
|
|
286 most-is-exact
|
|
287 (alternatives
|
|
288 (apply
|
|
289 (function concat)
|
|
290 (cdr (apply
|
|
291 (function nconc)
|
|
292 (mapcar '(lambda (com)
|
|
293 (if (= (length com) most-len)
|
|
294 ;; Most is one exact match,
|
|
295 ;; note that and leave out
|
|
296 ;; for later indication:
|
|
297 (progn
|
|
298 (setq most-is-exact t)
|
|
299 ())
|
|
300 (list ","
|
|
301 (substring com
|
|
302 most-len))))
|
|
303 comps))))))
|
|
304 (concat (and (> most-len (length name))
|
|
305 (concat open-bracket-determined
|
|
306 (substring most (length name))
|
0
|
307 close-bracket-determined))
|
70
|
308 open-bracket-prospects
|
|
309 (if most-is-exact
|
|
310 (concat "," alternatives)
|
|
311 alternatives)
|
|
312 close-bracket-prospects))))))
|
0
|
313
|
|
314 ;;;_ + Initialization
|
|
315 ;;; If user hasn't setq-default icomplete-mode to nil, then setup for
|
|
316 ;;; activation:
|
|
317 (if icomplete-mode
|
|
318 (icomplete-mode))
|
|
319
|
|
320
|
|
321 ;;;_* Local emacs vars.
|
|
322 ;;;Local variables:
|
|
323 ;;;outline-layout: (-2 :)
|
|
324 ;;;End:
|
|
325
|
|
326 ;;; icomplete.el ends here
|