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