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