Commit | Line | Data |
---|---|---|
f927c5ae | 1 | /* Minibuffer input and completion. |
ffd56f97 | 2 | Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc. |
f927c5ae JB |
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 | |
ffd56f97 | 8 | the Free Software Foundation; either version 2, or (at your option) |
f927c5ae JB |
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 "dispextern.h" | |
26 | #include "screen.h" | |
27 | #include "window.h" | |
28 | #include "syntax.h" | |
29 | ||
30 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
31 | ||
32 | /* List of buffers for use as minibuffers. | |
33 | The first element of the list is used for the outermost minibuffer invocation, | |
34 | the next element is used for a recursive minibuffer invocation, etc. | |
35 | The list is extended at the end as deeped minibuffer recursions are encountered. */ | |
36 | Lisp_Object Vminibuffer_list; | |
37 | ||
38 | struct minibuf_save_data | |
39 | { | |
40 | char *prompt; | |
41 | int prompt_width; | |
42 | Lisp_Object help_form; | |
43 | Lisp_Object current_prefix_arg; | |
44 | }; | |
45 | ||
46 | int minibuf_save_vector_size; | |
47 | struct minibuf_save_data *minibuf_save_vector; | |
48 | ||
49 | /* Depth in minibuffer invocations. */ | |
50 | int minibuf_level; | |
51 | ||
52 | /* Nonzero means display completion help for invalid input */ | |
53 | int auto_help; | |
54 | ||
55 | /* Fread_minibuffer leaves the input, as a string, here */ | |
56 | Lisp_Object last_minibuf_string; | |
57 | ||
58 | /* Nonzero means let functions called when within a minibuffer | |
59 | invoke recursive minibuffers (to read arguments, or whatever) */ | |
60 | int enable_recursive_minibuffers; | |
61 | ||
62 | /* help-form is bound to this while in the minibuffer. */ | |
63 | ||
64 | Lisp_Object Vminibuffer_help_form; | |
65 | ||
66 | /* Nonzero means completion ignores case. */ | |
67 | ||
68 | int completion_ignore_case; | |
69 | ||
70 | /* If last completion attempt reported "Complete but not unique" | |
71 | then this is the string completed then; otherwise this is nil. */ | |
72 | ||
73 | static Lisp_Object last_exact_completion; | |
74 | ||
75 | Lisp_Object Quser_variable_p; | |
76 | ||
f927c5ae JB |
77 | \f |
78 | /* Actual minibuffer invocation. */ | |
79 | ||
80 | void read_minibuf_unwind (); | |
81 | Lisp_Object get_minibuffer (); | |
82 | Lisp_Object read_minibuf (); | |
83 | ||
84 | Lisp_Object | |
85 | read_minibuf (map, initial, prompt, backup_n, expflag) | |
86 | Lisp_Object map; | |
87 | Lisp_Object initial; | |
88 | Lisp_Object prompt; | |
89 | Lisp_Object backup_n; | |
90 | int expflag; | |
91 | { | |
92 | register Lisp_Object val; | |
93 | int count = specpdl_ptr - specpdl; | |
43bad991 | 94 | Lisp_Object mini_screen = WINDOW_SCREEN (XWINDOW (minibuf_window)); |
f927c5ae | 95 | struct gcpro gcpro1, gcpro2; |
f927c5ae JB |
96 | |
97 | if (XTYPE (prompt) != Lisp_String) | |
98 | prompt = build_string (""); | |
99 | ||
100 | /* Emacs in -batch mode calls minibuffer: print the prompt. */ | |
101 | if (noninteractive && XTYPE (prompt) == Lisp_String) | |
102 | printf ("%s", XSTRING (prompt)->data); | |
103 | ||
104 | if (!enable_recursive_minibuffers | |
105 | && minibuf_level > 0 | |
106 | && (EQ (selected_window, minibuf_window))) | |
107 | #if 0 | |
108 | || selected_screen != XSCREEN (WINDOW_SCREEN (XWINDOW (minibuf_window))) | |
109 | #endif | |
110 | error ("Command attempted to use minibuffer while in minibuffer"); | |
111 | ||
112 | if (minibuf_level == minibuf_save_vector_size) | |
113 | minibuf_save_vector = | |
114 | (struct minibuf_save_data *) | |
115 | xrealloc (minibuf_save_vector, | |
116 | (minibuf_save_vector_size *= 2) | |
117 | * sizeof (struct minibuf_save_data)); | |
118 | minibuf_save_vector[minibuf_level].prompt = minibuf_prompt; | |
119 | minibuf_save_vector[minibuf_level].prompt_width = minibuf_prompt_width; | |
120 | minibuf_prompt_width = 0; | |
121 | /* >> Why is this done this way rather than binding these variables? */ | |
122 | minibuf_save_vector[minibuf_level].help_form = Vhelp_form; | |
123 | minibuf_save_vector[minibuf_level].current_prefix_arg = Vcurrent_prefix_arg; | |
124 | GCPRO2 (minibuf_save_vector[minibuf_level].help_form, | |
125 | minibuf_save_vector[minibuf_level].current_prefix_arg); | |
126 | ||
127 | record_unwind_protect (Fset_window_configuration, | |
b2b2c677 JB |
128 | Fcurrent_window_configuration (Qnil)); |
129 | ||
130 | /* If the minibuffer window is on a different screen, save that | |
131 | screen's configuration too. */ | |
43bad991 JB |
132 | if (XSCREEN (mini_screen) != selected_screen) |
133 | { | |
134 | record_unwind_protect (Fset_window_configuration, | |
135 | Fcurrent_window_configuration (mini_screen)); | |
136 | } | |
f927c5ae JB |
137 | |
138 | val = current_buffer->directory; | |
139 | Fset_buffer (get_minibuffer (minibuf_level)); | |
140 | current_buffer->directory = val; | |
141 | Fmake_local_variable (Qprint_escape_newlines); | |
142 | print_escape_newlines = 1; | |
143 | ||
43bad991 JB |
144 | #ifdef MULTI_SCREEN |
145 | /* If the minibuffer window is on another screen, shift this screen's | |
146 | focus to that window, and arrange to put it back later. */ | |
147 | if (XSCREEN (WINDOW_SCREEN (XWINDOW (minibuf_window))) | |
148 | != selected_screen) | |
149 | { | |
150 | record_unwind_protect (read_minibuf_unwind, | |
151 | Fcons (Fselected_screen (), | |
152 | SCREEN_FOCUS_SCREEN (selected_screen))); | |
153 | ||
154 | Fredirect_screen_focus (Fselected_screen (), mini_screen); | |
155 | } | |
156 | else | |
157 | record_unwind_protect (read_minibuf_unwind, Qnil); | |
158 | #else | |
159 | record_unwind_protect (read_minibuf_unwind, Qnil); | |
160 | #endif | |
161 | ||
f927c5ae JB |
162 | Vminibuf_scroll_window = selected_window; |
163 | Fset_window_buffer (minibuf_window, Fcurrent_buffer ()); | |
f927c5ae JB |
164 | Fselect_window (minibuf_window); |
165 | XFASTINT (XWINDOW (minibuf_window)->hscroll) = 0; | |
166 | ||
167 | Ferase_buffer (); | |
168 | minibuf_level++; | |
f927c5ae | 169 | |
56a98455 | 170 | if (!NILP (initial)) |
f927c5ae JB |
171 | { |
172 | Finsert (1, &initial); | |
56a98455 | 173 | if (!NILP (backup_n) && XTYPE (backup_n) == Lisp_Int) |
f927c5ae JB |
174 | Fforward_char (backup_n); |
175 | } | |
176 | ||
177 | minibuf_prompt = (char *) alloca (XSTRING (prompt)->size + 1); | |
178 | bcopy (XSTRING (prompt)->data, minibuf_prompt, XSTRING (prompt)->size + 1); | |
179 | echo_area_glyphs = 0; | |
180 | ||
181 | Vhelp_form = Vminibuffer_help_form; | |
182 | current_buffer->keymap = map; | |
183 | ||
184 | /* ??? MCC did redraw_screen here if switching screens. */ | |
185 | recursive_edit_1 (); | |
186 | ||
187 | /* If cursor is on the minibuffer line, | |
188 | show the user we have exited by putting it in column 0. */ | |
189 | if ((SCREEN_CURSOR_Y (selected_screen) | |
190 | >= XFASTINT (XWINDOW (minibuf_window)->top)) | |
191 | && !noninteractive) | |
192 | { | |
193 | SCREEN_CURSOR_X (selected_screen) = 0; | |
194 | update_screen (selected_screen, 1, 1); | |
195 | } | |
196 | ||
197 | /* Make minibuffer contents into a string */ | |
ffd56f97 | 198 | val = make_buffer_string (1, Z); |
f927c5ae JB |
199 | bcopy (GAP_END_ADDR, XSTRING (val)->data + GPT - BEG, Z - GPT); |
200 | unbind_to (count, Qnil); /* The appropriate screen will get selected | |
43bad991 | 201 | in set-window-configuration. */ |
f927c5ae JB |
202 | |
203 | UNGCPRO; | |
204 | ||
205 | /* VAL is the string of minibuffer text. */ | |
206 | ||
207 | last_minibuf_string = val; | |
208 | ||
209 | /* If Lisp form desired instead of string, parse it */ | |
210 | if (expflag) | |
211 | val = Fread (val); | |
212 | ||
f927c5ae JB |
213 | return val; |
214 | } | |
215 | ||
216 | /* Return a buffer to be used as the minibuffer at depth `depth'. | |
217 | depth = 0 is the lowest allowed argument, and that is the value | |
218 | used for nonrecursive minibuffer invocations */ | |
219 | ||
220 | Lisp_Object | |
221 | get_minibuffer (depth) | |
222 | int depth; | |
223 | { | |
224 | Lisp_Object tail, num, buf; | |
225 | char name[14]; | |
226 | extern Lisp_Object nconc2 (); | |
227 | ||
228 | XFASTINT (num) = depth; | |
229 | tail = Fnthcdr (num, Vminibuffer_list); | |
56a98455 | 230 | if (NILP (tail)) |
f927c5ae JB |
231 | { |
232 | tail = Fcons (Qnil, Qnil); | |
233 | Vminibuffer_list = nconc2 (Vminibuffer_list, tail); | |
234 | } | |
235 | buf = Fcar (tail); | |
56a98455 | 236 | if (NILP (buf) || NILP (XBUFFER (buf)->name)) |
f927c5ae JB |
237 | { |
238 | sprintf (name, " *Minibuf-%d*", depth); | |
239 | buf = Fget_buffer_create (build_string (name)); | |
240 | XCONS (tail)->car = buf; | |
241 | } | |
242 | else | |
243 | reset_buffer (XBUFFER (buf)); | |
244 | return buf; | |
245 | } | |
246 | ||
247 | /* This function is called on exiting minibuffer, whether normally or not, | |
248 | and it restores the current window, buffer, etc. */ | |
249 | ||
250 | void | |
43bad991 JB |
251 | read_minibuf_unwind (data) |
252 | Lisp_Object data; | |
f927c5ae JB |
253 | { |
254 | /* Erase the minibuffer we were using at this level. */ | |
255 | Fset_buffer (XWINDOW (minibuf_window)->buffer); | |
256 | ||
257 | /* Prevent error in erase-buffer. */ | |
258 | current_buffer->read_only = Qnil; | |
259 | Ferase_buffer (); | |
260 | ||
261 | /* If this was a recursive minibuffer, | |
262 | tie the minibuffer window back to the outer level minibuffer buffer */ | |
263 | minibuf_level--; | |
264 | /* Make sure minibuffer window is erased, not ignored */ | |
265 | windows_or_buffers_changed++; | |
266 | XFASTINT (XWINDOW (minibuf_window)->last_modified) = 0; | |
267 | ||
268 | /* Restore prompt from outer minibuffer */ | |
269 | minibuf_prompt = minibuf_save_vector[minibuf_level].prompt; | |
270 | minibuf_prompt_width = minibuf_save_vector[minibuf_level].prompt_width; | |
271 | Vhelp_form = minibuf_save_vector[minibuf_level].help_form; | |
272 | Vcurrent_prefix_arg = minibuf_save_vector[minibuf_level].current_prefix_arg; | |
43bad991 JB |
273 | |
274 | #ifdef MULTI_SCREEN | |
275 | /* Redirect the focus of the screen that called the minibuffer. */ | |
276 | if (CONSP (data)) | |
277 | Fredirect_screen_focus (XCONS (data)->car, XCONS (data)->cdr); | |
278 | #endif | |
f927c5ae JB |
279 | } |
280 | \f | |
281 | DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0, | |
282 | "Read a string from the minibuffer, prompting with string PROMPT.\n\ | |
283 | If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\ | |
284 | to be inserted into the minibuffer before reading input.\n\ | |
285 | Third arg KEYMAP is a keymap to use whilst reading;\n\ | |
286 | if omitted or nil, the default is `minibuffer-local-map'.\n\ | |
287 | If fourth arg READ is non-nil, then interpret the result as a lisp object\n\ | |
288 | and return that object:\n\ | |
289 | in other words, do `(car (read-from-string INPUT-STRING))'\n\ | |
290 | Fifth arg POSITION, if non-nil, is where to put point\n\ | |
291 | in the minibuffer after inserting INITIAL-CONTENTS.") | |
292 | (prompt, initial_input, keymap, read, position) | |
293 | Lisp_Object prompt, initial_input, keymap, read, position; | |
294 | { | |
295 | int pos = 0; | |
296 | ||
297 | CHECK_STRING (prompt, 0); | |
56a98455 | 298 | if (!NILP (initial_input)) |
f927c5ae JB |
299 | { |
300 | CHECK_STRING (initial_input, 1); | |
56a98455 | 301 | if (!NILP (position)) |
f927c5ae JB |
302 | { |
303 | CHECK_NUMBER (position, 0); | |
304 | /* Convert to distance from end of input. */ | |
305 | pos = XINT (position) - 1 - XSTRING (initial_input)->size; | |
306 | } | |
307 | } | |
308 | ||
56a98455 | 309 | if (NILP (keymap)) |
f927c5ae JB |
310 | keymap = Vminibuffer_local_map; |
311 | else | |
312 | keymap = get_keymap (keymap,2); | |
313 | return read_minibuf (keymap, initial_input, prompt, | |
56a98455 | 314 | pos, !NILP (read)); |
f927c5ae JB |
315 | } |
316 | ||
317 | DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0, | |
318 | "Return a Lisp object read using the minibuffer.\n\ | |
319 | Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\ | |
320 | is a string to insert in the minibuffer before reading.") | |
321 | (prompt, initial_contents) | |
322 | Lisp_Object prompt, initial_contents; | |
323 | { | |
324 | CHECK_STRING (prompt, 0); | |
56a98455 | 325 | if (!NILP (initial_contents)) |
f927c5ae JB |
326 | CHECK_STRING (initial_contents, 1) |
327 | return read_minibuf (Vminibuffer_local_map, initial_contents, prompt, Qnil, 1); | |
328 | } | |
329 | ||
330 | DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0, | |
331 | "Return value of Lisp expression read using the minibuffer.\n\ | |
332 | Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\ | |
333 | is a string to insert in the minibuffer before reading.") | |
334 | (prompt, initial_contents) | |
335 | Lisp_Object prompt, initial_contents; | |
336 | { | |
337 | return Feval (Fread_minibuffer (prompt, initial_contents)); | |
338 | } | |
339 | ||
340 | /* Functions that use the minibuffer to read various things. */ | |
341 | ||
342 | DEFUN ("read-string", Fread_string, Sread_string, 1, 2, 0, | |
343 | "Read a string from the minibuffer, prompting with string PROMPT.\n\ | |
344 | If non-nil second arg INITIAL-INPUT is a string to insert before reading.") | |
345 | (prompt, initial_input) | |
346 | Lisp_Object prompt, initial_input; | |
347 | { | |
348 | return Fread_from_minibuffer (prompt, initial_input, Qnil, Qnil, Qnil); | |
349 | } | |
350 | ||
e5d77022 | 351 | DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 2, 2, 0, |
f927c5ae JB |
352 | "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\ |
353 | Prompt with PROMPT, and provide INIT as an initial value of the input string.") | |
354 | (prompt, init) | |
355 | Lisp_Object prompt, init; | |
356 | { | |
357 | CHECK_STRING (prompt, 0); | |
56a98455 | 358 | if (! NILP (init)) |
f927c5ae JB |
359 | CHECK_STRING (init, 1); |
360 | ||
361 | return read_minibuf (Vminibuffer_local_ns_map, init, prompt, Qnil, 0); | |
362 | } | |
363 | ||
364 | DEFUN ("read-command", Fread_command, Sread_command, 1, 1, 0, | |
365 | "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\ | |
366 | Prompts with PROMPT.") | |
367 | (prompt) | |
368 | Lisp_Object prompt; | |
369 | { | |
370 | return Fintern (Fcompleting_read (prompt, Vobarray, Qcommandp, Qt, Qnil, Qnil), | |
371 | Qnil); | |
372 | } | |
373 | ||
374 | #ifdef NOTDEF | |
375 | DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0, | |
376 | "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\ | |
377 | Prompts with PROMPT.") | |
378 | (prompt) | |
379 | Lisp_Object prompt; | |
380 | { | |
381 | return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil), | |
382 | Qnil); | |
383 | } | |
384 | #endif /* NOTDEF */ | |
385 | ||
386 | DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 1, 0, | |
387 | "One arg PROMPT, a string. Read the name of a user variable and return\n\ | |
388 | it as a symbol. Prompts with PROMPT.\n\ | |
389 | A user variable is one whose documentation starts with a `*' character.") | |
390 | (prompt) | |
391 | Lisp_Object prompt; | |
392 | { | |
393 | return Fintern (Fcompleting_read (prompt, Vobarray, | |
394 | Quser_variable_p, Qt, Qnil, Qnil), | |
395 | Qnil); | |
396 | } | |
397 | ||
398 | DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0, | |
399 | "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\ | |
400 | Prompts with PROMPT.\n\ | |
401 | Optional second arg is value to return if user enters an empty line.\n\ | |
402 | If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.") | |
403 | (prompt, def, require_match) | |
404 | Lisp_Object prompt, def, require_match; | |
405 | { | |
406 | Lisp_Object tem; | |
407 | Lisp_Object args[3]; | |
408 | struct gcpro gcpro1; | |
409 | ||
410 | if (XTYPE (def) == Lisp_Buffer) | |
411 | def = XBUFFER (def)->name; | |
56a98455 | 412 | if (!NILP (def)) |
f927c5ae JB |
413 | { |
414 | args[0] = build_string ("%s(default %s) "); | |
415 | args[1] = prompt; | |
416 | args[2] = def; | |
417 | prompt = Fformat (3, args); | |
418 | } | |
419 | GCPRO1 (def); | |
420 | tem = Fcompleting_read (prompt, Vbuffer_alist, Qnil, require_match, Qnil, Qnil); | |
421 | UNGCPRO; | |
422 | if (XSTRING (tem)->size) | |
423 | return tem; | |
424 | return def; | |
425 | } | |
426 | \f | |
427 | DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0, | |
428 | "Return common substring of all completions of STRING in ALIST.\n\ | |
429 | Each car of each element of ALIST is tested to see if it begins with STRING.\n\ | |
430 | All that match are compared together; the longest initial sequence\n\ | |
431 | common to all matches is returned as a string.\n\ | |
432 | If there is no match at all, nil is returned.\n\ | |
433 | For an exact match, t is returned.\n\ | |
434 | \n\ | |
435 | ALIST can be an obarray instead of an alist.\n\ | |
436 | Then the print names of all symbols in the obarray are the possible matches.\n\ | |
437 | \n\ | |
438 | ALIST can also be a function to do the completion itself.\n\ | |
439 | It receives three arguments: the values STRING, PREDICATE and nil.\n\ | |
440 | Whatever it returns becomes the value of `try-completion'.\n\ | |
441 | \n\ | |
442 | If optional third argument PREDICATE is non-nil,\n\ | |
443 | it is used to test each possible match.\n\ | |
444 | The match is a candidate only if PREDICATE returns non-nil.\n\ | |
445 | The argument given to PREDICATE is the alist element or the symbol from the obarray.") | |
446 | (string, alist, pred) | |
447 | Lisp_Object string, alist, pred; | |
448 | { | |
449 | Lisp_Object bestmatch, tail, elt, eltstring; | |
450 | int bestmatchsize; | |
451 | int compare, matchsize; | |
56a98455 | 452 | int list = CONSP (alist) || NILP (alist); |
f927c5ae JB |
453 | int index, obsize; |
454 | int matchcount = 0; | |
455 | Lisp_Object bucket, zero, end, tem; | |
456 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
457 | ||
458 | CHECK_STRING (string, 0); | |
459 | if (!list && XTYPE (alist) != Lisp_Vector) | |
460 | return call3 (alist, string, pred, Qnil); | |
461 | ||
462 | bestmatch = Qnil; | |
463 | ||
464 | /* If ALIST is not a list, set TAIL just for gc pro. */ | |
465 | tail = alist; | |
466 | if (! list) | |
467 | { | |
468 | index = 0; | |
469 | obsize = XVECTOR (alist)->size; | |
470 | bucket = XVECTOR (alist)->contents[index]; | |
471 | } | |
472 | ||
473 | while (1) | |
474 | { | |
475 | /* Get the next element of the alist or obarray. */ | |
476 | /* Exit the loop if the elements are all used up. */ | |
477 | /* elt gets the alist element or symbol. | |
478 | eltstring gets the name to check as a completion. */ | |
479 | ||
480 | if (list) | |
481 | { | |
56a98455 | 482 | if (NILP (tail)) |
f927c5ae JB |
483 | break; |
484 | elt = Fcar (tail); | |
485 | eltstring = Fcar (elt); | |
486 | tail = Fcdr (tail); | |
487 | } | |
488 | else | |
489 | { | |
490 | if (XFASTINT (bucket) != 0) | |
491 | { | |
492 | elt = bucket; | |
493 | eltstring = Fsymbol_name (elt); | |
494 | if (XSYMBOL (bucket)->next) | |
495 | XSETSYMBOL (bucket, XSYMBOL (bucket)->next); | |
496 | else | |
497 | XFASTINT (bucket) = 0; | |
498 | } | |
499 | else if (++index >= obsize) | |
500 | break; | |
501 | else | |
502 | { | |
503 | bucket = XVECTOR (alist)->contents[index]; | |
504 | continue; | |
505 | } | |
506 | } | |
507 | ||
508 | /* Is this element a possible completion? */ | |
509 | ||
510 | if (XTYPE (eltstring) == Lisp_String && | |
511 | XSTRING (string)->size <= XSTRING (eltstring)->size && | |
512 | 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data, | |
513 | XSTRING (string)->size)) | |
514 | { | |
515 | /* Yes. */ | |
516 | /* Ignore this element if there is a predicate | |
517 | and the predicate doesn't like it. */ | |
518 | ||
56a98455 | 519 | if (!NILP (pred)) |
f927c5ae JB |
520 | { |
521 | if (EQ (pred, Qcommandp)) | |
522 | tem = Fcommandp (elt); | |
523 | else | |
524 | { | |
525 | GCPRO4 (tail, string, eltstring, bestmatch); | |
526 | tem = call1 (pred, elt); | |
527 | UNGCPRO; | |
528 | } | |
56a98455 | 529 | if (NILP (tem)) continue; |
f927c5ae JB |
530 | } |
531 | ||
532 | /* Update computation of how much all possible completions match */ | |
533 | ||
534 | matchcount++; | |
56a98455 | 535 | if (NILP (bestmatch)) |
f927c5ae JB |
536 | bestmatch = eltstring, bestmatchsize = XSTRING (eltstring)->size; |
537 | else | |
538 | { | |
539 | compare = min (bestmatchsize, XSTRING (eltstring)->size); | |
540 | matchsize = scmp (XSTRING (bestmatch)->data, | |
541 | XSTRING (eltstring)->data, | |
542 | compare); | |
52b14ac0 JB |
543 | if (matchsize < 0) |
544 | matchsize = compare; | |
545 | if (completion_ignore_case) | |
546 | { | |
547 | /* If this is an exact match except for case, | |
548 | use it as the best match rather than one that is not an | |
549 | exact match. This way, we get the case pattern | |
550 | of the actual match. */ | |
551 | if ((matchsize == XSTRING (eltstring)->size | |
552 | && matchsize < XSTRING (bestmatch)->size) | |
553 | || | |
554 | /* If there is more than one exact match ignoring case, | |
555 | and one of them is exact including case, | |
556 | prefer that one. */ | |
557 | /* If there is no exact match ignoring case, | |
558 | prefer a match that does not change the case | |
559 | of the input. */ | |
560 | ((matchsize == XSTRING (eltstring)->size) | |
561 | == | |
562 | (matchsize == XSTRING (bestmatch)->size) | |
563 | && !bcmp (XSTRING (eltstring)->data, | |
564 | XSTRING (string)->data, XSTRING (string)->size) | |
565 | && bcmp (XSTRING (bestmatch)->data, | |
566 | XSTRING (string)->data, XSTRING (string)->size))) | |
567 | bestmatch = eltstring; | |
568 | } | |
569 | bestmatchsize = matchsize; | |
f927c5ae JB |
570 | } |
571 | } | |
572 | } | |
573 | ||
56a98455 | 574 | if (NILP (bestmatch)) |
f927c5ae | 575 | return Qnil; /* No completions found */ |
52b14ac0 JB |
576 | /* If we are ignoring case, and there is no exact match, |
577 | and no additional text was supplied, | |
578 | don't change the case of what the user typed. */ | |
579 | if (completion_ignore_case && bestmatchsize == XSTRING (string)->size | |
580 | && XSTRING (bestmatch)->size > bestmatchsize) | |
581 | return string; | |
582 | ||
583 | /* Return t if the supplied string is an exact match (counting case); | |
584 | it does not require any change to be made. */ | |
585 | if (matchcount == 1 && bestmatchsize == XSTRING (string)->size | |
586 | && !bcmp (XSTRING (bestmatch)->data, XSTRING (string)->data, | |
587 | bestmatchsize)) | |
f927c5ae JB |
588 | return Qt; |
589 | ||
590 | XFASTINT (zero) = 0; /* Else extract the part in which */ | |
591 | XFASTINT (end) = bestmatchsize; /* all completions agree */ | |
592 | return Fsubstring (bestmatch, zero, end); | |
593 | } | |
594 | ||
595 | /* Compare exactly LEN chars of strings at S1 and S2, | |
596 | ignoring case if appropriate. | |
597 | Return -1 if strings match, | |
598 | else number of chars that match at the beginning. */ | |
599 | ||
600 | scmp (s1, s2, len) | |
601 | register char *s1, *s2; | |
602 | int len; | |
603 | { | |
604 | register int l = len; | |
605 | ||
606 | if (completion_ignore_case) | |
607 | { | |
608 | while (l && DOWNCASE (*s1++) == DOWNCASE (*s2++)) | |
609 | l--; | |
610 | } | |
611 | else | |
612 | { | |
613 | while (l && *s1++ == *s2++) | |
614 | l--; | |
615 | } | |
616 | if (l == 0) | |
617 | return -1; | |
618 | else return len - l; | |
619 | } | |
620 | \f | |
621 | DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 3, 0, | |
622 | "Search for partial matches to STRING in ALIST.\n\ | |
623 | Each car of each element of ALIST is tested to see if it begins with STRING.\n\ | |
624 | The value is a list of all the strings from ALIST that match.\n\ | |
625 | ALIST can be an obarray instead of an alist.\n\ | |
626 | Then the print names of all symbols in the obarray are the possible matches.\n\ | |
627 | \n\ | |
628 | ALIST can also be a function to do the completion itself.\n\ | |
629 | It receives three arguments: the values STRING, PREDICATE and t.\n\ | |
630 | Whatever it returns becomes the value of `all-completion'.\n\ | |
631 | \n\ | |
632 | If optional third argument PREDICATE is non-nil,\n\ | |
633 | it is used to test each possible match.\n\ | |
634 | The match is a candidate only if PREDICATE returns non-nil.\n\ | |
635 | The argument given to PREDICATE is the alist element or the symbol from the obarray.") | |
636 | (string, alist, pred) | |
637 | Lisp_Object string, alist, pred; | |
638 | { | |
639 | Lisp_Object tail, elt, eltstring; | |
640 | Lisp_Object allmatches; | |
56a98455 | 641 | int list = CONSP (alist) || NILP (alist); |
f927c5ae JB |
642 | int index, obsize; |
643 | Lisp_Object bucket, tem; | |
644 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
645 | ||
646 | CHECK_STRING (string, 0); | |
647 | if (!list && XTYPE (alist) != Lisp_Vector) | |
648 | { | |
649 | return call3 (alist, string, pred, Qt); | |
650 | } | |
651 | allmatches = Qnil; | |
652 | ||
653 | /* If ALIST is not a list, set TAIL just for gc pro. */ | |
654 | tail = alist; | |
655 | if (! list) | |
656 | { | |
657 | index = 0; | |
658 | obsize = XVECTOR (alist)->size; | |
659 | bucket = XVECTOR (alist)->contents[index]; | |
660 | } | |
661 | ||
662 | while (1) | |
663 | { | |
664 | /* Get the next element of the alist or obarray. */ | |
665 | /* Exit the loop if the elements are all used up. */ | |
666 | /* elt gets the alist element or symbol. | |
667 | eltstring gets the name to check as a completion. */ | |
668 | ||
669 | if (list) | |
670 | { | |
56a98455 | 671 | if (NILP (tail)) |
f927c5ae JB |
672 | break; |
673 | elt = Fcar (tail); | |
674 | eltstring = Fcar (elt); | |
675 | tail = Fcdr (tail); | |
676 | } | |
677 | else | |
678 | { | |
679 | if (XFASTINT (bucket) != 0) | |
680 | { | |
681 | elt = bucket; | |
682 | eltstring = Fsymbol_name (elt); | |
683 | if (XSYMBOL (bucket)->next) | |
684 | XSETSYMBOL (bucket, XSYMBOL (bucket)->next); | |
685 | else | |
686 | XFASTINT (bucket) = 0; | |
687 | } | |
688 | else if (++index >= obsize) | |
689 | break; | |
690 | else | |
691 | { | |
692 | bucket = XVECTOR (alist)->contents[index]; | |
693 | continue; | |
694 | } | |
695 | } | |
696 | ||
697 | /* Is this element a possible completion? */ | |
698 | ||
699 | if (XTYPE (eltstring) == Lisp_String && | |
700 | XSTRING (string)->size <= XSTRING (eltstring)->size && | |
701 | XSTRING (eltstring)->data[0] != ' ' && | |
702 | 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data, | |
703 | XSTRING (string)->size)) | |
704 | { | |
705 | /* Yes. */ | |
706 | /* Ignore this element if there is a predicate | |
707 | and the predicate doesn't like it. */ | |
708 | ||
56a98455 | 709 | if (!NILP (pred)) |
f927c5ae JB |
710 | { |
711 | if (EQ (pred, Qcommandp)) | |
712 | tem = Fcommandp (elt); | |
713 | else | |
714 | { | |
715 | GCPRO4 (tail, eltstring, allmatches, string); | |
716 | tem = call1 (pred, elt); | |
717 | UNGCPRO; | |
718 | } | |
56a98455 | 719 | if (NILP (tem)) continue; |
f927c5ae JB |
720 | } |
721 | /* Ok => put it on the list. */ | |
722 | allmatches = Fcons (eltstring, allmatches); | |
723 | } | |
724 | } | |
725 | ||
726 | return Fnreverse (allmatches); | |
727 | } | |
728 | \f | |
729 | Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table; | |
730 | Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate; | |
731 | Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm; | |
732 | ||
733 | DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0, | |
734 | "Read a string in the minibuffer, with completion.\n\ | |
3371bdfc | 735 | Args: PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, BACKUP-N.\n\ |
f927c5ae JB |
736 | PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\ |
737 | TABLE is an alist whose elements' cars are strings, or an obarray.\n\ | |
738 | PREDICATE limits completion to a subset of TABLE.\n\ | |
739 | See `try-completion' for more details on completion, TABLE, and PREDICATE.\n\ | |
740 | If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\ | |
741 | the input is (or completes to) an element of TABLE.\n\ | |
742 | If it is also not t, Return does not exit if it does non-null completion.\n\ | |
743 | If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\ | |
744 | Case is ignored if ambient value of `completion-ignore-case' is non-nil.\n\ | |
745 | If BACKUP-N is specified, point should be placed that many spaces from\n\ | |
746 | the end of the buffer. This is useful when providing default values,\n\ | |
747 | because you can put point before the last component of a filename or any\n\ | |
748 | other component that is likely to be deleted.") | |
749 | (prompt, table, pred, require_match, init, backup_n) | |
750 | Lisp_Object prompt, table, pred, require_match, init, backup_n; | |
751 | { | |
752 | Lisp_Object val; | |
753 | int count = specpdl_ptr - specpdl; | |
754 | specbind (Qminibuffer_completion_table, table); | |
755 | specbind (Qminibuffer_completion_predicate, pred); | |
756 | specbind (Qminibuffer_completion_confirm, | |
757 | EQ (require_match, Qt) ? Qnil : Qt); | |
758 | last_exact_completion = Qnil; | |
56a98455 | 759 | val = read_minibuf (NILP (require_match) |
f927c5ae JB |
760 | ? Vminibuffer_local_completion_map |
761 | : Vminibuffer_local_must_match_map, | |
762 | init, prompt, backup_n, 0); | |
763 | return unbind_to (count, val); | |
764 | } | |
765 | \f | |
766 | /* Temporarily display the string M at the end of the current | |
767 | minibuffer contents. This is used to display things like | |
768 | "[No Match]" when the user requests a completion for a prefix | |
769 | that has no possible completions, and other quick, unobtrusive | |
770 | messages. */ | |
771 | ||
772 | temp_echo_area_glyphs (m) | |
773 | char *m; | |
774 | { | |
775 | /* It's not very modular to do things this way, but then it seems | |
776 | to me that the whole echo_area_glyphs thing is a hack anyway. */ | |
777 | extern char *previous_echo_glyphs; | |
778 | ||
779 | int osize = ZV; | |
780 | Lisp_Object oinhibit; | |
781 | oinhibit = Vinhibit_quit; | |
782 | ||
783 | /* Clear out any old echo-area message to make way for our new | |
784 | thing. */ | |
785 | echo_area_glyphs = previous_echo_glyphs = 0; | |
786 | ||
787 | SET_PT (osize); | |
788 | insert_string (m); | |
789 | SET_PT (osize); | |
790 | Vinhibit_quit = Qt; | |
791 | Fsit_for (make_number (2), Qnil, Qnil); | |
792 | del_range (point, ZV); | |
56a98455 | 793 | if (!NILP (Vquit_flag)) |
f927c5ae JB |
794 | { |
795 | Vquit_flag = Qnil; | |
796 | unread_command_char = Ctl ('g'); | |
797 | } | |
798 | Vinhibit_quit = oinhibit; | |
799 | } | |
800 | ||
801 | Lisp_Object Fminibuffer_completion_help (); | |
52b14ac0 | 802 | Lisp_Object assoc_for_completion (); |
f927c5ae JB |
803 | |
804 | /* returns: | |
805 | * 0 no possible completion | |
806 | * 1 was already an exact and unique completion | |
807 | * 3 was already an exact completion | |
808 | * 4 completed to an exact completion | |
809 | * 5 some completion happened | |
810 | * 6 no completion happened | |
811 | */ | |
812 | int | |
813 | do_completion () | |
814 | { | |
815 | Lisp_Object completion, tem; | |
816 | int completedp; | |
817 | Lisp_Object last; | |
818 | ||
819 | completion = Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table, | |
820 | Vminibuffer_completion_predicate); | |
821 | last = last_exact_completion; | |
822 | last_exact_completion = Qnil; | |
823 | ||
56a98455 | 824 | if (NILP (completion)) |
f927c5ae JB |
825 | { |
826 | bitch_at_user (); | |
827 | temp_echo_area_glyphs (" [No match]"); | |
828 | return 0; | |
829 | } | |
830 | ||
831 | if (EQ (completion, Qt)) /* exact and unique match */ | |
832 | return 1; | |
833 | ||
834 | /* compiler bug */ | |
835 | tem = Fstring_equal (completion, Fbuffer_string()); | |
56a98455 | 836 | if (completedp = NILP (tem)) |
f927c5ae JB |
837 | { |
838 | Ferase_buffer (); /* Some completion happened */ | |
839 | Finsert (1, &completion); | |
840 | } | |
841 | ||
842 | /* It did find a match. Do we match some possibility exactly now? */ | |
843 | if (CONSP (Vminibuffer_completion_table) | |
56a98455 | 844 | || NILP (Vminibuffer_completion_table)) |
52b14ac0 JB |
845 | tem = assoc_for_completion (Fbuffer_string (), |
846 | Vminibuffer_completion_table); | |
f927c5ae JB |
847 | else if (XTYPE (Vminibuffer_completion_table) == Lisp_Vector) |
848 | { | |
849 | /* the primitive used by Fintern_soft */ | |
850 | extern Lisp_Object oblookup (); | |
851 | ||
852 | tem = Fbuffer_string (); | |
853 | /* Bypass intern-soft as that loses for nil */ | |
854 | tem = oblookup (Vminibuffer_completion_table, | |
855 | XSTRING (tem)->data, XSTRING (tem)->size); | |
856 | if (XTYPE (tem) != Lisp_Symbol) | |
857 | tem = Qnil; | |
56a98455 | 858 | else if (!NILP (Vminibuffer_completion_predicate)) |
f927c5ae JB |
859 | tem = call1 (Vminibuffer_completion_predicate, tem); |
860 | else | |
861 | tem = Qt; | |
862 | } | |
863 | else | |
864 | tem = call3 (Vminibuffer_completion_table, | |
865 | Fbuffer_string (), | |
866 | Vminibuffer_completion_predicate, | |
867 | Qlambda); | |
868 | ||
56a98455 | 869 | if (NILP (tem)) |
f927c5ae JB |
870 | { /* not an exact match */ |
871 | if (completedp) | |
872 | return 5; | |
873 | else if (auto_help) | |
874 | Fminibuffer_completion_help (); | |
875 | else | |
876 | temp_echo_area_glyphs (" [Next char not unique]"); | |
877 | return 6; | |
878 | } | |
879 | else if (completedp) | |
880 | return 4; | |
881 | /* If the last exact completion and this one were the same, | |
882 | it means we've already given a "Complete but not unique" | |
52b14ac0 | 883 | message and the user's hit TAB again, so now we give him help. */ |
f927c5ae | 884 | last_exact_completion = completion; |
56a98455 | 885 | if (!NILP (last)) |
f927c5ae JB |
886 | { |
887 | tem = Fbuffer_string (); | |
56a98455 | 888 | if (!NILP (Fequal (tem, last))) |
f927c5ae JB |
889 | Fminibuffer_completion_help (); |
890 | } | |
891 | return 3; | |
f927c5ae JB |
892 | } |
893 | ||
52b14ac0 JB |
894 | /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */ |
895 | ||
896 | Lisp_Object | |
897 | assoc_for_completion (key, list) | |
898 | register Lisp_Object key; | |
899 | Lisp_Object list; | |
900 | { | |
901 | register Lisp_Object tail; | |
902 | ||
903 | if (completion_ignore_case) | |
904 | key = Fupcase (key); | |
905 | ||
56a98455 | 906 | for (tail = list; !NILP (tail); tail = Fcdr (tail)) |
52b14ac0 JB |
907 | { |
908 | register Lisp_Object elt, tem, thiscar; | |
909 | elt = Fcar (tail); | |
910 | if (!CONSP (elt)) continue; | |
911 | thiscar = Fcar (elt); | |
912 | if (XTYPE (thiscar) != Lisp_String) | |
913 | continue; | |
914 | if (completion_ignore_case) | |
915 | thiscar = Fupcase (thiscar); | |
916 | tem = Fequal (thiscar, key); | |
56a98455 | 917 | if (!NILP (tem)) return elt; |
52b14ac0 JB |
918 | QUIT; |
919 | } | |
920 | return Qnil; | |
921 | } | |
f927c5ae JB |
922 | |
923 | DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "", | |
924 | "Complete the minibuffer contents as far as possible.") | |
925 | () | |
926 | { | |
927 | register int i = do_completion (); | |
928 | switch (i) | |
929 | { | |
930 | case 0: | |
931 | return Qnil; | |
932 | ||
933 | case 1: | |
934 | temp_echo_area_glyphs (" [Sole completion]"); | |
935 | break; | |
936 | ||
937 | case 3: | |
938 | temp_echo_area_glyphs (" [Complete, but not unique]"); | |
939 | break; | |
940 | } | |
941 | ||
942 | return Qt; | |
943 | } | |
944 | ||
945 | DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit, | |
946 | Sminibuffer_complete_and_exit, 0, 0, "", | |
947 | "Complete the minibuffer contents, and maybe exit.\n\ | |
948 | Exit if the name is valid with no completion needed.\n\ | |
949 | If name was completed to a valid match,\n\ | |
950 | a repetition of this command will exit.") | |
951 | () | |
952 | { | |
953 | register int i; | |
954 | ||
955 | /* Allow user to specify null string */ | |
956 | if (BEGV == ZV) | |
957 | goto exit; | |
958 | ||
959 | i = do_completion (); | |
960 | switch (i) | |
961 | { | |
962 | case 1: | |
963 | case 3: | |
964 | goto exit; | |
965 | ||
966 | case 4: | |
56a98455 | 967 | if (!NILP (Vminibuffer_completion_confirm)) |
f927c5ae JB |
968 | { |
969 | temp_echo_area_glyphs (" [Confirm]"); | |
970 | return Qnil; | |
971 | } | |
972 | else | |
973 | goto exit; | |
974 | ||
975 | default: | |
976 | return Qnil; | |
977 | } | |
978 | exit: | |
979 | Fthrow (Qexit, Qnil); | |
980 | /* NOTREACHED */ | |
981 | } | |
982 | ||
983 | DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word, | |
984 | 0, 0, "", | |
985 | "Complete the minibuffer contents at most a single word.\n\ | |
986 | After one word is completed as much as possible, a space or hyphen\n\ | |
987 | is added, provided that matches some possible completion.") | |
988 | () | |
989 | { | |
990 | Lisp_Object completion, tem; | |
991 | register int i; | |
992 | register unsigned char *completion_string; | |
993 | /* We keep calling Fbuffer_string | |
994 | rather than arrange for GC to hold onto a pointer to | |
995 | one of the strings thus made. */ | |
996 | ||
997 | completion = Ftry_completion (Fbuffer_string (), | |
998 | Vminibuffer_completion_table, | |
999 | Vminibuffer_completion_predicate); | |
56a98455 | 1000 | if (NILP (completion)) |
f927c5ae JB |
1001 | { |
1002 | bitch_at_user (); | |
1003 | temp_echo_area_glyphs (" [No match]"); | |
1004 | return Qnil; | |
1005 | } | |
1006 | if (EQ (completion, Qt)) | |
1007 | return Qnil; | |
1008 | ||
1009 | #if 0 /* How the below code used to look, for reference */ | |
1010 | tem = Fbuffer_string (); | |
1011 | b = XSTRING (tem)->data; | |
1012 | i = ZV - 1 - XSTRING (completion)->size; | |
1013 | p = XSTRING (completion)->data; | |
1014 | if (i > 0 || | |
1015 | 0 <= scmp (b, p, ZV - 1)) | |
1016 | { | |
1017 | i = 1; | |
1018 | /* Set buffer to longest match of buffer tail and completion head. */ | |
1019 | while (0 <= scmp (b + i, p, ZV - 1 - i)) | |
1020 | i++; | |
1021 | del_range (1, i + 1); | |
1022 | SET_PT (ZV); | |
1023 | } | |
1024 | #else /* Rewritten code */ | |
1025 | { | |
1026 | register unsigned char *buffer_string; | |
1027 | int buffer_length, completion_length; | |
1028 | ||
1029 | tem = Fbuffer_string (); | |
1030 | buffer_string = XSTRING (tem)->data; | |
1031 | completion_string = XSTRING (completion)->data; | |
1032 | buffer_length = XSTRING (tem)->size; /* ie ZV - BEGV */ | |
1033 | completion_length = XSTRING (completion)->size; | |
1034 | i = buffer_length - completion_length; | |
1035 | /* Mly: I don't understand what this is supposed to do AT ALL */ | |
1036 | if (i > 0 || | |
1037 | 0 <= scmp (buffer_string, completion_string, buffer_length)) | |
1038 | { | |
1039 | /* Set buffer to longest match of buffer tail and completion head. */ | |
1040 | if (i <= 0) i = 1; | |
1041 | buffer_string += i; | |
1042 | buffer_length -= i; | |
1043 | while (0 <= scmp (buffer_string++, completion_string, buffer_length--)) | |
1044 | i++; | |
1045 | del_range (1, i + 1); | |
1046 | SET_PT (ZV); | |
1047 | } | |
1048 | } | |
1049 | #endif /* Rewritten code */ | |
1050 | i = ZV - BEGV; | |
1051 | ||
1052 | /* If completion finds next char not unique, | |
1053 | consider adding a space or a hyphen */ | |
1054 | if (i == XSTRING (completion)->size) | |
1055 | { | |
1056 | tem = Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")), | |
1057 | Vminibuffer_completion_table, | |
1058 | Vminibuffer_completion_predicate); | |
1059 | if (XTYPE (tem) == Lisp_String) | |
1060 | completion = tem; | |
1061 | else | |
1062 | { | |
1063 | tem = Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")), | |
1064 | Vminibuffer_completion_table, | |
1065 | Vminibuffer_completion_predicate); | |
1066 | if (XTYPE (tem) == Lisp_String) | |
1067 | completion = tem; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | /* Now find first word-break in the stuff found by completion. | |
1072 | i gets index in string of where to stop completing. */ | |
1073 | completion_string = XSTRING (completion)->data; | |
1074 | ||
1075 | for (; i < XSTRING (completion)->size; i++) | |
1076 | if (SYNTAX (completion_string[i]) != Sword) break; | |
1077 | if (i < XSTRING (completion)->size) | |
1078 | i = i + 1; | |
1079 | ||
1080 | /* If got no characters, print help for user. */ | |
1081 | ||
1082 | if (i == ZV - BEGV) | |
1083 | { | |
1084 | if (auto_help) | |
1085 | Fminibuffer_completion_help (); | |
1086 | return Qnil; | |
1087 | } | |
1088 | ||
1089 | /* Otherwise insert in minibuffer the chars we got */ | |
1090 | ||
1091 | Ferase_buffer (); | |
1092 | insert_from_string (completion, 0, i); | |
1093 | return Qt; | |
1094 | } | |
1095 | \f | |
1096 | DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list, | |
1097 | 1, 1, 0, | |
1098 | "Display in a buffer the list of completions, COMPLETIONS.\n\ | |
1099 | Each element may be just a symbol or string\n\ | |
1100 | or may be a list of two strings to be printed as if concatenated.") | |
1101 | (completions) | |
1102 | Lisp_Object completions; | |
1103 | { | |
1104 | register Lisp_Object tail, elt; | |
1105 | register int i; | |
1106 | struct buffer *old = current_buffer; | |
1107 | /* No GCPRO needed, since (when it matters) every variable | |
1108 | points to a non-string that is pointed to by COMPLETIONS. */ | |
1109 | ||
1110 | set_buffer_internal (XBUFFER (Vstandard_output)); | |
1111 | ||
56a98455 | 1112 | if (NILP (completions)) |
f927c5ae JB |
1113 | insert_string ("There are no possible completions of what you have typed."); |
1114 | else | |
1115 | { | |
1116 | insert_string ("Possible completions are:"); | |
56a98455 | 1117 | for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++) |
f927c5ae JB |
1118 | { |
1119 | /* this needs fixing for the case of long completions | |
1120 | and/or narrow windows */ | |
1121 | /* Sadly, the window it will appear in is not known | |
1122 | until after the text has been made. */ | |
1123 | if (i & 1) | |
1124 | Findent_to (make_number (35), make_number (1)); | |
1125 | else | |
1126 | Fterpri (Qnil); | |
1127 | elt = Fcar (tail); | |
1128 | if (CONSP (elt)) | |
1129 | { | |
1130 | Fprinc (Fcar (elt), Qnil); | |
1131 | Fprinc (Fcar (Fcdr (elt)), Qnil); | |
1132 | } | |
1133 | else | |
1134 | Fprinc (elt, Qnil); | |
1135 | } | |
1136 | } | |
1137 | set_buffer_internal (old); | |
1138 | return Qnil; | |
1139 | } | |
1140 | ||
1141 | DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help, | |
1142 | 0, 0, "", | |
1143 | "Display a list of possible completions of the current minibuffer contents.") | |
1144 | () | |
1145 | { | |
1146 | Lisp_Object completions; | |
1147 | ||
1148 | message ("Making completion list..."); | |
1149 | completions = Fall_completions (Fbuffer_string (), | |
1150 | Vminibuffer_completion_table, | |
1151 | Vminibuffer_completion_predicate); | |
1152 | echo_area_glyphs = 0; | |
1153 | ||
56a98455 | 1154 | if (NILP (completions)) |
f927c5ae JB |
1155 | { |
1156 | bitch_at_user (); | |
1157 | temp_echo_area_glyphs (" [No completions]"); | |
1158 | } | |
1159 | else | |
1160 | internal_with_output_to_temp_buffer ("*Completions*", | |
1161 | Fdisplay_completion_list, | |
1162 | Fsort (completions, Qstring_lessp)); | |
1163 | return Qnil; | |
1164 | } | |
1165 | \f | |
1166 | DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "", | |
1167 | "Terminate minibuffer input.") | |
1168 | () | |
1169 | { | |
1170 | if (XTYPE (last_command_char) == Lisp_Int) | |
1171 | internal_self_insert (last_command_char, 0); | |
1172 | else | |
1173 | bitch_at_user (); | |
1174 | ||
1175 | Fthrow (Qexit, Qnil); | |
1176 | } | |
1177 | ||
1178 | DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "", | |
1179 | "Terminate this minibuffer argument.") | |
1180 | () | |
1181 | { | |
1182 | Fthrow (Qexit, Qnil); | |
1183 | } | |
1184 | ||
1185 | DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0, | |
1186 | "Return current depth of activations of minibuffer, a nonnegative integer.") | |
1187 | () | |
1188 | { | |
1189 | return make_number (minibuf_level); | |
1190 | } | |
1191 | ||
1192 | \f | |
1193 | init_minibuf_once () | |
1194 | { | |
1195 | Vminibuffer_list = Qnil; | |
1196 | staticpro (&Vminibuffer_list); | |
1197 | } | |
1198 | ||
1199 | syms_of_minibuf () | |
1200 | { | |
1201 | minibuf_level = 0; | |
1202 | minibuf_prompt = 0; | |
1203 | minibuf_save_vector_size = 5; | |
1204 | minibuf_save_vector = (struct minibuf_save_data *) malloc (5 * sizeof (struct minibuf_save_data)); | |
1205 | ||
1206 | Qminibuffer_completion_table = intern ("minibuffer-completion-table"); | |
1207 | staticpro (&Qminibuffer_completion_table); | |
1208 | ||
1209 | Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm"); | |
1210 | staticpro (&Qminibuffer_completion_confirm); | |
1211 | ||
1212 | Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate"); | |
1213 | staticpro (&Qminibuffer_completion_predicate); | |
1214 | ||
1215 | staticpro (&last_minibuf_string); | |
1216 | last_minibuf_string = Qnil; | |
1217 | ||
1218 | Quser_variable_p = intern ("user-variable-p"); | |
1219 | staticpro (&Quser_variable_p); | |
1220 | ||
1221 | ||
1222 | ||
1223 | DEFVAR_BOOL ("completion-auto-help", &auto_help, | |
1224 | "*Non-nil means automatically provide help for invalid completion input."); | |
1225 | auto_help = 1; | |
1226 | ||
1227 | DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case, | |
1228 | "Non-nil means don't consider case significant in completion."); | |
1229 | completion_ignore_case = 0; | |
1230 | ||
1231 | DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers, | |
1232 | "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\ | |
1233 | More precisely, this variable makes a difference when the minibuffer window\n\ | |
1234 | is the selected window. If you are in some other window, minibuffer commands\n\ | |
1235 | are allowed even if a minibuffer is active."); | |
1236 | enable_recursive_minibuffers = 0; | |
1237 | ||
1238 | DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table, | |
1239 | "Alist or obarray used for completion in the minibuffer.\n\ | |
1240 | This becomes the ALIST argument to `try-completion' and `all-completion'.\n\ | |
1241 | \n\ | |
1242 | The value may alternatively be a function, which is given three arguments:\n\ | |
1243 | STRING, the current buffer contents;\n\ | |
1244 | PREDICATE, the predicate for filtering possible matches;\n\ | |
1245 | CODE, which says what kind of things to do.\n\ | |
1246 | CODE can be nil, t or `lambda'.\n\ | |
1247 | nil means to return the best completion of STRING, or nil if there is none.\n\ | |
1248 | t means to return a list of all possible completions of STRING.\n\ | |
1249 | `lambda' means to return t if STRING is a valid completion as it stands."); | |
1250 | Vminibuffer_completion_table = Qnil; | |
1251 | ||
1252 | DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate, | |
1253 | "Within call to `completing-read', this holds the PREDICATE argument."); | |
1254 | Vminibuffer_completion_predicate = Qnil; | |
1255 | ||
1256 | DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm, | |
1257 | "Non-nil => demand confirmation of completion before exiting minibuffer."); | |
1258 | Vminibuffer_completion_confirm = Qnil; | |
1259 | ||
1260 | DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form, | |
1261 | "Value that `help-form' takes on inside the minibuffer."); | |
1262 | Vminibuffer_help_form = Qnil; | |
1263 | ||
1264 | defsubr (&Sread_from_minibuffer); | |
1265 | defsubr (&Seval_minibuffer); | |
1266 | defsubr (&Sread_minibuffer); | |
1267 | defsubr (&Sread_string); | |
1268 | defsubr (&Sread_command); | |
1269 | defsubr (&Sread_variable); | |
1270 | defsubr (&Sread_buffer); | |
1271 | defsubr (&Sread_no_blanks_input); | |
1272 | defsubr (&Sminibuffer_depth); | |
1273 | ||
1274 | defsubr (&Stry_completion); | |
1275 | defsubr (&Sall_completions); | |
1276 | defsubr (&Scompleting_read); | |
1277 | defsubr (&Sminibuffer_complete); | |
1278 | defsubr (&Sminibuffer_complete_word); | |
1279 | defsubr (&Sminibuffer_complete_and_exit); | |
1280 | defsubr (&Sdisplay_completion_list); | |
1281 | defsubr (&Sminibuffer_completion_help); | |
1282 | ||
1283 | defsubr (&Sself_insert_and_exit); | |
1284 | defsubr (&Sexit_minibuffer); | |
1285 | ||
1286 | } | |
1287 | ||
1288 | keys_of_minibuf () | |
1289 | { | |
1290 | initial_define_key (Vminibuffer_local_map, Ctl ('g'), | |
1291 | "abort-recursive-edit"); | |
1292 | initial_define_key (Vminibuffer_local_map, Ctl ('m'), | |
1293 | "exit-minibuffer"); | |
1294 | initial_define_key (Vminibuffer_local_map, Ctl ('j'), | |
1295 | "exit-minibuffer"); | |
1296 | ||
1297 | initial_define_key (Vminibuffer_local_ns_map, Ctl ('g'), | |
1298 | "abort-recursive-edit"); | |
1299 | initial_define_key (Vminibuffer_local_ns_map, Ctl ('m'), | |
1300 | "exit-minibuffer"); | |
1301 | initial_define_key (Vminibuffer_local_ns_map, Ctl ('j'), | |
1302 | "exit-minibuffer"); | |
1303 | ||
1304 | initial_define_key (Vminibuffer_local_ns_map, ' ', | |
1305 | "exit-minibuffer"); | |
1306 | initial_define_key (Vminibuffer_local_ns_map, '\t', | |
1307 | "exit-minibuffer"); | |
1308 | initial_define_key (Vminibuffer_local_ns_map, '?', | |
1309 | "self-insert-and-exit"); | |
1310 | ||
1311 | initial_define_key (Vminibuffer_local_completion_map, Ctl ('g'), | |
1312 | "abort-recursive-edit"); | |
1313 | initial_define_key (Vminibuffer_local_completion_map, Ctl ('m'), | |
1314 | "exit-minibuffer"); | |
1315 | initial_define_key (Vminibuffer_local_completion_map, Ctl ('j'), | |
1316 | "exit-minibuffer"); | |
1317 | ||
1318 | initial_define_key (Vminibuffer_local_completion_map, '\t', | |
1319 | "minibuffer-complete"); | |
1320 | initial_define_key (Vminibuffer_local_completion_map, ' ', | |
1321 | "minibuffer-complete-word"); | |
1322 | initial_define_key (Vminibuffer_local_completion_map, '?', | |
1323 | "minibuffer-completion-help"); | |
1324 | ||
1325 | initial_define_key (Vminibuffer_local_must_match_map, Ctl ('g'), | |
1326 | "abort-recursive-edit"); | |
1327 | initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'), | |
1328 | "minibuffer-complete-and-exit"); | |
1329 | initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'), | |
1330 | "minibuffer-complete-and-exit"); | |
1331 | initial_define_key (Vminibuffer_local_must_match_map, '\t', | |
1332 | "minibuffer-complete"); | |
1333 | initial_define_key (Vminibuffer_local_must_match_map, ' ', | |
1334 | "minibuffer-complete-word"); | |
1335 | initial_define_key (Vminibuffer_local_must_match_map, '?', | |
1336 | "minibuffer-completion-help"); | |
1337 | } |