New directory
[bpt/emacs.git] / lispref / minibuf.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/minibuf
7 @node Minibuffers, Command Loop, Read and Print, Top
8 @chapter Minibuffers
9 @cindex arguments, reading
10 @cindex complex arguments
11 @cindex minibuffer
12
13 A @dfn{minibuffer} is a special buffer that Emacs commands use to read
14 arguments more complicated than the single numeric prefix argument.
15 These arguments include file names, buffer names, and command names (as
16 in @kbd{M-x}). The minibuffer is displayed on the bottom line of the
17 frame, in the same place as the echo area, but only while it is in use
18 for reading an argument.
19
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 * Completion:: How to invoke and customize completion.
27 * Yes-or-No Queries:: Asking a question with a simple answer.
28 * Multiple Queries:: Asking a series of similar questions.
29 * Reading a Password:: Reading a password from the terminal.
30 * Minibuffer Misc:: Various customization hooks and variables.
31 @end menu
32
33 @node Intro to Minibuffers
34 @section Introduction to Minibuffers
35
36 In most ways, a minibuffer is a normal Emacs buffer. Most operations
37 @emph{within} a buffer, such as editing commands, work normally in a
38 minibuffer. However, many operations for managing buffers do not apply
39 to minibuffers. The name of a minibuffer always has the form @w{@samp{
40 *Minibuf-@var{number}*}}, and it cannot be changed. Minibuffers are
41 displayed only in special windows used only for minibuffers; these
42 windows always appear at the bottom of a frame. (Sometimes frames have
43 no minibuffer window, and sometimes a special kind of frame contains
44 nothing but a minibuffer window; see @ref{Minibuffers and Frames}.)
45
46 The text in the minibuffer always starts with the @dfn{prompt string},
47 the text that was specified by the program that is using the minibuffer
48 to tell the user what sort of input to type. This text is marked
49 read-only so you won't accidentally delete or change it. It is also
50 marked as a field (@pxref{Fields}), so that certain motion functions,
51 including @code{beginning-of-line}, @code{forward-word},
52 @code{forward-sentence}, and @code{forward-paragraph}, stop at the
53 boundary between the prompt and the actual text. (In older Emacs
54 versions, the prompt was displayed using a special mechanism and was not
55 part of the buffer contents.)
56
57 The minibuffer's window is normally a single line; it grows
58 automatically if necessary if the contents require more space. You can
59 explicitly resize it temporarily with the window sizing commands; it
60 reverts to its normal size when the minibuffer is exited. You can
61 resize it permanently by using the window sizing commands in the frame's
62 other window, when the minibuffer is not active. If the frame contains
63 just a minibuffer, you can change the minibuffer's size by changing the
64 frame's size.
65
66 If a command uses a minibuffer while there is an active minibuffer,
67 this is called a @dfn{recursive minibuffer}. The first minibuffer is
68 named @w{@samp{ *Minibuf-0*}}. Recursive minibuffers are named by
69 incrementing the number at the end of the name. (The names begin with a
70 space so that they won't show up in normal buffer lists.) Of several
71 recursive minibuffers, the innermost (or most recently entered) is the
72 active minibuffer. We usually call this ``the'' minibuffer. You can
73 permit or forbid recursive minibuffers by setting the variable
74 @code{enable-recursive-minibuffers} or by putting properties of that
75 name on command symbols (@pxref{Minibuffer Misc}).
76
77 Like other buffers, a minibuffer may use any of several local keymaps
78 (@pxref{Keymaps}); these contain various exit commands and in some cases
79 completion commands (@pxref{Completion}).
80
81 @itemize @bullet
82 @item
83 @code{minibuffer-local-map} is for ordinary input (no completion).
84
85 @item
86 @code{minibuffer-local-ns-map} is similar, except that @key{SPC} exits
87 just like @key{RET}.
88
89 @item
90 @code{minibuffer-local-completion-map} is for permissive completion.
91
92 @item
93 @code{minibuffer-local-must-match-map} is for strict completion and
94 for cautious completion.
95 @end itemize
96
97 When Emacs is running in batch mode, any request to read from the
98 minibuffer actually reads a line from the standard input descriptor that
99 was supplied when Emacs was started.
100
101 @node Text from Minibuffer
102 @section Reading Text Strings with the Minibuffer
103
104 Most often, the minibuffer is used to read text as a string. It can
105 also be used to read a Lisp object in textual form. The most basic
106 primitive for minibuffer input is @code{read-from-minibuffer}; it can do
107 either one.
108
109 In most cases, you should not call minibuffer input functions in the
110 middle of a Lisp function. Instead, do all minibuffer input as part of
111 reading the arguments for a command, in the @code{interactive}
112 specification. @xref{Defining Commands}.
113
114 @defun read-from-minibuffer prompt-string &optional initial-contents keymap read hist default inherit-input-method
115 This function is the most general way to get input through the
116 minibuffer. By default, it accepts arbitrary text and returns it as a
117 string; however, if @var{read} is non-@code{nil}, then it uses
118 @code{read} to convert the text into a Lisp object (@pxref{Input
119 Functions}).
120
121 The first thing this function does is to activate a minibuffer and
122 display it with @var{prompt-string} as the prompt. This value must be a
123 string. Then the user can edit text in the minibuffer.
124
125 When the user types a command to exit the minibuffer,
126 @code{read-from-minibuffer} constructs the return value from the text in
127 the minibuffer. Normally it returns a string containing that text.
128 However, if @var{read} is non-@code{nil}, @code{read-from-minibuffer}
129 reads the text and returns the resulting Lisp object, unevaluated.
130 (@xref{Input Functions}, for information about reading.)
131
132 The argument @var{default} specifies a default value to make available
133 through the history commands. It should be a string, or @code{nil}. If
134 @var{read} is non-@code{nil}, then @var{default} is also used as the
135 input to @code{read}, if the user enters empty input. However, in the
136 usual case (where @var{read} is @code{nil}), @code{read-from-minibuffer}
137 does not return @var{default} when the user enters empty input; it
138 returns an empty string, @code{""}. In this respect, it is different
139 from all the other minibuffer input functions in this chapter.
140
141 If @var{keymap} is non-@code{nil}, that keymap is the local keymap to
142 use in the minibuffer. If @var{keymap} is omitted or @code{nil}, the
143 value of @code{minibuffer-local-map} is used as the keymap. Specifying
144 a keymap is the most important way to customize the minibuffer for
145 various applications such as completion.
146
147 The argument @var{hist} specifies which history list variable to use
148 for saving the input and for history commands used in the minibuffer.
149 It defaults to @code{minibuffer-history}. @xref{Minibuffer History}.
150
151 If the variable @code{minibuffer-allow-text-properties} is
152 non-@code{nil}, then the string which is returned includes whatever text
153 properties were present in the minibuffer. Otherwise all the text
154 properties are stripped when the value is returned.
155
156 If the argument @var{inherit-input-method} is non-@code{nil}, then the
157 minibuffer inherits the current input method (@pxref{Input Methods}) and
158 the setting of @code{enable-multibyte-characters} (@pxref{Text
159 Representations}) from whichever buffer was current before entering the
160 minibuffer.
161
162 If @var{initial-contents} is a string, @code{read-from-minibuffer}
163 inserts it into the minibuffer, leaving point at the end, before the
164 user starts to edit the text. The minibuffer appears with this text as
165 its initial contents.
166
167 Alternatively, @var{initial-contents} can be a cons cell of the form
168 @code{(@var{string} . @var{position})}. This means to insert
169 @var{string} in the minibuffer but put point @var{position} characters
170 from the beginning, rather than at the end.
171
172 @strong{Usage note:} The @var{initial-contents} argument and the
173 @var{default} argument are two alternative features for more or less the
174 same job. It does not make sense to use both features in a single call
175 to @code{read-from-minibuffer}. In general, we recommend using
176 @var{default}, since this permits the user to insert the default value
177 when it is wanted, but does not burden the user with deleting it from
178 the minibuffer on other occasions.
179 @end defun
180
181 @defun read-string prompt &optional initial history default inherit-input-method
182 This function reads a string from the minibuffer and returns it. The
183 arguments @var{prompt} and @var{initial} are used as in
184 @code{read-from-minibuffer}. The keymap used is
185 @code{minibuffer-local-map}.
186
187 The optional argument @var{history}, if non-@code{nil}, specifies a
188 history list and optionally the initial position in the list. The
189 optional argument @var{default} specifies a default value to return if
190 the user enters null input; it should be a string. The optional
191 argument @var{inherit-input-method} specifies whether to inherit the
192 current buffer's input method.
193
194 This function is a simplified interface to the
195 @code{read-from-minibuffer} function:
196
197 @smallexample
198 @group
199 (read-string @var{prompt} @var{initial} @var{history} @var{default} @var{inherit})
200 @equiv{}
201 (let ((value
202 (read-from-minibuffer @var{prompt} @var{initial} nil nil
203 @var{history} @var{default} @var{inherit})))
204 (if (equal value "")
205 @var{default}
206 value))
207 @end group
208 @end smallexample
209 @end defun
210
211 @defvar minibuffer-allow-text-properties
212 If this variable is @code{nil}, then @code{read-from-minibuffer} strips
213 all text properties from the minibuffer input before returning it.
214 Since all minibuffer input uses @code{read-from-minibuffer}, this
215 variable applies to all minibuffer input.
216
217 Note that the completion functions discard text properties unconditionally,
218 regardless of the value of this variable.
219 @end defvar
220
221 @defvar minibuffer-local-map
222 This is the default local keymap for reading from the minibuffer. By
223 default, it makes the following bindings:
224
225 @table @asis
226 @item @kbd{C-j}
227 @code{exit-minibuffer}
228
229 @item @key{RET}
230 @code{exit-minibuffer}
231
232 @item @kbd{C-g}
233 @code{abort-recursive-edit}
234
235 @item @kbd{M-n}
236 @code{next-history-element}
237
238 @item @kbd{M-p}
239 @code{previous-history-element}
240
241 @item @kbd{M-r}
242 @code{next-matching-history-element}
243
244 @item @kbd{M-s}
245 @code{previous-matching-history-element}
246 @end table
247 @end defvar
248
249 @c In version 18, initial is required
250 @c Emacs 19 feature
251 @defun read-no-blanks-input prompt &optional initial inherit-input-method
252 This function reads a string from the minibuffer, but does not allow
253 whitespace characters as part of the input: instead, those characters
254 terminate the input. The arguments @var{prompt}, @var{initial}, and
255 @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
256
257 This is a simplified interface to the @code{read-from-minibuffer}
258 function, and passes the value of the @code{minibuffer-local-ns-map}
259 keymap as the @var{keymap} argument for that function. Since the keymap
260 @code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
261 possible to put a space into the string, by quoting it.
262
263 @smallexample
264 @group
265 (read-no-blanks-input @var{prompt} @var{initial})
266 @equiv{}
267 (read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map)
268 @end group
269 @end smallexample
270 @end defun
271
272 @defvar minibuffer-local-ns-map
273 This built-in variable is the keymap used as the minibuffer local keymap
274 in the function @code{read-no-blanks-input}. By default, it makes the
275 following bindings, in addition to those of @code{minibuffer-local-map}:
276
277 @table @asis
278 @item @key{SPC}
279 @cindex @key{SPC} in minibuffer
280 @code{exit-minibuffer}
281
282 @item @key{TAB}
283 @cindex @key{TAB} in minibuffer
284 @code{exit-minibuffer}
285
286 @item @kbd{?}
287 @cindex @kbd{?} in minibuffer
288 @code{self-insert-and-exit}
289 @end table
290 @end defvar
291
292 @node Object from Minibuffer
293 @section Reading Lisp Objects with the Minibuffer
294
295 This section describes functions for reading Lisp objects with the
296 minibuffer.
297
298 @defun read-minibuffer prompt &optional initial
299 This function reads a Lisp object using the minibuffer, and returns it
300 without evaluating it. The arguments @var{prompt} and @var{initial} are
301 used as in @code{read-from-minibuffer}.
302
303 This is a simplified interface to the
304 @code{read-from-minibuffer} function:
305
306 @smallexample
307 @group
308 (read-minibuffer @var{prompt} @var{initial})
309 @equiv{}
310 (read-from-minibuffer @var{prompt} @var{initial} nil t)
311 @end group
312 @end smallexample
313
314 Here is an example in which we supply the string @code{"(testing)"} as
315 initial input:
316
317 @smallexample
318 @group
319 (read-minibuffer
320 "Enter an expression: " (format "%s" '(testing)))
321
322 ;; @r{Here is how the minibuffer is displayed:}
323 @end group
324
325 @group
326 ---------- Buffer: Minibuffer ----------
327 Enter an expression: (testing)@point{}
328 ---------- Buffer: Minibuffer ----------
329 @end group
330 @end smallexample
331
332 @noindent
333 The user can type @key{RET} immediately to use the initial input as a
334 default, or can edit the input.
335 @end defun
336
337 @defun eval-minibuffer prompt &optional initial
338 This function reads a Lisp expression using the minibuffer, evaluates
339 it, then returns the result. The arguments @var{prompt} and
340 @var{initial} are used as in @code{read-from-minibuffer}.
341
342 This function simply evaluates the result of a call to
343 @code{read-minibuffer}:
344
345 @smallexample
346 @group
347 (eval-minibuffer @var{prompt} @var{initial})
348 @equiv{}
349 (eval (read-minibuffer @var{prompt} @var{initial}))
350 @end group
351 @end smallexample
352 @end defun
353
354 @defun edit-and-eval-command prompt form
355 This function reads a Lisp expression in the minibuffer, and then
356 evaluates it. The difference between this command and
357 @code{eval-minibuffer} is that here the initial @var{form} is not
358 optional and it is treated as a Lisp object to be converted to printed
359 representation rather than as a string of text. It is printed with
360 @code{prin1}, so if it is a string, double-quote characters (@samp{"})
361 appear in the initial text. @xref{Output Functions}.
362
363 The first thing @code{edit-and-eval-command} does is to activate the
364 minibuffer with @var{prompt} as the prompt. Then it inserts the printed
365 representation of @var{form} in the minibuffer, and lets the user edit it.
366 When the user exits the minibuffer, the edited text is read with
367 @code{read} and then evaluated. The resulting value becomes the value
368 of @code{edit-and-eval-command}.
369
370 In the following example, we offer the user an expression with initial
371 text which is a valid form already:
372
373 @smallexample
374 @group
375 (edit-and-eval-command "Please edit: " '(forward-word 1))
376
377 ;; @r{After evaluation of the preceding expression,}
378 ;; @r{the following appears in the minibuffer:}
379 @end group
380
381 @group
382 ---------- Buffer: Minibuffer ----------
383 Please edit: (forward-word 1)@point{}
384 ---------- Buffer: Minibuffer ----------
385 @end group
386 @end smallexample
387
388 @noindent
389 Typing @key{RET} right away would exit the minibuffer and evaluate the
390 expression, thus moving point forward one word.
391 @code{edit-and-eval-command} returns @code{nil} in this example.
392 @end defun
393
394 @node Minibuffer History
395 @section Minibuffer History
396 @cindex minibuffer history
397 @cindex history list
398
399 A @dfn{minibuffer history list} records previous minibuffer inputs so
400 the user can reuse them conveniently. A history list is actually a
401 symbol, not a list; it is a variable whose value is a list of strings
402 (previous inputs), most recent first.
403
404 There are many separate history lists, used for different kinds of
405 inputs. It's the Lisp programmer's job to specify the right history
406 list for each use of the minibuffer.
407
408 The basic minibuffer input functions @code{read-from-minibuffer} and
409 @code{completing-read} both accept an optional argument named @var{hist}
410 which is how you specify the history list. Here are the possible
411 values:
412
413 @table @asis
414 @item @var{variable}
415 Use @var{variable} (a symbol) as the history list.
416
417 @item (@var{variable} . @var{startpos})
418 Use @var{variable} (a symbol) as the history list, and assume that the
419 initial history position is @var{startpos} (an integer, counting from
420 zero which specifies the most recent element of the history).
421
422 If you specify @var{startpos}, then you should also specify that element
423 of the history as the initial minibuffer contents, for consistency.
424 @end table
425
426 If you don't specify @var{hist}, then the default history list
427 @code{minibuffer-history} is used. For other standard history lists,
428 see below. You can also create your own history list variable; just
429 initialize it to @code{nil} before the first use.
430
431 Both @code{read-from-minibuffer} and @code{completing-read} add new
432 elements to the history list automatically, and provide commands to
433 allow the user to reuse items on the list. The only thing your program
434 needs to do to use a history list is to initialize it and to pass its
435 name to the input functions when you wish. But it is safe to modify the
436 list by hand when the minibuffer input functions are not using it.
437
438 Emacs functions that add a new element to a history list can also
439 delete old elements if the list gets too long. The variable
440 @code{history-length} specifies the maximum length for most history
441 lists. To specify a different maximum length for a particular history
442 list, put the length in the @code{history-length} property of the
443 history list symbol.
444
445 @defvar history-length
446 The value of this variable specifies the maximum length for all
447 history lists that don't specify their own maximum lengths. If the
448 value is @code{t}, that means there no maximum (don't delete old
449 elements).
450 @end defvar
451
452 Here are some of the standard minibuffer history list variables:
453
454 @defvar minibuffer-history
455 The default history list for minibuffer history input.
456 @end defvar
457
458 @defvar query-replace-history
459 A history list for arguments to @code{query-replace} (and similar
460 arguments to other commands).
461 @end defvar
462
463 @defvar file-name-history
464 A history list for file-name arguments.
465 @end defvar
466
467 @defvar buffer-name-history
468 A history list for buffer-name arguments.
469 @end defvar
470
471 @defvar regexp-history
472 A history list for regular expression arguments.
473 @end defvar
474
475 @defvar extended-command-history
476 A history list for arguments that are names of extended commands.
477 @end defvar
478
479 @defvar shell-command-history
480 A history list for arguments that are shell commands.
481 @end defvar
482
483 @defvar read-expression-history
484 A history list for arguments that are Lisp expressions to evaluate.
485 @end defvar
486
487 @node Completion
488 @section Completion
489 @cindex completion
490
491 @dfn{Completion} is a feature that fills in the rest of a name
492 starting from an abbreviation for it. Completion works by comparing the
493 user's input against a list of valid names and determining how much of
494 the name is determined uniquely by what the user has typed. For
495 example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
496 type the first few letters of the name of the buffer to which you wish
497 to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
498 extends the name as far as it can.
499
500 Standard Emacs commands offer completion for names of symbols, files,
501 buffers, and processes; with the functions in this section, you can
502 implement completion for other kinds of names.
503
504 The @code{try-completion} function is the basic primitive for
505 completion: it returns the longest determined completion of a given
506 initial string, with a given set of strings to match against.
507
508 The function @code{completing-read} provides a higher-level interface
509 for completion. A call to @code{completing-read} specifies how to
510 determine the list of valid names. The function then activates the
511 minibuffer with a local keymap that binds a few keys to commands useful
512 for completion. Other functions provide convenient simple interfaces
513 for reading certain kinds of names with completion.
514
515 @menu
516 * Basic Completion:: Low-level functions for completing strings.
517 (These are too low level to use the minibuffer.)
518 * Minibuffer Completion:: Invoking the minibuffer with completion.
519 * Completion Commands:: Minibuffer commands that do completion.
520 * High-Level Completion:: Convenient special cases of completion
521 (reading buffer name, file name, etc.)
522 * Reading File Names:: Using completion to read file names.
523 * Programmed Completion:: Writing your own completion-function.
524 @end menu
525
526 @node Basic Completion
527 @subsection Basic Completion Functions
528
529 The functions @code{try-completion}, @code{all-completions} and
530 @code{test-completion} have nothing in themselves to do with
531 minibuffers. We describe them in this chapter so as to keep them near
532 the higher-level completion features that do use the minibuffer.
533
534 @defun try-completion string collection &optional predicate
535 This function returns the longest common substring of all possible
536 completions of @var{string} in @var{collection}. The value of
537 @var{collection} must be a list of strings, an alist, an obarray, or a
538 function that implements a virtual set of strings (see below).
539
540 Completion compares @var{string} against each of the permissible
541 completions specified by @var{collection}; if the beginning of the
542 permissible completion equals @var{string}, it matches. If no permissible
543 completions match, @code{try-completion} returns @code{nil}. If only
544 one permissible completion matches, and the match is exact, then
545 @code{try-completion} returns @code{t}. Otherwise, the value is the
546 longest initial sequence common to all the permissible completions that
547 match.
548
549 If @var{collection} is an alist (@pxref{Association Lists}), the
550 @sc{car}s of the alist elements form the set of permissible completions.
551
552 @cindex obarray in completion
553 If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
554 of all symbols in the obarray form the set of permissible completions. The
555 global variable @code{obarray} holds an obarray containing the names of
556 all interned Lisp symbols.
557
558 Note that the only valid way to make a new obarray is to create it
559 empty and then add symbols to it one by one using @code{intern}.
560 Also, you cannot intern a given symbol in more than one obarray.
561
562 You can also use a symbol that is a function as @var{collection}. Then
563 the function is solely responsible for performing completion;
564 @code{try-completion} returns whatever this function returns. The
565 function is called with three arguments: @var{string}, @var{predicate}
566 and @code{nil}. (The reason for the third argument is so that the same
567 function can be used in @code{all-completions} and do the appropriate
568 thing in either case.) @xref{Programmed Completion}.
569
570 If the argument @var{predicate} is non-@code{nil}, then it must be a
571 function of one argument. It is used to test each possible match, and
572 the match is accepted only if @var{predicate} returns non-@code{nil}.
573 The argument given to @var{predicate} is either a string from the
574 list, a cons cell from the alist (the @sc{car} of which is a string)
575 or a symbol (@emph{not} a symbol name) from the obarray.
576
577 In the first of the following examples, the string @samp{foo} is
578 matched by three of the alist @sc{car}s. All of the matches begin with
579 the characters @samp{fooba}, so that is the result. In the second
580 example, there is only one possible match, and it is exact, so the value
581 is @code{t}.
582
583 @smallexample
584 @group
585 (try-completion
586 "foo"
587 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
588 @result{} "fooba"
589 @end group
590
591 @group
592 (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
593 @result{} t
594 @end group
595 @end smallexample
596
597 In the following example, numerous symbols begin with the characters
598 @samp{forw}, and all of them begin with the word @samp{forward}. In
599 most of the symbols, this is followed with a @samp{-}, but not in all,
600 so no more than @samp{forward} can be completed.
601
602 @smallexample
603 @group
604 (try-completion "forw" obarray)
605 @result{} "forward"
606 @end group
607 @end smallexample
608
609 Finally, in the following example, only two of the three possible
610 matches pass the predicate @code{test} (the string @samp{foobaz} is
611 too short). Both of those begin with the string @samp{foobar}.
612
613 @smallexample
614 @group
615 (defun test (s)
616 (> (length (car s)) 6))
617 @result{} test
618 @end group
619 @group
620 (try-completion
621 "foo"
622 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
623 'test)
624 @result{} "foobar"
625 @end group
626 @end smallexample
627 @end defun
628
629 @defun all-completions string collection &optional predicate nospace
630 This function returns a list of all possible completions of
631 @var{string}. The arguments to this function (aside from @var{nospace})
632 are the same as those of @code{try-completion}. If @var{nospace} is
633 non-@code{nil}, completions that start with a space are ignored unless
634 @var{string} also starts with a space.
635
636 If @var{collection} is a function, it is called with three arguments:
637 @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
638 returns whatever the function returns. @xref{Programmed Completion}.
639
640 Here is an example, using the function @code{test} shown in the
641 example for @code{try-completion}:
642
643 @smallexample
644 @group
645 (defun test (s)
646 (> (length (car s)) 6))
647 @result{} test
648 @end group
649
650 @group
651 (all-completions
652 "foo"
653 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
654 'test)
655 @result{} ("foobar1" "foobar2")
656 @end group
657 @end smallexample
658 @end defun
659
660 @defun test-completion string collection &optional predicate
661 This function returns non-@code{nil} if @var{string} is a valid
662 completion possibility specified by @var{collection} and
663 @var{predicate}. The other arguments are the same as in
664 @code{try-completion}. For instance, if @var{collection} is a list,
665 this is true if @var{string} appears in the list and @var{predicate}
666 is satisfied.
667
668 If @var{collection} is a function, it is called with three arguments,
669 the values @var{string}, @var{predicate} and @code{lambda}; whatever
670 it returns, @code{test-completion} returns in turn.
671 @end defun
672
673 @defvar completion-ignore-case
674 If the value of this variable is non-@code{nil}, Emacs does not
675 consider case significant in completion.
676 @end defvar
677
678 @defmac lazy-completion-table var fun &rest args
679 This macro provides a way to initialize the variable @var{var} as a
680 collection for completion in a lazy way, not computing its actual
681 contents until they are first needed. You use this macro to produce a
682 value that you store in @var{var}. The actual computation of the
683 proper value is done the first time you do completion using @var{var}.
684 It is done by calling @var{fun} with the arguments @var{args}. The
685 value @var{fun} returns becomes the permanent value of @var{var}.
686
687 Here are two examples of use:
688
689 @example
690 (defvar foo (lazy-completion-table foo make-my-alist 'global))
691
692 (make-local-variable 'bar)
693 (setq bar (lazy-completion-table foo make-my-alist 'local)
694 @end example
695 @end defmac
696
697 @node Minibuffer Completion
698 @subsection Completion and the Minibuffer
699
700 This section describes the basic interface for reading from the
701 minibuffer with completion.
702
703 @defun completing-read prompt collection &optional predicate require-match initial hist default inherit-input-method
704 This function reads a string in the minibuffer, assisting the user by
705 providing completion. It activates the minibuffer with prompt
706 @var{prompt}, which must be a string.
707
708 The actual completion is done by passing @var{collection} and
709 @var{predicate} to the function @code{try-completion}. This happens in
710 certain commands bound in the local keymaps used for completion.
711
712 If @var{require-match} is @code{nil}, the exit commands work regardless
713 of the input in the minibuffer. If @var{require-match} is @code{t}, the
714 usual minibuffer exit commands won't exit unless the input completes to
715 an element of @var{collection}. If @var{require-match} is neither
716 @code{nil} nor @code{t}, then the exit commands won't exit unless the
717 input already in the buffer matches an element of @var{collection}.
718
719 However, empty input is always permitted, regardless of the value of
720 @var{require-match}; in that case, @code{completing-read} returns
721 @var{default}. The value of @var{default} (if non-@code{nil}) is also
722 available to the user through the history commands.
723
724 The user can exit with null input by typing @key{RET} with an empty
725 minibuffer. Then @code{completing-read} returns @code{""}. This is how
726 the user requests whatever default the command uses for the value being
727 read. The user can return using @key{RET} in this way regardless of the
728 value of @var{require-match}, and regardless of whether the empty string
729 is included in @var{collection}.
730
731 The function @code{completing-read} works by calling
732 @code{read-minibuffer}. It uses @code{minibuffer-local-completion-map}
733 as the keymap if @var{require-match} is @code{nil}, and uses
734 @code{minibuffer-local-must-match-map} if @var{require-match} is
735 non-@code{nil}. @xref{Completion Commands}.
736
737 The argument @var{hist} specifies which history list variable to use for
738 saving the input and for minibuffer history commands. It defaults to
739 @code{minibuffer-history}. @xref{Minibuffer History}.
740
741 If @var{initial} is non-@code{nil}, @code{completing-read} inserts it
742 into the minibuffer as part of the input. Then it allows the user to
743 edit the input, providing several commands to attempt completion.
744 In most cases, we recommend using @var{default}, and not @var{initial}.
745
746 @strong{We discourage use of a non-@code{nil} value for
747 @var{initial}}, because it is an intrusive interface. The history
748 list feature (which did not exist when we introduced @var{initial})
749 offers a far more convenient and general way for the user to get the
750 default and edit it, and it is always available.
751
752 If the argument @var{inherit-input-method} is non-@code{nil}, then the
753 minibuffer inherits the current input method (@pxref{Input
754 Methods}) and the setting of @code{enable-multibyte-characters}
755 (@pxref{Text Representations}) from whichever buffer was current before
756 entering the minibuffer.
757
758 Completion ignores case when comparing the input against the possible
759 matches, if the built-in variable @code{completion-ignore-case} is
760 non-@code{nil}. @xref{Basic Completion}.
761
762 Here's an example of using @code{completing-read}:
763
764 @smallexample
765 @group
766 (completing-read
767 "Complete a foo: "
768 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
769 nil t "fo")
770 @end group
771
772 @group
773 ;; @r{After evaluation of the preceding expression,}
774 ;; @r{the following appears in the minibuffer:}
775
776 ---------- Buffer: Minibuffer ----------
777 Complete a foo: fo@point{}
778 ---------- Buffer: Minibuffer ----------
779 @end group
780 @end smallexample
781
782 @noindent
783 If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
784 @code{completing-read} returns @code{barfoo}.
785
786 The @code{completing-read} function binds three variables to pass
787 information to the commands that actually do completion. These
788 variables are @code{minibuffer-completion-table},
789 @code{minibuffer-completion-predicate} and
790 @code{minibuffer-completion-confirm}. For more information about them,
791 see @ref{Completion Commands}.
792 @end defun
793
794 @node Completion Commands
795 @subsection Minibuffer Commands that Do Completion
796
797 This section describes the keymaps, commands and user options used in
798 the minibuffer to do completion.
799
800 @defvar minibuffer-local-completion-map
801 @code{completing-read} uses this value as the local keymap when an
802 exact match of one of the completions is not required. By default, this
803 keymap makes the following bindings:
804
805 @table @asis
806 @item @kbd{?}
807 @code{minibuffer-completion-help}
808
809 @item @key{SPC}
810 @code{minibuffer-complete-word}
811
812 @item @key{TAB}
813 @code{minibuffer-complete}
814 @end table
815
816 @noindent
817 with other characters bound as in @code{minibuffer-local-map}
818 (@pxref{Text from Minibuffer}).
819 @end defvar
820
821 @defvar minibuffer-local-must-match-map
822 @code{completing-read} uses this value as the local keymap when an
823 exact match of one of the completions is required. Therefore, no keys
824 are bound to @code{exit-minibuffer}, the command that exits the
825 minibuffer unconditionally. By default, this keymap makes the following
826 bindings:
827
828 @table @asis
829 @item @kbd{?}
830 @code{minibuffer-completion-help}
831
832 @item @key{SPC}
833 @code{minibuffer-complete-word}
834
835 @item @key{TAB}
836 @code{minibuffer-complete}
837
838 @item @kbd{C-j}
839 @code{minibuffer-complete-and-exit}
840
841 @item @key{RET}
842 @code{minibuffer-complete-and-exit}
843 @end table
844
845 @noindent
846 with other characters bound as in @code{minibuffer-local-map}.
847 @end defvar
848
849 @defvar minibuffer-completion-table
850 The value of this variable is the alist or obarray used for completion
851 in the minibuffer. This is the global variable that contains what
852 @code{completing-read} passes to @code{try-completion}. It is used by
853 minibuffer completion commands such as @code{minibuffer-complete-word}.
854 @end defvar
855
856 @defvar minibuffer-completion-predicate
857 This variable's value is the predicate that @code{completing-read}
858 passes to @code{try-completion}. The variable is also used by the other
859 minibuffer completion functions.
860 @end defvar
861
862 @deffn Command minibuffer-complete-word
863 This function completes the minibuffer contents by at most a single
864 word. Even if the minibuffer contents have only one completion,
865 @code{minibuffer-complete-word} does not add any characters beyond the
866 first character that is not a word constituent. @xref{Syntax Tables}.
867 @end deffn
868
869 @deffn Command minibuffer-complete
870 This function completes the minibuffer contents as far as possible.
871 @end deffn
872
873 @deffn Command minibuffer-complete-and-exit
874 This function completes the minibuffer contents, and exits if
875 confirmation is not required, i.e., if
876 @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
877 @emph{is} required, it is given by repeating this command
878 immediately---the command is programmed to work without confirmation
879 when run twice in succession.
880 @end deffn
881
882 @defvar minibuffer-completion-confirm
883 When the value of this variable is non-@code{nil}, Emacs asks for
884 confirmation of a completion before exiting the minibuffer. The
885 function @code{minibuffer-complete-and-exit} checks the value of this
886 variable before it exits.
887 @end defvar
888
889 @deffn Command minibuffer-completion-help
890 This function creates a list of the possible completions of the
891 current minibuffer contents. It works by calling @code{all-completions}
892 using the value of the variable @code{minibuffer-completion-table} as
893 the @var{collection} argument, and the value of
894 @code{minibuffer-completion-predicate} as the @var{predicate} argument.
895 The list of completions is displayed as text in a buffer named
896 @samp{*Completions*}.
897 @end deffn
898
899 @defun display-completion-list completions
900 This function displays @var{completions} to the stream in
901 @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
902 information about streams.) The argument @var{completions} is normally
903 a list of completions just returned by @code{all-completions}, but it
904 does not have to be. Each element may be a symbol or a string, either
905 of which is simply printed, or a list of two strings, which is printed
906 as if the strings were concatenated.
907
908 This function is called by @code{minibuffer-completion-help}. The
909 most common way to use it is together with
910 @code{with-output-to-temp-buffer}, like this:
911
912 @example
913 (with-output-to-temp-buffer "*Completions*"
914 (display-completion-list
915 (all-completions (buffer-string) my-alist)))
916 @end example
917 @end defun
918
919 @defopt completion-auto-help
920 If this variable is non-@code{nil}, the completion commands
921 automatically display a list of possible completions whenever nothing
922 can be completed because the next character is not uniquely determined.
923 @end defopt
924
925 @node High-Level Completion
926 @subsection High-Level Completion Functions
927
928 This section describes the higher-level convenient functions for
929 reading certain sorts of names with completion.
930
931 In most cases, you should not call these functions in the middle of a
932 Lisp function. When possible, do all minibuffer input as part of
933 reading the arguments for a command, in the @code{interactive}
934 specification. @xref{Defining Commands}.
935
936 @defun read-buffer prompt &optional default existing
937 This function reads the name of a buffer and returns it as a string.
938 The argument @var{default} is the default name to use, the value to
939 return if the user exits with an empty minibuffer. If non-@code{nil},
940 it should be a string or a buffer. It is mentioned in the prompt, but
941 is not inserted in the minibuffer as initial input.
942
943 If @var{existing} is non-@code{nil}, then the name specified must be
944 that of an existing buffer. The usual commands to exit the minibuffer
945 do not exit if the text is not valid, and @key{RET} does completion to
946 attempt to find a valid name. (However, @var{default} is not checked
947 for validity; it is returned, whatever it is, if the user exits with the
948 minibuffer empty.)
949
950 In the following example, the user enters @samp{minibuffer.t}, and
951 then types @key{RET}. The argument @var{existing} is @code{t}, and the
952 only buffer name starting with the given input is
953 @samp{minibuffer.texi}, so that name is the value.
954
955 @example
956 (read-buffer "Buffer name? " "foo" t)
957 @group
958 ;; @r{After evaluation of the preceding expression,}
959 ;; @r{the following prompt appears,}
960 ;; @r{with an empty minibuffer:}
961 @end group
962
963 @group
964 ---------- Buffer: Minibuffer ----------
965 Buffer name? (default foo) @point{}
966 ---------- Buffer: Minibuffer ----------
967 @end group
968
969 @group
970 ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
971 @result{} "minibuffer.texi"
972 @end group
973 @end example
974 @end defun
975
976 @defvar read-buffer-function
977 This variable specifies how to read buffer names. For example, if you
978 set this variable to @code{iswitchb-read-buffer}, all Emacs commands
979 that call @code{read-buffer} to read a buffer name will actually use the
980 @code{iswitchb} package to read it.
981 @end defvar
982
983 @defun read-command prompt &optional default
984 This function reads the name of a command and returns it as a Lisp
985 symbol. The argument @var{prompt} is used as in
986 @code{read-from-minibuffer}. Recall that a command is anything for
987 which @code{commandp} returns @code{t}, and a command name is a symbol
988 for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
989
990 The argument @var{default} specifies what to return if the user enters
991 null input. It can be a symbol or a string; if it is a string,
992 @code{read-command} interns it before returning it. If @var{default} is
993 @code{nil}, that means no default has been specified; then if the user
994 enters null input, the return value is @code{nil}.
995
996 @example
997 (read-command "Command name? ")
998
999 @group
1000 ;; @r{After evaluation of the preceding expression,}
1001 ;; @r{the following prompt appears with an empty minibuffer:}
1002 @end group
1003
1004 @group
1005 ---------- Buffer: Minibuffer ----------
1006 Command name?
1007 ---------- Buffer: Minibuffer ----------
1008 @end group
1009 @end example
1010
1011 @noindent
1012 If the user types @kbd{forward-c @key{RET}}, then this function returns
1013 @code{forward-char}.
1014
1015 The @code{read-command} function is a simplified interface to
1016 @code{completing-read}. It uses the variable @code{obarray} so as to
1017 complete in the set of extant Lisp symbols, and it uses the
1018 @code{commandp} predicate so as to accept only command names:
1019
1020 @cindex @code{commandp} example
1021 @example
1022 @group
1023 (read-command @var{prompt})
1024 @equiv{}
1025 (intern (completing-read @var{prompt} obarray
1026 'commandp t nil))
1027 @end group
1028 @end example
1029 @end defun
1030
1031 @defun read-variable prompt &optional default
1032 This function reads the name of a user variable and returns it as a
1033 symbol.
1034
1035 The argument @var{default} specifies what to return if the user enters
1036 null input. It can be a symbol or a string; if it is a string,
1037 @code{read-variable} interns it before returning it. If @var{default}
1038 is @code{nil}, that means no default has been specified; then if the
1039 user enters null input, the return value is @code{nil}.
1040
1041 @example
1042 @group
1043 (read-variable "Variable name? ")
1044
1045 ;; @r{After evaluation of the preceding expression,}
1046 ;; @r{the following prompt appears,}
1047 ;; @r{with an empty minibuffer:}
1048 @end group
1049
1050 @group
1051 ---------- Buffer: Minibuffer ----------
1052 Variable name? @point{}
1053 ---------- Buffer: Minibuffer ----------
1054 @end group
1055 @end example
1056
1057 @noindent
1058 If the user then types @kbd{fill-p @key{RET}}, @code{read-variable}
1059 returns @code{fill-prefix}.
1060
1061 This function is similar to @code{read-command}, but uses the
1062 predicate @code{user-variable-p} instead of @code{commandp}:
1063
1064 @cindex @code{user-variable-p} example
1065 @example
1066 @group
1067 (read-variable @var{prompt})
1068 @equiv{}
1069 (intern
1070 (completing-read @var{prompt} obarray
1071 'user-variable-p t nil))
1072 @end group
1073 @end example
1074 @end defun
1075
1076 See also the functions @code{read-coding-system} and
1077 @code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems}.
1078
1079 @node Reading File Names
1080 @subsection Reading File Names
1081
1082 Here is another high-level completion function, designed for reading a
1083 file name. It provides special features including automatic insertion
1084 of the default directory.
1085
1086 @defun read-file-name prompt &optional directory default existing initial
1087 This function reads a file name in the minibuffer, prompting with
1088 @var{prompt} and providing completion. If @var{default} is
1089 non-@code{nil}, then the function returns @var{default} if the user just
1090 types @key{RET}. @var{default} is not checked for validity; it is
1091 returned, whatever it is, if the user exits with the minibuffer empty.
1092
1093 If @var{existing} is non-@code{nil}, then the user must specify the name
1094 of an existing file; @key{RET} performs completion to make the name
1095 valid if possible, and then refuses to exit if it is not valid. If the
1096 value of @var{existing} is neither @code{nil} nor @code{t}, then
1097 @key{RET} also requires confirmation after completion. If
1098 @var{existing} is @code{nil}, then the name of a nonexistent file is
1099 acceptable.
1100
1101 The argument @var{directory} specifies the directory to use for
1102 completion of relative file names. If @code{insert-default-directory}
1103 is non-@code{nil}, @var{directory} is also inserted in the minibuffer as
1104 initial input. It defaults to the current buffer's value of
1105 @code{default-directory}.
1106
1107 @c Emacs 19 feature
1108 If you specify @var{initial}, that is an initial file name to insert in
1109 the buffer (after @var{directory}, if that is inserted). In this
1110 case, point goes at the beginning of @var{initial}. The default for
1111 @var{initial} is @code{nil}---don't insert any file name. To see what
1112 @var{initial} does, try the command @kbd{C-x C-v}. @strong{Note:} we
1113 recommend using @var{default} rather than @var{initial} in most cases.
1114
1115 Here is an example:
1116
1117 @example
1118 @group
1119 (read-file-name "The file is ")
1120
1121 ;; @r{After evaluation of the preceding expression,}
1122 ;; @r{the following appears in the minibuffer:}
1123 @end group
1124
1125 @group
1126 ---------- Buffer: Minibuffer ----------
1127 The file is /gp/gnu/elisp/@point{}
1128 ---------- Buffer: Minibuffer ----------
1129 @end group
1130 @end example
1131
1132 @noindent
1133 Typing @kbd{manual @key{TAB}} results in the following:
1134
1135 @example
1136 @group
1137 ---------- Buffer: Minibuffer ----------
1138 The file is /gp/gnu/elisp/manual.texi@point{}
1139 ---------- Buffer: Minibuffer ----------
1140 @end group
1141 @end example
1142
1143 @c Wordy to avoid overfull hbox in smallbook mode.
1144 @noindent
1145 If the user types @key{RET}, @code{read-file-name} returns the file name
1146 as the string @code{"/gp/gnu/elisp/manual.texi"}.
1147 @end defun
1148
1149 @defopt insert-default-directory
1150 This variable is used by @code{read-file-name}. Its value controls
1151 whether @code{read-file-name} starts by placing the name of the default
1152 directory in the minibuffer, plus the initial file name if any. If the
1153 value of this variable is @code{nil}, then @code{read-file-name} does
1154 not place any initial input in the minibuffer (unless you specify
1155 initial input with the @var{initial} argument). In that case, the
1156 default directory is still used for completion of relative file names,
1157 but is not displayed.
1158
1159 For example:
1160
1161 @example
1162 @group
1163 ;; @r{Here the minibuffer starts out with the default directory.}
1164 (let ((insert-default-directory t))
1165 (read-file-name "The file is "))
1166 @end group
1167
1168 @group
1169 ---------- Buffer: Minibuffer ----------
1170 The file is ~lewis/manual/@point{}
1171 ---------- Buffer: Minibuffer ----------
1172 @end group
1173
1174 @group
1175 ;; @r{Here the minibuffer is empty and only the prompt}
1176 ;; @r{appears on its line.}
1177 (let ((insert-default-directory nil))
1178 (read-file-name "The file is "))
1179 @end group
1180
1181 @group
1182 ---------- Buffer: Minibuffer ----------
1183 The file is @point{}
1184 ---------- Buffer: Minibuffer ----------
1185 @end group
1186 @end example
1187 @end defopt
1188
1189 @node Programmed Completion
1190 @subsection Programmed Completion
1191 @cindex programmed completion
1192
1193 Sometimes it is not possible to create an alist or an obarray
1194 containing all the intended possible completions. In such a case, you
1195 can supply your own function to compute the completion of a given string.
1196 This is called @dfn{programmed completion}.
1197
1198 To use this feature, pass a symbol with a function definition as the
1199 @var{collection} argument to @code{completing-read}. The function
1200 @code{completing-read} arranges to pass your completion function along
1201 to @code{try-completion} and @code{all-completions}, which will then let
1202 your function do all the work.
1203
1204 The completion function should accept three arguments:
1205
1206 @itemize @bullet
1207 @item
1208 The string to be completed.
1209
1210 @item
1211 The predicate function to filter possible matches, or @code{nil} if
1212 none. Your function should call the predicate for each possible match,
1213 and ignore the possible match if the predicate returns @code{nil}.
1214
1215 @item
1216 A flag specifying the type of operation.
1217 @end itemize
1218
1219 There are three flag values for three operations:
1220
1221 @itemize @bullet
1222 @item
1223 @code{nil} specifies @code{try-completion}. The completion function
1224 should return the completion of the specified string, or @code{t} if the
1225 string is a unique and exact match already, or @code{nil} if the string
1226 matches no possibility.
1227
1228 If the string is an exact match for one possibility, but also matches
1229 other longer possibilities, the function should return the string, not
1230 @code{t}.
1231
1232 @item
1233 @code{t} specifies @code{all-completions}. The completion function
1234 should return a list of all possible completions of the specified
1235 string.
1236
1237 @item
1238 @code{lambda} specifies @code{test-completion}. The completion
1239 function should return @code{t} if the specified string is an exact
1240 match for some possibility; @code{nil} otherwise.
1241 @end itemize
1242
1243 It would be consistent and clean for completion functions to allow
1244 lambda expressions (lists that are functions) as well as function
1245 symbols as @var{collection}, but this is impossible. Lists as
1246 completion tables already have other meanings, and it would be
1247 unreliable to treat one differently just because it is also a possible
1248 function. So you must arrange for any function you wish to use for
1249 completion to be encapsulated in a symbol.
1250
1251 Emacs uses programmed completion when completing file names.
1252 @xref{File Name Completion}.
1253
1254 @defmac dynamic-completion-table function
1255 This macro is a convenient way to write a function that can act as
1256 programmed completion function. The argument @var{function} should be
1257 a function that takes one argument, a string, and returns an alist of
1258 possible completions of it. You can think of
1259 @code{dynamic-completion-table} as a transducer between that interface
1260 and the interface for programmed completion functions.
1261 @end defmac
1262
1263 @node Yes-or-No Queries
1264 @section Yes-or-No Queries
1265 @cindex asking the user questions
1266 @cindex querying the user
1267 @cindex yes-or-no questions
1268
1269 This section describes functions used to ask the user a yes-or-no
1270 question. The function @code{y-or-n-p} can be answered with a single
1271 character; it is useful for questions where an inadvertent wrong answer
1272 will not have serious consequences. @code{yes-or-no-p} is suitable for
1273 more momentous questions, since it requires three or four characters to
1274 answer.
1275
1276 If either of these functions is called in a command that was invoked
1277 using the mouse---more precisely, if @code{last-nonmenu-event}
1278 (@pxref{Command Loop Info}) is either @code{nil} or a list---then it
1279 uses a dialog box or pop-up menu to ask the question. Otherwise, it
1280 uses keyboard input. You can force use of the mouse or use of keyboard
1281 input by binding @code{last-nonmenu-event} to a suitable value around
1282 the call.
1283
1284 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
1285 @code{y-or-n-p} does not; but it seems best to describe them together.
1286
1287 @defun y-or-n-p prompt
1288 This function asks the user a question, expecting input in the echo
1289 area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
1290 user types @kbd{n}. This function also accepts @key{SPC} to mean yes
1291 and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit'', like
1292 @kbd{C-g}, because the question might look like a minibuffer and for
1293 that reason the user might try to use @kbd{C-]} to get out. The answer
1294 is a single character, with no @key{RET} needed to terminate it. Upper
1295 and lower case are equivalent.
1296
1297 ``Asking the question'' means printing @var{prompt} in the echo area,
1298 followed by the string @w{@samp{(y or n) }}. If the input is not one of
1299 the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
1300 @kbd{@key{DEL}}, or something that quits), the function responds
1301 @samp{Please answer y or n.}, and repeats the request.
1302
1303 This function does not actually use the minibuffer, since it does not
1304 allow editing of the answer. It actually uses the echo area (@pxref{The
1305 Echo Area}), which uses the same screen space as the minibuffer. The
1306 cursor moves to the echo area while the question is being asked.
1307
1308 The answers and their meanings, even @samp{y} and @samp{n}, are not
1309 hardwired. The keymap @code{query-replace-map} specifies them.
1310 @xref{Search and Replace}.
1311
1312 In the following example, the user first types @kbd{q}, which is
1313 invalid. At the next prompt the user types @kbd{y}.
1314
1315 @smallexample
1316 @group
1317 (y-or-n-p "Do you need a lift? ")
1318
1319 ;; @r{After evaluation of the preceding expression,}
1320 ;; @r{the following prompt appears in the echo area:}
1321 @end group
1322
1323 @group
1324 ---------- Echo area ----------
1325 Do you need a lift? (y or n)
1326 ---------- Echo area ----------
1327 @end group
1328
1329 ;; @r{If the user then types @kbd{q}, the following appears:}
1330
1331 @group
1332 ---------- Echo area ----------
1333 Please answer y or n. Do you need a lift? (y or n)
1334 ---------- Echo area ----------
1335 @end group
1336
1337 ;; @r{When the user types a valid answer,}
1338 ;; @r{it is displayed after the question:}
1339
1340 @group
1341 ---------- Echo area ----------
1342 Do you need a lift? (y or n) y
1343 ---------- Echo area ----------
1344 @end group
1345 @end smallexample
1346
1347 @noindent
1348 We show successive lines of echo area messages, but only one actually
1349 appears on the screen at a time.
1350 @end defun
1351
1352 @defun y-or-n-p-with-timeout prompt seconds default-value
1353 Like @code{y-or-n-p}, except that if the user fails to answer within
1354 @var{seconds} seconds, this function stops waiting and returns
1355 @var{default-value}. It works by setting up a timer; see @ref{Timers}.
1356 The argument @var{seconds} may be an integer or a floating point number.
1357 @end defun
1358
1359 @defun yes-or-no-p prompt
1360 This function asks the user a question, expecting input in the
1361 minibuffer. It returns @code{t} if the user enters @samp{yes},
1362 @code{nil} if the user types @samp{no}. The user must type @key{RET} to
1363 finalize the response. Upper and lower case are equivalent.
1364
1365 @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
1366 followed by @w{@samp{(yes or no) }}. The user must type one of the
1367 expected responses; otherwise, the function responds @samp{Please answer
1368 yes or no.}, waits about two seconds and repeats the request.
1369
1370 @code{yes-or-no-p} requires more work from the user than
1371 @code{y-or-n-p} and is appropriate for more crucial decisions.
1372
1373 Here is an example:
1374
1375 @smallexample
1376 @group
1377 (yes-or-no-p "Do you really want to remove everything? ")
1378
1379 ;; @r{After evaluation of the preceding expression,}
1380 ;; @r{the following prompt appears,}
1381 ;; @r{with an empty minibuffer:}
1382 @end group
1383
1384 @group
1385 ---------- Buffer: minibuffer ----------
1386 Do you really want to remove everything? (yes or no)
1387 ---------- Buffer: minibuffer ----------
1388 @end group
1389 @end smallexample
1390
1391 @noindent
1392 If the user first types @kbd{y @key{RET}}, which is invalid because this
1393 function demands the entire word @samp{yes}, it responds by displaying
1394 these prompts, with a brief pause between them:
1395
1396 @smallexample
1397 @group
1398 ---------- Buffer: minibuffer ----------
1399 Please answer yes or no.
1400 Do you really want to remove everything? (yes or no)
1401 ---------- Buffer: minibuffer ----------
1402 @end group
1403 @end smallexample
1404 @end defun
1405
1406 @node Multiple Queries
1407 @section Asking Multiple Y-or-N Questions
1408
1409 When you have a series of similar questions to ask, such as ``Do you
1410 want to save this buffer'' for each buffer in turn, you should use
1411 @code{map-y-or-n-p} to ask the collection of questions, rather than
1412 asking each question individually. This gives the user certain
1413 convenient facilities such as the ability to answer the whole series at
1414 once.
1415
1416 @defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
1417 This function asks the user a series of questions, reading a
1418 single-character answer in the echo area for each one.
1419
1420 The value of @var{list} specifies the objects to ask questions about.
1421 It should be either a list of objects or a generator function. If it is
1422 a function, it should expect no arguments, and should return either the
1423 next object to ask about, or @code{nil} meaning stop asking questions.
1424
1425 The argument @var{prompter} specifies how to ask each question. If
1426 @var{prompter} is a string, the question text is computed like this:
1427
1428 @example
1429 (format @var{prompter} @var{object})
1430 @end example
1431
1432 @noindent
1433 where @var{object} is the next object to ask about (as obtained from
1434 @var{list}).
1435
1436 If not a string, @var{prompter} should be a function of one argument
1437 (the next object to ask about) and should return the question text. If
1438 the value is a string, that is the question to ask the user. The
1439 function can also return @code{t} meaning do act on this object (and
1440 don't ask the user), or @code{nil} meaning ignore this object (and don't
1441 ask the user).
1442
1443 The argument @var{actor} says how to act on the answers that the user
1444 gives. It should be a function of one argument, and it is called with
1445 each object that the user says yes for. Its argument is always an
1446 object obtained from @var{list}.
1447
1448 If the argument @var{help} is given, it should be a list of this form:
1449
1450 @example
1451 (@var{singular} @var{plural} @var{action})
1452 @end example
1453
1454 @noindent
1455 where @var{singular} is a string containing a singular noun that
1456 describes the objects conceptually being acted on, @var{plural} is the
1457 corresponding plural noun, and @var{action} is a transitive verb
1458 describing what @var{actor} does.
1459
1460 If you don't specify @var{help}, the default is @code{("object"
1461 "objects" "act on")}.
1462
1463 Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
1464 @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
1465 that object; @kbd{!} to act on all following objects; @key{ESC} or
1466 @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
1467 the current object and then exit; or @kbd{C-h} to get help. These are
1468 the same answers that @code{query-replace} accepts. The keymap
1469 @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
1470 as well as for @code{query-replace}; see @ref{Search and Replace}.
1471
1472 You can use @var{action-alist} to specify additional possible answers
1473 and what they mean. It is an alist of elements of the form
1474 @code{(@var{char} @var{function} @var{help})}, each of which defines one
1475 additional answer. In this element, @var{char} is a character (the
1476 answer); @var{function} is a function of one argument (an object from
1477 @var{list}); @var{help} is a string.
1478
1479 When the user responds with @var{char}, @code{map-y-or-n-p} calls
1480 @var{function}. If it returns non-@code{nil}, the object is considered
1481 ``acted upon'', and @code{map-y-or-n-p} advances to the next object in
1482 @var{list}. If it returns @code{nil}, the prompt is repeated for the
1483 same object.
1484
1485 Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
1486 prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
1487 does not do that.
1488
1489 If @code{map-y-or-n-p} is called in a command that was invoked using the
1490 mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
1491 Loop Info}) is either @code{nil} or a list---then it uses a dialog box
1492 or pop-up menu to ask the question. In this case, it does not use
1493 keyboard input or the echo area. You can force use of the mouse or use
1494 of keyboard input by binding @code{last-nonmenu-event} to a suitable
1495 value around the call.
1496
1497 The return value of @code{map-y-or-n-p} is the number of objects acted on.
1498 @end defun
1499
1500 @node Reading a Password
1501 @section Reading a Password
1502 @cindex passwords, reading
1503
1504 To read a password to pass to another program, you can use the
1505 function @code{read-passwd}.
1506
1507 @defun read-passwd prompt &optional confirm default
1508 This function reads a password, prompting with @var{prompt}. It does
1509 not echo the password as the user types it; instead, it echoes @samp{.}
1510 for each character in the password.
1511
1512 The optional argument @var{confirm}, if non-@code{nil}, says to read the
1513 password twice and insist it must be the same both times. If it isn't
1514 the same, the user has to type it over and over until the last two
1515 times match.
1516
1517 The optional argument @var{default} specifies the default password to
1518 return if the user enters empty input. If @var{default} is @code{nil},
1519 then @code{read-passwd} returns the null string in that case.
1520 @end defun
1521
1522 @node Minibuffer Misc
1523 @section Minibuffer Miscellany
1524
1525 This section describes some basic functions and variables related to
1526 minibuffers.
1527
1528 @deffn Command exit-minibuffer
1529 This command exits the active minibuffer. It is normally bound to
1530 keys in minibuffer local keymaps.
1531 @end deffn
1532
1533 @deffn Command self-insert-and-exit
1534 This command exits the active minibuffer after inserting the last
1535 character typed on the keyboard (found in @code{last-command-char};
1536 @pxref{Command Loop Info}).
1537 @end deffn
1538
1539 @deffn Command previous-history-element n
1540 This command replaces the minibuffer contents with the value of the
1541 @var{n}th previous (older) history element.
1542 @end deffn
1543
1544 @deffn Command next-history-element n
1545 This command replaces the minibuffer contents with the value of the
1546 @var{n}th more recent history element.
1547 @end deffn
1548
1549 @deffn Command previous-matching-history-element pattern n
1550 This command replaces the minibuffer contents with the value of the
1551 @var{n}th previous (older) history element that matches @var{pattern} (a
1552 regular expression).
1553 @end deffn
1554
1555 @deffn Command next-matching-history-element pattern n
1556 This command replaces the minibuffer contents with the value of the
1557 @var{n}th next (newer) history element that matches @var{pattern} (a
1558 regular expression).
1559 @end deffn
1560
1561 @defun minibuffer-prompt
1562 This function returns the prompt string of the currently active
1563 minibuffer. If no minibuffer is active, it returns @code{nil}.
1564 @end defun
1565
1566 @defun minibuffer-prompt-end
1567 @tindex minibuffer-prompt-end
1568 This function, available starting in Emacs 21, returns the current
1569 position of the end of the minibuffer prompt, if a minibuffer is
1570 current. Otherwise, it returns the minimum valid buffer position.
1571 @end defun
1572
1573 @defun minibuffer-contents
1574 @tindex minibuffer-contents
1575 This function, available starting in Emacs 21, returns the editable
1576 contents of the minibuffer (that is, everything except the prompt) as
1577 a string, if a minibuffer is current. Otherwise, it returns the
1578 entire contents of the current buffer.
1579 @end defun
1580
1581 @defun minibuffer-contents-no-properties
1582 @tindex minibuffer-contents-no-properties
1583 This is like @code{minibuffer-contents}, except that it does not copy text
1584 properties, just the characters themselves. @xref{Text Properties}.
1585 @end defun
1586
1587 @defun delete-minibuffer-contents
1588 @tindex delete-minibuffer-contents
1589 This function, available starting in Emacs 21, erases the editable
1590 contents of the minibuffer (that is, everything except the prompt), if
1591 a minibuffer is current. Otherwise, it erases the entire buffer.
1592 @end defun
1593
1594 @defun minibuffer-prompt-width
1595 This function returns the current display-width of the minibuffer
1596 prompt, if a minibuffer is current. Otherwise, it returns zero.
1597 @end defun
1598
1599 @defvar minibuffer-setup-hook
1600 This is a normal hook that is run whenever the minibuffer is entered.
1601 @xref{Hooks}.
1602 @end defvar
1603
1604 @defvar minibuffer-exit-hook
1605 This is a normal hook that is run whenever the minibuffer is exited.
1606 @xref{Hooks}.
1607 @end defvar
1608
1609 @defvar minibuffer-help-form
1610 The current value of this variable is used to rebind @code{help-form}
1611 locally inside the minibuffer (@pxref{Help Functions}).
1612 @end defvar
1613
1614 @defun minibufferp &optional buffer
1615 This function returns non-@code{nil} if @var{buffer} is a minibuffer.
1616 If @var{buffer} is omitted, it tests the current buffer.
1617 @end defun
1618
1619 @defun active-minibuffer-window
1620 This function returns the currently active minibuffer window, or
1621 @code{nil} if none is currently active.
1622 @end defun
1623
1624 @defun minibuffer-window &optional frame
1625 This function returns the minibuffer window used for frame @var{frame}.
1626 If @var{frame} is @code{nil}, that stands for the current frame. Note
1627 that the minibuffer window used by a frame need not be part of that
1628 frame---a frame that has no minibuffer of its own necessarily uses some
1629 other frame's minibuffer window.
1630 @end defun
1631
1632 @c Emacs 19 feature
1633 @defun window-minibuffer-p window
1634 This function returns non-@code{nil} if @var{window} is a minibuffer window.
1635 @end defun
1636
1637 It is not correct to determine whether a given window is a minibuffer by
1638 comparing it with the result of @code{(minibuffer-window)}, because
1639 there can be more than one minibuffer window if there is more than one
1640 frame.
1641
1642 @defun minibuffer-window-active-p window
1643 This function returns non-@code{nil} if @var{window}, assumed to be
1644 a minibuffer window, is currently active.
1645 @end defun
1646
1647 @defvar minibuffer-scroll-window
1648 If the value of this variable is non-@code{nil}, it should be a window
1649 object. When the function @code{scroll-other-window} is called in the
1650 minibuffer, it scrolls this window.
1651 @end defvar
1652
1653 Finally, some functions and variables deal with recursive minibuffers
1654 (@pxref{Recursive Editing}):
1655
1656 @defun minibuffer-depth
1657 This function returns the current depth of activations of the
1658 minibuffer, a nonnegative integer. If no minibuffers are active, it
1659 returns zero.
1660 @end defun
1661
1662 @defopt enable-recursive-minibuffers
1663 If this variable is non-@code{nil}, you can invoke commands (such as
1664 @code{find-file}) that use minibuffers even while the minibuffer window
1665 is active. Such invocation produces a recursive editing level for a new
1666 minibuffer. The outer-level minibuffer is invisible while you are
1667 editing the inner one.
1668
1669 If this variable is @code{nil}, you cannot invoke minibuffer
1670 commands when the minibuffer window is active, not even if you switch to
1671 another window to do it.
1672 @end defopt
1673
1674 @c Emacs 19 feature
1675 If a command name has a property @code{enable-recursive-minibuffers}
1676 that is non-@code{nil}, then the command can use the minibuffer to read
1677 arguments even if it is invoked from the minibuffer. The minibuffer
1678 command @code{next-matching-history-element} (normally @kbd{M-s} in the
1679 minibuffer) uses this feature.
1680
1681 @defun minibuffer-message string &optional timeout
1682 This function displays @var{string} temporarily at the end of the
1683 minibuffer text, for @var{timeout} seconds. (The default is 2
1684 seconds.)
1685 @end defun