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