* make-dist: Include lib-src/makedoc.com and emacs.csh in the
[bpt/emacs.git] / src / callint.c
CommitLineData
ec28a64d 1/* Call a Lisp function interactively.
46947372 2 Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
ec28a64d
MB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include "config.h"
22#include "lisp.h"
23#include "buffer.h"
24#include "commands.h"
760cbdd3 25#include "keyboard.h"
ec28a64d
MB
26#include "window.h"
27#include "mocklisp.h"
28
29extern char *index ();
30
31Lisp_Object Vprefix_arg, Vcurrent_prefix_arg, Qminus;
32Lisp_Object Qcall_interactively;
33Lisp_Object Vcommand_history;
34
35Lisp_Object Vcommand_debug_status, Qcommand_debug_status;
52614803 36Lisp_Object Qenable_recursive_minibuffers;
ec28a64d
MB
37
38/* This comment supplies the doc string for interactive,
39 for make-docfile to see. We cannot put this in the real DEFUN
40 due to limits in the Unix cpp.
41
42DEFUN ("interactive", Ffoo, Sfoo, 0, 0, 0,
43 "Specify a way of parsing arguments for interactive use of a function.\n\
44For example, write\n\
45 (defun foo (arg) \"Doc string\" (interactive \"p\") ...use arg...)\n\
46to make ARG be the prefix argument when `foo' is called as a command.\n\
47The \"call\" to `interactive' is actually a declaration rather than a function;\n\
48 it tells `call-interactively' how to read arguments\n\
49 to pass to the function.\n\
50When actually called, `interactive' just returns nil.\n\
51\n\
52The argument of `interactive' is usually a string containing a code letter\n\
53 followed by a prompt. (Some code letters do not use I/O to get\n\
54 the argument and do not need prompts.) To prompt for multiple arguments,\n\
55 give a code letter, its prompt, a newline, and another code letter, etc.\n\
56 Prompts are passed to format, and may use % escapes to print the\n\
57 arguments that have already been read.\n\
58If the argument is not a string, it is evaluated to get a list of\n\
59 arguments to pass to the function.\n\
60Just `(interactive)' means pass no args when calling interactively.\n\
61\nCode letters available are:\n\
62a -- Function name: symbol with a function definition.\n\
63b -- Name of existing buffer.\n\
64B -- Name of buffer, possibly nonexistent.\n\
65c -- Character.\n\
66C -- Command name: symbol with interactive function definition.\n\
67d -- Value of point as number. Does not do I/O.\n\
68D -- Directory name.\n\
bc78232c
JB
69e -- Event that invoked this command (value of `last-nonmenu-event').\n\
70 This skips events without parameters.\n\
71 If used more than once, the Nth 'e' returns the Nth parameterized event.\n\
ec28a64d
MB
72f -- Existing file name.\n\
73F -- Possibly nonexistent file name.\n\
74k -- Key sequence (string).\n\
75m -- Value of mark as number. Does not do I/O.\n\
76n -- Number read using minibuffer.\n\
77N -- Prefix arg converted to number, or if none, do like code `n'.\n\
78p -- Prefix arg converted to number. Does not do I/O.\n\
79P -- Prefix arg in raw form. Does not do I/O.\n\
80r -- Region: point and mark as 2 numeric args, smallest first. Does no I/O.\n\
81s -- Any string.\n\
82S -- Any symbol.\n\
83v -- Variable name: symbol that is user-variable-p.\n\
84x -- Lisp expression read but not evaluated.\n\
85X -- Lisp expression read and evaluated.\n\
86In addition, if the string begins with `*'\n\
87 then an error is signaled if the buffer is read-only.\n\
88 This happens before reading any arguments.\n\
89If the string begins with `@', then the window the mouse is over is selected\n\
90 before anything else is done. You may use both `@' and `*';\n\
91they are processed in the order that they appear."
92*/
93
94/* ARGSUSED */
95DEFUN ("interactive", Finteractive, Sinteractive, 0, UNEVALLED, 0,
96 0 /* See immediately above */)
97 (args)
98 Lisp_Object args;
99{
100 return Qnil;
101}
102
103/* Quotify EXP: if EXP is constant, return it.
104 If EXP is not constant, return (quote EXP). */
105Lisp_Object
106quotify_arg (exp)
107 register Lisp_Object exp;
108{
109 if (XTYPE (exp) != Lisp_Int && XTYPE (exp) != Lisp_String
265a9e55 110 && !NILP (exp) && !EQ (exp, Qt))
ec28a64d
MB
111 return Fcons (Qquote, Fcons (exp, Qnil));
112
113 return exp;
114}
115
116/* Modify EXP by quotifying each element (except the first). */
117Lisp_Object
118quotify_args (exp)
119 Lisp_Object exp;
120{
121 register Lisp_Object tail;
122 register struct Lisp_Cons *ptr;
123 for (tail = exp; CONSP (tail); tail = ptr->cdr)
124 {
125 ptr = XCONS (tail);
126 ptr->car = quotify_arg (ptr->car);
127 }
128 return exp;
129}
130
131char *callint_argfuns[]
132 = {"", "point", "mark", "region-beginning", "region-end"};
133
134static void
135check_mark ()
136{
137 Lisp_Object tem = Fmarker_buffer (current_buffer->mark);
265a9e55 138 if (NILP (tem) || (XBUFFER (tem) != current_buffer))
ec28a64d
MB
139 error ("The mark is not set now");
140}
141
142
143DEFUN ("call-interactively", Fcall_interactively, Scall_interactively, 1, 2, 0,
144 "Call FUNCTION, reading args according to its interactive calling specs.\n\
145The function contains a specification of how to do the argument reading.\n\
146In the case of user-defined functions, this is specified by placing a call\n\
147to the function `interactive' at the top level of the function body.\n\
148See `interactive'.\n\
149\n\
150Optional second arg RECORD-FLAG non-nil\n\
151means unconditionally put this command in the command-history.\n\
152Otherwise, this is done only if an arg is read using the minibuffer.")
153 (function, record)
154 Lisp_Object function, record;
155{
156 Lisp_Object *args, *visargs;
157 unsigned char **argstrings;
158 Lisp_Object fun;
159 Lisp_Object funcar;
160 Lisp_Object specs;
161 Lisp_Object teml;
52614803
RS
162 Lisp_Object enable;
163 int speccount = specpdl_ptr - specpdl;
ec28a64d 164
bc78232c
JB
165 /* The index of the next element of this_command_keys to examine for
166 the 'e' interactive code. */
167 int next_event = 0;
168
ec28a64d
MB
169 Lisp_Object prefix_arg;
170 unsigned char *string;
171 unsigned char *tem;
63007de2
JB
172
173 /* If varies[i] > 0, the i'th argument shouldn't just have its value
174 in this call quoted in the command history. It should be
175 recorded as a call to the function named callint_argfuns[varies[i]]. */
ec28a64d 176 int *varies;
63007de2 177
ec28a64d
MB
178 register int i, j;
179 int count, foo;
180 char prompt[100];
181 char prompt1[100];
182 char *tem1;
183 int arg_from_tty = 0;
184 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
185
e5d77022 186 /* Save this now, since use of minibuffer will clobber it. */
ec28a64d
MB
187 prefix_arg = Vcurrent_prefix_arg;
188
46947372 189 retry:
ec28a64d 190
afa4c0f3
JB
191 if (XTYPE (function) == Lisp_Symbol)
192 enable = Fget (function, Qenable_recursive_minibuffers);
52614803 193
ffd56f97 194 fun = indirect_function (function);
ec28a64d
MB
195
196 specs = Qnil;
197 string = 0;
198
199 /* Decode the kind of function. Either handle it and return,
200 or go to `lose' if not interactive, or go to `retry'
201 to specify a different function, or set either STRING or SPECS. */
202
203 if (XTYPE (fun) == Lisp_Subr)
204 {
205 string = (unsigned char *) XSUBR (fun)->prompt;
206 if (!string)
207 {
208 lose:
209 function = wrong_type_argument (Qcommandp, function, 0);
210 goto retry;
211 }
212 if ((int) string == 1)
213 /* Let SPECS (which is nil) be used as the args. */
214 string = 0;
215 }
216 else if (XTYPE (fun) == Lisp_Compiled)
217 {
218 if (XVECTOR (fun)->size <= COMPILED_INTERACTIVE)
219 goto lose;
220 specs = XVECTOR (fun)->contents[COMPILED_INTERACTIVE];
221 }
222 else if (!CONSP (fun))
223 goto lose;
224 else if (funcar = Fcar (fun), EQ (funcar, Qautoload))
225 {
226 GCPRO2 (function, prefix_arg);
227 do_autoload (fun, function);
228 UNGCPRO;
229 goto retry;
230 }
231 else if (EQ (funcar, Qlambda))
232 {
233 specs = Fassq (Qinteractive, Fcdr (Fcdr (fun)));
265a9e55 234 if (NILP (specs))
ec28a64d
MB
235 goto lose;
236 specs = Fcar (Fcdr (specs));
237 }
238 else if (EQ (funcar, Qmocklisp))
239 return ml_apply (fun, Qinteractive);
240 else
241 goto lose;
242
46947372 243 /* If either specs or string is set to a string, use it. */
ec28a64d 244 if (XTYPE (specs) == Lisp_String)
46947372
JB
245 {
246 /* Make a copy of string so that if a GC relocates specs,
247 `string' will still be valid. */
e5d77022 248 string = (unsigned char *) alloca (XSTRING (specs)->size + 1);
46947372
JB
249 bcopy (XSTRING (specs)->data, string, XSTRING (specs)->size + 1);
250 }
ec28a64d
MB
251 else if (string == 0)
252 {
253 i = num_input_chars;
254 specs = Feval (specs);
265a9e55 255 if (i != num_input_chars || !NILP (record))
ec28a64d
MB
256 Vcommand_history
257 = Fcons (Fcons (function, quotify_args (Fcopy_sequence (specs))),
258 Vcommand_history);
259 return apply1 (function, specs);
260 }
261
262 /* Here if function specifies a string to control parsing the defaults */
263
264 /* Handle special starting chars `*' and `@'. */
265 while (1)
266 {
267 if (*string == '*')
268 {
269 string++;
265a9e55 270 if (!NILP (current_buffer->read_only))
ec28a64d
MB
271 Fbarf_if_buffer_read_only ();
272 }
273 else if (*string == '@')
274 {
275 string++;
265a9e55 276 if (!NILP (Vmouse_window))
ec28a64d
MB
277 Fselect_window (Vmouse_window);
278 }
279 else break;
280 }
281
282 /* Count the number of arguments the interactive spec would have
283 us give to the function. */
284 tem = string;
285 for (j = 0; *tem; j++)
286 {
287 /* 'r' specifications ("point and mark as 2 numeric args")
288 produce *two* arguments. */
289 if (*tem == 'r') j++;
290 tem = (unsigned char *) index (tem, '\n');
291 if (tem)
292 tem++;
293 else
294 tem = (unsigned char *) "";
295 }
296 count = j;
297
298 args = (Lisp_Object *) alloca ((count + 1) * sizeof (Lisp_Object));
299 visargs = (Lisp_Object *) alloca ((count + 1) * sizeof (Lisp_Object));
300 argstrings = (unsigned char **) alloca ((count + 1) * sizeof (char *));
301 varies = (int *) alloca ((count + 1) * sizeof (int));
302
303 for (i = 0; i < (count + 1); i++)
304 {
305 args[i] = Qnil;
306 visargs[i] = Qnil;
307 varies[i] = 0;
308 }
309
310 GCPRO4 (prefix_arg, function, *args, *visargs);
311 gcpro3.nvars = (count + 1);
312 gcpro4.nvars = (count + 1);
313
52614803
RS
314 if (!NILP (enable))
315 specbind (Qenable_recursive_minibuffers, Qt);
316
ec28a64d 317 tem = string;
46947372 318 for (i = 1; *tem; i++)
ec28a64d
MB
319 {
320 strncpy (prompt1, tem + 1, sizeof prompt1 - 1);
321 prompt1[sizeof prompt1 - 1] = 0;
322 tem1 = index (prompt1, '\n');
323 if (tem1) *tem1 = 0;
324 /* Fill argstrings with a vector of C strings
325 corresponding to the Lisp strings in visargs. */
326 for (j = 1; j < i; j++)
327 argstrings[j]
328 = EQ (visargs[j], Qnil)
329 ? (unsigned char *) ""
46947372 330 : XSTRING (visargs[j])->data;
ec28a64d
MB
331
332 doprnt (prompt, sizeof prompt, prompt1, 0, j - 1, argstrings + 1);
333
334 switch (*tem)
335 {
336 case 'a': /* Symbol defined as a function */
337 visargs[i] = Fcompleting_read (build_string (prompt),
338 Vobarray, Qfboundp, Qt, Qnil, Qnil);
339 /* Passing args[i] directly stimulates compiler bug */
340 teml = visargs[i];
341 args[i] = Fintern (teml, Qnil);
342 break;
343
344 case 'b': /* Name of existing buffer */
345 args[i] = Fcurrent_buffer ();
346 if (EQ (selected_window, minibuf_window))
9262fcb6 347 args[i] = Fother_buffer (args[i], Qnil);
ec28a64d
MB
348 args[i] = Fread_buffer (build_string (prompt), args[i], Qt);
349 break;
350
351 case 'B': /* Name of buffer, possibly nonexistent */
352 args[i] = Fread_buffer (build_string (prompt),
9262fcb6
RS
353 Fother_buffer (Fcurrent_buffer (), Qnil),
354 Qnil);
ec28a64d
MB
355 break;
356
357 case 'c': /* Character */
358 message1 (prompt);
359 args[i] = Fread_char ();
360 /* Passing args[i] directly stimulates compiler bug */
361 teml = args[i];
362 visargs[i] = Fchar_to_string (teml);
363 break;
364
365 case 'C': /* Command: symbol with interactive function */
366 visargs[i] = Fcompleting_read (build_string (prompt),
367 Vobarray, Qcommandp, Qt, Qnil, Qnil);
368 /* Passing args[i] directly stimulates compiler bug */
369 teml = visargs[i];
370 args[i] = Fintern (teml, Qnil);
371 break;
372
373 case 'd': /* Value of point. Does not do I/O. */
374 XFASTINT (args[i]) = point;
375 /* visargs[i] = Qnil; */
376 varies[i] = 1;
377 break;
378
ec28a64d
MB
379 case 'D': /* Directory name. */
380 args[i] = Fread_file_name (build_string (prompt), Qnil,
381 current_buffer->directory, Qlambda, Qnil);
382 break;
383
384 case 'f': /* Existing file name. */
385 args[i] = Fread_file_name (build_string (prompt),
386 Qnil, Qnil, Qlambda, Qnil);
387 break;
388
389 case 'F': /* Possibly nonexistent file name. */
390 args[i] = Fread_file_name (build_string (prompt),
391 Qnil, Qnil, Qnil, Qnil);
392 break;
393
394 case 'k': /* Key sequence (string) */
63007de2 395 args[i] = Fread_key_sequence (build_string (prompt), Qnil);
ec28a64d
MB
396 teml = args[i];
397 visargs[i] = Fkey_description (teml);
398 break;
399
bc78232c
JB
400 case 'e': /* The invoking event. */
401 /* Find the next parameterized event. */
402 while (next_event < this_command_key_count
403 && ! EVENT_HAS_PARAMETERS (this_command_keys[next_event]))
404 next_event++;
405 if (next_event >= this_command_key_count)
406 error ("%s must be bound to an event with parameters",
63007de2
JB
407 (XTYPE (function) == Lisp_Symbol
408 ? (char *) XSYMBOL (function)->name->data
bc78232c
JB
409 : "command"));
410 args[i] = this_command_keys[next_event++];
e5d77022 411 varies[i] = -1;
63007de2
JB
412 break;
413
ec28a64d
MB
414 case 'm': /* Value of mark. Does not do I/O. */
415 check_mark ();
416 /* visargs[i] = Qnil; */
417 XFASTINT (args[i]) = marker_position (current_buffer->mark);
418 varies[i] = 2;
419 break;
420
421 case 'N': /* Prefix arg, else number from minibuffer */
265a9e55 422 if (!NILP (prefix_arg))
ec28a64d
MB
423 goto have_prefix_arg;
424 case 'n': /* Read number from minibuffer. */
425 do
426 args[i] = Fread_minibuffer (build_string (prompt), Qnil);
4746118a 427 while (! NUMBERP (args[i]));
ec28a64d
MB
428 visargs[i] = last_minibuf_string;
429 break;
430
431 case 'P': /* Prefix arg in raw form. Does no I/O. */
432 have_prefix_arg:
433 args[i] = prefix_arg;
434 /* visargs[i] = Qnil; */
435 varies[i] = -1;
436 break;
437
438 case 'p': /* Prefix arg converted to number. No I/O. */
439 args[i] = Fprefix_numeric_value (prefix_arg);
440 /* visargs[i] = Qnil; */
441 varies[i] = -1;
442 break;
443
444 case 'r': /* Region, point and mark as 2 args. */
445 check_mark ();
446 /* visargs[i+1] = Qnil; */
447 foo = marker_position (current_buffer->mark);
448 /* visargs[i] = Qnil; */
449 XFASTINT (args[i]) = point < foo ? point : foo;
450 varies[i] = 3;
451 XFASTINT (args[++i]) = point > foo ? point : foo;
452 varies[i] = 4;
453 break;
454
455 case 's': /* String read via minibuffer. */
456 args[i] = Fread_string (build_string (prompt), Qnil);
457 break;
458
459 case 'S': /* Any symbol. */
15c65264 460 visargs[i] = Fread_no_blanks_input (build_string (prompt), Qnil);
ec28a64d
MB
461 /* Passing args[i] directly stimulates compiler bug */
462 teml = visargs[i];
463 args[i] = Fintern (teml, Qnil);
464 break;
465
466 case 'v': /* Variable name: symbol that is
467 user-variable-p. */
468 args[i] = Fread_variable (build_string (prompt));
469 visargs[i] = last_minibuf_string;
470 break;
471
472 case 'x': /* Lisp expression read but not evaluated */
473 args[i] = Fread_minibuffer (build_string (prompt), Qnil);
474 visargs[i] = last_minibuf_string;
475 break;
476
477 case 'X': /* Lisp expression read and evaluated */
478 args[i] = Feval_minibuffer (build_string (prompt), Qnil);
479 visargs[i] = last_minibuf_string;
480 break;
481
482 default:
483 error ("Invalid control letter \"%c\" (%03o) in interactive calling string",
484 *tem, *tem);
485 }
486
487 if (varies[i] == 0)
488 arg_from_tty = 1;
489
265a9e55 490 if (NILP (visargs[i]) && XTYPE (args[i]) == Lisp_String)
ec28a64d
MB
491 visargs[i] = args[i];
492
493 tem = (unsigned char *) index (tem, '\n');
494 if (tem) tem++;
495 else tem = (unsigned char *) "";
496 }
52614803 497 unbind_to (speccount, Qnil);
ec28a64d
MB
498
499 QUIT;
500
501 args[0] = function;
502
265a9e55 503 if (arg_from_tty || !NILP (record))
ec28a64d
MB
504 {
505 visargs[0] = function;
63007de2
JB
506 for (i = 1; i < count + 1; i++)
507 if (varies[i] > 0)
ec28a64d
MB
508 visargs[i] = Fcons (intern (callint_argfuns[varies[i]]), Qnil);
509 else
510 visargs[i] = quotify_arg (args[i]);
511 Vcommand_history = Fcons (Flist (count + 1, visargs),
512 Vcommand_history);
513 }
514
515 {
516 Lisp_Object val;
ec28a64d
MB
517 specbind (Qcommand_debug_status, Qnil);
518
519 val = Ffuncall (count + 1, args);
520 UNGCPRO;
521 return unbind_to (speccount, val);
522 }
523}
524
525DEFUN ("prefix-numeric-value", Fprefix_numeric_value, Sprefix_numeric_value,
526 1, 1, 0,
527 "Return numeric meaning of raw prefix argument ARG.\n\
528A raw prefix argument is what you get from `(interactive \"P\")'.\n\
529Its numeric meaning is what you would get from `(interactive \"p\")'.")
530 (raw)
531 Lisp_Object raw;
532{
533 Lisp_Object val;
534
535 /* Tag val as an integer, so the rest of the assignments
536 may use XSETINT. */
537 XFASTINT (val) = 0;
538
265a9e55 539 if (NILP (raw))
ec28a64d 540 XFASTINT (val) = 1;
fd5285f3 541 else if (EQ (raw, Qminus))
ec28a64d
MB
542 XSETINT (val, -1);
543 else if (CONSP (raw))
544 XSETINT (val, XINT (XCONS (raw)->car));
545 else if (XTYPE (raw) == Lisp_Int)
546 val = raw;
547 else
548 XFASTINT (val) = 1;
549
550 return val;
551}
552
553syms_of_callint ()
554{
555 Qminus = intern ("-");
556 staticpro (&Qminus);
557
558 Qcall_interactively = intern ("call-interactively");
559 staticpro (&Qcall_interactively);
560
561 Qcommand_debug_status = intern ("command-debug-status");
562 staticpro (&Qcommand_debug_status);
563
52614803
RS
564 Qenable_recursive_minibuffers = intern ("enable-recursive-minibuffers");
565 staticpro (&Qenable_recursive_minibuffers);
566
ec28a64d
MB
567 DEFVAR_LISP ("prefix-arg", &Vprefix_arg,
568 "The value of the prefix argument for the next editing command.\n\
569It may be a number, or the symbol `-' for just a minus sign as arg,\n\
570or a list whose car is a number for just one or more C-U's\n\
571or nil if no argument has been specified.\n\
572\n\
573You cannot examine this variable to find the argument for this command\n\
574since it has been set to nil by the time you can look.\n\
575Instead, you should use the variable `current-prefix-arg', although\n\
576normally commands can get this prefix argument with (interactive \"P\").");
577 Vprefix_arg = Qnil;
578
579 DEFVAR_LISP ("current-prefix-arg", &Vcurrent_prefix_arg,
580 "The value of the prefix argument for this editing command.\n\
581It may be a number, or the symbol `-' for just a minus sign as arg,\n\
582or a list whose car is a number for just one or more C-U's\n\
583or nil if no argument has been specified.\n\
584This is what `(interactive \"P\")' returns.");
585 Vcurrent_prefix_arg = Qnil;
586
587 DEFVAR_LISP ("command-history", &Vcommand_history,
588 "List of recent commands that read arguments from terminal.\n\
589Each command is represented as a form to evaluate.");
590 Vcommand_history = Qnil;
591
592 DEFVAR_LISP ("command-debug-status", &Vcommand_debug_status,
593 "Debugging status of current interactive command.\n\
594Bound each time `call-interactively' is called;\n\
595may be set by the debugger as a reminder for itself.");
596 Vcommand_debug_status = Qnil;
597
598 defsubr (&Sinteractive);
599 defsubr (&Scall_interactively);
600 defsubr (&Sprefix_numeric_value);
601}