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