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