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