Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; icomplete.el --- minibuffer completion incremental feedback |
239c87a1 | 2 | |
be010748 | 3 | ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc. |
d462ff97 | 4 | |
be010748 RS |
5 | ;; Author: Ken Manheimer <klm@nist.gov> |
6 | ;; Maintainer: Ken Manheimer <klm@nist.gov> | |
7 | ;; Created: Mar 1993 klm@nist.gov - first release to usenet | |
8 | ;; Keywords: help, abbrev | |
d462ff97 | 9 | |
239c87a1 | 10 | ;; This file is part of GNU Emacs. |
d462ff97 | 11 | |
239c87a1 RS |
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
d462ff97 | 16 | |
239c87a1 RS |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
c89164c5 | 21 | |
239c87a1 | 22 | ;; You should have received a copy of the GNU General Public License |
b578f267 EN |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
d462ff97 | 26 | |
239c87a1 | 27 | ;;; Commentary: |
d462ff97 | 28 | |
b578f267 EN |
29 | ;; Loading this package implements a more fine-grained minibuffer |
30 | ;; completion feedback scheme. Prospective completions are concisely | |
31 | ;; indicated within the minibuffer itself, with each successive | |
32 | ;; keystroke. | |
33 | ||
34 | ;; See 'icomplete-completions' docstring for a description of the | |
35 | ;; icomplete display format. | |
36 | ||
37 | ;; See the `icomplete-minibuffer-setup-hook' docstring for a means to | |
38 | ;; customize icomplete setup for interoperation with other | |
39 | ;; minibuffer-oriented packages. | |
40 | ||
41 | ;; To activate icomplete mode, simply load the package. You can | |
42 | ;; subsequently deactivate it by invoking the function icomplete-mode | |
43 | ;; with a negative prefix-arg (C-U -1 ESC-x icomplete-mode). Also, | |
44 | ;; you can prevent activation of the mode during package load by | |
45 | ;; first setting the variable `icomplete-mode' to nil. Icompletion | |
46 | ;; can be enabled any time after the package is loaded by invoking | |
47 | ;; icomplete-mode without a prefix arg. | |
48 | ||
49 | ;; Thanks to everyone for their suggestions for refinements of this | |
50 | ;; package. I particularly have to credit Michael Cook, who | |
51 | ;; implemented an incremental completion style in his 'iswitch' | |
52 | ;; functions that served as a model for icomplete. Some other | |
53 | ;; contributors: Noah Freidman (restructuring as minor mode), Colin | |
54 | ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and | |
55 | ;; others. | |
56 | ||
57 | ;; klm. | |
c89164c5 | 58 | |
239c87a1 RS |
59 | ;;; Code: |
60 | ||
61 | ;;;_* Provide | |
d462ff97 RS |
62 | (provide 'icomplete) |
63 | ||
239c87a1 RS |
64 | ;;;_* User Customization variables |
65 | ||
66 | ;;;_* Initialization | |
67 | ;;;_ = icomplete-minibuffer-setup-hook | |
68 | (defvar icomplete-minibuffer-setup-hook nil | |
69 | "*Icomplete-specific customization of minibuffer setup. | |
70 | ||
71 | This hook is run during minibuffer setup iff icomplete will be active. | |
72 | It is intended for use in customizing icomplete for interoperation | |
73 | with other packages. For instance: | |
74 | ||
75 | \(add-hook 'icomplete-minibuffer-setup-hook | |
76 | \(function | |
77 | \(lambda () | |
78 | \(make-local-variable 'resize-minibuffer-window-max-height) | |
79 | \(setq resize-minibuffer-window-max-height 3)))) | |
80 | ||
81 | will constrain rsz-mini to a maximum minibuffer height of 3 lines when | |
82 | icompletion is occurring.") | |
d462ff97 | 83 | |
c89164c5 | 84 | ;;;_ + Internal Variables |
239c87a1 RS |
85 | ;;;_ = icomplete-mode |
86 | (defvar icomplete-mode t | |
87 | "Non-nil enables incremental minibuffer completion, once | |
88 | `\\[icomplete-mode]' function has set things up.") | |
c89164c5 | 89 | ;;;_ = icomplete-eoinput 1 |
d462ff97 RS |
90 | (defvar icomplete-eoinput 1 |
91 | "Point where minibuffer input ends and completion info begins.") | |
92 | (make-variable-buffer-local 'icomplete-eoinput) | |
239c87a1 RS |
93 | ;;;_ = icomplete-pre-command-hook |
94 | (defvar icomplete-pre-command-hook nil | |
95 | "Incremental-minibuffer-completion pre-command-hook. | |
96 | ||
97 | Is run in minibuffer before user input when `icomplete-mode' is non-nil. | |
98 | Use `icomplete-mode' function to set it up properly for incremental | |
99 | minibuffer completion.") | |
100 | (add-hook 'icomplete-pre-command-hook 'icomplete-tidy) | |
101 | ;;;_ = icomplete-post-command-hook | |
102 | (defvar icomplete-post-command-hook nil | |
103 | "Incremental-minibuffer-completion post-command-hook. | |
104 | ||
105 | Is run in minibuffer after user input when `icomplete-mode' is non-nil. | |
106 | Use `icomplete-mode' function to set it up properly for incremental | |
107 | minibuffer completion.") | |
108 | (add-hook 'icomplete-post-command-hook 'icomplete-exhibit) | |
109 | ||
110 | ;;;_ > icomplete-mode (&optional prefix) | |
c89164c5 | 111 | ;;;###autoload |
239c87a1 RS |
112 | (defun icomplete-mode (&optional prefix) |
113 | "Activate incremental minibuffer completion for this emacs session, | |
114 | or deactivate with negative prefix arg." | |
115 | (interactive "p") | |
116 | (or prefix (setq prefix 0)) | |
117 | (cond ((>= prefix 0) | |
118 | (setq icomplete-mode t) | |
119 | ;; The following is not really necessary after first time - | |
120 | ;; no great loss. | |
121 | (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)) | |
122 | (t (setq icomplete-mode nil)))) | |
123 | ||
124 | ;;;_ > icomplete-simple-completing-p () | |
125 | (defun icomplete-simple-completing-p () | |
126 | ||
127 | "Non-nil if current window is minibuffer that's doing simple completion. | |
128 | ||
129 | Conditions are: | |
130 | the selected window is a minibuffer, | |
131 | and not in the middle of macro execution, | |
132 | and minibuffer-completion-table is not a symbol (which would | |
81276211 | 133 | indicate some non-standard, non-simple completion mechanism, |
239c87a1 RS |
134 | like file-name and other custom-func completions)." |
135 | ||
136 | (and (window-minibuffer-p (selected-window)) | |
137 | (not executing-macro) | |
138 | (not (symbolp minibuffer-completion-table)))) | |
d183f322 | 139 | |
239c87a1 RS |
140 | ;;;_ > icomplete-minibuffer-setup () |
141 | ;;;###autoload | |
142 | (defun icomplete-minibuffer-setup () | |
239c87a1 | 143 | "Run in minibuffer on activation to establish incremental completion. |
d183f322 | 144 | Usually run by inclusion in `minibuffer-setup-hook'." |
239c87a1 | 145 | (cond ((and icomplete-mode (icomplete-simple-completing-p)) |
d183f322 | 146 | (make-local-hook 'pre-command-hook) |
239c87a1 RS |
147 | (add-hook 'pre-command-hook |
148 | (function (lambda () | |
d183f322 RS |
149 | (run-hooks 'icomplete-pre-command-hook))) |
150 | nil t) | |
151 | (make-local-hook 'post-command-hook) | |
239c87a1 RS |
152 | (add-hook 'post-command-hook |
153 | (function (lambda () | |
d183f322 RS |
154 | (run-hooks 'icomplete-post-command-hook))) |
155 | nil t) | |
239c87a1 | 156 | (run-hooks 'icomplete-minibuffer-setup-hook)))) |
d183f322 | 157 | \f |
239c87a1 RS |
158 | ;;;_* Completion |
159 | ||
160 | ;;;_ > icomplete-tidy () | |
161 | (defun icomplete-tidy () | |
162 | "Remove completions display \(if any) prior to new user input. | |
d183f322 | 163 | Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode' |
239c87a1 RS |
164 | and `minibuffer-setup-hook'." |
165 | (if (icomplete-simple-completing-p) | |
d462ff97 RS |
166 | (if (and (boundp 'icomplete-eoinput) |
167 | icomplete-eoinput) | |
239c87a1 | 168 | |
d462ff97 RS |
169 | (if (> icomplete-eoinput (point-max)) |
170 | ;; Oops, got rug pulled out from under us - reinit: | |
171 | (setq icomplete-eoinput (point-max)) | |
c89164c5 | 172 | (let ((buffer-undo-list buffer-undo-list )) ; prevent entry |
d462ff97 | 173 | (delete-region icomplete-eoinput (point-max)))) |
239c87a1 | 174 | |
c89164c5 | 175 | ;; Reestablish the local variable 'cause minibuffer-setup is weird: |
d462ff97 RS |
176 | (make-local-variable 'icomplete-eoinput) |
177 | (setq icomplete-eoinput 1)))) | |
d183f322 | 178 | |
239c87a1 | 179 | ;;;_ > icomplete-exhibit () |
d462ff97 | 180 | (defun icomplete-exhibit () |
239c87a1 | 181 | "Insert icomplete completions display. |
d183f322 | 182 | Should be run via minibuffer `post-command-hook'. See `icomplete-mode' |
239c87a1 RS |
183 | and `minibuffer-setup-hook'." |
184 | (if (icomplete-simple-completing-p) | |
d462ff97 RS |
185 | (let ((contents (buffer-substring (point-min)(point-max))) |
186 | (buffer-undo-list t)) | |
187 | (save-excursion | |
188 | (goto-char (point-max)) | |
189 | ; Register the end of input, so we | |
190 | ; know where the extra stuff | |
191 | ; (match-status info) begins: | |
192 | (if (not (boundp 'icomplete-eoinput)) | |
193 | ;; In case it got wiped out by major mode business: | |
194 | (make-local-variable 'icomplete-eoinput)) | |
195 | (setq icomplete-eoinput (point)) | |
196 | ; Insert the match-status information: | |
197 | (if (> (point-max) 1) | |
198 | (insert-string | |
239c87a1 RS |
199 | (icomplete-completions contents |
200 | minibuffer-completion-table | |
201 | minibuffer-completion-predicate | |
202 | (not | |
203 | minibuffer-completion-confirm)))))))) | |
d183f322 | 204 | |
239c87a1 RS |
205 | ;;;_ > icomplete-completions (name candidates predicate require-match) |
206 | (defun icomplete-completions (name candidates predicate require-match) | |
d462ff97 RS |
207 | "Identify prospective candidates for minibuffer completion. |
208 | ||
c89164c5 | 209 | The display is updated with each minibuffer keystroke during |
d462ff97 RS |
210 | minibuffer completion. |
211 | ||
212 | Prospective completion suffixes (if any) are displayed, bracketed by | |
213 | one of \(), \[], or \{} pairs. The choice of brackets is as follows: | |
214 | ||
215 | \(...) - a single prospect is identified and matching is enforced, | |
216 | \[...] - a single prospect is identified but matching is optional, or | |
217 | \{...} - multiple prospects, separated by commas, are indicated, and | |
81276211 | 218 | further input is required to distinguish a single one. |
d462ff97 | 219 | |
81276211 | 220 | The displays for unambiguous matches have ` [Matched]' appended |
d183f322 | 221 | \(whether complete or not), or ` \[No matches]', if no eligible |
d462ff97 RS |
222 | matches exist." |
223 | ||
224 | (let ((comps (all-completions name candidates predicate)) | |
225 | ; "-determined" - only one candidate | |
226 | (open-bracket-determined (if require-match "(" "[")) | |
227 | (close-bracket-determined (if require-match ")" "]")) | |
228 | ;"-prospects" - more than one candidate | |
229 | (open-bracket-prospects "{") | |
230 | (close-bracket-prospects "}") | |
231 | ) | |
232 | (cond ((null comps) (format " %sNo matches%s" | |
233 | open-bracket-determined | |
234 | close-bracket-determined)) | |
235 | ((null (cdr comps)) ;one match | |
236 | (concat (if (and (> (length (car comps)) | |
237 | (length name))) | |
238 | (concat open-bracket-determined | |
239 | (substring (car comps) (length name)) | |
240 | close-bracket-determined) | |
241 | "") | |
242 | " [Matched]")) | |
243 | (t ;multiple matches | |
244 | (let* ((most (try-completion name candidates predicate)) | |
245 | (most-len (length most)) | |
246 | most-is-exact | |
247 | (alternatives | |
248 | (apply | |
239c87a1 RS |
249 | (function concat) |
250 | (cdr (apply | |
251 | (function nconc) | |
252 | (mapcar '(lambda (com) | |
253 | (if (= (length com) most-len) | |
254 | ;; Most is one exact match, | |
255 | ;; note that and leave out | |
256 | ;; for later indication: | |
257 | (progn | |
258 | (setq most-is-exact t) | |
259 | ()) | |
260 | (list "," | |
261 | (substring com | |
262 | most-len)))) | |
263 | comps)))))) | |
d462ff97 RS |
264 | (concat (and (> most-len (length name)) |
265 | (concat open-bracket-determined | |
266 | (substring most (length name)) | |
267 | close-bracket-determined)) | |
268 | open-bracket-prospects | |
269 | (if most-is-exact | |
270 | (concat "," alternatives) | |
271 | alternatives) | |
272 | close-bracket-prospects)))))) | |
273 | ||
239c87a1 RS |
274 | ;;;_ + Initialization |
275 | ;;; If user hasn't setq-default icomplete-mode to nil, then setup for | |
276 | ;;; activation: | |
277 | (if icomplete-mode | |
278 | (icomplete-mode)) | |
279 | ||
280 | ||
281 | ;;;_* Local emacs vars. | |
282 | ;;;Local variables: | |
283 | ;;;outline-layout: (-2 :) | |
284 | ;;;End: | |
d462ff97 RS |
285 | |
286 | ;;; icomplete.el ends here | |
c89164c5 | 287 |