Require cl-lib at runtime in vc-hg
[emacs.git] / src / callint.c
blobd96454883cf7f215382b72a6a1161d60d690bb3e
1 /* Call a Lisp function interactively.
2 Copyright (C) 1985-1986, 1993-1995, 1997, 2000-2017 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 (at
10 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 "keyboard.h"
27 #include "window.h"
29 static Lisp_Object preserved_fns;
31 /* Marker used within call-interactively to refer to point. */
32 static Lisp_Object point_marker;
34 /* String for the prompt text used in Fcall_interactively. */
35 static Lisp_Object callint_message;
37 /* ARGSUSED */
38 DEFUN ("interactive", Finteractive, Sinteractive, 0, UNEVALLED, 0,
39 doc: /* Specify a way of parsing arguments for interactive use of a function.
40 For example, write
41 (defun foo (arg buf) "Doc string" (interactive "P\\nbbuffer: ") .... )
42 to make ARG be the raw prefix argument, and set BUF to an existing buffer,
43 when `foo' is called as a command.
45 The "call" to `interactive' is actually a declaration rather than a
46 function; it tells `call-interactively' how to read arguments to pass
47 to the function. When actually called, `interactive' just returns
48 nil.
50 Usually the argument of `interactive' is a string containing a code
51 letter followed optionally by a prompt. (Some code letters do not
52 use I/O to get the argument and do not use prompts.) To pass several
53 arguments to the command, concatenate the individual strings,
54 separating them by newline characters.
56 Prompts are passed to `format', and may use % escapes to print the
57 arguments that have already been read.
58 If the argument is not a string, it is evaluated to get a list of
59 arguments to pass to the command.
60 Just `(interactive)' means pass no arguments to the command when
61 calling interactively.
63 Code letters available are:
64 a -- Function name: symbol with a function definition.
65 b -- Name of existing buffer.
66 B -- Name of buffer, possibly nonexistent.
67 c -- Character (no input method is used).
68 C -- Command name: symbol with interactive function definition.
69 d -- Value of point as number. Does not do I/O.
70 D -- Directory name.
71 e -- Parameterized event (i.e., one that's a list) that invoked this command.
72 If used more than once, the Nth `e' returns the Nth parameterized event.
73 This skips events that are integers or symbols.
74 f -- Existing file name.
75 F -- Possibly nonexistent file name.
76 G -- Possibly nonexistent file name, defaulting to just directory name.
77 i -- Ignored, i.e. always nil. Does not do I/O.
78 k -- Key sequence (downcase the last event if needed to get a definition).
79 K -- Key sequence to be redefined (do not downcase the last event).
80 m -- Value of mark as number. Does not do I/O.
81 M -- Any string. Inherits the current input method.
82 n -- Number read using minibuffer.
83 N -- Numeric prefix arg, or if none, do like code `n'.
84 p -- Prefix arg converted to number. Does not do I/O.
85 P -- Prefix arg in raw form. Does not do I/O.
86 r -- Region: point and mark as 2 numeric args, smallest first. Does no I/O.
87 s -- Any string. Does not inherit the current input method.
88 S -- Any symbol.
89 U -- Mouse up event discarded by a previous k or K argument.
90 v -- Variable name: symbol that is `custom-variable-p'.
91 x -- Lisp expression read but not evaluated.
92 X -- Lisp expression read and evaluated.
93 z -- Coding system.
94 Z -- Coding system, nil if no prefix arg.
96 In addition, if the string begins with `*', an error is signaled if
97 the buffer is read-only.
98 If `@' appears at the beginning of the string, and if the key sequence
99 used to invoke the command includes any mouse events, then the window
100 associated with the first of those events is selected before the
101 command is run.
102 If the string begins with `^' and `shift-select-mode' is non-nil,
103 Emacs first calls the function `handle-shift-selection'.
104 You may use `@', `*', and `^' together. They are processed in the
105 order that they appear, before reading any arguments.
106 usage: (interactive &optional ARG-DESCRIPTOR) */
107 attributes: const)
108 (Lisp_Object args)
110 return Qnil;
113 /* Quotify EXP: if EXP is constant, return it.
114 If EXP is not constant, return (quote EXP). */
115 static Lisp_Object
116 quotify_arg (register Lisp_Object exp)
118 if (CONSP (exp)
119 || (SYMBOLP (exp)
120 && !NILP (exp) && !EQ (exp, Qt)))
121 return list2 (Qquote, exp);
123 return exp;
126 /* Modify EXP by quotifying each element (except the first). */
127 static Lisp_Object
128 quotify_args (Lisp_Object exp)
130 register Lisp_Object tail;
131 Lisp_Object next;
132 for (tail = exp; CONSP (tail); tail = next)
134 next = XCDR (tail);
135 XSETCAR (tail, quotify_arg (XCAR (tail)));
137 return exp;
140 static const char *callint_argfuns[]
141 = {"", "point", "mark", "region-beginning", "region-end"};
143 static void
144 check_mark (bool for_region)
146 Lisp_Object tem;
147 tem = Fmarker_buffer (BVAR (current_buffer, mark));
148 if (NILP (tem) || (XBUFFER (tem) != current_buffer))
149 error (for_region ? "The mark is not set now, so there is no region"
150 : "The mark is not set now");
151 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
152 && NILP (BVAR (current_buffer, mark_active)))
153 xsignal0 (Qmark_inactive);
156 /* If the list of args INPUT was produced with an explicit call to
157 `list', look for elements that were computed with
158 (region-beginning) or (region-end), and put those expressions into
159 VALUES instead of the present values.
161 This function doesn't return a value because it modifies elements
162 of VALUES to do its job. */
164 static void
165 fix_command (Lisp_Object input, Lisp_Object values)
167 /* FIXME: Instead of this ugly hack, we should provide a way for an
168 interactive spec to return an expression/function that will re-build the
169 args without user intervention. */
170 if (CONSP (input))
172 Lisp_Object car;
174 car = XCAR (input);
175 /* Skip through certain special forms. */
176 while (EQ (car, Qlet) || EQ (car, Qletx)
177 || EQ (car, Qsave_excursion)
178 || EQ (car, Qprogn))
180 while (CONSP (XCDR (input)))
181 input = XCDR (input);
182 input = XCAR (input);
183 if (!CONSP (input))
184 break;
185 car = XCAR (input);
187 if (EQ (car, Qlist))
189 Lisp_Object intail, valtail;
190 for (intail = Fcdr (input), valtail = values;
191 CONSP (valtail);
192 intail = Fcdr (intail), valtail = XCDR (valtail))
194 Lisp_Object elt;
195 elt = Fcar (intail);
196 if (CONSP (elt))
198 Lisp_Object presflag, carelt;
199 carelt = XCAR (elt);
200 /* If it is (if X Y), look at Y. */
201 if (EQ (carelt, Qif)
202 && EQ (Fnthcdr (make_number (3), elt), Qnil))
203 elt = Fnth (make_number (2), elt);
204 /* If it is (when ... Y), look at Y. */
205 else if (EQ (carelt, Qwhen))
207 while (CONSP (XCDR (elt)))
208 elt = XCDR (elt);
209 elt = Fcar (elt);
212 /* If the function call we're looking at
213 is a special preserved one, copy the
214 whole expression for this argument. */
215 if (CONSP (elt))
217 presflag = Fmemq (Fcar (elt), preserved_fns);
218 if (!NILP (presflag))
219 Fsetcar (valtail, Fcar (intail));
227 /* Helper function to call `read-file-name' from C. */
229 static Lisp_Object
230 read_file_name (Lisp_Object default_filename, Lisp_Object mustmatch,
231 Lisp_Object initial, Lisp_Object predicate)
233 return CALLN (Ffuncall, intern ("read-file-name"),
234 callint_message, Qnil, default_filename,
235 mustmatch, initial, predicate);
238 /* BEWARE: Calling this directly from C would defeat the purpose! */
239 DEFUN ("funcall-interactively", Ffuncall_interactively, Sfuncall_interactively,
240 1, MANY, 0, doc: /* Like `funcall' but marks the call as interactive.
241 I.e. arrange that within the called function `called-interactively-p' will
242 return non-nil.
243 usage: (funcall-interactively FUNCTION &rest ARGUMENTS) */)
244 (ptrdiff_t nargs, Lisp_Object *args)
246 ptrdiff_t speccount = SPECPDL_INDEX ();
247 temporarily_switch_to_single_kboard (NULL);
249 /* Nothing special to do here, all the work is inside
250 `called-interactively-p'. Which will look for us as a marker in the
251 backtrace. */
252 return unbind_to (speccount, Ffuncall (nargs, args));
255 DEFUN ("call-interactively", Fcall_interactively, Scall_interactively, 1, 3, 0,
256 doc: /* Call FUNCTION, providing args according to its interactive calling specs.
257 Return the value FUNCTION returns.
258 The function contains a specification of how to do the argument reading.
259 In the case of user-defined functions, this is specified by placing a call
260 to the function `interactive' at the top level of the function body.
261 See `interactive'.
263 Optional second arg RECORD-FLAG non-nil
264 means unconditionally put this command in the command-history.
265 Otherwise, this is done only if an arg is read using the minibuffer.
267 Optional third arg KEYS, if given, specifies the sequence of events to
268 supply, as a vector, if the command inquires which events were used to
269 invoke it. If KEYS is omitted or nil, the return value of
270 `this-command-keys-vector' is used. */)
271 (Lisp_Object function, Lisp_Object record_flag, Lisp_Object keys)
273 /* `args' will contain the array of arguments to pass to the function.
274 `visargs' will contain the same list but in a nicer form, so that if we
275 pass it to `Fformat_message' it will be understandable to a human. */
276 Lisp_Object *args, *visargs;
277 Lisp_Object specs;
278 Lisp_Object filter_specs;
279 Lisp_Object teml;
280 Lisp_Object up_event;
281 Lisp_Object enable;
282 USE_SAFE_ALLOCA;
283 ptrdiff_t speccount = SPECPDL_INDEX ();
285 /* The index of the next element of this_command_keys to examine for
286 the 'e' interactive code. */
287 ptrdiff_t next_event;
289 Lisp_Object prefix_arg;
290 char *string;
291 const char *tem;
293 /* If varies[i] > 0, the i'th argument shouldn't just have its value
294 in this call quoted in the command history. It should be
295 recorded as a call to the function named callint_argfuns[varies[i]]. */
296 signed char *varies;
298 ptrdiff_t i, nargs;
299 ptrdiff_t mark;
300 bool arg_from_tty = 0;
301 ptrdiff_t key_count;
302 bool record_then_fail = 0;
304 Lisp_Object save_this_command, save_last_command;
305 Lisp_Object save_this_original_command, save_real_this_command;
307 save_this_command = Vthis_command;
308 save_this_original_command = Vthis_original_command;
309 save_real_this_command = Vreal_this_command;
310 save_last_command = KVAR (current_kboard, Vlast_command);
312 if (NILP (keys))
313 keys = this_command_keys, key_count = this_command_key_count;
314 else
316 CHECK_VECTOR (keys);
317 key_count = ASIZE (keys);
320 /* Save this now, since use of minibuffer will clobber it. */
321 prefix_arg = Vcurrent_prefix_arg;
323 if (SYMBOLP (function))
324 enable = Fget (function, Qenable_recursive_minibuffers);
325 else
326 enable = Qnil;
328 specs = Qnil;
329 string = 0;
330 /* The idea of FILTER_SPECS is to provide a way to
331 specify how to represent the arguments in command history.
332 The feature is not fully implemented. */
333 filter_specs = Qnil;
335 /* If k or K discard an up-event, save it here so it can be retrieved with
336 U. */
337 up_event = Qnil;
339 /* Set SPECS to the interactive form, or barf if not interactive. */
341 Lisp_Object form;
342 form = Finteractive_form (function);
343 if (CONSP (form))
344 specs = filter_specs = Fcar (XCDR (form));
345 else
346 wrong_type_argument (Qcommandp, function);
349 /* If SPECS is not a string, invent one. */
350 if (! STRINGP (specs))
352 Lisp_Object input;
353 Lisp_Object funval = Findirect_function (function, Qt);
354 uintmax_t events = num_input_events;
355 input = specs;
356 /* Compute the arg values using the user's expression. */
357 specs = Feval (specs,
358 CONSP (funval) && EQ (Qclosure, XCAR (funval))
359 ? CAR_SAFE (XCDR (funval)) : Qnil);
360 if (events != num_input_events || !NILP (record_flag))
362 /* We should record this command on the command history. */
363 Lisp_Object values;
364 Lisp_Object this_cmd;
365 /* Make a copy of the list of values, for the command history,
366 and turn them into things we can eval. */
367 values = quotify_args (Fcopy_sequence (specs));
368 fix_command (input, values);
369 this_cmd = Fcons (function, values);
370 if (history_delete_duplicates)
371 Vcommand_history = Fdelete (this_cmd, Vcommand_history);
372 Vcommand_history = Fcons (this_cmd, Vcommand_history);
374 /* Don't keep command history around forever. */
375 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
377 teml = Fnthcdr (Vhistory_length, Vcommand_history);
378 if (CONSP (teml))
379 XSETCDR (teml, Qnil);
383 Vthis_command = save_this_command;
384 Vthis_original_command = save_this_original_command;
385 Vreal_this_command = save_real_this_command;
386 kset_last_command (current_kboard, save_last_command);
388 Lisp_Object result
389 = unbind_to (speccount, CALLN (Fapply, Qfuncall_interactively,
390 function, specs));
391 SAFE_FREE ();
392 return result;
395 /* SPECS is set to a string; use it as an interactive prompt.
396 Copy it so that STRING will be valid even if a GC relocates SPECS. */
397 SAFE_ALLOCA_STRING (string, specs);
399 /* Here if function specifies a string to control parsing the defaults. */
401 /* Set next_event to point to the first event with parameters. */
402 for (next_event = 0; next_event < key_count; next_event++)
403 if (EVENT_HAS_PARAMETERS (AREF (keys, next_event)))
404 break;
406 /* Handle special starting chars `*' and `@'. Also `-'. */
407 /* Note that `+' is reserved for user extensions. */
408 while (1)
410 if (*string == '+')
411 error ("`+' is not used in `interactive' for ordinary commands");
412 else if (*string == '*')
414 string++;
415 if (!NILP (BVAR (current_buffer, read_only)))
417 if (!NILP (record_flag))
419 char *p = string;
420 while (*p)
422 if (! (*p == 'r' || *p == 'p' || *p == 'P'
423 || *p == '\n'))
424 Fbarf_if_buffer_read_only (Qnil);
425 p++;
427 record_then_fail = 1;
429 else
430 Fbarf_if_buffer_read_only (Qnil);
433 /* Ignore this for semi-compatibility with Lucid. */
434 else if (*string == '-')
435 string++;
436 else if (*string == '@')
438 Lisp_Object event, w;
440 event = (next_event < key_count
441 ? AREF (keys, next_event)
442 : Qnil);
443 if (EVENT_HAS_PARAMETERS (event)
444 && (w = XCDR (event), CONSP (w))
445 && (w = XCAR (w), CONSP (w))
446 && (w = XCAR (w), WINDOWP (w)))
448 if (MINI_WINDOW_P (XWINDOW (w))
449 && ! (minibuf_level > 0 && EQ (w, minibuf_window)))
450 error ("Attempt to select inactive minibuffer window");
452 /* If the current buffer wants to clean up, let it. */
453 run_hook (Qmouse_leave_buffer_hook);
455 Fselect_window (w, Qnil);
457 string++;
459 else if (*string == '^')
461 call0 (Qhandle_shift_selection);
462 string++;
464 else break;
467 /* Count the number of arguments, which is two (the function itself and
468 `funcall-interactively') plus the number of arguments the interactive spec
469 would have us give to the function. */
470 tem = string;
471 for (nargs = 2; *tem; )
473 /* 'r' specifications ("point and mark as 2 numeric args")
474 produce *two* arguments. */
475 if (*tem == 'r')
476 nargs += 2;
477 else
478 nargs++;
479 tem = strchr (tem, '\n');
480 if (tem)
481 ++tem;
482 else
483 break;
486 if (MOST_POSITIVE_FIXNUM < min (PTRDIFF_MAX, SIZE_MAX) / word_size
487 && MOST_POSITIVE_FIXNUM < nargs)
488 memory_full (SIZE_MAX);
490 /* Allocate them all at one go. This wastes a bit of memory, but
491 it's OK to trade space for speed. */
492 SAFE_NALLOCA (args, 3, nargs);
493 visargs = args + nargs;
494 varies = (signed char *) (visargs + nargs);
496 memclear (args, nargs * (2 * word_size + 1));
498 if (!NILP (enable))
499 specbind (Qenable_recursive_minibuffers, Qt);
501 tem = string;
502 for (i = 2; *tem; i++)
504 visargs[1] = make_string (tem + 1, strcspn (tem + 1, "\n"));
505 if (strchr (SSDATA (visargs[1]), '%'))
506 callint_message = Fformat_message (i - 1, visargs + 1);
507 else
508 callint_message = visargs[1];
510 switch (*tem)
512 case 'a': /* Symbol defined as a function. */
513 visargs[i] = Fcompleting_read (callint_message,
514 Vobarray, Qfboundp, Qt,
515 Qnil, Qnil, Qnil, Qnil);
516 /* Passing args[i] directly stimulates compiler bug. */
517 teml = visargs[i];
518 args[i] = Fintern (teml, Qnil);
519 break;
521 case 'b': /* Name of existing buffer. */
522 args[i] = Fcurrent_buffer ();
523 if (EQ (selected_window, minibuf_window))
524 args[i] = Fother_buffer (args[i], Qnil, Qnil);
525 args[i] = Fread_buffer (callint_message, args[i], Qt, Qnil);
526 break;
528 case 'B': /* Name of buffer, possibly nonexistent. */
529 args[i] = Fread_buffer (callint_message,
530 Fother_buffer (Fcurrent_buffer (), Qnil, Qnil),
531 Qnil, Qnil);
532 break;
534 case 'c': /* Character. */
535 /* Prompt in `minibuffer-prompt' face. */
536 Fput_text_property (make_number (0),
537 make_number (SCHARS (callint_message)),
538 Qface, Qminibuffer_prompt, callint_message);
539 args[i] = Fread_char (callint_message, Qnil, Qnil);
540 message1_nolog (0);
541 /* Passing args[i] directly stimulates compiler bug. */
542 teml = args[i];
543 /* See bug#8479. */
544 if (! CHARACTERP (teml)) error ("Non-character input-event");
545 visargs[i] = Fchar_to_string (teml);
546 break;
548 case 'C': /* Command: symbol with interactive function. */
549 visargs[i] = Fcompleting_read (callint_message,
550 Vobarray, Qcommandp,
551 Qt, Qnil, Qnil, Qnil, Qnil);
552 /* Passing args[i] directly stimulates compiler bug. */
553 teml = visargs[i];
554 args[i] = Fintern (teml, Qnil);
555 break;
557 case 'd': /* Value of point. Does not do I/O. */
558 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
559 args[i] = point_marker;
560 /* visargs[i] = Qnil; */
561 varies[i] = 1;
562 break;
564 case 'D': /* Directory name. */
565 args[i] = read_file_name (BVAR (current_buffer, directory), Qlambda, Qnil,
566 Qfile_directory_p);
567 break;
569 case 'f': /* Existing file name. */
570 args[i] = read_file_name (Qnil, Qlambda, Qnil, Qnil);
571 break;
573 case 'F': /* Possibly nonexistent file name. */
574 args[i] = read_file_name (Qnil, Qnil, Qnil, Qnil);
575 break;
577 case 'G': /* Possibly nonexistent file name,
578 default to directory alone. */
579 args[i] = read_file_name (Qnil, Qnil, empty_unibyte_string, Qnil);
580 break;
582 case 'i': /* Ignore an argument -- Does not do I/O. */
583 varies[i] = -1;
584 break;
586 case 'k': /* Key sequence. */
588 ptrdiff_t speccount1 = SPECPDL_INDEX ();
589 specbind (Qcursor_in_echo_area, Qt);
590 /* Prompt in `minibuffer-prompt' face. */
591 Fput_text_property (make_number (0),
592 make_number (SCHARS (callint_message)),
593 Qface, Qminibuffer_prompt, callint_message);
594 args[i] = Fread_key_sequence (callint_message,
595 Qnil, Qnil, Qnil, Qnil);
596 unbind_to (speccount1, Qnil);
597 teml = args[i];
598 visargs[i] = Fkey_description (teml, Qnil);
600 /* If the key sequence ends with a down-event,
601 discard the following up-event. */
602 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
603 if (CONSP (teml))
604 teml = XCAR (teml);
605 if (SYMBOLP (teml))
607 Lisp_Object tem2;
609 teml = Fget (teml, Qevent_symbol_elements);
610 /* Ignore first element, which is the base key. */
611 tem2 = Fmemq (Qdown, Fcdr (teml));
612 if (! NILP (tem2))
613 up_event = Fread_event (Qnil, Qnil, Qnil);
616 break;
618 case 'K': /* Key sequence to be defined. */
620 ptrdiff_t speccount1 = SPECPDL_INDEX ();
621 specbind (Qcursor_in_echo_area, Qt);
622 /* Prompt in `minibuffer-prompt' face. */
623 Fput_text_property (make_number (0),
624 make_number (SCHARS (callint_message)),
625 Qface, Qminibuffer_prompt, callint_message);
626 args[i] = Fread_key_sequence_vector (callint_message,
627 Qnil, Qt, Qnil, Qnil);
628 teml = args[i];
629 visargs[i] = Fkey_description (teml, Qnil);
630 unbind_to (speccount1, Qnil);
632 /* If the key sequence ends with a down-event,
633 discard the following up-event. */
634 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
635 if (CONSP (teml))
636 teml = XCAR (teml);
637 if (SYMBOLP (teml))
639 Lisp_Object tem2;
641 teml = Fget (teml, Qevent_symbol_elements);
642 /* Ignore first element, which is the base key. */
643 tem2 = Fmemq (Qdown, Fcdr (teml));
644 if (! NILP (tem2))
645 up_event = Fread_event (Qnil, Qnil, Qnil);
648 break;
650 case 'U': /* Up event from last k or K. */
651 if (!NILP (up_event))
653 args[i] = Fmake_vector (make_number (1), up_event);
654 up_event = Qnil;
655 teml = args[i];
656 visargs[i] = Fkey_description (teml, Qnil);
658 break;
660 case 'e': /* The invoking event. */
661 if (next_event >= key_count)
662 error ("%s must be bound to an event with parameters",
663 (SYMBOLP (function)
664 ? SSDATA (SYMBOL_NAME (function))
665 : "command"));
666 args[i] = AREF (keys, next_event);
667 next_event++;
668 varies[i] = -1;
670 /* Find the next parameterized event. */
671 while (next_event < key_count
672 && !(EVENT_HAS_PARAMETERS (AREF (keys, next_event))))
673 next_event++;
675 break;
677 case 'm': /* Value of mark. Does not do I/O. */
678 check_mark (0);
679 /* visargs[i] = Qnil; */
680 args[i] = BVAR (current_buffer, mark);
681 varies[i] = 2;
682 break;
684 case 'M': /* String read via minibuffer with
685 inheriting the current input method. */
686 args[i] = Fread_string (callint_message,
687 Qnil, Qnil, Qnil, Qt);
688 break;
690 case 'N': /* Prefix arg as number, else number from minibuffer. */
691 if (!NILP (prefix_arg))
692 goto have_prefix_arg;
693 case 'n': /* Read number from minibuffer. */
694 args[i] = call1 (Qread_number, callint_message);
695 /* Passing args[i] directly stimulates compiler bug. */
696 teml = args[i];
697 visargs[i] = Fnumber_to_string (teml);
698 break;
700 case 'P': /* Prefix arg in raw form. Does no I/O. */
701 args[i] = prefix_arg;
702 /* visargs[i] = Qnil; */
703 varies[i] = -1;
704 break;
706 case 'p': /* Prefix arg converted to number. No I/O. */
707 have_prefix_arg:
708 args[i] = Fprefix_numeric_value (prefix_arg);
709 /* visargs[i] = Qnil; */
710 varies[i] = -1;
711 break;
713 case 'r': /* Region, point and mark as 2 args. */
714 check_mark (1);
715 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
716 /* visargs[i+1] = Qnil; */
717 mark = marker_position (BVAR (current_buffer, mark));
718 /* visargs[i] = Qnil; */
719 args[i] = PT < mark ? point_marker : BVAR (current_buffer, mark);
720 varies[i] = 3;
721 args[++i] = PT > mark ? point_marker : BVAR (current_buffer, mark);
722 varies[i] = 4;
723 break;
725 case 's': /* String read via minibuffer without
726 inheriting the current input method. */
727 args[i] = Fread_string (callint_message,
728 Qnil, Qnil, Qnil, Qnil);
729 break;
731 case 'S': /* Any symbol. */
732 visargs[i] = Fread_string (callint_message,
733 Qnil, Qnil, Qnil, Qnil);
734 /* Passing args[i] directly stimulates compiler bug. */
735 teml = visargs[i];
736 args[i] = Fintern (teml, Qnil);
737 break;
739 case 'v': /* Variable name: symbol that is
740 custom-variable-p. */
741 args[i] = Fread_variable (callint_message, Qnil);
742 visargs[i] = last_minibuf_string;
743 break;
745 case 'x': /* Lisp expression read but not evaluated. */
746 args[i] = call1 (intern ("read-minibuffer"), callint_message);
747 visargs[i] = last_minibuf_string;
748 break;
750 case 'X': /* Lisp expression read and evaluated. */
751 args[i] = call1 (intern ("eval-minibuffer"), callint_message);
752 visargs[i] = last_minibuf_string;
753 break;
755 case 'Z': /* Coding-system symbol, or ignore the
756 argument if no prefix. */
757 if (NILP (prefix_arg))
759 /* args[i] = Qnil; */
760 varies[i] = -1;
762 else
764 args[i]
765 = Fread_non_nil_coding_system (callint_message);
766 visargs[i] = last_minibuf_string;
768 break;
770 case 'z': /* Coding-system symbol or nil. */
771 args[i] = Fread_coding_system (callint_message, Qnil);
772 visargs[i] = last_minibuf_string;
773 break;
775 /* We have a case for `+' so we get an error
776 if anyone tries to define one here. */
777 case '+':
778 default:
779 error ("Invalid control letter `%c' (#o%03o, #x%04x) in interactive calling string",
780 STRING_CHAR ((unsigned char *) tem),
781 (unsigned) STRING_CHAR ((unsigned char *) tem),
782 (unsigned) STRING_CHAR ((unsigned char *) tem));
785 if (varies[i] == 0)
786 arg_from_tty = 1;
788 if (NILP (visargs[i]) && STRINGP (args[i]))
789 visargs[i] = args[i];
791 tem = strchr (tem, '\n');
792 if (tem) tem++;
793 else tem = "";
795 unbind_to (speccount, Qnil);
797 maybe_quit ();
799 args[0] = Qfuncall_interactively;
800 args[1] = function;
802 if (arg_from_tty || !NILP (record_flag))
804 /* We don't need `visargs' any more, so let's recycle it since we need
805 an array of just the same size. */
806 visargs[1] = function;
807 for (i = 2; i < nargs; i++)
809 if (varies[i] > 0)
810 visargs[i] = list1 (intern (callint_argfuns[varies[i]]));
811 else
812 visargs[i] = quotify_arg (args[i]);
814 Vcommand_history = Fcons (Flist (nargs - 1, visargs + 1),
815 Vcommand_history);
816 /* Don't keep command history around forever. */
817 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
819 teml = Fnthcdr (Vhistory_length, Vcommand_history);
820 if (CONSP (teml))
821 XSETCDR (teml, Qnil);
825 /* If we used a marker to hold point, mark, or an end of the region,
826 temporarily, convert it to an integer now. */
827 for (i = 2; i < nargs; i++)
828 if (varies[i] >= 1 && varies[i] <= 4)
829 XSETINT (args[i], marker_position (args[i]));
831 if (record_then_fail)
832 Fbarf_if_buffer_read_only (Qnil);
834 Vthis_command = save_this_command;
835 Vthis_original_command = save_this_original_command;
836 Vreal_this_command = save_real_this_command;
837 kset_last_command (current_kboard, save_last_command);
840 Lisp_Object val;
841 specbind (Qcommand_debug_status, Qnil);
843 val = Ffuncall (nargs, args);
844 val = unbind_to (speccount, val);
845 SAFE_FREE ();
846 return val;
850 DEFUN ("prefix-numeric-value", Fprefix_numeric_value, Sprefix_numeric_value,
851 1, 1, 0,
852 doc: /* Return numeric meaning of raw prefix argument RAW.
853 A raw prefix argument is what you get from `(interactive "P")'.
854 Its numeric meaning is what you would get from `(interactive "p")'. */)
855 (Lisp_Object raw)
857 Lisp_Object val;
859 if (NILP (raw))
860 XSETFASTINT (val, 1);
861 else if (EQ (raw, Qminus))
862 XSETINT (val, -1);
863 else if (CONSP (raw) && INTEGERP (XCAR (raw)))
864 XSETINT (val, XINT (XCAR (raw)));
865 else if (INTEGERP (raw))
866 val = raw;
867 else
868 XSETFASTINT (val, 1);
870 return val;
873 void
874 syms_of_callint (void)
876 point_marker = Fmake_marker ();
877 staticpro (&point_marker);
879 callint_message = Qnil;
880 staticpro (&callint_message);
882 preserved_fns = listn (CONSTYPE_PURE, 4,
883 intern_c_string ("region-beginning"),
884 intern_c_string ("region-end"),
885 intern_c_string ("point"),
886 intern_c_string ("mark"));
888 DEFSYM (Qlist, "list");
889 DEFSYM (Qlet, "let");
890 DEFSYM (Qif, "if");
891 DEFSYM (Qwhen, "when");
892 DEFSYM (Qletx, "let*");
893 DEFSYM (Qsave_excursion, "save-excursion");
894 DEFSYM (Qprogn, "progn");
895 DEFSYM (Qminus, "-");
896 DEFSYM (Qplus, "+");
897 DEFSYM (Qhandle_shift_selection, "handle-shift-selection");
898 DEFSYM (Qread_number, "read-number");
899 DEFSYM (Qfuncall_interactively, "funcall-interactively");
900 DEFSYM (Qcommand_debug_status, "command-debug-status");
901 DEFSYM (Qenable_recursive_minibuffers, "enable-recursive-minibuffers");
902 DEFSYM (Qmouse_leave_buffer_hook, "mouse-leave-buffer-hook");
904 DEFVAR_KBOARD ("prefix-arg", Vprefix_arg,
905 doc: /* The value of the prefix argument for the next editing command.
906 It may be a number, or the symbol `-' for just a minus sign as arg,
907 or a list whose car is a number for just one or more C-u's
908 or nil if no argument has been specified.
910 You cannot examine this variable to find the argument for this command
911 since it has been set to nil by the time you can look.
912 Instead, you should use the variable `current-prefix-arg', although
913 normally commands can get this prefix argument with (interactive "P"). */);
915 DEFVAR_KBOARD ("last-prefix-arg", Vlast_prefix_arg,
916 doc: /* The value of the prefix argument for the previous editing command.
917 See `prefix-arg' for the meaning of the value. */);
919 DEFVAR_LISP ("current-prefix-arg", Vcurrent_prefix_arg,
920 doc: /* The value of the prefix argument for this editing command.
921 It may be a number, or the symbol `-' for just a minus sign as arg,
922 or a list whose car is a number for just one or more C-u's
923 or nil if no argument has been specified.
924 This is what `(interactive \"P\")' returns. */);
925 Vcurrent_prefix_arg = Qnil;
927 DEFVAR_LISP ("command-history", Vcommand_history,
928 doc: /* List of recent commands that read arguments from terminal.
929 Each command is represented as a form to evaluate.
931 Maximum length of the history list is determined by the value
932 of `history-length', which see. */);
933 Vcommand_history = Qnil;
935 DEFVAR_LISP ("command-debug-status", Vcommand_debug_status,
936 doc: /* Debugging status of current interactive command.
937 Bound each time `call-interactively' is called;
938 may be set by the debugger as a reminder for itself. */);
939 Vcommand_debug_status = Qnil;
941 DEFVAR_LISP ("mark-even-if-inactive", Vmark_even_if_inactive,
942 doc: /* Non-nil means you can use the mark even when inactive.
943 This option makes a difference in Transient Mark mode.
944 When the option is non-nil, deactivation of the mark
945 turns off region highlighting, but commands that use the mark
946 behave as if the mark were still active. */);
947 Vmark_even_if_inactive = Qt;
949 DEFVAR_LISP ("mouse-leave-buffer-hook", Vmouse_leave_buffer_hook,
950 doc: /* Hook to run when about to switch windows with a mouse command.
951 Its purpose is to give temporary modes such as Isearch mode
952 a way to turn themselves off when a mouse command switches windows. */);
953 Vmouse_leave_buffer_hook = Qnil;
955 defsubr (&Sinteractive);
956 defsubr (&Scall_interactively);
957 defsubr (&Sfuncall_interactively);
958 defsubr (&Sprefix_numeric_value);