(Deletion): Upcase @key argument.
[bpt/emacs.git] / src / minibuf.c
CommitLineData
f927c5ae 1/* Minibuffer input and completion.
0b5538bd
TTN
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
f927c5ae
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
ffd56f97 9the Free Software Foundation; either version 2, or (at your option)
f927c5ae
JB
10any later version.
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
18along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
f927c5ae
JB
21
22
18160b98 23#include <config.h>
3f708f4c 24#include <stdio.h>
af52c32d 25
f927c5ae
JB
26#include "lisp.h"
27#include "commands.h"
28#include "buffer.h"
d50a3d2a 29#include "charset.h"
f927c5ae 30#include "dispextern.h"
2538fae4 31#include "keyboard.h"
ff11dfa1 32#include "frame.h"
f927c5ae
JB
33#include "window.h"
34#include "syntax.h"
0c21eeeb 35#include "intervals.h"
8feddab4 36#include "keymap.h"
f927c5ae 37
a4e71d81
KH
38extern int quit_char;
39
f927c5ae 40/* List of buffers for use as minibuffers.
4d04c1f1
KH
41 The first element of the list is used for the outermost minibuffer
42 invocation, the next element is used for a recursive minibuffer
43 invocation, etc. The list is extended at the end as deeper
44 minibuffer recursions are encountered. */
279cf52c 45
f927c5ae
JB
46Lisp_Object Vminibuffer_list;
47
4d04c1f1 48/* Data to remember during recursive minibuffer invocations */
279cf52c 49
4d04c1f1 50Lisp_Object minibuf_save_list;
f927c5ae
JB
51
52/* Depth in minibuffer invocations. */
279cf52c 53
f927c5ae
JB
54int minibuf_level;
55
77aa8edf 56/* Nonzero means display completion help for invalid input. */
279cf52c 57
739cc391 58Lisp_Object Vcompletion_auto_help;
f927c5ae 59
77aa8edf 60/* The maximum length of a minibuffer history. */
279cf52c 61
77aa8edf
RS
62Lisp_Object Qhistory_length, Vhistory_length;
63
0da4d471
JL
64/* No duplicates in history. */
65
66int history_delete_duplicates;
67
b278606c 68/* Fread_minibuffer leaves the input here as a string. */
279cf52c 69
f927c5ae
JB
70Lisp_Object last_minibuf_string;
71
9ab4a725 72/* Nonzero means let functions called when within a minibuffer
f927c5ae 73 invoke recursive minibuffers (to read arguments, or whatever) */
279cf52c 74
f927c5ae
JB
75int enable_recursive_minibuffers;
76
c3421833
RS
77/* Nonzero means don't ignore text properties
78 in Fread_from_minibuffer. */
279cf52c 79
c3421833
RS
80int minibuffer_allow_text_properties;
81
f927c5ae
JB
82/* help-form is bound to this while in the minibuffer. */
83
84Lisp_Object Vminibuffer_help_form;
85
770970cb
RS
86/* Variable which is the history list to add minibuffer values to. */
87
88Lisp_Object Vminibuffer_history_variable;
89
90/* Current position in the history list (adjusted by M-n and M-p). */
91
92Lisp_Object Vminibuffer_history_position;
93
cc64f5c9
MB
94/* Text properties that are added to minibuffer prompts.
95 These are in addition to the basic `field' property, and stickiness
96 properties. */
97
98Lisp_Object Vminibuffer_prompt_properties;
99
406e55df 100Lisp_Object Qminibuffer_history, Qbuffer_name_history;
770970cb 101
719b4a40
RS
102Lisp_Object Qread_file_name_internal;
103
177aecf9 104/* Normal hooks for entry to and exit from minibuffer. */
5c781212
RS
105
106Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
177aecf9 107Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
5c781212 108
eebbfb01 109/* Function to call to read a buffer name. */
9ab4a725 110Lisp_Object Vread_buffer_function;
eebbfb01 111
f927c5ae
JB
112/* Nonzero means completion ignores case. */
113
114int completion_ignore_case;
115
42006772
RS
116/* List of regexps that should restrict possible completions. */
117
118Lisp_Object Vcompletion_regexp_list;
119
6a9ee000
RS
120/* Nonzero means raise the minibuffer frame when the minibuffer
121 is entered. */
122
123int minibuffer_auto_raise;
124
f927c5ae
JB
125/* If last completion attempt reported "Complete but not unique"
126 then this is the string completed then; otherwise this is nil. */
127
128static Lisp_Object last_exact_completion;
129
30e13e56 130extern Lisp_Object Voverriding_local_map;
cee54539 131
5744fa7c
RS
132Lisp_Object Quser_variable_p;
133
134Lisp_Object Qminibuffer_default;
135
cee54539
KH
136Lisp_Object Qcurrent_input_method, Qactivate_input_method;
137
b0c138ce
LT
138Lisp_Object Qcase_fold_search;
139
5744fa7c 140extern Lisp_Object Qmouse_face;
279cf52c 141
8e9968c6 142extern Lisp_Object Qfield;
f927c5ae 143\f
89fdc4a0
KH
144/* Put minibuf on currently selected frame's minibuffer.
145 We do this whenever the user starts a new minibuffer
146 or when a minibuffer exits. */
147
148void
149choose_minibuf_frame ()
150{
a4aafc54
GM
151 if (FRAMEP (selected_frame)
152 && FRAME_LIVE_P (XFRAME (selected_frame))
153 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
89fdc4a0 154 {
a4aafc54
GM
155 struct frame *sf = XFRAME (selected_frame);
156 Lisp_Object buffer;
9ab4a725 157
89fdc4a0
KH
158 /* I don't think that any frames may validly have a null minibuffer
159 window anymore. */
a4aafc54 160 if (NILP (sf->minibuffer_window))
89fdc4a0
KH
161 abort ();
162
a4aafc54
GM
163 /* Under X, we come here with minibuf_window being the
164 minibuffer window of the unused termcap window created in
165 init_window_once. That window doesn't have a buffer. */
166 buffer = XWINDOW (minibuf_window)->buffer;
167 if (BUFFERP (buffer))
cbbfe056 168 Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
a4aafc54 169 minibuf_window = sf->minibuffer_window;
89fdc4a0 170 }
914860c2
RS
171
172 /* Make sure no other frame has a minibuffer as its selected window,
173 because the text would not be displayed in it, and that would be
07d402c8
RS
174 confusing. Only allow the selected frame to do this,
175 and that only if the minibuffer is active. */
914860c2
RS
176 {
177 Lisp_Object tail, frame;
178
179 FOR_EACH_FRAME (tail, frame)
07d402c8 180 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
a4aafc54 181 && !(EQ (frame, selected_frame)
07d402c8 182 && minibuf_level > 0))
914860c2
RS
183 Fset_frame_selected_window (frame, Fframe_first_window (frame));
184 }
89fdc4a0 185}
68313ed8 186
0c94f256
RS
187Lisp_Object
188choose_minibuf_frame_1 (ignore)
189 Lisp_Object ignore;
190{
191 choose_minibuf_frame ();
192 return Qnil;
193}
194
68313ed8
RS
195DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
196 Sset_minibuffer_window, 1, 1, 0,
af52c32d 197 doc: /* Specify which minibuffer window to use for the minibuffer.
dea7e2ba 198This affects where the minibuffer is displayed if you put text in it
af52c32d
MB
199without invoking the usual minibuffer commands. */)
200 (window)
68313ed8
RS
201 Lisp_Object window;
202{
b7826503 203 CHECK_WINDOW (window);
68313ed8
RS
204 if (! MINI_WINDOW_P (XWINDOW (window)))
205 error ("Window is not a minibuffer window");
206
207 minibuf_window = window;
208
209 return window;
210}
211
89fdc4a0 212\f
f927c5ae
JB
213/* Actual minibuffer invocation. */
214
af2b7cd5 215static Lisp_Object read_minibuf_unwind P_ ((Lisp_Object));
7292839d 216static Lisp_Object run_exit_minibuf_hook P_ ((Lisp_Object));
af2b7cd5
RS
217static Lisp_Object read_minibuf P_ ((Lisp_Object, Lisp_Object,
218 Lisp_Object, Lisp_Object,
219 int, Lisp_Object,
220 Lisp_Object, Lisp_Object,
da34f64e 221 int, int, int));
edfef199
GM
222static Lisp_Object read_minibuf_noninteractive P_ ((Lisp_Object, Lisp_Object,
223 Lisp_Object, Lisp_Object,
224 int, Lisp_Object,
225 Lisp_Object, Lisp_Object,
226 int, int));
227static Lisp_Object string_to_object P_ ((Lisp_Object, Lisp_Object));
228
229
230/* Read a Lisp object from VAL and return it. If VAL is an empty
231 string, and DEFALT is a string, read from DEFALT instead of VAL. */
232
233static Lisp_Object
234string_to_object (val, defalt)
235 Lisp_Object val, defalt;
236{
237 struct gcpro gcpro1, gcpro2;
238 Lisp_Object expr_and_pos;
239 int pos;
9ab4a725 240
edfef199 241 GCPRO2 (val, defalt);
9ab4a725 242
d5db4077 243 if (STRINGP (val) && SCHARS (val) == 0
edfef199
GM
244 && STRINGP (defalt))
245 val = defalt;
9ab4a725 246
edfef199
GM
247 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
248 pos = XINT (Fcdr (expr_and_pos));
d5db4077 249 if (pos != SCHARS (val))
edfef199
GM
250 {
251 /* Ignore trailing whitespace; any other trailing junk
252 is an error. */
253 int i;
254 pos = string_char_to_byte (val, pos);
d5db4077 255 for (i = pos; i < SBYTES (val); i++)
edfef199 256 {
d5db4077 257 int c = SREF (val, i);
edfef199
GM
258 if (c != ' ' && c != '\t' && c != '\n')
259 error ("Trailing garbage following expression");
260 }
261 }
9ab4a725 262
edfef199
GM
263 val = Fcar (expr_and_pos);
264 RETURN_UNGCPRO (val);
265}
266
267
268/* Like read_minibuf but reading from stdin. This function is called
269 from read_minibuf to do the job if noninteractive. */
270
271static Lisp_Object
272read_minibuf_noninteractive (map, initial, prompt, backup_n, expflag,
273 histvar, histpos, defalt, allow_props,
274 inherit_input_method)
275 Lisp_Object map;
276 Lisp_Object initial;
277 Lisp_Object prompt;
278 Lisp_Object backup_n;
279 int expflag;
280 Lisp_Object histvar;
281 Lisp_Object histpos;
282 Lisp_Object defalt;
283 int allow_props;
284 int inherit_input_method;
285{
286 int size, len;
287 char *line, *s;
edfef199
GM
288 Lisp_Object val;
289
d5db4077 290 fprintf (stdout, "%s", SDATA (prompt));
edfef199
GM
291 fflush (stdout);
292
6bbd7a29 293 val = Qnil;
edfef199
GM
294 size = 100;
295 len = 0;
296 line = (char *) xmalloc (size * sizeof *line);
297 while ((s = fgets (line + len, size - len, stdin)) != NULL
298 && (len = strlen (line),
299 len == size - 1 && line[len - 1] != '\n'))
300 {
301 size *= 2;
302 line = (char *) xrealloc (line, size);
303 }
304
305 if (s)
306 {
307 len = strlen (line);
9ab4a725 308
edfef199
GM
309 if (len > 0 && line[len - 1] == '\n')
310 line[--len] = '\0';
9ab4a725 311
edfef199
GM
312 val = build_string (line);
313 xfree (line);
314 }
315 else
316 {
317 xfree (line);
318 error ("Error reading from stdin");
319 }
9ab4a725 320
edfef199
GM
321 /* If Lisp form desired instead of string, parse it. */
322 if (expflag)
323 val = string_to_object (val, defalt);
9ab4a725 324
edfef199
GM
325 return val;
326}
1011edb5 327\f
873ef78e 328DEFUN ("minibufferp", Fminibufferp,
6d1b1d7b
JPW
329 Sminibufferp, 0, 1, 0,
330 doc: /* Return t if BUFFER is a minibuffer.
b0c138ce
LT
331No argument or nil as argument means use current buffer as BUFFER.
332BUFFER can be a buffer or a buffer name. */)
6d1b1d7b
JPW
333 (buffer)
334 Lisp_Object buffer;
873ef78e
RS
335{
336 Lisp_Object tem;
337
6d1b1d7b
JPW
338 if (NILP (buffer))
339 buffer = Fcurrent_buffer ();
340 else if (STRINGP (buffer))
341 buffer = Fget_buffer (buffer);
342 else
343 CHECK_BUFFER (buffer);
344
345 tem = Fmemq (buffer, Vminibuffer_list);
873ef78e
RS
346 return ! NILP (tem) ? Qt : Qnil;
347}
348
a346ec72
MB
349DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
350 Sminibuffer_prompt_end, 0, 0, 0,
af52c32d 351 doc: /* Return the buffer position of the end of the minibuffer prompt.
eb7c9b64 352Return (point-min) if current buffer is not a minibuffer. */)
af52c32d 353 ()
a346ec72
MB
354{
355 /* This function is written to be most efficient when there's a prompt. */
873ef78e
RS
356 Lisp_Object beg, end, tem;
357 beg = make_number (BEGV);
358
359 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
360 if (NILP (tem))
361 return beg;
362
363 end = Ffield_end (beg, Qnil, Qnil);
9ab4a725 364
a346ec72 365 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
b056f36d 366 return beg;
a346ec72
MB
367 else
368 return end;
369}
370
371DEFUN ("minibuffer-contents", Fminibuffer_contents,
372 Sminibuffer_contents, 0, 0, 0,
029a305c 373 doc: /* Return the user input in a minibuffer as a string.
af52c32d
MB
374The current buffer must be a minibuffer. */)
375 ()
a346ec72
MB
376{
377 int prompt_end = XINT (Fminibuffer_prompt_end ());
378 return make_buffer_string (prompt_end, ZV, 1);
379}
380
381DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
382 Sminibuffer_contents_no_properties, 0, 0, 0,
029a305c 383 doc: /* Return the user input in a minibuffer as a string, without text-properties.
af52c32d
MB
384The current buffer must be a minibuffer. */)
385 ()
a346ec72
MB
386{
387 int prompt_end = XINT (Fminibuffer_prompt_end ());
388 return make_buffer_string (prompt_end, ZV, 0);
389}
390
b5e1e449
JL
391DEFUN ("minibuffer-completion-contents", Fminibuffer_completion_contents,
392 Sminibuffer_completion_contents, 0, 0, 0,
393 doc: /* Return the user input in a minibuffer before point as a string.
394That is what completion commands operate on.
395The current buffer must be a minibuffer. */)
396 ()
397{
398 int prompt_end = XINT (Fminibuffer_prompt_end ());
399 if (PT < prompt_end)
400 error ("Cannot do completion in the prompt");
401 return make_buffer_string (prompt_end, PT, 1);
402}
403
a346ec72
MB
404DEFUN ("delete-minibuffer-contents", Fdelete_minibuffer_contents,
405 Sdelete_minibuffer_contents, 0, 0, 0,
af52c32d
MB
406 doc: /* Delete all user input in a minibuffer.
407The current buffer must be a minibuffer. */)
408 ()
a346ec72
MB
409{
410 int prompt_end = XINT (Fminibuffer_prompt_end ());
411 if (prompt_end < ZV)
412 del_range (prompt_end, ZV);
413 return Qnil;
414}
415
1011edb5 416\f
adb0708c
LT
417/* Read from the minibuffer using keymap MAP and initial contents INITIAL,
418 putting point minus BACKUP_N bytes from the end of INITIAL,
770970cb 419 prompting with PROMPT (a string), using history list HISTVAR
adb0708c
LT
420 with initial position HISTPOS. INITIAL should be a string or a
421 cons of a string and an integer. BACKUP_N should be <= 0, or
422 Qnil, which is equivalent to 0. If INITIAL is a cons, BACKUP_N is
b0c138ce
LT
423 ignored and replaced with an integer that puts point at one-indexed
424 position N in INITIAL, where N is the CDR of INITIAL, or at the
425 beginning of INITIAL if N <= 0.
770970cb
RS
426
427 Normally return the result as a string (the text that was read),
3ab14176 428 but if EXPFLAG is nonzero, read it and return the object read.
b278606c
BF
429 If HISTVAR is given, save the value read on that history only if it doesn't
430 match the front of that history list exactly. The value is pushed onto
e5d4686b 431 the list as the string that was read.
770970cb 432
4f9f637a 433 DEFALT specifies the default value for the sake of history commands.
c3421833 434
cee54539
KH
435 If ALLOW_PROPS is nonzero, we do not throw away text properties.
436
adb0708c 437 if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
cee54539 438 current input method. */
e5d4686b
RS
439
440static Lisp_Object
441read_minibuf (map, initial, prompt, backup_n, expflag,
da34f64e
RS
442 histvar, histpos, defalt, allow_props, inherit_input_method,
443 keep_all)
f927c5ae
JB
444 Lisp_Object map;
445 Lisp_Object initial;
446 Lisp_Object prompt;
5061d9c3 447 Lisp_Object backup_n;
f927c5ae 448 int expflag;
770970cb
RS
449 Lisp_Object histvar;
450 Lisp_Object histpos;
e5d4686b 451 Lisp_Object defalt;
af2b7cd5 452 int allow_props;
cee54539 453 int inherit_input_method;
da34f64e 454 int keep_all;
f927c5ae 455{
00a34088 456 Lisp_Object val;
aed13378 457 int count = SPECPDL_INDEX ();
cee54539
KH
458 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
459 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
c75000c7 460 Lisp_Object enable_multibyte;
adb0708c 461 int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
4f9f637a
RS
462
463 /* String to add to the history. */
464 Lisp_Object histstring;
465
b20de1cd
RS
466 Lisp_Object empty_minibuf;
467 Lisp_Object dummy, frame;
468
04012254 469 extern Lisp_Object Qfront_sticky;
fbd1209a 470 extern Lisp_Object Qrear_nonsticky;
00a34088 471
e5d4686b
RS
472 specbind (Qminibuffer_default, defalt);
473
08f7d623 474 single_kboard_state ();
e5baa1a7 475#ifdef HAVE_X_WINDOWS
526a058f
GM
476 if (display_hourglass_p)
477 cancel_hourglass ();
e5baa1a7 478#endif
718d3251 479
adb0708c
LT
480 if (!NILP (initial))
481 {
482 if (CONSP (initial))
483 {
484 backup_n = Fcdr (initial);
485 initial = Fcar (initial);
486 CHECK_STRING (initial);
487 if (!NILP (backup_n))
488 {
489 CHECK_NUMBER (backup_n);
490 /* Convert to distance from end of input. */
491 if (XINT (backup_n) < 1)
492 /* A number too small means the beginning of the string. */
493 pos = - SCHARS (initial);
494 else
495 pos = XINT (backup_n) - 1 - SCHARS (initial);
496 }
497 }
498 else
499 CHECK_STRING (initial);
500 }
00a34088 501 val = Qnil;
57ceaa8a 502 ambient_dir = current_buffer->directory;
cee54539 503 input_method = Qnil;
c75000c7 504 enable_multibyte = Qnil;
57ceaa8a 505
00a34088
RS
506 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
507 store them away before we can GC. Don't need to protect
508 BACKUP_N because we use the value only if it is an integer. */
cee54539 509 GCPRO5 (map, initial, val, ambient_dir, input_method);
f927c5ae 510
7510b296 511 if (!STRINGP (prompt))
695deb18 512 prompt = empty_string;
f927c5ae 513
f927c5ae 514 if (!enable_recursive_minibuffers
be15a518
RS
515 && minibuf_level > 0)
516 {
517 if (EQ (selected_window, minibuf_window))
518 error ("Command attempted to use minibuffer while in minibuffer");
519 else
520 /* If we're in another window, cancel the minibuffer that's active. */
521 Fthrow (Qexit,
522 build_string ("Command attempted to use minibuffer while in minibuffer"));
523 }
f927c5ae 524
edfef199 525 if (noninteractive)
c28b847b 526 {
adb0708c
LT
527 val = read_minibuf_noninteractive (map, initial, prompt,
528 make_number (pos),
c28b847b
GM
529 expflag, histvar, histpos, defalt,
530 allow_props, inherit_input_method);
f9b9ccef 531 UNGCPRO;
c28b847b
GM
532 return unbind_to (count, val);
533 }
edfef199 534
748dc60a 535 /* Choose the minibuffer window and frame, and take action on them. */
f927c5ae 536
c5b6b680
RS
537 choose_minibuf_frame ();
538
0c94f256
RS
539 record_unwind_protect (choose_minibuf_frame_1, Qnil);
540
f927c5ae 541 record_unwind_protect (Fset_window_configuration,
b2b2c677
JB
542 Fcurrent_window_configuration (Qnil));
543
ff11dfa1
JB
544 /* If the minibuffer window is on a different frame, save that
545 frame's configuration too. */
75f00e72 546 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
a4aafc54 547 if (!EQ (mini_frame, selected_frame))
5061d9c3
RS
548 record_unwind_protect (Fset_window_configuration,
549 Fcurrent_window_configuration (mini_frame));
5563e8e8
KH
550
551 /* If the minibuffer is on an iconified or invisible frame,
552 make it visible now. */
553 Fmake_frame_visible (mini_frame);
554
6a9ee000
RS
555 if (minibuffer_auto_raise)
556 Fraise_frame (mini_frame);
f927c5ae 557
748dc60a
RS
558 /* We have to do this after saving the window configuration
559 since that is what restores the current buffer. */
560
561 /* Arrange to restore a number of minibuffer-related variables.
562 We could bind each variable separately, but that would use lots of
563 specpdl slots. */
564 minibuf_save_list
565 = Fcons (Voverriding_local_map,
566 Fcons (minibuf_window, minibuf_save_list));
567 minibuf_save_list
568 = Fcons (minibuf_prompt,
569 Fcons (make_number (minibuf_prompt_width),
570 Fcons (Vhelp_form,
571 Fcons (Vcurrent_prefix_arg,
572 Fcons (Vminibuffer_history_position,
573 Fcons (Vminibuffer_history_variable,
574 minibuf_save_list))))));
575
576 record_unwind_protect (read_minibuf_unwind, Qnil);
577 minibuf_level++;
7292839d
SM
578 /* We are exiting the minibuffer one way or the other, so run the hook.
579 It should be run before unwinding the minibuf settings. Do it
580 separately from read_minibuf_unwind because we need to make sure that
581 read_minibuf_unwind is fully executed even if exit-minibuffer-hook
582 signals an error. --Stef */
583 record_unwind_protect (run_exit_minibuf_hook, Qnil);
748dc60a
RS
584
585 /* Now that we can restore all those variables, start changing them. */
586
1e3f16d5 587 minibuf_prompt_width = 0;
748dc60a
RS
588 minibuf_prompt = Fcopy_sequence (prompt);
589 Vminibuffer_history_position = histpos;
590 Vminibuffer_history_variable = histvar;
591 Vhelp_form = Vminibuffer_help_form;
592
cee54539 593 if (inherit_input_method)
c75000c7 594 {
fd771ceb 595 /* `current-input-method' is buffer local. So, remember it in
c75000c7
RS
596 INPUT_METHOD before changing the current buffer. */
597 input_method = Fsymbol_value (Qcurrent_input_method);
598 enable_multibyte = current_buffer->enable_multibyte_characters;
599 }
cee54539 600
748dc60a
RS
601 /* Switch to the minibuffer. */
602
4f69d8f6
RS
603 minibuffer = get_minibuffer (minibuf_level);
604 Fset_buffer (minibuffer);
64a3a3c0 605
1d69c502
RS
606 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
607 if (inherit_input_method)
608 current_buffer->enable_multibyte_characters = enable_multibyte;
609
64a3a3c0
JB
610 /* The current buffer's default directory is usually the right thing
611 for our minibuffer here. However, if you're typing a command at
612 a minibuffer-only frame when minibuf_level is zero, then buf IS
613 the current_buffer, so reset_buffer leaves buf's default
614 directory unchanged. This is a bummer when you've just started
615 up Emacs and buf's default directory is Qnil. Here's a hack; can
616 you think of something better to do? Find another buffer with a
617 better directory, and use that one instead. */
748dc60a
RS
618 if (STRINGP (ambient_dir))
619 current_buffer->directory = ambient_dir;
64a3a3c0
JB
620 else
621 {
622 Lisp_Object buf_list;
623
624 for (buf_list = Vbuffer_alist;
625 CONSP (buf_list);
7539e11f 626 buf_list = XCDR (buf_list))
64a3a3c0 627 {
1e62748e 628 Lisp_Object other_buf;
64a3a3c0 629
7539e11f 630 other_buf = XCDR (XCAR (buf_list));
7510b296 631 if (STRINGP (XBUFFER (other_buf)->directory))
64a3a3c0
JB
632 {
633 current_buffer->directory = XBUFFER (other_buf)->directory;
634 break;
635 }
636 }
637 }
638
a4aafc54
GM
639 if (!EQ (mini_frame, selected_frame))
640 Fredirect_frame_focus (selected_frame, mini_frame);
43bad991 641
f927c5ae 642 Vminibuf_scroll_window = selected_window;
6122844a 643 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
5705966b 644 minibuf_selected_window = selected_window;
b20de1cd
RS
645
646 /* Empty out the minibuffers of all frames other than the one
647 where we are going to display one now.
648 Set them to point to ` *Minibuf-0*', which is always empty. */
649 empty_minibuf = Fget_buffer (build_string (" *Minibuf-0*"));
650
651 FOR_EACH_FRAME (dummy, frame)
652 {
653 Lisp_Object root_window = Fframe_root_window (frame);
654 Lisp_Object mini_window = XWINDOW (root_window)->next;
655
656 if (! NILP (mini_window) && !NILP (Fwindow_minibuffer_p (mini_window)))
657 Fset_window_buffer (mini_window, empty_minibuf, Qnil);
658 }
659
660 /* Display this minibuffer in the proper window. */
cbbfe056 661 Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
f1321dc3 662 Fselect_window (minibuf_window, Qnil);
5a866662 663 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
f927c5ae 664
748dc60a
RS
665 Fmake_local_variable (Qprint_escape_newlines);
666 print_escape_newlines = 1;
667
668 /* Erase the buffer. */
59115a22 669 {
331379bf 670 int count1 = SPECPDL_INDEX ();
59115a22 671 specbind (Qinhibit_read_only, Qt);
a41edd99 672 specbind (Qinhibit_modification_hooks, Qt);
59115a22
RS
673 Ferase_buffer ();
674 unbind_to (count1, Qnil);
675 }
676
279cf52c
GM
677 if (!NILP (current_buffer->enable_multibyte_characters)
678 && ! STRING_MULTIBYTE (minibuf_prompt))
679 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
680
279cf52c
GM
681 /* Insert the prompt, record where it ends. */
682 Finsert (1, &minibuf_prompt);
279cf52c
GM
683 if (PT > BEG)
684 {
fbd1209a 685 Fput_text_property (make_number (BEG), make_number (PT),
279cf52c 686 Qfront_sticky, Qt, Qnil);
fbd1209a 687 Fput_text_property (make_number (BEG), make_number (PT),
279cf52c 688 Qrear_nonsticky, Qt, Qnil);
8e9968c6
GM
689 Fput_text_property (make_number (BEG), make_number (PT),
690 Qfield, Qt, Qnil);
cc64f5c9
MB
691 Fadd_text_properties (make_number (BEG), make_number (PT),
692 Vminibuffer_prompt_properties, Qnil);
279cf52c 693 }
9ab4a725
TTN
694
695 minibuf_prompt_width = (int) current_column (); /* iftc */
696
748dc60a 697 /* Put in the initial input. */
56a98455 698 if (!NILP (initial))
f927c5ae
JB
699 {
700 Finsert (1, &initial);
adb0708c 701 Fforward_char (make_number (pos));
f927c5ae
JB
702 }
703
39e98b38 704 clear_message (1, 1);
f927c5ae
JB
705 current_buffer->keymap = map;
706
cee54539 707 /* Turn on an input method stored in INPUT_METHOD if any. */
6801b300 708 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
cee54539
KH
709 call1 (Qactivate_input_method, input_method);
710
5c781212
RS
711 /* Run our hook, but not if it is empty.
712 (run-hooks would do nothing if it is empty,
fd771ceb 713 but it's important to save time here in the usual case.) */
92d3b06e
RS
714 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
715 && !NILP (Vrun_hooks))
5c781212
RS
716 call1 (Vrun_hooks, Qminibuffer_setup_hook);
717
4e6b7204
MB
718 /* Don't allow the user to undo past this point. */
719 current_buffer->undo_list = Qnil;
720
f927c5ae
JB
721 recursive_edit_1 ();
722
723 /* If cursor is on the minibuffer line,
724 show the user we have exited by putting it in column 0. */
279cf52c 725 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
f927c5ae
JB
726 && !noninteractive)
727 {
279cf52c
GM
728 XWINDOW (minibuf_window)->cursor.hpos = 0;
729 XWINDOW (minibuf_window)->cursor.x = 0;
730 XWINDOW (minibuf_window)->must_be_updated_p = 1;
a4aafc54 731 update_frame (XFRAME (selected_frame), 1, 1);
337aaf1f
GM
732 if (rif && rif->flush_display)
733 rif->flush_display (XFRAME (XWINDOW (minibuf_window)->frame));
f927c5ae
JB
734 }
735
c3421833 736 /* Make minibuffer contents into a string. */
4f69d8f6 737 Fset_buffer (minibuffer);
be95a9b6 738 if (allow_props)
a346ec72 739 val = Fminibuffer_contents ();
be95a9b6 740 else
a346ec72 741 val = Fminibuffer_contents_no_properties ();
770970cb 742
b278606c 743 /* VAL is the string of minibuffer text. */
7019cca7 744
b278606c
BF
745 last_minibuf_string = val;
746
4f9f637a 747 /* Choose the string to add to the history. */
da34f64e 748 if (SCHARS (val) != 0 || keep_all)
4f9f637a
RS
749 histstring = val;
750 else if (STRINGP (defalt))
751 histstring = defalt;
752 else
753 histstring = Qnil;
754
755 /* Add the value to the appropriate history list, if any. */
756 if (SYMBOLP (Vminibuffer_history_variable)
757 && !NILP (histstring))
3ab14176
KH
758 {
759 /* If the caller wanted to save the value read on a history list,
760 then do so if the value is not already the front of the list. */
761 Lisp_Object histval;
c6d65724
RS
762
763 /* If variable is unbound, make it nil. */
f5c1dd0d 764 if (EQ (SYMBOL_VALUE (Vminibuffer_history_variable), Qunbound))
c6d65724
RS
765 Fset (Vminibuffer_history_variable, Qnil);
766
3ab14176
KH
767 histval = Fsymbol_value (Vminibuffer_history_variable);
768
769 /* The value of the history variable must be a cons or nil. Other
770 values are unacceptable. We silently ignore these values. */
4f9f637a 771
3ab14176 772 if (NILP (histval)
9f6131cf 773 || (CONSP (histval)
4f9f637a 774 /* Don't duplicate the most recent entry in the history. */
da34f64e
RS
775 && (keep_all
776 || NILP (Fequal (histstring, Fcar (histval))))))
77aa8edf
RS
777 {
778 Lisp_Object length;
779
0da4d471 780 if (history_delete_duplicates) Fdelete (histstring, histval);
4f9f637a 781 histval = Fcons (histstring, histval);
77aa8edf
RS
782 Fset (Vminibuffer_history_variable, histval);
783
784 /* Truncate if requested. */
785 length = Fget (Vminibuffer_history_variable, Qhistory_length);
786 if (NILP (length)) length = Vhistory_length;
e5d4686b
RS
787 if (INTEGERP (length))
788 {
789 if (XINT (length) <= 0)
790 Fset (Vminibuffer_history_variable, Qnil);
791 else
792 {
793 Lisp_Object temp;
794
795 temp = Fnthcdr (Fsub1 (length), histval);
796 if (CONSP (temp)) Fsetcdr (temp, Qnil);
797 }
798 }
77aa8edf 799 }
9f6131cf
RS
800 }
801
802 /* If Lisp form desired instead of string, parse it. */
803 if (expflag)
edfef199 804 val = string_to_object (val, defalt);
3ab14176 805
00a34088
RS
806 /* The appropriate frame will get selected
807 in set-window-configuration. */
f9b9ccef
SM
808 UNGCPRO;
809 return unbind_to (count, val);
f927c5ae
JB
810}
811
812/* Return a buffer to be used as the minibuffer at depth `depth'.
813 depth = 0 is the lowest allowed argument, and that is the value
814 used for nonrecursive minibuffer invocations */
815
816Lisp_Object
817get_minibuffer (depth)
818 int depth;
819{
820 Lisp_Object tail, num, buf;
9f6c23bc 821 char name[24];
f927c5ae
JB
822 extern Lisp_Object nconc2 ();
823
5a866662 824 XSETFASTINT (num, depth);
f927c5ae 825 tail = Fnthcdr (num, Vminibuffer_list);
56a98455 826 if (NILP (tail))
f927c5ae
JB
827 {
828 tail = Fcons (Qnil, Qnil);
829 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
830 }
831 buf = Fcar (tail);
56a98455 832 if (NILP (buf) || NILP (XBUFFER (buf)->name))
f927c5ae
JB
833 {
834 sprintf (name, " *Minibuf-%d*", depth);
835 buf = Fget_buffer_create (build_string (name));
5d6533f1
JB
836
837 /* Although the buffer's name starts with a space, undo should be
838 enabled in it. */
839 Fbuffer_enable_undo (buf);
840
f3fbd155 841 XSETCAR (tail, buf);
f927c5ae
JB
842 }
843 else
5956f71d 844 {
aed13378 845 int count = SPECPDL_INDEX ();
7f856567
SM
846 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
847 have to empty the list, otherwise we end up with overlays that
848 think they belong to this buffer while the buffer doesn't know about
849 them any more. */
599ca9c2
SM
850 delete_all_overlays (XBUFFER (buf));
851 reset_buffer (XBUFFER (buf));
6b3faad8
RS
852 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
853 Fset_buffer (buf);
854 Fkill_all_local_variables ();
855 unbind_to (count, Qnil);
5956f71d 856 }
64a3a3c0 857
f927c5ae
JB
858 return buf;
859}
860
7292839d
SM
861static Lisp_Object
862run_exit_minibuf_hook (data)
863 Lisp_Object data;
864{
865 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
866 && !NILP (Vrun_hooks))
867 safe_run_hooks (Qminibuffer_exit_hook);
868
869 return Qnil;
870}
871
279cf52c
GM
872/* This function is called on exiting minibuffer, whether normally or
873 not, and it restores the current window, buffer, etc. */
f927c5ae 874
9d815fd9 875static Lisp_Object
43bad991
JB
876read_minibuf_unwind (data)
877 Lisp_Object data;
f927c5ae 878{
c24e1160 879 Lisp_Object old_deactivate_mark;
59115a22 880 Lisp_Object window;
c24e1160 881
f927c5ae 882 /* If this was a recursive minibuffer,
59115a22 883 tie the minibuffer window back to the outer level minibuffer buffer. */
f927c5ae 884 minibuf_level--;
f927c5ae 885
59115a22 886 window = minibuf_window;
279cf52c
GM
887 /* To keep things predictable, in case it matters, let's be in the
888 minibuffer when we reset the relevant variables. */
59115a22
RS
889 Fset_buffer (XWINDOW (window)->buffer);
890
891 /* Restore prompt, etc, from outer minibuffer level. */
4d04c1f1
KH
892 minibuf_prompt = Fcar (minibuf_save_list);
893 minibuf_save_list = Fcdr (minibuf_save_list);
894 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
895 minibuf_save_list = Fcdr (minibuf_save_list);
896 Vhelp_form = Fcar (minibuf_save_list);
897 minibuf_save_list = Fcdr (minibuf_save_list);
ee9e37ab 898 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
4d04c1f1
KH
899 minibuf_save_list = Fcdr (minibuf_save_list);
900 Vminibuffer_history_position = Fcar (minibuf_save_list);
901 minibuf_save_list = Fcdr (minibuf_save_list);
902 Vminibuffer_history_variable = Fcar (minibuf_save_list);
903 minibuf_save_list = Fcdr (minibuf_save_list);
30e13e56 904 Voverriding_local_map = Fcar (minibuf_save_list);
c5b6b680 905 minibuf_save_list = Fcdr (minibuf_save_list);
914860c2
RS
906#if 0
907 temp = Fcar (minibuf_save_list);
908 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
909 minibuf_window = temp;
910#endif
30e13e56 911 minibuf_save_list = Fcdr (minibuf_save_list);
59115a22
RS
912
913 /* Erase the minibuffer we were using at this level. */
914 {
aed13378 915 int count = SPECPDL_INDEX ();
59115a22
RS
916 /* Prevent error in erase-buffer. */
917 specbind (Qinhibit_read_only, Qt);
bd819d14 918 specbind (Qinhibit_modification_hooks, Qt);
59115a22
RS
919 old_deactivate_mark = Vdeactivate_mark;
920 Ferase_buffer ();
921 Vdeactivate_mark = old_deactivate_mark;
922 unbind_to (count, Qnil);
923 }
924
544e358d
GM
925 /* When we get to the outmost level, make sure we resize the
926 mini-window back to its normal size. */
927 if (minibuf_level == 0)
1e3f16d5 928 resize_mini_window (XWINDOW (window), 0);
544e358d 929
59115a22
RS
930 /* Make sure minibuffer window is erased, not ignored. */
931 windows_or_buffers_changed++;
932 XSETFASTINT (XWINDOW (window)->last_modified, 0);
193e4518 933 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
9d815fd9 934 return Qnil;
f927c5ae
JB
935}
936\f
b9d721de 937
da34f64e 938DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 8, 0,
af52c32d 939 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
b0c138ce
LT
940The optional second arg INITIAL-CONTENTS is an obsolete alternative to
941 DEFAULT-VALUE. It normally should be nil in new code, except when
942 HIST is a cons. It is discussed in more detail below.
a1f17501
PJ
943Third arg KEYMAP is a keymap to use whilst reading;
944 if omitted or nil, the default is `minibuffer-local-map'.
fd771ceb 945If fourth arg READ is non-nil, then interpret the result as a Lisp object
a1f17501
PJ
946 and return that object:
947 in other words, do `(car (read-from-string INPUT-STRING))'
b0c138ce
LT
948Fifth arg HIST, if non-nil, specifies a history list and optionally
949 the initial position in the list. It can be a symbol, which is the
950 history list variable to use, or it can be a cons cell
951 (HISTVAR . HISTPOS). In that case, HISTVAR is the history list variable
952 to use, and HISTPOS is the initial position for use by the minibuffer
953 history commands. For consistency, you should also specify that
954 element of the history as the value of INITIAL-CONTENTS. Positions
955 are counted starting from 1 at the beginning of the list.
a1f17501 956Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
adb0708c
LT
957 for history commands; but, unless READ is non-nil, `read-from-minibuffer'
958 does NOT return DEFAULT-VALUE if the user enters empty input! It returns
959 the empty string.
a1f17501 960Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
029a305c 961 the current input method and the setting of `enable-multibyte-characters'.
da34f64e
RS
962Eight arg KEEP-ALL, if non-nil, says to put all inputs in the history list,
963 even empty or duplicate inputs.
a1f17501
PJ
964If the variable `minibuffer-allow-text-properties' is non-nil,
965 then the string which is returned includes whatever text properties
b0c138ce
LT
966 were present in the minibuffer. Otherwise the value has no text properties.
967
968The remainder of this documentation string describes the
969INITIAL-CONTENTS argument in more detail. It is only relevant when
970studying existing code, or when HIST is a cons. If non-nil,
971INITIAL-CONTENTS is a string to be inserted into the minibuffer before
972reading input. Normally, point is put at the end of that string.
973However, if INITIAL-CONTENTS is \(STRING . POSITION), the initial
974input is STRING, but point is placed at _one-indexed_ position
975POSITION in the minibuffer. Any integer value less than or equal to
976one puts point at the beginning of the string. *Note* that this
977behavior differs from the way such arguments are used in `completing-read'
978and some related functions, which use zero-indexing for POSITION. */)
da34f64e 979 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method, keep_all)
e5d4686b 980 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
da34f64e 981 Lisp_Object inherit_input_method, keep_all;
f927c5ae 982{
adb0708c 983 Lisp_Object histvar, histpos, val;
1d8d92f4
RS
984 struct gcpro gcpro1;
985
b7826503 986 CHECK_STRING (prompt);
56a98455 987 if (NILP (keymap))
f927c5ae
JB
988 keymap = Vminibuffer_local_map;
989 else
02067692 990 keymap = get_keymap (keymap, 1, 0);
770970cb 991
7510b296 992 if (SYMBOLP (hist))
770970cb
RS
993 {
994 histvar = hist;
995 histpos = Qnil;
996 }
997 else
998 {
999 histvar = Fcar_safe (hist);
1000 histpos = Fcdr_safe (hist);
1001 }
1002 if (NILP (histvar))
1003 histvar = Qminibuffer_history;
1004 if (NILP (histpos))
5a866662 1005 XSETFASTINT (histpos, 0);
770970cb 1006
1d8d92f4 1007 GCPRO1 (default_value);
e5d4686b 1008 val = read_minibuf (keymap, initial_contents, prompt,
adb0708c 1009 Qnil, !NILP (read),
c3421833 1010 histvar, histpos, default_value,
cee54539 1011 minibuffer_allow_text_properties,
da34f64e
RS
1012 !NILP (inherit_input_method),
1013 !NILP (keep_all));
1d8d92f4 1014 UNGCPRO;
e5d4686b 1015 return val;
f927c5ae
JB
1016}
1017
1018DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
b0c138ce 1019 doc: /* Return a Lisp object read using the minibuffer, unevaluated.
a1f17501 1020Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
b0c138ce
LT
1021is a string to insert in the minibuffer before reading.
1022\(INITIAL-CONTENTS can also be a cons of a string and an integer. Such
1023arguments are used as in `read-from-minibuffer') */)
af52c32d 1024 (prompt, initial_contents)
f927c5ae
JB
1025 Lisp_Object prompt, initial_contents;
1026{
b7826503 1027 CHECK_STRING (prompt);
770970cb 1028 return read_minibuf (Vminibuffer_local_map, initial_contents,
e5d4686b 1029 prompt, Qnil, 1, Qminibuffer_history,
da34f64e 1030 make_number (0), Qnil, 0, 0, 0);
f927c5ae
JB
1031}
1032
1033DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
af52c32d 1034 doc: /* Return value of Lisp expression read using the minibuffer.
a1f17501 1035Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
b0c138ce
LT
1036is a string to insert in the minibuffer before reading.
1037\(INITIAL-CONTENTS can also be a cons of a string and an integer. Such
eb7c9b64 1038arguments are used as in `read-from-minibuffer'.) */)
af52c32d 1039 (prompt, initial_contents)
f927c5ae
JB
1040 Lisp_Object prompt, initial_contents;
1041{
1042 return Feval (Fread_minibuffer (prompt, initial_contents));
1043}
1044
1045/* Functions that use the minibuffer to read various things. */
1046
cee54539 1047DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
af52c32d 1048 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
a1f17501 1049If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
b0c138ce
LT
1050 This argument has been superseded by DEFAULT-VALUE and should normally
1051 be nil in new code. It behaves as in `read-from-minibuffer'. See the
1052 documentation string of that function for details.
a1f17501
PJ
1053The third arg HISTORY, if non-nil, specifies a history list
1054 and optionally the initial position in the list.
1055See `read-from-minibuffer' for details of HISTORY argument.
1056Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
1057 for history commands, and as the value to return if the user enters
1058 the empty string.
1059Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
029a305c 1060 the current input method and the setting of `enable-multibyte-characters'. */)
af52c32d 1061 (prompt, initial_input, history, default_value, inherit_input_method)
e5d4686b 1062 Lisp_Object prompt, initial_input, history, default_value;
cee54539 1063 Lisp_Object inherit_input_method;
f927c5ae 1064{
b9a86585
RS
1065 Lisp_Object val;
1066 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
1067 Qnil, history, default_value,
da34f64e 1068 inherit_input_method, Qnil);
d5db4077 1069 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
b9a86585
RS
1070 val = default_value;
1071 return val;
f927c5ae
JB
1072}
1073
cee54539 1074DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
af52c32d 1075 doc: /* Read a string from the terminal, not allowing blanks.
b0c138ce
LT
1076Prompt with PROMPT. Whitespace terminates the input. If INITIAL is
1077non-nil, it should be a string, which is used as initial input, with
1078point positioned at the end, so that SPACE will accept the input.
1079\(Actually, INITIAL can also be a cons of a string and an integer.
1080Such values are treated as in `read-from-minibuffer', but are normally
1081not useful in this function.)
a1f17501 1082Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
b0c138ce 1083the current input method and the setting of`enable-multibyte-characters'. */)
af52c32d 1084 (prompt, initial, inherit_input_method)
c923ccc9 1085 Lisp_Object prompt, initial, inherit_input_method;
f927c5ae 1086{
b7826503 1087 CHECK_STRING (prompt);
c923ccc9 1088 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
cee54539 1089 0, Qminibuffer_history, make_number (0), Qnil, 0,
da34f64e 1090 !NILP (inherit_input_method), 0);
f927c5ae
JB
1091}
1092
e5d4686b 1093DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
af52c32d 1094 doc: /* Read the name of a command and return as a symbol.
fd771ceb 1095Prompt with PROMPT. By default, return DEFAULT-VALUE. */)
af52c32d 1096 (prompt, default_value)
e5d4686b 1097 Lisp_Object prompt, default_value;
f927c5ae 1098{
7c8d9931
RS
1099 Lisp_Object name, default_string;
1100
1101 if (NILP (default_value))
1102 default_string = Qnil;
1103 else if (SYMBOLP (default_value))
caa4733e 1104 default_string = SYMBOL_NAME (default_value);
7c8d9931
RS
1105 else
1106 default_string = default_value;
9ab4a725 1107
7c8d9931
RS
1108 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1109 Qnil, Qnil, default_string, Qnil);
1110 if (NILP (name))
1111 return name;
1112 return Fintern (name, Qnil);
f927c5ae
JB
1113}
1114
1115#ifdef NOTDEF
1116DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
af52c32d 1117 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
fd771ceb 1118Prompt with PROMPT. */)
af52c32d 1119 (prompt)
f927c5ae
JB
1120 Lisp_Object prompt;
1121{
cee54539 1122 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
f927c5ae
JB
1123 Qnil);
1124}
1125#endif /* NOTDEF */
1126
e5d4686b 1127DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
af52c32d 1128 doc: /* Read the name of a user variable and return it as a symbol.
fd771ceb 1129Prompt with PROMPT. By default, return DEFAULT-VALUE.
316f9bfc 1130A user variable is one for which `user-variable-p' returns non-nil. */)
af52c32d 1131 (prompt, default_value)
e5d4686b 1132 Lisp_Object prompt, default_value;
f927c5ae 1133{
7c8d9931
RS
1134 Lisp_Object name, default_string;
1135
1136 if (NILP (default_value))
1137 default_string = Qnil;
1138 else if (SYMBOLP (default_value))
caa4733e 1139 default_string = SYMBOL_NAME (default_value);
7c8d9931
RS
1140 else
1141 default_string = default_value;
9ab4a725 1142
7c8d9931
RS
1143 name = Fcompleting_read (prompt, Vobarray,
1144 Quser_variable_p, Qt,
1145 Qnil, Qnil, default_string, Qnil);
1146 if (NILP (name))
1147 return name;
1148 return Fintern (name, Qnil);
f927c5ae
JB
1149}
1150
1151DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
fd771ceb
PJ
1152 doc: /* Read the name of a buffer and return as a string.
1153Prompt with PROMPT.
a1f17501 1154Optional second arg DEF is value to return if user enters an empty line.
fd771ceb 1155If optional third arg REQUIRE-MATCH is non-nil,
97c3e4cc
RF
1156 only existing buffer names are allowed.
1157The argument PROMPT should be a string ending with a colon and a space. */)
af52c32d 1158 (prompt, def, require_match)
f927c5ae
JB
1159 Lisp_Object prompt, def, require_match;
1160{
eebbfb01 1161 Lisp_Object args[4];
97c3e4cc
RF
1162 unsigned char *s;
1163 int len;
9ab4a725 1164
7510b296 1165 if (BUFFERP (def))
f927c5ae 1166 def = XBUFFER (def)->name;
eebbfb01
KH
1167
1168 if (NILP (Vread_buffer_function))
1169 {
1170 if (!NILP (def))
1171 {
97c3e4cc
RF
1172 /* A default value was provided: we must change PROMPT,
1173 editing the default value in before the colon. To achieve
1174 this, we replace PROMPT with a substring that doesn't
1175 contain the terminal space and colon (if present). They
1176 are then added back using Fformat. */
1177
1178 if (STRINGP (prompt))
1179 {
1180 s = SDATA (prompt);
1181 len = strlen (s);
1182 if (len >= 2 && s[len - 2] == ':' && s[len - 1] == ' ')
1183 len = len - 2;
1184 else if (len >= 1 && (s[len - 1] == ':' || s[len - 1] == ' '))
1185 len--;
1186
1187 prompt = make_specified_string (s, -1, len,
1188 STRING_MULTIBYTE (prompt));
1189 }
1190
1191 args[0] = build_string ("%s (default %s): ");
eebbfb01
KH
1192 args[1] = prompt;
1193 args[2] = def;
1194 prompt = Fformat (3, args);
1195 }
1196
1197 return Fcompleting_read (prompt, Vbuffer_alist, Qnil,
406e55df
RS
1198 require_match, Qnil, Qbuffer_name_history,
1199 def, Qnil);
eebbfb01
KH
1200 }
1201 else
f927c5ae 1202 {
eebbfb01 1203 args[0] = Vread_buffer_function;
f927c5ae
JB
1204 args[1] = prompt;
1205 args[2] = def;
eebbfb01
KH
1206 args[3] = require_match;
1207 return Ffuncall(4, args);
f927c5ae 1208 }
f927c5ae
JB
1209}
1210\f
ec067ec7
RS
1211static Lisp_Object
1212minibuf_conform_representation (string, basis)
1213 Lisp_Object string, basis;
1214{
1215 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1216 return string;
1217
1218 if (STRING_MULTIBYTE (string))
1219 return Fstring_make_unibyte (string);
1220 else
1221 return Fstring_make_multibyte (string);
1222}
1223
f927c5ae 1224DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
af52c32d 1225 doc: /* Return common substring of all completions of STRING in ALIST.
f9b9ccef 1226Each car of each element of ALIST (or each element if it is not a cons cell)
3809648a
KS
1227is tested to see if it begins with STRING. The possible matches may be
1228strings or symbols. Symbols are converted to strings before testing,
1229see `symbol-name'.
a1f17501
PJ
1230All that match are compared together; the longest initial sequence
1231common to all matches is returned as a string.
1232If there is no match at all, nil is returned.
1233For a unique match which is exact, t is returned.
1234
3809648a
KS
1235If ALIST is a hash-table, all the string and symbol keys are the
1236possible matches.
85cd4372
SM
1237If ALIST is an obarray, the names of all symbols in the obarray
1238are the possible matches.
a1f17501
PJ
1239
1240ALIST can also be a function to do the completion itself.
1241It receives three arguments: the values STRING, PREDICATE and nil.
1242Whatever it returns becomes the value of `try-completion'.
1243
1244If optional third argument PREDICATE is non-nil,
1245it is used to test each possible match.
1246The match is a candidate only if PREDICATE returns non-nil.
1247The argument given to PREDICATE is the alist element
85cd4372
SM
1248or the symbol from the obarray. If ALIST is a hash-table,
1249predicate is called with two arguments: the key and the value.
a1f17501 1250Additionally to this predicate, `completion-regexp-list'
af52c32d
MB
1251is used to further constrain the set of candidates. */)
1252 (string, alist, predicate)
7efd36fc 1253 Lisp_Object string, alist, predicate;
f927c5ae
JB
1254{
1255 Lisp_Object bestmatch, tail, elt, eltstring;
af2b7cd5 1256 /* Size in bytes of BESTMATCH. */
6bbd7a29 1257 int bestmatchsize = 0;
af2b7cd5 1258 /* These are in bytes, too. */
f927c5ae 1259 int compare, matchsize;
1d69c502
RS
1260 int type = (HASH_TABLE_P (alist) ? 3
1261 : VECTORP (alist) ? 2
1262 : NILP (alist) || (CONSP (alist)
1263 && (!SYMBOLP (XCAR (alist))
1264 || NILP (XCAR (alist)))));
6bbd7a29 1265 int index = 0, obsize = 0;
f927c5ae 1266 int matchcount = 0;
acd81db9 1267 int bindcount = -1;
f927c5ae
JB
1268 Lisp_Object bucket, zero, end, tem;
1269 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1270
b7826503 1271 CHECK_STRING (string);
85cd4372 1272 if (type == 0)
7efd36fc 1273 return call3 (alist, string, predicate, Qnil);
f927c5ae 1274
6bbd7a29 1275 bestmatch = bucket = Qnil;
c8ae863b 1276 zero = make_number (0);
f927c5ae
JB
1277
1278 /* If ALIST is not a list, set TAIL just for gc pro. */
1279 tail = alist;
85cd4372 1280 if (type == 2)
f927c5ae 1281 {
f927c5ae
JB
1282 obsize = XVECTOR (alist)->size;
1283 bucket = XVECTOR (alist)->contents[index];
1284 }
1285
1286 while (1)
1287 {
85cd4372 1288 /* Get the next element of the alist, obarray, or hash-table. */
f927c5ae
JB
1289 /* Exit the loop if the elements are all used up. */
1290 /* elt gets the alist element or symbol.
1291 eltstring gets the name to check as a completion. */
1292
85cd4372 1293 if (type == 1)
f927c5ae 1294 {
695deb18 1295 if (!CONSP (tail))
f927c5ae 1296 break;
695deb18
SM
1297 elt = XCAR (tail);
1298 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1299 tail = XCDR (tail);
f927c5ae 1300 }
85cd4372 1301 else if (type == 2)
f927c5ae 1302 {
c8ae863b 1303 if (!EQ (bucket, zero))
f927c5ae
JB
1304 {
1305 elt = bucket;
3809648a 1306 eltstring = elt;
f927c5ae
JB
1307 if (XSYMBOL (bucket)->next)
1308 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1309 else
5a866662 1310 XSETFASTINT (bucket, 0);
f927c5ae
JB
1311 }
1312 else if (++index >= obsize)
1313 break;
1314 else
1315 {
1316 bucket = XVECTOR (alist)->contents[index];
1317 continue;
1318 }
1319 }
85cd4372
SM
1320 else /* if (type == 3) */
1321 {
1322 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1323 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1324 index++;
1325 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1326 break;
1327 else
1328 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1329 }
f927c5ae
JB
1330
1331 /* Is this element a possible completion? */
1332
3809648a
KS
1333 if (SYMBOLP (eltstring))
1334 eltstring = Fsymbol_name (eltstring);
1335
7510b296 1336 if (STRINGP (eltstring)
d5db4077 1337 && SCHARS (string) <= SCHARS (eltstring)
c8ae863b 1338 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1339 make_number (SCHARS (string)),
c8ae863b 1340 string, zero, Qnil,
b0c138ce 1341 completion_ignore_case ? Qt : Qnil),
69f4ef20 1342 EQ (Qt, tem)))
f927c5ae
JB
1343 {
1344 /* Yes. */
42006772 1345 Lisp_Object regexps;
42006772
RS
1346
1347 /* Ignore this element if it fails to match all the regexps. */
acd81db9
DK
1348 {
1349 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1350 regexps = XCDR (regexps))
1351 {
1352 if (bindcount < 0) {
1353 bindcount = SPECPDL_INDEX ();
1354 specbind (Qcase_fold_search,
1355 completion_ignore_case ? Qt : Qnil);
77443789 1356 }
acd81db9
DK
1357 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1358 if (NILP (tem))
1359 break;
1360 }
1361 if (CONSP (regexps))
1362 continue;
1363 }
42006772 1364
f927c5ae
JB
1365 /* Ignore this element if there is a predicate
1366 and the predicate doesn't like it. */
1367
7efd36fc 1368 if (!NILP (predicate))
f927c5ae 1369 {
7efd36fc 1370 if (EQ (predicate, Qcommandp))
c837f4f9 1371 tem = Fcommandp (elt, Qnil);
f927c5ae
JB
1372 else
1373 {
acd81db9
DK
1374 if (bindcount >= 0) {
1375 unbind_to (bindcount, Qnil);
1376 bindcount = -1;
1377 }
f927c5ae 1378 GCPRO4 (tail, string, eltstring, bestmatch);
85cd4372
SM
1379 tem = type == 3
1380 ? call2 (predicate, elt,
1381 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1382 : call1 (predicate, elt);
f927c5ae
JB
1383 UNGCPRO;
1384 }
56a98455 1385 if (NILP (tem)) continue;
f927c5ae
JB
1386 }
1387
1388 /* Update computation of how much all possible completions match */
1389
56a98455 1390 if (NILP (bestmatch))
af2b7cd5 1391 {
85cd4372 1392 matchcount = 1;
af2b7cd5 1393 bestmatch = eltstring;
d5db4077 1394 bestmatchsize = SCHARS (eltstring);
af2b7cd5 1395 }
f927c5ae
JB
1396 else
1397 {
d5db4077 1398 compare = min (bestmatchsize, SCHARS (eltstring));
c8ae863b 1399 tem = Fcompare_strings (bestmatch, zero,
69f4ef20 1400 make_number (compare),
c8ae863b 1401 eltstring, zero,
69f4ef20
RS
1402 make_number (compare),
1403 completion_ignore_case ? Qt : Qnil);
1404 if (EQ (tem, Qt))
1405 matchsize = compare;
1406 else if (XINT (tem) < 0)
1407 matchsize = - XINT (tem) - 1;
1408 else
1409 matchsize = XINT (tem) - 1;
1410
52b14ac0 1411 if (matchsize < 0)
695deb18 1412 /* When can this happen ? -stef */
52b14ac0
JB
1413 matchsize = compare;
1414 if (completion_ignore_case)
1415 {
1416 /* If this is an exact match except for case,
1417 use it as the best match rather than one that is not an
1418 exact match. This way, we get the case pattern
1419 of the actual match. */
d5db4077
KR
1420 if ((matchsize == SCHARS (eltstring)
1421 && matchsize < SCHARS (bestmatch))
52b14ac0
JB
1422 ||
1423 /* If there is more than one exact match ignoring case,
1424 and one of them is exact including case,
1425 prefer that one. */
1426 /* If there is no exact match ignoring case,
1427 prefer a match that does not change the case
1428 of the input. */
d5db4077 1429 ((matchsize == SCHARS (eltstring))
52b14ac0 1430 ==
d5db4077 1431 (matchsize == SCHARS (bestmatch))
c8ae863b 1432 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1433 make_number (SCHARS (string)),
c8ae863b 1434 string, zero,
69f4ef20
RS
1435 Qnil,
1436 Qnil),
1437 EQ (Qt, tem))
c8ae863b 1438 && (tem = Fcompare_strings (bestmatch, zero,
d5db4077 1439 make_number (SCHARS (string)),
c8ae863b 1440 string, zero,
69f4ef20
RS
1441 Qnil,
1442 Qnil),
1443 ! EQ (Qt, tem))))
52b14ac0
JB
1444 bestmatch = eltstring;
1445 }
d5db4077 1446 if (bestmatchsize != SCHARS (eltstring)
85cd4372
SM
1447 || bestmatchsize != matchsize)
1448 /* Don't count the same string multiple times. */
1449 matchcount++;
52b14ac0 1450 bestmatchsize = matchsize;
d5db4077 1451 if (matchsize <= SCHARS (string)
695deb18
SM
1452 && matchcount > 1)
1453 /* No need to look any further. */
1454 break;
f927c5ae
JB
1455 }
1456 }
1457 }
1458
acd81db9
DK
1459 if (bindcount >= 0) {
1460 unbind_to (bindcount, Qnil);
1461 bindcount = -1;
1462 }
1463
56a98455 1464 if (NILP (bestmatch))
f927c5ae 1465 return Qnil; /* No completions found */
52b14ac0
JB
1466 /* If we are ignoring case, and there is no exact match,
1467 and no additional text was supplied,
1468 don't change the case of what the user typed. */
d5db4077
KR
1469 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1470 && SCHARS (bestmatch) > bestmatchsize)
ec067ec7 1471 return minibuf_conform_representation (string, bestmatch);
52b14ac0
JB
1472
1473 /* Return t if the supplied string is an exact match (counting case);
1474 it does not require any change to be made. */
d5db4077 1475 if (matchcount == 1 && bestmatchsize == SCHARS (string)
69f4ef20
RS
1476 && (tem = Fcompare_strings (bestmatch, make_number (0),
1477 make_number (bestmatchsize),
1478 string, make_number (0),
1479 make_number (bestmatchsize),
1480 Qnil),
1481 EQ (Qt, tem)))
f927c5ae
JB
1482 return Qt;
1483
5a866662
KH
1484 XSETFASTINT (zero, 0); /* Else extract the part in which */
1485 XSETFASTINT (end, bestmatchsize); /* all completions agree */
f927c5ae
JB
1486 return Fsubstring (bestmatch, zero, end);
1487}
f927c5ae 1488\f
89a255dc 1489DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
af52c32d 1490 doc: /* Search for partial matches to STRING in ALIST.
f9b9ccef 1491Each car of each element of ALIST (or each element if it is not a cons cell)
3809648a
KS
1492is tested to see if it begins with STRING. The possible matches may be
1493strings or symbols. Symbols are converted to strings before testing,
1494see `symbol-name'.
a1f17501
PJ
1495The value is a list of all the strings from ALIST that match.
1496
3809648a
KS
1497If ALIST is a hash-table, all the string and symbol keys are the
1498possible matches.
85cd4372
SM
1499If ALIST is an obarray, the names of all symbols in the obarray
1500are the possible matches.
a1f17501
PJ
1501
1502ALIST can also be a function to do the completion itself.
1503It receives three arguments: the values STRING, PREDICATE and t.
1504Whatever it returns becomes the value of `all-completions'.
1505
1506If optional third argument PREDICATE is non-nil,
1507it is used to test each possible match.
1508The match is a candidate only if PREDICATE returns non-nil.
1509The argument given to PREDICATE is the alist element
85cd4372
SM
1510or the symbol from the obarray. If ALIST is a hash-table,
1511predicate is called with two arguments: the key and the value.
a1f17501
PJ
1512Additionally to this predicate, `completion-regexp-list'
1513is used to further constrain the set of candidates.
1514
1515If the optional fourth argument HIDE-SPACES is non-nil,
1516strings in ALIST that start with a space
af52c32d
MB
1517are ignored unless STRING itself starts with a space. */)
1518 (string, alist, predicate, hide_spaces)
7efd36fc 1519 Lisp_Object string, alist, predicate, hide_spaces;
f927c5ae
JB
1520{
1521 Lisp_Object tail, elt, eltstring;
1522 Lisp_Object allmatches;
85cd4372
SM
1523 int type = HASH_TABLE_P (alist) ? 3
1524 : VECTORP (alist) ? 2
1525 : NILP (alist) || (CONSP (alist)
1526 && (!SYMBOLP (XCAR (alist))
1527 || NILP (XCAR (alist))));
6bbd7a29 1528 int index = 0, obsize = 0;
acd81db9 1529 int bindcount = -1;
c8ae863b 1530 Lisp_Object bucket, tem, zero;
f927c5ae
JB
1531 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1532
b7826503 1533 CHECK_STRING (string);
85cd4372
SM
1534 if (type == 0)
1535 return call3 (alist, string, predicate, Qt);
6bbd7a29 1536 allmatches = bucket = Qnil;
c8ae863b 1537 zero = make_number (0);
f927c5ae
JB
1538
1539 /* If ALIST is not a list, set TAIL just for gc pro. */
1540 tail = alist;
85cd4372 1541 if (type == 2)
f927c5ae 1542 {
f927c5ae
JB
1543 obsize = XVECTOR (alist)->size;
1544 bucket = XVECTOR (alist)->contents[index];
1545 }
1546
1547 while (1)
1548 {
85cd4372 1549 /* Get the next element of the alist, obarray, or hash-table. */
f927c5ae
JB
1550 /* Exit the loop if the elements are all used up. */
1551 /* elt gets the alist element or symbol.
1552 eltstring gets the name to check as a completion. */
1553
85cd4372 1554 if (type == 1)
f927c5ae 1555 {
695deb18 1556 if (!CONSP (tail))
f927c5ae 1557 break;
695deb18
SM
1558 elt = XCAR (tail);
1559 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1560 tail = XCDR (tail);
f927c5ae 1561 }
85cd4372 1562 else if (type == 2)
f927c5ae 1563 {
c8ae863b 1564 if (!EQ (bucket, zero))
f927c5ae
JB
1565 {
1566 elt = bucket;
3809648a 1567 eltstring = elt;
f927c5ae
JB
1568 if (XSYMBOL (bucket)->next)
1569 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1570 else
5a866662 1571 XSETFASTINT (bucket, 0);
f927c5ae
JB
1572 }
1573 else if (++index >= obsize)
1574 break;
1575 else
1576 {
1577 bucket = XVECTOR (alist)->contents[index];
1578 continue;
1579 }
1580 }
85cd4372
SM
1581 else /* if (type == 3) */
1582 {
1583 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1584 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1585 index++;
1586 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1587 break;
1588 else
1589 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1590 }
f927c5ae
JB
1591
1592 /* Is this element a possible completion? */
1593
3809648a
KS
1594 if (SYMBOLP (eltstring))
1595 eltstring = Fsymbol_name (eltstring);
1596
7510b296 1597 if (STRINGP (eltstring)
d5db4077 1598 && SCHARS (string) <= SCHARS (eltstring)
89a255dc 1599 /* If HIDE_SPACES, reject alternatives that start with space
2cbaf886 1600 unless the input starts with space. */
d5db4077
KR
1601 && ((SBYTES (string) > 0
1602 && SREF (string, 0) == ' ')
1603 || SREF (eltstring, 0) != ' '
89a255dc 1604 || NILP (hide_spaces))
c8ae863b 1605 && (tem = Fcompare_strings (eltstring, zero,
d5db4077 1606 make_number (SCHARS (string)),
c8ae863b 1607 string, zero,
d5db4077 1608 make_number (SCHARS (string)),
69f4ef20
RS
1609 completion_ignore_case ? Qt : Qnil),
1610 EQ (Qt, tem)))
f927c5ae
JB
1611 {
1612 /* Yes. */
42006772
RS
1613 Lisp_Object regexps;
1614 Lisp_Object zero;
5a866662 1615 XSETFASTINT (zero, 0);
42006772
RS
1616
1617 /* Ignore this element if it fails to match all the regexps. */
acd81db9
DK
1618 {
1619 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1620 regexps = XCDR (regexps))
1621 {
1622 if (bindcount < 0) {
1623 bindcount = SPECPDL_INDEX ();
1624 specbind (Qcase_fold_search,
1625 completion_ignore_case ? Qt : Qnil);
77443789 1626 }
acd81db9
DK
1627 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1628 if (NILP (tem))
1629 break;
1630 }
1631 if (CONSP (regexps))
1632 continue;
1633 }
42006772 1634
f927c5ae
JB
1635 /* Ignore this element if there is a predicate
1636 and the predicate doesn't like it. */
1637
7efd36fc 1638 if (!NILP (predicate))
f927c5ae 1639 {
7efd36fc 1640 if (EQ (predicate, Qcommandp))
c837f4f9 1641 tem = Fcommandp (elt, Qnil);
f927c5ae
JB
1642 else
1643 {
acd81db9
DK
1644 if (bindcount >= 0) {
1645 unbind_to (bindcount, Qnil);
1646 bindcount = -1;
1647 }
f927c5ae 1648 GCPRO4 (tail, eltstring, allmatches, string);
85cd4372
SM
1649 tem = type == 3
1650 ? call2 (predicate, elt,
1651 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1652 : call1 (predicate, elt);
f927c5ae
JB
1653 UNGCPRO;
1654 }
56a98455 1655 if (NILP (tem)) continue;
f927c5ae
JB
1656 }
1657 /* Ok => put it on the list. */
1658 allmatches = Fcons (eltstring, allmatches);
1659 }
1660 }
1661
acd81db9
DK
1662 if (bindcount >= 0) {
1663 unbind_to (bindcount, Qnil);
1664 bindcount = -1;
1665 }
1666
f927c5ae
JB
1667 return Fnreverse (allmatches);
1668}
1669\f
1670Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1671Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1672Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
ccf330a6 1673Lisp_Object Vminibuffer_completing_file_name;
f927c5ae 1674
cee54539 1675DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
af52c32d 1676 doc: /* Read a string in the minibuffer, with completion.
a1f17501 1677PROMPT is a string to prompt with; normally it ends in a colon and a space.
b0c138ce 1678TABLE can be an list of strings, an alist, an obarray or a hash table.
a1f17501
PJ
1679TABLE can also be a function to do the completion itself.
1680PREDICATE limits completion to a subset of TABLE.
1681See `try-completion' and `all-completions' for more details
1682 on completion, TABLE, and PREDICATE.
1683
1684If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
1685 the input is (or completes to) an element of TABLE or is null.
345c105f 1686 If it is also not t, typing RET does not exit if it does non-null completion.
b0c138ce
LT
1687If the input is null, `completing-read' returns DEF, or an empty string
1688 if DEF is nil, regardless of the value of REQUIRE-MATCH.
1689
1690If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
1691 with point positioned at the end.
1692 If it is (STRING . POSITION), the initial input is STRING, but point
1693 is placed at _zero-indexed_ position POSITION in STRING. (*Note*
1694 that this is different from `read-from-minibuffer' and related
1695 functions, which use one-indexing for POSITION.) This feature is
1696 deprecated--it is best to pass nil for INITIAL-INPUT and supply the
1697 default value DEF instead. The user can yank the default value into
1698 the minibuffer easily using \\[next-history-element].
1699
1700HIST, if non-nil, specifies a history list and optionally the initial
1701 position in the list. It can be a symbol, which is the history list
1702 variable to use, or it can be a cons cell (HISTVAR . HISTPOS). In
1703 that case, HISTVAR is the history list variable to use, and HISTPOS
1704 is the initial position (the position in the list used by the
1705 minibuffer history commands). For consistency, you should also
1706 specify that element of the history as the value of
1fb101f3 1707 INITIAL-INPUT. (This is the only case in which you should use
b0c138ce
LT
1708 INITIAL-INPUT instead of DEF.) Positions are counted starting from
1709 1 at the beginning of the list. The variable `history-length'
1710 controls the maximum length of a history list.
38668f81 1711
a1f17501
PJ
1712DEF, if non-nil, is the default value.
1713
1714If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
029a305c 1715 the current input method and the setting of `enable-multibyte-characters'.
a1f17501
PJ
1716
1717Completion ignores case if the ambient value of
af52c32d
MB
1718 `completion-ignore-case' is non-nil. */)
1719 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method)
d43f85c2
RS
1720 Lisp_Object prompt, table, predicate, require_match, initial_input;
1721 Lisp_Object hist, def, inherit_input_method;
f927c5ae 1722{
86408abd
LT
1723 Lisp_Object val, histvar, histpos, position;
1724 Lisp_Object init;
1725 int pos = 0;
aed13378 1726 int count = SPECPDL_INDEX ();
0c8ee1a2
RS
1727 struct gcpro gcpro1;
1728
86408abd 1729 init = initial_input;
0c8ee1a2
RS
1730 GCPRO1 (def);
1731
f927c5ae 1732 specbind (Qminibuffer_completion_table, table);
7efd36fc 1733 specbind (Qminibuffer_completion_predicate, predicate);
f927c5ae 1734 specbind (Qminibuffer_completion_confirm,
695deb18 1735 EQ (require_match, Qt) ? Qnil : require_match);
f927c5ae 1736 last_exact_completion = Qnil;
770970cb 1737
86408abd
LT
1738 position = Qnil;
1739 if (!NILP (init))
1740 {
1741 if (CONSP (init))
1742 {
1743 position = Fcdr (init);
1744 init = Fcar (init);
1745 }
1746 CHECK_STRING (init);
1747 if (!NILP (position))
1748 {
1749 CHECK_NUMBER (position);
1750 /* Convert to distance from end of input. */
1751 pos = XINT (position) - SCHARS (init);
1752 }
1753 }
1754
7510b296 1755 if (SYMBOLP (hist))
770970cb
RS
1756 {
1757 histvar = hist;
1758 histpos = Qnil;
1759 }
1760 else
1761 {
1762 histvar = Fcar_safe (hist);
1763 histpos = Fcdr_safe (hist);
1764 }
1765 if (NILP (histvar))
1766 histvar = Qminibuffer_history;
1767 if (NILP (histpos))
5a866662 1768 XSETFASTINT (histpos, 0);
770970cb 1769
56a98455 1770 val = read_minibuf (NILP (require_match)
bb0bdf70
EZ
1771 ? (NILP (Vminibuffer_completing_file_name)
1772 ? Vminibuffer_local_completion_map
1773 : Vminibuffer_local_filename_completion_map)
1774 : (NILP (Vminibuffer_completing_file_name)
1775 ? Vminibuffer_local_must_match_map
1776 : Vminibuffer_local_must_match_filename_map),
86408abd 1777 init, prompt, make_number (pos), 0,
cee54539 1778 histvar, histpos, def, 0,
da34f64e 1779 !NILP (inherit_input_method), 0);
b9a86585 1780
d5db4077 1781 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
b9a86585
RS
1782 val = def;
1783
0c8ee1a2 1784 RETURN_UNGCPRO (unbind_to (count, val));
f927c5ae
JB
1785}
1786\f
f927c5ae 1787Lisp_Object Fminibuffer_completion_help ();
695deb18 1788Lisp_Object Fassoc_string ();
5d2ca7ae
RS
1789
1790/* Test whether TXT is an exact completion. */
695deb18
SM
1791DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1792 doc: /* Return non-nil if STRING is a valid completion.
1793Takes the same arguments as `all-completions' and `try-completion'.
1794If ALIST is a function, it is called with three arguments:
1795the values STRING, PREDICATE and `lambda'. */)
1796 (string, alist, predicate)
1797 Lisp_Object string, alist, predicate;
5d2ca7ae 1798{
adb0708c 1799 Lisp_Object regexps, tail, tem = Qnil;
85cd4372 1800 int i = 0;
5d2ca7ae 1801
695deb18
SM
1802 CHECK_STRING (string);
1803
1804 if ((CONSP (alist) && (!SYMBOLP (XCAR (alist)) || NILP (XCAR (alist))))
1805 || NILP (alist))
1806 {
1807 tem = Fassoc_string (string, alist, completion_ignore_case ? Qt : Qnil);
9f4a578d 1808 if (NILP (tem))
695deb18
SM
1809 return Qnil;
1810 }
1811 else if (VECTORP (alist))
5d2ca7ae 1812 {
695deb18
SM
1813 /* Bypass intern-soft as that loses for nil. */
1814 tem = oblookup (alist,
d5db4077
KR
1815 SDATA (string),
1816 SCHARS (string),
1817 SBYTES (string));
44472c88 1818 if (!SYMBOLP (tem))
9404446f 1819 {
695deb18
SM
1820 if (STRING_MULTIBYTE (string))
1821 string = Fstring_make_unibyte (string);
9404446f 1822 else
695deb18 1823 string = Fstring_make_multibyte (string);
9404446f 1824
adb0708c 1825 tem = oblookup (alist,
d5db4077
KR
1826 SDATA (string),
1827 SCHARS (string),
1828 SBYTES (string));
9404446f 1829 }
adb0708c
LT
1830
1831 if (completion_ignore_case && !SYMBOLP (tem))
1832 {
1833 for (i = XVECTOR (alist)->size - 1; i >= 0; i--)
1834 {
1835 tail = XVECTOR (alist)->contents[i];
1836 if (SYMBOLP (tail))
1837 while (1)
1838 {
1839 if (EQ((Fcompare_strings (string, make_number (0), Qnil,
1840 Fsymbol_name (tail),
1841 make_number (0) , Qnil, Qt)),
1842 Qt))
1843 {
1844 tem = tail;
1845 break;
1846 }
1847 if (XSYMBOL (tail)->next == 0)
1848 break;
1849 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1850 }
1851 }
1852 }
1853
1854 if (!SYMBOLP (tem))
1855 return Qnil;
5d2ca7ae 1856 }
85cd4372
SM
1857 else if (HASH_TABLE_P (alist))
1858 {
adb0708c
LT
1859 struct Lisp_Hash_Table *h = XHASH_TABLE (alist);
1860 i = hash_lookup (h, string, NULL);
85cd4372 1861 if (i >= 0)
adb0708c 1862 tem = HASH_KEY (h, i);
85cd4372 1863 else
adb0708c
LT
1864 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1865 if (!NILP (HASH_HASH (h, i)) &&
1866 EQ (Fcompare_strings (string, make_number (0), Qnil,
1867 HASH_KEY (h, i), make_number (0) , Qnil,
1868 completion_ignore_case ? Qt : Qnil),
1869 Qt))
1870 {
1871 tem = HASH_KEY (h, i);
1872 break;
1873 }
1874 if (!STRINGP (tem))
85cd4372
SM
1875 return Qnil;
1876 }
5d2ca7ae 1877 else
695deb18
SM
1878 return call3 (alist, string, predicate, Qlambda);
1879
1880 /* Reject this element if it fails to match all the regexps. */
77443789
AS
1881 if (CONSP (Vcompletion_regexp_list))
1882 {
1883 int count = SPECPDL_INDEX ();
1884 specbind (Qcase_fold_search, completion_ignore_case ? Qt : Qnil);
1885 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1886 regexps = XCDR (regexps))
1887 {
1888 if (NILP (Fstring_match (XCAR (regexps),
1889 SYMBOLP (tem) ? string : tem,
1890 Qnil)))
1891 return unbind_to (count, Qnil);
1892 }
1893 unbind_to (count, Qnil);
1894 }
695deb18
SM
1895
1896 /* Finally, check the predicate. */
1897 if (!NILP (predicate))
b0c138ce
LT
1898 {
1899 return HASH_TABLE_P (alist)
1900 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (alist), i))
1901 : call1 (predicate, tem);
1902 }
695deb18
SM
1903 else
1904 return Qt;
5d2ca7ae 1905}
f927c5ae
JB
1906
1907/* returns:
1908 * 0 no possible completion
1909 * 1 was already an exact and unique completion
1910 * 3 was already an exact completion
1911 * 4 completed to an exact completion
1912 * 5 some completion happened
1913 * 6 no completion happened
1914 */
1915int
1916do_completion ()
1917{
0c80756e 1918 Lisp_Object completion, string, tem;
f927c5ae
JB
1919 int completedp;
1920 Lisp_Object last;
1e00c2ff 1921 struct gcpro gcpro1, gcpro2;
f927c5ae 1922
b5e1e449 1923 completion = Ftry_completion (Fminibuffer_completion_contents (),
8e9968c6 1924 Vminibuffer_completion_table,
f927c5ae
JB
1925 Vminibuffer_completion_predicate);
1926 last = last_exact_completion;
1927 last_exact_completion = Qnil;
1928
4f9b95e5
KH
1929 GCPRO2 (completion, last);
1930
56a98455 1931 if (NILP (completion))
f927c5ae
JB
1932 {
1933 bitch_at_user ();
805221da 1934 temp_echo_area_glyphs (build_string (" [No match]"));
1e00c2ff 1935 UNGCPRO;
f927c5ae
JB
1936 return 0;
1937 }
1938
1939 if (EQ (completion, Qt)) /* exact and unique match */
1e00c2ff
KH
1940 {
1941 UNGCPRO;
1942 return 1;
1943 }
f927c5ae 1944
b5e1e449 1945 string = Fminibuffer_completion_contents ();
0c80756e
MB
1946
1947 /* COMPLETEDP should be true if some completion was done, which
1948 doesn't include simply changing the case of the entered string.
1949 However, for appearance, the string is rewritten if the case
1950 changes. */
1951 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qt);
01cce3e7 1952 completedp = !EQ (tem, Qt);
0c80756e 1953
7892a68b
KH
1954 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qnil);
1955 if (!EQ (tem, Qt))
0c80756e 1956 /* Rewrite the user's input. */
f927c5ae 1957 {
1011edb5
RS
1958 int prompt_end = XINT (Fminibuffer_prompt_end ());
1959 /* Some completion happened */
1960
1961 if (! NILP (Vminibuffer_completing_file_name)
d5db4077 1962 && SREF (completion, SBYTES (completion) - 1) == '/'
1011edb5
RS
1963 && PT < ZV
1964 && FETCH_CHAR (PT_BYTE) == '/')
1965 {
1966 del_range (prompt_end, PT + 1);
1967 }
1968 else
1969 del_range (prompt_end, PT);
1970
f927c5ae 1971 Finsert (1, &completion);
6e69ab8c
MB
1972
1973 if (! completedp)
1974 /* The case of the string changed, but that's all. We're not
1975 sure whether this is a unique completion or not, so try again
1976 using the real case (this shouldn't recurse again, because
1977 the next time try-completion will return either `t' or the
1978 exact string). */
1979 {
1980 UNGCPRO;
1981 return do_completion ();
1982 }
f927c5ae
JB
1983 }
1984
1985 /* It did find a match. Do we match some possibility exactly now? */
695deb18
SM
1986 tem = Ftest_completion (Fminibuffer_contents (),
1987 Vminibuffer_completion_table,
1988 Vminibuffer_completion_predicate);
56a98455 1989 if (NILP (tem))
1e00c2ff
KH
1990 {
1991 /* not an exact match */
1992 UNGCPRO;
f927c5ae
JB
1993 if (completedp)
1994 return 5;
739cc391 1995 else if (!NILP (Vcompletion_auto_help))
f927c5ae
JB
1996 Fminibuffer_completion_help ();
1997 else
805221da 1998 temp_echo_area_glyphs (build_string (" [Next char not unique]"));
f927c5ae
JB
1999 return 6;
2000 }
2001 else if (completedp)
1e00c2ff
KH
2002 {
2003 UNGCPRO;
2004 return 4;
2005 }
f927c5ae
JB
2006 /* If the last exact completion and this one were the same,
2007 it means we've already given a "Complete but not unique"
52b14ac0 2008 message and the user's hit TAB again, so now we give him help. */
f927c5ae 2009 last_exact_completion = completion;
56a98455 2010 if (!NILP (last))
f927c5ae 2011 {
b5e1e449 2012 tem = Fminibuffer_completion_contents ();
56a98455 2013 if (!NILP (Fequal (tem, last)))
f927c5ae
JB
2014 Fminibuffer_completion_help ();
2015 }
1e00c2ff 2016 UNGCPRO;
f927c5ae 2017 return 3;
f927c5ae 2018}
1e00c2ff 2019
52b14ac0
JB
2020/* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
2021
695deb18
SM
2022DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
2023 doc: /* Like `assoc' but specifically for strings.
2024Unibyte strings are converted to multibyte for comparison.
2025And case is ignored if CASE-FOLD is non-nil.
2026As opposed to `assoc', it will also match an entry consisting of a single
2027string rather than a cons cell whose car is a string. */)
2028 (key, list, case_fold)
52b14ac0 2029 register Lisp_Object key;
695deb18 2030 Lisp_Object list, case_fold;
52b14ac0
JB
2031{
2032 register Lisp_Object tail;
2033
56a98455 2034 for (tail = list; !NILP (tail); tail = Fcdr (tail))
52b14ac0
JB
2035 {
2036 register Lisp_Object elt, tem, thiscar;
2037 elt = Fcar (tail);
695deb18 2038 thiscar = CONSP (elt) ? XCAR (elt) : elt;
7510b296 2039 if (!STRINGP (thiscar))
52b14ac0 2040 continue;
9404446f
RS
2041 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
2042 key, make_number (0), Qnil,
695deb18 2043 case_fold);
9404446f
RS
2044 if (EQ (tem, Qt))
2045 return elt;
52b14ac0
JB
2046 QUIT;
2047 }
2048 return Qnil;
2049}
f927c5ae
JB
2050
2051DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
af52c32d 2052 doc: /* Complete the minibuffer contents as far as possible.
a1f17501
PJ
2053Return nil if there is no valid completion, else t.
2054If no characters can be completed, display a list of possible completions.
2055If you repeat this command after it displayed such a list,
af52c32d
MB
2056scroll the window of possible completions. */)
2057 ()
f927c5ae 2058{
2cb6da5c
RS
2059 register int i;
2060 Lisp_Object window, tem;
2061
df902b62
RS
2062 /* If the previous command was not this,
2063 mark the completion buffer obsolete. */
2064 if (! EQ (current_kboard->Vlast_command, Vthis_command))
2cb6da5c
RS
2065 Vminibuf_scroll_window = Qnil;
2066
2067 window = Vminibuf_scroll_window;
2068 /* If there's a fresh completion window with a live buffer,
2069 and this command is repeated, scroll that window. */
2070 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
2071 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
2072 {
2073 struct buffer *obuf = current_buffer;
2074
2075 Fset_buffer (XWINDOW (window)->buffer);
7d7c3861 2076 tem = Fpos_visible_in_window_p (make_number (ZV), window, Qnil);
2cb6da5c
RS
2077 if (! NILP (tem))
2078 /* If end is in view, scroll up to the beginning. */
b7df898e 2079 Fset_window_start (window, make_number (BEGV), Qnil);
2cb6da5c
RS
2080 else
2081 /* Else scroll down one screen. */
2082 Fscroll_other_window (Qnil);
2083
2084 set_buffer_internal (obuf);
2085 return Qnil;
2086 }
2087
2088 i = do_completion ();
f927c5ae
JB
2089 switch (i)
2090 {
2091 case 0:
2092 return Qnil;
2093
2094 case 1:
a1c5aa96
GM
2095 if (PT != ZV)
2096 Fgoto_char (make_number (ZV));
805221da 2097 temp_echo_area_glyphs (build_string (" [Sole completion]"));
f927c5ae
JB
2098 break;
2099
2100 case 3:
9994cc69
GM
2101 if (PT != ZV)
2102 Fgoto_char (make_number (ZV));
805221da 2103 temp_echo_area_glyphs (build_string (" [Complete, but not unique]"));
f927c5ae
JB
2104 break;
2105 }
2106
2107 return Qt;
2108}
e4c97a67
RS
2109\f
2110/* Subroutines of Fminibuffer_complete_and_exit. */
2111
2112/* This one is called by internal_condition_case to do the real work. */
2113
2114Lisp_Object
2115complete_and_exit_1 ()
2116{
2117 return make_number (do_completion ());
2118}
2119
2120/* This one is called by internal_condition_case if an error happens.
2121 Pretend the current value is an exact match. */
2122
2123Lisp_Object
2124complete_and_exit_2 (ignore)
2125 Lisp_Object ignore;
2126{
2127 return make_number (1);
2128}
f927c5ae 2129
d850b038
SM
2130EXFUN (Fexit_minibuffer, 0) NO_RETURN;
2131
f927c5ae 2132DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
94c7167b 2133 Sminibuffer_complete_and_exit, 0, 0, "",
af52c32d 2134 doc: /* If the minibuffer contents is a valid completion then exit.
a1f17501 2135Otherwise try to complete it. If completion leads to a valid completion,
af52c32d
MB
2136a repetition of this command will exit. */)
2137 ()
f927c5ae
JB
2138{
2139 register int i;
7723a3e5 2140 Lisp_Object val, tem;
f927c5ae
JB
2141
2142 /* Allow user to specify null string */
a346ec72 2143 if (XINT (Fminibuffer_prompt_end ()) == ZV)
f927c5ae
JB
2144 goto exit;
2145
36baf3b7 2146 val = Fminibuffer_contents ();
7723a3e5
KR
2147 tem = Ftest_completion (val,
2148 Vminibuffer_completion_table,
2149 Vminibuffer_completion_predicate);
2150 if (!NILP (tem))
36baf3b7
RS
2151 {
2152 if (completion_ignore_case)
2153 { /* Fixup case of the field, if necessary. */
2154 Lisp_Object compl
2155 = Ftry_completion (val,
2156 Vminibuffer_completion_table,
2157 Vminibuffer_completion_predicate);
2158 if (STRINGP (compl)
2159 /* If it weren't for this piece of paranoia, I'd replace
2160 the whole thing with a call to do_completion. */
2161 && EQ (Flength (val), Flength (compl)))
2162 {
2163 del_range (XINT (Fminibuffer_prompt_end ()), ZV);
2164 Finsert (1, &compl);
2165 }
2166 }
2167 goto exit;
2168 }
2169
e4c97a67 2170 /* Call do_completion, but ignore errors. */
8db426c7 2171 SET_PT (ZV);
e4c97a67
RS
2172 val = internal_condition_case (complete_and_exit_1, Qerror,
2173 complete_and_exit_2);
2174
2175 i = XFASTINT (val);
f927c5ae
JB
2176 switch (i)
2177 {
2178 case 1:
2179 case 3:
2180 goto exit;
2181
2182 case 4:
56a98455 2183 if (!NILP (Vminibuffer_completion_confirm))
f927c5ae 2184 {
805221da 2185 temp_echo_area_glyphs (build_string (" [Confirm]"));
f927c5ae
JB
2186 return Qnil;
2187 }
2188 else
2189 goto exit;
2190
2191 default:
2192 return Qnil;
2193 }
2194 exit:
d850b038 2195 return Fexit_minibuffer ();
f927c5ae
JB
2196 /* NOTREACHED */
2197}
2198
2199DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
94c7167b 2200 0, 0, "",
af52c32d 2201 doc: /* Complete the minibuffer contents at most a single word.
a1f17501
PJ
2202After one word is completed as much as possible, a space or hyphen
2203is added, provided that matches some possible completion.
af52c32d
MB
2204Return nil if there is no valid completion, else t. */)
2205 ()
f927c5ae 2206{
69f4ef20 2207 Lisp_Object completion, tem, tem1;
af2b7cd5 2208 register int i, i_byte;
d7be4211 2209 struct gcpro gcpro1, gcpro2;
1011edb5 2210 int prompt_end_charpos = XINT (Fminibuffer_prompt_end ());
b278606c
BF
2211
2212 /* We keep calling Fbuffer_string rather than arrange for GC to
2213 hold onto a pointer to one of the strings thus made. */
f927c5ae 2214
b5e1e449 2215 completion = Ftry_completion (Fminibuffer_completion_contents (),
f927c5ae
JB
2216 Vminibuffer_completion_table,
2217 Vminibuffer_completion_predicate);
56a98455 2218 if (NILP (completion))
f927c5ae
JB
2219 {
2220 bitch_at_user ();
805221da 2221 temp_echo_area_glyphs (build_string (" [No match]"));
f927c5ae
JB
2222 return Qnil;
2223 }
2224 if (EQ (completion, Qt))
2225 return Qnil;
2226
b278606c 2227#if 0 /* How the below code used to look, for reference. */
a346ec72 2228 tem = Fminibuffer_contents ();
d5db4077
KR
2229 b = SDATA (tem);
2230 i = ZV - 1 - SCHARS (completion);
2231 p = SDATA (completion);
f927c5ae
JB
2232 if (i > 0 ||
2233 0 <= scmp (b, p, ZV - 1))
2234 {
2235 i = 1;
2236 /* Set buffer to longest match of buffer tail and completion head. */
2237 while (0 <= scmp (b + i, p, ZV - 1 - i))
2238 i++;
2239 del_range (1, i + 1);
2240 SET_PT (ZV);
2241 }
2242#else /* Rewritten code */
2243 {
69f4ef20 2244 int buffer_nchars, completion_nchars;
f927c5ae 2245
b7826503 2246 CHECK_STRING (completion);
b5e1e449 2247 tem = Fminibuffer_completion_contents ();
d7be4211 2248 GCPRO2 (completion, tem);
719b4a40
RS
2249 /* If reading a file name,
2250 expand any $ENVVAR refs in the buffer and in TEM. */
ccf330a6 2251 if (! NILP (Vminibuffer_completing_file_name))
719b4a40
RS
2252 {
2253 Lisp_Object substituted;
2254 substituted = Fsubstitute_in_file_name (tem);
2255 if (! EQ (substituted, tem))
2256 {
2257 tem = substituted;
1011edb5
RS
2258 del_range (prompt_end_charpos, PT);
2259 Finsert (1, &tem);
719b4a40
RS
2260 }
2261 }
d5db4077
KR
2262 buffer_nchars = SCHARS (tem); /* # chars in what we completed. */
2263 completion_nchars = SCHARS (completion);
69f4ef20
RS
2264 i = buffer_nchars - completion_nchars;
2265 if (i > 0
2266 ||
2267 (tem1 = Fcompare_strings (tem, make_number (0),
2268 make_number (buffer_nchars),
2269 completion, make_number (0),
2270 make_number (buffer_nchars),
2271 completion_ignore_case ? Qt : Qnil),
2272 ! EQ (tem1, Qt)))
f927c5ae 2273 {
69f4ef20
RS
2274 int start_pos;
2275
1011edb5
RS
2276 /* Make buffer (before point) contain the longest match
2277 of TEM's tail and COMPLETION's head. */
69f4ef20
RS
2278 if (i <= 0) i = 1;
2279 start_pos= i;
2280 buffer_nchars -= i;
669c6673 2281 while (i > 0)
69f4ef20 2282 {
669c6673 2283 tem1 = Fcompare_strings (tem, make_number (start_pos), Qnil,
69f4ef20
RS
2284 completion, make_number (0),
2285 make_number (buffer_nchars),
2286 completion_ignore_case ? Qt : Qnil);
2287 start_pos++;
2288 if (EQ (tem1, Qt))
2289 break;
2290 i++;
669c6673 2291 buffer_nchars--;
69f4ef20 2292 }
4f9f637a 2293 del_range (start_pos, start_pos + buffer_nchars);
f927c5ae 2294 }
d7be4211 2295 UNGCPRO;
f927c5ae
JB
2296 }
2297#endif /* Rewritten code */
9ab4a725 2298
279cf52c 2299 {
8e9968c6 2300 int prompt_end_bytepos;
279cf52c 2301 prompt_end_bytepos = CHAR_TO_BYTE (prompt_end_charpos);
1011edb5
RS
2302 i = PT - prompt_end_charpos;
2303 i_byte = PT_BYTE - prompt_end_bytepos;
279cf52c 2304 }
f927c5ae
JB
2305
2306 /* If completion finds next char not unique,
b278606c 2307 consider adding a space or a hyphen. */
d5db4077 2308 if (i == SCHARS (completion))
f927c5ae 2309 {
b278606c 2310 GCPRO1 (completion);
b5e1e449 2311 tem = Ftry_completion (concat2 (Fminibuffer_completion_contents (),
1011edb5 2312 build_string (" ")),
f927c5ae
JB
2313 Vminibuffer_completion_table,
2314 Vminibuffer_completion_predicate);
b278606c
BF
2315 UNGCPRO;
2316
7510b296 2317 if (STRINGP (tem))
f927c5ae
JB
2318 completion = tem;
2319 else
2320 {
b278606c
BF
2321 GCPRO1 (completion);
2322 tem =
b5e1e449 2323 Ftry_completion (concat2 (Fminibuffer_completion_contents (),
1011edb5 2324 build_string ("-")),
b278606c
BF
2325 Vminibuffer_completion_table,
2326 Vminibuffer_completion_predicate);
2327 UNGCPRO;
2328
7510b296 2329 if (STRINGP (tem))
f927c5ae
JB
2330 completion = tem;
2331 }
9ab4a725 2332 }
f927c5ae
JB
2333
2334 /* Now find first word-break in the stuff found by completion.
2335 i gets index in string of where to stop completing. */
d50a3d2a
KH
2336 {
2337 int len, c;
d5db4077 2338 int bytes = SBYTES (completion);
e8157eae 2339 register const unsigned char *completion_string = SDATA (completion);
d5db4077 2340 for (; i_byte < SBYTES (completion); i_byte += len, i++)
d50a3d2a 2341 {
af2b7cd5 2342 c = STRING_CHAR_AND_LENGTH (completion_string + i_byte,
54643b2c 2343 bytes - i_byte,
d50a3d2a
KH
2344 len);
2345 if (SYNTAX (c) != Sword)
2346 {
af2b7cd5
RS
2347 i_byte += len;
2348 i++;
d50a3d2a
KH
2349 break;
2350 }
2351 }
2352 }
f927c5ae
JB
2353
2354 /* If got no characters, print help for user. */
2355
1011edb5 2356 if (i == PT - prompt_end_charpos)
f927c5ae 2357 {
739cc391 2358 if (!NILP (Vcompletion_auto_help))
f927c5ae
JB
2359 Fminibuffer_completion_help ();
2360 return Qnil;
2361 }
2362
2363 /* Otherwise insert in minibuffer the chars we got */
2364
1011edb5 2365 if (! NILP (Vminibuffer_completing_file_name)
d5db4077 2366 && SREF (completion, SBYTES (completion) - 1) == '/'
1011edb5
RS
2367 && PT < ZV
2368 && FETCH_CHAR (PT_BYTE) == '/')
2369 {
2370 del_range (prompt_end_charpos, PT + 1);
2371 }
2372 else
2373 del_range (prompt_end_charpos, PT);
2374
af2b7cd5 2375 insert_from_string (completion, 0, 0, i, i_byte, 1);
f927c5ae
JB
2376 return Qt;
2377}
2378\f
2379DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
f5fab556 2380 1, 2, 0,
af52c32d 2381 doc: /* Display the list of completions, COMPLETIONS, using `standard-output'.
a1f17501
PJ
2382Each element may be just a symbol or string
2383or may be a list of two strings to be printed as if concatenated.
b0c138ce
LT
2384If it is a list of two strings, the first is the actual completion
2385alternative, the second serves as annotation.
a1f17501
PJ
2386`standard-output' must be a buffer.
2387The actual completion alternatives, as inserted, are given `mouse-face'
2388properties of `highlight'.
2389At the end, this runs the normal hook `completion-setup-hook'.
d6ae289c
KS
2390It can find the completion buffer in `standard-output'.
2391The optional second arg COMMON-SUBSTRING is a string.
128e729d 2392It is used to put faces, `completions-first-difference' and
26e9d2cf 2393`completions-common-part' on the completion buffer. The
f5fab556 2394`completions-common-part' face is put on the common substring
b5e1e449
JL
2395specified by COMMON-SUBSTRING. If COMMON-SUBSTRING is nil
2396and the current buffer is not the minibuffer, the faces are not put.
d6ae289c 2397Internally, COMMON-SUBSTRING is bound to `completion-common-substring'
f5fab556
MY
2398during running `completion-setup-hook'. */)
2399 (completions, common_substring)
f927c5ae 2400 Lisp_Object completions;
f5fab556 2401 Lisp_Object common_substring;
f927c5ae 2402{
dae36123 2403 Lisp_Object tail, elt;
f927c5ae 2404 register int i;
2dc2b736 2405 int column = 0;
f5fab556 2406 struct gcpro gcpro1, gcpro2, gcpro3;
2dc2b736 2407 struct buffer *old = current_buffer;
681f5af4 2408 int first = 1;
486cc7fb
RS
2409
2410 /* Note that (when it matters) every variable
dae36123
RS
2411 points to a non-string that is pointed to by COMPLETIONS,
2412 except for ELT. ELT can be pointing to a string
2413 when terpri or Findent_to calls a change hook. */
2414 elt = Qnil;
f5fab556 2415 GCPRO3 (completions, elt, common_substring);
486cc7fb 2416
7510b296 2417 if (BUFFERP (Vstandard_output))
2dc2b736 2418 set_buffer_internal (XBUFFER (Vstandard_output));
f927c5ae 2419
56a98455 2420 if (NILP (completions))
cfc736bf
RS
2421 write_string ("There are no possible completions of what you have typed.",
2422 -1);
f927c5ae
JB
2423 else
2424 {
2dc2b736 2425 write_string ("Possible completions are:", -1);
a425bd03 2426 for (tail = completions, i = 0; CONSP (tail); tail = XCDR (tail), i++)
f927c5ae 2427 {
ec067ec7 2428 Lisp_Object tem, string;
681f5af4 2429 int length;
47d7d104 2430 Lisp_Object startpos, endpos;
681f5af4 2431
6bbd7a29
GM
2432 startpos = Qnil;
2433
a425bd03 2434 elt = XCAR (tail);
b0c138ce
LT
2435 if (SYMBOLP (elt))
2436 elt = SYMBOL_NAME (elt);
681f5af4
RS
2437 /* Compute the length of this element. */
2438 if (CONSP (elt))
2439 {
49db96ce 2440 tem = XCAR (elt);
b7826503 2441 CHECK_STRING (tem);
d5db4077 2442 length = SCHARS (tem);
681f5af4 2443
49db96ce 2444 tem = Fcar (XCDR (elt));
b7826503 2445 CHECK_STRING (tem);
d5db4077 2446 length += SCHARS (tem);
681f5af4
RS
2447 }
2448 else
2449 {
b7826503 2450 CHECK_STRING (elt);
d5db4077 2451 length = SCHARS (elt);
681f5af4
RS
2452 }
2453
2454 /* This does a bad job for narrower than usual windows.
2455 Sadly, the window it will appear in is not known
2456 until after the text has been made. */
2457
47d7d104
RS
2458 if (BUFFERP (Vstandard_output))
2459 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2460
681f5af4
RS
2461 /* If the previous completion was very wide,
2462 or we have two on this line already,
2463 don't put another on the same line. */
2464 if (column > 33 || first
2465 /* If this is really wide, don't put it second on a line. */
13c67cc6 2466 || (column > 0 && length > 45))
681f5af4
RS
2467 {
2468 Fterpri (Qnil);
2469 column = 0;
2470 }
2471 /* Otherwise advance to column 35. */
2472 else
2dc2b736 2473 {
7510b296 2474 if (BUFFERP (Vstandard_output))
681f5af4
RS
2475 {
2476 tem = Findent_to (make_number (35), make_number (2));
9ab4a725 2477
681f5af4
RS
2478 column = XINT (tem);
2479 }
2dc2b736
RS
2480 else
2481 {
2482 do
2483 {
2484 write_string (" ", -1);
2485 column++;
2486 }
2487 while (column < 35);
2488 }
2489 }
681f5af4 2490
47d7d104
RS
2491 if (BUFFERP (Vstandard_output))
2492 {
2493 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2494 Fset_text_properties (startpos, endpos,
2495 Qnil, Vstandard_output);
2496 }
2497
ec067ec7 2498 /* Output this element.
9404446f 2499 If necessary, convert it to unibyte or to multibyte first. */
f927c5ae 2500 if (CONSP (elt))
ec067ec7 2501 string = Fcar (elt);
f927c5ae 2502 else
ec067ec7
RS
2503 string = elt;
2504 if (NILP (current_buffer->enable_multibyte_characters)
2505 && STRING_MULTIBYTE (string))
2506 string = Fstring_make_unibyte (string);
9404446f
RS
2507 else if (!NILP (current_buffer->enable_multibyte_characters)
2508 && !STRING_MULTIBYTE (string))
2509 string = Fstring_make_multibyte (string);
5744fa7c
RS
2510
2511 if (BUFFERP (Vstandard_output))
2512 {
4b37311e 2513 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
5744fa7c
RS
2514
2515 Fprinc (string, Qnil);
2516
4b37311e 2517 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
5744fa7c 2518
4b37311e 2519 Fput_text_property (startpos, endpos,
5744fa7c
RS
2520 Qmouse_face, intern ("highlight"),
2521 Vstandard_output);
2522 }
2523 else
2524 {
2525 Fprinc (string, Qnil);
2526 }
ec067ec7
RS
2527
2528 /* Output the annotation for this element. */
2529 if (CONSP (elt))
4b37311e
RS
2530 {
2531 if (BUFFERP (Vstandard_output))
2532 {
2533 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2534
2535 Fprinc (Fcar (Fcdr (elt)), Qnil);
2536
2537 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2538
2539 Fset_text_properties (startpos, endpos, Qnil,
2540 Vstandard_output);
2541 }
2542 else
2543 {
2544 Fprinc (Fcar (Fcdr (elt)), Qnil);
2545 }
2546 }
2547
681f5af4 2548
ec067ec7 2549 /* Update COLUMN for what we have output. */
681f5af4
RS
2550 column += length;
2551
2552 /* If output is to a buffer, recompute COLUMN in a way
2553 that takes account of character widths. */
2554 if (BUFFERP (Vstandard_output))
2dc2b736 2555 {
681f5af4
RS
2556 tem = Fcurrent_column ();
2557 column = XINT (tem);
2dc2b736 2558 }
681f5af4
RS
2559
2560 first = 0;
f927c5ae
JB
2561 }
2562 }
2dc2b736 2563
7510b296 2564 if (BUFFERP (Vstandard_output))
2d7e41fe
RS
2565 set_buffer_internal (old);
2566
cfc736bf 2567 if (!NILP (Vrun_hooks))
f5fab556
MY
2568 {
2569 int count1 = SPECPDL_INDEX ();
2570
2571 specbind (intern ("completion-common-substring"), common_substring);
2572 call1 (Vrun_hooks, intern ("completion-setup-hook"));
d6ae289c 2573
f5fab556
MY
2574 unbind_to (count1, Qnil);
2575 }
2576
2577 UNGCPRO;
cfc736bf 2578
f927c5ae
JB
2579 return Qnil;
2580}
2581
d6ae289c
KS
2582
2583static Lisp_Object
2584display_completion_list_1 (list)
2585 Lisp_Object list;
2586{
b5e1e449 2587 return Fdisplay_completion_list (list, Qnil);
d6ae289c
KS
2588}
2589
f927c5ae 2590DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
94c7167b 2591 0, 0, "",
af52c32d
MB
2592 doc: /* Display a list of possible completions of the current minibuffer contents. */)
2593 ()
f927c5ae
JB
2594{
2595 Lisp_Object completions;
2596
2597 message ("Making completion list...");
b5e1e449 2598 completions = Fall_completions (Fminibuffer_completion_contents (),
f927c5ae 2599 Vminibuffer_completion_table,
89a255dc
RS
2600 Vminibuffer_completion_predicate,
2601 Qt);
39e98b38 2602 clear_message (1, 0);
f927c5ae 2603
56a98455 2604 if (NILP (completions))
f927c5ae
JB
2605 {
2606 bitch_at_user ();
805221da 2607 temp_echo_area_glyphs (build_string (" [No completions]"));
f927c5ae
JB
2608 }
2609 else
a425bd03
SM
2610 {
2611 /* Sort and remove duplicates. */
2612 Lisp_Object tmp = completions = Fsort (completions, Qstring_lessp);
2613 while (CONSP (tmp))
2614 {
2615 if (CONSP (XCDR (tmp))
2616 && !NILP (Fequal (XCAR (tmp), XCAR (XCDR (tmp)))))
2617 XSETCDR (tmp, XCDR (XCDR (tmp)));
2618 else
2619 tmp = XCDR (tmp);
2620 }
2621 internal_with_output_to_temp_buffer ("*Completions*",
2622 display_completion_list_1,
2623 completions);
2624 }
f927c5ae
JB
2625 return Qnil;
2626}
2627\f
2628DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
af52c32d
MB
2629 doc: /* Terminate minibuffer input. */)
2630 ()
f927c5ae 2631{
7510b296 2632 if (INTEGERP (last_command_char))
dab3ab3c 2633 internal_self_insert (XINT (last_command_char), 0);
f927c5ae
JB
2634 else
2635 bitch_at_user ();
2636
d850b038 2637 return Fexit_minibuffer ();
f927c5ae
JB
2638}
2639
2640DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
af52c32d
MB
2641 doc: /* Terminate this minibuffer argument. */)
2642 ()
f927c5ae 2643{
d850b038
SM
2644 /* If the command that uses this has made modifications in the minibuffer,
2645 we don't want them to cause deactivation of the mark in the original
2646 buffer.
2647 A better solution would be to make deactivate-mark buffer-local
2648 (or to turn it into a list of buffers, ...), but in the mean time,
2649 this should do the trick in most cases. */
2650 Vdeactivate_mark = Qnil;
2651 Fthrow (Qexit, Qnil);
f927c5ae
JB
2652}
2653
2654DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
af52c32d
MB
2655 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
2656 ()
f927c5ae
JB
2657{
2658 return make_number (minibuf_level);
2659}
2660
37e9a934 2661DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
af52c32d
MB
2662 doc: /* Return the prompt string of the currently-active minibuffer.
2663If no minibuffer is active, return nil. */)
2664 ()
37e9a934 2665{
4d04c1f1 2666 return Fcopy_sequence (minibuf_prompt);
37e9a934
KH
2667}
2668
f927c5ae 2669\f
805221da 2670/* Temporarily display STRING at the end of the current
2d955a50
RS
2671 minibuffer contents. This is used to display things like
2672 "[No Match]" when the user requests a completion for a prefix
2673 that has no possible completions, and other quick, unobtrusive
2674 messages. */
2675
9d815fd9 2676void
805221da
KH
2677temp_echo_area_glyphs (string)
2678 Lisp_Object string;
2d955a50
RS
2679{
2680 int osize = ZV;
a9c26562 2681 int osize_byte = ZV_BYTE;
2d955a50 2682 int opoint = PT;
a9c26562 2683 int opoint_byte = PT_BYTE;
2d955a50
RS
2684 Lisp_Object oinhibit;
2685 oinhibit = Vinhibit_quit;
2686
2687 /* Clear out any old echo-area message to make way for our new thing. */
2688 message (0);
2689
a9c26562 2690 SET_PT_BOTH (osize, osize_byte);
805221da 2691 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 0);
a9c26562 2692 SET_PT_BOTH (opoint, opoint_byte);
2d955a50
RS
2693 Vinhibit_quit = Qt;
2694 Fsit_for (make_number (2), Qnil, Qnil);
bc69581b 2695 del_range_both (osize, osize_byte, ZV, ZV_BYTE, 1);
a9c26562 2696 SET_PT_BOTH (opoint, opoint_byte);
2d955a50
RS
2697 if (!NILP (Vquit_flag))
2698 {
2699 Vquit_flag = Qnil;
2700 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
2701 }
2702 Vinhibit_quit = oinhibit;
2703}
2704
2705DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
94c7167b 2706 1, 1, 0,
af52c32d 2707 doc: /* Temporarily display STRING at the end of the minibuffer.
1d69c502 2708The text is displayed for a period controlled by `minibuffer-message-timeout',
af52c32d
MB
2709or until the next input event arrives, whichever comes first. */)
2710 (string)
2d955a50
RS
2711 Lisp_Object string;
2712{
1a9e275b 2713 CHECK_STRING (string);
805221da 2714 temp_echo_area_glyphs (string);
2d955a50
RS
2715 return Qnil;
2716}
2717\f
9d815fd9 2718void
f927c5ae
JB
2719init_minibuf_once ()
2720{
2721 Vminibuffer_list = Qnil;
2722 staticpro (&Vminibuffer_list);
2723}
2724
9d815fd9 2725void
f927c5ae
JB
2726syms_of_minibuf ()
2727{
2728 minibuf_level = 0;
4d04c1f1
KH
2729 minibuf_prompt = Qnil;
2730 staticpro (&minibuf_prompt);
2731
2732 minibuf_save_list = Qnil;
2733 staticpro (&minibuf_save_list);
f927c5ae 2734
719b4a40
RS
2735 Qread_file_name_internal = intern ("read-file-name-internal");
2736 staticpro (&Qread_file_name_internal);
2737
e5d4686b
RS
2738 Qminibuffer_default = intern ("minibuffer-default");
2739 staticpro (&Qminibuffer_default);
2740 Fset (Qminibuffer_default, Qnil);
2741
f927c5ae
JB
2742 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
2743 staticpro (&Qminibuffer_completion_table);
2744
2745 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
2746 staticpro (&Qminibuffer_completion_confirm);
2747
2748 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
2749 staticpro (&Qminibuffer_completion_predicate);
2750
1e00c2ff
KH
2751 staticpro (&last_exact_completion);
2752 last_exact_completion = Qnil;
2753
f927c5ae
JB
2754 staticpro (&last_minibuf_string);
2755 last_minibuf_string = Qnil;
2756
2757 Quser_variable_p = intern ("user-variable-p");
2758 staticpro (&Quser_variable_p);
2759
770970cb
RS
2760 Qminibuffer_history = intern ("minibuffer-history");
2761 staticpro (&Qminibuffer_history);
f927c5ae 2762
406e55df
RS
2763 Qbuffer_name_history = intern ("buffer-name-history");
2764 staticpro (&Qbuffer_name_history);
33d0a17f 2765 Fset (Qbuffer_name_history, Qnil);
406e55df 2766
5c781212
RS
2767 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
2768 staticpro (&Qminibuffer_setup_hook);
2769
177aecf9
KH
2770 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
2771 staticpro (&Qminibuffer_exit_hook);
2772
77aa8edf
RS
2773 Qhistory_length = intern ("history-length");
2774 staticpro (&Qhistory_length);
2775
cee54539
KH
2776 Qcurrent_input_method = intern ("current-input-method");
2777 staticpro (&Qcurrent_input_method);
2778
2779 Qactivate_input_method = intern ("activate-input-method");
2780 staticpro (&Qactivate_input_method);
2781
b0c138ce
LT
2782 Qcase_fold_search = intern ("case-fold-search");
2783 staticpro (&Qcase_fold_search);
2784
af52c32d
MB
2785 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2786 doc: /* If this is non-nil, `read-buffer' does its work by calling this function. */);
eebbfb01
KH
2787 Vread_buffer_function = Qnil;
2788
af52c32d
MB
2789 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2790 doc: /* Normal hook run just after entry to minibuffer. */);
5c781212
RS
2791 Vminibuffer_setup_hook = Qnil;
2792
af52c32d
MB
2793 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2794 doc: /* Normal hook run just after exit from minibuffer. */);
177aecf9
KH
2795 Vminibuffer_exit_hook = Qnil;
2796
af52c32d
MB
2797 DEFVAR_LISP ("history-length", &Vhistory_length,
2798 doc: /* *Maximum length for history lists before truncation takes place.
a1f17501
PJ
2799A number means that length; t means infinite. Truncation takes place
2800just after a new element is inserted. Setting the history-length
2801property of a history variable overrides this default. */);
77aa8edf
RS
2802 XSETFASTINT (Vhistory_length, 30);
2803
0da4d471
JL
2804 DEFVAR_BOOL ("history-delete-duplicates", &history_delete_duplicates,
2805 doc: /* *Non-nil means to delete duplicates in history.
2806If set to t when adding a new history element, all previous identical
2807elements are deleted. */);
2808 history_delete_duplicates = 0;
2809
af52c32d 2810 DEFVAR_LISP ("completion-auto-help", &Vcompletion_auto_help,
0ca23eef
EZ
2811 doc: /* *Non-nil means automatically provide help for invalid completion input.
2812Under Partial Completion mode, a non-nil, non-t value has a special meaning;
2813see the doc string of `partial-completion-mode' for more details. */);
739cc391 2814 Vcompletion_auto_help = Qt;
f927c5ae 2815
af52c32d 2816 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
75028673
JPW
2817 doc: /* Non-nil means don't consider case significant in completion.
2818
d9a03ad2
RS
2819For file-name completion, the variable `read-file-name-completion-ignore-case'
2820controls the behavior, rather than this variable. */);
f927c5ae
JB
2821 completion_ignore_case = 0;
2822
af52c32d
MB
2823 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2824 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
a1f17501 2825This variable makes a difference whenever the minibuffer window is active. */);
f927c5ae
JB
2826 enable_recursive_minibuffers = 0;
2827
af52c32d
MB
2828 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2829 doc: /* Alist or obarray used for completion in the minibuffer.
b0c138ce
LT
2830This becomes the ALIST argument to `try-completion' and `all-completions'.
2831The value can also be a list of strings or a hash table.
a1f17501
PJ
2832
2833The value may alternatively be a function, which is given three arguments:
2834 STRING, the current buffer contents;
2835 PREDICATE, the predicate for filtering possible matches;
2836 CODE, which says what kind of things to do.
2837CODE can be nil, t or `lambda'.
2838nil means to return the best completion of STRING, or nil if there is none.
2839t means to return a list of all possible completions of STRING.
2840`lambda' means to return t if STRING is a valid completion as it stands. */);
f927c5ae
JB
2841 Vminibuffer_completion_table = Qnil;
2842
af52c32d
MB
2843 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2844 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
f927c5ae
JB
2845 Vminibuffer_completion_predicate = Qnil;
2846
af52c32d 2847 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
fd771ceb 2848 doc: /* Non-nil means to demand confirmation of completion before exiting minibuffer. */);
f927c5ae
JB
2849 Vminibuffer_completion_confirm = Qnil;
2850
ccf330a6 2851 DEFVAR_LISP ("minibuffer-completing-file-name",
af52c32d
MB
2852 &Vminibuffer_completing_file_name,
2853 doc: /* Non-nil means completing file names. */);
ccf330a6
RS
2854 Vminibuffer_completing_file_name = Qnil;
2855
af52c32d
MB
2856 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2857 doc: /* Value that `help-form' takes on inside the minibuffer. */);
f927c5ae
JB
2858 Vminibuffer_help_form = Qnil;
2859
af52c32d
MB
2860 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2861 doc: /* History list symbol to add minibuffer values to.
a1f17501
PJ
2862Each string of minibuffer input, as it appears on exit from the minibuffer,
2863is added with
2864 (set minibuffer-history-variable
2865 (cons STRING (symbol-value minibuffer-history-variable))) */);
5a866662 2866 XSETFASTINT (Vminibuffer_history_variable, 0);
770970cb 2867
af52c32d
MB
2868 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2869 doc: /* Current position of redoing in the history list. */);
770970cb
RS
2870 Vminibuffer_history_position = Qnil;
2871
af52c32d
MB
2872 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2873 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
a1f17501 2874Some uses of the echo area also raise that frame (since they use it too). */);
6a9ee000
RS
2875 minibuffer_auto_raise = 0;
2876
af52c32d 2877 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
b0c138ce
LT
2878 doc: /* List of regexps that should restrict possible completions.
2879The basic completion functions only consider a completion acceptable
2880if it matches all regular expressions in this list, with
2881`case-fold-search' bound to the value of `completion-ignore-case'.
2882See Info node `(elisp)Basic Completion', for a description of these
2883functions. */);
42006772
RS
2884 Vcompletion_regexp_list = Qnil;
2885
c3421833 2886 DEFVAR_BOOL ("minibuffer-allow-text-properties",
af52c32d
MB
2887 &minibuffer_allow_text_properties,
2888 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
a1f17501
PJ
2889This also affects `read-string', but it does not affect `read-minibuffer',
2890`read-no-blanks-input', or any of the functions that do minibuffer input
2891with completion; they always discard text properties. */);
c3421833
RS
2892 minibuffer_allow_text_properties = 0;
2893
af52c32d
MB
2894 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2895 doc: /* Text properties that are added to minibuffer prompts.
a1f17501
PJ
2896These are in addition to the basic `field' property, and stickiness
2897properties. */);
cc64f5c9
MB
2898 /* We use `intern' here instead of Qread_only to avoid
2899 initialization-order problems. */
2900 Vminibuffer_prompt_properties
2901 = Fcons (intern ("read-only"), Fcons (Qt, Qnil));
2902
68313ed8 2903 defsubr (&Sset_minibuffer_window);
f927c5ae
JB
2904 defsubr (&Sread_from_minibuffer);
2905 defsubr (&Seval_minibuffer);
2906 defsubr (&Sread_minibuffer);
2907 defsubr (&Sread_string);
2908 defsubr (&Sread_command);
2909 defsubr (&Sread_variable);
2910 defsubr (&Sread_buffer);
2911 defsubr (&Sread_no_blanks_input);
2912 defsubr (&Sminibuffer_depth);
37e9a934 2913 defsubr (&Sminibuffer_prompt);
f927c5ae 2914
873ef78e 2915 defsubr (&Sminibufferp);
a346ec72
MB
2916 defsubr (&Sminibuffer_prompt_end);
2917 defsubr (&Sminibuffer_contents);
2918 defsubr (&Sminibuffer_contents_no_properties);
b5e1e449 2919 defsubr (&Sminibuffer_completion_contents);
a346ec72
MB
2920 defsubr (&Sdelete_minibuffer_contents);
2921
f927c5ae
JB
2922 defsubr (&Stry_completion);
2923 defsubr (&Sall_completions);
695deb18
SM
2924 defsubr (&Stest_completion);
2925 defsubr (&Sassoc_string);
f927c5ae
JB
2926 defsubr (&Scompleting_read);
2927 defsubr (&Sminibuffer_complete);
2928 defsubr (&Sminibuffer_complete_word);
2929 defsubr (&Sminibuffer_complete_and_exit);
2930 defsubr (&Sdisplay_completion_list);
2931 defsubr (&Sminibuffer_completion_help);
2932
2933 defsubr (&Sself_insert_and_exit);
2934 defsubr (&Sexit_minibuffer);
2935
2d955a50 2936 defsubr (&Sminibuffer_message);
f927c5ae
JB
2937}
2938
9d815fd9 2939void
f927c5ae
JB
2940keys_of_minibuf ()
2941{
2942 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
2943 "abort-recursive-edit");
2944 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
2945 "exit-minibuffer");
2946 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
2947 "exit-minibuffer");
2948
f927c5ae
JB
2949 initial_define_key (Vminibuffer_local_ns_map, ' ',
2950 "exit-minibuffer");
2951 initial_define_key (Vminibuffer_local_ns_map, '\t',
2952 "exit-minibuffer");
2953 initial_define_key (Vminibuffer_local_ns_map, '?',
2954 "self-insert-and-exit");
2955
f927c5ae
JB
2956 initial_define_key (Vminibuffer_local_completion_map, '\t',
2957 "minibuffer-complete");
2958 initial_define_key (Vminibuffer_local_completion_map, ' ',
2959 "minibuffer-complete-word");
2960 initial_define_key (Vminibuffer_local_completion_map, '?',
2961 "minibuffer-completion-help");
2962
da8ec671
SM
2963 Fdefine_key (Vminibuffer_local_filename_completion_map,
2964 build_string (" "), Qnil);
bb0bdf70 2965
f927c5ae
JB
2966 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
2967 "minibuffer-complete-and-exit");
2968 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
2969 "minibuffer-complete-and-exit");
bb0bdf70 2970
5203cd03
JL
2971 Fdefine_key (Vminibuffer_local_must_match_filename_map,
2972 build_string (" "), Qnil);
f927c5ae 2973}
ab5796a9
MB
2974
2975/* arch-tag: 8f69b601-fba3-484c-a6dd-ceaee54a7a73
2976 (do not change this comment) */