(perform-replace): When matching lookahead, use markers rather than
[bpt/emacs.git] / src / minibuf.c
CommitLineData
f927c5ae 1/* Minibuffer input and completion.
68313ed8 2 Copyright (C) 1985, 1986, 93, 94, 95, 1996 Free Software Foundation, Inc.
f927c5ae
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
ffd56f97 8the Free Software Foundation; either version 2, or (at your option)
f927c5ae
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
f927c5ae
JB
20
21
18160b98 22#include <config.h>
f927c5ae
JB
23#include "lisp.h"
24#include "commands.h"
25#include "buffer.h"
d50a3d2a 26#include "charset.h"
f927c5ae 27#include "dispextern.h"
ff11dfa1 28#include "frame.h"
f927c5ae
JB
29#include "window.h"
30#include "syntax.h"
5221fd63 31#include "keyboard.h"
f927c5ae
JB
32
33#define min(a, b) ((a) < (b) ? (a) : (b))
34
a4e71d81
KH
35extern int quit_char;
36
f927c5ae 37/* List of buffers for use as minibuffers.
4d04c1f1
KH
38 The first element of the list is used for the outermost minibuffer
39 invocation, the next element is used for a recursive minibuffer
40 invocation, etc. The list is extended at the end as deeper
41 minibuffer recursions are encountered. */
f927c5ae
JB
42Lisp_Object Vminibuffer_list;
43
4d04c1f1
KH
44/* Data to remember during recursive minibuffer invocations */
45Lisp_Object minibuf_save_list;
f927c5ae
JB
46
47/* Depth in minibuffer invocations. */
48int minibuf_level;
49
77aa8edf 50/* Nonzero means display completion help for invalid input. */
f927c5ae
JB
51int auto_help;
52
77aa8edf
RS
53/* The maximum length of a minibuffer history. */
54Lisp_Object Qhistory_length, Vhistory_length;
55
b278606c 56/* Fread_minibuffer leaves the input here as a string. */
f927c5ae
JB
57Lisp_Object last_minibuf_string;
58
59/* Nonzero means let functions called when within a minibuffer
60 invoke recursive minibuffers (to read arguments, or whatever) */
61int enable_recursive_minibuffers;
62
63/* help-form is bound to this while in the minibuffer. */
64
65Lisp_Object Vminibuffer_help_form;
66
770970cb
RS
67/* Variable which is the history list to add minibuffer values to. */
68
69Lisp_Object Vminibuffer_history_variable;
70
71/* Current position in the history list (adjusted by M-n and M-p). */
72
73Lisp_Object Vminibuffer_history_position;
74
75Lisp_Object Qminibuffer_history;
76
719b4a40
RS
77Lisp_Object Qread_file_name_internal;
78
177aecf9 79/* Normal hooks for entry to and exit from minibuffer. */
5c781212
RS
80
81Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
177aecf9 82Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
5c781212 83
f927c5ae
JB
84/* Nonzero means completion ignores case. */
85
86int completion_ignore_case;
87
42006772
RS
88/* List of regexps that should restrict possible completions. */
89
90Lisp_Object Vcompletion_regexp_list;
91
6a9ee000
RS
92/* Nonzero means raise the minibuffer frame when the minibuffer
93 is entered. */
94
95int minibuffer_auto_raise;
96
f927c5ae
JB
97/* If last completion attempt reported "Complete but not unique"
98 then this is the string completed then; otherwise this is nil. */
99
100static Lisp_Object last_exact_completion;
101
102Lisp_Object Quser_variable_p;
2cb6da5c 103
e5d4686b
RS
104Lisp_Object Qminibuffer_default;
105
2cb6da5c
RS
106/* Non-nil means it is the window for C-M-v to scroll
107 when the minibuffer is selected. */
108extern Lisp_Object Vminibuf_scroll_window;
30e13e56
RS
109
110extern Lisp_Object Voverriding_local_map;
f927c5ae 111\f
89fdc4a0
KH
112/* Put minibuf on currently selected frame's minibuffer.
113 We do this whenever the user starts a new minibuffer
114 or when a minibuffer exits. */
115
116void
117choose_minibuf_frame ()
118{
119 if (selected_frame != 0
120 && !EQ (minibuf_window, selected_frame->minibuffer_window))
121 {
122 /* I don't think that any frames may validly have a null minibuffer
123 window anymore. */
124 if (NILP (selected_frame->minibuffer_window))
125 abort ();
126
127 Fset_window_buffer (selected_frame->minibuffer_window,
128 XWINDOW (minibuf_window)->buffer);
129 minibuf_window = selected_frame->minibuffer_window;
130 }
914860c2
RS
131
132 /* Make sure no other frame has a minibuffer as its selected window,
133 because the text would not be displayed in it, and that would be
07d402c8
RS
134 confusing. Only allow the selected frame to do this,
135 and that only if the minibuffer is active. */
914860c2
RS
136 {
137 Lisp_Object tail, frame;
138
139 FOR_EACH_FRAME (tail, frame)
07d402c8
RS
140 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
141 && !(XFRAME (frame) == selected_frame
142 && minibuf_level > 0))
914860c2
RS
143 Fset_frame_selected_window (frame, Fframe_first_window (frame));
144 }
89fdc4a0 145}
68313ed8
RS
146
147DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
148 Sset_minibuffer_window, 1, 1, 0,
149 "Specify which minibuffer window to use for the minibuffer.\n\
150This effects where the minibuffer is displayed if you put text in it\n\
151without invoking the usual minibuffer commands.")
152 (window)
153 Lisp_Object window;
154{
155 CHECK_WINDOW (window, 1);
156 if (! MINI_WINDOW_P (XWINDOW (window)))
157 error ("Window is not a minibuffer window");
158
159 minibuf_window = window;
160
161 return window;
162}
163
89fdc4a0 164\f
f927c5ae
JB
165/* Actual minibuffer invocation. */
166
e5d4686b 167static void read_minibuf_unwind ();
f927c5ae 168Lisp_Object get_minibuffer ();
e5d4686b 169static Lisp_Object read_minibuf ();
f927c5ae 170
770970cb 171/* Read from the minibuffer using keymap MAP, initial contents INITIAL
85b5fe07 172 (a string), putting point minus BACKUP_N chars from the end of INITIAL,
770970cb 173 prompting with PROMPT (a string), using history list HISTVAR
85b5fe07 174 with initial position HISTPOS. (BACKUP_N should be <= 0.)
770970cb
RS
175
176 Normally return the result as a string (the text that was read),
3ab14176 177 but if EXPFLAG is nonzero, read it and return the object read.
b278606c
BF
178 If HISTVAR is given, save the value read on that history only if it doesn't
179 match the front of that history list exactly. The value is pushed onto
e5d4686b 180 the list as the string that was read.
770970cb 181
e5d4686b
RS
182 DEFALT specifies te default value for the sake of history commands. */
183
184static Lisp_Object
185read_minibuf (map, initial, prompt, backup_n, expflag,
186 histvar, histpos, defalt)
f927c5ae
JB
187 Lisp_Object map;
188 Lisp_Object initial;
189 Lisp_Object prompt;
5061d9c3 190 Lisp_Object backup_n;
f927c5ae 191 int expflag;
770970cb
RS
192 Lisp_Object histvar;
193 Lisp_Object histpos;
e5d4686b 194 Lisp_Object defalt;
f927c5ae 195{
00a34088 196 Lisp_Object val;
f927c5ae 197 int count = specpdl_ptr - specpdl;
4f69d8f6 198 Lisp_Object mini_frame, ambient_dir, minibuffer;
57ceaa8a 199 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
00a34088 200
e5d4686b
RS
201 specbind (Qminibuffer_default, defalt);
202
08f7d623 203 single_kboard_state ();
718d3251 204
00a34088 205 val = Qnil;
57ceaa8a
RS
206 ambient_dir = current_buffer->directory;
207
00a34088
RS
208 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
209 store them away before we can GC. Don't need to protect
210 BACKUP_N because we use the value only if it is an integer. */
57ceaa8a 211 GCPRO4 (map, initial, val, ambient_dir);
f927c5ae 212
7510b296 213 if (!STRINGP (prompt))
f927c5ae
JB
214 prompt = build_string ("");
215
f927c5ae 216 if (!enable_recursive_minibuffers
be15a518
RS
217 && minibuf_level > 0)
218 {
219 if (EQ (selected_window, minibuf_window))
220 error ("Command attempted to use minibuffer while in minibuffer");
221 else
222 /* If we're in another window, cancel the minibuffer that's active. */
223 Fthrow (Qexit,
224 build_string ("Command attempted to use minibuffer while in minibuffer"));
225 }
f927c5ae 226
748dc60a 227 /* Choose the minibuffer window and frame, and take action on them. */
f927c5ae 228
c5b6b680
RS
229 choose_minibuf_frame ();
230
f927c5ae 231 record_unwind_protect (Fset_window_configuration,
b2b2c677
JB
232 Fcurrent_window_configuration (Qnil));
233
ff11dfa1
JB
234 /* If the minibuffer window is on a different frame, save that
235 frame's configuration too. */
75f00e72 236 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
ff11dfa1 237 if (XFRAME (mini_frame) != selected_frame)
5061d9c3
RS
238 record_unwind_protect (Fset_window_configuration,
239 Fcurrent_window_configuration (mini_frame));
5563e8e8
KH
240
241 /* If the minibuffer is on an iconified or invisible frame,
242 make it visible now. */
243 Fmake_frame_visible (mini_frame);
244
6a9ee000
RS
245 if (minibuffer_auto_raise)
246 Fraise_frame (mini_frame);
f927c5ae 247
748dc60a
RS
248 /* We have to do this after saving the window configuration
249 since that is what restores the current buffer. */
250
251 /* Arrange to restore a number of minibuffer-related variables.
252 We could bind each variable separately, but that would use lots of
253 specpdl slots. */
254 minibuf_save_list
255 = Fcons (Voverriding_local_map,
256 Fcons (minibuf_window, minibuf_save_list));
257 minibuf_save_list
258 = Fcons (minibuf_prompt,
259 Fcons (make_number (minibuf_prompt_width),
260 Fcons (Vhelp_form,
261 Fcons (Vcurrent_prefix_arg,
262 Fcons (Vminibuffer_history_position,
263 Fcons (Vminibuffer_history_variable,
264 minibuf_save_list))))));
265
266 record_unwind_protect (read_minibuf_unwind, Qnil);
267 minibuf_level++;
268
269 /* Now that we can restore all those variables, start changing them. */
270
271 minibuf_prompt_width = 0; /* xdisp.c puts in the right value. */
272 minibuf_prompt = Fcopy_sequence (prompt);
273 Vminibuffer_history_position = histpos;
274 Vminibuffer_history_variable = histvar;
275 Vhelp_form = Vminibuffer_help_form;
276
277 /* Switch to the minibuffer. */
278
4f69d8f6
RS
279 minibuffer = get_minibuffer (minibuf_level);
280 Fset_buffer (minibuffer);
64a3a3c0
JB
281
282 /* The current buffer's default directory is usually the right thing
283 for our minibuffer here. However, if you're typing a command at
284 a minibuffer-only frame when minibuf_level is zero, then buf IS
285 the current_buffer, so reset_buffer leaves buf's default
286 directory unchanged. This is a bummer when you've just started
287 up Emacs and buf's default directory is Qnil. Here's a hack; can
288 you think of something better to do? Find another buffer with a
289 better directory, and use that one instead. */
748dc60a
RS
290 if (STRINGP (ambient_dir))
291 current_buffer->directory = ambient_dir;
64a3a3c0
JB
292 else
293 {
294 Lisp_Object buf_list;
295
296 for (buf_list = Vbuffer_alist;
297 CONSP (buf_list);
298 buf_list = XCONS (buf_list)->cdr)
299 {
1e62748e 300 Lisp_Object other_buf;
64a3a3c0 301
1e62748e 302 other_buf = XCONS (XCONS (buf_list)->car)->cdr;
7510b296 303 if (STRINGP (XBUFFER (other_buf)->directory))
64a3a3c0
JB
304 {
305 current_buffer->directory = XBUFFER (other_buf)->directory;
306 break;
307 }
308 }
309 }
310
0abbff13
KH
311 if (XFRAME (mini_frame) != selected_frame)
312 Fredirect_frame_focus (Fselected_frame (), mini_frame);
43bad991 313
f927c5ae
JB
314 Vminibuf_scroll_window = selected_window;
315 Fset_window_buffer (minibuf_window, Fcurrent_buffer ());
f927c5ae 316 Fselect_window (minibuf_window);
5a866662 317 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
f927c5ae 318
748dc60a
RS
319 Fmake_local_variable (Qprint_escape_newlines);
320 print_escape_newlines = 1;
321
322 /* Erase the buffer. */
59115a22
RS
323 {
324 int count1 = specpdl_ptr - specpdl;
325 specbind (Qinhibit_read_only, Qt);
326 Ferase_buffer ();
327 unbind_to (count1, Qnil);
328 }
329
748dc60a 330 /* Put in the initial input. */
56a98455 331 if (!NILP (initial))
f927c5ae
JB
332 {
333 Finsert (1, &initial);
7510b296 334 if (!NILP (backup_n) && INTEGERP (backup_n))
d50a3d2a 335 Fgoto_char (make_number (PT + XFASTINT (backup_n)));
f927c5ae
JB
336 }
337
f927c5ae 338 echo_area_glyphs = 0;
73168c8b
RS
339 /* This is in case the minibuffer-setup-hook calls Fsit_for. */
340 previous_echo_glyphs = 0;
f927c5ae 341
f927c5ae
JB
342 current_buffer->keymap = map;
343
5c781212
RS
344 /* Run our hook, but not if it is empty.
345 (run-hooks would do nothing if it is empty,
748dc60a 346 but it's important to save time here in the usual case). */
92d3b06e
RS
347 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
348 && !NILP (Vrun_hooks))
5c781212
RS
349 call1 (Vrun_hooks, Qminibuffer_setup_hook);
350
f927c5ae
JB
351/* ??? MCC did redraw_screen here if switching screens. */
352 recursive_edit_1 ();
353
354 /* If cursor is on the minibuffer line,
355 show the user we have exited by putting it in column 0. */
ff11dfa1 356 if ((FRAME_CURSOR_Y (selected_frame)
f927c5ae
JB
357 >= XFASTINT (XWINDOW (minibuf_window)->top))
358 && !noninteractive)
359 {
85462999
RS
360 FRAME_CURSOR_X (selected_frame)
361 = FRAME_LEFT_SCROLL_BAR_WIDTH (selected_frame);
ff11dfa1 362 update_frame (selected_frame, 1, 1);
f927c5ae
JB
363 }
364
365 /* Make minibuffer contents into a string */
4f69d8f6 366 Fset_buffer (minibuffer);
5bb8cce1
KH
367 val = make_buffer_string (1, Z, 1);
368#if 0 /* make_buffer_string should handle the gap. */
f927c5ae 369 bcopy (GAP_END_ADDR, XSTRING (val)->data + GPT - BEG, Z - GPT);
5bb8cce1 370#endif
770970cb 371
b278606c
BF
372 /* VAL is the string of minibuffer text. */
373 last_minibuf_string = val;
374
3ab14176 375 /* Add the value to the appropriate history list unless it is empty. */
9f6131cf 376 if (XSTRING (val)->size != 0
3ab14176
KH
377 && SYMBOLP (Vminibuffer_history_variable)
378 && ! EQ (XSYMBOL (Vminibuffer_history_variable)->value, Qunbound))
379 {
380 /* If the caller wanted to save the value read on a history list,
381 then do so if the value is not already the front of the list. */
382 Lisp_Object histval;
383 histval = Fsymbol_value (Vminibuffer_history_variable);
384
385 /* The value of the history variable must be a cons or nil. Other
386 values are unacceptable. We silently ignore these values. */
387 if (NILP (histval)
9f6131cf
RS
388 || (CONSP (histval)
389 && NILP (Fequal (last_minibuf_string, Fcar (histval)))))
77aa8edf
RS
390 {
391 Lisp_Object length;
392
393 histval = Fcons (last_minibuf_string, histval);
394 Fset (Vminibuffer_history_variable, histval);
395
396 /* Truncate if requested. */
397 length = Fget (Vminibuffer_history_variable, Qhistory_length);
398 if (NILP (length)) length = Vhistory_length;
e5d4686b
RS
399 if (INTEGERP (length))
400 {
401 if (XINT (length) <= 0)
402 Fset (Vminibuffer_history_variable, Qnil);
403 else
404 {
405 Lisp_Object temp;
406
407 temp = Fnthcdr (Fsub1 (length), histval);
408 if (CONSP (temp)) Fsetcdr (temp, Qnil);
409 }
410 }
77aa8edf 411 }
9f6131cf
RS
412 }
413
414 /* If Lisp form desired instead of string, parse it. */
415 if (expflag)
416 {
417 Lisp_Object expr_and_pos;
418 unsigned char *p;
419
420 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
421 /* Ignore trailing whitespace; any other trailing junk is an error. */
422 for (p = XSTRING (val)->data + XINT (Fcdr (expr_and_pos)); *p; p++)
423 if (*p != ' ' && *p != '\t' && *p != '\n')
424 error ("Trailing garbage following expression");
425 val = Fcar (expr_and_pos);
3ab14176
KH
426 }
427
00a34088
RS
428 /* The appropriate frame will get selected
429 in set-window-configuration. */
430 RETURN_UNGCPRO (unbind_to (count, val));
f927c5ae
JB
431}
432
433/* Return a buffer to be used as the minibuffer at depth `depth'.
434 depth = 0 is the lowest allowed argument, and that is the value
435 used for nonrecursive minibuffer invocations */
436
437Lisp_Object
438get_minibuffer (depth)
439 int depth;
440{
441 Lisp_Object tail, num, buf;
9f6c23bc 442 char name[24];
f927c5ae
JB
443 extern Lisp_Object nconc2 ();
444
5a866662 445 XSETFASTINT (num, depth);
f927c5ae 446 tail = Fnthcdr (num, Vminibuffer_list);
56a98455 447 if (NILP (tail))
f927c5ae
JB
448 {
449 tail = Fcons (Qnil, Qnil);
450 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
451 }
452 buf = Fcar (tail);
56a98455 453 if (NILP (buf) || NILP (XBUFFER (buf)->name))
f927c5ae
JB
454 {
455 sprintf (name, " *Minibuf-%d*", depth);
456 buf = Fget_buffer_create (build_string (name));
5d6533f1
JB
457
458 /* Although the buffer's name starts with a space, undo should be
459 enabled in it. */
460 Fbuffer_enable_undo (buf);
461
f927c5ae
JB
462 XCONS (tail)->car = buf;
463 }
464 else
5956f71d 465 {
6b3faad8
RS
466 int count = specpdl_ptr - specpdl;
467
5956f71d 468 reset_buffer (XBUFFER (buf));
6b3faad8
RS
469 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
470 Fset_buffer (buf);
471 Fkill_all_local_variables ();
472 unbind_to (count, Qnil);
5956f71d 473 }
64a3a3c0 474
f927c5ae
JB
475 return buf;
476}
477
478/* This function is called on exiting minibuffer, whether normally or not,
479 and it restores the current window, buffer, etc. */
480
e5d4686b 481static void
43bad991
JB
482read_minibuf_unwind (data)
483 Lisp_Object data;
f927c5ae 484{
c24e1160 485 Lisp_Object old_deactivate_mark;
59115a22 486 Lisp_Object window;
c24e1160 487
0a1dd1c5
RS
488 /* We are exiting the minibuffer one way or the other,
489 so run the hook. */
490 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
491 && !NILP (Vrun_hooks))
060ffec1 492 safe_run_hooks (Qminibuffer_exit_hook);
0a1dd1c5 493
f927c5ae 494 /* If this was a recursive minibuffer,
59115a22 495 tie the minibuffer window back to the outer level minibuffer buffer. */
f927c5ae 496 minibuf_level--;
f927c5ae 497
59115a22
RS
498 window = minibuf_window;
499 /* To keep things predictable, in case it matters, let's be in the minibuffer
500 when we reset the relevant variables. */
501 Fset_buffer (XWINDOW (window)->buffer);
502
503 /* Restore prompt, etc, from outer minibuffer level. */
4d04c1f1
KH
504 minibuf_prompt = Fcar (minibuf_save_list);
505 minibuf_save_list = Fcdr (minibuf_save_list);
506 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
507 minibuf_save_list = Fcdr (minibuf_save_list);
508 Vhelp_form = Fcar (minibuf_save_list);
509 minibuf_save_list = Fcdr (minibuf_save_list);
ee9e37ab 510 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
4d04c1f1
KH
511 minibuf_save_list = Fcdr (minibuf_save_list);
512 Vminibuffer_history_position = Fcar (minibuf_save_list);
513 minibuf_save_list = Fcdr (minibuf_save_list);
514 Vminibuffer_history_variable = Fcar (minibuf_save_list);
515 minibuf_save_list = Fcdr (minibuf_save_list);
30e13e56 516 Voverriding_local_map = Fcar (minibuf_save_list);
c5b6b680 517 minibuf_save_list = Fcdr (minibuf_save_list);
914860c2
RS
518#if 0
519 temp = Fcar (minibuf_save_list);
520 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
521 minibuf_window = temp;
522#endif
30e13e56 523 minibuf_save_list = Fcdr (minibuf_save_list);
59115a22
RS
524
525 /* Erase the minibuffer we were using at this level. */
526 {
527 int count = specpdl_ptr - specpdl;
528 /* Prevent error in erase-buffer. */
529 specbind (Qinhibit_read_only, Qt);
530 old_deactivate_mark = Vdeactivate_mark;
531 Ferase_buffer ();
532 Vdeactivate_mark = old_deactivate_mark;
533 unbind_to (count, Qnil);
534 }
535
914860c2
RS
536 /* Make the minibuffer follow the selected frame
537 (in case we are exiting a recursive minibuffer). */
538 choose_minibuf_frame ();
539
59115a22
RS
540 /* Make sure minibuffer window is erased, not ignored. */
541 windows_or_buffers_changed++;
542 XSETFASTINT (XWINDOW (window)->last_modified, 0);
193e4518 543 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
f927c5ae
JB
544}
545\f
b9d721de
JB
546
547/* This comment supplies the doc string for read-from-minibuffer,
548 for make-docfile to see. We cannot put this in the real DEFUN
549 due to limits in the Unix cpp.
550
1d8d92f4 551DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 6, 0,
f927c5ae
JB
552 "Read a string from the minibuffer, prompting with string PROMPT.\n\
553If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
554 to be inserted into the minibuffer before reading input.\n\
770970cb 555 If INITIAL-CONTENTS is (STRING . POSITION), the initial input\n\
18fef111 556 is STRING, but point is placed at position POSITION in the minibuffer.\n\
f927c5ae
JB
557Third arg KEYMAP is a keymap to use whilst reading;\n\
558 if omitted or nil, the default is `minibuffer-local-map'.\n\
559If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
560 and return that object:\n\
561 in other words, do `(car (read-from-string INPUT-STRING))'\n\
770970cb
RS
562Fifth arg HIST, if non-nil, specifies a history list\n\
563 and optionally the initial position in the list.\n\
564 It can be a symbol, which is the history list variable to use,\n\
565 or it can be a cons cell (HISTVAR . HISTPOS).\n\
566 In that case, HISTVAR is the history list variable to use,\n\
567 and HISTPOS is the initial position (the position in the list\n\
568 which INITIAL-CONTENTS corresponds to).\n\
e5d4686b
RS
569 Positions are counted starting from 1 at the beginning of the list.\n\
570Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is used\n\
571 for history commands, and as the value to return if the user enters\n\
572 the empty string.\n\
b9d721de
JB
573*/
574
e5d4686b 575DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 6, 0,
b9d721de 576 0 /* See immediately above */)
e5d4686b
RS
577 (prompt, initial_contents, keymap, read, hist, default_value)
578 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
f927c5ae
JB
579{
580 int pos = 0;
e5d4686b 581 Lisp_Object histvar, histpos, position, val;
1d8d92f4
RS
582 struct gcpro gcpro1;
583
770970cb 584 position = Qnil;
f927c5ae
JB
585
586 CHECK_STRING (prompt, 0);
68e5a8a2 587 if (!NILP (initial_contents))
f927c5ae 588 {
7510b296 589 if (CONSP (initial_contents))
770970cb 590 {
68e5a8a2
RS
591 position = Fcdr (initial_contents);
592 initial_contents = Fcar (initial_contents);
770970cb 593 }
68e5a8a2 594 CHECK_STRING (initial_contents, 1);
56a98455 595 if (!NILP (position))
f927c5ae
JB
596 {
597 CHECK_NUMBER (position, 0);
598 /* Convert to distance from end of input. */
18fef111
RS
599 if (XINT (position) < 1)
600 /* A number too small means the beginning of the string. */
601 pos = - XSTRING (initial_contents)->size;
602 else
603 pos = XINT (position) - 1 - XSTRING (initial_contents)->size;
f927c5ae
JB
604 }
605 }
606
56a98455 607 if (NILP (keymap))
f927c5ae
JB
608 keymap = Vminibuffer_local_map;
609 else
610 keymap = get_keymap (keymap,2);
770970cb 611
7510b296 612 if (SYMBOLP (hist))
770970cb
RS
613 {
614 histvar = hist;
615 histpos = Qnil;
616 }
617 else
618 {
619 histvar = Fcar_safe (hist);
620 histpos = Fcdr_safe (hist);
621 }
622 if (NILP (histvar))
623 histvar = Qminibuffer_history;
624 if (NILP (histpos))
5a866662 625 XSETFASTINT (histpos, 0);
770970cb 626
1d8d92f4 627 GCPRO1 (default_value);
e5d4686b
RS
628 val = read_minibuf (keymap, initial_contents, prompt,
629 make_number (pos), !NILP (read),
630 histvar, histpos, default_value);
631 if (STRINGP (val) && XSTRING (val)->size == 0 && ! NILP (default_value))
632 val = default_value;
1d8d92f4 633 UNGCPRO;
e5d4686b 634 return val;
f927c5ae
JB
635}
636
637DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
638 "Return a Lisp object read using the minibuffer.\n\
639Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
640is a string to insert in the minibuffer before reading.")
641 (prompt, initial_contents)
642 Lisp_Object prompt, initial_contents;
643{
644 CHECK_STRING (prompt, 0);
56a98455 645 if (!NILP (initial_contents))
a1b4b084 646 CHECK_STRING (initial_contents, 1);
770970cb 647 return read_minibuf (Vminibuffer_local_map, initial_contents,
e5d4686b
RS
648 prompt, Qnil, 1, Qminibuffer_history,
649 make_number (0), Qnil);
f927c5ae
JB
650}
651
652DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
653 "Return value of Lisp expression read using the minibuffer.\n\
654Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
655is a string to insert in the minibuffer before reading.")
656 (prompt, initial_contents)
657 Lisp_Object prompt, initial_contents;
658{
659 return Feval (Fread_minibuffer (prompt, initial_contents));
660}
661
662/* Functions that use the minibuffer to read various things. */
663
e5d4686b 664DEFUN ("read-string", Fread_string, Sread_string, 1, 4, 0,
f927c5ae 665 "Read a string from the minibuffer, prompting with string PROMPT.\n\
80896ab4
RS
666If non-nil, second arg INITIAL-INPUT is a string to insert before reading.\n\
667The third arg HISTORY, if non-nil, specifies a history list\n\
668 and optionally the initial position in the list.\n\
669See `read-from-minibuffer' for details of HISTORY argument.")
e5d4686b
RS
670 (prompt, initial_input, history, default_value)
671 Lisp_Object prompt, initial_input, history, default_value;
f927c5ae 672{
e5d4686b
RS
673 return Fread_from_minibuffer (prompt, initial_input, Qnil,
674 Qnil, history, default_value);
f927c5ae
JB
675}
676
b278606c 677DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 2, 0,
f927c5ae
JB
678 "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\
679Prompt with PROMPT, and provide INIT as an initial value of the input string.")
680 (prompt, init)
681 Lisp_Object prompt, init;
682{
683 CHECK_STRING (prompt, 0);
56a98455 684 if (! NILP (init))
f927c5ae
JB
685 CHECK_STRING (init, 1);
686
e5d4686b
RS
687 return read_minibuf (Vminibuffer_local_ns_map, init, prompt, Qnil,
688 0, Qminibuffer_history, make_number (0), Qnil);
f927c5ae
JB
689}
690
e5d4686b
RS
691DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
692 "Read the name of a command and return as a symbol.\n\
693Prompts with PROMPT. By default, return DEFAULT-VALUE.")
694 (prompt, default_value)
695 Lisp_Object prompt, default_value;
f927c5ae 696{
e5d4686b
RS
697 return Fintern (Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
698 Qnil, Qnil, default_value),
f927c5ae
JB
699 Qnil);
700}
701
702#ifdef NOTDEF
703DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
704 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
705Prompts with PROMPT.")
706 (prompt)
707 Lisp_Object prompt;
708{
e5d4686b 709 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil),
f927c5ae
JB
710 Qnil);
711}
712#endif /* NOTDEF */
713
e5d4686b
RS
714DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
715 "Read the name of a user variable and return it as a symbol.\n\
716Prompts with PROMPT. By default, return DEFAULT-VALUE.\n\
f927c5ae 717A user variable is one whose documentation starts with a `*' character.")
e5d4686b
RS
718 (prompt, default_value)
719 Lisp_Object prompt, default_value;
f927c5ae
JB
720{
721 return Fintern (Fcompleting_read (prompt, Vobarray,
e5d4686b
RS
722 Quser_variable_p, Qt,
723 Qnil, Qnil, default_value),
f927c5ae
JB
724 Qnil);
725}
726
727DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
728 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
729Prompts with PROMPT.\n\
e5d4686b 730Optional second arg DEF is value to return if user enters an empty line.\n\
f927c5ae
JB
731If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
732 (prompt, def, require_match)
733 Lisp_Object prompt, def, require_match;
734{
735 Lisp_Object tem;
736 Lisp_Object args[3];
f927c5ae 737
7510b296 738 if (BUFFERP (def))
f927c5ae 739 def = XBUFFER (def)->name;
56a98455 740 if (!NILP (def))
f927c5ae
JB
741 {
742 args[0] = build_string ("%s(default %s) ");
743 args[1] = prompt;
744 args[2] = def;
745 prompt = Fformat (3, args);
746 }
e5d4686b
RS
747 return Fcompleting_read (prompt, Vbuffer_alist, Qnil,
748 require_match, Qnil, Qnil, def);
f927c5ae
JB
749}
750\f
751DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
752 "Return common substring of all completions of STRING in ALIST.\n\
753Each car of each element of ALIST is tested to see if it begins with STRING.\n\
754All that match are compared together; the longest initial sequence\n\
755common to all matches is returned as a string.\n\
756If there is no match at all, nil is returned.\n\
757For an exact match, t is returned.\n\
758\n\
759ALIST can be an obarray instead of an alist.\n\
760Then the print names of all symbols in the obarray are the possible matches.\n\
761\n\
762ALIST can also be a function to do the completion itself.\n\
763It receives three arguments: the values STRING, PREDICATE and nil.\n\
764Whatever it returns becomes the value of `try-completion'.\n\
765\n\
766If optional third argument PREDICATE is non-nil,\n\
767it is used to test each possible match.\n\
768The match is a candidate only if PREDICATE returns non-nil.\n\
89a255dc
RS
769The argument given to PREDICATE is the alist element\n\
770or the symbol from the obarray.")
7efd36fc
EN
771 (string, alist, predicate)
772 Lisp_Object string, alist, predicate;
f927c5ae
JB
773{
774 Lisp_Object bestmatch, tail, elt, eltstring;
775 int bestmatchsize;
776 int compare, matchsize;
56a98455 777 int list = CONSP (alist) || NILP (alist);
f927c5ae
JB
778 int index, obsize;
779 int matchcount = 0;
780 Lisp_Object bucket, zero, end, tem;
781 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
782
783 CHECK_STRING (string, 0);
7510b296 784 if (!list && !VECTORP (alist))
7efd36fc 785 return call3 (alist, string, predicate, Qnil);
f927c5ae
JB
786
787 bestmatch = Qnil;
788
789 /* If ALIST is not a list, set TAIL just for gc pro. */
790 tail = alist;
791 if (! list)
792 {
793 index = 0;
794 obsize = XVECTOR (alist)->size;
795 bucket = XVECTOR (alist)->contents[index];
796 }
797
798 while (1)
799 {
800 /* Get the next element of the alist or obarray. */
801 /* Exit the loop if the elements are all used up. */
802 /* elt gets the alist element or symbol.
803 eltstring gets the name to check as a completion. */
804
805 if (list)
806 {
56a98455 807 if (NILP (tail))
f927c5ae
JB
808 break;
809 elt = Fcar (tail);
810 eltstring = Fcar (elt);
811 tail = Fcdr (tail);
812 }
813 else
814 {
815 if (XFASTINT (bucket) != 0)
816 {
817 elt = bucket;
818 eltstring = Fsymbol_name (elt);
819 if (XSYMBOL (bucket)->next)
820 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
821 else
5a866662 822 XSETFASTINT (bucket, 0);
f927c5ae
JB
823 }
824 else if (++index >= obsize)
825 break;
826 else
827 {
828 bucket = XVECTOR (alist)->contents[index];
829 continue;
830 }
831 }
832
833 /* Is this element a possible completion? */
834
7510b296 835 if (STRINGP (eltstring)
42006772
RS
836 && XSTRING (string)->size <= XSTRING (eltstring)->size
837 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
838 XSTRING (string)->size))
f927c5ae
JB
839 {
840 /* Yes. */
42006772
RS
841 Lisp_Object regexps;
842 Lisp_Object zero;
5a866662 843 XSETFASTINT (zero, 0);
42006772
RS
844
845 /* Ignore this element if it fails to match all the regexps. */
846 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
847 regexps = XCONS (regexps)->cdr)
848 {
849 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
850 if (NILP (tem))
851 break;
852 }
853 if (CONSP (regexps))
854 continue;
855
f927c5ae
JB
856 /* Ignore this element if there is a predicate
857 and the predicate doesn't like it. */
858
7efd36fc 859 if (!NILP (predicate))
f927c5ae 860 {
7efd36fc 861 if (EQ (predicate, Qcommandp))
f927c5ae
JB
862 tem = Fcommandp (elt);
863 else
864 {
865 GCPRO4 (tail, string, eltstring, bestmatch);
7efd36fc 866 tem = call1 (predicate, elt);
f927c5ae
JB
867 UNGCPRO;
868 }
56a98455 869 if (NILP (tem)) continue;
f927c5ae
JB
870 }
871
872 /* Update computation of how much all possible completions match */
873
874 matchcount++;
56a98455 875 if (NILP (bestmatch))
f927c5ae
JB
876 bestmatch = eltstring, bestmatchsize = XSTRING (eltstring)->size;
877 else
878 {
879 compare = min (bestmatchsize, XSTRING (eltstring)->size);
880 matchsize = scmp (XSTRING (bestmatch)->data,
881 XSTRING (eltstring)->data,
882 compare);
52b14ac0
JB
883 if (matchsize < 0)
884 matchsize = compare;
885 if (completion_ignore_case)
886 {
887 /* If this is an exact match except for case,
888 use it as the best match rather than one that is not an
889 exact match. This way, we get the case pattern
890 of the actual match. */
891 if ((matchsize == XSTRING (eltstring)->size
892 && matchsize < XSTRING (bestmatch)->size)
893 ||
894 /* If there is more than one exact match ignoring case,
895 and one of them is exact including case,
896 prefer that one. */
897 /* If there is no exact match ignoring case,
898 prefer a match that does not change the case
899 of the input. */
900 ((matchsize == XSTRING (eltstring)->size)
901 ==
902 (matchsize == XSTRING (bestmatch)->size)
903 && !bcmp (XSTRING (eltstring)->data,
904 XSTRING (string)->data, XSTRING (string)->size)
905 && bcmp (XSTRING (bestmatch)->data,
906 XSTRING (string)->data, XSTRING (string)->size)))
907 bestmatch = eltstring;
908 }
909 bestmatchsize = matchsize;
f927c5ae
JB
910 }
911 }
912 }
913
56a98455 914 if (NILP (bestmatch))
f927c5ae 915 return Qnil; /* No completions found */
52b14ac0
JB
916 /* If we are ignoring case, and there is no exact match,
917 and no additional text was supplied,
918 don't change the case of what the user typed. */
919 if (completion_ignore_case && bestmatchsize == XSTRING (string)->size
920 && XSTRING (bestmatch)->size > bestmatchsize)
921 return string;
922
923 /* Return t if the supplied string is an exact match (counting case);
924 it does not require any change to be made. */
925 if (matchcount == 1 && bestmatchsize == XSTRING (string)->size
926 && !bcmp (XSTRING (bestmatch)->data, XSTRING (string)->data,
927 bestmatchsize))
f927c5ae
JB
928 return Qt;
929
5a866662
KH
930 XSETFASTINT (zero, 0); /* Else extract the part in which */
931 XSETFASTINT (end, bestmatchsize); /* all completions agree */
f927c5ae
JB
932 return Fsubstring (bestmatch, zero, end);
933}
934
935/* Compare exactly LEN chars of strings at S1 and S2,
936 ignoring case if appropriate.
937 Return -1 if strings match,
938 else number of chars that match at the beginning. */
939
829f7f7c 940int
f927c5ae 941scmp (s1, s2, len)
829f7f7c 942 register unsigned char *s1, *s2;
f927c5ae
JB
943 int len;
944{
945 register int l = len;
d50a3d2a 946 register unsigned char *start = s1;
f927c5ae
JB
947
948 if (completion_ignore_case)
949 {
49db96ce 950 while (l && EQ (DOWNCASE (*s1++), DOWNCASE (*s2++)))
f927c5ae
JB
951 l--;
952 }
953 else
954 {
955 while (l && *s1++ == *s2++)
956 l--;
957 }
958 if (l == 0)
959 return -1;
829f7f7c 960 else
d50a3d2a
KH
961 {
962 int match = len - l;
963
964 /* Now *--S1 is the unmatching byte. If it is in the middle of
965 multi-byte form, we must say that the multi-byte character
966 there doesn't match. */
967 while (match && *--s1 >= 0xA0) match--;
968 return match;
969 }
f927c5ae
JB
970}
971\f
89a255dc 972DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
f927c5ae
JB
973 "Search for partial matches to STRING in ALIST.\n\
974Each car of each element of ALIST is tested to see if it begins with STRING.\n\
975The value is a list of all the strings from ALIST that match.\n\
89a255dc 976\n\
f927c5ae
JB
977ALIST can be an obarray instead of an alist.\n\
978Then the print names of all symbols in the obarray are the possible matches.\n\
979\n\
980ALIST can also be a function to do the completion itself.\n\
981It receives three arguments: the values STRING, PREDICATE and t.\n\
982Whatever it returns becomes the value of `all-completion'.\n\
983\n\
984If optional third argument PREDICATE is non-nil,\n\
985it is used to test each possible match.\n\
986The match is a candidate only if PREDICATE returns non-nil.\n\
89a255dc
RS
987The argument given to PREDICATE is the alist element\n\
988or the symbol from the obarray.\n\
989\n\
990If the optional fourth argument HIDE-SPACES is non-nil,\n\
991strings in ALIST that start with a space\n\
992are ignored unless STRING itself starts with a space.")
7efd36fc
EN
993 (string, alist, predicate, hide_spaces)
994 Lisp_Object string, alist, predicate, hide_spaces;
f927c5ae
JB
995{
996 Lisp_Object tail, elt, eltstring;
997 Lisp_Object allmatches;
56a98455 998 int list = CONSP (alist) || NILP (alist);
f927c5ae
JB
999 int index, obsize;
1000 Lisp_Object bucket, tem;
1001 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1002
1003 CHECK_STRING (string, 0);
7510b296 1004 if (!list && !VECTORP (alist))
f927c5ae 1005 {
7efd36fc 1006 return call3 (alist, string, predicate, Qt);
f927c5ae
JB
1007 }
1008 allmatches = Qnil;
1009
1010 /* If ALIST is not a list, set TAIL just for gc pro. */
1011 tail = alist;
1012 if (! list)
1013 {
1014 index = 0;
1015 obsize = XVECTOR (alist)->size;
1016 bucket = XVECTOR (alist)->contents[index];
1017 }
1018
1019 while (1)
1020 {
1021 /* Get the next element of the alist or obarray. */
1022 /* Exit the loop if the elements are all used up. */
1023 /* elt gets the alist element or symbol.
1024 eltstring gets the name to check as a completion. */
1025
1026 if (list)
1027 {
56a98455 1028 if (NILP (tail))
f927c5ae
JB
1029 break;
1030 elt = Fcar (tail);
1031 eltstring = Fcar (elt);
1032 tail = Fcdr (tail);
1033 }
1034 else
1035 {
1036 if (XFASTINT (bucket) != 0)
1037 {
1038 elt = bucket;
1039 eltstring = Fsymbol_name (elt);
1040 if (XSYMBOL (bucket)->next)
1041 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1042 else
5a866662 1043 XSETFASTINT (bucket, 0);
f927c5ae
JB
1044 }
1045 else if (++index >= obsize)
1046 break;
1047 else
1048 {
1049 bucket = XVECTOR (alist)->contents[index];
1050 continue;
1051 }
1052 }
1053
1054 /* Is this element a possible completion? */
1055
7510b296 1056 if (STRINGP (eltstring)
2cbaf886 1057 && XSTRING (string)->size <= XSTRING (eltstring)->size
89a255dc 1058 /* If HIDE_SPACES, reject alternatives that start with space
2cbaf886
RS
1059 unless the input starts with space. */
1060 && ((XSTRING (string)->size > 0 && XSTRING (string)->data[0] == ' ')
89a255dc
RS
1061 || XSTRING (eltstring)->data[0] != ' '
1062 || NILP (hide_spaces))
2cbaf886
RS
1063 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
1064 XSTRING (string)->size))
f927c5ae
JB
1065 {
1066 /* Yes. */
42006772
RS
1067 Lisp_Object regexps;
1068 Lisp_Object zero;
5a866662 1069 XSETFASTINT (zero, 0);
42006772
RS
1070
1071 /* Ignore this element if it fails to match all the regexps. */
1072 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1073 regexps = XCONS (regexps)->cdr)
1074 {
1075 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
1076 if (NILP (tem))
1077 break;
1078 }
1079 if (CONSP (regexps))
1080 continue;
1081
f927c5ae
JB
1082 /* Ignore this element if there is a predicate
1083 and the predicate doesn't like it. */
1084
7efd36fc 1085 if (!NILP (predicate))
f927c5ae 1086 {
7efd36fc 1087 if (EQ (predicate, Qcommandp))
f927c5ae
JB
1088 tem = Fcommandp (elt);
1089 else
1090 {
1091 GCPRO4 (tail, eltstring, allmatches, string);
7efd36fc 1092 tem = call1 (predicate, elt);
f927c5ae
JB
1093 UNGCPRO;
1094 }
56a98455 1095 if (NILP (tem)) continue;
f927c5ae
JB
1096 }
1097 /* Ok => put it on the list. */
1098 allmatches = Fcons (eltstring, allmatches);
1099 }
1100 }
1101
1102 return Fnreverse (allmatches);
1103}
1104\f
1105Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1106Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1107Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
1108
b9d721de
JB
1109/* This comment supplies the doc string for completing-read,
1110 for make-docfile to see. We cannot put this in the real DEFUN
1111 due to limits in the Unix cpp.
1112
e5d4686b 1113DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 7, 0,
f927c5ae 1114 "Read a string in the minibuffer, with completion.\n\
f927c5ae
JB
1115PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
1116TABLE is an alist whose elements' cars are strings, or an obarray.\n\
1117PREDICATE limits completion to a subset of TABLE.\n\
6b3faad8
RS
1118See `try-completion' and `all-completions' for more details
1119 on completion, TABLE, and PREDICATE.\n\
cbbc3917 1120\n\
f927c5ae 1121If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
4ced3209 1122 the input is (or completes to) an element of TABLE or is null.\n\
f927c5ae 1123 If it is also not t, Return does not exit if it does non-null completion.\n\
fab95625 1124If the input is null, `completing-read' returns an empty string,\n\
cbbc3917
RS
1125 regardless of the value of REQUIRE-MATCH.\n\
1126\n\
f927c5ae 1127If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
770970cb
RS
1128 If it is (STRING . POSITION), the initial input\n\
1129 is STRING, but point is placed POSITION characters into the string.\n\
1130HIST, if non-nil, specifies a history list\n\
1131 and optionally the initial position in the list.\n\
1132 It can be a symbol, which is the history list variable to use,\n\
1133 or it can be a cons cell (HISTVAR . HISTPOS).\n\
1134 In that case, HISTVAR is the history list variable to use,\n\
1135 and HISTPOS is the initial position (the position in the list\n\
1136 which INITIAL-CONTENTS corresponds to).\n\
1137 Positions are counted starting from 1 at the beginning of the list.\n\
e5d4686b
RS
1138DEF, if non-nil, is the default value.
1139
770970cb 1140Completion ignores case if the ambient value of\n\
b9d721de
JB
1141 `completion-ignore-case' is non-nil."
1142*/
e5d4686b 1143DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 7, 0,
b9d721de 1144 0 /* See immediately above */)
e5d4686b
RS
1145 (prompt, table, predicate, require_match, init, hist, def)
1146 Lisp_Object prompt, table, predicate, require_match, init, hist, def;
f927c5ae 1147{
770970cb
RS
1148 Lisp_Object val, histvar, histpos, position;
1149 int pos = 0;
f927c5ae
JB
1150 int count = specpdl_ptr - specpdl;
1151 specbind (Qminibuffer_completion_table, table);
7efd36fc 1152 specbind (Qminibuffer_completion_predicate, predicate);
f927c5ae
JB
1153 specbind (Qminibuffer_completion_confirm,
1154 EQ (require_match, Qt) ? Qnil : Qt);
1155 last_exact_completion = Qnil;
770970cb
RS
1156
1157 position = Qnil;
1158 if (!NILP (init))
1159 {
7510b296 1160 if (CONSP (init))
770970cb
RS
1161 {
1162 position = Fcdr (init);
1163 init = Fcar (init);
1164 }
1165 CHECK_STRING (init, 0);
1166 if (!NILP (position))
1167 {
1168 CHECK_NUMBER (position, 0);
1169 /* Convert to distance from end of input. */
5dadd3a2 1170 pos = XINT (position) - XSTRING (init)->size;
770970cb
RS
1171 }
1172 }
1173
7510b296 1174 if (SYMBOLP (hist))
770970cb
RS
1175 {
1176 histvar = hist;
1177 histpos = Qnil;
1178 }
1179 else
1180 {
1181 histvar = Fcar_safe (hist);
1182 histpos = Fcdr_safe (hist);
1183 }
1184 if (NILP (histvar))
1185 histvar = Qminibuffer_history;
1186 if (NILP (histpos))
5a866662 1187 XSETFASTINT (histpos, 0);
770970cb 1188
56a98455 1189 val = read_minibuf (NILP (require_match)
f927c5ae
JB
1190 ? Vminibuffer_local_completion_map
1191 : Vminibuffer_local_must_match_map,
85b5fe07 1192 init, prompt, make_number (pos), 0,
e5d4686b
RS
1193 histvar, histpos, def);
1194 if (STRINGP (val) && XSTRING (val)->size == 0 && ! NILP (def))
1195 val = def;
f927c5ae
JB
1196 return unbind_to (count, val);
1197}
1198\f
f927c5ae 1199Lisp_Object Fminibuffer_completion_help ();
52b14ac0 1200Lisp_Object assoc_for_completion ();
5d2ca7ae
RS
1201/* A subroutine of Fintern_soft. */
1202extern Lisp_Object oblookup ();
1203
1204
1205/* Test whether TXT is an exact completion. */
1206Lisp_Object
1207test_completion (txt)
1208 Lisp_Object txt;
1209{
1210 Lisp_Object tem;
1211
1212 if (CONSP (Vminibuffer_completion_table)
1213 || NILP (Vminibuffer_completion_table))
1214 return assoc_for_completion (txt, Vminibuffer_completion_table);
1215 else if (VECTORP (Vminibuffer_completion_table))
1216 {
1217 /* Bypass intern-soft as that loses for nil */
1218 tem = oblookup (Vminibuffer_completion_table,
1219 XSTRING (txt)->data, XSTRING (txt)->size);
44472c88 1220 if (!SYMBOLP (tem))
5d2ca7ae
RS
1221 return Qnil;
1222 else if (!NILP (Vminibuffer_completion_predicate))
1223 return call1 (Vminibuffer_completion_predicate, tem);
1224 else
1225 return Qt;
1226 }
1227 else
1228 return call3 (Vminibuffer_completion_table, txt,
1229 Vminibuffer_completion_predicate, Qlambda);
1230}
f927c5ae
JB
1231
1232/* returns:
1233 * 0 no possible completion
1234 * 1 was already an exact and unique completion
1235 * 3 was already an exact completion
1236 * 4 completed to an exact completion
1237 * 5 some completion happened
1238 * 6 no completion happened
1239 */
1240int
1241do_completion ()
1242{
1243 Lisp_Object completion, tem;
1244 int completedp;
1245 Lisp_Object last;
1e00c2ff 1246 struct gcpro gcpro1, gcpro2;
f927c5ae
JB
1247
1248 completion = Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table,
1249 Vminibuffer_completion_predicate);
1250 last = last_exact_completion;
1251 last_exact_completion = Qnil;
1252
4f9b95e5
KH
1253 GCPRO2 (completion, last);
1254
56a98455 1255 if (NILP (completion))
f927c5ae
JB
1256 {
1257 bitch_at_user ();
1258 temp_echo_area_glyphs (" [No match]");
1e00c2ff 1259 UNGCPRO;
f927c5ae
JB
1260 return 0;
1261 }
1262
1263 if (EQ (completion, Qt)) /* exact and unique match */
1e00c2ff
KH
1264 {
1265 UNGCPRO;
1266 return 1;
1267 }
f927c5ae
JB
1268
1269 /* compiler bug */
1270 tem = Fstring_equal (completion, Fbuffer_string());
56a98455 1271 if (completedp = NILP (tem))
f927c5ae
JB
1272 {
1273 Ferase_buffer (); /* Some completion happened */
1274 Finsert (1, &completion);
1275 }
1276
1277 /* It did find a match. Do we match some possibility exactly now? */
5d2ca7ae 1278 tem = test_completion (Fbuffer_string ());
56a98455 1279 if (NILP (tem))
1e00c2ff
KH
1280 {
1281 /* not an exact match */
1282 UNGCPRO;
f927c5ae
JB
1283 if (completedp)
1284 return 5;
1285 else if (auto_help)
1286 Fminibuffer_completion_help ();
1287 else
1288 temp_echo_area_glyphs (" [Next char not unique]");
1289 return 6;
1290 }
1291 else if (completedp)
1e00c2ff
KH
1292 {
1293 UNGCPRO;
1294 return 4;
1295 }
f927c5ae
JB
1296 /* If the last exact completion and this one were the same,
1297 it means we've already given a "Complete but not unique"
52b14ac0 1298 message and the user's hit TAB again, so now we give him help. */
f927c5ae 1299 last_exact_completion = completion;
56a98455 1300 if (!NILP (last))
f927c5ae
JB
1301 {
1302 tem = Fbuffer_string ();
56a98455 1303 if (!NILP (Fequal (tem, last)))
f927c5ae
JB
1304 Fminibuffer_completion_help ();
1305 }
1e00c2ff 1306 UNGCPRO;
f927c5ae 1307 return 3;
f927c5ae 1308}
1e00c2ff 1309
52b14ac0
JB
1310/* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1311
1312Lisp_Object
1313assoc_for_completion (key, list)
1314 register Lisp_Object key;
1315 Lisp_Object list;
1316{
1317 register Lisp_Object tail;
1318
1319 if (completion_ignore_case)
1320 key = Fupcase (key);
1321
56a98455 1322 for (tail = list; !NILP (tail); tail = Fcdr (tail))
52b14ac0
JB
1323 {
1324 register Lisp_Object elt, tem, thiscar;
1325 elt = Fcar (tail);
1326 if (!CONSP (elt)) continue;
1327 thiscar = Fcar (elt);
7510b296 1328 if (!STRINGP (thiscar))
52b14ac0
JB
1329 continue;
1330 if (completion_ignore_case)
1331 thiscar = Fupcase (thiscar);
1332 tem = Fequal (thiscar, key);
56a98455 1333 if (!NILP (tem)) return elt;
52b14ac0
JB
1334 QUIT;
1335 }
1336 return Qnil;
1337}
f927c5ae
JB
1338
1339DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
6300d782 1340 "Complete the minibuffer contents as far as possible.\n\
2cb6da5c
RS
1341Return nil if there is no valid completion, else t.\n\
1342If no characters can be completed, display a list of possible completions.\n\
1343If you repeat this command after it displayed such a list,\n\
1344scroll the window of possible completions.")
f927c5ae
JB
1345 ()
1346{
2cb6da5c
RS
1347 register int i;
1348 Lisp_Object window, tem;
1349
1350 /* If the previous command was not this, then mark the completion
1351 buffer obsolete. */
5221fd63 1352 if (! EQ (current_kboard->Vlast_command, this_command))
2cb6da5c
RS
1353 Vminibuf_scroll_window = Qnil;
1354
1355 window = Vminibuf_scroll_window;
1356 /* If there's a fresh completion window with a live buffer,
1357 and this command is repeated, scroll that window. */
1358 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1359 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1360 {
1361 struct buffer *obuf = current_buffer;
1362
1363 Fset_buffer (XWINDOW (window)->buffer);
1364 tem = Fpos_visible_in_window_p (make_number (ZV), window);
1365 if (! NILP (tem))
1366 /* If end is in view, scroll up to the beginning. */
8768d630 1367 Fset_window_start (window, BEGV, Qnil);
2cb6da5c
RS
1368 else
1369 /* Else scroll down one screen. */
1370 Fscroll_other_window (Qnil);
1371
1372 set_buffer_internal (obuf);
1373 return Qnil;
1374 }
1375
1376 i = do_completion ();
f927c5ae
JB
1377 switch (i)
1378 {
1379 case 0:
1380 return Qnil;
1381
1382 case 1:
1383 temp_echo_area_glyphs (" [Sole completion]");
1384 break;
1385
1386 case 3:
1387 temp_echo_area_glyphs (" [Complete, but not unique]");
1388 break;
1389 }
1390
1391 return Qt;
1392}
e4c97a67
RS
1393\f
1394/* Subroutines of Fminibuffer_complete_and_exit. */
1395
1396/* This one is called by internal_condition_case to do the real work. */
1397
1398Lisp_Object
1399complete_and_exit_1 ()
1400{
1401 return make_number (do_completion ());
1402}
1403
1404/* This one is called by internal_condition_case if an error happens.
1405 Pretend the current value is an exact match. */
1406
1407Lisp_Object
1408complete_and_exit_2 (ignore)
1409 Lisp_Object ignore;
1410{
1411 return make_number (1);
1412}
f927c5ae
JB
1413
1414DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1415 Sminibuffer_complete_and_exit, 0, 0, "",
5d2ca7ae
RS
1416 "If the minibuffer contents is a valid completion then exit.\n\
1417Otherwise try to complete it. If completion leads to a valid completion,\n\
f927c5ae
JB
1418a repetition of this command will exit.")
1419 ()
1420{
1421 register int i;
e4c97a67 1422 Lisp_Object val;
f927c5ae
JB
1423
1424 /* Allow user to specify null string */
1425 if (BEGV == ZV)
1426 goto exit;
1427
5d2ca7ae
RS
1428 if (!NILP (test_completion (Fbuffer_string ())))
1429 goto exit;
1430
e4c97a67
RS
1431 /* Call do_completion, but ignore errors. */
1432 val = internal_condition_case (complete_and_exit_1, Qerror,
1433 complete_and_exit_2);
1434
1435 i = XFASTINT (val);
f927c5ae
JB
1436 switch (i)
1437 {
1438 case 1:
1439 case 3:
1440 goto exit;
1441
1442 case 4:
56a98455 1443 if (!NILP (Vminibuffer_completion_confirm))
f927c5ae
JB
1444 {
1445 temp_echo_area_glyphs (" [Confirm]");
1446 return Qnil;
1447 }
1448 else
1449 goto exit;
1450
1451 default:
1452 return Qnil;
1453 }
1454 exit:
1455 Fthrow (Qexit, Qnil);
1456 /* NOTREACHED */
1457}
1458
1459DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1460 0, 0, "",
1461 "Complete the minibuffer contents at most a single word.\n\
1462After one word is completed as much as possible, a space or hyphen\n\
6300d782
KH
1463is added, provided that matches some possible completion.\n\
1464Return nil if there is no valid completion, else t.")
f927c5ae
JB
1465 ()
1466{
1467 Lisp_Object completion, tem;
1468 register int i;
1469 register unsigned char *completion_string;
d7be4211 1470 struct gcpro gcpro1, gcpro2;
b278606c
BF
1471
1472 /* We keep calling Fbuffer_string rather than arrange for GC to
1473 hold onto a pointer to one of the strings thus made. */
f927c5ae
JB
1474
1475 completion = Ftry_completion (Fbuffer_string (),
1476 Vminibuffer_completion_table,
1477 Vminibuffer_completion_predicate);
56a98455 1478 if (NILP (completion))
f927c5ae
JB
1479 {
1480 bitch_at_user ();
1481 temp_echo_area_glyphs (" [No match]");
1482 return Qnil;
1483 }
1484 if (EQ (completion, Qt))
1485 return Qnil;
1486
b278606c 1487#if 0 /* How the below code used to look, for reference. */
f927c5ae
JB
1488 tem = Fbuffer_string ();
1489 b = XSTRING (tem)->data;
1490 i = ZV - 1 - XSTRING (completion)->size;
1491 p = XSTRING (completion)->data;
1492 if (i > 0 ||
1493 0 <= scmp (b, p, ZV - 1))
1494 {
1495 i = 1;
1496 /* Set buffer to longest match of buffer tail and completion head. */
1497 while (0 <= scmp (b + i, p, ZV - 1 - i))
1498 i++;
1499 del_range (1, i + 1);
1500 SET_PT (ZV);
1501 }
1502#else /* Rewritten code */
1503 {
1504 register unsigned char *buffer_string;
1505 int buffer_length, completion_length;
1506
59860a8c 1507 CHECK_STRING (completion, 0);
f927c5ae 1508 tem = Fbuffer_string ();
d7be4211 1509 GCPRO2 (completion, tem);
719b4a40
RS
1510 /* If reading a file name,
1511 expand any $ENVVAR refs in the buffer and in TEM. */
1512 if (EQ (Vminibuffer_completion_table, Qread_file_name_internal))
1513 {
1514 Lisp_Object substituted;
1515 substituted = Fsubstitute_in_file_name (tem);
1516 if (! EQ (substituted, tem))
1517 {
1518 tem = substituted;
1519 Ferase_buffer ();
3cab9ae4 1520 insert_from_string (tem, 0, XSTRING (tem)->size, 0);
719b4a40
RS
1521 }
1522 }
f927c5ae
JB
1523 buffer_string = XSTRING (tem)->data;
1524 completion_string = XSTRING (completion)->data;
1525 buffer_length = XSTRING (tem)->size; /* ie ZV - BEGV */
1526 completion_length = XSTRING (completion)->size;
1527 i = buffer_length - completion_length;
1528 /* Mly: I don't understand what this is supposed to do AT ALL */
1529 if (i > 0 ||
1530 0 <= scmp (buffer_string, completion_string, buffer_length))
1531 {
1532 /* Set buffer to longest match of buffer tail and completion head. */
1533 if (i <= 0) i = 1;
1534 buffer_string += i;
1535 buffer_length -= i;
1536 while (0 <= scmp (buffer_string++, completion_string, buffer_length--))
1537 i++;
1538 del_range (1, i + 1);
1539 SET_PT (ZV);
1540 }
d7be4211 1541 UNGCPRO;
f927c5ae
JB
1542 }
1543#endif /* Rewritten code */
1544 i = ZV - BEGV;
1545
1546 /* If completion finds next char not unique,
b278606c 1547 consider adding a space or a hyphen. */
f927c5ae
JB
1548 if (i == XSTRING (completion)->size)
1549 {
b278606c 1550 GCPRO1 (completion);
f927c5ae
JB
1551 tem = Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")),
1552 Vminibuffer_completion_table,
1553 Vminibuffer_completion_predicate);
b278606c
BF
1554 UNGCPRO;
1555
7510b296 1556 if (STRINGP (tem))
f927c5ae
JB
1557 completion = tem;
1558 else
1559 {
b278606c
BF
1560 GCPRO1 (completion);
1561 tem =
1562 Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")),
1563 Vminibuffer_completion_table,
1564 Vminibuffer_completion_predicate);
1565 UNGCPRO;
1566
7510b296 1567 if (STRINGP (tem))
f927c5ae
JB
1568 completion = tem;
1569 }
1570 }
1571
1572 /* Now find first word-break in the stuff found by completion.
1573 i gets index in string of where to stop completing. */
d50a3d2a
KH
1574 {
1575 int len, c;
b278606c 1576
d50a3d2a
KH
1577 completion_string = XSTRING (completion)->data;
1578 for (; i < XSTRING (completion)->size; i += len)
1579 {
1580 c = STRING_CHAR_AND_LENGTH (completion_string + i,
1581 XSTRING (completion)->size - i,
1582 len);
1583 if (SYNTAX (c) != Sword)
1584 {
1585 i += len;
1586 break;
1587 }
1588 }
1589 }
f927c5ae
JB
1590
1591 /* If got no characters, print help for user. */
1592
1593 if (i == ZV - BEGV)
1594 {
1595 if (auto_help)
1596 Fminibuffer_completion_help ();
1597 return Qnil;
1598 }
1599
1600 /* Otherwise insert in minibuffer the chars we got */
1601
1602 Ferase_buffer ();
3cab9ae4 1603 insert_from_string (completion, 0, i, 1);
f927c5ae
JB
1604 return Qt;
1605}
1606\f
1607DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
1608 1, 1, 0,
2dc2b736 1609 "Display the list of completions, COMPLETIONS, using `standard-output'.\n\
f927c5ae 1610Each element may be just a symbol or string\n\
2d7e41fe
RS
1611or may be a list of two strings to be printed as if concatenated.\n\
1612`standard-output' must be a buffer.\n\
1613At the end, run the normal hook `completion-setup-hook'.\n\
1614It can find the completion buffer in `standard-output'.")
f927c5ae
JB
1615 (completions)
1616 Lisp_Object completions;
1617{
dae36123 1618 Lisp_Object tail, elt;
f927c5ae 1619 register int i;
2dc2b736 1620 int column = 0;
dae36123 1621 struct gcpro gcpro1, gcpro2;
2dc2b736 1622 struct buffer *old = current_buffer;
681f5af4 1623 int first = 1;
486cc7fb
RS
1624
1625 /* Note that (when it matters) every variable
dae36123
RS
1626 points to a non-string that is pointed to by COMPLETIONS,
1627 except for ELT. ELT can be pointing to a string
1628 when terpri or Findent_to calls a change hook. */
1629 elt = Qnil;
1630 GCPRO2 (completions, elt);
486cc7fb 1631
7510b296 1632 if (BUFFERP (Vstandard_output))
2dc2b736 1633 set_buffer_internal (XBUFFER (Vstandard_output));
f927c5ae 1634
56a98455 1635 if (NILP (completions))
cfc736bf
RS
1636 write_string ("There are no possible completions of what you have typed.",
1637 -1);
f927c5ae
JB
1638 else
1639 {
2dc2b736 1640 write_string ("Possible completions are:", -1);
56a98455 1641 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
f927c5ae 1642 {
681f5af4
RS
1643 Lisp_Object tem;
1644 int length;
47d7d104 1645 Lisp_Object startpos, endpos;
681f5af4
RS
1646
1647 elt = Fcar (tail);
1648 /* Compute the length of this element. */
1649 if (CONSP (elt))
1650 {
49db96ce 1651 tem = XCAR (elt);
681f5af4 1652 CHECK_STRING (tem, 0);
49db96ce 1653 length = XSTRING (tem)->size;
681f5af4 1654
49db96ce 1655 tem = Fcar (XCDR (elt));
681f5af4 1656 CHECK_STRING (tem, 0);
49db96ce 1657 length += XSTRING (tem)->size;
681f5af4
RS
1658 }
1659 else
1660 {
1661 CHECK_STRING (elt, 0);
49db96ce 1662 length = XSTRING (elt)->size;
681f5af4
RS
1663 }
1664
1665 /* This does a bad job for narrower than usual windows.
1666 Sadly, the window it will appear in is not known
1667 until after the text has been made. */
1668
47d7d104
RS
1669 if (BUFFERP (Vstandard_output))
1670 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
1671
681f5af4
RS
1672 /* If the previous completion was very wide,
1673 or we have two on this line already,
1674 don't put another on the same line. */
1675 if (column > 33 || first
1676 /* If this is really wide, don't put it second on a line. */
1677 || column > 0 && length > 45)
1678 {
1679 Fterpri (Qnil);
1680 column = 0;
1681 }
1682 /* Otherwise advance to column 35. */
1683 else
2dc2b736 1684 {
7510b296 1685 if (BUFFERP (Vstandard_output))
681f5af4
RS
1686 {
1687 tem = Findent_to (make_number (35), make_number (2));
47d7d104 1688
681f5af4
RS
1689 column = XINT (tem);
1690 }
2dc2b736
RS
1691 else
1692 {
1693 do
1694 {
1695 write_string (" ", -1);
1696 column++;
1697 }
1698 while (column < 35);
1699 }
1700 }
681f5af4 1701
47d7d104
RS
1702 if (BUFFERP (Vstandard_output))
1703 {
1704 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
1705 Fset_text_properties (startpos, endpos,
1706 Qnil, Vstandard_output);
1707 }
1708
681f5af4 1709 /* Output this element and update COLUMN. */
f927c5ae
JB
1710 if (CONSP (elt))
1711 {
1712 Fprinc (Fcar (elt), Qnil);
1713 Fprinc (Fcar (Fcdr (elt)), Qnil);
1714 }
1715 else
681f5af4
RS
1716 Fprinc (elt, Qnil);
1717
1718 column += length;
1719
1720 /* If output is to a buffer, recompute COLUMN in a way
1721 that takes account of character widths. */
1722 if (BUFFERP (Vstandard_output))
2dc2b736 1723 {
681f5af4
RS
1724 tem = Fcurrent_column ();
1725 column = XINT (tem);
2dc2b736 1726 }
681f5af4
RS
1727
1728 first = 0;
f927c5ae
JB
1729 }
1730 }
2dc2b736 1731
486cc7fb
RS
1732 UNGCPRO;
1733
7510b296 1734 if (BUFFERP (Vstandard_output))
2d7e41fe
RS
1735 set_buffer_internal (old);
1736
cfc736bf
RS
1737 if (!NILP (Vrun_hooks))
1738 call1 (Vrun_hooks, intern ("completion-setup-hook"));
1739
f927c5ae
JB
1740 return Qnil;
1741}
1742
1743DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
1744 0, 0, "",
1745 "Display a list of possible completions of the current minibuffer contents.")
1746 ()
1747{
1748 Lisp_Object completions;
1749
1750 message ("Making completion list...");
1751 completions = Fall_completions (Fbuffer_string (),
1752 Vminibuffer_completion_table,
89a255dc
RS
1753 Vminibuffer_completion_predicate,
1754 Qt);
f927c5ae
JB
1755 echo_area_glyphs = 0;
1756
56a98455 1757 if (NILP (completions))
f927c5ae
JB
1758 {
1759 bitch_at_user ();
1760 temp_echo_area_glyphs (" [No completions]");
1761 }
1762 else
1763 internal_with_output_to_temp_buffer ("*Completions*",
1764 Fdisplay_completion_list,
1765 Fsort (completions, Qstring_lessp));
1766 return Qnil;
1767}
1768\f
1769DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
1770 "Terminate minibuffer input.")
1771 ()
1772{
7510b296 1773 if (INTEGERP (last_command_char))
f927c5ae
JB
1774 internal_self_insert (last_command_char, 0);
1775 else
1776 bitch_at_user ();
1777
1778 Fthrow (Qexit, Qnil);
1779}
1780
1781DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
1782 "Terminate this minibuffer argument.")
1783 ()
1784{
1785 Fthrow (Qexit, Qnil);
1786}
1787
1788DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
1789 "Return current depth of activations of minibuffer, a nonnegative integer.")
1790 ()
1791{
1792 return make_number (minibuf_level);
1793}
1794
37e9a934
KH
1795DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
1796 "Return the prompt string of the currently-active minibuffer.\n\
1797If no minibuffer is active, return nil.")
1798 ()
1799{
4d04c1f1 1800 return Fcopy_sequence (minibuf_prompt);
37e9a934
KH
1801}
1802
1803DEFUN ("minibuffer-prompt-width", Fminibuffer_prompt_width,
1804 Sminibuffer_prompt_width, 0, 0, 0,
c5896ef4 1805 "Return the display width of the minibuffer prompt.")
37e9a934
KH
1806 ()
1807{
1808 Lisp_Object width;
5a866662 1809 XSETFASTINT (width, minibuf_prompt_width);
37e9a934
KH
1810 return width;
1811}
f927c5ae 1812\f
2d955a50
RS
1813/* Temporarily display the string M at the end of the current
1814 minibuffer contents. This is used to display things like
1815 "[No Match]" when the user requests a completion for a prefix
1816 that has no possible completions, and other quick, unobtrusive
1817 messages. */
1818
1819temp_echo_area_glyphs (m)
1820 char *m;
1821{
1822 int osize = ZV;
1823 int opoint = PT;
1824 Lisp_Object oinhibit;
1825 oinhibit = Vinhibit_quit;
1826
1827 /* Clear out any old echo-area message to make way for our new thing. */
1828 message (0);
1829
1830 SET_PT (osize);
1831 insert_string (m);
1832 SET_PT (opoint);
1833 Vinhibit_quit = Qt;
1834 Fsit_for (make_number (2), Qnil, Qnil);
1835 del_range (osize, ZV);
1836 SET_PT (opoint);
1837 if (!NILP (Vquit_flag))
1838 {
1839 Vquit_flag = Qnil;
1840 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1841 }
1842 Vinhibit_quit = oinhibit;
1843}
1844
1845DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
1846 1, 1, 0,
1847 "Temporarily display STRING at the end of the minibuffer.\n\
1848The text is displayed for two seconds,\n\
1849or until the next input event arrives, whichever comes first.")
1850 (string)
1851 Lisp_Object string;
1852{
1853 temp_echo_area_glyphs (XSTRING (string)->data);
1854 return Qnil;
1855}
1856\f
f927c5ae
JB
1857init_minibuf_once ()
1858{
1859 Vminibuffer_list = Qnil;
1860 staticpro (&Vminibuffer_list);
1861}
1862
1863syms_of_minibuf ()
1864{
1865 minibuf_level = 0;
4d04c1f1
KH
1866 minibuf_prompt = Qnil;
1867 staticpro (&minibuf_prompt);
1868
1869 minibuf_save_list = Qnil;
1870 staticpro (&minibuf_save_list);
f927c5ae 1871
719b4a40
RS
1872 Qread_file_name_internal = intern ("read-file-name-internal");
1873 staticpro (&Qread_file_name_internal);
1874
e5d4686b
RS
1875 Qminibuffer_default = intern ("minibuffer-default");
1876 staticpro (&Qminibuffer_default);
1877 Fset (Qminibuffer_default, Qnil);
1878
f927c5ae
JB
1879 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
1880 staticpro (&Qminibuffer_completion_table);
1881
1882 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
1883 staticpro (&Qminibuffer_completion_confirm);
1884
1885 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
1886 staticpro (&Qminibuffer_completion_predicate);
1887
1e00c2ff
KH
1888 staticpro (&last_exact_completion);
1889 last_exact_completion = Qnil;
1890
f927c5ae
JB
1891 staticpro (&last_minibuf_string);
1892 last_minibuf_string = Qnil;
1893
1894 Quser_variable_p = intern ("user-variable-p");
1895 staticpro (&Quser_variable_p);
1896
770970cb
RS
1897 Qminibuffer_history = intern ("minibuffer-history");
1898 staticpro (&Qminibuffer_history);
f927c5ae 1899
5c781212
RS
1900 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
1901 staticpro (&Qminibuffer_setup_hook);
1902
177aecf9
KH
1903 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
1904 staticpro (&Qminibuffer_exit_hook);
1905
77aa8edf
RS
1906 Qhistory_length = intern ("history-length");
1907 staticpro (&Qhistory_length);
1908
5c781212
RS
1909 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
1910 "Normal hook run just after entry to minibuffer.");
1911 Vminibuffer_setup_hook = Qnil;
1912
177aecf9
KH
1913 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
1914 "Normal hook run just after exit from minibuffer.");
1915 Vminibuffer_exit_hook = Qnil;
1916
77aa8edf
RS
1917 DEFVAR_LISP ("history-length", &Vhistory_length,
1918 "*Maximum length for history lists before truncation takes place.\n\
1919A number means that length; t means infinite. Truncation takes place\n\
1920just after a new element is inserted. Setting the history-length\n\
1921property of a history variable overrides this default.");
1922 XSETFASTINT (Vhistory_length, 30);
1923
f927c5ae
JB
1924 DEFVAR_BOOL ("completion-auto-help", &auto_help,
1925 "*Non-nil means automatically provide help for invalid completion input.");
1926 auto_help = 1;
1927
1928 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
1929 "Non-nil means don't consider case significant in completion.");
1930 completion_ignore_case = 0;
1931
1932 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
1933 "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\
1934More precisely, this variable makes a difference when the minibuffer window\n\
1935is the selected window. If you are in some other window, minibuffer commands\n\
1936are allowed even if a minibuffer is active.");
1937 enable_recursive_minibuffers = 0;
1938
1939 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
1940 "Alist or obarray used for completion in the minibuffer.\n\
1941This becomes the ALIST argument to `try-completion' and `all-completion'.\n\
1942\n\
1943The value may alternatively be a function, which is given three arguments:\n\
1944 STRING, the current buffer contents;\n\
1945 PREDICATE, the predicate for filtering possible matches;\n\
1946 CODE, which says what kind of things to do.\n\
1947CODE can be nil, t or `lambda'.\n\
1948nil means to return the best completion of STRING, or nil if there is none.\n\
1949t means to return a list of all possible completions of STRING.\n\
1950`lambda' means to return t if STRING is a valid completion as it stands.");
1951 Vminibuffer_completion_table = Qnil;
1952
1953 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
1954 "Within call to `completing-read', this holds the PREDICATE argument.");
1955 Vminibuffer_completion_predicate = Qnil;
1956
1957 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
1958 "Non-nil => demand confirmation of completion before exiting minibuffer.");
1959 Vminibuffer_completion_confirm = Qnil;
1960
1961 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
1962 "Value that `help-form' takes on inside the minibuffer.");
1963 Vminibuffer_help_form = Qnil;
1964
770970cb
RS
1965 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
1966 "History list symbol to add minibuffer values to.\n\
2fa2413b
RS
1967Each string of minibuffer input, as it appears on exit from the minibuffer,\n\
1968is added with\n\
770970cb
RS
1969 (set minibuffer-history-variable\n\
1970 (cons STRING (symbol-value minibuffer-history-variable)))");
5a866662 1971 XSETFASTINT (Vminibuffer_history_variable, 0);
770970cb
RS
1972
1973 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
1974 "Current position of redoing in the history list.");
1975 Vminibuffer_history_position = Qnil;
1976
6a9ee000 1977 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
e98bcc1a
RS
1978 "*Non-nil means entering the minibuffer raises the minibuffer's frame.\n\
1979Some uses of the echo area also raise that frame (since they use it too).");
6a9ee000
RS
1980 minibuffer_auto_raise = 0;
1981
42006772
RS
1982 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
1983 "List of regexps that should restrict possible completions.");
1984 Vcompletion_regexp_list = Qnil;
1985
68313ed8 1986 defsubr (&Sset_minibuffer_window);
f927c5ae
JB
1987 defsubr (&Sread_from_minibuffer);
1988 defsubr (&Seval_minibuffer);
1989 defsubr (&Sread_minibuffer);
1990 defsubr (&Sread_string);
1991 defsubr (&Sread_command);
1992 defsubr (&Sread_variable);
1993 defsubr (&Sread_buffer);
1994 defsubr (&Sread_no_blanks_input);
1995 defsubr (&Sminibuffer_depth);
37e9a934
KH
1996 defsubr (&Sminibuffer_prompt);
1997 defsubr (&Sminibuffer_prompt_width);
f927c5ae
JB
1998
1999 defsubr (&Stry_completion);
2000 defsubr (&Sall_completions);
2001 defsubr (&Scompleting_read);
2002 defsubr (&Sminibuffer_complete);
2003 defsubr (&Sminibuffer_complete_word);
2004 defsubr (&Sminibuffer_complete_and_exit);
2005 defsubr (&Sdisplay_completion_list);
2006 defsubr (&Sminibuffer_completion_help);
2007
2008 defsubr (&Sself_insert_and_exit);
2009 defsubr (&Sexit_minibuffer);
2010
2d955a50 2011 defsubr (&Sminibuffer_message);
f927c5ae
JB
2012}
2013
2014keys_of_minibuf ()
2015{
2016 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
2017 "abort-recursive-edit");
2018 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
2019 "exit-minibuffer");
2020 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
2021 "exit-minibuffer");
2022
2023 initial_define_key (Vminibuffer_local_ns_map, Ctl ('g'),
2024 "abort-recursive-edit");
2025 initial_define_key (Vminibuffer_local_ns_map, Ctl ('m'),
2026 "exit-minibuffer");
2027 initial_define_key (Vminibuffer_local_ns_map, Ctl ('j'),
2028 "exit-minibuffer");
2029
2030 initial_define_key (Vminibuffer_local_ns_map, ' ',
2031 "exit-minibuffer");
2032 initial_define_key (Vminibuffer_local_ns_map, '\t',
2033 "exit-minibuffer");
2034 initial_define_key (Vminibuffer_local_ns_map, '?',
2035 "self-insert-and-exit");
2036
2037 initial_define_key (Vminibuffer_local_completion_map, Ctl ('g'),
2038 "abort-recursive-edit");
2039 initial_define_key (Vminibuffer_local_completion_map, Ctl ('m'),
2040 "exit-minibuffer");
2041 initial_define_key (Vminibuffer_local_completion_map, Ctl ('j'),
2042 "exit-minibuffer");
2043
2044 initial_define_key (Vminibuffer_local_completion_map, '\t',
2045 "minibuffer-complete");
2046 initial_define_key (Vminibuffer_local_completion_map, ' ',
2047 "minibuffer-complete-word");
2048 initial_define_key (Vminibuffer_local_completion_map, '?',
2049 "minibuffer-completion-help");
2050
2051 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('g'),
2052 "abort-recursive-edit");
2053 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
2054 "minibuffer-complete-and-exit");
2055 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
2056 "minibuffer-complete-and-exit");
2057 initial_define_key (Vminibuffer_local_must_match_map, '\t',
2058 "minibuffer-complete");
2059 initial_define_key (Vminibuffer_local_must_match_map, ' ',
2060 "minibuffer-complete-word");
2061 initial_define_key (Vminibuffer_local_must_match_map, '?',
2062 "minibuffer-completion-help");
2063}