Use PAT rather than UPAT in pcase macros
[emacs.git] / src / minibuf.c
blobcf0cbca77c1961135a852a3c674b271d2cd33795
1 /* Minibuffer input and completion.
3 Copyright (C) 1985-1986, 1993-2015 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <errno.h>
23 #include <stdio.h>
25 #include <binary-io.h>
27 #include "lisp.h"
28 #include "commands.h"
29 #include "character.h"
30 #include "buffer.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"
38 #include "termhooks.h"
39 #include "systty.h"
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 static Lisp_Object minibuf_save_list;
53 /* Depth in minibuffer invocations. */
55 EMACS_INT minibuf_level;
57 /* Fread_minibuffer leaves the input here as a string. */
59 Lisp_Object last_minibuf_string;
61 /* Prompt to display in front of the mini-buffer contents. */
63 static Lisp_Object minibuf_prompt;
65 /* Width of current mini-buffer prompt. Only set after display_line
66 of the line that contains the prompt. */
68 static ptrdiff_t minibuf_prompt_width;
71 /* Put minibuf on currently selected frame's minibuffer.
72 We do this whenever the user starts a new minibuffer
73 or when a minibuffer exits. */
75 static void
76 choose_minibuf_frame (void)
78 if (FRAMEP (selected_frame)
79 && FRAME_LIVE_P (XFRAME (selected_frame))
80 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
82 struct frame *sf = XFRAME (selected_frame);
83 Lisp_Object buffer;
85 /* I don't think that any frames may validly have a null minibuffer
86 window anymore. */
87 if (NILP (sf->minibuffer_window))
88 emacs_abort ();
90 /* Under X, we come here with minibuf_window being the
91 minibuffer window of the unused termcap window created in
92 init_window_once. That window doesn't have a buffer. */
93 buffer = XWINDOW (minibuf_window)->contents;
94 if (BUFFERP (buffer))
95 /* Use set_window_buffer instead of Fset_window_buffer (see
96 discussion of bug#11984, bug#12025, bug#12026). */
97 set_window_buffer (sf->minibuffer_window, buffer, 0, 0);
98 minibuf_window = sf->minibuffer_window;
101 /* Make sure no other frame has a minibuffer as its selected window,
102 because the text would not be displayed in it, and that would be
103 confusing. Only allow the selected frame to do this,
104 and that only if the minibuffer is active. */
106 Lisp_Object tail, frame;
108 FOR_EACH_FRAME (tail, frame)
109 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
110 && !(EQ (frame, selected_frame)
111 && minibuf_level > 0))
112 Fset_frame_selected_window (frame, Fframe_first_window (frame), Qnil);
116 DEFUN ("active-minibuffer-window", Factive_minibuffer_window,
117 Sactive_minibuffer_window, 0, 0, 0,
118 doc: /* Return the currently active minibuffer window, or nil if none. */)
119 (void)
121 return minibuf_level ? minibuf_window : Qnil;
124 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
125 Sset_minibuffer_window, 1, 1, 0,
126 doc: /* Specify which minibuffer window to use for the minibuffer.
127 This affects where the minibuffer is displayed if you put text in it
128 without invoking the usual minibuffer commands. */)
129 (Lisp_Object window)
131 CHECK_WINDOW (window);
132 if (! MINI_WINDOW_P (XWINDOW (window)))
133 error ("Window is not a minibuffer window");
135 minibuf_window = window;
137 return window;
141 /* Actual minibuffer invocation. */
143 static void read_minibuf_unwind (void);
144 static void run_exit_minibuf_hook (void);
147 /* Read a Lisp object from VAL and return it. If VAL is an empty
148 string, and DEFALT is a string, read from DEFALT instead of VAL. */
150 static Lisp_Object
151 string_to_object (Lisp_Object val, Lisp_Object defalt)
153 Lisp_Object expr_and_pos;
154 ptrdiff_t pos;
156 if (STRINGP (val) && SCHARS (val) == 0)
158 if (STRINGP (defalt))
159 val = defalt;
160 else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
161 val = XCAR (defalt);
164 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
165 pos = XINT (Fcdr (expr_and_pos));
166 if (pos != SCHARS (val))
168 /* Ignore trailing whitespace; any other trailing junk
169 is an error. */
170 ptrdiff_t i;
171 pos = string_char_to_byte (val, pos);
172 for (i = pos; i < SBYTES (val); i++)
174 int c = SREF (val, i);
175 if (c != ' ' && c != '\t' && c != '\n')
176 error ("Trailing garbage following expression");
180 val = Fcar (expr_and_pos);
181 return val;
185 /* Like read_minibuf but reading from stdin. This function is called
186 from read_minibuf to do the job if noninteractive. */
188 static Lisp_Object
189 read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
190 Lisp_Object prompt, Lisp_Object backup_n,
191 bool expflag,
192 Lisp_Object histvar, Lisp_Object histpos,
193 Lisp_Object defalt,
194 bool allow_props, bool inherit_input_method)
196 ptrdiff_t size, len;
197 char *line;
198 Lisp_Object val;
199 int c;
200 unsigned char hide_char = 0;
201 struct emacs_tty etty;
202 bool etty_valid;
204 /* Check, whether we need to suppress echoing. */
205 if (CHARACTERP (Vread_hide_char))
206 hide_char = XFASTINT (Vread_hide_char);
208 /* Manipulate tty. */
209 if (hide_char)
211 etty_valid = emacs_get_tty (fileno (stdin), &etty) == 0;
212 if (etty_valid)
213 set_binary_mode (fileno (stdin), O_BINARY);
214 suppress_echo_on_tty (fileno (stdin));
217 fwrite (SDATA (prompt), 1, SBYTES (prompt), stdout);
218 fflush (stdout);
220 val = Qnil;
221 size = 100;
222 len = 0;
223 line = xmalloc (size);
225 while ((c = getchar ()) != '\n' && c != '\r')
227 if (c == EOF)
229 if (errno != EINTR)
230 break;
232 else
234 if (hide_char)
235 fprintf (stdout, "%c", hide_char);
236 if (len == size)
238 if (STRING_BYTES_BOUND / 2 < size)
239 memory_full (SIZE_MAX);
240 size *= 2;
241 line = xrealloc (line, size);
243 line[len++] = c;
247 /* Reset tty. */
248 if (hide_char)
250 fprintf (stdout, "\n");
251 if (etty_valid)
253 emacs_set_tty (fileno (stdin), &etty, 0);
254 set_binary_mode (fileno (stdin), O_TEXT);
258 if (len || c == '\n' || c == '\r')
260 val = make_string (line, len);
261 xfree (line);
263 else
265 xfree (line);
266 error ("Error reading from stdin");
269 /* If Lisp form desired instead of string, parse it. */
270 if (expflag)
271 val = string_to_object (val, CONSP (defalt) ? XCAR (defalt) : defalt);
273 return val;
276 DEFUN ("minibufferp", Fminibufferp,
277 Sminibufferp, 0, 1, 0,
278 doc: /* Return t if BUFFER is a minibuffer.
279 No argument or nil as argument means use current buffer as BUFFER.
280 BUFFER can be a buffer or a buffer name. */)
281 (Lisp_Object buffer)
283 Lisp_Object tem;
285 if (NILP (buffer))
286 buffer = Fcurrent_buffer ();
287 else if (STRINGP (buffer))
288 buffer = Fget_buffer (buffer);
289 else
290 CHECK_BUFFER (buffer);
292 tem = Fmemq (buffer, Vminibuffer_list);
293 return ! NILP (tem) ? Qt : Qnil;
296 DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
297 Sminibuffer_prompt_end, 0, 0, 0,
298 doc: /* Return the buffer position of the end of the minibuffer prompt.
299 Return (point-min) if current buffer is not a minibuffer. */)
300 (void)
302 /* This function is written to be most efficient when there's a prompt. */
303 Lisp_Object beg, end, tem;
304 beg = make_number (BEGV);
306 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
307 if (NILP (tem))
308 return beg;
310 end = Ffield_end (beg, Qnil, Qnil);
312 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
313 return beg;
314 else
315 return end;
318 DEFUN ("minibuffer-contents", Fminibuffer_contents,
319 Sminibuffer_contents, 0, 0, 0,
320 doc: /* Return the user input in a minibuffer as a string.
321 If the current buffer is not a minibuffer, return its entire contents. */)
322 (void)
324 ptrdiff_t prompt_end = XINT (Fminibuffer_prompt_end ());
325 return make_buffer_string (prompt_end, ZV, 1);
328 DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
329 Sminibuffer_contents_no_properties, 0, 0, 0,
330 doc: /* Return the user input in a minibuffer as a string, without text-properties.
331 If the current buffer is not a minibuffer, return its entire contents. */)
332 (void)
334 ptrdiff_t prompt_end = XINT (Fminibuffer_prompt_end ());
335 return make_buffer_string (prompt_end, ZV, 0);
338 DEFUN ("minibuffer-completion-contents", Fminibuffer_completion_contents,
339 Sminibuffer_completion_contents, 0, 0, 0,
340 doc: /* Return the user input in a minibuffer before point as a string.
341 That is what completion commands operate on.
342 If the current buffer is not a minibuffer, return its entire contents. */)
343 (void)
345 ptrdiff_t prompt_end = XINT (Fminibuffer_prompt_end ());
346 if (PT < prompt_end)
347 error ("Cannot do completion in the prompt");
348 return make_buffer_string (prompt_end, PT, 1);
352 /* Read from the minibuffer using keymap MAP and initial contents INITIAL,
353 putting point minus BACKUP_N bytes from the end of INITIAL,
354 prompting with PROMPT (a string), using history list HISTVAR
355 with initial position HISTPOS. INITIAL should be a string or a
356 cons of a string and an integer. BACKUP_N should be <= 0, or
357 Qnil, which is equivalent to 0. If INITIAL is a cons, BACKUP_N is
358 ignored and replaced with an integer that puts point at one-indexed
359 position N in INITIAL, where N is the CDR of INITIAL, or at the
360 beginning of INITIAL if N <= 0.
362 Normally return the result as a string (the text that was read),
363 but if EXPFLAG, read it and return the object read.
364 If HISTVAR is given, save the value read on that history only if it doesn't
365 match the front of that history list exactly. The value is pushed onto
366 the list as the string that was read.
368 DEFALT specifies the default value for the sake of history commands.
370 If ALLOW_PROPS, do not throw away text properties.
372 if INHERIT_INPUT_METHOD, the minibuffer inherits the
373 current input method. */
375 static Lisp_Object
376 read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
377 bool expflag,
378 Lisp_Object histvar, Lisp_Object histpos, Lisp_Object defalt,
379 bool allow_props, bool inherit_input_method)
381 Lisp_Object val;
382 ptrdiff_t count = SPECPDL_INDEX ();
383 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
384 Lisp_Object enable_multibyte;
385 EMACS_INT pos = 0;
386 /* String to add to the history. */
387 Lisp_Object histstring;
388 Lisp_Object histval;
390 Lisp_Object empty_minibuf;
391 Lisp_Object dummy, frame;
393 specbind (Qminibuffer_default, defalt);
394 specbind (Qinhibit_read_only, Qnil);
396 /* If Vminibuffer_completing_file_name is `lambda' on entry, it was t
397 in previous recursive minibuffer, but was not set explicitly
398 to t for this invocation, so set it to nil in this minibuffer.
399 Save the old value now, before we change it. */
400 specbind (intern ("minibuffer-completing-file-name"),
401 Vminibuffer_completing_file_name);
402 if (EQ (Vminibuffer_completing_file_name, Qlambda))
403 Vminibuffer_completing_file_name = Qnil;
405 #ifdef HAVE_WINDOW_SYSTEM
406 if (display_hourglass_p)
407 cancel_hourglass ();
408 #endif
410 if (!NILP (initial))
412 if (CONSP (initial))
414 Lisp_Object backup_n = XCDR (initial);
415 initial = XCAR (initial);
416 CHECK_STRING (initial);
417 if (!NILP (backup_n))
419 CHECK_NUMBER (backup_n);
420 /* Convert to distance from end of input. */
421 if (XINT (backup_n) < 1)
422 /* A number too small means the beginning of the string. */
423 pos = - SCHARS (initial);
424 else
425 pos = XINT (backup_n) - 1 - SCHARS (initial);
428 else
429 CHECK_STRING (initial);
431 val = Qnil;
432 ambient_dir = BVAR (current_buffer, directory);
433 input_method = Qnil;
434 enable_multibyte = Qnil;
436 if (!STRINGP (prompt))
437 prompt = empty_unibyte_string;
439 if (!enable_recursive_minibuffers
440 && minibuf_level > 0)
442 if (EQ (selected_window, minibuf_window))
443 error ("Command attempted to use minibuffer while in minibuffer");
444 else
445 /* If we're in another window, cancel the minibuffer that's active. */
446 Fthrow (Qexit,
447 build_string ("Command attempted to use minibuffer while in minibuffer"));
450 if ((noninteractive
451 /* In case we are running as a daemon, only do this before
452 detaching from the terminal. */
453 || (IS_DAEMON && DAEMON_RUNNING))
454 && NILP (Vexecuting_kbd_macro))
456 val = read_minibuf_noninteractive (map, initial, prompt,
457 make_number (pos),
458 expflag, histvar, histpos, defalt,
459 allow_props, inherit_input_method);
460 return unbind_to (count, val);
463 /* Choose the minibuffer window and frame, and take action on them. */
465 /* Prepare for restoring the current buffer since choose_minibuf_frame
466 calling Fset_frame_selected_window may change it (Bug#12766). */
467 record_unwind_protect (restore_buffer, Fcurrent_buffer ());
469 choose_minibuf_frame ();
471 record_unwind_protect_void (choose_minibuf_frame);
473 record_unwind_protect (restore_window_configuration,
474 Fcurrent_window_configuration (Qnil));
476 /* If the minibuffer window is on a different frame, save that
477 frame's configuration too. */
478 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
479 if (!EQ (mini_frame, selected_frame))
480 record_unwind_protect (restore_window_configuration,
481 Fcurrent_window_configuration (mini_frame));
483 /* If the minibuffer is on an iconified or invisible frame,
484 make it visible now. */
485 Fmake_frame_visible (mini_frame);
487 if (minibuffer_auto_raise)
488 Fraise_frame (mini_frame);
490 temporarily_switch_to_single_kboard (XFRAME (mini_frame));
492 /* We have to do this after saving the window configuration
493 since that is what restores the current buffer. */
495 /* Arrange to restore a number of minibuffer-related variables.
496 We could bind each variable separately, but that would use lots of
497 specpdl slots. */
498 minibuf_save_list
499 = Fcons (Voverriding_local_map,
500 Fcons (minibuf_window,
501 minibuf_save_list));
502 minibuf_save_list
503 = Fcons (minibuf_prompt,
504 Fcons (make_number (minibuf_prompt_width),
505 Fcons (Vhelp_form,
506 Fcons (Vcurrent_prefix_arg,
507 Fcons (Vminibuffer_history_position,
508 Fcons (Vminibuffer_history_variable,
509 minibuf_save_list))))));
511 record_unwind_protect_void (read_minibuf_unwind);
512 minibuf_level++;
513 /* We are exiting the minibuffer one way or the other, so run the hook.
514 It should be run before unwinding the minibuf settings. Do it
515 separately from read_minibuf_unwind because we need to make sure that
516 read_minibuf_unwind is fully executed even if exit-minibuffer-hook
517 signals an error. --Stef */
518 record_unwind_protect_void (run_exit_minibuf_hook);
520 /* Now that we can restore all those variables, start changing them. */
522 minibuf_prompt_width = 0;
523 minibuf_prompt = Fcopy_sequence (prompt);
524 Vminibuffer_history_position = histpos;
525 Vminibuffer_history_variable = histvar;
526 Vhelp_form = Vminibuffer_help_form;
527 /* If this minibuffer is reading a file name, that doesn't mean
528 recursive ones are. But we cannot set it to nil, because
529 completion code still need to know the minibuffer is completing a
530 file name. So use `lambda' as intermediate value meaning
531 "t" in this minibuffer, but "nil" in next minibuffer. */
532 if (!NILP (Vminibuffer_completing_file_name))
533 Vminibuffer_completing_file_name = Qlambda;
535 /* If variable is unbound, make it nil. */
536 histval = find_symbol_value (Vminibuffer_history_variable);
537 if (EQ (histval, Qunbound))
539 Fset (Vminibuffer_history_variable, Qnil);
540 histval = Qnil;
543 if (inherit_input_method)
545 /* `current-input-method' is buffer local. So, remember it in
546 INPUT_METHOD before changing the current buffer. */
547 input_method = Fsymbol_value (Qcurrent_input_method);
548 enable_multibyte = BVAR (current_buffer, enable_multibyte_characters);
551 /* Switch to the minibuffer. */
553 minibuffer = get_minibuffer (minibuf_level);
554 Fset_buffer (minibuffer);
556 /* Defeat (setq-default truncate-lines t), since truncated lines do
557 not work correctly in minibuffers. (Bug#5715, etc) */
558 bset_truncate_lines (current_buffer, Qnil);
560 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
561 if (inherit_input_method)
562 bset_enable_multibyte_characters (current_buffer, enable_multibyte);
564 /* The current buffer's default directory is usually the right thing
565 for our minibuffer here. However, if you're typing a command at
566 a minibuffer-only frame when minibuf_level is zero, then buf IS
567 the current_buffer, so reset_buffer leaves buf's default
568 directory unchanged. This is a bummer when you've just started
569 up Emacs and buf's default directory is Qnil. Here's a hack; can
570 you think of something better to do? Find another buffer with a
571 better directory, and use that one instead. */
572 if (STRINGP (ambient_dir))
573 bset_directory (current_buffer, ambient_dir);
574 else
576 Lisp_Object tail, buf;
578 FOR_EACH_LIVE_BUFFER (tail, buf)
579 if (STRINGP (BVAR (XBUFFER (buf), directory)))
581 bset_directory (current_buffer,
582 BVAR (XBUFFER (buf), directory));
583 break;
587 if (!EQ (mini_frame, selected_frame))
588 Fredirect_frame_focus (selected_frame, mini_frame);
590 Vminibuf_scroll_window = selected_window;
591 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
592 minibuf_selected_window = selected_window;
594 /* Empty out the minibuffers of all frames other than the one
595 where we are going to display one now.
596 Set them to point to ` *Minibuf-0*', which is always empty. */
597 empty_minibuf = get_minibuffer (0);
599 FOR_EACH_FRAME (dummy, frame)
601 Lisp_Object root_window = Fframe_root_window (frame);
602 Lisp_Object mini_window = XWINDOW (root_window)->next;
604 if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
605 && !NILP (Fwindow_minibuffer_p (mini_window)))
606 /* Use set_window_buffer instead of Fset_window_buffer (see
607 discussion of bug#11984, bug#12025, bug#12026). */
608 set_window_buffer (mini_window, empty_minibuf, 0, 0);
611 /* Display this minibuffer in the proper window. */
612 /* Use set_window_buffer instead of Fset_window_buffer (see
613 discussion of bug#11984, bug#12025, bug#12026). */
614 set_window_buffer (minibuf_window, Fcurrent_buffer (), 0, 0);
615 Fselect_window (minibuf_window, Qnil);
616 XWINDOW (minibuf_window)->hscroll = 0;
617 XWINDOW (minibuf_window)->suspend_auto_hscroll = 0;
619 Fmake_local_variable (Qprint_escape_newlines);
620 print_escape_newlines = 1;
622 /* Erase the buffer. */
624 ptrdiff_t count1 = SPECPDL_INDEX ();
625 specbind (Qinhibit_read_only, Qt);
626 specbind (Qinhibit_modification_hooks, Qt);
627 Ferase_buffer ();
629 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
630 && ! STRING_MULTIBYTE (minibuf_prompt))
631 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
633 /* Insert the prompt, record where it ends. */
634 Finsert (1, &minibuf_prompt);
635 if (PT > BEG)
637 Fput_text_property (make_number (BEG), make_number (PT),
638 Qfront_sticky, Qt, Qnil);
639 Fput_text_property (make_number (BEG), make_number (PT),
640 Qrear_nonsticky, Qt, Qnil);
641 Fput_text_property (make_number (BEG), make_number (PT),
642 Qfield, Qt, Qnil);
643 Fadd_text_properties (make_number (BEG), make_number (PT),
644 Vminibuffer_prompt_properties, Qnil);
646 unbind_to (count1, Qnil);
649 minibuf_prompt_width = current_column ();
651 /* Put in the initial input. */
652 if (!NILP (initial))
654 Finsert (1, &initial);
655 Fforward_char (make_number (pos));
658 clear_message (1, 1);
659 bset_keymap (current_buffer, map);
661 /* Turn on an input method stored in INPUT_METHOD if any. */
662 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
663 call1 (Qactivate_input_method, input_method);
665 run_hook (Qminibuffer_setup_hook);
667 /* Don't allow the user to undo past this point. */
668 bset_undo_list (current_buffer, Qnil);
670 recursive_edit_1 ();
672 /* If cursor is on the minibuffer line,
673 show the user we have exited by putting it in column 0. */
674 if (XWINDOW (minibuf_window)->cursor.vpos >= 0
675 && !noninteractive)
677 XWINDOW (minibuf_window)->cursor.hpos = 0;
678 XWINDOW (minibuf_window)->cursor.x = 0;
679 XWINDOW (minibuf_window)->must_be_updated_p = true;
680 update_frame (XFRAME (selected_frame), true, true);
681 flush_frame (XFRAME (XWINDOW (minibuf_window)->frame));
684 /* Make minibuffer contents into a string. */
685 Fset_buffer (minibuffer);
686 if (allow_props)
687 val = Fminibuffer_contents ();
688 else
689 val = Fminibuffer_contents_no_properties ();
691 /* VAL is the string of minibuffer text. */
693 last_minibuf_string = val;
695 /* Choose the string to add to the history. */
696 if (SCHARS (val) != 0)
697 histstring = val;
698 else if (STRINGP (defalt))
699 histstring = defalt;
700 else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
701 histstring = XCAR (defalt);
702 else
703 histstring = Qnil;
705 /* Add the value to the appropriate history list, if any. */
706 if (!NILP (Vhistory_add_new_input)
707 && SYMBOLP (Vminibuffer_history_variable)
708 && !NILP (histstring))
710 /* If the caller wanted to save the value read on a history list,
711 then do so if the value is not already the front of the list. */
713 /* The value of the history variable must be a cons or nil. Other
714 values are unacceptable. We silently ignore these values. */
716 if (NILP (histval)
717 || (CONSP (histval)
718 /* Don't duplicate the most recent entry in the history. */
719 && (NILP (Fequal (histstring, Fcar (histval))))))
721 Lisp_Object length;
723 if (history_delete_duplicates) Fdelete (histstring, histval);
724 histval = Fcons (histstring, histval);
725 Fset (Vminibuffer_history_variable, histval);
727 /* Truncate if requested. */
728 length = Fget (Vminibuffer_history_variable, Qhistory_length);
729 if (NILP (length)) length = Vhistory_length;
730 if (INTEGERP (length))
732 if (XINT (length) <= 0)
733 Fset (Vminibuffer_history_variable, Qnil);
734 else
736 Lisp_Object temp;
738 temp = Fnthcdr (Fsub1 (length), histval);
739 if (CONSP (temp)) Fsetcdr (temp, Qnil);
745 /* If Lisp form desired instead of string, parse it. */
746 if (expflag)
747 val = string_to_object (val, defalt);
749 /* The appropriate frame will get selected
750 in set-window-configuration. */
751 return unbind_to (count, val);
754 /* Return a buffer to be used as the minibuffer at depth `depth'.
755 depth = 0 is the lowest allowed argument, and that is the value
756 used for nonrecursive minibuffer invocations. */
758 Lisp_Object
759 get_minibuffer (EMACS_INT depth)
761 Lisp_Object tail, num, buf;
762 char name[sizeof " *Minibuf-*" + INT_STRLEN_BOUND (EMACS_INT)];
764 XSETFASTINT (num, depth);
765 tail = Fnthcdr (num, Vminibuffer_list);
766 if (NILP (tail))
768 tail = list1 (Qnil);
769 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
771 buf = Fcar (tail);
772 if (NILP (buf) || !BUFFER_LIVE_P (XBUFFER (buf)))
774 buf = Fget_buffer_create
775 (make_formatted_string (name, " *Minibuf-%"pI"d*", depth));
777 /* Although the buffer's name starts with a space, undo should be
778 enabled in it. */
779 Fbuffer_enable_undo (buf);
781 XSETCAR (tail, buf);
783 else
785 ptrdiff_t count = SPECPDL_INDEX ();
786 /* We have to empty both overlay lists. Otherwise we end
787 up with overlays that think they belong to this buffer
788 while the buffer doesn't know about them any more. */
789 delete_all_overlays (XBUFFER (buf));
790 reset_buffer (XBUFFER (buf));
791 record_unwind_current_buffer ();
792 Fset_buffer (buf);
793 if (!NILP (Ffboundp (intern ("minibuffer-inactive-mode"))))
794 call0 (intern ("minibuffer-inactive-mode"));
795 else
796 Fkill_all_local_variables ();
797 unbind_to (count, Qnil);
800 return buf;
803 static void
804 run_exit_minibuf_hook (void)
806 safe_run_hooks (Qminibuffer_exit_hook);
809 /* This function is called on exiting minibuffer, whether normally or
810 not, and it restores the current window, buffer, etc. */
812 static void
813 read_minibuf_unwind (void)
815 Lisp_Object old_deactivate_mark;
816 Lisp_Object window;
818 /* If this was a recursive minibuffer,
819 tie the minibuffer window back to the outer level minibuffer buffer. */
820 minibuf_level--;
822 window = minibuf_window;
823 /* To keep things predictable, in case it matters, let's be in the
824 minibuffer when we reset the relevant variables. */
825 Fset_buffer (XWINDOW (window)->contents);
827 /* Restore prompt, etc, from outer minibuffer level. */
828 minibuf_prompt = Fcar (minibuf_save_list);
829 minibuf_save_list = Fcdr (minibuf_save_list);
830 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
831 minibuf_save_list = Fcdr (minibuf_save_list);
832 Vhelp_form = Fcar (minibuf_save_list);
833 minibuf_save_list = Fcdr (minibuf_save_list);
834 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
835 minibuf_save_list = Fcdr (minibuf_save_list);
836 Vminibuffer_history_position = Fcar (minibuf_save_list);
837 minibuf_save_list = Fcdr (minibuf_save_list);
838 Vminibuffer_history_variable = Fcar (minibuf_save_list);
839 minibuf_save_list = Fcdr (minibuf_save_list);
840 Voverriding_local_map = Fcar (minibuf_save_list);
841 minibuf_save_list = Fcdr (minibuf_save_list);
842 #if 0
843 temp = Fcar (minibuf_save_list);
844 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
845 minibuf_window = temp;
846 #endif
847 minibuf_save_list = Fcdr (minibuf_save_list);
849 /* Erase the minibuffer we were using at this level. */
851 ptrdiff_t count = SPECPDL_INDEX ();
852 /* Prevent error in erase-buffer. */
853 specbind (Qinhibit_read_only, Qt);
854 specbind (Qinhibit_modification_hooks, Qt);
855 old_deactivate_mark = Vdeactivate_mark;
856 Ferase_buffer ();
857 Vdeactivate_mark = old_deactivate_mark;
858 unbind_to (count, Qnil);
861 /* When we get to the outmost level, make sure we resize the
862 mini-window back to its normal size. */
863 if (minibuf_level == 0)
864 resize_mini_window (XWINDOW (window), 0);
866 /* In case the previous minibuffer displayed in this miniwindow is
867 dead, we may keep displaying this buffer (tho it's inactive), so reset it,
868 to make sure we don't leave around bindings and stuff which only
869 made sense during the read_minibuf invocation. */
870 call0 (intern ("minibuffer-inactive-mode"));
874 DEFUN ("read-from-minibuffer", Fread_from_minibuffer,
875 Sread_from_minibuffer, 1, 7, 0,
876 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
877 The optional second arg INITIAL-CONTENTS is an obsolete alternative to
878 DEFAULT-VALUE. It normally should be nil in new code, except when
879 HIST is a cons. It is discussed in more detail below.
881 Third arg KEYMAP is a keymap to use whilst reading;
882 if omitted or nil, the default is `minibuffer-local-map'.
884 If fourth arg READ is non-nil, interpret the result as a Lisp object
885 and return that object:
886 in other words, do `(car (read-from-string INPUT-STRING))'
888 Fifth arg HIST, if non-nil, specifies a history list and optionally
889 the initial position in the list. It can be a symbol, which is the
890 history list variable to use, or a cons cell (HISTVAR . HISTPOS).
891 In that case, HISTVAR is the history list variable to use, and
892 HISTPOS is the initial position for use by the minibuffer history
893 commands. For consistency, you should also specify that element of
894 the history as the value of INITIAL-CONTENTS. Positions are counted
895 starting from 1 at the beginning of the list.
897 Sixth arg DEFAULT-VALUE, if non-nil, should be a string, which is used
898 as the default to `read' if READ is non-nil and the user enters
899 empty input. But if READ is nil, this function does _not_ return
900 DEFAULT-VALUE for empty input! Instead, it returns the empty string.
902 Whatever the value of READ, DEFAULT-VALUE is made available via the
903 minibuffer history commands. DEFAULT-VALUE can also be a list of
904 strings, in which case all the strings are available in the history,
905 and the first string is the default to `read' if READ is non-nil.
907 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
908 the current input method and the setting of `enable-multibyte-characters'.
910 If the variable `minibuffer-allow-text-properties' is non-nil,
911 then the string which is returned includes whatever text properties
912 were present in the minibuffer. Otherwise the value has no text properties.
914 The remainder of this documentation string describes the
915 INITIAL-CONTENTS argument in more detail. It is only relevant when
916 studying existing code, or when HIST is a cons. If non-nil,
917 INITIAL-CONTENTS is a string to be inserted into the minibuffer before
918 reading input. Normally, point is put at the end of that string.
919 However, if INITIAL-CONTENTS is \(STRING . POSITION), the initial
920 input is STRING, but point is placed at _one-indexed_ position
921 POSITION in the minibuffer. Any integer value less than or equal to
922 one puts point at the beginning of the string. *Note* that this
923 behavior differs from the way such arguments are used in `completing-read'
924 and some related functions, which use zero-indexing for POSITION. */)
925 (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)
927 Lisp_Object histvar, histpos, val;
929 CHECK_STRING (prompt);
930 if (NILP (keymap))
931 keymap = Vminibuffer_local_map;
932 else
933 keymap = get_keymap (keymap, 1, 0);
935 if (SYMBOLP (hist))
937 histvar = hist;
938 histpos = Qnil;
940 else
942 histvar = Fcar_safe (hist);
943 histpos = Fcdr_safe (hist);
945 if (NILP (histvar))
946 histvar = Qminibuffer_history;
947 if (NILP (histpos))
948 XSETFASTINT (histpos, 0);
950 val = read_minibuf (keymap, initial_contents, prompt,
951 !NILP (read),
952 histvar, histpos, default_value,
953 minibuffer_allow_text_properties,
954 !NILP (inherit_input_method));
955 return val;
958 /* Functions that use the minibuffer to read various things. */
960 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
961 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
962 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
963 This argument has been superseded by DEFAULT-VALUE and should normally be nil
964 in new code. It behaves as INITIAL-CONTENTS in `read-from-minibuffer' (which
965 see).
966 The third arg HISTORY, if non-nil, specifies a history list
967 and optionally the initial position in the list.
968 See `read-from-minibuffer' for details of HISTORY argument.
969 Fourth arg DEFAULT-VALUE is the default value or the list of default values.
970 If non-nil, it is used for history commands, and as the value (or the first
971 element of the list of default values) to return if the user enters the
972 empty string.
973 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
974 the current input method and the setting of `enable-multibyte-characters'. */)
975 (Lisp_Object prompt, Lisp_Object initial_input, Lisp_Object history, Lisp_Object default_value, Lisp_Object inherit_input_method)
977 Lisp_Object val;
978 ptrdiff_t count = SPECPDL_INDEX ();
980 /* Just in case we're in a recursive minibuffer, make it clear that the
981 previous minibuffer's completion table does not apply to the new
982 minibuffer.
983 FIXME: `minibuffer-completion-table' should be buffer-local instead. */
984 specbind (Qminibuffer_completion_table, Qnil);
986 val = Fread_from_minibuffer (prompt, initial_input, Qnil,
987 Qnil, history, default_value,
988 inherit_input_method);
989 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
990 val = CONSP (default_value) ? XCAR (default_value) : default_value;
991 return unbind_to (count, val);
994 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
995 doc: /* Read a string from the terminal, not allowing blanks.
996 Prompt with PROMPT. Whitespace terminates the input. If INITIAL is
997 non-nil, it should be a string, which is used as initial input, with
998 point positioned at the end, so that SPACE will accept the input.
999 \(Actually, INITIAL can also be a cons of a string and an integer.
1000 Such values are treated as in `read-from-minibuffer', but are normally
1001 not useful in this function.)
1002 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1003 the current input method and the setting of`enable-multibyte-characters'. */)
1004 (Lisp_Object prompt, Lisp_Object initial, Lisp_Object inherit_input_method)
1006 CHECK_STRING (prompt);
1007 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt,
1008 0, Qminibuffer_history, make_number (0), Qnil, 0,
1009 !NILP (inherit_input_method));
1012 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
1013 doc: /* Read the name of a command and return as a symbol.
1014 Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1015 if it is a list. */)
1016 (Lisp_Object prompt, Lisp_Object default_value)
1018 Lisp_Object name, default_string;
1020 if (NILP (default_value))
1021 default_string = Qnil;
1022 else if (SYMBOLP (default_value))
1023 default_string = SYMBOL_NAME (default_value);
1024 else
1025 default_string = default_value;
1027 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1028 Qnil, Qnil, default_string, Qnil);
1029 if (NILP (name))
1030 return name;
1031 return Fintern (name, Qnil);
1034 #ifdef NOTDEF
1035 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
1036 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
1037 Prompt with PROMPT. */)
1038 (Lisp_Object prompt)
1040 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
1041 Qnil);
1043 #endif /* NOTDEF */
1045 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
1046 doc: /* Read the name of a user option and return it as a symbol.
1047 Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element
1048 if it is a list.
1049 A user option, or customizable variable, is one for which
1050 `custom-variable-p' returns non-nil. */)
1051 (Lisp_Object prompt, Lisp_Object default_value)
1053 Lisp_Object name, default_string;
1055 if (NILP (default_value))
1056 default_string = Qnil;
1057 else if (SYMBOLP (default_value))
1058 default_string = SYMBOL_NAME (default_value);
1059 else
1060 default_string = default_value;
1062 name = Fcompleting_read (prompt, Vobarray,
1063 Qcustom_variable_p, Qt,
1064 Qnil, Qnil, default_string, Qnil);
1065 if (NILP (name))
1066 return name;
1067 return Fintern (name, Qnil);
1070 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 4, 0,
1071 doc: /* Read the name of a buffer and return as a string.
1072 Prompt with PROMPT.
1073 Optional second arg DEF is value to return if user enters an empty line.
1074 If DEF is a list of default values, return its first element.
1075 Optional third arg REQUIRE-MATCH determines whether non-existing
1076 buffer names are allowed. It has the same meaning as the
1077 REQUIRE-MATCH argument of `completing-read'.
1078 The argument PROMPT should be a string ending with a colon and a space.
1079 If `read-buffer-completion-ignore-case' is non-nil, completion ignores
1080 case while reading the buffer name.
1081 If `read-buffer-function' is non-nil, this works by calling it as a
1082 function, instead of the usual behavior.
1083 Optional arg PREDICATE if non-nil is a function limiting the buffers that can
1084 be considered. */)
1085 (Lisp_Object prompt, Lisp_Object def, Lisp_Object require_match,
1086 Lisp_Object predicate)
1088 Lisp_Object result;
1089 char *s;
1090 ptrdiff_t len;
1091 ptrdiff_t count = SPECPDL_INDEX ();
1093 if (BUFFERP (def))
1094 def = BVAR (XBUFFER (def), name);
1096 specbind (Qcompletion_ignore_case,
1097 read_buffer_completion_ignore_case ? Qt : Qnil);
1099 if (NILP (Vread_buffer_function))
1101 if (!NILP (def))
1103 /* A default value was provided: we must change PROMPT,
1104 editing the default value in before the colon. To achieve
1105 this, we replace PROMPT with a substring that doesn't
1106 contain the terminal space and colon (if present). They
1107 are then added back using Fformat. */
1109 if (STRINGP (prompt))
1111 s = SSDATA (prompt);
1112 len = SBYTES (prompt);
1113 if (len >= 2 && s[len - 2] == ':' && s[len - 1] == ' ')
1114 len = len - 2;
1115 else if (len >= 1 && (s[len - 1] == ':' || s[len - 1] == ' '))
1116 len--;
1118 prompt = make_specified_string (s, -1, len,
1119 STRING_MULTIBYTE (prompt));
1122 AUTO_STRING (format, "%s (default %s): ");
1123 prompt = CALLN (Fformat, format, prompt,
1124 CONSP (def) ? XCAR (def) : def);
1127 result = Fcompleting_read (prompt, intern ("internal-complete-buffer"),
1128 predicate, require_match, Qnil,
1129 Qbuffer_name_history, def, Qnil);
1131 else
1132 result = (NILP (predicate)
1133 /* Partial backward compatibility for older read_buffer_functions
1134 which don't expect a `predicate' argument. */
1135 ? call3 (Vread_buffer_function, prompt, def, require_match)
1136 : call4 (Vread_buffer_function, prompt, def, require_match,
1137 predicate));
1138 return unbind_to (count, result);
1141 static Lisp_Object
1142 minibuf_conform_representation (Lisp_Object string, Lisp_Object basis)
1144 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1145 return string;
1147 if (STRING_MULTIBYTE (string))
1148 return Fstring_make_unibyte (string);
1149 else
1150 return Fstring_make_multibyte (string);
1153 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1154 doc: /* Return common substring of all completions of STRING in COLLECTION.
1155 Test each possible completion specified by COLLECTION
1156 to see if it begins with STRING. The possible completions may be
1157 strings or symbols. Symbols are converted to strings before testing,
1158 see `symbol-name'.
1159 All that match STRING are compared together; the longest initial sequence
1160 common to all these matches is the return value.
1161 If there is no match at all, the return value is nil.
1162 For a unique match which is exact, the return value is t.
1164 If COLLECTION is an alist, the keys (cars of elements) are the
1165 possible completions. If an element is not a cons cell, then the
1166 element itself is the possible completion.
1167 If COLLECTION is a hash-table, all the keys that are strings or symbols
1168 are the possible completions.
1169 If COLLECTION is an obarray, the names of all symbols in the obarray
1170 are the possible completions.
1172 COLLECTION can also be a function to do the completion itself.
1173 It receives three arguments: the values STRING, PREDICATE and nil.
1174 Whatever it returns becomes the value of `try-completion'.
1176 If optional third argument PREDICATE is non-nil,
1177 it is used to test each possible match.
1178 The match is a candidate only if PREDICATE returns non-nil.
1179 The argument given to PREDICATE is the alist element
1180 or the symbol from the obarray. If COLLECTION is a hash-table,
1181 predicate is called with two arguments: the key and the value.
1182 Additionally to this predicate, `completion-regexp-list'
1183 is used to further constrain the set of candidates. */)
1184 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
1186 Lisp_Object bestmatch, tail, elt, eltstring;
1187 /* Size in bytes of BESTMATCH. */
1188 ptrdiff_t bestmatchsize = 0;
1189 /* These are in bytes, too. */
1190 ptrdiff_t compare, matchsize;
1191 enum { function_table, list_table, obarray_table, hash_table}
1192 type = (HASH_TABLE_P (collection) ? hash_table
1193 : VECTORP (collection) ? obarray_table
1194 : ((NILP (collection)
1195 || (CONSP (collection) && !FUNCTIONP (collection)))
1196 ? list_table : function_table));
1197 ptrdiff_t idx = 0, obsize = 0;
1198 int matchcount = 0;
1199 ptrdiff_t bindcount = -1;
1200 Lisp_Object bucket, zero, end, tem;
1202 CHECK_STRING (string);
1203 if (type == function_table)
1204 return call3 (collection, string, predicate, Qnil);
1206 bestmatch = bucket = Qnil;
1207 zero = make_number (0);
1209 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1210 tail = collection;
1211 if (type == obarray_table)
1213 collection = check_obarray (collection);
1214 obsize = ASIZE (collection);
1215 bucket = AREF (collection, idx);
1218 while (1)
1220 /* Get the next element of the alist, obarray, or hash-table. */
1221 /* Exit the loop if the elements are all used up. */
1222 /* elt gets the alist element or symbol.
1223 eltstring gets the name to check as a completion. */
1225 if (type == list_table)
1227 if (!CONSP (tail))
1228 break;
1229 elt = XCAR (tail);
1230 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1231 tail = XCDR (tail);
1233 else if (type == obarray_table)
1235 if (!EQ (bucket, zero))
1237 if (!SYMBOLP (bucket))
1238 error ("Bad data in guts of obarray");
1239 elt = bucket;
1240 eltstring = elt;
1241 if (XSYMBOL (bucket)->next)
1242 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1243 else
1244 XSETFASTINT (bucket, 0);
1246 else if (++idx >= obsize)
1247 break;
1248 else
1250 bucket = AREF (collection, idx);
1251 continue;
1254 else /* if (type == hash_table) */
1256 while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1257 && NILP (HASH_HASH (XHASH_TABLE (collection), idx)))
1258 idx++;
1259 if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
1260 break;
1261 else
1262 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), idx++);
1265 /* Is this element a possible completion? */
1267 if (SYMBOLP (eltstring))
1268 eltstring = Fsymbol_name (eltstring);
1270 if (STRINGP (eltstring)
1271 && SCHARS (string) <= SCHARS (eltstring)
1272 && (tem = Fcompare_strings (eltstring, zero,
1273 make_number (SCHARS (string)),
1274 string, zero, Qnil,
1275 completion_ignore_case ? Qt : Qnil),
1276 EQ (Qt, tem)))
1278 /* Yes. */
1279 Lisp_Object regexps;
1281 /* Ignore this element if it fails to match all the regexps. */
1283 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1284 regexps = XCDR (regexps))
1286 if (bindcount < 0) {
1287 bindcount = SPECPDL_INDEX ();
1288 specbind (Qcase_fold_search,
1289 completion_ignore_case ? Qt : Qnil);
1291 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1292 if (NILP (tem))
1293 break;
1295 if (CONSP (regexps))
1296 continue;
1299 /* Ignore this element if there is a predicate
1300 and the predicate doesn't like it. */
1302 if (!NILP (predicate))
1304 if (EQ (predicate, Qcommandp))
1305 tem = Fcommandp (elt, Qnil);
1306 else
1308 if (bindcount >= 0)
1310 unbind_to (bindcount, Qnil);
1311 bindcount = -1;
1313 tem = (type == hash_table
1314 ? call2 (predicate, elt,
1315 HASH_VALUE (XHASH_TABLE (collection),
1316 idx - 1))
1317 : call1 (predicate, elt));
1319 if (NILP (tem)) continue;
1322 /* Update computation of how much all possible completions match */
1324 if (NILP (bestmatch))
1326 matchcount = 1;
1327 bestmatch = eltstring;
1328 bestmatchsize = SCHARS (eltstring);
1330 else
1332 compare = min (bestmatchsize, SCHARS (eltstring));
1333 tem = Fcompare_strings (bestmatch, zero,
1334 make_number (compare),
1335 eltstring, zero,
1336 make_number (compare),
1337 completion_ignore_case ? Qt : Qnil);
1338 matchsize = EQ (tem, Qt) ? compare : eabs (XINT (tem)) - 1;
1340 if (completion_ignore_case)
1342 /* If this is an exact match except for case,
1343 use it as the best match rather than one that is not an
1344 exact match. This way, we get the case pattern
1345 of the actual match. */
1346 if ((matchsize == SCHARS (eltstring)
1347 && matchsize < SCHARS (bestmatch))
1349 /* If there is more than one exact match ignoring case,
1350 and one of them is exact including case,
1351 prefer that one. */
1352 /* If there is no exact match ignoring case,
1353 prefer a match that does not change the case
1354 of the input. */
1355 ((matchsize == SCHARS (eltstring))
1357 (matchsize == SCHARS (bestmatch))
1358 && (tem = Fcompare_strings (eltstring, zero,
1359 make_number (SCHARS (string)),
1360 string, zero,
1361 Qnil,
1362 Qnil),
1363 EQ (Qt, tem))
1364 && (tem = Fcompare_strings (bestmatch, zero,
1365 make_number (SCHARS (string)),
1366 string, zero,
1367 Qnil,
1368 Qnil),
1369 ! EQ (Qt, tem))))
1370 bestmatch = eltstring;
1372 if (bestmatchsize != SCHARS (eltstring)
1373 || bestmatchsize != matchsize)
1374 /* Don't count the same string multiple times. */
1375 matchcount += matchcount <= 1;
1376 bestmatchsize = matchsize;
1377 if (matchsize <= SCHARS (string)
1378 /* If completion-ignore-case is non-nil, don't
1379 short-circuit because we want to find the best
1380 possible match *including* case differences. */
1381 && !completion_ignore_case
1382 && matchcount > 1)
1383 /* No need to look any further. */
1384 break;
1389 if (bindcount >= 0) {
1390 unbind_to (bindcount, Qnil);
1391 bindcount = -1;
1394 if (NILP (bestmatch))
1395 return Qnil; /* No completions found. */
1396 /* If we are ignoring case, and there is no exact match,
1397 and no additional text was supplied,
1398 don't change the case of what the user typed. */
1399 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1400 && SCHARS (bestmatch) > bestmatchsize)
1401 return minibuf_conform_representation (string, bestmatch);
1403 /* Return t if the supplied string is an exact match (counting case);
1404 it does not require any change to be made. */
1405 if (matchcount == 1 && !NILP (Fequal (bestmatch, string)))
1406 return Qt;
1408 XSETFASTINT (zero, 0); /* Else extract the part in which */
1409 XSETFASTINT (end, bestmatchsize); /* all completions agree. */
1410 return Fsubstring (bestmatch, zero, end);
1413 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1414 doc: /* Search for partial matches to STRING in COLLECTION.
1415 Test each of the possible completions specified by COLLECTION
1416 to see if it begins with STRING. The possible completions may be
1417 strings or symbols. Symbols are converted to strings before testing,
1418 see `symbol-name'.
1419 The value is a list of all the possible completions that match STRING.
1421 If COLLECTION is an alist, the keys (cars of elements) are the
1422 possible completions. If an element is not a cons cell, then the
1423 element itself is the possible completion.
1424 If COLLECTION is a hash-table, all the keys that are strings or symbols
1425 are the possible completions.
1426 If COLLECTION is an obarray, the names of all symbols in the obarray
1427 are the possible completions.
1429 COLLECTION can also be a function to do the completion itself.
1430 It receives three arguments: the values STRING, PREDICATE and t.
1431 Whatever it returns becomes the value of `all-completions'.
1433 If optional third argument PREDICATE is non-nil,
1434 it is used to test each possible match.
1435 The match is a candidate only if PREDICATE returns non-nil.
1436 The argument given to PREDICATE is the alist element
1437 or the symbol from the obarray. If COLLECTION is a hash-table,
1438 predicate is called with two arguments: the key and the value.
1439 Additionally to this predicate, `completion-regexp-list'
1440 is used to further constrain the set of candidates.
1442 An obsolete optional fourth argument HIDE-SPACES is still accepted for
1443 backward compatibility. If non-nil, strings in COLLECTION that start
1444 with a space are ignored unless STRING itself starts with a space. */)
1445 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate, Lisp_Object hide_spaces)
1447 Lisp_Object tail, elt, eltstring;
1448 Lisp_Object allmatches;
1449 int type = HASH_TABLE_P (collection) ? 3
1450 : VECTORP (collection) ? 2
1451 : NILP (collection) || (CONSP (collection) && !FUNCTIONP (collection));
1452 ptrdiff_t idx = 0, obsize = 0;
1453 ptrdiff_t bindcount = -1;
1454 Lisp_Object bucket, tem, zero;
1456 CHECK_STRING (string);
1457 if (type == 0)
1458 return call3 (collection, string, predicate, Qt);
1459 allmatches = bucket = Qnil;
1460 zero = make_number (0);
1462 /* If COLLECTION is not a list, set TAIL just for gc pro. */
1463 tail = collection;
1464 if (type == 2)
1466 collection = check_obarray (collection);
1467 obsize = ASIZE (collection);
1468 bucket = AREF (collection, idx);
1471 while (1)
1473 /* Get the next element of the alist, obarray, or hash-table. */
1474 /* Exit the loop if the elements are all used up. */
1475 /* elt gets the alist element or symbol.
1476 eltstring gets the name to check as a completion. */
1478 if (type == 1)
1480 if (!CONSP (tail))
1481 break;
1482 elt = XCAR (tail);
1483 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1484 tail = XCDR (tail);
1486 else if (type == 2)
1488 if (!EQ (bucket, zero))
1490 if (!SYMBOLP (bucket))
1491 error ("Bad data in guts of obarray");
1492 elt = bucket;
1493 eltstring = elt;
1494 if (XSYMBOL (bucket)->next)
1495 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1496 else
1497 XSETFASTINT (bucket, 0);
1499 else if (++idx >= obsize)
1500 break;
1501 else
1503 bucket = AREF (collection, idx);
1504 continue;
1507 else /* if (type == 3) */
1509 while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
1510 && NILP (HASH_HASH (XHASH_TABLE (collection), idx)))
1511 idx++;
1512 if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
1513 break;
1514 else
1515 elt = eltstring = HASH_KEY (XHASH_TABLE (collection), idx++);
1518 /* Is this element a possible completion? */
1520 if (SYMBOLP (eltstring))
1521 eltstring = Fsymbol_name (eltstring);
1523 if (STRINGP (eltstring)
1524 && SCHARS (string) <= SCHARS (eltstring)
1525 /* If HIDE_SPACES, reject alternatives that start with space
1526 unless the input starts with space. */
1527 && (NILP (hide_spaces)
1528 || (SBYTES (string) > 0
1529 && SREF (string, 0) == ' ')
1530 || SREF (eltstring, 0) != ' ')
1531 && (tem = Fcompare_strings (eltstring, zero,
1532 make_number (SCHARS (string)),
1533 string, zero,
1534 make_number (SCHARS (string)),
1535 completion_ignore_case ? Qt : Qnil),
1536 EQ (Qt, tem)))
1538 /* Yes. */
1539 Lisp_Object regexps;
1541 /* Ignore this element if it fails to match all the regexps. */
1543 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1544 regexps = XCDR (regexps))
1546 if (bindcount < 0) {
1547 bindcount = SPECPDL_INDEX ();
1548 specbind (Qcase_fold_search,
1549 completion_ignore_case ? Qt : Qnil);
1551 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1552 if (NILP (tem))
1553 break;
1555 if (CONSP (regexps))
1556 continue;
1559 /* Ignore this element if there is a predicate
1560 and the predicate doesn't like it. */
1562 if (!NILP (predicate))
1564 if (EQ (predicate, Qcommandp))
1565 tem = Fcommandp (elt, Qnil);
1566 else
1568 if (bindcount >= 0) {
1569 unbind_to (bindcount, Qnil);
1570 bindcount = -1;
1572 tem = type == 3
1573 ? call2 (predicate, elt,
1574 HASH_VALUE (XHASH_TABLE (collection), idx - 1))
1575 : call1 (predicate, elt);
1577 if (NILP (tem)) continue;
1579 /* Ok => put it on the list. */
1580 allmatches = Fcons (eltstring, allmatches);
1584 if (bindcount >= 0) {
1585 unbind_to (bindcount, Qnil);
1586 bindcount = -1;
1589 return Fnreverse (allmatches);
1592 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1593 doc: /* Read a string in the minibuffer, with completion.
1594 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1595 COLLECTION can be a list of strings, an alist, an obarray or a hash table.
1596 COLLECTION can also be a function to do the completion itself.
1597 PREDICATE limits completion to a subset of COLLECTION.
1598 See `try-completion' and `all-completions' for more details
1599 on completion, COLLECTION, and PREDICATE.
1601 REQUIRE-MATCH can take the following values:
1602 - t means that the user is not allowed to exit unless
1603 the input is (or completes to) an element of COLLECTION or is null.
1604 - nil means that the user can exit with any input.
1605 - `confirm' means that the user can exit with any input, but she needs
1606 to confirm her choice if the input is not an element of COLLECTION.
1607 - `confirm-after-completion' means that the user can exit with any
1608 input, but she needs to confirm her choice if she called
1609 `minibuffer-complete' right before `minibuffer-complete-and-exit'
1610 and the input is not an element of COLLECTION.
1611 - anything else behaves like t except that typing RET does not exit if it
1612 does non-null completion.
1614 If the input is null, `completing-read' returns DEF, or the first element
1615 of the list of default values, or an empty string if DEF is nil,
1616 regardless of the value of REQUIRE-MATCH.
1618 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
1619 with point positioned at the end.
1620 If it is (STRING . POSITION), the initial input is STRING, but point
1621 is placed at _zero-indexed_ position POSITION in STRING. (*Note*
1622 that this is different from `read-from-minibuffer' and related
1623 functions, which use one-indexing for POSITION.) This feature is
1624 deprecated--it is best to pass nil for INITIAL-INPUT and supply the
1625 default value DEF instead. The user can yank the default value into
1626 the minibuffer easily using \\<minibuffer-local-map>\\[next-history-element].
1628 HIST, if non-nil, specifies a history list and optionally the initial
1629 position in the list. It can be a symbol, which is the history list
1630 variable to use, or it can be a cons cell (HISTVAR . HISTPOS). In
1631 that case, HISTVAR is the history list variable to use, and HISTPOS
1632 is the initial position (the position in the list used by the
1633 minibuffer history commands). For consistency, you should also
1634 specify that element of the history as the value of
1635 INITIAL-INPUT. (This is the only case in which you should use
1636 INITIAL-INPUT instead of DEF.) Positions are counted starting from
1637 1 at the beginning of the list. The variable `history-length'
1638 controls the maximum length of a history list.
1640 DEF, if non-nil, is the default value or the list of default values.
1642 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
1643 the current input method and the setting of `enable-multibyte-characters'.
1645 Completion ignores case if the ambient value of
1646 `completion-ignore-case' is non-nil.
1648 See also `completing-read-function'. */)
1649 (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)
1651 return CALLN (Ffuncall,
1652 Fsymbol_value (intern ("completing-read-function")),
1653 prompt, collection, predicate, require_match, initial_input,
1654 hist, def, inherit_input_method);
1657 /* Test whether TXT is an exact completion. */
1658 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1659 doc: /* Return non-nil if STRING is a valid completion.
1660 Takes the same arguments as `all-completions' and `try-completion'.
1661 If COLLECTION is a function, it is called with three arguments:
1662 the values STRING, PREDICATE and `lambda'. */)
1663 (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
1665 Lisp_Object regexps, tail, tem = Qnil;
1666 ptrdiff_t i = 0;
1668 CHECK_STRING (string);
1670 if (NILP (collection) || (CONSP (collection) && !FUNCTIONP (collection)))
1672 tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
1673 if (NILP (tem))
1674 return Qnil;
1676 else if (VECTORP (collection))
1678 /* Bypass intern-soft as that loses for nil. */
1679 tem = oblookup (collection,
1680 SSDATA (string),
1681 SCHARS (string),
1682 SBYTES (string));
1683 if (!SYMBOLP (tem))
1685 if (STRING_MULTIBYTE (string))
1686 string = Fstring_make_unibyte (string);
1687 else
1688 string = Fstring_make_multibyte (string);
1690 tem = oblookup (collection,
1691 SSDATA (string),
1692 SCHARS (string),
1693 SBYTES (string));
1696 if (completion_ignore_case && !SYMBOLP (tem))
1698 for (i = ASIZE (collection) - 1; i >= 0; i--)
1700 tail = AREF (collection, i);
1701 if (SYMBOLP (tail))
1702 while (1)
1704 if (EQ (Fcompare_strings (string, make_number (0), Qnil,
1705 Fsymbol_name (tail),
1706 make_number (0) , Qnil, Qt),
1707 Qt))
1709 tem = tail;
1710 break;
1712 if (XSYMBOL (tail)->next == 0)
1713 break;
1714 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1719 if (!SYMBOLP (tem))
1720 return Qnil;
1722 else if (HASH_TABLE_P (collection))
1724 struct Lisp_Hash_Table *h = XHASH_TABLE (collection);
1725 Lisp_Object key = Qnil;
1726 i = hash_lookup (h, string, NULL);
1727 if (i >= 0)
1728 tem = HASH_KEY (h, i);
1729 else
1730 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1731 if (!NILP (HASH_HASH (h, i))
1732 && (key = HASH_KEY (h, i),
1733 SYMBOLP (key) ? key = Fsymbol_name (key) : key,
1734 STRINGP (key))
1735 && EQ (Fcompare_strings (string, make_number (0), Qnil,
1736 key, make_number (0) , Qnil,
1737 completion_ignore_case ? Qt : Qnil),
1738 Qt))
1740 tem = key;
1741 break;
1743 if (!STRINGP (tem))
1744 return Qnil;
1746 else
1747 return call3 (collection, string, predicate, Qlambda);
1749 /* Reject this element if it fails to match all the regexps. */
1750 if (CONSP (Vcompletion_regexp_list))
1752 ptrdiff_t count = SPECPDL_INDEX ();
1753 specbind (Qcase_fold_search, completion_ignore_case ? Qt : Qnil);
1754 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1755 regexps = XCDR (regexps))
1757 if (NILP (Fstring_match (XCAR (regexps),
1758 SYMBOLP (tem) ? string : tem,
1759 Qnil)))
1760 return unbind_to (count, Qnil);
1762 unbind_to (count, Qnil);
1765 /* Finally, check the predicate. */
1766 if (!NILP (predicate))
1768 return HASH_TABLE_P (collection)
1769 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (collection), i))
1770 : call1 (predicate, tem);
1772 else
1773 return Qt;
1776 DEFUN ("internal-complete-buffer", Finternal_complete_buffer, Sinternal_complete_buffer, 3, 3, 0,
1777 doc: /* Perform completion on buffer names.
1778 STRING and PREDICATE have the same meanings as in `try-completion',
1779 `all-completions', and `test-completion'.
1781 If FLAG is nil, invoke `try-completion'; if it is t, invoke
1782 `all-completions'; otherwise invoke `test-completion'. */)
1783 (Lisp_Object string, Lisp_Object predicate, Lisp_Object flag)
1785 if (NILP (flag))
1786 return Ftry_completion (string, Vbuffer_alist, predicate);
1787 else if (EQ (flag, Qt))
1789 Lisp_Object res = Fall_completions (string, Vbuffer_alist, predicate, Qnil);
1790 if (SCHARS (string) > 0)
1791 return res;
1792 else
1793 { /* Strip out internal buffers. */
1794 Lisp_Object bufs = res;
1795 /* First, look for a non-internal buffer in `res'. */
1796 while (CONSP (bufs) && SREF (XCAR (bufs), 0) == ' ')
1797 bufs = XCDR (bufs);
1798 if (NILP (bufs))
1799 return (EQ (Flength (res), Flength (Vbuffer_alist))
1800 /* If all bufs are internal don't strip them out. */
1801 ? res : bufs);
1802 res = bufs;
1803 while (CONSP (XCDR (bufs)))
1804 if (SREF (XCAR (XCDR (bufs)), 0) == ' ')
1805 XSETCDR (bufs, XCDR (XCDR (bufs)));
1806 else
1807 bufs = XCDR (bufs);
1808 return res;
1811 else if (EQ (flag, Qlambda))
1812 return Ftest_completion (string, Vbuffer_alist, predicate);
1813 else if (EQ (flag, Qmetadata))
1814 return list2 (Qmetadata, Fcons (Qcategory, Qbuffer));
1815 else
1816 return Qnil;
1819 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1821 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
1822 doc: /* Like `assoc' but specifically for strings (and symbols).
1824 This returns the first element of LIST whose car matches the string or
1825 symbol KEY, or nil if no match exists. When performing the
1826 comparison, symbols are first converted to strings, and unibyte
1827 strings to multibyte. If the optional arg CASE-FOLD is non-nil, case
1828 is ignored.
1830 Unlike `assoc', KEY can also match an entry in LIST consisting of a
1831 single string, rather than a cons cell whose car is a string. */)
1832 (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold)
1834 register Lisp_Object tail;
1836 if (SYMBOLP (key))
1837 key = Fsymbol_name (key);
1839 for (tail = list; CONSP (tail); tail = XCDR (tail))
1841 register Lisp_Object elt, tem, thiscar;
1842 elt = XCAR (tail);
1843 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1844 if (SYMBOLP (thiscar))
1845 thiscar = Fsymbol_name (thiscar);
1846 else if (!STRINGP (thiscar))
1847 continue;
1848 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1849 key, make_number (0), Qnil,
1850 case_fold);
1851 if (EQ (tem, Qt))
1852 return elt;
1853 QUIT;
1855 return Qnil;
1859 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
1860 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
1861 (void)
1863 return make_number (minibuf_level);
1866 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
1867 doc: /* Return the prompt string of the currently-active minibuffer.
1868 If no minibuffer is active, return nil. */)
1869 (void)
1871 return Fcopy_sequence (minibuf_prompt);
1875 void
1876 init_minibuf_once (void)
1878 Vminibuffer_list = Qnil;
1879 staticpro (&Vminibuffer_list);
1882 void
1883 syms_of_minibuf (void)
1885 minibuf_level = 0;
1886 minibuf_prompt = Qnil;
1887 staticpro (&minibuf_prompt);
1889 minibuf_save_list = Qnil;
1890 staticpro (&minibuf_save_list);
1892 DEFSYM (Qcompletion_ignore_case, "completion-ignore-case");
1893 DEFSYM (Qminibuffer_default, "minibuffer-default");
1894 Fset (Qminibuffer_default, Qnil);
1896 DEFSYM (Qminibuffer_completion_table, "minibuffer-completion-table");
1898 staticpro (&last_minibuf_string);
1899 last_minibuf_string = Qnil;
1901 DEFSYM (Qminibuffer_history, "minibuffer-history");
1902 DEFSYM (Qbuffer_name_history, "buffer-name-history");
1903 Fset (Qbuffer_name_history, Qnil);
1905 DEFSYM (Qcustom_variable_p, "custom-variable-p");
1907 /* Normal hooks for entry to and exit from minibuffer. */
1908 DEFSYM (Qminibuffer_setup_hook, "minibuffer-setup-hook");
1909 DEFSYM (Qminibuffer_exit_hook, "minibuffer-exit-hook");
1911 /* The maximum length of a minibuffer history. */
1912 DEFSYM (Qhistory_length, "history-length");
1914 DEFSYM (Qcurrent_input_method, "current-input-method");
1915 DEFSYM (Qactivate_input_method, "activate-input-method");
1916 DEFSYM (Qcase_fold_search, "case-fold-search");
1917 DEFSYM (Qmetadata, "metadata");
1919 DEFVAR_LISP ("read-expression-history", Vread_expression_history,
1920 doc: /* A history list for arguments that are Lisp expressions to evaluate.
1921 For example, `eval-expression' uses this. */);
1922 Vread_expression_history = Qnil;
1924 DEFVAR_LISP ("read-buffer-function", Vread_buffer_function,
1925 doc: /* If this is non-nil, `read-buffer' does its work by calling this function.
1926 The function is called with the arguments passed to `read-buffer'. */);
1927 Vread_buffer_function = Qnil;
1929 DEFVAR_BOOL ("read-buffer-completion-ignore-case",
1930 read_buffer_completion_ignore_case,
1931 doc: /* Non-nil means completion ignores case when reading a buffer name. */);
1932 read_buffer_completion_ignore_case = 0;
1934 DEFVAR_LISP ("minibuffer-setup-hook", Vminibuffer_setup_hook,
1935 doc: /* Normal hook run just after entry to minibuffer. */);
1936 Vminibuffer_setup_hook = Qnil;
1938 DEFVAR_LISP ("minibuffer-exit-hook", Vminibuffer_exit_hook,
1939 doc: /* Normal hook run just after exit from minibuffer. */);
1940 Vminibuffer_exit_hook = Qnil;
1942 DEFVAR_LISP ("history-length", Vhistory_length,
1943 doc: /* Maximum length of history lists before truncation takes place.
1944 A number means truncate to that length; truncation deletes old
1945 elements, and is done just after inserting a new element.
1946 A value of t means no truncation.
1948 This variable only affects history lists that don't specify their own
1949 maximum lengths. Setting the `history-length' property of a history
1950 variable overrides this default. */);
1951 XSETFASTINT (Vhistory_length, 100);
1953 DEFVAR_BOOL ("history-delete-duplicates", history_delete_duplicates,
1954 doc: /* Non-nil means to delete duplicates in history.
1955 If set to t when adding a new history element, all previous identical
1956 elements are deleted from the history list. */);
1957 history_delete_duplicates = 0;
1959 DEFVAR_LISP ("history-add-new-input", Vhistory_add_new_input,
1960 doc: /* Non-nil means to add new elements in history.
1961 If set to nil, minibuffer reading functions don't add new elements to the
1962 history list, so it is possible to do this afterwards by calling
1963 `add-to-history' explicitly. */);
1964 Vhistory_add_new_input = Qt;
1966 DEFVAR_BOOL ("completion-ignore-case", completion_ignore_case,
1967 doc: /* Non-nil means don't consider case significant in completion.
1968 For file-name completion, `read-file-name-completion-ignore-case'
1969 controls the behavior, rather than this variable.
1970 For buffer name completion, `read-buffer-completion-ignore-case'
1971 controls the behavior, rather than this variable. */);
1972 completion_ignore_case = 0;
1974 DEFVAR_BOOL ("enable-recursive-minibuffers", enable_recursive_minibuffers,
1975 doc: /* Non-nil means to allow minibuffer commands while in the minibuffer.
1976 This variable makes a difference whenever the minibuffer window is active. */);
1977 enable_recursive_minibuffers = 0;
1979 DEFVAR_LISP ("minibuffer-completion-table", Vminibuffer_completion_table,
1980 doc: /* Alist or obarray used for completion in the minibuffer.
1981 This becomes the ALIST argument to `try-completion' and `all-completions'.
1982 The value can also be a list of strings or a hash table.
1984 The value may alternatively be a function, which is given three arguments:
1985 STRING, the current buffer contents;
1986 PREDICATE, the predicate for filtering possible matches;
1987 CODE, which says what kind of things to do.
1988 CODE can be nil, t or `lambda':
1989 nil -- return the best completion of STRING, or nil if there is none.
1990 t -- return a list of all possible completions of STRING.
1991 lambda -- return t if STRING is a valid completion as it stands. */);
1992 Vminibuffer_completion_table = Qnil;
1994 DEFVAR_LISP ("minibuffer-completion-predicate", Vminibuffer_completion_predicate,
1995 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
1996 Vminibuffer_completion_predicate = Qnil;
1998 DEFVAR_LISP ("minibuffer-completion-confirm", Vminibuffer_completion_confirm,
1999 doc: /* Whether to demand confirmation of completion before exiting minibuffer.
2000 If nil, confirmation is not required.
2001 If the value is `confirm', the user may exit with an input that is not
2002 a valid completion alternative, but Emacs asks for confirmation.
2003 If the value is `confirm-after-completion', the user may exit with an
2004 input that is not a valid completion alternative, but Emacs asks for
2005 confirmation if the user submitted the input right after any of the
2006 completion commands listed in `minibuffer-confirm-exit-commands'. */);
2007 Vminibuffer_completion_confirm = Qnil;
2009 DEFVAR_LISP ("minibuffer-completing-file-name",
2010 Vminibuffer_completing_file_name,
2011 doc: /* Non-nil means completing file names. */);
2012 Vminibuffer_completing_file_name = Qnil;
2014 DEFVAR_LISP ("minibuffer-help-form", Vminibuffer_help_form,
2015 doc: /* Value that `help-form' takes on inside the minibuffer. */);
2016 Vminibuffer_help_form = Qnil;
2018 DEFVAR_LISP ("minibuffer-history-variable", Vminibuffer_history_variable,
2019 doc: /* History list symbol to add minibuffer values to.
2020 Each string of minibuffer input, as it appears on exit from the minibuffer,
2021 is added with
2022 (set minibuffer-history-variable
2023 (cons STRING (symbol-value minibuffer-history-variable))) */);
2024 XSETFASTINT (Vminibuffer_history_variable, 0);
2026 DEFVAR_LISP ("minibuffer-history-position", Vminibuffer_history_position,
2027 doc: /* Current position of redoing in the history list. */);
2028 Vminibuffer_history_position = Qnil;
2030 DEFVAR_BOOL ("minibuffer-auto-raise", minibuffer_auto_raise,
2031 doc: /* Non-nil means entering the minibuffer raises the minibuffer's frame.
2032 Some uses of the echo area also raise that frame (since they use it too). */);
2033 minibuffer_auto_raise = 0;
2035 DEFVAR_LISP ("completion-regexp-list", Vcompletion_regexp_list,
2036 doc: /* List of regexps that should restrict possible completions.
2037 The basic completion functions only consider a completion acceptable
2038 if it matches all regular expressions in this list, with
2039 `case-fold-search' bound to the value of `completion-ignore-case'.
2040 See Info node `(elisp)Basic Completion', for a description of these
2041 functions. */);
2042 Vcompletion_regexp_list = Qnil;
2044 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2045 minibuffer_allow_text_properties,
2046 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
2047 This also affects `read-string', but it does not affect `read-minibuffer',
2048 `read-no-blanks-input', or any of the functions that do minibuffer input
2049 with completion; they always discard text properties. */);
2050 minibuffer_allow_text_properties = 0;
2052 DEFVAR_LISP ("minibuffer-prompt-properties", Vminibuffer_prompt_properties,
2053 doc: /* Text properties that are added to minibuffer prompts.
2054 These are in addition to the basic `field' property, and stickiness
2055 properties. */);
2056 Vminibuffer_prompt_properties = list2 (Qread_only, Qt);
2058 DEFVAR_LISP ("read-hide-char", Vread_hide_char,
2059 doc: /* Whether to hide input characters in noninteractive mode.
2060 It must be a character, which will be used to mask the input
2061 characters. This variable should never be set globally. */);
2062 Vread_hide_char = Qnil;
2064 defsubr (&Sactive_minibuffer_window);
2065 defsubr (&Sset_minibuffer_window);
2066 defsubr (&Sread_from_minibuffer);
2067 defsubr (&Sread_string);
2068 defsubr (&Sread_command);
2069 defsubr (&Sread_variable);
2070 defsubr (&Sinternal_complete_buffer);
2071 defsubr (&Sread_buffer);
2072 defsubr (&Sread_no_blanks_input);
2073 defsubr (&Sminibuffer_depth);
2074 defsubr (&Sminibuffer_prompt);
2076 defsubr (&Sminibufferp);
2077 defsubr (&Sminibuffer_prompt_end);
2078 defsubr (&Sminibuffer_contents);
2079 defsubr (&Sminibuffer_contents_no_properties);
2080 defsubr (&Sminibuffer_completion_contents);
2082 defsubr (&Stry_completion);
2083 defsubr (&Sall_completions);
2084 defsubr (&Stest_completion);
2085 defsubr (&Sassoc_string);
2086 defsubr (&Scompleting_read);