2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software
5 @c See the file elisp.texi for copying conditions.
8 @cindex arguments, reading
9 @cindex complex arguments
12 A @dfn{minibuffer} is a special buffer that Emacs commands use to
13 read arguments more complicated than the single numeric prefix
14 argument. These arguments include file names, buffer names, and
15 command names (as in @kbd{M-x}). The minibuffer is displayed on the
16 bottom line of the frame, in the same place as the echo area
17 (@pxref{The Echo Area}), but only while it is in use for reading an
21 * Intro to Minibuffers:: Basic information about minibuffers.
22 * Text from Minibuffer:: How to read a straight text string.
23 * Object from Minibuffer:: How to read a Lisp object or expression.
24 * Minibuffer History:: Recording previous minibuffer inputs
25 so the user can reuse them.
26 * Initial Input:: Specifying initial contents for the minibuffer.
27 * Completion:: How to invoke and customize completion.
28 * Yes-or-No Queries:: Asking a question with a simple answer.
29 * Multiple Queries:: Asking a series of similar questions.
30 * Reading a Password:: Reading a password from the terminal.
31 * Minibuffer Commands:: Commands used as key bindings in minibuffers.
32 * Minibuffer Windows:: Operating on the special minibuffer windows.
33 * Minibuffer Contents:: How such commands access the minibuffer text.
34 * Recursive Mini:: Whether recursive entry to minibuffer is allowed.
35 * Minibuffer Misc:: Various customization hooks and variables.
38 @node Intro to Minibuffers
39 @section Introduction to Minibuffers
41 In most ways, a minibuffer is a normal Emacs buffer. Most operations
42 @emph{within} a buffer, such as editing commands, work normally in a
43 minibuffer. However, many operations for managing buffers do not apply
44 to minibuffers. The name of a minibuffer always has the form @w{@samp{
45 *Minibuf-@var{number}*}}, and it cannot be changed. Minibuffers are
46 displayed only in special windows used only for minibuffers; these
47 windows always appear at the bottom of a frame. (Sometimes frames have
48 no minibuffer window, and sometimes a special kind of frame contains
49 nothing but a minibuffer window; see @ref{Minibuffers and Frames}.)
51 The text in the minibuffer always starts with the @dfn{prompt string},
52 the text that was specified by the program that is using the minibuffer
53 to tell the user what sort of input to type. This text is marked
54 read-only so you won't accidentally delete or change it. It is also
55 marked as a field (@pxref{Fields}), so that certain motion functions,
56 including @code{beginning-of-line}, @code{forward-word},
57 @code{forward-sentence}, and @code{forward-paragraph}, stop at the
58 boundary between the prompt and the actual text.
60 @c See http://debbugs.gnu.org/11276
61 The minibuffer's window is normally a single line; it grows
62 automatically if the contents require more space. Whilst it is
63 active, you can explicitly resize it temporarily with the window
64 sizing commands; it reverts to its normal size when the minibuffer is
65 exited. When the minibuffer is not active, you can resize it
66 permanently by using the window sizing commands in the frame's other
67 window, or dragging the mode line with the mouse. (Due to details of
68 the current implementation, for this to work @code{resize-mini-windows}
69 must be @code{nil}.) If the frame contains just a minibuffer, you can
70 change the minibuffer's size by changing the frame's size.
72 Use of the minibuffer reads input events, and that alters the values
73 of variables such as @code{this-command} and @code{last-command}
74 (@pxref{Command Loop Info}). Your program should bind them around the
75 code that uses the minibuffer, if you do not want that to change them.
77 Under some circumstances, a command can use a minibuffer even if
78 there is an active minibuffer; such a minibuffer is called a
79 @dfn{recursive minibuffer}. The first minibuffer is named
80 @w{@samp{ *Minibuf-1*}}. Recursive minibuffers are named by
81 incrementing the number at the end of the name. (The names begin with
82 a space so that they won't show up in normal buffer lists.) Of
83 several recursive minibuffers, the innermost (or most recently
84 entered) is the active minibuffer. We usually call this ``the''
85 minibuffer. You can permit or forbid recursive minibuffers by setting
86 the variable @code{enable-recursive-minibuffers}, or by putting
87 properties of that name on command symbols (@xref{Recursive Mini}.)
89 Like other buffers, a minibuffer uses a local keymap
90 (@pxref{Keymaps}) to specify special key bindings. The function that
91 invokes the minibuffer also sets up its local map according to the job
92 to be done. @xref{Text from Minibuffer}, for the non-completion
93 minibuffer local maps. @xref{Completion Commands}, for the minibuffer
94 local maps for completion.
96 @cindex inactive minibuffer
97 When a minibuffer is inactive, its major mode is
98 @code{minibuffer-inactive-mode}, with keymap
99 @code{minibuffer-inactive-mode-map}. This is only really useful if
100 the minibuffer is in a separate frame. @xref{Minibuffers and Frames}.
102 When Emacs is running in batch mode, any request to read from the
103 minibuffer actually reads a line from the standard input descriptor that
104 was supplied when Emacs was started.
106 @node Text from Minibuffer
107 @section Reading Text Strings with the Minibuffer
109 The most basic primitive for minibuffer input is
110 @code{read-from-minibuffer}, which can be used to read either a string
111 or a Lisp object in textual form. The function @code{read-regexp} is
112 used for reading regular expressions (@pxref{Regular Expressions}),
113 which are a special kind of string. There are also specialized
114 functions for reading commands, variables, file names, etc.@:
115 (@pxref{Completion}).
117 In most cases, you should not call minibuffer input functions in the
118 middle of a Lisp function. Instead, do all minibuffer input as part of
119 reading the arguments for a command, in the @code{interactive}
120 specification. @xref{Defining Commands}.
122 @defun read-from-minibuffer prompt &optional initial keymap read history default inherit-input-method
123 This function is the most general way to get input from the
124 minibuffer. By default, it accepts arbitrary text and returns it as a
125 string; however, if @var{read} is non-@code{nil}, then it uses
126 @code{read} to convert the text into a Lisp object (@pxref{Input
129 The first thing this function does is to activate a minibuffer and
130 display it with @var{prompt} (which must be a string) as the
131 prompt. Then the user can edit text in the minibuffer.
133 When the user types a command to exit the minibuffer,
134 @code{read-from-minibuffer} constructs the return value from the text in
135 the minibuffer. Normally it returns a string containing that text.
136 However, if @var{read} is non-@code{nil}, @code{read-from-minibuffer}
137 reads the text and returns the resulting Lisp object, unevaluated.
138 (@xref{Input Functions}, for information about reading.)
140 The argument @var{default} specifies default values to make available
141 through the history commands. It should be a string, a list of
142 strings, or @code{nil}. The string or strings become the minibuffer's
143 ``future history'', available to the user with @kbd{M-n}.
145 If @var{read} is non-@code{nil}, then @var{default} is also used
146 as the input to @code{read}, if the user enters empty input.
147 If @var{default} is a list of strings, the first string is used as the input.
148 If @var{default} is @code{nil}, empty input results in an @code{end-of-file} error.
149 However, in the usual case (where @var{read} is @code{nil}),
150 @code{read-from-minibuffer} ignores @var{default} when the user enters
151 empty input and returns an empty string, @code{""}. In this respect,
152 it differs from all the other minibuffer input functions in this chapter.
154 If @var{keymap} is non-@code{nil}, that keymap is the local keymap to
155 use in the minibuffer. If @var{keymap} is omitted or @code{nil}, the
156 value of @code{minibuffer-local-map} is used as the keymap. Specifying
157 a keymap is the most important way to customize the minibuffer for
158 various applications such as completion.
160 The argument @var{history} specifies a history list variable to use
161 for saving the input and for history commands used in the minibuffer.
162 It defaults to @code{minibuffer-history}. You can optionally specify
163 a starting position in the history list as well. @xref{Minibuffer History}.
165 If the variable @code{minibuffer-allow-text-properties} is
166 non-@code{nil}, then the string that is returned includes whatever text
167 properties were present in the minibuffer. Otherwise all the text
168 properties are stripped when the value is returned.
170 If the argument @var{inherit-input-method} is non-@code{nil}, then the
171 minibuffer inherits the current input method (@pxref{Input Methods}) and
172 the setting of @code{enable-multibyte-characters} (@pxref{Text
173 Representations}) from whichever buffer was current before entering the
176 Use of @var{initial} is mostly deprecated; we recommend using
177 a non-@code{nil} value only in conjunction with specifying a cons cell
178 for @var{history}. @xref{Initial Input}.
181 @defun read-string prompt &optional initial history default inherit-input-method
182 This function reads a string from the minibuffer and returns it. The
183 arguments @var{prompt}, @var{initial}, @var{history} and
184 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
185 The keymap used is @code{minibuffer-local-map}.
187 The optional argument @var{default} is used as in
188 @code{read-from-minibuffer}, except that, if non-@code{nil}, it also
189 specifies a default value to return if the user enters null input. As
190 in @code{read-from-minibuffer} it should be a string, a list of
191 strings, or @code{nil}, which is equivalent to an empty string. When
192 @var{default} is a string, that string is the default value. When it
193 is a list of strings, the first string is the default value. (All
194 these strings are available to the user in the ``future minibuffer
197 This function works by calling the
198 @code{read-from-minibuffer} function:
202 (read-string @var{prompt} @var{initial} @var{history} @var{default} @var{inherit})
205 (read-from-minibuffer @var{prompt} @var{initial} nil nil
206 @var{history} @var{default} @var{inherit})))
207 (if (and (equal value "") @var{default})
208 (if (consp @var{default}) (car @var{default}) @var{default})
214 @defun read-regexp prompt &optional defaults history
215 This function reads a regular expression as a string from the
216 minibuffer and returns it. If the minibuffer prompt string
217 @var{prompt} does not end in @samp{:} (followed by optional
218 whitespace), the function adds @samp{: } to the end, preceded by the
219 default return value (see below), if that is non-empty.
221 The optional argument @var{defaults} controls the default value to
222 return if the user enters null input, and should be one of: a string;
223 @code{nil}, which is equivalent to an empty string; a list of strings;
226 If @var{defaults} is a symbol, @code{read-regexp} consults the value
227 of the variable @code{read-regexp-defaults-function} (see below), and
228 if that is non-@code{nil} uses it in preference to @var{defaults}.
229 The value in this case should be either:
233 @code{regexp-history-last}, which means to use the first element of
234 the appropriate minibuffer history list (see below).
237 A function of no arguments, whose return value (which should be
238 @code{nil}, a string, or a list of strings) becomes the value of
242 @code{read-regexp} now ensures that the result of processing
243 @var{defaults} is a list (i.e., if the value is @code{nil} or a
244 string, it converts it to a list of one element). To this list,
245 @code{read-regexp} then appends a few potentially useful candidates for
250 The word or symbol at point.
252 The last regexp used in an incremental search.
254 The last string used in an incremental search.
256 The last string or pattern used in query-replace commands.
259 The function now has a list of regular expressions that it passes to
260 @code{read-from-minibuffer} to obtain the user's input. The first
261 element of the list is the default result in case of empty input. All
262 elements of the list are available to the user as the ``future
263 minibuffer history list'' (@pxref{Minibuffer History, future list,,
264 emacs, The GNU Emacs Manual}).
266 The optional argument @var{history}, if non-@code{nil}, is a symbol
267 specifying a minibuffer history list to use (@pxref{Minibuffer
268 History}). If it is omitted or @code{nil}, the history list defaults
269 to @code{regexp-history}.
272 @defvar read-regexp-defaults-function
273 The function @code{read-regexp} may use the value of this variable to
274 determine its list of default regular expressions. If non-@code{nil},
275 the value of this variable should be either:
279 The symbol @code{regexp-history-last}.
282 A function of no arguments that returns either @code{nil}, a string,
283 or a list of strings.
287 See @code{read-regexp} above for details of how these values are used.
290 @defvar minibuffer-allow-text-properties
291 If this variable is @code{nil}, then @code{read-from-minibuffer}
292 and @code{read-string} strip all text properties from the minibuffer
293 input before returning it. However,
294 @code{read-no-blanks-input} (see below), as well as
295 @code{read-minibuffer} and related functions (@pxref{Object from
296 Minibuffer,, Reading Lisp Objects With the Minibuffer}), and all
297 functions that do minibuffer input with completion, discard text
298 properties unconditionally, regardless of the value of this variable.
301 @defvar minibuffer-local-map
303 @anchor{Definition of minibuffer-local-map}
304 @c avoid page break at anchor; work around Texinfo deficiency
305 is the default local keymap for reading from the minibuffer. By
306 default, it makes the following bindings:
310 @code{exit-minibuffer}
313 @code{exit-minibuffer}
316 @code{abort-recursive-edit}
320 @code{next-history-element}
324 @code{previous-history-element}
327 @code{next-matching-history-element}
330 @code{previous-matching-history-element}
333 @c Does not seem worth/appropriate mentioning.
334 @item @kbd{C-@key{TAB}}
335 @code{file-cache-minibuffer-complete}
340 @c In version 18, initial is required
342 @defun read-no-blanks-input prompt &optional initial inherit-input-method
343 This function reads a string from the minibuffer, but does not allow
344 whitespace characters as part of the input: instead, those characters
345 terminate the input. The arguments @var{prompt}, @var{initial}, and
346 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
348 This is a simplified interface to the @code{read-from-minibuffer}
349 function, and passes the value of the @code{minibuffer-local-ns-map}
350 keymap as the @var{keymap} argument for that function. Since the keymap
351 @code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
352 possible to put a space into the string, by quoting it.
354 This function discards text properties, regardless of the value of
355 @code{minibuffer-allow-text-properties}.
359 (read-no-blanks-input @var{prompt} @var{initial})
361 (let (minibuffer-allow-text-properties)
362 (read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map))
367 @c Slightly unfortunate name, suggesting it might be related to the
369 @defvar minibuffer-local-ns-map
370 This built-in variable is the keymap used as the minibuffer local keymap
371 in the function @code{read-no-blanks-input}. By default, it makes the
372 following bindings, in addition to those of @code{minibuffer-local-map}:
376 @cindex @key{SPC} in minibuffer
377 @code{exit-minibuffer}
380 @cindex @key{TAB} in minibuffer
381 @code{exit-minibuffer}
384 @cindex @kbd{?} in minibuffer
385 @code{self-insert-and-exit}
389 @node Object from Minibuffer
390 @section Reading Lisp Objects with the Minibuffer
392 This section describes functions for reading Lisp objects with the
395 @defun read-minibuffer prompt &optional initial
396 This function reads a Lisp object using the minibuffer, and returns it
397 without evaluating it. The arguments @var{prompt} and @var{initial} are
398 used as in @code{read-from-minibuffer}.
400 This is a simplified interface to the
401 @code{read-from-minibuffer} function:
405 (read-minibuffer @var{prompt} @var{initial})
407 (let (minibuffer-allow-text-properties)
408 (read-from-minibuffer @var{prompt} @var{initial} nil t))
412 Here is an example in which we supply the string @code{"(testing)"} as
418 "Enter an expression: " (format "%s" '(testing)))
420 ;; @r{Here is how the minibuffer is displayed:}
424 ---------- Buffer: Minibuffer ----------
425 Enter an expression: (testing)@point{}
426 ---------- Buffer: Minibuffer ----------
431 The user can type @key{RET} immediately to use the initial input as a
432 default, or can edit the input.
435 @defun eval-minibuffer prompt &optional initial
436 This function reads a Lisp expression using the minibuffer, evaluates
437 it, then returns the result. The arguments @var{prompt} and
438 @var{initial} are used as in @code{read-from-minibuffer}.
440 This function simply evaluates the result of a call to
441 @code{read-minibuffer}:
445 (eval-minibuffer @var{prompt} @var{initial})
447 (eval (read-minibuffer @var{prompt} @var{initial}))
452 @defun edit-and-eval-command prompt form
453 This function reads a Lisp expression in the minibuffer, evaluates it,
454 then returns the result. The difference between this command and
455 @code{eval-minibuffer} is that here the initial @var{form} is not
456 optional and it is treated as a Lisp object to be converted to printed
457 representation rather than as a string of text. It is printed with
458 @code{prin1}, so if it is a string, double-quote characters (@samp{"})
459 appear in the initial text. @xref{Output Functions}.
461 In the following example, we offer the user an expression with initial
462 text that is already a valid form:
466 (edit-and-eval-command "Please edit: " '(forward-word 1))
468 ;; @r{After evaluation of the preceding expression,}
469 ;; @r{the following appears in the minibuffer:}
473 ---------- Buffer: Minibuffer ----------
474 Please edit: (forward-word 1)@point{}
475 ---------- Buffer: Minibuffer ----------
480 Typing @key{RET} right away would exit the minibuffer and evaluate the
481 expression, thus moving point forward one word.
484 @node Minibuffer History
485 @section Minibuffer History
486 @cindex minibuffer history
489 A @dfn{minibuffer history list} records previous minibuffer inputs
490 so the user can reuse them conveniently. It is a variable whose value
491 is a list of strings (previous inputs), most recent first.
493 There are many separate minibuffer history lists, used for different
494 kinds of inputs. It's the Lisp programmer's job to specify the right
495 history list for each use of the minibuffer.
497 You specify a minibuffer history list with the optional @var{history}
498 argument to @code{read-from-minibuffer} or @code{completing-read}.
499 Here are the possible values for it:
503 Use @var{variable} (a symbol) as the history list.
505 @item (@var{variable} . @var{startpos})
506 Use @var{variable} (a symbol) as the history list, and assume that the
507 initial history position is @var{startpos} (a nonnegative integer).
509 Specifying 0 for @var{startpos} is equivalent to just specifying the
510 symbol @var{variable}. @code{previous-history-element} will display
511 the most recent element of the history list in the minibuffer. If you
512 specify a positive @var{startpos}, the minibuffer history functions
513 behave as if @code{(elt @var{variable} (1- @var{startpos}))} were the
514 history element currently shown in the minibuffer.
516 For consistency, you should also specify that element of the history
517 as the initial minibuffer contents, using the @var{initial} argument
518 to the minibuffer input function (@pxref{Initial Input}).
521 If you don't specify @var{history}, then the default history list
522 @code{minibuffer-history} is used. For other standard history lists,
523 see below. You can also create your own history list variable; just
524 initialize it to @code{nil} before the first use.
526 Both @code{read-from-minibuffer} and @code{completing-read} add new
527 elements to the history list automatically, and provide commands to
528 allow the user to reuse items on the list. The only thing your program
529 needs to do to use a history list is to initialize it and to pass its
530 name to the input functions when you wish. But it is safe to modify the
531 list by hand when the minibuffer input functions are not using it.
533 Emacs functions that add a new element to a history list can also
534 delete old elements if the list gets too long. The variable
535 @code{history-length} specifies the maximum length for most history
536 lists. To specify a different maximum length for a particular history
537 list, put the length in the @code{history-length} property of the
538 history list symbol. The variable @code{history-delete-duplicates}
539 specifies whether to delete duplicates in history.
541 @defun add-to-history history-var newelt &optional maxelt keep-all
542 This function adds a new element @var{newelt}, if it isn't the empty
543 string, to the history list stored in the variable @var{history-var},
544 and returns the updated history list. It limits the list length to
545 the value of @var{maxelt} (if non-@code{nil}) or @code{history-length}
546 (described below). The possible values of @var{maxelt} have the same
547 meaning as the values of @code{history-length}.
549 Normally, @code{add-to-history} removes duplicate members from the
550 history list if @code{history-delete-duplicates} is non-@code{nil}.
551 However, if @var{keep-all} is non-@code{nil}, that says not to remove
552 duplicates, and to add @var{newelt} to the list even if it is empty.
555 @defvar history-add-new-input
556 If the value of this variable is @code{nil}, standard functions that
557 read from the minibuffer don't add new elements to the history list.
558 This lets Lisp programs explicitly manage input history by using
559 @code{add-to-history}. The default value is @code{t}.
562 @defopt history-length
563 The value of this variable specifies the maximum length for all
564 history lists that don't specify their own maximum lengths. If the
565 value is @code{t}, that means there is no maximum (don't delete old
566 elements). If a history list variable's symbol has a non-@code{nil}
567 @code{history-length} property, it overrides this variable for that
568 particular history list.
571 @defopt history-delete-duplicates
572 If the value of this variable is @code{t}, that means when adding a
573 new history element, all previous identical elements are deleted.
576 Here are some of the standard minibuffer history list variables:
578 @defvar minibuffer-history
579 The default history list for minibuffer history input.
582 @defvar query-replace-history
583 A history list for arguments to @code{query-replace} (and similar
584 arguments to other commands).
587 @defvar file-name-history
588 A history list for file-name arguments.
591 @defvar buffer-name-history
592 A history list for buffer-name arguments.
595 @defvar regexp-history
596 A history list for regular expression arguments.
599 @defvar extended-command-history
600 A history list for arguments that are names of extended commands.
603 @defvar shell-command-history
604 A history list for arguments that are shell commands.
607 @defvar read-expression-history
608 A history list for arguments that are Lisp expressions to evaluate.
611 @defvar face-name-history
612 A history list for arguments that are faces.
615 @c Less common: coding-system-history, input-method-history,
616 @c command-history, grep-history, grep-find-history,
617 @c read-envvar-name-history, setenv-history, yes-or-no-p-history.
620 @section Initial Input
622 Several of the functions for minibuffer input have an argument called
623 @var{initial}. This is a mostly-deprecated
624 feature for specifying that the minibuffer should start out with
625 certain text, instead of empty as usual.
627 If @var{initial} is a string, the minibuffer starts out containing the
628 text of the string, with point at the end, when the user starts to
629 edit the text. If the user simply types @key{RET} to exit the
630 minibuffer, it will use the initial input string to determine the
633 @strong{We discourage use of a non-@code{nil} value for
634 @var{initial}}, because initial input is an intrusive interface.
635 History lists and default values provide a much more convenient method
636 to offer useful default inputs to the user.
638 There is just one situation where you should specify a string for an
639 @var{initial} argument. This is when you specify a cons cell for the
640 @var{history} argument. @xref{Minibuffer History}.
642 @var{initial} can also be a cons cell of the form @code{(@var{string}
643 . @var{position})}. This means to insert @var{string} in the
644 minibuffer but put point at @var{position} within the string's text.
646 As a historical accident, @var{position} was implemented
647 inconsistently in different functions. In @code{completing-read},
648 @var{position}'s value is interpreted as origin-zero; that is, a value
649 of 0 means the beginning of the string, 1 means after the first
650 character, etc. In @code{read-minibuffer}, and the other
651 non-completion minibuffer input functions that support this argument,
652 1 means the beginning of the string, 2 means after the first character,
655 Use of a cons cell as the value for @var{initial} arguments is deprecated.
661 @dfn{Completion} is a feature that fills in the rest of a name
662 starting from an abbreviation for it. Completion works by comparing the
663 user's input against a list of valid names and determining how much of
664 the name is determined uniquely by what the user has typed. For
665 example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
666 @c "This is the sort of English up with which I will not put."
667 type the first few letters of the name of the buffer to which you wish
668 to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
669 extends the name as far as it can.
671 Standard Emacs commands offer completion for names of symbols, files,
672 buffers, and processes; with the functions in this section, you can
673 implement completion for other kinds of names.
675 The @code{try-completion} function is the basic primitive for
676 completion: it returns the longest determined completion of a given
677 initial string, with a given set of strings to match against.
679 The function @code{completing-read} provides a higher-level interface
680 for completion. A call to @code{completing-read} specifies how to
681 determine the list of valid names. The function then activates the
682 minibuffer with a local keymap that binds a few keys to commands useful
683 for completion. Other functions provide convenient simple interfaces
684 for reading certain kinds of names with completion.
687 * Basic Completion:: Low-level functions for completing strings.
688 * Minibuffer Completion:: Invoking the minibuffer with completion.
689 * Completion Commands:: Minibuffer commands that do completion.
690 * High-Level Completion:: Convenient special cases of completion
691 (reading buffer names, variable names, etc.).
692 * Reading File Names:: Using completion to read file names and
694 * Completion Variables:: Variables controlling completion behavior.
695 * Programmed Completion:: Writing your own completion function.
696 * Completion in Buffers:: Completing text in ordinary buffers.
699 @node Basic Completion
700 @subsection Basic Completion Functions
702 The following completion functions have nothing in themselves to do
703 with minibuffers. We describe them here to keep them near the
704 higher-level completion features that do use the minibuffer.
706 @defun try-completion string collection &optional predicate
707 This function returns the longest common substring of all possible
708 completions of @var{string} in @var{collection}.
710 @cindex completion table
711 @var{collection} is called the @dfn{completion table}. Its value must
712 be a list of strings or cons cells, an obarray, a hash table, or a
715 @code{try-completion} compares @var{string} against each of the
716 permissible completions specified by the completion table. If no
717 permissible completions match, it returns @code{nil}. If there is
718 just one matching completion, and the match is exact, it returns
719 @code{t}. Otherwise, it returns the longest initial sequence common
720 to all possible matching completions.
722 If @var{collection} is an list, the permissible completions are
723 specified by the elements of the list, each of which should be either
724 a string, or a cons cell whose @sc{car} is either a string or a symbol
725 (a symbol is converted to a string using @code{symbol-name}). If the
726 list contains elements of any other type, those are ignored.
728 @cindex obarray in completion
729 If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
730 of all symbols in the obarray form the set of permissible completions.
732 If @var{collection} is a hash table, then the keys that are strings
733 are the possible completions. Other keys are ignored.
735 You can also use a function as @var{collection}. Then the function is
736 solely responsible for performing completion; @code{try-completion}
737 returns whatever this function returns. The function is called with
738 three arguments: @var{string}, @var{predicate} and @code{nil} (the
739 third argument is so that the same function can be used
740 in @code{all-completions} and do the appropriate thing in either
741 case). @xref{Programmed Completion}.
743 If the argument @var{predicate} is non-@code{nil}, then it must be a
744 function of one argument, unless @var{collection} is a hash table, in
745 which case it should be a function of two arguments. It is used to
746 test each possible match, and the match is accepted only if
747 @var{predicate} returns non-@code{nil}. The argument given to
748 @var{predicate} is either a string or a cons cell (the @sc{car} of
749 which is a string) from the alist, or a symbol (@emph{not} a symbol
750 name) from the obarray. If @var{collection} is a hash table,
751 @var{predicate} is called with two arguments, the string key and the
754 In addition, to be acceptable, a completion must also match all the
755 regular expressions in @code{completion-regexp-list}. (Unless
756 @var{collection} is a function, in which case that function has to
757 handle @code{completion-regexp-list} itself.)
759 In the first of the following examples, the string @samp{foo} is
760 matched by three of the alist @sc{car}s. All of the matches begin with
761 the characters @samp{fooba}, so that is the result. In the second
762 example, there is only one possible match, and it is exact, so the
763 return value is @code{t}.
769 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
774 (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
779 In the following example, numerous symbols begin with the characters
780 @samp{forw}, and all of them begin with the word @samp{forward}. In
781 most of the symbols, this is followed with a @samp{-}, but not in all,
782 so no more than @samp{forward} can be completed.
786 (try-completion "forw" obarray)
791 Finally, in the following example, only two of the three possible
792 matches pass the predicate @code{test} (the string @samp{foobaz} is
793 too short). Both of those begin with the string @samp{foobar}.
798 (> (length (car s)) 6))
804 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
811 @c Removed obsolete argument nospace.
812 @defun all-completions string collection &optional predicate
813 This function returns a list of all possible completions of
814 @var{string}. The arguments to this function
815 @c (aside from @var{nospace})
816 are the same as those of @code{try-completion}, and it
817 uses @code{completion-regexp-list} in the same way that
818 @code{try-completion} does.
821 The optional argument @var{nospace} is obsolete. If it is
822 non-@code{nil}, completions that start with a space are ignored unless
823 @var{string} starts with a space.
826 If @var{collection} is a function, it is called with three arguments:
827 @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
828 returns whatever the function returns. @xref{Programmed Completion}.
830 Here is an example, using the function @code{test} shown in the
831 example for @code{try-completion}:
836 (> (length (car s)) 6))
843 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
845 @result{} ("foobar1" "foobar2")
850 @defun test-completion string collection &optional predicate
851 @anchor{Definition of test-completion}
852 This function returns non-@code{nil} if @var{string} is a valid
853 completion alternative specified by @var{collection} and
854 @var{predicate}. The arguments are the same as in
855 @code{try-completion}. For instance, if @var{collection} is a list of
856 strings, this is true if @var{string} appears in the list and
857 @var{predicate} is satisfied.
859 This function uses @code{completion-regexp-list} in the same
860 way that @code{try-completion} does.
862 If @var{predicate} is non-@code{nil} and if @var{collection} contains
863 several strings that are equal to each other, as determined by
864 @code{compare-strings} according to @code{completion-ignore-case},
865 then @var{predicate} should accept either all or none of them.
866 Otherwise, the return value of @code{test-completion} is essentially
869 If @var{collection} is a function, it is called with three arguments,
870 the values @var{string}, @var{predicate} and @code{lambda}; whatever
871 it returns, @code{test-completion} returns in turn.
874 @defun completion-boundaries string collection predicate suffix
875 This function returns the boundaries of the field on which @var{collection}
876 will operate, assuming that @var{string} holds the text before point
877 and @var{suffix} holds the text after point.
879 Normally completion operates on the whole string, so for all normal
880 collections, this will always return @code{(0 . (length
881 @var{suffix}))}. But more complex completion such as completion on
882 files is done one field at a time. For example, completion of
883 @code{"/usr/sh"} will include @code{"/usr/share/"} but not
884 @code{"/usr/share/doc"} even if @code{"/usr/share/doc"} exists.
885 Also @code{all-completions} on @code{"/usr/sh"} will not include
886 @code{"/usr/share/"} but only @code{"share/"}. So if @var{string} is
887 @code{"/usr/sh"} and @var{suffix} is @code{"e/doc"},
888 @code{completion-boundaries} will return @code{(5 . 1)} which tells us
889 that the @var{collection} will only return completion information that
890 pertains to the area after @code{"/usr/"} and before @code{"/doc"}.
893 If you store a completion alist in a variable, you should mark the
894 variable as ``risky'' by giving it a non-@code{nil}
895 @code{risky-local-variable} property. @xref{File Local Variables}.
897 @defvar completion-ignore-case
898 If the value of this variable is non-@code{nil}, case is not
899 considered significant in completion. Within @code{read-file-name},
900 this variable is overridden by
901 @code{read-file-name-completion-ignore-case} (@pxref{Reading File
902 Names}); within @code{read-buffer}, it is overridden by
903 @code{read-buffer-completion-ignore-case} (@pxref{High-Level
907 @defvar completion-regexp-list
908 This is a list of regular expressions. The completion functions only
909 consider a completion acceptable if it matches all regular expressions
910 in this list, with @code{case-fold-search} (@pxref{Searching and Case})
911 bound to the value of @code{completion-ignore-case}.
914 @defmac lazy-completion-table var fun
915 This macro provides a way to initialize the variable @var{var} as a
916 collection for completion in a lazy way, not computing its actual
917 contents until they are first needed. You use this macro to produce a
918 value that you store in @var{var}. The actual computation of the
919 proper value is done the first time you do completion using @var{var}.
920 It is done by calling @var{fun} with no arguments. The
921 value @var{fun} returns becomes the permanent value of @var{var}.
926 (defvar foo (lazy-completion-table foo make-my-alist))
930 @c FIXME? completion-table-with-context?
931 @findex completion-table-case-fold
932 @findex completion-table-in-turn
933 @findex completion-table-merge
934 @findex completion-table-subvert
935 @findex completion-table-with-quoting
936 @findex completion-table-with-predicate
937 @findex completion-table-with-terminator
938 @cindex completion table, modifying
939 @cindex completion tables, combining
940 There are several functions that take an existing completion table and
941 return a modified version. @code{completion-table-case-fold} returns
942 a case-insensitive table. @code{completion-table-in-turn} and
943 @code{completion-table-merge} combine multiple input tables in
944 different ways. @code{completion-table-subvert} alters a table to use
945 a different initial prefix. @code{completion-table-with-quoting}
946 returns a table suitable for operating on quoted text.
947 @code{completion-table-with-predicate} filters a table with a
948 predicate function. @code{completion-table-with-terminator} adds a
952 @node Minibuffer Completion
953 @subsection Completion and the Minibuffer
954 @cindex minibuffer completion
955 @cindex reading from minibuffer with completion
957 This section describes the basic interface for reading from the
958 minibuffer with completion.
960 @defun completing-read prompt collection &optional predicate require-match initial history default inherit-input-method
961 This function reads a string in the minibuffer, assisting the user by
962 providing completion. It activates the minibuffer with prompt
963 @var{prompt}, which must be a string.
965 The actual completion is done by passing the completion table
966 @var{collection} and the completion predicate @var{predicate} to the
967 function @code{try-completion} (@pxref{Basic Completion}). This
968 happens in certain commands bound in the local keymaps used for
969 completion. Some of these commands also call @code{test-completion}.
970 Thus, if @var{predicate} is non-@code{nil}, it should be compatible
971 with @var{collection} and @code{completion-ignore-case}.
972 @xref{Definition of test-completion}.
974 The value of the optional argument @var{require-match} determines how
975 the user may exit the minibuffer:
979 If @code{nil}, the usual minibuffer exit commands work regardless of
980 the input in the minibuffer.
983 If @code{t}, the usual minibuffer exit commands won't exit unless the
984 input completes to an element of @var{collection}.
987 If @code{confirm}, the user can exit with any input, but is asked for
988 confirmation if the input is not an element of @var{collection}.
991 If @code{confirm-after-completion}, the user can exit with any input,
992 but is asked for confirmation if the preceding command was a
993 completion command (i.e., one of the commands in
994 @code{minibuffer-confirm-exit-commands}) and the resulting input is
995 not an element of @var{collection}. @xref{Completion Commands}.
998 Any other value of @var{require-match} behaves like @code{t}, except
999 that the exit commands won't exit if it performs completion.
1002 However, empty input is always permitted, regardless of the value of
1003 @var{require-match}; in that case, @code{completing-read} returns the
1004 first element of @var{default}, if it is a list; @code{""}, if
1005 @var{default} is @code{nil}; or @var{default}. The string or strings
1006 in @var{default} are also available to the user through the history
1009 The function @code{completing-read} uses
1010 @code{minibuffer-local-completion-map} as the keymap if
1011 @var{require-match} is @code{nil}, and uses
1012 @code{minibuffer-local-must-match-map} if @var{require-match} is
1013 non-@code{nil}. @xref{Completion Commands}.
1015 The argument @var{history} specifies which history list variable to use for
1016 saving the input and for minibuffer history commands. It defaults to
1017 @code{minibuffer-history}. @xref{Minibuffer History}.
1019 The argument @var{initial} is mostly deprecated; we recommend using a
1020 non-@code{nil} value only in conjunction with specifying a cons cell
1021 for @var{history}. @xref{Initial Input}. For default input, use
1022 @var{default} instead.
1024 If the argument @var{inherit-input-method} is non-@code{nil}, then the
1025 minibuffer inherits the current input method (@pxref{Input
1026 Methods}) and the setting of @code{enable-multibyte-characters}
1027 (@pxref{Text Representations}) from whichever buffer was current before
1028 entering the minibuffer.
1030 If the variable @code{completion-ignore-case} is
1031 non-@code{nil}, completion ignores case when comparing the input
1032 against the possible matches. @xref{Basic Completion}. In this mode
1033 of operation, @var{predicate} must also ignore case, or you will get
1036 Here's an example of using @code{completing-read}:
1042 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
1047 ;; @r{After evaluation of the preceding expression,}
1048 ;; @r{the following appears in the minibuffer:}
1050 ---------- Buffer: Minibuffer ----------
1051 Complete a foo: fo@point{}
1052 ---------- Buffer: Minibuffer ----------
1057 If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
1058 @code{completing-read} returns @code{barfoo}.
1060 The @code{completing-read} function binds variables to pass
1061 information to the commands that actually do completion.
1062 They are described in the following section.
1065 @defvar completing-read-function
1066 The value of this variable must be a function, which is called by
1067 @code{completing-read} to actually do its work. It should accept the
1068 same arguments as @code{completing-read}. This can be bound to a
1069 different function to completely override the normal behavior of
1070 @code{completing-read}.
1073 @node Completion Commands
1074 @subsection Minibuffer Commands that Do Completion
1076 This section describes the keymaps, commands and user options used
1077 in the minibuffer to do completion.
1079 @defvar minibuffer-completion-table
1080 The value of this variable is the completion table used for completion
1081 in the minibuffer. This is the global variable that contains what
1082 @code{completing-read} passes to @code{try-completion}. It is used by
1083 minibuffer completion commands such as
1084 @code{minibuffer-complete-word}.
1087 @defvar minibuffer-completion-predicate
1088 This variable's value is the predicate that @code{completing-read}
1089 passes to @code{try-completion}. The variable is also used by the other
1090 minibuffer completion functions.
1093 @defvar minibuffer-completion-confirm
1094 This variable determines whether Emacs asks for confirmation before
1095 exiting the minibuffer; @code{completing-read} binds this variable,
1096 and the function @code{minibuffer-complete-and-exit} checks the value
1097 before exiting. If the value is @code{nil}, confirmation is not
1098 required. If the value is @code{confirm}, the user may exit with an
1099 input that is not a valid completion alternative, but Emacs asks for
1100 confirmation. If the value is @code{confirm-after-completion}, the
1101 user may exit with an input that is not a valid completion
1102 alternative, but Emacs asks for confirmation if the user submitted the
1103 input right after any of the completion commands in
1104 @code{minibuffer-confirm-exit-commands}.
1107 @defvar minibuffer-confirm-exit-commands
1108 This variable holds a list of commands that cause Emacs to ask for
1109 confirmation before exiting the minibuffer, if the @var{require-match}
1110 argument to @code{completing-read} is @code{confirm-after-completion}.
1111 The confirmation is requested if the user attempts to exit the
1112 minibuffer immediately after calling any command in this list.
1115 @deffn Command minibuffer-complete-word
1116 This function completes the minibuffer contents by at most a single
1117 word. Even if the minibuffer contents have only one completion,
1118 @code{minibuffer-complete-word} does not add any characters beyond the
1119 first character that is not a word constituent. @xref{Syntax Tables}.
1122 @deffn Command minibuffer-complete
1123 This function completes the minibuffer contents as far as possible.
1126 @deffn Command minibuffer-complete-and-exit
1127 This function completes the minibuffer contents, and exits if
1128 confirmation is not required, i.e., if
1129 @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
1130 @emph{is} required, it is given by repeating this command
1131 immediately---the command is programmed to work without confirmation
1132 when run twice in succession.
1135 @deffn Command minibuffer-completion-help
1136 This function creates a list of the possible completions of the
1137 current minibuffer contents. It works by calling @code{all-completions}
1138 using the value of the variable @code{minibuffer-completion-table} as
1139 the @var{collection} argument, and the value of
1140 @code{minibuffer-completion-predicate} as the @var{predicate} argument.
1141 The list of completions is displayed as text in a buffer named
1142 @file{*Completions*}.
1145 @defun display-completion-list completions
1146 This function displays @var{completions} to the stream in
1147 @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
1148 information about streams.) The argument @var{completions} is normally
1149 a list of completions just returned by @code{all-completions}, but it
1150 does not have to be. Each element may be a symbol or a string, either
1151 of which is simply printed. It can also be a list of two strings,
1152 which is printed as if the strings were concatenated. The first of
1153 the two strings is the actual completion, the second string serves as
1156 This function is called by @code{minibuffer-completion-help}. A
1157 common way to use it is together with
1158 @code{with-output-to-temp-buffer}, like this:
1161 (with-output-to-temp-buffer "*Completions*"
1162 (display-completion-list
1163 (all-completions (buffer-string) my-alist)))
1167 @defopt completion-auto-help
1168 If this variable is non-@code{nil}, the completion commands
1169 automatically display a list of possible completions whenever nothing
1170 can be completed because the next character is not uniquely determined.
1173 @defvar minibuffer-local-completion-map
1174 @code{completing-read} uses this value as the local keymap when an
1175 exact match of one of the completions is not required. By default, this
1176 keymap makes the following bindings:
1180 @code{minibuffer-completion-help}
1183 @code{minibuffer-complete-word}
1186 @code{minibuffer-complete}
1190 and uses @code{minibuffer-local-map} as its parent keymap
1191 (@pxref{Definition of minibuffer-local-map}).
1194 @defvar minibuffer-local-must-match-map
1195 @code{completing-read} uses this value as the local keymap when an
1196 exact match of one of the completions is required. Therefore, no keys
1197 are bound to @code{exit-minibuffer}, the command that exits the
1198 minibuffer unconditionally. By default, this keymap makes the following
1203 @code{minibuffer-complete-and-exit}
1206 @code{minibuffer-complete-and-exit}
1210 and uses @code{minibuffer-local-completion-map} as its parent keymap.
1213 @defvar minibuffer-local-filename-completion-map
1214 This is a sparse keymap that simply unbinds @key{SPC}; because
1215 filenames can contain spaces. The function @code{read-file-name}
1216 combines this keymap with either @code{minibuffer-local-completion-map}
1217 or @code{minibuffer-local-must-match-map}.
1221 @node High-Level Completion
1222 @subsection High-Level Completion Functions
1224 This section describes the higher-level convenience functions for
1225 reading certain sorts of names with completion.
1227 In most cases, you should not call these functions in the middle of a
1228 Lisp function. When possible, do all minibuffer input as part of
1229 reading the arguments for a command, in the @code{interactive}
1230 specification. @xref{Defining Commands}.
1232 @defun read-buffer prompt &optional default require-match
1233 This function reads the name of a buffer and returns it as a string.
1234 The argument @var{default} is the default name to use, the value to
1235 return if the user exits with an empty minibuffer. If non-@code{nil},
1236 it should be a string, a list of strings, or a buffer. If it is
1237 a list, the default value is the first element of this list. It is
1238 mentioned in the prompt, but is not inserted in the minibuffer as
1241 The argument @var{prompt} should be a string ending with a colon and a
1242 space. If @var{default} is non-@code{nil}, the function inserts it in
1243 @var{prompt} before the colon to follow the convention for reading from
1244 the minibuffer with a default value (@pxref{Programming Tips}).
1246 The optional argument @var{require-match} has the same meaning as in
1247 @code{completing-read}. @xref{Minibuffer Completion}.
1249 In the following example, the user enters @samp{minibuffer.t}, and
1250 then types @key{RET}. The argument @var{require-match} is @code{t},
1251 and the only buffer name starting with the given input is
1252 @samp{minibuffer.texi}, so that name is the value.
1255 (read-buffer "Buffer name: " "foo" t)
1257 ;; @r{After evaluation of the preceding expression,}
1258 ;; @r{the following prompt appears,}
1259 ;; @r{with an empty minibuffer:}
1263 ---------- Buffer: Minibuffer ----------
1264 Buffer name (default foo): @point{}
1265 ---------- Buffer: Minibuffer ----------
1269 ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
1270 @result{} "minibuffer.texi"
1275 @defopt read-buffer-function
1276 This variable, if non-@code{nil}, specifies a function for reading
1277 buffer names. @code{read-buffer} calls this function instead of doing
1278 its usual work, with the same arguments passed to @code{read-buffer}.
1281 @defopt read-buffer-completion-ignore-case
1282 If this variable is non-@code{nil}, @code{read-buffer} ignores case
1283 when performing completion.
1286 @defun read-command prompt &optional default
1287 This function reads the name of a command and returns it as a Lisp
1288 symbol. The argument @var{prompt} is used as in
1289 @code{read-from-minibuffer}. Recall that a command is anything for
1290 which @code{commandp} returns @code{t}, and a command name is a symbol
1291 for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
1293 The argument @var{default} specifies what to return if the user enters
1294 null input. It can be a symbol, a string or a list of strings. If it
1295 is a string, @code{read-command} interns it before returning it.
1296 If it is a list, @code{read-command} interns the first element of this list.
1297 If @var{default} is @code{nil}, that means no default has been
1298 specified; then if the user enters null input, the return value is
1299 @code{(intern "")}, that is, a symbol whose name is an empty string.
1302 (read-command "Command name? ")
1305 ;; @r{After evaluation of the preceding expression,}
1306 ;; @r{the following prompt appears with an empty minibuffer:}
1310 ---------- Buffer: Minibuffer ----------
1312 ---------- Buffer: Minibuffer ----------
1317 If the user types @kbd{forward-c @key{RET}}, then this function returns
1318 @code{forward-char}.
1320 The @code{read-command} function is a simplified interface to
1321 @code{completing-read}. It uses the variable @code{obarray} so as to
1322 complete in the set of extant Lisp symbols, and it uses the
1323 @code{commandp} predicate so as to accept only command names:
1325 @cindex @code{commandp} example
1328 (read-command @var{prompt})
1330 (intern (completing-read @var{prompt} obarray
1336 @defun read-variable prompt &optional default
1337 @anchor{Definition of read-variable}
1338 This function reads the name of a customizable variable and returns it
1339 as a symbol. Its arguments have the same form as those of
1340 @code{read-command}. It behaves just like @code{read-command}, except
1341 that it uses the predicate @code{custom-variable-p} instead of
1345 @deffn Command read-color &optional prompt convert allow-empty display
1346 This function reads a string that is a color specification, either the
1347 color's name or an RGB hex value such as @code{#RRRGGGBBB}. It
1348 prompts with @var{prompt} (default: @code{"Color (name or #RGB triplet):"})
1349 and provides completion for color names, but not for hex RGB values.
1350 In addition to names of standard colors, completion candidates include
1351 the foreground and background colors at point.
1353 Valid RGB values are described in @ref{Color Names}.
1355 The function's return value is the string typed by the user in the
1356 minibuffer. However, when called interactively or if the optional
1357 argument @var{convert} is non-@code{nil}, it converts any input color
1358 name into the corresponding RGB value string and instead returns that.
1359 This function requires a valid color specification to be input.
1360 Empty color names are allowed when @var{allow-empty} is
1361 non-@code{nil} and the user enters null input.
1363 Interactively, or when @var{display} is non-@code{nil}, the return
1364 value is also displayed in the echo area.
1367 See also the functions @code{read-coding-system} and
1368 @code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems},
1369 and @code{read-input-method-name}, in @ref{Input Methods}.
1371 @node Reading File Names
1372 @subsection Reading File Names
1373 @cindex read file names
1374 @cindex prompt for file name
1376 The high-level completion functions @code{read-file-name},
1377 @code{read-directory-name}, and @code{read-shell-command} are designed
1378 to read file names, directory names, and shell commands, respectively.
1379 They provide special features, including automatic insertion of the
1382 @defun read-file-name prompt &optional directory default require-match initial predicate
1383 This function reads a file name, prompting with @var{prompt} and
1384 providing completion.
1386 As an exception, this function reads a file name using a graphical
1387 file dialog instead of the minibuffer, if all of the following are
1392 It is invoked via a mouse command.
1395 The selected frame is on a graphical display supporting such dialogs.
1398 The variable @code{use-dialog-box} is non-@code{nil}.
1399 @xref{Dialog Boxes,, Dialog Boxes, emacs, The GNU Emacs Manual}.
1402 The @var{directory} argument, described below, does not specify a
1403 remote file. @xref{Remote Files,, Remote Files, emacs, The GNU Emacs Manual}.
1407 The exact behavior when using a graphical file dialog is
1408 platform-dependent. Here, we simply document the behavior when using
1411 @code{read-file-name} does not automatically expand the returned file
1412 name. You must call @code{expand-file-name} yourself if an absolute
1413 file name is required.
1415 The optional argument @var{require-match} has the same meaning as in
1416 @code{completing-read}. @xref{Minibuffer Completion}.
1418 The argument @var{directory} specifies the directory to use for
1419 completing relative file names. It should be an absolute directory
1420 name. If the variable @code{insert-default-directory} is non-@code{nil},
1421 @var{directory} is also inserted in the minibuffer as initial input.
1422 It defaults to the current buffer's value of @code{default-directory}.
1424 If you specify @var{initial}, that is an initial file name to insert
1425 in the buffer (after @var{directory}, if that is inserted). In this
1426 case, point goes at the beginning of @var{initial}. The default for
1427 @var{initial} is @code{nil}---don't insert any file name. To see what
1428 @var{initial} does, try the command @kbd{C-x C-v} in a buffer visiting
1429 a file. @strong{Please note:} we recommend using @var{default} rather
1430 than @var{initial} in most cases.
1432 If @var{default} is non-@code{nil}, then the function returns
1433 @var{default} if the user exits the minibuffer with the same non-empty
1434 contents that @code{read-file-name} inserted initially. The initial
1435 minibuffer contents are always non-empty if
1436 @code{insert-default-directory} is non-@code{nil}, as it is by
1437 default. @var{default} is not checked for validity, regardless of the
1438 value of @var{require-match}. However, if @var{require-match} is
1439 non-@code{nil}, the initial minibuffer contents should be a valid file
1440 (or directory) name. Otherwise @code{read-file-name} attempts
1441 completion if the user exits without any editing, and does not return
1442 @var{default}. @var{default} is also available through the history
1445 If @var{default} is @code{nil}, @code{read-file-name} tries to find a
1446 substitute default to use in its place, which it treats in exactly the
1447 same way as if it had been specified explicitly. If @var{default} is
1448 @code{nil}, but @var{initial} is non-@code{nil}, then the default is
1449 the absolute file name obtained from @var{directory} and
1450 @var{initial}. If both @var{default} and @var{initial} are @code{nil}
1451 and the buffer is visiting a file, @code{read-file-name} uses the
1452 absolute file name of that file as default. If the buffer is not
1453 visiting a file, then there is no default. In that case, if the user
1454 types @key{RET} without any editing, @code{read-file-name} simply
1455 returns the pre-inserted contents of the minibuffer.
1457 If the user types @key{RET} in an empty minibuffer, this function
1458 returns an empty string, regardless of the value of
1459 @var{require-match}. This is, for instance, how the user can make the
1460 current buffer visit no file using @kbd{M-x set-visited-file-name}.
1462 If @var{predicate} is non-@code{nil}, it specifies a function of one
1463 argument that decides which file names are acceptable completion
1464 alternatives. A file name is an acceptable value if @var{predicate}
1465 returns non-@code{nil} for it.
1467 Here is an example of using @code{read-file-name}:
1471 (read-file-name "The file is ")
1473 ;; @r{After evaluation of the preceding expression,}
1474 ;; @r{the following appears in the minibuffer:}
1478 ---------- Buffer: Minibuffer ----------
1479 The file is /gp/gnu/elisp/@point{}
1480 ---------- Buffer: Minibuffer ----------
1485 Typing @kbd{manual @key{TAB}} results in the following:
1489 ---------- Buffer: Minibuffer ----------
1490 The file is /gp/gnu/elisp/manual.texi@point{}
1491 ---------- Buffer: Minibuffer ----------
1495 @c Wordy to avoid overfull hbox in smallbook mode.
1497 If the user types @key{RET}, @code{read-file-name} returns the file name
1498 as the string @code{"/gp/gnu/elisp/manual.texi"}.
1501 @defvar read-file-name-function
1502 If non-@code{nil}, this should be a function that accepts the same
1503 arguments as @code{read-file-name}. When @code{read-file-name} is
1504 called, it calls this function with the supplied arguments instead of
1505 doing its usual work.
1508 @defopt read-file-name-completion-ignore-case
1509 If this variable is non-@code{nil}, @code{read-file-name} ignores case
1510 when performing completion.
1513 @defun read-directory-name prompt &optional directory default require-match initial
1514 This function is like @code{read-file-name} but allows only directory
1515 names as completion alternatives.
1517 If @var{default} is @code{nil} and @var{initial} is non-@code{nil},
1518 @code{read-directory-name} constructs a substitute default by
1519 combining @var{directory} (or the current buffer's default directory
1520 if @var{directory} is @code{nil}) and @var{initial}. If both
1521 @var{default} and @var{initial} are @code{nil}, this function uses
1522 @var{directory} as substitute default, or the current buffer's default
1523 directory if @var{directory} is @code{nil}.
1526 @defopt insert-default-directory
1527 This variable is used by @code{read-file-name}, and thus, indirectly,
1528 by most commands reading file names. (This includes all commands that
1529 use the code letters @samp{f} or @samp{F} in their interactive form.
1530 @xref{Interactive Codes,, Code Characters for interactive}.) Its
1531 value controls whether @code{read-file-name} starts by placing the
1532 name of the default directory in the minibuffer, plus the initial file
1533 name, if any. If the value of this variable is @code{nil}, then
1534 @code{read-file-name} does not place any initial input in the
1535 minibuffer (unless you specify initial input with the @var{initial}
1536 argument). In that case, the default directory is still used for
1537 completion of relative file names, but is not displayed.
1539 If this variable is @code{nil} and the initial minibuffer contents are
1540 empty, the user may have to explicitly fetch the next history element
1541 to access a default value. If the variable is non-@code{nil}, the
1542 initial minibuffer contents are always non-empty and the user can
1543 always request a default value by immediately typing @key{RET} in an
1544 unedited minibuffer. (See above.)
1550 ;; @r{Here the minibuffer starts out with the default directory.}
1551 (let ((insert-default-directory t))
1552 (read-file-name "The file is "))
1556 ---------- Buffer: Minibuffer ----------
1557 The file is ~lewis/manual/@point{}
1558 ---------- Buffer: Minibuffer ----------
1562 ;; @r{Here the minibuffer is empty and only the prompt}
1563 ;; @r{appears on its line.}
1564 (let ((insert-default-directory nil))
1565 (read-file-name "The file is "))
1569 ---------- Buffer: Minibuffer ----------
1570 The file is @point{}
1571 ---------- Buffer: Minibuffer ----------
1576 @defun read-shell-command prompt &optional initial history &rest args
1577 This function reads a shell command from the minibuffer, prompting
1578 with @var{prompt} and providing intelligent completion. It completes
1579 the first word of the command using candidates that are appropriate
1580 for command names, and the rest of the command words as file names.
1582 This function uses @code{minibuffer-local-shell-command-map} as the
1583 keymap for minibuffer input. The @var{history} argument specifies the
1584 history list to use; if is omitted or @code{nil}, it defaults to
1585 @code{shell-command-history} (@pxref{Minibuffer History,
1586 shell-command-history}). The optional argument @var{initial}
1587 specifies the initial content of the minibuffer (@pxref{Initial
1588 Input}). The rest of @var{args}, if present, are used as the
1589 @var{default} and @var{inherit-input-method} arguments in
1590 @code{read-from-minibuffer} (@pxref{Text from Minibuffer}).
1593 @defvar minibuffer-local-shell-command-map
1594 This keymap is used by @code{read-shell-command} for completing
1595 command and file names that are part of a shell command. It uses
1596 @code{minibuffer-local-map} as its parent keymap, and binds @key{TAB}
1597 to @code{completion-at-point}.
1600 @node Completion Variables
1601 @subsection Completion Variables
1603 Here are some variables that can be used to alter the default
1604 completion behavior.
1606 @cindex completion styles
1607 @defopt completion-styles
1608 The value of this variable is a list of completion style (symbols) to
1609 use for performing completion. A @dfn{completion style} is a set of
1610 rules for generating completions. Each symbol occurring this list
1611 must have a corresponding entry in @code{completion-styles-alist}.
1614 @defvar completion-styles-alist
1615 This variable stores a list of available completion styles. Each
1616 element in the list has the form
1619 (@var{style} @var{try-completion} @var{all-completions} @var{doc})
1623 Here, @var{style} is the name of the completion style (a symbol),
1624 which may be used in the @code{completion-styles} variable to refer to
1625 this style; @var{try-completion} is the function that does the
1626 completion; @var{all-completions} is the function that lists the
1627 completions; and @var{doc} is a string describing the completion
1630 The @var{try-completion} and @var{all-completions} functions should
1631 each accept four arguments: @var{string}, @var{collection},
1632 @var{predicate}, and @var{point}. The @var{string}, @var{collection},
1633 and @var{predicate} arguments have the same meanings as in
1634 @code{try-completion} (@pxref{Basic Completion}), and the @var{point}
1635 argument is the position of point within @var{string}. Each function
1636 should return a non-@code{nil} value if it performed its job, and
1637 @code{nil} if it did not (e.g., if there is no way to complete
1638 @var{string} according to the completion style).
1640 When the user calls a completion command like
1641 @code{minibuffer-complete} (@pxref{Completion Commands}), Emacs looks
1642 for the first style listed in @code{completion-styles} and calls its
1643 @var{try-completion} function. If this function returns @code{nil},
1644 Emacs moves to the next listed completion style and calls its
1645 @var{try-completion} function, and so on until one of the
1646 @var{try-completion} functions successfully performs completion and
1647 returns a non-@code{nil} value. A similar procedure is used for
1648 listing completions, via the @var{all-completions} functions.
1650 @xref{Completion Styles,,, emacs, The GNU Emacs Manual}, for a
1651 description of the available completion styles.
1654 @defopt completion-category-overrides
1655 This variable specifies special completion styles and other completion
1656 behaviors to use when completing certain types of text. Its value
1657 should be an alist with elements of the form @code{(@var{category}
1658 . @var{alist})}. @var{category} is a symbol describing what is being
1659 completed; currently, the @code{buffer}, @code{file}, and
1660 @code{unicode-name} categories are defined, but others can be defined
1661 via specialized completion functions (@pxref{Programmed Completion}).
1662 @var{alist} is an association list describing how completion should
1663 behave for the corresponding category. The following alist keys are
1668 The value should be a list of completion styles (symbols).
1671 The value should be a value for @code{completion-cycle-threshold}
1672 (@pxref{Completion Options,,, emacs, The GNU Emacs Manual}) for this
1677 Additional alist entries may be defined in the future.
1680 @defvar completion-extra-properties
1681 This variable is used to specify extra properties of the current
1682 completion command. It is intended to be let-bound by specialized
1683 completion commands. Its value should be a list of property and value
1684 pairs. The following properties are supported:
1687 @item :annotation-function
1688 The value should be a function to add annotations in the completions
1689 buffer. This function must accept one argument, a completion, and
1690 should either return @code{nil} or a string to be displayed next to
1693 @item :exit-function
1694 The value should be a function to run after performing completion.
1695 The function should accept two arguments, @var{string} and
1696 @var{status}, where @var{string} is the text to which the field was
1697 completed, and @var{status} indicates what kind of operation happened:
1698 @code{finished} if text is now complete, @code{sole} if the text
1699 cannot be further completed but completion is not finished, or
1700 @code{exact} if the text is a valid completion but may be further
1705 @node Programmed Completion
1706 @subsection Programmed Completion
1707 @cindex programmed completion
1709 Sometimes it is not possible or convenient to create an alist or
1710 an obarray containing all the intended possible completions ahead
1711 of time. In such a case, you can supply your own function to compute
1712 the completion of a given string. This is called @dfn{programmed
1713 completion}. Emacs uses programmed completion when completing file
1714 names (@pxref{File Name Completion}), among many other cases.
1716 To use this feature, pass a function as the @var{collection}
1717 argument to @code{completing-read}. The function
1718 @code{completing-read} arranges to pass your completion function along
1719 to @code{try-completion}, @code{all-completions}, and other basic
1720 completion functions, which will then let your function do all
1723 The completion function should accept three arguments:
1727 The string to be completed.
1730 A predicate function with which to filter possible matches, or
1731 @code{nil} if none. The function should call the predicate for each
1732 possible match, and ignore the match if the predicate returns
1736 A flag specifying the type of completion operation to perform. This
1737 is one of the following four values:
1741 This specifies a @code{try-completion} operation. The function should
1742 return @code{t} if the specified string is a unique and exact match;
1743 if there is more than one match, it should return the common substring
1744 of all matches (if the string is an exact match for one completion
1745 alternative but also matches other longer alternatives, the return
1746 value is the string); if there are no matches, it should return
1750 This specifies an @code{all-completions} operation. The function
1751 should return a list of all possible completions of the specified
1755 This specifies a @code{test-completion} operation. The function
1756 should return @code{t} if the specified string is an exact match for
1757 some completion alternative; @code{nil} otherwise.
1759 @item (boundaries . @var{suffix})
1760 This specifies a @code{completion-boundaries} operation. The function
1761 should return @code{(boundaries @var{start} . @var{end})}, where
1762 @var{start} is the position of the beginning boundary in the specified
1763 string, and @var{end} is the position of the end boundary in
1767 This specifies a request for information about the state of the
1768 current completion. The return value should have the form
1769 @code{(metadata . @var{alist})}, where @var{alist} is an alist whose
1770 elements are described below.
1774 If the flag has any other value, the completion function should return
1778 The following is a list of metadata entries that a completion function
1779 may return in response to a @code{metadata} flag argument:
1783 The value should be a symbol describing what kind of text the
1784 completion function is trying to complete. If the symbol matches one
1785 of the keys in @code{completion-category-overrides}, the usual
1786 completion behavior is overridden. @xref{Completion Variables}.
1788 @item annotation-function
1789 The value should be a function for @dfn{annotating} completions. The
1790 function should take one argument, @var{string}, which is a possible
1791 completion. It should return a string, which is displayed after the
1792 completion @var{string} in the @file{*Completions*} buffer.
1794 @item display-sort-function
1795 The value should be a function for sorting completions. The function
1796 should take one argument, a list of completion strings, and return a
1797 sorted list of completion strings. It is allowed to alter the input
1800 @item cycle-sort-function
1801 The value should be a function for sorting completions, when
1802 @code{completion-cycle-threshold} is non-@code{nil} and the user is
1803 cycling through completion alternatives. @xref{Completion Options,,,
1804 emacs, The GNU Emacs Manual}. Its argument list and return value are
1805 the same as for @code{display-sort-function}.
1808 @defun completion-table-dynamic function
1809 This function is a convenient way to write a function that can act as
1810 a programmed completion function. The argument @var{function} should be
1811 a function that takes one argument, a string, and returns an alist of
1812 possible completions of it. You can think of
1813 @code{completion-table-dynamic} as a transducer between that interface
1814 and the interface for programmed completion functions.
1817 @defun completion-table-with-cache function &optional ignore-case
1818 This is a wrapper for @code{completion-table-dynamic} that saves the
1819 last argument-result pair. This means that multiple lookups with the
1820 same argument only need to call @var{function} once. This can be useful
1821 when a slow operation is involved, such as calling an external process.
1824 @node Completion in Buffers
1825 @subsection Completion in Ordinary Buffers
1826 @cindex inline completion
1828 @findex completion-at-point
1829 Although completion is usually done in the minibuffer, the
1830 completion facility can also be used on the text in ordinary Emacs
1831 buffers. In many major modes, in-buffer completion is performed by
1832 the @kbd{C-M-i} or @kbd{M-@key{TAB}} command, bound to
1833 @code{completion-at-point}. @xref{Symbol Completion,,, emacs, The GNU
1834 Emacs Manual}. This command uses the abnormal hook variable
1835 @code{completion-at-point-functions}:
1837 @defvar completion-at-point-functions
1838 The value of this abnormal hook should be a list of functions, which
1839 are used to compute a completion table for completing the text at
1840 point. It can be used by major modes to provide mode-specific
1841 completion tables (@pxref{Major Mode Conventions}).
1843 When the command @code{completion-at-point} runs, it calls the
1844 functions in the list one by one, without any argument. Each function
1845 should return @code{nil} if it is unable to produce a completion table
1846 for the text at point. Otherwise it should return a list of the form
1849 (@var{start} @var{end} @var{collection} . @var{props})
1853 @var{start} and @var{end} delimit the text to complete (which should
1854 enclose point). @var{collection} is a completion table for completing
1855 that text, in a form suitable for passing as the second argument to
1856 @code{try-completion} (@pxref{Basic Completion}); completion
1857 alternatives will be generated from this completion table in the usual
1858 way, via the completion styles defined in @code{completion-styles}
1859 (@pxref{Completion Variables}). @var{props} is a property list for
1860 additional information; any of the properties in
1861 @code{completion-extra-properties} are recognized (@pxref{Completion
1862 Variables}), as well as the following additional ones:
1866 The value should be a predicate that completion candidates need to
1870 If the value is @code{no}, then if the completion table fails to match
1871 the text at point, @code{completion-at-point} moves on to the
1872 next function in @code{completion-at-point-functions} instead of
1873 reporting a completion failure.
1876 A function in @code{completion-at-point-functions} may also return a
1877 function. In that case, that returned function is called, with no
1878 argument, and it is entirely responsible for performing the
1879 completion. We discourage this usage; it is intended to help convert
1880 old code to using @code{completion-at-point}.
1882 The first function in @code{completion-at-point-functions} to return a
1883 non-@code{nil} value is used by @code{completion-at-point}. The
1884 remaining functions are not called. The exception to this is when
1885 there is an @code{:exclusive} specification, as described above.
1888 The following function provides a convenient way to perform
1889 completion on an arbitrary stretch of text in an Emacs buffer:
1891 @defun completion-in-region start end collection &optional predicate
1892 This function completes the text in the current buffer between the
1893 positions @var{start} and @var{end}, using @var{collection}. The
1894 argument @var{collection} has the same meaning as in
1895 @code{try-completion} (@pxref{Basic Completion}).
1897 This function inserts the completion text directly into the current
1898 buffer. Unlike @code{completing-read} (@pxref{Minibuffer
1899 Completion}), it does not activate the minibuffer.
1901 For this function to work, point must be somewhere between @var{start}
1906 @node Yes-or-No Queries
1907 @section Yes-or-No Queries
1908 @cindex asking the user questions
1909 @cindex querying the user
1910 @cindex yes-or-no questions
1912 This section describes functions used to ask the user a yes-or-no
1913 question. The function @code{y-or-n-p} can be answered with a single
1914 character; it is useful for questions where an inadvertent wrong answer
1915 will not have serious consequences. @code{yes-or-no-p} is suitable for
1916 more momentous questions, since it requires three or four characters to
1919 If either of these functions is called in a command that was invoked
1920 using the mouse---more precisely, if @code{last-nonmenu-event}
1921 (@pxref{Command Loop Info}) is either @code{nil} or a list---then it
1922 uses a dialog box or pop-up menu to ask the question. Otherwise, it
1923 uses keyboard input. You can force use either of the mouse or of keyboard
1924 input by binding @code{last-nonmenu-event} to a suitable value around
1927 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
1928 @code{y-or-n-p} does not; but it seems best to describe them together.
1930 @defun y-or-n-p prompt
1931 This function asks the user a question, expecting input in the echo
1932 area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
1933 user types @kbd{n}. This function also accepts @key{SPC} to mean yes
1934 and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit'', like
1935 @kbd{C-g}, because the question might look like a minibuffer and for
1936 that reason the user might try to use @kbd{C-]} to get out. The answer
1937 is a single character, with no @key{RET} needed to terminate it. Upper
1938 and lower case are equivalent.
1940 ``Asking the question'' means printing @var{prompt} in the echo area,
1941 followed by the string @w{@samp{(y or n) }}. If the input is not one of
1942 the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
1943 @kbd{@key{DEL}}, or something that quits), the function responds
1944 @samp{Please answer y or n.}, and repeats the request.
1946 This function does not actually use the minibuffer, since it does not
1947 allow editing of the answer. It actually uses the echo area (@pxref{The
1948 Echo Area}), which uses the same screen space as the minibuffer. The
1949 cursor moves to the echo area while the question is being asked.
1951 The answers and their meanings, even @samp{y} and @samp{n}, are not
1952 hardwired, and are specified by the keymap @code{query-replace-map}
1953 (@pxref{Search and Replace}). In particular, if the user enters the
1954 special responses @code{recenter}, @code{scroll-up},
1955 @code{scroll-down}, @code{scroll-other-window}, or
1956 @code{scroll-other-window-down} (respectively bound to @kbd{C-l},
1957 @kbd{C-v}, @kbd{M-v}, @kbd{C-M-v} and @kbd{C-M-S-v} in
1958 @code{query-replace-map}), this function performs the specified window
1959 recentering or scrolling operation, and poses the question again.
1962 We show successive lines of echo area messages, but only one actually
1963 appears on the screen at a time.
1966 @defun y-or-n-p-with-timeout prompt seconds default
1967 Like @code{y-or-n-p}, except that if the user fails to answer within
1968 @var{seconds} seconds, this function stops waiting and returns
1969 @var{default}. It works by setting up a timer; see @ref{Timers}.
1970 The argument @var{seconds} should be a number.
1973 @defun yes-or-no-p prompt
1974 This function asks the user a question, expecting input in the
1975 minibuffer. It returns @code{t} if the user enters @samp{yes},
1976 @code{nil} if the user types @samp{no}. The user must type @key{RET} to
1977 finalize the response. Upper and lower case are equivalent.
1979 @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
1980 followed by @w{@samp{(yes or no) }}. The user must type one of the
1981 expected responses; otherwise, the function responds @samp{Please answer
1982 yes or no.}, waits about two seconds and repeats the request.
1984 @code{yes-or-no-p} requires more work from the user than
1985 @code{y-or-n-p} and is appropriate for more crucial decisions.
1991 (yes-or-no-p "Do you really want to remove everything? ")
1993 ;; @r{After evaluation of the preceding expression,}
1994 ;; @r{the following prompt appears,}
1995 ;; @r{with an empty minibuffer:}
1999 ---------- Buffer: minibuffer ----------
2000 Do you really want to remove everything? (yes or no)
2001 ---------- Buffer: minibuffer ----------
2006 If the user first types @kbd{y @key{RET}}, which is invalid because this
2007 function demands the entire word @samp{yes}, it responds by displaying
2008 these prompts, with a brief pause between them:
2012 ---------- Buffer: minibuffer ----------
2013 Please answer yes or no.
2014 Do you really want to remove everything? (yes or no)
2015 ---------- Buffer: minibuffer ----------
2020 @node Multiple Queries
2021 @section Asking Multiple Y-or-N Questions
2023 When you have a series of similar questions to ask, such as ``Do you
2024 want to save this buffer'' for each buffer in turn, you should use
2025 @code{map-y-or-n-p} to ask the collection of questions, rather than
2026 asking each question individually. This gives the user certain
2027 convenient facilities such as the ability to answer the whole series at
2030 @defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
2031 This function asks the user a series of questions, reading a
2032 single-character answer in the echo area for each one.
2034 The value of @var{list} specifies the objects to ask questions about.
2035 It should be either a list of objects or a generator function. If it is
2036 a function, it should expect no arguments, and should return either the
2037 next object to ask about, or @code{nil}, meaning to stop asking questions.
2039 The argument @var{prompter} specifies how to ask each question. If
2040 @var{prompter} is a string, the question text is computed like this:
2043 (format @var{prompter} @var{object})
2047 where @var{object} is the next object to ask about (as obtained from
2050 If not a string, @var{prompter} should be a function of one argument
2051 (the next object to ask about) and should return the question text. If
2052 the value is a string, that is the question to ask the user. The
2053 function can also return @code{t}, meaning do act on this object (and
2054 don't ask the user), or @code{nil}, meaning ignore this object (and don't
2057 The argument @var{actor} says how to act on the answers that the user
2058 gives. It should be a function of one argument, and it is called with
2059 each object that the user says yes for. Its argument is always an
2060 object obtained from @var{list}.
2062 If the argument @var{help} is given, it should be a list of this form:
2065 (@var{singular} @var{plural} @var{action})
2069 where @var{singular} is a string containing a singular noun that
2070 describes the objects conceptually being acted on, @var{plural} is the
2071 corresponding plural noun, and @var{action} is a transitive verb
2072 describing what @var{actor} does.
2074 If you don't specify @var{help}, the default is @code{("object"
2075 "objects" "act on")}.
2077 Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
2078 @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
2079 that object; @kbd{!} to act on all following objects; @key{ESC} or
2080 @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
2081 the current object and then exit; or @kbd{C-h} to get help. These are
2082 the same answers that @code{query-replace} accepts. The keymap
2083 @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
2084 as well as for @code{query-replace}; see @ref{Search and Replace}.
2086 You can use @var{action-alist} to specify additional possible answers
2087 and what they mean. It is an alist of elements of the form
2088 @code{(@var{char} @var{function} @var{help})}, each of which defines one
2089 additional answer. In this element, @var{char} is a character (the
2090 answer); @var{function} is a function of one argument (an object from
2091 @var{list}); @var{help} is a string.
2093 When the user responds with @var{char}, @code{map-y-or-n-p} calls
2094 @var{function}. If it returns non-@code{nil}, the object is considered
2095 ``acted upon'', and @code{map-y-or-n-p} advances to the next object in
2096 @var{list}. If it returns @code{nil}, the prompt is repeated for the
2099 Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
2100 prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
2103 If @code{map-y-or-n-p} is called in a command that was invoked using the
2104 mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
2105 Loop Info}) is either @code{nil} or a list---then it uses a dialog box
2106 or pop-up menu to ask the question. In this case, it does not use
2107 keyboard input or the echo area. You can force use either of the mouse or
2108 of keyboard input by binding @code{last-nonmenu-event} to a suitable
2109 value around the call.
2111 The return value of @code{map-y-or-n-p} is the number of objects acted on.
2113 @c FIXME An example of this would be more useful than all the
2114 @c preceding examples of simple things.
2116 @node Reading a Password
2117 @section Reading a Password
2118 @cindex passwords, reading
2120 To read a password to pass to another program, you can use the
2121 function @code{read-passwd}.
2123 @defun read-passwd prompt &optional confirm default
2124 This function reads a password, prompting with @var{prompt}. It does
2125 not echo the password as the user types it; instead, it echoes @samp{.}
2126 for each character in the password.
2128 The optional argument @var{confirm}, if non-@code{nil}, says to read the
2129 password twice and insist it must be the same both times. If it isn't
2130 the same, the user has to type it over and over until the last two
2133 The optional argument @var{default} specifies the default password to
2134 return if the user enters empty input. If @var{default} is @code{nil},
2135 then @code{read-passwd} returns the null string in that case.
2138 @node Minibuffer Commands
2139 @section Minibuffer Commands
2141 This section describes some commands meant for use in the
2144 @deffn Command exit-minibuffer
2145 This command exits the active minibuffer. It is normally bound to
2146 keys in minibuffer local keymaps.
2149 @deffn Command self-insert-and-exit
2150 This command exits the active minibuffer after inserting the last
2151 character typed on the keyboard (found in @code{last-command-event};
2152 @pxref{Command Loop Info}).
2155 @deffn Command previous-history-element n
2156 This command replaces the minibuffer contents with the value of the
2157 @var{n}th previous (older) history element.
2160 @deffn Command next-history-element n
2161 This command replaces the minibuffer contents with the value of the
2162 @var{n}th more recent history element.
2165 @deffn Command previous-matching-history-element pattern n
2166 This command replaces the minibuffer contents with the value of the
2167 @var{n}th previous (older) history element that matches @var{pattern} (a
2168 regular expression).
2171 @deffn Command next-matching-history-element pattern n
2172 This command replaces the minibuffer contents with the value of the
2173 @var{n}th next (newer) history element that matches @var{pattern} (a
2174 regular expression).
2177 @deffn Command previous-complete-history-element n
2178 This command replaces the minibuffer contents with the value of the
2179 @var{n}th previous (older) history element that completes the current
2180 contents of the minibuffer before the point.
2183 @deffn Command next-complete-history-element n
2184 This command replaces the minibuffer contents with the value of the
2185 @var{n}th next (newer) history element that completes the current
2186 contents of the minibuffer before the point.
2190 @node Minibuffer Windows
2191 @section Minibuffer Windows
2192 @cindex minibuffer windows
2194 These functions access and select minibuffer windows
2195 and test whether they are active.
2197 @defun active-minibuffer-window
2198 This function returns the currently active minibuffer window, or
2199 @code{nil} if there is none.
2202 @defun minibuffer-window &optional frame
2203 @anchor{Definition of minibuffer-window}
2204 This function returns the minibuffer window used for frame @var{frame}.
2205 If @var{frame} is @code{nil}, that stands for the current frame. Note
2206 that the minibuffer window used by a frame need not be part of that
2207 frame---a frame that has no minibuffer of its own necessarily uses some
2208 other frame's minibuffer window.
2211 @defun set-minibuffer-window window
2212 This function specifies @var{window} as the minibuffer window to use.
2213 This affects where the minibuffer is displayed if you put text in it
2214 without invoking the usual minibuffer commands. It has no effect on
2215 the usual minibuffer input functions because they all start by
2216 choosing the minibuffer window according to the current frame.
2220 @defun window-minibuffer-p &optional window
2221 This function returns non-@code{nil} if @var{window} is a minibuffer
2223 @var{window} defaults to the selected window.
2226 It is not correct to determine whether a given window is a minibuffer by
2227 comparing it with the result of @code{(minibuffer-window)}, because
2228 there can be more than one minibuffer window if there is more than one
2231 @defun minibuffer-window-active-p window
2232 This function returns non-@code{nil} if @var{window} is the currently
2233 active minibuffer window.
2236 @node Minibuffer Contents
2237 @section Minibuffer Contents
2239 These functions access the minibuffer prompt and contents.
2241 @defun minibuffer-prompt
2242 This function returns the prompt string of the currently active
2243 minibuffer. If no minibuffer is active, it returns @code{nil}.
2246 @defun minibuffer-prompt-end
2247 This function returns the current
2248 position of the end of the minibuffer prompt, if a minibuffer is
2249 current. Otherwise, it returns the minimum valid buffer position.
2252 @defun minibuffer-prompt-width
2253 This function returns the current display-width of the minibuffer
2254 prompt, if a minibuffer is current. Otherwise, it returns zero.
2257 @defun minibuffer-contents
2258 This function returns the editable
2259 contents of the minibuffer (that is, everything except the prompt) as
2260 a string, if a minibuffer is current. Otherwise, it returns the
2261 entire contents of the current buffer.
2264 @defun minibuffer-contents-no-properties
2265 This is like @code{minibuffer-contents}, except that it does not copy text
2266 properties, just the characters themselves. @xref{Text Properties}.
2269 @defun delete-minibuffer-contents
2270 This function erases the editable contents of the minibuffer (that is,
2271 everything except the prompt), if a minibuffer is current. Otherwise,
2272 it erases the entire current buffer.
2275 @node Recursive Mini
2276 @section Recursive Minibuffers
2277 @cindex recursive minibuffers
2279 These functions and variables deal with recursive minibuffers
2280 (@pxref{Recursive Editing}):
2282 @defun minibuffer-depth
2283 This function returns the current depth of activations of the
2284 minibuffer, a nonnegative integer. If no minibuffers are active, it
2288 @defopt enable-recursive-minibuffers
2289 If this variable is non-@code{nil}, you can invoke commands (such as
2290 @code{find-file}) that use minibuffers even while the minibuffer window
2291 is active. Such invocation produces a recursive editing level for a new
2292 minibuffer. The outer-level minibuffer is invisible while you are
2293 editing the inner one.
2295 If this variable is @code{nil}, you cannot invoke minibuffer
2296 commands when the minibuffer window is active, not even if you switch to
2297 another window to do it.
2301 If a command name has a property @code{enable-recursive-minibuffers}
2302 that is non-@code{nil}, then the command can use the minibuffer to read
2303 arguments even if it is invoked from the minibuffer. A command can
2304 also achieve this by binding @code{enable-recursive-minibuffers}
2305 to @code{t} in the interactive declaration (@pxref{Using Interactive}).
2306 The minibuffer command @code{next-matching-history-element} (normally
2307 @kbd{M-s} in the minibuffer) does the latter.
2309 @node Minibuffer Misc
2310 @section Minibuffer Miscellany
2312 @defun minibufferp &optional buffer-or-name
2313 This function returns non-@code{nil} if @var{buffer-or-name} is a
2314 minibuffer. If @var{buffer-or-name} is omitted, it tests the current
2318 @defvar minibuffer-setup-hook
2319 This is a normal hook that is run whenever the minibuffer is entered.
2323 @defvar minibuffer-exit-hook
2324 This is a normal hook that is run whenever the minibuffer is exited.
2328 @defvar minibuffer-help-form
2329 @anchor{Definition of minibuffer-help-form}
2330 The current value of this variable is used to rebind @code{help-form}
2331 locally inside the minibuffer (@pxref{Help Functions}).
2334 @defvar minibuffer-scroll-window
2335 @anchor{Definition of minibuffer-scroll-window}
2336 If the value of this variable is non-@code{nil}, it should be a window
2337 object. When the function @code{scroll-other-window} is called in the
2338 minibuffer, it scrolls this window.
2341 @defun minibuffer-selected-window
2342 This function returns the window that was selected when the
2343 minibuffer was entered. If selected window is not a minibuffer
2344 window, it returns @code{nil}.
2347 @defopt max-mini-window-height
2348 This variable specifies the maximum height for resizing minibuffer
2349 windows. If a float, it specifies a fraction of the height of the
2350 frame. If an integer, it specifies a number of lines.
2353 @vindex minibuffer-message-timeout
2354 @defun minibuffer-message string &rest args
2355 This function displays @var{string} temporarily at the end of the
2356 minibuffer text, for a few seconds, or until the next input event
2357 arrives, whichever comes first. The variable
2358 @code{minibuffer-message-timeout} specifies the number of seconds to
2359 wait in the absence of input. It defaults to 2. If @var{args} is
2360 non-@code{nil}, the actual message is obtained by passing @var{string}
2361 and @var{args} through @code{format}. @xref{Formatting Strings}.
2364 @deffn Command minibuffer-inactive-mode
2365 This is the major mode used in inactive minibuffers. It uses
2366 keymap @code{minibuffer-inactive-mode-map}. This can be useful
2367 if the minibuffer is in a separate frame. @xref{Minibuffers and Frames}.