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