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