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