Update md5 checksums
[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,
5df4f04c 4 2008, 2009, 2010, 2011 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{
627fb581
RS
1029 return Feval (read_minibuf (Vread_expression_map, initial_contents,
1030 prompt, Qnil, 1, Qread_expression_history,
1031 make_number (0), Qnil, 0, 0));
f927c5ae
JB
1032}
1033
1034/* Functions that use the minibuffer to read various things. */
1035
cee54539 1036DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
af52c32d 1037 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
a1f17501 1038If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
6b61353c
KH
1039 This argument has been superseded by DEFAULT-VALUE and should normally
1040 be nil in new code. It behaves as in `read-from-minibuffer'. See the
1041 documentation string of that function for details.
a1f17501
PJ
1042The third arg HISTORY, if non-nil, specifies a history list
1043 and optionally the initial position in the list.
1044See `read-from-minibuffer' for details of HISTORY argument.
2e3148e2
JL
1045Fourth arg DEFAULT-VALUE is the default value or the list of default values.
1046 If non-nil, it is used for history commands, and as the value (or the first
1047 element of the list of default values) to return if the user enters the
1048 empty string.
a1f17501 1049Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
029a305c 1050 the current input method and the setting of `enable-multibyte-characters'. */)
5842a27b 1051 (Lisp_Object prompt, Lisp_Object initial_input, Lisp_Object history, Lisp_Object default_value, Lisp_Object inherit_input_method)
f927c5ae 1052{
b9a86585
RS
1053 Lisp_Object val;
1054 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
1055 Qnil, history, default_value,
b35cd215 1056 inherit_input_method);
d5db4077 1057 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
fb30dfd2 1058 val = CONSP (default_value) ? XCAR (default_value) : default_value;
b9a86585 1059 return val;
f927c5ae
JB
1060}
1061
cee54539 1062DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
af52c32d 1063 doc: /* Read a string from the terminal, not allowing blanks.
6b61353c
KH
1064Prompt with PROMPT. Whitespace terminates the input. If INITIAL is
1065non-nil, it should be a string, which is used as initial input, with
1066point positioned at the end, so that SPACE will accept the input.
1067\(Actually, INITIAL can also be a cons of a string and an integer.
1068Such values are treated as in `read-from-minibuffer', but are normally
1069not useful in this function.)
a1f17501 1070Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
6b61353c 1071the current input method and the setting of`enable-multibyte-characters'. */)
5842a27b 1072 (Lisp_Object prompt, Lisp_Object initial, Lisp_Object inherit_input_method)
f927c5ae 1073{
b7826503 1074 CHECK_STRING (prompt);
c923ccc9 1075 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
cee54539 1076 0, Qminibuffer_history, make_number (0), Qnil, 0,
b35cd215 1077 !NILP (inherit_input_method));
f927c5ae
JB
1078}
1079
e5d4686b 1080DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
af52c32d 1081 doc: /* Read the name of a command and return as a symbol.
2e3148e2
JL
1082Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1083if it is a list. */)
5842a27b 1084 (Lisp_Object prompt, Lisp_Object default_value)
f927c5ae 1085{
7c8d9931
RS
1086 Lisp_Object name, default_string;
1087
1088 if (NILP (default_value))
1089 default_string = Qnil;
1090 else if (SYMBOLP (default_value))
caa4733e 1091 default_string = SYMBOL_NAME (default_value);
7c8d9931
RS
1092 else
1093 default_string = default_value;
9ab4a725 1094
7c8d9931
RS
1095 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1096 Qnil, Qnil, default_string, Qnil);
1097 if (NILP (name))
1098 return name;
1099 return Fintern (name, Qnil);
f927c5ae
JB
1100}
1101
1102#ifdef NOTDEF
1103DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
af52c32d 1104 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
fd771ceb 1105Prompt with PROMPT. */)
5842a27b 1106 (Lisp_Object prompt)
f927c5ae 1107{
cee54539 1108 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
f927c5ae
JB
1109 Qnil);
1110}
1111#endif /* NOTDEF */
1112
e5d4686b 1113DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
af52c32d 1114 doc: /* Read the name of a user variable and return it as a symbol.
2e3148e2
JL
1115Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1116if it is a list.
316f9bfc 1117A user variable is one for which `user-variable-p' returns non-nil. */)
5842a27b 1118 (Lisp_Object prompt, Lisp_Object default_value)
f927c5ae 1119{
7c8d9931
RS
1120 Lisp_Object name, default_string;
1121
1122 if (NILP (default_value))
1123 default_string = Qnil;
1124 else if (SYMBOLP (default_value))
caa4733e 1125 default_string = SYMBOL_NAME (default_value);
7c8d9931
RS
1126 else
1127 default_string = default_value;
9ab4a725 1128
7c8d9931
RS
1129 name = Fcompleting_read (prompt, Vobarray,
1130 Quser_variable_p, Qt,
1131 Qnil, Qnil, default_string, Qnil);
1132 if (NILP (name))
1133 return name;
1134 return Fintern (name, Qnil);
f927c5ae
JB
1135}
1136
1137DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
fd771ceb
PJ
1138 doc: /* Read the name of a buffer and return as a string.
1139Prompt with PROMPT.
a1f17501 1140Optional second arg DEF is value to return if user enters an empty line.
2e3148e2 1141 If DEF is a list of default values, return its first element.
5fedada7
CY
1142Optional third arg REQUIRE-MATCH determines whether non-existing
1143 buffer names are allowed. It has the same meaning as the
a2a0d36b 1144 REQUIRE-MATCH argument of `completing-read'.
33df281c
CY
1145The argument PROMPT should be a string ending with a colon and a space.
1146If `read-buffer-completion-ignore-case' is non-nil, completion ignores
1147case while reading the buffer name.
1148If `read-buffer-function' is non-nil, this works by calling it as a
1149function, instead of the usual behavior. */)
5842a27b 1150 (Lisp_Object prompt, Lisp_Object def, Lisp_Object require_match)
f927c5ae 1151{
33df281c 1152 Lisp_Object args[4], result;
97c3e4cc
RF
1153 unsigned char *s;
1154 int len;
33df281c 1155 int count = SPECPDL_INDEX ();
9ab4a725 1156
7510b296 1157 if (BUFFERP (def))
f927c5ae 1158 def = XBUFFER (def)->name;
eebbfb01 1159
33df281c
CY
1160 specbind (Qcompletion_ignore_case,
1161 read_buffer_completion_ignore_case ? Qt : Qnil);
1162
eebbfb01
KH
1163 if (NILP (Vread_buffer_function))
1164 {
1165 if (!NILP (def))
1166 {
97c3e4cc
RF
1167 /* A default value was provided: we must change PROMPT,
1168 editing the default value in before the colon. To achieve
1169 this, we replace PROMPT with a substring that doesn't
1170 contain the terminal space and colon (if present). They
1171 are then added back using Fformat. */
1172
1173 if (STRINGP (prompt))
1174 {
1175 s = SDATA (prompt);
1176 len = strlen (s);
1177 if (len >= 2 && s[len - 2] == ':' && s[len - 1] == ' ')
1178 len = len - 2;
1179 else if (len >= 1 && (s[len - 1] == ':' || s[len - 1] == ' '))
1180 len--;
1181
1182 prompt = make_specified_string (s, -1, len,
1183 STRING_MULTIBYTE (prompt));
1184 }
1185
1186 args[0] = build_string ("%s (default %s): ");
eebbfb01 1187 args[1] = prompt;
fb30dfd2 1188 args[2] = CONSP (def) ? XCAR (def) : def;
eebbfb01
KH
1189 prompt = Fformat (3, args);
1190 }
1191
33df281c
CY
1192 result = Fcompleting_read (prompt, intern ("internal-complete-buffer"),
1193 Qnil, require_match, Qnil, Qbuffer_name_history,
1194 def, Qnil);
eebbfb01
KH
1195 }
1196 else
f927c5ae 1197 {
eebbfb01 1198 args[0] = Vread_buffer_function;
f927c5ae
JB
1199 args[1] = prompt;
1200 args[2] = def;
eebbfb01 1201 args[3] = require_match;
33df281c 1202 result = Ffuncall(4, args);
f927c5ae 1203 }
33df281c 1204 return unbind_to (count, result);
f927c5ae
JB
1205}
1206\f
ec067ec7 1207static Lisp_Object
971de7fb 1208minibuf_conform_representation (Lisp_Object string, Lisp_Object basis)
ec067ec7
RS
1209{
1210 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1211 return string;
1212
1213 if (STRING_MULTIBYTE (string))
1214 return Fstring_make_unibyte (string);
1215 else
1216 return Fstring_make_multibyte (string);
1217}
1218
f927c5ae 1219DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
a284cdbb
RS
1220 doc: /* Return common substring of all completions of STRING in COLLECTION.
1221Test each possible completion specified by COLLECTION
1222to see if it begins with STRING. The possible completions may be
3809648a
KS
1223strings or symbols. Symbols are converted to strings before testing,
1224see `symbol-name'.
a284cdbb
RS
1225All that match STRING are compared together; the longest initial sequence
1226common to all these matches is the return value.
1227If there is no match at all, the return value is nil.
1228For a unique match which is exact, the return value is t.
1229
1230If COLLECTION is an alist, the keys (cars of elements) are the
1231possible completions. If an element is not a cons cell, then the
1232element itself is the possible completion.
1233If COLLECTION is a hash-table, all the keys that are strings or symbols
1234are the possible completions.
1235If COLLECTION is an obarray, the names of all symbols in the obarray
1236are the possible completions.
1237
1238COLLECTION can also be a function to do the completion itself.
a1f17501
PJ
1239It receives three arguments: the values STRING, PREDICATE and nil.
1240Whatever it returns becomes the value of `try-completion'.
1241
1242If optional third argument PREDICATE is non-nil,
1243it is used to test each possible match.
1244The match is a candidate only if PREDICATE returns non-nil.
1245The argument given to PREDICATE is the alist element
a284cdbb 1246or the symbol from the obarray. If COLLECTION is a hash-table,
85cd4372 1247predicate is called with two arguments: the key and the value.
a1f17501 1248Additionally to this predicate, `completion-regexp-list'
af52c32d 1249is used to further constrain the set of candidates. */)
5842a27b 1250 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
f927c5ae
JB
1251{
1252 Lisp_Object bestmatch, tail, elt, eltstring;
af2b7cd5 1253 /* Size in bytes of BESTMATCH. */
6bbd7a29 1254 int bestmatchsize = 0;
af2b7cd5 1255 /* These are in bytes, too. */
f927c5ae 1256 int compare, matchsize;
f6d4c7a4
RS
1257 enum { function_table, list_table, obarray_table, hash_table}
1258 type = (HASH_TABLE_P (collection) ? hash_table
1259 : VECTORP (collection) ? obarray_table
1260 : ((NILP (collection)
1261 || (CONSP (collection)
1262 && (!SYMBOLP (XCAR (collection))
1263 || NILP (XCAR (collection)))))
1264 ? list_table : function_table));
6bbd7a29 1265 int index = 0, obsize = 0;
f927c5ae 1266 int matchcount = 0;
acd81db9 1267 int bindcount = -1;
f927c5ae
JB
1268 Lisp_Object bucket, zero, end, tem;
1269 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1270
b7826503 1271 CHECK_STRING (string);
f6d4c7a4 1272 if (type == function_table)
a284cdbb 1273 return call3 (collection, string, predicate, Qnil);
f927c5ae 1274
6bbd7a29 1275 bestmatch = bucket = Qnil;
c8ae863b 1276 zero = make_number (0);
f927c5ae 1277
a284cdbb
RS
1278 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1279 tail = collection;
f6d4c7a4 1280 if (type == obarray_table)
f927c5ae 1281 {
a284cdbb
RS
1282 collection = check_obarray (collection);
1283 obsize = XVECTOR (collection)->size;
1284 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1285 }
1286
1287 while (1)
1288 {
85cd4372 1289 /* Get the next element of the alist, obarray, or hash-table. */
f927c5ae
JB
1290 /* Exit the loop if the elements are all used up. */
1291 /* elt gets the alist element or symbol.
1292 eltstring gets the name to check as a completion. */
1293
f6d4c7a4 1294 if (type == list_table)
f927c5ae 1295 {
695deb18 1296 if (!CONSP (tail))
f927c5ae 1297 break;
695deb18
SM
1298 elt = XCAR (tail);
1299 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1300 tail = XCDR (tail);
f927c5ae 1301 }
f6d4c7a4 1302 else if (type == obarray_table)
f927c5ae 1303 {
c8ae863b 1304 if (!EQ (bucket, zero))
f927c5ae 1305 {
9b6b374a
JB
1306 if (!SYMBOLP (bucket))
1307 error ("Bad data in guts of obarray");
f927c5ae 1308 elt = bucket;
3809648a 1309 eltstring = elt;
f927c5ae
JB
1310 if (XSYMBOL (bucket)->next)
1311 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1312 else
5a866662 1313 XSETFASTINT (bucket, 0);
f927c5ae
JB
1314 }
1315 else if (++index >= obsize)
1316 break;
1317 else
1318 {
a284cdbb 1319 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1320 continue;
1321 }
1322 }
f6d4c7a4 1323 else /* if (type == hash_table) */
85cd4372 1324 {
a284cdbb
RS
1325 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1326 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
85cd4372 1327 index++;
a284cdbb 1328 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
85cd4372
SM
1329 break;
1330 else
a284cdbb 1331 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
85cd4372 1332 }
f927c5ae
JB
1333
1334 /* Is this element a possible completion? */
1335
3809648a
KS
1336 if (SYMBOLP (eltstring))
1337 eltstring = Fsymbol_name (eltstring);
1338
7510b296 1339 if (STRINGP (eltstring)
d5db4077 1340 && SCHARS (string) <= SCHARS (eltstring)
c8ae863b 1341 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1342 make_number (SCHARS (string)),
c8ae863b 1343 string, zero, Qnil,
6b61353c 1344 completion_ignore_case ? Qt : Qnil),
69f4ef20 1345 EQ (Qt, tem)))
f927c5ae
JB
1346 {
1347 /* Yes. */
42006772 1348 Lisp_Object regexps;
42006772
RS
1349
1350 /* Ignore this element if it fails to match all the regexps. */
6b61353c 1351 {
6b61353c
KH
1352 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1353 regexps = XCDR (regexps))
1354 {
acd81db9
DK
1355 if (bindcount < 0) {
1356 bindcount = SPECPDL_INDEX ();
1357 specbind (Qcase_fold_search,
1358 completion_ignore_case ? Qt : Qnil);
77443789 1359 }
6b61353c
KH
1360 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1361 if (NILP (tem))
1362 break;
1363 }
6b61353c
KH
1364 if (CONSP (regexps))
1365 continue;
1366 }
42006772 1367
f927c5ae
JB
1368 /* Ignore this element if there is a predicate
1369 and the predicate doesn't like it. */
1370
7efd36fc 1371 if (!NILP (predicate))
f927c5ae 1372 {
7efd36fc 1373 if (EQ (predicate, Qcommandp))
c837f4f9 1374 tem = Fcommandp (elt, Qnil);
f927c5ae
JB
1375 else
1376 {
f6d4c7a4
RS
1377 if (bindcount >= 0)
1378 {
1379 unbind_to (bindcount, Qnil);
1380 bindcount = -1;
1381 }
f927c5ae 1382 GCPRO4 (tail, string, eltstring, bestmatch);
f6d4c7a4
RS
1383 tem = (type == hash_table
1384 ? call2 (predicate, elt,
1385 HASH_VALUE (XHASH_TABLE (collection),
1386 index - 1))
1387 : call1 (predicate, elt));
f927c5ae
JB
1388 UNGCPRO;
1389 }
56a98455 1390 if (NILP (tem)) continue;
f927c5ae
JB
1391 }
1392
1393 /* Update computation of how much all possible completions match */
1394
56a98455 1395 if (NILP (bestmatch))
af2b7cd5 1396 {
85cd4372 1397 matchcount = 1;
af2b7cd5 1398 bestmatch = eltstring;
d5db4077 1399 bestmatchsize = SCHARS (eltstring);
af2b7cd5 1400 }
f927c5ae
JB
1401 else
1402 {
d5db4077 1403 compare = min (bestmatchsize, SCHARS (eltstring));
c8ae863b 1404 tem = Fcompare_strings (bestmatch, zero,
69f4ef20 1405 make_number (compare),
c8ae863b 1406 eltstring, zero,
69f4ef20
RS
1407 make_number (compare),
1408 completion_ignore_case ? Qt : Qnil);
1409 if (EQ (tem, Qt))
1410 matchsize = compare;
1411 else if (XINT (tem) < 0)
1412 matchsize = - XINT (tem) - 1;
1413 else
1414 matchsize = XINT (tem) - 1;
1415
52b14ac0
JB
1416 if (completion_ignore_case)
1417 {
1418 /* If this is an exact match except for case,
1419 use it as the best match rather than one that is not an
1420 exact match. This way, we get the case pattern
1421 of the actual match. */
d5db4077
KR
1422 if ((matchsize == SCHARS (eltstring)
1423 && matchsize < SCHARS (bestmatch))
52b14ac0
JB
1424 ||
1425 /* If there is more than one exact match ignoring case,
1426 and one of them is exact including case,
1427 prefer that one. */
1428 /* If there is no exact match ignoring case,
1429 prefer a match that does not change the case
1430 of the input. */
d5db4077 1431 ((matchsize == SCHARS (eltstring))
52b14ac0 1432 ==
d5db4077 1433 (matchsize == SCHARS (bestmatch))
c8ae863b 1434 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1435 make_number (SCHARS (string)),
c8ae863b 1436 string, zero,
69f4ef20
RS
1437 Qnil,
1438 Qnil),
1439 EQ (Qt, tem))
c8ae863b 1440 && (tem = Fcompare_strings (bestmatch, zero,
d5db4077 1441 make_number (SCHARS (string)),
c8ae863b 1442 string, zero,
69f4ef20
RS
1443 Qnil,
1444 Qnil),
1445 ! EQ (Qt, tem))))
52b14ac0
JB
1446 bestmatch = eltstring;
1447 }
d5db4077 1448 if (bestmatchsize != SCHARS (eltstring)
85cd4372
SM
1449 || bestmatchsize != matchsize)
1450 /* Don't count the same string multiple times. */
1451 matchcount++;
52b14ac0 1452 bestmatchsize = matchsize;
d5db4077 1453 if (matchsize <= SCHARS (string)
78893e05
CY
1454 /* If completion-ignore-case is non-nil, don't
1455 short-circuit because we want to find the best
1456 possible match *including* case differences. */
1457 && !completion_ignore_case
695deb18
SM
1458 && matchcount > 1)
1459 /* No need to look any further. */
1460 break;
f927c5ae
JB
1461 }
1462 }
1463 }
1464
acd81db9
DK
1465 if (bindcount >= 0) {
1466 unbind_to (bindcount, Qnil);
1467 bindcount = -1;
1468 }
1469
56a98455 1470 if (NILP (bestmatch))
f927c5ae 1471 return Qnil; /* No completions found */
52b14ac0
JB
1472 /* If we are ignoring case, and there is no exact match,
1473 and no additional text was supplied,
1474 don't change the case of what the user typed. */
d5db4077
KR
1475 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1476 && SCHARS (bestmatch) > bestmatchsize)
ec067ec7 1477 return minibuf_conform_representation (string, bestmatch);
52b14ac0
JB
1478
1479 /* Return t if the supplied string is an exact match (counting case);
1480 it does not require any change to be made. */
928b5acc 1481 if (matchcount == 1 && !NILP (Fequal (bestmatch, string)))
f927c5ae
JB
1482 return Qt;
1483
5a866662
KH
1484 XSETFASTINT (zero, 0); /* Else extract the part in which */
1485 XSETFASTINT (end, bestmatchsize); /* all completions agree */
f927c5ae
JB
1486 return Fsubstring (bestmatch, zero, end);
1487}
f927c5ae 1488\f
89a255dc 1489DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
a284cdbb
RS
1490 doc: /* Search for partial matches to STRING in COLLECTION.
1491Test each of the possible completions specified by COLLECTION
1492to see if it begins with STRING. The possible completions may be
3809648a
KS
1493strings or symbols. Symbols are converted to strings before testing,
1494see `symbol-name'.
a284cdbb 1495The value is a list of all the possible completions that match STRING.
a1f17501 1496
a284cdbb
RS
1497If COLLECTION is an alist, the keys (cars of elements) are the
1498possible completions. If an element is not a cons cell, then the
1499element itself is the possible completion.
1500If COLLECTION is a hash-table, all the keys that are strings or symbols
1501are the possible completions.
1502If COLLECTION is an obarray, the names of all symbols in the obarray
1503are the possible completions.
a1f17501 1504
a284cdbb 1505COLLECTION can also be a function to do the completion itself.
a1f17501
PJ
1506It receives three arguments: the values STRING, PREDICATE and t.
1507Whatever it returns becomes the value of `all-completions'.
1508
1509If optional third argument PREDICATE is non-nil,
1510it is used to test each possible match.
1511The match is a candidate only if PREDICATE returns non-nil.
1512The argument given to PREDICATE is the alist element
a284cdbb 1513or the symbol from the obarray. If COLLECTION is a hash-table,
85cd4372 1514predicate is called with two arguments: the key and the value.
a1f17501
PJ
1515Additionally to this predicate, `completion-regexp-list'
1516is used to further constrain the set of candidates.
1517
782a943e 1518An obsolete optional fourth argument HIDE-SPACES is still accepted for
057bce6f 1519backward compatibility. If non-nil, strings in COLLECTION that start
4fcc3d32 1520with a space are ignored unless STRING itself starts with a space. */)
5842a27b 1521 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate, Lisp_Object hide_spaces)
f927c5ae
JB
1522{
1523 Lisp_Object tail, elt, eltstring;
1524 Lisp_Object allmatches;
a284cdbb
RS
1525 int type = HASH_TABLE_P (collection) ? 3
1526 : VECTORP (collection) ? 2
1527 : NILP (collection) || (CONSP (collection)
1528 && (!SYMBOLP (XCAR (collection))
1529 || NILP (XCAR (collection))));
6bbd7a29 1530 int index = 0, obsize = 0;
acd81db9 1531 int bindcount = -1;
c8ae863b 1532 Lisp_Object bucket, tem, zero;
f927c5ae
JB
1533 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1534
b7826503 1535 CHECK_STRING (string);
85cd4372 1536 if (type == 0)
a284cdbb 1537 return call3 (collection, string, predicate, Qt);
6bbd7a29 1538 allmatches = bucket = Qnil;
c8ae863b 1539 zero = make_number (0);
f927c5ae 1540
a284cdbb
RS
1541 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1542 tail = collection;
85cd4372 1543 if (type == 2)
f927c5ae 1544 {
373bf2b7 1545 collection = check_obarray (collection);
a284cdbb 1546 obsize = XVECTOR (collection)->size;
657d4c0b 1547 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1548 }
1549
1550 while (1)
1551 {
85cd4372 1552 /* Get the next element of the alist, obarray, or hash-table. */
f927c5ae
JB
1553 /* Exit the loop if the elements are all used up. */
1554 /* elt gets the alist element or symbol.
1555 eltstring gets the name to check as a completion. */
1556
85cd4372 1557 if (type == 1)
f927c5ae 1558 {
695deb18 1559 if (!CONSP (tail))
f927c5ae 1560 break;
695deb18
SM
1561 elt = XCAR (tail);
1562 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1563 tail = XCDR (tail);
f927c5ae 1564 }
85cd4372 1565 else if (type == 2)
f927c5ae 1566 {
373bf2b7 1567 if (!EQ (bucket, zero))
f927c5ae 1568 {
373bf2b7
AS
1569 if (!SYMBOLP (bucket))
1570 error ("Bad data in guts of obarray");
f927c5ae 1571 elt = bucket;
3809648a 1572 eltstring = elt;
f927c5ae
JB
1573 if (XSYMBOL (bucket)->next)
1574 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1575 else
5a866662 1576 XSETFASTINT (bucket, 0);
f927c5ae
JB
1577 }
1578 else if (++index >= obsize)
1579 break;
1580 else
1581 {
a284cdbb 1582 bucket = XVECTOR (collection)->contents[index];
f927c5ae
JB
1583 continue;
1584 }
1585 }
85cd4372
SM
1586 else /* if (type == 3) */
1587 {
a284cdbb
RS
1588 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1589 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
85cd4372 1590 index++;
a284cdbb 1591 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
85cd4372
SM
1592 break;
1593 else
a284cdbb 1594 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
85cd4372 1595 }
f927c5ae
JB
1596
1597 /* Is this element a possible completion? */
1598
3809648a
KS
1599 if (SYMBOLP (eltstring))
1600 eltstring = Fsymbol_name (eltstring);
1601
7510b296 1602 if (STRINGP (eltstring)
d5db4077 1603 && SCHARS (string) <= SCHARS (eltstring)
89a255dc 1604 /* If HIDE_SPACES, reject alternatives that start with space
2cbaf886 1605 unless the input starts with space. */
11e3c684
CY
1606 && (NILP (hide_spaces)
1607 || (SBYTES (string) > 0
1608 && SREF (string, 0) == ' ')
1609 || SREF (eltstring, 0) != ' ')
c8ae863b 1610 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1611 make_number (SCHARS (string)),
c8ae863b 1612 string, zero,
d5db4077 1613 make_number (SCHARS (string)),
69f4ef20
RS
1614 completion_ignore_case ? Qt : Qnil),
1615 EQ (Qt, tem)))
f927c5ae
JB
1616 {
1617 /* Yes. */
42006772
RS
1618 Lisp_Object regexps;
1619 Lisp_Object zero;
5a866662 1620 XSETFASTINT (zero, 0);
42006772
RS
1621
1622 /* Ignore this element if it fails to match all the regexps. */
6b61353c 1623 {
6b61353c
KH
1624 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1625 regexps = XCDR (regexps))
1626 {
acd81db9
DK
1627 if (bindcount < 0) {
1628 bindcount = SPECPDL_INDEX ();
1629 specbind (Qcase_fold_search,
1630 completion_ignore_case ? Qt : Qnil);
77443789 1631 }
6b61353c
KH
1632 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1633 if (NILP (tem))
1634 break;
1635 }
6b61353c
KH
1636 if (CONSP (regexps))
1637 continue;
1638 }
42006772 1639
f927c5ae
JB
1640 /* Ignore this element if there is a predicate
1641 and the predicate doesn't like it. */
1642
7efd36fc 1643 if (!NILP (predicate))
f927c5ae 1644 {
7efd36fc 1645 if (EQ (predicate, Qcommandp))
c837f4f9 1646 tem = Fcommandp (elt, Qnil);
f927c5ae
JB
1647 else
1648 {
acd81db9
DK
1649 if (bindcount >= 0) {
1650 unbind_to (bindcount, Qnil);
1651 bindcount = -1;
1652 }
f927c5ae 1653 GCPRO4 (tail, eltstring, allmatches, string);
85cd4372
SM
1654 tem = type == 3
1655 ? call2 (predicate, elt,
a284cdbb 1656 HASH_VALUE (XHASH_TABLE (collection), index - 1))
85cd4372 1657 : call1 (predicate, elt);
f927c5ae
JB
1658 UNGCPRO;
1659 }
56a98455 1660 if (NILP (tem)) continue;
f927c5ae
JB
1661 }
1662 /* Ok => put it on the list. */
1663 allmatches = Fcons (eltstring, allmatches);
1664 }
1665 }
1666
acd81db9
DK
1667 if (bindcount >= 0) {
1668 unbind_to (bindcount, Qnil);
1669 bindcount = -1;
1670 }
1671
f927c5ae
JB
1672 return Fnreverse (allmatches);
1673}
1674\f
cee54539 1675DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
af52c32d 1676 doc: /* Read a string in the minibuffer, with completion.
a1f17501 1677PROMPT is a string to prompt with; normally it ends in a colon and a space.
a284cdbb
RS
1678COLLECTION can be a list of strings, an alist, an obarray or a hash table.
1679COLLECTION can also be a function to do the completion itself.
1680PREDICATE limits completion to a subset of COLLECTION.
a1f17501 1681See `try-completion' and `all-completions' for more details
a284cdbb 1682 on completion, COLLECTION, and PREDICATE.
a1f17501 1683
4d02fc25
SM
1684REQUIRE-MATCH can take the following values:
1685- t means that the user is not allowed to exit unless
1686 the input is (or completes to) an element of COLLECTION or is null.
1687- nil means that the user can exit with any input.
b47d0e56
CY
1688- `confirm' means that the user can exit with any input, but she needs
1689 to confirm her choice if the input is not an element of COLLECTION.
1690- `confirm-after-completion' means that the user can exit with any
1691 input, but she needs to confirm her choice if she called
1692 `minibuffer-complete' right before `minibuffer-complete-and-exit'
1693 and the input is not an element of COLLECTION.
4d02fc25
SM
1694- anything else behaves like t except that typing RET does not exit if it
1695 does non-null completion.
1696
2e3148e2
JL
1697If the input is null, `completing-read' returns DEF, or the first element
1698of the list of default values, or an empty string if DEF is nil,
1699regardless of the value of REQUIRE-MATCH.
6b61353c
KH
1700
1701If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
1702 with point positioned at the end.
1703 If it is (STRING . POSITION), the initial input is STRING, but point
1704 is placed at _zero-indexed_ position POSITION in STRING. (*Note*
1705 that this is different from `read-from-minibuffer' and related
1706 functions, which use one-indexing for POSITION.) This feature is
1707 deprecated--it is best to pass nil for INITIAL-INPUT and supply the
1708 default value DEF instead. The user can yank the default value into
1709 the minibuffer easily using \\[next-history-element].
1710
1711HIST, if non-nil, specifies a history list and optionally the initial
1712 position in the list. It can be a symbol, which is the history list
1713 variable to use, or it can be a cons cell (HISTVAR . HISTPOS). In
1714 that case, HISTVAR is the history list variable to use, and HISTPOS
1715 is the initial position (the position in the list used by the
1716 minibuffer history commands). For consistency, you should also
1717 specify that element of the history as the value of
1fb101f3 1718 INITIAL-INPUT. (This is the only case in which you should use
6b61353c
KH
1719 INITIAL-INPUT instead of DEF.) Positions are counted starting from
1720 1 at the beginning of the list. The variable `history-length'
1721 controls the maximum length of a history list.
38668f81 1722
2e3148e2 1723DEF, if non-nil, is the default value or the list of default values.
a1f17501
PJ
1724
1725If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
029a305c 1726 the current input method and the setting of `enable-multibyte-characters'.
a1f17501
PJ
1727
1728Completion ignores case if the ambient value of
af52c32d 1729 `completion-ignore-case' is non-nil. */)
5842a27b 1730 (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 1731{
770970cb 1732 Lisp_Object val, histvar, histpos, position;
d43f85c2 1733 Lisp_Object init;
770970cb 1734 int pos = 0;
aed13378 1735 int count = SPECPDL_INDEX ();
0c8ee1a2
RS
1736 struct gcpro gcpro1;
1737
d43f85c2 1738 init = initial_input;
0c8ee1a2
RS
1739 GCPRO1 (def);
1740
a284cdbb 1741 specbind (Qminibuffer_completion_table, collection);
7efd36fc 1742 specbind (Qminibuffer_completion_predicate, predicate);
f927c5ae 1743 specbind (Qminibuffer_completion_confirm,
695deb18 1744 EQ (require_match, Qt) ? Qnil : require_match);
770970cb
RS
1745
1746 position = Qnil;
1747 if (!NILP (init))
1748 {
7510b296 1749 if (CONSP (init))
770970cb
RS
1750 {
1751 position = Fcdr (init);
1752 init = Fcar (init);
1753 }
b7826503 1754 CHECK_STRING (init);
770970cb
RS
1755 if (!NILP (position))
1756 {
b7826503 1757 CHECK_NUMBER (position);
770970cb 1758 /* Convert to distance from end of input. */
d5db4077 1759 pos = XINT (position) - SCHARS (init);
770970cb
RS
1760 }
1761 }
1762
7510b296 1763 if (SYMBOLP (hist))
770970cb
RS
1764 {
1765 histvar = hist;
1766 histpos = Qnil;
1767 }
1768 else
1769 {
1770 histvar = Fcar_safe (hist);
1771 histpos = Fcdr_safe (hist);
1772 }
1773 if (NILP (histvar))
1774 histvar = Qminibuffer_history;
1775 if (NILP (histpos))
5a866662 1776 XSETFASTINT (histpos, 0);
770970cb 1777
56a98455 1778 val = read_minibuf (NILP (require_match)
bb0bdf70 1779 ? (NILP (Vminibuffer_completing_file_name)
ef72eaff 1780 || EQ (Vminibuffer_completing_file_name, Qlambda)
bb0bdf70
EZ
1781 ? Vminibuffer_local_completion_map
1782 : Vminibuffer_local_filename_completion_map)
1783 : (NILP (Vminibuffer_completing_file_name)
ef72eaff 1784 || EQ (Vminibuffer_completing_file_name, Qlambda)
bb0bdf70 1785 ? Vminibuffer_local_must_match_map
8ba31f36 1786 : Vminibuffer_local_filename_must_match_map),
85b5fe07 1787 init, prompt, make_number (pos), 0,
cee54539 1788 histvar, histpos, def, 0,
b35cd215 1789 !NILP (inherit_input_method));
b9a86585 1790
d5db4077 1791 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
fb30dfd2 1792 val = CONSP (def) ? XCAR (def) : def;
b9a86585 1793
0c8ee1a2 1794 RETURN_UNGCPRO (unbind_to (count, val));
f927c5ae
JB
1795}
1796\f
971de7fb 1797Lisp_Object Fassoc_string (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold);
5d2ca7ae
RS
1798
1799/* Test whether TXT is an exact completion. */
695deb18
SM
1800DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1801 doc: /* Return non-nil if STRING is a valid completion.
1802Takes the same arguments as `all-completions' and `try-completion'.
a284cdbb 1803If COLLECTION is a function, it is called with three arguments:
695deb18 1804the values STRING, PREDICATE and `lambda'. */)
5842a27b 1805 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
5d2ca7ae 1806{
6b61353c 1807 Lisp_Object regexps, tail, tem = Qnil;
85cd4372 1808 int i = 0;
5d2ca7ae 1809
695deb18
SM
1810 CHECK_STRING (string);
1811
a284cdbb
RS
1812 if ((CONSP (collection)
1813 && (!SYMBOLP (XCAR (collection)) || NILP (XCAR (collection))))
1814 || NILP (collection))
695deb18 1815 {
a284cdbb 1816 tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
9f4a578d 1817 if (NILP (tem))
695deb18
SM
1818 return Qnil;
1819 }
a284cdbb 1820 else if (VECTORP (collection))
5d2ca7ae 1821 {
695deb18 1822 /* Bypass intern-soft as that loses for nil. */
a284cdbb 1823 tem = oblookup (collection,
d5db4077
KR
1824 SDATA (string),
1825 SCHARS (string),
1826 SBYTES (string));
44472c88 1827 if (!SYMBOLP (tem))
9404446f 1828 {
695deb18
SM
1829 if (STRING_MULTIBYTE (string))
1830 string = Fstring_make_unibyte (string);
9404446f 1831 else
695deb18 1832 string = Fstring_make_multibyte (string);
9404446f 1833
a284cdbb 1834 tem = oblookup (collection,
d5db4077
KR
1835 SDATA (string),
1836 SCHARS (string),
1837 SBYTES (string));
9404446f 1838 }
6b61353c
KH
1839
1840 if (completion_ignore_case && !SYMBOLP (tem))
1841 {
a284cdbb 1842 for (i = XVECTOR (collection)->size - 1; i >= 0; i--)
6b61353c 1843 {
a284cdbb 1844 tail = XVECTOR (collection)->contents[i];
6b61353c
KH
1845 if (SYMBOLP (tail))
1846 while (1)
1847 {
1848 if (EQ((Fcompare_strings (string, make_number (0), Qnil,
1849 Fsymbol_name (tail),
1850 make_number (0) , Qnil, Qt)),
1851 Qt))
1852 {
1853 tem = tail;
1854 break;
1855 }
1856 if (XSYMBOL (tail)->next == 0)
1857 break;
1858 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1859 }
1860 }
1861 }
1862
1863 if (!SYMBOLP (tem))
1864 return Qnil;
5d2ca7ae 1865 }
a284cdbb 1866 else if (HASH_TABLE_P (collection))
85cd4372 1867 {
a284cdbb 1868 struct Lisp_Hash_Table *h = XHASH_TABLE (collection);
6b61353c 1869 i = hash_lookup (h, string, NULL);
85cd4372 1870 if (i >= 0)
6b61353c 1871 tem = HASH_KEY (h, i);
85cd4372 1872 else
6b61353c
KH
1873 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1874 if (!NILP (HASH_HASH (h, i)) &&
1875 EQ (Fcompare_strings (string, make_number (0), Qnil,
1876 HASH_KEY (h, i), make_number (0) , Qnil,
1877 completion_ignore_case ? Qt : Qnil),
1878 Qt))
1879 {
1880 tem = HASH_KEY (h, i);
1881 break;
1882 }
1883 if (!STRINGP (tem))
85cd4372
SM
1884 return Qnil;
1885 }
5d2ca7ae 1886 else
a284cdbb 1887 return call3 (collection, string, predicate, Qlambda);
695deb18
SM
1888
1889 /* Reject this element if it fails to match all the regexps. */
77443789
AS
1890 if (CONSP (Vcompletion_regexp_list))
1891 {
1892 int count = SPECPDL_INDEX ();
1893 specbind (Qcase_fold_search, completion_ignore_case ? Qt : Qnil);
1894 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1895 regexps = XCDR (regexps))
1896 {
1897 if (NILP (Fstring_match (XCAR (regexps),
1898 SYMBOLP (tem) ? string : tem,
1899 Qnil)))
1900 return unbind_to (count, Qnil);
1901 }
1902 unbind_to (count, Qnil);
1903 }
695deb18
SM
1904
1905 /* Finally, check the predicate. */
1906 if (!NILP (predicate))
6b61353c 1907 {
a284cdbb
RS
1908 return HASH_TABLE_P (collection)
1909 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (collection), i))
6b61353c
KH
1910 : call1 (predicate, tem);
1911 }
695deb18
SM
1912 else
1913 return Qt;
5d2ca7ae 1914}
f927c5ae 1915
655ea21c
JD
1916DEFUN ("internal-complete-buffer", Finternal_complete_buffer, Sinternal_complete_buffer, 3, 3, 0,
1917 doc: /* Perform completion on buffer names.
1918If the argument FLAG is nil, invoke `try-completion', if it's t, invoke
1919`all-completions', otherwise invoke `test-completion'.
1920
ba5524f4 1921The arguments STRING and PREDICATE are as in `try-completion',
655ea21c 1922`all-completions', and `test-completion'. */)
5842a27b 1923 (Lisp_Object string, Lisp_Object predicate, Lisp_Object flag)
655ea21c
JD
1924{
1925 if (NILP (flag))
1926 return Ftry_completion (string, Vbuffer_alist, predicate);
1927 else if (EQ (flag, Qt))
8c2dccbf 1928 {
73913e2e 1929 Lisp_Object res = Fall_completions (string, Vbuffer_alist, predicate, Qnil);
8c2dccbf
SM
1930 if (SCHARS (string) > 0)
1931 return res;
1932 else
1933 { /* Strip out internal buffers. */
1934 Lisp_Object bufs = res;
1935 /* First, look for a non-internal buffer in `res'. */
1936 while (CONSP (bufs) && SREF (XCAR (bufs), 0) == ' ')
1937 bufs = XCDR (bufs);
1938 if (NILP (bufs))
1939 /* All bufs in `res' are internal, so don't trip them out. */
1940 return res;
1941 res = bufs;
1942 while (CONSP (XCDR (bufs)))
1943 if (SREF (XCAR (XCDR (bufs)), 0) == ' ')
1944 XSETCDR (bufs, XCDR (XCDR (bufs)));
1945 else
1946 bufs = XCDR (bufs);
1947 return res;
1948 }
1949 }
655ea21c
JD
1950 else /* assume `lambda' */
1951 return Ftest_completion (string, Vbuffer_alist, predicate);
1952}
1953
52b14ac0
JB
1954/* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1955
695deb18 1956DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
d3fc66fb 1957 doc: /* Like `assoc' but specifically for strings (and symbols).
c6a82c18
CY
1958
1959This returns the first element of LIST whose car matches the string or
1960symbol KEY, or nil if no match exists. When performing the
1961comparison, symbols are first converted to strings, and unibyte
1962strings to multibyte. If the optional arg CASE-FOLD is non-nil, case
1963is ignored.
1964
1965Unlike `assoc', KEY can also match an entry in LIST consisting of a
1966single string, rather than a cons cell whose car is a string. */)
5842a27b 1967 (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold)
52b14ac0
JB
1968{
1969 register Lisp_Object tail;
1970
d3fc66fb
KS
1971 if (SYMBOLP (key))
1972 key = Fsymbol_name (key);
1973
99784d63 1974 for (tail = list; CONSP (tail); tail = XCDR (tail))
52b14ac0
JB
1975 {
1976 register Lisp_Object elt, tem, thiscar;
99784d63 1977 elt = XCAR (tail);
695deb18 1978 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1954495f
KS
1979 if (SYMBOLP (thiscar))
1980 thiscar = Fsymbol_name (thiscar);
1981 else if (!STRINGP (thiscar))
52b14ac0 1982 continue;
9404446f
RS
1983 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1984 key, make_number (0), Qnil,
695deb18 1985 case_fold);
9404446f
RS
1986 if (EQ (tem, Qt))
1987 return elt;
52b14ac0
JB
1988 QUIT;
1989 }
1990 return Qnil;
1991}
f927c5ae 1992
e4c97a67 1993\f
f927c5ae 1994DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
af52c32d 1995 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
5842a27b 1996 (void)
f927c5ae
JB
1997{
1998 return make_number (minibuf_level);
1999}
2000
37e9a934 2001DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
af52c32d
MB
2002 doc: /* Return the prompt string of the currently-active minibuffer.
2003If no minibuffer is active, return nil. */)
5842a27b 2004 (void)
37e9a934 2005{
4d04c1f1 2006 return Fcopy_sequence (minibuf_prompt);
37e9a934
KH
2007}
2008
f927c5ae 2009\f
9d815fd9 2010void
971de7fb 2011init_minibuf_once (void)
f927c5ae
JB
2012{
2013 Vminibuffer_list = Qnil;
2014 staticpro (&Vminibuffer_list);
2015}
2016
9d815fd9 2017void
971de7fb 2018syms_of_minibuf (void)
f927c5ae
JB
2019{
2020 minibuf_level = 0;
4d04c1f1
KH
2021 minibuf_prompt = Qnil;
2022 staticpro (&minibuf_prompt);
2023
2024 minibuf_save_list = Qnil;
2025 staticpro (&minibuf_save_list);
f927c5ae 2026
d67b4f80 2027 Qcompletion_ignore_case = intern_c_string ("completion-ignore-case");
9fa5c213
GM
2028 staticpro (&Qcompletion_ignore_case);
2029
d67b4f80 2030 Qread_file_name_internal = intern_c_string ("read-file-name-internal");
719b4a40
RS
2031 staticpro (&Qread_file_name_internal);
2032
d67b4f80 2033 Qminibuffer_default = intern_c_string ("minibuffer-default");
e5d4686b
RS
2034 staticpro (&Qminibuffer_default);
2035 Fset (Qminibuffer_default, Qnil);
2036
d67b4f80 2037 Qminibuffer_completion_table = intern_c_string ("minibuffer-completion-table");
f927c5ae
JB
2038 staticpro (&Qminibuffer_completion_table);
2039
d67b4f80 2040 Qminibuffer_completion_confirm = intern_c_string ("minibuffer-completion-confirm");
f927c5ae
JB
2041 staticpro (&Qminibuffer_completion_confirm);
2042
d67b4f80 2043 Qminibuffer_completion_predicate = intern_c_string ("minibuffer-completion-predicate");
f927c5ae
JB
2044 staticpro (&Qminibuffer_completion_predicate);
2045
2046 staticpro (&last_minibuf_string);
2047 last_minibuf_string = Qnil;
2048
d67b4f80 2049 Quser_variable_p = intern_c_string ("user-variable-p");
f927c5ae
JB
2050 staticpro (&Quser_variable_p);
2051
d67b4f80 2052 Qminibuffer_history = intern_c_string ("minibuffer-history");
770970cb 2053 staticpro (&Qminibuffer_history);
f927c5ae 2054
d67b4f80 2055 Qbuffer_name_history = intern_c_string ("buffer-name-history");
406e55df 2056 staticpro (&Qbuffer_name_history);
33d0a17f 2057 Fset (Qbuffer_name_history, Qnil);
406e55df 2058
d67b4f80 2059 Qminibuffer_setup_hook = intern_c_string ("minibuffer-setup-hook");
5c781212
RS
2060 staticpro (&Qminibuffer_setup_hook);
2061
d67b4f80 2062 Qminibuffer_exit_hook = intern_c_string ("minibuffer-exit-hook");
177aecf9
KH
2063 staticpro (&Qminibuffer_exit_hook);
2064
d67b4f80 2065 Qhistory_length = intern_c_string ("history-length");
77aa8edf
RS
2066 staticpro (&Qhistory_length);
2067
d67b4f80 2068 Qcurrent_input_method = intern_c_string ("current-input-method");
cee54539
KH
2069 staticpro (&Qcurrent_input_method);
2070
d67b4f80 2071 Qactivate_input_method = intern_c_string ("activate-input-method");
cee54539
KH
2072 staticpro (&Qactivate_input_method);
2073
d67b4f80 2074 Qcase_fold_search = intern_c_string ("case-fold-search");
6b61353c
KH
2075 staticpro (&Qcase_fold_search);
2076
d67b4f80 2077 Qread_expression_history = intern_c_string ("read-expression-history");
627fb581
RS
2078 staticpro (&Qread_expression_history);
2079
af52c32d 2080 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
b4f588fa
JB
2081 doc: /* If this is non-nil, `read-buffer' does its work by calling this function.
2082The function is called with the arguments passed to `read-buffer'. */);
eebbfb01
KH
2083 Vread_buffer_function = Qnil;
2084
33df281c
CY
2085 DEFVAR_BOOL ("read-buffer-completion-ignore-case",
2086 &read_buffer_completion_ignore_case,
2087 doc: /* *Non-nil means completion ignores case when reading a buffer name. */);
2088 read_buffer_completion_ignore_case = 0;
2089
af52c32d
MB
2090 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2091 doc: /* Normal hook run just after entry to minibuffer. */);
5c781212
RS
2092 Vminibuffer_setup_hook = Qnil;
2093
af52c32d
MB
2094 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2095 doc: /* Normal hook run just after exit from minibuffer. */);
177aecf9
KH
2096 Vminibuffer_exit_hook = Qnil;
2097
af52c32d
MB
2098 DEFVAR_LISP ("history-length", &Vhistory_length,
2099 doc: /* *Maximum length for history lists before truncation takes place.
a1f17501 2100A number means that length; t means infinite. Truncation takes place
f4b46bb9 2101just after a new element is inserted. Setting the `history-length'
a1f17501 2102property of a history variable overrides this default. */);
77aa8edf
RS
2103 XSETFASTINT (Vhistory_length, 30);
2104
0da4d471
JL
2105 DEFVAR_BOOL ("history-delete-duplicates", &history_delete_duplicates,
2106 doc: /* *Non-nil means to delete duplicates in history.
2107If set to t when adding a new history element, all previous identical
179a4ca7 2108elements are deleted from the history list. */);
0da4d471
JL
2109 history_delete_duplicates = 0;
2110
179a4ca7
JL
2111 DEFVAR_LISP ("history-add-new-input", &Vhistory_add_new_input,
2112 doc: /* *Non-nil means to add new elements in history.
2113If set to nil, minibuffer reading functions don't add new elements to the
2114history list, so it is possible to do this afterwards by calling
2115`add-to-history' explicitly. */);
2116 Vhistory_add_new_input = Qt;
2117
af52c32d 2118 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
75028673 2119 doc: /* Non-nil means don't consider case significant in completion.
f562df03
CY
2120For file-name completion, `read-file-name-completion-ignore-case'
2121controls the behavior, rather than this variable.
2122For buffer name completion, `read-buffer-completion-ignore-case'
d9a03ad2 2123controls the behavior, rather than this variable. */);
f927c5ae
JB
2124 completion_ignore_case = 0;
2125
af52c32d
MB
2126 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2127 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
a1f17501 2128This variable makes a difference whenever the minibuffer window is active. */);
f927c5ae
JB
2129 enable_recursive_minibuffers = 0;
2130
af52c32d
MB
2131 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2132 doc: /* Alist or obarray used for completion in the minibuffer.
6b61353c
KH
2133This becomes the ALIST argument to `try-completion' and `all-completions'.
2134The value can also be a list of strings or a hash table.
a1f17501
PJ
2135
2136The value may alternatively be a function, which is given three arguments:
2137 STRING, the current buffer contents;
2138 PREDICATE, the predicate for filtering possible matches;
2139 CODE, which says what kind of things to do.
b2ef9714
JB
2140CODE can be nil, t or `lambda':
2141 nil -- return the best completion of STRING, or nil if there is none.
2142 t -- return a list of all possible completions of STRING.
2143 lambda -- return t if STRING is a valid completion as it stands. */);
f927c5ae
JB
2144 Vminibuffer_completion_table = Qnil;
2145
af52c32d
MB
2146 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2147 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
f927c5ae
JB
2148 Vminibuffer_completion_predicate = Qnil;
2149
af52c32d 2150 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
b47d0e56
CY
2151 doc: /* Whether to demand confirmation of completion before exiting minibuffer.
2152If nil, confirmation is not required.
2153If the value is `confirm', the user may exit with an input that is not
2154 a valid completion alternative, but Emacs asks for confirmation.
2155If the value is `confirm-after-completion', the user may exit with an
2156 input that is not a valid completion alternative, but Emacs asks for
1b79e224
CY
2157 confirmation if the user submitted the input right after any of the
2158 completion commands listed in `minibuffer-confirm-exit-commands'. */);
f927c5ae
JB
2159 Vminibuffer_completion_confirm = Qnil;
2160
ccf330a6 2161 DEFVAR_LISP ("minibuffer-completing-file-name",
af52c32d 2162 &Vminibuffer_completing_file_name,
6ddfc113 2163 doc: /* Non-nil means completing file names. */);
ccf330a6
RS
2164 Vminibuffer_completing_file_name = Qnil;
2165
af52c32d
MB
2166 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2167 doc: /* Value that `help-form' takes on inside the minibuffer. */);
f927c5ae
JB
2168 Vminibuffer_help_form = Qnil;
2169
af52c32d
MB
2170 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2171 doc: /* History list symbol to add minibuffer values to.
a1f17501
PJ
2172Each string of minibuffer input, as it appears on exit from the minibuffer,
2173is added with
2174 (set minibuffer-history-variable
2175 (cons STRING (symbol-value minibuffer-history-variable))) */);
5a866662 2176 XSETFASTINT (Vminibuffer_history_variable, 0);
770970cb 2177
af52c32d
MB
2178 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2179 doc: /* Current position of redoing in the history list. */);
770970cb
RS
2180 Vminibuffer_history_position = Qnil;
2181
af52c32d
MB
2182 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2183 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
a1f17501 2184Some uses of the echo area also raise that frame (since they use it too). */);
6a9ee000
RS
2185 minibuffer_auto_raise = 0;
2186
af52c32d 2187 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
6b61353c
KH
2188 doc: /* List of regexps that should restrict possible completions.
2189The basic completion functions only consider a completion acceptable
2190if it matches all regular expressions in this list, with
2191`case-fold-search' bound to the value of `completion-ignore-case'.
2192See Info node `(elisp)Basic Completion', for a description of these
2193functions. */);
42006772
RS
2194 Vcompletion_regexp_list = Qnil;
2195
c3421833 2196 DEFVAR_BOOL ("minibuffer-allow-text-properties",
af52c32d
MB
2197 &minibuffer_allow_text_properties,
2198 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
a1f17501
PJ
2199This also affects `read-string', but it does not affect `read-minibuffer',
2200`read-no-blanks-input', or any of the functions that do minibuffer input
2201with completion; they always discard text properties. */);
c3421833
RS
2202 minibuffer_allow_text_properties = 0;
2203
af52c32d
MB
2204 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2205 doc: /* Text properties that are added to minibuffer prompts.
a1f17501
PJ
2206These are in addition to the basic `field' property, and stickiness
2207properties. */);
cc64f5c9
MB
2208 /* We use `intern' here instead of Qread_only to avoid
2209 initialization-order problems. */
2210 Vminibuffer_prompt_properties
d67b4f80 2211 = Fcons (intern_c_string ("read-only"), Fcons (Qt, Qnil));
cc64f5c9 2212
627fb581
RS
2213 DEFVAR_LISP ("read-expression-map", &Vread_expression_map,
2214 doc: /* Minibuffer keymap used for reading Lisp expressions. */);
2215 Vread_expression_map = Qnil;
2216
68313ed8 2217 defsubr (&Sset_minibuffer_window);
f927c5ae
JB
2218 defsubr (&Sread_from_minibuffer);
2219 defsubr (&Seval_minibuffer);
2220 defsubr (&Sread_minibuffer);
2221 defsubr (&Sread_string);
2222 defsubr (&Sread_command);
2223 defsubr (&Sread_variable);
ff137f16 2224 defsubr (&Sinternal_complete_buffer);
f927c5ae
JB
2225 defsubr (&Sread_buffer);
2226 defsubr (&Sread_no_blanks_input);
2227 defsubr (&Sminibuffer_depth);
37e9a934 2228 defsubr (&Sminibuffer_prompt);
f927c5ae 2229
873ef78e 2230 defsubr (&Sminibufferp);
a346ec72
MB
2231 defsubr (&Sminibuffer_prompt_end);
2232 defsubr (&Sminibuffer_contents);
2233 defsubr (&Sminibuffer_contents_no_properties);
b5e1e449 2234 defsubr (&Sminibuffer_completion_contents);
a346ec72 2235
f927c5ae
JB
2236 defsubr (&Stry_completion);
2237 defsubr (&Sall_completions);
695deb18
SM
2238 defsubr (&Stest_completion);
2239 defsubr (&Sassoc_string);
f927c5ae 2240 defsubr (&Scompleting_read);
f927c5ae
JB
2241}
2242