Add arch taglines
[bpt/emacs.git] / lispref / minibuf.texi
CommitLineData
3e01fd9d
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
b3b044f2 3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001
177c0ea7 4@c Free Software Foundation, Inc.
3e01fd9d
RS
5@c See the file elisp.texi for copying conditions.
6@setfilename ../info/minibuf
3e099569 7@node Minibuffers, Command Loop, Read and Print, Top
3e01fd9d
RS
8@chapter Minibuffers
9@cindex arguments, reading
10@cindex complex arguments
11@cindex minibuffer
12
13 A @dfn{minibuffer} is a special buffer that Emacs commands use to read
14arguments more complicated than the single numeric prefix argument.
15These arguments include file names, buffer names, and command names (as
16in @kbd{M-x}). The minibuffer is displayed on the bottom line of the
969fe9b5
RS
17frame, in the same place as the echo area, but only while it is in use
18for reading an argument.
3e01fd9d
RS
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* Completion:: How to invoke and customize completion.
27* Yes-or-No Queries:: Asking a question with a simple answer.
28* Multiple Queries:: Asking a series of similar questions.
e75ecfec 29* Reading a Password:: Reading a password from the terminal.
3e01fd9d
RS
30* Minibuffer Misc:: Various customization hooks and variables.
31@end menu
32
33@node Intro to Minibuffers
34@section Introduction to Minibuffers
35
36 In most ways, a minibuffer is a normal Emacs buffer. Most operations
37@emph{within} a buffer, such as editing commands, work normally in a
38minibuffer. However, many operations for managing buffers do not apply
39to minibuffers. The name of a minibuffer always has the form @w{@samp{
56bd69df 40*Minibuf-@var{number}*}}, and it cannot be changed. Minibuffers are
3e01fd9d 41displayed only in special windows used only for minibuffers; these
1911e6e5 42windows always appear at the bottom of a frame. (Sometimes frames have
3e01fd9d
RS
43no minibuffer window, and sometimes a special kind of frame contains
44nothing but a minibuffer window; see @ref{Minibuffers and Frames}.)
45
8241495d
RS
46 The text in the minibuffer always starts with the @dfn{prompt string},
47the text that was specified by the program that is using the minibuffer
48to tell the user what sort of input to type. This text is marked
2468d0c0
DL
49read-only so you won't accidentally delete or change it. It is also
50marked as a field (@pxref{Fields}), so that certain motion functions,
51including @code{beginning-of-line}, @code{forward-word},
52@code{forward-sentence}, and @code{forward-paragraph}, stop at the
53boundary between the prompt and the actual text. (In older Emacs
54versions, the prompt was displayed using a special mechanism and was not
55part of the buffer contents.)
56
8241495d
RS
57 The minibuffer's window is normally a single line; it grows
58automatically if necessary if the contents require more space. You can
59explicitly resize it temporarily with the window sizing commands; it
60reverts to its normal size when the minibuffer is exited. You can
61resize it permanently by using the window sizing commands in the frame's
62other window, when the minibuffer is not active. If the frame contains
63just a minibuffer, you can change the minibuffer's size by changing the
64frame's size.
3e01fd9d
RS
65
66 If a command uses a minibuffer while there is an active minibuffer,
67this is called a @dfn{recursive minibuffer}. The first minibuffer is
68named @w{@samp{ *Minibuf-0*}}. Recursive minibuffers are named by
69incrementing the number at the end of the name. (The names begin with a
70space so that they won't show up in normal buffer lists.) Of several
71recursive minibuffers, the innermost (or most recently entered) is the
72active minibuffer. We usually call this ``the'' minibuffer. You can
73permit or forbid recursive minibuffers by setting the variable
74@code{enable-recursive-minibuffers} or by putting properties of that
75name on command symbols (@pxref{Minibuffer Misc}).
76
77 Like other buffers, a minibuffer may use any of several local keymaps
78(@pxref{Keymaps}); these contain various exit commands and in some cases
bfe721d1 79completion commands (@pxref{Completion}).
3e01fd9d
RS
80
81@itemize @bullet
82@item
83@code{minibuffer-local-map} is for ordinary input (no completion).
84
85@item
86@code{minibuffer-local-ns-map} is similar, except that @key{SPC} exits
4ff1926e 87just like @key{RET}.
3e01fd9d
RS
88
89@item
90@code{minibuffer-local-completion-map} is for permissive completion.
91
92@item
93@code{minibuffer-local-must-match-map} is for strict completion and
94for cautious completion.
95@end itemize
96
2468d0c0
DL
97 When Emacs is running in batch mode, any request to read from the
98minibuffer actually reads a line from the standard input descriptor that
99was supplied when Emacs was started.
100
3e01fd9d
RS
101@node Text from Minibuffer
102@section Reading Text Strings with the Minibuffer
103
793da230
RS
104 Most often, the minibuffer is used to read text as a string. It can
105also be used to read a Lisp object in textual form. The most basic
106primitive for minibuffer input is @code{read-from-minibuffer}; it can do
107either one.
3e01fd9d 108
bfe721d1
KH
109 In most cases, you should not call minibuffer input functions in the
110middle of a Lisp function. Instead, do all minibuffer input as part of
a9f0a989
RS
111reading the arguments for a command, in the @code{interactive}
112specification. @xref{Defining Commands}.
bfe721d1 113
f9f59935 114@defun read-from-minibuffer prompt-string &optional initial-contents keymap read hist default inherit-input-method
3e01fd9d
RS
115This function is the most general way to get input through the
116minibuffer. By default, it accepts arbitrary text and returns it as a
117string; however, if @var{read} is non-@code{nil}, then it uses
118@code{read} to convert the text into a Lisp object (@pxref{Input
119Functions}).
120
969fe9b5 121The first thing this function does is to activate a minibuffer and
3e01fd9d 122display it with @var{prompt-string} as the prompt. This value must be a
969fe9b5 123string. Then the user can edit text in the minibuffer.
3e01fd9d 124
969fe9b5
RS
125When the user types a command to exit the minibuffer,
126@code{read-from-minibuffer} constructs the return value from the text in
127the minibuffer. Normally it returns a string containing that text.
128However, if @var{read} is non-@code{nil}, @code{read-from-minibuffer}
129reads the text and returns the resulting Lisp object, unevaluated.
130(@xref{Input Functions}, for information about reading.)
f9f59935 131
969fe9b5
RS
132The argument @var{default} specifies a default value to make available
133through the history commands. It should be a string, or @code{nil}. If
134@var{read} is non-@code{nil}, then @var{default} is also used as the
135input to @code{read}, if the user enters empty input. However, in the
a9f0a989 136usual case (where @var{read} is @code{nil}), @code{read-from-minibuffer}
969fe9b5
RS
137does not return @var{default} when the user enters empty input; it
138returns an empty string, @code{""}. In this respect, it is different
139from all the other minibuffer input functions in this chapter.
3e01fd9d
RS
140
141If @var{keymap} is non-@code{nil}, that keymap is the local keymap to
142use in the minibuffer. If @var{keymap} is omitted or @code{nil}, the
143value of @code{minibuffer-local-map} is used as the keymap. Specifying
144a keymap is the most important way to customize the minibuffer for
145various applications such as completion.
146
147The argument @var{hist} specifies which history list variable to use
148for saving the input and for history commands used in the minibuffer.
149It defaults to @code{minibuffer-history}. @xref{Minibuffer History}.
150
f9f59935
RS
151If the variable @code{minibuffer-allow-text-properties} is
152non-@code{nil}, then the string which is returned includes whatever text
153properties were present in the minibuffer. Otherwise all the text
154properties are stripped when the value is returned.
155
f9f59935 156If the argument @var{inherit-input-method} is non-@code{nil}, then the
1911e6e5
RS
157minibuffer inherits the current input method (@pxref{Input Methods}) and
158the setting of @code{enable-multibyte-characters} (@pxref{Text
159Representations}) from whichever buffer was current before entering the
160minibuffer.
969fe9b5
RS
161
162If @var{initial-contents} is a string, @code{read-from-minibuffer}
163inserts it into the minibuffer, leaving point at the end, before the
164user starts to edit the text. The minibuffer appears with this text as
165its initial contents.
166
167Alternatively, @var{initial-contents} can be a cons cell of the form
168@code{(@var{string} . @var{position})}. This means to insert
169@var{string} in the minibuffer but put point @var{position} characters
170from the beginning, rather than at the end.
171
172@strong{Usage note:} The @var{initial-contents} argument and the
173@var{default} argument are two alternative features for more or less the
174same job. It does not make sense to use both features in a single call
175to @code{read-from-minibuffer}. In general, we recommend using
176@var{default}, since this permits the user to insert the default value
177when it is wanted, but does not burden the user with deleting it from
178the minibuffer on other occasions.
3e01fd9d
RS
179@end defun
180
f9f59935 181@defun read-string prompt &optional initial history default inherit-input-method
3e01fd9d
RS
182This function reads a string from the minibuffer and returns it. The
183arguments @var{prompt} and @var{initial} are used as in
184@code{read-from-minibuffer}. The keymap used is
185@code{minibuffer-local-map}.
186
6fe50867
RS
187The optional argument @var{history}, if non-@code{nil}, specifies a
188history list and optionally the initial position in the list. The
189optional argument @var{default} specifies a default value to return if
190the user enters null input; it should be a string. The optional
191argument @var{inherit-input-method} specifies whether to inherit the
192current buffer's input method.
f9f59935
RS
193
194This function is a simplified interface to the
3e01fd9d
RS
195@code{read-from-minibuffer} function:
196
197@smallexample
198@group
f9f59935 199(read-string @var{prompt} @var{initial} @var{history} @var{default} @var{inherit})
3e01fd9d 200@equiv{}
969fe9b5
RS
201(let ((value
202 (read-from-minibuffer @var{prompt} @var{initial} nil nil
203 @var{history} @var{default} @var{inherit})))
204 (if (equal value "")
205 @var{default}
206 value))
3e01fd9d
RS
207@end group
208@end smallexample
209@end defun
210
f9f59935 211@defvar minibuffer-allow-text-properties
1911e6e5
RS
212If this variable is @code{nil}, then @code{read-from-minibuffer} strips
213all text properties from the minibuffer input before returning it.
f9f59935
RS
214Since all minibuffer input uses @code{read-from-minibuffer}, this
215variable applies to all minibuffer input.
1911e6e5
RS
216
217Note that the completion functions discard text properties unconditionally,
218regardless of the value of this variable.
f9f59935
RS
219@end defvar
220
3e01fd9d
RS
221@defvar minibuffer-local-map
222This is the default local keymap for reading from the minibuffer. By
223default, it makes the following bindings:
224
225@table @asis
969fe9b5 226@item @kbd{C-j}
3e01fd9d
RS
227@code{exit-minibuffer}
228
229@item @key{RET}
230@code{exit-minibuffer}
231
232@item @kbd{C-g}
233@code{abort-recursive-edit}
234
235@item @kbd{M-n}
236@code{next-history-element}
237
238@item @kbd{M-p}
239@code{previous-history-element}
240
241@item @kbd{M-r}
242@code{next-matching-history-element}
243
244@item @kbd{M-s}
245@code{previous-matching-history-element}
246@end table
247@end defvar
248
249@c In version 18, initial is required
250@c Emacs 19 feature
f9f59935 251@defun read-no-blanks-input prompt &optional initial inherit-input-method
3e01fd9d
RS
252This function reads a string from the minibuffer, but does not allow
253whitespace characters as part of the input: instead, those characters
f9f59935
RS
254terminate the input. The arguments @var{prompt}, @var{initial}, and
255@var{inherit-input-method} are used as in @code{read-from-minibuffer}.
3e01fd9d
RS
256
257This is a simplified interface to the @code{read-from-minibuffer}
258function, and passes the value of the @code{minibuffer-local-ns-map}
259keymap as the @var{keymap} argument for that function. Since the keymap
260@code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
261possible to put a space into the string, by quoting it.
262
263@smallexample
264@group
265(read-no-blanks-input @var{prompt} @var{initial})
266@equiv{}
267(read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map)
268@end group
269@end smallexample
270@end defun
271
272@defvar minibuffer-local-ns-map
273This built-in variable is the keymap used as the minibuffer local keymap
274in the function @code{read-no-blanks-input}. By default, it makes the
bfe721d1 275following bindings, in addition to those of @code{minibuffer-local-map}:
3e01fd9d
RS
276
277@table @asis
3e01fd9d
RS
278@item @key{SPC}
279@cindex @key{SPC} in minibuffer
280@code{exit-minibuffer}
281
282@item @key{TAB}
283@cindex @key{TAB} in minibuffer
284@code{exit-minibuffer}
285
3e01fd9d
RS
286@item @kbd{?}
287@cindex @kbd{?} in minibuffer
288@code{self-insert-and-exit}
3e01fd9d
RS
289@end table
290@end defvar
291
292@node Object from Minibuffer
293@section Reading Lisp Objects with the Minibuffer
294
295 This section describes functions for reading Lisp objects with the
296minibuffer.
297
298@defun read-minibuffer prompt &optional initial
f9f59935 299This function reads a Lisp object using the minibuffer, and returns it
3e01fd9d 300without evaluating it. The arguments @var{prompt} and @var{initial} are
793da230 301used as in @code{read-from-minibuffer}.
3e01fd9d 302
793da230 303This is a simplified interface to the
3e01fd9d
RS
304@code{read-from-minibuffer} function:
305
306@smallexample
307@group
308(read-minibuffer @var{prompt} @var{initial})
309@equiv{}
310(read-from-minibuffer @var{prompt} @var{initial} nil t)
311@end group
312@end smallexample
313
314Here is an example in which we supply the string @code{"(testing)"} as
315initial input:
316
317@smallexample
318@group
319(read-minibuffer
320 "Enter an expression: " (format "%s" '(testing)))
321
322;; @r{Here is how the minibuffer is displayed:}
323@end group
324
325@group
326---------- Buffer: Minibuffer ----------
327Enter an expression: (testing)@point{}
328---------- Buffer: Minibuffer ----------
329@end group
330@end smallexample
331
332@noindent
333The user can type @key{RET} immediately to use the initial input as a
334default, or can edit the input.
335@end defun
336
337@defun eval-minibuffer prompt &optional initial
f9f59935
RS
338This function reads a Lisp expression using the minibuffer, evaluates
339it, then returns the result. The arguments @var{prompt} and
340@var{initial} are used as in @code{read-from-minibuffer}.
3e01fd9d 341
793da230 342This function simply evaluates the result of a call to
3e01fd9d
RS
343@code{read-minibuffer}:
344
345@smallexample
346@group
347(eval-minibuffer @var{prompt} @var{initial})
348@equiv{}
349(eval (read-minibuffer @var{prompt} @var{initial}))
350@end group
351@end smallexample
352@end defun
353
354@defun edit-and-eval-command prompt form
793da230 355This function reads a Lisp expression in the minibuffer, and then
3e01fd9d
RS
356evaluates it. The difference between this command and
357@code{eval-minibuffer} is that here the initial @var{form} is not
358optional and it is treated as a Lisp object to be converted to printed
359representation rather than as a string of text. It is printed with
360@code{prin1}, so if it is a string, double-quote characters (@samp{"})
361appear in the initial text. @xref{Output Functions}.
362
793da230 363The first thing @code{edit-and-eval-command} does is to activate the
3e01fd9d 364minibuffer with @var{prompt} as the prompt. Then it inserts the printed
f9f59935 365representation of @var{form} in the minibuffer, and lets the user edit it.
3e01fd9d
RS
366When the user exits the minibuffer, the edited text is read with
367@code{read} and then evaluated. The resulting value becomes the value
368of @code{edit-and-eval-command}.
369
793da230 370In the following example, we offer the user an expression with initial
3e01fd9d
RS
371text which is a valid form already:
372
373@smallexample
374@group
375(edit-and-eval-command "Please edit: " '(forward-word 1))
376
177c0ea7 377;; @r{After evaluation of the preceding expression,}
3e01fd9d
RS
378;; @r{the following appears in the minibuffer:}
379@end group
380
381@group
382---------- Buffer: Minibuffer ----------
383Please edit: (forward-word 1)@point{}
384---------- Buffer: Minibuffer ----------
385@end group
386@end smallexample
387
388@noindent
389Typing @key{RET} right away would exit the minibuffer and evaluate the
390expression, thus moving point forward one word.
391@code{edit-and-eval-command} returns @code{nil} in this example.
392@end defun
393
394@node Minibuffer History
395@section Minibuffer History
396@cindex minibuffer history
397@cindex history list
398
969fe9b5 399 A @dfn{minibuffer history list} records previous minibuffer inputs so
793da230
RS
400the user can reuse them conveniently. A history list is actually a
401symbol, not a list; it is a variable whose value is a list of strings
402(previous inputs), most recent first.
3e01fd9d 403
969fe9b5 404 There are many separate history lists, used for different kinds of
3e01fd9d
RS
405inputs. It's the Lisp programmer's job to specify the right history
406list for each use of the minibuffer.
407
969fe9b5 408 The basic minibuffer input functions @code{read-from-minibuffer} and
3e01fd9d
RS
409@code{completing-read} both accept an optional argument named @var{hist}
410which is how you specify the history list. Here are the possible
411values:
412
413@table @asis
414@item @var{variable}
415Use @var{variable} (a symbol) as the history list.
416
417@item (@var{variable} . @var{startpos})
418Use @var{variable} (a symbol) as the history list, and assume that the
419initial history position is @var{startpos} (an integer, counting from
420zero which specifies the most recent element of the history).
421
422If you specify @var{startpos}, then you should also specify that element
423of the history as the initial minibuffer contents, for consistency.
424@end table
425
969fe9b5 426 If you don't specify @var{hist}, then the default history list
3e01fd9d
RS
427@code{minibuffer-history} is used. For other standard history lists,
428see below. You can also create your own history list variable; just
429initialize it to @code{nil} before the first use.
430
969fe9b5 431 Both @code{read-from-minibuffer} and @code{completing-read} add new
3e01fd9d
RS
432elements to the history list automatically, and provide commands to
433allow the user to reuse items on the list. The only thing your program
434needs to do to use a history list is to initialize it and to pass its
435name to the input functions when you wish. But it is safe to modify the
436list by hand when the minibuffer input functions are not using it.
437
ff306b60
RS
438 Emacs functions that add a new element to a history list can also
439delete old elements if the list gets too long. The variable
440@code{history-length} specifies the maximum length for most history
441lists. To specify a different maximum length for a particular history
442list, put the length in the @code{history-length} property of the
443history list symbol.
444
445@defvar history-length
446The value of this variable specifies the maximum length for all
447history lists that don't specify their own maximum lengths. If the
448value is @code{t}, that means there no maximum (don't delete old
449elements).
450@end defvar
451
969fe9b5
RS
452 Here are some of the standard minibuffer history list variables:
453
3e01fd9d
RS
454@defvar minibuffer-history
455The default history list for minibuffer history input.
456@end defvar
457
458@defvar query-replace-history
459A history list for arguments to @code{query-replace} (and similar
460arguments to other commands).
461@end defvar
462
463@defvar file-name-history
a9f0a989
RS
464A history list for file-name arguments.
465@end defvar
466
467@defvar buffer-name-history
a9f0a989 468A history list for buffer-name arguments.
3e01fd9d
RS
469@end defvar
470
471@defvar regexp-history
472A history list for regular expression arguments.
473@end defvar
474
475@defvar extended-command-history
476A history list for arguments that are names of extended commands.
477@end defvar
478
479@defvar shell-command-history
480A history list for arguments that are shell commands.
481@end defvar
482
483@defvar read-expression-history
484A history list for arguments that are Lisp expressions to evaluate.
485@end defvar
486
487@node Completion
488@section Completion
489@cindex completion
490
491 @dfn{Completion} is a feature that fills in the rest of a name
492starting from an abbreviation for it. Completion works by comparing the
493user's input against a list of valid names and determining how much of
494the name is determined uniquely by what the user has typed. For
495example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
496type the first few letters of the name of the buffer to which you wish
497to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
498extends the name as far as it can.
499
500 Standard Emacs commands offer completion for names of symbols, files,
501buffers, and processes; with the functions in this section, you can
502implement completion for other kinds of names.
503
504 The @code{try-completion} function is the basic primitive for
505completion: it returns the longest determined completion of a given
506initial string, with a given set of strings to match against.
507
508 The function @code{completing-read} provides a higher-level interface
509for completion. A call to @code{completing-read} specifies how to
510determine the list of valid names. The function then activates the
511minibuffer with a local keymap that binds a few keys to commands useful
512for completion. Other functions provide convenient simple interfaces
513for reading certain kinds of names with completion.
514
515@menu
516* Basic Completion:: Low-level functions for completing strings.
517 (These are too low level to use the minibuffer.)
518* Minibuffer Completion:: Invoking the minibuffer with completion.
519* Completion Commands:: Minibuffer commands that do completion.
520* High-Level Completion:: Convenient special cases of completion
521 (reading buffer name, file name, etc.)
522* Reading File Names:: Using completion to read file names.
4ff1926e 523* Programmed Completion:: Writing your own completion-function.
3e01fd9d
RS
524@end menu
525
526@node Basic Completion
527@subsection Basic Completion Functions
528
7370e0a8
RS
529 The functions @code{try-completion}, @code{all-completions} and
530@code{test-completion} have nothing in themselves to do with
531minibuffers. We describe them in this chapter so as to keep them near
532the higher-level completion features that do use the minibuffer.
793da230 533
3e01fd9d
RS
534@defun try-completion string collection &optional predicate
535This function returns the longest common substring of all possible
536completions of @var{string} in @var{collection}. The value of
7370e0a8
RS
537@var{collection} must be a list of strings, an alist, an obarray, or a
538function that implements a virtual set of strings (see below).
3e01fd9d
RS
539
540Completion compares @var{string} against each of the permissible
541completions specified by @var{collection}; if the beginning of the
542permissible completion equals @var{string}, it matches. If no permissible
543completions match, @code{try-completion} returns @code{nil}. If only
544one permissible completion matches, and the match is exact, then
545@code{try-completion} returns @code{t}. Otherwise, the value is the
546longest initial sequence common to all the permissible completions that
547match.
548
549If @var{collection} is an alist (@pxref{Association Lists}), the
550@sc{car}s of the alist elements form the set of permissible completions.
551
552@cindex obarray in completion
553If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
554of all symbols in the obarray form the set of permissible completions. The
555global variable @code{obarray} holds an obarray containing the names of
556all interned Lisp symbols.
557
558Note that the only valid way to make a new obarray is to create it
559empty and then add symbols to it one by one using @code{intern}.
560Also, you cannot intern a given symbol in more than one obarray.
561
793da230
RS
562You can also use a symbol that is a function as @var{collection}. Then
563the function is solely responsible for performing completion;
3e01fd9d
RS
564@code{try-completion} returns whatever this function returns. The
565function is called with three arguments: @var{string}, @var{predicate}
566and @code{nil}. (The reason for the third argument is so that the same
567function can be used in @code{all-completions} and do the appropriate
568thing in either case.) @xref{Programmed Completion}.
569
7370e0a8
RS
570If the argument @var{predicate} is non-@code{nil}, then it must be a
571function of one argument. It is used to test each possible match, and
572the match is accepted only if @var{predicate} returns non-@code{nil}.
573The argument given to @var{predicate} is either a string from the
574list, a cons cell from the alist (the @sc{car} of which is a string)
575or a symbol (@emph{not} a symbol name) from the obarray.
576
3e01fd9d
RS
577In the first of the following examples, the string @samp{foo} is
578matched by three of the alist @sc{car}s. All of the matches begin with
579the characters @samp{fooba}, so that is the result. In the second
580example, there is only one possible match, and it is exact, so the value
581is @code{t}.
582
583@smallexample
584@group
177c0ea7 585(try-completion
3e01fd9d
RS
586 "foo"
587 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
588 @result{} "fooba"
589@end group
590
591@group
592(try-completion "foo" '(("barfoo" 2) ("foo" 3)))
593 @result{} t
594@end group
595@end smallexample
596
597In the following example, numerous symbols begin with the characters
598@samp{forw}, and all of them begin with the word @samp{forward}. In
599most of the symbols, this is followed with a @samp{-}, but not in all,
600so no more than @samp{forward} can be completed.
601
602@smallexample
603@group
604(try-completion "forw" obarray)
605 @result{} "forward"
606@end group
607@end smallexample
608
609Finally, in the following example, only two of the three possible
610matches pass the predicate @code{test} (the string @samp{foobaz} is
611too short). Both of those begin with the string @samp{foobar}.
612
613@smallexample
614@group
177c0ea7 615(defun test (s)
3e01fd9d
RS
616 (> (length (car s)) 6))
617 @result{} test
618@end group
619@group
177c0ea7 620(try-completion
3e01fd9d 621 "foo"
177c0ea7 622 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
793da230 623 'test)
3e01fd9d
RS
624 @result{} "foobar"
625@end group
626@end smallexample
627@end defun
628
22697dac 629@defun all-completions string collection &optional predicate nospace
3e01fd9d 630This function returns a list of all possible completions of
72821190
RS
631@var{string}. The arguments to this function (aside from @var{nospace})
632are the same as those of @code{try-completion}. If @var{nospace} is
633non-@code{nil}, completions that start with a space are ignored unless
634@var{string} also starts with a space.
3e01fd9d
RS
635
636If @var{collection} is a function, it is called with three arguments:
637@var{string}, @var{predicate} and @code{t}; then @code{all-completions}
638returns whatever the function returns. @xref{Programmed Completion}.
639
640Here is an example, using the function @code{test} shown in the
641example for @code{try-completion}:
642
643@smallexample
644@group
177c0ea7 645(defun test (s)
3e01fd9d
RS
646 (> (length (car s)) 6))
647 @result{} test
648@end group
649
650@group
177c0ea7 651(all-completions
3e01fd9d
RS
652 "foo"
653 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
793da230 654 'test)
3e01fd9d
RS
655 @result{} ("foobar1" "foobar2")
656@end group
657@end smallexample
658@end defun
659
7370e0a8
RS
660@defun test-completion string collection &optional predicate
661This function returns non-@code{nil} if @var{string} is a valid
662completion possibility specified by @var{collection} and
663@var{predicate}. The other arguments are the same as in
664@code{try-completion}. For instance, if @var{collection} is a list,
665this is true if @var{string} appears in the list and @var{predicate}
666is satisfied.
667
668If @var{collection} is a function, it is called with three arguments,
669the values @var{string}, @var{predicate} and @code{lambda}; whatever
670it returns, @code{test-completion} returns in turn.
671@end defun
672
3e01fd9d 673@defvar completion-ignore-case
7370e0a8
RS
674If the value of this variable is non-@code{nil}, Emacs does not
675consider case significant in completion.
3e01fd9d
RS
676@end defvar
677
4ff1926e
RS
678@defmac lazy-completion-table var fun &rest args
679This macro provides a way to initialize the variable @var{var} as a
7370e0a8
RS
680collection for completion in a lazy way, not computing its actual
681contents until they are first needed. You use this macro to produce a
682value that you store in @var{var}. The actual computation of the
683proper value is done the first time you do completion using @var{var}.
684It is done by calling @var{fun} with the arguments @var{args}. The
685value @var{fun} returns becomes the permanent value of @var{var}.
686
687Here are two examples of use:
4ff1926e
RS
688
689@example
690(defvar foo (lazy-completion-table foo make-my-alist 'global))
7370e0a8 691
4ff1926e
RS
692(make-local-variable 'bar)
693(setq bar (lazy-completion-table foo make-my-alist 'local)
694@end example
695@end defmac
696
3e01fd9d
RS
697@node Minibuffer Completion
698@subsection Completion and the Minibuffer
699
700 This section describes the basic interface for reading from the
701minibuffer with completion.
702
f9f59935 703@defun completing-read prompt collection &optional predicate require-match initial hist default inherit-input-method
3e01fd9d
RS
704This function reads a string in the minibuffer, assisting the user by
705providing completion. It activates the minibuffer with prompt
969fe9b5 706@var{prompt}, which must be a string.
3e01fd9d
RS
707
708The actual completion is done by passing @var{collection} and
709@var{predicate} to the function @code{try-completion}. This happens in
710certain commands bound in the local keymaps used for completion.
711
f9f59935
RS
712If @var{require-match} is @code{nil}, the exit commands work regardless
713of the input in the minibuffer. If @var{require-match} is @code{t}, the
714usual minibuffer exit commands won't exit unless the input completes to
715an element of @var{collection}. If @var{require-match} is neither
716@code{nil} nor @code{t}, then the exit commands won't exit unless the
717input already in the buffer matches an element of @var{collection}.
718
719However, empty input is always permitted, regardless of the value of
720@var{require-match}; in that case, @code{completing-read} returns
969fe9b5
RS
721@var{default}. The value of @var{default} (if non-@code{nil}) is also
722available to the user through the history commands.
3e01fd9d 723
ece23c27 724The user can exit with null input by typing @key{RET} with an empty
6ecb21f1
RS
725minibuffer. Then @code{completing-read} returns @code{""}. This is how
726the user requests whatever default the command uses for the value being
727read. The user can return using @key{RET} in this way regardless of the
728value of @var{require-match}, and regardless of whether the empty string
729is included in @var{collection}.
ece23c27 730
3e01fd9d
RS
731The function @code{completing-read} works by calling
732@code{read-minibuffer}. It uses @code{minibuffer-local-completion-map}
733as the keymap if @var{require-match} is @code{nil}, and uses
734@code{minibuffer-local-must-match-map} if @var{require-match} is
793da230 735non-@code{nil}. @xref{Completion Commands}.
3e01fd9d
RS
736
737The argument @var{hist} specifies which history list variable to use for
738saving the input and for minibuffer history commands. It defaults to
739@code{minibuffer-history}. @xref{Minibuffer History}.
740
969fe9b5
RS
741If @var{initial} is non-@code{nil}, @code{completing-read} inserts it
742into the minibuffer as part of the input. Then it allows the user to
743edit the input, providing several commands to attempt completion.
744In most cases, we recommend using @var{default}, and not @var{initial}.
f9f59935 745
70c750c0
RS
746@strong{We discourage use of a non-@code{nil} value for
747@var{initial}}, because it is an intrusive interface. The history
b9332010
RS
748list feature (which did not exist when we introduced @var{initial})
749offers a far more convenient and general way for the user to get the
750default and edit it, and it is always available.
70c750c0 751
f9f59935 752If the argument @var{inherit-input-method} is non-@code{nil}, then the
1911e6e5 753minibuffer inherits the current input method (@pxref{Input
a9f0a989
RS
754Methods}) and the setting of @code{enable-multibyte-characters}
755(@pxref{Text Representations}) from whichever buffer was current before
756entering the minibuffer.
f9f59935 757
3e01fd9d
RS
758Completion ignores case when comparing the input against the possible
759matches, if the built-in variable @code{completion-ignore-case} is
760non-@code{nil}. @xref{Basic Completion}.
761
762Here's an example of using @code{completing-read}:
763
764@smallexample
765@group
766(completing-read
767 "Complete a foo: "
768 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
769 nil t "fo")
770@end group
771
772@group
177c0ea7 773;; @r{After evaluation of the preceding expression,}
3e01fd9d
RS
774;; @r{the following appears in the minibuffer:}
775
776---------- Buffer: Minibuffer ----------
777Complete a foo: fo@point{}
778---------- Buffer: Minibuffer ----------
779@end group
780@end smallexample
781
782@noindent
783If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
784@code{completing-read} returns @code{barfoo}.
785
786The @code{completing-read} function binds three variables to pass
793da230
RS
787information to the commands that actually do completion. These
788variables are @code{minibuffer-completion-table},
789@code{minibuffer-completion-predicate} and
790@code{minibuffer-completion-confirm}. For more information about them,
791see @ref{Completion Commands}.
3e01fd9d
RS
792@end defun
793
794@node Completion Commands
8241495d 795@subsection Minibuffer Commands that Do Completion
3e01fd9d
RS
796
797 This section describes the keymaps, commands and user options used in
798the minibuffer to do completion.
799
800@defvar minibuffer-local-completion-map
793da230 801@code{completing-read} uses this value as the local keymap when an
3e01fd9d
RS
802exact match of one of the completions is not required. By default, this
803keymap makes the following bindings:
804
805@table @asis
806@item @kbd{?}
807@code{minibuffer-completion-help}
808
809@item @key{SPC}
810@code{minibuffer-complete-word}
811
812@item @key{TAB}
813@code{minibuffer-complete}
814@end table
815
816@noindent
793da230
RS
817with other characters bound as in @code{minibuffer-local-map}
818(@pxref{Text from Minibuffer}).
3e01fd9d
RS
819@end defvar
820
821@defvar minibuffer-local-must-match-map
822@code{completing-read} uses this value as the local keymap when an
823exact match of one of the completions is required. Therefore, no keys
793da230 824are bound to @code{exit-minibuffer}, the command that exits the
3e01fd9d
RS
825minibuffer unconditionally. By default, this keymap makes the following
826bindings:
827
828@table @asis
829@item @kbd{?}
830@code{minibuffer-completion-help}
831
832@item @key{SPC}
833@code{minibuffer-complete-word}
834
835@item @key{TAB}
836@code{minibuffer-complete}
837
969fe9b5 838@item @kbd{C-j}
3e01fd9d
RS
839@code{minibuffer-complete-and-exit}
840
841@item @key{RET}
842@code{minibuffer-complete-and-exit}
843@end table
844
845@noindent
846with other characters bound as in @code{minibuffer-local-map}.
847@end defvar
848
849@defvar minibuffer-completion-table
850The value of this variable is the alist or obarray used for completion
851in the minibuffer. This is the global variable that contains what
852@code{completing-read} passes to @code{try-completion}. It is used by
853minibuffer completion commands such as @code{minibuffer-complete-word}.
854@end defvar
855
856@defvar minibuffer-completion-predicate
857This variable's value is the predicate that @code{completing-read}
858passes to @code{try-completion}. The variable is also used by the other
859minibuffer completion functions.
860@end defvar
861
862@deffn Command minibuffer-complete-word
863This function completes the minibuffer contents by at most a single
864word. Even if the minibuffer contents have only one completion,
865@code{minibuffer-complete-word} does not add any characters beyond the
866first character that is not a word constituent. @xref{Syntax Tables}.
867@end deffn
868
869@deffn Command minibuffer-complete
870This function completes the minibuffer contents as far as possible.
871@end deffn
872
873@deffn Command minibuffer-complete-and-exit
874This function completes the minibuffer contents, and exits if
875confirmation is not required, i.e., if
2770e862 876@code{minibuffer-completion-confirm} is @code{nil}. If confirmation
793da230
RS
877@emph{is} required, it is given by repeating this command
878immediately---the command is programmed to work without confirmation
879when run twice in succession.
3e01fd9d
RS
880@end deffn
881
882@defvar minibuffer-completion-confirm
883When the value of this variable is non-@code{nil}, Emacs asks for
884confirmation of a completion before exiting the minibuffer. The
885function @code{minibuffer-complete-and-exit} checks the value of this
886variable before it exits.
887@end defvar
888
889@deffn Command minibuffer-completion-help
890This function creates a list of the possible completions of the
891current minibuffer contents. It works by calling @code{all-completions}
892using the value of the variable @code{minibuffer-completion-table} as
893the @var{collection} argument, and the value of
894@code{minibuffer-completion-predicate} as the @var{predicate} argument.
895The list of completions is displayed as text in a buffer named
896@samp{*Completions*}.
897@end deffn
898
899@defun display-completion-list completions
900This function displays @var{completions} to the stream in
3e099569 901@code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
3e01fd9d
RS
902information about streams.) The argument @var{completions} is normally
903a list of completions just returned by @code{all-completions}, but it
904does not have to be. Each element may be a symbol or a string, either
905of which is simply printed, or a list of two strings, which is printed
906as if the strings were concatenated.
907
908This function is called by @code{minibuffer-completion-help}. The
909most common way to use it is together with
910@code{with-output-to-temp-buffer}, like this:
911
912@example
913(with-output-to-temp-buffer "*Completions*"
914 (display-completion-list
915 (all-completions (buffer-string) my-alist)))
916@end example
917@end defun
918
919@defopt completion-auto-help
920If this variable is non-@code{nil}, the completion commands
921automatically display a list of possible completions whenever nothing
922can be completed because the next character is not uniquely determined.
923@end defopt
924
925@node High-Level Completion
926@subsection High-Level Completion Functions
927
928 This section describes the higher-level convenient functions for
929reading certain sorts of names with completion.
930
bfe721d1
KH
931 In most cases, you should not call these functions in the middle of a
932Lisp function. When possible, do all minibuffer input as part of
a9f0a989
RS
933reading the arguments for a command, in the @code{interactive}
934specification. @xref{Defining Commands}.
bfe721d1 935
3e01fd9d
RS
936@defun read-buffer prompt &optional default existing
937This function reads the name of a buffer and returns it as a string.
938The argument @var{default} is the default name to use, the value to
939return if the user exits with an empty minibuffer. If non-@code{nil},
940it should be a string or a buffer. It is mentioned in the prompt, but
941is not inserted in the minibuffer as initial input.
942
943If @var{existing} is non-@code{nil}, then the name specified must be
793da230
RS
944that of an existing buffer. The usual commands to exit the minibuffer
945do not exit if the text is not valid, and @key{RET} does completion to
946attempt to find a valid name. (However, @var{default} is not checked
947for validity; it is returned, whatever it is, if the user exits with the
948minibuffer empty.)
3e01fd9d
RS
949
950In the following example, the user enters @samp{minibuffer.t}, and
951then types @key{RET}. The argument @var{existing} is @code{t}, and the
952only buffer name starting with the given input is
953@samp{minibuffer.texi}, so that name is the value.
954
955@example
956(read-buffer "Buffer name? " "foo" t)
957@group
177c0ea7 958;; @r{After evaluation of the preceding expression,}
3e01fd9d
RS
959;; @r{the following prompt appears,}
960;; @r{with an empty minibuffer:}
961@end group
962
963@group
964---------- Buffer: Minibuffer ----------
965Buffer name? (default foo) @point{}
966---------- Buffer: Minibuffer ----------
967@end group
968
969@group
970;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
971 @result{} "minibuffer.texi"
972@end group
973@end example
974@end defun
975
f9f59935
RS
976@defvar read-buffer-function
977This variable specifies how to read buffer names. For example, if you
978set this variable to @code{iswitchb-read-buffer}, all Emacs commands
979that call @code{read-buffer} to read a buffer name will actually use the
980@code{iswitchb} package to read it.
981@end defvar
982
983@defun read-command prompt &optional default
3e01fd9d
RS
984This function reads the name of a command and returns it as a Lisp
985symbol. The argument @var{prompt} is used as in
986@code{read-from-minibuffer}. Recall that a command is anything for
987which @code{commandp} returns @code{t}, and a command name is a symbol
988for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
989
f9f59935 990The argument @var{default} specifies what to return if the user enters
969fe9b5
RS
991null input. It can be a symbol or a string; if it is a string,
992@code{read-command} interns it before returning it. If @var{default} is
993@code{nil}, that means no default has been specified; then if the user
994enters null input, the return value is @code{nil}.
f9f59935 995
3e01fd9d
RS
996@example
997(read-command "Command name? ")
998
999@group
177c0ea7 1000;; @r{After evaluation of the preceding expression,}
3e01fd9d
RS
1001;; @r{the following prompt appears with an empty minibuffer:}
1002@end group
1003
1004@group
177c0ea7
JB
1005---------- Buffer: Minibuffer ----------
1006Command name?
3e01fd9d
RS
1007---------- Buffer: Minibuffer ----------
1008@end group
1009@end example
1010
1011@noindent
1012If the user types @kbd{forward-c @key{RET}}, then this function returns
1013@code{forward-char}.
1014
9e2b495b
RS
1015The @code{read-command} function is a simplified interface to
1016@code{completing-read}. It uses the variable @code{obarray} so as to
1017complete in the set of extant Lisp symbols, and it uses the
3e01fd9d
RS
1018@code{commandp} predicate so as to accept only command names:
1019
1020@cindex @code{commandp} example
1021@example
1022@group
1023(read-command @var{prompt})
1024@equiv{}
177c0ea7 1025(intern (completing-read @var{prompt} obarray
3e01fd9d
RS
1026 'commandp t nil))
1027@end group
1028@end example
1029@end defun
1030
f9f59935 1031@defun read-variable prompt &optional default
3e01fd9d
RS
1032This function reads the name of a user variable and returns it as a
1033symbol.
1034
f9f59935 1035The argument @var{default} specifies what to return if the user enters
969fe9b5
RS
1036null input. It can be a symbol or a string; if it is a string,
1037@code{read-variable} interns it before returning it. If @var{default}
1038is @code{nil}, that means no default has been specified; then if the
1039user enters null input, the return value is @code{nil}.
f9f59935 1040
3e01fd9d
RS
1041@example
1042@group
1043(read-variable "Variable name? ")
1044
177c0ea7
JB
1045;; @r{After evaluation of the preceding expression,}
1046;; @r{the following prompt appears,}
3e01fd9d
RS
1047;; @r{with an empty minibuffer:}
1048@end group
1049
1050@group
1051---------- Buffer: Minibuffer ----------
1052Variable name? @point{}
1053---------- Buffer: Minibuffer ----------
1054@end group
1055@end example
1056
1057@noindent
1058If the user then types @kbd{fill-p @key{RET}}, @code{read-variable}
1059returns @code{fill-prefix}.
1060
1061This function is similar to @code{read-command}, but uses the
1062predicate @code{user-variable-p} instead of @code{commandp}:
1063
1064@cindex @code{user-variable-p} example
1065@example
1066@group
1067(read-variable @var{prompt})
1068@equiv{}
1069(intern
1070 (completing-read @var{prompt} obarray
1071 'user-variable-p t nil))
1072@end group
1073@end example
1074@end defun
1075
969fe9b5 1076 See also the functions @code{read-coding-system} and
1911e6e5 1077@code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems}.
f9f59935 1078
3e01fd9d
RS
1079@node Reading File Names
1080@subsection Reading File Names
1081
1082 Here is another high-level completion function, designed for reading a
1083file name. It provides special features including automatic insertion
1084of the default directory.
1085
1c7cdff5 1086@defun read-file-name prompt &optional directory default existing initial predicate
3e01fd9d
RS
1087This function reads a file name in the minibuffer, prompting with
1088@var{prompt} and providing completion. If @var{default} is
1089non-@code{nil}, then the function returns @var{default} if the user just
793da230
RS
1090types @key{RET}. @var{default} is not checked for validity; it is
1091returned, whatever it is, if the user exits with the minibuffer empty.
3e01fd9d 1092
793da230
RS
1093If @var{existing} is non-@code{nil}, then the user must specify the name
1094of an existing file; @key{RET} performs completion to make the name
1095valid if possible, and then refuses to exit if it is not valid. If the
1096value of @var{existing} is neither @code{nil} nor @code{t}, then
1097@key{RET} also requires confirmation after completion. If
1098@var{existing} is @code{nil}, then the name of a nonexistent file is
1099acceptable.
3e01fd9d
RS
1100
1101The argument @var{directory} specifies the directory to use for
793da230
RS
1102completion of relative file names. If @code{insert-default-directory}
1103is non-@code{nil}, @var{directory} is also inserted in the minibuffer as
1104initial input. It defaults to the current buffer's value of
1105@code{default-directory}.
3e01fd9d
RS
1106
1107@c Emacs 19 feature
1108If you specify @var{initial}, that is an initial file name to insert in
1911e6e5 1109the buffer (after @var{directory}, if that is inserted). In this
793da230
RS
1110case, point goes at the beginning of @var{initial}. The default for
1111@var{initial} is @code{nil}---don't insert any file name. To see what
969fe9b5
RS
1112@var{initial} does, try the command @kbd{C-x C-v}. @strong{Note:} we
1113recommend using @var{default} rather than @var{initial} in most cases.
3e01fd9d 1114
1c7cdff5
RS
1115If @var{predicate} is non-@code{nil}, it specifies a function of one
1116argument that decides which file names are acceptable completion
1117possibilities. A file name is an acceptable value if @var{predicate}
1118returns non-@code{nil} for it.
1119
177c0ea7 1120Here is an example:
3e01fd9d
RS
1121
1122@example
1123@group
1124(read-file-name "The file is ")
1125
177c0ea7 1126;; @r{After evaluation of the preceding expression,}
3e01fd9d
RS
1127;; @r{the following appears in the minibuffer:}
1128@end group
1129
1130@group
1131---------- Buffer: Minibuffer ----------
1132The file is /gp/gnu/elisp/@point{}
1133---------- Buffer: Minibuffer ----------
1134@end group
1135@end example
1136
1137@noindent
1138Typing @kbd{manual @key{TAB}} results in the following:
1139
1140@example
1141@group
1142---------- Buffer: Minibuffer ----------
1143The file is /gp/gnu/elisp/manual.texi@point{}
1144---------- Buffer: Minibuffer ----------
1145@end group
1146@end example
1147
1148@c Wordy to avoid overfull hbox in smallbook mode.
1149@noindent
1150If the user types @key{RET}, @code{read-file-name} returns the file name
1151as the string @code{"/gp/gnu/elisp/manual.texi"}.
1152@end defun
1153
1c7cdff5
RS
1154@defun read-directory-name prompt &optional directory default existing initial
1155This function is like @code{read-file-name} but allows only directory
1156names as completion possibilities.
1157@end defun
1158
3e01fd9d
RS
1159@defopt insert-default-directory
1160This variable is used by @code{read-file-name}. Its value controls
1161whether @code{read-file-name} starts by placing the name of the default
1162directory in the minibuffer, plus the initial file name if any. If the
1163value of this variable is @code{nil}, then @code{read-file-name} does
bfe721d1
KH
1164not place any initial input in the minibuffer (unless you specify
1165initial input with the @var{initial} argument). In that case, the
3e01fd9d
RS
1166default directory is still used for completion of relative file names,
1167but is not displayed.
1168
1169For example:
1170
1171@example
1172@group
1173;; @r{Here the minibuffer starts out with the default directory.}
1174(let ((insert-default-directory t))
1175 (read-file-name "The file is "))
1176@end group
1177
1178@group
1179---------- Buffer: Minibuffer ----------
1180The file is ~lewis/manual/@point{}
1181---------- Buffer: Minibuffer ----------
1182@end group
1183
1184@group
1185;; @r{Here the minibuffer is empty and only the prompt}
1186;; @r{appears on its line.}
1187(let ((insert-default-directory nil))
1188 (read-file-name "The file is "))
1189@end group
1190
1191@group
1192---------- Buffer: Minibuffer ----------
1193The file is @point{}
1194---------- Buffer: Minibuffer ----------
1195@end group
1196@end example
1197@end defopt
1198
1199@node Programmed Completion
1200@subsection Programmed Completion
1201@cindex programmed completion
1202
1203 Sometimes it is not possible to create an alist or an obarray
1204containing all the intended possible completions. In such a case, you
1205can supply your own function to compute the completion of a given string.
1206This is called @dfn{programmed completion}.
1207
1208 To use this feature, pass a symbol with a function definition as the
793da230
RS
1209@var{collection} argument to @code{completing-read}. The function
1210@code{completing-read} arranges to pass your completion function along
1211to @code{try-completion} and @code{all-completions}, which will then let
1212your function do all the work.
3e01fd9d
RS
1213
1214 The completion function should accept three arguments:
1215
1216@itemize @bullet
1217@item
1218The string to be completed.
1219
1220@item
1221The predicate function to filter possible matches, or @code{nil} if
1222none. Your function should call the predicate for each possible match,
1223and ignore the possible match if the predicate returns @code{nil}.
1224
1225@item
1226A flag specifying the type of operation.
1227@end itemize
1228
1229 There are three flag values for three operations:
1230
1231@itemize @bullet
1232@item
1233@code{nil} specifies @code{try-completion}. The completion function
1234should return the completion of the specified string, or @code{t} if the
d595eca0
RS
1235string is a unique and exact match already, or @code{nil} if the string
1236matches no possibility.
1237
1238If the string is an exact match for one possibility, but also matches
969fe9b5 1239other longer possibilities, the function should return the string, not
d595eca0 1240@code{t}.
3e01fd9d
RS
1241
1242@item
1243@code{t} specifies @code{all-completions}. The completion function
1244should return a list of all possible completions of the specified
1245string.
1246
1247@item
7370e0a8 1248@code{lambda} specifies @code{test-completion}. The completion
3e01fd9d
RS
1249function should return @code{t} if the specified string is an exact
1250match for some possibility; @code{nil} otherwise.
1251@end itemize
1252
1253 It would be consistent and clean for completion functions to allow
bfe721d1 1254lambda expressions (lists that are functions) as well as function
3e01fd9d 1255symbols as @var{collection}, but this is impossible. Lists as
7370e0a8
RS
1256completion tables already have other meanings, and it would be
1257unreliable to treat one differently just because it is also a possible
1258function. So you must arrange for any function you wish to use for
1259completion to be encapsulated in a symbol.
3e01fd9d
RS
1260
1261 Emacs uses programmed completion when completing file names.
1262@xref{File Name Completion}.
1263
4ff1926e
RS
1264@defmac dynamic-completion-table function
1265This macro is a convenient way to write a function that can act as
1266programmed completion function. The argument @var{function} should be
1267a function that takes one argument, a string, and returns an alist of
1268possible completions of it. You can think of
1269@code{dynamic-completion-table} as a transducer between that interface
1270and the interface for programmed completion functions.
1271@end defmac
1272
3e01fd9d
RS
1273@node Yes-or-No Queries
1274@section Yes-or-No Queries
1275@cindex asking the user questions
1276@cindex querying the user
1277@cindex yes-or-no questions
1278
1279 This section describes functions used to ask the user a yes-or-no
1280question. The function @code{y-or-n-p} can be answered with a single
1281character; it is useful for questions where an inadvertent wrong answer
1282will not have serious consequences. @code{yes-or-no-p} is suitable for
1283more momentous questions, since it requires three or four characters to
1284answer.
1285
3e099569
RS
1286 If either of these functions is called in a command that was invoked
1287using the mouse---more precisely, if @code{last-nonmenu-event}
1288(@pxref{Command Loop Info}) is either @code{nil} or a list---then it
1289uses a dialog box or pop-up menu to ask the question. Otherwise, it
1290uses keyboard input. You can force use of the mouse or use of keyboard
1291input by binding @code{last-nonmenu-event} to a suitable value around
1292the call.
1293
3e01fd9d
RS
1294 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
1295@code{y-or-n-p} does not; but it seems best to describe them together.
1296
1297@defun y-or-n-p prompt
793da230 1298This function asks the user a question, expecting input in the echo
3e01fd9d
RS
1299area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
1300user types @kbd{n}. This function also accepts @key{SPC} to mean yes
1301and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit'', like
1302@kbd{C-g}, because the question might look like a minibuffer and for
1303that reason the user might try to use @kbd{C-]} to get out. The answer
1304is a single character, with no @key{RET} needed to terminate it. Upper
1305and lower case are equivalent.
1306
793da230 1307``Asking the question'' means printing @var{prompt} in the echo area,
3e01fd9d
RS
1308followed by the string @w{@samp{(y or n) }}. If the input is not one of
1309the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
1310@kbd{@key{DEL}}, or something that quits), the function responds
1311@samp{Please answer y or n.}, and repeats the request.
1312
793da230 1313This function does not actually use the minibuffer, since it does not
3e01fd9d
RS
1314allow editing of the answer. It actually uses the echo area (@pxref{The
1315Echo Area}), which uses the same screen space as the minibuffer. The
1316cursor moves to the echo area while the question is being asked.
1317
793da230 1318The answers and their meanings, even @samp{y} and @samp{n}, are not
3e01fd9d
RS
1319hardwired. The keymap @code{query-replace-map} specifies them.
1320@xref{Search and Replace}.
1321
793da230 1322In the following example, the user first types @kbd{q}, which is
3e01fd9d
RS
1323invalid. At the next prompt the user types @kbd{y}.
1324
1325@smallexample
1326@group
1327(y-or-n-p "Do you need a lift? ")
1328
177c0ea7 1329;; @r{After evaluation of the preceding expression,}
3e01fd9d
RS
1330;; @r{the following prompt appears in the echo area:}
1331@end group
1332
1333@group
1334---------- Echo area ----------
177c0ea7 1335Do you need a lift? (y or n)
3e01fd9d
RS
1336---------- Echo area ----------
1337@end group
1338
1339;; @r{If the user then types @kbd{q}, the following appears:}
1340
1341@group
1342---------- Echo area ----------
177c0ea7 1343Please answer y or n. Do you need a lift? (y or n)
3e01fd9d
RS
1344---------- Echo area ----------
1345@end group
1346
1347;; @r{When the user types a valid answer,}
1348;; @r{it is displayed after the question:}
1349
1350@group
1351---------- Echo area ----------
1352Do you need a lift? (y or n) y
1353---------- Echo area ----------
1354@end group
1355@end smallexample
1356
1357@noindent
1358We show successive lines of echo area messages, but only one actually
1359appears on the screen at a time.
1360@end defun
1361
48a58303
RS
1362@defun y-or-n-p-with-timeout prompt seconds default-value
1363Like @code{y-or-n-p}, except that if the user fails to answer within
1364@var{seconds} seconds, this function stops waiting and returns
1365@var{default-value}. It works by setting up a timer; see @ref{Timers}.
1366The argument @var{seconds} may be an integer or a floating point number.
1367@end defun
1368
3e01fd9d 1369@defun yes-or-no-p prompt
793da230
RS
1370This function asks the user a question, expecting input in the
1371minibuffer. It returns @code{t} if the user enters @samp{yes},
1372@code{nil} if the user types @samp{no}. The user must type @key{RET} to
1373finalize the response. Upper and lower case are equivalent.
3e01fd9d 1374
793da230 1375@code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
3e01fd9d
RS
1376followed by @w{@samp{(yes or no) }}. The user must type one of the
1377expected responses; otherwise, the function responds @samp{Please answer
1378yes or no.}, waits about two seconds and repeats the request.
1379
793da230 1380@code{yes-or-no-p} requires more work from the user than
3e01fd9d
RS
1381@code{y-or-n-p} and is appropriate for more crucial decisions.
1382
3e01fd9d
RS
1383Here is an example:
1384
1385@smallexample
1386@group
1387(yes-or-no-p "Do you really want to remove everything? ")
1388
177c0ea7
JB
1389;; @r{After evaluation of the preceding expression,}
1390;; @r{the following prompt appears,}
3e01fd9d
RS
1391;; @r{with an empty minibuffer:}
1392@end group
1393
1394@group
1395---------- Buffer: minibuffer ----------
177c0ea7 1396Do you really want to remove everything? (yes or no)
3e01fd9d
RS
1397---------- Buffer: minibuffer ----------
1398@end group
1399@end smallexample
1400
1401@noindent
1402If the user first types @kbd{y @key{RET}}, which is invalid because this
1403function demands the entire word @samp{yes}, it responds by displaying
1404these prompts, with a brief pause between them:
1405
1406@smallexample
1407@group
1408---------- Buffer: minibuffer ----------
1409Please answer yes or no.
1410Do you really want to remove everything? (yes or no)
1411---------- Buffer: minibuffer ----------
1412@end group
1413@end smallexample
1414@end defun
1415
1416@node Multiple Queries
1417@section Asking Multiple Y-or-N Questions
1418
793da230
RS
1419 When you have a series of similar questions to ask, such as ``Do you
1420want to save this buffer'' for each buffer in turn, you should use
1421@code{map-y-or-n-p} to ask the collection of questions, rather than
1422asking each question individually. This gives the user certain
1423convenient facilities such as the ability to answer the whole series at
1424once.
1425
2468d0c0 1426@defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
f9f59935
RS
1427This function asks the user a series of questions, reading a
1428single-character answer in the echo area for each one.
3e01fd9d
RS
1429
1430The value of @var{list} specifies the objects to ask questions about.
1431It should be either a list of objects or a generator function. If it is
1432a function, it should expect no arguments, and should return either the
1433next object to ask about, or @code{nil} meaning stop asking questions.
1434
1435The argument @var{prompter} specifies how to ask each question. If
1436@var{prompter} is a string, the question text is computed like this:
1437
1438@example
1439(format @var{prompter} @var{object})
1440@end example
1441
1442@noindent
1443where @var{object} is the next object to ask about (as obtained from
1444@var{list}).
1445
1446If not a string, @var{prompter} should be a function of one argument
63ff95ee
MW
1447(the next object to ask about) and should return the question text. If
1448the value is a string, that is the question to ask the user. The
1449function can also return @code{t} meaning do act on this object (and
1450don't ask the user), or @code{nil} meaning ignore this object (and don't
1451ask the user).
3e01fd9d
RS
1452
1453The argument @var{actor} says how to act on the answers that the user
1454gives. It should be a function of one argument, and it is called with
1455each object that the user says yes for. Its argument is always an
1456object obtained from @var{list}.
1457
1458If the argument @var{help} is given, it should be a list of this form:
1459
1460@example
1461(@var{singular} @var{plural} @var{action})
1462@end example
1463
1464@noindent
1465where @var{singular} is a string containing a singular noun that
1466describes the objects conceptually being acted on, @var{plural} is the
1467corresponding plural noun, and @var{action} is a transitive verb
1468describing what @var{actor} does.
1469
1470If you don't specify @var{help}, the default is @code{("object"
1471"objects" "act on")}.
1472
1473Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
1474@key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
1475that object; @kbd{!} to act on all following objects; @key{ESC} or
1476@kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
1477the current object and then exit; or @kbd{C-h} to get help. These are
1478the same answers that @code{query-replace} accepts. The keymap
1479@code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
1480as well as for @code{query-replace}; see @ref{Search and Replace}.
1481
1482You can use @var{action-alist} to specify additional possible answers
1483and what they mean. It is an alist of elements of the form
1484@code{(@var{char} @var{function} @var{help})}, each of which defines one
1485additional answer. In this element, @var{char} is a character (the
1486answer); @var{function} is a function of one argument (an object from
1487@var{list}); @var{help} is a string.
1488
1489When the user responds with @var{char}, @code{map-y-or-n-p} calls
1490@var{function}. If it returns non-@code{nil}, the object is considered
1491``acted upon'', and @code{map-y-or-n-p} advances to the next object in
1492@var{list}. If it returns @code{nil}, the prompt is repeated for the
1493same object.
1494
2468d0c0
DL
1495Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
1496prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
1497does not do that.
1498
3e099569
RS
1499If @code{map-y-or-n-p} is called in a command that was invoked using the
1500mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
1501Loop Info}) is either @code{nil} or a list---then it uses a dialog box
1502or pop-up menu to ask the question. In this case, it does not use
1503keyboard input or the echo area. You can force use of the mouse or use
1504of keyboard input by binding @code{last-nonmenu-event} to a suitable
1505value around the call.
1506
3e01fd9d
RS
1507The return value of @code{map-y-or-n-p} is the number of objects acted on.
1508@end defun
1509
e75ecfec
KH
1510@node Reading a Password
1511@section Reading a Password
1512@cindex passwords, reading
1513
1514 To read a password to pass to another program, you can use the
1515function @code{read-passwd}.
1516
e75ecfec
KH
1517@defun read-passwd prompt &optional confirm default
1518This function reads a password, prompting with @var{prompt}. It does
1519not echo the password as the user types it; instead, it echoes @samp{.}
1520for each character in the password.
1521
1522The optional argument @var{confirm}, if non-@code{nil}, says to read the
1523password twice and insist it must be the same both times. If it isn't
1524the same, the user has to type it over and over until the last two
1525times match.
1526
1527The optional argument @var{default} specifies the default password to
1528return if the user enters empty input. If @var{default} is @code{nil},
1529then @code{read-passwd} returns the null string in that case.
1530@end defun
1531
3e01fd9d 1532@node Minibuffer Misc
3e01fd9d
RS
1533@section Minibuffer Miscellany
1534
1535 This section describes some basic functions and variables related to
1536minibuffers.
1537
1538@deffn Command exit-minibuffer
1539This command exits the active minibuffer. It is normally bound to
1540keys in minibuffer local keymaps.
1541@end deffn
1542
1543@deffn Command self-insert-and-exit
1544This command exits the active minibuffer after inserting the last
1545character typed on the keyboard (found in @code{last-command-char};
1546@pxref{Command Loop Info}).
1547@end deffn
1548
1549@deffn Command previous-history-element n
1550This command replaces the minibuffer contents with the value of the
1551@var{n}th previous (older) history element.
1552@end deffn
1553
1554@deffn Command next-history-element n
1555This command replaces the minibuffer contents with the value of the
1556@var{n}th more recent history element.
1557@end deffn
1558
2468d0c0 1559@deffn Command previous-matching-history-element pattern n
3e01fd9d 1560This command replaces the minibuffer contents with the value of the
2468d0c0
DL
1561@var{n}th previous (older) history element that matches @var{pattern} (a
1562regular expression).
3e01fd9d
RS
1563@end deffn
1564
2468d0c0
DL
1565@deffn Command next-matching-history-element pattern n
1566This command replaces the minibuffer contents with the value of the
1567@var{n}th next (newer) history element that matches @var{pattern} (a
1568regular expression).
3e01fd9d
RS
1569@end deffn
1570
bfe721d1
KH
1571@defun minibuffer-prompt
1572This function returns the prompt string of the currently active
1573minibuffer. If no minibuffer is active, it returns @code{nil}.
1574@end defun
1575
2468d0c0 1576@defun minibuffer-prompt-end
b3b044f2 1577@tindex minibuffer-prompt-end
8241495d
RS
1578This function, available starting in Emacs 21, returns the current
1579position of the end of the minibuffer prompt, if a minibuffer is
b3b044f2
MB
1580current. Otherwise, it returns the minimum valid buffer position.
1581@end defun
1582
1583@defun minibuffer-contents
1584@tindex minibuffer-contents
1585This function, available starting in Emacs 21, returns the editable
1586contents of the minibuffer (that is, everything except the prompt) as
1587a string, if a minibuffer is current. Otherwise, it returns the
1588entire contents of the current buffer.
1589@end defun
1590
1591@defun minibuffer-contents-no-properties
1592@tindex minibuffer-contents-no-properties
1593This is like @code{minibuffer-contents}, except that it does not copy text
1594properties, just the characters themselves. @xref{Text Properties}.
1595@end defun
1596
1597@defun delete-minibuffer-contents
1598@tindex delete-minibuffer-contents
1599This function, available starting in Emacs 21, erases the editable
1600contents of the minibuffer (that is, everything except the prompt), if
1601a minibuffer is current. Otherwise, it erases the entire buffer.
8241495d
RS
1602@end defun
1603
42d9d13a 1604@defun minibuffer-prompt-width
8241495d
RS
1605This function returns the current display-width of the minibuffer
1606prompt, if a minibuffer is current. Otherwise, it returns zero.
bfe721d1
KH
1607@end defun
1608
3e01fd9d
RS
1609@defvar minibuffer-setup-hook
1610This is a normal hook that is run whenever the minibuffer is entered.
793da230
RS
1611@xref{Hooks}.
1612@end defvar
1613
612b4d5c 1614@defvar minibuffer-exit-hook
793da230
RS
1615This is a normal hook that is run whenever the minibuffer is exited.
1616@xref{Hooks}.
3e01fd9d
RS
1617@end defvar
1618
1619@defvar minibuffer-help-form
1620The current value of this variable is used to rebind @code{help-form}
1621locally inside the minibuffer (@pxref{Help Functions}).
1622@end defvar
1623
7370e0a8
RS
1624@defun minibufferp &optional buffer
1625This function returns non-@code{nil} if @var{buffer} is a minibuffer.
1626If @var{buffer} is omitted, it tests the current buffer.
1627@end defun
1628
22697dac
KH
1629@defun active-minibuffer-window
1630This function returns the currently active minibuffer window, or
1631@code{nil} if none is currently active.
1632@end defun
1633
3e01fd9d 1634@defun minibuffer-window &optional frame
22697dac
KH
1635This function returns the minibuffer window used for frame @var{frame}.
1636If @var{frame} is @code{nil}, that stands for the current frame. Note
1637that the minibuffer window used by a frame need not be part of that
1638frame---a frame that has no minibuffer of its own necessarily uses some
1639other frame's minibuffer window.
3e01fd9d
RS
1640@end defun
1641
1642@c Emacs 19 feature
1643@defun window-minibuffer-p window
1644This function returns non-@code{nil} if @var{window} is a minibuffer window.
1645@end defun
1646
1647It is not correct to determine whether a given window is a minibuffer by
1648comparing it with the result of @code{(minibuffer-window)}, because
1649there can be more than one minibuffer window if there is more than one
1650frame.
1651
1652@defun minibuffer-window-active-p window
1653This function returns non-@code{nil} if @var{window}, assumed to be
1654a minibuffer window, is currently active.
1655@end defun
1656
1657@defvar minibuffer-scroll-window
1658If the value of this variable is non-@code{nil}, it should be a window
1659object. When the function @code{scroll-other-window} is called in the
1660minibuffer, it scrolls this window.
1661@end defvar
1662
1663Finally, some functions and variables deal with recursive minibuffers
1664(@pxref{Recursive Editing}):
1665
1666@defun minibuffer-depth
1667This function returns the current depth of activations of the
1668minibuffer, a nonnegative integer. If no minibuffers are active, it
1669returns zero.
1670@end defun
1671
1672@defopt enable-recursive-minibuffers
1673If this variable is non-@code{nil}, you can invoke commands (such as
969fe9b5
RS
1674@code{find-file}) that use minibuffers even while the minibuffer window
1675is active. Such invocation produces a recursive editing level for a new
3e01fd9d
RS
1676minibuffer. The outer-level minibuffer is invisible while you are
1677editing the inner one.
1678
969fe9b5
RS
1679If this variable is @code{nil}, you cannot invoke minibuffer
1680commands when the minibuffer window is active, not even if you switch to
1681another window to do it.
3e01fd9d
RS
1682@end defopt
1683
1684@c Emacs 19 feature
1685If a command name has a property @code{enable-recursive-minibuffers}
793da230 1686that is non-@code{nil}, then the command can use the minibuffer to read
3e01fd9d 1687arguments even if it is invoked from the minibuffer. The minibuffer
bfe721d1
KH
1688command @code{next-matching-history-element} (normally @kbd{M-s} in the
1689minibuffer) uses this feature.
9a191eba
RS
1690
1691@defun minibuffer-message string &optional timeout
1692This function displays @var{string} temporarily at the end of the
1693minibuffer text, for @var{timeout} seconds. (The default is 2
1694seconds.)
1695@end defun
ab5796a9
MB
1696
1697@ignore
1698 arch-tag: bba7f945-9078-477f-a2ce-18818a6e1218
1699@end ignore