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