Merge from Gnulib
[emacs.git] / src / callint.c
blob5d88082e38d87e365f5fb275fdec0f6143756430
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 <https://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 callint_message = Fformat_message (i - 1, visargs + 1);
507 switch (*tem)
509 case 'a': /* Symbol defined as a function. */
510 visargs[i] = Fcompleting_read (callint_message,
511 Vobarray, Qfboundp, Qt,
512 Qnil, Qnil, Qnil, Qnil);
513 /* Passing args[i] directly stimulates compiler bug. */
514 teml = visargs[i];
515 args[i] = Fintern (teml, Qnil);
516 break;
518 case 'b': /* Name of existing buffer. */
519 args[i] = Fcurrent_buffer ();
520 if (EQ (selected_window, minibuf_window))
521 args[i] = Fother_buffer (args[i], Qnil, Qnil);
522 args[i] = Fread_buffer (callint_message, args[i], Qt, Qnil);
523 break;
525 case 'B': /* Name of buffer, possibly nonexistent. */
526 args[i] = Fread_buffer (callint_message,
527 Fother_buffer (Fcurrent_buffer (), Qnil, Qnil),
528 Qnil, Qnil);
529 break;
531 case 'c': /* Character. */
532 /* Prompt in `minibuffer-prompt' face. */
533 Fput_text_property (make_number (0),
534 make_number (SCHARS (callint_message)),
535 Qface, Qminibuffer_prompt, callint_message);
536 args[i] = Fread_char (callint_message, Qnil, Qnil);
537 message1_nolog (0);
538 /* Passing args[i] directly stimulates compiler bug. */
539 teml = args[i];
540 /* See bug#8479. */
541 if (! CHARACTERP (teml)) error ("Non-character input-event");
542 visargs[i] = Fchar_to_string (teml);
543 break;
545 case 'C': /* Command: symbol with interactive function. */
546 visargs[i] = Fcompleting_read (callint_message,
547 Vobarray, Qcommandp,
548 Qt, Qnil, Qnil, Qnil, Qnil);
549 /* Passing args[i] directly stimulates compiler bug. */
550 teml = visargs[i];
551 args[i] = Fintern (teml, Qnil);
552 break;
554 case 'd': /* Value of point. Does not do I/O. */
555 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
556 args[i] = point_marker;
557 /* visargs[i] = Qnil; */
558 varies[i] = 1;
559 break;
561 case 'D': /* Directory name. */
562 args[i] = read_file_name (BVAR (current_buffer, directory), Qlambda, Qnil,
563 Qfile_directory_p);
564 break;
566 case 'f': /* Existing file name. */
567 args[i] = read_file_name (Qnil, Qlambda, Qnil, Qnil);
568 break;
570 case 'F': /* Possibly nonexistent file name. */
571 args[i] = read_file_name (Qnil, Qnil, Qnil, Qnil);
572 break;
574 case 'G': /* Possibly nonexistent file name,
575 default to directory alone. */
576 args[i] = read_file_name (Qnil, Qnil, empty_unibyte_string, Qnil);
577 break;
579 case 'i': /* Ignore an argument -- Does not do I/O. */
580 varies[i] = -1;
581 break;
583 case 'k': /* Key sequence. */
585 ptrdiff_t speccount1 = SPECPDL_INDEX ();
586 specbind (Qcursor_in_echo_area, Qt);
587 /* Prompt in `minibuffer-prompt' face. */
588 Fput_text_property (make_number (0),
589 make_number (SCHARS (callint_message)),
590 Qface, Qminibuffer_prompt, callint_message);
591 args[i] = Fread_key_sequence (callint_message,
592 Qnil, Qnil, Qnil, Qnil);
593 unbind_to (speccount1, Qnil);
594 teml = args[i];
595 visargs[i] = Fkey_description (teml, Qnil);
597 /* If the key sequence ends with a down-event,
598 discard the following up-event. */
599 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
600 if (CONSP (teml))
601 teml = XCAR (teml);
602 if (SYMBOLP (teml))
604 Lisp_Object tem2;
606 teml = Fget (teml, Qevent_symbol_elements);
607 /* Ignore first element, which is the base key. */
608 tem2 = Fmemq (Qdown, Fcdr (teml));
609 if (! NILP (tem2))
610 up_event = Fread_event (Qnil, Qnil, Qnil);
613 break;
615 case 'K': /* Key sequence to be defined. */
617 ptrdiff_t speccount1 = SPECPDL_INDEX ();
618 specbind (Qcursor_in_echo_area, Qt);
619 /* Prompt in `minibuffer-prompt' face. */
620 Fput_text_property (make_number (0),
621 make_number (SCHARS (callint_message)),
622 Qface, Qminibuffer_prompt, callint_message);
623 args[i] = Fread_key_sequence_vector (callint_message,
624 Qnil, Qt, Qnil, Qnil);
625 teml = args[i];
626 visargs[i] = Fkey_description (teml, Qnil);
627 unbind_to (speccount1, Qnil);
629 /* If the key sequence ends with a down-event,
630 discard the following up-event. */
631 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
632 if (CONSP (teml))
633 teml = XCAR (teml);
634 if (SYMBOLP (teml))
636 Lisp_Object tem2;
638 teml = Fget (teml, Qevent_symbol_elements);
639 /* Ignore first element, which is the base key. */
640 tem2 = Fmemq (Qdown, Fcdr (teml));
641 if (! NILP (tem2))
642 up_event = Fread_event (Qnil, Qnil, Qnil);
645 break;
647 case 'U': /* Up event from last k or K. */
648 if (!NILP (up_event))
650 args[i] = Fmake_vector (make_number (1), up_event);
651 up_event = Qnil;
652 teml = args[i];
653 visargs[i] = Fkey_description (teml, Qnil);
655 break;
657 case 'e': /* The invoking event. */
658 if (next_event >= key_count)
659 error ("%s must be bound to an event with parameters",
660 (SYMBOLP (function)
661 ? SSDATA (SYMBOL_NAME (function))
662 : "command"));
663 args[i] = AREF (keys, next_event);
664 next_event++;
665 varies[i] = -1;
667 /* Find the next parameterized event. */
668 while (next_event < key_count
669 && !(EVENT_HAS_PARAMETERS (AREF (keys, next_event))))
670 next_event++;
672 break;
674 case 'm': /* Value of mark. Does not do I/O. */
675 check_mark (0);
676 /* visargs[i] = Qnil; */
677 args[i] = BVAR (current_buffer, mark);
678 varies[i] = 2;
679 break;
681 case 'M': /* String read via minibuffer with
682 inheriting the current input method. */
683 args[i] = Fread_string (callint_message,
684 Qnil, Qnil, Qnil, Qt);
685 break;
687 case 'N': /* Prefix arg as number, else number from minibuffer. */
688 if (!NILP (prefix_arg))
689 goto have_prefix_arg;
690 FALLTHROUGH;
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 maybe_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;
839 specbind (Qcommand_debug_status, Qnil);
841 val = Ffuncall (nargs, args);
842 val = unbind_to (speccount, val);
843 SAFE_FREE ();
844 return val;
848 DEFUN ("prefix-numeric-value", Fprefix_numeric_value, Sprefix_numeric_value,
849 1, 1, 0,
850 doc: /* Return numeric meaning of raw prefix argument RAW.
851 A raw prefix argument is what you get from `(interactive "P")'.
852 Its numeric meaning is what you would get from `(interactive "p")'. */)
853 (Lisp_Object raw)
855 Lisp_Object val;
857 if (NILP (raw))
858 XSETFASTINT (val, 1);
859 else if (EQ (raw, Qminus))
860 XSETINT (val, -1);
861 else if (CONSP (raw) && INTEGERP (XCAR (raw)))
862 XSETINT (val, XINT (XCAR (raw)));
863 else if (INTEGERP (raw))
864 val = raw;
865 else
866 XSETFASTINT (val, 1);
868 return val;
871 void
872 syms_of_callint (void)
874 point_marker = Fmake_marker ();
875 staticpro (&point_marker);
877 callint_message = Qnil;
878 staticpro (&callint_message);
880 preserved_fns = listn (CONSTYPE_PURE, 4,
881 intern_c_string ("region-beginning"),
882 intern_c_string ("region-end"),
883 intern_c_string ("point"),
884 intern_c_string ("mark"));
886 DEFSYM (Qlist, "list");
887 DEFSYM (Qlet, "let");
888 DEFSYM (Qif, "if");
889 DEFSYM (Qwhen, "when");
890 DEFSYM (Qletx, "let*");
891 DEFSYM (Qsave_excursion, "save-excursion");
892 DEFSYM (Qprogn, "progn");
893 DEFSYM (Qminus, "-");
894 DEFSYM (Qplus, "+");
895 DEFSYM (Qhandle_shift_selection, "handle-shift-selection");
896 DEFSYM (Qread_number, "read-number");
897 DEFSYM (Qfuncall_interactively, "funcall-interactively");
898 DEFSYM (Qcommand_debug_status, "command-debug-status");
899 DEFSYM (Qenable_recursive_minibuffers, "enable-recursive-minibuffers");
900 DEFSYM (Qmouse_leave_buffer_hook, "mouse-leave-buffer-hook");
902 DEFVAR_KBOARD ("prefix-arg", Vprefix_arg,
903 doc: /* The value of the prefix argument for the next editing command.
904 It may be a number, or the symbol `-' for just a minus sign as arg,
905 or a list whose car is a number for just one or more C-u's
906 or nil if no argument has been specified.
908 You cannot examine this variable to find the argument for this command
909 since it has been set to nil by the time you can look.
910 Instead, you should use the variable `current-prefix-arg', although
911 normally commands can get this prefix argument with (interactive "P"). */);
913 DEFVAR_KBOARD ("last-prefix-arg", Vlast_prefix_arg,
914 doc: /* The value of the prefix argument for the previous editing command.
915 See `prefix-arg' for the meaning of the value. */);
917 DEFVAR_LISP ("current-prefix-arg", Vcurrent_prefix_arg,
918 doc: /* The value of the prefix argument for this editing command.
919 It may be a number, or the symbol `-' for just a minus sign as arg,
920 or a list whose car is a number for just one or more C-u's
921 or nil if no argument has been specified.
922 This is what `(interactive \"P\")' returns. */);
923 Vcurrent_prefix_arg = Qnil;
925 DEFVAR_LISP ("command-history", Vcommand_history,
926 doc: /* List of recent commands that read arguments from terminal.
927 Each command is represented as a form to evaluate.
929 Maximum length of the history list is determined by the value
930 of `history-length', which see. */);
931 Vcommand_history = Qnil;
933 DEFVAR_LISP ("command-debug-status", Vcommand_debug_status,
934 doc: /* Debugging status of current interactive command.
935 Bound each time `call-interactively' is called;
936 may be set by the debugger as a reminder for itself. */);
937 Vcommand_debug_status = Qnil;
939 DEFVAR_LISP ("mark-even-if-inactive", Vmark_even_if_inactive,
940 doc: /* Non-nil means you can use the mark even when inactive.
941 This option makes a difference in Transient Mark mode.
942 When the option is non-nil, deactivation of the mark
943 turns off region highlighting, but commands that use the mark
944 behave as if the mark were still active. */);
945 Vmark_even_if_inactive = Qt;
947 DEFVAR_LISP ("mouse-leave-buffer-hook", Vmouse_leave_buffer_hook,
948 doc: /* Hook to run when about to switch windows with a mouse command.
949 Its purpose is to give temporary modes such as Isearch mode
950 a way to turn themselves off when a mouse command switches windows. */);
951 Vmouse_leave_buffer_hook = Qnil;
953 defsubr (&Sinteractive);
954 defsubr (&Scall_interactively);
955 defsubr (&Sfuncall_interactively);
956 defsubr (&Sprefix_numeric_value);