]> code.delx.au - gnu-emacs/blob - src/eval.c
(Fbyte_code): Harmonize arguments with documentation.
[gnu-emacs] / src / eval.c
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include <config.h>
22 #include "lisp.h"
23 #include "blockinput.h"
24
25 #ifndef standalone
26 #include "commands.h"
27 #include "keyboard.h"
28 #else
29 #define INTERACTIVE 1
30 #endif
31
32 #include <setjmp.h>
33
34 /* This definition is duplicated in alloc.c and keyboard.c */
35 /* Putting it in lisp.h makes cc bomb out! */
36
37 struct backtrace
38 {
39 struct backtrace *next;
40 Lisp_Object *function;
41 Lisp_Object *args; /* Points to vector of args. */
42 int nargs; /* Length of vector.
43 If nargs is UNEVALLED, args points to slot holding
44 list of unevalled args */
45 char evalargs;
46 /* Nonzero means call value of debugger when done with this operation. */
47 char debug_on_exit;
48 };
49
50 struct backtrace *backtrace_list;
51
52 /* This structure helps implement the `catch' and `throw' control
53 structure. A struct catchtag contains all the information needed
54 to restore the state of the interpreter after a non-local jump.
55
56 Handlers for error conditions (represented by `struct handler'
57 structures) just point to a catch tag to do the cleanup required
58 for their jumps.
59
60 catchtag structures are chained together in the C calling stack;
61 the `next' member points to the next outer catchtag.
62
63 A call like (throw TAG VAL) searches for a catchtag whose `tag'
64 member is TAG, and then unbinds to it. The `val' member is used to
65 hold VAL while the stack is unwound; `val' is returned as the value
66 of the catch form.
67
68 All the other members are concerned with restoring the interpreter
69 state. */
70 struct catchtag
71 {
72 Lisp_Object tag;
73 Lisp_Object val;
74 struct catchtag *next;
75 struct gcpro *gcpro;
76 jmp_buf jmp;
77 struct backtrace *backlist;
78 struct handler *handlerlist;
79 int lisp_eval_depth;
80 int pdlcount;
81 int poll_suppress_count;
82 };
83
84 struct catchtag *catchlist;
85
86 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
87 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
88 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
89 Lisp_Object Qand_rest, Qand_optional;
90 Lisp_Object Qdebug_on_error;
91
92 Lisp_Object Vrun_hooks;
93
94 /* Non-nil means record all fset's and provide's, to be undone
95 if the file being autoloaded is not fully loaded.
96 They are recorded by being consed onto the front of Vautoload_queue:
97 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
98
99 Lisp_Object Vautoload_queue;
100
101 /* Current number of specbindings allocated in specpdl. */
102 int specpdl_size;
103
104 /* Pointer to beginning of specpdl. */
105 struct specbinding *specpdl;
106
107 /* Pointer to first unused element in specpdl. */
108 struct specbinding *specpdl_ptr;
109
110 /* Maximum size allowed for specpdl allocation */
111 int max_specpdl_size;
112
113 /* Depth in Lisp evaluations and function calls. */
114 int lisp_eval_depth;
115
116 /* Maximum allowed depth in Lisp evaluations and function calls. */
117 int max_lisp_eval_depth;
118
119 /* Nonzero means enter debugger before next function call */
120 int debug_on_next_call;
121
122 /* List of conditions (non-nil atom means all) which cause a backtrace
123 if an error is handled by the command loop's error handler. */
124 Lisp_Object Vstack_trace_on_error;
125
126 /* List of conditions (non-nil atom means all) which enter the debugger
127 if an error is handled by the command loop's error handler. */
128 Lisp_Object Vdebug_on_error;
129
130 /* List of conditions and regexps specifying error messages which
131 do not enter the debugger even if Vdebug_on_errors says they should. */
132 Lisp_Object Vdebug_ignored_errors;
133
134 /* Nonzero means enter debugger if a quit signal
135 is handled by the command loop's error handler. */
136 int debug_on_quit;
137
138 /* The value of num_nonmacro_input_chars as of the last time we
139 started to enter the debugger. If we decide to enter the debugger
140 again when this is still equal to num_nonmacro_input_chars, then we
141 know that the debugger itself has an error, and we should just
142 signal the error instead of entering an infinite loop of debugger
143 invocations. */
144 int when_entered_debugger;
145
146 Lisp_Object Vdebugger;
147
148 void specbind (), record_unwind_protect ();
149
150 Lisp_Object run_hook_with_args ();
151
152 Lisp_Object funcall_lambda ();
153 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
154
155 init_eval_once ()
156 {
157 specpdl_size = 50;
158 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
159 max_specpdl_size = 600;
160 max_lisp_eval_depth = 200;
161
162 Vrun_hooks = Qnil;
163 }
164
165 init_eval ()
166 {
167 specpdl_ptr = specpdl;
168 catchlist = 0;
169 handlerlist = 0;
170 backtrace_list = 0;
171 Vquit_flag = Qnil;
172 debug_on_next_call = 0;
173 lisp_eval_depth = 0;
174 /* This is less than the initial value of num_nonmacro_input_chars. */
175 when_entered_debugger = -1;
176 }
177
178 Lisp_Object
179 call_debugger (arg)
180 Lisp_Object arg;
181 {
182 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
183 max_lisp_eval_depth = lisp_eval_depth + 20;
184 if (specpdl_size + 40 > max_specpdl_size)
185 max_specpdl_size = specpdl_size + 40;
186 debug_on_next_call = 0;
187 when_entered_debugger = num_nonmacro_input_chars;
188 return apply1 (Vdebugger, arg);
189 }
190
191 do_debug_on_call (code)
192 Lisp_Object code;
193 {
194 debug_on_next_call = 0;
195 backtrace_list->debug_on_exit = 1;
196 call_debugger (Fcons (code, Qnil));
197 }
198 \f
199 /* NOTE!!! Every function that can call EVAL must protect its args
200 and temporaries from garbage collection while it needs them.
201 The definition of `For' shows what you have to do. */
202
203 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
204 "Eval args until one of them yields non-nil, then return that value.\n\
205 The remaining args are not evalled at all.\n\
206 If all args return nil, return nil.")
207 (args)
208 Lisp_Object args;
209 {
210 register Lisp_Object val;
211 Lisp_Object args_left;
212 struct gcpro gcpro1;
213
214 if (NILP(args))
215 return Qnil;
216
217 args_left = args;
218 GCPRO1 (args_left);
219
220 do
221 {
222 val = Feval (Fcar (args_left));
223 if (!NILP (val))
224 break;
225 args_left = Fcdr (args_left);
226 }
227 while (!NILP(args_left));
228
229 UNGCPRO;
230 return val;
231 }
232
233 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
234 "Eval args until one of them yields nil, then return nil.\n\
235 The remaining args are not evalled at all.\n\
236 If no arg yields nil, return the last arg's value.")
237 (args)
238 Lisp_Object args;
239 {
240 register Lisp_Object val;
241 Lisp_Object args_left;
242 struct gcpro gcpro1;
243
244 if (NILP(args))
245 return Qt;
246
247 args_left = args;
248 GCPRO1 (args_left);
249
250 do
251 {
252 val = Feval (Fcar (args_left));
253 if (NILP (val))
254 break;
255 args_left = Fcdr (args_left);
256 }
257 while (!NILP(args_left));
258
259 UNGCPRO;
260 return val;
261 }
262
263 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
264 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
265 Returns the value of THEN or the value of the last of the ELSE's.\n\
266 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
267 If COND yields nil, and there are no ELSE's, the value is nil.")
268 (args)
269 Lisp_Object args;
270 {
271 register Lisp_Object cond;
272 struct gcpro gcpro1;
273
274 GCPRO1 (args);
275 cond = Feval (Fcar (args));
276 UNGCPRO;
277
278 if (!NILP (cond))
279 return Feval (Fcar (Fcdr (args)));
280 return Fprogn (Fcdr (Fcdr (args)));
281 }
282
283 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
284 "(cond CLAUSES...): try each clause until one succeeds.\n\
285 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
286 and, if the value is non-nil, this clause succeeds:\n\
287 then the expressions in BODY are evaluated and the last one's\n\
288 value is the value of the cond-form.\n\
289 If no clause succeeds, cond returns nil.\n\
290 If a clause has one element, as in (CONDITION),\n\
291 CONDITION's value if non-nil is returned from the cond-form.")
292 (args)
293 Lisp_Object args;
294 {
295 register Lisp_Object clause, val;
296 struct gcpro gcpro1;
297
298 val = Qnil;
299 GCPRO1 (args);
300 while (!NILP (args))
301 {
302 clause = Fcar (args);
303 val = Feval (Fcar (clause));
304 if (!NILP (val))
305 {
306 if (!EQ (XCONS (clause)->cdr, Qnil))
307 val = Fprogn (XCONS (clause)->cdr);
308 break;
309 }
310 args = XCONS (args)->cdr;
311 }
312 UNGCPRO;
313
314 return val;
315 }
316
317 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
318 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
319 (args)
320 Lisp_Object args;
321 {
322 register Lisp_Object val, tem;
323 Lisp_Object args_left;
324 struct gcpro gcpro1;
325
326 /* In Mocklisp code, symbols at the front of the progn arglist
327 are to be bound to zero. */
328 if (!EQ (Vmocklisp_arguments, Qt))
329 {
330 val = make_number (0);
331 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
332 {
333 QUIT;
334 specbind (tem, val), args = Fcdr (args);
335 }
336 }
337
338 if (NILP(args))
339 return Qnil;
340
341 args_left = args;
342 GCPRO1 (args_left);
343
344 do
345 {
346 val = Feval (Fcar (args_left));
347 args_left = Fcdr (args_left);
348 }
349 while (!NILP(args_left));
350
351 UNGCPRO;
352 return val;
353 }
354
355 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
356 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
357 The value of FIRST is saved during the evaluation of the remaining args,\n\
358 whose values are discarded.")
359 (args)
360 Lisp_Object args;
361 {
362 Lisp_Object val;
363 register Lisp_Object args_left;
364 struct gcpro gcpro1, gcpro2;
365 register int argnum = 0;
366
367 if (NILP(args))
368 return Qnil;
369
370 args_left = args;
371 val = Qnil;
372 GCPRO2 (args, val);
373
374 do
375 {
376 if (!(argnum++))
377 val = Feval (Fcar (args_left));
378 else
379 Feval (Fcar (args_left));
380 args_left = Fcdr (args_left);
381 }
382 while (!NILP(args_left));
383
384 UNGCPRO;
385 return val;
386 }
387
388 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
389 "(prog2 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
390 The value of Y is saved during the evaluation of the remaining args,\n\
391 whose values are discarded.")
392 (args)
393 Lisp_Object args;
394 {
395 Lisp_Object val;
396 register Lisp_Object args_left;
397 struct gcpro gcpro1, gcpro2;
398 register int argnum = -1;
399
400 val = Qnil;
401
402 if (NILP (args))
403 return Qnil;
404
405 args_left = args;
406 val = Qnil;
407 GCPRO2 (args, val);
408
409 do
410 {
411 if (!(argnum++))
412 val = Feval (Fcar (args_left));
413 else
414 Feval (Fcar (args_left));
415 args_left = Fcdr (args_left);
416 }
417 while (!NILP (args_left));
418
419 UNGCPRO;
420 return val;
421 }
422
423 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
424 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
425 The symbols SYM are variables; they are literal (not evaluated).\n\
426 The values VAL are expressions; they are evaluated.\n\
427 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
428 The second VAL is not computed until after the first SYM is set, and so on;\n\
429 each VAL can use the new value of variables set earlier in the `setq'.\n\
430 The return value of the `setq' form is the value of the last VAL.")
431 (args)
432 Lisp_Object args;
433 {
434 register Lisp_Object args_left;
435 register Lisp_Object val, sym;
436 struct gcpro gcpro1;
437
438 if (NILP(args))
439 return Qnil;
440
441 args_left = args;
442 GCPRO1 (args);
443
444 do
445 {
446 val = Feval (Fcar (Fcdr (args_left)));
447 sym = Fcar (args_left);
448 Fset (sym, val);
449 args_left = Fcdr (Fcdr (args_left));
450 }
451 while (!NILP(args_left));
452
453 UNGCPRO;
454 return val;
455 }
456
457 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
458 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
459 (args)
460 Lisp_Object args;
461 {
462 return Fcar (args);
463 }
464
465 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
466 "Like `quote', but preferred for objects which are functions.\n\
467 In byte compilation, `function' causes its argument to be compiled.\n\
468 `quote' cannot do that.")
469 (args)
470 Lisp_Object args;
471 {
472 return Fcar (args);
473 }
474
475 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
476 "Return t if function in which this appears was called interactively.\n\
477 This means that the function was called with call-interactively (which\n\
478 includes being called as the binding of a key)\n\
479 and input is currently coming from the keyboard (not in keyboard macro).")
480 ()
481 {
482 register struct backtrace *btp;
483 register Lisp_Object fun;
484
485 if (!INTERACTIVE)
486 return Qnil;
487
488 btp = backtrace_list;
489
490 /* If this isn't a byte-compiled function, there may be a frame at
491 the top for Finteractive_p itself. If so, skip it. */
492 fun = Findirect_function (*btp->function);
493 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
494 btp = btp->next;
495
496 /* If we're running an Emacs 18-style byte-compiled function, there
497 may be a frame for Fbytecode. Now, given the strictest
498 definition, this function isn't really being called
499 interactively, but because that's the way Emacs 18 always builds
500 byte-compiled functions, we'll accept it for now. */
501 if (EQ (*btp->function, Qbytecode))
502 btp = btp->next;
503
504 /* If this isn't a byte-compiled function, then we may now be
505 looking at several frames for special forms. Skip past them. */
506 while (btp &&
507 btp->nargs == UNEVALLED)
508 btp = btp->next;
509
510 /* btp now points at the frame of the innermost function that isn't
511 a special form, ignoring frames for Finteractive_p and/or
512 Fbytecode at the top. If this frame is for a built-in function
513 (such as load or eval-region) return nil. */
514 fun = Findirect_function (*btp->function);
515 if (SUBRP (fun))
516 return Qnil;
517 /* btp points to the frame of a Lisp function that called interactive-p.
518 Return t if that function was called interactively. */
519 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
520 return Qt;
521 return Qnil;
522 }
523
524 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
525 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
526 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
527 See also the function `interactive'.")
528 (args)
529 Lisp_Object args;
530 {
531 register Lisp_Object fn_name;
532 register Lisp_Object defn;
533
534 fn_name = Fcar (args);
535 defn = Fcons (Qlambda, Fcdr (args));
536 if (!NILP (Vpurify_flag))
537 defn = Fpurecopy (defn);
538 Ffset (fn_name, defn);
539 LOADHIST_ATTACH (fn_name);
540 return fn_name;
541 }
542
543 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
544 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
545 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
546 When the macro is called, as in (NAME ARGS...),\n\
547 the function (lambda ARGLIST BODY...) is applied to\n\
548 the list ARGS... as it appears in the expression,\n\
549 and the result should be a form to be evaluated instead of the original.")
550 (args)
551 Lisp_Object args;
552 {
553 register Lisp_Object fn_name;
554 register Lisp_Object defn;
555
556 fn_name = Fcar (args);
557 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
558 if (!NILP (Vpurify_flag))
559 defn = Fpurecopy (defn);
560 Ffset (fn_name, defn);
561 LOADHIST_ATTACH (fn_name);
562 return fn_name;
563 }
564
565 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
566 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
567 You are not required to define a variable in order to use it,\n\
568 but the definition can supply documentation and an initial value\n\
569 in a way that tags can recognize.\n\n\
570 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
571 If SYMBOL is buffer-local, its default value is what is set;\n\
572 buffer-local values are not affected.\n\
573 INITVALUE and DOCSTRING are optional.\n\
574 If DOCSTRING starts with *, this variable is identified as a user option.\n\
575 This means that M-x set-variable and M-x edit-options recognize it.\n\
576 If INITVALUE is missing, SYMBOL's value is not set.")
577 (args)
578 Lisp_Object args;
579 {
580 register Lisp_Object sym, tem, tail;
581
582 sym = Fcar (args);
583 tail = Fcdr (args);
584 if (!NILP (Fcdr (Fcdr (tail))))
585 error ("too many arguments");
586
587 if (!NILP (tail))
588 {
589 tem = Fdefault_boundp (sym);
590 if (NILP (tem))
591 Fset_default (sym, Feval (Fcar (Fcdr (args))));
592 }
593 tail = Fcdr (Fcdr (args));
594 if (!NILP (Fcar (tail)))
595 {
596 tem = Fcar (tail);
597 if (!NILP (Vpurify_flag))
598 tem = Fpurecopy (tem);
599 Fput (sym, Qvariable_documentation, tem);
600 }
601 LOADHIST_ATTACH (sym);
602 return sym;
603 }
604
605 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
606 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
607 The intent is that programs do not change this value, but users may.\n\
608 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
609 If SYMBOL is buffer-local, its default value is what is set;\n\
610 buffer-local values are not affected.\n\
611 DOCSTRING is optional.\n\
612 If DOCSTRING starts with *, this variable is identified as a user option.\n\
613 This means that M-x set-variable and M-x edit-options recognize it.\n\n\
614 Note: do not use `defconst' for user options in libraries that are not\n\
615 normally loaded, since it is useful for users to be able to specify\n\
616 their own values for such variables before loading the library.\n\
617 Since `defconst' unconditionally assigns the variable,\n\
618 it would override the user's choice.")
619 (args)
620 Lisp_Object args;
621 {
622 register Lisp_Object sym, tem;
623
624 sym = Fcar (args);
625 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
626 error ("too many arguments");
627
628 Fset_default (sym, Feval (Fcar (Fcdr (args))));
629 tem = Fcar (Fcdr (Fcdr (args)));
630 if (!NILP (tem))
631 {
632 if (!NILP (Vpurify_flag))
633 tem = Fpurecopy (tem);
634 Fput (sym, Qvariable_documentation, tem);
635 }
636 LOADHIST_ATTACH (sym);
637 return sym;
638 }
639
640 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
641 "Returns t if VARIABLE is intended to be set and modified by users.\n\
642 \(The alternative is a variable used internally in a Lisp program.)\n\
643 Determined by whether the first character of the documentation\n\
644 for the variable is `*'.")
645 (variable)
646 Lisp_Object variable;
647 {
648 Lisp_Object documentation;
649
650 documentation = Fget (variable, Qvariable_documentation);
651 if (INTEGERP (documentation) && XINT (documentation) < 0)
652 return Qt;
653 if (STRINGP (documentation)
654 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
655 return Qt;
656 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
657 if (CONSP (documentation)
658 && STRINGP (XCONS (documentation)->car)
659 && INTEGERP (XCONS (documentation)->cdr)
660 && XINT (XCONS (documentation)->cdr) < 0)
661 return Qt;
662 return Qnil;
663 }
664 \f
665 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
666 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
667 The value of the last form in BODY is returned.\n\
668 Each element of VARLIST is a symbol (which is bound to nil)\n\
669 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
670 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
671 (args)
672 Lisp_Object args;
673 {
674 Lisp_Object varlist, val, elt;
675 int count = specpdl_ptr - specpdl;
676 struct gcpro gcpro1, gcpro2, gcpro3;
677
678 GCPRO3 (args, elt, varlist);
679
680 varlist = Fcar (args);
681 while (!NILP (varlist))
682 {
683 QUIT;
684 elt = Fcar (varlist);
685 if (SYMBOLP (elt))
686 specbind (elt, Qnil);
687 else if (! NILP (Fcdr (Fcdr (elt))))
688 Fsignal (Qerror,
689 Fcons (build_string ("`let' bindings can have only one value-form"),
690 elt));
691 else
692 {
693 val = Feval (Fcar (Fcdr (elt)));
694 specbind (Fcar (elt), val);
695 }
696 varlist = Fcdr (varlist);
697 }
698 UNGCPRO;
699 val = Fprogn (Fcdr (args));
700 return unbind_to (count, val);
701 }
702
703 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
704 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
705 The value of the last form in BODY is returned.\n\
706 Each element of VARLIST is a symbol (which is bound to nil)\n\
707 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
708 All the VALUEFORMs are evalled before any symbols are bound.")
709 (args)
710 Lisp_Object args;
711 {
712 Lisp_Object *temps, tem;
713 register Lisp_Object elt, varlist;
714 int count = specpdl_ptr - specpdl;
715 register int argnum;
716 struct gcpro gcpro1, gcpro2;
717
718 varlist = Fcar (args);
719
720 /* Make space to hold the values to give the bound variables */
721 elt = Flength (varlist);
722 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
723
724 /* Compute the values and store them in `temps' */
725
726 GCPRO2 (args, *temps);
727 gcpro2.nvars = 0;
728
729 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
730 {
731 QUIT;
732 elt = Fcar (varlist);
733 if (SYMBOLP (elt))
734 temps [argnum++] = Qnil;
735 else if (! NILP (Fcdr (Fcdr (elt))))
736 Fsignal (Qerror,
737 Fcons (build_string ("`let' bindings can have only one value-form"),
738 elt));
739 else
740 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
741 gcpro2.nvars = argnum;
742 }
743 UNGCPRO;
744
745 varlist = Fcar (args);
746 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
747 {
748 elt = Fcar (varlist);
749 tem = temps[argnum++];
750 if (SYMBOLP (elt))
751 specbind (elt, tem);
752 else
753 specbind (Fcar (elt), tem);
754 }
755
756 elt = Fprogn (Fcdr (args));
757 return unbind_to (count, elt);
758 }
759
760 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
761 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
762 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
763 until TEST returns nil.")
764 (args)
765 Lisp_Object args;
766 {
767 Lisp_Object test, body, tem;
768 struct gcpro gcpro1, gcpro2;
769
770 GCPRO2 (test, body);
771
772 test = Fcar (args);
773 body = Fcdr (args);
774 while (tem = Feval (test),
775 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
776 {
777 QUIT;
778 Fprogn (body);
779 }
780
781 UNGCPRO;
782 return Qnil;
783 }
784
785 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
786 "Return result of expanding macros at top level of FORM.\n\
787 If FORM is not a macro call, it is returned unchanged.\n\
788 Otherwise, the macro is expanded and the expansion is considered\n\
789 in place of FORM. When a non-macro-call results, it is returned.\n\n\
790 The second optional arg ENVIRONMENT species an environment of macro\n\
791 definitions to shadow the loaded ones for use in file byte-compilation.")
792 (form, env)
793 register Lisp_Object form;
794 Lisp_Object env;
795 {
796 /* With cleanups from Hallvard Furuseth. */
797 register Lisp_Object expander, sym, def, tem;
798
799 while (1)
800 {
801 /* Come back here each time we expand a macro call,
802 in case it expands into another macro call. */
803 if (!CONSP (form))
804 break;
805 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
806 def = sym = XCONS (form)->car;
807 tem = Qnil;
808 /* Trace symbols aliases to other symbols
809 until we get a symbol that is not an alias. */
810 while (SYMBOLP (def))
811 {
812 QUIT;
813 sym = def;
814 tem = Fassq (sym, env);
815 if (NILP (tem))
816 {
817 def = XSYMBOL (sym)->function;
818 if (!EQ (def, Qunbound))
819 continue;
820 }
821 break;
822 }
823 /* Right now TEM is the result from SYM in ENV,
824 and if TEM is nil then DEF is SYM's function definition. */
825 if (NILP (tem))
826 {
827 /* SYM is not mentioned in ENV.
828 Look at its function definition. */
829 if (EQ (def, Qunbound) || !CONSP (def))
830 /* Not defined or definition not suitable */
831 break;
832 if (EQ (XCONS (def)->car, Qautoload))
833 {
834 /* Autoloading function: will it be a macro when loaded? */
835 tem = Fnth (make_number (4), def);
836 if (EQ (tem, Qt) || EQ (tem, Qmacro))
837 /* Yes, load it and try again. */
838 {
839 do_autoload (def, sym);
840 continue;
841 }
842 else
843 break;
844 }
845 else if (!EQ (XCONS (def)->car, Qmacro))
846 break;
847 else expander = XCONS (def)->cdr;
848 }
849 else
850 {
851 expander = XCONS (tem)->cdr;
852 if (NILP (expander))
853 break;
854 }
855 form = apply1 (expander, XCONS (form)->cdr);
856 }
857 return form;
858 }
859 \f
860 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
861 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
862 TAG is evalled to get the tag to use. Then the BODY is executed.\n\
863 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
864 If no throw happens, `catch' returns the value of the last BODY form.\n\
865 If a throw happens, it specifies the value to return from `catch'.")
866 (args)
867 Lisp_Object args;
868 {
869 register Lisp_Object tag;
870 struct gcpro gcpro1;
871
872 GCPRO1 (args);
873 tag = Feval (Fcar (args));
874 UNGCPRO;
875 return internal_catch (tag, Fprogn, Fcdr (args));
876 }
877
878 /* Set up a catch, then call C function FUNC on argument ARG.
879 FUNC should return a Lisp_Object.
880 This is how catches are done from within C code. */
881
882 Lisp_Object
883 internal_catch (tag, func, arg)
884 Lisp_Object tag;
885 Lisp_Object (*func) ();
886 Lisp_Object arg;
887 {
888 /* This structure is made part of the chain `catchlist'. */
889 struct catchtag c;
890
891 /* Fill in the components of c, and put it on the list. */
892 c.next = catchlist;
893 c.tag = tag;
894 c.val = Qnil;
895 c.backlist = backtrace_list;
896 c.handlerlist = handlerlist;
897 c.lisp_eval_depth = lisp_eval_depth;
898 c.pdlcount = specpdl_ptr - specpdl;
899 c.poll_suppress_count = poll_suppress_count;
900 c.gcpro = gcprolist;
901 catchlist = &c;
902
903 /* Call FUNC. */
904 if (! _setjmp (c.jmp))
905 c.val = (*func) (arg);
906
907 /* Throw works by a longjmp that comes right here. */
908 catchlist = c.next;
909 return c.val;
910 }
911
912 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
913 jump to that CATCH, returning VALUE as the value of that catch.
914
915 This is the guts Fthrow and Fsignal; they differ only in the way
916 they choose the catch tag to throw to. A catch tag for a
917 condition-case form has a TAG of Qnil.
918
919 Before each catch is discarded, unbind all special bindings and
920 execute all unwind-protect clauses made above that catch. Unwind
921 the handler stack as we go, so that the proper handlers are in
922 effect for each unwind-protect clause we run. At the end, restore
923 some static info saved in CATCH, and longjmp to the location
924 specified in the
925
926 This is used for correct unwinding in Fthrow and Fsignal. */
927
928 static void
929 unwind_to_catch (catch, value)
930 struct catchtag *catch;
931 Lisp_Object value;
932 {
933 register int last_time;
934
935 /* Save the value in the tag. */
936 catch->val = value;
937
938 /* Restore the polling-suppression count. */
939 set_poll_suppress_count (catch->poll_suppress_count);
940
941 do
942 {
943 last_time = catchlist == catch;
944
945 /* Unwind the specpdl stack, and then restore the proper set of
946 handlers. */
947 unbind_to (catchlist->pdlcount, Qnil);
948 handlerlist = catchlist->handlerlist;
949 catchlist = catchlist->next;
950 }
951 while (! last_time);
952
953 gcprolist = catch->gcpro;
954 backtrace_list = catch->backlist;
955 lisp_eval_depth = catch->lisp_eval_depth;
956
957 _longjmp (catch->jmp, 1);
958 }
959
960 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
961 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
962 Both TAG and VALUE are evalled.")
963 (tag, val)
964 register Lisp_Object tag, val;
965 {
966 register struct catchtag *c;
967
968 while (1)
969 {
970 if (!NILP (tag))
971 for (c = catchlist; c; c = c->next)
972 {
973 if (EQ (c->tag, tag))
974 unwind_to_catch (c, val);
975 }
976 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (val, Qnil)));
977 }
978 }
979
980
981 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
982 "Do BODYFORM, protecting with UNWINDFORMS.\n\
983 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
984 If BODYFORM completes normally, its value is returned\n\
985 after executing the UNWINDFORMS.\n\
986 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
987 (args)
988 Lisp_Object args;
989 {
990 Lisp_Object val;
991 int count = specpdl_ptr - specpdl;
992
993 record_unwind_protect (0, Fcdr (args));
994 val = Feval (Fcar (args));
995 return unbind_to (count, val);
996 }
997 \f
998 /* Chain of condition handlers currently in effect.
999 The elements of this chain are contained in the stack frames
1000 of Fcondition_case and internal_condition_case.
1001 When an error is signaled (by calling Fsignal, below),
1002 this chain is searched for an element that applies. */
1003
1004 struct handler *handlerlist;
1005
1006 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1007 "Regain control when an error is signaled.\n\
1008 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
1009 executes BODYFORM and returns its value if no error happens.\n\
1010 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1011 where the BODY is made of Lisp expressions.\n\n\
1012 A handler is applicable to an error\n\
1013 if CONDITION-NAME is one of the error's condition names.\n\
1014 If an error happens, the first applicable handler is run.\n\
1015 \n\
1016 The car of a handler may be a list of condition names\n\
1017 instead of a single condition name.\n\
1018 \n\
1019 When a handler handles an error,\n\
1020 control returns to the condition-case and the handler BODY... is executed\n\
1021 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1022 VAR may be nil; then you do not get access to the signal information.\n\
1023 \n\
1024 The value of the last BODY form is returned from the condition-case.\n\
1025 See also the function `signal' for more info.")
1026 (args)
1027 Lisp_Object args;
1028 {
1029 Lisp_Object val;
1030 struct catchtag c;
1031 struct handler h;
1032 register Lisp_Object var, bodyform, handlers;
1033
1034 var = Fcar (args);
1035 bodyform = Fcar (Fcdr (args));
1036 handlers = Fcdr (Fcdr (args));
1037 CHECK_SYMBOL (var, 0);
1038
1039 for (val = handlers; ! NILP (val); val = Fcdr (val))
1040 {
1041 Lisp_Object tem;
1042 tem = Fcar (val);
1043 if (! (NILP (tem)
1044 || (CONSP (tem)
1045 && (SYMBOLP (XCONS (tem)->car)
1046 || CONSP (XCONS (tem)->car)))))
1047 error ("Invalid condition handler", tem);
1048 }
1049
1050 c.tag = Qnil;
1051 c.val = Qnil;
1052 c.backlist = backtrace_list;
1053 c.handlerlist = handlerlist;
1054 c.lisp_eval_depth = lisp_eval_depth;
1055 c.pdlcount = specpdl_ptr - specpdl;
1056 c.poll_suppress_count = poll_suppress_count;
1057 c.gcpro = gcprolist;
1058 if (_setjmp (c.jmp))
1059 {
1060 if (!NILP (h.var))
1061 specbind (h.var, c.val);
1062 val = Fprogn (Fcdr (h.chosen_clause));
1063
1064 /* Note that this just undoes the binding of h.var; whoever
1065 longjumped to us unwound the stack to c.pdlcount before
1066 throwing. */
1067 unbind_to (c.pdlcount, Qnil);
1068 return val;
1069 }
1070 c.next = catchlist;
1071 catchlist = &c;
1072
1073 h.var = var;
1074 h.handler = handlers;
1075 h.next = handlerlist;
1076 h.tag = &c;
1077 handlerlist = &h;
1078
1079 val = Feval (bodyform);
1080 catchlist = c.next;
1081 handlerlist = h.next;
1082 return val;
1083 }
1084
1085 Lisp_Object
1086 internal_condition_case (bfun, handlers, hfun)
1087 Lisp_Object (*bfun) ();
1088 Lisp_Object handlers;
1089 Lisp_Object (*hfun) ();
1090 {
1091 Lisp_Object val;
1092 struct catchtag c;
1093 struct handler h;
1094
1095 /* Since Fsignal resets this to 0, it had better be 0 now
1096 or else we have a potential bug. */
1097 if (interrupt_input_blocked != 0)
1098 abort ();
1099
1100 c.tag = Qnil;
1101 c.val = Qnil;
1102 c.backlist = backtrace_list;
1103 c.handlerlist = handlerlist;
1104 c.lisp_eval_depth = lisp_eval_depth;
1105 c.pdlcount = specpdl_ptr - specpdl;
1106 c.poll_suppress_count = poll_suppress_count;
1107 c.gcpro = gcprolist;
1108 if (_setjmp (c.jmp))
1109 {
1110 return (*hfun) (c.val);
1111 }
1112 c.next = catchlist;
1113 catchlist = &c;
1114 h.handler = handlers;
1115 h.var = Qnil;
1116 h.next = handlerlist;
1117 h.tag = &c;
1118 handlerlist = &h;
1119
1120 val = (*bfun) ();
1121 catchlist = c.next;
1122 handlerlist = h.next;
1123 return val;
1124 }
1125
1126 Lisp_Object
1127 internal_condition_case_1 (bfun, arg, handlers, hfun)
1128 Lisp_Object (*bfun) ();
1129 Lisp_Object arg;
1130 Lisp_Object handlers;
1131 Lisp_Object (*hfun) ();
1132 {
1133 Lisp_Object val;
1134 struct catchtag c;
1135 struct handler h;
1136
1137 c.tag = Qnil;
1138 c.val = Qnil;
1139 c.backlist = backtrace_list;
1140 c.handlerlist = handlerlist;
1141 c.lisp_eval_depth = lisp_eval_depth;
1142 c.pdlcount = specpdl_ptr - specpdl;
1143 c.poll_suppress_count = poll_suppress_count;
1144 c.gcpro = gcprolist;
1145 if (_setjmp (c.jmp))
1146 {
1147 return (*hfun) (c.val);
1148 }
1149 c.next = catchlist;
1150 catchlist = &c;
1151 h.handler = handlers;
1152 h.var = Qnil;
1153 h.next = handlerlist;
1154 h.tag = &c;
1155 handlerlist = &h;
1156
1157 val = (*bfun) (arg);
1158 catchlist = c.next;
1159 handlerlist = h.next;
1160 return val;
1161 }
1162 \f
1163 static Lisp_Object find_handler_clause ();
1164
1165 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1166 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1167 This function does not return.\n\n\
1168 An error symbol is a symbol with an `error-conditions' property\n\
1169 that is a list of condition names.\n\
1170 A handler for any of those names will get to handle this signal.\n\
1171 The symbol `error' should normally be one of them.\n\
1172 \n\
1173 DATA should be a list. Its elements are printed as part of the error message.\n\
1174 If the signal is handled, DATA is made available to the handler.\n\
1175 See also the function `condition-case'.")
1176 (error_symbol, data)
1177 Lisp_Object error_symbol, data;
1178 {
1179 register struct handler *allhandlers = handlerlist;
1180 Lisp_Object conditions;
1181 extern int gc_in_progress;
1182 extern int waiting_for_input;
1183 Lisp_Object debugger_value;
1184
1185 quit_error_check ();
1186 immediate_quit = 0;
1187 if (gc_in_progress || waiting_for_input)
1188 abort ();
1189
1190 #ifdef HAVE_X_WINDOWS
1191 TOTALLY_UNBLOCK_INPUT;
1192 #endif
1193
1194 conditions = Fget (error_symbol, Qerror_conditions);
1195
1196 for (; handlerlist; handlerlist = handlerlist->next)
1197 {
1198 register Lisp_Object clause;
1199 clause = find_handler_clause (handlerlist->handler, conditions,
1200 error_symbol, data, &debugger_value);
1201
1202 #if 0 /* Most callers are not prepared to handle gc if this returns.
1203 So, since this feature is not very useful, take it out. */
1204 /* If have called debugger and user wants to continue,
1205 just return nil. */
1206 if (EQ (clause, Qlambda))
1207 return debugger_value;
1208 #else
1209 if (EQ (clause, Qlambda))
1210 {
1211 /* We can't return values to code which signaled an error, but we
1212 can continue code which has signaled a quit. */
1213 if (EQ (error_symbol, Qquit))
1214 return Qnil;
1215 else
1216 error ("Cannot return from the debugger in an error");
1217 }
1218 #endif
1219
1220 if (!NILP (clause))
1221 {
1222 Lisp_Object unwind_data;
1223 struct handler *h = handlerlist;
1224
1225 handlerlist = allhandlers;
1226 if (EQ (data, memory_signal_data))
1227 unwind_data = memory_signal_data;
1228 else
1229 unwind_data = Fcons (error_symbol, data);
1230 h->chosen_clause = clause;
1231 unwind_to_catch (h->tag, unwind_data);
1232 }
1233 }
1234
1235 handlerlist = allhandlers;
1236 /* If no handler is present now, try to run the debugger,
1237 and if that fails, throw to top level. */
1238 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1239 Fthrow (Qtop_level, Qt);
1240 }
1241
1242 /* Return nonzero iff LIST is a non-nil atom or
1243 a list containing one of CONDITIONS. */
1244
1245 static int
1246 wants_debugger (list, conditions)
1247 Lisp_Object list, conditions;
1248 {
1249 if (NILP (list))
1250 return 0;
1251 if (! CONSP (list))
1252 return 1;
1253
1254 while (CONSP (conditions))
1255 {
1256 Lisp_Object this, tail;
1257 this = XCONS (conditions)->car;
1258 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
1259 if (EQ (XCONS (tail)->car, this))
1260 return 1;
1261 conditions = XCONS (conditions)->cdr;
1262 }
1263 return 0;
1264 }
1265
1266 /* Return 1 if an error with condition-symbols CONDITIONS,
1267 and described by SIGNAL-DATA, should skip the debugger
1268 according to debugger-ignore-errors. */
1269
1270 static int
1271 skip_debugger (conditions, data)
1272 Lisp_Object conditions, data;
1273 {
1274 Lisp_Object tail;
1275 int first_string = 1;
1276 Lisp_Object error_message;
1277
1278 for (tail = Vdebug_ignored_errors; CONSP (tail);
1279 tail = XCONS (tail)->cdr)
1280 {
1281 if (STRINGP (XCONS (tail)->car))
1282 {
1283 if (first_string)
1284 {
1285 error_message = Ferror_message_string (data);
1286 first_string = 0;
1287 }
1288 if (fast_string_match (XCONS (tail)->car, error_message) >= 0)
1289 return 1;
1290 }
1291 else
1292 {
1293 Lisp_Object contail;
1294
1295 for (contail = conditions; CONSP (contail);
1296 contail = XCONS (contail)->cdr)
1297 if (EQ (XCONS (tail)->car, XCONS (contail)->car))
1298 return 1;
1299 }
1300 }
1301
1302 return 0;
1303 }
1304
1305 /* Value of Qlambda means we have called debugger and user has continued.
1306 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1307
1308 static Lisp_Object
1309 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1310 Lisp_Object handlers, conditions, sig, data;
1311 Lisp_Object *debugger_value_ptr;
1312 {
1313 register Lisp_Object h;
1314 register Lisp_Object tem;
1315
1316 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1317 return Qt;
1318 if (EQ (handlers, Qerror)) /* error is used similarly, but means display a backtrace too */
1319 {
1320 if (wants_debugger (Vstack_trace_on_error, conditions))
1321 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace, Qnil);
1322 if ((EQ (sig, Qquit)
1323 ? debug_on_quit
1324 : wants_debugger (Vdebug_on_error, conditions))
1325 && ! skip_debugger (conditions, Fcons (sig, data))
1326 && when_entered_debugger < num_nonmacro_input_chars)
1327 {
1328 int count = specpdl_ptr - specpdl;
1329 specbind (Qdebug_on_error, Qnil);
1330 *debugger_value_ptr
1331 = call_debugger (Fcons (Qerror,
1332 Fcons (Fcons (sig, data),
1333 Qnil)));
1334 return unbind_to (count, Qlambda);
1335 }
1336 return Qt;
1337 }
1338 for (h = handlers; CONSP (h); h = Fcdr (h))
1339 {
1340 Lisp_Object handler, condit;
1341
1342 handler = Fcar (h);
1343 if (!CONSP (handler))
1344 continue;
1345 condit = Fcar (handler);
1346 /* Handle a single condition name in handler HANDLER. */
1347 if (SYMBOLP (condit))
1348 {
1349 tem = Fmemq (Fcar (handler), conditions);
1350 if (!NILP (tem))
1351 return handler;
1352 }
1353 /* Handle a list of condition names in handler HANDLER. */
1354 else if (CONSP (condit))
1355 {
1356 while (CONSP (condit))
1357 {
1358 tem = Fmemq (Fcar (condit), conditions);
1359 if (!NILP (tem))
1360 return handler;
1361 condit = XCONS (condit)->cdr;
1362 }
1363 }
1364 }
1365 return Qnil;
1366 }
1367
1368 /* dump an error message; called like printf */
1369
1370 /* VARARGS 1 */
1371 void
1372 error (m, a1, a2, a3)
1373 char *m;
1374 char *a1, *a2, *a3;
1375 {
1376 char buf[200];
1377 int size = 200;
1378 int mlen;
1379 char *buffer = buf;
1380 char *args[3];
1381 int allocated = 0;
1382 Lisp_Object string;
1383
1384 args[0] = a1;
1385 args[1] = a2;
1386 args[2] = a3;
1387
1388 mlen = strlen (m);
1389
1390 while (1)
1391 {
1392 int used = doprnt (buf, size, m, m + mlen, 3, args);
1393 if (used < size)
1394 break;
1395 size *= 2;
1396 if (allocated)
1397 buffer = (char *) xrealloc (buffer, size);
1398 else
1399 {
1400 buffer = (char *) xmalloc (size);
1401 allocated = 1;
1402 }
1403 }
1404
1405 string = build_string (buf);
1406 if (allocated)
1407 free (buffer);
1408
1409 Fsignal (Qerror, Fcons (string, Qnil));
1410 }
1411 \f
1412 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1413 "T if FUNCTION makes provisions for interactive calling.\n\
1414 This means it contains a description for how to read arguments to give it.\n\
1415 The value is nil for an invalid function or a symbol with no function\n\
1416 definition.\n\
1417 \n\
1418 Interactively callable functions include strings and vectors (treated\n\
1419 as keyboard macros), lambda-expressions that contain a top-level call\n\
1420 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1421 fourth argument, and some of the built-in functions of Lisp.\n\
1422 \n\
1423 Also, a symbol satisfies `commandp' if its function definition does so.")
1424 (function)
1425 Lisp_Object function;
1426 {
1427 register Lisp_Object fun;
1428 register Lisp_Object funcar;
1429 register Lisp_Object tem;
1430 register int i = 0;
1431
1432 fun = function;
1433
1434 fun = indirect_function (fun);
1435 if (EQ (fun, Qunbound))
1436 return Qnil;
1437
1438 /* Emacs primitives are interactive if their DEFUN specifies an
1439 interactive spec. */
1440 if (SUBRP (fun))
1441 {
1442 if (XSUBR (fun)->prompt)
1443 return Qt;
1444 else
1445 return Qnil;
1446 }
1447
1448 /* Bytecode objects are interactive if they are long enough to
1449 have an element whose index is COMPILED_INTERACTIVE, which is
1450 where the interactive spec is stored. */
1451 else if (COMPILEDP (fun))
1452 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1453 ? Qt : Qnil);
1454
1455 /* Strings and vectors are keyboard macros. */
1456 if (STRINGP (fun) || VECTORP (fun))
1457 return Qt;
1458
1459 /* Lists may represent commands. */
1460 if (!CONSP (fun))
1461 return Qnil;
1462 funcar = Fcar (fun);
1463 if (!SYMBOLP (funcar))
1464 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1465 if (EQ (funcar, Qlambda))
1466 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1467 if (EQ (funcar, Qmocklisp))
1468 return Qt; /* All mocklisp functions can be called interactively */
1469 if (EQ (funcar, Qautoload))
1470 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1471 else
1472 return Qnil;
1473 }
1474
1475 /* ARGSUSED */
1476 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1477 "Define FUNCTION to autoload from FILE.\n\
1478 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1479 Third arg DOCSTRING is documentation for the function.\n\
1480 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1481 Fifth arg TYPE indicates the type of the object:\n\
1482 nil or omitted says FUNCTION is a function,\n\
1483 `keymap' says FUNCTION is really a keymap, and\n\
1484 `macro' or t says FUNCTION is really a macro.\n\
1485 Third through fifth args give info about the real definition.\n\
1486 They default to nil.\n\
1487 If FUNCTION is already defined other than as an autoload,\n\
1488 this does nothing and returns nil.")
1489 (function, file, docstring, interactive, type)
1490 Lisp_Object function, file, docstring, interactive, type;
1491 {
1492 #ifdef NO_ARG_ARRAY
1493 Lisp_Object args[4];
1494 #endif
1495
1496 CHECK_SYMBOL (function, 0);
1497 CHECK_STRING (file, 1);
1498
1499 /* If function is defined and not as an autoload, don't override */
1500 if (!EQ (XSYMBOL (function)->function, Qunbound)
1501 && !(CONSP (XSYMBOL (function)->function)
1502 && EQ (XCONS (XSYMBOL (function)->function)->car, Qautoload)))
1503 return Qnil;
1504
1505 #ifdef NO_ARG_ARRAY
1506 args[0] = file;
1507 args[1] = docstring;
1508 args[2] = interactive;
1509 args[3] = type;
1510
1511 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1512 #else /* NO_ARG_ARRAY */
1513 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1514 #endif /* not NO_ARG_ARRAY */
1515 }
1516
1517 Lisp_Object
1518 un_autoload (oldqueue)
1519 Lisp_Object oldqueue;
1520 {
1521 register Lisp_Object queue, first, second;
1522
1523 /* Queue to unwind is current value of Vautoload_queue.
1524 oldqueue is the shadowed value to leave in Vautoload_queue. */
1525 queue = Vautoload_queue;
1526 Vautoload_queue = oldqueue;
1527 while (CONSP (queue))
1528 {
1529 first = Fcar (queue);
1530 second = Fcdr (first);
1531 first = Fcar (first);
1532 if (EQ (second, Qnil))
1533 Vfeatures = first;
1534 else
1535 Ffset (first, second);
1536 queue = Fcdr (queue);
1537 }
1538 return Qnil;
1539 }
1540
1541 do_autoload (fundef, funname)
1542 Lisp_Object fundef, funname;
1543 {
1544 int count = specpdl_ptr - specpdl;
1545 Lisp_Object fun, val, queue, first, second;
1546
1547 fun = funname;
1548 CHECK_SYMBOL (funname, 0);
1549
1550 /* Value saved here is to be restored into Vautoload_queue */
1551 record_unwind_protect (un_autoload, Vautoload_queue);
1552 Vautoload_queue = Qt;
1553 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);
1554
1555 /* Save the old autoloads, in case we ever do an unload. */
1556 queue = Vautoload_queue;
1557 while (CONSP (queue))
1558 {
1559 first = Fcar (queue);
1560 second = Fcdr (first);
1561 first = Fcar (first);
1562
1563 /* Note: This test is subtle. The cdr of an autoload-queue entry
1564 may be an atom if the autoload entry was generated by a defalias
1565 or fset. */
1566 if (CONSP (second))
1567 Fput (first, Qautoload, (Fcdr (second)));
1568
1569 queue = Fcdr (queue);
1570 }
1571
1572 /* Once loading finishes, don't undo it. */
1573 Vautoload_queue = Qt;
1574 unbind_to (count, Qnil);
1575
1576 fun = Findirect_function (fun);
1577
1578 if (!NILP (Fequal (fun, fundef)))
1579 error ("Autoloading failed to define function %s",
1580 XSYMBOL (funname)->name->data);
1581 }
1582 \f
1583 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1584 "Evaluate FORM and return its value.")
1585 (form)
1586 Lisp_Object form;
1587 {
1588 Lisp_Object fun, val, original_fun, original_args;
1589 Lisp_Object funcar;
1590 struct backtrace backtrace;
1591 struct gcpro gcpro1, gcpro2, gcpro3;
1592
1593 if (SYMBOLP (form))
1594 {
1595 if (EQ (Vmocklisp_arguments, Qt))
1596 return Fsymbol_value (form);
1597 val = Fsymbol_value (form);
1598 if (NILP (val))
1599 XSETFASTINT (val, 0);
1600 else if (EQ (val, Qt))
1601 XSETFASTINT (val, 1);
1602 return val;
1603 }
1604 if (!CONSP (form))
1605 return form;
1606
1607 QUIT;
1608 if (consing_since_gc > gc_cons_threshold)
1609 {
1610 GCPRO1 (form);
1611 Fgarbage_collect ();
1612 UNGCPRO;
1613 }
1614
1615 if (++lisp_eval_depth > max_lisp_eval_depth)
1616 {
1617 if (max_lisp_eval_depth < 100)
1618 max_lisp_eval_depth = 100;
1619 if (lisp_eval_depth > max_lisp_eval_depth)
1620 error ("Lisp nesting exceeds max-lisp-eval-depth");
1621 }
1622
1623 original_fun = Fcar (form);
1624 original_args = Fcdr (form);
1625
1626 backtrace.next = backtrace_list;
1627 backtrace_list = &backtrace;
1628 backtrace.function = &original_fun; /* This also protects them from gc */
1629 backtrace.args = &original_args;
1630 backtrace.nargs = UNEVALLED;
1631 backtrace.evalargs = 1;
1632 backtrace.debug_on_exit = 0;
1633
1634 if (debug_on_next_call)
1635 do_debug_on_call (Qt);
1636
1637 /* At this point, only original_fun and original_args
1638 have values that will be used below */
1639 retry:
1640 fun = Findirect_function (original_fun);
1641
1642 if (SUBRP (fun))
1643 {
1644 Lisp_Object numargs;
1645 Lisp_Object argvals[7];
1646 Lisp_Object args_left;
1647 register int i, maxargs;
1648
1649 args_left = original_args;
1650 numargs = Flength (args_left);
1651
1652 if (XINT (numargs) < XSUBR (fun)->min_args ||
1653 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1654 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1655
1656 if (XSUBR (fun)->max_args == UNEVALLED)
1657 {
1658 backtrace.evalargs = 0;
1659 val = (*XSUBR (fun)->function) (args_left);
1660 goto done;
1661 }
1662
1663 if (XSUBR (fun)->max_args == MANY)
1664 {
1665 /* Pass a vector of evaluated arguments */
1666 Lisp_Object *vals;
1667 register int argnum = 0;
1668
1669 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1670
1671 GCPRO3 (args_left, fun, fun);
1672 gcpro3.var = vals;
1673 gcpro3.nvars = 0;
1674
1675 while (!NILP (args_left))
1676 {
1677 vals[argnum++] = Feval (Fcar (args_left));
1678 args_left = Fcdr (args_left);
1679 gcpro3.nvars = argnum;
1680 }
1681
1682 backtrace.args = vals;
1683 backtrace.nargs = XINT (numargs);
1684
1685 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1686 UNGCPRO;
1687 goto done;
1688 }
1689
1690 GCPRO3 (args_left, fun, fun);
1691 gcpro3.var = argvals;
1692 gcpro3.nvars = 0;
1693
1694 maxargs = XSUBR (fun)->max_args;
1695 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1696 {
1697 argvals[i] = Feval (Fcar (args_left));
1698 gcpro3.nvars = ++i;
1699 }
1700
1701 UNGCPRO;
1702
1703 backtrace.args = argvals;
1704 backtrace.nargs = XINT (numargs);
1705
1706 switch (i)
1707 {
1708 case 0:
1709 val = (*XSUBR (fun)->function) ();
1710 goto done;
1711 case 1:
1712 val = (*XSUBR (fun)->function) (argvals[0]);
1713 goto done;
1714 case 2:
1715 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1716 goto done;
1717 case 3:
1718 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1719 argvals[2]);
1720 goto done;
1721 case 4:
1722 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1723 argvals[2], argvals[3]);
1724 goto done;
1725 case 5:
1726 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1727 argvals[3], argvals[4]);
1728 goto done;
1729 case 6:
1730 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1731 argvals[3], argvals[4], argvals[5]);
1732 goto done;
1733 case 7:
1734 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1735 argvals[3], argvals[4], argvals[5],
1736 argvals[6]);
1737 goto done;
1738
1739 default:
1740 /* Someone has created a subr that takes more arguments than
1741 is supported by this code. We need to either rewrite the
1742 subr to use a different argument protocol, or add more
1743 cases to this switch. */
1744 abort ();
1745 }
1746 }
1747 if (COMPILEDP (fun))
1748 val = apply_lambda (fun, original_args, 1);
1749 else
1750 {
1751 if (!CONSP (fun))
1752 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1753 funcar = Fcar (fun);
1754 if (!SYMBOLP (funcar))
1755 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1756 if (EQ (funcar, Qautoload))
1757 {
1758 do_autoload (fun, original_fun);
1759 goto retry;
1760 }
1761 if (EQ (funcar, Qmacro))
1762 val = Feval (apply1 (Fcdr (fun), original_args));
1763 else if (EQ (funcar, Qlambda))
1764 val = apply_lambda (fun, original_args, 1);
1765 else if (EQ (funcar, Qmocklisp))
1766 val = ml_apply (fun, original_args);
1767 else
1768 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1769 }
1770 done:
1771 if (!EQ (Vmocklisp_arguments, Qt))
1772 {
1773 if (NILP (val))
1774 XSETFASTINT (val, 0);
1775 else if (EQ (val, Qt))
1776 XSETFASTINT (val, 1);
1777 }
1778 lisp_eval_depth--;
1779 if (backtrace.debug_on_exit)
1780 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1781 backtrace_list = backtrace.next;
1782 return val;
1783 }
1784 \f
1785 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1786 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1787 Then return the value FUNCTION returns.\n\
1788 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1789 (nargs, args)
1790 int nargs;
1791 Lisp_Object *args;
1792 {
1793 register int i, numargs;
1794 register Lisp_Object spread_arg;
1795 register Lisp_Object *funcall_args;
1796 Lisp_Object fun;
1797 struct gcpro gcpro1;
1798
1799 fun = args [0];
1800 funcall_args = 0;
1801 spread_arg = args [nargs - 1];
1802 CHECK_LIST (spread_arg, nargs);
1803
1804 numargs = XINT (Flength (spread_arg));
1805
1806 if (numargs == 0)
1807 return Ffuncall (nargs - 1, args);
1808 else if (numargs == 1)
1809 {
1810 args [nargs - 1] = XCONS (spread_arg)->car;
1811 return Ffuncall (nargs, args);
1812 }
1813
1814 numargs += nargs - 2;
1815
1816 fun = indirect_function (fun);
1817 if (EQ (fun, Qunbound))
1818 {
1819 /* Let funcall get the error */
1820 fun = args[0];
1821 goto funcall;
1822 }
1823
1824 if (SUBRP (fun))
1825 {
1826 if (numargs < XSUBR (fun)->min_args
1827 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1828 goto funcall; /* Let funcall get the error */
1829 else if (XSUBR (fun)->max_args > numargs)
1830 {
1831 /* Avoid making funcall cons up a yet another new vector of arguments
1832 by explicitly supplying nil's for optional values */
1833 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
1834 * sizeof (Lisp_Object));
1835 for (i = numargs; i < XSUBR (fun)->max_args;)
1836 funcall_args[++i] = Qnil;
1837 GCPRO1 (*funcall_args);
1838 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
1839 }
1840 }
1841 funcall:
1842 /* We add 1 to numargs because funcall_args includes the
1843 function itself as well as its arguments. */
1844 if (!funcall_args)
1845 {
1846 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1847 * sizeof (Lisp_Object));
1848 GCPRO1 (*funcall_args);
1849 gcpro1.nvars = 1 + numargs;
1850 }
1851
1852 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
1853 /* Spread the last arg we got. Its first element goes in
1854 the slot that it used to occupy, hence this value of I. */
1855 i = nargs - 1;
1856 while (!NILP (spread_arg))
1857 {
1858 funcall_args [i++] = XCONS (spread_arg)->car;
1859 spread_arg = XCONS (spread_arg)->cdr;
1860 }
1861
1862 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
1863 }
1864 \f
1865 /* Run hook variables in various ways. */
1866
1867 enum run_hooks_condition {to_completion, until_success, until_failure};
1868
1869 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 1, MANY, 0,
1870 "Run each hook in HOOKS. Major mode functions use this.\n\
1871 Each argument should be a symbol, a hook variable.\n\
1872 These symbols are processed in the order specified.\n\
1873 If a hook symbol has a non-nil value, that value may be a function\n\
1874 or a list of functions to be called to run the hook.\n\
1875 If the value is a function, it is called with no arguments.\n\
1876 If it is a list, the elements are called, in order, with no arguments.\n\
1877 \n\
1878 To make a hook variable buffer-local, use `make-local-hook',\n\
1879 not `make-local-variable'.")
1880 (nargs, args)
1881 int nargs;
1882 Lisp_Object *args;
1883 {
1884 Lisp_Object hook[1];
1885 register int i;
1886
1887 for (i = 0; i < nargs; i++)
1888 {
1889 hook[0] = args[i];
1890 run_hook_with_args (1, hook, to_completion);
1891 }
1892
1893 return Qnil;
1894 }
1895
1896 DEFUN ("run-hook-with-args",
1897 Frun_hook_with_args, Srun_hook_with_args, 1, MANY, 0,
1898 "Run HOOK with the specified arguments ARGS.\n\
1899 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
1900 value, that value may be a function or a list of functions to be\n\
1901 called to run the hook. If the value is a function, it is called with\n\
1902 the given arguments and its return value is returned. If it is a list\n\
1903 of functions, those functions are called, in order,\n\
1904 with the given arguments ARGS.\n\
1905 It is best not to depend on the value return by `run-hook-with-args',\n\
1906 as that may change.\n\
1907 \n\
1908 To make a hook variable buffer-local, use `make-local-hook',\n\
1909 not `make-local-variable'.")
1910 (nargs, args)
1911 int nargs;
1912 Lisp_Object *args;
1913 {
1914 return run_hook_with_args (nargs, args, to_completion);
1915 }
1916
1917 DEFUN ("run-hook-with-args-until-success",
1918 Frun_hook_with_args_until_success, Srun_hook_with_args_until_success,
1919 1, MANY, 0,
1920 "Run HOOK with the specified arguments ARGS.\n\
1921 HOOK should be a symbol, a hook variable. Its value should\n\
1922 be a list of functions. We call those functions, one by one,\n\
1923 passing arguments ARGS to each of them, until one of them\n\
1924 returns a non-nil value. Then we return that value.\n\
1925 If all the functions return nil, we return nil.\n\
1926 \n\
1927 To make a hook variable buffer-local, use `make-local-hook',\n\
1928 not `make-local-variable'.")
1929 (nargs, args)
1930 int nargs;
1931 Lisp_Object *args;
1932 {
1933 return run_hook_with_args (nargs, args, until_success);
1934 }
1935
1936 DEFUN ("run-hook-with-args-until-failure",
1937 Frun_hook_with_args_until_failure, Srun_hook_with_args_until_failure,
1938 1, MANY, 0,
1939 "Run HOOK with the specified arguments ARGS.\n\
1940 HOOK should be a symbol, a hook variable. Its value should\n\
1941 be a list of functions. We call those functions, one by one,\n\
1942 passing arguments ARGS to each of them, until one of them\n\
1943 returns nil. Then we return nil.\n\
1944 If all the functions return non-nil, we return non-nil.\n\
1945 \n\
1946 To make a hook variable buffer-local, use `make-local-hook',\n\
1947 not `make-local-variable'.")
1948 (nargs, args)
1949 int nargs;
1950 Lisp_Object *args;
1951 {
1952 return run_hook_with_args (nargs, args, until_failure);
1953 }
1954
1955 /* ARGS[0] should be a hook symbol.
1956 Call each of the functions in the hook value, passing each of them
1957 as arguments all the rest of ARGS (all NARGS - 1 elements).
1958 COND specifies a condition to test after each call
1959 to decide whether to stop.
1960 The caller (or its caller, etc) must gcpro all of ARGS,
1961 except that it isn't necessary to gcpro ARGS[0]. */
1962
1963 Lisp_Object
1964 run_hook_with_args (nargs, args, cond)
1965 int nargs;
1966 Lisp_Object *args;
1967 enum run_hooks_condition cond;
1968 {
1969 Lisp_Object sym, val, ret;
1970 struct gcpro gcpro1, gcpro2;
1971
1972 sym = args[0];
1973 val = find_symbol_value (sym);
1974 ret = (cond == until_failure ? Qt : Qnil);
1975
1976 if (EQ (val, Qunbound) || NILP (val))
1977 return ret;
1978 else if (!CONSP (val) || EQ (XCONS (val)->car, Qlambda))
1979 {
1980 args[0] = val;
1981 return Ffuncall (nargs, args);
1982 }
1983 else
1984 {
1985 GCPRO2 (sym, val);
1986
1987 for (;
1988 CONSP (val) && ((cond == to_completion)
1989 || (cond == until_success ? NILP (ret)
1990 : !NILP (ret)));
1991 val = XCONS (val)->cdr)
1992 {
1993 if (EQ (XCONS (val)->car, Qt))
1994 {
1995 /* t indicates this hook has a local binding;
1996 it means to run the global binding too. */
1997 Lisp_Object globals;
1998
1999 for (globals = Fdefault_value (sym);
2000 CONSP (globals) && ((cond == to_completion)
2001 || (cond == until_success ? NILP (ret)
2002 : !NILP (ret)));
2003 globals = XCONS (globals)->cdr)
2004 {
2005 args[0] = XCONS (globals)->car;
2006 /* In a global value, t should not occur. If it does, we
2007 must ignore it to avoid an endless loop. */
2008 if (!EQ (args[0], Qt))
2009 ret = Ffuncall (nargs, args);
2010 }
2011 }
2012 else
2013 {
2014 args[0] = XCONS (val)->car;
2015 ret = Ffuncall (nargs, args);
2016 }
2017 }
2018
2019 UNGCPRO;
2020 return ret;
2021 }
2022 }
2023
2024 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2025 present value of that symbol.
2026 Call each element of FUNLIST,
2027 passing each of them the rest of ARGS.
2028 The caller (or its caller, etc) must gcpro all of ARGS,
2029 except that it isn't necessary to gcpro ARGS[0]. */
2030
2031 Lisp_Object
2032 run_hook_list_with_args (funlist, nargs, args)
2033 Lisp_Object funlist;
2034 int nargs;
2035 Lisp_Object *args;
2036 {
2037 Lisp_Object sym;
2038 Lisp_Object val;
2039 struct gcpro gcpro1, gcpro2;
2040
2041 sym = args[0];
2042 GCPRO2 (sym, val);
2043
2044 for (val = funlist; CONSP (val); val = XCONS (val)->cdr)
2045 {
2046 if (EQ (XCONS (val)->car, Qt))
2047 {
2048 /* t indicates this hook has a local binding;
2049 it means to run the global binding too. */
2050 Lisp_Object globals;
2051
2052 for (globals = Fdefault_value (sym);
2053 CONSP (globals);
2054 globals = XCONS (globals)->cdr)
2055 {
2056 args[0] = XCONS (globals)->car;
2057 /* In a global value, t should not occur. If it does, we
2058 must ignore it to avoid an endless loop. */
2059 if (!EQ (args[0], Qt))
2060 Ffuncall (nargs, args);
2061 }
2062 }
2063 else
2064 {
2065 args[0] = XCONS (val)->car;
2066 Ffuncall (nargs, args);
2067 }
2068 }
2069 UNGCPRO;
2070 return Qnil;
2071 }
2072
2073 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2074
2075 void
2076 run_hook_with_args_2 (hook, arg1, arg2)
2077 Lisp_Object hook, arg1, arg2;
2078 {
2079 Lisp_Object temp[3];
2080 temp[0] = hook;
2081 temp[1] = arg1;
2082 temp[2] = arg2;
2083
2084 Frun_hook_with_args (3, temp);
2085 }
2086 \f
2087 /* Apply fn to arg */
2088 Lisp_Object
2089 apply1 (fn, arg)
2090 Lisp_Object fn, arg;
2091 {
2092 struct gcpro gcpro1;
2093
2094 GCPRO1 (fn);
2095 if (NILP (arg))
2096 RETURN_UNGCPRO (Ffuncall (1, &fn));
2097 gcpro1.nvars = 2;
2098 #ifdef NO_ARG_ARRAY
2099 {
2100 Lisp_Object args[2];
2101 args[0] = fn;
2102 args[1] = arg;
2103 gcpro1.var = args;
2104 RETURN_UNGCPRO (Fapply (2, args));
2105 }
2106 #else /* not NO_ARG_ARRAY */
2107 RETURN_UNGCPRO (Fapply (2, &fn));
2108 #endif /* not NO_ARG_ARRAY */
2109 }
2110
2111 /* Call function fn on no arguments */
2112 Lisp_Object
2113 call0 (fn)
2114 Lisp_Object fn;
2115 {
2116 struct gcpro gcpro1;
2117
2118 GCPRO1 (fn);
2119 RETURN_UNGCPRO (Ffuncall (1, &fn));
2120 }
2121
2122 /* Call function fn with 1 argument arg1 */
2123 /* ARGSUSED */
2124 Lisp_Object
2125 call1 (fn, arg1)
2126 Lisp_Object fn, arg1;
2127 {
2128 struct gcpro gcpro1;
2129 #ifdef NO_ARG_ARRAY
2130 Lisp_Object args[2];
2131
2132 args[0] = fn;
2133 args[1] = arg1;
2134 GCPRO1 (args[0]);
2135 gcpro1.nvars = 2;
2136 RETURN_UNGCPRO (Ffuncall (2, args));
2137 #else /* not NO_ARG_ARRAY */
2138 GCPRO1 (fn);
2139 gcpro1.nvars = 2;
2140 RETURN_UNGCPRO (Ffuncall (2, &fn));
2141 #endif /* not NO_ARG_ARRAY */
2142 }
2143
2144 /* Call function fn with 2 arguments arg1, arg2 */
2145 /* ARGSUSED */
2146 Lisp_Object
2147 call2 (fn, arg1, arg2)
2148 Lisp_Object fn, arg1, arg2;
2149 {
2150 struct gcpro gcpro1;
2151 #ifdef NO_ARG_ARRAY
2152 Lisp_Object args[3];
2153 args[0] = fn;
2154 args[1] = arg1;
2155 args[2] = arg2;
2156 GCPRO1 (args[0]);
2157 gcpro1.nvars = 3;
2158 RETURN_UNGCPRO (Ffuncall (3, args));
2159 #else /* not NO_ARG_ARRAY */
2160 GCPRO1 (fn);
2161 gcpro1.nvars = 3;
2162 RETURN_UNGCPRO (Ffuncall (3, &fn));
2163 #endif /* not NO_ARG_ARRAY */
2164 }
2165
2166 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2167 /* ARGSUSED */
2168 Lisp_Object
2169 call3 (fn, arg1, arg2, arg3)
2170 Lisp_Object fn, arg1, arg2, arg3;
2171 {
2172 struct gcpro gcpro1;
2173 #ifdef NO_ARG_ARRAY
2174 Lisp_Object args[4];
2175 args[0] = fn;
2176 args[1] = arg1;
2177 args[2] = arg2;
2178 args[3] = arg3;
2179 GCPRO1 (args[0]);
2180 gcpro1.nvars = 4;
2181 RETURN_UNGCPRO (Ffuncall (4, args));
2182 #else /* not NO_ARG_ARRAY */
2183 GCPRO1 (fn);
2184 gcpro1.nvars = 4;
2185 RETURN_UNGCPRO (Ffuncall (4, &fn));
2186 #endif /* not NO_ARG_ARRAY */
2187 }
2188
2189 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2190 /* ARGSUSED */
2191 Lisp_Object
2192 call4 (fn, arg1, arg2, arg3, arg4)
2193 Lisp_Object fn, arg1, arg2, arg3, arg4;
2194 {
2195 struct gcpro gcpro1;
2196 #ifdef NO_ARG_ARRAY
2197 Lisp_Object args[5];
2198 args[0] = fn;
2199 args[1] = arg1;
2200 args[2] = arg2;
2201 args[3] = arg3;
2202 args[4] = arg4;
2203 GCPRO1 (args[0]);
2204 gcpro1.nvars = 5;
2205 RETURN_UNGCPRO (Ffuncall (5, args));
2206 #else /* not NO_ARG_ARRAY */
2207 GCPRO1 (fn);
2208 gcpro1.nvars = 5;
2209 RETURN_UNGCPRO (Ffuncall (5, &fn));
2210 #endif /* not NO_ARG_ARRAY */
2211 }
2212
2213 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2214 /* ARGSUSED */
2215 Lisp_Object
2216 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2217 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2218 {
2219 struct gcpro gcpro1;
2220 #ifdef NO_ARG_ARRAY
2221 Lisp_Object args[6];
2222 args[0] = fn;
2223 args[1] = arg1;
2224 args[2] = arg2;
2225 args[3] = arg3;
2226 args[4] = arg4;
2227 args[5] = arg5;
2228 GCPRO1 (args[0]);
2229 gcpro1.nvars = 6;
2230 RETURN_UNGCPRO (Ffuncall (6, args));
2231 #else /* not NO_ARG_ARRAY */
2232 GCPRO1 (fn);
2233 gcpro1.nvars = 6;
2234 RETURN_UNGCPRO (Ffuncall (6, &fn));
2235 #endif /* not NO_ARG_ARRAY */
2236 }
2237
2238 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2239 /* ARGSUSED */
2240 Lisp_Object
2241 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2242 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2243 {
2244 struct gcpro gcpro1;
2245 #ifdef NO_ARG_ARRAY
2246 Lisp_Object args[7];
2247 args[0] = fn;
2248 args[1] = arg1;
2249 args[2] = arg2;
2250 args[3] = arg3;
2251 args[4] = arg4;
2252 args[5] = arg5;
2253 args[6] = arg6;
2254 GCPRO1 (args[0]);
2255 gcpro1.nvars = 7;
2256 RETURN_UNGCPRO (Ffuncall (7, args));
2257 #else /* not NO_ARG_ARRAY */
2258 GCPRO1 (fn);
2259 gcpro1.nvars = 7;
2260 RETURN_UNGCPRO (Ffuncall (7, &fn));
2261 #endif /* not NO_ARG_ARRAY */
2262 }
2263
2264 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2265 "Call first argument as a function, passing remaining arguments to it.\n\
2266 Return the value that function returns.\n\
2267 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2268 (nargs, args)
2269 int nargs;
2270 Lisp_Object *args;
2271 {
2272 Lisp_Object fun;
2273 Lisp_Object funcar;
2274 int numargs = nargs - 1;
2275 Lisp_Object lisp_numargs;
2276 Lisp_Object val;
2277 struct backtrace backtrace;
2278 register Lisp_Object *internal_args;
2279 register int i;
2280
2281 QUIT;
2282 if (consing_since_gc > gc_cons_threshold)
2283 Fgarbage_collect ();
2284
2285 if (++lisp_eval_depth > max_lisp_eval_depth)
2286 {
2287 if (max_lisp_eval_depth < 100)
2288 max_lisp_eval_depth = 100;
2289 if (lisp_eval_depth > max_lisp_eval_depth)
2290 error ("Lisp nesting exceeds max-lisp-eval-depth");
2291 }
2292
2293 backtrace.next = backtrace_list;
2294 backtrace_list = &backtrace;
2295 backtrace.function = &args[0];
2296 backtrace.args = &args[1];
2297 backtrace.nargs = nargs - 1;
2298 backtrace.evalargs = 0;
2299 backtrace.debug_on_exit = 0;
2300
2301 if (debug_on_next_call)
2302 do_debug_on_call (Qlambda);
2303
2304 retry:
2305
2306 fun = args[0];
2307
2308 fun = Findirect_function (fun);
2309
2310 if (SUBRP (fun))
2311 {
2312 if (numargs < XSUBR (fun)->min_args
2313 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2314 {
2315 XSETFASTINT (lisp_numargs, numargs);
2316 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2317 }
2318
2319 if (XSUBR (fun)->max_args == UNEVALLED)
2320 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2321
2322 if (XSUBR (fun)->max_args == MANY)
2323 {
2324 val = (*XSUBR (fun)->function) (numargs, args + 1);
2325 goto done;
2326 }
2327
2328 if (XSUBR (fun)->max_args > numargs)
2329 {
2330 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2331 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2332 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2333 internal_args[i] = Qnil;
2334 }
2335 else
2336 internal_args = args + 1;
2337 switch (XSUBR (fun)->max_args)
2338 {
2339 case 0:
2340 val = (*XSUBR (fun)->function) ();
2341 goto done;
2342 case 1:
2343 val = (*XSUBR (fun)->function) (internal_args[0]);
2344 goto done;
2345 case 2:
2346 val = (*XSUBR (fun)->function) (internal_args[0],
2347 internal_args[1]);
2348 goto done;
2349 case 3:
2350 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2351 internal_args[2]);
2352 goto done;
2353 case 4:
2354 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2355 internal_args[2],
2356 internal_args[3]);
2357 goto done;
2358 case 5:
2359 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2360 internal_args[2], internal_args[3],
2361 internal_args[4]);
2362 goto done;
2363 case 6:
2364 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2365 internal_args[2], internal_args[3],
2366 internal_args[4], internal_args[5]);
2367 goto done;
2368 case 7:
2369 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2370 internal_args[2], internal_args[3],
2371 internal_args[4], internal_args[5],
2372 internal_args[6]);
2373 goto done;
2374
2375 default:
2376
2377 /* If a subr takes more than 6 arguments without using MANY
2378 or UNEVALLED, we need to extend this function to support it.
2379 Until this is done, there is no way to call the function. */
2380 abort ();
2381 }
2382 }
2383 if (COMPILEDP (fun))
2384 val = funcall_lambda (fun, numargs, args + 1);
2385 else
2386 {
2387 if (!CONSP (fun))
2388 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2389 funcar = Fcar (fun);
2390 if (!SYMBOLP (funcar))
2391 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2392 if (EQ (funcar, Qlambda))
2393 val = funcall_lambda (fun, numargs, args + 1);
2394 else if (EQ (funcar, Qmocklisp))
2395 val = ml_apply (fun, Flist (numargs, args + 1));
2396 else if (EQ (funcar, Qautoload))
2397 {
2398 do_autoload (fun, args[0]);
2399 goto retry;
2400 }
2401 else
2402 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2403 }
2404 done:
2405 lisp_eval_depth--;
2406 if (backtrace.debug_on_exit)
2407 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2408 backtrace_list = backtrace.next;
2409 return val;
2410 }
2411 \f
2412 Lisp_Object
2413 apply_lambda (fun, args, eval_flag)
2414 Lisp_Object fun, args;
2415 int eval_flag;
2416 {
2417 Lisp_Object args_left;
2418 Lisp_Object numargs;
2419 register Lisp_Object *arg_vector;
2420 struct gcpro gcpro1, gcpro2, gcpro3;
2421 register int i;
2422 register Lisp_Object tem;
2423
2424 numargs = Flength (args);
2425 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2426 args_left = args;
2427
2428 GCPRO3 (*arg_vector, args_left, fun);
2429 gcpro1.nvars = 0;
2430
2431 for (i = 0; i < XINT (numargs);)
2432 {
2433 tem = Fcar (args_left), args_left = Fcdr (args_left);
2434 if (eval_flag) tem = Feval (tem);
2435 arg_vector[i++] = tem;
2436 gcpro1.nvars = i;
2437 }
2438
2439 UNGCPRO;
2440
2441 if (eval_flag)
2442 {
2443 backtrace_list->args = arg_vector;
2444 backtrace_list->nargs = i;
2445 }
2446 backtrace_list->evalargs = 0;
2447 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2448
2449 /* Do the debug-on-exit now, while arg_vector still exists. */
2450 if (backtrace_list->debug_on_exit)
2451 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2452 /* Don't do it again when we return to eval. */
2453 backtrace_list->debug_on_exit = 0;
2454 return tem;
2455 }
2456
2457 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2458 and return the result of evaluation.
2459 FUN must be either a lambda-expression or a compiled-code object. */
2460
2461 Lisp_Object
2462 funcall_lambda (fun, nargs, arg_vector)
2463 Lisp_Object fun;
2464 int nargs;
2465 register Lisp_Object *arg_vector;
2466 {
2467 Lisp_Object val, tem;
2468 register Lisp_Object syms_left;
2469 Lisp_Object numargs;
2470 register Lisp_Object next;
2471 int count = specpdl_ptr - specpdl;
2472 register int i;
2473 int optional = 0, rest = 0;
2474
2475 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2476
2477 XSETFASTINT (numargs, nargs);
2478
2479 if (CONSP (fun))
2480 syms_left = Fcar (Fcdr (fun));
2481 else if (COMPILEDP (fun))
2482 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2483 else abort ();
2484
2485 i = 0;
2486 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
2487 {
2488 QUIT;
2489 next = Fcar (syms_left);
2490 while (!SYMBOLP (next))
2491 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2492 if (EQ (next, Qand_rest))
2493 rest = 1;
2494 else if (EQ (next, Qand_optional))
2495 optional = 1;
2496 else if (rest)
2497 {
2498 specbind (next, Flist (nargs - i, &arg_vector[i]));
2499 i = nargs;
2500 }
2501 else if (i < nargs)
2502 {
2503 tem = arg_vector[i++];
2504 specbind (next, tem);
2505 }
2506 else if (!optional)
2507 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2508 else
2509 specbind (next, Qnil);
2510 }
2511
2512 if (i < nargs)
2513 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2514
2515 if (CONSP (fun))
2516 val = Fprogn (Fcdr (Fcdr (fun)));
2517 else
2518 {
2519 /* If we have not actually read the bytecode string
2520 and constants vector yet, fetch them from the file. */
2521 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2522 Ffetch_bytecode (fun);
2523 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2524 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2525 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2526 }
2527 return unbind_to (count, val);
2528 }
2529
2530 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2531 1, 1, 0,
2532 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2533 (object)
2534 Lisp_Object object;
2535 {
2536 Lisp_Object tem;
2537
2538 if (COMPILEDP (object)
2539 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2540 {
2541 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2542 if (!CONSP (tem))
2543 error ("invalid byte code");
2544 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCONS (tem)->car;
2545 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCONS (tem)->cdr;
2546 }
2547 return object;
2548 }
2549 \f
2550 void
2551 grow_specpdl ()
2552 {
2553 register int count = specpdl_ptr - specpdl;
2554 if (specpdl_size >= max_specpdl_size)
2555 {
2556 if (max_specpdl_size < 400)
2557 max_specpdl_size = 400;
2558 if (specpdl_size >= max_specpdl_size)
2559 {
2560 if (!NILP (Vdebug_on_error))
2561 /* Leave room for some specpdl in the debugger. */
2562 max_specpdl_size = specpdl_size + 100;
2563 Fsignal (Qerror,
2564 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2565 }
2566 }
2567 specpdl_size *= 2;
2568 if (specpdl_size > max_specpdl_size)
2569 specpdl_size = max_specpdl_size;
2570 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2571 specpdl_ptr = specpdl + count;
2572 }
2573
2574 void
2575 specbind (symbol, value)
2576 Lisp_Object symbol, value;
2577 {
2578 Lisp_Object ovalue;
2579
2580 CHECK_SYMBOL (symbol, 0);
2581
2582 if (specpdl_ptr == specpdl + specpdl_size)
2583 grow_specpdl ();
2584 specpdl_ptr->symbol = symbol;
2585 specpdl_ptr->func = 0;
2586 specpdl_ptr->old_value = ovalue = find_symbol_value (symbol);
2587 specpdl_ptr++;
2588 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2589 store_symval_forwarding (symbol, ovalue, value);
2590 else
2591 Fset (symbol, value);
2592 }
2593
2594 void
2595 record_unwind_protect (function, arg)
2596 Lisp_Object (*function)();
2597 Lisp_Object arg;
2598 {
2599 if (specpdl_ptr == specpdl + specpdl_size)
2600 grow_specpdl ();
2601 specpdl_ptr->func = function;
2602 specpdl_ptr->symbol = Qnil;
2603 specpdl_ptr->old_value = arg;
2604 specpdl_ptr++;
2605 }
2606
2607 Lisp_Object
2608 unbind_to (count, value)
2609 int count;
2610 Lisp_Object value;
2611 {
2612 int quitf = !NILP (Vquit_flag);
2613 struct gcpro gcpro1;
2614
2615 GCPRO1 (value);
2616
2617 Vquit_flag = Qnil;
2618
2619 while (specpdl_ptr != specpdl + count)
2620 {
2621 --specpdl_ptr;
2622 if (specpdl_ptr->func != 0)
2623 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2624 /* Note that a "binding" of nil is really an unwind protect,
2625 so in that case the "old value" is a list of forms to evaluate. */
2626 else if (NILP (specpdl_ptr->symbol))
2627 Fprogn (specpdl_ptr->old_value);
2628 else
2629 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
2630 }
2631 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
2632
2633 UNGCPRO;
2634
2635 return value;
2636 }
2637 \f
2638 #if 0
2639
2640 /* Get the value of symbol's global binding, even if that binding
2641 is not now dynamically visible. */
2642
2643 Lisp_Object
2644 top_level_value (symbol)
2645 Lisp_Object symbol;
2646 {
2647 register struct specbinding *ptr = specpdl;
2648
2649 CHECK_SYMBOL (symbol, 0);
2650 for (; ptr != specpdl_ptr; ptr++)
2651 {
2652 if (EQ (ptr->symbol, symbol))
2653 return ptr->old_value;
2654 }
2655 return Fsymbol_value (symbol);
2656 }
2657
2658 Lisp_Object
2659 top_level_set (symbol, newval)
2660 Lisp_Object symbol, newval;
2661 {
2662 register struct specbinding *ptr = specpdl;
2663
2664 CHECK_SYMBOL (symbol, 0);
2665 for (; ptr != specpdl_ptr; ptr++)
2666 {
2667 if (EQ (ptr->symbol, symbol))
2668 {
2669 ptr->old_value = newval;
2670 return newval;
2671 }
2672 }
2673 return Fset (symbol, newval);
2674 }
2675
2676 #endif /* 0 */
2677 \f
2678 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2679 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2680 The debugger is entered when that frame exits, if the flag is non-nil.")
2681 (level, flag)
2682 Lisp_Object level, flag;
2683 {
2684 register struct backtrace *backlist = backtrace_list;
2685 register int i;
2686
2687 CHECK_NUMBER (level, 0);
2688
2689 for (i = 0; backlist && i < XINT (level); i++)
2690 {
2691 backlist = backlist->next;
2692 }
2693
2694 if (backlist)
2695 backlist->debug_on_exit = !NILP (flag);
2696
2697 return flag;
2698 }
2699
2700 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2701 "Print a trace of Lisp function calls currently active.\n\
2702 Output stream used is value of `standard-output'.")
2703 ()
2704 {
2705 register struct backtrace *backlist = backtrace_list;
2706 register int i;
2707 Lisp_Object tail;
2708 Lisp_Object tem;
2709 extern Lisp_Object Vprint_level;
2710 struct gcpro gcpro1;
2711
2712 XSETFASTINT (Vprint_level, 3);
2713
2714 tail = Qnil;
2715 GCPRO1 (tail);
2716
2717 while (backlist)
2718 {
2719 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2720 if (backlist->nargs == UNEVALLED)
2721 {
2722 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2723 write_string ("\n", -1);
2724 }
2725 else
2726 {
2727 tem = *backlist->function;
2728 Fprin1 (tem, Qnil); /* This can QUIT */
2729 write_string ("(", -1);
2730 if (backlist->nargs == MANY)
2731 {
2732 for (tail = *backlist->args, i = 0;
2733 !NILP (tail);
2734 tail = Fcdr (tail), i++)
2735 {
2736 if (i) write_string (" ", -1);
2737 Fprin1 (Fcar (tail), Qnil);
2738 }
2739 }
2740 else
2741 {
2742 for (i = 0; i < backlist->nargs; i++)
2743 {
2744 if (i) write_string (" ", -1);
2745 Fprin1 (backlist->args[i], Qnil);
2746 }
2747 }
2748 write_string (")\n", -1);
2749 }
2750 backlist = backlist->next;
2751 }
2752
2753 Vprint_level = Qnil;
2754 UNGCPRO;
2755 return Qnil;
2756 }
2757
2758 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2759 "Return the function and arguments N frames up from current execution point.\n\
2760 If that frame has not evaluated the arguments yet (or is a special form),\n\
2761 the value is (nil FUNCTION ARG-FORMS...).\n\
2762 If that frame has evaluated its arguments and called its function already,\n\
2763 the value is (t FUNCTION ARG-VALUES...).\n\
2764 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2765 FUNCTION is whatever was supplied as car of evaluated list,\n\
2766 or a lambda expression for macro calls.\n\
2767 If N is more than the number of frames, the value is nil.")
2768 (nframes)
2769 Lisp_Object nframes;
2770 {
2771 register struct backtrace *backlist = backtrace_list;
2772 register int i;
2773 Lisp_Object tem;
2774
2775 CHECK_NATNUM (nframes, 0);
2776
2777 /* Find the frame requested. */
2778 for (i = 0; backlist && i < XFASTINT (nframes); i++)
2779 backlist = backlist->next;
2780
2781 if (!backlist)
2782 return Qnil;
2783 if (backlist->nargs == UNEVALLED)
2784 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2785 else
2786 {
2787 if (backlist->nargs == MANY)
2788 tem = *backlist->args;
2789 else
2790 tem = Flist (backlist->nargs, backlist->args);
2791
2792 return Fcons (Qt, Fcons (*backlist->function, tem));
2793 }
2794 }
2795 \f
2796 syms_of_eval ()
2797 {
2798 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2799 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2800
2801 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2802 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2803 This limit is to catch infinite recursions for you before they cause\n\
2804 actual stack overflow in C, which would be fatal for Emacs.\n\
2805 You can safely make it considerably larger than its default value,\n\
2806 if that proves inconveniently small.");
2807
2808 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2809 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2810 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2811 Vquit_flag = Qnil;
2812
2813 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2814 "Non-nil inhibits C-g quitting from happening immediately.\n\
2815 Note that `quit-flag' will still be set by typing C-g,\n\
2816 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
2817 To prevent this happening, set `quit-flag' to nil\n\
2818 before making `inhibit-quit' nil.");
2819 Vinhibit_quit = Qnil;
2820
2821 Qinhibit_quit = intern ("inhibit-quit");
2822 staticpro (&Qinhibit_quit);
2823
2824 Qautoload = intern ("autoload");
2825 staticpro (&Qautoload);
2826
2827 Qdebug_on_error = intern ("debug-on-error");
2828 staticpro (&Qdebug_on_error);
2829
2830 Qmacro = intern ("macro");
2831 staticpro (&Qmacro);
2832
2833 /* Note that the process handling also uses Qexit, but we don't want
2834 to staticpro it twice, so we just do it here. */
2835 Qexit = intern ("exit");
2836 staticpro (&Qexit);
2837
2838 Qinteractive = intern ("interactive");
2839 staticpro (&Qinteractive);
2840
2841 Qcommandp = intern ("commandp");
2842 staticpro (&Qcommandp);
2843
2844 Qdefun = intern ("defun");
2845 staticpro (&Qdefun);
2846
2847 Qand_rest = intern ("&rest");
2848 staticpro (&Qand_rest);
2849
2850 Qand_optional = intern ("&optional");
2851 staticpro (&Qand_optional);
2852
2853 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
2854 "*Non-nil means automatically display a backtrace buffer\n\
2855 after any error that is handled by the editor command loop.\n\
2856 If the value is a list, an error only means to display a backtrace\n\
2857 if one of its condition symbols appears in the list.");
2858 Vstack_trace_on_error = Qnil;
2859
2860 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
2861 "*Non-nil means enter debugger if an error is signaled.\n\
2862 Does not apply to errors handled by `condition-case'.\n\
2863 If the value is a list, an error only means to enter the debugger\n\
2864 if one of its condition symbols appears in the list.\n\
2865 See also variable `debug-on-quit'.");
2866 Vdebug_on_error = Qnil;
2867
2868 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
2869 "*List of errors for which the debugger should not be called.\n\
2870 Each element may be a condition-name or a regexp that matches error messages.\n\
2871 If any element applies to a given error, that error skips the debugger\n\
2872 and just returns to top level.\n\
2873 This overrides the variable `debug-on-error'.\n\
2874 It does not apply to errors handled by `condition-case'.");
2875 Vdebug_ignored_errors = Qnil;
2876
2877 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
2878 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
2879 Does not apply if quit is handled by a `condition-case'.");
2880 debug_on_quit = 0;
2881
2882 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
2883 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2884
2885 DEFVAR_LISP ("debugger", &Vdebugger,
2886 "Function to call to invoke debugger.\n\
2887 If due to frame exit, args are `exit' and the value being returned;\n\
2888 this function's value will be returned instead of that.\n\
2889 If due to error, args are `error' and a list of the args to `signal'.\n\
2890 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2891 If due to `eval' entry, one arg, t.");
2892 Vdebugger = Qnil;
2893
2894 Qmocklisp_arguments = intern ("mocklisp-arguments");
2895 staticpro (&Qmocklisp_arguments);
2896 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
2897 "While in a mocklisp function, the list of its unevaluated args.");
2898 Vmocklisp_arguments = Qt;
2899
2900 DEFVAR_LISP ("run-hooks", &Vrun_hooks,
2901 "Set to the function `run-hooks', if that function has been defined.\n\
2902 Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
2903
2904 staticpro (&Vautoload_queue);
2905 Vautoload_queue = Qnil;
2906
2907 defsubr (&Sor);
2908 defsubr (&Sand);
2909 defsubr (&Sif);
2910 defsubr (&Scond);
2911 defsubr (&Sprogn);
2912 defsubr (&Sprog1);
2913 defsubr (&Sprog2);
2914 defsubr (&Ssetq);
2915 defsubr (&Squote);
2916 defsubr (&Sfunction);
2917 defsubr (&Sdefun);
2918 defsubr (&Sdefmacro);
2919 defsubr (&Sdefvar);
2920 defsubr (&Sdefconst);
2921 defsubr (&Suser_variable_p);
2922 defsubr (&Slet);
2923 defsubr (&SletX);
2924 defsubr (&Swhile);
2925 defsubr (&Smacroexpand);
2926 defsubr (&Scatch);
2927 defsubr (&Sthrow);
2928 defsubr (&Sunwind_protect);
2929 defsubr (&Scondition_case);
2930 defsubr (&Ssignal);
2931 defsubr (&Sinteractive_p);
2932 defsubr (&Scommandp);
2933 defsubr (&Sautoload);
2934 defsubr (&Seval);
2935 defsubr (&Sapply);
2936 defsubr (&Sfuncall);
2937 defsubr (&Srun_hooks);
2938 defsubr (&Srun_hook_with_args);
2939 defsubr (&Srun_hook_with_args_until_success);
2940 defsubr (&Srun_hook_with_args_until_failure);
2941 defsubr (&Sfetch_bytecode);
2942 defsubr (&Sbacktrace_debug);
2943 defsubr (&Sbacktrace);
2944 defsubr (&Sbacktrace_frame);
2945 }