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