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