Quoting fixes in lisp/progmodes
[emacs.git] / src / callint.c
blob12d116db28bb0564924d01eb756748c05d84ab5e
1 /* Call a Lisp function interactively.
2 Copyright (C) 1985-1986, 1993-1995, 1997, 2000-2015 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 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/>. */
21 #include <config.h>
23 #include "lisp.h"
24 #include "character.h"
25 #include "buffer.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "window.h"
29 #include "keymap.h"
31 static Lisp_Object preserved_fns;
33 /* Marker used within call-interactively to refer to point. */
34 static Lisp_Object point_marker;
36 /* String for the prompt text used in Fcall_interactively. */
37 static Lisp_Object callint_message;
39 /* ARGSUSED */
40 DEFUN ("interactive", Finteractive, Sinteractive, 0, UNEVALLED, 0,
41 doc: /* Specify a way of parsing arguments for interactive use of a function.
42 For example, write
43 (defun foo (arg buf) "Doc string" (interactive "P\\nbbuffer: ") .... )
44 to make ARG be the raw prefix argument, and set BUF to an existing buffer,
45 when `foo' is called as a command.
46 The "call" to `interactive' is actually a declaration rather than a function;
47 it tells `call-interactively' how to read arguments
48 to pass to the function.
49 When actually called, `interactive' just returns nil.
51 Usually the argument of `interactive' is a string containing a code letter
52 followed optionally by a prompt. (Some code letters do not use I/O to get
53 the argument and do not use prompts.) To get several arguments, concatenate
54 the individual strings, separating them by newline characters.
55 Prompts are passed to format, and may use % escapes to print the
56 arguments that have already been read.
57 If the argument is not a string, it is evaluated to get a list of
58 arguments to pass to the function.
59 Just `(interactive)' means pass no args when calling interactively.
61 Code letters available are:
62 a -- Function name: symbol with a function definition.
63 b -- Name of existing buffer.
64 B -- Name of buffer, possibly nonexistent.
65 c -- Character (no input method is used).
66 C -- Command name: symbol with interactive function definition.
67 d -- Value of point as number. Does not do I/O.
68 D -- Directory name.
69 e -- Parameterized event (i.e., one that's a list) that invoked this command.
70 If used more than once, the Nth `e' returns the Nth parameterized event.
71 This skips events that are integers or symbols.
72 f -- Existing file name.
73 F -- Possibly nonexistent file name.
74 G -- Possibly nonexistent file name, defaulting to just directory name.
75 i -- Ignored, i.e. always nil. Does not do I/O.
76 k -- Key sequence (downcase the last event if needed to get a definition).
77 K -- Key sequence to be redefined (do not downcase the last event).
78 m -- Value of mark as number. Does not do I/O.
79 M -- Any string. Inherits the current input method.
80 n -- Number read using minibuffer.
81 N -- Numeric prefix arg, or if none, do like code `n'.
82 p -- Prefix arg converted to number. Does not do I/O.
83 P -- Prefix arg in raw form. Does not do I/O.
84 r -- Region: point and mark as 2 numeric args, smallest first. Does no I/O.
85 s -- Any string. Does not inherit the current input method.
86 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 x -- Lisp expression read but not evaluated.
90 X -- Lisp expression read and evaluated.
91 z -- Coding system.
92 Z -- Coding system, nil if no prefix arg.
94 In addition, if the string begins with `*', an error is signaled if
95 the buffer is read-only.
96 If `@' appears at the beginning of the string, and if the key sequence
97 used to invoke the command includes any mouse events, then the window
98 associated with the first of those events is selected before the
99 command is run.
100 If the string begins with `^' and `shift-select-mode' is non-nil,
101 Emacs first calls the function `handle-shift-selection'.
102 You may use `@', `*', and `^' together. They are processed in the
103 order that they appear, before reading any arguments.
104 usage: (interactive &optional ARGS) */
105 attributes: const)
106 (Lisp_Object args)
108 return Qnil;
111 /* Quotify EXP: if EXP is constant, return it.
112 If EXP is not constant, return (quote EXP). */
113 static Lisp_Object
114 quotify_arg (register Lisp_Object exp)
116 if (CONSP (exp)
117 || (SYMBOLP (exp)
118 && !NILP (exp) && !EQ (exp, Qt)))
119 return list2 (Qquote, exp);
121 return exp;
124 /* Modify EXP by quotifying each element (except the first). */
125 static Lisp_Object
126 quotify_args (Lisp_Object exp)
128 register Lisp_Object tail;
129 Lisp_Object next;
130 for (tail = exp; CONSP (tail); tail = next)
132 next = XCDR (tail);
133 XSETCAR (tail, quotify_arg (XCAR (tail)));
135 return exp;
138 static const char *callint_argfuns[]
139 = {"", "point", "mark", "region-beginning", "region-end"};
141 static void
142 check_mark (bool for_region)
144 Lisp_Object tem;
145 tem = Fmarker_buffer (BVAR (current_buffer, mark));
146 if (NILP (tem) || (XBUFFER (tem) != current_buffer))
147 error (for_region ? "The mark is not set now, so there is no region"
148 : "The mark is not set now");
149 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
150 && NILP (BVAR (current_buffer, mark_active)))
151 xsignal0 (Qmark_inactive);
154 /* If the list of args INPUT was produced with an explicit call to
155 `list', look for elements that were computed with
156 (region-beginning) or (region-end), and put those expressions into
157 VALUES instead of the present values.
159 This function doesn't return a value because it modifies elements
160 of VALUES to do its job. */
162 static void
163 fix_command (Lisp_Object input, Lisp_Object values)
165 /* FIXME: Instead of this ugly hack, we should provide a way for an
166 interactive spec to return an expression/function that will re-build the
167 args without user intervention. */
168 if (CONSP (input))
170 Lisp_Object car;
172 car = XCAR (input);
173 /* Skip through certain special forms. */
174 while (EQ (car, Qlet) || EQ (car, Qletx)
175 || EQ (car, Qsave_excursion)
176 || EQ (car, Qprogn))
178 while (CONSP (XCDR (input)))
179 input = XCDR (input);
180 input = XCAR (input);
181 if (!CONSP (input))
182 break;
183 car = XCAR (input);
185 if (EQ (car, Qlist))
187 Lisp_Object intail, valtail;
188 for (intail = Fcdr (input), valtail = values;
189 CONSP (valtail);
190 intail = Fcdr (intail), valtail = XCDR (valtail))
192 Lisp_Object elt;
193 elt = Fcar (intail);
194 if (CONSP (elt))
196 Lisp_Object presflag, carelt;
197 carelt = XCAR (elt);
198 /* If it is (if X Y), look at Y. */
199 if (EQ (carelt, Qif)
200 && EQ (Fnthcdr (make_number (3), elt), Qnil))
201 elt = Fnth (make_number (2), elt);
202 /* If it is (when ... Y), look at Y. */
203 else if (EQ (carelt, Qwhen))
205 while (CONSP (XCDR (elt)))
206 elt = XCDR (elt);
207 elt = Fcar (elt);
210 /* If the function call we're looking at
211 is a special preserved one, copy the
212 whole expression for this argument. */
213 if (CONSP (elt))
215 presflag = Fmemq (Fcar (elt), preserved_fns);
216 if (!NILP (presflag))
217 Fsetcar (valtail, Fcar (intail));
225 /* Helper function to call `read-file-name' from C. */
227 static Lisp_Object
228 read_file_name (Lisp_Object default_filename, Lisp_Object mustmatch,
229 Lisp_Object initial, Lisp_Object predicate)
231 return CALLN (Ffuncall, intern ("read-file-name"),
232 callint_message, Qnil, default_filename,
233 mustmatch, initial, predicate);
236 /* BEWARE: Calling this directly from C would defeat the purpose! */
237 DEFUN ("funcall-interactively", Ffuncall_interactively, Sfuncall_interactively,
238 1, MANY, 0, doc: /* Like `funcall' but marks the call as interactive.
239 I.e. arrange that within the called function `called-interactively-p' will
240 return non-nil.
241 usage: (funcall-interactively FUNCTION &rest ARGUMENTS) */)
242 (ptrdiff_t nargs, Lisp_Object *args)
244 ptrdiff_t speccount = SPECPDL_INDEX ();
245 temporarily_switch_to_single_kboard (NULL);
247 /* Nothing special to do here, all the work is inside
248 `called-interactively-p'. Which will look for us as a marker in the
249 backtrace. */
250 return unbind_to (speccount, Ffuncall (nargs, args));
253 DEFUN ("call-interactively", Fcall_interactively, Scall_interactively, 1, 3, 0,
254 doc: /* Call FUNCTION, providing args according to its interactive calling specs.
255 Return the value FUNCTION returns.
256 The function contains a specification of how to do the argument reading.
257 In the case of user-defined functions, this is specified by placing a call
258 to the function `interactive' at the top level of the function body.
259 See `interactive'.
261 Optional second arg RECORD-FLAG non-nil
262 means unconditionally put this command in the command-history.
263 Otherwise, this is done only if an arg is read using the minibuffer.
265 Optional third arg KEYS, if given, specifies the sequence of events to
266 supply, as a vector, if the command inquires which events were used to
267 invoke it. If KEYS is omitted or nil, the return value of
268 `this-command-keys-vector' is used. */)
269 (Lisp_Object function, Lisp_Object record_flag, Lisp_Object keys)
271 /* `args' will contain the array of arguments to pass to the function.
272 `visargs' will contain the same list but in a nicer form, so that if we
273 pass it to `Fformat' it will be understandable to a human. */
274 Lisp_Object *args, *visargs;
275 Lisp_Object specs;
276 Lisp_Object filter_specs;
277 Lisp_Object teml;
278 Lisp_Object up_event;
279 Lisp_Object enable;
280 USE_SAFE_ALLOCA;
281 ptrdiff_t speccount = SPECPDL_INDEX ();
283 /* The index of the next element of this_command_keys to examine for
284 the 'e' interactive code. */
285 ptrdiff_t next_event;
287 Lisp_Object prefix_arg;
288 char *string;
289 const char *tem;
291 /* If varies[i] > 0, the i'th argument shouldn't just have its value
292 in this call quoted in the command history. It should be
293 recorded as a call to the function named callint_argfuns[varies[i]]. */
294 signed char *varies;
296 ptrdiff_t i, nargs;
297 ptrdiff_t mark;
298 bool arg_from_tty = 0;
299 ptrdiff_t key_count;
300 bool record_then_fail = 0;
302 Lisp_Object save_this_command, save_last_command;
303 Lisp_Object save_this_original_command, save_real_this_command;
305 save_this_command = Vthis_command;
306 save_this_original_command = Vthis_original_command;
307 save_real_this_command = Vreal_this_command;
308 save_last_command = KVAR (current_kboard, Vlast_command);
310 if (NILP (keys))
311 keys = this_command_keys, key_count = this_command_key_count;
312 else
314 CHECK_VECTOR (keys);
315 key_count = ASIZE (keys);
318 /* Save this now, since use of minibuffer will clobber it. */
319 prefix_arg = Vcurrent_prefix_arg;
321 if (SYMBOLP (function))
322 enable = Fget (function, Qenable_recursive_minibuffers);
323 else
324 enable = Qnil;
326 specs = Qnil;
327 string = 0;
328 /* The idea of FILTER_SPECS is to provide a way to
329 specify how to represent the arguments in command history.
330 The feature is not fully implemented. */
331 filter_specs = Qnil;
333 /* If k or K discard an up-event, save it here so it can be retrieved with
334 U. */
335 up_event = Qnil;
337 /* Set SPECS to the interactive form, or barf if not interactive. */
339 Lisp_Object form;
340 form = Finteractive_form (function);
341 if (CONSP (form))
342 specs = filter_specs = Fcar (XCDR (form));
343 else
344 wrong_type_argument (Qcommandp, function);
347 /* If SPECS is not a string, invent one. */
348 if (! STRINGP (specs))
350 Lisp_Object input;
351 Lisp_Object funval = Findirect_function (function, Qt);
352 uintmax_t events = num_input_events;
353 input = specs;
354 /* Compute the arg values using the user's expression. */
355 specs = Feval (specs,
356 CONSP (funval) && EQ (Qclosure, XCAR (funval))
357 ? CAR_SAFE (XCDR (funval)) : Qnil);
358 if (events != num_input_events || !NILP (record_flag))
360 /* We should record this command on the command history. */
361 Lisp_Object values;
362 Lisp_Object this_cmd;
363 /* Make a copy of the list of values, for the command history,
364 and turn them into things we can eval. */
365 values = quotify_args (Fcopy_sequence (specs));
366 fix_command (input, values);
367 this_cmd = Fcons (function, values);
368 if (history_delete_duplicates)
369 Vcommand_history = Fdelete (this_cmd, Vcommand_history);
370 Vcommand_history = Fcons (this_cmd, Vcommand_history);
372 /* Don't keep command history around forever. */
373 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
375 teml = Fnthcdr (Vhistory_length, Vcommand_history);
376 if (CONSP (teml))
377 XSETCDR (teml, Qnil);
381 Vthis_command = save_this_command;
382 Vthis_original_command = save_this_original_command;
383 Vreal_this_command = save_real_this_command;
384 kset_last_command (current_kboard, save_last_command);
386 Lisp_Object result
387 = unbind_to (speccount, CALLN (Fapply, Qfuncall_interactively,
388 function, specs));
389 SAFE_FREE ();
390 return result;
393 /* SPECS is set to a string; use it as an interactive prompt.
394 Copy it so that STRING will be valid even if a GC relocates SPECS. */
395 SAFE_ALLOCA_STRING (string, specs);
397 /* Here if function specifies a string to control parsing the defaults. */
399 /* Set next_event to point to the first event with parameters. */
400 for (next_event = 0; next_event < key_count; next_event++)
401 if (EVENT_HAS_PARAMETERS (AREF (keys, next_event)))
402 break;
404 /* Handle special starting chars `*' and `@'. Also `-'. */
405 /* Note that `+' is reserved for user extensions. */
406 while (1)
408 if (*string == '+')
409 error ("`+' is not used in `interactive' for ordinary commands");
410 else if (*string == '*')
412 string++;
413 if (!NILP (BVAR (current_buffer, read_only)))
415 if (!NILP (record_flag))
417 char *p = string;
418 while (*p)
420 if (! (*p == 'r' || *p == 'p' || *p == 'P'
421 || *p == '\n'))
422 Fbarf_if_buffer_read_only (Qnil);
423 p++;
425 record_then_fail = 1;
427 else
428 Fbarf_if_buffer_read_only (Qnil);
431 /* Ignore this for semi-compatibility with Lucid. */
432 else if (*string == '-')
433 string++;
434 else if (*string == '@')
436 Lisp_Object event, w;
438 event = (next_event < key_count
439 ? AREF (keys, next_event)
440 : Qnil);
441 if (EVENT_HAS_PARAMETERS (event)
442 && (w = XCDR (event), CONSP (w))
443 && (w = XCAR (w), CONSP (w))
444 && (w = XCAR (w), WINDOWP (w)))
446 if (MINI_WINDOW_P (XWINDOW (w))
447 && ! (minibuf_level > 0 && EQ (w, minibuf_window)))
448 error ("Attempt to select inactive minibuffer window");
450 /* If the current buffer wants to clean up, let it. */
451 run_hook (Qmouse_leave_buffer_hook);
453 Fselect_window (w, Qnil);
455 string++;
457 else if (*string == '^')
459 call0 (Qhandle_shift_selection);
460 string++;
462 else break;
465 /* Count the number of arguments, which is two (the function itself and
466 `funcall-interactively') plus the number of arguments the interactive spec
467 would have us give to the function. */
468 tem = string;
469 for (nargs = 2; *tem; )
471 /* 'r' specifications ("point and mark as 2 numeric args")
472 produce *two* arguments. */
473 if (*tem == 'r')
474 nargs += 2;
475 else
476 nargs++;
477 tem = strchr (tem, '\n');
478 if (tem)
479 ++tem;
480 else
481 break;
484 if (MOST_POSITIVE_FIXNUM < min (PTRDIFF_MAX, SIZE_MAX) / word_size
485 && MOST_POSITIVE_FIXNUM < nargs)
486 memory_full (SIZE_MAX);
488 /* Allocate them all at one go. This wastes a bit of memory, but
489 it's OK to trade space for speed. */
490 SAFE_NALLOCA (args, 3, nargs);
491 visargs = args + nargs;
492 varies = (signed char *) (visargs + nargs);
494 memclear (args, nargs * (2 * word_size + 1));
496 if (!NILP (enable))
497 specbind (Qenable_recursive_minibuffers, Qt);
499 tem = string;
500 for (i = 2; *tem; i++)
502 visargs[1] = make_string (tem + 1, strcspn (tem + 1, "\n"));
503 if (strchr (SSDATA (visargs[1]), '%'))
504 callint_message = Fformat_message (i - 1, visargs + 1);
505 else
506 callint_message = visargs[1];
508 switch (*tem)
510 case 'a': /* Symbol defined as a function. */
511 visargs[i] = Fcompleting_read (callint_message,
512 Vobarray, Qfboundp, Qt,
513 Qnil, Qnil, Qnil, Qnil);
514 /* Passing args[i] directly stimulates compiler bug. */
515 teml = visargs[i];
516 args[i] = Fintern (teml, Qnil);
517 break;
519 case 'b': /* Name of existing buffer. */
520 args[i] = Fcurrent_buffer ();
521 if (EQ (selected_window, minibuf_window))
522 args[i] = Fother_buffer (args[i], Qnil, Qnil);
523 args[i] = Fread_buffer (callint_message, args[i], Qt, Qnil);
524 break;
526 case 'B': /* Name of buffer, possibly nonexistent. */
527 args[i] = Fread_buffer (callint_message,
528 Fother_buffer (Fcurrent_buffer (), Qnil, Qnil),
529 Qnil, Qnil);
530 break;
532 case 'c': /* Character. */
533 /* Prompt in `minibuffer-prompt' face. */
534 Fput_text_property (make_number (0),
535 make_number (SCHARS (callint_message)),
536 Qface, Qminibuffer_prompt, callint_message);
537 args[i] = Fread_char (callint_message, Qnil, Qnil);
538 message1_nolog (0);
539 /* Passing args[i] directly stimulates compiler bug. */
540 teml = args[i];
541 /* See bug#8479. */
542 if (! CHARACTERP (teml)) error ("Non-character input-event");
543 visargs[i] = Fchar_to_string (teml);
544 break;
546 case 'C': /* Command: symbol with interactive function. */
547 visargs[i] = Fcompleting_read (callint_message,
548 Vobarray, Qcommandp,
549 Qt, Qnil, Qnil, Qnil, Qnil);
550 /* Passing args[i] directly stimulates compiler bug. */
551 teml = visargs[i];
552 args[i] = Fintern (teml, Qnil);
553 break;
555 case 'd': /* Value of point. Does not do I/O. */
556 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
557 args[i] = point_marker;
558 /* visargs[i] = Qnil; */
559 varies[i] = 1;
560 break;
562 case 'D': /* Directory name. */
563 args[i] = read_file_name (BVAR (current_buffer, directory), Qlambda, Qnil,
564 Qfile_directory_p);
565 break;
567 case 'f': /* Existing file name. */
568 args[i] = read_file_name (Qnil, Qlambda, Qnil, Qnil);
569 break;
571 case 'F': /* Possibly nonexistent file name. */
572 args[i] = read_file_name (Qnil, Qnil, Qnil, Qnil);
573 break;
575 case 'G': /* Possibly nonexistent file name,
576 default to directory alone. */
577 args[i] = read_file_name (Qnil, Qnil, empty_unibyte_string, Qnil);
578 break;
580 case 'i': /* Ignore an argument -- Does not do I/O. */
581 varies[i] = -1;
582 break;
584 case 'k': /* Key sequence. */
586 ptrdiff_t speccount1 = SPECPDL_INDEX ();
587 specbind (Qcursor_in_echo_area, Qt);
588 /* Prompt in `minibuffer-prompt' face. */
589 Fput_text_property (make_number (0),
590 make_number (SCHARS (callint_message)),
591 Qface, Qminibuffer_prompt, callint_message);
592 args[i] = Fread_key_sequence (callint_message,
593 Qnil, Qnil, Qnil, Qnil);
594 unbind_to (speccount1, Qnil);
595 teml = args[i];
596 visargs[i] = Fkey_description (teml, Qnil);
598 /* If the key sequence ends with a down-event,
599 discard the following up-event. */
600 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
601 if (CONSP (teml))
602 teml = XCAR (teml);
603 if (SYMBOLP (teml))
605 Lisp_Object tem2;
607 teml = Fget (teml, Qevent_symbol_elements);
608 /* Ignore first element, which is the base key. */
609 tem2 = Fmemq (Qdown, Fcdr (teml));
610 if (! NILP (tem2))
611 up_event = Fread_event (Qnil, Qnil, Qnil);
614 break;
616 case 'K': /* Key sequence to be defined. */
618 ptrdiff_t speccount1 = SPECPDL_INDEX ();
619 specbind (Qcursor_in_echo_area, Qt);
620 /* Prompt in `minibuffer-prompt' face. */
621 Fput_text_property (make_number (0),
622 make_number (SCHARS (callint_message)),
623 Qface, Qminibuffer_prompt, callint_message);
624 args[i] = Fread_key_sequence_vector (callint_message,
625 Qnil, Qt, Qnil, Qnil);
626 teml = args[i];
627 visargs[i] = Fkey_description (teml, Qnil);
628 unbind_to (speccount1, Qnil);
630 /* If the key sequence ends with a down-event,
631 discard the following up-event. */
632 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
633 if (CONSP (teml))
634 teml = XCAR (teml);
635 if (SYMBOLP (teml))
637 Lisp_Object tem2;
639 teml = Fget (teml, Qevent_symbol_elements);
640 /* Ignore first element, which is the base key. */
641 tem2 = Fmemq (Qdown, Fcdr (teml));
642 if (! NILP (tem2))
643 up_event = Fread_event (Qnil, Qnil, Qnil);
646 break;
648 case 'U': /* Up event from last k or K. */
649 if (!NILP (up_event))
651 args[i] = Fmake_vector (make_number (1), up_event);
652 up_event = Qnil;
653 teml = args[i];
654 visargs[i] = Fkey_description (teml, Qnil);
656 break;
658 case 'e': /* The invoking event. */
659 if (next_event >= key_count)
660 error ("%s must be bound to an event with parameters",
661 (SYMBOLP (function)
662 ? SSDATA (SYMBOL_NAME (function))
663 : "command"));
664 args[i] = AREF (keys, next_event);
665 next_event++;
666 varies[i] = -1;
668 /* Find the next parameterized event. */
669 while (next_event < key_count
670 && !(EVENT_HAS_PARAMETERS (AREF (keys, next_event))))
671 next_event++;
673 break;
675 case 'm': /* Value of mark. Does not do I/O. */
676 check_mark (0);
677 /* visargs[i] = Qnil; */
678 args[i] = BVAR (current_buffer, mark);
679 varies[i] = 2;
680 break;
682 case 'M': /* String read via minibuffer with
683 inheriting the current input method. */
684 args[i] = Fread_string (callint_message,
685 Qnil, Qnil, Qnil, Qt);
686 break;
688 case 'N': /* Prefix arg as number, else number from minibuffer. */
689 if (!NILP (prefix_arg))
690 goto have_prefix_arg;
691 case 'n': /* Read number from minibuffer. */
692 args[i] = call1 (Qread_number, callint_message);
693 /* Passing args[i] directly stimulates compiler bug. */
694 teml = args[i];
695 visargs[i] = Fnumber_to_string (teml);
696 break;
698 case 'P': /* Prefix arg in raw form. Does no I/O. */
699 args[i] = prefix_arg;
700 /* visargs[i] = Qnil; */
701 varies[i] = -1;
702 break;
704 case 'p': /* Prefix arg converted to number. No I/O. */
705 have_prefix_arg:
706 args[i] = Fprefix_numeric_value (prefix_arg);
707 /* visargs[i] = Qnil; */
708 varies[i] = -1;
709 break;
711 case 'r': /* Region, point and mark as 2 args. */
712 check_mark (1);
713 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
714 /* visargs[i+1] = Qnil; */
715 mark = marker_position (BVAR (current_buffer, mark));
716 /* visargs[i] = Qnil; */
717 args[i] = PT < mark ? point_marker : BVAR (current_buffer, mark);
718 varies[i] = 3;
719 args[++i] = PT > mark ? point_marker : BVAR (current_buffer, mark);
720 varies[i] = 4;
721 break;
723 case 's': /* String read via minibuffer without
724 inheriting the current input method. */
725 args[i] = Fread_string (callint_message,
726 Qnil, Qnil, Qnil, Qnil);
727 break;
729 case 'S': /* Any symbol. */
730 visargs[i] = Fread_string (callint_message,
731 Qnil, Qnil, Qnil, Qnil);
732 /* Passing args[i] directly stimulates compiler bug. */
733 teml = visargs[i];
734 args[i] = Fintern (teml, Qnil);
735 break;
737 case 'v': /* Variable name: symbol that is
738 custom-variable-p. */
739 args[i] = Fread_variable (callint_message, Qnil);
740 visargs[i] = last_minibuf_string;
741 break;
743 case 'x': /* Lisp expression read but not evaluated. */
744 args[i] = call1 (intern ("read-minibuffer"), callint_message);
745 visargs[i] = last_minibuf_string;
746 break;
748 case 'X': /* Lisp expression read and evaluated. */
749 args[i] = call1 (intern ("eval-minibuffer"), callint_message);
750 visargs[i] = last_minibuf_string;
751 break;
753 case 'Z': /* Coding-system symbol, or ignore the
754 argument if no prefix. */
755 if (NILP (prefix_arg))
757 /* args[i] = Qnil; */
758 varies[i] = -1;
760 else
762 args[i]
763 = Fread_non_nil_coding_system (callint_message);
764 visargs[i] = last_minibuf_string;
766 break;
768 case 'z': /* Coding-system symbol or nil. */
769 args[i] = Fread_coding_system (callint_message, Qnil);
770 visargs[i] = last_minibuf_string;
771 break;
773 /* We have a case for `+' so we get an error
774 if anyone tries to define one here. */
775 case '+':
776 default:
777 error ("Invalid control letter `%c' (#o%03o, #x%04x) in interactive calling string",
778 STRING_CHAR ((unsigned char *) tem),
779 (unsigned) STRING_CHAR ((unsigned char *) tem),
780 (unsigned) STRING_CHAR ((unsigned char *) tem));
783 if (varies[i] == 0)
784 arg_from_tty = 1;
786 if (NILP (visargs[i]) && STRINGP (args[i]))
787 visargs[i] = args[i];
789 tem = strchr (tem, '\n');
790 if (tem) tem++;
791 else tem = "";
793 unbind_to (speccount, Qnil);
795 QUIT;
797 args[0] = Qfuncall_interactively;
798 args[1] = function;
800 if (arg_from_tty || !NILP (record_flag))
802 /* We don't need `visargs' any more, so let's recycle it since we need
803 an array of just the same size. */
804 visargs[1] = function;
805 for (i = 2; i < nargs; i++)
807 if (varies[i] > 0)
808 visargs[i] = list1 (intern (callint_argfuns[varies[i]]));
809 else
810 visargs[i] = quotify_arg (args[i]);
812 Vcommand_history = Fcons (Flist (nargs - 1, visargs + 1),
813 Vcommand_history);
814 /* Don't keep command history around forever. */
815 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
817 teml = Fnthcdr (Vhistory_length, Vcommand_history);
818 if (CONSP (teml))
819 XSETCDR (teml, Qnil);
823 /* If we used a marker to hold point, mark, or an end of the region,
824 temporarily, convert it to an integer now. */
825 for (i = 2; i < nargs; i++)
826 if (varies[i] >= 1 && varies[i] <= 4)
827 XSETINT (args[i], marker_position (args[i]));
829 if (record_then_fail)
830 Fbarf_if_buffer_read_only (Qnil);
832 Vthis_command = save_this_command;
833 Vthis_original_command = save_this_original_command;
834 Vreal_this_command = save_real_this_command;
835 kset_last_command (current_kboard, save_last_command);
838 Lisp_Object val = Ffuncall (nargs, args);
839 val = unbind_to (speccount, val);
840 SAFE_FREE ();
841 return val;
845 DEFUN ("prefix-numeric-value", Fprefix_numeric_value, Sprefix_numeric_value,
846 1, 1, 0,
847 doc: /* Return numeric meaning of raw prefix argument RAW.
848 A raw prefix argument is what you get from `(interactive "P")'.
849 Its numeric meaning is what you would get from `(interactive "p")'. */)
850 (Lisp_Object raw)
852 Lisp_Object val;
854 if (NILP (raw))
855 XSETFASTINT (val, 1);
856 else if (EQ (raw, Qminus))
857 XSETINT (val, -1);
858 else if (CONSP (raw) && INTEGERP (XCAR (raw)))
859 XSETINT (val, XINT (XCAR (raw)));
860 else if (INTEGERP (raw))
861 val = raw;
862 else
863 XSETFASTINT (val, 1);
865 return val;
868 void
869 syms_of_callint (void)
871 point_marker = Fmake_marker ();
872 staticpro (&point_marker);
874 callint_message = Qnil;
875 staticpro (&callint_message);
877 preserved_fns = listn (CONSTYPE_PURE, 4,
878 intern_c_string ("region-beginning"),
879 intern_c_string ("region-end"),
880 intern_c_string ("point"),
881 intern_c_string ("mark"));
883 DEFSYM (Qlist, "list");
884 DEFSYM (Qlet, "let");
885 DEFSYM (Qif, "if");
886 DEFSYM (Qwhen, "when");
887 DEFSYM (Qletx, "let*");
888 DEFSYM (Qsave_excursion, "save-excursion");
889 DEFSYM (Qprogn, "progn");
890 DEFSYM (Qminus, "-");
891 DEFSYM (Qplus, "+");
892 DEFSYM (Qhandle_shift_selection, "handle-shift-selection");
893 DEFSYM (Qread_number, "read-number");
894 DEFSYM (Qfuncall_interactively, "funcall-interactively");
895 DEFSYM (Qenable_recursive_minibuffers, "enable-recursive-minibuffers");
896 DEFSYM (Qmouse_leave_buffer_hook, "mouse-leave-buffer-hook");
898 DEFVAR_KBOARD ("prefix-arg", Vprefix_arg,
899 doc: /* The value of the prefix argument for the next editing command.
900 It may be a number, or the symbol `-' for just a minus sign as arg,
901 or a list whose car is a number for just one or more C-u's
902 or nil if no argument has been specified.
904 You cannot examine this variable to find the argument for this command
905 since it has been set to nil by the time you can look.
906 Instead, you should use the variable `current-prefix-arg', although
907 normally commands can get this prefix argument with (interactive "P"). */);
909 DEFVAR_KBOARD ("last-prefix-arg", Vlast_prefix_arg,
910 doc: /* The value of the prefix argument for the previous editing command.
911 See `prefix-arg' for the meaning of the value. */);
913 DEFVAR_LISP ("current-prefix-arg", Vcurrent_prefix_arg,
914 doc: /* The value of the prefix argument for this editing command.
915 It may be a number, or the symbol `-' for just a minus sign as arg,
916 or a list whose car is a number for just one or more C-u's
917 or nil if no argument has been specified.
918 This is what `(interactive \"P\")' returns. */);
919 Vcurrent_prefix_arg = Qnil;
921 DEFVAR_LISP ("command-history", Vcommand_history,
922 doc: /* List of recent commands that read arguments from terminal.
923 Each command is represented as a form to evaluate.
925 Maximum length of the history list is determined by the value
926 of `history-length', which see. */);
927 Vcommand_history = Qnil;
929 DEFVAR_LISP ("command-debug-status", Vcommand_debug_status,
930 doc: /* Debugging status of current interactive command.
931 Bound each time `call-interactively' is called;
932 may be set by the debugger as a reminder for itself. */);
933 Vcommand_debug_status = Qnil;
935 DEFVAR_LISP ("mark-even-if-inactive", Vmark_even_if_inactive,
936 doc: /* Non-nil means you can use the mark even when inactive.
937 This option makes a difference in Transient Mark mode.
938 When the option is non-nil, deactivation of the mark
939 turns off region highlighting, but commands that use the mark
940 behave as if the mark were still active. */);
941 Vmark_even_if_inactive = Qt;
943 DEFVAR_LISP ("mouse-leave-buffer-hook", Vmouse_leave_buffer_hook,
944 doc: /* Hook to run when about to switch windows with a mouse command.
945 Its purpose is to give temporary modes such as Isearch mode
946 a way to turn themselves off when a mouse command switches windows. */);
947 Vmouse_leave_buffer_hook = Qnil;
949 defsubr (&Sinteractive);
950 defsubr (&Scall_interactively);
951 defsubr (&Sfuncall_interactively);
952 defsubr (&Sprefix_numeric_value);