minibuf.c 71.7 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Minibuffer input and completion.
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1985-1986, 1993-2017 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

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/>.  */
Jim Blandy's avatar
Jim Blandy committed
19 20


21
#include <config.h>
Paul Eggert's avatar
Paul Eggert committed
22
#include <errno.h>
23

24
#include <binary-io.h>
25

Jim Blandy's avatar
Jim Blandy committed
26
#include "lisp.h"
27
#include "character.h"
Jim Blandy's avatar
Jim Blandy committed
28
#include "buffer.h"
29
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
30
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
31
#include "window.h"
Stefan Monnier's avatar
Stefan Monnier committed
32
#include "keymap.h"
33
#include "sysstdio.h"
Michael Albinus's avatar
Michael Albinus committed
34 35
#include "systty.h"

Jim Blandy's avatar
Jim Blandy committed
36
/* List of buffers for use as minibuffers.
37 38 39 40
   The first element of the list is used for the outermost minibuffer
   invocation, the next element is used for a recursive minibuffer
   invocation, etc.  The list is extended at the end as deeper
   minibuffer recursions are encountered.  */
41

Jim Blandy's avatar
Jim Blandy committed
42 43
Lisp_Object Vminibuffer_list;

44
/* Data to remember during recursive minibuffer invocations.  */
45

46
static Lisp_Object minibuf_save_list;
Jim Blandy's avatar
Jim Blandy committed
47 48

/* Depth in minibuffer invocations.  */
49

50
EMACS_INT minibuf_level;
Jim Blandy's avatar
Jim Blandy committed
51

52
/* Fread_minibuffer leaves the input here as a string.  */
53

Jim Blandy's avatar
Jim Blandy committed
54 55
Lisp_Object last_minibuf_string;

56 57 58 59 60 61 62
/* Prompt to display in front of the mini-buffer contents.  */

static Lisp_Object minibuf_prompt;

/* Width of current mini-buffer prompt.  Only set after display_line
   of the line that contains the prompt.  */

63
static ptrdiff_t minibuf_prompt_width;
64

Jim Blandy's avatar
Jim Blandy committed
65

66 67 68 69
/* Put minibuf on currently selected frame's minibuffer.
   We do this whenever the user starts a new minibuffer
   or when a minibuffer exits.  */

70
static void
71
choose_minibuf_frame (void)
72
{
73 74
  if (FRAMEP (selected_frame)
      && FRAME_LIVE_P (XFRAME (selected_frame))
75
      && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
76
    {
77 78
      struct frame *sf = XFRAME (selected_frame);
      Lisp_Object buffer;
79

80 81
      /* I don't think that any frames may validly have a null minibuffer
	 window anymore.  */
82
      if (NILP (sf->minibuffer_window))
83
	emacs_abort ();
84

85 86 87
      /* Under X, we come here with minibuf_window being the
	 minibuffer window of the unused termcap window created in
	 init_window_once.  That window doesn't have a buffer.  */
88
      buffer = XWINDOW (minibuf_window)->contents;
89
      if (BUFFERP (buffer))
90 91 92
	/* Use set_window_buffer instead of Fset_window_buffer (see
	   discussion of bug#11984, bug#12025, bug#12026).  */
	set_window_buffer (sf->minibuffer_window, buffer, 0, 0);
93
      minibuf_window = sf->minibuffer_window;
94
    }
95 96 97

  /* Make sure no other frame has a minibuffer as its selected window,
     because the text would not be displayed in it, and that would be
98 99
     confusing.  Only allow the selected frame to do this,
     and that only if the minibuffer is active.  */
100 101 102 103
  {
    Lisp_Object tail, frame;

    FOR_EACH_FRAME (tail, frame)
104
      if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
105
	  && !(EQ (frame, selected_frame)
106
	       && minibuf_level > 0))
107
	Fset_frame_selected_window (frame, Fframe_first_window (frame), Qnil);
108
  }
109
}
110

111 112 113 114 115 116 117 118
DEFUN ("active-minibuffer-window", Factive_minibuffer_window,
       Sactive_minibuffer_window, 0, 0, 0,
       doc: /* Return the currently active minibuffer window, or nil if none.  */)
     (void)
{
  return minibuf_level ? minibuf_window : Qnil;
}

119 120
DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
       Sset_minibuffer_window, 1, 1, 0,
121
       doc: /* Specify which minibuffer window to use for the minibuffer.
Kenichi Handa's avatar
Kenichi Handa committed
122
This affects where the minibuffer is displayed if you put text in it
123
without invoking the usual minibuffer commands.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
124
  (Lisp_Object window)
125
{
126
  CHECK_WINDOW (window);
127 128 129 130 131 132 133 134
  if (! MINI_WINDOW_P (XWINDOW (window)))
    error ("Window is not a minibuffer window");

  minibuf_window = window;

  return window;
}

135

136
/* Actual minibuffer invocation.  */
Jim Blandy's avatar
Jim Blandy committed
137

138 139
static void read_minibuf_unwind (void);
static void run_exit_minibuf_hook (void);
Gerd Moellmann's avatar
Gerd Moellmann committed
140 141 142 143 144 145


/* Read a Lisp object from VAL and return it.  If VAL is an empty
   string, and DEFALT is a string, read from DEFALT instead of VAL.  */

static Lisp_Object
146
string_to_object (Lisp_Object val, Lisp_Object defalt)
Gerd Moellmann's avatar
Gerd Moellmann committed
147 148
{
  Lisp_Object expr_and_pos;
149
  ptrdiff_t pos;
150

151 152 153 154 155 156 157
  if (STRINGP (val) && SCHARS (val) == 0)
    {
      if (STRINGP (defalt))
	val = defalt;
      else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
	val = XCAR (defalt);
    }
158

Gerd Moellmann's avatar
Gerd Moellmann committed
159 160
  expr_and_pos = Fread_from_string (val, Qnil, Qnil);
  pos = XINT (Fcdr (expr_and_pos));
161
  if (pos != SCHARS (val))
Gerd Moellmann's avatar
Gerd Moellmann committed
162 163 164
    {
      /* Ignore trailing whitespace; any other trailing junk
	 is an error.  */
165
      ptrdiff_t i;
Gerd Moellmann's avatar
Gerd Moellmann committed
166
      pos = string_char_to_byte (val, pos);
167
      for (i = pos; i < SBYTES (val); i++)
Gerd Moellmann's avatar
Gerd Moellmann committed
168
	{
169
	  int c = SREF (val, i);
Gerd Moellmann's avatar
Gerd Moellmann committed
170 171 172 173
	  if (c != ' ' && c != '\t' && c != '\n')
	    error ("Trailing garbage following expression");
	}
    }
174

Gerd Moellmann's avatar
Gerd Moellmann committed
175
  val = Fcar (expr_and_pos);
176
  return val;
Gerd Moellmann's avatar
Gerd Moellmann committed
177 178 179 180 181 182 183
}


/* Like read_minibuf but reading from stdin.  This function is called
   from read_minibuf to do the job if noninteractive.  */

static Lisp_Object
Andreas Schwab's avatar
Andreas Schwab committed
184 185
read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
			     Lisp_Object prompt, Lisp_Object backup_n,
186
			     bool expflag,
Andreas Schwab's avatar
Andreas Schwab committed
187 188
			     Lisp_Object histvar, Lisp_Object histpos,
			     Lisp_Object defalt,
189
			     bool allow_props, bool inherit_input_method)
Gerd Moellmann's avatar
Gerd Moellmann committed
190
{
191
  ptrdiff_t size, len;
Paul Eggert's avatar
Paul Eggert committed
192
  char *line;
Gerd Moellmann's avatar
Gerd Moellmann committed
193
  Lisp_Object val;
Paul Eggert's avatar
Paul Eggert committed
194
  int c;
Michael Albinus's avatar
Michael Albinus committed
195
  unsigned char hide_char = 0;
196
  struct emacs_tty etty;
197
  bool etty_valid UNINIT;
Michael Albinus's avatar
Michael Albinus committed
198 199 200 201 202 203 204 205

  /* Check, whether we need to suppress echoing.  */
  if (CHARACTERP (Vread_hide_char))
    hide_char = XFASTINT (Vread_hide_char);

  /* Manipulate tty.  */
  if (hide_char)
    {
206
      etty_valid = emacs_get_tty (STDIN_FILENO, &etty) == 0;
207
      if (etty_valid)
208 209
	set_binary_mode (STDIN_FILENO, O_BINARY);
      suppress_echo_on_tty (STDIN_FILENO);
Michael Albinus's avatar
Michael Albinus committed
210
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
211

212 213
  fwrite_unlocked (SDATA (prompt), 1, SBYTES (prompt), stdout);
  fflush_unlocked (stdout);
Gerd Moellmann's avatar
Gerd Moellmann committed
214

215
  val = Qnil;
Gerd Moellmann's avatar
Gerd Moellmann committed
216 217
  size = 100;
  len = 0;
Dmitry Antipov's avatar
Dmitry Antipov committed
218
  line = xmalloc (size);
Paul Eggert's avatar
Paul Eggert committed
219

220
  while ((c = getchar_unlocked ()) != '\n' && c != '\r')
Gerd Moellmann's avatar
Gerd Moellmann committed
221
    {
Andreas Schwab's avatar
Andreas Schwab committed
222
      if (c == EOF)
Paul Eggert's avatar
Paul Eggert committed
223 224 225 226 227 228
	{
	  if (errno != EINTR)
	    break;
	}
      else
	{
Michael Albinus's avatar
Michael Albinus committed
229 230
	  if (hide_char)
	    fprintf (stdout, "%c", hide_char);
Paul Eggert's avatar
Paul Eggert committed
231
	  if (len == size)
232
	    line = xpalloc (line, &size, 1, -1, sizeof *line);
Paul Eggert's avatar
Paul Eggert committed
233 234
	  line[len++] = c;
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
235 236
    }

Michael Albinus's avatar
Michael Albinus committed
237 238 239 240
  /* Reset tty.  */
  if (hide_char)
    {
      fprintf (stdout, "\n");
241 242
      if (etty_valid)
	{
243 244
	  emacs_set_tty (STDIN_FILENO, &etty, 0);
	  set_binary_mode (STDIN_FILENO, O_TEXT);
245
	}
Michael Albinus's avatar
Michael Albinus committed
246 247
    }

248
  if (len || c == '\n' || c == '\r')
Gerd Moellmann's avatar
Gerd Moellmann committed
249
    {
Paul Eggert's avatar
Paul Eggert committed
250
      val = make_string (line, len);
Gerd Moellmann's avatar
Gerd Moellmann committed
251 252 253 254 255 256 257
      xfree (line);
    }
  else
    {
      xfree (line);
      error ("Error reading from stdin");
    }
258

259
  /* If Lisp form desired instead of string, parse it.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
260
  if (expflag)
261
    val = string_to_object (val, CONSP (defalt) ? XCAR (defalt) : defalt);
262

Gerd Moellmann's avatar
Gerd Moellmann committed
263 264
  return val;
}
265

266
DEFUN ("minibufferp", Fminibufferp,
267 268
       Sminibufferp, 0, 1, 0,
       doc: /* Return t if BUFFER is a minibuffer.
Kenichi Handa's avatar
Kenichi Handa committed
269 270
No argument or nil as argument means use current buffer as BUFFER.
BUFFER can be a buffer or a buffer name.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
271
  (Lisp_Object buffer)
272 273 274
{
  Lisp_Object tem;

275 276 277 278 279 280 281 282
  if (NILP (buffer))
    buffer = Fcurrent_buffer ();
  else if (STRINGP (buffer))
    buffer = Fget_buffer (buffer);
  else
    CHECK_BUFFER (buffer);

  tem = Fmemq (buffer, Vminibuffer_list);
283 284 285
  return ! NILP (tem) ? Qt : Qnil;
}

286 287
DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
       Sminibuffer_prompt_end, 0, 0, 0,
288
       doc: /* Return the buffer position of the end of the minibuffer prompt.
289
Return (point-min) if current buffer is not a minibuffer.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
290
  (void)
291 292
{
  /* This function is written to be most efficient when there's a prompt.  */
293 294 295 296 297 298 299 300
  Lisp_Object beg, end, tem;
  beg = make_number (BEGV);

  tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
  if (NILP (tem))
    return beg;

  end = Ffield_end (beg, Qnil, Qnil);
301

302
  if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
303
    return beg;
304 305 306 307 308 309
  else
    return end;
}

DEFUN ("minibuffer-contents", Fminibuffer_contents,
       Sminibuffer_contents, 0, 0, 0,
Pavel Janík's avatar
Pavel Janík committed
310
       doc: /* Return the user input in a minibuffer as a string.
311
If the current buffer is not a minibuffer, return its entire contents.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
312
  (void)
313
{
314
  ptrdiff_t prompt_end = XINT (Fminibuffer_prompt_end ());
315 316 317 318 319
  return make_buffer_string (prompt_end, ZV, 1);
}

DEFUN ("minibuffer-contents-no-properties", Fminibuffer_contents_no_properties,
       Sminibuffer_contents_no_properties, 0, 0, 0,
Pavel Janík's avatar
Pavel Janík committed
320
       doc: /* Return the user input in a minibuffer as a string, without text-properties.
321
If the current buffer is not a minibuffer, return its entire contents.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
322
  (void)
323
{
324
  ptrdiff_t prompt_end = XINT (Fminibuffer_prompt_end ());
325 326 327
  return make_buffer_string (prompt_end, ZV, 0);
}

328 329 330 331
DEFUN ("minibuffer-completion-contents", Fminibuffer_completion_contents,
       Sminibuffer_completion_contents, 0, 0, 0,
       doc: /* Return the user input in a minibuffer before point as a string.
That is what completion commands operate on.
332
If the current buffer is not a minibuffer, return its entire contents.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
333
  (void)
334
{
335
  ptrdiff_t prompt_end = XINT (Fminibuffer_prompt_end ());
336 337 338 339 340
  if (PT < prompt_end)
    error ("Cannot do completion in the prompt");
  return make_buffer_string (prompt_end, PT, 1);
}

341

Kenichi Handa's avatar
Kenichi Handa committed
342 343
/* Read from the minibuffer using keymap MAP and initial contents INITIAL,
   putting point minus BACKUP_N bytes from the end of INITIAL,
344
   prompting with PROMPT (a string), using history list HISTVAR
Kenichi Handa's avatar
Kenichi Handa committed
345 346 347 348 349 350
   with initial position HISTPOS.  INITIAL should be a string or a
   cons of a string and an integer.  BACKUP_N should be <= 0, or
   Qnil, which is equivalent to 0.  If INITIAL is a cons, BACKUP_N is
   ignored and replaced with an integer that puts point at one-indexed
   position N in INITIAL, where N is the CDR of INITIAL, or at the
   beginning of INITIAL if N <= 0.
351 352

   Normally return the result as a string (the text that was read),
353
   but if EXPFLAG, read it and return the object read.
354 355
   If HISTVAR is given, save the value read on that history only if it doesn't
   match the front of that history list exactly.  The value is pushed onto
356
   the list as the string that was read.
357

358
   DEFALT specifies the default value for the sake of history commands.
359

360
   If ALLOW_PROPS, do not throw away text properties.
361

362
   if INHERIT_INPUT_METHOD, the minibuffer inherits the
363
   current input method.  */
364 365

static Lisp_Object
Andreas Schwab's avatar
Andreas Schwab committed
366
read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
367
	      bool expflag,
Andreas Schwab's avatar
Andreas Schwab committed
368
	      Lisp_Object histvar, Lisp_Object histpos, Lisp_Object defalt,
369
	      bool allow_props, bool inherit_input_method)
Jim Blandy's avatar
Jim Blandy committed
370
{
371
  Lisp_Object val;
372
  ptrdiff_t count = SPECPDL_INDEX ();
373
  Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
374
  Lisp_Object enable_multibyte;
375
  EMACS_INT pos = 0;
376 377
  /* String to add to the history.  */
  Lisp_Object histstring;
378
  Lisp_Object histval;
379

380 381 382
  Lisp_Object empty_minibuf;
  Lisp_Object dummy, frame;

383
  specbind (Qminibuffer_default, defalt);
384
  specbind (Qinhibit_read_only, Qnil);
385

386 387 388 389
  /* If Vminibuffer_completing_file_name is `lambda' on entry, it was t
     in previous recursive minibuffer, but was not set explicitly
     to t for this invocation, so set it to nil in this minibuffer.
     Save the old value now, before we change it.  */
390 391
  specbind (intern ("minibuffer-completing-file-name"),
	    Vminibuffer_completing_file_name);
392 393 394
  if (EQ (Vminibuffer_completing_file_name, Qlambda))
    Vminibuffer_completing_file_name = Qnil;

395
#ifdef HAVE_WINDOW_SYSTEM
396 397
  if (display_hourglass_p)
    cancel_hourglass ();
398
#endif
399

Kenichi Handa's avatar
Kenichi Handa committed
400 401 402 403
  if (!NILP (initial))
    {
      if (CONSP (initial))
	{
404
	  Lisp_Object backup_n = XCDR (initial);
405
	  initial = XCAR (initial);
Kenichi Handa's avatar
Kenichi Handa committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	  CHECK_STRING (initial);
	  if (!NILP (backup_n))
	    {
	      CHECK_NUMBER (backup_n);
	      /* Convert to distance from end of input.  */
	      if (XINT (backup_n) < 1)
		/* A number too small means the beginning of the string.  */
		pos =  - SCHARS (initial);
	      else
		pos = XINT (backup_n) - 1 - SCHARS (initial);
	    }
	}
      else
	CHECK_STRING (initial);
    }
421
  val = Qnil;
Tom Tromey's avatar
Tom Tromey committed
422
  ambient_dir = BVAR (current_buffer, directory);
423
  input_method = Qnil;
424
  enable_multibyte = Qnil;
425

426
  if (!STRINGP (prompt))
427
    prompt = empty_unibyte_string;
Jim Blandy's avatar
Jim Blandy committed
428 429

  if (!enable_recursive_minibuffers
430 431 432 433 434 435 436 437 438
      && minibuf_level > 0)
    {
      if (EQ (selected_window, minibuf_window))
	error ("Command attempted to use minibuffer while in minibuffer");
      else
	/* If we're in another window, cancel the minibuffer that's active.  */
	Fthrow (Qexit,
		build_string ("Command attempted to use minibuffer while in minibuffer"));
    }
Jim Blandy's avatar
Jim Blandy committed
439

440 441 442
  if ((noninteractive
       /* In case we are running as a daemon, only do this before
	  detaching from the terminal.  */
443
       || (IS_DAEMON && DAEMON_RUNNING))
444
      && NILP (Vexecuting_kbd_macro))
445
    {
Kenichi Handa's avatar
Kenichi Handa committed
446 447
      val = read_minibuf_noninteractive (map, initial, prompt,
					 make_number (pos),
448 449 450 451
					 expflag, histvar, histpos, defalt,
					 allow_props, inherit_input_method);
      return unbind_to (count, val);
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
452

453
  /* Choose the minibuffer window and frame, and take action on them.  */
Jim Blandy's avatar
Jim Blandy committed
454

455 456
  /* Prepare for restoring the current buffer since choose_minibuf_frame
     calling Fset_frame_selected_window may change it (Bug#12766).  */
457
  record_unwind_protect (restore_buffer, Fcurrent_buffer ());
458

459 460
  choose_minibuf_frame ();

461
  record_unwind_protect_void (choose_minibuf_frame);
462

463
  record_unwind_protect (restore_window_configuration,
Jim Blandy's avatar
Jim Blandy committed
464 465
			 Fcurrent_window_configuration (Qnil));

Jim Blandy's avatar
Jim Blandy committed
466 467
  /* If the minibuffer window is on a different frame, save that
     frame's configuration too.  */
468
  mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
469
  if (!EQ (mini_frame, selected_frame))
470
    record_unwind_protect (restore_window_configuration,
471
			   Fcurrent_window_configuration (mini_frame));
472 473 474 475 476

  /* If the minibuffer is on an iconified or invisible frame,
     make it visible now.  */
  Fmake_frame_visible (mini_frame);

477 478
  if (minibuffer_auto_raise)
    Fraise_frame (mini_frame);
Jim Blandy's avatar
Jim Blandy committed
479

480
  temporarily_switch_to_single_kboard (XFRAME (mini_frame));
481

482 483 484 485 486 487 488 489
  /* We have to do this after saving the window configuration
     since that is what restores the current buffer.  */

  /* Arrange to restore a number of minibuffer-related variables.
     We could bind each variable separately, but that would use lots of
     specpdl slots.  */
  minibuf_save_list
    = Fcons (Voverriding_local_map,
490
	     Fcons (minibuf_window,
491
		    minibuf_save_list));
492 493 494 495 496 497 498 499
  minibuf_save_list
    = Fcons (minibuf_prompt,
	     Fcons (make_number (minibuf_prompt_width),
		    Fcons (Vhelp_form,
			   Fcons (Vcurrent_prefix_arg,
				  Fcons (Vminibuffer_history_position,
					 Fcons (Vminibuffer_history_variable,
						minibuf_save_list))))));
500 501
  minibuf_save_list
    = Fcons (Fthis_command_keys_vector (), minibuf_save_list);
502

503
  record_unwind_protect_void (read_minibuf_unwind);
504
  minibuf_level++;
505 506 507 508 509
  /* We are exiting the minibuffer one way or the other, so run the hook.
     It should be run before unwinding the minibuf settings.  Do it
     separately from read_minibuf_unwind because we need to make sure that
     read_minibuf_unwind is fully executed even if exit-minibuffer-hook
     signals an error.  --Stef  */
510
  record_unwind_protect_void (run_exit_minibuf_hook);
511 512 513

  /* Now that we can restore all those variables, start changing them.  */

514
  minibuf_prompt_width = 0;
515 516 517 518
  minibuf_prompt = Fcopy_sequence (prompt);
  Vminibuffer_history_position = histpos;
  Vminibuffer_history_variable = histvar;
  Vhelp_form = Vminibuffer_help_form;
519 520 521 522 523 524 525
  /* If this minibuffer is reading a file name, that doesn't mean
     recursive ones are.  But we cannot set it to nil, because
     completion code still need to know the minibuffer is completing a
     file name.  So use `lambda' as intermediate value meaning
     "t" in this minibuffer, but "nil" in next minibuffer.  */
  if (!NILP (Vminibuffer_completing_file_name))
    Vminibuffer_completing_file_name = Qlambda;
526

527
  /* If variable is unbound, make it nil.  */
528 529 530 531 532 533
  histval = find_symbol_value (Vminibuffer_history_variable);
  if (EQ (histval, Qunbound))
    {
      Fset (Vminibuffer_history_variable, Qnil);
      histval = Qnil;
    }
534

535
  if (inherit_input_method)
536
    {
537
      /* `current-input-method' is buffer local.  So, remember it in
538 539
	 INPUT_METHOD before changing the current buffer.  */
      input_method = Fsymbol_value (Qcurrent_input_method);
Tom Tromey's avatar
Tom Tromey committed
540
      enable_multibyte = BVAR (current_buffer, enable_multibyte_characters);
541
    }
542

543 544
  /* Switch to the minibuffer.  */

545 546
  minibuffer = get_minibuffer (minibuf_level);
  Fset_buffer (minibuffer);
547

548 549
  /* Defeat (setq-default truncate-lines t), since truncated lines do
     not work correctly in minibuffers.  (Bug#5715, etc)  */
Paul Eggert's avatar
Paul Eggert committed
550
  bset_truncate_lines (current_buffer, Qnil);
551

552 553
  /* If appropriate, copy enable-multibyte-characters into the minibuffer.  */
  if (inherit_input_method)
Paul Eggert's avatar
Paul Eggert committed
554
    bset_enable_multibyte_characters (current_buffer, enable_multibyte);
555

556 557 558 559 560 561 562 563
  /* The current buffer's default directory is usually the right thing
     for our minibuffer here.  However, if you're typing a command at
     a minibuffer-only frame when minibuf_level is zero, then buf IS
     the current_buffer, so reset_buffer leaves buf's default
     directory unchanged.  This is a bummer when you've just started
     up Emacs and buf's default directory is Qnil.  Here's a hack; can
     you think of something better to do?  Find another buffer with a
     better directory, and use that one instead.  */
564
  if (STRINGP (ambient_dir))
Paul Eggert's avatar
Paul Eggert committed
565
    bset_directory (current_buffer, ambient_dir);
566 567
  else
    {
568
      Lisp_Object tail, buf;
569

570 571 572 573 574 575 576
      FOR_EACH_LIVE_BUFFER (tail, buf)
	if (STRINGP (BVAR (XBUFFER (buf), directory)))
	  {
	    bset_directory (current_buffer,
			    BVAR (XBUFFER (buf), directory));
	    break;
	  }
577 578
    }

579 580
  if (!EQ (mini_frame, selected_frame))
    Fredirect_frame_focus (selected_frame, mini_frame);
Jim Blandy's avatar
Jim Blandy committed
581

Jim Blandy's avatar
Jim Blandy committed
582
  Vminibuf_scroll_window = selected_window;
583
  if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
584
    minibuf_selected_window = selected_window;
585 586 587 588

  /* Empty out the minibuffers of all frames other than the one
     where we are going to display one now.
     Set them to point to ` *Minibuf-0*', which is always empty.  */
589
  empty_minibuf = get_minibuffer (0);
590 591 592 593

  FOR_EACH_FRAME (dummy, frame)
    {
      Lisp_Object root_window = Fframe_root_window (frame);
594
      Lisp_Object mini_window = XWINDOW (root_window)->next;
595

596 597
      if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
	  && !NILP (Fwindow_minibuffer_p (mini_window)))
598 599 600
	/* Use set_window_buffer instead of Fset_window_buffer (see
	   discussion of bug#11984, bug#12025, bug#12026).  */
	set_window_buffer (mini_window, empty_minibuf, 0, 0);
601 602 603
    }

  /* Display this minibuffer in the proper window.  */
604 605 606
  /* Use set_window_buffer instead of Fset_window_buffer (see
     discussion of bug#11984, bug#12025, bug#12026).  */
  set_window_buffer (minibuf_window, Fcurrent_buffer (), 0, 0);
607
  Fselect_window (minibuf_window, Qnil);
608
  XWINDOW (minibuf_window)->hscroll = 0;
609
  XWINDOW (minibuf_window)->suspend_auto_hscroll = 0;
Jim Blandy's avatar
Jim Blandy committed
610

611 612 613 614
  Fmake_local_variable (Qprint_escape_newlines);
  print_escape_newlines = 1;

  /* Erase the buffer.  */
615
  {
616
    ptrdiff_t count1 = SPECPDL_INDEX ();
617
    specbind (Qinhibit_read_only, Qt);
618
    specbind (Qinhibit_modification_hooks, Qt);
619
    Ferase_buffer ();
620

Tom Tromey's avatar
Tom Tromey committed
621
    if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
622 623
	&& ! STRING_MULTIBYTE (minibuf_prompt))
      minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
624

625 626 627 628 629 630 631 632 633 634
    /* Insert the prompt, record where it ends.  */
    Finsert (1, &minibuf_prompt);
    if (PT > BEG)
      {
	Fput_text_property (make_number (BEG), make_number (PT),
			    Qfront_sticky, Qt, Qnil);
	Fput_text_property (make_number (BEG), make_number (PT),
			    Qrear_nonsticky, Qt, Qnil);
	Fput_text_property (make_number (BEG), make_number (PT),
			    Qfield, Qt, Qnil);
635
	if (CONSP (Vminibuffer_prompt_properties))
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	  {
	    /* We want to apply all properties from
	       `minibuffer-prompt-properties' to the region normally,
	       but if the `face' property is present, add that
	       property to the end of the face properties to avoid
	       overwriting faces. */
	    Lisp_Object list = Vminibuffer_prompt_properties;
	    while (CONSP (list))
	      {
		Lisp_Object key = XCAR (list);
		list = XCDR (list);
		if (CONSP (list))
		  {
		    Lisp_Object val = XCAR (list);
		    list = XCDR (list);
		    if (EQ (key, Qface))
		      Fadd_face_text_property (make_number (BEG),
					       make_number (PT), val, Qt, Qnil);
		    else
		      Fput_text_property (make_number (BEG), make_number (PT),
					  key, val, Qnil);
		  }
	      }
	  }
660
      }
661 662 663
    unbind_to (count1, Qnil);
  }

664
  minibuf_prompt_width = current_column ();
665

666
  /* Put in the initial input.  */
Jim Blandy's avatar
Jim Blandy committed
667
  if (!NILP (initial))
Jim Blandy's avatar
Jim Blandy committed
668 669
    {
      Finsert (1, &initial);
Kenichi Handa's avatar
Kenichi Handa committed
670
      Fforward_char (make_number (pos));
Jim Blandy's avatar
Jim Blandy committed
671 672
    }

673
  clear_message (1, 1);
Paul Eggert's avatar
Paul Eggert committed
674
  bset_keymap (current_buffer, map);
Jim Blandy's avatar
Jim Blandy committed
675

676
  /* Turn on an input method stored in INPUT_METHOD if any.  */
677
  if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
678 679
    call1 (Qactivate_input_method, input_method);

680
  run_hook (Qminibuffer_setup_hook);
681

682
  /* Don't allow the user to undo past this point.  */
Paul Eggert's avatar
Paul Eggert committed
683
  bset_undo_list (current_buffer, Qnil);
684

Jim Blandy's avatar
Jim Blandy committed
685 686 687 688
  recursive_edit_1 ();

  /* If cursor is on the minibuffer line,
     show the user we have exited by putting it in column 0.  */
689
  if (XWINDOW (minibuf_window)->cursor.vpos >= 0
Jim Blandy's avatar
Jim Blandy committed
690 691
      && !noninteractive)
    {
692 693
      XWINDOW (minibuf_window)->cursor.hpos = 0;
      XWINDOW (minibuf_window)->cursor.x = 0;
694 695
      XWINDOW (minibuf_window)->must_be_updated_p = true;
      update_frame (XFRAME (selected_frame), true, true);
Dmitry Antipov's avatar
Dmitry Antipov committed
696
      flush_frame (XFRAME (XWINDOW (minibuf_window)->frame));
Jim Blandy's avatar
Jim Blandy committed
697 698
    }

699
  /* Make minibuffer contents into a string.  */
700
  Fset_buffer (minibuffer);
701
  if (allow_props)
702
    val = Fminibuffer_contents ();
703
  else
704
    val = Fminibuffer_contents_no_properties ();
705

706
  /* VAL is the string of minibuffer text.  */
707

708 709
  last_minibuf_string = val;

710
  /* Choose the string to add to the history.  */
711
  if (SCHARS (val) != 0)
712 713 714
    histstring = val;
  else if (STRINGP (defalt))
    histstring = defalt;
715 716
  else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
    histstring = XCAR (defalt);
717 718 719 720
  else
    histstring = Qnil;

  /* Add the value to the appropriate history list, if any.  */
721 722
  if (!NILP (Vhistory_add_new_input)
      && SYMBOLP (Vminibuffer_history_variable)
723
      && !NILP (histstring))
724 725 726 727 728 729
    {
      /* If the caller wanted to save the value read on a history list,
	 then do so if the value is not already the front of the list.  */

      /* The value of the history variable must be a cons or nil.  Other
	 values are unacceptable.  We silently ignore these values.  */
730

731
      if (NILP (histval)
732
	  || (CONSP (histval)
733
	      /* Don't duplicate the most recent entry in the history.  */
734
	      && (NILP (Fequal (histstring, Fcar (histval))))))
735 736 737
	{
	  Lisp_Object length;

738
	  if (history_delete_duplicates) Fdelete (histstring, histval);
739
	  histval = Fcons (histstring, histval);
740 741 742 743 744
	  Fset (Vminibuffer_history_variable, histval);

	  /* Truncate if requested.  */
	  length = Fget (Vminibuffer_history_variable, Qhistory_length);
	  if (NILP (length)) length = Vhistory_length;
745 746 747 748 749 750 751 752 753 754 755 756
	  if (INTEGERP (length))
	    {
	      if (XINT (length) <= 0)
		Fset (Vminibuffer_history_variable, Qnil);
	      else
		{
		  Lisp_Object temp;

		  temp = Fnthcdr (Fsub1 (length), histval);
		  if (CONSP (temp)) Fsetcdr (temp, Qnil);
		}
	    }
757
	}
758 759
    }

760
  /* If Lisp form desired instead of string, parse it.  */
761
  if (expflag)
Gerd Moellmann's avatar
Gerd Moellmann committed
762
    val = string_to_object (val, defalt);
763

764 765
  /* The appropriate frame will get selected
     in set-window-configuration.  */
766
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
767 768 769
}

/* Return a buffer to be used as the minibuffer at depth `depth'.
770 771
   depth = 0 is the lowest allowed argument, and that is the value
   used for nonrecursive minibuffer invocations.  */
Jim Blandy's avatar
Jim Blandy committed
772 773

Lisp_Object
774
get_minibuffer (EMACS_INT depth)
Jim Blandy's avatar
Jim Blandy committed
775
{
776
  Lisp_Object tail = Fnthcdr (make_number (depth), Vminibuffer_list);
Jim Blandy's avatar
Jim Blandy committed
777
  if (NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
778
    {
779
      tail = list1 (Qnil);
Jim Blandy's avatar
Jim Blandy committed
780 781
      Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
    }
782
  Lisp_Object buf = Fcar (tail);
783
  if (NILP (buf) || !BUFFER_LIVE_P (XBUFFER (buf)))
Jim Blandy's avatar
Jim Blandy committed
784
    {
785 786 787 788
      static char const name_fmt[] = " *Minibuf-%"pI"d*";
      char name[sizeof name_fmt + INT_STRLEN_BOUND (EMACS_INT)];
      AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, depth));
      buf = Fget_buffer_create (lname);
789 790 791 792 793

      /* Although the buffer's name starts with a space, undo should be
	 enabled in it.  */
      Fbuffer_enable_undo (buf);

794
      XSETCAR (tail, buf);
Jim Blandy's avatar
Jim Blandy committed
795 796
    }
  else
797
    {
798
      ptrdiff_t count = SPECPDL_INDEX ();
799 800 801
      /* We have to empty both overlay lists.  Otherwise we end
	 up with overlays that think they belong to this buffer
	 while the buffer doesn't know about them any more.  */
Stefan Monnier's avatar
Stefan Monnier committed
802 803
      delete_all_overlays (XBUFFER (buf));
      reset_buffer (XBUFFER (buf));
804
      record_unwind_current_buffer ();
Richard M. Stallman's avatar