* xdisp.c (redisplay_window): Likewise.
[bpt/emacs.git] / src / minibuf.c
CommitLineData
f927c5ae 1/* Minibuffer input and completion.
c11136ec 2
73b0cd50 3Copyright (C) 1985-1986, 1993-2011 Free Software Foundation, Inc.
f927c5ae
JB
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
f927c5ae 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
f927c5ae
JB
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f927c5ae
JB
19
20
18160b98 21#include <config.h>
3f708f4c 22#include <stdio.h>
d7306fe6 23#include <setjmp.h>
af52c32d 24
f927c5ae
JB
25#include "lisp.h"
26#include "commands.h"
27#include "buffer.h"
83be827a 28#include "character.h"
f927c5ae 29#include "dispextern.h"
2538fae4 30#include "keyboard.h"
ff11dfa1 31#include "frame.h"
f927c5ae
JB
32#include "window.h"
33#include "syntax.h"
0c21eeeb 34#include "intervals.h"
8feddab4 35#include "keymap.h"
fa971ac3 36#include "termhooks.h"
f927c5ae 37
f927c5ae 38/* List of buffers for use as minibuffers.
4d04c1f1
KH
39 The first element of the list is used for the outermost minibuffer
40 invocation, the next element is used for a recursive minibuffer
41 invocation, etc. The list is extended at the end as deeper
42 minibuffer recursions are encountered. */
279cf52c 43
f927c5ae
JB
44Lisp_Object Vminibuffer_list;
45
4d04c1f1 46/* Data to remember during recursive minibuffer invocations */
279cf52c 47
4d04c1f1 48Lisp_Object minibuf_save_list;
f927c5ae
JB
49
50/* Depth in minibuffer invocations. */
279cf52c 51
f927c5ae
JB
52int minibuf_level;
53
77aa8edf 54/* The maximum length of a minibuffer history. */
279cf52c 55
29208e82 56Lisp_Object Qhistory_length;
179a4ca7 57
b278606c 58/* Fread_minibuffer leaves the input here as a string. */
279cf52c 59
f927c5ae
JB
60Lisp_Object last_minibuf_string;
61
406e55df 62Lisp_Object Qminibuffer_history, Qbuffer_name_history;
770970cb 63
719b4a40
RS
64Lisp_Object Qread_file_name_internal;
65
177aecf9 66/* Normal hooks for entry to and exit from minibuffer. */
5c781212 67
29208e82
TT
68Lisp_Object Qminibuffer_setup_hook;
69Lisp_Object Qminibuffer_exit_hook;
5c781212 70
9fa5c213 71Lisp_Object Qcompletion_ignore_case;
29208e82
TT
72Lisp_Object Qminibuffer_completion_table;
73Lisp_Object Qminibuffer_completion_predicate;
74Lisp_Object Qminibuffer_completion_confirm;
5744fa7c
RS
75Lisp_Object Quser_variable_p;
76
77Lisp_Object Qminibuffer_default;
78
cee54539
KH
79Lisp_Object Qcurrent_input_method, Qactivate_input_method;
80
6b61353c
KH
81Lisp_Object Qcase_fold_search;
82
627fb581
RS
83Lisp_Object Qread_expression_history;
84
e9588e2e
PE
85/* Prompt to display in front of the mini-buffer contents. */
86
87static Lisp_Object minibuf_prompt;
88
89/* Width of current mini-buffer prompt. Only set after display_line
90 of the line that contains the prompt. */
91
92static int minibuf_prompt_width;
93
f927c5ae 94\f
89fdc4a0
KH
95/* Put minibuf on currently selected frame's minibuffer.
96 We do this whenever the user starts a new minibuffer
97 or when a minibuffer exits. */
98
99void
971de7fb 100choose_minibuf_frame (void)
89fdc4a0 101{
a4aafc54
GM
102 if (FRAMEP (selected_frame)
103 && FRAME_LIVE_P (XFRAME (selected_frame))
104 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
89fdc4a0 105 {
a4aafc54
GM
106 struct frame *sf = XFRAME (selected_frame);
107 Lisp_Object buffer;
9ab4a725 108
89fdc4a0
KH
109 /* I don't think that any frames may validly have a null minibuffer
110 window anymore. */
a4aafc54 111 if (NILP (sf->minibuffer_window))
89fdc4a0
KH
112 abort ();
113
a4aafc54
GM
114 /* Under X, we come here with minibuf_window being the
115 minibuffer window of the unused termcap window created in
116 init_window_once. That window doesn't have a buffer. */
117 buffer = XWINDOW (minibuf_window)->buffer;
118 if (BUFFERP (buffer))
cbbfe056 119 Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
a4aafc54 120 minibuf_window = sf->minibuffer_window;
89fdc4a0 121 }
914860c2
RS
122
123 /* Make sure no other frame has a minibuffer as its selected window,
124 because the text would not be displayed in it, and that would be
07d402c8
RS
125 confusing. Only allow the selected frame to do this,
126 and that only if the minibuffer is active. */
914860c2
RS
127 {
128 Lisp_Object tail, frame;
129
130 FOR_EACH_FRAME (tail, frame)
07d402c8 131 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
a4aafc54 132 && !(EQ (frame, selected_frame)
07d402c8 133 && minibuf_level > 0))
c8938ab6 134 Fset_frame_selected_window (frame, Fframe_first_window (frame), Qnil);
914860c2 135 }
89fdc4a0 136}
68313ed8 137
0c94f256 138Lisp_Object
971de7fb 139choose_minibuf_frame_1 (Lisp_Object ignore)
0c94f256
RS
140{
141 choose_minibuf_frame ();
142 return Qnil;
143}
144
68313ed8
RS
145DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
146 Sset_minibuffer_window, 1, 1, 0,
af52c32d 147 doc: /* Specify which minibuffer window to use for the minibuffer.
6b61353c 148This affects where the minibuffer is displayed if you put text in it
af52c32d 149without invoking the usual minibuffer commands. */)
5842a27b 150 (Lisp_Object window)
68313ed8 151{
b7826503 152 CHECK_WINDOW (window);
68313ed8
RS
153 if (! MINI_WINDOW_P (XWINDOW (window)))
154 error ("Window is not a minibuffer window");
155
156 minibuf_window = window;
157
158 return window;
159}
160
89fdc4a0 161\f
f927c5ae
JB
162/* Actual minibuffer invocation. */
163
f57e2426
J
164static Lisp_Object read_minibuf_unwind (Lisp_Object);
165static Lisp_Object run_exit_minibuf_hook (Lisp_Object);
166static Lisp_Object read_minibuf (Lisp_Object, Lisp_Object,
167 Lisp_Object, Lisp_Object,
168 int, Lisp_Object,
169 Lisp_Object, Lisp_Object,
170 int, int);
171static Lisp_Object read_minibuf_noninteractive (Lisp_Object, Lisp_Object,
172 Lisp_Object, Lisp_Object,
173 int, Lisp_Object,
174 Lisp_Object, Lisp_Object,
175 int, int);
176static Lisp_Object string_to_object (Lisp_Object, Lisp_Object);
edfef199
GM
177
178
179/* Read a Lisp object from VAL and return it. If VAL is an empty
180 string, and DEFALT is a string, read from DEFALT instead of VAL. */
181
182static Lisp_Object
971de7fb 183string_to_object (Lisp_Object val, Lisp_Object defalt)
edfef199
GM
184{
185 struct gcpro gcpro1, gcpro2;
186 Lisp_Object expr_and_pos;
da43f021 187 EMACS_INT pos;
9ab4a725 188
edfef199 189 GCPRO2 (val, defalt);
9ab4a725 190
fb30dfd2
JL
191 if (STRINGP (val) && SCHARS (val) == 0)
192 {
193 if (STRINGP (defalt))
194 val = defalt;
195 else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
196 val = XCAR (defalt);
197 }
9ab4a725 198
edfef199
GM
199 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
200 pos = XINT (Fcdr (expr_and_pos));
d5db4077 201 if (pos != SCHARS (val))
edfef199
GM
202 {
203 /* Ignore trailing whitespace; any other trailing junk
204 is an error. */
da43f021 205 EMACS_INT i;
edfef199 206 pos = string_char_to_byte (val, pos);
d5db4077 207 for (i = pos; i < SBYTES (val); i++)
edfef199 208 {
d5db4077 209 int c = SREF (val, i);
edfef199
GM
210 if (c != ' ' && c != '\t' && c != '\n')
211 error ("Trailing garbage following expression");
212 }
213 }
9ab4a725 214
edfef199
GM
215 val = Fcar (expr_and_pos);
216 RETURN_UNGCPRO (val);
217}
218
219
220/* Like read_minibuf but reading from stdin. This function is called
221 from read_minibuf to do the job if noninteractive. */
222
223static Lisp_Object
d5a3eaaf
AS
224read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
225 Lisp_Object prompt, Lisp_Object backup_n,
226 int expflag,
227 Lisp_Object histvar, Lisp_Object histpos,
228 Lisp_Object defalt,
229 int allow_props, int inherit_input_method)
edfef199
GM
230{
231 int size, len;
232 char *line, *s;
edfef199
GM
233 Lisp_Object val;
234
d5db4077 235 fprintf (stdout, "%s", SDATA (prompt));
edfef199
GM
236 fflush (stdout);
237
6bbd7a29 238 val = Qnil;
edfef199
GM
239 size = 100;
240 len = 0;
241 line = (char *) xmalloc (size * sizeof *line);
242 while ((s = fgets (line + len, size - len, stdin)) != NULL
243 && (len = strlen (line),
244 len == size - 1 && line[len - 1] != '\n'))
245 {
246 size *= 2;
247 line = (char *) xrealloc (line, size);
248 }
249
250 if (s)
251 {
252 len = strlen (line);
9ab4a725 253
edfef199
GM
254 if (len > 0 && line[len - 1] == '\n')
255 line[--len] = '\0';
9ab4a725 256
edfef199
GM
257 val = build_string (line);
258 xfree (line);
259 }
260 else
261 {
262 xfree (line);
263 error ("Error reading from stdin");
264 }
9ab4a725 265
edfef199
GM
266 /* If Lisp form desired instead of string, parse it. */
267 if (expflag)
fb30dfd2 268 val = string_to_object (val, CONSP (defalt) ? XCAR (defalt) : defalt);
9ab4a725 269
edfef199
GM
270 return val;
271}
1011edb5 272\f
873ef78e 273DEFUN ("minibufferp", Fminibufferp,
6d1b1d7b
JPW
274 Sminibufferp, 0, 1, 0,
275 doc: /* Return t if BUFFER is a minibuffer.
6b61353c
KH
276No argument or nil as argument means use current buffer as BUFFER.
277BUFFER can be a buffer or a buffer name. */)
5842a27b 278 (Lisp_Object buffer)
873ef78e
RS
279{
280 Lisp_Object tem;
281
6d1b1d7b
JPW
282 if (NILP (buffer))
283 buffer = Fcurrent_buffer ();
284 else if (STRINGP (buffer))
285 buffer = Fget_buffer (buffer);
286 else
287 CHECK_BUFFER (buffer);
288
289 tem = Fmemq (buffer, Vminibuffer_list);
873ef78e
RS
290 return ! NILP (tem) ? Qt : Qnil;
291}
292
a346ec72
MB
293DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
294 Sminibuffer_prompt_end, 0, 0, 0,
af52c32d 295 doc: /* Return the buffer position of the end of the minibuffer prompt.
eb7c9b64 296Return (point-min) if current buffer is not a minibuffer. */)
5842a27b 297 (void)
a346ec72
MB
298{
299 /* This function is written to be most efficient when there's a prompt. */
873ef78e
RS
300 Lisp_Object beg, end, tem;
301 beg = make_number (BEGV);
302
303 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
304 if (NILP (tem))
305 return beg;
306
307 end = Ffield_end (beg, Qnil, Qnil);
9ab4a725 308
a346ec72 309 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
b056f36d 310 return beg;
a346ec72
MB
311 else
312 return end;
313}
314
315DEFUN ("minibuffer-contents", Fminibuffer_contents,
316 Sminibuffer_contents, 0, 0, 0,
029a305c 317 doc: /* Return the user input in a minibuffer as a string.
6cc62df0 318If the current buffer is not a minibuffer, return its entire contents. */)
5842a27b 319 (void)
a346ec72 320{
413d18e7 321 EMACS_INT prompt_end = XINT (Fminibuffer_prompt_end ());
a346ec72
MB
322 return make_buffer_string (prompt_end, ZV, 1);
323}
324
325DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
326 Sminibuffer_contents_no_properties, 0, 0, 0,
029a305c 327 doc: /* Return the user input in a minibuffer as a string, without text-properties.
6cc62df0 328If the current buffer is not a minibuffer, return its entire contents. */)
5842a27b 329 (void)
a346ec72 330{
413d18e7 331 EMACS_INT prompt_end = XINT (Fminibuffer_prompt_end ());
a346ec72
MB
332 return make_buffer_string (prompt_end, ZV, 0);
333}
334
b5e1e449
JL
335DEFUN ("minibuffer-completion-contents", Fminibuffer_completion_contents,
336 Sminibuffer_completion_contents, 0, 0, 0,
337 doc: /* Return the user input in a minibuffer before point as a string.
338That is what completion commands operate on.
6cc62df0 339If the current buffer is not a minibuffer, return its entire contents. */)
5842a27b 340 (void)
b5e1e449 341{
413d18e7 342 EMACS_INT prompt_end = XINT (Fminibuffer_prompt_end ());
b5e1e449
JL
343 if (PT < prompt_end)
344 error ("Cannot do completion in the prompt");
345 return make_buffer_string (prompt_end, PT, 1);
346}
347
1011edb5 348\f
6b61353c
KH
349/* Read from the minibuffer using keymap MAP and initial contents INITIAL,
350 putting point minus BACKUP_N bytes from the end of INITIAL,
770970cb 351 prompting with PROMPT (a string), using history list HISTVAR
6b61353c
KH
352 with initial position HISTPOS. INITIAL should be a string or a
353 cons of a string and an integer. BACKUP_N should be <= 0, or
354 Qnil, which is equivalent to 0. If INITIAL is a cons, BACKUP_N is
355 ignored and replaced with an integer that puts point at one-indexed
356 position N in INITIAL, where N is the CDR of INITIAL, or at the
357 beginning of INITIAL if N <= 0.
770970cb
RS
358
359 Normally return the result as a string (the text that was read),
3ab14176 360 but if EXPFLAG is nonzero, read it and return the object read.
b278606c
BF
361 If HISTVAR is given, save the value read on that history only if it doesn't
362 match the front of that history list exactly. The value is pushed onto
e5d4686b 363 the list as the string that was read.
770970cb 364
4f9f637a 365 DEFALT specifies the default value for the sake of history commands.
c3421833 366
cee54539
KH
367 If ALLOW_PROPS is nonzero, we do not throw away text properties.
368
6b61353c 369 if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
cee54539 370 current input method. */
e5d4686b
RS
371
372static Lisp_Object
d5a3eaaf
AS
373read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
374 Lisp_Object backup_n, int expflag,
375 Lisp_Object histvar, Lisp_Object histpos, Lisp_Object defalt,
376 int allow_props, int inherit_input_method)
f927c5ae 377{
00a34088 378 Lisp_Object val;
aed13378 379 int count = SPECPDL_INDEX ();
cee54539
KH
380 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
381 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
c75000c7 382 Lisp_Object enable_multibyte;
6b61353c 383 int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
4f9f637a
RS
384 /* String to add to the history. */
385 Lisp_Object histstring;
386
b20de1cd
RS
387 Lisp_Object empty_minibuf;
388 Lisp_Object dummy, frame;
389
e5d4686b
RS
390 specbind (Qminibuffer_default, defalt);
391
ef72eaff
KS
392 /* If Vminibuffer_completing_file_name is `lambda' on entry, it was t
393 in previous recursive minibuffer, but was not set explicitly
394 to t for this invocation, so set it to nil in this minibuffer.
395 Save the old value now, before we change it. */
396 specbind (intern ("minibuffer-completing-file-name"), Vminibuffer_completing_file_name);
397 if (EQ (Vminibuffer_completing_file_name, Qlambda))
398 Vminibuffer_completing_file_name = Qnil;
399
d148e14d 400#ifdef HAVE_WINDOW_SYSTEM
526a058f
GM
401 if (display_hourglass_p)
402 cancel_hourglass ();
e5baa1a7 403#endif
718d3251 404
6b61353c
KH
405 if (!NILP (initial))
406 {
407 if (CONSP (initial))
408 {
409 backup_n = Fcdr (initial);
410 initial = Fcar (initial);
411 CHECK_STRING (initial);
412 if (!NILP (backup_n))
413 {
414 CHECK_NUMBER (backup_n);
415 /* Convert to distance from end of input. */
416 if (XINT (backup_n) < 1)
417 /* A number too small means the beginning of the string. */
418 pos = - SCHARS (initial);
419 else
420 pos = XINT (backup_n) - 1 - SCHARS (initial);
421 }
422 }
423 else
424 CHECK_STRING (initial);
425 }
00a34088 426 val = Qnil;
4b4deea2 427 ambient_dir = BVAR (current_buffer, directory);
cee54539 428 input_method = Qnil;
c75000c7 429 enable_multibyte = Qnil;
57ceaa8a 430
00a34088
RS
431 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
432 store them away before we can GC. Don't need to protect
433 BACKUP_N because we use the value only if it is an integer. */
cee54539 434 GCPRO5 (map, initial, val, ambient_dir, input_method);
f927c5ae 435
7510b296 436 if (!STRINGP (prompt))
a2665596 437 prompt = empty_unibyte_string;
f927c5ae 438
f927c5ae 439 if (!enable_recursive_minibuffers
be15a518
RS
440 && minibuf_level > 0)
441 {
442 if (EQ (selected_window, minibuf_window))
443 error ("Command attempted to use minibuffer while in minibuffer");
444 else
445 /* If we're in another window, cancel the minibuffer that's active. */
446 Fthrow (Qexit,
447 build_string ("Command attempted to use minibuffer while in minibuffer"));
448 }
f927c5ae 449
a9051c88
DN
450 if ((noninteractive
451 /* In case we are running as a daemon, only do this before
452 detaching from the terminal. */
453 || (IS_DAEMON && (daemon_pipe[1] >= 0)))
454 && NILP (Vexecuting_kbd_macro))
c28b847b 455 {
6b61353c
KH
456 val = read_minibuf_noninteractive (map, initial, prompt,
457 make_number (pos),
c28b847b
GM
458 expflag, histvar, histpos, defalt,
459 allow_props, inherit_input_method);
f9b9ccef 460 UNGCPRO;
c28b847b
GM
461 return unbind_to (count, val);
462 }
edfef199 463
748dc60a 464 /* Choose the minibuffer window and frame, and take action on them. */
f927c5ae 465
c5b6b680
RS
466 choose_minibuf_frame ();
467
0c94f256
RS
468 record_unwind_protect (choose_minibuf_frame_1, Qnil);
469
f927c5ae 470 record_unwind_protect (Fset_window_configuration,
b2b2c677
JB
471 Fcurrent_window_configuration (Qnil));
472
ff11dfa1
JB
473 /* If the minibuffer window is on a different frame, save that
474 frame's configuration too. */
75f00e72 475 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
a4aafc54 476 if (!EQ (mini_frame, selected_frame))
5061d9c3
RS
477 record_unwind_protect (Fset_window_configuration,
478 Fcurrent_window_configuration (mini_frame));
5563e8e8
KH
479
480 /* If the minibuffer is on an iconified or invisible frame,
481 make it visible now. */
482 Fmake_frame_visible (mini_frame);
483
6a9ee000
RS
484 if (minibuffer_auto_raise)
485 Fraise_frame (mini_frame);
f927c5ae 486
b3e6f69c 487 temporarily_switch_to_single_kboard (XFRAME (mini_frame));
256c9c3a 488
748dc60a
RS
489 /* We have to do this after saving the window configuration
490 since that is what restores the current buffer. */
491
492 /* Arrange to restore a number of minibuffer-related variables.
493 We could bind each variable separately, but that would use lots of
494 specpdl slots. */
495 minibuf_save_list
496 = Fcons (Voverriding_local_map,
3372a51b 497 Fcons (minibuf_window,
ef72eaff 498 minibuf_save_list));
748dc60a
RS
499 minibuf_save_list
500 = Fcons (minibuf_prompt,
501 Fcons (make_number (minibuf_prompt_width),
502 Fcons (Vhelp_form,
503 Fcons (Vcurrent_prefix_arg,
504 Fcons (Vminibuffer_history_position,
505 Fcons (Vminibuffer_history_variable,
506 minibuf_save_list))))));
507
508 record_unwind_protect (read_minibuf_unwind, Qnil);
509 minibuf_level++;
7292839d
SM
510 /* We are exiting the minibuffer one way or the other, so run the hook.
511 It should be run before unwinding the minibuf settings. Do it
512 separately from read_minibuf_unwind because we need to make sure that
513 read_minibuf_unwind is fully executed even if exit-minibuffer-hook
514 signals an error. --Stef */
515 record_unwind_protect (run_exit_minibuf_hook, Qnil);
748dc60a
RS
516
517 /* Now that we can restore all those variables, start changing them. */
518
1e3f16d5 519 minibuf_prompt_width = 0;
748dc60a
RS
520 minibuf_prompt = Fcopy_sequence (prompt);
521 Vminibuffer_history_position = histpos;
522 Vminibuffer_history_variable = histvar;
523 Vhelp_form = Vminibuffer_help_form;
ef72eaff
KS
524 /* If this minibuffer is reading a file name, that doesn't mean
525 recursive ones are. But we cannot set it to nil, because
526 completion code still need to know the minibuffer is completing a
527 file name. So use `lambda' as intermediate value meaning
528 "t" in this minibuffer, but "nil" in next minibuffer. */
529 if (!NILP (Vminibuffer_completing_file_name))
530 Vminibuffer_completing_file_name = Qlambda;
748dc60a 531
cee54539 532 if (inherit_input_method)
c75000c7 533 {
fd771ceb 534 /* `current-input-method' is buffer local. So, remember it in
c75000c7
RS
535 INPUT_METHOD before changing the current buffer. */
536 input_method = Fsymbol_value (Qcurrent_input_method);
4b4deea2 537 enable_multibyte = BVAR (current_buffer, enable_multibyte_characters);
c75000c7 538 }
cee54539 539
748dc60a
RS
540 /* Switch to the minibuffer. */
541
4f69d8f6
RS
542 minibuffer = get_minibuffer (minibuf_level);
543 Fset_buffer (minibuffer);
64a3a3c0 544
1d69c502
RS
545 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
546 if (inherit_input_method)
4b4deea2 547 BVAR (current_buffer, enable_multibyte_characters) = enable_multibyte;
1d69c502 548
64a3a3c0
JB
549 /* The current buffer's default directory is usually the right thing
550 for our minibuffer here. However, if you're typing a command at
551 a minibuffer-only frame when minibuf_level is zero, then buf IS
552 the current_buffer, so reset_buffer leaves buf's default
553 directory unchanged. This is a bummer when you've just started
554 up Emacs and buf's default directory is Qnil. Here's a hack; can
555 you think of something better to do? Find another buffer with a
556 better directory, and use that one instead. */
748dc60a 557 if (STRINGP (ambient_dir))
4b4deea2 558 BVAR (current_buffer, directory) = ambient_dir;
64a3a3c0
JB
559 else
560 {
561 Lisp_Object buf_list;
562
563 for (buf_list = Vbuffer_alist;
564 CONSP (buf_list);
7539e11f 565 buf_list = XCDR (buf_list))
64a3a3c0 566 {
1e62748e 567 Lisp_Object other_buf;
64a3a3c0 568
7539e11f 569 other_buf = XCDR (XCAR (buf_list));
4b4deea2 570 if (STRINGP (BVAR (XBUFFER (other_buf), directory)))
64a3a3c0 571 {
4b4deea2 572 BVAR (current_buffer, directory) = BVAR (XBUFFER (other_buf), directory);
64a3a3c0
JB
573 break;
574 }
575 }
576 }
577
a4aafc54
GM
578 if (!EQ (mini_frame, selected_frame))
579 Fredirect_frame_focus (selected_frame, mini_frame);
43bad991 580
f927c5ae 581 Vminibuf_scroll_window = selected_window;
6122844a 582 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
5705966b 583 minibuf_selected_window = selected_window;
b20de1cd
RS
584
585 /* Empty out the minibuffers of all frames other than the one
586 where we are going to display one now.
587 Set them to point to ` *Minibuf-0*', which is always empty. */
588 empty_minibuf = Fget_buffer (build_string (" *Minibuf-0*"));
589
590 FOR_EACH_FRAME (dummy, frame)
591 {
592 Lisp_Object root_window = Fframe_root_window (frame);
593 Lisp_Object mini_window = XWINDOW (root_window)->next;
594
dd95745a
RS
595 if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
596 && !NILP (Fwindow_minibuffer_p (mini_window)))
b20de1cd
RS
597 Fset_window_buffer (mini_window, empty_minibuf, Qnil);
598 }
599
600 /* Display this minibuffer in the proper window. */
cbbfe056 601 Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
f1321dc3 602 Fselect_window (minibuf_window, Qnil);
5a866662 603 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
f927c5ae 604
748dc60a
RS
605 Fmake_local_variable (Qprint_escape_newlines);
606 print_escape_newlines = 1;
607
608 /* Erase the buffer. */
59115a22 609 {
331379bf 610 int count1 = SPECPDL_INDEX ();
59115a22 611 specbind (Qinhibit_read_only, Qt);
a41edd99 612 specbind (Qinhibit_modification_hooks, Qt);
59115a22 613 Ferase_buffer ();
d9d7b298 614
4b4deea2 615 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
d9d7b298
SM
616 && ! STRING_MULTIBYTE (minibuf_prompt))
617 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
6cc62df0 618
d9d7b298
SM
619 /* Insert the prompt, record where it ends. */
620 Finsert (1, &minibuf_prompt);
621 if (PT > BEG)
622 {
623 Fput_text_property (make_number (BEG), make_number (PT),
624 Qfront_sticky, Qt, Qnil);
625 Fput_text_property (make_number (BEG), make_number (PT),
626 Qrear_nonsticky, Qt, Qnil);
627 Fput_text_property (make_number (BEG), make_number (PT),
628 Qfield, Qt, Qnil);
629 Fadd_text_properties (make_number (BEG), make_number (PT),
630 Vminibuffer_prompt_properties, Qnil);
631 }
59115a22
RS
632 unbind_to (count1, Qnil);
633 }
634
9ab4a725
TTN
635 minibuf_prompt_width = (int) current_column (); /* iftc */
636
748dc60a 637 /* Put in the initial input. */
56a98455 638 if (!NILP (initial))
f927c5ae
JB
639 {
640 Finsert (1, &initial);
6b61353c 641 Fforward_char (make_number (pos));
f927c5ae
JB
642 }
643
39e98b38 644 clear_message (1, 1);
4b4deea2 645 BVAR (current_buffer, keymap) = map;
f927c5ae 646
cee54539 647 /* Turn on an input method stored in INPUT_METHOD if any. */
6801b300 648 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
cee54539
KH
649 call1 (Qactivate_input_method, input_method);
650
5c781212
RS
651 /* Run our hook, but not if it is empty.
652 (run-hooks would do nothing if it is empty,
fd771ceb 653 but it's important to save time here in the usual case.) */
92d3b06e
RS
654 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
655 && !NILP (Vrun_hooks))
5c781212
RS
656 call1 (Vrun_hooks, Qminibuffer_setup_hook);
657
4e6b7204 658 /* Don't allow the user to undo past this point. */
4b4deea2 659 BVAR (current_buffer, undo_list) = Qnil;
4e6b7204 660
f927c5ae
JB
661 recursive_edit_1 ();
662
663 /* If cursor is on the minibuffer line,
664 show the user we have exited by putting it in column 0. */
279cf52c 665 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
f927c5ae
JB
666 && !noninteractive)
667 {
279cf52c
GM
668 XWINDOW (minibuf_window)->cursor.hpos = 0;
669 XWINDOW (minibuf_window)->cursor.x = 0;
670 XWINDOW (minibuf_window)->must_be_updated_p = 1;
a4aafc54 671 update_frame (XFRAME (selected_frame), 1, 1);
fa971ac3
KL
672 {
673 struct frame *f = XFRAME (XWINDOW (minibuf_window)->frame);
674 struct redisplay_interface *rif = FRAME_RIF (f);
675 if (rif && rif->flush_display)
676 rif->flush_display (f);
677 }
f927c5ae
JB
678 }
679
c3421833 680 /* Make minibuffer contents into a string. */
4f69d8f6 681 Fset_buffer (minibuffer);
be95a9b6 682 if (allow_props)
a346ec72 683 val = Fminibuffer_contents ();
be95a9b6 684 else
a346ec72 685 val = Fminibuffer_contents_no_properties ();
770970cb 686
b278606c 687 /* VAL is the string of minibuffer text. */
7019cca7 688
b278606c
BF
689 last_minibuf_string = val;
690
4f9f637a 691 /* Choose the string to add to the history. */
b35cd215 692 if (SCHARS (val) != 0)
4f9f637a
RS
693 histstring = val;
694 else if (STRINGP (defalt))
695 histstring = defalt;
fb30dfd2
JL
696 else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
697 histstring = XCAR (defalt);
4f9f637a
RS
698 else
699 histstring = Qnil;
700
701 /* Add the value to the appropriate history list, if any. */
179a4ca7
JL
702 if (!NILP (Vhistory_add_new_input)
703 && SYMBOLP (Vminibuffer_history_variable)
4f9f637a 704 && !NILP (histstring))
3ab14176
KH
705 {
706 /* If the caller wanted to save the value read on a history list,
707 then do so if the value is not already the front of the list. */
708 Lisp_Object histval;
c6d65724
RS
709
710 /* If variable is unbound, make it nil. */
c6d65724 711
8c2dccbf
SM
712 histval = find_symbol_value (Vminibuffer_history_variable);
713 if (EQ (histval, Qunbound))
714 Fset (Vminibuffer_history_variable, Qnil);
3ab14176
KH
715
716 /* The value of the history variable must be a cons or nil. Other
717 values are unacceptable. We silently ignore these values. */
4f9f637a 718
3ab14176 719 if (NILP (histval)
9f6131cf 720 || (CONSP (histval)
4f9f637a 721 /* Don't duplicate the most recent entry in the history. */
b35cd215 722 && (NILP (Fequal (histstring, Fcar (histval))))))
77aa8edf
RS
723 {
724 Lisp_Object length;
725
0da4d471 726 if (history_delete_duplicates) Fdelete (histstring, histval);
4f9f637a 727 histval = Fcons (histstring, histval);
77aa8edf
RS
728 Fset (Vminibuffer_history_variable, histval);
729
730 /* Truncate if requested. */
731 length = Fget (Vminibuffer_history_variable, Qhistory_length);
732 if (NILP (length)) length = Vhistory_length;
e5d4686b
RS
733 if (INTEGERP (length))
734 {
735 if (XINT (length) <= 0)
736 Fset (Vminibuffer_history_variable, Qnil);
737 else
738 {
739 Lisp_Object temp;
740
741 temp = Fnthcdr (Fsub1 (length), histval);
742 if (CONSP (temp)) Fsetcdr (temp, Qnil);
743 }
744 }
77aa8edf 745 }
9f6131cf
RS
746 }
747
748 /* If Lisp form desired instead of string, parse it. */
749 if (expflag)
edfef199 750 val = string_to_object (val, defalt);
3ab14176 751
00a34088
RS
752 /* The appropriate frame will get selected
753 in set-window-configuration. */
f9b9ccef
SM
754 UNGCPRO;
755 return unbind_to (count, val);
f927c5ae
JB
756}
757
758/* Return a buffer to be used as the minibuffer at depth `depth'.
759 depth = 0 is the lowest allowed argument, and that is the value
760 used for nonrecursive minibuffer invocations */
761
762Lisp_Object
971de7fb 763get_minibuffer (int depth)
f927c5ae
JB
764{
765 Lisp_Object tail, num, buf;
9f6c23bc 766 char name[24];
f927c5ae 767
5a866662 768 XSETFASTINT (num, depth);
f927c5ae 769 tail = Fnthcdr (num, Vminibuffer_list);
56a98455 770 if (NILP (tail))
f927c5ae
JB
771 {
772 tail = Fcons (Qnil, Qnil);
773 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
774 }
775 buf = Fcar (tail);
4b4deea2 776 if (NILP (buf) || NILP (BVAR (XBUFFER (buf), name)))
f927c5ae
JB
777 {
778 sprintf (name, " *Minibuf-%d*", depth);
779 buf = Fget_buffer_create (build_string (name));
5d6533f1
JB
780
781 /* Although the buffer's name starts with a space, undo should be
782 enabled in it. */
783 Fbuffer_enable_undo (buf);
784
f3fbd155 785 XSETCAR (tail, buf);
f927c5ae
JB
786 }
787 else
5956f71d 788 {
aed13378 789 int count = SPECPDL_INDEX ();
7f856567
SM
790 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
791 have to empty the list, otherwise we end up with overlays that
792 think they belong to this buffer while the buffer doesn't know about
793 them any more. */
599ca9c2
SM
794 delete_all_overlays (XBUFFER (buf));
795 reset_buffer (XBUFFER (buf));
6b3faad8
RS
796 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
797 Fset_buffer (buf);
798 Fkill_all_local_variables ();
799 unbind_to (count, Qnil);
5956f71d 800 }
64a3a3c0 801
f927c5ae
JB
802 return buf;
803}
804
7292839d 805static Lisp_Object
971de7fb 806run_exit_minibuf_hook (Lisp_Object data)
7292839d
SM
807{
808 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
809 && !NILP (Vrun_hooks))
810 safe_run_hooks (Qminibuffer_exit_hook);
811
812 return Qnil;
813}
814
279cf52c
GM
815/* This function is called on exiting minibuffer, whether normally or
816 not, and it restores the current window, buffer, etc. */
f927c5ae 817
9d815fd9 818static Lisp_Object
971de7fb 819read_minibuf_unwind (Lisp_Object data)
f927c5ae 820{
c24e1160 821 Lisp_Object old_deactivate_mark;
59115a22 822 Lisp_Object window;
c24e1160 823
f927c5ae 824 /* If this was a recursive minibuffer,
59115a22 825 tie the minibuffer window back to the outer level minibuffer buffer. */
f927c5ae 826 minibuf_level--;
f927c5ae 827
59115a22 828 window = minibuf_window;
279cf52c
GM
829 /* To keep things predictable, in case it matters, let's be in the
830 minibuffer when we reset the relevant variables. */
59115a22
RS
831 Fset_buffer (XWINDOW (window)->buffer);
832
833 /* Restore prompt, etc, from outer minibuffer level. */
4d04c1f1
KH
834 minibuf_prompt = Fcar (minibuf_save_list);
835 minibuf_save_list = Fcdr (minibuf_save_list);
836 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
837 minibuf_save_list = Fcdr (minibuf_save_list);
838 Vhelp_form = Fcar (minibuf_save_list);
839 minibuf_save_list = Fcdr (minibuf_save_list);
ee9e37ab 840 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
4d04c1f1
KH
841 minibuf_save_list = Fcdr (minibuf_save_list);
842 Vminibuffer_history_position = Fcar (minibuf_save_list);
843 minibuf_save_list = Fcdr (minibuf_save_list);
844 Vminibuffer_history_variable = Fcar (minibuf_save_list);
845 minibuf_save_list = Fcdr (minibuf_save_list);
30e13e56 846 Voverriding_local_map = Fcar (minibuf_save_list);
c5b6b680 847 minibuf_save_list = Fcdr (minibuf_save_list);
914860c2
RS
848#if 0
849 temp = Fcar (minibuf_save_list);
850 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
851 minibuf_window = temp;
852#endif
30e13e56 853 minibuf_save_list = Fcdr (minibuf_save_list);
59115a22
RS
854
855 /* Erase the minibuffer we were using at this level. */
856 {
aed13378 857 int count = SPECPDL_INDEX ();
59115a22
RS
858 /* Prevent error in erase-buffer. */
859 specbind (Qinhibit_read_only, Qt);
bd819d14 860 specbind (Qinhibit_modification_hooks, Qt);
59115a22
RS
861 old_deactivate_mark = Vdeactivate_mark;
862 Ferase_buffer ();
863 Vdeactivate_mark = old_deactivate_mark;
864 unbind_to (count, Qnil);
865 }
866
544e358d
GM
867 /* When we get to the outmost level, make sure we resize the
868 mini-window back to its normal size. */
869 if (minibuf_level == 0)
1e3f16d5 870 resize_mini_window (XWINDOW (window), 0);
544e358d 871
59115a22
RS
872 /* Make sure minibuffer window is erased, not ignored. */
873 windows_or_buffers_changed++;
874 XSETFASTINT (XWINDOW (window)->last_modified, 0);
193e4518 875 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
9d815fd9 876 return Qnil;
f927c5ae
JB
877}
878\f
b9d721de 879
b35cd215 880DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
af52c32d 881 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
6b61353c
KH
882The optional second arg INITIAL-CONTENTS is an obsolete alternative to
883 DEFAULT-VALUE. It normally should be nil in new code, except when
884 HIST is a cons. It is discussed in more detail below.
a1f17501
PJ
885Third arg KEYMAP is a keymap to use whilst reading;
886 if omitted or nil, the default is `minibuffer-local-map'.
fd771ceb 887If fourth arg READ is non-nil, then interpret the result as a Lisp object
a1f17501
PJ
888 and return that object:
889 in other words, do `(car (read-from-string INPUT-STRING))'
6b61353c
KH
890Fifth arg HIST, if non-nil, specifies a history list and optionally
891 the initial position in the list. It can be a symbol, which is the
892 history list variable to use, or it can be a cons cell
893 (HISTVAR . HISTPOS). In that case, HISTVAR is the history list variable
894 to use, and HISTPOS is the initial position for use by the minibuffer
895 history commands. For consistency, you should also specify that
896 element of the history as the value of INITIAL-CONTENTS. Positions
897 are counted starting from 1 at the beginning of the list.
2e3148e2
JL
898Sixth arg DEFAULT-VALUE is the default value or the list of default values.
899 If non-nil, it is available for history commands, and as the value
900 (or the first element of the list of default values) to return
901 if the user enters the empty string. But, unless READ is non-nil,
902 `read-from-minibuffer' does NOT return DEFAULT-VALUE if the user enters
903 empty input! It returns the empty string.
a1f17501 904Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
029a305c 905 the current input method and the setting of `enable-multibyte-characters'.
a1f17501
PJ
906If the variable `minibuffer-allow-text-properties' is non-nil,
907 then the string which is returned includes whatever text properties
6b61353c
KH
908 were present in the minibuffer. Otherwise the value has no text properties.
909
910The remainder of this documentation string describes the
911INITIAL-CONTENTS argument in more detail. It is only relevant when
912studying existing code, or when HIST is a cons. If non-nil,
913INITIAL-CONTENTS is a string to be inserted into the minibuffer before
914reading input. Normally, point is put at the end of that string.
915However, if INITIAL-CONTENTS is \(STRING . POSITION), the initial
916input is STRING, but point is placed at _one-indexed_ position
917POSITION in the minibuffer. Any integer value less than or equal to
918one puts point at the beginning of the string. *Note* that this
919behavior differs from the way such arguments are used in `completing-read'
920and some related functions, which use zero-indexing for POSITION. */)
5842a27b 921 (Lisp_Object prompt, Lisp_Object initial_contents, Lisp_Object keymap, Lisp_Object read, Lisp_Object hist, Lisp_Object default_value, Lisp_Object inherit_input_method)
f927c5ae 922{
6b61353c 923 Lisp_Object histvar, histpos, val;
1d8d92f4
RS
924 struct gcpro gcpro1;
925
b7826503 926 CHECK_STRING (prompt);
56a98455 927 if (NILP (keymap))
f927c5ae
JB
928 keymap = Vminibuffer_local_map;
929 else
02067692 930 keymap = get_keymap (keymap, 1, 0);
770970cb 931
7510b296 932 if (SYMBOLP (hist))
770970cb
RS
933 {
934 histvar = hist;
935 histpos = Qnil;
936 }
937 else
938 {
939 histvar = Fcar_safe (hist);
940 histpos = Fcdr_safe (hist);
941 }
942 if (NILP (histvar))
943 histvar = Qminibuffer_history;
944 if (NILP (histpos))
5a866662 945 XSETFASTINT (histpos, 0);
770970cb 946
1d8d92f4 947 GCPRO1 (default_value);
e5d4686b 948 val = read_minibuf (keymap, initial_contents, prompt,
6b61353c 949 Qnil, !NILP (read),
c3421833 950 histvar, histpos, default_value,
cee54539 951 minibuffer_allow_text_properties,
b35cd215 952 !NILP (inherit_input_method));
1d8d92f4 953 UNGCPRO;
e5d4686b 954 return val;
f927c5ae
JB
955}
956
957DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
6b61353c 958 doc: /* Return a Lisp object read using the minibuffer, unevaluated.
a1f17501 959Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
6b61353c 960is a string to insert in the minibuffer before reading.
e298936f
JB
961\(INITIAL-CONTENTS can also be a cons of a string and an integer.
962Such arguments are used as in `read-from-minibuffer'.) */)
5842a27b 963 (Lisp_Object prompt, Lisp_Object initial_contents)
f927c5ae 964{
b7826503 965 CHECK_STRING (prompt);
770970cb 966 return read_minibuf (Vminibuffer_local_map, initial_contents,
e5d4686b 967 prompt, Qnil, 1, Qminibuffer_history,
b35cd215 968 make_number (0), Qnil, 0, 0);
f927c5ae
JB
969}
970
971DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
af52c32d 972 doc: /* Return value of Lisp expression read using the minibuffer.
a1f17501 973Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
6b61353c 974is a string to insert in the minibuffer before reading.
e298936f
JB
975\(INITIAL-CONTENTS can also be a cons of a string and an integer.
976Such arguments are used as in `read-from-minibuffer'.) */)
5842a27b 977 (Lisp_Object prompt, Lisp_Object initial_contents)
f927c5ae 978{
627fb581
RS
979 return Feval (read_minibuf (Vread_expression_map, initial_contents,
980 prompt, Qnil, 1, Qread_expression_history,
981 make_number (0), Qnil, 0, 0));
f927c5ae
JB
982}
983
984/* Functions that use the minibuffer to read various things. */
985
cee54539 986DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
af52c32d 987 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
a1f17501 988If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
6b61353c
KH
989 This argument has been superseded by DEFAULT-VALUE and should normally
990 be nil in new code. It behaves as in `read-from-minibuffer'. See the
991 documentation string of that function for details.
a1f17501
PJ
992The third arg HISTORY, if non-nil, specifies a history list
993 and optionally the initial position in the list.
994See `read-from-minibuffer' for details of HISTORY argument.
2e3148e2
JL
995Fourth arg DEFAULT-VALUE is the default value or the list of default values.
996 If non-nil, it is used for history commands, and as the value (or the first
997 element of the list of default values) to return if the user enters the
998 empty string.
a1f17501 999Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
029a305c 1000 the current input method and the setting of `enable-multibyte-characters'. */)
5842a27b 1001 (Lisp_Object prompt, Lisp_Object initial_input, Lisp_Object history, Lisp_Object default_value, Lisp_Object inherit_input_method)
f927c5ae 1002{
b9a86585
RS
1003 Lisp_Object val;
1004 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
1005 Qnil, history, default_value,
b35cd215 1006 inherit_input_method);
d5db4077 1007 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
fb30dfd2 1008 val = CONSP (default_value) ? XCAR (default_value) : default_value;
b9a86585 1009 return val;
f927c5ae
JB
1010}
1011
cee54539 1012DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
af52c32d 1013 doc: /* Read a string from the terminal, not allowing blanks.
6b61353c
KH
1014Prompt with PROMPT. Whitespace terminates the input. If INITIAL is
1015non-nil, it should be a string, which is used as initial input, with
1016point positioned at the end, so that SPACE will accept the input.
1017\(Actually, INITIAL can also be a cons of a string and an integer.
1018Such values are treated as in `read-from-minibuffer', but are normally
1019not useful in this function.)
a1f17501 1020Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
6b61353c 1021the current input method and the setting of`enable-multibyte-characters'. */)
5842a27b 1022 (Lisp_Object prompt, Lisp_Object initial, Lisp_Object inherit_input_method)
f927c5ae 1023{
b7826503 1024 CHECK_STRING (prompt);
c923ccc9 1025 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
cee54539 1026 0, Qminibuffer_history, make_number (0), Qnil, 0,
b35cd215 1027 !NILP (inherit_input_method));
f927c5ae
JB
1028}
1029
e5d4686b 1030DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
af52c32d 1031 doc: /* Read the name of a command and return as a symbol.
2e3148e2
JL
1032Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1033if it is a list. */)
5842a27b 1034 (Lisp_Object prompt, Lisp_Object default_value)
f927c5ae 1035{
7c8d9931
RS
1036 Lisp_Object name, default_string;
1037
1038 if (NILP (default_value))
1039 default_string = Qnil;
1040 else if (SYMBOLP (default_value))
caa4733e 1041 default_string = SYMBOL_NAME (default_value);
7c8d9931
RS
1042 else
1043 default_string = default_value;
9ab4a725 1044
7c8d9931
RS
1045 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1046 Qnil, Qnil, default_string, Qnil);
1047 if (NILP (name))
1048 return name;
1049 return Fintern (name, Qnil);
f927c5ae
JB
1050}
1051
1052#ifdef NOTDEF
1053DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
af52c32d 1054 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
fd771ceb 1055Prompt with PROMPT. */)
5842a27b 1056 (Lisp_Object prompt)
f927c5ae 1057{
cee54539 1058 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
f927c5ae
JB
1059 Qnil);
1060}
1061#endif /* NOTDEF */
1062
e5d4686b 1063DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
af52c32d 1064 doc: /* Read the name of a user variable and return it as a symbol.
2e3148e2
JL
1065Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1066if it is a list.
316f9bfc 1067A user variable is one for which `user-variable-p' returns non-nil. */)
5842a27b 1068 (Lisp_Object prompt, Lisp_Object default_value)
f927c5ae 1069{
7c8d9931
RS
1070 Lisp_Object name, default_string;
1071
1072 if (NILP (default_value))
1073 default_string = Qnil;
1074 else if (SYMBOLP (default_value))
caa4733e 1075 default_string = SYMBOL_NAME (default_value);
7c8d9931
RS
1076 else
1077 default_string = default_value;
9ab4a725 1078
7c8d9931
RS
1079 name = Fcompleting_read (prompt, Vobarray,
1080 Quser_variable_p, Qt,
1081 Qnil, Qnil, default_string, Qnil);
1082 if (NILP (name))
1083 return name;
1084 return Fintern (name, Qnil);
f927c5ae
JB
1085}
1086
1087DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
fd771ceb
PJ
1088 doc: /* Read the name of a buffer and return as a string.
1089Prompt with PROMPT.
a1f17501 1090Optional second arg DEF is value to return if user enters an empty line.
2e3148e2 1091 If DEF is a list of default values, return its first element.
5fedada7
CY
1092Optional third arg REQUIRE-MATCH determines whether non-existing
1093 buffer names are allowed. It has the same meaning as the
a2a0d36b 1094 REQUIRE-MATCH argument of `completing-read'.
33df281c
CY
1095The argument PROMPT should be a string ending with a colon and a space.
1096If `read-buffer-completion-ignore-case' is non-nil, completion ignores
1097case while reading the buffer name.
1098If `read-buffer-function' is non-nil, this works by calling it as a
1099function, instead of the usual behavior. */)
5842a27b 1100 (Lisp_Object prompt, Lisp_Object def, Lisp_Object require_match)
f927c5ae 1101{
33df281c 1102 Lisp_Object args[4], result;
637de37e 1103 char *s;
97c3e4cc 1104 int len;
33df281c 1105 int count = SPECPDL_INDEX ();
9ab4a725 1106
7510b296 1107 if (BUFFERP (def))
4b4deea2 1108 def = BVAR (XBUFFER (def), name);
eebbfb01 1109
33df281c
CY
1110 specbind (Qcompletion_ignore_case,
1111 read_buffer_completion_ignore_case ? Qt : Qnil);
1112
eebbfb01
KH
1113 if (NILP (Vread_buffer_function))
1114 {
1115 if (!NILP (def))
1116 {
97c3e4cc
RF
1117 /* A default value was provided: we must change PROMPT,
1118 editing the default value in before the colon. To achieve
1119 this, we replace PROMPT with a substring that doesn't
1120 contain the terminal space and colon (if present). They
1121 are then added back using Fformat. */
1122
1123 if (STRINGP (prompt))
1124 {
637de37e 1125 s = SSDATA (prompt);
97c3e4cc
RF
1126 len = strlen (s);
1127 if (len >= 2 && s[len - 2] == ':' && s[len - 1] == ' ')
1128 len = len - 2;
1129 else if (len >= 1 && (s[len - 1] == ':' || s[len - 1] == ' '))
1130 len--;
1131
1132 prompt = make_specified_string (s, -1, len,
1133 STRING_MULTIBYTE (prompt));
1134 }
1135
1136 args[0] = build_string ("%s (default %s): ");
eebbfb01 1137 args[1] = prompt;
fb30dfd2 1138 args[2] = CONSP (def) ? XCAR (def) : def;
eebbfb01
KH
1139 prompt = Fformat (3, args);
1140 }
1141
33df281c
CY
1142 result = Fcompleting_read (prompt, intern ("internal-complete-buffer"),
1143 Qnil, require_match, Qnil, Qbuffer_name_history,
1144 def, Qnil);
eebbfb01
KH
1145 }
1146 else
f927c5ae 1147 {
eebbfb01 1148 args[0] = Vread_buffer_function;
f927c5ae
JB
1149 args[1] = prompt;
1150 args[2] = def;
eebbfb01 1151 args[3] = require_match;
33df281c 1152 result = Ffuncall(4, args);
f927c5ae 1153 }
33df281c 1154 return unbind_to (count, result);
f927c5ae
JB
1155}
1156\f
ec067ec7 1157static Lisp_Object
971de7fb 1158minibuf_conform_representation (Lisp_Object string, Lisp_Object basis)
ec067ec7
RS
1159{
1160 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1161 return string;
1162
1163 if (STRING_MULTIBYTE (string))
1164 return Fstring_make_unibyte (string);
1165 else
1166 return Fstring_make_multibyte (string);
1167}
1168
f927c5ae 1169DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
a284cdbb
RS
1170 doc: /* Return common substring of all completions of STRING in COLLECTION.
1171Test each possible completion specified by COLLECTION
1172to see if it begins with STRING. The possible completions may be
3809648a
KS
1173strings or symbols. Symbols are converted to strings before testing,
1174see `symbol-name'.
a284cdbb
RS
1175All that match STRING are compared together; the longest initial sequence
1176common to all these matches is the return value.
1177If there is no match at all, the return value is nil.
1178For a unique match which is exact, the return value is t.
1179
1180If COLLECTION is an alist, the keys (cars of elements) are the
1181possible completions. If an element is not a cons cell, then the
1182element itself is the possible completion.
1183If COLLECTION is a hash-table, all the keys that are strings or symbols
1184are the possible completions.
1185If COLLECTION is an obarray, the names of all symbols in the obarray
1186are the possible completions.
1187
1188COLLECTION can also be a function to do the completion itself.
a1f17501
PJ
1189It receives three arguments: the values STRING, PREDICATE and nil.
1190Whatever it returns becomes the value of `try-completion'.
1191
1192If optional third argument PREDICATE is non-nil,
1193it is used to test each possible match.
1194The match is a candidate only if PREDICATE returns non-nil.
1195The argument given to PREDICATE is the alist element
a284cdbb 1196or the symbol from the obarray. If COLLECTION is a hash-table,
85cd4372 1197predicate is called with two arguments: the key and the value.
a1f17501 1198Additionally to this predicate, `completion-regexp-list'
af52c32d 1199is used to further constrain the set of candidates. */)
5842a27b 1200 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
f927c5ae
JB
1201{
1202 Lisp_Object bestmatch, tail, elt, eltstring;
af2b7cd5 1203 /* Size in bytes of BESTMATCH. */
6bbd7a29 1204 int bestmatchsize = 0;
af2b7cd5 1205 /* These are in bytes, too. */
f927c5ae 1206 int compare, matchsize;
f6d4c7a4
RS
1207 enum { function_table, list_table, obarray_table, hash_table}
1208 type = (HASH_TABLE_P (collection) ? hash_table
1209 : VECTORP (collection) ? obarray_table
1210 : ((NILP (collection)
1211 || (CONSP (collection)
1212 && (!SYMBOLP (XCAR (collection))
1213 || NILP (XCAR (collection)))))
1214 ? list_table : function_table));
6bbd7a29 1215 int index = 0, obsize = 0;
f927c5ae 1216 int matchcount = 0;
acd81db9 1217 int bindcount = -1;
f927c5ae
JB
1218 Lisp_Object bucket, zero, end, tem;
1219 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1220
b7826503 1221 CHECK_STRING (string);
f6d4c7a4 1222 if (type == function_table)
a284cdbb 1223 return call3 (collection, string, predicate, Qnil);
f927c5ae 1224
6bbd7a29 1225 bestmatch = bucket = Qnil;
c8ae863b 1226 zero = make_number (0);
f927c5ae 1227
a284cdbb
RS
1228 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1229 tail = collection;
f6d4c7a4 1230 if (type == obarray_table)
f927c5ae 1231 {
a284cdbb
RS
1232 collection = check_obarray (collection);
1233 obsize = XVECTOR (collection)->size;
1234 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1235 }
1236
1237 while (1)
1238 {
85cd4372 1239 /* Get the next element of the alist, obarray, or hash-table. */
f927c5ae
JB
1240 /* Exit the loop if the elements are all used up. */
1241 /* elt gets the alist element or symbol.
1242 eltstring gets the name to check as a completion. */
1243
f6d4c7a4 1244 if (type == list_table)
f927c5ae 1245 {
695deb18 1246 if (!CONSP (tail))
f927c5ae 1247 break;
695deb18
SM
1248 elt = XCAR (tail);
1249 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1250 tail = XCDR (tail);
f927c5ae 1251 }
f6d4c7a4 1252 else if (type == obarray_table)
f927c5ae 1253 {
c8ae863b 1254 if (!EQ (bucket, zero))
f927c5ae 1255 {
9b6b374a
JB
1256 if (!SYMBOLP (bucket))
1257 error ("Bad data in guts of obarray");
f927c5ae 1258 elt = bucket;
3809648a 1259 eltstring = elt;
f927c5ae
JB
1260 if (XSYMBOL (bucket)->next)
1261 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1262 else
5a866662 1263 XSETFASTINT (bucket, 0);
f927c5ae
JB
1264 }
1265 else if (++index >= obsize)
1266 break;
1267 else
1268 {
a284cdbb 1269 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1270 continue;
1271 }
1272 }
f6d4c7a4 1273 else /* if (type == hash_table) */
85cd4372 1274 {
a284cdbb
RS
1275 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1276 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
85cd4372 1277 index++;
a284cdbb 1278 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
85cd4372
SM
1279 break;
1280 else
a284cdbb 1281 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
85cd4372 1282 }
f927c5ae
JB
1283
1284 /* Is this element a possible completion? */
1285
3809648a
KS
1286 if (SYMBOLP (eltstring))
1287 eltstring = Fsymbol_name (eltstring);
1288
7510b296 1289 if (STRINGP (eltstring)
d5db4077 1290 && SCHARS (string) <= SCHARS (eltstring)
c8ae863b 1291 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1292 make_number (SCHARS (string)),
c8ae863b 1293 string, zero, Qnil,
6b61353c 1294 completion_ignore_case ? Qt : Qnil),
69f4ef20 1295 EQ (Qt, tem)))
f927c5ae
JB
1296 {
1297 /* Yes. */
42006772 1298 Lisp_Object regexps;
42006772
RS
1299
1300 /* Ignore this element if it fails to match all the regexps. */
6b61353c 1301 {
6b61353c
KH
1302 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1303 regexps = XCDR (regexps))
1304 {
acd81db9
DK
1305 if (bindcount < 0) {
1306 bindcount = SPECPDL_INDEX ();
1307 specbind (Qcase_fold_search,
1308 completion_ignore_case ? Qt : Qnil);
77443789 1309 }
6b61353c
KH
1310 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1311 if (NILP (tem))
1312 break;
1313 }
6b61353c
KH
1314 if (CONSP (regexps))
1315 continue;
1316 }
42006772 1317
f927c5ae
JB
1318 /* Ignore this element if there is a predicate
1319 and the predicate doesn't like it. */
1320
7efd36fc 1321 if (!NILP (predicate))
f927c5ae 1322 {
7efd36fc 1323 if (EQ (predicate, Qcommandp))
c837f4f9 1324 tem = Fcommandp (elt, Qnil);
f927c5ae
JB
1325 else
1326 {
f6d4c7a4
RS
1327 if (bindcount >= 0)
1328 {
1329 unbind_to (bindcount, Qnil);
1330 bindcount = -1;
1331 }
f927c5ae 1332 GCPRO4 (tail, string, eltstring, bestmatch);
f6d4c7a4
RS
1333 tem = (type == hash_table
1334 ? call2 (predicate, elt,
1335 HASH_VALUE (XHASH_TABLE (collection),
1336 index - 1))
1337 : call1 (predicate, elt));
f927c5ae
JB
1338 UNGCPRO;
1339 }
56a98455 1340 if (NILP (tem)) continue;
f927c5ae
JB
1341 }
1342
1343 /* Update computation of how much all possible completions match */
1344
56a98455 1345 if (NILP (bestmatch))
af2b7cd5 1346 {
85cd4372 1347 matchcount = 1;
af2b7cd5 1348 bestmatch = eltstring;
d5db4077 1349 bestmatchsize = SCHARS (eltstring);
af2b7cd5 1350 }
f927c5ae
JB
1351 else
1352 {
d5db4077 1353 compare = min (bestmatchsize, SCHARS (eltstring));
c8ae863b 1354 tem = Fcompare_strings (bestmatch, zero,
69f4ef20 1355 make_number (compare),
c8ae863b 1356 eltstring, zero,
69f4ef20
RS
1357 make_number (compare),
1358 completion_ignore_case ? Qt : Qnil);
1359 if (EQ (tem, Qt))
1360 matchsize = compare;
1361 else if (XINT (tem) < 0)
1362 matchsize = - XINT (tem) - 1;
1363 else
1364 matchsize = XINT (tem) - 1;
1365
52b14ac0
JB
1366 if (completion_ignore_case)
1367 {
1368 /* If this is an exact match except for case,
1369 use it as the best match rather than one that is not an
1370 exact match. This way, we get the case pattern
1371 of the actual match. */
d5db4077
KR
1372 if ((matchsize == SCHARS (eltstring)
1373 && matchsize < SCHARS (bestmatch))
52b14ac0
JB
1374 ||
1375 /* If there is more than one exact match ignoring case,
1376 and one of them is exact including case,
1377 prefer that one. */
1378 /* If there is no exact match ignoring case,
1379 prefer a match that does not change the case
1380 of the input. */
d5db4077 1381 ((matchsize == SCHARS (eltstring))
52b14ac0 1382 ==
d5db4077 1383 (matchsize == SCHARS (bestmatch))
c8ae863b 1384 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1385 make_number (SCHARS (string)),
c8ae863b 1386 string, zero,
69f4ef20
RS
1387 Qnil,
1388 Qnil),
1389 EQ (Qt, tem))
c8ae863b 1390 && (tem = Fcompare_strings (bestmatch, zero,
d5db4077 1391 make_number (SCHARS (string)),
c8ae863b 1392 string, zero,
69f4ef20
RS
1393 Qnil,
1394 Qnil),
1395 ! EQ (Qt, tem))))
52b14ac0
JB
1396 bestmatch = eltstring;
1397 }
d5db4077 1398 if (bestmatchsize != SCHARS (eltstring)
85cd4372
SM
1399 || bestmatchsize != matchsize)
1400 /* Don't count the same string multiple times. */
1401 matchcount++;
52b14ac0 1402 bestmatchsize = matchsize;
d5db4077 1403 if (matchsize <= SCHARS (string)
78893e05
CY
1404 /* If completion-ignore-case is non-nil, don't
1405 short-circuit because we want to find the best
1406 possible match *including* case differences. */
1407 && !completion_ignore_case
695deb18
SM
1408 && matchcount > 1)
1409 /* No need to look any further. */
1410 break;
f927c5ae
JB
1411 }
1412 }
1413 }
1414
acd81db9
DK
1415 if (bindcount >= 0) {
1416 unbind_to (bindcount, Qnil);
1417 bindcount = -1;
1418 }
1419
56a98455 1420 if (NILP (bestmatch))
f927c5ae 1421 return Qnil; /* No completions found */
52b14ac0
JB
1422 /* If we are ignoring case, and there is no exact match,
1423 and no additional text was supplied,
1424 don't change the case of what the user typed. */
d5db4077
KR
1425 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1426 && SCHARS (bestmatch) > bestmatchsize)
ec067ec7 1427 return minibuf_conform_representation (string, bestmatch);
52b14ac0
JB
1428
1429 /* Return t if the supplied string is an exact match (counting case);
1430 it does not require any change to be made. */
928b5acc 1431 if (matchcount == 1 && !NILP (Fequal (bestmatch, string)))
f927c5ae
JB
1432 return Qt;
1433
5a866662
KH
1434 XSETFASTINT (zero, 0); /* Else extract the part in which */
1435 XSETFASTINT (end, bestmatchsize); /* all completions agree */
f927c5ae
JB
1436 return Fsubstring (bestmatch, zero, end);
1437}
f927c5ae 1438\f
89a255dc 1439DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
a284cdbb
RS
1440 doc: /* Search for partial matches to STRING in COLLECTION.
1441Test each of the possible completions specified by COLLECTION
1442to see if it begins with STRING. The possible completions may be
3809648a
KS
1443strings or symbols. Symbols are converted to strings before testing,
1444see `symbol-name'.
a284cdbb 1445The value is a list of all the possible completions that match STRING.
a1f17501 1446
a284cdbb
RS
1447If COLLECTION is an alist, the keys (cars of elements) are the
1448possible completions. If an element is not a cons cell, then the
1449element itself is the possible completion.
1450If COLLECTION is a hash-table, all the keys that are strings or symbols
1451are the possible completions.
1452If COLLECTION is an obarray, the names of all symbols in the obarray
1453are the possible completions.
a1f17501 1454
a284cdbb 1455COLLECTION can also be a function to do the completion itself.
a1f17501
PJ
1456It receives three arguments: the values STRING, PREDICATE and t.
1457Whatever it returns becomes the value of `all-completions'.
1458
1459If optional third argument PREDICATE is non-nil,
1460it is used to test each possible match.
1461The match is a candidate only if PREDICATE returns non-nil.
1462The argument given to PREDICATE is the alist element
a284cdbb 1463or the symbol from the obarray. If COLLECTION is a hash-table,
85cd4372 1464predicate is called with two arguments: the key and the value.
a1f17501
PJ
1465Additionally to this predicate, `completion-regexp-list'
1466is used to further constrain the set of candidates.
1467
782a943e 1468An obsolete optional fourth argument HIDE-SPACES is still accepted for
057bce6f 1469backward compatibility. If non-nil, strings in COLLECTION that start
4fcc3d32 1470with a space are ignored unless STRING itself starts with a space. */)
5842a27b 1471 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate, Lisp_Object hide_spaces)
f927c5ae
JB
1472{
1473 Lisp_Object tail, elt, eltstring;
1474 Lisp_Object allmatches;
a284cdbb
RS
1475 int type = HASH_TABLE_P (collection) ? 3
1476 : VECTORP (collection) ? 2
1477 : NILP (collection) || (CONSP (collection)
1478 && (!SYMBOLP (XCAR (collection))
1479 || NILP (XCAR (collection))));
6bbd7a29 1480 int index = 0, obsize = 0;
acd81db9 1481 int bindcount = -1;
c8ae863b 1482 Lisp_Object bucket, tem, zero;
f927c5ae
JB
1483 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1484
b7826503 1485 CHECK_STRING (string);
85cd4372 1486 if (type == 0)
a284cdbb 1487 return call3 (collection, string, predicate, Qt);
6bbd7a29 1488 allmatches = bucket = Qnil;
c8ae863b 1489 zero = make_number (0);
f927c5ae 1490
a284cdbb
RS
1491 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1492 tail = collection;
85cd4372 1493 if (type == 2)
f927c5ae 1494 {
373bf2b7 1495 collection = check_obarray (collection);
a284cdbb 1496 obsize = XVECTOR (collection)->size;
657d4c0b 1497 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1498 }
1499
1500 while (1)
1501 {
85cd4372 1502 /* Get the next element of the alist, obarray, or hash-table. */
f927c5ae
JB
1503 /* Exit the loop if the elements are all used up. */
1504 /* elt gets the alist element or symbol.
1505 eltstring gets the name to check as a completion. */
1506
85cd4372 1507 if (type == 1)
f927c5ae 1508 {
695deb18 1509 if (!CONSP (tail))
f927c5ae 1510 break;
695deb18
SM
1511 elt = XCAR (tail);
1512 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1513 tail = XCDR (tail);
f927c5ae 1514 }
85cd4372 1515 else if (type == 2)
f927c5ae 1516 {
373bf2b7 1517 if (!EQ (bucket, zero))
f927c5ae 1518 {
373bf2b7
AS
1519 if (!SYMBOLP (bucket))
1520 error ("Bad data in guts of obarray");
f927c5ae 1521 elt = bucket;
3809648a 1522 eltstring = elt;
f927c5ae
JB
1523 if (XSYMBOL (bucket)->next)
1524 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1525 else
5a866662 1526 XSETFASTINT (bucket, 0);
f927c5ae
JB
1527 }
1528 else if (++index >= obsize)
1529 break;
1530 else
1531 {
a284cdbb 1532 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1533 continue;
1534 }
1535 }
85cd4372
SM
1536 else /* if (type == 3) */
1537 {
a284cdbb
RS
1538 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1539 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
85cd4372 1540 index++;
a284cdbb 1541 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
85cd4372
SM
1542 break;
1543 else
a284cdbb 1544 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
85cd4372 1545 }
f927c5ae
JB
1546
1547 /* Is this element a possible completion? */
1548
3809648a
KS
1549 if (SYMBOLP (eltstring))
1550 eltstring = Fsymbol_name (eltstring);
1551
7510b296 1552 if (STRINGP (eltstring)
d5db4077 1553 && SCHARS (string) <= SCHARS (eltstring)
89a255dc 1554 /* If HIDE_SPACES, reject alternatives that start with space
2cbaf886 1555 unless the input starts with space. */
11e3c684
CY
1556 && (NILP (hide_spaces)
1557 || (SBYTES (string) > 0
1558 && SREF (string, 0) == ' ')
1559 || SREF (eltstring, 0) != ' ')
c8ae863b 1560 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1561 make_number (SCHARS (string)),
c8ae863b 1562 string, zero,
d5db4077 1563 make_number (SCHARS (string)),
69f4ef20
RS
1564 completion_ignore_case ? Qt : Qnil),
1565 EQ (Qt, tem)))
f927c5ae
JB
1566 {
1567 /* Yes. */
42006772
RS
1568 Lisp_Object regexps;
1569 Lisp_Object zero;
5a866662 1570 XSETFASTINT (zero, 0);
42006772
RS
1571
1572 /* Ignore this element if it fails to match all the regexps. */
6b61353c 1573 {
6b61353c
KH
1574 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1575 regexps = XCDR (regexps))
1576 {
acd81db9
DK
1577 if (bindcount < 0) {
1578 bindcount = SPECPDL_INDEX ();
1579 specbind (Qcase_fold_search,
1580 completion_ignore_case ? Qt : Qnil);
77443789 1581 }
6b61353c
KH
1582 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1583 if (NILP (tem))
1584 break;
1585 }
6b61353c
KH
1586 if (CONSP (regexps))
1587 continue;
1588 }
42006772 1589
f927c5ae
JB
1590 /* Ignore this element if there is a predicate
1591 and the predicate doesn't like it. */
1592
7efd36fc 1593 if (!NILP (predicate))
f927c5ae 1594 {
7efd36fc 1595 if (EQ (predicate, Qcommandp))
c837f4f9 1596 tem = Fcommandp (elt, Qnil);
f927c5ae
JB
1597 else
1598 {
acd81db9
DK
1599 if (bindcount >= 0) {
1600 unbind_to (bindcount, Qnil);
1601 bindcount = -1;
1602 }
f927c5ae 1603 GCPRO4 (tail, eltstring, allmatches, string);
85cd4372
SM
1604 tem = type == 3
1605 ? call2 (predicate, elt,
a284cdbb 1606 HASH_VALUE (XHASH_TABLE (collection), index - 1))
85cd4372 1607 : call1 (predicate, elt);
f927c5ae
JB
1608 UNGCPRO;
1609 }
56a98455 1610 if (NILP (tem)) continue;
f927c5ae
JB
1611 }
1612 /* Ok => put it on the list. */
1613 allmatches = Fcons (eltstring, allmatches);
1614 }
1615 }
1616
acd81db9
DK
1617 if (bindcount >= 0) {
1618 unbind_to (bindcount, Qnil);
1619 bindcount = -1;
1620 }
1621
f927c5ae
JB
1622 return Fnreverse (allmatches);
1623}
1624\f
cee54539 1625DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
af52c32d 1626 doc: /* Read a string in the minibuffer, with completion.
a1f17501 1627PROMPT is a string to prompt with; normally it ends in a colon and a space.
a284cdbb
RS
1628COLLECTION can be a list of strings, an alist, an obarray or a hash table.
1629COLLECTION can also be a function to do the completion itself.
1630PREDICATE limits completion to a subset of COLLECTION.
a1f17501 1631See `try-completion' and `all-completions' for more details
a284cdbb 1632 on completion, COLLECTION, and PREDICATE.
a1f17501 1633
4d02fc25
SM
1634REQUIRE-MATCH can take the following values:
1635- t means that the user is not allowed to exit unless
1636 the input is (or completes to) an element of COLLECTION or is null.
1637- nil means that the user can exit with any input.
b47d0e56
CY
1638- `confirm' means that the user can exit with any input, but she needs
1639 to confirm her choice if the input is not an element of COLLECTION.
1640- `confirm-after-completion' means that the user can exit with any
1641 input, but she needs to confirm her choice if she called
1642 `minibuffer-complete' right before `minibuffer-complete-and-exit'
1643 and the input is not an element of COLLECTION.
4d02fc25
SM
1644- anything else behaves like t except that typing RET does not exit if it
1645 does non-null completion.
1646
2e3148e2
JL
1647If the input is null, `completing-read' returns DEF, or the first element
1648of the list of default values, or an empty string if DEF is nil,
1649regardless of the value of REQUIRE-MATCH.
6b61353c
KH
1650
1651If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
1652 with point positioned at the end.
1653 If it is (STRING . POSITION), the initial input is STRING, but point
1654 is placed at _zero-indexed_ position POSITION in STRING. (*Note*
1655 that this is different from `read-from-minibuffer' and related
1656 functions, which use one-indexing for POSITION.) This feature is
1657 deprecated--it is best to pass nil for INITIAL-INPUT and supply the
1658 default value DEF instead. The user can yank the default value into
1659 the minibuffer easily using \\[next-history-element].
1660
1661HIST, if non-nil, specifies a history list and optionally the initial
1662 position in the list. It can be a symbol, which is the history list
1663 variable to use, or it can be a cons cell (HISTVAR . HISTPOS). In
1664 that case, HISTVAR is the history list variable to use, and HISTPOS
1665 is the initial position (the position in the list used by the
1666 minibuffer history commands). For consistency, you should also
1667 specify that element of the history as the value of
1fb101f3 1668 INITIAL-INPUT. (This is the only case in which you should use
6b61353c
KH
1669 INITIAL-INPUT instead of DEF.) Positions are counted starting from
1670 1 at the beginning of the list. The variable `history-length'
1671 controls the maximum length of a history list.
38668f81 1672
2e3148e2 1673DEF, if non-nil, is the default value or the list of default values.
a1f17501
PJ
1674
1675If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
029a305c 1676 the current input method and the setting of `enable-multibyte-characters'.
a1f17501
PJ
1677
1678Completion ignores case if the ambient value of
af52c32d 1679 `completion-ignore-case' is non-nil. */)
5842a27b 1680 (Lisp_Object prompt, Lisp_Object collection, Lisp_Object predicate, Lisp_Object require_match, Lisp_Object initial_input, Lisp_Object hist, Lisp_Object def, Lisp_Object inherit_input_method)
f927c5ae 1681{
770970cb 1682 Lisp_Object val, histvar, histpos, position;
d43f85c2 1683 Lisp_Object init;
770970cb 1684 int pos = 0;
aed13378 1685 int count = SPECPDL_INDEX ();
0c8ee1a2
RS
1686 struct gcpro gcpro1;
1687
d43f85c2 1688 init = initial_input;
0c8ee1a2
RS
1689 GCPRO1 (def);
1690
a284cdbb 1691 specbind (Qminibuffer_completion_table, collection);
7efd36fc 1692 specbind (Qminibuffer_completion_predicate, predicate);
f927c5ae 1693 specbind (Qminibuffer_completion_confirm,
695deb18 1694 EQ (require_match, Qt) ? Qnil : require_match);
770970cb
RS
1695
1696 position = Qnil;
1697 if (!NILP (init))
1698 {
7510b296 1699 if (CONSP (init))
770970cb
RS
1700 {
1701 position = Fcdr (init);
1702 init = Fcar (init);
1703 }
b7826503 1704 CHECK_STRING (init);
770970cb
RS
1705 if (!NILP (position))
1706 {
b7826503 1707 CHECK_NUMBER (position);
770970cb 1708 /* Convert to distance from end of input. */
d5db4077 1709 pos = XINT (position) - SCHARS (init);
770970cb
RS
1710 }
1711 }
1712
7510b296 1713 if (SYMBOLP (hist))
770970cb
RS
1714 {
1715 histvar = hist;
1716 histpos = Qnil;
1717 }
1718 else
1719 {
1720 histvar = Fcar_safe (hist);
1721 histpos = Fcdr_safe (hist);
1722 }
1723 if (NILP (histvar))
1724 histvar = Qminibuffer_history;
1725 if (NILP (histpos))
5a866662 1726 XSETFASTINT (histpos, 0);
770970cb 1727
56a98455 1728 val = read_minibuf (NILP (require_match)
bb0bdf70 1729 ? (NILP (Vminibuffer_completing_file_name)
ef72eaff 1730 || EQ (Vminibuffer_completing_file_name, Qlambda)
bb0bdf70
EZ
1731 ? Vminibuffer_local_completion_map
1732 : Vminibuffer_local_filename_completion_map)
1733 : (NILP (Vminibuffer_completing_file_name)
ef72eaff 1734 || EQ (Vminibuffer_completing_file_name, Qlambda)
bb0bdf70 1735 ? Vminibuffer_local_must_match_map
8ba31f36 1736 : Vminibuffer_local_filename_must_match_map),
85b5fe07 1737 init, prompt, make_number (pos), 0,
cee54539 1738 histvar, histpos, def, 0,
b35cd215 1739 !NILP (inherit_input_method));
b9a86585 1740
d5db4077 1741 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
fb30dfd2 1742 val = CONSP (def) ? XCAR (def) : def;
b9a86585 1743
0c8ee1a2 1744 RETURN_UNGCPRO (unbind_to (count, val));
f927c5ae
JB
1745}
1746\f
971de7fb 1747Lisp_Object Fassoc_string (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold);
5d2ca7ae
RS
1748
1749/* Test whether TXT is an exact completion. */
695deb18
SM
1750DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1751 doc: /* Return non-nil if STRING is a valid completion.
1752Takes the same arguments as `all-completions' and `try-completion'.
a284cdbb 1753If COLLECTION is a function, it is called with three arguments:
695deb18 1754the values STRING, PREDICATE and `lambda'. */)
5842a27b 1755 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
5d2ca7ae 1756{
6b61353c 1757 Lisp_Object regexps, tail, tem = Qnil;
85cd4372 1758 int i = 0;
5d2ca7ae 1759
695deb18
SM
1760 CHECK_STRING (string);
1761
a284cdbb
RS
1762 if ((CONSP (collection)
1763 && (!SYMBOLP (XCAR (collection)) || NILP (XCAR (collection))))
1764 || NILP (collection))
695deb18 1765 {
a284cdbb 1766 tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
9f4a578d 1767 if (NILP (tem))
695deb18
SM
1768 return Qnil;
1769 }
a284cdbb 1770 else if (VECTORP (collection))
5d2ca7ae 1771 {
695deb18 1772 /* Bypass intern-soft as that loses for nil. */
a284cdbb 1773 tem = oblookup (collection,
42a5b22f 1774 SSDATA (string),
d5db4077
KR
1775 SCHARS (string),
1776 SBYTES (string));
44472c88 1777 if (!SYMBOLP (tem))
9404446f 1778 {
695deb18
SM
1779 if (STRING_MULTIBYTE (string))
1780 string = Fstring_make_unibyte (string);
9404446f 1781 else
695deb18 1782 string = Fstring_make_multibyte (string);
9404446f 1783
a284cdbb 1784 tem = oblookup (collection,
42a5b22f 1785 SSDATA (string),
d5db4077
KR
1786 SCHARS (string),
1787 SBYTES (string));
9404446f 1788 }
6b61353c
KH
1789
1790 if (completion_ignore_case && !SYMBOLP (tem))
1791 {
a284cdbb 1792 for (i = XVECTOR (collection)->size - 1; i >= 0; i--)
6b61353c 1793 {
a284cdbb 1794 tail = XVECTOR (collection)->contents[i];
6b61353c
KH
1795 if (SYMBOLP (tail))
1796 while (1)
1797 {
1798 if (EQ((Fcompare_strings (string, make_number (0), Qnil,
1799 Fsymbol_name (tail),
1800 make_number (0) , Qnil, Qt)),
1801 Qt))
1802 {
1803 tem = tail;
1804 break;
1805 }
1806 if (XSYMBOL (tail)->next == 0)
1807 break;
1808 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1809 }
1810 }
1811 }
1812
1813 if (!SYMBOLP (tem))
1814 return Qnil;
5d2ca7ae 1815 }
a284cdbb 1816 else if (HASH_TABLE_P (collection))
85cd4372 1817 {
a284cdbb 1818 struct Lisp_Hash_Table *h = XHASH_TABLE (collection);
6b61353c 1819 i = hash_lookup (h, string, NULL);
85cd4372 1820 if (i >= 0)
6b61353c 1821 tem = HASH_KEY (h, i);
85cd4372 1822 else
6b61353c
KH
1823 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1824 if (!NILP (HASH_HASH (h, i)) &&
1825 EQ (Fcompare_strings (string, make_number (0), Qnil,
1826 HASH_KEY (h, i), make_number (0) , Qnil,
1827 completion_ignore_case ? Qt : Qnil),
1828 Qt))
1829 {
1830 tem = HASH_KEY (h, i);
1831 break;
1832 }
1833 if (!STRINGP (tem))
85cd4372
SM
1834 return Qnil;
1835 }
5d2ca7ae 1836 else
a284cdbb 1837 return call3 (collection, string, predicate, Qlambda);
695deb18
SM
1838
1839 /* Reject this element if it fails to match all the regexps. */
77443789
AS
1840 if (CONSP (Vcompletion_regexp_list))
1841 {
1842 int count = SPECPDL_INDEX ();
1843 specbind (Qcase_fold_search, completion_ignore_case ? Qt : Qnil);
1844 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1845 regexps = XCDR (regexps))
1846 {
1847 if (NILP (Fstring_match (XCAR (regexps),
1848 SYMBOLP (tem) ? string : tem,
1849 Qnil)))
1850 return unbind_to (count, Qnil);
1851 }
1852 unbind_to (count, Qnil);
1853 }
695deb18
SM
1854
1855 /* Finally, check the predicate. */
1856 if (!NILP (predicate))
6b61353c 1857 {
a284cdbb
RS
1858 return HASH_TABLE_P (collection)
1859 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (collection), i))
6b61353c
KH
1860 : call1 (predicate, tem);
1861 }
695deb18
SM
1862 else
1863 return Qt;
5d2ca7ae 1864}
f927c5ae 1865
655ea21c
JD
1866DEFUN ("internal-complete-buffer", Finternal_complete_buffer, Sinternal_complete_buffer, 3, 3, 0,
1867 doc: /* Perform completion on buffer names.
1868If the argument FLAG is nil, invoke `try-completion', if it's t, invoke
1869`all-completions', otherwise invoke `test-completion'.
1870
ba5524f4 1871The arguments STRING and PREDICATE are as in `try-completion',
655ea21c 1872`all-completions', and `test-completion'. */)
5842a27b 1873 (Lisp_Object string, Lisp_Object predicate, Lisp_Object flag)
655ea21c
JD
1874{
1875 if (NILP (flag))
1876 return Ftry_completion (string, Vbuffer_alist, predicate);
1877 else if (EQ (flag, Qt))
8c2dccbf 1878 {
73913e2e 1879 Lisp_Object res = Fall_completions (string, Vbuffer_alist, predicate, Qnil);
8c2dccbf
SM
1880 if (SCHARS (string) > 0)
1881 return res;
1882 else
1883 { /* Strip out internal buffers. */
1884 Lisp_Object bufs = res;
1885 /* First, look for a non-internal buffer in `res'. */
1886 while (CONSP (bufs) && SREF (XCAR (bufs), 0) == ' ')
1887 bufs = XCDR (bufs);
1888 if (NILP (bufs))
1889 /* All bufs in `res' are internal, so don't trip them out. */
1890 return res;
1891 res = bufs;
1892 while (CONSP (XCDR (bufs)))
1893 if (SREF (XCAR (XCDR (bufs)), 0) == ' ')
1894 XSETCDR (bufs, XCDR (XCDR (bufs)));
1895 else
1896 bufs = XCDR (bufs);
1897 return res;
1898 }
1899 }
655ea21c
JD
1900 else /* assume `lambda' */
1901 return Ftest_completion (string, Vbuffer_alist, predicate);
1902}
1903
52b14ac0
JB
1904/* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1905
695deb18 1906DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
d3fc66fb 1907 doc: /* Like `assoc' but specifically for strings (and symbols).
c6a82c18
CY
1908
1909This returns the first element of LIST whose car matches the string or
1910symbol KEY, or nil if no match exists. When performing the
1911comparison, symbols are first converted to strings, and unibyte
1912strings to multibyte. If the optional arg CASE-FOLD is non-nil, case
1913is ignored.
1914
1915Unlike `assoc', KEY can also match an entry in LIST consisting of a
1916single string, rather than a cons cell whose car is a string. */)
5842a27b 1917 (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold)
52b14ac0
JB
1918{
1919 register Lisp_Object tail;
1920
d3fc66fb
KS
1921 if (SYMBOLP (key))
1922 key = Fsymbol_name (key);
1923
99784d63 1924 for (tail = list; CONSP (tail); tail = XCDR (tail))
52b14ac0
JB
1925 {
1926 register Lisp_Object elt, tem, thiscar;
99784d63 1927 elt = XCAR (tail);
695deb18 1928 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1954495f
KS
1929 if (SYMBOLP (thiscar))
1930 thiscar = Fsymbol_name (thiscar);
1931 else if (!STRINGP (thiscar))
52b14ac0 1932 continue;
9404446f
RS
1933 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1934 key, make_number (0), Qnil,
695deb18 1935 case_fold);
9404446f
RS
1936 if (EQ (tem, Qt))
1937 return elt;
52b14ac0
JB
1938 QUIT;
1939 }
1940 return Qnil;
1941}
f927c5ae 1942
e4c97a67 1943\f
f927c5ae 1944DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
af52c32d 1945 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
5842a27b 1946 (void)
f927c5ae
JB
1947{
1948 return make_number (minibuf_level);
1949}
1950
37e9a934 1951DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
af52c32d
MB
1952 doc: /* Return the prompt string of the currently-active minibuffer.
1953If no minibuffer is active, return nil. */)
5842a27b 1954 (void)
37e9a934 1955{
4d04c1f1 1956 return Fcopy_sequence (minibuf_prompt);
37e9a934
KH
1957}
1958
f927c5ae 1959\f
9d815fd9 1960void
971de7fb 1961init_minibuf_once (void)
f927c5ae
JB
1962{
1963 Vminibuffer_list = Qnil;
1964 staticpro (&Vminibuffer_list);
1965}
1966
9d815fd9 1967void
971de7fb 1968syms_of_minibuf (void)
f927c5ae
JB
1969{
1970 minibuf_level = 0;
4d04c1f1
KH
1971 minibuf_prompt = Qnil;
1972 staticpro (&minibuf_prompt);
1973
1974 minibuf_save_list = Qnil;
1975 staticpro (&minibuf_save_list);
f927c5ae 1976
d67b4f80 1977 Qcompletion_ignore_case = intern_c_string ("completion-ignore-case");
9fa5c213
GM
1978 staticpro (&Qcompletion_ignore_case);
1979
d67b4f80 1980 Qread_file_name_internal = intern_c_string ("read-file-name-internal");
719b4a40
RS
1981 staticpro (&Qread_file_name_internal);
1982
d67b4f80 1983 Qminibuffer_default = intern_c_string ("minibuffer-default");
e5d4686b
RS
1984 staticpro (&Qminibuffer_default);
1985 Fset (Qminibuffer_default, Qnil);
1986
d67b4f80 1987 Qminibuffer_completion_table = intern_c_string ("minibuffer-completion-table");
f927c5ae
JB
1988 staticpro (&Qminibuffer_completion_table);
1989
d67b4f80 1990 Qminibuffer_completion_confirm = intern_c_string ("minibuffer-completion-confirm");
f927c5ae
JB
1991 staticpro (&Qminibuffer_completion_confirm);
1992
d67b4f80 1993 Qminibuffer_completion_predicate = intern_c_string ("minibuffer-completion-predicate");
f927c5ae
JB
1994 staticpro (&Qminibuffer_completion_predicate);
1995
1996 staticpro (&last_minibuf_string);
1997 last_minibuf_string = Qnil;
1998
d67b4f80 1999 Quser_variable_p = intern_c_string ("user-variable-p");
f927c5ae
JB
2000 staticpro (&Quser_variable_p);
2001
d67b4f80 2002 Qminibuffer_history = intern_c_string ("minibuffer-history");
770970cb 2003 staticpro (&Qminibuffer_history);
f927c5ae 2004
d67b4f80 2005 Qbuffer_name_history = intern_c_string ("buffer-name-history");
406e55df 2006 staticpro (&Qbuffer_name_history);
33d0a17f 2007 Fset (Qbuffer_name_history, Qnil);
406e55df 2008
d67b4f80 2009 Qminibuffer_setup_hook = intern_c_string ("minibuffer-setup-hook");
5c781212
RS
2010 staticpro (&Qminibuffer_setup_hook);
2011
d67b4f80 2012 Qminibuffer_exit_hook = intern_c_string ("minibuffer-exit-hook");
177aecf9
KH
2013 staticpro (&Qminibuffer_exit_hook);
2014
d67b4f80 2015 Qhistory_length = intern_c_string ("history-length");
77aa8edf
RS
2016 staticpro (&Qhistory_length);
2017
d67b4f80 2018 Qcurrent_input_method = intern_c_string ("current-input-method");
cee54539
KH
2019 staticpro (&Qcurrent_input_method);
2020
d67b4f80 2021 Qactivate_input_method = intern_c_string ("activate-input-method");
cee54539
KH
2022 staticpro (&Qactivate_input_method);
2023
d67b4f80 2024 Qcase_fold_search = intern_c_string ("case-fold-search");
6b61353c
KH
2025 staticpro (&Qcase_fold_search);
2026
c11136ec
GM
2027 DEFVAR_LISP ("read-expression-history", Vread_expression_history,
2028 doc: /* A history list for arguments that are Lisp expressions to evaluate.
2029For example, `eval-expression' uses this. */);
2030 Vread_expression_history = Qnil;
2031
d67b4f80 2032 Qread_expression_history = intern_c_string ("read-expression-history");
627fb581
RS
2033 staticpro (&Qread_expression_history);
2034
29208e82 2035 DEFVAR_LISP ("read-buffer-function", Vread_buffer_function,
b4f588fa
JB
2036 doc: /* If this is non-nil, `read-buffer' does its work by calling this function.
2037The function is called with the arguments passed to `read-buffer'. */);
eebbfb01
KH
2038 Vread_buffer_function = Qnil;
2039
33df281c 2040 DEFVAR_BOOL ("read-buffer-completion-ignore-case",
29208e82 2041 read_buffer_completion_ignore_case,
33df281c
CY
2042 doc: /* *Non-nil means completion ignores case when reading a buffer name. */);
2043 read_buffer_completion_ignore_case = 0;
2044
29208e82 2045 DEFVAR_LISP ("minibuffer-setup-hook", Vminibuffer_setup_hook,
af52c32d 2046 doc: /* Normal hook run just after entry to minibuffer. */);
5c781212
RS
2047 Vminibuffer_setup_hook = Qnil;
2048
29208e82 2049 DEFVAR_LISP ("minibuffer-exit-hook", Vminibuffer_exit_hook,
af52c32d 2050 doc: /* Normal hook run just after exit from minibuffer. */);
177aecf9
KH
2051 Vminibuffer_exit_hook = Qnil;
2052
29208e82 2053 DEFVAR_LISP ("history-length", Vhistory_length,
af52c32d 2054 doc: /* *Maximum length for history lists before truncation takes place.
a1f17501 2055A number means that length; t means infinite. Truncation takes place
f4b46bb9 2056just after a new element is inserted. Setting the `history-length'
a1f17501 2057property of a history variable overrides this default. */);
77aa8edf
RS
2058 XSETFASTINT (Vhistory_length, 30);
2059
29208e82 2060 DEFVAR_BOOL ("history-delete-duplicates", history_delete_duplicates,
0da4d471
JL
2061 doc: /* *Non-nil means to delete duplicates in history.
2062If set to t when adding a new history element, all previous identical
179a4ca7 2063elements are deleted from the history list. */);
0da4d471
JL
2064 history_delete_duplicates = 0;
2065
29208e82 2066 DEFVAR_LISP ("history-add-new-input", Vhistory_add_new_input,
179a4ca7
JL
2067 doc: /* *Non-nil means to add new elements in history.
2068If set to nil, minibuffer reading functions don't add new elements to the
2069history list, so it is possible to do this afterwards by calling
2070`add-to-history' explicitly. */);
2071 Vhistory_add_new_input = Qt;
2072
29208e82 2073 DEFVAR_BOOL ("completion-ignore-case", completion_ignore_case,
75028673 2074 doc: /* Non-nil means don't consider case significant in completion.
f562df03
CY
2075For file-name completion, `read-file-name-completion-ignore-case'
2076controls the behavior, rather than this variable.
2077For buffer name completion, `read-buffer-completion-ignore-case'
d9a03ad2 2078controls the behavior, rather than this variable. */);
f927c5ae
JB
2079 completion_ignore_case = 0;
2080
29208e82 2081 DEFVAR_BOOL ("enable-recursive-minibuffers", enable_recursive_minibuffers,
af52c32d 2082 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
a1f17501 2083This variable makes a difference whenever the minibuffer window is active. */);
f927c5ae
JB
2084 enable_recursive_minibuffers = 0;
2085
29208e82 2086 DEFVAR_LISP ("minibuffer-completion-table", Vminibuffer_completion_table,
af52c32d 2087 doc: /* Alist or obarray used for completion in the minibuffer.
6b61353c
KH
2088This becomes the ALIST argument to `try-completion' and `all-completions'.
2089The value can also be a list of strings or a hash table.
a1f17501
PJ
2090
2091The value may alternatively be a function, which is given three arguments:
2092 STRING, the current buffer contents;
2093 PREDICATE, the predicate for filtering possible matches;
2094 CODE, which says what kind of things to do.
b2ef9714
JB
2095CODE can be nil, t or `lambda':
2096 nil -- return the best completion of STRING, or nil if there is none.
2097 t -- return a list of all possible completions of STRING.
2098 lambda -- return t if STRING is a valid completion as it stands. */);
f927c5ae
JB
2099 Vminibuffer_completion_table = Qnil;
2100
29208e82 2101 DEFVAR_LISP ("minibuffer-completion-predicate", Vminibuffer_completion_predicate,
af52c32d 2102 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
f927c5ae
JB
2103 Vminibuffer_completion_predicate = Qnil;
2104
29208e82 2105 DEFVAR_LISP ("minibuffer-completion-confirm", Vminibuffer_completion_confirm,
b47d0e56
CY
2106 doc: /* Whether to demand confirmation of completion before exiting minibuffer.
2107If nil, confirmation is not required.
2108If the value is `confirm', the user may exit with an input that is not
2109 a valid completion alternative, but Emacs asks for confirmation.
2110If the value is `confirm-after-completion', the user may exit with an
2111 input that is not a valid completion alternative, but Emacs asks for
1b79e224
CY
2112 confirmation if the user submitted the input right after any of the
2113 completion commands listed in `minibuffer-confirm-exit-commands'. */);
f927c5ae
JB
2114 Vminibuffer_completion_confirm = Qnil;
2115
ccf330a6 2116 DEFVAR_LISP ("minibuffer-completing-file-name",
29208e82 2117 Vminibuffer_completing_file_name,
6ddfc113 2118 doc: /* Non-nil means completing file names. */);
ccf330a6
RS
2119 Vminibuffer_completing_file_name = Qnil;
2120
29208e82 2121 DEFVAR_LISP ("minibuffer-help-form", Vminibuffer_help_form,
af52c32d 2122 doc: /* Value that `help-form' takes on inside the minibuffer. */);
f927c5ae
JB
2123 Vminibuffer_help_form = Qnil;
2124
29208e82 2125 DEFVAR_LISP ("minibuffer-history-variable", Vminibuffer_history_variable,
af52c32d 2126 doc: /* History list symbol to add minibuffer values to.
a1f17501
PJ
2127Each string of minibuffer input, as it appears on exit from the minibuffer,
2128is added with
2129 (set minibuffer-history-variable
2130 (cons STRING (symbol-value minibuffer-history-variable))) */);
5a866662 2131 XSETFASTINT (Vminibuffer_history_variable, 0);
770970cb 2132
29208e82 2133 DEFVAR_LISP ("minibuffer-history-position", Vminibuffer_history_position,
af52c32d 2134 doc: /* Current position of redoing in the history list. */);
770970cb
RS
2135 Vminibuffer_history_position = Qnil;
2136
29208e82 2137 DEFVAR_BOOL ("minibuffer-auto-raise", minibuffer_auto_raise,
af52c32d 2138 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
a1f17501 2139Some uses of the echo area also raise that frame (since they use it too). */);
6a9ee000
RS
2140 minibuffer_auto_raise = 0;
2141
29208e82 2142 DEFVAR_LISP ("completion-regexp-list", Vcompletion_regexp_list,
6b61353c
KH
2143 doc: /* List of regexps that should restrict possible completions.
2144The basic completion functions only consider a completion acceptable
2145if it matches all regular expressions in this list, with
2146`case-fold-search' bound to the value of `completion-ignore-case'.
2147See Info node `(elisp)Basic Completion', for a description of these
2148functions. */);
42006772
RS
2149 Vcompletion_regexp_list = Qnil;
2150
c3421833 2151 DEFVAR_BOOL ("minibuffer-allow-text-properties",
29208e82 2152 minibuffer_allow_text_properties,
af52c32d 2153 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
a1f17501
PJ
2154This also affects `read-string', but it does not affect `read-minibuffer',
2155`read-no-blanks-input', or any of the functions that do minibuffer input
2156with completion; they always discard text properties. */);
c3421833
RS
2157 minibuffer_allow_text_properties = 0;
2158
29208e82 2159 DEFVAR_LISP ("minibuffer-prompt-properties", Vminibuffer_prompt_properties,
af52c32d 2160 doc: /* Text properties that are added to minibuffer prompts.
a1f17501
PJ
2161These are in addition to the basic `field' property, and stickiness
2162properties. */);
cc64f5c9
MB
2163 /* We use `intern' here instead of Qread_only to avoid
2164 initialization-order problems. */
2165 Vminibuffer_prompt_properties
d67b4f80 2166 = Fcons (intern_c_string ("read-only"), Fcons (Qt, Qnil));
cc64f5c9 2167
29208e82 2168 DEFVAR_LISP ("read-expression-map", Vread_expression_map,
627fb581
RS
2169 doc: /* Minibuffer keymap used for reading Lisp expressions. */);
2170 Vread_expression_map = Qnil;
2171
68313ed8 2172 defsubr (&Sset_minibuffer_window);
f927c5ae
JB
2173 defsubr (&Sread_from_minibuffer);
2174 defsubr (&Seval_minibuffer);
2175 defsubr (&Sread_minibuffer);
2176 defsubr (&Sread_string);
2177 defsubr (&Sread_command);
2178 defsubr (&Sread_variable);
ff137f16 2179 defsubr (&Sinternal_complete_buffer);
f927c5ae
JB
2180 defsubr (&Sread_buffer);
2181 defsubr (&Sread_no_blanks_input);
2182 defsubr (&Sminibuffer_depth);
37e9a934 2183 defsubr (&Sminibuffer_prompt);
f927c5ae 2184
873ef78e 2185 defsubr (&Sminibufferp);
a346ec72
MB
2186 defsubr (&Sminibuffer_prompt_end);
2187 defsubr (&Sminibuffer_contents);
2188 defsubr (&Sminibuffer_contents_no_properties);
b5e1e449 2189 defsubr (&Sminibuffer_completion_contents);
a346ec72 2190
f927c5ae
JB
2191 defsubr (&Stry_completion);
2192 defsubr (&Sall_completions);
695deb18
SM
2193 defsubr (&Stest_completion);
2194 defsubr (&Sassoc_string);
f927c5ae 2195 defsubr (&Scompleting_read);
f927c5ae 2196}