callint.c 30.5 KB
Newer Older
Michael I. Bushnell's avatar
Michael I. Bushnell committed
1
/* Call a Lisp function interactively.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 1985-1986, 1993-1995, 1997, 2000-2015 Free Software
3
   Foundation, Inc.
Michael I. Bushnell's avatar
Michael I. Bushnell committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Michael I. Bushnell's avatar
Michael I. Bushnell 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.
Michael I. Bushnell's avatar
Michael I. Bushnell 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/>.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
19 20


21
#include <config.h>
22

Michael I. Bushnell's avatar
Michael I. Bushnell committed
23
#include "lisp.h"
24
#include "character.h"
Michael I. Bushnell's avatar
Michael I. Bushnell committed
25 26
#include "buffer.h"
#include "commands.h"
Jim Blandy's avatar
Jim Blandy committed
27
#include "keyboard.h"
Michael I. Bushnell's avatar
Michael I. Bushnell committed
28
#include "window.h"
Stefan Monnier's avatar
Stefan Monnier committed
29
#include "keymap.h"
Michael I. Bushnell's avatar
Michael I. Bushnell committed
30

31 32 33 34
static Lisp_Object preserved_fns;

/* Marker used within call-interactively to refer to point.  */
static Lisp_Object point_marker;
35

36 37
/* String for the prompt text used in Fcall_interactively.  */
static Lisp_Object callint_message;
Richard M. Stallman's avatar
Richard M. Stallman committed
38

Michael I. Bushnell's avatar
Michael I. Bushnell committed
39 40
/* ARGSUSED */
DEFUN ("interactive", Finteractive, Sinteractive, 0, UNEVALLED, 0,
41 42
       doc: /* Specify a way of parsing arguments for interactive use of a function.
For example, write
43 44 45
 (defun foo (arg buf) "Doc string" (interactive "P\\nbbuffer: ") .... )
 to make ARG be the raw prefix argument, and set BUF to an existing buffer,
 when `foo' is called as a command.
46 47 48 49 50
The "call" to `interactive' is actually a declaration rather than a function;
 it tells `call-interactively' how to read arguments
 to pass to the function.
When actually called, `interactive' just returns nil.

51 52 53 54 55
Usually the argument of `interactive' is a string containing a code letter
 followed optionally by a prompt.  (Some code letters do not use I/O to get
 the argument and do not use prompts.)  To get several arguments, concatenate
 the individual strings, separating them by newline characters.
Prompts are passed to format, and may use % escapes to print the
56 57 58 59 60 61 62 63 64 65 66 67 68
 arguments that have already been read.
If the argument is not a string, it is evaluated to get a list of
 arguments to pass to the function.
Just `(interactive)' means pass no args when calling interactively.

Code letters available are:
a -- Function name: symbol with a function definition.
b -- Name of existing buffer.
B -- Name of buffer, possibly nonexistent.
c -- Character (no input method is used).
C -- Command name: symbol with interactive function definition.
d -- Value of point as number.  Does not do I/O.
D -- Directory name.
Paul Eggert's avatar
Paul Eggert committed
69
e -- Parameterized event (i.e., one that's a list) that invoked this command.
70 71 72 73
     If used more than once, the Nth `e' returns the Nth parameterized event.
     This skips events that are integers or symbols.
f -- Existing file name.
F -- Possibly nonexistent file name.
74
G -- Possibly nonexistent file name, defaulting to just directory name.
75 76 77 78 79 80
i -- Ignored, i.e. always nil.  Does not do I/O.
k -- Key sequence (downcase the last event if needed to get a definition).
K -- Key sequence to be redefined (do not downcase the last event).
m -- Value of mark as number.  Does not do I/O.
M -- Any string.  Inherits the current input method.
n -- Number read using minibuffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
81
N -- Numeric prefix arg, or if none, do like code `n'.
82 83 84 85 86
p -- Prefix arg converted to number.  Does not do I/O.
P -- Prefix arg in raw form.  Does not do I/O.
r -- Region: point and mark as 2 numeric args, smallest first.  Does no I/O.
s -- Any string.  Does not inherit the current input method.
S -- Any symbol.
87
U -- Mouse up event discarded by a previous k or K argument.
88
v -- Variable name: symbol that is `custom-variable-p'.
89 90 91 92
x -- Lisp expression read but not evaluated.
X -- Lisp expression read and evaluated.
z -- Coding system.
Z -- Coding system, nil if no prefix arg.
Chong Yidong's avatar
Chong Yidong committed
93 94 95

In addition, if the string begins with `*', an error is signaled if
  the buffer is read-only.
96 97 98 99
If `@' appears at the beginning of the string, and if the key sequence
 used to invoke the command includes any mouse events, then the window
 associated with the first of those events is selected before the
 command is run.
Chong Yidong's avatar
Chong Yidong committed
100
If the string begins with `^' and `shift-select-mode' is non-nil,
101
 Emacs first calls the function `handle-shift-selection'.
Chong Yidong's avatar
Chong Yidong committed
102 103
You may use `@', `*', and `^' together.  They are processed in the
 order that they appear, before reading any arguments.
104 105
usage: (interactive &optional ARGS)  */
       attributes: const)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
106
  (Lisp_Object args)
Michael I. Bushnell's avatar
Michael I. Bushnell committed
107 108 109 110 111 112
{
  return Qnil;
}

/* Quotify EXP: if EXP is constant, return it.
   If EXP is not constant, return (quote EXP).  */
113
static Lisp_Object
114
quotify_arg (register Lisp_Object exp)
Michael I. Bushnell's avatar
Michael I. Bushnell committed
115
{
116 117 118
  if (CONSP (exp)
      || (SYMBOLP (exp)
	  && !NILP (exp) && !EQ (exp, Qt)))
119
    return list2 (Qquote, exp);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
120 121 122 123 124

  return exp;
}

/* Modify EXP by quotifying each element (except the first).  */
125
static Lisp_Object
126
quotify_args (Lisp_Object exp)
Michael I. Bushnell's avatar
Michael I. Bushnell committed
127 128
{
  register Lisp_Object tail;
129 130
  Lisp_Object next;
  for (tail = exp; CONSP (tail); tail = next)
Michael I. Bushnell's avatar
Michael I. Bushnell committed
131
    {
132
      next = XCDR (tail);
133
      XSETCAR (tail, quotify_arg (XCAR (tail)));
Michael I. Bushnell's avatar
Michael I. Bushnell committed
134 135 136 137
    }
  return exp;
}

138
static const char *callint_argfuns[]
Michael I. Bushnell's avatar
Michael I. Bushnell committed
139 140 141
    = {"", "point", "mark", "region-beginning", "region-end"};

static void
142
check_mark (bool for_region)
Michael I. Bushnell's avatar
Michael I. Bushnell committed
143
{
144
  Lisp_Object tem;
Tom Tromey's avatar
Tom Tromey committed
145
  tem = Fmarker_buffer (BVAR (current_buffer, mark));
Jim Blandy's avatar
Jim Blandy committed
146
  if (NILP (tem) || (XBUFFER (tem) != current_buffer))
147 148
    error (for_region ? "The mark is not set now, so there is no region"
	   : "The mark is not set now");
149
  if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
Tom Tromey's avatar
Tom Tromey committed
150
      && NILP (BVAR (current_buffer, mark_active)))
Kim F. Storm's avatar
Kim F. Storm committed
151
    xsignal0 (Qmark_inactive);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
152 153
}

Richard M. Stallman's avatar
Richard M. Stallman committed
154 155 156 157 158 159 160 161
/* If the list of args INPUT was produced with an explicit call to
   `list', look for elements that were computed with
   (region-beginning) or (region-end), and put those expressions into
   VALUES instead of the present values.

   This function doesn't return a value because it modifies elements
   of VALUES to do its job.  */

162
static void
163
fix_command (Lisp_Object input, Lisp_Object values)
164
{
165
  /* FIXME: Instead of this ugly hack, we should provide a way for an
166 167
     interactive spec to return an expression/function that will re-build the
     args without user intervention.  */
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  if (CONSP (input))
    {
      Lisp_Object car;

      car = XCAR (input);
      /* Skip through certain special forms.  */
      while (EQ (car, Qlet) || EQ (car, Qletx)
	     || EQ (car, Qsave_excursion)
	     || EQ (car, Qprogn))
	{
	  while (CONSP (XCDR (input)))
	    input = XCDR (input);
	  input = XCAR (input);
	  if (!CONSP (input))
	    break;
	  car = XCAR (input);
	}
      if (EQ (car, Qlist))
	{
	  Lisp_Object intail, valtail;
	  for (intail = Fcdr (input), valtail = values;
	       CONSP (valtail);
Stefan Monnier's avatar
Stefan Monnier committed
190
	       intail = Fcdr (intail), valtail = XCDR (valtail))
191 192 193 194 195 196
	    {
	      Lisp_Object elt;
	      elt = Fcar (intail);
	      if (CONSP (elt))
		{
		  Lisp_Object presflag, carelt;
197
		  carelt = XCAR (elt);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		  /* If it is (if X Y), look at Y.  */
		  if (EQ (carelt, Qif)
		      && EQ (Fnthcdr (make_number (3), elt), Qnil))
		    elt = Fnth (make_number (2), elt);
		  /* If it is (when ... Y), look at Y.  */
		  else if (EQ (carelt, Qwhen))
		    {
		      while (CONSP (XCDR (elt)))
			elt = XCDR (elt);
		      elt = Fcar (elt);
		    }

		  /* If the function call we're looking at
		     is a special preserved one, copy the
		     whole expression for this argument.  */
		  if (CONSP (elt))
		    {
		      presflag = Fmemq (Fcar (elt), preserved_fns);
		      if (!NILP (presflag))
			Fsetcar (valtail, Fcar (intail));
		    }
		}
	    }
	}
    }
}
Michael I. Bushnell's avatar
Michael I. Bushnell committed
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
/* Helper function to call `read-file-name' from C.  */

static Lisp_Object
read_file_name (Lisp_Object default_filename, Lisp_Object mustmatch,
		Lisp_Object initial, Lisp_Object predicate)
{
  struct gcpro gcpro1;
  Lisp_Object args[7];

  GCPRO1 (default_filename);
  args[0] = intern ("read-file-name");
  args[1] = callint_message;
  args[2] = Qnil;
  args[3] = default_filename;
  args[4] = mustmatch;
  args[5] = initial;
  args[6] = predicate;
  RETURN_UNGCPRO (Ffuncall (7, args));
}

245 246 247 248
/* BEWARE: Calling this directly from C would defeat the purpose!  */
DEFUN ("funcall-interactively", Ffuncall_interactively, Sfuncall_interactively,
       1, MANY, 0, doc: /* Like `funcall' but marks the call as interactive.
I.e. arrange that within the called function `called-interactively-p' will
249 250
return non-nil.
usage: (funcall-interactively FUNCTION &rest ARGUMENTS)  */)
251 252 253 254 255 256 257 258 259 260 261
     (ptrdiff_t nargs, Lisp_Object *args)
{
  ptrdiff_t speccount = SPECPDL_INDEX ();
  temporarily_switch_to_single_kboard (NULL);

  /* Nothing special to do here, all the work is inside
     `called-interactively-p'.  Which will look for us as a marker in the
     backtrace.  */
  return unbind_to (speccount, Ffuncall (nargs, args));
}

262
DEFUN ("call-interactively", Fcall_interactively, Scall_interactively, 1, 3, 0,
263
       doc: /* Call FUNCTION, providing args according to its interactive calling specs.
264 265 266 267 268 269 270 271 272
Return the value FUNCTION returns.
The function contains a specification of how to do the argument reading.
In the case of user-defined functions, this is specified by placing a call
to the function `interactive' at the top level of the function body.
See `interactive'.

Optional second arg RECORD-FLAG non-nil
means unconditionally put this command in the command-history.
Otherwise, this is done only if an arg is read using the minibuffer.
273

274
Optional third arg KEYS, if given, specifies the sequence of events to
275 276 277
supply, as a vector, if the command inquires which events were used to
invoke it.  If KEYS is omitted or nil, the return value of
`this-command-keys-vector' is used.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
278
  (Lisp_Object function, Lisp_Object record_flag, Lisp_Object keys)
Michael I. Bushnell's avatar
Michael I. Bushnell committed
279
{
280 281 282
  /* `args' will contain the array of arguments to pass to the function.
     `visargs' will contain the same list but in a nicer form, so that if we
     pass it to `Fformat' it will be understandable to a human.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
283 284
  Lisp_Object *args, *visargs;
  Lisp_Object specs;
285
  Lisp_Object filter_specs;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
286
  Lisp_Object teml;
287
  Lisp_Object up_event;
Richard M. Stallman's avatar
Richard M. Stallman committed
288
  Lisp_Object enable;
289
  USE_SAFE_ALLOCA;
290
  ptrdiff_t speccount = SPECPDL_INDEX ();
Michael I. Bushnell's avatar
Michael I. Bushnell committed
291

292 293
  /* The index of the next element of this_command_keys to examine for
     the 'e' interactive code.  */
294
  ptrdiff_t next_event;
295

Michael I. Bushnell's avatar
Michael I. Bushnell committed
296
  Lisp_Object prefix_arg;
297
  char *string;
298
  const char *tem;
Jim Blandy's avatar
Jim Blandy committed
299 300 301 302

  /* If varies[i] > 0, the i'th argument shouldn't just have its value
     in this call quoted in the command history.  It should be
     recorded as a call to the function named callint_argfuns[varies[i]].  */
303
  signed char *varies;
Jim Blandy's avatar
Jim Blandy committed
304

305
  ptrdiff_t i, nargs;
306 307
  ptrdiff_t mark;
  bool arg_from_tty = 0;
308
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
309
  ptrdiff_t key_count;
310
  bool record_then_fail = 0;
311

312 313 314 315 316
  Lisp_Object save_this_command, save_last_command;
  Lisp_Object save_this_original_command, save_real_this_command;

  save_this_command = Vthis_command;
  save_this_original_command = Vthis_original_command;
317
  save_real_this_command = Vreal_this_command;
318
  save_last_command = KVAR (current_kboard, Vlast_command);
319

320 321 322 323
  if (NILP (keys))
    keys = this_command_keys, key_count = this_command_key_count;
  else
    {
324
      CHECK_VECTOR (keys);
325
      key_count = ASIZE (keys);
326
    }
Michael I. Bushnell's avatar
Michael I. Bushnell committed
327

328
  /* Save this now, since use of minibuffer will clobber it.  */
329
  prefix_arg = Vcurrent_prefix_arg;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
330

331
  if (SYMBOLP (function))
332
    enable = Fget (function, Qenable_recursive_minibuffers);
333 334
  else
    enable = Qnil;
Richard M. Stallman's avatar
Richard M. Stallman committed
335

Michael I. Bushnell's avatar
Michael I. Bushnell committed
336 337
  specs = Qnil;
  string = 0;
Juri Linkov's avatar
Juri Linkov committed
338
  /* The idea of FILTER_SPECS is to provide a way to
339 340 341
     specify how to represent the arguments in command history.
     The feature is not fully implemented.  */
  filter_specs = Qnil;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
342

343 344
  /* If k or K discard an up-event, save it here so it can be retrieved with
     U.  */
345 346
  up_event = Qnil;

347
  /* Set SPECS to the interactive form, or barf if not interactive.  */
348 349 350 351 352 353 354 355 356 357
  {
    Lisp_Object form;
    GCPRO2 (function, prefix_arg);
    form = Finteractive_form (function);
    UNGCPRO;
    if (CONSP (form))
      specs = filter_specs = Fcar (XCDR (form));
    else
      wrong_type_argument (Qcommandp, function);
  }
Michael I. Bushnell's avatar
Michael I. Bushnell committed
358

359 360
  /* If SPECS is not a string, invent one.  */
  if (! STRINGP (specs))
Michael I. Bushnell's avatar
Michael I. Bushnell committed
361
    {
362
      Lisp_Object input;
363
      Lisp_Object funval = Findirect_function (function, Qt);
364
      uintmax_t events = num_input_events;
365 366
      input = specs;
      /* Compute the arg values using the user's expression.  */
367
      GCPRO2 (input, filter_specs);
368
      specs = Feval (specs,
369 370
 		     CONSP (funval) && EQ (Qclosure, XCAR (funval))
		     ? CAR_SAFE (XCDR (funval)) : Qnil);
371
      UNGCPRO;
372
      if (events != num_input_events || !NILP (record_flag))
373 374
	{
	  /* We should record this command on the command history.  */
375
	  Lisp_Object values;
376
	  Lisp_Object this_cmd;
377 378 379
	  /* Make a copy of the list of values, for the command history,
	     and turn them into things we can eval.  */
	  values = quotify_args (Fcopy_sequence (specs));
380
	  fix_command (input, values);
381 382 383 384
	  this_cmd = Fcons (function, values);
	  if (history_delete_duplicates)
	    Vcommand_history = Fdelete (this_cmd, Vcommand_history);
	  Vcommand_history = Fcons (this_cmd, Vcommand_history);
385 386

	  /* Don't keep command history around forever.  */
387
	  if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
388 389 390
	    {
	      teml = Fnthcdr (Vhistory_length, Vcommand_history);
	      if (CONSP (teml))
391
		XSETCDR (teml, Qnil);
392
	    }
393
	}
394 395 396

      Vthis_command = save_this_command;
      Vthis_original_command = save_this_original_command;
397
      Vreal_this_command = save_real_this_command;
Paul Eggert's avatar
Paul Eggert committed
398
      kset_last_command (current_kboard, save_last_command);
399

400 401 402 403 404
      {
	Lisp_Object args[3];
	args[0] = Qfuncall_interactively;
	args[1] = function;
	args[2] = specs;
405 406 407
	Lisp_Object result = unbind_to (speccount, Fapply (3, args));
	SAFE_FREE ();
	return result;
408
      }
Michael I. Bushnell's avatar
Michael I. Bushnell committed
409 410
    }

411 412 413 414
  /* SPECS is set to a string; use it as an interactive prompt.
     Copy it so that STRING will be valid even if a GC relocates SPECS.  */
  SAFE_ALLOCA_STRING (string, specs);

415
  /* Here if function specifies a string to control parsing the defaults.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
416

Jim Blandy's avatar
Jim Blandy committed
417
  /* Set next_event to point to the first event with parameters.  */
418
  for (next_event = 0; next_event < key_count; next_event++)
419
    if (EVENT_HAS_PARAMETERS (AREF (keys, next_event)))
Jim Blandy's avatar
Jim Blandy committed
420
      break;
421

422
  /* Handle special starting chars `*' and `@'.  Also `-'.  */
423
  /* Note that `+' is reserved for user extensions.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
424 425
  while (1)
    {
426
      if (*string == '+')
427 428
	error ("`+' is not used in `interactive' for ordinary commands");
      else if (*string == '*')
Michael I. Bushnell's avatar
Michael I. Bushnell committed
429 430
	{
	  string++;
Tom Tromey's avatar
Tom Tromey committed
431
	  if (!NILP (BVAR (current_buffer, read_only)))
432 433 434
	    {
	      if (!NILP (record_flag))
		{
435
		  char *p = string;
436 437 438 439
		  while (*p)
		    {
		      if (! (*p == 'r' || *p == 'p' || *p == 'P'
			     || *p == '\n'))
440
			Fbarf_if_buffer_read_only (Qnil);
441 442 443 444 445
		      p++;
		    }
		  record_then_fail = 1;
		}
	      else
446
		Fbarf_if_buffer_read_only (Qnil);
447
	    }
Michael I. Bushnell's avatar
Michael I. Bushnell committed
448
	}
449 450 451
      /* Ignore this for semi-compatibility with Lucid.  */
      else if (*string == '-')
	string++;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
452 453
      else if (*string == '@')
	{
454
	  Lisp_Object event, w;
Jim Blandy's avatar
Jim Blandy committed
455

456
	  event = (next_event < key_count
457
		   ? AREF (keys, next_event)
458
		   : Qnil);
Jim Blandy's avatar
Jim Blandy committed
459
	  if (EVENT_HAS_PARAMETERS (event)
460 461 462
	      && (w = XCDR (event), CONSP (w))
	      && (w = XCAR (w), CONSP (w))
	      && (w = XCAR (w), WINDOWP (w)))
463
	    {
464 465
	      if (MINI_WINDOW_P (XWINDOW (w))
		  && ! (minibuf_level > 0 && EQ (w, minibuf_window)))
466
		error ("Attempt to select inactive minibuffer window");
467 468

	      /* If the current buffer wants to clean up, let it.  */
469
              run_hook (Qmouse_leave_buffer_hook);
470

471
	      Fselect_window (w, Qnil);
472
	    }
Michael I. Bushnell's avatar
Michael I. Bushnell committed
473 474
	  string++;
	}
Chong Yidong's avatar
Chong Yidong committed
475 476
      else if (*string == '^')
	{
477
	  call0 (Qhandle_shift_selection);
Chong Yidong's avatar
Chong Yidong committed
478 479
	  string++;
	}
Michael I. Bushnell's avatar
Michael I. Bushnell committed
480 481 482
      else break;
    }

483 484 485
  /* Count the number of arguments, which is two (the function itself and
     `funcall-interactively') plus the number of arguments the interactive spec
     would have us give to the function.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
486
  tem = string;
487
  for (nargs = 2; *tem; )
Michael I. Bushnell's avatar
Michael I. Bushnell committed
488 489 490
    {
      /* 'r' specifications ("point and mark as 2 numeric args")
	 produce *two* arguments.  */
491
      if (*tem == 'r')
492
	nargs += 2;
493
      else
494
	nargs++;
495
      tem = strchr (tem, '\n');
Michael I. Bushnell's avatar
Michael I. Bushnell committed
496
      if (tem)
497
	++tem;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
498
      else
499
	break;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
500 501
    }

502 503
  if (MOST_POSITIVE_FIXNUM < min (PTRDIFF_MAX, SIZE_MAX) / word_size
      && MOST_POSITIVE_FIXNUM < nargs)
504 505
    memory_full (SIZE_MAX);

506 507 508 509 510
  /* Allocate them all at one go.  This wastes a bit of memory, but
     it's OK to trade space for speed.  */
  SAFE_NALLOCA (args, 3, nargs);
  visargs = args + nargs;
  varies = (signed char *) (visargs + nargs);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
511

512
  memclear (args, nargs * (2 * word_size + 1));
Michael I. Bushnell's avatar
Michael I. Bushnell committed
513

514
  GCPRO5 (prefix_arg, function, *args, *visargs, up_event);
515 516
  gcpro3.nvars = nargs;
  gcpro4.nvars = nargs;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
517

Richard M. Stallman's avatar
Richard M. Stallman committed
518 519 520
  if (!NILP (enable))
    specbind (Qenable_recursive_minibuffers, Qt);

Michael I. Bushnell's avatar
Michael I. Bushnell committed
521
  tem = string;
522
  for (i = 2; *tem; i++)
Michael I. Bushnell's avatar
Michael I. Bushnell committed
523
    {
524 525
      visargs[1] = make_string (tem + 1, strcspn (tem + 1, "\n"));
      if (strchr (SSDATA (visargs[1]), '%'))
526
	callint_message = Fformat (i - 1, visargs + 1);
527
      else
528
	callint_message = visargs[1];
Michael I. Bushnell's avatar
Michael I. Bushnell committed
529 530 531

      switch (*tem)
	{
532
	case 'a':		/* Symbol defined as a function.  */
533
	  visargs[i] = Fcompleting_read (callint_message,
534
					 Vobarray, Qfboundp, Qt,
535
					 Qnil, Qnil, Qnil, Qnil);
536
	  /* Passing args[i] directly stimulates compiler bug.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
537 538 539 540
	  teml = visargs[i];
	  args[i] = Fintern (teml, Qnil);
	  break;

541
	case 'b':   		/* Name of existing buffer.  */
542 543 544 545 546 547
	  args[i] = Fcurrent_buffer ();
	  if (EQ (selected_window, minibuf_window))
	    args[i] = Fother_buffer (args[i], Qnil, Qnil);
	  args[i] = Fread_buffer (callint_message, args[i], Qt);
	  break;

548
	case 'B':		/* Name of buffer, possibly nonexistent.  */
549 550 551
	  args[i] = Fread_buffer (callint_message,
				  Fother_buffer (Fcurrent_buffer (), Qnil, Qnil),
				  Qnil);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
552 553
	  break;

554
        case 'c':		/* Character.  */
555 556 557 558
	  /* Prompt in `minibuffer-prompt' face.  */
	  Fput_text_property (make_number (0),
			      make_number (SCHARS (callint_message)),
			      Qface, Qminibuffer_prompt, callint_message);
559
	  args[i] = Fread_char (callint_message, Qnil, Qnil);
Paul Eggert's avatar
Paul Eggert committed
560
	  message1_nolog (0);
561
	  /* Passing args[i] directly stimulates compiler bug.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
562
	  teml = args[i];
Glenn Morris's avatar
Glenn Morris committed
563 564
	  /* See bug#8479.  */
	  if (! CHARACTERP (teml)) error ("Non-character input-event");
Michael I. Bushnell's avatar
Michael I. Bushnell committed
565 566 567
	  visargs[i] = Fchar_to_string (teml);
	  break;

568
	case 'C':	      /* Command: symbol with interactive function.  */
569
	  visargs[i] = Fcompleting_read (callint_message,
570
					 Vobarray, Qcommandp,
571
					 Qt, Qnil, Qnil, Qnil, Qnil);
572
	  /* Passing args[i] directly stimulates compiler bug.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
573 574 575 576 577
	  teml = visargs[i];
	  args[i] = Fintern (teml, Qnil);
	  break;

	case 'd':		/* Value of point.  Does not do I/O.  */
578
	  set_marker_both (point_marker, Qnil, PT, PT_BYTE);
579
	  args[i] = point_marker;
Michael I. Bushnell's avatar
Michael I. Bushnell committed
580 581 582 583
	  /* visargs[i] = Qnil; */
	  varies[i] = 1;
	  break;

584
	case 'D':		/* Directory name.  */
585 586
	  args[i] = read_file_name (BVAR (current_buffer, directory), Qlambda, Qnil,
				    Qfile_directory_p);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
587 588
	  break;

589
	case 'f':		/* Existing file name.  */
590
	  args[i] = read_file_name (Qnil, Qlambda, Qnil, Qnil);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
591 592
	  break;

593
	case 'F':		/* Possibly nonexistent file name.  */
594
	  args[i] = read_file_name (Qnil, Qnil, Qnil, Qnil);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
595 596
	  break;

597
	case 'G':		/* Possibly nonexistent file name,
598
				   default to directory alone.  */
599
	  args[i] = read_file_name (Qnil, Qnil, empty_unibyte_string, Qnil);
600 601
	  break;

602
	case 'i':		/* Ignore an argument -- Does not do I/O.  */
603 604 605
	  varies[i] = -1;
	  break;

606
	case 'k':		/* Key sequence.  */
607
	  {
608
	    ptrdiff_t speccount1 = SPECPDL_INDEX ();
609
	    specbind (Qcursor_in_echo_area, Qt);
610 611 612 613
	    /* Prompt in `minibuffer-prompt' face.  */
	    Fput_text_property (make_number (0),
				make_number (SCHARS (callint_message)),
				Qface, Qminibuffer_prompt, callint_message);
614
	    args[i] = Fread_key_sequence (callint_message,
615
					  Qnil, Qnil, Qnil, Qnil);
616 617
	    unbind_to (speccount1, Qnil);
	    teml = args[i];
618
	    visargs[i] = Fkey_description (teml, Qnil);
619 620 621 622 623

	    /* If the key sequence ends with a down-event,
	       discard the following up-event.  */
	    teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
	    if (CONSP (teml))
624
	      teml = XCAR (teml);
625 626 627 628 629
	    if (SYMBOLP (teml))
	      {
		Lisp_Object tem2;

		teml = Fget (teml, intern ("event-symbol-elements"));
630 631
		/* Ignore first element, which is the base key.  */
		tem2 = Fmemq (intern ("down"), Fcdr (teml));
632
		if (! NILP (tem2))
Chong Yidong's avatar
Chong Yidong committed
633
		  up_event = Fread_event (Qnil, Qnil, Qnil);
634
	      }
635
	  }
636 637
	  break;

638
	case 'K':		/* Key sequence to be defined.  */
639
	  {
640
	    ptrdiff_t speccount1 = SPECPDL_INDEX ();
641
	    specbind (Qcursor_in_echo_area, Qt);
642 643 644 645
	    /* Prompt in `minibuffer-prompt' face.  */
	    Fput_text_property (make_number (0),
				make_number (SCHARS (callint_message)),
				Qface, Qminibuffer_prompt, callint_message);
646 647
	    args[i] = Fread_key_sequence_vector (callint_message,
						 Qnil, Qt, Qnil, Qnil);
648
	    teml = args[i];
649
	    visargs[i] = Fkey_description (teml, Qnil);
650
	    unbind_to (speccount1, Qnil);
651 652 653 654 655

	    /* If the key sequence ends with a down-event,
	       discard the following up-event.  */
	    teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
	    if (CONSP (teml))
656
	      teml = XCAR (teml);
657 658 659 660 661
	    if (SYMBOLP (teml))
	      {
		Lisp_Object tem2;

		teml = Fget (teml, intern ("event-symbol-elements"));
662 663
		/* Ignore first element, which is the base key.  */
		tem2 = Fmemq (intern ("down"), Fcdr (teml));
664
		if (! NILP (tem2))
Chong Yidong's avatar
Chong Yidong committed
665
		  up_event = Fread_event (Qnil, Qnil, Qnil);
666
	      }
667
	  }
Michael I. Bushnell's avatar
Michael I. Bushnell committed
668 669
	  break;

670
	case 'U':		/* Up event from last k or K.  */
671 672 673 674 675 676 677 678 679
	  if (!NILP (up_event))
	    {
	      args[i] = Fmake_vector (make_number (1), up_event);
	      up_event = Qnil;
	      teml = args[i];
	      visargs[i] = Fkey_description (teml, Qnil);
	    }
	  break;

680
	case 'e':		/* The invoking event.  */
681
	  if (next_event >= key_count)
682
	    error ("%s must be bound to an event with parameters",
683
		   (SYMBOLP (function)
684
		    ? SSDATA (SYMBOL_NAME (function))
685
		    : "command"));
686 687
	  args[i] = AREF (keys, next_event);
	  next_event++;
Jim Blandy's avatar
Jim Blandy committed
688
	  varies[i] = -1;
Jim Blandy's avatar
Jim Blandy committed
689 690

	  /* Find the next parameterized event.  */
691
	  while (next_event < key_count
692
		 && !(EVENT_HAS_PARAMETERS (AREF (keys, next_event))))
Jim Blandy's avatar
Jim Blandy committed
693 694
	    next_event++;

Jim Blandy's avatar
Jim Blandy committed
695 696
	  break;

Michael I. Bushnell's avatar
Michael I. Bushnell committed
697
	case 'm':		/* Value of mark.  Does not do I/O.  */
698
	  check_mark (0);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
699
	  /* visargs[i] = Qnil; */
Tom Tromey's avatar
Tom Tromey committed
700
	  args[i] = BVAR (current_buffer, mark);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
701 702 703
	  varies[i] = 2;
	  break;

704 705
	case 'M':		/* String read via minibuffer with
				   inheriting the current input method.  */
706
	  args[i] = Fread_string (callint_message,
707 708 709
				  Qnil, Qnil, Qnil, Qt);
	  break;

710
	case 'N':     /* Prefix arg as number, else number from minibuffer.  */
Jim Blandy's avatar
Jim Blandy committed
711
	  if (!NILP (prefix_arg))
Michael I. Bushnell's avatar
Michael I. Bushnell committed
712 713
	    goto have_prefix_arg;
	case 'n':		/* Read number from minibuffer.  */
714
	  args[i] = call1 (Qread_number, callint_message);
715 716 717
	  /* Passing args[i] directly stimulates compiler bug.  */
	  teml = args[i];
	  visargs[i] = Fnumber_to_string (teml);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
718 719 720 721 722 723 724 725
	  break;

	case 'P':		/* Prefix arg in raw form.  Does no I/O.  */
	  args[i] = prefix_arg;
	  /* visargs[i] = Qnil; */
	  varies[i] = -1;
	  break;

726
	case 'p':		/* Prefix arg converted to number.  No I/O.  */
727
	have_prefix_arg:
Michael I. Bushnell's avatar
Michael I. Bushnell committed
728 729 730 731 732
	  args[i] = Fprefix_numeric_value (prefix_arg);
	  /* visargs[i] = Qnil; */
	  varies[i] = -1;
	  break;

733
	case 'r':		/* Region, point and mark as 2 args.  */
734
	  check_mark (1);
735
	  set_marker_both (point_marker, Qnil, PT, PT_BYTE);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
736
	  /* visargs[i+1] = Qnil; */
737
	  mark = marker_position (BVAR (current_buffer, mark));
Michael I. Bushnell's avatar
Michael I. Bushnell committed
738
	  /* visargs[i] = Qnil; */
739
	  args[i] = PT < mark ? point_marker : BVAR (current_buffer, mark);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
740
	  varies[i] = 3;
741
	  args[++i] = PT > mark ? point_marker : BVAR (current_buffer, mark);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
742 743 744
	  varies[i] = 4;
	  break;

745 746
	case 's':		/* String read via minibuffer without
				   inheriting the current input method.  */
747
	  args[i] = Fread_string (callint_message,
748
				  Qnil, Qnil, Qnil, Qnil);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
749 750 751
	  break;

	case 'S':		/* Any symbol.  */
752
	  visargs[i] = Fread_string (callint_message,
753
				     Qnil, Qnil, Qnil, Qnil);
754
	  /* Passing args[i] directly stimulates compiler bug.  */
Michael I. Bushnell's avatar
Michael I. Bushnell committed
755 756 757 758 759
	  teml = visargs[i];
	  args[i] = Fintern (teml, Qnil);
	  break;

	case 'v':		/* Variable name: symbol that is
760
				   custom-variable-p.  */
761
	  args[i] = Fread_variable (callint_message, Qnil);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
762 763 764
	  visargs[i] = last_minibuf_string;
	  break;

765
	case 'x':		/* Lisp expression read but not evaluated.  */
766
	  args[i] = call1 (intern ("read-minibuffer"), callint_message);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
767 768 769
	  visargs[i] = last_minibuf_string;
	  break;

770
	case 'X':		/* Lisp expression read and evaluated.  */
771
	  args[i] = call1 (intern ("eval-minibuffer"), callint_message);
Michael I. Bushnell's avatar
Michael I. Bushnell committed
772 773 774
	  visargs[i] = last_minibuf_string;
 	  break;

775
	case 'Z':		/* Coding-system symbol, or ignore the
776
				   argument if no prefix.  */
777 778
	  if (NILP (prefix_arg))
	    {
779
	      /* args[i] = Qnil; */
780 781
	      varies[i] = -1;
	    }
782
	  else
783 784
	    {
	      args[i]
785
		= Fread_non_nil_coding_system (callint_message);
786 787 788 789
	      visargs[i] = last_minibuf_string;
	    }
	  break;

790
	case 'z':		/* Coding-system symbol or nil.  */
791
	  args[i] = Fread_coding_system (callint_message, Qnil);
792 793 794
	  visargs[i] = last_minibuf_string;
	  break;

795 796 797
	  /* We have a case for `+' so we get an error
	     if anyone tries to define one here.  */
	case '+':
Michael I. Bushnell's avatar
Michael I. Bushnell committed
798
	default:
799 800 801 802
	  error ("Invalid control letter `%c' (#o%03o, #x%04x) in interactive calling string",
		 STRING_CHAR ((unsigned char *) tem),
		 (unsigned) STRING_CHAR ((unsigned char *) tem),
		 (unsigned) STRING_CHAR ((unsigned char *) tem));
Michael I. Bushnell's avatar
Michael I. Bushnell committed
803 804 805 806 807
	}

      if (varies[i] == 0)
	arg_from_tty = 1;

808
      if (NILP (visargs[i]) && STRINGP (args[i]))
Michael I. Bushnell's avatar
Michael I. Bushnell committed
809 810
	visargs[i] = args[i];

811
      tem = strchr (tem, '\n');
Michael I. Bushnell's avatar
Michael I. Bushnell committed
812
      if (tem) tem++;
813
      else tem = "";
Michael I. Bushnell's avatar
Michael I. Bushnell committed
814
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
815
  unbind_to (speccount, Qnil);
Michael I. Bushnell's avatar