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
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}, if non-@code{nil}, specifies a
214 default value to return if the user enters null input. As
215 in @code{read-from-minibuffer} it should be a string, a list of
216 strings, or @code{nil} which is equivalent to an empty string. When
217 @var{default} is a string, that string is the default value. When it
218 is a list of strings, the first string is the default value. To this
219 default @code{read-regexp} adds a few other useful candidates, and
220 passes them to @code{read-from-minibuffer} to make them available to
221 the user as the ``future minibuffer history list'' (@pxref{Minibuffer
222 History, future list,, emacs, The GNU Emacs Manual}). These
227 The Word or symbol at point.
229 The last regexp used in an incremental search.
231 The last string used in an incremental search.
233 The last string or pattern used in query-replace commands.
236 This function works by calling the @code{read-from-minibuffer}
237 function, after computing the list of defaults as described above.
240 @defvar minibuffer-allow-text-properties
241 If this variable is @code{nil}, then @code{read-from-minibuffer} strips
242 all text properties from the minibuffer input before returning it.
243 This variable also affects @code{read-string}. However,
244 @code{read-no-blanks-input} (see below), as well as
245 @code{read-minibuffer} and related functions (@pxref{Object from
246 Minibuffer,, Reading Lisp Objects With the Minibuffer}), and all
247 functions that do minibuffer input with completion, discard text
248 properties unconditionally, regardless of the value of this variable.
251 @defvar minibuffer-local-map
253 @anchor{Definition of minibuffer-local-map}
254 @c avoid page break at anchor; work around Texinfo deficiency
255 is the default local keymap for reading from the minibuffer. By
256 default, it makes the following bindings:
260 @code{exit-minibuffer}
263 @code{exit-minibuffer}
266 @code{abort-recursive-edit}
270 @code{next-history-element}
274 @code{previous-history-element}
277 @code{next-matching-history-element}
280 @code{previous-matching-history-element}
284 @c In version 18, initial is required
286 @defun read-no-blanks-input prompt &optional initial inherit-input-method
287 This function reads a string from the minibuffer, but does not allow
288 whitespace characters as part of the input: instead, those characters
289 terminate the input. The arguments @var{prompt}, @var{initial}, and
290 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
292 This is a simplified interface to the @code{read-from-minibuffer}
293 function, and passes the value of the @code{minibuffer-local-ns-map}
294 keymap as the @var{keymap} argument for that function. Since the keymap
295 @code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
296 possible to put a space into the string, by quoting it.
298 This function discards text properties, regardless of the value of
299 @code{minibuffer-allow-text-properties}.
303 (read-no-blanks-input @var{prompt} @var{initial})
305 (let (minibuffer-allow-text-properties)
306 (read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map))
311 @defvar minibuffer-local-ns-map
312 This built-in variable is the keymap used as the minibuffer local keymap
313 in the function @code{read-no-blanks-input}. By default, it makes the
314 following bindings, in addition to those of @code{minibuffer-local-map}:
318 @cindex @key{SPC} in minibuffer
319 @code{exit-minibuffer}
322 @cindex @key{TAB} in minibuffer
323 @code{exit-minibuffer}
326 @cindex @kbd{?} in minibuffer
327 @code{self-insert-and-exit}
331 @node Object from Minibuffer
332 @section Reading Lisp Objects with the Minibuffer
334 This section describes functions for reading Lisp objects with the
337 @defun read-minibuffer prompt &optional initial
338 This function reads a Lisp object using the minibuffer, and returns it
339 without evaluating it. The arguments @var{prompt} and @var{initial} are
340 used as in @code{read-from-minibuffer}.
342 This is a simplified interface to the
343 @code{read-from-minibuffer} function:
347 (read-minibuffer @var{prompt} @var{initial})
349 (let (minibuffer-allow-text-properties)
350 (read-from-minibuffer @var{prompt} @var{initial} nil t))
354 Here is an example in which we supply the string @code{"(testing)"} as
360 "Enter an expression: " (format "%s" '(testing)))
362 ;; @r{Here is how the minibuffer is displayed:}
366 ---------- Buffer: Minibuffer ----------
367 Enter an expression: (testing)@point{}
368 ---------- Buffer: Minibuffer ----------
373 The user can type @key{RET} immediately to use the initial input as a
374 default, or can edit the input.
377 @defun eval-minibuffer prompt &optional initial
378 This function reads a Lisp expression using the minibuffer, evaluates
379 it, then returns the result. The arguments @var{prompt} and
380 @var{initial} are used as in @code{read-from-minibuffer}.
382 This function simply evaluates the result of a call to
383 @code{read-minibuffer}:
387 (eval-minibuffer @var{prompt} @var{initial})
389 (eval (read-minibuffer @var{prompt} @var{initial}))
394 @defun edit-and-eval-command prompt form
395 This function reads a Lisp expression in the minibuffer, and then
396 evaluates it. The difference between this command and
397 @code{eval-minibuffer} is that here the initial @var{form} is not
398 optional and it is treated as a Lisp object to be converted to printed
399 representation rather than as a string of text. It is printed with
400 @code{prin1}, so if it is a string, double-quote characters (@samp{"})
401 appear in the initial text. @xref{Output Functions}.
403 The first thing @code{edit-and-eval-command} does is to activate the
404 minibuffer with @var{prompt} as the prompt. Then it inserts the printed
405 representation of @var{form} in the minibuffer, and lets the user edit it.
406 When the user exits the minibuffer, the edited text is read with
407 @code{read} and then evaluated. The resulting value becomes the value
408 of @code{edit-and-eval-command}.
410 In the following example, we offer the user an expression with initial
411 text which is a valid form already:
415 (edit-and-eval-command "Please edit: " '(forward-word 1))
417 ;; @r{After evaluation of the preceding expression,}
418 ;; @r{the following appears in the minibuffer:}
422 ---------- Buffer: Minibuffer ----------
423 Please edit: (forward-word 1)@point{}
424 ---------- Buffer: Minibuffer ----------
429 Typing @key{RET} right away would exit the minibuffer and evaluate the
430 expression, thus moving point forward one word.
431 @code{edit-and-eval-command} returns @code{nil} in this example.
434 @node Minibuffer History
435 @section Minibuffer History
436 @cindex minibuffer history
439 A @dfn{minibuffer history list} records previous minibuffer inputs so
440 the user can reuse them conveniently. A history list is actually a
441 symbol, not a list; it is a variable whose value is a list of strings
442 (previous inputs), most recent first.
444 There are many separate history lists, used for different kinds of
445 inputs. It's the Lisp programmer's job to specify the right history
446 list for each use of the minibuffer.
448 You specify the history list with the optional @var{hist} argument
449 to either @code{read-from-minibuffer} or @code{completing-read}. Here
450 are the possible values for it:
454 Use @var{variable} (a symbol) as the history list.
456 @item (@var{variable} . @var{startpos})
457 Use @var{variable} (a symbol) as the history list, and assume that the
458 initial history position is @var{startpos} (a nonnegative integer).
460 Specifying 0 for @var{startpos} is equivalent to just specifying the
461 symbol @var{variable}. @code{previous-history-element} will display
462 the most recent element of the history list in the minibuffer. If you
463 specify a positive @var{startpos}, the minibuffer history functions
464 behave as if @code{(elt @var{variable} (1- @var{STARTPOS}))} were the
465 history element currently shown in the minibuffer.
467 For consistency, you should also specify that element of the history
468 as the initial minibuffer contents, using the @var{initial} argument
469 to the minibuffer input function (@pxref{Initial Input}).
472 If you don't specify @var{hist}, then the default history list
473 @code{minibuffer-history} is used. For other standard history lists,
474 see below. You can also create your own history list variable; just
475 initialize it to @code{nil} before the first use.
477 Both @code{read-from-minibuffer} and @code{completing-read} add new
478 elements to the history list automatically, and provide commands to
479 allow the user to reuse items on the list. The only thing your program
480 needs to do to use a history list is to initialize it and to pass its
481 name to the input functions when you wish. But it is safe to modify the
482 list by hand when the minibuffer input functions are not using it.
484 Emacs functions that add a new element to a history list can also
485 delete old elements if the list gets too long. The variable
486 @code{history-length} specifies the maximum length for most history
487 lists. To specify a different maximum length for a particular history
488 list, put the length in the @code{history-length} property of the
489 history list symbol. The variable @code{history-delete-duplicates}
490 specifies whether to delete duplicates in history.
492 @defun add-to-history history-var newelt &optional maxelt keep-all
493 This function adds a new element @var{newelt}, if it isn't the empty
494 string, to the history list stored in the variable @var{history-var},
495 and returns the updated history list. It limits the list length to
496 the value of @var{maxelt} (if non-@code{nil}) or @code{history-length}
497 (described below). The possible values of @var{maxelt} have the same
498 meaning as the values of @code{history-length}.
500 Normally, @code{add-to-history} removes duplicate members from the
501 history list if @code{history-delete-duplicates} is non-@code{nil}.
502 However, if @var{keep-all} is non-@code{nil}, that says not to remove
503 duplicates, and to add @var{newelt} to the list even if it is empty.
506 @defvar history-add-new-input
507 If the value of this variable is @code{nil}, standard functions that
508 read from the minibuffer don't add new elements to the history list.
509 This lets Lisp programs explicitly manage input history by using
510 @code{add-to-history}. By default, @code{history-add-new-input} is
511 set to a non-@code{nil} value.
514 @defvar history-length
515 The value of this variable specifies the maximum length for all
516 history lists that don't specify their own maximum lengths. If the
517 value is @code{t}, that means there no maximum (don't delete old
518 elements). The value of @code{history-length} property of the history
519 list variable's symbol, if set, overrides this variable for that
520 particular history list.
523 @defvar history-delete-duplicates
524 If the value of this variable is @code{t}, that means when adding a
525 new history element, all previous identical elements are deleted.
528 Here are some of the standard minibuffer history list variables:
530 @defvar minibuffer-history
531 The default history list for minibuffer history input.
534 @defvar query-replace-history
535 A history list for arguments to @code{query-replace} (and similar
536 arguments to other commands).
539 @defvar file-name-history
540 A history list for file-name arguments.
543 @defvar buffer-name-history
544 A history list for buffer-name arguments.
547 @defvar regexp-history
548 A history list for regular expression arguments.
551 @defvar extended-command-history
552 A history list for arguments that are names of extended commands.
555 @defvar shell-command-history
556 A history list for arguments that are shell commands.
559 @defvar read-expression-history
560 A history list for arguments that are Lisp expressions to evaluate.
564 @section Initial Input
566 Several of the functions for minibuffer input have an argument called
567 @var{initial} or @var{initial-contents}. This is a mostly-deprecated
568 feature for specifying that the minibuffer should start out with
569 certain text, instead of empty as usual.
571 If @var{initial} is a string, the minibuffer starts out containing the
572 text of the string, with point at the end, when the user starts to
573 edit the text. If the user simply types @key{RET} to exit the
574 minibuffer, it will use the initial input string to determine the
577 @strong{We discourage use of a non-@code{nil} value for
578 @var{initial}}, because initial input is an intrusive interface.
579 History lists and default values provide a much more convenient method
580 to offer useful default inputs to the user.
582 There is just one situation where you should specify a string for an
583 @var{initial} argument. This is when you specify a cons cell for the
584 @var{hist} or @var{history} argument. @xref{Minibuffer History}.
586 @var{initial} can also be a cons cell of the form @code{(@var{string}
587 . @var{position})}. This means to insert @var{string} in the
588 minibuffer but put point at @var{position} within the string's text.
590 As a historical accident, @var{position} was implemented
591 inconsistently in different functions. In @code{completing-read},
592 @var{position}'s value is interpreted as origin-zero; that is, a value
593 of 0 means the beginning of the string, 1 means after the first
594 character, etc. In @code{read-minibuffer}, and the other
595 non-completion minibuffer input functions that support this argument,
596 1 means the beginning of the string 2 means after the first character,
599 Use of a cons cell as the value for @var{initial} arguments is
600 deprecated in user code.
606 @dfn{Completion} is a feature that fills in the rest of a name
607 starting from an abbreviation for it. Completion works by comparing the
608 user's input against a list of valid names and determining how much of
609 the name is determined uniquely by what the user has typed. For
610 example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
611 type the first few letters of the name of the buffer to which you wish
612 to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
613 extends the name as far as it can.
615 Standard Emacs commands offer completion for names of symbols, files,
616 buffers, and processes; with the functions in this section, you can
617 implement completion for other kinds of names.
619 The @code{try-completion} function is the basic primitive for
620 completion: it returns the longest determined completion of a given
621 initial string, with a given set of strings to match against.
623 The function @code{completing-read} provides a higher-level interface
624 for completion. A call to @code{completing-read} specifies how to
625 determine the list of valid names. The function then activates the
626 minibuffer with a local keymap that binds a few keys to commands useful
627 for completion. Other functions provide convenient simple interfaces
628 for reading certain kinds of names with completion.
631 * Basic Completion:: Low-level functions for completing strings.
632 (These are too low level to use the minibuffer.)
633 * Minibuffer Completion:: Invoking the minibuffer with completion.
634 * Completion Commands:: Minibuffer commands that do completion.
635 * High-Level Completion:: Convenient special cases of completion
636 (reading buffer name, file name, etc.)
637 * Reading File Names:: Using completion to read file names.
638 * Programmed Completion:: Writing your own completion-function.
641 @node Basic Completion
642 @subsection Basic Completion Functions
644 The completion functions @code{try-completion},
645 @code{all-completions} and @code{test-completion} have nothing in
646 themselves to do with minibuffers. We describe them in this chapter
647 so as to keep them near the higher-level completion features that do
650 If you store a completion alist in a variable, you should mark the
651 variable as ``risky'' with a non-@code{nil}
652 @code{risky-local-variable} property.
654 @defun try-completion string collection &optional predicate
655 This function returns the longest common substring of all possible
656 completions of @var{string} in @var{collection}. The value of
657 @var{collection} must be a list of strings or symbols, an alist, an
658 obarray, a hash table, or a function that implements a virtual set of
661 Completion compares @var{string} against each of the permissible
662 completions specified by @var{collection}; if the beginning of the
663 permissible completion equals @var{string}, it matches. If no permissible
664 completions match, @code{try-completion} returns @code{nil}. If only
665 one permissible completion matches, and the match is exact, then
666 @code{try-completion} returns @code{t}. Otherwise, the value is the
667 longest initial sequence common to all the permissible completions that
670 If @var{collection} is an alist (@pxref{Association Lists}), the
671 permissible completions are the elements of the alist that are either
672 strings, symbols, or conses whose @sc{car} is a string or symbol.
673 Symbols are converted to strings using @code{symbol-name}. Other
674 elements of the alist are ignored. (Remember that in Emacs Lisp, the
675 elements of alists do not @emph{have} to be conses.) In particular, a
676 list of strings or symbols is allowed, even though we usually do not
677 think of such lists as alists.
679 @cindex obarray in completion
680 If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
681 of all symbols in the obarray form the set of permissible completions. The
682 global variable @code{obarray} holds an obarray containing the names of
683 all interned Lisp symbols.
685 Note that the only valid way to make a new obarray is to create it
686 empty and then add symbols to it one by one using @code{intern}.
687 Also, you cannot intern a given symbol in more than one obarray.
689 If @var{collection} is a hash table, then the keys that are strings
690 are the possible completions. Other keys are ignored.
692 You can also use a symbol that is a function as @var{collection}. Then
693 the function is solely responsible for performing completion;
694 @code{try-completion} returns whatever this function returns. The
695 function is called with three arguments: @var{string}, @var{predicate}
696 and @code{nil}. (The reason for the third argument is so that the same
697 function can be used in @code{all-completions} and do the appropriate
698 thing in either case.) @xref{Programmed Completion}.
700 If the argument @var{predicate} is non-@code{nil}, then it must be a
701 function of one argument, unless @var{collection} is a hash table, in
702 which case it should be a function of two arguments. It is used to
703 test each possible match, and the match is accepted only if
704 @var{predicate} returns non-@code{nil}. The argument given to
705 @var{predicate} is either a string or a cons cell (the @sc{car} of
706 which is a string) from the alist, or a symbol (@emph{not} a symbol
707 name) from the obarray. If @var{collection} is a hash table,
708 @var{predicate} is called with two arguments, the string key and the
711 In addition, to be acceptable, a completion must also match all the
712 regular expressions in @code{completion-regexp-list}. (Unless
713 @var{collection} is a function, in which case that function has to
714 handle @code{completion-regexp-list} itself.)
716 In the first of the following examples, the string @samp{foo} is
717 matched by three of the alist @sc{car}s. All of the matches begin with
718 the characters @samp{fooba}, so that is the result. In the second
719 example, there is only one possible match, and it is exact, so the value
726 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
731 (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
736 In the following example, numerous symbols begin with the characters
737 @samp{forw}, and all of them begin with the word @samp{forward}. In
738 most of the symbols, this is followed with a @samp{-}, but not in all,
739 so no more than @samp{forward} can be completed.
743 (try-completion "forw" obarray)
748 Finally, in the following example, only two of the three possible
749 matches pass the predicate @code{test} (the string @samp{foobaz} is
750 too short). Both of those begin with the string @samp{foobar}.
755 (> (length (car s)) 6))
761 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
768 @defun all-completions string collection &optional predicate nospace
769 This function returns a list of all possible completions of
770 @var{string}. The arguments to this function (aside from
771 @var{nospace}) are the same as those of @code{try-completion}. Also,
772 this function uses @code{completion-regexp-list} in the same way that
773 @code{try-completion} does. The optional argument @var{nospace} only
774 matters if @var{string} is the empty string. In that case, if
775 @var{nospace} is non-@code{nil}, completions that start with a space
778 If @var{collection} is a function, it is called with three arguments:
779 @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
780 returns whatever the function returns. @xref{Programmed Completion}.
782 Here is an example, using the function @code{test} shown in the
783 example for @code{try-completion}:
788 (> (length (car s)) 6))
795 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
797 @result{} ("foobar1" "foobar2")
802 @defun test-completion string collection &optional predicate
803 @anchor{Definition of test-completion}
804 This function returns non-@code{nil} if @var{string} is a valid
805 completion possibility specified by @var{collection} and
806 @var{predicate}. The arguments are the same as in
807 @code{try-completion}. For instance, if @var{collection} is a list of
808 strings, this is true if @var{string} appears in the list and
809 @var{predicate} is satisfied.
811 This function uses @code{completion-regexp-list} in the same
812 way that @code{try-completion} does.
814 If @var{predicate} is non-@code{nil} and if @var{collection} contains
815 several strings that are equal to each other, as determined by
816 @code{compare-strings} according to @code{completion-ignore-case},
817 then @var{predicate} should accept either all or none of them.
818 Otherwise, the return value of @code{test-completion} is essentially
821 If @var{collection} is a function, it is called with three arguments,
822 the values @var{string}, @var{predicate} and @code{lambda}; whatever
823 it returns, @code{test-completion} returns in turn.
826 @defvar completion-ignore-case
827 If the value of this variable is non-@code{nil}, Emacs does not
828 consider case significant in completion.
831 @defvar completion-regexp-list
832 This is a list of regular expressions. The completion functions only
833 consider a completion acceptable if it matches all regular expressions
834 in this list, with @code{case-fold-search} (@pxref{Searching and Case})
835 bound to the value of @code{completion-ignore-case}.
838 @defmac lazy-completion-table var fun
839 This macro provides a way to initialize the variable @var{var} as a
840 collection for completion in a lazy way, not computing its actual
841 contents until they are first needed. You use this macro to produce a
842 value that you store in @var{var}. The actual computation of the
843 proper value is done the first time you do completion using @var{var}.
844 It is done by calling @var{fun} with no arguments. The
845 value @var{fun} returns becomes the permanent value of @var{var}.
847 Here is an example of use:
850 (defvar foo (lazy-completion-table foo make-my-alist))
854 @node Minibuffer Completion
855 @subsection Completion and the Minibuffer
856 @cindex minibuffer completion
857 @cindex reading from minibuffer with completion
859 This section describes the basic interface for reading from the
860 minibuffer with completion.
862 @defun completing-read prompt collection &optional predicate require-match initial hist default inherit-input-method
863 This function reads a string in the minibuffer, assisting the user by
864 providing completion. It activates the minibuffer with prompt
865 @var{prompt}, which must be a string.
867 The actual completion is done by passing @var{collection} and
868 @var{predicate} to the function @code{try-completion}. This happens
869 in certain commands bound in the local keymaps used for completion.
870 Some of these commands also call @code{test-completion}. Thus, if
871 @var{predicate} is non-@code{nil}, it should be compatible with
872 @var{collection} and @code{completion-ignore-case}. @xref{Definition
875 If @var{require-match} is @code{nil}, the exit commands work regardless
876 of the input in the minibuffer. If @var{require-match} is @code{t}, the
877 usual minibuffer exit commands won't exit unless the input completes to
878 an element of @var{collection}. If @var{require-match} is
879 @code{confirm-only}, the user can exit with any input, but she will
880 be asked for a confirmation if the input is not an element of
881 @var{collection}. Any other value of @var{require-match} behaves like
882 @code{t}, except that the exit commands won't exit if it does non-null
885 However, empty input is always permitted, regardless of the value of
886 @var{require-match}; in that case, @code{completing-read} returns the
887 first element of @var{default}, if it is a list; @code{""}, if
888 @var{default} is @code{nil}; or @var{default}. The string or strings
889 in @var{default} are also available to the user through the history
892 The function @code{completing-read} uses
893 @code{minibuffer-local-completion-map} as the keymap if
894 @var{require-match} is @code{nil}, and uses
895 @code{minibuffer-local-must-match-map} if @var{require-match} is
896 non-@code{nil}. @xref{Completion Commands}.
898 The argument @var{hist} specifies which history list variable to use for
899 saving the input and for minibuffer history commands. It defaults to
900 @code{minibuffer-history}. @xref{Minibuffer History}.
902 The argument @var{initial} is mostly deprecated; we recommend using a
903 non-@code{nil} value only in conjunction with specifying a cons cell
904 for @var{hist}. @xref{Initial Input}. For default input, use
905 @var{default} instead.
907 If the argument @var{inherit-input-method} is non-@code{nil}, then the
908 minibuffer inherits the current input method (@pxref{Input
909 Methods}) and the setting of @code{enable-multibyte-characters}
910 (@pxref{Text Representations}) from whichever buffer was current before
911 entering the minibuffer.
913 If the built-in variable @code{completion-ignore-case} is
914 non-@code{nil}, completion ignores case when comparing the input
915 against the possible matches. @xref{Basic Completion}. In this mode
916 of operation, @var{predicate} must also ignore case, or you will get
919 Here's an example of using @code{completing-read}:
925 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
930 ;; @r{After evaluation of the preceding expression,}
931 ;; @r{the following appears in the minibuffer:}
933 ---------- Buffer: Minibuffer ----------
934 Complete a foo: fo@point{}
935 ---------- Buffer: Minibuffer ----------
940 If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
941 @code{completing-read} returns @code{barfoo}.
943 The @code{completing-read} function binds variables to pass
944 information to the commands that actually do completion.
945 They are described in the following section.
948 @node Completion Commands
949 @subsection Minibuffer Commands that Do Completion
951 This section describes the keymaps, commands and user options used
952 in the minibuffer to do completion. The description refers to the
953 situation when Partial Completion mode is disabled (as it is by
954 default). When enabled, this minor mode uses its own alternatives to
955 some of the commands described below. @xref{Completion Options,,,
956 emacs, The GNU Emacs Manual}, for a short description of Partial
959 @defvar minibuffer-completion-table
960 The value of this variable is the collection used for completion in
961 the minibuffer. This is the global variable that contains what
962 @code{completing-read} passes to @code{try-completion}. It is used by
963 minibuffer completion commands such as @code{minibuffer-complete-word}.
966 @defvar minibuffer-completion-predicate
967 This variable's value is the predicate that @code{completing-read}
968 passes to @code{try-completion}. The variable is also used by the other
969 minibuffer completion functions.
972 @defvar minibuffer-completion-confirm
973 When the value of this variable is non-@code{nil}, Emacs asks for
974 confirmation of a completion before exiting the minibuffer.
975 @code{completing-read} binds this variable, and the function
976 @code{minibuffer-complete-and-exit} checks the value before exiting.
979 @deffn Command minibuffer-complete-word
980 This function completes the minibuffer contents by at most a single
981 word. Even if the minibuffer contents have only one completion,
982 @code{minibuffer-complete-word} does not add any characters beyond the
983 first character that is not a word constituent. @xref{Syntax Tables}.
986 @deffn Command minibuffer-complete
987 This function completes the minibuffer contents as far as possible.
990 @deffn Command minibuffer-complete-and-exit
991 This function completes the minibuffer contents, and exits if
992 confirmation is not required, i.e., if
993 @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
994 @emph{is} required, it is given by repeating this command
995 immediately---the command is programmed to work without confirmation
996 when run twice in succession.
999 @deffn Command minibuffer-completion-help
1000 This function creates a list of the possible completions of the
1001 current minibuffer contents. It works by calling @code{all-completions}
1002 using the value of the variable @code{minibuffer-completion-table} as
1003 the @var{collection} argument, and the value of
1004 @code{minibuffer-completion-predicate} as the @var{predicate} argument.
1005 The list of completions is displayed as text in a buffer named
1006 @samp{*Completions*}.
1009 @defun display-completion-list completions &optional common-substring
1010 This function displays @var{completions} to the stream in
1011 @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
1012 information about streams.) The argument @var{completions} is normally
1013 a list of completions just returned by @code{all-completions}, but it
1014 does not have to be. Each element may be a symbol or a string, either
1015 of which is simply printed. It can also be a list of two strings,
1016 which is printed as if the strings were concatenated. The first of
1017 the two strings is the actual completion, the second string serves as
1020 The argument @var{common-substring} is the prefix that is common to
1021 all the completions. With normal Emacs completion, it is usually the
1022 same as the string that was completed. @code{display-completion-list}
1023 uses this to highlight text in the completion list for better visual
1024 feedback. This is not needed in the minibuffer; for minibuffer
1025 completion, you can pass @code{nil}.
1027 This function is called by @code{minibuffer-completion-help}. The
1028 most common way to use it is together with
1029 @code{with-output-to-temp-buffer}, like this:
1032 (with-output-to-temp-buffer "*Completions*"
1033 (display-completion-list
1034 (all-completions (buffer-string) my-alist)
1039 @defopt completion-auto-help
1040 If this variable is non-@code{nil}, the completion commands
1041 automatically display a list of possible completions whenever nothing
1042 can be completed because the next character is not uniquely determined.
1045 @defvar minibuffer-local-completion-map
1046 @code{completing-read} uses this value as the local keymap when an
1047 exact match of one of the completions is not required. By default, this
1048 keymap makes the following bindings:
1052 @code{minibuffer-completion-help}
1055 @code{minibuffer-complete-word}
1058 @code{minibuffer-complete}
1062 with other characters bound as in @code{minibuffer-local-map}
1063 (@pxref{Definition of minibuffer-local-map}).
1066 @defvar minibuffer-local-must-match-map
1067 @code{completing-read} uses this value as the local keymap when an
1068 exact match of one of the completions is required. Therefore, no keys
1069 are bound to @code{exit-minibuffer}, the command that exits the
1070 minibuffer unconditionally. By default, this keymap makes the following
1075 @code{minibuffer-completion-help}
1078 @code{minibuffer-complete-word}
1081 @code{minibuffer-complete}
1084 @code{minibuffer-complete-and-exit}
1087 @code{minibuffer-complete-and-exit}
1091 with other characters bound as in @code{minibuffer-local-map}.
1094 @defvar minibuffer-local-filename-completion-map
1095 This is like @code{minibuffer-local-completion-map}
1096 except that it does not bind @key{SPC}. This keymap is used by the
1097 function @code{read-file-name}.
1100 @defvar minibuffer-local-filename-must-match-map
1101 This is like @code{minibuffer-local-must-match-map}
1102 except that it does not bind @key{SPC}. This keymap is used by the
1103 function @code{read-file-name}.
1106 @node High-Level Completion
1107 @subsection High-Level Completion Functions
1109 This section describes the higher-level convenient functions for
1110 reading certain sorts of names with completion.
1112 In most cases, you should not call these functions in the middle of a
1113 Lisp function. When possible, do all minibuffer input as part of
1114 reading the arguments for a command, in the @code{interactive}
1115 specification. @xref{Defining Commands}.
1117 @defun read-buffer prompt &optional default existing
1118 This function reads the name of a buffer and returns it as a string.
1119 The argument @var{default} is the default name to use, the value to
1120 return if the user exits with an empty minibuffer. If non-@code{nil},
1121 it should be a string, a list of strings, or a buffer. If it is
1122 a list, the default value is the first element of this list. It is
1123 mentioned in the prompt, but is not inserted in the minibuffer as
1126 The argument @var{prompt} should be a string ending with a colon and a
1127 space. If @var{default} is non-@code{nil}, the function inserts it in
1128 @var{prompt} before the colon to follow the convention for reading from
1129 the minibuffer with a default value (@pxref{Programming Tips}).
1131 If @var{existing} is non-@code{nil}, then the name specified must be
1132 that of an existing buffer. The usual commands to exit the minibuffer
1133 do not exit if the text is not valid, and @key{RET} does completion to
1134 attempt to find a valid name. If @var{existing} is neither @code{nil}
1135 nor @code{t}, confirmation is required after completion. (However,
1136 @var{default} is not checked for validity; it is returned, whatever it
1137 is, if the user exits with the minibuffer empty.)
1139 In the following example, the user enters @samp{minibuffer.t}, and
1140 then types @key{RET}. The argument @var{existing} is @code{t}, and the
1141 only buffer name starting with the given input is
1142 @samp{minibuffer.texi}, so that name is the value.
1145 (read-buffer "Buffer name: " "foo" t)
1147 ;; @r{After evaluation of the preceding expression,}
1148 ;; @r{the following prompt appears,}
1149 ;; @r{with an empty minibuffer:}
1153 ---------- Buffer: Minibuffer ----------
1154 Buffer name (default foo): @point{}
1155 ---------- Buffer: Minibuffer ----------
1159 ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
1160 @result{} "minibuffer.texi"
1165 @defvar read-buffer-function
1166 This variable specifies how to read buffer names. For example, if you
1167 set this variable to @code{iswitchb-read-buffer}, all Emacs commands
1168 that call @code{read-buffer} to read a buffer name will actually use the
1169 @code{iswitchb} package to read it.
1172 @defun read-command prompt &optional default
1173 This function reads the name of a command and returns it as a Lisp
1174 symbol. The argument @var{prompt} is used as in
1175 @code{read-from-minibuffer}. Recall that a command is anything for
1176 which @code{commandp} returns @code{t}, and a command name is a symbol
1177 for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
1179 The argument @var{default} specifies what to return if the user enters
1180 null input. It can be a symbol, a string or a list of strings. If it
1181 is a string, @code{read-command} interns it before returning it.
1182 If it is a list, @code{read-command} returns the first element of this list.
1183 If @var{default} is @code{nil}, that means no default has been
1184 specified; then if the user enters null input, the return value is
1185 @code{(intern "")}, that is, a symbol whose name is an empty string.
1188 (read-command "Command name? ")
1191 ;; @r{After evaluation of the preceding expression,}
1192 ;; @r{the following prompt appears with an empty minibuffer:}
1196 ---------- Buffer: Minibuffer ----------
1198 ---------- Buffer: Minibuffer ----------
1203 If the user types @kbd{forward-c @key{RET}}, then this function returns
1204 @code{forward-char}.
1206 The @code{read-command} function is a simplified interface to
1207 @code{completing-read}. It uses the variable @code{obarray} so as to
1208 complete in the set of extant Lisp symbols, and it uses the
1209 @code{commandp} predicate so as to accept only command names:
1211 @cindex @code{commandp} example
1214 (read-command @var{prompt})
1216 (intern (completing-read @var{prompt} obarray
1222 @defun read-variable prompt &optional default
1223 @anchor{Definition of read-variable}
1224 This function reads the name of a user variable and returns it as a
1227 The argument @var{default} specifies the default value to return if
1228 the user enters null input. It can be a symbol, a string, or a list
1229 of strings. If it is a string, @code{read-variable} interns it to
1230 make the default value. If it is a list, @code{read-variable} interns
1231 the first element. If @var{default} is @code{nil}, that means no
1232 default has been specified; then if the user enters null input, the
1233 return value is @code{(intern "")}.
1237 (read-variable "Variable name? ")
1239 ;; @r{After evaluation of the preceding expression,}
1240 ;; @r{the following prompt appears,}
1241 ;; @r{with an empty minibuffer:}
1245 ---------- Buffer: Minibuffer ----------
1246 Variable name? @point{}
1247 ---------- Buffer: Minibuffer ----------
1252 If the user then types @kbd{fill-p @key{RET}}, @code{read-variable}
1253 returns @code{fill-prefix}.
1255 In general, @code{read-variable} is similar to @code{read-command},
1256 but uses the predicate @code{user-variable-p} instead of
1259 @cindex @code{user-variable-p} example
1262 (read-variable @var{prompt})
1265 (completing-read @var{prompt} obarray
1266 'user-variable-p t nil))
1271 See also the functions @code{read-coding-system} and
1272 @code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems},
1273 and @code{read-input-method-name}, in @ref{Input Methods}.
1275 @node Reading File Names
1276 @subsection Reading File Names
1277 @cindex read file names
1278 @cindex prompt for file name
1280 Here is another high-level completion function, designed for reading a
1281 file name. It provides special features including automatic insertion
1282 of the default directory.
1284 @defun read-file-name prompt &optional directory default existing initial predicate
1285 This function reads a file name in the minibuffer, prompting with
1286 @var{prompt} and providing completion.
1288 If @var{existing} is non-@code{nil}, then the user must specify the name
1289 of an existing file; @key{RET} performs completion to make the name
1290 valid if possible, and then refuses to exit if it is not valid. If the
1291 value of @var{existing} is neither @code{nil} nor @code{t}, then
1292 @key{RET} also requires confirmation after completion. If
1293 @var{existing} is @code{nil}, then the name of a nonexistent file is
1296 @code{read-file-name} uses
1297 @code{minibuffer-local-filename-completion-map} as the keymap if
1298 @var{existing} is @code{nil}, and uses
1299 @code{minibuffer-local-filename-must-match-map} if @var{existing} is
1300 non-@code{nil}. @xref{Completion Commands}.
1302 The argument @var{directory} specifies the directory to use for
1303 completion of relative file names. It should be an absolute directory
1304 name. If @code{insert-default-directory} is non-@code{nil},
1305 @var{directory} is also inserted in the minibuffer as initial input.
1306 It defaults to the current buffer's value of @code{default-directory}.
1309 If you specify @var{initial}, that is an initial file name to insert
1310 in the buffer (after @var{directory}, if that is inserted). In this
1311 case, point goes at the beginning of @var{initial}. The default for
1312 @var{initial} is @code{nil}---don't insert any file name. To see what
1313 @var{initial} does, try the command @kbd{C-x C-v}. @strong{Please
1314 note:} we recommend using @var{default} rather than @var{initial} in
1317 If @var{default} is non-@code{nil}, then the function returns
1318 @var{default} if the user exits the minibuffer with the same non-empty
1319 contents that @code{read-file-name} inserted initially. The initial
1320 minibuffer contents are always non-empty if
1321 @code{insert-default-directory} is non-@code{nil}, as it is by
1322 default. @var{default} is not checked for validity, regardless of the
1323 value of @var{existing}. However, if @var{existing} is
1324 non-@code{nil}, the initial minibuffer contents should be a valid file
1325 (or directory) name. Otherwise @code{read-file-name} attempts
1326 completion if the user exits without any editing, and does not return
1327 @var{default}. @var{default} is also available through the history
1330 If @var{default} is @code{nil}, @code{read-file-name} tries to find a
1331 substitute default to use in its place, which it treats in exactly the
1332 same way as if it had been specified explicitly. If @var{default} is
1333 @code{nil}, but @var{initial} is non-@code{nil}, then the default is
1334 the absolute file name obtained from @var{directory} and
1335 @var{initial}. If both @var{default} and @var{initial} are @code{nil}
1336 and the buffer is visiting a file, @code{read-file-name} uses the
1337 absolute file name of that file as default. If the buffer is not
1338 visiting a file, then there is no default. In that case, if the user
1339 types @key{RET} without any editing, @code{read-file-name} simply
1340 returns the pre-inserted contents of the minibuffer.
1342 If the user types @key{RET} in an empty minibuffer, this function
1343 returns an empty string, regardless of the value of @var{existing}.
1344 This is, for instance, how the user can make the current buffer visit
1345 no file using @code{M-x set-visited-file-name}.
1347 If @var{predicate} is non-@code{nil}, it specifies a function of one
1348 argument that decides which file names are acceptable completion
1349 possibilities. A file name is an acceptable value if @var{predicate}
1350 returns non-@code{nil} for it.
1352 @code{read-file-name} does not automatically expand file names. You
1353 must call @code{expand-file-name} yourself if an absolute file name is
1360 (read-file-name "The file is ")
1362 ;; @r{After evaluation of the preceding expression,}
1363 ;; @r{the following appears in the minibuffer:}
1367 ---------- Buffer: Minibuffer ----------
1368 The file is /gp/gnu/elisp/@point{}
1369 ---------- Buffer: Minibuffer ----------
1374 Typing @kbd{manual @key{TAB}} results in the following:
1378 ---------- Buffer: Minibuffer ----------
1379 The file is /gp/gnu/elisp/manual.texi@point{}
1380 ---------- Buffer: Minibuffer ----------
1384 @c Wordy to avoid overfull hbox in smallbook mode.
1386 If the user types @key{RET}, @code{read-file-name} returns the file name
1387 as the string @code{"/gp/gnu/elisp/manual.texi"}.
1390 @defvar read-file-name-function
1391 If non-@code{nil}, this should be a function that accepts the same
1392 arguments as @code{read-file-name}. When @code{read-file-name} is
1393 called, it calls this function with the supplied arguments instead of
1394 doing its usual work.
1397 @defvar read-file-name-completion-ignore-case
1398 If this variable is non-@code{nil}, @code{read-file-name} ignores case
1399 when performing completion.
1402 @defun read-directory-name prompt &optional directory default existing initial
1403 This function is like @code{read-file-name} but allows only directory
1404 names as completion possibilities.
1406 If @var{default} is @code{nil} and @var{initial} is non-@code{nil},
1407 @code{read-directory-name} constructs a substitute default by
1408 combining @var{directory} (or the current buffer's default directory
1409 if @var{directory} is @code{nil}) and @var{initial}. If both
1410 @var{default} and @var{initial} are @code{nil}, this function uses
1411 @var{directory} as substitute default, or the current buffer's default
1412 directory if @var{directory} is @code{nil}.
1415 @defopt insert-default-directory
1416 This variable is used by @code{read-file-name}, and thus, indirectly,
1417 by most commands reading file names. (This includes all commands that
1418 use the code letters @samp{f} or @samp{F} in their interactive form.
1419 @xref{Interactive Codes,, Code Characters for interactive}.) Its
1420 value controls whether @code{read-file-name} starts by placing the
1421 name of the default directory in the minibuffer, plus the initial file
1422 name if any. If the value of this variable is @code{nil}, then
1423 @code{read-file-name} does not place any initial input in the
1424 minibuffer (unless you specify initial input with the @var{initial}
1425 argument). In that case, the default directory is still used for
1426 completion of relative file names, but is not displayed.
1428 If this variable is @code{nil} and the initial minibuffer contents are
1429 empty, the user may have to explicitly fetch the next history element
1430 to access a default value. If the variable is non-@code{nil}, the
1431 initial minibuffer contents are always non-empty and the user can
1432 always request a default value by immediately typing @key{RET} in an
1433 unedited minibuffer. (See above.)
1439 ;; @r{Here the minibuffer starts out with the default directory.}
1440 (let ((insert-default-directory t))
1441 (read-file-name "The file is "))
1445 ---------- Buffer: Minibuffer ----------
1446 The file is ~lewis/manual/@point{}
1447 ---------- Buffer: Minibuffer ----------
1451 ;; @r{Here the minibuffer is empty and only the prompt}
1452 ;; @r{appears on its line.}
1453 (let ((insert-default-directory nil))
1454 (read-file-name "The file is "))
1458 ---------- Buffer: Minibuffer ----------
1459 The file is @point{}
1460 ---------- Buffer: Minibuffer ----------
1465 @node Programmed Completion
1466 @subsection Programmed Completion
1467 @cindex programmed completion
1469 Sometimes it is not possible to create an alist or an obarray
1470 containing all the intended possible completions. In such a case, you
1471 can supply your own function to compute the completion of a given string.
1472 This is called @dfn{programmed completion}.
1474 To use this feature, pass a symbol with a function definition as the
1475 @var{collection} argument to @code{completing-read}. The function
1476 @code{completing-read} arranges to pass your completion function along
1477 to @code{try-completion} and @code{all-completions}, which will then let
1478 your function do all the work.
1480 The completion function should accept three arguments:
1484 The string to be completed.
1487 The predicate function to filter possible matches, or @code{nil} if
1488 none. Your function should call the predicate for each possible match,
1489 and ignore the possible match if the predicate returns @code{nil}.
1492 A flag specifying the type of operation.
1495 There are three flag values for three operations:
1499 @code{nil} specifies @code{try-completion}. The completion function
1500 should return the completion of the specified string, or @code{t} if the
1501 string is a unique and exact match already, or @code{nil} if the string
1502 matches no possibility.
1504 If the string is an exact match for one possibility, but also matches
1505 other longer possibilities, the function should return the string, not
1509 @code{t} specifies @code{all-completions}. The completion function
1510 should return a list of all possible completions of the specified
1514 @code{lambda} specifies @code{test-completion}. The completion
1515 function should return @code{t} if the specified string is an exact
1516 match for some possibility; @code{nil} otherwise.
1519 It would be consistent and clean for completion functions to allow
1520 lambda expressions (lists that are functions) as well as function
1521 symbols as @var{collection}, but this is impossible. Lists as
1522 completion tables already have other meanings, and it would be
1523 unreliable to treat one differently just because it is also a possible
1524 function. So you must arrange for any function you wish to use for
1525 completion to be encapsulated in a symbol.
1527 Emacs uses programmed completion when completing file names.
1528 @xref{File Name Completion}.
1530 @defun completion-table-dynamic function
1531 This function is a convenient way to write a function that can act as
1532 programmed completion function. The argument @var{function} should be
1533 a function that takes one argument, a string, and returns an alist of
1534 possible completions of it. You can think of
1535 @code{completion-table-dynamic} as a transducer between that interface
1536 and the interface for programmed completion functions.
1539 @node Yes-or-No Queries
1540 @section Yes-or-No Queries
1541 @cindex asking the user questions
1542 @cindex querying the user
1543 @cindex yes-or-no questions
1545 This section describes functions used to ask the user a yes-or-no
1546 question. The function @code{y-or-n-p} can be answered with a single
1547 character; it is useful for questions where an inadvertent wrong answer
1548 will not have serious consequences. @code{yes-or-no-p} is suitable for
1549 more momentous questions, since it requires three or four characters to
1552 If either of these functions is called in a command that was invoked
1553 using the mouse---more precisely, if @code{last-nonmenu-event}
1554 (@pxref{Command Loop Info}) is either @code{nil} or a list---then it
1555 uses a dialog box or pop-up menu to ask the question. Otherwise, it
1556 uses keyboard input. You can force use of the mouse or use of keyboard
1557 input by binding @code{last-nonmenu-event} to a suitable value around
1560 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
1561 @code{y-or-n-p} does not; but it seems best to describe them together.
1563 @defun y-or-n-p prompt
1564 This function asks the user a question, expecting input in the echo
1565 area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
1566 user types @kbd{n}. This function also accepts @key{SPC} to mean yes
1567 and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit,'' like
1568 @kbd{C-g}, because the question might look like a minibuffer and for
1569 that reason the user might try to use @kbd{C-]} to get out. The answer
1570 is a single character, with no @key{RET} needed to terminate it. Upper
1571 and lower case are equivalent.
1573 ``Asking the question'' means printing @var{prompt} in the echo area,
1574 followed by the string @w{@samp{(y or n) }}. If the input is not one of
1575 the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
1576 @kbd{@key{DEL}}, or something that quits), the function responds
1577 @samp{Please answer y or n.}, and repeats the request.
1579 This function does not actually use the minibuffer, since it does not
1580 allow editing of the answer. It actually uses the echo area (@pxref{The
1581 Echo Area}), which uses the same screen space as the minibuffer. The
1582 cursor moves to the echo area while the question is being asked.
1584 The answers and their meanings, even @samp{y} and @samp{n}, are not
1585 hardwired. The keymap @code{query-replace-map} specifies them.
1586 @xref{Search and Replace}.
1588 In the following example, the user first types @kbd{q}, which is
1589 invalid. At the next prompt the user types @kbd{y}.
1593 (y-or-n-p "Do you need a lift? ")
1595 ;; @r{After evaluation of the preceding expression,}
1596 ;; @r{the following prompt appears in the echo area:}
1600 ---------- Echo area ----------
1601 Do you need a lift? (y or n)
1602 ---------- Echo area ----------
1605 ;; @r{If the user then types @kbd{q}, the following appears:}
1608 ---------- Echo area ----------
1609 Please answer y or n. Do you need a lift? (y or n)
1610 ---------- Echo area ----------
1613 ;; @r{When the user types a valid answer,}
1614 ;; @r{it is displayed after the question:}
1617 ---------- Echo area ----------
1618 Do you need a lift? (y or n) y
1619 ---------- Echo area ----------
1624 We show successive lines of echo area messages, but only one actually
1625 appears on the screen at a time.
1628 @defun y-or-n-p-with-timeout prompt seconds default-value
1629 Like @code{y-or-n-p}, except that if the user fails to answer within
1630 @var{seconds} seconds, this function stops waiting and returns
1631 @var{default-value}. It works by setting up a timer; see @ref{Timers}.
1632 The argument @var{seconds} may be an integer or a floating point number.
1635 @defun yes-or-no-p prompt
1636 This function asks the user a question, expecting input in the
1637 minibuffer. It returns @code{t} if the user enters @samp{yes},
1638 @code{nil} if the user types @samp{no}. The user must type @key{RET} to
1639 finalize the response. Upper and lower case are equivalent.
1641 @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
1642 followed by @w{@samp{(yes or no) }}. The user must type one of the
1643 expected responses; otherwise, the function responds @samp{Please answer
1644 yes or no.}, waits about two seconds and repeats the request.
1646 @code{yes-or-no-p} requires more work from the user than
1647 @code{y-or-n-p} and is appropriate for more crucial decisions.
1653 (yes-or-no-p "Do you really want to remove everything? ")
1655 ;; @r{After evaluation of the preceding expression,}
1656 ;; @r{the following prompt appears,}
1657 ;; @r{with an empty minibuffer:}
1661 ---------- Buffer: minibuffer ----------
1662 Do you really want to remove everything? (yes or no)
1663 ---------- Buffer: minibuffer ----------
1668 If the user first types @kbd{y @key{RET}}, which is invalid because this
1669 function demands the entire word @samp{yes}, it responds by displaying
1670 these prompts, with a brief pause between them:
1674 ---------- Buffer: minibuffer ----------
1675 Please answer yes or no.
1676 Do you really want to remove everything? (yes or no)
1677 ---------- Buffer: minibuffer ----------
1682 @node Multiple Queries
1683 @section Asking Multiple Y-or-N Questions
1685 When you have a series of similar questions to ask, such as ``Do you
1686 want to save this buffer'' for each buffer in turn, you should use
1687 @code{map-y-or-n-p} to ask the collection of questions, rather than
1688 asking each question individually. This gives the user certain
1689 convenient facilities such as the ability to answer the whole series at
1692 @defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
1693 This function asks the user a series of questions, reading a
1694 single-character answer in the echo area for each one.
1696 The value of @var{list} specifies the objects to ask questions about.
1697 It should be either a list of objects or a generator function. If it is
1698 a function, it should expect no arguments, and should return either the
1699 next object to ask about, or @code{nil} meaning stop asking questions.
1701 The argument @var{prompter} specifies how to ask each question. If
1702 @var{prompter} is a string, the question text is computed like this:
1705 (format @var{prompter} @var{object})
1709 where @var{object} is the next object to ask about (as obtained from
1712 If not a string, @var{prompter} should be a function of one argument
1713 (the next object to ask about) and should return the question text. If
1714 the value is a string, that is the question to ask the user. The
1715 function can also return @code{t} meaning do act on this object (and
1716 don't ask the user), or @code{nil} meaning ignore this object (and don't
1719 The argument @var{actor} says how to act on the answers that the user
1720 gives. It should be a function of one argument, and it is called with
1721 each object that the user says yes for. Its argument is always an
1722 object obtained from @var{list}.
1724 If the argument @var{help} is given, it should be a list of this form:
1727 (@var{singular} @var{plural} @var{action})
1731 where @var{singular} is a string containing a singular noun that
1732 describes the objects conceptually being acted on, @var{plural} is the
1733 corresponding plural noun, and @var{action} is a transitive verb
1734 describing what @var{actor} does.
1736 If you don't specify @var{help}, the default is @code{("object"
1737 "objects" "act on")}.
1739 Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
1740 @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
1741 that object; @kbd{!} to act on all following objects; @key{ESC} or
1742 @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
1743 the current object and then exit; or @kbd{C-h} to get help. These are
1744 the same answers that @code{query-replace} accepts. The keymap
1745 @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
1746 as well as for @code{query-replace}; see @ref{Search and Replace}.
1748 You can use @var{action-alist} to specify additional possible answers
1749 and what they mean. It is an alist of elements of the form
1750 @code{(@var{char} @var{function} @var{help})}, each of which defines one
1751 additional answer. In this element, @var{char} is a character (the
1752 answer); @var{function} is a function of one argument (an object from
1753 @var{list}); @var{help} is a string.
1755 When the user responds with @var{char}, @code{map-y-or-n-p} calls
1756 @var{function}. If it returns non-@code{nil}, the object is considered
1757 ``acted upon,'' and @code{map-y-or-n-p} advances to the next object in
1758 @var{list}. If it returns @code{nil}, the prompt is repeated for the
1761 Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
1762 prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
1765 If @code{map-y-or-n-p} is called in a command that was invoked using the
1766 mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
1767 Loop Info}) is either @code{nil} or a list---then it uses a dialog box
1768 or pop-up menu to ask the question. In this case, it does not use
1769 keyboard input or the echo area. You can force use of the mouse or use
1770 of keyboard input by binding @code{last-nonmenu-event} to a suitable
1771 value around the call.
1773 The return value of @code{map-y-or-n-p} is the number of objects acted on.
1776 @node Reading a Password
1777 @section Reading a Password
1778 @cindex passwords, reading
1780 To read a password to pass to another program, you can use the
1781 function @code{read-passwd}.
1783 @defun read-passwd prompt &optional confirm default
1784 This function reads a password, prompting with @var{prompt}. It does
1785 not echo the password as the user types it; instead, it echoes @samp{.}
1786 for each character in the password.
1788 The optional argument @var{confirm}, if non-@code{nil}, says to read the
1789 password twice and insist it must be the same both times. If it isn't
1790 the same, the user has to type it over and over until the last two
1793 The optional argument @var{default} specifies the default password to
1794 return if the user enters empty input. If @var{default} is @code{nil},
1795 then @code{read-passwd} returns the null string in that case.
1798 @node Minibuffer Commands
1799 @section Minibuffer Commands
1801 This section describes some commands meant for use in the
1804 @deffn Command exit-minibuffer
1805 This command exits the active minibuffer. It is normally bound to
1806 keys in minibuffer local keymaps.
1809 @deffn Command self-insert-and-exit
1810 This command exits the active minibuffer after inserting the last
1811 character typed on the keyboard (found in @code{last-command-char};
1812 @pxref{Command Loop Info}).
1815 @deffn Command previous-history-element n
1816 This command replaces the minibuffer contents with the value of the
1817 @var{n}th previous (older) history element.
1820 @deffn Command next-history-element n
1821 This command replaces the minibuffer contents with the value of the
1822 @var{n}th more recent history element.
1825 @deffn Command previous-matching-history-element pattern n
1826 This command replaces the minibuffer contents with the value of the
1827 @var{n}th previous (older) history element that matches @var{pattern} (a
1828 regular expression).
1831 @deffn Command next-matching-history-element pattern n
1832 This command replaces the minibuffer contents with the value of the
1833 @var{n}th next (newer) history element that matches @var{pattern} (a
1834 regular expression).
1837 @node Minibuffer Windows
1838 @section Minibuffer Windows
1839 @cindex minibuffer windows
1841 These functions access and select minibuffer windows
1842 and test whether they are active.
1844 @defun active-minibuffer-window
1845 This function returns the currently active minibuffer window, or
1846 @code{nil} if none is currently active.
1849 @defun minibuffer-window &optional frame
1850 @anchor{Definition of minibuffer-window}
1851 This function returns the minibuffer window used for frame @var{frame}.
1852 If @var{frame} is @code{nil}, that stands for the current frame. Note
1853 that the minibuffer window used by a frame need not be part of that
1854 frame---a frame that has no minibuffer of its own necessarily uses some
1855 other frame's minibuffer window.
1858 @defun set-minibuffer-window window
1859 This function specifies @var{window} as the minibuffer window to use.
1860 This affects where the minibuffer is displayed if you put text in it
1861 without invoking the usual minibuffer commands. It has no effect on
1862 the usual minibuffer input functions because they all start by
1863 choosing the minibuffer window according to the current frame.
1867 @defun window-minibuffer-p &optional window
1868 This function returns non-@code{nil} if @var{window} is a minibuffer
1870 @var{window} defaults to the selected window.
1873 It is not correct to determine whether a given window is a minibuffer by
1874 comparing it with the result of @code{(minibuffer-window)}, because
1875 there can be more than one minibuffer window if there is more than one
1878 @defun minibuffer-window-active-p window
1879 This function returns non-@code{nil} if @var{window}, assumed to be
1880 a minibuffer window, is currently active.
1883 @node Minibuffer Contents
1884 @section Minibuffer Contents
1886 These functions access the minibuffer prompt and contents.
1888 @defun minibuffer-prompt
1889 This function returns the prompt string of the currently active
1890 minibuffer. If no minibuffer is active, it returns @code{nil}.
1893 @defun minibuffer-prompt-end
1894 This function returns the current
1895 position of the end of the minibuffer prompt, if a minibuffer is
1896 current. Otherwise, it returns the minimum valid buffer position.
1899 @defun minibuffer-prompt-width
1900 This function returns the current display-width of the minibuffer
1901 prompt, if a minibuffer is current. Otherwise, it returns zero.
1904 @defun minibuffer-contents
1905 This function returns the editable
1906 contents of the minibuffer (that is, everything except the prompt) as
1907 a string, if a minibuffer is current. Otherwise, it returns the
1908 entire contents of the current buffer.
1911 @defun minibuffer-contents-no-properties
1912 This is like @code{minibuffer-contents}, except that it does not copy text
1913 properties, just the characters themselves. @xref{Text Properties}.
1916 @defun minibuffer-completion-contents
1917 This is like @code{minibuffer-contents}, except that it returns only
1918 the contents before point. That is the part that completion commands
1919 operate on. @xref{Minibuffer Completion}.
1922 @defun delete-minibuffer-contents
1923 This function erases the editable contents of the minibuffer (that is,
1924 everything except the prompt), if a minibuffer is current. Otherwise,
1925 it erases the entire current buffer.
1928 @node Recursive Mini
1929 @section Recursive Minibuffers
1930 @cindex recursive minibuffers
1932 These functions and variables deal with recursive minibuffers
1933 (@pxref{Recursive Editing}):
1935 @defun minibuffer-depth
1936 This function returns the current depth of activations of the
1937 minibuffer, a nonnegative integer. If no minibuffers are active, it
1941 @defopt enable-recursive-minibuffers
1942 If this variable is non-@code{nil}, you can invoke commands (such as
1943 @code{find-file}) that use minibuffers even while the minibuffer window
1944 is active. Such invocation produces a recursive editing level for a new
1945 minibuffer. The outer-level minibuffer is invisible while you are
1946 editing the inner one.
1948 If this variable is @code{nil}, you cannot invoke minibuffer
1949 commands when the minibuffer window is active, not even if you switch to
1950 another window to do it.
1954 If a command name has a property @code{enable-recursive-minibuffers}
1955 that is non-@code{nil}, then the command can use the minibuffer to read
1956 arguments even if it is invoked from the minibuffer. A command can
1957 also achieve this by binding @code{enable-recursive-minibuffers}
1958 to @code{t} in the interactive declaration (@pxref{Using Interactive}).
1959 The minibuffer command @code{next-matching-history-element} (normally
1960 @kbd{M-s} in the minibuffer) does the latter.
1962 @node Minibuffer Misc
1963 @section Minibuffer Miscellany
1965 @defun minibufferp &optional buffer-or-name
1966 This function returns non-@code{nil} if @var{buffer-or-name} is a
1967 minibuffer. If @var{buffer-or-name} is omitted, it tests the current
1971 @defvar minibuffer-setup-hook
1972 This is a normal hook that is run whenever the minibuffer is entered.
1976 @defvar minibuffer-exit-hook
1977 This is a normal hook that is run whenever the minibuffer is exited.
1981 @defvar minibuffer-help-form
1982 @anchor{Definition of minibuffer-help-form}
1983 The current value of this variable is used to rebind @code{help-form}
1984 locally inside the minibuffer (@pxref{Help Functions}).
1987 @defvar minibuffer-scroll-window
1988 @anchor{Definition of minibuffer-scroll-window}
1989 If the value of this variable is non-@code{nil}, it should be a window
1990 object. When the function @code{scroll-other-window} is called in the
1991 minibuffer, it scrolls this window.
1994 @defun minibuffer-selected-window
1995 This function returns the window which was selected when the
1996 minibuffer was entered. If selected window is not a minibuffer
1997 window, it returns @code{nil}.
2000 @defopt max-mini-window-height
2001 This variable specifies the maximum height for resizing minibuffer
2002 windows. If a float, it specifies a fraction of the height of the
2003 frame. If an integer, it specifies a number of lines.
2006 @defun minibuffer-message string
2007 This function displays @var{string} temporarily at the end of the
2008 minibuffer text, for two seconds, or until the next input event
2009 arrives, whichever comes first.
2013 arch-tag: bba7f945-9078-477f-a2ce-18818a6e1218