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