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