* image.c (pbm_load): Set to NO_PIXMAP on error.
[bpt/emacs.git] / lisp / icomplete.el
CommitLineData
fa157191 1;;; icomplete.el --- minibuffer completion incremental feedback
239c87a1 2
ba318903 3;; Copyright (C) 1992-1994, 1997, 1999, 2001-2014 Free Software
ab422c4d 4;; 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
5e537651
SE
61(defgroup icomplete nil
62 "Show completions dynamically in minibuffer."
63 :prefix "icomplete-"
64 :group 'minibuffer)
65
c2d0b538
SM
66(defvar icomplete-prospects-length 80)
67(make-obsolete-variable
68 'icomplete-prospects-length 'icomplete-prospects-height "23.1")
69
cc37e70f
J
70(defcustom icomplete-separator " | "
71 "String used by icomplete to separate alternatives in the minibuffer."
72 :type 'string
fb3bf6ce 73 :version "24.4")
cc37e70f 74
5cbfe5b9
J
75(defcustom icomplete-hide-common-prefix t
76 "When non-nil, hide common prefix from completion candidates.
77When nil, show candidates in full."
78 :type 'boolean
d64b6308
DC
79 :version "24.4")
80
a4151f3b 81(defcustom icomplete-show-matches-on-no-input nil
d64b6308
DC
82 "When non-nil, show completions when first prompting for input."
83 :type 'boolean
84 :version "24.4")
5cbfe5b9
J
85
86(defface icomplete-first-match '((t :weight bold))
87 "Face used by icomplete for highlighting first match."
d64b6308 88 :version "24.4")
5cbfe5b9 89
1934dbf4 90;;;_* User Customization variables
c2d0b538
SM
91(defcustom icomplete-prospects-height
92 ;; 20 is an estimated common size for the prompt + minibuffer content, to
93 ;; try to guess the number of lines used up by icomplete-prospects-length.
94 (+ 1 (/ (+ icomplete-prospects-length 20) (window-width)))
95 "Maximum number of lines to use in the minibuffer."
dfb4dab1 96 :type 'integer
af09cfd7 97 :version "23.1")
1934dbf4 98
5e537651 99(defcustom icomplete-compute-delay .3
077aec27
JB
100 "Completions-computation stall, used only with large-number completions.
101See `icomplete-delay-completions-threshold'."
d64b6308 102 :type 'number)
5e537651
SE
103
104(defcustom icomplete-delay-completions-threshold 400
077aec27 105 "Pending-completions number over which to apply `icomplete-compute-delay'."
d64b6308 106 :type 'integer)
5e537651
SE
107
108(defcustom icomplete-max-delay-chars 3
077aec27 109 "Maximum number of initial chars to apply icomplete compute delay."
d64b6308 110 :type 'integer)
5e537651 111
504a0381
SM
112(defvar icomplete-in-buffer nil
113 "If non-nil, also use Icomplete when completing in non-mini buffers.")
114
5e537651 115(defcustom icomplete-minibuffer-setup-hook nil
077aec27 116 "Icomplete-specific customization of minibuffer setup.
239c87a1 117
4837b516 118This hook is run during minibuffer setup if icomplete is active.
239c87a1 119It is intended for use in customizing icomplete for interoperation
9c8acefd 120with other features and packages. For instance:
239c87a1 121
92f7d003 122 \(add-hook 'icomplete-minibuffer-setup-hook
239c87a1
RS
123 \(function
124 \(lambda ()
9c8acefd
EZ
125 \(make-local-variable 'max-mini-window-height)
126 \(setq 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
397e713b 149;;;_ = icomplete-with-completion-tables
67982e2b 150(defcustom icomplete-with-completion-tables t
397e713b
CY
151 "Specialized completion tables with which icomplete should operate.
152
a54a7552
LI
153If this variable is a list, Icomplete will not operate with any
154specialized completion tables except those on this list. If this
155variable is t, it won't restrict completion tables."
846ffe77 156 :version "24.4"
67982e2b
SM
157 :type '(choice (const :tag "All" t)
158 (repeat function)))
92f7d003 159
cc37e70f
J
160(defvar icomplete-minibuffer-map
161 (let ((map (make-sparse-keymap)))
162 (define-key map [?\M-\t] 'minibuffer-force-complete)
163 (define-key map [?\C-j] 'minibuffer-force-complete-and-exit)
d7e76a89
J
164 (define-key map [?\C-.] 'icomplete-forward-completions)
165 (define-key map [?\C-,] 'icomplete-backward-completions)
cc37e70f
J
166 map))
167
168(defun icomplete-forward-completions ()
169 "Step forward completions by one entry.
170Second entry becomes the first and can be selected with
171`minibuffer-force-complete-and-exit'."
172 (interactive)
504a0381
SM
173 (let* ((beg (icomplete--field-beg))
174 (end (icomplete--field-end))
67982e2b 175 (comps (completion-all-sorted-completions beg end))
cc37e70f 176 (last (last comps)))
6130b96a
J
177 (when comps
178 (setcdr last (cons (car comps) (cdr last)))
67982e2b 179 (completion--cache-all-sorted-completions beg end (cdr comps)))))
cc37e70f
J
180
181(defun icomplete-backward-completions ()
182 "Step backward completions by one entry.
183Last entry becomes the first and can be selected with
184`minibuffer-force-complete-and-exit'."
185 (interactive)
504a0381
SM
186 (let* ((beg (icomplete--field-beg))
187 (end (icomplete--field-end))
67982e2b 188 (comps (completion-all-sorted-completions beg end))
cc37e70f
J
189 (last-but-one (last comps 2))
190 (last (cdr last-but-one)))
6130b96a 191 (when (consp last) ; At least two elements in comps
cc37e70f
J
192 (setcdr last-but-one (cdr last))
193 (push (car last) comps)
67982e2b 194 (completion--cache-all-sorted-completions beg end comps))))
cc37e70f 195
239c87a1 196;;;_ > icomplete-mode (&optional prefix)
c89164c5 197;;;###autoload
47fda8fc 198(define-minor-mode icomplete-mode
06e21633
CY
199 "Toggle incremental minibuffer completion (Icomplete mode).
200With a prefix argument ARG, enable Icomplete mode if ARG is
201positive, and disable it otherwise. If called from Lisp, enable
202the mode if ARG is omitted or nil."
47fda8fc 203 :global t :group 'icomplete
504a0381
SM
204 (remove-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)
205 (remove-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup)
206 (when icomplete-mode
207 (when icomplete-in-buffer
208 (add-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup))
209 (add-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)))
210
211(defun icomplete--completion-table ()
212 (if (window-minibuffer-p) minibuffer-completion-table
213 (or (nth 2 completion-in-region--data)
214 (message "In %S (w=%S): %S"
215 (current-buffer) (selected-window) (window-minibuffer-p)))))
216(defun icomplete--completion-predicate ()
217 (if (window-minibuffer-p) minibuffer-completion-predicate
218 (nth 3 completion-in-region--data)))
219(defun icomplete--field-string ()
220 (if (window-minibuffer-p) (minibuffer-contents)
221 (buffer-substring-no-properties
222 (nth 0 completion-in-region--data)
223 (nth 1 completion-in-region--data))))
224(defun icomplete--field-beg ()
225 (if (window-minibuffer-p) (minibuffer-prompt-end)
226 (nth 0 completion-in-region--data)))
227(defun icomplete--field-end ()
228 (if (window-minibuffer-p) (point-max)
229 (nth 1 completion-in-region--data)))
239c87a1
RS
230
231;;;_ > icomplete-simple-completing-p ()
232(defun icomplete-simple-completing-p ()
239c87a1
RS
233 "Non-nil if current window is minibuffer that's doing simple completion.
234
235Conditions are:
236 the selected window is a minibuffer,
237 and not in the middle of macro execution,
504a0381 238 and the completion table is not a function (which would
81276211 239 indicate some non-standard, non-simple completion mechanism,
c359b7bd
LI
240 like file-name and other custom-func completions),
241 and `icomplete-with-completion-tables' doesn't restrict completion."
239c87a1 242
504a0381
SM
243 (unless executing-kbd-macro
244 (let ((table (icomplete--completion-table)))
245 (and table
246 (or (not (functionp table))
247 (eq icomplete-with-completion-tables t)
248 (member table icomplete-with-completion-tables))))))
d183f322 249
239c87a1 250;;;_ > icomplete-minibuffer-setup ()
239c87a1 251(defun icomplete-minibuffer-setup ()
239c87a1 252 "Run in minibuffer on activation to establish incremental completion.
d183f322 253Usually run by inclusion in `minibuffer-setup-hook'."
47fda8fc 254 (when (and icomplete-mode (icomplete-simple-completing-p))
2176854d 255 (set (make-local-variable 'completion-show-inline-help) nil)
cc37e70f
J
256 (use-local-map (make-composed-keymap icomplete-minibuffer-map
257 (current-local-map)))
504a0381
SM
258 (add-hook 'pre-command-hook #'icomplete-pre-command-hook nil t)
259 (add-hook 'post-command-hook #'icomplete-post-command-hook nil t)
d64b6308
DC
260 (run-hooks 'icomplete-minibuffer-setup-hook)
261 (when icomplete-show-matches-on-no-input
262 (icomplete-exhibit))))
504a0381
SM
263
264(defvar icomplete--in-region-buffer nil)
265
266(defun icomplete--in-region-setup ()
267 (when (or (not completion-in-region-mode)
268 (and icomplete--in-region-buffer
269 (not (eq icomplete--in-region-buffer (current-buffer)))))
270 (with-current-buffer (or icomplete--in-region-buffer (current-buffer))
271 (setq icomplete--in-region-buffer nil)
272 (delete-overlay icomplete-overlay)
273 (kill-local-variable 'completion-show-inline-help)
274 (remove-hook 'pre-command-hook 'icomplete-pre-command-hook t)
275 (remove-hook 'post-command-hook 'icomplete-post-command-hook t)
276 (message nil)))
277 (when (and completion-in-region-mode
278 icomplete-mode (icomplete-simple-completing-p))
279 (setq icomplete--in-region-buffer (current-buffer))
280 (set (make-local-variable 'completion-show-inline-help) nil)
281 (let ((tem (assq 'completion-in-region-mode
282 minor-mode-overriding-map-alist)))
283 (unless (memq icomplete-minibuffer-map (cdr tem))
284 (setcdr tem (make-composed-keymap icomplete-minibuffer-map
285 (cdr tem)))))
286 (add-hook 'pre-command-hook 'icomplete-pre-command-hook nil t)
287 (add-hook 'post-command-hook 'icomplete-post-command-hook nil t)))
288\f
1934dbf4
GM
289
290
239c87a1
RS
291;;;_* Completion
292
293;;;_ > icomplete-tidy ()
294(defun icomplete-tidy ()
295 "Remove completions display \(if any) prior to new user input.
d183f322 296Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
239c87a1 297and `minibuffer-setup-hook'."
31d4b748 298 (delete-overlay icomplete-overlay))
d183f322 299
239c87a1 300;;;_ > icomplete-exhibit ()
d462ff97 301(defun icomplete-exhibit ()
239c87a1 302 "Insert icomplete completions display.
d183f322 303Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
239c87a1 304and `minibuffer-setup-hook'."
67982e2b
SM
305 (when (and icomplete-mode
306 (icomplete-simple-completing-p)) ;Shouldn't be necessary.
47fda8fc
SM
307 (save-excursion
308 (goto-char (point-max))
d462ff97 309 ; Insert the match-status information:
d64b6308
DC
310 (if (and (or icomplete-show-matches-on-no-input
311 (> (icomplete--field-end) (icomplete--field-beg)))
cc37e70f
J
312 (or
313 ;; Don't bother with delay after certain number of chars:
504a0381
SM
314 (> (- (point) (icomplete--field-beg))
315 icomplete-max-delay-chars)
cc37e70f
J
316 ;; Don't delay if the completions are known.
317 completion-all-sorted-completions
318 ;; Don't delay if alternatives number is small enough:
504a0381
SM
319 (and (sequencep (icomplete--completion-table))
320 (< (length (icomplete--completion-table))
cc37e70f
J
321 icomplete-delay-completions-threshold))
322 ;; Delay - give some grace time for next keystroke, before
47fda8fc
SM
323 ;; embarking on computing completions:
324 (sit-for icomplete-compute-delay)))
504a0381
SM
325 (let* ((field-string (icomplete--field-string))
326 (text (while-no-input
327 (icomplete-completions
328 field-string
329 (icomplete--completion-table)
330 (icomplete--completion-predicate)
331 (if (window-minibuffer-p)
332 (not minibuffer-completion-confirm)))))
333 (buffer-undo-list t)
334 deactivate-mark)
9a84ddd8 335 ;; Do nothing if while-no-input was aborted.
cc37e70f 336 (when (stringp text)
31d4b748
SM
337 (move-overlay icomplete-overlay (point) (point) (current-buffer))
338 ;; The current C cursor code doesn't know to use the overlay's
339 ;; marker's stickiness to figure out whether to place the cursor
340 ;; before or after the string, so let's spoon-feed it the pos.
341 (put-text-property 0 1 'cursor t text)
342 (overlay-put icomplete-overlay 'after-string text)))))))
d183f322 343
239c87a1
RS
344;;;_ > icomplete-completions (name candidates predicate require-match)
345(defun icomplete-completions (name candidates predicate require-match)
d462ff97
RS
346 "Identify prospective candidates for minibuffer completion.
347
c89164c5 348The display is updated with each minibuffer keystroke during
d462ff97
RS
349minibuffer completion.
350
351Prospective completion suffixes (if any) are displayed, bracketed by
352one of \(), \[], or \{} pairs. The choice of brackets is as follows:
353
354 \(...) - a single prospect is identified and matching is enforced,
355 \[...] - a single prospect is identified but matching is optional, or
356 \{...} - multiple prospects, separated by commas, are indicated, and
81276211 357 further input is required to distinguish a single one.
d462ff97 358
81276211 359The displays for unambiguous matches have ` [Matched]' appended
d183f322 360\(whether complete or not), or ` \[No matches]', if no eligible
1be5a284 361matches exist. \(Keybindings for uniquely matched commands
92f7d003
KH
362are exhibited within the square braces.)"
363
504a0381
SM
364 (let* ((minibuffer-completion-table candidates)
365 (minibuffer-completion-predicate predicate)
366 (md (completion--field-metadata (icomplete--field-beg)))
67982e2b 367 (comps (completion-all-sorted-completions
504a0381 368 (icomplete--field-beg) (icomplete--field-end)))
31d4b748
SM
369 (last (if (consp comps) (last comps)))
370 (base-size (cdr last))
476d2aef
SM
371 (open-bracket (if require-match "(" "["))
372 (close-bracket (if require-match ")" "]")))
c2d0b538 373 ;; `concat'/`mapconcat' is the slow part.
31d4b748 374 (if (not (consp comps))
504a0381
SM
375 (progn ;;(debug (format "Candidates=%S field=%S" candidates name))
376 (format " %sNo matches%s" open-bracket close-bracket))
31d4b748 377 (if last (setcdr last nil))
be91065f
SM
378 (let* ((most-try
379 (if (and base-size (> base-size 0))
380 (completion-try-completion
620c53a6 381 name candidates predicate (length name) md)
be91065f
SM
382 ;; If the `comps' are 0-based, the result should be
383 ;; the same with `comps'.
384 (completion-try-completion
620c53a6 385 name comps nil (length name) md)))
31d4b748
SM
386 (most (if (consp most-try) (car most-try)
387 (if most-try (car comps) "")))
476d2aef
SM
388 ;; Compare name and most, so we can determine if name is
389 ;; a prefix of most, or something else.
390 (compare (compare-strings name nil nil
391 most nil nil completion-ignore-case))
392 (determ (unless (or (eq t compare) (eq t most-try)
393 (= (setq compare (1- (abs compare)))
394 (length most)))
395 (concat open-bracket
396 (cond
397 ((= compare (length name))
398 ;; Typical case: name is a prefix.
399 (substring most compare))
2387610d
SM
400 ;; Don't bother truncating if it doesn't gain
401 ;; us at least 2 columns.
402 ((< compare 3) most)
403 (t (concat "…" (substring most compare))))
476d2aef 404 close-bracket)))
47fda8fc 405 ;;"-prospects" - more than one candidate
5cbfe5b9
J
406 (prospects-len (+ (string-width
407 (or determ (concat open-bracket close-bracket)))
bdd9367d
WD
408 (string-width icomplete-separator)
409 3 ;; take {…} into account
410 (string-width (buffer-string))))
c2d0b538
SM
411 (prospects-max
412 ;; Max total length to use, including the minibuffer content.
29f313d7 413 (* (+ icomplete-prospects-height
c2d0b538
SM
414 ;; If the minibuffer content already uses up more than
415 ;; one line, increase the allowable space accordingly.
416 (/ prospects-len (window-width)))
417 (window-width)))
aa2bddd7
SM
418 ;; Find the common prefix among `comps'.
419 ;; We can't use the optimization below because its assumptions
420 ;; aren't always true, e.g. when completion-cycling (bug#10850):
421 ;; (if (eq t (compare-strings (car comps) nil (length most)
422 ;; most nil nil completion-ignore-case))
423 ;; ;; Common case.
424 ;; (length most)
425 ;; Else, use try-completion.
5cbfe5b9
J
426 (prefix (when icomplete-hide-common-prefix
427 (try-completion "" comps)))
476d2aef 428 (prefix-len
aa2bddd7
SM
429 (and (stringp prefix)
430 ;; Only hide the prefix if the corresponding info
431 ;; is already displayed via `most'.
432 (string-prefix-p prefix most t)
433 (length prefix))) ;;)
5cbfe5b9 434 prospects comp limit)
504a0381 435 (if (or (eq most-try t) (not (consp (cdr comps))))
1934dbf4 436 (setq prospects nil)
5cbfe5b9
J
437 (when (member name comps)
438 ;; NAME is complete but not unique. This scenario poses
439 ;; following UI issues:
440 ;;
441 ;; - When `icomplete-hide-common-prefix' is non-nil, NAME
442 ;; is stripped empty. This would make the entry
443 ;; inconspicuous.
444 ;;
445 ;; - Due to sorting of completions, NAME may not be the
446 ;; first of the prospects and could be hidden deep in
447 ;; the displayed string.
448 ;;
449 ;; - Because of `icomplete-prospects-height' , NAME may
450 ;; not even be displayed to the user.
451 ;;
452 ;; To circumvent all the above problems, provide a visual
453 ;; cue to the user via an "empty string" in the try
454 ;; completion field.
455 (setq determ (concat open-bracket "" close-bracket)))
456 ;; Compute prospects for display.
7d94fb17 457 (while (and comps (not limit))
476d2aef 458 (setq comp
8f0fde21 459 (if prefix-len (substring (car comps) prefix-len) (car comps))
1934dbf4 460 comps (cdr comps))
5cbfe5b9 461 (setq prospects-len
bdd9367d
WD
462 (+ (string-width comp)
463 (string-width icomplete-separator)
464 prospects-len))
c2d0b538 465 (if (< prospects-len prospects-max)
31d4b748 466 (push comp prospects)
5cbfe5b9
J
467 (setq limit t))))
468 (setq prospects (nreverse prospects))
469 ;; Decorate first of the prospects.
470 (when prospects
471 (let ((first (copy-sequence (pop prospects))))
472 (put-text-property 0 (length first)
473 'face 'icomplete-first-match first)
474 (push first prospects)))
31d4b748
SM
475 ;; Restore the base-size info, since completion-all-sorted-completions
476 ;; is cached.
477 (if last (setcdr last base-size))
1934dbf4
GM
478 (if prospects
479 (concat determ
47fda8fc 480 "{"
5cbfe5b9 481 (mapconcat 'identity prospects icomplete-separator)
cc37e70f 482 (and limit (concat icomplete-separator "…"))
47fda8fc 483 "}")
cc37e70f 484 (concat determ " [Matched]"))))))
d462ff97 485
0da54898
DC
486;;;_* Provide
487(provide 'icomplete)
488
476d2aef
SM
489;;_* Local emacs vars.
490;;Local variables:
491;;allout-layout: (-2 :)
492;;End:
d462ff97
RS
493
494;;; icomplete.el ends here