* lisp/url/url-handlers.el: No need for subr-x at run-time.
[emacs.git] / src / callint.c
blob08a8bba464628eebba75679c754fd3155eca7a10
1 /* Call a Lisp function interactively.
2 Copyright (C) 1985-1986, 1993-1995, 1997, 2000-2018 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 "ptr-bounds.h"
25 #include "character.h"
26 #include "buffer.h"
27 #include "keyboard.h"
28 #include "window.h"
30 static Lisp_Object preserved_fns;
32 /* Marker used within call-interactively to refer to point. */
33 static Lisp_Object point_marker;
35 /* String for the prompt text used in Fcall_interactively. */
36 static Lisp_Object callint_message;
38 /* ARGSUSED */
39 DEFUN ("interactive", Finteractive, Sinteractive, 0, UNEVALLED, 0,
40 doc: /* Specify a way of parsing arguments for interactive use of a function.
41 For example, write
42 (defun foo (arg buf) "Doc string" (interactive "P\\nbbuffer: ") .... )
43 to make ARG be the raw prefix argument, and set BUF to an existing buffer,
44 when `foo' is called as a command.
46 The "call" to `interactive' is actually a declaration rather than a
47 function; it tells `call-interactively' how to read arguments to pass
48 to the function. When actually called, `interactive' just returns
49 nil.
51 Usually the argument of `interactive' is a string containing a code
52 letter followed optionally by a prompt. (Some code letters do not
53 use I/O to get the argument and do not use prompts.) To pass several
54 arguments to the command, concatenate the individual strings,
55 separating them by newline characters.
57 Prompts are passed to `format', and may use % escapes to print the
58 arguments that have already been read.
59 If the argument is not a string, it is evaluated to get a list of
60 arguments to pass to the command.
61 Just `(interactive)' means pass no arguments to the command when
62 calling interactively.
64 Code letters available are:
65 a -- Function name: symbol with a function definition.
66 b -- Name of existing buffer.
67 B -- Name of buffer, possibly nonexistent.
68 c -- Character (no input method is used).
69 C -- Command name: symbol with interactive function definition.
70 d -- Value of point as number. Does not do I/O.
71 D -- Directory name.
72 e -- Parameterized event (i.e., one that's a list) that invoked this command.
73 If used more than once, the Nth `e' returns the Nth parameterized event.
74 This skips events that are integers or symbols.
75 f -- Existing file name.
76 F -- Possibly nonexistent file name.
77 G -- Possibly nonexistent file name, defaulting to just directory name.
78 i -- Ignored, i.e. always nil. Does not do I/O.
79 k -- Key sequence (downcase the last event if needed to get a definition).
80 K -- Key sequence to be redefined (do not downcase the last event).
81 m -- Value of mark as number. Does not do I/O.
82 M -- Any string. Inherits the current input method.
83 n -- Number read using minibuffer.
84 N -- Numeric prefix arg, or if none, do like code `n'.
85 p -- Prefix arg converted to number. Does not do I/O.
86 P -- Prefix arg in raw form. Does not do I/O.
87 r -- Region: point and mark as 2 numeric args, smallest first. Does no I/O.
88 s -- Any string. Does not inherit the current input method.
89 S -- Any symbol.
90 U -- Mouse up event discarded by a previous k or K argument.
91 v -- Variable name: symbol that is `custom-variable-p'.
92 x -- Lisp expression read but not evaluated.
93 X -- Lisp expression read and evaluated.
94 z -- Coding system.
95 Z -- Coding system, nil if no prefix arg.
97 In addition, if the string begins with `*', an error is signaled if
98 the buffer is read-only.
99 If `@' appears at the beginning of the string, and if the key sequence
100 used to invoke the command includes any mouse events, then the window
101 associated with the first of those events is selected before the
102 command is run.
103 If the string begins with `^' and `shift-select-mode' is non-nil,
104 Emacs first calls the function `handle-shift-selection'.
105 You may use `@', `*', and `^' together. They are processed in the
106 order that they appear, before reading any arguments.
107 usage: (interactive &optional ARG-DESCRIPTOR) */
108 attributes: const)
109 (Lisp_Object args)
111 return Qnil;
114 /* Quotify EXP: if EXP is constant, return it.
115 If EXP is not constant, return (quote EXP). */
116 static Lisp_Object
117 quotify_arg (register Lisp_Object exp)
119 if (CONSP (exp)
120 || (SYMBOLP (exp)
121 && !NILP (exp) && !EQ (exp, Qt)))
122 return list2 (Qquote, exp);
124 return exp;
127 /* Modify EXP by quotifying each element (except the first). */
128 static Lisp_Object
129 quotify_args (Lisp_Object exp)
131 register Lisp_Object tail;
132 Lisp_Object next;
133 for (tail = exp; CONSP (tail); tail = next)
135 next = XCDR (tail);
136 XSETCAR (tail, quotify_arg (XCAR (tail)));
138 return exp;
141 static const char *callint_argfuns[]
142 = {"", "point", "mark", "region-beginning", "region-end"};
144 static void
145 check_mark (bool for_region)
147 Lisp_Object tem;
148 tem = Fmarker_buffer (BVAR (current_buffer, mark));
149 if (NILP (tem) || (XBUFFER (tem) != current_buffer))
150 error (for_region ? "The mark is not set now, so there is no region"
151 : "The mark is not set now");
152 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
153 && NILP (BVAR (current_buffer, mark_active)))
154 xsignal0 (Qmark_inactive);
157 /* If the list of args INPUT was produced with an explicit call to
158 `list', look for elements that were computed with
159 (region-beginning) or (region-end), and put those expressions into
160 VALUES instead of the present values.
162 This function doesn't return a value because it modifies elements
163 of VALUES to do its job. */
165 static void
166 fix_command (Lisp_Object input, Lisp_Object values)
168 /* FIXME: Instead of this ugly hack, we should provide a way for an
169 interactive spec to return an expression/function that will re-build the
170 args without user intervention. */
171 if (CONSP (input))
173 Lisp_Object car;
175 car = XCAR (input);
176 /* Skip through certain special forms. */
177 while (EQ (car, Qlet) || EQ (car, Qletx)
178 || EQ (car, Qsave_excursion)
179 || EQ (car, Qprogn))
181 while (CONSP (XCDR (input)))
182 input = XCDR (input);
183 input = XCAR (input);
184 if (!CONSP (input))
185 break;
186 car = XCAR (input);
188 if (EQ (car, Qlist))
190 Lisp_Object intail, valtail;
191 for (intail = Fcdr (input), valtail = values;
192 CONSP (valtail);
193 intail = Fcdr (intail), valtail = XCDR (valtail))
195 Lisp_Object elt;
196 elt = Fcar (intail);
197 if (CONSP (elt))
199 Lisp_Object presflag, carelt;
200 carelt = XCAR (elt);
201 /* If it is (if X Y), look at Y. */
202 if (EQ (carelt, Qif)
203 && EQ (Fnthcdr (make_number (3), elt), Qnil))
204 elt = Fnth (make_number (2), elt);
205 /* If it is (when ... Y), look at Y. */
206 else if (EQ (carelt, Qwhen))
208 while (CONSP (XCDR (elt)))
209 elt = XCDR (elt);
210 elt = Fcar (elt);
213 /* If the function call we're looking at
214 is a special preserved one, copy the
215 whole expression for this argument. */
216 if (CONSP (elt))
218 presflag = Fmemq (Fcar (elt), preserved_fns);
219 if (!NILP (presflag))
220 Fsetcar (valtail, Fcar (intail));
228 /* Helper function to call `read-file-name' from C. */
230 static Lisp_Object
231 read_file_name (Lisp_Object default_filename, Lisp_Object mustmatch,
232 Lisp_Object initial, Lisp_Object predicate)
234 return CALLN (Ffuncall, intern ("read-file-name"),
235 callint_message, Qnil, default_filename,
236 mustmatch, initial, predicate);
239 /* BEWARE: Calling this directly from C would defeat the purpose! */
240 DEFUN ("funcall-interactively", Ffuncall_interactively, Sfuncall_interactively,
241 1, MANY, 0, doc: /* Like `funcall' but marks the call as interactive.
242 I.e. arrange that within the called function `called-interactively-p' will
243 return non-nil.
244 usage: (funcall-interactively FUNCTION &rest ARGUMENTS) */)
245 (ptrdiff_t nargs, Lisp_Object *args)
247 ptrdiff_t speccount = SPECPDL_INDEX ();
248 temporarily_switch_to_single_kboard (NULL);
250 /* Nothing special to do here, all the work is inside
251 `called-interactively-p'. Which will look for us as a marker in the
252 backtrace. */
253 return unbind_to (speccount, Ffuncall (nargs, args));
256 DEFUN ("call-interactively", Fcall_interactively, Scall_interactively, 1, 3, 0,
257 doc: /* Call FUNCTION, providing args according to its interactive calling specs.
258 Return the value FUNCTION returns.
259 The function contains a specification of how to do the argument reading.
260 In the case of user-defined functions, this is specified by placing a call
261 to the function `interactive' at the top level of the function body.
262 See `interactive'.
264 Optional second arg RECORD-FLAG non-nil
265 means unconditionally put this command in the command-history.
266 Otherwise, this is done only if an arg is read using the minibuffer.
268 Optional third arg KEYS, if given, specifies the sequence of events to
269 supply, as a vector, if the command inquires which events were used to
270 invoke it. If KEYS is omitted or nil, the return value of
271 `this-command-keys-vector' is used. */)
272 (Lisp_Object function, Lisp_Object record_flag, Lisp_Object keys)
274 ptrdiff_t speccount = SPECPDL_INDEX ();
276 bool arg_from_tty = false;
277 ptrdiff_t key_count;
278 bool record_then_fail = false;
280 Lisp_Object save_this_command = Vthis_command;
281 Lisp_Object save_this_original_command = Vthis_original_command;
282 Lisp_Object save_real_this_command = Vreal_this_command;
283 Lisp_Object save_last_command = KVAR (current_kboard, Vlast_command);
285 if (NILP (keys))
286 keys = this_command_keys, key_count = this_command_key_count;
287 else
289 CHECK_VECTOR (keys);
290 key_count = ASIZE (keys);
293 /* Save this now, since use of minibuffer will clobber it. */
294 Lisp_Object prefix_arg = Vcurrent_prefix_arg;
296 Lisp_Object enable = (SYMBOLP (function)
297 ? Fget (function, Qenable_recursive_minibuffers)
298 : Qnil);
300 /* If k or K discard an up-event, save it here so it can be retrieved with
301 U. */
302 Lisp_Object up_event = Qnil;
304 /* Set SPECS to the interactive form, or barf if not interactive. */
305 Lisp_Object form = Finteractive_form (function);
306 if (! CONSP (form))
307 wrong_type_argument (Qcommandp, function);
308 Lisp_Object specs = Fcar (XCDR (form));
310 /* At this point the value of SPECS could help provide a way to
311 specify how to represent the arguments in command history.
312 The feature is not fully implemented. */
314 /* If SPECS is not a string, invent one. */
315 if (! STRINGP (specs))
317 Lisp_Object funval = Findirect_function (function, Qt);
318 uintmax_t events = num_input_events;
319 Lisp_Object input = specs;
320 /* Compute the arg values using the user's expression. */
321 specs = Feval (specs,
322 CONSP (funval) && EQ (Qclosure, XCAR (funval))
323 ? CAR_SAFE (XCDR (funval)) : Qnil);
324 if (events != num_input_events || !NILP (record_flag))
326 /* We should record this command on the command history.
327 Make a copy of the list of values, for the command history,
328 and turn them into things we can eval. */
329 Lisp_Object values = quotify_args (Fcopy_sequence (specs));
330 fix_command (input, values);
331 Lisp_Object this_cmd = Fcons (function, values);
332 if (history_delete_duplicates)
333 Vcommand_history = Fdelete (this_cmd, Vcommand_history);
334 Vcommand_history = Fcons (this_cmd, Vcommand_history);
336 /* Don't keep command history around forever. */
337 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
339 Lisp_Object teml = Fnthcdr (Vhistory_length, Vcommand_history);
340 if (CONSP (teml))
341 XSETCDR (teml, Qnil);
345 Vthis_command = save_this_command;
346 Vthis_original_command = save_this_original_command;
347 Vreal_this_command = save_real_this_command;
348 kset_last_command (current_kboard, save_last_command);
350 return unbind_to (speccount, CALLN (Fapply, Qfuncall_interactively,
351 function, specs));
354 /* SPECS is set to a string; use it as an interactive prompt.
355 Copy it so that STRING will be valid even if a GC relocates SPECS. */
356 USE_SAFE_ALLOCA;
357 ptrdiff_t string_len = SBYTES (specs);
358 char *string = SAFE_ALLOCA (string_len + 1);
359 memcpy (string, SDATA (specs), string_len + 1);
360 char *string_end = string + string_len;
362 /* The index of the next element of this_command_keys to examine for
363 the 'e' interactive code. Initialize it to point to the first
364 event with parameters. */
365 ptrdiff_t next_event;
366 for (next_event = 0; next_event < key_count; next_event++)
367 if (EVENT_HAS_PARAMETERS (AREF (keys, next_event)))
368 break;
370 /* Handle special starting chars `*' and `@'. Also `-'. */
371 /* Note that `+' is reserved for user extensions. */
372 for (;; string++)
374 if (*string == '+')
375 error ("`+' is not used in `interactive' for ordinary commands");
376 else if (*string == '*')
378 if (!NILP (BVAR (current_buffer, read_only)))
380 if (!NILP (record_flag))
382 for (char *p = string + 1; p < string_end; p++)
383 if (! (*p == 'r' || *p == 'p' || *p == 'P' || *p == '\n'))
384 Fbarf_if_buffer_read_only (Qnil);
385 record_then_fail = true;
387 else
388 Fbarf_if_buffer_read_only (Qnil);
391 /* Ignore this for semi-compatibility with Lucid. */
392 else if (*string == '-')
394 else if (*string == '@')
396 Lisp_Object w, event = (next_event < key_count
397 ? AREF (keys, next_event)
398 : Qnil);
399 if (EVENT_HAS_PARAMETERS (event)
400 && (w = XCDR (event), CONSP (w))
401 && (w = XCAR (w), CONSP (w))
402 && (w = XCAR (w), WINDOWP (w)))
404 if (MINI_WINDOW_P (XWINDOW (w))
405 && ! (minibuf_level > 0 && EQ (w, minibuf_window)))
406 error ("Attempt to select inactive minibuffer window");
408 /* If the current buffer wants to clean up, let it. */
409 run_hook (Qmouse_leave_buffer_hook);
411 Fselect_window (w, Qnil);
414 else if (*string == '^')
415 call0 (Qhandle_shift_selection);
416 else break;
419 /* Count the number of arguments, which is two (the function itself and
420 `funcall-interactively') plus the number of arguments the interactive spec
421 would have us give to the function. */
422 ptrdiff_t nargs = 2;
423 for (char const *tem = string; tem < string_end; tem++)
425 /* 'r' specifications ("point and mark as 2 numeric args")
426 produce *two* arguments. */
427 nargs += 1 + (*tem == 'r');
428 tem = memchr (tem, '\n', string_len - (tem - string));
429 if (!tem)
430 break;
433 if (MOST_POSITIVE_FIXNUM < min (PTRDIFF_MAX, SIZE_MAX) / word_size
434 && MOST_POSITIVE_FIXNUM < nargs)
435 memory_full (SIZE_MAX);
437 /* ARGS will contain the array of arguments to pass to the function.
438 VISARGS will contain the same list but in a nicer form, so that if we
439 pass it to Fformat_message it will be understandable to a human.
440 Allocate them all at one go. This wastes a bit of memory, but
441 it's OK to trade space for speed. */
442 Lisp_Object *args;
443 SAFE_NALLOCA (args, 3, nargs);
444 Lisp_Object *visargs = args + nargs;
445 /* If varies[I] > 0, the Ith argument shouldn't just have its value
446 in this call quoted in the command history. It should be
447 recorded as a call to the function named callint_argfuns[varies[I]]. */
448 signed char *varies = (signed char *) (visargs + nargs);
450 memclear (args, nargs * (2 * word_size + 1));
451 args = ptr_bounds_clip (args, nargs * sizeof *args);
452 visargs = ptr_bounds_clip (visargs, nargs * sizeof *visargs);
453 varies = ptr_bounds_clip (varies, nargs * sizeof *varies);
455 if (!NILP (enable))
456 specbind (Qenable_recursive_minibuffers, Qt);
458 char const *tem = string;
459 for (ptrdiff_t i = 2; tem < string_end; i++)
461 char *pnl = memchr (tem + 1, '\n', string_len - (tem + 1 - string));
462 ptrdiff_t sz = pnl ? pnl - (tem + 1) : string_end - (tem + 1);
464 visargs[1] = make_string (tem + 1, sz);
465 callint_message = Fformat_message (i - 1, visargs + 1);
467 switch (*tem)
469 case 'a': /* Symbol defined as a function. */
470 visargs[i] = Fcompleting_read (callint_message,
471 Vobarray, Qfboundp, Qt,
472 Qnil, Qnil, Qnil, Qnil);
473 args[i] = Fintern (visargs[i], Qnil);
474 break;
476 case 'b': /* Name of existing buffer. */
477 args[i] = Fcurrent_buffer ();
478 if (EQ (selected_window, minibuf_window))
479 args[i] = Fother_buffer (args[i], Qnil, Qnil);
480 args[i] = Fread_buffer (callint_message, args[i], Qt, Qnil);
481 break;
483 case 'B': /* Name of buffer, possibly nonexistent. */
484 args[i] = Fread_buffer (callint_message,
485 Fother_buffer (Fcurrent_buffer (),
486 Qnil, Qnil),
487 Qnil, Qnil);
488 break;
490 case 'c': /* Character. */
491 /* Prompt in `minibuffer-prompt' face. */
492 Fput_text_property (make_number (0),
493 make_number (SCHARS (callint_message)),
494 Qface, Qminibuffer_prompt, callint_message);
495 args[i] = Fread_char (callint_message, Qnil, Qnil);
496 message1_nolog (0);
497 /* See bug#8479. */
498 if (! CHARACTERP (args[i]))
499 error ("Non-character input-event");
500 visargs[i] = Fchar_to_string (args[i]);
501 break;
503 case 'C': /* Command: symbol with interactive function. */
504 visargs[i] = Fcompleting_read (callint_message,
505 Vobarray, Qcommandp,
506 Qt, Qnil, Qnil, Qnil, Qnil);
507 args[i] = Fintern (visargs[i], Qnil);
508 break;
510 case 'd': /* Value of point. Does not do I/O. */
511 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
512 args[i] = point_marker;
513 /* visargs[i] = Qnil; */
514 varies[i] = 1;
515 break;
517 case 'D': /* Directory name. */
518 args[i] = read_file_name (BVAR (current_buffer, directory), Qlambda,
519 Qnil, Qfile_directory_p);
520 break;
522 case 'f': /* Existing file name. */
523 args[i] = read_file_name (Qnil, Qlambda, Qnil, Qnil);
524 break;
526 case 'F': /* Possibly nonexistent file name. */
527 args[i] = read_file_name (Qnil, Qnil, Qnil, Qnil);
528 break;
530 case 'G': /* Possibly nonexistent file name,
531 default to directory alone. */
532 args[i] = read_file_name (Qnil, Qnil, empty_unibyte_string, Qnil);
533 break;
535 case 'i': /* Ignore an argument -- Does not do I/O. */
536 varies[i] = -1;
537 break;
539 case 'k': /* Key sequence. */
541 ptrdiff_t speccount1 = SPECPDL_INDEX ();
542 specbind (Qcursor_in_echo_area, Qt);
543 /* Prompt in `minibuffer-prompt' face. */
544 Fput_text_property (make_number (0),
545 make_number (SCHARS (callint_message)),
546 Qface, Qminibuffer_prompt, callint_message);
547 args[i] = Fread_key_sequence (callint_message,
548 Qnil, Qnil, Qnil, Qnil);
549 unbind_to (speccount1, Qnil);
550 visargs[i] = Fkey_description (args[i], Qnil);
552 /* If the key sequence ends with a down-event,
553 discard the following up-event. */
554 Lisp_Object teml
555 = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
556 if (CONSP (teml))
557 teml = XCAR (teml);
558 if (SYMBOLP (teml))
560 teml = Fget (teml, Qevent_symbol_elements);
561 /* Ignore first element, which is the base key. */
562 Lisp_Object tem2 = Fmemq (Qdown, Fcdr (teml));
563 if (! NILP (tem2))
564 up_event = Fread_event (Qnil, Qnil, Qnil);
567 break;
569 case 'K': /* Key sequence to be defined. */
571 ptrdiff_t speccount1 = SPECPDL_INDEX ();
572 specbind (Qcursor_in_echo_area, Qt);
573 /* Prompt in `minibuffer-prompt' face. */
574 Fput_text_property (make_number (0),
575 make_number (SCHARS (callint_message)),
576 Qface, Qminibuffer_prompt, callint_message);
577 args[i] = Fread_key_sequence_vector (callint_message,
578 Qnil, Qt, Qnil, Qnil);
579 visargs[i] = Fkey_description (args[i], Qnil);
580 unbind_to (speccount1, Qnil);
582 /* If the key sequence ends with a down-event,
583 discard the following up-event. */
584 Lisp_Object teml
585 = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
586 if (CONSP (teml))
587 teml = XCAR (teml);
588 if (SYMBOLP (teml))
590 teml = Fget (teml, Qevent_symbol_elements);
591 /* Ignore first element, which is the base key. */
592 Lisp_Object tem2 = Fmemq (Qdown, Fcdr (teml));
593 if (! NILP (tem2))
594 up_event = Fread_event (Qnil, Qnil, Qnil);
597 break;
599 case 'U': /* Up event from last k or K. */
600 if (!NILP (up_event))
602 args[i] = Fmake_vector (make_number (1), up_event);
603 up_event = Qnil;
604 visargs[i] = Fkey_description (args[i], Qnil);
606 break;
608 case 'e': /* The invoking event. */
609 if (next_event >= key_count)
610 error ("%s must be bound to an event with parameters",
611 (SYMBOLP (function)
612 ? SSDATA (SYMBOL_NAME (function))
613 : "command"));
614 args[i] = AREF (keys, next_event);
615 varies[i] = -1;
617 /* Find the next parameterized event. */
619 next_event++;
620 while (next_event < key_count
621 && ! EVENT_HAS_PARAMETERS (AREF (keys, next_event)));
623 break;
625 case 'm': /* Value of mark. Does not do I/O. */
626 check_mark (false);
627 /* visargs[i] = Qnil; */
628 args[i] = BVAR (current_buffer, mark);
629 varies[i] = 2;
630 break;
632 case 'M': /* String read via minibuffer with
633 inheriting the current input method. */
634 args[i] = Fread_string (callint_message,
635 Qnil, Qnil, Qnil, Qt);
636 break;
638 case 'N': /* Prefix arg as number, else number from minibuffer. */
639 if (!NILP (prefix_arg))
640 goto have_prefix_arg;
641 FALLTHROUGH;
642 case 'n': /* Read number from minibuffer. */
643 args[i] = call1 (Qread_number, callint_message);
644 visargs[i] = Fnumber_to_string (args[i]);
645 break;
647 case 'P': /* Prefix arg in raw form. Does no I/O. */
648 args[i] = prefix_arg;
649 /* visargs[i] = Qnil; */
650 varies[i] = -1;
651 break;
653 case 'p': /* Prefix arg converted to number. No I/O. */
654 have_prefix_arg:
655 args[i] = Fprefix_numeric_value (prefix_arg);
656 /* visargs[i] = Qnil; */
657 varies[i] = -1;
658 break;
660 case 'r': /* Region, point and mark as 2 args. */
662 check_mark (true);
663 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
664 ptrdiff_t mark = marker_position (BVAR (current_buffer, mark));
665 /* visargs[i] = visargs[i + 1] = Qnil; */
666 args[i] = PT < mark ? point_marker : BVAR (current_buffer, mark);
667 varies[i] = 3;
668 args[++i] = PT > mark ? point_marker : BVAR (current_buffer, mark);
669 varies[i] = 4;
671 break;
673 case 's': /* String read via minibuffer without
674 inheriting the current input method. */
675 args[i] = Fread_string (callint_message,
676 Qnil, Qnil, Qnil, Qnil);
677 break;
679 case 'S': /* Any symbol. */
680 visargs[i] = Fread_string (callint_message,
681 Qnil, Qnil, Qnil, Qnil);
682 args[i] = Fintern (visargs[i], Qnil);
683 break;
685 case 'v': /* Variable name: symbol that is
686 custom-variable-p. */
687 args[i] = Fread_variable (callint_message, Qnil);
688 visargs[i] = last_minibuf_string;
689 break;
691 case 'x': /* Lisp expression read but not evaluated. */
692 args[i] = call1 (intern ("read-minibuffer"), callint_message);
693 visargs[i] = last_minibuf_string;
694 break;
696 case 'X': /* Lisp expression read and evaluated. */
697 args[i] = call1 (intern ("eval-minibuffer"), callint_message);
698 visargs[i] = last_minibuf_string;
699 break;
701 case 'Z': /* Coding-system symbol, or ignore the
702 argument if no prefix. */
703 if (NILP (prefix_arg))
705 /* args[i] = Qnil; */
706 varies[i] = -1;
708 else
710 args[i]
711 = Fread_non_nil_coding_system (callint_message);
712 visargs[i] = last_minibuf_string;
714 break;
716 case 'z': /* Coding-system symbol or nil. */
717 args[i] = Fread_coding_system (callint_message, Qnil);
718 visargs[i] = last_minibuf_string;
719 break;
721 /* We have a case for `+' so we get an error
722 if anyone tries to define one here. */
723 case '+':
724 default:
726 /* How many bytes are left unprocessed in the specs string?
727 (Note that this excludes the trailing null byte.) */
728 ptrdiff_t bytes_left = string_len - (tem - string);
729 unsigned letter;
731 /* If we have enough bytes left to treat the sequence as a
732 character, show that character's codepoint; otherwise
733 show only its first byte. */
734 if (bytes_left >= BYTES_BY_CHAR_HEAD (*((unsigned char *) tem)))
735 letter = STRING_CHAR ((unsigned char *) tem);
736 else
737 letter = *((unsigned char *) tem);
739 error (("Invalid control letter `%c' (#o%03o, #x%04x)"
740 " in interactive calling string"),
741 (int) letter, letter, letter);
745 if (varies[i] == 0)
746 arg_from_tty = true;
748 if (NILP (visargs[i]) && STRINGP (args[i]))
749 visargs[i] = args[i];
751 tem = memchr (tem, '\n', string_len - (tem - string));
752 if (tem) tem++;
753 else tem = string_end;
755 unbind_to (speccount, Qnil);
757 maybe_quit ();
759 args[0] = Qfuncall_interactively;
760 args[1] = function;
762 if (arg_from_tty || !NILP (record_flag))
764 /* We don't need `visargs' any more, so let's recycle it since we need
765 an array of just the same size. */
766 visargs[1] = function;
767 for (ptrdiff_t i = 2; i < nargs; i++)
768 visargs[i] = (varies[i] > 0
769 ? list1 (intern (callint_argfuns[varies[i]]))
770 : quotify_arg (args[i]));
771 Vcommand_history = Fcons (Flist (nargs - 1, visargs + 1),
772 Vcommand_history);
773 /* Don't keep command history around forever. */
774 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
776 Lisp_Object teml = Fnthcdr (Vhistory_length, Vcommand_history);
777 if (CONSP (teml))
778 XSETCDR (teml, Qnil);
782 /* If we used a marker to hold point, mark, or an end of the region,
783 temporarily, convert it to an integer now. */
784 for (ptrdiff_t i = 2; i < nargs; i++)
785 if (varies[i] >= 1 && varies[i] <= 4)
786 XSETINT (args[i], marker_position (args[i]));
788 if (record_then_fail)
789 Fbarf_if_buffer_read_only (Qnil);
791 Vthis_command = save_this_command;
792 Vthis_original_command = save_this_original_command;
793 Vreal_this_command = save_real_this_command;
794 kset_last_command (current_kboard, save_last_command);
796 specbind (Qcommand_debug_status, Qnil);
798 Lisp_Object val = Ffuncall (nargs, args);
799 SAFE_FREE ();
800 return unbind_to (speccount, val);
803 DEFUN ("prefix-numeric-value", Fprefix_numeric_value, Sprefix_numeric_value,
804 1, 1, 0,
805 doc: /* Return numeric meaning of raw prefix argument RAW.
806 A raw prefix argument is what you get from `(interactive "P")'.
807 Its numeric meaning is what you would get from `(interactive "p")'. */)
808 (Lisp_Object raw)
810 Lisp_Object val;
812 if (NILP (raw))
813 XSETFASTINT (val, 1);
814 else if (EQ (raw, Qminus))
815 XSETINT (val, -1);
816 else if (CONSP (raw) && INTEGERP (XCAR (raw)))
817 XSETINT (val, XINT (XCAR (raw)));
818 else if (INTEGERP (raw))
819 val = raw;
820 else
821 XSETFASTINT (val, 1);
823 return val;
826 void
827 syms_of_callint (void)
829 point_marker = Fmake_marker ();
830 staticpro (&point_marker);
832 callint_message = Qnil;
833 staticpro (&callint_message);
835 preserved_fns = listn (CONSTYPE_PURE, 4,
836 intern_c_string ("region-beginning"),
837 intern_c_string ("region-end"),
838 intern_c_string ("point"),
839 intern_c_string ("mark"));
841 DEFSYM (Qlist, "list");
842 DEFSYM (Qlet, "let");
843 DEFSYM (Qif, "if");
844 DEFSYM (Qwhen, "when");
845 DEFSYM (Qletx, "let*");
846 DEFSYM (Qsave_excursion, "save-excursion");
847 DEFSYM (Qprogn, "progn");
848 DEFSYM (Qminus, "-");
849 DEFSYM (Qplus, "+");
850 DEFSYM (Qhandle_shift_selection, "handle-shift-selection");
851 DEFSYM (Qread_number, "read-number");
852 DEFSYM (Qfuncall_interactively, "funcall-interactively");
853 DEFSYM (Qcommand_debug_status, "command-debug-status");
854 DEFSYM (Qenable_recursive_minibuffers, "enable-recursive-minibuffers");
855 DEFSYM (Qmouse_leave_buffer_hook, "mouse-leave-buffer-hook");
857 DEFVAR_KBOARD ("prefix-arg", Vprefix_arg,
858 doc: /* The value of the prefix argument for the next editing command.
859 It may be a number, or the symbol `-' for just a minus sign as arg,
860 or a list whose car is a number for just one or more C-u's
861 or nil if no argument has been specified.
863 You cannot examine this variable to find the argument for this command
864 since it has been set to nil by the time you can look.
865 Instead, you should use the variable `current-prefix-arg', although
866 normally commands can get this prefix argument with (interactive "P"). */);
868 DEFVAR_KBOARD ("last-prefix-arg", Vlast_prefix_arg,
869 doc: /* The value of the prefix argument for the previous editing command.
870 See `prefix-arg' for the meaning of the value. */);
872 DEFVAR_LISP ("current-prefix-arg", Vcurrent_prefix_arg,
873 doc: /* The value of the prefix argument for this editing command.
874 It may be a number, or the symbol `-' for just a minus sign as arg,
875 or a list whose car is a number for just one or more C-u's
876 or nil if no argument has been specified.
877 This is what `(interactive \"P\")' returns. */);
878 Vcurrent_prefix_arg = Qnil;
880 DEFVAR_LISP ("command-history", Vcommand_history,
881 doc: /* List of recent commands that read arguments from terminal.
882 Each command is represented as a form to evaluate.
884 Maximum length of the history list is determined by the value
885 of `history-length', which see. */);
886 Vcommand_history = Qnil;
888 DEFVAR_LISP ("command-debug-status", Vcommand_debug_status,
889 doc: /* Debugging status of current interactive command.
890 Bound each time `call-interactively' is called;
891 may be set by the debugger as a reminder for itself. */);
892 Vcommand_debug_status = Qnil;
894 DEFVAR_LISP ("mark-even-if-inactive", Vmark_even_if_inactive,
895 doc: /* Non-nil means you can use the mark even when inactive.
896 This option makes a difference in Transient Mark mode.
897 When the option is non-nil, deactivation of the mark
898 turns off region highlighting, but commands that use the mark
899 behave as if the mark were still active. */);
900 Vmark_even_if_inactive = Qt;
902 DEFVAR_LISP ("mouse-leave-buffer-hook", Vmouse_leave_buffer_hook,
903 doc: /* Hook to run when about to switch windows with a mouse command.
904 Its purpose is to give temporary modes such as Isearch mode
905 a way to turn themselves off when a mouse command switches windows. */);
906 Vmouse_leave_buffer_hook = Qnil;
908 defsubr (&Sinteractive);
909 defsubr (&Scall_interactively);
910 defsubr (&Sfuncall_interactively);
911 defsubr (&Sprefix_numeric_value);