remove sigio blocking
[bpt/emacs.git] / lisp / icomplete.el
CommitLineData
fa157191 1;;; icomplete.el --- minibuffer completion incremental feedback
239c87a1 2
a8cb4247
GM
3;; Copyright (C) 1992-1994, 1997, 1999, 2001-2014
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
be010748 9;; Keywords: help, abbrev
d462ff97 10
239c87a1 11;; This file is part of GNU Emacs.
d462ff97 12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
239c87a1 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
d462ff97 17
239c87a1
RS
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
c89164c5 22
239c87a1 23;; You should have received a copy of the GNU General Public License
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
d462ff97 25
239c87a1 26;;; Commentary:
d462ff97 27
a8cb4247 28;; Enabling this package implements a more fine-grained minibuffer
b578f267
EN
29;; completion feedback scheme. Prospective completions are concisely
30;; indicated within the minibuffer itself, with each successive
31;; keystroke.
32
5e537651 33;; See `icomplete-completions' docstring for a description of the
b578f267
EN
34;; icomplete display format.
35
36;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
37;; customize icomplete setup for interoperation with other
38;; minibuffer-oriented packages.
39
a8cb4247 40;; To enable/disable icomplete mode, use the `icomplete-mode' function.
92f7d003 41
b578f267
EN
42;; Thanks to everyone for their suggestions for refinements of this
43;; package. I particularly have to credit Michael Cook, who
44;; implemented an incremental completion style in his 'iswitch'
45;; functions that served as a model for icomplete. Some other
5e537651 46;; contributors: Noah Friedman (restructuring as minor mode), Colin
1be5a284 47;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
b578f267
EN
48
49;; klm.
c89164c5 50
239c87a1
RS
51;;; Code:
52
5e537651
SE
53(defgroup icomplete nil
54 "Show completions dynamically in minibuffer."
55 :prefix "icomplete-"
a8cb4247 56 :link '(info-link "(emacs)Icomplete")
5e537651
SE
57 :group 'minibuffer)
58
c2d0b538
SM
59(defvar icomplete-prospects-length 80)
60(make-obsolete-variable
61 'icomplete-prospects-length 'icomplete-prospects-height "23.1")
62
cc37e70f 63(defcustom icomplete-separator " | "
d11320e5 64 "String used by Icomplete to separate alternatives in the minibuffer."
cc37e70f 65 :type 'string
fb3bf6ce 66 :version "24.4")
cc37e70f 67
5cbfe5b9
J
68(defcustom icomplete-hide-common-prefix t
69 "When non-nil, hide common prefix from completion candidates.
70When nil, show candidates in full."
71 :type 'boolean
d64b6308
DC
72 :version "24.4")
73
a4151f3b 74(defcustom icomplete-show-matches-on-no-input nil
d64b6308
DC
75 "When non-nil, show completions when first prompting for input."
76 :type 'boolean
77 :version "24.4")
5cbfe5b9 78
a8cb4247 79(defcustom icomplete-with-completion-tables t
d11320e5 80 "Specialized completion tables with which Icomplete should operate.
a8cb4247
GM
81If this is t, Icomplete operates on all tables.
82Otherwise this should be a list of the completion tables (e.g.,
83`internal-complete-buffer') on which Icomplete should operate."
84 ;; Prior to 24.4, not a user-option, default '(internal-complete-buffer).
85 :version "24.4"
86 :type '(choice (const :tag "All" t)
87 (repeat function)))
88
89(defface icomplete-first-match '((t :weight bold))
d11320e5 90 "Face used by Icomplete for highlighting first match."
d64b6308 91 :version "24.4")
5cbfe5b9 92
1934dbf4 93;;;_* User Customization variables
c2d0b538
SM
94(defcustom icomplete-prospects-height
95 ;; 20 is an estimated common size for the prompt + minibuffer content, to
96 ;; try to guess the number of lines used up by icomplete-prospects-length.
97 (+ 1 (/ (+ icomplete-prospects-length 20) (window-width)))
98 "Maximum number of lines to use in the minibuffer."
dfb4dab1 99 :type 'integer
af09cfd7 100 :version "23.1")
1934dbf4 101
5e537651 102(defcustom icomplete-compute-delay .3
077aec27
JB
103 "Completions-computation stall, used only with large-number completions.
104See `icomplete-delay-completions-threshold'."
d64b6308 105 :type 'number)
5e537651
SE
106
107(defcustom icomplete-delay-completions-threshold 400
077aec27 108 "Pending-completions number over which to apply `icomplete-compute-delay'."
d64b6308 109 :type 'integer)
5e537651
SE
110
111(defcustom icomplete-max-delay-chars 3
d11320e5 112 "Maximum number of initial chars to apply `icomplete-compute-delay'."
d64b6308 113 :type 'integer)
5e537651 114
504a0381
SM
115(defvar icomplete-in-buffer nil
116 "If non-nil, also use Icomplete when completing in non-mini buffers.")
117
5e537651 118(defcustom icomplete-minibuffer-setup-hook nil
077aec27 119 "Icomplete-specific customization of minibuffer setup.
239c87a1 120
d11320e5
JB
121This hook is run during minibuffer setup if Icomplete is active.
122It is intended for use in customizing Icomplete for interoperation
9c8acefd 123with other features and packages. For instance:
239c87a1 124
d11320e5
JB
125 (add-hook 'icomplete-minibuffer-setup-hook
126 (lambda () (setq-local max-mini-window-height 3)))
239c87a1 127
9c8acefd 128will constrain Emacs to a maximum minibuffer height of 3 lines when
5e537651
SE
129icompletion is occurring."
130 :type 'hook
131 :group 'icomplete)
132
133
134;;;_* Initialization
d462ff97 135
c89164c5 136;;;_ + Internal Variables
47fda8fc 137;;;_ = icomplete-eoinput nil
31d4b748
SM
138(defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
139 "Overlay used to display the list of completions.")
140
504a0381
SM
141(defun icomplete-pre-command-hook ()
142 (let ((non-essential t))
143 (icomplete-tidy)))
239c87a1 144
504a0381
SM
145(defun icomplete-post-command-hook ()
146 (let ((non-essential t)) ;E.g. don't prompt for password!
147 (icomplete-exhibit)))
239c87a1 148
cc37e70f
J
149(defvar icomplete-minibuffer-map
150 (let ((map (make-sparse-keymap)))
151 (define-key map [?\M-\t] 'minibuffer-force-complete)
152 (define-key map [?\C-j] 'minibuffer-force-complete-and-exit)
d7e76a89
J
153 (define-key map [?\C-.] 'icomplete-forward-completions)
154 (define-key map [?\C-,] 'icomplete-backward-completions)
a8cb4247
GM
155 map)
156 "Keymap used by `icomplete-mode' in the minibuffer.")
cc37e70f
J
157
158(defun icomplete-forward-completions ()
159 "Step forward completions by one entry.
160Second entry becomes the first and can be selected with
161`minibuffer-force-complete-and-exit'."
162 (interactive)
504a0381
SM
163 (let* ((beg (icomplete--field-beg))
164 (end (icomplete--field-end))
67982e2b 165 (comps (completion-all-sorted-completions beg end))
cc37e70f 166 (last (last comps)))
6130b96a
J
167 (when comps
168 (setcdr last (cons (car comps) (cdr last)))
67982e2b 169 (completion--cache-all-sorted-completions beg end (cdr comps)))))
cc37e70f
J
170
171(defun icomplete-backward-completions ()
172 "Step backward completions by one entry.
173Last entry becomes the first and can be selected with
174`minibuffer-force-complete-and-exit'."
175 (interactive)
504a0381
SM
176 (let* ((beg (icomplete--field-beg))
177 (end (icomplete--field-end))
67982e2b 178 (comps (completion-all-sorted-completions beg end))
cc37e70f
J
179 (last-but-one (last comps 2))
180 (last (cdr last-but-one)))
6130b96a 181 (when (consp last) ; At least two elements in comps
cc37e70f
J
182 (setcdr last-but-one (cdr last))
183 (push (car last) comps)
67982e2b 184 (completion--cache-all-sorted-completions beg end comps))))
cc37e70f 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).
190With a prefix argument ARG, enable Icomplete mode if ARG is
191positive, and disable it otherwise. If called from Lisp, enable
a8cb4247
GM
192the mode if ARG is omitted or nil.
193
194When this global minor mode is enabled, typing in the minibuffer
195continuously displays a list of possible completions that match
196the string you have typed. See `icomplete-completions' for a
197description of how prospective completions are displayed.
198
199For more information, see Info node `(emacs)Icomplete'.
d11320e5 200For options you can set, `\\[customize-group] icomplete'.
a8cb4247
GM
201
202You can use the following key bindings to navigate and select
203completions:
204
205\\{icomplete-minibuffer-map}"
47fda8fc 206 :global t :group 'icomplete
504a0381
SM
207 (remove-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)
208 (remove-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup)
209 (when icomplete-mode
210 (when icomplete-in-buffer
211 (add-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup))
212 (add-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)))
213
214(defun icomplete--completion-table ()
215 (if (window-minibuffer-p) minibuffer-completion-table
216 (or (nth 2 completion-in-region--data)
217 (message "In %S (w=%S): %S"
218 (current-buffer) (selected-window) (window-minibuffer-p)))))
219(defun icomplete--completion-predicate ()
220 (if (window-minibuffer-p) minibuffer-completion-predicate
221 (nth 3 completion-in-region--data)))
222(defun icomplete--field-string ()
223 (if (window-minibuffer-p) (minibuffer-contents)
224 (buffer-substring-no-properties
225 (nth 0 completion-in-region--data)
226 (nth 1 completion-in-region--data))))
227(defun icomplete--field-beg ()
228 (if (window-minibuffer-p) (minibuffer-prompt-end)
229 (nth 0 completion-in-region--data)))
230(defun icomplete--field-end ()
231 (if (window-minibuffer-p) (point-max)
232 (nth 1 completion-in-region--data)))
239c87a1
RS
233
234;;;_ > icomplete-simple-completing-p ()
235(defun icomplete-simple-completing-p ()
a8cb4247 236 "Non-nil if current window is a minibuffer that's doing simple completion.
239c87a1
RS
237
238Conditions are:
239 the selected window is a minibuffer,
240 and not in the middle of macro execution,
504a0381 241 and the completion table is not a function (which would
81276211 242 indicate some non-standard, non-simple completion mechanism,
c359b7bd
LI
243 like file-name and other custom-func completions),
244 and `icomplete-with-completion-tables' doesn't restrict completion."
239c87a1 245
504a0381
SM
246 (unless executing-kbd-macro
247 (let ((table (icomplete--completion-table)))
248 (and table
249 (or (not (functionp table))
250 (eq icomplete-with-completion-tables t)
251 (member table icomplete-with-completion-tables))))))
d183f322 252
239c87a1 253;;;_ > icomplete-minibuffer-setup ()
239c87a1 254(defun icomplete-minibuffer-setup ()
239c87a1 255 "Run in minibuffer on activation to establish incremental completion.
d183f322 256Usually run by inclusion in `minibuffer-setup-hook'."
47fda8fc 257 (when (and icomplete-mode (icomplete-simple-completing-p))
2176854d 258 (set (make-local-variable 'completion-show-inline-help) nil)
cc37e70f
J
259 (use-local-map (make-composed-keymap icomplete-minibuffer-map
260 (current-local-map)))
504a0381
SM
261 (add-hook 'pre-command-hook #'icomplete-pre-command-hook nil t)
262 (add-hook 'post-command-hook #'icomplete-post-command-hook nil t)
d64b6308
DC
263 (run-hooks 'icomplete-minibuffer-setup-hook)
264 (when icomplete-show-matches-on-no-input
265 (icomplete-exhibit))))
504a0381
SM
266
267(defvar icomplete--in-region-buffer nil)
268
269(defun icomplete--in-region-setup ()
270 (when (or (not completion-in-region-mode)
271 (and icomplete--in-region-buffer
272 (not (eq icomplete--in-region-buffer (current-buffer)))))
273 (with-current-buffer (or icomplete--in-region-buffer (current-buffer))
274 (setq icomplete--in-region-buffer nil)
275 (delete-overlay icomplete-overlay)
276 (kill-local-variable 'completion-show-inline-help)
277 (remove-hook 'pre-command-hook 'icomplete-pre-command-hook t)
278 (remove-hook 'post-command-hook 'icomplete-post-command-hook t)
279 (message nil)))
280 (when (and completion-in-region-mode
281 icomplete-mode (icomplete-simple-completing-p))
282 (setq icomplete--in-region-buffer (current-buffer))
283 (set (make-local-variable 'completion-show-inline-help) nil)
284 (let ((tem (assq 'completion-in-region-mode
285 minor-mode-overriding-map-alist)))
286 (unless (memq icomplete-minibuffer-map (cdr tem))
287 (setcdr tem (make-composed-keymap icomplete-minibuffer-map
288 (cdr tem)))))
289 (add-hook 'pre-command-hook 'icomplete-pre-command-hook nil t)
290 (add-hook 'post-command-hook 'icomplete-post-command-hook nil t)))
291\f
1934dbf4
GM
292
293
239c87a1
RS
294;;;_* Completion
295
296;;;_ > icomplete-tidy ()
297(defun icomplete-tidy ()
d11320e5
JB
298 "Remove completions display (if any) prior to new user input.
299Should be run in on the minibuffer `pre-command-hook'.
300See `icomplete-mode' and `minibuffer-setup-hook'."
31d4b748 301 (delete-overlay icomplete-overlay))
d183f322 302
239c87a1 303;;;_ > icomplete-exhibit ()
d462ff97 304(defun icomplete-exhibit ()
d11320e5
JB
305 "Insert Icomplete completions display.
306Should be run via minibuffer `post-command-hook'.
307See `icomplete-mode' and `minibuffer-setup-hook'."
67982e2b
SM
308 (when (and icomplete-mode
309 (icomplete-simple-completing-p)) ;Shouldn't be necessary.
47fda8fc
SM
310 (save-excursion
311 (goto-char (point-max))
d462ff97 312 ; Insert the match-status information:
d64b6308
DC
313 (if (and (or icomplete-show-matches-on-no-input
314 (> (icomplete--field-end) (icomplete--field-beg)))
cc37e70f
J
315 (or
316 ;; Don't bother with delay after certain number of chars:
504a0381
SM
317 (> (- (point) (icomplete--field-beg))
318 icomplete-max-delay-chars)
cc37e70f
J
319 ;; Don't delay if the completions are known.
320 completion-all-sorted-completions
321 ;; Don't delay if alternatives number is small enough:
504a0381
SM
322 (and (sequencep (icomplete--completion-table))
323 (< (length (icomplete--completion-table))
cc37e70f
J
324 icomplete-delay-completions-threshold))
325 ;; Delay - give some grace time for next keystroke, before
47fda8fc
SM
326 ;; embarking on computing completions:
327 (sit-for icomplete-compute-delay)))
504a0381
SM
328 (let* ((field-string (icomplete--field-string))
329 (text (while-no-input
330 (icomplete-completions
331 field-string
332 (icomplete--completion-table)
333 (icomplete--completion-predicate)
334 (if (window-minibuffer-p)
335 (not minibuffer-completion-confirm)))))
336 (buffer-undo-list t)
337 deactivate-mark)
9a84ddd8 338 ;; Do nothing if while-no-input was aborted.
cc37e70f 339 (when (stringp text)
31d4b748
SM
340 (move-overlay icomplete-overlay (point) (point) (current-buffer))
341 ;; The current C cursor code doesn't know to use the overlay's
342 ;; marker's stickiness to figure out whether to place the cursor
343 ;; before or after the string, so let's spoon-feed it the pos.
344 (put-text-property 0 1 'cursor t text)
345 (overlay-put icomplete-overlay 'after-string text)))))))
d183f322 346
239c87a1
RS
347;;;_ > icomplete-completions (name candidates predicate require-match)
348(defun icomplete-completions (name candidates predicate require-match)
d462ff97
RS
349 "Identify prospective candidates for minibuffer completion.
350
c89164c5 351The display is updated with each minibuffer keystroke during
d462ff97
RS
352minibuffer completion.
353
354Prospective completion suffixes (if any) are displayed, bracketed by
d11320e5 355one of (), [], or {} pairs. The choice of brackets is as follows:
d462ff97 356
d11320e5
JB
357 (...) - a single prospect is identified and matching is enforced,
358 [...] - a single prospect is identified but matching is optional, or
359 {...} - multiple prospects, separated by commas, are indicated, and
81276211 360 further input is required to distinguish a single one.
d462ff97 361
a8cb4247
GM
362If there are multiple possibilities, `icomplete-separator' separates them.
363
81276211 364The displays for unambiguous matches have ` [Matched]' appended
d11320e5 365\(whether complete or not), or ` [No matches]', if no eligible
a8cb4247 366matches exist."
504a0381
SM
367 (let* ((minibuffer-completion-table candidates)
368 (minibuffer-completion-predicate predicate)
369 (md (completion--field-metadata (icomplete--field-beg)))
67982e2b 370 (comps (completion-all-sorted-completions
504a0381 371 (icomplete--field-beg) (icomplete--field-end)))
31d4b748
SM
372 (last (if (consp comps) (last comps)))
373 (base-size (cdr last))
476d2aef
SM
374 (open-bracket (if require-match "(" "["))
375 (close-bracket (if require-match ")" "]")))
c2d0b538 376 ;; `concat'/`mapconcat' is the slow part.
31d4b748 377 (if (not (consp comps))
504a0381
SM
378 (progn ;;(debug (format "Candidates=%S field=%S" candidates name))
379 (format " %sNo matches%s" open-bracket close-bracket))
31d4b748 380 (if last (setcdr last nil))
be91065f
SM
381 (let* ((most-try
382 (if (and base-size (> base-size 0))
383 (completion-try-completion
620c53a6 384 name candidates predicate (length name) md)
be91065f
SM
385 ;; If the `comps' are 0-based, the result should be
386 ;; the same with `comps'.
387 (completion-try-completion
620c53a6 388 name comps nil (length name) md)))
31d4b748
SM
389 (most (if (consp most-try) (car most-try)
390 (if most-try (car comps) "")))
476d2aef
SM
391 ;; Compare name and most, so we can determine if name is
392 ;; a prefix of most, or something else.
393 (compare (compare-strings name nil nil
394 most nil nil completion-ignore-case))
9a155058 395 (ellipsis (if (char-displayable-p ?…) "…" "..."))
476d2aef
SM
396 (determ (unless (or (eq t compare) (eq t most-try)
397 (= (setq compare (1- (abs compare)))
398 (length most)))
399 (concat open-bracket
400 (cond
401 ((= compare (length name))
402 ;; Typical case: name is a prefix.
403 (substring most compare))
2387610d
SM
404 ;; Don't bother truncating if it doesn't gain
405 ;; us at least 2 columns.
b6e443c5 406 ((< compare (+ 2 (string-width ellipsis))) most)
9a155058 407 (t (concat ellipsis (substring most compare))))
476d2aef 408 close-bracket)))
47fda8fc 409 ;;"-prospects" - more than one candidate
5cbfe5b9
J
410 (prospects-len (+ (string-width
411 (or determ (concat open-bracket close-bracket)))
bdd9367d 412 (string-width icomplete-separator)
b6e443c5 413 (+ 2 (string-width ellipsis)) ;; take {…} into account
bdd9367d 414 (string-width (buffer-string))))
c2d0b538
SM
415 (prospects-max
416 ;; Max total length to use, including the minibuffer content.
29f313d7 417 (* (+ icomplete-prospects-height
c2d0b538
SM
418 ;; If the minibuffer content already uses up more than
419 ;; one line, increase the allowable space accordingly.
420 (/ prospects-len (window-width)))
421 (window-width)))
aa2bddd7
SM
422 ;; Find the common prefix among `comps'.
423 ;; We can't use the optimization below because its assumptions
424 ;; aren't always true, e.g. when completion-cycling (bug#10850):
425 ;; (if (eq t (compare-strings (car comps) nil (length most)
426 ;; most nil nil completion-ignore-case))
427 ;; ;; Common case.
428 ;; (length most)
429 ;; Else, use try-completion.
5cbfe5b9
J
430 (prefix (when icomplete-hide-common-prefix
431 (try-completion "" comps)))
476d2aef 432 (prefix-len
aa2bddd7
SM
433 (and (stringp prefix)
434 ;; Only hide the prefix if the corresponding info
435 ;; is already displayed via `most'.
436 (string-prefix-p prefix most t)
437 (length prefix))) ;;)
5cbfe5b9 438 prospects comp limit)
504a0381 439 (if (or (eq most-try t) (not (consp (cdr comps))))
1934dbf4 440 (setq prospects nil)
5cbfe5b9
J
441 (when (member name comps)
442 ;; NAME is complete but not unique. This scenario poses
443 ;; following UI issues:
444 ;;
445 ;; - When `icomplete-hide-common-prefix' is non-nil, NAME
446 ;; is stripped empty. This would make the entry
447 ;; inconspicuous.
448 ;;
449 ;; - Due to sorting of completions, NAME may not be the
450 ;; first of the prospects and could be hidden deep in
451 ;; the displayed string.
452 ;;
453 ;; - Because of `icomplete-prospects-height' , NAME may
454 ;; not even be displayed to the user.
455 ;;
456 ;; To circumvent all the above problems, provide a visual
457 ;; cue to the user via an "empty string" in the try
458 ;; completion field.
459 (setq determ (concat open-bracket "" close-bracket)))
460 ;; Compute prospects for display.
7d94fb17 461 (while (and comps (not limit))
476d2aef 462 (setq comp
8f0fde21 463 (if prefix-len (substring (car comps) prefix-len) (car comps))
1934dbf4 464 comps (cdr comps))
5cbfe5b9 465 (setq prospects-len
bdd9367d
WD
466 (+ (string-width comp)
467 (string-width icomplete-separator)
468 prospects-len))
c2d0b538 469 (if (< prospects-len prospects-max)
31d4b748 470 (push comp prospects)
5cbfe5b9
J
471 (setq limit t))))
472 (setq prospects (nreverse prospects))
473 ;; Decorate first of the prospects.
474 (when prospects
475 (let ((first (copy-sequence (pop prospects))))
476 (put-text-property 0 (length first)
477 'face 'icomplete-first-match first)
478 (push first prospects)))
31d4b748
SM
479 ;; Restore the base-size info, since completion-all-sorted-completions
480 ;; is cached.
481 (if last (setcdr last base-size))
1934dbf4
GM
482 (if prospects
483 (concat determ
47fda8fc 484 "{"
5cbfe5b9 485 (mapconcat 'identity prospects icomplete-separator)
542a3840 486 (and limit (concat icomplete-separator ellipsis))
47fda8fc 487 "}")
cc37e70f 488 (concat determ " [Matched]"))))))
d462ff97 489
0da54898
DC
490;;;_* Provide
491(provide 'icomplete)
492
476d2aef
SM
493;;_* Local emacs vars.
494;;Local variables:
495;;allout-layout: (-2 :)
496;;End:
d462ff97
RS
497
498;;; icomplete.el ends here