1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
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.
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. */
25 #include "dispextern.h"
30 #define min(a, b) ((a) < (b) ? (a) : (b))
32 /* List of buffers for use as minibuffers.
33 The first element of the list is used for the outermost minibuffer invocation,
34 the next element is used for a recursive minibuffer invocation, etc.
35 The list is extended at the end as deeped minibuffer recursions are encountered. */
36 Lisp_Object Vminibuffer_list
;
38 struct minibuf_save_data
42 Lisp_Object help_form
;
43 Lisp_Object current_prefix_arg
;
46 int minibuf_save_vector_size
;
47 struct minibuf_save_data
*minibuf_save_vector
;
49 /* Depth in minibuffer invocations. */
52 /* Nonzero means display completion help for invalid input */
55 /* Fread_minibuffer leaves the input, as a string, here */
56 Lisp_Object last_minibuf_string
;
58 /* Nonzero means let functions called when within a minibuffer
59 invoke recursive minibuffers (to read arguments, or whatever) */
60 int enable_recursive_minibuffers
;
62 /* help-form is bound to this while in the minibuffer. */
64 Lisp_Object Vminibuffer_help_form
;
66 /* Nonzero means completion ignores case. */
68 int completion_ignore_case
;
70 /* If last completion attempt reported "Complete but not unique"
71 then this is the string completed then; otherwise this is nil. */
73 static Lisp_Object last_exact_completion
;
75 Lisp_Object Quser_variable_p
;
78 /* Actual minibuffer invocation. */
80 void read_minibuf_unwind ();
81 Lisp_Object
get_minibuffer ();
82 Lisp_Object
read_minibuf ();
85 read_minibuf (map
, initial
, prompt
, backup_n
, expflag
)
92 register Lisp_Object val
;
93 int count
= specpdl_ptr
- specpdl
;
94 Lisp_Object mini_frame
= WINDOW_FRAME (XWINDOW (minibuf_window
));
95 struct gcpro gcpro1
, gcpro2
;
97 if (XTYPE (prompt
) != Lisp_String
)
98 prompt
= build_string ("");
100 /* Emacs in -batch mode calls minibuffer: print the prompt. */
101 if (noninteractive
&& XTYPE (prompt
) == Lisp_String
)
102 printf ("%s", XSTRING (prompt
)->data
);
104 if (!enable_recursive_minibuffers
106 && (EQ (selected_window
, minibuf_window
)))
108 || selected_frame
!= XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window
)))
110 error ("Command attempted to use minibuffer while in minibuffer");
112 if (minibuf_level
== minibuf_save_vector_size
)
113 minibuf_save_vector
=
114 (struct minibuf_save_data
*)
115 xrealloc (minibuf_save_vector
,
116 (minibuf_save_vector_size
*= 2)
117 * sizeof (struct minibuf_save_data
));
118 minibuf_save_vector
[minibuf_level
].prompt
= minibuf_prompt
;
119 minibuf_save_vector
[minibuf_level
].prompt_width
= minibuf_prompt_width
;
120 minibuf_prompt_width
= 0;
121 /* >> Why is this done this way rather than binding these variables? */
122 minibuf_save_vector
[minibuf_level
].help_form
= Vhelp_form
;
123 minibuf_save_vector
[minibuf_level
].current_prefix_arg
= Vcurrent_prefix_arg
;
124 GCPRO2 (minibuf_save_vector
[minibuf_level
].help_form
,
125 minibuf_save_vector
[minibuf_level
].current_prefix_arg
);
127 record_unwind_protect (Fset_window_configuration
,
128 Fcurrent_window_configuration (Qnil
));
130 /* If the minibuffer window is on a different frame, save that
131 frame's configuration too. */
132 if (XFRAME (mini_frame
) != selected_frame
)
134 record_unwind_protect (Fset_window_configuration
,
135 Fcurrent_window_configuration (mini_frame
));
138 val
= current_buffer
->directory
;
139 Fset_buffer (get_minibuffer (minibuf_level
));
140 current_buffer
->directory
= val
;
141 Fmake_local_variable (Qprint_escape_newlines
);
142 print_escape_newlines
= 1;
145 /* If the minibuffer window is on another frame, shift this frame's
146 focus to that window, and arrange to put it back later. */
147 if (XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window
)))
150 record_unwind_protect (read_minibuf_unwind
,
151 Fcons (Fselected_frame (),
152 FRAME_FOCUS_FRAME (selected_frame
)));
154 Fredirect_frame_focus (Fselected_frame (), mini_frame
);
157 record_unwind_protect (read_minibuf_unwind
, Qnil
);
159 record_unwind_protect (read_minibuf_unwind
, Qnil
);
162 Vminibuf_scroll_window
= selected_window
;
163 Fset_window_buffer (minibuf_window
, Fcurrent_buffer ());
164 Fselect_window (minibuf_window
);
165 XFASTINT (XWINDOW (minibuf_window
)->hscroll
) = 0;
172 Finsert (1, &initial
);
173 if (!NILP (backup_n
) && XTYPE (backup_n
) == Lisp_Int
)
174 Fforward_char (backup_n
);
177 minibuf_prompt
= (char *) alloca (XSTRING (prompt
)->size
+ 1);
178 bcopy (XSTRING (prompt
)->data
, minibuf_prompt
, XSTRING (prompt
)->size
+ 1);
179 echo_area_glyphs
= 0;
181 Vhelp_form
= Vminibuffer_help_form
;
182 current_buffer
->keymap
= map
;
184 /* ??? MCC did redraw_screen here if switching screens. */
187 /* If cursor is on the minibuffer line,
188 show the user we have exited by putting it in column 0. */
189 if ((FRAME_CURSOR_Y (selected_frame
)
190 >= XFASTINT (XWINDOW (minibuf_window
)->top
))
193 FRAME_CURSOR_X (selected_frame
) = 0;
194 update_frame (selected_frame
, 1, 1);
197 /* Make minibuffer contents into a string */
198 val
= make_buffer_string (1, Z
);
199 bcopy (GAP_END_ADDR
, XSTRING (val
)->data
+ GPT
- BEG
, Z
- GPT
);
200 unbind_to (count
, Qnil
); /* The appropriate frame will get selected
201 in set-window-configuration. */
205 /* VAL is the string of minibuffer text. */
207 last_minibuf_string
= val
;
209 /* If Lisp form desired instead of string, parse it */
216 /* Return a buffer to be used as the minibuffer at depth `depth'.
217 depth = 0 is the lowest allowed argument, and that is the value
218 used for nonrecursive minibuffer invocations */
221 get_minibuffer (depth
)
224 Lisp_Object tail
, num
, buf
;
226 extern Lisp_Object
nconc2 ();
228 XFASTINT (num
) = depth
;
229 tail
= Fnthcdr (num
, Vminibuffer_list
);
232 tail
= Fcons (Qnil
, Qnil
);
233 Vminibuffer_list
= nconc2 (Vminibuffer_list
, tail
);
236 if (NILP (buf
) || NILP (XBUFFER (buf
)->name
))
238 sprintf (name
, " *Minibuf-%d*", depth
);
239 buf
= Fget_buffer_create (build_string (name
));
240 XCONS (tail
)->car
= buf
;
243 reset_buffer (XBUFFER (buf
));
247 /* This function is called on exiting minibuffer, whether normally or not,
248 and it restores the current window, buffer, etc. */
251 read_minibuf_unwind (data
)
254 /* Erase the minibuffer we were using at this level. */
255 Fset_buffer (XWINDOW (minibuf_window
)->buffer
);
257 /* Prevent error in erase-buffer. */
258 current_buffer
->read_only
= Qnil
;
261 /* If this was a recursive minibuffer,
262 tie the minibuffer window back to the outer level minibuffer buffer */
264 /* Make sure minibuffer window is erased, not ignored */
265 windows_or_buffers_changed
++;
266 XFASTINT (XWINDOW (minibuf_window
)->last_modified
) = 0;
268 /* Restore prompt from outer minibuffer */
269 minibuf_prompt
= minibuf_save_vector
[minibuf_level
].prompt
;
270 minibuf_prompt_width
= minibuf_save_vector
[minibuf_level
].prompt_width
;
271 Vhelp_form
= minibuf_save_vector
[minibuf_level
].help_form
;
272 Vcurrent_prefix_arg
= minibuf_save_vector
[minibuf_level
].current_prefix_arg
;
275 /* Redirect the focus of the frame that called the minibuffer. */
277 Fredirect_frame_focus (XCONS (data
)->car
, XCONS (data
)->cdr
);
281 DEFUN ("read-from-minibuffer", Fread_from_minibuffer
, Sread_from_minibuffer
, 1, 5, 0,
282 "Read a string from the minibuffer, prompting with string PROMPT.\n\
283 If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
284 to be inserted into the minibuffer before reading input.\n\
285 Third arg KEYMAP is a keymap to use whilst reading;\n\
286 if omitted or nil, the default is `minibuffer-local-map'.\n\
287 If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
288 and return that object:\n\
289 in other words, do `(car (read-from-string INPUT-STRING))'\n\
290 Fifth arg POSITION, if non-nil, is where to put point\n\
291 in the minibuffer after inserting INITIAL-CONTENTS.")
292 (prompt
, initial_input
, keymap
, read
, position
)
293 Lisp_Object prompt
, initial_input
, keymap
, read
, position
;
297 CHECK_STRING (prompt
, 0);
298 if (!NILP (initial_input
))
300 CHECK_STRING (initial_input
, 1);
301 if (!NILP (position
))
303 CHECK_NUMBER (position
, 0);
304 /* Convert to distance from end of input. */
305 pos
= XINT (position
) - 1 - XSTRING (initial_input
)->size
;
310 keymap
= Vminibuffer_local_map
;
312 keymap
= get_keymap (keymap
,2);
313 return read_minibuf (keymap
, initial_input
, prompt
,
317 DEFUN ("read-minibuffer", Fread_minibuffer
, Sread_minibuffer
, 1, 2, 0,
318 "Return a Lisp object read using the minibuffer.\n\
319 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
320 is a string to insert in the minibuffer before reading.")
321 (prompt
, initial_contents
)
322 Lisp_Object prompt
, initial_contents
;
324 CHECK_STRING (prompt
, 0);
325 if (!NILP (initial_contents
))
326 CHECK_STRING (initial_contents
, 1)
327 return read_minibuf (Vminibuffer_local_map
, initial_contents
, prompt
, Qnil
, 1);
330 DEFUN ("eval-minibuffer", Feval_minibuffer
, Seval_minibuffer
, 1, 2, 0,
331 "Return value of Lisp expression read using the minibuffer.\n\
332 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
333 is a string to insert in the minibuffer before reading.")
334 (prompt
, initial_contents
)
335 Lisp_Object prompt
, initial_contents
;
337 return Feval (Fread_minibuffer (prompt
, initial_contents
));
340 /* Functions that use the minibuffer to read various things. */
342 DEFUN ("read-string", Fread_string
, Sread_string
, 1, 2, 0,
343 "Read a string from the minibuffer, prompting with string PROMPT.\n\
344 If non-nil second arg INITIAL-INPUT is a string to insert before reading.")
345 (prompt
, initial_input
)
346 Lisp_Object prompt
, initial_input
;
348 return Fread_from_minibuffer (prompt
, initial_input
, Qnil
, Qnil
, Qnil
);
351 DEFUN ("read-no-blanks-input", Fread_no_blanks_input
, Sread_no_blanks_input
, 2, 2, 0,
352 "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\
353 Prompt with PROMPT, and provide INIT as an initial value of the input string.")
355 Lisp_Object prompt
, init
;
357 CHECK_STRING (prompt
, 0);
359 CHECK_STRING (init
, 1);
361 return read_minibuf (Vminibuffer_local_ns_map
, init
, prompt
, Qnil
, 0);
364 DEFUN ("read-command", Fread_command
, Sread_command
, 1, 1, 0,
365 "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\
366 Prompts with PROMPT.")
370 return Fintern (Fcompleting_read (prompt
, Vobarray
, Qcommandp
, Qt
, Qnil
, Qnil
),
375 DEFUN ("read-function", Fread_function
, Sread_function
, 1, 1, 0,
376 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
377 Prompts with PROMPT.")
381 return Fintern (Fcompleting_read (prompt
, Vobarray
, Qfboundp
, Qt
, Qnil
, Qnil
),
386 DEFUN ("read-variable", Fread_variable
, Sread_variable
, 1, 1, 0,
387 "One arg PROMPT, a string. Read the name of a user variable and return\n\
388 it as a symbol. Prompts with PROMPT.\n\
389 A user variable is one whose documentation starts with a `*' character.")
393 return Fintern (Fcompleting_read (prompt
, Vobarray
,
394 Quser_variable_p
, Qt
, Qnil
, Qnil
),
398 DEFUN ("read-buffer", Fread_buffer
, Sread_buffer
, 1, 3, 0,
399 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
400 Prompts with PROMPT.\n\
401 Optional second arg is value to return if user enters an empty line.\n\
402 If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
403 (prompt
, def
, require_match
)
404 Lisp_Object prompt
, def
, require_match
;
410 if (XTYPE (def
) == Lisp_Buffer
)
411 def
= XBUFFER (def
)->name
;
414 args
[0] = build_string ("%s(default %s) ");
417 prompt
= Fformat (3, args
);
420 tem
= Fcompleting_read (prompt
, Vbuffer_alist
, Qnil
, require_match
, Qnil
, Qnil
);
422 if (XSTRING (tem
)->size
)
427 DEFUN ("try-completion", Ftry_completion
, Stry_completion
, 2, 3, 0,
428 "Return common substring of all completions of STRING in ALIST.\n\
429 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
430 All that match are compared together; the longest initial sequence\n\
431 common to all matches is returned as a string.\n\
432 If there is no match at all, nil is returned.\n\
433 For an exact match, t is returned.\n\
435 ALIST can be an obarray instead of an alist.\n\
436 Then the print names of all symbols in the obarray are the possible matches.\n\
438 ALIST can also be a function to do the completion itself.\n\
439 It receives three arguments: the values STRING, PREDICATE and nil.\n\
440 Whatever it returns becomes the value of `try-completion'.\n\
442 If optional third argument PREDICATE is non-nil,\n\
443 it is used to test each possible match.\n\
444 The match is a candidate only if PREDICATE returns non-nil.\n\
445 The argument given to PREDICATE is the alist element or the symbol from the obarray.")
446 (string
, alist
, pred
)
447 Lisp_Object string
, alist
, pred
;
449 Lisp_Object bestmatch
, tail
, elt
, eltstring
;
451 int compare
, matchsize
;
452 int list
= CONSP (alist
) || NILP (alist
);
455 Lisp_Object bucket
, zero
, end
, tem
;
456 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
458 CHECK_STRING (string
, 0);
459 if (!list
&& XTYPE (alist
) != Lisp_Vector
)
460 return call3 (alist
, string
, pred
, Qnil
);
464 /* If ALIST is not a list, set TAIL just for gc pro. */
469 obsize
= XVECTOR (alist
)->size
;
470 bucket
= XVECTOR (alist
)->contents
[index
];
475 /* Get the next element of the alist or obarray. */
476 /* Exit the loop if the elements are all used up. */
477 /* elt gets the alist element or symbol.
478 eltstring gets the name to check as a completion. */
485 eltstring
= Fcar (elt
);
490 if (XFASTINT (bucket
) != 0)
493 eltstring
= Fsymbol_name (elt
);
494 if (XSYMBOL (bucket
)->next
)
495 XSETSYMBOL (bucket
, XSYMBOL (bucket
)->next
);
497 XFASTINT (bucket
) = 0;
499 else if (++index
>= obsize
)
503 bucket
= XVECTOR (alist
)->contents
[index
];
508 /* Is this element a possible completion? */
510 if (XTYPE (eltstring
) == Lisp_String
&&
511 XSTRING (string
)->size
<= XSTRING (eltstring
)->size
&&
512 0 > scmp (XSTRING (eltstring
)->data
, XSTRING (string
)->data
,
513 XSTRING (string
)->size
))
516 /* Ignore this element if there is a predicate
517 and the predicate doesn't like it. */
521 if (EQ (pred
, Qcommandp
))
522 tem
= Fcommandp (elt
);
525 GCPRO4 (tail
, string
, eltstring
, bestmatch
);
526 tem
= call1 (pred
, elt
);
529 if (NILP (tem
)) continue;
532 /* Update computation of how much all possible completions match */
535 if (NILP (bestmatch
))
536 bestmatch
= eltstring
, bestmatchsize
= XSTRING (eltstring
)->size
;
539 compare
= min (bestmatchsize
, XSTRING (eltstring
)->size
);
540 matchsize
= scmp (XSTRING (bestmatch
)->data
,
541 XSTRING (eltstring
)->data
,
545 if (completion_ignore_case
)
547 /* If this is an exact match except for case,
548 use it as the best match rather than one that is not an
549 exact match. This way, we get the case pattern
550 of the actual match. */
551 if ((matchsize
== XSTRING (eltstring
)->size
552 && matchsize
< XSTRING (bestmatch
)->size
)
554 /* If there is more than one exact match ignoring case,
555 and one of them is exact including case,
557 /* If there is no exact match ignoring case,
558 prefer a match that does not change the case
560 ((matchsize
== XSTRING (eltstring
)->size
)
562 (matchsize
== XSTRING (bestmatch
)->size
)
563 && !bcmp (XSTRING (eltstring
)->data
,
564 XSTRING (string
)->data
, XSTRING (string
)->size
)
565 && bcmp (XSTRING (bestmatch
)->data
,
566 XSTRING (string
)->data
, XSTRING (string
)->size
)))
567 bestmatch
= eltstring
;
569 bestmatchsize
= matchsize
;
574 if (NILP (bestmatch
))
575 return Qnil
; /* No completions found */
576 /* If we are ignoring case, and there is no exact match,
577 and no additional text was supplied,
578 don't change the case of what the user typed. */
579 if (completion_ignore_case
&& bestmatchsize
== XSTRING (string
)->size
580 && XSTRING (bestmatch
)->size
> bestmatchsize
)
583 /* Return t if the supplied string is an exact match (counting case);
584 it does not require any change to be made. */
585 if (matchcount
== 1 && bestmatchsize
== XSTRING (string
)->size
586 && !bcmp (XSTRING (bestmatch
)->data
, XSTRING (string
)->data
,
590 XFASTINT (zero
) = 0; /* Else extract the part in which */
591 XFASTINT (end
) = bestmatchsize
; /* all completions agree */
592 return Fsubstring (bestmatch
, zero
, end
);
595 /* Compare exactly LEN chars of strings at S1 and S2,
596 ignoring case if appropriate.
597 Return -1 if strings match,
598 else number of chars that match at the beginning. */
601 register char *s1
, *s2
;
604 register int l
= len
;
606 if (completion_ignore_case
)
608 while (l
&& DOWNCASE (*s1
++) == DOWNCASE (*s2
++))
613 while (l
&& *s1
++ == *s2
++)
621 DEFUN ("all-completions", Fall_completions
, Sall_completions
, 2, 3, 0,
622 "Search for partial matches to STRING in ALIST.\n\
623 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
624 The value is a list of all the strings from ALIST that match.\n\
625 ALIST can be an obarray instead of an alist.\n\
626 Then the print names of all symbols in the obarray are the possible matches.\n\
628 ALIST can also be a function to do the completion itself.\n\
629 It receives three arguments: the values STRING, PREDICATE and t.\n\
630 Whatever it returns becomes the value of `all-completion'.\n\
632 If optional third argument PREDICATE is non-nil,\n\
633 it is used to test each possible match.\n\
634 The match is a candidate only if PREDICATE returns non-nil.\n\
635 The argument given to PREDICATE is the alist element or the symbol from the obarray.")
636 (string
, alist
, pred
)
637 Lisp_Object string
, alist
, pred
;
639 Lisp_Object tail
, elt
, eltstring
;
640 Lisp_Object allmatches
;
641 int list
= CONSP (alist
) || NILP (alist
);
643 Lisp_Object bucket
, tem
;
644 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
646 CHECK_STRING (string
, 0);
647 if (!list
&& XTYPE (alist
) != Lisp_Vector
)
649 return call3 (alist
, string
, pred
, Qt
);
653 /* If ALIST is not a list, set TAIL just for gc pro. */
658 obsize
= XVECTOR (alist
)->size
;
659 bucket
= XVECTOR (alist
)->contents
[index
];
664 /* Get the next element of the alist or obarray. */
665 /* Exit the loop if the elements are all used up. */
666 /* elt gets the alist element or symbol.
667 eltstring gets the name to check as a completion. */
674 eltstring
= Fcar (elt
);
679 if (XFASTINT (bucket
) != 0)
682 eltstring
= Fsymbol_name (elt
);
683 if (XSYMBOL (bucket
)->next
)
684 XSETSYMBOL (bucket
, XSYMBOL (bucket
)->next
);
686 XFASTINT (bucket
) = 0;
688 else if (++index
>= obsize
)
692 bucket
= XVECTOR (alist
)->contents
[index
];
697 /* Is this element a possible completion? */
699 if (XTYPE (eltstring
) == Lisp_String
&&
700 XSTRING (string
)->size
<= XSTRING (eltstring
)->size
&&
701 XSTRING (eltstring
)->data
[0] != ' ' &&
702 0 > scmp (XSTRING (eltstring
)->data
, XSTRING (string
)->data
,
703 XSTRING (string
)->size
))
706 /* Ignore this element if there is a predicate
707 and the predicate doesn't like it. */
711 if (EQ (pred
, Qcommandp
))
712 tem
= Fcommandp (elt
);
715 GCPRO4 (tail
, eltstring
, allmatches
, string
);
716 tem
= call1 (pred
, elt
);
719 if (NILP (tem
)) continue;
721 /* Ok => put it on the list. */
722 allmatches
= Fcons (eltstring
, allmatches
);
726 return Fnreverse (allmatches
);
729 Lisp_Object Vminibuffer_completion_table
, Qminibuffer_completion_table
;
730 Lisp_Object Vminibuffer_completion_predicate
, Qminibuffer_completion_predicate
;
731 Lisp_Object Vminibuffer_completion_confirm
, Qminibuffer_completion_confirm
;
733 DEFUN ("completing-read", Fcompleting_read
, Scompleting_read
, 2, 6, 0,
734 "Read a string in the minibuffer, with completion.\n\
735 Args: PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, BACKUP-N.\n\
736 PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
737 TABLE is an alist whose elements' cars are strings, or an obarray.\n\
738 PREDICATE limits completion to a subset of TABLE.\n\
739 See `try-completion' for more details on completion, TABLE, and PREDICATE.\n\
740 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
741 the input is (or completes to) an element of TABLE.\n\
742 If it is also not t, Return does not exit if it does non-null completion.\n\
743 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
744 Case is ignored if ambient value of `completion-ignore-case' is non-nil.\n\
745 If BACKUP-N is specified, point should be placed that many spaces from\n\
746 the end of the buffer. This is useful when providing default values,\n\
747 because you can put point before the last component of a filename or any\n\
748 other component that is likely to be deleted.")
749 (prompt
, table
, pred
, require_match
, init
, backup_n
)
750 Lisp_Object prompt
, table
, pred
, require_match
, init
, backup_n
;
753 int count
= specpdl_ptr
- specpdl
;
754 specbind (Qminibuffer_completion_table
, table
);
755 specbind (Qminibuffer_completion_predicate
, pred
);
756 specbind (Qminibuffer_completion_confirm
,
757 EQ (require_match
, Qt
) ? Qnil
: Qt
);
758 last_exact_completion
= Qnil
;
759 val
= read_minibuf (NILP (require_match
)
760 ? Vminibuffer_local_completion_map
761 : Vminibuffer_local_must_match_map
,
762 init
, prompt
, backup_n
, 0);
763 return unbind_to (count
, val
);
766 /* Temporarily display the string M at the end of the current
767 minibuffer contents. This is used to display things like
768 "[No Match]" when the user requests a completion for a prefix
769 that has no possible completions, and other quick, unobtrusive
772 temp_echo_area_glyphs (m
)
775 /* It's not very modular to do things this way, but then it seems
776 to me that the whole echo_area_glyphs thing is a hack anyway. */
777 extern char *previous_echo_glyphs
;
780 Lisp_Object oinhibit
;
781 oinhibit
= Vinhibit_quit
;
783 /* Clear out any old echo-area message to make way for our new
785 echo_area_glyphs
= previous_echo_glyphs
= 0;
791 Fsit_for (make_number (2), Qnil
, Qnil
);
792 del_range (point
, ZV
);
793 if (!NILP (Vquit_flag
))
796 unread_command_char
= Ctl ('g');
798 Vinhibit_quit
= oinhibit
;
801 Lisp_Object
Fminibuffer_completion_help ();
802 Lisp_Object
assoc_for_completion ();
805 * 0 no possible completion
806 * 1 was already an exact and unique completion
807 * 3 was already an exact completion
808 * 4 completed to an exact completion
809 * 5 some completion happened
810 * 6 no completion happened
815 Lisp_Object completion
, tem
;
819 completion
= Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table
,
820 Vminibuffer_completion_predicate
);
821 last
= last_exact_completion
;
822 last_exact_completion
= Qnil
;
824 if (NILP (completion
))
827 temp_echo_area_glyphs (" [No match]");
831 if (EQ (completion
, Qt
)) /* exact and unique match */
835 tem
= Fstring_equal (completion
, Fbuffer_string());
836 if (completedp
= NILP (tem
))
838 Ferase_buffer (); /* Some completion happened */
839 Finsert (1, &completion
);
842 /* It did find a match. Do we match some possibility exactly now? */
843 if (CONSP (Vminibuffer_completion_table
)
844 || NILP (Vminibuffer_completion_table
))
845 tem
= assoc_for_completion (Fbuffer_string (),
846 Vminibuffer_completion_table
);
847 else if (XTYPE (Vminibuffer_completion_table
) == Lisp_Vector
)
849 /* the primitive used by Fintern_soft */
850 extern Lisp_Object
oblookup ();
852 tem
= Fbuffer_string ();
853 /* Bypass intern-soft as that loses for nil */
854 tem
= oblookup (Vminibuffer_completion_table
,
855 XSTRING (tem
)->data
, XSTRING (tem
)->size
);
856 if (XTYPE (tem
) != Lisp_Symbol
)
858 else if (!NILP (Vminibuffer_completion_predicate
))
859 tem
= call1 (Vminibuffer_completion_predicate
, tem
);
864 tem
= call3 (Vminibuffer_completion_table
,
866 Vminibuffer_completion_predicate
,
870 { /* not an exact match */
874 Fminibuffer_completion_help ();
876 temp_echo_area_glyphs (" [Next char not unique]");
881 /* If the last exact completion and this one were the same,
882 it means we've already given a "Complete but not unique"
883 message and the user's hit TAB again, so now we give him help. */
884 last_exact_completion
= completion
;
887 tem
= Fbuffer_string ();
888 if (!NILP (Fequal (tem
, last
)))
889 Fminibuffer_completion_help ();
894 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
897 assoc_for_completion (key
, list
)
898 register Lisp_Object key
;
901 register Lisp_Object tail
;
903 if (completion_ignore_case
)
906 for (tail
= list
; !NILP (tail
); tail
= Fcdr (tail
))
908 register Lisp_Object elt
, tem
, thiscar
;
910 if (!CONSP (elt
)) continue;
911 thiscar
= Fcar (elt
);
912 if (XTYPE (thiscar
) != Lisp_String
)
914 if (completion_ignore_case
)
915 thiscar
= Fupcase (thiscar
);
916 tem
= Fequal (thiscar
, key
);
917 if (!NILP (tem
)) return elt
;
923 DEFUN ("minibuffer-complete", Fminibuffer_complete
, Sminibuffer_complete
, 0, 0, "",
924 "Complete the minibuffer contents as far as possible.")
927 register int i
= do_completion ();
934 temp_echo_area_glyphs (" [Sole completion]");
938 temp_echo_area_glyphs (" [Complete, but not unique]");
945 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit
,
946 Sminibuffer_complete_and_exit
, 0, 0, "",
947 "Complete the minibuffer contents, and maybe exit.\n\
948 Exit if the name is valid with no completion needed.\n\
949 If name was completed to a valid match,\n\
950 a repetition of this command will exit.")
955 /* Allow user to specify null string */
959 i
= do_completion ();
967 if (!NILP (Vminibuffer_completion_confirm
))
969 temp_echo_area_glyphs (" [Confirm]");
979 Fthrow (Qexit
, Qnil
);
983 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word
, Sminibuffer_complete_word
,
985 "Complete the minibuffer contents at most a single word.\n\
986 After one word is completed as much as possible, a space or hyphen\n\
987 is added, provided that matches some possible completion.")
990 Lisp_Object completion
, tem
;
992 register unsigned char *completion_string
;
993 /* We keep calling Fbuffer_string
994 rather than arrange for GC to hold onto a pointer to
995 one of the strings thus made. */
997 completion
= Ftry_completion (Fbuffer_string (),
998 Vminibuffer_completion_table
,
999 Vminibuffer_completion_predicate
);
1000 if (NILP (completion
))
1003 temp_echo_area_glyphs (" [No match]");
1006 if (EQ (completion
, Qt
))
1009 #if 0 /* How the below code used to look, for reference */
1010 tem
= Fbuffer_string ();
1011 b
= XSTRING (tem
)->data
;
1012 i
= ZV
- 1 - XSTRING (completion
)->size
;
1013 p
= XSTRING (completion
)->data
;
1015 0 <= scmp (b
, p
, ZV
- 1))
1018 /* Set buffer to longest match of buffer tail and completion head. */
1019 while (0 <= scmp (b
+ i
, p
, ZV
- 1 - i
))
1021 del_range (1, i
+ 1);
1024 #else /* Rewritten code */
1026 register unsigned char *buffer_string
;
1027 int buffer_length
, completion_length
;
1029 tem
= Fbuffer_string ();
1030 buffer_string
= XSTRING (tem
)->data
;
1031 completion_string
= XSTRING (completion
)->data
;
1032 buffer_length
= XSTRING (tem
)->size
; /* ie ZV - BEGV */
1033 completion_length
= XSTRING (completion
)->size
;
1034 i
= buffer_length
- completion_length
;
1035 /* Mly: I don't understand what this is supposed to do AT ALL */
1037 0 <= scmp (buffer_string
, completion_string
, buffer_length
))
1039 /* Set buffer to longest match of buffer tail and completion head. */
1043 while (0 <= scmp (buffer_string
++, completion_string
, buffer_length
--))
1045 del_range (1, i
+ 1);
1049 #endif /* Rewritten code */
1052 /* If completion finds next char not unique,
1053 consider adding a space or a hyphen */
1054 if (i
== XSTRING (completion
)->size
)
1056 tem
= Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")),
1057 Vminibuffer_completion_table
,
1058 Vminibuffer_completion_predicate
);
1059 if (XTYPE (tem
) == Lisp_String
)
1063 tem
= Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")),
1064 Vminibuffer_completion_table
,
1065 Vminibuffer_completion_predicate
);
1066 if (XTYPE (tem
) == Lisp_String
)
1071 /* Now find first word-break in the stuff found by completion.
1072 i gets index in string of where to stop completing. */
1073 completion_string
= XSTRING (completion
)->data
;
1075 for (; i
< XSTRING (completion
)->size
; i
++)
1076 if (SYNTAX (completion_string
[i
]) != Sword
) break;
1077 if (i
< XSTRING (completion
)->size
)
1080 /* If got no characters, print help for user. */
1085 Fminibuffer_completion_help ();
1089 /* Otherwise insert in minibuffer the chars we got */
1092 insert_from_string (completion
, 0, i
);
1096 DEFUN ("display-completion-list", Fdisplay_completion_list
, Sdisplay_completion_list
,
1098 "Display the list of completions, COMPLETIONS, using `standard-output'.\n\
1099 Each element may be just a symbol or string\n\
1100 or may be a list of two strings to be printed as if concatenated.")
1102 Lisp_Object completions
;
1104 register Lisp_Object tail
, elt
;
1107 /* No GCPRO needed, since (when it matters) every variable
1108 points to a non-string that is pointed to by COMPLETIONS. */
1109 struct buffer
*old
= current_buffer
;
1110 if (XTYPE (Vstandard_output
) == Lisp_Buffer
)
1111 set_buffer_internal (XBUFFER (Vstandard_output
));
1113 if (NILP (completions
))
1114 write_string ("There are no possible completions of what you have typed.", -1);
1117 write_string ("Possible completions are:", -1);
1118 for (tail
= completions
, i
= 0; !NILP (tail
); tail
= Fcdr (tail
), i
++)
1120 /* this needs fixing for the case of long completions
1121 and/or narrow windows */
1122 /* Sadly, the window it will appear in is not known
1123 until after the text has been made. */
1126 if (XTYPE (Vstandard_output
) == Lisp_Buffer
)
1127 Findent_to (make_number (35), make_number (1));
1132 write_string (" ", -1);
1135 while (column
< 35);
1146 if (XTYPE (Vstandard_output
) != Lisp_Buffer
)
1149 tem
= Flength (Fcar (elt
));
1150 column
+= XINT (tem
);
1151 tem
= Flength (Fcar (Fcdr (elt
)));
1152 column
+= XINT (tem
);
1154 Fprinc (Fcar (elt
), Qnil
);
1155 Fprinc (Fcar (Fcdr (elt
)), Qnil
);
1159 if (XTYPE (Vstandard_output
) != Lisp_Buffer
)
1162 tem
= Flength (elt
, Qt
);
1163 column
+= XINT (tem
);
1170 if (XTYPE (Vstandard_output
) == Lisp_Buffer
)
1171 set_buffer_internal (old
);
1175 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help
, Sminibuffer_completion_help
,
1177 "Display a list of possible completions of the current minibuffer contents.")
1180 Lisp_Object completions
;
1182 message ("Making completion list...");
1183 completions
= Fall_completions (Fbuffer_string (),
1184 Vminibuffer_completion_table
,
1185 Vminibuffer_completion_predicate
);
1186 echo_area_glyphs
= 0;
1188 if (NILP (completions
))
1191 temp_echo_area_glyphs (" [No completions]");
1194 internal_with_output_to_temp_buffer ("*Completions*",
1195 Fdisplay_completion_list
,
1196 Fsort (completions
, Qstring_lessp
));
1200 DEFUN ("self-insert-and-exit", Fself_insert_and_exit
, Sself_insert_and_exit
, 0, 0, "",
1201 "Terminate minibuffer input.")
1204 if (XTYPE (last_command_char
) == Lisp_Int
)
1205 internal_self_insert (last_command_char
, 0);
1209 Fthrow (Qexit
, Qnil
);
1212 DEFUN ("exit-minibuffer", Fexit_minibuffer
, Sexit_minibuffer
, 0, 0, "",
1213 "Terminate this minibuffer argument.")
1216 Fthrow (Qexit
, Qnil
);
1219 DEFUN ("minibuffer-depth", Fminibuffer_depth
, Sminibuffer_depth
, 0, 0, 0,
1220 "Return current depth of activations of minibuffer, a nonnegative integer.")
1223 return make_number (minibuf_level
);
1227 init_minibuf_once ()
1229 Vminibuffer_list
= Qnil
;
1230 staticpro (&Vminibuffer_list
);
1237 minibuf_save_vector_size
= 5;
1238 minibuf_save_vector
= (struct minibuf_save_data
*) malloc (5 * sizeof (struct minibuf_save_data
));
1240 Qminibuffer_completion_table
= intern ("minibuffer-completion-table");
1241 staticpro (&Qminibuffer_completion_table
);
1243 Qminibuffer_completion_confirm
= intern ("minibuffer-completion-confirm");
1244 staticpro (&Qminibuffer_completion_confirm
);
1246 Qminibuffer_completion_predicate
= intern ("minibuffer-completion-predicate");
1247 staticpro (&Qminibuffer_completion_predicate
);
1249 staticpro (&last_minibuf_string
);
1250 last_minibuf_string
= Qnil
;
1252 Quser_variable_p
= intern ("user-variable-p");
1253 staticpro (&Quser_variable_p
);
1257 DEFVAR_BOOL ("completion-auto-help", &auto_help
,
1258 "*Non-nil means automatically provide help for invalid completion input.");
1261 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case
,
1262 "Non-nil means don't consider case significant in completion.");
1263 completion_ignore_case
= 0;
1265 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers
,
1266 "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\
1267 More precisely, this variable makes a difference when the minibuffer window\n\
1268 is the selected window. If you are in some other window, minibuffer commands\n\
1269 are allowed even if a minibuffer is active.");
1270 enable_recursive_minibuffers
= 0;
1272 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table
,
1273 "Alist or obarray used for completion in the minibuffer.\n\
1274 This becomes the ALIST argument to `try-completion' and `all-completion'.\n\
1276 The value may alternatively be a function, which is given three arguments:\n\
1277 STRING, the current buffer contents;\n\
1278 PREDICATE, the predicate for filtering possible matches;\n\
1279 CODE, which says what kind of things to do.\n\
1280 CODE can be nil, t or `lambda'.\n\
1281 nil means to return the best completion of STRING, or nil if there is none.\n\
1282 t means to return a list of all possible completions of STRING.\n\
1283 `lambda' means to return t if STRING is a valid completion as it stands.");
1284 Vminibuffer_completion_table
= Qnil
;
1286 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate
,
1287 "Within call to `completing-read', this holds the PREDICATE argument.");
1288 Vminibuffer_completion_predicate
= Qnil
;
1290 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm
,
1291 "Non-nil => demand confirmation of completion before exiting minibuffer.");
1292 Vminibuffer_completion_confirm
= Qnil
;
1294 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form
,
1295 "Value that `help-form' takes on inside the minibuffer.");
1296 Vminibuffer_help_form
= Qnil
;
1298 defsubr (&Sread_from_minibuffer
);
1299 defsubr (&Seval_minibuffer
);
1300 defsubr (&Sread_minibuffer
);
1301 defsubr (&Sread_string
);
1302 defsubr (&Sread_command
);
1303 defsubr (&Sread_variable
);
1304 defsubr (&Sread_buffer
);
1305 defsubr (&Sread_no_blanks_input
);
1306 defsubr (&Sminibuffer_depth
);
1308 defsubr (&Stry_completion
);
1309 defsubr (&Sall_completions
);
1310 defsubr (&Scompleting_read
);
1311 defsubr (&Sminibuffer_complete
);
1312 defsubr (&Sminibuffer_complete_word
);
1313 defsubr (&Sminibuffer_complete_and_exit
);
1314 defsubr (&Sdisplay_completion_list
);
1315 defsubr (&Sminibuffer_completion_help
);
1317 defsubr (&Sself_insert_and_exit
);
1318 defsubr (&Sexit_minibuffer
);
1324 initial_define_key (Vminibuffer_local_map
, Ctl ('g'),
1325 "abort-recursive-edit");
1326 initial_define_key (Vminibuffer_local_map
, Ctl ('m'),
1328 initial_define_key (Vminibuffer_local_map
, Ctl ('j'),
1331 initial_define_key (Vminibuffer_local_ns_map
, Ctl ('g'),
1332 "abort-recursive-edit");
1333 initial_define_key (Vminibuffer_local_ns_map
, Ctl ('m'),
1335 initial_define_key (Vminibuffer_local_ns_map
, Ctl ('j'),
1338 initial_define_key (Vminibuffer_local_ns_map
, ' ',
1340 initial_define_key (Vminibuffer_local_ns_map
, '\t',
1342 initial_define_key (Vminibuffer_local_ns_map
, '?',
1343 "self-insert-and-exit");
1345 initial_define_key (Vminibuffer_local_completion_map
, Ctl ('g'),
1346 "abort-recursive-edit");
1347 initial_define_key (Vminibuffer_local_completion_map
, Ctl ('m'),
1349 initial_define_key (Vminibuffer_local_completion_map
, Ctl ('j'),
1352 initial_define_key (Vminibuffer_local_completion_map
, '\t',
1353 "minibuffer-complete");
1354 initial_define_key (Vminibuffer_local_completion_map
, ' ',
1355 "minibuffer-complete-word");
1356 initial_define_key (Vminibuffer_local_completion_map
, '?',
1357 "minibuffer-completion-help");
1359 initial_define_key (Vminibuffer_local_must_match_map
, Ctl ('g'),
1360 "abort-recursive-edit");
1361 initial_define_key (Vminibuffer_local_must_match_map
, Ctl ('m'),
1362 "minibuffer-complete-and-exit");
1363 initial_define_key (Vminibuffer_local_must_match_map
, Ctl ('j'),
1364 "minibuffer-complete-and-exit");
1365 initial_define_key (Vminibuffer_local_must_match_map
, '\t',
1366 "minibuffer-complete");
1367 initial_define_key (Vminibuffer_local_must_match_map
, ' ',
1368 "minibuffer-complete-word");
1369 initial_define_key (Vminibuffer_local_must_match_map
, '?',
1370 "minibuffer-completion-help");