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