Entry that should have been made last month.
[emacs.git] / src / minibuf.c
blobb0b3cdaa487e7cf1fabaac45dc571cc066263b97
1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2003 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, Qnil);
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, 1, 0,
323 doc: /* Return t if BUFFER is a minibuffer.
324 No argument or nil as argument means use current buffer as BUFFER.*/)
325 (buffer)
326 Lisp_Object buffer;
328 Lisp_Object tem;
330 if (NILP (buffer))
331 buffer = Fcurrent_buffer ();
332 else if (STRINGP (buffer))
333 buffer = Fget_buffer (buffer);
334 else
335 CHECK_BUFFER (buffer);
337 tem = Fmemq (buffer, Vminibuffer_list);
338 return ! NILP (tem) ? Qt : Qnil;
341 DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
342 Sminibuffer_prompt_end, 0, 0, 0,
343 doc: /* Return the buffer position of the end of the minibuffer prompt.
344 Return (point-min) if current buffer is not a mini-buffer. */)
347 /* This function is written to be most efficient when there's a prompt. */
348 Lisp_Object beg, end, tem;
349 beg = make_number (BEGV);
351 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
352 if (NILP (tem))
353 return beg;
355 end = Ffield_end (beg, Qnil, Qnil);
357 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
358 return beg;
359 else
360 return end;
363 DEFUN ("minibuffer-contents", Fminibuffer_contents,
364 Sminibuffer_contents, 0, 0, 0,
365 doc: /* Return the user input in a minibuffer as a string.
366 The current buffer must be a minibuffer. */)
369 int prompt_end = XINT (Fminibuffer_prompt_end ());
370 return make_buffer_string (prompt_end, ZV, 1);
373 DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
374 Sminibuffer_contents_no_properties, 0, 0, 0,
375 doc: /* Return the user input in a minibuffer as a string, without text-properties.
376 The current buffer must be a minibuffer. */)
379 int prompt_end = XINT (Fminibuffer_prompt_end ());
380 return make_buffer_string (prompt_end, ZV, 0);
383 DEFUN ("delete-minibuffer-contents", Fdelete_minibuffer_contents,
384 Sdelete_minibuffer_contents, 0, 0, 0,
385 doc: /* Delete all user input in a minibuffer.
386 The current buffer must be a minibuffer. */)
389 int prompt_end = XINT (Fminibuffer_prompt_end ());
390 if (prompt_end < ZV)
391 del_range (prompt_end, ZV);
392 return Qnil;
395 /* Get the text in the minibuffer before point.
396 That is what completion commands operate on. */
398 Lisp_Object
399 minibuffer_completion_contents ()
401 int prompt_end = XINT (Fminibuffer_prompt_end ());
402 if (PT < prompt_end)
403 error ("Cannot do completion in the prompt");
404 return make_buffer_string (prompt_end, PT, 1);
407 /* Read from the minibuffer using keymap MAP, initial contents INITIAL
408 (a string), putting point minus BACKUP_N bytes from the end of INITIAL,
409 prompting with PROMPT (a string), using history list HISTVAR
410 with initial position HISTPOS. (BACKUP_N should be <= 0.)
412 Normally return the result as a string (the text that was read),
413 but if EXPFLAG is nonzero, read it and return the object read.
414 If HISTVAR is given, save the value read on that history only if it doesn't
415 match the front of that history list exactly. The value is pushed onto
416 the list as the string that was read.
418 DEFALT specifies the default value for the sake of history commands.
420 If ALLOW_PROPS is nonzero, we do not throw away text properties.
422 if INHERIT_INPUT_METHOD is nonzeor, the minibuffer inherit the
423 current input method. */
425 static Lisp_Object
426 read_minibuf (map, initial, prompt, backup_n, expflag,
427 histvar, histpos, defalt, allow_props, inherit_input_method)
428 Lisp_Object map;
429 Lisp_Object initial;
430 Lisp_Object prompt;
431 Lisp_Object backup_n;
432 int expflag;
433 Lisp_Object histvar;
434 Lisp_Object histpos;
435 Lisp_Object defalt;
436 int allow_props;
437 int inherit_input_method;
439 Lisp_Object val;
440 int count = SPECPDL_INDEX ();
441 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
442 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
443 Lisp_Object enable_multibyte;
445 /* String to add to the history. */
446 Lisp_Object histstring;
448 extern Lisp_Object Qfront_sticky;
449 extern Lisp_Object Qrear_nonsticky;
451 specbind (Qminibuffer_default, defalt);
453 single_kboard_state ();
454 #ifdef HAVE_X_WINDOWS
455 if (display_hourglass_p)
456 cancel_hourglass ();
457 #endif
459 val = Qnil;
460 ambient_dir = current_buffer->directory;
461 input_method = Qnil;
462 enable_multibyte = Qnil;
464 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
465 store them away before we can GC. Don't need to protect
466 BACKUP_N because we use the value only if it is an integer. */
467 GCPRO5 (map, initial, val, ambient_dir, input_method);
469 if (!STRINGP (prompt))
470 prompt = empty_string;
472 if (!enable_recursive_minibuffers
473 && minibuf_level > 0)
475 if (EQ (selected_window, minibuf_window))
476 error ("Command attempted to use minibuffer while in minibuffer");
477 else
478 /* If we're in another window, cancel the minibuffer that's active. */
479 Fthrow (Qexit,
480 build_string ("Command attempted to use minibuffer while in minibuffer"));
483 if (noninteractive)
485 val = read_minibuf_noninteractive (map, initial, prompt, backup_n,
486 expflag, histvar, histpos, defalt,
487 allow_props, inherit_input_method);
488 return unbind_to (count, val);
491 /* Choose the minibuffer window and frame, and take action on them. */
493 choose_minibuf_frame ();
495 record_unwind_protect (choose_minibuf_frame_1, Qnil);
497 record_unwind_protect (Fset_window_configuration,
498 Fcurrent_window_configuration (Qnil));
500 /* If the minibuffer window is on a different frame, save that
501 frame's configuration too. */
502 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
503 if (!EQ (mini_frame, selected_frame))
504 record_unwind_protect (Fset_window_configuration,
505 Fcurrent_window_configuration (mini_frame));
507 /* If the minibuffer is on an iconified or invisible frame,
508 make it visible now. */
509 Fmake_frame_visible (mini_frame);
511 if (minibuffer_auto_raise)
512 Fraise_frame (mini_frame);
514 /* We have to do this after saving the window configuration
515 since that is what restores the current buffer. */
517 /* Arrange to restore a number of minibuffer-related variables.
518 We could bind each variable separately, but that would use lots of
519 specpdl slots. */
520 minibuf_save_list
521 = Fcons (Voverriding_local_map,
522 Fcons (minibuf_window, minibuf_save_list));
523 minibuf_save_list
524 = Fcons (minibuf_prompt,
525 Fcons (make_number (minibuf_prompt_width),
526 Fcons (Vhelp_form,
527 Fcons (Vcurrent_prefix_arg,
528 Fcons (Vminibuffer_history_position,
529 Fcons (Vminibuffer_history_variable,
530 minibuf_save_list))))));
532 record_unwind_protect (read_minibuf_unwind, Qnil);
533 minibuf_level++;
535 /* Now that we can restore all those variables, start changing them. */
537 minibuf_prompt_width = 0;
538 minibuf_prompt = Fcopy_sequence (prompt);
539 Vminibuffer_history_position = histpos;
540 Vminibuffer_history_variable = histvar;
541 Vhelp_form = Vminibuffer_help_form;
543 if (inherit_input_method)
545 /* `current-input-method' is buffer local. So, remember it in
546 INPUT_METHOD before changing the current buffer. */
547 input_method = Fsymbol_value (Qcurrent_input_method);
548 enable_multibyte = current_buffer->enable_multibyte_characters;
551 /* Switch to the minibuffer. */
553 minibuffer = get_minibuffer (minibuf_level);
554 Fset_buffer (minibuffer);
556 /* The current buffer's default directory is usually the right thing
557 for our minibuffer here. However, if you're typing a command at
558 a minibuffer-only frame when minibuf_level is zero, then buf IS
559 the current_buffer, so reset_buffer leaves buf's default
560 directory unchanged. This is a bummer when you've just started
561 up Emacs and buf's default directory is Qnil. Here's a hack; can
562 you think of something better to do? Find another buffer with a
563 better directory, and use that one instead. */
564 if (STRINGP (ambient_dir))
565 current_buffer->directory = ambient_dir;
566 else
568 Lisp_Object buf_list;
570 for (buf_list = Vbuffer_alist;
571 CONSP (buf_list);
572 buf_list = XCDR (buf_list))
574 Lisp_Object other_buf;
576 other_buf = XCDR (XCAR (buf_list));
577 if (STRINGP (XBUFFER (other_buf)->directory))
579 current_buffer->directory = XBUFFER (other_buf)->directory;
580 break;
585 if (!EQ (mini_frame, selected_frame))
586 Fredirect_frame_focus (selected_frame, mini_frame);
588 Vminibuf_scroll_window = selected_window;
589 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
590 minibuf_selected_window = selected_window;
591 Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
592 Fselect_window (minibuf_window, Qnil);
593 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
595 Fmake_local_variable (Qprint_escape_newlines);
596 print_escape_newlines = 1;
598 /* Erase the buffer. */
600 int count1 = SPECPDL_INDEX ();
601 specbind (Qinhibit_read_only, Qt);
602 specbind (Qinhibit_modification_hooks, Qt);
603 Ferase_buffer ();
604 unbind_to (count1, Qnil);
607 if (!NILP (current_buffer->enable_multibyte_characters)
608 && ! STRING_MULTIBYTE (minibuf_prompt))
609 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
611 /* Insert the prompt, record where it ends. */
612 Finsert (1, &minibuf_prompt);
613 if (PT > BEG)
615 Fput_text_property (make_number (BEG), make_number (PT),
616 Qfront_sticky, Qt, Qnil);
617 Fput_text_property (make_number (BEG), make_number (PT),
618 Qrear_nonsticky, Qt, Qnil);
619 Fput_text_property (make_number (BEG), make_number (PT),
620 Qfield, Qt, Qnil);
621 Fadd_text_properties (make_number (BEG), make_number (PT),
622 Vminibuffer_prompt_properties, Qnil);
625 minibuf_prompt_width = (int) current_column (); /* iftc */
627 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
628 if (inherit_input_method)
629 current_buffer->enable_multibyte_characters = enable_multibyte;
631 /* Put in the initial input. */
632 if (!NILP (initial))
634 Finsert (1, &initial);
635 if (INTEGERP (backup_n))
636 Fforward_char (backup_n);
639 clear_message (1, 1);
640 current_buffer->keymap = map;
642 /* Turn on an input method stored in INPUT_METHOD if any. */
643 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
644 call1 (Qactivate_input_method, input_method);
646 /* Run our hook, but not if it is empty.
647 (run-hooks would do nothing if it is empty,
648 but it's important to save time here in the usual case.) */
649 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
650 && !NILP (Vrun_hooks))
651 call1 (Vrun_hooks, Qminibuffer_setup_hook);
653 /* Don't allow the user to undo past this point. */
654 current_buffer->undo_list = Qnil;
656 recursive_edit_1 ();
658 /* If cursor is on the minibuffer line,
659 show the user we have exited by putting it in column 0. */
660 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
661 && !noninteractive)
663 XWINDOW (minibuf_window)->cursor.hpos = 0;
664 XWINDOW (minibuf_window)->cursor.x = 0;
665 XWINDOW (minibuf_window)->must_be_updated_p = 1;
666 update_frame (XFRAME (selected_frame), 1, 1);
667 if (rif && rif->flush_display)
668 rif->flush_display (XFRAME (XWINDOW (minibuf_window)->frame));
671 /* Make minibuffer contents into a string. */
672 Fset_buffer (minibuffer);
673 if (allow_props)
674 val = Fminibuffer_contents ();
675 else
676 val = Fminibuffer_contents_no_properties ();
678 /* VAL is the string of minibuffer text. */
680 last_minibuf_string = val;
682 /* Choose the string to add to the history. */
683 if (SCHARS (val) != 0)
684 histstring = val;
685 else if (STRINGP (defalt))
686 histstring = defalt;
687 else
688 histstring = Qnil;
690 /* Add the value to the appropriate history list, if any. */
691 if (SYMBOLP (Vminibuffer_history_variable)
692 && !NILP (histstring))
694 /* If the caller wanted to save the value read on a history list,
695 then do so if the value is not already the front of the list. */
696 Lisp_Object histval;
698 /* If variable is unbound, make it nil. */
699 if (EQ (SYMBOL_VALUE (Vminibuffer_history_variable), Qunbound))
700 Fset (Vminibuffer_history_variable, Qnil);
702 histval = Fsymbol_value (Vminibuffer_history_variable);
704 /* The value of the history variable must be a cons or nil. Other
705 values are unacceptable. We silently ignore these values. */
707 if (NILP (histval)
708 || (CONSP (histval)
709 /* Don't duplicate the most recent entry in the history. */
710 && NILP (Fequal (histstring, Fcar (histval)))))
712 Lisp_Object length;
714 histval = Fcons (histstring, histval);
715 Fset (Vminibuffer_history_variable, histval);
717 /* Truncate if requested. */
718 length = Fget (Vminibuffer_history_variable, Qhistory_length);
719 if (NILP (length)) length = Vhistory_length;
720 if (INTEGERP (length))
722 if (XINT (length) <= 0)
723 Fset (Vminibuffer_history_variable, Qnil);
724 else
726 Lisp_Object temp;
728 temp = Fnthcdr (Fsub1 (length), histval);
729 if (CONSP (temp)) Fsetcdr (temp, Qnil);
735 /* If Lisp form desired instead of string, parse it. */
736 if (expflag)
737 val = string_to_object (val, defalt);
739 /* The appropriate frame will get selected
740 in set-window-configuration. */
741 RETURN_UNGCPRO (unbind_to (count, val));
744 /* Return a buffer to be used as the minibuffer at depth `depth'.
745 depth = 0 is the lowest allowed argument, and that is the value
746 used for nonrecursive minibuffer invocations */
748 Lisp_Object
749 get_minibuffer (depth)
750 int depth;
752 Lisp_Object tail, num, buf;
753 char name[24];
754 extern Lisp_Object nconc2 ();
756 XSETFASTINT (num, depth);
757 tail = Fnthcdr (num, Vminibuffer_list);
758 if (NILP (tail))
760 tail = Fcons (Qnil, Qnil);
761 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
763 buf = Fcar (tail);
764 if (NILP (buf) || NILP (XBUFFER (buf)->name))
766 sprintf (name, " *Minibuf-%d*", depth);
767 buf = Fget_buffer_create (build_string (name));
769 /* Although the buffer's name starts with a space, undo should be
770 enabled in it. */
771 Fbuffer_enable_undo (buf);
773 XSETCAR (tail, buf);
775 else
777 int count = SPECPDL_INDEX ();
779 reset_buffer (XBUFFER (buf));
780 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
781 Fset_buffer (buf);
782 Fkill_all_local_variables ();
783 unbind_to (count, Qnil);
786 return buf;
789 /* This function is called on exiting minibuffer, whether normally or
790 not, and it restores the current window, buffer, etc. */
792 static Lisp_Object
793 read_minibuf_unwind (data)
794 Lisp_Object data;
796 Lisp_Object old_deactivate_mark;
797 Lisp_Object window;
799 /* We are exiting the minibuffer one way or the other,
800 so run the hook. */
801 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
802 && !NILP (Vrun_hooks))
803 safe_run_hooks (Qminibuffer_exit_hook);
805 /* If this was a recursive minibuffer,
806 tie the minibuffer window back to the outer level minibuffer buffer. */
807 minibuf_level--;
809 window = minibuf_window;
810 /* To keep things predictable, in case it matters, let's be in the
811 minibuffer when we reset the relevant variables. */
812 Fset_buffer (XWINDOW (window)->buffer);
814 /* Restore prompt, etc, from outer minibuffer level. */
815 minibuf_prompt = Fcar (minibuf_save_list);
816 minibuf_save_list = Fcdr (minibuf_save_list);
817 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
818 minibuf_save_list = Fcdr (minibuf_save_list);
819 Vhelp_form = Fcar (minibuf_save_list);
820 minibuf_save_list = Fcdr (minibuf_save_list);
821 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
822 minibuf_save_list = Fcdr (minibuf_save_list);
823 Vminibuffer_history_position = Fcar (minibuf_save_list);
824 minibuf_save_list = Fcdr (minibuf_save_list);
825 Vminibuffer_history_variable = Fcar (minibuf_save_list);
826 minibuf_save_list = Fcdr (minibuf_save_list);
827 Voverriding_local_map = Fcar (minibuf_save_list);
828 minibuf_save_list = Fcdr (minibuf_save_list);
829 #if 0
830 temp = Fcar (minibuf_save_list);
831 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
832 minibuf_window = temp;
833 #endif
834 minibuf_save_list = Fcdr (minibuf_save_list);
836 /* Erase the minibuffer we were using at this level. */
838 int count = SPECPDL_INDEX ();
839 /* Prevent error in erase-buffer. */
840 specbind (Qinhibit_read_only, Qt);
841 specbind (Qinhibit_modification_hooks, Qt);
842 old_deactivate_mark = Vdeactivate_mark;
843 Ferase_buffer ();
844 Vdeactivate_mark = old_deactivate_mark;
845 unbind_to (count, Qnil);
848 /* When we get to the outmost level, make sure we resize the
849 mini-window back to its normal size. */
850 if (minibuf_level == 0)
851 resize_mini_window (XWINDOW (window), 0);
853 /* Make sure minibuffer window is erased, not ignored. */
854 windows_or_buffers_changed++;
855 XSETFASTINT (XWINDOW (window)->last_modified, 0);
856 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
857 return Qnil;
861 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
862 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
863 If optional second arg INITIAL-CONTENTS is non-nil, it is a string
864 to be inserted into the minibuffer before reading input.
865 If INITIAL-CONTENTS is (STRING . POSITION), the initial input
866 is STRING, but point is placed at position POSITION in the minibuffer.
867 Third arg KEYMAP is a keymap to use whilst reading;
868 if omitted or nil, the default is `minibuffer-local-map'.
869 If fourth arg READ is non-nil, then interpret the result as a Lisp object
870 and return that object:
871 in other words, do `(car (read-from-string INPUT-STRING))'
872 Fifth arg HIST, if non-nil, specifies a history list
873 and optionally the initial position in the list.
874 It can be a symbol, which is the history list variable to use,
875 or it can be a cons cell (HISTVAR . HISTPOS).
876 In that case, HISTVAR is the history list variable to use,
877 and HISTPOS is the initial position (the position in the list
878 which INITIAL-CONTENTS corresponds to).
879 Positions are counted starting from 1 at the beginning of the list.
880 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
881 for history commands; but `read-from-minibuffer' does NOT return DEFAULT-VALUE
882 if the user enters empty input! It returns the empty string.
883 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
884 the current input method and the setting of `enable-multibyte-characters'.
885 If the variable `minibuffer-allow-text-properties' is non-nil,
886 then the string which is returned includes whatever text properties
887 were present in the minibuffer. Otherwise the value has no text properties. */)
888 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
889 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
890 Lisp_Object inherit_input_method;
892 int pos = 0;
893 Lisp_Object histvar, histpos, position, val;
894 struct gcpro gcpro1;
896 position = Qnil;
898 CHECK_STRING (prompt);
899 if (!NILP (initial_contents))
901 if (CONSP (initial_contents))
903 position = Fcdr (initial_contents);
904 initial_contents = Fcar (initial_contents);
906 CHECK_STRING (initial_contents);
907 if (!NILP (position))
909 CHECK_NUMBER (position);
910 /* Convert to distance from end of input. */
911 if (XINT (position) < 1)
912 /* A number too small means the beginning of the string. */
913 pos = - SCHARS (initial_contents);
914 else
915 pos = XINT (position) - 1 - SCHARS (initial_contents);
919 if (NILP (keymap))
920 keymap = Vminibuffer_local_map;
921 else
922 keymap = get_keymap (keymap, 1, 0);
924 if (SYMBOLP (hist))
926 histvar = hist;
927 histpos = Qnil;
929 else
931 histvar = Fcar_safe (hist);
932 histpos = Fcdr_safe (hist);
934 if (NILP (histvar))
935 histvar = Qminibuffer_history;
936 if (NILP (histpos))
937 XSETFASTINT (histpos, 0);
939 GCPRO1 (default_value);
940 val = read_minibuf (keymap, initial_contents, prompt,
941 make_number (pos), !NILP (read),
942 histvar, histpos, default_value,
943 minibuffer_allow_text_properties,
944 !NILP (inherit_input_method));
945 UNGCPRO;
946 return val;
949 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
950 doc: /* Return a Lisp object read using the minibuffer.
951 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
952 is a string to insert in the minibuffer before reading. */)
953 (prompt, initial_contents)
954 Lisp_Object prompt, initial_contents;
956 CHECK_STRING (prompt);
957 if (!NILP (initial_contents))
958 CHECK_STRING (initial_contents);
959 return read_minibuf (Vminibuffer_local_map, initial_contents,
960 prompt, Qnil, 1, Qminibuffer_history,
961 make_number (0), Qnil, 0, 0);
964 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
965 doc: /* Return value of Lisp expression read using the minibuffer.
966 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
967 is a string to insert in the minibuffer before reading. */)
968 (prompt, initial_contents)
969 Lisp_Object prompt, initial_contents;
971 return Feval (Fread_minibuffer (prompt, initial_contents));
974 /* Functions that use the minibuffer to read various things. */
976 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
977 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
978 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
979 The third arg HISTORY, if non-nil, specifies a history list
980 and optionally the initial position in the list.
981 See `read-from-minibuffer' for details of HISTORY argument.
982 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
983 for history commands, and as the value to return if the user enters
984 the empty string.
985 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
986 the current input method and the setting of `enable-multibyte-characters'. */)
987 (prompt, initial_input, history, default_value, inherit_input_method)
988 Lisp_Object prompt, initial_input, history, default_value;
989 Lisp_Object inherit_input_method;
991 Lisp_Object val;
992 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
993 Qnil, history, default_value,
994 inherit_input_method);
995 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
996 val = default_value;
997 return val;
1000 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
1001 doc: /* Read a string from the terminal, not allowing blanks.
1002 Prompt with PROMPT, and provide INITIAL as an initial value of the input string.
1003 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1004 the current input method and the setting of `enable-multibyte-characters'. */)
1005 (prompt, initial, inherit_input_method)
1006 Lisp_Object prompt, initial, inherit_input_method;
1008 CHECK_STRING (prompt);
1009 if (! NILP (initial))
1010 CHECK_STRING (initial);
1012 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
1013 0, Qminibuffer_history, make_number (0), Qnil, 0,
1014 !NILP (inherit_input_method));
1017 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
1018 doc: /* Read the name of a command and return as a symbol.
1019 Prompt with PROMPT. By default, return DEFAULT-VALUE. */)
1020 (prompt, default_value)
1021 Lisp_Object prompt, default_value;
1023 Lisp_Object name, default_string;
1025 if (NILP (default_value))
1026 default_string = Qnil;
1027 else if (SYMBOLP (default_value))
1028 default_string = SYMBOL_NAME (default_value);
1029 else
1030 default_string = default_value;
1032 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1033 Qnil, Qnil, default_string, Qnil);
1034 if (NILP (name))
1035 return name;
1036 return Fintern (name, Qnil);
1039 #ifdef NOTDEF
1040 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
1041 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
1042 Prompt with PROMPT. */)
1043 (prompt)
1044 Lisp_Object prompt;
1046 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
1047 Qnil);
1049 #endif /* NOTDEF */
1051 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
1052 doc: /* Read the name of a user variable and return it as a symbol.
1053 Prompt with PROMPT. By default, return DEFAULT-VALUE.
1054 A user variable is one for which `user-variable-p' returns non-nil. */)
1055 (prompt, default_value)
1056 Lisp_Object prompt, default_value;
1058 Lisp_Object name, default_string;
1060 if (NILP (default_value))
1061 default_string = Qnil;
1062 else if (SYMBOLP (default_value))
1063 default_string = SYMBOL_NAME (default_value);
1064 else
1065 default_string = default_value;
1067 name = Fcompleting_read (prompt, Vobarray,
1068 Quser_variable_p, Qt,
1069 Qnil, Qnil, default_string, Qnil);
1070 if (NILP (name))
1071 return name;
1072 return Fintern (name, Qnil);
1075 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
1076 doc: /* Read the name of a buffer and return as a string.
1077 Prompt with PROMPT.
1078 Optional second arg DEF is value to return if user enters an empty line.
1079 If optional third arg REQUIRE-MATCH is non-nil,
1080 only existing buffer names are allowed. */)
1081 (prompt, def, require_match)
1082 Lisp_Object prompt, def, require_match;
1084 Lisp_Object args[4];
1086 if (BUFFERP (def))
1087 def = XBUFFER (def)->name;
1089 if (NILP (Vread_buffer_function))
1091 if (!NILP (def))
1093 args[0] = build_string ("%s(default %s) ");
1094 args[1] = prompt;
1095 args[2] = def;
1096 prompt = Fformat (3, args);
1099 return Fcompleting_read (prompt, Vbuffer_alist, Qnil,
1100 require_match, Qnil, Qbuffer_name_history,
1101 def, Qnil);
1103 else
1105 args[0] = Vread_buffer_function;
1106 args[1] = prompt;
1107 args[2] = def;
1108 args[3] = require_match;
1109 return Ffuncall(4, args);
1113 static Lisp_Object
1114 minibuf_conform_representation (string, basis)
1115 Lisp_Object string, basis;
1117 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1118 return string;
1120 if (STRING_MULTIBYTE (string))
1121 return Fstring_make_unibyte (string);
1122 else
1123 return Fstring_make_multibyte (string);
1126 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1127 doc: /* Return common substring of all completions of STRING in ALIST.
1128 Each car of each element of ALIST is tested to see if it begins with STRING.
1129 All that match are compared together; the longest initial sequence
1130 common to all matches is returned as a string.
1131 If there is no match at all, nil is returned.
1132 For a unique match which is exact, t is returned.
1134 If ALIST is a hash-table, all the string keys are the possible matches.
1135 If ALIST is an obarray, the names of all symbols in the obarray
1136 are the possible matches.
1138 ALIST can also be a function to do the completion itself.
1139 It receives three arguments: the values STRING, PREDICATE and nil.
1140 Whatever it returns becomes the value of `try-completion'.
1142 If optional third argument PREDICATE is non-nil,
1143 it is used to test each possible match.
1144 The match is a candidate only if PREDICATE returns non-nil.
1145 The argument given to PREDICATE is the alist element
1146 or the symbol from the obarray. If ALIST is a hash-table,
1147 predicate is called with two arguments: the key and the value.
1148 Additionally to this predicate, `completion-regexp-list'
1149 is used to further constrain the set of candidates. */)
1150 (string, alist, predicate)
1151 Lisp_Object string, alist, predicate;
1153 Lisp_Object bestmatch, tail, elt, eltstring;
1154 /* Size in bytes of BESTMATCH. */
1155 int bestmatchsize = 0;
1156 /* These are in bytes, too. */
1157 int compare, matchsize;
1158 int type = HASH_TABLE_P (alist) ? 3
1159 : VECTORP (alist) ? 2
1160 : NILP (alist) || (CONSP (alist)
1161 && (!SYMBOLP (XCAR (alist))
1162 || NILP (XCAR (alist))));
1163 int index = 0, obsize = 0;
1164 int matchcount = 0;
1165 Lisp_Object bucket, zero, end, tem;
1166 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1168 CHECK_STRING (string);
1169 if (type == 0)
1170 return call3 (alist, string, predicate, Qnil);
1172 bestmatch = bucket = Qnil;
1174 /* If ALIST is not a list, set TAIL just for gc pro. */
1175 tail = alist;
1176 if (type == 2)
1178 obsize = XVECTOR (alist)->size;
1179 bucket = XVECTOR (alist)->contents[index];
1182 while (1)
1184 /* Get the next element of the alist, obarray, or hash-table. */
1185 /* Exit the loop if the elements are all used up. */
1186 /* elt gets the alist element or symbol.
1187 eltstring gets the name to check as a completion. */
1189 if (type == 1)
1191 if (!CONSP (tail))
1192 break;
1193 elt = XCAR (tail);
1194 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1195 tail = XCDR (tail);
1197 else if (type == 2)
1199 if (XFASTINT (bucket) != 0)
1201 elt = bucket;
1202 eltstring = Fsymbol_name (elt);
1203 if (XSYMBOL (bucket)->next)
1204 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1205 else
1206 XSETFASTINT (bucket, 0);
1208 else if (++index >= obsize)
1209 break;
1210 else
1212 bucket = XVECTOR (alist)->contents[index];
1213 continue;
1216 else /* if (type == 3) */
1218 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1219 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1220 index++;
1221 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1222 break;
1223 else
1224 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1227 /* Is this element a possible completion? */
1229 if (STRINGP (eltstring)
1230 && SCHARS (string) <= SCHARS (eltstring)
1231 && (tem = Fcompare_strings (eltstring, make_number (0),
1232 make_number (SCHARS (string)),
1233 string, make_number (0), Qnil,
1234 completion_ignore_case ?Qt : Qnil),
1235 EQ (Qt, tem)))
1237 /* Yes. */
1238 Lisp_Object regexps;
1239 Lisp_Object zero;
1240 XSETFASTINT (zero, 0);
1242 /* Ignore this element if it fails to match all the regexps. */
1243 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1244 regexps = XCDR (regexps))
1246 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1247 if (NILP (tem))
1248 break;
1250 if (CONSP (regexps))
1251 continue;
1253 /* Ignore this element if there is a predicate
1254 and the predicate doesn't like it. */
1256 if (!NILP (predicate))
1258 if (EQ (predicate, Qcommandp))
1259 tem = Fcommandp (elt, Qnil);
1260 else
1262 GCPRO4 (tail, string, eltstring, bestmatch);
1263 tem = type == 3
1264 ? call2 (predicate, elt,
1265 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1266 : call1 (predicate, elt);
1267 UNGCPRO;
1269 if (NILP (tem)) continue;
1272 /* Update computation of how much all possible completions match */
1274 if (NILP (bestmatch))
1276 matchcount = 1;
1277 bestmatch = eltstring;
1278 bestmatchsize = SCHARS (eltstring);
1280 else
1282 compare = min (bestmatchsize, SCHARS (eltstring));
1283 tem = Fcompare_strings (bestmatch, make_number (0),
1284 make_number (compare),
1285 eltstring, make_number (0),
1286 make_number (compare),
1287 completion_ignore_case ? Qt : Qnil);
1288 if (EQ (tem, Qt))
1289 matchsize = compare;
1290 else if (XINT (tem) < 0)
1291 matchsize = - XINT (tem) - 1;
1292 else
1293 matchsize = XINT (tem) - 1;
1295 if (matchsize < 0)
1296 /* When can this happen ? -stef */
1297 matchsize = compare;
1298 if (completion_ignore_case)
1300 /* If this is an exact match except for case,
1301 use it as the best match rather than one that is not an
1302 exact match. This way, we get the case pattern
1303 of the actual match. */
1304 if ((matchsize == SCHARS (eltstring)
1305 && matchsize < SCHARS (bestmatch))
1307 /* If there is more than one exact match ignoring case,
1308 and one of them is exact including case,
1309 prefer that one. */
1310 /* If there is no exact match ignoring case,
1311 prefer a match that does not change the case
1312 of the input. */
1313 ((matchsize == SCHARS (eltstring))
1315 (matchsize == SCHARS (bestmatch))
1316 && (tem = Fcompare_strings (eltstring, make_number (0),
1317 make_number (SCHARS (string)),
1318 string, make_number (0),
1319 Qnil,
1320 Qnil),
1321 EQ (Qt, tem))
1322 && (tem = Fcompare_strings (bestmatch, make_number (0),
1323 make_number (SCHARS (string)),
1324 string, make_number (0),
1325 Qnil,
1326 Qnil),
1327 ! EQ (Qt, tem))))
1328 bestmatch = eltstring;
1330 if (bestmatchsize != SCHARS (eltstring)
1331 || bestmatchsize != matchsize)
1332 /* Don't count the same string multiple times. */
1333 matchcount++;
1334 bestmatchsize = matchsize;
1335 if (matchsize <= SCHARS (string)
1336 && matchcount > 1)
1337 /* No need to look any further. */
1338 break;
1343 if (NILP (bestmatch))
1344 return Qnil; /* No completions found */
1345 /* If we are ignoring case, and there is no exact match,
1346 and no additional text was supplied,
1347 don't change the case of what the user typed. */
1348 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1349 && SCHARS (bestmatch) > bestmatchsize)
1350 return minibuf_conform_representation (string, bestmatch);
1352 /* Return t if the supplied string is an exact match (counting case);
1353 it does not require any change to be made. */
1354 if (matchcount == 1 && bestmatchsize == SCHARS (string)
1355 && (tem = Fcompare_strings (bestmatch, make_number (0),
1356 make_number (bestmatchsize),
1357 string, make_number (0),
1358 make_number (bestmatchsize),
1359 Qnil),
1360 EQ (Qt, tem)))
1361 return Qt;
1363 XSETFASTINT (zero, 0); /* Else extract the part in which */
1364 XSETFASTINT (end, bestmatchsize); /* all completions agree */
1365 return Fsubstring (bestmatch, zero, end);
1368 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1369 doc: /* Search for partial matches to STRING in ALIST.
1370 Each car of each element of ALIST is tested to see if it begins with STRING.
1371 The value is a list of all the strings from ALIST that match.
1373 If ALIST is a hash-table, all the string keys are the possible matches.
1374 If ALIST is an obarray, the names of all symbols in the obarray
1375 are the possible matches.
1377 ALIST can also be a function to do the completion itself.
1378 It receives three arguments: the values STRING, PREDICATE and t.
1379 Whatever it returns becomes the value of `all-completions'.
1381 If optional third argument PREDICATE is non-nil,
1382 it is used to test each possible match.
1383 The match is a candidate only if PREDICATE returns non-nil.
1384 The argument given to PREDICATE is the alist element
1385 or the symbol from the obarray. If ALIST is a hash-table,
1386 predicate is called with two arguments: the key and the value.
1387 Additionally to this predicate, `completion-regexp-list'
1388 is used to further constrain the set of candidates.
1390 If the optional fourth argument HIDE-SPACES is non-nil,
1391 strings in ALIST that start with a space
1392 are ignored unless STRING itself starts with a space. */)
1393 (string, alist, predicate, hide_spaces)
1394 Lisp_Object string, alist, predicate, hide_spaces;
1396 Lisp_Object tail, elt, eltstring;
1397 Lisp_Object allmatches;
1398 int type = HASH_TABLE_P (alist) ? 3
1399 : VECTORP (alist) ? 2
1400 : NILP (alist) || (CONSP (alist)
1401 && (!SYMBOLP (XCAR (alist))
1402 || NILP (XCAR (alist))));
1403 int index = 0, obsize = 0;
1404 Lisp_Object bucket, tem;
1405 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1407 CHECK_STRING (string);
1408 if (type == 0)
1409 return call3 (alist, string, predicate, Qt);
1410 allmatches = bucket = Qnil;
1412 /* If ALIST is not a list, set TAIL just for gc pro. */
1413 tail = alist;
1414 if (type == 2)
1416 obsize = XVECTOR (alist)->size;
1417 bucket = XVECTOR (alist)->contents[index];
1420 while (1)
1422 /* Get the next element of the alist, obarray, or hash-table. */
1423 /* Exit the loop if the elements are all used up. */
1424 /* elt gets the alist element or symbol.
1425 eltstring gets the name to check as a completion. */
1427 if (type == 1)
1429 if (!CONSP (tail))
1430 break;
1431 elt = XCAR (tail);
1432 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1433 tail = XCDR (tail);
1435 else if (type == 2)
1437 if (XFASTINT (bucket) != 0)
1439 elt = bucket;
1440 eltstring = Fsymbol_name (elt);
1441 if (XSYMBOL (bucket)->next)
1442 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1443 else
1444 XSETFASTINT (bucket, 0);
1446 else if (++index >= obsize)
1447 break;
1448 else
1450 bucket = XVECTOR (alist)->contents[index];
1451 continue;
1454 else /* if (type == 3) */
1456 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1457 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1458 index++;
1459 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1460 break;
1461 else
1462 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1465 /* Is this element a possible completion? */
1467 if (STRINGP (eltstring)
1468 && SCHARS (string) <= SCHARS (eltstring)
1469 /* If HIDE_SPACES, reject alternatives that start with space
1470 unless the input starts with space. */
1471 && ((SBYTES (string) > 0
1472 && SREF (string, 0) == ' ')
1473 || SREF (eltstring, 0) != ' '
1474 || NILP (hide_spaces))
1475 && (tem = Fcompare_strings (eltstring, make_number (0),
1476 make_number (SCHARS (string)),
1477 string, make_number (0),
1478 make_number (SCHARS (string)),
1479 completion_ignore_case ? Qt : Qnil),
1480 EQ (Qt, tem)))
1482 /* Yes. */
1483 Lisp_Object regexps;
1484 Lisp_Object zero;
1485 XSETFASTINT (zero, 0);
1487 /* Ignore this element if it fails to match all the regexps. */
1488 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1489 regexps = XCDR (regexps))
1491 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1492 if (NILP (tem))
1493 break;
1495 if (CONSP (regexps))
1496 continue;
1498 /* Ignore this element if there is a predicate
1499 and the predicate doesn't like it. */
1501 if (!NILP (predicate))
1503 if (EQ (predicate, Qcommandp))
1504 tem = Fcommandp (elt, Qnil);
1505 else
1507 GCPRO4 (tail, eltstring, allmatches, string);
1508 tem = type == 3
1509 ? call2 (predicate, elt,
1510 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1511 : call1 (predicate, elt);
1512 UNGCPRO;
1514 if (NILP (tem)) continue;
1516 /* Ok => put it on the list. */
1517 allmatches = Fcons (eltstring, allmatches);
1521 return Fnreverse (allmatches);
1524 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1525 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1526 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
1527 Lisp_Object Vminibuffer_completing_file_name;
1529 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1530 doc: /* Read a string in the minibuffer, with completion.
1531 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1532 TABLE is an alist whose elements' cars are strings, or an obarray.
1533 TABLE can also be a function to do the completion itself.
1534 PREDICATE limits completion to a subset of TABLE.
1535 See `try-completion' and `all-completions' for more details
1536 on completion, TABLE, and PREDICATE.
1538 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
1539 the input is (or completes to) an element of TABLE or is null.
1540 If it is also not t, typing RET does not exit if it does non-null completion.
1541 If the input is null, `completing-read' returns an empty string,
1542 regardless of the value of REQUIRE-MATCH.
1544 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1545 If it is (STRING . POSITION), the initial input
1546 is STRING, but point is placed POSITION characters into the string.
1547 This feature is deprecated--it is best to pass nil for INITIAL-INPUT
1548 and supply the default value DEF instead. The user can yank the
1549 default value into the minibuffer easily using \\[next-history-element].
1551 HIST, if non-nil, specifies a history list
1552 and optionally the initial position in the list.
1553 It can be a symbol, which is the history list variable to use,
1554 or it can be a cons cell (HISTVAR . HISTPOS).
1555 In that case, HISTVAR is the history list variable to use,
1556 and HISTPOS is the initial position (the position in the list
1557 which INITIAL-INPUT corresponds to).
1558 Positions are counted starting from 1 at the beginning of the list.
1559 The variable `history-length' controls the maximum length of a
1560 history list.
1562 DEF, if non-nil, is the default value.
1564 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
1565 the current input method and the setting of `enable-multibyte-characters'.
1567 Completion ignores case if the ambient value of
1568 `completion-ignore-case' is non-nil. */)
1569 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method)
1570 Lisp_Object prompt, table, predicate, require_match, initial_input;
1571 Lisp_Object hist, def, inherit_input_method;
1573 Lisp_Object val, histvar, histpos, position;
1574 Lisp_Object init;
1575 int pos = 0;
1576 int count = SPECPDL_INDEX ();
1577 struct gcpro gcpro1;
1579 init = initial_input;
1580 GCPRO1 (def);
1582 specbind (Qminibuffer_completion_table, table);
1583 specbind (Qminibuffer_completion_predicate, predicate);
1584 specbind (Qminibuffer_completion_confirm,
1585 EQ (require_match, Qt) ? Qnil : require_match);
1586 last_exact_completion = Qnil;
1588 position = Qnil;
1589 if (!NILP (init))
1591 if (CONSP (init))
1593 position = Fcdr (init);
1594 init = Fcar (init);
1596 CHECK_STRING (init);
1597 if (!NILP (position))
1599 CHECK_NUMBER (position);
1600 /* Convert to distance from end of input. */
1601 pos = XINT (position) - SCHARS (init);
1605 if (SYMBOLP (hist))
1607 histvar = hist;
1608 histpos = Qnil;
1610 else
1612 histvar = Fcar_safe (hist);
1613 histpos = Fcdr_safe (hist);
1615 if (NILP (histvar))
1616 histvar = Qminibuffer_history;
1617 if (NILP (histpos))
1618 XSETFASTINT (histpos, 0);
1620 val = read_minibuf (NILP (require_match)
1621 ? Vminibuffer_local_completion_map
1622 : Vminibuffer_local_must_match_map,
1623 init, prompt, make_number (pos), 0,
1624 histvar, histpos, def, 0,
1625 !NILP (inherit_input_method));
1627 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
1628 val = def;
1630 RETURN_UNGCPRO (unbind_to (count, val));
1633 Lisp_Object Fminibuffer_completion_help ();
1634 Lisp_Object Fassoc_string ();
1636 /* Test whether TXT is an exact completion. */
1637 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1638 doc: /* Return non-nil if STRING is a valid completion.
1639 Takes the same arguments as `all-completions' and `try-completion'.
1640 If ALIST is a function, it is called with three arguments:
1641 the values STRING, PREDICATE and `lambda'. */)
1642 (string, alist, predicate)
1643 Lisp_Object string, alist, predicate;
1645 Lisp_Object regexps, tem = Qnil;
1646 int i = 0;
1648 CHECK_STRING (string);
1650 if ((CONSP (alist) && (!SYMBOLP (XCAR (alist)) || NILP (XCAR (alist))))
1651 || NILP (alist))
1653 tem = Fassoc_string (string, alist, completion_ignore_case ? Qt : Qnil);
1654 if NILP (tem)
1655 return Qnil;
1657 else if (VECTORP (alist))
1659 /* Bypass intern-soft as that loses for nil. */
1660 tem = oblookup (alist,
1661 SDATA (string),
1662 SCHARS (string),
1663 SBYTES (string));
1664 if (!SYMBOLP (tem))
1666 if (STRING_MULTIBYTE (string))
1667 string = Fstring_make_unibyte (string);
1668 else
1669 string = Fstring_make_multibyte (string);
1671 tem = oblookup (Vminibuffer_completion_table,
1672 SDATA (string),
1673 SCHARS (string),
1674 SBYTES (string));
1675 if (!SYMBOLP (tem))
1676 return Qnil;
1679 else if (HASH_TABLE_P (alist))
1681 i = hash_lookup (XHASH_TABLE (alist), string, NULL);
1682 if (i >= 0)
1683 tem = HASH_KEY (XHASH_TABLE (alist), i);
1684 else
1685 return Qnil;
1687 else
1688 return call3 (alist, string, predicate, Qlambda);
1690 /* Reject this element if it fails to match all the regexps. */
1691 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1692 regexps = XCDR (regexps))
1694 if (NILP (Fstring_match (XCAR (regexps),
1695 SYMBOLP (tem) ? string : tem,
1696 Qnil)))
1697 return Qnil;
1700 /* Finally, check the predicate. */
1701 if (!NILP (predicate))
1702 return HASH_TABLE_P (alist)
1703 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (alist), i))
1704 : call1 (predicate, tem);
1705 else
1706 return Qt;
1709 /* returns:
1710 * 0 no possible completion
1711 * 1 was already an exact and unique completion
1712 * 3 was already an exact completion
1713 * 4 completed to an exact completion
1714 * 5 some completion happened
1715 * 6 no completion happened
1718 do_completion ()
1720 Lisp_Object completion, string, tem;
1721 int completedp;
1722 Lisp_Object last;
1723 struct gcpro gcpro1, gcpro2;
1725 completion = Ftry_completion (minibuffer_completion_contents (),
1726 Vminibuffer_completion_table,
1727 Vminibuffer_completion_predicate);
1728 last = last_exact_completion;
1729 last_exact_completion = Qnil;
1731 GCPRO2 (completion, last);
1733 if (NILP (completion))
1735 bitch_at_user ();
1736 temp_echo_area_glyphs (build_string (" [No match]"));
1737 UNGCPRO;
1738 return 0;
1741 if (EQ (completion, Qt)) /* exact and unique match */
1743 UNGCPRO;
1744 return 1;
1747 string = minibuffer_completion_contents ();
1749 /* COMPLETEDP should be true if some completion was done, which
1750 doesn't include simply changing the case of the entered string.
1751 However, for appearance, the string is rewritten if the case
1752 changes. */
1753 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qt);
1754 completedp = !EQ (tem, Qt);
1756 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qnil);
1757 if (!EQ (tem, Qt))
1758 /* Rewrite the user's input. */
1760 int prompt_end = XINT (Fminibuffer_prompt_end ());
1761 /* Some completion happened */
1763 if (! NILP (Vminibuffer_completing_file_name)
1764 && SREF (completion, SBYTES (completion) - 1) == '/'
1765 && PT < ZV
1766 && FETCH_CHAR (PT_BYTE) == '/')
1768 del_range (prompt_end, PT + 1);
1770 else
1771 del_range (prompt_end, PT);
1773 Finsert (1, &completion);
1775 if (! completedp)
1776 /* The case of the string changed, but that's all. We're not
1777 sure whether this is a unique completion or not, so try again
1778 using the real case (this shouldn't recurse again, because
1779 the next time try-completion will return either `t' or the
1780 exact string). */
1782 UNGCPRO;
1783 return do_completion ();
1787 /* It did find a match. Do we match some possibility exactly now? */
1788 tem = Ftest_completion (Fminibuffer_contents (),
1789 Vminibuffer_completion_table,
1790 Vminibuffer_completion_predicate);
1791 if (NILP (tem))
1793 /* not an exact match */
1794 UNGCPRO;
1795 if (completedp)
1796 return 5;
1797 else if (!NILP (Vcompletion_auto_help))
1798 Fminibuffer_completion_help ();
1799 else
1800 temp_echo_area_glyphs (build_string (" [Next char not unique]"));
1801 return 6;
1803 else if (completedp)
1805 UNGCPRO;
1806 return 4;
1808 /* If the last exact completion and this one were the same,
1809 it means we've already given a "Complete but not unique"
1810 message and the user's hit TAB again, so now we give him help. */
1811 last_exact_completion = completion;
1812 if (!NILP (last))
1814 tem = minibuffer_completion_contents ();
1815 if (!NILP (Fequal (tem, last)))
1816 Fminibuffer_completion_help ();
1818 UNGCPRO;
1819 return 3;
1822 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1824 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
1825 doc: /* Like `assoc' but specifically for strings.
1826 Unibyte strings are converted to multibyte for comparison.
1827 And case is ignored if CASE-FOLD is non-nil.
1828 As opposed to `assoc', it will also match an entry consisting of a single
1829 string rather than a cons cell whose car is a string. */)
1830 (key, list, case_fold)
1831 register Lisp_Object key;
1832 Lisp_Object list, case_fold;
1834 register Lisp_Object tail;
1836 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1838 register Lisp_Object elt, tem, thiscar;
1839 elt = Fcar (tail);
1840 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1841 if (!STRINGP (thiscar))
1842 continue;
1843 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1844 key, make_number (0), Qnil,
1845 case_fold);
1846 if (EQ (tem, Qt))
1847 return elt;
1848 QUIT;
1850 return Qnil;
1853 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1854 doc: /* Complete the minibuffer contents as far as possible.
1855 Return nil if there is no valid completion, else t.
1856 If no characters can be completed, display a list of possible completions.
1857 If you repeat this command after it displayed such a list,
1858 scroll the window of possible completions. */)
1861 register int i;
1862 Lisp_Object window, tem;
1864 /* If the previous command was not this,
1865 mark the completion buffer obsolete. */
1866 if (! EQ (current_kboard->Vlast_command, Vthis_command))
1867 Vminibuf_scroll_window = Qnil;
1869 window = Vminibuf_scroll_window;
1870 /* If there's a fresh completion window with a live buffer,
1871 and this command is repeated, scroll that window. */
1872 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1873 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1875 struct buffer *obuf = current_buffer;
1877 Fset_buffer (XWINDOW (window)->buffer);
1878 tem = Fpos_visible_in_window_p (make_number (ZV), window, Qnil);
1879 if (! NILP (tem))
1880 /* If end is in view, scroll up to the beginning. */
1881 Fset_window_start (window, make_number (BEGV), Qnil);
1882 else
1883 /* Else scroll down one screen. */
1884 Fscroll_other_window (Qnil);
1886 set_buffer_internal (obuf);
1887 return Qnil;
1890 i = do_completion ();
1891 switch (i)
1893 case 0:
1894 return Qnil;
1896 case 1:
1897 if (PT != ZV)
1898 Fgoto_char (make_number (ZV));
1899 temp_echo_area_glyphs (build_string (" [Sole completion]"));
1900 break;
1902 case 3:
1903 if (PT != ZV)
1904 Fgoto_char (make_number (ZV));
1905 temp_echo_area_glyphs (build_string (" [Complete, but not unique]"));
1906 break;
1909 return Qt;
1912 /* Subroutines of Fminibuffer_complete_and_exit. */
1914 /* This one is called by internal_condition_case to do the real work. */
1916 Lisp_Object
1917 complete_and_exit_1 ()
1919 return make_number (do_completion ());
1922 /* This one is called by internal_condition_case if an error happens.
1923 Pretend the current value is an exact match. */
1925 Lisp_Object
1926 complete_and_exit_2 (ignore)
1927 Lisp_Object ignore;
1929 return make_number (1);
1932 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1933 Sminibuffer_complete_and_exit, 0, 0, "",
1934 doc: /* If the minibuffer contents is a valid completion then exit.
1935 Otherwise try to complete it. If completion leads to a valid completion,
1936 a repetition of this command will exit. */)
1939 register int i;
1940 Lisp_Object val;
1942 /* Allow user to specify null string */
1943 if (XINT (Fminibuffer_prompt_end ()) == ZV)
1944 goto exit;
1946 if (!NILP (Ftest_completion (Fminibuffer_contents (),
1947 Vminibuffer_completion_table,
1948 Vminibuffer_completion_predicate)))
1949 goto exit;
1951 /* Call do_completion, but ignore errors. */
1952 SET_PT (ZV);
1953 val = internal_condition_case (complete_and_exit_1, Qerror,
1954 complete_and_exit_2);
1956 i = XFASTINT (val);
1957 switch (i)
1959 case 1:
1960 case 3:
1961 goto exit;
1963 case 4:
1964 if (!NILP (Vminibuffer_completion_confirm))
1966 temp_echo_area_glyphs (build_string (" [Confirm]"));
1967 return Qnil;
1969 else
1970 goto exit;
1972 default:
1973 return Qnil;
1975 exit:
1976 return Fthrow (Qexit, Qnil);
1977 /* NOTREACHED */
1980 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1981 0, 0, "",
1982 doc: /* Complete the minibuffer contents at most a single word.
1983 After one word is completed as much as possible, a space or hyphen
1984 is added, provided that matches some possible completion.
1985 Return nil if there is no valid completion, else t. */)
1988 Lisp_Object completion, tem, tem1;
1989 register int i, i_byte;
1990 register const unsigned char *completion_string;
1991 struct gcpro gcpro1, gcpro2;
1992 int prompt_end_charpos = XINT (Fminibuffer_prompt_end ());
1994 /* We keep calling Fbuffer_string rather than arrange for GC to
1995 hold onto a pointer to one of the strings thus made. */
1997 completion = Ftry_completion (minibuffer_completion_contents (),
1998 Vminibuffer_completion_table,
1999 Vminibuffer_completion_predicate);
2000 if (NILP (completion))
2002 bitch_at_user ();
2003 temp_echo_area_glyphs (build_string (" [No match]"));
2004 return Qnil;
2006 if (EQ (completion, Qt))
2007 return Qnil;
2009 #if 0 /* How the below code used to look, for reference. */
2010 tem = Fminibuffer_contents ();
2011 b = SDATA (tem);
2012 i = ZV - 1 - SCHARS (completion);
2013 p = SDATA (completion);
2014 if (i > 0 ||
2015 0 <= scmp (b, p, ZV - 1))
2017 i = 1;
2018 /* Set buffer to longest match of buffer tail and completion head. */
2019 while (0 <= scmp (b + i, p, ZV - 1 - i))
2020 i++;
2021 del_range (1, i + 1);
2022 SET_PT (ZV);
2024 #else /* Rewritten code */
2026 int buffer_nchars, completion_nchars;
2028 CHECK_STRING (completion);
2029 tem = minibuffer_completion_contents ();
2030 GCPRO2 (completion, tem);
2031 /* If reading a file name,
2032 expand any $ENVVAR refs in the buffer and in TEM. */
2033 if (! NILP (Vminibuffer_completing_file_name))
2035 Lisp_Object substituted;
2036 substituted = Fsubstitute_in_file_name (tem);
2037 if (! EQ (substituted, tem))
2039 tem = substituted;
2040 del_range (prompt_end_charpos, PT);
2041 Finsert (1, &tem);
2044 buffer_nchars = SCHARS (tem); /* # chars in what we completed. */
2045 completion_nchars = SCHARS (completion);
2046 i = buffer_nchars - completion_nchars;
2047 if (i > 0
2049 (tem1 = Fcompare_strings (tem, make_number (0),
2050 make_number (buffer_nchars),
2051 completion, make_number (0),
2052 make_number (buffer_nchars),
2053 completion_ignore_case ? Qt : Qnil),
2054 ! EQ (tem1, Qt)))
2056 int start_pos;
2058 /* Make buffer (before point) contain the longest match
2059 of TEM's tail and COMPLETION's head. */
2060 if (i <= 0) i = 1;
2061 start_pos= i;
2062 buffer_nchars -= i;
2063 while (i > 0)
2065 tem1 = Fcompare_strings (tem, make_number (start_pos), Qnil,
2066 completion, make_number (0),
2067 make_number (buffer_nchars),
2068 completion_ignore_case ? Qt : Qnil);
2069 start_pos++;
2070 if (EQ (tem1, Qt))
2071 break;
2072 i++;
2073 buffer_nchars--;
2075 del_range (start_pos, start_pos + buffer_nchars);
2077 UNGCPRO;
2079 #endif /* Rewritten code */
2082 int prompt_end_bytepos;
2083 prompt_end_bytepos = CHAR_TO_BYTE (prompt_end_charpos);
2084 i = PT - prompt_end_charpos;
2085 i_byte = PT_BYTE - prompt_end_bytepos;
2088 /* If completion finds next char not unique,
2089 consider adding a space or a hyphen. */
2090 if (i == SCHARS (completion))
2092 GCPRO1 (completion);
2093 tem = Ftry_completion (concat2 (minibuffer_completion_contents (),
2094 build_string (" ")),
2095 Vminibuffer_completion_table,
2096 Vminibuffer_completion_predicate);
2097 UNGCPRO;
2099 if (STRINGP (tem))
2100 completion = tem;
2101 else
2103 GCPRO1 (completion);
2104 tem =
2105 Ftry_completion (concat2 (minibuffer_completion_contents (),
2106 build_string ("-")),
2107 Vminibuffer_completion_table,
2108 Vminibuffer_completion_predicate);
2109 UNGCPRO;
2111 if (STRINGP (tem))
2112 completion = tem;
2116 /* Now find first word-break in the stuff found by completion.
2117 i gets index in string of where to stop completing. */
2119 int len, c;
2120 int bytes = SBYTES (completion);
2121 completion_string = SDATA (completion);
2122 for (; i_byte < SBYTES (completion); i_byte += len, i++)
2124 c = STRING_CHAR_AND_LENGTH (completion_string + i_byte,
2125 bytes - i_byte,
2126 len);
2127 if (SYNTAX (c) != Sword)
2129 i_byte += len;
2130 i++;
2131 break;
2136 /* If got no characters, print help for user. */
2138 if (i == PT - prompt_end_charpos)
2140 if (!NILP (Vcompletion_auto_help))
2141 Fminibuffer_completion_help ();
2142 return Qnil;
2145 /* Otherwise insert in minibuffer the chars we got */
2147 if (! NILP (Vminibuffer_completing_file_name)
2148 && SREF (completion, SBYTES (completion) - 1) == '/'
2149 && PT < ZV
2150 && FETCH_CHAR (PT_BYTE) == '/')
2152 del_range (prompt_end_charpos, PT + 1);
2154 else
2155 del_range (prompt_end_charpos, PT);
2157 insert_from_string (completion, 0, 0, i, i_byte, 1);
2158 return Qt;
2161 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
2162 1, 1, 0,
2163 doc: /* Display the list of completions, COMPLETIONS, using `standard-output'.
2164 Each element may be just a symbol or string
2165 or may be a list of two strings to be printed as if concatenated.
2166 `standard-output' must be a buffer.
2167 The actual completion alternatives, as inserted, are given `mouse-face'
2168 properties of `highlight'.
2169 At the end, this runs the normal hook `completion-setup-hook'.
2170 It can find the completion buffer in `standard-output'. */)
2171 (completions)
2172 Lisp_Object completions;
2174 Lisp_Object tail, elt;
2175 register int i;
2176 int column = 0;
2177 struct gcpro gcpro1, gcpro2;
2178 struct buffer *old = current_buffer;
2179 int first = 1;
2181 /* Note that (when it matters) every variable
2182 points to a non-string that is pointed to by COMPLETIONS,
2183 except for ELT. ELT can be pointing to a string
2184 when terpri or Findent_to calls a change hook. */
2185 elt = Qnil;
2186 GCPRO2 (completions, elt);
2188 if (BUFFERP (Vstandard_output))
2189 set_buffer_internal (XBUFFER (Vstandard_output));
2191 if (NILP (completions))
2192 write_string ("There are no possible completions of what you have typed.",
2193 -1);
2194 else
2196 write_string ("Possible completions are:", -1);
2197 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
2199 Lisp_Object tem, string;
2200 int length;
2201 Lisp_Object startpos, endpos;
2203 startpos = Qnil;
2205 elt = Fcar (tail);
2206 /* Compute the length of this element. */
2207 if (CONSP (elt))
2209 tem = XCAR (elt);
2210 CHECK_STRING (tem);
2211 length = SCHARS (tem);
2213 tem = Fcar (XCDR (elt));
2214 CHECK_STRING (tem);
2215 length += SCHARS (tem);
2217 else
2219 CHECK_STRING (elt);
2220 length = SCHARS (elt);
2223 /* This does a bad job for narrower than usual windows.
2224 Sadly, the window it will appear in is not known
2225 until after the text has been made. */
2227 if (BUFFERP (Vstandard_output))
2228 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2230 /* If the previous completion was very wide,
2231 or we have two on this line already,
2232 don't put another on the same line. */
2233 if (column > 33 || first
2234 /* If this is really wide, don't put it second on a line. */
2235 || (column > 0 && length > 45))
2237 Fterpri (Qnil);
2238 column = 0;
2240 /* Otherwise advance to column 35. */
2241 else
2243 if (BUFFERP (Vstandard_output))
2245 tem = Findent_to (make_number (35), make_number (2));
2247 column = XINT (tem);
2249 else
2253 write_string (" ", -1);
2254 column++;
2256 while (column < 35);
2260 if (BUFFERP (Vstandard_output))
2262 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2263 Fset_text_properties (startpos, endpos,
2264 Qnil, Vstandard_output);
2267 /* Output this element.
2268 If necessary, convert it to unibyte or to multibyte first. */
2269 if (CONSP (elt))
2270 string = Fcar (elt);
2271 else
2272 string = elt;
2273 if (NILP (current_buffer->enable_multibyte_characters)
2274 && STRING_MULTIBYTE (string))
2275 string = Fstring_make_unibyte (string);
2276 else if (!NILP (current_buffer->enable_multibyte_characters)
2277 && !STRING_MULTIBYTE (string))
2278 string = Fstring_make_multibyte (string);
2280 if (BUFFERP (Vstandard_output))
2282 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2284 Fprinc (string, Qnil);
2286 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2288 Fput_text_property (startpos, endpos,
2289 Qmouse_face, intern ("highlight"),
2290 Vstandard_output);
2292 else
2294 Fprinc (string, Qnil);
2297 /* Output the annotation for this element. */
2298 if (CONSP (elt))
2300 if (BUFFERP (Vstandard_output))
2302 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2304 Fprinc (Fcar (Fcdr (elt)), Qnil);
2306 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2308 Fset_text_properties (startpos, endpos, Qnil,
2309 Vstandard_output);
2311 else
2313 Fprinc (Fcar (Fcdr (elt)), Qnil);
2318 /* Update COLUMN for what we have output. */
2319 column += length;
2321 /* If output is to a buffer, recompute COLUMN in a way
2322 that takes account of character widths. */
2323 if (BUFFERP (Vstandard_output))
2325 tem = Fcurrent_column ();
2326 column = XINT (tem);
2329 first = 0;
2333 UNGCPRO;
2335 if (BUFFERP (Vstandard_output))
2336 set_buffer_internal (old);
2338 if (!NILP (Vrun_hooks))
2339 call1 (Vrun_hooks, intern ("completion-setup-hook"));
2341 return Qnil;
2344 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
2345 0, 0, "",
2346 doc: /* Display a list of possible completions of the current minibuffer contents. */)
2349 Lisp_Object completions;
2351 message ("Making completion list...");
2352 completions = Fall_completions (minibuffer_completion_contents (),
2353 Vminibuffer_completion_table,
2354 Vminibuffer_completion_predicate,
2355 Qt);
2356 clear_message (1, 0);
2358 if (NILP (completions))
2360 bitch_at_user ();
2361 temp_echo_area_glyphs (build_string (" [No completions]"));
2363 else
2364 internal_with_output_to_temp_buffer ("*Completions*",
2365 Fdisplay_completion_list,
2366 Fsort (completions, Qstring_lessp));
2367 return Qnil;
2370 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
2371 doc: /* Terminate minibuffer input. */)
2374 if (INTEGERP (last_command_char))
2375 internal_self_insert (XINT (last_command_char), 0);
2376 else
2377 bitch_at_user ();
2379 return Fthrow (Qexit, Qnil);
2382 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
2383 doc: /* Terminate this minibuffer argument. */)
2386 return Fthrow (Qexit, Qnil);
2389 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
2390 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
2393 return make_number (minibuf_level);
2396 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
2397 doc: /* Return the prompt string of the currently-active minibuffer.
2398 If no minibuffer is active, return nil. */)
2401 return Fcopy_sequence (minibuf_prompt);
2405 /* Temporarily display STRING at the end of the current
2406 minibuffer contents. This is used to display things like
2407 "[No Match]" when the user requests a completion for a prefix
2408 that has no possible completions, and other quick, unobtrusive
2409 messages. */
2411 void
2412 temp_echo_area_glyphs (string)
2413 Lisp_Object string;
2415 int osize = ZV;
2416 int osize_byte = ZV_BYTE;
2417 int opoint = PT;
2418 int opoint_byte = PT_BYTE;
2419 Lisp_Object oinhibit;
2420 oinhibit = Vinhibit_quit;
2422 /* Clear out any old echo-area message to make way for our new thing. */
2423 message (0);
2425 SET_PT_BOTH (osize, osize_byte);
2426 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 0);
2427 SET_PT_BOTH (opoint, opoint_byte);
2428 Vinhibit_quit = Qt;
2429 Fsit_for (make_number (2), Qnil, Qnil);
2430 del_range_both (osize, osize_byte, ZV, ZV_BYTE, 1);
2431 SET_PT_BOTH (opoint, opoint_byte);
2432 if (!NILP (Vquit_flag))
2434 Vquit_flag = Qnil;
2435 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
2437 Vinhibit_quit = oinhibit;
2440 DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
2441 1, 1, 0,
2442 doc: /* Temporarily display STRING at the end of the minibuffer.
2443 The text is displayed for two seconds,
2444 or until the next input event arrives, whichever comes first. */)
2445 (string)
2446 Lisp_Object string;
2448 CHECK_STRING (string);
2449 temp_echo_area_glyphs (string);
2450 return Qnil;
2453 void
2454 init_minibuf_once ()
2456 Vminibuffer_list = Qnil;
2457 staticpro (&Vminibuffer_list);
2460 void
2461 syms_of_minibuf ()
2463 minibuf_level = 0;
2464 minibuf_prompt = Qnil;
2465 staticpro (&minibuf_prompt);
2467 minibuf_save_list = Qnil;
2468 staticpro (&minibuf_save_list);
2470 Qread_file_name_internal = intern ("read-file-name-internal");
2471 staticpro (&Qread_file_name_internal);
2473 Qminibuffer_default = intern ("minibuffer-default");
2474 staticpro (&Qminibuffer_default);
2475 Fset (Qminibuffer_default, Qnil);
2477 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
2478 staticpro (&Qminibuffer_completion_table);
2480 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
2481 staticpro (&Qminibuffer_completion_confirm);
2483 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
2484 staticpro (&Qminibuffer_completion_predicate);
2486 staticpro (&last_exact_completion);
2487 last_exact_completion = Qnil;
2489 staticpro (&last_minibuf_string);
2490 last_minibuf_string = Qnil;
2492 Quser_variable_p = intern ("user-variable-p");
2493 staticpro (&Quser_variable_p);
2495 Qminibuffer_history = intern ("minibuffer-history");
2496 staticpro (&Qminibuffer_history);
2498 Qbuffer_name_history = intern ("buffer-name-history");
2499 staticpro (&Qbuffer_name_history);
2500 Fset (Qbuffer_name_history, Qnil);
2502 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
2503 staticpro (&Qminibuffer_setup_hook);
2505 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
2506 staticpro (&Qminibuffer_exit_hook);
2508 Qhistory_length = intern ("history-length");
2509 staticpro (&Qhistory_length);
2511 Qcurrent_input_method = intern ("current-input-method");
2512 staticpro (&Qcurrent_input_method);
2514 Qactivate_input_method = intern ("activate-input-method");
2515 staticpro (&Qactivate_input_method);
2517 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2518 doc: /* If this is non-nil, `read-buffer' does its work by calling this function. */);
2519 Vread_buffer_function = Qnil;
2521 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2522 doc: /* Normal hook run just after entry to minibuffer. */);
2523 Vminibuffer_setup_hook = Qnil;
2525 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2526 doc: /* Normal hook run just after exit from minibuffer. */);
2527 Vminibuffer_exit_hook = Qnil;
2529 DEFVAR_LISP ("history-length", &Vhistory_length,
2530 doc: /* *Maximum length for history lists before truncation takes place.
2531 A number means that length; t means infinite. Truncation takes place
2532 just after a new element is inserted. Setting the history-length
2533 property of a history variable overrides this default. */);
2534 XSETFASTINT (Vhistory_length, 30);
2536 DEFVAR_LISP ("completion-auto-help", &Vcompletion_auto_help,
2537 doc: /* *Non-nil means automatically provide help for invalid completion input. */);
2538 Vcompletion_auto_help = Qt;
2540 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
2541 doc: /* Non-nil means don't consider case significant in completion. */);
2542 completion_ignore_case = 0;
2544 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2545 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
2546 This variable makes a difference whenever the minibuffer window is active. */);
2547 enable_recursive_minibuffers = 0;
2549 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2550 doc: /* Alist or obarray used for completion in the minibuffer.
2551 This becomes the ALIST argument to `try-completion' and `all-completion'.
2553 The value may alternatively be a function, which is given three arguments:
2554 STRING, the current buffer contents;
2555 PREDICATE, the predicate for filtering possible matches;
2556 CODE, which says what kind of things to do.
2557 CODE can be nil, t or `lambda'.
2558 nil means to return the best completion of STRING, or nil if there is none.
2559 t means to return a list of all possible completions of STRING.
2560 `lambda' means to return t if STRING is a valid completion as it stands. */);
2561 Vminibuffer_completion_table = Qnil;
2563 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2564 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
2565 Vminibuffer_completion_predicate = Qnil;
2567 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
2568 doc: /* Non-nil means to demand confirmation of completion before exiting minibuffer. */);
2569 Vminibuffer_completion_confirm = Qnil;
2571 DEFVAR_LISP ("minibuffer-completing-file-name",
2572 &Vminibuffer_completing_file_name,
2573 doc: /* Non-nil means completing file names. */);
2574 Vminibuffer_completing_file_name = Qnil;
2576 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2577 doc: /* Value that `help-form' takes on inside the minibuffer. */);
2578 Vminibuffer_help_form = Qnil;
2580 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2581 doc: /* History list symbol to add minibuffer values to.
2582 Each string of minibuffer input, as it appears on exit from the minibuffer,
2583 is added with
2584 (set minibuffer-history-variable
2585 (cons STRING (symbol-value minibuffer-history-variable))) */);
2586 XSETFASTINT (Vminibuffer_history_variable, 0);
2588 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2589 doc: /* Current position of redoing in the history list. */);
2590 Vminibuffer_history_position = Qnil;
2592 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2593 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
2594 Some uses of the echo area also raise that frame (since they use it too). */);
2595 minibuffer_auto_raise = 0;
2597 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
2598 doc: /* List of regexps that should restrict possible completions. */);
2599 Vcompletion_regexp_list = Qnil;
2601 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2602 &minibuffer_allow_text_properties,
2603 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
2604 This also affects `read-string', but it does not affect `read-minibuffer',
2605 `read-no-blanks-input', or any of the functions that do minibuffer input
2606 with completion; they always discard text properties. */);
2607 minibuffer_allow_text_properties = 0;
2609 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2610 doc: /* Text properties that are added to minibuffer prompts.
2611 These are in addition to the basic `field' property, and stickiness
2612 properties. */);
2613 /* We use `intern' here instead of Qread_only to avoid
2614 initialization-order problems. */
2615 Vminibuffer_prompt_properties
2616 = Fcons (intern ("read-only"), Fcons (Qt, Qnil));
2618 defsubr (&Sset_minibuffer_window);
2619 defsubr (&Sread_from_minibuffer);
2620 defsubr (&Seval_minibuffer);
2621 defsubr (&Sread_minibuffer);
2622 defsubr (&Sread_string);
2623 defsubr (&Sread_command);
2624 defsubr (&Sread_variable);
2625 defsubr (&Sread_buffer);
2626 defsubr (&Sread_no_blanks_input);
2627 defsubr (&Sminibuffer_depth);
2628 defsubr (&Sminibuffer_prompt);
2630 defsubr (&Sminibufferp);
2631 defsubr (&Sminibuffer_prompt_end);
2632 defsubr (&Sminibuffer_contents);
2633 defsubr (&Sminibuffer_contents_no_properties);
2634 defsubr (&Sdelete_minibuffer_contents);
2636 defsubr (&Stry_completion);
2637 defsubr (&Sall_completions);
2638 defsubr (&Stest_completion);
2639 defsubr (&Sassoc_string);
2640 defsubr (&Scompleting_read);
2641 defsubr (&Sminibuffer_complete);
2642 defsubr (&Sminibuffer_complete_word);
2643 defsubr (&Sminibuffer_complete_and_exit);
2644 defsubr (&Sdisplay_completion_list);
2645 defsubr (&Sminibuffer_completion_help);
2647 defsubr (&Sself_insert_and_exit);
2648 defsubr (&Sexit_minibuffer);
2650 defsubr (&Sminibuffer_message);
2653 void
2654 keys_of_minibuf ()
2656 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
2657 "abort-recursive-edit");
2658 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
2659 "exit-minibuffer");
2660 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
2661 "exit-minibuffer");
2663 initial_define_key (Vminibuffer_local_ns_map, ' ',
2664 "exit-minibuffer");
2665 initial_define_key (Vminibuffer_local_ns_map, '\t',
2666 "exit-minibuffer");
2667 initial_define_key (Vminibuffer_local_ns_map, '?',
2668 "self-insert-and-exit");
2670 initial_define_key (Vminibuffer_local_completion_map, '\t',
2671 "minibuffer-complete");
2672 initial_define_key (Vminibuffer_local_completion_map, ' ',
2673 "minibuffer-complete-word");
2674 initial_define_key (Vminibuffer_local_completion_map, '?',
2675 "minibuffer-completion-help");
2677 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
2678 "minibuffer-complete-and-exit");
2679 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
2680 "minibuffer-complete-and-exit");