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