Added (load "ediff-hook") after loading vc-hook.
[emacs.git] / src / minibuf.c
blobef4289c8e3db744293bbd338712e9b45933f9327
1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
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 #include "keyboard.h"
31 #define min(a, b) ((a) < (b) ? (a) : (b))
33 extern int quit_char;
35 /* List of buffers for use as minibuffers.
36 The first element of the list is used for the outermost minibuffer
37 invocation, the next element is used for a recursive minibuffer
38 invocation, etc. The list is extended at the end as deeper
39 minibuffer recursions are encountered. */
40 Lisp_Object Vminibuffer_list;
42 /* Data to remember during recursive minibuffer invocations */
43 Lisp_Object minibuf_save_list;
45 /* Depth in minibuffer invocations. */
46 int minibuf_level;
48 /* Nonzero means display completion help for invalid input */
49 int auto_help;
51 /* Fread_minibuffer leaves the input here as a string. */
52 Lisp_Object last_minibuf_string;
54 /* Nonzero means let functions called when within a minibuffer
55 invoke recursive minibuffers (to read arguments, or whatever) */
56 int enable_recursive_minibuffers;
58 /* help-form is bound to this while in the minibuffer. */
60 Lisp_Object Vminibuffer_help_form;
62 /* Variable which is the history list to add minibuffer values to. */
64 Lisp_Object Vminibuffer_history_variable;
66 /* Current position in the history list (adjusted by M-n and M-p). */
68 Lisp_Object Vminibuffer_history_position;
70 Lisp_Object Qminibuffer_history;
72 Lisp_Object Qread_file_name_internal;
74 /* Normal hooks for entry to and exit from minibuffer. */
76 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
77 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
79 /* Nonzero means completion ignores case. */
81 int completion_ignore_case;
83 /* List of regexps that should restrict possible completions. */
85 Lisp_Object Vcompletion_regexp_list;
87 /* Nonzero means raise the minibuffer frame when the minibuffer
88 is entered. */
90 int minibuffer_auto_raise;
92 /* If last completion attempt reported "Complete but not unique"
93 then this is the string completed then; otherwise this is nil. */
95 static Lisp_Object last_exact_completion;
97 Lisp_Object Quser_variable_p;
99 /* Non-nil means it is the window for C-M-v to scroll
100 when the minibuffer is selected. */
101 extern Lisp_Object Vminibuf_scroll_window;
103 extern Lisp_Object Voverriding_local_map;
105 /* Actual minibuffer invocation. */
107 void read_minibuf_unwind ();
108 Lisp_Object get_minibuffer ();
109 Lisp_Object read_minibuf ();
111 /* Read from the minibuffer using keymap MAP, initial contents INITIAL
112 (a string), putting point minus BACKUP_N chars from the end of INITIAL,
113 prompting with PROMPT (a string), using history list HISTVAR
114 with initial position HISTPOS. (BACKUP_N should be <= 0.)
116 Normally return the result as a string (the text that was read),
117 but if EXPFLAG is nonzero, read it and return the object read.
118 If HISTVAR is given, save the value read on that history only if it doesn't
119 match the front of that history list exactly. The value is pushed onto
120 the list as the string that was read. */
122 Lisp_Object
123 read_minibuf (map, initial, prompt, backup_n, expflag, histvar, histpos)
124 Lisp_Object map;
125 Lisp_Object initial;
126 Lisp_Object prompt;
127 Lisp_Object backup_n;
128 int expflag;
129 Lisp_Object histvar;
130 Lisp_Object histpos;
132 Lisp_Object val;
133 int count = specpdl_ptr - specpdl;
134 Lisp_Object mini_frame;
135 struct gcpro gcpro1, gcpro2, gcpro3;
137 single_kboard_state ();
139 val = Qnil;
140 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
141 store them away before we can GC. Don't need to protect
142 BACKUP_N because we use the value only if it is an integer. */
143 GCPRO3 (map, initial, val);
145 if (!STRINGP (prompt))
146 prompt = build_string ("");
148 if (!enable_recursive_minibuffers
149 && minibuf_level > 0
150 && (EQ (selected_window, minibuf_window)))
151 error ("Command attempted to use minibuffer while in minibuffer");
153 /* Could we simply bind these variables instead? */
154 minibuf_save_list
155 = Fcons (Voverriding_local_map,
156 Fcons (minibuf_window, minibuf_save_list));
157 minibuf_save_list
158 = Fcons (minibuf_prompt,
159 Fcons (make_number (minibuf_prompt_width),
160 Fcons (Vhelp_form,
161 Fcons (Vcurrent_prefix_arg,
162 Fcons (Vminibuffer_history_position,
163 Fcons (Vminibuffer_history_variable,
164 minibuf_save_list))))));
166 minibuf_prompt_width = 0; /* xdisp.c puts in the right value. */
167 minibuf_prompt = Fcopy_sequence (prompt);
168 Vminibuffer_history_position = histpos;
169 Vminibuffer_history_variable = histvar;
171 choose_minibuf_frame ();
173 record_unwind_protect (Fset_window_configuration,
174 Fcurrent_window_configuration (Qnil));
176 /* If the minibuffer window is on a different frame, save that
177 frame's configuration too. */
178 #ifdef MULTI_FRAME
179 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
180 if (XFRAME (mini_frame) != selected_frame)
181 record_unwind_protect (Fset_window_configuration,
182 Fcurrent_window_configuration (mini_frame));
184 /* If the minibuffer is on an iconified or invisible frame,
185 make it visible now. */
186 Fmake_frame_visible (mini_frame);
188 if (minibuffer_auto_raise)
189 Fraise_frame (mini_frame);
190 #endif
192 val = current_buffer->directory;
193 Fset_buffer (get_minibuffer (minibuf_level));
195 /* The current buffer's default directory is usually the right thing
196 for our minibuffer here. However, if you're typing a command at
197 a minibuffer-only frame when minibuf_level is zero, then buf IS
198 the current_buffer, so reset_buffer leaves buf's default
199 directory unchanged. This is a bummer when you've just started
200 up Emacs and buf's default directory is Qnil. Here's a hack; can
201 you think of something better to do? Find another buffer with a
202 better directory, and use that one instead. */
203 if (STRINGP (val))
204 current_buffer->directory = val;
205 else
207 Lisp_Object buf_list;
209 for (buf_list = Vbuffer_alist;
210 CONSP (buf_list);
211 buf_list = XCONS (buf_list)->cdr)
213 Lisp_Object other_buf;
215 other_buf = XCONS (XCONS (buf_list)->car)->cdr;
216 if (STRINGP (XBUFFER (other_buf)->directory))
218 current_buffer->directory = XBUFFER (other_buf)->directory;
219 break;
224 #ifdef MULTI_FRAME
225 if (XFRAME (mini_frame) != selected_frame)
226 Fredirect_frame_focus (Fselected_frame (), mini_frame);
227 #endif
228 Fmake_local_variable (Qprint_escape_newlines);
229 print_escape_newlines = 1;
231 record_unwind_protect (read_minibuf_unwind, Qnil);
233 Vminibuf_scroll_window = selected_window;
234 Fset_window_buffer (minibuf_window, Fcurrent_buffer ());
235 Fselect_window (minibuf_window);
236 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
238 Ferase_buffer ();
239 minibuf_level++;
241 if (!NILP (initial))
243 Finsert (1, &initial);
244 if (!NILP (backup_n) && INTEGERP (backup_n))
245 Fforward_char (backup_n);
248 echo_area_glyphs = 0;
249 /* This is in case the minibuffer-setup-hook calls Fsit_for. */
250 previous_echo_glyphs = 0;
252 Vhelp_form = Vminibuffer_help_form;
253 current_buffer->keymap = map;
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. */
258 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
259 && !NILP (Vrun_hooks))
260 call1 (Vrun_hooks, Qminibuffer_setup_hook);
262 /* ??? MCC did redraw_screen here if switching screens. */
263 recursive_edit_1 ();
265 /* If cursor is on the minibuffer line,
266 show the user we have exited by putting it in column 0. */
267 if ((FRAME_CURSOR_Y (selected_frame)
268 >= XFASTINT (XWINDOW (minibuf_window)->top))
269 && !noninteractive)
271 FRAME_CURSOR_X (selected_frame) = 0;
272 update_frame (selected_frame, 1, 1);
275 /* Make minibuffer contents into a string */
276 val = make_buffer_string (1, Z);
277 bcopy (GAP_END_ADDR, XSTRING (val)->data + GPT - BEG, Z - GPT);
279 /* VAL is the string of minibuffer text. */
280 last_minibuf_string = val;
282 /* Add the value to the appropriate history list unless it is empty. */
283 if (XSTRING (val)->size != 0
284 && SYMBOLP (Vminibuffer_history_variable)
285 && ! EQ (XSYMBOL (Vminibuffer_history_variable)->value, Qunbound))
287 /* If the caller wanted to save the value read on a history list,
288 then do so if the value is not already the front of the list. */
289 Lisp_Object histval;
290 histval = Fsymbol_value (Vminibuffer_history_variable);
292 /* The value of the history variable must be a cons or nil. Other
293 values are unacceptable. We silently ignore these values. */
294 if (NILP (histval)
295 || (CONSP (histval)
296 && NILP (Fequal (last_minibuf_string, Fcar (histval)))))
297 Fset (Vminibuffer_history_variable,
298 Fcons (last_minibuf_string, histval));
301 /* If Lisp form desired instead of string, parse it. */
302 if (expflag)
304 Lisp_Object expr_and_pos;
305 unsigned char *p;
307 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
308 /* Ignore trailing whitespace; any other trailing junk is an error. */
309 for (p = XSTRING (val)->data + XINT (Fcdr (expr_and_pos)); *p; p++)
310 if (*p != ' ' && *p != '\t' && *p != '\n')
311 error ("Trailing garbage following expression");
312 val = Fcar (expr_and_pos);
315 /* The appropriate frame will get selected
316 in set-window-configuration. */
317 RETURN_UNGCPRO (unbind_to (count, val));
320 /* Return a buffer to be used as the minibuffer at depth `depth'.
321 depth = 0 is the lowest allowed argument, and that is the value
322 used for nonrecursive minibuffer invocations */
324 Lisp_Object
325 get_minibuffer (depth)
326 int depth;
328 Lisp_Object tail, num, buf;
329 char name[24];
330 extern Lisp_Object nconc2 ();
332 XSETFASTINT (num, depth);
333 tail = Fnthcdr (num, Vminibuffer_list);
334 if (NILP (tail))
336 tail = Fcons (Qnil, Qnil);
337 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
339 buf = Fcar (tail);
340 if (NILP (buf) || NILP (XBUFFER (buf)->name))
342 sprintf (name, " *Minibuf-%d*", depth);
343 buf = Fget_buffer_create (build_string (name));
345 /* Although the buffer's name starts with a space, undo should be
346 enabled in it. */
347 Fbuffer_enable_undo (buf);
349 XCONS (tail)->car = buf;
351 else
353 int count = specpdl_ptr - specpdl;
355 reset_buffer (XBUFFER (buf));
356 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
357 Fset_buffer (buf);
358 Fkill_all_local_variables ();
359 unbind_to (count, Qnil);
362 return buf;
365 /* This function is called on exiting minibuffer, whether normally or not,
366 and it restores the current window, buffer, etc. */
368 void
369 read_minibuf_unwind (data)
370 Lisp_Object data;
372 Lisp_Object old_deactivate_mark;
374 /* We are exiting the minibuffer one way or the other,
375 so run the hook. */
376 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
377 && !NILP (Vrun_hooks))
378 safe_run_hooks (Qminibuffer_exit_hook);
380 /* Erase the minibuffer we were using at this level. */
381 Fset_buffer (XWINDOW (minibuf_window)->buffer);
383 /* Prevent error in erase-buffer. */
384 current_buffer->read_only = Qnil;
386 old_deactivate_mark = Vdeactivate_mark;
387 Ferase_buffer ();
388 Vdeactivate_mark = old_deactivate_mark;
390 /* If this was a recursive minibuffer,
391 tie the minibuffer window back to the outer level minibuffer buffer */
392 minibuf_level--;
393 /* Make sure minibuffer window is erased, not ignored */
394 windows_or_buffers_changed++;
395 XSETFASTINT (XWINDOW (minibuf_window)->last_modified, 0);
397 /* Restore prompt, etc from outer minibuffer */
398 minibuf_prompt = Fcar (minibuf_save_list);
399 minibuf_save_list = Fcdr (minibuf_save_list);
400 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
401 minibuf_save_list = Fcdr (minibuf_save_list);
402 Vhelp_form = Fcar (minibuf_save_list);
403 minibuf_save_list = Fcdr (minibuf_save_list);
404 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
405 minibuf_save_list = Fcdr (minibuf_save_list);
406 Vminibuffer_history_position = Fcar (minibuf_save_list);
407 minibuf_save_list = Fcdr (minibuf_save_list);
408 Vminibuffer_history_variable = Fcar (minibuf_save_list);
409 minibuf_save_list = Fcdr (minibuf_save_list);
410 Voverriding_local_map = Fcar (minibuf_save_list);
411 minibuf_save_list = Fcdr (minibuf_save_list);
412 minibuf_window = Fcar (minibuf_save_list);
413 minibuf_save_list = Fcdr (minibuf_save_list);
417 /* This comment supplies the doc string for read-from-minibuffer,
418 for make-docfile to see. We cannot put this in the real DEFUN
419 due to limits in the Unix cpp.
421 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0,
422 "Read a string from the minibuffer, prompting with string PROMPT.\n\
423 If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
424 to be inserted into the minibuffer before reading input.\n\
425 If INITIAL-CONTENTS is (STRING . POSITION), the initial input\n\
426 is STRING, but point is placed POSITION characters into the string.\n\
427 Third arg KEYMAP is a keymap to use whilst reading;\n\
428 if omitted or nil, the default is `minibuffer-local-map'.\n\
429 If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
430 and return that object:\n\
431 in other words, do `(car (read-from-string INPUT-STRING))'\n\
432 Fifth arg HIST, if non-nil, specifies a history list\n\
433 and optionally the initial position in the list.\n\
434 It can be a symbol, which is the history list variable to use,\n\
435 or it can be a cons cell (HISTVAR . HISTPOS).\n\
436 In that case, HISTVAR is the history list variable to use,\n\
437 and HISTPOS is the initial position (the position in the list\n\
438 which INITIAL-CONTENTS corresponds to).\n\
439 Positions are counted starting from 1 at the beginning of the list."
442 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0,
443 0 /* See immediately above */)
444 (prompt, initial_contents, keymap, read, hist)
445 Lisp_Object prompt, initial_contents, keymap, read, hist;
447 int pos = 0;
448 Lisp_Object histvar, histpos, position;
449 position = Qnil;
451 CHECK_STRING (prompt, 0);
452 if (!NILP (initial_contents))
454 if (CONSP (initial_contents))
456 position = Fcdr (initial_contents);
457 initial_contents = Fcar (initial_contents);
459 CHECK_STRING (initial_contents, 1);
460 if (!NILP (position))
462 CHECK_NUMBER (position, 0);
463 /* Convert to distance from end of input. */
464 pos = XINT (position) - 1 - XSTRING (initial_contents)->size;
468 if (NILP (keymap))
469 keymap = Vminibuffer_local_map;
470 else
471 keymap = get_keymap (keymap,2);
473 if (SYMBOLP (hist))
475 histvar = hist;
476 histpos = Qnil;
478 else
480 histvar = Fcar_safe (hist);
481 histpos = Fcdr_safe (hist);
483 if (NILP (histvar))
484 histvar = Qminibuffer_history;
485 if (NILP (histpos))
486 XSETFASTINT (histpos, 0);
488 return read_minibuf (keymap, initial_contents, prompt,
489 make_number (pos), !NILP (read), histvar, histpos);
492 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
493 "Return a Lisp object read using the minibuffer.\n\
494 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
495 is a string to insert in the minibuffer before reading.")
496 (prompt, initial_contents)
497 Lisp_Object prompt, initial_contents;
499 CHECK_STRING (prompt, 0);
500 if (!NILP (initial_contents))
501 CHECK_STRING (initial_contents, 1);
502 return read_minibuf (Vminibuffer_local_map, initial_contents,
503 prompt, Qnil, 1, Qminibuffer_history, make_number (0));
506 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
507 "Return value of Lisp expression read using the minibuffer.\n\
508 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
509 is a string to insert in the minibuffer before reading.")
510 (prompt, initial_contents)
511 Lisp_Object prompt, initial_contents;
513 return Feval (Fread_minibuffer (prompt, initial_contents));
516 /* Functions that use the minibuffer to read various things. */
518 DEFUN ("read-string", Fread_string, Sread_string, 1, 3, 0,
519 "Read a string from the minibuffer, prompting with string PROMPT.\n\
520 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.\n\
521 The third arg HISTORY, if non-nil, specifies a history list\n\
522 and optionally the initial position in the list.\n\
523 See `read-from-minibuffer' for details of HISTORY argument.")
524 (prompt, initial_input, history)
525 Lisp_Object prompt, initial_input, history;
527 return Fread_from_minibuffer (prompt, initial_input, Qnil, Qnil, history);
530 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 2, 0,
531 "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\
532 Prompt with PROMPT, and provide INIT as an initial value of the input string.")
533 (prompt, init)
534 Lisp_Object prompt, init;
536 CHECK_STRING (prompt, 0);
537 if (! NILP (init))
538 CHECK_STRING (init, 1);
540 return read_minibuf (Vminibuffer_local_ns_map, init, prompt, Qnil, 0,
541 Qminibuffer_history, make_number (0));
544 DEFUN ("read-command", Fread_command, Sread_command, 1, 1, 0,
545 "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\
546 Prompts with PROMPT.")
547 (prompt)
548 Lisp_Object prompt;
550 return Fintern (Fcompleting_read (prompt, Vobarray, Qcommandp, Qt, Qnil, Qnil),
551 Qnil);
554 #ifdef NOTDEF
555 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
556 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
557 Prompts with PROMPT.")
558 (prompt)
559 Lisp_Object prompt;
561 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil),
562 Qnil);
564 #endif /* NOTDEF */
566 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 1, 0,
567 "One arg PROMPT, a string. Read the name of a user variable and return\n\
568 it as a symbol. Prompts with PROMPT.\n\
569 A user variable is one whose documentation starts with a `*' character.")
570 (prompt)
571 Lisp_Object prompt;
573 return Fintern (Fcompleting_read (prompt, Vobarray,
574 Quser_variable_p, Qt, Qnil, Qnil),
575 Qnil);
578 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
579 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
580 Prompts with PROMPT.\n\
581 Optional second arg is value to return if user enters an empty line.\n\
582 If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
583 (prompt, def, require_match)
584 Lisp_Object prompt, def, require_match;
586 Lisp_Object tem;
587 Lisp_Object args[3];
588 struct gcpro gcpro1;
590 if (BUFFERP (def))
591 def = XBUFFER (def)->name;
592 if (!NILP (def))
594 args[0] = build_string ("%s(default %s) ");
595 args[1] = prompt;
596 args[2] = def;
597 prompt = Fformat (3, args);
599 GCPRO1 (def);
600 tem = Fcompleting_read (prompt, Vbuffer_alist, Qnil, require_match, Qnil, Qnil);
601 UNGCPRO;
602 if (XSTRING (tem)->size)
603 return tem;
604 return def;
607 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
608 "Return common substring of all completions of STRING in ALIST.\n\
609 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
610 All that match are compared together; the longest initial sequence\n\
611 common to all matches is returned as a string.\n\
612 If there is no match at all, nil is returned.\n\
613 For an exact match, t is returned.\n\
615 ALIST can be an obarray instead of an alist.\n\
616 Then the print names of all symbols in the obarray are the possible matches.\n\
618 ALIST can also be a function to do the completion itself.\n\
619 It receives three arguments: the values STRING, PREDICATE and nil.\n\
620 Whatever it returns becomes the value of `try-completion'.\n\
622 If optional third argument PREDICATE is non-nil,\n\
623 it is used to test each possible match.\n\
624 The match is a candidate only if PREDICATE returns non-nil.\n\
625 The argument given to PREDICATE is the alist element\n\
626 or the symbol from the obarray.")
627 (string, alist, pred)
628 Lisp_Object string, alist, pred;
630 Lisp_Object bestmatch, tail, elt, eltstring;
631 int bestmatchsize;
632 int compare, matchsize;
633 int list = CONSP (alist) || NILP (alist);
634 int index, obsize;
635 int matchcount = 0;
636 Lisp_Object bucket, zero, end, tem;
637 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
639 CHECK_STRING (string, 0);
640 if (!list && !VECTORP (alist))
641 return call3 (alist, string, pred, Qnil);
643 bestmatch = Qnil;
645 /* If ALIST is not a list, set TAIL just for gc pro. */
646 tail = alist;
647 if (! list)
649 index = 0;
650 obsize = XVECTOR (alist)->size;
651 bucket = XVECTOR (alist)->contents[index];
654 while (1)
656 /* Get the next element of the alist or obarray. */
657 /* Exit the loop if the elements are all used up. */
658 /* elt gets the alist element or symbol.
659 eltstring gets the name to check as a completion. */
661 if (list)
663 if (NILP (tail))
664 break;
665 elt = Fcar (tail);
666 eltstring = Fcar (elt);
667 tail = Fcdr (tail);
669 else
671 if (XFASTINT (bucket) != 0)
673 elt = bucket;
674 eltstring = Fsymbol_name (elt);
675 if (XSYMBOL (bucket)->next)
676 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
677 else
678 XSETFASTINT (bucket, 0);
680 else if (++index >= obsize)
681 break;
682 else
684 bucket = XVECTOR (alist)->contents[index];
685 continue;
689 /* Is this element a possible completion? */
691 if (STRINGP (eltstring)
692 && XSTRING (string)->size <= XSTRING (eltstring)->size
693 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
694 XSTRING (string)->size))
696 /* Yes. */
697 Lisp_Object regexps;
698 Lisp_Object zero;
699 XSETFASTINT (zero, 0);
701 /* Ignore this element if it fails to match all the regexps. */
702 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
703 regexps = XCONS (regexps)->cdr)
705 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
706 if (NILP (tem))
707 break;
709 if (CONSP (regexps))
710 continue;
712 /* Ignore this element if there is a predicate
713 and the predicate doesn't like it. */
715 if (!NILP (pred))
717 if (EQ (pred, Qcommandp))
718 tem = Fcommandp (elt);
719 else
721 GCPRO4 (tail, string, eltstring, bestmatch);
722 tem = call1 (pred, elt);
723 UNGCPRO;
725 if (NILP (tem)) continue;
728 /* Update computation of how much all possible completions match */
730 matchcount++;
731 if (NILP (bestmatch))
732 bestmatch = eltstring, bestmatchsize = XSTRING (eltstring)->size;
733 else
735 compare = min (bestmatchsize, XSTRING (eltstring)->size);
736 matchsize = scmp (XSTRING (bestmatch)->data,
737 XSTRING (eltstring)->data,
738 compare);
739 if (matchsize < 0)
740 matchsize = compare;
741 if (completion_ignore_case)
743 /* If this is an exact match except for case,
744 use it as the best match rather than one that is not an
745 exact match. This way, we get the case pattern
746 of the actual match. */
747 if ((matchsize == XSTRING (eltstring)->size
748 && matchsize < XSTRING (bestmatch)->size)
750 /* If there is more than one exact match ignoring case,
751 and one of them is exact including case,
752 prefer that one. */
753 /* If there is no exact match ignoring case,
754 prefer a match that does not change the case
755 of the input. */
756 ((matchsize == XSTRING (eltstring)->size)
758 (matchsize == XSTRING (bestmatch)->size)
759 && !bcmp (XSTRING (eltstring)->data,
760 XSTRING (string)->data, XSTRING (string)->size)
761 && bcmp (XSTRING (bestmatch)->data,
762 XSTRING (string)->data, XSTRING (string)->size)))
763 bestmatch = eltstring;
765 bestmatchsize = matchsize;
770 if (NILP (bestmatch))
771 return Qnil; /* No completions found */
772 /* If we are ignoring case, and there is no exact match,
773 and no additional text was supplied,
774 don't change the case of what the user typed. */
775 if (completion_ignore_case && bestmatchsize == XSTRING (string)->size
776 && XSTRING (bestmatch)->size > bestmatchsize)
777 return string;
779 /* Return t if the supplied string is an exact match (counting case);
780 it does not require any change to be made. */
781 if (matchcount == 1 && bestmatchsize == XSTRING (string)->size
782 && !bcmp (XSTRING (bestmatch)->data, XSTRING (string)->data,
783 bestmatchsize))
784 return Qt;
786 XSETFASTINT (zero, 0); /* Else extract the part in which */
787 XSETFASTINT (end, bestmatchsize); /* all completions agree */
788 return Fsubstring (bestmatch, zero, end);
791 /* Compare exactly LEN chars of strings at S1 and S2,
792 ignoring case if appropriate.
793 Return -1 if strings match,
794 else number of chars that match at the beginning. */
797 scmp (s1, s2, len)
798 register unsigned char *s1, *s2;
799 int len;
801 register int l = len;
803 if (completion_ignore_case)
805 while (l && DOWNCASE (*s1++) == DOWNCASE (*s2++))
806 l--;
808 else
810 while (l && *s1++ == *s2++)
811 l--;
813 if (l == 0)
814 return -1;
815 else
816 return len - l;
819 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
820 "Search for partial matches to STRING in ALIST.\n\
821 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
822 The value is a list of all the strings from ALIST that match.\n\
824 ALIST can be an obarray instead of an alist.\n\
825 Then the print names of all symbols in the obarray are the possible matches.\n\
827 ALIST can also be a function to do the completion itself.\n\
828 It receives three arguments: the values STRING, PREDICATE and t.\n\
829 Whatever it returns becomes the value of `all-completion'.\n\
831 If optional third argument PREDICATE is non-nil,\n\
832 it is used to test each possible match.\n\
833 The match is a candidate only if PREDICATE returns non-nil.\n\
834 The argument given to PREDICATE is the alist element\n\
835 or the symbol from the obarray.\n\
837 If the optional fourth argument HIDE-SPACES is non-nil,\n\
838 strings in ALIST that start with a space\n\
839 are ignored unless STRING itself starts with a space.")
840 (string, alist, pred, hide_spaces)
841 Lisp_Object string, alist, pred, hide_spaces;
843 Lisp_Object tail, elt, eltstring;
844 Lisp_Object allmatches;
845 int list = CONSP (alist) || NILP (alist);
846 int index, obsize;
847 Lisp_Object bucket, tem;
848 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
850 CHECK_STRING (string, 0);
851 if (!list && !VECTORP (alist))
853 return call3 (alist, string, pred, Qt);
855 allmatches = Qnil;
857 /* If ALIST is not a list, set TAIL just for gc pro. */
858 tail = alist;
859 if (! list)
861 index = 0;
862 obsize = XVECTOR (alist)->size;
863 bucket = XVECTOR (alist)->contents[index];
866 while (1)
868 /* Get the next element of the alist or obarray. */
869 /* Exit the loop if the elements are all used up. */
870 /* elt gets the alist element or symbol.
871 eltstring gets the name to check as a completion. */
873 if (list)
875 if (NILP (tail))
876 break;
877 elt = Fcar (tail);
878 eltstring = Fcar (elt);
879 tail = Fcdr (tail);
881 else
883 if (XFASTINT (bucket) != 0)
885 elt = bucket;
886 eltstring = Fsymbol_name (elt);
887 if (XSYMBOL (bucket)->next)
888 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
889 else
890 XSETFASTINT (bucket, 0);
892 else if (++index >= obsize)
893 break;
894 else
896 bucket = XVECTOR (alist)->contents[index];
897 continue;
901 /* Is this element a possible completion? */
903 if (STRINGP (eltstring)
904 && XSTRING (string)->size <= XSTRING (eltstring)->size
905 /* If HIDE_SPACES, reject alternatives that start with space
906 unless the input starts with space. */
907 && ((XSTRING (string)->size > 0 && XSTRING (string)->data[0] == ' ')
908 || XSTRING (eltstring)->data[0] != ' '
909 || NILP (hide_spaces))
910 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
911 XSTRING (string)->size))
913 /* Yes. */
914 Lisp_Object regexps;
915 Lisp_Object zero;
916 XSETFASTINT (zero, 0);
918 /* Ignore this element if it fails to match all the regexps. */
919 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
920 regexps = XCONS (regexps)->cdr)
922 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
923 if (NILP (tem))
924 break;
926 if (CONSP (regexps))
927 continue;
929 /* Ignore this element if there is a predicate
930 and the predicate doesn't like it. */
932 if (!NILP (pred))
934 if (EQ (pred, Qcommandp))
935 tem = Fcommandp (elt);
936 else
938 GCPRO4 (tail, eltstring, allmatches, string);
939 tem = call1 (pred, elt);
940 UNGCPRO;
942 if (NILP (tem)) continue;
944 /* Ok => put it on the list. */
945 allmatches = Fcons (eltstring, allmatches);
949 return Fnreverse (allmatches);
952 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
953 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
954 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
956 /* This comment supplies the doc string for completing-read,
957 for make-docfile to see. We cannot put this in the real DEFUN
958 due to limits in the Unix cpp.
960 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0,
961 "Read a string in the minibuffer, with completion.\n\
962 Args: PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HIST.\n\
963 PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
964 TABLE is an alist whose elements' cars are strings, or an obarray.\n\
965 PREDICATE limits completion to a subset of TABLE.\n\
966 See `try-completion' and `all-completions' for more details
967 on completion, TABLE, and PREDICATE.\n\
969 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
970 the input is (or completes to) an element of TABLE or is null.\n\
971 If it is also not t, Return does not exit if it does non-null completion.\n\
972 If the input is null, `completing-read' returns nil,\n\
973 regardless of the value of REQUIRE-MATCH.\n\
975 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
976 If it is (STRING . POSITION), the initial input\n\
977 is STRING, but point is placed POSITION characters into the string.\n\
978 HIST, if non-nil, specifies a history list\n\
979 and optionally the initial position in the list.\n\
980 It can be a symbol, which is the history list variable to use,\n\
981 or it can be a cons cell (HISTVAR . HISTPOS).\n\
982 In that case, HISTVAR is the history list variable to use,\n\
983 and HISTPOS is the initial position (the position in the list\n\
984 which INITIAL-CONTENTS corresponds to).\n\
985 Positions are counted starting from 1 at the beginning of the list.\n\
986 Completion ignores case if the ambient value of\n\
987 `completion-ignore-case' is non-nil."
989 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0,
990 0 /* See immediately above */)
991 (prompt, table, pred, require_match, init, hist)
992 Lisp_Object prompt, table, pred, require_match, init, hist;
994 Lisp_Object val, histvar, histpos, position;
995 int pos = 0;
996 int count = specpdl_ptr - specpdl;
997 specbind (Qminibuffer_completion_table, table);
998 specbind (Qminibuffer_completion_predicate, pred);
999 specbind (Qminibuffer_completion_confirm,
1000 EQ (require_match, Qt) ? Qnil : Qt);
1001 last_exact_completion = Qnil;
1003 position = Qnil;
1004 if (!NILP (init))
1006 if (CONSP (init))
1008 position = Fcdr (init);
1009 init = Fcar (init);
1011 CHECK_STRING (init, 0);
1012 if (!NILP (position))
1014 CHECK_NUMBER (position, 0);
1015 /* Convert to distance from end of input. */
1016 pos = XINT (position) - XSTRING (init)->size;
1020 if (SYMBOLP (hist))
1022 histvar = hist;
1023 histpos = Qnil;
1025 else
1027 histvar = Fcar_safe (hist);
1028 histpos = Fcdr_safe (hist);
1030 if (NILP (histvar))
1031 histvar = Qminibuffer_history;
1032 if (NILP (histpos))
1033 XSETFASTINT (histpos, 0);
1035 val = read_minibuf (NILP (require_match)
1036 ? Vminibuffer_local_completion_map
1037 : Vminibuffer_local_must_match_map,
1038 init, prompt, make_number (pos), 0,
1039 histvar, histpos);
1040 return unbind_to (count, val);
1043 /* Temporarily display the string M at the end of the current
1044 minibuffer contents. This is used to display things like
1045 "[No Match]" when the user requests a completion for a prefix
1046 that has no possible completions, and other quick, unobtrusive
1047 messages. */
1049 temp_echo_area_glyphs (m)
1050 char *m;
1052 int osize = ZV;
1053 Lisp_Object oinhibit;
1054 oinhibit = Vinhibit_quit;
1056 /* Clear out any old echo-area message to make way for our new thing. */
1057 message (0);
1059 SET_PT (osize);
1060 insert_string (m);
1061 SET_PT (osize);
1062 Vinhibit_quit = Qt;
1063 Fsit_for (make_number (2), Qnil, Qnil);
1064 del_range (PT, ZV);
1065 if (!NILP (Vquit_flag))
1067 Vquit_flag = Qnil;
1068 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1070 Vinhibit_quit = oinhibit;
1073 Lisp_Object Fminibuffer_completion_help ();
1074 Lisp_Object assoc_for_completion ();
1075 /* A subroutine of Fintern_soft. */
1076 extern Lisp_Object oblookup ();
1079 /* Test whether TXT is an exact completion. */
1080 Lisp_Object
1081 test_completion (txt)
1082 Lisp_Object txt;
1084 Lisp_Object tem;
1086 if (CONSP (Vminibuffer_completion_table)
1087 || NILP (Vminibuffer_completion_table))
1088 return assoc_for_completion (txt, Vminibuffer_completion_table);
1089 else if (VECTORP (Vminibuffer_completion_table))
1091 /* Bypass intern-soft as that loses for nil */
1092 tem = oblookup (Vminibuffer_completion_table,
1093 XSTRING (txt)->data, XSTRING (txt)->size);
1094 if (!SYMBOLP (tem))
1095 return Qnil;
1096 else if (!NILP (Vminibuffer_completion_predicate))
1097 return call1 (Vminibuffer_completion_predicate, tem);
1098 else
1099 return Qt;
1101 else
1102 return call3 (Vminibuffer_completion_table, txt,
1103 Vminibuffer_completion_predicate, Qlambda);
1106 /* returns:
1107 * 0 no possible completion
1108 * 1 was already an exact and unique completion
1109 * 3 was already an exact completion
1110 * 4 completed to an exact completion
1111 * 5 some completion happened
1112 * 6 no completion happened
1115 do_completion ()
1117 Lisp_Object completion, tem;
1118 int completedp;
1119 Lisp_Object last;
1120 struct gcpro gcpro1, gcpro2;
1122 completion = Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table,
1123 Vminibuffer_completion_predicate);
1124 last = last_exact_completion;
1125 last_exact_completion = Qnil;
1127 GCPRO2 (completion, last);
1129 if (NILP (completion))
1131 bitch_at_user ();
1132 temp_echo_area_glyphs (" [No match]");
1133 UNGCPRO;
1134 return 0;
1137 if (EQ (completion, Qt)) /* exact and unique match */
1139 UNGCPRO;
1140 return 1;
1143 /* compiler bug */
1144 tem = Fstring_equal (completion, Fbuffer_string());
1145 if (completedp = NILP (tem))
1147 Ferase_buffer (); /* Some completion happened */
1148 Finsert (1, &completion);
1151 /* It did find a match. Do we match some possibility exactly now? */
1152 tem = test_completion (Fbuffer_string ());
1153 if (NILP (tem))
1155 /* not an exact match */
1156 UNGCPRO;
1157 if (completedp)
1158 return 5;
1159 else if (auto_help)
1160 Fminibuffer_completion_help ();
1161 else
1162 temp_echo_area_glyphs (" [Next char not unique]");
1163 return 6;
1165 else if (completedp)
1167 UNGCPRO;
1168 return 4;
1170 /* If the last exact completion and this one were the same,
1171 it means we've already given a "Complete but not unique"
1172 message and the user's hit TAB again, so now we give him help. */
1173 last_exact_completion = completion;
1174 if (!NILP (last))
1176 tem = Fbuffer_string ();
1177 if (!NILP (Fequal (tem, last)))
1178 Fminibuffer_completion_help ();
1180 UNGCPRO;
1181 return 3;
1184 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1186 Lisp_Object
1187 assoc_for_completion (key, list)
1188 register Lisp_Object key;
1189 Lisp_Object list;
1191 register Lisp_Object tail;
1193 if (completion_ignore_case)
1194 key = Fupcase (key);
1196 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1198 register Lisp_Object elt, tem, thiscar;
1199 elt = Fcar (tail);
1200 if (!CONSP (elt)) continue;
1201 thiscar = Fcar (elt);
1202 if (!STRINGP (thiscar))
1203 continue;
1204 if (completion_ignore_case)
1205 thiscar = Fupcase (thiscar);
1206 tem = Fequal (thiscar, key);
1207 if (!NILP (tem)) return elt;
1208 QUIT;
1210 return Qnil;
1213 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1214 "Complete the minibuffer contents as far as possible.\n\
1215 Return nil if there is no valid completion, else t.\n\
1216 If no characters can be completed, display a list of possible completions.\n\
1217 If you repeat this command after it displayed such a list,\n\
1218 scroll the window of possible completions.")
1221 register int i;
1222 Lisp_Object window, tem;
1224 /* If the previous command was not this, then mark the completion
1225 buffer obsolete. */
1226 if (! EQ (current_kboard->Vlast_command, this_command))
1227 Vminibuf_scroll_window = Qnil;
1229 window = Vminibuf_scroll_window;
1230 /* If there's a fresh completion window with a live buffer,
1231 and this command is repeated, scroll that window. */
1232 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1233 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1235 struct buffer *obuf = current_buffer;
1237 Fset_buffer (XWINDOW (window)->buffer);
1238 tem = Fpos_visible_in_window_p (make_number (ZV), window);
1239 if (! NILP (tem))
1240 /* If end is in view, scroll up to the beginning. */
1241 Fset_window_start (window, BEGV, Qnil);
1242 else
1243 /* Else scroll down one screen. */
1244 Fscroll_other_window (Qnil);
1246 set_buffer_internal (obuf);
1247 return Qnil;
1250 i = do_completion ();
1251 switch (i)
1253 case 0:
1254 return Qnil;
1256 case 1:
1257 temp_echo_area_glyphs (" [Sole completion]");
1258 break;
1260 case 3:
1261 temp_echo_area_glyphs (" [Complete, but not unique]");
1262 break;
1265 return Qt;
1268 /* Subroutines of Fminibuffer_complete_and_exit. */
1270 /* This one is called by internal_condition_case to do the real work. */
1272 Lisp_Object
1273 complete_and_exit_1 ()
1275 return make_number (do_completion ());
1278 /* This one is called by internal_condition_case if an error happens.
1279 Pretend the current value is an exact match. */
1281 Lisp_Object
1282 complete_and_exit_2 (ignore)
1283 Lisp_Object ignore;
1285 return make_number (1);
1288 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1289 Sminibuffer_complete_and_exit, 0, 0, "",
1290 "If the minibuffer contents is a valid completion then exit.\n\
1291 Otherwise try to complete it. If completion leads to a valid completion,\n\
1292 a repetition of this command will exit.")
1295 register int i;
1296 Lisp_Object val;
1298 /* Allow user to specify null string */
1299 if (BEGV == ZV)
1300 goto exit;
1302 if (!NILP (test_completion (Fbuffer_string ())))
1303 goto exit;
1305 /* Call do_completion, but ignore errors. */
1306 val = internal_condition_case (complete_and_exit_1, Qerror,
1307 complete_and_exit_2);
1309 i = XFASTINT (val);
1310 switch (i)
1312 case 1:
1313 case 3:
1314 goto exit;
1316 case 4:
1317 if (!NILP (Vminibuffer_completion_confirm))
1319 temp_echo_area_glyphs (" [Confirm]");
1320 return Qnil;
1322 else
1323 goto exit;
1325 default:
1326 return Qnil;
1328 exit:
1329 Fthrow (Qexit, Qnil);
1330 /* NOTREACHED */
1333 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1334 0, 0, "",
1335 "Complete the minibuffer contents at most a single word.\n\
1336 After one word is completed as much as possible, a space or hyphen\n\
1337 is added, provided that matches some possible completion.\n\
1338 Return nil if there is no valid completion, else t.")
1341 Lisp_Object completion, tem;
1342 register int i;
1343 register unsigned char *completion_string;
1344 struct gcpro gcpro1, gcpro2;
1346 /* We keep calling Fbuffer_string rather than arrange for GC to
1347 hold onto a pointer to one of the strings thus made. */
1349 completion = Ftry_completion (Fbuffer_string (),
1350 Vminibuffer_completion_table,
1351 Vminibuffer_completion_predicate);
1352 if (NILP (completion))
1354 bitch_at_user ();
1355 temp_echo_area_glyphs (" [No match]");
1356 return Qnil;
1358 if (EQ (completion, Qt))
1359 return Qnil;
1361 #if 0 /* How the below code used to look, for reference. */
1362 tem = Fbuffer_string ();
1363 b = XSTRING (tem)->data;
1364 i = ZV - 1 - XSTRING (completion)->size;
1365 p = XSTRING (completion)->data;
1366 if (i > 0 ||
1367 0 <= scmp (b, p, ZV - 1))
1369 i = 1;
1370 /* Set buffer to longest match of buffer tail and completion head. */
1371 while (0 <= scmp (b + i, p, ZV - 1 - i))
1372 i++;
1373 del_range (1, i + 1);
1374 SET_PT (ZV);
1376 #else /* Rewritten code */
1378 register unsigned char *buffer_string;
1379 int buffer_length, completion_length;
1381 tem = Fbuffer_string ();
1382 GCPRO2 (completion, tem);
1383 /* If reading a file name,
1384 expand any $ENVVAR refs in the buffer and in TEM. */
1385 if (EQ (Vminibuffer_completion_table, Qread_file_name_internal))
1387 Lisp_Object substituted;
1388 substituted = Fsubstitute_in_file_name (tem);
1389 if (! EQ (substituted, tem))
1391 tem = substituted;
1392 Ferase_buffer ();
1393 insert_from_string (tem, 0, XSTRING (tem)->size, 0);
1396 buffer_string = XSTRING (tem)->data;
1397 completion_string = XSTRING (completion)->data;
1398 buffer_length = XSTRING (tem)->size; /* ie ZV - BEGV */
1399 completion_length = XSTRING (completion)->size;
1400 i = buffer_length - completion_length;
1401 /* Mly: I don't understand what this is supposed to do AT ALL */
1402 if (i > 0 ||
1403 0 <= scmp (buffer_string, completion_string, buffer_length))
1405 /* Set buffer to longest match of buffer tail and completion head. */
1406 if (i <= 0) i = 1;
1407 buffer_string += i;
1408 buffer_length -= i;
1409 while (0 <= scmp (buffer_string++, completion_string, buffer_length--))
1410 i++;
1411 del_range (1, i + 1);
1412 SET_PT (ZV);
1414 UNGCPRO;
1416 #endif /* Rewritten code */
1417 i = ZV - BEGV;
1419 /* If completion finds next char not unique,
1420 consider adding a space or a hyphen. */
1421 if (i == XSTRING (completion)->size)
1423 GCPRO1 (completion);
1424 tem = Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")),
1425 Vminibuffer_completion_table,
1426 Vminibuffer_completion_predicate);
1427 UNGCPRO;
1429 if (STRINGP (tem))
1430 completion = tem;
1431 else
1433 GCPRO1 (completion);
1434 tem =
1435 Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")),
1436 Vminibuffer_completion_table,
1437 Vminibuffer_completion_predicate);
1438 UNGCPRO;
1440 if (STRINGP (tem))
1441 completion = tem;
1445 /* Now find first word-break in the stuff found by completion.
1446 i gets index in string of where to stop completing. */
1448 completion_string = XSTRING (completion)->data;
1450 for (; i < XSTRING (completion)->size; i++)
1451 if (SYNTAX (completion_string[i]) != Sword) break;
1452 if (i < XSTRING (completion)->size)
1453 i = i + 1;
1455 /* If got no characters, print help for user. */
1457 if (i == ZV - BEGV)
1459 if (auto_help)
1460 Fminibuffer_completion_help ();
1461 return Qnil;
1464 /* Otherwise insert in minibuffer the chars we got */
1466 Ferase_buffer ();
1467 insert_from_string (completion, 0, i, 1);
1468 return Qt;
1471 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
1472 1, 1, 0,
1473 "Display the list of completions, COMPLETIONS, using `standard-output'.\n\
1474 Each element may be just a symbol or string\n\
1475 or may be a list of two strings to be printed as if concatenated.\n\
1476 `standard-output' must be a buffer.\n\
1477 At the end, run the normal hook `completion-setup-hook'.\n\
1478 It can find the completion buffer in `standard-output'.")
1479 (completions)
1480 Lisp_Object completions;
1482 Lisp_Object tail, elt;
1483 register int i;
1484 int column = 0;
1485 struct gcpro gcpro1, gcpro2;
1486 struct buffer *old = current_buffer;
1487 int first = 1;
1489 /* Note that (when it matters) every variable
1490 points to a non-string that is pointed to by COMPLETIONS,
1491 except for ELT. ELT can be pointing to a string
1492 when terpri or Findent_to calls a change hook. */
1493 elt = Qnil;
1494 GCPRO2 (completions, elt);
1496 if (BUFFERP (Vstandard_output))
1497 set_buffer_internal (XBUFFER (Vstandard_output));
1499 if (NILP (completions))
1500 write_string ("There are no possible completions of what you have typed.",
1501 -1);
1502 else
1504 write_string ("Possible completions are:", -1);
1505 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
1507 Lisp_Object tem;
1508 int length;
1509 Lisp_Object startpos, endpos;
1511 elt = Fcar (tail);
1512 /* Compute the length of this element. */
1513 if (CONSP (elt))
1515 tem = Fcar (elt);
1516 CHECK_STRING (tem, 0);
1517 length = XINT (XSTRING (tem)->size);
1519 tem = Fcar (Fcdr (elt));
1520 CHECK_STRING (tem, 0);
1521 length += XINT (XSTRING (tem)->size);
1523 else
1525 CHECK_STRING (elt, 0);
1526 length = XINT (XSTRING (elt)->size);
1529 /* This does a bad job for narrower than usual windows.
1530 Sadly, the window it will appear in is not known
1531 until after the text has been made. */
1533 if (BUFFERP (Vstandard_output))
1534 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
1536 /* If the previous completion was very wide,
1537 or we have two on this line already,
1538 don't put another on the same line. */
1539 if (column > 33 || first
1540 /* If this is really wide, don't put it second on a line. */
1541 || column > 0 && length > 45)
1543 Fterpri (Qnil);
1544 column = 0;
1546 /* Otherwise advance to column 35. */
1547 else
1549 if (BUFFERP (Vstandard_output))
1551 tem = Findent_to (make_number (35), make_number (2));
1553 column = XINT (tem);
1555 else
1559 write_string (" ", -1);
1560 column++;
1562 while (column < 35);
1566 if (BUFFERP (Vstandard_output))
1568 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
1569 Fset_text_properties (startpos, endpos,
1570 Qnil, Vstandard_output);
1573 /* Output this element and update COLUMN. */
1574 if (CONSP (elt))
1576 Fprinc (Fcar (elt), Qnil);
1577 Fprinc (Fcar (Fcdr (elt)), Qnil);
1579 else
1580 Fprinc (elt, Qnil);
1582 column += length;
1584 /* If output is to a buffer, recompute COLUMN in a way
1585 that takes account of character widths. */
1586 if (BUFFERP (Vstandard_output))
1588 tem = Fcurrent_column ();
1589 column = XINT (tem);
1592 first = 0;
1596 UNGCPRO;
1598 if (BUFFERP (Vstandard_output))
1599 set_buffer_internal (old);
1601 if (!NILP (Vrun_hooks))
1602 call1 (Vrun_hooks, intern ("completion-setup-hook"));
1604 return Qnil;
1607 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
1608 0, 0, "",
1609 "Display a list of possible completions of the current minibuffer contents.")
1612 Lisp_Object completions;
1614 message ("Making completion list...");
1615 completions = Fall_completions (Fbuffer_string (),
1616 Vminibuffer_completion_table,
1617 Vminibuffer_completion_predicate,
1618 Qt);
1619 echo_area_glyphs = 0;
1621 if (NILP (completions))
1623 bitch_at_user ();
1624 temp_echo_area_glyphs (" [No completions]");
1626 else
1627 internal_with_output_to_temp_buffer ("*Completions*",
1628 Fdisplay_completion_list,
1629 Fsort (completions, Qstring_lessp));
1630 return Qnil;
1633 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
1634 "Terminate minibuffer input.")
1637 if (INTEGERP (last_command_char))
1638 internal_self_insert (last_command_char, 0);
1639 else
1640 bitch_at_user ();
1642 Fthrow (Qexit, Qnil);
1645 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
1646 "Terminate this minibuffer argument.")
1649 Fthrow (Qexit, Qnil);
1652 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
1653 "Return current depth of activations of minibuffer, a nonnegative integer.")
1656 return make_number (minibuf_level);
1659 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
1660 "Return the prompt string of the currently-active minibuffer.\n\
1661 If no minibuffer is active, return nil.")
1664 return Fcopy_sequence (minibuf_prompt);
1667 DEFUN ("minibuffer-prompt-width", Fminibuffer_prompt_width,
1668 Sminibuffer_prompt_width, 0, 0, 0,
1669 "Return the display width of the minibuffer prompt.")
1672 Lisp_Object width;
1673 XSETFASTINT (width, minibuf_prompt_width);
1674 return width;
1677 init_minibuf_once ()
1679 Vminibuffer_list = Qnil;
1680 staticpro (&Vminibuffer_list);
1683 syms_of_minibuf ()
1685 minibuf_level = 0;
1686 minibuf_prompt = Qnil;
1687 staticpro (&minibuf_prompt);
1689 minibuf_save_list = Qnil;
1690 staticpro (&minibuf_save_list);
1692 Qread_file_name_internal = intern ("read-file-name-internal");
1693 staticpro (&Qread_file_name_internal);
1695 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
1696 staticpro (&Qminibuffer_completion_table);
1698 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
1699 staticpro (&Qminibuffer_completion_confirm);
1701 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
1702 staticpro (&Qminibuffer_completion_predicate);
1704 staticpro (&last_exact_completion);
1705 last_exact_completion = Qnil;
1707 staticpro (&last_minibuf_string);
1708 last_minibuf_string = Qnil;
1710 Quser_variable_p = intern ("user-variable-p");
1711 staticpro (&Quser_variable_p);
1713 Qminibuffer_history = intern ("minibuffer-history");
1714 staticpro (&Qminibuffer_history);
1716 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
1717 staticpro (&Qminibuffer_setup_hook);
1719 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
1720 staticpro (&Qminibuffer_exit_hook);
1722 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
1723 "Normal hook run just after entry to minibuffer.");
1724 Vminibuffer_setup_hook = Qnil;
1726 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
1727 "Normal hook run just after exit from minibuffer.");
1728 Vminibuffer_exit_hook = Qnil;
1730 DEFVAR_BOOL ("completion-auto-help", &auto_help,
1731 "*Non-nil means automatically provide help for invalid completion input.");
1732 auto_help = 1;
1734 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
1735 "Non-nil means don't consider case significant in completion.");
1736 completion_ignore_case = 0;
1738 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
1739 "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\
1740 More precisely, this variable makes a difference when the minibuffer window\n\
1741 is the selected window. If you are in some other window, minibuffer commands\n\
1742 are allowed even if a minibuffer is active.");
1743 enable_recursive_minibuffers = 0;
1745 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
1746 "Alist or obarray used for completion in the minibuffer.\n\
1747 This becomes the ALIST argument to `try-completion' and `all-completion'.\n\
1749 The value may alternatively be a function, which is given three arguments:\n\
1750 STRING, the current buffer contents;\n\
1751 PREDICATE, the predicate for filtering possible matches;\n\
1752 CODE, which says what kind of things to do.\n\
1753 CODE can be nil, t or `lambda'.\n\
1754 nil means to return the best completion of STRING, or nil if there is none.\n\
1755 t means to return a list of all possible completions of STRING.\n\
1756 `lambda' means to return t if STRING is a valid completion as it stands.");
1757 Vminibuffer_completion_table = Qnil;
1759 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
1760 "Within call to `completing-read', this holds the PREDICATE argument.");
1761 Vminibuffer_completion_predicate = Qnil;
1763 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
1764 "Non-nil => demand confirmation of completion before exiting minibuffer.");
1765 Vminibuffer_completion_confirm = Qnil;
1767 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
1768 "Value that `help-form' takes on inside the minibuffer.");
1769 Vminibuffer_help_form = Qnil;
1771 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
1772 "History list symbol to add minibuffer values to.\n\
1773 Each minibuffer output is added with\n\
1774 (set minibuffer-history-variable\n\
1775 (cons STRING (symbol-value minibuffer-history-variable)))");
1776 XSETFASTINT (Vminibuffer_history_variable, 0);
1778 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
1779 "Current position of redoing in the history list.");
1780 Vminibuffer_history_position = Qnil;
1782 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
1783 "*Non-nil means entering the minibuffer raises the minibuffer's frame.");
1784 minibuffer_auto_raise = 0;
1786 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
1787 "List of regexps that should restrict possible completions.");
1788 Vcompletion_regexp_list = Qnil;
1790 defsubr (&Sread_from_minibuffer);
1791 defsubr (&Seval_minibuffer);
1792 defsubr (&Sread_minibuffer);
1793 defsubr (&Sread_string);
1794 defsubr (&Sread_command);
1795 defsubr (&Sread_variable);
1796 defsubr (&Sread_buffer);
1797 defsubr (&Sread_no_blanks_input);
1798 defsubr (&Sminibuffer_depth);
1799 defsubr (&Sminibuffer_prompt);
1800 defsubr (&Sminibuffer_prompt_width);
1802 defsubr (&Stry_completion);
1803 defsubr (&Sall_completions);
1804 defsubr (&Scompleting_read);
1805 defsubr (&Sminibuffer_complete);
1806 defsubr (&Sminibuffer_complete_word);
1807 defsubr (&Sminibuffer_complete_and_exit);
1808 defsubr (&Sdisplay_completion_list);
1809 defsubr (&Sminibuffer_completion_help);
1811 defsubr (&Sself_insert_and_exit);
1812 defsubr (&Sexit_minibuffer);
1816 keys_of_minibuf ()
1818 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
1819 "abort-recursive-edit");
1820 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
1821 "exit-minibuffer");
1822 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
1823 "exit-minibuffer");
1825 initial_define_key (Vminibuffer_local_ns_map, Ctl ('g'),
1826 "abort-recursive-edit");
1827 initial_define_key (Vminibuffer_local_ns_map, Ctl ('m'),
1828 "exit-minibuffer");
1829 initial_define_key (Vminibuffer_local_ns_map, Ctl ('j'),
1830 "exit-minibuffer");
1832 initial_define_key (Vminibuffer_local_ns_map, ' ',
1833 "exit-minibuffer");
1834 initial_define_key (Vminibuffer_local_ns_map, '\t',
1835 "exit-minibuffer");
1836 initial_define_key (Vminibuffer_local_ns_map, '?',
1837 "self-insert-and-exit");
1839 initial_define_key (Vminibuffer_local_completion_map, Ctl ('g'),
1840 "abort-recursive-edit");
1841 initial_define_key (Vminibuffer_local_completion_map, Ctl ('m'),
1842 "exit-minibuffer");
1843 initial_define_key (Vminibuffer_local_completion_map, Ctl ('j'),
1844 "exit-minibuffer");
1846 initial_define_key (Vminibuffer_local_completion_map, '\t',
1847 "minibuffer-complete");
1848 initial_define_key (Vminibuffer_local_completion_map, ' ',
1849 "minibuffer-complete-word");
1850 initial_define_key (Vminibuffer_local_completion_map, '?',
1851 "minibuffer-completion-help");
1853 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('g'),
1854 "abort-recursive-edit");
1855 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
1856 "minibuffer-complete-and-exit");
1857 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
1858 "minibuffer-complete-and-exit");
1859 initial_define_key (Vminibuffer_local_must_match_map, '\t',
1860 "minibuffer-complete");
1861 initial_define_key (Vminibuffer_local_must_match_map, ' ',
1862 "minibuffer-complete-word");
1863 initial_define_key (Vminibuffer_local_must_match_map, '?',
1864 "minibuffer-completion-help");