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