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