minibuf.c 73.2 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
#include "lisp.h"
#include "commands.h"
#include "buffer.h"
28
#include "character.h"
Jim Blandy's avatar
Jim Blandy committed
29
#include "dispextern.h"
30
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
31
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
32 33
#include "window.h"
#include "syntax.h"
34
#include "intervals.h"
Stefan Monnier's avatar
Stefan Monnier committed
35
#include "keymap.h"
36
#include "termhooks.h"
Jim Blandy's avatar
Jim Blandy committed
37 38

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

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

46
/* Data to remember during recursive minibuffer invocations  */
47

48
Lisp_Object minibuf_save_list;
Jim Blandy's avatar
Jim Blandy committed
49 50

/* Depth in minibuffer invocations.  */
51

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

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

56
Lisp_Object Qhistory_length;
57

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

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

62
Lisp_Object Qminibuffer_history, Qbuffer_name_history;
63

64 65
Lisp_Object Qread_file_name_internal;

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

68 69
Lisp_Object Qminibuffer_setup_hook;
Lisp_Object Qminibuffer_exit_hook;
70

71
Lisp_Object Qcompletion_ignore_case;
72 73 74
Lisp_Object Qminibuffer_completion_table;
Lisp_Object Qminibuffer_completion_predicate;
Lisp_Object Qminibuffer_completion_confirm;
75 76 77 78
Lisp_Object Quser_variable_p;

Lisp_Object Qminibuffer_default;

79 80
Lisp_Object Qcurrent_input_method, Qactivate_input_method;

Kenichi Handa's avatar
Kenichi Handa committed
81 82
Lisp_Object Qcase_fold_search;

83 84
Lisp_Object Qread_expression_history;

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

static int minibuf_prompt_width;

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

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

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

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

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

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

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

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

  minibuf_window = window;

  return window;
}

161

Jim Blandy's avatar
Jim Blandy committed
162 163
/* Actual minibuffer invocation. */

164 165 166 167 168 169 170 171 172 173 174 175 176
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
177 178 179 180 181 182


/* 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
183
string_to_object (Lisp_Object val, Lisp_Object defalt)
Gerd Moellmann's avatar
Gerd Moellmann committed
184 185 186
{
  struct gcpro gcpro1, gcpro2;
  Lisp_Object expr_and_pos;
187
  EMACS_INT pos;
188

Gerd Moellmann's avatar
Gerd Moellmann committed
189
  GCPRO2 (val, defalt);
190

191 192 193 194 195 196 197
  if (STRINGP (val) && SCHARS (val) == 0)
    {
      if (STRINGP (defalt))
	val = defalt;
      else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
	val = XCAR (defalt);
    }
198

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

Gerd Moellmann's avatar
Gerd Moellmann committed
215 216 217 218 219 220 221 222 223
  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
224 225 226 227 228 229
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
230 231 232 233 234
{
  int size, len;
  char *line, *s;
  Lisp_Object val;

235
  fprintf (stdout, "%s", SDATA (prompt));
Gerd Moellmann's avatar
Gerd Moellmann committed
236 237
  fflush (stdout);

238
  val = Qnil;
Gerd Moellmann's avatar
Gerd Moellmann committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252
  size = 100;
  len = 0;
  line = (char *) xmalloc (size * sizeof *line);
  while ((s = fgets (line + len, size - len, stdin)) != NULL
	 && (len = strlen (line),
	     len == size - 1 && line[len - 1] != '\n'))
    {
      size *= 2;
      line = (char *) xrealloc (line, size);
    }

  if (s)
    {
      len = strlen (line);
253

Gerd Moellmann's avatar
Gerd Moellmann committed
254 255
      if (len > 0 && line[len - 1] == '\n')
	line[--len] = '\0';
256

Gerd Moellmann's avatar
Gerd Moellmann committed
257 258 259 260 261 262 263 264
      val = build_string (line);
      xfree (line);
    }
  else
    {
      xfree (line);
      error ("Error reading from stdin");
    }
265

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

Gerd Moellmann's avatar
Gerd Moellmann committed
270 271
  return val;
}
272

273
DEFUN ("minibufferp", Fminibufferp,
274 275
       Sminibufferp, 0, 1, 0,
       doc: /* Return t if BUFFER is a minibuffer.
Kenichi Handa's avatar
Kenichi Handa committed
276 277
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
278
  (Lisp_Object buffer)
279 280 281
{
  Lisp_Object tem;

282 283 284 285 286 287 288 289
  if (NILP (buffer))
    buffer = Fcurrent_buffer ();
  else if (STRINGP (buffer))
    buffer = Fget_buffer (buffer);
  else
    CHECK_BUFFER (buffer);

  tem = Fmemq (buffer, Vminibuffer_list);
290 291 292
  return ! NILP (tem) ? Qt : Qnil;
}

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

309
  if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
310
    return beg;
311 312 313 314 315 316
  else
    return end;
}

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

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

348

Kenichi Handa's avatar
Kenichi Handa committed
349 350
/* Read from the minibuffer using keymap MAP and initial contents INITIAL,
   putting point minus BACKUP_N bytes from the end of INITIAL,
351
   prompting with PROMPT (a string), using history list HISTVAR
Kenichi Handa's avatar
Kenichi Handa committed
352 353 354 355 356 357
   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.
358 359

   Normally return the result as a string (the text that was read),
360
   but if EXPFLAG is nonzero, read it and return the object read.
361 362
   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
363
   the list as the string that was read.
364

365
   DEFALT specifies the default value for the sake of history commands.
366

367 368
   If ALLOW_PROPS is nonzero, we do not throw away text properties.

Kenichi Handa's avatar
Kenichi Handa committed
369
   if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
370
   current input method.  */
371 372

static Lisp_Object
Andreas Schwab's avatar
Andreas Schwab committed
373 374 375 376
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
377
{
378
  Lisp_Object val;
Juanma Barranquero's avatar
Juanma Barranquero committed
379
  int count = SPECPDL_INDEX ();
380 381
  Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
382
  Lisp_Object enable_multibyte;
Kenichi Handa's avatar
Kenichi Handa committed
383
  int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
384 385 386
  /* String to add to the history.  */
  Lisp_Object histstring;

387 388 389
  Lisp_Object empty_minibuf;
  Lisp_Object dummy, frame;

390 391
  specbind (Qminibuffer_default, defalt);

392 393 394 395 396 397 398 399
  /* 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;

400
#ifdef HAVE_WINDOW_SYSTEM
401 402
  if (display_hourglass_p)
    cancel_hourglass ();
403
#endif
404

Kenichi Handa's avatar
Kenichi Handa committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
  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);
    }
426
  val = Qnil;
Tom Tromey's avatar
Tom Tromey committed
427
  ambient_dir = BVAR (current_buffer, directory);
428
  input_method = Qnil;
429
  enable_multibyte = Qnil;
430

431 432 433
  /* 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.  */
434
  GCPRO5 (map, initial, val, ambient_dir, input_method);
Jim Blandy's avatar
Jim Blandy committed
435

436
  if (!STRINGP (prompt))
437
    prompt = empty_unibyte_string;
Jim Blandy's avatar
Jim Blandy committed
438 439

  if (!enable_recursive_minibuffers
440 441 442 443 444 445 446 447 448
      && 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
449

450 451 452 453 454
  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))
455
    {
Kenichi Handa's avatar
Kenichi Handa committed
456 457
      val = read_minibuf_noninteractive (map, initial, prompt,
					 make_number (pos),
458 459
					 expflag, histvar, histpos, defalt,
					 allow_props, inherit_input_method);
460
      UNGCPRO;
461 462
      return unbind_to (count, val);
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
463

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

466 467
  choose_minibuf_frame ();

468 469
  record_unwind_protect (choose_minibuf_frame_1, Qnil);

Jim Blandy's avatar
Jim Blandy committed
470
  record_unwind_protect (Fset_window_configuration,
Jim Blandy's avatar
Jim Blandy committed
471 472
			 Fcurrent_window_configuration (Qnil));

Jim Blandy's avatar
Jim Blandy committed
473 474
  /* If the minibuffer window is on a different frame, save that
     frame's configuration too.  */
475
  mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
476
  if (!EQ (mini_frame, selected_frame))
477 478
    record_unwind_protect (Fset_window_configuration,
			   Fcurrent_window_configuration (mini_frame));
479 480 481 482 483

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

484 485
  if (minibuffer_auto_raise)
    Fraise_frame (mini_frame);
Jim Blandy's avatar
Jim Blandy committed
486

487
  temporarily_switch_to_single_kboard (XFRAME (mini_frame));
488

489 490 491 492 493 494 495 496
  /* 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,
497
	     Fcons (minibuf_window,
498
		    minibuf_save_list));
499 500 501 502 503 504 505 506 507 508 509
  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++;
510 511 512 513 514 515
  /* 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);
516 517 518

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

519
  minibuf_prompt_width = 0;
520 521 522 523
  minibuf_prompt = Fcopy_sequence (prompt);
  Vminibuffer_history_position = histpos;
  Vminibuffer_history_variable = histvar;
  Vhelp_form = Vminibuffer_help_form;
524 525 526 527 528 529 530
  /* 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;
531

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

540 541
  /* Switch to the minibuffer.  */

542 543
  minibuffer = get_minibuffer (minibuf_level);
  Fset_buffer (minibuffer);
544

545 546
  /* If appropriate, copy enable-multibyte-characters into the minibuffer.  */
  if (inherit_input_method)
Tom Tromey's avatar
Tom Tromey committed
547
    BVAR (current_buffer, enable_multibyte_characters) = enable_multibyte;
548

549 550 551 552 553 554 555 556
  /* 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.  */
557
  if (STRINGP (ambient_dir))
Tom Tromey's avatar
Tom Tromey committed
558
    BVAR (current_buffer, directory) = ambient_dir;
559 560 561 562 563 564
  else
    {
      Lisp_Object buf_list;

      for (buf_list = Vbuffer_alist;
	   CONSP (buf_list);
565
	   buf_list = XCDR (buf_list))
566
	{
567
	  Lisp_Object other_buf;
568

569
	  other_buf = XCDR (XCAR (buf_list));
Tom Tromey's avatar
Tom Tromey committed
570
	  if (STRINGP (BVAR (XBUFFER (other_buf), directory)))
571
	    {
Tom Tromey's avatar
Tom Tromey committed
572
	      BVAR (current_buffer, directory) = BVAR (XBUFFER (other_buf), directory);
573 574 575 576 577
	      break;
	    }
	}
    }

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

Jim Blandy's avatar
Jim Blandy committed
581
  Vminibuf_scroll_window = selected_window;
582
  if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
583
    minibuf_selected_window = selected_window;
584 585 586 587 588 589 590 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.  */
  empty_minibuf = Fget_buffer (build_string (" *Minibuf-0*"));

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

595 596
      if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
	  && !NILP (Fwindow_minibuffer_p (mini_window)))
597 598 599 600
	Fset_window_buffer (mini_window, empty_minibuf, Qnil);
    }

  /* Display this minibuffer in the proper window.  */
601
  Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
602
  Fselect_window (minibuf_window, Qnil);
603
  XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
Jim Blandy's avatar
Jim Blandy committed
604

605 606 607 608
  Fmake_local_variable (Qprint_escape_newlines);
  print_escape_newlines = 1;

  /* Erase the buffer.  */
609
  {
610
    int count1 = SPECPDL_INDEX ();
611
    specbind (Qinhibit_read_only, Qt);
612
    specbind (Qinhibit_modification_hooks, Qt);
613
    Ferase_buffer ();
614

Tom Tromey's avatar
Tom Tromey committed
615
    if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
616 617
	&& ! STRING_MULTIBYTE (minibuf_prompt))
      minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
618

619 620 621 622 623 624 625 626 627 628 629 630 631
    /* 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);
      }
632 633 634
    unbind_to (count1, Qnil);
  }

635 636
  minibuf_prompt_width = (int) current_column (); /* iftc */

637
  /* Put in the initial input.  */
Jim Blandy's avatar
Jim Blandy committed
638
  if (!NILP (initial))
Jim Blandy's avatar
Jim Blandy committed
639 640
    {
      Finsert (1, &initial);
Kenichi Handa's avatar
Kenichi Handa committed
641
      Fforward_char (make_number (pos));
Jim Blandy's avatar
Jim Blandy committed
642 643
    }

644
  clear_message (1, 1);
Tom Tromey's avatar
Tom Tromey committed
645
  BVAR (current_buffer, keymap) = map;
Jim Blandy's avatar
Jim Blandy committed
646

647
  /* Turn on an input method stored in INPUT_METHOD if any.  */
648
  if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
649 650
    call1 (Qactivate_input_method, input_method);

651 652
  /* Run our hook, but not if it is empty.
     (run-hooks would do nothing if it is empty,
653
     but it's important to save time here in the usual case.)  */
654 655
  if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
      && !NILP (Vrun_hooks))
656 657
    call1 (Vrun_hooks, Qminibuffer_setup_hook);

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

Jim Blandy's avatar
Jim Blandy committed
661 662 663 664
  recursive_edit_1 ();

  /* If cursor is on the minibuffer line,
     show the user we have exited by putting it in column 0.  */
665
  if (XWINDOW (minibuf_window)->cursor.vpos >= 0
Jim Blandy's avatar
Jim Blandy committed
666 667
      && !noninteractive)
    {
668 669 670
      XWINDOW (minibuf_window)->cursor.hpos = 0;
      XWINDOW (minibuf_window)->cursor.x = 0;
      XWINDOW (minibuf_window)->must_be_updated_p = 1;
671
      update_frame (XFRAME (selected_frame), 1, 1);
672 673 674 675 676 677
      {
        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
678 679
    }

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

687
  /* VAL is the string of minibuffer text.  */
688

689 690
  last_minibuf_string = val;

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

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

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

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

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

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

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

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

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

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

/* Return a buffer to be used as the minibuffer at depth `depth'.
 depth = 0 is the lowest allowed argument, and that is the value
 used for nonrecursive minibuffer invocations */

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

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

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

785
      XSETCAR (tail, buf);
Jim Blandy's avatar
Jim Blandy committed
786 787
    }
  else
788
    {
Juanma Barranquero's avatar
Juanma Barranquero committed
789
      int count = SPECPDL_INDEX ();
790 791 792 793
      /* `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
794 795
      delete_all_overlays (XBUFFER (buf));
      reset_buffer (XBUFFER (buf));
796 797 798 799
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      Fset_buffer (buf);
      Fkill_all_local_variables ();
      unbind_to (count, Qnil);
800
    }
801

Jim Blandy's avatar
Jim Blandy committed
802 803 804
  return buf;
}

805
static Lisp_Object
806
run_exit_minibuf_hook (Lisp_Object data)
807 808 809 810 811 812 813 814
{
  if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
      && !NILP (Vrun_hooks))
    safe_run_hooks (Qminibuffer_exit_hook);

  return Qnil;
}

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

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

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

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

  /* Restore prompt, etc, from outer minibuffer level.  */
834 835 836 837 838 839
  minibuf_prompt = Fcar (minibuf_save_list);
  minibuf_save_list = Fcdr (minibuf_save_list);
  minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));
  minibuf_save_list = Fcdr (minibuf_save_list);
  Vhelp_form = Fcar (minibuf_save_list);
  minibuf_save_list = Fcdr (minibuf_save_list);
840
  Vcurrent_prefix_arg = Fcar (minibuf_save_list);
841 842 843 844 845
  minibuf_save_list = Fcdr (minibuf_save_list);
  Vminibuffer_history_position = Fcar (minibuf_save_list);
  minibuf_save_list = Fcdr (minibuf_save_list);
  Vminibuffer_history_variable = Fcar (minibuf_save_list);
  minibuf_save_list = Fcdr (minibuf_save_list);
846
  Voverriding_local_map = Fcar (minibuf_save_list);
847
  minibuf_save_list = Fcdr (minibuf_save_list);
848 849 850 851 852
#if 0
  temp = Fcar (minibuf_save_list);
  if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
    minibuf_window = temp;
#endif
853
  minibuf_save_list = Fcdr (minibuf_save_list);
854 855 856

  /* Erase the minibuffer we were using at this level.  */
  {
Juanma Barranquero's avatar
Juanma Barranquero committed
857
    int count = SPECPDL_INDEX ();
858 859
    /* Prevent error in erase-buffer.  */
    specbind (Qinhibit_read_only, Qt);
860
    specbind (Qinhibit_modification_hooks, Qt);
861 862 863 864 865 866
    old_deactivate_mark = Vdeactivate_mark;
    Ferase_buffer ();
    Vdeactivate_mark = old_deactivate_mark;
    unbind_to (count, Qnil);
  }

867 868 869
  /* When we get to the outmost level, make sure we resize the
     mini-window back to its normal size.  */
  if (minibuf_level == 0)
870
    resize_mini_window (XWINDOW (window), 0);