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