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