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