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