2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001, 2002,
4 @c 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../../info/minibuf
7 @node Minibuffers, Command Loop, Read and Print, Top
9 @cindex arguments, reading
10 @cindex complex arguments
13 A @dfn{minibuffer} is a special buffer that Emacs commands use to
14 read arguments more complicated than the single numeric prefix
15 argument. These arguments include file names, buffer names, and
16 command names (as in @kbd{M-x}). The minibuffer is displayed on the
17 bottom line of the frame, in the same place as the echo area
18 (@pxref{The Echo Area}), but only while it is in use for reading an
22 * Intro to Minibuffers:: Basic information about minibuffers.
23 * Text from Minibuffer:: How to read a straight text string.
24 * Object from Minibuffer:: How to read a Lisp object or expression.
25 * Minibuffer History:: Recording previous minibuffer inputs
26 so the user can reuse them.
27 * Initial Input:: Specifying initial contents for the minibuffer.
28 * Completion:: How to invoke and customize completion.
29 * Yes-or-No Queries:: Asking a question with a simple answer.
30 * Multiple Queries:: Asking a series of similar questions.
31 * Reading a Password:: Reading a password from the terminal.
32 * Minibuffer Commands:: Commands used as key bindings in minibuffers.
33 * Minibuffer Contents:: How such commands access the minibuffer text.
34 * Minibuffer Windows:: Operating on the special minibuffer windows.
35 * Recursive Mini:: Whether recursive entry to minibuffer is allowed.
36 * Minibuffer Misc:: Various customization hooks and variables.
39 @node Intro to Minibuffers
40 @section Introduction to Minibuffers
42 In most ways, a minibuffer is a normal Emacs buffer. Most operations
43 @emph{within} a buffer, such as editing commands, work normally in a
44 minibuffer. However, many operations for managing buffers do not apply
45 to minibuffers. The name of a minibuffer always has the form @w{@samp{
46 *Minibuf-@var{number}*}}, and it cannot be changed. Minibuffers are
47 displayed only in special windows used only for minibuffers; these
48 windows always appear at the bottom of a frame. (Sometimes frames have
49 no minibuffer window, and sometimes a special kind of frame contains
50 nothing but a minibuffer window; see @ref{Minibuffers and Frames}.)
52 The text in the minibuffer always starts with the @dfn{prompt string},
53 the text that was specified by the program that is using the minibuffer
54 to tell the user what sort of input to type. This text is marked
55 read-only so you won't accidentally delete or change it. It is also
56 marked as a field (@pxref{Fields}), so that certain motion functions,
57 including @code{beginning-of-line}, @code{forward-word},
58 @code{forward-sentence}, and @code{forward-paragraph}, stop at the
59 boundary between the prompt and the actual text. (In older Emacs
60 versions, the prompt was displayed using a special mechanism and was not
61 part of the buffer contents.)
63 The minibuffer's window is normally a single line; it grows
64 automatically if necessary if the contents require more space. You can
65 explicitly resize it temporarily with the window sizing commands; it
66 reverts to its normal size when the minibuffer is exited. You can
67 resize it permanently by using the window sizing commands in the frame's
68 other window, when the minibuffer is not active. If the frame contains
69 just a minibuffer, you can change the minibuffer's size by changing the
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 If a command uses a minibuffer while there is an active minibuffer,
78 this is called a @dfn{recursive minibuffer}. The first minibuffer is
79 named @w{@samp{ *Minibuf-0*}}. Recursive minibuffers are named by
80 incrementing the number at the end of the name. (The names begin with a
81 space so that they won't show up in normal buffer lists.) Of several
82 recursive minibuffers, the innermost (or most recently entered) is the
83 active minibuffer. We usually call this ``the'' minibuffer. You can
84 permit or forbid recursive minibuffers by setting the variable
85 @code{enable-recursive-minibuffers} or by putting properties of that
86 name on command symbols (@pxref{Recursive Mini}).
88 Like other buffers, a minibuffer uses a local keymap
89 (@pxref{Keymaps}) to specify special key bindings. The function that
90 invokes the minibuffer also sets up its local map according to the job
91 to be done. @xref{Text from Minibuffer}, for the non-completion
92 minibuffer local maps. @xref{Completion Commands}, for the minibuffer
93 local maps for completion.
95 When Emacs is running in batch mode, any request to read from the
96 minibuffer actually reads a line from the standard input descriptor that
97 was supplied when Emacs was started.
99 @node Text from Minibuffer
100 @section Reading Text Strings with the Minibuffer
102 Most often, the minibuffer is used to read text as a string. It can
103 also be used to read a Lisp object in textual form. The most basic
104 primitive for minibuffer input is @code{read-from-minibuffer}; it can
105 do either one. Regular expressions (@pxref{Regular Expressions}) are
106 a special kind of strings; use @code{read-regexp} for their minibuffer
107 input. There are also specialized commands for reading commands,
108 variables, file names, etc.@: (@pxref{Completion}).
110 In most cases, you should not call minibuffer input functions in the
111 middle of a Lisp function. Instead, do all minibuffer input as part of
112 reading the arguments for a command, in the @code{interactive}
113 specification. @xref{Defining Commands}.
115 @defun read-from-minibuffer prompt-string &optional initial-contents keymap read hist default inherit-input-method
116 This function is the most general way to get input through the
117 minibuffer. By default, it accepts arbitrary text and returns it as a
118 string; however, if @var{read} is non-@code{nil}, then it uses
119 @code{read} to convert the text into a Lisp object (@pxref{Input
122 The first thing this function does is to activate a minibuffer and
123 display it with @var{prompt-string} as the prompt. This value must be a
124 string. Then the user can edit text in the minibuffer.
126 When the user types a command to exit the minibuffer,
127 @code{read-from-minibuffer} constructs the return value from the text in
128 the minibuffer. Normally it returns a string containing that text.
129 However, if @var{read} is non-@code{nil}, @code{read-from-minibuffer}
130 reads the text and returns the resulting Lisp object, unevaluated.
131 (@xref{Input Functions}, for information about reading.)
133 The argument @var{default} specifies default values to make available
134 through the history commands. It should be a string, a list of
135 strings, or @code{nil}. The string or strings become the minibuffer's
136 ``future history,'' available to the user with @kbd{M-n}.
138 If @var{read} is non-@code{nil}, then @var{default} is also used
139 as the input to @code{read}, if the user enters empty input.
140 If @var{default} is a list of strings, the first string is used as the input.
141 If @var{default} is @code{nil}, empty input results in an @code{end-of-file} error.
142 However, in the usual case (where @var{read} is @code{nil}),
143 @code{read-from-minibuffer} ignores @var{default} when the user enters
144 empty input and returns an empty string, @code{""}. In this respect,
145 it differs from all the other minibuffer input functions in this chapter.
147 If @var{keymap} is non-@code{nil}, that keymap is the local keymap to
148 use in the minibuffer. If @var{keymap} is omitted or @code{nil}, the
149 value of @code{minibuffer-local-map} is used as the keymap. Specifying
150 a keymap is the most important way to customize the minibuffer for
151 various applications such as completion.
153 The argument @var{hist} specifies which history list variable to use
154 for saving the input and for history commands used in the minibuffer.
155 It defaults to @code{minibuffer-history}. @xref{Minibuffer History}.
157 If the variable @code{minibuffer-allow-text-properties} is
158 non-@code{nil}, then the string which is returned includes whatever text
159 properties were present in the minibuffer. Otherwise all the text
160 properties are stripped when the value is returned.
162 If the argument @var{inherit-input-method} is non-@code{nil}, then the
163 minibuffer inherits the current input method (@pxref{Input Methods}) and
164 the setting of @code{enable-multibyte-characters} (@pxref{Text
165 Representations}) from whichever buffer was current before entering the
168 Use of @var{initial-contents} is mostly deprecated; we recommend using
169 a non-@code{nil} value only in conjunction with specifying a cons cell
170 for @var{hist}. @xref{Initial Input}.
173 @defun read-string prompt &optional initial history default inherit-input-method
174 This function reads a string from the minibuffer and returns it. The
175 arguments @var{prompt}, @var{initial}, @var{history} and
176 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
177 The keymap used is @code{minibuffer-local-map}.
179 The optional argument @var{default} is used as in
180 @code{read-from-minibuffer}, except that, if non-@code{nil}, it also
181 specifies a default value to return if the user enters null input. As
182 in @code{read-from-minibuffer} it should be a string, a list of
183 strings, or @code{nil} which is equivalent to an empty string. When
184 @var{default} is a string, that string is the default value. When it
185 is a list of strings, the first string is the default value. (All
186 these strings are available to the user in the ``future minibuffer
189 This function works by calling the
190 @code{read-from-minibuffer} function:
194 (read-string @var{prompt} @var{initial} @var{history} @var{default} @var{inherit})
197 (read-from-minibuffer @var{prompt} @var{initial} nil nil
198 @var{history} @var{default} @var{inherit})))
199 (if (and (equal value "") @var{default})
200 (if (consp @var{default}) (car @var{default}) @var{default})
206 @defun read-regexp prompt &optional default-value
207 This function reads a regular expression as a string from the
208 minibuffer and returns it. The argument @var{prompt} is used as in
209 @code{read-from-minibuffer}. The keymap used is
210 @code{minibuffer-local-map}, and @code{regexp-history} is used as the
211 history list (@pxref{Minibuffer History, regexp-history}).
213 The optional argument @var{default-value} specifies a default value to
214 return if the user enters null input; it should be a string, or
215 @code{nil} which is equivalent to an empty string.
217 In addition, @code{read-regexp} collects a few useful candidates for
218 input and passes them to @code{read-from-minibuffer}, to make them
219 available to the user as the ``future minibuffer history list''
220 (@pxref{Minibuffer History, future list,, emacs, The GNU Emacs
221 Manual}). These candidates are:
225 The word or symbol at point.
227 The last regexp used in an incremental search.
229 The last string used in an incremental search.
231 The last string or pattern used in query-replace commands.
234 This function works by calling the @code{read-from-minibuffer}
235 function, after computing the list of defaults as described above.
238 @defvar minibuffer-allow-text-properties
239 If this variable is @code{nil}, then @code{read-from-minibuffer} strips
240 all text properties from the minibuffer input before returning it.
241 This variable also affects @code{read-string}. However,
242 @code{read-no-blanks-input} (see below), as well as
243 @code{read-minibuffer} and related functions (@pxref{Object from
244 Minibuffer,, Reading Lisp Objects With the Minibuffer}), and all
245 functions that do minibuffer input with completion, discard text
246 properties unconditionally, regardless of the value of this variable.
249 @defvar minibuffer-local-map
251 @anchor{Definition of minibuffer-local-map}
252 @c avoid page break at anchor; work around Texinfo deficiency
253 is the default local keymap for reading from the minibuffer. By
254 default, it makes the following bindings:
258 @code{exit-minibuffer}
261 @code{exit-minibuffer}
264 @code{abort-recursive-edit}
268 @code{next-history-element}
272 @code{previous-history-element}
275 @code{next-matching-history-element}
278 @code{previous-matching-history-element}
282 @c In version 18, initial is required
284 @defun read-no-blanks-input prompt &optional initial inherit-input-method
285 This function reads a string from the minibuffer, but does not allow
286 whitespace characters as part of the input: instead, those characters
287 terminate the input. The arguments @var{prompt}, @var{initial}, and
288 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
290 This is a simplified interface to the @code{read-from-minibuffer}
291 function, and passes the value of the @code{minibuffer-local-ns-map}
292 keymap as the @var{keymap} argument for that function. Since the keymap
293 @code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
294 possible to put a space into the string, by quoting it.
296 This function discards text properties, regardless of the value of
297 @code{minibuffer-allow-text-properties}.
301 (read-no-blanks-input @var{prompt} @var{initial})
303 (let (minibuffer-allow-text-properties)
304 (read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map))
309 @defvar minibuffer-local-ns-map
310 This built-in variable is the keymap used as the minibuffer local keymap
311 in the function @code{read-no-blanks-input}. By default, it makes the
312 following bindings, in addition to those of @code{minibuffer-local-map}:
316 @cindex @key{SPC} in minibuffer
317 @code{exit-minibuffer}
320 @cindex @key{TAB} in minibuffer
321 @code{exit-minibuffer}
324 @cindex @kbd{?} in minibuffer
325 @code{self-insert-and-exit}
329 @node Object from Minibuffer
330 @section Reading Lisp Objects with the Minibuffer
332 This section describes functions for reading Lisp objects with the
335 @defun read-minibuffer prompt &optional initial
336 This function reads a Lisp object using the minibuffer, and returns it
337 without evaluating it. The arguments @var{prompt} and @var{initial} are
338 used as in @code{read-from-minibuffer}.
340 This is a simplified interface to the
341 @code{read-from-minibuffer} function:
345 (read-minibuffer @var{prompt} @var{initial})
347 (let (minibuffer-allow-text-properties)
348 (read-from-minibuffer @var{prompt} @var{initial} nil t))
352 Here is an example in which we supply the string @code{"(testing)"} as
358 "Enter an expression: " (format "%s" '(testing)))
360 ;; @r{Here is how the minibuffer is displayed:}
364 ---------- Buffer: Minibuffer ----------
365 Enter an expression: (testing)@point{}
366 ---------- Buffer: Minibuffer ----------
371 The user can type @key{RET} immediately to use the initial input as a
372 default, or can edit the input.
375 @defun eval-minibuffer prompt &optional initial
376 This function reads a Lisp expression using the minibuffer, evaluates
377 it, then returns the result. The arguments @var{prompt} and
378 @var{initial} are used as in @code{read-from-minibuffer}.
380 This function simply evaluates the result of a call to
381 @code{read-minibuffer}:
385 (eval-minibuffer @var{prompt} @var{initial})
387 (eval (read-minibuffer @var{prompt} @var{initial}))
392 @defun edit-and-eval-command prompt form
393 This function reads a Lisp expression in the minibuffer, and then
394 evaluates it. The difference between this command and
395 @code{eval-minibuffer} is that here the initial @var{form} is not
396 optional and it is treated as a Lisp object to be converted to printed
397 representation rather than as a string of text. It is printed with
398 @code{prin1}, so if it is a string, double-quote characters (@samp{"})
399 appear in the initial text. @xref{Output Functions}.
401 The first thing @code{edit-and-eval-command} does is to activate the
402 minibuffer with @var{prompt} as the prompt. Then it inserts the printed
403 representation of @var{form} in the minibuffer, and lets the user edit it.
404 When the user exits the minibuffer, the edited text is read with
405 @code{read} and then evaluated. The resulting value becomes the value
406 of @code{edit-and-eval-command}.
408 In the following example, we offer the user an expression with initial
409 text which is a valid form already:
413 (edit-and-eval-command "Please edit: " '(forward-word 1))
415 ;; @r{After evaluation of the preceding expression,}
416 ;; @r{the following appears in the minibuffer:}
420 ---------- Buffer: Minibuffer ----------
421 Please edit: (forward-word 1)@point{}
422 ---------- Buffer: Minibuffer ----------
427 Typing @key{RET} right away would exit the minibuffer and evaluate the
428 expression, thus moving point forward one word.
429 @code{edit-and-eval-command} returns @code{nil} in this example.
432 @node Minibuffer History
433 @section Minibuffer History
434 @cindex minibuffer history
437 A @dfn{minibuffer history list} records previous minibuffer inputs so
438 the user can reuse them conveniently. A history list is actually a
439 symbol, not a list; it is a variable whose value is a list of strings
440 (previous inputs), most recent first.
442 There are many separate history lists, used for different kinds of
443 inputs. It's the Lisp programmer's job to specify the right history
444 list for each use of the minibuffer.
446 You specify the history list with the optional @var{hist} argument
447 to either @code{read-from-minibuffer} or @code{completing-read}. Here
448 are the possible values for it:
452 Use @var{variable} (a symbol) as the history list.
454 @item (@var{variable} . @var{startpos})
455 Use @var{variable} (a symbol) as the history list, and assume that the
456 initial history position is @var{startpos} (a nonnegative integer).
458 Specifying 0 for @var{startpos} is equivalent to just specifying the
459 symbol @var{variable}. @code{previous-history-element} will display
460 the most recent element of the history list in the minibuffer. If you
461 specify a positive @var{startpos}, the minibuffer history functions
462 behave as if @code{(elt @var{variable} (1- @var{STARTPOS}))} were the
463 history element currently shown in the minibuffer.
465 For consistency, you should also specify that element of the history
466 as the initial minibuffer contents, using the @var{initial} argument
467 to the minibuffer input function (@pxref{Initial Input}).
470 If you don't specify @var{hist}, then the default history list
471 @code{minibuffer-history} is used. For other standard history lists,
472 see below. You can also create your own history list variable; just
473 initialize it to @code{nil} before the first use.
475 Both @code{read-from-minibuffer} and @code{completing-read} add new
476 elements to the history list automatically, and provide commands to
477 allow the user to reuse items on the list. The only thing your program
478 needs to do to use a history list is to initialize it and to pass its
479 name to the input functions when you wish. But it is safe to modify the
480 list by hand when the minibuffer input functions are not using it.
482 Emacs functions that add a new element to a history list can also
483 delete old elements if the list gets too long. The variable
484 @code{history-length} specifies the maximum length for most history
485 lists. To specify a different maximum length for a particular history
486 list, put the length in the @code{history-length} property of the
487 history list symbol. The variable @code{history-delete-duplicates}
488 specifies whether to delete duplicates in history.
490 @defun add-to-history history-var newelt &optional maxelt keep-all
491 This function adds a new element @var{newelt}, if it isn't the empty
492 string, to the history list stored in the variable @var{history-var},
493 and returns the updated history list. It limits the list length to
494 the value of @var{maxelt} (if non-@code{nil}) or @code{history-length}
495 (described below). The possible values of @var{maxelt} have the same
496 meaning as the values of @code{history-length}.
498 Normally, @code{add-to-history} removes duplicate members from the
499 history list if @code{history-delete-duplicates} is non-@code{nil}.
500 However, if @var{keep-all} is non-@code{nil}, that says not to remove
501 duplicates, and to add @var{newelt} to the list even if it is empty.
504 @defvar history-add-new-input
505 If the value of this variable is @code{nil}, standard functions that
506 read from the minibuffer don't add new elements to the history list.
507 This lets Lisp programs explicitly manage input history by using
508 @code{add-to-history}. By default, @code{history-add-new-input} is
509 set to a non-@code{nil} value.
512 @defvar history-length
513 The value of this variable specifies the maximum length for all
514 history lists that don't specify their own maximum lengths. If the
515 value is @code{t}, that means there no maximum (don't delete old
516 elements). The value of @code{history-length} property of the history
517 list variable's symbol, if set, overrides this variable for that
518 particular history list.
521 @defvar history-delete-duplicates
522 If the value of this variable is @code{t}, that means when adding a
523 new history element, all previous identical elements are deleted.
526 Here are some of the standard minibuffer history list variables:
528 @defvar minibuffer-history
529 The default history list for minibuffer history input.
532 @defvar query-replace-history
533 A history list for arguments to @code{query-replace} (and similar
534 arguments to other commands).
537 @defvar file-name-history
538 A history list for file-name arguments.
541 @defvar buffer-name-history
542 A history list for buffer-name arguments.
545 @defvar regexp-history
546 A history list for regular expression arguments.
549 @defvar extended-command-history
550 A history list for arguments that are names of extended commands.
553 @defvar shell-command-history
554 A history list for arguments that are shell commands.
557 @defvar read-expression-history
558 A history list for arguments that are Lisp expressions to evaluate.
562 @section Initial Input
564 Several of the functions for minibuffer input have an argument called
565 @var{initial} or @var{initial-contents}. This is a mostly-deprecated
566 feature for specifying that the minibuffer should start out with
567 certain text, instead of empty as usual.
569 If @var{initial} is a string, the minibuffer starts out containing the
570 text of the string, with point at the end, when the user starts to
571 edit the text. If the user simply types @key{RET} to exit the
572 minibuffer, it will use the initial input string to determine the
575 @strong{We discourage use of a non-@code{nil} value for
576 @var{initial}}, because initial input is an intrusive interface.
577 History lists and default values provide a much more convenient method
578 to offer useful default inputs to the user.
580 There is just one situation where you should specify a string for an
581 @var{initial} argument. This is when you specify a cons cell for the
582 @var{hist} or @var{history} argument. @xref{Minibuffer History}.
584 @var{initial} can also be a cons cell of the form @code{(@var{string}
585 . @var{position})}. This means to insert @var{string} in the
586 minibuffer but put point at @var{position} within the string's text.
588 As a historical accident, @var{position} was implemented
589 inconsistently in different functions. In @code{completing-read},
590 @var{position}'s value is interpreted as origin-zero; that is, a value
591 of 0 means the beginning of the string, 1 means after the first
592 character, etc. In @code{read-minibuffer}, and the other
593 non-completion minibuffer input functions that support this argument,
594 1 means the beginning of the string 2 means after the first character,
597 Use of a cons cell as the value for @var{initial} arguments is
598 deprecated in user code.
604 @dfn{Completion} is a feature that fills in the rest of a name
605 starting from an abbreviation for it. Completion works by comparing the
606 user's input against a list of valid names and determining how much of
607 the name is determined uniquely by what the user has typed. For
608 example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
609 type the first few letters of the name of the buffer to which you wish
610 to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
611 extends the name as far as it can.
613 Standard Emacs commands offer completion for names of symbols, files,
614 buffers, and processes; with the functions in this section, you can
615 implement completion for other kinds of names.
617 The @code{try-completion} function is the basic primitive for
618 completion: it returns the longest determined completion of a given
619 initial string, with a given set of strings to match against.
621 The function @code{completing-read} provides a higher-level interface
622 for completion. A call to @code{completing-read} specifies how to
623 determine the list of valid names. The function then activates the
624 minibuffer with a local keymap that binds a few keys to commands useful
625 for completion. Other functions provide convenient simple interfaces
626 for reading certain kinds of names with completion.
629 * Basic Completion:: Low-level functions for completing strings.
630 (These are too low level to use the minibuffer.)
631 * Minibuffer Completion:: Invoking the minibuffer with completion.
632 * Completion Commands:: Minibuffer commands that do completion.
633 * High-Level Completion:: Convenient special cases of completion
634 (reading buffer name, file name, etc.)
635 * Reading File Names:: Using completion to read file names and
637 * Programmed Completion:: Writing your own completion-function.
640 @node Basic Completion
641 @subsection Basic Completion Functions
643 The completion functions @code{try-completion},
644 @code{all-completions} and @code{test-completion} have nothing in
645 themselves to do with minibuffers. We describe them in this chapter
646 so as to keep them near the higher-level completion features that do
649 If you store a completion alist in a variable, you should mark the
650 variable as ``risky'' with a non-@code{nil}
651 @code{risky-local-variable} property.
653 @defun try-completion string collection &optional predicate
654 This function returns the longest common substring of all possible
655 completions of @var{string} in @var{collection}. The value of
656 @var{collection} must be a list of strings or symbols, an alist, an
657 obarray, a hash table, or a function that implements a virtual set of
660 Completion compares @var{string} against each of the permissible
661 completions specified by @var{collection}; if the beginning of the
662 permissible completion equals @var{string}, it matches. If no permissible
663 completions match, @code{try-completion} returns @code{nil}. If only
664 one permissible completion matches, and the match is exact, then
665 @code{try-completion} returns @code{t}. Otherwise, the value is the
666 longest initial sequence common to all the permissible completions that
669 If @var{collection} is an alist (@pxref{Association Lists}), the
670 permissible completions are the elements of the alist that are either
671 strings, symbols, or conses whose @sc{car} is a string or symbol.
672 Symbols are converted to strings using @code{symbol-name}. Other
673 elements of the alist are ignored. (Remember that in Emacs Lisp, the
674 elements of alists do not @emph{have} to be conses.) In particular, a
675 list of strings or symbols is allowed, even though we usually do not
676 think of such lists as alists.
678 @cindex obarray in completion
679 If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
680 of all symbols in the obarray form the set of permissible completions. The
681 global variable @code{obarray} holds an obarray containing the names of
682 all interned Lisp symbols.
684 Note that the only valid way to make a new obarray is to create it
685 empty and then add symbols to it one by one using @code{intern}.
686 Also, you cannot intern a given symbol in more than one obarray.
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 symbol that is a function as @var{collection}. Then
692 the function is solely responsible for performing completion;
693 @code{try-completion} returns whatever this function returns. The
694 function is called with three arguments: @var{string}, @var{predicate}
695 and @code{nil}. (The reason for the third argument is so that the same
696 function can be used in @code{all-completions} and do the appropriate
697 thing in either 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
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 value
725 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
730 (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
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.
742 (try-completion "forw" obarray)
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}.
754 (> (length (car s)) 6))
760 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
767 @defun all-completions string collection &optional predicate nospace
768 This function returns a list of all possible completions of
769 @var{string}. The arguments to this function (aside from
770 @var{nospace}) are the same as those of @code{try-completion}. Also,
771 this function uses @code{completion-regexp-list} in the same way that
772 @code{try-completion} does. The optional argument @var{nospace} only
773 matters if @var{string} is the empty string. In that case, if
774 @var{nospace} is non-@code{nil}, completions that start with a space
777 If @var{collection} is a function, it is called with three arguments:
778 @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
779 returns whatever the function returns. @xref{Programmed Completion}.
781 Here is an example, using the function @code{test} shown in the
782 example for @code{try-completion}:
787 (> (length (car s)) 6))
794 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
796 @result{} ("foobar1" "foobar2")
801 @defun test-completion string collection &optional predicate
802 @anchor{Definition of test-completion}
803 This function returns non-@code{nil} if @var{string} is a valid
804 completion possibility specified by @var{collection} and
805 @var{predicate}. The arguments are the same as in
806 @code{try-completion}. For instance, if @var{collection} is a list of
807 strings, this is true if @var{string} appears in the list and
808 @var{predicate} is satisfied.
810 This function uses @code{completion-regexp-list} in the same
811 way that @code{try-completion} does.
813 If @var{predicate} is non-@code{nil} and if @var{collection} contains
814 several strings that are equal to each other, as determined by
815 @code{compare-strings} according to @code{completion-ignore-case},
816 then @var{predicate} should accept either all or none of them.
817 Otherwise, the return value of @code{test-completion} is essentially
820 If @var{collection} is a function, it is called with three arguments,
821 the values @var{string}, @var{predicate} and @code{lambda}; whatever
822 it returns, @code{test-completion} returns in turn.
825 @defvar completion-ignore-case
826 If the value of this variable is non-@code{nil}, Emacs does not
827 consider case significant in completion.
830 @defvar completion-regexp-list
831 This is a list of regular expressions. The completion functions only
832 consider a completion acceptable if it matches all regular expressions
833 in this list, with @code{case-fold-search} (@pxref{Searching and Case})
834 bound to the value of @code{completion-ignore-case}.
837 @defmac lazy-completion-table var fun
838 This macro provides a way to initialize the variable @var{var} as a
839 collection for completion in a lazy way, not computing its actual
840 contents until they are first needed. You use this macro to produce a
841 value that you store in @var{var}. The actual computation of the
842 proper value is done the first time you do completion using @var{var}.
843 It is done by calling @var{fun} with no arguments. The
844 value @var{fun} returns becomes the permanent value of @var{var}.
846 Here is an example of use:
849 (defvar foo (lazy-completion-table foo make-my-alist))
853 @node Minibuffer Completion
854 @subsection Completion and the Minibuffer
855 @cindex minibuffer completion
856 @cindex reading from minibuffer with completion
858 This section describes the basic interface for reading from the
859 minibuffer with completion.
861 @defun completing-read prompt collection &optional predicate require-match initial hist default inherit-input-method
862 This function reads a string in the minibuffer, assisting the user by
863 providing completion. It activates the minibuffer with prompt
864 @var{prompt}, which must be a string.
866 The actual completion is done by passing @var{collection} and
867 @var{predicate} to the function @code{try-completion}. This happens
868 in certain commands bound in the local keymaps used for completion.
869 Some of these commands also call @code{test-completion}. Thus, if
870 @var{predicate} is non-@code{nil}, it should be compatible with
871 @var{collection} and @code{completion-ignore-case}. @xref{Definition
874 If @var{require-match} is @code{nil}, the exit commands work regardless
875 of the input in the minibuffer. If @var{require-match} is @code{t}, the
876 usual minibuffer exit commands won't exit unless the input completes to
877 an element of @var{collection}. If @var{require-match} is
878 @code{confirm-only}, the user can exit with any input, but she will
879 be asked for a confirmation if the input is not an element of
880 @var{collection}. Any other value of @var{require-match} behaves like
881 @code{t}, except that the exit commands won't exit if it does non-null
884 However, empty input is always permitted, regardless of the value of
885 @var{require-match}; in that case, @code{completing-read} returns the
886 first element of @var{default}, if it is a list; @code{""}, if
887 @var{default} is @code{nil}; or @var{default}. The string or strings
888 in @var{default} are also available to the user through the history
891 The function @code{completing-read} uses
892 @code{minibuffer-local-completion-map} as the keymap if
893 @var{require-match} is @code{nil}, and uses
894 @code{minibuffer-local-must-match-map} if @var{require-match} is
895 non-@code{nil}. @xref{Completion Commands}.
897 The argument @var{hist} specifies which history list variable to use for
898 saving the input and for minibuffer history commands. It defaults to
899 @code{minibuffer-history}. @xref{Minibuffer History}.
901 The argument @var{initial} is mostly deprecated; we recommend using a
902 non-@code{nil} value only in conjunction with specifying a cons cell
903 for @var{hist}. @xref{Initial Input}. For default input, use
904 @var{default} instead.
906 If the argument @var{inherit-input-method} is non-@code{nil}, then the
907 minibuffer inherits the current input method (@pxref{Input
908 Methods}) and the setting of @code{enable-multibyte-characters}
909 (@pxref{Text Representations}) from whichever buffer was current before
910 entering the minibuffer.
912 If the built-in variable @code{completion-ignore-case} is
913 non-@code{nil}, completion ignores case when comparing the input
914 against the possible matches. @xref{Basic Completion}. In this mode
915 of operation, @var{predicate} must also ignore case, or you will get
918 Here's an example of using @code{completing-read}:
924 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
929 ;; @r{After evaluation of the preceding expression,}
930 ;; @r{the following appears in the minibuffer:}
932 ---------- Buffer: Minibuffer ----------
933 Complete a foo: fo@point{}
934 ---------- Buffer: Minibuffer ----------
939 If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
940 @code{completing-read} returns @code{barfoo}.
942 The @code{completing-read} function binds variables to pass
943 information to the commands that actually do completion.
944 They are described in the following section.
947 @node Completion Commands
948 @subsection Minibuffer Commands that Do Completion
950 This section describes the keymaps, commands and user options used
951 in the minibuffer to do completion. The description refers to the
952 situation when Partial Completion mode is disabled (as it is by
953 default). When enabled, this minor mode uses its own alternatives to
954 some of the commands described below. @xref{Completion Options,,,
955 emacs, The GNU Emacs Manual}, for a short description of Partial
958 @defvar minibuffer-completion-table
959 The value of this variable is the collection used for completion in
960 the minibuffer. This is the global variable that contains what
961 @code{completing-read} passes to @code{try-completion}. It is used by
962 minibuffer completion commands such as @code{minibuffer-complete-word}.
965 @defvar minibuffer-completion-predicate
966 This variable's value is the predicate that @code{completing-read}
967 passes to @code{try-completion}. The variable is also used by the other
968 minibuffer completion functions.
971 @defvar minibuffer-completion-confirm
972 When the value of this variable is non-@code{nil}, Emacs asks for
973 confirmation of a completion before exiting the minibuffer.
974 @code{completing-read} binds this variable, and the function
975 @code{minibuffer-complete-and-exit} checks the value before exiting.
978 @deffn Command minibuffer-complete-word
979 This function completes the minibuffer contents by at most a single
980 word. Even if the minibuffer contents have only one completion,
981 @code{minibuffer-complete-word} does not add any characters beyond the
982 first character that is not a word constituent. @xref{Syntax Tables}.
985 @deffn Command minibuffer-complete
986 This function completes the minibuffer contents as far as possible.
989 @deffn Command minibuffer-complete-and-exit
990 This function completes the minibuffer contents, and exits if
991 confirmation is not required, i.e., if
992 @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
993 @emph{is} required, it is given by repeating this command
994 immediately---the command is programmed to work without confirmation
995 when run twice in succession.
998 @deffn Command minibuffer-completion-help
999 This function creates a list of the possible completions of the
1000 current minibuffer contents. It works by calling @code{all-completions}
1001 using the value of the variable @code{minibuffer-completion-table} as
1002 the @var{collection} argument, and the value of
1003 @code{minibuffer-completion-predicate} as the @var{predicate} argument.
1004 The list of completions is displayed as text in a buffer named
1005 @samp{*Completions*}.
1008 @defun display-completion-list completions &optional common-substring
1009 This function displays @var{completions} to the stream in
1010 @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
1011 information about streams.) The argument @var{completions} is normally
1012 a list of completions just returned by @code{all-completions}, but it
1013 does not have to be. Each element may be a symbol or a string, either
1014 of which is simply printed. It can also be a list of two strings,
1015 which is printed as if the strings were concatenated. The first of
1016 the two strings is the actual completion, the second string serves as
1019 The argument @var{common-substring} is the prefix that is common to
1020 all the completions. With normal Emacs completion, it is usually the
1021 same as the string that was completed. @code{display-completion-list}
1022 uses this to highlight text in the completion list for better visual
1023 feedback. This is not needed in the minibuffer; for minibuffer
1024 completion, you can pass @code{nil}.
1026 This function is called by @code{minibuffer-completion-help}. The
1027 most common way to use it is together with
1028 @code{with-output-to-temp-buffer}, like this:
1031 (with-output-to-temp-buffer "*Completions*"
1032 (display-completion-list
1033 (all-completions (buffer-string) my-alist)
1038 @defopt completion-auto-help
1039 If this variable is non-@code{nil}, the completion commands
1040 automatically display a list of possible completions whenever nothing
1041 can be completed because the next character is not uniquely determined.
1044 @defvar minibuffer-local-completion-map
1045 @code{completing-read} uses this value as the local keymap when an
1046 exact match of one of the completions is not required. By default, this
1047 keymap makes the following bindings:
1051 @code{minibuffer-completion-help}
1054 @code{minibuffer-complete-word}
1057 @code{minibuffer-complete}
1061 with other characters bound as in @code{minibuffer-local-map}
1062 (@pxref{Definition of minibuffer-local-map}).
1065 @defvar minibuffer-local-must-match-map
1066 @code{completing-read} uses this value as the local keymap when an
1067 exact match of one of the completions is required. Therefore, no keys
1068 are bound to @code{exit-minibuffer}, the command that exits the
1069 minibuffer unconditionally. By default, this keymap makes the following
1074 @code{minibuffer-completion-help}
1077 @code{minibuffer-complete-word}
1080 @code{minibuffer-complete}
1083 @code{minibuffer-complete-and-exit}
1086 @code{minibuffer-complete-and-exit}
1090 with other characters bound as in @code{minibuffer-local-map}.
1093 @defvar minibuffer-local-filename-completion-map
1094 This is like @code{minibuffer-local-completion-map}
1095 except that it does not bind @key{SPC}. This keymap is used by the
1096 function @code{read-file-name}.
1099 @defvar minibuffer-local-filename-must-match-map
1100 This is like @code{minibuffer-local-must-match-map}
1101 except that it does not bind @key{SPC}. This keymap is used by the
1102 function @code{read-file-name}.
1105 @node High-Level Completion
1106 @subsection High-Level Completion Functions
1108 This section describes the higher-level convenient functions for
1109 reading certain sorts of names with completion.
1111 In most cases, you should not call these functions in the middle of a
1112 Lisp function. When possible, do all minibuffer input as part of
1113 reading the arguments for a command, in the @code{interactive}
1114 specification. @xref{Defining Commands}.
1116 @defun read-buffer prompt &optional default existing
1117 This function reads the name of a buffer and returns it as a string.
1118 The argument @var{default} is the default name to use, the value to
1119 return if the user exits with an empty minibuffer. If non-@code{nil},
1120 it should be a string, a list of strings, or a buffer. If it is
1121 a list, the default value is the first element of this list. It is
1122 mentioned in the prompt, but is not inserted in the minibuffer as
1125 The argument @var{prompt} should be a string ending with a colon and a
1126 space. If @var{default} is non-@code{nil}, the function inserts it in
1127 @var{prompt} before the colon to follow the convention for reading from
1128 the minibuffer with a default value (@pxref{Programming Tips}).
1130 If @var{existing} is non-@code{nil}, then the name specified must be
1131 that of an existing buffer. The usual commands to exit the minibuffer
1132 do not exit if the text is not valid, and @key{RET} does completion to
1133 attempt to find a valid name. If @var{existing} is neither @code{nil}
1134 nor @code{t}, confirmation is required after completion. (However,
1135 @var{default} is not checked for validity; it is returned, whatever it
1136 is, if the user exits with the minibuffer empty.)
1138 In the following example, the user enters @samp{minibuffer.t}, and
1139 then types @key{RET}. The argument @var{existing} is @code{t}, and the
1140 only buffer name starting with the given input is
1141 @samp{minibuffer.texi}, so that name is the value.
1144 (read-buffer "Buffer name: " "foo" t)
1146 ;; @r{After evaluation of the preceding expression,}
1147 ;; @r{the following prompt appears,}
1148 ;; @r{with an empty minibuffer:}
1152 ---------- Buffer: Minibuffer ----------
1153 Buffer name (default foo): @point{}
1154 ---------- Buffer: Minibuffer ----------
1158 ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
1159 @result{} "minibuffer.texi"
1164 @defvar read-buffer-function
1165 This variable specifies how to read buffer names. For example, if you
1166 set this variable to @code{iswitchb-read-buffer}, all Emacs commands
1167 that call @code{read-buffer} to read a buffer name will actually use the
1168 @code{iswitchb} package to read it.
1171 @defun read-command prompt &optional default
1172 This function reads the name of a command and returns it as a Lisp
1173 symbol. The argument @var{prompt} is used as in
1174 @code{read-from-minibuffer}. Recall that a command is anything for
1175 which @code{commandp} returns @code{t}, and a command name is a symbol
1176 for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
1178 The argument @var{default} specifies what to return if the user enters
1179 null input. It can be a symbol, a string or a list of strings. If it
1180 is a string, @code{read-command} interns it before returning it.
1181 If it is a list, @code{read-command} returns the first element of this list.
1182 If @var{default} is @code{nil}, that means no default has been
1183 specified; then if the user enters null input, the return value is
1184 @code{(intern "")}, that is, a symbol whose name is an empty string.
1187 (read-command "Command name? ")
1190 ;; @r{After evaluation of the preceding expression,}
1191 ;; @r{the following prompt appears with an empty minibuffer:}
1195 ---------- Buffer: Minibuffer ----------
1197 ---------- Buffer: Minibuffer ----------
1202 If the user types @kbd{forward-c @key{RET}}, then this function returns
1203 @code{forward-char}.
1205 The @code{read-command} function is a simplified interface to
1206 @code{completing-read}. It uses the variable @code{obarray} so as to
1207 complete in the set of extant Lisp symbols, and it uses the
1208 @code{commandp} predicate so as to accept only command names:
1210 @cindex @code{commandp} example
1213 (read-command @var{prompt})
1215 (intern (completing-read @var{prompt} obarray
1221 @defun read-variable prompt &optional default
1222 @anchor{Definition of read-variable}
1223 This function reads the name of a user variable and returns it as a
1226 The argument @var{default} specifies the default value to return if
1227 the user enters null input. It can be a symbol, a string, or a list
1228 of strings. If it is a string, @code{read-variable} interns it to
1229 make the default value. If it is a list, @code{read-variable} interns
1230 the first element. If @var{default} is @code{nil}, that means no
1231 default has been specified; then if the user enters null input, the
1232 return value is @code{(intern "")}.
1236 (read-variable "Variable name? ")
1238 ;; @r{After evaluation of the preceding expression,}
1239 ;; @r{the following prompt appears,}
1240 ;; @r{with an empty minibuffer:}
1244 ---------- Buffer: Minibuffer ----------
1245 Variable name? @point{}
1246 ---------- Buffer: Minibuffer ----------
1251 If the user then types @kbd{fill-p @key{RET}}, @code{read-variable}
1252 returns @code{fill-prefix}.
1254 In general, @code{read-variable} is similar to @code{read-command},
1255 but uses the predicate @code{user-variable-p} instead of
1258 @cindex @code{user-variable-p} example
1261 (read-variable @var{prompt})
1264 (completing-read @var{prompt} obarray
1265 'user-variable-p t nil))
1270 See also the functions @code{read-coding-system} and
1271 @code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems},
1272 and @code{read-input-method-name}, in @ref{Input Methods}.
1274 @node Reading File Names
1275 @subsection Reading File Names
1276 @cindex read file names
1277 @cindex prompt for file name
1279 Here is a couple of other high-level completion function, designed
1280 for reading file names and shell commands. They provide special
1281 features including automatic insertion of the default directory.
1283 @defun read-file-name prompt &optional directory default existing initial predicate
1284 This function reads a file name in the minibuffer, prompting with
1285 @var{prompt} and providing completion.
1287 If @var{existing} is non-@code{nil}, then the user must specify the name
1288 of an existing file; @key{RET} performs completion to make the name
1289 valid if possible, and then refuses to exit if it is not valid. If the
1290 value of @var{existing} is neither @code{nil} nor @code{t}, then
1291 @key{RET} also requires confirmation after completion. If
1292 @var{existing} is @code{nil}, then the name of a nonexistent file is
1295 @code{read-file-name} uses
1296 @code{minibuffer-local-filename-completion-map} as the keymap if
1297 @var{existing} is @code{nil}, and uses
1298 @code{minibuffer-local-filename-must-match-map} if @var{existing} is
1299 non-@code{nil}. @xref{Completion Commands}.
1301 The argument @var{directory} specifies the directory to use for
1302 completion of relative file names. It should be an absolute directory
1303 name. If @code{insert-default-directory} is non-@code{nil},
1304 @var{directory} is also inserted in the minibuffer as initial input.
1305 It defaults to the current buffer's value of @code{default-directory}.
1308 If you specify @var{initial}, that is an initial file name to insert
1309 in the buffer (after @var{directory}, if that is inserted). In this
1310 case, point goes at the beginning of @var{initial}. The default for
1311 @var{initial} is @code{nil}---don't insert any file name. To see what
1312 @var{initial} does, try the command @kbd{C-x C-v}. @strong{Please
1313 note:} we recommend using @var{default} rather than @var{initial} in
1316 If @var{default} is non-@code{nil}, then the function returns
1317 @var{default} if the user exits the minibuffer with the same non-empty
1318 contents that @code{read-file-name} inserted initially. The initial
1319 minibuffer contents are always non-empty if
1320 @code{insert-default-directory} is non-@code{nil}, as it is by
1321 default. @var{default} is not checked for validity, regardless of the
1322 value of @var{existing}. However, if @var{existing} is
1323 non-@code{nil}, the initial minibuffer contents should be a valid file
1324 (or directory) name. Otherwise @code{read-file-name} attempts
1325 completion if the user exits without any editing, and does not return
1326 @var{default}. @var{default} is also available through the history
1329 If @var{default} is @code{nil}, @code{read-file-name} tries to find a
1330 substitute default to use in its place, which it treats in exactly the
1331 same way as if it had been specified explicitly. If @var{default} is
1332 @code{nil}, but @var{initial} is non-@code{nil}, then the default is
1333 the absolute file name obtained from @var{directory} and
1334 @var{initial}. If both @var{default} and @var{initial} are @code{nil}
1335 and the buffer is visiting a file, @code{read-file-name} uses the
1336 absolute file name of that file as default. If the buffer is not
1337 visiting a file, then there is no default. In that case, if the user
1338 types @key{RET} without any editing, @code{read-file-name} simply
1339 returns the pre-inserted contents of the minibuffer.
1341 If the user types @key{RET} in an empty minibuffer, this function
1342 returns an empty string, regardless of the value of @var{existing}.
1343 This is, for instance, how the user can make the current buffer visit
1344 no file using @code{M-x set-visited-file-name}.
1346 If @var{predicate} is non-@code{nil}, it specifies a function of one
1347 argument that decides which file names are acceptable completion
1348 possibilities. A file name is an acceptable value if @var{predicate}
1349 returns non-@code{nil} for it.
1351 @code{read-file-name} does not automatically expand file names. You
1352 must call @code{expand-file-name} yourself if an absolute file name is
1359 (read-file-name "The file is ")
1361 ;; @r{After evaluation of the preceding expression,}
1362 ;; @r{the following appears in the minibuffer:}
1366 ---------- Buffer: Minibuffer ----------
1367 The file is /gp/gnu/elisp/@point{}
1368 ---------- Buffer: Minibuffer ----------
1373 Typing @kbd{manual @key{TAB}} results in the following:
1377 ---------- Buffer: Minibuffer ----------
1378 The file is /gp/gnu/elisp/manual.texi@point{}
1379 ---------- Buffer: Minibuffer ----------
1383 @c Wordy to avoid overfull hbox in smallbook mode.
1385 If the user types @key{RET}, @code{read-file-name} returns the file name
1386 as the string @code{"/gp/gnu/elisp/manual.texi"}.
1389 @defvar read-file-name-function
1390 If non-@code{nil}, this should be a function that accepts the same
1391 arguments as @code{read-file-name}. When @code{read-file-name} is
1392 called, it calls this function with the supplied arguments instead of
1393 doing its usual work.
1396 @defvar read-file-name-completion-ignore-case
1397 If this variable is non-@code{nil}, @code{read-file-name} ignores case
1398 when performing completion.
1401 @defun read-directory-name prompt &optional directory default existing initial
1402 This function is like @code{read-file-name} but allows only directory
1403 names as completion possibilities.
1405 If @var{default} is @code{nil} and @var{initial} is non-@code{nil},
1406 @code{read-directory-name} constructs a substitute default by
1407 combining @var{directory} (or the current buffer's default directory
1408 if @var{directory} is @code{nil}) and @var{initial}. If both
1409 @var{default} and @var{initial} are @code{nil}, this function uses
1410 @var{directory} as substitute default, or the current buffer's default
1411 directory if @var{directory} is @code{nil}.
1414 @defopt insert-default-directory
1415 This variable is used by @code{read-file-name}, and thus, indirectly,
1416 by most commands reading file names. (This includes all commands that
1417 use the code letters @samp{f} or @samp{F} in their interactive form.
1418 @xref{Interactive Codes,, Code Characters for interactive}.) Its
1419 value controls whether @code{read-file-name} starts by placing the
1420 name of the default directory in the minibuffer, plus the initial file
1421 name if any. If the value of this variable is @code{nil}, then
1422 @code{read-file-name} does not place any initial input in the
1423 minibuffer (unless you specify initial input with the @var{initial}
1424 argument). In that case, the default directory is still used for
1425 completion of relative file names, but is not displayed.
1427 If this variable is @code{nil} and the initial minibuffer contents are
1428 empty, the user may have to explicitly fetch the next history element
1429 to access a default value. If the variable is non-@code{nil}, the
1430 initial minibuffer contents are always non-empty and the user can
1431 always request a default value by immediately typing @key{RET} in an
1432 unedited minibuffer. (See above.)
1438 ;; @r{Here the minibuffer starts out with the default directory.}
1439 (let ((insert-default-directory t))
1440 (read-file-name "The file is "))
1444 ---------- Buffer: Minibuffer ----------
1445 The file is ~lewis/manual/@point{}
1446 ---------- Buffer: Minibuffer ----------
1450 ;; @r{Here the minibuffer is empty and only the prompt}
1451 ;; @r{appears on its line.}
1452 (let ((insert-default-directory nil))
1453 (read-file-name "The file is "))
1457 ---------- Buffer: Minibuffer ----------
1458 The file is @point{}
1459 ---------- Buffer: Minibuffer ----------
1464 @defun read-shell-command prompt &optional initial-contents hist &rest args
1465 This function reads a shell command from the minibuffer, prompting
1466 with @var{prompt} and providing intelligent completion. It completes
1467 the first word of the command using candidates that are appropriate
1468 for command names. The rest of the shell command arguments are
1469 completed as file names.
1471 This function works by calling @code{read-from-minibuffer}
1472 (@pxref{Text from Minibuffer}), passing it
1473 @code{minibuffer-local-shell-command-map} as the @var{keymap}
1474 argument. The optional arguments @var{initial-contents} and
1475 @var{hist} are passed to @code{read-from-minibuffer} unaltered, except
1476 that if @var{hist} is omitted or @code{nil}, it defaults to
1477 @code{shell-command-history} (@pxref{Minibuffer History,
1478 shell-command-history}), and the @var{read} argument is passed as
1479 @code{nil}. The rest of @var{args}, if present, are used by
1480 @code{read-from-minibuffer} as its @var{default} and
1481 @var{inherit-input-method} arguments.
1484 @defvar minibuffer-local-shell-command-map
1485 This keymap is used by @code{read-shell-command} for completing
1486 command and file names that are part of a shell command.
1489 @node Programmed Completion
1490 @subsection Programmed Completion
1491 @cindex programmed completion
1493 Sometimes it is not possible to create an alist or an obarray
1494 containing all the intended possible completions. In such a case, you
1495 can supply your own function to compute the completion of a given string.
1496 This is called @dfn{programmed completion}.
1498 To use this feature, pass a symbol with a function definition as the
1499 @var{collection} argument to @code{completing-read}. The function
1500 @code{completing-read} arranges to pass your completion function along
1501 to @code{try-completion} and @code{all-completions}, which will then let
1502 your function do all the work.
1504 The completion function should accept three arguments:
1508 The string to be completed.
1511 The predicate function to filter possible matches, or @code{nil} if
1512 none. Your function should call the predicate for each possible match,
1513 and ignore the possible match if the predicate returns @code{nil}.
1516 A flag specifying the type of operation.
1519 There are three flag values for three operations:
1523 @code{nil} specifies @code{try-completion}. The completion function
1524 should return the completion of the specified string, or @code{t} if the
1525 string is a unique and exact match already, or @code{nil} if the string
1526 matches no possibility.
1528 If the string is an exact match for one possibility, but also matches
1529 other longer possibilities, the function should return the string, not
1533 @code{t} specifies @code{all-completions}. The completion function
1534 should return a list of all possible completions of the specified
1538 @code{lambda} specifies @code{test-completion}. The completion
1539 function should return @code{t} if the specified string is an exact
1540 match for some possibility; @code{nil} otherwise.
1543 It would be consistent and clean for completion functions to allow
1544 lambda expressions (lists that are functions) as well as function
1545 symbols as @var{collection}, but this is impossible. Lists as
1546 completion tables already have other meanings, and it would be
1547 unreliable to treat one differently just because it is also a possible
1548 function. So you must arrange for any function you wish to use for
1549 completion to be encapsulated in a symbol.
1551 Emacs uses programmed completion when completing file names.
1552 @xref{File Name Completion}.
1554 @defun completion-table-dynamic function
1555 This function is a convenient way to write a function that can act as
1556 programmed completion function. The argument @var{function} should be
1557 a function that takes one argument, a string, and returns an alist of
1558 possible completions of it. You can think of
1559 @code{completion-table-dynamic} as a transducer between that interface
1560 and the interface for programmed completion functions.
1563 @node Yes-or-No Queries
1564 @section Yes-or-No Queries
1565 @cindex asking the user questions
1566 @cindex querying the user
1567 @cindex yes-or-no questions
1569 This section describes functions used to ask the user a yes-or-no
1570 question. The function @code{y-or-n-p} can be answered with a single
1571 character; it is useful for questions where an inadvertent wrong answer
1572 will not have serious consequences. @code{yes-or-no-p} is suitable for
1573 more momentous questions, since it requires three or four characters to
1576 If either of these functions is called in a command that was invoked
1577 using the mouse---more precisely, if @code{last-nonmenu-event}
1578 (@pxref{Command Loop Info}) is either @code{nil} or a list---then it
1579 uses a dialog box or pop-up menu to ask the question. Otherwise, it
1580 uses keyboard input. You can force use of the mouse or use of keyboard
1581 input by binding @code{last-nonmenu-event} to a suitable value around
1584 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
1585 @code{y-or-n-p} does not; but it seems best to describe them together.
1587 @defun y-or-n-p prompt
1588 This function asks the user a question, expecting input in the echo
1589 area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
1590 user types @kbd{n}. This function also accepts @key{SPC} to mean yes
1591 and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit,'' like
1592 @kbd{C-g}, because the question might look like a minibuffer and for
1593 that reason the user might try to use @kbd{C-]} to get out. The answer
1594 is a single character, with no @key{RET} needed to terminate it. Upper
1595 and lower case are equivalent.
1597 ``Asking the question'' means printing @var{prompt} in the echo area,
1598 followed by the string @w{@samp{(y or n) }}. If the input is not one of
1599 the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
1600 @kbd{@key{DEL}}, or something that quits), the function responds
1601 @samp{Please answer y or n.}, and repeats the request.
1603 This function does not actually use the minibuffer, since it does not
1604 allow editing of the answer. It actually uses the echo area (@pxref{The
1605 Echo Area}), which uses the same screen space as the minibuffer. The
1606 cursor moves to the echo area while the question is being asked.
1608 The answers and their meanings, even @samp{y} and @samp{n}, are not
1609 hardwired. The keymap @code{query-replace-map} specifies them.
1610 @xref{Search and Replace}.
1612 In the following example, the user first types @kbd{q}, which is
1613 invalid. At the next prompt the user types @kbd{y}.
1617 (y-or-n-p "Do you need a lift? ")
1619 ;; @r{After evaluation of the preceding expression,}
1620 ;; @r{the following prompt appears in the echo area:}
1624 ---------- Echo area ----------
1625 Do you need a lift? (y or n)
1626 ---------- Echo area ----------
1629 ;; @r{If the user then types @kbd{q}, the following appears:}
1632 ---------- Echo area ----------
1633 Please answer y or n. Do you need a lift? (y or n)
1634 ---------- Echo area ----------
1637 ;; @r{When the user types a valid answer,}
1638 ;; @r{it is displayed after the question:}
1641 ---------- Echo area ----------
1642 Do you need a lift? (y or n) y
1643 ---------- Echo area ----------
1648 We show successive lines of echo area messages, but only one actually
1649 appears on the screen at a time.
1652 @defun y-or-n-p-with-timeout prompt seconds default-value
1653 Like @code{y-or-n-p}, except that if the user fails to answer within
1654 @var{seconds} seconds, this function stops waiting and returns
1655 @var{default-value}. It works by setting up a timer; see @ref{Timers}.
1656 The argument @var{seconds} may be an integer or a floating point number.
1659 @defun yes-or-no-p prompt
1660 This function asks the user a question, expecting input in the
1661 minibuffer. It returns @code{t} if the user enters @samp{yes},
1662 @code{nil} if the user types @samp{no}. The user must type @key{RET} to
1663 finalize the response. Upper and lower case are equivalent.
1665 @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
1666 followed by @w{@samp{(yes or no) }}. The user must type one of the
1667 expected responses; otherwise, the function responds @samp{Please answer
1668 yes or no.}, waits about two seconds and repeats the request.
1670 @code{yes-or-no-p} requires more work from the user than
1671 @code{y-or-n-p} and is appropriate for more crucial decisions.
1677 (yes-or-no-p "Do you really want to remove everything? ")
1679 ;; @r{After evaluation of the preceding expression,}
1680 ;; @r{the following prompt appears,}
1681 ;; @r{with an empty minibuffer:}
1685 ---------- Buffer: minibuffer ----------
1686 Do you really want to remove everything? (yes or no)
1687 ---------- Buffer: minibuffer ----------
1692 If the user first types @kbd{y @key{RET}}, which is invalid because this
1693 function demands the entire word @samp{yes}, it responds by displaying
1694 these prompts, with a brief pause between them:
1698 ---------- Buffer: minibuffer ----------
1699 Please answer yes or no.
1700 Do you really want to remove everything? (yes or no)
1701 ---------- Buffer: minibuffer ----------
1706 @node Multiple Queries
1707 @section Asking Multiple Y-or-N Questions
1709 When you have a series of similar questions to ask, such as ``Do you
1710 want to save this buffer'' for each buffer in turn, you should use
1711 @code{map-y-or-n-p} to ask the collection of questions, rather than
1712 asking each question individually. This gives the user certain
1713 convenient facilities such as the ability to answer the whole series at
1716 @defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
1717 This function asks the user a series of questions, reading a
1718 single-character answer in the echo area for each one.
1720 The value of @var{list} specifies the objects to ask questions about.
1721 It should be either a list of objects or a generator function. If it is
1722 a function, it should expect no arguments, and should return either the
1723 next object to ask about, or @code{nil} meaning stop asking questions.
1725 The argument @var{prompter} specifies how to ask each question. If
1726 @var{prompter} is a string, the question text is computed like this:
1729 (format @var{prompter} @var{object})
1733 where @var{object} is the next object to ask about (as obtained from
1736 If not a string, @var{prompter} should be a function of one argument
1737 (the next object to ask about) and should return the question text. If
1738 the value is a string, that is the question to ask the user. The
1739 function can also return @code{t} meaning do act on this object (and
1740 don't ask the user), or @code{nil} meaning ignore this object (and don't
1743 The argument @var{actor} says how to act on the answers that the user
1744 gives. It should be a function of one argument, and it is called with
1745 each object that the user says yes for. Its argument is always an
1746 object obtained from @var{list}.
1748 If the argument @var{help} is given, it should be a list of this form:
1751 (@var{singular} @var{plural} @var{action})
1755 where @var{singular} is a string containing a singular noun that
1756 describes the objects conceptually being acted on, @var{plural} is the
1757 corresponding plural noun, and @var{action} is a transitive verb
1758 describing what @var{actor} does.
1760 If you don't specify @var{help}, the default is @code{("object"
1761 "objects" "act on")}.
1763 Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
1764 @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
1765 that object; @kbd{!} to act on all following objects; @key{ESC} or
1766 @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
1767 the current object and then exit; or @kbd{C-h} to get help. These are
1768 the same answers that @code{query-replace} accepts. The keymap
1769 @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
1770 as well as for @code{query-replace}; see @ref{Search and Replace}.
1772 You can use @var{action-alist} to specify additional possible answers
1773 and what they mean. It is an alist of elements of the form
1774 @code{(@var{char} @var{function} @var{help})}, each of which defines one
1775 additional answer. In this element, @var{char} is a character (the
1776 answer); @var{function} is a function of one argument (an object from
1777 @var{list}); @var{help} is a string.
1779 When the user responds with @var{char}, @code{map-y-or-n-p} calls
1780 @var{function}. If it returns non-@code{nil}, the object is considered
1781 ``acted upon,'' and @code{map-y-or-n-p} advances to the next object in
1782 @var{list}. If it returns @code{nil}, the prompt is repeated for the
1785 Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
1786 prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
1789 If @code{map-y-or-n-p} is called in a command that was invoked using the
1790 mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
1791 Loop Info}) is either @code{nil} or a list---then it uses a dialog box
1792 or pop-up menu to ask the question. In this case, it does not use
1793 keyboard input or the echo area. You can force use of the mouse or use
1794 of keyboard input by binding @code{last-nonmenu-event} to a suitable
1795 value around the call.
1797 The return value of @code{map-y-or-n-p} is the number of objects acted on.
1800 @node Reading a Password
1801 @section Reading a Password
1802 @cindex passwords, reading
1804 To read a password to pass to another program, you can use the
1805 function @code{read-passwd}.
1807 @defun read-passwd prompt &optional confirm default
1808 This function reads a password, prompting with @var{prompt}. It does
1809 not echo the password as the user types it; instead, it echoes @samp{.}
1810 for each character in the password.
1812 The optional argument @var{confirm}, if non-@code{nil}, says to read the
1813 password twice and insist it must be the same both times. If it isn't
1814 the same, the user has to type it over and over until the last two
1817 The optional argument @var{default} specifies the default password to
1818 return if the user enters empty input. If @var{default} is @code{nil},
1819 then @code{read-passwd} returns the null string in that case.
1822 @node Minibuffer Commands
1823 @section Minibuffer Commands
1825 This section describes some commands meant for use in the
1828 @deffn Command exit-minibuffer
1829 This command exits the active minibuffer. It is normally bound to
1830 keys in minibuffer local keymaps.
1833 @deffn Command self-insert-and-exit
1834 This command exits the active minibuffer after inserting the last
1835 character typed on the keyboard (found in @code{last-command-char};
1836 @pxref{Command Loop Info}).
1839 @deffn Command previous-history-element n
1840 This command replaces the minibuffer contents with the value of the
1841 @var{n}th previous (older) history element.
1844 @deffn Command next-history-element n
1845 This command replaces the minibuffer contents with the value of the
1846 @var{n}th more recent history element.
1849 @deffn Command previous-matching-history-element pattern n
1850 This command replaces the minibuffer contents with the value of the
1851 @var{n}th previous (older) history element that matches @var{pattern} (a
1852 regular expression).
1855 @deffn Command next-matching-history-element pattern n
1856 This command replaces the minibuffer contents with the value of the
1857 @var{n}th next (newer) history element that matches @var{pattern} (a
1858 regular expression).
1861 @node Minibuffer Windows
1862 @section Minibuffer Windows
1863 @cindex minibuffer windows
1865 These functions access and select minibuffer windows
1866 and test whether they are active.
1868 @defun active-minibuffer-window
1869 This function returns the currently active minibuffer window, or
1870 @code{nil} if none is currently active.
1873 @defun minibuffer-window &optional frame
1874 @anchor{Definition of minibuffer-window}
1875 This function returns the minibuffer window used for frame @var{frame}.
1876 If @var{frame} is @code{nil}, that stands for the current frame. Note
1877 that the minibuffer window used by a frame need not be part of that
1878 frame---a frame that has no minibuffer of its own necessarily uses some
1879 other frame's minibuffer window.
1882 @defun set-minibuffer-window window
1883 This function specifies @var{window} as the minibuffer window to use.
1884 This affects where the minibuffer is displayed if you put text in it
1885 without invoking the usual minibuffer commands. It has no effect on
1886 the usual minibuffer input functions because they all start by
1887 choosing the minibuffer window according to the current frame.
1891 @defun window-minibuffer-p &optional window
1892 This function returns non-@code{nil} if @var{window} is a minibuffer
1894 @var{window} defaults to the selected window.
1897 It is not correct to determine whether a given window is a minibuffer by
1898 comparing it with the result of @code{(minibuffer-window)}, because
1899 there can be more than one minibuffer window if there is more than one
1902 @defun minibuffer-window-active-p window
1903 This function returns non-@code{nil} if @var{window}, assumed to be
1904 a minibuffer window, is currently active.
1907 @node Minibuffer Contents
1908 @section Minibuffer Contents
1910 These functions access the minibuffer prompt and contents.
1912 @defun minibuffer-prompt
1913 This function returns the prompt string of the currently active
1914 minibuffer. If no minibuffer is active, it returns @code{nil}.
1917 @defun minibuffer-prompt-end
1918 This function returns the current
1919 position of the end of the minibuffer prompt, if a minibuffer is
1920 current. Otherwise, it returns the minimum valid buffer position.
1923 @defun minibuffer-prompt-width
1924 This function returns the current display-width of the minibuffer
1925 prompt, if a minibuffer is current. Otherwise, it returns zero.
1928 @defun minibuffer-contents
1929 This function returns the editable
1930 contents of the minibuffer (that is, everything except the prompt) as
1931 a string, if a minibuffer is current. Otherwise, it returns the
1932 entire contents of the current buffer.
1935 @defun minibuffer-contents-no-properties
1936 This is like @code{minibuffer-contents}, except that it does not copy text
1937 properties, just the characters themselves. @xref{Text Properties}.
1940 @defun minibuffer-completion-contents
1941 This is like @code{minibuffer-contents}, except that it returns only
1942 the contents before point. That is the part that completion commands
1943 operate on. @xref{Minibuffer Completion}.
1946 @defun delete-minibuffer-contents
1947 This function erases the editable contents of the minibuffer (that is,
1948 everything except the prompt), if a minibuffer is current. Otherwise,
1949 it erases the entire current buffer.
1952 @node Recursive Mini
1953 @section Recursive Minibuffers
1954 @cindex recursive minibuffers
1956 These functions and variables deal with recursive minibuffers
1957 (@pxref{Recursive Editing}):
1959 @defun minibuffer-depth
1960 This function returns the current depth of activations of the
1961 minibuffer, a nonnegative integer. If no minibuffers are active, it
1965 @defopt enable-recursive-minibuffers
1966 If this variable is non-@code{nil}, you can invoke commands (such as
1967 @code{find-file}) that use minibuffers even while the minibuffer window
1968 is active. Such invocation produces a recursive editing level for a new
1969 minibuffer. The outer-level minibuffer is invisible while you are
1970 editing the inner one.
1972 If this variable is @code{nil}, you cannot invoke minibuffer
1973 commands when the minibuffer window is active, not even if you switch to
1974 another window to do it.
1978 If a command name has a property @code{enable-recursive-minibuffers}
1979 that is non-@code{nil}, then the command can use the minibuffer to read
1980 arguments even if it is invoked from the minibuffer. A command can
1981 also achieve this by binding @code{enable-recursive-minibuffers}
1982 to @code{t} in the interactive declaration (@pxref{Using Interactive}).
1983 The minibuffer command @code{next-matching-history-element} (normally
1984 @kbd{M-s} in the minibuffer) does the latter.
1986 @node Minibuffer Misc
1987 @section Minibuffer Miscellany
1989 @defun minibufferp &optional buffer-or-name
1990 This function returns non-@code{nil} if @var{buffer-or-name} is a
1991 minibuffer. If @var{buffer-or-name} is omitted, it tests the current
1995 @defvar minibuffer-setup-hook
1996 This is a normal hook that is run whenever the minibuffer is entered.
2000 @defvar minibuffer-exit-hook
2001 This is a normal hook that is run whenever the minibuffer is exited.
2005 @defvar minibuffer-help-form
2006 @anchor{Definition of minibuffer-help-form}
2007 The current value of this variable is used to rebind @code{help-form}
2008 locally inside the minibuffer (@pxref{Help Functions}).
2011 @defvar minibuffer-scroll-window
2012 @anchor{Definition of minibuffer-scroll-window}
2013 If the value of this variable is non-@code{nil}, it should be a window
2014 object. When the function @code{scroll-other-window} is called in the
2015 minibuffer, it scrolls this window.
2018 @defun minibuffer-selected-window
2019 This function returns the window which was selected when the
2020 minibuffer was entered. If selected window is not a minibuffer
2021 window, it returns @code{nil}.
2024 @defopt max-mini-window-height
2025 This variable specifies the maximum height for resizing minibuffer
2026 windows. If a float, it specifies a fraction of the height of the
2027 frame. If an integer, it specifies a number of lines.
2030 @defun minibuffer-message string
2031 This function displays @var{string} temporarily at the end of the
2032 minibuffer text, for two seconds, or until the next input event
2033 arrives, whichever comes first.
2037 arch-tag: bba7f945-9078-477f-a2ce-18818a6e1218