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