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