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