28c7d1c2cfe39541acbd62d878a181fb0d28204f
[sxemacs] / src / ui / minibuf.c
1 /* Minibuffer input and completion.
2    Copyright (C) 1985, 1986, 1992-1995 Free Software Foundation, Inc.
3    Copyright (C) 1995 Sun Microsystems, Inc.
4
5 This file is part of SXEmacs
6
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20
21 /* Synched up with: Mule 2.0, FSF 19.28.  Mule-ized except as noted.
22    Substantially different from FSF. */
23
24 /* #### dmoore - All sorts of things in here can call lisp, like message.
25    Track all this stuff. */
26
27 #include <config.h>
28 #include "lisp.h"
29
30 #include "buffer.h"
31 #include "commands.h"
32 #include "console-stream.h"
33 #include "events/events.h"
34 #include "frame.h"
35 #include "insdel.h"
36 #include "redisplay.h"
37 #include "window.h"
38
39 /* Depth in minibuffer invocations.  */
40 int minibuf_level;
41
42 Lisp_Object Qcompletion_ignore_case;
43
44 /* Nonzero means completion ignores case.  */
45 int completion_ignore_case;
46
47 /* List of regexps that should restrict possible completions.  */
48 Lisp_Object Vcompletion_regexp_list;
49
50 /* The echo area buffer. */
51 Lisp_Object Vecho_area_buffer;
52
53 /* Prompt to display in front of the minibuffer contents */
54 Lisp_Object Vminibuf_prompt;
55
56 /* Added on 97/3/14 by Jareth Hein (jhod@po.iijnet.or.jp) for input system support */
57 /* String to be displayed in front of prompt of the minibuffer contents */
58 Lisp_Object Vminibuf_preprompt;
59
60 /* Hook to run just after entry to minibuffer. */
61 Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
62
63 Lisp_Object Qappend_message, Qcurrent_message_label,
64     Qclear_message, Qdisplay_message;
65 \f
66 DEFUN("minibuffer-depth", Fminibuffer_depth, 0, 0, 0,   /*
67 Return current depth of activations of minibuffer, a nonnegative integer.
68 */
69       ())
70 {
71         return make_int(minibuf_level);
72 }
73
74 /* The default buffer to use as the window-buffer of minibuffer windows */
75 /*  Note there is special code in kill-buffer to make this unkillable */
76 Lisp_Object Vminibuffer_zero;
77 \f
78 /* Actual minibuffer invocation. */
79
80 static Lisp_Object read_minibuffer_internal_unwind(Lisp_Object unwind_data)
81 {
82         Lisp_Object frame;
83         XWINDOW(minibuf_window)->last_modified[CURRENT_DISP] = Qzero;
84         XWINDOW(minibuf_window)->last_modified[DESIRED_DISP] = Qzero;
85         XWINDOW(minibuf_window)->last_modified[CMOTION_DISP] = Qzero;
86         XWINDOW(minibuf_window)->last_facechange[CURRENT_DISP] = Qzero;
87         XWINDOW(minibuf_window)->last_facechange[DESIRED_DISP] = Qzero;
88         XWINDOW(minibuf_window)->last_facechange[CMOTION_DISP] = Qzero;
89         Vminibuf_prompt = Felt(unwind_data, Qzero);
90         minibuf_level = XINT(Felt(unwind_data, make_int(1)));
91         while (CONSP(unwind_data)) {
92                 Lisp_Object victim = unwind_data;
93                 unwind_data = XCDR(unwind_data);
94                 free_cons(XCONS(victim));
95         }
96
97         /* If cursor is on the minibuffer line,
98            show the user we have exited by putting it in column 0.  */
99         frame = Fselected_frame(Qnil);
100         if (!noninteractive && !NILP(frame)
101             && !NILP(XFRAME(frame)->minibuffer_window)) {
102                 struct window *w = XWINDOW(XFRAME(frame)->minibuffer_window);
103                 redisplay_move_cursor(w, 0, 0);
104         }
105
106         return Qnil;
107 }
108
109 /* 97/4/13 jhod: Added for input methods */
110 DEFUN("set-minibuffer-preprompt", Fset_minibuffer_preprompt, 1, 1, 0,   /*
111 Set the minibuffer preprompt string to PREPROMPT. This is used by language
112 input methods to relay state information to the user.
113 */
114       (preprompt))
115 {
116         if (NILP(preprompt)) {
117                 Vminibuf_preprompt = Qnil;
118         } else {
119                 CHECK_STRING(preprompt);
120
121                 Vminibuf_preprompt = LISP_GETTEXT(preprompt);
122         }
123         return Qnil;
124 }
125
126 DEFUN("read-minibuffer-internal", Fread_minibuffer_internal, 1, 1, 0,   /*
127 Lowest-level interface to minibuffers.  Don't call this.
128 */
129       (prompt))
130 {
131         /* This function can GC */
132         int speccount = specpdl_depth();
133         Lisp_Object val;
134
135         CHECK_STRING(prompt);
136
137         single_console_state();
138
139         record_unwind_protect(read_minibuffer_internal_unwind,
140                               noseeum_cons
141                               (Vminibuf_prompt,
142                                noseeum_cons(make_int(minibuf_level), Qnil)));
143         Vminibuf_prompt = LISP_GETTEXT(prompt);
144
145         /* NOTE: Here (or somewhere around here), in FSFmacs 19.30,
146            choose_minibuf_frame() is called.  This is the only
147            place in FSFmacs that it's called any more -- there's
148            also a call in xterm.c, but commented out, and 19.28
149            had the calls in different places.
150
151            choose_minibuf_frame() does the following:
152
153            if (!EQ (minibuf_window, selected_frame()->minibuffer_window))
154            {
155            Fset_window_buffer (selected_frame()->minibuffer_window,
156            XWINDOW (minibuf_window)->buffer);
157            minibuf_window = selected_frame()->minibuffer_window;
158            }
159
160            #### Note that we don't do the set-window-buffer.  This call is
161            similar, but not identical, to a set-window-buffer call made
162            in `read-from-minibuffer' in minibuf.el.  I hope it's close
163            enough, because minibuf_window isn't really exported to Lisp.
164
165            The comment above choose_minibuf_frame() reads:
166
167            Put minibuf on currently selected frame's minibuffer.
168            We do this whenever the user starts a new minibuffer
169            or when a minibuffer exits.  */
170
171         minibuf_window = FRAME_MINIBUF_WINDOW(selected_frame());
172
173         run_hook(Qminibuffer_setup_hook);
174
175         minibuf_level++;
176         clear_echo_area(selected_frame(), Qnil, 0);
177
178         val = call_command_loop(Qt);
179
180         return unbind_to(speccount, val);
181 }
182 \f
183 /* Completion hair */
184
185 /* Compare exactly LEN chars of strings at S1 and S2,
186    ignoring case if appropriate.
187    Return -1 if strings match,
188    else number of chars that match at the beginning.  */
189
190 /* Note that this function works in Charcounts, unlike most functions.
191    This is necessary for many reasons, one of which is that two
192    strings may match even if they have different numbers of bytes,
193    if IGNORE_CASE is true. */
194
195 Charcount
196 scmp_1(const Bufbyte * s1, const Bufbyte * s2, Charcount len, int ignore_case)
197 {
198         Charcount l = len;
199
200         if (ignore_case) {
201                 while (l) {
202                         Emchar c1 =
203                             DOWNCASE(current_buffer, charptr_emchar(s1));
204                         Emchar c2 =
205                             DOWNCASE(current_buffer, charptr_emchar(s2));
206
207                         if (c1 == c2) {
208                                 l--;
209                                 INC_CHARPTR(s1);
210                                 INC_CHARPTR(s2);
211                         } else
212                                 break;
213                 }
214         } else {
215                 while (l && charptr_emchar(s1) == charptr_emchar(s2)) {
216                         l--;
217                         INC_CHARPTR(s1);
218                         INC_CHARPTR(s2);
219                 }
220         }
221
222         if (l == 0)
223                 return -1;
224         else
225                 return len - l;
226 }
227
228 int
229 regexp_ignore_completion_p(const Bufbyte * nonreloc,
230                            Lisp_Object reloc, Bytecount offset,
231                            Bytecount length)
232 {
233         /* Ignore this element if it fails to match all the regexps.  */
234         if (!NILP(Vcompletion_regexp_list)) {
235                 Lisp_Object regexps;
236                 EXTERNAL_LIST_LOOP(regexps, Vcompletion_regexp_list) {
237                         Lisp_Object re = XCAR(regexps);
238                         CHECK_STRING(re);
239                         if (fast_string_match(re, nonreloc, reloc, offset,
240                                               length, 0, ERROR_ME, 0) < 0)
241                                 return 1;
242                 }
243         }
244         return 0;
245 }
246
247 /* Callers should GCPRO, since this may call eval */
248 static int
249 ignore_completion_p(Lisp_Object completion_string,
250                     Lisp_Object pred, Lisp_Object completion)
251 {
252         if (regexp_ignore_completion_p(0, completion_string, 0, -1))
253                 return 1;
254
255         /* Ignore this element if there is a predicate
256            and the predicate doesn't like it. */
257         if (!NILP(pred)) {
258                 Lisp_Object tem;
259                 if (EQ(pred, Qcommandp))
260                         tem = Fcommandp(completion);
261                 else
262                         tem = call1(pred, completion);
263                 if (NILP(tem))
264                         return 1;
265         }
266         return 0;
267 }
268
269 /* #### Maybe we should allow COLLECTION to be a hash table.
270    It is wrong for the use of obarrays to be better-rewarded than the
271    use of hash tables.  By better-rewarded I mean that you can pass an
272    obarray to all of the completion functions, whereas you can't do
273    anything like that with a hash table.
274
275    To do so, there should probably be a
276    map_obarray_or_alist_or_hash_table function which would be used by
277    both Ftry_completion and Fall_completions.  But would the
278    additional funcalls slow things down?  */
279
280 DEFUN("try-completion", Ftry_completion, 2, 3, 0,       /*
281 Return common substring of all completions of STRING in COLLECTION.
282 COLLECTION must be an alist, an obarray, or a function.
283 Each string in COLLECTION is tested to see if it begins with STRING.
284 All that match are compared together; the longest initial sequence
285 common to all matches is returned as a string.  If there is no match
286 at all, nil is returned.  For an exact match, t is returned.
287
288 If COLLECTION is an alist, the cars of the elements of the alist
289 \(which must be strings) form the set of possible completions.
290
291 If COLLECTION is an obarray, the names of all symbols in the obarray
292 are the possible completions.
293
294 If COLLECTION is a function, it is called with three arguments: the
295 values STRING, PREDICATE and nil.  Whatever it returns becomes the
296 value of `try-completion'.
297
298 If optional third argument PREDICATE is non-nil, it is used to test
299 each possible match.  The match is a candidate only if PREDICATE
300 returns non-nil.  The argument given to PREDICATE is the alist element
301 or the symbol from the obarray.
302 */
303       (string, collection, predicate))
304 {
305         /* This function can GC */
306         Lisp_Object bestmatch, tail;
307         Charcount bestmatchsize = 0;
308         int list;
309         int indice = 0;
310         int matchcount = 0;
311         int obsize;
312         Lisp_Object bucket;
313         Charcount slength, blength;
314
315         CHECK_STRING(string);
316
317         if (CONSP(collection)) {
318                 Lisp_Object tem = XCAR(collection);
319                 if (SYMBOLP(tem))       /* lambda, autoload, etc.  Emacs-lisp sucks */
320                         return call3(collection, string, predicate, Qnil);
321                 else
322                         list = 1;
323         } else if (VECTORP(collection))
324                 list = 0;
325         else if (NILP(collection))
326                 list = 1;
327         else
328                 return call3(collection, string, predicate, Qnil);
329
330         bestmatch = Qnil;
331         blength = 0;
332         slength = XSTRING_CHAR_LENGTH(string);
333
334         /* If COLLECTION is not a list, set TAIL just for gc pro.  */
335         tail = collection;
336         if (!list) {
337                 obsize = XVECTOR_LENGTH(collection);
338                 bucket = XVECTOR_DATA(collection)[indice];
339         } else {                /* warning suppression */
340
341                 obsize = 0;
342                 bucket = Qnil;
343         }
344
345         while (1) {
346                 /* Get the next element of the alist or obarray. */
347                 /* Exit the loop if the elements are all used up. */
348                 /* elt gets the alist element or symbol.
349                    eltstring gets the name to check as a completion. */
350                 Lisp_Object elt;
351                 Lisp_Object eltstring;
352
353                 if (list) {
354                         if (NILP(tail))
355                                 break;
356                         elt = Fcar(tail);
357                         eltstring = Fcar(elt);
358                         tail = Fcdr(tail);
359                 } else {
360                         if (!ZEROP(bucket)) {
361                                 Lisp_Symbol *next;
362                                 if (!SYMBOLP(bucket)) {
363                                         signal_simple_error
364                                             ("Bad obarray passed to try-completions",
365                                              bucket);
366                                 }
367                                 next = symbol_next(XSYMBOL(bucket));
368                                 elt = bucket;
369                                 eltstring = Fsymbol_name(elt);
370                                 if (next)
371                                         XSETSYMBOL(bucket, next);
372                                 else
373                                         bucket = Qzero;
374                         } else if (++indice >= obsize)
375                                 break;
376                         else {
377                                 bucket = XVECTOR_DATA(collection)[indice];
378                                 continue;
379                         }
380                 }
381
382                 /* Is this element a possible completion? */
383
384                 if (STRINGP(eltstring)) {
385                         Charcount eltlength = XSTRING_CHAR_LENGTH(eltstring);
386                         if (slength <= eltlength
387                             && (0 > scmp(XSTRING_DATA(eltstring),
388                                          XSTRING_DATA(string), slength))) {
389                                 {
390                                         struct gcpro gcpro1, gcpro2, gcpro3,
391                                             gcpro4;
392                                         int loser;
393                                         GCPRO4(tail, string, eltstring,
394                                                bestmatch);
395                                         loser =
396                                             ignore_completion_p(eltstring,
397                                                                 predicate, elt);
398                                         UNGCPRO;
399                                         if (loser)      /* reject this one */
400                                                 continue;
401                                 }
402
403                                 /* Update computation of how much all possible
404                                    completions match */
405
406                                 matchcount++;
407                                 if (NILP(bestmatch)) {
408                                         bestmatch = eltstring;
409                                         blength = eltlength;
410                                         bestmatchsize = eltlength;
411                                 } else {
412                                         Charcount compare =
413                                             min(bestmatchsize, eltlength);
414                                         Charcount matchsize =
415                                             scmp(XSTRING_DATA(bestmatch),
416                                                  XSTRING_DATA(eltstring),
417                                                  compare);
418                                         if (matchsize < 0)
419                                                 matchsize = compare;
420                                         if (completion_ignore_case) {
421                                                 /* If this is an exact match except for case,
422                                                    use it as the best match rather than one that is not
423                                                    an exact match.  This way, we get the case pattern
424                                                    of the actual match.  */
425                                                 if ((matchsize == eltlength
426                                                      && matchsize < blength)
427                                                     ||
428                                                     /* If there is more than one exact match ignoring
429                                                        case, and one of them is exact including case,
430                                                        prefer that one.  */
431                                                     /* If there is no exact match ignoring case,
432                                                        prefer a match that does not change the case
433                                                        of the input.  */
434                                                     ((matchsize == eltlength)
435                                                      == (matchsize == blength)
436                                                      && 0 >
437                                                      scmp_1(XSTRING_DATA
438                                                             (eltstring),
439                                                             XSTRING_DATA
440                                                             (string), slength,
441                                                             0)
442                                                      && 0 <=
443                                                      scmp_1(XSTRING_DATA
444                                                             (bestmatch),
445                                                             XSTRING_DATA
446                                                             (string), slength,
447                                                             0))) {
448                                                         bestmatch = eltstring;
449                                                         blength = eltlength;
450                                                 }
451                                         }
452                                         bestmatchsize = matchsize;
453                                 }
454                         }
455                 }
456         }
457
458         if (NILP(bestmatch))
459                 return Qnil;    /* No completions found */
460         /* If we are ignoring case, and there is no exact match,
461            and no additional text was supplied,
462            don't change the case of what the user typed.  */
463         if (completion_ignore_case
464             && bestmatchsize == slength && blength > bestmatchsize)
465                 return string;
466
467         /* Return t if the supplied string is an exact match (counting case);
468            it does not require any change to be made.  */
469         if (matchcount == 1
470             && bestmatchsize == slength
471             && 0 > scmp_1(XSTRING_DATA(bestmatch),
472                           XSTRING_DATA(string), bestmatchsize, 0))
473                 return Qt;
474
475         /* Else extract the part in which all completions agree */
476         return Fsubstring(bestmatch, Qzero, make_int(bestmatchsize));
477 }
478 \f
479 DEFUN("all-completions", Fall_completions, 2, 3, 0,     /*
480 Search for partial matches to STRING in COLLECTION.
481 COLLECTION must be an alist, an obarray, or a function.
482 Each string in COLLECTION is tested to see if it begins with STRING.
483 The value is a list of all the strings from COLLECTION that match.
484
485 If COLLECTION is an alist, the cars of the elements of the alist
486 \(which must be strings) form the set of possible completions.
487
488 If COLLECTION is an obarray, the names of all symbols in the obarray
489 are the possible completions.
490
491 If COLLECTION is a function, it is called with three arguments: the
492 values STRING, PREDICATE and t.  Whatever it returns becomes the
493 value of `all-completions'.
494
495 If optional third argument PREDICATE is non-nil, it is used to test
496 each possible match.  The match is a candidate only if PREDICATE
497 returns non-nil.  The argument given to PREDICATE is the alist element
498 or the symbol from the obarray.
499 */
500       (string, collection, predicate))
501 {
502         /* This function can GC */
503         Lisp_Object tail;
504         Lisp_Object allmatches;
505         int list;
506         int indice = 0;
507         int obsize;
508         Lisp_Object bucket;
509         Charcount slength;
510
511         CHECK_STRING(string);
512
513         if (CONSP(collection)) {
514                 Lisp_Object tem = XCAR(collection);
515                 if (SYMBOLP(tem))       /* lambda, autoload, etc.  Emacs-lisp sucks */
516                         return call3(collection, string, predicate, Qt);
517                 else
518                         list = 1;
519         } else if (VECTORP(collection))
520                 list = 0;
521         else if (NILP(collection))
522                 list = 1;
523         else
524                 return call3(collection, string, predicate, Qt);
525
526         allmatches = Qnil;
527         slength = XSTRING_CHAR_LENGTH(string);
528
529         /* If COLLECTION is not a list, set TAIL just for gc pro.  */
530         tail = collection;
531         if (!list) {
532                 obsize = XVECTOR_LENGTH(collection);
533                 bucket = XVECTOR_DATA(collection)[indice];
534         } else {                /* warning suppression */
535
536                 obsize = 0;
537                 bucket = Qnil;
538         }
539
540         while (1) {
541                 /* Get the next element of the alist or obarray. */
542                 /* Exit the loop if the elements are all used up. */
543                 /* elt gets the alist element or symbol.
544                    eltstring gets the name to check as a completion. */
545                 Lisp_Object elt;
546                 Lisp_Object eltstring;
547
548                 if (list) {
549                         if (NILP(tail))
550                                 break;
551                         elt = Fcar(tail);
552                         eltstring = Fcar(elt);
553                         tail = Fcdr(tail);
554                 } else {
555                         if (!ZEROP(bucket)) {
556                                 Lisp_Symbol *next =
557                                     symbol_next(XSYMBOL(bucket));
558                                 elt = bucket;
559                                 eltstring = Fsymbol_name(elt);
560                                 if (next)
561                                         XSETSYMBOL(bucket, next);
562                                 else
563                                         bucket = Qzero;
564                         } else if (++indice >= obsize)
565                                 break;
566                         else {
567                                 bucket = XVECTOR_DATA(collection)[indice];
568                                 continue;
569                         }
570                 }
571
572                 /* Is this element a possible completion? */
573
574                 if (STRINGP(eltstring)
575                     && (slength <= XSTRING_CHAR_LENGTH(eltstring))
576                     /* Reject alternatives that start with space
577                        unless the input starts with space.  */
578                     && ((XSTRING_CHAR_LENGTH(string) > 0 &&
579                          string_char(XSTRING(string), 0) == ' ')
580                         || string_char(XSTRING(eltstring), 0) != ' ')
581                     && (0 > scmp(XSTRING_DATA(eltstring),
582                                  XSTRING_DATA(string), slength))) {
583                         /* Yes.  Now check whether predicate likes it. */
584                         struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
585                         int loser;
586                         GCPRO4(tail, eltstring, allmatches, string);
587                         loser = ignore_completion_p(eltstring, predicate, elt);
588                         UNGCPRO;
589                         if (!loser)
590                                 /* Ok => put it on the list. */
591                                 allmatches = Fcons(eltstring, allmatches);
592                 }
593         }
594
595         return Fnreverse(allmatches);
596 }
597 \f
598 /* Useless FSFmacs functions */
599 /* More than useless.  I've nuked minibuf_prompt_width so they won't
600    function at all in XEmacs at the moment.  They are used to
601    implement some braindamage in FSF which we aren't including. --cet */
602
603 #if 0
604 xxDEFUN("minibuffer-prompt", Fminibuffer_prompt, 0, 0, 0,       /*
605 Return the prompt string of the currently-active minibuffer.
606 If no minibuffer is active, return nil.
607                                                                  */
608         ())
609 {
610         return Fcopy_sequence(Vminibuf_prompt);
611 }
612
613 xxDEFUN("minibuffer-prompt-width", Fminibuffer_prompt_width, 0, 0, 0,   /*
614 Return the display width of the minibuffer prompt.
615                                                                          */
616         ())
617 {
618         return make_int(minibuf_prompt_width);
619 }
620 #endif                          /* 0 */
621 \f
622 /************************************************************************/
623 /*                              echo area                               */
624 /************************************************************************/
625
626 extern int stdout_needs_newline;
627
628 static Lisp_Object
629 clear_echo_area_internal(struct frame *f, Lisp_Object label, int from_print,
630                          int no_restore)
631 {
632         /* This function can call lisp */
633         if (!NILP(Ffboundp(Qclear_message))) {
634                 Lisp_Object frame;
635
636                 XSETFRAME(frame, f);
637                 return call4(Qclear_message, label, frame,
638                              from_print ? Qt : Qnil, no_restore ? Qt : Qnil);
639         } else {
640                 write_string_to_stdio_stream(stderr, 0, (const Bufbyte *)"\n",
641                                              0, 1, Qterminal, 0);
642                 return Qnil;
643         }
644 }
645
646 Lisp_Object clear_echo_area(struct frame * f, Lisp_Object label, int no_restore)
647 {
648         /* This function can call lisp */
649         return clear_echo_area_internal(f, label, 0, no_restore);
650 }
651
652 Lisp_Object
653 clear_echo_area_from_print(struct frame * f, Lisp_Object label, int no_restore)
654 {
655         /* This function can call lisp */
656         return clear_echo_area_internal(f, label, 1, no_restore);
657 }
658
659 void
660 echo_area_append(struct frame *f, const Bufbyte * nonreloc, Lisp_Object reloc,
661                  Bytecount offset, Bytecount length, Lisp_Object label)
662 {
663         /* This function can call lisp */
664         Lisp_Object obj = Qnil;
665         struct gcpro gcpro1;
666         Lisp_Object frame;
667
668         /* There is an inlining bug in egcs-20000131 c++ that can be worked
669            around as follows:  */
670 #if defined (__GNUC__) && defined (__cplusplus)
671         alloca(4);
672 #endif
673
674         /* some callers pass in a null string as a way of clearing the echo area.
675            check for length == 0 now; if this case, neither nonreloc nor reloc
676            may be valid.  */
677         if (length == 0)
678                 return;
679
680         fixup_internal_substring(nonreloc, reloc, offset, &length);
681
682         /* also check it here, in case the string was really blank. */
683         if (length == 0)
684                 return;
685
686         if (!NILP(Ffboundp(Qappend_message))) {
687                 GCPRO1(obj);
688                 if (STRINGP(reloc) && offset == 0
689                     && length == XSTRING_LENGTH(reloc))
690                         obj = reloc;
691                 else {
692                         if (STRINGP(reloc))
693                                 nonreloc = XSTRING_DATA(reloc);
694                         obj = make_string(nonreloc + offset, length);
695                 }
696
697                 XSETFRAME(frame, f);
698                 call4(Qappend_message, label, obj, frame,
699                       EQ(label, Qprint) ? Qt : Qnil);
700                 UNGCPRO;
701         } else {
702                 if (STRINGP(reloc))
703                         nonreloc = XSTRING_DATA(reloc);
704                 write_string_to_stdio_stream(stderr, 0, nonreloc, offset,
705                                              length, Qterminal, 0);
706         }
707 }
708
709 void
710 echo_area_message(struct frame *f, const Bufbyte * nonreloc,
711                   Lisp_Object reloc, Bytecount offset, Bytecount length,
712                   Lisp_Object label)
713 {
714         /* This function can call lisp */
715         clear_echo_area(f, label, 1);
716         echo_area_append(f, nonreloc, reloc, offset, length, label);
717 }
718
719 int echo_area_active(struct frame *f)
720 {
721         /* By definition, the echo area is active if the echo-area buffer
722            is not empty.  No need to call Lisp code. (Anyway, this function
723            is called from redisplay.) */
724         struct buffer *echo_buffer = XBUFFER(Vecho_area_buffer);
725         return BUF_BEGV(echo_buffer) != BUF_ZV(echo_buffer);
726 }
727
728 Lisp_Object echo_area_status(struct frame * f)
729 {
730         /* This function can call lisp */
731         if (!NILP(Ffboundp(Qcurrent_message_label))) {
732                 Lisp_Object frame;
733
734                 XSETFRAME(frame, f);
735                 return call1(Qcurrent_message_label, frame);
736         } else
737                 return stdout_needs_newline ? Qmessage : Qnil;
738 }
739
740 Lisp_Object echo_area_contents(struct frame * f)
741 {
742         /* See above.  By definition, the contents of the echo-area buffer
743            are the contents of the echo area. */
744         return Fbuffer_substring(Qnil, Qnil, Vecho_area_buffer);
745 }
746
747 /* Dump an informative message to the echo area.  This function takes a
748    string in internal format. */
749 void
750 message_internal(const Bufbyte * nonreloc, Lisp_Object reloc,
751                  Bytecount offset, Bytecount length)
752 {
753         /* This function can call lisp  */
754         if (NILP(Vexecuting_macro))
755                 echo_area_message(selected_frame(), nonreloc, reloc, offset,
756                                   length, Qmessage);
757 }
758
759 void
760 message_append_internal(const Bufbyte * nonreloc, Lisp_Object reloc,
761                         Bytecount offset, Bytecount length)
762 {
763         /* This function can call lisp  */
764         if (NILP(Vexecuting_macro))
765                 echo_area_append(selected_frame(), nonreloc, reloc, offset,
766                                  length, Qmessage);
767 }
768
769 /* The next three functions are interfaces to message_internal() that
770    take strings in external format.  message() does I18N3 translating
771    on the format string; message_no_translate() does not. */
772
773 static void message_1(const char *fmt, va_list args)
774 {
775 /* MT-safe */
776         /* This function can call lisp */
777         if (fmt) {
778                 struct gcpro gcpro1;
779                 /* message_internal() might GC, e.g. if there are
780                    after-change-hooks on the echo area buffer */
781                 Lisp_Object obj = Qnil;
782
783                 GCPRO1(obj);
784                 obj = emacs_doprnt_string_va(
785                         (const Bufbyte *)fmt, Qnil, -1, args);
786                 message_internal(0, obj, 0, -1);
787                 UNGCPRO;
788         } else
789                 message_internal(0, Qnil, 0, 0);
790 }
791
792 static void message_append_1(const char *fmt, va_list args)
793 {
794         /* This function can call lisp */
795         if (fmt) {
796                 struct gcpro gcpro1;
797                 /* message_internal() might GC, e.g. if there are after-change-hooks
798                    on the echo area buffer */
799                 Lisp_Object obj =
800                     emacs_doprnt_string_va((const Bufbyte *)fmt, Qnil,
801                                            -1, args);
802                 GCPRO1(obj);
803                 message_append_internal(0, obj, 0, -1);
804                 UNGCPRO;
805         } else
806                 message_append_internal(0, Qnil, 0, 0);
807 }
808
809 void clear_message(void)
810 {
811         /* This function can call lisp */
812         message_internal(0, Qnil, 0, 0);
813 }
814
815 void message(const char *fmt, ...)
816 {
817         /* This function can call lisp */
818         /* I think it's OK to pass the data of Lisp strings as arguments to
819            this function.  No GC'ing will occur until the data has already
820            been copied. */
821         va_list args;
822
823         va_start(args, fmt);
824         if (fmt)
825                 fmt = GETTEXT(fmt);
826         message_1(fmt, args);
827         va_end(args);
828 }
829
830 void message_append(const char *fmt, ...)
831 {
832         /* This function can call lisp */
833         va_list args;
834
835         va_start(args, fmt);
836         if (fmt)
837                 fmt = GETTEXT(fmt);
838         message_append_1(fmt, args);
839         va_end(args);
840 }
841
842 void message_no_translate(const char *fmt, ...)
843 {
844         /* This function can call lisp */
845         /* I think it's OK to pass the data of Lisp strings as arguments to
846            this function.  No GC'ing will occur until the data has already
847            been copied. */
848         va_list args;
849
850         va_start(args, fmt);
851         message_1(fmt, args);
852         va_end(args);
853 }
854 \f
855 /************************************************************************/
856 /*                            initialization                            */
857 /************************************************************************/
858
859 void syms_of_minibuf(void)
860 {
861         defsymbol(&Qminibuffer_setup_hook, "minibuffer-setup-hook");
862
863         defsymbol(&Qcompletion_ignore_case, "completion-ignore-case");
864
865         DEFSUBR(Fminibuffer_depth);
866 #if 0
867         DEFSUBR(Fminibuffer_prompt);
868         DEFSUBR(Fminibuffer_prompt_width);
869 #endif
870         DEFSUBR(Fset_minibuffer_preprompt);
871         DEFSUBR(Fread_minibuffer_internal);
872
873         DEFSUBR(Ftry_completion);
874         DEFSUBR(Fall_completions);
875
876         defsymbol(&Qappend_message, "append-message");
877         defsymbol(&Qclear_message, "clear-message");
878         defsymbol(&Qdisplay_message, "display-message");
879         defsymbol(&Qcurrent_message_label, "current-message-label");
880 }
881
882 void reinit_vars_of_minibuf(void)
883 {
884         minibuf_level = 0;
885 }
886
887 void vars_of_minibuf(void)
888 {
889         reinit_vars_of_minibuf();
890
891         staticpro(&Vminibuf_prompt);
892         Vminibuf_prompt = Qnil;
893
894         /* Added by Jareth Hein (jhod@po.iijnet.or.jp) for input system support */
895         staticpro(&Vminibuf_preprompt);
896         Vminibuf_preprompt = Qnil;
897
898         DEFVAR_LISP("minibuffer-setup-hook", &Vminibuffer_setup_hook    /*
899 Normal hook run just after entry to minibuffer.
900                                                                          */ );
901         Vminibuffer_setup_hook = Qnil;
902
903         DEFVAR_BOOL("completion-ignore-case", &completion_ignore_case   /*
904 Non-nil means don't consider case significant in completion.
905                                                                          */ );
906         completion_ignore_case = 0;
907
908         DEFVAR_LISP("completion-regexp-list", &Vcompletion_regexp_list  /*
909 List of regexps that should restrict possible completions.
910 Each completion has to match all regexps in this list.
911                                                                          */ );
912         Vcompletion_regexp_list = Qnil;
913 }
914
915 void reinit_complex_vars_of_minibuf(void)
916 {
917         /* This function can GC */
918 #ifdef I18N3
919         /* #### This needs to be fixed up so that the gettext() gets called
920            at runtime instead of at load time. */
921 #endif
922         Vminibuffer_zero
923             = Fget_buffer_create(build_string(DEFER_GETTEXT(" *Minibuf-0*")));
924         Vecho_area_buffer
925             = Fget_buffer_create(build_string(DEFER_GETTEXT(" *Echo Area*")));
926 }
927
928 void complex_vars_of_minibuf(void)
929 {
930         reinit_complex_vars_of_minibuf();
931 }