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