(w32font_open_internal): Prefer truetype fonts unless
[bpt/emacs.git] / lisp / icomplete.el
1 ;;; icomplete.el --- minibuffer completion incremental feedback
2
3 ;; Copyright (C) 1992, 1993, 1994, 1997, 1999, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
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.
10 ;; Keywords: help, abbrev
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
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.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
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
34 ;; See `icomplete-completions' docstring for a description of the
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
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.
49
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
54 ;; contributors: Noah Friedman (restructuring as minor mode), Colin
55 ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
56
57 ;; klm.
58
59 ;;; Code:
60
61 ;;;_* Provide
62 (provide 'icomplete)
63
64
65 (defgroup icomplete nil
66 "Show completions dynamically in minibuffer."
67 :prefix "icomplete-"
68 :group 'minibuffer)
69
70 ;;;_* User Customization variables
71 (defcustom icomplete-prospects-length 80
72 "Length of string displaying the prospects."
73 :type 'integer
74 :group 'icomplete)
75
76 (defcustom icomplete-compute-delay .3
77 "Completions-computation stall, used only with large-number completions.
78 See `icomplete-delay-completions-threshold'."
79 :type 'number
80 :group 'icomplete)
81
82 (defcustom icomplete-delay-completions-threshold 400
83 "Pending-completions number over which to apply `icomplete-compute-delay'."
84 :type 'integer
85 :group 'icomplete)
86
87 (defcustom icomplete-max-delay-chars 3
88 "Maximum number of initial chars to apply icomplete compute delay."
89 :type 'integer
90 :group 'icomplete)
91
92 (defcustom icomplete-show-key-bindings t
93 "If non-nil, show key bindings as well as completion for sole matches."
94 :type 'boolean
95 :group 'icomplete)
96
97 (defcustom icomplete-minibuffer-setup-hook nil
98 "Icomplete-specific customization of minibuffer setup.
99
100 This hook is run during minibuffer setup if icomplete is active.
101 It is intended for use in customizing icomplete for interoperation
102 with other features and packages. For instance:
103
104 \(add-hook 'icomplete-minibuffer-setup-hook
105 \(function
106 \(lambda ()
107 \(make-local-variable 'max-mini-window-height)
108 \(setq max-mini-window-height 3))))
109
110 will constrain Emacs to a maximum minibuffer height of 3 lines when
111 icompletion is occurring."
112 :type 'hook
113 :group 'icomplete)
114
115
116 ;;;_* Initialization
117
118 ;;;_ + Internal Variables
119 ;;;_ = icomplete-eoinput nil
120 (defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
121 "Overlay used to display the list of completions.")
122
123 ;;;_ = icomplete-pre-command-hook
124 (defvar icomplete-pre-command-hook nil
125 "Incremental-minibuffer-completion pre-command-hook.
126
127 Is run in minibuffer before user input when `icomplete-mode' is non-nil.
128 Use `icomplete-mode' function to set it up properly for incremental
129 minibuffer completion.")
130 (add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
131 ;;;_ = icomplete-post-command-hook
132 (defvar icomplete-post-command-hook nil
133 "Incremental-minibuffer-completion post-command-hook.
134
135 Is run in minibuffer after 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-post-command-hook 'icomplete-exhibit)
139
140 (defun icomplete-get-keys (func-name)
141 "Return strings naming keys bound to `func-name', or nil if none.
142 Examines the prior, not current, buffer, presuming that current buffer
143 is minibuffer."
144 (if (commandp func-name)
145 (save-excursion
146 (let* ((sym (intern func-name))
147 (buf (other-buffer nil t))
148 (keys (with-current-buffer buf (where-is-internal sym))))
149 (if keys
150 (concat "<"
151 (mapconcat 'key-description
152 (sort keys
153 #'(lambda (x y)
154 (< (length x) (length y))))
155 ", ")
156 ">"))))))
157 ;;;_ = icomplete-with-completion-tables
158 (defvar icomplete-with-completion-tables '(internal-complete-buffer)
159 "Specialized completion tables with which icomplete should operate.
160
161 Icomplete does not operate with any specialized completion tables
162 except those on this list.")
163
164 ;;;_ > icomplete-mode (&optional prefix)
165 ;;;###autoload
166 (define-minor-mode icomplete-mode
167 "Toggle incremental minibuffer completion for this Emacs session.
168 With a numeric argument, turn Icomplete mode on if ARG is positive,
169 otherwise turn it off."
170 :global t :group 'icomplete
171 (if icomplete-mode
172 ;; The following is not really necessary after first time -
173 ;; no great loss.
174 (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
175 (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
176
177 ;;;_ > icomplete-simple-completing-p ()
178 (defun icomplete-simple-completing-p ()
179 "Non-nil if current window is minibuffer that's doing simple completion.
180
181 Conditions are:
182 the selected window is a minibuffer,
183 and not in the middle of macro execution,
184 and `minibuffer-completion-table' is not a symbol (which would
185 indicate some non-standard, non-simple completion mechanism,
186 like file-name and other custom-func completions)."
187
188 (and (window-minibuffer-p (selected-window))
189 (not executing-kbd-macro)
190 minibuffer-completion-table
191 (or (not (functionp minibuffer-completion-table))
192 (eq icomplete-with-completion-tables t)
193 (member minibuffer-completion-table
194 icomplete-with-completion-tables))))
195
196 ;;;_ > icomplete-minibuffer-setup ()
197 (defun icomplete-minibuffer-setup ()
198 "Run in minibuffer on activation to establish incremental completion.
199 Usually run by inclusion in `minibuffer-setup-hook'."
200 (when (and icomplete-mode (icomplete-simple-completing-p))
201 (add-hook 'pre-command-hook
202 (lambda () (run-hooks 'icomplete-pre-command-hook))
203 nil t)
204 (add-hook 'post-command-hook
205 (lambda () (run-hooks 'icomplete-post-command-hook))
206 nil t)
207 (run-hooks 'icomplete-minibuffer-setup-hook)))
208 ;\f
209
210
211 ;;;_* Completion
212
213 ;;;_ > icomplete-tidy ()
214 (defun icomplete-tidy ()
215 "Remove completions display \(if any) prior to new user input.
216 Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
217 and `minibuffer-setup-hook'."
218 (delete-overlay icomplete-overlay))
219
220 ;;;_ > icomplete-exhibit ()
221 (defun icomplete-exhibit ()
222 "Insert icomplete completions display.
223 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
224 and `minibuffer-setup-hook'."
225 (when (and icomplete-mode (icomplete-simple-completing-p))
226 (save-excursion
227 (goto-char (point-max))
228 ; Insert the match-status information:
229 (if (and (> (point-max) (minibuffer-prompt-end))
230 buffer-undo-list ; Wait for some user input.
231 (or
232 ;; Don't bother with delay after certain number of chars:
233 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
234 ;; Don't delay if alternatives number is small enough:
235 (and (sequencep minibuffer-completion-table)
236 (< (length minibuffer-completion-table)
237 icomplete-delay-completions-threshold))
238 ;; Delay - give some grace time for next keystroke, before
239 ;; embarking on computing completions:
240 (sit-for icomplete-compute-delay)))
241 (let ((text (while-no-input
242 (icomplete-completions
243 (field-string)
244 minibuffer-completion-table
245 minibuffer-completion-predicate
246 (not minibuffer-completion-confirm))))
247 (buffer-undo-list t)
248 deactivate-mark)
249 ;; Do nothing if while-no-input was aborted.
250 (when (stringp text)
251 (move-overlay icomplete-overlay (point) (point) (current-buffer))
252 ;; The current C cursor code doesn't know to use the overlay's
253 ;; marker's stickiness to figure out whether to place the cursor
254 ;; before or after the string, so let's spoon-feed it the pos.
255 (put-text-property 0 1 'cursor t text)
256 (overlay-put icomplete-overlay 'after-string text)))))))
257
258 ;;;_ > icomplete-completions (name candidates predicate require-match)
259 (defun icomplete-completions (name candidates predicate require-match)
260 "Identify prospective candidates for minibuffer completion.
261
262 The display is updated with each minibuffer keystroke during
263 minibuffer completion.
264
265 Prospective completion suffixes (if any) are displayed, bracketed by
266 one of \(), \[], or \{} pairs. The choice of brackets is as follows:
267
268 \(...) - a single prospect is identified and matching is enforced,
269 \[...] - a single prospect is identified but matching is optional, or
270 \{...} - multiple prospects, separated by commas, are indicated, and
271 further input is required to distinguish a single one.
272
273 The displays for unambiguous matches have ` [Matched]' appended
274 \(whether complete or not), or ` \[No matches]', if no eligible
275 matches exist. \(Keybindings for uniquely matched commands
276 are exhibited within the square braces.)"
277
278 (let* ((comps (completion-all-sorted-completions))
279 (last (if (consp comps) (last comps)))
280 (base-size (cdr last))
281 (open-bracket (if require-match "(" "["))
282 (close-bracket (if require-match ")" "]")))
283 ;; `concat'/`mapconcat' is the slow part. With the introduction of
284 ;; `icomplete-prospects-length', there is no need for `catch'/`throw'.
285 (if (not (consp comps))
286 (format " %sNo matches%s" open-bracket close-bracket)
287 (if last (setcdr last nil))
288 (let* ((most-try
289 (if (and base-size (> base-size 0))
290 (completion-try-completion
291 name candidates predicate (length name))
292 ;; If the `comps' are 0-based, the result should be
293 ;; the same with `comps'.
294 (completion-try-completion
295 name comps nil (length name))))
296 (most (if (consp most-try) (car most-try)
297 (if most-try (car comps) "")))
298 ;; Compare name and most, so we can determine if name is
299 ;; a prefix of most, or something else.
300 (compare (compare-strings name nil nil
301 most nil nil completion-ignore-case))
302 (determ (unless (or (eq t compare) (eq t most-try)
303 (= (setq compare (1- (abs compare)))
304 (length most)))
305 (concat open-bracket
306 (cond
307 ((= compare (length name))
308 ;; Typical case: name is a prefix.
309 (substring most compare))
310 ((< compare 5) most)
311 (t (concat "..." (substring most compare))))
312 close-bracket)))
313 ;;"-prospects" - more than one candidate
314 (prospects-len (+ (length determ) 6)) ;; take {,...} into account
315 (prefix-len
316 ;; Find the common prefix among `comps'.
317 (if (eq t (compare-strings (car comps) nil (length most)
318 most nil nil completion-ignore-case))
319 ;; Common case.
320 (length most)
321 ;; Else, use try-completion.
322 (let ((comps-prefix (try-completion "" comps)))
323 (and (stringp comps-prefix)
324 (length comps-prefix)))))
325
326 prospects most-is-exact comp limit)
327 (if (eq most-try t) ;; (or (null (cdr comps))
328 (setq prospects nil)
329 (while (and comps (not limit))
330 (setq comp
331 (if prefix-len (substring (car comps) prefix-len) (car comps))
332 comps (cdr comps))
333 (cond ((string-equal comp "") (setq most-is-exact t))
334 ((member comp prospects))
335 (t (setq prospects-len (+ (length comp) 1 prospects-len))
336 (if (< prospects-len icomplete-prospects-length)
337 (push comp prospects)
338 (setq limit t))))))
339 ;; Restore the base-size info, since completion-all-sorted-completions
340 ;; is cached.
341 (if last (setcdr last base-size))
342 (if prospects
343 (concat determ
344 "{"
345 (and most-is-exact ",")
346 (mapconcat 'identity (nreverse prospects) ",")
347 (and limit ",...")
348 "}")
349 (concat determ
350 " [Matched"
351 (let ((keys (and icomplete-show-key-bindings
352 (commandp (intern-soft most))
353 (icomplete-get-keys most))))
354 (if keys (concat "; " keys) ""))
355 "]"))))))
356
357 ;;_* Local emacs vars.
358 ;;Local variables:
359 ;;allout-layout: (-2 :)
360 ;;End:
361
362 ;; arch-tag: 339ec25a-0741-4eb6-be63-997532e89b0f
363 ;;; icomplete.el ends here