* lread.c (lisp_file_lexically_bound_p): Fix hang at ";-*-\n".
[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-1995, 1998-1999, 2001-2012
4 @c 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
437 so the user can reuse them conveniently. It is a variable whose value
438 is a list of strings (previous inputs), most recent first.
439
440 There are many separate minibuffer history lists, used for different
441 kinds of inputs. It's the Lisp programmer's job to specify the right
442 history list for each use of the minibuffer.
443
444 You specify a minibuffer history list with the optional @var{hist}
445 argument to @code{read-from-minibuffer} or @code{completing-read}.
446 Here are the possible values for it:
447
448 @table @asis
449 @item @var{variable}
450 Use @var{variable} (a symbol) as the history list.
451
452 @item (@var{variable} . @var{startpos})
453 Use @var{variable} (a symbol) as the history list, and assume that the
454 initial history position is @var{startpos} (a nonnegative integer).
455
456 Specifying 0 for @var{startpos} is equivalent to just specifying the
457 symbol @var{variable}. @code{previous-history-element} will display
458 the most recent element of the history list in the minibuffer. If you
459 specify a positive @var{startpos}, the minibuffer history functions
460 behave as if @code{(elt @var{variable} (1- @var{STARTPOS}))} were the
461 history element currently shown in the minibuffer.
462
463 For consistency, you should also specify that element of the history
464 as the initial minibuffer contents, using the @var{initial} argument
465 to the minibuffer input function (@pxref{Initial Input}).
466 @end table
467
468 If you don't specify @var{hist}, then the default history list
469 @code{minibuffer-history} is used. For other standard history lists,
470 see below. You can also create your own history list variable; just
471 initialize it to @code{nil} before the first use.
472
473 Both @code{read-from-minibuffer} and @code{completing-read} add new
474 elements to the history list automatically, and provide commands to
475 allow the user to reuse items on the list. The only thing your program
476 needs to do to use a history list is to initialize it and to pass its
477 name to the input functions when you wish. But it is safe to modify the
478 list by hand when the minibuffer input functions are not using it.
479
480 Emacs functions that add a new element to a history list can also
481 delete old elements if the list gets too long. The variable
482 @code{history-length} specifies the maximum length for most history
483 lists. To specify a different maximum length for a particular history
484 list, put the length in the @code{history-length} property of the
485 history list symbol. The variable @code{history-delete-duplicates}
486 specifies whether to delete duplicates in history.
487
488 @defun add-to-history history-var newelt &optional maxelt keep-all
489 This function adds a new element @var{newelt}, if it isn't the empty
490 string, to the history list stored in the variable @var{history-var},
491 and returns the updated history list. It limits the list length to
492 the value of @var{maxelt} (if non-@code{nil}) or @code{history-length}
493 (described below). The possible values of @var{maxelt} have the same
494 meaning as the values of @code{history-length}.
495
496 Normally, @code{add-to-history} removes duplicate members from the
497 history list if @code{history-delete-duplicates} is non-@code{nil}.
498 However, if @var{keep-all} is non-@code{nil}, that says not to remove
499 duplicates, and to add @var{newelt} to the list even if it is empty.
500 @end defun
501
502 @defvar history-add-new-input
503 If the value of this variable is @code{nil}, standard functions that
504 read from the minibuffer don't add new elements to the history list.
505 This lets Lisp programs explicitly manage input history by using
506 @code{add-to-history}. By default, @code{history-add-new-input} is
507 set to a non-@code{nil} value.
508 @end defvar
509
510 @defopt history-length
511 The value of this variable specifies the maximum length for all
512 history lists that don't specify their own maximum lengths. If the
513 value is @code{t}, that means there is no maximum (don't delete old
514 elements). The value of @code{history-length} property of the history
515 list variable's symbol, if set, overrides this variable for that
516 particular history list.
517 @end defopt
518
519 @defopt history-delete-duplicates
520 If the value of this variable is @code{t}, that means when adding a
521 new history element, all previous identical elements are deleted.
522 @end defopt
523
524 Here are some of the standard minibuffer history list variables:
525
526 @defvar minibuffer-history
527 The default history list for minibuffer history input.
528 @end defvar
529
530 @defvar query-replace-history
531 A history list for arguments to @code{query-replace} (and similar
532 arguments to other commands).
533 @end defvar
534
535 @defvar file-name-history
536 A history list for file-name arguments.
537 @end defvar
538
539 @defvar buffer-name-history
540 A history list for buffer-name arguments.
541 @end defvar
542
543 @defvar regexp-history
544 A history list for regular expression arguments.
545 @end defvar
546
547 @defvar extended-command-history
548 A history list for arguments that are names of extended commands.
549 @end defvar
550
551 @defvar shell-command-history
552 A history list for arguments that are shell commands.
553 @end defvar
554
555 @defvar read-expression-history
556 A history list for arguments that are Lisp expressions to evaluate.
557 @end defvar
558
559 @node Initial Input
560 @section Initial Input
561
562 Several of the functions for minibuffer input have an argument called
563 @var{initial} or @var{initial-contents}. This is a mostly-deprecated
564 feature for specifying that the minibuffer should start out with
565 certain text, instead of empty as usual.
566
567 If @var{initial} is a string, the minibuffer starts out containing the
568 text of the string, with point at the end, when the user starts to
569 edit the text. If the user simply types @key{RET} to exit the
570 minibuffer, it will use the initial input string to determine the
571 value to return.
572
573 @strong{We discourage use of a non-@code{nil} value for
574 @var{initial}}, because initial input is an intrusive interface.
575 History lists and default values provide a much more convenient method
576 to offer useful default inputs to the user.
577
578 There is just one situation where you should specify a string for an
579 @var{initial} argument. This is when you specify a cons cell for the
580 @var{hist} or @var{history} argument. @xref{Minibuffer History}.
581
582 @var{initial} can also be a cons cell of the form @code{(@var{string}
583 . @var{position})}. This means to insert @var{string} in the
584 minibuffer but put point at @var{position} within the string's text.
585
586 As a historical accident, @var{position} was implemented
587 inconsistently in different functions. In @code{completing-read},
588 @var{position}'s value is interpreted as origin-zero; that is, a value
589 of 0 means the beginning of the string, 1 means after the first
590 character, etc. In @code{read-minibuffer}, and the other
591 non-completion minibuffer input functions that support this argument,
592 1 means the beginning of the string 2 means after the first character,
593 etc.
594
595 Use of a cons cell as the value for @var{initial} arguments is
596 deprecated in user code.
597
598 @node Completion
599 @section Completion
600 @cindex completion
601
602 @dfn{Completion} is a feature that fills in the rest of a name
603 starting from an abbreviation for it. Completion works by comparing the
604 user's input against a list of valid names and determining how much of
605 the name is determined uniquely by what the user has typed. For
606 example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
607 type the first few letters of the name of the buffer to which you wish
608 to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
609 extends the name as far as it can.
610
611 Standard Emacs commands offer completion for names of symbols, files,
612 buffers, and processes; with the functions in this section, you can
613 implement completion for other kinds of names.
614
615 The @code{try-completion} function is the basic primitive for
616 completion: it returns the longest determined completion of a given
617 initial string, with a given set of strings to match against.
618
619 The function @code{completing-read} provides a higher-level interface
620 for completion. A call to @code{completing-read} specifies how to
621 determine the list of valid names. The function then activates the
622 minibuffer with a local keymap that binds a few keys to commands useful
623 for completion. Other functions provide convenient simple interfaces
624 for reading certain kinds of names with completion.
625
626 @menu
627 * Basic Completion:: Low-level functions for completing strings.
628 * Minibuffer Completion:: Invoking the minibuffer with completion.
629 * Completion Commands:: Minibuffer commands that do completion.
630 * High-Level Completion:: Convenient special cases of completion
631 (reading buffer name, file name, etc.).
632 * Reading File Names:: Using completion to read file names and
633 shell commands.
634 * Completion Variables:: Variables controlling completion behavior.
635 * Programmed Completion:: Writing your own completion function.
636 * Completion in Buffers:: Completing text in ordinary buffers.
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}.
649
650 @cindex completion table
651 The @var{collection} argument is called the @dfn{completion table}.
652 Its value must be a list of strings, an alist whose keys are strings
653 or symbols, an obarray, a hash table, or a completion function.
654
655 Completion compares @var{string} against each of the permissible
656 completions specified by @var{collection}. If no permissible
657 completions match, @code{try-completion} returns @code{nil}. If there
658 is just one matching completion, and the match is exact, it returns
659 @code{t}. Otherwise, it returns the longest initial sequence common
660 to all possible matching completions.
661
662 If @var{collection} is an alist (@pxref{Association Lists}), the
663 permissible completions are the elements of the alist that are either
664 strings, or conses whose @sc{car} is a string or symbol.
665 Symbols are converted to strings using @code{symbol-name}. Other
666 elements of the alist are ignored. (Remember that in Emacs Lisp, the
667 elements of alists do not @emph{have} to be conses.) In particular, a
668 list of strings is allowed, even though we usually do not
669 think of such lists as alists.
670
671 @cindex obarray in completion
672 If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
673 of all symbols in the obarray form the set of permissible completions. The
674 global variable @code{obarray} holds an obarray containing the names of
675 all interned Lisp symbols.
676
677 Note that the only valid way to make a new obarray is to create it
678 empty and then add symbols to it one by one using @code{intern}.
679 Also, you cannot intern a given symbol in more than one obarray.
680
681 If @var{collection} is a hash table, then the keys that are strings
682 are the possible completions. Other keys are ignored.
683
684 You can also use a function as @var{collection}. Then the function is
685 solely responsible for performing completion; @code{try-completion}
686 returns whatever this function returns. The function is called with
687 three arguments: @var{string}, @var{predicate} and @code{nil} (the
688 reason for the third argument is so that the same function can be used
689 in @code{all-completions} and do the appropriate thing in either
690 case). @xref{Programmed Completion}.
691
692 If the argument @var{predicate} is non-@code{nil}, then it must be a
693 function of one argument, unless @var{collection} is a hash table, in
694 which case it should be a function of two arguments. It is used to
695 test each possible match, and the match is accepted only if
696 @var{predicate} returns non-@code{nil}. The argument given to
697 @var{predicate} is either a string or a cons cell (the @sc{car} of
698 which is a string) from the alist, or a symbol (@emph{not} a symbol
699 name) from the obarray. If @var{collection} is a hash table,
700 @var{predicate} is called with two arguments, the string key and the
701 associated value.
702
703 In addition, to be acceptable, a completion must also match all the
704 regular expressions in @code{completion-regexp-list}. (Unless
705 @var{collection} is a function, in which case that function has to
706 handle @code{completion-regexp-list} itself.)
707
708 In the first of the following examples, the string @samp{foo} is
709 matched by three of the alist @sc{car}s. All of the matches begin with
710 the characters @samp{fooba}, so that is the result. In the second
711 example, there is only one possible match, and it is exact, so the value
712 is @code{t}.
713
714 @smallexample
715 @group
716 (try-completion
717 "foo"
718 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
719 @result{} "fooba"
720 @end group
721
722 @group
723 (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
724 @result{} t
725 @end group
726 @end smallexample
727
728 In the following example, numerous symbols begin with the characters
729 @samp{forw}, and all of them begin with the word @samp{forward}. In
730 most of the symbols, this is followed with a @samp{-}, but not in all,
731 so no more than @samp{forward} can be completed.
732
733 @smallexample
734 @group
735 (try-completion "forw" obarray)
736 @result{} "forward"
737 @end group
738 @end smallexample
739
740 Finally, in the following example, only two of the three possible
741 matches pass the predicate @code{test} (the string @samp{foobaz} is
742 too short). Both of those begin with the string @samp{foobar}.
743
744 @smallexample
745 @group
746 (defun test (s)
747 (> (length (car s)) 6))
748 @result{} test
749 @end group
750 @group
751 (try-completion
752 "foo"
753 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
754 'test)
755 @result{} "foobar"
756 @end group
757 @end smallexample
758 @end defun
759
760 @defun all-completions string collection &optional predicate nospace
761 This function returns a list of all possible completions of
762 @var{string}. The arguments to this function (aside from
763 @var{nospace}) are the same as those of @code{try-completion}. Also,
764 this function uses @code{completion-regexp-list} in the same way that
765 @code{try-completion} does.
766
767 The optional argument @var{nospace} is obsolete. If it is
768 non-@code{nil}, completions that start with a space are ignored unless
769 @var{string} starts with a space.
770
771 If @var{collection} is a function, it is called with three arguments:
772 @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
773 returns whatever the function returns. @xref{Programmed Completion}.
774
775 Here is an example, using the function @code{test} shown in the
776 example for @code{try-completion}:
777
778 @smallexample
779 @group
780 (defun test (s)
781 (> (length (car s)) 6))
782 @result{} test
783 @end group
784
785 @group
786 (all-completions
787 "foo"
788 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
789 'test)
790 @result{} ("foobar1" "foobar2")
791 @end group
792 @end smallexample
793 @end defun
794
795 @defun test-completion string collection &optional predicate
796 @anchor{Definition of test-completion}
797 This function returns non-@code{nil} if @var{string} is a valid
798 completion alternative specified by @var{collection} and
799 @var{predicate}. The arguments are the same as in
800 @code{try-completion}. For instance, if @var{collection} is a list of
801 strings, this is true if @var{string} appears in the list and
802 @var{predicate} is satisfied.
803
804 This function uses @code{completion-regexp-list} in the same
805 way that @code{try-completion} does.
806
807 If @var{predicate} is non-@code{nil} and if @var{collection} contains
808 several strings that are equal to each other, as determined by
809 @code{compare-strings} according to @code{completion-ignore-case},
810 then @var{predicate} should accept either all or none of them.
811 Otherwise, the return value of @code{test-completion} is essentially
812 unpredictable.
813
814 If @var{collection} is a function, it is called with three arguments,
815 the values @var{string}, @var{predicate} and @code{lambda}; whatever
816 it returns, @code{test-completion} returns in turn.
817 @end defun
818
819 @defun completion-boundaries string collection predicate suffix
820 This function returns the boundaries of the field on which @var{collection}
821 will operate, assuming that @var{string} holds the text before point
822 and @var{suffix} holds the text after point.
823
824 Normally completion operates on the whole string, so for all normal
825 collections, this will always return @code{(0 . (length
826 @var{suffix}))}. But more complex completion such as completion on
827 files is done one field at a time. For example, completion of
828 @code{"/usr/sh"} will include @code{"/usr/share/"} but not
829 @code{"/usr/share/doc"} even if @code{"/usr/share/doc"} exists.
830 Also @code{all-completions} on @code{"/usr/sh"} will not include
831 @code{"/usr/share/"} but only @code{"share/"}. So if @var{string} is
832 @code{"/usr/sh"} and @var{suffix} is @code{"e/doc"},
833 @code{completion-boundaries} will return @code{(5 . 1)} which tells us
834 that the @var{collection} will only return completion information that
835 pertains to the area after @code{"/usr/"} and before @code{"/doc"}.
836 @end defun
837
838 If you store a completion alist in a variable, you should mark the
839 variable as ``risky'' with a non-@code{nil}
840 @code{risky-local-variable} property. @xref{File Local Variables}.
841
842 @defvar completion-ignore-case
843 If the value of this variable is non-@code{nil}, case is not
844 considered significant in completion. Within @code{read-file-name},
845 this variable is overridden by
846 @code{read-file-name-completion-ignore-case} (@pxref{Reading File
847 Names}); within @code{read-buffer}, it is overridden by
848 @code{read-buffer-completion-ignore-case} (@pxref{High-Level
849 Completion}).
850 @end defvar
851
852 @defvar completion-regexp-list
853 This is a list of regular expressions. The completion functions only
854 consider a completion acceptable if it matches all regular expressions
855 in this list, with @code{case-fold-search} (@pxref{Searching and Case})
856 bound to the value of @code{completion-ignore-case}.
857 @end defvar
858
859 @defmac lazy-completion-table var fun
860 This macro provides a way to initialize the variable @var{var} as a
861 collection for completion in a lazy way, not computing its actual
862 contents until they are first needed. You use this macro to produce a
863 value that you store in @var{var}. The actual computation of the
864 proper value is done the first time you do completion using @var{var}.
865 It is done by calling @var{fun} with no arguments. The
866 value @var{fun} returns becomes the permanent value of @var{var}.
867
868 Here is a usage example:
869
870 @smallexample
871 (defvar foo (lazy-completion-table foo make-my-alist))
872 @end smallexample
873 @end defmac
874
875 @node Minibuffer Completion
876 @subsection Completion and the Minibuffer
877 @cindex minibuffer completion
878 @cindex reading from minibuffer with completion
879
880 This section describes the basic interface for reading from the
881 minibuffer with completion.
882
883 @defun completing-read prompt collection &optional predicate require-match initial hist default inherit-input-method
884 This function reads a string in the minibuffer, assisting the user by
885 providing completion. It activates the minibuffer with prompt
886 @var{prompt}, which must be a string.
887
888 The actual completion is done by passing the completion table
889 @var{collection} and the completion predicate @var{predicate} to the
890 function @code{try-completion} (@pxref{Basic Completion}). This
891 happens in certain commands bound in the local keymaps used for
892 completion. Some of these commands also call @code{test-completion}.
893 Thus, if @var{predicate} is non-@code{nil}, it should be compatible
894 with @var{collection} and @code{completion-ignore-case}.
895 @xref{Definition of test-completion}.
896
897 The value of the optional argument @var{require-match} determines how
898 the user may exit the minibuffer:
899
900 @itemize @bullet
901 @item
902 If @code{nil}, the usual minibuffer exit commands work regardless of
903 the input in the minibuffer.
904
905 @item
906 If @code{t}, the usual minibuffer exit commands won't exit unless the
907 input completes to an element of @var{collection}.
908
909 @item
910 If @code{confirm}, the user can exit with any input, but is asked for
911 confirmation if the input is not an element of @var{collection}.
912
913 @item
914 If @code{confirm-after-completion}, the user can exit with any input,
915 but is asked for confirmation if the preceding command was a
916 completion command (i.e., one of the commands in
917 @code{minibuffer-confirm-exit-commands}) and the resulting input is
918 not an element of @var{collection}. @xref{Completion Commands}.
919
920 @item
921 Any other value of @var{require-match} behaves like @code{t}, except
922 that the exit commands won't exit if it performs completion.
923 @end itemize
924
925 However, empty input is always permitted, regardless of the value of
926 @var{require-match}; in that case, @code{completing-read} returns the
927 first element of @var{default}, if it is a list; @code{""}, if
928 @var{default} is @code{nil}; or @var{default}. The string or strings
929 in @var{default} are also available to the user through the history
930 commands.
931
932 The function @code{completing-read} uses
933 @code{minibuffer-local-completion-map} as the keymap if
934 @var{require-match} is @code{nil}, and uses
935 @code{minibuffer-local-must-match-map} if @var{require-match} is
936 non-@code{nil}. @xref{Completion Commands}.
937
938 The argument @var{hist} specifies which history list variable to use for
939 saving the input and for minibuffer history commands. It defaults to
940 @code{minibuffer-history}. @xref{Minibuffer History}.
941
942 The argument @var{initial} is mostly deprecated; we recommend using a
943 non-@code{nil} value only in conjunction with specifying a cons cell
944 for @var{hist}. @xref{Initial Input}. For default input, use
945 @var{default} instead.
946
947 If the argument @var{inherit-input-method} is non-@code{nil}, then the
948 minibuffer inherits the current input method (@pxref{Input
949 Methods}) and the setting of @code{enable-multibyte-characters}
950 (@pxref{Text Representations}) from whichever buffer was current before
951 entering the minibuffer.
952
953 If the built-in variable @code{completion-ignore-case} is
954 non-@code{nil}, completion ignores case when comparing the input
955 against the possible matches. @xref{Basic Completion}. In this mode
956 of operation, @var{predicate} must also ignore case, or you will get
957 surprising results.
958
959 Here's an example of using @code{completing-read}:
960
961 @smallexample
962 @group
963 (completing-read
964 "Complete a foo: "
965 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
966 nil t "fo")
967 @end group
968
969 @group
970 ;; @r{After evaluation of the preceding expression,}
971 ;; @r{the following appears in the minibuffer:}
972
973 ---------- Buffer: Minibuffer ----------
974 Complete a foo: fo@point{}
975 ---------- Buffer: Minibuffer ----------
976 @end group
977 @end smallexample
978
979 @noindent
980 If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
981 @code{completing-read} returns @code{barfoo}.
982
983 The @code{completing-read} function binds variables to pass
984 information to the commands that actually do completion.
985 They are described in the following section.
986 @end defun
987
988 @defvar completing-read-function
989 The value of this variable must be a function, which is called by
990 @code{completing-read} to actually do its work. It should accept the
991 same arguments as @code{completing-read}. This can be bound to a
992 different function to completely override the normal behavior of
993 @code{completing-read}.
994 @end defvar
995
996 @node Completion Commands
997 @subsection Minibuffer Commands that Do Completion
998
999 This section describes the keymaps, commands and user options used
1000 in the minibuffer to do completion.
1001
1002 @defvar minibuffer-completion-table
1003 The value of this variable is the completion table used for completion
1004 in the minibuffer. This is the global variable that contains what
1005 @code{completing-read} passes to @code{try-completion}. It is used by
1006 minibuffer completion commands such as
1007 @code{minibuffer-complete-word}.
1008 @end defvar
1009
1010 @defvar minibuffer-completion-predicate
1011 This variable's value is the predicate that @code{completing-read}
1012 passes to @code{try-completion}. The variable is also used by the other
1013 minibuffer completion functions.
1014 @end defvar
1015
1016 @defvar minibuffer-completion-confirm
1017 This variable determines whether Emacs asks for confirmation before
1018 exiting the minibuffer; @code{completing-read} binds this variable,
1019 and the function @code{minibuffer-complete-and-exit} checks the value
1020 before exiting. If the value is @code{nil}, confirmation is not
1021 required. If the value is @code{confirm}, the user may exit with an
1022 input that is not a valid completion alternative, but Emacs asks for
1023 confirmation. If the value is @code{confirm-after-completion}, the
1024 user may exit with an input that is not a valid completion
1025 alternative, but Emacs asks for confirmation if the user submitted the
1026 input right after any of the completion commands in
1027 @code{minibuffer-confirm-exit-commands}.
1028 @end defvar
1029
1030 @defvar minibuffer-confirm-exit-commands
1031 This variable holds a list of commands that cause Emacs to ask for
1032 confirmation before exiting the minibuffer, if the @var{require-match}
1033 argument to @code{completing-read} is @code{confirm-after-completion}.
1034 The confirmation is requested if the user attempts to exit the
1035 minibuffer immediately after calling any command in this list.
1036 @end defvar
1037
1038 @deffn Command minibuffer-complete-word
1039 This function completes the minibuffer contents by at most a single
1040 word. Even if the minibuffer contents have only one completion,
1041 @code{minibuffer-complete-word} does not add any characters beyond the
1042 first character that is not a word constituent. @xref{Syntax Tables}.
1043 @end deffn
1044
1045 @deffn Command minibuffer-complete
1046 This function completes the minibuffer contents as far as possible.
1047 @end deffn
1048
1049 @deffn Command minibuffer-complete-and-exit
1050 This function completes the minibuffer contents, and exits if
1051 confirmation is not required, i.e., if
1052 @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
1053 @emph{is} required, it is given by repeating this command
1054 immediately---the command is programmed to work without confirmation
1055 when run twice in succession.
1056 @end deffn
1057
1058 @deffn Command minibuffer-completion-help
1059 This function creates a list of the possible completions of the
1060 current minibuffer contents. It works by calling @code{all-completions}
1061 using the value of the variable @code{minibuffer-completion-table} as
1062 the @var{collection} argument, and the value of
1063 @code{minibuffer-completion-predicate} as the @var{predicate} argument.
1064 The list of completions is displayed as text in a buffer named
1065 @file{*Completions*}.
1066 @end deffn
1067
1068 @defun display-completion-list completions &optional common-substring
1069 This function displays @var{completions} to the stream in
1070 @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
1071 information about streams.) The argument @var{completions} is normally
1072 a list of completions just returned by @code{all-completions}, but it
1073 does not have to be. Each element may be a symbol or a string, either
1074 of which is simply printed. It can also be a list of two strings,
1075 which is printed as if the strings were concatenated. The first of
1076 the two strings is the actual completion, the second string serves as
1077 annotation.
1078
1079 The argument @var{common-substring} is the prefix that is common to
1080 all the completions. With normal Emacs completion, it is usually the
1081 same as the string that was completed. @code{display-completion-list}
1082 uses this to highlight text in the completion list for better visual
1083 feedback. This is not needed in the minibuffer; for minibuffer
1084 completion, you can pass @code{nil}.
1085
1086 This function is called by @code{minibuffer-completion-help}. The
1087 most common way to use it is together with
1088 @code{with-output-to-temp-buffer}, like this:
1089
1090 @example
1091 (with-output-to-temp-buffer "*Completions*"
1092 (display-completion-list
1093 (all-completions (buffer-string) my-alist)
1094 (buffer-string)))
1095 @end example
1096 @end defun
1097
1098 @defopt completion-auto-help
1099 If this variable is non-@code{nil}, the completion commands
1100 automatically display a list of possible completions whenever nothing
1101 can be completed because the next character is not uniquely determined.
1102 @end defopt
1103
1104 @defvar minibuffer-local-completion-map
1105 @code{completing-read} uses this value as the local keymap when an
1106 exact match of one of the completions is not required. By default, this
1107 keymap makes the following bindings:
1108
1109 @table @asis
1110 @item @kbd{?}
1111 @code{minibuffer-completion-help}
1112
1113 @item @key{SPC}
1114 @code{minibuffer-complete-word}
1115
1116 @item @key{TAB}
1117 @code{minibuffer-complete}
1118 @end table
1119
1120 @noindent
1121 with other characters bound as in @code{minibuffer-local-map}
1122 (@pxref{Definition of minibuffer-local-map}).
1123 @end defvar
1124
1125 @defvar minibuffer-local-must-match-map
1126 @code{completing-read} uses this value as the local keymap when an
1127 exact match of one of the completions is required. Therefore, no keys
1128 are bound to @code{exit-minibuffer}, the command that exits the
1129 minibuffer unconditionally. By default, this keymap makes the following
1130 bindings:
1131
1132 @table @asis
1133 @item @kbd{?}
1134 @code{minibuffer-completion-help}
1135
1136 @item @key{SPC}
1137 @code{minibuffer-complete-word}
1138
1139 @item @key{TAB}
1140 @code{minibuffer-complete}
1141
1142 @item @kbd{C-j}
1143 @code{minibuffer-complete-and-exit}
1144
1145 @item @key{RET}
1146 @code{minibuffer-complete-and-exit}
1147 @end table
1148
1149 @noindent
1150 with other characters bound as in @code{minibuffer-local-map}.
1151 @end defvar
1152
1153 @defvar minibuffer-local-filename-completion-map
1154 This is like @code{minibuffer-local-completion-map}
1155 except that it does not bind @key{SPC}. This keymap is used by the
1156 function @code{read-file-name}.
1157 @end defvar
1158
1159 @defvar minibuffer-local-filename-must-match-map
1160 This is like @code{minibuffer-local-must-match-map}
1161 except that it does not bind @key{SPC}. This keymap is used by the
1162 function @code{read-file-name}.
1163 @end defvar
1164
1165 @node High-Level Completion
1166 @subsection High-Level Completion Functions
1167
1168 This section describes the higher-level convenient functions for
1169 reading certain sorts of names with completion.
1170
1171 In most cases, you should not call these functions in the middle of a
1172 Lisp function. When possible, do all minibuffer input as part of
1173 reading the arguments for a command, in the @code{interactive}
1174 specification. @xref{Defining Commands}.
1175
1176 @defun read-buffer prompt &optional default require-match
1177 This function reads the name of a buffer and returns it as a string.
1178 The argument @var{default} is the default name to use, the value to
1179 return if the user exits with an empty minibuffer. If non-@code{nil},
1180 it should be a string, a list of strings, or a buffer. If it is
1181 a list, the default value is the first element of this list. It is
1182 mentioned in the prompt, but is not inserted in the minibuffer as
1183 initial input.
1184
1185 The argument @var{prompt} should be a string ending with a colon and a
1186 space. If @var{default} is non-@code{nil}, the function inserts it in
1187 @var{prompt} before the colon to follow the convention for reading from
1188 the minibuffer with a default value (@pxref{Programming Tips}).
1189
1190 The optional argument @var{require-match} has the same meaning as in
1191 @code{completing-read}. @xref{Minibuffer Completion}.
1192
1193 In the following example, the user enters @samp{minibuffer.t}, and
1194 then types @key{RET}. The argument @var{require-match} is @code{t},
1195 and the only buffer name starting with the given input is
1196 @samp{minibuffer.texi}, so that name is the value.
1197
1198 @example
1199 (read-buffer "Buffer name: " "foo" t)
1200 @group
1201 ;; @r{After evaluation of the preceding expression,}
1202 ;; @r{the following prompt appears,}
1203 ;; @r{with an empty minibuffer:}
1204 @end group
1205
1206 @group
1207 ---------- Buffer: Minibuffer ----------
1208 Buffer name (default foo): @point{}
1209 ---------- Buffer: Minibuffer ----------
1210 @end group
1211
1212 @group
1213 ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
1214 @result{} "minibuffer.texi"
1215 @end group
1216 @end example
1217 @end defun
1218
1219 @defopt read-buffer-function
1220 This variable specifies how to read buffer names. The function is
1221 called with the arguments passed to @code{read-buffer}. For example,
1222 if you set this variable to @code{iswitchb-read-buffer}, all Emacs
1223 commands that call @code{read-buffer} to read a buffer name will
1224 actually use the @code{iswitchb} package to read it.
1225 @end defopt
1226
1227 @defopt read-buffer-completion-ignore-case
1228 If this variable is non-@code{nil}, @code{read-buffer} ignores case
1229 when performing completion.
1230 @end defopt
1231
1232 @defun read-command prompt &optional default
1233 This function reads the name of a command and returns it as a Lisp
1234 symbol. The argument @var{prompt} is used as in
1235 @code{read-from-minibuffer}. Recall that a command is anything for
1236 which @code{commandp} returns @code{t}, and a command name is a symbol
1237 for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
1238
1239 The argument @var{default} specifies what to return if the user enters
1240 null input. It can be a symbol, a string or a list of strings. If it
1241 is a string, @code{read-command} interns it before returning it.
1242 If it is a list, @code{read-command} returns the first element of this list.
1243 If @var{default} is @code{nil}, that means no default has been
1244 specified; then if the user enters null input, the return value is
1245 @code{(intern "")}, that is, a symbol whose name is an empty string.
1246
1247 @example
1248 (read-command "Command name? ")
1249
1250 @group
1251 ;; @r{After evaluation of the preceding expression,}
1252 ;; @r{the following prompt appears with an empty minibuffer:}
1253 @end group
1254
1255 @group
1256 ---------- Buffer: Minibuffer ----------
1257 Command name?
1258 ---------- Buffer: Minibuffer ----------
1259 @end group
1260 @end example
1261
1262 @noindent
1263 If the user types @kbd{forward-c @key{RET}}, then this function returns
1264 @code{forward-char}.
1265
1266 The @code{read-command} function is a simplified interface to
1267 @code{completing-read}. It uses the variable @code{obarray} so as to
1268 complete in the set of extant Lisp symbols, and it uses the
1269 @code{commandp} predicate so as to accept only command names:
1270
1271 @cindex @code{commandp} example
1272 @example
1273 @group
1274 (read-command @var{prompt})
1275 @equiv{}
1276 (intern (completing-read @var{prompt} obarray
1277 'commandp t nil))
1278 @end group
1279 @end example
1280 @end defun
1281
1282 @defun read-variable prompt &optional default
1283 @anchor{Definition of read-variable}
1284 This function reads the name of a user variable and returns it as a
1285 symbol.
1286
1287 The argument @var{default} specifies the default value to return if
1288 the user enters null input. It can be a symbol, a string, or a list
1289 of strings. If it is a string, @code{read-variable} interns it to
1290 make the default value. If it is a list, @code{read-variable} interns
1291 the first element. If @var{default} is @code{nil}, that means no
1292 default has been specified; then if the user enters null input, the
1293 return value is @code{(intern "")}.
1294
1295 @example
1296 @group
1297 (read-variable "Variable name? ")
1298
1299 ;; @r{After evaluation of the preceding expression,}
1300 ;; @r{the following prompt appears,}
1301 ;; @r{with an empty minibuffer:}
1302 @end group
1303
1304 @group
1305 ---------- Buffer: Minibuffer ----------
1306 Variable name? @point{}
1307 ---------- Buffer: Minibuffer ----------
1308 @end group
1309 @end example
1310
1311 @noindent
1312 If the user then types @kbd{fill-p @key{RET}}, @code{read-variable}
1313 returns @code{fill-prefix}.
1314
1315 In general, @code{read-variable} is similar to @code{read-command},
1316 but uses the predicate @code{user-variable-p} instead of
1317 @code{commandp}:
1318
1319 @cindex @code{user-variable-p} example
1320 @example
1321 @group
1322 (read-variable @var{prompt})
1323 @equiv{}
1324 (intern
1325 (completing-read @var{prompt} obarray
1326 'user-variable-p t nil))
1327 @end group
1328 @end example
1329 @end defun
1330
1331 @deffn Command read-color &optional prompt convert allow-empty display
1332 This function reads a string that is a color specification, either the
1333 color's name or an RGB hex value such as @code{#RRRGGGBBB}. It
1334 prompts with @var{prompt} (default: @code{"Color (name or #RGB triplet):"})
1335 and provides completion for color names, but not for hex RGB values.
1336 In addition to names of standard colors, completion candidates include
1337 the foreground and background colors at point.
1338
1339 Valid RGB values are described in @ref{Color Names}.
1340
1341 The function's return value is the string typed by the user in the
1342 minibuffer. However, when called interactively or if the optional
1343 argument @var{convert} is non-@code{nil}, it converts any input color
1344 name into the corresponding RGB value string and instead returns that.
1345 This function requires a valid color specification to be input.
1346 Empty color names are allowed when @code{allow-empty} is
1347 non-@code{nil} and the user enters null input.
1348
1349 Interactively, or when @var{display} is non-@code{nil}, the return
1350 value is also displayed in the echo area.
1351 @end deffn
1352
1353 See also the functions @code{read-coding-system} and
1354 @code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems},
1355 and @code{read-input-method-name}, in @ref{Input Methods}.
1356
1357 @node Reading File Names
1358 @subsection Reading File Names
1359 @cindex read file names
1360 @cindex prompt for file name
1361
1362 The high-level completion functions @code{read-file-name},
1363 @code{read-directory-name}, and @code{read-shell-command} are designed
1364 to read file names, directory names, and shell commands respectively.
1365 They provide special features, including automatic insertion of the
1366 default directory.
1367
1368 @defun read-file-name prompt &optional directory default require-match initial predicate
1369 This function reads a file name, prompting with @var{prompt} and
1370 providing completion.
1371
1372 As an exception, this function reads a file name using a graphical
1373 file dialog instead of the minibuffer, if (i) it is invoked via a
1374 mouse command, and (ii) the selected frame is on a graphical display
1375 supporting such dialogs, and (iii) the variable @code{use-dialog-box}
1376 is non-@code{nil} (@pxref{Dialog Boxes,, Dialog Boxes, emacs, The GNU
1377 Emacs Manual}), and (iv) the @var{directory} argument, described
1378 below, does not specify a remote file (@pxref{Remote Files,, Remote
1379 Files, emacs, The GNU Emacs Manual}). The exact behavior when using a
1380 graphical file dialog is platform-dependent. Here, we simply document
1381 the behavior when using the minibuffer.
1382
1383 @code{read-file-name} does not automatically expand the returned file
1384 name. You must call @code{expand-file-name} yourself if an absolute
1385 file name is required.
1386
1387 The optional argument @var{require-match} has the same meaning as in
1388 @code{completing-read}. @xref{Minibuffer Completion}.
1389
1390 The argument @var{directory} specifies the directory to use for
1391 completing relative file names. It should be an absolute directory
1392 name. If @code{insert-default-directory} is non-@code{nil},
1393 @var{directory} is also inserted in the minibuffer as initial input.
1394 It defaults to the current buffer's value of @code{default-directory}.
1395
1396 If you specify @var{initial}, that is an initial file name to insert
1397 in the buffer (after @var{directory}, if that is inserted). In this
1398 case, point goes at the beginning of @var{initial}. The default for
1399 @var{initial} is @code{nil}---don't insert any file name. To see what
1400 @var{initial} does, try the command @kbd{C-x C-v}. @strong{Please
1401 note:} we recommend using @var{default} rather than @var{initial} in
1402 most cases.
1403
1404 If @var{default} is non-@code{nil}, then the function returns
1405 @var{default} if the user exits the minibuffer with the same non-empty
1406 contents that @code{read-file-name} inserted initially. The initial
1407 minibuffer contents are always non-empty if
1408 @code{insert-default-directory} is non-@code{nil}, as it is by
1409 default. @var{default} is not checked for validity, regardless of the
1410 value of @var{require-match}. However, if @var{require-match} is
1411 non-@code{nil}, the initial minibuffer contents should be a valid file
1412 (or directory) name. Otherwise @code{read-file-name} attempts
1413 completion if the user exits without any editing, and does not return
1414 @var{default}. @var{default} is also available through the history
1415 commands.
1416
1417 If @var{default} is @code{nil}, @code{read-file-name} tries to find a
1418 substitute default to use in its place, which it treats in exactly the
1419 same way as if it had been specified explicitly. If @var{default} is
1420 @code{nil}, but @var{initial} is non-@code{nil}, then the default is
1421 the absolute file name obtained from @var{directory} and
1422 @var{initial}. If both @var{default} and @var{initial} are @code{nil}
1423 and the buffer is visiting a file, @code{read-file-name} uses the
1424 absolute file name of that file as default. If the buffer is not
1425 visiting a file, then there is no default. In that case, if the user
1426 types @key{RET} without any editing, @code{read-file-name} simply
1427 returns the pre-inserted contents of the minibuffer.
1428
1429 If the user types @key{RET} in an empty minibuffer, this function
1430 returns an empty string, regardless of the value of
1431 @var{require-match}. This is, for instance, how the user can make the
1432 current buffer visit no file using @code{M-x set-visited-file-name}.
1433
1434 If @var{predicate} is non-@code{nil}, it specifies a function of one
1435 argument that decides which file names are acceptable completion
1436 alternatives. A file name is an acceptable value if @var{predicate}
1437 returns non-@code{nil} for it.
1438
1439 Here is an example of using @code{read-file-name}:
1440
1441 @example
1442 @group
1443 (read-file-name "The file is ")
1444
1445 ;; @r{After evaluation of the preceding expression,}
1446 ;; @r{the following appears in the minibuffer:}
1447 @end group
1448
1449 @group
1450 ---------- Buffer: Minibuffer ----------
1451 The file is /gp/gnu/elisp/@point{}
1452 ---------- Buffer: Minibuffer ----------
1453 @end group
1454 @end example
1455
1456 @noindent
1457 Typing @kbd{manual @key{TAB}} results in the following:
1458
1459 @example
1460 @group
1461 ---------- Buffer: Minibuffer ----------
1462 The file is /gp/gnu/elisp/manual.texi@point{}
1463 ---------- Buffer: Minibuffer ----------
1464 @end group
1465 @end example
1466
1467 @c Wordy to avoid overfull hbox in smallbook mode.
1468 @noindent
1469 If the user types @key{RET}, @code{read-file-name} returns the file name
1470 as the string @code{"/gp/gnu/elisp/manual.texi"}.
1471 @end defun
1472
1473 @defvar read-file-name-function
1474 If non-@code{nil}, this should be a function that accepts the same
1475 arguments as @code{read-file-name}. When @code{read-file-name} is
1476 called, it calls this function with the supplied arguments instead of
1477 doing its usual work.
1478 @end defvar
1479
1480 @defopt read-file-name-completion-ignore-case
1481 If this variable is non-@code{nil}, @code{read-file-name} ignores case
1482 when performing completion.
1483 @end defopt
1484
1485 @defun read-directory-name prompt &optional directory default require-match initial
1486 This function is like @code{read-file-name} but allows only directory
1487 names as completion alternatives.
1488
1489 If @var{default} is @code{nil} and @var{initial} is non-@code{nil},
1490 @code{read-directory-name} constructs a substitute default by
1491 combining @var{directory} (or the current buffer's default directory
1492 if @var{directory} is @code{nil}) and @var{initial}. If both
1493 @var{default} and @var{initial} are @code{nil}, this function uses
1494 @var{directory} as substitute default, or the current buffer's default
1495 directory if @var{directory} is @code{nil}.
1496 @end defun
1497
1498 @defopt insert-default-directory
1499 This variable is used by @code{read-file-name}, and thus, indirectly,
1500 by most commands reading file names. (This includes all commands that
1501 use the code letters @samp{f} or @samp{F} in their interactive form.
1502 @xref{Interactive Codes,, Code Characters for interactive}.) Its
1503 value controls whether @code{read-file-name} starts by placing the
1504 name of the default directory in the minibuffer, plus the initial file
1505 name if any. If the value of this variable is @code{nil}, then
1506 @code{read-file-name} does not place any initial input in the
1507 minibuffer (unless you specify initial input with the @var{initial}
1508 argument). In that case, the default directory is still used for
1509 completion of relative file names, but is not displayed.
1510
1511 If this variable is @code{nil} and the initial minibuffer contents are
1512 empty, the user may have to explicitly fetch the next history element
1513 to access a default value. If the variable is non-@code{nil}, the
1514 initial minibuffer contents are always non-empty and the user can
1515 always request a default value by immediately typing @key{RET} in an
1516 unedited minibuffer. (See above.)
1517
1518 For example:
1519
1520 @example
1521 @group
1522 ;; @r{Here the minibuffer starts out with the default directory.}
1523 (let ((insert-default-directory t))
1524 (read-file-name "The file is "))
1525 @end group
1526
1527 @group
1528 ---------- Buffer: Minibuffer ----------
1529 The file is ~lewis/manual/@point{}
1530 ---------- Buffer: Minibuffer ----------
1531 @end group
1532
1533 @group
1534 ;; @r{Here the minibuffer is empty and only the prompt}
1535 ;; @r{appears on its line.}
1536 (let ((insert-default-directory nil))
1537 (read-file-name "The file is "))
1538 @end group
1539
1540 @group
1541 ---------- Buffer: Minibuffer ----------
1542 The file is @point{}
1543 ---------- Buffer: Minibuffer ----------
1544 @end group
1545 @end example
1546 @end defopt
1547
1548 @defun read-shell-command prompt &optional initial-contents hist &rest args
1549 This function reads a shell command from the minibuffer, prompting
1550 with @var{prompt} and providing intelligent completion. It completes
1551 the first word of the command using candidates that are appropriate
1552 for command names, and the rest of the command words as file names.
1553
1554 This function uses @code{minibuffer-local-shell-command-map} as the
1555 keymap for minibuffer input. The @var{hist} argument specifies the
1556 history list to use; if is omitted or @code{nil}, it defaults to
1557 @code{shell-command-history} (@pxref{Minibuffer History,
1558 shell-command-history}). The optional argument @var{initial-contents}
1559 specifies the initial content of the minibuffer (@pxref{Initial
1560 Input}). The rest of @var{args}, if present, are used as the
1561 @var{default} and @var{inherit-input-method} arguments in
1562 @code{read-from-minibuffer} (@pxref{Text from Minibuffer}).
1563 @end defun
1564
1565 @defvar minibuffer-local-shell-command-map
1566 This keymap is used by @code{read-shell-command} for completing
1567 command and file names that are part of a shell command.
1568 @end defvar
1569
1570 @node Completion Variables
1571 @subsection Completion Variables
1572
1573 Here are some variables which can be used to alter the default
1574 completion behavior.
1575
1576 @cindex completion styles
1577 @defopt completion-styles
1578 The value of this variable is a list of completion style (symbols) to
1579 use for performing completion. A @dfn{completion style} is a set of
1580 rules for generating completions. Each symbol in occurring this list
1581 must have a corresponding entry in @code{completion-styles-alist}.
1582 @end defopt
1583
1584 @defvar completion-styles-alist
1585 This variable stores a list of available completion styles. Each
1586 element in the list has the form
1587
1588 @example
1589 (@var{style} @var{try-completion} @var{all-completions} @var{doc})
1590 @end example
1591
1592 @noindent
1593 Here, @var{style} is the name of the completion style (a symbol),
1594 which may be used in the @code{completion-styles} variable to refer to
1595 this style; @var{try-completion} is the function that does the
1596 completion; @var{all-completions} is the function that lists the
1597 completions; and @var{doc} is a string describing the completion
1598 style.
1599
1600 The @var{try-completion} and @var{all-completions} functions should
1601 each accept four arguments: @var{string}, @var{collection},
1602 @var{predicate}, and @var{point}. The @var{string}, @var{collection},
1603 and @var{predicate} arguments have the same meanings as in
1604 @code{try-completion} (@pxref{Basic Completion}), and the @var{point}
1605 argument is the position of point within @var{string}. Each function
1606 should return a non-@code{nil} value if it performed its job, and
1607 @code{nil} if it did not (e.g.@: if there is no way to complete
1608 @var{string} according to the completion style).
1609
1610 When the user calls a completion command like
1611 @code{minibuffer-complete} (@pxref{Completion Commands}), Emacs looks
1612 for the first style listed in @code{completion-styles} and calls its
1613 @var{try-completion} function. If this function returns @code{nil},
1614 Emacs moves to the next listed completion style and calls its
1615 @var{try-completion} function, and so on until one of the
1616 @var{try-completion} functions successfully performs completion and
1617 returns a non-@code{nil} value. A similar procedure is used for
1618 listing completions, via the @var{all-completions} functions.
1619
1620 @xref{Completion Styles,,, emacs, The GNU Emacs Manual}, for a
1621 description of the available completion styles.
1622 @end defvar
1623
1624 @defopt completion-category-overrides
1625 This variable specifies special completion styles and other completion
1626 behaviors to use when completing certain types of text. Its value
1627 should be a list of the form @code{(@var{category} . @var{alist})}.
1628 @var{category} is a symbol describing what is being completed;
1629 currently, the @code{buffer} and @code{file} categories are defined,
1630 but others can be defined via specialized completion functions
1631 (@pxref{Programmed Completion}). @var{alist} is an association list
1632 describing how completion should behave for the corresponding
1633 category. The following alist keys are supported:
1634
1635 @table @code
1636 @item styles
1637 The value should be a list of completion styles (symbols).
1638
1639 @item cycle
1640 The value should be a value for @code{completion-cycle-threshold}
1641 (@pxref{Completion Options,,, emacs, The GNU Emacs Manual}) for this
1642 category.
1643 @end table
1644
1645 @noindent
1646 Additional alist entries may be defined in the future.
1647 @end defopt
1648
1649 @defvar completion-extra-properties
1650 This variable is used to specify extra properties of the current
1651 completion command. It is intended to be let-bound by specialized
1652 completion commands. Its value should be a list of property and value
1653 pairs. The following properties are supported:
1654
1655 @table @code
1656 @item :annotation-function
1657 The value should be a function to add annotations in the completions
1658 buffer. This function must accept one argument, a completion, and
1659 should either return @code{nil} or a string to be displayed next to
1660 the completion.
1661
1662 @item :exit-function
1663 The value should be a function to run after performing completion.
1664 The function should accept two arguments, @var{string} and
1665 @var{status}, where @var{string} is the text to which the field was
1666 completed and @var{status} indicates what kind of operation happened:
1667 @code{finished} if text is now complete, @code{sole} if the text
1668 cannot be further completed but completion is not finished, or
1669 @code{exact} if the text is a valid completion but may be further
1670 completed.
1671 @end table
1672 @end defvar
1673
1674 @node Programmed Completion
1675 @subsection Programmed Completion
1676 @cindex programmed completion
1677
1678 Sometimes it is not possible or convenient to create an alist or
1679 an obarray containing all the intended possible completions ahead
1680 of time. In such a case, you can supply your own function to compute
1681 the completion of a given string. This is called @dfn{programmed
1682 completion}. Emacs uses programmed completion when completing file
1683 names (@pxref{File Name Completion}), among many other cases.
1684
1685 To use this feature, pass a function as the @var{collection}
1686 argument to @code{completing-read}. The function
1687 @code{completing-read} arranges to pass your completion function along
1688 to @code{try-completion}, @code{all-completions}, and other basic
1689 completion functions, which will then let your function do all
1690 the work.
1691
1692 The completion function should accept three arguments:
1693
1694 @itemize @bullet
1695 @item
1696 The string to be completed.
1697
1698 @item
1699 A predicate function with which to filter possible matches, or
1700 @code{nil} if none. The function should call the predicate for each
1701 possible match, and ignore the match if the predicate returns
1702 @code{nil}.
1703
1704 @item
1705 A flag specifying the type of completion operation to perform. This
1706 is one of the following four values:
1707
1708 @table @code
1709 @item nil
1710 This specifies a @code{try-completion} operation. The function should
1711 return @code{t} if the specified string is a unique and exact match;
1712 if there is more than one match, it should return the common substring
1713 of all matches (if the string is an exact match for one completion
1714 alternative but also matches other longer alternatives, the return
1715 value is the string); if there are no matches, it should return
1716 @code{nil}.
1717
1718 @item t
1719 This specifies an @code{all-completions} operation. The function
1720 should return a list of all possible completions of the specified
1721 string.
1722
1723 @item lambda
1724 This specifies a @code{test-completion} operation. The function
1725 should return @code{t} if the specified string is an exact match for
1726 some completion alternative; @code{nil} otherwise.
1727
1728 @item (boundaries . @var{suffix})
1729 This specifies a @code{completion-boundaries} operation. The function
1730 should return @code{(boundaries START . END)}, where START is the
1731 position of the beginning boundary in the specified string, and END is
1732 the position of the end boundary in SUFFIX.
1733
1734 @item metadata
1735 This specifies a request for information about the state of the
1736 current completion. The function should return an alist, as described
1737 below. The alist may contain any number of elements.
1738 @end table
1739
1740 @noindent
1741 If the flag has any other value, the completion function should return
1742 @code{nil}.
1743 @end itemize
1744
1745 The following is a list of metadata entries that a completion function
1746 may return in response to a @code{metadata} flag argument:
1747
1748 @table @code
1749 @item category
1750 The value should be a symbol describing what kind of text the
1751 completion function is trying to complete. If the symbol matches one
1752 of the keys in @code{completion-category-overrides}, the usual
1753 completion behavior is overridden. @xref{Completion Variables}.
1754
1755 @item annotation-function
1756 The value should be a function for @dfn{annotating} completions. The
1757 function should take one argument, @var{string}, which is a possible
1758 completion. It should return a string, which is displayed after the
1759 completion @var{string} in the @file{*Completions*} buffer.
1760
1761 @item display-sort-function
1762 The value should be a function for sorting completions. The function
1763 should take one argument, a list of completion strings, and return a
1764 sorted list of completion strings. It is allowed to alter the input
1765 list destructively.
1766
1767 @item cycle-sort-function
1768 The value should be a function for sorting completions, when
1769 @code{completion-cycle-threshold} is non-@code{nil} and the user is
1770 cycling through completion alternatives. @xref{Completion Options,,,
1771 emacs, The GNU Emacs Manual}. Its argument list and return value are
1772 the same as for @code{display-sort-function}.
1773 @end table
1774
1775 @defun completion-table-dynamic function
1776 This function is a convenient way to write a function that can act as
1777 programmed completion function. The argument @var{function} should be
1778 a function that takes one argument, a string, and returns an alist of
1779 possible completions of it. You can think of
1780 @code{completion-table-dynamic} as a transducer between that interface
1781 and the interface for programmed completion functions.
1782 @end defun
1783
1784 @node Completion in Buffers
1785 @subsection Completion in Ordinary Buffers
1786 @cindex inline completion
1787
1788 @findex completion-at-point
1789 Although completion is usually done in the minibuffer, the
1790 completion facility can also be used on the text in ordinary Emacs
1791 buffers. In many major modes, in-buffer completion is performed by
1792 the @kbd{C-M-i} or @kbd{M-@key{TAB}} command, bound to
1793 @code{completion-at-point}. @xref{Symbol Completion,,, emacs, The GNU
1794 Emacs Manual}. This command uses the abnormal hook variable
1795 @code{completion-at-point-functions}:
1796
1797 @defvar completion-at-point-functions
1798 The value of this abnormal hook should be a list of functions, which
1799 are used to compute a completion table for completing the text at
1800 point. It can be used by major modes to provide mode-specific
1801 completion tables (@pxref{Major Mode Conventions}).
1802
1803 When the command @code{completion-at-point} runs, it calls the
1804 functions in the list one by one, without any argument. Each function
1805 should return @code{nil} if it is unable to produce a completion table
1806 for the text at point. Otherwise it should return a list of the form
1807
1808 @example
1809 (@var{start} @var{end} @var{collection} . @var{props})
1810 @end example
1811
1812 @noindent
1813 @var{start} and @var{end} delimit the text to complete (which should
1814 enclose point). @var{collection} is a completion table for completing
1815 that text, in a form suitable for passing as the second argument to
1816 @code{try-completion} (@pxref{Basic Completion}); completion
1817 alternatives will be generated from this completion table in the usual
1818 way, via the completion styles defined in @code{completion-styles}
1819 (@pxref{Completion Variables}). @var{props} is a property list for
1820 additional information; any of the properties in
1821 @code{completion-extra-properties} are recognized (@pxref{Completion
1822 Variables}), as well as the following additional ones:
1823
1824 @table @code
1825 @item :predicate
1826 The value should be a predicate that completion candidates need to
1827 satisfy.
1828
1829 @item :exclusive
1830 If the value is @code{no}, then if the completion table fails to match
1831 the text at point, then @code{completion-at-point} moves on to the
1832 next function in @code{completion-at-point-functions} instead of
1833 reporting a completion failure.
1834 @end table
1835
1836 A function in @code{completion-at-point-functions} may also return a
1837 function. In that case, that returned function is called, with no
1838 argument, and it is entirely responsible for performing the
1839 completion. We discourage this usage; it is intended to help convert
1840 old code to using @code{completion-at-point}.
1841
1842 The first function in @code{completion-at-point-functions} to return a
1843 non-@code{nil} value is used by @code{completion-at-point}. The
1844 remaining functions are not called. The exception to this is when
1845 there is a @code{:exclusive} specification, as described above.
1846 @end defvar
1847
1848 The following function provides a convenient way to perform
1849 completion on an arbitrary stretch of text in an Emacs buffer:
1850
1851 @defun completion-in-region start end collection &optional predicate
1852 This function completes the text in the current buffer between the
1853 positions @var{start} and @var{end}, using @var{collection}. The
1854 argument @var{collection} has the same meaning as in
1855 @code{try-completion} (@pxref{Basic Completion}).
1856
1857 This function inserts the completion text directly into the current
1858 buffer. Unlike @code{completing-read} (@pxref{Minibuffer
1859 Completion}), it does not activate the minibuffer.
1860
1861 For this function to work, point must be somewhere between @var{start}
1862 and @var{end}.
1863 @end defun
1864
1865
1866 @node Yes-or-No Queries
1867 @section Yes-or-No Queries
1868 @cindex asking the user questions
1869 @cindex querying the user
1870 @cindex yes-or-no questions
1871
1872 This section describes functions used to ask the user a yes-or-no
1873 question. The function @code{y-or-n-p} can be answered with a single
1874 character; it is useful for questions where an inadvertent wrong answer
1875 will not have serious consequences. @code{yes-or-no-p} is suitable for
1876 more momentous questions, since it requires three or four characters to
1877 answer.
1878
1879 If either of these functions is called in a command that was invoked
1880 using the mouse---more precisely, if @code{last-nonmenu-event}
1881 (@pxref{Command Loop Info}) is either @code{nil} or a list---then it
1882 uses a dialog box or pop-up menu to ask the question. Otherwise, it
1883 uses keyboard input. You can force use of the mouse or use of keyboard
1884 input by binding @code{last-nonmenu-event} to a suitable value around
1885 the call.
1886
1887 Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
1888 @code{y-or-n-p} does not; but it seems best to describe them together.
1889
1890 @defun y-or-n-p prompt
1891 This function asks the user a question, expecting input in the echo
1892 area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
1893 user types @kbd{n}. This function also accepts @key{SPC} to mean yes
1894 and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit,'' like
1895 @kbd{C-g}, because the question might look like a minibuffer and for
1896 that reason the user might try to use @kbd{C-]} to get out. The answer
1897 is a single character, with no @key{RET} needed to terminate it. Upper
1898 and lower case are equivalent.
1899
1900 ``Asking the question'' means printing @var{prompt} in the echo area,
1901 followed by the string @w{@samp{(y or n) }}. If the input is not one of
1902 the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
1903 @kbd{@key{DEL}}, or something that quits), the function responds
1904 @samp{Please answer y or n.}, and repeats the request.
1905
1906 This function does not actually use the minibuffer, since it does not
1907 allow editing of the answer. It actually uses the echo area (@pxref{The
1908 Echo Area}), which uses the same screen space as the minibuffer. The
1909 cursor moves to the echo area while the question is being asked.
1910
1911 The answers and their meanings, even @samp{y} and @samp{n}, are not
1912 hardwired. The keymap @code{query-replace-map} specifies them.
1913 @xref{Search and Replace}.
1914
1915 In the following example, the user first types @kbd{q}, which is
1916 invalid. At the next prompt the user types @kbd{y}.
1917
1918 @smallexample
1919 @group
1920 (y-or-n-p "Do you need a lift? ")
1921
1922 ;; @r{After evaluation of the preceding expression,}
1923 ;; @r{the following prompt appears in the echo area:}
1924 @end group
1925
1926 @group
1927 ---------- Echo area ----------
1928 Do you need a lift? (y or n)
1929 ---------- Echo area ----------
1930 @end group
1931
1932 ;; @r{If the user then types @kbd{q}, the following appears:}
1933
1934 @group
1935 ---------- Echo area ----------
1936 Please answer y or n. Do you need a lift? (y or n)
1937 ---------- Echo area ----------
1938 @end group
1939
1940 ;; @r{When the user types a valid answer,}
1941 ;; @r{it is displayed after the question:}
1942
1943 @group
1944 ---------- Echo area ----------
1945 Do you need a lift? (y or n) y
1946 ---------- Echo area ----------
1947 @end group
1948 @end smallexample
1949
1950 @noindent
1951 We show successive lines of echo area messages, but only one actually
1952 appears on the screen at a time.
1953 @end defun
1954
1955 @defun y-or-n-p-with-timeout prompt seconds default-value
1956 Like @code{y-or-n-p}, except that if the user fails to answer within
1957 @var{seconds} seconds, this function stops waiting and returns
1958 @var{default-value}. It works by setting up a timer; see @ref{Timers}.
1959 The argument @var{seconds} may be an integer or a floating point number.
1960 @end defun
1961
1962 @defun yes-or-no-p prompt
1963 This function asks the user a question, expecting input in the
1964 minibuffer. It returns @code{t} if the user enters @samp{yes},
1965 @code{nil} if the user types @samp{no}. The user must type @key{RET} to
1966 finalize the response. Upper and lower case are equivalent.
1967
1968 @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
1969 followed by @w{@samp{(yes or no) }}. The user must type one of the
1970 expected responses; otherwise, the function responds @samp{Please answer
1971 yes or no.}, waits about two seconds and repeats the request.
1972
1973 @code{yes-or-no-p} requires more work from the user than
1974 @code{y-or-n-p} and is appropriate for more crucial decisions.
1975
1976 Here is an example:
1977
1978 @smallexample
1979 @group
1980 (yes-or-no-p "Do you really want to remove everything? ")
1981
1982 ;; @r{After evaluation of the preceding expression,}
1983 ;; @r{the following prompt appears,}
1984 ;; @r{with an empty minibuffer:}
1985 @end group
1986
1987 @group
1988 ---------- Buffer: minibuffer ----------
1989 Do you really want to remove everything? (yes or no)
1990 ---------- Buffer: minibuffer ----------
1991 @end group
1992 @end smallexample
1993
1994 @noindent
1995 If the user first types @kbd{y @key{RET}}, which is invalid because this
1996 function demands the entire word @samp{yes}, it responds by displaying
1997 these prompts, with a brief pause between them:
1998
1999 @smallexample
2000 @group
2001 ---------- Buffer: minibuffer ----------
2002 Please answer yes or no.
2003 Do you really want to remove everything? (yes or no)
2004 ---------- Buffer: minibuffer ----------
2005 @end group
2006 @end smallexample
2007 @end defun
2008
2009 @node Multiple Queries
2010 @section Asking Multiple Y-or-N Questions
2011
2012 When you have a series of similar questions to ask, such as ``Do you
2013 want to save this buffer'' for each buffer in turn, you should use
2014 @code{map-y-or-n-p} to ask the collection of questions, rather than
2015 asking each question individually. This gives the user certain
2016 convenient facilities such as the ability to answer the whole series at
2017 once.
2018
2019 @defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
2020 This function asks the user a series of questions, reading a
2021 single-character answer in the echo area for each one.
2022
2023 The value of @var{list} specifies the objects to ask questions about.
2024 It should be either a list of objects or a generator function. If it is
2025 a function, it should expect no arguments, and should return either the
2026 next object to ask about, or @code{nil} meaning stop asking questions.
2027
2028 The argument @var{prompter} specifies how to ask each question. If
2029 @var{prompter} is a string, the question text is computed like this:
2030
2031 @example
2032 (format @var{prompter} @var{object})
2033 @end example
2034
2035 @noindent
2036 where @var{object} is the next object to ask about (as obtained from
2037 @var{list}).
2038
2039 If not a string, @var{prompter} should be a function of one argument
2040 (the next object to ask about) and should return the question text. If
2041 the value is a string, that is the question to ask the user. The
2042 function can also return @code{t} meaning do act on this object (and
2043 don't ask the user), or @code{nil} meaning ignore this object (and don't
2044 ask the user).
2045
2046 The argument @var{actor} says how to act on the answers that the user
2047 gives. It should be a function of one argument, and it is called with
2048 each object that the user says yes for. Its argument is always an
2049 object obtained from @var{list}.
2050
2051 If the argument @var{help} is given, it should be a list of this form:
2052
2053 @example
2054 (@var{singular} @var{plural} @var{action})
2055 @end example
2056
2057 @noindent
2058 where @var{singular} is a string containing a singular noun that
2059 describes the objects conceptually being acted on, @var{plural} is the
2060 corresponding plural noun, and @var{action} is a transitive verb
2061 describing what @var{actor} does.
2062
2063 If you don't specify @var{help}, the default is @code{("object"
2064 "objects" "act on")}.
2065
2066 Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
2067 @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
2068 that object; @kbd{!} to act on all following objects; @key{ESC} or
2069 @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
2070 the current object and then exit; or @kbd{C-h} to get help. These are
2071 the same answers that @code{query-replace} accepts. The keymap
2072 @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
2073 as well as for @code{query-replace}; see @ref{Search and Replace}.
2074
2075 You can use @var{action-alist} to specify additional possible answers
2076 and what they mean. It is an alist of elements of the form
2077 @code{(@var{char} @var{function} @var{help})}, each of which defines one
2078 additional answer. In this element, @var{char} is a character (the
2079 answer); @var{function} is a function of one argument (an object from
2080 @var{list}); @var{help} is a string.
2081
2082 When the user responds with @var{char}, @code{map-y-or-n-p} calls
2083 @var{function}. If it returns non-@code{nil}, the object is considered
2084 ``acted upon,'' and @code{map-y-or-n-p} advances to the next object in
2085 @var{list}. If it returns @code{nil}, the prompt is repeated for the
2086 same object.
2087
2088 Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
2089 prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
2090 does not do that.
2091
2092 If @code{map-y-or-n-p} is called in a command that was invoked using the
2093 mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
2094 Loop Info}) is either @code{nil} or a list---then it uses a dialog box
2095 or pop-up menu to ask the question. In this case, it does not use
2096 keyboard input or the echo area. You can force use of the mouse or use
2097 of keyboard input by binding @code{last-nonmenu-event} to a suitable
2098 value around the call.
2099
2100 The return value of @code{map-y-or-n-p} is the number of objects acted on.
2101 @end defun
2102
2103 @node Reading a Password
2104 @section Reading a Password
2105 @cindex passwords, reading
2106
2107 To read a password to pass to another program, you can use the
2108 function @code{read-passwd}.
2109
2110 @defun read-passwd prompt &optional confirm default
2111 This function reads a password, prompting with @var{prompt}. It does
2112 not echo the password as the user types it; instead, it echoes @samp{.}
2113 for each character in the password.
2114
2115 The optional argument @var{confirm}, if non-@code{nil}, says to read the
2116 password twice and insist it must be the same both times. If it isn't
2117 the same, the user has to type it over and over until the last two
2118 times match.
2119
2120 The optional argument @var{default} specifies the default password to
2121 return if the user enters empty input. If @var{default} is @code{nil},
2122 then @code{read-passwd} returns the null string in that case.
2123 @end defun
2124
2125 @node Minibuffer Commands
2126 @section Minibuffer Commands
2127
2128 This section describes some commands meant for use in the
2129 minibuffer.
2130
2131 @deffn Command exit-minibuffer
2132 This command exits the active minibuffer. It is normally bound to
2133 keys in minibuffer local keymaps.
2134 @end deffn
2135
2136 @deffn Command self-insert-and-exit
2137 This command exits the active minibuffer after inserting the last
2138 character typed on the keyboard (found in @code{last-command-event};
2139 @pxref{Command Loop Info}).
2140 @end deffn
2141
2142 @deffn Command previous-history-element n
2143 This command replaces the minibuffer contents with the value of the
2144 @var{n}th previous (older) history element.
2145 @end deffn
2146
2147 @deffn Command next-history-element n
2148 This command replaces the minibuffer contents with the value of the
2149 @var{n}th more recent history element.
2150 @end deffn
2151
2152 @deffn Command previous-matching-history-element pattern n
2153 This command replaces the minibuffer contents with the value of the
2154 @var{n}th previous (older) history element that matches @var{pattern} (a
2155 regular expression).
2156 @end deffn
2157
2158 @deffn Command next-matching-history-element pattern n
2159 This command replaces the minibuffer contents with the value of the
2160 @var{n}th next (newer) history element that matches @var{pattern} (a
2161 regular expression).
2162 @end deffn
2163
2164 @node Minibuffer Windows
2165 @section Minibuffer Windows
2166 @cindex minibuffer windows
2167
2168 These functions access and select minibuffer windows
2169 and test whether they are active.
2170
2171 @defun active-minibuffer-window
2172 This function returns the currently active minibuffer window, or
2173 @code{nil} if none is currently active.
2174 @end defun
2175
2176 @defun minibuffer-window &optional frame
2177 @anchor{Definition of minibuffer-window}
2178 This function returns the minibuffer window used for frame @var{frame}.
2179 If @var{frame} is @code{nil}, that stands for the current frame. Note
2180 that the minibuffer window used by a frame need not be part of that
2181 frame---a frame that has no minibuffer of its own necessarily uses some
2182 other frame's minibuffer window.
2183 @end defun
2184
2185 @defun set-minibuffer-window window
2186 This function specifies @var{window} as the minibuffer window to use.
2187 This affects where the minibuffer is displayed if you put text in it
2188 without invoking the usual minibuffer commands. It has no effect on
2189 the usual minibuffer input functions because they all start by
2190 choosing the minibuffer window according to the current frame.
2191 @end defun
2192
2193 @c Emacs 19 feature
2194 @defun window-minibuffer-p &optional window
2195 This function returns non-@code{nil} if @var{window} is a minibuffer
2196 window.
2197 @var{window} defaults to the selected window.
2198 @end defun
2199
2200 It is not correct to determine whether a given window is a minibuffer by
2201 comparing it with the result of @code{(minibuffer-window)}, because
2202 there can be more than one minibuffer window if there is more than one
2203 frame.
2204
2205 @defun minibuffer-window-active-p window
2206 This function returns non-@code{nil} if @var{window}, assumed to be
2207 a minibuffer window, is currently active.
2208 @end defun
2209
2210 @node Minibuffer Contents
2211 @section Minibuffer Contents
2212
2213 These functions access the minibuffer prompt and contents.
2214
2215 @defun minibuffer-prompt
2216 This function returns the prompt string of the currently active
2217 minibuffer. If no minibuffer is active, it returns @code{nil}.
2218 @end defun
2219
2220 @defun minibuffer-prompt-end
2221 This function returns the current
2222 position of the end of the minibuffer prompt, if a minibuffer is
2223 current. Otherwise, it returns the minimum valid buffer position.
2224 @end defun
2225
2226 @defun minibuffer-prompt-width
2227 This function returns the current display-width of the minibuffer
2228 prompt, if a minibuffer is current. Otherwise, it returns zero.
2229 @end defun
2230
2231 @defun minibuffer-contents
2232 This function returns the editable
2233 contents of the minibuffer (that is, everything except the prompt) as
2234 a string, if a minibuffer is current. Otherwise, it returns the
2235 entire contents of the current buffer.
2236 @end defun
2237
2238 @defun minibuffer-contents-no-properties
2239 This is like @code{minibuffer-contents}, except that it does not copy text
2240 properties, just the characters themselves. @xref{Text Properties}.
2241 @end defun
2242
2243 @defun minibuffer-completion-contents
2244 This is like @code{minibuffer-contents}, except that it returns only
2245 the contents before point. That is the part that completion commands
2246 operate on. @xref{Minibuffer Completion}.
2247 @end defun
2248
2249 @defun delete-minibuffer-contents
2250 This function erases the editable contents of the minibuffer (that is,
2251 everything except the prompt), if a minibuffer is current. Otherwise,
2252 it erases the entire current buffer.
2253 @end defun
2254
2255 @node Recursive Mini
2256 @section Recursive Minibuffers
2257 @cindex recursive minibuffers
2258
2259 These functions and variables deal with recursive minibuffers
2260 (@pxref{Recursive Editing}):
2261
2262 @defun minibuffer-depth
2263 This function returns the current depth of activations of the
2264 minibuffer, a nonnegative integer. If no minibuffers are active, it
2265 returns zero.
2266 @end defun
2267
2268 @defopt enable-recursive-minibuffers
2269 If this variable is non-@code{nil}, you can invoke commands (such as
2270 @code{find-file}) that use minibuffers even while the minibuffer window
2271 is active. Such invocation produces a recursive editing level for a new
2272 minibuffer. The outer-level minibuffer is invisible while you are
2273 editing the inner one.
2274
2275 If this variable is @code{nil}, you cannot invoke minibuffer
2276 commands when the minibuffer window is active, not even if you switch to
2277 another window to do it.
2278 @end defopt
2279
2280 @c Emacs 19 feature
2281 If a command name has a property @code{enable-recursive-minibuffers}
2282 that is non-@code{nil}, then the command can use the minibuffer to read
2283 arguments even if it is invoked from the minibuffer. A command can
2284 also achieve this by binding @code{enable-recursive-minibuffers}
2285 to @code{t} in the interactive declaration (@pxref{Using Interactive}).
2286 The minibuffer command @code{next-matching-history-element} (normally
2287 @kbd{M-s} in the minibuffer) does the latter.
2288
2289 @node Minibuffer Misc
2290 @section Minibuffer Miscellany
2291
2292 @defun minibufferp &optional buffer-or-name
2293 This function returns non-@code{nil} if @var{buffer-or-name} is a
2294 minibuffer. If @var{buffer-or-name} is omitted, it tests the current
2295 buffer.
2296 @end defun
2297
2298 @defvar minibuffer-setup-hook
2299 This is a normal hook that is run whenever the minibuffer is entered.
2300 @xref{Hooks}.
2301 @end defvar
2302
2303 @defvar minibuffer-exit-hook
2304 This is a normal hook that is run whenever the minibuffer is exited.
2305 @xref{Hooks}.
2306 @end defvar
2307
2308 @defvar minibuffer-help-form
2309 @anchor{Definition of minibuffer-help-form}
2310 The current value of this variable is used to rebind @code{help-form}
2311 locally inside the minibuffer (@pxref{Help Functions}).
2312 @end defvar
2313
2314 @defvar minibuffer-scroll-window
2315 @anchor{Definition of minibuffer-scroll-window}
2316 If the value of this variable is non-@code{nil}, it should be a window
2317 object. When the function @code{scroll-other-window} is called in the
2318 minibuffer, it scrolls this window.
2319 @end defvar
2320
2321 @defun minibuffer-selected-window
2322 This function returns the window which was selected when the
2323 minibuffer was entered. If selected window is not a minibuffer
2324 window, it returns @code{nil}.
2325 @end defun
2326
2327 @defopt max-mini-window-height
2328 This variable specifies the maximum height for resizing minibuffer
2329 windows. If a float, it specifies a fraction of the height of the
2330 frame. If an integer, it specifies a number of lines.
2331 @end defopt
2332
2333 @defun minibuffer-message string &rest args
2334 This function displays @var{string} temporarily at the end of the
2335 minibuffer text, for two seconds, or until the next input event
2336 arrives, whichever comes first. If @var{args} is non-@code{nil}, the
2337 actual message is obtained by passing @var{string} and @var{args}
2338 through @code{format}. @xref{Formatting Strings}.
2339 @end defun