minibuf.c 72.9 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;

Jim Blandy's avatar
Jim Blandy committed
85

86 87 88 89 90
/* Put minibuf on currently selected frame's minibuffer.
   We do this whenever the user starts a new minibuffer
   or when a minibuffer exits.  */

void
91
choose_minibuf_frame (void)
92
{
93 94 95
  if (FRAMEP (selected_frame)
      && FRAME_LIVE_P (XFRAME (selected_frame))
      && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
96
    {
97 98
      struct frame *sf = XFRAME (selected_frame);
      Lisp_Object buffer;
99

100 101
      /* I don't think that any frames may validly have a null minibuffer
	 window anymore.  */
102
      if (NILP (sf->minibuffer_window))
103 104
	abort ();

105 106 107 108 109
      /* 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))
110
	Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
111
      minibuf_window = sf->minibuffer_window;
112
    }
113 114 115

  /* 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
116 117
     confusing.  Only allow the selected frame to do this,
     and that only if the minibuffer is active.  */
118 119 120 121
  {
    Lisp_Object tail, frame;

    FOR_EACH_FRAME (tail, frame)
122
      if (MINI_WINDOW_P (XWINDOW (FRAME_SELECTED_WINDOW (XFRAME (frame))))
123
	  && !(EQ (frame, selected_frame)
124
	       && minibuf_level > 0))
125
	Fset_frame_selected_window (frame, Fframe_first_window (frame), Qnil);
126
  }
127
}
128

129
Lisp_Object
130
choose_minibuf_frame_1 (Lisp_Object ignore)
131 132 133 134 135
{
  choose_minibuf_frame ();
  return Qnil;
}

136 137
DEFUN ("set-minibuffer-window", Fset_minibuffer_window,
       Sset_minibuffer_window, 1, 1, 0,
138
       doc: /* Specify which minibuffer window to use for the minibuffer.
Kenichi Handa's avatar
Kenichi Handa committed
139
This affects where the minibuffer is displayed if you put text in it
140
without invoking the usual minibuffer commands.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
141
  (Lisp_Object window)
142
{
143
  CHECK_WINDOW (window);
144 145 146 147 148 149 150 151
  if (! MINI_WINDOW_P (XWINDOW (window)))
    error ("Window is not a minibuffer window");

  minibuf_window = window;

  return window;
}

152

Jim Blandy's avatar
Jim Blandy committed
153 154
/* Actual minibuffer invocation. */

155 156 157 158 159 160 161 162 163 164 165 166 167
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
168 169 170 171 172 173


/* 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
174
string_to_object (Lisp_Object val, Lisp_Object defalt)
Gerd Moellmann's avatar
Gerd Moellmann committed
175 176 177
{
  struct gcpro gcpro1, gcpro2;
  Lisp_Object expr_and_pos;
178
  EMACS_INT pos;
179

Gerd Moellmann's avatar
Gerd Moellmann committed
180
  GCPRO2 (val, defalt);
181

182 183 184 185 186 187 188
  if (STRINGP (val) && SCHARS (val) == 0)
    {
      if (STRINGP (defalt))
	val = defalt;
      else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
	val = XCAR (defalt);
    }
189

Gerd Moellmann's avatar
Gerd Moellmann committed
190 191
  expr_and_pos = Fread_from_string (val, Qnil, Qnil);
  pos = XINT (Fcdr (expr_and_pos));
192
  if (pos != SCHARS (val))
Gerd Moellmann's avatar
Gerd Moellmann committed
193 194 195
    {
      /* Ignore trailing whitespace; any other trailing junk
	 is an error.  */
196
      EMACS_INT i;
Gerd Moellmann's avatar
Gerd Moellmann committed
197
      pos = string_char_to_byte (val, pos);
198
      for (i = pos; i < SBYTES (val); i++)
Gerd Moellmann's avatar
Gerd Moellmann committed
199
	{
200
	  int c = SREF (val, i);
Gerd Moellmann's avatar
Gerd Moellmann committed
201 202 203 204
	  if (c != ' ' && c != '\t' && c != '\n')
	    error ("Trailing garbage following expression");
	}
    }
205

Gerd Moellmann's avatar
Gerd Moellmann committed
206 207 208 209 210 211 212 213 214
  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
215 216 217 218 219 220
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
221 222 223 224 225
{
  int size, len;
  char *line, *s;
  Lisp_Object val;

226
  fprintf (stdout, "%s", SDATA (prompt));
Gerd Moellmann's avatar
Gerd Moellmann committed
227 228
  fflush (stdout);

229
  val = Qnil;
Gerd Moellmann's avatar
Gerd Moellmann committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243
  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);
244

Gerd Moellmann's avatar
Gerd Moellmann committed
245 246
      if (len > 0 && line[len - 1] == '\n')
	line[--len] = '\0';
247

Gerd Moellmann's avatar
Gerd Moellmann committed
248 249 250 251 252 253 254 255
      val = build_string (line);
      xfree (line);
    }
  else
    {
      xfree (line);
      error ("Error reading from stdin");
    }
256

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

Gerd Moellmann's avatar
Gerd Moellmann committed
261 262
  return val;
}
263

264
DEFUN ("minibufferp", Fminibufferp,
265 266
       Sminibufferp, 0, 1, 0,
       doc: /* Return t if BUFFER is a minibuffer.
Kenichi Handa's avatar
Kenichi Handa committed
267 268
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
269
  (Lisp_Object buffer)
270 271 272
{
  Lisp_Object tem;

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

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

284 285
DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
       Sminibuffer_prompt_end, 0, 0, 0,
286
       doc: /* Return the buffer position of the end of the minibuffer prompt.
287
Return (point-min) if current buffer is not a minibuffer.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
288
  (void)
289 290
{
  /* This function is written to be most efficient when there's a prompt.  */
291 292 293 294 295 296 297 298
  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);
299

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

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

326 327 328 329
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.
330
If the current buffer is not a minibuffer, return its entire contents.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
331
  (void)
332
{
333
  EMACS_INT prompt_end = XINT (Fminibuffer_prompt_end ());
334 335 336 337 338
  if (PT < prompt_end)
    error ("Cannot do completion in the prompt");
  return make_buffer_string (prompt_end, PT, 1);
}

339

Kenichi Handa's avatar
Kenichi Handa committed
340 341
/* Read from the minibuffer using keymap MAP and initial contents INITIAL,
   putting point minus BACKUP_N bytes from the end of INITIAL,
342
   prompting with PROMPT (a string), using history list HISTVAR
Kenichi Handa's avatar
Kenichi Handa committed
343 344 345 346 347 348
   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.
349 350

   Normally return the result as a string (the text that was read),
351
   but if EXPFLAG is nonzero, read it and return the object read.
352 353
   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
354
   the list as the string that was read.
355

356
   DEFALT specifies the default value for the sake of history commands.
357

358 359
   If ALLOW_PROPS is nonzero, we do not throw away text properties.

Kenichi Handa's avatar
Kenichi Handa committed
360
   if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
361
   current input method.  */
362 363

static Lisp_Object
Andreas Schwab's avatar
Andreas Schwab committed
364 365 366 367
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
368
{
369
  Lisp_Object val;
Juanma Barranquero's avatar
Juanma Barranquero committed
370
  int count = SPECPDL_INDEX ();
371 372
  Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
373
  Lisp_Object enable_multibyte;
Kenichi Handa's avatar
Kenichi Handa committed
374
  int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
375 376 377
  /* String to add to the history.  */
  Lisp_Object histstring;

378 379 380
  Lisp_Object empty_minibuf;
  Lisp_Object dummy, frame;

381 382
  specbind (Qminibuffer_default, defalt);

383 384 385 386 387 388 389 390
  /* 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;

391
#ifdef HAVE_WINDOW_SYSTEM
392 393
  if (display_hourglass_p)
    cancel_hourglass ();
394
#endif
395

Kenichi Handa's avatar
Kenichi Handa committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  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);
    }
417
  val = Qnil;
418
  ambient_dir = current_buffer->directory;
419
  input_method = Qnil;
420
  enable_multibyte = Qnil;
421

422 423 424
  /* 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.  */
425
  GCPRO5 (map, initial, val, ambient_dir, input_method);
Jim Blandy's avatar
Jim Blandy committed
426

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

  if (!enable_recursive_minibuffers
431 432 433 434 435 436 437 438 439
      && 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
440

441 442 443 444 445
  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))
446
    {
Kenichi Handa's avatar
Kenichi Handa committed
447 448
      val = read_minibuf_noninteractive (map, initial, prompt,
					 make_number (pos),
449 450
					 expflag, histvar, histpos, defalt,
					 allow_props, inherit_input_method);
451
      UNGCPRO;
452 453
      return unbind_to (count, val);
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
454

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

457 458
  choose_minibuf_frame ();

459 460
  record_unwind_protect (choose_minibuf_frame_1, Qnil);

Jim Blandy's avatar
Jim Blandy committed
461
  record_unwind_protect (Fset_window_configuration,
Jim Blandy's avatar
Jim Blandy committed
462 463
			 Fcurrent_window_configuration (Qnil));

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

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

475 476
  if (minibuffer_auto_raise)
    Fraise_frame (mini_frame);
Jim Blandy's avatar
Jim Blandy committed
477

478
  temporarily_switch_to_single_kboard (XFRAME (mini_frame));
479

480 481 482 483 484 485 486 487
  /* 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,
488
	     Fcons (minibuf_window,
489
		    minibuf_save_list));
490 491 492 493 494 495 496 497 498 499 500
  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++;
501 502 503 504 505 506
  /* 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);
507 508 509

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

510
  minibuf_prompt_width = 0;
511 512 513 514
  minibuf_prompt = Fcopy_sequence (prompt);
  Vminibuffer_history_position = histpos;
  Vminibuffer_history_variable = histvar;
  Vhelp_form = Vminibuffer_help_form;
515 516 517 518 519 520 521
  /* 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;
522

523
  if (inherit_input_method)
524
    {
525
      /* `current-input-method' is buffer local.  So, remember it in
526 527 528 529
	 INPUT_METHOD before changing the current buffer.  */
      input_method = Fsymbol_value (Qcurrent_input_method);
      enable_multibyte = current_buffer->enable_multibyte_characters;
    }
530

531 532
  /* Switch to the minibuffer.  */

533 534
  minibuffer = get_minibuffer (minibuf_level);
  Fset_buffer (minibuffer);
535

536 537 538 539
  /* If appropriate, copy enable-multibyte-characters into the minibuffer.  */
  if (inherit_input_method)
    current_buffer->enable_multibyte_characters = enable_multibyte;

540 541 542 543 544 545 546 547
  /* 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.  */
548 549
  if (STRINGP (ambient_dir))
    current_buffer->directory = ambient_dir;
550 551 552 553 554 555
  else
    {
      Lisp_Object buf_list;

      for (buf_list = Vbuffer_alist;
	   CONSP (buf_list);
556
	   buf_list = XCDR (buf_list))
557
	{
558
	  Lisp_Object other_buf;
559

560
	  other_buf = XCDR (XCAR (buf_list));
561
	  if (STRINGP (XBUFFER (other_buf)->directory))
562 563 564 565 566 567 568
	    {
	      current_buffer->directory = XBUFFER (other_buf)->directory;
	      break;
	    }
	}
    }

569 570
  if (!EQ (mini_frame, selected_frame))
    Fredirect_frame_focus (selected_frame, mini_frame);
Jim Blandy's avatar
Jim Blandy committed
571

Jim Blandy's avatar
Jim Blandy committed
572
  Vminibuf_scroll_window = selected_window;
573
  if (minibuf_level == 1 || !EQ (minibuf_window, selected_window))
574
    minibuf_selected_window = selected_window;
575 576 577 578 579 580 581 582 583 584 585

  /* 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;

586 587
      if (! NILP (mini_window) && ! EQ (mini_window, minibuf_window)
	  && !NILP (Fwindow_minibuffer_p (mini_window)))
588 589 590 591
	Fset_window_buffer (mini_window, empty_minibuf, Qnil);
    }

  /* Display this minibuffer in the proper window.  */
592
  Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
593
  Fselect_window (minibuf_window, Qnil);
594
  XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
Jim Blandy's avatar
Jim Blandy committed
595

596 597 598 599
  Fmake_local_variable (Qprint_escape_newlines);
  print_escape_newlines = 1;

  /* Erase the buffer.  */
600
  {
601
    int count1 = SPECPDL_INDEX ();
602
    specbind (Qinhibit_read_only, Qt);
603
    specbind (Qinhibit_modification_hooks, Qt);
604
    Ferase_buffer ();
605 606 607 608

    if (!NILP (current_buffer->enable_multibyte_characters)
	&& ! STRING_MULTIBYTE (minibuf_prompt))
      minibuf_prompt = Fstring_make_multibyte (minibuf_prompt);
609

610 611 612 613 614 615 616 617 618 619 620 621 622
    /* 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);
      }
623 624 625
    unbind_to (count1, Qnil);
  }

626 627
  minibuf_prompt_width = (int) current_column (); /* iftc */

628
  /* Put in the initial input.  */
Jim Blandy's avatar
Jim Blandy committed
629
  if (!NILP (initial))
Jim Blandy's avatar
Jim Blandy committed
630 631
    {
      Finsert (1, &initial);
Kenichi Handa's avatar
Kenichi Handa committed
632
      Fforward_char (make_number (pos));
Jim Blandy's avatar
Jim Blandy committed
633 634
    }

635
  clear_message (1, 1);
Jim Blandy's avatar
Jim Blandy committed
636 637
  current_buffer->keymap = map;

638
  /* Turn on an input method stored in INPUT_METHOD if any.  */
639
  if (STRINGP (input_method) && !NILP (Ffboundp (Qactivate_input_method)))
640 641
    call1 (Qactivate_input_method, input_method);

642 643
  /* Run our hook, but not if it is empty.
     (run-hooks would do nothing if it is empty,
644
     but it's important to save time here in the usual case.)  */
645 646
  if (!NILP (Vminibuffer_setup_hook) && !EQ (Vminibuffer_setup_hook, Qunbound)
      && !NILP (Vrun_hooks))
647 648
    call1 (Vrun_hooks, Qminibuffer_setup_hook);

649 650 651
  /* Don't allow the user to undo past this point.  */
  current_buffer->undo_list = Qnil;

Jim Blandy's avatar
Jim Blandy committed
652 653 654 655
  recursive_edit_1 ();

  /* If cursor is on the minibuffer line,
     show the user we have exited by putting it in column 0.  */
656
  if (XWINDOW (minibuf_window)->cursor.vpos >= 0
Jim Blandy's avatar
Jim Blandy committed
657 658
      && !noninteractive)
    {
659 660 661
      XWINDOW (minibuf_window)->cursor.hpos = 0;
      XWINDOW (minibuf_window)->cursor.x = 0;
      XWINDOW (minibuf_window)->must_be_updated_p = 1;
662
      update_frame (XFRAME (selected_frame), 1, 1);
663 664 665 666 667 668
      {
        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
669 670
    }

671
  /* Make minibuffer contents into a string.  */
672
  Fset_buffer (minibuffer);
673
  if (allow_props)
674
    val = Fminibuffer_contents ();
675
  else
676
    val = Fminibuffer_contents_no_properties ();
677

678
  /* VAL is the string of minibuffer text.  */
679

680 681
  last_minibuf_string = val;

682
  /* Choose the string to add to the history.  */
683
  if (SCHARS (val) != 0)
684 685 686
    histstring = val;
  else if (STRINGP (defalt))
    histstring = defalt;
687 688
  else if (CONSP (defalt) && STRINGP (XCAR (defalt)))
    histstring = XCAR (defalt);
689 690 691 692
  else
    histstring = Qnil;

  /* Add the value to the appropriate history list, if any.  */
693 694
  if (!NILP (Vhistory_add_new_input)
      && SYMBOLP (Vminibuffer_history_variable)
695
      && !NILP (histstring))
696 697 698 699
    {
      /* 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;
700 701 702

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

703 704 705
      histval = find_symbol_value (Vminibuffer_history_variable);
      if (EQ (histval, Qunbound))
	Fset (Vminibuffer_history_variable, Qnil);
706 707 708

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

710
      if (NILP (histval)
711
	  || (CONSP (histval)
712
	      /* Don't duplicate the most recent entry in the history.  */
713
	      && (NILP (Fequal (histstring, Fcar (histval))))))
714 715 716
	{
	  Lisp_Object length;

717
	  if (history_delete_duplicates) Fdelete (histstring, histval);
718
	  histval = Fcons (histstring, histval);
719 720 721 722 723
	  Fset (Vminibuffer_history_variable, histval);

	  /* Truncate if requested.  */
	  length = Fget (Vminibuffer_history_variable, Qhistory_length);
	  if (NILP (length)) length = Vhistory_length;
724 725 726 727 728 729 730 731 732 733 734 735
	  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);
		}
	    }
736
	}
737 738 739 740
    }

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

743 744
  /* The appropriate frame will get selected
     in set-window-configuration.  */
745 746
  UNGCPRO;
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
747 748 749 750 751 752 753
}

/* 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
754
get_minibuffer (int depth)
Jim Blandy's avatar
Jim Blandy committed
755 756
{
  Lisp_Object tail, num, buf;
757
  char name[24];
Jim Blandy's avatar
Jim Blandy committed
758

759
  XSETFASTINT (num, depth);
Jim Blandy's avatar
Jim Blandy committed
760
  tail = Fnthcdr (num, Vminibuffer_list);
Jim Blandy's avatar
Jim Blandy committed
761
  if (NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
762 763 764 765 766
    {
      tail = Fcons (Qnil, Qnil);
      Vminibuffer_list = nconc2 (Vminibuffer_list, tail);
    }
  buf = Fcar (tail);
Jim Blandy's avatar
Jim Blandy committed
767
  if (NILP (buf) || NILP (XBUFFER (buf)->name))
Jim Blandy's avatar
Jim Blandy committed
768 769 770
    {
      sprintf (name, " *Minibuf-%d*", depth);
      buf = Fget_buffer_create (build_string (name));
771 772 773 774 775

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

776
      XSETCAR (tail, buf);
Jim Blandy's avatar
Jim Blandy committed
777 778
    }
  else
779
    {
Juanma Barranquero's avatar
Juanma Barranquero committed
780
      int count = SPECPDL_INDEX ();
781 782 783 784
      /* `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
785 786
      delete_all_overlays (XBUFFER (buf));
      reset_buffer (XBUFFER (buf));
787 788 789 790
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      Fset_buffer (buf);
      Fkill_all_local_variables ();
      unbind_to (count, Qnil);
791
    }
792

Jim Blandy's avatar
Jim Blandy committed
793 794 795
  return buf;
}

796
static Lisp_Object
797
run_exit_minibuf_hook (Lisp_Object data)
798 799 800 801 802 803 804 805
{
  if (!NILP (Vminibuffer_exit_hook) && !EQ (Vminibuffer_exit_hook, Qunbound)
      && !NILP (Vrun_hooks))
    safe_run_hooks (Qminibuffer_exit_hook);

  return Qnil;
}

806 807
/* 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
808

809
static Lisp_Object
810
read_minibuf_unwind (Lisp_Object data)
Jim Blandy's avatar
Jim Blandy committed
811
{
812
  Lisp_Object old_deactivate_mark;
813
  Lisp_Object window;
814

Jim Blandy's avatar
Jim Blandy committed
815
  /* If this was a recursive minibuffer,
816
     tie the minibuffer window back to the outer level minibuffer buffer.  */
Jim Blandy's avatar
Jim Blandy committed
817 818
  minibuf_level--;

819
  window = minibuf_window;
820 821
  /* To keep things predictable, in case it matters, let's be in the
     minibuffer when we reset the relevant variables.  */
822 823 824
  Fset_buffer (XWINDOW (window)->buffer);

  /* Restore prompt, etc, from outer minibuffer level.  */
825 826 827 828 829 830
  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);
831
  Vcurrent_prefix_arg = Fcar (minibuf_save_list);
832 833 834 835 836
  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);
837
  Voverriding_local_map = Fcar (minibuf_save_list);
838
  minibuf_save_list = Fcdr (minibuf_save_list);
839 840 841 842 843
#if 0
  temp = Fcar (minibuf_save_list);
  if (FRAME_LIVE_P (XFRAME (WINDOW_FRAME (XWINDOW (temp)))))
    minibuf_window = temp;
#endif
844
  minibuf_save_list = Fcdr (minibuf_save_list);
845 846 847

  /* Erase the minibuffer we were using at this level.  */
  {
Juanma Barranquero's avatar
Juanma Barranquero committed
848
    int count = SPECPDL_INDEX ();
849 850
    /* Prevent error in erase-buffer.  */
    specbind (Qinhibit_read_only, Qt);
851
    specbind (Qinhibit_modification_hooks, Qt);
852 853 854 855 856 857
    old_deactivate_mark = Vdeactivate_mark;
    Ferase_buffer ();
    Vdeactivate_mark = old_deactivate_mark;
    unbind_to (count, Qnil);
  }

858 859 860
  /* When we get to the outmost level, make sure we resize the
     mini-window back to its normal size.  */
  if (minibuf_level == 0)
861
    resize_mini_window (XWINDOW (window), 0);
862

863 864 865
  /* Make sure minibuffer window is erased, not ignored.  */
  windows_or_buffers_changed++;
  XSETFASTINT (XWINDOW (window)->last_modified, 0);
866
  XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
867
  return Qnil;
Jim Blandy's avatar
Jim Blandy committed
868 869
}

870

871
DEFUN ("read-from-minibuffer", Fread_from_minibuffer, Sread_from_minibuffer, 1, 7, 0,
872
       doc: /* Read a string from the minibuffer, prompting with string PROMPT.
Kenichi Handa's avatar
Kenichi Handa committed
873 874 875
The optional second arg INITIAL-CONTENTS is an obsolete alternative to
  DEFAULT-VALUE.  It normally should be nil in new code, except when
  HIST is a cons.  It is discussed in more detail below.
Pavel Janík's avatar
Pavel Janík committed
876 877
Third arg KEYMAP is a keymap to use whilst reading;
  if omitted or nil, the default is `minibuffer-local-map'.