(iso-transl-dead-key-alist): Fix syntax for ?^.
[emacs.git] / src / minibuf.c
blob2abb46710e534f6bc987b0a9bb8631509b93cc93
1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 93, 94, 95, 1996 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "commands.h"
25 #include "buffer.h"
26 #include "dispextern.h"
27 #include "frame.h"
28 #include "window.h"
29 #include "syntax.h"
30 #include "keyboard.h"
32 #define min(a, b) ((a) < (b) ? (a) : (b))
34 extern int quit_char;
36 /* List of buffers for use as minibuffers.
37 The first element of the list is used for the outermost minibuffer
38 invocation, the next element is used for a recursive minibuffer
39 invocation, etc. The list is extended at the end as deeper
40 minibuffer recursions are encountered. */
41 Lisp_Object Vminibuffer_list;
43 /* Data to remember during recursive minibuffer invocations */
44 Lisp_Object minibuf_save_list;
46 /* Depth in minibuffer invocations. */
47 int minibuf_level;
49 /* Nonzero means display completion help for invalid input */
50 int auto_help;
52 /* Fread_minibuffer leaves the input here as a string. */
53 Lisp_Object last_minibuf_string;
55 /* Nonzero means let functions called when within a minibuffer
56 invoke recursive minibuffers (to read arguments, or whatever) */
57 int enable_recursive_minibuffers;
59 /* help-form is bound to this while in the minibuffer. */
61 Lisp_Object Vminibuffer_help_form;
63 /* Variable which is the history list to add minibuffer values to. */
65 Lisp_Object Vminibuffer_history_variable;
67 /* Current position in the history list (adjusted by M-n and M-p). */
69 Lisp_Object Vminibuffer_history_position;
71 Lisp_Object Qminibuffer_history;
73 Lisp_Object Qread_file_name_internal;
75 /* Normal hooks for entry to and exit from minibuffer. */
77 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
78 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
80 /* Nonzero means completion ignores case. */
82 int completion_ignore_case;
84 /* List of regexps that should restrict possible completions. */
86 Lisp_Object Vcompletion_regexp_list;
88 /* Nonzero means raise the minibuffer frame when the minibuffer
89 is entered. */
91 int minibuffer_auto_raise;
93 /* If last completion attempt reported "Complete but not unique"
94 then this is the string completed then; otherwise this is nil. */
96 static Lisp_Object last_exact_completion;
98 Lisp_Object Quser_variable_p;
100 /* Non-nil means it is the window for C-M-v to scroll
101 when the minibuffer is selected. */
102 extern Lisp_Object Vminibuf_scroll_window;
104 extern Lisp_Object Voverriding_local_map;
106 /* Put minibuf on currently selected frame's minibuffer.
107 We do this whenever the user starts a new minibuffer
108 or when a minibuffer exits. */
110 void
111 choose_minibuf_frame ()
113 if (selected_frame != 0
114 && !EQ (minibuf_window, selected_frame->minibuffer_window))
116 #if defined(MSDOS) && !defined(HAVE_X_WINDOWS)
117 selected_frame->minibuffer_window = minibuf_window;
118 #else
119 /* I don't think that any frames may validly have a null minibuffer
120 window anymore. */
121 if (NILP (selected_frame->minibuffer_window))
122 abort ();
124 Fset_window_buffer (selected_frame->minibuffer_window,
125 XWINDOW (minibuf_window)->buffer);
126 minibuf_window = selected_frame->minibuffer_window;
127 #endif
131 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
132 Sset_minibuffer_window, 1, 1, 0,
133 "Specify which minibuffer window to use for the minibuffer.\n\
134 This effects where the minibuffer is displayed if you put text in it\n\
135 without invoking the usual minibuffer commands.")
136 (window)
137 Lisp_Object window;
139 CHECK_WINDOW (window, 1);
140 if (! MINI_WINDOW_P (XWINDOW (window)))
141 error ("Window is not a minibuffer window");
143 minibuf_window = window;
145 return window;
149 /* Actual minibuffer invocation. */
151 void read_minibuf_unwind ();
152 Lisp_Object get_minibuffer ();
153 Lisp_Object read_minibuf ();
155 /* Read from the minibuffer using keymap MAP, initial contents INITIAL
156 (a string), putting point minus BACKUP_N chars from the end of INITIAL,
157 prompting with PROMPT (a string), using history list HISTVAR
158 with initial position HISTPOS. (BACKUP_N should be <= 0.)
160 Normally return the result as a string (the text that was read),
161 but if EXPFLAG is nonzero, read it and return the object read.
162 If HISTVAR is given, save the value read on that history only if it doesn't
163 match the front of that history list exactly. The value is pushed onto
164 the list as the string that was read. */
166 Lisp_Object
167 read_minibuf (map, initial, prompt, backup_n, expflag, histvar, histpos)
168 Lisp_Object map;
169 Lisp_Object initial;
170 Lisp_Object prompt;
171 Lisp_Object backup_n;
172 int expflag;
173 Lisp_Object histvar;
174 Lisp_Object histpos;
176 Lisp_Object val;
177 int count = specpdl_ptr - specpdl;
178 Lisp_Object mini_frame;
179 struct gcpro gcpro1, gcpro2, gcpro3;
181 single_kboard_state ();
183 val = Qnil;
184 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
185 store them away before we can GC. Don't need to protect
186 BACKUP_N because we use the value only if it is an integer. */
187 GCPRO3 (map, initial, val);
189 if (!STRINGP (prompt))
190 prompt = build_string ("");
192 if (!enable_recursive_minibuffers
193 && minibuf_level > 0
194 && (EQ (selected_window, minibuf_window)))
195 error ("Command attempted to use minibuffer while in minibuffer");
197 /* Could we simply bind these variables instead? */
198 minibuf_save_list
199 = Fcons (Voverriding_local_map,
200 Fcons (minibuf_window, minibuf_save_list));
201 minibuf_save_list
202 = Fcons (minibuf_prompt,
203 Fcons (make_number (minibuf_prompt_width),
204 Fcons (Vhelp_form,
205 Fcons (Vcurrent_prefix_arg,
206 Fcons (Vminibuffer_history_position,
207 Fcons (Vminibuffer_history_variable,
208 minibuf_save_list))))));
210 minibuf_prompt_width = 0; /* xdisp.c puts in the right value. */
211 minibuf_prompt = Fcopy_sequence (prompt);
212 Vminibuffer_history_position = histpos;
213 Vminibuffer_history_variable = histvar;
215 choose_minibuf_frame ();
217 record_unwind_protect (Fset_window_configuration,
218 Fcurrent_window_configuration (Qnil));
220 /* If the minibuffer window is on a different frame, save that
221 frame's configuration too. */
222 #ifdef MULTI_FRAME
223 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
224 if (XFRAME (mini_frame) != selected_frame)
225 record_unwind_protect (Fset_window_configuration,
226 Fcurrent_window_configuration (mini_frame));
228 /* If the minibuffer is on an iconified or invisible frame,
229 make it visible now. */
230 Fmake_frame_visible (mini_frame);
232 if (minibuffer_auto_raise)
233 Fraise_frame (mini_frame);
234 #endif
236 val = current_buffer->directory;
237 Fset_buffer (get_minibuffer (minibuf_level));
239 /* The current buffer's default directory is usually the right thing
240 for our minibuffer here. However, if you're typing a command at
241 a minibuffer-only frame when minibuf_level is zero, then buf IS
242 the current_buffer, so reset_buffer leaves buf's default
243 directory unchanged. This is a bummer when you've just started
244 up Emacs and buf's default directory is Qnil. Here's a hack; can
245 you think of something better to do? Find another buffer with a
246 better directory, and use that one instead. */
247 if (STRINGP (val))
248 current_buffer->directory = val;
249 else
251 Lisp_Object buf_list;
253 for (buf_list = Vbuffer_alist;
254 CONSP (buf_list);
255 buf_list = XCONS (buf_list)->cdr)
257 Lisp_Object other_buf;
259 other_buf = XCONS (XCONS (buf_list)->car)->cdr;
260 if (STRINGP (XBUFFER (other_buf)->directory))
262 current_buffer->directory = XBUFFER (other_buf)->directory;
263 break;
268 #ifdef MULTI_FRAME
269 if (XFRAME (mini_frame) != selected_frame)
270 Fredirect_frame_focus (Fselected_frame (), mini_frame);
271 #endif
272 Fmake_local_variable (Qprint_escape_newlines);
273 print_escape_newlines = 1;
275 record_unwind_protect (read_minibuf_unwind, Qnil);
277 Vminibuf_scroll_window = selected_window;
278 Fset_window_buffer (minibuf_window, Fcurrent_buffer ());
279 Fselect_window (minibuf_window);
280 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
283 int count1 = specpdl_ptr - specpdl;
284 specbind (Qinhibit_read_only, Qt);
285 Ferase_buffer ();
286 unbind_to (count1, Qnil);
289 minibuf_level++;
291 if (!NILP (initial))
293 Finsert (1, &initial);
294 if (!NILP (backup_n) && INTEGERP (backup_n))
295 Fforward_char (backup_n);
298 echo_area_glyphs = 0;
299 /* This is in case the minibuffer-setup-hook calls Fsit_for. */
300 previous_echo_glyphs = 0;
302 Vhelp_form = Vminibuffer_help_form;
303 current_buffer->keymap = map;
305 /* Run our hook, but not if it is empty.
306 (run-hooks would do nothing if it is empty,
307 but it's important to save time here in the usual case. */
308 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
309 && !NILP (Vrun_hooks))
310 call1 (Vrun_hooks, Qminibuffer_setup_hook);
312 /* ??? MCC did redraw_screen here if switching screens. */
313 recursive_edit_1 ();
315 /* If cursor is on the minibuffer line,
316 show the user we have exited by putting it in column 0. */
317 if ((FRAME_CURSOR_Y (selected_frame)
318 >= XFASTINT (XWINDOW (minibuf_window)->top))
319 && !noninteractive)
321 FRAME_CURSOR_X (selected_frame) = 0;
322 update_frame (selected_frame, 1, 1);
325 /* Make minibuffer contents into a string */
326 val = make_buffer_string (1, Z, 1);
327 #if 0 /* make_buffer_string should handle the gap. */
328 bcopy (GAP_END_ADDR, XSTRING (val)->data + GPT - BEG, Z - GPT);
329 #endif
331 /* VAL is the string of minibuffer text. */
332 last_minibuf_string = val;
334 /* Add the value to the appropriate history list unless it is empty. */
335 if (XSTRING (val)->size != 0
336 && SYMBOLP (Vminibuffer_history_variable)
337 && ! EQ (XSYMBOL (Vminibuffer_history_variable)->value, Qunbound))
339 /* If the caller wanted to save the value read on a history list,
340 then do so if the value is not already the front of the list. */
341 Lisp_Object histval;
342 histval = Fsymbol_value (Vminibuffer_history_variable);
344 /* The value of the history variable must be a cons or nil. Other
345 values are unacceptable. We silently ignore these values. */
346 if (NILP (histval)
347 || (CONSP (histval)
348 && NILP (Fequal (last_minibuf_string, Fcar (histval)))))
349 Fset (Vminibuffer_history_variable,
350 Fcons (last_minibuf_string, histval));
353 /* If Lisp form desired instead of string, parse it. */
354 if (expflag)
356 Lisp_Object expr_and_pos;
357 unsigned char *p;
359 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
360 /* Ignore trailing whitespace; any other trailing junk is an error. */
361 for (p = XSTRING (val)->data + XINT (Fcdr (expr_and_pos)); *p; p++)
362 if (*p != ' ' && *p != '\t' && *p != '\n')
363 error ("Trailing garbage following expression");
364 val = Fcar (expr_and_pos);
367 /* The appropriate frame will get selected
368 in set-window-configuration. */
369 RETURN_UNGCPRO (unbind_to (count, val));
372 /* Return a buffer to be used as the minibuffer at depth `depth'.
373 depth = 0 is the lowest allowed argument, and that is the value
374 used for nonrecursive minibuffer invocations */
376 Lisp_Object
377 get_minibuffer (depth)
378 int depth;
380 Lisp_Object tail, num, buf;
381 char name[24];
382 extern Lisp_Object nconc2 ();
384 XSETFASTINT (num, depth);
385 tail = Fnthcdr (num, Vminibuffer_list);
386 if (NILP (tail))
388 tail = Fcons (Qnil, Qnil);
389 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
391 buf = Fcar (tail);
392 if (NILP (buf) || NILP (XBUFFER (buf)->name))
394 sprintf (name, " *Minibuf-%d*", depth);
395 buf = Fget_buffer_create (build_string (name));
397 /* Although the buffer's name starts with a space, undo should be
398 enabled in it. */
399 Fbuffer_enable_undo (buf);
401 XCONS (tail)->car = buf;
403 else
405 int count = specpdl_ptr - specpdl;
407 reset_buffer (XBUFFER (buf));
408 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
409 Fset_buffer (buf);
410 Fkill_all_local_variables ();
411 unbind_to (count, Qnil);
414 return buf;
417 /* This function is called on exiting minibuffer, whether normally or not,
418 and it restores the current window, buffer, etc. */
420 void
421 read_minibuf_unwind (data)
422 Lisp_Object data;
424 Lisp_Object old_deactivate_mark;
425 Lisp_Object window;
427 /* We are exiting the minibuffer one way or the other,
428 so run the hook. */
429 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
430 && !NILP (Vrun_hooks))
431 safe_run_hooks (Qminibuffer_exit_hook);
433 /* If this was a recursive minibuffer,
434 tie the minibuffer window back to the outer level minibuffer buffer. */
435 minibuf_level--;
437 window = minibuf_window;
438 /* To keep things predictable, in case it matters, let's be in the minibuffer
439 when we reset the relevant variables. */
440 Fset_buffer (XWINDOW (window)->buffer);
442 /* Restore prompt, etc, from outer minibuffer level. */
443 minibuf_prompt = Fcar (minibuf_save_list);
444 minibuf_save_list = Fcdr (minibuf_save_list);
445 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
446 minibuf_save_list = Fcdr (minibuf_save_list);
447 Vhelp_form = Fcar (minibuf_save_list);
448 minibuf_save_list = Fcdr (minibuf_save_list);
449 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
450 minibuf_save_list = Fcdr (minibuf_save_list);
451 Vminibuffer_history_position = Fcar (minibuf_save_list);
452 minibuf_save_list = Fcdr (minibuf_save_list);
453 Vminibuffer_history_variable = Fcar (minibuf_save_list);
454 minibuf_save_list = Fcdr (minibuf_save_list);
455 Voverriding_local_map = Fcar (minibuf_save_list);
456 minibuf_save_list = Fcdr (minibuf_save_list);
457 minibuf_window = Fcar (minibuf_save_list);
458 minibuf_save_list = Fcdr (minibuf_save_list);
460 /* Erase the minibuffer we were using at this level. */
462 int count = specpdl_ptr - specpdl;
463 /* Prevent error in erase-buffer. */
464 specbind (Qinhibit_read_only, Qt);
465 old_deactivate_mark = Vdeactivate_mark;
466 Ferase_buffer ();
467 Vdeactivate_mark = old_deactivate_mark;
468 unbind_to (count, Qnil);
471 /* Make sure minibuffer window is erased, not ignored. */
472 windows_or_buffers_changed++;
473 XSETFASTINT (XWINDOW (window)->last_modified, 0);
477 /* This comment supplies the doc string for read-from-minibuffer,
478 for make-docfile to see. We cannot put this in the real DEFUN
479 due to limits in the Unix cpp.
481 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0,
482 "Read a string from the minibuffer, prompting with string PROMPT.\n\
483 If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
484 to be inserted into the minibuffer before reading input.\n\
485 If INITIAL-CONTENTS is (STRING . POSITION), the initial input\n\
486 is STRING, but point is placed POSITION characters into the string.\n\
487 Third arg KEYMAP is a keymap to use whilst reading;\n\
488 if omitted or nil, the default is `minibuffer-local-map'.\n\
489 If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
490 and return that object:\n\
491 in other words, do `(car (read-from-string INPUT-STRING))'\n\
492 Fifth arg HIST, if non-nil, specifies a history list\n\
493 and optionally the initial position in the list.\n\
494 It can be a symbol, which is the history list variable to use,\n\
495 or it can be a cons cell (HISTVAR . HISTPOS).\n\
496 In that case, HISTVAR is the history list variable to use,\n\
497 and HISTPOS is the initial position (the position in the list\n\
498 which INITIAL-CONTENTS corresponds to).\n\
499 Positions are counted starting from 1 at the beginning of the list."
502 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0,
503 0 /* See immediately above */)
504 (prompt, initial_contents, keymap, read, hist)
505 Lisp_Object prompt, initial_contents, keymap, read, hist;
507 int pos = 0;
508 Lisp_Object histvar, histpos, position;
509 position = Qnil;
511 CHECK_STRING (prompt, 0);
512 if (!NILP (initial_contents))
514 if (CONSP (initial_contents))
516 position = Fcdr (initial_contents);
517 initial_contents = Fcar (initial_contents);
519 CHECK_STRING (initial_contents, 1);
520 if (!NILP (position))
522 CHECK_NUMBER (position, 0);
523 /* Convert to distance from end of input. */
524 pos = XINT (position) - 1 - XSTRING (initial_contents)->size;
528 if (NILP (keymap))
529 keymap = Vminibuffer_local_map;
530 else
531 keymap = get_keymap (keymap,2);
533 if (SYMBOLP (hist))
535 histvar = hist;
536 histpos = Qnil;
538 else
540 histvar = Fcar_safe (hist);
541 histpos = Fcdr_safe (hist);
543 if (NILP (histvar))
544 histvar = Qminibuffer_history;
545 if (NILP (histpos))
546 XSETFASTINT (histpos, 0);
548 return read_minibuf (keymap, initial_contents, prompt,
549 make_number (pos), !NILP (read), histvar, histpos);
552 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
553 "Return a Lisp object read using the minibuffer.\n\
554 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
555 is a string to insert in the minibuffer before reading.")
556 (prompt, initial_contents)
557 Lisp_Object prompt, initial_contents;
559 CHECK_STRING (prompt, 0);
560 if (!NILP (initial_contents))
561 CHECK_STRING (initial_contents, 1);
562 return read_minibuf (Vminibuffer_local_map, initial_contents,
563 prompt, Qnil, 1, Qminibuffer_history, make_number (0));
566 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
567 "Return value of Lisp expression read using the minibuffer.\n\
568 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
569 is a string to insert in the minibuffer before reading.")
570 (prompt, initial_contents)
571 Lisp_Object prompt, initial_contents;
573 return Feval (Fread_minibuffer (prompt, initial_contents));
576 /* Functions that use the minibuffer to read various things. */
578 DEFUN ("read-string", Fread_string, Sread_string, 1, 3, 0,
579 "Read a string from the minibuffer, prompting with string PROMPT.\n\
580 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.\n\
581 The third arg HISTORY, if non-nil, specifies a history list\n\
582 and optionally the initial position in the list.\n\
583 See `read-from-minibuffer' for details of HISTORY argument.")
584 (prompt, initial_input, history)
585 Lisp_Object prompt, initial_input, history;
587 return Fread_from_minibuffer (prompt, initial_input, Qnil, Qnil, history);
590 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 2, 0,
591 "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\
592 Prompt with PROMPT, and provide INIT as an initial value of the input string.")
593 (prompt, init)
594 Lisp_Object prompt, init;
596 CHECK_STRING (prompt, 0);
597 if (! NILP (init))
598 CHECK_STRING (init, 1);
600 return read_minibuf (Vminibuffer_local_ns_map, init, prompt, Qnil, 0,
601 Qminibuffer_history, make_number (0));
604 DEFUN ("read-command", Fread_command, Sread_command, 1, 1, 0,
605 "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\
606 Prompts with PROMPT.")
607 (prompt)
608 Lisp_Object prompt;
610 return Fintern (Fcompleting_read (prompt, Vobarray, Qcommandp, Qt, Qnil, Qnil),
611 Qnil);
614 #ifdef NOTDEF
615 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
616 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
617 Prompts with PROMPT.")
618 (prompt)
619 Lisp_Object prompt;
621 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil),
622 Qnil);
624 #endif /* NOTDEF */
626 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 1, 0,
627 "One arg PROMPT, a string. Read the name of a user variable and return\n\
628 it as a symbol. Prompts with PROMPT.\n\
629 A user variable is one whose documentation starts with a `*' character.")
630 (prompt)
631 Lisp_Object prompt;
633 return Fintern (Fcompleting_read (prompt, Vobarray,
634 Quser_variable_p, Qt, Qnil, Qnil),
635 Qnil);
638 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
639 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
640 Prompts with PROMPT.\n\
641 Optional second arg is value to return if user enters an empty line.\n\
642 If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
643 (prompt, def, require_match)
644 Lisp_Object prompt, def, require_match;
646 Lisp_Object tem;
647 Lisp_Object args[3];
648 struct gcpro gcpro1;
650 if (BUFFERP (def))
651 def = XBUFFER (def)->name;
652 if (!NILP (def))
654 args[0] = build_string ("%s(default %s) ");
655 args[1] = prompt;
656 args[2] = def;
657 prompt = Fformat (3, args);
659 GCPRO1 (def);
660 tem = Fcompleting_read (prompt, Vbuffer_alist, Qnil, require_match, Qnil, Qnil);
661 UNGCPRO;
662 if (XSTRING (tem)->size)
663 return tem;
664 return def;
667 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
668 "Return common substring of all completions of STRING in ALIST.\n\
669 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
670 All that match are compared together; the longest initial sequence\n\
671 common to all matches is returned as a string.\n\
672 If there is no match at all, nil is returned.\n\
673 For an exact match, t is returned.\n\
675 ALIST can be an obarray instead of an alist.\n\
676 Then the print names of all symbols in the obarray are the possible matches.\n\
678 ALIST can also be a function to do the completion itself.\n\
679 It receives three arguments: the values STRING, PREDICATE and nil.\n\
680 Whatever it returns becomes the value of `try-completion'.\n\
682 If optional third argument PREDICATE is non-nil,\n\
683 it is used to test each possible match.\n\
684 The match is a candidate only if PREDICATE returns non-nil.\n\
685 The argument given to PREDICATE is the alist element\n\
686 or the symbol from the obarray.")
687 (string, alist, predicate)
688 Lisp_Object string, alist, predicate;
690 Lisp_Object bestmatch, tail, elt, eltstring;
691 int bestmatchsize;
692 int compare, matchsize;
693 int list = CONSP (alist) || NILP (alist);
694 int index, obsize;
695 int matchcount = 0;
696 Lisp_Object bucket, zero, end, tem;
697 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
699 CHECK_STRING (string, 0);
700 if (!list && !VECTORP (alist))
701 return call3 (alist, string, predicate, Qnil);
703 bestmatch = Qnil;
705 /* If ALIST is not a list, set TAIL just for gc pro. */
706 tail = alist;
707 if (! list)
709 index = 0;
710 obsize = XVECTOR (alist)->size;
711 bucket = XVECTOR (alist)->contents[index];
714 while (1)
716 /* Get the next element of the alist or obarray. */
717 /* Exit the loop if the elements are all used up. */
718 /* elt gets the alist element or symbol.
719 eltstring gets the name to check as a completion. */
721 if (list)
723 if (NILP (tail))
724 break;
725 elt = Fcar (tail);
726 eltstring = Fcar (elt);
727 tail = Fcdr (tail);
729 else
731 if (XFASTINT (bucket) != 0)
733 elt = bucket;
734 eltstring = Fsymbol_name (elt);
735 if (XSYMBOL (bucket)->next)
736 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
737 else
738 XSETFASTINT (bucket, 0);
740 else if (++index >= obsize)
741 break;
742 else
744 bucket = XVECTOR (alist)->contents[index];
745 continue;
749 /* Is this element a possible completion? */
751 if (STRINGP (eltstring)
752 && XSTRING (string)->size <= XSTRING (eltstring)->size
753 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
754 XSTRING (string)->size))
756 /* Yes. */
757 Lisp_Object regexps;
758 Lisp_Object zero;
759 XSETFASTINT (zero, 0);
761 /* Ignore this element if it fails to match all the regexps. */
762 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
763 regexps = XCONS (regexps)->cdr)
765 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
766 if (NILP (tem))
767 break;
769 if (CONSP (regexps))
770 continue;
772 /* Ignore this element if there is a predicate
773 and the predicate doesn't like it. */
775 if (!NILP (predicate))
777 if (EQ (predicate, Qcommandp))
778 tem = Fcommandp (elt);
779 else
781 GCPRO4 (tail, string, eltstring, bestmatch);
782 tem = call1 (predicate, elt);
783 UNGCPRO;
785 if (NILP (tem)) continue;
788 /* Update computation of how much all possible completions match */
790 matchcount++;
791 if (NILP (bestmatch))
792 bestmatch = eltstring, bestmatchsize = XSTRING (eltstring)->size;
793 else
795 compare = min (bestmatchsize, XSTRING (eltstring)->size);
796 matchsize = scmp (XSTRING (bestmatch)->data,
797 XSTRING (eltstring)->data,
798 compare);
799 if (matchsize < 0)
800 matchsize = compare;
801 if (completion_ignore_case)
803 /* If this is an exact match except for case,
804 use it as the best match rather than one that is not an
805 exact match. This way, we get the case pattern
806 of the actual match. */
807 if ((matchsize == XSTRING (eltstring)->size
808 && matchsize < XSTRING (bestmatch)->size)
810 /* If there is more than one exact match ignoring case,
811 and one of them is exact including case,
812 prefer that one. */
813 /* If there is no exact match ignoring case,
814 prefer a match that does not change the case
815 of the input. */
816 ((matchsize == XSTRING (eltstring)->size)
818 (matchsize == XSTRING (bestmatch)->size)
819 && !bcmp (XSTRING (eltstring)->data,
820 XSTRING (string)->data, XSTRING (string)->size)
821 && bcmp (XSTRING (bestmatch)->data,
822 XSTRING (string)->data, XSTRING (string)->size)))
823 bestmatch = eltstring;
825 bestmatchsize = matchsize;
830 if (NILP (bestmatch))
831 return Qnil; /* No completions found */
832 /* If we are ignoring case, and there is no exact match,
833 and no additional text was supplied,
834 don't change the case of what the user typed. */
835 if (completion_ignore_case && bestmatchsize == XSTRING (string)->size
836 && XSTRING (bestmatch)->size > bestmatchsize)
837 return string;
839 /* Return t if the supplied string is an exact match (counting case);
840 it does not require any change to be made. */
841 if (matchcount == 1 && bestmatchsize == XSTRING (string)->size
842 && !bcmp (XSTRING (bestmatch)->data, XSTRING (string)->data,
843 bestmatchsize))
844 return Qt;
846 XSETFASTINT (zero, 0); /* Else extract the part in which */
847 XSETFASTINT (end, bestmatchsize); /* all completions agree */
848 return Fsubstring (bestmatch, zero, end);
851 /* Compare exactly LEN chars of strings at S1 and S2,
852 ignoring case if appropriate.
853 Return -1 if strings match,
854 else number of chars that match at the beginning. */
857 scmp (s1, s2, len)
858 register unsigned char *s1, *s2;
859 int len;
861 register int l = len;
863 if (completion_ignore_case)
865 while (l && DOWNCASE (*s1++) == DOWNCASE (*s2++))
866 l--;
868 else
870 while (l && *s1++ == *s2++)
871 l--;
873 if (l == 0)
874 return -1;
875 else
876 return len - l;
879 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
880 "Search for partial matches to STRING in ALIST.\n\
881 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
882 The value is a list of all the strings from ALIST that match.\n\
884 ALIST can be an obarray instead of an alist.\n\
885 Then the print names of all symbols in the obarray are the possible matches.\n\
887 ALIST can also be a function to do the completion itself.\n\
888 It receives three arguments: the values STRING, PREDICATE and t.\n\
889 Whatever it returns becomes the value of `all-completion'.\n\
891 If optional third argument PREDICATE is non-nil,\n\
892 it is used to test each possible match.\n\
893 The match is a candidate only if PREDICATE returns non-nil.\n\
894 The argument given to PREDICATE is the alist element\n\
895 or the symbol from the obarray.\n\
897 If the optional fourth argument HIDE-SPACES is non-nil,\n\
898 strings in ALIST that start with a space\n\
899 are ignored unless STRING itself starts with a space.")
900 (string, alist, predicate, hide_spaces)
901 Lisp_Object string, alist, predicate, hide_spaces;
903 Lisp_Object tail, elt, eltstring;
904 Lisp_Object allmatches;
905 int list = CONSP (alist) || NILP (alist);
906 int index, obsize;
907 Lisp_Object bucket, tem;
908 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
910 CHECK_STRING (string, 0);
911 if (!list && !VECTORP (alist))
913 return call3 (alist, string, predicate, Qt);
915 allmatches = Qnil;
917 /* If ALIST is not a list, set TAIL just for gc pro. */
918 tail = alist;
919 if (! list)
921 index = 0;
922 obsize = XVECTOR (alist)->size;
923 bucket = XVECTOR (alist)->contents[index];
926 while (1)
928 /* Get the next element of the alist or obarray. */
929 /* Exit the loop if the elements are all used up. */
930 /* elt gets the alist element or symbol.
931 eltstring gets the name to check as a completion. */
933 if (list)
935 if (NILP (tail))
936 break;
937 elt = Fcar (tail);
938 eltstring = Fcar (elt);
939 tail = Fcdr (tail);
941 else
943 if (XFASTINT (bucket) != 0)
945 elt = bucket;
946 eltstring = Fsymbol_name (elt);
947 if (XSYMBOL (bucket)->next)
948 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
949 else
950 XSETFASTINT (bucket, 0);
952 else if (++index >= obsize)
953 break;
954 else
956 bucket = XVECTOR (alist)->contents[index];
957 continue;
961 /* Is this element a possible completion? */
963 if (STRINGP (eltstring)
964 && XSTRING (string)->size <= XSTRING (eltstring)->size
965 /* If HIDE_SPACES, reject alternatives that start with space
966 unless the input starts with space. */
967 && ((XSTRING (string)->size > 0 && XSTRING (string)->data[0] == ' ')
968 || XSTRING (eltstring)->data[0] != ' '
969 || NILP (hide_spaces))
970 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
971 XSTRING (string)->size))
973 /* Yes. */
974 Lisp_Object regexps;
975 Lisp_Object zero;
976 XSETFASTINT (zero, 0);
978 /* Ignore this element if it fails to match all the regexps. */
979 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
980 regexps = XCONS (regexps)->cdr)
982 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
983 if (NILP (tem))
984 break;
986 if (CONSP (regexps))
987 continue;
989 /* Ignore this element if there is a predicate
990 and the predicate doesn't like it. */
992 if (!NILP (predicate))
994 if (EQ (predicate, Qcommandp))
995 tem = Fcommandp (elt);
996 else
998 GCPRO4 (tail, eltstring, allmatches, string);
999 tem = call1 (predicate, elt);
1000 UNGCPRO;
1002 if (NILP (tem)) continue;
1004 /* Ok => put it on the list. */
1005 allmatches = Fcons (eltstring, allmatches);
1009 return Fnreverse (allmatches);
1012 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1013 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1014 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
1016 /* This comment supplies the doc string for completing-read,
1017 for make-docfile to see. We cannot put this in the real DEFUN
1018 due to limits in the Unix cpp.
1020 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0,
1021 "Read a string in the minibuffer, with completion.\n\
1022 PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
1023 TABLE is an alist whose elements' cars are strings, or an obarray.\n\
1024 PREDICATE limits completion to a subset of TABLE.\n\
1025 See `try-completion' and `all-completions' for more details
1026 on completion, TABLE, and PREDICATE.\n\
1028 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
1029 the input is (or completes to) an element of TABLE or is null.\n\
1030 If it is also not t, Return does not exit if it does non-null completion.\n\
1031 If the input is null, `completing-read' returns nil,\n\
1032 regardless of the value of REQUIRE-MATCH.\n\
1034 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
1035 If it is (STRING . POSITION), the initial input\n\
1036 is STRING, but point is placed POSITION characters into the string.\n\
1037 HIST, if non-nil, specifies a history list\n\
1038 and optionally the initial position in the list.\n\
1039 It can be a symbol, which is the history list variable to use,\n\
1040 or it can be a cons cell (HISTVAR . HISTPOS).\n\
1041 In that case, HISTVAR is the history list variable to use,\n\
1042 and HISTPOS is the initial position (the position in the list\n\
1043 which INITIAL-CONTENTS corresponds to).\n\
1044 Positions are counted starting from 1 at the beginning of the list.\n\
1045 Completion ignores case if the ambient value of\n\
1046 `completion-ignore-case' is non-nil."
1048 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0,
1049 0 /* See immediately above */)
1050 (prompt, table, predicate, require_match, init, hist)
1051 Lisp_Object prompt, table, predicate, require_match, init, hist;
1053 Lisp_Object val, histvar, histpos, position;
1054 int pos = 0;
1055 int count = specpdl_ptr - specpdl;
1056 specbind (Qminibuffer_completion_table, table);
1057 specbind (Qminibuffer_completion_predicate, predicate);
1058 specbind (Qminibuffer_completion_confirm,
1059 EQ (require_match, Qt) ? Qnil : Qt);
1060 last_exact_completion = Qnil;
1062 position = Qnil;
1063 if (!NILP (init))
1065 if (CONSP (init))
1067 position = Fcdr (init);
1068 init = Fcar (init);
1070 CHECK_STRING (init, 0);
1071 if (!NILP (position))
1073 CHECK_NUMBER (position, 0);
1074 /* Convert to distance from end of input. */
1075 pos = XINT (position) - XSTRING (init)->size;
1079 if (SYMBOLP (hist))
1081 histvar = hist;
1082 histpos = Qnil;
1084 else
1086 histvar = Fcar_safe (hist);
1087 histpos = Fcdr_safe (hist);
1089 if (NILP (histvar))
1090 histvar = Qminibuffer_history;
1091 if (NILP (histpos))
1092 XSETFASTINT (histpos, 0);
1094 val = read_minibuf (NILP (require_match)
1095 ? Vminibuffer_local_completion_map
1096 : Vminibuffer_local_must_match_map,
1097 init, prompt, make_number (pos), 0,
1098 histvar, histpos);
1099 return unbind_to (count, val);
1102 /* Temporarily display the string M at the end of the current
1103 minibuffer contents. This is used to display things like
1104 "[No Match]" when the user requests a completion for a prefix
1105 that has no possible completions, and other quick, unobtrusive
1106 messages. */
1108 temp_echo_area_glyphs (m)
1109 char *m;
1111 int osize = ZV;
1112 int opoint = PT;
1113 Lisp_Object oinhibit;
1114 oinhibit = Vinhibit_quit;
1116 /* Clear out any old echo-area message to make way for our new thing. */
1117 message (0);
1119 SET_PT (osize);
1120 insert_string (m);
1121 SET_PT (opoint);
1122 Vinhibit_quit = Qt;
1123 Fsit_for (make_number (2), Qnil, Qnil);
1124 del_range (osize, ZV);
1125 SET_PT (opoint);
1126 if (!NILP (Vquit_flag))
1128 Vquit_flag = Qnil;
1129 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1131 Vinhibit_quit = oinhibit;
1134 Lisp_Object Fminibuffer_completion_help ();
1135 Lisp_Object assoc_for_completion ();
1136 /* A subroutine of Fintern_soft. */
1137 extern Lisp_Object oblookup ();
1140 /* Test whether TXT is an exact completion. */
1141 Lisp_Object
1142 test_completion (txt)
1143 Lisp_Object txt;
1145 Lisp_Object tem;
1147 if (CONSP (Vminibuffer_completion_table)
1148 || NILP (Vminibuffer_completion_table))
1149 return assoc_for_completion (txt, Vminibuffer_completion_table);
1150 else if (VECTORP (Vminibuffer_completion_table))
1152 /* Bypass intern-soft as that loses for nil */
1153 tem = oblookup (Vminibuffer_completion_table,
1154 XSTRING (txt)->data, XSTRING (txt)->size);
1155 if (!SYMBOLP (tem))
1156 return Qnil;
1157 else if (!NILP (Vminibuffer_completion_predicate))
1158 return call1 (Vminibuffer_completion_predicate, tem);
1159 else
1160 return Qt;
1162 else
1163 return call3 (Vminibuffer_completion_table, txt,
1164 Vminibuffer_completion_predicate, Qlambda);
1167 /* returns:
1168 * 0 no possible completion
1169 * 1 was already an exact and unique completion
1170 * 3 was already an exact completion
1171 * 4 completed to an exact completion
1172 * 5 some completion happened
1173 * 6 no completion happened
1176 do_completion ()
1178 Lisp_Object completion, tem;
1179 int completedp;
1180 Lisp_Object last;
1181 struct gcpro gcpro1, gcpro2;
1183 completion = Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table,
1184 Vminibuffer_completion_predicate);
1185 last = last_exact_completion;
1186 last_exact_completion = Qnil;
1188 GCPRO2 (completion, last);
1190 if (NILP (completion))
1192 bitch_at_user ();
1193 temp_echo_area_glyphs (" [No match]");
1194 UNGCPRO;
1195 return 0;
1198 if (EQ (completion, Qt)) /* exact and unique match */
1200 UNGCPRO;
1201 return 1;
1204 /* compiler bug */
1205 tem = Fstring_equal (completion, Fbuffer_string());
1206 if (completedp = NILP (tem))
1208 Ferase_buffer (); /* Some completion happened */
1209 Finsert (1, &completion);
1212 /* It did find a match. Do we match some possibility exactly now? */
1213 tem = test_completion (Fbuffer_string ());
1214 if (NILP (tem))
1216 /* not an exact match */
1217 UNGCPRO;
1218 if (completedp)
1219 return 5;
1220 else if (auto_help)
1221 Fminibuffer_completion_help ();
1222 else
1223 temp_echo_area_glyphs (" [Next char not unique]");
1224 return 6;
1226 else if (completedp)
1228 UNGCPRO;
1229 return 4;
1231 /* If the last exact completion and this one were the same,
1232 it means we've already given a "Complete but not unique"
1233 message and the user's hit TAB again, so now we give him help. */
1234 last_exact_completion = completion;
1235 if (!NILP (last))
1237 tem = Fbuffer_string ();
1238 if (!NILP (Fequal (tem, last)))
1239 Fminibuffer_completion_help ();
1241 UNGCPRO;
1242 return 3;
1245 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1247 Lisp_Object
1248 assoc_for_completion (key, list)
1249 register Lisp_Object key;
1250 Lisp_Object list;
1252 register Lisp_Object tail;
1254 if (completion_ignore_case)
1255 key = Fupcase (key);
1257 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1259 register Lisp_Object elt, tem, thiscar;
1260 elt = Fcar (tail);
1261 if (!CONSP (elt)) continue;
1262 thiscar = Fcar (elt);
1263 if (!STRINGP (thiscar))
1264 continue;
1265 if (completion_ignore_case)
1266 thiscar = Fupcase (thiscar);
1267 tem = Fequal (thiscar, key);
1268 if (!NILP (tem)) return elt;
1269 QUIT;
1271 return Qnil;
1274 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1275 "Complete the minibuffer contents as far as possible.\n\
1276 Return nil if there is no valid completion, else t.\n\
1277 If no characters can be completed, display a list of possible completions.\n\
1278 If you repeat this command after it displayed such a list,\n\
1279 scroll the window of possible completions.")
1282 register int i;
1283 Lisp_Object window, tem;
1285 /* If the previous command was not this, then mark the completion
1286 buffer obsolete. */
1287 if (! EQ (current_kboard->Vlast_command, this_command))
1288 Vminibuf_scroll_window = Qnil;
1290 window = Vminibuf_scroll_window;
1291 /* If there's a fresh completion window with a live buffer,
1292 and this command is repeated, scroll that window. */
1293 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1294 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1296 struct buffer *obuf = current_buffer;
1298 Fset_buffer (XWINDOW (window)->buffer);
1299 tem = Fpos_visible_in_window_p (make_number (ZV), window);
1300 if (! NILP (tem))
1301 /* If end is in view, scroll up to the beginning. */
1302 Fset_window_start (window, BEGV, Qnil);
1303 else
1304 /* Else scroll down one screen. */
1305 Fscroll_other_window (Qnil);
1307 set_buffer_internal (obuf);
1308 return Qnil;
1311 i = do_completion ();
1312 switch (i)
1314 case 0:
1315 return Qnil;
1317 case 1:
1318 temp_echo_area_glyphs (" [Sole completion]");
1319 break;
1321 case 3:
1322 temp_echo_area_glyphs (" [Complete, but not unique]");
1323 break;
1326 return Qt;
1329 /* Subroutines of Fminibuffer_complete_and_exit. */
1331 /* This one is called by internal_condition_case to do the real work. */
1333 Lisp_Object
1334 complete_and_exit_1 ()
1336 return make_number (do_completion ());
1339 /* This one is called by internal_condition_case if an error happens.
1340 Pretend the current value is an exact match. */
1342 Lisp_Object
1343 complete_and_exit_2 (ignore)
1344 Lisp_Object ignore;
1346 return make_number (1);
1349 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1350 Sminibuffer_complete_and_exit, 0, 0, "",
1351 "If the minibuffer contents is a valid completion then exit.\n\
1352 Otherwise try to complete it. If completion leads to a valid completion,\n\
1353 a repetition of this command will exit.")
1356 register int i;
1357 Lisp_Object val;
1359 /* Allow user to specify null string */
1360 if (BEGV == ZV)
1361 goto exit;
1363 if (!NILP (test_completion (Fbuffer_string ())))
1364 goto exit;
1366 /* Call do_completion, but ignore errors. */
1367 val = internal_condition_case (complete_and_exit_1, Qerror,
1368 complete_and_exit_2);
1370 i = XFASTINT (val);
1371 switch (i)
1373 case 1:
1374 case 3:
1375 goto exit;
1377 case 4:
1378 if (!NILP (Vminibuffer_completion_confirm))
1380 temp_echo_area_glyphs (" [Confirm]");
1381 return Qnil;
1383 else
1384 goto exit;
1386 default:
1387 return Qnil;
1389 exit:
1390 Fthrow (Qexit, Qnil);
1391 /* NOTREACHED */
1394 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1395 0, 0, "",
1396 "Complete the minibuffer contents at most a single word.\n\
1397 After one word is completed as much as possible, a space or hyphen\n\
1398 is added, provided that matches some possible completion.\n\
1399 Return nil if there is no valid completion, else t.")
1402 Lisp_Object completion, tem;
1403 register int i;
1404 register unsigned char *completion_string;
1405 struct gcpro gcpro1, gcpro2;
1407 /* We keep calling Fbuffer_string rather than arrange for GC to
1408 hold onto a pointer to one of the strings thus made. */
1410 completion = Ftry_completion (Fbuffer_string (),
1411 Vminibuffer_completion_table,
1412 Vminibuffer_completion_predicate);
1413 if (NILP (completion))
1415 bitch_at_user ();
1416 temp_echo_area_glyphs (" [No match]");
1417 return Qnil;
1419 if (EQ (completion, Qt))
1420 return Qnil;
1422 #if 0 /* How the below code used to look, for reference. */
1423 tem = Fbuffer_string ();
1424 b = XSTRING (tem)->data;
1425 i = ZV - 1 - XSTRING (completion)->size;
1426 p = XSTRING (completion)->data;
1427 if (i > 0 ||
1428 0 <= scmp (b, p, ZV - 1))
1430 i = 1;
1431 /* Set buffer to longest match of buffer tail and completion head. */
1432 while (0 <= scmp (b + i, p, ZV - 1 - i))
1433 i++;
1434 del_range (1, i + 1);
1435 SET_PT (ZV);
1437 #else /* Rewritten code */
1439 register unsigned char *buffer_string;
1440 int buffer_length, completion_length;
1442 tem = Fbuffer_string ();
1443 GCPRO2 (completion, tem);
1444 /* If reading a file name,
1445 expand any $ENVVAR refs in the buffer and in TEM. */
1446 if (EQ (Vminibuffer_completion_table, Qread_file_name_internal))
1448 Lisp_Object substituted;
1449 substituted = Fsubstitute_in_file_name (tem);
1450 if (! EQ (substituted, tem))
1452 tem = substituted;
1453 Ferase_buffer ();
1454 insert_from_string (tem, 0, XSTRING (tem)->size, 0);
1457 buffer_string = XSTRING (tem)->data;
1458 completion_string = XSTRING (completion)->data;
1459 buffer_length = XSTRING (tem)->size; /* ie ZV - BEGV */
1460 completion_length = XSTRING (completion)->size;
1461 i = buffer_length - completion_length;
1462 /* Mly: I don't understand what this is supposed to do AT ALL */
1463 if (i > 0 ||
1464 0 <= scmp (buffer_string, completion_string, buffer_length))
1466 /* Set buffer to longest match of buffer tail and completion head. */
1467 if (i <= 0) i = 1;
1468 buffer_string += i;
1469 buffer_length -= i;
1470 while (0 <= scmp (buffer_string++, completion_string, buffer_length--))
1471 i++;
1472 del_range (1, i + 1);
1473 SET_PT (ZV);
1475 UNGCPRO;
1477 #endif /* Rewritten code */
1478 i = ZV - BEGV;
1480 /* If completion finds next char not unique,
1481 consider adding a space or a hyphen. */
1482 if (i == XSTRING (completion)->size)
1484 GCPRO1 (completion);
1485 tem = Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")),
1486 Vminibuffer_completion_table,
1487 Vminibuffer_completion_predicate);
1488 UNGCPRO;
1490 if (STRINGP (tem))
1491 completion = tem;
1492 else
1494 GCPRO1 (completion);
1495 tem =
1496 Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")),
1497 Vminibuffer_completion_table,
1498 Vminibuffer_completion_predicate);
1499 UNGCPRO;
1501 if (STRINGP (tem))
1502 completion = tem;
1506 /* Now find first word-break in the stuff found by completion.
1507 i gets index in string of where to stop completing. */
1509 completion_string = XSTRING (completion)->data;
1511 for (; i < XSTRING (completion)->size; i++)
1512 if (SYNTAX (completion_string[i]) != Sword) break;
1513 if (i < XSTRING (completion)->size)
1514 i = i + 1;
1516 /* If got no characters, print help for user. */
1518 if (i == ZV - BEGV)
1520 if (auto_help)
1521 Fminibuffer_completion_help ();
1522 return Qnil;
1525 /* Otherwise insert in minibuffer the chars we got */
1527 Ferase_buffer ();
1528 insert_from_string (completion, 0, i, 1);
1529 return Qt;
1532 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
1533 1, 1, 0,
1534 "Display the list of completions, COMPLETIONS, using `standard-output'.\n\
1535 Each element may be just a symbol or string\n\
1536 or may be a list of two strings to be printed as if concatenated.\n\
1537 `standard-output' must be a buffer.\n\
1538 At the end, run the normal hook `completion-setup-hook'.\n\
1539 It can find the completion buffer in `standard-output'.")
1540 (completions)
1541 Lisp_Object completions;
1543 Lisp_Object tail, elt;
1544 register int i;
1545 int column = 0;
1546 struct gcpro gcpro1, gcpro2;
1547 struct buffer *old = current_buffer;
1548 int first = 1;
1550 /* Note that (when it matters) every variable
1551 points to a non-string that is pointed to by COMPLETIONS,
1552 except for ELT. ELT can be pointing to a string
1553 when terpri or Findent_to calls a change hook. */
1554 elt = Qnil;
1555 GCPRO2 (completions, elt);
1557 if (BUFFERP (Vstandard_output))
1558 set_buffer_internal (XBUFFER (Vstandard_output));
1560 if (NILP (completions))
1561 write_string ("There are no possible completions of what you have typed.",
1562 -1);
1563 else
1565 write_string ("Possible completions are:", -1);
1566 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
1568 Lisp_Object tem;
1569 int length;
1570 Lisp_Object startpos, endpos;
1572 elt = Fcar (tail);
1573 /* Compute the length of this element. */
1574 if (CONSP (elt))
1576 tem = Fcar (elt);
1577 CHECK_STRING (tem, 0);
1578 length = XINT (XSTRING (tem)->size);
1580 tem = Fcar (Fcdr (elt));
1581 CHECK_STRING (tem, 0);
1582 length += XINT (XSTRING (tem)->size);
1584 else
1586 CHECK_STRING (elt, 0);
1587 length = XINT (XSTRING (elt)->size);
1590 /* This does a bad job for narrower than usual windows.
1591 Sadly, the window it will appear in is not known
1592 until after the text has been made. */
1594 if (BUFFERP (Vstandard_output))
1595 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
1597 /* If the previous completion was very wide,
1598 or we have two on this line already,
1599 don't put another on the same line. */
1600 if (column > 33 || first
1601 /* If this is really wide, don't put it second on a line. */
1602 || column > 0 && length > 45)
1604 Fterpri (Qnil);
1605 column = 0;
1607 /* Otherwise advance to column 35. */
1608 else
1610 if (BUFFERP (Vstandard_output))
1612 tem = Findent_to (make_number (35), make_number (2));
1614 column = XINT (tem);
1616 else
1620 write_string (" ", -1);
1621 column++;
1623 while (column < 35);
1627 if (BUFFERP (Vstandard_output))
1629 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
1630 Fset_text_properties (startpos, endpos,
1631 Qnil, Vstandard_output);
1634 /* Output this element and update COLUMN. */
1635 if (CONSP (elt))
1637 Fprinc (Fcar (elt), Qnil);
1638 Fprinc (Fcar (Fcdr (elt)), Qnil);
1640 else
1641 Fprinc (elt, Qnil);
1643 column += length;
1645 /* If output is to a buffer, recompute COLUMN in a way
1646 that takes account of character widths. */
1647 if (BUFFERP (Vstandard_output))
1649 tem = Fcurrent_column ();
1650 column = XINT (tem);
1653 first = 0;
1657 UNGCPRO;
1659 if (BUFFERP (Vstandard_output))
1660 set_buffer_internal (old);
1662 if (!NILP (Vrun_hooks))
1663 call1 (Vrun_hooks, intern ("completion-setup-hook"));
1665 return Qnil;
1668 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
1669 0, 0, "",
1670 "Display a list of possible completions of the current minibuffer contents.")
1673 Lisp_Object completions;
1675 message ("Making completion list...");
1676 completions = Fall_completions (Fbuffer_string (),
1677 Vminibuffer_completion_table,
1678 Vminibuffer_completion_predicate,
1679 Qt);
1680 echo_area_glyphs = 0;
1682 if (NILP (completions))
1684 bitch_at_user ();
1685 temp_echo_area_glyphs (" [No completions]");
1687 else
1688 internal_with_output_to_temp_buffer ("*Completions*",
1689 Fdisplay_completion_list,
1690 Fsort (completions, Qstring_lessp));
1691 return Qnil;
1694 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
1695 "Terminate minibuffer input.")
1698 if (INTEGERP (last_command_char))
1699 internal_self_insert (last_command_char, 0);
1700 else
1701 bitch_at_user ();
1703 Fthrow (Qexit, Qnil);
1706 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
1707 "Terminate this minibuffer argument.")
1710 Fthrow (Qexit, Qnil);
1713 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
1714 "Return current depth of activations of minibuffer, a nonnegative integer.")
1717 return make_number (minibuf_level);
1720 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
1721 "Return the prompt string of the currently-active minibuffer.\n\
1722 If no minibuffer is active, return nil.")
1725 return Fcopy_sequence (minibuf_prompt);
1728 DEFUN ("minibuffer-prompt-width", Fminibuffer_prompt_width,
1729 Sminibuffer_prompt_width, 0, 0, 0,
1730 "Return the display width of the minibuffer prompt.")
1733 Lisp_Object width;
1734 XSETFASTINT (width, minibuf_prompt_width);
1735 return width;
1738 init_minibuf_once ()
1740 Vminibuffer_list = Qnil;
1741 staticpro (&Vminibuffer_list);
1744 syms_of_minibuf ()
1746 minibuf_level = 0;
1747 minibuf_prompt = Qnil;
1748 staticpro (&minibuf_prompt);
1750 minibuf_save_list = Qnil;
1751 staticpro (&minibuf_save_list);
1753 Qread_file_name_internal = intern ("read-file-name-internal");
1754 staticpro (&Qread_file_name_internal);
1756 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
1757 staticpro (&Qminibuffer_completion_table);
1759 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
1760 staticpro (&Qminibuffer_completion_confirm);
1762 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
1763 staticpro (&Qminibuffer_completion_predicate);
1765 staticpro (&last_exact_completion);
1766 last_exact_completion = Qnil;
1768 staticpro (&last_minibuf_string);
1769 last_minibuf_string = Qnil;
1771 Quser_variable_p = intern ("user-variable-p");
1772 staticpro (&Quser_variable_p);
1774 Qminibuffer_history = intern ("minibuffer-history");
1775 staticpro (&Qminibuffer_history);
1777 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
1778 staticpro (&Qminibuffer_setup_hook);
1780 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
1781 staticpro (&Qminibuffer_exit_hook);
1783 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
1784 "Normal hook run just after entry to minibuffer.");
1785 Vminibuffer_setup_hook = Qnil;
1787 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
1788 "Normal hook run just after exit from minibuffer.");
1789 Vminibuffer_exit_hook = Qnil;
1791 DEFVAR_BOOL ("completion-auto-help", &auto_help,
1792 "*Non-nil means automatically provide help for invalid completion input.");
1793 auto_help = 1;
1795 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
1796 "Non-nil means don't consider case significant in completion.");
1797 completion_ignore_case = 0;
1799 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
1800 "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\
1801 More precisely, this variable makes a difference when the minibuffer window\n\
1802 is the selected window. If you are in some other window, minibuffer commands\n\
1803 are allowed even if a minibuffer is active.");
1804 enable_recursive_minibuffers = 0;
1806 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
1807 "Alist or obarray used for completion in the minibuffer.\n\
1808 This becomes the ALIST argument to `try-completion' and `all-completion'.\n\
1810 The value may alternatively be a function, which is given three arguments:\n\
1811 STRING, the current buffer contents;\n\
1812 PREDICATE, the predicate for filtering possible matches;\n\
1813 CODE, which says what kind of things to do.\n\
1814 CODE can be nil, t or `lambda'.\n\
1815 nil means to return the best completion of STRING, or nil if there is none.\n\
1816 t means to return a list of all possible completions of STRING.\n\
1817 `lambda' means to return t if STRING is a valid completion as it stands.");
1818 Vminibuffer_completion_table = Qnil;
1820 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
1821 "Within call to `completing-read', this holds the PREDICATE argument.");
1822 Vminibuffer_completion_predicate = Qnil;
1824 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
1825 "Non-nil => demand confirmation of completion before exiting minibuffer.");
1826 Vminibuffer_completion_confirm = Qnil;
1828 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
1829 "Value that `help-form' takes on inside the minibuffer.");
1830 Vminibuffer_help_form = Qnil;
1832 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
1833 "History list symbol to add minibuffer values to.\n\
1834 Each string of minibuffer input, as it appears on exit from the minibuffer,\n\
1835 is added with\n\
1836 (set minibuffer-history-variable\n\
1837 (cons STRING (symbol-value minibuffer-history-variable)))");
1838 XSETFASTINT (Vminibuffer_history_variable, 0);
1840 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
1841 "Current position of redoing in the history list.");
1842 Vminibuffer_history_position = Qnil;
1844 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
1845 "*Non-nil means entering the minibuffer raises the minibuffer's frame.");
1846 minibuffer_auto_raise = 0;
1848 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
1849 "List of regexps that should restrict possible completions.");
1850 Vcompletion_regexp_list = Qnil;
1852 defsubr (&Sset_minibuffer_window);
1853 defsubr (&Sread_from_minibuffer);
1854 defsubr (&Seval_minibuffer);
1855 defsubr (&Sread_minibuffer);
1856 defsubr (&Sread_string);
1857 defsubr (&Sread_command);
1858 defsubr (&Sread_variable);
1859 defsubr (&Sread_buffer);
1860 defsubr (&Sread_no_blanks_input);
1861 defsubr (&Sminibuffer_depth);
1862 defsubr (&Sminibuffer_prompt);
1863 defsubr (&Sminibuffer_prompt_width);
1865 defsubr (&Stry_completion);
1866 defsubr (&Sall_completions);
1867 defsubr (&Scompleting_read);
1868 defsubr (&Sminibuffer_complete);
1869 defsubr (&Sminibuffer_complete_word);
1870 defsubr (&Sminibuffer_complete_and_exit);
1871 defsubr (&Sdisplay_completion_list);
1872 defsubr (&Sminibuffer_completion_help);
1874 defsubr (&Sself_insert_and_exit);
1875 defsubr (&Sexit_minibuffer);
1879 keys_of_minibuf ()
1881 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
1882 "abort-recursive-edit");
1883 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
1884 "exit-minibuffer");
1885 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
1886 "exit-minibuffer");
1888 initial_define_key (Vminibuffer_local_ns_map, Ctl ('g'),
1889 "abort-recursive-edit");
1890 initial_define_key (Vminibuffer_local_ns_map, Ctl ('m'),
1891 "exit-minibuffer");
1892 initial_define_key (Vminibuffer_local_ns_map, Ctl ('j'),
1893 "exit-minibuffer");
1895 initial_define_key (Vminibuffer_local_ns_map, ' ',
1896 "exit-minibuffer");
1897 initial_define_key (Vminibuffer_local_ns_map, '\t',
1898 "exit-minibuffer");
1899 initial_define_key (Vminibuffer_local_ns_map, '?',
1900 "self-insert-and-exit");
1902 initial_define_key (Vminibuffer_local_completion_map, Ctl ('g'),
1903 "abort-recursive-edit");
1904 initial_define_key (Vminibuffer_local_completion_map, Ctl ('m'),
1905 "exit-minibuffer");
1906 initial_define_key (Vminibuffer_local_completion_map, Ctl ('j'),
1907 "exit-minibuffer");
1909 initial_define_key (Vminibuffer_local_completion_map, '\t',
1910 "minibuffer-complete");
1911 initial_define_key (Vminibuffer_local_completion_map, ' ',
1912 "minibuffer-complete-word");
1913 initial_define_key (Vminibuffer_local_completion_map, '?',
1914 "minibuffer-completion-help");
1916 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('g'),
1917 "abort-recursive-edit");
1918 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
1919 "minibuffer-complete-and-exit");
1920 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
1921 "minibuffer-complete-and-exit");
1922 initial_define_key (Vminibuffer_local_must_match_map, '\t',
1923 "minibuffer-complete");
1924 initial_define_key (Vminibuffer_local_must_match_map, ' ',
1925 "minibuffer-complete-word");
1926 initial_define_key (Vminibuffer_local_must_match_map, '?',
1927 "minibuffer-completion-help");