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