Comments.
[emacs.git] / src / minibuf.c
blob6581159e841e5b082bc5b5cf60e0d3ae3ca56501
1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005,
4 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include <config.h>
25 #include <stdio.h>
27 #include "lisp.h"
28 #include "commands.h"
29 #include "buffer.h"
30 #include "charset.h"
31 #include "dispextern.h"
32 #include "keyboard.h"
33 #include "frame.h"
34 #include "window.h"
35 #include "syntax.h"
36 #include "intervals.h"
37 #include "keymap.h"
39 extern int quit_char;
41 /* List of buffers for use as minibuffers.
42 The first element of the list is used for the outermost minibuffer
43 invocation, the next element is used for a recursive minibuffer
44 invocation, etc. The list is extended at the end as deeper
45 minibuffer recursions are encountered. */
47 Lisp_Object Vminibuffer_list;
49 /* Data to remember during recursive minibuffer invocations */
51 Lisp_Object minibuf_save_list;
53 /* Depth in minibuffer invocations. */
55 int minibuf_level;
57 /* Nonzero means display completion help for invalid input. */
59 Lisp_Object Vcompletion_auto_help;
61 /* The maximum length of a minibuffer history. */
63 Lisp_Object Qhistory_length, Vhistory_length;
65 /* No duplicates in history. */
67 int history_delete_duplicates;
69 /* Non-nil means add new input to history. */
71 Lisp_Object Vhistory_add_new_input;
73 /* Fread_minibuffer leaves the input here as a string. */
75 Lisp_Object last_minibuf_string;
77 /* Nonzero means let functions called when within a minibuffer
78 invoke recursive minibuffers (to read arguments, or whatever) */
80 int enable_recursive_minibuffers;
82 /* Nonzero means don't ignore text properties
83 in Fread_from_minibuffer. */
85 int minibuffer_allow_text_properties;
87 /* help-form is bound to this while in the minibuffer. */
89 Lisp_Object Vminibuffer_help_form;
91 /* Variable which is the history list to add minibuffer values to. */
93 Lisp_Object Vminibuffer_history_variable;
95 /* Current position in the history list (adjusted by M-n and M-p). */
97 Lisp_Object Vminibuffer_history_position;
99 /* Text properties that are added to minibuffer prompts.
100 These are in addition to the basic `field' property, and stickiness
101 properties. */
103 Lisp_Object Vminibuffer_prompt_properties;
105 Lisp_Object Qminibuffer_history, Qbuffer_name_history;
107 Lisp_Object Qread_file_name_internal;
109 /* Normal hooks for entry to and exit from minibuffer. */
111 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
112 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
114 /* Function to call to read a buffer name. */
115 Lisp_Object Vread_buffer_function;
117 /* Nonzero means completion ignores case. */
119 int completion_ignore_case;
121 /* List of regexps that should restrict possible completions. */
123 Lisp_Object Vcompletion_regexp_list;
125 /* Nonzero means raise the minibuffer frame when the minibuffer
126 is entered. */
128 int minibuffer_auto_raise;
130 /* If last completion attempt reported "Complete but not unique"
131 then this is the string completed then; otherwise this is nil. */
133 static Lisp_Object last_exact_completion;
135 /* Keymap for reading expressions. */
136 Lisp_Object Vread_expression_map;
138 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
139 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
140 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
141 Lisp_Object Vminibuffer_completing_file_name;
143 Lisp_Object Quser_variable_p;
145 Lisp_Object Qminibuffer_default;
147 Lisp_Object Qcurrent_input_method, Qactivate_input_method;
149 Lisp_Object Qcase_fold_search;
151 Lisp_Object Qread_expression_history;
153 extern Lisp_Object Voverriding_local_map;
155 extern Lisp_Object Qmouse_face;
157 extern Lisp_Object Qfield;
159 /* Put minibuf on currently selected frame's minibuffer.
160 We do this whenever the user starts a new minibuffer
161 or when a minibuffer exits. */
163 void
164 choose_minibuf_frame ()
166 if (FRAMEP (selected_frame)
167 && FRAME_LIVE_P (XFRAME (selected_frame))
168 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
170 struct frame *sf = XFRAME (selected_frame);
171 Lisp_Object buffer;
173 /* I don't think that any frames may validly have a null minibuffer
174 window anymore. */
175 if (NILP (sf->minibuffer_window))
176 abort ();
178 /* Under X, we come here with minibuf_window being the
179 minibuffer window of the unused termcap window created in
180 init_window_once. That window doesn't have a buffer. */
181 buffer = XWINDOW (minibuf_window)->buffer;
182 if (BUFFERP (buffer))
183 Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
184 minibuf_window = sf->minibuffer_window;
187 /* Make sure no other frame has a minibuffer as its selected window,
188 because the text would not be displayed in it, and that would be
189 confusing. Only allow the selected frame to do this,
190 and that only if the minibuffer is active. */
192 Lisp_Object tail, frame;
194 FOR_EACH_FRAME (tail, frame)
195 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
196 && !(EQ (frame, selected_frame)
197 && minibuf_level > 0))
198 Fset_frame_selected_window (frame, Fframe_first_window (frame));
202 Lisp_Object
203 choose_minibuf_frame_1 (ignore)
204 Lisp_Object ignore;
206 choose_minibuf_frame ();
207 return Qnil;
210 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
211 Sset_minibuffer_window, 1, 1, 0,
212 doc: /* Specify which minibuffer window to use for the minibuffer.
213 This affects where the minibuffer is displayed if you put text in it
214 without invoking the usual minibuffer commands. */)
215 (window)
216 Lisp_Object window;
218 CHECK_WINDOW (window);
219 if (! MINI_WINDOW_P (XWINDOW (window)))
220 error ("Window is not a minibuffer window");
222 minibuf_window = window;
224 return window;
228 /* Actual minibuffer invocation. */
230 static Lisp_Object read_minibuf_unwind P_ ((Lisp_Object));
231 static Lisp_Object run_exit_minibuf_hook P_ ((Lisp_Object));
232 static Lisp_Object read_minibuf P_ ((Lisp_Object, Lisp_Object,
233 Lisp_Object, Lisp_Object,
234 int, Lisp_Object,
235 Lisp_Object, Lisp_Object,
236 int, int));
237 static Lisp_Object read_minibuf_noninteractive P_ ((Lisp_Object, Lisp_Object,
238 Lisp_Object, Lisp_Object,
239 int, Lisp_Object,
240 Lisp_Object, Lisp_Object,
241 int, int));
242 static Lisp_Object string_to_object P_ ((Lisp_Object, Lisp_Object));
245 /* Read a Lisp object from VAL and return it. If VAL is an empty
246 string, and DEFALT is a string, read from DEFALT instead of VAL. */
248 static Lisp_Object
249 string_to_object (val, defalt)
250 Lisp_Object val, defalt;
252 struct gcpro gcpro1, gcpro2;
253 Lisp_Object expr_and_pos;
254 int pos;
256 GCPRO2 (val, defalt);
258 if (STRINGP (val) && SCHARS (val) == 0
259 && STRINGP (defalt))
260 val = defalt;
262 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
263 pos = XINT (Fcdr (expr_and_pos));
264 if (pos != SCHARS (val))
266 /* Ignore trailing whitespace; any other trailing junk
267 is an error. */
268 int i;
269 pos = string_char_to_byte (val, pos);
270 for (i = pos; i < SBYTES (val); i++)
272 int c = SREF (val, i);
273 if (c != ' ' && c != '\t' && c != '\n')
274 error ("Trailing garbage following expression");
278 val = Fcar (expr_and_pos);
279 RETURN_UNGCPRO (val);
283 /* Like read_minibuf but reading from stdin. This function is called
284 from read_minibuf to do the job if noninteractive. */
286 static Lisp_Object
287 read_minibuf_noninteractive (map, initial, prompt, backup_n, expflag,
288 histvar, histpos, defalt, allow_props,
289 inherit_input_method)
290 Lisp_Object map;
291 Lisp_Object initial;
292 Lisp_Object prompt;
293 Lisp_Object backup_n;
294 int expflag;
295 Lisp_Object histvar;
296 Lisp_Object histpos;
297 Lisp_Object defalt;
298 int allow_props;
299 int inherit_input_method;
301 int size, len;
302 char *line, *s;
303 Lisp_Object val;
305 fprintf (stdout, "%s", SDATA (prompt));
306 fflush (stdout);
308 val = Qnil;
309 size = 100;
310 len = 0;
311 line = (char *) xmalloc (size * sizeof *line);
312 while ((s = fgets (line + len, size - len, stdin)) != NULL
313 && (len = strlen (line),
314 len == size - 1 && line[len - 1] != '\n'))
316 size *= 2;
317 line = (char *) xrealloc (line, size);
320 if (s)
322 len = strlen (line);
324 if (len > 0 && line[len - 1] == '\n')
325 line[--len] = '\0';
327 val = build_string (line);
328 xfree (line);
330 else
332 xfree (line);
333 error ("Error reading from stdin");
336 /* If Lisp form desired instead of string, parse it. */
337 if (expflag)
338 val = string_to_object (val, defalt);
340 return val;
343 DEFUN ("minibufferp", Fminibufferp,
344 Sminibufferp, 0, 1, 0,
345 doc: /* Return t if BUFFER is a minibuffer.
346 No argument or nil as argument means use current buffer as BUFFER.
347 BUFFER can be a buffer or a buffer name. */)
348 (buffer)
349 Lisp_Object buffer;
351 Lisp_Object tem;
353 if (NILP (buffer))
354 buffer = Fcurrent_buffer ();
355 else if (STRINGP (buffer))
356 buffer = Fget_buffer (buffer);
357 else
358 CHECK_BUFFER (buffer);
360 tem = Fmemq (buffer, Vminibuffer_list);
361 return ! NILP (tem) ? Qt : Qnil;
364 DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
365 Sminibuffer_prompt_end, 0, 0, 0,
366 doc: /* Return the buffer position of the end of the minibuffer prompt.
367 Return (point-min) if current buffer is not a minibuffer. */)
370 /* This function is written to be most efficient when there's a prompt. */
371 Lisp_Object beg, end, tem;
372 beg = make_number (BEGV);
374 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
375 if (NILP (tem))
376 return beg;
378 end = Ffield_end (beg, Qnil, Qnil);
380 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
381 return beg;
382 else
383 return end;
386 DEFUN ("minibuffer-contents", Fminibuffer_contents,
387 Sminibuffer_contents, 0, 0, 0,
388 doc: /* Return the user input in a minibuffer as a string.
389 The current buffer must be a minibuffer. */)
392 int prompt_end = XINT (Fminibuffer_prompt_end ());
393 return make_buffer_string (prompt_end, ZV, 1);
396 DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
397 Sminibuffer_contents_no_properties, 0, 0, 0,
398 doc: /* Return the user input in a minibuffer as a string, without text-properties.
399 The current buffer must be a minibuffer. */)
402 int prompt_end = XINT (Fminibuffer_prompt_end ());
403 return make_buffer_string (prompt_end, ZV, 0);
406 DEFUN ("minibuffer-completion-contents", Fminibuffer_completion_contents,
407 Sminibuffer_completion_contents, 0, 0, 0,
408 doc: /* Return the user input in a minibuffer before point as a string.
409 That is what completion commands operate on.
410 The current buffer must be a minibuffer. */)
413 int prompt_end = XINT (Fminibuffer_prompt_end ());
414 if (PT < prompt_end)
415 error ("Cannot do completion in the prompt");
416 return make_buffer_string (prompt_end, PT, 1);
419 DEFUN ("delete-minibuffer-contents", Fdelete_minibuffer_contents,
420 Sdelete_minibuffer_contents, 0, 0, 0,
421 doc: /* Delete all user input in a minibuffer.
422 The current buffer must be a minibuffer. */)
425 int prompt_end = XINT (Fminibuffer_prompt_end ());
426 if (prompt_end < ZV)
427 del_range (prompt_end, ZV);
428 return Qnil;
432 /* Read from the minibuffer using keymap MAP and initial contents INITIAL,
433 putting point minus BACKUP_N bytes from the end of INITIAL,
434 prompting with PROMPT (a string), using history list HISTVAR
435 with initial position HISTPOS. INITIAL should be a string or a
436 cons of a string and an integer. BACKUP_N should be <= 0, or
437 Qnil, which is equivalent to 0. If INITIAL is a cons, BACKUP_N is
438 ignored and replaced with an integer that puts point at one-indexed
439 position N in INITIAL, where N is the CDR of INITIAL, or at the
440 beginning of INITIAL if N <= 0.
442 Normally return the result as a string (the text that was read),
443 but if EXPFLAG is nonzero, read it and return the object read.
444 If HISTVAR is given, save the value read on that history only if it doesn't
445 match the front of that history list exactly. The value is pushed onto
446 the list as the string that was read.
448 DEFALT specifies the default value for the sake of history commands.
450 If ALLOW_PROPS is nonzero, we do not throw away text properties.
452 if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
453 current input method. */
455 static Lisp_Object
456 read_minibuf (map, initial, prompt, backup_n, expflag,
457 histvar, histpos, defalt, allow_props, inherit_input_method)
458 Lisp_Object map;
459 Lisp_Object initial;
460 Lisp_Object prompt;
461 Lisp_Object backup_n;
462 int expflag;
463 Lisp_Object histvar;
464 Lisp_Object histpos;
465 Lisp_Object defalt;
466 int allow_props;
467 int inherit_input_method;
469 Lisp_Object val;
470 int count = SPECPDL_INDEX ();
471 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
472 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
473 Lisp_Object enable_multibyte;
474 int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
476 /* String to add to the history. */
477 Lisp_Object histstring;
479 Lisp_Object empty_minibuf;
480 Lisp_Object dummy, frame;
482 extern Lisp_Object Qfront_sticky;
483 extern Lisp_Object Qrear_nonsticky;
485 specbind (Qminibuffer_default, defalt);
487 single_kboard_state ();
488 #ifdef HAVE_X_WINDOWS
489 if (display_hourglass_p)
490 cancel_hourglass ();
491 #endif
493 if (!NILP (initial))
495 if (CONSP (initial))
497 backup_n = Fcdr (initial);
498 initial = Fcar (initial);
499 CHECK_STRING (initial);
500 if (!NILP (backup_n))
502 CHECK_NUMBER (backup_n);
503 /* Convert to distance from end of input. */
504 if (XINT (backup_n) < 1)
505 /* A number too small means the beginning of the string. */
506 pos = - SCHARS (initial);
507 else
508 pos = XINT (backup_n) - 1 - SCHARS (initial);
511 else
512 CHECK_STRING (initial);
514 val = Qnil;
515 ambient_dir = current_buffer->directory;
516 input_method = Qnil;
517 enable_multibyte = Qnil;
519 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
520 store them away before we can GC. Don't need to protect
521 BACKUP_N because we use the value only if it is an integer. */
522 GCPRO5 (map, initial, val, ambient_dir, input_method);
524 if (!STRINGP (prompt))
525 prompt = empty_string;
527 if (!enable_recursive_minibuffers
528 && minibuf_level > 0)
530 if (EQ (selected_window, minibuf_window))
531 error ("Command attempted to use minibuffer while in minibuffer");
532 else
533 /* If we're in another window, cancel the minibuffer that's active. */
534 Fthrow (Qexit,
535 build_string ("Command attempted to use minibuffer while in minibuffer"));
538 if (noninteractive && NILP (Vexecuting_kbd_macro))
540 val = read_minibuf_noninteractive (map, initial, prompt,
541 make_number (pos),
542 expflag, histvar, histpos, defalt,
543 allow_props, inherit_input_method);
544 UNGCPRO;
545 return unbind_to (count, val);
548 /* Choose the minibuffer window and frame, and take action on them. */
550 choose_minibuf_frame ();
552 record_unwind_protect (choose_minibuf_frame_1, Qnil);
554 record_unwind_protect (Fset_window_configuration,
555 Fcurrent_window_configuration (Qnil));
557 /* If the minibuffer window is on a different frame, save that
558 frame's configuration too. */
559 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
560 if (!EQ (mini_frame, selected_frame))
561 record_unwind_protect (Fset_window_configuration,
562 Fcurrent_window_configuration (mini_frame));
564 /* If the minibuffer is on an iconified or invisible frame,
565 make it visible now. */
566 Fmake_frame_visible (mini_frame);
568 if (minibuffer_auto_raise)
569 Fraise_frame (mini_frame);
571 /* We have to do this after saving the window configuration
572 since that is what restores the current buffer. */
574 /* Arrange to restore a number of minibuffer-related variables.
575 We could bind each variable separately, but that would use lots of
576 specpdl slots. */
577 minibuf_save_list
578 = Fcons (Voverriding_local_map,
579 Fcons (minibuf_window,
580 Fcons (Vminibuffer_completing_file_name,
581 minibuf_save_list)));
582 minibuf_save_list
583 = Fcons (minibuf_prompt,
584 Fcons (make_number (minibuf_prompt_width),
585 Fcons (Vhelp_form,
586 Fcons (Vcurrent_prefix_arg,
587 Fcons (Vminibuffer_history_position,
588 Fcons (Vminibuffer_history_variable,
589 minibuf_save_list))))));
591 record_unwind_protect (read_minibuf_unwind, Qnil);
592 minibuf_level++;
593 /* We are exiting the minibuffer one way or the other, so run the hook.
594 It should be run before unwinding the minibuf settings. Do it
595 separately from read_minibuf_unwind because we need to make sure that
596 read_minibuf_unwind is fully executed even if exit-minibuffer-hook
597 signals an error. --Stef */
598 record_unwind_protect (run_exit_minibuf_hook, Qnil);
600 /* Now that we can restore all those variables, start changing them. */
602 minibuf_prompt_width = 0;
603 minibuf_prompt = Fcopy_sequence (prompt);
604 Vminibuffer_history_position = histpos;
605 Vminibuffer_history_variable = histvar;
606 Vhelp_form = Vminibuffer_help_form;
607 /* If this minibuffer is reading a file name,
608 that doesn't mean recursive ones are. */
609 Vminibuffer_completing_file_name = Qnil;
611 if (inherit_input_method)
613 /* `current-input-method' is buffer local. So, remember it in
614 INPUT_METHOD before changing the current buffer. */
615 input_method = Fsymbol_value (Qcurrent_input_method);
616 enable_multibyte = current_buffer->enable_multibyte_characters;
619 /* Switch to the minibuffer. */
621 minibuffer = get_minibuffer (minibuf_level);
622 Fset_buffer (minibuffer);
624 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
625 if (inherit_input_method)
626 current_buffer->enable_multibyte_characters = enable_multibyte;
628 /* The current buffer's default directory is usually the right thing
629 for our minibuffer here. However, if you're typing a command at
630 a minibuffer-only frame when minibuf_level is zero, then buf IS
631 the current_buffer, so reset_buffer leaves buf's default
632 directory unchanged. This is a bummer when you've just started
633 up Emacs and buf's default directory is Qnil. Here's a hack; can
634 you think of something better to do? Find another buffer with a
635 better directory, and use that one instead. */
636 if (STRINGP (ambient_dir))
637 current_buffer->directory = ambient_dir;
638 else
640 Lisp_Object buf_list;
642 for (buf_list = Vbuffer_alist;
643 CONSP (buf_list);
644 buf_list = XCDR (buf_list))
646 Lisp_Object other_buf;
648 other_buf = XCDR (XCAR (buf_list));
649 if (STRINGP (XBUFFER (other_buf)->directory))
651 current_buffer->directory = XBUFFER (other_buf)->directory;
652 break;
657 if (!EQ (mini_frame, selected_frame))
658 Fredirect_frame_focus (selected_frame, mini_frame);
660 Vminibuf_scroll_window = selected_window;
661 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
662 minibuf_selected_window = selected_window;
664 /* Empty out the minibuffers of all frames other than the one
665 where we are going to display one now.
666 Set them to point to ` *Minibuf-0*', which is always empty. */
667 empty_minibuf = Fget_buffer (build_string (" *Minibuf-0*"));
669 FOR_EACH_FRAME (dummy, frame)
671 Lisp_Object root_window = Fframe_root_window (frame);
672 Lisp_Object mini_window = XWINDOW (root_window)->next;
674 if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
675 && !NILP (Fwindow_minibuffer_p (mini_window)))
676 Fset_window_buffer (mini_window, empty_minibuf, Qnil);
679 /* Display this minibuffer in the proper window. */
680 Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
681 Fselect_window (minibuf_window, Qnil);
682 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
684 Fmake_local_variable (Qprint_escape_newlines);
685 print_escape_newlines = 1;
687 /* Erase the buffer. */
689 int count1 = SPECPDL_INDEX ();
690 specbind (Qinhibit_read_only, Qt);
691 specbind (Qinhibit_modification_hooks, Qt);
692 Ferase_buffer ();
693 unbind_to (count1, Qnil);
696 if (!NILP (current_buffer->enable_multibyte_characters)
697 && ! STRING_MULTIBYTE (minibuf_prompt))
698 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
700 /* Insert the prompt, record where it ends. */
701 Finsert (1, &minibuf_prompt);
702 if (PT > BEG)
704 Fput_text_property (make_number (BEG), make_number (PT),
705 Qfront_sticky, Qt, Qnil);
706 Fput_text_property (make_number (BEG), make_number (PT),
707 Qrear_nonsticky, Qt, Qnil);
708 Fput_text_property (make_number (BEG), make_number (PT),
709 Qfield, Qt, Qnil);
710 Fadd_text_properties (make_number (BEG), make_number (PT),
711 Vminibuffer_prompt_properties, Qnil);
714 minibuf_prompt_width = (int) current_column (); /* iftc */
716 /* Put in the initial input. */
717 if (!NILP (initial))
719 Finsert (1, &initial);
720 Fforward_char (make_number (pos));
723 clear_message (1, 1);
724 current_buffer->keymap = map;
726 /* Turn on an input method stored in INPUT_METHOD if any. */
727 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
728 call1 (Qactivate_input_method, input_method);
730 /* Run our hook, but not if it is empty.
731 (run-hooks would do nothing if it is empty,
732 but it's important to save time here in the usual case.) */
733 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
734 && !NILP (Vrun_hooks))
735 call1 (Vrun_hooks, Qminibuffer_setup_hook);
737 /* Don't allow the user to undo past this point. */
738 current_buffer->undo_list = Qnil;
740 recursive_edit_1 ();
742 /* If cursor is on the minibuffer line,
743 show the user we have exited by putting it in column 0. */
744 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
745 && !noninteractive)
747 XWINDOW (minibuf_window)->cursor.hpos = 0;
748 XWINDOW (minibuf_window)->cursor.x = 0;
749 XWINDOW (minibuf_window)->must_be_updated_p = 1;
750 update_frame (XFRAME (selected_frame), 1, 1);
751 if (rif && rif->flush_display)
752 rif->flush_display (XFRAME (XWINDOW (minibuf_window)->frame));
755 /* Make minibuffer contents into a string. */
756 Fset_buffer (minibuffer);
757 if (allow_props)
758 val = Fminibuffer_contents ();
759 else
760 val = Fminibuffer_contents_no_properties ();
762 /* VAL is the string of minibuffer text. */
764 last_minibuf_string = val;
766 /* Choose the string to add to the history. */
767 if (SCHARS (val) != 0)
768 histstring = val;
769 else if (STRINGP (defalt))
770 histstring = defalt;
771 else
772 histstring = Qnil;
774 /* Add the value to the appropriate history list, if any. */
775 if (!NILP (Vhistory_add_new_input)
776 && SYMBOLP (Vminibuffer_history_variable)
777 && !NILP (histstring))
779 /* If the caller wanted to save the value read on a history list,
780 then do so if the value is not already the front of the list. */
781 Lisp_Object histval;
783 /* If variable is unbound, make it nil. */
784 if (EQ (SYMBOL_VALUE (Vminibuffer_history_variable), Qunbound))
785 Fset (Vminibuffer_history_variable, Qnil);
787 histval = Fsymbol_value (Vminibuffer_history_variable);
789 /* The value of the history variable must be a cons or nil. Other
790 values are unacceptable. We silently ignore these values. */
792 if (NILP (histval)
793 || (CONSP (histval)
794 /* Don't duplicate the most recent entry in the history. */
795 && (NILP (Fequal (histstring, Fcar (histval))))))
797 Lisp_Object length;
799 if (history_delete_duplicates) Fdelete (histstring, histval);
800 histval = Fcons (histstring, histval);
801 Fset (Vminibuffer_history_variable, histval);
803 /* Truncate if requested. */
804 length = Fget (Vminibuffer_history_variable, Qhistory_length);
805 if (NILP (length)) length = Vhistory_length;
806 if (INTEGERP (length))
808 if (XINT (length) <= 0)
809 Fset (Vminibuffer_history_variable, Qnil);
810 else
812 Lisp_Object temp;
814 temp = Fnthcdr (Fsub1 (length), histval);
815 if (CONSP (temp)) Fsetcdr (temp, Qnil);
821 /* If Lisp form desired instead of string, parse it. */
822 if (expflag)
823 val = string_to_object (val, defalt);
825 /* The appropriate frame will get selected
826 in set-window-configuration. */
827 UNGCPRO;
828 return unbind_to (count, val);
831 /* Return a buffer to be used as the minibuffer at depth `depth'.
832 depth = 0 is the lowest allowed argument, and that is the value
833 used for nonrecursive minibuffer invocations */
835 Lisp_Object
836 get_minibuffer (depth)
837 int depth;
839 Lisp_Object tail, num, buf;
840 char name[24];
841 extern Lisp_Object nconc2 ();
843 XSETFASTINT (num, depth);
844 tail = Fnthcdr (num, Vminibuffer_list);
845 if (NILP (tail))
847 tail = Fcons (Qnil, Qnil);
848 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
850 buf = Fcar (tail);
851 if (NILP (buf) || NILP (XBUFFER (buf)->name))
853 sprintf (name, " *Minibuf-%d*", depth);
854 buf = Fget_buffer_create (build_string (name));
856 /* Although the buffer's name starts with a space, undo should be
857 enabled in it. */
858 Fbuffer_enable_undo (buf);
860 XSETCAR (tail, buf);
862 else
864 int count = SPECPDL_INDEX ();
865 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
866 have to empty the list, otherwise we end up with overlays that
867 think they belong to this buffer while the buffer doesn't know about
868 them any more. */
869 delete_all_overlays (XBUFFER (buf));
870 reset_buffer (XBUFFER (buf));
871 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
872 Fset_buffer (buf);
873 Fkill_all_local_variables ();
874 unbind_to (count, Qnil);
877 return buf;
880 static Lisp_Object
881 run_exit_minibuf_hook (data)
882 Lisp_Object data;
884 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
885 && !NILP (Vrun_hooks))
886 safe_run_hooks (Qminibuffer_exit_hook);
888 return Qnil;
891 /* This function is called on exiting minibuffer, whether normally or
892 not, and it restores the current window, buffer, etc. */
894 static Lisp_Object
895 read_minibuf_unwind (data)
896 Lisp_Object data;
898 Lisp_Object old_deactivate_mark;
899 Lisp_Object window;
901 /* If this was a recursive minibuffer,
902 tie the minibuffer window back to the outer level minibuffer buffer. */
903 minibuf_level--;
905 window = minibuf_window;
906 /* To keep things predictable, in case it matters, let's be in the
907 minibuffer when we reset the relevant variables. */
908 Fset_buffer (XWINDOW (window)->buffer);
910 /* Restore prompt, etc, from outer minibuffer level. */
911 minibuf_prompt = Fcar (minibuf_save_list);
912 minibuf_save_list = Fcdr (minibuf_save_list);
913 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
914 minibuf_save_list = Fcdr (minibuf_save_list);
915 Vhelp_form = Fcar (minibuf_save_list);
916 minibuf_save_list = Fcdr (minibuf_save_list);
917 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
918 minibuf_save_list = Fcdr (minibuf_save_list);
919 Vminibuffer_history_position = Fcar (minibuf_save_list);
920 minibuf_save_list = Fcdr (minibuf_save_list);
921 Vminibuffer_history_variable = Fcar (minibuf_save_list);
922 minibuf_save_list = Fcdr (minibuf_save_list);
923 Voverriding_local_map = Fcar (minibuf_save_list);
924 minibuf_save_list = Fcdr (minibuf_save_list);
925 #if 0
926 temp = Fcar (minibuf_save_list);
927 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
928 minibuf_window = temp;
929 #endif
930 minibuf_save_list = Fcdr (minibuf_save_list);
931 Vminibuffer_completing_file_name = Fcar (minibuf_save_list);
932 minibuf_save_list = Fcdr (minibuf_save_list);
934 /* Erase the minibuffer we were using at this level. */
936 int count = SPECPDL_INDEX ();
937 /* Prevent error in erase-buffer. */
938 specbind (Qinhibit_read_only, Qt);
939 specbind (Qinhibit_modification_hooks, Qt);
940 old_deactivate_mark = Vdeactivate_mark;
941 Ferase_buffer ();
942 Vdeactivate_mark = old_deactivate_mark;
943 unbind_to (count, Qnil);
946 /* When we get to the outmost level, make sure we resize the
947 mini-window back to its normal size. */
948 if (minibuf_level == 0)
949 resize_mini_window (XWINDOW (window), 0);
951 /* Make sure minibuffer window is erased, not ignored. */
952 windows_or_buffers_changed++;
953 XSETFASTINT (XWINDOW (window)->last_modified, 0);
954 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
955 return Qnil;
959 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
960 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
961 The optional second arg INITIAL-CONTENTS is an obsolete alternative to
962 DEFAULT-VALUE. It normally should be nil in new code, except when
963 HIST is a cons. It is discussed in more detail below.
964 Third arg KEYMAP is a keymap to use whilst reading;
965 if omitted or nil, the default is `minibuffer-local-map'.
966 If fourth arg READ is non-nil, then interpret the result as a Lisp object
967 and return that object:
968 in other words, do `(car (read-from-string INPUT-STRING))'
969 Fifth arg HIST, if non-nil, specifies a history list and optionally
970 the initial position in the list. It can be a symbol, which is the
971 history list variable to use, or it can be a cons cell
972 (HISTVAR . HISTPOS). In that case, HISTVAR is the history list variable
973 to use, and HISTPOS is the initial position for use by the minibuffer
974 history commands. For consistency, you should also specify that
975 element of the history as the value of INITIAL-CONTENTS. Positions
976 are counted starting from 1 at the beginning of the list.
977 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
978 for history commands; but, unless READ is non-nil, `read-from-minibuffer'
979 does NOT return DEFAULT-VALUE if the user enters empty input! It returns
980 the empty string.
981 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
982 the current input method and the setting of `enable-multibyte-characters'.
983 If the variable `minibuffer-allow-text-properties' is non-nil,
984 then the string which is returned includes whatever text properties
985 were present in the minibuffer. Otherwise the value has no text properties.
987 The remainder of this documentation string describes the
988 INITIAL-CONTENTS argument in more detail. It is only relevant when
989 studying existing code, or when HIST is a cons. If non-nil,
990 INITIAL-CONTENTS is a string to be inserted into the minibuffer before
991 reading input. Normally, point is put at the end of that string.
992 However, if INITIAL-CONTENTS is \(STRING . POSITION), the initial
993 input is STRING, but point is placed at _one-indexed_ position
994 POSITION in the minibuffer. Any integer value less than or equal to
995 one puts point at the beginning of the string. *Note* that this
996 behavior differs from the way such arguments are used in `completing-read'
997 and some related functions, which use zero-indexing for POSITION. */)
998 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
999 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
1000 Lisp_Object inherit_input_method;
1002 Lisp_Object histvar, histpos, val;
1003 struct gcpro gcpro1;
1005 CHECK_STRING (prompt);
1006 if (NILP (keymap))
1007 keymap = Vminibuffer_local_map;
1008 else
1009 keymap = get_keymap (keymap, 1, 0);
1011 if (SYMBOLP (hist))
1013 histvar = hist;
1014 histpos = Qnil;
1016 else
1018 histvar = Fcar_safe (hist);
1019 histpos = Fcdr_safe (hist);
1021 if (NILP (histvar))
1022 histvar = Qminibuffer_history;
1023 if (NILP (histpos))
1024 XSETFASTINT (histpos, 0);
1026 GCPRO1 (default_value);
1027 val = read_minibuf (keymap, initial_contents, prompt,
1028 Qnil, !NILP (read),
1029 histvar, histpos, default_value,
1030 minibuffer_allow_text_properties,
1031 !NILP (inherit_input_method));
1032 UNGCPRO;
1033 return val;
1036 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
1037 doc: /* Return a Lisp object read using the minibuffer, unevaluated.
1038 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
1039 is a string to insert in the minibuffer before reading.
1040 \(INITIAL-CONTENTS can also be a cons of a string and an integer. Such
1041 arguments are used as in `read-from-minibuffer'.) */)
1042 (prompt, initial_contents)
1043 Lisp_Object prompt, initial_contents;
1045 CHECK_STRING (prompt);
1046 return read_minibuf (Vminibuffer_local_map, initial_contents,
1047 prompt, Qnil, 1, Qminibuffer_history,
1048 make_number (0), Qnil, 0, 0);
1051 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
1052 doc: /* Return value of Lisp expression read using the minibuffer.
1053 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
1054 is a string to insert in the minibuffer before reading.
1055 \(INITIAL-CONTENTS can also be a cons of a string and an integer. Such
1056 arguments are used as in `read-from-minibuffer'.) */)
1057 (prompt, initial_contents)
1058 Lisp_Object prompt, initial_contents;
1060 return Feval (read_minibuf (Vread_expression_map, initial_contents,
1061 prompt, Qnil, 1, Qread_expression_history,
1062 make_number (0), Qnil, 0, 0));
1065 /* Functions that use the minibuffer to read various things. */
1067 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
1068 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
1069 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
1070 This argument has been superseded by DEFAULT-VALUE and should normally
1071 be nil in new code. It behaves as in `read-from-minibuffer'. See the
1072 documentation string of that function for details.
1073 The third arg HISTORY, if non-nil, specifies a history list
1074 and optionally the initial position in the list.
1075 See `read-from-minibuffer' for details of HISTORY argument.
1076 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
1077 for history commands, and as the value to return if the user enters
1078 the empty string.
1079 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1080 the current input method and the setting of `enable-multibyte-characters'. */)
1081 (prompt, initial_input, history, default_value, inherit_input_method)
1082 Lisp_Object prompt, initial_input, history, default_value;
1083 Lisp_Object inherit_input_method;
1085 Lisp_Object val;
1086 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
1087 Qnil, history, default_value,
1088 inherit_input_method);
1089 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
1090 val = default_value;
1091 return val;
1094 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
1095 doc: /* Read a string from the terminal, not allowing blanks.
1096 Prompt with PROMPT. Whitespace terminates the input. If INITIAL is
1097 non-nil, it should be a string, which is used as initial input, with
1098 point positioned at the end, so that SPACE will accept the input.
1099 \(Actually, INITIAL can also be a cons of a string and an integer.
1100 Such values are treated as in `read-from-minibuffer', but are normally
1101 not useful in this function.)
1102 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1103 the current input method and the setting of`enable-multibyte-characters'. */)
1104 (prompt, initial, inherit_input_method)
1105 Lisp_Object prompt, initial, inherit_input_method;
1107 CHECK_STRING (prompt);
1108 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
1109 0, Qminibuffer_history, make_number (0), Qnil, 0,
1110 !NILP (inherit_input_method));
1113 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
1114 doc: /* Read the name of a command and return as a symbol.
1115 Prompt with PROMPT. By default, return DEFAULT-VALUE. */)
1116 (prompt, default_value)
1117 Lisp_Object prompt, default_value;
1119 Lisp_Object name, default_string;
1121 if (NILP (default_value))
1122 default_string = Qnil;
1123 else if (SYMBOLP (default_value))
1124 default_string = SYMBOL_NAME (default_value);
1125 else
1126 default_string = default_value;
1128 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1129 Qnil, Qnil, default_string, Qnil);
1130 if (NILP (name))
1131 return name;
1132 return Fintern (name, Qnil);
1135 #ifdef NOTDEF
1136 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
1137 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
1138 Prompt with PROMPT. */)
1139 (prompt)
1140 Lisp_Object prompt;
1142 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
1143 Qnil);
1145 #endif /* NOTDEF */
1147 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
1148 doc: /* Read the name of a user variable and return it as a symbol.
1149 Prompt with PROMPT. By default, return DEFAULT-VALUE.
1150 A user variable is one for which `user-variable-p' returns non-nil. */)
1151 (prompt, default_value)
1152 Lisp_Object prompt, default_value;
1154 Lisp_Object name, default_string;
1156 if (NILP (default_value))
1157 default_string = Qnil;
1158 else if (SYMBOLP (default_value))
1159 default_string = SYMBOL_NAME (default_value);
1160 else
1161 default_string = default_value;
1163 name = Fcompleting_read (prompt, Vobarray,
1164 Quser_variable_p, Qt,
1165 Qnil, Qnil, default_string, Qnil);
1166 if (NILP (name))
1167 return name;
1168 return Fintern (name, Qnil);
1171 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
1172 doc: /* Read the name of a buffer and return as a string.
1173 Prompt with PROMPT.
1174 Optional second arg DEF is value to return if user enters an empty line.
1175 If optional third arg REQUIRE-MATCH is non-nil,
1176 only existing buffer names are allowed.
1177 The argument PROMPT should be a string ending with a colon and a space. */)
1178 (prompt, def, require_match)
1179 Lisp_Object prompt, def, require_match;
1181 Lisp_Object args[4];
1182 unsigned char *s;
1183 int len;
1185 if (BUFFERP (def))
1186 def = XBUFFER (def)->name;
1188 if (NILP (Vread_buffer_function))
1190 if (!NILP (def))
1192 /* A default value was provided: we must change PROMPT,
1193 editing the default value in before the colon. To achieve
1194 this, we replace PROMPT with a substring that doesn't
1195 contain the terminal space and colon (if present). They
1196 are then added back using Fformat. */
1198 if (STRINGP (prompt))
1200 s = SDATA (prompt);
1201 len = strlen (s);
1202 if (len >= 2 && s[len - 2] == ':' && s[len - 1] == ' ')
1203 len = len - 2;
1204 else if (len >= 1 && (s[len - 1] == ':' || s[len - 1] == ' '))
1205 len--;
1207 prompt = make_specified_string (s, -1, len,
1208 STRING_MULTIBYTE (prompt));
1211 args[0] = build_string ("%s (default %s): ");
1212 args[1] = prompt;
1213 args[2] = def;
1214 prompt = Fformat (3, args);
1217 return Fcompleting_read (prompt, intern ("internal-complete-buffer"),
1218 Qnil, require_match, Qnil, Qbuffer_name_history,
1219 def, Qnil);
1221 else
1223 args[0] = Vread_buffer_function;
1224 args[1] = prompt;
1225 args[2] = def;
1226 args[3] = require_match;
1227 return Ffuncall(4, args);
1231 static Lisp_Object
1232 minibuf_conform_representation (string, basis)
1233 Lisp_Object string, basis;
1235 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1236 return string;
1238 if (STRING_MULTIBYTE (string))
1239 return Fstring_make_unibyte (string);
1240 else
1241 return Fstring_make_multibyte (string);
1244 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1245 doc: /* Return common substring of all completions of STRING in COLLECTION.
1246 Test each possible completion specified by COLLECTION
1247 to see if it begins with STRING. The possible completions may be
1248 strings or symbols. Symbols are converted to strings before testing,
1249 see `symbol-name'.
1250 All that match STRING are compared together; the longest initial sequence
1251 common to all these matches is the return value.
1252 If there is no match at all, the return value is nil.
1253 For a unique match which is exact, the return value is t.
1255 If COLLECTION is an alist, the keys (cars of elements) are the
1256 possible completions. If an element is not a cons cell, then the
1257 element itself is the possible completion.
1258 If COLLECTION is a hash-table, all the keys that are strings or symbols
1259 are the possible completions.
1260 If COLLECTION is an obarray, the names of all symbols in the obarray
1261 are the possible completions.
1263 COLLECTION can also be a function to do the completion itself.
1264 It receives three arguments: the values STRING, PREDICATE and nil.
1265 Whatever it returns becomes the value of `try-completion'.
1267 If optional third argument PREDICATE is non-nil,
1268 it is used to test each possible match.
1269 The match is a candidate only if PREDICATE returns non-nil.
1270 The argument given to PREDICATE is the alist element
1271 or the symbol from the obarray. If COLLECTION is a hash-table,
1272 predicate is called with two arguments: the key and the value.
1273 Additionally to this predicate, `completion-regexp-list'
1274 is used to further constrain the set of candidates. */)
1275 (string, collection, predicate)
1276 Lisp_Object string, collection, predicate;
1278 Lisp_Object bestmatch, tail, elt, eltstring;
1279 /* Size in bytes of BESTMATCH. */
1280 int bestmatchsize = 0;
1281 /* These are in bytes, too. */
1282 int compare, matchsize;
1283 int type = (HASH_TABLE_P (collection) ? 3
1284 : VECTORP (collection) ? 2
1285 : NILP (collection) || (CONSP (collection)
1286 && (!SYMBOLP (XCAR (collection))
1287 || NILP (XCAR (collection)))));
1288 int index = 0, obsize = 0;
1289 int matchcount = 0;
1290 int bindcount = -1;
1291 Lisp_Object bucket, zero, end, tem;
1292 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1294 CHECK_STRING (string);
1295 if (type == 0)
1296 return call3 (collection, string, predicate, Qnil);
1298 bestmatch = bucket = Qnil;
1299 zero = make_number (0);
1301 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1302 tail = collection;
1303 if (type == 2)
1305 collection = check_obarray (collection);
1306 obsize = XVECTOR (collection)->size;
1307 bucket = XVECTOR (collection)->contents[index];
1310 while (1)
1312 /* Get the next element of the alist, obarray, or hash-table. */
1313 /* Exit the loop if the elements are all used up. */
1314 /* elt gets the alist element or symbol.
1315 eltstring gets the name to check as a completion. */
1317 if (type == 1)
1319 if (!CONSP (tail))
1320 break;
1321 elt = XCAR (tail);
1322 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1323 tail = XCDR (tail);
1325 else if (type == 2)
1327 if (!EQ (bucket, zero))
1329 if (!SYMBOLP (bucket))
1330 error ("Bad data in guts of obarray");
1331 elt = bucket;
1332 eltstring = elt;
1333 if (XSYMBOL (bucket)->next)
1334 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1335 else
1336 XSETFASTINT (bucket, 0);
1338 else if (++index >= obsize)
1339 break;
1340 else
1342 bucket = XVECTOR (collection)->contents[index];
1343 continue;
1346 else /* if (type == 3) */
1348 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1349 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
1350 index++;
1351 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
1352 break;
1353 else
1354 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
1357 /* Is this element a possible completion? */
1359 if (SYMBOLP (eltstring))
1360 eltstring = Fsymbol_name (eltstring);
1362 if (STRINGP (eltstring)
1363 && SCHARS (string) <= SCHARS (eltstring)
1364 && (tem = Fcompare_strings (eltstring, zero,
1365 make_number (SCHARS (string)),
1366 string, zero, Qnil,
1367 completion_ignore_case ? Qt : Qnil),
1368 EQ (Qt, tem)))
1370 /* Yes. */
1371 Lisp_Object regexps;
1373 /* Ignore this element if it fails to match all the regexps. */
1375 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1376 regexps = XCDR (regexps))
1378 if (bindcount < 0) {
1379 bindcount = SPECPDL_INDEX ();
1380 specbind (Qcase_fold_search,
1381 completion_ignore_case ? Qt : Qnil);
1383 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1384 if (NILP (tem))
1385 break;
1387 if (CONSP (regexps))
1388 continue;
1391 /* Ignore this element if there is a predicate
1392 and the predicate doesn't like it. */
1394 if (!NILP (predicate))
1396 if (EQ (predicate, Qcommandp))
1397 tem = Fcommandp (elt, Qnil);
1398 else
1400 if (bindcount >= 0) {
1401 unbind_to (bindcount, Qnil);
1402 bindcount = -1;
1404 GCPRO4 (tail, string, eltstring, bestmatch);
1405 tem = type == 3
1406 ? call2 (predicate, elt,
1407 HASH_VALUE (XHASH_TABLE (collection), index - 1))
1408 : call1 (predicate, elt);
1409 UNGCPRO;
1411 if (NILP (tem)) continue;
1414 /* Update computation of how much all possible completions match */
1416 if (NILP (bestmatch))
1418 matchcount = 1;
1419 bestmatch = eltstring;
1420 bestmatchsize = SCHARS (eltstring);
1422 else
1424 compare = min (bestmatchsize, SCHARS (eltstring));
1425 tem = Fcompare_strings (bestmatch, zero,
1426 make_number (compare),
1427 eltstring, zero,
1428 make_number (compare),
1429 completion_ignore_case ? Qt : Qnil);
1430 if (EQ (tem, Qt))
1431 matchsize = compare;
1432 else if (XINT (tem) < 0)
1433 matchsize = - XINT (tem) - 1;
1434 else
1435 matchsize = XINT (tem) - 1;
1437 if (matchsize < 0)
1438 /* When can this happen ? -stef */
1439 matchsize = compare;
1440 if (completion_ignore_case)
1442 /* If this is an exact match except for case,
1443 use it as the best match rather than one that is not an
1444 exact match. This way, we get the case pattern
1445 of the actual match. */
1446 if ((matchsize == SCHARS (eltstring)
1447 && matchsize < SCHARS (bestmatch))
1449 /* If there is more than one exact match ignoring case,
1450 and one of them is exact including case,
1451 prefer that one. */
1452 /* If there is no exact match ignoring case,
1453 prefer a match that does not change the case
1454 of the input. */
1455 ((matchsize == SCHARS (eltstring))
1457 (matchsize == SCHARS (bestmatch))
1458 && (tem = Fcompare_strings (eltstring, zero,
1459 make_number (SCHARS (string)),
1460 string, zero,
1461 Qnil,
1462 Qnil),
1463 EQ (Qt, tem))
1464 && (tem = Fcompare_strings (bestmatch, zero,
1465 make_number (SCHARS (string)),
1466 string, zero,
1467 Qnil,
1468 Qnil),
1469 ! EQ (Qt, tem))))
1470 bestmatch = eltstring;
1472 if (bestmatchsize != SCHARS (eltstring)
1473 || bestmatchsize != matchsize)
1474 /* Don't count the same string multiple times. */
1475 matchcount++;
1476 bestmatchsize = matchsize;
1477 if (matchsize <= SCHARS (string)
1478 && matchcount > 1)
1479 /* No need to look any further. */
1480 break;
1485 if (bindcount >= 0) {
1486 unbind_to (bindcount, Qnil);
1487 bindcount = -1;
1490 if (NILP (bestmatch))
1491 return Qnil; /* No completions found */
1492 /* If we are ignoring case, and there is no exact match,
1493 and no additional text was supplied,
1494 don't change the case of what the user typed. */
1495 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1496 && SCHARS (bestmatch) > bestmatchsize)
1497 return minibuf_conform_representation (string, bestmatch);
1499 /* Return t if the supplied string is an exact match (counting case);
1500 it does not require any change to be made. */
1501 if (matchcount == 1 && bestmatchsize == SCHARS (string)
1502 && (tem = Fcompare_strings (bestmatch, make_number (0),
1503 make_number (bestmatchsize),
1504 string, make_number (0),
1505 make_number (bestmatchsize),
1506 Qnil),
1507 EQ (Qt, tem)))
1508 return Qt;
1510 XSETFASTINT (zero, 0); /* Else extract the part in which */
1511 XSETFASTINT (end, bestmatchsize); /* all completions agree */
1512 return Fsubstring (bestmatch, zero, end);
1515 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1516 doc: /* Search for partial matches to STRING in COLLECTION.
1517 Test each of the possible completions specified by COLLECTION
1518 to see if it begins with STRING. The possible completions may be
1519 strings or symbols. Symbols are converted to strings before testing,
1520 see `symbol-name'.
1521 The value is a list of all the possible completions that match STRING.
1523 If COLLECTION is an alist, the keys (cars of elements) are the
1524 possible completions. If an element is not a cons cell, then the
1525 element itself is the possible completion.
1526 If COLLECTION is a hash-table, all the keys that are strings or symbols
1527 are the possible completions.
1528 If COLLECTION is an obarray, the names of all symbols in the obarray
1529 are the possible completions.
1531 COLLECTION can also be a function to do the completion itself.
1532 It receives three arguments: the values STRING, PREDICATE and t.
1533 Whatever it returns becomes the value of `all-completions'.
1535 If optional third argument PREDICATE is non-nil,
1536 it is used to test each possible match.
1537 The match is a candidate only if PREDICATE returns non-nil.
1538 The argument given to PREDICATE is the alist element
1539 or the symbol from the obarray. If COLLECTION is a hash-table,
1540 predicate is called with two arguments: the key and the value.
1541 Additionally to this predicate, `completion-regexp-list'
1542 is used to further constrain the set of candidates.
1544 If the optional fourth argument HIDE-SPACES is non-nil,
1545 strings in COLLECTION that start with a space
1546 are ignored unless STRING itself starts with a space. */)
1547 (string, collection, predicate, hide_spaces)
1548 Lisp_Object string, collection, predicate, hide_spaces;
1550 Lisp_Object tail, elt, eltstring;
1551 Lisp_Object allmatches;
1552 int type = HASH_TABLE_P (collection) ? 3
1553 : VECTORP (collection) ? 2
1554 : NILP (collection) || (CONSP (collection)
1555 && (!SYMBOLP (XCAR (collection))
1556 || NILP (XCAR (collection))));
1557 int index = 0, obsize = 0;
1558 int bindcount = -1;
1559 Lisp_Object bucket, tem, zero;
1560 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1562 CHECK_STRING (string);
1563 if (type == 0)
1564 return call3 (collection, string, predicate, Qt);
1565 allmatches = bucket = Qnil;
1566 zero = make_number (0);
1568 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1569 tail = collection;
1570 if (type == 2)
1572 obsize = XVECTOR (collection)->size;
1573 bucket = XVECTOR (collection)->contents[index];
1576 while (1)
1578 /* Get the next element of the alist, obarray, or hash-table. */
1579 /* Exit the loop if the elements are all used up. */
1580 /* elt gets the alist element or symbol.
1581 eltstring gets the name to check as a completion. */
1583 if (type == 1)
1585 if (!CONSP (tail))
1586 break;
1587 elt = XCAR (tail);
1588 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1589 tail = XCDR (tail);
1591 else if (type == 2)
1593 if (!EQ (bucket, zero))
1595 elt = bucket;
1596 eltstring = elt;
1597 if (XSYMBOL (bucket)->next)
1598 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1599 else
1600 XSETFASTINT (bucket, 0);
1602 else if (++index >= obsize)
1603 break;
1604 else
1606 bucket = XVECTOR (collection)->contents[index];
1607 continue;
1610 else /* if (type == 3) */
1612 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1613 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
1614 index++;
1615 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
1616 break;
1617 else
1618 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
1621 /* Is this element a possible completion? */
1623 if (SYMBOLP (eltstring))
1624 eltstring = Fsymbol_name (eltstring);
1626 if (STRINGP (eltstring)
1627 && SCHARS (string) <= SCHARS (eltstring)
1628 /* If HIDE_SPACES, reject alternatives that start with space
1629 unless the input starts with space. */
1630 && ((SBYTES (string) > 0
1631 && SREF (string, 0) == ' ')
1632 || SREF (eltstring, 0) != ' '
1633 || NILP (hide_spaces))
1634 && (tem = Fcompare_strings (eltstring, zero,
1635 make_number (SCHARS (string)),
1636 string, zero,
1637 make_number (SCHARS (string)),
1638 completion_ignore_case ? Qt : Qnil),
1639 EQ (Qt, tem)))
1641 /* Yes. */
1642 Lisp_Object regexps;
1643 Lisp_Object zero;
1644 XSETFASTINT (zero, 0);
1646 /* Ignore this element if it fails to match all the regexps. */
1648 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1649 regexps = XCDR (regexps))
1651 if (bindcount < 0) {
1652 bindcount = SPECPDL_INDEX ();
1653 specbind (Qcase_fold_search,
1654 completion_ignore_case ? Qt : Qnil);
1656 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1657 if (NILP (tem))
1658 break;
1660 if (CONSP (regexps))
1661 continue;
1664 /* Ignore this element if there is a predicate
1665 and the predicate doesn't like it. */
1667 if (!NILP (predicate))
1669 if (EQ (predicate, Qcommandp))
1670 tem = Fcommandp (elt, Qnil);
1671 else
1673 if (bindcount >= 0) {
1674 unbind_to (bindcount, Qnil);
1675 bindcount = -1;
1677 GCPRO4 (tail, eltstring, allmatches, string);
1678 tem = type == 3
1679 ? call2 (predicate, elt,
1680 HASH_VALUE (XHASH_TABLE (collection), index - 1))
1681 : call1 (predicate, elt);
1682 UNGCPRO;
1684 if (NILP (tem)) continue;
1686 /* Ok => put it on the list. */
1687 allmatches = Fcons (eltstring, allmatches);
1691 if (bindcount >= 0) {
1692 unbind_to (bindcount, Qnil);
1693 bindcount = -1;
1696 return Fnreverse (allmatches);
1699 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1700 doc: /* Read a string in the minibuffer, with completion.
1701 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1702 COLLECTION can be a list of strings, an alist, an obarray or a hash table.
1703 COLLECTION can also be a function to do the completion itself.
1704 PREDICATE limits completion to a subset of COLLECTION.
1705 See `try-completion' and `all-completions' for more details
1706 on completion, COLLECTION, and PREDICATE.
1708 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
1709 the input is (or completes to) an element of COLLECTION or is null.
1710 If it is also not t, typing RET does not exit if it does non-null completion.
1711 If the input is null, `completing-read' returns DEF, or an empty string
1712 if DEF is nil, regardless of the value of REQUIRE-MATCH.
1714 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
1715 with point positioned at the end.
1716 If it is (STRING . POSITION), the initial input is STRING, but point
1717 is placed at _zero-indexed_ position POSITION in STRING. (*Note*
1718 that this is different from `read-from-minibuffer' and related
1719 functions, which use one-indexing for POSITION.) This feature is
1720 deprecated--it is best to pass nil for INITIAL-INPUT and supply the
1721 default value DEF instead. The user can yank the default value into
1722 the minibuffer easily using \\[next-history-element].
1724 HIST, if non-nil, specifies a history list and optionally the initial
1725 position in the list. It can be a symbol, which is the history list
1726 variable to use, or it can be a cons cell (HISTVAR . HISTPOS). In
1727 that case, HISTVAR is the history list variable to use, and HISTPOS
1728 is the initial position (the position in the list used by the
1729 minibuffer history commands). For consistency, you should also
1730 specify that element of the history as the value of
1731 INITIAL-INPUT. (This is the only case in which you should use
1732 INITIAL-INPUT instead of DEF.) Positions are counted starting from
1733 1 at the beginning of the list. The variable `history-length'
1734 controls the maximum length of a history list.
1736 DEF, if non-nil, is the default value.
1738 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
1739 the current input method and the setting of `enable-multibyte-characters'.
1741 Completion ignores case if the ambient value of
1742 `completion-ignore-case' is non-nil. */)
1743 (prompt, collection, predicate, require_match, initial_input, hist, def, inherit_input_method)
1744 Lisp_Object prompt, collection, predicate, require_match, initial_input;
1745 Lisp_Object hist, def, inherit_input_method;
1747 Lisp_Object val, histvar, histpos, position;
1748 Lisp_Object init;
1749 int pos = 0;
1750 int count = SPECPDL_INDEX ();
1751 struct gcpro gcpro1;
1753 init = initial_input;
1754 GCPRO1 (def);
1756 specbind (Qminibuffer_completion_table, collection);
1757 specbind (Qminibuffer_completion_predicate, predicate);
1758 specbind (Qminibuffer_completion_confirm,
1759 EQ (require_match, Qt) ? Qnil : require_match);
1760 last_exact_completion = Qnil;
1762 position = Qnil;
1763 if (!NILP (init))
1765 if (CONSP (init))
1767 position = Fcdr (init);
1768 init = Fcar (init);
1770 CHECK_STRING (init);
1771 if (!NILP (position))
1773 CHECK_NUMBER (position);
1774 /* Convert to distance from end of input. */
1775 pos = XINT (position) - SCHARS (init);
1779 if (SYMBOLP (hist))
1781 histvar = hist;
1782 histpos = Qnil;
1784 else
1786 histvar = Fcar_safe (hist);
1787 histpos = Fcdr_safe (hist);
1789 if (NILP (histvar))
1790 histvar = Qminibuffer_history;
1791 if (NILP (histpos))
1792 XSETFASTINT (histpos, 0);
1794 val = read_minibuf (NILP (require_match)
1795 ? (NILP (Vminibuffer_completing_file_name)
1796 ? Vminibuffer_local_completion_map
1797 : Vminibuffer_local_filename_completion_map)
1798 : (NILP (Vminibuffer_completing_file_name)
1799 ? Vminibuffer_local_must_match_map
1800 : Vminibuffer_local_must_match_filename_map),
1801 init, prompt, make_number (pos), 0,
1802 histvar, histpos, def, 0,
1803 !NILP (inherit_input_method));
1805 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
1806 val = def;
1808 RETURN_UNGCPRO (unbind_to (count, val));
1811 Lisp_Object Fminibuffer_completion_help ();
1812 Lisp_Object Fassoc_string ();
1814 /* Test whether TXT is an exact completion. */
1815 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1816 doc: /* Return non-nil if STRING is a valid completion.
1817 Takes the same arguments as `all-completions' and `try-completion'.
1818 If COLLECTION is a function, it is called with three arguments:
1819 the values STRING, PREDICATE and `lambda'. */)
1820 (string, collection, predicate)
1821 Lisp_Object string, collection, predicate;
1823 Lisp_Object regexps, tail, tem = Qnil;
1824 int i = 0;
1826 CHECK_STRING (string);
1828 if ((CONSP (collection)
1829 && (!SYMBOLP (XCAR (collection)) || NILP (XCAR (collection))))
1830 || NILP (collection))
1832 tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
1833 if (NILP (tem))
1834 return Qnil;
1836 else if (VECTORP (collection))
1838 /* Bypass intern-soft as that loses for nil. */
1839 tem = oblookup (collection,
1840 SDATA (string),
1841 SCHARS (string),
1842 SBYTES (string));
1843 if (!SYMBOLP (tem))
1845 if (STRING_MULTIBYTE (string))
1846 string = Fstring_make_unibyte (string);
1847 else
1848 string = Fstring_make_multibyte (string);
1850 tem = oblookup (collection,
1851 SDATA (string),
1852 SCHARS (string),
1853 SBYTES (string));
1856 if (completion_ignore_case && !SYMBOLP (tem))
1858 for (i = XVECTOR (collection)->size - 1; i >= 0; i--)
1860 tail = XVECTOR (collection)->contents[i];
1861 if (SYMBOLP (tail))
1862 while (1)
1864 if (EQ((Fcompare_strings (string, make_number (0), Qnil,
1865 Fsymbol_name (tail),
1866 make_number (0) , Qnil, Qt)),
1867 Qt))
1869 tem = tail;
1870 break;
1872 if (XSYMBOL (tail)->next == 0)
1873 break;
1874 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1879 if (!SYMBOLP (tem))
1880 return Qnil;
1882 else if (HASH_TABLE_P (collection))
1884 struct Lisp_Hash_Table *h = XHASH_TABLE (collection);
1885 i = hash_lookup (h, string, NULL);
1886 if (i >= 0)
1887 tem = HASH_KEY (h, i);
1888 else
1889 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1890 if (!NILP (HASH_HASH (h, i)) &&
1891 EQ (Fcompare_strings (string, make_number (0), Qnil,
1892 HASH_KEY (h, i), make_number (0) , Qnil,
1893 completion_ignore_case ? Qt : Qnil),
1894 Qt))
1896 tem = HASH_KEY (h, i);
1897 break;
1899 if (!STRINGP (tem))
1900 return Qnil;
1902 else
1903 return call3 (collection, string, predicate, Qlambda);
1905 /* Reject this element if it fails to match all the regexps. */
1906 if (CONSP (Vcompletion_regexp_list))
1908 int count = SPECPDL_INDEX ();
1909 specbind (Qcase_fold_search, completion_ignore_case ? Qt : Qnil);
1910 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1911 regexps = XCDR (regexps))
1913 if (NILP (Fstring_match (XCAR (regexps),
1914 SYMBOLP (tem) ? string : tem,
1915 Qnil)))
1916 return unbind_to (count, Qnil);
1918 unbind_to (count, Qnil);
1921 /* Finally, check the predicate. */
1922 if (!NILP (predicate))
1924 return HASH_TABLE_P (collection)
1925 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (collection), i))
1926 : call1 (predicate, tem);
1928 else
1929 return Qt;
1932 DEFUN ("internal-complete-buffer", Finternal_complete_buffer, Sinternal_complete_buffer, 3, 3, 0,
1933 doc: /* Perform completion on buffer names.
1934 If the argument FLAG is nil, invoke `try-completion', if it's t, invoke
1935 `all-completions', otherwise invoke `test-completion'.
1937 The arguments STRING and PREDICATE are as in `try-completion',
1938 `all-completions', and `test-completion'. */)
1939 (string, predicate, flag)
1940 Lisp_Object string, predicate, flag;
1942 if (NILP (flag))
1943 return Ftry_completion (string, Vbuffer_alist, predicate);
1944 else if (EQ (flag, Qt))
1945 return Fall_completions (string, Vbuffer_alist, predicate, Qt);
1946 else /* assume `lambda' */
1947 return Ftest_completion (string, Vbuffer_alist, predicate);
1950 /* returns:
1951 * 0 no possible completion
1952 * 1 was already an exact and unique completion
1953 * 3 was already an exact completion
1954 * 4 completed to an exact completion
1955 * 5 some completion happened
1956 * 6 no completion happened
1959 do_completion ()
1961 Lisp_Object completion, string, tem;
1962 int completedp;
1963 Lisp_Object last;
1964 struct gcpro gcpro1, gcpro2;
1966 completion = Ftry_completion (Fminibuffer_completion_contents (),
1967 Vminibuffer_completion_table,
1968 Vminibuffer_completion_predicate);
1969 last = last_exact_completion;
1970 last_exact_completion = Qnil;
1972 GCPRO2 (completion, last);
1974 if (NILP (completion))
1976 bitch_at_user ();
1977 temp_echo_area_glyphs (build_string (" [No match]"));
1978 UNGCPRO;
1979 return 0;
1982 if (EQ (completion, Qt)) /* exact and unique match */
1984 UNGCPRO;
1985 return 1;
1988 string = Fminibuffer_completion_contents ();
1990 /* COMPLETEDP should be true if some completion was done, which
1991 doesn't include simply changing the case of the entered string.
1992 However, for appearance, the string is rewritten if the case
1993 changes. */
1994 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qt);
1995 completedp = !EQ (tem, Qt);
1997 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qnil);
1998 if (!EQ (tem, Qt))
1999 /* Rewrite the user's input. */
2001 int prompt_end = XINT (Fminibuffer_prompt_end ());
2002 /* Some completion happened */
2004 if (! NILP (Vminibuffer_completing_file_name)
2005 && SREF (completion, SBYTES (completion) - 1) == '/'
2006 && PT < ZV
2007 && FETCH_CHAR (PT_BYTE) == '/')
2009 del_range (prompt_end, PT + 1);
2011 else
2012 del_range (prompt_end, PT);
2014 Finsert (1, &completion);
2016 if (! completedp)
2017 /* The case of the string changed, but that's all. We're not
2018 sure whether this is a unique completion or not, so try again
2019 using the real case (this shouldn't recurse again, because
2020 the next time try-completion will return either `t' or the
2021 exact string). */
2023 UNGCPRO;
2024 return do_completion ();
2028 /* It did find a match. Do we match some possibility exactly now? */
2029 tem = Ftest_completion (Fminibuffer_contents (),
2030 Vminibuffer_completion_table,
2031 Vminibuffer_completion_predicate);
2032 if (NILP (tem))
2034 /* not an exact match */
2035 UNGCPRO;
2036 if (completedp)
2037 return 5;
2038 else if (!NILP (Vcompletion_auto_help))
2039 Fminibuffer_completion_help ();
2040 else
2041 temp_echo_area_glyphs (build_string (" [Next char not unique]"));
2042 return 6;
2044 else if (completedp)
2046 UNGCPRO;
2047 return 4;
2049 /* If the last exact completion and this one were the same,
2050 it means we've already given a "Complete but not unique"
2051 message and the user's hit TAB again, so now we give him help. */
2052 last_exact_completion = completion;
2053 if (!NILP (last))
2055 tem = Fminibuffer_completion_contents ();
2056 if (!NILP (Fequal (tem, last)))
2057 Fminibuffer_completion_help ();
2059 UNGCPRO;
2060 return 3;
2063 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
2065 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
2066 doc: /* Like `assoc' but specifically for strings.
2067 Unibyte strings are converted to multibyte for comparison.
2068 And case is ignored if CASE-FOLD is non-nil.
2069 As opposed to `assoc', it will also match an entry consisting of a single
2070 string rather than a cons cell whose car is a string. */)
2071 (key, list, case_fold)
2072 register Lisp_Object key;
2073 Lisp_Object list, case_fold;
2075 register Lisp_Object tail;
2077 for (tail = list; !NILP (tail); tail = Fcdr (tail))
2079 register Lisp_Object elt, tem, thiscar;
2080 elt = Fcar (tail);
2081 thiscar = CONSP (elt) ? XCAR (elt) : elt;
2082 if (!STRINGP (thiscar))
2083 continue;
2084 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
2085 key, make_number (0), Qnil,
2086 case_fold);
2087 if (EQ (tem, Qt))
2088 return elt;
2089 QUIT;
2091 return Qnil;
2094 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
2095 doc: /* Complete the minibuffer contents as far as possible.
2096 Return nil if there is no valid completion, else t.
2097 If no characters can be completed, display a list of possible completions.
2098 If you repeat this command after it displayed such a list,
2099 scroll the window of possible completions. */)
2102 register int i;
2103 Lisp_Object window, tem;
2105 /* If the previous command was not this,
2106 mark the completion buffer obsolete. */
2107 if (! EQ (current_kboard->Vlast_command, Vthis_command))
2108 Vminibuf_scroll_window = Qnil;
2110 window = Vminibuf_scroll_window;
2111 /* If there's a fresh completion window with a live buffer,
2112 and this command is repeated, scroll that window. */
2113 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
2114 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
2116 struct buffer *obuf = current_buffer;
2118 Fset_buffer (XWINDOW (window)->buffer);
2119 tem = Fpos_visible_in_window_p (make_number (ZV), window, Qnil);
2120 if (! NILP (tem))
2121 /* If end is in view, scroll up to the beginning. */
2122 Fset_window_start (window, make_number (BEGV), Qnil);
2123 else
2124 /* Else scroll down one screen. */
2125 Fscroll_other_window (Qnil);
2127 set_buffer_internal (obuf);
2128 return Qnil;
2131 i = do_completion ();
2132 switch (i)
2134 case 0:
2135 return Qnil;
2137 case 1:
2138 if (PT != ZV)
2139 Fgoto_char (make_number (ZV));
2140 temp_echo_area_glyphs (build_string (" [Sole completion]"));
2141 break;
2143 case 3:
2144 if (PT != ZV)
2145 Fgoto_char (make_number (ZV));
2146 temp_echo_area_glyphs (build_string (" [Complete, but not unique]"));
2147 break;
2150 return Qt;
2153 /* Subroutines of Fminibuffer_complete_and_exit. */
2155 /* This one is called by internal_condition_case to do the real work. */
2157 Lisp_Object
2158 complete_and_exit_1 ()
2160 return make_number (do_completion ());
2163 /* This one is called by internal_condition_case if an error happens.
2164 Pretend the current value is an exact match. */
2166 Lisp_Object
2167 complete_and_exit_2 (ignore)
2168 Lisp_Object ignore;
2170 return make_number (1);
2173 EXFUN (Fexit_minibuffer, 0) NO_RETURN;
2175 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
2176 Sminibuffer_complete_and_exit, 0, 0, "",
2177 doc: /* If the minibuffer contents is a valid completion then exit.
2178 Otherwise try to complete it. If completion leads to a valid completion,
2179 a repetition of this command will exit. */)
2182 register int i;
2183 Lisp_Object val, tem;
2185 /* Allow user to specify null string */
2186 if (XINT (Fminibuffer_prompt_end ()) == ZV)
2187 goto exit;
2189 val = Fminibuffer_contents ();
2190 tem = Ftest_completion (val,
2191 Vminibuffer_completion_table,
2192 Vminibuffer_completion_predicate);
2193 if (!NILP (tem))
2195 if (completion_ignore_case)
2196 { /* Fixup case of the field, if necessary. */
2197 Lisp_Object compl
2198 = Ftry_completion (val,
2199 Vminibuffer_completion_table,
2200 Vminibuffer_completion_predicate);
2201 if (STRINGP (compl)
2202 /* If it weren't for this piece of paranoia, I'd replace
2203 the whole thing with a call to do_completion. */
2204 && EQ (Flength (val), Flength (compl)))
2206 del_range (XINT (Fminibuffer_prompt_end ()), ZV);
2207 Finsert (1, &compl);
2210 goto exit;
2213 /* Call do_completion, but ignore errors. */
2214 SET_PT (ZV);
2215 val = internal_condition_case (complete_and_exit_1, Qerror,
2216 complete_and_exit_2);
2218 i = XFASTINT (val);
2219 switch (i)
2221 case 1:
2222 case 3:
2223 goto exit;
2225 case 4:
2226 if (!NILP (Vminibuffer_completion_confirm))
2228 temp_echo_area_glyphs (build_string (" [Confirm]"));
2229 return Qnil;
2231 else
2232 goto exit;
2234 default:
2235 return Qnil;
2237 exit:
2238 return Fexit_minibuffer ();
2239 /* NOTREACHED */
2242 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
2243 0, 0, "",
2244 doc: /* Complete the minibuffer contents at most a single word.
2245 After one word is completed as much as possible, a space or hyphen
2246 is added, provided that matches some possible completion.
2247 Return nil if there is no valid completion, else t. */)
2250 Lisp_Object completion, tem, tem1;
2251 register int i, i_byte;
2252 struct gcpro gcpro1, gcpro2;
2253 int prompt_end_charpos = XINT (Fminibuffer_prompt_end ());
2255 /* We keep calling Fbuffer_string rather than arrange for GC to
2256 hold onto a pointer to one of the strings thus made. */
2258 completion = Ftry_completion (Fminibuffer_completion_contents (),
2259 Vminibuffer_completion_table,
2260 Vminibuffer_completion_predicate);
2261 if (NILP (completion))
2263 bitch_at_user ();
2264 temp_echo_area_glyphs (build_string (" [No match]"));
2265 return Qnil;
2267 if (EQ (completion, Qt))
2268 return Qnil;
2270 #if 0 /* How the below code used to look, for reference. */
2271 tem = Fminibuffer_contents ();
2272 b = SDATA (tem);
2273 i = ZV - 1 - SCHARS (completion);
2274 p = SDATA (completion);
2275 if (i > 0 ||
2276 0 <= scmp (b, p, ZV - 1))
2278 i = 1;
2279 /* Set buffer to longest match of buffer tail and completion head. */
2280 while (0 <= scmp (b + i, p, ZV - 1 - i))
2281 i++;
2282 del_range (1, i + 1);
2283 SET_PT (ZV);
2285 #else /* Rewritten code */
2287 int buffer_nchars, completion_nchars;
2289 CHECK_STRING (completion);
2290 tem = Fminibuffer_completion_contents ();
2291 GCPRO2 (completion, tem);
2292 /* If reading a file name,
2293 expand any $ENVVAR refs in the buffer and in TEM. */
2294 if (! NILP (Vminibuffer_completing_file_name))
2296 Lisp_Object substituted;
2297 substituted = Fsubstitute_in_file_name (tem);
2298 if (! EQ (substituted, tem))
2300 tem = substituted;
2301 del_range (prompt_end_charpos, PT);
2302 Finsert (1, &tem);
2305 buffer_nchars = SCHARS (tem); /* # chars in what we completed. */
2306 completion_nchars = SCHARS (completion);
2307 i = buffer_nchars - completion_nchars;
2308 if (i > 0
2310 (tem1 = Fcompare_strings (tem, make_number (0),
2311 make_number (buffer_nchars),
2312 completion, make_number (0),
2313 make_number (buffer_nchars),
2314 completion_ignore_case ? Qt : Qnil),
2315 ! EQ (tem1, Qt)))
2317 int start_pos;
2319 /* Make buffer (before point) contain the longest match
2320 of TEM's tail and COMPLETION's head. */
2321 if (i <= 0) i = 1;
2322 start_pos= i;
2323 buffer_nchars -= i;
2324 while (i > 0)
2326 tem1 = Fcompare_strings (tem, make_number (start_pos), Qnil,
2327 completion, make_number (0),
2328 make_number (buffer_nchars),
2329 completion_ignore_case ? Qt : Qnil);
2330 start_pos++;
2331 if (EQ (tem1, Qt))
2332 break;
2333 i++;
2334 buffer_nchars--;
2336 del_range (start_pos, start_pos + buffer_nchars);
2338 UNGCPRO;
2340 #endif /* Rewritten code */
2343 int prompt_end_bytepos;
2344 prompt_end_bytepos = CHAR_TO_BYTE (prompt_end_charpos);
2345 i = PT - prompt_end_charpos;
2346 i_byte = PT_BYTE - prompt_end_bytepos;
2349 /* If completion finds next char not unique,
2350 consider adding a space or a hyphen. */
2351 if (i == SCHARS (completion))
2353 GCPRO1 (completion);
2354 tem = Ftry_completion (concat2 (Fminibuffer_completion_contents (),
2355 build_string (" ")),
2356 Vminibuffer_completion_table,
2357 Vminibuffer_completion_predicate);
2358 UNGCPRO;
2360 if (STRINGP (tem))
2361 completion = tem;
2362 else
2364 GCPRO1 (completion);
2365 tem =
2366 Ftry_completion (concat2 (Fminibuffer_completion_contents (),
2367 build_string ("-")),
2368 Vminibuffer_completion_table,
2369 Vminibuffer_completion_predicate);
2370 UNGCPRO;
2372 if (STRINGP (tem))
2373 completion = tem;
2377 /* Now find first word-break in the stuff found by completion.
2378 i gets index in string of where to stop completing. */
2380 int len, c;
2381 int bytes = SBYTES (completion);
2382 register const unsigned char *completion_string = SDATA (completion);
2383 for (; i_byte < SBYTES (completion); i_byte += len, i++)
2385 c = STRING_CHAR_AND_LENGTH (completion_string + i_byte,
2386 bytes - i_byte,
2387 len);
2388 if (SYNTAX (c) != Sword)
2390 i_byte += len;
2391 i++;
2392 break;
2397 /* If got no characters, print help for user. */
2399 if (i == PT - prompt_end_charpos)
2401 if (!NILP (Vcompletion_auto_help))
2402 Fminibuffer_completion_help ();
2403 return Qnil;
2406 /* Otherwise insert in minibuffer the chars we got */
2408 if (! NILP (Vminibuffer_completing_file_name)
2409 && SREF (completion, SBYTES (completion) - 1) == '/'
2410 && PT < ZV
2411 && FETCH_CHAR (PT_BYTE) == '/')
2413 del_range (prompt_end_charpos, PT + 1);
2415 else
2416 del_range (prompt_end_charpos, PT);
2418 insert_from_string (completion, 0, 0, i, i_byte, 1);
2419 return Qt;
2422 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
2423 1, 2, 0,
2424 doc: /* Display the list of completions, COMPLETIONS, using `standard-output'.
2425 Each element may be just a symbol or string
2426 or may be a list of two strings to be printed as if concatenated.
2427 If it is a list of two strings, the first is the actual completion
2428 alternative, the second serves as annotation.
2429 `standard-output' must be a buffer.
2430 The actual completion alternatives, as inserted, are given `mouse-face'
2431 properties of `highlight'.
2432 At the end, this runs the normal hook `completion-setup-hook'.
2433 It can find the completion buffer in `standard-output'.
2434 The optional second arg COMMON-SUBSTRING is a string.
2435 It is used to put faces, `completions-first-difference' and
2436 `completions-common-part' on the completion buffer. The
2437 `completions-common-part' face is put on the common substring
2438 specified by COMMON-SUBSTRING. If COMMON-SUBSTRING is nil
2439 and the current buffer is not the minibuffer, the faces are not put.
2440 Internally, COMMON-SUBSTRING is bound to `completion-common-substring'
2441 during running `completion-setup-hook'. */)
2442 (completions, common_substring)
2443 Lisp_Object completions;
2444 Lisp_Object common_substring;
2446 Lisp_Object tail, elt;
2447 register int i;
2448 int column = 0;
2449 struct gcpro gcpro1, gcpro2, gcpro3;
2450 struct buffer *old = current_buffer;
2451 int first = 1;
2453 /* Note that (when it matters) every variable
2454 points to a non-string that is pointed to by COMPLETIONS,
2455 except for ELT. ELT can be pointing to a string
2456 when terpri or Findent_to calls a change hook. */
2457 elt = Qnil;
2458 GCPRO3 (completions, elt, common_substring);
2460 if (BUFFERP (Vstandard_output))
2461 set_buffer_internal (XBUFFER (Vstandard_output));
2463 if (NILP (completions))
2464 write_string ("There are no possible completions of what you have typed.",
2465 -1);
2466 else
2468 write_string ("Possible completions are:", -1);
2469 for (tail = completions, i = 0; CONSP (tail); tail = XCDR (tail), i++)
2471 Lisp_Object tem, string;
2472 int length;
2473 Lisp_Object startpos, endpos;
2475 startpos = Qnil;
2477 elt = XCAR (tail);
2478 if (SYMBOLP (elt))
2479 elt = SYMBOL_NAME (elt);
2480 /* Compute the length of this element. */
2481 if (CONSP (elt))
2483 tem = XCAR (elt);
2484 CHECK_STRING (tem);
2485 length = SCHARS (tem);
2487 tem = Fcar (XCDR (elt));
2488 CHECK_STRING (tem);
2489 length += SCHARS (tem);
2491 else
2493 CHECK_STRING (elt);
2494 length = SCHARS (elt);
2497 /* This does a bad job for narrower than usual windows.
2498 Sadly, the window it will appear in is not known
2499 until after the text has been made. */
2501 if (BUFFERP (Vstandard_output))
2502 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2504 /* If the previous completion was very wide,
2505 or we have two on this line already,
2506 don't put another on the same line. */
2507 if (column > 33 || first
2508 /* If this is really wide, don't put it second on a line. */
2509 || (column > 0 && length > 45))
2511 Fterpri (Qnil);
2512 column = 0;
2514 /* Otherwise advance to column 35. */
2515 else
2517 if (BUFFERP (Vstandard_output))
2519 tem = Findent_to (make_number (35), make_number (2));
2521 column = XINT (tem);
2523 else
2527 write_string (" ", -1);
2528 column++;
2530 while (column < 35);
2534 if (BUFFERP (Vstandard_output))
2536 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2537 Fset_text_properties (startpos, endpos,
2538 Qnil, Vstandard_output);
2541 /* Output this element.
2542 If necessary, convert it to unibyte or to multibyte first. */
2543 if (CONSP (elt))
2544 string = Fcar (elt);
2545 else
2546 string = elt;
2547 if (NILP (current_buffer->enable_multibyte_characters)
2548 && STRING_MULTIBYTE (string))
2549 string = Fstring_make_unibyte (string);
2550 else if (!NILP (current_buffer->enable_multibyte_characters)
2551 && !STRING_MULTIBYTE (string))
2552 string = Fstring_make_multibyte (string);
2554 if (BUFFERP (Vstandard_output))
2556 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2558 Fprinc (string, Qnil);
2560 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2562 Fput_text_property (startpos, endpos,
2563 Qmouse_face, intern ("highlight"),
2564 Vstandard_output);
2566 else
2568 Fprinc (string, Qnil);
2571 /* Output the annotation for this element. */
2572 if (CONSP (elt))
2574 if (BUFFERP (Vstandard_output))
2576 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2578 Fprinc (Fcar (Fcdr (elt)), Qnil);
2580 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2582 Fset_text_properties (startpos, endpos, Qnil,
2583 Vstandard_output);
2585 else
2587 Fprinc (Fcar (Fcdr (elt)), Qnil);
2592 /* Update COLUMN for what we have output. */
2593 column += length;
2595 /* If output is to a buffer, recompute COLUMN in a way
2596 that takes account of character widths. */
2597 if (BUFFERP (Vstandard_output))
2599 tem = Fcurrent_column ();
2600 column = XINT (tem);
2603 first = 0;
2607 if (BUFFERP (Vstandard_output))
2608 set_buffer_internal (old);
2610 if (!NILP (Vrun_hooks))
2612 int count1 = SPECPDL_INDEX ();
2614 specbind (intern ("completion-common-substring"), common_substring);
2615 call1 (Vrun_hooks, intern ("completion-setup-hook"));
2617 unbind_to (count1, Qnil);
2620 UNGCPRO;
2622 return Qnil;
2626 static Lisp_Object
2627 display_completion_list_1 (list)
2628 Lisp_Object list;
2630 return Fdisplay_completion_list (list, Qnil);
2633 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
2634 0, 0, "",
2635 doc: /* Display a list of possible completions of the current minibuffer contents. */)
2638 Lisp_Object completions;
2640 message ("Making completion list...");
2641 completions = Fall_completions (Fminibuffer_completion_contents (),
2642 Vminibuffer_completion_table,
2643 Vminibuffer_completion_predicate,
2644 Qt);
2645 clear_message (1, 0);
2647 if (NILP (completions))
2649 bitch_at_user ();
2650 temp_echo_area_glyphs (build_string (" [No completions]"));
2652 else
2654 /* Sort and remove duplicates. */
2655 Lisp_Object tmp = completions = Fsort (completions, Qstring_lessp);
2656 while (CONSP (tmp))
2658 if (CONSP (XCDR (tmp))
2659 && !NILP (Fequal (XCAR (tmp), XCAR (XCDR (tmp)))))
2660 XSETCDR (tmp, XCDR (XCDR (tmp)));
2661 else
2662 tmp = XCDR (tmp);
2664 internal_with_output_to_temp_buffer ("*Completions*",
2665 display_completion_list_1,
2666 completions);
2668 return Qnil;
2671 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
2672 doc: /* Terminate minibuffer input. */)
2675 if (INTEGERP (last_command_char))
2676 internal_self_insert (XINT (last_command_char), 0);
2677 else
2678 bitch_at_user ();
2680 return Fexit_minibuffer ();
2683 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
2684 doc: /* Terminate this minibuffer argument. */)
2687 /* If the command that uses this has made modifications in the minibuffer,
2688 we don't want them to cause deactivation of the mark in the original
2689 buffer.
2690 A better solution would be to make deactivate-mark buffer-local
2691 (or to turn it into a list of buffers, ...), but in the mean time,
2692 this should do the trick in most cases. */
2693 Vdeactivate_mark = Qnil;
2694 Fthrow (Qexit, Qnil);
2697 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
2698 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
2701 return make_number (minibuf_level);
2704 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
2705 doc: /* Return the prompt string of the currently-active minibuffer.
2706 If no minibuffer is active, return nil. */)
2709 return Fcopy_sequence (minibuf_prompt);
2713 /* Temporarily display STRING at the end of the current
2714 minibuffer contents. This is used to display things like
2715 "[No Match]" when the user requests a completion for a prefix
2716 that has no possible completions, and other quick, unobtrusive
2717 messages. */
2719 extern Lisp_Object Vminibuffer_message_timeout;
2721 void
2722 temp_echo_area_glyphs (string)
2723 Lisp_Object string;
2725 int osize = ZV;
2726 int osize_byte = ZV_BYTE;
2727 int opoint = PT;
2728 int opoint_byte = PT_BYTE;
2729 Lisp_Object oinhibit;
2730 oinhibit = Vinhibit_quit;
2732 /* Clear out any old echo-area message to make way for our new thing. */
2733 message (0);
2735 SET_PT_BOTH (osize, osize_byte);
2736 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 0);
2737 SET_PT_BOTH (opoint, opoint_byte);
2738 Vinhibit_quit = Qt;
2740 if (NUMBERP (Vminibuffer_message_timeout))
2741 sit_for (Vminibuffer_message_timeout, 0, 2);
2742 else
2743 sit_for (Qt, 0, 2);
2745 del_range_both (osize, osize_byte, ZV, ZV_BYTE, 1);
2746 SET_PT_BOTH (opoint, opoint_byte);
2747 if (!NILP (Vquit_flag))
2749 Vquit_flag = Qnil;
2750 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
2752 Vinhibit_quit = oinhibit;
2755 DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
2756 1, 1, 0,
2757 doc: /* Temporarily display STRING at the end of the minibuffer.
2758 The text is displayed for a period controlled by `minibuffer-message-timeout',
2759 or until the next input event arrives, whichever comes first. */)
2760 (string)
2761 Lisp_Object string;
2763 CHECK_STRING (string);
2764 temp_echo_area_glyphs (string);
2765 return Qnil;
2768 void
2769 init_minibuf_once ()
2771 Vminibuffer_list = Qnil;
2772 staticpro (&Vminibuffer_list);
2775 void
2776 syms_of_minibuf ()
2778 minibuf_level = 0;
2779 minibuf_prompt = Qnil;
2780 staticpro (&minibuf_prompt);
2782 minibuf_save_list = Qnil;
2783 staticpro (&minibuf_save_list);
2785 Qread_file_name_internal = intern ("read-file-name-internal");
2786 staticpro (&Qread_file_name_internal);
2788 Qminibuffer_default = intern ("minibuffer-default");
2789 staticpro (&Qminibuffer_default);
2790 Fset (Qminibuffer_default, Qnil);
2792 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
2793 staticpro (&Qminibuffer_completion_table);
2795 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
2796 staticpro (&Qminibuffer_completion_confirm);
2798 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
2799 staticpro (&Qminibuffer_completion_predicate);
2801 staticpro (&last_exact_completion);
2802 last_exact_completion = Qnil;
2804 staticpro (&last_minibuf_string);
2805 last_minibuf_string = Qnil;
2807 Quser_variable_p = intern ("user-variable-p");
2808 staticpro (&Quser_variable_p);
2810 Qminibuffer_history = intern ("minibuffer-history");
2811 staticpro (&Qminibuffer_history);
2813 Qbuffer_name_history = intern ("buffer-name-history");
2814 staticpro (&Qbuffer_name_history);
2815 Fset (Qbuffer_name_history, Qnil);
2817 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
2818 staticpro (&Qminibuffer_setup_hook);
2820 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
2821 staticpro (&Qminibuffer_exit_hook);
2823 Qhistory_length = intern ("history-length");
2824 staticpro (&Qhistory_length);
2826 Qcurrent_input_method = intern ("current-input-method");
2827 staticpro (&Qcurrent_input_method);
2829 Qactivate_input_method = intern ("activate-input-method");
2830 staticpro (&Qactivate_input_method);
2832 Qcase_fold_search = intern ("case-fold-search");
2833 staticpro (&Qcase_fold_search);
2835 Qread_expression_history = intern ("read-expression-history");
2836 staticpro (&Qread_expression_history);
2838 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2839 doc: /* If this is non-nil, `read-buffer' does its work by calling this function. */);
2840 Vread_buffer_function = Qnil;
2842 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2843 doc: /* Normal hook run just after entry to minibuffer. */);
2844 Vminibuffer_setup_hook = Qnil;
2846 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2847 doc: /* Normal hook run just after exit from minibuffer. */);
2848 Vminibuffer_exit_hook = Qnil;
2850 DEFVAR_LISP ("history-length", &Vhistory_length,
2851 doc: /* *Maximum length for history lists before truncation takes place.
2852 A number means that length; t means infinite. Truncation takes place
2853 just after a new element is inserted. Setting the `history-length'
2854 property of a history variable overrides this default. */);
2855 XSETFASTINT (Vhistory_length, 30);
2857 DEFVAR_BOOL ("history-delete-duplicates", &history_delete_duplicates,
2858 doc: /* *Non-nil means to delete duplicates in history.
2859 If set to t when adding a new history element, all previous identical
2860 elements are deleted from the history list. */);
2861 history_delete_duplicates = 0;
2863 DEFVAR_LISP ("history-add-new-input", &Vhistory_add_new_input,
2864 doc: /* *Non-nil means to add new elements in history.
2865 If set to nil, minibuffer reading functions don't add new elements to the
2866 history list, so it is possible to do this afterwards by calling
2867 `add-to-history' explicitly. */);
2868 Vhistory_add_new_input = Qt;
2870 DEFVAR_LISP ("completion-auto-help", &Vcompletion_auto_help,
2871 doc: /* *Non-nil means automatically provide help for invalid completion input.
2872 Under Partial Completion mode, a non-nil, non-t value has a special meaning;
2873 see the doc string of `partial-completion-mode' for more details. */);
2874 Vcompletion_auto_help = Qt;
2876 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
2877 doc: /* Non-nil means don't consider case significant in completion.
2879 For file-name completion, the variable `read-file-name-completion-ignore-case'
2880 controls the behavior, rather than this variable. */);
2881 completion_ignore_case = 0;
2883 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2884 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
2885 This variable makes a difference whenever the minibuffer window is active. */);
2886 enable_recursive_minibuffers = 0;
2888 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2889 doc: /* Alist or obarray used for completion in the minibuffer.
2890 This becomes the ALIST argument to `try-completion' and `all-completions'.
2891 The value can also be a list of strings or a hash table.
2893 The value may alternatively be a function, which is given three arguments:
2894 STRING, the current buffer contents;
2895 PREDICATE, the predicate for filtering possible matches;
2896 CODE, which says what kind of things to do.
2897 CODE can be nil, t or `lambda':
2898 nil -- return the best completion of STRING, or nil if there is none.
2899 t -- return a list of all possible completions of STRING.
2900 lambda -- return t if STRING is a valid completion as it stands. */);
2901 Vminibuffer_completion_table = Qnil;
2903 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2904 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
2905 Vminibuffer_completion_predicate = Qnil;
2907 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
2908 doc: /* Non-nil means to demand confirmation of completion before exiting minibuffer. */);
2909 Vminibuffer_completion_confirm = Qnil;
2911 DEFVAR_LISP ("minibuffer-completing-file-name",
2912 &Vminibuffer_completing_file_name,
2913 doc: /* Non-nil means completing file names. */);
2914 Vminibuffer_completing_file_name = Qnil;
2916 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2917 doc: /* Value that `help-form' takes on inside the minibuffer. */);
2918 Vminibuffer_help_form = Qnil;
2920 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2921 doc: /* History list symbol to add minibuffer values to.
2922 Each string of minibuffer input, as it appears on exit from the minibuffer,
2923 is added with
2924 (set minibuffer-history-variable
2925 (cons STRING (symbol-value minibuffer-history-variable))) */);
2926 XSETFASTINT (Vminibuffer_history_variable, 0);
2928 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2929 doc: /* Current position of redoing in the history list. */);
2930 Vminibuffer_history_position = Qnil;
2932 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2933 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
2934 Some uses of the echo area also raise that frame (since they use it too). */);
2935 minibuffer_auto_raise = 0;
2937 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
2938 doc: /* List of regexps that should restrict possible completions.
2939 The basic completion functions only consider a completion acceptable
2940 if it matches all regular expressions in this list, with
2941 `case-fold-search' bound to the value of `completion-ignore-case'.
2942 See Info node `(elisp)Basic Completion', for a description of these
2943 functions. */);
2944 Vcompletion_regexp_list = Qnil;
2946 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2947 &minibuffer_allow_text_properties,
2948 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
2949 This also affects `read-string', but it does not affect `read-minibuffer',
2950 `read-no-blanks-input', or any of the functions that do minibuffer input
2951 with completion; they always discard text properties. */);
2952 minibuffer_allow_text_properties = 0;
2954 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2955 doc: /* Text properties that are added to minibuffer prompts.
2956 These are in addition to the basic `field' property, and stickiness
2957 properties. */);
2958 /* We use `intern' here instead of Qread_only to avoid
2959 initialization-order problems. */
2960 Vminibuffer_prompt_properties
2961 = Fcons (intern ("read-only"), Fcons (Qt, Qnil));
2963 DEFVAR_LISP ("read-expression-map", &Vread_expression_map,
2964 doc: /* Minibuffer keymap used for reading Lisp expressions. */);
2965 Vread_expression_map = Qnil;
2967 defsubr (&Sset_minibuffer_window);
2968 defsubr (&Sread_from_minibuffer);
2969 defsubr (&Seval_minibuffer);
2970 defsubr (&Sread_minibuffer);
2971 defsubr (&Sread_string);
2972 defsubr (&Sread_command);
2973 defsubr (&Sread_variable);
2974 defsubr (&Sinternal_complete_buffer);
2975 defsubr (&Sread_buffer);
2976 defsubr (&Sread_no_blanks_input);
2977 defsubr (&Sminibuffer_depth);
2978 defsubr (&Sminibuffer_prompt);
2980 defsubr (&Sminibufferp);
2981 defsubr (&Sminibuffer_prompt_end);
2982 defsubr (&Sminibuffer_contents);
2983 defsubr (&Sminibuffer_contents_no_properties);
2984 defsubr (&Sminibuffer_completion_contents);
2985 defsubr (&Sdelete_minibuffer_contents);
2987 defsubr (&Stry_completion);
2988 defsubr (&Sall_completions);
2989 defsubr (&Stest_completion);
2990 defsubr (&Sassoc_string);
2991 defsubr (&Scompleting_read);
2992 defsubr (&Sminibuffer_complete);
2993 defsubr (&Sminibuffer_complete_word);
2994 defsubr (&Sminibuffer_complete_and_exit);
2995 defsubr (&Sdisplay_completion_list);
2996 defsubr (&Sminibuffer_completion_help);
2998 defsubr (&Sself_insert_and_exit);
2999 defsubr (&Sexit_minibuffer);
3001 defsubr (&Sminibuffer_message);
3004 void
3005 keys_of_minibuf ()
3007 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
3008 "abort-recursive-edit");
3009 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
3010 "exit-minibuffer");
3011 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
3012 "exit-minibuffer");
3014 initial_define_key (Vminibuffer_local_ns_map, ' ',
3015 "exit-minibuffer");
3016 initial_define_key (Vminibuffer_local_ns_map, '\t',
3017 "exit-minibuffer");
3018 initial_define_key (Vminibuffer_local_ns_map, '?',
3019 "self-insert-and-exit");
3021 initial_define_key (Vminibuffer_local_completion_map, '\t',
3022 "minibuffer-complete");
3023 initial_define_key (Vminibuffer_local_completion_map, ' ',
3024 "minibuffer-complete-word");
3025 initial_define_key (Vminibuffer_local_completion_map, '?',
3026 "minibuffer-completion-help");
3028 Fdefine_key (Vminibuffer_local_filename_completion_map,
3029 build_string (" "), Qnil);
3031 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
3032 "minibuffer-complete-and-exit");
3033 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
3034 "minibuffer-complete-and-exit");
3036 Fdefine_key (Vminibuffer_local_must_match_filename_map,
3037 build_string (" "), Qnil);
3040 /* arch-tag: 8f69b601-fba3-484c-a6dd-ceaee54a7a73
3041 (do not change this comment) */