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