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