428
+ − 1 @c -*-texinfo-*-
+ − 2 @c This is part of the XEmacs Lisp Reference Manual.
444
+ − 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
428
+ − 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.
434
+ − 28 * Reading a Password:: Reading a password from the terminal.
428
+ − 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
434
+ − 41 windows always appear at the bottom of a frame. (Sometimes frames have
428
+ − 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
434
+ − 91 @defun read-from-minibuffer prompt-string &optional initial-contents keymap read hist abbrev-table default
428
+ − 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
444
+ − 98 The first thing this function does is to activate a minibuffer and
428
+ − 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
434
+ − 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
428
+ − 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
434
+ − 128 The argument @var{abbrev-table} specifies @code{local-abbrev-table} in
+ − 129 the minibuffer (@pxref{Standard Abbrev Tables}).
+ − 130
428
+ − 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.)
434
+ − 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.
428
+ − 151 @end defun
+ − 152
442
+ − 153 @defun read-string prompt &optional initial history default-value
428
+ − 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
444
+ − 159 The optional argument @var{history}, if non-@code{nil}, specifies a history
442
+ − 160 list and optionally the initial position in the list. The optional
444
+ − 161 argument @var{default-value} specifies a default value to return if the user
442
+ − 162 enters null input; it should be a string.
434
+ − 163
+ − 164 This function is a simplified interface to the
428
+ − 165 @code{read-from-minibuffer} function:
+ − 166
+ − 167 @smallexample
+ − 168 @group
442
+ − 169 (read-string @var{prompt} @var{initial} @var{history} @var{default})
428
+ − 170 @equiv{}
442
+ − 171 (read-from-minibuffer @var{prompt} @var{initial} nil nil
+ − 172 @var{history} nil @var{default})))
428
+ − 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
434
+ − 182 @item @kbd{C-j}
428
+ − 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
442
+ − 211 @defun read-expression prompt &optional initial history default-value
434
+ − 212 This function reads a Lisp object using the minibuffer, and returns it
428
+ − 213 without evaluating it. The arguments @var{prompt} and @var{initial} are
+ − 214 used as in @code{read-from-minibuffer}.
+ − 215
444
+ − 216 The optional argument @var{history}, if non-@code{nil}, specifies a history
442
+ − 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
428
+ − 221 This is a simplified interface to the
+ − 222 @code{read-from-minibuffer} function:
+ − 223
+ − 224 @smallexample
+ − 225 @group
442
+ − 226 (read-expression @var{prompt} @var{initial} @var{history} @var{default-value})
428
+ − 227 @equiv{}
442
+ − 228 (read-from-minibuffer @var{prompt} @var{initial} nil t
+ − 229 @var{history} nil @var{default-value})
428
+ − 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
442
+ − 238 (read-expression
428
+ − 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
442
+ − 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
434
+ − 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}.
428
+ − 266
444
+ − 267 The optional argument @var{history}, if non-@code{nil}, specifies a history
442
+ − 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
428
+ − 272 This function simply evaluates the result of a call to
442
+ − 273 @code{read-expression}:
428
+ − 274
+ − 275 @smallexample
+ − 276 @group
+ − 277 (eval-minibuffer @var{prompt} @var{initial})
+ − 278 @equiv{}
442
+ − 279 (eval (read-expression @var{prompt} @var{initial}))
428
+ − 280 @end group
+ − 281 @end smallexample
+ − 282 @end defun
+ − 283
444
+ − 284 @defun edit-and-eval-command prompt form &optional history
428
+ − 285 This function reads a Lisp expression in the minibuffer, and then
+ − 286 evaluates it. The difference between this command and
444
+ − 287 @code{eval-minibuffer} is that here the initial @var{form} is not
428
+ − 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
434
+ − 295 representation of @var{form} in the minibuffer, and lets the user edit it.
428
+ − 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
444
+ − 307 ;; @r{After evaluation of the preceding expression,}
428
+ − 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
434
+ − 368 Here are some of the standard minibuffer history list variables:
+ − 369
428
+ − 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
444
+ − 459 This function returns the longest common prefix of all possible
428
+ − 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
444
+ − 509 (try-completion
428
+ − 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
444
+ − 539 (defun test (s)
428
+ − 540 (> (length (car s)) 6))
+ − 541 @result{} test
+ − 542 @end group
+ − 543 @group
444
+ − 544 (try-completion
428
+ − 545 "foo"
444
+ − 546 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
428
+ − 547 'test)
+ − 548 @result{} "foobar"
+ − 549 @end group
+ − 550 @end smallexample
+ − 551 @end defun
+ − 552
444
+ − 553 @defun all-completions string collection &optional predicate
+ − 554 This function returns a list of all possible completions of @var{string}.
+ − 555 The arguments to this function are the same as those of @code{try-completion}.
428
+ − 556
+ − 557 If @var{collection} is a function, it is called with three arguments:
+ − 558 @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
+ − 559 returns whatever the function returns. @xref{Programmed Completion}.
+ − 560
+ − 561 Here is an example, using the function @code{test} shown in the
+ − 562 example for @code{try-completion}:
+ − 563
+ − 564 @smallexample
+ − 565 @group
444
+ − 566 (defun test (s)
428
+ − 567 (> (length (car s)) 6))
+ − 568 @result{} test
+ − 569 @end group
+ − 570
+ − 571 @group
444
+ − 572 (all-completions
428
+ − 573 "foo"
+ − 574 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
+ − 575 'test)
+ − 576 @result{} ("foobar1" "foobar2")
+ − 577 @end group
+ − 578 @end smallexample
+ − 579 @end defun
+ − 580
+ − 581 @defvar completion-ignore-case
444
+ − 582 If the value of this variable is
428
+ − 583 non-@code{nil}, XEmacs does not consider case significant in completion.
+ − 584 @end defvar
+ − 585
+ − 586 @node Minibuffer Completion
+ − 587 @subsection Completion and the Minibuffer
+ − 588
+ − 589 This section describes the basic interface for reading from the
+ − 590 minibuffer with completion.
+ − 591
434
+ − 592 @defun completing-read prompt collection &optional predicate require-match initial hist default
428
+ − 593 This function reads a string in the minibuffer, assisting the user by
+ − 594 providing completion. It activates the minibuffer with prompt
+ − 595 @var{prompt}, which must be a string. If @var{initial} is
+ − 596 non-@code{nil}, @code{completing-read} inserts it into the minibuffer as
+ − 597 part of the input. Then it allows the user to edit the input, providing
+ − 598 several commands to attempt completion.
+ − 599
+ − 600 The actual completion is done by passing @var{collection} and
+ − 601 @var{predicate} to the function @code{try-completion}. This happens in
+ − 602 certain commands bound in the local keymaps used for completion.
+ − 603
+ − 604 If @var{require-match} is @code{t}, the usual minibuffer exit commands
+ − 605 won't exit unless the input completes to an element of @var{collection}.
+ − 606 If @var{require-match} is neither @code{nil} nor @code{t}, then the exit
+ − 607 commands won't exit unless the input typed is itself an element of
+ − 608 @var{collection}. If @var{require-match} is @code{nil}, the exit
+ − 609 commands work regardless of the input in the minibuffer.
+ − 610
434
+ − 611 However, empty input is always permitted, regardless of the value of
+ − 612 @var{require-match}; in that case, @code{completing-read} returns
+ − 613 @var{default}. The value of @var{default} (if non-@code{nil}) is also
+ − 614 available to the user through the history commands.
+ − 615
428
+ − 616 The user can exit with null input by typing @key{RET} with an empty
434
+ − 617 minibuffer. Then @code{completing-read} returns @code{""}. This is how
+ − 618 the user requests whatever default the command uses for the value being
+ − 619 read. The user can return using @key{RET} in this way regardless of the
+ − 620 value of @var{require-match}, and regardless of whether the empty string
+ − 621 is included in @var{collection}.
428
+ − 622
+ − 623 The function @code{completing-read} works by calling
442
+ − 624 @code{read-expression}. It uses @code{minibuffer-local-completion-map}
428
+ − 625 as the keymap if @var{require-match} is @code{nil}, and uses
+ − 626 @code{minibuffer-local-must-match-map} if @var{require-match} is
+ − 627 non-@code{nil}. @xref{Completion Commands}.
+ − 628
+ − 629 The argument @var{hist} specifies which history list variable to use for
+ − 630 saving the input and for minibuffer history commands. It defaults to
+ − 631 @code{minibuffer-history}. @xref{Minibuffer History}.
+ − 632
+ − 633 Completion ignores case when comparing the input against the possible
+ − 634 matches, if the built-in variable @code{completion-ignore-case} is
+ − 635 non-@code{nil}. @xref{Basic Completion}.
+ − 636
+ − 637 Here's an example of using @code{completing-read}:
+ − 638
+ − 639 @smallexample
+ − 640 @group
+ − 641 (completing-read
+ − 642 "Complete a foo: "
+ − 643 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
+ − 644 nil t "fo")
+ − 645 @end group
+ − 646
+ − 647 @group
444
+ − 648 ;; @r{After evaluation of the preceding expression,}
428
+ − 649 ;; @r{the following appears in the minibuffer:}
+ − 650
+ − 651 ---------- Buffer: Minibuffer ----------
+ − 652 Complete a foo: fo@point{}
+ − 653 ---------- Buffer: Minibuffer ----------
+ − 654 @end group
+ − 655 @end smallexample
+ − 656
+ − 657 @noindent
+ − 658 If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
+ − 659 @code{completing-read} returns @code{barfoo}.
+ − 660
+ − 661 The @code{completing-read} function binds three variables to pass
+ − 662 information to the commands that actually do completion. These
+ − 663 variables are @code{minibuffer-completion-table},
+ − 664 @code{minibuffer-completion-predicate} and
+ − 665 @code{minibuffer-completion-confirm}. For more information about them,
+ − 666 see @ref{Completion Commands}.
+ − 667 @end defun
+ − 668
+ − 669 @node Completion Commands
+ − 670 @subsection Minibuffer Commands That Do Completion
+ − 671
+ − 672 This section describes the keymaps, commands and user options used in
+ − 673 the minibuffer to do completion.
+ − 674
+ − 675 @defvar minibuffer-local-completion-map
+ − 676 @code{completing-read} uses this value as the local keymap when an
+ − 677 exact match of one of the completions is not required. By default, this
+ − 678 keymap makes the following bindings:
+ − 679
+ − 680 @table @asis
+ − 681 @item @kbd{?}
+ − 682 @code{minibuffer-completion-help}
+ − 683
+ − 684 @item @key{SPC}
+ − 685 @code{minibuffer-complete-word}
+ − 686
+ − 687 @item @key{TAB}
+ − 688 @code{minibuffer-complete}
+ − 689 @end table
+ − 690
+ − 691 @noindent
+ − 692 with other characters bound as in @code{minibuffer-local-map}
+ − 693 (@pxref{Text from Minibuffer}).
+ − 694 @end defvar
+ − 695
+ − 696 @defvar minibuffer-local-must-match-map
+ − 697 @code{completing-read} uses this value as the local keymap when an
+ − 698 exact match of one of the completions is required. Therefore, no keys
+ − 699 are bound to @code{exit-minibuffer}, the command that exits the
+ − 700 minibuffer unconditionally. By default, this keymap makes the following
+ − 701 bindings:
+ − 702
+ − 703 @table @asis
+ − 704 @item @kbd{?}
+ − 705 @code{minibuffer-completion-help}
+ − 706
+ − 707 @item @key{SPC}
+ − 708 @code{minibuffer-complete-word}
+ − 709
+ − 710 @item @key{TAB}
+ − 711 @code{minibuffer-complete}
+ − 712
434
+ − 713 @item @kbd{C-j}
428
+ − 714 @code{minibuffer-complete-and-exit}
+ − 715
+ − 716 @item @key{RET}
+ − 717 @code{minibuffer-complete-and-exit}
+ − 718 @end table
+ − 719
+ − 720 @noindent
+ − 721 with other characters bound as in @code{minibuffer-local-map}.
+ − 722 @end defvar
+ − 723
+ − 724 @defvar minibuffer-completion-table
+ − 725 The value of this variable is the alist or obarray used for completion
+ − 726 in the minibuffer. This is the global variable that contains what
+ − 727 @code{completing-read} passes to @code{try-completion}. It is used by
+ − 728 minibuffer completion commands such as @code{minibuffer-complete-word}.
+ − 729 @end defvar
+ − 730
+ − 731 @defvar minibuffer-completion-predicate
+ − 732 This variable's value is the predicate that @code{completing-read}
+ − 733 passes to @code{try-completion}. The variable is also used by the other
+ − 734 minibuffer completion functions.
+ − 735 @end defvar
+ − 736
+ − 737 @deffn Command minibuffer-complete-word
+ − 738 This function completes the minibuffer contents by at most a single
+ − 739 word. Even if the minibuffer contents have only one completion,
+ − 740 @code{minibuffer-complete-word} does not add any characters beyond the
+ − 741 first character that is not a word constituent. @xref{Syntax Tables}.
+ − 742 @end deffn
+ − 743
+ − 744 @deffn Command minibuffer-complete
+ − 745 This function completes the minibuffer contents as far as possible.
+ − 746 @end deffn
+ − 747
+ − 748 @deffn Command minibuffer-complete-and-exit
+ − 749 This function completes the minibuffer contents, and exits if
+ − 750 confirmation is not required, i.e., if
434
+ − 751 @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
428
+ − 752 @emph{is} required, it is given by repeating this command
+ − 753 immediately---the command is programmed to work without confirmation
+ − 754 when run twice in succession.
+ − 755 @end deffn
+ − 756
+ − 757 @defvar minibuffer-completion-confirm
+ − 758 When the value of this variable is non-@code{nil}, XEmacs asks for
+ − 759 confirmation of a completion before exiting the minibuffer. The
+ − 760 function @code{minibuffer-complete-and-exit} checks the value of this
+ − 761 variable before it exits.
+ − 762 @end defvar
+ − 763
+ − 764 @deffn Command minibuffer-completion-help
+ − 765 This function creates a list of the possible completions of the
+ − 766 current minibuffer contents. It works by calling @code{all-completions}
+ − 767 using the value of the variable @code{minibuffer-completion-table} as
+ − 768 the @var{collection} argument, and the value of
+ − 769 @code{minibuffer-completion-predicate} as the @var{predicate} argument.
+ − 770 The list of completions is displayed as text in a buffer named
+ − 771 @samp{*Completions*}.
+ − 772 @end deffn
+ − 773
442
+ − 774 @defun display-completion-list completions &rest cl-keys
428
+ − 775 This function displays @var{completions} to the stream in
+ − 776 @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
+ − 777 information about streams.) The argument @var{completions} is normally
+ − 778 a list of completions just returned by @code{all-completions}, but it
+ − 779 does not have to be. Each element may be a symbol or a string, either
+ − 780 of which is simply printed, or a list of two strings, which is printed
+ − 781 as if the strings were concatenated.
+ − 782
+ − 783 This function is called by @code{minibuffer-completion-help}. The
+ − 784 most common way to use it is together with
+ − 785 @code{with-output-to-temp-buffer}, like this:
+ − 786
+ − 787 @example
+ − 788 (with-output-to-temp-buffer "*Completions*"
+ − 789 (display-completion-list
+ − 790 (all-completions (buffer-string) my-alist)))
+ − 791 @end example
+ − 792 @end defun
+ − 793
+ − 794 @defopt completion-auto-help
+ − 795 If this variable is non-@code{nil}, the completion commands
+ − 796 automatically display a list of possible completions whenever nothing
+ − 797 can be completed because the next character is not uniquely determined.
+ − 798 @end defopt
+ − 799
+ − 800 @node High-Level Completion
+ − 801 @subsection High-Level Completion Functions
+ − 802
+ − 803 This section describes the higher-level convenient functions for
+ − 804 reading certain sorts of names with completion.
+ − 805
+ − 806 In most cases, you should not call these functions in the middle of a
+ − 807 Lisp function. When possible, do all minibuffer input as part of
+ − 808 reading the arguments for a command, in the @code{interactive} spec.
+ − 809 @xref{Defining Commands}.
+ − 810
+ − 811 @defun read-buffer prompt &optional default existing
+ − 812 This function reads the name of a buffer and returns it as a string.
+ − 813 The argument @var{default} is the default name to use, the value to
+ − 814 return if the user exits with an empty minibuffer. If non-@code{nil},
+ − 815 it should be a string or a buffer. It is mentioned in the prompt, but
+ − 816 is not inserted in the minibuffer as initial input.
+ − 817
+ − 818 If @var{existing} is non-@code{nil}, then the name specified must be
+ − 819 that of an existing buffer. The usual commands to exit the minibuffer
+ − 820 do not exit if the text is not valid, and @key{RET} does completion to
+ − 821 attempt to find a valid name. (However, @var{default} is not checked
+ − 822 for validity; it is returned, whatever it is, if the user exits with the
+ − 823 minibuffer empty.)
+ − 824
+ − 825 In the following example, the user enters @samp{minibuffer.t}, and
+ − 826 then types @key{RET}. The argument @var{existing} is @code{t}, and the
+ − 827 only buffer name starting with the given input is
+ − 828 @samp{minibuffer.texi}, so that name is the value.
+ − 829
+ − 830 @example
+ − 831 (read-buffer "Buffer name? " "foo" t)
+ − 832 @group
444
+ − 833 ;; @r{After evaluation of the preceding expression,}
428
+ − 834 ;; @r{the following prompt appears,}
+ − 835 ;; @r{with an empty minibuffer:}
+ − 836 @end group
+ − 837
+ − 838 @group
+ − 839 ---------- Buffer: Minibuffer ----------
+ − 840 Buffer name? (default foo) @point{}
+ − 841 ---------- Buffer: Minibuffer ----------
+ − 842 @end group
+ − 843
+ − 844 @group
+ − 845 ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
+ − 846 @result{} "minibuffer.texi"
+ − 847 @end group
+ − 848 @end example
+ − 849 @end defun
+ − 850
442
+ − 851 @defun read-command prompt &optional default-value
428
+ − 852 This function reads the name of a command and returns it as a Lisp
+ − 853 symbol. The argument @var{prompt} is used as in
+ − 854 @code{read-from-minibuffer}. Recall that a command is anything for
+ − 855 which @code{commandp} returns @code{t}, and a command name is a symbol
+ − 856 for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
+ − 857
442
+ − 858 The argument @var{default-value} specifies what to return if the user
+ − 859 enters null input. It can be a symbol or a string; if it is a string,
+ − 860 @code{read-command} interns it before returning it. If @var{default} is
+ − 861 @code{nil}, that means no default has been specified; then if the user
+ − 862 enters null input, the return value is @code{nil}.
+ − 863
428
+ − 864 @example
+ − 865 (read-command "Command name? ")
+ − 866
+ − 867 @group
444
+ − 868 ;; @r{After evaluation of the preceding expression,}
428
+ − 869 ;; @r{the following prompt appears with an empty minibuffer:}
+ − 870 @end group
+ − 871
+ − 872 @group
444
+ − 873 ---------- Buffer: Minibuffer ----------
+ − 874 Command name?
428
+ − 875 ---------- Buffer: Minibuffer ----------
+ − 876 @end group
+ − 877 @end example
+ − 878
+ − 879 @noindent
+ − 880 If the user types @kbd{forward-c @key{RET}}, then this function returns
+ − 881 @code{forward-char}.
+ − 882
+ − 883 The @code{read-command} function is a simplified interface to the
+ − 884 function @code{completing-read}. It uses the variable @code{obarray} so
+ − 885 as to complete in the set of extant Lisp symbols, and it uses the
+ − 886 @code{commandp} predicate so as to accept only command names:
+ − 887
+ − 888 @cindex @code{commandp} example
+ − 889 @example
+ − 890 @group
+ − 891 (read-command @var{prompt})
+ − 892 @equiv{}
444
+ − 893 (intern (completing-read @var{prompt} obarray
428
+ − 894 'commandp t nil))
+ − 895 @end group
+ − 896 @end example
+ − 897 @end defun
+ − 898
442
+ − 899 @defun read-variable prompt &optional default-value
428
+ − 900 This function reads the name of a user variable and returns it as a
+ − 901 symbol.
+ − 902
442
+ − 903 The argument @var{default-value} specifies what to return if the user
+ − 904 enters null input. It can be a symbol or a string; if it is a string,
444
+ − 905 @code{read-variable} interns it before returning it. If @var{default-value}
442
+ − 906 is @code{nil}, that means no default has been specified; then if the
+ − 907 user enters null input, the return value is @code{nil}.
+ − 908
428
+ − 909 @example
+ − 910 @group
+ − 911 (read-variable "Variable name? ")
+ − 912
444
+ − 913 ;; @r{After evaluation of the preceding expression,}
+ − 914 ;; @r{the following prompt appears,}
428
+ − 915 ;; @r{with an empty minibuffer:}
+ − 916 @end group
+ − 917
+ − 918 @group
+ − 919 ---------- Buffer: Minibuffer ----------
+ − 920 Variable name? @point{}
+ − 921 ---------- Buffer: Minibuffer ----------
+ − 922 @end group
+ − 923 @end example
+ − 924
+ − 925 @noindent
+ − 926 If the user then types @kbd{fill-p @key{RET}}, @code{read-variable}
+ − 927 returns @code{fill-prefix}.
+ − 928
+ − 929 This function is similar to @code{read-command}, but uses the
+ − 930 predicate @code{user-variable-p} instead of @code{commandp}:
+ − 931
+ − 932 @cindex @code{user-variable-p} example
+ − 933 @example
+ − 934 @group
+ − 935 (read-variable @var{prompt})
+ − 936 @equiv{}
+ − 937 (intern
+ − 938 (completing-read @var{prompt} obarray
+ − 939 'user-variable-p t nil))
+ − 940 @end group
+ − 941 @end example
+ − 942 @end defun
+ − 943
+ − 944 @node Reading File Names
+ − 945 @subsection Reading File Names
+ − 946
+ − 947 Here is another high-level completion function, designed for reading a
+ − 948 file name. It provides special features including automatic insertion
+ − 949 of the default directory.
+ − 950
442
+ − 951 @defun read-file-name prompt &optional directory default existing initial history
428
+ − 952 This function reads a file name in the minibuffer, prompting with
+ − 953 @var{prompt} and providing completion. If @var{default} is
+ − 954 non-@code{nil}, then the function returns @var{default} if the user just
+ − 955 types @key{RET}. @var{default} is not checked for validity; it is
+ − 956 returned, whatever it is, if the user exits with the minibuffer empty.
+ − 957
+ − 958 If @var{existing} is non-@code{nil}, then the user must specify the name
+ − 959 of an existing file; @key{RET} performs completion to make the name
+ − 960 valid if possible, and then refuses to exit if it is not valid. If the
+ − 961 value of @var{existing} is neither @code{nil} nor @code{t}, then
+ − 962 @key{RET} also requires confirmation after completion. If
+ − 963 @var{existing} is @code{nil}, then the name of a nonexistent file is
+ − 964 acceptable.
+ − 965
+ − 966 The argument @var{directory} specifies the directory to use for
+ − 967 completion of relative file names. If @code{insert-default-directory}
+ − 968 is non-@code{nil}, @var{directory} is also inserted in the minibuffer as
+ − 969 initial input. It defaults to the current buffer's value of
+ − 970 @code{default-directory}.
+ − 971
+ − 972 @c Emacs 19 feature
+ − 973 If you specify @var{initial}, that is an initial file name to insert in
434
+ − 974 the buffer (after @var{directory}, if that is inserted). In this
428
+ − 975 case, point goes at the beginning of @var{initial}. The default for
+ − 976 @var{initial} is @code{nil}---don't insert any file name. To see what
+ − 977 @var{initial} does, try the command @kbd{C-x C-v}.
+ − 978
444
+ − 979 Here is an example:
428
+ − 980
+ − 981 @example
+ − 982 @group
+ − 983 (read-file-name "The file is ")
+ − 984
444
+ − 985 ;; @r{After evaluation of the preceding expression,}
428
+ − 986 ;; @r{the following appears in the minibuffer:}
+ − 987 @end group
+ − 988
+ − 989 @group
+ − 990 ---------- Buffer: Minibuffer ----------
+ − 991 The file is /gp/gnu/elisp/@point{}
+ − 992 ---------- Buffer: Minibuffer ----------
+ − 993 @end group
+ − 994 @end example
+ − 995
+ − 996 @noindent
+ − 997 Typing @kbd{manual @key{TAB}} results in the following:
+ − 998
+ − 999 @example
+ − 1000 @group
+ − 1001 ---------- Buffer: Minibuffer ----------
+ − 1002 The file is /gp/gnu/elisp/manual.texi@point{}
+ − 1003 ---------- Buffer: Minibuffer ----------
+ − 1004 @end group
+ − 1005 @end example
+ − 1006
+ − 1007 @c Wordy to avoid overfull hbox in smallbook mode.
+ − 1008 @noindent
+ − 1009 If the user types @key{RET}, @code{read-file-name} returns the file name
+ − 1010 as the string @code{"/gp/gnu/elisp/manual.texi"}.
+ − 1011 @end defun
+ − 1012
+ − 1013 @defopt insert-default-directory
+ − 1014 This variable is used by @code{read-file-name}. Its value controls
+ − 1015 whether @code{read-file-name} starts by placing the name of the default
+ − 1016 directory in the minibuffer, plus the initial file name if any. If the
+ − 1017 value of this variable is @code{nil}, then @code{read-file-name} does
+ − 1018 not place any initial input in the minibuffer (unless you specify
+ − 1019 initial input with the @var{initial} argument). In that case, the
+ − 1020 default directory is still used for completion of relative file names,
+ − 1021 but is not displayed.
+ − 1022
+ − 1023 For example:
+ − 1024
+ − 1025 @example
+ − 1026 @group
+ − 1027 ;; @r{Here the minibuffer starts out with the default directory.}
+ − 1028 (let ((insert-default-directory t))
+ − 1029 (read-file-name "The file is "))
+ − 1030 @end group
+ − 1031
+ − 1032 @group
+ − 1033 ---------- Buffer: Minibuffer ----------
+ − 1034 The file is ~lewis/manual/@point{}
+ − 1035 ---------- Buffer: Minibuffer ----------
+ − 1036 @end group
+ − 1037
+ − 1038 @group
+ − 1039 ;; @r{Here the minibuffer is empty and only the prompt}
+ − 1040 ;; @r{appears on its line.}
+ − 1041 (let ((insert-default-directory nil))
+ − 1042 (read-file-name "The file is "))
+ − 1043 @end group
+ − 1044
+ − 1045 @group
+ − 1046 ---------- Buffer: Minibuffer ----------
+ − 1047 The file is @point{}
+ − 1048 ---------- Buffer: Minibuffer ----------
+ − 1049 @end group
+ − 1050 @end example
+ − 1051 @end defopt
+ − 1052
+ − 1053 @node Programmed Completion
+ − 1054 @subsection Programmed Completion
+ − 1055 @cindex programmed completion
+ − 1056
+ − 1057 Sometimes it is not possible to create an alist or an obarray
+ − 1058 containing all the intended possible completions. In such a case, you
+ − 1059 can supply your own function to compute the completion of a given string.
+ − 1060 This is called @dfn{programmed completion}.
+ − 1061
+ − 1062 To use this feature, pass a symbol with a function definition as the
+ − 1063 @var{collection} argument to @code{completing-read}. The function
+ − 1064 @code{completing-read} arranges to pass your completion function along
+ − 1065 to @code{try-completion} and @code{all-completions}, which will then let
+ − 1066 your function do all the work.
+ − 1067
+ − 1068 The completion function should accept three arguments:
+ − 1069
+ − 1070 @itemize @bullet
+ − 1071 @item
+ − 1072 The string to be completed.
+ − 1073
+ − 1074 @item
+ − 1075 The predicate function to filter possible matches, or @code{nil} if
+ − 1076 none. Your function should call the predicate for each possible match,
+ − 1077 and ignore the possible match if the predicate returns @code{nil}.
+ − 1078
+ − 1079 @item
+ − 1080 A flag specifying the type of operation.
+ − 1081 @end itemize
+ − 1082
+ − 1083 There are three flag values for three operations:
+ − 1084
+ − 1085 @itemize @bullet
+ − 1086 @item
+ − 1087 @code{nil} specifies @code{try-completion}. The completion function
+ − 1088 should return the completion of the specified string, or @code{t} if the
434
+ − 1089 string is a unique and exact match already, or @code{nil} if the string
+ − 1090 matches no possibility.
+ − 1091
+ − 1092 If the string is an exact match for one possibility, but also matches
+ − 1093 other longer possibilities, the function should return the string, not
+ − 1094 @code{t}.
428
+ − 1095
+ − 1096 @item
+ − 1097 @code{t} specifies @code{all-completions}. The completion function
+ − 1098 should return a list of all possible completions of the specified
+ − 1099 string.
+ − 1100
+ − 1101 @item
+ − 1102 @code{lambda} specifies a test for an exact match. The completion
+ − 1103 function should return @code{t} if the specified string is an exact
+ − 1104 match for some possibility; @code{nil} otherwise.
+ − 1105 @end itemize
+ − 1106
+ − 1107 It would be consistent and clean for completion functions to allow
+ − 1108 lambda expressions (lists that are functions) as well as function
+ − 1109 symbols as @var{collection}, but this is impossible. Lists as
+ − 1110 completion tables are already assigned another meaning---as alists. It
+ − 1111 would be unreliable to fail to handle an alist normally because it is
+ − 1112 also a possible function. So you must arrange for any function you wish
+ − 1113 to use for completion to be encapsulated in a symbol.
+ − 1114
+ − 1115 Emacs uses programmed completion when completing file names.
+ − 1116 @xref{File Name Completion}.
+ − 1117
+ − 1118 @node Yes-or-No Queries
+ − 1119 @section Yes-or-No Queries
+ − 1120 @cindex asking the user questions
+ − 1121 @cindex querying the user
+ − 1122 @cindex yes-or-no questions
+ − 1123
+ − 1124 This section describes functions used to ask the user a yes-or-no
+ − 1125 question. The function @code{y-or-n-p} can be answered with a single
+ − 1126 character; it is useful for questions where an inadvertent wrong answer
+ − 1127 will not have serious consequences. @code{yes-or-no-p} is suitable for
+ − 1128 more momentous questions, since it requires three or four characters to
+ − 1129 answer. Variations of these functions can be used to ask a yes-or-no
+ − 1130 question using a dialog box, or optionally using one.
+ − 1131
+ − 1132 If either of these functions is called in a command that was invoked
+ − 1133 using the mouse, then it uses a dialog box or pop-up menu to ask the
+ − 1134 question. Otherwise, it uses keyboard input.
+ − 1135
+ − 1136 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
+ − 1137 @code{y-or-n-p} does not; but it seems best to describe them together.
+ − 1138
+ − 1139 @defun y-or-n-p prompt
+ − 1140 This function asks the user a question, expecting input in the echo
+ − 1141 area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
+ − 1142 user types @kbd{n}. This function also accepts @key{SPC} to mean yes
+ − 1143 and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit'', like
+ − 1144 @kbd{C-g}, because the question might look like a minibuffer and for
+ − 1145 that reason the user might try to use @kbd{C-]} to get out. The answer
+ − 1146 is a single character, with no @key{RET} needed to terminate it. Upper
+ − 1147 and lower case are equivalent.
+ − 1148
+ − 1149 ``Asking the question'' means printing @var{prompt} in the echo area,
+ − 1150 followed by the string @w{@samp{(y or n) }}. If the input is not one of
+ − 1151 the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
+ − 1152 @kbd{@key{DEL}}, or something that quits), the function responds
+ − 1153 @samp{Please answer y or n.}, and repeats the request.
+ − 1154
+ − 1155 This function does not actually use the minibuffer, since it does not
+ − 1156 allow editing of the answer. It actually uses the echo area (@pxref{The
+ − 1157 Echo Area}), which uses the same screen space as the minibuffer. The
+ − 1158 cursor moves to the echo area while the question is being asked.
+ − 1159
+ − 1160 The answers and their meanings, even @samp{y} and @samp{n}, are not
+ − 1161 hardwired. The keymap @code{query-replace-map} specifies them.
+ − 1162 @xref{Search and Replace}.
+ − 1163
+ − 1164 In the following example, the user first types @kbd{q}, which is
+ − 1165 invalid. At the next prompt the user types @kbd{y}.
+ − 1166
+ − 1167 @smallexample
+ − 1168 @group
+ − 1169 (y-or-n-p "Do you need a lift? ")
+ − 1170
444
+ − 1171 ;; @r{After evaluation of the preceding expression,}
428
+ − 1172 ;; @r{the following prompt appears in the echo area:}
+ − 1173 @end group
+ − 1174
+ − 1175 @group
+ − 1176 ---------- Echo area ----------
444
+ − 1177 Do you need a lift? (y or n)
428
+ − 1178 ---------- Echo area ----------
+ − 1179 @end group
+ − 1180
+ − 1181 ;; @r{If the user then types @kbd{q}, the following appears:}
+ − 1182
+ − 1183 @group
+ − 1184 ---------- Echo area ----------
444
+ − 1185 Please answer y or n. Do you need a lift? (y or n)
428
+ − 1186 ---------- Echo area ----------
+ − 1187 @end group
+ − 1188
+ − 1189 ;; @r{When the user types a valid answer,}
+ − 1190 ;; @r{it is displayed after the question:}
+ − 1191
+ − 1192 @group
+ − 1193 ---------- Echo area ----------
+ − 1194 Do you need a lift? (y or n) y
+ − 1195 ---------- Echo area ----------
+ − 1196 @end group
+ − 1197 @end smallexample
+ − 1198
+ − 1199 @noindent
+ − 1200 We show successive lines of echo area messages, but only one actually
+ − 1201 appears on the screen at a time.
+ − 1202 @end defun
+ − 1203
+ − 1204 @defun yes-or-no-p prompt
+ − 1205 This function asks the user a question, expecting input in the
+ − 1206 minibuffer. It returns @code{t} if the user enters @samp{yes},
+ − 1207 @code{nil} if the user types @samp{no}. The user must type @key{RET} to
+ − 1208 finalize the response. Upper and lower case are equivalent.
+ − 1209
+ − 1210 @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
+ − 1211 followed by @w{@samp{(yes or no) }}. The user must type one of the
+ − 1212 expected responses; otherwise, the function responds @samp{Please answer
+ − 1213 yes or no.}, waits about two seconds and repeats the request.
+ − 1214
+ − 1215 @code{yes-or-no-p} requires more work from the user than
+ − 1216 @code{y-or-n-p} and is appropriate for more crucial decisions.
+ − 1217
+ − 1218 Here is an example:
+ − 1219
+ − 1220 @smallexample
+ − 1221 @group
+ − 1222 (yes-or-no-p "Do you really want to remove everything? ")
+ − 1223
444
+ − 1224 ;; @r{After evaluation of the preceding expression,}
+ − 1225 ;; @r{the following prompt appears,}
428
+ − 1226 ;; @r{with an empty minibuffer:}
+ − 1227 @end group
+ − 1228
+ − 1229 @group
+ − 1230 ---------- Buffer: minibuffer ----------
444
+ − 1231 Do you really want to remove everything? (yes or no)
428
+ − 1232 ---------- Buffer: minibuffer ----------
+ − 1233 @end group
+ − 1234 @end smallexample
+ − 1235
+ − 1236 @noindent
+ − 1237 If the user first types @kbd{y @key{RET}}, which is invalid because this
+ − 1238 function demands the entire word @samp{yes}, it responds by displaying
+ − 1239 these prompts, with a brief pause between them:
+ − 1240
+ − 1241 @smallexample
+ − 1242 @group
+ − 1243 ---------- Buffer: minibuffer ----------
+ − 1244 Please answer yes or no.
+ − 1245 Do you really want to remove everything? (yes or no)
+ − 1246 ---------- Buffer: minibuffer ----------
+ − 1247 @end group
+ − 1248 @end smallexample
+ − 1249 @end defun
+ − 1250
+ − 1251 @c The rest is XEmacs stuff
+ − 1252 @defun yes-or-no-p-dialog-box prompt
+ − 1253 This function asks the user a ``y or n'' question with a popup dialog
+ − 1254 box. It returns @code{t} if the answer is ``yes''. @var{prompt} is the
+ − 1255 string to display to ask the question.
+ − 1256 @end defun
+ − 1257
+ − 1258 The following functions ask a question either in the minibuffer or a
+ − 1259 dialog box, depending on whether the last user event (which presumably
+ − 1260 invoked this command) was a keyboard or mouse event. When XEmacs is
+ − 1261 running on a window system, the functions @code{y-or-n-p} and
+ − 1262 @code{yes-or-no-p} are replaced with the following functions, so that
+ − 1263 menu items bring up dialog boxes instead of minibuffer questions.
+ − 1264
+ − 1265 @defun y-or-n-p-maybe-dialog-box prompt
+ − 1266 This function asks user a ``y or n'' question, using either a dialog box
+ − 1267 or the minibuffer, as appropriate.
+ − 1268 @end defun
+ − 1269
+ − 1270 @defun yes-or-no-p-maybe-dialog-box prompt
+ − 1271 This function asks user a ``yes or no'' question, using either a dialog
+ − 1272 box or the minibuffer, as appropriate.
+ − 1273 @end defun
+ − 1274
+ − 1275 @node Multiple Queries
+ − 1276 @section Asking Multiple Y-or-N Questions
+ − 1277
+ − 1278 When you have a series of similar questions to ask, such as ``Do you
+ − 1279 want to save this buffer'' for each buffer in turn, you should use
+ − 1280 @code{map-y-or-n-p} to ask the collection of questions, rather than
+ − 1281 asking each question individually. This gives the user certain
+ − 1282 convenient facilities such as the ability to answer the whole series at
+ − 1283 once.
+ − 1284
+ − 1285 @defun map-y-or-n-p prompter actor list &optional help action-alist
+ − 1286 This function, new in Emacs 19, asks the user a series of questions,
+ − 1287 reading a single-character answer in the echo area for each one.
+ − 1288
+ − 1289 The value of @var{list} specifies the objects to ask questions about.
+ − 1290 It should be either a list of objects or a generator function. If it is
+ − 1291 a function, it should expect no arguments, and should return either the
+ − 1292 next object to ask about, or @code{nil} meaning stop asking questions.
+ − 1293
+ − 1294 The argument @var{prompter} specifies how to ask each question. If
+ − 1295 @var{prompter} is a string, the question text is computed like this:
+ − 1296
+ − 1297 @example
+ − 1298 (format @var{prompter} @var{object})
+ − 1299 @end example
+ − 1300
+ − 1301 @noindent
+ − 1302 where @var{object} is the next object to ask about (as obtained from
+ − 1303 @var{list}).
+ − 1304
+ − 1305 If not a string, @var{prompter} should be a function of one argument
+ − 1306 (the next object to ask about) and should return the question text. If
+ − 1307 the value is a string, that is the question to ask the user. The
+ − 1308 function can also return @code{t} meaning do act on this object (and
+ − 1309 don't ask the user), or @code{nil} meaning ignore this object (and don't
+ − 1310 ask the user).
+ − 1311
+ − 1312 The argument @var{actor} says how to act on the answers that the user
+ − 1313 gives. It should be a function of one argument, and it is called with
+ − 1314 each object that the user says yes for. Its argument is always an
+ − 1315 object obtained from @var{list}.
+ − 1316
+ − 1317 If the argument @var{help} is given, it should be a list of this form:
+ − 1318
+ − 1319 @example
+ − 1320 (@var{singular} @var{plural} @var{action})
+ − 1321 @end example
+ − 1322
+ − 1323 @noindent
+ − 1324 where @var{singular} is a string containing a singular noun that
+ − 1325 describes the objects conceptually being acted on, @var{plural} is the
+ − 1326 corresponding plural noun, and @var{action} is a transitive verb
+ − 1327 describing what @var{actor} does.
+ − 1328
+ − 1329 If you don't specify @var{help}, the default is @code{("object"
+ − 1330 "objects" "act on")}.
+ − 1331
+ − 1332 Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
+ − 1333 @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
+ − 1334 that object; @kbd{!} to act on all following objects; @key{ESC} or
+ − 1335 @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
+ − 1336 the current object and then exit; or @kbd{C-h} to get help. These are
+ − 1337 the same answers that @code{query-replace} accepts. The keymap
+ − 1338 @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
+ − 1339 as well as for @code{query-replace}; see @ref{Search and Replace}.
+ − 1340
+ − 1341 You can use @var{action-alist} to specify additional possible answers
+ − 1342 and what they mean. It is an alist of elements of the form
+ − 1343 @code{(@var{char} @var{function} @var{help})}, each of which defines one
+ − 1344 additional answer. In this element, @var{char} is a character (the
+ − 1345 answer); @var{function} is a function of one argument (an object from
+ − 1346 @var{list}); @var{help} is a string.
+ − 1347
+ − 1348 When the user responds with @var{char}, @code{map-y-or-n-p} calls
+ − 1349 @var{function}. If it returns non-@code{nil}, the object is considered
+ − 1350 ``acted upon'', and @code{map-y-or-n-p} advances to the next object in
+ − 1351 @var{list}. If it returns @code{nil}, the prompt is repeated for the
+ − 1352 same object.
+ − 1353
+ − 1354 If @code{map-y-or-n-p} is called in a command that was invoked using the
+ − 1355 mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
+ − 1356 Loop Info}) is either @code{nil} or a list---then it uses a dialog box
+ − 1357 or pop-up menu to ask the question. In this case, it does not use
+ − 1358 keyboard input or the echo area. You can force use of the mouse or use
+ − 1359 of keyboard input by binding @code{last-nonmenu-event} to a suitable
+ − 1360 value around the call.
+ − 1361
+ − 1362 The return value of @code{map-y-or-n-p} is the number of objects acted on.
+ − 1363 @end defun
+ − 1364
434
+ − 1365 @node Reading a Password
+ − 1366 @section Reading a Password
+ − 1367 @cindex passwords, reading
+ − 1368
+ − 1369 To read a password to pass to another program, you can use the
+ − 1370 function @code{read-passwd}.
+ − 1371
+ − 1372 @defun read-passwd prompt &optional confirm default
+ − 1373 This function reads a password, prompting with @var{prompt}. It does
+ − 1374 not echo the password as the user types it; instead, it echoes @samp{.}
+ − 1375 for each character in the password.
+ − 1376
+ − 1377 The optional argument @var{confirm}, if non-@code{nil}, says to read the
+ − 1378 password twice and insist it must be the same both times. If it isn't
+ − 1379 the same, the user has to type it over and over until the last two
+ − 1380 times match.
+ − 1381
+ − 1382 The optional argument @var{default} specifies the default password to
+ − 1383 return if the user enters empty input. It is translated to @samp{.}
+ − 1384 and inserted in the minibuffer. If @var{default} is @code{nil}, then
+ − 1385 @code{read-passwd} returns the null string in that case.
+ − 1386 @end defun
+ − 1387
+ − 1388 @defopt passwd-invert-frame-when-keyboard-grabbed
444
+ − 1389 If non-@code{nil}, swap the foreground and background colors of all faces while
+ − 1390 reading a password. Default values is @code{t}, unless feature
434
+ − 1391 @code{infodock} is provided.
+ − 1392 @end defopt
+ − 1393
+ − 1394 @defopt passwd-echo
444
+ − 1395 This specifies the character echoed when typing a password. When @code{nil},
434
+ − 1396 nothing is echoed.
+ − 1397 @end defopt
+ − 1398
428
+ − 1399 @node Minibuffer Misc
+ − 1400 @section Minibuffer Miscellany
+ − 1401
+ − 1402 This section describes some basic functions and variables related to
+ − 1403 minibuffers.
+ − 1404
+ − 1405 @deffn Command exit-minibuffer
+ − 1406 This command exits the active minibuffer. It is normally bound to
+ − 1407 keys in minibuffer local keymaps.
+ − 1408 @end deffn
+ − 1409
+ − 1410 @deffn Command self-insert-and-exit
+ − 1411 This command exits the active minibuffer after inserting the last
+ − 1412 character typed on the keyboard (found in @code{last-command-char};
+ − 1413 @pxref{Command Loop Info}).
+ − 1414 @end deffn
+ − 1415
+ − 1416 @deffn Command previous-history-element n
+ − 1417 This command replaces the minibuffer contents with the value of the
+ − 1418 @var{n}th previous (older) history element.
+ − 1419 @end deffn
+ − 1420
+ − 1421 @deffn Command next-history-element n
+ − 1422 This command replaces the minibuffer contents with the value of the
+ − 1423 @var{n}th more recent history element.
+ − 1424 @end deffn
+ − 1425
+ − 1426 @deffn Command previous-matching-history-element pattern
+ − 1427 This command replaces the minibuffer contents with the value of the
+ − 1428 previous (older) history element that matches @var{pattern} (a regular
+ − 1429 expression).
+ − 1430 @end deffn
+ − 1431
+ − 1432 @deffn Command next-matching-history-element pattern
+ − 1433 This command replaces the minibuffer contents with the value of the next
+ − 1434 (newer) history element that matches @var{pattern} (a regular
+ − 1435 expression).
+ − 1436 @end deffn
+ − 1437
+ − 1438 @defun minibuffer-prompt
+ − 1439 This function returns the prompt string of the currently active
+ − 1440 minibuffer. If no minibuffer is active, it returns @code{nil}.
+ − 1441 @end defun
+ − 1442
+ − 1443 @defun minibuffer-prompt-width
+ − 1444 This function returns the display width of the prompt string of the
+ − 1445 currently active minibuffer. If no minibuffer is active, it returns 0.
+ − 1446 @end defun
+ − 1447
+ − 1448 @defvar minibuffer-setup-hook
+ − 1449 This is a normal hook that is run whenever the minibuffer is entered.
+ − 1450 @xref{Hooks}.
+ − 1451 @end defvar
+ − 1452
+ − 1453 @defvar minibuffer-exit-hook
+ − 1454 This is a normal hook that is run whenever the minibuffer is exited.
+ − 1455 @xref{Hooks}.
+ − 1456 @end defvar
+ − 1457
+ − 1458 @defvar minibuffer-help-form
+ − 1459 The current value of this variable is used to rebind @code{help-form}
+ − 1460 locally inside the minibuffer (@pxref{Help Functions}).
+ − 1461 @end defvar
+ − 1462
+ − 1463 @defun active-minibuffer-window
+ − 1464 This function returns the currently active minibuffer window, or
+ − 1465 @code{nil} if none is currently active.
+ − 1466 @end defun
+ − 1467
+ − 1468 @defun minibuffer-window &optional frame
+ − 1469 This function returns the minibuffer window used for frame @var{frame}.
+ − 1470 If @var{frame} is @code{nil}, that stands for the current frame. Note
+ − 1471 that the minibuffer window used by a frame need not be part of that
+ − 1472 frame---a frame that has no minibuffer of its own necessarily uses some
+ − 1473 other frame's minibuffer window.
+ − 1474 @end defun
+ − 1475
+ − 1476 @c Emacs 19 feature
444
+ − 1477 @defun window-minibuffer-p &optional window
428
+ − 1478 This function returns non-@code{nil} if @var{window} is a minibuffer window.
+ − 1479 @end defun
+ − 1480
+ − 1481 It is not correct to determine whether a given window is a minibuffer by
+ − 1482 comparing it with the result of @code{(minibuffer-window)}, because
+ − 1483 there can be more than one minibuffer window if there is more than one
+ − 1484 frame.
+ − 1485
+ − 1486 @defun minibuffer-window-active-p window
+ − 1487 This function returns non-@code{nil} if @var{window}, assumed to be
+ − 1488 a minibuffer window, is currently active.
+ − 1489 @end defun
+ − 1490
+ − 1491 @defvar minibuffer-scroll-window
+ − 1492 If the value of this variable is non-@code{nil}, it should be a window
+ − 1493 object. When the function @code{scroll-other-window} is called in the
+ − 1494 minibuffer, it scrolls this window.
+ − 1495 @end defvar
+ − 1496
+ − 1497 Finally, some functions and variables deal with recursive minibuffers
+ − 1498 (@pxref{Recursive Editing}):
+ − 1499
+ − 1500 @defun minibuffer-depth
+ − 1501 This function returns the current depth of activations of the
+ − 1502 minibuffer, a nonnegative integer. If no minibuffers are active, it
+ − 1503 returns zero.
+ − 1504 @end defun
+ − 1505
+ − 1506 @defopt enable-recursive-minibuffers
+ − 1507 If this variable is non-@code{nil}, you can invoke commands (such as
434
+ − 1508 @code{find-file}) that use minibuffers even while the minibuffer window
+ − 1509 is active. Such invocation produces a recursive editing level for a new
428
+ − 1510 minibuffer. The outer-level minibuffer is invisible while you are
+ − 1511 editing the inner one.
+ − 1512
+ − 1513 This variable only affects invoking the minibuffer while the
444
+ − 1514 minibuffer window is selected. If you switch windows while in the
428
+ − 1515 minibuffer, you can always invoke minibuffer commands while some other
+ − 1516 window is selected.
+ − 1517 @end defopt
+ − 1518
+ − 1519 @c Emacs 19 feature
+ − 1520 In FSF Emacs 19, if a command name has a property
+ − 1521 @code{enable-recursive-minibuffers} that is non-@code{nil}, then the
+ − 1522 command can use the minibuffer to read arguments even if it is invoked
+ − 1523 from the minibuffer. The minibuffer command
+ − 1524 @code{next-matching-history-element} (normally @kbd{M-s} in the
+ − 1525 minibuffer) uses this feature.
+ − 1526
+ − 1527 This is not implemented in XEmacs because it is a kludge. If you
+ − 1528 want to explicitly set the value of @code{enable-recursive-minibuffers}
+ − 1529 in this fashion, just use an evaluated interactive spec and bind
+ − 1530 @code{enable-recursive-minibuffers} while reading from the minibuffer.
+ − 1531 See the definition of @code{next-matching-history-element} in
434
+ − 1532 @file{lisp/minibuf.el}.