comparison lisp/icomplete.el @ 60805:aba5f8c41ae0

Don't forcibly turn on the mode upon load. (icomplete-mode): Use define-minor-mode. (icomplete-eoinput): Default to nil. (icomplete-minibuffer-setup): Remove autoload. (icomplete-tidy): Simplify. (icomplete-exhibit): Use buffer-undo-list to determine if we're still in the initial state or if the user has modified the field. Fix handling of icomplete-max-delay-chars. Remove code that handles the oddball case where minibuffer-completion-table is an integer. Wrap icomplete-completions in while-no-input in case building completions takes more time than expected. (icomplete-completions): Simplify.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Mon, 21 Mar 2005 19:19:15 +0000
parents 695cf19ef79e
children 53a24edea9ee
comparison
equal deleted inserted replaced
60804:2d1a165680f5 60805:aba5f8c41ae0
1 ;;; icomplete.el --- minibuffer completion incremental feedback 1 ;;; icomplete.el --- minibuffer completion incremental feedback
2 2
3 ;; Copyright (C) 1992, 1993, 1994, 1997, 1999, 2001 3 ;; Copyright (C) 1992, 1993, 1994, 1997, 1999, 2001, 2005
4 ;;; Free Software Foundation, Inc. 4 ;; Free Software Foundation, Inc.
5 5
6 ;; Author: Ken Manheimer <klm@i.am> 6 ;; Author: Ken Manheimer <klm@i.am>
7 ;; Maintainer: 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 8 ;; Created: Mar 1993 Ken Manheimer, klm@nist.gov - first release to usenet
9 ;; Last update: Ken Manheimer <klm@i.am>, 11/18/1999. 9 ;; Last update: Ken Manheimer <klm@i.am>, 11/18/1999.
67 (defgroup icomplete nil 67 (defgroup icomplete nil
68 "Show completions dynamically in minibuffer." 68 "Show completions dynamically in minibuffer."
69 :prefix "icomplete-" 69 :prefix "icomplete-"
70 :group 'minibuffer) 70 :group 'minibuffer)
71 71
72 (defcustom icomplete-mode nil
73 "*Toggle incremental minibuffer completion.
74 As text is typed into the minibuffer, prospective completions are indicated
75 in the minibuffer.
76 Setting this variable directly does not take effect;
77 use either \\[customize] or the function `icomplete-mode'."
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
85 ;;;_* User Customization variables 72 ;;;_* User Customization variables
86 (defcustom icomplete-prospects-length 80 73 (defcustom icomplete-prospects-length 80
87 "*Length of string displaying the prospects." 74 "*Length of string displaying the prospects."
88 :type 'integer 75 :type 'integer
89 :group 'icomplete) 76 :group 'icomplete)
129 116
130 117
131 ;;;_* Initialization 118 ;;;_* Initialization
132 119
133 ;;;_ + Internal Variables 120 ;;;_ + Internal Variables
134 ;;;_ = icomplete-eoinput 1 121 ;;;_ = icomplete-eoinput nil
135 (defvar icomplete-eoinput 1 122 (defvar icomplete-eoinput nil
136 "Point where minibuffer input ends and completion info begins.") 123 "Point where minibuffer input ends and completion info begins.")
137 (make-variable-buffer-local 'icomplete-eoinput) 124 (make-variable-buffer-local 'icomplete-eoinput)
138 ;;;_ = icomplete-pre-command-hook 125 ;;;_ = icomplete-pre-command-hook
139 (defvar icomplete-pre-command-hook nil 126 (defvar icomplete-pre-command-hook nil
140 "Incremental-minibuffer-completion pre-command-hook. 127 "Incremental-minibuffer-completion pre-command-hook.
171 ", ") 158 ", ")
172 ">")))))) 159 ">"))))))
173 160
174 ;;;_ > icomplete-mode (&optional prefix) 161 ;;;_ > icomplete-mode (&optional prefix)
175 ;;;###autoload 162 ;;;###autoload
176 (defun icomplete-mode (&optional arg) 163 (define-minor-mode icomplete-mode
177 "Toggle incremental minibuffer completion for this Emacs session. 164 "Toggle incremental minibuffer completion for this Emacs session.
178 With a numeric argument, turn Icomplete mode on iff ARG is positive." 165 With a numeric argument, turn Icomplete mode on iff ARG is positive."
179 (interactive "P") 166 :global t :group 'icomplete
180 (let ((on-p (if (null arg) 167 (if icomplete-mode
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 - 168 ;; The following is not really necessary after first time -
186 ;; no great loss. 169 ;; no great loss.
187 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))) 170 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
171 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
188 172
189 ;;;_ > icomplete-simple-completing-p () 173 ;;;_ > icomplete-simple-completing-p ()
190 (defun icomplete-simple-completing-p () 174 (defun icomplete-simple-completing-p ()
191 "Non-nil if current window is minibuffer that's doing simple completion. 175 "Non-nil if current window is minibuffer that's doing simple completion.
192 176
193 Conditions are: 177 Conditions are:
194 the selected window is a minibuffer, 178 the selected window is a minibuffer,
195 and not in the middle of macro execution, 179 and not in the middle of macro execution,
196 and minibuffer-completion-table is not a symbol (which would 180 and `minibuffer-completion-table' is not a symbol (which would
197 indicate some non-standard, non-simple completion mechanism, 181 indicate some non-standard, non-simple completion mechanism,
198 like file-name and other custom-func completions)." 182 like file-name and other custom-func completions)."
199 183
200 (and (window-minibuffer-p (selected-window)) 184 (and (window-minibuffer-p (selected-window))
201 (not executing-kbd-macro) 185 (not executing-kbd-macro)
202 (not (symbolp minibuffer-completion-table)))) 186 ;; (or minibuffer-completing-file-name
187 (not (functionp minibuffer-completion-table)))) ;; )
203 188
204 ;;;_ > icomplete-minibuffer-setup () 189 ;;;_ > icomplete-minibuffer-setup ()
205 ;;;###autoload
206 (defun icomplete-minibuffer-setup () 190 (defun icomplete-minibuffer-setup ()
207 "Run in minibuffer on activation to establish incremental completion. 191 "Run in minibuffer on activation to establish incremental completion.
208 Usually run by inclusion in `minibuffer-setup-hook'." 192 Usually run by inclusion in `minibuffer-setup-hook'."
209 (cond ((and icomplete-mode (icomplete-simple-completing-p)) 193 (when (and icomplete-mode (icomplete-simple-completing-p))
210 (add-hook 'pre-command-hook 194 (add-hook 'pre-command-hook
211 (function (lambda () 195 (lambda () (run-hooks 'icomplete-pre-command-hook))
212 (run-hooks 'icomplete-pre-command-hook))) 196 nil t)
213 nil t) 197 (add-hook 'post-command-hook
214 (add-hook 'post-command-hook 198 (lambda () (run-hooks 'icomplete-post-command-hook))
215 (function (lambda () 199 nil t)
216 (run-hooks 'icomplete-post-command-hook))) 200 (run-hooks 'icomplete-minibuffer-setup-hook)))
217 nil t)
218 (run-hooks 'icomplete-minibuffer-setup-hook))))
219 ; 201 ;
220 202
221 203
222 ;;;_* Completion 204 ;;;_* Completion
223 205
224 ;;;_ > icomplete-tidy () 206 ;;;_ > icomplete-tidy ()
225 (defun icomplete-tidy () 207 (defun icomplete-tidy ()
226 "Remove completions display \(if any) prior to new user input. 208 "Remove completions display \(if any) prior to new user input.
227 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode' 209 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
228 and `minibuffer-setup-hook'." 210 and `minibuffer-setup-hook'."
229 (if (icomplete-simple-completing-p) 211 (when icomplete-eoinput
230 (if (and (boundp 'icomplete-eoinput) 212
231 icomplete-eoinput) 213 (unless (>= icomplete-eoinput (point-max))
232 214 (let ((buffer-undo-list t)) ; prevent entry
233 (if (> icomplete-eoinput (point-max)) 215 (delete-region icomplete-eoinput (point-max))))
234 ;; Oops, got rug pulled out from under us - reinit: 216
235 (setq icomplete-eoinput (point-max)) 217 ;; Reestablish the safe value.
236 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry 218 (setq icomplete-eoinput nil)))
237 (delete-region icomplete-eoinput (point-max))))
238
239 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
240 (make-local-variable 'icomplete-eoinput)
241 (setq icomplete-eoinput 1))))
242 219
243 ;;;_ > icomplete-exhibit () 220 ;;;_ > icomplete-exhibit ()
244 (defun icomplete-exhibit () 221 (defun icomplete-exhibit ()
245 "Insert icomplete completions display. 222 "Insert icomplete completions display.
246 Should be run via minibuffer `post-command-hook'. See `icomplete-mode' 223 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
247 and `minibuffer-setup-hook'." 224 and `minibuffer-setup-hook'."
248 (if (icomplete-simple-completing-p) 225 (when (icomplete-simple-completing-p)
249 (let ((contents (buffer-substring (minibuffer-prompt-end)(point-max))) 226 (save-excursion
250 (buffer-undo-list t)) 227 (goto-char (point-max))
251 (save-excursion 228 ;; Register the end of input, so we know where the extra stuff
252 (goto-char (point-max)) 229 ;; (match-status info) begins:
253 ; Register the end of input, so we 230 (setq icomplete-eoinput (point))
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: 231 ; Insert the match-status information:
261 (if (and (> (point-max) (minibuffer-prompt-end)) 232 (if (and (> (point-max) (minibuffer-prompt-end))
262 (or 233 buffer-undo-list ; Wait for some user input.
263 ;; Don't bother with delay after certain number of chars: 234 (or
264 (> (point-max) icomplete-max-delay-chars) 235 ;; Don't bother with delay after certain number of chars:
265 ;; Don't delay if alternatives number is small enough: 236 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
266 (if minibuffer-completion-table 237 ;; Don't delay if alternatives number is small enough:
267 (cond ((numberp minibuffer-completion-table) 238 (and (sequencep minibuffer-completion-table)
268 (< minibuffer-completion-table 239 (< (length minibuffer-completion-table)
269 icomplete-delay-completions-threshold)) 240 icomplete-delay-completions-threshold))
270 ((sequencep minibuffer-completion-table) 241 ;; Delay - give some grace time for next keystroke, before
271 (< (length minibuffer-completion-table) 242 ;; embarking on computing completions:
272 icomplete-delay-completions-threshold)) 243 (sit-for icomplete-compute-delay)))
273 )) 244 (let ((text (while-no-input
274 ;; Delay - give some grace time for next keystroke, before 245 (icomplete-completions
275 ;; embarking on computing completions: 246 (field-string)
276 (sit-for icomplete-compute-delay))) 247 minibuffer-completion-table
277 (insert 248 minibuffer-completion-predicate
278 (icomplete-completions contents 249 (not minibuffer-completion-confirm))))
279 minibuffer-completion-table 250 (buffer-undo-list t))
280 minibuffer-completion-predicate 251 (if text (insert text)))))))
281 (not
282 minibuffer-completion-confirm))))))))
283 252
284 ;;;_ > icomplete-completions (name candidates predicate require-match) 253 ;;;_ > icomplete-completions (name candidates predicate require-match)
285 (defun icomplete-completions (name candidates predicate require-match) 254 (defun icomplete-completions (name candidates predicate require-match)
286 "Identify prospective candidates for minibuffer completion. 255 "Identify prospective candidates for minibuffer completion.
287 256
320 (most-len (length most)) 289 (most-len (length most))
321 (determ (and (> most-len (length name)) 290 (determ (and (> most-len (length name))
322 (concat open-bracket-determined 291 (concat open-bracket-determined
323 (substring most (length name)) 292 (substring most (length name))
324 close-bracket-determined))) 293 close-bracket-determined)))
325 (open-bracket-prospects "{") 294 ;;"-prospects" - more than one candidate
326 (close-bracket-prospects "}")
327 ;"-prospects" - more than one candidate
328 (prospects-len 0) 295 (prospects-len 0)
329 prospects most-is-exact comp) 296 prospects most-is-exact comp)
330 (if (eq most-try t) 297 (if (eq most-try t)
331 (setq prospects nil) 298 (setq prospects nil)
332 (while (and comps (< prospects-len icomplete-prospects-length)) 299 (while (and comps (< prospects-len icomplete-prospects-length))
336 ((member comp prospects)) 303 ((member comp prospects))
337 (t (setq prospects (cons comp prospects) 304 (t (setq prospects (cons comp prospects)
338 prospects-len (+ (length comp) 1 prospects-len)))))) 305 prospects-len (+ (length comp) 1 prospects-len))))))
339 (if prospects 306 (if prospects
340 (concat determ 307 (concat determ
341 open-bracket-prospects 308 "{"
342 (and most-is-exact ",") 309 (and most-is-exact ",")
343 (mapconcat 'identity 310 (mapconcat 'identity
344 (sort prospects (function string-lessp)) 311 (sort prospects (function string-lessp))
345 ",") 312 ",")
346 (and comps ",...") 313 (and comps ",...")
347 close-bracket-prospects) 314 "}")
348 (concat determ 315 (concat determ
349 " [Matched" 316 " [Matched"
350 (let ((keys (and icomplete-show-key-bindings 317 (let ((keys (and icomplete-show-key-bindings
351 (commandp (intern-soft most)) 318 (commandp (intern-soft most))
352 (icomplete-get-keys most)))) 319 (icomplete-get-keys most))))
353 (if keys 320 (if keys (concat "; " keys) ""))
354 (concat "; " keys)
355 ""))
356 "]")))))) 321 "]"))))))
357
358 (if icomplete-mode
359 (icomplete-mode 1))
360 322
361 ;;;_* Local emacs vars. 323 ;;;_* Local emacs vars.
362 ;;;Local variables: 324 ;;;Local variables:
363 ;;;outline-layout: (-2 :) 325 ;;;outline-layout: (-2 :)
364 ;;;End: 326 ;;;End:
365 327
366 ;;; arch-tag: 339ec25a-0741-4eb6-be63-997532e89b0f 328 ;; arch-tag: 339ec25a-0741-4eb6-be63-997532e89b0f
367 ;;; icomplete.el ends here 329 ;;; icomplete.el ends here