(comint-quote-filename): Correctly handle backslash
[emacs.git] / src / minibuf.c
blob4c98f7ba70807850bce823d6fb68b372f004511d
1 /* Minibuffer input and completion.
2 Copyright (C) 1985,86,93,94,95,96,97,98,99,2000,01,03
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include <stdio.h>
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 and initial contents INITIAL,
408 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. INITIAL should be a string or a
411 cons of a string and an integer. BACKUP_N should be <= 0, or
412 Qnil, which is equivalent to 0. If INITIAL is a cons, BACKUP_N is
413 ignored and replaced with an integer that puts point N characters
414 from the beginning of INITIAL, where N is the CDR of INITIAL, or at
415 the beginning of INITIAL if N <= 0.
417 Normally return the result as a string (the text that was read),
418 but if EXPFLAG is nonzero, read it and return the object read.
419 If HISTVAR is given, save the value read on that history only if it doesn't
420 match the front of that history list exactly. The value is pushed onto
421 the list as the string that was read.
423 DEFALT specifies the default value for the sake of history commands.
425 If ALLOW_PROPS is nonzero, we do not throw away text properties.
427 if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
428 current input method. */
430 static Lisp_Object
431 read_minibuf (map, initial, prompt, backup_n, expflag,
432 histvar, histpos, defalt, allow_props, inherit_input_method)
433 Lisp_Object map;
434 Lisp_Object initial;
435 Lisp_Object prompt;
436 Lisp_Object backup_n;
437 int expflag;
438 Lisp_Object histvar;
439 Lisp_Object histpos;
440 Lisp_Object defalt;
441 int allow_props;
442 int inherit_input_method;
444 Lisp_Object val;
445 int count = SPECPDL_INDEX ();
446 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
447 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
448 Lisp_Object enable_multibyte;
449 int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
451 /* String to add to the history. */
452 Lisp_Object histstring;
454 extern Lisp_Object Qfront_sticky;
455 extern Lisp_Object Qrear_nonsticky;
457 specbind (Qminibuffer_default, defalt);
459 single_kboard_state ();
460 #ifdef HAVE_X_WINDOWS
461 if (display_hourglass_p)
462 cancel_hourglass ();
463 #endif
465 if (!NILP (initial))
467 if (CONSP (initial))
469 backup_n = Fcdr (initial);
470 initial = Fcar (initial);
471 CHECK_STRING (initial);
472 if (!NILP (backup_n))
474 CHECK_NUMBER (backup_n);
475 /* Convert to distance from end of input. */
476 if (XINT (backup_n) < 1)
477 /* A number too small means the beginning of the string. */
478 pos = - SCHARS (initial);
479 else
480 pos = XINT (backup_n) - 1 - SCHARS (initial);
483 else
484 CHECK_STRING (initial);
486 val = Qnil;
487 ambient_dir = current_buffer->directory;
488 input_method = Qnil;
489 enable_multibyte = Qnil;
491 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
492 store them away before we can GC. Don't need to protect
493 BACKUP_N because we use the value only if it is an integer. */
494 GCPRO5 (map, initial, val, ambient_dir, input_method);
496 if (!STRINGP (prompt))
497 prompt = empty_string;
499 if (!enable_recursive_minibuffers
500 && minibuf_level > 0)
502 if (EQ (selected_window, minibuf_window))
503 error ("Command attempted to use minibuffer while in minibuffer");
504 else
505 /* If we're in another window, cancel the minibuffer that's active. */
506 Fthrow (Qexit,
507 build_string ("Command attempted to use minibuffer while in minibuffer"));
510 if (noninteractive)
512 val = read_minibuf_noninteractive (map, initial, prompt,
513 make_number (pos),
514 expflag, histvar, histpos, defalt,
515 allow_props, inherit_input_method);
516 UNGCPRO;
517 return unbind_to (count, val);
520 /* Choose the minibuffer window and frame, and take action on them. */
522 choose_minibuf_frame ();
524 record_unwind_protect (choose_minibuf_frame_1, Qnil);
526 record_unwind_protect (Fset_window_configuration,
527 Fcurrent_window_configuration (Qnil));
529 /* If the minibuffer window is on a different frame, save that
530 frame's configuration too. */
531 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
532 if (!EQ (mini_frame, selected_frame))
533 record_unwind_protect (Fset_window_configuration,
534 Fcurrent_window_configuration (mini_frame));
536 /* If the minibuffer is on an iconified or invisible frame,
537 make it visible now. */
538 Fmake_frame_visible (mini_frame);
540 if (minibuffer_auto_raise)
541 Fraise_frame (mini_frame);
543 /* We have to do this after saving the window configuration
544 since that is what restores the current buffer. */
546 /* Arrange to restore a number of minibuffer-related variables.
547 We could bind each variable separately, but that would use lots of
548 specpdl slots. */
549 minibuf_save_list
550 = Fcons (Voverriding_local_map,
551 Fcons (minibuf_window, minibuf_save_list));
552 minibuf_save_list
553 = Fcons (minibuf_prompt,
554 Fcons (make_number (minibuf_prompt_width),
555 Fcons (Vhelp_form,
556 Fcons (Vcurrent_prefix_arg,
557 Fcons (Vminibuffer_history_position,
558 Fcons (Vminibuffer_history_variable,
559 minibuf_save_list))))));
561 record_unwind_protect (read_minibuf_unwind, Qnil);
562 minibuf_level++;
564 /* Now that we can restore all those variables, start changing them. */
566 minibuf_prompt_width = 0;
567 minibuf_prompt = Fcopy_sequence (prompt);
568 Vminibuffer_history_position = histpos;
569 Vminibuffer_history_variable = histvar;
570 Vhelp_form = Vminibuffer_help_form;
572 if (inherit_input_method)
574 /* `current-input-method' is buffer local. So, remember it in
575 INPUT_METHOD before changing the current buffer. */
576 input_method = Fsymbol_value (Qcurrent_input_method);
577 enable_multibyte = current_buffer->enable_multibyte_characters;
580 /* Switch to the minibuffer. */
582 minibuffer = get_minibuffer (minibuf_level);
583 Fset_buffer (minibuffer);
585 /* The current buffer's default directory is usually the right thing
586 for our minibuffer here. However, if you're typing a command at
587 a minibuffer-only frame when minibuf_level is zero, then buf IS
588 the current_buffer, so reset_buffer leaves buf's default
589 directory unchanged. This is a bummer when you've just started
590 up Emacs and buf's default directory is Qnil. Here's a hack; can
591 you think of something better to do? Find another buffer with a
592 better directory, and use that one instead. */
593 if (STRINGP (ambient_dir))
594 current_buffer->directory = ambient_dir;
595 else
597 Lisp_Object buf_list;
599 for (buf_list = Vbuffer_alist;
600 CONSP (buf_list);
601 buf_list = XCDR (buf_list))
603 Lisp_Object other_buf;
605 other_buf = XCDR (XCAR (buf_list));
606 if (STRINGP (XBUFFER (other_buf)->directory))
608 current_buffer->directory = XBUFFER (other_buf)->directory;
609 break;
614 if (!EQ (mini_frame, selected_frame))
615 Fredirect_frame_focus (selected_frame, mini_frame);
617 Vminibuf_scroll_window = selected_window;
618 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
619 minibuf_selected_window = selected_window;
620 Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
621 Fselect_window (minibuf_window, Qnil);
622 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
624 Fmake_local_variable (Qprint_escape_newlines);
625 print_escape_newlines = 1;
627 /* Erase the buffer. */
629 int count1 = SPECPDL_INDEX ();
630 specbind (Qinhibit_read_only, Qt);
631 specbind (Qinhibit_modification_hooks, Qt);
632 Ferase_buffer ();
633 unbind_to (count1, Qnil);
636 if (!NILP (current_buffer->enable_multibyte_characters)
637 && ! STRING_MULTIBYTE (minibuf_prompt))
638 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
640 /* Insert the prompt, record where it ends. */
641 Finsert (1, &minibuf_prompt);
642 if (PT > BEG)
644 Fput_text_property (make_number (BEG), make_number (PT),
645 Qfront_sticky, Qt, Qnil);
646 Fput_text_property (make_number (BEG), make_number (PT),
647 Qrear_nonsticky, Qt, Qnil);
648 Fput_text_property (make_number (BEG), make_number (PT),
649 Qfield, Qt, Qnil);
650 Fadd_text_properties (make_number (BEG), make_number (PT),
651 Vminibuffer_prompt_properties, Qnil);
654 minibuf_prompt_width = (int) current_column (); /* iftc */
656 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
657 if (inherit_input_method)
658 current_buffer->enable_multibyte_characters = enable_multibyte;
660 /* Put in the initial input. */
661 if (!NILP (initial))
663 Finsert (1, &initial);
664 Fforward_char (make_number (pos));
667 clear_message (1, 1);
668 current_buffer->keymap = map;
670 /* Turn on an input method stored in INPUT_METHOD if any. */
671 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
672 call1 (Qactivate_input_method, input_method);
674 /* Run our hook, but not if it is empty.
675 (run-hooks would do nothing if it is empty,
676 but it's important to save time here in the usual case.) */
677 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
678 && !NILP (Vrun_hooks))
679 call1 (Vrun_hooks, Qminibuffer_setup_hook);
681 /* Don't allow the user to undo past this point. */
682 current_buffer->undo_list = Qnil;
684 recursive_edit_1 ();
686 /* If cursor is on the minibuffer line,
687 show the user we have exited by putting it in column 0. */
688 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
689 && !noninteractive)
691 XWINDOW (minibuf_window)->cursor.hpos = 0;
692 XWINDOW (minibuf_window)->cursor.x = 0;
693 XWINDOW (minibuf_window)->must_be_updated_p = 1;
694 update_frame (XFRAME (selected_frame), 1, 1);
695 if (rif && rif->flush_display)
696 rif->flush_display (XFRAME (XWINDOW (minibuf_window)->frame));
699 /* Make minibuffer contents into a string. */
700 Fset_buffer (minibuffer);
701 if (allow_props)
702 val = Fminibuffer_contents ();
703 else
704 val = Fminibuffer_contents_no_properties ();
706 /* VAL is the string of minibuffer text. */
708 last_minibuf_string = val;
710 /* Choose the string to add to the history. */
711 if (SCHARS (val) != 0)
712 histstring = val;
713 else if (STRINGP (defalt))
714 histstring = defalt;
715 else
716 histstring = Qnil;
718 /* Add the value to the appropriate history list, if any. */
719 if (SYMBOLP (Vminibuffer_history_variable)
720 && !NILP (histstring))
722 /* If the caller wanted to save the value read on a history list,
723 then do so if the value is not already the front of the list. */
724 Lisp_Object histval;
726 /* If variable is unbound, make it nil. */
727 if (EQ (SYMBOL_VALUE (Vminibuffer_history_variable), Qunbound))
728 Fset (Vminibuffer_history_variable, Qnil);
730 histval = Fsymbol_value (Vminibuffer_history_variable);
732 /* The value of the history variable must be a cons or nil. Other
733 values are unacceptable. We silently ignore these values. */
735 if (NILP (histval)
736 || (CONSP (histval)
737 /* Don't duplicate the most recent entry in the history. */
738 && NILP (Fequal (histstring, Fcar (histval)))))
740 Lisp_Object length;
742 histval = Fcons (histstring, histval);
743 Fset (Vminibuffer_history_variable, histval);
745 /* Truncate if requested. */
746 length = Fget (Vminibuffer_history_variable, Qhistory_length);
747 if (NILP (length)) length = Vhistory_length;
748 if (INTEGERP (length))
750 if (XINT (length) <= 0)
751 Fset (Vminibuffer_history_variable, Qnil);
752 else
754 Lisp_Object temp;
756 temp = Fnthcdr (Fsub1 (length), histval);
757 if (CONSP (temp)) Fsetcdr (temp, Qnil);
763 /* If Lisp form desired instead of string, parse it. */
764 if (expflag)
765 val = string_to_object (val, defalt);
767 /* The appropriate frame will get selected
768 in set-window-configuration. */
769 UNGCPRO;
770 return unbind_to (count, val);
773 /* Return a buffer to be used as the minibuffer at depth `depth'.
774 depth = 0 is the lowest allowed argument, and that is the value
775 used for nonrecursive minibuffer invocations */
777 Lisp_Object
778 get_minibuffer (depth)
779 int depth;
781 Lisp_Object tail, num, buf;
782 char name[24];
783 extern Lisp_Object nconc2 ();
785 XSETFASTINT (num, depth);
786 tail = Fnthcdr (num, Vminibuffer_list);
787 if (NILP (tail))
789 tail = Fcons (Qnil, Qnil);
790 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
792 buf = Fcar (tail);
793 if (NILP (buf) || NILP (XBUFFER (buf)->name))
795 sprintf (name, " *Minibuf-%d*", depth);
796 buf = Fget_buffer_create (build_string (name));
798 /* Although the buffer's name starts with a space, undo should be
799 enabled in it. */
800 Fbuffer_enable_undo (buf);
802 XSETCAR (tail, buf);
804 else
806 int count = SPECPDL_INDEX ();
807 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
808 have to empty the list, otherwise we end up with overlays that
809 think they belong to this buffer while the buffer doesn't know about
810 them any more. */
811 delete_all_overlays (XBUFFER (buf));
812 reset_buffer (XBUFFER (buf));
813 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
814 Fset_buffer (buf);
815 Fkill_all_local_variables ();
816 unbind_to (count, Qnil);
819 return buf;
822 /* This function is called on exiting minibuffer, whether normally or
823 not, and it restores the current window, buffer, etc. */
825 static Lisp_Object
826 read_minibuf_unwind (data)
827 Lisp_Object data;
829 Lisp_Object old_deactivate_mark;
830 Lisp_Object window;
832 /* We are exiting the minibuffer one way or the other,
833 so run the hook. */
834 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
835 && !NILP (Vrun_hooks))
836 safe_run_hooks (Qminibuffer_exit_hook);
838 /* If this was a recursive minibuffer,
839 tie the minibuffer window back to the outer level minibuffer buffer. */
840 minibuf_level--;
842 window = minibuf_window;
843 /* To keep things predictable, in case it matters, let's be in the
844 minibuffer when we reset the relevant variables. */
845 Fset_buffer (XWINDOW (window)->buffer);
847 /* Restore prompt, etc, from outer minibuffer level. */
848 minibuf_prompt = Fcar (minibuf_save_list);
849 minibuf_save_list = Fcdr (minibuf_save_list);
850 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
851 minibuf_save_list = Fcdr (minibuf_save_list);
852 Vhelp_form = Fcar (minibuf_save_list);
853 minibuf_save_list = Fcdr (minibuf_save_list);
854 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
855 minibuf_save_list = Fcdr (minibuf_save_list);
856 Vminibuffer_history_position = Fcar (minibuf_save_list);
857 minibuf_save_list = Fcdr (minibuf_save_list);
858 Vminibuffer_history_variable = Fcar (minibuf_save_list);
859 minibuf_save_list = Fcdr (minibuf_save_list);
860 Voverriding_local_map = Fcar (minibuf_save_list);
861 minibuf_save_list = Fcdr (minibuf_save_list);
862 #if 0
863 temp = Fcar (minibuf_save_list);
864 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
865 minibuf_window = temp;
866 #endif
867 minibuf_save_list = Fcdr (minibuf_save_list);
869 /* Erase the minibuffer we were using at this level. */
871 int count = SPECPDL_INDEX ();
872 /* Prevent error in erase-buffer. */
873 specbind (Qinhibit_read_only, Qt);
874 specbind (Qinhibit_modification_hooks, Qt);
875 old_deactivate_mark = Vdeactivate_mark;
876 Ferase_buffer ();
877 Vdeactivate_mark = old_deactivate_mark;
878 unbind_to (count, Qnil);
881 /* When we get to the outmost level, make sure we resize the
882 mini-window back to its normal size. */
883 if (minibuf_level == 0)
884 resize_mini_window (XWINDOW (window), 0);
886 /* Make sure minibuffer window is erased, not ignored. */
887 windows_or_buffers_changed++;
888 XSETFASTINT (XWINDOW (window)->last_modified, 0);
889 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
890 return Qnil;
894 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
895 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
896 If optional second arg INITIAL-CONTENTS is non-nil, it is a string
897 to be inserted into the minibuffer before reading input.
898 If INITIAL-CONTENTS is (STRING . POSITION), the initial input
899 is STRING, but point is placed at position POSITION in the minibuffer.
900 Third arg KEYMAP is a keymap to use whilst reading;
901 if omitted or nil, the default is `minibuffer-local-map'.
902 If fourth arg READ is non-nil, then interpret the result as a Lisp object
903 and return that object:
904 in other words, do `(car (read-from-string INPUT-STRING))'
905 Fifth arg HIST, if non-nil, specifies a history list
906 and optionally the initial position in the list.
907 It can be a symbol, which is the history list variable to use,
908 or it can be a cons cell (HISTVAR . HISTPOS).
909 In that case, HISTVAR is the history list variable to use,
910 and HISTPOS is the initial position (the position in the list
911 which INITIAL-CONTENTS corresponds to).
912 Positions are counted starting from 1 at the beginning of the list.
913 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
914 for history commands; but, unless READ is non-nil, `read-from-minibuffer'
915 does NOT return DEFAULT-VALUE if the user enters empty input! It returns
916 the empty string.
917 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
918 the current input method and the setting of `enable-multibyte-characters'.
919 If the variable `minibuffer-allow-text-properties' is non-nil,
920 then the string which is returned includes whatever text properties
921 were present in the minibuffer. Otherwise the value has no text properties. */)
922 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
923 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
924 Lisp_Object inherit_input_method;
926 Lisp_Object histvar, histpos, val;
927 struct gcpro gcpro1;
929 CHECK_STRING (prompt);
930 if (NILP (keymap))
931 keymap = Vminibuffer_local_map;
932 else
933 keymap = get_keymap (keymap, 1, 0);
935 if (SYMBOLP (hist))
937 histvar = hist;
938 histpos = Qnil;
940 else
942 histvar = Fcar_safe (hist);
943 histpos = Fcdr_safe (hist);
945 if (NILP (histvar))
946 histvar = Qminibuffer_history;
947 if (NILP (histpos))
948 XSETFASTINT (histpos, 0);
950 GCPRO1 (default_value);
951 val = read_minibuf (keymap, initial_contents, prompt,
952 Qnil, !NILP (read),
953 histvar, histpos, default_value,
954 minibuffer_allow_text_properties,
955 !NILP (inherit_input_method));
956 UNGCPRO;
957 return val;
960 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
961 doc: /* Return a Lisp object read using the minibuffer.
962 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
963 is a string to insert in the minibuffer before reading. */)
964 (prompt, initial_contents)
965 Lisp_Object prompt, initial_contents;
967 CHECK_STRING (prompt);
968 return read_minibuf (Vminibuffer_local_map, initial_contents,
969 prompt, Qnil, 1, Qminibuffer_history,
970 make_number (0), Qnil, 0, 0);
973 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
974 doc: /* Return value of Lisp expression read using the minibuffer.
975 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
976 is a string to insert in the minibuffer before reading. */)
977 (prompt, initial_contents)
978 Lisp_Object prompt, initial_contents;
980 return Feval (Fread_minibuffer (prompt, initial_contents));
983 /* Functions that use the minibuffer to read various things. */
985 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
986 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
987 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
988 The third arg HISTORY, if non-nil, specifies a history list
989 and optionally the initial position in the list.
990 See `read-from-minibuffer' for details of HISTORY argument.
991 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
992 for history commands, and as the value to return if the user enters
993 the empty string.
994 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
995 the current input method and the setting of `enable-multibyte-characters'. */)
996 (prompt, initial_input, history, default_value, inherit_input_method)
997 Lisp_Object prompt, initial_input, history, default_value;
998 Lisp_Object inherit_input_method;
1000 Lisp_Object val;
1001 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
1002 Qnil, history, default_value,
1003 inherit_input_method);
1004 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
1005 val = default_value;
1006 return val;
1009 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
1010 doc: /* Read a string from the terminal, not allowing blanks.
1011 Prompt with PROMPT, and provide INITIAL as an initial value of the input string.
1012 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1013 the current input method and the setting of `enable-multibyte-characters'. */)
1014 (prompt, initial, inherit_input_method)
1015 Lisp_Object prompt, initial, inherit_input_method;
1017 CHECK_STRING (prompt);
1018 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
1019 0, Qminibuffer_history, make_number (0), Qnil, 0,
1020 !NILP (inherit_input_method));
1023 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
1024 doc: /* Read the name of a command and return as a symbol.
1025 Prompt with PROMPT. By default, return DEFAULT-VALUE. */)
1026 (prompt, default_value)
1027 Lisp_Object prompt, default_value;
1029 Lisp_Object name, default_string;
1031 if (NILP (default_value))
1032 default_string = Qnil;
1033 else if (SYMBOLP (default_value))
1034 default_string = SYMBOL_NAME (default_value);
1035 else
1036 default_string = default_value;
1038 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1039 Qnil, Qnil, default_string, Qnil);
1040 if (NILP (name))
1041 return name;
1042 return Fintern (name, Qnil);
1045 #ifdef NOTDEF
1046 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
1047 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
1048 Prompt with PROMPT. */)
1049 (prompt)
1050 Lisp_Object prompt;
1052 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
1053 Qnil);
1055 #endif /* NOTDEF */
1057 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
1058 doc: /* Read the name of a user variable and return it as a symbol.
1059 Prompt with PROMPT. By default, return DEFAULT-VALUE.
1060 A user variable is one for which `user-variable-p' returns non-nil. */)
1061 (prompt, default_value)
1062 Lisp_Object prompt, default_value;
1064 Lisp_Object name, default_string;
1066 if (NILP (default_value))
1067 default_string = Qnil;
1068 else if (SYMBOLP (default_value))
1069 default_string = SYMBOL_NAME (default_value);
1070 else
1071 default_string = default_value;
1073 name = Fcompleting_read (prompt, Vobarray,
1074 Quser_variable_p, Qt,
1075 Qnil, Qnil, default_string, Qnil);
1076 if (NILP (name))
1077 return name;
1078 return Fintern (name, Qnil);
1081 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
1082 doc: /* Read the name of a buffer and return as a string.
1083 Prompt with PROMPT.
1084 Optional second arg DEF is value to return if user enters an empty line.
1085 If optional third arg REQUIRE-MATCH is non-nil,
1086 only existing buffer names are allowed. */)
1087 (prompt, def, require_match)
1088 Lisp_Object prompt, def, require_match;
1090 Lisp_Object args[4];
1092 if (BUFFERP (def))
1093 def = XBUFFER (def)->name;
1095 if (NILP (Vread_buffer_function))
1097 if (!NILP (def))
1099 args[0] = build_string ("%s(default %s) ");
1100 args[1] = prompt;
1101 args[2] = def;
1102 prompt = Fformat (3, args);
1105 return Fcompleting_read (prompt, Vbuffer_alist, Qnil,
1106 require_match, Qnil, Qbuffer_name_history,
1107 def, Qnil);
1109 else
1111 args[0] = Vread_buffer_function;
1112 args[1] = prompt;
1113 args[2] = def;
1114 args[3] = require_match;
1115 return Ffuncall(4, args);
1119 static Lisp_Object
1120 minibuf_conform_representation (string, basis)
1121 Lisp_Object string, basis;
1123 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1124 return string;
1126 if (STRING_MULTIBYTE (string))
1127 return Fstring_make_unibyte (string);
1128 else
1129 return Fstring_make_multibyte (string);
1132 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1133 doc: /* Return common substring of all completions of STRING in ALIST.
1134 Each car of each element of ALIST (or each element if it is not a cons cell)
1135 is tested to see if it begins with STRING.
1136 All that match are compared together; the longest initial sequence
1137 common to all matches is returned as a string.
1138 If there is no match at all, nil is returned.
1139 For a unique match which is exact, t is returned.
1141 If ALIST is a hash-table, all the string keys are the possible matches.
1142 If ALIST is an obarray, the names of all symbols in the obarray
1143 are the possible matches.
1145 ALIST can also be a function to do the completion itself.
1146 It receives three arguments: the values STRING, PREDICATE and nil.
1147 Whatever it returns becomes the value of `try-completion'.
1149 If optional third argument PREDICATE is non-nil,
1150 it is used to test each possible match.
1151 The match is a candidate only if PREDICATE returns non-nil.
1152 The argument given to PREDICATE is the alist element
1153 or the symbol from the obarray. If ALIST is a hash-table,
1154 predicate is called with two arguments: the key and the value.
1155 Additionally to this predicate, `completion-regexp-list'
1156 is used to further constrain the set of candidates. */)
1157 (string, alist, predicate)
1158 Lisp_Object string, alist, predicate;
1160 Lisp_Object bestmatch, tail, elt, eltstring;
1161 /* Size in bytes of BESTMATCH. */
1162 int bestmatchsize = 0;
1163 /* These are in bytes, too. */
1164 int compare, matchsize;
1165 int type = HASH_TABLE_P (alist) ? 3
1166 : VECTORP (alist) ? 2
1167 : NILP (alist) || (CONSP (alist)
1168 && (!SYMBOLP (XCAR (alist))
1169 || NILP (XCAR (alist))));
1170 int index = 0, obsize = 0;
1171 int matchcount = 0;
1172 Lisp_Object bucket, zero, end, tem;
1173 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1175 CHECK_STRING (string);
1176 if (type == 0)
1177 return call3 (alist, string, predicate, Qnil);
1179 bestmatch = bucket = Qnil;
1181 /* If ALIST is not a list, set TAIL just for gc pro. */
1182 tail = alist;
1183 if (type == 2)
1185 obsize = XVECTOR (alist)->size;
1186 bucket = XVECTOR (alist)->contents[index];
1189 while (1)
1191 /* Get the next element of the alist, obarray, or hash-table. */
1192 /* Exit the loop if the elements are all used up. */
1193 /* elt gets the alist element or symbol.
1194 eltstring gets the name to check as a completion. */
1196 if (type == 1)
1198 if (!CONSP (tail))
1199 break;
1200 elt = XCAR (tail);
1201 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1202 tail = XCDR (tail);
1204 else if (type == 2)
1206 if (XFASTINT (bucket) != 0)
1208 elt = bucket;
1209 eltstring = Fsymbol_name (elt);
1210 if (XSYMBOL (bucket)->next)
1211 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1212 else
1213 XSETFASTINT (bucket, 0);
1215 else if (++index >= obsize)
1216 break;
1217 else
1219 bucket = XVECTOR (alist)->contents[index];
1220 continue;
1223 else /* if (type == 3) */
1225 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1226 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1227 index++;
1228 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1229 break;
1230 else
1231 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1234 /* Is this element a possible completion? */
1236 if (STRINGP (eltstring)
1237 && SCHARS (string) <= SCHARS (eltstring)
1238 && (tem = Fcompare_strings (eltstring, make_number (0),
1239 make_number (SCHARS (string)),
1240 string, make_number (0), Qnil,
1241 completion_ignore_case ?Qt : Qnil),
1242 EQ (Qt, tem)))
1244 /* Yes. */
1245 Lisp_Object regexps;
1246 Lisp_Object zero;
1247 XSETFASTINT (zero, 0);
1249 /* Ignore this element if it fails to match all the regexps. */
1250 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1251 regexps = XCDR (regexps))
1253 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1254 if (NILP (tem))
1255 break;
1257 if (CONSP (regexps))
1258 continue;
1260 /* Ignore this element if there is a predicate
1261 and the predicate doesn't like it. */
1263 if (!NILP (predicate))
1265 if (EQ (predicate, Qcommandp))
1266 tem = Fcommandp (elt, Qnil);
1267 else
1269 GCPRO4 (tail, string, eltstring, bestmatch);
1270 tem = type == 3
1271 ? call2 (predicate, elt,
1272 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1273 : call1 (predicate, elt);
1274 UNGCPRO;
1276 if (NILP (tem)) continue;
1279 /* Update computation of how much all possible completions match */
1281 if (NILP (bestmatch))
1283 matchcount = 1;
1284 bestmatch = eltstring;
1285 bestmatchsize = SCHARS (eltstring);
1287 else
1289 compare = min (bestmatchsize, SCHARS (eltstring));
1290 tem = Fcompare_strings (bestmatch, make_number (0),
1291 make_number (compare),
1292 eltstring, make_number (0),
1293 make_number (compare),
1294 completion_ignore_case ? Qt : Qnil);
1295 if (EQ (tem, Qt))
1296 matchsize = compare;
1297 else if (XINT (tem) < 0)
1298 matchsize = - XINT (tem) - 1;
1299 else
1300 matchsize = XINT (tem) - 1;
1302 if (matchsize < 0)
1303 /* When can this happen ? -stef */
1304 matchsize = compare;
1305 if (completion_ignore_case)
1307 /* If this is an exact match except for case,
1308 use it as the best match rather than one that is not an
1309 exact match. This way, we get the case pattern
1310 of the actual match. */
1311 if ((matchsize == SCHARS (eltstring)
1312 && matchsize < SCHARS (bestmatch))
1314 /* If there is more than one exact match ignoring case,
1315 and one of them is exact including case,
1316 prefer that one. */
1317 /* If there is no exact match ignoring case,
1318 prefer a match that does not change the case
1319 of the input. */
1320 ((matchsize == SCHARS (eltstring))
1322 (matchsize == SCHARS (bestmatch))
1323 && (tem = Fcompare_strings (eltstring, make_number (0),
1324 make_number (SCHARS (string)),
1325 string, make_number (0),
1326 Qnil,
1327 Qnil),
1328 EQ (Qt, tem))
1329 && (tem = Fcompare_strings (bestmatch, make_number (0),
1330 make_number (SCHARS (string)),
1331 string, make_number (0),
1332 Qnil,
1333 Qnil),
1334 ! EQ (Qt, tem))))
1335 bestmatch = eltstring;
1337 if (bestmatchsize != SCHARS (eltstring)
1338 || bestmatchsize != matchsize)
1339 /* Don't count the same string multiple times. */
1340 matchcount++;
1341 bestmatchsize = matchsize;
1342 if (matchsize <= SCHARS (string)
1343 && matchcount > 1)
1344 /* No need to look any further. */
1345 break;
1350 if (NILP (bestmatch))
1351 return Qnil; /* No completions found */
1352 /* If we are ignoring case, and there is no exact match,
1353 and no additional text was supplied,
1354 don't change the case of what the user typed. */
1355 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1356 && SCHARS (bestmatch) > bestmatchsize)
1357 return minibuf_conform_representation (string, bestmatch);
1359 /* Return t if the supplied string is an exact match (counting case);
1360 it does not require any change to be made. */
1361 if (matchcount == 1 && bestmatchsize == SCHARS (string)
1362 && (tem = Fcompare_strings (bestmatch, make_number (0),
1363 make_number (bestmatchsize),
1364 string, make_number (0),
1365 make_number (bestmatchsize),
1366 Qnil),
1367 EQ (Qt, tem)))
1368 return Qt;
1370 XSETFASTINT (zero, 0); /* Else extract the part in which */
1371 XSETFASTINT (end, bestmatchsize); /* all completions agree */
1372 return Fsubstring (bestmatch, zero, end);
1375 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1376 doc: /* Search for partial matches to STRING in ALIST.
1377 Each car of each element of ALIST (or each element if it is not a cons cell)
1378 is tested to see if it begins with STRING.
1379 The value is a list of all the strings from ALIST that match.
1381 If ALIST is a hash-table, all the string keys are the possible matches.
1382 If ALIST is an obarray, the names of all symbols in the obarray
1383 are the possible matches.
1385 ALIST can also be a function to do the completion itself.
1386 It receives three arguments: the values STRING, PREDICATE and t.
1387 Whatever it returns becomes the value of `all-completions'.
1389 If optional third argument PREDICATE is non-nil,
1390 it is used to test each possible match.
1391 The match is a candidate only if PREDICATE returns non-nil.
1392 The argument given to PREDICATE is the alist element
1393 or the symbol from the obarray. If ALIST is a hash-table,
1394 predicate is called with two arguments: the key and the value.
1395 Additionally to this predicate, `completion-regexp-list'
1396 is used to further constrain the set of candidates.
1398 If the optional fourth argument HIDE-SPACES is non-nil,
1399 strings in ALIST that start with a space
1400 are ignored unless STRING itself starts with a space. */)
1401 (string, alist, predicate, hide_spaces)
1402 Lisp_Object string, alist, predicate, hide_spaces;
1404 Lisp_Object tail, elt, eltstring;
1405 Lisp_Object allmatches;
1406 int type = HASH_TABLE_P (alist) ? 3
1407 : VECTORP (alist) ? 2
1408 : NILP (alist) || (CONSP (alist)
1409 && (!SYMBOLP (XCAR (alist))
1410 || NILP (XCAR (alist))));
1411 int index = 0, obsize = 0;
1412 Lisp_Object bucket, tem;
1413 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1415 CHECK_STRING (string);
1416 if (type == 0)
1417 return call3 (alist, string, predicate, Qt);
1418 allmatches = bucket = Qnil;
1420 /* If ALIST is not a list, set TAIL just for gc pro. */
1421 tail = alist;
1422 if (type == 2)
1424 obsize = XVECTOR (alist)->size;
1425 bucket = XVECTOR (alist)->contents[index];
1428 while (1)
1430 /* Get the next element of the alist, obarray, or hash-table. */
1431 /* Exit the loop if the elements are all used up. */
1432 /* elt gets the alist element or symbol.
1433 eltstring gets the name to check as a completion. */
1435 if (type == 1)
1437 if (!CONSP (tail))
1438 break;
1439 elt = XCAR (tail);
1440 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1441 tail = XCDR (tail);
1443 else if (type == 2)
1445 if (XFASTINT (bucket) != 0)
1447 elt = bucket;
1448 eltstring = Fsymbol_name (elt);
1449 if (XSYMBOL (bucket)->next)
1450 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1451 else
1452 XSETFASTINT (bucket, 0);
1454 else if (++index >= obsize)
1455 break;
1456 else
1458 bucket = XVECTOR (alist)->contents[index];
1459 continue;
1462 else /* if (type == 3) */
1464 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1465 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1466 index++;
1467 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1468 break;
1469 else
1470 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1473 /* Is this element a possible completion? */
1475 if (STRINGP (eltstring)
1476 && SCHARS (string) <= SCHARS (eltstring)
1477 /* If HIDE_SPACES, reject alternatives that start with space
1478 unless the input starts with space. */
1479 && ((SBYTES (string) > 0
1480 && SREF (string, 0) == ' ')
1481 || SREF (eltstring, 0) != ' '
1482 || NILP (hide_spaces))
1483 && (tem = Fcompare_strings (eltstring, make_number (0),
1484 make_number (SCHARS (string)),
1485 string, make_number (0),
1486 make_number (SCHARS (string)),
1487 completion_ignore_case ? Qt : Qnil),
1488 EQ (Qt, tem)))
1490 /* Yes. */
1491 Lisp_Object regexps;
1492 Lisp_Object zero;
1493 XSETFASTINT (zero, 0);
1495 /* Ignore this element if it fails to match all the regexps. */
1496 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1497 regexps = XCDR (regexps))
1499 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1500 if (NILP (tem))
1501 break;
1503 if (CONSP (regexps))
1504 continue;
1506 /* Ignore this element if there is a predicate
1507 and the predicate doesn't like it. */
1509 if (!NILP (predicate))
1511 if (EQ (predicate, Qcommandp))
1512 tem = Fcommandp (elt, Qnil);
1513 else
1515 GCPRO4 (tail, eltstring, allmatches, string);
1516 tem = type == 3
1517 ? call2 (predicate, elt,
1518 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1519 : call1 (predicate, elt);
1520 UNGCPRO;
1522 if (NILP (tem)) continue;
1524 /* Ok => put it on the list. */
1525 allmatches = Fcons (eltstring, allmatches);
1529 return Fnreverse (allmatches);
1532 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1533 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1534 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
1535 Lisp_Object Vminibuffer_completing_file_name;
1537 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1538 doc: /* Read a string in the minibuffer, with completion.
1539 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1540 TABLE is an alist whose elements' cars are strings, or an obarray.
1541 TABLE can also be a function to do the completion itself.
1542 PREDICATE limits completion to a subset of TABLE.
1543 See `try-completion' and `all-completions' for more details
1544 on completion, TABLE, and PREDICATE.
1546 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
1547 the input is (or completes to) an element of TABLE or is null.
1548 If it is also not t, typing RET does not exit if it does non-null completion.
1549 If the input is null, `completing-read' returns an empty string,
1550 regardless of the value of REQUIRE-MATCH.
1552 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1553 If it is (STRING . POSITION), the initial input
1554 is STRING, but point is placed POSITION characters into the string.
1555 This feature is deprecated--it is best to pass nil for INITIAL-INPUT
1556 and supply the default value DEF instead. The user can yank the
1557 default value into the minibuffer easily using \\[next-history-element].
1559 HIST, if non-nil, specifies a history list
1560 and optionally the initial position in the list.
1561 It can be a symbol, which is the history list variable to use,
1562 or it can be a cons cell (HISTVAR . HISTPOS).
1563 In that case, HISTVAR is the history list variable to use,
1564 and HISTPOS is the initial position (the position in the list
1565 which INITIAL-INPUT corresponds to).
1566 Positions are counted starting from 1 at the beginning of the list.
1567 The variable `history-length' controls the maximum length of a
1568 history list.
1570 DEF, if non-nil, is the default value.
1572 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
1573 the current input method and the setting of `enable-multibyte-characters'.
1575 Completion ignores case if the ambient value of
1576 `completion-ignore-case' is non-nil. */)
1577 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method)
1578 Lisp_Object prompt, table, predicate, require_match, initial_input;
1579 Lisp_Object hist, def, inherit_input_method;
1581 Lisp_Object val, histvar, histpos;
1582 int count = SPECPDL_INDEX ();
1583 struct gcpro gcpro1;
1585 GCPRO1 (def);
1587 specbind (Qminibuffer_completion_table, table);
1588 specbind (Qminibuffer_completion_predicate, predicate);
1589 specbind (Qminibuffer_completion_confirm,
1590 EQ (require_match, Qt) ? Qnil : require_match);
1591 last_exact_completion = Qnil;
1593 if (SYMBOLP (hist))
1595 histvar = hist;
1596 histpos = Qnil;
1598 else
1600 histvar = Fcar_safe (hist);
1601 histpos = Fcdr_safe (hist);
1603 if (NILP (histvar))
1604 histvar = Qminibuffer_history;
1605 if (NILP (histpos))
1606 XSETFASTINT (histpos, 0);
1608 val = read_minibuf (NILP (require_match)
1609 ? Vminibuffer_local_completion_map
1610 : Vminibuffer_local_must_match_map,
1611 initial_input, prompt, Qnil, 0,
1612 histvar, histpos, def, 0,
1613 !NILP (inherit_input_method));
1615 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
1616 val = def;
1618 RETURN_UNGCPRO (unbind_to (count, val));
1621 Lisp_Object Fminibuffer_completion_help ();
1622 Lisp_Object Fassoc_string ();
1624 /* Test whether TXT is an exact completion. */
1625 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1626 doc: /* Return non-nil if STRING is a valid completion.
1627 Takes the same arguments as `all-completions' and `try-completion'.
1628 If ALIST is a function, it is called with three arguments:
1629 the values STRING, PREDICATE and `lambda'. */)
1630 (string, alist, predicate)
1631 Lisp_Object string, alist, predicate;
1633 Lisp_Object regexps, tail, tem = Qnil;
1634 int i = 0;
1636 CHECK_STRING (string);
1638 if ((CONSP (alist) && (!SYMBOLP (XCAR (alist)) || NILP (XCAR (alist))))
1639 || NILP (alist))
1641 tem = Fassoc_string (string, alist, completion_ignore_case ? Qt : Qnil);
1642 if NILP (tem)
1643 return Qnil;
1645 else if (VECTORP (alist))
1647 /* Bypass intern-soft as that loses for nil. */
1648 tem = oblookup (alist,
1649 SDATA (string),
1650 SCHARS (string),
1651 SBYTES (string));
1652 if (!SYMBOLP (tem))
1654 if (STRING_MULTIBYTE (string))
1655 string = Fstring_make_unibyte (string);
1656 else
1657 string = Fstring_make_multibyte (string);
1659 tem = oblookup (alist,
1660 SDATA (string),
1661 SCHARS (string),
1662 SBYTES (string));
1665 if (completion_ignore_case && !SYMBOLP (tem))
1667 for (i = XVECTOR (alist)->size - 1; i >= 0; i--)
1669 tail = XVECTOR (alist)->contents[i];
1670 if (SYMBOLP (tail))
1671 while (1)
1673 if (EQ((Fcompare_strings (string, make_number (0), Qnil,
1674 Fsymbol_name (tail),
1675 make_number (0) , Qnil, Qt)),
1676 Qt))
1678 tem = tail;
1679 break;
1681 if (XSYMBOL (tail)->next == 0)
1682 break;
1683 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1688 if (!SYMBOLP (tem))
1689 return Qnil;
1691 else if (HASH_TABLE_P (alist))
1693 struct Lisp_Hash_Table *h = XHASH_TABLE (alist);
1694 i = hash_lookup (h, string, NULL);
1695 if (i >= 0)
1696 tem = HASH_KEY (h, i);
1697 else
1698 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1699 if (!NILP (HASH_HASH (h, i)) &&
1700 EQ (Fcompare_strings (string, make_number (0), Qnil,
1701 HASH_KEY (h, i), make_number (0) , Qnil,
1702 completion_ignore_case ? Qt : Qnil),
1703 Qt))
1705 tem = HASH_KEY (h, i);
1706 break;
1708 if (!STRINGP (tem))
1709 return Qnil;
1711 else
1712 return call3 (alist, string, predicate, Qlambda);
1714 /* Reject this element if it fails to match all the regexps. */
1715 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1716 regexps = XCDR (regexps))
1718 if (NILP (Fstring_match (XCAR (regexps),
1719 SYMBOLP (tem) ? string : tem,
1720 Qnil)))
1721 return Qnil;
1724 /* Finally, check the predicate. */
1725 if (!NILP (predicate))
1726 return HASH_TABLE_P (alist)
1727 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (alist), i))
1728 : call1 (predicate, tem);
1729 else
1730 return Qt;
1733 /* returns:
1734 * 0 no possible completion
1735 * 1 was already an exact and unique completion
1736 * 3 was already an exact completion
1737 * 4 completed to an exact completion
1738 * 5 some completion happened
1739 * 6 no completion happened
1742 do_completion ()
1744 Lisp_Object completion, string, tem;
1745 int completedp;
1746 Lisp_Object last;
1747 struct gcpro gcpro1, gcpro2;
1749 completion = Ftry_completion (minibuffer_completion_contents (),
1750 Vminibuffer_completion_table,
1751 Vminibuffer_completion_predicate);
1752 last = last_exact_completion;
1753 last_exact_completion = Qnil;
1755 GCPRO2 (completion, last);
1757 if (NILP (completion))
1759 bitch_at_user ();
1760 temp_echo_area_glyphs (build_string (" [No match]"));
1761 UNGCPRO;
1762 return 0;
1765 if (EQ (completion, Qt)) /* exact and unique match */
1767 UNGCPRO;
1768 return 1;
1771 string = minibuffer_completion_contents ();
1773 /* COMPLETEDP should be true if some completion was done, which
1774 doesn't include simply changing the case of the entered string.
1775 However, for appearance, the string is rewritten if the case
1776 changes. */
1777 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qt);
1778 completedp = !EQ (tem, Qt);
1780 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qnil);
1781 if (!EQ (tem, Qt))
1782 /* Rewrite the user's input. */
1784 int prompt_end = XINT (Fminibuffer_prompt_end ());
1785 /* Some completion happened */
1787 if (! NILP (Vminibuffer_completing_file_name)
1788 && SREF (completion, SBYTES (completion) - 1) == '/'
1789 && PT < ZV
1790 && FETCH_CHAR (PT_BYTE) == '/')
1792 del_range (prompt_end, PT + 1);
1794 else
1795 del_range (prompt_end, PT);
1797 Finsert (1, &completion);
1799 if (! completedp)
1800 /* The case of the string changed, but that's all. We're not
1801 sure whether this is a unique completion or not, so try again
1802 using the real case (this shouldn't recurse again, because
1803 the next time try-completion will return either `t' or the
1804 exact string). */
1806 UNGCPRO;
1807 return do_completion ();
1811 /* It did find a match. Do we match some possibility exactly now? */
1812 tem = Ftest_completion (Fminibuffer_contents (),
1813 Vminibuffer_completion_table,
1814 Vminibuffer_completion_predicate);
1815 if (NILP (tem))
1817 /* not an exact match */
1818 UNGCPRO;
1819 if (completedp)
1820 return 5;
1821 else if (!NILP (Vcompletion_auto_help))
1822 Fminibuffer_completion_help ();
1823 else
1824 temp_echo_area_glyphs (build_string (" [Next char not unique]"));
1825 return 6;
1827 else if (completedp)
1829 UNGCPRO;
1830 return 4;
1832 /* If the last exact completion and this one were the same,
1833 it means we've already given a "Complete but not unique"
1834 message and the user's hit TAB again, so now we give him help. */
1835 last_exact_completion = completion;
1836 if (!NILP (last))
1838 tem = minibuffer_completion_contents ();
1839 if (!NILP (Fequal (tem, last)))
1840 Fminibuffer_completion_help ();
1842 UNGCPRO;
1843 return 3;
1846 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1848 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
1849 doc: /* Like `assoc' but specifically for strings.
1850 Unibyte strings are converted to multibyte for comparison.
1851 And case is ignored if CASE-FOLD is non-nil.
1852 As opposed to `assoc', it will also match an entry consisting of a single
1853 string rather than a cons cell whose car is a string. */)
1854 (key, list, case_fold)
1855 register Lisp_Object key;
1856 Lisp_Object list, case_fold;
1858 register Lisp_Object tail;
1860 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1862 register Lisp_Object elt, tem, thiscar;
1863 elt = Fcar (tail);
1864 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1865 if (!STRINGP (thiscar))
1866 continue;
1867 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1868 key, make_number (0), Qnil,
1869 case_fold);
1870 if (EQ (tem, Qt))
1871 return elt;
1872 QUIT;
1874 return Qnil;
1877 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1878 doc: /* Complete the minibuffer contents as far as possible.
1879 Return nil if there is no valid completion, else t.
1880 If no characters can be completed, display a list of possible completions.
1881 If you repeat this command after it displayed such a list,
1882 scroll the window of possible completions. */)
1885 register int i;
1886 Lisp_Object window, tem;
1888 /* If the previous command was not this,
1889 mark the completion buffer obsolete. */
1890 if (! EQ (current_kboard->Vlast_command, Vthis_command))
1891 Vminibuf_scroll_window = Qnil;
1893 window = Vminibuf_scroll_window;
1894 /* If there's a fresh completion window with a live buffer,
1895 and this command is repeated, scroll that window. */
1896 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1897 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1899 struct buffer *obuf = current_buffer;
1901 Fset_buffer (XWINDOW (window)->buffer);
1902 tem = Fpos_visible_in_window_p (make_number (ZV), window, Qnil);
1903 if (! NILP (tem))
1904 /* If end is in view, scroll up to the beginning. */
1905 Fset_window_start (window, make_number (BEGV), Qnil);
1906 else
1907 /* Else scroll down one screen. */
1908 Fscroll_other_window (Qnil);
1910 set_buffer_internal (obuf);
1911 return Qnil;
1914 i = do_completion ();
1915 switch (i)
1917 case 0:
1918 return Qnil;
1920 case 1:
1921 if (PT != ZV)
1922 Fgoto_char (make_number (ZV));
1923 temp_echo_area_glyphs (build_string (" [Sole completion]"));
1924 break;
1926 case 3:
1927 if (PT != ZV)
1928 Fgoto_char (make_number (ZV));
1929 temp_echo_area_glyphs (build_string (" [Complete, but not unique]"));
1930 break;
1933 return Qt;
1936 /* Subroutines of Fminibuffer_complete_and_exit. */
1938 /* This one is called by internal_condition_case to do the real work. */
1940 Lisp_Object
1941 complete_and_exit_1 ()
1943 return make_number (do_completion ());
1946 /* This one is called by internal_condition_case if an error happens.
1947 Pretend the current value is an exact match. */
1949 Lisp_Object
1950 complete_and_exit_2 (ignore)
1951 Lisp_Object ignore;
1953 return make_number (1);
1956 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1957 Sminibuffer_complete_and_exit, 0, 0, "",
1958 doc: /* If the minibuffer contents is a valid completion then exit.
1959 Otherwise try to complete it. If completion leads to a valid completion,
1960 a repetition of this command will exit. */)
1963 register int i;
1964 Lisp_Object val;
1966 /* Allow user to specify null string */
1967 if (XINT (Fminibuffer_prompt_end ()) == ZV)
1968 goto exit;
1970 if (!NILP (Ftest_completion (Fminibuffer_contents (),
1971 Vminibuffer_completion_table,
1972 Vminibuffer_completion_predicate)))
1973 goto exit;
1975 /* Call do_completion, but ignore errors. */
1976 SET_PT (ZV);
1977 val = internal_condition_case (complete_and_exit_1, Qerror,
1978 complete_and_exit_2);
1980 i = XFASTINT (val);
1981 switch (i)
1983 case 1:
1984 case 3:
1985 goto exit;
1987 case 4:
1988 if (!NILP (Vminibuffer_completion_confirm))
1990 temp_echo_area_glyphs (build_string (" [Confirm]"));
1991 return Qnil;
1993 else
1994 goto exit;
1996 default:
1997 return Qnil;
1999 exit:
2000 return Fthrow (Qexit, Qnil);
2001 /* NOTREACHED */
2004 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
2005 0, 0, "",
2006 doc: /* Complete the minibuffer contents at most a single word.
2007 After one word is completed as much as possible, a space or hyphen
2008 is added, provided that matches some possible completion.
2009 Return nil if there is no valid completion, else t. */)
2012 Lisp_Object completion, tem, tem1;
2013 register int i, i_byte;
2014 register const unsigned char *completion_string;
2015 struct gcpro gcpro1, gcpro2;
2016 int prompt_end_charpos = XINT (Fminibuffer_prompt_end ());
2018 /* We keep calling Fbuffer_string rather than arrange for GC to
2019 hold onto a pointer to one of the strings thus made. */
2021 completion = Ftry_completion (minibuffer_completion_contents (),
2022 Vminibuffer_completion_table,
2023 Vminibuffer_completion_predicate);
2024 if (NILP (completion))
2026 bitch_at_user ();
2027 temp_echo_area_glyphs (build_string (" [No match]"));
2028 return Qnil;
2030 if (EQ (completion, Qt))
2031 return Qnil;
2033 #if 0 /* How the below code used to look, for reference. */
2034 tem = Fminibuffer_contents ();
2035 b = SDATA (tem);
2036 i = ZV - 1 - SCHARS (completion);
2037 p = SDATA (completion);
2038 if (i > 0 ||
2039 0 <= scmp (b, p, ZV - 1))
2041 i = 1;
2042 /* Set buffer to longest match of buffer tail and completion head. */
2043 while (0 <= scmp (b + i, p, ZV - 1 - i))
2044 i++;
2045 del_range (1, i + 1);
2046 SET_PT (ZV);
2048 #else /* Rewritten code */
2050 int buffer_nchars, completion_nchars;
2052 CHECK_STRING (completion);
2053 tem = minibuffer_completion_contents ();
2054 GCPRO2 (completion, tem);
2055 /* If reading a file name,
2056 expand any $ENVVAR refs in the buffer and in TEM. */
2057 if (! NILP (Vminibuffer_completing_file_name))
2059 Lisp_Object substituted;
2060 substituted = Fsubstitute_in_file_name (tem);
2061 if (! EQ (substituted, tem))
2063 tem = substituted;
2064 del_range (prompt_end_charpos, PT);
2065 Finsert (1, &tem);
2068 buffer_nchars = SCHARS (tem); /* # chars in what we completed. */
2069 completion_nchars = SCHARS (completion);
2070 i = buffer_nchars - completion_nchars;
2071 if (i > 0
2073 (tem1 = Fcompare_strings (tem, make_number (0),
2074 make_number (buffer_nchars),
2075 completion, make_number (0),
2076 make_number (buffer_nchars),
2077 completion_ignore_case ? Qt : Qnil),
2078 ! EQ (tem1, Qt)))
2080 int start_pos;
2082 /* Make buffer (before point) contain the longest match
2083 of TEM's tail and COMPLETION's head. */
2084 if (i <= 0) i = 1;
2085 start_pos= i;
2086 buffer_nchars -= i;
2087 while (i > 0)
2089 tem1 = Fcompare_strings (tem, make_number (start_pos), Qnil,
2090 completion, make_number (0),
2091 make_number (buffer_nchars),
2092 completion_ignore_case ? Qt : Qnil);
2093 start_pos++;
2094 if (EQ (tem1, Qt))
2095 break;
2096 i++;
2097 buffer_nchars--;
2099 del_range (start_pos, start_pos + buffer_nchars);
2101 UNGCPRO;
2103 #endif /* Rewritten code */
2106 int prompt_end_bytepos;
2107 prompt_end_bytepos = CHAR_TO_BYTE (prompt_end_charpos);
2108 i = PT - prompt_end_charpos;
2109 i_byte = PT_BYTE - prompt_end_bytepos;
2112 /* If completion finds next char not unique,
2113 consider adding a space or a hyphen. */
2114 if (i == SCHARS (completion))
2116 GCPRO1 (completion);
2117 tem = Ftry_completion (concat2 (minibuffer_completion_contents (),
2118 build_string (" ")),
2119 Vminibuffer_completion_table,
2120 Vminibuffer_completion_predicate);
2121 UNGCPRO;
2123 if (STRINGP (tem))
2124 completion = tem;
2125 else
2127 GCPRO1 (completion);
2128 tem =
2129 Ftry_completion (concat2 (minibuffer_completion_contents (),
2130 build_string ("-")),
2131 Vminibuffer_completion_table,
2132 Vminibuffer_completion_predicate);
2133 UNGCPRO;
2135 if (STRINGP (tem))
2136 completion = tem;
2140 /* Now find first word-break in the stuff found by completion.
2141 i gets index in string of where to stop completing. */
2143 int len, c;
2144 int bytes = SBYTES (completion);
2145 completion_string = SDATA (completion);
2146 for (; i_byte < SBYTES (completion); i_byte += len, i++)
2148 c = STRING_CHAR_AND_LENGTH (completion_string + i_byte,
2149 bytes - i_byte,
2150 len);
2151 if (SYNTAX (c) != Sword)
2153 i_byte += len;
2154 i++;
2155 break;
2160 /* If got no characters, print help for user. */
2162 if (i == PT - prompt_end_charpos)
2164 if (!NILP (Vcompletion_auto_help))
2165 Fminibuffer_completion_help ();
2166 return Qnil;
2169 /* Otherwise insert in minibuffer the chars we got */
2171 if (! NILP (Vminibuffer_completing_file_name)
2172 && SREF (completion, SBYTES (completion) - 1) == '/'
2173 && PT < ZV
2174 && FETCH_CHAR (PT_BYTE) == '/')
2176 del_range (prompt_end_charpos, PT + 1);
2178 else
2179 del_range (prompt_end_charpos, PT);
2181 insert_from_string (completion, 0, 0, i, i_byte, 1);
2182 return Qt;
2185 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
2186 1, 1, 0,
2187 doc: /* Display the list of completions, COMPLETIONS, using `standard-output'.
2188 Each element may be just a symbol or string
2189 or may be a list of two strings to be printed as if concatenated.
2190 `standard-output' must be a buffer.
2191 The actual completion alternatives, as inserted, are given `mouse-face'
2192 properties of `highlight'.
2193 At the end, this runs the normal hook `completion-setup-hook'.
2194 It can find the completion buffer in `standard-output'. */)
2195 (completions)
2196 Lisp_Object completions;
2198 Lisp_Object tail, elt;
2199 register int i;
2200 int column = 0;
2201 struct gcpro gcpro1, gcpro2;
2202 struct buffer *old = current_buffer;
2203 int first = 1;
2205 /* Note that (when it matters) every variable
2206 points to a non-string that is pointed to by COMPLETIONS,
2207 except for ELT. ELT can be pointing to a string
2208 when terpri or Findent_to calls a change hook. */
2209 elt = Qnil;
2210 GCPRO2 (completions, elt);
2212 if (BUFFERP (Vstandard_output))
2213 set_buffer_internal (XBUFFER (Vstandard_output));
2215 if (NILP (completions))
2216 write_string ("There are no possible completions of what you have typed.",
2217 -1);
2218 else
2220 write_string ("Possible completions are:", -1);
2221 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
2223 Lisp_Object tem, string;
2224 int length;
2225 Lisp_Object startpos, endpos;
2227 startpos = Qnil;
2229 elt = Fcar (tail);
2230 /* Compute the length of this element. */
2231 if (CONSP (elt))
2233 tem = XCAR (elt);
2234 CHECK_STRING (tem);
2235 length = SCHARS (tem);
2237 tem = Fcar (XCDR (elt));
2238 CHECK_STRING (tem);
2239 length += SCHARS (tem);
2241 else
2243 CHECK_STRING (elt);
2244 length = SCHARS (elt);
2247 /* This does a bad job for narrower than usual windows.
2248 Sadly, the window it will appear in is not known
2249 until after the text has been made. */
2251 if (BUFFERP (Vstandard_output))
2252 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2254 /* If the previous completion was very wide,
2255 or we have two on this line already,
2256 don't put another on the same line. */
2257 if (column > 33 || first
2258 /* If this is really wide, don't put it second on a line. */
2259 || (column > 0 && length > 45))
2261 Fterpri (Qnil);
2262 column = 0;
2264 /* Otherwise advance to column 35. */
2265 else
2267 if (BUFFERP (Vstandard_output))
2269 tem = Findent_to (make_number (35), make_number (2));
2271 column = XINT (tem);
2273 else
2277 write_string (" ", -1);
2278 column++;
2280 while (column < 35);
2284 if (BUFFERP (Vstandard_output))
2286 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2287 Fset_text_properties (startpos, endpos,
2288 Qnil, Vstandard_output);
2291 /* Output this element.
2292 If necessary, convert it to unibyte or to multibyte first. */
2293 if (CONSP (elt))
2294 string = Fcar (elt);
2295 else
2296 string = elt;
2297 if (NILP (current_buffer->enable_multibyte_characters)
2298 && STRING_MULTIBYTE (string))
2299 string = Fstring_make_unibyte (string);
2300 else if (!NILP (current_buffer->enable_multibyte_characters)
2301 && !STRING_MULTIBYTE (string))
2302 string = Fstring_make_multibyte (string);
2304 if (BUFFERP (Vstandard_output))
2306 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2308 Fprinc (string, Qnil);
2310 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2312 Fput_text_property (startpos, endpos,
2313 Qmouse_face, intern ("highlight"),
2314 Vstandard_output);
2316 else
2318 Fprinc (string, Qnil);
2321 /* Output the annotation for this element. */
2322 if (CONSP (elt))
2324 if (BUFFERP (Vstandard_output))
2326 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2328 Fprinc (Fcar (Fcdr (elt)), Qnil);
2330 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2332 Fset_text_properties (startpos, endpos, Qnil,
2333 Vstandard_output);
2335 else
2337 Fprinc (Fcar (Fcdr (elt)), Qnil);
2342 /* Update COLUMN for what we have output. */
2343 column += length;
2345 /* If output is to a buffer, recompute COLUMN in a way
2346 that takes account of character widths. */
2347 if (BUFFERP (Vstandard_output))
2349 tem = Fcurrent_column ();
2350 column = XINT (tem);
2353 first = 0;
2357 UNGCPRO;
2359 if (BUFFERP (Vstandard_output))
2360 set_buffer_internal (old);
2362 if (!NILP (Vrun_hooks))
2363 call1 (Vrun_hooks, intern ("completion-setup-hook"));
2365 return Qnil;
2368 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
2369 0, 0, "",
2370 doc: /* Display a list of possible completions of the current minibuffer contents. */)
2373 Lisp_Object completions;
2375 message ("Making completion list...");
2376 completions = Fall_completions (minibuffer_completion_contents (),
2377 Vminibuffer_completion_table,
2378 Vminibuffer_completion_predicate,
2379 Qt);
2380 clear_message (1, 0);
2382 if (NILP (completions))
2384 bitch_at_user ();
2385 temp_echo_area_glyphs (build_string (" [No completions]"));
2387 else
2388 internal_with_output_to_temp_buffer ("*Completions*",
2389 Fdisplay_completion_list,
2390 Fsort (completions, Qstring_lessp));
2391 return Qnil;
2394 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
2395 doc: /* Terminate minibuffer input. */)
2398 if (INTEGERP (last_command_char))
2399 internal_self_insert (XINT (last_command_char), 0);
2400 else
2401 bitch_at_user ();
2403 return Fthrow (Qexit, Qnil);
2406 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
2407 doc: /* Terminate this minibuffer argument. */)
2410 return Fthrow (Qexit, Qnil);
2413 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
2414 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
2417 return make_number (minibuf_level);
2420 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
2421 doc: /* Return the prompt string of the currently-active minibuffer.
2422 If no minibuffer is active, return nil. */)
2425 return Fcopy_sequence (minibuf_prompt);
2429 /* Temporarily display STRING at the end of the current
2430 minibuffer contents. This is used to display things like
2431 "[No Match]" when the user requests a completion for a prefix
2432 that has no possible completions, and other quick, unobtrusive
2433 messages. */
2435 void
2436 temp_echo_area_glyphs (string)
2437 Lisp_Object string;
2439 int osize = ZV;
2440 int osize_byte = ZV_BYTE;
2441 int opoint = PT;
2442 int opoint_byte = PT_BYTE;
2443 Lisp_Object oinhibit;
2444 oinhibit = Vinhibit_quit;
2446 /* Clear out any old echo-area message to make way for our new thing. */
2447 message (0);
2449 SET_PT_BOTH (osize, osize_byte);
2450 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 0);
2451 SET_PT_BOTH (opoint, opoint_byte);
2452 Vinhibit_quit = Qt;
2453 Fsit_for (make_number (2), Qnil, Qnil);
2454 del_range_both (osize, osize_byte, ZV, ZV_BYTE, 1);
2455 SET_PT_BOTH (opoint, opoint_byte);
2456 if (!NILP (Vquit_flag))
2458 Vquit_flag = Qnil;
2459 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
2461 Vinhibit_quit = oinhibit;
2464 DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
2465 1, 1, 0,
2466 doc: /* Temporarily display STRING at the end of the minibuffer.
2467 The text is displayed for two seconds,
2468 or until the next input event arrives, whichever comes first. */)
2469 (string)
2470 Lisp_Object string;
2472 CHECK_STRING (string);
2473 temp_echo_area_glyphs (string);
2474 return Qnil;
2477 void
2478 init_minibuf_once ()
2480 Vminibuffer_list = Qnil;
2481 staticpro (&Vminibuffer_list);
2484 void
2485 syms_of_minibuf ()
2487 minibuf_level = 0;
2488 minibuf_prompt = Qnil;
2489 staticpro (&minibuf_prompt);
2491 minibuf_save_list = Qnil;
2492 staticpro (&minibuf_save_list);
2494 Qread_file_name_internal = intern ("read-file-name-internal");
2495 staticpro (&Qread_file_name_internal);
2497 Qminibuffer_default = intern ("minibuffer-default");
2498 staticpro (&Qminibuffer_default);
2499 Fset (Qminibuffer_default, Qnil);
2501 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
2502 staticpro (&Qminibuffer_completion_table);
2504 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
2505 staticpro (&Qminibuffer_completion_confirm);
2507 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
2508 staticpro (&Qminibuffer_completion_predicate);
2510 staticpro (&last_exact_completion);
2511 last_exact_completion = Qnil;
2513 staticpro (&last_minibuf_string);
2514 last_minibuf_string = Qnil;
2516 Quser_variable_p = intern ("user-variable-p");
2517 staticpro (&Quser_variable_p);
2519 Qminibuffer_history = intern ("minibuffer-history");
2520 staticpro (&Qminibuffer_history);
2522 Qbuffer_name_history = intern ("buffer-name-history");
2523 staticpro (&Qbuffer_name_history);
2524 Fset (Qbuffer_name_history, Qnil);
2526 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
2527 staticpro (&Qminibuffer_setup_hook);
2529 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
2530 staticpro (&Qminibuffer_exit_hook);
2532 Qhistory_length = intern ("history-length");
2533 staticpro (&Qhistory_length);
2535 Qcurrent_input_method = intern ("current-input-method");
2536 staticpro (&Qcurrent_input_method);
2538 Qactivate_input_method = intern ("activate-input-method");
2539 staticpro (&Qactivate_input_method);
2541 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2542 doc: /* If this is non-nil, `read-buffer' does its work by calling this function. */);
2543 Vread_buffer_function = Qnil;
2545 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2546 doc: /* Normal hook run just after entry to minibuffer. */);
2547 Vminibuffer_setup_hook = Qnil;
2549 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2550 doc: /* Normal hook run just after exit from minibuffer. */);
2551 Vminibuffer_exit_hook = Qnil;
2553 DEFVAR_LISP ("history-length", &Vhistory_length,
2554 doc: /* *Maximum length for history lists before truncation takes place.
2555 A number means that length; t means infinite. Truncation takes place
2556 just after a new element is inserted. Setting the history-length
2557 property of a history variable overrides this default. */);
2558 XSETFASTINT (Vhistory_length, 30);
2560 DEFVAR_LISP ("completion-auto-help", &Vcompletion_auto_help,
2561 doc: /* *Non-nil means automatically provide help for invalid completion input. */);
2562 Vcompletion_auto_help = Qt;
2564 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
2565 doc: /* Non-nil means don't consider case significant in completion. */);
2566 completion_ignore_case = 0;
2568 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2569 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
2570 This variable makes a difference whenever the minibuffer window is active. */);
2571 enable_recursive_minibuffers = 0;
2573 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2574 doc: /* Alist or obarray used for completion in the minibuffer.
2575 This becomes the ALIST argument to `try-completion' and `all-completion'.
2577 The value may alternatively be a function, which is given three arguments:
2578 STRING, the current buffer contents;
2579 PREDICATE, the predicate for filtering possible matches;
2580 CODE, which says what kind of things to do.
2581 CODE can be nil, t or `lambda'.
2582 nil means to return the best completion of STRING, or nil if there is none.
2583 t means to return a list of all possible completions of STRING.
2584 `lambda' means to return t if STRING is a valid completion as it stands. */);
2585 Vminibuffer_completion_table = Qnil;
2587 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2588 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
2589 Vminibuffer_completion_predicate = Qnil;
2591 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
2592 doc: /* Non-nil means to demand confirmation of completion before exiting minibuffer. */);
2593 Vminibuffer_completion_confirm = Qnil;
2595 DEFVAR_LISP ("minibuffer-completing-file-name",
2596 &Vminibuffer_completing_file_name,
2597 doc: /* Non-nil means completing file names. */);
2598 Vminibuffer_completing_file_name = Qnil;
2600 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2601 doc: /* Value that `help-form' takes on inside the minibuffer. */);
2602 Vminibuffer_help_form = Qnil;
2604 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2605 doc: /* History list symbol to add minibuffer values to.
2606 Each string of minibuffer input, as it appears on exit from the minibuffer,
2607 is added with
2608 (set minibuffer-history-variable
2609 (cons STRING (symbol-value minibuffer-history-variable))) */);
2610 XSETFASTINT (Vminibuffer_history_variable, 0);
2612 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2613 doc: /* Current position of redoing in the history list. */);
2614 Vminibuffer_history_position = Qnil;
2616 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2617 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
2618 Some uses of the echo area also raise that frame (since they use it too). */);
2619 minibuffer_auto_raise = 0;
2621 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
2622 doc: /* List of regexps that should restrict possible completions. */);
2623 Vcompletion_regexp_list = Qnil;
2625 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2626 &minibuffer_allow_text_properties,
2627 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
2628 This also affects `read-string', but it does not affect `read-minibuffer',
2629 `read-no-blanks-input', or any of the functions that do minibuffer input
2630 with completion; they always discard text properties. */);
2631 minibuffer_allow_text_properties = 0;
2633 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2634 doc: /* Text properties that are added to minibuffer prompts.
2635 These are in addition to the basic `field' property, and stickiness
2636 properties. */);
2637 /* We use `intern' here instead of Qread_only to avoid
2638 initialization-order problems. */
2639 Vminibuffer_prompt_properties
2640 = Fcons (intern ("read-only"), Fcons (Qt, Qnil));
2642 defsubr (&Sset_minibuffer_window);
2643 defsubr (&Sread_from_minibuffer);
2644 defsubr (&Seval_minibuffer);
2645 defsubr (&Sread_minibuffer);
2646 defsubr (&Sread_string);
2647 defsubr (&Sread_command);
2648 defsubr (&Sread_variable);
2649 defsubr (&Sread_buffer);
2650 defsubr (&Sread_no_blanks_input);
2651 defsubr (&Sminibuffer_depth);
2652 defsubr (&Sminibuffer_prompt);
2654 defsubr (&Sminibufferp);
2655 defsubr (&Sminibuffer_prompt_end);
2656 defsubr (&Sminibuffer_contents);
2657 defsubr (&Sminibuffer_contents_no_properties);
2658 defsubr (&Sdelete_minibuffer_contents);
2660 defsubr (&Stry_completion);
2661 defsubr (&Sall_completions);
2662 defsubr (&Stest_completion);
2663 defsubr (&Sassoc_string);
2664 defsubr (&Scompleting_read);
2665 defsubr (&Sminibuffer_complete);
2666 defsubr (&Sminibuffer_complete_word);
2667 defsubr (&Sminibuffer_complete_and_exit);
2668 defsubr (&Sdisplay_completion_list);
2669 defsubr (&Sminibuffer_completion_help);
2671 defsubr (&Sself_insert_and_exit);
2672 defsubr (&Sexit_minibuffer);
2674 defsubr (&Sminibuffer_message);
2677 void
2678 keys_of_minibuf ()
2680 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
2681 "abort-recursive-edit");
2682 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
2683 "exit-minibuffer");
2684 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
2685 "exit-minibuffer");
2687 initial_define_key (Vminibuffer_local_ns_map, ' ',
2688 "exit-minibuffer");
2689 initial_define_key (Vminibuffer_local_ns_map, '\t',
2690 "exit-minibuffer");
2691 initial_define_key (Vminibuffer_local_ns_map, '?',
2692 "self-insert-and-exit");
2694 initial_define_key (Vminibuffer_local_completion_map, '\t',
2695 "minibuffer-complete");
2696 initial_define_key (Vminibuffer_local_completion_map, ' ',
2697 "minibuffer-complete-word");
2698 initial_define_key (Vminibuffer_local_completion_map, '?',
2699 "minibuffer-completion-help");
2701 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
2702 "minibuffer-complete-and-exit");
2703 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
2704 "minibuffer-complete-and-exit");
2707 /* arch-tag: 8f69b601-fba3-484c-a6dd-ceaee54a7a73
2708 (do not change this comment) */