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