*** empty log message ***
[emacs.git] / src / eval.c
blob0e012d45008b93178356335bef0d4c06f92e74a6
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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 1, or (at your option)
9 any later version.
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.
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. */
21 #include "config.h"
22 #include "lisp.h"
23 #ifdef HAVE_X_WINDOWS
24 #include "xterm.h"
25 #endif
27 #ifndef standalone
28 #include "commands.h"
29 #include "keyboard.h"
30 #else
31 #define INTERACTIVE 1
32 #endif
34 #include <setjmp.h>
36 /* This definition is duplicated in alloc.c and keyboard.c */
37 /* Putting it in lisp.h makes cc bomb out! */
39 struct backtrace
41 struct backtrace *next;
42 Lisp_Object *function;
43 Lisp_Object *args; /* Points to vector of args. */
44 int nargs; /* Length of vector.
45 If nargs is UNEVALLED, args points to slot holding
46 list of unevalled args */
47 char evalargs;
48 /* Nonzero means call value of debugger when done with this operation. */
49 char debug_on_exit;
52 struct backtrace *backtrace_list;
54 struct catchtag
56 Lisp_Object tag;
57 Lisp_Object val;
58 struct catchtag *next;
59 struct gcpro *gcpro;
60 jmp_buf jmp;
61 struct backtrace *backlist;
62 struct handler *handlerlist;
63 int lisp_eval_depth;
64 int pdlcount;
65 int poll_suppress_count;
68 struct catchtag *catchlist;
70 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
71 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
72 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
73 Lisp_Object Qand_rest, Qand_optional;
74 Lisp_Object Qdebug_on_error;
76 Lisp_Object Vrun_hooks;
78 /* Non-nil means record all fset's and provide's, to be undone
79 if the file being autoloaded is not fully loaded.
80 They are recorded by being consed onto the front of Vautoload_queue:
81 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
83 Lisp_Object Vautoload_queue;
85 /* Current number of specbindings allocated in specpdl. */
86 int specpdl_size;
88 /* Pointer to beginning of specpdl. */
89 struct specbinding *specpdl;
91 /* Pointer to first unused element in specpdl. */
92 struct specbinding *specpdl_ptr;
94 /* Maximum size allowed for specpdl allocation */
95 int max_specpdl_size;
97 /* Depth in Lisp evaluations and function calls. */
98 int lisp_eval_depth;
100 /* Maximum allowed depth in Lisp evaluations and function calls. */
101 int max_lisp_eval_depth;
103 /* Nonzero means enter debugger before next function call */
104 int debug_on_next_call;
106 /* List of conditions (non-nil atom means all) which cause a backtrace
107 if an error is handled by the command loop's error handler. */
108 Lisp_Object Vstack_trace_on_error;
110 /* List of conditions (non-nil atom means all) which enter the debugger
111 if an error is handled by the command loop's error handler. */
112 Lisp_Object Vdebug_on_error;
114 /* Nonzero means enter debugger if a quit signal
115 is handled by the command loop's error handler. */
116 int debug_on_quit;
118 /* Nonzero means we are trying to enter the debugger.
119 This is to prevent recursive attempts. */
120 int entering_debugger;
122 Lisp_Object Vdebugger;
124 void specbind (), record_unwind_protect ();
126 Lisp_Object funcall_lambda ();
127 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
129 init_eval_once ()
131 specpdl_size = 50;
132 specpdl = (struct specbinding *) malloc (specpdl_size * sizeof (struct specbinding));
133 max_specpdl_size = 600;
134 max_lisp_eval_depth = 200;
137 init_eval ()
139 specpdl_ptr = specpdl;
140 catchlist = 0;
141 handlerlist = 0;
142 backtrace_list = 0;
143 Vquit_flag = Qnil;
144 debug_on_next_call = 0;
145 lisp_eval_depth = 0;
146 entering_debugger = 0;
149 Lisp_Object
150 call_debugger (arg)
151 Lisp_Object arg;
153 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
154 max_lisp_eval_depth = lisp_eval_depth + 20;
155 if (specpdl_size + 40 > max_specpdl_size)
156 max_specpdl_size = specpdl_size + 40;
157 debug_on_next_call = 0;
158 entering_debugger = 1;
159 return apply1 (Vdebugger, arg);
162 do_debug_on_call (code)
163 Lisp_Object code;
165 debug_on_next_call = 0;
166 backtrace_list->debug_on_exit = 1;
167 call_debugger (Fcons (code, Qnil));
170 /* NOTE!!! Every function that can call EVAL must protect its args
171 and temporaries from garbage collection while it needs them.
172 The definition of `For' shows what you have to do. */
174 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
175 "Eval args until one of them yields non-nil, then return that value.\n\
176 The remaining args are not evalled at all.\n\
177 If all args return nil, return nil.")
178 (args)
179 Lisp_Object args;
181 register Lisp_Object val;
182 Lisp_Object args_left;
183 struct gcpro gcpro1;
185 if (NILP(args))
186 return Qnil;
188 args_left = args;
189 GCPRO1 (args_left);
193 val = Feval (Fcar (args_left));
194 if (!NILP (val))
195 break;
196 args_left = Fcdr (args_left);
198 while (!NILP(args_left));
200 UNGCPRO;
201 return val;
204 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
205 "Eval args until one of them yields nil, then return nil.\n\
206 The remaining args are not evalled at all.\n\
207 If no arg yields nil, return the last arg's value.")
208 (args)
209 Lisp_Object args;
211 register Lisp_Object val;
212 Lisp_Object args_left;
213 struct gcpro gcpro1;
215 if (NILP(args))
216 return Qt;
218 args_left = args;
219 GCPRO1 (args_left);
223 val = Feval (Fcar (args_left));
224 if (NILP (val))
225 break;
226 args_left = Fcdr (args_left);
228 while (!NILP(args_left));
230 UNGCPRO;
231 return val;
234 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
235 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
236 Returns the value of THEN or the value of the last of the ELSE's.\n\
237 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
238 If COND yields nil, and there are no ELSE's, the value is nil.")
239 (args)
240 Lisp_Object args;
242 register Lisp_Object cond;
243 struct gcpro gcpro1;
245 GCPRO1 (args);
246 cond = Feval (Fcar (args));
247 UNGCPRO;
249 if (!NILP (cond))
250 return Feval (Fcar (Fcdr (args)));
251 return Fprogn (Fcdr (Fcdr (args)));
254 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
255 "(cond CLAUSES...): try each clause until one succeeds.\n\
256 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
257 and, if the value is non-nil, this clause succeeds:\n\
258 then the expressions in BODY are evaluated and the last one's\n\
259 value is the value of the cond-form.\n\
260 If no clause succeeds, cond returns nil.\n\
261 If a clause has one element, as in (CONDITION),\n\
262 CONDITION's value if non-nil is returned from the cond-form.")
263 (args)
264 Lisp_Object args;
266 register Lisp_Object clause, val;
267 struct gcpro gcpro1;
269 val = Qnil;
270 GCPRO1 (args);
271 while (!NILP (args))
273 clause = Fcar (args);
274 val = Feval (Fcar (clause));
275 if (!NILP (val))
277 if (!EQ (XCONS (clause)->cdr, Qnil))
278 val = Fprogn (XCONS (clause)->cdr);
279 break;
281 args = XCONS (args)->cdr;
283 UNGCPRO;
285 return val;
288 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
289 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
290 (args)
291 Lisp_Object args;
293 register Lisp_Object val, tem;
294 Lisp_Object args_left;
295 struct gcpro gcpro1;
297 /* In Mocklisp code, symbols at the front of the progn arglist
298 are to be bound to zero. */
299 if (!EQ (Vmocklisp_arguments, Qt))
301 val = make_number (0);
302 while (!NILP (args) && (tem = Fcar (args), XTYPE (tem) == Lisp_Symbol))
304 QUIT;
305 specbind (tem, val), args = Fcdr (args);
309 if (NILP(args))
310 return Qnil;
312 args_left = args;
313 GCPRO1 (args_left);
317 val = Feval (Fcar (args_left));
318 args_left = Fcdr (args_left);
320 while (!NILP(args_left));
322 UNGCPRO;
323 return val;
326 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
327 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
328 The value of FIRST is saved during the evaluation of the remaining args,\n\
329 whose values are discarded.")
330 (args)
331 Lisp_Object args;
333 Lisp_Object val;
334 register Lisp_Object args_left;
335 struct gcpro gcpro1, gcpro2;
336 register int argnum = 0;
338 if (NILP(args))
339 return Qnil;
341 args_left = args;
342 val = Qnil;
343 GCPRO2 (args, val);
347 if (!(argnum++))
348 val = Feval (Fcar (args_left));
349 else
350 Feval (Fcar (args_left));
351 args_left = Fcdr (args_left);
353 while (!NILP(args_left));
355 UNGCPRO;
356 return val;
359 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
360 "(prog1 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
361 The value of Y is saved during the evaluation of the remaining args,\n\
362 whose values are discarded.")
363 (args)
364 Lisp_Object args;
366 Lisp_Object val;
367 register Lisp_Object args_left;
368 struct gcpro gcpro1, gcpro2;
369 register int argnum = -1;
371 val = Qnil;
373 if (NILP(args))
374 return Qnil;
376 args_left = args;
377 val = Qnil;
378 GCPRO2 (args, val);
382 if (!(argnum++))
383 val = Feval (Fcar (args_left));
384 else
385 Feval (Fcar (args_left));
386 args_left = Fcdr (args_left);
388 while (!NILP(args_left));
390 UNGCPRO;
391 return val;
394 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
395 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
396 The SYMs are not evaluated. Thus (setq x y) sets x to the value of y.\n\
397 Each SYM is set before the next VAL is computed.")
398 (args)
399 Lisp_Object args;
401 register Lisp_Object args_left;
402 register Lisp_Object val, sym;
403 struct gcpro gcpro1;
405 if (NILP(args))
406 return Qnil;
408 args_left = args;
409 GCPRO1 (args);
413 val = Feval (Fcar (Fcdr (args_left)));
414 sym = Fcar (args_left);
415 Fset (sym, val);
416 args_left = Fcdr (Fcdr (args_left));
418 while (!NILP(args_left));
420 UNGCPRO;
421 return val;
424 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
425 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
426 (args)
427 Lisp_Object args;
429 return Fcar (args);
432 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
433 "Like `quote', but preferred for objects which are functions.\n\
434 In byte compilation, `function' causes its argument to be compiled.\n\
435 `quote' cannot do that.")
436 (args)
437 Lisp_Object args;
439 return Fcar (args);
442 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
443 "Return t if function in which this appears was called interactively.\n\
444 This means that the function was called with call-interactively (which\n\
445 includes being called as the binding of a key)\n\
446 and input is currently coming from the keyboard (not in keyboard macro).")
449 register struct backtrace *btp;
450 register Lisp_Object fun;
452 if (!INTERACTIVE)
453 return Qnil;
455 btp = backtrace_list;
457 /* If this isn't a byte-compiled function, there may be a frame at
458 the top for Finteractive_p itself. If so, skip it. */
459 fun = Findirect_function (*btp->function);
460 if (XTYPE (fun) == Lisp_Subr
461 && (struct Lisp_Subr *) XPNTR (fun) == &Sinteractive_p)
462 btp = btp->next;
464 /* If we're running an Emacs 18-style byte-compiled function, there
465 may be a frame for Fbytecode. Now, given the strictest
466 definition, this function isn't really being called
467 interactively, but because that's the way Emacs 18 always builds
468 byte-compiled functions, we'll accept it for now. */
469 if (EQ (*btp->function, Qbytecode))
470 btp = btp->next;
472 /* If this isn't a byte-compiled function, then we may now be
473 looking at several frames for special forms. Skip past them. */
474 while (btp &&
475 btp->nargs == UNEVALLED)
476 btp = btp->next;
478 /* btp now points at the frame of the innermost function that isn't
479 a special form, ignoring frames for Finteractive_p and/or
480 Fbytecode at the top. If this frame is for a built-in function
481 (such as load or eval-region) return nil. */
482 fun = Findirect_function (*btp->function);
483 if (XTYPE (fun) == Lisp_Subr)
484 return Qnil;
485 /* btp points to the frame of a Lisp function that called interactive-p.
486 Return t if that function was called interactively. */
487 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
488 return Qt;
489 return Qnil;
492 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
493 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
494 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
495 See also the function `interactive'.")
496 (args)
497 Lisp_Object args;
499 register Lisp_Object fn_name;
500 register Lisp_Object defn;
502 fn_name = Fcar (args);
503 defn = Fcons (Qlambda, Fcdr (args));
504 if (!NILP (Vpurify_flag))
505 defn = Fpurecopy (defn);
506 Ffset (fn_name, defn);
507 return fn_name;
510 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
511 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
512 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
513 When the macro is called, as in (NAME ARGS...),\n\
514 the function (lambda ARGLIST BODY...) is applied to\n\
515 the list ARGS... as it appears in the expression,\n\
516 and the result should be a form to be evaluated instead of the original.")
517 (args)
518 Lisp_Object args;
520 register Lisp_Object fn_name;
521 register Lisp_Object defn;
523 fn_name = Fcar (args);
524 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
525 if (!NILP (Vpurify_flag))
526 defn = Fpurecopy (defn);
527 Ffset (fn_name, defn);
528 return fn_name;
531 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
532 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
533 You are not required to define a variable in order to use it,\n\
534 but the definition can supply documentation and an initial value\n\
535 in a way that tags can recognize.\n\n\
536 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
537 If SYMBOL is buffer-local, its default value is what is set;\n\
538 buffer-local values are not affected.\n\
539 INITVALUE and DOCSTRING are optional.\n\
540 If DOCSTRING starts with *, this variable is identified as a user option.\n\
541 This means that M-x set-variable and M-x edit-options recognize it.\n\
542 If INITVALUE is missing, SYMBOL's value is not set.")
543 (args)
544 Lisp_Object args;
546 register Lisp_Object sym, tem;
548 sym = Fcar (args);
549 tem = Fcdr (args);
550 if (!NILP (tem))
552 tem = Fdefault_boundp (sym);
553 if (NILP (tem))
554 Fset_default (sym, Feval (Fcar (Fcdr (args))));
556 tem = Fcar (Fcdr (Fcdr (args)));
557 if (!NILP (tem))
559 if (!NILP (Vpurify_flag))
560 tem = Fpurecopy (tem);
561 Fput (sym, Qvariable_documentation, tem);
563 return sym;
566 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
567 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
568 The intent is that programs do not change this value, but users may.\n\
569 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
570 If SYMBOL is buffer-local, its default value is what is set;\n\
571 buffer-local values are not affected.\n\
572 DOCSTRING is optional.\n\
573 If DOCSTRING starts with *, this variable is identified as a user option.\n\
574 This means that M-x set-variable and M-x edit-options recognize it.\n\n\
575 Note: do not use `defconst' for user options in libraries that are not\n\
576 normally loaded, since it is useful for users to be able to specify\n\
577 their own values for such variables before loading the library.\n\
578 Since `defconst' unconditionally assigns the variable,\n\
579 it would override the user's choice.")
580 (args)
581 Lisp_Object args;
583 register Lisp_Object sym, tem;
585 sym = Fcar (args);
586 Fset_default (sym, Feval (Fcar (Fcdr (args))));
587 tem = Fcar (Fcdr (Fcdr (args)));
588 if (!NILP (tem))
590 if (!NILP (Vpurify_flag))
591 tem = Fpurecopy (tem);
592 Fput (sym, Qvariable_documentation, tem);
594 return sym;
597 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
598 "Returns t if VARIABLE is intended to be set and modified by users.\n\
599 \(The alternative is a variable used internally in a Lisp program.)\n\
600 Determined by whether the first character of the documentation\n\
601 for the variable is \"*\"")
602 (variable)
603 Lisp_Object variable;
605 Lisp_Object documentation;
607 documentation = Fget (variable, Qvariable_documentation);
608 if (XTYPE (documentation) == Lisp_Int && XINT (documentation) < 0)
609 return Qt;
610 if ((XTYPE (documentation) == Lisp_String) &&
611 ((unsigned char) XSTRING (documentation)->data[0] == '*'))
612 return Qt;
613 return Qnil;
616 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
617 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
618 The value of the last form in BODY is returned.\n\
619 Each element of VARLIST is a symbol (which is bound to nil)\n\
620 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
621 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
622 (args)
623 Lisp_Object args;
625 Lisp_Object varlist, val, elt;
626 int count = specpdl_ptr - specpdl;
627 struct gcpro gcpro1, gcpro2, gcpro3;
629 GCPRO3 (args, elt, varlist);
631 varlist = Fcar (args);
632 while (!NILP (varlist))
634 QUIT;
635 elt = Fcar (varlist);
636 if (XTYPE (elt) == Lisp_Symbol)
637 specbind (elt, Qnil);
638 else if (! NILP (Fcdr (Fcdr (elt))))
639 Fsignal (Qerror,
640 Fcons (build_string ("`let' bindings can have only one value-form"),
641 elt));
642 else
644 val = Feval (Fcar (Fcdr (elt)));
645 specbind (Fcar (elt), val);
647 varlist = Fcdr (varlist);
649 UNGCPRO;
650 val = Fprogn (Fcdr (args));
651 return unbind_to (count, val);
654 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
655 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
656 The value of the last form in BODY is returned.\n\
657 Each element of VARLIST is a symbol (which is bound to nil)\n\
658 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
659 All the VALUEFORMs are evalled before any symbols are bound.")
660 (args)
661 Lisp_Object args;
663 Lisp_Object *temps, tem;
664 register Lisp_Object elt, varlist;
665 int count = specpdl_ptr - specpdl;
666 register int argnum;
667 struct gcpro gcpro1, gcpro2;
669 varlist = Fcar (args);
671 /* Make space to hold the values to give the bound variables */
672 elt = Flength (varlist);
673 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
675 /* Compute the values and store them in `temps' */
677 GCPRO2 (args, *temps);
678 gcpro2.nvars = 0;
680 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
682 QUIT;
683 elt = Fcar (varlist);
684 if (XTYPE (elt) == Lisp_Symbol)
685 temps [argnum++] = Qnil;
686 else if (! NILP (Fcdr (Fcdr (elt))))
687 Fsignal (Qerror,
688 Fcons (build_string ("`let' bindings can have only one value-form"),
689 elt));
690 else
691 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
692 gcpro2.nvars = argnum;
694 UNGCPRO;
696 varlist = Fcar (args);
697 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
699 elt = Fcar (varlist);
700 tem = temps[argnum++];
701 if (XTYPE (elt) == Lisp_Symbol)
702 specbind (elt, tem);
703 else
704 specbind (Fcar (elt), tem);
707 elt = Fprogn (Fcdr (args));
708 return unbind_to (count, elt);
711 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
712 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
713 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
714 until TEST returns nil.")
715 (args)
716 Lisp_Object args;
718 Lisp_Object test, body, tem;
719 struct gcpro gcpro1, gcpro2;
721 GCPRO2 (test, body);
723 test = Fcar (args);
724 body = Fcdr (args);
725 while (tem = Feval (test), !NILP (tem))
727 QUIT;
728 Fprogn (body);
731 UNGCPRO;
732 return Qnil;
735 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
736 "Return result of expanding macros at top level of FORM.\n\
737 If FORM is not a macro call, it is returned unchanged.\n\
738 Otherwise, the macro is expanded and the expansion is considered\n\
739 in place of FORM. When a non-macro-call results, it is returned.\n\n\
740 The second optional arg ENVIRONMENT species an environment of macro\n\
741 definitions to shadow the loaded ones for use in file byte-compilation.")
742 (form, env)
743 register Lisp_Object form;
744 Lisp_Object env;
746 register Lisp_Object expander, sym, def, tem;
748 while (1)
750 /* Come back here each time we expand a macro call,
751 in case it expands into another macro call. */
752 if (XTYPE (form) != Lisp_Cons)
753 break;
754 sym = XCONS (form)->car;
755 /* Detect ((macro lambda ...) ...) */
756 if (XTYPE (sym) == Lisp_Cons
757 && EQ (XCONS (sym)->car, Qmacro))
759 expander = XCONS (sym)->cdr;
760 goto explicit;
762 if (XTYPE (sym) != Lisp_Symbol)
763 break;
764 /* Trace symbols aliases to other symbols
765 until we get a symbol that is not an alias. */
766 while (1)
768 QUIT;
769 tem = Fassq (sym, env);
770 if (NILP (tem))
772 def = XSYMBOL (sym)->function;
773 if (XTYPE (def) == Lisp_Symbol && !EQ (def, Qunbound))
774 sym = def;
775 else
776 break;
778 else
780 #if 0 /* This is turned off because it caused an element (foo . bar)
781 to have the effect of defining foo as an alias for the macro bar.
782 That is inconsistent; bar should be a function to expand foo. */
783 if (XTYPE (tem) == Lisp_Cons
784 && XTYPE (XCONS (tem)->cdr) == Lisp_Symbol)
785 sym = XCONS (tem)->cdr;
786 else
787 #endif
788 break;
791 /* Right now TEM is the result from SYM in ENV,
792 and if TEM is nil then DEF is SYM's function definition. */
793 if (NILP (tem))
795 /* SYM is not mentioned in ENV.
796 Look at its function definition. */
797 if (EQ (def, Qunbound)
798 || XTYPE (def) != Lisp_Cons)
799 /* Not defined or definition not suitable */
800 break;
801 if (EQ (XCONS (def)->car, Qautoload))
803 /* Autoloading function: will it be a macro when loaded? */
804 tem = Fcar (Fnthcdr (make_number (4), def));
805 if (NILP (tem))
806 break;
807 /* Yes, load it and try again. */
808 do_autoload (def, sym);
809 continue;
811 else if (!EQ (XCONS (def)->car, Qmacro))
812 break;
813 else expander = XCONS (def)->cdr;
815 else
817 expander = XCONS (tem)->cdr;
818 if (NILP (expander))
819 break;
821 explicit:
822 form = apply1 (expander, XCONS (form)->cdr);
824 return form;
827 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
828 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
829 TAG is evalled to get the tag to use. Then the BODY is executed.\n\
830 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
831 If no throw happens, `catch' returns the value of the last BODY form.\n\
832 If a throw happens, it specifies the value to return from `catch'.")
833 (args)
834 Lisp_Object args;
836 register Lisp_Object tag;
837 struct gcpro gcpro1;
839 GCPRO1 (args);
840 tag = Feval (Fcar (args));
841 UNGCPRO;
842 return internal_catch (tag, Fprogn, Fcdr (args));
845 /* Set up a catch, then call C function FUNC on argument ARG.
846 FUNC should return a Lisp_Object.
847 This is how catches are done from within C code. */
849 Lisp_Object
850 internal_catch (tag, func, arg)
851 Lisp_Object tag;
852 Lisp_Object (*func) ();
853 Lisp_Object arg;
855 /* This structure is made part of the chain `catchlist'. */
856 struct catchtag c;
858 /* Fill in the components of c, and put it on the list. */
859 c.next = catchlist;
860 c.tag = tag;
861 c.val = Qnil;
862 c.backlist = backtrace_list;
863 c.handlerlist = handlerlist;
864 c.lisp_eval_depth = lisp_eval_depth;
865 c.pdlcount = specpdl_ptr - specpdl;
866 c.poll_suppress_count = poll_suppress_count;
867 c.gcpro = gcprolist;
868 catchlist = &c;
870 /* Call FUNC. */
871 if (! _setjmp (c.jmp))
872 c.val = (*func) (arg);
874 /* Throw works by a longjmp that comes right here. */
875 catchlist = c.next;
876 return c.val;
879 /* Discard from the catchlist all catch tags back through CATCH.
880 Before each catch is discarded, unbind all special bindings
881 made within that catch. Also, when discarding a catch that
882 corresponds to a condition handler, discard that handler.
884 At the end, restore some static info saved in CATCH.
886 This is used for correct unwinding in Fthrow and Fsignal,
887 before doing the longjmp that actually destroys the stack frames
888 in which these handlers and catches reside. */
890 static void
891 unbind_catch (catch)
892 struct catchtag *catch;
894 register int last_time;
898 last_time = catchlist == catch;
899 unbind_to (catchlist->pdlcount, Qnil);
900 handlerlist = catchlist->handlerlist;
901 catchlist = catchlist->next;
903 while (! last_time);
905 gcprolist = catch->gcpro;
906 backtrace_list = catch->backlist;
907 lisp_eval_depth = catch->lisp_eval_depth;
910 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
911 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
912 Both TAG and VALUE are evalled.")
913 (tag, val)
914 register Lisp_Object tag, val;
916 register struct catchtag *c;
918 while (1)
920 if (!NILP (tag))
921 for (c = catchlist; c; c = c->next)
923 if (EQ (c->tag, tag))
925 /* Restore the polling-suppression count. */
926 if (c->poll_suppress_count > poll_suppress_count)
927 abort ();
928 while (c->poll_suppress_count < poll_suppress_count)
929 start_polling ();
930 c->val = val;
931 unbind_catch (c);
932 _longjmp (c->jmp, 1);
935 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (val, Qnil)));
940 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
941 "Do BODYFORM, protecting with UNWINDFORMS.\n\
942 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
943 If BODYFORM completes normally, its value is returned\n\
944 after executing the UNWINDFORMS.\n\
945 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
946 (args)
947 Lisp_Object args;
949 Lisp_Object val;
950 int count = specpdl_ptr - specpdl;
952 record_unwind_protect (0, Fcdr (args));
953 val = Feval (Fcar (args));
954 return unbind_to (count, val);
957 /* Chain of condition handlers currently in effect.
958 The elements of this chain are contained in the stack frames
959 of Fcondition_case and internal_condition_case.
960 When an error is signaled (by calling Fsignal, below),
961 this chain is searched for an element that applies. */
963 struct handler *handlerlist;
965 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
966 "Regain control when an error is signaled.\n\
967 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
968 executes BODYFORM and returns its value if no error happens.\n\
969 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
970 where the BODY is made of Lisp expressions.\n\n\
971 A handler is applicable to an error\n\
972 if CONDITION-NAME is one of the error's condition names.\n\
973 If an error happens, the first applicable handler is run.\n\
975 When a handler handles an error,\n\
976 control returns to the condition-case and the handler BODY... is executed\n\
977 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
978 VAR may be nil; then you do not get access to the signal information.\n\
980 The value of the last BODY form is returned from the condition-case.\n\
981 See also the function `signal' for more info.")
982 (args)
983 Lisp_Object args;
985 Lisp_Object val;
986 struct catchtag c;
987 struct handler h;
988 register Lisp_Object tem;
990 tem = Fcar (args);
991 CHECK_SYMBOL (tem, 0);
993 c.tag = Qnil;
994 c.val = Qnil;
995 c.backlist = backtrace_list;
996 c.handlerlist = handlerlist;
997 c.lisp_eval_depth = lisp_eval_depth;
998 c.pdlcount = specpdl_ptr - specpdl;
999 c.poll_suppress_count = poll_suppress_count;
1000 c.gcpro = gcprolist;
1001 if (_setjmp (c.jmp))
1003 if (!NILP (h.var))
1004 specbind (h.var, Fcdr (c.val));
1005 val = Fprogn (Fcdr (Fcar (c.val)));
1006 unbind_to (c.pdlcount, Qnil);
1007 return val;
1009 c.next = catchlist;
1010 catchlist = &c;
1011 h.var = Fcar (args);
1012 h.handler = Fcdr (Fcdr (args));
1014 for (val = h.handler; ! NILP (val); val = Fcdr (val))
1016 tem = Fcar (val);
1017 if ((!NILP (tem)) &&
1018 (!CONSP (tem) || (XTYPE (XCONS (tem)->car) != Lisp_Symbol)))
1019 error ("Invalid condition handler", tem);
1022 h.next = handlerlist;
1023 h.poll_suppress_count = poll_suppress_count;
1024 h.tag = &c;
1025 handlerlist = &h;
1027 val = Feval (Fcar (Fcdr (args)));
1028 catchlist = c.next;
1029 handlerlist = h.next;
1030 return val;
1033 Lisp_Object
1034 internal_condition_case (bfun, handlers, hfun)
1035 Lisp_Object (*bfun) ();
1036 Lisp_Object handlers;
1037 Lisp_Object (*hfun) ();
1039 Lisp_Object val;
1040 struct catchtag c;
1041 struct handler h;
1043 c.tag = Qnil;
1044 c.val = Qnil;
1045 c.backlist = backtrace_list;
1046 c.handlerlist = handlerlist;
1047 c.lisp_eval_depth = lisp_eval_depth;
1048 c.pdlcount = specpdl_ptr - specpdl;
1049 c.poll_suppress_count = poll_suppress_count;
1050 c.gcpro = gcprolist;
1051 if (_setjmp (c.jmp))
1053 return (*hfun) (Fcdr (c.val));
1055 c.next = catchlist;
1056 catchlist = &c;
1057 h.handler = handlers;
1058 h.var = Qnil;
1059 h.poll_suppress_count = poll_suppress_count;
1060 h.next = handlerlist;
1061 h.tag = &c;
1062 handlerlist = &h;
1064 val = (*bfun) ();
1065 catchlist = c.next;
1066 handlerlist = h.next;
1067 return val;
1070 static Lisp_Object find_handler_clause ();
1072 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1073 "Signal an error. Args are SIGNAL-NAME, and associated DATA.\n\
1074 This function does not return.\n\n\
1075 A signal name is a symbol with an `error-conditions' property\n\
1076 that is a list of condition names.\n\
1077 A handler for any of those names will get to handle this signal.\n\
1078 The symbol `error' should normally be one of them.\n\
1080 DATA should be a list. Its elements are printed as part of the error message.\n\
1081 If the signal is handled, DATA is made available to the handler.\n\
1082 See also the function `condition-case'.")
1083 (sig, data)
1084 Lisp_Object sig, data;
1086 register struct handler *allhandlers = handlerlist;
1087 Lisp_Object conditions;
1088 extern int gc_in_progress;
1089 extern int waiting_for_input;
1090 Lisp_Object debugger_value;
1092 quit_error_check ();
1093 immediate_quit = 0;
1094 if (gc_in_progress || waiting_for_input)
1095 abort ();
1097 TOTALLY_UNBLOCK_INPUT;
1099 conditions = Fget (sig, Qerror_conditions);
1101 for (; handlerlist; handlerlist = handlerlist->next)
1103 register Lisp_Object clause;
1104 clause = find_handler_clause (handlerlist->handler, conditions,
1105 sig, data, &debugger_value);
1107 #if 0 /* Most callers are not prepared to handle gc if this returns.
1108 So, since this feature is not very useful, take it out. */
1109 /* If have called debugger and user wants to continue,
1110 just return nil. */
1111 if (EQ (clause, Qlambda))
1112 return debugger_value;
1113 #else
1114 if (EQ (clause, Qlambda))
1115 error ("Returning a value from an error is no longer supported");
1116 #endif
1118 if (!NILP (clause))
1120 struct handler *h = handlerlist;
1121 /* Restore the polling-suppression count. */
1122 if (h->poll_suppress_count > poll_suppress_count)
1123 abort ();
1124 while (h->poll_suppress_count < poll_suppress_count)
1125 start_polling ();
1126 handlerlist = allhandlers;
1127 unbind_catch (h->tag);
1128 h->tag->val = Fcons (clause, Fcons (sig, data));
1129 _longjmp (h->tag->jmp, 1);
1133 handlerlist = allhandlers;
1134 /* If no handler is present now, try to run the debugger,
1135 and if that fails, throw to top level. */
1136 find_handler_clause (Qerror, conditions, sig, data, &debugger_value);
1137 Fthrow (Qtop_level, Qt);
1140 /* Return nonzero iff LIST is a non-nil atom or
1141 a list containing one of CONDITIONS. */
1143 static int
1144 wants_debugger (list, conditions)
1145 Lisp_Object list, conditions;
1147 static int looking = 0;
1149 if (looking)
1151 /* We got an error while looking in LIST. */
1152 looking = 0;
1153 return 1;
1156 if (NILP (list))
1157 return 0;
1158 if (! CONSP (list))
1159 return 1;
1161 looking = 1;
1162 while (!NILP (conditions))
1164 Lisp_Object tem;
1165 tem = Fmemq (XCONS (conditions)->car, list);
1166 if (! NILP (tem))
1168 looking = 0;
1169 return 1;
1171 conditions = XCONS (conditions)->cdr;
1175 /* Value of Qlambda means we have called debugger and user has continued.
1176 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1178 static Lisp_Object
1179 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1180 Lisp_Object handlers, conditions, sig, data;
1181 Lisp_Object *debugger_value_ptr;
1183 register Lisp_Object h;
1184 register Lisp_Object tem;
1185 register Lisp_Object tem1;
1187 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1188 return Qt;
1189 if (EQ (handlers, Qerror)) /* error is used similarly, but means display a backtrace too */
1191 if (wants_debugger (Vstack_trace_on_error, conditions))
1192 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace, Qnil);
1193 if (!entering_debugger
1194 && ((EQ (sig, Qquit) && debug_on_quit)
1195 || wants_debugger (Vdebug_on_error, conditions)))
1197 int count = specpdl_ptr - specpdl;
1198 specbind (Qdebug_on_error, Qnil);
1199 *debugger_value_ptr =
1200 call_debugger (Fcons (Qerror,
1201 Fcons (Fcons (sig, data),
1202 Qnil)));
1203 return unbind_to (count, Qlambda);
1205 return Qt;
1207 for (h = handlers; CONSP (h); h = Fcdr (h))
1209 tem1 = Fcar (h);
1210 if (!CONSP (tem1))
1211 continue;
1212 tem = Fmemq (Fcar (tem1), conditions);
1213 if (!NILP (tem))
1214 return tem1;
1216 return Qnil;
1219 /* dump an error message; called like printf */
1221 /* VARARGS 1 */
1222 void
1223 error (m, a1, a2, a3)
1224 char *m;
1226 char buf[200];
1227 sprintf (buf, m, a1, a2, a3);
1229 while (1)
1230 Fsignal (Qerror, Fcons (build_string (buf), Qnil));
1233 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1234 "T if FUNCTION makes provisions for interactive calling.\n\
1235 This means it contains a description for how to read arguments to give it.\n\
1236 The value is nil for an invalid function or a symbol with no function\n\
1237 definition.\n\
1239 Interactively callable functions include strings and vectors (treated\n\
1240 as keyboard macros), lambda-expressions that contain a top-level call\n\
1241 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1242 fourth argument, and some of the built-in functions of Lisp.\n\
1244 Also, a symbol satisfies `commandp' if its function definition does so.")
1245 (function)
1246 Lisp_Object function;
1248 register Lisp_Object fun;
1249 register Lisp_Object funcar;
1250 register Lisp_Object tem;
1251 register int i = 0;
1253 fun = function;
1255 fun = indirect_function (fun);
1256 if (EQ (fun, Qunbound))
1257 return Qnil;
1259 /* Emacs primitives are interactive if their DEFUN specifies an
1260 interactive spec. */
1261 if (XTYPE (fun) == Lisp_Subr)
1263 if (XSUBR (fun)->prompt)
1264 return Qt;
1265 else
1266 return Qnil;
1269 /* Bytecode objects are interactive if they are long enough to
1270 have an element whose index is COMPILED_INTERACTIVE, which is
1271 where the interactive spec is stored. */
1272 else if (XTYPE (fun) == Lisp_Compiled)
1273 return (XVECTOR (fun)->size > COMPILED_INTERACTIVE
1274 ? Qt : Qnil);
1276 /* Strings and vectors are keyboard macros. */
1277 if (XTYPE (fun) == Lisp_String
1278 || XTYPE (fun) == Lisp_Vector)
1279 return Qt;
1281 /* Lists may represent commands. */
1282 if (!CONSP (fun))
1283 return Qnil;
1284 funcar = Fcar (fun);
1285 if (XTYPE (funcar) != Lisp_Symbol)
1286 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1287 if (EQ (funcar, Qlambda))
1288 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1289 if (EQ (funcar, Qmocklisp))
1290 return Qt; /* All mocklisp functions can be called interactively */
1291 if (EQ (funcar, Qautoload))
1292 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1293 else
1294 return Qnil;
1297 /* ARGSUSED */
1298 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1299 "Define FUNCTION to autoload from FILE.\n\
1300 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1301 Third arg DOCSTRING is documentation for the function.\n\
1302 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1303 Fifth arg MACRO if non-nil says the function is really a macro.\n\
1304 Third through fifth args give info about the real definition.\n\
1305 They default to nil.\n\
1306 If FUNCTION is already defined other than as an autoload,\n\
1307 this does nothing and returns nil.")
1308 (function, file, docstring, interactive, macro)
1309 Lisp_Object function, file, docstring, interactive, macro;
1311 #ifdef NO_ARG_ARRAY
1312 Lisp_Object args[4];
1313 #endif
1315 CHECK_SYMBOL (function, 0);
1316 CHECK_STRING (file, 1);
1318 /* If function is defined and not as an autoload, don't override */
1319 if (!EQ (XSYMBOL (function)->function, Qunbound)
1320 && !(XTYPE (XSYMBOL (function)->function) == Lisp_Cons
1321 && EQ (XCONS (XSYMBOL (function)->function)->car, Qautoload)))
1322 return Qnil;
1324 #ifdef NO_ARG_ARRAY
1325 args[0] = file;
1326 args[1] = docstring;
1327 args[2] = interactive;
1328 args[3] = macro;
1330 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1331 #else /* NO_ARG_ARRAY */
1332 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1333 #endif /* not NO_ARG_ARRAY */
1336 Lisp_Object
1337 un_autoload (oldqueue)
1338 Lisp_Object oldqueue;
1340 register Lisp_Object queue, first, second;
1342 /* Queue to unwind is current value of Vautoload_queue.
1343 oldqueue is the shadowed value to leave in Vautoload_queue. */
1344 queue = Vautoload_queue;
1345 Vautoload_queue = oldqueue;
1346 while (CONSP (queue))
1348 first = Fcar (queue);
1349 second = Fcdr (first);
1350 first = Fcar (first);
1351 if (EQ (second, Qnil))
1352 Vfeatures = first;
1353 else
1354 Ffset (first, second);
1355 queue = Fcdr (queue);
1357 return Qnil;
1360 do_autoload (fundef, funname)
1361 Lisp_Object fundef, funname;
1363 int count = specpdl_ptr - specpdl;
1364 Lisp_Object fun, val;
1366 fun = funname;
1367 CHECK_SYMBOL (funname, 0);
1369 /* Value saved here is to be restored into Vautoload_queue */
1370 record_unwind_protect (un_autoload, Vautoload_queue);
1371 Vautoload_queue = Qt;
1372 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);
1373 /* Once loading finishes, don't undo it. */
1374 Vautoload_queue = Qt;
1375 unbind_to (count, Qnil);
1377 fun = Findirect_function (fun);
1379 if (XTYPE (fun) == Lisp_Cons
1380 && EQ (XCONS (fun)->car, Qautoload))
1381 error ("Autoloading failed to define function %s",
1382 XSYMBOL (funname)->name->data);
1385 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1386 "Evaluate FORM and return its value.")
1387 (form)
1388 Lisp_Object form;
1390 Lisp_Object fun, val, original_fun, original_args;
1391 Lisp_Object funcar;
1392 struct backtrace backtrace;
1393 struct gcpro gcpro1, gcpro2, gcpro3;
1395 if (XTYPE (form) == Lisp_Symbol)
1397 if (EQ (Vmocklisp_arguments, Qt))
1398 return Fsymbol_value (form);
1399 val = Fsymbol_value (form);
1400 if (NILP (val))
1401 XFASTINT (val) = 0;
1402 else if (EQ (val, Qt))
1403 XFASTINT (val) = 1;
1404 return val;
1406 if (!CONSP (form))
1407 return form;
1409 QUIT;
1410 if (consing_since_gc > gc_cons_threshold)
1412 GCPRO1 (form);
1413 Fgarbage_collect ();
1414 UNGCPRO;
1417 if (++lisp_eval_depth > max_lisp_eval_depth)
1419 if (max_lisp_eval_depth < 100)
1420 max_lisp_eval_depth = 100;
1421 if (lisp_eval_depth > max_lisp_eval_depth)
1422 error ("Lisp nesting exceeds max-lisp-eval-depth");
1425 original_fun = Fcar (form);
1426 original_args = Fcdr (form);
1428 backtrace.next = backtrace_list;
1429 backtrace_list = &backtrace;
1430 backtrace.function = &original_fun; /* This also protects them from gc */
1431 backtrace.args = &original_args;
1432 backtrace.nargs = UNEVALLED;
1433 backtrace.evalargs = 1;
1434 backtrace.debug_on_exit = 0;
1436 if (debug_on_next_call)
1437 do_debug_on_call (Qt);
1439 /* At this point, only original_fun and original_args
1440 have values that will be used below */
1441 retry:
1442 fun = Findirect_function (original_fun);
1444 if (XTYPE (fun) == Lisp_Subr)
1446 Lisp_Object numargs;
1447 Lisp_Object argvals[7];
1448 Lisp_Object args_left;
1449 register int i, maxargs;
1451 args_left = original_args;
1452 numargs = Flength (args_left);
1454 if (XINT (numargs) < XSUBR (fun)->min_args ||
1455 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1456 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1458 if (XSUBR (fun)->max_args == UNEVALLED)
1460 backtrace.evalargs = 0;
1461 val = (*XSUBR (fun)->function) (args_left);
1462 goto done;
1465 if (XSUBR (fun)->max_args == MANY)
1467 /* Pass a vector of evaluated arguments */
1468 Lisp_Object *vals;
1469 register int argnum = 0;
1471 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1473 GCPRO3 (args_left, fun, fun);
1474 gcpro3.var = vals;
1475 gcpro3.nvars = 0;
1477 while (!NILP (args_left))
1479 vals[argnum++] = Feval (Fcar (args_left));
1480 args_left = Fcdr (args_left);
1481 gcpro3.nvars = argnum;
1484 backtrace.args = vals;
1485 backtrace.nargs = XINT (numargs);
1487 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1488 UNGCPRO;
1489 goto done;
1492 GCPRO3 (args_left, fun, fun);
1493 gcpro3.var = argvals;
1494 gcpro3.nvars = 0;
1496 maxargs = XSUBR (fun)->max_args;
1497 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1499 argvals[i] = Feval (Fcar (args_left));
1500 gcpro3.nvars = ++i;
1503 UNGCPRO;
1505 backtrace.args = argvals;
1506 backtrace.nargs = XINT (numargs);
1508 switch (i)
1510 case 0:
1511 val = (*XSUBR (fun)->function) ();
1512 goto done;
1513 case 1:
1514 val = (*XSUBR (fun)->function) (argvals[0]);
1515 goto done;
1516 case 2:
1517 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1518 goto done;
1519 case 3:
1520 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1521 argvals[2]);
1522 goto done;
1523 case 4:
1524 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1525 argvals[2], argvals[3]);
1526 goto done;
1527 case 5:
1528 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1529 argvals[3], argvals[4]);
1530 goto done;
1531 case 6:
1532 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1533 argvals[3], argvals[4], argvals[5]);
1534 goto done;
1536 default:
1537 /* Someone has created a subr that takes more arguments than
1538 is supported by this code. We need to either rewrite the
1539 subr to use a different argument protocol, or add more
1540 cases to this switch. */
1541 abort ();
1544 if (XTYPE (fun) == Lisp_Compiled)
1545 val = apply_lambda (fun, original_args, 1);
1546 else
1548 if (!CONSP (fun))
1549 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1550 funcar = Fcar (fun);
1551 if (XTYPE (funcar) != Lisp_Symbol)
1552 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1553 if (EQ (funcar, Qautoload))
1555 do_autoload (fun, original_fun);
1556 goto retry;
1558 if (EQ (funcar, Qmacro))
1559 val = Feval (apply1 (Fcdr (fun), original_args));
1560 else if (EQ (funcar, Qlambda))
1561 val = apply_lambda (fun, original_args, 1);
1562 else if (EQ (funcar, Qmocklisp))
1563 val = ml_apply (fun, original_args);
1564 else
1565 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1567 done:
1568 if (!EQ (Vmocklisp_arguments, Qt))
1570 if (NILP (val))
1571 XFASTINT (val) = 0;
1572 else if (EQ (val, Qt))
1573 XFASTINT (val) = 1;
1575 lisp_eval_depth--;
1576 if (backtrace.debug_on_exit)
1577 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1578 backtrace_list = backtrace.next;
1579 return val;
1582 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1583 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1584 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1585 (nargs, args)
1586 int nargs;
1587 Lisp_Object *args;
1589 register int i, numargs;
1590 register Lisp_Object spread_arg;
1591 register Lisp_Object *funcall_args;
1592 Lisp_Object fun;
1593 struct gcpro gcpro1;
1595 fun = args [0];
1596 funcall_args = 0;
1597 spread_arg = args [nargs - 1];
1598 CHECK_LIST (spread_arg, nargs);
1600 numargs = XINT (Flength (spread_arg));
1602 if (numargs == 0)
1603 return Ffuncall (nargs - 1, args);
1604 else if (numargs == 1)
1606 args [nargs - 1] = XCONS (spread_arg)->car;
1607 return Ffuncall (nargs, args);
1610 numargs += nargs - 2;
1612 fun = indirect_function (fun);
1613 if (EQ (fun, Qunbound))
1615 /* Let funcall get the error */
1616 fun = args[0];
1617 goto funcall;
1620 if (XTYPE (fun) == Lisp_Subr)
1622 if (numargs < XSUBR (fun)->min_args
1623 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1624 goto funcall; /* Let funcall get the error */
1625 else if (XSUBR (fun)->max_args > numargs)
1627 /* Avoid making funcall cons up a yet another new vector of arguments
1628 by explicitly supplying nil's for optional values */
1629 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
1630 * sizeof (Lisp_Object));
1631 for (i = numargs; i < XSUBR (fun)->max_args;)
1632 funcall_args[++i] = Qnil;
1633 GCPRO1 (*funcall_args);
1634 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
1637 funcall:
1638 /* We add 1 to numargs because funcall_args includes the
1639 function itself as well as its arguments. */
1640 if (!funcall_args)
1642 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1643 * sizeof (Lisp_Object));
1644 GCPRO1 (*funcall_args);
1645 gcpro1.nvars = 1 + numargs;
1648 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
1649 /* Spread the last arg we got. Its first element goes in
1650 the slot that it used to occupy, hence this value of I. */
1651 i = nargs - 1;
1652 while (!NILP (spread_arg))
1654 funcall_args [i++] = XCONS (spread_arg)->car;
1655 spread_arg = XCONS (spread_arg)->cdr;
1658 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
1661 /* Apply fn to arg */
1662 Lisp_Object
1663 apply1 (fn, arg)
1664 Lisp_Object fn, arg;
1666 struct gcpro gcpro1;
1668 GCPRO1 (fn);
1669 if (NILP (arg))
1670 RETURN_UNGCPRO (Ffuncall (1, &fn));
1671 gcpro1.nvars = 2;
1672 #ifdef NO_ARG_ARRAY
1674 Lisp_Object args[2];
1675 args[0] = fn;
1676 args[1] = arg;
1677 gcpro1.var = args;
1678 RETURN_UNGCPRO (Fapply (2, args));
1680 #else /* not NO_ARG_ARRAY */
1681 RETURN_UNGCPRO (Fapply (2, &fn));
1682 #endif /* not NO_ARG_ARRAY */
1685 /* Call function fn on no arguments */
1686 Lisp_Object
1687 call0 (fn)
1688 Lisp_Object fn;
1690 struct gcpro gcpro1;
1692 GCPRO1 (fn);
1693 RETURN_UNGCPRO (Ffuncall (1, &fn));
1696 /* Call function fn with argument arg */
1697 /* ARGSUSED */
1698 Lisp_Object
1699 call1 (fn, arg)
1700 Lisp_Object fn, arg;
1702 struct gcpro gcpro1;
1703 #ifdef NO_ARG_ARRAY
1704 Lisp_Object args[2];
1706 args[0] = fn;
1707 args[1] = arg;
1708 GCPRO1 (args[0]);
1709 gcpro1.nvars = 2;
1710 RETURN_UNGCPRO (Ffuncall (2, args));
1711 #else /* not NO_ARG_ARRAY */
1712 GCPRO1 (fn);
1713 gcpro1.nvars = 2;
1714 RETURN_UNGCPRO (Ffuncall (2, &fn));
1715 #endif /* not NO_ARG_ARRAY */
1718 /* Call function fn with arguments arg, arg1 */
1719 /* ARGSUSED */
1720 Lisp_Object
1721 call2 (fn, arg, arg1)
1722 Lisp_Object fn, arg, arg1;
1724 struct gcpro gcpro1;
1725 #ifdef NO_ARG_ARRAY
1726 Lisp_Object args[3];
1727 args[0] = fn;
1728 args[1] = arg;
1729 args[2] = arg1;
1730 GCPRO1 (args[0]);
1731 gcpro1.nvars = 3;
1732 RETURN_UNGCPRO (Ffuncall (3, args));
1733 #else /* not NO_ARG_ARRAY */
1734 GCPRO1 (fn);
1735 gcpro1.nvars = 3;
1736 RETURN_UNGCPRO (Ffuncall (3, &fn));
1737 #endif /* not NO_ARG_ARRAY */
1740 /* Call function fn with arguments arg, arg1, arg2 */
1741 /* ARGSUSED */
1742 Lisp_Object
1743 call3 (fn, arg, arg1, arg2)
1744 Lisp_Object fn, arg, arg1, arg2;
1746 struct gcpro gcpro1;
1747 #ifdef NO_ARG_ARRAY
1748 Lisp_Object args[4];
1749 args[0] = fn;
1750 args[1] = arg;
1751 args[2] = arg1;
1752 args[3] = arg2;
1753 GCPRO1 (args[0]);
1754 gcpro1.nvars = 4;
1755 RETURN_UNGCPRO (Ffuncall (4, args));
1756 #else /* not NO_ARG_ARRAY */
1757 GCPRO1 (fn);
1758 gcpro1.nvars = 4;
1759 RETURN_UNGCPRO (Ffuncall (4, &fn));
1760 #endif /* not NO_ARG_ARRAY */
1763 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
1764 "Call first argument as a function, passing remaining arguments to it.\n\
1765 Thus, (funcall 'cons 'x 'y) returns (x . y).")
1766 (nargs, args)
1767 int nargs;
1768 Lisp_Object *args;
1770 Lisp_Object fun;
1771 Lisp_Object funcar;
1772 int numargs = nargs - 1;
1773 Lisp_Object lisp_numargs;
1774 Lisp_Object val;
1775 struct backtrace backtrace;
1776 register Lisp_Object *internal_args;
1777 register int i;
1779 QUIT;
1780 if (consing_since_gc > gc_cons_threshold)
1781 Fgarbage_collect ();
1783 if (++lisp_eval_depth > max_lisp_eval_depth)
1785 if (max_lisp_eval_depth < 100)
1786 max_lisp_eval_depth = 100;
1787 if (lisp_eval_depth > max_lisp_eval_depth)
1788 error ("Lisp nesting exceeds max-lisp-eval-depth");
1791 backtrace.next = backtrace_list;
1792 backtrace_list = &backtrace;
1793 backtrace.function = &args[0];
1794 backtrace.args = &args[1];
1795 backtrace.nargs = nargs - 1;
1796 backtrace.evalargs = 0;
1797 backtrace.debug_on_exit = 0;
1799 if (debug_on_next_call)
1800 do_debug_on_call (Qlambda);
1802 retry:
1804 fun = args[0];
1806 fun = Findirect_function (fun);
1808 if (XTYPE (fun) == Lisp_Subr)
1810 if (numargs < XSUBR (fun)->min_args
1811 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1813 XFASTINT (lisp_numargs) = numargs;
1814 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
1817 if (XSUBR (fun)->max_args == UNEVALLED)
1818 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1820 if (XSUBR (fun)->max_args == MANY)
1822 val = (*XSUBR (fun)->function) (numargs, args + 1);
1823 goto done;
1826 if (XSUBR (fun)->max_args > numargs)
1828 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
1829 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
1830 for (i = numargs; i < XSUBR (fun)->max_args; i++)
1831 internal_args[i] = Qnil;
1833 else
1834 internal_args = args + 1;
1835 switch (XSUBR (fun)->max_args)
1837 case 0:
1838 val = (*XSUBR (fun)->function) ();
1839 goto done;
1840 case 1:
1841 val = (*XSUBR (fun)->function) (internal_args[0]);
1842 goto done;
1843 case 2:
1844 val = (*XSUBR (fun)->function) (internal_args[0],
1845 internal_args[1]);
1846 goto done;
1847 case 3:
1848 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
1849 internal_args[2]);
1850 goto done;
1851 case 4:
1852 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
1853 internal_args[2],
1854 internal_args[3]);
1855 goto done;
1856 case 5:
1857 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
1858 internal_args[2], internal_args[3],
1859 internal_args[4]);
1860 goto done;
1861 case 6:
1862 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
1863 internal_args[2], internal_args[3],
1864 internal_args[4], internal_args[5]);
1865 goto done;
1867 default:
1869 /* If a subr takes more than 6 arguments without using MANY
1870 or UNEVALLED, we need to extend this function to support it.
1871 Until this is done, there is no way to call the function. */
1872 abort ();
1875 if (XTYPE (fun) == Lisp_Compiled)
1876 val = funcall_lambda (fun, numargs, args + 1);
1877 else
1879 if (!CONSP (fun))
1880 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1881 funcar = Fcar (fun);
1882 if (XTYPE (funcar) != Lisp_Symbol)
1883 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1884 if (EQ (funcar, Qlambda))
1885 val = funcall_lambda (fun, numargs, args + 1);
1886 else if (EQ (funcar, Qmocklisp))
1887 val = ml_apply (fun, Flist (numargs, args + 1));
1888 else if (EQ (funcar, Qautoload))
1890 do_autoload (fun, args[0]);
1891 goto retry;
1893 else
1894 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1896 done:
1897 lisp_eval_depth--;
1898 if (backtrace.debug_on_exit)
1899 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1900 backtrace_list = backtrace.next;
1901 return val;
1904 Lisp_Object
1905 apply_lambda (fun, args, eval_flag)
1906 Lisp_Object fun, args;
1907 int eval_flag;
1909 Lisp_Object args_left;
1910 Lisp_Object numargs;
1911 register Lisp_Object *arg_vector;
1912 struct gcpro gcpro1, gcpro2, gcpro3;
1913 register int i;
1914 register Lisp_Object tem;
1916 numargs = Flength (args);
1917 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1918 args_left = args;
1920 GCPRO3 (*arg_vector, args_left, fun);
1921 gcpro1.nvars = 0;
1923 for (i = 0; i < XINT (numargs);)
1925 tem = Fcar (args_left), args_left = Fcdr (args_left);
1926 if (eval_flag) tem = Feval (tem);
1927 arg_vector[i++] = tem;
1928 gcpro1.nvars = i;
1931 UNGCPRO;
1933 if (eval_flag)
1935 backtrace_list->args = arg_vector;
1936 backtrace_list->nargs = i;
1938 backtrace_list->evalargs = 0;
1939 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
1941 /* Do the debug-on-exit now, while arg_vector still exists. */
1942 if (backtrace_list->debug_on_exit)
1943 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
1944 /* Don't do it again when we return to eval. */
1945 backtrace_list->debug_on_exit = 0;
1946 return tem;
1949 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
1950 and return the result of evaluation.
1951 FUN must be either a lambda-expression or a compiled-code object. */
1953 Lisp_Object
1954 funcall_lambda (fun, nargs, arg_vector)
1955 Lisp_Object fun;
1956 int nargs;
1957 register Lisp_Object *arg_vector;
1959 Lisp_Object val, tem;
1960 register Lisp_Object syms_left;
1961 Lisp_Object numargs;
1962 register Lisp_Object next;
1963 int count = specpdl_ptr - specpdl;
1964 register int i;
1965 int optional = 0, rest = 0;
1967 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
1969 XFASTINT (numargs) = nargs;
1971 if (XTYPE (fun) == Lisp_Cons)
1972 syms_left = Fcar (Fcdr (fun));
1973 else if (XTYPE (fun) == Lisp_Compiled)
1974 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
1975 else abort ();
1977 i = 0;
1978 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
1980 QUIT;
1981 next = Fcar (syms_left);
1982 while (XTYPE (next) != Lisp_Symbol)
1983 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1984 if (EQ (next, Qand_rest))
1985 rest = 1;
1986 else if (EQ (next, Qand_optional))
1987 optional = 1;
1988 else if (rest)
1990 specbind (next, Flist (nargs - i, &arg_vector[i]));
1991 i = nargs;
1993 else if (i < nargs)
1995 tem = arg_vector[i++];
1996 specbind (next, tem);
1998 else if (!optional)
1999 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2000 else
2001 specbind (next, Qnil);
2004 if (i < nargs)
2005 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2007 if (XTYPE (fun) == Lisp_Cons)
2008 val = Fprogn (Fcdr (Fcdr (fun)));
2009 else
2010 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2011 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2012 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2013 return unbind_to (count, val);
2016 void
2017 grow_specpdl ()
2019 register int count = specpdl_ptr - specpdl;
2020 if (specpdl_size >= max_specpdl_size)
2022 if (max_specpdl_size < 400)
2023 max_specpdl_size = 400;
2024 if (specpdl_size >= max_specpdl_size)
2026 Fsignal (Qerror,
2027 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2028 max_specpdl_size *= 2;
2031 specpdl_size *= 2;
2032 if (specpdl_size > max_specpdl_size)
2033 specpdl_size = max_specpdl_size;
2034 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2035 specpdl_ptr = specpdl + count;
2038 void
2039 specbind (symbol, value)
2040 Lisp_Object symbol, value;
2042 extern void store_symval_forwarding (); /* in eval.c */
2043 Lisp_Object ovalue;
2045 CHECK_SYMBOL (symbol, 0);
2047 if (specpdl_ptr == specpdl + specpdl_size)
2048 grow_specpdl ();
2049 specpdl_ptr->symbol = symbol;
2050 specpdl_ptr->func = 0;
2051 ovalue = XSYMBOL (symbol)->value;
2052 specpdl_ptr->old_value = EQ (ovalue, Qunbound) ? Qunbound : Fsymbol_value (symbol);
2053 specpdl_ptr++;
2054 if (XTYPE (ovalue) == Lisp_Buffer_Objfwd)
2055 store_symval_forwarding (symbol, ovalue, value);
2056 else
2057 Fset (symbol, value);
2060 void
2061 record_unwind_protect (function, arg)
2062 Lisp_Object (*function)();
2063 Lisp_Object arg;
2065 if (specpdl_ptr == specpdl + specpdl_size)
2066 grow_specpdl ();
2067 specpdl_ptr->func = function;
2068 specpdl_ptr->symbol = Qnil;
2069 specpdl_ptr->old_value = arg;
2070 specpdl_ptr++;
2073 Lisp_Object
2074 unbind_to (count, value)
2075 int count;
2076 Lisp_Object value;
2078 int quitf = !NILP (Vquit_flag);
2079 struct gcpro gcpro1;
2081 GCPRO1 (value);
2083 Vquit_flag = Qnil;
2085 while (specpdl_ptr != specpdl + count)
2087 --specpdl_ptr;
2088 if (specpdl_ptr->func != 0)
2089 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2090 /* Note that a "binding" of nil is really an unwind protect,
2091 so in that case the "old value" is a list of forms to evaluate. */
2092 else if (NILP (specpdl_ptr->symbol))
2093 Fprogn (specpdl_ptr->old_value);
2094 else
2095 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
2097 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
2099 UNGCPRO;
2101 return value;
2104 #if 0
2106 /* Get the value of symbol's global binding, even if that binding
2107 is not now dynamically visible. */
2109 Lisp_Object
2110 top_level_value (symbol)
2111 Lisp_Object symbol;
2113 register struct specbinding *ptr = specpdl;
2115 CHECK_SYMBOL (symbol, 0);
2116 for (; ptr != specpdl_ptr; ptr++)
2118 if (EQ (ptr->symbol, symbol))
2119 return ptr->old_value;
2121 return Fsymbol_value (symbol);
2124 Lisp_Object
2125 top_level_set (symbol, newval)
2126 Lisp_Object symbol, newval;
2128 register struct specbinding *ptr = specpdl;
2130 CHECK_SYMBOL (symbol, 0);
2131 for (; ptr != specpdl_ptr; ptr++)
2133 if (EQ (ptr->symbol, symbol))
2135 ptr->old_value = newval;
2136 return newval;
2139 return Fset (symbol, newval);
2142 #endif /* 0 */
2144 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2145 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2146 The debugger is entered when that frame exits, if the flag is non-nil.")
2147 (level, flag)
2148 Lisp_Object level, flag;
2150 register struct backtrace *backlist = backtrace_list;
2151 register int i;
2153 CHECK_NUMBER (level, 0);
2155 for (i = 0; backlist && i < XINT (level); i++)
2157 backlist = backlist->next;
2160 if (backlist)
2161 backlist->debug_on_exit = !NILP (flag);
2163 return flag;
2166 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2167 "Print a trace of Lisp function calls currently active.\n\
2168 Output stream used is value of `standard-output'.")
2171 register struct backtrace *backlist = backtrace_list;
2172 register int i;
2173 Lisp_Object tail;
2174 Lisp_Object tem;
2175 extern Lisp_Object Vprint_level;
2176 struct gcpro gcpro1;
2178 entering_debugger = 0;
2180 XFASTINT (Vprint_level) = 3;
2182 tail = Qnil;
2183 GCPRO1 (tail);
2185 while (backlist)
2187 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2188 if (backlist->nargs == UNEVALLED)
2190 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2192 else
2194 tem = *backlist->function;
2195 Fprin1 (tem, Qnil); /* This can QUIT */
2196 write_string ("(", -1);
2197 if (backlist->nargs == MANY)
2199 for (tail = *backlist->args, i = 0;
2200 !NILP (tail);
2201 tail = Fcdr (tail), i++)
2203 if (i) write_string (" ", -1);
2204 Fprin1 (Fcar (tail), Qnil);
2207 else
2209 for (i = 0; i < backlist->nargs; i++)
2211 if (i) write_string (" ", -1);
2212 Fprin1 (backlist->args[i], Qnil);
2216 write_string (")\n", -1);
2217 backlist = backlist->next;
2220 Vprint_level = Qnil;
2221 UNGCPRO;
2222 return Qnil;
2225 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2226 "Return the function and arguments N frames up from current execution point.\n\
2227 If that frame has not evaluated the arguments yet (or is a special form),\n\
2228 the value is (nil FUNCTION ARG-FORMS...).\n\
2229 If that frame has evaluated its arguments and called its function already,\n\
2230 the value is (t FUNCTION ARG-VALUES...).\n\
2231 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2232 FUNCTION is whatever was supplied as car of evaluated list,\n\
2233 or a lambda expression for macro calls.\n\
2234 If N is more than the number of frames, the value is nil.")
2235 (nframes)
2236 Lisp_Object nframes;
2238 register struct backtrace *backlist = backtrace_list;
2239 register int i;
2240 Lisp_Object tem;
2242 CHECK_NATNUM (nframes, 0);
2244 /* Find the frame requested. */
2245 for (i = 0; i < XFASTINT (nframes); i++)
2246 backlist = backlist->next;
2248 if (!backlist)
2249 return Qnil;
2250 if (backlist->nargs == UNEVALLED)
2251 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2252 else
2254 if (backlist->nargs == MANY)
2255 tem = *backlist->args;
2256 else
2257 tem = Flist (backlist->nargs, backlist->args);
2259 return Fcons (Qt, Fcons (*backlist->function, tem));
2263 syms_of_eval ()
2265 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2266 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2268 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2269 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2270 This limit is to catch infinite recursions for you before they cause\n\
2271 actual stack overflow in C, which would be fatal for Emacs.\n\
2272 You can safely make it considerably larger than its default value,\n\
2273 if that proves inconveniently small.");
2275 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2276 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2277 Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2278 Vquit_flag = Qnil;
2280 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2281 "Non-nil inhibits C-g quitting from happening immediately.\n\
2282 Note that `quit-flag' will still be set by typing C-g,\n\
2283 so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
2284 To prevent this happening, set `quit-flag' to nil\n\
2285 before making `inhibit-quit' nil.");
2286 Vinhibit_quit = Qnil;
2288 Qinhibit_quit = intern ("inhibit-quit");
2289 staticpro (&Qinhibit_quit);
2291 Qautoload = intern ("autoload");
2292 staticpro (&Qautoload);
2294 Qdebug_on_error = intern ("debug-on-error");
2295 staticpro (&Qdebug_on_error);
2297 Qmacro = intern ("macro");
2298 staticpro (&Qmacro);
2300 /* Note that the process handling also uses Qexit, but we don't want
2301 to staticpro it twice, so we just do it here. */
2302 Qexit = intern ("exit");
2303 staticpro (&Qexit);
2305 Qinteractive = intern ("interactive");
2306 staticpro (&Qinteractive);
2308 Qcommandp = intern ("commandp");
2309 staticpro (&Qcommandp);
2311 Qdefun = intern ("defun");
2312 staticpro (&Qdefun);
2314 Qand_rest = intern ("&rest");
2315 staticpro (&Qand_rest);
2317 Qand_optional = intern ("&optional");
2318 staticpro (&Qand_optional);
2320 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
2321 "*Non-nil means automatically display a backtrace buffer\n\
2322 after any error that is handled by the editor command loop.\n\
2323 If the value is a list, an error only means to display a backtrace\n\
2324 if one of its condition symbols appears in the list.");
2325 Vstack_trace_on_error = Qnil;
2327 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
2328 "*Non-nil means enter debugger if an error is signaled.\n\
2329 Does not apply to errors handled by `condition-case'.\n\
2330 If the value is a list, an error only means to enter the debugger\n\
2331 if one of its condition symbols appears in the list.\n\
2332 See also variable `debug-on-quit'.");
2333 Vdebug_on_error = Qnil;
2335 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
2336 "*Non-nil means enter debugger if quit is signaled (C-G, for example).\n\
2337 Does not apply if quit is handled by a `condition-case'.\n\
2338 A non-nil value is equivalent to a `debug-on-error' value containing `quit'.");
2339 debug_on_quit = 0;
2341 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
2342 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2344 DEFVAR_LISP ("debugger", &Vdebugger,
2345 "Function to call to invoke debugger.\n\
2346 If due to frame exit, args are `exit' and the value being returned;\n\
2347 this function's value will be returned instead of that.\n\
2348 If due to error, args are `error' and a list of the args to `signal'.\n\
2349 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2350 If due to `eval' entry, one arg, t.");
2351 Vdebugger = Qnil;
2353 Qmocklisp_arguments = intern ("mocklisp-arguments");
2354 staticpro (&Qmocklisp_arguments);
2355 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
2356 "While in a mocklisp function, the list of its unevaluated args.");
2357 Vmocklisp_arguments = Qt;
2359 DEFVAR_LISP ("run-hooks", &Vrun_hooks,
2360 "Set to the function `run-hooks', if that function has been defined.\n\
2361 Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
2362 Vrun_hooks = Qnil;
2364 staticpro (&Vautoload_queue);
2365 Vautoload_queue = Qnil;
2367 defsubr (&Sor);
2368 defsubr (&Sand);
2369 defsubr (&Sif);
2370 defsubr (&Scond);
2371 defsubr (&Sprogn);
2372 defsubr (&Sprog1);
2373 defsubr (&Sprog2);
2374 defsubr (&Ssetq);
2375 defsubr (&Squote);
2376 defsubr (&Sfunction);
2377 defsubr (&Sdefun);
2378 defsubr (&Sdefmacro);
2379 defsubr (&Sdefvar);
2380 defsubr (&Sdefconst);
2381 defsubr (&Suser_variable_p);
2382 defsubr (&Slet);
2383 defsubr (&SletX);
2384 defsubr (&Swhile);
2385 defsubr (&Smacroexpand);
2386 defsubr (&Scatch);
2387 defsubr (&Sthrow);
2388 defsubr (&Sunwind_protect);
2389 defsubr (&Scondition_case);
2390 defsubr (&Ssignal);
2391 defsubr (&Sinteractive_p);
2392 defsubr (&Scommandp);
2393 defsubr (&Sautoload);
2394 defsubr (&Seval);
2395 defsubr (&Sapply);
2396 defsubr (&Sfuncall);
2397 defsubr (&Sbacktrace_debug);
2398 defsubr (&Sbacktrace);
2399 defsubr (&Sbacktrace_frame);