Commit | Line | Data |
---|---|---|
fa157191 | 1 | ;;; icomplete.el --- minibuffer completion incremental feedback |
239c87a1 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1992-1994, 1997, 1999, 2001-2012 |
95df8112 | 4 | ;; Free Software Foundation, Inc. |
d462ff97 | 5 | |
1934dbf4 GM |
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. | |
be010748 | 10 | ;; Keywords: help, abbrev |
d462ff97 | 11 | |
239c87a1 | 12 | ;; This file is part of GNU Emacs. |
d462ff97 | 13 | |
eb3fa2cf | 14 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
239c87a1 | 15 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
16 | ;; the Free Software Foundation, either version 3 of the License, or |
17 | ;; (at your option) any later version. | |
d462ff97 | 18 | |
239c87a1 RS |
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. | |
c89164c5 | 23 | |
239c87a1 | 24 | ;; You should have received a copy of the GNU General Public License |
eb3fa2cf | 25 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
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 | ||
5e537651 | 34 | ;; See `icomplete-completions' docstring for a description of the |
b578f267 EN |
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 | ||
1934dbf4 GM |
41 | ;; To activate icomplete mode, load the package and use the |
42 | ;; `icomplete-mode' function. You can subsequently deactivate it by | |
43 | ;; invoking the function icomplete-mode with a negative prefix-arg | |
44 | ;; (C-U -1 ESC-x icomplete-mode). Also, you can prevent activation of | |
45 | ;; the mode during package load by first setting the variable | |
46 | ;; `icomplete-mode' to nil. Icompletion can be enabled any time after | |
47 | ;; the package is loaded by invoking icomplete-mode without a prefix | |
48 | ;; arg. | |
92f7d003 | 49 | |
b578f267 EN |
50 | ;; Thanks to everyone for their suggestions for refinements of this |
51 | ;; package. I particularly have to credit Michael Cook, who | |
52 | ;; implemented an incremental completion style in his 'iswitch' | |
53 | ;; functions that served as a model for icomplete. Some other | |
5e537651 | 54 | ;; contributors: Noah Friedman (restructuring as minor mode), Colin |
1be5a284 | 55 | ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others. |
b578f267 EN |
56 | |
57 | ;; klm. | |
c89164c5 | 58 | |
239c87a1 RS |
59 | ;;; Code: |
60 | ||
61 | ;;;_* Provide | |
d462ff97 RS |
62 | (provide 'icomplete) |
63 | ||
5e537651 SE |
64 | |
65 | (defgroup icomplete nil | |
66 | "Show completions dynamically in minibuffer." | |
67 | :prefix "icomplete-" | |
68 | :group 'minibuffer) | |
69 | ||
c2d0b538 SM |
70 | (defvar icomplete-prospects-length 80) |
71 | (make-obsolete-variable | |
72 | 'icomplete-prospects-length 'icomplete-prospects-height "23.1") | |
73 | ||
1934dbf4 | 74 | ;;;_* User Customization variables |
c2d0b538 SM |
75 | (defcustom icomplete-prospects-height |
76 | ;; 20 is an estimated common size for the prompt + minibuffer content, to | |
77 | ;; try to guess the number of lines used up by icomplete-prospects-length. | |
78 | (+ 1 (/ (+ icomplete-prospects-length 20) (window-width))) | |
79 | "Maximum number of lines to use in the minibuffer." | |
dfb4dab1 | 80 | :type 'integer |
af09cfd7 JB |
81 | :group 'icomplete |
82 | :version "23.1") | |
1934dbf4 | 83 | |
5e537651 | 84 | (defcustom icomplete-compute-delay .3 |
077aec27 JB |
85 | "Completions-computation stall, used only with large-number completions. |
86 | See `icomplete-delay-completions-threshold'." | |
5e537651 SE |
87 | :type 'number |
88 | :group 'icomplete) | |
89 | ||
90 | (defcustom icomplete-delay-completions-threshold 400 | |
077aec27 | 91 | "Pending-completions number over which to apply `icomplete-compute-delay'." |
5e537651 SE |
92 | :type 'integer |
93 | :group 'icomplete) | |
94 | ||
95 | (defcustom icomplete-max-delay-chars 3 | |
077aec27 | 96 | "Maximum number of initial chars to apply icomplete compute delay." |
5e537651 SE |
97 | :type 'integer |
98 | :group 'icomplete) | |
99 | ||
100 | (defcustom icomplete-show-key-bindings t | |
077aec27 | 101 | "If non-nil, show key bindings as well as completion for sole matches." |
5e537651 SE |
102 | :type 'boolean |
103 | :group 'icomplete) | |
104 | ||
105 | (defcustom icomplete-minibuffer-setup-hook nil | |
077aec27 | 106 | "Icomplete-specific customization of minibuffer setup. |
239c87a1 | 107 | |
4837b516 | 108 | This hook is run during minibuffer setup if icomplete is active. |
239c87a1 | 109 | It is intended for use in customizing icomplete for interoperation |
9c8acefd | 110 | with other features and packages. For instance: |
239c87a1 | 111 | |
92f7d003 | 112 | \(add-hook 'icomplete-minibuffer-setup-hook |
239c87a1 RS |
113 | \(function |
114 | \(lambda () | |
9c8acefd EZ |
115 | \(make-local-variable 'max-mini-window-height) |
116 | \(setq max-mini-window-height 3)))) | |
239c87a1 | 117 | |
9c8acefd | 118 | will constrain Emacs to a maximum minibuffer height of 3 lines when |
5e537651 SE |
119 | icompletion is occurring." |
120 | :type 'hook | |
121 | :group 'icomplete) | |
122 | ||
123 | ||
124 | ;;;_* Initialization | |
d462ff97 | 125 | |
c89164c5 | 126 | ;;;_ + Internal Variables |
47fda8fc | 127 | ;;;_ = icomplete-eoinput nil |
31d4b748 SM |
128 | (defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t) |
129 | "Overlay used to display the list of completions.") | |
130 | ||
239c87a1 RS |
131 | ;;;_ = icomplete-pre-command-hook |
132 | (defvar icomplete-pre-command-hook nil | |
133 | "Incremental-minibuffer-completion pre-command-hook. | |
134 | ||
135 | Is run in minibuffer before user input when `icomplete-mode' is non-nil. | |
136 | Use `icomplete-mode' function to set it up properly for incremental | |
137 | minibuffer completion.") | |
138 | (add-hook 'icomplete-pre-command-hook 'icomplete-tidy) | |
139 | ;;;_ = icomplete-post-command-hook | |
140 | (defvar icomplete-post-command-hook nil | |
141 | "Incremental-minibuffer-completion post-command-hook. | |
142 | ||
143 | Is run in minibuffer after user input when `icomplete-mode' is non-nil. | |
144 | Use `icomplete-mode' function to set it up properly for incremental | |
145 | minibuffer completion.") | |
146 | (add-hook 'icomplete-post-command-hook 'icomplete-exhibit) | |
147 | ||
92f7d003 | 148 | (defun icomplete-get-keys (func-name) |
8793dabb | 149 | "Return strings naming keys bound to FUNC-NAME, or nil if none. |
1be5a284 RS |
150 | Examines the prior, not current, buffer, presuming that current buffer |
151 | is minibuffer." | |
8793dabb | 152 | (when (commandp func-name) |
92f7d003 KH |
153 | (save-excursion |
154 | (let* ((sym (intern func-name)) | |
78421c58 | 155 | (buf (other-buffer nil t)) |
4afd234b | 156 | (keys (with-current-buffer buf (where-is-internal sym)))) |
8793dabb JB |
157 | (when keys |
158 | (concat "<" | |
159 | (mapconcat 'key-description | |
160 | (sort keys | |
161 | #'(lambda (x y) | |
162 | (< (length x) (length y)))) | |
163 | ", ") | |
164 | ">")))))) | |
397e713b CY |
165 | ;;;_ = icomplete-with-completion-tables |
166 | (defvar icomplete-with-completion-tables '(internal-complete-buffer) | |
167 | "Specialized completion tables with which icomplete should operate. | |
168 | ||
169 | Icomplete does not operate with any specialized completion tables | |
170 | except those on this list.") | |
92f7d003 | 171 | |
239c87a1 | 172 | ;;;_ > icomplete-mode (&optional prefix) |
c89164c5 | 173 | ;;;###autoload |
47fda8fc | 174 | (define-minor-mode icomplete-mode |
06e21633 CY |
175 | "Toggle incremental minibuffer completion (Icomplete mode). |
176 | With a prefix argument ARG, enable Icomplete mode if ARG is | |
177 | positive, and disable it otherwise. If called from Lisp, enable | |
178 | the mode if ARG is omitted or nil." | |
47fda8fc SM |
179 | :global t :group 'icomplete |
180 | (if icomplete-mode | |
4b404c58 GM |
181 | ;; The following is not really necessary after first time - |
182 | ;; no great loss. | |
2176854d JB |
183 | (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup) |
184 | (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup))) | |
239c87a1 RS |
185 | |
186 | ;;;_ > icomplete-simple-completing-p () | |
187 | (defun icomplete-simple-completing-p () | |
239c87a1 RS |
188 | "Non-nil if current window is minibuffer that's doing simple completion. |
189 | ||
190 | Conditions are: | |
191 | the selected window is a minibuffer, | |
192 | and not in the middle of macro execution, | |
47fda8fc | 193 | and `minibuffer-completion-table' is not a symbol (which would |
81276211 | 194 | indicate some non-standard, non-simple completion mechanism, |
239c87a1 RS |
195 | like file-name and other custom-func completions)." |
196 | ||
197 | (and (window-minibuffer-p (selected-window)) | |
efcf38c7 | 198 | (not executing-kbd-macro) |
c22e3cd4 | 199 | minibuffer-completion-table |
397e713b | 200 | (or (not (functionp minibuffer-completion-table)) |
476d2aef | 201 | (eq icomplete-with-completion-tables t) |
397e713b | 202 | (member minibuffer-completion-table |
3f56acf9 | 203 | icomplete-with-completion-tables)))) |
d183f322 | 204 | |
239c87a1 | 205 | ;;;_ > icomplete-minibuffer-setup () |
239c87a1 | 206 | (defun icomplete-minibuffer-setup () |
239c87a1 | 207 | "Run in minibuffer on activation to establish incremental completion. |
d183f322 | 208 | Usually run by inclusion in `minibuffer-setup-hook'." |
47fda8fc | 209 | (when (and icomplete-mode (icomplete-simple-completing-p)) |
2176854d | 210 | (set (make-local-variable 'completion-show-inline-help) nil) |
47fda8fc SM |
211 | (add-hook 'pre-command-hook |
212 | (lambda () (run-hooks 'icomplete-pre-command-hook)) | |
213 | nil t) | |
214 | (add-hook 'post-command-hook | |
215 | (lambda () (run-hooks 'icomplete-post-command-hook)) | |
216 | nil t) | |
217 | (run-hooks 'icomplete-minibuffer-setup-hook))) | |
1934dbf4 GM |
218 | ;\f |
219 | ||
220 | ||
239c87a1 RS |
221 | ;;;_* Completion |
222 | ||
223 | ;;;_ > icomplete-tidy () | |
224 | (defun icomplete-tidy () | |
225 | "Remove completions display \(if any) prior to new user input. | |
d183f322 | 226 | Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode' |
239c87a1 | 227 | and `minibuffer-setup-hook'." |
31d4b748 | 228 | (delete-overlay icomplete-overlay)) |
d183f322 | 229 | |
239c87a1 | 230 | ;;;_ > icomplete-exhibit () |
d462ff97 | 231 | (defun icomplete-exhibit () |
239c87a1 | 232 | "Insert icomplete completions display. |
d183f322 | 233 | Should be run via minibuffer `post-command-hook'. See `icomplete-mode' |
239c87a1 | 234 | and `minibuffer-setup-hook'." |
fe689fb6 | 235 | (when (and icomplete-mode (icomplete-simple-completing-p)) |
47fda8fc SM |
236 | (save-excursion |
237 | (goto-char (point-max)) | |
d462ff97 | 238 | ; Insert the match-status information: |
47fda8fc SM |
239 | (if (and (> (point-max) (minibuffer-prompt-end)) |
240 | buffer-undo-list ; Wait for some user input. | |
241 | (or | |
242 | ;; Don't bother with delay after certain number of chars: | |
243 | (> (- (point) (field-beginning)) icomplete-max-delay-chars) | |
244 | ;; Don't delay if alternatives number is small enough: | |
245 | (and (sequencep minibuffer-completion-table) | |
246 | (< (length minibuffer-completion-table) | |
247 | icomplete-delay-completions-threshold)) | |
248 | ;; Delay - give some grace time for next keystroke, before | |
249 | ;; embarking on computing completions: | |
250 | (sit-for icomplete-compute-delay))) | |
251 | (let ((text (while-no-input | |
9a84ddd8 RS |
252 | (icomplete-completions |
253 | (field-string) | |
254 | minibuffer-completion-table | |
255 | minibuffer-completion-predicate | |
31d4b748 | 256 | (not minibuffer-completion-confirm)))) |
7a78ffec EZ |
257 | (buffer-undo-list t) |
258 | deactivate-mark) | |
9a84ddd8 | 259 | ;; Do nothing if while-no-input was aborted. |
31d4b748 SM |
260 | (when (stringp text) |
261 | (move-overlay icomplete-overlay (point) (point) (current-buffer)) | |
262 | ;; The current C cursor code doesn't know to use the overlay's | |
263 | ;; marker's stickiness to figure out whether to place the cursor | |
264 | ;; before or after the string, so let's spoon-feed it the pos. | |
265 | (put-text-property 0 1 'cursor t text) | |
266 | (overlay-put icomplete-overlay 'after-string text))))))) | |
d183f322 | 267 | |
239c87a1 RS |
268 | ;;;_ > icomplete-completions (name candidates predicate require-match) |
269 | (defun icomplete-completions (name candidates predicate require-match) | |
d462ff97 RS |
270 | "Identify prospective candidates for minibuffer completion. |
271 | ||
c89164c5 | 272 | The display is updated with each minibuffer keystroke during |
d462ff97 RS |
273 | minibuffer completion. |
274 | ||
275 | Prospective completion suffixes (if any) are displayed, bracketed by | |
276 | one of \(), \[], or \{} pairs. The choice of brackets is as follows: | |
277 | ||
278 | \(...) - a single prospect is identified and matching is enforced, | |
279 | \[...] - a single prospect is identified but matching is optional, or | |
280 | \{...} - multiple prospects, separated by commas, are indicated, and | |
81276211 | 281 | further input is required to distinguish a single one. |
d462ff97 | 282 | |
81276211 | 283 | The displays for unambiguous matches have ` [Matched]' appended |
d183f322 | 284 | \(whether complete or not), or ` \[No matches]', if no eligible |
1be5a284 | 285 | matches exist. \(Keybindings for uniquely matched commands |
92f7d003 KH |
286 | are exhibited within the square braces.)" |
287 | ||
acd1f317 | 288 | (let* ((non-essential t) |
620c53a6 | 289 | (md (completion--field-metadata (field-beginning))) |
acd1f317 | 290 | (comps (completion-all-sorted-completions)) |
31d4b748 SM |
291 | (last (if (consp comps) (last comps))) |
292 | (base-size (cdr last)) | |
476d2aef SM |
293 | (open-bracket (if require-match "(" "[")) |
294 | (close-bracket (if require-match ")" "]"))) | |
c2d0b538 | 295 | ;; `concat'/`mapconcat' is the slow part. |
31d4b748 | 296 | (if (not (consp comps)) |
476d2aef | 297 | (format " %sNo matches%s" open-bracket close-bracket) |
31d4b748 | 298 | (if last (setcdr last nil)) |
be91065f SM |
299 | (let* ((most-try |
300 | (if (and base-size (> base-size 0)) | |
301 | (completion-try-completion | |
620c53a6 | 302 | name candidates predicate (length name) md) |
be91065f SM |
303 | ;; If the `comps' are 0-based, the result should be |
304 | ;; the same with `comps'. | |
305 | (completion-try-completion | |
620c53a6 | 306 | name comps nil (length name) md))) |
31d4b748 SM |
307 | (most (if (consp most-try) (car most-try) |
308 | (if most-try (car comps) ""))) | |
476d2aef SM |
309 | ;; Compare name and most, so we can determine if name is |
310 | ;; a prefix of most, or something else. | |
311 | (compare (compare-strings name nil nil | |
312 | most nil nil completion-ignore-case)) | |
313 | (determ (unless (or (eq t compare) (eq t most-try) | |
314 | (= (setq compare (1- (abs compare))) | |
315 | (length most))) | |
316 | (concat open-bracket | |
317 | (cond | |
318 | ((= compare (length name)) | |
319 | ;; Typical case: name is a prefix. | |
320 | (substring most compare)) | |
321 | ((< compare 5) most) | |
322 | (t (concat "..." (substring most compare)))) | |
323 | close-bracket))) | |
47fda8fc | 324 | ;;"-prospects" - more than one candidate |
c2d0b538 SM |
325 | (prospects-len (+ (length determ) 6 ;; take {,...} into account |
326 | (string-width (buffer-string)))) | |
327 | (prospects-max | |
328 | ;; Max total length to use, including the minibuffer content. | |
29f313d7 | 329 | (* (+ icomplete-prospects-height |
c2d0b538 SM |
330 | ;; If the minibuffer content already uses up more than |
331 | ;; one line, increase the allowable space accordingly. | |
332 | (/ prospects-len (window-width))) | |
333 | (window-width))) | |
476d2aef SM |
334 | (prefix-len |
335 | ;; Find the common prefix among `comps'. | |
336 | (if (eq t (compare-strings (car comps) nil (length most) | |
31d4b748 | 337 | most nil nil completion-ignore-case)) |
476d2aef SM |
338 | ;; Common case. |
339 | (length most) | |
340 | ;; Else, use try-completion. | |
341 | (let ((comps-prefix (try-completion "" comps))) | |
342 | (and (stringp comps-prefix) | |
343 | (length comps-prefix))))) | |
344 | ||
7d94fb17 | 345 | prospects most-is-exact comp limit) |
31d4b748 | 346 | (if (eq most-try t) ;; (or (null (cdr comps)) |
1934dbf4 | 347 | (setq prospects nil) |
7d94fb17 | 348 | (while (and comps (not limit)) |
476d2aef | 349 | (setq comp |
ab1ce9d7 LL |
350 | (if (and prefix-len (<= prefix-len (length (car comps)))) |
351 | (substring (car comps) prefix-len) | |
352 | (car comps)) | |
1934dbf4 GM |
353 | comps (cdr comps)) |
354 | (cond ((string-equal comp "") (setq most-is-exact t)) | |
355 | ((member comp prospects)) | |
c2d0b538 SM |
356 | (t (setq prospects-len |
357 | (+ (string-width comp) 1 prospects-len)) | |
358 | (if (< prospects-len prospects-max) | |
31d4b748 | 359 | (push comp prospects) |
7d94fb17 | 360 | (setq limit t)))))) |
31d4b748 SM |
361 | ;; Restore the base-size info, since completion-all-sorted-completions |
362 | ;; is cached. | |
363 | (if last (setcdr last base-size)) | |
1934dbf4 GM |
364 | (if prospects |
365 | (concat determ | |
47fda8fc | 366 | "{" |
1934dbf4 | 367 | (and most-is-exact ",") |
31d4b748 SM |
368 | (mapconcat 'identity (nreverse prospects) ",") |
369 | (and limit ",...") | |
47fda8fc | 370 | "}") |
1934dbf4 GM |
371 | (concat determ |
372 | " [Matched" | |
373 | (let ((keys (and icomplete-show-key-bindings | |
374 | (commandp (intern-soft most)) | |
375 | (icomplete-get-keys most)))) | |
47fda8fc | 376 | (if keys (concat "; " keys) "")) |
1934dbf4 | 377 | "]")))))) |
d462ff97 | 378 | |
476d2aef SM |
379 | ;;_* Local emacs vars. |
380 | ;;Local variables: | |
381 | ;;allout-layout: (-2 :) | |
382 | ;;End: | |
d462ff97 RS |
383 | |
384 | ;;; icomplete.el ends here |