1 /* Minibuffer input and completion.
2 Copyright (C) 1985, 1986 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 1, 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
;
77 /* Width in columns of current minibuffer prompt. */
79 extern int minibuf_prompt_width
;
83 extern Lisp_Object Vglobal_minibuffer_screen
;
86 /* Actual minibuffer invocation. */
88 void read_minibuf_unwind ();
89 Lisp_Object
get_minibuffer ();
90 Lisp_Object
read_minibuf ();
93 read_minibuf (map
, initial
, prompt
, backup_n
, expflag
)
100 register Lisp_Object val
;
101 int count
= specpdl_ptr
- specpdl
;
102 Lisp_Object mini_screen
= WINDOW_SCREEN (XWINDOW (minibuf_window
));
103 struct gcpro gcpro1
, gcpro2
;
105 if (XTYPE (prompt
) != Lisp_String
)
106 prompt
= build_string ("");
108 /* Emacs in -batch mode calls minibuffer: print the prompt. */
109 if (noninteractive
&& XTYPE (prompt
) == Lisp_String
)
110 printf ("%s", XSTRING (prompt
)->data
);
112 if (!enable_recursive_minibuffers
114 && (EQ (selected_window
, minibuf_window
)))
116 || selected_screen
!= XSCREEN (WINDOW_SCREEN (XWINDOW (minibuf_window
)))
118 error ("Command attempted to use minibuffer while in minibuffer");
120 if (minibuf_level
== minibuf_save_vector_size
)
121 minibuf_save_vector
=
122 (struct minibuf_save_data
*)
123 xrealloc (minibuf_save_vector
,
124 (minibuf_save_vector_size
*= 2)
125 * sizeof (struct minibuf_save_data
));
126 minibuf_save_vector
[minibuf_level
].prompt
= minibuf_prompt
;
127 minibuf_save_vector
[minibuf_level
].prompt_width
= minibuf_prompt_width
;
128 minibuf_prompt_width
= 0;
129 /* >> Why is this done this way rather than binding these variables? */
130 minibuf_save_vector
[minibuf_level
].help_form
= Vhelp_form
;
131 minibuf_save_vector
[minibuf_level
].current_prefix_arg
= Vcurrent_prefix_arg
;
132 GCPRO2 (minibuf_save_vector
[minibuf_level
].help_form
,
133 minibuf_save_vector
[minibuf_level
].current_prefix_arg
);
135 record_unwind_protect (Fset_window_configuration
,
136 Fcurrent_window_configuration (Qnil
));
138 /* If the minibuffer window is on a different screen, save that
139 screen's configuration too. */
140 if (XSCREEN (mini_screen
) != selected_screen
)
142 record_unwind_protect (Fset_window_configuration
,
143 Fcurrent_window_configuration (mini_screen
));
146 val
= current_buffer
->directory
;
147 Fset_buffer (get_minibuffer (minibuf_level
));
148 current_buffer
->directory
= val
;
149 Fmake_local_variable (Qprint_escape_newlines
);
150 print_escape_newlines
= 1;
153 /* If the minibuffer window is on another screen, shift this screen's
154 focus to that window, and arrange to put it back later. */
155 if (XSCREEN (WINDOW_SCREEN (XWINDOW (minibuf_window
)))
158 record_unwind_protect (read_minibuf_unwind
,
159 Fcons (Fselected_screen (),
160 SCREEN_FOCUS_SCREEN (selected_screen
)));
162 Fredirect_screen_focus (Fselected_screen (), mini_screen
);
165 record_unwind_protect (read_minibuf_unwind
, Qnil
);
167 record_unwind_protect (read_minibuf_unwind
, Qnil
);
170 Vminibuf_scroll_window
= selected_window
;
171 Fset_window_buffer (minibuf_window
, Fcurrent_buffer ());
172 Fselect_window (minibuf_window
);
173 XFASTINT (XWINDOW (minibuf_window
)->hscroll
) = 0;
180 Finsert (1, &initial
);
181 if (!NULL (backup_n
) && XTYPE (backup_n
) == Lisp_Int
)
182 Fforward_char (backup_n
);
185 minibuf_prompt
= (char *) alloca (XSTRING (prompt
)->size
+ 1);
186 bcopy (XSTRING (prompt
)->data
, minibuf_prompt
, XSTRING (prompt
)->size
+ 1);
187 echo_area_glyphs
= 0;
189 Vhelp_form
= Vminibuffer_help_form
;
190 current_buffer
->keymap
= map
;
192 /* ??? MCC did redraw_screen here if switching screens. */
195 /* If cursor is on the minibuffer line,
196 show the user we have exited by putting it in column 0. */
197 if ((SCREEN_CURSOR_Y (selected_screen
)
198 >= XFASTINT (XWINDOW (minibuf_window
)->top
))
201 SCREEN_CURSOR_X (selected_screen
) = 0;
202 update_screen (selected_screen
, 1, 1);
205 /* Make minibuffer contents into a string */
206 val
= make_string (BEG_ADDR
, Z
- BEG
);
207 bcopy (GAP_END_ADDR
, XSTRING (val
)->data
+ GPT
- BEG
, Z
- GPT
);
208 unbind_to (count
, Qnil
); /* The appropriate screen will get selected
209 in set-window-configuration. */
213 /* VAL is the string of minibuffer text. */
215 last_minibuf_string
= val
;
217 /* If Lisp form desired instead of string, parse it */
224 /* Return a buffer to be used as the minibuffer at depth `depth'.
225 depth = 0 is the lowest allowed argument, and that is the value
226 used for nonrecursive minibuffer invocations */
229 get_minibuffer (depth
)
232 Lisp_Object tail
, num
, buf
;
234 extern Lisp_Object
nconc2 ();
236 XFASTINT (num
) = depth
;
237 tail
= Fnthcdr (num
, Vminibuffer_list
);
240 tail
= Fcons (Qnil
, Qnil
);
241 Vminibuffer_list
= nconc2 (Vminibuffer_list
, tail
);
244 if (NULL (buf
) || NULL (XBUFFER (buf
)->name
))
246 sprintf (name
, " *Minibuf-%d*", depth
);
247 buf
= Fget_buffer_create (build_string (name
));
248 XCONS (tail
)->car
= buf
;
251 reset_buffer (XBUFFER (buf
));
255 /* This function is called on exiting minibuffer, whether normally or not,
256 and it restores the current window, buffer, etc. */
259 read_minibuf_unwind (data
)
262 /* Erase the minibuffer we were using at this level. */
263 Fset_buffer (XWINDOW (minibuf_window
)->buffer
);
265 /* Prevent error in erase-buffer. */
266 current_buffer
->read_only
= Qnil
;
269 /* If this was a recursive minibuffer,
270 tie the minibuffer window back to the outer level minibuffer buffer */
272 /* Make sure minibuffer window is erased, not ignored */
273 windows_or_buffers_changed
++;
274 XFASTINT (XWINDOW (minibuf_window
)->last_modified
) = 0;
276 /* Restore prompt from outer minibuffer */
277 minibuf_prompt
= minibuf_save_vector
[minibuf_level
].prompt
;
278 minibuf_prompt_width
= minibuf_save_vector
[minibuf_level
].prompt_width
;
279 Vhelp_form
= minibuf_save_vector
[minibuf_level
].help_form
;
280 Vcurrent_prefix_arg
= minibuf_save_vector
[minibuf_level
].current_prefix_arg
;
283 /* Redirect the focus of the screen that called the minibuffer. */
285 Fredirect_screen_focus (XCONS (data
)->car
, XCONS (data
)->cdr
);
289 DEFUN ("read-from-minibuffer", Fread_from_minibuffer
, Sread_from_minibuffer
, 1, 5, 0,
290 "Read a string from the minibuffer, prompting with string PROMPT.\n\
291 If optional second arg INITIAL-CONTENTS is non-nil, it is a string\n\
292 to be inserted into the minibuffer before reading input.\n\
293 Third arg KEYMAP is a keymap to use whilst reading;\n\
294 if omitted or nil, the default is `minibuffer-local-map'.\n\
295 If fourth arg READ is non-nil, then interpret the result as a lisp object\n\
296 and return that object:\n\
297 in other words, do `(car (read-from-string INPUT-STRING))'\n\
298 Fifth arg POSITION, if non-nil, is where to put point\n\
299 in the minibuffer after inserting INITIAL-CONTENTS.")
300 (prompt
, initial_input
, keymap
, read
, position
)
301 Lisp_Object prompt
, initial_input
, keymap
, read
, position
;
305 CHECK_STRING (prompt
, 0);
306 if (!NULL (initial_input
))
308 CHECK_STRING (initial_input
, 1);
309 if (!NULL (position
))
311 CHECK_NUMBER (position
, 0);
312 /* Convert to distance from end of input. */
313 pos
= XINT (position
) - 1 - XSTRING (initial_input
)->size
;
318 keymap
= Vminibuffer_local_map
;
320 keymap
= get_keymap (keymap
,2);
321 return read_minibuf (keymap
, initial_input
, prompt
,
325 DEFUN ("read-minibuffer", Fread_minibuffer
, Sread_minibuffer
, 1, 2, 0,
326 "Return a Lisp object read using the minibuffer.\n\
327 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
328 is a string to insert in the minibuffer before reading.")
329 (prompt
, initial_contents
)
330 Lisp_Object prompt
, initial_contents
;
332 CHECK_STRING (prompt
, 0);
333 if (!NULL (initial_contents
))
334 CHECK_STRING (initial_contents
, 1)
335 return read_minibuf (Vminibuffer_local_map
, initial_contents
, prompt
, Qnil
, 1);
338 DEFUN ("eval-minibuffer", Feval_minibuffer
, Seval_minibuffer
, 1, 2, 0,
339 "Return value of Lisp expression read using the minibuffer.\n\
340 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS\n\
341 is a string to insert in the minibuffer before reading.")
342 (prompt
, initial_contents
)
343 Lisp_Object prompt
, initial_contents
;
345 return Feval (Fread_minibuffer (prompt
, initial_contents
));
348 /* Functions that use the minibuffer to read various things. */
350 DEFUN ("read-string", Fread_string
, Sread_string
, 1, 2, 0,
351 "Read a string from the minibuffer, prompting with string PROMPT.\n\
352 If non-nil second arg INITIAL-INPUT is a string to insert before reading.")
353 (prompt
, initial_input
)
354 Lisp_Object prompt
, initial_input
;
356 return Fread_from_minibuffer (prompt
, initial_input
, Qnil
, Qnil
, Qnil
);
359 DEFUN ("read-no-blanks-input", Fread_no_blanks_input
, Sread_no_blanks_input
, 2, 1, 0,
360 "Args PROMPT and INIT, strings. Read a string from the terminal, not allowing blanks.\n\
361 Prompt with PROMPT, and provide INIT as an initial value of the input string.")
363 Lisp_Object prompt
, init
;
365 CHECK_STRING (prompt
, 0);
367 CHECK_STRING (init
, 1);
369 return read_minibuf (Vminibuffer_local_ns_map
, init
, prompt
, Qnil
, 0);
372 DEFUN ("read-command", Fread_command
, Sread_command
, 1, 1, 0,
373 "One arg PROMPT, a string. Read the name of a command and return as a symbol.\n\
374 Prompts with PROMPT.")
378 return Fintern (Fcompleting_read (prompt
, Vobarray
, Qcommandp
, Qt
, Qnil
, Qnil
),
383 DEFUN ("read-function", Fread_function
, Sread_function
, 1, 1, 0,
384 "One arg PROMPT, a string. Read the name of a function and return as a symbol.\n\
385 Prompts with PROMPT.")
389 return Fintern (Fcompleting_read (prompt
, Vobarray
, Qfboundp
, Qt
, Qnil
, Qnil
),
394 DEFUN ("read-variable", Fread_variable
, Sread_variable
, 1, 1, 0,
395 "One arg PROMPT, a string. Read the name of a user variable and return\n\
396 it as a symbol. Prompts with PROMPT.\n\
397 A user variable is one whose documentation starts with a `*' character.")
401 return Fintern (Fcompleting_read (prompt
, Vobarray
,
402 Quser_variable_p
, Qt
, Qnil
, Qnil
),
406 DEFUN ("read-buffer", Fread_buffer
, Sread_buffer
, 1, 3, 0,
407 "One arg PROMPT, a string. Read the name of a buffer and return as a string.\n\
408 Prompts with PROMPT.\n\
409 Optional second arg is value to return if user enters an empty line.\n\
410 If optional third arg REQUIRE-MATCH is non-nil, only existing buffer names are allowed.")
411 (prompt
, def
, require_match
)
412 Lisp_Object prompt
, def
, require_match
;
418 if (XTYPE (def
) == Lisp_Buffer
)
419 def
= XBUFFER (def
)->name
;
422 args
[0] = build_string ("%s(default %s) ");
425 prompt
= Fformat (3, args
);
428 tem
= Fcompleting_read (prompt
, Vbuffer_alist
, Qnil
, require_match
, Qnil
, Qnil
);
430 if (XSTRING (tem
)->size
)
435 DEFUN ("try-completion", Ftry_completion
, Stry_completion
, 2, 3, 0,
436 "Return common substring of all completions of STRING in ALIST.\n\
437 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
438 All that match are compared together; the longest initial sequence\n\
439 common to all matches is returned as a string.\n\
440 If there is no match at all, nil is returned.\n\
441 For an exact match, t is returned.\n\
443 ALIST can be an obarray instead of an alist.\n\
444 Then the print names of all symbols in the obarray are the possible matches.\n\
446 ALIST can also be a function to do the completion itself.\n\
447 It receives three arguments: the values STRING, PREDICATE and nil.\n\
448 Whatever it returns becomes the value of `try-completion'.\n\
450 If optional third argument PREDICATE is non-nil,\n\
451 it is used to test each possible match.\n\
452 The match is a candidate only if PREDICATE returns non-nil.\n\
453 The argument given to PREDICATE is the alist element or the symbol from the obarray.")
454 (string
, alist
, pred
)
455 Lisp_Object string
, alist
, pred
;
457 Lisp_Object bestmatch
, tail
, elt
, eltstring
;
459 int compare
, matchsize
;
460 int list
= CONSP (alist
) || NULL (alist
);
463 Lisp_Object bucket
, zero
, end
, tem
;
464 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
466 CHECK_STRING (string
, 0);
467 if (!list
&& XTYPE (alist
) != Lisp_Vector
)
468 return call3 (alist
, string
, pred
, Qnil
);
472 /* If ALIST is not a list, set TAIL just for gc pro. */
477 obsize
= XVECTOR (alist
)->size
;
478 bucket
= XVECTOR (alist
)->contents
[index
];
483 /* Get the next element of the alist or obarray. */
484 /* Exit the loop if the elements are all used up. */
485 /* elt gets the alist element or symbol.
486 eltstring gets the name to check as a completion. */
493 eltstring
= Fcar (elt
);
498 if (XFASTINT (bucket
) != 0)
501 eltstring
= Fsymbol_name (elt
);
502 if (XSYMBOL (bucket
)->next
)
503 XSETSYMBOL (bucket
, XSYMBOL (bucket
)->next
);
505 XFASTINT (bucket
) = 0;
507 else if (++index
>= obsize
)
511 bucket
= XVECTOR (alist
)->contents
[index
];
516 /* Is this element a possible completion? */
518 if (XTYPE (eltstring
) == Lisp_String
&&
519 XSTRING (string
)->size
<= XSTRING (eltstring
)->size
&&
520 0 > scmp (XSTRING (eltstring
)->data
, XSTRING (string
)->data
,
521 XSTRING (string
)->size
))
524 /* Ignore this element if there is a predicate
525 and the predicate doesn't like it. */
529 if (EQ (pred
, Qcommandp
))
530 tem
= Fcommandp (elt
);
533 GCPRO4 (tail
, string
, eltstring
, bestmatch
);
534 tem
= call1 (pred
, elt
);
537 if (NULL (tem
)) continue;
540 /* Update computation of how much all possible completions match */
543 if (NULL (bestmatch
))
544 bestmatch
= eltstring
, bestmatchsize
= XSTRING (eltstring
)->size
;
547 compare
= min (bestmatchsize
, XSTRING (eltstring
)->size
);
548 matchsize
= scmp (XSTRING (bestmatch
)->data
,
549 XSTRING (eltstring
)->data
,
553 if (completion_ignore_case
)
555 /* If this is an exact match except for case,
556 use it as the best match rather than one that is not an
557 exact match. This way, we get the case pattern
558 of the actual match. */
559 if ((matchsize
== XSTRING (eltstring
)->size
560 && matchsize
< XSTRING (bestmatch
)->size
)
562 /* If there is more than one exact match ignoring case,
563 and one of them is exact including case,
565 /* If there is no exact match ignoring case,
566 prefer a match that does not change the case
568 ((matchsize
== XSTRING (eltstring
)->size
)
570 (matchsize
== XSTRING (bestmatch
)->size
)
571 && !bcmp (XSTRING (eltstring
)->data
,
572 XSTRING (string
)->data
, XSTRING (string
)->size
)
573 && bcmp (XSTRING (bestmatch
)->data
,
574 XSTRING (string
)->data
, XSTRING (string
)->size
)))
575 bestmatch
= eltstring
;
577 bestmatchsize
= matchsize
;
582 if (NULL (bestmatch
))
583 return Qnil
; /* No completions found */
584 /* If we are ignoring case, and there is no exact match,
585 and no additional text was supplied,
586 don't change the case of what the user typed. */
587 if (completion_ignore_case
&& bestmatchsize
== XSTRING (string
)->size
588 && XSTRING (bestmatch
)->size
> bestmatchsize
)
591 /* Return t if the supplied string is an exact match (counting case);
592 it does not require any change to be made. */
593 if (matchcount
== 1 && bestmatchsize
== XSTRING (string
)->size
594 && !bcmp (XSTRING (bestmatch
)->data
, XSTRING (string
)->data
,
598 XFASTINT (zero
) = 0; /* Else extract the part in which */
599 XFASTINT (end
) = bestmatchsize
; /* all completions agree */
600 return Fsubstring (bestmatch
, zero
, end
);
603 /* Compare exactly LEN chars of strings at S1 and S2,
604 ignoring case if appropriate.
605 Return -1 if strings match,
606 else number of chars that match at the beginning. */
609 register char *s1
, *s2
;
612 register int l
= len
;
614 if (completion_ignore_case
)
616 while (l
&& DOWNCASE (*s1
++) == DOWNCASE (*s2
++))
621 while (l
&& *s1
++ == *s2
++)
629 DEFUN ("all-completions", Fall_completions
, Sall_completions
, 2, 3, 0,
630 "Search for partial matches to STRING in ALIST.\n\
631 Each car of each element of ALIST is tested to see if it begins with STRING.\n\
632 The value is a list of all the strings from ALIST that match.\n\
633 ALIST can be an obarray instead of an alist.\n\
634 Then the print names of all symbols in the obarray are the possible matches.\n\
636 ALIST can also be a function to do the completion itself.\n\
637 It receives three arguments: the values STRING, PREDICATE and t.\n\
638 Whatever it returns becomes the value of `all-completion'.\n\
640 If optional third argument PREDICATE is non-nil,\n\
641 it is used to test each possible match.\n\
642 The match is a candidate only if PREDICATE returns non-nil.\n\
643 The argument given to PREDICATE is the alist element or the symbol from the obarray.")
644 (string
, alist
, pred
)
645 Lisp_Object string
, alist
, pred
;
647 Lisp_Object tail
, elt
, eltstring
;
648 Lisp_Object allmatches
;
649 int list
= CONSP (alist
) || NULL (alist
);
651 Lisp_Object bucket
, tem
;
652 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
654 CHECK_STRING (string
, 0);
655 if (!list
&& XTYPE (alist
) != Lisp_Vector
)
657 return call3 (alist
, string
, pred
, Qt
);
661 /* If ALIST is not a list, set TAIL just for gc pro. */
666 obsize
= XVECTOR (alist
)->size
;
667 bucket
= XVECTOR (alist
)->contents
[index
];
672 /* Get the next element of the alist or obarray. */
673 /* Exit the loop if the elements are all used up. */
674 /* elt gets the alist element or symbol.
675 eltstring gets the name to check as a completion. */
682 eltstring
= Fcar (elt
);
687 if (XFASTINT (bucket
) != 0)
690 eltstring
= Fsymbol_name (elt
);
691 if (XSYMBOL (bucket
)->next
)
692 XSETSYMBOL (bucket
, XSYMBOL (bucket
)->next
);
694 XFASTINT (bucket
) = 0;
696 else if (++index
>= obsize
)
700 bucket
= XVECTOR (alist
)->contents
[index
];
705 /* Is this element a possible completion? */
707 if (XTYPE (eltstring
) == Lisp_String
&&
708 XSTRING (string
)->size
<= XSTRING (eltstring
)->size
&&
709 XSTRING (eltstring
)->data
[0] != ' ' &&
710 0 > scmp (XSTRING (eltstring
)->data
, XSTRING (string
)->data
,
711 XSTRING (string
)->size
))
714 /* Ignore this element if there is a predicate
715 and the predicate doesn't like it. */
719 if (EQ (pred
, Qcommandp
))
720 tem
= Fcommandp (elt
);
723 GCPRO4 (tail
, eltstring
, allmatches
, string
);
724 tem
= call1 (pred
, elt
);
727 if (NULL (tem
)) continue;
729 /* Ok => put it on the list. */
730 allmatches
= Fcons (eltstring
, allmatches
);
734 return Fnreverse (allmatches
);
737 Lisp_Object Vminibuffer_completion_table
, Qminibuffer_completion_table
;
738 Lisp_Object Vminibuffer_completion_predicate
, Qminibuffer_completion_predicate
;
739 Lisp_Object Vminibuffer_completion_confirm
, Qminibuffer_completion_confirm
;
741 DEFUN ("completing-read", Fcompleting_read
, Scompleting_read
, 2, 6, 0,
742 "Read a string in the minibuffer, with completion.\n\
743 Args are PROMPT, TABLE, PREDICATE, REQUIRE-MATCH and INITIAL-INPUT.\n\
744 PROMPT is a string to prompt with; normally it ends in a colon and a space.\n\
745 TABLE is an alist whose elements' cars are strings, or an obarray.\n\
746 PREDICATE limits completion to a subset of TABLE.\n\
747 See `try-completion' for more details on completion, TABLE, and PREDICATE.\n\
748 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless\n\
749 the input is (or completes to) an element of TABLE.\n\
750 If it is also not t, Return does not exit if it does non-null completion.\n\
751 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.\n\
752 Case is ignored if ambient value of `completion-ignore-case' is non-nil.\n\
753 If BACKUP-N is specified, point should be placed that many spaces from\n\
754 the end of the buffer. This is useful when providing default values,\n\
755 because you can put point before the last component of a filename or any\n\
756 other component that is likely to be deleted.")
757 (prompt
, table
, pred
, require_match
, init
, backup_n
)
758 Lisp_Object prompt
, table
, pred
, require_match
, init
, backup_n
;
761 int count
= specpdl_ptr
- specpdl
;
762 specbind (Qminibuffer_completion_table
, table
);
763 specbind (Qminibuffer_completion_predicate
, pred
);
764 specbind (Qminibuffer_completion_confirm
,
765 EQ (require_match
, Qt
) ? Qnil
: Qt
);
766 last_exact_completion
= Qnil
;
767 val
= read_minibuf (NULL (require_match
)
768 ? Vminibuffer_local_completion_map
769 : Vminibuffer_local_must_match_map
,
770 init
, prompt
, backup_n
, 0);
771 return unbind_to (count
, val
);
774 /* Temporarily display the string M at the end of the current
775 minibuffer contents. This is used to display things like
776 "[No Match]" when the user requests a completion for a prefix
777 that has no possible completions, and other quick, unobtrusive
780 temp_echo_area_glyphs (m
)
783 /* It's not very modular to do things this way, but then it seems
784 to me that the whole echo_area_glyphs thing is a hack anyway. */
785 extern char *previous_echo_glyphs
;
788 Lisp_Object oinhibit
;
789 oinhibit
= Vinhibit_quit
;
791 /* Clear out any old echo-area message to make way for our new
793 echo_area_glyphs
= previous_echo_glyphs
= 0;
799 Fsit_for (make_number (2), Qnil
, Qnil
);
800 del_range (point
, ZV
);
801 if (!NULL (Vquit_flag
))
804 unread_command_char
= Ctl ('g');
806 Vinhibit_quit
= oinhibit
;
809 Lisp_Object
Fminibuffer_completion_help ();
810 Lisp_Object
assoc_for_completion ();
813 * 0 no possible completion
814 * 1 was already an exact and unique completion
815 * 3 was already an exact completion
816 * 4 completed to an exact completion
817 * 5 some completion happened
818 * 6 no completion happened
823 Lisp_Object completion
, tem
;
827 completion
= Ftry_completion (Fbuffer_string (), Vminibuffer_completion_table
,
828 Vminibuffer_completion_predicate
);
829 last
= last_exact_completion
;
830 last_exact_completion
= Qnil
;
832 if (NULL (completion
))
835 temp_echo_area_glyphs (" [No match]");
839 if (EQ (completion
, Qt
)) /* exact and unique match */
843 tem
= Fstring_equal (completion
, Fbuffer_string());
844 if (completedp
= NULL (tem
))
846 Ferase_buffer (); /* Some completion happened */
847 Finsert (1, &completion
);
850 /* It did find a match. Do we match some possibility exactly now? */
851 if (CONSP (Vminibuffer_completion_table
)
852 || NULL (Vminibuffer_completion_table
))
853 tem
= assoc_for_completion (Fbuffer_string (),
854 Vminibuffer_completion_table
);
855 else if (XTYPE (Vminibuffer_completion_table
) == Lisp_Vector
)
857 /* the primitive used by Fintern_soft */
858 extern Lisp_Object
oblookup ();
860 tem
= Fbuffer_string ();
861 /* Bypass intern-soft as that loses for nil */
862 tem
= oblookup (Vminibuffer_completion_table
,
863 XSTRING (tem
)->data
, XSTRING (tem
)->size
);
864 if (XTYPE (tem
) != Lisp_Symbol
)
866 else if (!NULL (Vminibuffer_completion_predicate
))
867 tem
= call1 (Vminibuffer_completion_predicate
, tem
);
872 tem
= call3 (Vminibuffer_completion_table
,
874 Vminibuffer_completion_predicate
,
878 { /* not an exact match */
882 Fminibuffer_completion_help ();
884 temp_echo_area_glyphs (" [Next char not unique]");
889 /* If the last exact completion and this one were the same,
890 it means we've already given a "Complete but not unique"
891 message and the user's hit TAB again, so now we give him help. */
892 last_exact_completion
= completion
;
895 tem
= Fbuffer_string ();
896 if (!NULL (Fequal (tem
, last
)))
897 Fminibuffer_completion_help ();
902 /* Like assoc but assumes KEY is a string, and ignores case if appropriate. */
905 assoc_for_completion (key
, list
)
906 register Lisp_Object key
;
909 register Lisp_Object tail
;
911 if (completion_ignore_case
)
914 for (tail
= list
; !NULL (tail
); tail
= Fcdr (tail
))
916 register Lisp_Object elt
, tem
, thiscar
;
918 if (!CONSP (elt
)) continue;
919 thiscar
= Fcar (elt
);
920 if (XTYPE (thiscar
) != Lisp_String
)
922 if (completion_ignore_case
)
923 thiscar
= Fupcase (thiscar
);
924 tem
= Fequal (thiscar
, key
);
925 if (!NULL (tem
)) return elt
;
931 DEFUN ("minibuffer-complete", Fminibuffer_complete
, Sminibuffer_complete
, 0, 0, "",
932 "Complete the minibuffer contents as far as possible.")
935 register int i
= do_completion ();
942 temp_echo_area_glyphs (" [Sole completion]");
946 temp_echo_area_glyphs (" [Complete, but not unique]");
953 DEFUN ("minibuffer-complete-and-exit", Fminibuffer_complete_and_exit
,
954 Sminibuffer_complete_and_exit
, 0, 0, "",
955 "Complete the minibuffer contents, and maybe exit.\n\
956 Exit if the name is valid with no completion needed.\n\
957 If name was completed to a valid match,\n\
958 a repetition of this command will exit.")
963 /* Allow user to specify null string */
967 i
= do_completion ();
975 if (!NULL (Vminibuffer_completion_confirm
))
977 temp_echo_area_glyphs (" [Confirm]");
987 Fthrow (Qexit
, Qnil
);
991 DEFUN ("minibuffer-complete-word", Fminibuffer_complete_word
, Sminibuffer_complete_word
,
993 "Complete the minibuffer contents at most a single word.\n\
994 After one word is completed as much as possible, a space or hyphen\n\
995 is added, provided that matches some possible completion.")
998 Lisp_Object completion
, tem
;
1000 register unsigned char *completion_string
;
1001 /* We keep calling Fbuffer_string
1002 rather than arrange for GC to hold onto a pointer to
1003 one of the strings thus made. */
1005 completion
= Ftry_completion (Fbuffer_string (),
1006 Vminibuffer_completion_table
,
1007 Vminibuffer_completion_predicate
);
1008 if (NULL (completion
))
1011 temp_echo_area_glyphs (" [No match]");
1014 if (EQ (completion
, Qt
))
1017 #if 0 /* How the below code used to look, for reference */
1018 tem
= Fbuffer_string ();
1019 b
= XSTRING (tem
)->data
;
1020 i
= ZV
- 1 - XSTRING (completion
)->size
;
1021 p
= XSTRING (completion
)->data
;
1023 0 <= scmp (b
, p
, ZV
- 1))
1026 /* Set buffer to longest match of buffer tail and completion head. */
1027 while (0 <= scmp (b
+ i
, p
, ZV
- 1 - i
))
1029 del_range (1, i
+ 1);
1032 #else /* Rewritten code */
1034 register unsigned char *buffer_string
;
1035 int buffer_length
, completion_length
;
1037 tem
= Fbuffer_string ();
1038 buffer_string
= XSTRING (tem
)->data
;
1039 completion_string
= XSTRING (completion
)->data
;
1040 buffer_length
= XSTRING (tem
)->size
; /* ie ZV - BEGV */
1041 completion_length
= XSTRING (completion
)->size
;
1042 i
= buffer_length
- completion_length
;
1043 /* Mly: I don't understand what this is supposed to do AT ALL */
1045 0 <= scmp (buffer_string
, completion_string
, buffer_length
))
1047 /* Set buffer to longest match of buffer tail and completion head. */
1051 while (0 <= scmp (buffer_string
++, completion_string
, buffer_length
--))
1053 del_range (1, i
+ 1);
1057 #endif /* Rewritten code */
1060 /* If completion finds next char not unique,
1061 consider adding a space or a hyphen */
1062 if (i
== XSTRING (completion
)->size
)
1064 tem
= Ftry_completion (concat2 (Fbuffer_string (), build_string (" ")),
1065 Vminibuffer_completion_table
,
1066 Vminibuffer_completion_predicate
);
1067 if (XTYPE (tem
) == Lisp_String
)
1071 tem
= Ftry_completion (concat2 (Fbuffer_string (), build_string ("-")),
1072 Vminibuffer_completion_table
,
1073 Vminibuffer_completion_predicate
);
1074 if (XTYPE (tem
) == Lisp_String
)
1079 /* Now find first word-break in the stuff found by completion.
1080 i gets index in string of where to stop completing. */
1081 completion_string
= XSTRING (completion
)->data
;
1083 for (; i
< XSTRING (completion
)->size
; i
++)
1084 if (SYNTAX (completion_string
[i
]) != Sword
) break;
1085 if (i
< XSTRING (completion
)->size
)
1088 /* If got no characters, print help for user. */
1093 Fminibuffer_completion_help ();
1097 /* Otherwise insert in minibuffer the chars we got */
1100 insert_from_string (completion
, 0, i
);
1104 DEFUN ("display-completion-list", Fdisplay_completion_list
, Sdisplay_completion_list
,
1106 "Display in a buffer the list of completions, COMPLETIONS.\n\
1107 Each element may be just a symbol or string\n\
1108 or may be a list of two strings to be printed as if concatenated.")
1110 Lisp_Object completions
;
1112 register Lisp_Object tail
, elt
;
1114 struct buffer
*old
= current_buffer
;
1115 /* No GCPRO needed, since (when it matters) every variable
1116 points to a non-string that is pointed to by COMPLETIONS. */
1118 set_buffer_internal (XBUFFER (Vstandard_output
));
1120 if (NULL (completions
))
1121 insert_string ("There are no possible completions of what you have typed.");
1124 insert_string ("Possible completions are:");
1125 for (tail
= completions
, i
= 0; !NULL (tail
); tail
= Fcdr (tail
), i
++)
1127 /* this needs fixing for the case of long completions
1128 and/or narrow windows */
1129 /* Sadly, the window it will appear in is not known
1130 until after the text has been made. */
1132 Findent_to (make_number (35), make_number (1));
1138 Fprinc (Fcar (elt
), Qnil
);
1139 Fprinc (Fcar (Fcdr (elt
)), Qnil
);
1145 set_buffer_internal (old
);
1149 DEFUN ("minibuffer-completion-help", Fminibuffer_completion_help
, Sminibuffer_completion_help
,
1151 "Display a list of possible completions of the current minibuffer contents.")
1154 Lisp_Object completions
;
1156 message ("Making completion list...");
1157 completions
= Fall_completions (Fbuffer_string (),
1158 Vminibuffer_completion_table
,
1159 Vminibuffer_completion_predicate
);
1160 echo_area_glyphs
= 0;
1162 if (NULL (completions
))
1165 temp_echo_area_glyphs (" [No completions]");
1168 internal_with_output_to_temp_buffer ("*Completions*",
1169 Fdisplay_completion_list
,
1170 Fsort (completions
, Qstring_lessp
));
1174 DEFUN ("self-insert-and-exit", Fself_insert_and_exit
, Sself_insert_and_exit
, 0, 0, "",
1175 "Terminate minibuffer input.")
1178 if (XTYPE (last_command_char
) == Lisp_Int
)
1179 internal_self_insert (last_command_char
, 0);
1183 Fthrow (Qexit
, Qnil
);
1186 DEFUN ("exit-minibuffer", Fexit_minibuffer
, Sexit_minibuffer
, 0, 0, "",
1187 "Terminate this minibuffer argument.")
1190 Fthrow (Qexit
, Qnil
);
1193 DEFUN ("minibuffer-depth", Fminibuffer_depth
, Sminibuffer_depth
, 0, 0, 0,
1194 "Return current depth of activations of minibuffer, a nonnegative integer.")
1197 return make_number (minibuf_level
);
1201 init_minibuf_once ()
1203 Vminibuffer_list
= Qnil
;
1204 staticpro (&Vminibuffer_list
);
1211 minibuf_save_vector_size
= 5;
1212 minibuf_save_vector
= (struct minibuf_save_data
*) malloc (5 * sizeof (struct minibuf_save_data
));
1214 Qminibuffer_completion_table
= intern ("minibuffer-completion-table");
1215 staticpro (&Qminibuffer_completion_table
);
1217 Qminibuffer_completion_confirm
= intern ("minibuffer-completion-confirm");
1218 staticpro (&Qminibuffer_completion_confirm
);
1220 Qminibuffer_completion_predicate
= intern ("minibuffer-completion-predicate");
1221 staticpro (&Qminibuffer_completion_predicate
);
1223 staticpro (&last_minibuf_string
);
1224 last_minibuf_string
= Qnil
;
1226 Quser_variable_p
= intern ("user-variable-p");
1227 staticpro (&Quser_variable_p
);
1231 DEFVAR_BOOL ("completion-auto-help", &auto_help
,
1232 "*Non-nil means automatically provide help for invalid completion input.");
1235 DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case
,
1236 "Non-nil means don't consider case significant in completion.");
1237 completion_ignore_case
= 0;
1239 DEFVAR_BOOL ("enable-recursive-minibuffers", &enable_recursive_minibuffers
,
1240 "*Non-nil means to allow minibuffer commands while in the minibuffer.\n\
1241 More precisely, this variable makes a difference when the minibuffer window\n\
1242 is the selected window. If you are in some other window, minibuffer commands\n\
1243 are allowed even if a minibuffer is active.");
1244 enable_recursive_minibuffers
= 0;
1246 DEFVAR_LISP ("minibuffer-completion-table", &Vminibuffer_completion_table
,
1247 "Alist or obarray used for completion in the minibuffer.\n\
1248 This becomes the ALIST argument to `try-completion' and `all-completion'.\n\
1250 The value may alternatively be a function, which is given three arguments:\n\
1251 STRING, the current buffer contents;\n\
1252 PREDICATE, the predicate for filtering possible matches;\n\
1253 CODE, which says what kind of things to do.\n\
1254 CODE can be nil, t or `lambda'.\n\
1255 nil means to return the best completion of STRING, or nil if there is none.\n\
1256 t means to return a list of all possible completions of STRING.\n\
1257 `lambda' means to return t if STRING is a valid completion as it stands.");
1258 Vminibuffer_completion_table
= Qnil
;
1260 DEFVAR_LISP ("minibuffer-completion-predicate", &Vminibuffer_completion_predicate
,
1261 "Within call to `completing-read', this holds the PREDICATE argument.");
1262 Vminibuffer_completion_predicate
= Qnil
;
1264 DEFVAR_LISP ("minibuffer-completion-confirm", &Vminibuffer_completion_confirm
,
1265 "Non-nil => demand confirmation of completion before exiting minibuffer.");
1266 Vminibuffer_completion_confirm
= Qnil
;
1268 DEFVAR_LISP ("minibuffer-help-form", &Vminibuffer_help_form
,
1269 "Value that `help-form' takes on inside the minibuffer.");
1270 Vminibuffer_help_form
= Qnil
;
1272 defsubr (&Sread_from_minibuffer
);
1273 defsubr (&Seval_minibuffer
);
1274 defsubr (&Sread_minibuffer
);
1275 defsubr (&Sread_string
);
1276 defsubr (&Sread_command
);
1277 defsubr (&Sread_variable
);
1278 defsubr (&Sread_buffer
);
1279 defsubr (&Sread_no_blanks_input
);
1280 defsubr (&Sminibuffer_depth
);
1282 defsubr (&Stry_completion
);
1283 defsubr (&Sall_completions
);
1284 defsubr (&Scompleting_read
);
1285 defsubr (&Sminibuffer_complete
);
1286 defsubr (&Sminibuffer_complete_word
);
1287 defsubr (&Sminibuffer_complete_and_exit
);
1288 defsubr (&Sdisplay_completion_list
);
1289 defsubr (&Sminibuffer_completion_help
);
1291 defsubr (&Sself_insert_and_exit
);
1292 defsubr (&Sexit_minibuffer
);
1298 initial_define_key (Vminibuffer_local_map
, Ctl ('g'),
1299 "abort-recursive-edit");
1300 initial_define_key (Vminibuffer_local_map
, Ctl ('m'),
1302 initial_define_key (Vminibuffer_local_map
, Ctl ('j'),
1305 initial_define_key (Vminibuffer_local_ns_map
, Ctl ('g'),
1306 "abort-recursive-edit");
1307 initial_define_key (Vminibuffer_local_ns_map
, Ctl ('m'),
1309 initial_define_key (Vminibuffer_local_ns_map
, Ctl ('j'),
1312 initial_define_key (Vminibuffer_local_ns_map
, ' ',
1314 initial_define_key (Vminibuffer_local_ns_map
, '\t',
1316 initial_define_key (Vminibuffer_local_ns_map
, '?',
1317 "self-insert-and-exit");
1319 initial_define_key (Vminibuffer_local_completion_map
, Ctl ('g'),
1320 "abort-recursive-edit");
1321 initial_define_key (Vminibuffer_local_completion_map
, Ctl ('m'),
1323 initial_define_key (Vminibuffer_local_completion_map
, Ctl ('j'),
1326 initial_define_key (Vminibuffer_local_completion_map
, '\t',
1327 "minibuffer-complete");
1328 initial_define_key (Vminibuffer_local_completion_map
, ' ',
1329 "minibuffer-complete-word");
1330 initial_define_key (Vminibuffer_local_completion_map
, '?',
1331 "minibuffer-completion-help");
1333 initial_define_key (Vminibuffer_local_must_match_map
, Ctl ('g'),
1334 "abort-recursive-edit");
1335 initial_define_key (Vminibuffer_local_must_match_map
, Ctl ('m'),
1336 "minibuffer-complete-and-exit");
1337 initial_define_key (Vminibuffer_local_must_match_map
, Ctl ('j'),
1338 "minibuffer-complete-and-exit");
1339 initial_define_key (Vminibuffer_local_must_match_map
, '\t',
1340 "minibuffer-complete");
1341 initial_define_key (Vminibuffer_local_must_match_map
, ' ',
1342 "minibuffer-complete-word");
1343 initial_define_key (Vminibuffer_local_must_match_map
, '?',
1344 "minibuffer-completion-help");