]> code.delx.au - gnu-emacs/blob - src/callint.c
Compute C decls for DEFSYMs automatically
[gnu-emacs] / src / callint.c
1 /* Call a Lisp function interactively.
2 Copyright (C) 1985-1986, 1993-1995, 1997, 2000-2015 Free Software
3 Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20
21 #include <config.h>
22
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"
30
31 static Lisp_Object preserved_fns;
32
33 /* Marker used within call-interactively to refer to point. */
34 static Lisp_Object point_marker;
35
36 /* String for the prompt text used in Fcall_interactively. */
37 static Lisp_Object callint_message;
38 \f
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.
50
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.
60
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.
93
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 (Lisp_Object args)
106 {
107 return Qnil;
108 }
109
110 /* Quotify EXP: if EXP is constant, return it.
111 If EXP is not constant, return (quote EXP). */
112 static Lisp_Object
113 quotify_arg (register Lisp_Object exp)
114 {
115 if (CONSP (exp)
116 || (SYMBOLP (exp)
117 && !NILP (exp) && !EQ (exp, Qt)))
118 return list2 (Qquote, exp);
119
120 return exp;
121 }
122
123 /* Modify EXP by quotifying each element (except the first). */
124 static Lisp_Object
125 quotify_args (Lisp_Object exp)
126 {
127 register Lisp_Object tail;
128 Lisp_Object next;
129 for (tail = exp; CONSP (tail); tail = next)
130 {
131 next = XCDR (tail);
132 XSETCAR (tail, quotify_arg (XCAR (tail)));
133 }
134 return exp;
135 }
136
137 static const char *callint_argfuns[]
138 = {"", "point", "mark", "region-beginning", "region-end"};
139
140 static void
141 check_mark (bool for_region)
142 {
143 Lisp_Object tem;
144 tem = Fmarker_buffer (BVAR (current_buffer, mark));
145 if (NILP (tem) || (XBUFFER (tem) != current_buffer))
146 error (for_region ? "The mark is not set now, so there is no region"
147 : "The mark is not set now");
148 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
149 && NILP (BVAR (current_buffer, mark_active)))
150 xsignal0 (Qmark_inactive);
151 }
152
153 /* If the list of args INPUT was produced with an explicit call to
154 `list', look for elements that were computed with
155 (region-beginning) or (region-end), and put those expressions into
156 VALUES instead of the present values.
157
158 This function doesn't return a value because it modifies elements
159 of VALUES to do its job. */
160
161 static void
162 fix_command (Lisp_Object input, Lisp_Object values)
163 {
164 /* FIXME: Instead of this ugly hack, we should provide a way for an
165 interactive spec to return an expression/function that will re-build the
166 args without user intervention. */
167 if (CONSP (input))
168 {
169 Lisp_Object car;
170
171 car = XCAR (input);
172 /* Skip through certain special forms. */
173 while (EQ (car, Qlet) || EQ (car, Qletx)
174 || EQ (car, Qsave_excursion)
175 || EQ (car, Qprogn))
176 {
177 while (CONSP (XCDR (input)))
178 input = XCDR (input);
179 input = XCAR (input);
180 if (!CONSP (input))
181 break;
182 car = XCAR (input);
183 }
184 if (EQ (car, Qlist))
185 {
186 Lisp_Object intail, valtail;
187 for (intail = Fcdr (input), valtail = values;
188 CONSP (valtail);
189 intail = Fcdr (intail), valtail = XCDR (valtail))
190 {
191 Lisp_Object elt;
192 elt = Fcar (intail);
193 if (CONSP (elt))
194 {
195 Lisp_Object presflag, carelt;
196 carelt = XCAR (elt);
197 /* If it is (if X Y), look at Y. */
198 if (EQ (carelt, Qif)
199 && EQ (Fnthcdr (make_number (3), elt), Qnil))
200 elt = Fnth (make_number (2), elt);
201 /* If it is (when ... Y), look at Y. */
202 else if (EQ (carelt, Qwhen))
203 {
204 while (CONSP (XCDR (elt)))
205 elt = XCDR (elt);
206 elt = Fcar (elt);
207 }
208
209 /* If the function call we're looking at
210 is a special preserved one, copy the
211 whole expression for this argument. */
212 if (CONSP (elt))
213 {
214 presflag = Fmemq (Fcar (elt), preserved_fns);
215 if (!NILP (presflag))
216 Fsetcar (valtail, Fcar (intail));
217 }
218 }
219 }
220 }
221 }
222 }
223
224 /* Helper function to call `read-file-name' from C. */
225
226 static Lisp_Object
227 read_file_name (Lisp_Object default_filename, Lisp_Object mustmatch,
228 Lisp_Object initial, Lisp_Object predicate)
229 {
230 struct gcpro gcpro1;
231 Lisp_Object args[7];
232
233 GCPRO1 (default_filename);
234 args[0] = intern ("read-file-name");
235 args[1] = callint_message;
236 args[2] = Qnil;
237 args[3] = default_filename;
238 args[4] = mustmatch;
239 args[5] = initial;
240 args[6] = predicate;
241 RETURN_UNGCPRO (Ffuncall (7, args));
242 }
243
244 /* BEWARE: Calling this directly from C would defeat the purpose! */
245 DEFUN ("funcall-interactively", Ffuncall_interactively, Sfuncall_interactively,
246 1, MANY, 0, doc: /* Like `funcall' but marks the call as interactive.
247 I.e. arrange that within the called function `called-interactively-p' will
248 return non-nil.
249 usage: (funcall-interactively FUNCTION &rest ARGUMENTS) */)
250 (ptrdiff_t nargs, Lisp_Object *args)
251 {
252 ptrdiff_t speccount = SPECPDL_INDEX ();
253 temporarily_switch_to_single_kboard (NULL);
254
255 /* Nothing special to do here, all the work is inside
256 `called-interactively-p'. Which will look for us as a marker in the
257 backtrace. */
258 return unbind_to (speccount, Ffuncall (nargs, args));
259 }
260
261 DEFUN ("call-interactively", Fcall_interactively, Scall_interactively, 1, 3, 0,
262 doc: /* Call FUNCTION, providing args according to its interactive calling specs.
263 Return the value FUNCTION returns.
264 The function contains a specification of how to do the argument reading.
265 In the case of user-defined functions, this is specified by placing a call
266 to the function `interactive' at the top level of the function body.
267 See `interactive'.
268
269 Optional second arg RECORD-FLAG non-nil
270 means unconditionally put this command in the command-history.
271 Otherwise, this is done only if an arg is read using the minibuffer.
272
273 Optional third arg KEYS, if given, specifies the sequence of events to
274 supply, as a vector, if the command inquires which events were used to
275 invoke it. If KEYS is omitted or nil, the return value of
276 `this-command-keys-vector' is used. */)
277 (Lisp_Object function, Lisp_Object record_flag, Lisp_Object keys)
278 {
279 /* `args' will contain the array of arguments to pass to the function.
280 `visargs' will contain the same list but in a nicer form, so that if we
281 pass it to `Fformat' it will be understandable to a human. */
282 Lisp_Object *args, *visargs;
283 Lisp_Object specs;
284 Lisp_Object filter_specs;
285 Lisp_Object teml;
286 Lisp_Object up_event;
287 Lisp_Object enable;
288 USE_SAFE_ALLOCA;
289 ptrdiff_t speccount = SPECPDL_INDEX ();
290
291 /* The index of the next element of this_command_keys to examine for
292 the 'e' interactive code. */
293 ptrdiff_t next_event;
294
295 Lisp_Object prefix_arg;
296 char *string;
297 const char *tem;
298
299 /* If varies[i] > 0, the i'th argument shouldn't just have its value
300 in this call quoted in the command history. It should be
301 recorded as a call to the function named callint_argfuns[varies[i]]. */
302 signed char *varies;
303
304 ptrdiff_t i, nargs;
305 ptrdiff_t mark;
306 bool arg_from_tty = 0;
307 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
308 ptrdiff_t key_count;
309 bool record_then_fail = 0;
310
311 Lisp_Object save_this_command, save_last_command;
312 Lisp_Object save_this_original_command, save_real_this_command;
313
314 save_this_command = Vthis_command;
315 save_this_original_command = Vthis_original_command;
316 save_real_this_command = Vreal_this_command;
317 save_last_command = KVAR (current_kboard, Vlast_command);
318
319 if (NILP (keys))
320 keys = this_command_keys, key_count = this_command_key_count;
321 else
322 {
323 CHECK_VECTOR (keys);
324 key_count = ASIZE (keys);
325 }
326
327 /* Save this now, since use of minibuffer will clobber it. */
328 prefix_arg = Vcurrent_prefix_arg;
329
330 if (SYMBOLP (function))
331 enable = Fget (function, Qenable_recursive_minibuffers);
332 else
333 enable = Qnil;
334
335 specs = Qnil;
336 string = 0;
337 /* The idea of FILTER_SPECS is to provide a way to
338 specify how to represent the arguments in command history.
339 The feature is not fully implemented. */
340 filter_specs = Qnil;
341
342 /* If k or K discard an up-event, save it here so it can be retrieved with
343 U. */
344 up_event = Qnil;
345
346 /* Set SPECS to the interactive form, or barf if not interactive. */
347 {
348 Lisp_Object form;
349 GCPRO2 (function, prefix_arg);
350 form = Finteractive_form (function);
351 UNGCPRO;
352 if (CONSP (form))
353 specs = filter_specs = Fcar (XCDR (form));
354 else
355 wrong_type_argument (Qcommandp, function);
356 }
357
358 /* If SPECS is not a string, invent one. */
359 if (! STRINGP (specs))
360 {
361 Lisp_Object input;
362 Lisp_Object funval = Findirect_function (function, Qt);
363 uintmax_t events = num_input_events;
364 input = specs;
365 /* Compute the arg values using the user's expression. */
366 GCPRO2 (input, filter_specs);
367 specs = Feval (specs,
368 CONSP (funval) && EQ (Qclosure, XCAR (funval))
369 ? CAR_SAFE (XCDR (funval)) : Qnil);
370 UNGCPRO;
371 if (events != num_input_events || !NILP (record_flag))
372 {
373 /* We should record this command on the command history. */
374 Lisp_Object values;
375 Lisp_Object this_cmd;
376 /* Make a copy of the list of values, for the command history,
377 and turn them into things we can eval. */
378 values = quotify_args (Fcopy_sequence (specs));
379 fix_command (input, values);
380 this_cmd = Fcons (function, values);
381 if (history_delete_duplicates)
382 Vcommand_history = Fdelete (this_cmd, Vcommand_history);
383 Vcommand_history = Fcons (this_cmd, Vcommand_history);
384
385 /* Don't keep command history around forever. */
386 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
387 {
388 teml = Fnthcdr (Vhistory_length, Vcommand_history);
389 if (CONSP (teml))
390 XSETCDR (teml, Qnil);
391 }
392 }
393
394 Vthis_command = save_this_command;
395 Vthis_original_command = save_this_original_command;
396 Vreal_this_command = save_real_this_command;
397 kset_last_command (current_kboard, save_last_command);
398
399 {
400 Lisp_Object args[3];
401 args[0] = Qfuncall_interactively;
402 args[1] = function;
403 args[2] = specs;
404 Lisp_Object result = unbind_to (speccount, Fapply (3, args));
405 SAFE_FREE ();
406 return result;
407 }
408 }
409
410 /* SPECS is set to a string; use it as an interactive prompt.
411 Copy it so that STRING will be valid even if a GC relocates SPECS. */
412 SAFE_ALLOCA_STRING (string, specs);
413
414 /* Here if function specifies a string to control parsing the defaults. */
415
416 /* Set next_event to point to the first event with parameters. */
417 for (next_event = 0; next_event < key_count; next_event++)
418 if (EVENT_HAS_PARAMETERS (AREF (keys, next_event)))
419 break;
420
421 /* Handle special starting chars `*' and `@'. Also `-'. */
422 /* Note that `+' is reserved for user extensions. */
423 while (1)
424 {
425 if (*string == '+')
426 error ("`+' is not used in `interactive' for ordinary commands");
427 else if (*string == '*')
428 {
429 string++;
430 if (!NILP (BVAR (current_buffer, read_only)))
431 {
432 if (!NILP (record_flag))
433 {
434 char *p = string;
435 while (*p)
436 {
437 if (! (*p == 'r' || *p == 'p' || *p == 'P'
438 || *p == '\n'))
439 Fbarf_if_buffer_read_only (Qnil);
440 p++;
441 }
442 record_then_fail = 1;
443 }
444 else
445 Fbarf_if_buffer_read_only (Qnil);
446 }
447 }
448 /* Ignore this for semi-compatibility with Lucid. */
449 else if (*string == '-')
450 string++;
451 else if (*string == '@')
452 {
453 Lisp_Object event, w;
454
455 event = (next_event < key_count
456 ? AREF (keys, next_event)
457 : Qnil);
458 if (EVENT_HAS_PARAMETERS (event)
459 && (w = XCDR (event), CONSP (w))
460 && (w = XCAR (w), CONSP (w))
461 && (w = XCAR (w), WINDOWP (w)))
462 {
463 if (MINI_WINDOW_P (XWINDOW (w))
464 && ! (minibuf_level > 0 && EQ (w, minibuf_window)))
465 error ("Attempt to select inactive minibuffer window");
466
467 /* If the current buffer wants to clean up, let it. */
468 run_hook (Qmouse_leave_buffer_hook);
469
470 Fselect_window (w, Qnil);
471 }
472 string++;
473 }
474 else if (*string == '^')
475 {
476 call0 (Qhandle_shift_selection);
477 string++;
478 }
479 else break;
480 }
481
482 /* Count the number of arguments, which is two (the function itself and
483 `funcall-interactively') plus the number of arguments the interactive spec
484 would have us give to the function. */
485 tem = string;
486 for (nargs = 2; *tem; )
487 {
488 /* 'r' specifications ("point and mark as 2 numeric args")
489 produce *two* arguments. */
490 if (*tem == 'r')
491 nargs += 2;
492 else
493 nargs++;
494 tem = strchr (tem, '\n');
495 if (tem)
496 ++tem;
497 else
498 break;
499 }
500
501 if (MOST_POSITIVE_FIXNUM < min (PTRDIFF_MAX, SIZE_MAX) / word_size
502 && MOST_POSITIVE_FIXNUM < nargs)
503 memory_full (SIZE_MAX);
504
505 /* Allocate them all at one go. This wastes a bit of memory, but
506 it's OK to trade space for speed. */
507 SAFE_NALLOCA (args, 3, nargs);
508 visargs = args + nargs;
509 varies = (signed char *) (visargs + nargs);
510
511 for (i = 0; i < nargs; i++)
512 {
513 args[i] = Qnil;
514 visargs[i] = Qnil;
515 varies[i] = 0;
516 }
517
518 GCPRO5 (prefix_arg, function, *args, *visargs, up_event);
519 gcpro3.nvars = nargs;
520 gcpro4.nvars = nargs;
521
522 if (!NILP (enable))
523 specbind (Qenable_recursive_minibuffers, Qt);
524
525 tem = string;
526 for (i = 2; *tem; i++)
527 {
528 visargs[1] = make_string (tem + 1, strcspn (tem + 1, "\n"));
529 if (strchr (SSDATA (visargs[1]), '%'))
530 callint_message = Fformat (i - 1, visargs + 1);
531 else
532 callint_message = visargs[1];
533
534 switch (*tem)
535 {
536 case 'a': /* Symbol defined as a function. */
537 visargs[i] = Fcompleting_read (callint_message,
538 Vobarray, Qfboundp, Qt,
539 Qnil, Qnil, Qnil, Qnil);
540 /* Passing args[i] directly stimulates compiler bug. */
541 teml = visargs[i];
542 args[i] = Fintern (teml, Qnil);
543 break;
544
545 case 'b': /* Name of existing buffer. */
546 args[i] = Fcurrent_buffer ();
547 if (EQ (selected_window, minibuf_window))
548 args[i] = Fother_buffer (args[i], Qnil, Qnil);
549 args[i] = Fread_buffer (callint_message, args[i], Qt);
550 break;
551
552 case 'B': /* Name of buffer, possibly nonexistent. */
553 args[i] = Fread_buffer (callint_message,
554 Fother_buffer (Fcurrent_buffer (), Qnil, Qnil),
555 Qnil);
556 break;
557
558 case 'c': /* Character. */
559 /* Prompt in `minibuffer-prompt' face. */
560 Fput_text_property (make_number (0),
561 make_number (SCHARS (callint_message)),
562 Qface, Qminibuffer_prompt, callint_message);
563 args[i] = Fread_char (callint_message, Qnil, Qnil);
564 message1_nolog (0);
565 /* Passing args[i] directly stimulates compiler bug. */
566 teml = args[i];
567 /* See bug#8479. */
568 if (! CHARACTERP (teml)) error ("Non-character input-event");
569 visargs[i] = Fchar_to_string (teml);
570 break;
571
572 case 'C': /* Command: symbol with interactive function. */
573 visargs[i] = Fcompleting_read (callint_message,
574 Vobarray, Qcommandp,
575 Qt, Qnil, Qnil, Qnil, Qnil);
576 /* Passing args[i] directly stimulates compiler bug. */
577 teml = visargs[i];
578 args[i] = Fintern (teml, Qnil);
579 break;
580
581 case 'd': /* Value of point. Does not do I/O. */
582 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
583 args[i] = point_marker;
584 /* visargs[i] = Qnil; */
585 varies[i] = 1;
586 break;
587
588 case 'D': /* Directory name. */
589 args[i] = read_file_name (BVAR (current_buffer, directory), Qlambda, Qnil,
590 Qfile_directory_p);
591 break;
592
593 case 'f': /* Existing file name. */
594 args[i] = read_file_name (Qnil, Qlambda, Qnil, Qnil);
595 break;
596
597 case 'F': /* Possibly nonexistent file name. */
598 args[i] = read_file_name (Qnil, Qnil, Qnil, Qnil);
599 break;
600
601 case 'G': /* Possibly nonexistent file name,
602 default to directory alone. */
603 args[i] = read_file_name (Qnil, Qnil, empty_unibyte_string, Qnil);
604 break;
605
606 case 'i': /* Ignore an argument -- Does not do I/O. */
607 varies[i] = -1;
608 break;
609
610 case 'k': /* Key sequence. */
611 {
612 ptrdiff_t speccount1 = SPECPDL_INDEX ();
613 specbind (Qcursor_in_echo_area, Qt);
614 /* Prompt in `minibuffer-prompt' face. */
615 Fput_text_property (make_number (0),
616 make_number (SCHARS (callint_message)),
617 Qface, Qminibuffer_prompt, callint_message);
618 args[i] = Fread_key_sequence (callint_message,
619 Qnil, Qnil, Qnil, Qnil);
620 unbind_to (speccount1, Qnil);
621 teml = args[i];
622 visargs[i] = Fkey_description (teml, Qnil);
623
624 /* If the key sequence ends with a down-event,
625 discard the following up-event. */
626 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
627 if (CONSP (teml))
628 teml = XCAR (teml);
629 if (SYMBOLP (teml))
630 {
631 Lisp_Object tem2;
632
633 teml = Fget (teml, intern ("event-symbol-elements"));
634 /* Ignore first element, which is the base key. */
635 tem2 = Fmemq (intern ("down"), Fcdr (teml));
636 if (! NILP (tem2))
637 up_event = Fread_event (Qnil, Qnil, Qnil);
638 }
639 }
640 break;
641
642 case 'K': /* Key sequence to be defined. */
643 {
644 ptrdiff_t speccount1 = SPECPDL_INDEX ();
645 specbind (Qcursor_in_echo_area, Qt);
646 /* Prompt in `minibuffer-prompt' face. */
647 Fput_text_property (make_number (0),
648 make_number (SCHARS (callint_message)),
649 Qface, Qminibuffer_prompt, callint_message);
650 args[i] = Fread_key_sequence_vector (callint_message,
651 Qnil, Qt, Qnil, Qnil);
652 teml = args[i];
653 visargs[i] = Fkey_description (teml, Qnil);
654 unbind_to (speccount1, Qnil);
655
656 /* If the key sequence ends with a down-event,
657 discard the following up-event. */
658 teml = Faref (args[i], make_number (XINT (Flength (args[i])) - 1));
659 if (CONSP (teml))
660 teml = XCAR (teml);
661 if (SYMBOLP (teml))
662 {
663 Lisp_Object tem2;
664
665 teml = Fget (teml, intern ("event-symbol-elements"));
666 /* Ignore first element, which is the base key. */
667 tem2 = Fmemq (intern ("down"), Fcdr (teml));
668 if (! NILP (tem2))
669 up_event = Fread_event (Qnil, Qnil, Qnil);
670 }
671 }
672 break;
673
674 case 'U': /* Up event from last k or K. */
675 if (!NILP (up_event))
676 {
677 args[i] = Fmake_vector (make_number (1), up_event);
678 up_event = Qnil;
679 teml = args[i];
680 visargs[i] = Fkey_description (teml, Qnil);
681 }
682 break;
683
684 case 'e': /* The invoking event. */
685 if (next_event >= key_count)
686 error ("%s must be bound to an event with parameters",
687 (SYMBOLP (function)
688 ? SSDATA (SYMBOL_NAME (function))
689 : "command"));
690 args[i] = AREF (keys, next_event);
691 next_event++;
692 varies[i] = -1;
693
694 /* Find the next parameterized event. */
695 while (next_event < key_count
696 && !(EVENT_HAS_PARAMETERS (AREF (keys, next_event))))
697 next_event++;
698
699 break;
700
701 case 'm': /* Value of mark. Does not do I/O. */
702 check_mark (0);
703 /* visargs[i] = Qnil; */
704 args[i] = BVAR (current_buffer, mark);
705 varies[i] = 2;
706 break;
707
708 case 'M': /* String read via minibuffer with
709 inheriting the current input method. */
710 args[i] = Fread_string (callint_message,
711 Qnil, Qnil, Qnil, Qt);
712 break;
713
714 case 'N': /* Prefix arg as number, else number from minibuffer. */
715 if (!NILP (prefix_arg))
716 goto have_prefix_arg;
717 case 'n': /* Read number from minibuffer. */
718 args[i] = call1 (Qread_number, callint_message);
719 /* Passing args[i] directly stimulates compiler bug. */
720 teml = args[i];
721 visargs[i] = Fnumber_to_string (teml);
722 break;
723
724 case 'P': /* Prefix arg in raw form. Does no I/O. */
725 args[i] = prefix_arg;
726 /* visargs[i] = Qnil; */
727 varies[i] = -1;
728 break;
729
730 case 'p': /* Prefix arg converted to number. No I/O. */
731 have_prefix_arg:
732 args[i] = Fprefix_numeric_value (prefix_arg);
733 /* visargs[i] = Qnil; */
734 varies[i] = -1;
735 break;
736
737 case 'r': /* Region, point and mark as 2 args. */
738 check_mark (1);
739 set_marker_both (point_marker, Qnil, PT, PT_BYTE);
740 /* visargs[i+1] = Qnil; */
741 mark = marker_position (BVAR (current_buffer, mark));
742 /* visargs[i] = Qnil; */
743 args[i] = PT < mark ? point_marker : BVAR (current_buffer, mark);
744 varies[i] = 3;
745 args[++i] = PT > mark ? point_marker : BVAR (current_buffer, mark);
746 varies[i] = 4;
747 break;
748
749 case 's': /* String read via minibuffer without
750 inheriting the current input method. */
751 args[i] = Fread_string (callint_message,
752 Qnil, Qnil, Qnil, Qnil);
753 break;
754
755 case 'S': /* Any symbol. */
756 visargs[i] = Fread_string (callint_message,
757 Qnil, Qnil, Qnil, Qnil);
758 /* Passing args[i] directly stimulates compiler bug. */
759 teml = visargs[i];
760 args[i] = Fintern (teml, Qnil);
761 break;
762
763 case 'v': /* Variable name: symbol that is
764 custom-variable-p. */
765 args[i] = Fread_variable (callint_message, Qnil);
766 visargs[i] = last_minibuf_string;
767 break;
768
769 case 'x': /* Lisp expression read but not evaluated. */
770 args[i] = call1 (intern ("read-minibuffer"), callint_message);
771 visargs[i] = last_minibuf_string;
772 break;
773
774 case 'X': /* Lisp expression read and evaluated. */
775 args[i] = call1 (intern ("eval-minibuffer"), callint_message);
776 visargs[i] = last_minibuf_string;
777 break;
778
779 case 'Z': /* Coding-system symbol, or ignore the
780 argument if no prefix. */
781 if (NILP (prefix_arg))
782 {
783 args[i] = Qnil;
784 varies[i] = -1;
785 }
786 else
787 {
788 args[i]
789 = Fread_non_nil_coding_system (callint_message);
790 visargs[i] = last_minibuf_string;
791 }
792 break;
793
794 case 'z': /* Coding-system symbol or nil. */
795 args[i] = Fread_coding_system (callint_message, Qnil);
796 visargs[i] = last_minibuf_string;
797 break;
798
799 /* We have a case for `+' so we get an error
800 if anyone tries to define one here. */
801 case '+':
802 default:
803 error ("Invalid control letter `%c' (#o%03o, #x%04x) in interactive calling string",
804 STRING_CHAR ((unsigned char *) tem),
805 (unsigned) STRING_CHAR ((unsigned char *) tem),
806 (unsigned) STRING_CHAR ((unsigned char *) tem));
807 }
808
809 if (varies[i] == 0)
810 arg_from_tty = 1;
811
812 if (NILP (visargs[i]) && STRINGP (args[i]))
813 visargs[i] = args[i];
814
815 tem = strchr (tem, '\n');
816 if (tem) tem++;
817 else tem = "";
818 }
819 unbind_to (speccount, Qnil);
820
821 QUIT;
822
823 args[0] = Qfuncall_interactively;
824 args[1] = function;
825
826 if (arg_from_tty || !NILP (record_flag))
827 {
828 /* We don't need `visargs' any more, so let's recycle it since we need
829 an array of just the same size. */
830 visargs[1] = function;
831 for (i = 2; i < nargs; i++)
832 {
833 if (varies[i] > 0)
834 visargs[i] = list1 (intern (callint_argfuns[varies[i]]));
835 else
836 visargs[i] = quotify_arg (args[i]);
837 }
838 Vcommand_history = Fcons (Flist (nargs - 1, visargs + 1),
839 Vcommand_history);
840 /* Don't keep command history around forever. */
841 if (INTEGERP (Vhistory_length) && XINT (Vhistory_length) > 0)
842 {
843 teml = Fnthcdr (Vhistory_length, Vcommand_history);
844 if (CONSP (teml))
845 XSETCDR (teml, Qnil);
846 }
847 }
848
849 /* If we used a marker to hold point, mark, or an end of the region,
850 temporarily, convert it to an integer now. */
851 for (i = 2; i < nargs; i++)
852 if (varies[i] >= 1 && varies[i] <= 4)
853 XSETINT (args[i], marker_position (args[i]));
854
855 if (record_then_fail)
856 Fbarf_if_buffer_read_only (Qnil);
857
858 Vthis_command = save_this_command;
859 Vthis_original_command = save_this_original_command;
860 Vreal_this_command = save_real_this_command;
861 kset_last_command (current_kboard, save_last_command);
862
863 {
864 Lisp_Object val = Ffuncall (nargs, args);
865 UNGCPRO;
866 val = unbind_to (speccount, val);
867 SAFE_FREE ();
868 return val;
869 }
870 }
871
872 DEFUN ("prefix-numeric-value", Fprefix_numeric_value, Sprefix_numeric_value,
873 1, 1, 0,
874 doc: /* Return numeric meaning of raw prefix argument RAW.
875 A raw prefix argument is what you get from `(interactive "P")'.
876 Its numeric meaning is what you would get from `(interactive "p")'. */)
877 (Lisp_Object raw)
878 {
879 Lisp_Object val;
880
881 if (NILP (raw))
882 XSETFASTINT (val, 1);
883 else if (EQ (raw, Qminus))
884 XSETINT (val, -1);
885 else if (CONSP (raw) && INTEGERP (XCAR (raw)))
886 XSETINT (val, XINT (XCAR (raw)));
887 else if (INTEGERP (raw))
888 val = raw;
889 else
890 XSETFASTINT (val, 1);
891
892 return val;
893 }
894
895 void
896 syms_of_callint (void)
897 {
898 point_marker = Fmake_marker ();
899 staticpro (&point_marker);
900
901 callint_message = Qnil;
902 staticpro (&callint_message);
903
904 preserved_fns = listn (CONSTYPE_PURE, 4,
905 intern_c_string ("region-beginning"),
906 intern_c_string ("region-end"),
907 intern_c_string ("point"),
908 intern_c_string ("mark"));
909
910 DEFSYM (Qlist, "list");
911 DEFSYM (Qlet, "let");
912 DEFSYM (Qif, "if");
913 DEFSYM (Qwhen, "when");
914 DEFSYM (Qletx, "let*");
915 DEFSYM (Qsave_excursion, "save-excursion");
916 DEFSYM (Qprogn, "progn");
917 DEFSYM (Qminus, "-");
918 DEFSYM (Qplus, "+");
919 DEFSYM (Qhandle_shift_selection, "handle-shift-selection");
920 DEFSYM (Qread_number, "read-number");
921 DEFSYM (Qfuncall_interactively, "funcall-interactively");
922 DEFSYM (Qcommand_debug_status, "command-debug-status");
923 DEFSYM (Qenable_recursive_minibuffers, "enable-recursive-minibuffers");
924 DEFSYM (Qmouse_leave_buffer_hook, "mouse-leave-buffer-hook");
925
926 DEFVAR_KBOARD ("prefix-arg", Vprefix_arg,
927 doc: /* The value of the prefix argument for the next 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
932 You cannot examine this variable to find the argument for this command
933 since it has been set to nil by the time you can look.
934 Instead, you should use the variable `current-prefix-arg', although
935 normally commands can get this prefix argument with (interactive "P"). */);
936
937 DEFVAR_KBOARD ("last-prefix-arg", Vlast_prefix_arg,
938 doc: /* The value of the prefix argument for the previous editing command.
939 See `prefix-arg' for the meaning of the value. */);
940
941 DEFVAR_LISP ("current-prefix-arg", Vcurrent_prefix_arg,
942 doc: /* The value of the prefix argument for this editing command.
943 It may be a number, or the symbol `-' for just a minus sign as arg,
944 or a list whose car is a number for just one or more C-u's
945 or nil if no argument has been specified.
946 This is what `(interactive \"P\")' returns. */);
947 Vcurrent_prefix_arg = Qnil;
948
949 DEFVAR_LISP ("command-history", Vcommand_history,
950 doc: /* List of recent commands that read arguments from terminal.
951 Each command is represented as a form to evaluate.
952
953 Maximum length of the history list is determined by the value
954 of `history-length', which see. */);
955 Vcommand_history = Qnil;
956
957 DEFVAR_LISP ("command-debug-status", Vcommand_debug_status,
958 doc: /* Debugging status of current interactive command.
959 Bound each time `call-interactively' is called;
960 may be set by the debugger as a reminder for itself. */);
961 Vcommand_debug_status = Qnil;
962
963 DEFVAR_LISP ("mark-even-if-inactive", Vmark_even_if_inactive,
964 doc: /* Non-nil means you can use the mark even when inactive.
965 This option makes a difference in Transient Mark mode.
966 When the option is non-nil, deactivation of the mark
967 turns off region highlighting, but commands that use the mark
968 behave as if the mark were still active. */);
969 Vmark_even_if_inactive = Qt;
970
971 DEFVAR_LISP ("mouse-leave-buffer-hook", Vmouse_leave_buffer_hook,
972 doc: /* Hook to run when about to switch windows with a mouse command.
973 Its purpose is to give temporary modes such as Isearch mode
974 a way to turn themselves off when a mouse command switches windows. */);
975 Vmouse_leave_buffer_hook = Qnil;
976
977 defsubr (&Sinteractive);
978 defsubr (&Scall_interactively);
979 defsubr (&Sfuncall_interactively);
980 defsubr (&Sprefix_numeric_value);
981 }