(CFLAGS): Use shell syntax, not Makefile.
[emacs.git] / src / eval.c
blob254913727bca12fca0bf1c690c68304eeaaa151e
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1993 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 #include "blockinput.h"
25 #ifndef standalone
26 #include "commands.h"
27 #include "keyboard.h"
28 #else
29 #define INTERACTIVE 1
30 #endif
32 #include <setjmp.h>
34 /* This definition is duplicated in alloc.c and keyboard.c */
35 /* Putting it in lisp.h makes cc bomb out! */
37 struct backtrace
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;
50 struct backtrace *backtrace_list;
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.
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.
60 catchtag structures are chained together in the C calling stack;
61 the `next' member points to the next outer catchtag.
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.
68 All the other members are concerned with restoring the interpreter
69 state. */
70 struct catchtag
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;
84 struct catchtag *catchlist;
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;
92 Lisp_Object Vrun_hooks;
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. */
99 Lisp_Object Vautoload_queue;
101 /* Current number of specbindings allocated in specpdl. */
102 int specpdl_size;
104 /* Pointer to beginning of specpdl. */
105 struct specbinding *specpdl;
107 /* Pointer to first unused element in specpdl. */
108 struct specbinding *specpdl_ptr;
110 /* Maximum size allowed for specpdl allocation */
111 int max_specpdl_size;
113 /* Depth in Lisp evaluations and function calls. */
114 int lisp_eval_depth;
116 /* Maximum allowed depth in Lisp evaluations and function calls. */
117 int max_lisp_eval_depth;
119 /* Nonzero means enter debugger before next function call */
120 int debug_on_next_call;
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;
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;
130 /* Nonzero means enter debugger if a quit signal
131 is handled by the command loop's error handler. */
132 int debug_on_quit;
134 /* The value of num_nonmacro_input_chars as of the last time we
135 started to enter the debugger. If we decide to enter the debugger
136 again when this is still equal to num_nonmacro_input_chars, then we
137 know that the debugger itself has an error, and we should just
138 signal the error instead of entering an infinite loop of debugger
139 invocations. */
140 int when_entered_debugger;
142 Lisp_Object Vdebugger;
144 void specbind (), record_unwind_protect ();
146 Lisp_Object funcall_lambda ();
147 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
149 init_eval_once ()
151 specpdl_size = 50;
152 specpdl = (struct specbinding *) malloc (specpdl_size * sizeof (struct specbinding));
153 max_specpdl_size = 600;
154 max_lisp_eval_depth = 200;
157 init_eval ()
159 specpdl_ptr = specpdl;
160 catchlist = 0;
161 handlerlist = 0;
162 backtrace_list = 0;
163 Vquit_flag = Qnil;
164 debug_on_next_call = 0;
165 lisp_eval_depth = 0;
166 when_entered_debugger = 0;
169 Lisp_Object
170 call_debugger (arg)
171 Lisp_Object arg;
173 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
174 max_lisp_eval_depth = lisp_eval_depth + 20;
175 if (specpdl_size + 40 > max_specpdl_size)
176 max_specpdl_size = specpdl_size + 40;
177 debug_on_next_call = 0;
178 when_entered_debugger = num_nonmacro_input_chars;
179 return apply1 (Vdebugger, arg);
182 do_debug_on_call (code)
183 Lisp_Object code;
185 debug_on_next_call = 0;
186 backtrace_list->debug_on_exit = 1;
187 call_debugger (Fcons (code, Qnil));
190 /* NOTE!!! Every function that can call EVAL must protect its args
191 and temporaries from garbage collection while it needs them.
192 The definition of `For' shows what you have to do. */
194 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
195 "Eval args until one of them yields non-nil, then return that value.\n\
196 The remaining args are not evalled at all.\n\
197 If all args return nil, return nil.")
198 (args)
199 Lisp_Object args;
201 register Lisp_Object val;
202 Lisp_Object args_left;
203 struct gcpro gcpro1;
205 if (NILP(args))
206 return Qnil;
208 args_left = args;
209 GCPRO1 (args_left);
213 val = Feval (Fcar (args_left));
214 if (!NILP (val))
215 break;
216 args_left = Fcdr (args_left);
218 while (!NILP(args_left));
220 UNGCPRO;
221 return val;
224 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
225 "Eval args until one of them yields nil, then return nil.\n\
226 The remaining args are not evalled at all.\n\
227 If no arg yields nil, return the last arg's value.")
228 (args)
229 Lisp_Object args;
231 register Lisp_Object val;
232 Lisp_Object args_left;
233 struct gcpro gcpro1;
235 if (NILP(args))
236 return Qt;
238 args_left = args;
239 GCPRO1 (args_left);
243 val = Feval (Fcar (args_left));
244 if (NILP (val))
245 break;
246 args_left = Fcdr (args_left);
248 while (!NILP(args_left));
250 UNGCPRO;
251 return val;
254 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
255 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
256 Returns the value of THEN or the value of the last of the ELSE's.\n\
257 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
258 If COND yields nil, and there are no ELSE's, the value is nil.")
259 (args)
260 Lisp_Object args;
262 register Lisp_Object cond;
263 struct gcpro gcpro1;
265 GCPRO1 (args);
266 cond = Feval (Fcar (args));
267 UNGCPRO;
269 if (!NILP (cond))
270 return Feval (Fcar (Fcdr (args)));
271 return Fprogn (Fcdr (Fcdr (args)));
274 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
275 "(cond CLAUSES...): try each clause until one succeeds.\n\
276 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
277 and, if the value is non-nil, this clause succeeds:\n\
278 then the expressions in BODY are evaluated and the last one's\n\
279 value is the value of the cond-form.\n\
280 If no clause succeeds, cond returns nil.\n\
281 If a clause has one element, as in (CONDITION),\n\
282 CONDITION's value if non-nil is returned from the cond-form.")
283 (args)
284 Lisp_Object args;
286 register Lisp_Object clause, val;
287 struct gcpro gcpro1;
289 val = Qnil;
290 GCPRO1 (args);
291 while (!NILP (args))
293 clause = Fcar (args);
294 val = Feval (Fcar (clause));
295 if (!NILP (val))
297 if (!EQ (XCONS (clause)->cdr, Qnil))
298 val = Fprogn (XCONS (clause)->cdr);
299 break;
301 args = XCONS (args)->cdr;
303 UNGCPRO;
305 return val;
308 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
309 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
310 (args)
311 Lisp_Object args;
313 register Lisp_Object val, tem;
314 Lisp_Object args_left;
315 struct gcpro gcpro1;
317 /* In Mocklisp code, symbols at the front of the progn arglist
318 are to be bound to zero. */
319 if (!EQ (Vmocklisp_arguments, Qt))
321 val = make_number (0);
322 while (!NILP (args) && (tem = Fcar (args), XTYPE (tem) == Lisp_Symbol))
324 QUIT;
325 specbind (tem, val), args = Fcdr (args);
329 if (NILP(args))
330 return Qnil;
332 args_left = args;
333 GCPRO1 (args_left);
337 val = Feval (Fcar (args_left));
338 args_left = Fcdr (args_left);
340 while (!NILP(args_left));
342 UNGCPRO;
343 return val;
346 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
347 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
348 The value of FIRST is saved during the evaluation of the remaining args,\n\
349 whose values are discarded.")
350 (args)
351 Lisp_Object args;
353 Lisp_Object val;
354 register Lisp_Object args_left;
355 struct gcpro gcpro1, gcpro2;
356 register int argnum = 0;
358 if (NILP(args))
359 return Qnil;
361 args_left = args;
362 val = Qnil;
363 GCPRO2 (args, val);
367 if (!(argnum++))
368 val = Feval (Fcar (args_left));
369 else
370 Feval (Fcar (args_left));
371 args_left = Fcdr (args_left);
373 while (!NILP(args_left));
375 UNGCPRO;
376 return val;
379 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
380 "(prog1 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
381 The value of Y is saved during the evaluation of the remaining args,\n\
382 whose values are discarded.")
383 (args)
384 Lisp_Object args;
386 Lisp_Object val;
387 register Lisp_Object args_left;
388 struct gcpro gcpro1, gcpro2;
389 register int argnum = -1;
391 val = Qnil;
393 if (NILP (args))
394 return Qnil;
396 args_left = args;
397 val = Qnil;
398 GCPRO2 (args, val);
402 if (!(argnum++))
403 val = Feval (Fcar (args_left));
404 else
405 Feval (Fcar (args_left));
406 args_left = Fcdr (args_left);
408 while (!NILP (args_left));
410 UNGCPRO;
411 return val;
414 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
415 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
416 The SYMs are not evaluated. Thus (setq x y) sets x to the value of y.\n\
417 Each SYM is set before the next VAL is computed.\n\
418 The return value of the `setq' form is the value of the last VAL.")
419 (args)
420 Lisp_Object args;
422 register Lisp_Object args_left;
423 register Lisp_Object val, sym;
424 struct gcpro gcpro1;
426 if (NILP(args))
427 return Qnil;
429 args_left = args;
430 GCPRO1 (args);
434 val = Feval (Fcar (Fcdr (args_left)));
435 sym = Fcar (args_left);
436 Fset (sym, val);
437 args_left = Fcdr (Fcdr (args_left));
439 while (!NILP(args_left));
441 UNGCPRO;
442 return val;
445 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
446 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
447 (args)
448 Lisp_Object args;
450 return Fcar (args);
453 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
454 "Like `quote', but preferred for objects which are functions.\n\
455 In byte compilation, `function' causes its argument to be compiled.\n\
456 `quote' cannot do that.")
457 (args)
458 Lisp_Object args;
460 return Fcar (args);
463 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
464 "Return t if function in which this appears was called interactively.\n\
465 This means that the function was called with call-interactively (which\n\
466 includes being called as the binding of a key)\n\
467 and input is currently coming from the keyboard (not in keyboard macro).")
470 register struct backtrace *btp;
471 register Lisp_Object fun;
473 if (!INTERACTIVE)
474 return Qnil;
476 btp = backtrace_list;
478 /* If this isn't a byte-compiled function, there may be a frame at
479 the top for Finteractive_p itself. If so, skip it. */
480 fun = Findirect_function (*btp->function);
481 if (XTYPE (fun) == Lisp_Subr
482 && (struct Lisp_Subr *) XPNTR (fun) == &Sinteractive_p)
483 btp = btp->next;
485 /* If we're running an Emacs 18-style byte-compiled function, there
486 may be a frame for Fbytecode. Now, given the strictest
487 definition, this function isn't really being called
488 interactively, but because that's the way Emacs 18 always builds
489 byte-compiled functions, we'll accept it for now. */
490 if (EQ (*btp->function, Qbytecode))
491 btp = btp->next;
493 /* If this isn't a byte-compiled function, then we may now be
494 looking at several frames for special forms. Skip past them. */
495 while (btp &&
496 btp->nargs == UNEVALLED)
497 btp = btp->next;
499 /* btp now points at the frame of the innermost function that isn't
500 a special form, ignoring frames for Finteractive_p and/or
501 Fbytecode at the top. If this frame is for a built-in function
502 (such as load or eval-region) return nil. */
503 fun = Findirect_function (*btp->function);
504 if (XTYPE (fun) == Lisp_Subr)
505 return Qnil;
506 /* btp points to the frame of a Lisp function that called interactive-p.
507 Return t if that function was called interactively. */
508 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
509 return Qt;
510 return Qnil;
513 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
514 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
515 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
516 See also the function `interactive'.")
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 (Qlambda, Fcdr (args));
525 if (!NILP (Vpurify_flag))
526 defn = Fpurecopy (defn);
527 Ffset (fn_name, defn);
528 LOADHIST_ATTACH (fn_name);
529 return fn_name;
532 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
533 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
534 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
535 When the macro is called, as in (NAME ARGS...),\n\
536 the function (lambda ARGLIST BODY...) is applied to\n\
537 the list ARGS... as it appears in the expression,\n\
538 and the result should be a form to be evaluated instead of the original.")
539 (args)
540 Lisp_Object args;
542 register Lisp_Object fn_name;
543 register Lisp_Object defn;
545 fn_name = Fcar (args);
546 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
547 if (!NILP (Vpurify_flag))
548 defn = Fpurecopy (defn);
549 Ffset (fn_name, defn);
550 LOADHIST_ATTACH (fn_name);
551 return fn_name;
554 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
555 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
556 You are not required to define a variable in order to use it,\n\
557 but the definition can supply documentation and an initial value\n\
558 in a way that tags can recognize.\n\n\
559 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
560 If SYMBOL is buffer-local, its default value is what is set;\n\
561 buffer-local values are not affected.\n\
562 INITVALUE and DOCSTRING are optional.\n\
563 If DOCSTRING starts with *, this variable is identified as a user option.\n\
564 This means that M-x set-variable and M-x edit-options recognize it.\n\
565 If INITVALUE is missing, SYMBOL's value is not set.")
566 (args)
567 Lisp_Object args;
569 register Lisp_Object sym, tem;
571 sym = Fcar (args);
572 tem = Fcdr (args);
573 if (!NILP (tem))
575 tem = Fdefault_boundp (sym);
576 if (NILP (tem))
577 Fset_default (sym, Feval (Fcar (Fcdr (args))));
579 tem = Fcar (Fcdr (Fcdr (args)));
580 if (!NILP (tem))
582 if (!NILP (Vpurify_flag))
583 tem = Fpurecopy (tem);
584 Fput (sym, Qvariable_documentation, tem);
586 LOADHIST_ATTACH (sym);
587 return sym;
590 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
591 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
592 The intent is that programs do not change this value, but users may.\n\
593 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
594 If SYMBOL is buffer-local, its default value is what is set;\n\
595 buffer-local values are not affected.\n\
596 DOCSTRING is optional.\n\
597 If DOCSTRING starts with *, this variable is identified as a user option.\n\
598 This means that M-x set-variable and M-x edit-options recognize it.\n\n\
599 Note: do not use `defconst' for user options in libraries that are not\n\
600 normally loaded, since it is useful for users to be able to specify\n\
601 their own values for such variables before loading the library.\n\
602 Since `defconst' unconditionally assigns the variable,\n\
603 it would override the user's choice.")
604 (args)
605 Lisp_Object args;
607 register Lisp_Object sym, tem;
609 sym = Fcar (args);
610 Fset_default (sym, Feval (Fcar (Fcdr (args))));
611 tem = Fcar (Fcdr (Fcdr (args)));
612 if (!NILP (tem))
614 if (!NILP (Vpurify_flag))
615 tem = Fpurecopy (tem);
616 Fput (sym, Qvariable_documentation, tem);
618 LOADHIST_ATTACH (sym);
619 return sym;
622 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
623 "Returns t if VARIABLE is intended to be set and modified by users.\n\
624 \(The alternative is a variable used internally in a Lisp program.)\n\
625 Determined by whether the first character of the documentation\n\
626 for the variable is \"*\"")
627 (variable)
628 Lisp_Object variable;
630 Lisp_Object documentation;
632 documentation = Fget (variable, Qvariable_documentation);
633 if (XTYPE (documentation) == Lisp_Int && XINT (documentation) < 0)
634 return Qt;
635 if ((XTYPE (documentation) == Lisp_String) &&
636 ((unsigned char) XSTRING (documentation)->data[0] == '*'))
637 return Qt;
638 return Qnil;
641 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
642 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
643 The value of the last form in BODY is returned.\n\
644 Each element of VARLIST is a symbol (which is bound to nil)\n\
645 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
646 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
647 (args)
648 Lisp_Object args;
650 Lisp_Object varlist, val, elt;
651 int count = specpdl_ptr - specpdl;
652 struct gcpro gcpro1, gcpro2, gcpro3;
654 GCPRO3 (args, elt, varlist);
656 varlist = Fcar (args);
657 while (!NILP (varlist))
659 QUIT;
660 elt = Fcar (varlist);
661 if (XTYPE (elt) == Lisp_Symbol)
662 specbind (elt, Qnil);
663 else if (! NILP (Fcdr (Fcdr (elt))))
664 Fsignal (Qerror,
665 Fcons (build_string ("`let' bindings can have only one value-form"),
666 elt));
667 else
669 val = Feval (Fcar (Fcdr (elt)));
670 specbind (Fcar (elt), val);
672 varlist = Fcdr (varlist);
674 UNGCPRO;
675 val = Fprogn (Fcdr (args));
676 return unbind_to (count, val);
679 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
680 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
681 The value of the last form in BODY is returned.\n\
682 Each element of VARLIST is a symbol (which is bound to nil)\n\
683 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
684 All the VALUEFORMs are evalled before any symbols are bound.")
685 (args)
686 Lisp_Object args;
688 Lisp_Object *temps, tem;
689 register Lisp_Object elt, varlist;
690 int count = specpdl_ptr - specpdl;
691 register int argnum;
692 struct gcpro gcpro1, gcpro2;
694 varlist = Fcar (args);
696 /* Make space to hold the values to give the bound variables */
697 elt = Flength (varlist);
698 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
700 /* Compute the values and store them in `temps' */
702 GCPRO2 (args, *temps);
703 gcpro2.nvars = 0;
705 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
707 QUIT;
708 elt = Fcar (varlist);
709 if (XTYPE (elt) == Lisp_Symbol)
710 temps [argnum++] = Qnil;
711 else if (! NILP (Fcdr (Fcdr (elt))))
712 Fsignal (Qerror,
713 Fcons (build_string ("`let' bindings can have only one value-form"),
714 elt));
715 else
716 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
717 gcpro2.nvars = argnum;
719 UNGCPRO;
721 varlist = Fcar (args);
722 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
724 elt = Fcar (varlist);
725 tem = temps[argnum++];
726 if (XTYPE (elt) == Lisp_Symbol)
727 specbind (elt, tem);
728 else
729 specbind (Fcar (elt), tem);
732 elt = Fprogn (Fcdr (args));
733 return unbind_to (count, elt);
736 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
737 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
738 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
739 until TEST returns nil.")
740 (args)
741 Lisp_Object args;
743 Lisp_Object test, body, tem;
744 struct gcpro gcpro1, gcpro2;
746 GCPRO2 (test, body);
748 test = Fcar (args);
749 body = Fcdr (args);
750 while (tem = Feval (test),
751 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
753 QUIT;
754 Fprogn (body);
757 UNGCPRO;
758 return Qnil;
761 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
762 "Return result of expanding macros at top level of FORM.\n\
763 If FORM is not a macro call, it is returned unchanged.\n\
764 Otherwise, the macro is expanded and the expansion is considered\n\
765 in place of FORM. When a non-macro-call results, it is returned.\n\n\
766 The second optional arg ENVIRONMENT species an environment of macro\n\
767 definitions to shadow the loaded ones for use in file byte-compilation.")
768 (form, env)
769 register Lisp_Object form;
770 Lisp_Object env;
772 /* With cleanups from Hallvard Furuseth. */
773 register Lisp_Object expander, sym, def, tem;
775 while (1)
777 /* Come back here each time we expand a macro call,
778 in case it expands into another macro call. */
779 if (XTYPE (form) != Lisp_Cons)
780 break;
781 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
782 def = sym = XCONS (form)->car;
783 tem = Qnil;
784 /* Trace symbols aliases to other symbols
785 until we get a symbol that is not an alias. */
786 while (XTYPE (def) == Lisp_Symbol)
788 QUIT;
789 sym = def;
790 tem = Fassq (sym, env);
791 if (NILP (tem))
793 def = XSYMBOL (sym)->function;
794 if (!EQ (def, Qunbound))
795 continue;
797 break;
799 /* Right now TEM is the result from SYM in ENV,
800 and if TEM is nil then DEF is SYM's function definition. */
801 if (NILP (tem))
803 /* SYM is not mentioned in ENV.
804 Look at its function definition. */
805 if (EQ (def, Qunbound)
806 || XTYPE (def) != Lisp_Cons)
807 /* Not defined or definition not suitable */
808 break;
809 if (EQ (XCONS (def)->car, Qautoload))
811 /* Autoloading function: will it be a macro when loaded? */
812 tem = Fnth (make_number (4), def);
813 if (EQ (tem, Qt) || EQ (tem, Qmacro))
814 /* Yes, load it and try again. */
816 do_autoload (def, sym);
817 continue;
819 else
820 break;
822 else if (!EQ (XCONS (def)->car, Qmacro))
823 break;
824 else expander = XCONS (def)->cdr;
826 else
828 expander = XCONS (tem)->cdr;
829 if (NILP (expander))
830 break;
832 form = apply1 (expander, XCONS (form)->cdr);
834 return form;
837 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
838 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
839 TAG is evalled to get the tag to use. Then the BODY is executed.\n\
840 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
841 If no throw happens, `catch' returns the value of the last BODY form.\n\
842 If a throw happens, it specifies the value to return from `catch'.")
843 (args)
844 Lisp_Object args;
846 register Lisp_Object tag;
847 struct gcpro gcpro1;
849 GCPRO1 (args);
850 tag = Feval (Fcar (args));
851 UNGCPRO;
852 return internal_catch (tag, Fprogn, Fcdr (args));
855 /* Set up a catch, then call C function FUNC on argument ARG.
856 FUNC should return a Lisp_Object.
857 This is how catches are done from within C code. */
859 Lisp_Object
860 internal_catch (tag, func, arg)
861 Lisp_Object tag;
862 Lisp_Object (*func) ();
863 Lisp_Object arg;
865 /* This structure is made part of the chain `catchlist'. */
866 struct catchtag c;
868 /* Fill in the components of c, and put it on the list. */
869 c.next = catchlist;
870 c.tag = tag;
871 c.val = Qnil;
872 c.backlist = backtrace_list;
873 c.handlerlist = handlerlist;
874 c.lisp_eval_depth = lisp_eval_depth;
875 c.pdlcount = specpdl_ptr - specpdl;
876 c.poll_suppress_count = poll_suppress_count;
877 c.gcpro = gcprolist;
878 catchlist = &c;
880 /* Call FUNC. */
881 if (! _setjmp (c.jmp))
882 c.val = (*func) (arg);
884 /* Throw works by a longjmp that comes right here. */
885 catchlist = c.next;
886 return c.val;
889 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
890 jump to that CATCH, returning VALUE as the value of that catch.
892 This is the guts Fthrow and Fsignal; they differ only in the way
893 they choose the catch tag to throw to. A catch tag for a
894 condition-case form has a TAG of Qnil.
896 Before each catch is discarded, unbind all special bindings and
897 execute all unwind-protect clauses made above that catch. Unwind
898 the handler stack as we go, so that the proper handlers are in
899 effect for each unwind-protect clause we run. At the end, restore
900 some static info saved in CATCH, and longjmp to the location
901 specified in the
903 This is used for correct unwinding in Fthrow and Fsignal. */
905 static void
906 unwind_to_catch (catch, value)
907 struct catchtag *catch;
908 Lisp_Object value;
910 register int last_time;
912 /* Save the value in the tag. */
913 catch->val = value;
915 /* Restore the polling-suppression count. */
916 set_poll_suppress_count (catch->poll_suppress_count);
920 last_time = catchlist == catch;
922 /* Unwind the specpdl stack, and then restore the proper set of
923 handlers. */
924 unbind_to (catchlist->pdlcount, Qnil);
925 handlerlist = catchlist->handlerlist;
926 catchlist = catchlist->next;
928 while (! last_time);
930 gcprolist = catch->gcpro;
931 backtrace_list = catch->backlist;
932 lisp_eval_depth = catch->lisp_eval_depth;
934 _longjmp (catch->jmp, 1);
937 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
938 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
939 Both TAG and VALUE are evalled.")
940 (tag, val)
941 register Lisp_Object tag, val;
943 register struct catchtag *c;
945 while (1)
947 if (!NILP (tag))
948 for (c = catchlist; c; c = c->next)
950 if (EQ (c->tag, tag))
951 unwind_to_catch (c, val);
953 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (val, Qnil)));
958 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
959 "Do BODYFORM, protecting with UNWINDFORMS.\n\
960 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
961 If BODYFORM completes normally, its value is returned\n\
962 after executing the UNWINDFORMS.\n\
963 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
964 (args)
965 Lisp_Object args;
967 Lisp_Object val;
968 int count = specpdl_ptr - specpdl;
970 record_unwind_protect (0, Fcdr (args));
971 val = Feval (Fcar (args));
972 return unbind_to (count, val);
975 /* Chain of condition handlers currently in effect.
976 The elements of this chain are contained in the stack frames
977 of Fcondition_case and internal_condition_case.
978 When an error is signaled (by calling Fsignal, below),
979 this chain is searched for an element that applies. */
981 struct handler *handlerlist;
983 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
984 "Regain control when an error is signaled.\n\
985 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
986 executes BODYFORM and returns its value if no error happens.\n\
987 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
988 where the BODY is made of Lisp expressions.\n\n\
989 A handler is applicable to an error\n\
990 if CONDITION-NAME is one of the error's condition names.\n\
991 If an error happens, the first applicable handler is run.\n\
993 The car of a handler may be a list of condition names\n\
994 instead of a single condition name.\n\
996 When a handler handles an error,\n\
997 control returns to the condition-case and the handler BODY... is executed\n\
998 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
999 VAR may be nil; then you do not get access to the signal information.\n\
1001 The value of the last BODY form is returned from the condition-case.\n\
1002 See also the function `signal' for more info.")
1003 (args)
1004 Lisp_Object args;
1006 Lisp_Object val;
1007 struct catchtag c;
1008 struct handler h;
1009 register Lisp_Object var, bodyform, handlers;
1011 var = Fcar (args);
1012 bodyform = Fcar (Fcdr (args));
1013 handlers = Fcdr (Fcdr (args));
1014 CHECK_SYMBOL (var, 0);
1016 for (val = handlers; ! NILP (val); val = Fcdr (val))
1018 Lisp_Object tem;
1019 tem = Fcar (val);
1020 if (! (NILP (tem)
1021 || (CONSP (tem)
1022 && (SYMBOLP (XCONS (tem)->car)
1023 || CONSP (XCONS (tem)->car)))))
1024 error ("Invalid condition handler", tem);
1027 c.tag = Qnil;
1028 c.val = Qnil;
1029 c.backlist = backtrace_list;
1030 c.handlerlist = handlerlist;
1031 c.lisp_eval_depth = lisp_eval_depth;
1032 c.pdlcount = specpdl_ptr - specpdl;
1033 c.poll_suppress_count = poll_suppress_count;
1034 c.gcpro = gcprolist;
1035 if (_setjmp (c.jmp))
1037 if (!NILP (h.var))
1038 specbind (h.var, c.val);
1039 val = Fprogn (Fcdr (h.chosen_clause));
1041 /* Note that this just undoes the binding of h.var; whoever
1042 longjumped to us unwound the stack to c.pdlcount before
1043 throwing. */
1044 unbind_to (c.pdlcount, Qnil);
1045 return val;
1047 c.next = catchlist;
1048 catchlist = &c;
1050 h.var = var;
1051 h.handler = handlers;
1052 h.next = handlerlist;
1053 h.tag = &c;
1054 handlerlist = &h;
1056 val = Feval (bodyform);
1057 catchlist = c.next;
1058 handlerlist = h.next;
1059 return val;
1062 Lisp_Object
1063 internal_condition_case (bfun, handlers, hfun)
1064 Lisp_Object (*bfun) ();
1065 Lisp_Object handlers;
1066 Lisp_Object (*hfun) ();
1068 Lisp_Object val;
1069 struct catchtag c;
1070 struct handler h;
1072 c.tag = Qnil;
1073 c.val = Qnil;
1074 c.backlist = backtrace_list;
1075 c.handlerlist = handlerlist;
1076 c.lisp_eval_depth = lisp_eval_depth;
1077 c.pdlcount = specpdl_ptr - specpdl;
1078 c.poll_suppress_count = poll_suppress_count;
1079 c.gcpro = gcprolist;
1080 if (_setjmp (c.jmp))
1082 return (*hfun) (c.val);
1084 c.next = catchlist;
1085 catchlist = &c;
1086 h.handler = handlers;
1087 h.var = Qnil;
1088 h.next = handlerlist;
1089 h.tag = &c;
1090 handlerlist = &h;
1092 val = (*bfun) ();
1093 catchlist = c.next;
1094 handlerlist = h.next;
1095 return val;
1098 Lisp_Object
1099 internal_condition_case_1 (bfun, arg, handlers, hfun)
1100 Lisp_Object (*bfun) ();
1101 Lisp_Object arg;
1102 Lisp_Object handlers;
1103 Lisp_Object (*hfun) ();
1105 Lisp_Object val;
1106 struct catchtag c;
1107 struct handler h;
1109 c.tag = Qnil;
1110 c.val = Qnil;
1111 c.backlist = backtrace_list;
1112 c.handlerlist = handlerlist;
1113 c.lisp_eval_depth = lisp_eval_depth;
1114 c.pdlcount = specpdl_ptr - specpdl;
1115 c.poll_suppress_count = poll_suppress_count;
1116 c.gcpro = gcprolist;
1117 if (_setjmp (c.jmp))
1119 return (*hfun) (c.val);
1121 c.next = catchlist;
1122 catchlist = &c;
1123 h.handler = handlers;
1124 h.var = Qnil;
1125 h.next = handlerlist;
1126 h.tag = &c;
1127 handlerlist = &h;
1129 val = (*bfun) (arg);
1130 catchlist = c.next;
1131 handlerlist = h.next;
1132 return val;
1135 static Lisp_Object find_handler_clause ();
1137 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1138 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1139 This function does not return.\n\n\
1140 An error symbol is a symbol with an `error-conditions' property\n\
1141 that is a list of condition names.\n\
1142 A handler for any of those names will get to handle this signal.\n\
1143 The symbol `error' should normally be one of them.\n\
1145 DATA should be a list. Its elements are printed as part of the error message.\n\
1146 If the signal is handled, DATA is made available to the handler.\n\
1147 See also the function `condition-case'.")
1148 (error_symbol, data)
1149 Lisp_Object error_symbol, data;
1151 register struct handler *allhandlers = handlerlist;
1152 Lisp_Object conditions;
1153 extern int gc_in_progress;
1154 extern int waiting_for_input;
1155 Lisp_Object debugger_value;
1157 quit_error_check ();
1158 immediate_quit = 0;
1159 if (gc_in_progress || waiting_for_input)
1160 abort ();
1162 #ifdef HAVE_X_WINDOWS
1163 TOTALLY_UNBLOCK_INPUT;
1164 #endif
1166 conditions = Fget (error_symbol, Qerror_conditions);
1168 for (; handlerlist; handlerlist = handlerlist->next)
1170 register Lisp_Object clause;
1171 clause = find_handler_clause (handlerlist->handler, conditions,
1172 error_symbol, data, &debugger_value);
1174 #if 0 /* Most callers are not prepared to handle gc if this returns.
1175 So, since this feature is not very useful, take it out. */
1176 /* If have called debugger and user wants to continue,
1177 just return nil. */
1178 if (EQ (clause, Qlambda))
1179 return debugger_value;
1180 #else
1181 if (EQ (clause, Qlambda))
1183 /* We can't return values to code which signalled an error, but we
1184 can continue code which has signalled a quit. */
1185 if (EQ (error_symbol, Qquit))
1186 return Qnil;
1187 else
1188 error ("Cannot return from the debugger in an error");
1190 #endif
1192 if (!NILP (clause))
1194 Lisp_Object unwind_data;
1195 struct handler *h = handlerlist;
1197 handlerlist = allhandlers;
1198 if (data == memory_signal_data)
1199 unwind_data = memory_signal_data;
1200 else
1201 unwind_data = Fcons (error_symbol, data);
1202 h->chosen_clause = clause;
1203 unwind_to_catch (h->tag, unwind_data);
1207 handlerlist = allhandlers;
1208 /* If no handler is present now, try to run the debugger,
1209 and if that fails, throw to top level. */
1210 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1211 Fthrow (Qtop_level, Qt);
1214 /* Return nonzero iff LIST is a non-nil atom or
1215 a list containing one of CONDITIONS. */
1217 static int
1218 wants_debugger (list, conditions)
1219 Lisp_Object list, conditions;
1221 if (NILP (list))
1222 return 0;
1223 if (! CONSP (list))
1224 return 1;
1226 while (CONSP (conditions))
1228 Lisp_Object this, tail;
1229 this = XCONS (conditions)->car;
1230 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
1231 if (EQ (XCONS (tail)->car, this))
1232 return 1;
1233 conditions = XCONS (conditions)->cdr;
1235 return 0;
1238 /* Value of Qlambda means we have called debugger and user has continued.
1239 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1241 static Lisp_Object
1242 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1243 Lisp_Object handlers, conditions, sig, data;
1244 Lisp_Object *debugger_value_ptr;
1246 register Lisp_Object h;
1247 register Lisp_Object tem;
1249 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1250 return Qt;
1251 if (EQ (handlers, Qerror)) /* error is used similarly, but means display a backtrace too */
1253 if (wants_debugger (Vstack_trace_on_error, conditions))
1254 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace, Qnil);
1255 if ((EQ (sig, Qquit)
1256 ? debug_on_quit
1257 : wants_debugger (Vdebug_on_error, conditions))
1258 && when_entered_debugger < num_nonmacro_input_chars)
1260 int count = specpdl_ptr - specpdl;
1261 specbind (Qdebug_on_error, Qnil);
1262 *debugger_value_ptr =
1263 call_debugger (Fcons (Qerror,
1264 Fcons (Fcons (sig, data),
1265 Qnil)));
1266 return unbind_to (count, Qlambda);
1268 return Qt;
1270 for (h = handlers; CONSP (h); h = Fcdr (h))
1272 Lisp_Object handler, condit;
1274 handler = Fcar (h);
1275 if (!CONSP (handler))
1276 continue;
1277 condit = Fcar (handler);
1278 /* Handle a single condition name in handler HANDLER. */
1279 if (SYMBOLP (condit))
1281 tem = Fmemq (Fcar (handler), conditions);
1282 if (!NILP (tem))
1283 return handler;
1285 /* Handle a list of condition names in handler HANDLER. */
1286 else if (CONSP (condit))
1288 while (CONSP (condit))
1290 tem = Fmemq (Fcar (condit), conditions);
1291 if (!NILP (tem))
1292 return handler;
1293 condit = XCONS (condit)->cdr;
1297 return Qnil;
1300 /* dump an error message; called like printf */
1302 /* VARARGS 1 */
1303 void
1304 error (m, a1, a2, a3)
1305 char *m;
1306 char *a1, *a2, *a3;
1308 char buf[200];
1309 int size = 200;
1310 int mlen;
1311 char *buffer = buf;
1312 char *args[3];
1313 int allocated = 0;
1314 Lisp_Object string;
1316 args[0] = a1;
1317 args[1] = a2;
1318 args[2] = a3;
1320 mlen = strlen (m);
1322 while (1)
1324 int used = doprnt (buf, size, m, m + mlen, 3, args);
1325 if (used < size)
1326 break;
1327 size *= 2;
1328 if (allocated)
1329 buffer = (char *) xrealloc (buffer, size);
1330 buffer = (char *) xmalloc (size);
1333 string = build_string (buf);
1334 if (allocated)
1335 free (buffer);
1337 Fsignal (Qerror, Fcons (string, Qnil));
1340 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1341 "T if FUNCTION makes provisions for interactive calling.\n\
1342 This means it contains a description for how to read arguments to give it.\n\
1343 The value is nil for an invalid function or a symbol with no function\n\
1344 definition.\n\
1346 Interactively callable functions include strings and vectors (treated\n\
1347 as keyboard macros), lambda-expressions that contain a top-level call\n\
1348 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1349 fourth argument, and some of the built-in functions of Lisp.\n\
1351 Also, a symbol satisfies `commandp' if its function definition does so.")
1352 (function)
1353 Lisp_Object function;
1355 register Lisp_Object fun;
1356 register Lisp_Object funcar;
1357 register Lisp_Object tem;
1358 register int i = 0;
1360 fun = function;
1362 fun = indirect_function (fun);
1363 if (EQ (fun, Qunbound))
1364 return Qnil;
1366 /* Emacs primitives are interactive if their DEFUN specifies an
1367 interactive spec. */
1368 if (XTYPE (fun) == Lisp_Subr)
1370 if (XSUBR (fun)->prompt)
1371 return Qt;
1372 else
1373 return Qnil;
1376 /* Bytecode objects are interactive if they are long enough to
1377 have an element whose index is COMPILED_INTERACTIVE, which is
1378 where the interactive spec is stored. */
1379 else if (XTYPE (fun) == Lisp_Compiled)
1380 return (XVECTOR (fun)->size > COMPILED_INTERACTIVE
1381 ? Qt : Qnil);
1383 /* Strings and vectors are keyboard macros. */
1384 if (XTYPE (fun) == Lisp_String
1385 || XTYPE (fun) == Lisp_Vector)
1386 return Qt;
1388 /* Lists may represent commands. */
1389 if (!CONSP (fun))
1390 return Qnil;
1391 funcar = Fcar (fun);
1392 if (XTYPE (funcar) != Lisp_Symbol)
1393 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1394 if (EQ (funcar, Qlambda))
1395 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1396 if (EQ (funcar, Qmocklisp))
1397 return Qt; /* All mocklisp functions can be called interactively */
1398 if (EQ (funcar, Qautoload))
1399 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1400 else
1401 return Qnil;
1404 /* ARGSUSED */
1405 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1406 "Define FUNCTION to autoload from FILE.\n\
1407 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1408 Third arg DOCSTRING is documentation for the function.\n\
1409 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1410 Fifth arg TYPE indicates the type of the object:\n\
1411 nil or omitted says FUNCTION is a function,\n\
1412 `keymap' says FUNCTION is really a keymap, and\n\
1413 `macro' or t says FUNCTION is really a macro.\n\
1414 Third through fifth args give info about the real definition.\n\
1415 They default to nil.\n\
1416 If FUNCTION is already defined other than as an autoload,\n\
1417 this does nothing and returns nil.")
1418 (function, file, docstring, interactive, type)
1419 Lisp_Object function, file, docstring, interactive, type;
1421 #ifdef NO_ARG_ARRAY
1422 Lisp_Object args[4];
1423 #endif
1425 CHECK_SYMBOL (function, 0);
1426 CHECK_STRING (file, 1);
1428 /* If function is defined and not as an autoload, don't override */
1429 if (!EQ (XSYMBOL (function)->function, Qunbound)
1430 && !(XTYPE (XSYMBOL (function)->function) == Lisp_Cons
1431 && EQ (XCONS (XSYMBOL (function)->function)->car, Qautoload)))
1432 return Qnil;
1434 #ifdef NO_ARG_ARRAY
1435 args[0] = file;
1436 args[1] = docstring;
1437 args[2] = interactive;
1438 args[3] = type;
1440 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1441 #else /* NO_ARG_ARRAY */
1442 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1443 #endif /* not NO_ARG_ARRAY */
1446 Lisp_Object
1447 un_autoload (oldqueue)
1448 Lisp_Object oldqueue;
1450 register Lisp_Object queue, first, second;
1452 /* Queue to unwind is current value of Vautoload_queue.
1453 oldqueue is the shadowed value to leave in Vautoload_queue. */
1454 queue = Vautoload_queue;
1455 Vautoload_queue = oldqueue;
1456 while (CONSP (queue))
1458 first = Fcar (queue);
1459 second = Fcdr (first);
1460 first = Fcar (first);
1461 if (EQ (second, Qnil))
1462 Vfeatures = first;
1463 else
1464 Ffset (first, second);
1465 queue = Fcdr (queue);
1467 return Qnil;
1470 do_autoload (fundef, funname)
1471 Lisp_Object fundef, funname;
1473 int count = specpdl_ptr - specpdl;
1474 Lisp_Object fun, val, queue, first, second;
1476 fun = funname;
1477 CHECK_SYMBOL (funname, 0);
1479 /* Value saved here is to be restored into Vautoload_queue */
1480 record_unwind_protect (un_autoload, Vautoload_queue);
1481 Vautoload_queue = Qt;
1482 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);
1484 /* Save the old autoloads, in case we ever do an unload. */
1485 queue = Vautoload_queue;
1486 while (CONSP (queue))
1488 first = Fcar (queue);
1489 second = Fcdr (first);
1490 first = Fcar (first);
1492 /* Note: This test is subtle. The cdr of an autoload-queue entry
1493 may be an atom if the autoload entry was generated by a defalias
1494 or fset. */
1495 if (CONSP (second))
1496 Fput (first, Qautoload, (Fcdr (second)));
1498 queue = Fcdr (queue);
1501 /* Once loading finishes, don't undo it. */
1502 Vautoload_queue = Qt;
1503 unbind_to (count, Qnil);
1505 fun = Findirect_function (fun);
1507 if (!NILP (Fequal (fun, fundef)))
1508 error ("Autoloading failed to define function %s",
1509 XSYMBOL (funname)->name->data);
1512 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1513 "Evaluate FORM and return its value.")
1514 (form)
1515 Lisp_Object form;
1517 Lisp_Object fun, val, original_fun, original_args;
1518 Lisp_Object funcar;
1519 struct backtrace backtrace;
1520 struct gcpro gcpro1, gcpro2, gcpro3;
1522 if (XTYPE (form) == Lisp_Symbol)
1524 if (EQ (Vmocklisp_arguments, Qt))
1525 return Fsymbol_value (form);
1526 val = Fsymbol_value (form);
1527 if (NILP (val))
1528 XFASTINT (val) = 0;
1529 else if (EQ (val, Qt))
1530 XFASTINT (val) = 1;
1531 return val;
1533 if (!CONSP (form))
1534 return form;
1536 QUIT;
1537 if (consing_since_gc > gc_cons_threshold)
1539 GCPRO1 (form);
1540 Fgarbage_collect ();
1541 UNGCPRO;
1544 if (++lisp_eval_depth > max_lisp_eval_depth)
1546 if (max_lisp_eval_depth < 100)
1547 max_lisp_eval_depth = 100;
1548 if (lisp_eval_depth > max_lisp_eval_depth)
1549 error ("Lisp nesting exceeds max-lisp-eval-depth");
1552 original_fun = Fcar (form);
1553 original_args = Fcdr (form);
1555 backtrace.next = backtrace_list;
1556 backtrace_list = &backtrace;
1557 backtrace.function = &original_fun; /* This also protects them from gc */
1558 backtrace.args = &original_args;
1559 backtrace.nargs = UNEVALLED;
1560 backtrace.evalargs = 1;
1561 backtrace.debug_on_exit = 0;
1563 if (debug_on_next_call)
1564 do_debug_on_call (Qt);
1566 /* At this point, only original_fun and original_args
1567 have values that will be used below */
1568 retry:
1569 fun = Findirect_function (original_fun);
1571 if (XTYPE (fun) == Lisp_Subr)
1573 Lisp_Object numargs;
1574 Lisp_Object argvals[7];
1575 Lisp_Object args_left;
1576 register int i, maxargs;
1578 args_left = original_args;
1579 numargs = Flength (args_left);
1581 if (XINT (numargs) < XSUBR (fun)->min_args ||
1582 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1583 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1585 if (XSUBR (fun)->max_args == UNEVALLED)
1587 backtrace.evalargs = 0;
1588 val = (*XSUBR (fun)->function) (args_left);
1589 goto done;
1592 if (XSUBR (fun)->max_args == MANY)
1594 /* Pass a vector of evaluated arguments */
1595 Lisp_Object *vals;
1596 register int argnum = 0;
1598 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1600 GCPRO3 (args_left, fun, fun);
1601 gcpro3.var = vals;
1602 gcpro3.nvars = 0;
1604 while (!NILP (args_left))
1606 vals[argnum++] = Feval (Fcar (args_left));
1607 args_left = Fcdr (args_left);
1608 gcpro3.nvars = argnum;
1611 backtrace.args = vals;
1612 backtrace.nargs = XINT (numargs);
1614 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1615 UNGCPRO;
1616 goto done;
1619 GCPRO3 (args_left, fun, fun);
1620 gcpro3.var = argvals;
1621 gcpro3.nvars = 0;
1623 maxargs = XSUBR (fun)->max_args;
1624 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1626 argvals[i] = Feval (Fcar (args_left));
1627 gcpro3.nvars = ++i;
1630 UNGCPRO;
1632 backtrace.args = argvals;
1633 backtrace.nargs = XINT (numargs);
1635 switch (i)
1637 case 0:
1638 val = (*XSUBR (fun)->function) ();
1639 goto done;
1640 case 1:
1641 val = (*XSUBR (fun)->function) (argvals[0]);
1642 goto done;
1643 case 2:
1644 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1645 goto done;
1646 case 3:
1647 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1648 argvals[2]);
1649 goto done;
1650 case 4:
1651 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1652 argvals[2], argvals[3]);
1653 goto done;
1654 case 5:
1655 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1656 argvals[3], argvals[4]);
1657 goto done;
1658 case 6:
1659 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1660 argvals[3], argvals[4], argvals[5]);
1661 goto done;
1662 case 7:
1663 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1664 argvals[3], argvals[4], argvals[5],
1665 argvals[6]);
1666 goto done;
1668 default:
1669 /* Someone has created a subr that takes more arguments than
1670 is supported by this code. We need to either rewrite the
1671 subr to use a different argument protocol, or add more
1672 cases to this switch. */
1673 abort ();
1676 if (XTYPE (fun) == Lisp_Compiled)
1677 val = apply_lambda (fun, original_args, 1);
1678 else
1680 if (!CONSP (fun))
1681 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1682 funcar = Fcar (fun);
1683 if (XTYPE (funcar) != Lisp_Symbol)
1684 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1685 if (EQ (funcar, Qautoload))
1687 do_autoload (fun, original_fun);
1688 goto retry;
1690 if (EQ (funcar, Qmacro))
1691 val = Feval (apply1 (Fcdr (fun), original_args));
1692 else if (EQ (funcar, Qlambda))
1693 val = apply_lambda (fun, original_args, 1);
1694 else if (EQ (funcar, Qmocklisp))
1695 val = ml_apply (fun, original_args);
1696 else
1697 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1699 done:
1700 if (!EQ (Vmocklisp_arguments, Qt))
1702 if (NILP (val))
1703 XFASTINT (val) = 0;
1704 else if (EQ (val, Qt))
1705 XFASTINT (val) = 1;
1707 lisp_eval_depth--;
1708 if (backtrace.debug_on_exit)
1709 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1710 backtrace_list = backtrace.next;
1711 return val;
1714 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1715 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1716 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1717 (nargs, args)
1718 int nargs;
1719 Lisp_Object *args;
1721 register int i, numargs;
1722 register Lisp_Object spread_arg;
1723 register Lisp_Object *funcall_args;
1724 Lisp_Object fun;
1725 struct gcpro gcpro1;
1727 fun = args [0];
1728 funcall_args = 0;
1729 spread_arg = args [nargs - 1];
1730 CHECK_LIST (spread_arg, nargs);
1732 numargs = XINT (Flength (spread_arg));
1734 if (numargs == 0)
1735 return Ffuncall (nargs - 1, args);
1736 else if (numargs == 1)
1738 args [nargs - 1] = XCONS (spread_arg)->car;
1739 return Ffuncall (nargs, args);
1742 numargs += nargs - 2;
1744 fun = indirect_function (fun);
1745 if (EQ (fun, Qunbound))
1747 /* Let funcall get the error */
1748 fun = args[0];
1749 goto funcall;
1752 if (XTYPE (fun) == Lisp_Subr)
1754 if (numargs < XSUBR (fun)->min_args
1755 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1756 goto funcall; /* Let funcall get the error */
1757 else if (XSUBR (fun)->max_args > numargs)
1759 /* Avoid making funcall cons up a yet another new vector of arguments
1760 by explicitly supplying nil's for optional values */
1761 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
1762 * sizeof (Lisp_Object));
1763 for (i = numargs; i < XSUBR (fun)->max_args;)
1764 funcall_args[++i] = Qnil;
1765 GCPRO1 (*funcall_args);
1766 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
1769 funcall:
1770 /* We add 1 to numargs because funcall_args includes the
1771 function itself as well as its arguments. */
1772 if (!funcall_args)
1774 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1775 * sizeof (Lisp_Object));
1776 GCPRO1 (*funcall_args);
1777 gcpro1.nvars = 1 + numargs;
1780 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
1781 /* Spread the last arg we got. Its first element goes in
1782 the slot that it used to occupy, hence this value of I. */
1783 i = nargs - 1;
1784 while (!NILP (spread_arg))
1786 funcall_args [i++] = XCONS (spread_arg)->car;
1787 spread_arg = XCONS (spread_arg)->cdr;
1790 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
1793 /* Apply fn to arg */
1794 Lisp_Object
1795 apply1 (fn, arg)
1796 Lisp_Object fn, arg;
1798 struct gcpro gcpro1;
1800 GCPRO1 (fn);
1801 if (NILP (arg))
1802 RETURN_UNGCPRO (Ffuncall (1, &fn));
1803 gcpro1.nvars = 2;
1804 #ifdef NO_ARG_ARRAY
1806 Lisp_Object args[2];
1807 args[0] = fn;
1808 args[1] = arg;
1809 gcpro1.var = args;
1810 RETURN_UNGCPRO (Fapply (2, args));
1812 #else /* not NO_ARG_ARRAY */
1813 RETURN_UNGCPRO (Fapply (2, &fn));
1814 #endif /* not NO_ARG_ARRAY */
1817 /* Call function fn on no arguments */
1818 Lisp_Object
1819 call0 (fn)
1820 Lisp_Object fn;
1822 struct gcpro gcpro1;
1824 GCPRO1 (fn);
1825 RETURN_UNGCPRO (Ffuncall (1, &fn));
1828 /* Call function fn with 1 argument arg1 */
1829 /* ARGSUSED */
1830 Lisp_Object
1831 call1 (fn, arg1)
1832 Lisp_Object fn, arg1;
1834 struct gcpro gcpro1;
1835 #ifdef NO_ARG_ARRAY
1836 Lisp_Object args[2];
1838 args[0] = fn;
1839 args[1] = arg1;
1840 GCPRO1 (args[0]);
1841 gcpro1.nvars = 2;
1842 RETURN_UNGCPRO (Ffuncall (2, args));
1843 #else /* not NO_ARG_ARRAY */
1844 GCPRO1 (fn);
1845 gcpro1.nvars = 2;
1846 RETURN_UNGCPRO (Ffuncall (2, &fn));
1847 #endif /* not NO_ARG_ARRAY */
1850 /* Call function fn with 2 arguments arg1, arg2 */
1851 /* ARGSUSED */
1852 Lisp_Object
1853 call2 (fn, arg1, arg2)
1854 Lisp_Object fn, arg1, arg2;
1856 struct gcpro gcpro1;
1857 #ifdef NO_ARG_ARRAY
1858 Lisp_Object args[3];
1859 args[0] = fn;
1860 args[1] = arg1;
1861 args[2] = arg2;
1862 GCPRO1 (args[0]);
1863 gcpro1.nvars = 3;
1864 RETURN_UNGCPRO (Ffuncall (3, args));
1865 #else /* not NO_ARG_ARRAY */
1866 GCPRO1 (fn);
1867 gcpro1.nvars = 3;
1868 RETURN_UNGCPRO (Ffuncall (3, &fn));
1869 #endif /* not NO_ARG_ARRAY */
1872 /* Call function fn with 3 arguments arg1, arg2, arg3 */
1873 /* ARGSUSED */
1874 Lisp_Object
1875 call3 (fn, arg1, arg2, arg3)
1876 Lisp_Object fn, arg1, arg2, arg3;
1878 struct gcpro gcpro1;
1879 #ifdef NO_ARG_ARRAY
1880 Lisp_Object args[4];
1881 args[0] = fn;
1882 args[1] = arg1;
1883 args[2] = arg2;
1884 args[3] = arg3;
1885 GCPRO1 (args[0]);
1886 gcpro1.nvars = 4;
1887 RETURN_UNGCPRO (Ffuncall (4, args));
1888 #else /* not NO_ARG_ARRAY */
1889 GCPRO1 (fn);
1890 gcpro1.nvars = 4;
1891 RETURN_UNGCPRO (Ffuncall (4, &fn));
1892 #endif /* not NO_ARG_ARRAY */
1895 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
1896 /* ARGSUSED */
1897 Lisp_Object
1898 call4 (fn, arg1, arg2, arg3, arg4)
1899 Lisp_Object fn, arg1, arg2, arg3, arg4;
1901 struct gcpro gcpro1;
1902 #ifdef NO_ARG_ARRAY
1903 Lisp_Object args[5];
1904 args[0] = fn;
1905 args[1] = arg1;
1906 args[2] = arg2;
1907 args[3] = arg3;
1908 args[4] = arg4;
1909 GCPRO1 (args[0]);
1910 gcpro1.nvars = 5;
1911 RETURN_UNGCPRO (Ffuncall (5, args));
1912 #else /* not NO_ARG_ARRAY */
1913 GCPRO1 (fn);
1914 gcpro1.nvars = 5;
1915 RETURN_UNGCPRO (Ffuncall (5, &fn));
1916 #endif /* not NO_ARG_ARRAY */
1919 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
1920 /* ARGSUSED */
1921 Lisp_Object
1922 call5 (fn, arg1, arg2, arg3, arg4, arg5)
1923 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
1925 struct gcpro gcpro1;
1926 #ifdef NO_ARG_ARRAY
1927 Lisp_Object args[6];
1928 args[0] = fn;
1929 args[1] = arg1;
1930 args[2] = arg2;
1931 args[3] = arg3;
1932 args[4] = arg4;
1933 args[5] = arg5;
1934 GCPRO1 (args[0]);
1935 gcpro1.nvars = 6;
1936 RETURN_UNGCPRO (Ffuncall (6, args));
1937 #else /* not NO_ARG_ARRAY */
1938 GCPRO1 (fn);
1939 gcpro1.nvars = 6;
1940 RETURN_UNGCPRO (Ffuncall (6, &fn));
1941 #endif /* not NO_ARG_ARRAY */
1944 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
1945 /* ARGSUSED */
1946 Lisp_Object
1947 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
1948 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
1950 struct gcpro gcpro1;
1951 #ifdef NO_ARG_ARRAY
1952 Lisp_Object args[7];
1953 args[0] = fn;
1954 args[1] = arg1;
1955 args[2] = arg2;
1956 args[3] = arg3;
1957 args[4] = arg4;
1958 args[5] = arg5;
1959 args[6] = arg6;
1960 GCPRO1 (args[0]);
1961 gcpro1.nvars = 7;
1962 RETURN_UNGCPRO (Ffuncall (7, args));
1963 #else /* not NO_ARG_ARRAY */
1964 GCPRO1 (fn);
1965 gcpro1.nvars = 7;
1966 RETURN_UNGCPRO (Ffuncall (7, &fn));
1967 #endif /* not NO_ARG_ARRAY */
1970 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
1971 "Call first argument as a function, passing remaining arguments to it.\n\
1972 Thus, (funcall 'cons 'x 'y) returns (x . y).")
1973 (nargs, args)
1974 int nargs;
1975 Lisp_Object *args;
1977 Lisp_Object fun;
1978 Lisp_Object funcar;
1979 int numargs = nargs - 1;
1980 Lisp_Object lisp_numargs;
1981 Lisp_Object val;
1982 struct backtrace backtrace;
1983 register Lisp_Object *internal_args;
1984 register int i;
1986 QUIT;
1987 if (consing_since_gc > gc_cons_threshold)
1988 Fgarbage_collect ();
1990 if (++lisp_eval_depth > max_lisp_eval_depth)
1992 if (max_lisp_eval_depth < 100)
1993 max_lisp_eval_depth = 100;
1994 if (lisp_eval_depth > max_lisp_eval_depth)
1995 error ("Lisp nesting exceeds max-lisp-eval-depth");
1998 backtrace.next = backtrace_list;
1999 backtrace_list = &backtrace;
2000 backtrace.function = &args[0];
2001 backtrace.args = &args[1];
2002 backtrace.nargs = nargs - 1;
2003 backtrace.evalargs = 0;
2004 backtrace.debug_on_exit = 0;
2006 if (debug_on_next_call)
2007 do_debug_on_call (Qlambda);
2009 retry:
2011 fun = args[0];
2013 fun = Findirect_function (fun);
2015 if (XTYPE (fun) == Lisp_Subr)
2017 if (numargs < XSUBR (fun)->min_args
2018 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2020 XFASTINT (lisp_numargs) = numargs;
2021 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2024 if (XSUBR (fun)->max_args == UNEVALLED)
2025 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2027 if (XSUBR (fun)->max_args == MANY)
2029 val = (*XSUBR (fun)->function) (numargs, args + 1);
2030 goto done;
2033 if (XSUBR (fun)->max_args > numargs)
2035 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2036 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2037 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2038 internal_args[i] = Qnil;
2040 else
2041 internal_args = args + 1;
2042 switch (XSUBR (fun)->max_args)
2044 case 0:
2045 val = (*XSUBR (fun)->function) ();
2046 goto done;
2047 case 1:
2048 val = (*XSUBR (fun)->function) (internal_args[0]);
2049 goto done;
2050 case 2:
2051 val = (*XSUBR (fun)->function) (internal_args[0],
2052 internal_args[1]);
2053 goto done;
2054 case 3:
2055 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2056 internal_args[2]);
2057 goto done;
2058 case 4:
2059 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2060 internal_args[2],
2061 internal_args[3]);
2062 goto done;
2063 case 5:
2064 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2065 internal_args[2], internal_args[3],
2066 internal_args[4]);
2067 goto done;
2068 case 6:
2069 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2070 internal_args[2], internal_args[3],
2071 internal_args[4], internal_args[5]);
2072 goto done;
2073 case 7:
2074 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2075 internal_args[2], internal_args[3],
2076 internal_args[4], internal_args[5],
2077 internal_args[6]);
2078 goto done;
2080 default:
2082 /* If a subr takes more than 6 arguments without using MANY
2083 or UNEVALLED, we need to extend this function to support it.
2084 Until this is done, there is no way to call the function. */
2085 abort ();
2088 if (XTYPE (fun) == Lisp_Compiled)
2089 val = funcall_lambda (fun, numargs, args + 1);
2090 else
2092 if (!CONSP (fun))
2093 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2094 funcar = Fcar (fun);
2095 if (XTYPE (funcar) != Lisp_Symbol)
2096 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2097 if (EQ (funcar, Qlambda))
2098 val = funcall_lambda (fun, numargs, args + 1);
2099 else if (EQ (funcar, Qmocklisp))
2100 val = ml_apply (fun, Flist (numargs, args + 1));
2101 else if (EQ (funcar, Qautoload))
2103 do_autoload (fun, args[0]);
2104 goto retry;
2106 else
2107 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2109 done:
2110 lisp_eval_depth--;
2111 if (backtrace.debug_on_exit)
2112 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2113 backtrace_list = backtrace.next;
2114 return val;
2117 Lisp_Object
2118 apply_lambda (fun, args, eval_flag)
2119 Lisp_Object fun, args;
2120 int eval_flag;
2122 Lisp_Object args_left;
2123 Lisp_Object numargs;
2124 register Lisp_Object *arg_vector;
2125 struct gcpro gcpro1, gcpro2, gcpro3;
2126 register int i;
2127 register Lisp_Object tem;
2129 numargs = Flength (args);
2130 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2131 args_left = args;
2133 GCPRO3 (*arg_vector, args_left, fun);
2134 gcpro1.nvars = 0;
2136 for (i = 0; i < XINT (numargs);)
2138 tem = Fcar (args_left), args_left = Fcdr (args_left);
2139 if (eval_flag) tem = Feval (tem);
2140 arg_vector[i++] = tem;
2141 gcpro1.nvars = i;
2144 UNGCPRO;
2146 if (eval_flag)
2148 backtrace_list->args = arg_vector;
2149 backtrace_list->nargs = i;
2151 backtrace_list->evalargs = 0;
2152 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2154 /* Do the debug-on-exit now, while arg_vector still exists. */
2155 if (backtrace_list->debug_on_exit)
2156 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2157 /* Don't do it again when we return to eval. */
2158 backtrace_list->debug_on_exit = 0;
2159 return tem;
2162 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2163 and return the result of evaluation.
2164 FUN must be either a lambda-expression or a compiled-code object. */
2166 Lisp_Object
2167 funcall_lambda (fun, nargs, arg_vector)
2168 Lisp_Object fun;
2169 int nargs;
2170 register Lisp_Object *arg_vector;
2172 Lisp_Object val, tem;
2173 register Lisp_Object syms_left;
2174 Lisp_Object numargs;
2175 register Lisp_Object next;
2176 int count = specpdl_ptr - specpdl;
2177 register int i;
2178 int optional = 0, rest = 0;
2180 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2182 XFASTINT (numargs) = nargs;
2184 if (XTYPE (fun) == Lisp_Cons)
2185 syms_left = Fcar (Fcdr (fun));
2186 else if (XTYPE (fun) == Lisp_Compiled)
2187 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2188 else abort ();
2190 i = 0;
2191 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
2193 QUIT;
2194 next = Fcar (syms_left);
2195 while (XTYPE (next) != Lisp_Symbol)
2196 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2197 if (EQ (next, Qand_rest))
2198 rest = 1;
2199 else if (EQ (next, Qand_optional))
2200 optional = 1;
2201 else if (rest)
2203 specbind (next, Flist (nargs - i, &arg_vector[i]));
2204 i = nargs;
2206 else if (i < nargs)
2208 tem = arg_vector[i++];
2209 specbind (next, tem);
2211 else if (!optional)
2212 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2213 else
2214 specbind (next, Qnil);
2217 if (i < nargs)
2218 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2220 if (XTYPE (fun) == Lisp_Cons)
2221 val = Fprogn (Fcdr (Fcdr (fun)));
2222 else
2223 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2224 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2225 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2226 return unbind_to (count, val);
2229 void
2230 grow_specpdl ()
2232 register int count = specpdl_ptr - specpdl;
2233 if (specpdl_size >= max_specpdl_size)
2235 if (max_specpdl_size < 400)
2236 max_specpdl_size = 400;
2237 if (specpdl_size >= max_specpdl_size)
2239 if (!NILP (Vdebug_on_error))
2240 /* Leave room for some specpdl in the debugger. */
2241 max_specpdl_size = specpdl_size + 100;
2242 Fsignal (Qerror,
2243 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2246 specpdl_size *= 2;
2247 if (specpdl_size > max_specpdl_size)
2248 specpdl_size = max_specpdl_size;
2249 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2250 specpdl_ptr = specpdl + count;
2253 void
2254 specbind (symbol, value)
2255 Lisp_Object symbol, value;
2257 extern void store_symval_forwarding (); /* in eval.c */
2258 Lisp_Object ovalue;
2260 CHECK_SYMBOL (symbol, 0);
2262 if (specpdl_ptr == specpdl + specpdl_size)
2263 grow_specpdl ();
2264 specpdl_ptr->symbol = symbol;
2265 specpdl_ptr->func = 0;
2266 specpdl_ptr->old_value = ovalue = find_symbol_value (symbol);
2267 specpdl_ptr++;
2268 if (XTYPE (ovalue) == Lisp_Buffer_Objfwd)
2269 store_symval_forwarding (symbol, ovalue, value);
2270 else
2271 Fset (symbol, value);
2274 void
2275 record_unwind_protect (function, arg)
2276 Lisp_Object (*function)();
2277 Lisp_Object arg;
2279 if (specpdl_ptr == specpdl + specpdl_size)
2280 grow_specpdl ();
2281 specpdl_ptr->func = function;
2282 specpdl_ptr->symbol = Qnil;
2283 specpdl_ptr->old_value = arg;
2284 specpdl_ptr++;
2287 Lisp_Object
2288 unbind_to (count, value)
2289 int count;
2290 Lisp_Object value;
2292 int quitf = !NILP (Vquit_flag);
2293 struct gcpro gcpro1;
2295 GCPRO1 (value);
2297 Vquit_flag = Qnil;
2299 while (specpdl_ptr != specpdl + count)
2301 --specpdl_ptr;
2302 if (specpdl_ptr->func != 0)
2303 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2304 /* Note that a "binding" of nil is really an unwind protect,
2305 so in that case the "old value" is a list of forms to evaluate. */
2306 else if (NILP (specpdl_ptr->symbol))
2307 Fprogn (specpdl_ptr->old_value);
2308 else
2309 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
2311 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
2313 UNGCPRO;
2315 return value;
2318 #if 0
2320 /* Get the value of symbol's global binding, even if that binding
2321 is not now dynamically visible. */
2323 Lisp_Object
2324 top_level_value (symbol)
2325 Lisp_Object symbol;
2327 register struct specbinding *ptr = specpdl;
2329 CHECK_SYMBOL (symbol, 0);
2330 for (; ptr != specpdl_ptr; ptr++)
2332 if (EQ (ptr->symbol, symbol))
2333 return ptr->old_value;
2335 return Fsymbol_value (symbol);
2338 Lisp_Object
2339 top_level_set (symbol, newval)
2340 Lisp_Object symbol, newval;
2342 register struct specbinding *ptr = specpdl;
2344 CHECK_SYMBOL (symbol, 0);
2345 for (; ptr != specpdl_ptr; ptr++)
2347 if (EQ (ptr->symbol, symbol))
2349 ptr->old_value = newval;
2350 return newval;
2353 return Fset (symbol, newval);
2356 #endif /* 0 */
2358 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2359 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2360 The debugger is entered when that frame exits, if the flag is non-nil.")
2361 (level, flag)
2362 Lisp_Object level, flag;
2364 register struct backtrace *backlist = backtrace_list;
2365 register int i;
2367 CHECK_NUMBER (level, 0);
2369 for (i = 0; backlist && i < XINT (level); i++)
2371 backlist = backlist->next;
2374 if (backlist)
2375 backlist->debug_on_exit = !NILP (flag);
2377 return flag;
2380 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2381 "Print a trace of Lisp function calls currently active.\n\
2382 Output stream used is value of `standard-output'.")
2385 register struct backtrace *backlist = backtrace_list;
2386 register int i;
2387 Lisp_Object tail;
2388 Lisp_Object tem;
2389 extern Lisp_Object Vprint_level;
2390 struct gcpro gcpro1;
2392 XFASTINT (Vprint_level) = 3;
2394 tail = Qnil;
2395 GCPRO1 (tail);
2397 while (backlist)
2399 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2400 if (backlist->nargs == UNEVALLED)
2402 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2404 else
2406 tem = *backlist->function;
2407 Fprin1 (tem, Qnil); /* This can QUIT */
2408 write_string ("(", -1);
2409 if (backlist->nargs == MANY)
2411 for (tail = *backlist->args, i = 0;
2412 !NILP (tail);
2413 tail = Fcdr (tail), i++)
2415 if (i) write_string (" ", -1);
2416 Fprin1 (Fcar (tail), Qnil);
2419 else
2421 for (i = 0; i < backlist->nargs; i++)
2423 if (i) write_string (" ", -1);
2424 Fprin1 (backlist->args[i], Qnil);
2428 write_string (")\n", -1);
2429 backlist = backlist->next;
2432 Vprint_level = Qnil;
2433 UNGCPRO;
2434 return Qnil;
2437 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2438 "Return the function and arguments N frames up from current execution point.\n\
2439 If that frame has not evaluated the arguments yet (or is a special form),\n\
2440 the value is (nil FUNCTION ARG-FORMS...).\n\
2441 If that frame has evaluated its arguments and called its function already,\n\
2442 the value is (t FUNCTION ARG-VALUES...).\n\
2443 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2444 FUNCTION is whatever was supplied as car of evaluated list,\n\
2445 or a lambda expression for macro calls.\n\
2446 If N is more than the number of frames, the value is nil.")
2447 (nframes)
2448 Lisp_Object nframes;
2450 register struct backtrace *backlist = backtrace_list;
2451 register int i;
2452 Lisp_Object tem;
2454 CHECK_NATNUM (nframes, 0);
2456 /* Find the frame requested. */
2457 for (i = 0; i < XFASTINT (nframes); i++)
2458 backlist = backlist->next;
2460 if (!backlist)
2461 return Qnil;
2462 if (backlist->nargs == UNEVALLED)
2463 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2464 else
2466 if (backlist->nargs == MANY)
2467 tem = *backlist->args;
2468 else
2469 tem = Flist (backlist->nargs, backlist->args);
2471 return Fcons (Qt, Fcons (*backlist->function, tem));
2475 syms_of_eval ()
2477 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2478 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2480 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2481 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2482 This limit is to catch infinite recursions for you before they cause\n\
2483 actual stack overflow in C, which would be fatal for Emacs.\n\
2484 You can safely make it considerably larger than its default value,\n\
2485 if that proves inconveniently small.");
2487 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2488 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2489 Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2490 Vquit_flag = Qnil;
2492 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2493 "Non-nil inhibits C-g quitting from happening immediately.\n\
2494 Note that `quit-flag' will still be set by typing C-g,\n\
2495 so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
2496 To prevent this happening, set `quit-flag' to nil\n\
2497 before making `inhibit-quit' nil.");
2498 Vinhibit_quit = Qnil;
2500 Qinhibit_quit = intern ("inhibit-quit");
2501 staticpro (&Qinhibit_quit);
2503 Qautoload = intern ("autoload");
2504 staticpro (&Qautoload);
2506 Qdebug_on_error = intern ("debug-on-error");
2507 staticpro (&Qdebug_on_error);
2509 Qmacro = intern ("macro");
2510 staticpro (&Qmacro);
2512 /* Note that the process handling also uses Qexit, but we don't want
2513 to staticpro it twice, so we just do it here. */
2514 Qexit = intern ("exit");
2515 staticpro (&Qexit);
2517 Qinteractive = intern ("interactive");
2518 staticpro (&Qinteractive);
2520 Qcommandp = intern ("commandp");
2521 staticpro (&Qcommandp);
2523 Qdefun = intern ("defun");
2524 staticpro (&Qdefun);
2526 Qand_rest = intern ("&rest");
2527 staticpro (&Qand_rest);
2529 Qand_optional = intern ("&optional");
2530 staticpro (&Qand_optional);
2532 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
2533 "*Non-nil means automatically display a backtrace buffer\n\
2534 after any error that is handled by the editor command loop.\n\
2535 If the value is a list, an error only means to display a backtrace\n\
2536 if one of its condition symbols appears in the list.");
2537 Vstack_trace_on_error = Qnil;
2539 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
2540 "*Non-nil means enter debugger if an error is signaled.\n\
2541 Does not apply to errors handled by `condition-case'.\n\
2542 If the value is a list, an error only means to enter the debugger\n\
2543 if one of its condition symbols appears in the list.\n\
2544 See also variable `debug-on-quit'.");
2545 Vdebug_on_error = Qnil;
2547 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
2548 "*Non-nil means enter debugger if quit is signaled (C-G, for example).\n\
2549 Does not apply if quit is handled by a `condition-case'.");
2550 debug_on_quit = 0;
2552 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
2553 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2555 DEFVAR_LISP ("debugger", &Vdebugger,
2556 "Function to call to invoke debugger.\n\
2557 If due to frame exit, args are `exit' and the value being returned;\n\
2558 this function's value will be returned instead of that.\n\
2559 If due to error, args are `error' and a list of the args to `signal'.\n\
2560 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2561 If due to `eval' entry, one arg, t.");
2562 Vdebugger = Qnil;
2564 Qmocklisp_arguments = intern ("mocklisp-arguments");
2565 staticpro (&Qmocklisp_arguments);
2566 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
2567 "While in a mocklisp function, the list of its unevaluated args.");
2568 Vmocklisp_arguments = Qt;
2570 DEFVAR_LISP ("run-hooks", &Vrun_hooks,
2571 "Set to the function `run-hooks', if that function has been defined.\n\
2572 Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
2573 Vrun_hooks = Qnil;
2575 staticpro (&Vautoload_queue);
2576 Vautoload_queue = Qnil;
2578 defsubr (&Sor);
2579 defsubr (&Sand);
2580 defsubr (&Sif);
2581 defsubr (&Scond);
2582 defsubr (&Sprogn);
2583 defsubr (&Sprog1);
2584 defsubr (&Sprog2);
2585 defsubr (&Ssetq);
2586 defsubr (&Squote);
2587 defsubr (&Sfunction);
2588 defsubr (&Sdefun);
2589 defsubr (&Sdefmacro);
2590 defsubr (&Sdefvar);
2591 defsubr (&Sdefconst);
2592 defsubr (&Suser_variable_p);
2593 defsubr (&Slet);
2594 defsubr (&SletX);
2595 defsubr (&Swhile);
2596 defsubr (&Smacroexpand);
2597 defsubr (&Scatch);
2598 defsubr (&Sthrow);
2599 defsubr (&Sunwind_protect);
2600 defsubr (&Scondition_case);
2601 defsubr (&Ssignal);
2602 defsubr (&Sinteractive_p);
2603 defsubr (&Scommandp);
2604 defsubr (&Sautoload);
2605 defsubr (&Seval);
2606 defsubr (&Sapply);
2607 defsubr (&Sfuncall);
2608 defsubr (&Sbacktrace_debug);
2609 defsubr (&Sbacktrace);
2610 defsubr (&Sbacktrace_frame);