minibuf.texi 49.9 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 
@c See the file elisp.texi for copying conditions.
@setfilename ../info/minibuf
@node Minibuffers, Command Loop, Streams, Top
@chapter Minibuffers
@cindex arguments, reading
@cindex complex arguments
@cindex minibuffer

  A @dfn{minibuffer} is a special buffer that Emacs commands use to read
arguments more complicated than the single numeric prefix argument.
These arguments include file names, buffer names, and command names (as
in @kbd{M-x}).  The minibuffer is displayed on the bottom line of the
screen, in the same place as the echo area, but only while it is in
use for reading an argument.

@menu
* Intro to Minibuffers::      Basic information about minibuffers.
* Text from Minibuffer::      How to read a straight text string.
* Object from Minibuffer::    How to read a Lisp object or expression.
* Minibuffer History::	      Recording previous minibuffer inputs
				so the user can reuse them.
* Completion::                How to invoke and customize completion.
* Yes-or-No Queries::         Asking a question with a simple answer.
* Multiple Queries::	      Asking a series of similar questions.
* Minibuffer Misc::           Various customization hooks and variables.
@end menu

@node Intro to Minibuffers
@section Introduction to Minibuffers

  In most ways, a minibuffer is a normal Emacs buffer.  Most operations
@emph{within} a buffer, such as editing commands, work normally in a
minibuffer.  However, many operations for managing buffers do not apply
to minibuffers.  The name of a minibuffer always has the form @w{@samp{
*Minibuf-@var{number}}}, and it cannot be changed.  Minibuffers are
displayed only in special windows used only for minibuffers; these
windows always appear at the bottom of a frame.  (Sometime frames have
no minibuffer window, and sometimes a special kind of frame contains
nothing but a minibuffer window; see @ref{Minibuffers and Frames}.)

44
  The minibuffer's window is normally a single line.  You can resize it
Richard M. Stallman's avatar
Richard M. Stallman committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
temporarily with the window sizing commands; it reverts to its normal
size when the minibuffer is exited.  You can resize it permanently by
using the window sizing commands in the frame's other window, when the
minibuffer is not active.  If the frame contains just a minibuffer, you
can change the minibuffer's size by changing the frame's size.

  If a command uses a minibuffer while there is an active minibuffer,
this is called a @dfn{recursive minibuffer}.  The first minibuffer is
named @w{@samp{ *Minibuf-0*}}.  Recursive minibuffers are named by
incrementing the number at the end of the name.  (The names begin with a
space so that they won't show up in normal buffer lists.)  Of several
recursive minibuffers, the innermost (or most recently entered) is the
active minibuffer.  We usually call this ``the'' minibuffer.  You can
permit or forbid recursive minibuffers by setting the variable
@code{enable-recursive-minibuffers} or by putting properties of that
name on command symbols (@pxref{Minibuffer Misc}).

  Like other buffers, a minibuffer may use any of several local keymaps
(@pxref{Keymaps}); these contain various exit commands and in some cases
completion commands.  @xref{Completion}.

@itemize @bullet
@item
@code{minibuffer-local-map} is for ordinary input (no completion).

@item
@code{minibuffer-local-ns-map} is similar, except that @key{SPC} exits
just like @key{RET}.  This is used mainly for Mocklisp compatibility.

@item
@code{minibuffer-local-completion-map} is for permissive completion.

@item
@code{minibuffer-local-must-match-map} is for strict completion and
for cautious completion.
@end itemize

@node Text from Minibuffer
@section Reading Text Strings with the Minibuffer

85 86 87 88
  Most often, the minibuffer is used to read text as a string.  It can
also be used to read a Lisp object in textual form.  The most basic
primitive for minibuffer input is @code{read-from-minibuffer}; it can do
either one.
Richard M. Stallman's avatar
Richard M. Stallman committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

@defun read-from-minibuffer prompt-string &optional initial-contents keymap read hist
This function is the most general way to get input through the
minibuffer.  By default, it accepts arbitrary text and returns it as a
string; however, if @var{read} is non-@code{nil}, then it uses
@code{read} to convert the text into a Lisp object (@pxref{Input
Functions}).

The first thing this function does is to activate a minibuffer and 
display it with @var{prompt-string} as the prompt.  This value must be a
string.

Then, if @var{initial-contents} is a string, @code{read-from-minibuffer}
inserts it into the minibuffer, leaving point at the end.  The
minibuffer appears with this text as its contents.

@c Emacs 19 feature
The value of @var{initial-contents} may also be a cons cell of the form
@code{(@var{string} . @var{position})}.  This means to insert
@var{string} in the minibuffer but put point @var{position} characters
from the beginning, rather than at the end.

If @var{keymap} is non-@code{nil}, that keymap is the local keymap to
use in the minibuffer.  If @var{keymap} is omitted or @code{nil}, the
value of @code{minibuffer-local-map} is used as the keymap.  Specifying
a keymap is the most important way to customize the minibuffer for
various applications such as completion.

The argument @var{hist} specifies which history list variable to use
for saving the input and for history commands used in the minibuffer.
It defaults to @code{minibuffer-history}.  @xref{Minibuffer History}.

When the user types a command to exit the minibuffer,
@code{read-from-minibuffer} uses the text in the minibuffer to produce
its return value.  Normally it simply makes a string containing that
text.  However, if @var{read} is non-@code{nil},
@code{read-from-minibuffer} reads the text and returns the resulting
Lisp object, unevaluated.  (@xref{Input Functions}, for information
about reading.)
@end defun

@defun read-string prompt &optional initial
This function reads a string from the minibuffer and returns it.  The
arguments @var{prompt} and @var{initial} are used as in
@code{read-from-minibuffer}.  The keymap used is
@code{minibuffer-local-map}.

This is a simplified interface to the
@code{read-from-minibuffer} function:

@smallexample
@group
(read-string @var{prompt} @var{initial})
@equiv{}
143
(read-from-minibuffer @var{prompt} @var{initial} nil nil nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
@end group
@end smallexample
@end defun

@defvar minibuffer-local-map
This is the default local keymap for reading from the minibuffer.  By
default, it makes the following bindings:

@table @asis
@item @key{LFD}
@code{exit-minibuffer}

@item @key{RET}
@code{exit-minibuffer}

@item @kbd{C-g}
@code{abort-recursive-edit}

@item @kbd{M-n}
@code{next-history-element}

@item @kbd{M-p}
@code{previous-history-element}

@item @kbd{M-r}
@code{next-matching-history-element}

@item @kbd{M-s}
@code{previous-matching-history-element}
@end table
@end defvar

@c In version 18, initial is required
@c Emacs 19 feature
@defun read-no-blanks-input prompt &optional initial
This function reads a string from the minibuffer, but does not allow
whitespace characters as part of the input: instead, those characters
terminate the input.  The arguments @var{prompt} and @var{initial} are
used as in @code{read-from-minibuffer}.

This is a simplified interface to the @code{read-from-minibuffer}
function, and passes the value of the @code{minibuffer-local-ns-map}
keymap as the @var{keymap} argument for that function.  Since the keymap
@code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
possible to put a space into the string, by quoting it.

@smallexample
@group
(read-no-blanks-input @var{prompt} @var{initial})
@equiv{}
(read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map)
@end group
@end smallexample
@end defun

@defvar minibuffer-local-ns-map
This built-in variable is the keymap used as the minibuffer local keymap
in the function @code{read-no-blanks-input}.  By default, it makes the
following bindings:

@table @asis
@item @key{LFD}
@code{exit-minibuffer}

@item @key{SPC}
@cindex @key{SPC} in minibuffer
@code{exit-minibuffer}

@item @key{TAB}
@cindex @key{TAB} in minibuffer
@code{exit-minibuffer}

@item @key{RET}
@code{exit-minibuffer}

@item @kbd{C-g}
@code{abort-recursive-edit}

@item @kbd{?}
@cindex @kbd{?} in minibuffer
@code{self-insert-and-exit}

226 227 228 229 230
@item @kbd{M-n}
@code{next-history-element}

@item @kbd{M-p}
@code{previous-history-element}
Richard M. Stallman's avatar
Richard M. Stallman committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

@item @kbd{M-r}
@code{next-matching-history-element}

@item @kbd{M-s}
@code{previous-matching-history-element}
@end table
@end defvar

@node Object from Minibuffer
@section Reading Lisp Objects with the Minibuffer

  This section describes functions for reading Lisp objects with the
minibuffer.

@defun read-minibuffer prompt &optional initial
247
This function reads a Lisp object in the minibuffer and returns it,
Richard M. Stallman's avatar
Richard M. Stallman committed
248
without evaluating it.  The arguments @var{prompt} and @var{initial} are
249
used as in @code{read-from-minibuffer}.
Richard M. Stallman's avatar
Richard M. Stallman committed
250

251
This is a simplified interface to the
Richard M. Stallman's avatar
Richard M. Stallman committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
@code{read-from-minibuffer} function:

@smallexample
@group
(read-minibuffer @var{prompt} @var{initial})
@equiv{}
(read-from-minibuffer @var{prompt} @var{initial} nil t)
@end group
@end smallexample

Here is an example in which we supply the string @code{"(testing)"} as
initial input:

@smallexample
@group
(read-minibuffer
 "Enter an expression: " (format "%s" '(testing)))

;; @r{Here is how the minibuffer is displayed:}
@end group

@group
---------- Buffer: Minibuffer ----------
Enter an expression: (testing)@point{}
---------- Buffer: Minibuffer ----------
@end group
@end smallexample

@noindent
The user can type @key{RET} immediately to use the initial input as a
default, or can edit the input.
@end defun

@defun eval-minibuffer prompt &optional initial
286
This function reads a Lisp expression in the minibuffer, evaluates it,
Richard M. Stallman's avatar
Richard M. Stallman committed
287 288 289
then returns the result.  The arguments @var{prompt} and @var{initial}
are used as in @code{read-from-minibuffer}.

290
This function simply evaluates the result of a call to
Richard M. Stallman's avatar
Richard M. Stallman committed
291 292 293 294 295 296 297 298 299 300 301 302
@code{read-minibuffer}:

@smallexample
@group
(eval-minibuffer @var{prompt} @var{initial})
@equiv{}
(eval (read-minibuffer @var{prompt} @var{initial}))
@end group
@end smallexample
@end defun

@defun edit-and-eval-command prompt form
303
This function reads a Lisp expression in the minibuffer, and then
Richard M. Stallman's avatar
Richard M. Stallman committed
304 305 306 307 308 309 310
evaluates it.  The difference between this command and
@code{eval-minibuffer} is that here the initial @var{form} is not
optional and it is treated as a Lisp object to be converted to printed
representation rather than as a string of text.  It is printed with
@code{prin1}, so if it is a string, double-quote characters (@samp{"})
appear in the initial text.  @xref{Output Functions}.

311
The first thing @code{edit-and-eval-command} does is to activate the
Richard M. Stallman's avatar
Richard M. Stallman committed
312 313 314 315 316 317
minibuffer with @var{prompt} as the prompt.  Then it inserts the printed
representation of @var{form} in the minibuffer, and lets the user edit.
When the user exits the minibuffer, the edited text is read with
@code{read} and then evaluated.  The resulting value becomes the value
of @code{edit-and-eval-command}.

318
In the following example, we offer the user an expression with initial
Richard M. Stallman's avatar
Richard M. Stallman committed
319 320 321 322 323 324
text which is a valid form already:

@smallexample
@group
(edit-and-eval-command "Please edit: " '(forward-word 1))

325
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
;;   @r{the following appears in the minibuffer:}
@end group

@group
---------- Buffer: Minibuffer ----------
Please edit: (forward-word 1)@point{}
---------- Buffer: Minibuffer ----------
@end group
@end smallexample

@noindent
Typing @key{RET} right away would exit the minibuffer and evaluate the
expression, thus moving point forward one word.
@code{edit-and-eval-command} returns @code{nil} in this example.
@end defun

@node Minibuffer History
@section Minibuffer History
@cindex minibuffer history
@cindex history list

A @dfn{minibuffer history list} records previous minibuffer inputs so
348 349 350
the user can reuse them conveniently.  A history list is actually a
symbol, not a list; it is a variable whose value is a list of strings
(previous inputs), most recent first.
Richard M. Stallman's avatar
Richard M. Stallman committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

There are many separate history lists, used for different kinds of
inputs.  It's the Lisp programmer's job to specify the right history
list for each use of the minibuffer.

The basic minibuffer input functions @code{read-from-minibuffer} and
@code{completing-read} both accept an optional argument named @var{hist}
which is how you specify the history list.  Here are the possible
values:

@table @asis
@item @var{variable}
Use @var{variable} (a symbol) as the history list.

@item (@var{variable} . @var{startpos})
Use @var{variable} (a symbol) as the history list, and assume that the
initial history position is @var{startpos} (an integer, counting from
zero which specifies the most recent element of the history).

If you specify @var{startpos}, then you should also specify that element
of the history as the initial minibuffer contents, for consistency.
@end table

If you don't specify @var{hist}, then the default history list
@code{minibuffer-history} is used.  For other standard history lists,
see below.  You can also create your own history list variable; just
initialize it to @code{nil} before the first use.

Both @code{read-from-minibuffer} and @code{completing-read} add new
elements to the history list automatically, and provide commands to
allow the user to reuse items on the list.  The only thing your program
needs to do to use a history list is to initialize it and to pass its
name to the input functions when you wish.  But it is safe to modify the
list by hand when the minibuffer input functions are not using it.

@defvar minibuffer-history
The default history list for minibuffer history input.
@end defvar

@defvar query-replace-history
A history list for arguments to @code{query-replace} (and similar
arguments to other commands).
@end defvar

@defvar file-name-history
A history list for file name arguments.
@end defvar

@defvar regexp-history
A history list for regular expression arguments.
@end defvar

@defvar extended-command-history
A history list for arguments that are names of extended commands.
@end defvar

@defvar shell-command-history
A history list for arguments that are shell commands.
@end defvar

@defvar read-expression-history
A history list for arguments that are Lisp expressions to evaluate.
@end defvar

@node Completion
@section Completion
@cindex completion

  @dfn{Completion} is a feature that fills in the rest of a name
starting from an abbreviation for it.  Completion works by comparing the
user's input against a list of valid names and determining how much of
the name is determined uniquely by what the user has typed.  For
example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
type the first few letters of the name of the buffer to which you wish
to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
extends the name as far as it can.

  Standard Emacs commands offer completion for names of symbols, files,
buffers, and processes; with the functions in this section, you can
implement completion for other kinds of names.

  The @code{try-completion} function is the basic primitive for
completion: it returns the longest determined completion of a given
initial string, with a given set of strings to match against.

  The function @code{completing-read} provides a higher-level interface
for completion.  A call to @code{completing-read} specifies how to
determine the list of valid names.  The function then activates the
minibuffer with a local keymap that binds a few keys to commands useful
for completion.  Other functions provide convenient simple interfaces
for reading certain kinds of names with completion.

@menu
* Basic Completion::       Low-level functions for completing strings.
                             (These are too low level to use the minibuffer.)
* Minibuffer Completion::  Invoking the minibuffer with completion.
* Completion Commands::    Minibuffer commands that do completion.
* High-Level Completion::  Convenient special cases of completion
                             (reading buffer name, file name, etc.)
* Reading File Names::     Using completion to read file names.
* Programmed Completion::  Finding the completions for a given file name.
@end menu

@node Basic Completion
@subsection Basic Completion Functions

457 458 459 460 461
  The two functions @code{try-completion} and @code{all-completions}
have nothing in themselves to do with minibuffers.  We describe them in
this chapter so as to keep them near the higher-level completion
features that do use the minibuffer.

Richard M. Stallman's avatar
Richard M. Stallman committed
462 463 464
@defun try-completion string collection &optional predicate
This function returns the longest common substring of all possible
completions of @var{string} in @var{collection}.  The value of
465
@var{collection} must be an alist, an obarray, or a function that
Richard M. Stallman's avatar
Richard M. Stallman committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
implements a virtual set of strings (see below).

Completion compares @var{string} against each of the permissible
completions specified by @var{collection}; if the beginning of the
permissible completion equals @var{string}, it matches.  If no permissible
completions match, @code{try-completion} returns @code{nil}.  If only
one permissible completion matches, and the match is exact, then
@code{try-completion} returns @code{t}.  Otherwise, the value is the
longest initial sequence common to all the permissible completions that
match.

If @var{collection} is an alist (@pxref{Association Lists}), the
@sc{car}s of the alist elements form the set of permissible completions.

@cindex obarray in completion
If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
of all symbols in the obarray form the set of permissible completions.  The
global variable @code{obarray} holds an obarray containing the names of
all interned Lisp symbols.

Note that the only valid way to make a new obarray is to create it
empty and then add symbols to it one by one using @code{intern}.
Also, you cannot intern a given symbol in more than one obarray.

If the argument @var{predicate} is non-@code{nil}, then it must be a
function of one argument.  It is used to test each possible match, and
the match is accepted only if @var{predicate} returns non-@code{nil}.
The argument given to @var{predicate} is either a cons cell from the alist
(the @sc{car} of which is a string) or else it is a symbol (@emph{not} a
symbol name) from the obarray.

497 498
You can also use a symbol that is a function as @var{collection}.  Then
the function is solely responsible for performing completion;
Richard M. Stallman's avatar
Richard M. Stallman committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
@code{try-completion} returns whatever this function returns.  The
function is called with three arguments: @var{string}, @var{predicate}
and @code{nil}.  (The reason for the third argument is so that the same
function can be used in @code{all-completions} and do the appropriate
thing in either case.)  @xref{Programmed Completion}.

In the first of the following examples, the string @samp{foo} is
matched by three of the alist @sc{car}s.  All of the matches begin with
the characters @samp{fooba}, so that is the result.  In the second
example, there is only one possible match, and it is exact, so the value
is @code{t}.

@smallexample
@group
(try-completion 
 "foo"
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
     @result{} "fooba"
@end group

@group
(try-completion "foo" '(("barfoo" 2) ("foo" 3)))
     @result{} t
@end group
@end smallexample

In the following example, numerous symbols begin with the characters
@samp{forw}, and all of them begin with the word @samp{forward}.  In
most of the symbols, this is followed with a @samp{-}, but not in all,
so no more than @samp{forward} can be completed.

@smallexample
@group
(try-completion "forw" obarray)
     @result{} "forward"
@end group
@end smallexample

Finally, in the following example, only two of the three possible
matches pass the predicate @code{test} (the string @samp{foobaz} is
too short).  Both of those begin with the string @samp{foobar}.

@smallexample
@group
(defun test (s) 
  (> (length (car s)) 6))
     @result{} test
@end group
@group
(try-completion 
 "foo"
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)) 
551
 'test)
Richard M. Stallman's avatar
Richard M. Stallman committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
     @result{} "foobar"
@end group
@end smallexample
@end defun

@defun all-completions string collection &optional predicate 
This function returns a list of all possible completions of
@var{string}.  The parameters to this function are the same as to
@code{try-completion}.

If @var{collection} is a function, it is called with three arguments:
@var{string}, @var{predicate} and @code{t}; then @code{all-completions}
returns whatever the function returns.  @xref{Programmed Completion}.

Here is an example, using the function @code{test} shown in the
example for @code{try-completion}:

@smallexample
@group
(defun test (s) 
  (> (length (car s)) 6))
     @result{} test
@end group

@group
(all-completions  
 "foo"
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
580
 'test)
Richard M. Stallman's avatar
Richard M. Stallman committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
     @result{} ("foobar1" "foobar2")
@end group
@end smallexample
@end defun

@defvar completion-ignore-case
If the value of this variable is 
non-@code{nil}, Emacs does not consider case significant in completion.
@end defvar

@node Minibuffer Completion
@subsection Completion and the Minibuffer

  This section describes the basic interface for reading from the
minibuffer with completion.

@defun completing-read prompt collection &optional predicate require-match initial hist
This function reads a string in the minibuffer, assisting the user by
providing completion.  It activates the minibuffer with prompt
@var{prompt}, which must be a string.  If @var{initial} is
non-@code{nil}, @code{completing-read} inserts it into the minibuffer as
part of the input.  Then it allows the user to edit the input, providing
several commands to attempt completion.

The actual completion is done by passing @var{collection} and
@var{predicate} to the function @code{try-completion}.  This happens in
certain commands bound in the local keymaps used for completion.

If @var{require-match} is @code{t}, the usual minibuffer exit commands
won't exit unless the input completes to an element of @var{collection}.
If @var{require-match} is neither @code{nil} nor @code{t}, then the exit
commands won't exit unless the input typed is itself an element of
613 614
@var{collection}.  If @var{require-match} is @code{nil}, the exit
commands work regardless of the input in the minibuffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
615 616 617 618 619

The function @code{completing-read} works by calling
@code{read-minibuffer}.  It uses @code{minibuffer-local-completion-map}
as the keymap if @var{require-match} is @code{nil}, and uses
@code{minibuffer-local-must-match-map} if @var{require-match} is
620
non-@code{nil}.  @xref{Completion Commands}.
Richard M. Stallman's avatar
Richard M. Stallman committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640

The argument @var{hist} specifies which history list variable to use for
saving the input and for minibuffer history commands.  It defaults to
@code{minibuffer-history}.  @xref{Minibuffer History}.

Completion ignores case when comparing the input against the possible
matches, if the built-in variable @code{completion-ignore-case} is
non-@code{nil}.  @xref{Basic Completion}.

Here's an example of using @code{completing-read}:

@smallexample
@group
(completing-read
 "Complete a foo: "
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
 nil t "fo")
@end group

@group
641
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
642 643 644 645 646 647 648 649 650 651 652 653 654
;;   @r{the following appears in the minibuffer:}

---------- Buffer: Minibuffer ----------
Complete a foo: fo@point{}
---------- Buffer: Minibuffer ----------
@end group
@end smallexample

@noindent
If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
@code{completing-read} returns @code{barfoo}.

The @code{completing-read} function binds three variables to pass
655 656 657 658 659
information to the commands that actually do completion.  These
variables are @code{minibuffer-completion-table},
@code{minibuffer-completion-predicate} and
@code{minibuffer-completion-confirm}.  For more information about them,
see @ref{Completion Commands}.
Richard M. Stallman's avatar
Richard M. Stallman committed
660 661 662 663 664 665 666 667 668
@end defun

@node Completion Commands
@subsection Minibuffer Commands That Do Completion

  This section describes the keymaps, commands and user options used in
the minibuffer to do completion.

@defvar minibuffer-local-completion-map
669
@code{completing-read} uses this value as the local keymap when an
Richard M. Stallman's avatar
Richard M. Stallman committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
exact match of one of the completions is not required.  By default, this
keymap makes the following bindings:

@table @asis
@item @kbd{?}
@code{minibuffer-completion-help}

@item @key{SPC}
@code{minibuffer-complete-word}

@item @key{TAB}
@code{minibuffer-complete}
@end table

@noindent
685 686
with other characters bound as in @code{minibuffer-local-map}
(@pxref{Text from Minibuffer}).
Richard M. Stallman's avatar
Richard M. Stallman committed
687 688 689 690 691
@end defvar

@defvar minibuffer-local-must-match-map
@code{completing-read} uses this value as the local keymap when an
exact match of one of the completions is required.  Therefore, no keys
692
are bound to @code{exit-minibuffer}, the command that exits the
Richard M. Stallman's avatar
Richard M. Stallman committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
minibuffer unconditionally.  By default, this keymap makes the following
bindings:

@table @asis
@item @kbd{?}
@code{minibuffer-completion-help}

@item @key{SPC}
@code{minibuffer-complete-word}

@item @key{TAB}
@code{minibuffer-complete}

@item @key{LFD}
@code{minibuffer-complete-and-exit}

@item @key{RET}
@code{minibuffer-complete-and-exit}
@end table

@noindent
with other characters bound as in @code{minibuffer-local-map}.
@end defvar

@defvar minibuffer-completion-table
The value of this variable is the alist or obarray used for completion
in the minibuffer.  This is the global variable that contains what
@code{completing-read} passes to @code{try-completion}.  It is used by
minibuffer completion commands such as @code{minibuffer-complete-word}.
@end defvar

@defvar minibuffer-completion-predicate
This variable's value is the predicate that @code{completing-read}
passes to @code{try-completion}.  The variable is also used by the other
minibuffer completion functions.
@end defvar

@deffn Command minibuffer-complete-word
This function completes the minibuffer contents by at most a single
word.  Even if the minibuffer contents have only one completion,
@code{minibuffer-complete-word} does not add any characters beyond the
first character that is not a word constituent.  @xref{Syntax Tables}.
@end deffn

@deffn Command minibuffer-complete
This function completes the minibuffer contents as far as possible.
@end deffn

@deffn Command minibuffer-complete-and-exit
This function completes the minibuffer contents, and exits if
confirmation is not required, i.e., if
@code{minibuffer-completion-confirm} is non-@code{nil}.  If confirmation
745 746 747
@emph{is} required, it is given by repeating this command
immediately---the command is programmed to work without confirmation
when run twice in succession.
Richard M. Stallman's avatar
Richard M. Stallman committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
@end deffn

@defvar minibuffer-completion-confirm
When the value of this variable is non-@code{nil}, Emacs asks for
confirmation of a completion before exiting the minibuffer.  The
function @code{minibuffer-complete-and-exit} checks the value of this
variable before it exits.
@end defvar

@deffn Command minibuffer-completion-help
This function creates a list of the possible completions of the
current minibuffer contents.  It works by calling @code{all-completions}
using the value of the variable @code{minibuffer-completion-table} as
the @var{collection} argument, and the value of
@code{minibuffer-completion-predicate} as the @var{predicate} argument.
The list of completions is displayed as text in a buffer named
@samp{*Completions*}.
@end deffn

@defun display-completion-list completions
This function displays @var{completions} to the stream in
@code{standard-output}, usually a buffer.  (@xref{Streams}, for more
information about streams.)  The argument @var{completions} is normally
a list of completions just returned by @code{all-completions}, but it
does not have to be.  Each element may be a symbol or a string, either
of which is simply printed, or a list of two strings, which is printed
as if the strings were concatenated.

This function is called by @code{minibuffer-completion-help}.  The
most common way to use it is together with
@code{with-output-to-temp-buffer}, like this:

@example
(with-output-to-temp-buffer "*Completions*"
  (display-completion-list
    (all-completions (buffer-string) my-alist)))
@end example
@end defun

@defopt completion-auto-help
If this variable is non-@code{nil}, the completion commands
automatically display a list of possible completions whenever nothing
can be completed because the next character is not uniquely determined.
@end defopt

@node High-Level Completion
@subsection High-Level Completion  Functions

  This section describes the higher-level convenient functions for
reading certain sorts of names with completion.

@defun read-buffer prompt &optional default existing
This function reads the name of a buffer and returns it as a string.
The argument @var{default} is the default name to use, the value to
return if the user exits with an empty minibuffer.  If non-@code{nil},
it should be a string or a buffer.  It is mentioned in the prompt, but
is not inserted in the minibuffer as initial input.

If @var{existing} is non-@code{nil}, then the name specified must be
807 808 809 810 811
that of an existing buffer.  The usual commands to exit the minibuffer
do not exit if the text is not valid, and @key{RET} does completion to
attempt to find a valid name.  (However, @var{default} is not checked
for validity; it is returned, whatever it is, if the user exits with the
minibuffer empty.)
Richard M. Stallman's avatar
Richard M. Stallman committed
812 813 814 815 816 817 818 819 820

In the following example, the user enters @samp{minibuffer.t}, and
then types @key{RET}.  The argument @var{existing} is @code{t}, and the
only buffer name starting with the given input is
@samp{minibuffer.texi}, so that name is the value.

@example
(read-buffer "Buffer name? " "foo" t)
@group
821
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
;;   @r{the following prompt appears,}
;;   @r{with an empty minibuffer:}
@end group

@group
---------- Buffer: Minibuffer ----------
Buffer name? (default foo) @point{}
---------- Buffer: Minibuffer ----------
@end group

@group
;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
     @result{} "minibuffer.texi"
@end group
@end example
@end defun

@defun read-command prompt
This function reads the name of a command and returns it as a Lisp
symbol.  The argument @var{prompt} is used as in
@code{read-from-minibuffer}.  Recall that a command is anything for
which @code{commandp} returns @code{t}, and a command name is a symbol
for which @code{commandp} returns @code{t}.  @xref{Interactive Call}.

@example
(read-command "Command name? ")

@group
850
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
;;   @r{the following prompt appears with an empty minibuffer:}
@end group

@group
---------- Buffer: Minibuffer ---------- 
Command name?  
---------- Buffer: Minibuffer ----------
@end group
@end example

@noindent
If the user types @kbd{forward-c @key{RET}}, then this function returns
@code{forward-char}.

The @code{read-command} function is a simplified interface to the
function @code{completing-read}.  It uses the variable @code{obarray} so
as to complete in the set of extant Lisp symbols, and it uses the
@code{commandp} predicate so as to accept only command names:

@cindex @code{commandp} example
@example
@group
(read-command @var{prompt})
@equiv{}
(intern (completing-read @var{prompt} obarray 
                         'commandp t nil))
@end group
@end example
@end defun

@defun read-variable prompt
This function reads the name of a user variable and returns it as a
symbol.

@example
@group
(read-variable "Variable name? ")

889
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
;;   @r{the following prompt appears,} 
;;   @r{with an empty minibuffer:}
@end group

@group
---------- Buffer: Minibuffer ----------
Variable name? @point{}
---------- Buffer: Minibuffer ----------
@end group
@end example

@noindent
If the user then types @kbd{fill-p @key{RET}}, @code{read-variable}
returns @code{fill-prefix}.

This function is similar to @code{read-command}, but uses the
predicate @code{user-variable-p} instead of @code{commandp}:

@cindex @code{user-variable-p} example
@example
@group
(read-variable @var{prompt})
@equiv{}
(intern
 (completing-read @var{prompt} obarray
                  'user-variable-p t nil))
@end group
@end example
@end defun

@node Reading File Names
@subsection Reading File Names

  Here is another high-level completion function, designed for reading a
file name.  It provides special features including automatic insertion
of the default directory.

@defun read-file-name prompt &optional directory default existing initial
This function reads a file name in the minibuffer, prompting with
@var{prompt} and providing completion.  If @var{default} is
non-@code{nil}, then the function returns @var{default} if the user just
931 932
types @key{RET}.  @var{default} is not checked for validity; it is
returned, whatever it is, if the user exits with the minibuffer empty.
Richard M. Stallman's avatar
Richard M. Stallman committed
933

934 935 936 937 938 939 940
If @var{existing} is non-@code{nil}, then the user must specify the name
of an existing file; @key{RET} performs completion to make the name
valid if possible, and then refuses to exit if it is not valid.  If the
value of @var{existing} is neither @code{nil} nor @code{t}, then
@key{RET} also requires confirmation after completion.  If
@var{existing} is @code{nil}, then the name of a nonexistent file is
acceptable.
Richard M. Stallman's avatar
Richard M. Stallman committed
941 942

The argument @var{directory} specifies the directory to use for
943 944 945 946
completion of relative file names.  If @code{insert-default-directory}
is non-@code{nil}, @var{directory} is also inserted in the minibuffer as
initial input.  It defaults to the current buffer's value of
@code{default-directory}.
Richard M. Stallman's avatar
Richard M. Stallman committed
947 948 949

@c Emacs 19 feature
If you specify @var{initial}, that is an initial file name to insert in
950 951 952 953
the buffer (after with @var{directory}, if that is inserted).  In this
case, point goes at the beginning of @var{initial}.  The default for
@var{initial} is @code{nil}---don't insert any file name.  To see what
@var{initial} does, try the command @kbd{C-x C-v}.
Richard M. Stallman's avatar
Richard M. Stallman committed
954 955 956 957 958 959 960

Here is an example: 

@example
@group
(read-file-name "The file is ")

961
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
;;   @r{the following appears in the minibuffer:}
@end group

@group
---------- Buffer: Minibuffer ----------
The file is /gp/gnu/elisp/@point{}
---------- Buffer: Minibuffer ----------
@end group
@end example

@noindent
Typing @kbd{manual @key{TAB}} results in the following:

@example
@group
---------- Buffer: Minibuffer ----------
The file is /gp/gnu/elisp/manual.texi@point{}
---------- Buffer: Minibuffer ----------
@end group
@end example

@c Wordy to avoid overfull hbox in smallbook mode.
@noindent
If the user types @key{RET}, @code{read-file-name} returns the file name
as the string @code{"/gp/gnu/elisp/manual.texi"}.
@end defun

@defopt insert-default-directory
This variable is used by @code{read-file-name}.  Its value controls
whether @code{read-file-name} starts by placing the name of the default
directory in the minibuffer, plus the initial file name if any.  If the
value of this variable is @code{nil}, then @code{read-file-name} does
not place any initial input in the minibuffer.  In that case, the
default directory is still used for completion of relative file names,
but is not displayed.

For example:

@example
@group
;; @r{Here the minibuffer starts out with the default directory.}
(let ((insert-default-directory t))
  (read-file-name "The file is "))
@end group

@group
---------- Buffer: Minibuffer ----------
The file is ~lewis/manual/@point{}
---------- Buffer: Minibuffer ----------
@end group

@group
;; @r{Here the minibuffer is empty and only the prompt}
;;   @r{appears on its line.}
(let ((insert-default-directory nil))
  (read-file-name "The file is "))
@end group

@group
---------- Buffer: Minibuffer ----------
The file is @point{}
---------- Buffer: Minibuffer ----------
@end group
@end example
@end defopt

@node Programmed Completion
@subsection Programmed Completion
@cindex programmed completion

  Sometimes it is not possible to create an alist or an obarray
containing all the intended possible completions.  In such a case, you
can supply your own function to compute the completion of a given string.
This is called @dfn{programmed completion}.

  To use this feature, pass a symbol with a function definition as the
1038 1039 1040 1041
@var{collection} argument to @code{completing-read}.  The function
@code{completing-read} arranges to pass your completion function along
to @code{try-completion} and @code{all-completions}, which will then let
your function do all the work.
Richard M. Stallman's avatar
Richard M. Stallman committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

  The completion function should accept three arguments:

@itemize @bullet
@item
The string to be completed.

@item
The predicate function to filter possible matches, or @code{nil} if
none.  Your function should call the predicate for each possible match,
and ignore the possible match if the predicate returns @code{nil}.

@item
A flag specifying the type of operation.
@end itemize

  There are three flag values for three operations:

@itemize @bullet
@item
@code{nil} specifies @code{try-completion}.  The completion function
should return the completion of the specified string, or @code{t} if the
string is an exact match already, or @code{nil} if the string matches no
possibility.

@item
@code{t} specifies @code{all-completions}.  The completion function
should return a list of all possible completions of the specified
string.

@item
@code{lambda} specifies a test for an exact match.  The completion
function should return @code{t} if the specified string is an exact
match for some possibility; @code{nil} otherwise.
@end itemize

  It would be consistent and clean for completion functions to allow
1079
lambda expressions (lists tha are functions) as well as function
Richard M. Stallman's avatar
Richard M. Stallman committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
symbols as @var{collection}, but this is impossible.  Lists as
completion tables are already assigned another meaning---as alists.  It
would be unreliable to fail to handle an alist normally because it is
also a possible function.  So you must arrange for any function you wish
to use for completion to be encapsulated in a symbol.

  Emacs uses programmed completion when completing file names.
@xref{File Name Completion}.

@node Yes-or-No Queries
@section Yes-or-No Queries
@cindex asking the user questions
@cindex querying the user
@cindex yes-or-no questions

  This section describes functions used to ask the user a yes-or-no
question.  The function @code{y-or-n-p} can be answered with a single
character; it is useful for questions where an inadvertent wrong answer
will not have serious consequences.  @code{yes-or-no-p} is suitable for
more momentous questions, since it requires three or four characters to
answer.

  Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
@code{y-or-n-p} does not; but it seems best to describe them together.

@defun y-or-n-p prompt
1106
This function asks the user a question, expecting input in the echo
Richard M. Stallman's avatar
Richard M. Stallman committed
1107 1108 1109 1110 1111 1112 1113 1114
area.  It returns @code{t} if the user types @kbd{y}, @code{nil} if the
user types @kbd{n}.  This function also accepts @key{SPC} to mean yes
and @key{DEL} to mean no.  It accepts @kbd{C-]} to mean ``quit'', like
@kbd{C-g}, because the question might look like a minibuffer and for
that reason the user might try to use @kbd{C-]} to get out.  The answer
is a single character, with no @key{RET} needed to terminate it.  Upper
and lower case are equivalent.

1115
``Asking the question'' means printing @var{prompt} in the echo area,
Richard M. Stallman's avatar
Richard M. Stallman committed
1116 1117 1118 1119 1120
followed by the string @w{@samp{(y or n) }}.  If the input is not one of
the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
@kbd{@key{DEL}}, or something that quits), the function responds
@samp{Please answer y or n.}, and repeats the request.

1121
This function does not actually use the minibuffer, since it does not
Richard M. Stallman's avatar
Richard M. Stallman committed
1122 1123 1124 1125
allow editing of the answer.  It actually uses the echo area (@pxref{The
Echo Area}), which uses the same screen space as the minibuffer.  The
cursor moves to the echo area while the question is being asked.

1126
The answers and their meanings, even @samp{y} and @samp{n}, are not
Richard M. Stallman's avatar
Richard M. Stallman committed
1127 1128 1129
hardwired.  The keymap @code{query-replace-map} specifies them.
@xref{Search and Replace}.

1130
If @code{y-or-n-p} is called in a command that was invoked using the
Richard M. Stallman's avatar
Richard M. Stallman committed
1131 1132 1133 1134 1135
mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
Loop Info}) is either @code{nil} or a mouse event---then it uses a
dialog box or pop-up menu to ask the question.  In this case, it does
not use keyboard input or the echo area.

1136
In the following example, the user first types @kbd{q}, which is
Richard M. Stallman's avatar
Richard M. Stallman committed
1137 1138 1139 1140 1141 1142
invalid.  At the next prompt the user types @kbd{y}.

@smallexample
@group
(y-or-n-p "Do you need a lift? ")

1143
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
;;   @r{the following prompt appears in the echo area:}
@end group

@group
---------- Echo area ----------
Do you need a lift? (y or n) 
---------- Echo area ----------
@end group

;; @r{If the user then types @kbd{q}, the following appears:}

@group
---------- Echo area ----------
Please answer y or n.  Do you need a lift? (y or n) 
---------- Echo area ----------
@end group

;; @r{When the user types a valid answer,}
;;   @r{it is displayed after the question:}

@group
---------- Echo area ----------
Do you need a lift? (y or n) y
---------- Echo area ----------
@end group
@end smallexample

@noindent
We show successive lines of echo area messages, but only one actually
appears on the screen at a time.
@end defun

@defun yes-or-no-p prompt
1177 1178 1179 1180
This function asks the user a question, expecting input in the
minibuffer.  It returns @code{t} if the user enters @samp{yes},
@code{nil} if the user types @samp{no}.  The user must type @key{RET} to
finalize the response.  Upper and lower case are equivalent.
Richard M. Stallman's avatar
Richard M. Stallman committed
1181

1182
@code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
Richard M. Stallman's avatar
Richard M. Stallman committed
1183 1184 1185 1186
followed by @w{@samp{(yes or no) }}.  The user must type one of the
expected responses; otherwise, the function responds @samp{Please answer
yes or no.}, waits about two seconds and repeats the request.

1187
@code{yes-or-no-p} requires more work from the user than
Richard M. Stallman's avatar
Richard M. Stallman committed
1188 1189
@code{y-or-n-p} and is appropriate for more crucial decisions.

1190
If @code{yes-or-no-p} is called in a command that was invoked using
Richard M. Stallman's avatar
Richard M. Stallman committed
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
the mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
Loop Info}) is either @code{nil} or a mouse event---then it uses a
dialog box or pop-up menu to ask the question.  In this case, it does
not use keyboard input or the echo area.

Here is an example:

@smallexample
@group
(yes-or-no-p "Do you really want to remove everything? ")

1202
;; @r{After evaluation of the preceding expression,} 
Richard M. Stallman's avatar
Richard M. Stallman committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
;;   @r{the following prompt appears,} 
;;   @r{with an empty minibuffer:}
@end group

@group
---------- Buffer: minibuffer ----------
Do you really want to remove everything? (yes or no) 
---------- Buffer: minibuffer ----------
@end group
@end smallexample

@noindent
If the user first types @kbd{y @key{RET}}, which is invalid because this
function demands the entire word @samp{yes}, it responds by displaying
these prompts, with a brief pause between them:

@smallexample
@group
---------- Buffer: minibuffer ----------
Please answer yes or no.
Do you really want to remove everything? (yes or no)
---------- Buffer: minibuffer ----------
@end group
@end smallexample
@end defun

@node Multiple Queries
@section Asking Multiple Y-or-N Questions

1232 1233 1234 1235 1236 1237 1238
  When you have a series of similar questions to ask, such as ``Do you
want to save this buffer'' for each buffer in turn, you should use
@code{map-y-or-n-p} to ask the collection of questions, rather than
asking each question individually.  This gives the user certain
convenient facilities such as the ability to answer the whole series at
once.

Richard M. Stallman's avatar
Richard M. Stallman committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
@defun map-y-or-n-p prompter actor list &optional help action-alist
This function, new in Emacs 19, asks the user a series of questions,
reading a single-character answer in the echo area for each one.

The value of @var{list} specifies the objects to ask questions about.
It should be either a list of objects or a generator function.  If it is
a function, it should expect no arguments, and should return either the
next object to ask about, or @code{nil} meaning stop asking questions.

The argument @var{prompter} specifies how to ask each question.  If
@var{prompter} is a string, the question text is computed like this:

@example
(format @var{prompter} @var{object})
@end example

@noindent
where @var{object} is the next object to ask about (as obtained from
@var{list}).

If not a string, @var{prompter} should be a function of one argument
(the next object to ask about) and should return the question text.

The argument @var{actor} says how to act on the answers that the user
gives.  It should be a function of one argument, and it is called with
each object that the user says yes for.  Its argument is always an
object obtained from @var{list}.

If the argument @var{help} is given, it should be a list of this form:

@example
(@var{singular} @var{plural} @var{action})
@end example

@noindent
where @var{singular} is a string containing a singular noun that
describes the objects conceptually being acted on, @var{plural} is the
corresponding plural noun, and @var{action} is a transitive verb
describing what @var{actor} does.

If you don't specify @var{help}, the default is @code{("object"
"objects" "act on")}.

Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
@key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
that object; @kbd{!} to act on all following objects; @key{ESC} or
@kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
the current object and then exit; or @kbd{C-h} to get help.  These are
the same answers that @code{query-replace} accepts.  The keymap
@code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
as well as for @code{query-replace}; see @ref{Search and Replace}.

You can use @var{action-alist} to specify additional possible answers
and what they mean.  It is an alist of elements of the form
@code{(@var{char} @var{function} @var{help})}, each of which defines one
additional answer.  In this element, @var{char} is a character (the
answer); @var{function} is a function of one argument (an object from
@var{list}); @var{help} is a string.

When the user responds with @var{char}, @code{map-y-or-n-p} calls
@var{function}.  If it returns non-@code{nil}, the object is considered
``acted upon'', and @code{map-y-or-n-p} advances to the next object in
@var{list}.  If it returns @code{nil}, the prompt is repeated for the
same object.

The return value of @code{map-y-or-n-p} is the number of objects acted on.
@end defun

@node Minibuffer Misc
@comment  node-name,  next,  previous,  up
@section Minibuffer Miscellany

  This section describes some basic functions and variables related to
minibuffers.

@deffn Command exit-minibuffer
This command exits the active minibuffer.  It is normally bound to
keys in minibuffer local keymaps.
@end deffn

@deffn Command self-insert-and-exit
This command exits the active minibuffer after inserting the last
character typed on the keyboard (found in @code{last-command-char};
@pxref{Command Loop Info}).
@end deffn

@deffn Command previous-history-element n
This command replaces the minibuffer contents with the value of the
@var{n}th previous (older) history element.
@end deffn

@deffn Command next-history-element n
This command replaces the minibuffer contents with the value of the
@var{n}th more recent history element.
@end deffn

@deffn Command previous-matching-history-element pattern
This command replaces the minibuffer contents with the value of the
1337 1338
previous (older) history element that matches @var{pattern} (a regular
expression).
Richard M. Stallman's avatar
Richard M. Stallman committed
1339 1340 1341
@end deffn

@deffn Command next-matching-history-element pattern
1342 1343 1344
This command replaces the minibuffer contents with the value of the next
(newer) history element that matches @var{pattern} (a regular
expression).
Richard M. Stallman's avatar
Richard M. Stallman committed
1345 1346 1347 1348
@end deffn

@defvar minibuffer-setup-hook
This is a normal hook that is run whenever the minibuffer is entered.
1349 1350 1351 1352 1353 1354
@xref{Hooks}.
@end defvar

@defvar minibuffer-setup-hook
This is a normal hook that is run whenever the minibuffer is exited.
@xref{Hooks}.
Richard M. Stallman's avatar
Richard M. Stallman committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
@end defvar

@defvar minibuffer-help-form
The current value of this variable is used to rebind @code{help-form}
locally inside the minibuffer (@pxref{Help Functions}).
@end defvar

@defun minibuffer-window &optional frame
This function returns the window that is used for the minibuffer.  In
Emacs 18, there is one and only one minibuffer window; this window
always exists and cannot be deleted.  In Emacs 19, each frame can have
its own minibuffer, and this function returns the minibuffer window used
for frame @var{frame} (which defaults to the currently selected frame).
@end defun

@c Emacs 19 feature
@defun window-minibuffer-p window
This function returns non-@code{nil} if @var{window} is a minibuffer window.
@end defun

It is not correct to determine whether a given window is a minibuffer by
comparing it with the result of @code{(minibuffer-window)}, because
there can be more than one minibuffer window if there is more than one
frame.

@defun minibuffer-window-active-p window
This function returns non-@code{nil} if @var{window}, assumed to be
a minibuffer window, is currently active.
@end defun

@defvar minibuffer-scroll-window
If the value of this variable is non-@code{nil}, it should be a window
object.  When the function @code{scroll-other-window} is called in the
minibuffer, it scrolls this window.
@end defvar

Finally, some functions and variables deal with recursive minibuffers
(@pxref{Recursive Editing}):

@defun minibuffer-depth
This function returns the current depth of activations of the
minibuffer, a nonnegative integer.  If no minibuffers are active, it
returns zero.
@end defun

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
@defun minibuffer-prompt
This function returns the prompt string of the currently active
minibuffer.  If no minibuffer is active, it returns @code{nil}.
@end defun

@defun minibuffer-prompt-width
This function returns the display width of the prompt string of the
currently active minibuffer.  If no minibuffer is active, it returns 0.
@end defun

Richard M. Stallman's avatar
Richard M. Stallman committed
1410 1411
@defopt enable-recursive-minibuffers
If this variable is non-@code{nil}, you can invoke commands (such as
1412
@code{find-file}) that use minibuffers even while in the minibuffer
Richard M. Stallman's avatar
Richard M. Stallman committed
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
window.  Such invocation produces a recursive editing level for a new
minibuffer.  The outer-level minibuffer is invisible while you are
editing the inner one.

This variable only affects invoking the minibuffer while the
minibuffer window is selected.   If you switch windows while in the 
minibuffer, you can always invoke minibuffer commands while some other
window is selected.
@end defopt

@c Emacs 19 feature
If a command name has a property @code{enable-recursive-minibuffers}
1425
that is non-@code{nil}, then the command can use the minibuffer to read
Richard M. Stallman's avatar
Richard M. Stallman committed
1426 1427 1428
arguments even if it is invoked from the minibuffer.  The minibuffer
command @code{next-matching-history-element} (normally bound to
@kbd{M-s} in the minibuffer) uses this feature.