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