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