(dired-bunch-files): Put the arg FILES
[emacs.git] / src / minibuf.c
blob4b5185abdb164eb28783da4b15aa17c4c9c461db
1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001 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>
26 #include "lisp.h"
27 #include "commands.h"
28 #include "buffer.h"
29 #include "charset.h"
30 #include "dispextern.h"
31 #include "keyboard.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "syntax.h"
35 #include "intervals.h"
36 #include "keymap.h"
38 extern int quit_char;
40 /* List of buffers for use as minibuffers.
41 The first element of the list is used for the outermost minibuffer
42 invocation, the next element is used for a recursive minibuffer
43 invocation, etc. The list is extended at the end as deeper
44 minibuffer recursions are encountered. */
46 Lisp_Object Vminibuffer_list;
48 /* Data to remember during recursive minibuffer invocations */
50 Lisp_Object minibuf_save_list;
52 /* Depth in minibuffer invocations. */
54 int minibuf_level;
56 /* Nonzero means display completion help for invalid input. */
58 Lisp_Object Vcompletion_auto_help;
60 /* The maximum length of a minibuffer history. */
62 Lisp_Object Qhistory_length, Vhistory_length;
64 /* Fread_minibuffer leaves the input here as a string. */
66 Lisp_Object last_minibuf_string;
68 /* Nonzero means let functions called when within a minibuffer
69 invoke recursive minibuffers (to read arguments, or whatever) */
71 int enable_recursive_minibuffers;
73 /* Nonzero means don't ignore text properties
74 in Fread_from_minibuffer. */
76 int minibuffer_allow_text_properties;
78 /* help-form is bound to this while in the minibuffer. */
80 Lisp_Object Vminibuffer_help_form;
82 /* Variable which is the history list to add minibuffer values to. */
84 Lisp_Object Vminibuffer_history_variable;
86 /* Current position in the history list (adjusted by M-n and M-p). */
88 Lisp_Object Vminibuffer_history_position;
90 /* Text properties that are added to minibuffer prompts.
91 These are in addition to the basic `field' property, and stickiness
92 properties. */
94 Lisp_Object Vminibuffer_prompt_properties;
96 Lisp_Object Qminibuffer_history, Qbuffer_name_history;
98 Lisp_Object Qread_file_name_internal;
100 /* Normal hooks for entry to and exit from minibuffer. */
102 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
103 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
105 /* Function to call to read a buffer name. */
106 Lisp_Object Vread_buffer_function;
108 /* Nonzero means completion ignores case. */
110 int completion_ignore_case;
112 /* List of regexps that should restrict possible completions. */
114 Lisp_Object Vcompletion_regexp_list;
116 /* Nonzero means raise the minibuffer frame when the minibuffer
117 is entered. */
119 int minibuffer_auto_raise;
121 /* If last completion attempt reported "Complete but not unique"
122 then this is the string completed then; otherwise this is nil. */
124 static Lisp_Object last_exact_completion;
126 extern Lisp_Object Voverriding_local_map;
128 Lisp_Object Quser_variable_p;
130 Lisp_Object Qminibuffer_default;
132 Lisp_Object Qcurrent_input_method, Qactivate_input_method;
134 extern Lisp_Object Qmouse_face;
136 extern Lisp_Object Qfield;
138 /* Put minibuf on currently selected frame's minibuffer.
139 We do this whenever the user starts a new minibuffer
140 or when a minibuffer exits. */
142 void
143 choose_minibuf_frame ()
145 if (FRAMEP (selected_frame)
146 && FRAME_LIVE_P (XFRAME (selected_frame))
147 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
149 struct frame *sf = XFRAME (selected_frame);
150 Lisp_Object buffer;
152 /* I don't think that any frames may validly have a null minibuffer
153 window anymore. */
154 if (NILP (sf->minibuffer_window))
155 abort ();
157 /* Under X, we come here with minibuf_window being the
158 minibuffer window of the unused termcap window created in
159 init_window_once. That window doesn't have a buffer. */
160 buffer = XWINDOW (minibuf_window)->buffer;
161 if (BUFFERP (buffer))
162 Fset_window_buffer (sf->minibuffer_window, buffer);
163 minibuf_window = sf->minibuffer_window;
166 /* Make sure no other frame has a minibuffer as its selected window,
167 because the text would not be displayed in it, and that would be
168 confusing. Only allow the selected frame to do this,
169 and that only if the minibuffer is active. */
171 Lisp_Object tail, frame;
173 FOR_EACH_FRAME (tail, frame)
174 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
175 && !(EQ (frame, selected_frame)
176 && minibuf_level > 0))
177 Fset_frame_selected_window (frame, Fframe_first_window (frame));
181 Lisp_Object
182 choose_minibuf_frame_1 (ignore)
183 Lisp_Object ignore;
185 choose_minibuf_frame ();
186 return Qnil;
189 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
190 Sset_minibuffer_window, 1, 1, 0,
191 doc: /* Specify which minibuffer window to use for the minibuffer.
192 This effects where the minibuffer is displayed if you put text in it
193 without invoking the usual minibuffer commands. */)
194 (window)
195 Lisp_Object window;
197 CHECK_WINDOW (window);
198 if (! MINI_WINDOW_P (XWINDOW (window)))
199 error ("Window is not a minibuffer window");
201 minibuf_window = window;
203 return window;
207 /* Actual minibuffer invocation. */
209 static Lisp_Object read_minibuf_unwind P_ ((Lisp_Object));
210 static Lisp_Object read_minibuf P_ ((Lisp_Object, Lisp_Object,
211 Lisp_Object, Lisp_Object,
212 int, Lisp_Object,
213 Lisp_Object, Lisp_Object,
214 int, int));
215 static Lisp_Object read_minibuf_noninteractive P_ ((Lisp_Object, Lisp_Object,
216 Lisp_Object, Lisp_Object,
217 int, Lisp_Object,
218 Lisp_Object, Lisp_Object,
219 int, int));
220 static Lisp_Object string_to_object P_ ((Lisp_Object, Lisp_Object));
223 /* Read a Lisp object from VAL and return it. If VAL is an empty
224 string, and DEFALT is a string, read from DEFALT instead of VAL. */
226 static Lisp_Object
227 string_to_object (val, defalt)
228 Lisp_Object val, defalt;
230 struct gcpro gcpro1, gcpro2;
231 Lisp_Object expr_and_pos;
232 int pos;
234 GCPRO2 (val, defalt);
236 if (STRINGP (val) && SCHARS (val) == 0
237 && STRINGP (defalt))
238 val = defalt;
240 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
241 pos = XINT (Fcdr (expr_and_pos));
242 if (pos != SCHARS (val))
244 /* Ignore trailing whitespace; any other trailing junk
245 is an error. */
246 int i;
247 pos = string_char_to_byte (val, pos);
248 for (i = pos; i < SBYTES (val); i++)
250 int c = SREF (val, i);
251 if (c != ' ' && c != '\t' && c != '\n')
252 error ("Trailing garbage following expression");
256 val = Fcar (expr_and_pos);
257 RETURN_UNGCPRO (val);
261 /* Like read_minibuf but reading from stdin. This function is called
262 from read_minibuf to do the job if noninteractive. */
264 static Lisp_Object
265 read_minibuf_noninteractive (map, initial, prompt, backup_n, expflag,
266 histvar, histpos, defalt, allow_props,
267 inherit_input_method)
268 Lisp_Object map;
269 Lisp_Object initial;
270 Lisp_Object prompt;
271 Lisp_Object backup_n;
272 int expflag;
273 Lisp_Object histvar;
274 Lisp_Object histpos;
275 Lisp_Object defalt;
276 int allow_props;
277 int inherit_input_method;
279 int size, len;
280 char *line, *s;
281 Lisp_Object val;
283 fprintf (stdout, "%s", SDATA (prompt));
284 fflush (stdout);
286 val = Qnil;
287 size = 100;
288 len = 0;
289 line = (char *) xmalloc (size * sizeof *line);
290 while ((s = fgets (line + len, size - len, stdin)) != NULL
291 && (len = strlen (line),
292 len == size - 1 && line[len - 1] != '\n'))
294 size *= 2;
295 line = (char *) xrealloc (line, size);
298 if (s)
300 len = strlen (line);
302 if (len > 0 && line[len - 1] == '\n')
303 line[--len] = '\0';
305 val = build_string (line);
306 xfree (line);
308 else
310 xfree (line);
311 error ("Error reading from stdin");
314 /* If Lisp form desired instead of string, parse it. */
315 if (expflag)
316 val = string_to_object (val, defalt);
318 return val;
321 DEFUN ("minibufferp", Fminibufferp,
322 Sminibufferp, 0, 0, 0,
323 doc: /* Return t if the current buffer is a minibuffer. */)
326 Lisp_Object tem;
328 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
329 return ! NILP (tem) ? Qt : Qnil;
332 DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
333 Sminibuffer_prompt_end, 0, 0, 0,
334 doc: /* Return the buffer position of the end of the minibuffer prompt.
335 Return (point-min) if current buffer is not a mini-buffer. */)
338 /* This function is written to be most efficient when there's a prompt. */
339 Lisp_Object beg, end, tem;
340 beg = make_number (BEGV);
342 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
343 if (NILP (tem))
344 return beg;
346 end = Ffield_end (beg, Qnil, Qnil);
348 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
349 return beg;
350 else
351 return end;
354 DEFUN ("minibuffer-contents", Fminibuffer_contents,
355 Sminibuffer_contents, 0, 0, 0,
356 doc: /* Return the user input in a minibuffer as a string.
357 The current buffer must be a minibuffer. */)
360 int prompt_end = XINT (Fminibuffer_prompt_end ());
361 return make_buffer_string (prompt_end, ZV, 1);
364 DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
365 Sminibuffer_contents_no_properties, 0, 0, 0,
366 doc: /* Return the user input in a minibuffer as a string, without text-properties.
367 The current buffer must be a minibuffer. */)
370 int prompt_end = XINT (Fminibuffer_prompt_end ());
371 return make_buffer_string (prompt_end, ZV, 0);
374 DEFUN ("delete-minibuffer-contents", Fdelete_minibuffer_contents,
375 Sdelete_minibuffer_contents, 0, 0, 0,
376 doc: /* Delete all user input in a minibuffer.
377 The current buffer must be a minibuffer. */)
380 int prompt_end = XINT (Fminibuffer_prompt_end ());
381 if (prompt_end < ZV)
382 del_range (prompt_end, ZV);
383 return Qnil;
386 /* Get the text in the minibuffer before point.
387 That is what completion commands operate on. */
389 Lisp_Object
390 minibuffer_completion_contents ()
392 int prompt_end = XINT (Fminibuffer_prompt_end ());
393 return make_buffer_string (prompt_end, PT, 1);
396 /* Read from the minibuffer using keymap MAP, initial contents INITIAL
397 (a string), putting point minus BACKUP_N bytes from the end of INITIAL,
398 prompting with PROMPT (a string), using history list HISTVAR
399 with initial position HISTPOS. (BACKUP_N should be <= 0.)
401 Normally return the result as a string (the text that was read),
402 but if EXPFLAG is nonzero, read it and return the object read.
403 If HISTVAR is given, save the value read on that history only if it doesn't
404 match the front of that history list exactly. The value is pushed onto
405 the list as the string that was read.
407 DEFALT specifies te default value for the sake of history commands.
409 If ALLOW_PROPS is nonzero, we do not throw away text properties.
411 if INHERIT_INPUT_METHOD is nonzeor, the minibuffer inherit the
412 current input method. */
414 static Lisp_Object
415 read_minibuf (map, initial, prompt, backup_n, expflag,
416 histvar, histpos, defalt, allow_props, inherit_input_method)
417 Lisp_Object map;
418 Lisp_Object initial;
419 Lisp_Object prompt;
420 Lisp_Object backup_n;
421 int expflag;
422 Lisp_Object histvar;
423 Lisp_Object histpos;
424 Lisp_Object defalt;
425 int allow_props;
426 int inherit_input_method;
428 Lisp_Object val;
429 int count = SPECPDL_INDEX ();
430 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
431 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
432 Lisp_Object enable_multibyte;
433 extern Lisp_Object Qfront_sticky;
434 extern Lisp_Object Qrear_nonsticky;
436 specbind (Qminibuffer_default, defalt);
438 single_kboard_state ();
439 #ifdef HAVE_X_WINDOWS
440 if (display_hourglass_p)
441 cancel_hourglass ();
442 #endif
444 val = Qnil;
445 ambient_dir = current_buffer->directory;
446 input_method = Qnil;
447 enable_multibyte = Qnil;
449 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
450 store them away before we can GC. Don't need to protect
451 BACKUP_N because we use the value only if it is an integer. */
452 GCPRO5 (map, initial, val, ambient_dir, input_method);
454 if (!STRINGP (prompt))
455 prompt = empty_string;
457 if (!enable_recursive_minibuffers
458 && minibuf_level > 0)
460 if (EQ (selected_window, minibuf_window))
461 error ("Command attempted to use minibuffer while in minibuffer");
462 else
463 /* If we're in another window, cancel the minibuffer that's active. */
464 Fthrow (Qexit,
465 build_string ("Command attempted to use minibuffer while in minibuffer"));
468 if (noninteractive)
470 val = read_minibuf_noninteractive (map, initial, prompt, backup_n,
471 expflag, histvar, histpos, defalt,
472 allow_props, inherit_input_method);
473 return unbind_to (count, val);
476 /* Choose the minibuffer window and frame, and take action on them. */
478 choose_minibuf_frame ();
480 record_unwind_protect (choose_minibuf_frame_1, Qnil);
482 record_unwind_protect (Fset_window_configuration,
483 Fcurrent_window_configuration (Qnil));
485 /* If the minibuffer window is on a different frame, save that
486 frame's configuration too. */
487 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
488 if (!EQ (mini_frame, selected_frame))
489 record_unwind_protect (Fset_window_configuration,
490 Fcurrent_window_configuration (mini_frame));
492 /* If the minibuffer is on an iconified or invisible frame,
493 make it visible now. */
494 Fmake_frame_visible (mini_frame);
496 if (minibuffer_auto_raise)
497 Fraise_frame (mini_frame);
499 /* We have to do this after saving the window configuration
500 since that is what restores the current buffer. */
502 /* Arrange to restore a number of minibuffer-related variables.
503 We could bind each variable separately, but that would use lots of
504 specpdl slots. */
505 minibuf_save_list
506 = Fcons (Voverriding_local_map,
507 Fcons (minibuf_window, minibuf_save_list));
508 minibuf_save_list
509 = Fcons (minibuf_prompt,
510 Fcons (make_number (minibuf_prompt_width),
511 Fcons (Vhelp_form,
512 Fcons (Vcurrent_prefix_arg,
513 Fcons (Vminibuffer_history_position,
514 Fcons (Vminibuffer_history_variable,
515 minibuf_save_list))))));
517 record_unwind_protect (read_minibuf_unwind, Qnil);
518 minibuf_level++;
520 /* Now that we can restore all those variables, start changing them. */
522 minibuf_prompt_width = 0;
523 minibuf_prompt = Fcopy_sequence (prompt);
524 Vminibuffer_history_position = histpos;
525 Vminibuffer_history_variable = histvar;
526 Vhelp_form = Vminibuffer_help_form;
528 if (inherit_input_method)
530 /* `current-input-method' is buffer local. So, remember it in
531 INPUT_METHOD before changing the current buffer. */
532 input_method = Fsymbol_value (Qcurrent_input_method);
533 enable_multibyte = current_buffer->enable_multibyte_characters;
536 /* Switch to the minibuffer. */
538 minibuffer = get_minibuffer (minibuf_level);
539 Fset_buffer (minibuffer);
541 /* The current buffer's default directory is usually the right thing
542 for our minibuffer here. However, if you're typing a command at
543 a minibuffer-only frame when minibuf_level is zero, then buf IS
544 the current_buffer, so reset_buffer leaves buf's default
545 directory unchanged. This is a bummer when you've just started
546 up Emacs and buf's default directory is Qnil. Here's a hack; can
547 you think of something better to do? Find another buffer with a
548 better directory, and use that one instead. */
549 if (STRINGP (ambient_dir))
550 current_buffer->directory = ambient_dir;
551 else
553 Lisp_Object buf_list;
555 for (buf_list = Vbuffer_alist;
556 CONSP (buf_list);
557 buf_list = XCDR (buf_list))
559 Lisp_Object other_buf;
561 other_buf = XCDR (XCAR (buf_list));
562 if (STRINGP (XBUFFER (other_buf)->directory))
564 current_buffer->directory = XBUFFER (other_buf)->directory;
565 break;
570 if (!EQ (mini_frame, selected_frame))
571 Fredirect_frame_focus (selected_frame, mini_frame);
573 Vminibuf_scroll_window = selected_window;
574 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
575 minibuf_selected_window = selected_window;
576 Fset_window_buffer (minibuf_window, Fcurrent_buffer ());
577 Fselect_window (minibuf_window);
578 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
580 Fmake_local_variable (Qprint_escape_newlines);
581 print_escape_newlines = 1;
583 /* Erase the buffer. */
585 int count1 = SPECPDL_INDEX ();
586 specbind (Qinhibit_read_only, Qt);
587 specbind (Qinhibit_modification_hooks, Qt);
588 Ferase_buffer ();
589 unbind_to (count1, Qnil);
592 if (!NILP (current_buffer->enable_multibyte_characters)
593 && ! STRING_MULTIBYTE (minibuf_prompt))
594 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
596 /* Insert the prompt, record where it ends. */
597 Finsert (1, &minibuf_prompt);
598 if (PT > BEG)
600 Fput_text_property (make_number (BEG), make_number (PT),
601 Qfront_sticky, Qt, Qnil);
602 Fput_text_property (make_number (BEG), make_number (PT),
603 Qrear_nonsticky, Qt, Qnil);
604 Fput_text_property (make_number (BEG), make_number (PT),
605 Qfield, Qt, Qnil);
606 Fadd_text_properties (make_number (BEG), make_number (PT),
607 Vminibuffer_prompt_properties, Qnil);
610 minibuf_prompt_width = (int) current_column (); /* iftc */
612 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
613 if (inherit_input_method)
614 current_buffer->enable_multibyte_characters = enable_multibyte;
616 /* Put in the initial input. */
617 if (!NILP (initial))
619 Finsert (1, &initial);
620 if (INTEGERP (backup_n))
621 Fforward_char (backup_n);
624 clear_message (1, 1);
625 current_buffer->keymap = map;
627 /* Turn on an input method stored in INPUT_METHOD if any. */
628 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
629 call1 (Qactivate_input_method, input_method);
631 /* Run our hook, but not if it is empty.
632 (run-hooks would do nothing if it is empty,
633 but it's important to save time here in the usual case.) */
634 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
635 && !NILP (Vrun_hooks))
636 call1 (Vrun_hooks, Qminibuffer_setup_hook);
638 /* Don't allow the user to undo past this point. */
639 current_buffer->undo_list = Qnil;
641 recursive_edit_1 ();
643 /* If cursor is on the minibuffer line,
644 show the user we have exited by putting it in column 0. */
645 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
646 && !noninteractive)
648 XWINDOW (minibuf_window)->cursor.hpos = 0;
649 XWINDOW (minibuf_window)->cursor.x = 0;
650 XWINDOW (minibuf_window)->must_be_updated_p = 1;
651 update_frame (XFRAME (selected_frame), 1, 1);
652 if (rif && rif->flush_display)
653 rif->flush_display (XFRAME (XWINDOW (minibuf_window)->frame));
656 /* Make minibuffer contents into a string. */
657 Fset_buffer (minibuffer);
658 if (allow_props)
659 val = Fminibuffer_contents ();
660 else
661 val = Fminibuffer_contents_no_properties ();
663 /* VAL is the string of minibuffer text. */
665 last_minibuf_string = val;
667 /* Add the value to the appropriate history list unless it is empty. */
668 if (SCHARS (val) != 0
669 && SYMBOLP (Vminibuffer_history_variable))
671 /* If the caller wanted to save the value read on a history list,
672 then do so if the value is not already the front of the list. */
673 Lisp_Object histval;
675 /* If variable is unbound, make it nil. */
676 if (EQ (SYMBOL_VALUE (Vminibuffer_history_variable), Qunbound))
677 Fset (Vminibuffer_history_variable, Qnil);
679 histval = Fsymbol_value (Vminibuffer_history_variable);
681 /* The value of the history variable must be a cons or nil. Other
682 values are unacceptable. We silently ignore these values. */
683 if (NILP (histval)
684 || (CONSP (histval)
685 && NILP (Fequal (last_minibuf_string, Fcar (histval)))))
687 Lisp_Object length;
689 histval = Fcons (last_minibuf_string, histval);
690 Fset (Vminibuffer_history_variable, histval);
692 /* Truncate if requested. */
693 length = Fget (Vminibuffer_history_variable, Qhistory_length);
694 if (NILP (length)) length = Vhistory_length;
695 if (INTEGERP (length))
697 if (XINT (length) <= 0)
698 Fset (Vminibuffer_history_variable, Qnil);
699 else
701 Lisp_Object temp;
703 temp = Fnthcdr (Fsub1 (length), histval);
704 if (CONSP (temp)) Fsetcdr (temp, Qnil);
710 /* If Lisp form desired instead of string, parse it. */
711 if (expflag)
712 val = string_to_object (val, defalt);
714 /* The appropriate frame will get selected
715 in set-window-configuration. */
716 RETURN_UNGCPRO (unbind_to (count, val));
719 /* Return a buffer to be used as the minibuffer at depth `depth'.
720 depth = 0 is the lowest allowed argument, and that is the value
721 used for nonrecursive minibuffer invocations */
723 Lisp_Object
724 get_minibuffer (depth)
725 int depth;
727 Lisp_Object tail, num, buf;
728 char name[24];
729 extern Lisp_Object nconc2 ();
731 XSETFASTINT (num, depth);
732 tail = Fnthcdr (num, Vminibuffer_list);
733 if (NILP (tail))
735 tail = Fcons (Qnil, Qnil);
736 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
738 buf = Fcar (tail);
739 if (NILP (buf) || NILP (XBUFFER (buf)->name))
741 sprintf (name, " *Minibuf-%d*", depth);
742 buf = Fget_buffer_create (build_string (name));
744 /* Although the buffer's name starts with a space, undo should be
745 enabled in it. */
746 Fbuffer_enable_undo (buf);
748 XSETCAR (tail, buf);
750 else
752 int count = SPECPDL_INDEX ();
754 reset_buffer (XBUFFER (buf));
755 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
756 Fset_buffer (buf);
757 Fkill_all_local_variables ();
758 unbind_to (count, Qnil);
761 return buf;
764 /* This function is called on exiting minibuffer, whether normally or
765 not, and it restores the current window, buffer, etc. */
767 static Lisp_Object
768 read_minibuf_unwind (data)
769 Lisp_Object data;
771 Lisp_Object old_deactivate_mark;
772 Lisp_Object window;
774 /* We are exiting the minibuffer one way or the other,
775 so run the hook. */
776 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
777 && !NILP (Vrun_hooks))
778 safe_run_hooks (Qminibuffer_exit_hook);
780 /* If this was a recursive minibuffer,
781 tie the minibuffer window back to the outer level minibuffer buffer. */
782 minibuf_level--;
784 window = minibuf_window;
785 /* To keep things predictable, in case it matters, let's be in the
786 minibuffer when we reset the relevant variables. */
787 Fset_buffer (XWINDOW (window)->buffer);
789 /* Restore prompt, etc, from outer minibuffer level. */
790 minibuf_prompt = Fcar (minibuf_save_list);
791 minibuf_save_list = Fcdr (minibuf_save_list);
792 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
793 minibuf_save_list = Fcdr (minibuf_save_list);
794 Vhelp_form = Fcar (minibuf_save_list);
795 minibuf_save_list = Fcdr (minibuf_save_list);
796 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
797 minibuf_save_list = Fcdr (minibuf_save_list);
798 Vminibuffer_history_position = Fcar (minibuf_save_list);
799 minibuf_save_list = Fcdr (minibuf_save_list);
800 Vminibuffer_history_variable = Fcar (minibuf_save_list);
801 minibuf_save_list = Fcdr (minibuf_save_list);
802 Voverriding_local_map = Fcar (minibuf_save_list);
803 minibuf_save_list = Fcdr (minibuf_save_list);
804 #if 0
805 temp = Fcar (minibuf_save_list);
806 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
807 minibuf_window = temp;
808 #endif
809 minibuf_save_list = Fcdr (minibuf_save_list);
811 /* Erase the minibuffer we were using at this level. */
813 int count = SPECPDL_INDEX ();
814 /* Prevent error in erase-buffer. */
815 specbind (Qinhibit_read_only, Qt);
816 specbind (Qinhibit_modification_hooks, Qt);
817 old_deactivate_mark = Vdeactivate_mark;
818 Ferase_buffer ();
819 Vdeactivate_mark = old_deactivate_mark;
820 unbind_to (count, Qnil);
823 /* When we get to the outmost level, make sure we resize the
824 mini-window back to its normal size. */
825 if (minibuf_level == 0)
826 resize_mini_window (XWINDOW (window), 0);
828 /* Make sure minibuffer window is erased, not ignored. */
829 windows_or_buffers_changed++;
830 XSETFASTINT (XWINDOW (window)->last_modified, 0);
831 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
832 return Qnil;
836 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
837 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
838 If optional second arg INITIAL-CONTENTS is non-nil, it is a string
839 to be inserted into the minibuffer before reading input.
840 If INITIAL-CONTENTS is (STRING . POSITION), the initial input
841 is STRING, but point is placed at position POSITION in the minibuffer.
842 Third arg KEYMAP is a keymap to use whilst reading;
843 if omitted or nil, the default is `minibuffer-local-map'.
844 If fourth arg READ is non-nil, then interpret the result as a Lisp object
845 and return that object:
846 in other words, do `(car (read-from-string INPUT-STRING))'
847 Fifth arg HIST, if non-nil, specifies a history list
848 and optionally the initial position in the list.
849 It can be a symbol, which is the history list variable to use,
850 or it can be a cons cell (HISTVAR . HISTPOS).
851 In that case, HISTVAR is the history list variable to use,
852 and HISTPOS is the initial position (the position in the list
853 which INITIAL-CONTENTS corresponds to).
854 Positions are counted starting from 1 at the beginning of the list.
855 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
856 for history commands; but `read-from-minibuffer' does NOT return DEFAULT-VALUE
857 if the user enters empty input! It returns the empty string.
858 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
859 the current input method and the setting of `enable-multibyte-characters'.
860 If the variable `minibuffer-allow-text-properties' is non-nil,
861 then the string which is returned includes whatever text properties
862 were present in the minibuffer. Otherwise the value has no text properties. */)
863 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
864 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
865 Lisp_Object inherit_input_method;
867 int pos = 0;
868 Lisp_Object histvar, histpos, position, val;
869 struct gcpro gcpro1;
871 position = Qnil;
873 CHECK_STRING (prompt);
874 if (!NILP (initial_contents))
876 if (CONSP (initial_contents))
878 position = Fcdr (initial_contents);
879 initial_contents = Fcar (initial_contents);
881 CHECK_STRING (initial_contents);
882 if (!NILP (position))
884 CHECK_NUMBER (position);
885 /* Convert to distance from end of input. */
886 if (XINT (position) < 1)
887 /* A number too small means the beginning of the string. */
888 pos = - SCHARS (initial_contents);
889 else
890 pos = XINT (position) - 1 - SCHARS (initial_contents);
894 if (NILP (keymap))
895 keymap = Vminibuffer_local_map;
896 else
897 keymap = get_keymap (keymap, 1, 0);
899 if (SYMBOLP (hist))
901 histvar = hist;
902 histpos = Qnil;
904 else
906 histvar = Fcar_safe (hist);
907 histpos = Fcdr_safe (hist);
909 if (NILP (histvar))
910 histvar = Qminibuffer_history;
911 if (NILP (histpos))
912 XSETFASTINT (histpos, 0);
914 GCPRO1 (default_value);
915 val = read_minibuf (keymap, initial_contents, prompt,
916 make_number (pos), !NILP (read),
917 histvar, histpos, default_value,
918 minibuffer_allow_text_properties,
919 !NILP (inherit_input_method));
920 UNGCPRO;
921 return val;
924 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
925 doc: /* Return a Lisp object read using the minibuffer.
926 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
927 is a string to insert in the minibuffer before reading. */)
928 (prompt, initial_contents)
929 Lisp_Object prompt, initial_contents;
931 CHECK_STRING (prompt);
932 if (!NILP (initial_contents))
933 CHECK_STRING (initial_contents);
934 return read_minibuf (Vminibuffer_local_map, initial_contents,
935 prompt, Qnil, 1, Qminibuffer_history,
936 make_number (0), Qnil, 0, 0);
939 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
940 doc: /* Return value of Lisp expression read using the minibuffer.
941 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
942 is a string to insert in the minibuffer before reading. */)
943 (prompt, initial_contents)
944 Lisp_Object prompt, initial_contents;
946 return Feval (Fread_minibuffer (prompt, initial_contents));
949 /* Functions that use the minibuffer to read various things. */
951 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
952 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
953 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
954 The third arg HISTORY, if non-nil, specifies a history list
955 and optionally the initial position in the list.
956 See `read-from-minibuffer' for details of HISTORY argument.
957 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
958 for history commands, and as the value to return if the user enters
959 the empty string.
960 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
961 the current input method and the setting of `enable-multibyte-characters'. */)
962 (prompt, initial_input, history, default_value, inherit_input_method)
963 Lisp_Object prompt, initial_input, history, default_value;
964 Lisp_Object inherit_input_method;
966 Lisp_Object val;
967 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
968 Qnil, history, default_value,
969 inherit_input_method);
970 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
971 val = default_value;
972 return val;
975 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
976 doc: /* Read a string from the terminal, not allowing blanks.
977 Prompt with PROMPT, and provide INITIAL as an initial value of the input string.
978 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
979 the current input method and the setting of `enable-multibyte-characters'. */)
980 (prompt, initial, inherit_input_method)
981 Lisp_Object prompt, initial, inherit_input_method;
983 CHECK_STRING (prompt);
984 if (! NILP (initial))
985 CHECK_STRING (initial);
987 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
988 0, Qminibuffer_history, make_number (0), Qnil, 0,
989 !NILP (inherit_input_method));
992 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
993 doc: /* Read the name of a command and return as a symbol.
994 Prompt with PROMPT. By default, return DEFAULT-VALUE. */)
995 (prompt, default_value)
996 Lisp_Object prompt, default_value;
998 Lisp_Object name, default_string;
1000 if (NILP (default_value))
1001 default_string = Qnil;
1002 else if (SYMBOLP (default_value))
1003 default_string = SYMBOL_NAME (default_value);
1004 else
1005 default_string = default_value;
1007 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1008 Qnil, Qnil, default_string, Qnil);
1009 if (NILP (name))
1010 return name;
1011 return Fintern (name, Qnil);
1014 #ifdef NOTDEF
1015 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
1016 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
1017 Prompt with PROMPT. */)
1018 (prompt)
1019 Lisp_Object prompt;
1021 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
1022 Qnil);
1024 #endif /* NOTDEF */
1026 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
1027 doc: /* Read the name of a user variable and return it as a symbol.
1028 Prompt with PROMPT. By default, return DEFAULT-VALUE.
1029 A user variable is one whose documentation starts with a `*' character. */)
1030 (prompt, default_value)
1031 Lisp_Object prompt, default_value;
1033 Lisp_Object name, default_string;
1035 if (NILP (default_value))
1036 default_string = Qnil;
1037 else if (SYMBOLP (default_value))
1038 default_string = SYMBOL_NAME (default_value);
1039 else
1040 default_string = default_value;
1042 name = Fcompleting_read (prompt, Vobarray,
1043 Quser_variable_p, Qt,
1044 Qnil, Qnil, default_string, Qnil);
1045 if (NILP (name))
1046 return name;
1047 return Fintern (name, Qnil);
1050 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
1051 doc: /* Read the name of a buffer and return as a string.
1052 Prompt with PROMPT.
1053 Optional second arg DEF is value to return if user enters an empty line.
1054 If optional third arg REQUIRE-MATCH is non-nil,
1055 only existing buffer names are allowed. */)
1056 (prompt, def, require_match)
1057 Lisp_Object prompt, def, require_match;
1059 Lisp_Object args[4];
1061 if (BUFFERP (def))
1062 def = XBUFFER (def)->name;
1064 if (NILP (Vread_buffer_function))
1066 if (!NILP (def))
1068 args[0] = build_string ("%s(default %s) ");
1069 args[1] = prompt;
1070 args[2] = def;
1071 prompt = Fformat (3, args);
1074 return Fcompleting_read (prompt, Vbuffer_alist, Qnil,
1075 require_match, Qnil, Qbuffer_name_history,
1076 def, Qnil);
1078 else
1080 args[0] = Vread_buffer_function;
1081 args[1] = prompt;
1082 args[2] = def;
1083 args[3] = require_match;
1084 return Ffuncall(4, args);
1088 static Lisp_Object
1089 minibuf_conform_representation (string, basis)
1090 Lisp_Object string, basis;
1092 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1093 return string;
1095 if (STRING_MULTIBYTE (string))
1096 return Fstring_make_unibyte (string);
1097 else
1098 return Fstring_make_multibyte (string);
1101 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1102 doc: /* Return common substring of all completions of STRING in ALIST.
1103 Each car of each element of ALIST is tested to see if it begins with STRING.
1104 All that match are compared together; the longest initial sequence
1105 common to all matches is returned as a string.
1106 If there is no match at all, nil is returned.
1107 For a unique match which is exact, t is returned.
1109 If ALIST is a hash-table, all the string keys are the possible matches.
1110 If ALIST is an obarray, the names of all symbols in the obarray
1111 are the possible matches.
1113 ALIST can also be a function to do the completion itself.
1114 It receives three arguments: the values STRING, PREDICATE and nil.
1115 Whatever it returns becomes the value of `try-completion'.
1117 If optional third argument PREDICATE is non-nil,
1118 it is used to test each possible match.
1119 The match is a candidate only if PREDICATE returns non-nil.
1120 The argument given to PREDICATE is the alist element
1121 or the symbol from the obarray. If ALIST is a hash-table,
1122 predicate is called with two arguments: the key and the value.
1123 Additionally to this predicate, `completion-regexp-list'
1124 is used to further constrain the set of candidates. */)
1125 (string, alist, predicate)
1126 Lisp_Object string, alist, predicate;
1128 Lisp_Object bestmatch, tail, elt, eltstring;
1129 /* Size in bytes of BESTMATCH. */
1130 int bestmatchsize = 0;
1131 /* These are in bytes, too. */
1132 int compare, matchsize;
1133 int type = HASH_TABLE_P (alist) ? 3
1134 : VECTORP (alist) ? 2
1135 : NILP (alist) || (CONSP (alist)
1136 && (!SYMBOLP (XCAR (alist))
1137 || NILP (XCAR (alist))));
1138 int index = 0, obsize = 0;
1139 int matchcount = 0;
1140 Lisp_Object bucket, zero, end, tem;
1141 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1143 CHECK_STRING (string);
1144 if (type == 0)
1145 return call3 (alist, string, predicate, Qnil);
1147 bestmatch = bucket = Qnil;
1149 /* If ALIST is not a list, set TAIL just for gc pro. */
1150 tail = alist;
1151 if (type == 2)
1153 obsize = XVECTOR (alist)->size;
1154 bucket = XVECTOR (alist)->contents[index];
1157 while (1)
1159 /* Get the next element of the alist, obarray, or hash-table. */
1160 /* Exit the loop if the elements are all used up. */
1161 /* elt gets the alist element or symbol.
1162 eltstring gets the name to check as a completion. */
1164 if (type == 1)
1166 if (!CONSP (tail))
1167 break;
1168 elt = XCAR (tail);
1169 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1170 tail = XCDR (tail);
1172 else if (type == 2)
1174 if (XFASTINT (bucket) != 0)
1176 elt = bucket;
1177 eltstring = Fsymbol_name (elt);
1178 if (XSYMBOL (bucket)->next)
1179 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1180 else
1181 XSETFASTINT (bucket, 0);
1183 else if (++index >= obsize)
1184 break;
1185 else
1187 bucket = XVECTOR (alist)->contents[index];
1188 continue;
1191 else /* if (type == 3) */
1193 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1194 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1195 index++;
1196 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1197 break;
1198 else
1199 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1202 /* Is this element a possible completion? */
1204 if (STRINGP (eltstring)
1205 && SCHARS (string) <= SCHARS (eltstring)
1206 && (tem = Fcompare_strings (eltstring, make_number (0),
1207 make_number (SCHARS (string)),
1208 string, make_number (0), Qnil,
1209 completion_ignore_case ?Qt : Qnil),
1210 EQ (Qt, tem)))
1212 /* Yes. */
1213 Lisp_Object regexps;
1214 Lisp_Object zero;
1215 XSETFASTINT (zero, 0);
1217 /* Ignore this element if it fails to match all the regexps. */
1218 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1219 regexps = XCDR (regexps))
1221 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1222 if (NILP (tem))
1223 break;
1225 if (CONSP (regexps))
1226 continue;
1228 /* Ignore this element if there is a predicate
1229 and the predicate doesn't like it. */
1231 if (!NILP (predicate))
1233 if (EQ (predicate, Qcommandp))
1234 tem = Fcommandp (elt, Qnil);
1235 else
1237 GCPRO4 (tail, string, eltstring, bestmatch);
1238 tem = type == 3
1239 ? call2 (predicate, elt,
1240 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1241 : call1 (predicate, elt);
1242 UNGCPRO;
1244 if (NILP (tem)) continue;
1247 /* Update computation of how much all possible completions match */
1249 if (NILP (bestmatch))
1251 matchcount = 1;
1252 bestmatch = eltstring;
1253 bestmatchsize = SCHARS (eltstring);
1255 else
1257 compare = min (bestmatchsize, SCHARS (eltstring));
1258 tem = Fcompare_strings (bestmatch, make_number (0),
1259 make_number (compare),
1260 eltstring, make_number (0),
1261 make_number (compare),
1262 completion_ignore_case ? Qt : Qnil);
1263 if (EQ (tem, Qt))
1264 matchsize = compare;
1265 else if (XINT (tem) < 0)
1266 matchsize = - XINT (tem) - 1;
1267 else
1268 matchsize = XINT (tem) - 1;
1270 if (matchsize < 0)
1271 /* When can this happen ? -stef */
1272 matchsize = compare;
1273 if (completion_ignore_case)
1275 /* If this is an exact match except for case,
1276 use it as the best match rather than one that is not an
1277 exact match. This way, we get the case pattern
1278 of the actual match. */
1279 if ((matchsize == SCHARS (eltstring)
1280 && matchsize < SCHARS (bestmatch))
1282 /* If there is more than one exact match ignoring case,
1283 and one of them is exact including case,
1284 prefer that one. */
1285 /* If there is no exact match ignoring case,
1286 prefer a match that does not change the case
1287 of the input. */
1288 ((matchsize == SCHARS (eltstring))
1290 (matchsize == SCHARS (bestmatch))
1291 && (tem = Fcompare_strings (eltstring, make_number (0),
1292 make_number (SCHARS (string)),
1293 string, make_number (0),
1294 Qnil,
1295 Qnil),
1296 EQ (Qt, tem))
1297 && (tem = Fcompare_strings (bestmatch, make_number (0),
1298 make_number (SCHARS (string)),
1299 string, make_number (0),
1300 Qnil,
1301 Qnil),
1302 ! EQ (Qt, tem))))
1303 bestmatch = eltstring;
1305 if (bestmatchsize != SCHARS (eltstring)
1306 || bestmatchsize != matchsize)
1307 /* Don't count the same string multiple times. */
1308 matchcount++;
1309 bestmatchsize = matchsize;
1310 if (matchsize <= SCHARS (string)
1311 && matchcount > 1)
1312 /* No need to look any further. */
1313 break;
1318 if (NILP (bestmatch))
1319 return Qnil; /* No completions found */
1320 /* If we are ignoring case, and there is no exact match,
1321 and no additional text was supplied,
1322 don't change the case of what the user typed. */
1323 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1324 && SCHARS (bestmatch) > bestmatchsize)
1325 return minibuf_conform_representation (string, bestmatch);
1327 /* Return t if the supplied string is an exact match (counting case);
1328 it does not require any change to be made. */
1329 if (matchcount == 1 && bestmatchsize == SCHARS (string)
1330 && (tem = Fcompare_strings (bestmatch, make_number (0),
1331 make_number (bestmatchsize),
1332 string, make_number (0),
1333 make_number (bestmatchsize),
1334 Qnil),
1335 EQ (Qt, tem)))
1336 return Qt;
1338 XSETFASTINT (zero, 0); /* Else extract the part in which */
1339 XSETFASTINT (end, bestmatchsize); /* all completions agree */
1340 return Fsubstring (bestmatch, zero, end);
1343 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1344 doc: /* Search for partial matches to STRING in ALIST.
1345 Each car of each element of ALIST is tested to see if it begins with STRING.
1346 The value is a list of all the strings from ALIST that match.
1348 If ALIST is a hash-table, all the string keys are the possible matches.
1349 If ALIST is an obarray, the names of all symbols in the obarray
1350 are the possible matches.
1352 ALIST can also be a function to do the completion itself.
1353 It receives three arguments: the values STRING, PREDICATE and t.
1354 Whatever it returns becomes the value of `all-completions'.
1356 If optional third argument PREDICATE is non-nil,
1357 it is used to test each possible match.
1358 The match is a candidate only if PREDICATE returns non-nil.
1359 The argument given to PREDICATE is the alist element
1360 or the symbol from the obarray. If ALIST is a hash-table,
1361 predicate is called with two arguments: the key and the value.
1362 Additionally to this predicate, `completion-regexp-list'
1363 is used to further constrain the set of candidates.
1365 If the optional fourth argument HIDE-SPACES is non-nil,
1366 strings in ALIST that start with a space
1367 are ignored unless STRING itself starts with a space. */)
1368 (string, alist, predicate, hide_spaces)
1369 Lisp_Object string, alist, predicate, hide_spaces;
1371 Lisp_Object tail, elt, eltstring;
1372 Lisp_Object allmatches;
1373 int type = HASH_TABLE_P (alist) ? 3
1374 : VECTORP (alist) ? 2
1375 : NILP (alist) || (CONSP (alist)
1376 && (!SYMBOLP (XCAR (alist))
1377 || NILP (XCAR (alist))));
1378 int index = 0, obsize = 0;
1379 Lisp_Object bucket, tem;
1380 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1382 CHECK_STRING (string);
1383 if (type == 0)
1384 return call3 (alist, string, predicate, Qt);
1385 allmatches = bucket = Qnil;
1387 /* If ALIST is not a list, set TAIL just for gc pro. */
1388 tail = alist;
1389 if (type == 2)
1391 obsize = XVECTOR (alist)->size;
1392 bucket = XVECTOR (alist)->contents[index];
1395 while (1)
1397 /* Get the next element of the alist, obarray, or hash-table. */
1398 /* Exit the loop if the elements are all used up. */
1399 /* elt gets the alist element or symbol.
1400 eltstring gets the name to check as a completion. */
1402 if (type == 1)
1404 if (!CONSP (tail))
1405 break;
1406 elt = XCAR (tail);
1407 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1408 tail = XCDR (tail);
1410 else if (type == 2)
1412 if (XFASTINT (bucket) != 0)
1414 elt = bucket;
1415 eltstring = Fsymbol_name (elt);
1416 if (XSYMBOL (bucket)->next)
1417 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1418 else
1419 XSETFASTINT (bucket, 0);
1421 else if (++index >= obsize)
1422 break;
1423 else
1425 bucket = XVECTOR (alist)->contents[index];
1426 continue;
1429 else /* if (type == 3) */
1431 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1432 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1433 index++;
1434 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1435 break;
1436 else
1437 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1440 /* Is this element a possible completion? */
1442 if (STRINGP (eltstring)
1443 && SCHARS (string) <= SCHARS (eltstring)
1444 /* If HIDE_SPACES, reject alternatives that start with space
1445 unless the input starts with space. */
1446 && ((SBYTES (string) > 0
1447 && SREF (string, 0) == ' ')
1448 || SREF (eltstring, 0) != ' '
1449 || NILP (hide_spaces))
1450 && (tem = Fcompare_strings (eltstring, make_number (0),
1451 make_number (SCHARS (string)),
1452 string, make_number (0),
1453 make_number (SCHARS (string)),
1454 completion_ignore_case ? Qt : Qnil),
1455 EQ (Qt, tem)))
1457 /* Yes. */
1458 Lisp_Object regexps;
1459 Lisp_Object zero;
1460 XSETFASTINT (zero, 0);
1462 /* Ignore this element if it fails to match all the regexps. */
1463 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1464 regexps = XCDR (regexps))
1466 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1467 if (NILP (tem))
1468 break;
1470 if (CONSP (regexps))
1471 continue;
1473 /* Ignore this element if there is a predicate
1474 and the predicate doesn't like it. */
1476 if (!NILP (predicate))
1478 if (EQ (predicate, Qcommandp))
1479 tem = Fcommandp (elt, Qnil);
1480 else
1482 GCPRO4 (tail, eltstring, allmatches, string);
1483 tem = type == 3
1484 ? call2 (predicate, elt,
1485 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1486 : call1 (predicate, elt);
1487 UNGCPRO;
1489 if (NILP (tem)) continue;
1491 /* Ok => put it on the list. */
1492 allmatches = Fcons (eltstring, allmatches);
1496 return Fnreverse (allmatches);
1499 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1500 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1501 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
1502 Lisp_Object Vminibuffer_completing_file_name;
1504 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1505 doc: /* Read a string in the minibuffer, with completion.
1506 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1507 TABLE is an alist whose elements' cars are strings, or an obarray.
1508 TABLE can also be a function to do the completion itself.
1509 PREDICATE limits completion to a subset of TABLE.
1510 See `try-completion' and `all-completions' for more details
1511 on completion, TABLE, and PREDICATE.
1513 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
1514 the input is (or completes to) an element of TABLE or is null.
1515 If it is also not t, typing RET does not exit if it does non-null completion.
1516 If the input is null, `completing-read' returns an empty string,
1517 regardless of the value of REQUIRE-MATCH.
1519 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1520 If it is (STRING . POSITION), the initial input
1521 is STRING, but point is placed POSITION characters into the string.
1522 This feature is deprecated--it is best to pass nil for INITIAL-INPUT
1523 and supply the default value DEF instead. The user can yank the
1524 default value into the minibuffer easily using \\[next-history-element].
1526 HIST, if non-nil, specifies a history list
1527 and optionally the initial position in the list.
1528 It can be a symbol, which is the history list variable to use,
1529 or it can be a cons cell (HISTVAR . HISTPOS).
1530 In that case, HISTVAR is the history list variable to use,
1531 and HISTPOS is the initial position (the position in the list
1532 which INITIAL-INPUT corresponds to).
1533 Positions are counted starting from 1 at the beginning of the list.
1534 DEF, if non-nil, is the default value.
1536 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
1537 the current input method and the setting of `enable-multibyte-characters'.
1539 Completion ignores case if the ambient value of
1540 `completion-ignore-case' is non-nil. */)
1541 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method)
1542 Lisp_Object prompt, table, predicate, require_match, initial_input;
1543 Lisp_Object hist, def, inherit_input_method;
1545 Lisp_Object val, histvar, histpos, position;
1546 Lisp_Object init;
1547 int pos = 0;
1548 int count = SPECPDL_INDEX ();
1549 struct gcpro gcpro1;
1551 init = initial_input;
1552 GCPRO1 (def);
1554 specbind (Qminibuffer_completion_table, table);
1555 specbind (Qminibuffer_completion_predicate, predicate);
1556 specbind (Qminibuffer_completion_confirm,
1557 EQ (require_match, Qt) ? Qnil : require_match);
1558 last_exact_completion = Qnil;
1560 position = Qnil;
1561 if (!NILP (init))
1563 if (CONSP (init))
1565 position = Fcdr (init);
1566 init = Fcar (init);
1568 CHECK_STRING (init);
1569 if (!NILP (position))
1571 CHECK_NUMBER (position);
1572 /* Convert to distance from end of input. */
1573 pos = XINT (position) - SCHARS (init);
1577 if (SYMBOLP (hist))
1579 histvar = hist;
1580 histpos = Qnil;
1582 else
1584 histvar = Fcar_safe (hist);
1585 histpos = Fcdr_safe (hist);
1587 if (NILP (histvar))
1588 histvar = Qminibuffer_history;
1589 if (NILP (histpos))
1590 XSETFASTINT (histpos, 0);
1592 val = read_minibuf (NILP (require_match)
1593 ? Vminibuffer_local_completion_map
1594 : Vminibuffer_local_must_match_map,
1595 init, prompt, make_number (pos), 0,
1596 histvar, histpos, def, 0,
1597 !NILP (inherit_input_method));
1599 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
1600 val = def;
1602 RETURN_UNGCPRO (unbind_to (count, val));
1605 Lisp_Object Fminibuffer_completion_help ();
1606 Lisp_Object Fassoc_string ();
1608 /* Test whether TXT is an exact completion. */
1609 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1610 doc: /* Return non-nil if STRING is a valid completion.
1611 Takes the same arguments as `all-completions' and `try-completion'.
1612 If ALIST is a function, it is called with three arguments:
1613 the values STRING, PREDICATE and `lambda'. */)
1614 (string, alist, predicate)
1615 Lisp_Object string, alist, predicate;
1617 Lisp_Object regexps, tem = Qnil;
1618 int i = 0;
1620 CHECK_STRING (string);
1622 if ((CONSP (alist) && (!SYMBOLP (XCAR (alist)) || NILP (XCAR (alist))))
1623 || NILP (alist))
1625 tem = Fassoc_string (string, alist, completion_ignore_case ? Qt : Qnil);
1626 if NILP (tem)
1627 return Qnil;
1629 else if (VECTORP (alist))
1631 /* Bypass intern-soft as that loses for nil. */
1632 tem = oblookup (alist,
1633 SDATA (string),
1634 SCHARS (string),
1635 SBYTES (string));
1636 if (!SYMBOLP (tem))
1638 if (STRING_MULTIBYTE (string))
1639 string = Fstring_make_unibyte (string);
1640 else
1641 string = Fstring_make_multibyte (string);
1643 tem = oblookup (Vminibuffer_completion_table,
1644 SDATA (string),
1645 SCHARS (string),
1646 SBYTES (string));
1647 if (!SYMBOLP (tem))
1648 return Qnil;
1651 else if (HASH_TABLE_P (alist))
1653 i = hash_lookup (XHASH_TABLE (alist), string, NULL);
1654 if (i >= 0)
1655 tem = HASH_KEY (XHASH_TABLE (alist), i);
1656 else
1657 return Qnil;
1659 else
1660 return call3 (alist, string, predicate, Qlambda);
1662 /* Reject this element if it fails to match all the regexps. */
1663 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1664 regexps = XCDR (regexps))
1666 if (NILP (Fstring_match (XCAR (regexps),
1667 SYMBOLP (tem) ? string : tem,
1668 Qnil)))
1669 return Qnil;
1672 /* Finally, check the predicate. */
1673 if (!NILP (predicate))
1674 return HASH_TABLE_P (alist)
1675 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (alist), i))
1676 : call1 (predicate, tem);
1677 else
1678 return Qt;
1681 /* returns:
1682 * 0 no possible completion
1683 * 1 was already an exact and unique completion
1684 * 3 was already an exact completion
1685 * 4 completed to an exact completion
1686 * 5 some completion happened
1687 * 6 no completion happened
1690 do_completion ()
1692 Lisp_Object completion, string, tem;
1693 int completedp;
1694 Lisp_Object last;
1695 struct gcpro gcpro1, gcpro2;
1697 completion = Ftry_completion (minibuffer_completion_contents (),
1698 Vminibuffer_completion_table,
1699 Vminibuffer_completion_predicate);
1700 last = last_exact_completion;
1701 last_exact_completion = Qnil;
1703 GCPRO2 (completion, last);
1705 if (NILP (completion))
1707 bitch_at_user ();
1708 temp_echo_area_glyphs (" [No match]");
1709 UNGCPRO;
1710 return 0;
1713 if (EQ (completion, Qt)) /* exact and unique match */
1715 UNGCPRO;
1716 return 1;
1719 string = minibuffer_completion_contents ();
1721 /* COMPLETEDP should be true if some completion was done, which
1722 doesn't include simply changing the case of the entered string.
1723 However, for appearance, the string is rewritten if the case
1724 changes. */
1725 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qt);
1726 completedp = !EQ (tem, Qt);
1728 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qnil);
1729 if (!EQ (tem, Qt))
1730 /* Rewrite the user's input. */
1732 int prompt_end = XINT (Fminibuffer_prompt_end ());
1733 /* Some completion happened */
1735 if (! NILP (Vminibuffer_completing_file_name)
1736 && SREF (completion, SBYTES (completion) - 1) == '/'
1737 && PT < ZV
1738 && FETCH_CHAR (PT_BYTE) == '/')
1740 del_range (prompt_end, PT + 1);
1742 else
1743 del_range (prompt_end, PT);
1745 Finsert (1, &completion);
1747 if (! completedp)
1748 /* The case of the string changed, but that's all. We're not
1749 sure whether this is a unique completion or not, so try again
1750 using the real case (this shouldn't recurse again, because
1751 the next time try-completion will return either `t' or the
1752 exact string). */
1754 UNGCPRO;
1755 return do_completion ();
1759 /* It did find a match. Do we match some possibility exactly now? */
1760 tem = Ftest_completion (Fminibuffer_contents (),
1761 Vminibuffer_completion_table,
1762 Vminibuffer_completion_predicate);
1763 if (NILP (tem))
1765 /* not an exact match */
1766 UNGCPRO;
1767 if (completedp)
1768 return 5;
1769 else if (!NILP (Vcompletion_auto_help))
1770 Fminibuffer_completion_help ();
1771 else
1772 temp_echo_area_glyphs (" [Next char not unique]");
1773 return 6;
1775 else if (completedp)
1777 UNGCPRO;
1778 return 4;
1780 /* If the last exact completion and this one were the same,
1781 it means we've already given a "Complete but not unique"
1782 message and the user's hit TAB again, so now we give him help. */
1783 last_exact_completion = completion;
1784 if (!NILP (last))
1786 tem = minibuffer_completion_contents ();
1787 if (!NILP (Fequal (tem, last)))
1788 Fminibuffer_completion_help ();
1790 UNGCPRO;
1791 return 3;
1794 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1796 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
1797 doc: /* Like `assoc' but specifically for strings.
1798 Unibyte strings are converted to multibyte for comparison.
1799 And case is ignored if CASE-FOLD is non-nil.
1800 As opposed to `assoc', it will also match an entry consisting of a single
1801 string rather than a cons cell whose car is a string. */)
1802 (key, list, case_fold)
1803 register Lisp_Object key;
1804 Lisp_Object list, case_fold;
1806 register Lisp_Object tail;
1808 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1810 register Lisp_Object elt, tem, thiscar;
1811 elt = Fcar (tail);
1812 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1813 if (!STRINGP (thiscar))
1814 continue;
1815 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1816 key, make_number (0), Qnil,
1817 case_fold);
1818 if (EQ (tem, Qt))
1819 return elt;
1820 QUIT;
1822 return Qnil;
1825 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1826 doc: /* Complete the minibuffer contents as far as possible.
1827 Return nil if there is no valid completion, else t.
1828 If no characters can be completed, display a list of possible completions.
1829 If you repeat this command after it displayed such a list,
1830 scroll the window of possible completions. */)
1833 register int i;
1834 Lisp_Object window, tem;
1836 /* If the previous command was not this,
1837 mark the completion buffer obsolete. */
1838 if (! EQ (current_kboard->Vlast_command, Vthis_command))
1839 Vminibuf_scroll_window = Qnil;
1841 window = Vminibuf_scroll_window;
1842 /* If there's a fresh completion window with a live buffer,
1843 and this command is repeated, scroll that window. */
1844 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1845 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1847 struct buffer *obuf = current_buffer;
1849 Fset_buffer (XWINDOW (window)->buffer);
1850 tem = Fpos_visible_in_window_p (make_number (ZV), window, Qnil);
1851 if (! NILP (tem))
1852 /* If end is in view, scroll up to the beginning. */
1853 Fset_window_start (window, make_number (BEGV), Qnil);
1854 else
1855 /* Else scroll down one screen. */
1856 Fscroll_other_window (Qnil);
1858 set_buffer_internal (obuf);
1859 return Qnil;
1862 i = do_completion ();
1863 switch (i)
1865 case 0:
1866 return Qnil;
1868 case 1:
1869 if (PT != ZV)
1870 Fgoto_char (make_number (ZV));
1871 temp_echo_area_glyphs (" [Sole completion]");
1872 break;
1874 case 3:
1875 if (PT != ZV)
1876 Fgoto_char (make_number (ZV));
1877 temp_echo_area_glyphs (" [Complete, but not unique]");
1878 break;
1881 return Qt;
1884 /* Subroutines of Fminibuffer_complete_and_exit. */
1886 /* This one is called by internal_condition_case to do the real work. */
1888 Lisp_Object
1889 complete_and_exit_1 ()
1891 return make_number (do_completion ());
1894 /* This one is called by internal_condition_case if an error happens.
1895 Pretend the current value is an exact match. */
1897 Lisp_Object
1898 complete_and_exit_2 (ignore)
1899 Lisp_Object ignore;
1901 return make_number (1);
1904 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1905 Sminibuffer_complete_and_exit, 0, 0, "",
1906 doc: /* If the minibuffer contents is a valid completion then exit.
1907 Otherwise try to complete it. If completion leads to a valid completion,
1908 a repetition of this command will exit. */)
1911 register int i;
1912 Lisp_Object val;
1914 /* Allow user to specify null string */
1915 if (XINT (Fminibuffer_prompt_end ()) == ZV)
1916 goto exit;
1918 if (!NILP (Ftest_completion (Fminibuffer_contents (),
1919 Vminibuffer_completion_table,
1920 Vminibuffer_completion_predicate)))
1921 goto exit;
1923 /* Call do_completion, but ignore errors. */
1924 SET_PT (ZV);
1925 val = internal_condition_case (complete_and_exit_1, Qerror,
1926 complete_and_exit_2);
1928 i = XFASTINT (val);
1929 switch (i)
1931 case 1:
1932 case 3:
1933 goto exit;
1935 case 4:
1936 if (!NILP (Vminibuffer_completion_confirm))
1938 temp_echo_area_glyphs (" [Confirm]");
1939 return Qnil;
1941 else
1942 goto exit;
1944 default:
1945 return Qnil;
1947 exit:
1948 return Fthrow (Qexit, Qnil);
1949 /* NOTREACHED */
1952 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1953 0, 0, "",
1954 doc: /* Complete the minibuffer contents at most a single word.
1955 After one word is completed as much as possible, a space or hyphen
1956 is added, provided that matches some possible completion.
1957 Return nil if there is no valid completion, else t. */)
1960 Lisp_Object completion, tem, tem1;
1961 register int i, i_byte;
1962 register const unsigned char *completion_string;
1963 struct gcpro gcpro1, gcpro2;
1964 int prompt_end_charpos = XINT (Fminibuffer_prompt_end ());
1966 /* We keep calling Fbuffer_string rather than arrange for GC to
1967 hold onto a pointer to one of the strings thus made. */
1969 completion = Ftry_completion (minibuffer_completion_contents (),
1970 Vminibuffer_completion_table,
1971 Vminibuffer_completion_predicate);
1972 if (NILP (completion))
1974 bitch_at_user ();
1975 temp_echo_area_glyphs (" [No match]");
1976 return Qnil;
1978 if (EQ (completion, Qt))
1979 return Qnil;
1981 #if 0 /* How the below code used to look, for reference. */
1982 tem = Fminibuffer_contents ();
1983 b = SDATA (tem);
1984 i = ZV - 1 - SCHARS (completion);
1985 p = SDATA (completion);
1986 if (i > 0 ||
1987 0 <= scmp (b, p, ZV - 1))
1989 i = 1;
1990 /* Set buffer to longest match of buffer tail and completion head. */
1991 while (0 <= scmp (b + i, p, ZV - 1 - i))
1992 i++;
1993 del_range (1, i + 1);
1994 SET_PT (ZV);
1996 #else /* Rewritten code */
1998 int buffer_nchars, completion_nchars;
2000 CHECK_STRING (completion);
2001 tem = minibuffer_completion_contents ();
2002 GCPRO2 (completion, tem);
2003 /* If reading a file name,
2004 expand any $ENVVAR refs in the buffer and in TEM. */
2005 if (! NILP (Vminibuffer_completing_file_name))
2007 Lisp_Object substituted;
2008 substituted = Fsubstitute_in_file_name (tem);
2009 if (! EQ (substituted, tem))
2011 tem = substituted;
2012 del_range (prompt_end_charpos, PT);
2013 Finsert (1, &tem);
2016 buffer_nchars = SCHARS (tem); /* # chars in what we completed. */
2017 completion_nchars = SCHARS (completion);
2018 i = buffer_nchars - completion_nchars;
2019 if (i > 0
2021 (tem1 = Fcompare_strings (tem, make_number (0),
2022 make_number (buffer_nchars),
2023 completion, make_number (0),
2024 make_number (buffer_nchars),
2025 completion_ignore_case ? Qt : Qnil),
2026 ! EQ (tem1, Qt)))
2028 int start_pos;
2030 /* Make buffer (before point) contain the longest match
2031 of TEM's tail and COMPLETION's head. */
2032 if (i <= 0) i = 1;
2033 start_pos= i;
2034 buffer_nchars -= i;
2035 while (i > 0)
2037 tem1 = Fcompare_strings (tem, make_number (start_pos), Qnil,
2038 completion, make_number (0),
2039 make_number (buffer_nchars),
2040 completion_ignore_case ? Qt : Qnil);
2041 start_pos++;
2042 if (EQ (tem1, Qt))
2043 break;
2044 i++;
2045 buffer_nchars--;
2047 del_range (1, i + 1);
2049 UNGCPRO;
2051 #endif /* Rewritten code */
2054 int prompt_end_bytepos;
2055 prompt_end_bytepos = CHAR_TO_BYTE (prompt_end_charpos);
2056 i = PT - prompt_end_charpos;
2057 i_byte = PT_BYTE - prompt_end_bytepos;
2060 /* If completion finds next char not unique,
2061 consider adding a space or a hyphen. */
2062 if (i == SCHARS (completion))
2064 GCPRO1 (completion);
2065 tem = Ftry_completion (concat2 (minibuffer_completion_contents (),
2066 build_string (" ")),
2067 Vminibuffer_completion_table,
2068 Vminibuffer_completion_predicate);
2069 UNGCPRO;
2071 if (STRINGP (tem))
2072 completion = tem;
2073 else
2075 GCPRO1 (completion);
2076 tem =
2077 Ftry_completion (concat2 (minibuffer_completion_contents (),
2078 build_string ("-")),
2079 Vminibuffer_completion_table,
2080 Vminibuffer_completion_predicate);
2081 UNGCPRO;
2083 if (STRINGP (tem))
2084 completion = tem;
2088 /* Now find first word-break in the stuff found by completion.
2089 i gets index in string of where to stop completing. */
2091 int len, c;
2092 int bytes = SBYTES (completion);
2093 completion_string = SDATA (completion);
2094 for (; i_byte < SBYTES (completion); i_byte += len, i++)
2096 c = STRING_CHAR_AND_LENGTH (completion_string + i_byte,
2097 bytes - i_byte,
2098 len);
2099 if (SYNTAX (c) != Sword)
2101 i_byte += len;
2102 i++;
2103 break;
2108 /* If got no characters, print help for user. */
2110 if (i == PT - prompt_end_charpos)
2112 if (!NILP (Vcompletion_auto_help))
2113 Fminibuffer_completion_help ();
2114 return Qnil;
2117 /* Otherwise insert in minibuffer the chars we got */
2119 if (! NILP (Vminibuffer_completing_file_name)
2120 && SREF (completion, SBYTES (completion) - 1) == '/'
2121 && PT < ZV
2122 && FETCH_CHAR (PT_BYTE) == '/')
2124 del_range (prompt_end_charpos, PT + 1);
2126 else
2127 del_range (prompt_end_charpos, PT);
2129 insert_from_string (completion, 0, 0, i, i_byte, 1);
2130 return Qt;
2133 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
2134 1, 1, 0,
2135 doc: /* Display the list of completions, COMPLETIONS, using `standard-output'.
2136 Each element may be just a symbol or string
2137 or may be a list of two strings to be printed as if concatenated.
2138 `standard-output' must be a buffer.
2139 The actual completion alternatives, as inserted, are given `mouse-face'
2140 properties of `highlight'.
2141 At the end, this runs the normal hook `completion-setup-hook'.
2142 It can find the completion buffer in `standard-output'. */)
2143 (completions)
2144 Lisp_Object completions;
2146 Lisp_Object tail, elt;
2147 register int i;
2148 int column = 0;
2149 struct gcpro gcpro1, gcpro2;
2150 struct buffer *old = current_buffer;
2151 int first = 1;
2153 /* Note that (when it matters) every variable
2154 points to a non-string that is pointed to by COMPLETIONS,
2155 except for ELT. ELT can be pointing to a string
2156 when terpri or Findent_to calls a change hook. */
2157 elt = Qnil;
2158 GCPRO2 (completions, elt);
2160 if (BUFFERP (Vstandard_output))
2161 set_buffer_internal (XBUFFER (Vstandard_output));
2163 if (NILP (completions))
2164 write_string ("There are no possible completions of what you have typed.",
2165 -1);
2166 else
2168 write_string ("Possible completions are:", -1);
2169 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
2171 Lisp_Object tem, string;
2172 int length;
2173 Lisp_Object startpos, endpos;
2175 startpos = Qnil;
2177 elt = Fcar (tail);
2178 /* Compute the length of this element. */
2179 if (CONSP (elt))
2181 tem = XCAR (elt);
2182 CHECK_STRING (tem);
2183 length = SCHARS (tem);
2185 tem = Fcar (XCDR (elt));
2186 CHECK_STRING (tem);
2187 length += SCHARS (tem);
2189 else
2191 CHECK_STRING (elt);
2192 length = SCHARS (elt);
2195 /* This does a bad job for narrower than usual windows.
2196 Sadly, the window it will appear in is not known
2197 until after the text has been made. */
2199 if (BUFFERP (Vstandard_output))
2200 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2202 /* If the previous completion was very wide,
2203 or we have two on this line already,
2204 don't put another on the same line. */
2205 if (column > 33 || first
2206 /* If this is really wide, don't put it second on a line. */
2207 || (column > 0 && length > 45))
2209 Fterpri (Qnil);
2210 column = 0;
2212 /* Otherwise advance to column 35. */
2213 else
2215 if (BUFFERP (Vstandard_output))
2217 tem = Findent_to (make_number (35), make_number (2));
2219 column = XINT (tem);
2221 else
2225 write_string (" ", -1);
2226 column++;
2228 while (column < 35);
2232 if (BUFFERP (Vstandard_output))
2234 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2235 Fset_text_properties (startpos, endpos,
2236 Qnil, Vstandard_output);
2239 /* Output this element.
2240 If necessary, convert it to unibyte or to multibyte first. */
2241 if (CONSP (elt))
2242 string = Fcar (elt);
2243 else
2244 string = elt;
2245 if (NILP (current_buffer->enable_multibyte_characters)
2246 && STRING_MULTIBYTE (string))
2247 string = Fstring_make_unibyte (string);
2248 else if (!NILP (current_buffer->enable_multibyte_characters)
2249 && !STRING_MULTIBYTE (string))
2250 string = Fstring_make_multibyte (string);
2252 if (BUFFERP (Vstandard_output))
2254 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2256 Fprinc (string, Qnil);
2258 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2260 Fput_text_property (startpos, endpos,
2261 Qmouse_face, intern ("highlight"),
2262 Vstandard_output);
2264 else
2266 Fprinc (string, Qnil);
2269 /* Output the annotation for this element. */
2270 if (CONSP (elt))
2272 if (BUFFERP (Vstandard_output))
2274 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2276 Fprinc (Fcar (Fcdr (elt)), Qnil);
2278 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2280 Fset_text_properties (startpos, endpos, Qnil,
2281 Vstandard_output);
2283 else
2285 Fprinc (Fcar (Fcdr (elt)), Qnil);
2290 /* Update COLUMN for what we have output. */
2291 column += length;
2293 /* If output is to a buffer, recompute COLUMN in a way
2294 that takes account of character widths. */
2295 if (BUFFERP (Vstandard_output))
2297 tem = Fcurrent_column ();
2298 column = XINT (tem);
2301 first = 0;
2305 UNGCPRO;
2307 if (BUFFERP (Vstandard_output))
2308 set_buffer_internal (old);
2310 if (!NILP (Vrun_hooks))
2311 call1 (Vrun_hooks, intern ("completion-setup-hook"));
2313 return Qnil;
2316 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
2317 0, 0, "",
2318 doc: /* Display a list of possible completions of the current minibuffer contents. */)
2321 Lisp_Object completions;
2323 message ("Making completion list...");
2324 completions = Fall_completions (minibuffer_completion_contents (),
2325 Vminibuffer_completion_table,
2326 Vminibuffer_completion_predicate,
2327 Qt);
2328 clear_message (1, 0);
2330 if (NILP (completions))
2332 bitch_at_user ();
2333 temp_echo_area_glyphs (" [No completions]");
2335 else
2336 internal_with_output_to_temp_buffer ("*Completions*",
2337 Fdisplay_completion_list,
2338 Fsort (completions, Qstring_lessp));
2339 return Qnil;
2342 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
2343 doc: /* Terminate minibuffer input. */)
2346 if (INTEGERP (last_command_char))
2347 internal_self_insert (XINT (last_command_char), 0);
2348 else
2349 bitch_at_user ();
2351 return Fthrow (Qexit, Qnil);
2354 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
2355 doc: /* Terminate this minibuffer argument. */)
2358 return Fthrow (Qexit, Qnil);
2361 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
2362 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
2365 return make_number (minibuf_level);
2368 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
2369 doc: /* Return the prompt string of the currently-active minibuffer.
2370 If no minibuffer is active, return nil. */)
2373 return Fcopy_sequence (minibuf_prompt);
2377 /* Temporarily display the string M at the end of the current
2378 minibuffer contents. This is used to display things like
2379 "[No Match]" when the user requests a completion for a prefix
2380 that has no possible completions, and other quick, unobtrusive
2381 messages. */
2383 void
2384 temp_echo_area_glyphs (m)
2385 const char *m;
2387 int osize = ZV;
2388 int osize_byte = ZV_BYTE;
2389 int opoint = PT;
2390 int opoint_byte = PT_BYTE;
2391 Lisp_Object oinhibit;
2392 oinhibit = Vinhibit_quit;
2394 /* Clear out any old echo-area message to make way for our new thing. */
2395 message (0);
2397 SET_PT_BOTH (osize, osize_byte);
2398 insert_string (m);
2399 SET_PT_BOTH (opoint, opoint_byte);
2400 Vinhibit_quit = Qt;
2401 Fsit_for (make_number (2), Qnil, Qnil);
2402 del_range_both (osize, osize_byte, ZV, ZV_BYTE, 1);
2403 SET_PT_BOTH (opoint, opoint_byte);
2404 if (!NILP (Vquit_flag))
2406 Vquit_flag = Qnil;
2407 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
2409 Vinhibit_quit = oinhibit;
2412 DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
2413 1, 1, 0,
2414 doc: /* Temporarily display STRING at the end of the minibuffer.
2415 The text is displayed for two seconds,
2416 or until the next input event arrives, whichever comes first. */)
2417 (string)
2418 Lisp_Object string;
2420 temp_echo_area_glyphs (SDATA (string));
2421 return Qnil;
2424 void
2425 init_minibuf_once ()
2427 Vminibuffer_list = Qnil;
2428 staticpro (&Vminibuffer_list);
2431 void
2432 syms_of_minibuf ()
2434 minibuf_level = 0;
2435 minibuf_prompt = Qnil;
2436 staticpro (&minibuf_prompt);
2438 minibuf_save_list = Qnil;
2439 staticpro (&minibuf_save_list);
2441 Qread_file_name_internal = intern ("read-file-name-internal");
2442 staticpro (&Qread_file_name_internal);
2444 Qminibuffer_default = intern ("minibuffer-default");
2445 staticpro (&Qminibuffer_default);
2446 Fset (Qminibuffer_default, Qnil);
2448 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
2449 staticpro (&Qminibuffer_completion_table);
2451 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
2452 staticpro (&Qminibuffer_completion_confirm);
2454 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
2455 staticpro (&Qminibuffer_completion_predicate);
2457 staticpro (&last_exact_completion);
2458 last_exact_completion = Qnil;
2460 staticpro (&last_minibuf_string);
2461 last_minibuf_string = Qnil;
2463 Quser_variable_p = intern ("user-variable-p");
2464 staticpro (&Quser_variable_p);
2466 Qminibuffer_history = intern ("minibuffer-history");
2467 staticpro (&Qminibuffer_history);
2469 Qbuffer_name_history = intern ("buffer-name-history");
2470 staticpro (&Qbuffer_name_history);
2471 Fset (Qbuffer_name_history, Qnil);
2473 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
2474 staticpro (&Qminibuffer_setup_hook);
2476 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
2477 staticpro (&Qminibuffer_exit_hook);
2479 Qhistory_length = intern ("history-length");
2480 staticpro (&Qhistory_length);
2482 Qcurrent_input_method = intern ("current-input-method");
2483 staticpro (&Qcurrent_input_method);
2485 Qactivate_input_method = intern ("activate-input-method");
2486 staticpro (&Qactivate_input_method);
2488 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2489 doc: /* If this is non-nil, `read-buffer' does its work by calling this function. */);
2490 Vread_buffer_function = Qnil;
2492 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2493 doc: /* Normal hook run just after entry to minibuffer. */);
2494 Vminibuffer_setup_hook = Qnil;
2496 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2497 doc: /* Normal hook run just after exit from minibuffer. */);
2498 Vminibuffer_exit_hook = Qnil;
2500 DEFVAR_LISP ("history-length", &Vhistory_length,
2501 doc: /* *Maximum length for history lists before truncation takes place.
2502 A number means that length; t means infinite. Truncation takes place
2503 just after a new element is inserted. Setting the history-length
2504 property of a history variable overrides this default. */);
2505 XSETFASTINT (Vhistory_length, 30);
2507 DEFVAR_LISP ("completion-auto-help", &Vcompletion_auto_help,
2508 doc: /* *Non-nil means automatically provide help for invalid completion input. */);
2509 Vcompletion_auto_help = Qt;
2511 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
2512 doc: /* Non-nil means don't consider case significant in completion. */);
2513 completion_ignore_case = 0;
2515 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2516 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
2517 This variable makes a difference whenever the minibuffer window is active. */);
2518 enable_recursive_minibuffers = 0;
2520 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2521 doc: /* Alist or obarray used for completion in the minibuffer.
2522 This becomes the ALIST argument to `try-completion' and `all-completion'.
2524 The value may alternatively be a function, which is given three arguments:
2525 STRING, the current buffer contents;
2526 PREDICATE, the predicate for filtering possible matches;
2527 CODE, which says what kind of things to do.
2528 CODE can be nil, t or `lambda'.
2529 nil means to return the best completion of STRING, or nil if there is none.
2530 t means to return a list of all possible completions of STRING.
2531 `lambda' means to return t if STRING is a valid completion as it stands. */);
2532 Vminibuffer_completion_table = Qnil;
2534 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2535 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
2536 Vminibuffer_completion_predicate = Qnil;
2538 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
2539 doc: /* Non-nil means to demand confirmation of completion before exiting minibuffer. */);
2540 Vminibuffer_completion_confirm = Qnil;
2542 DEFVAR_LISP ("minibuffer-completing-file-name",
2543 &Vminibuffer_completing_file_name,
2544 doc: /* Non-nil means completing file names. */);
2545 Vminibuffer_completing_file_name = Qnil;
2547 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2548 doc: /* Value that `help-form' takes on inside the minibuffer. */);
2549 Vminibuffer_help_form = Qnil;
2551 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2552 doc: /* History list symbol to add minibuffer values to.
2553 Each string of minibuffer input, as it appears on exit from the minibuffer,
2554 is added with
2555 (set minibuffer-history-variable
2556 (cons STRING (symbol-value minibuffer-history-variable))) */);
2557 XSETFASTINT (Vminibuffer_history_variable, 0);
2559 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2560 doc: /* Current position of redoing in the history list. */);
2561 Vminibuffer_history_position = Qnil;
2563 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2564 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
2565 Some uses of the echo area also raise that frame (since they use it too). */);
2566 minibuffer_auto_raise = 0;
2568 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
2569 doc: /* List of regexps that should restrict possible completions. */);
2570 Vcompletion_regexp_list = Qnil;
2572 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2573 &minibuffer_allow_text_properties,
2574 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
2575 This also affects `read-string', but it does not affect `read-minibuffer',
2576 `read-no-blanks-input', or any of the functions that do minibuffer input
2577 with completion; they always discard text properties. */);
2578 minibuffer_allow_text_properties = 0;
2580 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2581 doc: /* Text properties that are added to minibuffer prompts.
2582 These are in addition to the basic `field' property, and stickiness
2583 properties. */);
2584 /* We use `intern' here instead of Qread_only to avoid
2585 initialization-order problems. */
2586 Vminibuffer_prompt_properties
2587 = Fcons (intern ("read-only"), Fcons (Qt, Qnil));
2589 defsubr (&Sset_minibuffer_window);
2590 defsubr (&Sread_from_minibuffer);
2591 defsubr (&Seval_minibuffer);
2592 defsubr (&Sread_minibuffer);
2593 defsubr (&Sread_string);
2594 defsubr (&Sread_command);
2595 defsubr (&Sread_variable);
2596 defsubr (&Sread_buffer);
2597 defsubr (&Sread_no_blanks_input);
2598 defsubr (&Sminibuffer_depth);
2599 defsubr (&Sminibuffer_prompt);
2601 defsubr (&Sminibufferp);
2602 defsubr (&Sminibuffer_prompt_end);
2603 defsubr (&Sminibuffer_contents);
2604 defsubr (&Sminibuffer_contents_no_properties);
2605 defsubr (&Sdelete_minibuffer_contents);
2607 defsubr (&Stry_completion);
2608 defsubr (&Sall_completions);
2609 defsubr (&Stest_completion);
2610 defsubr (&Sassoc_string);
2611 defsubr (&Scompleting_read);
2612 defsubr (&Sminibuffer_complete);
2613 defsubr (&Sminibuffer_complete_word);
2614 defsubr (&Sminibuffer_complete_and_exit);
2615 defsubr (&Sdisplay_completion_list);
2616 defsubr (&Sminibuffer_completion_help);
2618 defsubr (&Sself_insert_and_exit);
2619 defsubr (&Sexit_minibuffer);
2621 defsubr (&Sminibuffer_message);
2624 void
2625 keys_of_minibuf ()
2627 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
2628 "abort-recursive-edit");
2629 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
2630 "exit-minibuffer");
2631 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
2632 "exit-minibuffer");
2634 initial_define_key (Vminibuffer_local_ns_map, ' ',
2635 "exit-minibuffer");
2636 initial_define_key (Vminibuffer_local_ns_map, '\t',
2637 "exit-minibuffer");
2638 initial_define_key (Vminibuffer_local_ns_map, '?',
2639 "self-insert-and-exit");
2641 initial_define_key (Vminibuffer_local_completion_map, '\t',
2642 "minibuffer-complete");
2643 initial_define_key (Vminibuffer_local_completion_map, ' ',
2644 "minibuffer-complete-word");
2645 initial_define_key (Vminibuffer_local_completion_map, '?',
2646 "minibuffer-completion-help");
2648 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
2649 "minibuffer-complete-and-exit");
2650 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
2651 "minibuffer-complete-and-exit");