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