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