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 | ||
cc37e70f J |
74 | (defcustom icomplete-separator " | " |
75 | "String used by icomplete to separate alternatives in the minibuffer." | |
76 | :type 'string | |
77 | :version "24.3") | |
78 | ||
1934dbf4 | 79 | ;;;_* User Customization variables |
c2d0b538 SM |
80 | (defcustom icomplete-prospects-height |
81 | ;; 20 is an estimated common size for the prompt + minibuffer content, to | |
82 | ;; try to guess the number of lines used up by icomplete-prospects-length. | |
83 | (+ 1 (/ (+ icomplete-prospects-length 20) (window-width))) | |
84 | "Maximum number of lines to use in the minibuffer." | |
dfb4dab1 | 85 | :type 'integer |
af09cfd7 JB |
86 | :group 'icomplete |
87 | :version "23.1") | |
1934dbf4 | 88 | |
5e537651 | 89 | (defcustom icomplete-compute-delay .3 |
077aec27 JB |
90 | "Completions-computation stall, used only with large-number completions. |
91 | See `icomplete-delay-completions-threshold'." | |
5e537651 SE |
92 | :type 'number |
93 | :group 'icomplete) | |
94 | ||
95 | (defcustom icomplete-delay-completions-threshold 400 | |
077aec27 | 96 | "Pending-completions number over which to apply `icomplete-compute-delay'." |
5e537651 SE |
97 | :type 'integer |
98 | :group 'icomplete) | |
99 | ||
100 | (defcustom icomplete-max-delay-chars 3 | |
077aec27 | 101 | "Maximum number of initial chars to apply icomplete compute delay." |
5e537651 SE |
102 | :type 'integer |
103 | :group 'icomplete) | |
104 | ||
5e537651 | 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 | ||
397e713b CY |
148 | ;;;_ = icomplete-with-completion-tables |
149 | (defvar icomplete-with-completion-tables '(internal-complete-buffer) | |
150 | "Specialized completion tables with which icomplete should operate. | |
151 | ||
152 | Icomplete does not operate with any specialized completion tables | |
153 | except those on this list.") | |
92f7d003 | 154 | |
cc37e70f J |
155 | (defvar icomplete-minibuffer-map |
156 | (let ((map (make-sparse-keymap))) | |
157 | (define-key map [?\M-\t] 'minibuffer-force-complete) | |
158 | (define-key map [?\C-j] 'minibuffer-force-complete-and-exit) | |
159 | (define-key map [?\C-s] 'icomplete-forward-completions) | |
160 | (define-key map [?\C-r] 'icomplete-backward-completions) | |
161 | map)) | |
162 | ||
163 | (defun icomplete-forward-completions () | |
164 | "Step forward completions by one entry. | |
165 | Second entry becomes the first and can be selected with | |
166 | `minibuffer-force-complete-and-exit'." | |
167 | (interactive) | |
168 | (let* ((comps (completion-all-sorted-completions)) | |
169 | (last (last comps))) | |
170 | (setcdr last (cons (car comps) (cdr last))) | |
171 | (completion--cache-all-sorted-completions (cdr comps)))) | |
172 | ||
173 | (defun icomplete-backward-completions () | |
174 | "Step backward completions by one entry. | |
175 | Last entry becomes the first and can be selected with | |
176 | `minibuffer-force-complete-and-exit'." | |
177 | (interactive) | |
178 | (let* ((comps (completion-all-sorted-completions)) | |
179 | (last-but-one (last comps 2)) | |
180 | (last (cdr last-but-one))) | |
181 | (when last | |
182 | (setcdr last-but-one (cdr last)) | |
183 | (push (car last) comps) | |
184 | (completion--cache-all-sorted-completions comps)))) | |
185 | ||
239c87a1 | 186 | ;;;_ > icomplete-mode (&optional prefix) |
c89164c5 | 187 | ;;;###autoload |
47fda8fc | 188 | (define-minor-mode icomplete-mode |
06e21633 CY |
189 | "Toggle incremental minibuffer completion (Icomplete mode). |
190 | With a prefix argument ARG, enable Icomplete mode if ARG is | |
191 | positive, and disable it otherwise. If called from Lisp, enable | |
192 | the mode if ARG is omitted or nil." | |
47fda8fc SM |
193 | :global t :group 'icomplete |
194 | (if icomplete-mode | |
4b404c58 GM |
195 | ;; The following is not really necessary after first time - |
196 | ;; no great loss. | |
2176854d JB |
197 | (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup) |
198 | (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup))) | |
239c87a1 RS |
199 | |
200 | ;;;_ > icomplete-simple-completing-p () | |
201 | (defun icomplete-simple-completing-p () | |
239c87a1 RS |
202 | "Non-nil if current window is minibuffer that's doing simple completion. |
203 | ||
204 | Conditions are: | |
205 | the selected window is a minibuffer, | |
206 | and not in the middle of macro execution, | |
47fda8fc | 207 | and `minibuffer-completion-table' is not a symbol (which would |
81276211 | 208 | indicate some non-standard, non-simple completion mechanism, |
239c87a1 RS |
209 | like file-name and other custom-func completions)." |
210 | ||
211 | (and (window-minibuffer-p (selected-window)) | |
efcf38c7 | 212 | (not executing-kbd-macro) |
c22e3cd4 | 213 | minibuffer-completion-table |
397e713b | 214 | (or (not (functionp minibuffer-completion-table)) |
476d2aef | 215 | (eq icomplete-with-completion-tables t) |
397e713b | 216 | (member minibuffer-completion-table |
3f56acf9 | 217 | icomplete-with-completion-tables)))) |
d183f322 | 218 | |
239c87a1 | 219 | ;;;_ > icomplete-minibuffer-setup () |
239c87a1 | 220 | (defun icomplete-minibuffer-setup () |
239c87a1 | 221 | "Run in minibuffer on activation to establish incremental completion. |
d183f322 | 222 | Usually run by inclusion in `minibuffer-setup-hook'." |
47fda8fc | 223 | (when (and icomplete-mode (icomplete-simple-completing-p)) |
2176854d | 224 | (set (make-local-variable 'completion-show-inline-help) nil) |
cc37e70f J |
225 | (use-local-map (make-composed-keymap icomplete-minibuffer-map |
226 | (current-local-map))) | |
47fda8fc | 227 | (add-hook 'pre-command-hook |
f143bfe3 SM |
228 | (lambda () (let ((non-essential t)) |
229 | (run-hooks 'icomplete-pre-command-hook))) | |
47fda8fc SM |
230 | nil t) |
231 | (add-hook 'post-command-hook | |
f143bfe3 SM |
232 | (lambda () (let ((non-essential t)) ;E.g. don't prompt for password! |
233 | (run-hooks 'icomplete-post-command-hook))) | |
47fda8fc SM |
234 | nil t) |
235 | (run-hooks 'icomplete-minibuffer-setup-hook))) | |
1934dbf4 GM |
236 | ;\f |
237 | ||
238 | ||
239c87a1 RS |
239 | ;;;_* Completion |
240 | ||
241 | ;;;_ > icomplete-tidy () | |
242 | (defun icomplete-tidy () | |
243 | "Remove completions display \(if any) prior to new user input. | |
d183f322 | 244 | Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode' |
239c87a1 | 245 | and `minibuffer-setup-hook'." |
31d4b748 | 246 | (delete-overlay icomplete-overlay)) |
d183f322 | 247 | |
239c87a1 | 248 | ;;;_ > icomplete-exhibit () |
d462ff97 | 249 | (defun icomplete-exhibit () |
239c87a1 | 250 | "Insert icomplete completions display. |
d183f322 | 251 | Should be run via minibuffer `post-command-hook'. See `icomplete-mode' |
239c87a1 | 252 | and `minibuffer-setup-hook'." |
fe689fb6 | 253 | (when (and icomplete-mode (icomplete-simple-completing-p)) |
47fda8fc SM |
254 | (save-excursion |
255 | (goto-char (point-max)) | |
d462ff97 | 256 | ; Insert the match-status information: |
47fda8fc | 257 | (if (and (> (point-max) (minibuffer-prompt-end)) |
cc37e70f J |
258 | buffer-undo-list ; Wait for some user input. |
259 | (or | |
260 | ;; Don't bother with delay after certain number of chars: | |
261 | (> (- (point) (field-beginning)) icomplete-max-delay-chars) | |
262 | ;; Don't delay if the completions are known. | |
263 | completion-all-sorted-completions | |
264 | ;; Don't delay if alternatives number is small enough: | |
265 | (and (sequencep minibuffer-completion-table) | |
266 | (< (length minibuffer-completion-table) | |
267 | icomplete-delay-completions-threshold)) | |
268 | ;; Delay - give some grace time for next keystroke, before | |
47fda8fc SM |
269 | ;; embarking on computing completions: |
270 | (sit-for icomplete-compute-delay))) | |
271 | (let ((text (while-no-input | |
cc37e70f J |
272 | (icomplete-completions |
273 | (field-string) | |
274 | minibuffer-completion-table | |
275 | minibuffer-completion-predicate | |
31d4b748 | 276 | (not minibuffer-completion-confirm)))) |
7a78ffec EZ |
277 | (buffer-undo-list t) |
278 | deactivate-mark) | |
9a84ddd8 | 279 | ;; Do nothing if while-no-input was aborted. |
cc37e70f | 280 | (when (stringp text) |
31d4b748 SM |
281 | (move-overlay icomplete-overlay (point) (point) (current-buffer)) |
282 | ;; The current C cursor code doesn't know to use the overlay's | |
283 | ;; marker's stickiness to figure out whether to place the cursor | |
284 | ;; before or after the string, so let's spoon-feed it the pos. | |
285 | (put-text-property 0 1 'cursor t text) | |
286 | (overlay-put icomplete-overlay 'after-string text))))))) | |
d183f322 | 287 | |
239c87a1 RS |
288 | ;;;_ > icomplete-completions (name candidates predicate require-match) |
289 | (defun icomplete-completions (name candidates predicate require-match) | |
d462ff97 RS |
290 | "Identify prospective candidates for minibuffer completion. |
291 | ||
c89164c5 | 292 | The display is updated with each minibuffer keystroke during |
d462ff97 RS |
293 | minibuffer completion. |
294 | ||
295 | Prospective completion suffixes (if any) are displayed, bracketed by | |
296 | one of \(), \[], or \{} pairs. The choice of brackets is as follows: | |
297 | ||
298 | \(...) - a single prospect is identified and matching is enforced, | |
299 | \[...] - a single prospect is identified but matching is optional, or | |
300 | \{...} - multiple prospects, separated by commas, are indicated, and | |
81276211 | 301 | further input is required to distinguish a single one. |
d462ff97 | 302 | |
81276211 | 303 | The displays for unambiguous matches have ` [Matched]' appended |
d183f322 | 304 | \(whether complete or not), or ` \[No matches]', if no eligible |
1be5a284 | 305 | matches exist. \(Keybindings for uniquely matched commands |
92f7d003 KH |
306 | are exhibited within the square braces.)" |
307 | ||
f143bfe3 | 308 | (let* ((md (completion--field-metadata (field-beginning))) |
acd1f317 | 309 | (comps (completion-all-sorted-completions)) |
31d4b748 SM |
310 | (last (if (consp comps) (last comps))) |
311 | (base-size (cdr last)) | |
476d2aef SM |
312 | (open-bracket (if require-match "(" "[")) |
313 | (close-bracket (if require-match ")" "]"))) | |
c2d0b538 | 314 | ;; `concat'/`mapconcat' is the slow part. |
31d4b748 | 315 | (if (not (consp comps)) |
476d2aef | 316 | (format " %sNo matches%s" open-bracket close-bracket) |
31d4b748 | 317 | (if last (setcdr last nil)) |
be91065f SM |
318 | (let* ((most-try |
319 | (if (and base-size (> base-size 0)) | |
320 | (completion-try-completion | |
620c53a6 | 321 | name candidates predicate (length name) md) |
be91065f SM |
322 | ;; If the `comps' are 0-based, the result should be |
323 | ;; the same with `comps'. | |
324 | (completion-try-completion | |
620c53a6 | 325 | name comps nil (length name) md))) |
31d4b748 SM |
326 | (most (if (consp most-try) (car most-try) |
327 | (if most-try (car comps) ""))) | |
476d2aef SM |
328 | ;; Compare name and most, so we can determine if name is |
329 | ;; a prefix of most, or something else. | |
330 | (compare (compare-strings name nil nil | |
331 | most nil nil completion-ignore-case)) | |
332 | (determ (unless (or (eq t compare) (eq t most-try) | |
333 | (= (setq compare (1- (abs compare))) | |
334 | (length most))) | |
335 | (concat open-bracket | |
336 | (cond | |
337 | ((= compare (length name)) | |
338 | ;; Typical case: name is a prefix. | |
339 | (substring most compare)) | |
340 | ((< compare 5) most) | |
341 | (t (concat "..." (substring most compare)))) | |
342 | close-bracket))) | |
47fda8fc | 343 | ;;"-prospects" - more than one candidate |
c2d0b538 SM |
344 | (prospects-len (+ (length determ) 6 ;; take {,...} into account |
345 | (string-width (buffer-string)))) | |
346 | (prospects-max | |
347 | ;; Max total length to use, including the minibuffer content. | |
29f313d7 | 348 | (* (+ icomplete-prospects-height |
c2d0b538 SM |
349 | ;; If the minibuffer content already uses up more than |
350 | ;; one line, increase the allowable space accordingly. | |
351 | (/ prospects-len (window-width))) | |
352 | (window-width))) | |
476d2aef SM |
353 | (prefix-len |
354 | ;; Find the common prefix among `comps'. | |
8f0fde21 SM |
355 | ;; We can't use the optimization below because its assumptions |
356 | ;; aren't always true, e.g. when completion-cycling (bug#10850): | |
357 | ;; (if (eq t (compare-strings (car comps) nil (length most) | |
358 | ;; most nil nil completion-ignore-case)) | |
359 | ;; ;; Common case. | |
360 | ;; (length most) | |
361 | ;; Else, use try-completion. | |
362 | (let ((comps-prefix (try-completion "" comps))) | |
363 | (and (stringp comps-prefix) | |
364 | (length comps-prefix)))) ;;) | |
476d2aef | 365 | |
7d94fb17 | 366 | prospects most-is-exact comp limit) |
31d4b748 | 367 | (if (eq most-try t) ;; (or (null (cdr comps)) |
1934dbf4 | 368 | (setq prospects nil) |
7d94fb17 | 369 | (while (and comps (not limit)) |
476d2aef | 370 | (setq comp |
8f0fde21 | 371 | (if prefix-len (substring (car comps) prefix-len) (car comps)) |
1934dbf4 GM |
372 | comps (cdr comps)) |
373 | (cond ((string-equal comp "") (setq most-is-exact t)) | |
374 | ((member comp prospects)) | |
c2d0b538 SM |
375 | (t (setq prospects-len |
376 | (+ (string-width comp) 1 prospects-len)) | |
377 | (if (< prospects-len prospects-max) | |
31d4b748 | 378 | (push comp prospects) |
7d94fb17 | 379 | (setq limit t)))))) |
31d4b748 SM |
380 | ;; Restore the base-size info, since completion-all-sorted-completions |
381 | ;; is cached. | |
382 | (if last (setcdr last base-size)) | |
1934dbf4 GM |
383 | (if prospects |
384 | (concat determ | |
47fda8fc | 385 | "{" |
cc37e70f J |
386 | (and most-is-exact |
387 | (substring icomplete-separator | |
388 | (string-match "[^ ]" icomplete-separator))) | |
389 | (mapconcat 'identity (nreverse prospects) | |
390 | icomplete-separator) | |
391 | (and limit (concat icomplete-separator "…")) | |
47fda8fc | 392 | "}") |
cc37e70f | 393 | (concat determ " [Matched]")))))) |
d462ff97 | 394 | |
476d2aef SM |
395 | ;;_* Local emacs vars. |
396 | ;;Local variables: | |
397 | ;;allout-layout: (-2 :) | |
398 | ;;End: | |
d462ff97 RS |
399 | |
400 | ;;; icomplete.el ends here |