]> code.delx.au - gnu-emacs/blob - doc/lispref/minibuf.texi
Merge from emacs-24; up to 2014-04-25T10:35:01Z!michael.albinus@gmx.de
[gnu-emacs] / doc / lispref / minibuf.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software
4 @c Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @node Minibuffers
7 @chapter Minibuffers
8 @cindex arguments, reading
9 @cindex complex arguments
10 @cindex minibuffer
11
12 A @dfn{minibuffer} is a special buffer that Emacs commands use to
13 read arguments more complicated than the single numeric prefix
14 argument. These arguments include file names, buffer names, and
15 command names (as in @kbd{M-x}). The minibuffer is displayed on the
16 bottom line of the frame, in the same place as the echo area
17 (@pxref{The Echo Area}), but only while it is in use for reading an
18 argument.
19
20 @menu
21 * Intro to Minibuffers:: Basic information about minibuffers.
22 * Text from Minibuffer:: How to read a straight text string.
23 * Object from Minibuffer:: How to read a Lisp object or expression.
24 * Minibuffer History:: Recording previous minibuffer inputs
25 so the user can reuse them.
26 * Initial Input:: Specifying initial contents for the minibuffer.
27 * Completion:: How to invoke and customize completion.
28 * Yes-or-No Queries:: Asking a question with a simple answer.
29 * Multiple Queries:: Asking a series of similar questions.
30 * Reading a Password:: Reading a password from the terminal.
31 * Minibuffer Commands:: Commands used as key bindings in minibuffers.
32 * Minibuffer Windows:: Operating on the special minibuffer windows.
33 * Minibuffer Contents:: How such commands access the minibuffer text.
34 * Recursive Mini:: Whether recursive entry to minibuffer is allowed.
35 * Minibuffer Misc:: Various customization hooks and variables.
36 @end menu
37
38 @node Intro to Minibuffers
39 @section Introduction to Minibuffers
40
41 In most ways, a minibuffer is a normal Emacs buffer. Most operations
42 @emph{within} a buffer, such as editing commands, work normally in a
43 minibuffer. However, many operations for managing buffers do not apply
44 to minibuffers. The name of a minibuffer always has the form @w{@samp{
45 *Minibuf-@var{number}*}}, and it cannot be changed. Minibuffers are
46 displayed only in special windows used only for minibuffers; these
47 windows always appear at the bottom of a frame. (Sometimes frames have
48 no minibuffer window, and sometimes a special kind of frame contains
49 nothing but a minibuffer window; see @ref{Minibuffers and Frames}.)
50
51 The text in the minibuffer always starts with the @dfn{prompt string},
52 the text that was specified by the program that is using the minibuffer
53 to tell the user what sort of input to type. This text is marked
54 read-only so you won't accidentally delete or change it. It is also
55 marked as a field (@pxref{Fields}), so that certain motion functions,
56 including @code{beginning-of-line}, @code{forward-word},
57 @code{forward-sentence}, and @code{forward-paragraph}, stop at the
58 boundary between the prompt and the actual text.
59
60 @c See http://debbugs.gnu.org/11276
61 The minibuffer's window is normally a single line; it grows
62 automatically if the contents require more space. Whilst it is
63 active, you can explicitly resize it temporarily with the window
64 sizing commands; it reverts to its normal size when the minibuffer is
65 exited. When the minibuffer is not active, you can resize it
66 permanently by using the window sizing commands in the frame's other
67 window, or dragging the mode line with the mouse. (Due to details of
68 the current implementation, for this to work @code{resize-mini-windows}
69 must be @code{nil}.) If the frame contains just a minibuffer, you can
70 change the minibuffer's size by changing the frame's size.
71
72 Use of the minibuffer reads input events, and that alters the values
73 of variables such as @code{this-command} and @code{last-command}
74 (@pxref{Command Loop Info}). Your program should bind them around the
75 code that uses the minibuffer, if you do not want that to change them.
76
77 Under some circumstances, a command can use a minibuffer even if
78 there is an active minibuffer; such a minibuffer is called a
79 @dfn{recursive minibuffer}. The first minibuffer is named
80 @w{@samp{ *Minibuf-1*}}. Recursive minibuffers are named by
81 incrementing the number at the end of the name. (The names begin with
82 a space so that they won't show up in normal buffer lists.) Of
83 several recursive minibuffers, the innermost (or most recently
84 entered) is the active minibuffer. We usually call this ``the''
85 minibuffer. You can permit or forbid recursive minibuffers by setting
86 the variable @code{enable-recursive-minibuffers}, or by putting
87 properties of that name on command symbols (@xref{Recursive Mini}.)
88
89 Like other buffers, a minibuffer uses a local keymap
90 (@pxref{Keymaps}) to specify special key bindings. The function that
91 invokes the minibuffer also sets up its local map according to the job
92 to be done. @xref{Text from Minibuffer}, for the non-completion
93 minibuffer local maps. @xref{Completion Commands}, for the minibuffer
94 local maps for completion.
95
96 @cindex inactive minibuffer
97 When a minibuffer is inactive, its major mode is
98 @code{minibuffer-inactive-mode}, with keymap
99 @code{minibuffer-inactive-mode-map}. This is only really useful if
100 the minibuffer is in a separate frame. @xref{Minibuffers and Frames}.
101
102 When Emacs is running in batch mode, any request to read from the
103 minibuffer actually reads a line from the standard input descriptor that
104 was supplied when Emacs was started.
105
106 @node Text from Minibuffer
107 @section Reading Text Strings with the Minibuffer
108
109 The most basic primitive for minibuffer input is
110 @code{read-from-minibuffer}, which can be used to read either a string
111 or a Lisp object in textual form. The function @code{read-regexp} is
112 used for reading regular expressions (@pxref{Regular Expressions}),
113 which are a special kind of string. There are also specialized
114 functions for reading commands, variables, file names, etc.@:
115 (@pxref{Completion}).
116
117 In most cases, you should not call minibuffer input functions in the
118 middle of a Lisp function. Instead, do all minibuffer input as part of
119 reading the arguments for a command, in the @code{interactive}
120 specification. @xref{Defining Commands}.
121
122 @defun read-from-minibuffer prompt &optional initial keymap read history default inherit-input-method
123 This function is the most general way to get input from the
124 minibuffer. By default, it accepts arbitrary text and returns it as a
125 string; however, if @var{read} is non-@code{nil}, then it uses
126 @code{read} to convert the text into a Lisp object (@pxref{Input
127 Functions}).
128
129 The first thing this function does is to activate a minibuffer and
130 display it with @var{prompt} (which must be a string) as the
131 prompt. Then the user can edit text in the minibuffer.
132
133 When the user types a command to exit the minibuffer,
134 @code{read-from-minibuffer} constructs the return value from the text in
135 the minibuffer. Normally it returns a string containing that text.
136 However, if @var{read} is non-@code{nil}, @code{read-from-minibuffer}
137 reads the text and returns the resulting Lisp object, unevaluated.
138 (@xref{Input Functions}, for information about reading.)
139
140 The argument @var{default} specifies default values to make available
141 through the history commands. It should be a string, a list of
142 strings, or @code{nil}. The string or strings become the minibuffer's
143 ``future history'', available to the user with @kbd{M-n}.
144
145 If @var{read} is non-@code{nil}, then @var{default} is also used
146 as the input to @code{read}, if the user enters empty input.
147 If @var{default} is a list of strings, the first string is used as the input.
148 If @var{default} is @code{nil}, empty input results in an @code{end-of-file} error.
149 However, in the usual case (where @var{read} is @code{nil}),
150 @code{read-from-minibuffer} ignores @var{default} when the user enters
151 empty input and returns an empty string, @code{""}. In this respect,
152 it differs from all the other minibuffer input functions in this chapter.
153
154 If @var{keymap} is non-@code{nil}, that keymap is the local keymap to
155 use in the minibuffer. If @var{keymap} is omitted or @code{nil}, the
156 value of @code{minibuffer-local-map} is used as the keymap. Specifying
157 a keymap is the most important way to customize the minibuffer for
158 various applications such as completion.
159
160 The argument @var{history} specifies a history list variable to use
161 for saving the input and for history commands used in the minibuffer.
162 It defaults to @code{minibuffer-history}. You can optionally specify
163 a starting position in the history list as well. @xref{Minibuffer History}.
164
165 If the variable @code{minibuffer-allow-text-properties} is
166 non-@code{nil}, then the string that is returned includes whatever text
167 properties were present in the minibuffer. Otherwise all the text
168 properties are stripped when the value is returned.
169
170 If the argument @var{inherit-input-method} is non-@code{nil}, then the
171 minibuffer inherits the current input method (@pxref{Input Methods}) and
172 the setting of @code{enable-multibyte-characters} (@pxref{Text
173 Representations}) from whichever buffer was current before entering the
174 minibuffer.
175
176 Use of @var{initial} is mostly deprecated; we recommend using
177 a non-@code{nil} value only in conjunction with specifying a cons cell
178 for @var{history}. @xref{Initial Input}.
179 @end defun
180
181 @defun read-string prompt &optional initial history default inherit-input-method
182 This function reads a string from the minibuffer and returns it. The
183 arguments @var{prompt}, @var{initial}, @var{history} and
184 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
185 The keymap used is @code{minibuffer-local-map}.
186
187 The optional argument @var{default} is used as in
188 @code{read-from-minibuffer}, except that, if non-@code{nil}, it also
189 specifies a default value to return if the user enters null input. As
190 in @code{read-from-minibuffer} it should be a string, a list of
191 strings, or @code{nil}, which is equivalent to an empty string. When
192 @var{default} is a string, that string is the default value. When it
193 is a list of strings, the first string is the default value. (All
194 these strings are available to the user in the ``future minibuffer
195 history''.)
196
197 This function works by calling the
198 @code{read-from-minibuffer} function:
199
200 @smallexample
201 @group
202 (read-string @var{prompt} @var{initial} @var{history} @var{default} @var{inherit})
203 @equiv{}
204 (let ((value
205 (read-from-minibuffer @var{prompt} @var{initial} nil nil
206 @var{history} @var{default} @var{inherit})))
207 (if (and (equal value "") @var{default})
208 (if (consp @var{default}) (car @var{default}) @var{default})
209 value))
210 @end group
211 @end smallexample
212 @end defun
213
214 @defun read-regexp prompt &optional defaults history
215 This function reads a regular expression as a string from the
216 minibuffer and returns it. If the minibuffer prompt string
217 @var{prompt} does not end in @samp{:} (followed by optional
218 whitespace), the function adds @samp{: } to the end, preceded by the
219 default return value (see below), if that is non-empty.
220
221 The optional argument @var{defaults} controls the default value to
222 return if the user enters null input, and should be one of: a string;
223 @code{nil}, which is equivalent to an empty string; a list of strings;
224 or a symbol.
225
226 If @var{defaults} is a symbol, @code{read-regexp} consults the value
227 of the variable @code{read-regexp-defaults-function} (see below), and
228 if that is non-@code{nil} uses it in preference to @var{defaults}.
229 The value in this case should be either:
230
231 @itemize @minus
232 @item
233 @code{regexp-history-last}, which means to use the first element of
234 the appropriate minibuffer history list (see below).
235
236 @item
237 A function of no arguments, whose return value (which should be
238 @code{nil}, a string, or a list of strings) becomes the value of
239 @var{defaults}.
240 @end itemize
241
242 @code{read-regexp} now ensures that the result of processing
243 @var{defaults} is a list (i.e., if the value is @code{nil} or a
244 string, it converts it to a list of one element). To this list,
245 @code{read-regexp} then appends a few potentially useful candidates for
246 input. These are:
247
248 @itemize @minus
249 @item
250 The word or symbol at point.
251 @item
252 The last regexp used in an incremental search.
253 @item
254 The last string used in an incremental search.
255 @item
256 The last string or pattern used in query-replace commands.
257 @end itemize
258
259 The function now has a list of regular expressions that it passes to
260 @code{read-from-minibuffer} to obtain the user's input. The first
261 element of the list is the default result in case of empty input. All
262 elements of the list are available to the user as the ``future
263 minibuffer history list'' (@pxref{Minibuffer History, future list,,
264 emacs, The GNU Emacs Manual}).
265
266 The optional argument @var{history}, if non-@code{nil}, is a symbol
267 specifying a minibuffer history list to use (@pxref{Minibuffer
268 History}). If it is omitted or @code{nil}, the history list defaults
269 to @code{regexp-history}.
270 @end defun
271
272 @defvar read-regexp-defaults-function
273 The function @code{read-regexp} may use the value of this variable to
274 determine its list of default regular expressions. If non-@code{nil},
275 the value of this variable should be either:
276
277 @itemize @minus
278 @item
279 The symbol @code{regexp-history-last}.
280
281 @item
282 A function of no arguments that returns either @code{nil}, a string,
283 or a list of strings.
284 @end itemize
285
286 @noindent
287 See @code{read-regexp} above for details of how these values are used.
288 @end defvar
289
290 @defvar minibuffer-allow-text-properties
291 If this variable is @code{nil}, then @code{read-from-minibuffer}
292 and @code{read-string} strip all text properties from the minibuffer
293 input before returning it. However,
294 @code{read-no-blanks-input} (see below), as well as
295 @code{read-minibuffer} and related functions (@pxref{Object from
296 Minibuffer,, Reading Lisp Objects With the Minibuffer}), and all
297 functions that do minibuffer input with completion, discard text
298 properties unconditionally, regardless of the value of this variable.
299 @end defvar
300
301 @defvar minibuffer-local-map
302 This
303 @anchor{Definition of minibuffer-local-map}
304 @c avoid page break at anchor; work around Texinfo deficiency
305 is the default local keymap for reading from the minibuffer. By
306 default, it makes the following bindings:
307
308 @table @asis
309 @item @kbd{C-j}
310 @code{exit-minibuffer}
311
312 @item @key{RET}
313 @code{exit-minibuffer}
314
315 @item @kbd{C-g}
316 @code{abort-recursive-edit}
317
318 @item @kbd{M-n}
319 @itemx @key{DOWN}
320 @code{next-history-element}
321
322 @item @kbd{M-p}
323 @itemx @key{UP}
324 @code{previous-history-element}
325
326 @item @kbd{M-s}
327 @code{next-matching-history-element}
328
329 @item @kbd{M-r}
330 @code{previous-matching-history-element}
331
332 @ignore
333 @c Does not seem worth/appropriate mentioning.
334 @item @kbd{C-@key{TAB}}
335 @code{file-cache-minibuffer-complete}
336 @end ignore
337 @end table
338 @end defvar
339
340 @c In version 18, initial is required
341 @c Emacs 19 feature
342 @defun read-no-blanks-input prompt &optional initial inherit-input-method
343 This function reads a string from the minibuffer, but does not allow
344 whitespace characters as part of the input: instead, those characters
345 terminate the input. The arguments @var{prompt}, @var{initial}, and
346 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
347
348 This is a simplified interface to the @code{read-from-minibuffer}
349 function, and passes the value of the @code{minibuffer-local-ns-map}
350 keymap as the @var{keymap} argument for that function. Since the keymap
351 @code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
352 possible to put a space into the string, by quoting it.
353
354 This function discards text properties, regardless of the value of
355 @code{minibuffer-allow-text-properties}.
356
357 @smallexample
358 @group
359 (read-no-blanks-input @var{prompt} @var{initial})
360 @equiv{}
361 (let (minibuffer-allow-text-properties)
362 (read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map))
363 @end group
364 @end smallexample
365 @end defun
366
367 @c Slightly unfortunate name, suggesting it might be related to the
368 @c Nextstep port...
369 @defvar minibuffer-local-ns-map
370 This built-in variable is the keymap used as the minibuffer local keymap
371 in the function @code{read-no-blanks-input}. By default, it makes the
372 following bindings, in addition to those of @code{minibuffer-local-map}:
373
374 @table @asis
375 @item @key{SPC}
376 @cindex @key{SPC} in minibuffer
377 @code{exit-minibuffer}
378
379 @item @key{TAB}
380 @cindex @key{TAB} in minibuffer
381 @code{exit-minibuffer}
382
383 @item @kbd{?}
384 @cindex @kbd{?} in minibuffer
385 @code{self-insert-and-exit}
386 @end table
387 @end defvar
388
389 @node Object from Minibuffer
390 @section Reading Lisp Objects with the Minibuffer
391
392 This section describes functions for reading Lisp objects with the
393 minibuffer.
394
395 @defun read-minibuffer prompt &optional initial
396 This function reads a Lisp object using the minibuffer, and returns it
397 without evaluating it. The arguments @var{prompt} and @var{initial} are
398 used as in @code{read-from-minibuffer}.
399
400 This is a simplified interface to the
401 @code{read-from-minibuffer} function:
402
403 @smallexample
404 @group
405 (read-minibuffer @var{prompt} @var{initial})
406 @equiv{}
407 (let (minibuffer-allow-text-properties)
408 (read-from-minibuffer @var{prompt} @var{initial} nil t))
409 @end group
410 @end smallexample
411
412 Here is an example in which we supply the string @code{"(testing)"} as
413 initial input:
414
415 @smallexample
416 @group
417 (read-minibuffer
418 "Enter an expression: " (format "%s" '(testing)))
419
420 ;; @r{Here is how the minibuffer is displayed:}
421 @end group
422
423 @group
424 ---------- Buffer: Minibuffer ----------
425 Enter an expression: (testing)@point{}
426 ---------- Buffer: Minibuffer ----------
427 @end group
428 @end smallexample
429
430 @noindent
431 The user can type @key{RET} immediately to use the initial input as a
432 default, or can edit the input.
433 @end defun
434
435 @defun eval-minibuffer prompt &optional initial
436 This function reads a Lisp expression using the minibuffer, evaluates
437 it, then returns the result. The arguments @var{prompt} and
438 @var{initial} are used as in @code{read-from-minibuffer}.
439
440 This function simply evaluates the result of a call to
441 @code{read-minibuffer}:
442
443 @smallexample
444 @group
445 (eval-minibuffer @var{prompt} @var{initial})
446 @equiv{}
447 (eval (read-minibuffer @var{prompt} @var{initial}))
448 @end group
449 @end smallexample
450 @end defun
451
452 @defun edit-and-eval-command prompt form
453 This function reads a Lisp expression in the minibuffer, evaluates it,
454 then returns the result. The difference between this command and
455 @code{eval-minibuffer} is that here the initial @var{form} is not
456 optional and it is treated as a Lisp object to be converted to printed
457 representation rather than as a string of text. It is printed with
458 @code{prin1}, so if it is a string, double-quote characters (@samp{"})
459 appear in the initial text. @xref{Output Functions}.
460
461 In the following example, we offer the user an expression with initial
462 text that is already a valid form:
463
464 @smallexample
465 @group
466 (edit-and-eval-command "Please edit: " '(forward-word 1))
467
468 ;; @r{After evaluation of the preceding expression,}
469 ;; @r{the following appears in the minibuffer:}
470 @end group
471
472 @group
473 ---------- Buffer: Minibuffer ----------
474 Please edit: (forward-word 1)@point{}
475 ---------- Buffer: Minibuffer ----------
476 @end group
477 @end smallexample
478
479 @noindent
480 Typing @key{RET} right away would exit the minibuffer and evaluate the
481 expression, thus moving point forward one word.
482 @end defun
483
484 @node Minibuffer History
485 @section Minibuffer History
486 @cindex minibuffer history
487 @cindex history list
488
489 A @dfn{minibuffer history list} records previous minibuffer inputs
490 so the user can reuse them conveniently. It is a variable whose value
491 is a list of strings (previous inputs), most recent first.
492
493 There are many separate minibuffer history lists, used for different
494 kinds of inputs. It's the Lisp programmer's job to specify the right
495 history list for each use of the minibuffer.
496
497 You specify a minibuffer history list with the optional @var{history}
498 argument to @code{read-from-minibuffer} or @code{completing-read}.
499 Here are the possible values for it:
500
501 @table @asis
502 @item @var{variable}
503 Use @var{variable} (a symbol) as the history list.
504
505 @item (@var{variable} . @var{startpos})
506 Use @var{variable} (a symbol) as the history list, and assume that the
507 initial history position is @var{startpos} (a nonnegative integer).
508
509 Specifying 0 for @var{startpos} is equivalent to just specifying the
510 symbol @var{variable}. @code{previous-history-element} will display
511 the most recent element of the history list in the minibuffer. If you
512 specify a positive @var{startpos}, the minibuffer history functions
513 behave as if @code{(elt @var{variable} (1- @var{startpos}))} were the
514 history element currently shown in the minibuffer.
515
516 For consistency, you should also specify that element of the history
517 as the initial minibuffer contents, using the @var{initial} argument
518 to the minibuffer input function (@pxref{Initial Input}).
519 @end table
520
521 If you don't specify @var{history}, then the default history list
522 @code{minibuffer-history} is used. For other standard history lists,
523 see below. You can also create your own history list variable; just
524 initialize it to @code{nil} before the first use.
525
526 Both @code{read-from-minibuffer} and @code{completing-read} add new
527 elements to the history list automatically, and provide commands to
528 allow the user to reuse items on the list. The only thing your program
529 needs to do to use a history list is to initialize it and to pass its
530 name to the input functions when you wish. But it is safe to modify the
531 list by hand when the minibuffer input functions are not using it.
532
533 Emacs functions that add a new element to a history list can also
534 delete old elements if the list gets too long. The variable
535 @code{history-length} specifies the maximum length for most history
536 lists. To specify a different maximum length for a particular history
537 list, put the length in the @code{history-length} property of the
538 history list symbol. The variable @code{history-delete-duplicates}
539 specifies whether to delete duplicates in history.
540
541 @defun add-to-history history-var newelt &optional maxelt keep-all
542 This function adds a new element @var{newelt}, if it isn't the empty
543 string, to the history list stored in the variable @var{history-var},
544 and returns the updated history list. It limits the list length to
545 the value of @var{maxelt} (if non-@code{nil}) or @code{history-length}
546 (described below). The possible values of @var{maxelt} have the same
547 meaning as the values of @code{history-length}.
548
549 Normally, @code{add-to-history} removes duplicate members from the
550 history list if @code{history-delete-duplicates} is non-@code{nil}.
551 However, if @var{keep-all} is non-@code{nil}, that says not to remove
552 duplicates, and to add @var{newelt} to the list even if it is empty.
553 @end defun
554
555 @defvar history-add-new-input
556 If the value of this variable is @code{nil}, standard functions that
557 read from the minibuffer don't add new elements to the history list.
558 This lets Lisp programs explicitly manage input history by using
559 @code{add-to-history}. The default value is @code{t}.
560 @end defvar
561
562 @defopt history-length
563 The value of this variable specifies the maximum length for all
564 history lists that don't specify their own maximum lengths. If the
565 value is @code{t}, that means there is no maximum (don't delete old
566 elements). If a history list variable's symbol has a non-@code{nil}
567 @code{history-length} property, it overrides this variable for that
568 particular history list.
569 @end defopt
570
571 @defopt history-delete-duplicates
572 If the value of this variable is @code{t}, that means when adding a
573 new history element, all previous identical elements are deleted.
574 @end defopt
575
576 Here are some of the standard minibuffer history list variables:
577
578 @defvar minibuffer-history
579 The default history list for minibuffer history input.
580 @end defvar
581
582 @defvar query-replace-history
583 A history list for arguments to @code{query-replace} (and similar
584 arguments to other commands).
585 @end defvar
586
587 @defvar file-name-history
588 A history list for file-name arguments.
589 @end defvar
590
591 @defvar buffer-name-history
592 A history list for buffer-name arguments.
593 @end defvar
594
595 @defvar regexp-history
596 A history list for regular expression arguments.
597 @end defvar
598
599 @defvar extended-command-history
600 A history list for arguments that are names of extended commands.
601 @end defvar
602
603 @defvar shell-command-history
604 A history list for arguments that are shell commands.
605 @end defvar
606
607 @defvar read-expression-history
608 A history list for arguments that are Lisp expressions to evaluate.
609 @end defvar
610
611 @defvar face-name-history
612 A history list for arguments that are faces.
613 @end defvar
614
615 @c Less common: coding-system-history, input-method-history,
616 @c command-history, grep-history, grep-find-history,
617 @c read-envvar-name-history, setenv-history, yes-or-no-p-history.
618
619 @node Initial Input
620 @section Initial Input
621
622 Several of the functions for minibuffer input have an argument called
623 @var{initial}. This is a mostly-deprecated
624 feature for specifying that the minibuffer should start out with
625 certain text, instead of empty as usual.
626
627 If @var{initial} is a string, the minibuffer starts out containing the
628 text of the string, with point at the end, when the user starts to
629 edit the text. If the user simply types @key{RET} to exit the
630 minibuffer, it will use the initial input string to determine the
631 value to return.
632
633 @strong{We discourage use of a non-@code{nil} value for
634 @var{initial}}, because initial input is an intrusive interface.
635 History lists and default values provide a much more convenient method
636 to offer useful default inputs to the user.
637
638 There is just one situation where you should specify a string for an
639 @var{initial} argument. This is when you specify a cons cell for the
640 @var{history} argument. @xref{Minibuffer History}.
641
642 @var{initial} can also be a cons cell of the form @code{(@var{string}
643 . @var{position})}. This means to insert @var{string} in the
644 minibuffer but put point at @var{position} within the string's text.
645
646 As a historical accident, @var{position} was implemented
647 inconsistently in different functions. In @code{completing-read},
648 @var{position}'s value is interpreted as origin-zero; that is, a value
649 of 0 means the beginning of the string, 1 means after the first
650 character, etc. In @code{read-minibuffer}, and the other
651 non-completion minibuffer input functions that support this argument,
652 1 means the beginning of the string, 2 means after the first character,
653 etc.
654
655 Use of a cons cell as the value for @var{initial} arguments is deprecated.
656
657 @node Completion
658 @section Completion
659 @cindex completion
660
661 @dfn{Completion} is a feature that fills in the rest of a name
662 starting from an abbreviation for it. Completion works by comparing the
663 user's input against a list of valid names and determining how much of
664 the name is determined uniquely by what the user has typed. For
665 example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
666 @c "This is the sort of English up with which I will not put."
667 type the first few letters of the name of the buffer to which you wish
668 to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
669 extends the name as far as it can.
670
671 Standard Emacs commands offer completion for names of symbols, files,
672 buffers, and processes; with the functions in this section, you can
673 implement completion for other kinds of names.
674
675 The @code{try-completion} function is the basic primitive for
676 completion: it returns the longest determined completion of a given
677 initial string, with a given set of strings to match against.
678
679 The function @code{completing-read} provides a higher-level interface
680 for completion. A call to @code{completing-read} specifies how to
681 determine the list of valid names. The function then activates the
682 minibuffer with a local keymap that binds a few keys to commands useful
683 for completion. Other functions provide convenient simple interfaces
684 for reading certain kinds of names with completion.
685
686 @menu
687 * Basic Completion:: Low-level functions for completing strings.
688 * Minibuffer Completion:: Invoking the minibuffer with completion.
689 * Completion Commands:: Minibuffer commands that do completion.
690 * High-Level Completion:: Convenient special cases of completion
691 (reading buffer names, variable names, etc.).
692 * Reading File Names:: Using completion to read file names and
693 shell commands.
694 * Completion Variables:: Variables controlling completion behavior.
695 * Programmed Completion:: Writing your own completion function.
696 * Completion in Buffers:: Completing text in ordinary buffers.
697 @end menu
698
699 @node Basic Completion
700 @subsection Basic Completion Functions
701
702 The following completion functions have nothing in themselves to do
703 with minibuffers. We describe them here to keep them near the
704 higher-level completion features that do use the minibuffer.
705
706 @defun try-completion string collection &optional predicate
707 This function returns the longest common substring of all possible
708 completions of @var{string} in @var{collection}.
709
710 @cindex completion table
711 @var{collection} is called the @dfn{completion table}. Its value must
712 be a list of strings or cons cells, an obarray, a hash table, or a
713 completion function.
714
715 @code{try-completion} compares @var{string} against each of the
716 permissible completions specified by the completion table. If no
717 permissible completions match, it returns @code{nil}. If there is
718 just one matching completion, and the match is exact, it returns
719 @code{t}. Otherwise, it returns the longest initial sequence common
720 to all possible matching completions.
721
722 If @var{collection} is an list, the permissible completions are
723 specified by the elements of the list, each of which should be either
724 a string, or a cons cell whose @sc{car} is either a string or a symbol
725 (a symbol is converted to a string using @code{symbol-name}). If the
726 list contains elements of any other type, those are ignored.
727
728 @cindex obarray in completion
729 If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
730 of all symbols in the obarray form the set of permissible completions.
731
732 If @var{collection} is a hash table, then the keys that are strings
733 are the possible completions. Other keys are ignored.
734
735 You can also use a function as @var{collection}. Then the function is
736 solely responsible for performing completion; @code{try-completion}
737 returns whatever this function returns. The function is called with
738 three arguments: @var{string}, @var{predicate} and @code{nil} (the
739 third argument is so that the same function can be used
740 in @code{all-completions} and do the appropriate thing in either
741 case). @xref{Programmed Completion}.
742
743 If the argument @var{predicate} is non-@code{nil}, then it must be a
744 function of one argument, unless @var{collection} is a hash table, in
745 which case it should be a function of two arguments. It is used to
746 test each possible match, and the match is accepted only if
747 @var{predicate} returns non-@code{nil}. The argument given to
748 @var{predicate} is either a string or a cons cell (the @sc{car} of
749 which is a string) from the alist, or a symbol (@emph{not} a symbol
750 name) from the obarray. If @var{collection} is a hash table,
751 @var{predicate} is called with two arguments, the string key and the
752 associated value.
753
754 In addition, to be acceptable, a completion must also match all the
755 regular expressions in @code{completion-regexp-list}. (Unless
756 @var{collection} is a function, in which case that function has to
757 handle @code{completion-regexp-list} itself.)
758
759 In the first of the following examples, the string @samp{foo} is
760 matched by three of the alist @sc{car}s. All of the matches begin with
761 the characters @samp{fooba}, so that is the result. In the second
762 example, there is only one possible match, and it is exact, so the
763 return value is @code{t}.
764
765 @smallexample
766 @group
767 (try-completion
768 "foo"
769 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
770 @result{} "fooba"
771 @end group
772
773 @group
774 (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
775 @result{} t
776 @end group
777 @end smallexample
778
779 In the following example, numerous symbols begin with the characters
780 @samp{forw}, and all of them begin with the word @samp{forward}. In
781 most of the symbols, this is followed with a @samp{-}, but not in all,
782 so no more than @samp{forward} can be completed.
783
784 @smallexample
785 @group
786 (try-completion "forw" obarray)
787 @result{} "forward"
788 @end group
789 @end smallexample
790
791 Finally, in the following example, only two of the three possible
792 matches pass the predicate @code{test} (the string @samp{foobaz} is
793 too short). Both of those begin with the string @samp{foobar}.
794
795 @smallexample
796 @group
797 (defun test (s)
798 (> (length (car s)) 6))
799 @result{} test
800 @end group
801 @group
802 (try-completion
803 "foo"
804 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
805 'test)
806 @result{} "foobar"
807 @end group
808 @end smallexample
809 @end defun
810
811 @c Removed obsolete argument nospace.
812 @defun all-completions string collection &optional predicate
813 This function returns a list of all possible completions of
814 @var{string}. The arguments to this function
815 @c (aside from @var{nospace})
816 are the same as those of @code{try-completion}, and it
817 uses @code{completion-regexp-list} in the same way that
818 @code{try-completion} does.
819
820 @ignore
821 The optional argument @var{nospace} is obsolete. If it is
822 non-@code{nil}, completions that start with a space are ignored unless
823 @var{string} starts with a space.
824 @end ignore
825
826 If @var{collection} is a function, it is called with three arguments:
827 @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
828 returns whatever the function returns. @xref{Programmed Completion}.
829
830 Here is an example, using the function @code{test} shown in the
831 example for @code{try-completion}:
832
833 @smallexample
834 @group
835 (defun test (s)
836 (> (length (car s)) 6))
837 @result{} test
838 @end group
839
840 @group
841 (all-completions
842 "foo"
843 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
844 'test)
845 @result{} ("foobar1" "foobar2")
846 @end group
847 @end smallexample
848 @end defun
849
850 @defun test-completion string collection &optional predicate
851 @anchor{Definition of test-completion}
852 This function returns non-@code{nil} if @var{string} is a valid
853 completion alternative specified by @var{collection} and
854 @var{predicate}. The arguments are the same as in
855 @code{try-completion}. For instance, if @var{collection} is a list of
856 strings, this is true if @var{string} appears in the list and
857 @var{predicate} is satisfied.
858
859 This function uses @code{completion-regexp-list} in the same
860 way that @code{try-completion} does.
861
862 If @var{predicate} is non-@code{nil} and if @var{collection} contains
863 several strings that are equal to each other, as determined by
864 @code{compare-strings} according to @code{completion-ignore-case},
865 then @var{predicate} should accept either all or none of them.
866 Otherwise, the return value of @code{test-completion} is essentially
867 unpredictable.
868
869 If @var{collection} is a function, it is called with three arguments,
870 the values @var{string}, @var{predicate} and @code{lambda}; whatever
871 it returns, @code{test-completion} returns in turn.
872 @end defun
873
874 @defun completion-boundaries string collection predicate suffix
875 This function returns the boundaries of the field on which @var{collection}
876 will operate, assuming that @var{string} holds the text before point
877 and @var{suffix} holds the text after point.
878
879 Normally completion operates on the whole string, so for all normal
880 collections, this will always return @code{(0 . (length
881 @var{suffix}))}. But more complex completion such as completion on
882 files is done one field at a time. For example, completion of
883 @code{"/usr/sh"} will include @code{"/usr/share/"} but not
884 @code{"/usr/share/doc"} even if @code{"/usr/share/doc"} exists.
885 Also @code{all-completions} on @code{"/usr/sh"} will not include
886 @code{"/usr/share/"} but only @code{"share/"}. So if @var{string} is
887 @code{"/usr/sh"} and @var{suffix} is @code{"e/doc"},
888 @code{completion-boundaries} will return @code{(5 . 1)} which tells us
889 that the @var{collection} will only return completion information that
890 pertains to the area after @code{"/usr/"} and before @code{"/doc"}.
891 @end defun
892
893 If you store a completion alist in a variable, you should mark the
894 variable as ``risky'' by giving it a non-@code{nil}
895 @code{risky-local-variable} property. @xref{File Local Variables}.
896
897 @defvar completion-ignore-case
898 If the value of this variable is non-@code{nil}, case is not
899 considered significant in completion. Within @code{read-file-name},
900 this variable is overridden by
901 @code{read-file-name-completion-ignore-case} (@pxref{Reading File
902 Names}); within @code{read-buffer}, it is overridden by
903 @code{read-buffer-completion-ignore-case} (@pxref{High-Level
904 Completion}).
905 @end defvar
906
907 @defvar completion-regexp-list
908 This is a list of regular expressions. The completion functions only
909 consider a completion acceptable if it matches all regular expressions
910 in this list, with @code{case-fold-search} (@pxref{Searching and Case})
911 bound to the value of @code{completion-ignore-case}.
912 @end defvar
913
914 @defmac lazy-completion-table var fun
915 This macro provides a way to initialize the variable @var{var} as a
916 collection for completion in a lazy way, not computing its actual
917 contents until they are first needed. You use this macro to produce a
918 value that you store in @var{var}. The actual computation of the
919 proper value is done the first time you do completion using @var{var}.
920 It is done by calling @var{fun} with no arguments. The
921 value @var{fun} returns becomes the permanent value of @var{var}.
922
923 Here is an example:
924
925 @smallexample
926 (defvar foo (lazy-completion-table foo make-my-alist))
927 @end smallexample
928 @end defmac
929
930 @c FIXME? completion-table-with-context?
931 @findex completion-table-case-fold
932 @findex completion-table-in-turn
933 @findex completion-table-merge
934 @findex completion-table-subvert
935 @findex completion-table-with-quoting
936 @findex completion-table-with-predicate
937 @findex completion-table-with-terminator
938 @cindex completion table, modifying
939 @cindex completion tables, combining
940 There are several functions that take an existing completion table and
941 return a modified version. @code{completion-table-case-fold} returns
942 a case-insensitive table. @code{completion-table-in-turn} and
943 @code{completion-table-merge} combine multiple input tables in
944 different ways. @code{completion-table-subvert} alters a table to use
945 a different initial prefix. @code{completion-table-with-quoting}
946 returns a table suitable for operating on quoted text.
947 @code{completion-table-with-predicate} filters a table with a
948 predicate function. @code{completion-table-with-terminator} adds a
949 terminating string.
950
951
952 @node Minibuffer Completion
953 @subsection Completion and the Minibuffer
954 @cindex minibuffer completion
955 @cindex reading from minibuffer with completion
956
957 This section describes the basic interface for reading from the
958 minibuffer with completion.
959
960 @defun completing-read prompt collection &optional predicate require-match initial history default inherit-input-method
961 This function reads a string in the minibuffer, assisting the user by
962 providing completion. It activates the minibuffer with prompt
963 @var{prompt}, which must be a string.
964
965 The actual completion is done by passing the completion table
966 @var{collection} and the completion predicate @var{predicate} to the
967 function @code{try-completion} (@pxref{Basic Completion}). This
968 happens in certain commands bound in the local keymaps used for
969 completion. Some of these commands also call @code{test-completion}.
970 Thus, if @var{predicate} is non-@code{nil}, it should be compatible
971 with @var{collection} and @code{completion-ignore-case}.
972 @xref{Definition of test-completion}.
973
974 The value of the optional argument @var{require-match} determines how
975 the user may exit the minibuffer:
976
977 @itemize @bullet
978 @item
979 If @code{nil}, the usual minibuffer exit commands work regardless of
980 the input in the minibuffer.
981
982 @item
983 If @code{t}, the usual minibuffer exit commands won't exit unless the
984 input completes to an element of @var{collection}.
985
986 @item
987 If @code{confirm}, the user can exit with any input, but is asked for
988 confirmation if the input is not an element of @var{collection}.
989
990 @item
991 If @code{confirm-after-completion}, the user can exit with any input,
992 but is asked for confirmation if the preceding command was a
993 completion command (i.e., one of the commands in
994 @code{minibuffer-confirm-exit-commands}) and the resulting input is
995 not an element of @var{collection}. @xref{Completion Commands}.
996
997 @item
998 Any other value of @var{require-match} behaves like @code{t}, except
999 that the exit commands won't exit if it performs completion.
1000 @end itemize
1001
1002 However, empty input is always permitted, regardless of the value of
1003 @var{require-match}; in that case, @code{completing-read} returns the
1004 first element of @var{default}, if it is a list; @code{""}, if
1005 @var{default} is @code{nil}; or @var{default}. The string or strings
1006 in @var{default} are also available to the user through the history
1007 commands.
1008
1009 The function @code{completing-read} uses
1010 @code{minibuffer-local-completion-map} as the keymap if
1011 @var{require-match} is @code{nil}, and uses
1012 @code{minibuffer-local-must-match-map} if @var{require-match} is
1013 non-@code{nil}. @xref{Completion Commands}.
1014
1015 The argument @var{history} specifies which history list variable to use for
1016 saving the input and for minibuffer history commands. It defaults to
1017 @code{minibuffer-history}. @xref{Minibuffer History}.
1018
1019 The argument @var{initial} is mostly deprecated; we recommend using a
1020 non-@code{nil} value only in conjunction with specifying a cons cell
1021 for @var{history}. @xref{Initial Input}. For default input, use
1022 @var{default} instead.
1023
1024 If the argument @var{inherit-input-method} is non-@code{nil}, then the
1025 minibuffer inherits the current input method (@pxref{Input
1026 Methods}) and the setting of @code{enable-multibyte-characters}
1027 (@pxref{Text Representations}) from whichever buffer was current before
1028 entering the minibuffer.
1029
1030 If the variable @code{completion-ignore-case} is
1031 non-@code{nil}, completion ignores case when comparing the input
1032 against the possible matches. @xref{Basic Completion}. In this mode
1033 of operation, @var{predicate} must also ignore case, or you will get
1034 surprising results.
1035
1036 Here's an example of using @code{completing-read}:
1037
1038 @smallexample
1039 @group
1040 (completing-read
1041 "Complete a foo: "
1042 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
1043 nil t "fo")
1044 @end group
1045
1046 @group
1047 ;; @r{After evaluation of the preceding expression,}
1048 ;; @r{the following appears in the minibuffer:}
1049
1050 ---------- Buffer: Minibuffer ----------
1051 Complete a foo: fo@point{}
1052 ---------- Buffer: Minibuffer ----------
1053 @end group
1054 @end smallexample
1055
1056 @noindent
1057 If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
1058 @code{completing-read} returns @code{barfoo}.
1059
1060 The @code{completing-read} function binds variables to pass
1061 information to the commands that actually do completion.
1062 They are described in the following section.
1063 @end defun
1064
1065 @defvar completing-read-function
1066 The value of this variable must be a function, which is called by
1067 @code{completing-read} to actually do its work. It should accept the
1068 same arguments as @code{completing-read}. This can be bound to a
1069 different function to completely override the normal behavior of
1070 @code{completing-read}.
1071 @end defvar
1072
1073 @node Completion Commands
1074 @subsection Minibuffer Commands that Do Completion
1075
1076 This section describes the keymaps, commands and user options used
1077 in the minibuffer to do completion.
1078
1079 @defvar minibuffer-completion-table
1080 The value of this variable is the completion table used for completion
1081 in the minibuffer. This is the global variable that contains what
1082 @code{completing-read} passes to @code{try-completion}. It is used by
1083 minibuffer completion commands such as
1084 @code{minibuffer-complete-word}.
1085 @end defvar
1086
1087 @defvar minibuffer-completion-predicate
1088 This variable's value is the predicate that @code{completing-read}
1089 passes to @code{try-completion}. The variable is also used by the other
1090 minibuffer completion functions.
1091 @end defvar
1092
1093 @defvar minibuffer-completion-confirm
1094 This variable determines whether Emacs asks for confirmation before
1095 exiting the minibuffer; @code{completing-read} binds this variable,
1096 and the function @code{minibuffer-complete-and-exit} checks the value
1097 before exiting. If the value is @code{nil}, confirmation is not
1098 required. If the value is @code{confirm}, the user may exit with an
1099 input that is not a valid completion alternative, but Emacs asks for
1100 confirmation. If the value is @code{confirm-after-completion}, the
1101 user may exit with an input that is not a valid completion
1102 alternative, but Emacs asks for confirmation if the user submitted the
1103 input right after any of the completion commands in
1104 @code{minibuffer-confirm-exit-commands}.
1105 @end defvar
1106
1107 @defvar minibuffer-confirm-exit-commands
1108 This variable holds a list of commands that cause Emacs to ask for
1109 confirmation before exiting the minibuffer, if the @var{require-match}
1110 argument to @code{completing-read} is @code{confirm-after-completion}.
1111 The confirmation is requested if the user attempts to exit the
1112 minibuffer immediately after calling any command in this list.
1113 @end defvar
1114
1115 @deffn Command minibuffer-complete-word
1116 This function completes the minibuffer contents by at most a single
1117 word. Even if the minibuffer contents have only one completion,
1118 @code{minibuffer-complete-word} does not add any characters beyond the
1119 first character that is not a word constituent. @xref{Syntax Tables}.
1120 @end deffn
1121
1122 @deffn Command minibuffer-complete
1123 This function completes the minibuffer contents as far as possible.
1124 @end deffn
1125
1126 @deffn Command minibuffer-complete-and-exit
1127 This function completes the minibuffer contents, and exits if
1128 confirmation is not required, i.e., if
1129 @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
1130 @emph{is} required, it is given by repeating this command
1131 immediately---the command is programmed to work without confirmation
1132 when run twice in succession.
1133 @end deffn
1134
1135 @deffn Command minibuffer-completion-help
1136 This function creates a list of the possible completions of the
1137 current minibuffer contents. It works by calling @code{all-completions}
1138 using the value of the variable @code{minibuffer-completion-table} as
1139 the @var{collection} argument, and the value of
1140 @code{minibuffer-completion-predicate} as the @var{predicate} argument.
1141 The list of completions is displayed as text in a buffer named
1142 @file{*Completions*}.
1143 @end deffn
1144
1145 @defun display-completion-list completions
1146 This function displays @var{completions} to the stream in
1147 @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
1148 information about streams.) The argument @var{completions} is normally
1149 a list of completions just returned by @code{all-completions}, but it
1150 does not have to be. Each element may be a symbol or a string, either
1151 of which is simply printed. It can also be a list of two strings,
1152 which is printed as if the strings were concatenated. The first of
1153 the two strings is the actual completion, the second string serves as
1154 annotation.
1155
1156 This function is called by @code{minibuffer-completion-help}. A
1157 common way to use it is together with
1158 @code{with-output-to-temp-buffer}, like this:
1159
1160 @example
1161 (with-output-to-temp-buffer "*Completions*"
1162 (display-completion-list
1163 (all-completions (buffer-string) my-alist)))
1164 @end example
1165 @end defun
1166
1167 @defopt completion-auto-help
1168 If this variable is non-@code{nil}, the completion commands
1169 automatically display a list of possible completions whenever nothing
1170 can be completed because the next character is not uniquely determined.
1171 @end defopt
1172
1173 @defvar minibuffer-local-completion-map
1174 @code{completing-read} uses this value as the local keymap when an
1175 exact match of one of the completions is not required. By default, this
1176 keymap makes the following bindings:
1177
1178 @table @asis
1179 @item @kbd{?}
1180 @code{minibuffer-completion-help}
1181
1182 @item @key{SPC}
1183 @code{minibuffer-complete-word}
1184
1185 @item @key{TAB}
1186 @code{minibuffer-complete}
1187 @end table
1188
1189 @noindent
1190 and uses @code{minibuffer-local-map} as its parent keymap
1191 (@pxref{Definition of minibuffer-local-map}).
1192 @end defvar
1193
1194 @defvar minibuffer-local-must-match-map
1195 @code{completing-read} uses this value as the local keymap when an
1196 exact match of one of the completions is required. Therefore, no keys
1197 are bound to @code{exit-minibuffer}, the command that exits the
1198 minibuffer unconditionally. By default, this keymap makes the following
1199 bindings:
1200
1201 @table @asis
1202 @item @kbd{C-j}
1203 @code{minibuffer-complete-and-exit}
1204
1205 @item @key{RET}
1206 @code{minibuffer-complete-and-exit}
1207 @end table
1208
1209 @noindent
1210 and uses @code{minibuffer-local-completion-map} as its parent keymap.
1211 @end defvar
1212
1213 @defvar minibuffer-local-filename-completion-map
1214 This is a sparse keymap that simply unbinds @key{SPC}; because
1215 filenames can contain spaces. The function @code{read-file-name}
1216 combines this keymap with either @code{minibuffer-local-completion-map}
1217 or @code{minibuffer-local-must-match-map}.
1218 @end defvar
1219
1220
1221 @node High-Level Completion
1222 @subsection High-Level Completion Functions
1223
1224 This section describes the higher-level convenience functions for
1225 reading certain sorts of names with completion.
1226
1227 In most cases, you should not call these functions in the middle of a
1228 Lisp function. When possible, do all minibuffer input as part of
1229 reading the arguments for a command, in the @code{interactive}
1230 specification. @xref{Defining Commands}.
1231
1232 @defun read-buffer prompt &optional default require-match
1233 This function reads the name of a buffer and returns it as a string.
1234 The argument @var{default} is the default name to use, the value to
1235 return if the user exits with an empty minibuffer. If non-@code{nil},
1236 it should be a string, a list of strings, or a buffer. If it is
1237 a list, the default value is the first element of this list. It is
1238 mentioned in the prompt, but is not inserted in the minibuffer as
1239 initial input.
1240
1241 The argument @var{prompt} should be a string ending with a colon and a
1242 space. If @var{default} is non-@code{nil}, the function inserts it in
1243 @var{prompt} before the colon to follow the convention for reading from
1244 the minibuffer with a default value (@pxref{Programming Tips}).
1245
1246 The optional argument @var{require-match} has the same meaning as in
1247 @code{completing-read}. @xref{Minibuffer Completion}.
1248
1249 In the following example, the user enters @samp{minibuffer.t}, and
1250 then types @key{RET}. The argument @var{require-match} is @code{t},
1251 and the only buffer name starting with the given input is
1252 @samp{minibuffer.texi}, so that name is the value.
1253
1254 @example
1255 (read-buffer "Buffer name: " "foo" t)
1256 @group
1257 ;; @r{After evaluation of the preceding expression,}
1258 ;; @r{the following prompt appears,}
1259 ;; @r{with an empty minibuffer:}
1260 @end group
1261
1262 @group
1263 ---------- Buffer: Minibuffer ----------
1264 Buffer name (default foo): @point{}
1265 ---------- Buffer: Minibuffer ----------
1266 @end group
1267
1268 @group
1269 ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
1270 @result{} "minibuffer.texi"
1271 @end group
1272 @end example
1273 @end defun
1274
1275 @defopt read-buffer-function
1276 This variable, if non-@code{nil}, specifies a function for reading
1277 buffer names. @code{read-buffer} calls this function instead of doing
1278 its usual work, with the same arguments passed to @code{read-buffer}.
1279 @end defopt
1280
1281 @defopt read-buffer-completion-ignore-case
1282 If this variable is non-@code{nil}, @code{read-buffer} ignores case
1283 when performing completion.
1284 @end defopt
1285
1286 @defun read-command prompt &optional default
1287 This function reads the name of a command and returns it as a Lisp
1288 symbol. The argument @var{prompt} is used as in
1289 @code{read-from-minibuffer}. Recall that a command is anything for
1290 which @code{commandp} returns @code{t}, and a command name is a symbol
1291 for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
1292
1293 The argument @var{default} specifies what to return if the user enters
1294 null input. It can be a symbol, a string or a list of strings. If it
1295 is a string, @code{read-command} interns it before returning it.
1296 If it is a list, @code{read-command} interns the first element of this list.
1297 If @var{default} is @code{nil}, that means no default has been
1298 specified; then if the user enters null input, the return value is
1299 @code{(intern "")}, that is, a symbol whose name is an empty string.
1300
1301 @example
1302 (read-command "Command name? ")
1303
1304 @group
1305 ;; @r{After evaluation of the preceding expression,}
1306 ;; @r{the following prompt appears with an empty minibuffer:}
1307 @end group
1308
1309 @group
1310 ---------- Buffer: Minibuffer ----------
1311 Command name?
1312 ---------- Buffer: Minibuffer ----------
1313 @end group
1314 @end example
1315
1316 @noindent
1317 If the user types @kbd{forward-c @key{RET}}, then this function returns
1318 @code{forward-char}.
1319
1320 The @code{read-command} function is a simplified interface to
1321 @code{completing-read}. It uses the variable @code{obarray} so as to
1322 complete in the set of extant Lisp symbols, and it uses the
1323 @code{commandp} predicate so as to accept only command names:
1324
1325 @cindex @code{commandp} example
1326 @example
1327 @group
1328 (read-command @var{prompt})
1329 @equiv{}
1330 (intern (completing-read @var{prompt} obarray
1331 'commandp t nil))
1332 @end group
1333 @end example
1334 @end defun
1335
1336 @defun read-variable prompt &optional default
1337 @anchor{Definition of read-variable}
1338 This function reads the name of a customizable variable and returns it
1339 as a symbol. Its arguments have the same form as those of
1340 @code{read-command}. It behaves just like @code{read-command}, except
1341 that it uses the predicate @code{custom-variable-p} instead of
1342 @code{commandp}.
1343 @end defun
1344
1345 @deffn Command read-color &optional prompt convert allow-empty display
1346 This function reads a string that is a color specification, either the
1347 color's name or an RGB hex value such as @code{#RRRGGGBBB}. It
1348 prompts with @var{prompt} (default: @code{"Color (name or #RGB triplet):"})
1349 and provides completion for color names, but not for hex RGB values.
1350 In addition to names of standard colors, completion candidates include
1351 the foreground and background colors at point.
1352
1353 Valid RGB values are described in @ref{Color Names}.
1354
1355 The function's return value is the string typed by the user in the
1356 minibuffer. However, when called interactively or if the optional
1357 argument @var{convert} is non-@code{nil}, it converts any input color
1358 name into the corresponding RGB value string and instead returns that.
1359 This function requires a valid color specification to be input.
1360 Empty color names are allowed when @var{allow-empty} is
1361 non-@code{nil} and the user enters null input.
1362
1363 Interactively, or when @var{display} is non-@code{nil}, the return
1364 value is also displayed in the echo area.
1365 @end deffn
1366
1367 See also the functions @code{read-coding-system} and
1368 @code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems},
1369 and @code{read-input-method-name}, in @ref{Input Methods}.
1370
1371 @node Reading File Names
1372 @subsection Reading File Names
1373 @cindex read file names
1374 @cindex prompt for file name
1375
1376 The high-level completion functions @code{read-file-name},
1377 @code{read-directory-name}, and @code{read-shell-command} are designed
1378 to read file names, directory names, and shell commands, respectively.
1379 They provide special features, including automatic insertion of the
1380 default directory.
1381
1382 @defun read-file-name prompt &optional directory default require-match initial predicate
1383 This function reads a file name, prompting with @var{prompt} and
1384 providing completion.
1385
1386 As an exception, this function reads a file name using a graphical
1387 file dialog instead of the minibuffer, if all of the following are
1388 true:
1389
1390 @enumerate
1391 @item
1392 It is invoked via a mouse command.
1393
1394 @item
1395 The selected frame is on a graphical display supporting such dialogs.
1396
1397 @item
1398 The variable @code{use-dialog-box} is non-@code{nil}.
1399 @xref{Dialog Boxes,, Dialog Boxes, emacs, The GNU Emacs Manual}.
1400
1401 @item
1402 The @var{directory} argument, described below, does not specify a
1403 remote file. @xref{Remote Files,, Remote Files, emacs, The GNU Emacs Manual}.
1404 @end enumerate
1405
1406 @noindent
1407 The exact behavior when using a graphical file dialog is
1408 platform-dependent. Here, we simply document the behavior when using
1409 the minibuffer.
1410
1411 @code{read-file-name} does not automatically expand the returned file
1412 name. You must call @code{expand-file-name} yourself if an absolute
1413 file name is required.
1414
1415 The optional argument @var{require-match} has the same meaning as in
1416 @code{completing-read}. @xref{Minibuffer Completion}.
1417
1418 The argument @var{directory} specifies the directory to use for
1419 completing relative file names. It should be an absolute directory
1420 name. If the variable @code{insert-default-directory} is non-@code{nil},
1421 @var{directory} is also inserted in the minibuffer as initial input.
1422 It defaults to the current buffer's value of @code{default-directory}.
1423
1424 If you specify @var{initial}, that is an initial file name to insert
1425 in the buffer (after @var{directory}, if that is inserted). In this
1426 case, point goes at the beginning of @var{initial}. The default for
1427 @var{initial} is @code{nil}---don't insert any file name. To see what
1428 @var{initial} does, try the command @kbd{C-x C-v} in a buffer visiting
1429 a file. @strong{Please note:} we recommend using @var{default} rather
1430 than @var{initial} in most cases.
1431
1432 If @var{default} is non-@code{nil}, then the function returns
1433 @var{default} if the user exits the minibuffer with the same non-empty
1434 contents that @code{read-file-name} inserted initially. The initial
1435 minibuffer contents are always non-empty if
1436 @code{insert-default-directory} is non-@code{nil}, as it is by
1437 default. @var{default} is not checked for validity, regardless of the
1438 value of @var{require-match}. However, if @var{require-match} is
1439 non-@code{nil}, the initial minibuffer contents should be a valid file
1440 (or directory) name. Otherwise @code{read-file-name} attempts
1441 completion if the user exits without any editing, and does not return
1442 @var{default}. @var{default} is also available through the history
1443 commands.
1444
1445 If @var{default} is @code{nil}, @code{read-file-name} tries to find a
1446 substitute default to use in its place, which it treats in exactly the
1447 same way as if it had been specified explicitly. If @var{default} is
1448 @code{nil}, but @var{initial} is non-@code{nil}, then the default is
1449 the absolute file name obtained from @var{directory} and
1450 @var{initial}. If both @var{default} and @var{initial} are @code{nil}
1451 and the buffer is visiting a file, @code{read-file-name} uses the
1452 absolute file name of that file as default. If the buffer is not
1453 visiting a file, then there is no default. In that case, if the user
1454 types @key{RET} without any editing, @code{read-file-name} simply
1455 returns the pre-inserted contents of the minibuffer.
1456
1457 If the user types @key{RET} in an empty minibuffer, this function
1458 returns an empty string, regardless of the value of
1459 @var{require-match}. This is, for instance, how the user can make the
1460 current buffer visit no file using @kbd{M-x set-visited-file-name}.
1461
1462 If @var{predicate} is non-@code{nil}, it specifies a function of one
1463 argument that decides which file names are acceptable completion
1464 alternatives. A file name is an acceptable value if @var{predicate}
1465 returns non-@code{nil} for it.
1466
1467 Here is an example of using @code{read-file-name}:
1468
1469 @example
1470 @group
1471 (read-file-name "The file is ")
1472
1473 ;; @r{After evaluation of the preceding expression,}
1474 ;; @r{the following appears in the minibuffer:}
1475 @end group
1476
1477 @group
1478 ---------- Buffer: Minibuffer ----------
1479 The file is /gp/gnu/elisp/@point{}
1480 ---------- Buffer: Minibuffer ----------
1481 @end group
1482 @end example
1483
1484 @noindent
1485 Typing @kbd{manual @key{TAB}} results in the following:
1486
1487 @example
1488 @group
1489 ---------- Buffer: Minibuffer ----------
1490 The file is /gp/gnu/elisp/manual.texi@point{}
1491 ---------- Buffer: Minibuffer ----------
1492 @end group
1493 @end example
1494
1495 @c Wordy to avoid overfull hbox in smallbook mode.
1496 @noindent
1497 If the user types @key{RET}, @code{read-file-name} returns the file name
1498 as the string @code{"/gp/gnu/elisp/manual.texi"}.
1499 @end defun
1500
1501 @defvar read-file-name-function
1502 If non-@code{nil}, this should be a function that accepts the same
1503 arguments as @code{read-file-name}. When @code{read-file-name} is
1504 called, it calls this function with the supplied arguments instead of
1505 doing its usual work.
1506 @end defvar
1507
1508 @defopt read-file-name-completion-ignore-case
1509 If this variable is non-@code{nil}, @code{read-file-name} ignores case
1510 when performing completion.
1511 @end defopt
1512
1513 @defun read-directory-name prompt &optional directory default require-match initial
1514 This function is like @code{read-file-name} but allows only directory
1515 names as completion alternatives.
1516
1517 If @var{default} is @code{nil} and @var{initial} is non-@code{nil},
1518 @code{read-directory-name} constructs a substitute default by
1519 combining @var{directory} (or the current buffer's default directory
1520 if @var{directory} is @code{nil}) and @var{initial}. If both
1521 @var{default} and @var{initial} are @code{nil}, this function uses
1522 @var{directory} as substitute default, or the current buffer's default
1523 directory if @var{directory} is @code{nil}.
1524 @end defun
1525
1526 @defopt insert-default-directory
1527 This variable is used by @code{read-file-name}, and thus, indirectly,
1528 by most commands reading file names. (This includes all commands that
1529 use the code letters @samp{f} or @samp{F} in their interactive form.
1530 @xref{Interactive Codes,, Code Characters for interactive}.) Its
1531 value controls whether @code{read-file-name} starts by placing the
1532 name of the default directory in the minibuffer, plus the initial file
1533 name, if any. If the value of this variable is @code{nil}, then
1534 @code{read-file-name} does not place any initial input in the
1535 minibuffer (unless you specify initial input with the @var{initial}
1536 argument). In that case, the default directory is still used for
1537 completion of relative file names, but is not displayed.
1538
1539 If this variable is @code{nil} and the initial minibuffer contents are
1540 empty, the user may have to explicitly fetch the next history element
1541 to access a default value. If the variable is non-@code{nil}, the
1542 initial minibuffer contents are always non-empty and the user can
1543 always request a default value by immediately typing @key{RET} in an
1544 unedited minibuffer. (See above.)
1545
1546 For example:
1547
1548 @example
1549 @group
1550 ;; @r{Here the minibuffer starts out with the default directory.}
1551 (let ((insert-default-directory t))
1552 (read-file-name "The file is "))
1553 @end group
1554
1555 @group
1556 ---------- Buffer: Minibuffer ----------
1557 The file is ~lewis/manual/@point{}
1558 ---------- Buffer: Minibuffer ----------
1559 @end group
1560
1561 @group
1562 ;; @r{Here the minibuffer is empty and only the prompt}
1563 ;; @r{appears on its line.}
1564 (let ((insert-default-directory nil))
1565 (read-file-name "The file is "))
1566 @end group
1567
1568 @group
1569 ---------- Buffer: Minibuffer ----------
1570 The file is @point{}
1571 ---------- Buffer: Minibuffer ----------
1572 @end group
1573 @end example
1574 @end defopt
1575
1576 @defun read-shell-command prompt &optional initial history &rest args
1577 This function reads a shell command from the minibuffer, prompting
1578 with @var{prompt} and providing intelligent completion. It completes
1579 the first word of the command using candidates that are appropriate
1580 for command names, and the rest of the command words as file names.
1581
1582 This function uses @code{minibuffer-local-shell-command-map} as the
1583 keymap for minibuffer input. The @var{history} argument specifies the
1584 history list to use; if is omitted or @code{nil}, it defaults to
1585 @code{shell-command-history} (@pxref{Minibuffer History,
1586 shell-command-history}). The optional argument @var{initial}
1587 specifies the initial content of the minibuffer (@pxref{Initial
1588 Input}). The rest of @var{args}, if present, are used as the
1589 @var{default} and @var{inherit-input-method} arguments in
1590 @code{read-from-minibuffer} (@pxref{Text from Minibuffer}).
1591 @end defun
1592
1593 @defvar minibuffer-local-shell-command-map
1594 This keymap is used by @code{read-shell-command} for completing
1595 command and file names that are part of a shell command. It uses
1596 @code{minibuffer-local-map} as its parent keymap, and binds @key{TAB}
1597 to @code{completion-at-point}.
1598 @end defvar
1599
1600 @node Completion Variables
1601 @subsection Completion Variables
1602
1603 Here are some variables that can be used to alter the default
1604 completion behavior.
1605
1606 @cindex completion styles
1607 @defopt completion-styles
1608 The value of this variable is a list of completion style (symbols) to
1609 use for performing completion. A @dfn{completion style} is a set of
1610 rules for generating completions. Each symbol occurring this list
1611 must have a corresponding entry in @code{completion-styles-alist}.
1612 @end defopt
1613
1614 @defvar completion-styles-alist
1615 This variable stores a list of available completion styles. Each
1616 element in the list has the form
1617
1618 @example
1619 (@var{style} @var{try-completion} @var{all-completions} @var{doc})
1620 @end example
1621
1622 @noindent
1623 Here, @var{style} is the name of the completion style (a symbol),
1624 which may be used in the @code{completion-styles} variable to refer to
1625 this style; @var{try-completion} is the function that does the
1626 completion; @var{all-completions} is the function that lists the
1627 completions; and @var{doc} is a string describing the completion
1628 style.
1629
1630 The @var{try-completion} and @var{all-completions} functions should
1631 each accept four arguments: @var{string}, @var{collection},
1632 @var{predicate}, and @var{point}. The @var{string}, @var{collection},
1633 and @var{predicate} arguments have the same meanings as in
1634 @code{try-completion} (@pxref{Basic Completion}), and the @var{point}
1635 argument is the position of point within @var{string}. Each function
1636 should return a non-@code{nil} value if it performed its job, and
1637 @code{nil} if it did not (e.g., if there is no way to complete
1638 @var{string} according to the completion style).
1639
1640 When the user calls a completion command like
1641 @code{minibuffer-complete} (@pxref{Completion Commands}), Emacs looks
1642 for the first style listed in @code{completion-styles} and calls its
1643 @var{try-completion} function. If this function returns @code{nil},
1644 Emacs moves to the next listed completion style and calls its
1645 @var{try-completion} function, and so on until one of the
1646 @var{try-completion} functions successfully performs completion and
1647 returns a non-@code{nil} value. A similar procedure is used for
1648 listing completions, via the @var{all-completions} functions.
1649
1650 @xref{Completion Styles,,, emacs, The GNU Emacs Manual}, for a
1651 description of the available completion styles.
1652 @end defvar
1653
1654 @defopt completion-category-overrides
1655 This variable specifies special completion styles and other completion
1656 behaviors to use when completing certain types of text. Its value
1657 should be an alist with elements of the form @code{(@var{category}
1658 . @var{alist})}. @var{category} is a symbol describing what is being
1659 completed; currently, the @code{buffer}, @code{file}, and
1660 @code{unicode-name} categories are defined, but others can be defined
1661 via specialized completion functions (@pxref{Programmed Completion}).
1662 @var{alist} is an association list describing how completion should
1663 behave for the corresponding category. The following alist keys are
1664 supported:
1665
1666 @table @code
1667 @item styles
1668 The value should be a list of completion styles (symbols).
1669
1670 @item cycle
1671 The value should be a value for @code{completion-cycle-threshold}
1672 (@pxref{Completion Options,,, emacs, The GNU Emacs Manual}) for this
1673 category.
1674 @end table
1675
1676 @noindent
1677 Additional alist entries may be defined in the future.
1678 @end defopt
1679
1680 @defvar completion-extra-properties
1681 This variable is used to specify extra properties of the current
1682 completion command. It is intended to be let-bound by specialized
1683 completion commands. Its value should be a list of property and value
1684 pairs. The following properties are supported:
1685
1686 @table @code
1687 @item :annotation-function
1688 The value should be a function to add annotations in the completions
1689 buffer. This function must accept one argument, a completion, and
1690 should either return @code{nil} or a string to be displayed next to
1691 the completion.
1692
1693 @item :exit-function
1694 The value should be a function to run after performing completion.
1695 The function should accept two arguments, @var{string} and
1696 @var{status}, where @var{string} is the text to which the field was
1697 completed, and @var{status} indicates what kind of operation happened:
1698 @code{finished} if text is now complete, @code{sole} if the text
1699 cannot be further completed but completion is not finished, or
1700 @code{exact} if the text is a valid completion but may be further
1701 completed.
1702 @end table
1703 @end defvar
1704
1705 @node Programmed Completion
1706 @subsection Programmed Completion
1707 @cindex programmed completion
1708
1709 Sometimes it is not possible or convenient to create an alist or
1710 an obarray containing all the intended possible completions ahead
1711 of time. In such a case, you can supply your own function to compute
1712 the completion of a given string. This is called @dfn{programmed
1713 completion}. Emacs uses programmed completion when completing file
1714 names (@pxref{File Name Completion}), among many other cases.
1715
1716 To use this feature, pass a function as the @var{collection}
1717 argument to @code{completing-read}. The function
1718 @code{completing-read} arranges to pass your completion function along
1719 to @code{try-completion}, @code{all-completions}, and other basic
1720 completion functions, which will then let your function do all
1721 the work.
1722
1723 The completion function should accept three arguments:
1724
1725 @itemize @bullet
1726 @item
1727 The string to be completed.
1728
1729 @item
1730 A predicate function with which to filter possible matches, or
1731 @code{nil} if none. The function should call the predicate for each
1732 possible match, and ignore the match if the predicate returns
1733 @code{nil}.
1734
1735 @item
1736 A flag specifying the type of completion operation to perform. This
1737 flag may be one of the following values.
1738
1739 @table @code
1740 @item nil
1741 This specifies a @code{try-completion} operation. The function should
1742 return @code{t} if the specified string is a unique and exact match;
1743 if there is more than one match, it should return the common substring
1744 of all matches (if the string is an exact match for one completion
1745 alternative but also matches other longer alternatives, the return
1746 value is the string); if there are no matches, it should return
1747 @code{nil}.
1748
1749 @item t
1750 This specifies an @code{all-completions} operation. The function
1751 should return a list of all possible completions of the specified
1752 string.
1753
1754 @item lambda
1755 This specifies a @code{test-completion} operation. The function
1756 should return @code{t} if the specified string is an exact match for
1757 some completion alternative; @code{nil} otherwise.
1758
1759 @item (boundaries . @var{suffix})
1760 This specifies a @code{completion-boundaries} operation. The function
1761 should return @code{(boundaries @var{start} . @var{end})}, where
1762 @var{start} is the position of the beginning boundary in the specified
1763 string, and @var{end} is the position of the end boundary in
1764 @var{suffix}.
1765
1766 @item metadata
1767 This specifies a request for information about the state of the
1768 current completion. The return value should have the form
1769 @code{(metadata . @var{alist})}, where @var{alist} is an alist whose
1770 elements are described below.
1771 @end table
1772
1773 @noindent
1774 If the flag has any other value, the completion function should return
1775 @code{nil}.
1776 @end itemize
1777
1778 The following is a list of metadata entries that a completion function
1779 may return in response to a @code{metadata} flag argument:
1780
1781 @table @code
1782 @item category
1783 The value should be a symbol describing what kind of text the
1784 completion function is trying to complete. If the symbol matches one
1785 of the keys in @code{completion-category-overrides}, the usual
1786 completion behavior is overridden. @xref{Completion Variables}.
1787
1788 @item annotation-function
1789 The value should be a function for @dfn{annotating} completions. The
1790 function should take one argument, @var{string}, which is a possible
1791 completion. It should return a string, which is displayed after the
1792 completion @var{string} in the @file{*Completions*} buffer.
1793
1794 @item display-sort-function
1795 The value should be a function for sorting completions. The function
1796 should take one argument, a list of completion strings, and return a
1797 sorted list of completion strings. It is allowed to alter the input
1798 list destructively.
1799
1800 @item cycle-sort-function
1801 The value should be a function for sorting completions, when
1802 @code{completion-cycle-threshold} is non-@code{nil} and the user is
1803 cycling through completion alternatives. @xref{Completion Options,,,
1804 emacs, The GNU Emacs Manual}. Its argument list and return value are
1805 the same as for @code{display-sort-function}.
1806 @end table
1807
1808 @defun completion-table-dynamic function
1809 This function is a convenient way to write a function that can act as
1810 a programmed completion function. The argument @var{function} should be
1811 a function that takes one argument, a string, and returns an alist of
1812 possible completions of it. You can think of
1813 @code{completion-table-dynamic} as a transducer between that interface
1814 and the interface for programmed completion functions.
1815 @end defun
1816
1817 @defun completion-table-with-cache function &optional ignore-case
1818 This is a wrapper for @code{completion-table-dynamic} that saves the
1819 last argument-result pair. This means that multiple lookups with the
1820 same argument only need to call @var{function} once. This can be useful
1821 when a slow operation is involved, such as calling an external process.
1822 @end defun
1823
1824 @node Completion in Buffers
1825 @subsection Completion in Ordinary Buffers
1826 @cindex inline completion
1827
1828 @findex completion-at-point
1829 Although completion is usually done in the minibuffer, the
1830 completion facility can also be used on the text in ordinary Emacs
1831 buffers. In many major modes, in-buffer completion is performed by
1832 the @kbd{C-M-i} or @kbd{M-@key{TAB}} command, bound to
1833 @code{completion-at-point}. @xref{Symbol Completion,,, emacs, The GNU
1834 Emacs Manual}. This command uses the abnormal hook variable
1835 @code{completion-at-point-functions}:
1836
1837 @defvar completion-at-point-functions
1838 The value of this abnormal hook should be a list of functions, which
1839 are used to compute a completion table for completing the text at
1840 point. It can be used by major modes to provide mode-specific
1841 completion tables (@pxref{Major Mode Conventions}).
1842
1843 When the command @code{completion-at-point} runs, it calls the
1844 functions in the list one by one, without any argument. Each function
1845 should return @code{nil} if it is unable to produce a completion table
1846 for the text at point. Otherwise it should return a list of the form
1847
1848 @example
1849 (@var{start} @var{end} @var{collection} . @var{props})
1850 @end example
1851
1852 @noindent
1853 @var{start} and @var{end} delimit the text to complete (which should
1854 enclose point). @var{collection} is a completion table for completing
1855 that text, in a form suitable for passing as the second argument to
1856 @code{try-completion} (@pxref{Basic Completion}); completion
1857 alternatives will be generated from this completion table in the usual
1858 way, via the completion styles defined in @code{completion-styles}
1859 (@pxref{Completion Variables}). @var{props} is a property list for
1860 additional information; any of the properties in
1861 @code{completion-extra-properties} are recognized (@pxref{Completion
1862 Variables}), as well as the following additional ones:
1863
1864 @table @code
1865 @item :predicate
1866 The value should be a predicate that completion candidates need to
1867 satisfy.
1868
1869 @item :exclusive
1870 If the value is @code{no}, then if the completion table fails to match
1871 the text at point, @code{completion-at-point} moves on to the
1872 next function in @code{completion-at-point-functions} instead of
1873 reporting a completion failure.
1874 @end table
1875
1876 Supplying a function for @var{collection} is strongly recommended if
1877 generating the list of completions is an expensive operation. Emacs
1878 may internally call functions in @code{completion-at-point-functions}
1879 many times, but care about the value of @var{collection} for only some
1880 of these calls. By supplying a function for @var{collection}, Emacs
1881 can defer generating completions until necessary. You can use
1882 @var{completion-table-dynamic} to create a wrapper function:
1883
1884 @smallexample
1885 ;; Avoid this pattern.
1886 (let ((beg ...) (end ...) (my-completions (my-make-completions)))
1887 (list beg end my-completions))
1888
1889 ;; Use this instead.
1890 (let ((beg ...) (end ...))
1891 (list beg
1892 end
1893 (completion-table-dynamic
1894 (lambda (_)
1895 (my-make-completions)))))
1896 @end smallexample
1897
1898 A function in @code{completion-at-point-functions} may also return a
1899 function instead of a list as described above. In that case, that
1900 returned function is called, with no argument, and it is entirely
1901 responsible for performing the completion. We discourage this usage;
1902 it is intended to help convert old code to using
1903 @code{completion-at-point}.
1904
1905 The first function in @code{completion-at-point-functions} to return a
1906 non-@code{nil} value is used by @code{completion-at-point}. The
1907 remaining functions are not called. The exception to this is when
1908 there is an @code{:exclusive} specification, as described above.
1909 @end defvar
1910
1911 The following function provides a convenient way to perform
1912 completion on an arbitrary stretch of text in an Emacs buffer:
1913
1914 @defun completion-in-region start end collection &optional predicate
1915 This function completes the text in the current buffer between the
1916 positions @var{start} and @var{end}, using @var{collection}. The
1917 argument @var{collection} has the same meaning as in
1918 @code{try-completion} (@pxref{Basic Completion}).
1919
1920 This function inserts the completion text directly into the current
1921 buffer. Unlike @code{completing-read} (@pxref{Minibuffer
1922 Completion}), it does not activate the minibuffer.
1923
1924 For this function to work, point must be somewhere between @var{start}
1925 and @var{end}.
1926 @end defun
1927
1928
1929 @node Yes-or-No Queries
1930 @section Yes-or-No Queries
1931 @cindex asking the user questions
1932 @cindex querying the user
1933 @cindex yes-or-no questions
1934
1935 This section describes functions used to ask the user a yes-or-no
1936 question. The function @code{y-or-n-p} can be answered with a single
1937 character; it is useful for questions where an inadvertent wrong answer
1938 will not have serious consequences. @code{yes-or-no-p} is suitable for
1939 more momentous questions, since it requires three or four characters to
1940 answer.
1941
1942 If either of these functions is called in a command that was invoked
1943 using the mouse---more precisely, if @code{last-nonmenu-event}
1944 (@pxref{Command Loop Info}) is either @code{nil} or a list---then it
1945 uses a dialog box or pop-up menu to ask the question. Otherwise, it
1946 uses keyboard input. You can force use either of the mouse or of keyboard
1947 input by binding @code{last-nonmenu-event} to a suitable value around
1948 the call.
1949
1950 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
1951 @code{y-or-n-p} does not; but it seems best to describe them together.
1952
1953 @defun y-or-n-p prompt
1954 This function asks the user a question, expecting input in the echo
1955 area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
1956 user types @kbd{n}. This function also accepts @key{SPC} to mean yes
1957 and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit'', like
1958 @kbd{C-g}, because the question might look like a minibuffer and for
1959 that reason the user might try to use @kbd{C-]} to get out. The answer
1960 is a single character, with no @key{RET} needed to terminate it. Upper
1961 and lower case are equivalent.
1962
1963 ``Asking the question'' means printing @var{prompt} in the echo area,
1964 followed by the string @w{@samp{(y or n) }}. If the input is not one of
1965 the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
1966 @kbd{@key{DEL}}, or something that quits), the function responds
1967 @samp{Please answer y or n.}, and repeats the request.
1968
1969 This function does not actually use the minibuffer, since it does not
1970 allow editing of the answer. It actually uses the echo area (@pxref{The
1971 Echo Area}), which uses the same screen space as the minibuffer. The
1972 cursor moves to the echo area while the question is being asked.
1973
1974 The answers and their meanings, even @samp{y} and @samp{n}, are not
1975 hardwired, and are specified by the keymap @code{query-replace-map}
1976 (@pxref{Search and Replace}). In particular, if the user enters the
1977 special responses @code{recenter}, @code{scroll-up},
1978 @code{scroll-down}, @code{scroll-other-window}, or
1979 @code{scroll-other-window-down} (respectively bound to @kbd{C-l},
1980 @kbd{C-v}, @kbd{M-v}, @kbd{C-M-v} and @kbd{C-M-S-v} in
1981 @code{query-replace-map}), this function performs the specified window
1982 recentering or scrolling operation, and poses the question again.
1983
1984 @noindent
1985 We show successive lines of echo area messages, but only one actually
1986 appears on the screen at a time.
1987 @end defun
1988
1989 @defun y-or-n-p-with-timeout prompt seconds default
1990 Like @code{y-or-n-p}, except that if the user fails to answer within
1991 @var{seconds} seconds, this function stops waiting and returns
1992 @var{default}. It works by setting up a timer; see @ref{Timers}.
1993 The argument @var{seconds} should be a number.
1994 @end defun
1995
1996 @defun yes-or-no-p prompt
1997 This function asks the user a question, expecting input in the
1998 minibuffer. It returns @code{t} if the user enters @samp{yes},
1999 @code{nil} if the user types @samp{no}. The user must type @key{RET} to
2000 finalize the response. Upper and lower case are equivalent.
2001
2002 @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
2003 followed by @w{@samp{(yes or no) }}. The user must type one of the
2004 expected responses; otherwise, the function responds @samp{Please answer
2005 yes or no.}, waits about two seconds and repeats the request.
2006
2007 @code{yes-or-no-p} requires more work from the user than
2008 @code{y-or-n-p} and is appropriate for more crucial decisions.
2009
2010 Here is an example:
2011
2012 @smallexample
2013 @group
2014 (yes-or-no-p "Do you really want to remove everything? ")
2015
2016 ;; @r{After evaluation of the preceding expression,}
2017 ;; @r{the following prompt appears,}
2018 ;; @r{with an empty minibuffer:}
2019 @end group
2020
2021 @group
2022 ---------- Buffer: minibuffer ----------
2023 Do you really want to remove everything? (yes or no)
2024 ---------- Buffer: minibuffer ----------
2025 @end group
2026 @end smallexample
2027
2028 @noindent
2029 If the user first types @kbd{y @key{RET}}, which is invalid because this
2030 function demands the entire word @samp{yes}, it responds by displaying
2031 these prompts, with a brief pause between them:
2032
2033 @smallexample
2034 @group
2035 ---------- Buffer: minibuffer ----------
2036 Please answer yes or no.
2037 Do you really want to remove everything? (yes or no)
2038 ---------- Buffer: minibuffer ----------
2039 @end group
2040 @end smallexample
2041 @end defun
2042
2043 @node Multiple Queries
2044 @section Asking Multiple Y-or-N Questions
2045
2046 When you have a series of similar questions to ask, such as ``Do you
2047 want to save this buffer'' for each buffer in turn, you should use
2048 @code{map-y-or-n-p} to ask the collection of questions, rather than
2049 asking each question individually. This gives the user certain
2050 convenient facilities such as the ability to answer the whole series at
2051 once.
2052
2053 @defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
2054 This function asks the user a series of questions, reading a
2055 single-character answer in the echo area for each one.
2056
2057 The value of @var{list} specifies the objects to ask questions about.
2058 It should be either a list of objects or a generator function. If it is
2059 a function, it should expect no arguments, and should return either the
2060 next object to ask about, or @code{nil}, meaning to stop asking questions.
2061
2062 The argument @var{prompter} specifies how to ask each question. If
2063 @var{prompter} is a string, the question text is computed like this:
2064
2065 @example
2066 (format @var{prompter} @var{object})
2067 @end example
2068
2069 @noindent
2070 where @var{object} is the next object to ask about (as obtained from
2071 @var{list}).
2072
2073 If not a string, @var{prompter} should be a function of one argument
2074 (the next object to ask about) and should return the question text. If
2075 the value is a string, that is the question to ask the user. The
2076 function can also return @code{t}, meaning do act on this object (and
2077 don't ask the user), or @code{nil}, meaning ignore this object (and don't
2078 ask the user).
2079
2080 The argument @var{actor} says how to act on the answers that the user
2081 gives. It should be a function of one argument, and it is called with
2082 each object that the user says yes for. Its argument is always an
2083 object obtained from @var{list}.
2084
2085 If the argument @var{help} is given, it should be a list of this form:
2086
2087 @example
2088 (@var{singular} @var{plural} @var{action})
2089 @end example
2090
2091 @noindent
2092 where @var{singular} is a string containing a singular noun that
2093 describes the objects conceptually being acted on, @var{plural} is the
2094 corresponding plural noun, and @var{action} is a transitive verb
2095 describing what @var{actor} does.
2096
2097 If you don't specify @var{help}, the default is @code{("object"
2098 "objects" "act on")}.
2099
2100 Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
2101 @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
2102 that object; @kbd{!} to act on all following objects; @key{ESC} or
2103 @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
2104 the current object and then exit; or @kbd{C-h} to get help. These are
2105 the same answers that @code{query-replace} accepts. The keymap
2106 @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
2107 as well as for @code{query-replace}; see @ref{Search and Replace}.
2108
2109 You can use @var{action-alist} to specify additional possible answers
2110 and what they mean. It is an alist of elements of the form
2111 @code{(@var{char} @var{function} @var{help})}, each of which defines one
2112 additional answer. In this element, @var{char} is a character (the
2113 answer); @var{function} is a function of one argument (an object from
2114 @var{list}); @var{help} is a string.
2115
2116 When the user responds with @var{char}, @code{map-y-or-n-p} calls
2117 @var{function}. If it returns non-@code{nil}, the object is considered
2118 ``acted upon'', and @code{map-y-or-n-p} advances to the next object in
2119 @var{list}. If it returns @code{nil}, the prompt is repeated for the
2120 same object.
2121
2122 Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
2123 prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
2124 does not do that.
2125
2126 If @code{map-y-or-n-p} is called in a command that was invoked using the
2127 mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
2128 Loop Info}) is either @code{nil} or a list---then it uses a dialog box
2129 or pop-up menu to ask the question. In this case, it does not use
2130 keyboard input or the echo area. You can force use either of the mouse or
2131 of keyboard input by binding @code{last-nonmenu-event} to a suitable
2132 value around the call.
2133
2134 The return value of @code{map-y-or-n-p} is the number of objects acted on.
2135 @end defun
2136 @c FIXME An example of this would be more useful than all the
2137 @c preceding examples of simple things.
2138
2139 @node Reading a Password
2140 @section Reading a Password
2141 @cindex passwords, reading
2142
2143 To read a password to pass to another program, you can use the
2144 function @code{read-passwd}.
2145
2146 @defun read-passwd prompt &optional confirm default
2147 This function reads a password, prompting with @var{prompt}. It does
2148 not echo the password as the user types it; instead, it echoes @samp{.}
2149 for each character in the password.
2150
2151 The optional argument @var{confirm}, if non-@code{nil}, says to read the
2152 password twice and insist it must be the same both times. If it isn't
2153 the same, the user has to type it over and over until the last two
2154 times match.
2155
2156 The optional argument @var{default} specifies the default password to
2157 return if the user enters empty input. If @var{default} is @code{nil},
2158 then @code{read-passwd} returns the null string in that case.
2159 @end defun
2160
2161 @node Minibuffer Commands
2162 @section Minibuffer Commands
2163
2164 This section describes some commands meant for use in the
2165 minibuffer.
2166
2167 @deffn Command exit-minibuffer
2168 This command exits the active minibuffer. It is normally bound to
2169 keys in minibuffer local keymaps.
2170 @end deffn
2171
2172 @deffn Command self-insert-and-exit
2173 This command exits the active minibuffer after inserting the last
2174 character typed on the keyboard (found in @code{last-command-event};
2175 @pxref{Command Loop Info}).
2176 @end deffn
2177
2178 @deffn Command previous-history-element n
2179 This command replaces the minibuffer contents with the value of the
2180 @var{n}th previous (older) history element.
2181 @end deffn
2182
2183 @deffn Command next-history-element n
2184 This command replaces the minibuffer contents with the value of the
2185 @var{n}th more recent history element.
2186 @end deffn
2187
2188 @deffn Command previous-matching-history-element pattern n
2189 This command replaces the minibuffer contents with the value of the
2190 @var{n}th previous (older) history element that matches @var{pattern} (a
2191 regular expression).
2192 @end deffn
2193
2194 @deffn Command next-matching-history-element pattern n
2195 This command replaces the minibuffer contents with the value of the
2196 @var{n}th next (newer) history element that matches @var{pattern} (a
2197 regular expression).
2198 @end deffn
2199
2200 @deffn Command previous-complete-history-element n
2201 This command replaces the minibuffer contents with the value of the
2202 @var{n}th previous (older) history element that completes the current
2203 contents of the minibuffer before the point.
2204 @end deffn
2205
2206 @deffn Command next-complete-history-element n
2207 This command replaces the minibuffer contents with the value of the
2208 @var{n}th next (newer) history element that completes the current
2209 contents of the minibuffer before the point.
2210 @end deffn
2211
2212
2213 @node Minibuffer Windows
2214 @section Minibuffer Windows
2215 @cindex minibuffer windows
2216
2217 These functions access and select minibuffer windows
2218 and test whether they are active.
2219
2220 @defun active-minibuffer-window
2221 This function returns the currently active minibuffer window, or
2222 @code{nil} if there is none.
2223 @end defun
2224
2225 @defun minibuffer-window &optional frame
2226 @anchor{Definition of minibuffer-window}
2227 This function returns the minibuffer window used for frame @var{frame}.
2228 If @var{frame} is @code{nil}, that stands for the current frame. Note
2229 that the minibuffer window used by a frame need not be part of that
2230 frame---a frame that has no minibuffer of its own necessarily uses some
2231 other frame's minibuffer window.
2232 @end defun
2233
2234 @defun set-minibuffer-window window
2235 This function specifies @var{window} as the minibuffer window to use.
2236 This affects where the minibuffer is displayed if you put text in it
2237 without invoking the usual minibuffer commands. It has no effect on
2238 the usual minibuffer input functions because they all start by
2239 choosing the minibuffer window according to the current frame.
2240 @end defun
2241
2242 @c Emacs 19 feature
2243 @defun window-minibuffer-p &optional window
2244 This function returns non-@code{nil} if @var{window} is a minibuffer
2245 window.
2246 @var{window} defaults to the selected window.
2247 @end defun
2248
2249 It is not correct to determine whether a given window is a minibuffer by
2250 comparing it with the result of @code{(minibuffer-window)}, because
2251 there can be more than one minibuffer window if there is more than one
2252 frame.
2253
2254 @defun minibuffer-window-active-p window
2255 This function returns non-@code{nil} if @var{window} is the currently
2256 active minibuffer window.
2257 @end defun
2258
2259 @node Minibuffer Contents
2260 @section Minibuffer Contents
2261
2262 These functions access the minibuffer prompt and contents.
2263
2264 @defun minibuffer-prompt
2265 This function returns the prompt string of the currently active
2266 minibuffer. If no minibuffer is active, it returns @code{nil}.
2267 @end defun
2268
2269 @defun minibuffer-prompt-end
2270 This function returns the current
2271 position of the end of the minibuffer prompt, if a minibuffer is
2272 current. Otherwise, it returns the minimum valid buffer position.
2273 @end defun
2274
2275 @defun minibuffer-prompt-width
2276 This function returns the current display-width of the minibuffer
2277 prompt, if a minibuffer is current. Otherwise, it returns zero.
2278 @end defun
2279
2280 @defun minibuffer-contents
2281 This function returns the editable
2282 contents of the minibuffer (that is, everything except the prompt) as
2283 a string, if a minibuffer is current. Otherwise, it returns the
2284 entire contents of the current buffer.
2285 @end defun
2286
2287 @defun minibuffer-contents-no-properties
2288 This is like @code{minibuffer-contents}, except that it does not copy text
2289 properties, just the characters themselves. @xref{Text Properties}.
2290 @end defun
2291
2292 @defun delete-minibuffer-contents
2293 This function erases the editable contents of the minibuffer (that is,
2294 everything except the prompt), if a minibuffer is current. Otherwise,
2295 it erases the entire current buffer.
2296 @end defun
2297
2298 @node Recursive Mini
2299 @section Recursive Minibuffers
2300 @cindex recursive minibuffers
2301
2302 These functions and variables deal with recursive minibuffers
2303 (@pxref{Recursive Editing}):
2304
2305 @defun minibuffer-depth
2306 This function returns the current depth of activations of the
2307 minibuffer, a nonnegative integer. If no minibuffers are active, it
2308 returns zero.
2309 @end defun
2310
2311 @defopt enable-recursive-minibuffers
2312 If this variable is non-@code{nil}, you can invoke commands (such as
2313 @code{find-file}) that use minibuffers even while the minibuffer window
2314 is active. Such invocation produces a recursive editing level for a new
2315 minibuffer. The outer-level minibuffer is invisible while you are
2316 editing the inner one.
2317
2318 If this variable is @code{nil}, you cannot invoke minibuffer
2319 commands when the minibuffer window is active, not even if you switch to
2320 another window to do it.
2321 @end defopt
2322
2323 @c Emacs 19 feature
2324 If a command name has a property @code{enable-recursive-minibuffers}
2325 that is non-@code{nil}, then the command can use the minibuffer to read
2326 arguments even if it is invoked from the minibuffer. A command can
2327 also achieve this by binding @code{enable-recursive-minibuffers}
2328 to @code{t} in the interactive declaration (@pxref{Using Interactive}).
2329 The minibuffer command @code{next-matching-history-element} (normally
2330 @kbd{M-s} in the minibuffer) does the latter.
2331
2332 @node Minibuffer Misc
2333 @section Minibuffer Miscellany
2334
2335 @defun minibufferp &optional buffer-or-name
2336 This function returns non-@code{nil} if @var{buffer-or-name} is a
2337 minibuffer. If @var{buffer-or-name} is omitted, it tests the current
2338 buffer.
2339 @end defun
2340
2341 @defvar minibuffer-setup-hook
2342 This is a normal hook that is run whenever the minibuffer is entered.
2343 @xref{Hooks}.
2344 @end defvar
2345
2346 @defvar minibuffer-exit-hook
2347 This is a normal hook that is run whenever the minibuffer is exited.
2348 @xref{Hooks}.
2349 @end defvar
2350
2351 @defvar minibuffer-help-form
2352 @anchor{Definition of minibuffer-help-form}
2353 The current value of this variable is used to rebind @code{help-form}
2354 locally inside the minibuffer (@pxref{Help Functions}).
2355 @end defvar
2356
2357 @defvar minibuffer-scroll-window
2358 @anchor{Definition of minibuffer-scroll-window}
2359 If the value of this variable is non-@code{nil}, it should be a window
2360 object. When the function @code{scroll-other-window} is called in the
2361 minibuffer, it scrolls this window.
2362 @end defvar
2363
2364 @defun minibuffer-selected-window
2365 This function returns the window that was selected when the
2366 minibuffer was entered. If selected window is not a minibuffer
2367 window, it returns @code{nil}.
2368 @end defun
2369
2370 @defopt max-mini-window-height
2371 This variable specifies the maximum height for resizing minibuffer
2372 windows. If a float, it specifies a fraction of the height of the
2373 frame. If an integer, it specifies a number of lines.
2374 @end defopt
2375
2376 @vindex minibuffer-message-timeout
2377 @defun minibuffer-message string &rest args
2378 This function displays @var{string} temporarily at the end of the
2379 minibuffer text, for a few seconds, or until the next input event
2380 arrives, whichever comes first. The variable
2381 @code{minibuffer-message-timeout} specifies the number of seconds to
2382 wait in the absence of input. It defaults to 2. If @var{args} is
2383 non-@code{nil}, the actual message is obtained by passing @var{string}
2384 and @var{args} through @code{format}. @xref{Formatting Strings}.
2385 @end defun
2386
2387 @deffn Command minibuffer-inactive-mode
2388 This is the major mode used in inactive minibuffers. It uses
2389 keymap @code{minibuffer-inactive-mode-map}. This can be useful
2390 if the minibuffer is in a separate frame. @xref{Minibuffers and Frames}.
2391 @end deffn