BSD 4_4 development
[unix-history] / usr / src / contrib / emacs-18.57 / src / minibuf.c
CommitLineData
c962cc99
C
1/* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1990 Free Software Foundation, Inc.
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include "config.h"
22#include "lisp.h"
23#include "commands.h"
24#include "buffer.h"
25#include "window.h"
26#include "syntax.h"
27#include "dispextern.h"
28
29#define min(a, b) ((a) < (b) ? (a) : (b))
30
31/* List of buffers for use as minibuffers.
32 The first element of the list is used for the outermost minibuffer invocation,
33 the next element is used for a recursive minibuffer invocation, etc.
34 The list is extended at the end as deeped minibuffer recursions are encountered. */
35Lisp_Object Vminibuffer_list;
36
37struct minibuf_save_data
38 {
39 char *prompt;
40 int prompt_width;
41 Lisp_Object help_form;
42 Lisp_Object current_prefix_arg;
43 };
44
45int minibuf_save_vector_size;
46struct minibuf_save_data *minibuf_save_vector;
47
48/* Depth in minibuffer invocations. */
49int minibuf_level;
50
51/* Nonzero means display completion help for invalid input. */
52int completion_auto_help;
53
54/* Fread_minibuffer leaves the input, as a string, here. */
55Lisp_Object last_minibuf_string;
56
57/* Nonzero means let functions called when within a minibuffer
58 invoke recursive minibuffers (to read arguments, or whatever). */
59int enable_recursive_minibuffers;
60
61/* help-form is bound to this while in the minibuffer. */
62Lisp_Object Vminibuffer_help_form;
63
64/* Nonzero means completion ignores case. */
65int completion_ignore_case;
66
67Lisp_Object Quser_variable_p;
68
69/* Width in columns of current minibuffer prompt. */
70extern int minibuf_prompt_width;
71\f
72/* Actual minibuffer invocation. */
73
74void read_minibuf_unwind ();
75Lisp_Object get_minibuffer ();
76Lisp_Object read_minibuf ();
77
78Lisp_Object
79read_minibuf (map, initial, prompt, expflag)
80 Lisp_Object map;
81 Lisp_Object initial;
82 Lisp_Object prompt;
83 int expflag;
84{
85 register Lisp_Object val;
86 int count = specpdl_ptr - specpdl;
87 struct gcpro gcpro1, gcpro2;
88
89 if (XTYPE (prompt) != Lisp_String)
90 prompt = build_string ("");
91
92 /* Emacs in -batch mode calls minibuffer: print the prompt. */
93 if (noninteractive)
94 printf ("%s", XSTRING (prompt)->data);
95
96 if (!enable_recursive_minibuffers &&
97 (EQ (selected_window, minibuf_window)))
98 error ("Command attempted to use minibuffer while in minibuffer");
99
100 if (minibuf_level == minibuf_save_vector_size)
101 minibuf_save_vector =
102 (struct minibuf_save_data *) xrealloc (minibuf_save_vector,
103 (minibuf_save_vector_size *= 2) * sizeof (struct minibuf_save_data));
104 minibuf_save_vector[minibuf_level].prompt = minibuf_prompt;
105 minibuf_save_vector[minibuf_level].prompt_width = minibuf_prompt_width;
106 minibuf_prompt_width = 0;
107 /* >> Why is this done this way rather than binding these variables? */
108 minibuf_save_vector[minibuf_level].help_form = Vhelp_form;
109 minibuf_save_vector[minibuf_level].current_prefix_arg = Vcurrent_prefix_arg;
110 GCPRO2 (minibuf_save_vector[minibuf_level].help_form,
111 minibuf_save_vector[minibuf_level].current_prefix_arg);
112
113
114 record_unwind_protect (Fset_window_configuration,
115 Fcurrent_window_configuration ());
116
117 val = current_buffer->directory;
118 Fset_buffer (get_minibuffer (minibuf_level));
119 current_buffer->directory = val;
120
121 Fset_window_buffer (minibuf_window, Fcurrent_buffer ());
122 Fselect_window (minibuf_window);
123 XFASTINT (XWINDOW (minibuf_window)->hscroll) = 0;
124
125 Ferase_buffer ();
126 minibuf_level++;
127 record_unwind_protect (read_minibuf_unwind, Qnil);
128 Vminibuf_scroll_window = Qnil;
129
130 if (!NULL (initial))
131 Finsert (1, &initial);
132
133 minibuf_prompt = (char *) alloca (XSTRING (prompt)->size + 1);
134 bcopy (XSTRING (prompt)->data, minibuf_prompt, XSTRING (prompt)->size + 1);
135 echo_area_contents = 0;
136
137 Vhelp_form = Vminibuffer_help_form;
138 current_buffer->keymap = map;
139 recursive_edit_1 ();
140
141 /* If cursor is on the minibuffer line,
142 show the user we have exited by putting it in column 0. */
143 if (cursor_vpos >= XFASTINT (XWINDOW (minibuf_window)->top)
144 && !noninteractive)
145 {
146 cursor_hpos = 0;
147 update_screen (1, 1);
148 }
149
150 /* Make minibuffer contents into a string */
151 val = make_string (BEG_ADDR, Z - BEG);
152 bcopy (GAP_END_ADDR, XSTRING (val)->data + GPT - BEG, Z - GPT);
153 unbind_to (count);
154 UNGCPRO;
155
156 /* VAL is the string of minibuffer text. */
157
158 last_minibuf_string = val;
159
160 /* If Lisp form desired instead of string, parse it */
161 if (expflag)
162 val = Fread (val);
163
164 return val;
165}
166
167/* Return a buffer to be used as the minibuffer at depth `depth'.
168 depth = 0 is the lowest allowed argument, and that is the value
169 used for nonrecursive minibuffer invocations */
170
171Lisp_Object
172get_minibuffer (depth)
173 int depth;
174{
175 Lisp_Object tail, num, buf;
176 char name[14];
177 extern Lisp_Object nconc2 ();
178
179 XFASTINT (num) = depth;
180 tail = Fnthcdr (num, Vminibuffer_list);
181 if (NULL (tail))
182 {
183 tail = Fcons (Qnil, Qnil);
184 Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
185 }
186 buf = Fcar (tail);
187 if (NULL (buf) || NULL (XBUFFER (buf)->name))
188 {
189 sprintf (name, " *Minibuf-%d*", depth);
190 buf = Fget_buffer_create (build_string (name));
191 XCONS (tail)->car = buf;
192 }
193 else
194 reset_buffer (XBUFFER (buf));
195 return buf;
196}
197
198/* This function is called on exiting minibuffer, whether normally or not,
199 and it restores the current window, buffer, etc. */
200
201void
202read_minibuf_unwind ()
203{
204 /* Erase the minibuffer we were using at this level. */
205 Fset_buffer (XWINDOW (minibuf_window)->buffer);
206 Ferase_buffer ();
207
208 /* If this was a recursive minibuffer,
209 tie the minibuffer window back to the outer level minibuffer buffer */
210 minibuf_level--;
211 /* Make sure minibuffer window is erased, not ignored */
212 windows_or_buffers_changed++;
213 XFASTINT (XWINDOW (minibuf_window)->last_modified) = 0;
214
215 /* Restore prompt from outer minibuffer */
216 minibuf_prompt = minibuf_save_vector[minibuf_level].prompt;
217 minibuf_prompt_width = minibuf_save_vector[minibuf_level].prompt_width;
218 Vhelp_form = minibuf_save_vector[minibuf_level].help_form;
219 Vcurrent_prefix_arg = minibuf_save_vector[minibuf_level].current_prefix_arg;
220}
221\f
222DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 4, 0,
223 "Read a string from the minibuffer, prompting with string PROMPT.\n\
224If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
225 to be inserted into the minibuffer before reading input.\n\
226Third arg KEYMAP is a keymap to use whilst reading; the default is\n\
227 minibuffer-local-map.\n\
228If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
229 and return that object (ie (car (read-from-string <input-string>)))")
230 (prompt, initial_input, keymap, read)
231 Lisp_Object prompt, initial_input, keymap, read;
232{
233 CHECK_STRING (prompt, 0);
234 if (!NULL (initial_input))
235 CHECK_STRING (initial_input, 1);
236 if (NULL (keymap))
237 keymap = Vminibuffer_local_map;
238 else
239 keymap = get_keymap (keymap,2);
240 return read_minibuf (keymap, initial_input, prompt, !NULL(read));
241}
242
243DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0,
244 "Return a Lisp object read using the minibuffer.\n\
245Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
246is a string to insert in the minibuffer before reading.")
247 (prompt, initial_contents)
248 Lisp_Object prompt, initial_contents;
249{
250 CHECK_STRING (prompt, 0);
251 if (!NULL (initial_contents))
252 CHECK_STRING (initial_contents, 1)
253 return read_minibuf (Vminibuffer_local_map, initial_contents, prompt, 1);
254}
255
256DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0,
257 "Return value of Lisp expression read using the minibuffer.\n\
258Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
259is a string to insert in the minibuffer before reading.")
260 (prompt, initial_contents)
261 Lisp_Object prompt, initial_contents;
262{
263 return Feval (Fread_minibuffer (prompt, initial_contents));
264}
265
266/* Functions that use the minibuffer to read various things. */
267
268DEFUN ("read-string", Fread_string, Sread_string, 1, 2, 0,
269 "Read a string from the minibuffer, prompting with string PROMPT.\n\
270If non-nil second arg INITIAL-INPUT is a string to insert before reading.")
271 (prompt, initial_input)
272 Lisp_Object prompt, initial_input;
273{
274 return Fread_from_minibuffer (prompt, initial_input, Qnil, Qnil);
275}
276
277DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 2, 2, 0,
278 "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\
279Prompt with PROMPT, and provide INIT as an initial value of the input string.")
280 (prompt, init)
281 Lisp_Object prompt, init;
282{
283 CHECK_STRING (prompt, 0);
284 CHECK_STRING (init, 1);
285
286 return read_minibuf (Vminibuffer_local_ns_map, init, prompt, 0);
287}
288
289DEFUN ("read-command", Fread_command, Sread_command, 1, 1, 0,
290 "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\
291Prompts with PROMPT.")
292 (prompt)
293 Lisp_Object prompt;
294{
295 return Fintern (Fcompleting_read (prompt, Vobarray, Qcommandp, Qt, Qnil),
296 Qnil);
297}
298
299#ifdef NOTDEF
300DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
301 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
302Prompts with PROMPT.")
303 (prompt)
304 Lisp_Object prompt;
305{
306 return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil),
307 Qnil);
308}
309#endif /* NOTDEF */
310
311DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 1, 0,
312 "One arg PROMPT, a string. Read the name of a user variable and return\n\
313it as a symbol. Prompts with PROMPT.\n\
314A user variable is one whose documentation starts with a \"*\" character.")
315 (prompt)
316 Lisp_Object prompt;
317{
318 return Fintern (Fcompleting_read (prompt, Vobarray,
319 Quser_variable_p, Qt, Qnil),
320 Qnil);
321}
322
323DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0,
324 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
325Prompts with PROMPT.\n\
326Optional second arg is value to return if user enters an empty line.\n\
327If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
328 (prompt, def, require_match)
329 Lisp_Object prompt, def, require_match;
330{
331 Lisp_Object tem;
332 Lisp_Object args[3];
333 struct gcpro gcpro1;
334
335 if (XTYPE (def) == Lisp_Buffer)
336 def = XBUFFER (def)->name;
337 if (!NULL (def))
338 {
339 args[0] = build_string ("%s(default %s) ");
340 args[1] = prompt;
341 args[2] = def;
342 prompt = Fformat (3, args);
343 }
344 GCPRO1 (def);
345 tem = Fcompleting_read (prompt, Vbuffer_alist, Qnil, require_match, Qnil);
346 UNGCPRO;
347 if (XSTRING (tem)->size)
348 return tem;
349 return def;
350}
351\f
352DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
353 "Return common substring of all completions of STRING in ALIST.\n\
354Each car of each element of ALIST is tested to see if it begins with STRING.\n\
355All that match are compared together; the longest initial sequence\n\
356common to all matches is returned as a string.\n\
357If there is no match at all, nil is returned.\n\
358For an exact match, t is returned.\n\
359\n\
360ALIST can be an obarray instead of an alist.\n\
361Then the print names of all symbols in the obarray are the possible matches.\n\
362\n\
363If optional third argument PREDICATE is non-nil,\n\
364it is used to test each possible match.\n\
365The match is a candidate only if PREDICATE returns non-nil.\n\
366The argument given to PREDICATE is the alist element or the symbol from the obarray.")
367 (string, alist, pred)
368 Lisp_Object string, alist, pred;
369{
370 Lisp_Object bestmatch, tail, elt, eltstring;
371 int bestmatchsize;
372 int compare, matchsize;
373 int list = CONSP (alist) || NULL (alist);
374 int index, obsize;
375 int matchcount = 0;
376 Lisp_Object bucket, zero, end, tem;
377 struct gcpro gcpro1, gcpro2, gcpro3;
378
379 CHECK_STRING (string, 0);
380 if (!list && XTYPE (alist) != Lisp_Vector)
381 return call3 (alist, string, pred, Qnil);
382
383 bestmatch = Qnil;
384
385 if (list)
386 tail = alist;
387 else
388 {
389 index = 0;
390 obsize = XVECTOR (alist)->size;
391 bucket = XVECTOR (alist)->contents[index];
392 }
393
394 while (1)
395 {
396 /* Get the next element of the alist or obarray. */
397 /* Exit the loop if the elements are all used up. */
398 /* elt gets the alist element or symbol.
399 eltstring gets the name to check as a completion. */
400
401 if (list)
402 {
403 if (NULL (tail))
404 break;
405 elt = Fcar (tail);
406 eltstring = Fcar (elt);
407 tail = Fcdr (tail);
408 }
409 else
410 {
411 if (XFASTINT (bucket) != 0)
412 {
413 elt = bucket;
414 eltstring = Fsymbol_name (elt);
415 if (XSYMBOL (bucket)->next)
416 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
417 else
418 XFASTINT (bucket) = 0;
419 }
420 else if (++index >= obsize)
421 break;
422 else
423 {
424 bucket = XVECTOR (alist)->contents[index];
425 continue;
426 }
427 }
428
429 /* Is this element a possible completion? */
430
431 if (XTYPE (eltstring) == Lisp_String &&
432 XSTRING (string)->size <= XSTRING (eltstring)->size &&
433 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
434 XSTRING (string)->size))
435 {
436 /* Yes. */
437 /* Ignore this element if there is a predicate
438 and the predicate doesn't like it. */
439
440 if (!NULL (pred))
441 {
442 if (EQ (pred, Qcommandp))
443 tem = Fcommandp (elt);
444 else
445 {
446 GCPRO3 (string, eltstring, bestmatch);
447 tem = call1 (pred, elt);
448 UNGCPRO;
449 }
450 if (NULL (tem)) continue;
451 }
452
453 /* Update computation of how much all possible completions match */
454
455 matchcount++;
456 if (NULL (bestmatch))
457 bestmatch = eltstring, bestmatchsize = XSTRING (eltstring)->size;
458 else
459 {
460 compare = min (bestmatchsize, XSTRING (eltstring)->size);
461 matchsize = scmp (XSTRING (bestmatch)->data,
462 XSTRING (eltstring)->data,
463 compare);
464 bestmatchsize = (matchsize >= 0) ? matchsize : compare;
465 }
466 }
467 }
468
469 if (NULL (bestmatch))
470 return Qnil; /* No completions found */
471 if (matchcount == 1 && bestmatchsize == XSTRING (string)->size)
472 return Qt;
473
474 XFASTINT (zero) = 0; /* Else extract the part in which */
475 XFASTINT (end) = bestmatchsize; /* all completions agree */
476 return Fsubstring (bestmatch, zero, end);
477}
478
479/* Compare exactly LEN chars of strings at S1 and S2,
480 ignoring case if appropriate.
481 Return -1 if strings match,
482 else number of chars that match at the beginning. */
483
484scmp (s1, s2, len)
485 register char *s1, *s2;
486 int len;
487{
488 register int l = len;
489
490 if (completion_ignore_case)
491 {
492 while (l && downcase_table[*s1++] == downcase_table[*s2++])
493 l--;
494 }
495 else
496 {
497 while (l && *s1++ == *s2++)
498 l--;
499 }
500 if (l == 0)
501 return -1;
502 else return len - l;
503}
504\f
505DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 3, 0,
506 "Search for partial matches to STRING in ALIST.\n\
507Each car of each element of ALIST is tested to see if it begins with STRING.\n\
508The value is a list of all the strings from ALIST that match.\n\
509ALIST can be an obarray instead of an alist.\n\
510Then the print names of all symbols in the obarray are the possible matches.\n\
511\n\
512If optional third argument PREDICATE is non-nil,\n\
513it is used to test each possible match.\n\
514The match is a candidate only if PREDICATE returns non-nil.\n\
515The argument given to PREDICATE is the alist element or the symbol from the obarray.")
516 (string, alist, pred)
517 Lisp_Object string, alist, pred;
518{
519 Lisp_Object tail, elt, eltstring;
520 Lisp_Object allmatches;
521 int list = CONSP (alist) || NULL (alist);
522 int index, obsize;
523 Lisp_Object bucket, tem;
524 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
525
526 CHECK_STRING (string, 0);
527 if (!list && XTYPE (alist) != Lisp_Vector)
528 {
529 return call3 (alist, string, pred, Qt);
530 }
531 allmatches = Qnil;
532
533 /* If ALIST is not a list, set TAIL just for gc pro. */
534 tail = alist;
535 if (! list)
536 {
537 index = 0;
538 obsize = XVECTOR (alist)->size;
539 bucket = XVECTOR (alist)->contents[index];
540 }
541
542 while (1)
543 {
544 /* Get the next element of the alist or obarray. */
545 /* Exit the loop if the elements are all used up. */
546 /* elt gets the alist element or symbol.
547 eltstring gets the name to check as a completion. */
548
549 if (list)
550 {
551 if (NULL (tail))
552 break;
553 elt = Fcar (tail);
554 eltstring = Fcar (elt);
555 tail = Fcdr (tail);
556 }
557 else
558 {
559 if (XFASTINT (bucket) != 0)
560 {
561 elt = bucket;
562 eltstring = Fsymbol_name (elt);
563 if (XSYMBOL (bucket)->next)
564 XSETSYMBOL (bucket, XSYMBOL (bucket)->next);
565 else
566 XFASTINT (bucket) = 0;
567 }
568 else if (++index >= obsize)
569 break;
570 else
571 {
572 bucket = XVECTOR (alist)->contents[index];
573 continue;
574 }
575 }
576
577 /* Is this element a possible completion? */
578
579 if (XTYPE (eltstring) == Lisp_String &&
580 XSTRING (string)->size <= XSTRING (eltstring)->size &&
581 XSTRING (eltstring)->data[0] != ' ' &&
582 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data,
583 XSTRING (string)->size))
584 {
585 /* Yes. */
586 /* Ignore this element if there is a predicate
587 and the predicate doesn't like it. */
588
589 if (!NULL (pred))
590 {
591 if (EQ (pred, Qcommandp))
592 tem = Fcommandp (elt);
593 else
594 {
595 GCPRO4 (tail, eltstring, allmatches, string);
596 tem = call1 (pred, elt);
597 UNGCPRO;
598 }
599 if (NULL (tem)) continue;
600 }
601 /* Ok => put it on the list. */
602 allmatches = Fcons (eltstring, allmatches);
603 }
604 }
605
606 return Fnreverse (allmatches);
607}
608\f
609Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table;
610Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate;
611Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm;
612
613DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 5, 0,
614 "Read a string in the minibuffer, with completion.\n\
615Args are PROMPT, TABLE, PREDICATE, REQUIRE-MATCH and INITIAL-INPUT.\n\
616PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
617TABLE is an alist whose elements' cars are strings, or an obarray (see try-completion).\n\
618PREDICATE limits completion to a subset of TABLE; see try-completion for details.\n\
619If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
620 the input is (or completes to) an element of TABLE.\n\
621 If it is also not t, Return does not exit if it does non-null completion.\n\
622If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
623Case is ignored if ambient value of completion-ignore-case is non-nil.")
624 (prompt, table, pred, require_match, init)
625 Lisp_Object prompt, table, pred, require_match, init;
626{
627 Lisp_Object val;
628 int count = specpdl_ptr - specpdl;
629 specbind (Qminibuffer_completion_table, table);
630 specbind (Qminibuffer_completion_predicate, pred);
631 specbind (Qminibuffer_completion_confirm,
632 EQ (require_match, Qt) ? Qnil : Qt);
633 val = read_minibuf (NULL (require_match)
634 ? Vminibuffer_local_completion_map
635 : Vminibuffer_local_must_match_map,
636 init, prompt, 0);
637 unbind_to (count);
638 return val;
639}
640\f
641temp_echo_area_contents (m)
642 char *m;
643{
644 int osize = ZV;
645 Lisp_Object oinhibit;
646 oinhibit = Vinhibit_quit;
647
648 SET_PT (osize);
649 InsStr (m);
650 SET_PT (osize);
651 Vinhibit_quit = Qt;
652 Fsit_for (make_number (2), Qnil);
653 del_range (point, ZV);
654 if (!NULL (Vquit_flag))
655 {
656 Vquit_flag = Qnil;
657 unread_command_char = Ctl ('g');
658 }
659 Vinhibit_quit = oinhibit;
660}
661
662Lisp_Object Fminibuffer_completion_help ();
663
664/* returns:
665 * 0 no possible completion
666 * 1 was already an exact and unique completion
667 * 3 was already an exact completion
668 * 4 completed to an exact completion
669 * 5 some completion happened
670 * 6 no completion happened
671 */
672int
673do_completion ()
674{
675 Lisp_Object completion, tem;
676 int completedp;
677
678 completion = Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table,
679 Vminibuffer_completion_predicate);
680 if (NULL (completion))
681 {
682 bell ();
683 temp_echo_area_contents (" [No match]");
684 return 0;
685 }
686
687 if (EQ (completion, Qt)) /* exact and unique match */
688 return 1;
689
690 /* compiler bug */
691 tem = Fstring_equal (completion, Fbuffer_string());
692 if (completedp = NULL (tem))
693 {
694 Ferase_buffer (); /* Some completion happened */
695 Finsert (1, &completion);
696 }
697
698 /* It did find a match. Do we match some possibility exactly now? */
699 if (CONSP (Vminibuffer_completion_table)
700 || NULL (Vminibuffer_completion_table))
701 tem = Fassoc (Fbuffer_string (), Vminibuffer_completion_table);
702 else if (XTYPE (Vminibuffer_completion_table) == Lisp_Vector)
703 {
704 /* the primitive used by Fintern_soft */
705 extern Lisp_Object oblookup ();
706
707 tem = Fbuffer_string ();
708 /* Bypass intern-soft as that loses for nil */
709 tem = oblookup (Vminibuffer_completion_table,
710 XSTRING (tem)->data, XSTRING (tem)->size);
711 if (XTYPE (tem) != Lisp_Symbol)
712 tem = Qnil;
713 else if (!NULL (Vminibuffer_completion_predicate))
714 tem = call1 (Vminibuffer_completion_predicate, tem);
715 else
716 tem = Qt;
717 }
718 else
719 tem = call3 (Vminibuffer_completion_table,
720 Fbuffer_string (),
721 Vminibuffer_completion_predicate,
722 Qlambda);
723
724 if (NULL (tem))
725 { /* not an exact match */
726 if (completedp)
727 return 5;
728 else if (completion_auto_help)
729 Fminibuffer_completion_help ();
730 else
731 temp_echo_area_contents (" [Next char not unique]");
732 return 6;
733 }
734 else
735 return (completedp ? 4 : 3);
736}
737
738
739DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "",
740 "Complete the minibuffer contents as far as possible.")
741 ()
742{
743 register int i = do_completion ();
744 switch (i)
745 {
746 case 0:
747 return Qnil;
748
749 case 1:
750 temp_echo_area_contents(" [Sole completion]");
751 break;
752
753 case 3:
754 temp_echo_area_contents(" [Complete, but not unique]");
755 break;
756 }
757 return Qt;
758}
759
760DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit,
761 Sminibuffer_complete_and_exit, 0, 0, "",
762 "Complete the minibuffer contents, and maybe exit.\n\
763Exit if the name is valid with no completion needed.\n\
764If name was completed to a valid match,\n\
765a repetition of this command will exit.")
766 ()
767{
768 register int i;
769
770 /* Allow user to specify null string */
771 if (BEGV == ZV)
772 goto exit;
773
774 i = do_completion ();
775 switch (i)
776 {
777 case 1:
778 case 3:
779 goto exit;
780
781 case 4:
782 if (!NULL (Vminibuffer_completion_confirm))
783 {
784 temp_echo_area_contents(" [Confirm]");
785 return Qnil;
786 }
787 else
788 goto exit;
789
790 default:
791 return Qnil;
792 }
793 exit:
794 Fthrow (Qexit, Qnil);
795 /* NOTREACHED */
796}
797
798DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word,
799 0, 0, "",
800 "Complete the minibuffer contents at most a single word.")
801 ()
802{
803 Lisp_Object completion, tem;
804 register int i;
805 register unsigned char *completion_string;
806 /* We keep calling Fbuffer_string
807 rather than arrange for GC to hold onto a pointer to
808 one of the strings thus made. */
809
810 completion = Ftry_completion (Fbuffer_string (),
811 Vminibuffer_completion_table,
812 Vminibuffer_completion_predicate);
813 if (NULL (completion))
814 {
815 bell ();
816 temp_echo_area_contents (" [No match]");
817 return Qnil;
818 }
819 if (EQ (completion, Qt))
820 return Qnil;
821
822#if 0 /* How the below code used to look, for reference */
823 tem = Fbuffer_string ();
824 b = XSTRING (tem)->data;
825 i = ZV - 1 - XSTRING (completion)->size;
826 p = XSTRING (completion)->data;
827 if (i > 0 ||
828 0 <= scmp (b, p, ZV - 1))
829 {
830 i = 1;
831 /* Set buffer to longest match of buffer tail and completion head. */
832 while (0 <= scmp (b + i, p, ZV - 1 - i))
833 i++;
834 del_range (1, i + 1);
835 SET_PT (ZV);
836 }
837#else /* Rewritten code */
838 {
839 register unsigned char *buffer_string;
840 int buffer_length, completion_length;
841
842 tem = Fbuffer_string ();
843 buffer_string = XSTRING (tem)->data;
844 completion_string = XSTRING (completion)->data;
845 buffer_length = XSTRING (tem)->size; /* ie ZV - BEGV */
846 completion_length = XSTRING (completion)->size;
847 i = buffer_length - completion_length;
848 /* Mly: I don't understand what this is supposed to do AT ALL */
849 if (i > 0 ||
850 0 <= scmp (buffer_string, completion_string, buffer_length))
851 {
852 /* Set buffer to longest match of buffer tail and completion head. */
853 if (i <= 0) i = 1;
854 buffer_string += i;
855 buffer_length -= i;
856 while (0 <= scmp (buffer_string++, completion_string, buffer_length--))
857 i++;
858 del_range (1, i + 1);
859 SET_PT (ZV);
860 }
861 }
862#endif /* Rewritten code */
863 i = ZV - BEGV;
864
865 /* If completion finds next char not unique,
866 consider adding a space or a hyphen */
867 if (i == XSTRING (completion)->size)
868 {
869 tem = Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")),
870 Vminibuffer_completion_table,
871 Vminibuffer_completion_predicate);
872 if (XTYPE (tem) == Lisp_String)
873 completion = tem;
874 else
875 {
876 tem = Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")),
877 Vminibuffer_completion_table,
878 Vminibuffer_completion_predicate);
879 if (XTYPE (tem) == Lisp_String)
880 completion = tem;
881 }
882 }
883
884 /* Now find first word-break in the stuff found by completion.
885 i gets index in string of where to stop completing. */
886 completion_string = XSTRING (completion)->data;
887
888 for (; i < XSTRING (completion)->size; i++)
889 if (SYNTAX (completion_string[i]) != Sword) break;
890 if (i < XSTRING (completion)->size)
891 i = i + 1;
892
893 /* If got no characters, print help for user. */
894
895 if (i == ZV - BEGV)
896 {
897 if (completion_auto_help)
898 Fminibuffer_completion_help ();
899 return Qnil;
900 }
901
902 /* Otherwise insert in minibuffer the chars we got */
903
904 Ferase_buffer ();
905 insert (completion_string, i);
906 return Qt;
907}
908\f
909DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list,
910 1, 1, 0,
911 "Display in a buffer the list of completions, COMPLETIONS.\n\
912Each element may be just a symbol or string\n\
913or may be a list of two strings to be printed as if concatenated.")
914 (completions)
915 Lisp_Object completions;
916{
917 register Lisp_Object tail, elt;
918 register int i;
919 struct buffer *old = current_buffer;
920 /* No GCPRO needed, since (when it matters) every variable
921 points to a non-string that is pointed to by COMPLETIONS. */
922
923 set_buffer_internal (XBUFFER (Vstandard_output));
924
925 if (NULL (completions))
926 InsStr ("There are no possible completions of what you have typed.");
927 else
928 {
929 InsStr ("Possible completions are:");
930 for (tail = completions, i = 0; !NULL (tail); tail = Fcdr (tail), i++)
931 {
932 /* this needs fixing for the case of long completions
933 and/or narrow windows */
934 /* Sadly, the window it will appear in is not known
935 until after the text has been made. */
936 if (i & 1)
937 Findent_to (make_number (35), make_number (1));
938 else
939 Fterpri (Qnil);
940 elt = Fcar (tail);
941 if (CONSP (elt))
942 {
943 Fprinc (Fcar (elt), Qnil);
944 Fprinc (Fcar (Fcdr (elt)), Qnil);
945 }
946 else
947 Fprinc (elt, Qnil);
948 }
949 }
950 set_buffer_internal (old);
951 return Qnil;
952}
953
954DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help,
955 0, 0, "",
956 "Display a list of possible completions of the current minibuffer contents.")
957 ()
958{
959 Lisp_Object completions;
960 message ("Making completion list...");
961 completions = Fall_completions (Fbuffer_string (), Vminibuffer_completion_table,
962 Vminibuffer_completion_predicate);
963 echo_area_contents = 0;
964 if (NULL (completions))
965 {
966 bell ();
967 temp_echo_area_contents (" [No completions]");
968 }
969 else
970 internal_with_output_to_temp_buffer (" *Completions*",
971 Fdisplay_completion_list,
972 Fsort (completions, Qstring_lessp));
973 return Qnil;
974}
975\f
976DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "",
977 "Terminate minibuffer input.")
978 ()
979{
980 self_insert_internal (last_command_char, 0);
981 Fthrow (Qexit, Qnil);
982}
983
984DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "",
985 "Terminate this minibuffer argument.")
986 ()
987{
988 Fthrow (Qexit, Qnil);
989}
990
991DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
992 "Return current depth of activations of minibuffer, a nonnegative integer.")
993 ()
994{
995 return make_number (minibuf_level);
996}
997
998\f
999init_minibuf_once ()
1000{
1001 Vminibuffer_list = Qnil;
1002 staticpro (&Vminibuffer_list);
1003}
1004
1005syms_of_minibuf ()
1006{
1007 minibuf_level = 0;
1008 minibuf_prompt = 0;
1009 minibuf_save_vector_size = 5;
1010 minibuf_save_vector = (struct minibuf_save_data *) malloc (5 * sizeof (struct minibuf_save_data));
1011
1012 Qminibuffer_completion_table = intern ("minibuffer-completion-table");
1013 staticpro (&Qminibuffer_completion_table);
1014
1015 Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm");
1016 staticpro (&Qminibuffer_completion_confirm);
1017
1018 Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate");
1019 staticpro (&Qminibuffer_completion_predicate);
1020
1021 staticpro (&last_minibuf_string);
1022 last_minibuf_string = Qnil;
1023
1024 Quser_variable_p = intern ("user-variable-p");
1025 staticpro (&Quser_variable_p);
1026
1027
1028
1029 DEFVAR_BOOL ("completion-auto-help", &completion_auto_help,
1030 "*Non-nil means automatically provide help for invalid completion input.");
1031 completion_auto_help = 1;
1032
1033 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
1034 "Non-nil means don't consider case significant in completion.");
1035 completion_ignore_case = 0;
1036
1037 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers,
1038 "*Non-nil means to allow minibuffers to invoke commands which use\n\
1039recursive minibuffers.");
1040 enable_recursive_minibuffers = 0;
1041
1042 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table,
1043 "Alist or obarray used for completion in the minibuffer.");
1044 Vminibuffer_completion_table = Qnil;
1045
1046 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate,
1047 "Holds PREDICATE argument to completing-read.");
1048 Vminibuffer_completion_predicate = Qnil;
1049
1050 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm,
1051 "Non-nil => demand confirmation of completion before exiting minibuffer.");
1052 Vminibuffer_completion_confirm = Qnil;
1053
1054 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form,
1055 "Value that help-form takes on inside the minibuffer.");
1056 Vminibuffer_help_form = Qnil;
1057
1058 defsubr (&Sread_from_minibuffer);
1059 defsubr (&Seval_minibuffer);
1060 defsubr (&Sread_minibuffer);
1061 defsubr (&Sread_string);
1062 defsubr (&Sread_command);
1063 defsubr (&Sread_variable);
1064 defsubr (&Sread_buffer);
1065 defsubr (&Sread_no_blanks_input);
1066 defsubr (&Sminibuffer_depth);
1067
1068 defsubr (&Stry_completion);
1069 defsubr (&Sall_completions);
1070 defsubr (&Scompleting_read);
1071 defsubr (&Sminibuffer_complete);
1072 defsubr (&Sminibuffer_complete_word);
1073 defsubr (&Sminibuffer_complete_and_exit);
1074 defsubr (&Sdisplay_completion_list);
1075 defsubr (&Sminibuffer_completion_help);
1076
1077 defsubr (&Sself_insert_and_exit);
1078 defsubr (&Sexit_minibuffer);
1079
1080}
1081
1082keys_of_minibuf ()
1083{
1084 ndefkey (Vminibuffer_local_map, Ctl ('g'), "abort-recursive-edit");
1085 ndefkey (Vminibuffer_local_map, Ctl ('m'), "exit-minibuffer");
1086 ndefkey (Vminibuffer_local_map, Ctl ('j'), "exit-minibuffer");
1087
1088 ndefkey (Vminibuffer_local_ns_map, Ctl ('g'), "abort-recursive-edit");
1089 ndefkey (Vminibuffer_local_ns_map, Ctl ('m'), "exit-minibuffer");
1090 ndefkey (Vminibuffer_local_ns_map, Ctl ('j'), "exit-minibuffer");
1091
1092 ndefkey (Vminibuffer_local_ns_map, ' ', "exit-minibuffer");
1093 ndefkey (Vminibuffer_local_ns_map, '\t', "exit-minibuffer");
1094 ndefkey (Vminibuffer_local_ns_map, '?', "self-insert-and-exit");
1095
1096 ndefkey (Vminibuffer_local_completion_map, Ctl ('g'), "abort-recursive-edit");
1097 ndefkey (Vminibuffer_local_completion_map, Ctl ('m'), "exit-minibuffer");
1098 ndefkey (Vminibuffer_local_completion_map, Ctl ('j'), "exit-minibuffer");
1099 ndefkey (Vminibuffer_local_completion_map, '\t', "minibuffer-complete");
1100 ndefkey (Vminibuffer_local_completion_map, ' ', "minibuffer-complete-word");
1101 ndefkey (Vminibuffer_local_completion_map, '?', "minibuffer-completion-help");
1102
1103 ndefkey (Vminibuffer_local_must_match_map, Ctl ('g'), "abort-recursive-edit");
1104 ndefkey (Vminibuffer_local_must_match_map, Ctl ('m'), "minibuffer-complete-and-exit");
1105 ndefkey (Vminibuffer_local_must_match_map, Ctl ('j'), "minibuffer-complete-and-exit");
1106 ndefkey (Vminibuffer_local_must_match_map, '\t', "minibuffer-complete");
1107 ndefkey (Vminibuffer_local_must_match_map, ' ', "minibuffer-complete-word");
1108 ndefkey (Vminibuffer_local_must_match_map, '?', "minibuffer-completion-help");
1109}