]> code.delx.au - gnu-emacs/blob - src/minibuf.c
*** empty log message ***
[gnu-emacs] / src / minibuf.c
1 /* Minibuffer input and completion.
2 Copyright (C) 1985,86,93,94,95,96,97,98,99,2000,01,03
3 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24 #include <stdio.h>
25
26 #include "lisp.h"
27 #include "commands.h"
28 #include "buffer.h"
29 #include "character.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
38 extern int quit_char;
39
40 /* List of buffers for use as minibuffers.
41 The first element of the list is used for the outermost minibuffer
42 invocation, the next element is used for a recursive minibuffer
43 invocation, etc. The list is extended at the end as deeper
44 minibuffer recursions are encountered. */
45
46 Lisp_Object Vminibuffer_list;
47
48 /* Data to remember during recursive minibuffer invocations */
49
50 Lisp_Object minibuf_save_list;
51
52 /* Depth in minibuffer invocations. */
53
54 int minibuf_level;
55
56 /* Nonzero means display completion help for invalid input. */
57
58 Lisp_Object Vcompletion_auto_help;
59
60 /* The maximum length of a minibuffer history. */
61
62 Lisp_Object Qhistory_length, Vhistory_length;
63
64 /* Fread_minibuffer leaves the input here as a string. */
65
66 Lisp_Object last_minibuf_string;
67
68 /* Nonzero means let functions called when within a minibuffer
69 invoke recursive minibuffers (to read arguments, or whatever) */
70
71 int enable_recursive_minibuffers;
72
73 /* Nonzero means don't ignore text properties
74 in Fread_from_minibuffer. */
75
76 int minibuffer_allow_text_properties;
77
78 /* help-form is bound to this while in the minibuffer. */
79
80 Lisp_Object Vminibuffer_help_form;
81
82 /* Variable which is the history list to add minibuffer values to. */
83
84 Lisp_Object Vminibuffer_history_variable;
85
86 /* Current position in the history list (adjusted by M-n and M-p). */
87
88 Lisp_Object Vminibuffer_history_position;
89
90 /* Text properties that are added to minibuffer prompts.
91 These are in addition to the basic `field' property, and stickiness
92 properties. */
93
94 Lisp_Object Vminibuffer_prompt_properties;
95
96 Lisp_Object Qminibuffer_history, Qbuffer_name_history;
97
98 Lisp_Object Qread_file_name_internal;
99
100 /* Normal hooks for entry to and exit from minibuffer. */
101
102 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
103 Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook;
104
105 /* Function to call to read a buffer name. */
106 Lisp_Object Vread_buffer_function;
107
108 /* Nonzero means completion ignores case. */
109
110 int completion_ignore_case;
111
112 /* List of regexps that should restrict possible completions. */
113
114 Lisp_Object Vcompletion_regexp_list;
115
116 /* Nonzero means raise the minibuffer frame when the minibuffer
117 is entered. */
118
119 int minibuffer_auto_raise;
120
121 /* If last completion attempt reported "Complete but not unique"
122 then this is the string completed then; otherwise this is nil. */
123
124 static Lisp_Object last_exact_completion;
125
126 extern Lisp_Object Voverriding_local_map;
127
128 Lisp_Object Quser_variable_p;
129
130 Lisp_Object Qminibuffer_default;
131
132 Lisp_Object Qcurrent_input_method, Qactivate_input_method;
133
134 extern Lisp_Object Qmouse_face;
135
136 extern Lisp_Object Qfield;
137 \f
138 /* Put minibuf on currently selected frame's minibuffer.
139 We do this whenever the user starts a new minibuffer
140 or when a minibuffer exits. */
141
142 void
143 choose_minibuf_frame ()
144 {
145 if (FRAMEP (selected_frame)
146 && FRAME_LIVE_P (XFRAME (selected_frame))
147 && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
148 {
149 struct frame *sf = XFRAME (selected_frame);
150 Lisp_Object buffer;
151
152 /* I don't think that any frames may validly have a null minibuffer
153 window anymore. */
154 if (NILP (sf->minibuffer_window))
155 abort ();
156
157 /* Under X, we come here with minibuf_window being the
158 minibuffer window of the unused termcap window created in
159 init_window_once. That window doesn't have a buffer. */
160 buffer = XWINDOW (minibuf_window)->buffer;
161 if (BUFFERP (buffer))
162 Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
163 minibuf_window = sf->minibuffer_window;
164 }
165
166 /* Make sure no other frame has a minibuffer as its selected window,
167 because the text would not be displayed in it, and that would be
168 confusing. Only allow the selected frame to do this,
169 and that only if the minibuffer is active. */
170 {
171 Lisp_Object tail, frame;
172
173 FOR_EACH_FRAME (tail, frame)
174 if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
175 && !(EQ (frame, selected_frame)
176 && minibuf_level > 0))
177 Fset_frame_selected_window (frame, Fframe_first_window (frame));
178 }
179 }
180
181 Lisp_Object
182 choose_minibuf_frame_1 (ignore)
183 Lisp_Object ignore;
184 {
185 choose_minibuf_frame ();
186 return Qnil;
187 }
188
189 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
190 Sset_minibuffer_window, 1, 1, 0,
191 doc: /* Specify which minibuffer window to use for the minibuffer.
192 This effects where the minibuffer is displayed if you put text in it
193 without invoking the usual minibuffer commands. */)
194 (window)
195 Lisp_Object window;
196 {
197 CHECK_WINDOW (window);
198 if (! MINI_WINDOW_P (XWINDOW (window)))
199 error ("Window is not a minibuffer window");
200
201 minibuf_window = window;
202
203 return window;
204 }
205
206 \f
207 /* Actual minibuffer invocation. */
208
209 static Lisp_Object read_minibuf_unwind P_ ((Lisp_Object));
210 static Lisp_Object read_minibuf P_ ((Lisp_Object, Lisp_Object,
211 Lisp_Object, Lisp_Object,
212 int, Lisp_Object,
213 Lisp_Object, Lisp_Object,
214 int, int));
215 static Lisp_Object read_minibuf_noninteractive P_ ((Lisp_Object, Lisp_Object,
216 Lisp_Object, Lisp_Object,
217 int, Lisp_Object,
218 Lisp_Object, Lisp_Object,
219 int, int));
220 static Lisp_Object string_to_object P_ ((Lisp_Object, Lisp_Object));
221
222
223 /* Read a Lisp object from VAL and return it. If VAL is an empty
224 string, and DEFALT is a string, read from DEFALT instead of VAL. */
225
226 static Lisp_Object
227 string_to_object (val, defalt)
228 Lisp_Object val, defalt;
229 {
230 struct gcpro gcpro1, gcpro2;
231 Lisp_Object expr_and_pos;
232 int pos;
233
234 GCPRO2 (val, defalt);
235
236 if (STRINGP (val) && SCHARS (val) == 0
237 && STRINGP (defalt))
238 val = defalt;
239
240 expr_and_pos = Fread_from_string (val, Qnil, Qnil);
241 pos = XINT (Fcdr (expr_and_pos));
242 if (pos != SCHARS (val))
243 {
244 /* Ignore trailing whitespace; any other trailing junk
245 is an error. */
246 int i;
247 pos = string_char_to_byte (val, pos);
248 for (i = pos; i < SBYTES (val); i++)
249 {
250 int c = SREF (val, i);
251 if (c != ' ' && c != '\t' && c != '\n')
252 error ("Trailing garbage following expression");
253 }
254 }
255
256 val = Fcar (expr_and_pos);
257 RETURN_UNGCPRO (val);
258 }
259
260
261 /* Like read_minibuf but reading from stdin. This function is called
262 from read_minibuf to do the job if noninteractive. */
263
264 static Lisp_Object
265 read_minibuf_noninteractive (map, initial, prompt, backup_n, expflag,
266 histvar, histpos, defalt, allow_props,
267 inherit_input_method)
268 Lisp_Object map;
269 Lisp_Object initial;
270 Lisp_Object prompt;
271 Lisp_Object backup_n;
272 int expflag;
273 Lisp_Object histvar;
274 Lisp_Object histpos;
275 Lisp_Object defalt;
276 int allow_props;
277 int inherit_input_method;
278 {
279 int size, len;
280 char *line, *s;
281 Lisp_Object val;
282
283 fprintf (stdout, "%s", SDATA (prompt));
284 fflush (stdout);
285
286 val = Qnil;
287 size = 100;
288 len = 0;
289 line = (char *) xmalloc (size * sizeof *line);
290 while ((s = fgets (line + len, size - len, stdin)) != NULL
291 && (len = strlen (line),
292 len == size - 1 && line[len - 1] != '\n'))
293 {
294 size *= 2;
295 line = (char *) xrealloc (line, size);
296 }
297
298 if (s)
299 {
300 len = strlen (line);
301
302 if (len > 0 && line[len - 1] == '\n')
303 line[--len] = '\0';
304
305 val = build_string (line);
306 xfree (line);
307 }
308 else
309 {
310 xfree (line);
311 error ("Error reading from stdin");
312 }
313
314 /* If Lisp form desired instead of string, parse it. */
315 if (expflag)
316 val = string_to_object (val, defalt);
317
318 return val;
319 }
320 \f
321 DEFUN ("minibufferp", Fminibufferp,
322 Sminibufferp, 0, 1, 0,
323 doc: /* Return t if BUFFER is a minibuffer.
324 No argument or nil as argument means use current buffer as BUFFER.*/)
325 (buffer)
326 Lisp_Object buffer;
327 {
328 Lisp_Object tem;
329
330 if (NILP (buffer))
331 buffer = Fcurrent_buffer ();
332 else if (STRINGP (buffer))
333 buffer = Fget_buffer (buffer);
334 else
335 CHECK_BUFFER (buffer);
336
337 tem = Fmemq (buffer, Vminibuffer_list);
338 return ! NILP (tem) ? Qt : Qnil;
339 }
340
341 DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
342 Sminibuffer_prompt_end, 0, 0, 0,
343 doc: /* Return the buffer position of the end of the minibuffer prompt.
344 Return (point-min) if current buffer is not a mini-buffer. */)
345 ()
346 {
347 /* This function is written to be most efficient when there's a prompt. */
348 Lisp_Object beg, end, tem;
349 beg = make_number (BEGV);
350
351 tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
352 if (NILP (tem))
353 return beg;
354
355 end = Ffield_end (beg, Qnil, Qnil);
356
357 if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
358 return beg;
359 else
360 return end;
361 }
362
363 DEFUN ("minibuffer-contents", Fminibuffer_contents,
364 Sminibuffer_contents, 0, 0, 0,
365 doc: /* Return the user input in a minibuffer as a string.
366 The current buffer must be a minibuffer. */)
367 ()
368 {
369 int prompt_end = XINT (Fminibuffer_prompt_end ());
370 return make_buffer_string (prompt_end, ZV, 1);
371 }
372
373 DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
374 Sminibuffer_contents_no_properties, 0, 0, 0,
375 doc: /* Return the user input in a minibuffer as a string, without text-properties.
376 The current buffer must be a minibuffer. */)
377 ()
378 {
379 int prompt_end = XINT (Fminibuffer_prompt_end ());
380 return make_buffer_string (prompt_end, ZV, 0);
381 }
382
383 DEFUN ("delete-minibuffer-contents", Fdelete_minibuffer_contents,
384 Sdelete_minibuffer_contents, 0, 0, 0,
385 doc: /* Delete all user input in a minibuffer.
386 The current buffer must be a minibuffer. */)
387 ()
388 {
389 int prompt_end = XINT (Fminibuffer_prompt_end ());
390 if (prompt_end < ZV)
391 del_range (prompt_end, ZV);
392 return Qnil;
393 }
394
395 /* Get the text in the minibuffer before point.
396 That is what completion commands operate on. */
397
398 Lisp_Object
399 minibuffer_completion_contents ()
400 {
401 int prompt_end = XINT (Fminibuffer_prompt_end ());
402 if (PT < prompt_end)
403 error ("Cannot do completion in the prompt");
404 return make_buffer_string (prompt_end, PT, 1);
405 }
406 \f
407 /* Read from the minibuffer using keymap MAP, initial contents INITIAL
408 (a string), putting point minus BACKUP_N bytes from the end of INITIAL,
409 prompting with PROMPT (a string), using history list HISTVAR
410 with initial position HISTPOS. (BACKUP_N should be <= 0.)
411
412 Normally return the result as a string (the text that was read),
413 but if EXPFLAG is nonzero, read it and return the object read.
414 If HISTVAR is given, save the value read on that history only if it doesn't
415 match the front of that history list exactly. The value is pushed onto
416 the list as the string that was read.
417
418 DEFALT specifies the default value for the sake of history commands.
419
420 If ALLOW_PROPS is nonzero, we do not throw away text properties.
421
422 if INHERIT_INPUT_METHOD is nonzeor, the minibuffer inherit the
423 current input method. */
424
425 static Lisp_Object
426 read_minibuf (map, initial, prompt, backup_n, expflag,
427 histvar, histpos, defalt, allow_props, inherit_input_method)
428 Lisp_Object map;
429 Lisp_Object initial;
430 Lisp_Object prompt;
431 Lisp_Object backup_n;
432 int expflag;
433 Lisp_Object histvar;
434 Lisp_Object histpos;
435 Lisp_Object defalt;
436 int allow_props;
437 int inherit_input_method;
438 {
439 Lisp_Object val;
440 int count = SPECPDL_INDEX ();
441 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
442 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
443 Lisp_Object enable_multibyte;
444
445 /* String to add to the history. */
446 Lisp_Object histstring;
447
448 extern Lisp_Object Qfront_sticky;
449 extern Lisp_Object Qrear_nonsticky;
450
451 specbind (Qminibuffer_default, defalt);
452
453 single_kboard_state ();
454 #ifdef HAVE_X_WINDOWS
455 if (display_hourglass_p)
456 cancel_hourglass ();
457 #endif
458
459 val = Qnil;
460 ambient_dir = current_buffer->directory;
461 input_method = Qnil;
462 enable_multibyte = Qnil;
463
464 /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
465 store them away before we can GC. Don't need to protect
466 BACKUP_N because we use the value only if it is an integer. */
467 GCPRO5 (map, initial, val, ambient_dir, input_method);
468
469 if (!STRINGP (prompt))
470 prompt = empty_string;
471
472 if (!enable_recursive_minibuffers
473 && minibuf_level > 0)
474 {
475 if (EQ (selected_window, minibuf_window))
476 error ("Command attempted to use minibuffer while in minibuffer");
477 else
478 /* If we're in another window, cancel the minibuffer that's active. */
479 Fthrow (Qexit,
480 build_string ("Command attempted to use minibuffer while in minibuffer"));
481 }
482
483 if (noninteractive)
484 {
485 val = read_minibuf_noninteractive (map, initial, prompt, backup_n,
486 expflag, histvar, histpos, defalt,
487 allow_props, inherit_input_method);
488 UNGCPRO;
489 return unbind_to (count, val);
490 }
491
492 /* Choose the minibuffer window and frame, and take action on them. */
493
494 choose_minibuf_frame ();
495
496 record_unwind_protect (choose_minibuf_frame_1, Qnil);
497
498 record_unwind_protect (Fset_window_configuration,
499 Fcurrent_window_configuration (Qnil));
500
501 /* If the minibuffer window is on a different frame, save that
502 frame's configuration too. */
503 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
504 if (!EQ (mini_frame, selected_frame))
505 record_unwind_protect (Fset_window_configuration,
506 Fcurrent_window_configuration (mini_frame));
507
508 /* If the minibuffer is on an iconified or invisible frame,
509 make it visible now. */
510 Fmake_frame_visible (mini_frame);
511
512 if (minibuffer_auto_raise)
513 Fraise_frame (mini_frame);
514
515 /* We have to do this after saving the window configuration
516 since that is what restores the current buffer. */
517
518 /* Arrange to restore a number of minibuffer-related variables.
519 We could bind each variable separately, but that would use lots of
520 specpdl slots. */
521 minibuf_save_list
522 = Fcons (Voverriding_local_map,
523 Fcons (minibuf_window, minibuf_save_list));
524 minibuf_save_list
525 = Fcons (minibuf_prompt,
526 Fcons (make_number (minibuf_prompt_width),
527 Fcons (Vhelp_form,
528 Fcons (Vcurrent_prefix_arg,
529 Fcons (Vminibuffer_history_position,
530 Fcons (Vminibuffer_history_variable,
531 minibuf_save_list))))));
532
533 record_unwind_protect (read_minibuf_unwind, Qnil);
534 minibuf_level++;
535
536 /* Now that we can restore all those variables, start changing them. */
537
538 minibuf_prompt_width = 0;
539 minibuf_prompt = Fcopy_sequence (prompt);
540 Vminibuffer_history_position = histpos;
541 Vminibuffer_history_variable = histvar;
542 Vhelp_form = Vminibuffer_help_form;
543
544 if (inherit_input_method)
545 {
546 /* `current-input-method' is buffer local. So, remember it in
547 INPUT_METHOD before changing the current buffer. */
548 input_method = Fsymbol_value (Qcurrent_input_method);
549 enable_multibyte = current_buffer->enable_multibyte_characters;
550 }
551
552 /* Switch to the minibuffer. */
553
554 minibuffer = get_minibuffer (minibuf_level);
555 Fset_buffer (minibuffer);
556
557 /* The current buffer's default directory is usually the right thing
558 for our minibuffer here. However, if you're typing a command at
559 a minibuffer-only frame when minibuf_level is zero, then buf IS
560 the current_buffer, so reset_buffer leaves buf's default
561 directory unchanged. This is a bummer when you've just started
562 up Emacs and buf's default directory is Qnil. Here's a hack; can
563 you think of something better to do? Find another buffer with a
564 better directory, and use that one instead. */
565 if (STRINGP (ambient_dir))
566 current_buffer->directory = ambient_dir;
567 else
568 {
569 Lisp_Object buf_list;
570
571 for (buf_list = Vbuffer_alist;
572 CONSP (buf_list);
573 buf_list = XCDR (buf_list))
574 {
575 Lisp_Object other_buf;
576
577 other_buf = XCDR (XCAR (buf_list));
578 if (STRINGP (XBUFFER (other_buf)->directory))
579 {
580 current_buffer->directory = XBUFFER (other_buf)->directory;
581 break;
582 }
583 }
584 }
585
586 if (!EQ (mini_frame, selected_frame))
587 Fredirect_frame_focus (selected_frame, mini_frame);
588
589 Vminibuf_scroll_window = selected_window;
590 if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
591 minibuf_selected_window = selected_window;
592 Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
593 Fselect_window (minibuf_window, Qnil);
594 XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
595
596 Fmake_local_variable (Qprint_escape_newlines);
597 print_escape_newlines = 1;
598
599 /* Erase the buffer. */
600 {
601 int count1 = SPECPDL_INDEX ();
602 specbind (Qinhibit_read_only, Qt);
603 specbind (Qinhibit_modification_hooks, Qt);
604 Ferase_buffer ();
605 unbind_to (count1, Qnil);
606 }
607
608 if (!NILP (current_buffer->enable_multibyte_characters)
609 && ! STRING_MULTIBYTE (minibuf_prompt))
610 minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
611
612 /* Insert the prompt, record where it ends. */
613 Finsert (1, &minibuf_prompt);
614 if (PT > BEG)
615 {
616 Fput_text_property (make_number (BEG), make_number (PT),
617 Qfront_sticky, Qt, Qnil);
618 Fput_text_property (make_number (BEG), make_number (PT),
619 Qrear_nonsticky, Qt, Qnil);
620 Fput_text_property (make_number (BEG), make_number (PT),
621 Qfield, Qt, Qnil);
622 Fadd_text_properties (make_number (BEG), make_number (PT),
623 Vminibuffer_prompt_properties, Qnil);
624 }
625
626 minibuf_prompt_width = (int) current_column (); /* iftc */
627
628 /* If appropriate, copy enable-multibyte-characters into the minibuffer. */
629 if (inherit_input_method)
630 current_buffer->enable_multibyte_characters = enable_multibyte;
631
632 /* Put in the initial input. */
633 if (!NILP (initial))
634 {
635 Finsert (1, &initial);
636 if (INTEGERP (backup_n))
637 Fforward_char (backup_n);
638 }
639
640 clear_message (1, 1);
641 current_buffer->keymap = map;
642
643 /* Turn on an input method stored in INPUT_METHOD if any. */
644 if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
645 call1 (Qactivate_input_method, input_method);
646
647 /* Run our hook, but not if it is empty.
648 (run-hooks would do nothing if it is empty,
649 but it's important to save time here in the usual case.) */
650 if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
651 && !NILP (Vrun_hooks))
652 call1 (Vrun_hooks, Qminibuffer_setup_hook);
653
654 /* Don't allow the user to undo past this point. */
655 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 if (rif && rif->flush_display)
669 rif->flush_display (XFRAME (XWINDOW (minibuf_window)->frame));
670 }
671
672 /* Make minibuffer contents into a string. */
673 Fset_buffer (minibuffer);
674 if (allow_props)
675 val = Fminibuffer_contents ();
676 else
677 val = Fminibuffer_contents_no_properties ();
678
679 /* VAL is the string of minibuffer text. */
680
681 last_minibuf_string = val;
682
683 /* Choose the string to add to the history. */
684 if (SCHARS (val) != 0)
685 histstring = val;
686 else if (STRINGP (defalt))
687 histstring = defalt;
688 else
689 histstring = Qnil;
690
691 /* Add the value to the appropriate history list, if any. */
692 if (SYMBOLP (Vminibuffer_history_variable)
693 && !NILP (histstring))
694 {
695 /* If the caller wanted to save the value read on a history list,
696 then do so if the value is not already the front of the list. */
697 Lisp_Object histval;
698
699 /* If variable is unbound, make it nil. */
700 if (EQ (SYMBOL_VALUE (Vminibuffer_history_variable), Qunbound))
701 Fset (Vminibuffer_history_variable, Qnil);
702
703 histval = Fsymbol_value (Vminibuffer_history_variable);
704
705 /* The value of the history variable must be a cons or nil. Other
706 values are unacceptable. We silently ignore these values. */
707
708 if (NILP (histval)
709 || (CONSP (histval)
710 /* Don't duplicate the most recent entry in the history. */
711 && NILP (Fequal (histstring, Fcar (histval)))))
712 {
713 Lisp_Object length;
714
715 histval = Fcons (histstring, histval);
716 Fset (Vminibuffer_history_variable, histval);
717
718 /* Truncate if requested. */
719 length = Fget (Vminibuffer_history_variable, Qhistory_length);
720 if (NILP (length)) length = Vhistory_length;
721 if (INTEGERP (length))
722 {
723 if (XINT (length) <= 0)
724 Fset (Vminibuffer_history_variable, Qnil);
725 else
726 {
727 Lisp_Object temp;
728
729 temp = Fnthcdr (Fsub1 (length), histval);
730 if (CONSP (temp)) Fsetcdr (temp, Qnil);
731 }
732 }
733 }
734 }
735
736 /* If Lisp form desired instead of string, parse it. */
737 if (expflag)
738 val = string_to_object (val, defalt);
739
740 /* The appropriate frame will get selected
741 in set-window-configuration. */
742 UNGCPRO;
743 return unbind_to (count, val);
744 }
745
746 /* Return a buffer to be used as the minibuffer at depth `depth'.
747 depth = 0 is the lowest allowed argument, and that is the value
748 used for nonrecursive minibuffer invocations */
749
750 Lisp_Object
751 get_minibuffer (depth)
752 int depth;
753 {
754 Lisp_Object tail, num, buf;
755 char name[24];
756 extern Lisp_Object nconc2 ();
757
758 XSETFASTINT (num, depth);
759 tail = Fnthcdr (num, Vminibuffer_list);
760 if (NILP (tail))
761 {
762 tail = Fcons (Qnil, Qnil);
763 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
764 }
765 buf = Fcar (tail);
766 if (NILP (buf) || NILP (XBUFFER (buf)->name))
767 {
768 sprintf (name, " *Minibuf-%d*", depth);
769 buf = Fget_buffer_create (build_string (name));
770
771 /* Although the buffer's name starts with a space, undo should be
772 enabled in it. */
773 Fbuffer_enable_undo (buf);
774
775 XSETCAR (tail, buf);
776 }
777 else
778 {
779 int count = SPECPDL_INDEX ();
780 /* `reset_buffer' blindly sets the list of overlays to NULL, so we
781 have to empty the list, otherwise we end up with overlays that
782 think they belong to this buffer while the buffer doesn't know about
783 them any more. */
784 delete_all_overlays (XBUFFER (buf));
785 reset_buffer (XBUFFER (buf));
786 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
787 Fset_buffer (buf);
788 Fkill_all_local_variables ();
789 unbind_to (count, Qnil);
790 }
791
792 return buf;
793 }
794
795 /* This function is called on exiting minibuffer, whether normally or
796 not, and it restores the current window, buffer, etc. */
797
798 static Lisp_Object
799 read_minibuf_unwind (data)
800 Lisp_Object data;
801 {
802 Lisp_Object old_deactivate_mark;
803 Lisp_Object window;
804
805 /* We are exiting the minibuffer one way or the other,
806 so run the hook. */
807 if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
808 && !NILP (Vrun_hooks))
809 safe_run_hooks (Qminibuffer_exit_hook);
810
811 /* If this was a recursive minibuffer,
812 tie the minibuffer window back to the outer level minibuffer buffer. */
813 minibuf_level--;
814
815 window = minibuf_window;
816 /* To keep things predictable, in case it matters, let's be in the
817 minibuffer when we reset the relevant variables. */
818 Fset_buffer (XWINDOW (window)->buffer);
819
820 /* Restore prompt, etc, from outer minibuffer level. */
821 minibuf_prompt = Fcar (minibuf_save_list);
822 minibuf_save_list = Fcdr (minibuf_save_list);
823 minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
824 minibuf_save_list = Fcdr (minibuf_save_list);
825 Vhelp_form = Fcar (minibuf_save_list);
826 minibuf_save_list = Fcdr (minibuf_save_list);
827 Vcurrent_prefix_arg = Fcar (minibuf_save_list);
828 minibuf_save_list = Fcdr (minibuf_save_list);
829 Vminibuffer_history_position = Fcar (minibuf_save_list);
830 minibuf_save_list = Fcdr (minibuf_save_list);
831 Vminibuffer_history_variable = Fcar (minibuf_save_list);
832 minibuf_save_list = Fcdr (minibuf_save_list);
833 Voverriding_local_map = Fcar (minibuf_save_list);
834 minibuf_save_list = Fcdr (minibuf_save_list);
835 #if 0
836 temp = Fcar (minibuf_save_list);
837 if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
838 minibuf_window = temp;
839 #endif
840 minibuf_save_list = Fcdr (minibuf_save_list);
841
842 /* Erase the minibuffer we were using at this level. */
843 {
844 int count = SPECPDL_INDEX ();
845 /* Prevent error in erase-buffer. */
846 specbind (Qinhibit_read_only, Qt);
847 specbind (Qinhibit_modification_hooks, Qt);
848 old_deactivate_mark = Vdeactivate_mark;
849 Ferase_buffer ();
850 Vdeactivate_mark = old_deactivate_mark;
851 unbind_to (count, Qnil);
852 }
853
854 /* When we get to the outmost level, make sure we resize the
855 mini-window back to its normal size. */
856 if (minibuf_level == 0)
857 resize_mini_window (XWINDOW (window), 0);
858
859 /* Make sure minibuffer window is erased, not ignored. */
860 windows_or_buffers_changed++;
861 XSETFASTINT (XWINDOW (window)->last_modified, 0);
862 XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
863 return Qnil;
864 }
865 \f
866
867 DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
868 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
869 If optional second arg INITIAL-CONTENTS is non-nil, it is a string
870 to be inserted into the minibuffer before reading input.
871 If INITIAL-CONTENTS is (STRING . POSITION), the initial input
872 is STRING, but point is placed at position POSITION in the minibuffer.
873 Third arg KEYMAP is a keymap to use whilst reading;
874 if omitted or nil, the default is `minibuffer-local-map'.
875 If fourth arg READ is non-nil, then interpret the result as a Lisp object
876 and return that object:
877 in other words, do `(car (read-from-string INPUT-STRING))'
878 Fifth arg HIST, if non-nil, specifies a history list
879 and optionally the initial position in the list.
880 It can be a symbol, which is the history list variable to use,
881 or it can be a cons cell (HISTVAR . HISTPOS).
882 In that case, HISTVAR is the history list variable to use,
883 and HISTPOS is the initial position (the position in the list
884 which INITIAL-CONTENTS corresponds to).
885 Positions are counted starting from 1 at the beginning of the list.
886 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
887 for history commands; but `read-from-minibuffer' does NOT return DEFAULT-VALUE
888 if the user enters empty input! It returns the empty string.
889 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
890 the current input method and the setting of `enable-multibyte-characters'.
891 If the variable `minibuffer-allow-text-properties' is non-nil,
892 then the string which is returned includes whatever text properties
893 were present in the minibuffer. Otherwise the value has no text properties. */)
894 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
895 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
896 Lisp_Object inherit_input_method;
897 {
898 int pos = 0;
899 Lisp_Object histvar, histpos, position, val;
900 struct gcpro gcpro1;
901
902 position = Qnil;
903
904 CHECK_STRING (prompt);
905 if (!NILP (initial_contents))
906 {
907 if (CONSP (initial_contents))
908 {
909 position = Fcdr (initial_contents);
910 initial_contents = Fcar (initial_contents);
911 }
912 CHECK_STRING (initial_contents);
913 if (!NILP (position))
914 {
915 CHECK_NUMBER (position);
916 /* Convert to distance from end of input. */
917 if (XINT (position) < 1)
918 /* A number too small means the beginning of the string. */
919 pos = - SCHARS (initial_contents);
920 else
921 pos = XINT (position) - 1 - SCHARS (initial_contents);
922 }
923 }
924
925 if (NILP (keymap))
926 keymap = Vminibuffer_local_map;
927 else
928 keymap = get_keymap (keymap, 1, 0);
929
930 if (SYMBOLP (hist))
931 {
932 histvar = hist;
933 histpos = Qnil;
934 }
935 else
936 {
937 histvar = Fcar_safe (hist);
938 histpos = Fcdr_safe (hist);
939 }
940 if (NILP (histvar))
941 histvar = Qminibuffer_history;
942 if (NILP (histpos))
943 XSETFASTINT (histpos, 0);
944
945 GCPRO1 (default_value);
946 val = read_minibuf (keymap, initial_contents, prompt,
947 make_number (pos), !NILP (read),
948 histvar, histpos, default_value,
949 minibuffer_allow_text_properties,
950 !NILP (inherit_input_method));
951 UNGCPRO;
952 return val;
953 }
954
955 DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
956 doc: /* Return a Lisp object read using the minibuffer.
957 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
958 is a string to insert in the minibuffer before reading. */)
959 (prompt, initial_contents)
960 Lisp_Object prompt, initial_contents;
961 {
962 CHECK_STRING (prompt);
963 if (!NILP (initial_contents))
964 CHECK_STRING (initial_contents);
965 return read_minibuf (Vminibuffer_local_map, initial_contents,
966 prompt, Qnil, 1, Qminibuffer_history,
967 make_number (0), Qnil, 0, 0);
968 }
969
970 DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
971 doc: /* Return value of Lisp expression read using the minibuffer.
972 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
973 is a string to insert in the minibuffer before reading. */)
974 (prompt, initial_contents)
975 Lisp_Object prompt, initial_contents;
976 {
977 return Feval (Fread_minibuffer (prompt, initial_contents));
978 }
979
980 /* Functions that use the minibuffer to read various things. */
981
982 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
983 doc: /* Read a string from the minibuffer, prompting with string PROMPT.
984 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
985 The third arg HISTORY, if non-nil, specifies a history list
986 and optionally the initial position in the list.
987 See `read-from-minibuffer' for details of HISTORY argument.
988 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
989 for history commands, and as the value to return if the user enters
990 the empty string.
991 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
992 the current input method and the setting of `enable-multibyte-characters'. */)
993 (prompt, initial_input, history, default_value, inherit_input_method)
994 Lisp_Object prompt, initial_input, history, default_value;
995 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 = 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, and provide INITIAL as an initial value of the input string.
1009 Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
1010 the current input method and the setting of `enable-multibyte-characters'. */)
1011 (prompt, initial, inherit_input_method)
1012 Lisp_Object prompt, initial, inherit_input_method;
1013 {
1014 CHECK_STRING (prompt);
1015 if (! NILP (initial))
1016 CHECK_STRING (initial);
1017
1018 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
1019 0, Qminibuffer_history, make_number (0), Qnil, 0,
1020 !NILP (inherit_input_method));
1021 }
1022
1023 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
1024 doc: /* Read the name of a command and return as a symbol.
1025 Prompt with PROMPT. By default, return DEFAULT-VALUE. */)
1026 (prompt, default_value)
1027 Lisp_Object prompt, default_value;
1028 {
1029 Lisp_Object name, default_string;
1030
1031 if (NILP (default_value))
1032 default_string = Qnil;
1033 else if (SYMBOLP (default_value))
1034 default_string = SYMBOL_NAME (default_value);
1035 else
1036 default_string = default_value;
1037
1038 name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
1039 Qnil, Qnil, default_string, Qnil);
1040 if (NILP (name))
1041 return name;
1042 return Fintern (name, Qnil);
1043 }
1044
1045 #ifdef NOTDEF
1046 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
1047 doc: /* One arg PROMPT, a string. Read the name of a function and return as a symbol.
1048 Prompt with PROMPT. */)
1049 (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.
1060 A user variable is one for which `user-variable-p' returns non-nil. */)
1061 (prompt, default_value)
1062 Lisp_Object prompt, 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 optional third arg REQUIRE-MATCH is non-nil,
1086 only existing buffer names are allowed. */)
1087 (prompt, def, require_match)
1088 Lisp_Object prompt, def, require_match;
1089 {
1090 Lisp_Object args[4];
1091
1092 if (BUFFERP (def))
1093 def = XBUFFER (def)->name;
1094
1095 if (NILP (Vread_buffer_function))
1096 {
1097 if (!NILP (def))
1098 {
1099 args[0] = build_string ("%s(default %s) ");
1100 args[1] = prompt;
1101 args[2] = def;
1102 prompt = Fformat (3, args);
1103 }
1104
1105 return Fcompleting_read (prompt, Vbuffer_alist, Qnil,
1106 require_match, Qnil, Qbuffer_name_history,
1107 def, Qnil);
1108 }
1109 else
1110 {
1111 args[0] = Vread_buffer_function;
1112 args[1] = prompt;
1113 args[2] = def;
1114 args[3] = require_match;
1115 return Ffuncall(4, args);
1116 }
1117 }
1118 \f
1119 static Lisp_Object
1120 minibuf_conform_representation (string, basis)
1121 Lisp_Object string, basis;
1122 {
1123 if (STRING_MULTIBYTE (string) == STRING_MULTIBYTE (basis))
1124 return string;
1125
1126 if (STRING_MULTIBYTE (string))
1127 return Fstring_make_unibyte (string);
1128 else
1129 return Fstring_make_multibyte (string);
1130 }
1131
1132 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
1133 doc: /* Return common substring of all completions of STRING in ALIST.
1134 Each car of each element of ALIST (or each element if it is not a cons cell)
1135 is tested to see if it begins with STRING.
1136 All that match are compared together; the longest initial sequence
1137 common to all matches is returned as a string.
1138 If there is no match at all, nil is returned.
1139 For a unique match which is exact, t is returned.
1140
1141 If ALIST is a hash-table, all the string keys are the possible matches.
1142 If ALIST is an obarray, the names of all symbols in the obarray
1143 are the possible matches.
1144
1145 ALIST can also be a function to do the completion itself.
1146 It receives three arguments: the values STRING, PREDICATE and nil.
1147 Whatever it returns becomes the value of `try-completion'.
1148
1149 If optional third argument PREDICATE is non-nil,
1150 it is used to test each possible match.
1151 The match is a candidate only if PREDICATE returns non-nil.
1152 The argument given to PREDICATE is the alist element
1153 or the symbol from the obarray. If ALIST is a hash-table,
1154 predicate is called with two arguments: the key and the value.
1155 Additionally to this predicate, `completion-regexp-list'
1156 is used to further constrain the set of candidates. */)
1157 (string, alist, predicate)
1158 Lisp_Object string, alist, predicate;
1159 {
1160 Lisp_Object bestmatch, tail, elt, eltstring;
1161 /* Size in bytes of BESTMATCH. */
1162 int bestmatchsize = 0;
1163 /* These are in bytes, too. */
1164 int compare, matchsize;
1165 int type = HASH_TABLE_P (alist) ? 3
1166 : VECTORP (alist) ? 2
1167 : NILP (alist) || (CONSP (alist)
1168 && (!SYMBOLP (XCAR (alist))
1169 || NILP (XCAR (alist))));
1170 int index = 0, obsize = 0;
1171 int matchcount = 0;
1172 Lisp_Object bucket, zero, end, tem;
1173 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1174
1175 CHECK_STRING (string);
1176 if (type == 0)
1177 return call3 (alist, string, predicate, Qnil);
1178
1179 bestmatch = bucket = Qnil;
1180
1181 /* If ALIST is not a list, set TAIL just for gc pro. */
1182 tail = alist;
1183 if (type == 2)
1184 {
1185 obsize = XVECTOR (alist)->size;
1186 bucket = XVECTOR (alist)->contents[index];
1187 }
1188
1189 while (1)
1190 {
1191 /* Get the next element of the alist, obarray, or hash-table. */
1192 /* Exit the loop if the elements are all used up. */
1193 /* elt gets the alist element or symbol.
1194 eltstring gets the name to check as a completion. */
1195
1196 if (type == 1)
1197 {
1198 if (!CONSP (tail))
1199 break;
1200 elt = XCAR (tail);
1201 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1202 tail = XCDR (tail);
1203 }
1204 else if (type == 2)
1205 {
1206 if (XFASTINT (bucket) != 0)
1207 {
1208 elt = bucket;
1209 eltstring = Fsymbol_name (elt);
1210 if (XSYMBOL (bucket)->next)
1211 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1212 else
1213 XSETFASTINT (bucket, 0);
1214 }
1215 else if (++index >= obsize)
1216 break;
1217 else
1218 {
1219 bucket = XVECTOR (alist)->contents[index];
1220 continue;
1221 }
1222 }
1223 else /* if (type == 3) */
1224 {
1225 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1226 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1227 index++;
1228 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1229 break;
1230 else
1231 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1232 }
1233
1234 /* Is this element a possible completion? */
1235
1236 if (STRINGP (eltstring)
1237 && SCHARS (string) <= SCHARS (eltstring)
1238 && (tem = Fcompare_strings (eltstring, make_number (0),
1239 make_number (SCHARS (string)),
1240 string, make_number (0), Qnil,
1241 completion_ignore_case ?Qt : Qnil),
1242 EQ (Qt, tem)))
1243 {
1244 /* Yes. */
1245 Lisp_Object regexps;
1246 Lisp_Object zero;
1247 XSETFASTINT (zero, 0);
1248
1249 /* Ignore this element if it fails to match all the regexps. */
1250 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1251 regexps = XCDR (regexps))
1252 {
1253 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1254 if (NILP (tem))
1255 break;
1256 }
1257 if (CONSP (regexps))
1258 continue;
1259
1260 /* Ignore this element if there is a predicate
1261 and the predicate doesn't like it. */
1262
1263 if (!NILP (predicate))
1264 {
1265 if (EQ (predicate, Qcommandp))
1266 tem = Fcommandp (elt, Qnil);
1267 else
1268 {
1269 GCPRO4 (tail, string, eltstring, bestmatch);
1270 tem = type == 3
1271 ? call2 (predicate, elt,
1272 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1273 : call1 (predicate, elt);
1274 UNGCPRO;
1275 }
1276 if (NILP (tem)) continue;
1277 }
1278
1279 /* Update computation of how much all possible completions match */
1280
1281 if (NILP (bestmatch))
1282 {
1283 matchcount = 1;
1284 bestmatch = eltstring;
1285 bestmatchsize = SCHARS (eltstring);
1286 }
1287 else
1288 {
1289 compare = min (bestmatchsize, SCHARS (eltstring));
1290 tem = Fcompare_strings (bestmatch, make_number (0),
1291 make_number (compare),
1292 eltstring, make_number (0),
1293 make_number (compare),
1294 completion_ignore_case ? Qt : Qnil);
1295 if (EQ (tem, Qt))
1296 matchsize = compare;
1297 else if (XINT (tem) < 0)
1298 matchsize = - XINT (tem) - 1;
1299 else
1300 matchsize = XINT (tem) - 1;
1301
1302 if (matchsize < 0)
1303 /* When can this happen ? -stef */
1304 matchsize = compare;
1305 if (completion_ignore_case)
1306 {
1307 /* If this is an exact match except for case,
1308 use it as the best match rather than one that is not an
1309 exact match. This way, we get the case pattern
1310 of the actual match. */
1311 if ((matchsize == SCHARS (eltstring)
1312 && matchsize < SCHARS (bestmatch))
1313 ||
1314 /* If there is more than one exact match ignoring case,
1315 and one of them is exact including case,
1316 prefer that one. */
1317 /* If there is no exact match ignoring case,
1318 prefer a match that does not change the case
1319 of the input. */
1320 ((matchsize == SCHARS (eltstring))
1321 ==
1322 (matchsize == SCHARS (bestmatch))
1323 && (tem = Fcompare_strings (eltstring, make_number (0),
1324 make_number (SCHARS (string)),
1325 string, make_number (0),
1326 Qnil,
1327 Qnil),
1328 EQ (Qt, tem))
1329 && (tem = Fcompare_strings (bestmatch, make_number (0),
1330 make_number (SCHARS (string)),
1331 string, make_number (0),
1332 Qnil,
1333 Qnil),
1334 ! EQ (Qt, tem))))
1335 bestmatch = eltstring;
1336 }
1337 if (bestmatchsize != SCHARS (eltstring)
1338 || bestmatchsize != matchsize)
1339 /* Don't count the same string multiple times. */
1340 matchcount++;
1341 bestmatchsize = matchsize;
1342 if (matchsize <= SCHARS (string)
1343 && matchcount > 1)
1344 /* No need to look any further. */
1345 break;
1346 }
1347 }
1348 }
1349
1350 if (NILP (bestmatch))
1351 return Qnil; /* No completions found */
1352 /* If we are ignoring case, and there is no exact match,
1353 and no additional text was supplied,
1354 don't change the case of what the user typed. */
1355 if (completion_ignore_case && bestmatchsize == SCHARS (string)
1356 && SCHARS (bestmatch) > bestmatchsize)
1357 return minibuf_conform_representation (string, bestmatch);
1358
1359 /* Return t if the supplied string is an exact match (counting case);
1360 it does not require any change to be made. */
1361 if (matchcount == 1 && bestmatchsize == SCHARS (string)
1362 && (tem = Fcompare_strings (bestmatch, make_number (0),
1363 make_number (bestmatchsize),
1364 string, make_number (0),
1365 make_number (bestmatchsize),
1366 Qnil),
1367 EQ (Qt, tem)))
1368 return Qt;
1369
1370 XSETFASTINT (zero, 0); /* Else extract the part in which */
1371 XSETFASTINT (end, bestmatchsize); /* all completions agree */
1372 return Fsubstring (bestmatch, zero, end);
1373 }
1374 \f
1375 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
1376 doc: /* Search for partial matches to STRING in ALIST.
1377 Each car of each element of ALIST (or each element if it is not a cons cell)
1378 is tested to see if it begins with STRING.
1379 The value is a list of all the strings from ALIST that match.
1380
1381 If ALIST is a hash-table, all the string keys are the possible matches.
1382 If ALIST is an obarray, the names of all symbols in the obarray
1383 are the possible matches.
1384
1385 ALIST can also be a function to do the completion itself.
1386 It receives three arguments: the values STRING, PREDICATE and t.
1387 Whatever it returns becomes the value of `all-completions'.
1388
1389 If optional third argument PREDICATE is non-nil,
1390 it is used to test each possible match.
1391 The match is a candidate only if PREDICATE returns non-nil.
1392 The argument given to PREDICATE is the alist element
1393 or the symbol from the obarray. If ALIST is a hash-table,
1394 predicate is called with two arguments: the key and the value.
1395 Additionally to this predicate, `completion-regexp-list'
1396 is used to further constrain the set of candidates.
1397
1398 If the optional fourth argument HIDE-SPACES is non-nil,
1399 strings in ALIST that start with a space
1400 are ignored unless STRING itself starts with a space. */)
1401 (string, alist, predicate, hide_spaces)
1402 Lisp_Object string, alist, predicate, hide_spaces;
1403 {
1404 Lisp_Object tail, elt, eltstring;
1405 Lisp_Object allmatches;
1406 int type = HASH_TABLE_P (alist) ? 3
1407 : VECTORP (alist) ? 2
1408 : NILP (alist) || (CONSP (alist)
1409 && (!SYMBOLP (XCAR (alist))
1410 || NILP (XCAR (alist))));
1411 int index = 0, obsize = 0;
1412 Lisp_Object bucket, tem;
1413 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1414
1415 CHECK_STRING (string);
1416 if (type == 0)
1417 return call3 (alist, string, predicate, Qt);
1418 allmatches = bucket = Qnil;
1419
1420 /* If ALIST is not a list, set TAIL just for gc pro. */
1421 tail = alist;
1422 if (type == 2)
1423 {
1424 obsize = XVECTOR (alist)->size;
1425 bucket = XVECTOR (alist)->contents[index];
1426 }
1427
1428 while (1)
1429 {
1430 /* Get the next element of the alist, obarray, or hash-table. */
1431 /* Exit the loop if the elements are all used up. */
1432 /* elt gets the alist element or symbol.
1433 eltstring gets the name to check as a completion. */
1434
1435 if (type == 1)
1436 {
1437 if (!CONSP (tail))
1438 break;
1439 elt = XCAR (tail);
1440 eltstring = CONSP (elt) ? XCAR (elt) : elt;
1441 tail = XCDR (tail);
1442 }
1443 else if (type == 2)
1444 {
1445 if (XFASTINT (bucket) != 0)
1446 {
1447 elt = bucket;
1448 eltstring = Fsymbol_name (elt);
1449 if (XSYMBOL (bucket)->next)
1450 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
1451 else
1452 XSETFASTINT (bucket, 0);
1453 }
1454 else if (++index >= obsize)
1455 break;
1456 else
1457 {
1458 bucket = XVECTOR (alist)->contents[index];
1459 continue;
1460 }
1461 }
1462 else /* if (type == 3) */
1463 {
1464 while (index < HASH_TABLE_SIZE (XHASH_TABLE (alist))
1465 && NILP (HASH_HASH (XHASH_TABLE (alist), index)))
1466 index++;
1467 if (index >= HASH_TABLE_SIZE (XHASH_TABLE (alist)))
1468 break;
1469 else
1470 elt = eltstring = HASH_KEY (XHASH_TABLE (alist), index++);
1471 }
1472
1473 /* Is this element a possible completion? */
1474
1475 if (STRINGP (eltstring)
1476 && SCHARS (string) <= SCHARS (eltstring)
1477 /* If HIDE_SPACES, reject alternatives that start with space
1478 unless the input starts with space. */
1479 && ((SBYTES (string) > 0
1480 && SREF (string, 0) == ' ')
1481 || SREF (eltstring, 0) != ' '
1482 || NILP (hide_spaces))
1483 && (tem = Fcompare_strings (eltstring, make_number (0),
1484 make_number (SCHARS (string)),
1485 string, make_number (0),
1486 make_number (SCHARS (string)),
1487 completion_ignore_case ? Qt : Qnil),
1488 EQ (Qt, tem)))
1489 {
1490 /* Yes. */
1491 Lisp_Object regexps;
1492 Lisp_Object zero;
1493 XSETFASTINT (zero, 0);
1494
1495 /* Ignore this element if it fails to match all the regexps. */
1496 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1497 regexps = XCDR (regexps))
1498 {
1499 tem = Fstring_match (XCAR (regexps), eltstring, zero);
1500 if (NILP (tem))
1501 break;
1502 }
1503 if (CONSP (regexps))
1504 continue;
1505
1506 /* Ignore this element if there is a predicate
1507 and the predicate doesn't like it. */
1508
1509 if (!NILP (predicate))
1510 {
1511 if (EQ (predicate, Qcommandp))
1512 tem = Fcommandp (elt, Qnil);
1513 else
1514 {
1515 GCPRO4 (tail, eltstring, allmatches, string);
1516 tem = type == 3
1517 ? call2 (predicate, elt,
1518 HASH_VALUE (XHASH_TABLE (alist), index - 1))
1519 : call1 (predicate, elt);
1520 UNGCPRO;
1521 }
1522 if (NILP (tem)) continue;
1523 }
1524 /* Ok => put it on the list. */
1525 allmatches = Fcons (eltstring, allmatches);
1526 }
1527 }
1528
1529 return Fnreverse (allmatches);
1530 }
1531 \f
1532 Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
1533 Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
1534 Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
1535 Lisp_Object Vminibuffer_completing_file_name;
1536
1537 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
1538 doc: /* Read a string in the minibuffer, with completion.
1539 PROMPT is a string to prompt with; normally it ends in a colon and a space.
1540 TABLE is an alist whose elements' cars are strings, or an obarray.
1541 TABLE can also be a function to do the completion itself.
1542 PREDICATE limits completion to a subset of TABLE.
1543 See `try-completion' and `all-completions' for more details
1544 on completion, TABLE, and PREDICATE.
1545
1546 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
1547 the input is (or completes to) an element of TABLE or is null.
1548 If it is also not t, typing RET does not exit if it does non-null completion.
1549 If the input is null, `completing-read' returns an empty string,
1550 regardless of the value of REQUIRE-MATCH.
1551
1552 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1553 If it is (STRING . POSITION), the initial input
1554 is STRING, but point is placed POSITION characters into the string.
1555 This feature is deprecated--it is best to pass nil for INITIAL-INPUT
1556 and supply the default value DEF instead. The user can yank the
1557 default value into the minibuffer easily using \\[next-history-element].
1558
1559 HIST, if non-nil, specifies a history list
1560 and optionally the initial position in the list.
1561 It can be a symbol, which is the history list variable to use,
1562 or it can be a cons cell (HISTVAR . HISTPOS).
1563 In that case, HISTVAR is the history list variable to use,
1564 and HISTPOS is the initial position (the position in the list
1565 which INITIAL-INPUT corresponds to).
1566 Positions are counted starting from 1 at the beginning of the list.
1567 The variable `history-length' controls the maximum length of a
1568 history list.
1569
1570 DEF, if non-nil, is the default value.
1571
1572 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits
1573 the current input method and the setting of `enable-multibyte-characters'.
1574
1575 Completion ignores case if the ambient value of
1576 `completion-ignore-case' is non-nil. */)
1577 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method)
1578 Lisp_Object prompt, table, predicate, require_match, initial_input;
1579 Lisp_Object hist, def, inherit_input_method;
1580 {
1581 Lisp_Object val, histvar, histpos, position;
1582 Lisp_Object init;
1583 int pos = 0;
1584 int count = SPECPDL_INDEX ();
1585 struct gcpro gcpro1;
1586
1587 init = initial_input;
1588 GCPRO1 (def);
1589
1590 specbind (Qminibuffer_completion_table, table);
1591 specbind (Qminibuffer_completion_predicate, predicate);
1592 specbind (Qminibuffer_completion_confirm,
1593 EQ (require_match, Qt) ? Qnil : require_match);
1594 last_exact_completion = Qnil;
1595
1596 position = Qnil;
1597 if (!NILP (init))
1598 {
1599 if (CONSP (init))
1600 {
1601 position = Fcdr (init);
1602 init = Fcar (init);
1603 }
1604 CHECK_STRING (init);
1605 if (!NILP (position))
1606 {
1607 CHECK_NUMBER (position);
1608 /* Convert to distance from end of input. */
1609 pos = XINT (position) - SCHARS (init);
1610 }
1611 }
1612
1613 if (SYMBOLP (hist))
1614 {
1615 histvar = hist;
1616 histpos = Qnil;
1617 }
1618 else
1619 {
1620 histvar = Fcar_safe (hist);
1621 histpos = Fcdr_safe (hist);
1622 }
1623 if (NILP (histvar))
1624 histvar = Qminibuffer_history;
1625 if (NILP (histpos))
1626 XSETFASTINT (histpos, 0);
1627
1628 val = read_minibuf (NILP (require_match)
1629 ? Vminibuffer_local_completion_map
1630 : Vminibuffer_local_must_match_map,
1631 init, prompt, make_number (pos), 0,
1632 histvar, histpos, def, 0,
1633 !NILP (inherit_input_method));
1634
1635 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
1636 val = def;
1637
1638 RETURN_UNGCPRO (unbind_to (count, val));
1639 }
1640 \f
1641 Lisp_Object Fminibuffer_completion_help ();
1642 Lisp_Object Fassoc_string ();
1643
1644 /* Test whether TXT is an exact completion. */
1645 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
1646 doc: /* Return non-nil if STRING is a valid completion.
1647 Takes the same arguments as `all-completions' and `try-completion'.
1648 If ALIST is a function, it is called with three arguments:
1649 the values STRING, PREDICATE and `lambda'. */)
1650 (string, alist, predicate)
1651 Lisp_Object string, alist, predicate;
1652 {
1653 Lisp_Object regexps, tem = Qnil;
1654 int i = 0;
1655
1656 CHECK_STRING (string);
1657
1658 if ((CONSP (alist) && (!SYMBOLP (XCAR (alist)) || NILP (XCAR (alist))))
1659 || NILP (alist))
1660 {
1661 tem = Fassoc_string (string, alist, completion_ignore_case ? Qt : Qnil);
1662 if NILP (tem)
1663 return Qnil;
1664 }
1665 else if (VECTORP (alist))
1666 {
1667 /* Bypass intern-soft as that loses for nil. */
1668 tem = oblookup (alist,
1669 SDATA (string),
1670 SCHARS (string),
1671 SBYTES (string));
1672 if (!SYMBOLP (tem))
1673 {
1674 if (STRING_MULTIBYTE (string))
1675 string = Fstring_make_unibyte (string);
1676 else
1677 string = Fstring_make_multibyte (string);
1678
1679 tem = oblookup (Vminibuffer_completion_table,
1680 SDATA (string),
1681 SCHARS (string),
1682 SBYTES (string));
1683 if (!SYMBOLP (tem))
1684 return Qnil;
1685 }
1686 }
1687 else if (HASH_TABLE_P (alist))
1688 {
1689 i = hash_lookup (XHASH_TABLE (alist), string, NULL);
1690 if (i >= 0)
1691 tem = HASH_KEY (XHASH_TABLE (alist), i);
1692 else
1693 return Qnil;
1694 }
1695 else
1696 return call3 (alist, string, predicate, Qlambda);
1697
1698 /* Reject this element if it fails to match all the regexps. */
1699 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
1700 regexps = XCDR (regexps))
1701 {
1702 if (NILP (Fstring_match (XCAR (regexps),
1703 SYMBOLP (tem) ? string : tem,
1704 Qnil)))
1705 return Qnil;
1706 }
1707
1708 /* Finally, check the predicate. */
1709 if (!NILP (predicate))
1710 return HASH_TABLE_P (alist)
1711 ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (alist), i))
1712 : call1 (predicate, tem);
1713 else
1714 return Qt;
1715 }
1716
1717 /* returns:
1718 * 0 no possible completion
1719 * 1 was already an exact and unique completion
1720 * 3 was already an exact completion
1721 * 4 completed to an exact completion
1722 * 5 some completion happened
1723 * 6 no completion happened
1724 */
1725 int
1726 do_completion ()
1727 {
1728 Lisp_Object completion, string, tem;
1729 int completedp;
1730 Lisp_Object last;
1731 struct gcpro gcpro1, gcpro2;
1732
1733 completion = Ftry_completion (minibuffer_completion_contents (),
1734 Vminibuffer_completion_table,
1735 Vminibuffer_completion_predicate);
1736 last = last_exact_completion;
1737 last_exact_completion = Qnil;
1738
1739 GCPRO2 (completion, last);
1740
1741 if (NILP (completion))
1742 {
1743 bitch_at_user ();
1744 temp_echo_area_glyphs (build_string (" [No match]"));
1745 UNGCPRO;
1746 return 0;
1747 }
1748
1749 if (EQ (completion, Qt)) /* exact and unique match */
1750 {
1751 UNGCPRO;
1752 return 1;
1753 }
1754
1755 string = minibuffer_completion_contents ();
1756
1757 /* COMPLETEDP should be true if some completion was done, which
1758 doesn't include simply changing the case of the entered string.
1759 However, for appearance, the string is rewritten if the case
1760 changes. */
1761 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qt);
1762 completedp = !EQ (tem, Qt);
1763
1764 tem = Fcompare_strings (completion, Qnil, Qnil, string, Qnil, Qnil, Qnil);
1765 if (!EQ (tem, Qt))
1766 /* Rewrite the user's input. */
1767 {
1768 int prompt_end = XINT (Fminibuffer_prompt_end ());
1769 /* Some completion happened */
1770
1771 if (! NILP (Vminibuffer_completing_file_name)
1772 && SREF (completion, SBYTES (completion) - 1) == '/'
1773 && PT < ZV
1774 && FETCH_CHAR (PT_BYTE) == '/')
1775 {
1776 del_range (prompt_end, PT + 1);
1777 }
1778 else
1779 del_range (prompt_end, PT);
1780
1781 Finsert (1, &completion);
1782
1783 if (! completedp)
1784 /* The case of the string changed, but that's all. We're not
1785 sure whether this is a unique completion or not, so try again
1786 using the real case (this shouldn't recurse again, because
1787 the next time try-completion will return either `t' or the
1788 exact string). */
1789 {
1790 UNGCPRO;
1791 return do_completion ();
1792 }
1793 }
1794
1795 /* It did find a match. Do we match some possibility exactly now? */
1796 tem = Ftest_completion (Fminibuffer_contents (),
1797 Vminibuffer_completion_table,
1798 Vminibuffer_completion_predicate);
1799 if (NILP (tem))
1800 {
1801 /* not an exact match */
1802 UNGCPRO;
1803 if (completedp)
1804 return 5;
1805 else if (!NILP (Vcompletion_auto_help))
1806 Fminibuffer_completion_help ();
1807 else
1808 temp_echo_area_glyphs (build_string (" [Next char not unique]"));
1809 return 6;
1810 }
1811 else if (completedp)
1812 {
1813 UNGCPRO;
1814 return 4;
1815 }
1816 /* If the last exact completion and this one were the same,
1817 it means we've already given a "Complete but not unique"
1818 message and the user's hit TAB again, so now we give him help. */
1819 last_exact_completion = completion;
1820 if (!NILP (last))
1821 {
1822 tem = minibuffer_completion_contents ();
1823 if (!NILP (Fequal (tem, last)))
1824 Fminibuffer_completion_help ();
1825 }
1826 UNGCPRO;
1827 return 3;
1828 }
1829
1830 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
1831
1832 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
1833 doc: /* Like `assoc' but specifically for strings.
1834 Unibyte strings are converted to multibyte for comparison.
1835 And case is ignored if CASE-FOLD is non-nil.
1836 As opposed to `assoc', it will also match an entry consisting of a single
1837 string rather than a cons cell whose car is a string. */)
1838 (key, list, case_fold)
1839 register Lisp_Object key;
1840 Lisp_Object list, case_fold;
1841 {
1842 register Lisp_Object tail;
1843
1844 for (tail = list; !NILP (tail); tail = Fcdr (tail))
1845 {
1846 register Lisp_Object elt, tem, thiscar;
1847 elt = Fcar (tail);
1848 thiscar = CONSP (elt) ? XCAR (elt) : elt;
1849 if (!STRINGP (thiscar))
1850 continue;
1851 tem = Fcompare_strings (thiscar, make_number (0), Qnil,
1852 key, make_number (0), Qnil,
1853 case_fold);
1854 if (EQ (tem, Qt))
1855 return elt;
1856 QUIT;
1857 }
1858 return Qnil;
1859 }
1860
1861 DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
1862 doc: /* Complete the minibuffer contents as far as possible.
1863 Return nil if there is no valid completion, else t.
1864 If no characters can be completed, display a list of possible completions.
1865 If you repeat this command after it displayed such a list,
1866 scroll the window of possible completions. */)
1867 ()
1868 {
1869 register int i;
1870 Lisp_Object window, tem;
1871
1872 /* If the previous command was not this,
1873 mark the completion buffer obsolete. */
1874 if (! EQ (current_kboard->Vlast_command, Vthis_command))
1875 Vminibuf_scroll_window = Qnil;
1876
1877 window = Vminibuf_scroll_window;
1878 /* If there's a fresh completion window with a live buffer,
1879 and this command is repeated, scroll that window. */
1880 if (! NILP (window) && ! NILP (XWINDOW (window)->buffer)
1881 && !NILP (XBUFFER (XWINDOW (window)->buffer)->name))
1882 {
1883 struct buffer *obuf = current_buffer;
1884
1885 Fset_buffer (XWINDOW (window)->buffer);
1886 tem = Fpos_visible_in_window_p (make_number (ZV), window, Qnil);
1887 if (! NILP (tem))
1888 /* If end is in view, scroll up to the beginning. */
1889 Fset_window_start (window, make_number (BEGV), Qnil);
1890 else
1891 /* Else scroll down one screen. */
1892 Fscroll_other_window (Qnil);
1893
1894 set_buffer_internal (obuf);
1895 return Qnil;
1896 }
1897
1898 i = do_completion ();
1899 switch (i)
1900 {
1901 case 0:
1902 return Qnil;
1903
1904 case 1:
1905 if (PT != ZV)
1906 Fgoto_char (make_number (ZV));
1907 temp_echo_area_glyphs (build_string (" [Sole completion]"));
1908 break;
1909
1910 case 3:
1911 if (PT != ZV)
1912 Fgoto_char (make_number (ZV));
1913 temp_echo_area_glyphs (build_string (" [Complete, but not unique]"));
1914 break;
1915 }
1916
1917 return Qt;
1918 }
1919 \f
1920 /* Subroutines of Fminibuffer_complete_and_exit. */
1921
1922 /* This one is called by internal_condition_case to do the real work. */
1923
1924 Lisp_Object
1925 complete_and_exit_1 ()
1926 {
1927 return make_number (do_completion ());
1928 }
1929
1930 /* This one is called by internal_condition_case if an error happens.
1931 Pretend the current value is an exact match. */
1932
1933 Lisp_Object
1934 complete_and_exit_2 (ignore)
1935 Lisp_Object ignore;
1936 {
1937 return make_number (1);
1938 }
1939
1940 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
1941 Sminibuffer_complete_and_exit, 0, 0, "",
1942 doc: /* If the minibuffer contents is a valid completion then exit.
1943 Otherwise try to complete it. If completion leads to a valid completion,
1944 a repetition of this command will exit. */)
1945 ()
1946 {
1947 register int i;
1948 Lisp_Object val;
1949
1950 /* Allow user to specify null string */
1951 if (XINT (Fminibuffer_prompt_end ()) == ZV)
1952 goto exit;
1953
1954 if (!NILP (Ftest_completion (Fminibuffer_contents (),
1955 Vminibuffer_completion_table,
1956 Vminibuffer_completion_predicate)))
1957 goto exit;
1958
1959 /* Call do_completion, but ignore errors. */
1960 SET_PT (ZV);
1961 val = internal_condition_case (complete_and_exit_1, Qerror,
1962 complete_and_exit_2);
1963
1964 i = XFASTINT (val);
1965 switch (i)
1966 {
1967 case 1:
1968 case 3:
1969 goto exit;
1970
1971 case 4:
1972 if (!NILP (Vminibuffer_completion_confirm))
1973 {
1974 temp_echo_area_glyphs (build_string (" [Confirm]"));
1975 return Qnil;
1976 }
1977 else
1978 goto exit;
1979
1980 default:
1981 return Qnil;
1982 }
1983 exit:
1984 return Fthrow (Qexit, Qnil);
1985 /* NOTREACHED */
1986 }
1987
1988 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
1989 0, 0, "",
1990 doc: /* Complete the minibuffer contents at most a single word.
1991 After one word is completed as much as possible, a space or hyphen
1992 is added, provided that matches some possible completion.
1993 Return nil if there is no valid completion, else t. */)
1994 ()
1995 {
1996 Lisp_Object completion, tem, tem1;
1997 register int i, i_byte;
1998 register const unsigned char *completion_string;
1999 struct gcpro gcpro1, gcpro2;
2000 int prompt_end_charpos = XINT (Fminibuffer_prompt_end ());
2001
2002 /* We keep calling Fbuffer_string rather than arrange for GC to
2003 hold onto a pointer to one of the strings thus made. */
2004
2005 completion = Ftry_completion (minibuffer_completion_contents (),
2006 Vminibuffer_completion_table,
2007 Vminibuffer_completion_predicate);
2008 if (NILP (completion))
2009 {
2010 bitch_at_user ();
2011 temp_echo_area_glyphs (build_string (" [No match]"));
2012 return Qnil;
2013 }
2014 if (EQ (completion, Qt))
2015 return Qnil;
2016
2017 #if 0 /* How the below code used to look, for reference. */
2018 tem = Fminibuffer_contents ();
2019 b = SDATA (tem);
2020 i = ZV - 1 - SCHARS (completion);
2021 p = SDATA (completion);
2022 if (i > 0 ||
2023 0 <= scmp (b, p, ZV - 1))
2024 {
2025 i = 1;
2026 /* Set buffer to longest match of buffer tail and completion head. */
2027 while (0 <= scmp (b + i, p, ZV - 1 - i))
2028 i++;
2029 del_range (1, i + 1);
2030 SET_PT (ZV);
2031 }
2032 #else /* Rewritten code */
2033 {
2034 int buffer_nchars, completion_nchars;
2035
2036 CHECK_STRING (completion);
2037 tem = minibuffer_completion_contents ();
2038 GCPRO2 (completion, tem);
2039 /* If reading a file name,
2040 expand any $ENVVAR refs in the buffer and in TEM. */
2041 if (! NILP (Vminibuffer_completing_file_name))
2042 {
2043 Lisp_Object substituted;
2044 substituted = Fsubstitute_in_file_name (tem);
2045 if (! EQ (substituted, tem))
2046 {
2047 tem = substituted;
2048 del_range (prompt_end_charpos, PT);
2049 Finsert (1, &tem);
2050 }
2051 }
2052 buffer_nchars = SCHARS (tem); /* # chars in what we completed. */
2053 completion_nchars = SCHARS (completion);
2054 i = buffer_nchars - completion_nchars;
2055 if (i > 0
2056 ||
2057 (tem1 = Fcompare_strings (tem, make_number (0),
2058 make_number (buffer_nchars),
2059 completion, make_number (0),
2060 make_number (buffer_nchars),
2061 completion_ignore_case ? Qt : Qnil),
2062 ! EQ (tem1, Qt)))
2063 {
2064 int start_pos;
2065
2066 /* Make buffer (before point) contain the longest match
2067 of TEM's tail and COMPLETION's head. */
2068 if (i <= 0) i = 1;
2069 start_pos= i;
2070 buffer_nchars -= i;
2071 while (i > 0)
2072 {
2073 tem1 = Fcompare_strings (tem, make_number (start_pos), Qnil,
2074 completion, make_number (0),
2075 make_number (buffer_nchars),
2076 completion_ignore_case ? Qt : Qnil);
2077 start_pos++;
2078 if (EQ (tem1, Qt))
2079 break;
2080 i++;
2081 buffer_nchars--;
2082 }
2083 del_range (start_pos, start_pos + buffer_nchars);
2084 }
2085 UNGCPRO;
2086 }
2087 #endif /* Rewritten code */
2088
2089 {
2090 int prompt_end_bytepos;
2091 prompt_end_bytepos = CHAR_TO_BYTE (prompt_end_charpos);
2092 i = PT - prompt_end_charpos;
2093 i_byte = PT_BYTE - prompt_end_bytepos;
2094 }
2095
2096 /* If completion finds next char not unique,
2097 consider adding a space or a hyphen. */
2098 if (i == SCHARS (completion))
2099 {
2100 GCPRO1 (completion);
2101 tem = Ftry_completion (concat2 (minibuffer_completion_contents (),
2102 build_string (" ")),
2103 Vminibuffer_completion_table,
2104 Vminibuffer_completion_predicate);
2105 UNGCPRO;
2106
2107 if (STRINGP (tem))
2108 completion = tem;
2109 else
2110 {
2111 GCPRO1 (completion);
2112 tem =
2113 Ftry_completion (concat2 (minibuffer_completion_contents (),
2114 build_string ("-")),
2115 Vminibuffer_completion_table,
2116 Vminibuffer_completion_predicate);
2117 UNGCPRO;
2118
2119 if (STRINGP (tem))
2120 completion = tem;
2121 }
2122 }
2123
2124 /* Now find first word-break in the stuff found by completion.
2125 i gets index in string of where to stop completing. */
2126 while (i_byte < SBYTES (completion))
2127 {
2128 int c;
2129
2130 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c, completion, i, i_byte);
2131 if (SYNTAX (c) != Sword)
2132 break;
2133 }
2134
2135 /* If got no characters, print help for user. */
2136
2137 if (i == PT - prompt_end_charpos)
2138 {
2139 if (!NILP (Vcompletion_auto_help))
2140 Fminibuffer_completion_help ();
2141 return Qnil;
2142 }
2143
2144 /* Otherwise insert in minibuffer the chars we got */
2145
2146 if (! NILP (Vminibuffer_completing_file_name)
2147 && SREF (completion, SBYTES (completion) - 1) == '/'
2148 && PT < ZV
2149 && FETCH_CHAR (PT_BYTE) == '/')
2150 {
2151 del_range (prompt_end_charpos, PT + 1);
2152 }
2153 else
2154 del_range (prompt_end_charpos, PT);
2155
2156 insert_from_string (completion, 0, 0, i, i_byte, 1);
2157 return Qt;
2158 }
2159 \f
2160 DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
2161 1, 1, 0,
2162 doc: /* Display the list of completions, COMPLETIONS, using `standard-output'.
2163 Each element may be just a symbol or string
2164 or may be a list of two strings to be printed as if concatenated.
2165 `standard-output' must be a buffer.
2166 The actual completion alternatives, as inserted, are given `mouse-face'
2167 properties of `highlight'.
2168 At the end, this runs the normal hook `completion-setup-hook'.
2169 It can find the completion buffer in `standard-output'. */)
2170 (completions)
2171 Lisp_Object completions;
2172 {
2173 Lisp_Object tail, elt;
2174 register int i;
2175 int column = 0;
2176 struct gcpro gcpro1, gcpro2;
2177 struct buffer *old = current_buffer;
2178 int first = 1;
2179
2180 /* Note that (when it matters) every variable
2181 points to a non-string that is pointed to by COMPLETIONS,
2182 except for ELT. ELT can be pointing to a string
2183 when terpri or Findent_to calls a change hook. */
2184 elt = Qnil;
2185 GCPRO2 (completions, elt);
2186
2187 if (BUFFERP (Vstandard_output))
2188 set_buffer_internal (XBUFFER (Vstandard_output));
2189
2190 if (NILP (completions))
2191 write_string ("There are no possible completions of what you have typed.",
2192 -1);
2193 else
2194 {
2195 write_string ("Possible completions are:", -1);
2196 for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++)
2197 {
2198 Lisp_Object tem, string;
2199 int length;
2200 Lisp_Object startpos, endpos;
2201
2202 startpos = Qnil;
2203
2204 elt = Fcar (tail);
2205 /* Compute the length of this element. */
2206 if (CONSP (elt))
2207 {
2208 tem = XCAR (elt);
2209 CHECK_STRING (tem);
2210 length = SCHARS (tem);
2211
2212 tem = Fcar (XCDR (elt));
2213 CHECK_STRING (tem);
2214 length += SCHARS (tem);
2215 }
2216 else
2217 {
2218 CHECK_STRING (elt);
2219 length = SCHARS (elt);
2220 }
2221
2222 /* This does a bad job for narrower than usual windows.
2223 Sadly, the window it will appear in is not known
2224 until after the text has been made. */
2225
2226 if (BUFFERP (Vstandard_output))
2227 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2228
2229 /* If the previous completion was very wide,
2230 or we have two on this line already,
2231 don't put another on the same line. */
2232 if (column > 33 || first
2233 /* If this is really wide, don't put it second on a line. */
2234 || (column > 0 && length > 45))
2235 {
2236 Fterpri (Qnil);
2237 column = 0;
2238 }
2239 /* Otherwise advance to column 35. */
2240 else
2241 {
2242 if (BUFFERP (Vstandard_output))
2243 {
2244 tem = Findent_to (make_number (35), make_number (2));
2245
2246 column = XINT (tem);
2247 }
2248 else
2249 {
2250 do
2251 {
2252 write_string (" ", -1);
2253 column++;
2254 }
2255 while (column < 35);
2256 }
2257 }
2258
2259 if (BUFFERP (Vstandard_output))
2260 {
2261 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2262 Fset_text_properties (startpos, endpos,
2263 Qnil, Vstandard_output);
2264 }
2265
2266 /* Output this element.
2267 If necessary, convert it to unibyte or to multibyte first. */
2268 if (CONSP (elt))
2269 string = Fcar (elt);
2270 else
2271 string = elt;
2272 if (NILP (current_buffer->enable_multibyte_characters)
2273 && STRING_MULTIBYTE (string))
2274 string = Fstring_make_unibyte (string);
2275 else if (!NILP (current_buffer->enable_multibyte_characters)
2276 && !STRING_MULTIBYTE (string))
2277 string = Fstring_make_multibyte (string);
2278
2279 if (BUFFERP (Vstandard_output))
2280 {
2281 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2282
2283 Fprinc (string, Qnil);
2284
2285 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2286
2287 Fput_text_property (startpos, endpos,
2288 Qmouse_face, intern ("highlight"),
2289 Vstandard_output);
2290 }
2291 else
2292 {
2293 Fprinc (string, Qnil);
2294 }
2295
2296 /* Output the annotation for this element. */
2297 if (CONSP (elt))
2298 {
2299 if (BUFFERP (Vstandard_output))
2300 {
2301 XSETINT (startpos, BUF_PT (XBUFFER (Vstandard_output)));
2302
2303 Fprinc (Fcar (Fcdr (elt)), Qnil);
2304
2305 XSETINT (endpos, BUF_PT (XBUFFER (Vstandard_output)));
2306
2307 Fset_text_properties (startpos, endpos, Qnil,
2308 Vstandard_output);
2309 }
2310 else
2311 {
2312 Fprinc (Fcar (Fcdr (elt)), Qnil);
2313 }
2314 }
2315
2316
2317 /* Update COLUMN for what we have output. */
2318 column += length;
2319
2320 /* If output is to a buffer, recompute COLUMN in a way
2321 that takes account of character widths. */
2322 if (BUFFERP (Vstandard_output))
2323 {
2324 tem = Fcurrent_column ();
2325 column = XINT (tem);
2326 }
2327
2328 first = 0;
2329 }
2330 }
2331
2332 UNGCPRO;
2333
2334 if (BUFFERP (Vstandard_output))
2335 set_buffer_internal (old);
2336
2337 if (!NILP (Vrun_hooks))
2338 call1 (Vrun_hooks, intern ("completion-setup-hook"));
2339
2340 return Qnil;
2341 }
2342
2343 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
2344 0, 0, "",
2345 doc: /* Display a list of possible completions of the current minibuffer contents. */)
2346 ()
2347 {
2348 Lisp_Object completions;
2349
2350 message ("Making completion list...");
2351 completions = Fall_completions (minibuffer_completion_contents (),
2352 Vminibuffer_completion_table,
2353 Vminibuffer_completion_predicate,
2354 Qt);
2355 clear_message (1, 0);
2356
2357 if (NILP (completions))
2358 {
2359 bitch_at_user ();
2360 temp_echo_area_glyphs (build_string (" [No completions]"));
2361 }
2362 else
2363 internal_with_output_to_temp_buffer ("*Completions*",
2364 Fdisplay_completion_list,
2365 Fsort (completions, Qstring_lessp));
2366 return Qnil;
2367 }
2368 \f
2369 DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
2370 doc: /* Terminate minibuffer input. */)
2371 ()
2372 {
2373 if (CHARACTERP (last_command_char))
2374 internal_self_insert (XINT (last_command_char), 0);
2375 else
2376 bitch_at_user ();
2377
2378 return Fthrow (Qexit, Qnil);
2379 }
2380
2381 DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
2382 doc: /* Terminate this minibuffer argument. */)
2383 ()
2384 {
2385 return Fthrow (Qexit, Qnil);
2386 }
2387
2388 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
2389 doc: /* Return current depth of activations of minibuffer, a nonnegative integer. */)
2390 ()
2391 {
2392 return make_number (minibuf_level);
2393 }
2394
2395 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
2396 doc: /* Return the prompt string of the currently-active minibuffer.
2397 If no minibuffer is active, return nil. */)
2398 ()
2399 {
2400 return Fcopy_sequence (minibuf_prompt);
2401 }
2402
2403 \f
2404 /* Temporarily display STRING at the end of the current
2405 minibuffer contents. This is used to display things like
2406 "[No Match]" when the user requests a completion for a prefix
2407 that has no possible completions, and other quick, unobtrusive
2408 messages. */
2409
2410 void
2411 temp_echo_area_glyphs (string)
2412 Lisp_Object string;
2413 {
2414 int osize = ZV;
2415 int osize_byte = ZV_BYTE;
2416 int opoint = PT;
2417 int opoint_byte = PT_BYTE;
2418 Lisp_Object oinhibit;
2419 oinhibit = Vinhibit_quit;
2420
2421 /* Clear out any old echo-area message to make way for our new thing. */
2422 message (0);
2423
2424 SET_PT_BOTH (osize, osize_byte);
2425 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 0);
2426 SET_PT_BOTH (opoint, opoint_byte);
2427 Vinhibit_quit = Qt;
2428 Fsit_for (make_number (2), Qnil, Qnil);
2429 del_range_both (osize, osize_byte, ZV, ZV_BYTE, 1);
2430 SET_PT_BOTH (opoint, opoint_byte);
2431 if (!NILP (Vquit_flag))
2432 {
2433 Vquit_flag = Qnil;
2434 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
2435 }
2436 Vinhibit_quit = oinhibit;
2437 }
2438
2439 DEFUN ("minibuffer-message", Fminibuffer_message, Sminibuffer_message,
2440 1, 1, 0,
2441 doc: /* Temporarily display STRING at the end of the minibuffer.
2442 The text is displayed for two seconds,
2443 or until the next input event arrives, whichever comes first. */)
2444 (string)
2445 Lisp_Object string;
2446 {
2447 CHECK_STRING (string);
2448 temp_echo_area_glyphs (string);
2449 return Qnil;
2450 }
2451 \f
2452 void
2453 init_minibuf_once ()
2454 {
2455 Vminibuffer_list = Qnil;
2456 staticpro (&Vminibuffer_list);
2457 }
2458
2459 void
2460 syms_of_minibuf ()
2461 {
2462 minibuf_level = 0;
2463 minibuf_prompt = Qnil;
2464 staticpro (&minibuf_prompt);
2465
2466 minibuf_save_list = Qnil;
2467 staticpro (&minibuf_save_list);
2468
2469 Qread_file_name_internal = intern ("read-file-name-internal");
2470 staticpro (&Qread_file_name_internal);
2471
2472 Qminibuffer_default = intern ("minibuffer-default");
2473 staticpro (&Qminibuffer_default);
2474 Fset (Qminibuffer_default, Qnil);
2475
2476 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
2477 staticpro (&Qminibuffer_completion_table);
2478
2479 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
2480 staticpro (&Qminibuffer_completion_confirm);
2481
2482 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
2483 staticpro (&Qminibuffer_completion_predicate);
2484
2485 staticpro (&last_exact_completion);
2486 last_exact_completion = Qnil;
2487
2488 staticpro (&last_minibuf_string);
2489 last_minibuf_string = Qnil;
2490
2491 Quser_variable_p = intern ("user-variable-p");
2492 staticpro (&Quser_variable_p);
2493
2494 Qminibuffer_history = intern ("minibuffer-history");
2495 staticpro (&Qminibuffer_history);
2496
2497 Qbuffer_name_history = intern ("buffer-name-history");
2498 staticpro (&Qbuffer_name_history);
2499 Fset (Qbuffer_name_history, Qnil);
2500
2501 Qminibuffer_setup_hook = intern ("minibuffer-setup-hook");
2502 staticpro (&Qminibuffer_setup_hook);
2503
2504 Qminibuffer_exit_hook = intern ("minibuffer-exit-hook");
2505 staticpro (&Qminibuffer_exit_hook);
2506
2507 Qhistory_length = intern ("history-length");
2508 staticpro (&Qhistory_length);
2509
2510 Qcurrent_input_method = intern ("current-input-method");
2511 staticpro (&Qcurrent_input_method);
2512
2513 Qactivate_input_method = intern ("activate-input-method");
2514 staticpro (&Qactivate_input_method);
2515
2516 DEFVAR_LISP ("read-buffer-function", &Vread_buffer_function,
2517 doc: /* If this is non-nil, `read-buffer' does its work by calling this function. */);
2518 Vread_buffer_function = Qnil;
2519
2520 DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
2521 doc: /* Normal hook run just after entry to minibuffer. */);
2522 Vminibuffer_setup_hook = Qnil;
2523
2524 DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook,
2525 doc: /* Normal hook run just after exit from minibuffer. */);
2526 Vminibuffer_exit_hook = Qnil;
2527
2528 DEFVAR_LISP ("history-length", &Vhistory_length,
2529 doc: /* *Maximum length for history lists before truncation takes place.
2530 A number means that length; t means infinite. Truncation takes place
2531 just after a new element is inserted. Setting the history-length
2532 property of a history variable overrides this default. */);
2533 XSETFASTINT (Vhistory_length, 30);
2534
2535 DEFVAR_LISP ("completion-auto-help", &Vcompletion_auto_help,
2536 doc: /* *Non-nil means automatically provide help for invalid completion input. */);
2537 Vcompletion_auto_help = Qt;
2538
2539 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
2540 doc: /* Non-nil means don't consider case significant in completion. */);
2541 completion_ignore_case = 0;
2542
2543 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
2544 doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
2545 This variable makes a difference whenever the minibuffer window is active. */);
2546 enable_recursive_minibuffers = 0;
2547
2548 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
2549 doc: /* Alist or obarray used for completion in the minibuffer.
2550 This becomes the ALIST argument to `try-completion' and `all-completion'.
2551
2552 The value may alternatively be a function, which is given three arguments:
2553 STRING, the current buffer contents;
2554 PREDICATE, the predicate for filtering possible matches;
2555 CODE, which says what kind of things to do.
2556 CODE can be nil, t or `lambda'.
2557 nil means to return the best completion of STRING, or nil if there is none.
2558 t means to return a list of all possible completions of STRING.
2559 `lambda' means to return t if STRING is a valid completion as it stands. */);
2560 Vminibuffer_completion_table = Qnil;
2561
2562 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
2563 doc: /* Within call to `completing-read', this holds the PREDICATE argument. */);
2564 Vminibuffer_completion_predicate = Qnil;
2565
2566 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
2567 doc: /* Non-nil means to demand confirmation of completion before exiting minibuffer. */);
2568 Vminibuffer_completion_confirm = Qnil;
2569
2570 DEFVAR_LISP ("minibuffer-completing-file-name",
2571 &Vminibuffer_completing_file_name,
2572 doc: /* Non-nil means completing file names. */);
2573 Vminibuffer_completing_file_name = Qnil;
2574
2575 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
2576 doc: /* Value that `help-form' takes on inside the minibuffer. */);
2577 Vminibuffer_help_form = Qnil;
2578
2579 DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable,
2580 doc: /* History list symbol to add minibuffer values to.
2581 Each string of minibuffer input, as it appears on exit from the minibuffer,
2582 is added with
2583 (set minibuffer-history-variable
2584 (cons STRING (symbol-value minibuffer-history-variable))) */);
2585 XSETFASTINT (Vminibuffer_history_variable, 0);
2586
2587 DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position,
2588 doc: /* Current position of redoing in the history list. */);
2589 Vminibuffer_history_position = Qnil;
2590
2591 DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise,
2592 doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
2593 Some uses of the echo area also raise that frame (since they use it too). */);
2594 minibuffer_auto_raise = 0;
2595
2596 DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
2597 doc: /* List of regexps that should restrict possible completions. */);
2598 Vcompletion_regexp_list = Qnil;
2599
2600 DEFVAR_BOOL ("minibuffer-allow-text-properties",
2601 &minibuffer_allow_text_properties,
2602 doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
2603 This also affects `read-string', but it does not affect `read-minibuffer',
2604 `read-no-blanks-input', or any of the functions that do minibuffer input
2605 with completion; they always discard text properties. */);
2606 minibuffer_allow_text_properties = 0;
2607
2608 DEFVAR_LISP ("minibuffer-prompt-properties", &Vminibuffer_prompt_properties,
2609 doc: /* Text properties that are added to minibuffer prompts.
2610 These are in addition to the basic `field' property, and stickiness
2611 properties. */);
2612 /* We use `intern' here instead of Qread_only to avoid
2613 initialization-order problems. */
2614 Vminibuffer_prompt_properties
2615 = Fcons (intern ("read-only"), Fcons (Qt, Qnil));
2616
2617 defsubr (&Sset_minibuffer_window);
2618 defsubr (&Sread_from_minibuffer);
2619 defsubr (&Seval_minibuffer);
2620 defsubr (&Sread_minibuffer);
2621 defsubr (&Sread_string);
2622 defsubr (&Sread_command);
2623 defsubr (&Sread_variable);
2624 defsubr (&Sread_buffer);
2625 defsubr (&Sread_no_blanks_input);
2626 defsubr (&Sminibuffer_depth);
2627 defsubr (&Sminibuffer_prompt);
2628
2629 defsubr (&Sminibufferp);
2630 defsubr (&Sminibuffer_prompt_end);
2631 defsubr (&Sminibuffer_contents);
2632 defsubr (&Sminibuffer_contents_no_properties);
2633 defsubr (&Sdelete_minibuffer_contents);
2634
2635 defsubr (&Stry_completion);
2636 defsubr (&Sall_completions);
2637 defsubr (&Stest_completion);
2638 defsubr (&Sassoc_string);
2639 defsubr (&Scompleting_read);
2640 defsubr (&Sminibuffer_complete);
2641 defsubr (&Sminibuffer_complete_word);
2642 defsubr (&Sminibuffer_complete_and_exit);
2643 defsubr (&Sdisplay_completion_list);
2644 defsubr (&Sminibuffer_completion_help);
2645
2646 defsubr (&Sself_insert_and_exit);
2647 defsubr (&Sexit_minibuffer);
2648
2649 defsubr (&Sminibuffer_message);
2650 }
2651
2652 void
2653 keys_of_minibuf ()
2654 {
2655 initial_define_key (Vminibuffer_local_map, Ctl ('g'),
2656 "abort-recursive-edit");
2657 initial_define_key (Vminibuffer_local_map, Ctl ('m'),
2658 "exit-minibuffer");
2659 initial_define_key (Vminibuffer_local_map, Ctl ('j'),
2660 "exit-minibuffer");
2661
2662 initial_define_key (Vminibuffer_local_ns_map, ' ',
2663 "exit-minibuffer");
2664 initial_define_key (Vminibuffer_local_ns_map, '\t',
2665 "exit-minibuffer");
2666 initial_define_key (Vminibuffer_local_ns_map, '?',
2667 "self-insert-and-exit");
2668
2669 initial_define_key (Vminibuffer_local_completion_map, '\t',
2670 "minibuffer-complete");
2671 initial_define_key (Vminibuffer_local_completion_map, ' ',
2672 "minibuffer-complete-word");
2673 initial_define_key (Vminibuffer_local_completion_map, '?',
2674 "minibuffer-completion-help");
2675
2676 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'),
2677 "minibuffer-complete-and-exit");
2678 initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'),
2679 "minibuffer-complete-and-exit");
2680 }