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