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