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

3
Copyright (C) 1985-1986, 1993-2011  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 <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19 20


21
#include <config.h>
22
#include <stdio.h>
23
#include <setjmp.h>
24

Jim Blandy's avatar
Jim Blandy committed
25 26 27 28
#include "lisp.h"
#include "commands.h"
#include "buffer.h"
#include "dispextern.h"
29
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
30
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
31 32
#include "window.h"
#include "syntax.h"
33
#include "intervals.h"
Stefan Monnier's avatar
Stefan Monnier committed
34
#include "keymap.h"
35
#include "termhooks.h"
Jim Blandy's avatar
Jim Blandy committed
36 37

/* List of buffers for use as minibuffers.
38 39 40 41
   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.  */
42

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

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

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

/* Depth in minibuffer invocations.  */
50

Jim Blandy's avatar
Jim Blandy committed
51 52
int minibuf_level;

53
/* The maximum length of a minibuffer history.  */
54

55
static Lisp_Object Qhistory_length;
56

57
/* Fread_minibuffer leaves the input here as a string.  */
58

Jim Blandy's avatar
Jim Blandy committed
59 60
Lisp_Object last_minibuf_string;

61
static Lisp_Object Qminibuffer_history, Qbuffer_name_history;
62

63
static Lisp_Object Qread_file_name_internal;
64

65
/* Normal hooks for entry to and exit from minibuffer.  */
66

67 68
static Lisp_Object Qminibuffer_setup_hook;
static Lisp_Object Qminibuffer_exit_hook;
Karl Heuer's avatar
Karl Heuer committed
69

70
Lisp_Object Qcompletion_ignore_case;
71 72 73 74
static Lisp_Object Qminibuffer_completion_table;
static Lisp_Object Qminibuffer_completion_predicate;
static Lisp_Object Qminibuffer_completion_confirm;
static Lisp_Object Quser_variable_p;
75

76
static Lisp_Object Qminibuffer_default;
77

78
static Lisp_Object Qcurrent_input_method, Qactivate_input_method;
79

80
static Lisp_Object Qcase_fold_search;
Kenichi Handa's avatar
Kenichi Handa committed
81

82
static Lisp_Object Qread_expression_history;
83

84 85 86 87 88 89 90
/* 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.  */

91
static EMACS_INT minibuf_prompt_width;
92

Jim Blandy's avatar
Jim Blandy committed
93

94 95 96 97
/* Put minibuf on currently selected frame's minibuffer.
   We do this whenever the user starts a new minibuffer
   or when a minibuffer exits.  */

98
static void
99
choose_minibuf_frame (void)
100
{
101 102 103
  if (FRAMEP (selected_frame)
      && FRAME_LIVE_P (XFRAME (selected_frame))
      && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
104
    {
105 106
      struct frame *sf = XFRAME (selected_frame);
      Lisp_Object buffer;
107

108 109
      /* I don't think that any frames may validly have a null minibuffer
	 window anymore.  */
110
      if (NILP (sf->minibuffer_window))
111 112
	abort ();

113 114 115 116 117
      /* 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.  */
      buffer = XWINDOW (minibuf_window)->buffer;
      if (BUFFERP (buffer))
118
	Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
119
      minibuf_window = sf->minibuffer_window;
120
    }
121 122 123

  /* 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
124 125
     confusing.  Only allow the selected frame to do this,
     and that only if the minibuffer is active.  */
126 127 128 129
  {
    Lisp_Object tail, frame;

    FOR_EACH_FRAME (tail, frame)
130
      if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
131
	  && !(EQ (frame, selected_frame)
132
	       && minibuf_level > 0))
133
	Fset_frame_selected_window (frame, Fframe_first_window (frame), Qnil);
134
  }
135
}
136

137
static Lisp_Object
138
choose_minibuf_frame_1 (Lisp_Object ignore)
139 140 141 142 143
{
  choose_minibuf_frame ();
  return Qnil;
}

144 145 146 147 148 149 150 151
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;
}

152 153
DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
       Sset_minibuffer_window, 1, 1, 0,
154
       doc: /* Specify which minibuffer window to use for the minibuffer.
Kenichi Handa's avatar
Kenichi Handa committed
155
This affects where the minibuffer is displayed if you put text in it
156
without invoking the usual minibuffer commands.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
157
  (Lisp_Object window)
158
{
159
  CHECK_WINDOW (window);
160 161 162 163 164 165 166 167
  if (! MINI_WINDOW_P (XWINDOW (window)))
    error ("Window is not a minibuffer window");

  minibuf_window = window;

  return window;
}

168

169
/* Actual minibuffer invocation.  */
Jim Blandy's avatar
Jim Blandy committed
170

171 172 173 174 175 176 177 178 179 180 181 182 183
static Lisp_Object read_minibuf_unwind (Lisp_Object);
static Lisp_Object run_exit_minibuf_hook (Lisp_Object);
static Lisp_Object read_minibuf (Lisp_Object, Lisp_Object,
                                 Lisp_Object, Lisp_Object,
                                 int, Lisp_Object,
                                 Lisp_Object, Lisp_Object,
                                 int, int);
static Lisp_Object read_minibuf_noninteractive (Lisp_Object, Lisp_Object,
                                                Lisp_Object, Lisp_Object,
                                                int, Lisp_Object,
                                                Lisp_Object, Lisp_Object,
                                                int, int);
static Lisp_Object string_to_object (Lisp_Object, Lisp_Object);
Gerd Moellmann's avatar
Gerd Moellmann committed
184 185 186 187 188 189


/* 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
190
string_to_object (Lisp_Object val, Lisp_Object defalt)
Gerd Moellmann's avatar
Gerd Moellmann committed
191 192 193
{
  struct gcpro gcpro1, gcpro2;
  Lisp_Object expr_and_pos;
194
  EMACS_INT pos;
195

Gerd Moellmann's avatar
Gerd Moellmann committed
196
  GCPRO2 (val, defalt);
197

198 199 200 201 202 203 204
  if (STRINGP (val) && SCHARS (val) == 0)
    {
      if (STRINGP (defalt))
	val = defalt;
      else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
	val = XCAR (defalt);
    }
205

Gerd Moellmann's avatar
Gerd Moellmann committed
206 207
  expr_and_pos = Fread_from_string (val, Qnil, Qnil);
  pos = XINT (Fcdr (expr_and_pos));
208
  if (pos != SCHARS (val))
Gerd Moellmann's avatar
Gerd Moellmann committed
209 210 211
    {
      /* Ignore trailing whitespace; any other trailing junk
	 is an error.  */
212
      EMACS_INT i;
Gerd Moellmann's avatar
Gerd Moellmann committed
213
      pos = string_char_to_byte (val, pos);
214
      for (i = pos; i < SBYTES (val); i++)
Gerd Moellmann's avatar
Gerd Moellmann committed
215
	{
216
	  int c = SREF (val, i);
Gerd Moellmann's avatar
Gerd Moellmann committed
217 218 219 220
	  if (c != ' ' && c != '\t' && c != '\n')
	    error ("Trailing garbage following expression");
	}
    }
221

Gerd Moellmann's avatar
Gerd Moellmann committed
222 223 224 225 226 227 228 229 230
  val = Fcar (expr_and_pos);
  RETURN_UNGCPRO (val);
}


/* 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
231 232 233 234 235 236
read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
			     Lisp_Object prompt, Lisp_Object backup_n,
			     int expflag,
			     Lisp_Object histvar, Lisp_Object histpos,
			     Lisp_Object defalt,
			     int allow_props, int inherit_input_method)
Gerd Moellmann's avatar
Gerd Moellmann committed
237
{
238
  ptrdiff_t size, len;
Gerd Moellmann's avatar
Gerd Moellmann committed
239 240 241
  char *line, *s;
  Lisp_Object val;

242
  fprintf (stdout, "%s", SDATA (prompt));
Gerd Moellmann's avatar
Gerd Moellmann committed
243 244
  fflush (stdout);

245
  val = Qnil;
Gerd Moellmann's avatar
Gerd Moellmann committed
246 247
  size = 100;
  len = 0;
248
  line = (char *) xmalloc (size);
Gerd Moellmann's avatar
Gerd Moellmann committed
249 250 251 252
  while ((s = fgets (line + len, size - len, stdin)) != NULL
	 && (len = strlen (line),
	     len == size - 1 && line[len - 1] != '\n'))
    {
253
      if (STRING_BYTES_BOUND / 2 < size)
Paul Eggert's avatar
Paul Eggert committed
254
	memory_full (SIZE_MAX);
Gerd Moellmann's avatar
Gerd Moellmann committed
255 256 257 258 259 260
      size *= 2;
      line = (char *) xrealloc (line, size);
    }

  if (s)
    {
261 262 263
      char *nl = strchr (line, '\n');
      if (nl)
	*nl = '\0';
Gerd Moellmann's avatar
Gerd Moellmann committed
264 265 266 267 268 269 270 271
      val = build_string (line);
      xfree (line);
    }
  else
    {
      xfree (line);
      error ("Error reading from stdin");
    }
272

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

Gerd Moellmann's avatar
Gerd Moellmann committed
277 278
  return val;
}
279

280
DEFUN ("minibufferp", Fminibufferp,
281 282
       Sminibufferp, 0, 1, 0,
       doc: /* Return t if BUFFER is a minibuffer.
Kenichi Handa's avatar
Kenichi Handa committed
283 284
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
285
  (Lisp_Object buffer)
286 287 288
{
  Lisp_Object tem;

289 290 291 292 293 294 295 296
  if (NILP (buffer))
    buffer = Fcurrent_buffer ();
  else if (STRINGP (buffer))
    buffer = Fget_buffer (buffer);
  else
    CHECK_BUFFER (buffer);

  tem = Fmemq (buffer, Vminibuffer_list);
297 298 299
  return ! NILP (tem) ? Qt : Qnil;
}

300 301
DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
       Sminibuffer_prompt_end, 0, 0, 0,
302
       doc: /* Return the buffer position of the end of the minibuffer prompt.
303
Return (point-min) if current buffer is not a minibuffer.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
304
  (void)
305 306
{
  /* This function is written to be most efficient when there's a prompt.  */
307 308 309 310 311 312 313 314
  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);
315

316
  if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
317
    return beg;
318 319 320 321 322 323
  else
    return end;
}

DEFUN ("minibuffer-contents", Fminibuffer_contents,
       Sminibuffer_contents, 0, 0, 0,
Pavel Janík's avatar
Pavel Janík committed
324
       doc: /* Return the user input in a minibuffer as a string.
325
If the current buffer is not a minibuffer, return its entire contents.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
326
  (void)
327
{
328
  EMACS_INT prompt_end = XINT (Fminibuffer_prompt_end ());
329 330 331 332 333
  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
334
       doc: /* Return the user input in a minibuffer as a string, without text-properties.
335
If the current buffer is not a minibuffer, return its entire contents.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
336
  (void)
337
{
338
  EMACS_INT prompt_end = XINT (Fminibuffer_prompt_end ());
339 340 341
  return make_buffer_string (prompt_end, ZV, 0);
}

342 343 344 345
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.
346
If the current buffer is not a minibuffer, return its entire contents.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
347
  (void)
348
{
349
  EMACS_INT prompt_end = XINT (Fminibuffer_prompt_end ());
350 351 352 353 354
  if (PT < prompt_end)
    error ("Cannot do completion in the prompt");
  return make_buffer_string (prompt_end, PT, 1);
}

355

Kenichi Handa's avatar
Kenichi Handa committed
356 357
/* Read from the minibuffer using keymap MAP and initial contents INITIAL,
   putting point minus BACKUP_N bytes from the end of INITIAL,
358
   prompting with PROMPT (a string), using history list HISTVAR
Kenichi Handa's avatar
Kenichi Handa committed
359 360 361 362 363 364
   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.
365 366

   Normally return the result as a string (the text that was read),
367
   but if EXPFLAG is nonzero, read it and return the object read.
368 369
   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
370
   the list as the string that was read.
371

372
   DEFALT specifies the default value for the sake of history commands.
373

374 375
   If ALLOW_PROPS is nonzero, we do not throw away text properties.

Kenichi Handa's avatar
Kenichi Handa committed
376
   if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
377
   current input method.  */
378 379

static Lisp_Object
Andreas Schwab's avatar
Andreas Schwab committed
380 381 382 383
read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
	      Lisp_Object backup_n, int expflag,
	      Lisp_Object histvar, Lisp_Object histpos, Lisp_Object defalt,
	      int allow_props, int inherit_input_method)
Jim Blandy's avatar
Jim Blandy committed
384
{
385
  Lisp_Object val;
Juanma Barranquero's avatar
Juanma Barranquero committed
386
  int count = SPECPDL_INDEX ();
387 388
  Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
389
  Lisp_Object enable_multibyte;
Kenichi Handa's avatar
Kenichi Handa committed
390
  int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
391 392 393
  /* String to add to the history.  */
  Lisp_Object histstring;

394 395 396
  Lisp_Object empty_minibuf;
  Lisp_Object dummy, frame;

397 398
  specbind (Qminibuffer_default, defalt);

399 400 401 402 403 404 405 406
  /* 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.  */
  specbind (intern ("minibuffer-completing-file-name"), Vminibuffer_completing_file_name);
  if (EQ (Vminibuffer_completing_file_name, Qlambda))
    Vminibuffer_completing_file_name = Qnil;

407
#ifdef HAVE_WINDOW_SYSTEM
408 409
  if (display_hourglass_p)
    cancel_hourglass ();
410
#endif
411

Kenichi Handa's avatar
Kenichi Handa committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  if (!NILP (initial))
    {
      if (CONSP (initial))
	{
	  backup_n = Fcdr (initial);
	  initial = Fcar (initial);
	  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);
    }
433
  val = Qnil;
Tom Tromey's avatar
Tom Tromey committed
434
  ambient_dir = BVAR (current_buffer, directory);
435
  input_method = Qnil;
436
  enable_multibyte = Qnil;
437

438 439 440
  /* Don't need to protect PROMPT, HISTVAR, and HISTPOS because we
     store them away before we can GC.  Don't need to protect
     BACKUP_N because we use the value only if it is an integer.  */
441
  GCPRO5 (map, initial, val, ambient_dir, input_method);
Jim Blandy's avatar
Jim Blandy committed
442

443
  if (!STRINGP (prompt))
444
    prompt = empty_unibyte_string;
Jim Blandy's avatar
Jim Blandy committed
445 446

  if (!enable_recursive_minibuffers
447 448 449 450 451 452 453 454 455
      && 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
456

457 458 459 460 461
  if ((noninteractive
       /* In case we are running as a daemon, only do this before
	  detaching from the terminal.  */
       || (IS_DAEMON && (daemon_pipe[1] >= 0)))
      && NILP (Vexecuting_kbd_macro))
462
    {
Kenichi Handa's avatar
Kenichi Handa committed
463 464
      val = read_minibuf_noninteractive (map, initial, prompt,
					 make_number (pos),
465 466
					 expflag, histvar, histpos, defalt,
					 allow_props, inherit_input_method);
467
      UNGCPRO;
468 469
      return unbind_to (count, val);
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
470

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

473 474
  choose_minibuf_frame ();

475 476
  record_unwind_protect (choose_minibuf_frame_1, Qnil);

Jim Blandy's avatar
Jim Blandy committed
477
  record_unwind_protect (Fset_window_configuration,
Jim Blandy's avatar
Jim Blandy committed
478 479
			 Fcurrent_window_configuration (Qnil));

Jim Blandy's avatar
Jim Blandy committed
480 481
  /* If the minibuffer window is on a different frame, save that
     frame's configuration too.  */
482
  mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
483
  if (!EQ (mini_frame, selected_frame))
484 485
    record_unwind_protect (Fset_window_configuration,
			   Fcurrent_window_configuration (mini_frame));
486 487 488 489 490

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

491 492
  if (minibuffer_auto_raise)
    Fraise_frame (mini_frame);
Jim Blandy's avatar
Jim Blandy committed
493

494
  temporarily_switch_to_single_kboard (XFRAME (mini_frame));
495

496 497 498 499 500 501 502 503
  /* 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,
504
	     Fcons (minibuf_window,
505
		    minibuf_save_list));
506 507 508 509 510 511 512 513 514 515 516
  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))))));

  record_unwind_protect (read_minibuf_unwind, Qnil);
  minibuf_level++;
517 518 519 520 521 522
  /* 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  */
  record_unwind_protect (run_exit_minibuf_hook, Qnil);
523 524 525

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

526
  minibuf_prompt_width = 0;
527 528 529 530
  minibuf_prompt = Fcopy_sequence (prompt);
  Vminibuffer_history_position = histpos;
  Vminibuffer_history_variable = histvar;
  Vhelp_form = Vminibuffer_help_form;
531 532 533 534 535 536 537
  /* 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;
538

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

547 548
  /* Switch to the minibuffer.  */

549 550
  minibuffer = get_minibuffer (minibuf_level);
  Fset_buffer (minibuffer);
551

552 553
  /* If appropriate, copy enable-multibyte-characters into the minibuffer.  */
  if (inherit_input_method)
Tom Tromey's avatar
Tom Tromey committed
554
    BVAR (current_buffer, enable_multibyte_characters) = 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))
Tom Tromey's avatar
Tom Tromey committed
565
    BVAR (current_buffer, directory) = ambient_dir;
566 567 568 569 570 571
  else
    {
      Lisp_Object buf_list;

      for (buf_list = Vbuffer_alist;
	   CONSP (buf_list);
572
	   buf_list = XCDR (buf_list))
573
	{
574
	  Lisp_Object other_buf;
575

576
	  other_buf = XCDR (XCAR (buf_list));
Tom Tromey's avatar
Tom Tromey committed
577
	  if (STRINGP (BVAR (XBUFFER (other_buf), directory)))
578
	    {
Tom Tromey's avatar
Tom Tromey committed
579
	      BVAR (current_buffer, directory) = BVAR (XBUFFER (other_buf), directory);
580 581 582 583 584
	      break;
	    }
	}
    }

585 586
  if (!EQ (mini_frame, selected_frame))
    Fredirect_frame_focus (selected_frame, mini_frame);
Jim Blandy's avatar
Jim Blandy committed
587

Jim Blandy's avatar
Jim Blandy committed
588
  Vminibuf_scroll_window = selected_window;
589
  if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
590
    minibuf_selected_window = selected_window;
591 592 593 594

  /* 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.  */
595
  empty_minibuf = get_minibuffer (0);
596 597 598 599 600 601

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

602 603
      if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
	  && !NILP (Fwindow_minibuffer_p (mini_window)))
604 605 606 607
	Fset_window_buffer (mini_window, empty_minibuf, Qnil);
    }

  /* Display this minibuffer in the proper window.  */
608
  Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
609
  Fselect_window (minibuf_window, Qnil);
610
  XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
Jim Blandy's avatar
Jim Blandy committed
611

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

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

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

626 627 628 629 630 631 632 633 634 635 636 637 638
    /* 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);
	Fadd_text_properties (make_number (BEG), make_number (PT),
			      Vminibuffer_prompt_properties, Qnil);
      }
639 640 641
    unbind_to (count1, Qnil);
  }

642
  minibuf_prompt_width = current_column ();
643

644
  /* Put in the initial input.  */
Jim Blandy's avatar
Jim Blandy committed
645
  if (!NILP (initial))
Jim Blandy's avatar
Jim Blandy committed
646 647
    {
      Finsert (1, &initial);
Kenichi Handa's avatar
Kenichi Handa committed
648
      Fforward_char (make_number (pos));
Jim Blandy's avatar
Jim Blandy committed
649 650
    }

651
  clear_message (1, 1);
Tom Tromey's avatar
Tom Tromey committed
652
  BVAR (current_buffer, keymap) = map;
Jim Blandy's avatar
Jim Blandy committed
653

654
  /* Turn on an input method stored in INPUT_METHOD if any.  */
655
  if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
656 657
    call1 (Qactivate_input_method, input_method);

658
  Frun_hooks (1, &Qminibuffer_setup_hook);
659

660
  /* Don't allow the user to undo past this point.  */
Tom Tromey's avatar
Tom Tromey committed
661
  BVAR (current_buffer, undo_list) = Qnil;
662

Jim Blandy's avatar
Jim Blandy committed
663 664 665 666
  recursive_edit_1 ();

  /* If cursor is on the minibuffer line,
     show the user we have exited by putting it in column 0.  */
667
  if (XWINDOW (minibuf_window)->cursor.vpos >= 0
Jim Blandy's avatar
Jim Blandy committed
668 669
      && !noninteractive)
    {
670 671 672
      XWINDOW (minibuf_window)->cursor.hpos = 0;
      XWINDOW (minibuf_window)->cursor.x = 0;
      XWINDOW (minibuf_window)->must_be_updated_p = 1;
673
      update_frame (XFRAME (selected_frame), 1, 1);
674 675 676 677 678 679
      {
        struct frame *f = XFRAME (XWINDOW (minibuf_window)->frame);
        struct redisplay_interface *rif = FRAME_RIF (f);
        if (rif && rif->flush_display)
          rif->flush_display (f);
      }
Jim Blandy's avatar
Jim Blandy committed
680 681
    }

682
  /* Make minibuffer contents into a string.  */
683
  Fset_buffer (minibuffer);
684
  if (allow_props)
685
    val = Fminibuffer_contents ();
686
  else
687
    val = Fminibuffer_contents_no_properties ();
688

689
  /* VAL is the string of minibuffer text.  */
690

691 692
  last_minibuf_string = val;

693
  /* Choose the string to add to the history.  */
694
  if (SCHARS (val) != 0)
695 696 697
    histstring = val;
  else if (STRINGP (defalt))
    histstring = defalt;
698 699
  else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
    histstring = XCAR (defalt);
700 701 702 703
  else
    histstring = Qnil;

  /* Add the value to the appropriate history list, if any.  */
704 705
  if (!NILP (Vhistory_add_new_input)
      && SYMBOLP (Vminibuffer_history_variable)
706
      && !NILP (histstring))
707 708 709 710
    {
      /* 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.  */
      Lisp_Object histval;
711 712 713

      /* If variable is unbound, make it nil.  */

714 715 716
      histval = find_symbol_value (Vminibuffer_history_variable);
      if (EQ (histval, Qunbound))
	Fset (Vminibuffer_history_variable, Qnil);
717 718 719

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

721
      if (NILP (histval)
722
	  || (CONSP (histval)
723
	      /* Don't duplicate the most recent entry in the history.  */
724
	      && (NILP (Fequal (histstring, Fcar (histval))))))
725 726 727
	{
	  Lisp_Object length;

728
	  if (history_delete_duplicates) Fdelete (histstring, histval);
729
	  histval = Fcons (histstring, histval);
730 731 732 733 734
	  Fset (Vminibuffer_history_variable, histval);

	  /* Truncate if requested.  */
	  length = Fget (Vminibuffer_history_variable, Qhistory_length);
	  if (NILP (length)) length = Vhistory_length;
735 736 737 738 739 740 741 742 743 744 745 746
	  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);
		}
	    }
747
	}
748 749
    }

750
  /* If Lisp form desired instead of string, parse it.  */
751
  if (expflag)
Gerd Moellmann's avatar
Gerd Moellmann committed
752
    val = string_to_object (val, defalt);
753

754 755
  /* The appropriate frame will get selected
     in set-window-configuration.  */
756 757
  UNGCPRO;
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
758 759 760 761
}

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

Lisp_Object
765
get_minibuffer (int depth)
Jim Blandy's avatar
Jim Blandy committed
766 767
{
  Lisp_Object tail, num, buf;
768
  char name[24];
Jim Blandy's avatar
Jim Blandy committed
769

770
  XSETFASTINT (num, depth);
Jim Blandy's avatar
Jim Blandy committed
771
  tail = Fnthcdr (num, Vminibuffer_list);
Jim Blandy's avatar
Jim Blandy committed
772
  if (NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
773 774 775 776 777
    {
      tail = Fcons (Qnil, Qnil);
      Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
    }
  buf = Fcar (tail);
Tom Tromey's avatar
Tom Tromey committed
778
  if (NILP (buf) || NILP (BVAR (XBUFFER (buf), name)))
Jim Blandy's avatar
Jim Blandy committed
779 780 781
    {
      sprintf (name, " *Minibuf-%d*", depth);
      buf = Fget_buffer_create (build_string (name));
782 783 784 785 786

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

787
      XSETCAR (tail, buf);
Jim Blandy's avatar
Jim Blandy committed
788 789
    }
  else
790
    {
Juanma Barranquero's avatar
Juanma Barranquero committed
791
      int count = SPECPDL_INDEX ();
792 793 794 795
      /* `reset_buffer' blindly sets the list of overlays to NULL, so we
	 have to empty the list, 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
796 797
      delete_all_overlays (XBUFFER (buf));
      reset_buffer (XBUFFER (buf));
798 799
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      Fset_buffer (buf);
800 801 802 803
      if (!NILP (Ffboundp (intern ("minibuffer-inactive-mode"))))
	call0 (intern ("minibuffer-inactive-mode"));
      else
        Fkill_all_local_variables ();
804
      unbind_to (count, Qnil);
805
    }
806

Jim Blandy's avatar
Jim Blandy committed
807 808 809
  return buf;
}

810
static Lisp_Object
811
run_exit_minibuf_hook (Lisp_Object data)
812
{
813
  safe_run_hooks (Qminibuffer_exit_hook);
814 815 816
  return Qnil;
}

817
/* This function is called on exiting minibuffer, whether normally or
818
   not, and it restores the current window, buffer, etc.  */
Jim Blandy's avatar
Jim Blandy committed
819

820
static Lisp_Object
821
read_minibuf_unwind (Lisp_Object data)
Jim Blandy's avatar
Jim Blandy committed
822
{
823
  Lisp_Object old_deactivate_mark;
824
  Lisp_Object window;
825

Jim Blandy's avatar
Jim Blandy committed
826
  /* If this was a recursive minibuffer,
827
     tie the minibuffer window back to the outer level minibuffer buffer.  */
Jim Blandy's avatar
Jim Blandy committed
828 829
  minibuf_level--;

830
  window = minibuf_window;
831 832
  /* To keep things predictable, in case it matters, let's be in the
     minibuffer when we reset the relevant variables.  */
833 834 835
  Fset_buffer (XWINDOW (window)->buffer);

  /* Restore prompt, etc, from outer minibuffer level.  */