Merge from trunk and apply standard C changes.
[emacs.git] / src / minibuf.c
blob52bfffad0fc65e818ad0b4acff5dcbd5d63197ad
1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009, 2010 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 3 of the License, or
11 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <setjmp.h>
26 #include "lisp.h"
27 #include "commands.h"
28 #include "buffer.h"
29 #include "character.h"
30 #include "dispextern.h"
31 #include "keyboard.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "syntax.h"
35 #include "intervals.h"
36 #include "keymap.h"
37 #include "termhooks.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 /* The maximum length of a minibuffer history. */
59 Lisp_Object Qhistory_length, Vhistory_length;
61 /* No duplicates in history. */
63 int history_delete_duplicates;
65 /* Non-nil means add new input to history. */
67 Lisp_Object Vhistory_add_new_input;
69 /* Fread_minibuffer leaves the input here as a string. */
71 Lisp_Object last_minibuf_string;
73 /* Nonzero means let functions called when within a minibuffer
74 invoke recursive minibuffers (to read arguments, or whatever) */
76 int enable_recursive_minibuffers;
78 /* Nonzero means don't ignore text properties
79 in Fread_from_minibuffer. */
81 int minibuffer_allow_text_properties;
83 /* help-form is bound to this while in the minibuffer. */
85 Lisp_Object Vminibuffer_help_form;
87 /* Variable which is the history list to add minibuffer values to. */
89 Lisp_Object Vminibuffer_history_variable;
91 /* Current position in the history list (adjusted by M-n and M-p). */
93 Lisp_Object Vminibuffer_history_position;
95 /* Text properties that are added to minibuffer prompts.
96 These are in addition to the basic `field' property, and stickiness
97 properties. */
99 Lisp_Object Vminibuffer_prompt_properties;
101 Lisp_Object Qminibuffer_history, Qbuffer_name_history;
103 Lisp_Object Qread_file_name_internal;
105 /* Normal hooks for entry to and exit from minibuffer. */
107 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
108 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
110 /* Function to call to read a buffer name. */
111 Lisp_Object Vread_buffer_function;
113 /* Nonzero means completion ignores case. */
115 int completion_ignore_case;
116 Lisp_Object Qcompletion_ignore_case;
117 int read_buffer_completion_ignore_case;
119 /* List of regexps that should restrict possible completions. */
121 Lisp_Object Vcompletion_regexp_list;
123 /* Nonzero means raise the minibuffer frame when the minibuffer
124 is entered. */
126 int minibuffer_auto_raise;
128 /* Keymap for reading expressions. */
129 Lisp_Object Vread_expression_map;
131 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
132 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
133 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
134 Lisp_Object Vminibuffer_completing_file_name;
136 Lisp_Object Quser_variable_p;
138 Lisp_Object Qminibuffer_default;
140 Lisp_Object Qcurrent_input_method, Qactivate_input_method;
142 Lisp_Object Qcase_fold_search;
144 Lisp_Object Qread_expression_history;
146 extern Lisp_Object Voverriding_local_map;
148 extern Lisp_Object Qfield;
150 /* Put minibuf on currently selected frame's minibuffer.
151 We do this whenever the user starts a new minibuffer
152 or when a minibuffer exits. */
154 void
155 choose_minibuf_frame (void)
157 if (FRAMEP (selected_frame)
158 && FRAME_LIVE_P (XFRAME (selected_frame))
159 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
161 struct frame *sf = XFRAME (selected_frame);
162 Lisp_Object buffer;
164 /* I don't think that any frames may validly have a null minibuffer
165 window anymore. */
166 if (NILP (sf->minibuffer_window))
167 abort ();
169 /* Under X, we come here with minibuf_window being the
170 minibuffer window of the unused termcap window created in
171 init_window_once. That window doesn't have a buffer. */
172 buffer = XWINDOW (minibuf_window)->buffer;
173 if (BUFFERP (buffer))
174 Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
175 minibuf_window = sf->minibuffer_window;
178 /* Make sure no other frame has a minibuffer as its selected window,
179 because the text would not be displayed in it, and that would be
180 confusing. Only allow the selected frame to do this,
181 and that only if the minibuffer is active. */
183 Lisp_Object tail, frame;
185 FOR_EACH_FRAME (tail, frame)
186 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
187 && !(EQ (frame, selected_frame)
188 && minibuf_level > 0))
189 Fset_frame_selected_window (frame, Fframe_first_window (frame), Qnil);
193 Lisp_Object
194 choose_minibuf_frame_1 (Lisp_Object ignore)
196 choose_minibuf_frame ();
197 return Qnil;
200 DEFUN ("active-minibuffer-window", Factive_minibuffer_window,
201 Sactive_minibuffer_window, 0, 0, 0,
202 doc: /* Return the currently active minibuffer window, or nil if none. */)
205 return minibuf_level ? minibuf_window : Qnil;
208 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
209 Sset_minibuffer_window, 1, 1, 0,
210 doc: /* Specify which minibuffer window to use for the minibuffer.
211 This affects where the minibuffer is displayed if you put text in it
212 without invoking the usual minibuffer commands. */)
213 (Lisp_Object window)
215 CHECK_WINDOW (window);
216 if (! MINI_WINDOW_P (XWINDOW (window)))
217 error ("Window is not a minibuffer window");
219 minibuf_window = window;
221 return window;
225 /* Actual minibuffer invocation. */
227 static Lisp_Object read_minibuf_unwind (Lisp_Object);
228 static Lisp_Object run_exit_minibuf_hook (Lisp_Object);
229 static Lisp_Object read_minibuf (Lisp_Object, Lisp_Object,
230 Lisp_Object, Lisp_Object,
231 int, Lisp_Object,
232 Lisp_Object, Lisp_Object,
233 int, int);
234 static Lisp_Object read_minibuf_noninteractive (Lisp_Object, Lisp_Object,
235 Lisp_Object, Lisp_Object,
236 int, Lisp_Object,
237 Lisp_Object, Lisp_Object,
238 int, int);
239 static Lisp_Object string_to_object (Lisp_Object, Lisp_Object);
242 /* Read a Lisp object from VAL and return it. If VAL is an empty
243 string, and DEFALT is a string, read from DEFALT instead of VAL. */
245 static Lisp_Object
246 string_to_object (Lisp_Object val, Lisp_Object defalt)
248 struct gcpro gcpro1, gcpro2;
249 Lisp_Object expr_and_pos;
250 int pos;
252 GCPRO2 (val, defalt);
254 if (STRINGP (val) && SCHARS (val) == 0)
256 if (STRINGP (defalt))
257 val = defalt;
258 else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
259 val = XCAR (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 (Lisp_Object map, Lisp_Object initial,
288 Lisp_Object prompt, Lisp_Object backup_n,
289 int expflag,
290 Lisp_Object histvar, Lisp_Object histpos,
291 Lisp_Object defalt,
292 int allow_props, int inherit_input_method)
294 int size, len;
295 char *line, *s;
296 Lisp_Object val;
298 fprintf (stdout, "%s", SDATA (prompt));
299 fflush (stdout);
301 val = Qnil;
302 size = 100;
303 len = 0;
304 line = (char *) xmalloc (size * sizeof *line);
305 while ((s = fgets (line + len, size - len, stdin)) != NULL
306 && (len = strlen (line),
307 len == size - 1 && line[len - 1] != '\n'))
309 size *= 2;
310 line = (char *) xrealloc (line, size);
313 if (s)
315 len = strlen (line);
317 if (len > 0 && line[len - 1] == '\n')
318 line[--len] = '\0';
320 val = build_string (line);
321 xfree (line);
323 else
325 xfree (line);
326 error ("Error reading from stdin");
329 /* If Lisp form desired instead of string, parse it. */
330 if (expflag)
331 val = string_to_object (val, CONSP (defalt) ? XCAR (defalt) : defalt);
333 return val;
336 DEFUN ("minibufferp", Fminibufferp,
337 Sminibufferp, 0, 1, 0,
338 doc: /* Return t if BUFFER is a minibuffer.
339 No argument or nil as argument means use current buffer as BUFFER.
340 BUFFER can be a buffer or a buffer name. */)
341 (Lisp_Object buffer)
343 Lisp_Object tem;
345 if (NILP (buffer))
346 buffer = Fcurrent_buffer ();
347 else if (STRINGP (buffer))
348 buffer = Fget_buffer (buffer);
349 else
350 CHECK_BUFFER (buffer);
352 tem = Fmemq (buffer, Vminibuffer_list);
353 return ! NILP (tem) ? Qt : Qnil;
356 DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
357 Sminibuffer_prompt_end, 0, 0, 0,
358 doc: /* Return the buffer position of the end of the minibuffer prompt.
359 Return (point-min) if current buffer is not a minibuffer. */)
360 (void)
362 /* This function is written to be most efficient when there's a prompt. */
363 Lisp_Object beg, end, tem;
364 beg = make_number (BEGV);
366 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
367 if (NILP (tem))
368 return beg;
370 end = Ffield_end (beg, Qnil, Qnil);
372 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
373 return beg;
374 else
375 return end;
378 DEFUN ("minibuffer-contents", Fminibuffer_contents,
379 Sminibuffer_contents, 0, 0, 0,
380 doc: /* Return the user input in a minibuffer as a string.
381 If the current buffer is not a minibuffer, return its entire contents. */)
382 (void)
384 int prompt_end = XINT (Fminibuffer_prompt_end ());
385 return make_buffer_string (prompt_end, ZV, 1);
388 DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
389 Sminibuffer_contents_no_properties, 0, 0, 0,
390 doc: /* Return the user input in a minibuffer as a string, without text-properties.
391 If the current buffer is not a minibuffer, return its entire contents. */)
392 (void)
394 int prompt_end = XINT (Fminibuffer_prompt_end ());
395 return make_buffer_string (prompt_end, ZV, 0);
398 DEFUN ("minibuffer-completion-contents", Fminibuffer_completion_contents,
399 Sminibuffer_completion_contents, 0, 0, 0,
400 doc: /* Return the user input in a minibuffer before point as a string.
401 That is what completion commands operate on.
402 If the current buffer is not a minibuffer, return its entire contents. */)
403 (void)
405 int prompt_end = XINT (Fminibuffer_prompt_end ());
406 if (PT < prompt_end)
407 error ("Cannot do completion in the prompt");
408 return make_buffer_string (prompt_end, PT, 1);
412 /* Read from the minibuffer using keymap MAP and initial contents INITIAL,
413 putting point minus BACKUP_N bytes from the end of INITIAL,
414 prompting with PROMPT (a string), using history list HISTVAR
415 with initial position HISTPOS. INITIAL should be a string or a
416 cons of a string and an integer. BACKUP_N should be <= 0, or
417 Qnil, which is equivalent to 0. If INITIAL is a cons, BACKUP_N is
418 ignored and replaced with an integer that puts point at one-indexed
419 position N in INITIAL, where N is the CDR of INITIAL, or at the
420 beginning of INITIAL if N <= 0.
422 Normally return the result as a string (the text that was read),
423 but if EXPFLAG is nonzero, read it and return the object read.
424 If HISTVAR is given, save the value read on that history only if it doesn't
425 match the front of that history list exactly. The value is pushed onto
426 the list as the string that was read.
428 DEFALT specifies the default value for the sake of history commands.
430 If ALLOW_PROPS is nonzero, we do not throw away text properties.
432 if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
433 current input method. */
435 static Lisp_Object
436 read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
437 Lisp_Object backup_n, int expflag,
438 Lisp_Object histvar, Lisp_Object histpos, Lisp_Object defalt,
439 int allow_props, int inherit_input_method)
441 Lisp_Object val;
442 int count = SPECPDL_INDEX ();
443 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
444 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
445 Lisp_Object enable_multibyte;
446 int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
447 /* String to add to the history. */
448 Lisp_Object histstring;
450 Lisp_Object empty_minibuf;
451 Lisp_Object dummy, frame;
453 extern Lisp_Object Qfront_sticky;
454 extern Lisp_Object Qrear_nonsticky;
456 specbind (Qminibuffer_default, defalt);
458 /* If Vminibuffer_completing_file_name is `lambda' on entry, it was t
459 in previous recursive minibuffer, but was not set explicitly
460 to t for this invocation, so set it to nil in this minibuffer.
461 Save the old value now, before we change it. */
462 specbind (intern ("minibuffer-completing-file-name"), Vminibuffer_completing_file_name);
463 if (EQ (Vminibuffer_completing_file_name, Qlambda))
464 Vminibuffer_completing_file_name = Qnil;
466 #ifdef HAVE_WINDOW_SYSTEM
467 if (display_hourglass_p)
468 cancel_hourglass ();
469 #endif
471 if (!NILP (initial))
473 if (CONSP (initial))
475 backup_n = Fcdr (initial);
476 initial = Fcar (initial);
477 CHECK_STRING (initial);
478 if (!NILP (backup_n))
480 CHECK_NUMBER (backup_n);
481 /* Convert to distance from end of input. */
482 if (XINT (backup_n) < 1)
483 /* A number too small means the beginning of the string. */
484 pos = - SCHARS (initial);
485 else
486 pos = XINT (backup_n) - 1 - SCHARS (initial);
489 else
490 CHECK_STRING (initial);
492 val = Qnil;
493 ambient_dir = current_buffer->directory;
494 input_method = Qnil;
495 enable_multibyte = Qnil;
497 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
498 store them away before we can GC. Don't need to protect
499 BACKUP_N because we use the value only if it is an integer. */
500 GCPRO5 (map, initial, val, ambient_dir, input_method);
502 if (!STRINGP (prompt))
503 prompt = empty_unibyte_string;
505 if (!enable_recursive_minibuffers
506 && minibuf_level > 0)
508 if (EQ (selected_window, minibuf_window))
509 error ("Command attempted to use minibuffer while in minibuffer");
510 else
511 /* If we're in another window, cancel the minibuffer that's active. */
512 Fthrow (Qexit,
513 build_string ("Command attempted to use minibuffer while in minibuffer"));
516 if ((noninteractive
517 /* In case we are running as a daemon, only do this before
518 detaching from the terminal. */
519 || (IS_DAEMON && (daemon_pipe[1] >= 0)))
520 && NILP (Vexecuting_kbd_macro))
522 val = read_minibuf_noninteractive (map, initial, prompt,
523 make_number (pos),
524 expflag, histvar, histpos, defalt,
525 allow_props, inherit_input_method);
526 UNGCPRO;
527 return unbind_to (count, val);
530 /* Choose the minibuffer window and frame, and take action on them. */
532 choose_minibuf_frame ();
534 record_unwind_protect (choose_minibuf_frame_1, Qnil);
536 record_unwind_protect (Fset_window_configuration,
537 Fcurrent_window_configuration (Qnil));
539 /* If the minibuffer window is on a different frame, save that
540 frame's configuration too. */
541 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
542 if (!EQ (mini_frame, selected_frame))
543 record_unwind_protect (Fset_window_configuration,
544 Fcurrent_window_configuration (mini_frame));
546 /* If the minibuffer is on an iconified or invisible frame,
547 make it visible now. */
548 Fmake_frame_visible (mini_frame);
550 if (minibuffer_auto_raise)
551 Fraise_frame (mini_frame);
553 temporarily_switch_to_single_kboard (XFRAME (mini_frame));
555 /* We have to do this after saving the window configuration
556 since that is what restores the current buffer. */
558 /* Arrange to restore a number of minibuffer-related variables.
559 We could bind each variable separately, but that would use lots of
560 specpdl slots. */
561 minibuf_save_list
562 = Fcons (Voverriding_local_map,
563 Fcons (minibuf_window,
564 minibuf_save_list));
565 minibuf_save_list
566 = Fcons (minibuf_prompt,
567 Fcons (make_number (minibuf_prompt_width),
568 Fcons (Vhelp_form,
569 Fcons (Vcurrent_prefix_arg,
570 Fcons (Vminibuffer_history_position,
571 Fcons (Vminibuffer_history_variable,
572 minibuf_save_list))))));
574 record_unwind_protect (read_minibuf_unwind, Qnil);
575 minibuf_level++;
576 /* We are exiting the minibuffer one way or the other, so run the hook.
577 It should be run before unwinding the minibuf settings. Do it
578 separately from read_minibuf_unwind because we need to make sure that
579 read_minibuf_unwind is fully executed even if exit-minibuffer-hook
580 signals an error. --Stef */
581 record_unwind_protect (run_exit_minibuf_hook, Qnil);
583 /* Now that we can restore all those variables, start changing them. */
585 minibuf_prompt_width = 0;
586 minibuf_prompt = Fcopy_sequence (prompt);
587 Vminibuffer_history_position = histpos;
588 Vminibuffer_history_variable = histvar;
589 Vhelp_form = Vminibuffer_help_form;
590 /* If this minibuffer is reading a file name, that doesn't mean
591 recursive ones are. But we cannot set it to nil, because
592 completion code still need to know the minibuffer is completing a
593 file name. So use `lambda' as intermediate value meaning
594 "t" in this minibuffer, but "nil" in next minibuffer. */
595 if (!NILP (Vminibuffer_completing_file_name))
596 Vminibuffer_completing_file_name = Qlambda;
598 if (inherit_input_method)
600 /* `current-input-method' is buffer local. So, remember it in
601 INPUT_METHOD before changing the current buffer. */
602 input_method = Fsymbol_value (Qcurrent_input_method);
603 enable_multibyte = current_buffer->enable_multibyte_characters;
606 /* Switch to the minibuffer. */
608 minibuffer = get_minibuffer (minibuf_level);
609 Fset_buffer (minibuffer);
611 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
612 if (inherit_input_method)
613 current_buffer->enable_multibyte_characters = enable_multibyte;
615 /* The current buffer's default directory is usually the right thing
616 for our minibuffer here. However, if you're typing a command at
617 a minibuffer-only frame when minibuf_level is zero, then buf IS
618 the current_buffer, so reset_buffer leaves buf's default
619 directory unchanged. This is a bummer when you've just started
620 up Emacs and buf's default directory is Qnil. Here's a hack; can
621 you think of something better to do? Find another buffer with a
622 better directory, and use that one instead. */
623 if (STRINGP (ambient_dir))
624 current_buffer->directory = ambient_dir;
625 else
627 Lisp_Object buf_list;
629 for (buf_list = Vbuffer_alist;
630 CONSP (buf_list);
631 buf_list = XCDR (buf_list))
633 Lisp_Object other_buf;
635 other_buf = XCDR (XCAR (buf_list));
636 if (STRINGP (XBUFFER (other_buf)->directory))
638 current_buffer->directory = XBUFFER (other_buf)->directory;
639 break;
644 if (!EQ (mini_frame, selected_frame))
645 Fredirect_frame_focus (selected_frame, mini_frame);
647 Vminibuf_scroll_window = selected_window;
648 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
649 minibuf_selected_window = selected_window;
651 /* Empty out the minibuffers of all frames other than the one
652 where we are going to display one now.
653 Set them to point to ` *Minibuf-0*', which is always empty. */
654 empty_minibuf = Fget_buffer (build_string (" *Minibuf-0*"));
656 FOR_EACH_FRAME (dummy, frame)
658 Lisp_Object root_window = Fframe_root_window (frame);
659 Lisp_Object mini_window = XWINDOW (root_window)->next;
661 if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
662 && !NILP (Fwindow_minibuffer_p (mini_window)))
663 Fset_window_buffer (mini_window, empty_minibuf, Qnil);
666 /* Display this minibuffer in the proper window. */
667 Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
668 Fselect_window (minibuf_window, Qnil);
669 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
671 Fmake_local_variable (Qprint_escape_newlines);
672 print_escape_newlines = 1;
674 /* Erase the buffer. */
676 int count1 = SPECPDL_INDEX ();
677 specbind (Qinhibit_read_only, Qt);
678 specbind (Qinhibit_modification_hooks, Qt);
679 Ferase_buffer ();
681 if (!NILP (current_buffer->enable_multibyte_characters)
682 && ! STRING_MULTIBYTE (minibuf_prompt))
683 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
685 /* Insert the prompt, record where it ends. */
686 Finsert (1, &minibuf_prompt);
687 if (PT > BEG)
689 Fput_text_property (make_number (BEG), make_number (PT),
690 Qfront_sticky, Qt, Qnil);
691 Fput_text_property (make_number (BEG), make_number (PT),
692 Qrear_nonsticky, Qt, Qnil);
693 Fput_text_property (make_number (BEG), make_number (PT),
694 Qfield, Qt, Qnil);
695 Fadd_text_properties (make_number (BEG), make_number (PT),
696 Vminibuffer_prompt_properties, Qnil);
698 unbind_to (count1, Qnil);
701 minibuf_prompt_width = (int) current_column (); /* iftc */
703 /* Put in the initial input. */
704 if (!NILP (initial))
706 Finsert (1, &initial);
707 Fforward_char (make_number (pos));
710 clear_message (1, 1);
711 current_buffer->keymap = map;
713 /* Turn on an input method stored in INPUT_METHOD if any. */
714 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
715 call1 (Qactivate_input_method, input_method);
717 /* Run our hook, but not if it is empty.
718 (run-hooks would do nothing if it is empty,
719 but it's important to save time here in the usual case.) */
720 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
721 && !NILP (Vrun_hooks))
722 call1 (Vrun_hooks, Qminibuffer_setup_hook);
724 /* Don't allow the user to undo past this point. */
725 current_buffer->undo_list = Qnil;
727 recursive_edit_1 ();
729 /* If cursor is on the minibuffer line,
730 show the user we have exited by putting it in column 0. */
731 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
732 && !noninteractive)
734 XWINDOW (minibuf_window)->cursor.hpos = 0;
735 XWINDOW (minibuf_window)->cursor.x = 0;
736 XWINDOW (minibuf_window)->must_be_updated_p = 1;
737 update_frame (XFRAME (selected_frame), 1, 1);
739 struct frame *f = XFRAME (XWINDOW (minibuf_window)->frame);
740 struct redisplay_interface *rif = FRAME_RIF (f);
741 if (rif && rif->flush_display)
742 rif->flush_display (f);
746 /* Make minibuffer contents into a string. */
747 Fset_buffer (minibuffer);
748 if (allow_props)
749 val = Fminibuffer_contents ();
750 else
751 val = Fminibuffer_contents_no_properties ();
753 /* VAL is the string of minibuffer text. */
755 last_minibuf_string = val;
757 /* Choose the string to add to the history. */
758 if (SCHARS (val) != 0)
759 histstring = val;
760 else if (STRINGP (defalt))
761 histstring = defalt;
762 else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
763 histstring = XCAR (defalt);
764 else
765 histstring = Qnil;
767 /* Add the value to the appropriate history list, if any. */
768 if (!NILP (Vhistory_add_new_input)
769 && SYMBOLP (Vminibuffer_history_variable)
770 && !NILP (histstring))
772 /* If the caller wanted to save the value read on a history list,
773 then do so if the value is not already the front of the list. */
774 Lisp_Object histval;
776 /* If variable is unbound, make it nil. */
778 histval = find_symbol_value (Vminibuffer_history_variable);
779 if (EQ (histval, Qunbound))
780 Fset (Vminibuffer_history_variable, Qnil);
782 /* The value of the history variable must be a cons or nil. Other
783 values are unacceptable. We silently ignore these values. */
785 if (NILP (histval)
786 || (CONSP (histval)
787 /* Don't duplicate the most recent entry in the history. */
788 && (NILP (Fequal (histstring, Fcar (histval))))))
790 Lisp_Object length;
792 if (history_delete_duplicates) Fdelete (histstring, histval);
793 histval = Fcons (histstring, histval);
794 Fset (Vminibuffer_history_variable, histval);
796 /* Truncate if requested. */
797 length = Fget (Vminibuffer_history_variable, Qhistory_length);
798 if (NILP (length)) length = Vhistory_length;
799 if (INTEGERP (length))
801 if (XINT (length) <= 0)
802 Fset (Vminibuffer_history_variable, Qnil);
803 else
805 Lisp_Object temp;
807 temp = Fnthcdr (Fsub1 (length), histval);
808 if (CONSP (temp)) Fsetcdr (temp, Qnil);
814 /* If Lisp form desired instead of string, parse it. */
815 if (expflag)
816 val = string_to_object (val, defalt);
818 /* The appropriate frame will get selected
819 in set-window-configuration. */
820 UNGCPRO;
821 return unbind_to (count, val);
824 /* Return a buffer to be used as the minibuffer at depth `depth'.
825 depth = 0 is the lowest allowed argument, and that is the value
826 used for nonrecursive minibuffer invocations */
828 Lisp_Object
829 get_minibuffer (int depth)
831 Lisp_Object tail, num, buf;
832 char name[24];
833 extern Lisp_Object nconc2 (Lisp_Object, Lisp_Object);
835 XSETFASTINT (num, depth);
836 tail = Fnthcdr (num, Vminibuffer_list);
837 if (NILP (tail))
839 tail = Fcons (Qnil, Qnil);
840 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
842 buf = Fcar (tail);
843 if (NILP (buf) || NILP (XBUFFER (buf)->name))
845 sprintf (name, " *Minibuf-%d*", depth);
846 buf = Fget_buffer_create (build_string (name));
848 /* Although the buffer's name starts with a space, undo should be
849 enabled in it. */
850 Fbuffer_enable_undo (buf);
852 XSETCAR (tail, buf);
854 else
856 int count = SPECPDL_INDEX ();
857 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
858 have to empty the list, otherwise we end up with overlays that
859 think they belong to this buffer while the buffer doesn't know about
860 them any more. */
861 delete_all_overlays (XBUFFER (buf));
862 reset_buffer (XBUFFER (buf));
863 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
864 Fset_buffer (buf);
865 Fkill_all_local_variables ();
866 unbind_to (count, Qnil);
869 return buf;
872 static Lisp_Object
873 run_exit_minibuf_hook (Lisp_Object data)
875 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
876 && !NILP (Vrun_hooks))
877 safe_run_hooks (Qminibuffer_exit_hook);
879 return Qnil;
882 /* This function is called on exiting minibuffer, whether normally or
883 not, and it restores the current window, buffer, etc. */
885 static Lisp_Object
886 read_minibuf_unwind (Lisp_Object data)
888 Lisp_Object old_deactivate_mark;
889 Lisp_Object window;
891 /* If this was a recursive minibuffer,
892 tie the minibuffer window back to the outer level minibuffer buffer. */
893 minibuf_level--;
895 window = minibuf_window;
896 /* To keep things predictable, in case it matters, let's be in the
897 minibuffer when we reset the relevant variables. */
898 Fset_buffer (XWINDOW (window)->buffer);
900 /* Restore prompt, etc, from outer minibuffer level. */
901 minibuf_prompt = Fcar (minibuf_save_list);
902 minibuf_save_list = Fcdr (minibuf_save_list);
903 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
904 minibuf_save_list = Fcdr (minibuf_save_list);
905 Vhelp_form = Fcar (minibuf_save_list);
906 minibuf_save_list = Fcdr (minibuf_save_list);
907 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
908 minibuf_save_list = Fcdr (minibuf_save_list);
909 Vminibuffer_history_position = Fcar (minibuf_save_list);
910 minibuf_save_list = Fcdr (minibuf_save_list);
911 Vminibuffer_history_variable = Fcar (minibuf_save_list);
912 minibuf_save_list = Fcdr (minibuf_save_list);
913 Voverriding_local_map = Fcar (minibuf_save_list);
914 minibuf_save_list = Fcdr (minibuf_save_list);
915 #if 0
916 temp = Fcar (minibuf_save_list);
917 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
918 minibuf_window = temp;
919 #endif
920 minibuf_save_list = Fcdr (minibuf_save_list);
922 /* Erase the minibuffer we were using at this level. */
924 int count = SPECPDL_INDEX ();
925 /* Prevent error in erase-buffer. */
926 specbind (Qinhibit_read_only, Qt);
927 specbind (Qinhibit_modification_hooks, Qt);
928 old_deactivate_mark = Vdeactivate_mark;
929 Ferase_buffer ();
930 Vdeactivate_mark = old_deactivate_mark;
931 unbind_to (count, Qnil);
934 /* When we get to the outmost level, make sure we resize the
935 mini-window back to its normal size. */
936 if (minibuf_level == 0)
937 resize_mini_window (XWINDOW (window), 0);
939 /* Make sure minibuffer window is erased, not ignored. */
940 windows_or_buffers_changed++;
941 XSETFASTINT (XWINDOW (window)->last_modified, 0);
942 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
943 return Qnil;
947 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
948 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
949 The optional second arg INITIAL-CONTENTS is an obsolete alternative to
950 DEFAULT-VALUE. It normally should be nil in new code, except when
951 HIST is a cons. It is discussed in more detail below.
952 Third arg KEYMAP is a keymap to use whilst reading;
953 if omitted or nil, the default is `minibuffer-local-map'.
954 If fourth arg READ is non-nil, then interpret the result as a Lisp object
955 and return that object:
956 in other words, do `(car (read-from-string INPUT-STRING))'
957 Fifth arg HIST, if non-nil, specifies a history list and optionally
958 the initial position in the list. It can be a symbol, which is the
959 history list variable to use, or it can be a cons cell
960 (HISTVAR . HISTPOS). In that case, HISTVAR is the history list variable
961 to use, and HISTPOS is the initial position for use by the minibuffer
962 history commands. For consistency, you should also specify that
963 element of the history as the value of INITIAL-CONTENTS. Positions
964 are counted starting from 1 at the beginning of the list.
965 Sixth arg DEFAULT-VALUE is the default value or the list of default values.
966 If non-nil, it is available for history commands, and as the value
967 (or the first element of the list of default values) to return
968 if the user enters the empty string. But, unless READ is non-nil,
969 `read-from-minibuffer' does NOT return DEFAULT-VALUE if the user enters
970 empty input! It returns the empty string.
971 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
972 the current input method and the setting of `enable-multibyte-characters'.
973 If the variable `minibuffer-allow-text-properties' is non-nil,
974 then the string which is returned includes whatever text properties
975 were present in the minibuffer. Otherwise the value has no text properties.
977 The remainder of this documentation string describes the
978 INITIAL-CONTENTS argument in more detail. It is only relevant when
979 studying existing code, or when HIST is a cons. If non-nil,
980 INITIAL-CONTENTS is a string to be inserted into the minibuffer before
981 reading input. Normally, point is put at the end of that string.
982 However, if INITIAL-CONTENTS is \(STRING . POSITION), the initial
983 input is STRING, but point is placed at _one-indexed_ position
984 POSITION in the minibuffer. Any integer value less than or equal to
985 one puts point at the beginning of the string. *Note* that this
986 behavior differs from the way such arguments are used in `completing-read'
987 and some related functions, which use zero-indexing for POSITION. */)
988 (Lisp_Object prompt, Lisp_Object initial_contents, Lisp_Object keymap, Lisp_Object read, Lisp_Object hist, Lisp_Object default_value, Lisp_Object inherit_input_method)
990 Lisp_Object histvar, histpos, val;
991 struct gcpro gcpro1;
993 CHECK_STRING (prompt);
994 if (NILP (keymap))
995 keymap = Vminibuffer_local_map;
996 else
997 keymap = get_keymap (keymap, 1, 0);
999 if (SYMBOLP (hist))
1001 histvar = hist;
1002 histpos = Qnil;
1004 else
1006 histvar = Fcar_safe (hist);
1007 histpos = Fcdr_safe (hist);
1009 if (NILP (histvar))
1010 histvar = Qminibuffer_history;
1011 if (NILP (histpos))
1012 XSETFASTINT (histpos, 0);
1014 GCPRO1 (default_value);
1015 val = read_minibuf (keymap, initial_contents, prompt,
1016 Qnil, !NILP (read),
1017 histvar, histpos, default_value,
1018 minibuffer_allow_text_properties,
1019 !NILP (inherit_input_method));
1020 UNGCPRO;
1021 return val;
1024 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
1025 doc: /* Return a Lisp object read using the minibuffer, unevaluated.
1026 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
1027 is a string to insert in the minibuffer before reading.
1028 \(INITIAL-CONTENTS can also be a cons of a string and an integer.
1029 Such arguments are used as in `read-from-minibuffer'.) */)
1030 (Lisp_Object prompt, Lisp_Object initial_contents)
1032 CHECK_STRING (prompt);
1033 return read_minibuf (Vminibuffer_local_map, initial_contents,
1034 prompt, Qnil, 1, Qminibuffer_history,
1035 make_number (0), Qnil, 0, 0);
1038 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
1039 doc: /* Return value of Lisp expression read using the minibuffer.
1040 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
1041 is a string to insert in the minibuffer before reading.
1042 \(INITIAL-CONTENTS can also be a cons of a string and an integer.
1043 Such arguments are used as in `read-from-minibuffer'.) */)
1044 (Lisp_Object prompt, Lisp_Object initial_contents)
1046 return Feval (read_minibuf (Vread_expression_map, initial_contents,
1047 prompt, Qnil, 1, Qread_expression_history,
1048 make_number (0), Qnil, 0, 0));
1051 /* Functions that use the minibuffer to read various things. */
1053 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
1054 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
1055 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
1056 This argument has been superseded by DEFAULT-VALUE and should normally
1057 be nil in new code. It behaves as in `read-from-minibuffer'. See the
1058 documentation string of that function for details.
1059 The third arg HISTORY, if non-nil, specifies a history list
1060 and optionally the initial position in the list.
1061 See `read-from-minibuffer' for details of HISTORY argument.
1062 Fourth arg DEFAULT-VALUE is the default value or the list of default values.
1063 If non-nil, it is used for history commands, and as the value (or the first
1064 element of the list of default values) to return if the user enters the
1065 empty string.
1066 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1067 the current input method and the setting of `enable-multibyte-characters'. */)
1068 (Lisp_Object prompt, Lisp_Object initial_input, Lisp_Object history, Lisp_Object default_value, Lisp_Object inherit_input_method)
1070 Lisp_Object val;
1071 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
1072 Qnil, history, default_value,
1073 inherit_input_method);
1074 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
1075 val = CONSP (default_value) ? XCAR (default_value) : default_value;
1076 return val;
1079 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
1080 doc: /* Read a string from the terminal, not allowing blanks.
1081 Prompt with PROMPT. Whitespace terminates the input. If INITIAL is
1082 non-nil, it should be a string, which is used as initial input, with
1083 point positioned at the end, so that SPACE will accept the input.
1084 \(Actually, INITIAL can also be a cons of a string and an integer.
1085 Such values are treated as in `read-from-minibuffer', but are normally
1086 not useful in this function.)
1087 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1088 the current input method and the setting of`enable-multibyte-characters'. */)
1089 (Lisp_Object prompt, Lisp_Object initial, Lisp_Object inherit_input_method)
1091 CHECK_STRING (prompt);
1092 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
1093 0, Qminibuffer_history, make_number (0), Qnil, 0,
1094 !NILP (inherit_input_method));
1097 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
1098 doc: /* Read the name of a command and return as a symbol.
1099 Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1100 if it is a list. */)
1101 (Lisp_Object prompt, Lisp_Object default_value)
1103 Lisp_Object name, default_string;
1105 if (NILP (default_value))
1106 default_string = Qnil;
1107 else if (SYMBOLP (default_value))
1108 default_string = SYMBOL_NAME (default_value);
1109 else
1110 default_string = default_value;
1112 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1113 Qnil, Qnil, default_string, Qnil);
1114 if (NILP (name))
1115 return name;
1116 return Fintern (name, Qnil);
1119 #ifdef NOTDEF
1120 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
1121 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
1122 Prompt with PROMPT. */)
1123 (Lisp_Object prompt)
1125 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
1126 Qnil);
1128 #endif /* NOTDEF */
1130 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
1131 doc: /* Read the name of a user variable and return it as a symbol.
1132 Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1133 if it is a list.
1134 A user variable is one for which `user-variable-p' returns non-nil. */)
1135 (Lisp_Object prompt, Lisp_Object default_value)
1137 Lisp_Object name, default_string;
1139 if (NILP (default_value))
1140 default_string = Qnil;
1141 else if (SYMBOLP (default_value))
1142 default_string = SYMBOL_NAME (default_value);
1143 else
1144 default_string = default_value;
1146 name = Fcompleting_read (prompt, Vobarray,
1147 Quser_variable_p, Qt,
1148 Qnil, Qnil, default_string, Qnil);
1149 if (NILP (name))
1150 return name;
1151 return Fintern (name, Qnil);
1154 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
1155 doc: /* Read the name of a buffer and return as a string.
1156 Prompt with PROMPT.
1157 Optional second arg DEF is value to return if user enters an empty line.
1158 If DEF is a list of default values, return its first element.
1159 Optional third arg REQUIRE-MATCH determines whether non-existing
1160 buffer names are allowed. It has the same meaning as the
1161 REQUIRE-MATCH argument of `completing-read'.
1162 The argument PROMPT should be a string ending with a colon and a space.
1163 If `read-buffer-completion-ignore-case' is non-nil, completion ignores
1164 case while reading the buffer name.
1165 If `read-buffer-function' is non-nil, this works by calling it as a
1166 function, instead of the usual behavior. */)
1167 (Lisp_Object prompt, Lisp_Object def, Lisp_Object require_match)
1169 Lisp_Object args[4], result;
1170 unsigned char *s;
1171 int len;
1172 int count = SPECPDL_INDEX ();
1174 if (BUFFERP (def))
1175 def = XBUFFER (def)->name;
1177 specbind (Qcompletion_ignore_case,
1178 read_buffer_completion_ignore_case ? Qt : Qnil);
1180 if (NILP (Vread_buffer_function))
1182 if (!NILP (def))
1184 /* A default value was provided: we must change PROMPT,
1185 editing the default value in before the colon. To achieve
1186 this, we replace PROMPT with a substring that doesn't
1187 contain the terminal space and colon (if present). They
1188 are then added back using Fformat. */
1190 if (STRINGP (prompt))
1192 s = SDATA (prompt);
1193 len = strlen (s);
1194 if (len >= 2 && s[len - 2] == ':' && s[len - 1] == ' ')
1195 len = len - 2;
1196 else if (len >= 1 && (s[len - 1] == ':' || s[len - 1] == ' '))
1197 len--;
1199 prompt = make_specified_string (s, -1, len,
1200 STRING_MULTIBYTE (prompt));
1203 args[0] = build_string ("%s (default %s): ");
1204 args[1] = prompt;
1205 args[2] = CONSP (def) ? XCAR (def) : def;
1206 prompt = Fformat (3, args);
1209 result = Fcompleting_read (prompt, intern ("internal-complete-buffer"),
1210 Qnil, require_match, Qnil, Qbuffer_name_history,
1211 def, Qnil);
1213 else
1215 args[0] = Vread_buffer_function;
1216 args[1] = prompt;
1217 args[2] = def;
1218 args[3] = require_match;
1219 result = Ffuncall(4, args);
1221 return unbind_to (count, result);
1224 static Lisp_Object
1225 minibuf_conform_representation (Lisp_Object string, Lisp_Object basis)
1227 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1228 return string;
1230 if (STRING_MULTIBYTE (string))
1231 return Fstring_make_unibyte (string);
1232 else
1233 return Fstring_make_multibyte (string);
1236 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1237 doc: /* Return common substring of all completions of STRING in COLLECTION.
1238 Test each possible completion specified by COLLECTION
1239 to see if it begins with STRING. The possible completions may be
1240 strings or symbols. Symbols are converted to strings before testing,
1241 see `symbol-name'.
1242 All that match STRING are compared together; the longest initial sequence
1243 common to all these matches is the return value.
1244 If there is no match at all, the return value is nil.
1245 For a unique match which is exact, the return value is t.
1247 If COLLECTION is an alist, the keys (cars of elements) are the
1248 possible completions. If an element is not a cons cell, then the
1249 element itself is the possible completion.
1250 If COLLECTION is a hash-table, all the keys that are strings or symbols
1251 are the possible completions.
1252 If COLLECTION is an obarray, the names of all symbols in the obarray
1253 are the possible completions.
1255 COLLECTION can also be a function to do the completion itself.
1256 It receives three arguments: the values STRING, PREDICATE and nil.
1257 Whatever it returns becomes the value of `try-completion'.
1259 If optional third argument PREDICATE is non-nil,
1260 it is used to test each possible match.
1261 The match is a candidate only if PREDICATE returns non-nil.
1262 The argument given to PREDICATE is the alist element
1263 or the symbol from the obarray. If COLLECTION is a hash-table,
1264 predicate is called with two arguments: the key and the value.
1265 Additionally to this predicate, `completion-regexp-list'
1266 is used to further constrain the set of candidates. */)
1267 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
1269 Lisp_Object bestmatch, tail, elt, eltstring;
1270 /* Size in bytes of BESTMATCH. */
1271 int bestmatchsize = 0;
1272 /* These are in bytes, too. */
1273 int compare, matchsize;
1274 enum { function_table, list_table, obarray_table, hash_table}
1275 type = (HASH_TABLE_P (collection) ? hash_table
1276 : VECTORP (collection) ? obarray_table
1277 : ((NILP (collection)
1278 || (CONSP (collection)
1279 && (!SYMBOLP (XCAR (collection))
1280 || NILP (XCAR (collection)))))
1281 ? list_table : function_table));
1282 int index = 0, obsize = 0;
1283 int matchcount = 0;
1284 int bindcount = -1;
1285 Lisp_Object bucket, zero, end, tem;
1286 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1288 CHECK_STRING (string);
1289 if (type == function_table)
1290 return call3 (collection, string, predicate, Qnil);
1292 bestmatch = bucket = Qnil;
1293 zero = make_number (0);
1295 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1296 tail = collection;
1297 if (type == obarray_table)
1299 collection = check_obarray (collection);
1300 obsize = XVECTOR (collection)->size;
1301 bucket = XVECTOR (collection)->contents[index];
1304 while (1)
1306 /* Get the next element of the alist, obarray, or hash-table. */
1307 /* Exit the loop if the elements are all used up. */
1308 /* elt gets the alist element or symbol.
1309 eltstring gets the name to check as a completion. */
1311 if (type == list_table)
1313 if (!CONSP (tail))
1314 break;
1315 elt = XCAR (tail);
1316 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1317 tail = XCDR (tail);
1319 else if (type == obarray_table)
1321 if (!EQ (bucket, zero))
1323 if (!SYMBOLP (bucket))
1324 error ("Bad data in guts of obarray");
1325 elt = bucket;
1326 eltstring = elt;
1327 if (XSYMBOL (bucket)->next)
1328 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1329 else
1330 XSETFASTINT (bucket, 0);
1332 else if (++index >= obsize)
1333 break;
1334 else
1336 bucket = XVECTOR (collection)->contents[index];
1337 continue;
1340 else /* if (type == hash_table) */
1342 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1343 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
1344 index++;
1345 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
1346 break;
1347 else
1348 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
1351 /* Is this element a possible completion? */
1353 if (SYMBOLP (eltstring))
1354 eltstring = Fsymbol_name (eltstring);
1356 if (STRINGP (eltstring)
1357 && SCHARS (string) <= SCHARS (eltstring)
1358 && (tem = Fcompare_strings (eltstring, zero,
1359 make_number (SCHARS (string)),
1360 string, zero, Qnil,
1361 completion_ignore_case ? Qt : Qnil),
1362 EQ (Qt, tem)))
1364 /* Yes. */
1365 Lisp_Object regexps;
1367 /* Ignore this element if it fails to match all the regexps. */
1369 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1370 regexps = XCDR (regexps))
1372 if (bindcount < 0) {
1373 bindcount = SPECPDL_INDEX ();
1374 specbind (Qcase_fold_search,
1375 completion_ignore_case ? Qt : Qnil);
1377 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1378 if (NILP (tem))
1379 break;
1381 if (CONSP (regexps))
1382 continue;
1385 /* Ignore this element if there is a predicate
1386 and the predicate doesn't like it. */
1388 if (!NILP (predicate))
1390 if (EQ (predicate, Qcommandp))
1391 tem = Fcommandp (elt, Qnil);
1392 else
1394 if (bindcount >= 0)
1396 unbind_to (bindcount, Qnil);
1397 bindcount = -1;
1399 GCPRO4 (tail, string, eltstring, bestmatch);
1400 tem = (type == hash_table
1401 ? call2 (predicate, elt,
1402 HASH_VALUE (XHASH_TABLE (collection),
1403 index - 1))
1404 : call1 (predicate, elt));
1405 UNGCPRO;
1407 if (NILP (tem)) continue;
1410 /* Update computation of how much all possible completions match */
1412 if (NILP (bestmatch))
1414 matchcount = 1;
1415 bestmatch = eltstring;
1416 bestmatchsize = SCHARS (eltstring);
1418 else
1420 compare = min (bestmatchsize, SCHARS (eltstring));
1421 tem = Fcompare_strings (bestmatch, zero,
1422 make_number (compare),
1423 eltstring, zero,
1424 make_number (compare),
1425 completion_ignore_case ? Qt : Qnil);
1426 if (EQ (tem, Qt))
1427 matchsize = compare;
1428 else if (XINT (tem) < 0)
1429 matchsize = - XINT (tem) - 1;
1430 else
1431 matchsize = XINT (tem) - 1;
1433 if (completion_ignore_case)
1435 /* If this is an exact match except for case,
1436 use it as the best match rather than one that is not an
1437 exact match. This way, we get the case pattern
1438 of the actual match. */
1439 if ((matchsize == SCHARS (eltstring)
1440 && matchsize < SCHARS (bestmatch))
1442 /* If there is more than one exact match ignoring case,
1443 and one of them is exact including case,
1444 prefer that one. */
1445 /* If there is no exact match ignoring case,
1446 prefer a match that does not change the case
1447 of the input. */
1448 ((matchsize == SCHARS (eltstring))
1450 (matchsize == SCHARS (bestmatch))
1451 && (tem = Fcompare_strings (eltstring, zero,
1452 make_number (SCHARS (string)),
1453 string, zero,
1454 Qnil,
1455 Qnil),
1456 EQ (Qt, tem))
1457 && (tem = Fcompare_strings (bestmatch, zero,
1458 make_number (SCHARS (string)),
1459 string, zero,
1460 Qnil,
1461 Qnil),
1462 ! EQ (Qt, tem))))
1463 bestmatch = eltstring;
1465 if (bestmatchsize != SCHARS (eltstring)
1466 || bestmatchsize != matchsize)
1467 /* Don't count the same string multiple times. */
1468 matchcount++;
1469 bestmatchsize = matchsize;
1470 if (matchsize <= SCHARS (string)
1471 /* If completion-ignore-case is non-nil, don't
1472 short-circuit because we want to find the best
1473 possible match *including* case differences. */
1474 && !completion_ignore_case
1475 && matchcount > 1)
1476 /* No need to look any further. */
1477 break;
1482 if (bindcount >= 0) {
1483 unbind_to (bindcount, Qnil);
1484 bindcount = -1;
1487 if (NILP (bestmatch))
1488 return Qnil; /* No completions found */
1489 /* If we are ignoring case, and there is no exact match,
1490 and no additional text was supplied,
1491 don't change the case of what the user typed. */
1492 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1493 && SCHARS (bestmatch) > bestmatchsize)
1494 return minibuf_conform_representation (string, bestmatch);
1496 /* Return t if the supplied string is an exact match (counting case);
1497 it does not require any change to be made. */
1498 if (matchcount == 1 && !NILP (Fequal (bestmatch, string)))
1499 return Qt;
1501 XSETFASTINT (zero, 0); /* Else extract the part in which */
1502 XSETFASTINT (end, bestmatchsize); /* all completions agree */
1503 return Fsubstring (bestmatch, zero, end);
1506 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1507 doc: /* Search for partial matches to STRING in COLLECTION.
1508 Test each of the possible completions specified by COLLECTION
1509 to see if it begins with STRING. The possible completions may be
1510 strings or symbols. Symbols are converted to strings before testing,
1511 see `symbol-name'.
1512 The value is a list of all the possible completions that match STRING.
1514 If COLLECTION is an alist, the keys (cars of elements) are the
1515 possible completions. If an element is not a cons cell, then the
1516 element itself is the possible completion.
1517 If COLLECTION is a hash-table, all the keys that are strings or symbols
1518 are the possible completions.
1519 If COLLECTION is an obarray, the names of all symbols in the obarray
1520 are the possible completions.
1522 COLLECTION can also be a function to do the completion itself.
1523 It receives three arguments: the values STRING, PREDICATE and t.
1524 Whatever it returns becomes the value of `all-completions'.
1526 If optional third argument PREDICATE is non-nil,
1527 it is used to test each possible match.
1528 The match is a candidate only if PREDICATE returns non-nil.
1529 The argument given to PREDICATE is the alist element
1530 or the symbol from the obarray. If COLLECTION is a hash-table,
1531 predicate is called with two arguments: the key and the value.
1532 Additionally to this predicate, `completion-regexp-list'
1533 is used to further constrain the set of candidates.
1535 An obsolete optional fourth argument HIDE-SPACES is still accepted for
1536 backward compatibility. If non-nil, strings in COLLECTION that start
1537 with a space are ignored unless STRING itself starts with a space. */)
1538 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate, Lisp_Object hide_spaces)
1540 Lisp_Object tail, elt, eltstring;
1541 Lisp_Object allmatches;
1542 int type = HASH_TABLE_P (collection) ? 3
1543 : VECTORP (collection) ? 2
1544 : NILP (collection) || (CONSP (collection)
1545 && (!SYMBOLP (XCAR (collection))
1546 || NILP (XCAR (collection))));
1547 int index = 0, obsize = 0;
1548 int bindcount = -1;
1549 Lisp_Object bucket, tem, zero;
1550 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1552 CHECK_STRING (string);
1553 if (type == 0)
1554 return call3 (collection, string, predicate, Qt);
1555 allmatches = bucket = Qnil;
1556 zero = make_number (0);
1558 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1559 tail = collection;
1560 if (type == 2)
1562 collection = check_obarray (collection);
1563 obsize = XVECTOR (collection)->size;
1564 bucket = XVECTOR (collection)->contents[index];
1567 while (1)
1569 /* Get the next element of the alist, obarray, or hash-table. */
1570 /* Exit the loop if the elements are all used up. */
1571 /* elt gets the alist element or symbol.
1572 eltstring gets the name to check as a completion. */
1574 if (type == 1)
1576 if (!CONSP (tail))
1577 break;
1578 elt = XCAR (tail);
1579 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1580 tail = XCDR (tail);
1582 else if (type == 2)
1584 if (!EQ (bucket, zero))
1586 if (!SYMBOLP (bucket))
1587 error ("Bad data in guts of obarray");
1588 elt = bucket;
1589 eltstring = elt;
1590 if (XSYMBOL (bucket)->next)
1591 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1592 else
1593 XSETFASTINT (bucket, 0);
1595 else if (++index >= obsize)
1596 break;
1597 else
1599 bucket = XVECTOR (collection)->contents[index];
1600 continue;
1603 else /* if (type == 3) */
1605 while (index < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1606 && NILP (HASH_HASH (XHASH_TABLE (collection), index)))
1607 index++;
1608 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
1609 break;
1610 else
1611 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), index++);
1614 /* Is this element a possible completion? */
1616 if (SYMBOLP (eltstring))
1617 eltstring = Fsymbol_name (eltstring);
1619 if (STRINGP (eltstring)
1620 && SCHARS (string) <= SCHARS (eltstring)
1621 /* If HIDE_SPACES, reject alternatives that start with space
1622 unless the input starts with space. */
1623 && (NILP (hide_spaces)
1624 || (SBYTES (string) > 0
1625 && SREF (string, 0) == ' ')
1626 || SREF (eltstring, 0) != ' ')
1627 && (tem = Fcompare_strings (eltstring, zero,
1628 make_number (SCHARS (string)),
1629 string, zero,
1630 make_number (SCHARS (string)),
1631 completion_ignore_case ? Qt : Qnil),
1632 EQ (Qt, tem)))
1634 /* Yes. */
1635 Lisp_Object regexps;
1636 Lisp_Object zero;
1637 XSETFASTINT (zero, 0);
1639 /* Ignore this element if it fails to match all the regexps. */
1641 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1642 regexps = XCDR (regexps))
1644 if (bindcount < 0) {
1645 bindcount = SPECPDL_INDEX ();
1646 specbind (Qcase_fold_search,
1647 completion_ignore_case ? Qt : Qnil);
1649 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1650 if (NILP (tem))
1651 break;
1653 if (CONSP (regexps))
1654 continue;
1657 /* Ignore this element if there is a predicate
1658 and the predicate doesn't like it. */
1660 if (!NILP (predicate))
1662 if (EQ (predicate, Qcommandp))
1663 tem = Fcommandp (elt, Qnil);
1664 else
1666 if (bindcount >= 0) {
1667 unbind_to (bindcount, Qnil);
1668 bindcount = -1;
1670 GCPRO4 (tail, eltstring, allmatches, string);
1671 tem = type == 3
1672 ? call2 (predicate, elt,
1673 HASH_VALUE (XHASH_TABLE (collection), index - 1))
1674 : call1 (predicate, elt);
1675 UNGCPRO;
1677 if (NILP (tem)) continue;
1679 /* Ok => put it on the list. */
1680 allmatches = Fcons (eltstring, allmatches);
1684 if (bindcount >= 0) {
1685 unbind_to (bindcount, Qnil);
1686 bindcount = -1;
1689 return Fnreverse (allmatches);
1692 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1693 doc: /* Read a string in the minibuffer, with completion.
1694 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1695 COLLECTION can be a list of strings, an alist, an obarray or a hash table.
1696 COLLECTION can also be a function to do the completion itself.
1697 PREDICATE limits completion to a subset of COLLECTION.
1698 See `try-completion' and `all-completions' for more details
1699 on completion, COLLECTION, and PREDICATE.
1701 REQUIRE-MATCH can take the following values:
1702 - t means that the user is not allowed to exit unless
1703 the input is (or completes to) an element of COLLECTION or is null.
1704 - nil means that the user can exit with any input.
1705 - `confirm' means that the user can exit with any input, but she needs
1706 to confirm her choice if the input is not an element of COLLECTION.
1707 - `confirm-after-completion' means that the user can exit with any
1708 input, but she needs to confirm her choice if she called
1709 `minibuffer-complete' right before `minibuffer-complete-and-exit'
1710 and the input is not an element of COLLECTION.
1711 - anything else behaves like t except that typing RET does not exit if it
1712 does non-null completion.
1714 If the input is null, `completing-read' returns DEF, or the first element
1715 of the list of default values, or an empty string if DEF is nil,
1716 regardless of the value of REQUIRE-MATCH.
1718 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
1719 with point positioned at the end.
1720 If it is (STRING . POSITION), the initial input is STRING, but point
1721 is placed at _zero-indexed_ position POSITION in STRING. (*Note*
1722 that this is different from `read-from-minibuffer' and related
1723 functions, which use one-indexing for POSITION.) This feature is
1724 deprecated--it is best to pass nil for INITIAL-INPUT and supply the
1725 default value DEF instead. The user can yank the default value into
1726 the minibuffer easily using \\[next-history-element].
1728 HIST, if non-nil, specifies a history list and optionally the initial
1729 position in the list. It can be a symbol, which is the history list
1730 variable to use, or it can be a cons cell (HISTVAR . HISTPOS). In
1731 that case, HISTVAR is the history list variable to use, and HISTPOS
1732 is the initial position (the position in the list used by the
1733 minibuffer history commands). For consistency, you should also
1734 specify that element of the history as the value of
1735 INITIAL-INPUT. (This is the only case in which you should use
1736 INITIAL-INPUT instead of DEF.) Positions are counted starting from
1737 1 at the beginning of the list. The variable `history-length'
1738 controls the maximum length of a history list.
1740 DEF, if non-nil, is the default value or the list of default values.
1742 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
1743 the current input method and the setting of `enable-multibyte-characters'.
1745 Completion ignores case if the ambient value of
1746 `completion-ignore-case' is non-nil. */)
1747 (Lisp_Object prompt, Lisp_Object collection, Lisp_Object predicate, Lisp_Object require_match, Lisp_Object initial_input, Lisp_Object hist, Lisp_Object def, Lisp_Object inherit_input_method)
1749 Lisp_Object val, histvar, histpos, position;
1750 Lisp_Object init;
1751 int pos = 0;
1752 int count = SPECPDL_INDEX ();
1753 struct gcpro gcpro1;
1755 init = initial_input;
1756 GCPRO1 (def);
1758 specbind (Qminibuffer_completion_table, collection);
1759 specbind (Qminibuffer_completion_predicate, predicate);
1760 specbind (Qminibuffer_completion_confirm,
1761 EQ (require_match, Qt) ? Qnil : require_match);
1763 position = Qnil;
1764 if (!NILP (init))
1766 if (CONSP (init))
1768 position = Fcdr (init);
1769 init = Fcar (init);
1771 CHECK_STRING (init);
1772 if (!NILP (position))
1774 CHECK_NUMBER (position);
1775 /* Convert to distance from end of input. */
1776 pos = XINT (position) - SCHARS (init);
1780 if (SYMBOLP (hist))
1782 histvar = hist;
1783 histpos = Qnil;
1785 else
1787 histvar = Fcar_safe (hist);
1788 histpos = Fcdr_safe (hist);
1790 if (NILP (histvar))
1791 histvar = Qminibuffer_history;
1792 if (NILP (histpos))
1793 XSETFASTINT (histpos, 0);
1795 val = read_minibuf (NILP (require_match)
1796 ? (NILP (Vminibuffer_completing_file_name)
1797 || EQ (Vminibuffer_completing_file_name, Qlambda)
1798 ? Vminibuffer_local_completion_map
1799 : Vminibuffer_local_filename_completion_map)
1800 : (NILP (Vminibuffer_completing_file_name)
1801 || EQ (Vminibuffer_completing_file_name, Qlambda)
1802 ? Vminibuffer_local_must_match_map
1803 : Vminibuffer_local_filename_must_match_map),
1804 init, prompt, make_number (pos), 0,
1805 histvar, histpos, def, 0,
1806 !NILP (inherit_input_method));
1808 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
1809 val = CONSP (def) ? XCAR (def) : def;
1811 RETURN_UNGCPRO (unbind_to (count, val));
1814 Lisp_Object Fassoc_string (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold);
1816 /* Test whether TXT is an exact completion. */
1817 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1818 doc: /* Return non-nil if STRING is a valid completion.
1819 Takes the same arguments as `all-completions' and `try-completion'.
1820 If COLLECTION is a function, it is called with three arguments:
1821 the values STRING, PREDICATE and `lambda'. */)
1822 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
1824 Lisp_Object regexps, tail, tem = Qnil;
1825 int i = 0;
1827 CHECK_STRING (string);
1829 if ((CONSP (collection)
1830 && (!SYMBOLP (XCAR (collection)) || NILP (XCAR (collection))))
1831 || NILP (collection))
1833 tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
1834 if (NILP (tem))
1835 return Qnil;
1837 else if (VECTORP (collection))
1839 /* Bypass intern-soft as that loses for nil. */
1840 tem = oblookup (collection,
1841 SDATA (string),
1842 SCHARS (string),
1843 SBYTES (string));
1844 if (!SYMBOLP (tem))
1846 if (STRING_MULTIBYTE (string))
1847 string = Fstring_make_unibyte (string);
1848 else
1849 string = Fstring_make_multibyte (string);
1851 tem = oblookup (collection,
1852 SDATA (string),
1853 SCHARS (string),
1854 SBYTES (string));
1857 if (completion_ignore_case && !SYMBOLP (tem))
1859 for (i = XVECTOR (collection)->size - 1; i >= 0; i--)
1861 tail = XVECTOR (collection)->contents[i];
1862 if (SYMBOLP (tail))
1863 while (1)
1865 if (EQ((Fcompare_strings (string, make_number (0), Qnil,
1866 Fsymbol_name (tail),
1867 make_number (0) , Qnil, Qt)),
1868 Qt))
1870 tem = tail;
1871 break;
1873 if (XSYMBOL (tail)->next == 0)
1874 break;
1875 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1880 if (!SYMBOLP (tem))
1881 return Qnil;
1883 else if (HASH_TABLE_P (collection))
1885 struct Lisp_Hash_Table *h = XHASH_TABLE (collection);
1886 i = hash_lookup (h, string, NULL);
1887 if (i >= 0)
1888 tem = HASH_KEY (h, i);
1889 else
1890 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1891 if (!NILP (HASH_HASH (h, i)) &&
1892 EQ (Fcompare_strings (string, make_number (0), Qnil,
1893 HASH_KEY (h, i), make_number (0) , Qnil,
1894 completion_ignore_case ? Qt : Qnil),
1895 Qt))
1897 tem = HASH_KEY (h, i);
1898 break;
1900 if (!STRINGP (tem))
1901 return Qnil;
1903 else
1904 return call3 (collection, string, predicate, Qlambda);
1906 /* Reject this element if it fails to match all the regexps. */
1907 if (CONSP (Vcompletion_regexp_list))
1909 int count = SPECPDL_INDEX ();
1910 specbind (Qcase_fold_search, completion_ignore_case ? Qt : Qnil);
1911 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1912 regexps = XCDR (regexps))
1914 if (NILP (Fstring_match (XCAR (regexps),
1915 SYMBOLP (tem) ? string : tem,
1916 Qnil)))
1917 return unbind_to (count, Qnil);
1919 unbind_to (count, Qnil);
1922 /* Finally, check the predicate. */
1923 if (!NILP (predicate))
1925 return HASH_TABLE_P (collection)
1926 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (collection), i))
1927 : call1 (predicate, tem);
1929 else
1930 return Qt;
1933 DEFUN ("internal-complete-buffer", Finternal_complete_buffer, Sinternal_complete_buffer, 3, 3, 0,
1934 doc: /* Perform completion on buffer names.
1935 If the argument FLAG is nil, invoke `try-completion', if it's t, invoke
1936 `all-completions', otherwise invoke `test-completion'.
1938 The arguments STRING and PREDICATE are as in `try-completion',
1939 `all-completions', and `test-completion'. */)
1940 (Lisp_Object string, Lisp_Object predicate, Lisp_Object flag)
1942 if (NILP (flag))
1943 return Ftry_completion (string, Vbuffer_alist, predicate);
1944 else if (EQ (flag, Qt))
1946 Lisp_Object res = Fall_completions (string, Vbuffer_alist, predicate, Qnil);
1947 if (SCHARS (string) > 0)
1948 return res;
1949 else
1950 { /* Strip out internal buffers. */
1951 Lisp_Object bufs = res;
1952 /* First, look for a non-internal buffer in `res'. */
1953 while (CONSP (bufs) && SREF (XCAR (bufs), 0) == ' ')
1954 bufs = XCDR (bufs);
1955 if (NILP (bufs))
1956 /* All bufs in `res' are internal, so don't trip them out. */
1957 return res;
1958 res = bufs;
1959 while (CONSP (XCDR (bufs)))
1960 if (SREF (XCAR (XCDR (bufs)), 0) == ' ')
1961 XSETCDR (bufs, XCDR (XCDR (bufs)));
1962 else
1963 bufs = XCDR (bufs);
1964 return res;
1967 else /* assume `lambda' */
1968 return Ftest_completion (string, Vbuffer_alist, predicate);
1971 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1973 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
1974 doc: /* Like `assoc' but specifically for strings (and symbols).
1976 This returns the first element of LIST whose car matches the string or
1977 symbol KEY, or nil if no match exists. When performing the
1978 comparison, symbols are first converted to strings, and unibyte
1979 strings to multibyte. If the optional arg CASE-FOLD is non-nil, case
1980 is ignored.
1982 Unlike `assoc', KEY can also match an entry in LIST consisting of a
1983 single string, rather than a cons cell whose car is a string. */)
1984 (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold)
1986 register Lisp_Object tail;
1988 if (SYMBOLP (key))
1989 key = Fsymbol_name (key);
1991 for (tail = list; CONSP (tail); tail = XCDR (tail))
1993 register Lisp_Object elt, tem, thiscar;
1994 elt = XCAR (tail);
1995 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1996 if (SYMBOLP (thiscar))
1997 thiscar = Fsymbol_name (thiscar);
1998 else if (!STRINGP (thiscar))
1999 continue;
2000 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
2001 key, make_number (0), Qnil,
2002 case_fold);
2003 if (EQ (tem, Qt))
2004 return elt;
2005 QUIT;
2007 return Qnil;
2011 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
2012 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
2013 (void)
2015 return make_number (minibuf_level);
2018 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
2019 doc: /* Return the prompt string of the currently-active minibuffer.
2020 If no minibuffer is active, return nil. */)
2021 (void)
2023 return Fcopy_sequence (minibuf_prompt);
2027 void
2028 init_minibuf_once (void)
2030 Vminibuffer_list = Qnil;
2031 staticpro (&Vminibuffer_list);
2034 void
2035 syms_of_minibuf (void)
2037 minibuf_level = 0;
2038 minibuf_prompt = Qnil;
2039 staticpro (&minibuf_prompt);
2041 minibuf_save_list = Qnil;
2042 staticpro (&minibuf_save_list);
2044 Qcompletion_ignore_case = intern_c_string ("completion-ignore-case");
2045 staticpro (&Qcompletion_ignore_case);
2047 Qread_file_name_internal = intern_c_string ("read-file-name-internal");
2048 staticpro (&Qread_file_name_internal);
2050 Qminibuffer_default = intern_c_string ("minibuffer-default");
2051 staticpro (&Qminibuffer_default);
2052 Fset (Qminibuffer_default, Qnil);
2054 Qminibuffer_completion_table = intern_c_string ("minibuffer-completion-table");
2055 staticpro (&Qminibuffer_completion_table);
2057 Qminibuffer_completion_confirm = intern_c_string ("minibuffer-completion-confirm");
2058 staticpro (&Qminibuffer_completion_confirm);
2060 Qminibuffer_completion_predicate = intern_c_string ("minibuffer-completion-predicate");
2061 staticpro (&Qminibuffer_completion_predicate);
2063 staticpro (&last_minibuf_string);
2064 last_minibuf_string = Qnil;
2066 Quser_variable_p = intern_c_string ("user-variable-p");
2067 staticpro (&Quser_variable_p);
2069 Qminibuffer_history = intern_c_string ("minibuffer-history");
2070 staticpro (&Qminibuffer_history);
2072 Qbuffer_name_history = intern_c_string ("buffer-name-history");
2073 staticpro (&Qbuffer_name_history);
2074 Fset (Qbuffer_name_history, Qnil);
2076 Qminibuffer_setup_hook = intern_c_string ("minibuffer-setup-hook");
2077 staticpro (&Qminibuffer_setup_hook);
2079 Qminibuffer_exit_hook = intern_c_string ("minibuffer-exit-hook");
2080 staticpro (&Qminibuffer_exit_hook);
2082 Qhistory_length = intern_c_string ("history-length");
2083 staticpro (&Qhistory_length);
2085 Qcurrent_input_method = intern_c_string ("current-input-method");
2086 staticpro (&Qcurrent_input_method);
2088 Qactivate_input_method = intern_c_string ("activate-input-method");
2089 staticpro (&Qactivate_input_method);
2091 Qcase_fold_search = intern_c_string ("case-fold-search");
2092 staticpro (&Qcase_fold_search);
2094 Qread_expression_history = intern_c_string ("read-expression-history");
2095 staticpro (&Qread_expression_history);
2097 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2098 doc: /* If this is non-nil, `read-buffer' does its work by calling this function. */);
2099 Vread_buffer_function = Qnil;
2101 DEFVAR_BOOL ("read-buffer-completion-ignore-case",
2102 &read_buffer_completion_ignore_case,
2103 doc: /* *Non-nil means completion ignores case when reading a buffer name. */);
2104 read_buffer_completion_ignore_case = 0;
2106 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2107 doc: /* Normal hook run just after entry to minibuffer. */);
2108 Vminibuffer_setup_hook = Qnil;
2110 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2111 doc: /* Normal hook run just after exit from minibuffer. */);
2112 Vminibuffer_exit_hook = Qnil;
2114 DEFVAR_LISP ("history-length", &Vhistory_length,
2115 doc: /* *Maximum length for history lists before truncation takes place.
2116 A number means that length; t means infinite. Truncation takes place
2117 just after a new element is inserted. Setting the `history-length'
2118 property of a history variable overrides this default. */);
2119 XSETFASTINT (Vhistory_length, 30);
2121 DEFVAR_BOOL ("history-delete-duplicates", &history_delete_duplicates,
2122 doc: /* *Non-nil means to delete duplicates in history.
2123 If set to t when adding a new history element, all previous identical
2124 elements are deleted from the history list. */);
2125 history_delete_duplicates = 0;
2127 DEFVAR_LISP ("history-add-new-input", &Vhistory_add_new_input,
2128 doc: /* *Non-nil means to add new elements in history.
2129 If set to nil, minibuffer reading functions don't add new elements to the
2130 history list, so it is possible to do this afterwards by calling
2131 `add-to-history' explicitly. */);
2132 Vhistory_add_new_input = Qt;
2134 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
2135 doc: /* Non-nil means don't consider case significant in completion.
2136 For file-name completion, `read-file-name-completion-ignore-case'
2137 controls the behavior, rather than this variable.
2138 For buffer name completion, `read-buffer-completion-ignore-case'
2139 controls the behavior, rather than this variable. */);
2140 completion_ignore_case = 0;
2142 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2143 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
2144 This variable makes a difference whenever the minibuffer window is active. */);
2145 enable_recursive_minibuffers = 0;
2147 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2148 doc: /* Alist or obarray used for completion in the minibuffer.
2149 This becomes the ALIST argument to `try-completion' and `all-completions'.
2150 The value can also be a list of strings or a hash table.
2152 The value may alternatively be a function, which is given three arguments:
2153 STRING, the current buffer contents;
2154 PREDICATE, the predicate for filtering possible matches;
2155 CODE, which says what kind of things to do.
2156 CODE can be nil, t or `lambda':
2157 nil -- return the best completion of STRING, or nil if there is none.
2158 t -- return a list of all possible completions of STRING.
2159 lambda -- return t if STRING is a valid completion as it stands. */);
2160 Vminibuffer_completion_table = Qnil;
2162 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2163 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
2164 Vminibuffer_completion_predicate = Qnil;
2166 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
2167 doc: /* Whether to demand confirmation of completion before exiting minibuffer.
2168 If nil, confirmation is not required.
2169 If the value is `confirm', the user may exit with an input that is not
2170 a valid completion alternative, but Emacs asks for confirmation.
2171 If the value is `confirm-after-completion', the user may exit with an
2172 input that is not a valid completion alternative, but Emacs asks for
2173 confirmation if the user submitted the input right after any of the
2174 completion commands listed in `minibuffer-confirm-exit-commands'. */);
2175 Vminibuffer_completion_confirm = Qnil;
2177 DEFVAR_LISP ("minibuffer-completing-file-name",
2178 &Vminibuffer_completing_file_name,
2179 doc: /* Non-nil means completing file names. */);
2180 Vminibuffer_completing_file_name = Qnil;
2182 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2183 doc: /* Value that `help-form' takes on inside the minibuffer. */);
2184 Vminibuffer_help_form = Qnil;
2186 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2187 doc: /* History list symbol to add minibuffer values to.
2188 Each string of minibuffer input, as it appears on exit from the minibuffer,
2189 is added with
2190 (set minibuffer-history-variable
2191 (cons STRING (symbol-value minibuffer-history-variable))) */);
2192 XSETFASTINT (Vminibuffer_history_variable, 0);
2194 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2195 doc: /* Current position of redoing in the history list. */);
2196 Vminibuffer_history_position = Qnil;
2198 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2199 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
2200 Some uses of the echo area also raise that frame (since they use it too). */);
2201 minibuffer_auto_raise = 0;
2203 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
2204 doc: /* List of regexps that should restrict possible completions.
2205 The basic completion functions only consider a completion acceptable
2206 if it matches all regular expressions in this list, with
2207 `case-fold-search' bound to the value of `completion-ignore-case'.
2208 See Info node `(elisp)Basic Completion', for a description of these
2209 functions. */);
2210 Vcompletion_regexp_list = Qnil;
2212 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2213 &minibuffer_allow_text_properties,
2214 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
2215 This also affects `read-string', but it does not affect `read-minibuffer',
2216 `read-no-blanks-input', or any of the functions that do minibuffer input
2217 with completion; they always discard text properties. */);
2218 minibuffer_allow_text_properties = 0;
2220 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2221 doc: /* Text properties that are added to minibuffer prompts.
2222 These are in addition to the basic `field' property, and stickiness
2223 properties. */);
2224 /* We use `intern' here instead of Qread_only to avoid
2225 initialization-order problems. */
2226 Vminibuffer_prompt_properties
2227 = Fcons (intern_c_string ("read-only"), Fcons (Qt, Qnil));
2229 DEFVAR_LISP ("read-expression-map", &Vread_expression_map,
2230 doc: /* Minibuffer keymap used for reading Lisp expressions. */);
2231 Vread_expression_map = Qnil;
2233 defsubr (&Sactive_minibuffer_window);
2234 defsubr (&Sset_minibuffer_window);
2235 defsubr (&Sread_from_minibuffer);
2236 defsubr (&Seval_minibuffer);
2237 defsubr (&Sread_minibuffer);
2238 defsubr (&Sread_string);
2239 defsubr (&Sread_command);
2240 defsubr (&Sread_variable);
2241 defsubr (&Sinternal_complete_buffer);
2242 defsubr (&Sread_buffer);
2243 defsubr (&Sread_no_blanks_input);
2244 defsubr (&Sminibuffer_depth);
2245 defsubr (&Sminibuffer_prompt);
2247 defsubr (&Sminibufferp);
2248 defsubr (&Sminibuffer_prompt_end);
2249 defsubr (&Sminibuffer_contents);
2250 defsubr (&Sminibuffer_contents_no_properties);
2251 defsubr (&Sminibuffer_completion_contents);
2253 defsubr (&Stry_completion);
2254 defsubr (&Sall_completions);
2255 defsubr (&Stest_completion);
2256 defsubr (&Sassoc_string);
2257 defsubr (&Scompleting_read);
2260 /* arch-tag: 8f69b601-fba3-484c-a6dd-ceaee54a7a73
2261 (do not change this comment) */