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