(cvs-butlast, cvs-nbutlast): New (copied from CL).
[emacs.git] / src / minibuf.c
blob29788a39467ed8acca77cbcd061f7eaf5bdaadb1
1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "commands.h"
27 #include "buffer.h"
28 #include "charset.h"
29 #include "dispextern.h"
30 #include "keyboard.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "syntax.h"
35 #define min(a, b) ((a) < (b) ? (a) : (b))
37 extern int quit_char;
39 /* List of buffers for use as minibuffers.
40 The first element of the list is used for the outermost minibuffer
41 invocation, the next element is used for a recursive minibuffer
42 invocation, etc. The list is extended at the end as deeper
43 minibuffer recursions are encountered. */
45 Lisp_Object Vminibuffer_list;
47 /* Data to remember during recursive minibuffer invocations */
49 Lisp_Object minibuf_save_list;
51 /* Depth in minibuffer invocations. */
53 int minibuf_level;
55 /* Nonzero means display completion help for invalid input. */
57 Lisp_Object Vcompletion_auto_help;
59 /* The maximum length of a minibuffer history. */
61 Lisp_Object Qhistory_length, Vhistory_length;
63 /* Fread_minibuffer leaves the input here as a string. */
65 Lisp_Object last_minibuf_string;
67 /* Nonzero means let functions called when within a minibuffer
68 invoke recursive minibuffers (to read arguments, or whatever) */
70 int enable_recursive_minibuffers;
72 /* Nonzero means don't ignore text properties
73 in Fread_from_minibuffer. */
75 int minibuffer_allow_text_properties;
77 /* help-form is bound to this while in the minibuffer. */
79 Lisp_Object Vminibuffer_help_form;
81 /* Variable which is the history list to add minibuffer values to. */
83 Lisp_Object Vminibuffer_history_variable;
85 /* Current position in the history list (adjusted by M-n and M-p). */
87 Lisp_Object Vminibuffer_history_position;
89 Lisp_Object Qminibuffer_history, Qbuffer_name_history;
91 Lisp_Object Qread_file_name_internal;
93 /* Normal hooks for entry to and exit from minibuffer. */
95 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
96 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
98 /* Function to call to read a buffer name. */
99 Lisp_Object Vread_buffer_function;
101 /* Nonzero means completion ignores case. */
103 int completion_ignore_case;
105 /* List of regexps that should restrict possible completions. */
107 Lisp_Object Vcompletion_regexp_list;
109 /* Nonzero means raise the minibuffer frame when the minibuffer
110 is entered. */
112 int minibuffer_auto_raise;
114 /* If last completion attempt reported "Complete but not unique"
115 then this is the string completed then; otherwise this is nil. */
117 static Lisp_Object last_exact_completion;
119 /* Non-nil means it is the window for C-M-v to scroll
120 when the minibuffer is selected. */
122 extern Lisp_Object Vminibuf_scroll_window;
124 extern Lisp_Object Voverriding_local_map;
126 Lisp_Object Quser_variable_p;
128 Lisp_Object Qminibuffer_default;
130 Lisp_Object Qcurrent_input_method, Qactivate_input_method;
132 extern Lisp_Object Qmouse_face;
134 extern Lisp_Object Qfield;
136 /* Put minibuf on currently selected frame's minibuffer.
137 We do this whenever the user starts a new minibuffer
138 or when a minibuffer exits. */
140 void
141 choose_minibuf_frame ()
143 if (FRAMEP (selected_frame)
144 && FRAME_LIVE_P (XFRAME (selected_frame))
145 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
147 struct frame *sf = XFRAME (selected_frame);
148 Lisp_Object buffer;
150 /* I don't think that any frames may validly have a null minibuffer
151 window anymore. */
152 if (NILP (sf->minibuffer_window))
153 abort ();
155 /* Under X, we come here with minibuf_window being the
156 minibuffer window of the unused termcap window created in
157 init_window_once. That window doesn't have a buffer. */
158 buffer = XWINDOW (minibuf_window)->buffer;
159 if (BUFFERP (buffer))
160 Fset_window_buffer (sf->minibuffer_window, buffer);
161 minibuf_window = sf->minibuffer_window;
164 /* Make sure no other frame has a minibuffer as its selected window,
165 because the text would not be displayed in it, and that would be
166 confusing. Only allow the selected frame to do this,
167 and that only if the minibuffer is active. */
169 Lisp_Object tail, frame;
171 FOR_EACH_FRAME (tail, frame)
172 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
173 && !(EQ (frame, selected_frame)
174 && minibuf_level > 0))
175 Fset_frame_selected_window (frame, Fframe_first_window (frame));
179 Lisp_Object
180 choose_minibuf_frame_1 (ignore)
181 Lisp_Object ignore;
183 choose_minibuf_frame ();
184 return Qnil;
187 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
188 Sset_minibuffer_window, 1, 1, 0,
189 "Specify which minibuffer window to use for the minibuffer.\n\
190 This effects where the minibuffer is displayed if you put text in it\n\
191 without invoking the usual minibuffer commands.")
192 (window)
193 Lisp_Object window;
195 CHECK_WINDOW (window, 1);
196 if (! MINI_WINDOW_P (XWINDOW (window)))
197 error ("Window is not a minibuffer window");
199 minibuf_window = window;
201 return window;
205 /* Actual minibuffer invocation. */
207 static Lisp_Object read_minibuf_unwind P_ ((Lisp_Object));
208 static Lisp_Object read_minibuf P_ ((Lisp_Object, Lisp_Object,
209 Lisp_Object, Lisp_Object,
210 int, Lisp_Object,
211 Lisp_Object, Lisp_Object,
212 int, int));
213 static Lisp_Object read_minibuf_noninteractive P_ ((Lisp_Object, Lisp_Object,
214 Lisp_Object, Lisp_Object,
215 int, Lisp_Object,
216 Lisp_Object, Lisp_Object,
217 int, int));
218 static Lisp_Object string_to_object P_ ((Lisp_Object, Lisp_Object));
221 /* Read a Lisp object from VAL and return it. If VAL is an empty
222 string, and DEFALT is a string, read from DEFALT instead of VAL. */
224 static Lisp_Object
225 string_to_object (val, defalt)
226 Lisp_Object val, defalt;
228 struct gcpro gcpro1, gcpro2;
229 Lisp_Object expr_and_pos;
230 int pos;
232 GCPRO2 (val, defalt);
234 if (STRINGP (val) && XSTRING (val)->size == 0
235 && STRINGP (defalt))
236 val = defalt;
238 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
239 pos = XINT (Fcdr (expr_and_pos));
240 if (pos != XSTRING (val)->size)
242 /* Ignore trailing whitespace; any other trailing junk
243 is an error. */
244 int i;
245 pos = string_char_to_byte (val, pos);
246 for (i = pos; i < STRING_BYTES (XSTRING (val)); i++)
248 int c = XSTRING (val)->data[i];
249 if (c != ' ' && c != '\t' && c != '\n')
250 error ("Trailing garbage following expression");
254 val = Fcar (expr_and_pos);
255 RETURN_UNGCPRO (val);
259 /* Like read_minibuf but reading from stdin. This function is called
260 from read_minibuf to do the job if noninteractive. */
262 static Lisp_Object
263 read_minibuf_noninteractive (map, initial, prompt, backup_n, expflag,
264 histvar, histpos, defalt, allow_props,
265 inherit_input_method)
266 Lisp_Object map;
267 Lisp_Object initial;
268 Lisp_Object prompt;
269 Lisp_Object backup_n;
270 int expflag;
271 Lisp_Object histvar;
272 Lisp_Object histpos;
273 Lisp_Object defalt;
274 int allow_props;
275 int inherit_input_method;
277 int size, len;
278 char *line, *s;
279 Lisp_Object val;
281 fprintf (stdout, "%s", XSTRING (prompt)->data);
282 fflush (stdout);
284 val = Qnil;
285 size = 100;
286 len = 0;
287 line = (char *) xmalloc (size * sizeof *line);
288 while ((s = fgets (line + len, size - len, stdin)) != NULL
289 && (len = strlen (line),
290 len == size - 1 && line[len - 1] != '\n'))
292 size *= 2;
293 line = (char *) xrealloc (line, size);
296 if (s)
298 len = strlen (line);
300 if (len > 0 && line[len - 1] == '\n')
301 line[--len] = '\0';
303 val = build_string (line);
304 xfree (line);
306 else
308 xfree (line);
309 error ("Error reading from stdin");
312 /* If Lisp form desired instead of string, parse it. */
313 if (expflag)
314 val = string_to_object (val, defalt);
316 return val;
320 /* Read from the minibuffer using keymap MAP, initial contents INITIAL
321 (a string), putting point minus BACKUP_N bytes from the end of INITIAL,
322 prompting with PROMPT (a string), using history list HISTVAR
323 with initial position HISTPOS. (BACKUP_N should be <= 0.)
325 Normally return the result as a string (the text that was read),
326 but if EXPFLAG is nonzero, read it and return the object read.
327 If HISTVAR is given, save the value read on that history only if it doesn't
328 match the front of that history list exactly. The value is pushed onto
329 the list as the string that was read.
331 DEFALT specifies te default value for the sake of history commands.
333 If ALLOW_PROPS is nonzero, we do not throw away text properties.
335 if INHERIT_INPUT_METHOD is nonzeor, the minibuffer inherit the
336 current input method. */
338 static Lisp_Object
339 read_minibuf (map, initial, prompt, backup_n, expflag,
340 histvar, histpos, defalt, allow_props, inherit_input_method)
341 Lisp_Object map;
342 Lisp_Object initial;
343 Lisp_Object prompt;
344 Lisp_Object backup_n;
345 int expflag;
346 Lisp_Object histvar;
347 Lisp_Object histpos;
348 Lisp_Object defalt;
349 int allow_props;
350 int inherit_input_method;
352 Lisp_Object val;
353 int count = specpdl_ptr - specpdl;
354 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
355 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
356 Lisp_Object enable_multibyte;
357 extern Lisp_Object Qread_only, Qfront_sticky;
358 extern Lisp_Object Qrear_nonsticky;
360 specbind (Qminibuffer_default, defalt);
362 single_kboard_state ();
363 #ifdef HAVE_X_WINDOWS
364 if (display_busy_cursor_p)
365 cancel_busy_cursor ();
366 #endif
368 val = Qnil;
369 ambient_dir = current_buffer->directory;
370 input_method = Qnil;
371 enable_multibyte = Qnil;
373 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
374 store them away before we can GC. Don't need to protect
375 BACKUP_N because we use the value only if it is an integer. */
376 GCPRO5 (map, initial, val, ambient_dir, input_method);
378 if (!STRINGP (prompt))
379 prompt = build_string ("");
381 if (!enable_recursive_minibuffers
382 && minibuf_level > 0)
384 if (EQ (selected_window, minibuf_window))
385 error ("Command attempted to use minibuffer while in minibuffer");
386 else
387 /* If we're in another window, cancel the minibuffer that's active. */
388 Fthrow (Qexit,
389 build_string ("Command attempted to use minibuffer while in minibuffer"));
392 if (noninteractive)
393 return read_minibuf_noninteractive (map, initial, prompt, backup_n,
394 expflag, histvar, histpos, defalt,
395 allow_props, inherit_input_method);
397 /* Choose the minibuffer window and frame, and take action on them. */
399 choose_minibuf_frame ();
401 record_unwind_protect (choose_minibuf_frame_1, Qnil);
403 record_unwind_protect (Fset_window_configuration,
404 Fcurrent_window_configuration (Qnil));
406 /* If the minibuffer window is on a different frame, save that
407 frame's configuration too. */
408 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
409 if (!EQ (mini_frame, selected_frame))
410 record_unwind_protect (Fset_window_configuration,
411 Fcurrent_window_configuration (mini_frame));
413 /* If the minibuffer is on an iconified or invisible frame,
414 make it visible now. */
415 Fmake_frame_visible (mini_frame);
417 if (minibuffer_auto_raise)
418 Fraise_frame (mini_frame);
420 /* We have to do this after saving the window configuration
421 since that is what restores the current buffer. */
423 /* Arrange to restore a number of minibuffer-related variables.
424 We could bind each variable separately, but that would use lots of
425 specpdl slots. */
426 minibuf_save_list
427 = Fcons (Voverriding_local_map,
428 Fcons (minibuf_window, minibuf_save_list));
429 minibuf_save_list
430 = Fcons (minibuf_prompt,
431 Fcons (make_number (minibuf_prompt_width),
432 Fcons (Vhelp_form,
433 Fcons (Vcurrent_prefix_arg,
434 Fcons (Vminibuffer_history_position,
435 Fcons (Vminibuffer_history_variable,
436 minibuf_save_list))))));
438 record_unwind_protect (read_minibuf_unwind, Qnil);
439 minibuf_level++;
441 /* Now that we can restore all those variables, start changing them. */
443 minibuf_prompt_width = 0;
444 minibuf_prompt = Fcopy_sequence (prompt);
445 Vminibuffer_history_position = histpos;
446 Vminibuffer_history_variable = histvar;
447 Vhelp_form = Vminibuffer_help_form;
449 if (inherit_input_method)
451 /* `current-input-method' is buffer local. So, remeber it in
452 INPUT_METHOD before changing the current buffer. */
453 input_method = Fsymbol_value (Qcurrent_input_method);
454 enable_multibyte = current_buffer->enable_multibyte_characters;
457 /* Switch to the minibuffer. */
459 minibuffer = get_minibuffer (minibuf_level);
460 Fset_buffer (minibuffer);
462 /* The current buffer's default directory is usually the right thing
463 for our minibuffer here. However, if you're typing a command at
464 a minibuffer-only frame when minibuf_level is zero, then buf IS
465 the current_buffer, so reset_buffer leaves buf's default
466 directory unchanged. This is a bummer when you've just started
467 up Emacs and buf's default directory is Qnil. Here's a hack; can
468 you think of something better to do? Find another buffer with a
469 better directory, and use that one instead. */
470 if (STRINGP (ambient_dir))
471 current_buffer->directory = ambient_dir;
472 else
474 Lisp_Object buf_list;
476 for (buf_list = Vbuffer_alist;
477 CONSP (buf_list);
478 buf_list = XCDR (buf_list))
480 Lisp_Object other_buf;
482 other_buf = XCDR (XCAR (buf_list));
483 if (STRINGP (XBUFFER (other_buf)->directory))
485 current_buffer->directory = XBUFFER (other_buf)->directory;
486 break;
491 if (!EQ (mini_frame, selected_frame))
492 Fredirect_frame_focus (selected_frame, mini_frame);
494 Vminibuf_scroll_window = selected_window;
495 Fset_window_buffer (minibuf_window, Fcurrent_buffer ());
496 Fselect_window (minibuf_window);
497 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
499 Fmake_local_variable (Qprint_escape_newlines);
500 print_escape_newlines = 1;
502 /* Erase the buffer. */
504 int count1 = specpdl_ptr - specpdl;
505 specbind (Qinhibit_read_only, Qt);
506 Ferase_buffer ();
507 unbind_to (count1, Qnil);
510 if (!NILP (current_buffer->enable_multibyte_characters)
511 && ! STRING_MULTIBYTE (minibuf_prompt))
512 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
514 /* Insert the prompt, record where it ends. */
515 Finsert (1, &minibuf_prompt);
516 if (PT > BEG)
518 Fput_text_property (make_number (BEG), make_number (PT),
519 Qfront_sticky, Qt, Qnil);
520 Fput_text_property (make_number (BEG), make_number (PT),
521 Qrear_nonsticky, Qt, Qnil);
522 Fput_text_property (make_number (BEG), make_number (PT),
523 Qfield, Qt, Qnil);
524 Fput_text_property (make_number (BEG), make_number (PT),
525 Qread_only, Qt, Qnil);
528 minibuf_prompt_width = current_column ();
530 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
531 if (inherit_input_method)
532 current_buffer->enable_multibyte_characters = enable_multibyte;
534 /* Put in the initial input. */
535 if (!NILP (initial))
537 Finsert (1, &initial);
538 if (INTEGERP (backup_n))
539 Fforward_char (backup_n);
542 clear_message (1, 1);
543 current_buffer->keymap = map;
545 /* Turn on an input method stored in INPUT_METHOD if any. */
546 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
547 call1 (Qactivate_input_method, input_method);
549 /* Run our hook, but not if it is empty.
550 (run-hooks would do nothing if it is empty,
551 but it's important to save time here in the usual case). */
552 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
553 && !NILP (Vrun_hooks))
554 call1 (Vrun_hooks, Qminibuffer_setup_hook);
556 /* Don't allow the user to undo past this point. */
557 current_buffer->undo_list = Qnil;
559 recursive_edit_1 ();
561 /* If cursor is on the minibuffer line,
562 show the user we have exited by putting it in column 0. */
563 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
564 && !noninteractive)
566 XWINDOW (minibuf_window)->cursor.hpos = 0;
567 XWINDOW (minibuf_window)->cursor.x = 0;
568 XWINDOW (minibuf_window)->must_be_updated_p = 1;
569 update_frame (XFRAME (selected_frame), 1, 1);
570 if (rif && rif->flush_display)
571 rif->flush_display (XFRAME (XWINDOW (minibuf_window)->frame));
574 /* Make minibuffer contents into a string. */
575 Fset_buffer (minibuffer);
576 if (allow_props)
577 val = Ffield_string (make_number (ZV));
578 else
579 val = Ffield_string_no_properties (make_number (ZV));
581 /* VAL is the string of minibuffer text. */
583 last_minibuf_string = val;
585 /* Add the value to the appropriate history list unless it is empty. */
586 if (XSTRING (val)->size != 0
587 && SYMBOLP (Vminibuffer_history_variable))
589 /* If the caller wanted to save the value read on a history list,
590 then do so if the value is not already the front of the list. */
591 Lisp_Object histval;
593 /* If variable is unbound, make it nil. */
594 if (EQ (XSYMBOL (Vminibuffer_history_variable)->value, Qunbound))
595 Fset (Vminibuffer_history_variable, Qnil);
597 histval = Fsymbol_value (Vminibuffer_history_variable);
599 /* The value of the history variable must be a cons or nil. Other
600 values are unacceptable. We silently ignore these values. */
601 if (NILP (histval)
602 || (CONSP (histval)
603 && NILP (Fequal (last_minibuf_string, Fcar (histval)))))
605 Lisp_Object length;
607 histval = Fcons (last_minibuf_string, histval);
608 Fset (Vminibuffer_history_variable, histval);
610 /* Truncate if requested. */
611 length = Fget (Vminibuffer_history_variable, Qhistory_length);
612 if (NILP (length)) length = Vhistory_length;
613 if (INTEGERP (length))
615 if (XINT (length) <= 0)
616 Fset (Vminibuffer_history_variable, Qnil);
617 else
619 Lisp_Object temp;
621 temp = Fnthcdr (Fsub1 (length), histval);
622 if (CONSP (temp)) Fsetcdr (temp, Qnil);
628 /* If Lisp form desired instead of string, parse it. */
629 if (expflag)
630 val = string_to_object (val, defalt);
632 /* The appropriate frame will get selected
633 in set-window-configuration. */
634 RETURN_UNGCPRO (unbind_to (count, val));
637 /* Return a buffer to be used as the minibuffer at depth `depth'.
638 depth = 0 is the lowest allowed argument, and that is the value
639 used for nonrecursive minibuffer invocations */
641 Lisp_Object
642 get_minibuffer (depth)
643 int depth;
645 Lisp_Object tail, num, buf;
646 char name[24];
647 extern Lisp_Object nconc2 ();
649 XSETFASTINT (num, depth);
650 tail = Fnthcdr (num, Vminibuffer_list);
651 if (NILP (tail))
653 tail = Fcons (Qnil, Qnil);
654 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
656 buf = Fcar (tail);
657 if (NILP (buf) || NILP (XBUFFER (buf)->name))
659 sprintf (name, " *Minibuf-%d*", depth);
660 buf = Fget_buffer_create (build_string (name));
662 /* Although the buffer's name starts with a space, undo should be
663 enabled in it. */
664 Fbuffer_enable_undo (buf);
666 XCAR (tail) = buf;
668 else
670 int count = specpdl_ptr - specpdl;
672 reset_buffer (XBUFFER (buf));
673 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
674 Fset_buffer (buf);
675 Fkill_all_local_variables ();
676 unbind_to (count, Qnil);
679 return buf;
682 /* This function is called on exiting minibuffer, whether normally or
683 not, and it restores the current window, buffer, etc. */
685 static Lisp_Object
686 read_minibuf_unwind (data)
687 Lisp_Object data;
689 Lisp_Object old_deactivate_mark;
690 Lisp_Object window;
692 /* We are exiting the minibuffer one way or the other,
693 so run the hook. */
694 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
695 && !NILP (Vrun_hooks))
696 safe_run_hooks (Qminibuffer_exit_hook);
698 /* If this was a recursive minibuffer,
699 tie the minibuffer window back to the outer level minibuffer buffer. */
700 minibuf_level--;
702 window = minibuf_window;
703 /* To keep things predictable, in case it matters, let's be in the
704 minibuffer when we reset the relevant variables. */
705 Fset_buffer (XWINDOW (window)->buffer);
707 /* Restore prompt, etc, from outer minibuffer level. */
708 minibuf_prompt = Fcar (minibuf_save_list);
709 minibuf_save_list = Fcdr (minibuf_save_list);
710 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
711 minibuf_save_list = Fcdr (minibuf_save_list);
712 Vhelp_form = Fcar (minibuf_save_list);
713 minibuf_save_list = Fcdr (minibuf_save_list);
714 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
715 minibuf_save_list = Fcdr (minibuf_save_list);
716 Vminibuffer_history_position = Fcar (minibuf_save_list);
717 minibuf_save_list = Fcdr (minibuf_save_list);
718 Vminibuffer_history_variable = Fcar (minibuf_save_list);
719 minibuf_save_list = Fcdr (minibuf_save_list);
720 Voverriding_local_map = Fcar (minibuf_save_list);
721 minibuf_save_list = Fcdr (minibuf_save_list);
722 #if 0
723 temp = Fcar (minibuf_save_list);
724 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
725 minibuf_window = temp;
726 #endif
727 minibuf_save_list = Fcdr (minibuf_save_list);
729 /* Erase the minibuffer we were using at this level. */
731 int count = specpdl_ptr - specpdl;
732 /* Prevent error in erase-buffer. */
733 specbind (Qinhibit_read_only, Qt);
734 old_deactivate_mark = Vdeactivate_mark;
735 Ferase_buffer ();
736 Vdeactivate_mark = old_deactivate_mark;
737 unbind_to (count, Qnil);
740 /* When we get to the outmost level, make sure we resize the
741 mini-window back to its normal size. */
742 if (minibuf_level == 0)
743 resize_mini_window (XWINDOW (window), 0);
745 /* Make sure minibuffer window is erased, not ignored. */
746 windows_or_buffers_changed++;
747 XSETFASTINT (XWINDOW (window)->last_modified, 0);
748 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
749 return Qnil;
753 /* This comment supplies the doc string for read-from-minibuffer,
754 for make-docfile to see. We cannot put this in the real DEFUN
755 due to limits in the Unix cpp.
757 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
758 "Read a string from the minibuffer, prompting with string PROMPT.\n\
759 If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
760 to be inserted into the minibuffer before reading input.\n\
761 If INITIAL-CONTENTS is (STRING . POSITION), the initial input\n\
762 is STRING, but point is placed at position POSITION in the minibuffer.\n\
763 Third arg KEYMAP is a keymap to use whilst reading;\n\
764 if omitted or nil, the default is `minibuffer-local-map'.\n\
765 If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
766 and return that object:\n\
767 in other words, do `(car (read-from-string INPUT-STRING))'\n\
768 Fifth arg HIST, if non-nil, specifies a history list\n\
769 and optionally the initial position in the list.\n\
770 It can be a symbol, which is the history list variable to use,\n\
771 or it can be a cons cell (HISTVAR . HISTPOS).\n\
772 In that case, HISTVAR is the history list variable to use,\n\
773 and HISTPOS is the initial position (the position in the list\n\
774 which INITIAL-CONTENTS corresponds to).\n\
775 Positions are counted starting from 1 at the beginning of the list.\n\
776 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available\n\
777 for history commands; but `read-from-minibuffer' does NOT return DEFAULT-VALUE\n\
778 if the user enters empty input! It returns the empty string.\n\
779 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits\n\
780 the current input method and the setting of enable-multibyte-characters.\n\
781 If the variable `minibuffer-allow-text-properties' is non-nil,\n\
782 then the string which is returned includes whatever text properties\n\
783 were present in the minibuffer. Otherwise the value has no text properties.")
784 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
787 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
788 0 /* See immediately above */)
789 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
790 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
791 Lisp_Object inherit_input_method;
793 int pos = 0;
794 Lisp_Object histvar, histpos, position, val;
795 struct gcpro gcpro1;
797 position = Qnil;
799 CHECK_STRING (prompt, 0);
800 if (!NILP (initial_contents))
802 if (CONSP (initial_contents))
804 position = Fcdr (initial_contents);
805 initial_contents = Fcar (initial_contents);
807 CHECK_STRING (initial_contents, 1);
808 if (!NILP (position))
810 CHECK_NUMBER (position, 0);
811 /* Convert to distance from end of input. */
812 if (XINT (position) < 1)
813 /* A number too small means the beginning of the string. */
814 pos = - XSTRING (initial_contents)->size;
815 else
816 pos = XINT (position) - 1 - XSTRING (initial_contents)->size;
820 if (NILP (keymap))
821 keymap = Vminibuffer_local_map;
822 else
823 keymap = get_keymap (keymap, 1, 0);
825 if (SYMBOLP (hist))
827 histvar = hist;
828 histpos = Qnil;
830 else
832 histvar = Fcar_safe (hist);
833 histpos = Fcdr_safe (hist);
835 if (NILP (histvar))
836 histvar = Qminibuffer_history;
837 if (NILP (histpos))
838 XSETFASTINT (histpos, 0);
840 GCPRO1 (default_value);
841 val = read_minibuf (keymap, initial_contents, prompt,
842 make_number (pos), !NILP (read),
843 histvar, histpos, default_value,
844 minibuffer_allow_text_properties,
845 !NILP (inherit_input_method));
846 UNGCPRO;
847 return val;
850 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
851 "Return a Lisp object read using the minibuffer.\n\
852 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
853 is a string to insert in the minibuffer before reading.")
854 (prompt, initial_contents)
855 Lisp_Object prompt, initial_contents;
857 CHECK_STRING (prompt, 0);
858 if (!NILP (initial_contents))
859 CHECK_STRING (initial_contents, 1);
860 return read_minibuf (Vminibuffer_local_map, initial_contents,
861 prompt, Qnil, 1, Qminibuffer_history,
862 make_number (0), Qnil, 0, 0);
865 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
866 "Return value of Lisp expression read using the minibuffer.\n\
867 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
868 is a string to insert in the minibuffer before reading.")
869 (prompt, initial_contents)
870 Lisp_Object prompt, initial_contents;
872 return Feval (Fread_minibuffer (prompt, initial_contents));
875 /* Functions that use the minibuffer to read various things. */
877 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
878 "Read a string from the minibuffer, prompting with string PROMPT.\n\
879 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.\n\
880 The third arg HISTORY, if non-nil, specifies a history list\n\
881 and optionally the initial position in the list.\n\
882 See `read-from-minibuffer' for details of HISTORY argument.\n\
883 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used\n\
884 for history commands, and as the value to return if the user enters\n\
885 the empty string.\n\
886 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits\n\
887 the current input method and the setting of enable-multibyte-characters.")
888 (prompt, initial_input, history, default_value, inherit_input_method)
889 Lisp_Object prompt, initial_input, history, default_value;
890 Lisp_Object inherit_input_method;
892 Lisp_Object val;
893 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
894 Qnil, history, default_value,
895 inherit_input_method);
896 if (STRINGP (val) && XSTRING (val)->size == 0 && ! NILP (default_value))
897 val = default_value;
898 return val;
901 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
902 "Read a string from the terminal, not allowing blanks.\n\
903 Prompt with PROMPT, and provide INITIAL as an initial value of the input string.\n\
904 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits\n\
905 the current input method and the setting of enable-multibyte-characters.")
906 (prompt, initial, inherit_input_method)
907 Lisp_Object prompt, initial, inherit_input_method;
909 CHECK_STRING (prompt, 0);
910 if (! NILP (initial))
911 CHECK_STRING (initial, 1);
913 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
914 0, Qminibuffer_history, make_number (0), Qnil, 0,
915 !NILP (inherit_input_method));
918 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
919 "Read the name of a command and return as a symbol.\n\
920 Prompts with PROMPT. By default, return DEFAULT-VALUE.")
921 (prompt, default_value)
922 Lisp_Object prompt, default_value;
924 Lisp_Object name, default_string;
926 if (NILP (default_value))
927 default_string = Qnil;
928 else if (SYMBOLP (default_value))
929 XSETSTRING (default_string, XSYMBOL (default_value)->name);
930 else
931 default_string = default_value;
933 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
934 Qnil, Qnil, default_string, Qnil);
935 if (NILP (name))
936 return name;
937 return Fintern (name, Qnil);
940 #ifdef NOTDEF
941 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
942 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
943 Prompts with PROMPT.")
944 (prompt)
945 Lisp_Object prompt;
947 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
948 Qnil);
950 #endif /* NOTDEF */
952 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
953 "Read the name of a user variable and return it as a symbol.\n\
954 Prompts with PROMPT. By default, return DEFAULT-VALUE.\n\
955 A user variable is one whose documentation starts with a `*' character.")
956 (prompt, default_value)
957 Lisp_Object prompt, default_value;
959 Lisp_Object name, default_string;
961 if (NILP (default_value))
962 default_string = Qnil;
963 else if (SYMBOLP (default_value))
964 XSETSTRING (default_string, XSYMBOL (default_value)->name);
965 else
966 default_string = default_value;
968 name = Fcompleting_read (prompt, Vobarray,
969 Quser_variable_p, Qt,
970 Qnil, Qnil, default_string, Qnil);
971 if (NILP (name))
972 return name;
973 return Fintern (name, Qnil);
976 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
977 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
978 Prompts with PROMPT.\n\
979 Optional second arg DEF is value to return if user enters an empty line.\n\
980 If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
981 (prompt, def, require_match)
982 Lisp_Object prompt, def, require_match;
984 Lisp_Object args[4];
986 if (BUFFERP (def))
987 def = XBUFFER (def)->name;
989 if (NILP (Vread_buffer_function))
991 if (!NILP (def))
993 args[0] = build_string ("%s(default %s) ");
994 args[1] = prompt;
995 args[2] = def;
996 prompt = Fformat (3, args);
999 return Fcompleting_read (prompt, Vbuffer_alist, Qnil,
1000 require_match, Qnil, Qbuffer_name_history,
1001 def, Qnil);
1003 else
1005 args[0] = Vread_buffer_function;
1006 args[1] = prompt;
1007 args[2] = def;
1008 args[3] = require_match;
1009 return Ffuncall(4, args);
1013 static Lisp_Object
1014 minibuf_conform_representation (string, basis)
1015 Lisp_Object string, basis;
1017 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1018 return string;
1020 if (STRING_MULTIBYTE (string))
1021 return Fstring_make_unibyte (string);
1022 else
1023 return Fstring_make_multibyte (string);
1026 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1027 "Return common substring of all completions of STRING in ALIST.\n\
1028 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
1029 All that match are compared together; the longest initial sequence\n\
1030 common to all matches is returned as a string.\n\
1031 If there is no match at all, nil is returned.\n\
1032 For a unique match which is exact, t is returned.\n\
1034 ALIST can be an obarray instead of an alist.\n\
1035 Then the print names of all symbols in the obarray are the possible matches.\n\
1037 ALIST can also be a function to do the completion itself.\n\
1038 It receives three arguments: the values STRING, PREDICATE and nil.\n\
1039 Whatever it returns becomes the value of `try-completion'.\n\
1041 If optional third argument PREDICATE is non-nil,\n\
1042 it is used to test each possible match.\n\
1043 The match is a candidate only if PREDICATE returns non-nil.\n\
1044 The argument given to PREDICATE is the alist element\n\
1045 or the symbol from the obarray.\n\
1046 Additionally to this predicate, `completion-regexp-list'\n\
1047 is used to further constrain the set of candidates.")
1048 (string, alist, predicate)
1049 Lisp_Object string, alist, predicate;
1051 Lisp_Object bestmatch, tail, elt, eltstring;
1052 /* Size in bytes of BESTMATCH. */
1053 int bestmatchsize = 0;
1054 /* These are in bytes, too. */
1055 int compare, matchsize;
1056 int list = CONSP (alist) || NILP (alist);
1057 int index = 0, obsize = 0;
1058 int matchcount = 0;
1059 Lisp_Object bucket, zero, end, tem;
1060 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1062 CHECK_STRING (string, 0);
1063 if (!list && !VECTORP (alist))
1064 return call3 (alist, string, predicate, Qnil);
1066 bestmatch = bucket = Qnil;
1068 /* If ALIST is not a list, set TAIL just for gc pro. */
1069 tail = alist;
1070 if (! list)
1072 index = 0;
1073 obsize = XVECTOR (alist)->size;
1074 bucket = XVECTOR (alist)->contents[index];
1077 while (1)
1079 /* Get the next element of the alist or obarray. */
1080 /* Exit the loop if the elements are all used up. */
1081 /* elt gets the alist element or symbol.
1082 eltstring gets the name to check as a completion. */
1084 if (list)
1086 if (NILP (tail))
1087 break;
1088 elt = Fcar (tail);
1089 eltstring = Fcar (elt);
1090 tail = Fcdr (tail);
1092 else
1094 if (XFASTINT (bucket) != 0)
1096 elt = bucket;
1097 eltstring = Fsymbol_name (elt);
1098 if (XSYMBOL (bucket)->next)
1099 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1100 else
1101 XSETFASTINT (bucket, 0);
1103 else if (++index >= obsize)
1104 break;
1105 else
1107 bucket = XVECTOR (alist)->contents[index];
1108 continue;
1112 /* Is this element a possible completion? */
1114 if (STRINGP (eltstring)
1115 && XSTRING (string)->size <= XSTRING (eltstring)->size
1116 && (tem = Fcompare_strings (eltstring, make_number (0),
1117 make_number (XSTRING (string)->size),
1118 string, make_number (0), Qnil,
1119 completion_ignore_case ?Qt : Qnil),
1120 EQ (Qt, tem)))
1122 /* Yes. */
1123 Lisp_Object regexps;
1124 Lisp_Object zero;
1125 XSETFASTINT (zero, 0);
1127 /* Ignore this element if it fails to match all the regexps. */
1128 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1129 regexps = XCDR (regexps))
1131 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1132 if (NILP (tem))
1133 break;
1135 if (CONSP (regexps))
1136 continue;
1138 /* Ignore this element if there is a predicate
1139 and the predicate doesn't like it. */
1141 if (!NILP (predicate))
1143 if (EQ (predicate, Qcommandp))
1144 tem = Fcommandp (elt);
1145 else
1147 GCPRO4 (tail, string, eltstring, bestmatch);
1148 tem = call1 (predicate, elt);
1149 UNGCPRO;
1151 if (NILP (tem)) continue;
1154 /* Update computation of how much all possible completions match */
1156 matchcount++;
1157 if (NILP (bestmatch))
1159 bestmatch = eltstring;
1160 bestmatchsize = XSTRING (eltstring)->size;
1162 else
1164 compare = min (bestmatchsize, XSTRING (eltstring)->size);
1165 tem = Fcompare_strings (bestmatch, make_number (0),
1166 make_number (compare),
1167 eltstring, make_number (0),
1168 make_number (compare),
1169 completion_ignore_case ? Qt : Qnil);
1170 if (EQ (tem, Qt))
1171 matchsize = compare;
1172 else if (XINT (tem) < 0)
1173 matchsize = - XINT (tem) - 1;
1174 else
1175 matchsize = XINT (tem) - 1;
1177 if (matchsize < 0)
1178 matchsize = compare;
1179 if (completion_ignore_case)
1181 /* If this is an exact match except for case,
1182 use it as the best match rather than one that is not an
1183 exact match. This way, we get the case pattern
1184 of the actual match. */
1185 if ((matchsize == XSTRING (eltstring)->size
1186 && matchsize < XSTRING (bestmatch)->size)
1188 /* If there is more than one exact match ignoring case,
1189 and one of them is exact including case,
1190 prefer that one. */
1191 /* If there is no exact match ignoring case,
1192 prefer a match that does not change the case
1193 of the input. */
1194 ((matchsize == XSTRING (eltstring)->size)
1196 (matchsize == XSTRING (bestmatch)->size)
1197 && (tem = Fcompare_strings (eltstring, make_number (0),
1198 make_number (XSTRING (string)->size),
1199 string, make_number (0),
1200 Qnil,
1201 Qnil),
1202 EQ (Qt, tem))
1203 && (tem = Fcompare_strings (bestmatch, make_number (0),
1204 make_number (XSTRING (string)->size),
1205 string, make_number (0),
1206 Qnil,
1207 Qnil),
1208 ! EQ (Qt, tem))))
1209 bestmatch = eltstring;
1211 bestmatchsize = matchsize;
1216 if (NILP (bestmatch))
1217 return Qnil; /* No completions found */
1218 /* If we are ignoring case, and there is no exact match,
1219 and no additional text was supplied,
1220 don't change the case of what the user typed. */
1221 if (completion_ignore_case && bestmatchsize == XSTRING (string)->size
1222 && XSTRING (bestmatch)->size > bestmatchsize)
1223 return minibuf_conform_representation (string, bestmatch);
1225 /* Return t if the supplied string is an exact match (counting case);
1226 it does not require any change to be made. */
1227 if (matchcount == 1 && bestmatchsize == XSTRING (string)->size
1228 && (tem = Fcompare_strings (bestmatch, make_number (0),
1229 make_number (bestmatchsize),
1230 string, make_number (0),
1231 make_number (bestmatchsize),
1232 Qnil),
1233 EQ (Qt, tem)))
1234 return Qt;
1236 XSETFASTINT (zero, 0); /* Else extract the part in which */
1237 XSETFASTINT (end, bestmatchsize); /* all completions agree */
1238 return Fsubstring (bestmatch, zero, end);
1241 /* Compare exactly LEN chars of strings at S1 and S2,
1242 ignoring case if appropriate.
1243 Return -1 if strings match,
1244 else number of chars that match at the beginning. */
1247 scmp (s1, s2, len)
1248 register unsigned char *s1, *s2;
1249 int len;
1251 register int l = len;
1253 if (completion_ignore_case)
1255 while (l && DOWNCASE (*s1++) == DOWNCASE (*s2++))
1256 l--;
1258 else
1260 while (l && *s1++ == *s2++)
1261 l--;
1263 if (l == 0)
1264 return -1;
1265 else
1267 int match = len - l;
1269 /* Now *--S1 is the unmatching byte. If it is in the middle of
1270 multi-byte form, we must say that the multi-byte character
1271 there doesn't match. */
1272 while (match && *--s1 >= 0xA0) match--;
1273 return match;
1277 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1278 "Search for partial matches to STRING in ALIST.\n\
1279 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
1280 The value is a list of all the strings from ALIST that match.\n\
1282 ALIST can be an obarray instead of an alist.\n\
1283 Then the print names of all symbols in the obarray are the possible matches.\n\
1285 ALIST can also be a function to do the completion itself.\n\
1286 It receives three arguments: the values STRING, PREDICATE and t.\n\
1287 Whatever it returns becomes the value of `all-completions'.\n\
1289 If optional third argument PREDICATE is non-nil,\n\
1290 it is used to test each possible match.\n\
1291 The match is a candidate only if PREDICATE returns non-nil.\n\
1292 The argument given to PREDICATE is the alist element\n\
1293 or the symbol from the obarray.\n\
1294 Additionally to this predicate, `completion-regexp-list'\n\
1295 is used to further constrain the set of candidates.\n\
1297 If the optional fourth argument HIDE-SPACES is non-nil,\n\
1298 strings in ALIST that start with a space\n\
1299 are ignored unless STRING itself starts with a space.")
1300 (string, alist, predicate, hide_spaces)
1301 Lisp_Object string, alist, predicate, hide_spaces;
1303 Lisp_Object tail, elt, eltstring;
1304 Lisp_Object allmatches;
1305 int list = CONSP (alist) || NILP (alist);
1306 int index = 0, obsize = 0;
1307 Lisp_Object bucket, tem;
1308 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1310 CHECK_STRING (string, 0);
1311 if (!list && !VECTORP (alist))
1313 return call3 (alist, string, predicate, Qt);
1315 allmatches = bucket = Qnil;
1317 /* If ALIST is not a list, set TAIL just for gc pro. */
1318 tail = alist;
1319 if (! list)
1321 index = 0;
1322 obsize = XVECTOR (alist)->size;
1323 bucket = XVECTOR (alist)->contents[index];
1326 while (1)
1328 /* Get the next element of the alist or obarray. */
1329 /* Exit the loop if the elements are all used up. */
1330 /* elt gets the alist element or symbol.
1331 eltstring gets the name to check as a completion. */
1333 if (list)
1335 if (NILP (tail))
1336 break;
1337 elt = Fcar (tail);
1338 eltstring = Fcar (elt);
1339 tail = Fcdr (tail);
1341 else
1343 if (XFASTINT (bucket) != 0)
1345 elt = bucket;
1346 eltstring = Fsymbol_name (elt);
1347 if (XSYMBOL (bucket)->next)
1348 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1349 else
1350 XSETFASTINT (bucket, 0);
1352 else if (++index >= obsize)
1353 break;
1354 else
1356 bucket = XVECTOR (alist)->contents[index];
1357 continue;
1361 /* Is this element a possible completion? */
1363 if (STRINGP (eltstring)
1364 && XSTRING (string)->size <= XSTRING (eltstring)->size
1365 /* If HIDE_SPACES, reject alternatives that start with space
1366 unless the input starts with space. */
1367 && ((STRING_BYTES (XSTRING (string)) > 0
1368 && XSTRING (string)->data[0] == ' ')
1369 || XSTRING (eltstring)->data[0] != ' '
1370 || NILP (hide_spaces))
1371 && (tem = Fcompare_strings (eltstring, make_number (0),
1372 make_number (XSTRING (string)->size),
1373 string, make_number (0),
1374 make_number (XSTRING (string)->size),
1375 completion_ignore_case ? Qt : Qnil),
1376 EQ (Qt, tem)))
1378 /* Yes. */
1379 Lisp_Object regexps;
1380 Lisp_Object zero;
1381 XSETFASTINT (zero, 0);
1383 /* Ignore this element if it fails to match all the regexps. */
1384 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1385 regexps = XCDR (regexps))
1387 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1388 if (NILP (tem))
1389 break;
1391 if (CONSP (regexps))
1392 continue;
1394 /* Ignore this element if there is a predicate
1395 and the predicate doesn't like it. */
1397 if (!NILP (predicate))
1399 if (EQ (predicate, Qcommandp))
1400 tem = Fcommandp (elt);
1401 else
1403 GCPRO4 (tail, eltstring, allmatches, string);
1404 tem = call1 (predicate, elt);
1405 UNGCPRO;
1407 if (NILP (tem)) continue;
1409 /* Ok => put it on the list. */
1410 allmatches = Fcons (eltstring, allmatches);
1414 return Fnreverse (allmatches);
1417 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1418 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1419 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
1420 Lisp_Object Vminibuffer_completing_file_name;
1422 /* This comment supplies the doc string for completing-read,
1423 for make-docfile to see. We cannot put this in the real DEFUN
1424 due to limits in the Unix cpp.
1426 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1427 "Read a string in the minibuffer, with completion.\n\
1428 PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
1429 TABLE is an alist whose elements' cars are strings, or an obarray.\n\
1430 TABLE can also be a function to do the completion itself.\n\
1431 PREDICATE limits completion to a subset of TABLE.\n\
1432 See `try-completion' and `all-completions' for more details\n\
1433 on completion, TABLE, and PREDICATE.\n\
1435 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
1436 the input is (or completes to) an element of TABLE or is null.\n\
1437 If it is also not t, Return does not exit if it does non-null completion.\n\
1438 If the input is null, `completing-read' returns an empty string,\n\
1439 regardless of the value of REQUIRE-MATCH.\n\
1441 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
1442 If it is (STRING . POSITION), the initial input\n\
1443 is STRING, but point is placed POSITION characters into the string.\n\
1444 HIST, if non-nil, specifies a history list\n\
1445 and optionally the initial position in the list.\n\
1446 It can be a symbol, which is the history list variable to use,\n\
1447 or it can be a cons cell (HISTVAR . HISTPOS).\n\
1448 In that case, HISTVAR is the history list variable to use,\n\
1449 and HISTPOS is the initial position (the position in the list\n\
1450 which INITIAL-INPUT corresponds to).\n\
1451 Positions are counted starting from 1 at the beginning of the list.\n\
1452 DEF, if non-nil, is the default value.\n\
1454 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits\n\
1455 the current input method and the setting of enable-multibyte-characters.\n\
1457 Completion ignores case if the ambient value of\n\
1458 `completion-ignore-case' is non-nil."
1460 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1461 0 /* See immediately above */)
1462 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method)
1463 Lisp_Object prompt, table, predicate, require_match, initial_input;
1464 Lisp_Object hist, def, inherit_input_method;
1466 Lisp_Object val, histvar, histpos, position;
1467 Lisp_Object init;
1468 int pos = 0;
1469 int count = specpdl_ptr - specpdl;
1470 struct gcpro gcpro1;
1472 init = initial_input;
1473 GCPRO1 (def);
1475 specbind (Qminibuffer_completion_table, table);
1476 specbind (Qminibuffer_completion_predicate, predicate);
1477 specbind (Qminibuffer_completion_confirm,
1478 EQ (require_match, Qt) ? Qnil : Qt);
1479 last_exact_completion = Qnil;
1481 position = Qnil;
1482 if (!NILP (init))
1484 if (CONSP (init))
1486 position = Fcdr (init);
1487 init = Fcar (init);
1489 CHECK_STRING (init, 0);
1490 if (!NILP (position))
1492 CHECK_NUMBER (position, 0);
1493 /* Convert to distance from end of input. */
1494 pos = XINT (position) - XSTRING (init)->size;
1498 if (SYMBOLP (hist))
1500 histvar = hist;
1501 histpos = Qnil;
1503 else
1505 histvar = Fcar_safe (hist);
1506 histpos = Fcdr_safe (hist);
1508 if (NILP (histvar))
1509 histvar = Qminibuffer_history;
1510 if (NILP (histpos))
1511 XSETFASTINT (histpos, 0);
1513 val = read_minibuf (NILP (require_match)
1514 ? Vminibuffer_local_completion_map
1515 : Vminibuffer_local_must_match_map,
1516 init, prompt, make_number (pos), 0,
1517 histvar, histpos, def, 0,
1518 !NILP (inherit_input_method));
1520 if (STRINGP (val) && XSTRING (val)->size == 0 && ! NILP (def))
1521 val = def;
1523 RETURN_UNGCPRO (unbind_to (count, val));
1526 Lisp_Object Fminibuffer_completion_help ();
1527 Lisp_Object assoc_for_completion ();
1529 /* Test whether TXT is an exact completion. */
1530 Lisp_Object
1531 test_completion (txt)
1532 Lisp_Object txt;
1534 Lisp_Object tem;
1536 if (CONSP (Vminibuffer_completion_table)
1537 || NILP (Vminibuffer_completion_table))
1538 return assoc_for_completion (txt, Vminibuffer_completion_table);
1539 else if (VECTORP (Vminibuffer_completion_table))
1541 /* Bypass intern-soft as that loses for nil */
1542 tem = oblookup (Vminibuffer_completion_table,
1543 XSTRING (txt)->data,
1544 XSTRING (txt)->size,
1545 STRING_BYTES (XSTRING (txt)));
1546 if (!SYMBOLP (tem))
1548 if (STRING_MULTIBYTE (txt))
1549 txt = Fstring_make_unibyte (txt);
1550 else
1551 txt = Fstring_make_multibyte (txt);
1553 tem = oblookup (Vminibuffer_completion_table,
1554 XSTRING (txt)->data,
1555 XSTRING (txt)->size,
1556 STRING_BYTES (XSTRING (txt)));
1557 if (!SYMBOLP (tem))
1558 return Qnil;
1560 if (!NILP (Vminibuffer_completion_predicate))
1561 return call1 (Vminibuffer_completion_predicate, tem);
1562 else
1563 return Qt;
1565 else
1566 return call3 (Vminibuffer_completion_table, txt,
1567 Vminibuffer_completion_predicate, Qlambda);
1570 /* returns:
1571 * 0 no possible completion
1572 * 1 was already an exact and unique completion
1573 * 3 was already an exact completion
1574 * 4 completed to an exact completion
1575 * 5 some completion happened
1576 * 6 no completion happened
1579 do_completion ()
1581 Lisp_Object completion, string, tem;
1582 int completedp;
1583 Lisp_Object last;
1584 struct gcpro gcpro1, gcpro2;
1586 completion = Ftry_completion (Ffield_string (make_number (ZV)),
1587 Vminibuffer_completion_table,
1588 Vminibuffer_completion_predicate);
1589 last = last_exact_completion;
1590 last_exact_completion = Qnil;
1592 GCPRO2 (completion, last);
1594 if (NILP (completion))
1596 bitch_at_user ();
1597 temp_echo_area_glyphs (" [No match]");
1598 UNGCPRO;
1599 return 0;
1602 if (EQ (completion, Qt)) /* exact and unique match */
1604 UNGCPRO;
1605 return 1;
1608 string = Ffield_string (make_number (ZV));
1610 /* COMPLETEDP should be true if some completion was done, which
1611 doesn't include simply changing the case of the entered string.
1612 However, for appearance, the string is rewritten if the case
1613 changes. */
1614 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qt);
1615 completedp = !EQ (tem, Qt);
1617 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qnil);
1618 if (!EQ (tem, Qt))
1619 /* Rewrite the user's input. */
1621 Fdelete_field (make_number (ZV)); /* Some completion happened */
1622 Finsert (1, &completion);
1624 if (! completedp)
1625 /* The case of the string changed, but that's all. We're not
1626 sure whether this is a unique completion or not, so try again
1627 using the real case (this shouldn't recurse again, because
1628 the next time try-completion will return either `t' or the
1629 exact string). */
1631 UNGCPRO;
1632 return do_completion ();
1636 /* It did find a match. Do we match some possibility exactly now? */
1637 tem = test_completion (Ffield_string (make_number (ZV)));
1638 if (NILP (tem))
1640 /* not an exact match */
1641 UNGCPRO;
1642 if (completedp)
1643 return 5;
1644 else if (!NILP (Vcompletion_auto_help))
1645 Fminibuffer_completion_help ();
1646 else
1647 temp_echo_area_glyphs (" [Next char not unique]");
1648 return 6;
1650 else if (completedp)
1652 UNGCPRO;
1653 return 4;
1655 /* If the last exact completion and this one were the same,
1656 it means we've already given a "Complete but not unique"
1657 message and the user's hit TAB again, so now we give him help. */
1658 last_exact_completion = completion;
1659 if (!NILP (last))
1661 tem = Ffield_string (make_number (ZV));
1662 if (!NILP (Fequal (tem, last)))
1663 Fminibuffer_completion_help ();
1665 UNGCPRO;
1666 return 3;
1669 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1671 Lisp_Object
1672 assoc_for_completion (key, list)
1673 register Lisp_Object key;
1674 Lisp_Object list;
1676 register Lisp_Object tail;
1678 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1680 register Lisp_Object elt, tem, thiscar;
1681 elt = Fcar (tail);
1682 if (!CONSP (elt)) continue;
1683 thiscar = Fcar (elt);
1684 if (!STRINGP (thiscar))
1685 continue;
1686 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1687 key, make_number (0), Qnil,
1688 completion_ignore_case ? Qt : Qnil);
1689 if (EQ (tem, Qt))
1690 return elt;
1691 QUIT;
1693 return Qnil;
1696 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1697 "Complete the minibuffer contents as far as possible.\n\
1698 Return nil if there is no valid completion, else t.\n\
1699 If no characters can be completed, display a list of possible completions.\n\
1700 If you repeat this command after it displayed such a list,\n\
1701 scroll the window of possible completions.")
1704 register int i;
1705 Lisp_Object window, tem;
1707 /* If the previous command was not this,
1708 mark the completion buffer obsolete. */
1709 if (! EQ (current_kboard->Vlast_command, Vthis_command))
1710 Vminibuf_scroll_window = Qnil;
1712 window = Vminibuf_scroll_window;
1713 /* If there's a fresh completion window with a live buffer,
1714 and this command is repeated, scroll that window. */
1715 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1716 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1718 struct buffer *obuf = current_buffer;
1720 Fset_buffer (XWINDOW (window)->buffer);
1721 tem = Fpos_visible_in_window_p (make_number (ZV), window, Qnil);
1722 if (! NILP (tem))
1723 /* If end is in view, scroll up to the beginning. */
1724 Fset_window_start (window, make_number (BEGV), Qnil);
1725 else
1726 /* Else scroll down one screen. */
1727 Fscroll_other_window (Qnil);
1729 set_buffer_internal (obuf);
1730 return Qnil;
1733 i = do_completion ();
1734 switch (i)
1736 case 0:
1737 return Qnil;
1739 case 1:
1740 if (PT != ZV)
1741 Fgoto_char (make_number (ZV));
1742 temp_echo_area_glyphs (" [Sole completion]");
1743 break;
1745 case 3:
1746 if (PT != ZV)
1747 Fgoto_char (make_number (ZV));
1748 temp_echo_area_glyphs (" [Complete, but not unique]");
1749 break;
1752 return Qt;
1755 /* Subroutines of Fminibuffer_complete_and_exit. */
1757 /* This one is called by internal_condition_case to do the real work. */
1759 Lisp_Object
1760 complete_and_exit_1 ()
1762 return make_number (do_completion ());
1765 /* This one is called by internal_condition_case if an error happens.
1766 Pretend the current value is an exact match. */
1768 Lisp_Object
1769 complete_and_exit_2 (ignore)
1770 Lisp_Object ignore;
1772 return make_number (1);
1775 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1776 Sminibuffer_complete_and_exit, 0, 0, "",
1777 "If the minibuffer contents is a valid completion then exit.\n\
1778 Otherwise try to complete it. If completion leads to a valid completion,\n\
1779 a repetition of this command will exit.")
1782 register int i;
1783 Lisp_Object val;
1785 /* Allow user to specify null string */
1786 if (XINT (Ffield_beginning (make_number (ZV), Qnil)) == ZV)
1787 goto exit;
1789 if (!NILP (test_completion (Ffield_string (make_number (ZV)))))
1790 goto exit;
1792 /* Call do_completion, but ignore errors. */
1793 val = internal_condition_case (complete_and_exit_1, Qerror,
1794 complete_and_exit_2);
1796 i = XFASTINT (val);
1797 switch (i)
1799 case 1:
1800 case 3:
1801 goto exit;
1803 case 4:
1804 if (!NILP (Vminibuffer_completion_confirm))
1806 temp_echo_area_glyphs (" [Confirm]");
1807 return Qnil;
1809 else
1810 goto exit;
1812 default:
1813 return Qnil;
1815 exit:
1816 return Fthrow (Qexit, Qnil);
1817 /* NOTREACHED */
1820 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1821 0, 0, "",
1822 "Complete the minibuffer contents at most a single word.\n\
1823 After one word is completed as much as possible, a space or hyphen\n\
1824 is added, provided that matches some possible completion.\n\
1825 Return nil if there is no valid completion, else t.")
1828 Lisp_Object completion, tem, tem1;
1829 register int i, i_byte;
1830 register unsigned char *completion_string;
1831 struct gcpro gcpro1, gcpro2;
1832 int prompt_end_charpos;
1834 /* We keep calling Fbuffer_string rather than arrange for GC to
1835 hold onto a pointer to one of the strings thus made. */
1837 completion = Ftry_completion (Ffield_string (make_number (ZV)),
1838 Vminibuffer_completion_table,
1839 Vminibuffer_completion_predicate);
1840 if (NILP (completion))
1842 bitch_at_user ();
1843 temp_echo_area_glyphs (" [No match]");
1844 return Qnil;
1846 if (EQ (completion, Qt))
1847 return Qnil;
1849 #if 0 /* How the below code used to look, for reference. */
1850 tem = Ffield_string (make_number (ZV));
1851 b = XSTRING (tem)->data;
1852 i = ZV - 1 - XSTRING (completion)->size;
1853 p = XSTRING (completion)->data;
1854 if (i > 0 ||
1855 0 <= scmp (b, p, ZV - 1))
1857 i = 1;
1858 /* Set buffer to longest match of buffer tail and completion head. */
1859 while (0 <= scmp (b + i, p, ZV - 1 - i))
1860 i++;
1861 del_range (1, i + 1);
1862 SET_PT (ZV);
1864 #else /* Rewritten code */
1866 int buffer_nchars, completion_nchars;
1868 CHECK_STRING (completion, 0);
1869 tem = Ffield_string (make_number (ZV));
1870 GCPRO2 (completion, tem);
1871 /* If reading a file name,
1872 expand any $ENVVAR refs in the buffer and in TEM. */
1873 if (! NILP (Vminibuffer_completing_file_name))
1875 Lisp_Object substituted;
1876 substituted = Fsubstitute_in_file_name (tem);
1877 if (! EQ (substituted, tem))
1879 tem = substituted;
1880 Fdelete_field (make_number (ZV));
1881 insert_from_string (tem, 0, 0, XSTRING (tem)->size,
1882 STRING_BYTES (XSTRING (tem)), 0);
1885 buffer_nchars = XSTRING (tem)->size; /* ie ZV - BEGV */
1886 completion_nchars = XSTRING (completion)->size;
1887 i = buffer_nchars - completion_nchars;
1888 if (i > 0
1890 (tem1 = Fcompare_strings (tem, make_number (0),
1891 make_number (buffer_nchars),
1892 completion, make_number (0),
1893 make_number (buffer_nchars),
1894 completion_ignore_case ? Qt : Qnil),
1895 ! EQ (tem1, Qt)))
1897 int start_pos;
1899 /* Set buffer to longest match of buffer tail and completion head. */
1900 if (i <= 0) i = 1;
1901 start_pos= i;
1902 buffer_nchars -= i;
1903 while (i > 0)
1905 tem1 = Fcompare_strings (tem, make_number (start_pos), Qnil,
1906 completion, make_number (0),
1907 make_number (buffer_nchars),
1908 completion_ignore_case ? Qt : Qnil);
1909 start_pos++;
1910 if (EQ (tem1, Qt))
1911 break;
1912 i++;
1913 buffer_nchars--;
1915 del_range (1, i + 1);
1916 SET_PT_BOTH (ZV, ZV_BYTE);
1918 UNGCPRO;
1920 #endif /* Rewritten code */
1922 prompt_end_charpos = XINT (Ffield_beginning (make_number (ZV), Qnil));
1925 int prompt_end_bytepos;
1926 prompt_end_bytepos = CHAR_TO_BYTE (prompt_end_charpos);
1927 i = ZV - prompt_end_charpos;
1928 i_byte = ZV_BYTE - prompt_end_bytepos;
1931 /* If completion finds next char not unique,
1932 consider adding a space or a hyphen. */
1933 if (i == XSTRING (completion)->size)
1935 GCPRO1 (completion);
1936 tem = Ftry_completion (concat2 (Ffield_string (make_number (ZV)), build_string (" ")),
1937 Vminibuffer_completion_table,
1938 Vminibuffer_completion_predicate);
1939 UNGCPRO;
1941 if (STRINGP (tem))
1942 completion = tem;
1943 else
1945 GCPRO1 (completion);
1946 tem =
1947 Ftry_completion (concat2 (Ffield_string (make_number (ZV)), build_string ("-")),
1948 Vminibuffer_completion_table,
1949 Vminibuffer_completion_predicate);
1950 UNGCPRO;
1952 if (STRINGP (tem))
1953 completion = tem;
1957 /* Now find first word-break in the stuff found by completion.
1958 i gets index in string of where to stop completing. */
1960 int len, c;
1961 int bytes = STRING_BYTES (XSTRING (completion));
1962 completion_string = XSTRING (completion)->data;
1963 for (; i_byte < STRING_BYTES (XSTRING (completion)); i_byte += len, i++)
1965 c = STRING_CHAR_AND_LENGTH (completion_string + i_byte,
1966 bytes - i_byte,
1967 len);
1968 if (SYNTAX (c) != Sword)
1970 i_byte += len;
1971 i++;
1972 break;
1977 /* If got no characters, print help for user. */
1979 if (i == ZV - prompt_end_charpos)
1981 if (!NILP (Vcompletion_auto_help))
1982 Fminibuffer_completion_help ();
1983 return Qnil;
1986 /* Otherwise insert in minibuffer the chars we got */
1988 Fdelete_field (make_number (ZV));
1989 insert_from_string (completion, 0, 0, i, i_byte, 1);
1990 return Qt;
1993 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
1994 1, 1, 0,
1995 "Display the list of completions, COMPLETIONS, using `standard-output'.\n\
1996 Each element may be just a symbol or string\n\
1997 or may be a list of two strings to be printed as if concatenated.\n\
1998 `standard-output' must be a buffer.\n\
1999 The actual completion alternatives, as inserted, are given `mouse-face'\n\
2000 properties of `highlight'.\n\
2001 At the end, this runs the normal hook `completion-setup-hook'.\n\
2002 It can find the completion buffer in `standard-output'.")
2003 (completions)
2004 Lisp_Object completions;
2006 Lisp_Object tail, elt;
2007 register int i;
2008 int column = 0;
2009 struct gcpro gcpro1, gcpro2;
2010 struct buffer *old = current_buffer;
2011 int first = 1;
2013 /* Note that (when it matters) every variable
2014 points to a non-string that is pointed to by COMPLETIONS,
2015 except for ELT. ELT can be pointing to a string
2016 when terpri or Findent_to calls a change hook. */
2017 elt = Qnil;
2018 GCPRO2 (completions, elt);
2020 if (BUFFERP (Vstandard_output))
2021 set_buffer_internal (XBUFFER (Vstandard_output));
2023 if (NILP (completions))
2024 write_string ("There are no possible completions of what you have typed.",
2025 -1);
2026 else
2028 write_string ("Possible completions are:", -1);
2029 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
2031 Lisp_Object tem, string;
2032 int length;
2033 Lisp_Object startpos, endpos;
2035 startpos = Qnil;
2037 elt = Fcar (tail);
2038 /* Compute the length of this element. */
2039 if (CONSP (elt))
2041 tem = XCAR (elt);
2042 CHECK_STRING (tem, 0);
2043 length = XSTRING (tem)->size;
2045 tem = Fcar (XCDR (elt));
2046 CHECK_STRING (tem, 0);
2047 length += XSTRING (tem)->size;
2049 else
2051 CHECK_STRING (elt, 0);
2052 length = XSTRING (elt)->size;
2055 /* This does a bad job for narrower than usual windows.
2056 Sadly, the window it will appear in is not known
2057 until after the text has been made. */
2059 if (BUFFERP (Vstandard_output))
2060 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2062 /* If the previous completion was very wide,
2063 or we have two on this line already,
2064 don't put another on the same line. */
2065 if (column > 33 || first
2066 /* If this is really wide, don't put it second on a line. */
2067 || (column > 0 && length > 45))
2069 Fterpri (Qnil);
2070 column = 0;
2072 /* Otherwise advance to column 35. */
2073 else
2075 if (BUFFERP (Vstandard_output))
2077 tem = Findent_to (make_number (35), make_number (2));
2079 column = XINT (tem);
2081 else
2085 write_string (" ", -1);
2086 column++;
2088 while (column < 35);
2092 if (BUFFERP (Vstandard_output))
2094 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2095 Fset_text_properties (startpos, endpos,
2096 Qnil, Vstandard_output);
2099 /* Output this element.
2100 If necessary, convert it to unibyte or to multibyte first. */
2101 if (CONSP (elt))
2102 string = Fcar (elt);
2103 else
2104 string = elt;
2105 if (NILP (current_buffer->enable_multibyte_characters)
2106 && STRING_MULTIBYTE (string))
2107 string = Fstring_make_unibyte (string);
2108 else if (!NILP (current_buffer->enable_multibyte_characters)
2109 && !STRING_MULTIBYTE (string))
2110 string = Fstring_make_multibyte (string);
2112 if (BUFFERP (Vstandard_output))
2114 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2116 Fprinc (string, Qnil);
2118 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2120 Fput_text_property (startpos, endpos,
2121 Qmouse_face, intern ("highlight"),
2122 Vstandard_output);
2124 else
2126 Fprinc (string, Qnil);
2129 /* Output the annotation for this element. */
2130 if (CONSP (elt))
2132 if (BUFFERP (Vstandard_output))
2134 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2136 Fprinc (Fcar (Fcdr (elt)), Qnil);
2138 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2140 Fset_text_properties (startpos, endpos, Qnil,
2141 Vstandard_output);
2143 else
2145 Fprinc (Fcar (Fcdr (elt)), Qnil);
2150 /* Update COLUMN for what we have output. */
2151 column += length;
2153 /* If output is to a buffer, recompute COLUMN in a way
2154 that takes account of character widths. */
2155 if (BUFFERP (Vstandard_output))
2157 tem = Fcurrent_column ();
2158 column = XINT (tem);
2161 first = 0;
2165 UNGCPRO;
2167 if (BUFFERP (Vstandard_output))
2168 set_buffer_internal (old);
2170 if (!NILP (Vrun_hooks))
2171 call1 (Vrun_hooks, intern ("completion-setup-hook"));
2173 return Qnil;
2176 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
2177 0, 0, "",
2178 "Display a list of possible completions of the current minibuffer contents.")
2181 Lisp_Object completions;
2183 message ("Making completion list...");
2184 completions = Fall_completions (Ffield_string (make_number (ZV)),
2185 Vminibuffer_completion_table,
2186 Vminibuffer_completion_predicate,
2187 Qt);
2188 clear_message (1, 0);
2190 if (NILP (completions))
2192 bitch_at_user ();
2193 temp_echo_area_glyphs (" [No completions]");
2195 else
2196 internal_with_output_to_temp_buffer ("*Completions*",
2197 Fdisplay_completion_list,
2198 Fsort (completions, Qstring_lessp));
2199 return Qnil;
2202 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
2203 "Terminate minibuffer input.")
2206 if (INTEGERP (last_command_char))
2207 internal_self_insert (XINT (last_command_char), 0);
2208 else
2209 bitch_at_user ();
2211 return Fthrow (Qexit, Qnil);
2214 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
2215 "Terminate this minibuffer argument.")
2218 return Fthrow (Qexit, Qnil);
2221 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
2222 "Return current depth of activations of minibuffer, a nonnegative integer.")
2225 return make_number (minibuf_level);
2228 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
2229 "Return the prompt string of the currently-active minibuffer.\n\
2230 If no minibuffer is active, return nil.")
2233 return Fcopy_sequence (minibuf_prompt);
2237 /* Temporarily display the string M at the end of the current
2238 minibuffer contents. This is used to display things like
2239 "[No Match]" when the user requests a completion for a prefix
2240 that has no possible completions, and other quick, unobtrusive
2241 messages. */
2243 void
2244 temp_echo_area_glyphs (m)
2245 char *m;
2247 int osize = ZV;
2248 int osize_byte = ZV_BYTE;
2249 int opoint = PT;
2250 int opoint_byte = PT_BYTE;
2251 Lisp_Object oinhibit;
2252 oinhibit = Vinhibit_quit;
2254 /* Clear out any old echo-area message to make way for our new thing. */
2255 message (0);
2257 SET_PT_BOTH (osize, osize_byte);
2258 insert_string (m);
2259 SET_PT_BOTH (opoint, opoint_byte);
2260 Vinhibit_quit = Qt;
2261 Fsit_for (make_number (2), Qnil, Qnil);
2262 del_range_both (osize, osize_byte, ZV, ZV_BYTE, 1);
2263 SET_PT_BOTH (opoint, opoint_byte);
2264 if (!NILP (Vquit_flag))
2266 Vquit_flag = Qnil;
2267 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
2269 Vinhibit_quit = oinhibit;
2272 DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
2273 1, 1, 0,
2274 "Temporarily display STRING at the end of the minibuffer.\n\
2275 The text is displayed for two seconds,\n\
2276 or until the next input event arrives, whichever comes first.")
2277 (string)
2278 Lisp_Object string;
2280 temp_echo_area_glyphs (XSTRING (string)->data);
2281 return Qnil;
2284 void
2285 init_minibuf_once ()
2287 Vminibuffer_list = Qnil;
2288 staticpro (&Vminibuffer_list);
2291 void
2292 syms_of_minibuf ()
2294 minibuf_level = 0;
2295 minibuf_prompt = Qnil;
2296 staticpro (&minibuf_prompt);
2298 minibuf_save_list = Qnil;
2299 staticpro (&minibuf_save_list);
2301 Qread_file_name_internal = intern ("read-file-name-internal");
2302 staticpro (&Qread_file_name_internal);
2304 Qminibuffer_default = intern ("minibuffer-default");
2305 staticpro (&Qminibuffer_default);
2306 Fset (Qminibuffer_default, Qnil);
2308 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
2309 staticpro (&Qminibuffer_completion_table);
2311 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
2312 staticpro (&Qminibuffer_completion_confirm);
2314 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
2315 staticpro (&Qminibuffer_completion_predicate);
2317 staticpro (&last_exact_completion);
2318 last_exact_completion = Qnil;
2320 staticpro (&last_minibuf_string);
2321 last_minibuf_string = Qnil;
2323 Quser_variable_p = intern ("user-variable-p");
2324 staticpro (&Quser_variable_p);
2326 Qminibuffer_history = intern ("minibuffer-history");
2327 staticpro (&Qminibuffer_history);
2329 Qbuffer_name_history = intern ("buffer-name-history");
2330 staticpro (&Qbuffer_name_history);
2331 Fset (Qbuffer_name_history, Qnil);
2333 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
2334 staticpro (&Qminibuffer_setup_hook);
2336 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
2337 staticpro (&Qminibuffer_exit_hook);
2339 Qhistory_length = intern ("history-length");
2340 staticpro (&Qhistory_length);
2342 Qcurrent_input_method = intern ("current-input-method");
2343 staticpro (&Qcurrent_input_method);
2345 Qactivate_input_method = intern ("activate-input-method");
2346 staticpro (&Qactivate_input_method);
2348 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2349 "If this is non-nil, `read-buffer' does its work by calling this function.");
2350 Vread_buffer_function = Qnil;
2352 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2353 "Normal hook run just after entry to minibuffer.");
2354 Vminibuffer_setup_hook = Qnil;
2356 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2357 "Normal hook run just after exit from minibuffer.");
2358 Vminibuffer_exit_hook = Qnil;
2360 DEFVAR_LISP ("history-length", &Vhistory_length,
2361 "*Maximum length for history lists before truncation takes place.\n\
2362 A number means that length; t means infinite. Truncation takes place\n\
2363 just after a new element is inserted. Setting the history-length\n\
2364 property of a history variable overrides this default.");
2365 XSETFASTINT (Vhistory_length, 30);
2367 DEFVAR_LISP ("completion-auto-help", &Vcompletion_auto_help,
2368 "*Non-nil means automatically provide help for invalid completion input.");
2369 Vcompletion_auto_help = Qt;
2371 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
2372 "Non-nil means don't consider case significant in completion.");
2373 completion_ignore_case = 0;
2375 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2376 "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\
2377 This variable makes a difference whenever the minibuffer window is active.");
2378 enable_recursive_minibuffers = 0;
2380 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2381 "Alist or obarray used for completion in the minibuffer.\n\
2382 This becomes the ALIST argument to `try-completion' and `all-completion'.\n\
2384 The value may alternatively be a function, which is given three arguments:\n\
2385 STRING, the current buffer contents;\n\
2386 PREDICATE, the predicate for filtering possible matches;\n\
2387 CODE, which says what kind of things to do.\n\
2388 CODE can be nil, t or `lambda'.\n\
2389 nil means to return the best completion of STRING, or nil if there is none.\n\
2390 t means to return a list of all possible completions of STRING.\n\
2391 `lambda' means to return t if STRING is a valid completion as it stands.");
2392 Vminibuffer_completion_table = Qnil;
2394 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2395 "Within call to `completing-read', this holds the PREDICATE argument.");
2396 Vminibuffer_completion_predicate = Qnil;
2398 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
2399 "Non-nil => demand confirmation of completion before exiting minibuffer.");
2400 Vminibuffer_completion_confirm = Qnil;
2402 DEFVAR_LISP ("minibuffer-completing-file-name",
2403 &Vminibuffer_completing_file_name,
2404 "Non-nil means completing file names.");
2405 Vminibuffer_completing_file_name = Qnil;
2407 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2408 "Value that `help-form' takes on inside the minibuffer.");
2409 Vminibuffer_help_form = Qnil;
2411 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2412 "History list symbol to add minibuffer values to.\n\
2413 Each string of minibuffer input, as it appears on exit from the minibuffer,\n\
2414 is added with\n\
2415 (set minibuffer-history-variable\n\
2416 (cons STRING (symbol-value minibuffer-history-variable)))");
2417 XSETFASTINT (Vminibuffer_history_variable, 0);
2419 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2420 "Current position of redoing in the history list.");
2421 Vminibuffer_history_position = Qnil;
2423 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2424 "*Non-nil means entering the minibuffer raises the minibuffer's frame.\n\
2425 Some uses of the echo area also raise that frame (since they use it too).");
2426 minibuffer_auto_raise = 0;
2428 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
2429 "List of regexps that should restrict possible completions.");
2430 Vcompletion_regexp_list = Qnil;
2432 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2433 &minibuffer_allow_text_properties,
2434 "Non-nil means `read-from-minibuffer' should not discard text properties.\n\
2435 This also affects `read-string', but it does not affect `read-minibuffer',\n\
2436 `read-no-blanks-input', or any of the functions that do minibuffer input\n\
2437 with completion; they always discard text properties.");
2438 minibuffer_allow_text_properties = 0;
2440 defsubr (&Sset_minibuffer_window);
2441 defsubr (&Sread_from_minibuffer);
2442 defsubr (&Seval_minibuffer);
2443 defsubr (&Sread_minibuffer);
2444 defsubr (&Sread_string);
2445 defsubr (&Sread_command);
2446 defsubr (&Sread_variable);
2447 defsubr (&Sread_buffer);
2448 defsubr (&Sread_no_blanks_input);
2449 defsubr (&Sminibuffer_depth);
2450 defsubr (&Sminibuffer_prompt);
2452 defsubr (&Stry_completion);
2453 defsubr (&Sall_completions);
2454 defsubr (&Scompleting_read);
2455 defsubr (&Sminibuffer_complete);
2456 defsubr (&Sminibuffer_complete_word);
2457 defsubr (&Sminibuffer_complete_and_exit);
2458 defsubr (&Sdisplay_completion_list);
2459 defsubr (&Sminibuffer_completion_help);
2461 defsubr (&Sself_insert_and_exit);
2462 defsubr (&Sexit_minibuffer);
2464 defsubr (&Sminibuffer_message);
2467 void
2468 keys_of_minibuf ()
2470 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
2471 "abort-recursive-edit");
2472 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
2473 "exit-minibuffer");
2474 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
2475 "exit-minibuffer");
2477 initial_define_key (Vminibuffer_local_ns_map, Ctl ('g'),
2478 "abort-recursive-edit");
2479 initial_define_key (Vminibuffer_local_ns_map, Ctl ('m'),
2480 "exit-minibuffer");
2481 initial_define_key (Vminibuffer_local_ns_map, Ctl ('j'),
2482 "exit-minibuffer");
2484 initial_define_key (Vminibuffer_local_ns_map, ' ',
2485 "exit-minibuffer");
2486 initial_define_key (Vminibuffer_local_ns_map, '\t',
2487 "exit-minibuffer");
2488 initial_define_key (Vminibuffer_local_ns_map, '?',
2489 "self-insert-and-exit");
2491 initial_define_key (Vminibuffer_local_completion_map, Ctl ('g'),
2492 "abort-recursive-edit");
2493 initial_define_key (Vminibuffer_local_completion_map, Ctl ('m'),
2494 "exit-minibuffer");
2495 initial_define_key (Vminibuffer_local_completion_map, Ctl ('j'),
2496 "exit-minibuffer");
2498 initial_define_key (Vminibuffer_local_completion_map, '\t',
2499 "minibuffer-complete");
2500 initial_define_key (Vminibuffer_local_completion_map, ' ',
2501 "minibuffer-complete-word");
2502 initial_define_key (Vminibuffer_local_completion_map, '?',
2503 "minibuffer-completion-help");
2505 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('g'),
2506 "abort-recursive-edit");
2507 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
2508 "minibuffer-complete-and-exit");
2509 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
2510 "minibuffer-complete-and-exit");
2511 initial_define_key (Vminibuffer_local_must_match_map, '\t',
2512 "minibuffer-complete");
2513 initial_define_key (Vminibuffer_local_must_match_map, ' ',
2514 "minibuffer-complete-word");
2515 initial_define_key (Vminibuffer_local_must_match_map, '?',
2516 "minibuffer-completion-help");