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