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