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