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