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