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