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