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