(find_handler_clause): Use PROTOTYPES.
[emacs.git] / src / eval.c
blob7034f43ca5aaff26f386e4ed16ef42e8694b0257
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include <setjmp.h>
31 /* This definition is duplicated in alloc.c and keyboard.c */
32 /* Putting it in lisp.h makes cc bomb out! */
34 struct backtrace
36 struct backtrace *next;
37 Lisp_Object *function;
38 Lisp_Object *args; /* Points to vector of args. */
39 int nargs; /* Length of vector.
40 If nargs is UNEVALLED, args points to slot holding
41 list of unevalled args */
42 char evalargs;
43 /* Nonzero means call value of debugger when done with this operation. */
44 char debug_on_exit;
47 struct backtrace *backtrace_list;
49 /* This structure helps implement the `catch' and `throw' control
50 structure. A struct catchtag contains all the information needed
51 to restore the state of the interpreter after a non-local jump.
53 Handlers for error conditions (represented by `struct handler'
54 structures) just point to a catch tag to do the cleanup required
55 for their jumps.
57 catchtag structures are chained together in the C calling stack;
58 the `next' member points to the next outer catchtag.
60 A call like (throw TAG VAL) searches for a catchtag whose `tag'
61 member is TAG, and then unbinds to it. The `val' member is used to
62 hold VAL while the stack is unwound; `val' is returned as the value
63 of the catch form.
65 All the other members are concerned with restoring the interpreter
66 state. */
67 struct catchtag
69 Lisp_Object tag;
70 Lisp_Object val;
71 struct catchtag *next;
72 struct gcpro *gcpro;
73 jmp_buf jmp;
74 struct backtrace *backlist;
75 struct handler *handlerlist;
76 int lisp_eval_depth;
77 int pdlcount;
78 int poll_suppress_count;
79 struct byte_stack *byte_stack;
82 struct catchtag *catchlist;
84 #ifdef DEBUG_GCPRO
85 /* Count levels of GCPRO to detect failure to UNGCPRO. */
86 int gcpro_level;
87 #endif
89 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
90 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
91 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
92 Lisp_Object Qand_rest, Qand_optional;
93 Lisp_Object Qdebug_on_error;
95 /* This holds either the symbol `run-hooks' or nil.
96 It is nil at an early stage of startup, and when Emacs
97 is shutting down. */
98 Lisp_Object Vrun_hooks;
100 /* Non-nil means record all fset's and provide's, to be undone
101 if the file being autoloaded is not fully loaded.
102 They are recorded by being consed onto the front of Vautoload_queue:
103 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
105 Lisp_Object Vautoload_queue;
107 /* Current number of specbindings allocated in specpdl. */
108 int specpdl_size;
110 /* Pointer to beginning of specpdl. */
111 struct specbinding *specpdl;
113 /* Pointer to first unused element in specpdl. */
114 struct specbinding *specpdl_ptr;
116 /* Maximum size allowed for specpdl allocation */
117 int max_specpdl_size;
119 /* Depth in Lisp evaluations and function calls. */
120 int lisp_eval_depth;
122 /* Maximum allowed depth in Lisp evaluations and function calls. */
123 int max_lisp_eval_depth;
125 /* Nonzero means enter debugger before next function call */
126 int debug_on_next_call;
128 /* Non-zero means debuffer may continue. This is zero when the
129 debugger is called during redisplay, where it might not be safe to
130 continue the interrupted redisplay. */
132 int debugger_may_continue;
134 /* List of conditions (non-nil atom means all) which cause a backtrace
135 if an error is handled by the command loop's error handler. */
136 Lisp_Object Vstack_trace_on_error;
138 /* List of conditions (non-nil atom means all) which enter the debugger
139 if an error is handled by the command loop's error handler. */
140 Lisp_Object Vdebug_on_error;
142 /* List of conditions and regexps specifying error messages which
143 do not enter the debugger even if Vdebug_on_errors says they should. */
144 Lisp_Object Vdebug_ignored_errors;
146 /* Non-nil means call the debugger even if the error will be handled. */
147 Lisp_Object Vdebug_on_signal;
149 /* Hook for edebug to use. */
150 Lisp_Object Vsignal_hook_function;
152 /* Nonzero means enter debugger if a quit signal
153 is handled by the command loop's error handler. */
154 int debug_on_quit;
156 /* The value of num_nonmacro_input_events as of the last time we
157 started to enter the debugger. If we decide to enter the debugger
158 again when this is still equal to num_nonmacro_input_events, then we
159 know that the debugger itself has an error, and we should just
160 signal the error instead of entering an infinite loop of debugger
161 invocations. */
162 int when_entered_debugger;
164 Lisp_Object Vdebugger;
166 void specbind (), record_unwind_protect ();
168 Lisp_Object run_hook_with_args ();
170 Lisp_Object funcall_lambda ();
171 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
173 void
174 init_eval_once ()
176 specpdl_size = 50;
177 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
178 specpdl_ptr = specpdl;
179 max_specpdl_size = 600;
180 max_lisp_eval_depth = 300;
182 Vrun_hooks = Qnil;
185 void
186 init_eval ()
188 specpdl_ptr = specpdl;
189 catchlist = 0;
190 handlerlist = 0;
191 backtrace_list = 0;
192 Vquit_flag = Qnil;
193 debug_on_next_call = 0;
194 lisp_eval_depth = 0;
195 #ifdef DEBUG_GCPRO
196 gcpro_level = 0;
197 #endif
198 /* This is less than the initial value of num_nonmacro_input_events. */
199 when_entered_debugger = -1;
202 Lisp_Object
203 call_debugger (arg)
204 Lisp_Object arg;
206 int debug_while_redisplaying;
207 int count = specpdl_ptr - specpdl;
208 Lisp_Object val;
210 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
211 max_lisp_eval_depth = lisp_eval_depth + 20;
213 if (specpdl_size + 40 > max_specpdl_size)
214 max_specpdl_size = specpdl_size + 40;
216 debug_on_next_call = 0;
217 when_entered_debugger = num_nonmacro_input_events;
219 /* Resetting redisplaying_p to 0 makes sure that debug output is
220 displayed if the debugger is invoked during redisplay. */
221 debug_while_redisplaying = redisplaying_p;
222 redisplaying_p = 0;
223 specbind (intern ("debugger-may-continue"),
224 debug_while_redisplaying ? Qnil : Qt);
226 val = apply1 (Vdebugger, arg);
228 /* Interrupting redisplay and resuming it later is not safe under
229 all circumstances. So, when the debugger returns, abort the
230 interupted redisplay by going back to the top-level. */
231 if (debug_while_redisplaying)
232 Ftop_level ();
234 return unbind_to (count, val);
237 void
238 do_debug_on_call (code)
239 Lisp_Object code;
241 debug_on_next_call = 0;
242 backtrace_list->debug_on_exit = 1;
243 call_debugger (Fcons (code, Qnil));
246 /* NOTE!!! Every function that can call EVAL must protect its args
247 and temporaries from garbage collection while it needs them.
248 The definition of `For' shows what you have to do. */
250 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
251 "Eval args until one of them yields non-nil, then return that value.\n\
252 The remaining args are not evalled at all.\n\
253 If all args return nil, return nil.")
254 (args)
255 Lisp_Object args;
257 register Lisp_Object val;
258 Lisp_Object args_left;
259 struct gcpro gcpro1;
261 if (NILP(args))
262 return Qnil;
264 args_left = args;
265 GCPRO1 (args_left);
269 val = Feval (Fcar (args_left));
270 if (!NILP (val))
271 break;
272 args_left = Fcdr (args_left);
274 while (!NILP(args_left));
276 UNGCPRO;
277 return val;
280 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
281 "Eval args until one of them yields nil, then return nil.\n\
282 The remaining args are not evalled at all.\n\
283 If no arg yields nil, return the last arg's value.")
284 (args)
285 Lisp_Object args;
287 register Lisp_Object val;
288 Lisp_Object args_left;
289 struct gcpro gcpro1;
291 if (NILP(args))
292 return Qt;
294 args_left = args;
295 GCPRO1 (args_left);
299 val = Feval (Fcar (args_left));
300 if (NILP (val))
301 break;
302 args_left = Fcdr (args_left);
304 while (!NILP(args_left));
306 UNGCPRO;
307 return val;
310 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
311 "If COND yields non-nil, do THEN, else do ELSE...\n\
312 Returns the value of THEN or the value of the last of the ELSE's.\n\
313 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
314 If COND yields nil, and there are no ELSE's, the value is nil.")
315 (args)
316 Lisp_Object args;
318 register Lisp_Object cond;
319 struct gcpro gcpro1;
321 GCPRO1 (args);
322 cond = Feval (Fcar (args));
323 UNGCPRO;
325 if (!NILP (cond))
326 return Feval (Fcar (Fcdr (args)));
327 return Fprogn (Fcdr (Fcdr (args)));
330 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
331 "Try each clause until one succeeds.\n\
332 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
333 and, if the value is non-nil, this clause succeeds:\n\
334 then the expressions in BODY are evaluated and the last one's\n\
335 value is the value of the cond-form.\n\
336 If no clause succeeds, cond returns nil.\n\
337 If a clause has one element, as in (CONDITION),\n\
338 CONDITION's value if non-nil is returned from the cond-form.")
339 (args)
340 Lisp_Object args;
342 register Lisp_Object clause, val;
343 struct gcpro gcpro1;
345 val = Qnil;
346 GCPRO1 (args);
347 while (!NILP (args))
349 clause = Fcar (args);
350 val = Feval (Fcar (clause));
351 if (!NILP (val))
353 if (!EQ (XCDR (clause), Qnil))
354 val = Fprogn (XCDR (clause));
355 break;
357 args = XCDR (args);
359 UNGCPRO;
361 return val;
364 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
365 "Eval BODY forms sequentially and return value of last one.")
366 (args)
367 Lisp_Object args;
369 register Lisp_Object val, tem;
370 Lisp_Object args_left;
371 struct gcpro gcpro1;
373 /* In Mocklisp code, symbols at the front of the progn arglist
374 are to be bound to zero. */
375 if (!EQ (Vmocklisp_arguments, Qt))
377 val = make_number (0);
378 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
380 QUIT;
381 specbind (tem, val), args = Fcdr (args);
385 if (NILP(args))
386 return Qnil;
388 args_left = args;
389 GCPRO1 (args_left);
393 val = Feval (Fcar (args_left));
394 args_left = Fcdr (args_left);
396 while (!NILP(args_left));
398 UNGCPRO;
399 return val;
402 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
403 "Eval FIRST and BODY sequentially; value from FIRST.\n\
404 The value of FIRST is saved during the evaluation of the remaining args,\n\
405 whose values are discarded.")
406 (args)
407 Lisp_Object args;
409 Lisp_Object val;
410 register Lisp_Object args_left;
411 struct gcpro gcpro1, gcpro2;
412 register int argnum = 0;
414 if (NILP(args))
415 return Qnil;
417 args_left = args;
418 val = Qnil;
419 GCPRO2 (args, val);
423 if (!(argnum++))
424 val = Feval (Fcar (args_left));
425 else
426 Feval (Fcar (args_left));
427 args_left = Fcdr (args_left);
429 while (!NILP(args_left));
431 UNGCPRO;
432 return val;
435 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
436 "Eval X, Y and BODY sequentially; value from Y.\n\
437 The value of Y is saved during the evaluation of the remaining args,\n\
438 whose values are discarded.")
439 (args)
440 Lisp_Object args;
442 Lisp_Object val;
443 register Lisp_Object args_left;
444 struct gcpro gcpro1, gcpro2;
445 register int argnum = -1;
447 val = Qnil;
449 if (NILP (args))
450 return Qnil;
452 args_left = args;
453 val = Qnil;
454 GCPRO2 (args, val);
458 if (!(argnum++))
459 val = Feval (Fcar (args_left));
460 else
461 Feval (Fcar (args_left));
462 args_left = Fcdr (args_left);
464 while (!NILP (args_left));
466 UNGCPRO;
467 return val;
470 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
471 "Set each SYM to the value of its VAL.\n\
472 The symbols SYM are variables; they are literal (not evaluated).\n\
473 The values VAL are expressions; they are evaluated.\n\
474 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
475 The second VAL is not computed until after the first SYM is set, and so on;\n\
476 each VAL can use the new value of variables set earlier in the `setq'.\n\
477 The return value of the `setq' form is the value of the last VAL.")
478 (args)
479 Lisp_Object args;
481 register Lisp_Object args_left;
482 register Lisp_Object val, sym;
483 struct gcpro gcpro1;
485 if (NILP(args))
486 return Qnil;
488 args_left = args;
489 GCPRO1 (args);
493 val = Feval (Fcar (Fcdr (args_left)));
494 sym = Fcar (args_left);
495 Fset (sym, val);
496 args_left = Fcdr (Fcdr (args_left));
498 while (!NILP(args_left));
500 UNGCPRO;
501 return val;
504 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
505 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
506 (args)
507 Lisp_Object args;
509 return Fcar (args);
512 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
513 "Like `quote', but preferred for objects which are functions.\n\
514 In byte compilation, `function' causes its argument to be compiled.\n\
515 `quote' cannot do that.")
516 (args)
517 Lisp_Object args;
519 return Fcar (args);
522 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
523 "Return t if function in which this appears was called interactively.\n\
524 This means that the function was called with call-interactively (which\n\
525 includes being called as the binding of a key)\n\
526 and input is currently coming from the keyboard (not in keyboard macro).")
529 register struct backtrace *btp;
530 register Lisp_Object fun;
532 if (!INTERACTIVE)
533 return Qnil;
535 btp = backtrace_list;
537 /* If this isn't a byte-compiled function, there may be a frame at
538 the top for Finteractive_p itself. If so, skip it. */
539 fun = Findirect_function (*btp->function);
540 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
541 btp = btp->next;
543 /* If we're running an Emacs 18-style byte-compiled function, there
544 may be a frame for Fbytecode. Now, given the strictest
545 definition, this function isn't really being called
546 interactively, but because that's the way Emacs 18 always builds
547 byte-compiled functions, we'll accept it for now. */
548 if (EQ (*btp->function, Qbytecode))
549 btp = btp->next;
551 /* If this isn't a byte-compiled function, then we may now be
552 looking at several frames for special forms. Skip past them. */
553 while (btp &&
554 btp->nargs == UNEVALLED)
555 btp = btp->next;
557 /* btp now points at the frame of the innermost function that isn't
558 a special form, ignoring frames for Finteractive_p and/or
559 Fbytecode at the top. If this frame is for a built-in function
560 (such as load or eval-region) return nil. */
561 fun = Findirect_function (*btp->function);
562 if (SUBRP (fun))
563 return Qnil;
564 /* btp points to the frame of a Lisp function that called interactive-p.
565 Return t if that function was called interactively. */
566 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
567 return Qt;
568 return Qnil;
571 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
572 "Define NAME as a function.\n\
573 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
574 See also the function `interactive'.")
575 (args)
576 Lisp_Object args;
578 register Lisp_Object fn_name;
579 register Lisp_Object defn;
581 fn_name = Fcar (args);
582 defn = Fcons (Qlambda, Fcdr (args));
583 if (!NILP (Vpurify_flag))
584 defn = Fpurecopy (defn);
585 Ffset (fn_name, defn);
586 LOADHIST_ATTACH (fn_name);
587 return fn_name;
590 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
591 "Define NAME as a macro.\n\
592 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
593 When the macro is called, as in (NAME ARGS...),\n\
594 the function (lambda ARGLIST BODY...) is applied to\n\
595 the list ARGS... as it appears in the expression,\n\
596 and the result should be a form to be evaluated instead of the original.")
597 (args)
598 Lisp_Object args;
600 register Lisp_Object fn_name;
601 register Lisp_Object defn;
603 fn_name = Fcar (args);
604 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
605 if (!NILP (Vpurify_flag))
606 defn = Fpurecopy (defn);
607 Ffset (fn_name, defn);
608 LOADHIST_ATTACH (fn_name);
609 return fn_name;
612 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
613 "Define SYMBOL as a variable.\n\
614 You are not required to define a variable in order to use it,\n\
615 but the definition can supply documentation and an initial value\n\
616 in a way that tags can recognize.\n\n\
617 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
618 If SYMBOL is buffer-local, its default value is what is set;\n\
619 buffer-local values are not affected.\n\
620 INITVALUE and DOCSTRING are optional.\n\
621 If DOCSTRING starts with *, this variable is identified as a user option.\n\
622 This means that M-x set-variable and M-x edit-options recognize it.\n\
623 If INITVALUE is missing, SYMBOL's value is not set.")
624 (args)
625 Lisp_Object args;
627 register Lisp_Object sym, tem, tail;
629 sym = Fcar (args);
630 tail = Fcdr (args);
631 if (!NILP (Fcdr (Fcdr (tail))))
632 error ("too many arguments");
634 if (!NILP (tail))
636 tem = Fdefault_boundp (sym);
637 if (NILP (tem))
638 Fset_default (sym, Feval (Fcar (Fcdr (args))));
640 tail = Fcdr (Fcdr (args));
641 if (!NILP (Fcar (tail)))
643 tem = Fcar (tail);
644 if (!NILP (Vpurify_flag))
645 tem = Fpurecopy (tem);
646 Fput (sym, Qvariable_documentation, tem);
648 LOADHIST_ATTACH (sym);
649 return sym;
652 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
653 "Define SYMBOL as a constant variable.\n\
654 The intent is that neither programs nor users should ever change this value.\n\
655 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
656 If SYMBOL is buffer-local, its default value is what is set;\n\
657 buffer-local values are not affected.\n\
658 DOCSTRING is optional.")
659 (args)
660 Lisp_Object args;
662 register Lisp_Object sym, tem;
664 sym = Fcar (args);
665 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
666 error ("too many arguments");
668 tem = Feval (Fcar (Fcdr (args)));
669 if (!NILP (Vpurify_flag))
670 tem = Fpurecopy (tem);
671 Fset_default (sym, tem);
672 tem = Fcar (Fcdr (Fcdr (args)));
673 if (!NILP (tem))
675 if (!NILP (Vpurify_flag))
676 tem = Fpurecopy (tem);
677 Fput (sym, Qvariable_documentation, tem);
679 LOADHIST_ATTACH (sym);
680 return sym;
683 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
684 "Returns t if VARIABLE is intended to be set and modified by users.\n\
685 \(The alternative is a variable used internally in a Lisp program.)\n\
686 Determined by whether the first character of the documentation\n\
687 for the variable is `*' or if the variable is customizable (has a non-nil\n\
688 value of any of `custom-type', `custom-loads' or `standard-value'\n\
689 on its property list).")
690 (variable)
691 Lisp_Object variable;
693 Lisp_Object documentation;
695 if (!SYMBOLP (variable))
696 return Qnil;
698 documentation = Fget (variable, Qvariable_documentation);
699 if (INTEGERP (documentation) && XINT (documentation) < 0)
700 return Qt;
701 if (STRINGP (documentation)
702 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
703 return Qt;
704 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
705 if (CONSP (documentation)
706 && STRINGP (XCAR (documentation))
707 && INTEGERP (XCDR (documentation))
708 && XINT (XCDR (documentation)) < 0)
709 return Qt;
710 /* Customizable? */
711 if ((!NILP (Fget (variable, intern ("custom-type"))))
712 || (!NILP (Fget (variable, intern ("custom-loads"))))
713 || (!NILP (Fget (variable, intern ("standard-value")))))
714 return Qt;
715 return Qnil;
718 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
719 "Bind variables according to VARLIST then eval BODY.\n\
720 The value of the last form in BODY is returned.\n\
721 Each element of VARLIST is a symbol (which is bound to nil)\n\
722 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
723 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
724 (args)
725 Lisp_Object args;
727 Lisp_Object varlist, val, elt;
728 int count = specpdl_ptr - specpdl;
729 struct gcpro gcpro1, gcpro2, gcpro3;
731 GCPRO3 (args, elt, varlist);
733 varlist = Fcar (args);
734 while (!NILP (varlist))
736 QUIT;
737 elt = Fcar (varlist);
738 if (SYMBOLP (elt))
739 specbind (elt, Qnil);
740 else if (! NILP (Fcdr (Fcdr (elt))))
741 Fsignal (Qerror,
742 Fcons (build_string ("`let' bindings can have only one value-form"),
743 elt));
744 else
746 val = Feval (Fcar (Fcdr (elt)));
747 specbind (Fcar (elt), val);
749 varlist = Fcdr (varlist);
751 UNGCPRO;
752 val = Fprogn (Fcdr (args));
753 return unbind_to (count, val);
756 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
757 "Bind variables according to VARLIST then eval BODY.\n\
758 The value of the last form in BODY is returned.\n\
759 Each element of VARLIST is a symbol (which is bound to nil)\n\
760 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
761 All the VALUEFORMs are evalled before any symbols are bound.")
762 (args)
763 Lisp_Object args;
765 Lisp_Object *temps, tem;
766 register Lisp_Object elt, varlist;
767 int count = specpdl_ptr - specpdl;
768 register int argnum;
769 struct gcpro gcpro1, gcpro2;
771 varlist = Fcar (args);
773 /* Make space to hold the values to give the bound variables */
774 elt = Flength (varlist);
775 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
777 /* Compute the values and store them in `temps' */
779 GCPRO2 (args, *temps);
780 gcpro2.nvars = 0;
782 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
784 QUIT;
785 elt = Fcar (varlist);
786 if (SYMBOLP (elt))
787 temps [argnum++] = Qnil;
788 else if (! NILP (Fcdr (Fcdr (elt))))
789 Fsignal (Qerror,
790 Fcons (build_string ("`let' bindings can have only one value-form"),
791 elt));
792 else
793 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
794 gcpro2.nvars = argnum;
796 UNGCPRO;
798 varlist = Fcar (args);
799 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
801 elt = Fcar (varlist);
802 tem = temps[argnum++];
803 if (SYMBOLP (elt))
804 specbind (elt, tem);
805 else
806 specbind (Fcar (elt), tem);
809 elt = Fprogn (Fcdr (args));
810 return unbind_to (count, elt);
813 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
814 "If TEST yields non-nil, eval BODY... and repeat.\n\
815 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
816 until TEST returns nil.")
817 (args)
818 Lisp_Object args;
820 Lisp_Object test, body, tem;
821 struct gcpro gcpro1, gcpro2;
823 GCPRO2 (test, body);
825 test = Fcar (args);
826 body = Fcdr (args);
827 while (tem = Feval (test),
828 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
830 QUIT;
831 Fprogn (body);
834 UNGCPRO;
835 return Qnil;
838 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
839 "Return result of expanding macros at top level of FORM.\n\
840 If FORM is not a macro call, it is returned unchanged.\n\
841 Otherwise, the macro is expanded and the expansion is considered\n\
842 in place of FORM. When a non-macro-call results, it is returned.\n\n\
843 The second optional arg ENVIRONMENT species an environment of macro\n\
844 definitions to shadow the loaded ones for use in file byte-compilation.")
845 (form, environment)
846 Lisp_Object form;
847 Lisp_Object environment;
849 /* With cleanups from Hallvard Furuseth. */
850 register Lisp_Object expander, sym, def, tem;
852 while (1)
854 /* Come back here each time we expand a macro call,
855 in case it expands into another macro call. */
856 if (!CONSP (form))
857 break;
858 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
859 def = sym = XCAR (form);
860 tem = Qnil;
861 /* Trace symbols aliases to other symbols
862 until we get a symbol that is not an alias. */
863 while (SYMBOLP (def))
865 QUIT;
866 sym = def;
867 tem = Fassq (sym, environment);
868 if (NILP (tem))
870 def = XSYMBOL (sym)->function;
871 if (!EQ (def, Qunbound))
872 continue;
874 break;
876 /* Right now TEM is the result from SYM in ENVIRONMENT,
877 and if TEM is nil then DEF is SYM's function definition. */
878 if (NILP (tem))
880 /* SYM is not mentioned in ENVIRONMENT.
881 Look at its function definition. */
882 if (EQ (def, Qunbound) || !CONSP (def))
883 /* Not defined or definition not suitable */
884 break;
885 if (EQ (XCAR (def), Qautoload))
887 /* Autoloading function: will it be a macro when loaded? */
888 tem = Fnth (make_number (4), def);
889 if (EQ (tem, Qt) || EQ (tem, Qmacro))
890 /* Yes, load it and try again. */
892 struct gcpro gcpro1;
893 GCPRO1 (form);
894 do_autoload (def, sym);
895 UNGCPRO;
896 continue;
898 else
899 break;
901 else if (!EQ (XCAR (def), Qmacro))
902 break;
903 else expander = XCDR (def);
905 else
907 expander = XCDR (tem);
908 if (NILP (expander))
909 break;
911 form = apply1 (expander, XCDR (form));
913 return form;
916 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
917 "Eval BODY allowing nonlocal exits using `throw'.\n\
918 TAG is evalled to get the tag to use; it must not be nil.\n\
920 Then the BODY is executed.\n\
921 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
922 If no throw happens, `catch' returns the value of the last BODY form.\n\
923 If a throw happens, it specifies the value to return from `catch'.")
924 (args)
925 Lisp_Object args;
927 register Lisp_Object tag;
928 struct gcpro gcpro1;
930 GCPRO1 (args);
931 tag = Feval (Fcar (args));
932 UNGCPRO;
933 return internal_catch (tag, Fprogn, Fcdr (args));
936 /* Set up a catch, then call C function FUNC on argument ARG.
937 FUNC should return a Lisp_Object.
938 This is how catches are done from within C code. */
940 Lisp_Object
941 internal_catch (tag, func, arg)
942 Lisp_Object tag;
943 Lisp_Object (*func) ();
944 Lisp_Object arg;
946 /* This structure is made part of the chain `catchlist'. */
947 struct catchtag c;
949 /* Fill in the components of c, and put it on the list. */
950 c.next = catchlist;
951 c.tag = tag;
952 c.val = Qnil;
953 c.backlist = backtrace_list;
954 c.handlerlist = handlerlist;
955 c.lisp_eval_depth = lisp_eval_depth;
956 c.pdlcount = specpdl_ptr - specpdl;
957 c.poll_suppress_count = poll_suppress_count;
958 c.gcpro = gcprolist;
959 c.byte_stack = byte_stack_list;
960 catchlist = &c;
962 /* Call FUNC. */
963 if (! _setjmp (c.jmp))
964 c.val = (*func) (arg);
966 /* Throw works by a longjmp that comes right here. */
967 catchlist = c.next;
968 return c.val;
971 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
972 jump to that CATCH, returning VALUE as the value of that catch.
974 This is the guts Fthrow and Fsignal; they differ only in the way
975 they choose the catch tag to throw to. A catch tag for a
976 condition-case form has a TAG of Qnil.
978 Before each catch is discarded, unbind all special bindings and
979 execute all unwind-protect clauses made above that catch. Unwind
980 the handler stack as we go, so that the proper handlers are in
981 effect for each unwind-protect clause we run. At the end, restore
982 some static info saved in CATCH, and longjmp to the location
983 specified in the
985 This is used for correct unwinding in Fthrow and Fsignal. */
987 static void
988 unwind_to_catch (catch, value)
989 struct catchtag *catch;
990 Lisp_Object value;
992 register int last_time;
994 /* Save the value in the tag. */
995 catch->val = value;
997 /* Restore the polling-suppression count. */
998 set_poll_suppress_count (catch->poll_suppress_count);
1002 last_time = catchlist == catch;
1004 /* Unwind the specpdl stack, and then restore the proper set of
1005 handlers. */
1006 unbind_to (catchlist->pdlcount, Qnil);
1007 handlerlist = catchlist->handlerlist;
1008 catchlist = catchlist->next;
1010 while (! last_time);
1012 byte_stack_list = catch->byte_stack;
1013 gcprolist = catch->gcpro;
1014 #ifdef DEBUG_GCPRO
1015 if (gcprolist != 0)
1016 gcpro_level = gcprolist->level + 1;
1017 else
1018 gcpro_level = 0;
1019 #endif
1020 backtrace_list = catch->backlist;
1021 lisp_eval_depth = catch->lisp_eval_depth;
1023 _longjmp (catch->jmp, 1);
1026 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1027 "Throw to the catch for TAG and return VALUE from it.\n\
1028 Both TAG and VALUE are evalled.")
1029 (tag, value)
1030 register Lisp_Object tag, value;
1032 register struct catchtag *c;
1034 while (1)
1036 if (!NILP (tag))
1037 for (c = catchlist; c; c = c->next)
1039 if (EQ (c->tag, tag))
1040 unwind_to_catch (c, value);
1042 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1047 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1048 "Do BODYFORM, protecting with UNWINDFORMS.\n\
1049 If BODYFORM completes normally, its value is returned\n\
1050 after executing the UNWINDFORMS.\n\
1051 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1052 (args)
1053 Lisp_Object args;
1055 Lisp_Object val;
1056 int count = specpdl_ptr - specpdl;
1058 record_unwind_protect (0, Fcdr (args));
1059 val = Feval (Fcar (args));
1060 return unbind_to (count, val);
1063 /* Chain of condition handlers currently in effect.
1064 The elements of this chain are contained in the stack frames
1065 of Fcondition_case and internal_condition_case.
1066 When an error is signaled (by calling Fsignal, below),
1067 this chain is searched for an element that applies. */
1069 struct handler *handlerlist;
1071 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1072 "Regain control when an error is signaled.\n\
1073 executes BODYFORM and returns its value if no error happens.\n\
1074 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1075 where the BODY is made of Lisp expressions.\n\n\
1076 A handler is applicable to an error\n\
1077 if CONDITION-NAME is one of the error's condition names.\n\
1078 If an error happens, the first applicable handler is run.\n\
1080 The car of a handler may be a list of condition names\n\
1081 instead of a single condition name.\n\
1083 When a handler handles an error,\n\
1084 control returns to the condition-case and the handler BODY... is executed\n\
1085 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1086 VAR may be nil; then you do not get access to the signal information.\n\
1088 The value of the last BODY form is returned from the condition-case.\n\
1089 See also the function `signal' for more info.")
1090 (args)
1091 Lisp_Object args;
1093 Lisp_Object val;
1094 struct catchtag c;
1095 struct handler h;
1096 register Lisp_Object var, bodyform, handlers;
1098 var = Fcar (args);
1099 bodyform = Fcar (Fcdr (args));
1100 handlers = Fcdr (Fcdr (args));
1101 CHECK_SYMBOL (var, 0);
1103 for (val = handlers; ! NILP (val); val = Fcdr (val))
1105 Lisp_Object tem;
1106 tem = Fcar (val);
1107 if (! (NILP (tem)
1108 || (CONSP (tem)
1109 && (SYMBOLP (XCAR (tem))
1110 || CONSP (XCAR (tem))))))
1111 error ("Invalid condition handler", tem);
1114 c.tag = Qnil;
1115 c.val = Qnil;
1116 c.backlist = backtrace_list;
1117 c.handlerlist = handlerlist;
1118 c.lisp_eval_depth = lisp_eval_depth;
1119 c.pdlcount = specpdl_ptr - specpdl;
1120 c.poll_suppress_count = poll_suppress_count;
1121 c.gcpro = gcprolist;
1122 c.byte_stack = byte_stack_list;
1123 if (_setjmp (c.jmp))
1125 if (!NILP (h.var))
1126 specbind (h.var, c.val);
1127 val = Fprogn (Fcdr (h.chosen_clause));
1129 /* Note that this just undoes the binding of h.var; whoever
1130 longjumped to us unwound the stack to c.pdlcount before
1131 throwing. */
1132 unbind_to (c.pdlcount, Qnil);
1133 return val;
1135 c.next = catchlist;
1136 catchlist = &c;
1138 h.var = var;
1139 h.handler = handlers;
1140 h.next = handlerlist;
1141 h.tag = &c;
1142 handlerlist = &h;
1144 val = Feval (bodyform);
1145 catchlist = c.next;
1146 handlerlist = h.next;
1147 return val;
1150 /* Call the function BFUN with no arguments, catching errors within it
1151 according to HANDLERS. If there is an error, call HFUN with
1152 one argument which is the data that describes the error:
1153 (SIGNALNAME . DATA)
1155 HANDLERS can be a list of conditions to catch.
1156 If HANDLERS is Qt, catch all errors.
1157 If HANDLERS is Qerror, catch all errors
1158 but allow the debugger to run if that is enabled. */
1160 Lisp_Object
1161 internal_condition_case (bfun, handlers, hfun)
1162 Lisp_Object (*bfun) ();
1163 Lisp_Object handlers;
1164 Lisp_Object (*hfun) ();
1166 Lisp_Object val;
1167 struct catchtag c;
1168 struct handler h;
1170 /* Since Fsignal resets this to 0, it had better be 0 now
1171 or else we have a potential bug. */
1172 if (interrupt_input_blocked != 0)
1173 abort ();
1175 c.tag = Qnil;
1176 c.val = Qnil;
1177 c.backlist = backtrace_list;
1178 c.handlerlist = handlerlist;
1179 c.lisp_eval_depth = lisp_eval_depth;
1180 c.pdlcount = specpdl_ptr - specpdl;
1181 c.poll_suppress_count = poll_suppress_count;
1182 c.gcpro = gcprolist;
1183 c.byte_stack = byte_stack_list;
1184 if (_setjmp (c.jmp))
1186 return (*hfun) (c.val);
1188 c.next = catchlist;
1189 catchlist = &c;
1190 h.handler = handlers;
1191 h.var = Qnil;
1192 h.next = handlerlist;
1193 h.tag = &c;
1194 handlerlist = &h;
1196 val = (*bfun) ();
1197 catchlist = c.next;
1198 handlerlist = h.next;
1199 return val;
1202 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1204 Lisp_Object
1205 internal_condition_case_1 (bfun, arg, handlers, hfun)
1206 Lisp_Object (*bfun) ();
1207 Lisp_Object arg;
1208 Lisp_Object handlers;
1209 Lisp_Object (*hfun) ();
1211 Lisp_Object val;
1212 struct catchtag c;
1213 struct handler h;
1215 c.tag = Qnil;
1216 c.val = Qnil;
1217 c.backlist = backtrace_list;
1218 c.handlerlist = handlerlist;
1219 c.lisp_eval_depth = lisp_eval_depth;
1220 c.pdlcount = specpdl_ptr - specpdl;
1221 c.poll_suppress_count = poll_suppress_count;
1222 c.gcpro = gcprolist;
1223 c.byte_stack = byte_stack_list;
1224 if (_setjmp (c.jmp))
1226 return (*hfun) (c.val);
1228 c.next = catchlist;
1229 catchlist = &c;
1230 h.handler = handlers;
1231 h.var = Qnil;
1232 h.next = handlerlist;
1233 h.tag = &c;
1234 handlerlist = &h;
1236 val = (*bfun) (arg);
1237 catchlist = c.next;
1238 handlerlist = h.next;
1239 return val;
1242 static Lisp_Object find_handler_clause ();
1244 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1245 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1246 This function does not return.\n\n\
1247 An error symbol is a symbol with an `error-conditions' property\n\
1248 that is a list of condition names.\n\
1249 A handler for any of those names will get to handle this signal.\n\
1250 The symbol `error' should normally be one of them.\n\
1252 DATA should be a list. Its elements are printed as part of the error message.\n\
1253 If the signal is handled, DATA is made available to the handler.\n\
1254 See also the function `condition-case'.")
1255 (error_symbol, data)
1256 Lisp_Object error_symbol, data;
1258 /* When memory is full, ERROR-SYMBOL is nil,
1259 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1260 register struct handler *allhandlers = handlerlist;
1261 Lisp_Object conditions;
1262 extern int gc_in_progress;
1263 extern int waiting_for_input;
1264 Lisp_Object debugger_value;
1265 Lisp_Object string;
1266 Lisp_Object real_error_symbol;
1267 extern int display_busy_cursor_p;
1269 immediate_quit = 0;
1270 if (gc_in_progress || waiting_for_input)
1271 abort ();
1273 TOTALLY_UNBLOCK_INPUT;
1275 if (NILP (error_symbol))
1276 real_error_symbol = Fcar (data);
1277 else
1278 real_error_symbol = error_symbol;
1280 #ifdef HAVE_X_WINDOWS
1281 if (display_busy_cursor_p)
1282 cancel_busy_cursor ();
1283 #endif
1285 /* This hook is used by edebug. */
1286 if (! NILP (Vsignal_hook_function))
1287 call2 (Vsignal_hook_function, error_symbol, data);
1289 conditions = Fget (real_error_symbol, Qerror_conditions);
1291 for (; handlerlist; handlerlist = handlerlist->next)
1293 register Lisp_Object clause;
1294 clause = find_handler_clause (handlerlist->handler, conditions,
1295 error_symbol, data, &debugger_value);
1297 #if 0 /* Most callers are not prepared to handle gc if this returns.
1298 So, since this feature is not very useful, take it out. */
1299 /* If have called debugger and user wants to continue,
1300 just return nil. */
1301 if (EQ (clause, Qlambda))
1302 return debugger_value;
1303 #else
1304 if (EQ (clause, Qlambda))
1306 /* We can't return values to code which signaled an error, but we
1307 can continue code which has signaled a quit. */
1308 if (EQ (real_error_symbol, Qquit))
1309 return Qnil;
1310 else
1311 error ("Cannot return from the debugger in an error");
1313 #endif
1315 if (!NILP (clause))
1317 Lisp_Object unwind_data;
1318 struct handler *h = handlerlist;
1320 handlerlist = allhandlers;
1322 if (NILP (error_symbol))
1323 unwind_data = data;
1324 else
1325 unwind_data = Fcons (error_symbol, data);
1326 h->chosen_clause = clause;
1327 unwind_to_catch (h->tag, unwind_data);
1331 handlerlist = allhandlers;
1332 /* If no handler is present now, try to run the debugger,
1333 and if that fails, throw to top level. */
1334 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1335 if (catchlist != 0)
1336 Fthrow (Qtop_level, Qt);
1338 if (! NILP (error_symbol))
1339 data = Fcons (error_symbol, data);
1341 string = Ferror_message_string (data);
1342 fatal ("%s", XSTRING (string)->data, 0);
1345 /* Return nonzero iff LIST is a non-nil atom or
1346 a list containing one of CONDITIONS. */
1348 static int
1349 wants_debugger (list, conditions)
1350 Lisp_Object list, conditions;
1352 if (NILP (list))
1353 return 0;
1354 if (! CONSP (list))
1355 return 1;
1357 while (CONSP (conditions))
1359 Lisp_Object this, tail;
1360 this = XCAR (conditions);
1361 for (tail = list; CONSP (tail); tail = XCDR (tail))
1362 if (EQ (XCAR (tail), this))
1363 return 1;
1364 conditions = XCDR (conditions);
1366 return 0;
1369 /* Return 1 if an error with condition-symbols CONDITIONS,
1370 and described by SIGNAL-DATA, should skip the debugger
1371 according to debugger-ignore-errors. */
1373 static int
1374 skip_debugger (conditions, data)
1375 Lisp_Object conditions, data;
1377 Lisp_Object tail;
1378 int first_string = 1;
1379 Lisp_Object error_message;
1381 for (tail = Vdebug_ignored_errors; CONSP (tail);
1382 tail = XCDR (tail))
1384 if (STRINGP (XCAR (tail)))
1386 if (first_string)
1388 error_message = Ferror_message_string (data);
1389 first_string = 0;
1391 if (fast_string_match (XCAR (tail), error_message) >= 0)
1392 return 1;
1394 else
1396 Lisp_Object contail;
1398 for (contail = conditions; CONSP (contail);
1399 contail = XCDR (contail))
1400 if (EQ (XCAR (tail), XCAR (contail)))
1401 return 1;
1405 return 0;
1408 /* Value of Qlambda means we have called debugger and user has continued.
1409 There are two ways to pass SIG and DATA:
1410 = SIG is the error symbol, and DATA is the rest of the data.
1411 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1412 This is for memory-full errors only.
1414 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1416 static Lisp_Object
1417 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1418 Lisp_Object handlers, conditions, sig, data;
1419 Lisp_Object *debugger_value_ptr;
1421 register Lisp_Object h;
1422 register Lisp_Object tem;
1424 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1425 return Qt;
1426 /* error is used similarly, but means print an error message
1427 and run the debugger if that is enabled. */
1428 if (EQ (handlers, Qerror)
1429 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1430 there is a handler. */
1432 int count = specpdl_ptr - specpdl;
1433 int debugger_called = 0;
1434 Lisp_Object sig_symbol, combined_data;
1435 /* This is set to 1 if we are handling a memory-full error,
1436 because these must not run the debugger.
1437 (There is no room in memory to do that!) */
1438 int no_debugger = 0;
1440 if (NILP (sig))
1442 combined_data = data;
1443 sig_symbol = Fcar (data);
1444 no_debugger = 1;
1446 else
1448 combined_data = Fcons (sig, data);
1449 sig_symbol = sig;
1452 if (wants_debugger (Vstack_trace_on_error, conditions))
1454 #ifdef PROTOTYPES
1455 internal_with_output_to_temp_buffer ("*Backtrace*",
1456 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1457 Qnil);
1458 #else
1459 internal_with_output_to_temp_buffer ("*Backtrace*",
1460 Fbacktrace, Qnil);
1461 #endif
1463 if (! no_debugger
1464 && (EQ (sig_symbol, Qquit)
1465 ? debug_on_quit
1466 : wants_debugger (Vdebug_on_error, conditions))
1467 && ! skip_debugger (conditions, combined_data)
1468 && when_entered_debugger < num_nonmacro_input_events)
1470 specbind (Qdebug_on_error, Qnil);
1471 *debugger_value_ptr
1472 = call_debugger (Fcons (Qerror,
1473 Fcons (combined_data, Qnil)));
1474 debugger_called = 1;
1476 /* If there is no handler, return saying whether we ran the debugger. */
1477 if (EQ (handlers, Qerror))
1479 if (debugger_called)
1480 return unbind_to (count, Qlambda);
1481 return Qt;
1484 for (h = handlers; CONSP (h); h = Fcdr (h))
1486 Lisp_Object handler, condit;
1488 handler = Fcar (h);
1489 if (!CONSP (handler))
1490 continue;
1491 condit = Fcar (handler);
1492 /* Handle a single condition name in handler HANDLER. */
1493 if (SYMBOLP (condit))
1495 tem = Fmemq (Fcar (handler), conditions);
1496 if (!NILP (tem))
1497 return handler;
1499 /* Handle a list of condition names in handler HANDLER. */
1500 else if (CONSP (condit))
1502 while (CONSP (condit))
1504 tem = Fmemq (Fcar (condit), conditions);
1505 if (!NILP (tem))
1506 return handler;
1507 condit = XCDR (condit);
1511 return Qnil;
1514 /* dump an error message; called like printf */
1516 /* VARARGS 1 */
1517 void
1518 error (m, a1, a2, a3)
1519 char *m;
1520 char *a1, *a2, *a3;
1522 char buf[200];
1523 int size = 200;
1524 int mlen;
1525 char *buffer = buf;
1526 char *args[3];
1527 int allocated = 0;
1528 Lisp_Object string;
1530 args[0] = a1;
1531 args[1] = a2;
1532 args[2] = a3;
1534 mlen = strlen (m);
1536 while (1)
1538 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1539 if (used < size)
1540 break;
1541 size *= 2;
1542 if (allocated)
1543 buffer = (char *) xrealloc (buffer, size);
1544 else
1546 buffer = (char *) xmalloc (size);
1547 allocated = 1;
1551 string = build_string (buffer);
1552 if (allocated)
1553 free (buffer);
1555 Fsignal (Qerror, Fcons (string, Qnil));
1558 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1559 "T if FUNCTION makes provisions for interactive calling.\n\
1560 This means it contains a description for how to read arguments to give it.\n\
1561 The value is nil for an invalid function or a symbol with no function\n\
1562 definition.\n\
1564 Interactively callable functions include strings and vectors (treated\n\
1565 as keyboard macros), lambda-expressions that contain a top-level call\n\
1566 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1567 fourth argument, and some of the built-in functions of Lisp.\n\
1569 Also, a symbol satisfies `commandp' if its function definition does so.")
1570 (function)
1571 Lisp_Object function;
1573 register Lisp_Object fun;
1574 register Lisp_Object funcar;
1576 fun = function;
1578 fun = indirect_function (fun);
1579 if (EQ (fun, Qunbound))
1580 return Qnil;
1582 /* Emacs primitives are interactive if their DEFUN specifies an
1583 interactive spec. */
1584 if (SUBRP (fun))
1586 if (XSUBR (fun)->prompt)
1587 return Qt;
1588 else
1589 return Qnil;
1592 /* Bytecode objects are interactive if they are long enough to
1593 have an element whose index is COMPILED_INTERACTIVE, which is
1594 where the interactive spec is stored. */
1595 else if (COMPILEDP (fun))
1596 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1597 ? Qt : Qnil);
1599 /* Strings and vectors are keyboard macros. */
1600 if (STRINGP (fun) || VECTORP (fun))
1601 return Qt;
1603 /* Lists may represent commands. */
1604 if (!CONSP (fun))
1605 return Qnil;
1606 funcar = Fcar (fun);
1607 if (!SYMBOLP (funcar))
1608 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1609 if (EQ (funcar, Qlambda))
1610 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1611 if (EQ (funcar, Qmocklisp))
1612 return Qt; /* All mocklisp functions can be called interactively */
1613 if (EQ (funcar, Qautoload))
1614 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1615 else
1616 return Qnil;
1619 /* ARGSUSED */
1620 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1621 "Define FUNCTION to autoload from FILE.\n\
1622 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1623 Third arg DOCSTRING is documentation for the function.\n\
1624 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1625 Fifth arg TYPE indicates the type of the object:\n\
1626 nil or omitted says FUNCTION is a function,\n\
1627 `keymap' says FUNCTION is really a keymap, and\n\
1628 `macro' or t says FUNCTION is really a macro.\n\
1629 Third through fifth args give info about the real definition.\n\
1630 They default to nil.\n\
1631 If FUNCTION is already defined other than as an autoload,\n\
1632 this does nothing and returns nil.")
1633 (function, file, docstring, interactive, type)
1634 Lisp_Object function, file, docstring, interactive, type;
1636 #ifdef NO_ARG_ARRAY
1637 Lisp_Object args[4];
1638 #endif
1640 CHECK_SYMBOL (function, 0);
1641 CHECK_STRING (file, 1);
1643 /* If function is defined and not as an autoload, don't override */
1644 if (!EQ (XSYMBOL (function)->function, Qunbound)
1645 && !(CONSP (XSYMBOL (function)->function)
1646 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1647 return Qnil;
1649 #ifdef NO_ARG_ARRAY
1650 args[0] = file;
1651 args[1] = docstring;
1652 args[2] = interactive;
1653 args[3] = type;
1655 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1656 #else /* NO_ARG_ARRAY */
1657 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1658 #endif /* not NO_ARG_ARRAY */
1661 Lisp_Object
1662 un_autoload (oldqueue)
1663 Lisp_Object oldqueue;
1665 register Lisp_Object queue, first, second;
1667 /* Queue to unwind is current value of Vautoload_queue.
1668 oldqueue is the shadowed value to leave in Vautoload_queue. */
1669 queue = Vautoload_queue;
1670 Vautoload_queue = oldqueue;
1671 while (CONSP (queue))
1673 first = Fcar (queue);
1674 second = Fcdr (first);
1675 first = Fcar (first);
1676 if (EQ (second, Qnil))
1677 Vfeatures = first;
1678 else
1679 Ffset (first, second);
1680 queue = Fcdr (queue);
1682 return Qnil;
1685 /* Load an autoloaded function.
1686 FUNNAME is the symbol which is the function's name.
1687 FUNDEF is the autoload definition (a list). */
1689 void
1690 do_autoload (fundef, funname)
1691 Lisp_Object fundef, funname;
1693 int count = specpdl_ptr - specpdl;
1694 Lisp_Object fun, queue, first, second;
1695 struct gcpro gcpro1, gcpro2, gcpro3;
1697 fun = funname;
1698 CHECK_SYMBOL (funname, 0);
1699 GCPRO3 (fun, funname, fundef);
1701 /* Preserve the match data. */
1702 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1704 /* Value saved here is to be restored into Vautoload_queue. */
1705 record_unwind_protect (un_autoload, Vautoload_queue);
1706 Vautoload_queue = Qt;
1707 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1709 /* Save the old autoloads, in case we ever do an unload. */
1710 queue = Vautoload_queue;
1711 while (CONSP (queue))
1713 first = Fcar (queue);
1714 second = Fcdr (first);
1715 first = Fcar (first);
1717 /* Note: This test is subtle. The cdr of an autoload-queue entry
1718 may be an atom if the autoload entry was generated by a defalias
1719 or fset. */
1720 if (CONSP (second))
1721 Fput (first, Qautoload, (Fcdr (second)));
1723 queue = Fcdr (queue);
1726 /* Once loading finishes, don't undo it. */
1727 Vautoload_queue = Qt;
1728 unbind_to (count, Qnil);
1730 fun = Findirect_function (fun);
1732 if (!NILP (Fequal (fun, fundef)))
1733 error ("Autoloading failed to define function %s",
1734 XSYMBOL (funname)->name->data);
1735 UNGCPRO;
1738 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1739 "Evaluate FORM and return its value.")
1740 (form)
1741 Lisp_Object form;
1743 Lisp_Object fun, val, original_fun, original_args;
1744 Lisp_Object funcar;
1745 struct backtrace backtrace;
1746 struct gcpro gcpro1, gcpro2, gcpro3;
1748 /* Since Fsignal resets this to 0, it had better be 0 now
1749 or else we have a potential bug. */
1750 if (interrupt_input_blocked != 0)
1751 abort ();
1753 if (SYMBOLP (form))
1755 if (EQ (Vmocklisp_arguments, Qt))
1756 return Fsymbol_value (form);
1757 val = Fsymbol_value (form);
1758 if (NILP (val))
1759 XSETFASTINT (val, 0);
1760 else if (EQ (val, Qt))
1761 XSETFASTINT (val, 1);
1762 return val;
1764 if (!CONSP (form))
1765 return form;
1767 QUIT;
1768 if (consing_since_gc > gc_cons_threshold)
1770 GCPRO1 (form);
1771 Fgarbage_collect ();
1772 UNGCPRO;
1775 if (++lisp_eval_depth > max_lisp_eval_depth)
1777 if (max_lisp_eval_depth < 100)
1778 max_lisp_eval_depth = 100;
1779 if (lisp_eval_depth > max_lisp_eval_depth)
1780 error ("Lisp nesting exceeds max-lisp-eval-depth");
1783 original_fun = Fcar (form);
1784 original_args = Fcdr (form);
1786 backtrace.next = backtrace_list;
1787 backtrace_list = &backtrace;
1788 backtrace.function = &original_fun; /* This also protects them from gc */
1789 backtrace.args = &original_args;
1790 backtrace.nargs = UNEVALLED;
1791 backtrace.evalargs = 1;
1792 backtrace.debug_on_exit = 0;
1794 if (debug_on_next_call)
1795 do_debug_on_call (Qt);
1797 /* At this point, only original_fun and original_args
1798 have values that will be used below */
1799 retry:
1800 fun = Findirect_function (original_fun);
1802 if (SUBRP (fun))
1804 Lisp_Object numargs;
1805 Lisp_Object argvals[8];
1806 Lisp_Object args_left;
1807 register int i, maxargs;
1809 args_left = original_args;
1810 numargs = Flength (args_left);
1812 if (XINT (numargs) < XSUBR (fun)->min_args ||
1813 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1814 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1816 if (XSUBR (fun)->max_args == UNEVALLED)
1818 backtrace.evalargs = 0;
1819 val = (*XSUBR (fun)->function) (args_left);
1820 goto done;
1823 if (XSUBR (fun)->max_args == MANY)
1825 /* Pass a vector of evaluated arguments */
1826 Lisp_Object *vals;
1827 register int argnum = 0;
1829 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1831 GCPRO3 (args_left, fun, fun);
1832 gcpro3.var = vals;
1833 gcpro3.nvars = 0;
1835 while (!NILP (args_left))
1837 vals[argnum++] = Feval (Fcar (args_left));
1838 args_left = Fcdr (args_left);
1839 gcpro3.nvars = argnum;
1842 backtrace.args = vals;
1843 backtrace.nargs = XINT (numargs);
1845 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1846 UNGCPRO;
1847 goto done;
1850 GCPRO3 (args_left, fun, fun);
1851 gcpro3.var = argvals;
1852 gcpro3.nvars = 0;
1854 maxargs = XSUBR (fun)->max_args;
1855 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1857 argvals[i] = Feval (Fcar (args_left));
1858 gcpro3.nvars = ++i;
1861 UNGCPRO;
1863 backtrace.args = argvals;
1864 backtrace.nargs = XINT (numargs);
1866 switch (i)
1868 case 0:
1869 val = (*XSUBR (fun)->function) ();
1870 goto done;
1871 case 1:
1872 val = (*XSUBR (fun)->function) (argvals[0]);
1873 goto done;
1874 case 2:
1875 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1876 goto done;
1877 case 3:
1878 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1879 argvals[2]);
1880 goto done;
1881 case 4:
1882 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1883 argvals[2], argvals[3]);
1884 goto done;
1885 case 5:
1886 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1887 argvals[3], argvals[4]);
1888 goto done;
1889 case 6:
1890 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1891 argvals[3], argvals[4], argvals[5]);
1892 goto done;
1893 case 7:
1894 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1895 argvals[3], argvals[4], argvals[5],
1896 argvals[6]);
1897 goto done;
1899 case 8:
1900 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1901 argvals[3], argvals[4], argvals[5],
1902 argvals[6], argvals[7]);
1903 goto done;
1905 default:
1906 /* Someone has created a subr that takes more arguments than
1907 is supported by this code. We need to either rewrite the
1908 subr to use a different argument protocol, or add more
1909 cases to this switch. */
1910 abort ();
1913 if (COMPILEDP (fun))
1914 val = apply_lambda (fun, original_args, 1);
1915 else
1917 if (!CONSP (fun))
1918 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1919 funcar = Fcar (fun);
1920 if (!SYMBOLP (funcar))
1921 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1922 if (EQ (funcar, Qautoload))
1924 do_autoload (fun, original_fun);
1925 goto retry;
1927 if (EQ (funcar, Qmacro))
1928 val = Feval (apply1 (Fcdr (fun), original_args));
1929 else if (EQ (funcar, Qlambda))
1930 val = apply_lambda (fun, original_args, 1);
1931 else if (EQ (funcar, Qmocklisp))
1932 val = ml_apply (fun, original_args);
1933 else
1934 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1936 done:
1937 if (!EQ (Vmocklisp_arguments, Qt))
1939 if (NILP (val))
1940 XSETFASTINT (val, 0);
1941 else if (EQ (val, Qt))
1942 XSETFASTINT (val, 1);
1944 lisp_eval_depth--;
1945 if (backtrace.debug_on_exit)
1946 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1947 backtrace_list = backtrace.next;
1948 return val;
1951 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1952 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1953 Then return the value FUNCTION returns.\n\
1954 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1955 (nargs, args)
1956 int nargs;
1957 Lisp_Object *args;
1959 register int i, numargs;
1960 register Lisp_Object spread_arg;
1961 register Lisp_Object *funcall_args;
1962 Lisp_Object fun;
1963 struct gcpro gcpro1;
1965 fun = args [0];
1966 funcall_args = 0;
1967 spread_arg = args [nargs - 1];
1968 CHECK_LIST (spread_arg, nargs);
1970 numargs = XINT (Flength (spread_arg));
1972 if (numargs == 0)
1973 return Ffuncall (nargs - 1, args);
1974 else if (numargs == 1)
1976 args [nargs - 1] = XCAR (spread_arg);
1977 return Ffuncall (nargs, args);
1980 numargs += nargs - 2;
1982 fun = indirect_function (fun);
1983 if (EQ (fun, Qunbound))
1985 /* Let funcall get the error */
1986 fun = args[0];
1987 goto funcall;
1990 if (SUBRP (fun))
1992 if (numargs < XSUBR (fun)->min_args
1993 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1994 goto funcall; /* Let funcall get the error */
1995 else if (XSUBR (fun)->max_args > numargs)
1997 /* Avoid making funcall cons up a yet another new vector of arguments
1998 by explicitly supplying nil's for optional values */
1999 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2000 * sizeof (Lisp_Object));
2001 for (i = numargs; i < XSUBR (fun)->max_args;)
2002 funcall_args[++i] = Qnil;
2003 GCPRO1 (*funcall_args);
2004 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2007 funcall:
2008 /* We add 1 to numargs because funcall_args includes the
2009 function itself as well as its arguments. */
2010 if (!funcall_args)
2012 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2013 * sizeof (Lisp_Object));
2014 GCPRO1 (*funcall_args);
2015 gcpro1.nvars = 1 + numargs;
2018 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2019 /* Spread the last arg we got. Its first element goes in
2020 the slot that it used to occupy, hence this value of I. */
2021 i = nargs - 1;
2022 while (!NILP (spread_arg))
2024 funcall_args [i++] = XCAR (spread_arg);
2025 spread_arg = XCDR (spread_arg);
2028 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2031 /* Run hook variables in various ways. */
2033 enum run_hooks_condition {to_completion, until_success, until_failure};
2035 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 1, MANY, 0,
2036 "Run each hook in HOOKS. Major mode functions use this.\n\
2037 Each argument should be a symbol, a hook variable.\n\
2038 These symbols are processed in the order specified.\n\
2039 If a hook symbol has a non-nil value, that value may be a function\n\
2040 or a list of functions to be called to run the hook.\n\
2041 If the value is a function, it is called with no arguments.\n\
2042 If it is a list, the elements are called, in order, with no arguments.\n\
2044 To make a hook variable buffer-local, use `make-local-hook',\n\
2045 not `make-local-variable'.")
2046 (nargs, args)
2047 int nargs;
2048 Lisp_Object *args;
2050 Lisp_Object hook[1];
2051 register int i;
2053 for (i = 0; i < nargs; i++)
2055 hook[0] = args[i];
2056 run_hook_with_args (1, hook, to_completion);
2059 return Qnil;
2062 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2063 Srun_hook_with_args, 1, MANY, 0,
2064 "Run HOOK with the specified arguments ARGS.\n\
2065 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2066 value, that value may be a function or a list of functions to be\n\
2067 called to run the hook. If the value is a function, it is called with\n\
2068 the given arguments and its return value is returned. If it is a list\n\
2069 of functions, those functions are called, in order,\n\
2070 with the given arguments ARGS.\n\
2071 It is best not to depend on the value return by `run-hook-with-args',\n\
2072 as that may change.\n\
2074 To make a hook variable buffer-local, use `make-local-hook',\n\
2075 not `make-local-variable'.")
2076 (nargs, args)
2077 int nargs;
2078 Lisp_Object *args;
2080 return run_hook_with_args (nargs, args, to_completion);
2083 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2084 Srun_hook_with_args_until_success, 1, MANY, 0,
2085 "Run HOOK with the specified arguments ARGS.\n\
2086 HOOK should be a symbol, a hook variable. Its value should\n\
2087 be a list of functions. We call those functions, one by one,\n\
2088 passing arguments ARGS to each of them, until one of them\n\
2089 returns a non-nil value. Then we return that value.\n\
2090 If all the functions return nil, we return nil.\n\
2092 To make a hook variable buffer-local, use `make-local-hook',\n\
2093 not `make-local-variable'.")
2094 (nargs, args)
2095 int nargs;
2096 Lisp_Object *args;
2098 return run_hook_with_args (nargs, args, until_success);
2101 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2102 Srun_hook_with_args_until_failure, 1, MANY, 0,
2103 "Run HOOK with the specified arguments ARGS.\n\
2104 HOOK should be a symbol, a hook variable. Its value should\n\
2105 be a list of functions. We call those functions, one by one,\n\
2106 passing arguments ARGS to each of them, until one of them\n\
2107 returns nil. Then we return nil.\n\
2108 If all the functions return non-nil, we return non-nil.\n\
2110 To make a hook variable buffer-local, use `make-local-hook',\n\
2111 not `make-local-variable'.")
2112 (nargs, args)
2113 int nargs;
2114 Lisp_Object *args;
2116 return run_hook_with_args (nargs, args, until_failure);
2119 /* ARGS[0] should be a hook symbol.
2120 Call each of the functions in the hook value, passing each of them
2121 as arguments all the rest of ARGS (all NARGS - 1 elements).
2122 COND specifies a condition to test after each call
2123 to decide whether to stop.
2124 The caller (or its caller, etc) must gcpro all of ARGS,
2125 except that it isn't necessary to gcpro ARGS[0]. */
2127 Lisp_Object
2128 run_hook_with_args (nargs, args, cond)
2129 int nargs;
2130 Lisp_Object *args;
2131 enum run_hooks_condition cond;
2133 Lisp_Object sym, val, ret;
2134 Lisp_Object globals;
2135 struct gcpro gcpro1, gcpro2, gcpro3;
2137 /* If we are dying or still initializing,
2138 don't do anything--it would probably crash if we tried. */
2139 if (NILP (Vrun_hooks))
2140 return Qnil;
2142 sym = args[0];
2143 val = find_symbol_value (sym);
2144 ret = (cond == until_failure ? Qt : Qnil);
2146 if (EQ (val, Qunbound) || NILP (val))
2147 return ret;
2148 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2150 args[0] = val;
2151 return Ffuncall (nargs, args);
2153 else
2155 globals = Qnil;
2156 GCPRO3 (sym, val, globals);
2158 for (;
2159 CONSP (val) && ((cond == to_completion)
2160 || (cond == until_success ? NILP (ret)
2161 : !NILP (ret)));
2162 val = XCDR (val))
2164 if (EQ (XCAR (val), Qt))
2166 /* t indicates this hook has a local binding;
2167 it means to run the global binding too. */
2169 for (globals = Fdefault_value (sym);
2170 CONSP (globals) && ((cond == to_completion)
2171 || (cond == until_success ? NILP (ret)
2172 : !NILP (ret)));
2173 globals = XCDR (globals))
2175 args[0] = XCAR (globals);
2176 /* In a global value, t should not occur. If it does, we
2177 must ignore it to avoid an endless loop. */
2178 if (!EQ (args[0], Qt))
2179 ret = Ffuncall (nargs, args);
2182 else
2184 args[0] = XCAR (val);
2185 ret = Ffuncall (nargs, args);
2189 UNGCPRO;
2190 return ret;
2194 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2195 present value of that symbol.
2196 Call each element of FUNLIST,
2197 passing each of them the rest of ARGS.
2198 The caller (or its caller, etc) must gcpro all of ARGS,
2199 except that it isn't necessary to gcpro ARGS[0]. */
2201 Lisp_Object
2202 run_hook_list_with_args (funlist, nargs, args)
2203 Lisp_Object funlist;
2204 int nargs;
2205 Lisp_Object *args;
2207 Lisp_Object sym;
2208 Lisp_Object val;
2209 Lisp_Object globals;
2210 struct gcpro gcpro1, gcpro2, gcpro3;
2212 sym = args[0];
2213 globals = Qnil;
2214 GCPRO3 (sym, val, globals);
2216 for (val = funlist; CONSP (val); val = XCDR (val))
2218 if (EQ (XCAR (val), Qt))
2220 /* t indicates this hook has a local binding;
2221 it means to run the global binding too. */
2223 for (globals = Fdefault_value (sym);
2224 CONSP (globals);
2225 globals = XCDR (globals))
2227 args[0] = XCAR (globals);
2228 /* In a global value, t should not occur. If it does, we
2229 must ignore it to avoid an endless loop. */
2230 if (!EQ (args[0], Qt))
2231 Ffuncall (nargs, args);
2234 else
2236 args[0] = XCAR (val);
2237 Ffuncall (nargs, args);
2240 UNGCPRO;
2241 return Qnil;
2244 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2246 void
2247 run_hook_with_args_2 (hook, arg1, arg2)
2248 Lisp_Object hook, arg1, arg2;
2250 Lisp_Object temp[3];
2251 temp[0] = hook;
2252 temp[1] = arg1;
2253 temp[2] = arg2;
2255 Frun_hook_with_args (3, temp);
2258 /* Apply fn to arg */
2259 Lisp_Object
2260 apply1 (fn, arg)
2261 Lisp_Object fn, arg;
2263 struct gcpro gcpro1;
2265 GCPRO1 (fn);
2266 if (NILP (arg))
2267 RETURN_UNGCPRO (Ffuncall (1, &fn));
2268 gcpro1.nvars = 2;
2269 #ifdef NO_ARG_ARRAY
2271 Lisp_Object args[2];
2272 args[0] = fn;
2273 args[1] = arg;
2274 gcpro1.var = args;
2275 RETURN_UNGCPRO (Fapply (2, args));
2277 #else /* not NO_ARG_ARRAY */
2278 RETURN_UNGCPRO (Fapply (2, &fn));
2279 #endif /* not NO_ARG_ARRAY */
2282 /* Call function fn on no arguments */
2283 Lisp_Object
2284 call0 (fn)
2285 Lisp_Object fn;
2287 struct gcpro gcpro1;
2289 GCPRO1 (fn);
2290 RETURN_UNGCPRO (Ffuncall (1, &fn));
2293 /* Call function fn with 1 argument arg1 */
2294 /* ARGSUSED */
2295 Lisp_Object
2296 call1 (fn, arg1)
2297 Lisp_Object fn, arg1;
2299 struct gcpro gcpro1;
2300 #ifdef NO_ARG_ARRAY
2301 Lisp_Object args[2];
2303 args[0] = fn;
2304 args[1] = arg1;
2305 GCPRO1 (args[0]);
2306 gcpro1.nvars = 2;
2307 RETURN_UNGCPRO (Ffuncall (2, args));
2308 #else /* not NO_ARG_ARRAY */
2309 GCPRO1 (fn);
2310 gcpro1.nvars = 2;
2311 RETURN_UNGCPRO (Ffuncall (2, &fn));
2312 #endif /* not NO_ARG_ARRAY */
2315 /* Call function fn with 2 arguments arg1, arg2 */
2316 /* ARGSUSED */
2317 Lisp_Object
2318 call2 (fn, arg1, arg2)
2319 Lisp_Object fn, arg1, arg2;
2321 struct gcpro gcpro1;
2322 #ifdef NO_ARG_ARRAY
2323 Lisp_Object args[3];
2324 args[0] = fn;
2325 args[1] = arg1;
2326 args[2] = arg2;
2327 GCPRO1 (args[0]);
2328 gcpro1.nvars = 3;
2329 RETURN_UNGCPRO (Ffuncall (3, args));
2330 #else /* not NO_ARG_ARRAY */
2331 GCPRO1 (fn);
2332 gcpro1.nvars = 3;
2333 RETURN_UNGCPRO (Ffuncall (3, &fn));
2334 #endif /* not NO_ARG_ARRAY */
2337 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2338 /* ARGSUSED */
2339 Lisp_Object
2340 call3 (fn, arg1, arg2, arg3)
2341 Lisp_Object fn, arg1, arg2, arg3;
2343 struct gcpro gcpro1;
2344 #ifdef NO_ARG_ARRAY
2345 Lisp_Object args[4];
2346 args[0] = fn;
2347 args[1] = arg1;
2348 args[2] = arg2;
2349 args[3] = arg3;
2350 GCPRO1 (args[0]);
2351 gcpro1.nvars = 4;
2352 RETURN_UNGCPRO (Ffuncall (4, args));
2353 #else /* not NO_ARG_ARRAY */
2354 GCPRO1 (fn);
2355 gcpro1.nvars = 4;
2356 RETURN_UNGCPRO (Ffuncall (4, &fn));
2357 #endif /* not NO_ARG_ARRAY */
2360 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2361 /* ARGSUSED */
2362 Lisp_Object
2363 call4 (fn, arg1, arg2, arg3, arg4)
2364 Lisp_Object fn, arg1, arg2, arg3, arg4;
2366 struct gcpro gcpro1;
2367 #ifdef NO_ARG_ARRAY
2368 Lisp_Object args[5];
2369 args[0] = fn;
2370 args[1] = arg1;
2371 args[2] = arg2;
2372 args[3] = arg3;
2373 args[4] = arg4;
2374 GCPRO1 (args[0]);
2375 gcpro1.nvars = 5;
2376 RETURN_UNGCPRO (Ffuncall (5, args));
2377 #else /* not NO_ARG_ARRAY */
2378 GCPRO1 (fn);
2379 gcpro1.nvars = 5;
2380 RETURN_UNGCPRO (Ffuncall (5, &fn));
2381 #endif /* not NO_ARG_ARRAY */
2384 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2385 /* ARGSUSED */
2386 Lisp_Object
2387 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2388 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2390 struct gcpro gcpro1;
2391 #ifdef NO_ARG_ARRAY
2392 Lisp_Object args[6];
2393 args[0] = fn;
2394 args[1] = arg1;
2395 args[2] = arg2;
2396 args[3] = arg3;
2397 args[4] = arg4;
2398 args[5] = arg5;
2399 GCPRO1 (args[0]);
2400 gcpro1.nvars = 6;
2401 RETURN_UNGCPRO (Ffuncall (6, args));
2402 #else /* not NO_ARG_ARRAY */
2403 GCPRO1 (fn);
2404 gcpro1.nvars = 6;
2405 RETURN_UNGCPRO (Ffuncall (6, &fn));
2406 #endif /* not NO_ARG_ARRAY */
2409 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2410 /* ARGSUSED */
2411 Lisp_Object
2412 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2413 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2415 struct gcpro gcpro1;
2416 #ifdef NO_ARG_ARRAY
2417 Lisp_Object args[7];
2418 args[0] = fn;
2419 args[1] = arg1;
2420 args[2] = arg2;
2421 args[3] = arg3;
2422 args[4] = arg4;
2423 args[5] = arg5;
2424 args[6] = arg6;
2425 GCPRO1 (args[0]);
2426 gcpro1.nvars = 7;
2427 RETURN_UNGCPRO (Ffuncall (7, args));
2428 #else /* not NO_ARG_ARRAY */
2429 GCPRO1 (fn);
2430 gcpro1.nvars = 7;
2431 RETURN_UNGCPRO (Ffuncall (7, &fn));
2432 #endif /* not NO_ARG_ARRAY */
2435 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2436 "Call first argument as a function, passing remaining arguments to it.\n\
2437 Return the value that function returns.\n\
2438 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2439 (nargs, args)
2440 int nargs;
2441 Lisp_Object *args;
2443 Lisp_Object fun;
2444 Lisp_Object funcar;
2445 int numargs = nargs - 1;
2446 Lisp_Object lisp_numargs;
2447 Lisp_Object val;
2448 struct backtrace backtrace;
2449 register Lisp_Object *internal_args;
2450 register int i;
2452 QUIT;
2453 if (consing_since_gc > gc_cons_threshold)
2454 Fgarbage_collect ();
2456 if (++lisp_eval_depth > max_lisp_eval_depth)
2458 if (max_lisp_eval_depth < 100)
2459 max_lisp_eval_depth = 100;
2460 if (lisp_eval_depth > max_lisp_eval_depth)
2461 error ("Lisp nesting exceeds max-lisp-eval-depth");
2464 backtrace.next = backtrace_list;
2465 backtrace_list = &backtrace;
2466 backtrace.function = &args[0];
2467 backtrace.args = &args[1];
2468 backtrace.nargs = nargs - 1;
2469 backtrace.evalargs = 0;
2470 backtrace.debug_on_exit = 0;
2472 if (debug_on_next_call)
2473 do_debug_on_call (Qlambda);
2475 retry:
2477 fun = args[0];
2479 fun = Findirect_function (fun);
2481 if (SUBRP (fun))
2483 if (numargs < XSUBR (fun)->min_args
2484 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2486 XSETFASTINT (lisp_numargs, numargs);
2487 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2490 if (XSUBR (fun)->max_args == UNEVALLED)
2491 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2493 if (XSUBR (fun)->max_args == MANY)
2495 val = (*XSUBR (fun)->function) (numargs, args + 1);
2496 goto done;
2499 if (XSUBR (fun)->max_args > numargs)
2501 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2502 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2503 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2504 internal_args[i] = Qnil;
2506 else
2507 internal_args = args + 1;
2508 switch (XSUBR (fun)->max_args)
2510 case 0:
2511 val = (*XSUBR (fun)->function) ();
2512 goto done;
2513 case 1:
2514 val = (*XSUBR (fun)->function) (internal_args[0]);
2515 goto done;
2516 case 2:
2517 val = (*XSUBR (fun)->function) (internal_args[0],
2518 internal_args[1]);
2519 goto done;
2520 case 3:
2521 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2522 internal_args[2]);
2523 goto done;
2524 case 4:
2525 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2526 internal_args[2],
2527 internal_args[3]);
2528 goto done;
2529 case 5:
2530 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2531 internal_args[2], internal_args[3],
2532 internal_args[4]);
2533 goto done;
2534 case 6:
2535 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2536 internal_args[2], internal_args[3],
2537 internal_args[4], internal_args[5]);
2538 goto done;
2539 case 7:
2540 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2541 internal_args[2], internal_args[3],
2542 internal_args[4], internal_args[5],
2543 internal_args[6]);
2544 goto done;
2546 case 8:
2547 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2548 internal_args[2], internal_args[3],
2549 internal_args[4], internal_args[5],
2550 internal_args[6], internal_args[7]);
2551 goto done;
2553 default:
2555 /* If a subr takes more than 8 arguments without using MANY
2556 or UNEVALLED, we need to extend this function to support it.
2557 Until this is done, there is no way to call the function. */
2558 abort ();
2561 if (COMPILEDP (fun))
2562 val = funcall_lambda (fun, numargs, args + 1);
2563 else
2565 if (!CONSP (fun))
2566 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2567 funcar = Fcar (fun);
2568 if (!SYMBOLP (funcar))
2569 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2570 if (EQ (funcar, Qlambda))
2571 val = funcall_lambda (fun, numargs, args + 1);
2572 else if (EQ (funcar, Qmocklisp))
2573 val = ml_apply (fun, Flist (numargs, args + 1));
2574 else if (EQ (funcar, Qautoload))
2576 do_autoload (fun, args[0]);
2577 goto retry;
2579 else
2580 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2582 done:
2583 lisp_eval_depth--;
2584 if (backtrace.debug_on_exit)
2585 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2586 backtrace_list = backtrace.next;
2587 return val;
2590 Lisp_Object
2591 apply_lambda (fun, args, eval_flag)
2592 Lisp_Object fun, args;
2593 int eval_flag;
2595 Lisp_Object args_left;
2596 Lisp_Object numargs;
2597 register Lisp_Object *arg_vector;
2598 struct gcpro gcpro1, gcpro2, gcpro3;
2599 register int i;
2600 register Lisp_Object tem;
2602 numargs = Flength (args);
2603 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2604 args_left = args;
2606 GCPRO3 (*arg_vector, args_left, fun);
2607 gcpro1.nvars = 0;
2609 for (i = 0; i < XINT (numargs);)
2611 tem = Fcar (args_left), args_left = Fcdr (args_left);
2612 if (eval_flag) tem = Feval (tem);
2613 arg_vector[i++] = tem;
2614 gcpro1.nvars = i;
2617 UNGCPRO;
2619 if (eval_flag)
2621 backtrace_list->args = arg_vector;
2622 backtrace_list->nargs = i;
2624 backtrace_list->evalargs = 0;
2625 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2627 /* Do the debug-on-exit now, while arg_vector still exists. */
2628 if (backtrace_list->debug_on_exit)
2629 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2630 /* Don't do it again when we return to eval. */
2631 backtrace_list->debug_on_exit = 0;
2632 return tem;
2635 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2636 and return the result of evaluation.
2637 FUN must be either a lambda-expression or a compiled-code object. */
2639 Lisp_Object
2640 funcall_lambda (fun, nargs, arg_vector)
2641 Lisp_Object fun;
2642 int nargs;
2643 register Lisp_Object *arg_vector;
2645 Lisp_Object val, syms_left, next;
2646 int count = specpdl_ptr - specpdl;
2647 int i, optional, rest;
2649 if (NILP (Vmocklisp_arguments))
2650 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2652 if (CONSP (fun))
2654 syms_left = XCDR (fun);
2655 if (CONSP (syms_left))
2656 syms_left = XCAR (syms_left);
2657 else
2658 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2660 else if (COMPILEDP (fun))
2661 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2662 else
2663 abort ();
2665 i = optional = rest = 0;
2666 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2668 QUIT;
2670 next = XCAR (syms_left);
2671 while (!SYMBOLP (next))
2672 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2674 if (EQ (next, Qand_rest))
2675 rest = 1;
2676 else if (EQ (next, Qand_optional))
2677 optional = 1;
2678 else if (rest)
2680 specbind (next, Flist (nargs - i, &arg_vector[i]));
2681 i = nargs;
2683 else if (i < nargs)
2684 specbind (next, arg_vector[i++]);
2685 else if (!optional)
2686 return Fsignal (Qwrong_number_of_arguments,
2687 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2688 else
2689 specbind (next, Qnil);
2692 if (!NILP (syms_left))
2693 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2694 else if (i < nargs)
2695 return Fsignal (Qwrong_number_of_arguments,
2696 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2698 if (CONSP (fun))
2699 val = Fprogn (XCDR (XCDR (fun)));
2700 else
2702 /* If we have not actually read the bytecode string
2703 and constants vector yet, fetch them from the file. */
2704 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2705 Ffetch_bytecode (fun);
2706 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2707 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2708 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2711 return unbind_to (count, val);
2714 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2715 1, 1, 0,
2716 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2717 (object)
2718 Lisp_Object object;
2720 Lisp_Object tem;
2722 if (COMPILEDP (object)
2723 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2725 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2726 if (!CONSP (tem))
2727 error ("invalid byte code");
2728 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCAR (tem);
2729 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCDR (tem);
2731 return object;
2734 void
2735 grow_specpdl ()
2737 register int count = specpdl_ptr - specpdl;
2738 if (specpdl_size >= max_specpdl_size)
2740 if (max_specpdl_size < 400)
2741 max_specpdl_size = 400;
2742 if (specpdl_size >= max_specpdl_size)
2744 if (!NILP (Vdebug_on_error))
2745 /* Leave room for some specpdl in the debugger. */
2746 max_specpdl_size = specpdl_size + 100;
2747 Fsignal (Qerror,
2748 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2751 specpdl_size *= 2;
2752 if (specpdl_size > max_specpdl_size)
2753 specpdl_size = max_specpdl_size;
2754 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2755 specpdl_ptr = specpdl + count;
2758 void
2759 specbind (symbol, value)
2760 Lisp_Object symbol, value;
2762 Lisp_Object ovalue;
2764 CHECK_SYMBOL (symbol, 0);
2765 if (specpdl_ptr == specpdl + specpdl_size)
2766 grow_specpdl ();
2768 /* The most common case is that a non-constant symbol with a trivial
2769 value. Make that as fast as we can. */
2770 if (!MISCP (XSYMBOL (symbol)->value)
2771 && !EQ (symbol, Qnil)
2772 && !EQ (symbol, Qt)
2773 && !(XSYMBOL (symbol)->name->data[0] == ':'
2774 && EQ (XSYMBOL (symbol)->obarray, initial_obarray)
2775 && !EQ (value, symbol)))
2777 specpdl_ptr->symbol = symbol;
2778 specpdl_ptr->old_value = XSYMBOL (symbol)->value;
2779 specpdl_ptr->func = NULL;
2780 ++specpdl_ptr;
2781 XSYMBOL (symbol)->value = value;
2783 else
2785 ovalue = find_symbol_value (symbol);
2786 specpdl_ptr->func = 0;
2787 specpdl_ptr->old_value = ovalue;
2789 if (BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2790 || SOME_BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2791 || BUFFER_OBJFWDP (XSYMBOL (symbol)->value))
2793 Lisp_Object current_buffer, binding_buffer;
2794 /* For a local variable, record both the symbol and which
2795 buffer's value we are saving. */
2796 current_buffer = Fcurrent_buffer ();
2797 binding_buffer = current_buffer;
2798 /* If the variable is not local in this buffer,
2799 we are saving the global value, so restore that. */
2800 if (NILP (Flocal_variable_p (symbol, binding_buffer)))
2801 binding_buffer = Qnil;
2802 specpdl_ptr->symbol
2803 = Fcons (symbol, Fcons (binding_buffer, current_buffer));
2805 else
2806 specpdl_ptr->symbol = symbol;
2808 specpdl_ptr++;
2809 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2810 store_symval_forwarding (symbol, ovalue, value);
2811 else
2812 set_internal (symbol, value, 0, 1);
2816 void
2817 record_unwind_protect (function, arg)
2818 Lisp_Object (*function) P_ ((Lisp_Object));
2819 Lisp_Object arg;
2821 if (specpdl_ptr == specpdl + specpdl_size)
2822 grow_specpdl ();
2823 specpdl_ptr->func = function;
2824 specpdl_ptr->symbol = Qnil;
2825 specpdl_ptr->old_value = arg;
2826 specpdl_ptr++;
2829 Lisp_Object
2830 unbind_to (count, value)
2831 int count;
2832 Lisp_Object value;
2834 int quitf = !NILP (Vquit_flag);
2835 struct gcpro gcpro1;
2837 GCPRO1 (value);
2838 Vquit_flag = Qnil;
2840 while (specpdl_ptr != specpdl + count)
2842 --specpdl_ptr;
2844 if (specpdl_ptr->func != 0)
2845 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2846 /* Note that a "binding" of nil is really an unwind protect,
2847 so in that case the "old value" is a list of forms to evaluate. */
2848 else if (NILP (specpdl_ptr->symbol))
2849 Fprogn (specpdl_ptr->old_value);
2850 /* If the symbol is a list, it is really
2851 (SYMBOL BINDING_BUFFER . CURRENT_BUFFER)
2852 and it indicates we bound a variable that has
2853 buffer-local bindings. */
2854 else if (CONSP (specpdl_ptr->symbol))
2856 Lisp_Object symbol, buffer;
2858 symbol = XCAR (specpdl_ptr->symbol);
2859 buffer = XCAR (XCDR (specpdl_ptr->symbol));
2861 /* Handle restoring a default value. */
2862 if (NILP (buffer))
2863 Fset_default (symbol, specpdl_ptr->old_value);
2864 /* Handle restoring a value saved from a live buffer. */
2865 else
2866 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (buffer), 1);
2868 else
2870 /* If variable has a trivial value (no forwarding), we can
2871 just set it. No need to check for constant symbols here,
2872 since that was already done by specbind. */
2873 if (!MISCP (XSYMBOL (specpdl_ptr->symbol)->value))
2874 XSYMBOL (specpdl_ptr->symbol)->value = specpdl_ptr->old_value;
2875 else
2876 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
2880 if (NILP (Vquit_flag) && quitf)
2881 Vquit_flag = Qt;
2883 UNGCPRO;
2884 return value;
2887 #if 0
2889 /* Get the value of symbol's global binding, even if that binding
2890 is not now dynamically visible. */
2892 Lisp_Object
2893 top_level_value (symbol)
2894 Lisp_Object symbol;
2896 register struct specbinding *ptr = specpdl;
2898 CHECK_SYMBOL (symbol, 0);
2899 for (; ptr != specpdl_ptr; ptr++)
2901 if (EQ (ptr->symbol, symbol))
2902 return ptr->old_value;
2904 return Fsymbol_value (symbol);
2907 Lisp_Object
2908 top_level_set (symbol, newval)
2909 Lisp_Object symbol, newval;
2911 register struct specbinding *ptr = specpdl;
2913 CHECK_SYMBOL (symbol, 0);
2914 for (; ptr != specpdl_ptr; ptr++)
2916 if (EQ (ptr->symbol, symbol))
2918 ptr->old_value = newval;
2919 return newval;
2922 return Fset (symbol, newval);
2925 #endif /* 0 */
2927 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2928 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2929 The debugger is entered when that frame exits, if the flag is non-nil.")
2930 (level, flag)
2931 Lisp_Object level, flag;
2933 register struct backtrace *backlist = backtrace_list;
2934 register int i;
2936 CHECK_NUMBER (level, 0);
2938 for (i = 0; backlist && i < XINT (level); i++)
2940 backlist = backlist->next;
2943 if (backlist)
2944 backlist->debug_on_exit = !NILP (flag);
2946 return flag;
2949 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2950 "Print a trace of Lisp function calls currently active.\n\
2951 Output stream used is value of `standard-output'.")
2954 register struct backtrace *backlist = backtrace_list;
2955 register int i;
2956 Lisp_Object tail;
2957 Lisp_Object tem;
2958 extern Lisp_Object Vprint_level;
2959 struct gcpro gcpro1;
2961 XSETFASTINT (Vprint_level, 3);
2963 tail = Qnil;
2964 GCPRO1 (tail);
2966 while (backlist)
2968 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2969 if (backlist->nargs == UNEVALLED)
2971 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2972 write_string ("\n", -1);
2974 else
2976 tem = *backlist->function;
2977 Fprin1 (tem, Qnil); /* This can QUIT */
2978 write_string ("(", -1);
2979 if (backlist->nargs == MANY)
2981 for (tail = *backlist->args, i = 0;
2982 !NILP (tail);
2983 tail = Fcdr (tail), i++)
2985 if (i) write_string (" ", -1);
2986 Fprin1 (Fcar (tail), Qnil);
2989 else
2991 for (i = 0; i < backlist->nargs; i++)
2993 if (i) write_string (" ", -1);
2994 Fprin1 (backlist->args[i], Qnil);
2997 write_string (")\n", -1);
2999 backlist = backlist->next;
3002 Vprint_level = Qnil;
3003 UNGCPRO;
3004 return Qnil;
3007 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
3008 "Return the function and arguments NFRAMES up from current execution point.\n\
3009 If that frame has not evaluated the arguments yet (or is a special form),\n\
3010 the value is (nil FUNCTION ARG-FORMS...).\n\
3011 If that frame has evaluated its arguments and called its function already,\n\
3012 the value is (t FUNCTION ARG-VALUES...).\n\
3013 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
3014 FUNCTION is whatever was supplied as car of evaluated list,\n\
3015 or a lambda expression for macro calls.\n\
3016 If NFRAMES is more than the number of frames, the value is nil.")
3017 (nframes)
3018 Lisp_Object nframes;
3020 register struct backtrace *backlist = backtrace_list;
3021 register int i;
3022 Lisp_Object tem;
3024 CHECK_NATNUM (nframes, 0);
3026 /* Find the frame requested. */
3027 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3028 backlist = backlist->next;
3030 if (!backlist)
3031 return Qnil;
3032 if (backlist->nargs == UNEVALLED)
3033 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3034 else
3036 if (backlist->nargs == MANY)
3037 tem = *backlist->args;
3038 else
3039 tem = Flist (backlist->nargs, backlist->args);
3041 return Fcons (Qt, Fcons (*backlist->function, tem));
3045 void
3046 syms_of_eval ()
3048 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3049 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
3050 If Lisp code tries to make more than this many at once,\n\
3051 an error is signaled.");
3053 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3054 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
3055 This limit is to catch infinite recursions for you before they cause\n\
3056 actual stack overflow in C, which would be fatal for Emacs.\n\
3057 You can safely make it considerably larger than its default value,\n\
3058 if that proves inconveniently small.");
3060 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3061 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
3062 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
3063 Vquit_flag = Qnil;
3065 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3066 "Non-nil inhibits C-g quitting from happening immediately.\n\
3067 Note that `quit-flag' will still be set by typing C-g,\n\
3068 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
3069 To prevent this happening, set `quit-flag' to nil\n\
3070 before making `inhibit-quit' nil.");
3071 Vinhibit_quit = Qnil;
3073 Qinhibit_quit = intern ("inhibit-quit");
3074 staticpro (&Qinhibit_quit);
3076 Qautoload = intern ("autoload");
3077 staticpro (&Qautoload);
3079 Qdebug_on_error = intern ("debug-on-error");
3080 staticpro (&Qdebug_on_error);
3082 Qmacro = intern ("macro");
3083 staticpro (&Qmacro);
3085 /* Note that the process handling also uses Qexit, but we don't want
3086 to staticpro it twice, so we just do it here. */
3087 Qexit = intern ("exit");
3088 staticpro (&Qexit);
3090 Qinteractive = intern ("interactive");
3091 staticpro (&Qinteractive);
3093 Qcommandp = intern ("commandp");
3094 staticpro (&Qcommandp);
3096 Qdefun = intern ("defun");
3097 staticpro (&Qdefun);
3099 Qand_rest = intern ("&rest");
3100 staticpro (&Qand_rest);
3102 Qand_optional = intern ("&optional");
3103 staticpro (&Qand_optional);
3105 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3106 "*Non-nil means automatically display a backtrace buffer\n\
3107 after any error that is handled by the editor command loop.\n\
3108 If the value is a list, an error only means to display a backtrace\n\
3109 if one of its condition symbols appears in the list.");
3110 Vstack_trace_on_error = Qnil;
3112 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3113 "*Non-nil means enter debugger if an error is signaled.\n\
3114 Does not apply to errors handled by `condition-case'.\n\
3115 If the value is a list, an error only means to enter the debugger\n\
3116 if one of its condition symbols appears in the list.\n\
3117 See also variable `debug-on-quit'.");
3118 Vdebug_on_error = Qnil;
3120 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3121 "*List of errors for which the debugger should not be called.\n\
3122 Each element may be a condition-name or a regexp that matches error messages.\n\
3123 If any element applies to a given error, that error skips the debugger\n\
3124 and just returns to top level.\n\
3125 This overrides the variable `debug-on-error'.\n\
3126 It does not apply to errors handled by `condition-case'.");
3127 Vdebug_ignored_errors = Qnil;
3129 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3130 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3131 Does not apply if quit is handled by a `condition-case'.");
3132 debug_on_quit = 0;
3134 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3135 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3137 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3138 "Non-nil means debugger may continue execution.\n\
3139 This is nil when the debugger is called under circumstances where it\n\
3140 might not be safe to continue.");
3141 debugger_may_continue = 1;
3143 DEFVAR_LISP ("debugger", &Vdebugger,
3144 "Function to call to invoke debugger.\n\
3145 If due to frame exit, args are `exit' and the value being returned;\n\
3146 this function's value will be returned instead of that.\n\
3147 If due to error, args are `error' and a list of the args to `signal'.\n\
3148 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3149 If due to `eval' entry, one arg, t.");
3150 Vdebugger = Qnil;
3152 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3153 "If non-nil, this is a function for `signal' to call.\n\
3154 It receives the same arguments that `signal' was given.\n\
3155 The Edebug package uses this to regain control.");
3156 Vsignal_hook_function = Qnil;
3158 Qmocklisp_arguments = intern ("mocklisp-arguments");
3159 staticpro (&Qmocklisp_arguments);
3160 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
3161 "While in a mocklisp function, the list of its unevaluated args.");
3162 Vmocklisp_arguments = Qt;
3164 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3165 "*Non-nil means call the debugger regardless of condition handlers.\n\
3166 Note that `debug-on-error', `debug-on-quit' and friends\n\
3167 still determine whether to handle the particular condition.");
3168 Vdebug_on_signal = Qnil;
3170 Vrun_hooks = intern ("run-hooks");
3171 staticpro (&Vrun_hooks);
3173 staticpro (&Vautoload_queue);
3174 Vautoload_queue = Qnil;
3176 defsubr (&Sor);
3177 defsubr (&Sand);
3178 defsubr (&Sif);
3179 defsubr (&Scond);
3180 defsubr (&Sprogn);
3181 defsubr (&Sprog1);
3182 defsubr (&Sprog2);
3183 defsubr (&Ssetq);
3184 defsubr (&Squote);
3185 defsubr (&Sfunction);
3186 defsubr (&Sdefun);
3187 defsubr (&Sdefmacro);
3188 defsubr (&Sdefvar);
3189 defsubr (&Sdefconst);
3190 defsubr (&Suser_variable_p);
3191 defsubr (&Slet);
3192 defsubr (&SletX);
3193 defsubr (&Swhile);
3194 defsubr (&Smacroexpand);
3195 defsubr (&Scatch);
3196 defsubr (&Sthrow);
3197 defsubr (&Sunwind_protect);
3198 defsubr (&Scondition_case);
3199 defsubr (&Ssignal);
3200 defsubr (&Sinteractive_p);
3201 defsubr (&Scommandp);
3202 defsubr (&Sautoload);
3203 defsubr (&Seval);
3204 defsubr (&Sapply);
3205 defsubr (&Sfuncall);
3206 defsubr (&Srun_hooks);
3207 defsubr (&Srun_hook_with_args);
3208 defsubr (&Srun_hook_with_args_until_success);
3209 defsubr (&Srun_hook_with_args_until_failure);
3210 defsubr (&Sfetch_bytecode);
3211 defsubr (&Sbacktrace_debug);
3212 defsubr (&Sbacktrace);
3213 defsubr (&Sbacktrace_frame);