Merge from origin/emacs-24
[emacs.git] / src / callint.c
blob0c6c03036c8167306b47a8e4ac24276e717a61af
1 /* Call a Lisp function interactively.
2 Copyright (C) 1985-1986, 1993-1995, 1997, 2000-2015 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include "lisp.h"
24 #include "character.h"
25 #include "buffer.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "window.h"
29 #include "keymap.h"
31 static Lisp_Object preserved_fns;
33 /* Marker used within call-interactively to refer to point. */
34 static Lisp_Object point_marker;
36 /* String for the prompt text used in Fcall_interactively. */
37 static Lisp_Object callint_message;
39 /* ARGSUSED */
40 DEFUN ("interactive", Finteractive, Sinteractive, 0, UNEVALLED, 0,
41 doc: /* Specify a way of parsing arguments for interactive use of a function.
42 For example, write
43 (defun foo (arg buf) "Doc string" (interactive "P\\nbbuffer: ") .... )
44 to make ARG be the raw prefix argument, and set BUF to an existing buffer,
45 when `foo' is called as a command.
46 The "call" to `interactive' is actually a declaration rather than a function;
47 it tells `call-interactively' how to read arguments
48 to pass to the function.
49 When actually called, `interactive' just returns nil.
51 Usually the argument of `interactive' is a string containing a code letter
52 followed optionally by a prompt. (Some code letters do not use I/O to get
53 the argument and do not use prompts.) To get several arguments, concatenate
54 the individual strings, separating them by newline characters.
55 Prompts are passed to format, and may use % escapes to print the
56 arguments that have already been read.
57 If the argument is not a string, it is evaluated to get a list of
58 arguments to pass to the function.
59 Just `(interactive)' means pass no args when calling interactively.
61 Code letters available are:
62 a -- Function name: symbol with a function definition.
63 b -- Name of existing buffer.
64 B -- Name of buffer, possibly nonexistent.
65 c -- Character (no input method is used).
66 C -- Command name: symbol with interactive function definition.
67 d -- Value of point as number. Does not do I/O.
68 D -- Directory name.
69 e -- Parameterized event (i.e., one that's a list) that invoked this command.
70 If used more than once, the Nth `e' returns the Nth parameterized event.
71 This skips events that are integers or symbols.
72 f -- Existing file name.
73 F -- Possibly nonexistent file name.
74 G -- Possibly nonexistent file name, defaulting to just directory name.
75 i -- Ignored, i.e. always nil. Does not do I/O.
76 k -- Key sequence (downcase the last event if needed to get a definition).
77 K -- Key sequence to be redefined (do not downcase the last event).
78 m -- Value of mark as number. Does not do I/O.
79 M -- Any string. Inherits the current input method.
80 n -- Number read using minibuffer.
81 N -- Numeric prefix arg, or if none, do like code `n'.
82 p -- Prefix arg converted to number. Does not do I/O.
83 P -- Prefix arg in raw form. Does not do I/O.
84 r -- Region: point and mark as 2 numeric args, smallest first. Does no I/O.
85 s -- Any string. Does not inherit the current input method.
86 S -- Any symbol.
87 U -- Mouse up event discarded by a previous k or K argument.
88 v -- Variable name: symbol that is `custom-variable-p'.
89 x -- Lisp expression read but not evaluated.
90 X -- Lisp expression read and evaluated.
91 z -- Coding system.
92 Z -- Coding system, nil if no prefix arg.
94 In addition, if the string begins with `*', an error is signaled if
95 the buffer is read-only.
96 If `@' appears at the beginning of the string, and if the key sequence
97 used to invoke the command includes any mouse events, then the window
98 associated with the first of those events is selected before the
99 command is run.
100 If the string begins with `^' and `shift-select-mode' is non-nil,
101 Emacs first calls the function `handle-shift-selection'.
102 You may use `@', `*', and `^' together. They are processed in the
103 order that they appear, before reading any arguments.
104 usage: (interactive &optional ARGS) */
105 attributes: const)
106 (Lisp_Object args)
108 return Qnil;
111 /* Quotify EXP: if EXP is constant, return it.
112 If EXP is not constant, return (quote EXP). */
113 static Lisp_Object
114 quotify_arg (register Lisp_Object exp)
116 if (CONSP (exp)
117 || (SYMBOLP (exp)
118 && !NILP (exp) && !EQ (exp, Qt)))
119 return list2 (Qquote, exp);
121 return exp;
124 /* Modify EXP by quotifying each element (except the first). */
125 static Lisp_Object
126 quotify_args (Lisp_Object exp)
128 register Lisp_Object tail;
129 Lisp_Object next;
130 for (tail = exp; CONSP (tail); tail = next)
132 next = XCDR (tail);
133 XSETCAR (tail, quotify_arg (XCAR (tail)));
135 return exp;
138 static const char *callint_argfuns[]
139 = {"", "point", "mark", "region-beginning", "region-end"};
141 static void
142 check_mark (bool for_region)
144 Lisp_Object tem;
145 tem = Fmarker_buffer (BVAR (current_buffer, mark));
146 if (NILP (tem) || (XBUFFER (tem) != current_buffer))
147 error (for_region ? "The mark is not set now, so there is no region"
148 : "The mark is not set now");
149 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
150 && NILP (BVAR (current_buffer, mark_active)))
151 xsignal0 (Qmark_inactive);
154 /* If the list of args INPUT was produced with an explicit call to
155 `list', look for elements that were computed with
156 (region-beginning) or (region-end), and put those expressions into
157 VALUES instead of the present values.
159 This function doesn't return a value because it modifies elements
160 of VALUES to do its job. */
162 static void
163 fix_command (Lisp_Object input, Lisp_Object values)
165 /* FIXME: Instead of this ugly hack, we should provide a way for an
166 interactive spec to return an expression/function that will re-build the
167 args without user intervention. */
168 if (CONSP (input))
170 Lisp_Object car;
172 car = XCAR (input);
173 /* Skip through certain special forms. */
174 while (EQ (car, Qlet) || EQ (car, Qletx)
175 || EQ (car, Qsave_excursion)
176 || EQ (car, Qprogn))
178 while (CONSP (XCDR (input)))
179 input = XCDR (input);
180 input = XCAR (input);
181 if (!CONSP (input))
182 break;
183 car = XCAR (input);
185 if (EQ (car, Qlist))
187 Lisp_Object intail, valtail;
188 for (intail = Fcdr (input), valtail = values;
189 CONSP (valtail);
190 intail = Fcdr (intail), valtail = XCDR (valtail))
192 Lisp_Object elt;
193 elt = Fcar (intail);
194 if (CONSP (elt))
196 Lisp_Object presflag, carelt;
197 carelt = XCAR (elt);
198 /* If it is (if X Y), look at Y. */
199 if (EQ (carelt, Qif)
200 && EQ (Fnthcdr (make_number (3), elt), Qnil))
201 elt = Fnth (make_number (2), elt);
202 /* If it is (when ... Y), look at Y. */
203 else if (EQ (carelt, Qwhen))
205 while (CONSP (XCDR (elt)))
206 elt = XCDR (elt);
207 elt = Fcar (elt);
210 /* If the function call we're looking at
211 is a special preserved one, copy the
212 whole expression for this argument. */
213 if (CONSP (elt))
215 presflag = Fmemq (Fcar (elt), preserved_fns);
216 if (!NILP (presflag))
217 Fsetcar (valtail, Fcar (intail));
225 /* Helper function to call `read-file-name' from C. */
227 static Lisp_Object
228 read_file_name (Lisp_Object default_filename, Lisp_Object mustmatch,
229 Lisp_Object initial, Lisp_Object predicate)
231 struct gcpro gcpro1;
232 GCPRO1 (default_filename);
233 RETURN_UNGCPRO (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' 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 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
302 ptrdiff_t key_count;
303 bool record_then_fail = 0;
305 Lisp_Object save_this_command, save_last_command;
306 Lisp_Object save_this_original_command, save_real_this_command;
308 save_this_command = Vthis_command;
309 save_this_original_command = Vthis_original_command;
310 save_real_this_command = Vreal_this_command;
311 save_last_command = KVAR (current_kboard, Vlast_command);
313 if (NILP (keys))
314 keys = this_command_keys, key_count = this_command_key_count;
315 else
317 CHECK_VECTOR (keys);
318 key_count = ASIZE (keys);
321 /* Save this now, since use of minibuffer will clobber it. */
322 prefix_arg = Vcurrent_prefix_arg;
324 if (SYMBOLP (function))
325 enable = Fget (function, Qenable_recursive_minibuffers);
326 else
327 enable = Qnil;
329 specs = Qnil;
330 string = 0;
331 /* The idea of FILTER_SPECS is to provide a way to
332 specify how to represent the arguments in command history.
333 The feature is not fully implemented. */
334 filter_specs = Qnil;
336 /* If k or K discard an up-event, save it here so it can be retrieved with
337 U. */
338 up_event = Qnil;
340 /* Set SPECS to the interactive form, or barf if not interactive. */
342 Lisp_Object form;
343 GCPRO2 (function, prefix_arg);
344 form = Finteractive_form (function);
345 UNGCPRO;
346 if (CONSP (form))
347 specs = filter_specs = Fcar (XCDR (form));
348 else
349 wrong_type_argument (Qcommandp, function);
352 /* If SPECS is not a string, invent one. */
353 if (! STRINGP (specs))
355 Lisp_Object input;
356 Lisp_Object funval = Findirect_function (function, Qt);
357 uintmax_t events = num_input_events;
358 input = specs;
359 /* Compute the arg values using the user's expression. */
360 GCPRO2 (input, filter_specs);
361 specs = Feval (specs,
362 CONSP (funval) && EQ (Qclosure, XCAR (funval))
363 ? CAR_SAFE (XCDR (funval)) : Qnil);
364 UNGCPRO;
365 if (events != num_input_events || !NILP (record_flag))
367 /* We should record this command on the command history. */
368 Lisp_Object values;
369 Lisp_Object this_cmd;
370 /* Make a copy of the list of values, for the command history,
371 and turn them into things we can eval. */
372 values = quotify_args (Fcopy_sequence (specs));
373 fix_command (input, values);
374 this_cmd = Fcons (function, values);
375 if (history_delete_duplicates)
376 Vcommand_history = Fdelete (this_cmd, Vcommand_history);
377 Vcommand_history = Fcons (this_cmd, Vcommand_history);
379 /* Don't keep command history around forever. */
380 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
382 teml = Fnthcdr (Vhistory_length, Vcommand_history);
383 if (CONSP (teml))
384 XSETCDR (teml, Qnil);
388 Vthis_command = save_this_command;
389 Vthis_original_command = save_this_original_command;
390 Vreal_this_command = save_real_this_command;
391 kset_last_command (current_kboard, save_last_command);
393 Lisp_Object result
394 = unbind_to (speccount, CALLN (Fapply, Qfuncall_interactively,
395 function, specs));
396 SAFE_FREE ();
397 return result;
400 /* SPECS is set to a string; use it as an interactive prompt.
401 Copy it so that STRING will be valid even if a GC relocates SPECS. */
402 SAFE_ALLOCA_STRING (string, specs);
404 /* Here if function specifies a string to control parsing the defaults. */
406 /* Set next_event to point to the first event with parameters. */
407 for (next_event = 0; next_event < key_count; next_event++)
408 if (EVENT_HAS_PARAMETERS (AREF (keys, next_event)))
409 break;
411 /* Handle special starting chars `*' and `@'. Also `-'. */
412 /* Note that `+' is reserved for user extensions. */
413 while (1)
415 if (*string == '+')
416 error ("`+' is not used in `interactive' for ordinary commands");
417 else if (*string == '*')
419 string++;
420 if (!NILP (BVAR (current_buffer, read_only)))
422 if (!NILP (record_flag))
424 char *p = string;
425 while (*p)
427 if (! (*p == 'r' || *p == 'p' || *p == 'P'
428 || *p == '\n'))
429 Fbarf_if_buffer_read_only (Qnil);
430 p++;
432 record_then_fail = 1;
434 else
435 Fbarf_if_buffer_read_only (Qnil);
438 /* Ignore this for semi-compatibility with Lucid. */
439 else if (*string == '-')
440 string++;
441 else if (*string == '@')
443 Lisp_Object event, w;
445 event = (next_event < key_count
446 ? AREF (keys, next_event)
447 : Qnil);
448 if (EVENT_HAS_PARAMETERS (event)
449 && (w = XCDR (event), CONSP (w))
450 && (w = XCAR (w), CONSP (w))
451 && (w = XCAR (w), WINDOWP (w)))
453 if (MINI_WINDOW_P (XWINDOW (w))
454 && ! (minibuf_level > 0 && EQ (w, minibuf_window)))
455 error ("Attempt to select inactive minibuffer window");
457 /* If the current buffer wants to clean up, let it. */
458 run_hook (Qmouse_leave_buffer_hook);
460 Fselect_window (w, Qnil);
462 string++;
464 else if (*string == '^')
466 call0 (Qhandle_shift_selection);
467 string++;
469 else break;
472 /* Count the number of arguments, which is two (the function itself and
473 `funcall-interactively') plus the number of arguments the interactive spec
474 would have us give to the function. */
475 tem = string;
476 for (nargs = 2; *tem; )
478 /* 'r' specifications ("point and mark as 2 numeric args")
479 produce *two* arguments. */
480 if (*tem == 'r')
481 nargs += 2;
482 else
483 nargs++;
484 tem = strchr (tem, '\n');
485 if (tem)
486 ++tem;
487 else
488 break;
491 if (MOST_POSITIVE_FIXNUM < min (PTRDIFF_MAX, SIZE_MAX) / word_size
492 && MOST_POSITIVE_FIXNUM < nargs)
493 memory_full (SIZE_MAX);
495 /* Allocate them all at one go. This wastes a bit of memory, but
496 it's OK to trade space for speed. */
497 SAFE_NALLOCA (args, 3, nargs);
498 visargs = args + nargs;
499 varies = (signed char *) (visargs + nargs);
501 memclear (args, nargs * (2 * word_size + 1));
503 GCPRO5 (prefix_arg, function, *args, *visargs, up_event);
504 gcpro3.nvars = nargs;
505 gcpro4.nvars = nargs;
507 if (!NILP (enable))
508 specbind (Qenable_recursive_minibuffers, Qt);
510 tem = string;
511 for (i = 2; *tem; i++)
513 visargs[1] = make_string (tem + 1, strcspn (tem + 1, "\n"));
514 if (strchr (SSDATA (visargs[1]), '%'))
515 callint_message = Fformat (i - 1, visargs + 1);
516 else
517 callint_message = visargs[1];
519 switch (*tem)
521 case 'a': /* Symbol defined as a function. */
522 visargs[i] = Fcompleting_read (callint_message,
523 Vobarray, Qfboundp, Qt,
524 Qnil, Qnil, Qnil, Qnil);
525 /* Passing args[i] directly stimulates compiler bug. */
526 teml = visargs[i];
527 args[i] = Fintern (teml, Qnil);
528 break;
530 case 'b': /* Name of existing buffer. */
531 args[i] = Fcurrent_buffer ();
532 if (EQ (selected_window, minibuf_window))
533 args[i] = Fother_buffer (args[i], Qnil, Qnil);
534 args[i] = Fread_buffer (callint_message, args[i], Qt);
535 break;
537 case 'B': /* Name of buffer, possibly nonexistent. */
538 args[i] = Fread_buffer (callint_message,
539 Fother_buffer (Fcurrent_buffer (), Qnil, Qnil),
540 Qnil);
541 break;
543 case 'c': /* Character. */
544 /* Prompt in `minibuffer-prompt' face. */
545 Fput_text_property (make_number (0),
546 make_number (SCHARS (callint_message)),
547 Qface, Qminibuffer_prompt, callint_message);
548 args[i] = Fread_char (callint_message, Qnil, Qnil);
549 message1_nolog (0);
550 /* Passing args[i] directly stimulates compiler bug. */
551 teml = args[i];
552 /* See bug#8479. */
553 if (! CHARACTERP (teml)) error ("Non-character input-event");
554 visargs[i] = Fchar_to_string (teml);
555 break;
557 case 'C': /* Command: symbol with interactive function. */
558 visargs[i] = Fcompleting_read (callint_message,
559 Vobarray, Qcommandp,
560 Qt, Qnil, Qnil, Qnil, Qnil);
561 /* Passing args[i] directly stimulates compiler bug. */
562 teml = visargs[i];
563 args[i] = Fintern (teml, Qnil);
564 break;
566 case 'd': /* Value of point. Does not do I/O. */
567 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
568 args[i] = point_marker;
569 /* visargs[i] = Qnil; */
570 varies[i] = 1;
571 break;
573 case 'D': /* Directory name. */
574 args[i] = read_file_name (BVAR (current_buffer, directory), Qlambda, Qnil,
575 Qfile_directory_p);
576 break;
578 case 'f': /* Existing file name. */
579 args[i] = read_file_name (Qnil, Qlambda, Qnil, Qnil);
580 break;
582 case 'F': /* Possibly nonexistent file name. */
583 args[i] = read_file_name (Qnil, Qnil, Qnil, Qnil);
584 break;
586 case 'G': /* Possibly nonexistent file name,
587 default to directory alone. */
588 args[i] = read_file_name (Qnil, Qnil, empty_unibyte_string, Qnil);
589 break;
591 case 'i': /* Ignore an argument -- Does not do I/O. */
592 varies[i] = -1;
593 break;
595 case 'k': /* Key sequence. */
597 ptrdiff_t speccount1 = SPECPDL_INDEX ();
598 specbind (Qcursor_in_echo_area, Qt);
599 /* Prompt in `minibuffer-prompt' face. */
600 Fput_text_property (make_number (0),
601 make_number (SCHARS (callint_message)),
602 Qface, Qminibuffer_prompt, callint_message);
603 args[i] = Fread_key_sequence (callint_message,
604 Qnil, Qnil, Qnil, Qnil);
605 unbind_to (speccount1, Qnil);
606 teml = args[i];
607 visargs[i] = Fkey_description (teml, Qnil);
609 /* If the key sequence ends with a down-event,
610 discard the following up-event. */
611 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
612 if (CONSP (teml))
613 teml = XCAR (teml);
614 if (SYMBOLP (teml))
616 Lisp_Object tem2;
618 teml = Fget (teml, Qevent_symbol_elements);
619 /* Ignore first element, which is the base key. */
620 tem2 = Fmemq (Qdown, Fcdr (teml));
621 if (! NILP (tem2))
622 up_event = Fread_event (Qnil, Qnil, Qnil);
625 break;
627 case 'K': /* Key sequence to be defined. */
629 ptrdiff_t speccount1 = SPECPDL_INDEX ();
630 specbind (Qcursor_in_echo_area, Qt);
631 /* Prompt in `minibuffer-prompt' face. */
632 Fput_text_property (make_number (0),
633 make_number (SCHARS (callint_message)),
634 Qface, Qminibuffer_prompt, callint_message);
635 args[i] = Fread_key_sequence_vector (callint_message,
636 Qnil, Qt, Qnil, Qnil);
637 teml = args[i];
638 visargs[i] = Fkey_description (teml, Qnil);
639 unbind_to (speccount1, Qnil);
641 /* If the key sequence ends with a down-event,
642 discard the following up-event. */
643 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
644 if (CONSP (teml))
645 teml = XCAR (teml);
646 if (SYMBOLP (teml))
648 Lisp_Object tem2;
650 teml = Fget (teml, Qevent_symbol_elements);
651 /* Ignore first element, which is the base key. */
652 tem2 = Fmemq (Qdown, Fcdr (teml));
653 if (! NILP (tem2))
654 up_event = Fread_event (Qnil, Qnil, Qnil);
657 break;
659 case 'U': /* Up event from last k or K. */
660 if (!NILP (up_event))
662 args[i] = Fmake_vector (make_number (1), up_event);
663 up_event = Qnil;
664 teml = args[i];
665 visargs[i] = Fkey_description (teml, Qnil);
667 break;
669 case 'e': /* The invoking event. */
670 if (next_event >= key_count)
671 error ("%s must be bound to an event with parameters",
672 (SYMBOLP (function)
673 ? SSDATA (SYMBOL_NAME (function))
674 : "command"));
675 args[i] = AREF (keys, next_event);
676 next_event++;
677 varies[i] = -1;
679 /* Find the next parameterized event. */
680 while (next_event < key_count
681 && !(EVENT_HAS_PARAMETERS (AREF (keys, next_event))))
682 next_event++;
684 break;
686 case 'm': /* Value of mark. Does not do I/O. */
687 check_mark (0);
688 /* visargs[i] = Qnil; */
689 args[i] = BVAR (current_buffer, mark);
690 varies[i] = 2;
691 break;
693 case 'M': /* String read via minibuffer with
694 inheriting the current input method. */
695 args[i] = Fread_string (callint_message,
696 Qnil, Qnil, Qnil, Qt);
697 break;
699 case 'N': /* Prefix arg as number, else number from minibuffer. */
700 if (!NILP (prefix_arg))
701 goto have_prefix_arg;
702 case 'n': /* Read number from minibuffer. */
703 args[i] = call1 (Qread_number, callint_message);
704 /* Passing args[i] directly stimulates compiler bug. */
705 teml = args[i];
706 visargs[i] = Fnumber_to_string (teml);
707 break;
709 case 'P': /* Prefix arg in raw form. Does no I/O. */
710 args[i] = prefix_arg;
711 /* visargs[i] = Qnil; */
712 varies[i] = -1;
713 break;
715 case 'p': /* Prefix arg converted to number. No I/O. */
716 have_prefix_arg:
717 args[i] = Fprefix_numeric_value (prefix_arg);
718 /* visargs[i] = Qnil; */
719 varies[i] = -1;
720 break;
722 case 'r': /* Region, point and mark as 2 args. */
723 check_mark (1);
724 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
725 /* visargs[i+1] = Qnil; */
726 mark = marker_position (BVAR (current_buffer, mark));
727 /* visargs[i] = Qnil; */
728 args[i] = PT < mark ? point_marker : BVAR (current_buffer, mark);
729 varies[i] = 3;
730 args[++i] = PT > mark ? point_marker : BVAR (current_buffer, mark);
731 varies[i] = 4;
732 break;
734 case 's': /* String read via minibuffer without
735 inheriting the current input method. */
736 args[i] = Fread_string (callint_message,
737 Qnil, Qnil, Qnil, Qnil);
738 break;
740 case 'S': /* Any symbol. */
741 visargs[i] = Fread_string (callint_message,
742 Qnil, Qnil, Qnil, Qnil);
743 /* Passing args[i] directly stimulates compiler bug. */
744 teml = visargs[i];
745 args[i] = Fintern (teml, Qnil);
746 break;
748 case 'v': /* Variable name: symbol that is
749 custom-variable-p. */
750 args[i] = Fread_variable (callint_message, Qnil);
751 visargs[i] = last_minibuf_string;
752 break;
754 case 'x': /* Lisp expression read but not evaluated. */
755 args[i] = call1 (intern ("read-minibuffer"), callint_message);
756 visargs[i] = last_minibuf_string;
757 break;
759 case 'X': /* Lisp expression read and evaluated. */
760 args[i] = call1 (intern ("eval-minibuffer"), callint_message);
761 visargs[i] = last_minibuf_string;
762 break;
764 case 'Z': /* Coding-system symbol, or ignore the
765 argument if no prefix. */
766 if (NILP (prefix_arg))
768 /* args[i] = Qnil; */
769 varies[i] = -1;
771 else
773 args[i]
774 = Fread_non_nil_coding_system (callint_message);
775 visargs[i] = last_minibuf_string;
777 break;
779 case 'z': /* Coding-system symbol or nil. */
780 args[i] = Fread_coding_system (callint_message, Qnil);
781 visargs[i] = last_minibuf_string;
782 break;
784 /* We have a case for `+' so we get an error
785 if anyone tries to define one here. */
786 case '+':
787 default:
788 error ("Invalid control letter `%c' (#o%03o, #x%04x) in interactive calling string",
789 STRING_CHAR ((unsigned char *) tem),
790 (unsigned) STRING_CHAR ((unsigned char *) tem),
791 (unsigned) STRING_CHAR ((unsigned char *) tem));
794 if (varies[i] == 0)
795 arg_from_tty = 1;
797 if (NILP (visargs[i]) && STRINGP (args[i]))
798 visargs[i] = args[i];
800 tem = strchr (tem, '\n');
801 if (tem) tem++;
802 else tem = "";
804 unbind_to (speccount, Qnil);
806 QUIT;
808 args[0] = Qfuncall_interactively;
809 args[1] = function;
811 if (arg_from_tty || !NILP (record_flag))
813 /* We don't need `visargs' any more, so let's recycle it since we need
814 an array of just the same size. */
815 visargs[1] = function;
816 for (i = 2; i < nargs; i++)
818 if (varies[i] > 0)
819 visargs[i] = list1 (intern (callint_argfuns[varies[i]]));
820 else
821 visargs[i] = quotify_arg (args[i]);
823 Vcommand_history = Fcons (Flist (nargs - 1, visargs + 1),
824 Vcommand_history);
825 /* Don't keep command history around forever. */
826 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
828 teml = Fnthcdr (Vhistory_length, Vcommand_history);
829 if (CONSP (teml))
830 XSETCDR (teml, Qnil);
834 /* If we used a marker to hold point, mark, or an end of the region,
835 temporarily, convert it to an integer now. */
836 for (i = 2; i < nargs; i++)
837 if (varies[i] >= 1 && varies[i] <= 4)
838 XSETINT (args[i], marker_position (args[i]));
840 if (record_then_fail)
841 Fbarf_if_buffer_read_only (Qnil);
843 Vthis_command = save_this_command;
844 Vthis_original_command = save_this_original_command;
845 Vreal_this_command = save_real_this_command;
846 kset_last_command (current_kboard, save_last_command);
849 Lisp_Object val = Ffuncall (nargs, args);
850 UNGCPRO;
851 val = unbind_to (speccount, val);
852 SAFE_FREE ();
853 return val;
857 DEFUN ("prefix-numeric-value", Fprefix_numeric_value, Sprefix_numeric_value,
858 1, 1, 0,
859 doc: /* Return numeric meaning of raw prefix argument RAW.
860 A raw prefix argument is what you get from `(interactive "P")'.
861 Its numeric meaning is what you would get from `(interactive "p")'. */)
862 (Lisp_Object raw)
864 Lisp_Object val;
866 if (NILP (raw))
867 XSETFASTINT (val, 1);
868 else if (EQ (raw, Qminus))
869 XSETINT (val, -1);
870 else if (CONSP (raw) && INTEGERP (XCAR (raw)))
871 XSETINT (val, XINT (XCAR (raw)));
872 else if (INTEGERP (raw))
873 val = raw;
874 else
875 XSETFASTINT (val, 1);
877 return val;
880 void
881 syms_of_callint (void)
883 point_marker = Fmake_marker ();
884 staticpro (&point_marker);
886 callint_message = Qnil;
887 staticpro (&callint_message);
889 preserved_fns = listn (CONSTYPE_PURE, 4,
890 intern_c_string ("region-beginning"),
891 intern_c_string ("region-end"),
892 intern_c_string ("point"),
893 intern_c_string ("mark"));
895 DEFSYM (Qlist, "list");
896 DEFSYM (Qlet, "let");
897 DEFSYM (Qif, "if");
898 DEFSYM (Qwhen, "when");
899 DEFSYM (Qletx, "let*");
900 DEFSYM (Qsave_excursion, "save-excursion");
901 DEFSYM (Qprogn, "progn");
902 DEFSYM (Qminus, "-");
903 DEFSYM (Qplus, "+");
904 DEFSYM (Qhandle_shift_selection, "handle-shift-selection");
905 DEFSYM (Qread_number, "read-number");
906 DEFSYM (Qfuncall_interactively, "funcall-interactively");
907 DEFSYM (Qcommand_debug_status, "command-debug-status");
908 DEFSYM (Qenable_recursive_minibuffers, "enable-recursive-minibuffers");
909 DEFSYM (Qmouse_leave_buffer_hook, "mouse-leave-buffer-hook");
911 DEFVAR_KBOARD ("prefix-arg", Vprefix_arg,
912 doc: /* The value of the prefix argument for the next editing command.
913 It may be a number, or the symbol `-' for just a minus sign as arg,
914 or a list whose car is a number for just one or more C-u's
915 or nil if no argument has been specified.
917 You cannot examine this variable to find the argument for this command
918 since it has been set to nil by the time you can look.
919 Instead, you should use the variable `current-prefix-arg', although
920 normally commands can get this prefix argument with (interactive "P"). */);
922 DEFVAR_KBOARD ("last-prefix-arg", Vlast_prefix_arg,
923 doc: /* The value of the prefix argument for the previous editing command.
924 See `prefix-arg' for the meaning of the value. */);
926 DEFVAR_LISP ("current-prefix-arg", Vcurrent_prefix_arg,
927 doc: /* The value of the prefix argument for this editing command.
928 It may be a number, or the symbol `-' for just a minus sign as arg,
929 or a list whose car is a number for just one or more C-u's
930 or nil if no argument has been specified.
931 This is what `(interactive \"P\")' returns. */);
932 Vcurrent_prefix_arg = Qnil;
934 DEFVAR_LISP ("command-history", Vcommand_history,
935 doc: /* List of recent commands that read arguments from terminal.
936 Each command is represented as a form to evaluate.
938 Maximum length of the history list is determined by the value
939 of `history-length', which see. */);
940 Vcommand_history = Qnil;
942 DEFVAR_LISP ("command-debug-status", Vcommand_debug_status,
943 doc: /* Debugging status of current interactive command.
944 Bound each time `call-interactively' is called;
945 may be set by the debugger as a reminder for itself. */);
946 Vcommand_debug_status = Qnil;
948 DEFVAR_LISP ("mark-even-if-inactive", Vmark_even_if_inactive,
949 doc: /* Non-nil means you can use the mark even when inactive.
950 This option makes a difference in Transient Mark mode.
951 When the option is non-nil, deactivation of the mark
952 turns off region highlighting, but commands that use the mark
953 behave as if the mark were still active. */);
954 Vmark_even_if_inactive = Qt;
956 DEFVAR_LISP ("mouse-leave-buffer-hook", Vmouse_leave_buffer_hook,
957 doc: /* Hook to run when about to switch windows with a mouse command.
958 Its purpose is to give temporary modes such as Isearch mode
959 a way to turn themselves off when a mouse command switches windows. */);
960 Vmouse_leave_buffer_hook = Qnil;
962 defsubr (&Sinteractive);
963 defsubr (&Scall_interactively);
964 defsubr (&Sfuncall_interactively);
965 defsubr (&Sprefix_numeric_value);