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