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