(print): Use current_perdisplay, not get_perdisplay.
[emacs.git] / src / minibuf.c
blob56b927b0caa638553b109afb622aa722c5debb36
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"
30 #define min(a, b) ((a) < (b) ? (a) : (b))
32 extern int quit_char;
34 /* List of buffers for use as minibuffers.
35 The first element of the list is used for the outermost minibuffer
36 invocation, the next element is used for a recursive minibuffer
37 invocation, etc. The list is extended at the end as deeper
38 minibuffer recursions are encountered. */
39 Lisp_Object Vminibuffer_list;
41 /* Data to remember during recursive minibuffer invocations */
42 Lisp_Object minibuf_save_list;
44 /* Depth in minibuffer invocations. */
45 int minibuf_level;
47 /* Nonzero means display completion help for invalid input */
48 int auto_help;
50 /* Fread_minibuffer leaves the input here as a string. */
51 Lisp_Object last_minibuf_string;
53 /* Nonzero means let functions called when within a minibuffer
54 invoke recursive minibuffers (to read arguments, or whatever) */
55 int enable_recursive_minibuffers;
57 /* help-form is bound to this while in the minibuffer. */
59 Lisp_Object Vminibuffer_help_form;
61 /* Variable which is the history list to add minibuffer values to. */
63 Lisp_Object Vminibuffer_history_variable;
65 /* Current position in the history list (adjusted by M-n and M-p). */
67 Lisp_Object Vminibuffer_history_position;
69 Lisp_Object Qminibuffer_history;
71 Lisp_Object Qread_file_name_internal;
73 /* Normal hooks for entry to and exit from minibuffer. */
75 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
76 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
78 /* Nonzero means completion ignores case. */
80 int completion_ignore_case;
82 /* List of regexps that should restrict possible completions. */
84 Lisp_Object Vcompletion_regexp_list;
86 /* Nonzero means raise the minibuffer frame when the minibuffer
87 is entered. */
89 int minibuffer_auto_raise;
91 /* If last completion attempt reported "Complete but not unique"
92 then this is the string completed then; otherwise this is nil. */
94 static Lisp_Object last_exact_completion;
96 Lisp_Object Quser_variable_p;
98 /* Non-nil means it is the window for C-M-v to scroll
99 when the minibuffer is selected. */
100 extern Lisp_Object Vminibuf_scroll_window;
102 /* Actual minibuffer invocation. */
104 void read_minibuf_unwind ();
105 Lisp_Object get_minibuffer ();
106 Lisp_Object read_minibuf ();
108 /* Read from the minibuffer using keymap MAP, initial contents INITIAL
109 (a string), putting point minus BACKUP_N chars from the end of INITIAL,
110 prompting with PROMPT (a string), using history list HISTVAR
111 with initial position HISTPOS. (BACKUP_N should be <= 0.)
113 Normally return the result as a string (the text that was read),
114 but if EXPFLAG is nonzero, read it and return the object read.
115 If HISTVAR is given, save the value read on that history only if it doesn't
116 match the front of that history list exactly. The value is pushed onto
117 the list as the string that was read. */
119 Lisp_Object
120 read_minibuf (map, initial, prompt, backup_n, expflag, histvar, histpos)
121 Lisp_Object map;
122 Lisp_Object initial;
123 Lisp_Object prompt;
124 Lisp_Object backup_n;
125 int expflag;
126 Lisp_Object histvar;
127 Lisp_Object histpos;
129 Lisp_Object val;
130 int count = specpdl_ptr - specpdl;
131 Lisp_Object mini_frame;
132 struct gcpro gcpro1, gcpro2, gcpro3;
134 val = Qnil;
135 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
136 store them away before we can GC. Don't need to protect
137 BACKUP_N because we use the value only if it is an integer. */
138 GCPRO3 (map, initial, val);
140 if (!STRINGP (prompt))
141 prompt = build_string ("");
143 if (!enable_recursive_minibuffers
144 && minibuf_level > 0
145 && (EQ (selected_window, minibuf_window)))
146 #if 0
147 || selected_frame != XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)))
148 #endif
149 error ("Command attempted to use minibuffer while in minibuffer");
151 /* Could we simply bind these variables instead? */
152 minibuf_save_list
153 = Fcons (minibuf_prompt,
154 Fcons (make_number (minibuf_prompt_width),
155 Fcons (Vhelp_form,
156 Fcons (current_perdisplay->Vcurrent_prefix_arg,
157 Fcons (Vminibuffer_history_position,
158 Fcons (Vminibuffer_history_variable,
159 minibuf_save_list))))));
160 minibuf_prompt_width = 0; /* xdisp.c puts in the right value. */
161 minibuf_prompt = Fcopy_sequence (prompt);
162 Vminibuffer_history_position = histpos;
163 Vminibuffer_history_variable = histvar;
165 record_unwind_protect (Fset_window_configuration,
166 Fcurrent_window_configuration (Qnil));
168 /* If the minibuffer window is on a different frame, save that
169 frame's configuration too. */
170 #ifdef MULTI_FRAME
171 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
172 if (XFRAME (mini_frame) != selected_frame)
173 record_unwind_protect (Fset_window_configuration,
174 Fcurrent_window_configuration (mini_frame));
176 /* If the minibuffer is on an iconified or invisible frame,
177 make it visible now. */
178 Fmake_frame_visible (mini_frame);
180 if (minibuffer_auto_raise)
181 Fraise_frame (mini_frame);
182 #endif
184 val = current_buffer->directory;
185 Fset_buffer (get_minibuffer (minibuf_level));
187 /* The current buffer's default directory is usually the right thing
188 for our minibuffer here. However, if you're typing a command at
189 a minibuffer-only frame when minibuf_level is zero, then buf IS
190 the current_buffer, so reset_buffer leaves buf's default
191 directory unchanged. This is a bummer when you've just started
192 up Emacs and buf's default directory is Qnil. Here's a hack; can
193 you think of something better to do? Find another buffer with a
194 better directory, and use that one instead. */
195 if (STRINGP (val))
196 current_buffer->directory = val;
197 else
199 Lisp_Object buf_list;
201 for (buf_list = Vbuffer_alist;
202 CONSP (buf_list);
203 buf_list = XCONS (buf_list)->cdr)
205 Lisp_Object other_buf;
207 other_buf = XCONS (XCONS (buf_list)->car)->cdr;
208 if (STRINGP (XBUFFER (other_buf)->directory))
210 current_buffer->directory = XBUFFER (other_buf)->directory;
211 break;
216 #ifdef MULTI_FRAME
217 if (XFRAME (mini_frame) != selected_frame)
218 Fredirect_frame_focus (Fselected_frame (), mini_frame);
219 #endif
220 Fmake_local_variable (Qprint_escape_newlines);
221 print_escape_newlines = 1;
223 record_unwind_protect (read_minibuf_unwind, Qnil);
225 Vminibuf_scroll_window = selected_window;
226 Fset_window_buffer (minibuf_window, Fcurrent_buffer ());
227 Fselect_window (minibuf_window);
228 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
230 Ferase_buffer ();
231 minibuf_level++;
233 if (!NILP (initial))
235 Finsert (1, &initial);
236 if (!NILP (backup_n) && INTEGERP (backup_n))
237 Fforward_char (backup_n);
240 echo_area_glyphs = 0;
241 /* This is in case the minibuffer-setup-hook calls Fsit_for. */
242 previous_echo_glyphs = 0;
244 Vhelp_form = Vminibuffer_help_form;
245 current_buffer->keymap = map;
247 /* Run our hook, but not if it is empty.
248 (run-hooks would do nothing if it is empty,
249 but it's important to save time here in the usual case. */
250 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
251 && !NILP (Vrun_hooks))
252 call1 (Vrun_hooks, Qminibuffer_setup_hook);
254 /* ??? MCC did redraw_screen here if switching screens. */
255 recursive_edit_1 ();
257 /* If cursor is on the minibuffer line,
258 show the user we have exited by putting it in column 0. */
259 if ((FRAME_CURSOR_Y (selected_frame)
260 >= XFASTINT (XWINDOW (minibuf_window)->top))
261 && !noninteractive)
263 FRAME_CURSOR_X (selected_frame) = 0;
264 update_frame (selected_frame, 1, 1);
267 /* Make minibuffer contents into a string */
268 val = make_buffer_string (1, Z);
269 bcopy (GAP_END_ADDR, XSTRING (val)->data + GPT - BEG, Z - GPT);
271 /* VAL is the string of minibuffer text. */
272 last_minibuf_string = val;
274 /* Add the value to the appropriate history list unless it is empty. */
275 if (XSTRING (val)->size != 0
276 && SYMBOLP (Vminibuffer_history_variable)
277 && ! EQ (XSYMBOL (Vminibuffer_history_variable)->value, Qunbound))
279 /* If the caller wanted to save the value read on a history list,
280 then do so if the value is not already the front of the list. */
281 Lisp_Object histval;
282 histval = Fsymbol_value (Vminibuffer_history_variable);
284 /* The value of the history variable must be a cons or nil. Other
285 values are unacceptable. We silently ignore these values. */
286 if (NILP (histval)
287 || (CONSP (histval)
288 && NILP (Fequal (last_minibuf_string, Fcar (histval)))))
289 Fset (Vminibuffer_history_variable,
290 Fcons (last_minibuf_string, histval));
293 /* If Lisp form desired instead of string, parse it. */
294 if (expflag)
296 Lisp_Object expr_and_pos;
297 unsigned char *p;
299 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
300 /* Ignore trailing whitespace; any other trailing junk is an error. */
301 for (p = XSTRING (val)->data + XINT (Fcdr (expr_and_pos)); *p; p++)
302 if (*p != ' ' && *p != '\t' && *p != '\n')
303 error ("Trailing garbage following expression");
304 val = Fcar (expr_and_pos);
307 /* The appropriate frame will get selected
308 in set-window-configuration. */
309 RETURN_UNGCPRO (unbind_to (count, val));
312 /* Return a buffer to be used as the minibuffer at depth `depth'.
313 depth = 0 is the lowest allowed argument, and that is the value
314 used for nonrecursive minibuffer invocations */
316 Lisp_Object
317 get_minibuffer (depth)
318 int depth;
320 Lisp_Object tail, num, buf;
321 char name[24];
322 extern Lisp_Object nconc2 ();
324 XSETFASTINT (num, depth);
325 tail = Fnthcdr (num, Vminibuffer_list);
326 if (NILP (tail))
328 tail = Fcons (Qnil, Qnil);
329 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
331 buf = Fcar (tail);
332 if (NILP (buf) || NILP (XBUFFER (buf)->name))
334 sprintf (name, " *Minibuf-%d*", depth);
335 buf = Fget_buffer_create (build_string (name));
337 /* Although the buffer's name starts with a space, undo should be
338 enabled in it. */
339 Fbuffer_enable_undo (buf);
341 XCONS (tail)->car = buf;
343 else
345 int count = specpdl_ptr - specpdl;
347 reset_buffer (XBUFFER (buf));
348 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
349 Fset_buffer (buf);
350 Fkill_all_local_variables ();
351 unbind_to (count, Qnil);
354 return buf;
357 /* This function is called on exiting minibuffer, whether normally or not,
358 and it restores the current window, buffer, etc. */
360 void
361 read_minibuf_unwind (data)
362 Lisp_Object data;
364 Lisp_Object old_deactivate_mark;
366 /* We are exiting the minibuffer one way or the other,
367 so run the hook. */
368 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
369 && !NILP (Vrun_hooks))
370 call1 (Vrun_hooks, Qminibuffer_exit_hook);
372 /* Erase the minibuffer we were using at this level. */
373 Fset_buffer (XWINDOW (minibuf_window)->buffer);
375 /* Prevent error in erase-buffer. */
376 current_buffer->read_only = Qnil;
378 old_deactivate_mark = Vdeactivate_mark;
379 Ferase_buffer ();
380 Vdeactivate_mark = old_deactivate_mark;
382 /* If this was a recursive minibuffer,
383 tie the minibuffer window back to the outer level minibuffer buffer */
384 minibuf_level--;
385 /* Make sure minibuffer window is erased, not ignored */
386 windows_or_buffers_changed++;
387 XSETFASTINT (XWINDOW (minibuf_window)->last_modified, 0);
389 /* Restore prompt, etc from outer minibuffer */
390 minibuf_prompt = Fcar (minibuf_save_list);
391 minibuf_save_list = Fcdr (minibuf_save_list);
392 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
393 minibuf_save_list = Fcdr (minibuf_save_list);
394 Vhelp_form = Fcar (minibuf_save_list);
395 minibuf_save_list = Fcdr (minibuf_save_list);
396 current_perdisplay->Vcurrent_prefix_arg = Fcar (minibuf_save_list);
397 minibuf_save_list = Fcdr (minibuf_save_list);
398 Vminibuffer_history_position = Fcar (minibuf_save_list);
399 minibuf_save_list = Fcdr (minibuf_save_list);
400 Vminibuffer_history_variable = Fcar (minibuf_save_list);
401 minibuf_save_list = Fcdr (minibuf_save_list);
405 /* This comment supplies the doc string for read-from-minibuffer,
406 for make-docfile to see. We cannot put this in the real DEFUN
407 due to limits in the Unix cpp.
409 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0,
410 "Read a string from the minibuffer, prompting with string PROMPT.\n\
411 If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
412 to be inserted into the minibuffer before reading input.\n\
413 If INITIAL-CONTENTS is (STRING . POSITION), the initial input\n\
414 is STRING, but point is placed POSITION characters into the string.\n\
415 Third arg KEYMAP is a keymap to use whilst reading;\n\
416 if omitted or nil, the default is `minibuffer-local-map'.\n\
417 If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
418 and return that object:\n\
419 in other words, do `(car (read-from-string INPUT-STRING))'\n\
420 Fifth arg HIST, if non-nil, specifies a history list\n\
421 and optionally the initial position in the list.\n\
422 It can be a symbol, which is the history list variable to use,\n\
423 or it can be a cons cell (HISTVAR . HISTPOS).\n\
424 In that case, HISTVAR is the history list variable to use,\n\
425 and HISTPOS is the initial position (the position in the list\n\
426 which INITIAL-CONTENTS corresponds to).\n\
427 Positions are counted starting from 1 at the beginning of the list."
430 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0,
431 0 /* See immediately above */)
432 (prompt, initial_contents, keymap, read, hist)
433 Lisp_Object prompt, initial_contents, keymap, read, hist;
435 int pos = 0;
436 Lisp_Object histvar, histpos, position;
437 position = Qnil;
439 CHECK_STRING (prompt, 0);
440 if (!NILP (initial_contents))
442 if (CONSP (initial_contents))
444 position = Fcdr (initial_contents);
445 initial_contents = Fcar (initial_contents);
447 CHECK_STRING (initial_contents, 1);
448 if (!NILP (position))
450 CHECK_NUMBER (position, 0);
451 /* Convert to distance from end of input. */
452 pos = XINT (position) - 1 - XSTRING (initial_contents)->size;
456 if (NILP (keymap))
457 keymap = Vminibuffer_local_map;
458 else
459 keymap = get_keymap (keymap,2);
461 if (SYMBOLP (hist))
463 histvar = hist;
464 histpos = Qnil;
466 else
468 histvar = Fcar_safe (hist);
469 histpos = Fcdr_safe (hist);
471 if (NILP (histvar))
472 histvar = Qminibuffer_history;
473 if (NILP (histpos))
474 XSETFASTINT (histpos, 0);
476 return read_minibuf (keymap, initial_contents, prompt,
477 make_number (pos), !NILP (read), histvar, histpos);
480 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
481 "Return a Lisp object read using the minibuffer.\n\
482 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
483 is a string to insert in the minibuffer before reading.")
484 (prompt, initial_contents)
485 Lisp_Object prompt, initial_contents;
487 CHECK_STRING (prompt, 0);
488 if (!NILP (initial_contents))
489 CHECK_STRING (initial_contents, 1);
490 return read_minibuf (Vminibuffer_local_map, initial_contents,
491 prompt, Qnil, 1, Qminibuffer_history, make_number (0));
494 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
495 "Return value of Lisp expression read using the minibuffer.\n\
496 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
497 is a string to insert in the minibuffer before reading.")
498 (prompt, initial_contents)
499 Lisp_Object prompt, initial_contents;
501 return Feval (Fread_minibuffer (prompt, initial_contents));
504 /* Functions that use the minibuffer to read various things. */
506 DEFUN ("read-string", Fread_string, Sread_string, 1, 3, 0,
507 "Read a string from the minibuffer, prompting with string PROMPT.\n\
508 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.\n\
509 The third arg HISTORY, if non-nil, specifies a history list\n\
510 and optionally the initial position in the list.\n\
511 See `read-from-minibuffer' for details of HISTORY argument.")
512 (prompt, initial_input, history)
513 Lisp_Object prompt, initial_input, history;
515 return Fread_from_minibuffer (prompt, initial_input, Qnil, Qnil, history);
518 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 2, 0,
519 "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\
520 Prompt with PROMPT, and provide INIT as an initial value of the input string.")
521 (prompt, init)
522 Lisp_Object prompt, init;
524 CHECK_STRING (prompt, 0);
525 if (! NILP (init))
526 CHECK_STRING (init, 1);
528 return read_minibuf (Vminibuffer_local_ns_map, init, prompt, Qnil, 0,
529 Qminibuffer_history, make_number (0));
532 DEFUN ("read-command", Fread_command, Sread_command, 1, 1, 0,
533 "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\
534 Prompts with PROMPT.")
535 (prompt)
536 Lisp_Object prompt;
538 return Fintern (Fcompleting_read (prompt, Vobarray, Qcommandp, Qt, Qnil, Qnil),
539 Qnil);
542 #ifdef NOTDEF
543 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
544 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
545 Prompts with PROMPT.")
546 (prompt)
547 Lisp_Object prompt;
549 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil),
550 Qnil);
552 #endif /* NOTDEF */
554 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 1, 0,
555 "One arg PROMPT, a string. Read the name of a user variable and return\n\
556 it as a symbol. Prompts with PROMPT.\n\
557 A user variable is one whose documentation starts with a `*' character.")
558 (prompt)
559 Lisp_Object prompt;
561 return Fintern (Fcompleting_read (prompt, Vobarray,
562 Quser_variable_p, Qt, Qnil, Qnil),
563 Qnil);
566 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
567 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
568 Prompts with PROMPT.\n\
569 Optional second arg is value to return if user enters an empty line.\n\
570 If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
571 (prompt, def, require_match)
572 Lisp_Object prompt, def, require_match;
574 Lisp_Object tem;
575 Lisp_Object args[3];
576 struct gcpro gcpro1;
578 if (BUFFERP (def))
579 def = XBUFFER (def)->name;
580 if (!NILP (def))
582 args[0] = build_string ("%s(default %s) ");
583 args[1] = prompt;
584 args[2] = def;
585 prompt = Fformat (3, args);
587 GCPRO1 (def);
588 tem = Fcompleting_read (prompt, Vbuffer_alist, Qnil, require_match, Qnil, Qnil);
589 UNGCPRO;
590 if (XSTRING (tem)->size)
591 return tem;
592 return def;
595 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
596 "Return common substring of all completions of STRING in ALIST.\n\
597 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
598 All that match are compared together; the longest initial sequence\n\
599 common to all matches is returned as a string.\n\
600 If there is no match at all, nil is returned.\n\
601 For an exact match, t is returned.\n\
603 ALIST can be an obarray instead of an alist.\n\
604 Then the print names of all symbols in the obarray are the possible matches.\n\
606 ALIST can also be a function to do the completion itself.\n\
607 It receives three arguments: the values STRING, PREDICATE and nil.\n\
608 Whatever it returns becomes the value of `try-completion'.\n\
610 If optional third argument PREDICATE is non-nil,\n\
611 it is used to test each possible match.\n\
612 The match is a candidate only if PREDICATE returns non-nil.\n\
613 The argument given to PREDICATE is the alist element\n\
614 or the symbol from the obarray.")
615 (string, alist, pred)
616 Lisp_Object string, alist, pred;
618 Lisp_Object bestmatch, tail, elt, eltstring;
619 int bestmatchsize;
620 int compare, matchsize;
621 int list = CONSP (alist) || NILP (alist);
622 int index, obsize;
623 int matchcount = 0;
624 Lisp_Object bucket, zero, end, tem;
625 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
627 CHECK_STRING (string, 0);
628 if (!list && !VECTORP (alist))
629 return call3 (alist, string, pred, Qnil);
631 bestmatch = Qnil;
633 /* If ALIST is not a list, set TAIL just for gc pro. */
634 tail = alist;
635 if (! list)
637 index = 0;
638 obsize = XVECTOR (alist)->size;
639 bucket = XVECTOR (alist)->contents[index];
642 while (1)
644 /* Get the next element of the alist or obarray. */
645 /* Exit the loop if the elements are all used up. */
646 /* elt gets the alist element or symbol.
647 eltstring gets the name to check as a completion. */
649 if (list)
651 if (NILP (tail))
652 break;
653 elt = Fcar (tail);
654 eltstring = Fcar (elt);
655 tail = Fcdr (tail);
657 else
659 if (XFASTINT (bucket) != 0)
661 elt = bucket;
662 eltstring = Fsymbol_name (elt);
663 if (XSYMBOL (bucket)->next)
664 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
665 else
666 XSETFASTINT (bucket, 0);
668 else if (++index >= obsize)
669 break;
670 else
672 bucket = XVECTOR (alist)->contents[index];
673 continue;
677 /* Is this element a possible completion? */
679 if (STRINGP (eltstring)
680 && XSTRING (string)->size <= XSTRING (eltstring)->size
681 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
682 XSTRING (string)->size))
684 /* Yes. */
685 Lisp_Object regexps;
686 Lisp_Object zero;
687 XSETFASTINT (zero, 0);
689 /* Ignore this element if it fails to match all the regexps. */
690 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
691 regexps = XCONS (regexps)->cdr)
693 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
694 if (NILP (tem))
695 break;
697 if (CONSP (regexps))
698 continue;
700 /* Ignore this element if there is a predicate
701 and the predicate doesn't like it. */
703 if (!NILP (pred))
705 if (EQ (pred, Qcommandp))
706 tem = Fcommandp (elt);
707 else
709 GCPRO4 (tail, string, eltstring, bestmatch);
710 tem = call1 (pred, elt);
711 UNGCPRO;
713 if (NILP (tem)) continue;
716 /* Update computation of how much all possible completions match */
718 matchcount++;
719 if (NILP (bestmatch))
720 bestmatch = eltstring, bestmatchsize = XSTRING (eltstring)->size;
721 else
723 compare = min (bestmatchsize, XSTRING (eltstring)->size);
724 matchsize = scmp (XSTRING (bestmatch)->data,
725 XSTRING (eltstring)->data,
726 compare);
727 if (matchsize < 0)
728 matchsize = compare;
729 if (completion_ignore_case)
731 /* If this is an exact match except for case,
732 use it as the best match rather than one that is not an
733 exact match. This way, we get the case pattern
734 of the actual match. */
735 if ((matchsize == XSTRING (eltstring)->size
736 && matchsize < XSTRING (bestmatch)->size)
738 /* If there is more than one exact match ignoring case,
739 and one of them is exact including case,
740 prefer that one. */
741 /* If there is no exact match ignoring case,
742 prefer a match that does not change the case
743 of the input. */
744 ((matchsize == XSTRING (eltstring)->size)
746 (matchsize == XSTRING (bestmatch)->size)
747 && !bcmp (XSTRING (eltstring)->data,
748 XSTRING (string)->data, XSTRING (string)->size)
749 && bcmp (XSTRING (bestmatch)->data,
750 XSTRING (string)->data, XSTRING (string)->size)))
751 bestmatch = eltstring;
753 bestmatchsize = matchsize;
758 if (NILP (bestmatch))
759 return Qnil; /* No completions found */
760 /* If we are ignoring case, and there is no exact match,
761 and no additional text was supplied,
762 don't change the case of what the user typed. */
763 if (completion_ignore_case && bestmatchsize == XSTRING (string)->size
764 && XSTRING (bestmatch)->size > bestmatchsize)
765 return string;
767 /* Return t if the supplied string is an exact match (counting case);
768 it does not require any change to be made. */
769 if (matchcount == 1 && bestmatchsize == XSTRING (string)->size
770 && !bcmp (XSTRING (bestmatch)->data, XSTRING (string)->data,
771 bestmatchsize))
772 return Qt;
774 XSETFASTINT (zero, 0); /* Else extract the part in which */
775 XSETFASTINT (end, bestmatchsize); /* all completions agree */
776 return Fsubstring (bestmatch, zero, end);
779 /* Compare exactly LEN chars of strings at S1 and S2,
780 ignoring case if appropriate.
781 Return -1 if strings match,
782 else number of chars that match at the beginning. */
785 scmp (s1, s2, len)
786 register unsigned char *s1, *s2;
787 int len;
789 register int l = len;
791 if (completion_ignore_case)
793 while (l && DOWNCASE (*s1++) == DOWNCASE (*s2++))
794 l--;
796 else
798 while (l && *s1++ == *s2++)
799 l--;
801 if (l == 0)
802 return -1;
803 else
804 return len - l;
807 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
808 "Search for partial matches to STRING in ALIST.\n\
809 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
810 The value is a list of all the strings from ALIST that match.\n\
812 ALIST can be an obarray instead of an alist.\n\
813 Then the print names of all symbols in the obarray are the possible matches.\n\
815 ALIST can also be a function to do the completion itself.\n\
816 It receives three arguments: the values STRING, PREDICATE and t.\n\
817 Whatever it returns becomes the value of `all-completion'.\n\
819 If optional third argument PREDICATE is non-nil,\n\
820 it is used to test each possible match.\n\
821 The match is a candidate only if PREDICATE returns non-nil.\n\
822 The argument given to PREDICATE is the alist element\n\
823 or the symbol from the obarray.\n\
825 If the optional fourth argument HIDE-SPACES is non-nil,\n\
826 strings in ALIST that start with a space\n\
827 are ignored unless STRING itself starts with a space.")
828 (string, alist, pred, hide_spaces)
829 Lisp_Object string, alist, pred, hide_spaces;
831 Lisp_Object tail, elt, eltstring;
832 Lisp_Object allmatches;
833 int list = CONSP (alist) || NILP (alist);
834 int index, obsize;
835 Lisp_Object bucket, tem;
836 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
838 CHECK_STRING (string, 0);
839 if (!list && !VECTORP (alist))
841 return call3 (alist, string, pred, Qt);
843 allmatches = Qnil;
845 /* If ALIST is not a list, set TAIL just for gc pro. */
846 tail = alist;
847 if (! list)
849 index = 0;
850 obsize = XVECTOR (alist)->size;
851 bucket = XVECTOR (alist)->contents[index];
854 while (1)
856 /* Get the next element of the alist or obarray. */
857 /* Exit the loop if the elements are all used up. */
858 /* elt gets the alist element or symbol.
859 eltstring gets the name to check as a completion. */
861 if (list)
863 if (NILP (tail))
864 break;
865 elt = Fcar (tail);
866 eltstring = Fcar (elt);
867 tail = Fcdr (tail);
869 else
871 if (XFASTINT (bucket) != 0)
873 elt = bucket;
874 eltstring = Fsymbol_name (elt);
875 if (XSYMBOL (bucket)->next)
876 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
877 else
878 XSETFASTINT (bucket, 0);
880 else if (++index >= obsize)
881 break;
882 else
884 bucket = XVECTOR (alist)->contents[index];
885 continue;
889 /* Is this element a possible completion? */
891 if (STRINGP (eltstring)
892 && XSTRING (string)->size <= XSTRING (eltstring)->size
893 /* If HIDE_SPACES, reject alternatives that start with space
894 unless the input starts with space. */
895 && ((XSTRING (string)->size > 0 && XSTRING (string)->data[0] == ' ')
896 || XSTRING (eltstring)->data[0] != ' '
897 || NILP (hide_spaces))
898 && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
899 XSTRING (string)->size))
901 /* Yes. */
902 Lisp_Object regexps;
903 Lisp_Object zero;
904 XSETFASTINT (zero, 0);
906 /* Ignore this element if it fails to match all the regexps. */
907 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
908 regexps = XCONS (regexps)->cdr)
910 tem = Fstring_match (XCONS (regexps)->car, eltstring, zero);
911 if (NILP (tem))
912 break;
914 if (CONSP (regexps))
915 continue;
917 /* Ignore this element if there is a predicate
918 and the predicate doesn't like it. */
920 if (!NILP (pred))
922 if (EQ (pred, Qcommandp))
923 tem = Fcommandp (elt);
924 else
926 GCPRO4 (tail, eltstring, allmatches, string);
927 tem = call1 (pred, elt);
928 UNGCPRO;
930 if (NILP (tem)) continue;
932 /* Ok => put it on the list. */
933 allmatches = Fcons (eltstring, allmatches);
937 return Fnreverse (allmatches);
940 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
941 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
942 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
944 /* This comment supplies the doc string for completing-read,
945 for make-docfile to see. We cannot put this in the real DEFUN
946 due to limits in the Unix cpp.
948 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0,
949 "Read a string in the minibuffer, with completion.\n\
950 Args: PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HIST.\n\
951 PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
952 TABLE is an alist whose elements' cars are strings, or an obarray.\n\
953 PREDICATE limits completion to a subset of TABLE.\n\
954 See `try-completion' and `all-completions' for more details
955 on completion, TABLE, and PREDICATE.\n\
957 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
958 the input is (or completes to) an element of TABLE or is null.\n\
959 If it is also not t, Return does not exit if it does non-null completion.\n\
960 If the input is null, `completing-read' returns nil,\n\
961 regardless of the value of REQUIRE-MATCH.\n\
963 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
964 If it is (STRING . POSITION), the initial input\n\
965 is STRING, but point is placed POSITION characters into the string.\n\
966 HIST, if non-nil, specifies a history list\n\
967 and optionally the initial position in the list.\n\
968 It can be a symbol, which is the history list variable to use,\n\
969 or it can be a cons cell (HISTVAR . HISTPOS).\n\
970 In that case, HISTVAR is the history list variable to use,\n\
971 and HISTPOS is the initial position (the position in the list\n\
972 which INITIAL-CONTENTS corresponds to).\n\
973 Positions are counted starting from 1 at the beginning of the list.\n\
974 Completion ignores case if the ambient value of\n\
975 `completion-ignore-case' is non-nil."
977 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0,
978 0 /* See immediately above */)
979 (prompt, table, pred, require_match, init, hist)
980 Lisp_Object prompt, table, pred, require_match, init, hist;
982 Lisp_Object val, histvar, histpos, position;
983 int pos = 0;
984 int count = specpdl_ptr - specpdl;
985 specbind (Qminibuffer_completion_table, table);
986 specbind (Qminibuffer_completion_predicate, pred);
987 specbind (Qminibuffer_completion_confirm,
988 EQ (require_match, Qt) ? Qnil : Qt);
989 last_exact_completion = Qnil;
991 position = Qnil;
992 if (!NILP (init))
994 if (CONSP (init))
996 position = Fcdr (init);
997 init = Fcar (init);
999 CHECK_STRING (init, 0);
1000 if (!NILP (position))
1002 CHECK_NUMBER (position, 0);
1003 /* Convert to distance from end of input. */
1004 pos = XINT (position) - XSTRING (init)->size;
1008 if (SYMBOLP (hist))
1010 histvar = hist;
1011 histpos = Qnil;
1013 else
1015 histvar = Fcar_safe (hist);
1016 histpos = Fcdr_safe (hist);
1018 if (NILP (histvar))
1019 histvar = Qminibuffer_history;
1020 if (NILP (histpos))
1021 XSETFASTINT (histpos, 0);
1023 val = read_minibuf (NILP (require_match)
1024 ? Vminibuffer_local_completion_map
1025 : Vminibuffer_local_must_match_map,
1026 init, prompt, make_number (pos), 0,
1027 histvar, histpos);
1028 return unbind_to (count, val);
1031 /* Temporarily display the string M at the end of the current
1032 minibuffer contents. This is used to display things like
1033 "[No Match]" when the user requests a completion for a prefix
1034 that has no possible completions, and other quick, unobtrusive
1035 messages. */
1037 temp_echo_area_glyphs (m)
1038 char *m;
1040 int osize = ZV;
1041 Lisp_Object oinhibit;
1042 oinhibit = Vinhibit_quit;
1044 /* Clear out any old echo-area message to make way for our new thing. */
1045 message (0);
1047 SET_PT (osize);
1048 insert_string (m);
1049 SET_PT (osize);
1050 Vinhibit_quit = Qt;
1051 Fsit_for (make_number (2), Qnil, Qnil);
1052 del_range (PT, ZV);
1053 if (!NILP (Vquit_flag))
1055 Vquit_flag = Qnil;
1056 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1058 Vinhibit_quit = oinhibit;
1061 Lisp_Object Fminibuffer_completion_help ();
1062 Lisp_Object assoc_for_completion ();
1063 /* A subroutine of Fintern_soft. */
1064 extern Lisp_Object oblookup ();
1067 /* Test whether TXT is an exact completion. */
1068 Lisp_Object
1069 test_completion (txt)
1070 Lisp_Object txt;
1072 Lisp_Object tem;
1074 if (CONSP (Vminibuffer_completion_table)
1075 || NILP (Vminibuffer_completion_table))
1076 return assoc_for_completion (txt, Vminibuffer_completion_table);
1077 else if (VECTORP (Vminibuffer_completion_table))
1079 /* Bypass intern-soft as that loses for nil */
1080 tem = oblookup (Vminibuffer_completion_table,
1081 XSTRING (txt)->data, XSTRING (txt)->size);
1082 if (!SYMBOLP (tem))
1083 return Qnil;
1084 else if (!NILP (Vminibuffer_completion_predicate))
1085 return call1 (Vminibuffer_completion_predicate, tem);
1086 else
1087 return Qt;
1089 else
1090 return call3 (Vminibuffer_completion_table, txt,
1091 Vminibuffer_completion_predicate, Qlambda);
1094 /* returns:
1095 * 0 no possible completion
1096 * 1 was already an exact and unique completion
1097 * 3 was already an exact completion
1098 * 4 completed to an exact completion
1099 * 5 some completion happened
1100 * 6 no completion happened
1103 do_completion ()
1105 Lisp_Object completion, tem;
1106 int completedp;
1107 Lisp_Object last;
1108 struct gcpro gcpro1, gcpro2;
1110 completion = Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table,
1111 Vminibuffer_completion_predicate);
1112 last = last_exact_completion;
1113 last_exact_completion = Qnil;
1115 GCPRO2 (completion, last);
1117 if (NILP (completion))
1119 bitch_at_user ();
1120 temp_echo_area_glyphs (" [No match]");
1121 UNGCPRO;
1122 return 0;
1125 if (EQ (completion, Qt)) /* exact and unique match */
1127 UNGCPRO;
1128 return 1;
1131 /* compiler bug */
1132 tem = Fstring_equal (completion, Fbuffer_string());
1133 if (completedp = NILP (tem))
1135 Ferase_buffer (); /* Some completion happened */
1136 Finsert (1, &completion);
1139 /* It did find a match. Do we match some possibility exactly now? */
1140 tem = test_completion (Fbuffer_string ());
1141 if (NILP (tem))
1143 /* not an exact match */
1144 UNGCPRO;
1145 if (completedp)
1146 return 5;
1147 else if (auto_help)
1148 Fminibuffer_completion_help ();
1149 else
1150 temp_echo_area_glyphs (" [Next char not unique]");
1151 return 6;
1153 else if (completedp)
1155 UNGCPRO;
1156 return 4;
1158 /* If the last exact completion and this one were the same,
1159 it means we've already given a "Complete but not unique"
1160 message and the user's hit TAB again, so now we give him help. */
1161 last_exact_completion = completion;
1162 if (!NILP (last))
1164 tem = Fbuffer_string ();
1165 if (!NILP (Fequal (tem, last)))
1166 Fminibuffer_completion_help ();
1168 UNGCPRO;
1169 return 3;
1172 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1174 Lisp_Object
1175 assoc_for_completion (key, list)
1176 register Lisp_Object key;
1177 Lisp_Object list;
1179 register Lisp_Object tail;
1181 if (completion_ignore_case)
1182 key = Fupcase (key);
1184 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1186 register Lisp_Object elt, tem, thiscar;
1187 elt = Fcar (tail);
1188 if (!CONSP (elt)) continue;
1189 thiscar = Fcar (elt);
1190 if (!STRINGP (thiscar))
1191 continue;
1192 if (completion_ignore_case)
1193 thiscar = Fupcase (thiscar);
1194 tem = Fequal (thiscar, key);
1195 if (!NILP (tem)) return elt;
1196 QUIT;
1198 return Qnil;
1201 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1202 "Complete the minibuffer contents as far as possible.\n\
1203 Return nil if there is no valid completion, else t.\n\
1204 If no characters can be completed, display a list of possible completions.\n\
1205 If you repeat this command after it displayed such a list,\n\
1206 scroll the window of possible completions.")
1209 register int i;
1210 Lisp_Object window, tem;
1212 /* If the previous command was not this, then mark the completion
1213 buffer obsolete. */
1214 if (! EQ (last_command, this_command))
1215 Vminibuf_scroll_window = Qnil;
1217 window = Vminibuf_scroll_window;
1218 /* If there's a fresh completion window with a live buffer,
1219 and this command is repeated, scroll that window. */
1220 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1221 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1223 struct buffer *obuf = current_buffer;
1225 Fset_buffer (XWINDOW (window)->buffer);
1226 tem = Fpos_visible_in_window_p (make_number (ZV), window);
1227 if (! NILP (tem))
1228 /* If end is in view, scroll up to the beginning. */
1229 Fset_window_start (window, BEGV, Qnil);
1230 else
1231 /* Else scroll down one screen. */
1232 Fscroll_other_window (Qnil);
1234 set_buffer_internal (obuf);
1235 return Qnil;
1238 i = do_completion ();
1239 switch (i)
1241 case 0:
1242 return Qnil;
1244 case 1:
1245 temp_echo_area_glyphs (" [Sole completion]");
1246 break;
1248 case 3:
1249 temp_echo_area_glyphs (" [Complete, but not unique]");
1250 break;
1253 return Qt;
1256 /* Subroutines of Fminibuffer_complete_and_exit. */
1258 /* This one is called by internal_condition_case to do the real work. */
1260 Lisp_Object
1261 complete_and_exit_1 ()
1263 return make_number (do_completion ());
1266 /* This one is called by internal_condition_case if an error happens.
1267 Pretend the current value is an exact match. */
1269 Lisp_Object
1270 complete_and_exit_2 (ignore)
1271 Lisp_Object ignore;
1273 return make_number (1);
1276 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1277 Sminibuffer_complete_and_exit, 0, 0, "",
1278 "If the minibuffer contents is a valid completion then exit.\n\
1279 Otherwise try to complete it. If completion leads to a valid completion,\n\
1280 a repetition of this command will exit.")
1283 register int i;
1284 Lisp_Object val;
1286 /* Allow user to specify null string */
1287 if (BEGV == ZV)
1288 goto exit;
1290 if (!NILP (test_completion (Fbuffer_string ())))
1291 goto exit;
1293 /* Call do_completion, but ignore errors. */
1294 val = internal_condition_case (complete_and_exit_1, Qerror,
1295 complete_and_exit_2);
1297 i = XFASTINT (val);
1298 switch (i)
1300 case 1:
1301 case 3:
1302 goto exit;
1304 case 4:
1305 if (!NILP (Vminibuffer_completion_confirm))
1307 temp_echo_area_glyphs (" [Confirm]");
1308 return Qnil;
1310 else
1311 goto exit;
1313 default:
1314 return Qnil;
1316 exit:
1317 Fthrow (Qexit, Qnil);
1318 /* NOTREACHED */
1321 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1322 0, 0, "",
1323 "Complete the minibuffer contents at most a single word.\n\
1324 After one word is completed as much as possible, a space or hyphen\n\
1325 is added, provided that matches some possible completion.\n\
1326 Return nil if there is no valid completion, else t.")
1329 Lisp_Object completion, tem;
1330 register int i;
1331 register unsigned char *completion_string;
1332 struct gcpro gcpro1, gcpro2;
1334 /* We keep calling Fbuffer_string rather than arrange for GC to
1335 hold onto a pointer to one of the strings thus made. */
1337 completion = Ftry_completion (Fbuffer_string (),
1338 Vminibuffer_completion_table,
1339 Vminibuffer_completion_predicate);
1340 if (NILP (completion))
1342 bitch_at_user ();
1343 temp_echo_area_glyphs (" [No match]");
1344 return Qnil;
1346 if (EQ (completion, Qt))
1347 return Qnil;
1349 #if 0 /* How the below code used to look, for reference. */
1350 tem = Fbuffer_string ();
1351 b = XSTRING (tem)->data;
1352 i = ZV - 1 - XSTRING (completion)->size;
1353 p = XSTRING (completion)->data;
1354 if (i > 0 ||
1355 0 <= scmp (b, p, ZV - 1))
1357 i = 1;
1358 /* Set buffer to longest match of buffer tail and completion head. */
1359 while (0 <= scmp (b + i, p, ZV - 1 - i))
1360 i++;
1361 del_range (1, i + 1);
1362 SET_PT (ZV);
1364 #else /* Rewritten code */
1366 register unsigned char *buffer_string;
1367 int buffer_length, completion_length;
1369 tem = Fbuffer_string ();
1370 GCPRO2 (completion, tem);
1371 /* If reading a file name,
1372 expand any $ENVVAR refs in the buffer and in TEM. */
1373 if (EQ (Vminibuffer_completion_table, Qread_file_name_internal))
1375 Lisp_Object substituted;
1376 substituted = Fsubstitute_in_file_name (tem);
1377 if (! EQ (substituted, tem))
1379 tem = substituted;
1380 Ferase_buffer ();
1381 insert_from_string (tem, 0, XSTRING (tem)->size, 0);
1384 buffer_string = XSTRING (tem)->data;
1385 completion_string = XSTRING (completion)->data;
1386 buffer_length = XSTRING (tem)->size; /* ie ZV - BEGV */
1387 completion_length = XSTRING (completion)->size;
1388 i = buffer_length - completion_length;
1389 /* Mly: I don't understand what this is supposed to do AT ALL */
1390 if (i > 0 ||
1391 0 <= scmp (buffer_string, completion_string, buffer_length))
1393 /* Set buffer to longest match of buffer tail and completion head. */
1394 if (i <= 0) i = 1;
1395 buffer_string += i;
1396 buffer_length -= i;
1397 while (0 <= scmp (buffer_string++, completion_string, buffer_length--))
1398 i++;
1399 del_range (1, i + 1);
1400 SET_PT (ZV);
1402 UNGCPRO;
1404 #endif /* Rewritten code */
1405 i = ZV - BEGV;
1407 /* If completion finds next char not unique,
1408 consider adding a space or a hyphen. */
1409 if (i == XSTRING (completion)->size)
1411 GCPRO1 (completion);
1412 tem = Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")),
1413 Vminibuffer_completion_table,
1414 Vminibuffer_completion_predicate);
1415 UNGCPRO;
1417 if (STRINGP (tem))
1418 completion = tem;
1419 else
1421 GCPRO1 (completion);
1422 tem =
1423 Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")),
1424 Vminibuffer_completion_table,
1425 Vminibuffer_completion_predicate);
1426 UNGCPRO;
1428 if (STRINGP (tem))
1429 completion = tem;
1433 /* Now find first word-break in the stuff found by completion.
1434 i gets index in string of where to stop completing. */
1436 completion_string = XSTRING (completion)->data;
1438 for (; i < XSTRING (completion)->size; i++)
1439 if (SYNTAX (completion_string[i]) != Sword) break;
1440 if (i < XSTRING (completion)->size)
1441 i = i + 1;
1443 /* If got no characters, print help for user. */
1445 if (i == ZV - BEGV)
1447 if (auto_help)
1448 Fminibuffer_completion_help ();
1449 return Qnil;
1452 /* Otherwise insert in minibuffer the chars we got */
1454 Ferase_buffer ();
1455 insert_from_string (completion, 0, i, 1);
1456 return Qt;
1459 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
1460 1, 1, 0,
1461 "Display the list of completions, COMPLETIONS, using `standard-output'.\n\
1462 Each element may be just a symbol or string\n\
1463 or may be a list of two strings to be printed as if concatenated.\n\
1464 `standard-output' must be a buffer.\n\
1465 At the end, run the normal hook `completion-setup-hook'.\n\
1466 It can find the completion buffer in `standard-output'.")
1467 (completions)
1468 Lisp_Object completions;
1470 register Lisp_Object tail, elt;
1471 register int i;
1472 int column = 0;
1473 struct gcpro gcpro1;
1474 struct buffer *old = current_buffer;
1475 int first = 1;
1477 /* Note that (when it matters) every variable
1478 points to a non-string that is pointed to by COMPLETIONS. */
1479 GCPRO1 (completions);
1481 if (BUFFERP (Vstandard_output))
1482 set_buffer_internal (XBUFFER (Vstandard_output));
1484 if (NILP (completions))
1485 write_string ("There are no possible completions of what you have typed.",
1486 -1);
1487 else
1489 write_string ("Possible completions are:", -1);
1490 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
1492 Lisp_Object tem;
1493 int length;
1495 elt = Fcar (tail);
1496 /* Compute the length of this element. */
1497 if (CONSP (elt))
1499 tem = Fcar (elt);
1500 CHECK_STRING (tem, 0);
1501 length = XINT (XSTRING (tem)->size);
1503 tem = Fcar (Fcdr (elt));
1504 CHECK_STRING (tem, 0);
1505 length += XINT (XSTRING (tem)->size);
1507 else
1509 CHECK_STRING (elt, 0);
1510 length = XINT (XSTRING (elt)->size);
1513 /* This does a bad job for narrower than usual windows.
1514 Sadly, the window it will appear in is not known
1515 until after the text has been made. */
1517 /* If the previous completion was very wide,
1518 or we have two on this line already,
1519 don't put another on the same line. */
1520 if (column > 33 || first
1521 /* If this is really wide, don't put it second on a line. */
1522 || column > 0 && length > 45)
1524 Fterpri (Qnil);
1525 column = 0;
1527 /* Otherwise advance to column 35. */
1528 else
1530 if (BUFFERP (Vstandard_output))
1532 tem = Findent_to (make_number (35), make_number (2));
1533 column = XINT (tem);
1535 else
1539 write_string (" ", -1);
1540 column++;
1542 while (column < 35);
1546 /* Output this element and update COLUMN. */
1547 if (CONSP (elt))
1549 Fprinc (Fcar (elt), Qnil);
1550 Fprinc (Fcar (Fcdr (elt)), Qnil);
1552 else
1553 Fprinc (elt, Qnil);
1555 column += length;
1557 /* If output is to a buffer, recompute COLUMN in a way
1558 that takes account of character widths. */
1559 if (BUFFERP (Vstandard_output))
1561 tem = Fcurrent_column ();
1562 column = XINT (tem);
1565 first = 0;
1569 UNGCPRO;
1571 if (BUFFERP (Vstandard_output))
1572 set_buffer_internal (old);
1574 if (!NILP (Vrun_hooks))
1575 call1 (Vrun_hooks, intern ("completion-setup-hook"));
1577 return Qnil;
1580 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
1581 0, 0, "",
1582 "Display a list of possible completions of the current minibuffer contents.")
1585 Lisp_Object completions;
1587 message ("Making completion list...");
1588 completions = Fall_completions (Fbuffer_string (),
1589 Vminibuffer_completion_table,
1590 Vminibuffer_completion_predicate,
1591 Qt);
1592 echo_area_glyphs = 0;
1594 if (NILP (completions))
1596 bitch_at_user ();
1597 temp_echo_area_glyphs (" [No completions]");
1599 else
1600 internal_with_output_to_temp_buffer ("*Completions*",
1601 Fdisplay_completion_list,
1602 Fsort (completions, Qstring_lessp));
1603 return Qnil;
1606 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
1607 "Terminate minibuffer input.")
1610 if (INTEGERP (last_command_char))
1611 internal_self_insert (last_command_char, 0);
1612 else
1613 bitch_at_user ();
1615 Fthrow (Qexit, Qnil);
1618 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
1619 "Terminate this minibuffer argument.")
1622 Fthrow (Qexit, Qnil);
1625 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
1626 "Return current depth of activations of minibuffer, a nonnegative integer.")
1629 return make_number (minibuf_level);
1632 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
1633 "Return the prompt string of the currently-active minibuffer.\n\
1634 If no minibuffer is active, return nil.")
1637 return Fcopy_sequence (minibuf_prompt);
1640 DEFUN ("minibuffer-prompt-width", Fminibuffer_prompt_width,
1641 Sminibuffer_prompt_width, 0, 0, 0,
1642 "Return the display width of the minibuffer prompt.")
1645 Lisp_Object width;
1646 XSETFASTINT (width, minibuf_prompt_width);
1647 return width;
1650 init_minibuf_once ()
1652 Vminibuffer_list = Qnil;
1653 staticpro (&Vminibuffer_list);
1656 syms_of_minibuf ()
1658 minibuf_level = 0;
1659 minibuf_prompt = Qnil;
1660 staticpro (&minibuf_prompt);
1662 minibuf_save_list = Qnil;
1663 staticpro (&minibuf_save_list);
1665 Qread_file_name_internal = intern ("read-file-name-internal");
1666 staticpro (&Qread_file_name_internal);
1668 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
1669 staticpro (&Qminibuffer_completion_table);
1671 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
1672 staticpro (&Qminibuffer_completion_confirm);
1674 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
1675 staticpro (&Qminibuffer_completion_predicate);
1677 staticpro (&last_exact_completion);
1678 last_exact_completion = Qnil;
1680 staticpro (&last_minibuf_string);
1681 last_minibuf_string = Qnil;
1683 Quser_variable_p = intern ("user-variable-p");
1684 staticpro (&Quser_variable_p);
1686 Qminibuffer_history = intern ("minibuffer-history");
1687 staticpro (&Qminibuffer_history);
1689 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
1690 staticpro (&Qminibuffer_setup_hook);
1692 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
1693 staticpro (&Qminibuffer_exit_hook);
1695 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
1696 "Normal hook run just after entry to minibuffer.");
1697 Vminibuffer_setup_hook = Qnil;
1699 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
1700 "Normal hook run just after exit from minibuffer.");
1701 Vminibuffer_exit_hook = Qnil;
1703 DEFVAR_BOOL ("completion-auto-help", &auto_help,
1704 "*Non-nil means automatically provide help for invalid completion input.");
1705 auto_help = 1;
1707 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
1708 "Non-nil means don't consider case significant in completion.");
1709 completion_ignore_case = 0;
1711 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
1712 "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\
1713 More precisely, this variable makes a difference when the minibuffer window\n\
1714 is the selected window. If you are in some other window, minibuffer commands\n\
1715 are allowed even if a minibuffer is active.");
1716 enable_recursive_minibuffers = 0;
1718 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
1719 "Alist or obarray used for completion in the minibuffer.\n\
1720 This becomes the ALIST argument to `try-completion' and `all-completion'.\n\
1722 The value may alternatively be a function, which is given three arguments:\n\
1723 STRING, the current buffer contents;\n\
1724 PREDICATE, the predicate for filtering possible matches;\n\
1725 CODE, which says what kind of things to do.\n\
1726 CODE can be nil, t or `lambda'.\n\
1727 nil means to return the best completion of STRING, or nil if there is none.\n\
1728 t means to return a list of all possible completions of STRING.\n\
1729 `lambda' means to return t if STRING is a valid completion as it stands.");
1730 Vminibuffer_completion_table = Qnil;
1732 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
1733 "Within call to `completing-read', this holds the PREDICATE argument.");
1734 Vminibuffer_completion_predicate = Qnil;
1736 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
1737 "Non-nil => demand confirmation of completion before exiting minibuffer.");
1738 Vminibuffer_completion_confirm = Qnil;
1740 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
1741 "Value that `help-form' takes on inside the minibuffer.");
1742 Vminibuffer_help_form = Qnil;
1744 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
1745 "History list symbol to add minibuffer values to.\n\
1746 Each minibuffer output is added with\n\
1747 (set minibuffer-history-variable\n\
1748 (cons STRING (symbol-value minibuffer-history-variable)))");
1749 XSETFASTINT (Vminibuffer_history_variable, 0);
1751 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
1752 "Current position of redoing in the history list.");
1753 Vminibuffer_history_position = Qnil;
1755 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
1756 "*Non-nil means entering the minibuffer raises the minibuffer's frame.");
1757 minibuffer_auto_raise = 0;
1759 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
1760 "List of regexps that should restrict possible completions.");
1761 Vcompletion_regexp_list = Qnil;
1763 defsubr (&Sread_from_minibuffer);
1764 defsubr (&Seval_minibuffer);
1765 defsubr (&Sread_minibuffer);
1766 defsubr (&Sread_string);
1767 defsubr (&Sread_command);
1768 defsubr (&Sread_variable);
1769 defsubr (&Sread_buffer);
1770 defsubr (&Sread_no_blanks_input);
1771 defsubr (&Sminibuffer_depth);
1772 defsubr (&Sminibuffer_prompt);
1773 defsubr (&Sminibuffer_prompt_width);
1775 defsubr (&Stry_completion);
1776 defsubr (&Sall_completions);
1777 defsubr (&Scompleting_read);
1778 defsubr (&Sminibuffer_complete);
1779 defsubr (&Sminibuffer_complete_word);
1780 defsubr (&Sminibuffer_complete_and_exit);
1781 defsubr (&Sdisplay_completion_list);
1782 defsubr (&Sminibuffer_completion_help);
1784 defsubr (&Sself_insert_and_exit);
1785 defsubr (&Sexit_minibuffer);
1789 keys_of_minibuf ()
1791 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
1792 "abort-recursive-edit");
1793 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
1794 "exit-minibuffer");
1795 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
1796 "exit-minibuffer");
1798 initial_define_key (Vminibuffer_local_ns_map, Ctl ('g'),
1799 "abort-recursive-edit");
1800 initial_define_key (Vminibuffer_local_ns_map, Ctl ('m'),
1801 "exit-minibuffer");
1802 initial_define_key (Vminibuffer_local_ns_map, Ctl ('j'),
1803 "exit-minibuffer");
1805 initial_define_key (Vminibuffer_local_ns_map, ' ',
1806 "exit-minibuffer");
1807 initial_define_key (Vminibuffer_local_ns_map, '\t',
1808 "exit-minibuffer");
1809 initial_define_key (Vminibuffer_local_ns_map, '?',
1810 "self-insert-and-exit");
1812 initial_define_key (Vminibuffer_local_completion_map, Ctl ('g'),
1813 "abort-recursive-edit");
1814 initial_define_key (Vminibuffer_local_completion_map, Ctl ('m'),
1815 "exit-minibuffer");
1816 initial_define_key (Vminibuffer_local_completion_map, Ctl ('j'),
1817 "exit-minibuffer");
1819 initial_define_key (Vminibuffer_local_completion_map, '\t',
1820 "minibuffer-complete");
1821 initial_define_key (Vminibuffer_local_completion_map, ' ',
1822 "minibuffer-complete-word");
1823 initial_define_key (Vminibuffer_local_completion_map, '?',
1824 "minibuffer-completion-help");
1826 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('g'),
1827 "abort-recursive-edit");
1828 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
1829 "minibuffer-complete-and-exit");
1830 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
1831 "minibuffer-complete-and-exit");
1832 initial_define_key (Vminibuffer_local_must_match_map, '\t',
1833 "minibuffer-complete");
1834 initial_define_key (Vminibuffer_local_must_match_map, ' ',
1835 "minibuffer-complete-word");
1836 initial_define_key (Vminibuffer_local_must_match_map, '?',
1837 "minibuffer-completion-help");