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