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