root/src/minibuf.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. minibuf_follows_frame
  2. minibuf_stays_put
  3. minibuf_moves_frame_when_opened
  4. choose_minibuf_frame
  5. minibuffer_ent_greater
  6. zip_minibuffer_stacks
  7. move_minibuffers_onto_frame
  8. DEFUN
  9. DEFUN
  10. string_to_object
  11. read_minibuf_noninteractive
  12. live_minibuffer_p
  13. DEFUN
  14. DEFUN
  15. this_minibuffer_depth
  16. DEFUN
  17. DEFUN
  18. DEFUN
  19. DEFUN
  20. read_minibuf
  21. is_minibuffer
  22. nth_minibuffer
  23. set_minibuffer_mode
  24. get_minibuffer
  25. minibuf_c_loop_level
  26. run_exit_minibuf_hook
  27. read_minibuf_unwind
  28. minibuffer_unwind
  29. barf_if_interaction_inhibited
  30. DEFUN
  31. match_regexps
  32. DEFUN
  33. DEFUN
  34. set_initial_minibuffer_mode
  35. init_minibuf_once
  36. init_minibuf_once_for_pdumper
  37. syms_of_minibuf

     1 /* Minibuffer input and completion.
     2 
     3 Copyright (C) 1985-1986, 1993-2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs 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 (at
    10 your option) any later version.
    11 
    12 GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 
    21 #include <config.h>
    22 #include <errno.h>
    23 
    24 #include <binary-io.h>
    25 
    26 #include "lisp.h"
    27 #include "character.h"
    28 #include "buffer.h"
    29 #include "keyboard.h"
    30 #include "frame.h"
    31 #include "window.h"
    32 #include "keymap.h"
    33 #include "sysstdio.h"
    34 #include "systty.h"
    35 #include "pdumper.h"
    36 
    37 #ifdef HAVE_NTGUI
    38 #include "w32term.h"
    39 #endif
    40 
    41 /* List of buffers for use as minibuffers.
    42    The first element of the list is used for the outermost minibuffer
    43    invocation, the next element is used for a recursive minibuffer
    44    invocation, etc.  The list is extended at the end as deeper
    45    minibuffer recursions are encountered.  */
    46 
    47 Lisp_Object Vminibuffer_list;
    48 static Lisp_Object Vcommand_loop_level_list;
    49 
    50 /* Data to remember during recursive minibuffer invocations.  */
    51 
    52 static Lisp_Object minibuf_save_list;
    53 
    54 /* Depth in minibuffer invocations.  */
    55 
    56 EMACS_INT minibuf_level;
    57 
    58 /* Fread_minibuffer leaves the input here as a string.  */
    59 
    60 Lisp_Object last_minibuf_string;
    61 
    62 /* Prompt to display in front of the mini-buffer contents.  */
    63 
    64 static Lisp_Object minibuf_prompt;
    65 
    66 /* The frame containinug the most recently opened Minibuffer.  This is
    67    used only when `minibuffer-follows-selected-frame' is neither nil
    68    nor t.  */
    69 
    70 static Lisp_Object MB_frame;
    71 
    72 /* Width of current mini-buffer prompt.  Only set after display_line
    73    of the line that contains the prompt.  */
    74 
    75 static ptrdiff_t minibuf_prompt_width;
    76 
    77 static Lisp_Object nth_minibuffer (EMACS_INT depth);
    78 static EMACS_INT minibuf_c_loop_level (EMACS_INT depth);
    79 static void set_minibuffer_mode (Lisp_Object buf, EMACS_INT depth);
    80 static bool live_minibuffer_p (Lisp_Object);
    81 
    82 
    83 /* Return TRUE when a frame switch causes a minibuffer on the old
    84    frame to move onto the new one. */
    85 static bool
    86 minibuf_follows_frame (void)
    87 {
    88   return EQ (Fdefault_toplevel_value (Qminibuffer_follows_selected_frame),
    89              Qt);
    90 }
    91 
    92 #if 0
    93 /* Return TRUE when a minibuffer always remains on the frame where it
    94    was first invoked. */
    95 static bool
    96 minibuf_stays_put (void)
    97 {
    98   return NILP (Fdefault_toplevel_value (Qminibuffer_follows_selected_frame));
    99 }
   100 #endif
   101 
   102 /* Return TRUE when opening a (recursive) minibuffer causes
   103    minibuffers on other frames to move to the selected frame.  */
   104 static bool
   105 minibuf_moves_frame_when_opened (void)
   106 {
   107   return !NILP (Fdefault_toplevel_value (Qminibuffer_follows_selected_frame));
   108 }
   109 
   110 /* Put minibuf on currently selected frame's minibuffer.
   111    We do this whenever the user starts a new minibuffer
   112    or when a minibuffer exits.  */
   113 
   114 static void
   115 choose_minibuf_frame (void)
   116 {
   117   if (FRAMEP (selected_frame)
   118       && FRAME_LIVE_P (XFRAME (selected_frame))
   119       && WINDOW_LIVE_P (XFRAME (selected_frame)->minibuffer_window)
   120       && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
   121     {
   122       struct frame *sf = XFRAME (selected_frame);
   123       /* I don't think that any frames may validly have a null
   124          minibuffer window anymore.  (2021-04-15): Tooltip frames have
   125          a null MB.  Comment out the following.  */
   126       /* if (NILP (sf->minibuffer_window)) */
   127       /*        emacs_abort (); */
   128 
   129       minibuf_window = sf->minibuffer_window;
   130     }
   131 }
   132 
   133 /* If ENT1 has a higher minibuffer index than ENT2, return true.  More
   134 precisely, compare the buffer components of each window->prev_buffers
   135 entry.  */
   136 static bool
   137 minibuffer_ent_greater (Lisp_Object ent1, Lisp_Object ent2)
   138 {
   139   return this_minibuffer_depth (Fcar (ent1))
   140     > this_minibuffer_depth (Fcar (ent2)) ;
   141 }
   142 
   143 /* Move the ordered "stack" of minibuffers from SOURCE_WINDOW to
   144    DEST_WINDOW, interleaving those minibuffers with any in DEST_WINDOW
   145    to produce an ordered combination.  The ordering is by minibuffer
   146    depth.  A stack of minibuffers consists of the minibuffer currently
   147    in DEST/SOURCE_WINDOW together with any recorded in the
   148    ->prev_buffers field of the struct window.  */
   149 static void
   150 zip_minibuffer_stacks (Lisp_Object dest_window, Lisp_Object source_window)
   151 {
   152   struct window *dw = XWINDOW (dest_window);
   153   struct window *sw = XWINDOW (source_window);
   154   Lisp_Object acc;
   155   Lisp_Object d_ent;    /* Entry from dw->prev_buffers */
   156 
   157   if (!live_minibuffer_p (dw->contents)
   158       && NILP (dw->prev_buffers))
   159     {
   160       set_window_buffer (dest_window, sw->contents, 0, 0);
   161       Fset_window_start (dest_window, Fwindow_start (source_window), Qnil);
   162       Fset_window_point (dest_window, Fwindow_point (source_window));
   163       dw->prev_buffers = sw->prev_buffers;
   164       set_window_buffer (source_window, nth_minibuffer (0), 0, 0);
   165       sw->prev_buffers = Qnil;
   166       return;
   167     }
   168 
   169   if (live_minibuffer_p (dw->contents))
   170     call1 (Qpush_window_buffer_onto_prev, dest_window);
   171   if (live_minibuffer_p (sw->contents))
   172     call1 (Qpush_window_buffer_onto_prev, source_window);
   173   acc = merge_c (dw->prev_buffers, sw->prev_buffers, minibuffer_ent_greater);
   174 
   175   if (!NILP (acc))
   176     {
   177       d_ent = Fcar (acc);
   178       acc = Fcdr (acc);
   179       set_window_buffer (dest_window, Fcar (d_ent), 0, 0);
   180       Fset_window_start (dest_window, Fcar (Fcdr (d_ent)), Qnil);
   181       Fset_window_point (dest_window, Fcar (Fcdr (Fcdr (d_ent))));
   182     }
   183   dw->prev_buffers = acc;
   184   sw->prev_buffers = Qnil;
   185   set_window_buffer (source_window, nth_minibuffer (0), 0, 0);
   186 }
   187 
   188 /* If `minibuffer_follows_selected_frame' is t, or we're about to
   189    delete a frame which potentially "contains" minibuffers, move them
   190    from the old frame to the to-be-selected frame.  This function is
   191    intended to be called from `do_switch_frame' in frame.c.  OF is the
   192    old frame, FRAME is the to-be-selected frame, and FOR_DELETION is true
   193    if OF is about to be deleted.  */
   194 void
   195 move_minibuffers_onto_frame (struct frame *of, Lisp_Object frame,
   196                              bool for_deletion)
   197 {
   198   struct frame *f = XFRAME (frame);
   199 
   200   minibuf_window = f->minibuffer_window;
   201   if (!(minibuf_level
   202         && (for_deletion || minibuf_follows_frame () || FRAME_INITIAL_P (of))))
   203     return;
   204   if (FRAME_LIVE_P (f)
   205       && !EQ (f->minibuffer_window, of->minibuffer_window)
   206       && WINDOW_LIVE_P (f->minibuffer_window) /* F not a tooltip frame */
   207       && WINDOW_LIVE_P (of->minibuffer_window))
   208     {
   209       zip_minibuffer_stacks (f->minibuffer_window, of->minibuffer_window);
   210       if (for_deletion && XFRAME (MB_frame) != of)
   211         MB_frame = frame;
   212     }
   213 }
   214 
   215 DEFUN ("active-minibuffer-window", Factive_minibuffer_window,
   216        Sactive_minibuffer_window, 0, 0, 0,
   217        doc: /* Return the currently active minibuffer window, or nil if none.  */)
   218      (void)
   219 {
   220   Lisp_Object frames, frame;
   221   struct frame *f;
   222   Lisp_Object innermost_MB;
   223 
   224   if (!minibuf_level)
   225     return Qnil;
   226 
   227   innermost_MB = nth_minibuffer (minibuf_level);
   228   if (NILP (innermost_MB))
   229     emacs_abort ();
   230   FOR_EACH_FRAME (frames, frame)
   231     {
   232       f = XFRAME (frame);
   233       if (FRAME_LIVE_P (f)
   234           && WINDOW_LIVE_P (f->minibuffer_window)
   235           && EQ (XWINDOW (f->minibuffer_window)->contents, innermost_MB))
   236         return f->minibuffer_window;
   237     }
   238   return minibuf_window;        /* "Can't happen." */
   239 }
   240 
   241 DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
   242        Sset_minibuffer_window, 1, 1, 0,
   243        doc: /* Specify which minibuffer window to use for the minibuffer.
   244 This affects where the minibuffer is displayed if you put text in it
   245 without invoking the usual minibuffer commands.  */)
   246   (Lisp_Object window)
   247 {
   248   CHECK_WINDOW (window);
   249   if (! MINI_WINDOW_P (XWINDOW (window)))
   250     error ("Window is not a minibuffer window");
   251 
   252   minibuf_window = window;
   253 
   254   return window;
   255 }
   256 
   257 
   258 /* Actual minibuffer invocation.  */
   259 
   260 static void read_minibuf_unwind (void);
   261 static void minibuffer_unwind (void);
   262 static void run_exit_minibuf_hook (Lisp_Object minibuf);
   263 
   264 
   265 /* Read a Lisp object from VAL and return it.  If VAL is an empty
   266    string, and DEFALT is a string, read from DEFALT instead of VAL.  */
   267 
   268 static Lisp_Object
   269 string_to_object (Lisp_Object val, Lisp_Object defalt)
   270 {
   271   Lisp_Object expr_and_pos;
   272   ptrdiff_t pos;
   273 
   274   if (STRINGP (val) && SCHARS (val) == 0)
   275     {
   276       if (STRINGP (defalt))
   277         val = defalt;
   278       else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
   279         val = XCAR (defalt);
   280     }
   281 
   282   expr_and_pos = Fread_from_string (val, Qnil, Qnil);
   283   pos = XFIXNUM (Fcdr (expr_and_pos));
   284   if (pos != SCHARS (val))
   285     {
   286       /* Ignore trailing whitespace; any other trailing junk
   287          is an error.  */
   288       ptrdiff_t i;
   289       pos = string_char_to_byte (val, pos);
   290       for (i = pos; i < SBYTES (val); i++)
   291         {
   292           int c = SREF (val, i);
   293           if (c != ' ' && c != '\t' && c != '\n')
   294             xsignal1 (Qinvalid_read_syntax,
   295                       build_string ("Trailing garbage following expression"));
   296         }
   297     }
   298 
   299   val = Fcar (expr_and_pos);
   300   return val;
   301 }
   302 
   303 
   304 /* Like read_minibuf but reading from stdin.  This function is called
   305    from read_minibuf to do the job if noninteractive.  */
   306 
   307 static Lisp_Object
   308 read_minibuf_noninteractive (Lisp_Object prompt, bool expflag,
   309                              Lisp_Object defalt)
   310 {
   311   ptrdiff_t size, len;
   312   char *line;
   313   Lisp_Object val;
   314   int c;
   315   unsigned char hide_char = 0;
   316   struct emacs_tty etty;
   317   bool etty_valid UNINIT;
   318 
   319   /* Check, whether we need to suppress echoing.  */
   320   if (CHARACTERP (Vread_hide_char))
   321     hide_char = XFIXNAT (Vread_hide_char);
   322 
   323   /* Manipulate tty.  */
   324   if (hide_char)
   325     {
   326       etty_valid = emacs_get_tty (STDIN_FILENO, &etty) == 0;
   327       if (etty_valid)
   328         set_binary_mode (STDIN_FILENO, O_BINARY);
   329       suppress_echo_on_tty (STDIN_FILENO);
   330     }
   331 
   332   fwrite (SDATA (prompt), 1, SBYTES (prompt), stdout);
   333   fflush (stdout);
   334 
   335   val = Qnil;
   336   size = 100;
   337   len = 0;
   338   line = xmalloc (size);
   339 
   340   while ((c = getchar ()) != '\n' && c != '\r')
   341     {
   342       if (c == EOF)
   343         {
   344           if (errno != EINTR)
   345             break;
   346         }
   347       else
   348         {
   349           if (hide_char)
   350             putchar (hide_char);
   351           if (len == size)
   352             line = xpalloc (line, &size, 1, -1, sizeof *line);
   353           line[len++] = c;
   354         }
   355     }
   356 
   357   /* Reset tty.  */
   358   if (hide_char)
   359     {
   360       putc ('\n', stdout);
   361       if (etty_valid)
   362         {
   363           emacs_set_tty (STDIN_FILENO, &etty, 0);
   364           set_binary_mode (STDIN_FILENO, O_TEXT);
   365         }
   366     }
   367 
   368   if (len || c == '\n' || c == '\r')
   369     {
   370       val = make_string (line, len);
   371       xfree (line);
   372     }
   373   else
   374     {
   375       xfree (line);
   376       xsignal1 (Qend_of_file, build_string ("Error reading from stdin"));
   377     }
   378 
   379   /* If Lisp form desired instead of string, parse it.  */
   380   if (expflag)
   381     val = string_to_object (val, CONSP (defalt) ? XCAR (defalt) : defalt);
   382 
   383   return val;
   384 }
   385 
   386 /* Return true when BUFFER is an active minibuffer.  */
   387 static bool
   388 live_minibuffer_p (Lisp_Object buffer)
   389 {
   390   Lisp_Object tem;
   391   EMACS_INT i;
   392 
   393   if (EQ (buffer, Fcar (Vminibuffer_list)))
   394     /*  *Minibuf-0* is never active.  */
   395     return false;
   396   tem = Fcdr (Vminibuffer_list);
   397   for (i = 1; i <= minibuf_level; i++, tem = Fcdr (tem))
   398     if (EQ (Fcar (tem), buffer))
   399       return true;
   400   return false;
   401 }
   402 
   403 DEFUN ("minibufferp", Fminibufferp,
   404        Sminibufferp, 0, 2, 0,
   405        doc: /* Return t if BUFFER is a minibuffer.
   406 No argument or nil as argument means use current buffer as BUFFER.
   407 BUFFER can be a buffer or a buffer name.  If LIVE is non-nil, then
   408 return t only if BUFFER is an active minibuffer.  */)
   409   (Lisp_Object buffer, Lisp_Object live)
   410 {
   411   if (NILP (buffer))
   412     buffer = Fcurrent_buffer ();
   413   else if (STRINGP (buffer))
   414     buffer = Fget_buffer (buffer);
   415   else
   416     CHECK_BUFFER (buffer);
   417 
   418   return (NILP (live)
   419           ? !NILP (Fmemq (buffer, Vminibuffer_list))
   420           : live_minibuffer_p (buffer))
   421     ? Qt : Qnil;
   422 }
   423 
   424 DEFUN ("innermost-minibuffer-p", Finnermost_minibuffer_p,
   425        Sinnermost_minibuffer_p, 0, 1, 0,
   426        doc: /* Return t if BUFFER is the most nested active minibuffer.
   427 No argument or nil as argument means use the current buffer as BUFFER.  */)
   428   (Lisp_Object buffer)
   429 {
   430   if (NILP (buffer))
   431     buffer = Fcurrent_buffer ();
   432   return BASE_EQ (buffer, (Fcar (Fnthcdr (make_fixnum (minibuf_level),
   433                                           Vminibuffer_list))))
   434     ? Qt
   435     : Qnil;
   436 }
   437 
   438 DEFUN ("minibuffer-innermost-command-loop-p", Fminibuffer_innermost_command_loop_p,
   439        Sminibuffer_innermost_command_loop_p, 0, 1, 0,
   440        doc: /* Return t if BUFFER is a minibuffer at the current command loop level.
   441 No argument or nil as argument means use the current buffer as BUFFER.  */)
   442   (Lisp_Object buffer)
   443 {
   444   EMACS_INT depth;
   445   if (NILP (buffer))
   446     buffer = Fcurrent_buffer ();
   447   depth = this_minibuffer_depth (buffer);
   448   return depth && minibuf_c_loop_level (depth) == command_loop_level
   449     ? Qt
   450     : Qnil;
   451 }
   452 
   453 /* Return the nesting depth of the active minibuffer BUFFER, or 0 if
   454    BUFFER isn't such a thing.  If BUFFER is nil, this means use the current
   455    buffer.  */
   456 EMACS_INT
   457 this_minibuffer_depth (Lisp_Object buffer)
   458 {
   459   EMACS_INT i;
   460   Lisp_Object bufs;
   461 
   462   if (NILP (buffer))
   463     buffer = Fcurrent_buffer ();
   464   for (i = 1, bufs = Fcdr (Vminibuffer_list);
   465        i <= minibuf_level;
   466        i++, bufs = Fcdr (bufs))
   467     if (EQ (Fcar (bufs), buffer))
   468       return i;
   469   return 0;
   470 }
   471 
   472 DEFUN ("abort-minibuffers", Fabort_minibuffers, Sabort_minibuffers, 0, 0, "",
   473        doc: /* Abort the current minibuffer.
   474 If we are not currently in the innermost minibuffer, prompt the user to
   475 confirm the aborting of the current minibuffer and all contained ones.  */)
   476   (void)
   477 {
   478   EMACS_INT minibuf_depth = this_minibuffer_depth (Qnil);
   479   Lisp_Object array[2];
   480   AUTO_STRING (fmt, "Abort %s minibuffer levels? ");
   481 
   482   if (!minibuf_depth)
   483     error ("Not in a minibuffer");
   484   if (NILP (Fminibuffer_innermost_command_loop_p (Qnil)))
   485     error ("Not in most nested command loop");
   486   if (minibuf_depth < minibuf_level)
   487     {
   488       array[0] = fmt;
   489       array[1] = make_fixnum (minibuf_level - minibuf_depth + 1);
   490       if (!NILP (Fyes_or_no_p (Fformat (2, array))))
   491         {
   492           /* Due to the above check, the current minibuffer is in the
   493              most nested command loop, which means that we don't have
   494              to abort any extra non-minibuffer recursive edits.  Thus,
   495              the number of recursive edits we have to abort equals the
   496              number of minibuffers we have to abort.  */
   497           CALLN (Ffuncall, intern ("minibuffer-quit-recursive-edit"),
   498                  array[1]);
   499         }
   500     }
   501   else
   502     CALLN (Ffuncall, intern ("minibuffer-quit-recursive-edit"));
   503   return Qnil;
   504 }
   505 
   506 DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
   507        Sminibuffer_prompt_end, 0, 0, 0,
   508        doc: /* Return the buffer position of the end of the minibuffer prompt.
   509 Return (point-min) if current buffer is not a minibuffer.  */)
   510   (void)
   511 {
   512   /* This function is written to be most efficient when there's a prompt.  */
   513   Lisp_Object beg, end, tem;
   514   beg = make_fixnum (BEGV);
   515 
   516   tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
   517   if (NILP (tem))
   518     return beg;
   519 
   520   end = Ffield_end (beg, Qnil, Qnil);
   521 
   522   if (XFIXNUM (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
   523     return beg;
   524   else
   525     return end;
   526 }
   527 
   528 DEFUN ("minibuffer-contents", Fminibuffer_contents,
   529        Sminibuffer_contents, 0, 0, 0,
   530        doc: /* Return the user input in a minibuffer as a string.
   531 If the current buffer is not a minibuffer, return its entire contents.  */)
   532   (void)
   533 {
   534   ptrdiff_t prompt_end = XFIXNUM (Fminibuffer_prompt_end ());
   535   return make_buffer_string (prompt_end, ZV, 1);
   536 }
   537 
   538 DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
   539        Sminibuffer_contents_no_properties, 0, 0, 0,
   540        doc: /* Return the user input in a minibuffer as a string, without text-properties.
   541 If the current buffer is not a minibuffer, return its entire contents.  */)
   542   (void)
   543 {
   544   ptrdiff_t prompt_end = XFIXNUM (Fminibuffer_prompt_end ());
   545   return make_buffer_string (prompt_end, ZV, 0);
   546 }
   547 
   548 
   549 /* Read from the minibuffer using keymap MAP and initial contents INITIAL,
   550    putting point minus BACKUP_N bytes from the end of INITIAL,
   551    prompting with PROMPT (a string), using history list HISTVAR
   552    with initial position HISTPOS.  INITIAL should be a string or a
   553    cons of a string and an integer.  BACKUP_N should be <= 0, or
   554    Qnil, which is equivalent to 0.  If INITIAL is a cons, BACKUP_N is
   555    ignored and replaced with an integer that puts point at one-indexed
   556    position N in INITIAL, where N is the CDR of INITIAL, or at the
   557    beginning of INITIAL if N <= 0.
   558 
   559    Normally return the result as a string (the text that was read),
   560    but if EXPFLAG, read it and return the object read.
   561    If HISTVAR is given, save the value read on that history only if it doesn't
   562    match the front of that history list exactly.  The value is pushed onto
   563    the list as the string that was read.
   564 
   565    DEFALT specifies the default value for the sake of history commands.
   566 
   567    If ALLOW_PROPS, do not throw away text properties.
   568 
   569    if INHERIT_INPUT_METHOD, the minibuffer inherits the
   570    current input method.  */
   571 
   572 static Lisp_Object
   573 read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
   574               bool expflag,
   575               Lisp_Object histvar, Lisp_Object histpos, Lisp_Object defalt,
   576               bool allow_props, bool inherit_input_method)
   577 {
   578   Lisp_Object val;
   579   specpdl_ref count = SPECPDL_INDEX ();
   580   Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
   581   Lisp_Object calling_frame = selected_frame;
   582   Lisp_Object calling_window = selected_window;
   583   Lisp_Object enable_multibyte;
   584   EMACS_INT pos = 0;
   585   /* String to add to the history.  */
   586   Lisp_Object histstring;
   587   Lisp_Object histval;
   588 
   589   Lisp_Object empty_minibuf;
   590 
   591   specbind (Qminibuffer_default, defalt);
   592   specbind (Qinhibit_read_only, Qnil);
   593 
   594   /* If Vminibuffer_completing_file_name is `lambda' on entry, it was t
   595      in previous recursive minibuffer, but was not set explicitly
   596      to t for this invocation, so set it to nil in this minibuffer.
   597      Save the old value now, before we change it.  */
   598   specbind (Qminibuffer_completing_file_name,
   599             Vminibuffer_completing_file_name);
   600   if (EQ (Vminibuffer_completing_file_name, Qlambda))
   601     Vminibuffer_completing_file_name = Qnil;
   602 
   603 #ifdef HAVE_WINDOW_SYSTEM
   604   if (display_hourglass_p)
   605     cancel_hourglass ();
   606 #endif
   607 
   608   if (!NILP (initial))
   609     {
   610       if (CONSP (initial))
   611         {
   612           Lisp_Object backup_n = XCDR (initial);
   613           initial = XCAR (initial);
   614           CHECK_STRING (initial);
   615           if (!NILP (backup_n))
   616             {
   617               CHECK_FIXNUM (backup_n);
   618               /* Convert to distance from end of input.  */
   619               if (XFIXNUM (backup_n) < 1)
   620                 /* A number too small means the beginning of the string.  */
   621                 pos =  - SCHARS (initial);
   622               else
   623                 pos = XFIXNUM (backup_n) - 1 - SCHARS (initial);
   624             }
   625         }
   626       else
   627         CHECK_STRING (initial);
   628     }
   629   val = Qnil;
   630   ambient_dir = BVAR (current_buffer, directory);
   631   input_method = Qnil;
   632   enable_multibyte = Qnil;
   633 
   634   if (!STRINGP (prompt))
   635     prompt = empty_unibyte_string;
   636 
   637   if (!enable_recursive_minibuffers
   638       && minibuf_level > 0)
   639     {
   640       Lisp_Object str
   641         = build_string ("Command attempted to use minibuffer while in minibuffer");
   642       if (EQ (selected_window, minibuf_window))
   643         Fsignal (Quser_error, (list1 (str)));
   644       else
   645         /* If we're in another window, cancel the minibuffer that's active.  */
   646         Fthrow (Qexit, str);
   647     }
   648 
   649   if ((noninteractive
   650        /* In case we are running as a daemon, only do this before
   651           detaching from the terminal.  */
   652        || (IS_DAEMON && DAEMON_RUNNING))
   653       && NILP (Vexecuting_kbd_macro))
   654     {
   655       val = read_minibuf_noninteractive (prompt, expflag, defalt);
   656       return unbind_to (count, val);
   657     }
   658 
   659   /* Ensure now that the latest minibuffer has been created and pushed
   660      onto Vminibuffer_list before incrementing minibuf_level, in case
   661      a hook called during the minibuffer creation calls
   662      Factive_minibuffer_window.  */
   663   minibuffer = get_minibuffer (minibuf_level + 1);
   664   minibuf_level++;              /* Before calling choose_minibuf_frame.  */
   665 
   666   /* Choose the minibuffer window and frame, and take action on them.  */
   667 
   668   /* Prepare for restoring the current buffer since choose_minibuf_frame
   669      calling Fset_frame_selected_window may change it (Bug#12766).  */
   670   record_unwind_protect (restore_buffer, Fcurrent_buffer ());
   671 
   672   choose_minibuf_frame ();
   673   mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
   674 
   675   if (minibuf_level > 1
   676       && WINDOW_LIVE_P (XFRAME (MB_frame)->minibuffer_window)
   677       && !EQ (XWINDOW (XFRAME (selected_frame)->minibuffer_window)->frame,
   678               MB_frame)
   679       && minibuf_moves_frame_when_opened ()
   680       && (!minibuf_follows_frame ()))
   681     {
   682       struct frame *of = XFRAME (MB_frame);
   683 
   684       zip_minibuffer_stacks (minibuf_window, of->minibuffer_window);
   685       /* MB_frame's minibuffer can be on a different frame.  */
   686       if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (of))))
   687         Fset_frame_selected_window (MB_frame,
   688                                     Fframe_first_window (MB_frame), Qnil);
   689     }
   690   MB_frame = XWINDOW (XFRAME (selected_frame)->minibuffer_window)->frame;
   691   if (live_minibuffer_p (XWINDOW (minibuf_window)->contents))
   692     call1 (Qpush_window_buffer_onto_prev, minibuf_window);
   693 
   694   record_unwind_protect_void (minibuffer_unwind);
   695   if (read_minibuffer_restore_windows)
   696     record_unwind_protect (restore_window_configuration,
   697                            list3 (Fcurrent_window_configuration (Qnil),
   698                                   Qt, Qt));
   699 
   700   /* If the minibuffer window is on a different frame, save that
   701      frame's configuration too.  */
   702   if (read_minibuffer_restore_windows &&
   703       !EQ (mini_frame, selected_frame))
   704     record_unwind_protect (restore_window_configuration,
   705                            list3 (Fcurrent_window_configuration (mini_frame),
   706                                   Qnil, Qt));
   707 
   708   /* If the minibuffer is on an iconified or invisible frame,
   709      make it visible now.  */
   710   Fmake_frame_visible (mini_frame);
   711 
   712   if (minibuffer_auto_raise)
   713     Fraise_frame (mini_frame);
   714 
   715   temporarily_switch_to_single_kboard (XFRAME (mini_frame));
   716 
   717   /* We have to do this after saving the window configuration
   718      since that is what restores the current buffer.  */
   719 
   720   /* Arrange to restore a number of minibuffer-related variables.
   721      We could bind each variable separately, but that would use lots of
   722      specpdl slots.  */
   723   minibuf_save_list
   724     = Fcons (Voverriding_local_map,
   725              Fcons (minibuf_window,
   726                     Fcons (calling_frame,
   727                            Fcons (calling_window,
   728                                   minibuf_save_list))));
   729   minibuf_save_list
   730     = Fcons (minibuf_prompt,
   731              Fcons (make_fixnum (minibuf_prompt_width),
   732                     Fcons (Vhelp_form,
   733                            Fcons (Vcurrent_prefix_arg,
   734                                   Fcons (Vminibuffer_history_position,
   735                                          Fcons (Vminibuffer_history_variable,
   736                                                 minibuf_save_list))))));
   737   minibuf_save_list
   738     = Fcons (Fthis_command_keys_vector (), minibuf_save_list);
   739 
   740   record_unwind_protect_void (read_minibuf_unwind);
   741   /* We are exiting the minibuffer one way or the other, so run the hook.
   742      It should be run before unwinding the minibuf settings.  Do it
   743      separately from read_minibuf_unwind because we need to make sure that
   744      read_minibuf_unwind is fully executed even if exit-minibuffer-hook
   745      signals an error.  --Stef  */
   746   record_unwind_protect (run_exit_minibuf_hook, minibuffer);
   747 
   748   /* Now that we can restore all those variables, start changing them.  */
   749 
   750   minibuf_prompt_width = 0;
   751   minibuf_prompt = Fcopy_sequence (prompt);
   752   Vminibuffer_history_position = histpos;
   753   Vminibuffer_history_variable = histvar;
   754   Vhelp_form = Vminibuffer_help_form;
   755   /* If this minibuffer is reading a file name, that doesn't mean
   756      recursive ones are.  But we cannot set it to nil, because
   757      completion code still need to know the minibuffer is completing a
   758      file name.  So use `lambda' as intermediate value meaning
   759      "t" in this minibuffer, but "nil" in next minibuffer.  */
   760   if (!NILP (Vminibuffer_completing_file_name))
   761     Vminibuffer_completing_file_name = Qlambda;
   762 
   763   /* If variable is unbound, make it nil.  */
   764   histval = find_symbol_value (histvar);
   765   if (BASE_EQ (histval, Qunbound))
   766     {
   767       Fset (histvar, Qnil);
   768       histval = Qnil;
   769     }
   770 
   771   if (inherit_input_method)
   772     {
   773       /* `current-input-method' is buffer local.  So, remember it in
   774          INPUT_METHOD before changing the current buffer.  */
   775       input_method = Fsymbol_value (Qcurrent_input_method);
   776       enable_multibyte = BVAR (current_buffer, enable_multibyte_characters);
   777     }
   778 
   779   /* Switch to the minibuffer.  */
   780 
   781   set_minibuffer_mode (minibuffer, minibuf_level);
   782   Fset_buffer (minibuffer);
   783 
   784   /* Defeat (setq-default truncate-lines t), since truncated lines do
   785      not work correctly in minibuffers.  (Bug#5715, etc)  */
   786   bset_truncate_lines (current_buffer, Qnil);
   787 
   788   /* The current buffer's default directory is usually the right thing
   789      for our minibuffer here.  However, if you're typing a command at
   790      a minibuffer-only frame when minibuf_level is zero, then buf IS
   791      the current_buffer, so reset_buffer leaves buf's default
   792      directory unchanged.  This is a bummer when you've just started
   793      up Emacs and buf's default directory is Qnil.  Here's a hack; can
   794      you think of something better to do?  Find another buffer with a
   795      better directory, and use that one instead.  */
   796   if (STRINGP (ambient_dir))
   797     bset_directory (current_buffer, ambient_dir);
   798   else
   799     {
   800       Lisp_Object tail, buf;
   801 
   802       FOR_EACH_LIVE_BUFFER (tail, buf)
   803         if (STRINGP (BVAR (XBUFFER (buf), directory)))
   804           {
   805             bset_directory (current_buffer,
   806                             BVAR (XBUFFER (buf), directory));
   807             break;
   808           }
   809     }
   810 
   811   if (!EQ (mini_frame, selected_frame))
   812     Fredirect_frame_focus (selected_frame, mini_frame);
   813 
   814   Vminibuf_scroll_window = selected_window;
   815   if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
   816     minibuf_selected_window = selected_window;
   817 
   818   /* Empty out the minibuffers of all frames, except those frames
   819      where there is an active minibuffer.
   820      Set them to point to ` *Minibuf-0*', which is always empty.  */
   821   empty_minibuf = nth_minibuffer (0);
   822   set_minibuffer_mode (empty_minibuf, 0);
   823 
   824   /* Display this minibuffer in the proper window.  */
   825   /* Use set_window_buffer instead of Fset_window_buffer (see
   826      discussion of bug#11984, bug#12025, bug#12026).  */
   827   set_window_buffer (minibuf_window, Fcurrent_buffer (), 0, 0);
   828   Fselect_window (minibuf_window, Qnil);
   829   XWINDOW (minibuf_window)->hscroll = 0;
   830   XWINDOW (minibuf_window)->suspend_auto_hscroll = 0;
   831 
   832   /* Erase the buffer.  */
   833   {
   834     specpdl_ref count1 = SPECPDL_INDEX ();
   835     specbind (Qinhibit_read_only, Qt);
   836     specbind (Qinhibit_modification_hooks, Qt);
   837     Ferase_buffer ();
   838 
   839     /* If appropriate, copy enable-multibyte-characters into the minibuffer.
   840        In any case don't blindly inherit the multibyteness used previously.  */
   841     bset_enable_multibyte_characters (current_buffer,
   842                                       inherit_input_method ? enable_multibyte
   843                                       : Qt);
   844 
   845     /* Insert the prompt, record where it ends.  */
   846     Finsert (1, &minibuf_prompt);
   847     if (PT > BEG)
   848       {
   849         Fput_text_property (make_fixnum (BEG), make_fixnum (PT),
   850                             Qfront_sticky, Qt, Qnil);
   851         Fput_text_property (make_fixnum (BEG), make_fixnum (PT),
   852                             Qrear_nonsticky, Qt, Qnil);
   853         Fput_text_property (make_fixnum (BEG), make_fixnum (PT),
   854                             Qfield, Qt, Qnil);
   855         if (CONSP (Vminibuffer_prompt_properties))
   856           {
   857             /* We want to apply all properties from
   858                `minibuffer-prompt-properties' to the region normally,
   859                but if the `face' property is present, add that
   860                property to the end of the face properties to avoid
   861                overwriting faces. */
   862             Lisp_Object list = Vminibuffer_prompt_properties;
   863             while (CONSP (list))
   864               {
   865                 Lisp_Object key = XCAR (list);
   866                 list = XCDR (list);
   867                 if (CONSP (list))
   868                   {
   869                     Lisp_Object val = XCAR (list);
   870                     list = XCDR (list);
   871                     if (EQ (key, Qface))
   872                       Fadd_face_text_property (make_fixnum (BEG),
   873                                                make_fixnum (PT), val, Qt, Qnil);
   874                     else
   875                       Fput_text_property (make_fixnum (BEG), make_fixnum (PT),
   876                                           key, val, Qnil);
   877                   }
   878               }
   879           }
   880       }
   881     unbind_to (count1, Qnil);
   882   }
   883 
   884   minibuf_prompt_width = current_column ();
   885 
   886   /* Put in the initial input.  */
   887   if (!NILP (initial))
   888     {
   889       Finsert (1, &initial);
   890       Fforward_char (make_fixnum (pos));
   891     }
   892 
   893   clear_message (1, 1);
   894   bset_keymap (current_buffer, map);
   895 
   896   /* Turn on an input method stored in INPUT_METHOD if any.  */
   897   if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
   898     call1 (Qactivate_input_method, input_method);
   899 
   900   run_hook (Qminibuffer_setup_hook);
   901 
   902   /* Don't allow the user to undo past this point.  */
   903   bset_undo_list (current_buffer, Qnil);
   904 
   905   recursive_edit_1 ();
   906 
   907   /* If cursor is on the minibuffer line,
   908      show the user we have exited by putting it in column 0.  */
   909   if (XWINDOW (minibuf_window)->cursor.vpos >= 0
   910       && !noninteractive
   911       && !FRAME_INITIAL_P (SELECTED_FRAME ()))
   912     {
   913       XWINDOW (minibuf_window)->cursor.hpos = 0;
   914       XWINDOW (minibuf_window)->cursor.x = 0;
   915       XWINDOW (minibuf_window)->must_be_updated_p = true;
   916       update_frame (XFRAME (selected_frame), true, true);
   917 #ifndef HAVE_NTGUI
   918       flush_frame (XFRAME (XWINDOW (minibuf_window)->frame));
   919 #else
   920       /* The reason this function isn't `flush_display' in the RIF is
   921          that `flush_frame' is also called in many other circumstances
   922          when some code wants X requests to be sent to the X server,
   923          but there is no corresponding "flush" concept on MS Windows,
   924          and flipping buffers every time `flush_frame' is called
   925          causes flicker.  */
   926       w32_flip_buffers_if_dirty (XFRAME (XWINDOW (minibuf_window)->frame));
   927 #endif
   928     }
   929 
   930   /* Make minibuffer contents into a string.  */
   931   Fset_buffer (minibuffer);
   932   if (allow_props)
   933     val = Fminibuffer_contents ();
   934   else
   935     val = Fminibuffer_contents_no_properties ();
   936 
   937   /* VAL is the string of minibuffer text.  */
   938 
   939   last_minibuf_string = val;
   940 
   941   /* Choose the string to add to the history.  */
   942   if (SCHARS (val) != 0)
   943     histstring = val;
   944   else if (STRINGP (defalt))
   945     histstring = defalt;
   946   else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
   947     histstring = XCAR (defalt);
   948   else
   949     histstring = Qnil;
   950 
   951   /* The appropriate frame will get selected
   952      in set-window-configuration.  */
   953   unbind_to (count, Qnil);
   954 
   955   /* Switch the frame back to the calling frame.  */
   956   if (FRAMEP (calling_frame)
   957       && FRAME_LIVE_P (XFRAME (calling_frame))
   958       && (!EQ (selected_frame, calling_frame)
   959           || (WINDOW_LIVE_P (XFRAME (calling_frame)->minibuffer_window)
   960               && !EQ (XWINDOW (XFRAME (calling_frame)->minibuffer_window)
   961                       ->frame,
   962                       calling_frame))))
   963     call2 (Qselect_frame_set_input_focus, calling_frame, Qnil);
   964 
   965   /* Add the value to the appropriate history list, if any.  This is
   966      done after the previous buffer has been made current again, in
   967      case the history variable is buffer-local.  */
   968   if (! (NILP (Vhistory_add_new_input) || NILP (histstring)))
   969     call2 (Qadd_to_history, histvar, histstring);
   970 
   971   /* If Lisp form desired instead of string, parse it.  */
   972   if (expflag)
   973     val = string_to_object (val, defalt);
   974 
   975   return val;
   976 }
   977 
   978 /* Return true if BUF is a particular existing minibuffer.  */
   979 bool
   980 is_minibuffer (EMACS_INT depth, Lisp_Object buf)
   981 {
   982   Lisp_Object tail = Fnthcdr (make_fixnum (depth), Vminibuffer_list);
   983   return
   984     !NILP (tail)
   985     && EQ (Fcar (tail), buf);
   986 }
   987 
   988 /* Return the DEPTHth minibuffer, or nil if such does not yet exist.  */
   989 static Lisp_Object
   990 nth_minibuffer (EMACS_INT depth)
   991 {
   992   Lisp_Object tail = Fnthcdr (make_fixnum (depth), Vminibuffer_list);
   993   return Fcar (tail);
   994 }
   995 
   996 /* Set the major mode of the minibuffer BUF, depending on DEPTH, the
   997    minibuffer depth.  */
   998 
   999 static void
  1000 set_minibuffer_mode (Lisp_Object buf, EMACS_INT depth)
  1001 {
  1002   specpdl_ref count = SPECPDL_INDEX ();
  1003 
  1004   record_unwind_current_buffer ();
  1005   Fset_buffer (buf);
  1006   if (depth > 0)
  1007     {
  1008       if (!NILP (Ffboundp (Qminibuffer_mode)))
  1009         call0 (Qminibuffer_mode);
  1010     }
  1011   else
  1012     {
  1013       if (!NILP (Ffboundp (Qminibuffer_inactive_mode)))
  1014         call0 (Qminibuffer_inactive_mode);
  1015       else
  1016         Fkill_all_local_variables (Qnil);
  1017     }
  1018   buf = unbind_to (count, buf);
  1019 }
  1020 
  1021 /* Return a buffer to be used as the minibuffer at depth `depth'.
  1022    depth = 0 is the lowest allowed argument, and that is the value
  1023    used for nonrecursive minibuffer invocations.  */
  1024 
  1025 Lisp_Object
  1026 get_minibuffer (EMACS_INT depth)
  1027 {
  1028   Lisp_Object tail = Fnthcdr (make_fixnum (depth), Vminibuffer_list);
  1029   Lisp_Object cll_tail = Fnthcdr (make_fixnum (depth),
  1030                                   Vcommand_loop_level_list);
  1031   if (NILP (tail))
  1032     {
  1033       tail = list1 (Qnil);
  1034       Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
  1035       cll_tail = list1 (Qnil);
  1036       Vcommand_loop_level_list = nconc2 (Vcommand_loop_level_list, cll_tail);
  1037     }
  1038   XSETCAR (cll_tail, make_fixnum (depth ? command_loop_level : 0));
  1039   Lisp_Object buf = Fcar (tail);
  1040   if (NILP (buf) || !BUFFER_LIVE_P (XBUFFER (buf)))
  1041     {
  1042       static char const name_fmt[] = " *Minibuf-%"pI"d*";
  1043       char name[sizeof name_fmt + INT_STRLEN_BOUND (EMACS_INT)];
  1044       AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, depth));
  1045       buf = Fget_buffer_create (lname, Qnil);
  1046       /* Do this before set_minibuffer_mode.  */
  1047       XSETCAR (tail, buf);
  1048       /* Although the buffer's name starts with a space, undo should be
  1049          enabled in it.  */
  1050       Fbuffer_enable_undo (buf);
  1051     }
  1052   else
  1053     {
  1054       /* We have to empty both overlay lists.  Otherwise we end
  1055          up with overlays that think they belong to this buffer
  1056          while the buffer doesn't know about them any more.  */
  1057       delete_all_overlays (XBUFFER (buf));
  1058       reset_buffer (XBUFFER (buf));
  1059     }
  1060 
  1061   return buf;
  1062 }
  1063 
  1064 static EMACS_INT minibuf_c_loop_level (EMACS_INT depth)
  1065 {
  1066   Lisp_Object cll = Fnth (make_fixnum (depth), Vcommand_loop_level_list);
  1067   if (FIXNUMP (cll))
  1068     return XFIXNUM (cll);
  1069   return 0;
  1070 }
  1071 
  1072 static void
  1073 run_exit_minibuf_hook (Lisp_Object minibuf)
  1074 {
  1075   specpdl_ref count = SPECPDL_INDEX ();
  1076   record_unwind_current_buffer ();
  1077   if (BUFFER_LIVE_P (XBUFFER (minibuf)))
  1078     Fset_buffer (minibuf);
  1079   safe_run_hooks (Qminibuffer_exit_hook);
  1080   unbind_to (count, Qnil);
  1081 }
  1082 
  1083 /* This variable records the expired minibuffer's frame between the
  1084    calls of `read_minibuf_unwind' and `minibuffer_unwind'.  It should
  1085    be used only by these two functions.  Note that the same search
  1086    method for the MB's frame won't always work in `minibuffer_unwind'
  1087    because the intervening `restore-window-configuration' will have
  1088    changed the buffer in the mini-window.  */
  1089 static Lisp_Object exp_MB_frame;
  1090 
  1091 /* This function is called on exiting minibuffer, whether normally or
  1092    not, and it restores the current window, buffer, etc.  */
  1093 
  1094 static void
  1095 read_minibuf_unwind (void)
  1096 {
  1097   Lisp_Object old_deactivate_mark;
  1098   Lisp_Object calling_frame;
  1099   Lisp_Object calling_window;
  1100   Lisp_Object future_mini_window;
  1101   Lisp_Object saved_selected_frame = selected_frame;
  1102   Lisp_Object window, frames;
  1103   Lisp_Object expired_MB = nth_minibuffer (minibuf_level);
  1104   struct window *w;
  1105   struct frame *f;
  1106 
  1107   if (NILP (expired_MB))
  1108     emacs_abort ();
  1109 
  1110   /* Locate the expired minibuffer.  */
  1111   FOR_EACH_FRAME (frames, exp_MB_frame)
  1112     {
  1113       f = XFRAME (exp_MB_frame);
  1114       window = f->minibuffer_window;
  1115       if (WINDOW_LIVE_P (window))
  1116         {
  1117           w = XWINDOW (window);
  1118           if (EQ (w->frame, exp_MB_frame)
  1119               && EQ (w->contents, expired_MB))
  1120             goto found;
  1121         }
  1122     }
  1123   exp_MB_frame = Qnil;          /* "Can't happen." */
  1124 
  1125  found:
  1126   if (!EQ (exp_MB_frame, saved_selected_frame)
  1127       && !NILP (exp_MB_frame))
  1128     do_switch_frame (exp_MB_frame, 0, 0, Qt); /* This also sets
  1129                                              minibuf_window */
  1130 
  1131   /* To keep things predictable, in case it matters, let's be in the
  1132      minibuffer when we reset the relevant variables.  Don't depend on
  1133      `minibuf_window' here.  This could by now be the mini-window of any
  1134      frame.  */
  1135   Fset_buffer (expired_MB);
  1136   minibuf_level--;
  1137 
  1138   /* Restore prompt, etc, from outer minibuffer level.  */
  1139   Lisp_Object key_vec = Fcar (minibuf_save_list);
  1140   this_command_key_count = ASIZE (key_vec);
  1141   this_command_keys = key_vec;
  1142   minibuf_save_list = Fcdr (minibuf_save_list);
  1143   minibuf_prompt = Fcar (minibuf_save_list);
  1144   minibuf_save_list = Fcdr (minibuf_save_list);
  1145   minibuf_prompt_width = XFIXNAT (Fcar (minibuf_save_list));
  1146   minibuf_save_list = Fcdr (minibuf_save_list);
  1147   Vhelp_form = Fcar (minibuf_save_list);
  1148   minibuf_save_list = Fcdr (minibuf_save_list);
  1149   Vcurrent_prefix_arg = Fcar (minibuf_save_list);
  1150   minibuf_save_list = Fcdr (minibuf_save_list);
  1151   Vminibuffer_history_position = Fcar (minibuf_save_list);
  1152   minibuf_save_list = Fcdr (minibuf_save_list);
  1153   Vminibuffer_history_variable = Fcar (minibuf_save_list);
  1154   minibuf_save_list = Fcdr (minibuf_save_list);
  1155   Voverriding_local_map = Fcar (minibuf_save_list);
  1156   minibuf_save_list = Fcdr (minibuf_save_list);
  1157 #if 0
  1158   temp = Fcar (minibuf_save_list);
  1159   if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
  1160     minibuf_window = temp;
  1161 #endif
  1162   future_mini_window = Fcar (minibuf_save_list);
  1163   minibuf_save_list = Fcdr (minibuf_save_list);
  1164   calling_frame = Fcar (minibuf_save_list);
  1165   minibuf_save_list = Fcdr (minibuf_save_list);
  1166   calling_window = Fcar (minibuf_save_list);
  1167   minibuf_save_list = Fcdr (minibuf_save_list);
  1168 
  1169   /* Erase the minibuffer we were using at this level.  */
  1170   {
  1171     specpdl_ref count = SPECPDL_INDEX ();
  1172     /* Prevent error in erase-buffer.  */
  1173     specbind (Qinhibit_read_only, Qt);
  1174     specbind (Qinhibit_modification_hooks, Qt);
  1175     old_deactivate_mark = Vdeactivate_mark;
  1176     Ferase_buffer ();
  1177     Vdeactivate_mark = old_deactivate_mark;
  1178     unbind_to (count, Qnil);
  1179   }
  1180 
  1181   /* When we get to the outmost level, make sure we resize the
  1182      mini-window back to its normal size.  */
  1183   if (minibuf_level == 0
  1184       || !EQ (selected_frame, WINDOW_FRAME (XWINDOW (future_mini_window))))
  1185     resize_mini_window (XWINDOW (minibuf_window), 0);
  1186 
  1187   /* Deal with frames that should be removed when exiting the
  1188      minibuffer.  */
  1189   {
  1190     Lisp_Object frames, frame1, val;
  1191     struct frame *f1;
  1192 
  1193     FOR_EACH_FRAME (frames, frame1)
  1194       {
  1195         f1 = XFRAME (frame1);
  1196 
  1197         if ((FRAME_PARENT_FRAME (f1)
  1198              || !NILP (get_frame_param (f1, Qdelete_before)))
  1199             && !NILP (val = (get_frame_param (f1, Qminibuffer_exit))))
  1200           {
  1201             if (EQ (val, Qiconify_frame))
  1202               Ficonify_frame (frame1);
  1203             else if (EQ (val, Qdelete_frame))
  1204               Fdelete_frame (frame1, Qnil);
  1205             else
  1206               Fmake_frame_invisible (frame1, Qnil);
  1207           }
  1208       }
  1209   }
  1210 
  1211   /* In case the previous minibuffer displayed in this miniwindow is
  1212      dead, we may keep displaying this buffer (tho it's inactive), so reset it,
  1213      to make sure we don't leave around bindings and stuff which only
  1214      made sense during the read_minibuf invocation.  */
  1215   call0 (Qminibuffer_inactive_mode);
  1216 
  1217   /* We've exited the recursive edit, so switch the current windows
  1218      away from the expired minibuffer window, both in the current
  1219      minibuffer's frame and the original calling frame.  */
  1220   choose_minibuf_frame ();
  1221   if (NILP (XWINDOW (minibuf_window)->prev_buffers))
  1222     {
  1223       if (!EQ (WINDOW_FRAME (XWINDOW (minibuf_window)), calling_frame))
  1224         {
  1225           Lisp_Object prev = Fprevious_window (minibuf_window, Qnil, Qnil);
  1226           /* PREV can be on a different frame when we have a minibuffer only
  1227              frame, the other frame's minibuffer window is MINIBUF_WINDOW,
  1228              and its "focus window" is also MINIBUF_WINDOW.  */
  1229           if (!EQ (prev, minibuf_window)
  1230               && EQ (WINDOW_FRAME (XWINDOW (prev)),
  1231                      WINDOW_FRAME (XWINDOW (minibuf_window))))
  1232             Fset_frame_selected_window (selected_frame, prev, Qnil);
  1233         }
  1234       else if (WINDOW_LIVE_P (calling_window))
  1235         Fset_frame_selected_window (calling_frame, calling_window, Qnil);
  1236     }
  1237 
  1238   /* Restore the selected frame. */
  1239   if (!EQ (exp_MB_frame, saved_selected_frame)
  1240       && !NILP (exp_MB_frame))
  1241     do_switch_frame (saved_selected_frame, 0, 0, Qt);
  1242 }
  1243 
  1244 /* Replace the expired minibuffer in frame exp_MB_frame with the next less
  1245    nested minibuffer in that frame, if any.  Otherwise, replace it
  1246    with the null minibuffer.  MINIBUF_WINDOW is not changed.  */
  1247 static void
  1248 minibuffer_unwind (void)
  1249 {
  1250   struct frame *f;
  1251   struct window *w;
  1252   Lisp_Object window;
  1253   Lisp_Object entry;
  1254 
  1255   if (NILP (exp_MB_frame)) return; /* "Can't happen." */
  1256   f = XFRAME (exp_MB_frame);
  1257   window = f->minibuffer_window;
  1258   w = XWINDOW (window);
  1259   if (FRAME_LIVE_P (f))
  1260     {
  1261       /* minibuf_window = sf->minibuffer_window; */
  1262       if (!NILP (w->prev_buffers))
  1263         {
  1264           entry = Fcar (w->prev_buffers);
  1265           w->prev_buffers = Fcdr (w->prev_buffers);
  1266           set_window_buffer (window, Fcar (entry), 0, 0);
  1267           Fset_window_start (window, Fcar (Fcdr (entry)), Qnil);
  1268           Fset_window_point (window, Fcar (Fcdr (Fcdr (entry))));
  1269         }
  1270       else
  1271         set_window_buffer (window, nth_minibuffer (0), 0, 0);
  1272     }
  1273 }
  1274 
  1275 
  1276 
  1277 void
  1278 barf_if_interaction_inhibited (void)
  1279 {
  1280   if (inhibit_interaction)
  1281     xsignal0 (Qinhibited_interaction);
  1282 }
  1283 
  1284 DEFUN ("read-from-minibuffer", Fread_from_minibuffer,
  1285        Sread_from_minibuffer, 1, 7, 0,
  1286        doc: /* Read a string from the minibuffer, prompting with string PROMPT.
  1287 The optional second arg INITIAL-CONTENTS is an obsolete alternative to
  1288   DEFAULT-VALUE.  It normally should be nil in new code, except when
  1289   HIST is a cons.  It is discussed in more detail below.
  1290 
  1291 Third arg KEYMAP is a keymap to use whilst reading;
  1292   if omitted or nil, the default is `minibuffer-local-map'.
  1293 
  1294 If fourth arg READ is non-nil, interpret the result as a Lisp object
  1295   and return that object:
  1296   in other words, do `(car (read-from-string INPUT-STRING))'
  1297 
  1298 Fifth arg HIST, if non-nil, specifies a history list and optionally
  1299   the initial position in the list.  It can be a symbol, which is the
  1300   history list variable to use, or a cons cell (HISTVAR . HISTPOS).
  1301   In that case, HISTVAR is the history list variable to use, and
  1302   HISTPOS is the initial position for use by the minibuffer history
  1303   commands.  For consistency, you should also specify that element of
  1304   the history as the value of INITIAL-CONTENTS.  Positions are counted
  1305   starting from 1 at the beginning of the list.  If HIST is nil, the
  1306   default history list `minibuffer-history' is used.  If HIST is t,
  1307   history is not recorded.
  1308 
  1309   If `history-add-new-input' is non-nil (the default), the result will
  1310   be added to the history list using `add-to-history'.
  1311 
  1312 Sixth arg DEFAULT-VALUE, if non-nil, should be a string, which is used
  1313   as the default to `read' if READ is non-nil and the user enters
  1314   empty input.  But if READ is nil, this function does _not_ return
  1315   DEFAULT-VALUE for empty input!  Instead, it returns the empty string.
  1316 
  1317   Whatever the value of READ, DEFAULT-VALUE is made available via the
  1318   minibuffer history commands.  DEFAULT-VALUE can also be a list of
  1319   strings, in which case all the strings are available in the history,
  1320   and the first string is the default to `read' if READ is non-nil.
  1321 
  1322 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
  1323  the current input method and the setting of `enable-multibyte-characters'.
  1324 
  1325 If the variable `minibuffer-allow-text-properties' is non-nil,
  1326  then the string which is returned includes whatever text properties
  1327  were present in the minibuffer.  Otherwise the value has no text properties.
  1328 
  1329 If `inhibit-interaction' is non-nil, this function will signal an
  1330   `inhibited-interaction' error.
  1331 
  1332 The remainder of this documentation string describes the
  1333 INITIAL-CONTENTS argument in more detail.  It is only relevant when
  1334 studying existing code, or when HIST is a cons.  If non-nil,
  1335 INITIAL-CONTENTS is a string to be inserted into the minibuffer before
  1336 reading input.  Normally, point is put at the end of that string.
  1337 However, if INITIAL-CONTENTS is (STRING . POSITION), the initial
  1338 input is STRING, but point is placed at _one-indexed_ position
  1339 POSITION in the minibuffer.  Any integer value less than or equal to
  1340 one puts point at the beginning of the string.  *Note* that this
  1341 behavior differs from the way such arguments are used in `completing-read'
  1342 and some related functions, which use zero-indexing for POSITION.  */)
  1343   (Lisp_Object prompt, Lisp_Object initial_contents, Lisp_Object keymap, Lisp_Object read, Lisp_Object hist, Lisp_Object default_value, Lisp_Object inherit_input_method)
  1344 {
  1345   Lisp_Object histvar, histpos, val;
  1346 
  1347   barf_if_interaction_inhibited ();
  1348 
  1349   CHECK_STRING (prompt);
  1350   if (NILP (keymap))
  1351     keymap = Vminibuffer_local_map;
  1352   else
  1353     keymap = get_keymap (keymap, 1, 0);
  1354 
  1355   if (SYMBOLP (hist))
  1356     {
  1357       histvar = hist;
  1358       histpos = Qnil;
  1359     }
  1360   else
  1361     {
  1362       histvar = Fcar_safe (hist);
  1363       histpos = Fcdr_safe (hist);
  1364     }
  1365   if (NILP (histvar))
  1366     histvar = Qminibuffer_history;
  1367   if (NILP (histpos))
  1368     XSETFASTINT (histpos, 0);
  1369 
  1370   val = read_minibuf (keymap, initial_contents, prompt,
  1371                       !NILP (read),
  1372                       histvar, histpos, default_value,
  1373                       minibuffer_allow_text_properties,
  1374                       !NILP (inherit_input_method));
  1375   return val;
  1376 }
  1377 
  1378 /* Functions that use the minibuffer to read various things.  */
  1379 
  1380 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
  1381        doc: /* Read a string from the minibuffer, prompting with string PROMPT.
  1382 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
  1383   This argument has been superseded by DEFAULT-VALUE and should normally be nil
  1384   in new code.  It behaves as INITIAL-CONTENTS in `read-from-minibuffer' (which
  1385   see).
  1386 The third arg HISTORY, if non-nil, specifies a history list
  1387   and optionally the initial position in the list.
  1388 See `read-from-minibuffer' for details of HISTORY argument.
  1389 Fourth arg DEFAULT-VALUE is the default value or the list of default values.
  1390  If non-nil, it is used for history commands, and as the value (or the first
  1391  element of the list of default values) to return if the user enters the
  1392  empty string.
  1393 Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
  1394  the current input method and the setting of `enable-multibyte-characters'.  */)
  1395   (Lisp_Object prompt, Lisp_Object initial_input, Lisp_Object history, Lisp_Object default_value, Lisp_Object inherit_input_method)
  1396 {
  1397   Lisp_Object val;
  1398   specpdl_ref count = SPECPDL_INDEX ();
  1399 
  1400   /* Just in case we're in a recursive minibuffer, make it clear that the
  1401      previous minibuffer's completion table does not apply to the new
  1402      minibuffer.
  1403      FIXME: `minibuffer-completion-table' should be buffer-local instead.  */
  1404   specbind (Qminibuffer_completion_table, Qnil);
  1405 
  1406   val = Fread_from_minibuffer (prompt, initial_input, Qnil,
  1407                                Qnil, history, default_value,
  1408                                inherit_input_method);
  1409   if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
  1410     val = CONSP (default_value) ? XCAR (default_value) : default_value;
  1411   return unbind_to (count, val);
  1412 }
  1413 
  1414 DEFUN ("read-command", Fread_command, Sread_command, 1, 2, 0,
  1415        doc: /* Read the name of a command and return as a symbol.
  1416 Prompt with PROMPT.  By default, return DEFAULT-VALUE or its first element
  1417 if it is a list.  If DEFAULT-VALUE is omitted or nil, and the user enters
  1418 null input, return a symbol whose name is an empty string.  */)
  1419   (Lisp_Object prompt, Lisp_Object default_value)
  1420 {
  1421   Lisp_Object name, default_string;
  1422 
  1423   if (NILP (default_value))
  1424     default_string = Qnil;
  1425   else if (SYMBOLP (default_value))
  1426     default_string = SYMBOL_NAME (default_value);
  1427   else
  1428     default_string = default_value;
  1429 
  1430   name = Fcompleting_read (prompt, Vobarray, Qcommandp, Qt,
  1431                            Qnil, Qnil, default_string, Qnil);
  1432   if (NILP (name))
  1433     return name;
  1434   return Fintern (name, Qnil);
  1435 }
  1436 
  1437 #ifdef NOTDEF
  1438 DEFUN ("read-function", Fread_function, Sread_function, 1, 1, 0,
  1439        doc: /* One arg PROMPT, a string.  Read the name of a function and return as a symbol.
  1440 Prompt with PROMPT.  */)
  1441   (Lisp_Object prompt)
  1442 {
  1443   return Fintern (Fcompleting_read (prompt, Vobarray, Qfboundp, Qt, Qnil, Qnil, Qnil, Qnil),
  1444                   Qnil);
  1445 }
  1446 #endif /* NOTDEF */
  1447 
  1448 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
  1449        doc: /* Read the name of a user option and return it as a symbol.
  1450 Prompt with PROMPT.  By default, return DEFAULT-VALUE or its first element
  1451 if it is a list of strings.
  1452 A user option, or customizable variable, is one for which
  1453 `custom-variable-p' returns non-nil.  */)
  1454   (Lisp_Object prompt, Lisp_Object default_value)
  1455 {
  1456   Lisp_Object name, default_string;
  1457 
  1458   if (NILP (default_value))
  1459     default_string = Qnil;
  1460   else if (SYMBOLP (default_value))
  1461     default_string = SYMBOL_NAME (default_value);
  1462   else
  1463     default_string = default_value;
  1464 
  1465   name = Fcompleting_read (prompt, Vobarray,
  1466                            Qcustom_variable_p, Qt,
  1467                            Qnil, Qcustom_variable_history,
  1468                            default_string, Qnil);
  1469   if (NILP (name))
  1470     return name;
  1471   return Fintern (name, Qnil);
  1472 }
  1473 
  1474 DEFUN ("read-buffer", Fread_buffer, Sread_buffer, 1, 4, 0,
  1475        doc: /* Read the name of a buffer and return it as a string.
  1476 Prompt with PROMPT, which should be a string ending with a colon and a space.
  1477 Provides completion on buffer names the user types.
  1478 Optional second arg DEF is value to return if user enters an empty line,
  1479  instead of that empty string.
  1480  If DEF is a list of default values, return its first element.
  1481 Optional third arg REQUIRE-MATCH has the same meaning as the
  1482  REQUIRE-MATCH argument of `completing-read'.
  1483 Optional arg PREDICATE, if non-nil, is a function limiting the buffers that
  1484 can be considered.  It will be called with each potential candidate, in
  1485 the form of either a string or a cons cell whose `car' is a string, and
  1486 should return non-nil to accept the candidate for completion, nil otherwise.
  1487 If `read-buffer-completion-ignore-case' is non-nil, completion ignores
  1488 case while reading the buffer name.
  1489 If `read-buffer-function' is non-nil, this works by calling it as a
  1490 function, instead of the usual behavior.  */)
  1491   (Lisp_Object prompt, Lisp_Object def, Lisp_Object require_match,
  1492    Lisp_Object predicate)
  1493 {
  1494   Lisp_Object result;
  1495   char *s;
  1496   ptrdiff_t len;
  1497   specpdl_ref count = SPECPDL_INDEX ();
  1498 
  1499   if (BUFFERP (def))
  1500     def = BVAR (XBUFFER (def), name);
  1501 
  1502   specbind (Qcompletion_ignore_case,
  1503             read_buffer_completion_ignore_case ? Qt : Qnil);
  1504 
  1505   if (NILP (Vread_buffer_function))
  1506     {
  1507       if (!NILP (def))
  1508         {
  1509           /* A default value was provided: we must change PROMPT,
  1510              editing the default value in before the colon.  To achieve
  1511              this, we replace PROMPT with a substring that doesn't
  1512              contain the terminal space and colon (if present).  They
  1513              are then added back using Fformat.  */
  1514 
  1515           if (STRINGP (prompt))
  1516             {
  1517               s = SSDATA (prompt);
  1518               len = SBYTES (prompt);
  1519               if (len >= 2 && s[len - 2] == ':' && s[len - 1] == ' ')
  1520                 len = len - 2;
  1521               else if (len >= 1 && (s[len - 1] == ':' || s[len - 1] == ' '))
  1522                 len--;
  1523 
  1524               prompt = make_specified_string (s, -1, len,
  1525                                               STRING_MULTIBYTE (prompt));
  1526             }
  1527 
  1528           prompt = CALLN (Ffuncall, intern("format-prompt"),
  1529                           prompt,
  1530                           CONSP (def) ? XCAR (def) : def);
  1531         }
  1532 
  1533       result = Fcompleting_read (prompt, intern ("internal-complete-buffer"),
  1534                                  predicate, require_match, Qnil,
  1535                                  Qbuffer_name_history, def, Qnil);
  1536     }
  1537   else
  1538     result = (NILP (predicate)
  1539               /* Partial backward compatibility for older read_buffer_functions
  1540                  which don't expect a `predicate' argument.  */
  1541               ? call3 (Vread_buffer_function, prompt, def, require_match)
  1542               : call4 (Vread_buffer_function, prompt, def, require_match,
  1543                        predicate));
  1544   return unbind_to (count, result);
  1545 }
  1546 
  1547 static bool
  1548 match_regexps (Lisp_Object string, Lisp_Object regexps,
  1549                bool ignore_case)
  1550 {
  1551   ptrdiff_t val;
  1552   for (; CONSP (regexps); regexps = XCDR (regexps))
  1553     {
  1554       CHECK_STRING (XCAR (regexps));
  1555 
  1556       val = fast_string_match_internal
  1557         (XCAR (regexps), string,
  1558          (ignore_case ? BVAR (current_buffer, case_canon_table) : Qnil));
  1559 
  1560       if (val == -2)
  1561         error ("Stack overflow in regexp matcher");
  1562       if (val < 0)
  1563         return false;
  1564     }
  1565   return true;
  1566 }
  1567 
  1568 DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
  1569        doc: /* Return longest common substring of all completions of STRING in COLLECTION.
  1570 
  1571 Test each possible completion specified by COLLECTION
  1572 to see if it begins with STRING.  The possible completions may be
  1573 strings or symbols.  Symbols are converted to strings before testing,
  1574 by using `symbol-name'.
  1575 
  1576 If no possible completions match, the function returns nil; if
  1577 there's just one exact match, it returns t; otherwise it returns
  1578 the longest initial substring common to all possible completions
  1579 that begin with STRING.
  1580 
  1581 If COLLECTION is an alist, the keys (cars of elements) are the
  1582 possible completions.  If an element is not a cons cell, then the
  1583 element itself is a possible completion.
  1584 If COLLECTION is a hash-table, all the keys that are either strings
  1585 or symbols are the possible completions.
  1586 If COLLECTION is an obarray, the names of all symbols in the obarray
  1587 are the possible completions.
  1588 
  1589 COLLECTION can also be a function to do the completion itself.
  1590 It receives three arguments: STRING, PREDICATE and nil.
  1591 Whatever it returns becomes the value of `try-completion'.
  1592 
  1593 If optional third argument PREDICATE is non-nil, it must be a function
  1594 of one or two arguments, and is used to test each possible completion.
  1595 A possible completion is accepted only if PREDICATE returns non-nil.
  1596 
  1597 The argument given to PREDICATE is either a string or a cons cell (whose
  1598 car is a string) from the alist, or a symbol from the obarray.
  1599 If COLLECTION is a hash-table, PREDICATE is called with two arguments:
  1600 the string key and the associated value.
  1601 
  1602 To be acceptable, a possible completion must also match all the regexps
  1603 in `completion-regexp-list' (unless COLLECTION is a function, in
  1604 which case that function should itself handle `completion-regexp-list').
  1605 
  1606 If `completion-ignore-case' is non-nil, possible completions are matched
  1607 while ignoring letter-case, but no guarantee is made about the letter-case
  1608 of the return value, except that it comes either from the user's input
  1609 or from one of the possible completions.  */)
  1610   (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
  1611 {
  1612 
  1613   Lisp_Object bestmatch, tail, elt, eltstring;
  1614   /* Size in bytes of BESTMATCH.  */
  1615   ptrdiff_t bestmatchsize = 0;
  1616   /* These are in bytes, too.  */
  1617   ptrdiff_t compare, matchsize;
  1618   enum { function_table, list_table, obarray_table, hash_table}
  1619     type = (HASH_TABLE_P (collection) ? hash_table
  1620             : VECTORP (collection) ? obarray_table
  1621             : ((NILP (collection)
  1622                 || (CONSP (collection) && !FUNCTIONP (collection)))
  1623                ? list_table : function_table));
  1624   ptrdiff_t idx = 0, obsize = 0;
  1625   int matchcount = 0;
  1626   Lisp_Object bucket, zero, end, tem;
  1627 
  1628   CHECK_STRING (string);
  1629   if (type == function_table)
  1630     return call3 (collection, string, predicate, Qnil);
  1631 
  1632   bestmatch = bucket = Qnil;
  1633   zero = make_fixnum (0);
  1634 
  1635   /* If COLLECTION is not a list, set TAIL just for gc pro.  */
  1636   tail = collection;
  1637   if (type == obarray_table)
  1638     {
  1639       collection = check_obarray (collection);
  1640       obsize = ASIZE (collection);
  1641       bucket = AREF (collection, idx);
  1642     }
  1643 
  1644   while (1)
  1645     {
  1646       /* Get the next element of the alist, obarray, or hash-table.  */
  1647       /* Exit the loop if the elements are all used up.  */
  1648       /* elt gets the alist element or symbol.
  1649          eltstring gets the name to check as a completion.  */
  1650 
  1651       if (type == list_table)
  1652         {
  1653           if (!CONSP (tail))
  1654             break;
  1655           elt = XCAR (tail);
  1656           eltstring = CONSP (elt) ? XCAR (elt) : elt;
  1657           tail = XCDR (tail);
  1658         }
  1659       else if (type == obarray_table)
  1660         {
  1661           if (!EQ (bucket, zero))
  1662             {
  1663               if (!SYMBOLP (bucket))
  1664                 error ("Bad data in guts of obarray");
  1665               elt = bucket;
  1666               eltstring = elt;
  1667               if (XSYMBOL (bucket)->u.s.next)
  1668                 XSETSYMBOL (bucket, XSYMBOL (bucket)->u.s.next);
  1669               else
  1670                 XSETFASTINT (bucket, 0);
  1671             }
  1672           else if (++idx >= obsize)
  1673             break;
  1674           else
  1675             {
  1676               bucket = AREF (collection, idx);
  1677               continue;
  1678             }
  1679         }
  1680       else /* if (type == hash_table) */
  1681         {
  1682           while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
  1683                  && BASE_EQ (HASH_KEY (XHASH_TABLE (collection), idx),
  1684                              Qunbound))
  1685             idx++;
  1686           if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
  1687             break;
  1688           else
  1689             elt = eltstring = HASH_KEY (XHASH_TABLE (collection), idx++);
  1690         }
  1691 
  1692       /* Is this element a possible completion?  */
  1693 
  1694       if (SYMBOLP (eltstring))
  1695         eltstring = Fsymbol_name (eltstring);
  1696 
  1697       if (STRINGP (eltstring)
  1698           && SCHARS (string) <= SCHARS (eltstring)
  1699           && (tem = Fcompare_strings (eltstring, zero,
  1700                                       make_fixnum (SCHARS (string)),
  1701                                       string, zero, Qnil,
  1702                                       completion_ignore_case ? Qt : Qnil),
  1703               EQ (Qt, tem)))
  1704         {
  1705           /* Ignore this element if it fails to match all the regexps.  */
  1706           if (!match_regexps (eltstring, Vcompletion_regexp_list,
  1707                               completion_ignore_case))
  1708             continue;
  1709 
  1710           /* Ignore this element if there is a predicate
  1711              and the predicate doesn't like it.  */
  1712 
  1713           if (!NILP (predicate))
  1714             {
  1715               if (EQ (predicate, Qcommandp))
  1716                 tem = Fcommandp (elt, Qnil);
  1717               else
  1718                 {
  1719                   tem = (type == hash_table
  1720                          ? call2 (predicate, elt,
  1721                                   HASH_VALUE (XHASH_TABLE (collection),
  1722                                               idx - 1))
  1723                          : call1 (predicate, elt));
  1724                 }
  1725               if (NILP (tem)) continue;
  1726             }
  1727 
  1728           /* Update computation of how much all possible completions match */
  1729 
  1730           if (NILP (bestmatch))
  1731             {
  1732               matchcount = 1;
  1733               bestmatch = eltstring;
  1734               bestmatchsize = SCHARS (eltstring);
  1735             }
  1736           else
  1737             {
  1738               compare = min (bestmatchsize, SCHARS (eltstring));
  1739               Lisp_Object lcompare = make_fixnum (compare);
  1740               tem = Fcompare_strings (bestmatch, zero, lcompare,
  1741                                       eltstring, zero, lcompare,
  1742                                       completion_ignore_case ? Qt : Qnil);
  1743               matchsize = EQ (tem, Qt) ? compare : eabs (XFIXNUM (tem)) - 1;
  1744 
  1745               Lisp_Object old_bestmatch = bestmatch;
  1746               if (completion_ignore_case)
  1747                 {
  1748                   /* If this is an exact match except for case,
  1749                      use it as the best match rather than one that is not an
  1750                      exact match.  This way, we get the case pattern
  1751                      of the actual match.  */
  1752                   if ((matchsize == SCHARS (eltstring)
  1753                        && matchsize < SCHARS (bestmatch))
  1754                       ||
  1755                       /* If there is more than one exact match ignoring case,
  1756                          and one of them is exact including case,
  1757                          prefer that one.  */
  1758                       /* If there is no exact match ignoring case,
  1759                          prefer a match that does not change the case
  1760                          of the input.  */
  1761                       ((matchsize == SCHARS (eltstring))
  1762                        ==
  1763                        (matchsize == SCHARS (bestmatch))
  1764                        && (tem = Fcompare_strings (eltstring, zero,
  1765                                                    make_fixnum (SCHARS (string)),
  1766                                                    string, zero,
  1767                                                    Qnil,
  1768                                                    Qnil),
  1769                            EQ (Qt, tem))
  1770                        && (tem = Fcompare_strings (bestmatch, zero,
  1771                                                    make_fixnum (SCHARS (string)),
  1772                                                    string, zero,
  1773                                                    Qnil,
  1774                                                    Qnil),
  1775                            ! EQ (Qt, tem))))
  1776                     bestmatch = eltstring;
  1777                 }
  1778               if (bestmatchsize != SCHARS (eltstring)
  1779                   || bestmatchsize != matchsize
  1780                   || (completion_ignore_case
  1781                       && !BASE_EQ (Fcompare_strings (old_bestmatch, zero,
  1782                                                      lcompare, eltstring, zero,
  1783                                                      lcompare, Qnil),
  1784                                    Qt)))
  1785                 /* Don't count the same string multiple times.  */
  1786                 matchcount += matchcount <= 1;
  1787               bestmatchsize = matchsize;
  1788               if (matchsize <= SCHARS (string)
  1789                   /* If completion-ignore-case is non-nil, don't
  1790                      short-circuit because we want to find the best
  1791                      possible match *including* case differences.  */
  1792                   && !completion_ignore_case
  1793                   && matchcount > 1)
  1794                 /* No need to look any further.  */
  1795                 break;
  1796             }
  1797         }
  1798     }
  1799 
  1800   if (NILP (bestmatch))
  1801     return Qnil;                /* No completions found.  */
  1802   /* If we are ignoring case, and there is no exact match,
  1803      and no additional text was supplied,
  1804      don't change the case of what the user typed.  */
  1805   if (completion_ignore_case && bestmatchsize == SCHARS (string)
  1806       && SCHARS (bestmatch) > bestmatchsize)
  1807     return string;
  1808 
  1809   /* Return t if the supplied string is an exact match (counting case);
  1810      it does not require any change to be made.  */
  1811   if (matchcount == 1 && !NILP (Fequal (bestmatch, string)))
  1812     return Qt;
  1813 
  1814   XSETFASTINT (zero, 0);                /* Else extract the part in which */
  1815   XSETFASTINT (end, bestmatchsize);     /* all completions agree.  */
  1816   return Fsubstring (bestmatch, zero, end);
  1817 }
  1818 
  1819 DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 4, 0,
  1820        doc: /* Search for partial matches of STRING in COLLECTION.
  1821 
  1822 Test each possible completion specified by COLLECTION
  1823 to see if it begins with STRING.  The possible completions may be
  1824 strings or symbols.  Symbols are converted to strings before testing,
  1825 by using `symbol-name'.
  1826 
  1827 The value is a list of all the possible completions that match STRING.
  1828 
  1829 If COLLECTION is an alist, the keys (cars of elements) are the
  1830 possible completions.  If an element is not a cons cell, then the
  1831 element itself is the possible completion.
  1832 If COLLECTION is a hash-table, all the keys that are strings or symbols
  1833 are the possible completions.
  1834 If COLLECTION is an obarray, the names of all symbols in the obarray
  1835 are the possible completions.
  1836 
  1837 COLLECTION can also be a function to do the completion itself.
  1838 It receives three arguments: STRING, PREDICATE and t.
  1839 Whatever it returns becomes the value of `all-completions'.
  1840 
  1841 If optional third argument PREDICATE is non-nil, it must be a function
  1842 of one or two arguments, and is used to test each possible completion.
  1843 A possible completion is accepted only if PREDICATE returns non-nil.
  1844 
  1845 The argument given to PREDICATE is either a string or a cons cell (whose
  1846 car is a string) from the alist, or a symbol from the obarray.
  1847 If COLLECTION is a hash-table, PREDICATE is called with two arguments:
  1848 the string key and the associated value.
  1849 
  1850 To be acceptable, a possible completion must also match all the regexps
  1851 in `completion-regexp-list' (unless COLLECTION is a function, in
  1852 which case that function should itself handle `completion-regexp-list').
  1853 
  1854 An obsolete optional fourth argument HIDE-SPACES is still accepted for
  1855 backward compatibility.  If non-nil, strings in COLLECTION that start
  1856 with a space are ignored unless STRING itself starts with a space.  */)
  1857   (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate, Lisp_Object hide_spaces)
  1858 {
  1859   Lisp_Object tail, elt, eltstring;
  1860   Lisp_Object allmatches;
  1861   int type = HASH_TABLE_P (collection) ? 3
  1862     : VECTORP (collection) ? 2
  1863     : NILP (collection) || (CONSP (collection) && !FUNCTIONP (collection));
  1864   ptrdiff_t idx = 0, obsize = 0;
  1865   Lisp_Object bucket, tem, zero;
  1866 
  1867   CHECK_STRING (string);
  1868   if (type == 0)
  1869     return call3 (collection, string, predicate, Qt);
  1870   allmatches = bucket = Qnil;
  1871   zero = make_fixnum (0);
  1872 
  1873   /* If COLLECTION is not a list, set TAIL just for gc pro.  */
  1874   tail = collection;
  1875   if (type == 2)
  1876     {
  1877       collection = check_obarray (collection);
  1878       obsize = ASIZE (collection);
  1879       bucket = AREF (collection, idx);
  1880     }
  1881 
  1882   while (1)
  1883     {
  1884       /* Get the next element of the alist, obarray, or hash-table.  */
  1885       /* Exit the loop if the elements are all used up.  */
  1886       /* elt gets the alist element or symbol.
  1887          eltstring gets the name to check as a completion.  */
  1888 
  1889       if (type == 1)
  1890         {
  1891           if (!CONSP (tail))
  1892             break;
  1893           elt = XCAR (tail);
  1894           eltstring = CONSP (elt) ? XCAR (elt) : elt;
  1895           tail = XCDR (tail);
  1896         }
  1897       else if (type == 2)
  1898         {
  1899           if (!EQ (bucket, zero))
  1900             {
  1901               if (!SYMBOLP (bucket))
  1902                 error ("Bad data in guts of obarray");
  1903               elt = bucket;
  1904               eltstring = elt;
  1905               if (XSYMBOL (bucket)->u.s.next)
  1906                 XSETSYMBOL (bucket, XSYMBOL (bucket)->u.s.next);
  1907               else
  1908                 XSETFASTINT (bucket, 0);
  1909             }
  1910           else if (++idx >= obsize)
  1911             break;
  1912           else
  1913             {
  1914               bucket = AREF (collection, idx);
  1915               continue;
  1916             }
  1917         }
  1918       else /* if (type == 3) */
  1919         {
  1920           while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
  1921                  && BASE_EQ (HASH_KEY (XHASH_TABLE (collection), idx),
  1922                              Qunbound))
  1923             idx++;
  1924           if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
  1925             break;
  1926           else
  1927             elt = eltstring = HASH_KEY (XHASH_TABLE (collection), idx++);
  1928         }
  1929 
  1930       /* Is this element a possible completion?  */
  1931 
  1932       if (SYMBOLP (eltstring))
  1933         eltstring = Fsymbol_name (eltstring);
  1934 
  1935       if (STRINGP (eltstring)
  1936           && SCHARS (string) <= SCHARS (eltstring)
  1937           /* If HIDE_SPACES, reject alternatives that start with space
  1938              unless the input starts with space.  */
  1939           && (NILP (hide_spaces)
  1940               || (SBYTES (string) > 0
  1941                   && SREF (string, 0) == ' ')
  1942               || SREF (eltstring, 0) != ' ')
  1943           && (tem = Fcompare_strings (eltstring, zero,
  1944                                       make_fixnum (SCHARS (string)),
  1945                                       string, zero,
  1946                                       make_fixnum (SCHARS (string)),
  1947                                       completion_ignore_case ? Qt : Qnil),
  1948               EQ (Qt, tem)))
  1949         {
  1950           /* Ignore this element if it fails to match all the regexps.  */
  1951           if (!match_regexps (eltstring, Vcompletion_regexp_list,
  1952                               completion_ignore_case))
  1953             continue;
  1954 
  1955           /* Ignore this element if there is a predicate
  1956              and the predicate doesn't like it.  */
  1957 
  1958           if (!NILP (predicate))
  1959             {
  1960               if (EQ (predicate, Qcommandp))
  1961                 tem = Fcommandp (elt, Qnil);
  1962               else
  1963                 {
  1964                   tem = type == 3
  1965                     ? call2 (predicate, elt,
  1966                              HASH_VALUE (XHASH_TABLE (collection), idx - 1))
  1967                     : call1 (predicate, elt);
  1968                 }
  1969               if (NILP (tem)) continue;
  1970             }
  1971           /* Ok => put it on the list.  */
  1972           allmatches = Fcons (eltstring, allmatches);
  1973         }
  1974     }
  1975 
  1976   return Fnreverse (allmatches);
  1977 }
  1978 
  1979 DEFUN ("completing-read", Fcompleting_read, Scompleting_read, 2, 8, 0,
  1980        doc: /* Read a string in the minibuffer, with completion.
  1981 PROMPT is a string to prompt with; normally it ends in a colon and a space.
  1982 COLLECTION can be a list of strings, an alist, an obarray or a hash table.
  1983 COLLECTION can also be a function to do the completion itself.
  1984 PREDICATE limits completion to a subset of COLLECTION.
  1985 See `try-completion', `all-completions', `test-completion',
  1986 and `completion-boundaries', for more details on completion,
  1987 COLLECTION, and PREDICATE.  See also Info node `(elisp)Basic Completion'
  1988 for the details about completion, and Info node `(elisp)Programmed
  1989 Completion' for expectations from COLLECTION when it's a function.
  1990 
  1991 REQUIRE-MATCH can take the following values:
  1992 - t means that the user is not allowed to exit unless the input is (or
  1993   completes to) an element of COLLECTION or is null.
  1994 - nil means that the user can exit with any input.
  1995 - `confirm' means that the user can exit with any input, but she needs
  1996   to confirm her choice if the input is not an element of COLLECTION.
  1997 - `confirm-after-completion' means that the user can exit with any
  1998   input, but she needs to confirm her choice if she called
  1999   `minibuffer-complete' right before `minibuffer-complete-and-exit'
  2000   and the input is not an element of COLLECTION.
  2001 - a function, which will be called with the input as the
  2002   argument.  If the function returns a non-nil value, the
  2003   minibuffer is exited with that argument as the value.
  2004 - anything else behaves like t except that typing RET does not exit if it
  2005   does non-null completion.
  2006 
  2007 If the input is null, `completing-read' returns DEF, or the first
  2008 element of the list of default values, or an empty string if DEF is
  2009 nil, regardless of the value of REQUIRE-MATCH.
  2010 
  2011 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
  2012   with point positioned at the end.  If it is (STRING . POSITION), the
  2013   initial input is STRING, but point is placed at _zero-indexed_
  2014   position POSITION in STRING.  (*Note* that this is different from
  2015   `read-from-minibuffer' and related functions, which use one-indexing
  2016   for POSITION.)  This feature is deprecated--it is best to pass nil
  2017   for INITIAL-INPUT and supply the default value DEF instead.  The
  2018   user can yank the default value into the minibuffer easily using
  2019   \\<minibuffer-local-map>\\[next-history-element].
  2020 
  2021 HIST, if non-nil, specifies a history list and optionally the initial
  2022   position in the list.  It can be a symbol, which is the history list
  2023   variable to use, or it can be a cons cell (HISTVAR . HISTPOS).  In
  2024   that case, HISTVAR is the history list variable to use, and HISTPOS
  2025   is the initial position (the position in the list used by the
  2026   minibuffer history commands).  For consistency, you should also
  2027   specify that element of the history as the value of INITIAL-INPUT.
  2028   (This is the only case in which you should use INITIAL-INPUT instead
  2029   of DEF.)  Positions are counted starting from 1 at the beginning of
  2030   the list.  The variable `history-length' controls the maximum length
  2031   of a history list.  If HIST is t, history is not recorded.
  2032 
  2033 DEF, if non-nil, is the default value or the list of default values.
  2034 
  2035 If INHERIT-INPUT-METHOD is non-nil, the minibuffer inherits the
  2036   current input method and the setting of `enable-multibyte-characters'.
  2037 
  2038 Completion ignores case if the ambient value of
  2039   `completion-ignore-case' is non-nil.
  2040 
  2041 See also `completing-read-function'.  */)
  2042   (Lisp_Object prompt, Lisp_Object collection, Lisp_Object predicate, Lisp_Object require_match, Lisp_Object initial_input, Lisp_Object hist, Lisp_Object def, Lisp_Object inherit_input_method)
  2043 {
  2044   return CALLN (Ffuncall,
  2045                 Fsymbol_value (intern ("completing-read-function")),
  2046                 prompt, collection, predicate, require_match, initial_input,
  2047                 hist, def, inherit_input_method);
  2048 }
  2049 
  2050 /* Test whether TXT is an exact completion.  */
  2051 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
  2052        doc: /* Return non-nil if STRING is a valid completion.
  2053 For instance, if COLLECTION is a list of strings, STRING is a
  2054 valid completion if it appears in the list and PREDICATE is satisfied.
  2055 
  2056 Takes the same arguments as `all-completions' and `try-completion'.
  2057 
  2058 If COLLECTION is a function, it is called with three arguments:
  2059 the values STRING, PREDICATE and `lambda'.  */)
  2060   (Lisp_Object string, Lisp_Object collection, Lisp_Object predicate)
  2061 {
  2062   Lisp_Object tail, tem = Qnil;
  2063   ptrdiff_t i = 0;
  2064 
  2065   CHECK_STRING (string);
  2066 
  2067   if (NILP (collection) || (CONSP (collection) && !FUNCTIONP (collection)))
  2068     {
  2069       tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
  2070       if (NILP (tem))
  2071         return Qnil;
  2072     }
  2073   else if (VECTORP (collection))
  2074     {
  2075       /* Bypass intern-soft as that loses for nil.  */
  2076       tem = oblookup (collection,
  2077                       SSDATA (string),
  2078                       SCHARS (string),
  2079                       SBYTES (string));
  2080       if (completion_ignore_case && !SYMBOLP (tem))
  2081         {
  2082           for (i = ASIZE (collection) - 1; i >= 0; i--)
  2083             {
  2084               tail = AREF (collection, i);
  2085               if (SYMBOLP (tail))
  2086                 while (1)
  2087                   {
  2088                     if (BASE_EQ (Fcompare_strings (string, make_fixnum (0),
  2089                                                    Qnil,
  2090                                                    Fsymbol_name (tail),
  2091                                                    make_fixnum (0) , Qnil, Qt),
  2092                                  Qt))
  2093                       {
  2094                         tem = tail;
  2095                         break;
  2096                       }
  2097                     if (XSYMBOL (tail)->u.s.next == 0)
  2098                       break;
  2099                     XSETSYMBOL (tail, XSYMBOL (tail)->u.s.next);
  2100                   }
  2101             }
  2102         }
  2103 
  2104       if (!SYMBOLP (tem))
  2105         return Qnil;
  2106     }
  2107   else if (HASH_TABLE_P (collection))
  2108     {
  2109       struct Lisp_Hash_Table *h = XHASH_TABLE (collection);
  2110       i = hash_lookup (h, string, NULL);
  2111       if (i >= 0)
  2112         {
  2113           tem = HASH_KEY (h, i);
  2114           goto found_matching_key;
  2115         }
  2116       else
  2117         for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
  2118           {
  2119             tem = HASH_KEY (h, i);
  2120             if (BASE_EQ (tem, Qunbound)) continue;
  2121             Lisp_Object strkey = (SYMBOLP (tem) ? Fsymbol_name (tem) : tem);
  2122             if (!STRINGP (strkey)) continue;
  2123             if (BASE_EQ (Fcompare_strings (string, Qnil, Qnil,
  2124                                            strkey, Qnil, Qnil,
  2125                                            completion_ignore_case ? Qt : Qnil),
  2126                     Qt))
  2127               goto found_matching_key;
  2128           }
  2129       return Qnil;
  2130     found_matching_key: ;
  2131     }
  2132   else
  2133     return call3 (collection, string, predicate, Qlambda);
  2134 
  2135   /* Reject this element if it fails to match all the regexps.  */
  2136   if (!match_regexps (string, Vcompletion_regexp_list,
  2137                       completion_ignore_case))
  2138     return Qnil;
  2139 
  2140   /* Finally, check the predicate.  */
  2141   if (!NILP (predicate))
  2142     {
  2143       return HASH_TABLE_P (collection)
  2144         ? call2 (predicate, tem, HASH_VALUE (XHASH_TABLE (collection), i))
  2145         : call1 (predicate, tem);
  2146     }
  2147   else
  2148     return Qt;
  2149 }
  2150 
  2151 DEFUN ("internal-complete-buffer", Finternal_complete_buffer, Sinternal_complete_buffer, 3, 3, 0,
  2152        doc: /* Perform completion on buffer names.
  2153 STRING and PREDICATE have the same meanings as in `try-completion',
  2154 `all-completions', and `test-completion'.
  2155 
  2156 If FLAG is nil, invoke `try-completion'; if it is t, invoke
  2157 `all-completions'; otherwise invoke `test-completion'.  */)
  2158   (Lisp_Object string, Lisp_Object predicate, Lisp_Object flag)
  2159 {
  2160   if (NILP (flag))
  2161     return Ftry_completion (string, Vbuffer_alist, predicate);
  2162   else if (EQ (flag, Qt))
  2163     {
  2164       Lisp_Object res = Fall_completions (string, Vbuffer_alist, predicate, Qnil);
  2165       if (SCHARS (string) > 0)
  2166         return res;
  2167       else
  2168         { /* Strip out internal buffers.  */
  2169           Lisp_Object bufs = res;
  2170           /* First, look for a non-internal buffer in `res'.  */
  2171           while (CONSP (bufs) && SREF (XCAR (bufs), 0) == ' ')
  2172             bufs = XCDR (bufs);
  2173           if (NILP (bufs))
  2174             return (list_length (res) == list_length (Vbuffer_alist)
  2175                     /* If all bufs are internal don't strip them out.  */
  2176                     ? res : bufs);
  2177           res = bufs;
  2178           while (CONSP (XCDR (bufs)))
  2179             if (SREF (XCAR (XCDR (bufs)), 0) == ' ')
  2180               XSETCDR (bufs, XCDR (XCDR (bufs)));
  2181             else
  2182               bufs = XCDR (bufs);
  2183           return res;
  2184         }
  2185     }
  2186   else if (EQ (flag, Qlambda))
  2187     return Ftest_completion (string, Vbuffer_alist, predicate);
  2188   else if (EQ (flag, Qmetadata))
  2189     return list3 (Qmetadata,
  2190                   Fcons (Qcategory, Qbuffer),
  2191                   Fcons (Qcycle_sort_function, Qidentity));
  2192   else
  2193     return Qnil;
  2194 }
  2195 
  2196 /* Like assoc but assumes KEY is a string, and ignores case if appropriate.  */
  2197 
  2198 DEFUN ("assoc-string", Fassoc_string, Sassoc_string, 2, 3, 0,
  2199        doc: /* Like `assoc' but specifically for strings (and symbols).
  2200 
  2201 This returns the first element of LIST whose car matches the string or
  2202 symbol KEY, or nil if no match exists.  When performing the
  2203 comparison, symbols are first converted to strings, and unibyte
  2204 strings to multibyte.  If the optional arg CASE-FOLD is non-nil, both
  2205 KEY and the elements of LIST are upcased for comparison.
  2206 
  2207 Unlike `assoc', KEY can also match an entry in LIST consisting of a
  2208 single string, rather than a cons cell whose car is a string.  */)
  2209   (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold)
  2210 {
  2211   register Lisp_Object tail;
  2212 
  2213   if (SYMBOLP (key))
  2214     key = Fsymbol_name (key);
  2215 
  2216   for (tail = list; CONSP (tail); tail = XCDR (tail))
  2217     {
  2218       register Lisp_Object elt, tem, thiscar;
  2219       elt = XCAR (tail);
  2220       thiscar = CONSP (elt) ? XCAR (elt) : elt;
  2221       if (SYMBOLP (thiscar))
  2222         thiscar = Fsymbol_name (thiscar);
  2223       else if (!STRINGP (thiscar))
  2224         continue;
  2225       tem = Fcompare_strings (thiscar, make_fixnum (0), Qnil,
  2226                               key, make_fixnum (0), Qnil,
  2227                               case_fold);
  2228       if (EQ (tem, Qt))
  2229         return elt;
  2230       maybe_quit ();
  2231     }
  2232   return Qnil;
  2233 }
  2234 
  2235 
  2236 DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
  2237        doc: /* Return current depth of activations of minibuffer, a nonnegative integer.  */)
  2238   (void)
  2239 {
  2240   return make_fixnum (minibuf_level);
  2241 }
  2242 
  2243 DEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
  2244        doc: /* Return the prompt string of the currently-active minibuffer.
  2245 If no minibuffer is active, return nil.  */)
  2246   (void)
  2247 {
  2248   return Fcopy_sequence (minibuf_prompt);
  2249 }
  2250 
  2251 
  2252 
  2253 void
  2254 set_initial_minibuffer_mode (void)
  2255 {
  2256   Lisp_Object minibuf = get_minibuffer (0);
  2257   set_minibuffer_mode (minibuf, 0);
  2258 }
  2259 
  2260 static void init_minibuf_once_for_pdumper (void);
  2261 
  2262 void
  2263 init_minibuf_once (void)
  2264 {
  2265   staticpro (&Vminibuffer_list);
  2266   staticpro (&Vcommand_loop_level_list);
  2267   pdumper_do_now_and_after_load (init_minibuf_once_for_pdumper);
  2268   /* Ensure our inactive minibuffer exists.  */
  2269   get_minibuffer (0);
  2270 }
  2271 
  2272 static void
  2273 init_minibuf_once_for_pdumper (void)
  2274 {
  2275   PDUMPER_IGNORE (minibuf_level);
  2276   PDUMPER_IGNORE (minibuf_prompt_width);
  2277 
  2278   /* We run this function on first initialization and whenever we
  2279      restore from a dump file.  pdumper doesn't try to preserve
  2280      frames, windows, and so on, so reset everything related here.  */
  2281   Vminibuffer_list = Qnil;
  2282   Vcommand_loop_level_list = Qnil;
  2283   minibuf_level = 0;
  2284   minibuf_prompt = Qnil;
  2285   minibuf_save_list = Qnil;
  2286   last_minibuf_string = Qnil;
  2287 }
  2288 
  2289 void
  2290 syms_of_minibuf (void)
  2291 {
  2292   staticpro (&minibuf_prompt);
  2293   staticpro (&minibuf_save_list);
  2294   staticpro (&MB_frame);
  2295   MB_frame = Qnil;
  2296   staticpro (&exp_MB_frame);
  2297 
  2298   DEFSYM (Qminibuffer_follows_selected_frame,
  2299           "minibuffer-follows-selected-frame");
  2300   DEFSYM (Qcompletion_ignore_case, "completion-ignore-case");
  2301   DEFSYM (Qminibuffer_default, "minibuffer-default");
  2302   Fset (Qminibuffer_default, Qnil);
  2303 
  2304   DEFSYM (Qminibuffer_completion_table, "minibuffer-completion-table");
  2305 
  2306   staticpro (&last_minibuf_string);
  2307 
  2308   DEFSYM (Qcustom_variable_history, "custom-variable-history");
  2309   Fset (Qcustom_variable_history, Qnil);
  2310 
  2311   DEFSYM (Qminibuffer_history, "minibuffer-history");
  2312   DEFSYM (Qbuffer_name_history, "buffer-name-history");
  2313   Fset (Qbuffer_name_history, Qnil);
  2314 
  2315   DEFSYM (Qcustom_variable_p, "custom-variable-p");
  2316 
  2317   /* Normal hooks for entry to and exit from minibuffer.  */
  2318   DEFSYM (Qminibuffer_setup_hook, "minibuffer-setup-hook");
  2319   DEFSYM (Qminibuffer_exit_hook, "minibuffer-exit-hook");
  2320 
  2321   DEFSYM (Qcurrent_input_method, "current-input-method");
  2322   DEFSYM (Qactivate_input_method, "activate-input-method");
  2323   DEFSYM (Qcase_fold_search, "case-fold-search");
  2324   DEFSYM (Qmetadata, "metadata");
  2325   DEFSYM (Qcycle_sort_function, "cycle-sort-function");
  2326 
  2327   /* A frame parameter.  */
  2328   DEFSYM (Qminibuffer_exit, "minibuffer-exit");
  2329 
  2330   DEFSYM (Qminibuffer_mode, "minibuffer-mode");
  2331   DEFSYM (Qminibuffer_inactive_mode, "minibuffer-inactive-mode");
  2332   DEFSYM (Qminibuffer_completing_file_name, "minibuffer-completing-file-name");
  2333   DEFSYM (Qselect_frame_set_input_focus, "select-frame-set-input-focus");
  2334   DEFSYM (Qadd_to_history, "add-to-history");
  2335   DEFSYM (Qpush_window_buffer_onto_prev, "push-window-buffer-onto-prev");
  2336 
  2337   DEFVAR_LISP ("read-expression-history", Vread_expression_history,
  2338                doc: /* A history list for arguments that are Lisp expressions to evaluate.
  2339 For example, `eval-expression' uses this.  */);
  2340   Vread_expression_history = Qnil;
  2341 
  2342   DEFVAR_LISP ("read-buffer-function", Vread_buffer_function,
  2343                doc: /* If this is non-nil, `read-buffer' does its work by calling this function.
  2344 The function is called with the arguments passed to `read-buffer'.  */);
  2345   Vread_buffer_function = Qnil;
  2346 
  2347   DEFVAR_LISP ("minibuffer-follows-selected-frame", minibuffer_follows_selected_frame,
  2348                doc: /* t means the active minibuffer always displays on the selected frame.
  2349 Nil means that a minibuffer will appear only in the frame which created it.
  2350 Any other value means the minibuffer will move onto another frame, but
  2351 only when the user starts using a minibuffer there.
  2352 
  2353 Any buffer local or dynamic binding of this variable is ignored.  Only the
  2354 default top level value is used.  */);
  2355   minibuffer_follows_selected_frame = Qt;
  2356 
  2357   DEFVAR_BOOL ("read-buffer-completion-ignore-case",
  2358                read_buffer_completion_ignore_case,
  2359                doc: /* Non-nil means completion ignores case when reading a buffer name.  */);
  2360   read_buffer_completion_ignore_case = 0;
  2361 
  2362   DEFVAR_LISP ("minibuffer-setup-hook", Vminibuffer_setup_hook,
  2363                doc: /* Normal hook run just after entry to minibuffer.  */);
  2364   Vminibuffer_setup_hook = Qnil;
  2365 
  2366   DEFVAR_LISP ("minibuffer-exit-hook", Vminibuffer_exit_hook,
  2367                doc: /* Normal hook run whenever a minibuffer is exited.  */);
  2368   Vminibuffer_exit_hook = Qnil;
  2369 
  2370   DEFVAR_LISP ("history-length", Vhistory_length,
  2371                doc: /* Maximum length of history lists before truncation takes place.
  2372 A number means truncate to that length; truncation deletes old
  2373 elements, and is done just after inserting a new element.
  2374 A value of t means no truncation.
  2375 
  2376 This variable only affects history lists that don't specify their own
  2377 maximum lengths.  Setting the `history-length' property of a history
  2378 variable overrides this default.  */);
  2379   XSETFASTINT (Vhistory_length, 100);
  2380 
  2381   DEFVAR_BOOL ("history-delete-duplicates", history_delete_duplicates,
  2382                doc: /* Non-nil means to delete duplicates in history.
  2383 If set to t when adding a new history element, all previous identical
  2384 elements are deleted from the history list.  */);
  2385   history_delete_duplicates = 0;
  2386 
  2387   DEFVAR_LISP ("history-add-new-input", Vhistory_add_new_input,
  2388                doc: /* Non-nil means to add new elements in history.
  2389 If set to nil, minibuffer reading functions don't add new elements to the
  2390 history list, so it is possible to do this afterwards by calling
  2391 `add-to-history' explicitly.  */);
  2392   Vhistory_add_new_input = Qt;
  2393 
  2394   DEFVAR_BOOL ("completion-ignore-case", completion_ignore_case,
  2395                doc: /* Non-nil means don't consider case significant in completion.
  2396 For file-name completion, `read-file-name-completion-ignore-case'
  2397 controls the behavior, rather than this variable.
  2398 For buffer name completion, `read-buffer-completion-ignore-case'
  2399 controls the behavior, rather than this variable.  */);
  2400   completion_ignore_case = 0;
  2401 
  2402   DEFVAR_BOOL ("enable-recursive-minibuffers", enable_recursive_minibuffers,
  2403                doc: /* Non-nil means to allow minibuffer commands while in the minibuffer.
  2404 This variable makes a difference whenever the minibuffer window is active.
  2405 Also see `minibuffer-depth-indicate-mode', which may be handy if this
  2406 variable is non-nil. */);
  2407   enable_recursive_minibuffers = 0;
  2408 
  2409   DEFVAR_LISP ("minibuffer-completion-table", Vminibuffer_completion_table,
  2410                doc: /* Alist or obarray used for completion in the minibuffer.
  2411 This becomes the ALIST argument to `try-completion' and `all-completions'.
  2412 The value can also be a list of strings or a hash table.
  2413 
  2414 The value may alternatively be a function, which is given three arguments:
  2415   STRING, the current buffer contents;
  2416   PREDICATE, the predicate for filtering possible matches;
  2417   CODE, which says what kind of things to do.
  2418 CODE can be nil, t or `lambda':
  2419   nil    -- return the best completion of STRING, or nil if there is none.
  2420   t      -- return a list of all possible completions of STRING.
  2421   lambda -- return t if STRING is a valid completion as it stands.  */);
  2422   Vminibuffer_completion_table = Qnil;
  2423 
  2424   DEFVAR_LISP ("minibuffer-completion-predicate", Vminibuffer_completion_predicate,
  2425                doc: /* Within call to `completing-read', this holds the PREDICATE argument.  */);
  2426   Vminibuffer_completion_predicate = Qnil;
  2427 
  2428   DEFVAR_LISP ("minibuffer-completion-confirm", Vminibuffer_completion_confirm,
  2429                doc: /* Whether to demand confirmation of completion before exiting minibuffer.
  2430 If nil, confirmation is not required.
  2431 If the value is `confirm', the user may exit with an input that is not
  2432  a valid completion alternative, but Emacs asks for confirmation.
  2433 If the value is `confirm-after-completion', the user may exit with an
  2434  input that is not a valid completion alternative, but Emacs asks for
  2435  confirmation if the user submitted the input right after any of the
  2436  completion commands listed in `minibuffer-confirm-exit-commands'.  */);
  2437   Vminibuffer_completion_confirm = Qnil;
  2438 
  2439   DEFVAR_LISP ("minibuffer-completing-file-name",
  2440                Vminibuffer_completing_file_name,
  2441                doc: /* Non-nil means completing file names.  */);
  2442   Vminibuffer_completing_file_name = Qnil;
  2443 
  2444   DEFVAR_LISP ("minibuffer-help-form", Vminibuffer_help_form,
  2445                doc: /* Value that `help-form' takes on inside the minibuffer.  */);
  2446   Vminibuffer_help_form = Qnil;
  2447 
  2448   DEFVAR_LISP ("minibuffer-history-variable", Vminibuffer_history_variable,
  2449                doc: /* History list symbol to add minibuffer values to.
  2450 Each string of minibuffer input, as it appears on exit from the minibuffer,
  2451 is added with
  2452 
  2453   (set minibuffer-history-variable
  2454        (cons STRING (symbol-value minibuffer-history-variable)))
  2455 
  2456  If the variable is t, no history is recorded.  */);
  2457   XSETFASTINT (Vminibuffer_history_variable, 0);
  2458 
  2459   DEFVAR_LISP ("minibuffer-history-position", Vminibuffer_history_position,
  2460                doc: /* Current position of redoing in the history list.  */);
  2461   Vminibuffer_history_position = Qnil;
  2462 
  2463   DEFVAR_BOOL ("minibuffer-auto-raise", minibuffer_auto_raise,
  2464                doc: /* Non-nil means entering the minibuffer raises the minibuffer's frame.
  2465 Some uses of the echo area also raise that frame (since they use it too).  */);
  2466   minibuffer_auto_raise = 0;
  2467 
  2468   DEFVAR_LISP ("completion-regexp-list", Vcompletion_regexp_list,
  2469                doc: /* List of regexps that should restrict possible completions.
  2470 The basic completion functions only consider a completion acceptable
  2471 if it matches all regular expressions in this list, with
  2472 `case-fold-search' bound to the value of `completion-ignore-case'.
  2473 See Info node `(elisp)Basic Completion', for a description of these
  2474 functions.
  2475 
  2476 Do not set this variable to a non-nil value globally, as that is not
  2477 safe and will probably cause errors in completion commands.  This
  2478 variable should be only let-bound to non-nil values around calls to
  2479 basic completion functions like `try-completion' and `all-completions'.  */);
  2480   Vcompletion_regexp_list = Qnil;
  2481 
  2482   DEFVAR_BOOL ("minibuffer-allow-text-properties",
  2483                minibuffer_allow_text_properties,
  2484                doc: /* Non-nil means `read-from-minibuffer' should not discard text properties.
  2485 This also affects `read-string', but it does not affect `read-minibuffer',
  2486 `read-no-blanks-input', or any of the functions that do minibuffer input
  2487 with completion; they always discard text properties.  */);
  2488   minibuffer_allow_text_properties = 0;
  2489 
  2490   DEFVAR_LISP ("minibuffer-prompt-properties", Vminibuffer_prompt_properties,
  2491                doc: /* Text properties that are added to minibuffer prompts.
  2492 These are in addition to the basic `field' property, and stickiness
  2493 properties.  */);
  2494   Vminibuffer_prompt_properties = list2 (Qread_only, Qt);
  2495 
  2496   DEFVAR_LISP ("read-hide-char", Vread_hide_char,
  2497                doc: /* Whether to hide input characters in noninteractive mode.
  2498 If non-nil, it must be a character, which will be used to mask the
  2499 input characters.  This variable should never be set globally.
  2500 
  2501 This variable also overrides the default character that `read-passwd'
  2502 uses to hide passwords.  */);
  2503   Vread_hide_char = Qnil;
  2504 
  2505   DEFVAR_BOOL ("inhibit-interaction",
  2506                inhibit_interaction,
  2507                doc: /* Non-nil means any user interaction will signal an error.
  2508 This variable can be bound when user interaction can't be performed,
  2509 for instance when running a headless Emacs server.  Functions like
  2510 `read-from-minibuffer' (and the like) will signal `inhibited-interaction'
  2511 instead. */);
  2512   inhibit_interaction = 0;
  2513 
  2514   DEFVAR_BOOL ("read-minibuffer-restore-windows", read_minibuffer_restore_windows,
  2515                doc: /* Non-nil means restore window configurations on exit from minibuffer.
  2516 If this is non-nil (the default), reading input with the minibuffer will
  2517 restore, on exit, the window configurations of the frame where the
  2518 minibuffer was entered from and, if it is different, the frame that owns
  2519 the associated minibuffer window.
  2520 
  2521 If this is nil, window configurations are not restored upon exiting
  2522 the minibuffer.  However, if `minibuffer-restore-windows' is present
  2523 in `minibuffer-exit-hook', exiting the minibuffer will remove the window
  2524 showing the *Completions* buffer, if any.  */);
  2525   read_minibuffer_restore_windows = true;
  2526 
  2527   defsubr (&Sactive_minibuffer_window);
  2528   defsubr (&Sset_minibuffer_window);
  2529   defsubr (&Sread_from_minibuffer);
  2530   defsubr (&Sread_string);
  2531   defsubr (&Sread_command);
  2532   defsubr (&Sread_variable);
  2533   defsubr (&Sinternal_complete_buffer);
  2534   defsubr (&Sread_buffer);
  2535   defsubr (&Sminibuffer_depth);
  2536   defsubr (&Sminibuffer_prompt);
  2537 
  2538   defsubr (&Sminibufferp);
  2539   defsubr (&Sinnermost_minibuffer_p);
  2540   defsubr (&Sminibuffer_innermost_command_loop_p);
  2541   defsubr (&Sabort_minibuffers);
  2542   defsubr (&Sminibuffer_prompt_end);
  2543   defsubr (&Sminibuffer_contents);
  2544   defsubr (&Sminibuffer_contents_no_properties);
  2545 
  2546   defsubr (&Stry_completion);
  2547   defsubr (&Sall_completions);
  2548   defsubr (&Stest_completion);
  2549   defsubr (&Sassoc_string);
  2550   defsubr (&Scompleting_read);
  2551 }

/* [<][>][^][v][top][bottom][index][help] */