Commit | Line | Data |
---|---|---|
f927c5ae | 1 | /* Minibuffer input and completion. |
4e6a91e7 | 2 | Copyright (C) 1985, 1986, 1993, 1994, 1995 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 | ||
18160b98 | 21 | #include <config.h> |
f927c5ae JB |
22 | #include "lisp.h" |
23 | #include "commands.h" | |
24 | #include "buffer.h" | |
25 | #include "dispextern.h" | |
ff11dfa1 | 26 | #include "frame.h" |
f927c5ae JB |
27 | #include "window.h" |
28 | #include "syntax.h" | |
5221fd63 | 29 | #include "keyboard.h" |
f927c5ae JB |
30 | |
31 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
32 | ||
a4e71d81 KH |
33 | extern int quit_char; |
34 | ||
f927c5ae | 35 | /* List of buffers for use as minibuffers. |
4d04c1f1 KH |
36 | The first element of the list is used for the outermost minibuffer |
37 | invocation, the next element is used for a recursive minibuffer | |
38 | invocation, etc. The list is extended at the end as deeper | |
39 | minibuffer recursions are encountered. */ | |
f927c5ae JB |
40 | Lisp_Object Vminibuffer_list; |
41 | ||
4d04c1f1 KH |
42 | /* Data to remember during recursive minibuffer invocations */ |
43 | Lisp_Object minibuf_save_list; | |
f927c5ae JB |
44 | |
45 | /* Depth in minibuffer invocations. */ | |
46 | int minibuf_level; | |
47 | ||
48 | /* Nonzero means display completion help for invalid input */ | |
49 | int auto_help; | |
50 | ||
b278606c | 51 | /* Fread_minibuffer leaves the input here as a string. */ |
f927c5ae JB |
52 | Lisp_Object last_minibuf_string; |
53 | ||
54 | /* Nonzero means let functions called when within a minibuffer | |
55 | invoke recursive minibuffers (to read arguments, or whatever) */ | |
56 | int enable_recursive_minibuffers; | |
57 | ||
58 | /* help-form is bound to this while in the minibuffer. */ | |
59 | ||
60 | Lisp_Object Vminibuffer_help_form; | |
61 | ||
770970cb RS |
62 | /* Variable which is the history list to add minibuffer values to. */ |
63 | ||
64 | Lisp_Object Vminibuffer_history_variable; | |
65 | ||
66 | /* Current position in the history list (adjusted by M-n and M-p). */ | |
67 | ||
68 | Lisp_Object Vminibuffer_history_position; | |
69 | ||
70 | Lisp_Object Qminibuffer_history; | |
71 | ||
719b4a40 RS |
72 | Lisp_Object Qread_file_name_internal; |
73 | ||
177aecf9 | 74 | /* Normal hooks for entry to and exit from minibuffer. */ |
5c781212 RS |
75 | |
76 | Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook; | |
177aecf9 | 77 | Lisp_Object Qminibuffer_exit_hook, Vminibuffer_exit_hook; |
5c781212 | 78 | |
f927c5ae JB |
79 | /* Nonzero means completion ignores case. */ |
80 | ||
81 | int completion_ignore_case; | |
82 | ||
42006772 RS |
83 | /* List of regexps that should restrict possible completions. */ |
84 | ||
85 | Lisp_Object Vcompletion_regexp_list; | |
86 | ||
6a9ee000 RS |
87 | /* Nonzero means raise the minibuffer frame when the minibuffer |
88 | is entered. */ | |
89 | ||
90 | int minibuffer_auto_raise; | |
91 | ||
f927c5ae JB |
92 | /* If last completion attempt reported "Complete but not unique" |
93 | then this is the string completed then; otherwise this is nil. */ | |
94 | ||
95 | static Lisp_Object last_exact_completion; | |
96 | ||
97 | Lisp_Object Quser_variable_p; | |
2cb6da5c RS |
98 | |
99 | /* Non-nil means it is the window for C-M-v to scroll | |
100 | when the minibuffer is selected. */ | |
101 | extern Lisp_Object Vminibuf_scroll_window; | |
30e13e56 RS |
102 | |
103 | extern Lisp_Object Voverriding_local_map; | |
f927c5ae JB |
104 | \f |
105 | /* Actual minibuffer invocation. */ | |
106 | ||
107 | void read_minibuf_unwind (); | |
108 | Lisp_Object get_minibuffer (); | |
109 | Lisp_Object read_minibuf (); | |
110 | ||
770970cb | 111 | /* Read from the minibuffer using keymap MAP, initial contents INITIAL |
85b5fe07 | 112 | (a string), putting point minus BACKUP_N chars from the end of INITIAL, |
770970cb | 113 | prompting with PROMPT (a string), using history list HISTVAR |
85b5fe07 | 114 | with initial position HISTPOS. (BACKUP_N should be <= 0.) |
770970cb RS |
115 | |
116 | Normally return the result as a string (the text that was read), | |
3ab14176 | 117 | but if EXPFLAG is nonzero, read it and return the object read. |
b278606c BF |
118 | If HISTVAR is given, save the value read on that history only if it doesn't |
119 | match the front of that history list exactly. The value is pushed onto | |
be765114 | 120 | the list as the string that was read. */ |
770970cb | 121 | |
f927c5ae | 122 | Lisp_Object |
770970cb | 123 | read_minibuf (map, initial, prompt, backup_n, expflag, histvar, histpos) |
f927c5ae JB |
124 | Lisp_Object map; |
125 | Lisp_Object initial; | |
126 | Lisp_Object prompt; | |
5061d9c3 | 127 | Lisp_Object backup_n; |
f927c5ae | 128 | int expflag; |
770970cb RS |
129 | Lisp_Object histvar; |
130 | Lisp_Object histpos; | |
f927c5ae | 131 | { |
00a34088 | 132 | Lisp_Object val; |
f927c5ae | 133 | int count = specpdl_ptr - specpdl; |
5061d9c3 | 134 | Lisp_Object mini_frame; |
00a34088 RS |
135 | struct gcpro gcpro1, gcpro2, gcpro3; |
136 | ||
08f7d623 | 137 | single_kboard_state (); |
718d3251 | 138 | |
00a34088 RS |
139 | val = Qnil; |
140 | /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we | |
141 | store them away before we can GC. Don't need to protect | |
142 | BACKUP_N because we use the value only if it is an integer. */ | |
143 | GCPRO3 (map, initial, val); | |
f927c5ae | 144 | |
7510b296 | 145 | if (!STRINGP (prompt)) |
f927c5ae JB |
146 | prompt = build_string (""); |
147 | ||
f927c5ae JB |
148 | if (!enable_recursive_minibuffers |
149 | && minibuf_level > 0 | |
150 | && (EQ (selected_window, minibuf_window))) | |
f927c5ae JB |
151 | error ("Command attempted to use minibuffer while in minibuffer"); |
152 | ||
4d04c1f1 | 153 | /* Could we simply bind these variables instead? */ |
c5b6b680 RS |
154 | minibuf_save_list |
155 | = Fcons (Voverriding_local_map, | |
156 | Fcons (minibuf_window, minibuf_save_list)); | |
4d04c1f1 KH |
157 | minibuf_save_list |
158 | = Fcons (minibuf_prompt, | |
159 | Fcons (make_number (minibuf_prompt_width), | |
160 | Fcons (Vhelp_form, | |
ee9e37ab | 161 | Fcons (Vcurrent_prefix_arg, |
4d04c1f1 KH |
162 | Fcons (Vminibuffer_history_position, |
163 | Fcons (Vminibuffer_history_variable, | |
c5b6b680 RS |
164 | minibuf_save_list)))))); |
165 | ||
00a34088 RS |
166 | minibuf_prompt_width = 0; /* xdisp.c puts in the right value. */ |
167 | minibuf_prompt = Fcopy_sequence (prompt); | |
168 | Vminibuffer_history_position = histpos; | |
169 | Vminibuffer_history_variable = histvar; | |
f927c5ae | 170 | |
c5b6b680 RS |
171 | choose_minibuf_frame (); |
172 | ||
f927c5ae | 173 | record_unwind_protect (Fset_window_configuration, |
b2b2c677 JB |
174 | Fcurrent_window_configuration (Qnil)); |
175 | ||
ff11dfa1 JB |
176 | /* If the minibuffer window is on a different frame, save that |
177 | frame's configuration too. */ | |
5061d9c3 | 178 | #ifdef MULTI_FRAME |
75f00e72 | 179 | mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window)); |
ff11dfa1 | 180 | if (XFRAME (mini_frame) != selected_frame) |
5061d9c3 RS |
181 | record_unwind_protect (Fset_window_configuration, |
182 | Fcurrent_window_configuration (mini_frame)); | |
5563e8e8 KH |
183 | |
184 | /* If the minibuffer is on an iconified or invisible frame, | |
185 | make it visible now. */ | |
186 | Fmake_frame_visible (mini_frame); | |
187 | ||
6a9ee000 RS |
188 | if (minibuffer_auto_raise) |
189 | Fraise_frame (mini_frame); | |
5061d9c3 | 190 | #endif |
f927c5ae JB |
191 | |
192 | val = current_buffer->directory; | |
193 | Fset_buffer (get_minibuffer (minibuf_level)); | |
64a3a3c0 JB |
194 | |
195 | /* The current buffer's default directory is usually the right thing | |
196 | for our minibuffer here. However, if you're typing a command at | |
197 | a minibuffer-only frame when minibuf_level is zero, then buf IS | |
198 | the current_buffer, so reset_buffer leaves buf's default | |
199 | directory unchanged. This is a bummer when you've just started | |
200 | up Emacs and buf's default directory is Qnil. Here's a hack; can | |
201 | you think of something better to do? Find another buffer with a | |
202 | better directory, and use that one instead. */ | |
7510b296 | 203 | if (STRINGP (val)) |
64a3a3c0 JB |
204 | current_buffer->directory = val; |
205 | else | |
206 | { | |
207 | Lisp_Object buf_list; | |
208 | ||
209 | for (buf_list = Vbuffer_alist; | |
210 | CONSP (buf_list); | |
211 | buf_list = XCONS (buf_list)->cdr) | |
212 | { | |
1e62748e | 213 | Lisp_Object other_buf; |
64a3a3c0 | 214 | |
1e62748e | 215 | other_buf = XCONS (XCONS (buf_list)->car)->cdr; |
7510b296 | 216 | if (STRINGP (XBUFFER (other_buf)->directory)) |
64a3a3c0 JB |
217 | { |
218 | current_buffer->directory = XBUFFER (other_buf)->directory; | |
219 | break; | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
33b1baf6 | 224 | #ifdef MULTI_FRAME |
0abbff13 KH |
225 | if (XFRAME (mini_frame) != selected_frame) |
226 | Fredirect_frame_focus (Fselected_frame (), mini_frame); | |
33b1baf6 | 227 | #endif |
f927c5ae JB |
228 | Fmake_local_variable (Qprint_escape_newlines); |
229 | print_escape_newlines = 1; | |
230 | ||
43bad991 | 231 | record_unwind_protect (read_minibuf_unwind, Qnil); |
43bad991 | 232 | |
f927c5ae JB |
233 | Vminibuf_scroll_window = selected_window; |
234 | Fset_window_buffer (minibuf_window, Fcurrent_buffer ()); | |
f927c5ae | 235 | Fselect_window (minibuf_window); |
5a866662 | 236 | XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0); |
f927c5ae JB |
237 | |
238 | Ferase_buffer (); | |
239 | minibuf_level++; | |
f927c5ae | 240 | |
56a98455 | 241 | if (!NILP (initial)) |
f927c5ae JB |
242 | { |
243 | Finsert (1, &initial); | |
7510b296 | 244 | if (!NILP (backup_n) && INTEGERP (backup_n)) |
f927c5ae JB |
245 | Fforward_char (backup_n); |
246 | } | |
247 | ||
f927c5ae | 248 | echo_area_glyphs = 0; |
73168c8b RS |
249 | /* This is in case the minibuffer-setup-hook calls Fsit_for. */ |
250 | previous_echo_glyphs = 0; | |
f927c5ae JB |
251 | |
252 | Vhelp_form = Vminibuffer_help_form; | |
253 | current_buffer->keymap = map; | |
254 | ||
5c781212 RS |
255 | /* Run our hook, but not if it is empty. |
256 | (run-hooks would do nothing if it is empty, | |
257 | but it's important to save time here in the usual case. */ | |
92d3b06e RS |
258 | if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound) |
259 | && !NILP (Vrun_hooks)) | |
5c781212 RS |
260 | call1 (Vrun_hooks, Qminibuffer_setup_hook); |
261 | ||
f927c5ae JB |
262 | /* ??? MCC did redraw_screen here if switching screens. */ |
263 | recursive_edit_1 (); | |
264 | ||
265 | /* If cursor is on the minibuffer line, | |
266 | show the user we have exited by putting it in column 0. */ | |
ff11dfa1 | 267 | if ((FRAME_CURSOR_Y (selected_frame) |
f927c5ae JB |
268 | >= XFASTINT (XWINDOW (minibuf_window)->top)) |
269 | && !noninteractive) | |
270 | { | |
ff11dfa1 JB |
271 | FRAME_CURSOR_X (selected_frame) = 0; |
272 | update_frame (selected_frame, 1, 1); | |
f927c5ae JB |
273 | } |
274 | ||
275 | /* Make minibuffer contents into a string */ | |
ffd56f97 | 276 | val = make_buffer_string (1, Z); |
f927c5ae | 277 | bcopy (GAP_END_ADDR, XSTRING (val)->data + GPT - BEG, Z - GPT); |
770970cb | 278 | |
b278606c BF |
279 | /* VAL is the string of minibuffer text. */ |
280 | last_minibuf_string = val; | |
281 | ||
3ab14176 | 282 | /* Add the value to the appropriate history list unless it is empty. */ |
9f6131cf | 283 | if (XSTRING (val)->size != 0 |
3ab14176 KH |
284 | && SYMBOLP (Vminibuffer_history_variable) |
285 | && ! EQ (XSYMBOL (Vminibuffer_history_variable)->value, Qunbound)) | |
286 | { | |
287 | /* If the caller wanted to save the value read on a history list, | |
288 | then do so if the value is not already the front of the list. */ | |
289 | Lisp_Object histval; | |
290 | histval = Fsymbol_value (Vminibuffer_history_variable); | |
291 | ||
292 | /* The value of the history variable must be a cons or nil. Other | |
293 | values are unacceptable. We silently ignore these values. */ | |
294 | if (NILP (histval) | |
9f6131cf RS |
295 | || (CONSP (histval) |
296 | && NILP (Fequal (last_minibuf_string, Fcar (histval))))) | |
297 | Fset (Vminibuffer_history_variable, | |
298 | Fcons (last_minibuf_string, histval)); | |
299 | } | |
300 | ||
301 | /* If Lisp form desired instead of string, parse it. */ | |
302 | if (expflag) | |
303 | { | |
304 | Lisp_Object expr_and_pos; | |
305 | unsigned char *p; | |
306 | ||
307 | expr_and_pos = Fread_from_string (val, Qnil, Qnil); | |
308 | /* Ignore trailing whitespace; any other trailing junk is an error. */ | |
309 | for (p = XSTRING (val)->data + XINT (Fcdr (expr_and_pos)); *p; p++) | |
310 | if (*p != ' ' && *p != '\t' && *p != '\n') | |
311 | error ("Trailing garbage following expression"); | |
312 | val = Fcar (expr_and_pos); | |
3ab14176 KH |
313 | } |
314 | ||
00a34088 RS |
315 | /* The appropriate frame will get selected |
316 | in set-window-configuration. */ | |
317 | RETURN_UNGCPRO (unbind_to (count, val)); | |
f927c5ae JB |
318 | } |
319 | ||
320 | /* Return a buffer to be used as the minibuffer at depth `depth'. | |
321 | depth = 0 is the lowest allowed argument, and that is the value | |
322 | used for nonrecursive minibuffer invocations */ | |
323 | ||
324 | Lisp_Object | |
325 | get_minibuffer (depth) | |
326 | int depth; | |
327 | { | |
328 | Lisp_Object tail, num, buf; | |
9f6c23bc | 329 | char name[24]; |
f927c5ae JB |
330 | extern Lisp_Object nconc2 (); |
331 | ||
5a866662 | 332 | XSETFASTINT (num, depth); |
f927c5ae | 333 | tail = Fnthcdr (num, Vminibuffer_list); |
56a98455 | 334 | if (NILP (tail)) |
f927c5ae JB |
335 | { |
336 | tail = Fcons (Qnil, Qnil); | |
337 | Vminibuffer_list = nconc2 (Vminibuffer_list, tail); | |
338 | } | |
339 | buf = Fcar (tail); | |
56a98455 | 340 | if (NILP (buf) || NILP (XBUFFER (buf)->name)) |
f927c5ae JB |
341 | { |
342 | sprintf (name, " *Minibuf-%d*", depth); | |
343 | buf = Fget_buffer_create (build_string (name)); | |
5d6533f1 JB |
344 | |
345 | /* Although the buffer's name starts with a space, undo should be | |
346 | enabled in it. */ | |
347 | Fbuffer_enable_undo (buf); | |
348 | ||
f927c5ae JB |
349 | XCONS (tail)->car = buf; |
350 | } | |
351 | else | |
5956f71d | 352 | { |
6b3faad8 RS |
353 | int count = specpdl_ptr - specpdl; |
354 | ||
5956f71d | 355 | reset_buffer (XBUFFER (buf)); |
6b3faad8 RS |
356 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); |
357 | Fset_buffer (buf); | |
358 | Fkill_all_local_variables (); | |
359 | unbind_to (count, Qnil); | |
5956f71d | 360 | } |
64a3a3c0 | 361 | |
f927c5ae JB |
362 | return buf; |
363 | } | |
364 | ||
365 | /* This function is called on exiting minibuffer, whether normally or not, | |
366 | and it restores the current window, buffer, etc. */ | |
367 | ||
368 | void | |
43bad991 JB |
369 | read_minibuf_unwind (data) |
370 | Lisp_Object data; | |
f927c5ae | 371 | { |
c24e1160 RS |
372 | Lisp_Object old_deactivate_mark; |
373 | ||
0a1dd1c5 RS |
374 | /* We are exiting the minibuffer one way or the other, |
375 | so run the hook. */ | |
376 | if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound) | |
377 | && !NILP (Vrun_hooks)) | |
378 | call1 (Vrun_hooks, Qminibuffer_exit_hook); | |
379 | ||
f927c5ae JB |
380 | /* Erase the minibuffer we were using at this level. */ |
381 | Fset_buffer (XWINDOW (minibuf_window)->buffer); | |
382 | ||
383 | /* Prevent error in erase-buffer. */ | |
384 | current_buffer->read_only = Qnil; | |
c24e1160 RS |
385 | |
386 | old_deactivate_mark = Vdeactivate_mark; | |
f927c5ae | 387 | Ferase_buffer (); |
c24e1160 | 388 | Vdeactivate_mark = old_deactivate_mark; |
f927c5ae JB |
389 | |
390 | /* If this was a recursive minibuffer, | |
391 | tie the minibuffer window back to the outer level minibuffer buffer */ | |
392 | minibuf_level--; | |
393 | /* Make sure minibuffer window is erased, not ignored */ | |
394 | windows_or_buffers_changed++; | |
5a866662 | 395 | XSETFASTINT (XWINDOW (minibuf_window)->last_modified, 0); |
f927c5ae | 396 | |
4d04c1f1 KH |
397 | /* Restore prompt, etc from outer minibuffer */ |
398 | minibuf_prompt = Fcar (minibuf_save_list); | |
399 | minibuf_save_list = Fcdr (minibuf_save_list); | |
400 | minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list)); | |
401 | minibuf_save_list = Fcdr (minibuf_save_list); | |
402 | Vhelp_form = Fcar (minibuf_save_list); | |
403 | minibuf_save_list = Fcdr (minibuf_save_list); | |
ee9e37ab | 404 | Vcurrent_prefix_arg = Fcar (minibuf_save_list); |
4d04c1f1 KH |
405 | minibuf_save_list = Fcdr (minibuf_save_list); |
406 | Vminibuffer_history_position = Fcar (minibuf_save_list); | |
407 | minibuf_save_list = Fcdr (minibuf_save_list); | |
408 | Vminibuffer_history_variable = Fcar (minibuf_save_list); | |
409 | minibuf_save_list = Fcdr (minibuf_save_list); | |
30e13e56 | 410 | Voverriding_local_map = Fcar (minibuf_save_list); |
c5b6b680 RS |
411 | minibuf_save_list = Fcdr (minibuf_save_list); |
412 | minibuf_window = Fcar (minibuf_save_list); | |
30e13e56 | 413 | minibuf_save_list = Fcdr (minibuf_save_list); |
f927c5ae JB |
414 | } |
415 | \f | |
b9d721de JB |
416 | |
417 | /* This comment supplies the doc string for read-from-minibuffer, | |
418 | for make-docfile to see. We cannot put this in the real DEFUN | |
419 | due to limits in the Unix cpp. | |
420 | ||
f927c5ae JB |
421 | DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0, |
422 | "Read a string from the minibuffer, prompting with string PROMPT.\n\ | |
423 | If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\ | |
424 | to be inserted into the minibuffer before reading input.\n\ | |
770970cb RS |
425 | If INITIAL-CONTENTS is (STRING . POSITION), the initial input\n\ |
426 | is STRING, but point is placed POSITION characters into the string.\n\ | |
f927c5ae JB |
427 | Third arg KEYMAP is a keymap to use whilst reading;\n\ |
428 | if omitted or nil, the default is `minibuffer-local-map'.\n\ | |
429 | If fourth arg READ is non-nil, then interpret the result as a lisp object\n\ | |
430 | and return that object:\n\ | |
431 | in other words, do `(car (read-from-string INPUT-STRING))'\n\ | |
770970cb RS |
432 | Fifth arg HIST, if non-nil, specifies a history list\n\ |
433 | and optionally the initial position in the list.\n\ | |
434 | It can be a symbol, which is the history list variable to use,\n\ | |
435 | or it can be a cons cell (HISTVAR . HISTPOS).\n\ | |
436 | In that case, HISTVAR is the history list variable to use,\n\ | |
437 | and HISTPOS is the initial position (the position in the list\n\ | |
438 | which INITIAL-CONTENTS corresponds to).\n\ | |
b9d721de JB |
439 | Positions are counted starting from 1 at the beginning of the list." |
440 | */ | |
441 | ||
442 | DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 5, 0, | |
443 | 0 /* See immediately above */) | |
68e5a8a2 RS |
444 | (prompt, initial_contents, keymap, read, hist) |
445 | Lisp_Object prompt, initial_contents, keymap, read, hist; | |
f927c5ae JB |
446 | { |
447 | int pos = 0; | |
770970cb RS |
448 | Lisp_Object histvar, histpos, position; |
449 | position = Qnil; | |
f927c5ae JB |
450 | |
451 | CHECK_STRING (prompt, 0); | |
68e5a8a2 | 452 | if (!NILP (initial_contents)) |
f927c5ae | 453 | { |
7510b296 | 454 | if (CONSP (initial_contents)) |
770970cb | 455 | { |
68e5a8a2 RS |
456 | position = Fcdr (initial_contents); |
457 | initial_contents = Fcar (initial_contents); | |
770970cb | 458 | } |
68e5a8a2 | 459 | CHECK_STRING (initial_contents, 1); |
56a98455 | 460 | if (!NILP (position)) |
f927c5ae JB |
461 | { |
462 | CHECK_NUMBER (position, 0); | |
463 | /* Convert to distance from end of input. */ | |
68e5a8a2 | 464 | pos = XINT (position) - 1 - XSTRING (initial_contents)->size; |
f927c5ae JB |
465 | } |
466 | } | |
467 | ||
56a98455 | 468 | if (NILP (keymap)) |
f927c5ae JB |
469 | keymap = Vminibuffer_local_map; |
470 | else | |
471 | keymap = get_keymap (keymap,2); | |
770970cb | 472 | |
7510b296 | 473 | if (SYMBOLP (hist)) |
770970cb RS |
474 | { |
475 | histvar = hist; | |
476 | histpos = Qnil; | |
477 | } | |
478 | else | |
479 | { | |
480 | histvar = Fcar_safe (hist); | |
481 | histpos = Fcdr_safe (hist); | |
482 | } | |
483 | if (NILP (histvar)) | |
484 | histvar = Qminibuffer_history; | |
485 | if (NILP (histpos)) | |
5a866662 | 486 | XSETFASTINT (histpos, 0); |
770970cb | 487 | |
68e5a8a2 | 488 | return read_minibuf (keymap, initial_contents, prompt, |
85b5fe07 | 489 | make_number (pos), !NILP (read), histvar, histpos); |
f927c5ae JB |
490 | } |
491 | ||
492 | DEFUN ("read-minibuffer", Fread_minibuffer, Sread_minibuffer, 1, 2, 0, | |
493 | "Return a Lisp object read using the minibuffer.\n\ | |
494 | Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\ | |
495 | is a string to insert in the minibuffer before reading.") | |
496 | (prompt, initial_contents) | |
497 | Lisp_Object prompt, initial_contents; | |
498 | { | |
499 | CHECK_STRING (prompt, 0); | |
56a98455 | 500 | if (!NILP (initial_contents)) |
a1b4b084 | 501 | CHECK_STRING (initial_contents, 1); |
770970cb RS |
502 | return read_minibuf (Vminibuffer_local_map, initial_contents, |
503 | prompt, Qnil, 1, Qminibuffer_history, make_number (0)); | |
f927c5ae JB |
504 | } |
505 | ||
506 | DEFUN ("eval-minibuffer", Feval_minibuffer, Seval_minibuffer, 1, 2, 0, | |
507 | "Return value of Lisp expression read using the minibuffer.\n\ | |
508 | Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\ | |
509 | is a string to insert in the minibuffer before reading.") | |
510 | (prompt, initial_contents) | |
511 | Lisp_Object prompt, initial_contents; | |
512 | { | |
513 | return Feval (Fread_minibuffer (prompt, initial_contents)); | |
514 | } | |
515 | ||
516 | /* Functions that use the minibuffer to read various things. */ | |
517 | ||
80896ab4 | 518 | DEFUN ("read-string", Fread_string, Sread_string, 1, 3, 0, |
f927c5ae | 519 | "Read a string from the minibuffer, prompting with string PROMPT.\n\ |
80896ab4 RS |
520 | If non-nil, second arg INITIAL-INPUT is a string to insert before reading.\n\ |
521 | The third arg HISTORY, if non-nil, specifies a history list\n\ | |
522 | and optionally the initial position in the list.\n\ | |
523 | See `read-from-minibuffer' for details of HISTORY argument.") | |
524 | (prompt, initial_input, history) | |
525 | Lisp_Object prompt, initial_input, history; | |
f927c5ae | 526 | { |
80896ab4 | 527 | return Fread_from_minibuffer (prompt, initial_input, Qnil, Qnil, history); |
f927c5ae JB |
528 | } |
529 | ||
b278606c | 530 | DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 2, 0, |
f927c5ae JB |
531 | "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\ |
532 | Prompt with PROMPT, and provide INIT as an initial value of the input string.") | |
533 | (prompt, init) | |
534 | Lisp_Object prompt, init; | |
535 | { | |
536 | CHECK_STRING (prompt, 0); | |
56a98455 | 537 | if (! NILP (init)) |
f927c5ae JB |
538 | CHECK_STRING (init, 1); |
539 | ||
770970cb RS |
540 | return read_minibuf (Vminibuffer_local_ns_map, init, prompt, Qnil, 0, |
541 | Qminibuffer_history, make_number (0)); | |
f927c5ae JB |
542 | } |
543 | ||
544 | DEFUN ("read-command", Fread_command, Sread_command, 1, 1, 0, | |
545 | "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\ | |
546 | Prompts with PROMPT.") | |
547 | (prompt) | |
548 | Lisp_Object prompt; | |
549 | { | |
550 | return Fintern (Fcompleting_read (prompt, Vobarray, Qcommandp, Qt, Qnil, Qnil), | |
551 | Qnil); | |
552 | } | |
553 | ||
554 | #ifdef NOTDEF | |
555 | DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0, | |
556 | "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\ | |
557 | Prompts with PROMPT.") | |
558 | (prompt) | |
559 | Lisp_Object prompt; | |
560 | { | |
561 | return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil), | |
562 | Qnil); | |
563 | } | |
564 | #endif /* NOTDEF */ | |
565 | ||
566 | DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 1, 0, | |
567 | "One arg PROMPT, a string. Read the name of a user variable and return\n\ | |
568 | it as a symbol. Prompts with PROMPT.\n\ | |
569 | A user variable is one whose documentation starts with a `*' character.") | |
570 | (prompt) | |
571 | Lisp_Object prompt; | |
572 | { | |
573 | return Fintern (Fcompleting_read (prompt, Vobarray, | |
574 | Quser_variable_p, Qt, Qnil, Qnil), | |
575 | Qnil); | |
576 | } | |
577 | ||
578 | DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 3, 0, | |
579 | "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\ | |
580 | Prompts with PROMPT.\n\ | |
581 | Optional second arg is value to return if user enters an empty line.\n\ | |
582 | If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.") | |
583 | (prompt, def, require_match) | |
584 | Lisp_Object prompt, def, require_match; | |
585 | { | |
586 | Lisp_Object tem; | |
587 | Lisp_Object args[3]; | |
588 | struct gcpro gcpro1; | |
589 | ||
7510b296 | 590 | if (BUFFERP (def)) |
f927c5ae | 591 | def = XBUFFER (def)->name; |
56a98455 | 592 | if (!NILP (def)) |
f927c5ae JB |
593 | { |
594 | args[0] = build_string ("%s(default %s) "); | |
595 | args[1] = prompt; | |
596 | args[2] = def; | |
597 | prompt = Fformat (3, args); | |
598 | } | |
599 | GCPRO1 (def); | |
600 | tem = Fcompleting_read (prompt, Vbuffer_alist, Qnil, require_match, Qnil, Qnil); | |
601 | UNGCPRO; | |
602 | if (XSTRING (tem)->size) | |
603 | return tem; | |
604 | return def; | |
605 | } | |
606 | \f | |
607 | DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0, | |
608 | "Return common substring of all completions of STRING in ALIST.\n\ | |
609 | Each car of each element of ALIST is tested to see if it begins with STRING.\n\ | |
610 | All that match are compared together; the longest initial sequence\n\ | |
611 | common to all matches is returned as a string.\n\ | |
612 | If there is no match at all, nil is returned.\n\ | |
613 | For an exact match, t is returned.\n\ | |
614 | \n\ | |
615 | ALIST can be an obarray instead of an alist.\n\ | |
616 | Then the print names of all symbols in the obarray are the possible matches.\n\ | |
617 | \n\ | |
618 | ALIST can also be a function to do the completion itself.\n\ | |
619 | It receives three arguments: the values STRING, PREDICATE and nil.\n\ | |
620 | Whatever it returns becomes the value of `try-completion'.\n\ | |
621 | \n\ | |
622 | If optional third argument PREDICATE is non-nil,\n\ | |
623 | it is used to test each possible match.\n\ | |
624 | The match is a candidate only if PREDICATE returns non-nil.\n\ | |
89a255dc RS |
625 | The argument given to PREDICATE is the alist element\n\ |
626 | or the symbol from the obarray.") | |
f927c5ae JB |
627 | (string, alist, pred) |
628 | Lisp_Object string, alist, pred; | |
629 | { | |
630 | Lisp_Object bestmatch, tail, elt, eltstring; | |
631 | int bestmatchsize; | |
632 | int compare, matchsize; | |
56a98455 | 633 | int list = CONSP (alist) || NILP (alist); |
f927c5ae JB |
634 | int index, obsize; |
635 | int matchcount = 0; | |
636 | Lisp_Object bucket, zero, end, tem; | |
637 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
638 | ||
639 | CHECK_STRING (string, 0); | |
7510b296 | 640 | if (!list && !VECTORP (alist)) |
f927c5ae JB |
641 | return call3 (alist, string, pred, Qnil); |
642 | ||
643 | bestmatch = Qnil; | |
644 | ||
645 | /* If ALIST is not a list, set TAIL just for gc pro. */ | |
646 | tail = alist; | |
647 | if (! list) | |
648 | { | |
649 | index = 0; | |
650 | obsize = XVECTOR (alist)->size; | |
651 | bucket = XVECTOR (alist)->contents[index]; | |
652 | } | |
653 | ||
654 | while (1) | |
655 | { | |
656 | /* Get the next element of the alist or obarray. */ | |
657 | /* Exit the loop if the elements are all used up. */ | |
658 | /* elt gets the alist element or symbol. | |
659 | eltstring gets the name to check as a completion. */ | |
660 | ||
661 | if (list) | |
662 | { | |
56a98455 | 663 | if (NILP (tail)) |
f927c5ae JB |
664 | break; |
665 | elt = Fcar (tail); | |
666 | eltstring = Fcar (elt); | |
667 | tail = Fcdr (tail); | |
668 | } | |
669 | else | |
670 | { | |
671 | if (XFASTINT (bucket) != 0) | |
672 | { | |
673 | elt = bucket; | |
674 | eltstring = Fsymbol_name (elt); | |
675 | if (XSYMBOL (bucket)->next) | |
676 | XSETSYMBOL (bucket, XSYMBOL (bucket)->next); | |
677 | else | |
5a866662 | 678 | XSETFASTINT (bucket, 0); |
f927c5ae JB |
679 | } |
680 | else if (++index >= obsize) | |
681 | break; | |
682 | else | |
683 | { | |
684 | bucket = XVECTOR (alist)->contents[index]; | |
685 | continue; | |
686 | } | |
687 | } | |
688 | ||
689 | /* Is this element a possible completion? */ | |
690 | ||
7510b296 | 691 | if (STRINGP (eltstring) |
42006772 RS |
692 | && XSTRING (string)->size <= XSTRING (eltstring)->size |
693 | && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data, | |
694 | XSTRING (string)->size)) | |
f927c5ae JB |
695 | { |
696 | /* Yes. */ | |
42006772 RS |
697 | Lisp_Object regexps; |
698 | Lisp_Object zero; | |
5a866662 | 699 | XSETFASTINT (zero, 0); |
42006772 RS |
700 | |
701 | /* Ignore this element if it fails to match all the regexps. */ | |
702 | for (regexps = Vcompletion_regexp_list; CONSP (regexps); | |
703 | regexps = XCONS (regexps)->cdr) | |
704 | { | |
705 | tem = Fstring_match (XCONS (regexps)->car, eltstring, zero); | |
706 | if (NILP (tem)) | |
707 | break; | |
708 | } | |
709 | if (CONSP (regexps)) | |
710 | continue; | |
711 | ||
f927c5ae JB |
712 | /* Ignore this element if there is a predicate |
713 | and the predicate doesn't like it. */ | |
714 | ||
56a98455 | 715 | if (!NILP (pred)) |
f927c5ae JB |
716 | { |
717 | if (EQ (pred, Qcommandp)) | |
718 | tem = Fcommandp (elt); | |
719 | else | |
720 | { | |
721 | GCPRO4 (tail, string, eltstring, bestmatch); | |
722 | tem = call1 (pred, elt); | |
723 | UNGCPRO; | |
724 | } | |
56a98455 | 725 | if (NILP (tem)) continue; |
f927c5ae JB |
726 | } |
727 | ||
728 | /* Update computation of how much all possible completions match */ | |
729 | ||
730 | matchcount++; | |
56a98455 | 731 | if (NILP (bestmatch)) |
f927c5ae JB |
732 | bestmatch = eltstring, bestmatchsize = XSTRING (eltstring)->size; |
733 | else | |
734 | { | |
735 | compare = min (bestmatchsize, XSTRING (eltstring)->size); | |
736 | matchsize = scmp (XSTRING (bestmatch)->data, | |
737 | XSTRING (eltstring)->data, | |
738 | compare); | |
52b14ac0 JB |
739 | if (matchsize < 0) |
740 | matchsize = compare; | |
741 | if (completion_ignore_case) | |
742 | { | |
743 | /* If this is an exact match except for case, | |
744 | use it as the best match rather than one that is not an | |
745 | exact match. This way, we get the case pattern | |
746 | of the actual match. */ | |
747 | if ((matchsize == XSTRING (eltstring)->size | |
748 | && matchsize < XSTRING (bestmatch)->size) | |
749 | || | |
750 | /* If there is more than one exact match ignoring case, | |
751 | and one of them is exact including case, | |
752 | prefer that one. */ | |
753 | /* If there is no exact match ignoring case, | |
754 | prefer a match that does not change the case | |
755 | of the input. */ | |
756 | ((matchsize == XSTRING (eltstring)->size) | |
757 | == | |
758 | (matchsize == XSTRING (bestmatch)->size) | |
759 | && !bcmp (XSTRING (eltstring)->data, | |
760 | XSTRING (string)->data, XSTRING (string)->size) | |
761 | && bcmp (XSTRING (bestmatch)->data, | |
762 | XSTRING (string)->data, XSTRING (string)->size))) | |
763 | bestmatch = eltstring; | |
764 | } | |
765 | bestmatchsize = matchsize; | |
f927c5ae JB |
766 | } |
767 | } | |
768 | } | |
769 | ||
56a98455 | 770 | if (NILP (bestmatch)) |
f927c5ae | 771 | return Qnil; /* No completions found */ |
52b14ac0 JB |
772 | /* If we are ignoring case, and there is no exact match, |
773 | and no additional text was supplied, | |
774 | don't change the case of what the user typed. */ | |
775 | if (completion_ignore_case && bestmatchsize == XSTRING (string)->size | |
776 | && XSTRING (bestmatch)->size > bestmatchsize) | |
777 | return string; | |
778 | ||
779 | /* Return t if the supplied string is an exact match (counting case); | |
780 | it does not require any change to be made. */ | |
781 | if (matchcount == 1 && bestmatchsize == XSTRING (string)->size | |
782 | && !bcmp (XSTRING (bestmatch)->data, XSTRING (string)->data, | |
783 | bestmatchsize)) | |
f927c5ae JB |
784 | return Qt; |
785 | ||
5a866662 KH |
786 | XSETFASTINT (zero, 0); /* Else extract the part in which */ |
787 | XSETFASTINT (end, bestmatchsize); /* all completions agree */ | |
f927c5ae JB |
788 | return Fsubstring (bestmatch, zero, end); |
789 | } | |
790 | ||
791 | /* Compare exactly LEN chars of strings at S1 and S2, | |
792 | ignoring case if appropriate. | |
793 | Return -1 if strings match, | |
794 | else number of chars that match at the beginning. */ | |
795 | ||
829f7f7c | 796 | int |
f927c5ae | 797 | scmp (s1, s2, len) |
829f7f7c | 798 | register unsigned char *s1, *s2; |
f927c5ae JB |
799 | int len; |
800 | { | |
801 | register int l = len; | |
802 | ||
803 | if (completion_ignore_case) | |
804 | { | |
805 | while (l && DOWNCASE (*s1++) == DOWNCASE (*s2++)) | |
806 | l--; | |
807 | } | |
808 | else | |
809 | { | |
810 | while (l && *s1++ == *s2++) | |
811 | l--; | |
812 | } | |
813 | if (l == 0) | |
814 | return -1; | |
829f7f7c KH |
815 | else |
816 | return len - l; | |
f927c5ae JB |
817 | } |
818 | \f | |
89a255dc | 819 | DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0, |
f927c5ae JB |
820 | "Search for partial matches to STRING in ALIST.\n\ |
821 | Each car of each element of ALIST is tested to see if it begins with STRING.\n\ | |
822 | The value is a list of all the strings from ALIST that match.\n\ | |
89a255dc | 823 | \n\ |
f927c5ae JB |
824 | ALIST can be an obarray instead of an alist.\n\ |
825 | Then the print names of all symbols in the obarray are the possible matches.\n\ | |
826 | \n\ | |
827 | ALIST can also be a function to do the completion itself.\n\ | |
828 | It receives three arguments: the values STRING, PREDICATE and t.\n\ | |
829 | Whatever it returns becomes the value of `all-completion'.\n\ | |
830 | \n\ | |
831 | If optional third argument PREDICATE is non-nil,\n\ | |
832 | it is used to test each possible match.\n\ | |
833 | The match is a candidate only if PREDICATE returns non-nil.\n\ | |
89a255dc RS |
834 | The argument given to PREDICATE is the alist element\n\ |
835 | or the symbol from the obarray.\n\ | |
836 | \n\ | |
837 | If the optional fourth argument HIDE-SPACES is non-nil,\n\ | |
838 | strings in ALIST that start with a space\n\ | |
839 | are ignored unless STRING itself starts with a space.") | |
840 | (string, alist, pred, hide_spaces) | |
841 | Lisp_Object string, alist, pred, hide_spaces; | |
f927c5ae JB |
842 | { |
843 | Lisp_Object tail, elt, eltstring; | |
844 | Lisp_Object allmatches; | |
56a98455 | 845 | int list = CONSP (alist) || NILP (alist); |
f927c5ae JB |
846 | int index, obsize; |
847 | Lisp_Object bucket, tem; | |
848 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
849 | ||
850 | CHECK_STRING (string, 0); | |
7510b296 | 851 | if (!list && !VECTORP (alist)) |
f927c5ae JB |
852 | { |
853 | return call3 (alist, string, pred, Qt); | |
854 | } | |
855 | allmatches = Qnil; | |
856 | ||
857 | /* If ALIST is not a list, set TAIL just for gc pro. */ | |
858 | tail = alist; | |
859 | if (! list) | |
860 | { | |
861 | index = 0; | |
862 | obsize = XVECTOR (alist)->size; | |
863 | bucket = XVECTOR (alist)->contents[index]; | |
864 | } | |
865 | ||
866 | while (1) | |
867 | { | |
868 | /* Get the next element of the alist or obarray. */ | |
869 | /* Exit the loop if the elements are all used up. */ | |
870 | /* elt gets the alist element or symbol. | |
871 | eltstring gets the name to check as a completion. */ | |
872 | ||
873 | if (list) | |
874 | { | |
56a98455 | 875 | if (NILP (tail)) |
f927c5ae JB |
876 | break; |
877 | elt = Fcar (tail); | |
878 | eltstring = Fcar (elt); | |
879 | tail = Fcdr (tail); | |
880 | } | |
881 | else | |
882 | { | |
883 | if (XFASTINT (bucket) != 0) | |
884 | { | |
885 | elt = bucket; | |
886 | eltstring = Fsymbol_name (elt); | |
887 | if (XSYMBOL (bucket)->next) | |
888 | XSETSYMBOL (bucket, XSYMBOL (bucket)->next); | |
889 | else | |
5a866662 | 890 | XSETFASTINT (bucket, 0); |
f927c5ae JB |
891 | } |
892 | else if (++index >= obsize) | |
893 | break; | |
894 | else | |
895 | { | |
896 | bucket = XVECTOR (alist)->contents[index]; | |
897 | continue; | |
898 | } | |
899 | } | |
900 | ||
901 | /* Is this element a possible completion? */ | |
902 | ||
7510b296 | 903 | if (STRINGP (eltstring) |
2cbaf886 | 904 | && XSTRING (string)->size <= XSTRING (eltstring)->size |
89a255dc | 905 | /* If HIDE_SPACES, reject alternatives that start with space |
2cbaf886 RS |
906 | unless the input starts with space. */ |
907 | && ((XSTRING (string)->size > 0 && XSTRING (string)->data[0] == ' ') | |
89a255dc RS |
908 | || XSTRING (eltstring)->data[0] != ' ' |
909 | || NILP (hide_spaces)) | |
2cbaf886 RS |
910 | && 0 > scmp (XSTRING (eltstring)->data, XSTRING (string)->data, |
911 | XSTRING (string)->size)) | |
f927c5ae JB |
912 | { |
913 | /* Yes. */ | |
42006772 RS |
914 | Lisp_Object regexps; |
915 | Lisp_Object zero; | |
5a866662 | 916 | XSETFASTINT (zero, 0); |
42006772 RS |
917 | |
918 | /* Ignore this element if it fails to match all the regexps. */ | |
919 | for (regexps = Vcompletion_regexp_list; CONSP (regexps); | |
920 | regexps = XCONS (regexps)->cdr) | |
921 | { | |
922 | tem = Fstring_match (XCONS (regexps)->car, eltstring, zero); | |
923 | if (NILP (tem)) | |
924 | break; | |
925 | } | |
926 | if (CONSP (regexps)) | |
927 | continue; | |
928 | ||
f927c5ae JB |
929 | /* Ignore this element if there is a predicate |
930 | and the predicate doesn't like it. */ | |
931 | ||
56a98455 | 932 | if (!NILP (pred)) |
f927c5ae JB |
933 | { |
934 | if (EQ (pred, Qcommandp)) | |
935 | tem = Fcommandp (elt); | |
936 | else | |
937 | { | |
938 | GCPRO4 (tail, eltstring, allmatches, string); | |
939 | tem = call1 (pred, elt); | |
940 | UNGCPRO; | |
941 | } | |
56a98455 | 942 | if (NILP (tem)) continue; |
f927c5ae JB |
943 | } |
944 | /* Ok => put it on the list. */ | |
945 | allmatches = Fcons (eltstring, allmatches); | |
946 | } | |
947 | } | |
948 | ||
949 | return Fnreverse (allmatches); | |
950 | } | |
951 | \f | |
952 | Lisp_Object Vminibuffer_completion_table, Qminibuffer_completion_table; | |
953 | Lisp_Object Vminibuffer_completion_predicate, Qminibuffer_completion_predicate; | |
954 | Lisp_Object Vminibuffer_completion_confirm, Qminibuffer_completion_confirm; | |
955 | ||
b9d721de JB |
956 | /* This comment supplies the doc string for completing-read, |
957 | for make-docfile to see. We cannot put this in the real DEFUN | |
958 | due to limits in the Unix cpp. | |
959 | ||
f927c5ae JB |
960 | DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0, |
961 | "Read a string in the minibuffer, with completion.\n\ | |
770970cb | 962 | Args: PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HIST.\n\ |
f927c5ae JB |
963 | PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\ |
964 | TABLE is an alist whose elements' cars are strings, or an obarray.\n\ | |
965 | PREDICATE limits completion to a subset of TABLE.\n\ | |
6b3faad8 RS |
966 | See `try-completion' and `all-completions' for more details |
967 | on completion, TABLE, and PREDICATE.\n\ | |
cbbc3917 | 968 | \n\ |
f927c5ae | 969 | If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\ |
4ced3209 | 970 | the input is (or completes to) an element of TABLE or is null.\n\ |
f927c5ae | 971 | If it is also not t, Return does not exit if it does non-null completion.\n\ |
cbbc3917 RS |
972 | If the input is null, `completing-read' returns nil,\n\ |
973 | regardless of the value of REQUIRE-MATCH.\n\ | |
974 | \n\ | |
f927c5ae | 975 | If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\ |
770970cb RS |
976 | If it is (STRING . POSITION), the initial input\n\ |
977 | is STRING, but point is placed POSITION characters into the string.\n\ | |
978 | HIST, if non-nil, specifies a history list\n\ | |
979 | and optionally the initial position in the list.\n\ | |
980 | It can be a symbol, which is the history list variable to use,\n\ | |
981 | or it can be a cons cell (HISTVAR . HISTPOS).\n\ | |
982 | In that case, HISTVAR is the history list variable to use,\n\ | |
983 | and HISTPOS is the initial position (the position in the list\n\ | |
984 | which INITIAL-CONTENTS corresponds to).\n\ | |
985 | Positions are counted starting from 1 at the beginning of the list.\n\ | |
986 | Completion ignores case if the ambient value of\n\ | |
b9d721de JB |
987 | `completion-ignore-case' is non-nil." |
988 | */ | |
989 | DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 6, 0, | |
990 | 0 /* See immediately above */) | |
770970cb RS |
991 | (prompt, table, pred, require_match, init, hist) |
992 | Lisp_Object prompt, table, pred, require_match, init, hist; | |
f927c5ae | 993 | { |
770970cb RS |
994 | Lisp_Object val, histvar, histpos, position; |
995 | int pos = 0; | |
f927c5ae JB |
996 | int count = specpdl_ptr - specpdl; |
997 | specbind (Qminibuffer_completion_table, table); | |
998 | specbind (Qminibuffer_completion_predicate, pred); | |
999 | specbind (Qminibuffer_completion_confirm, | |
1000 | EQ (require_match, Qt) ? Qnil : Qt); | |
1001 | last_exact_completion = Qnil; | |
770970cb RS |
1002 | |
1003 | position = Qnil; | |
1004 | if (!NILP (init)) | |
1005 | { | |
7510b296 | 1006 | if (CONSP (init)) |
770970cb RS |
1007 | { |
1008 | position = Fcdr (init); | |
1009 | init = Fcar (init); | |
1010 | } | |
1011 | CHECK_STRING (init, 0); | |
1012 | if (!NILP (position)) | |
1013 | { | |
1014 | CHECK_NUMBER (position, 0); | |
1015 | /* Convert to distance from end of input. */ | |
5dadd3a2 | 1016 | pos = XINT (position) - XSTRING (init)->size; |
770970cb RS |
1017 | } |
1018 | } | |
1019 | ||
7510b296 | 1020 | if (SYMBOLP (hist)) |
770970cb RS |
1021 | { |
1022 | histvar = hist; | |
1023 | histpos = Qnil; | |
1024 | } | |
1025 | else | |
1026 | { | |
1027 | histvar = Fcar_safe (hist); | |
1028 | histpos = Fcdr_safe (hist); | |
1029 | } | |
1030 | if (NILP (histvar)) | |
1031 | histvar = Qminibuffer_history; | |
1032 | if (NILP (histpos)) | |
5a866662 | 1033 | XSETFASTINT (histpos, 0); |
770970cb | 1034 | |
56a98455 | 1035 | val = read_minibuf (NILP (require_match) |
f927c5ae JB |
1036 | ? Vminibuffer_local_completion_map |
1037 | : Vminibuffer_local_must_match_map, | |
85b5fe07 | 1038 | init, prompt, make_number (pos), 0, |
770970cb | 1039 | histvar, histpos); |
f927c5ae JB |
1040 | return unbind_to (count, val); |
1041 | } | |
1042 | \f | |
1043 | /* Temporarily display the string M at the end of the current | |
1044 | minibuffer contents. This is used to display things like | |
1045 | "[No Match]" when the user requests a completion for a prefix | |
1046 | that has no possible completions, and other quick, unobtrusive | |
1047 | messages. */ | |
1048 | ||
1049 | temp_echo_area_glyphs (m) | |
1050 | char *m; | |
1051 | { | |
f927c5ae JB |
1052 | int osize = ZV; |
1053 | Lisp_Object oinhibit; | |
1054 | oinhibit = Vinhibit_quit; | |
1055 | ||
896adf84 JB |
1056 | /* Clear out any old echo-area message to make way for our new thing. */ |
1057 | message (0); | |
f927c5ae JB |
1058 | |
1059 | SET_PT (osize); | |
1060 | insert_string (m); | |
1061 | SET_PT (osize); | |
1062 | Vinhibit_quit = Qt; | |
1063 | Fsit_for (make_number (2), Qnil, Qnil); | |
a2dd849e | 1064 | del_range (PT, ZV); |
56a98455 | 1065 | if (!NILP (Vquit_flag)) |
f927c5ae JB |
1066 | { |
1067 | Vquit_flag = Qnil; | |
ba71d84a | 1068 | Vunread_command_events = Fcons (make_number (quit_char), Qnil); |
f927c5ae JB |
1069 | } |
1070 | Vinhibit_quit = oinhibit; | |
1071 | } | |
1072 | ||
1073 | Lisp_Object Fminibuffer_completion_help (); | |
52b14ac0 | 1074 | Lisp_Object assoc_for_completion (); |
5d2ca7ae RS |
1075 | /* A subroutine of Fintern_soft. */ |
1076 | extern Lisp_Object oblookup (); | |
1077 | ||
1078 | ||
1079 | /* Test whether TXT is an exact completion. */ | |
1080 | Lisp_Object | |
1081 | test_completion (txt) | |
1082 | Lisp_Object txt; | |
1083 | { | |
1084 | Lisp_Object tem; | |
1085 | ||
1086 | if (CONSP (Vminibuffer_completion_table) | |
1087 | || NILP (Vminibuffer_completion_table)) | |
1088 | return assoc_for_completion (txt, Vminibuffer_completion_table); | |
1089 | else if (VECTORP (Vminibuffer_completion_table)) | |
1090 | { | |
1091 | /* Bypass intern-soft as that loses for nil */ | |
1092 | tem = oblookup (Vminibuffer_completion_table, | |
1093 | XSTRING (txt)->data, XSTRING (txt)->size); | |
44472c88 | 1094 | if (!SYMBOLP (tem)) |
5d2ca7ae RS |
1095 | return Qnil; |
1096 | else if (!NILP (Vminibuffer_completion_predicate)) | |
1097 | return call1 (Vminibuffer_completion_predicate, tem); | |
1098 | else | |
1099 | return Qt; | |
1100 | } | |
1101 | else | |
1102 | return call3 (Vminibuffer_completion_table, txt, | |
1103 | Vminibuffer_completion_predicate, Qlambda); | |
1104 | } | |
f927c5ae JB |
1105 | |
1106 | /* returns: | |
1107 | * 0 no possible completion | |
1108 | * 1 was already an exact and unique completion | |
1109 | * 3 was already an exact completion | |
1110 | * 4 completed to an exact completion | |
1111 | * 5 some completion happened | |
1112 | * 6 no completion happened | |
1113 | */ | |
1114 | int | |
1115 | do_completion () | |
1116 | { | |
1117 | Lisp_Object completion, tem; | |
1118 | int completedp; | |
1119 | Lisp_Object last; | |
1e00c2ff | 1120 | struct gcpro gcpro1, gcpro2; |
f927c5ae JB |
1121 | |
1122 | completion = Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table, | |
1123 | Vminibuffer_completion_predicate); | |
1124 | last = last_exact_completion; | |
1125 | last_exact_completion = Qnil; | |
1126 | ||
4f9b95e5 KH |
1127 | GCPRO2 (completion, last); |
1128 | ||
56a98455 | 1129 | if (NILP (completion)) |
f927c5ae JB |
1130 | { |
1131 | bitch_at_user (); | |
1132 | temp_echo_area_glyphs (" [No match]"); | |
1e00c2ff | 1133 | UNGCPRO; |
f927c5ae JB |
1134 | return 0; |
1135 | } | |
1136 | ||
1137 | if (EQ (completion, Qt)) /* exact and unique match */ | |
1e00c2ff KH |
1138 | { |
1139 | UNGCPRO; | |
1140 | return 1; | |
1141 | } | |
f927c5ae JB |
1142 | |
1143 | /* compiler bug */ | |
1144 | tem = Fstring_equal (completion, Fbuffer_string()); | |
56a98455 | 1145 | if (completedp = NILP (tem)) |
f927c5ae JB |
1146 | { |
1147 | Ferase_buffer (); /* Some completion happened */ | |
1148 | Finsert (1, &completion); | |
1149 | } | |
1150 | ||
1151 | /* It did find a match. Do we match some possibility exactly now? */ | |
5d2ca7ae | 1152 | tem = test_completion (Fbuffer_string ()); |
56a98455 | 1153 | if (NILP (tem)) |
1e00c2ff KH |
1154 | { |
1155 | /* not an exact match */ | |
1156 | UNGCPRO; | |
f927c5ae JB |
1157 | if (completedp) |
1158 | return 5; | |
1159 | else if (auto_help) | |
1160 | Fminibuffer_completion_help (); | |
1161 | else | |
1162 | temp_echo_area_glyphs (" [Next char not unique]"); | |
1163 | return 6; | |
1164 | } | |
1165 | else if (completedp) | |
1e00c2ff KH |
1166 | { |
1167 | UNGCPRO; | |
1168 | return 4; | |
1169 | } | |
f927c5ae JB |
1170 | /* If the last exact completion and this one were the same, |
1171 | it means we've already given a "Complete but not unique" | |
52b14ac0 | 1172 | message and the user's hit TAB again, so now we give him help. */ |
f927c5ae | 1173 | last_exact_completion = completion; |
56a98455 | 1174 | if (!NILP (last)) |
f927c5ae JB |
1175 | { |
1176 | tem = Fbuffer_string (); | |
56a98455 | 1177 | if (!NILP (Fequal (tem, last))) |
f927c5ae JB |
1178 | Fminibuffer_completion_help (); |
1179 | } | |
1e00c2ff | 1180 | UNGCPRO; |
f927c5ae | 1181 | return 3; |
f927c5ae | 1182 | } |
1e00c2ff | 1183 | |
52b14ac0 JB |
1184 | /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */ |
1185 | ||
1186 | Lisp_Object | |
1187 | assoc_for_completion (key, list) | |
1188 | register Lisp_Object key; | |
1189 | Lisp_Object list; | |
1190 | { | |
1191 | register Lisp_Object tail; | |
1192 | ||
1193 | if (completion_ignore_case) | |
1194 | key = Fupcase (key); | |
1195 | ||
56a98455 | 1196 | for (tail = list; !NILP (tail); tail = Fcdr (tail)) |
52b14ac0 JB |
1197 | { |
1198 | register Lisp_Object elt, tem, thiscar; | |
1199 | elt = Fcar (tail); | |
1200 | if (!CONSP (elt)) continue; | |
1201 | thiscar = Fcar (elt); | |
7510b296 | 1202 | if (!STRINGP (thiscar)) |
52b14ac0 JB |
1203 | continue; |
1204 | if (completion_ignore_case) | |
1205 | thiscar = Fupcase (thiscar); | |
1206 | tem = Fequal (thiscar, key); | |
56a98455 | 1207 | if (!NILP (tem)) return elt; |
52b14ac0 JB |
1208 | QUIT; |
1209 | } | |
1210 | return Qnil; | |
1211 | } | |
f927c5ae JB |
1212 | |
1213 | DEFUN ("minibuffer-complete", Fminibuffer_complete, Sminibuffer_complete, 0, 0, "", | |
6300d782 | 1214 | "Complete the minibuffer contents as far as possible.\n\ |
2cb6da5c RS |
1215 | Return nil if there is no valid completion, else t.\n\ |
1216 | If no characters can be completed, display a list of possible completions.\n\ | |
1217 | If you repeat this command after it displayed such a list,\n\ | |
1218 | scroll the window of possible completions.") | |
f927c5ae JB |
1219 | () |
1220 | { | |
2cb6da5c RS |
1221 | register int i; |
1222 | Lisp_Object window, tem; | |
1223 | ||
1224 | /* If the previous command was not this, then mark the completion | |
1225 | buffer obsolete. */ | |
5221fd63 | 1226 | if (! EQ (current_kboard->Vlast_command, this_command)) |
2cb6da5c RS |
1227 | Vminibuf_scroll_window = Qnil; |
1228 | ||
1229 | window = Vminibuf_scroll_window; | |
1230 | /* If there's a fresh completion window with a live buffer, | |
1231 | and this command is repeated, scroll that window. */ | |
1232 | if (! NILP (window) && ! NILP (XWINDOW (window)->buffer) | |
1233 | && !NILP (XBUFFER (XWINDOW (window)->buffer)->name)) | |
1234 | { | |
1235 | struct buffer *obuf = current_buffer; | |
1236 | ||
1237 | Fset_buffer (XWINDOW (window)->buffer); | |
1238 | tem = Fpos_visible_in_window_p (make_number (ZV), window); | |
1239 | if (! NILP (tem)) | |
1240 | /* If end is in view, scroll up to the beginning. */ | |
8768d630 | 1241 | Fset_window_start (window, BEGV, Qnil); |
2cb6da5c RS |
1242 | else |
1243 | /* Else scroll down one screen. */ | |
1244 | Fscroll_other_window (Qnil); | |
1245 | ||
1246 | set_buffer_internal (obuf); | |
1247 | return Qnil; | |
1248 | } | |
1249 | ||
1250 | i = do_completion (); | |
f927c5ae JB |
1251 | switch (i) |
1252 | { | |
1253 | case 0: | |
1254 | return Qnil; | |
1255 | ||
1256 | case 1: | |
1257 | temp_echo_area_glyphs (" [Sole completion]"); | |
1258 | break; | |
1259 | ||
1260 | case 3: | |
1261 | temp_echo_area_glyphs (" [Complete, but not unique]"); | |
1262 | break; | |
1263 | } | |
1264 | ||
1265 | return Qt; | |
1266 | } | |
e4c97a67 RS |
1267 | \f |
1268 | /* Subroutines of Fminibuffer_complete_and_exit. */ | |
1269 | ||
1270 | /* This one is called by internal_condition_case to do the real work. */ | |
1271 | ||
1272 | Lisp_Object | |
1273 | complete_and_exit_1 () | |
1274 | { | |
1275 | return make_number (do_completion ()); | |
1276 | } | |
1277 | ||
1278 | /* This one is called by internal_condition_case if an error happens. | |
1279 | Pretend the current value is an exact match. */ | |
1280 | ||
1281 | Lisp_Object | |
1282 | complete_and_exit_2 (ignore) | |
1283 | Lisp_Object ignore; | |
1284 | { | |
1285 | return make_number (1); | |
1286 | } | |
f927c5ae JB |
1287 | |
1288 | DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit, | |
1289 | Sminibuffer_complete_and_exit, 0, 0, "", | |
5d2ca7ae RS |
1290 | "If the minibuffer contents is a valid completion then exit.\n\ |
1291 | Otherwise try to complete it. If completion leads to a valid completion,\n\ | |
f927c5ae JB |
1292 | a repetition of this command will exit.") |
1293 | () | |
1294 | { | |
1295 | register int i; | |
e4c97a67 | 1296 | Lisp_Object val; |
f927c5ae JB |
1297 | |
1298 | /* Allow user to specify null string */ | |
1299 | if (BEGV == ZV) | |
1300 | goto exit; | |
1301 | ||
5d2ca7ae RS |
1302 | if (!NILP (test_completion (Fbuffer_string ()))) |
1303 | goto exit; | |
1304 | ||
e4c97a67 RS |
1305 | /* Call do_completion, but ignore errors. */ |
1306 | val = internal_condition_case (complete_and_exit_1, Qerror, | |
1307 | complete_and_exit_2); | |
1308 | ||
1309 | i = XFASTINT (val); | |
f927c5ae JB |
1310 | switch (i) |
1311 | { | |
1312 | case 1: | |
1313 | case 3: | |
1314 | goto exit; | |
1315 | ||
1316 | case 4: | |
56a98455 | 1317 | if (!NILP (Vminibuffer_completion_confirm)) |
f927c5ae JB |
1318 | { |
1319 | temp_echo_area_glyphs (" [Confirm]"); | |
1320 | return Qnil; | |
1321 | } | |
1322 | else | |
1323 | goto exit; | |
1324 | ||
1325 | default: | |
1326 | return Qnil; | |
1327 | } | |
1328 | exit: | |
1329 | Fthrow (Qexit, Qnil); | |
1330 | /* NOTREACHED */ | |
1331 | } | |
1332 | ||
1333 | DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word, Sminibuffer_complete_word, | |
1334 | 0, 0, "", | |
1335 | "Complete the minibuffer contents at most a single word.\n\ | |
1336 | After one word is completed as much as possible, a space or hyphen\n\ | |
6300d782 KH |
1337 | is added, provided that matches some possible completion.\n\ |
1338 | Return nil if there is no valid completion, else t.") | |
f927c5ae JB |
1339 | () |
1340 | { | |
1341 | Lisp_Object completion, tem; | |
1342 | register int i; | |
1343 | register unsigned char *completion_string; | |
d7be4211 | 1344 | struct gcpro gcpro1, gcpro2; |
b278606c BF |
1345 | |
1346 | /* We keep calling Fbuffer_string rather than arrange for GC to | |
1347 | hold onto a pointer to one of the strings thus made. */ | |
f927c5ae JB |
1348 | |
1349 | completion = Ftry_completion (Fbuffer_string (), | |
1350 | Vminibuffer_completion_table, | |
1351 | Vminibuffer_completion_predicate); | |
56a98455 | 1352 | if (NILP (completion)) |
f927c5ae JB |
1353 | { |
1354 | bitch_at_user (); | |
1355 | temp_echo_area_glyphs (" [No match]"); | |
1356 | return Qnil; | |
1357 | } | |
1358 | if (EQ (completion, Qt)) | |
1359 | return Qnil; | |
1360 | ||
b278606c | 1361 | #if 0 /* How the below code used to look, for reference. */ |
f927c5ae JB |
1362 | tem = Fbuffer_string (); |
1363 | b = XSTRING (tem)->data; | |
1364 | i = ZV - 1 - XSTRING (completion)->size; | |
1365 | p = XSTRING (completion)->data; | |
1366 | if (i > 0 || | |
1367 | 0 <= scmp (b, p, ZV - 1)) | |
1368 | { | |
1369 | i = 1; | |
1370 | /* Set buffer to longest match of buffer tail and completion head. */ | |
1371 | while (0 <= scmp (b + i, p, ZV - 1 - i)) | |
1372 | i++; | |
1373 | del_range (1, i + 1); | |
1374 | SET_PT (ZV); | |
1375 | } | |
1376 | #else /* Rewritten code */ | |
1377 | { | |
1378 | register unsigned char *buffer_string; | |
1379 | int buffer_length, completion_length; | |
1380 | ||
1381 | tem = Fbuffer_string (); | |
d7be4211 | 1382 | GCPRO2 (completion, tem); |
719b4a40 RS |
1383 | /* If reading a file name, |
1384 | expand any $ENVVAR refs in the buffer and in TEM. */ | |
1385 | if (EQ (Vminibuffer_completion_table, Qread_file_name_internal)) | |
1386 | { | |
1387 | Lisp_Object substituted; | |
1388 | substituted = Fsubstitute_in_file_name (tem); | |
1389 | if (! EQ (substituted, tem)) | |
1390 | { | |
1391 | tem = substituted; | |
1392 | Ferase_buffer (); | |
3cab9ae4 | 1393 | insert_from_string (tem, 0, XSTRING (tem)->size, 0); |
719b4a40 RS |
1394 | } |
1395 | } | |
f927c5ae JB |
1396 | buffer_string = XSTRING (tem)->data; |
1397 | completion_string = XSTRING (completion)->data; | |
1398 | buffer_length = XSTRING (tem)->size; /* ie ZV - BEGV */ | |
1399 | completion_length = XSTRING (completion)->size; | |
1400 | i = buffer_length - completion_length; | |
1401 | /* Mly: I don't understand what this is supposed to do AT ALL */ | |
1402 | if (i > 0 || | |
1403 | 0 <= scmp (buffer_string, completion_string, buffer_length)) | |
1404 | { | |
1405 | /* Set buffer to longest match of buffer tail and completion head. */ | |
1406 | if (i <= 0) i = 1; | |
1407 | buffer_string += i; | |
1408 | buffer_length -= i; | |
1409 | while (0 <= scmp (buffer_string++, completion_string, buffer_length--)) | |
1410 | i++; | |
1411 | del_range (1, i + 1); | |
1412 | SET_PT (ZV); | |
1413 | } | |
d7be4211 | 1414 | UNGCPRO; |
f927c5ae JB |
1415 | } |
1416 | #endif /* Rewritten code */ | |
1417 | i = ZV - BEGV; | |
1418 | ||
1419 | /* If completion finds next char not unique, | |
b278606c | 1420 | consider adding a space or a hyphen. */ |
f927c5ae JB |
1421 | if (i == XSTRING (completion)->size) |
1422 | { | |
b278606c | 1423 | GCPRO1 (completion); |
f927c5ae JB |
1424 | tem = Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")), |
1425 | Vminibuffer_completion_table, | |
1426 | Vminibuffer_completion_predicate); | |
b278606c BF |
1427 | UNGCPRO; |
1428 | ||
7510b296 | 1429 | if (STRINGP (tem)) |
f927c5ae JB |
1430 | completion = tem; |
1431 | else | |
1432 | { | |
b278606c BF |
1433 | GCPRO1 (completion); |
1434 | tem = | |
1435 | Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")), | |
1436 | Vminibuffer_completion_table, | |
1437 | Vminibuffer_completion_predicate); | |
1438 | UNGCPRO; | |
1439 | ||
7510b296 | 1440 | if (STRINGP (tem)) |
f927c5ae JB |
1441 | completion = tem; |
1442 | } | |
1443 | } | |
1444 | ||
1445 | /* Now find first word-break in the stuff found by completion. | |
1446 | i gets index in string of where to stop completing. */ | |
b278606c | 1447 | |
f927c5ae JB |
1448 | completion_string = XSTRING (completion)->data; |
1449 | ||
1450 | for (; i < XSTRING (completion)->size; i++) | |
1451 | if (SYNTAX (completion_string[i]) != Sword) break; | |
1452 | if (i < XSTRING (completion)->size) | |
1453 | i = i + 1; | |
1454 | ||
1455 | /* If got no characters, print help for user. */ | |
1456 | ||
1457 | if (i == ZV - BEGV) | |
1458 | { | |
1459 | if (auto_help) | |
1460 | Fminibuffer_completion_help (); | |
1461 | return Qnil; | |
1462 | } | |
1463 | ||
1464 | /* Otherwise insert in minibuffer the chars we got */ | |
1465 | ||
1466 | Ferase_buffer (); | |
3cab9ae4 | 1467 | insert_from_string (completion, 0, i, 1); |
f927c5ae JB |
1468 | return Qt; |
1469 | } | |
1470 | \f | |
1471 | DEFUN ("display-completion-list", Fdisplay_completion_list, Sdisplay_completion_list, | |
1472 | 1, 1, 0, | |
2dc2b736 | 1473 | "Display the list of completions, COMPLETIONS, using `standard-output'.\n\ |
f927c5ae | 1474 | Each element may be just a symbol or string\n\ |
2d7e41fe RS |
1475 | or may be a list of two strings to be printed as if concatenated.\n\ |
1476 | `standard-output' must be a buffer.\n\ | |
1477 | At the end, run the normal hook `completion-setup-hook'.\n\ | |
1478 | It can find the completion buffer in `standard-output'.") | |
f927c5ae JB |
1479 | (completions) |
1480 | Lisp_Object completions; | |
1481 | { | |
dae36123 | 1482 | Lisp_Object tail, elt; |
f927c5ae | 1483 | register int i; |
2dc2b736 | 1484 | int column = 0; |
dae36123 | 1485 | struct gcpro gcpro1, gcpro2; |
2dc2b736 | 1486 | struct buffer *old = current_buffer; |
681f5af4 | 1487 | int first = 1; |
486cc7fb RS |
1488 | |
1489 | /* Note that (when it matters) every variable | |
dae36123 RS |
1490 | points to a non-string that is pointed to by COMPLETIONS, |
1491 | except for ELT. ELT can be pointing to a string | |
1492 | when terpri or Findent_to calls a change hook. */ | |
1493 | elt = Qnil; | |
1494 | GCPRO2 (completions, elt); | |
486cc7fb | 1495 | |
7510b296 | 1496 | if (BUFFERP (Vstandard_output)) |
2dc2b736 | 1497 | set_buffer_internal (XBUFFER (Vstandard_output)); |
f927c5ae | 1498 | |
56a98455 | 1499 | if (NILP (completions)) |
cfc736bf RS |
1500 | write_string ("There are no possible completions of what you have typed.", |
1501 | -1); | |
f927c5ae JB |
1502 | else |
1503 | { | |
2dc2b736 | 1504 | write_string ("Possible completions are:", -1); |
56a98455 | 1505 | for (tail = completions, i = 0; !NILP (tail); tail = Fcdr (tail), i++) |
f927c5ae | 1506 | { |
681f5af4 RS |
1507 | Lisp_Object tem; |
1508 | int length; | |
1509 | ||
1510 | elt = Fcar (tail); | |
1511 | /* Compute the length of this element. */ | |
1512 | if (CONSP (elt)) | |
1513 | { | |
1514 | tem = Fcar (elt); | |
1515 | CHECK_STRING (tem, 0); | |
1516 | length = XINT (XSTRING (tem)->size); | |
1517 | ||
1518 | tem = Fcar (Fcdr (elt)); | |
1519 | CHECK_STRING (tem, 0); | |
1520 | length += XINT (XSTRING (tem)->size); | |
1521 | } | |
1522 | else | |
1523 | { | |
1524 | CHECK_STRING (elt, 0); | |
1525 | length = XINT (XSTRING (elt)->size); | |
1526 | } | |
1527 | ||
1528 | /* This does a bad job for narrower than usual windows. | |
1529 | Sadly, the window it will appear in is not known | |
1530 | until after the text has been made. */ | |
1531 | ||
1532 | /* If the previous completion was very wide, | |
1533 | or we have two on this line already, | |
1534 | don't put another on the same line. */ | |
1535 | if (column > 33 || first | |
1536 | /* If this is really wide, don't put it second on a line. */ | |
1537 | || column > 0 && length > 45) | |
1538 | { | |
1539 | Fterpri (Qnil); | |
1540 | column = 0; | |
1541 | } | |
1542 | /* Otherwise advance to column 35. */ | |
1543 | else | |
2dc2b736 | 1544 | { |
7510b296 | 1545 | if (BUFFERP (Vstandard_output)) |
681f5af4 RS |
1546 | { |
1547 | tem = Findent_to (make_number (35), make_number (2)); | |
1548 | column = XINT (tem); | |
1549 | } | |
2dc2b736 RS |
1550 | else |
1551 | { | |
1552 | do | |
1553 | { | |
1554 | write_string (" ", -1); | |
1555 | column++; | |
1556 | } | |
1557 | while (column < 35); | |
1558 | } | |
1559 | } | |
681f5af4 RS |
1560 | |
1561 | /* Output this element and update COLUMN. */ | |
f927c5ae JB |
1562 | if (CONSP (elt)) |
1563 | { | |
1564 | Fprinc (Fcar (elt), Qnil); | |
1565 | Fprinc (Fcar (Fcdr (elt)), Qnil); | |
1566 | } | |
1567 | else | |
681f5af4 RS |
1568 | Fprinc (elt, Qnil); |
1569 | ||
1570 | column += length; | |
1571 | ||
1572 | /* If output is to a buffer, recompute COLUMN in a way | |
1573 | that takes account of character widths. */ | |
1574 | if (BUFFERP (Vstandard_output)) | |
2dc2b736 | 1575 | { |
681f5af4 RS |
1576 | tem = Fcurrent_column (); |
1577 | column = XINT (tem); | |
2dc2b736 | 1578 | } |
681f5af4 RS |
1579 | |
1580 | first = 0; | |
f927c5ae JB |
1581 | } |
1582 | } | |
2dc2b736 | 1583 | |
486cc7fb RS |
1584 | UNGCPRO; |
1585 | ||
7510b296 | 1586 | if (BUFFERP (Vstandard_output)) |
2d7e41fe RS |
1587 | set_buffer_internal (old); |
1588 | ||
cfc736bf RS |
1589 | if (!NILP (Vrun_hooks)) |
1590 | call1 (Vrun_hooks, intern ("completion-setup-hook")); | |
1591 | ||
f927c5ae JB |
1592 | return Qnil; |
1593 | } | |
1594 | ||
1595 | DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help, Sminibuffer_completion_help, | |
1596 | 0, 0, "", | |
1597 | "Display a list of possible completions of the current minibuffer contents.") | |
1598 | () | |
1599 | { | |
1600 | Lisp_Object completions; | |
1601 | ||
1602 | message ("Making completion list..."); | |
1603 | completions = Fall_completions (Fbuffer_string (), | |
1604 | Vminibuffer_completion_table, | |
89a255dc RS |
1605 | Vminibuffer_completion_predicate, |
1606 | Qt); | |
f927c5ae JB |
1607 | echo_area_glyphs = 0; |
1608 | ||
56a98455 | 1609 | if (NILP (completions)) |
f927c5ae JB |
1610 | { |
1611 | bitch_at_user (); | |
1612 | temp_echo_area_glyphs (" [No completions]"); | |
1613 | } | |
1614 | else | |
1615 | internal_with_output_to_temp_buffer ("*Completions*", | |
1616 | Fdisplay_completion_list, | |
1617 | Fsort (completions, Qstring_lessp)); | |
1618 | return Qnil; | |
1619 | } | |
1620 | \f | |
1621 | DEFUN ("self-insert-and-exit", Fself_insert_and_exit, Sself_insert_and_exit, 0, 0, "", | |
1622 | "Terminate minibuffer input.") | |
1623 | () | |
1624 | { | |
7510b296 | 1625 | if (INTEGERP (last_command_char)) |
f927c5ae JB |
1626 | internal_self_insert (last_command_char, 0); |
1627 | else | |
1628 | bitch_at_user (); | |
1629 | ||
1630 | Fthrow (Qexit, Qnil); | |
1631 | } | |
1632 | ||
1633 | DEFUN ("exit-minibuffer", Fexit_minibuffer, Sexit_minibuffer, 0, 0, "", | |
1634 | "Terminate this minibuffer argument.") | |
1635 | () | |
1636 | { | |
1637 | Fthrow (Qexit, Qnil); | |
1638 | } | |
1639 | ||
1640 | DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0, | |
1641 | "Return current depth of activations of minibuffer, a nonnegative integer.") | |
1642 | () | |
1643 | { | |
1644 | return make_number (minibuf_level); | |
1645 | } | |
1646 | ||
37e9a934 KH |
1647 | DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0, |
1648 | "Return the prompt string of the currently-active minibuffer.\n\ | |
1649 | If no minibuffer is active, return nil.") | |
1650 | () | |
1651 | { | |
4d04c1f1 | 1652 | return Fcopy_sequence (minibuf_prompt); |
37e9a934 KH |
1653 | } |
1654 | ||
1655 | DEFUN ("minibuffer-prompt-width", Fminibuffer_prompt_width, | |
1656 | Sminibuffer_prompt_width, 0, 0, 0, | |
c5896ef4 | 1657 | "Return the display width of the minibuffer prompt.") |
37e9a934 KH |
1658 | () |
1659 | { | |
1660 | Lisp_Object width; | |
5a866662 | 1661 | XSETFASTINT (width, minibuf_prompt_width); |
37e9a934 KH |
1662 | return width; |
1663 | } | |
f927c5ae JB |
1664 | \f |
1665 | init_minibuf_once () | |
1666 | { | |
1667 | Vminibuffer_list = Qnil; | |
1668 | staticpro (&Vminibuffer_list); | |
1669 | } | |
1670 | ||
1671 | syms_of_minibuf () | |
1672 | { | |
1673 | minibuf_level = 0; | |
4d04c1f1 KH |
1674 | minibuf_prompt = Qnil; |
1675 | staticpro (&minibuf_prompt); | |
1676 | ||
1677 | minibuf_save_list = Qnil; | |
1678 | staticpro (&minibuf_save_list); | |
f927c5ae | 1679 | |
719b4a40 RS |
1680 | Qread_file_name_internal = intern ("read-file-name-internal"); |
1681 | staticpro (&Qread_file_name_internal); | |
1682 | ||
f927c5ae JB |
1683 | Qminibuffer_completion_table = intern ("minibuffer-completion-table"); |
1684 | staticpro (&Qminibuffer_completion_table); | |
1685 | ||
1686 | Qminibuffer_completion_confirm = intern ("minibuffer-completion-confirm"); | |
1687 | staticpro (&Qminibuffer_completion_confirm); | |
1688 | ||
1689 | Qminibuffer_completion_predicate = intern ("minibuffer-completion-predicate"); | |
1690 | staticpro (&Qminibuffer_completion_predicate); | |
1691 | ||
1e00c2ff KH |
1692 | staticpro (&last_exact_completion); |
1693 | last_exact_completion = Qnil; | |
1694 | ||
f927c5ae JB |
1695 | staticpro (&last_minibuf_string); |
1696 | last_minibuf_string = Qnil; | |
1697 | ||
1698 | Quser_variable_p = intern ("user-variable-p"); | |
1699 | staticpro (&Quser_variable_p); | |
1700 | ||
770970cb RS |
1701 | Qminibuffer_history = intern ("minibuffer-history"); |
1702 | staticpro (&Qminibuffer_history); | |
f927c5ae | 1703 | |
5c781212 RS |
1704 | Qminibuffer_setup_hook = intern ("minibuffer-setup-hook"); |
1705 | staticpro (&Qminibuffer_setup_hook); | |
1706 | ||
177aecf9 KH |
1707 | Qminibuffer_exit_hook = intern ("minibuffer-exit-hook"); |
1708 | staticpro (&Qminibuffer_exit_hook); | |
1709 | ||
5c781212 RS |
1710 | DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook, |
1711 | "Normal hook run just after entry to minibuffer."); | |
1712 | Vminibuffer_setup_hook = Qnil; | |
1713 | ||
177aecf9 KH |
1714 | DEFVAR_LISP ("minibuffer-exit-hook", &Vminibuffer_exit_hook, |
1715 | "Normal hook run just after exit from minibuffer."); | |
1716 | Vminibuffer_exit_hook = Qnil; | |
1717 | ||
f927c5ae JB |
1718 | DEFVAR_BOOL ("completion-auto-help", &auto_help, |
1719 | "*Non-nil means automatically provide help for invalid completion input."); | |
1720 | auto_help = 1; | |
1721 | ||
1722 | DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case, | |
1723 | "Non-nil means don't consider case significant in completion."); | |
1724 | completion_ignore_case = 0; | |
1725 | ||
1726 | DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers, | |
1727 | "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\ | |
1728 | More precisely, this variable makes a difference when the minibuffer window\n\ | |
1729 | is the selected window. If you are in some other window, minibuffer commands\n\ | |
1730 | are allowed even if a minibuffer is active."); | |
1731 | enable_recursive_minibuffers = 0; | |
1732 | ||
1733 | DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table, | |
1734 | "Alist or obarray used for completion in the minibuffer.\n\ | |
1735 | This becomes the ALIST argument to `try-completion' and `all-completion'.\n\ | |
1736 | \n\ | |
1737 | The value may alternatively be a function, which is given three arguments:\n\ | |
1738 | STRING, the current buffer contents;\n\ | |
1739 | PREDICATE, the predicate for filtering possible matches;\n\ | |
1740 | CODE, which says what kind of things to do.\n\ | |
1741 | CODE can be nil, t or `lambda'.\n\ | |
1742 | nil means to return the best completion of STRING, or nil if there is none.\n\ | |
1743 | t means to return a list of all possible completions of STRING.\n\ | |
1744 | `lambda' means to return t if STRING is a valid completion as it stands."); | |
1745 | Vminibuffer_completion_table = Qnil; | |
1746 | ||
1747 | DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate, | |
1748 | "Within call to `completing-read', this holds the PREDICATE argument."); | |
1749 | Vminibuffer_completion_predicate = Qnil; | |
1750 | ||
1751 | DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm, | |
1752 | "Non-nil => demand confirmation of completion before exiting minibuffer."); | |
1753 | Vminibuffer_completion_confirm = Qnil; | |
1754 | ||
1755 | DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form, | |
1756 | "Value that `help-form' takes on inside the minibuffer."); | |
1757 | Vminibuffer_help_form = Qnil; | |
1758 | ||
770970cb RS |
1759 | DEFVAR_LISP ("minibuffer-history-variable", &Vminibuffer_history_variable, |
1760 | "History list symbol to add minibuffer values to.\n\ | |
1761 | Each minibuffer output is added with\n\ | |
1762 | (set minibuffer-history-variable\n\ | |
1763 | (cons STRING (symbol-value minibuffer-history-variable)))"); | |
5a866662 | 1764 | XSETFASTINT (Vminibuffer_history_variable, 0); |
770970cb RS |
1765 | |
1766 | DEFVAR_LISP ("minibuffer-history-position", &Vminibuffer_history_position, | |
1767 | "Current position of redoing in the history list."); | |
1768 | Vminibuffer_history_position = Qnil; | |
1769 | ||
6a9ee000 RS |
1770 | DEFVAR_BOOL ("minibuffer-auto-raise", &minibuffer_auto_raise, |
1771 | "*Non-nil means entering the minibuffer raises the minibuffer's frame."); | |
1772 | minibuffer_auto_raise = 0; | |
1773 | ||
42006772 RS |
1774 | DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list, |
1775 | "List of regexps that should restrict possible completions."); | |
1776 | Vcompletion_regexp_list = Qnil; | |
1777 | ||
f927c5ae JB |
1778 | defsubr (&Sread_from_minibuffer); |
1779 | defsubr (&Seval_minibuffer); | |
1780 | defsubr (&Sread_minibuffer); | |
1781 | defsubr (&Sread_string); | |
1782 | defsubr (&Sread_command); | |
1783 | defsubr (&Sread_variable); | |
1784 | defsubr (&Sread_buffer); | |
1785 | defsubr (&Sread_no_blanks_input); | |
1786 | defsubr (&Sminibuffer_depth); | |
37e9a934 KH |
1787 | defsubr (&Sminibuffer_prompt); |
1788 | defsubr (&Sminibuffer_prompt_width); | |
f927c5ae JB |
1789 | |
1790 | defsubr (&Stry_completion); | |
1791 | defsubr (&Sall_completions); | |
1792 | defsubr (&Scompleting_read); | |
1793 | defsubr (&Sminibuffer_complete); | |
1794 | defsubr (&Sminibuffer_complete_word); | |
1795 | defsubr (&Sminibuffer_complete_and_exit); | |
1796 | defsubr (&Sdisplay_completion_list); | |
1797 | defsubr (&Sminibuffer_completion_help); | |
1798 | ||
1799 | defsubr (&Sself_insert_and_exit); | |
1800 | defsubr (&Sexit_minibuffer); | |
1801 | ||
1802 | } | |
1803 | ||
1804 | keys_of_minibuf () | |
1805 | { | |
1806 | initial_define_key (Vminibuffer_local_map, Ctl ('g'), | |
1807 | "abort-recursive-edit"); | |
1808 | initial_define_key (Vminibuffer_local_map, Ctl ('m'), | |
1809 | "exit-minibuffer"); | |
1810 | initial_define_key (Vminibuffer_local_map, Ctl ('j'), | |
1811 | "exit-minibuffer"); | |
1812 | ||
1813 | initial_define_key (Vminibuffer_local_ns_map, Ctl ('g'), | |
1814 | "abort-recursive-edit"); | |
1815 | initial_define_key (Vminibuffer_local_ns_map, Ctl ('m'), | |
1816 | "exit-minibuffer"); | |
1817 | initial_define_key (Vminibuffer_local_ns_map, Ctl ('j'), | |
1818 | "exit-minibuffer"); | |
1819 | ||
1820 | initial_define_key (Vminibuffer_local_ns_map, ' ', | |
1821 | "exit-minibuffer"); | |
1822 | initial_define_key (Vminibuffer_local_ns_map, '\t', | |
1823 | "exit-minibuffer"); | |
1824 | initial_define_key (Vminibuffer_local_ns_map, '?', | |
1825 | "self-insert-and-exit"); | |
1826 | ||
1827 | initial_define_key (Vminibuffer_local_completion_map, Ctl ('g'), | |
1828 | "abort-recursive-edit"); | |
1829 | initial_define_key (Vminibuffer_local_completion_map, Ctl ('m'), | |
1830 | "exit-minibuffer"); | |
1831 | initial_define_key (Vminibuffer_local_completion_map, Ctl ('j'), | |
1832 | "exit-minibuffer"); | |
1833 | ||
1834 | initial_define_key (Vminibuffer_local_completion_map, '\t', | |
1835 | "minibuffer-complete"); | |
1836 | initial_define_key (Vminibuffer_local_completion_map, ' ', | |
1837 | "minibuffer-complete-word"); | |
1838 | initial_define_key (Vminibuffer_local_completion_map, '?', | |
1839 | "minibuffer-completion-help"); | |
1840 | ||
1841 | initial_define_key (Vminibuffer_local_must_match_map, Ctl ('g'), | |
1842 | "abort-recursive-edit"); | |
1843 | initial_define_key (Vminibuffer_local_must_match_map, Ctl ('m'), | |
1844 | "minibuffer-complete-and-exit"); | |
1845 | initial_define_key (Vminibuffer_local_must_match_map, Ctl ('j'), | |
1846 | "minibuffer-complete-and-exit"); | |
1847 | initial_define_key (Vminibuffer_local_must_match_map, '\t', | |
1848 | "minibuffer-complete"); | |
1849 | initial_define_key (Vminibuffer_local_must_match_map, ' ', | |
1850 | "minibuffer-complete-word"); | |
1851 | initial_define_key (Vminibuffer_local_must_match_map, '?', | |
1852 | "minibuffer-completion-help"); | |
1853 | } |