Allow comma separated lists after Java "implements".
[emacs.git] / src / eval.c
blob1e0a63a0eceb54e5e04522ee8eae180bd1249132
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2013 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 3 of the License, or
10 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
31 #if HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
35 /* Chain of condition and catch handlers currently in effect. */
37 struct handler *handlerlist;
39 #ifdef DEBUG_GCPRO
40 /* Count levels of GCPRO to detect failure to UNGCPRO. */
41 int gcpro_level;
42 #endif
44 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
45 Lisp_Object Qinhibit_quit;
46 Lisp_Object Qand_rest;
47 static Lisp_Object Qand_optional;
48 static Lisp_Object Qinhibit_debugger;
49 static Lisp_Object Qdeclare;
50 Lisp_Object Qinternal_interpreter_environment, Qclosure;
52 static Lisp_Object Qdebug;
54 /* This holds either the symbol `run-hooks' or nil.
55 It is nil at an early stage of startup, and when Emacs
56 is shutting down. */
58 Lisp_Object Vrun_hooks;
60 /* Non-nil means record all fset's and provide's, to be undone
61 if the file being autoloaded is not fully loaded.
62 They are recorded by being consed onto the front of Vautoload_queue:
63 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
65 Lisp_Object Vautoload_queue;
67 /* Current number of specbindings allocated in specpdl, not counting
68 the dummy entry specpdl[-1]. */
70 ptrdiff_t specpdl_size;
72 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
73 only so that its address can be taken. */
75 union specbinding *specpdl;
77 /* Pointer to first unused element in specpdl. */
79 union specbinding *specpdl_ptr;
81 /* Depth in Lisp evaluations and function calls. */
83 EMACS_INT lisp_eval_depth;
85 /* The value of num_nonmacro_input_events as of the last time we
86 started to enter the debugger. If we decide to enter the debugger
87 again when this is still equal to num_nonmacro_input_events, then we
88 know that the debugger itself has an error, and we should just
89 signal the error instead of entering an infinite loop of debugger
90 invocations. */
92 static EMACS_INT when_entered_debugger;
94 /* The function from which the last `signal' was called. Set in
95 Fsignal. */
96 /* FIXME: We should probably get rid of this! */
97 Lisp_Object Vsignaling_function;
99 /* If non-nil, Lisp code must not be run since some part of Emacs is
100 in an inconsistent state. Currently, x-create-frame uses this to
101 avoid triggering window-configuration-change-hook while the new
102 frame is half-initialized. */
103 Lisp_Object inhibit_lisp_code;
105 /* These would ordinarily be static, but they need to be visible to GDB. */
106 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
107 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
108 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
109 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
110 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
112 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
113 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
115 static Lisp_Object
116 specpdl_symbol (union specbinding *pdl)
118 eassert (pdl->kind >= SPECPDL_LET);
119 return pdl->let.symbol;
122 static Lisp_Object
123 specpdl_old_value (union specbinding *pdl)
125 eassert (pdl->kind >= SPECPDL_LET);
126 return pdl->let.old_value;
129 static void
130 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
132 eassert (pdl->kind >= SPECPDL_LET);
133 pdl->let.old_value = val;
136 static Lisp_Object
137 specpdl_where (union specbinding *pdl)
139 eassert (pdl->kind > SPECPDL_LET);
140 return pdl->let.where;
143 static Lisp_Object
144 specpdl_arg (union specbinding *pdl)
146 eassert (pdl->kind == SPECPDL_UNWIND);
147 return pdl->unwind.arg;
150 Lisp_Object
151 backtrace_function (union specbinding *pdl)
153 eassert (pdl->kind == SPECPDL_BACKTRACE);
154 return pdl->bt.function;
157 static ptrdiff_t
158 backtrace_nargs (union specbinding *pdl)
160 eassert (pdl->kind == SPECPDL_BACKTRACE);
161 return pdl->bt.nargs;
164 Lisp_Object *
165 backtrace_args (union specbinding *pdl)
167 eassert (pdl->kind == SPECPDL_BACKTRACE);
168 return pdl->bt.args;
171 static bool
172 backtrace_debug_on_exit (union specbinding *pdl)
174 eassert (pdl->kind == SPECPDL_BACKTRACE);
175 return pdl->bt.debug_on_exit;
178 /* Functions to modify slots of backtrace records. */
180 static void
181 set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
183 eassert (pdl->kind == SPECPDL_BACKTRACE);
184 pdl->bt.args = args;
187 static void
188 set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
190 eassert (pdl->kind == SPECPDL_BACKTRACE);
191 pdl->bt.nargs = n;
194 static void
195 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
197 eassert (pdl->kind == SPECPDL_BACKTRACE);
198 pdl->bt.debug_on_exit = doe;
201 /* Helper functions to scan the backtrace. */
203 bool
204 backtrace_p (union specbinding *pdl)
205 { return pdl >= specpdl; }
207 union specbinding *
208 backtrace_top (void)
210 union specbinding *pdl = specpdl_ptr - 1;
211 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
212 pdl--;
213 return pdl;
216 union specbinding *
217 backtrace_next (union specbinding *pdl)
219 pdl--;
220 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
221 pdl--;
222 return pdl;
226 void
227 init_eval_once (void)
229 enum { size = 50 };
230 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
231 specpdl_size = size;
232 specpdl = specpdl_ptr = pdlvec + 1;
233 /* Don't forget to update docs (lispref node "Local Variables"). */
234 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
235 max_lisp_eval_depth = 600;
237 Vrun_hooks = Qnil;
240 void
241 init_eval (void)
243 specpdl_ptr = specpdl;
244 handlerlist = NULL;
245 Vquit_flag = Qnil;
246 debug_on_next_call = 0;
247 lisp_eval_depth = 0;
248 #ifdef DEBUG_GCPRO
249 gcpro_level = 0;
250 #endif
251 /* This is less than the initial value of num_nonmacro_input_events. */
252 when_entered_debugger = -1;
255 /* Unwind-protect function used by call_debugger. */
257 static void
258 restore_stack_limits (Lisp_Object data)
260 max_specpdl_size = XINT (XCAR (data));
261 max_lisp_eval_depth = XINT (XCDR (data));
264 /* Call the Lisp debugger, giving it argument ARG. */
266 Lisp_Object
267 call_debugger (Lisp_Object arg)
269 bool debug_while_redisplaying;
270 ptrdiff_t count = SPECPDL_INDEX ();
271 Lisp_Object val;
272 EMACS_INT old_max = max_specpdl_size;
274 /* Temporarily bump up the stack limits,
275 so the debugger won't run out of stack. */
277 max_specpdl_size += 1;
278 record_unwind_protect (restore_stack_limits,
279 Fcons (make_number (old_max),
280 make_number (max_lisp_eval_depth)));
281 max_specpdl_size = old_max;
283 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
284 max_lisp_eval_depth = lisp_eval_depth + 40;
286 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
287 max_specpdl_size = SPECPDL_INDEX () + 100;
289 #ifdef HAVE_WINDOW_SYSTEM
290 if (display_hourglass_p)
291 cancel_hourglass ();
292 #endif
294 debug_on_next_call = 0;
295 when_entered_debugger = num_nonmacro_input_events;
297 /* Resetting redisplaying_p to 0 makes sure that debug output is
298 displayed if the debugger is invoked during redisplay. */
299 debug_while_redisplaying = redisplaying_p;
300 redisplaying_p = 0;
301 specbind (intern ("debugger-may-continue"),
302 debug_while_redisplaying ? Qnil : Qt);
303 specbind (Qinhibit_redisplay, Qnil);
304 specbind (Qinhibit_debugger, Qt);
306 #if 0 /* Binding this prevents execution of Lisp code during
307 redisplay, which necessarily leads to display problems. */
308 specbind (Qinhibit_eval_during_redisplay, Qt);
309 #endif
311 val = apply1 (Vdebugger, arg);
313 /* Interrupting redisplay and resuming it later is not safe under
314 all circumstances. So, when the debugger returns, abort the
315 interrupted redisplay by going back to the top-level. */
316 if (debug_while_redisplaying)
317 Ftop_level ();
319 return unbind_to (count, val);
322 static void
323 do_debug_on_call (Lisp_Object code)
325 debug_on_next_call = 0;
326 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
327 call_debugger (list1 (code));
330 /* NOTE!!! Every function that can call EVAL must protect its args
331 and temporaries from garbage collection while it needs them.
332 The definition of `For' shows what you have to do. */
334 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
335 doc: /* Eval args until one of them yields non-nil, then return that value.
336 The remaining args are not evalled at all.
337 If all args return nil, return nil.
338 usage: (or CONDITIONS...) */)
339 (Lisp_Object args)
341 register Lisp_Object val = Qnil;
342 struct gcpro gcpro1;
344 GCPRO1 (args);
346 while (CONSP (args))
348 val = eval_sub (XCAR (args));
349 if (!NILP (val))
350 break;
351 args = XCDR (args);
354 UNGCPRO;
355 return val;
358 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
359 doc: /* Eval args until one of them yields nil, then return nil.
360 The remaining args are not evalled at all.
361 If no arg yields nil, return the last arg's value.
362 usage: (and CONDITIONS...) */)
363 (Lisp_Object args)
365 register Lisp_Object val = Qt;
366 struct gcpro gcpro1;
368 GCPRO1 (args);
370 while (CONSP (args))
372 val = eval_sub (XCAR (args));
373 if (NILP (val))
374 break;
375 args = XCDR (args);
378 UNGCPRO;
379 return val;
382 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
383 doc: /* If COND yields non-nil, do THEN, else do ELSE...
384 Returns the value of THEN or the value of the last of the ELSE's.
385 THEN must be one expression, but ELSE... can be zero or more expressions.
386 If COND yields nil, and there are no ELSE's, the value is nil.
387 usage: (if COND THEN ELSE...) */)
388 (Lisp_Object args)
390 Lisp_Object cond;
391 struct gcpro gcpro1;
393 GCPRO1 (args);
394 cond = eval_sub (XCAR (args));
395 UNGCPRO;
397 if (!NILP (cond))
398 return eval_sub (Fcar (XCDR (args)));
399 return Fprogn (XCDR (XCDR (args)));
402 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
403 doc: /* Try each clause until one succeeds.
404 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
405 and, if the value is non-nil, this clause succeeds:
406 then the expressions in BODY are evaluated and the last one's
407 value is the value of the cond-form.
408 If a clause has one element, as in (CONDITION), then the cond-form
409 returns CONDITION's value, if that is non-nil.
410 If no clause succeeds, cond returns nil.
411 usage: (cond CLAUSES...) */)
412 (Lisp_Object args)
414 Lisp_Object val = args;
415 struct gcpro gcpro1;
417 GCPRO1 (args);
418 while (CONSP (args))
420 Lisp_Object clause = XCAR (args);
421 val = eval_sub (Fcar (clause));
422 if (!NILP (val))
424 if (!NILP (XCDR (clause)))
425 val = Fprogn (XCDR (clause));
426 break;
428 args = XCDR (args);
430 UNGCPRO;
432 return val;
435 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
436 doc: /* Eval BODY forms sequentially and return value of last one.
437 usage: (progn BODY...) */)
438 (Lisp_Object body)
440 Lisp_Object val = Qnil;
441 struct gcpro gcpro1;
443 GCPRO1 (body);
445 while (CONSP (body))
447 val = eval_sub (XCAR (body));
448 body = XCDR (body);
451 UNGCPRO;
452 return val;
455 /* Evaluate BODY sequentially, discarding its value. Suitable for
456 record_unwind_protect. */
458 void
459 unwind_body (Lisp_Object body)
461 Fprogn (body);
464 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
465 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
466 The value of FIRST is saved during the evaluation of the remaining args,
467 whose values are discarded.
468 usage: (prog1 FIRST BODY...) */)
469 (Lisp_Object args)
471 Lisp_Object val;
472 Lisp_Object args_left;
473 struct gcpro gcpro1, gcpro2;
475 args_left = args;
476 val = args;
477 GCPRO2 (args, val);
479 val = eval_sub (XCAR (args_left));
480 while (CONSP (args_left = XCDR (args_left)))
481 eval_sub (XCAR (args_left));
483 UNGCPRO;
484 return val;
487 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
488 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
489 The value of FORM2 is saved during the evaluation of the
490 remaining args, whose values are discarded.
491 usage: (prog2 FORM1 FORM2 BODY...) */)
492 (Lisp_Object args)
494 struct gcpro gcpro1;
496 GCPRO1 (args);
497 eval_sub (XCAR (args));
498 UNGCPRO;
499 return Fprog1 (XCDR (args));
502 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
503 doc: /* Set each SYM to the value of its VAL.
504 The symbols SYM are variables; they are literal (not evaluated).
505 The values VAL are expressions; they are evaluated.
506 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
507 The second VAL is not computed until after the first SYM is set, and so on;
508 each VAL can use the new value of variables set earlier in the `setq'.
509 The return value of the `setq' form is the value of the last VAL.
510 usage: (setq [SYM VAL]...) */)
511 (Lisp_Object args)
513 Lisp_Object val, sym, lex_binding;
515 val = args;
516 if (CONSP (args))
518 Lisp_Object args_left = args;
519 struct gcpro gcpro1;
520 GCPRO1 (args);
524 val = eval_sub (Fcar (XCDR (args_left)));
525 sym = XCAR (args_left);
527 /* Like for eval_sub, we do not check declared_special here since
528 it's been done when let-binding. */
529 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
530 && SYMBOLP (sym)
531 && !NILP (lex_binding
532 = Fassq (sym, Vinternal_interpreter_environment)))
533 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
534 else
535 Fset (sym, val); /* SYM is dynamically bound. */
537 args_left = Fcdr (XCDR (args_left));
539 while (CONSP (args_left));
541 UNGCPRO;
544 return val;
547 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
548 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
549 Warning: `quote' does not construct its return value, but just returns
550 the value that was pre-constructed by the Lisp reader (see info node
551 `(elisp)Printed Representation').
552 This means that '(a . b) is not identical to (cons 'a 'b): the former
553 does not cons. Quoting should be reserved for constants that will
554 never be modified by side-effects, unless you like self-modifying code.
555 See the common pitfall in info node `(elisp)Rearrangement' for an example
556 of unexpected results when a quoted object is modified.
557 usage: (quote ARG) */)
558 (Lisp_Object args)
560 if (CONSP (XCDR (args)))
561 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
562 return XCAR (args);
565 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
566 doc: /* Like `quote', but preferred for objects which are functions.
567 In byte compilation, `function' causes its argument to be compiled.
568 `quote' cannot do that.
569 usage: (function ARG) */)
570 (Lisp_Object args)
572 Lisp_Object quoted = XCAR (args);
574 if (CONSP (XCDR (args)))
575 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
577 if (!NILP (Vinternal_interpreter_environment)
578 && CONSP (quoted)
579 && EQ (XCAR (quoted), Qlambda))
580 /* This is a lambda expression within a lexical environment;
581 return an interpreted closure instead of a simple lambda. */
582 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
583 XCDR (quoted)));
584 else
585 /* Simply quote the argument. */
586 return quoted;
590 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
591 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
592 Aliased variables always have the same value; setting one sets the other.
593 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
594 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
595 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
596 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
597 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
598 The return value is BASE-VARIABLE. */)
599 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
601 struct Lisp_Symbol *sym;
603 CHECK_SYMBOL (new_alias);
604 CHECK_SYMBOL (base_variable);
606 sym = XSYMBOL (new_alias);
608 if (sym->constant)
609 /* Not sure why, but why not? */
610 error ("Cannot make a constant an alias");
612 switch (sym->redirect)
614 case SYMBOL_FORWARDED:
615 error ("Cannot make an internal variable an alias");
616 case SYMBOL_LOCALIZED:
617 error ("Don't know how to make a localized variable an alias");
620 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
621 If n_a is bound, but b_v is not, set the value of b_v to n_a,
622 so that old-code that affects n_a before the aliasing is setup
623 still works. */
624 if (NILP (Fboundp (base_variable)))
625 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
628 union specbinding *p;
630 for (p = specpdl_ptr; p > specpdl; )
631 if ((--p)->kind >= SPECPDL_LET
632 && (EQ (new_alias, specpdl_symbol (p))))
633 error ("Don't know how to make a let-bound variable an alias");
636 sym->declared_special = 1;
637 XSYMBOL (base_variable)->declared_special = 1;
638 sym->redirect = SYMBOL_VARALIAS;
639 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
640 sym->constant = SYMBOL_CONSTANT_P (base_variable);
641 LOADHIST_ATTACH (new_alias);
642 /* Even if docstring is nil: remove old docstring. */
643 Fput (new_alias, Qvariable_documentation, docstring);
645 return base_variable;
648 static union specbinding *
649 default_toplevel_binding (Lisp_Object symbol)
651 union specbinding *binding = NULL;
652 union specbinding *pdl = specpdl_ptr;
653 while (pdl > specpdl)
655 switch ((--pdl)->kind)
657 case SPECPDL_LET_DEFAULT:
658 case SPECPDL_LET:
659 if (EQ (specpdl_symbol (pdl), symbol))
660 binding = pdl;
661 break;
664 return binding;
667 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
668 doc: /* Return SYMBOL's toplevel default value.
669 "Toplevel" means outside of any let binding. */)
670 (Lisp_Object symbol)
672 union specbinding *binding = default_toplevel_binding (symbol);
673 Lisp_Object value
674 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
675 if (!EQ (value, Qunbound))
676 return value;
677 xsignal1 (Qvoid_variable, symbol);
680 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
681 Sset_default_toplevel_value, 2, 2, 0,
682 doc: /* Set SYMBOL's toplevel default value to VALUE.
683 "Toplevel" means outside of any let binding. */)
684 (Lisp_Object symbol, Lisp_Object value)
686 union specbinding *binding = default_toplevel_binding (symbol);
687 if (binding)
688 set_specpdl_old_value (binding, value);
689 else
690 Fset_default (symbol, value);
691 return Qnil;
694 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
695 doc: /* Define SYMBOL as a variable, and return SYMBOL.
696 You are not required to define a variable in order to use it, but
697 defining it lets you supply an initial value and documentation, which
698 can be referred to by the Emacs help facilities and other programming
699 tools. The `defvar' form also declares the variable as \"special\",
700 so that it is always dynamically bound even if `lexical-binding' is t.
702 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
703 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
704 default value is what is set; buffer-local values are not affected.
705 If INITVALUE is missing, SYMBOL's value is not set.
707 If SYMBOL has a local binding, then this form affects the local
708 binding. This is usually not what you want. Thus, if you need to
709 load a file defining variables, with this form or with `defconst' or
710 `defcustom', you should always load that file _outside_ any bindings
711 for these variables. \(`defconst' and `defcustom' behave similarly in
712 this respect.)
714 The optional argument DOCSTRING is a documentation string for the
715 variable.
717 To define a user option, use `defcustom' instead of `defvar'.
718 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
719 (Lisp_Object args)
721 Lisp_Object sym, tem, tail;
723 sym = XCAR (args);
724 tail = XCDR (args);
726 if (CONSP (tail))
728 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
729 error ("Too many arguments");
731 tem = Fdefault_boundp (sym);
733 /* Do it before evaluating the initial value, for self-references. */
734 XSYMBOL (sym)->declared_special = 1;
736 if (NILP (tem))
737 Fset_default (sym, eval_sub (XCAR (tail)));
738 else
739 { /* Check if there is really a global binding rather than just a let
740 binding that shadows the global unboundness of the var. */
741 union specbinding *binding = default_toplevel_binding (sym);
742 if (binding && EQ (specpdl_old_value (binding), Qunbound))
744 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
747 tail = XCDR (tail);
748 tem = Fcar (tail);
749 if (!NILP (tem))
751 if (!NILP (Vpurify_flag))
752 tem = Fpurecopy (tem);
753 Fput (sym, Qvariable_documentation, tem);
755 LOADHIST_ATTACH (sym);
757 else if (!NILP (Vinternal_interpreter_environment)
758 && !XSYMBOL (sym)->declared_special)
759 /* A simple (defvar foo) with lexical scoping does "nothing" except
760 declare that var to be dynamically scoped *locally* (i.e. within
761 the current file or let-block). */
762 Vinternal_interpreter_environment
763 = Fcons (sym, Vinternal_interpreter_environment);
764 else
766 /* Simple (defvar <var>) should not count as a definition at all.
767 It could get in the way of other definitions, and unloading this
768 package could try to make the variable unbound. */
771 return sym;
774 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
775 doc: /* Define SYMBOL as a constant variable.
776 This declares that neither programs nor users should ever change the
777 value. This constancy is not actually enforced by Emacs Lisp, but
778 SYMBOL is marked as a special variable so that it is never lexically
779 bound.
781 The `defconst' form always sets the value of SYMBOL to the result of
782 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
783 what is set; buffer-local values are not affected. If SYMBOL has a
784 local binding, then this form sets the local binding's value.
785 However, you should normally not make local bindings for variables
786 defined with this form.
788 The optional DOCSTRING specifies the variable's documentation string.
789 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
790 (Lisp_Object args)
792 Lisp_Object sym, tem;
794 sym = XCAR (args);
795 if (CONSP (Fcdr (XCDR (XCDR (args)))))
796 error ("Too many arguments");
798 tem = eval_sub (Fcar (XCDR (args)));
799 if (!NILP (Vpurify_flag))
800 tem = Fpurecopy (tem);
801 Fset_default (sym, tem);
802 XSYMBOL (sym)->declared_special = 1;
803 tem = Fcar (XCDR (XCDR (args)));
804 if (!NILP (tem))
806 if (!NILP (Vpurify_flag))
807 tem = Fpurecopy (tem);
808 Fput (sym, Qvariable_documentation, tem);
810 Fput (sym, Qrisky_local_variable, Qt);
811 LOADHIST_ATTACH (sym);
812 return sym;
815 /* Make SYMBOL lexically scoped. */
816 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
817 Smake_var_non_special, 1, 1, 0,
818 doc: /* Internal function. */)
819 (Lisp_Object symbol)
821 CHECK_SYMBOL (symbol);
822 XSYMBOL (symbol)->declared_special = 0;
823 return Qnil;
827 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
828 doc: /* Bind variables according to VARLIST then eval BODY.
829 The value of the last form in BODY is returned.
830 Each element of VARLIST is a symbol (which is bound to nil)
831 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
832 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
833 usage: (let* VARLIST BODY...) */)
834 (Lisp_Object args)
836 Lisp_Object varlist, var, val, elt, lexenv;
837 ptrdiff_t count = SPECPDL_INDEX ();
838 struct gcpro gcpro1, gcpro2, gcpro3;
840 GCPRO3 (args, elt, varlist);
842 lexenv = Vinternal_interpreter_environment;
844 varlist = XCAR (args);
845 while (CONSP (varlist))
847 QUIT;
849 elt = XCAR (varlist);
850 if (SYMBOLP (elt))
852 var = elt;
853 val = Qnil;
855 else if (! NILP (Fcdr (Fcdr (elt))))
856 signal_error ("`let' bindings can have only one value-form", elt);
857 else
859 var = Fcar (elt);
860 val = eval_sub (Fcar (Fcdr (elt)));
863 if (!NILP (lexenv) && SYMBOLP (var)
864 && !XSYMBOL (var)->declared_special
865 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
866 /* Lexically bind VAR by adding it to the interpreter's binding
867 alist. */
869 Lisp_Object newenv
870 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
871 if (EQ (Vinternal_interpreter_environment, lexenv))
872 /* Save the old lexical environment on the specpdl stack,
873 but only for the first lexical binding, since we'll never
874 need to revert to one of the intermediate ones. */
875 specbind (Qinternal_interpreter_environment, newenv);
876 else
877 Vinternal_interpreter_environment = newenv;
879 else
880 specbind (var, val);
882 varlist = XCDR (varlist);
884 UNGCPRO;
885 val = Fprogn (XCDR (args));
886 return unbind_to (count, val);
889 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
890 doc: /* Bind variables according to VARLIST then eval BODY.
891 The value of the last form in BODY is returned.
892 Each element of VARLIST is a symbol (which is bound to nil)
893 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
894 All the VALUEFORMs are evalled before any symbols are bound.
895 usage: (let VARLIST BODY...) */)
896 (Lisp_Object args)
898 Lisp_Object *temps, tem, lexenv;
899 register Lisp_Object elt, varlist;
900 ptrdiff_t count = SPECPDL_INDEX ();
901 ptrdiff_t argnum;
902 struct gcpro gcpro1, gcpro2;
903 USE_SAFE_ALLOCA;
905 varlist = XCAR (args);
907 /* Make space to hold the values to give the bound variables. */
908 elt = Flength (varlist);
909 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
911 /* Compute the values and store them in `temps'. */
913 GCPRO2 (args, *temps);
914 gcpro2.nvars = 0;
916 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
918 QUIT;
919 elt = XCAR (varlist);
920 if (SYMBOLP (elt))
921 temps [argnum++] = Qnil;
922 else if (! NILP (Fcdr (Fcdr (elt))))
923 signal_error ("`let' bindings can have only one value-form", elt);
924 else
925 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
926 gcpro2.nvars = argnum;
928 UNGCPRO;
930 lexenv = Vinternal_interpreter_environment;
932 varlist = XCAR (args);
933 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
935 Lisp_Object var;
937 elt = XCAR (varlist);
938 var = SYMBOLP (elt) ? elt : Fcar (elt);
939 tem = temps[argnum++];
941 if (!NILP (lexenv) && SYMBOLP (var)
942 && !XSYMBOL (var)->declared_special
943 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
944 /* Lexically bind VAR by adding it to the lexenv alist. */
945 lexenv = Fcons (Fcons (var, tem), lexenv);
946 else
947 /* Dynamically bind VAR. */
948 specbind (var, tem);
951 if (!EQ (lexenv, Vinternal_interpreter_environment))
952 /* Instantiate a new lexical environment. */
953 specbind (Qinternal_interpreter_environment, lexenv);
955 elt = Fprogn (XCDR (args));
956 SAFE_FREE ();
957 return unbind_to (count, elt);
960 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
961 doc: /* If TEST yields non-nil, eval BODY... and repeat.
962 The order of execution is thus TEST, BODY, TEST, BODY and so on
963 until TEST returns nil.
964 usage: (while TEST BODY...) */)
965 (Lisp_Object args)
967 Lisp_Object test, body;
968 struct gcpro gcpro1, gcpro2;
970 GCPRO2 (test, body);
972 test = XCAR (args);
973 body = XCDR (args);
974 while (!NILP (eval_sub (test)))
976 QUIT;
977 Fprogn (body);
980 UNGCPRO;
981 return Qnil;
984 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
985 doc: /* Return result of expanding macros at top level of FORM.
986 If FORM is not a macro call, it is returned unchanged.
987 Otherwise, the macro is expanded and the expansion is considered
988 in place of FORM. When a non-macro-call results, it is returned.
990 The second optional arg ENVIRONMENT specifies an environment of macro
991 definitions to shadow the loaded ones for use in file byte-compilation. */)
992 (Lisp_Object form, Lisp_Object environment)
994 /* With cleanups from Hallvard Furuseth. */
995 register Lisp_Object expander, sym, def, tem;
997 while (1)
999 /* Come back here each time we expand a macro call,
1000 in case it expands into another macro call. */
1001 if (!CONSP (form))
1002 break;
1003 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1004 def = sym = XCAR (form);
1005 tem = Qnil;
1006 /* Trace symbols aliases to other symbols
1007 until we get a symbol that is not an alias. */
1008 while (SYMBOLP (def))
1010 QUIT;
1011 sym = def;
1012 tem = Fassq (sym, environment);
1013 if (NILP (tem))
1015 def = XSYMBOL (sym)->function;
1016 if (!NILP (def))
1017 continue;
1019 break;
1021 /* Right now TEM is the result from SYM in ENVIRONMENT,
1022 and if TEM is nil then DEF is SYM's function definition. */
1023 if (NILP (tem))
1025 /* SYM is not mentioned in ENVIRONMENT.
1026 Look at its function definition. */
1027 struct gcpro gcpro1;
1028 GCPRO1 (form);
1029 def = Fautoload_do_load (def, sym, Qmacro);
1030 UNGCPRO;
1031 if (!CONSP (def))
1032 /* Not defined or definition not suitable. */
1033 break;
1034 if (!EQ (XCAR (def), Qmacro))
1035 break;
1036 else expander = XCDR (def);
1038 else
1040 expander = XCDR (tem);
1041 if (NILP (expander))
1042 break;
1045 Lisp_Object newform = apply1 (expander, XCDR (form));
1046 if (EQ (form, newform))
1047 break;
1048 else
1049 form = newform;
1052 return form;
1055 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1056 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1057 TAG is evalled to get the tag to use; it must not be nil.
1059 Then the BODY is executed.
1060 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1061 If no throw happens, `catch' returns the value of the last BODY form.
1062 If a throw happens, it specifies the value to return from `catch'.
1063 usage: (catch TAG BODY...) */)
1064 (Lisp_Object args)
1066 register Lisp_Object tag;
1067 struct gcpro gcpro1;
1069 GCPRO1 (args);
1070 tag = eval_sub (XCAR (args));
1071 UNGCPRO;
1072 return internal_catch (tag, Fprogn, XCDR (args));
1075 /* Assert that E is true, as a comment only. Use this instead of
1076 eassert (E) when E contains variables that might be clobbered by a
1077 longjmp. */
1079 #define clobbered_eassert(E) ((void) 0)
1081 /* Set up a catch, then call C function FUNC on argument ARG.
1082 FUNC should return a Lisp_Object.
1083 This is how catches are done from within C code. */
1085 Lisp_Object
1086 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1088 /* This structure is made part of the chain `catchlist'. */
1089 struct handler *c;
1091 /* Fill in the components of c, and put it on the list. */
1092 PUSH_HANDLER (c, tag, CATCHER);
1094 /* Call FUNC. */
1095 if (! sys_setjmp (c->jmp))
1097 Lisp_Object val = (*func) (arg);
1098 clobbered_eassert (handlerlist == c);
1099 handlerlist = handlerlist->next;
1100 return val;
1102 else
1103 { /* Throw works by a longjmp that comes right here. */
1104 Lisp_Object val = handlerlist->val;
1105 clobbered_eassert (handlerlist == c);
1106 handlerlist = handlerlist->next;
1107 return val;
1111 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1112 jump to that CATCH, returning VALUE as the value of that catch.
1114 This is the guts of Fthrow and Fsignal; they differ only in the way
1115 they choose the catch tag to throw to. A catch tag for a
1116 condition-case form has a TAG of Qnil.
1118 Before each catch is discarded, unbind all special bindings and
1119 execute all unwind-protect clauses made above that catch. Unwind
1120 the handler stack as we go, so that the proper handlers are in
1121 effect for each unwind-protect clause we run. At the end, restore
1122 some static info saved in CATCH, and longjmp to the location
1123 specified there.
1125 This is used for correct unwinding in Fthrow and Fsignal. */
1127 static _Noreturn void
1128 unwind_to_catch (struct handler *catch, Lisp_Object value)
1130 bool last_time;
1132 /* Save the value in the tag. */
1133 catch->val = value;
1135 /* Restore certain special C variables. */
1136 set_poll_suppress_count (catch->poll_suppress_count);
1137 unblock_input_to (catch->interrupt_input_blocked);
1138 immediate_quit = 0;
1142 /* Unwind the specpdl stack, and then restore the proper set of
1143 handlers. */
1144 unbind_to (handlerlist->pdlcount, Qnil);
1145 last_time = handlerlist == catch;
1146 if (! last_time)
1147 handlerlist = handlerlist->next;
1149 while (! last_time);
1151 eassert (handlerlist == catch);
1153 byte_stack_list = catch->byte_stack;
1154 gcprolist = catch->gcpro;
1155 #ifdef DEBUG_GCPRO
1156 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1157 #endif
1158 lisp_eval_depth = catch->lisp_eval_depth;
1160 sys_longjmp (catch->jmp, 1);
1163 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1164 doc: /* Throw to the catch for TAG and return VALUE from it.
1165 Both TAG and VALUE are evalled. */)
1166 (register Lisp_Object tag, Lisp_Object value)
1168 struct handler *c;
1170 if (!NILP (tag))
1171 for (c = handlerlist; c; c = c->next)
1173 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1174 unwind_to_catch (c, value);
1176 xsignal2 (Qno_catch, tag, value);
1180 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1181 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1182 If BODYFORM completes normally, its value is returned
1183 after executing the UNWINDFORMS.
1184 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1185 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1186 (Lisp_Object args)
1188 Lisp_Object val;
1189 ptrdiff_t count = SPECPDL_INDEX ();
1191 record_unwind_protect (unwind_body, XCDR (args));
1192 val = eval_sub (XCAR (args));
1193 return unbind_to (count, val);
1196 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1197 doc: /* Regain control when an error is signaled.
1198 Executes BODYFORM and returns its value if no error happens.
1199 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1200 where the BODY is made of Lisp expressions.
1202 A handler is applicable to an error
1203 if CONDITION-NAME is one of the error's condition names.
1204 If an error happens, the first applicable handler is run.
1206 The car of a handler may be a list of condition names instead of a
1207 single condition name; then it handles all of them. If the special
1208 condition name `debug' is present in this list, it allows another
1209 condition in the list to run the debugger if `debug-on-error' and the
1210 other usual mechanisms says it should (otherwise, `condition-case'
1211 suppresses the debugger).
1213 When a handler handles an error, control returns to the `condition-case'
1214 and it executes the handler's BODY...
1215 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1216 \(If VAR is nil, the handler can't access that information.)
1217 Then the value of the last BODY form is returned from the `condition-case'
1218 expression.
1220 See also the function `signal' for more info.
1221 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1222 (Lisp_Object args)
1224 Lisp_Object var = XCAR (args);
1225 Lisp_Object bodyform = XCAR (XCDR (args));
1226 Lisp_Object handlers = XCDR (XCDR (args));
1228 return internal_lisp_condition_case (var, bodyform, handlers);
1231 /* Like Fcondition_case, but the args are separate
1232 rather than passed in a list. Used by Fbyte_code. */
1234 Lisp_Object
1235 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1236 Lisp_Object handlers)
1238 Lisp_Object val;
1239 struct handler *c;
1240 struct handler *oldhandlerlist = handlerlist;
1241 int clausenb = 0;
1243 CHECK_SYMBOL (var);
1245 for (val = handlers; CONSP (val); val = XCDR (val))
1247 Lisp_Object tem = XCAR (val);
1248 clausenb++;
1249 if (! (NILP (tem)
1250 || (CONSP (tem)
1251 && (SYMBOLP (XCAR (tem))
1252 || CONSP (XCAR (tem))))))
1253 error ("Invalid condition handler: %s",
1254 SDATA (Fprin1_to_string (tem, Qt)));
1257 { /* The first clause is the one that should be checked first, so it should
1258 be added to handlerlist last. So we build in `clauses' a table that
1259 contains `handlers' but in reverse order. */
1260 Lisp_Object *clauses = alloca (clausenb * sizeof (Lisp_Object *));
1261 Lisp_Object *volatile clauses_volatile = clauses;
1262 int i = clausenb;
1263 for (val = handlers; CONSP (val); val = XCDR (val))
1264 clauses[--i] = XCAR (val);
1265 for (i = 0; i < clausenb; i++)
1267 Lisp_Object clause = clauses[i];
1268 Lisp_Object condition = XCAR (clause);
1269 if (!CONSP (condition))
1270 condition = Fcons (condition, Qnil);
1271 PUSH_HANDLER (c, condition, CONDITION_CASE);
1272 if (sys_setjmp (c->jmp))
1274 ptrdiff_t count = SPECPDL_INDEX ();
1275 Lisp_Object val = handlerlist->val;
1276 Lisp_Object *chosen_clause = clauses_volatile;
1277 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1278 chosen_clause++;
1279 handlerlist = oldhandlerlist;
1280 if (!NILP (var))
1282 if (!NILP (Vinternal_interpreter_environment))
1283 specbind (Qinternal_interpreter_environment,
1284 Fcons (Fcons (var, val),
1285 Vinternal_interpreter_environment));
1286 else
1287 specbind (var, val);
1289 val = Fprogn (XCDR (*chosen_clause));
1290 /* Note that this just undoes the binding of var; whoever
1291 longjumped to us unwound the stack to c.pdlcount before
1292 throwing. */
1293 if (!NILP (var))
1294 unbind_to (count, Qnil);
1295 return val;
1300 val = eval_sub (bodyform);
1301 handlerlist = oldhandlerlist;
1302 return val;
1305 /* Call the function BFUN with no arguments, catching errors within it
1306 according to HANDLERS. If there is an error, call HFUN with
1307 one argument which is the data that describes the error:
1308 (SIGNALNAME . DATA)
1310 HANDLERS can be a list of conditions to catch.
1311 If HANDLERS is Qt, catch all errors.
1312 If HANDLERS is Qerror, catch all errors
1313 but allow the debugger to run if that is enabled. */
1315 Lisp_Object
1316 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1317 Lisp_Object (*hfun) (Lisp_Object))
1319 Lisp_Object val;
1320 struct handler *c;
1322 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1323 if (sys_setjmp (c->jmp))
1325 Lisp_Object val = handlerlist->val;
1326 clobbered_eassert (handlerlist == c);
1327 handlerlist = handlerlist->next;
1328 return (*hfun) (val);
1331 val = (*bfun) ();
1332 clobbered_eassert (handlerlist == c);
1333 handlerlist = handlerlist->next;
1334 return val;
1337 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1339 Lisp_Object
1340 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1341 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1343 Lisp_Object val;
1344 struct handler *c;
1346 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1347 if (sys_setjmp (c->jmp))
1349 Lisp_Object val = handlerlist->val;
1350 clobbered_eassert (handlerlist == c);
1351 handlerlist = handlerlist->next;
1352 return (*hfun) (val);
1355 val = (*bfun) (arg);
1356 clobbered_eassert (handlerlist == c);
1357 handlerlist = handlerlist->next;
1358 return val;
1361 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1362 its arguments. */
1364 Lisp_Object
1365 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1366 Lisp_Object arg1,
1367 Lisp_Object arg2,
1368 Lisp_Object handlers,
1369 Lisp_Object (*hfun) (Lisp_Object))
1371 Lisp_Object val;
1372 struct handler *c;
1374 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1375 if (sys_setjmp (c->jmp))
1377 Lisp_Object val = handlerlist->val;
1378 clobbered_eassert (handlerlist == c);
1379 handlerlist = handlerlist->next;
1380 return (*hfun) (val);
1383 val = (*bfun) (arg1, arg2);
1384 clobbered_eassert (handlerlist == c);
1385 handlerlist = handlerlist->next;
1386 return val;
1389 /* Like internal_condition_case but call BFUN with NARGS as first,
1390 and ARGS as second argument. */
1392 Lisp_Object
1393 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1394 ptrdiff_t nargs,
1395 Lisp_Object *args,
1396 Lisp_Object handlers,
1397 Lisp_Object (*hfun) (Lisp_Object err,
1398 ptrdiff_t nargs,
1399 Lisp_Object *args))
1401 Lisp_Object val;
1402 struct handler *c;
1404 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1405 if (sys_setjmp (c->jmp))
1407 Lisp_Object val = handlerlist->val;
1408 clobbered_eassert (handlerlist == c);
1409 handlerlist = handlerlist->next;
1410 return (*hfun) (val, nargs, args);
1413 val = (*bfun) (nargs, args);
1414 clobbered_eassert (handlerlist == c);
1415 handlerlist = handlerlist->next;
1416 return val;
1420 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1421 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1422 Lisp_Object data);
1424 void
1425 process_quit_flag (void)
1427 Lisp_Object flag = Vquit_flag;
1428 Vquit_flag = Qnil;
1429 if (EQ (flag, Qkill_emacs))
1430 Fkill_emacs (Qnil);
1431 if (EQ (Vthrow_on_input, flag))
1432 Fthrow (Vthrow_on_input, Qt);
1433 Fsignal (Qquit, Qnil);
1436 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1437 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1438 This function does not return.
1440 An error symbol is a symbol with an `error-conditions' property
1441 that is a list of condition names.
1442 A handler for any of those names will get to handle this signal.
1443 The symbol `error' should normally be one of them.
1445 DATA should be a list. Its elements are printed as part of the error message.
1446 See Info anchor `(elisp)Definition of signal' for some details on how this
1447 error message is constructed.
1448 If the signal is handled, DATA is made available to the handler.
1449 See also the function `condition-case'. */)
1450 (Lisp_Object error_symbol, Lisp_Object data)
1452 /* When memory is full, ERROR-SYMBOL is nil,
1453 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1454 That is a special case--don't do this in other situations. */
1455 Lisp_Object conditions;
1456 Lisp_Object string;
1457 Lisp_Object real_error_symbol
1458 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1459 register Lisp_Object clause = Qnil;
1460 struct handler *h;
1462 immediate_quit = 0;
1463 abort_on_gc = 0;
1464 if (gc_in_progress || waiting_for_input)
1465 emacs_abort ();
1467 #if 0 /* rms: I don't know why this was here,
1468 but it is surely wrong for an error that is handled. */
1469 #ifdef HAVE_WINDOW_SYSTEM
1470 if (display_hourglass_p)
1471 cancel_hourglass ();
1472 #endif
1473 #endif
1475 /* This hook is used by edebug. */
1476 if (! NILP (Vsignal_hook_function)
1477 && ! NILP (error_symbol))
1479 /* Edebug takes care of restoring these variables when it exits. */
1480 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1481 max_lisp_eval_depth = lisp_eval_depth + 20;
1483 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1484 max_specpdl_size = SPECPDL_INDEX () + 40;
1486 call2 (Vsignal_hook_function, error_symbol, data);
1489 conditions = Fget (real_error_symbol, Qerror_conditions);
1491 /* Remember from where signal was called. Skip over the frame for
1492 `signal' itself. If a frame for `error' follows, skip that,
1493 too. Don't do this when ERROR_SYMBOL is nil, because that
1494 is a memory-full error. */
1495 Vsignaling_function = Qnil;
1496 if (!NILP (error_symbol))
1498 union specbinding *pdl = backtrace_next (backtrace_top ());
1499 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1500 pdl = backtrace_next (pdl);
1501 if (backtrace_p (pdl))
1502 Vsignaling_function = backtrace_function (pdl);
1505 for (h = handlerlist; h; h = h->next)
1507 if (h->type != CONDITION_CASE)
1508 continue;
1509 clause = find_handler_clause (h->tag_or_ch, conditions);
1510 if (!NILP (clause))
1511 break;
1514 if (/* Don't run the debugger for a memory-full error.
1515 (There is no room in memory to do that!) */
1516 !NILP (error_symbol)
1517 && (!NILP (Vdebug_on_signal)
1518 /* If no handler is present now, try to run the debugger. */
1519 || NILP (clause)
1520 /* A `debug' symbol in the handler list disables the normal
1521 suppression of the debugger. */
1522 || (CONSP (clause) && CONSP (XCAR (clause))
1523 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1524 /* Special handler that means "print a message and run debugger
1525 if requested". */
1526 || EQ (h->tag_or_ch, Qerror)))
1528 bool debugger_called
1529 = maybe_call_debugger (conditions, error_symbol, data);
1530 /* We can't return values to code which signaled an error, but we
1531 can continue code which has signaled a quit. */
1532 if (debugger_called && EQ (real_error_symbol, Qquit))
1533 return Qnil;
1536 if (!NILP (clause))
1538 Lisp_Object unwind_data
1539 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1541 unwind_to_catch (h, unwind_data);
1543 else
1545 if (handlerlist != 0)
1546 Fthrow (Qtop_level, Qt);
1549 if (! NILP (error_symbol))
1550 data = Fcons (error_symbol, data);
1552 string = Ferror_message_string (data);
1553 fatal ("%s", SDATA (string));
1556 /* Internal version of Fsignal that never returns.
1557 Used for anything but Qquit (which can return from Fsignal). */
1559 void
1560 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1562 Fsignal (error_symbol, data);
1563 emacs_abort ();
1566 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1568 void
1569 xsignal0 (Lisp_Object error_symbol)
1571 xsignal (error_symbol, Qnil);
1574 void
1575 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1577 xsignal (error_symbol, list1 (arg));
1580 void
1581 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1583 xsignal (error_symbol, list2 (arg1, arg2));
1586 void
1587 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1589 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1592 /* Signal `error' with message S, and additional arg ARG.
1593 If ARG is not a genuine list, make it a one-element list. */
1595 void
1596 signal_error (const char *s, Lisp_Object arg)
1598 Lisp_Object tortoise, hare;
1600 hare = tortoise = arg;
1601 while (CONSP (hare))
1603 hare = XCDR (hare);
1604 if (!CONSP (hare))
1605 break;
1607 hare = XCDR (hare);
1608 tortoise = XCDR (tortoise);
1610 if (EQ (hare, tortoise))
1611 break;
1614 if (!NILP (hare))
1615 arg = list1 (arg);
1617 xsignal (Qerror, Fcons (build_string (s), arg));
1621 /* Return true if LIST is a non-nil atom or
1622 a list containing one of CONDITIONS. */
1624 static bool
1625 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1627 if (NILP (list))
1628 return 0;
1629 if (! CONSP (list))
1630 return 1;
1632 while (CONSP (conditions))
1634 Lisp_Object this, tail;
1635 this = XCAR (conditions);
1636 for (tail = list; CONSP (tail); tail = XCDR (tail))
1637 if (EQ (XCAR (tail), this))
1638 return 1;
1639 conditions = XCDR (conditions);
1641 return 0;
1644 /* Return true if an error with condition-symbols CONDITIONS,
1645 and described by SIGNAL-DATA, should skip the debugger
1646 according to debugger-ignored-errors. */
1648 static bool
1649 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1651 Lisp_Object tail;
1652 bool first_string = 1;
1653 Lisp_Object error_message;
1655 error_message = Qnil;
1656 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1658 if (STRINGP (XCAR (tail)))
1660 if (first_string)
1662 error_message = Ferror_message_string (data);
1663 first_string = 0;
1666 if (fast_string_match (XCAR (tail), error_message) >= 0)
1667 return 1;
1669 else
1671 Lisp_Object contail;
1673 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1674 if (EQ (XCAR (tail), XCAR (contail)))
1675 return 1;
1679 return 0;
1682 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1683 SIG and DATA describe the signal. There are two ways to pass them:
1684 = SIG is the error symbol, and DATA is the rest of the data.
1685 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1686 This is for memory-full errors only. */
1687 static bool
1688 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1690 Lisp_Object combined_data;
1692 combined_data = Fcons (sig, data);
1694 if (
1695 /* Don't try to run the debugger with interrupts blocked.
1696 The editing loop would return anyway. */
1697 ! input_blocked_p ()
1698 && NILP (Vinhibit_debugger)
1699 /* Does user want to enter debugger for this kind of error? */
1700 && (EQ (sig, Qquit)
1701 ? debug_on_quit
1702 : wants_debugger (Vdebug_on_error, conditions))
1703 && ! skip_debugger (conditions, combined_data)
1704 /* RMS: What's this for? */
1705 && when_entered_debugger < num_nonmacro_input_events)
1707 call_debugger (list2 (Qerror, combined_data));
1708 return 1;
1711 return 0;
1714 static Lisp_Object
1715 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1717 register Lisp_Object h;
1719 /* t is used by handlers for all conditions, set up by C code. */
1720 if (EQ (handlers, Qt))
1721 return Qt;
1723 /* error is used similarly, but means print an error message
1724 and run the debugger if that is enabled. */
1725 if (EQ (handlers, Qerror))
1726 return Qt;
1728 for (h = handlers; CONSP (h); h = XCDR (h))
1730 Lisp_Object handler = XCAR (h);
1731 if (!NILP (Fmemq (handler, conditions)))
1732 return handlers;
1735 return Qnil;
1739 /* Dump an error message; called like vprintf. */
1740 void
1741 verror (const char *m, va_list ap)
1743 char buf[4000];
1744 ptrdiff_t size = sizeof buf;
1745 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1746 char *buffer = buf;
1747 ptrdiff_t used;
1748 Lisp_Object string;
1750 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1751 string = make_string (buffer, used);
1752 if (buffer != buf)
1753 xfree (buffer);
1755 xsignal1 (Qerror, string);
1759 /* Dump an error message; called like printf. */
1761 /* VARARGS 1 */
1762 void
1763 error (const char *m, ...)
1765 va_list ap;
1766 va_start (ap, m);
1767 verror (m, ap);
1770 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1771 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1772 This means it contains a description for how to read arguments to give it.
1773 The value is nil for an invalid function or a symbol with no function
1774 definition.
1776 Interactively callable functions include strings and vectors (treated
1777 as keyboard macros), lambda-expressions that contain a top-level call
1778 to `interactive', autoload definitions made by `autoload' with non-nil
1779 fourth argument, and some of the built-in functions of Lisp.
1781 Also, a symbol satisfies `commandp' if its function definition does so.
1783 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1784 then strings and vectors are not accepted. */)
1785 (Lisp_Object function, Lisp_Object for_call_interactively)
1787 register Lisp_Object fun;
1788 register Lisp_Object funcar;
1789 Lisp_Object if_prop = Qnil;
1791 fun = function;
1793 fun = indirect_function (fun); /* Check cycles. */
1794 if (NILP (fun))
1795 return Qnil;
1797 /* Check an `interactive-form' property if present, analogous to the
1798 function-documentation property. */
1799 fun = function;
1800 while (SYMBOLP (fun))
1802 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1803 if (!NILP (tmp))
1804 if_prop = Qt;
1805 fun = Fsymbol_function (fun);
1808 /* Emacs primitives are interactive if their DEFUN specifies an
1809 interactive spec. */
1810 if (SUBRP (fun))
1811 return XSUBR (fun)->intspec ? Qt : if_prop;
1813 /* Bytecode objects are interactive if they are long enough to
1814 have an element whose index is COMPILED_INTERACTIVE, which is
1815 where the interactive spec is stored. */
1816 else if (COMPILEDP (fun))
1817 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1818 ? Qt : if_prop);
1820 /* Strings and vectors are keyboard macros. */
1821 if (STRINGP (fun) || VECTORP (fun))
1822 return (NILP (for_call_interactively) ? Qt : Qnil);
1824 /* Lists may represent commands. */
1825 if (!CONSP (fun))
1826 return Qnil;
1827 funcar = XCAR (fun);
1828 if (EQ (funcar, Qclosure))
1829 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1830 ? Qt : if_prop);
1831 else if (EQ (funcar, Qlambda))
1832 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1833 else if (EQ (funcar, Qautoload))
1834 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1835 else
1836 return Qnil;
1839 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1840 doc: /* Define FUNCTION to autoload from FILE.
1841 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1842 Third arg DOCSTRING is documentation for the function.
1843 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1844 Fifth arg TYPE indicates the type of the object:
1845 nil or omitted says FUNCTION is a function,
1846 `keymap' says FUNCTION is really a keymap, and
1847 `macro' or t says FUNCTION is really a macro.
1848 Third through fifth args give info about the real definition.
1849 They default to nil.
1850 If FUNCTION is already defined other than as an autoload,
1851 this does nothing and returns nil. */)
1852 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1854 CHECK_SYMBOL (function);
1855 CHECK_STRING (file);
1857 /* If function is defined and not as an autoload, don't override. */
1858 if (!NILP (XSYMBOL (function)->function)
1859 && !AUTOLOADP (XSYMBOL (function)->function))
1860 return Qnil;
1862 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1863 /* `read1' in lread.c has found the docstring starting with "\
1864 and assumed the docstring will be provided by Snarf-documentation, so it
1865 passed us 0 instead. But that leads to accidental sharing in purecopy's
1866 hash-consing, so we use a (hopefully) unique integer instead. */
1867 docstring = make_number (XHASH (function));
1868 return Fdefalias (function,
1869 list5 (Qautoload, file, docstring, interactive, type),
1870 Qnil);
1873 void
1874 un_autoload (Lisp_Object oldqueue)
1876 Lisp_Object queue, first, second;
1878 /* Queue to unwind is current value of Vautoload_queue.
1879 oldqueue is the shadowed value to leave in Vautoload_queue. */
1880 queue = Vautoload_queue;
1881 Vautoload_queue = oldqueue;
1882 while (CONSP (queue))
1884 first = XCAR (queue);
1885 second = Fcdr (first);
1886 first = Fcar (first);
1887 if (EQ (first, make_number (0)))
1888 Vfeatures = second;
1889 else
1890 Ffset (first, second);
1891 queue = XCDR (queue);
1895 /* Load an autoloaded function.
1896 FUNNAME is the symbol which is the function's name.
1897 FUNDEF is the autoload definition (a list). */
1899 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1900 doc: /* Load FUNDEF which should be an autoload.
1901 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1902 in which case the function returns the new autoloaded function value.
1903 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1904 it is defines a macro. */)
1905 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1907 ptrdiff_t count = SPECPDL_INDEX ();
1908 struct gcpro gcpro1, gcpro2, gcpro3;
1910 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1911 return fundef;
1913 if (EQ (macro_only, Qmacro))
1915 Lisp_Object kind = Fnth (make_number (4), fundef);
1916 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1917 return fundef;
1920 /* This is to make sure that loadup.el gives a clear picture
1921 of what files are preloaded and when. */
1922 if (! NILP (Vpurify_flag))
1923 error ("Attempt to autoload %s while preparing to dump",
1924 SDATA (SYMBOL_NAME (funname)));
1926 CHECK_SYMBOL (funname);
1927 GCPRO3 (funname, fundef, macro_only);
1929 /* Preserve the match data. */
1930 record_unwind_save_match_data ();
1932 /* If autoloading gets an error (which includes the error of failing
1933 to define the function being called), we use Vautoload_queue
1934 to undo function definitions and `provide' calls made by
1935 the function. We do this in the specific case of autoloading
1936 because autoloading is not an explicit request "load this file",
1937 but rather a request to "call this function".
1939 The value saved here is to be restored into Vautoload_queue. */
1940 record_unwind_protect (un_autoload, Vautoload_queue);
1941 Vautoload_queue = Qt;
1942 /* If `macro_only', assume this autoload to be a "best-effort",
1943 so don't signal an error if autoloading fails. */
1944 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1946 /* Once loading finishes, don't undo it. */
1947 Vautoload_queue = Qt;
1948 unbind_to (count, Qnil);
1950 UNGCPRO;
1952 if (NILP (funname))
1953 return Qnil;
1954 else
1956 Lisp_Object fun = Findirect_function (funname, Qnil);
1958 if (!NILP (Fequal (fun, fundef)))
1959 error ("Autoloading failed to define function %s",
1960 SDATA (SYMBOL_NAME (funname)));
1961 else
1962 return fun;
1967 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1968 doc: /* Evaluate FORM and return its value.
1969 If LEXICAL is t, evaluate using lexical scoping.
1970 LEXICAL can also be an actual lexical environment, in the form of an
1971 alist mapping symbols to their value. */)
1972 (Lisp_Object form, Lisp_Object lexical)
1974 ptrdiff_t count = SPECPDL_INDEX ();
1975 specbind (Qinternal_interpreter_environment,
1976 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1977 return unbind_to (count, eval_sub (form));
1980 /* Grow the specpdl stack by one entry.
1981 The caller should have already initialized the entry.
1982 Signal an error on stack overflow.
1984 Make sure that there is always one unused entry past the top of the
1985 stack, so that the just-initialized entry is safely unwound if
1986 memory exhausted and an error is signaled here. Also, allocate a
1987 never-used entry just before the bottom of the stack; sometimes its
1988 address is taken. */
1990 static void
1991 grow_specpdl (void)
1993 specpdl_ptr++;
1995 if (specpdl_ptr == specpdl + specpdl_size)
1997 ptrdiff_t count = SPECPDL_INDEX ();
1998 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
1999 union specbinding *pdlvec = specpdl - 1;
2000 ptrdiff_t pdlvecsize = specpdl_size + 1;
2001 if (max_size <= specpdl_size)
2003 if (max_specpdl_size < 400)
2004 max_size = max_specpdl_size = 400;
2005 if (max_size <= specpdl_size)
2006 signal_error ("Variable binding depth exceeds max-specpdl-size",
2007 Qnil);
2009 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2010 specpdl = pdlvec + 1;
2011 specpdl_size = pdlvecsize - 1;
2012 specpdl_ptr = specpdl + count;
2016 void
2017 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2019 eassert (nargs >= UNEVALLED);
2020 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2021 specpdl_ptr->bt.debug_on_exit = false;
2022 specpdl_ptr->bt.function = function;
2023 specpdl_ptr->bt.args = args;
2024 specpdl_ptr->bt.nargs = nargs;
2025 grow_specpdl ();
2028 /* Eval a sub-expression of the current expression (i.e. in the same
2029 lexical scope). */
2030 Lisp_Object
2031 eval_sub (Lisp_Object form)
2033 Lisp_Object fun, val, original_fun, original_args;
2034 Lisp_Object funcar;
2035 struct gcpro gcpro1, gcpro2, gcpro3;
2037 if (SYMBOLP (form))
2039 /* Look up its binding in the lexical environment.
2040 We do not pay attention to the declared_special flag here, since we
2041 already did that when let-binding the variable. */
2042 Lisp_Object lex_binding
2043 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2044 ? Fassq (form, Vinternal_interpreter_environment)
2045 : Qnil;
2046 if (CONSP (lex_binding))
2047 return XCDR (lex_binding);
2048 else
2049 return Fsymbol_value (form);
2052 if (!CONSP (form))
2053 return form;
2055 QUIT;
2057 GCPRO1 (form);
2058 maybe_gc ();
2059 UNGCPRO;
2061 if (++lisp_eval_depth > max_lisp_eval_depth)
2063 if (max_lisp_eval_depth < 100)
2064 max_lisp_eval_depth = 100;
2065 if (lisp_eval_depth > max_lisp_eval_depth)
2066 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2069 original_fun = XCAR (form);
2070 original_args = XCDR (form);
2072 /* This also protects them from gc. */
2073 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2075 if (debug_on_next_call)
2076 do_debug_on_call (Qt);
2078 /* At this point, only original_fun and original_args
2079 have values that will be used below. */
2080 retry:
2082 /* Optimize for no indirection. */
2083 fun = original_fun;
2084 if (!SYMBOLP (fun))
2085 fun = Ffunction (Fcons (fun, Qnil));
2086 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2087 fun = indirect_function (fun);
2089 if (SUBRP (fun))
2091 Lisp_Object numargs;
2092 Lisp_Object argvals[8];
2093 Lisp_Object args_left;
2094 register int i, maxargs;
2096 args_left = original_args;
2097 numargs = Flength (args_left);
2099 check_cons_list ();
2101 if (XINT (numargs) < XSUBR (fun)->min_args
2102 || (XSUBR (fun)->max_args >= 0
2103 && XSUBR (fun)->max_args < XINT (numargs)))
2104 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2106 else if (XSUBR (fun)->max_args == UNEVALLED)
2107 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2108 else if (XSUBR (fun)->max_args == MANY)
2110 /* Pass a vector of evaluated arguments. */
2111 Lisp_Object *vals;
2112 ptrdiff_t argnum = 0;
2113 USE_SAFE_ALLOCA;
2115 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2117 GCPRO3 (args_left, fun, fun);
2118 gcpro3.var = vals;
2119 gcpro3.nvars = 0;
2121 while (!NILP (args_left))
2123 vals[argnum++] = eval_sub (Fcar (args_left));
2124 args_left = Fcdr (args_left);
2125 gcpro3.nvars = argnum;
2128 set_backtrace_args (specpdl_ptr - 1, vals);
2129 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2131 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2132 UNGCPRO;
2133 SAFE_FREE ();
2135 else
2137 GCPRO3 (args_left, fun, fun);
2138 gcpro3.var = argvals;
2139 gcpro3.nvars = 0;
2141 maxargs = XSUBR (fun)->max_args;
2142 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2144 argvals[i] = eval_sub (Fcar (args_left));
2145 gcpro3.nvars = ++i;
2148 UNGCPRO;
2150 set_backtrace_args (specpdl_ptr - 1, argvals);
2151 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2153 switch (i)
2155 case 0:
2156 val = (XSUBR (fun)->function.a0 ());
2157 break;
2158 case 1:
2159 val = (XSUBR (fun)->function.a1 (argvals[0]));
2160 break;
2161 case 2:
2162 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2163 break;
2164 case 3:
2165 val = (XSUBR (fun)->function.a3
2166 (argvals[0], argvals[1], argvals[2]));
2167 break;
2168 case 4:
2169 val = (XSUBR (fun)->function.a4
2170 (argvals[0], argvals[1], argvals[2], argvals[3]));
2171 break;
2172 case 5:
2173 val = (XSUBR (fun)->function.a5
2174 (argvals[0], argvals[1], argvals[2], argvals[3],
2175 argvals[4]));
2176 break;
2177 case 6:
2178 val = (XSUBR (fun)->function.a6
2179 (argvals[0], argvals[1], argvals[2], argvals[3],
2180 argvals[4], argvals[5]));
2181 break;
2182 case 7:
2183 val = (XSUBR (fun)->function.a7
2184 (argvals[0], argvals[1], argvals[2], argvals[3],
2185 argvals[4], argvals[5], argvals[6]));
2186 break;
2188 case 8:
2189 val = (XSUBR (fun)->function.a8
2190 (argvals[0], argvals[1], argvals[2], argvals[3],
2191 argvals[4], argvals[5], argvals[6], argvals[7]));
2192 break;
2194 default:
2195 /* Someone has created a subr that takes more arguments than
2196 is supported by this code. We need to either rewrite the
2197 subr to use a different argument protocol, or add more
2198 cases to this switch. */
2199 emacs_abort ();
2203 else if (COMPILEDP (fun))
2204 val = apply_lambda (fun, original_args);
2205 else
2207 if (NILP (fun))
2208 xsignal1 (Qvoid_function, original_fun);
2209 if (!CONSP (fun))
2210 xsignal1 (Qinvalid_function, original_fun);
2211 funcar = XCAR (fun);
2212 if (!SYMBOLP (funcar))
2213 xsignal1 (Qinvalid_function, original_fun);
2214 if (EQ (funcar, Qautoload))
2216 Fautoload_do_load (fun, original_fun, Qnil);
2217 goto retry;
2219 if (EQ (funcar, Qmacro))
2221 ptrdiff_t count = SPECPDL_INDEX ();
2222 Lisp_Object exp;
2223 /* Bind lexical-binding during expansion of the macro, so the
2224 macro can know reliably if the code it outputs will be
2225 interpreted using lexical-binding or not. */
2226 specbind (Qlexical_binding,
2227 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2228 exp = apply1 (Fcdr (fun), original_args);
2229 unbind_to (count, Qnil);
2230 val = eval_sub (exp);
2232 else if (EQ (funcar, Qlambda)
2233 || EQ (funcar, Qclosure))
2234 val = apply_lambda (fun, original_args);
2235 else
2236 xsignal1 (Qinvalid_function, original_fun);
2238 check_cons_list ();
2240 lisp_eval_depth--;
2241 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2242 val = call_debugger (list2 (Qexit, val));
2243 specpdl_ptr--;
2245 return val;
2248 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2249 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2250 Then return the value FUNCTION returns.
2251 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2252 usage: (apply FUNCTION &rest ARGUMENTS) */)
2253 (ptrdiff_t nargs, Lisp_Object *args)
2255 ptrdiff_t i;
2256 EMACS_INT numargs;
2257 register Lisp_Object spread_arg;
2258 register Lisp_Object *funcall_args;
2259 Lisp_Object fun, retval;
2260 struct gcpro gcpro1;
2261 USE_SAFE_ALLOCA;
2263 fun = args [0];
2264 funcall_args = 0;
2265 spread_arg = args [nargs - 1];
2266 CHECK_LIST (spread_arg);
2268 numargs = XINT (Flength (spread_arg));
2270 if (numargs == 0)
2271 return Ffuncall (nargs - 1, args);
2272 else if (numargs == 1)
2274 args [nargs - 1] = XCAR (spread_arg);
2275 return Ffuncall (nargs, args);
2278 numargs += nargs - 2;
2280 /* Optimize for no indirection. */
2281 if (SYMBOLP (fun) && !NILP (fun)
2282 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2283 fun = indirect_function (fun);
2284 if (NILP (fun))
2286 /* Let funcall get the error. */
2287 fun = args[0];
2288 goto funcall;
2291 if (SUBRP (fun))
2293 if (numargs < XSUBR (fun)->min_args
2294 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2295 goto funcall; /* Let funcall get the error. */
2296 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2298 /* Avoid making funcall cons up a yet another new vector of arguments
2299 by explicitly supplying nil's for optional values. */
2300 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2301 for (i = numargs; i < XSUBR (fun)->max_args;)
2302 funcall_args[++i] = Qnil;
2303 GCPRO1 (*funcall_args);
2304 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2307 funcall:
2308 /* We add 1 to numargs because funcall_args includes the
2309 function itself as well as its arguments. */
2310 if (!funcall_args)
2312 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2313 GCPRO1 (*funcall_args);
2314 gcpro1.nvars = 1 + numargs;
2317 memcpy (funcall_args, args, nargs * word_size);
2318 /* Spread the last arg we got. Its first element goes in
2319 the slot that it used to occupy, hence this value of I. */
2320 i = nargs - 1;
2321 while (!NILP (spread_arg))
2323 funcall_args [i++] = XCAR (spread_arg);
2324 spread_arg = XCDR (spread_arg);
2327 /* By convention, the caller needs to gcpro Ffuncall's args. */
2328 retval = Ffuncall (gcpro1.nvars, funcall_args);
2329 UNGCPRO;
2330 SAFE_FREE ();
2332 return retval;
2335 /* Run hook variables in various ways. */
2337 static Lisp_Object
2338 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2340 Ffuncall (nargs, args);
2341 return Qnil;
2344 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2345 doc: /* Run each hook in HOOKS.
2346 Each argument should be a symbol, a hook variable.
2347 These symbols are processed in the order specified.
2348 If a hook symbol has a non-nil value, that value may be a function
2349 or a list of functions to be called to run the hook.
2350 If the value is a function, it is called with no arguments.
2351 If it is a list, the elements are called, in order, with no arguments.
2353 Major modes should not use this function directly to run their mode
2354 hook; they should use `run-mode-hooks' instead.
2356 Do not use `make-local-variable' to make a hook variable buffer-local.
2357 Instead, use `add-hook' and specify t for the LOCAL argument.
2358 usage: (run-hooks &rest HOOKS) */)
2359 (ptrdiff_t nargs, Lisp_Object *args)
2361 Lisp_Object hook[1];
2362 ptrdiff_t i;
2364 for (i = 0; i < nargs; i++)
2366 hook[0] = args[i];
2367 run_hook_with_args (1, hook, funcall_nil);
2370 return Qnil;
2373 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2374 Srun_hook_with_args, 1, MANY, 0,
2375 doc: /* Run HOOK with the specified arguments ARGS.
2376 HOOK should be a symbol, a hook variable. The value of HOOK
2377 may be nil, a function, or a list of functions. Call each
2378 function in order with arguments ARGS. The final return value
2379 is unspecified.
2381 Do not use `make-local-variable' to make a hook variable buffer-local.
2382 Instead, use `add-hook' and specify t for the LOCAL argument.
2383 usage: (run-hook-with-args HOOK &rest ARGS) */)
2384 (ptrdiff_t nargs, Lisp_Object *args)
2386 return run_hook_with_args (nargs, args, funcall_nil);
2389 /* NB this one still documents a specific non-nil return value.
2390 (As did run-hook-with-args and run-hook-with-args-until-failure
2391 until they were changed in 24.1.) */
2392 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2393 Srun_hook_with_args_until_success, 1, MANY, 0,
2394 doc: /* Run HOOK with the specified arguments ARGS.
2395 HOOK should be a symbol, a hook variable. The value of HOOK
2396 may be nil, a function, or a list of functions. Call each
2397 function in order with arguments ARGS, stopping at the first
2398 one that returns non-nil, and return that value. Otherwise (if
2399 all functions return nil, or if there are no functions to call),
2400 return nil.
2402 Do not use `make-local-variable' to make a hook variable buffer-local.
2403 Instead, use `add-hook' and specify t for the LOCAL argument.
2404 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2405 (ptrdiff_t nargs, Lisp_Object *args)
2407 return run_hook_with_args (nargs, args, Ffuncall);
2410 static Lisp_Object
2411 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2413 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2416 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2417 Srun_hook_with_args_until_failure, 1, MANY, 0,
2418 doc: /* Run HOOK with the specified arguments ARGS.
2419 HOOK should be a symbol, a hook variable. The value of HOOK
2420 may be nil, a function, or a list of functions. Call each
2421 function in order with arguments ARGS, stopping at the first
2422 one that returns nil, and return nil. Otherwise (if all functions
2423 return non-nil, or if there are no functions to call), return non-nil
2424 \(do not rely on the precise return value in this case).
2426 Do not use `make-local-variable' to make a hook variable buffer-local.
2427 Instead, use `add-hook' and specify t for the LOCAL argument.
2428 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2429 (ptrdiff_t nargs, Lisp_Object *args)
2431 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2434 static Lisp_Object
2435 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2437 Lisp_Object tmp = args[0], ret;
2438 args[0] = args[1];
2439 args[1] = tmp;
2440 ret = Ffuncall (nargs, args);
2441 args[1] = args[0];
2442 args[0] = tmp;
2443 return ret;
2446 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2447 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2448 I.e. instead of calling each function FUN directly with arguments ARGS,
2449 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2450 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2451 aborts and returns that value.
2452 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2453 (ptrdiff_t nargs, Lisp_Object *args)
2455 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2458 /* ARGS[0] should be a hook symbol.
2459 Call each of the functions in the hook value, passing each of them
2460 as arguments all the rest of ARGS (all NARGS - 1 elements).
2461 FUNCALL specifies how to call each function on the hook.
2462 The caller (or its caller, etc) must gcpro all of ARGS,
2463 except that it isn't necessary to gcpro ARGS[0]. */
2465 Lisp_Object
2466 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2467 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2469 Lisp_Object sym, val, ret = Qnil;
2470 struct gcpro gcpro1, gcpro2, gcpro3;
2472 /* If we are dying or still initializing,
2473 don't do anything--it would probably crash if we tried. */
2474 if (NILP (Vrun_hooks))
2475 return Qnil;
2477 sym = args[0];
2478 val = find_symbol_value (sym);
2480 if (EQ (val, Qunbound) || NILP (val))
2481 return ret;
2482 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2484 args[0] = val;
2485 return funcall (nargs, args);
2487 else
2489 Lisp_Object global_vals = Qnil;
2490 GCPRO3 (sym, val, global_vals);
2492 for (;
2493 CONSP (val) && NILP (ret);
2494 val = XCDR (val))
2496 if (EQ (XCAR (val), Qt))
2498 /* t indicates this hook has a local binding;
2499 it means to run the global binding too. */
2500 global_vals = Fdefault_value (sym);
2501 if (NILP (global_vals)) continue;
2503 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2505 args[0] = global_vals;
2506 ret = funcall (nargs, args);
2508 else
2510 for (;
2511 CONSP (global_vals) && NILP (ret);
2512 global_vals = XCDR (global_vals))
2514 args[0] = XCAR (global_vals);
2515 /* In a global value, t should not occur. If it does, we
2516 must ignore it to avoid an endless loop. */
2517 if (!EQ (args[0], Qt))
2518 ret = funcall (nargs, args);
2522 else
2524 args[0] = XCAR (val);
2525 ret = funcall (nargs, args);
2529 UNGCPRO;
2530 return ret;
2534 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2536 void
2537 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2539 Lisp_Object temp[3];
2540 temp[0] = hook;
2541 temp[1] = arg1;
2542 temp[2] = arg2;
2544 Frun_hook_with_args (3, temp);
2547 /* Apply fn to arg. */
2548 Lisp_Object
2549 apply1 (Lisp_Object fn, Lisp_Object arg)
2551 struct gcpro gcpro1;
2553 GCPRO1 (fn);
2554 if (NILP (arg))
2555 RETURN_UNGCPRO (Ffuncall (1, &fn));
2556 gcpro1.nvars = 2;
2558 Lisp_Object args[2];
2559 args[0] = fn;
2560 args[1] = arg;
2561 gcpro1.var = args;
2562 RETURN_UNGCPRO (Fapply (2, args));
2566 /* Call function fn on no arguments. */
2567 Lisp_Object
2568 call0 (Lisp_Object fn)
2570 struct gcpro gcpro1;
2572 GCPRO1 (fn);
2573 RETURN_UNGCPRO (Ffuncall (1, &fn));
2576 /* Call function fn with 1 argument arg1. */
2577 /* ARGSUSED */
2578 Lisp_Object
2579 call1 (Lisp_Object fn, Lisp_Object arg1)
2581 struct gcpro gcpro1;
2582 Lisp_Object args[2];
2584 args[0] = fn;
2585 args[1] = arg1;
2586 GCPRO1 (args[0]);
2587 gcpro1.nvars = 2;
2588 RETURN_UNGCPRO (Ffuncall (2, args));
2591 /* Call function fn with 2 arguments arg1, arg2. */
2592 /* ARGSUSED */
2593 Lisp_Object
2594 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2596 struct gcpro gcpro1;
2597 Lisp_Object args[3];
2598 args[0] = fn;
2599 args[1] = arg1;
2600 args[2] = arg2;
2601 GCPRO1 (args[0]);
2602 gcpro1.nvars = 3;
2603 RETURN_UNGCPRO (Ffuncall (3, args));
2606 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2607 /* ARGSUSED */
2608 Lisp_Object
2609 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2611 struct gcpro gcpro1;
2612 Lisp_Object args[4];
2613 args[0] = fn;
2614 args[1] = arg1;
2615 args[2] = arg2;
2616 args[3] = arg3;
2617 GCPRO1 (args[0]);
2618 gcpro1.nvars = 4;
2619 RETURN_UNGCPRO (Ffuncall (4, args));
2622 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2623 /* ARGSUSED */
2624 Lisp_Object
2625 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2626 Lisp_Object arg4)
2628 struct gcpro gcpro1;
2629 Lisp_Object args[5];
2630 args[0] = fn;
2631 args[1] = arg1;
2632 args[2] = arg2;
2633 args[3] = arg3;
2634 args[4] = arg4;
2635 GCPRO1 (args[0]);
2636 gcpro1.nvars = 5;
2637 RETURN_UNGCPRO (Ffuncall (5, args));
2640 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2641 /* ARGSUSED */
2642 Lisp_Object
2643 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2644 Lisp_Object arg4, Lisp_Object arg5)
2646 struct gcpro gcpro1;
2647 Lisp_Object args[6];
2648 args[0] = fn;
2649 args[1] = arg1;
2650 args[2] = arg2;
2651 args[3] = arg3;
2652 args[4] = arg4;
2653 args[5] = arg5;
2654 GCPRO1 (args[0]);
2655 gcpro1.nvars = 6;
2656 RETURN_UNGCPRO (Ffuncall (6, args));
2659 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2660 /* ARGSUSED */
2661 Lisp_Object
2662 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2663 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2665 struct gcpro gcpro1;
2666 Lisp_Object args[7];
2667 args[0] = fn;
2668 args[1] = arg1;
2669 args[2] = arg2;
2670 args[3] = arg3;
2671 args[4] = arg4;
2672 args[5] = arg5;
2673 args[6] = arg6;
2674 GCPRO1 (args[0]);
2675 gcpro1.nvars = 7;
2676 RETURN_UNGCPRO (Ffuncall (7, args));
2679 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2680 /* ARGSUSED */
2681 Lisp_Object
2682 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2683 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2685 struct gcpro gcpro1;
2686 Lisp_Object args[8];
2687 args[0] = fn;
2688 args[1] = arg1;
2689 args[2] = arg2;
2690 args[3] = arg3;
2691 args[4] = arg4;
2692 args[5] = arg5;
2693 args[6] = arg6;
2694 args[7] = arg7;
2695 GCPRO1 (args[0]);
2696 gcpro1.nvars = 8;
2697 RETURN_UNGCPRO (Ffuncall (8, args));
2700 /* The caller should GCPRO all the elements of ARGS. */
2702 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2703 doc: /* Non-nil if OBJECT is a function. */)
2704 (Lisp_Object object)
2706 if (FUNCTIONP (object))
2707 return Qt;
2708 return Qnil;
2711 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2712 doc: /* Call first argument as a function, passing remaining arguments to it.
2713 Return the value that function returns.
2714 Thus, (funcall 'cons 'x 'y) returns (x . y).
2715 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2716 (ptrdiff_t nargs, Lisp_Object *args)
2718 Lisp_Object fun, original_fun;
2719 Lisp_Object funcar;
2720 ptrdiff_t numargs = nargs - 1;
2721 Lisp_Object lisp_numargs;
2722 Lisp_Object val;
2723 register Lisp_Object *internal_args;
2724 ptrdiff_t i;
2726 QUIT;
2728 if (++lisp_eval_depth > max_lisp_eval_depth)
2730 if (max_lisp_eval_depth < 100)
2731 max_lisp_eval_depth = 100;
2732 if (lisp_eval_depth > max_lisp_eval_depth)
2733 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2736 /* This also GCPROs them. */
2737 record_in_backtrace (args[0], &args[1], nargs - 1);
2739 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2740 maybe_gc ();
2742 if (debug_on_next_call)
2743 do_debug_on_call (Qlambda);
2745 check_cons_list ();
2747 original_fun = args[0];
2749 retry:
2751 /* Optimize for no indirection. */
2752 fun = original_fun;
2753 if (SYMBOLP (fun) && !NILP (fun)
2754 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2755 fun = indirect_function (fun);
2757 if (SUBRP (fun))
2759 if (numargs < XSUBR (fun)->min_args
2760 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2762 XSETFASTINT (lisp_numargs, numargs);
2763 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2766 else if (XSUBR (fun)->max_args == UNEVALLED)
2767 xsignal1 (Qinvalid_function, original_fun);
2769 else if (XSUBR (fun)->max_args == MANY)
2770 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2771 else
2773 if (XSUBR (fun)->max_args > numargs)
2775 internal_args = alloca (XSUBR (fun)->max_args
2776 * sizeof *internal_args);
2777 memcpy (internal_args, args + 1, numargs * word_size);
2778 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2779 internal_args[i] = Qnil;
2781 else
2782 internal_args = args + 1;
2783 switch (XSUBR (fun)->max_args)
2785 case 0:
2786 val = (XSUBR (fun)->function.a0 ());
2787 break;
2788 case 1:
2789 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2790 break;
2791 case 2:
2792 val = (XSUBR (fun)->function.a2
2793 (internal_args[0], internal_args[1]));
2794 break;
2795 case 3:
2796 val = (XSUBR (fun)->function.a3
2797 (internal_args[0], internal_args[1], internal_args[2]));
2798 break;
2799 case 4:
2800 val = (XSUBR (fun)->function.a4
2801 (internal_args[0], internal_args[1], internal_args[2],
2802 internal_args[3]));
2803 break;
2804 case 5:
2805 val = (XSUBR (fun)->function.a5
2806 (internal_args[0], internal_args[1], internal_args[2],
2807 internal_args[3], internal_args[4]));
2808 break;
2809 case 6:
2810 val = (XSUBR (fun)->function.a6
2811 (internal_args[0], internal_args[1], internal_args[2],
2812 internal_args[3], internal_args[4], internal_args[5]));
2813 break;
2814 case 7:
2815 val = (XSUBR (fun)->function.a7
2816 (internal_args[0], internal_args[1], internal_args[2],
2817 internal_args[3], internal_args[4], internal_args[5],
2818 internal_args[6]));
2819 break;
2821 case 8:
2822 val = (XSUBR (fun)->function.a8
2823 (internal_args[0], internal_args[1], internal_args[2],
2824 internal_args[3], internal_args[4], internal_args[5],
2825 internal_args[6], internal_args[7]));
2826 break;
2828 default:
2830 /* If a subr takes more than 8 arguments without using MANY
2831 or UNEVALLED, we need to extend this function to support it.
2832 Until this is done, there is no way to call the function. */
2833 emacs_abort ();
2837 else if (COMPILEDP (fun))
2838 val = funcall_lambda (fun, numargs, args + 1);
2839 else
2841 if (NILP (fun))
2842 xsignal1 (Qvoid_function, original_fun);
2843 if (!CONSP (fun))
2844 xsignal1 (Qinvalid_function, original_fun);
2845 funcar = XCAR (fun);
2846 if (!SYMBOLP (funcar))
2847 xsignal1 (Qinvalid_function, original_fun);
2848 if (EQ (funcar, Qlambda)
2849 || EQ (funcar, Qclosure))
2850 val = funcall_lambda (fun, numargs, args + 1);
2851 else if (EQ (funcar, Qautoload))
2853 Fautoload_do_load (fun, original_fun, Qnil);
2854 check_cons_list ();
2855 goto retry;
2857 else
2858 xsignal1 (Qinvalid_function, original_fun);
2860 check_cons_list ();
2861 lisp_eval_depth--;
2862 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2863 val = call_debugger (list2 (Qexit, val));
2864 specpdl_ptr--;
2865 return val;
2868 static Lisp_Object
2869 apply_lambda (Lisp_Object fun, Lisp_Object args)
2871 Lisp_Object args_left;
2872 ptrdiff_t i;
2873 EMACS_INT numargs;
2874 register Lisp_Object *arg_vector;
2875 struct gcpro gcpro1, gcpro2, gcpro3;
2876 register Lisp_Object tem;
2877 USE_SAFE_ALLOCA;
2879 numargs = XFASTINT (Flength (args));
2880 SAFE_ALLOCA_LISP (arg_vector, numargs);
2881 args_left = args;
2883 GCPRO3 (*arg_vector, args_left, fun);
2884 gcpro1.nvars = 0;
2886 for (i = 0; i < numargs; )
2888 tem = Fcar (args_left), args_left = Fcdr (args_left);
2889 tem = eval_sub (tem);
2890 arg_vector[i++] = tem;
2891 gcpro1.nvars = i;
2894 UNGCPRO;
2896 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2897 set_backtrace_nargs (specpdl_ptr - 1, i);
2898 tem = funcall_lambda (fun, numargs, arg_vector);
2900 /* Do the debug-on-exit now, while arg_vector still exists. */
2901 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2903 /* Don't do it again when we return to eval. */
2904 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2905 tem = call_debugger (list2 (Qexit, tem));
2907 SAFE_FREE ();
2908 return tem;
2911 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2912 and return the result of evaluation.
2913 FUN must be either a lambda-expression or a compiled-code object. */
2915 static Lisp_Object
2916 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2917 register Lisp_Object *arg_vector)
2919 Lisp_Object val, syms_left, next, lexenv;
2920 ptrdiff_t count = SPECPDL_INDEX ();
2921 ptrdiff_t i;
2922 bool optional, rest;
2924 if (CONSP (fun))
2926 if (EQ (XCAR (fun), Qclosure))
2928 fun = XCDR (fun); /* Drop `closure'. */
2929 lexenv = XCAR (fun);
2930 CHECK_LIST_CONS (fun, fun);
2932 else
2933 lexenv = Qnil;
2934 syms_left = XCDR (fun);
2935 if (CONSP (syms_left))
2936 syms_left = XCAR (syms_left);
2937 else
2938 xsignal1 (Qinvalid_function, fun);
2940 else if (COMPILEDP (fun))
2942 syms_left = AREF (fun, COMPILED_ARGLIST);
2943 if (INTEGERP (syms_left))
2944 /* A byte-code object with a non-nil `push args' slot means we
2945 shouldn't bind any arguments, instead just call the byte-code
2946 interpreter directly; it will push arguments as necessary.
2948 Byte-code objects with either a non-existent, or a nil value for
2949 the `push args' slot (the default), have dynamically-bound
2950 arguments, and use the argument-binding code below instead (as do
2951 all interpreted functions, even lexically bound ones). */
2953 /* If we have not actually read the bytecode string
2954 and constants vector yet, fetch them from the file. */
2955 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2956 Ffetch_bytecode (fun);
2957 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2958 AREF (fun, COMPILED_CONSTANTS),
2959 AREF (fun, COMPILED_STACK_DEPTH),
2960 syms_left,
2961 nargs, arg_vector);
2963 lexenv = Qnil;
2965 else
2966 emacs_abort ();
2968 i = optional = rest = 0;
2969 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2971 QUIT;
2973 next = XCAR (syms_left);
2974 if (!SYMBOLP (next))
2975 xsignal1 (Qinvalid_function, fun);
2977 if (EQ (next, Qand_rest))
2978 rest = 1;
2979 else if (EQ (next, Qand_optional))
2980 optional = 1;
2981 else
2983 Lisp_Object arg;
2984 if (rest)
2986 arg = Flist (nargs - i, &arg_vector[i]);
2987 i = nargs;
2989 else if (i < nargs)
2990 arg = arg_vector[i++];
2991 else if (!optional)
2992 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2993 else
2994 arg = Qnil;
2996 /* Bind the argument. */
2997 if (!NILP (lexenv) && SYMBOLP (next))
2998 /* Lexically bind NEXT by adding it to the lexenv alist. */
2999 lexenv = Fcons (Fcons (next, arg), lexenv);
3000 else
3001 /* Dynamically bind NEXT. */
3002 specbind (next, arg);
3006 if (!NILP (syms_left))
3007 xsignal1 (Qinvalid_function, fun);
3008 else if (i < nargs)
3009 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3011 if (!EQ (lexenv, Vinternal_interpreter_environment))
3012 /* Instantiate a new lexical environment. */
3013 specbind (Qinternal_interpreter_environment, lexenv);
3015 if (CONSP (fun))
3016 val = Fprogn (XCDR (XCDR (fun)));
3017 else
3019 /* If we have not actually read the bytecode string
3020 and constants vector yet, fetch them from the file. */
3021 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3022 Ffetch_bytecode (fun);
3023 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3024 AREF (fun, COMPILED_CONSTANTS),
3025 AREF (fun, COMPILED_STACK_DEPTH),
3026 Qnil, 0, 0);
3029 return unbind_to (count, val);
3032 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3033 1, 1, 0,
3034 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3035 (Lisp_Object object)
3037 Lisp_Object tem;
3039 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3041 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3042 if (!CONSP (tem))
3044 tem = AREF (object, COMPILED_BYTECODE);
3045 if (CONSP (tem) && STRINGP (XCAR (tem)))
3046 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3047 else
3048 error ("Invalid byte code");
3050 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3051 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3053 return object;
3056 /* Return true if SYMBOL currently has a let-binding
3057 which was made in the buffer that is now current. */
3059 bool
3060 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3062 union specbinding *p;
3063 Lisp_Object buf = Fcurrent_buffer ();
3065 for (p = specpdl_ptr; p > specpdl; )
3066 if ((--p)->kind > SPECPDL_LET)
3068 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3069 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3070 if (symbol == let_bound_symbol
3071 && EQ (specpdl_where (p), buf))
3072 return 1;
3075 return 0;
3078 bool
3079 let_shadows_global_binding_p (Lisp_Object symbol)
3081 union specbinding *p;
3083 for (p = specpdl_ptr; p > specpdl; )
3084 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3085 return 1;
3087 return 0;
3090 /* `specpdl_ptr' describes which variable is
3091 let-bound, so it can be properly undone when we unbind_to.
3092 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3093 - SYMBOL is the variable being bound. Note that it should not be
3094 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3095 to record V2 here).
3096 - WHERE tells us in which buffer the binding took place.
3097 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3098 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3099 i.e. bindings to the default value of a variable which can be
3100 buffer-local. */
3102 void
3103 specbind (Lisp_Object symbol, Lisp_Object value)
3105 struct Lisp_Symbol *sym;
3107 CHECK_SYMBOL (symbol);
3108 sym = XSYMBOL (symbol);
3110 start:
3111 switch (sym->redirect)
3113 case SYMBOL_VARALIAS:
3114 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3115 case SYMBOL_PLAINVAL:
3116 /* The most common case is that of a non-constant symbol with a
3117 trivial value. Make that as fast as we can. */
3118 specpdl_ptr->let.kind = SPECPDL_LET;
3119 specpdl_ptr->let.symbol = symbol;
3120 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3121 grow_specpdl ();
3122 if (!sym->constant)
3123 SET_SYMBOL_VAL (sym, value);
3124 else
3125 set_internal (symbol, value, Qnil, 1);
3126 break;
3127 case SYMBOL_LOCALIZED:
3128 if (SYMBOL_BLV (sym)->frame_local)
3129 error ("Frame-local vars cannot be let-bound");
3130 case SYMBOL_FORWARDED:
3132 Lisp_Object ovalue = find_symbol_value (symbol);
3133 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3134 specpdl_ptr->let.symbol = symbol;
3135 specpdl_ptr->let.old_value = ovalue;
3136 specpdl_ptr->let.where = Fcurrent_buffer ();
3138 eassert (sym->redirect != SYMBOL_LOCALIZED
3139 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3141 if (sym->redirect == SYMBOL_LOCALIZED)
3143 if (!blv_found (SYMBOL_BLV (sym)))
3144 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3146 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3148 /* If SYMBOL is a per-buffer variable which doesn't have a
3149 buffer-local value here, make the `let' change the global
3150 value by changing the value of SYMBOL in all buffers not
3151 having their own value. This is consistent with what
3152 happens with other buffer-local variables. */
3153 if (NILP (Flocal_variable_p (symbol, Qnil)))
3155 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3156 grow_specpdl ();
3157 Fset_default (symbol, value);
3158 return;
3161 else
3162 specpdl_ptr->let.kind = SPECPDL_LET;
3164 grow_specpdl ();
3165 set_internal (symbol, value, Qnil, 1);
3166 break;
3168 default: emacs_abort ();
3172 /* Push unwind-protect entries of various types. */
3174 void
3175 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3177 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3178 specpdl_ptr->unwind.func = function;
3179 specpdl_ptr->unwind.arg = arg;
3180 grow_specpdl ();
3183 void
3184 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3186 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3187 specpdl_ptr->unwind_ptr.func = function;
3188 specpdl_ptr->unwind_ptr.arg = arg;
3189 grow_specpdl ();
3192 void
3193 record_unwind_protect_int (void (*function) (int), int arg)
3195 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3196 specpdl_ptr->unwind_int.func = function;
3197 specpdl_ptr->unwind_int.arg = arg;
3198 grow_specpdl ();
3201 void
3202 record_unwind_protect_void (void (*function) (void))
3204 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3205 specpdl_ptr->unwind_void.func = function;
3206 grow_specpdl ();
3209 static void
3210 do_nothing (void)
3213 /* Push an unwind-protect entry that does nothing, so that
3214 set_unwind_protect_ptr can overwrite it later. */
3216 void
3217 record_unwind_protect_nothing (void)
3219 record_unwind_protect_void (do_nothing);
3222 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3223 It need not be at the top of the stack. */
3225 void
3226 clear_unwind_protect (ptrdiff_t count)
3228 union specbinding *p = specpdl + count;
3229 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3230 p->unwind_void.func = do_nothing;
3233 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3234 It need not be at the top of the stack. Discard the entry's
3235 previous value without invoking it. */
3237 void
3238 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3239 Lisp_Object arg)
3241 union specbinding *p = specpdl + count;
3242 p->unwind.kind = SPECPDL_UNWIND;
3243 p->unwind.func = func;
3244 p->unwind.arg = arg;
3247 void
3248 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3250 union specbinding *p = specpdl + count;
3251 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3252 p->unwind_ptr.func = func;
3253 p->unwind_ptr.arg = arg;
3256 /* Pop and execute entries from the unwind-protect stack until the
3257 depth COUNT is reached. Return VALUE. */
3259 Lisp_Object
3260 unbind_to (ptrdiff_t count, Lisp_Object value)
3262 Lisp_Object quitf = Vquit_flag;
3263 struct gcpro gcpro1, gcpro2;
3265 GCPRO2 (value, quitf);
3266 Vquit_flag = Qnil;
3268 while (specpdl_ptr != specpdl + count)
3270 /* Decrement specpdl_ptr before we do the work to unbind it, so
3271 that an error in unbinding won't try to unbind the same entry
3272 again. Take care to copy any parts of the binding needed
3273 before invoking any code that can make more bindings. */
3275 specpdl_ptr--;
3277 switch (specpdl_ptr->kind)
3279 case SPECPDL_UNWIND:
3280 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3281 break;
3282 case SPECPDL_UNWIND_PTR:
3283 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3284 break;
3285 case SPECPDL_UNWIND_INT:
3286 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3287 break;
3288 case SPECPDL_UNWIND_VOID:
3289 specpdl_ptr->unwind_void.func ();
3290 break;
3291 case SPECPDL_BACKTRACE:
3292 break;
3293 case SPECPDL_LET:
3294 { /* If variable has a trivial value (no forwarding), we can
3295 just set it. No need to check for constant symbols here,
3296 since that was already done by specbind. */
3297 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3298 if (sym->redirect == SYMBOL_PLAINVAL)
3300 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3301 break;
3303 else
3304 { /* FALLTHROUGH!!
3305 NOTE: we only ever come here if make_local_foo was used for
3306 the first time on this var within this let. */
3309 case SPECPDL_LET_DEFAULT:
3310 Fset_default (specpdl_symbol (specpdl_ptr),
3311 specpdl_old_value (specpdl_ptr));
3312 break;
3313 case SPECPDL_LET_LOCAL:
3315 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3316 Lisp_Object where = specpdl_where (specpdl_ptr);
3317 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3318 eassert (BUFFERP (where));
3320 /* If this was a local binding, reset the value in the appropriate
3321 buffer, but only if that buffer's binding still exists. */
3322 if (!NILP (Flocal_variable_p (symbol, where)))
3323 set_internal (symbol, old_value, where, 1);
3325 break;
3329 if (NILP (Vquit_flag) && !NILP (quitf))
3330 Vquit_flag = quitf;
3332 UNGCPRO;
3333 return value;
3336 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3337 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3338 A special variable is one that will be bound dynamically, even in a
3339 context where binding is lexical by default. */)
3340 (Lisp_Object symbol)
3342 CHECK_SYMBOL (symbol);
3343 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3347 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3348 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3349 The debugger is entered when that frame exits, if the flag is non-nil. */)
3350 (Lisp_Object level, Lisp_Object flag)
3352 union specbinding *pdl = backtrace_top ();
3353 register EMACS_INT i;
3355 CHECK_NUMBER (level);
3357 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3358 pdl = backtrace_next (pdl);
3360 if (backtrace_p (pdl))
3361 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3363 return flag;
3366 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3367 doc: /* Print a trace of Lisp function calls currently active.
3368 Output stream used is value of `standard-output'. */)
3369 (void)
3371 union specbinding *pdl = backtrace_top ();
3372 Lisp_Object tem;
3373 Lisp_Object old_print_level = Vprint_level;
3375 if (NILP (Vprint_level))
3376 XSETFASTINT (Vprint_level, 8);
3378 while (backtrace_p (pdl))
3380 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3381 if (backtrace_nargs (pdl) == UNEVALLED)
3383 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3384 Qnil);
3385 write_string ("\n", -1);
3387 else
3389 tem = backtrace_function (pdl);
3390 Fprin1 (tem, Qnil); /* This can QUIT. */
3391 write_string ("(", -1);
3393 ptrdiff_t i;
3394 for (i = 0; i < backtrace_nargs (pdl); i++)
3396 if (i) write_string (" ", -1);
3397 Fprin1 (backtrace_args (pdl)[i], Qnil);
3400 write_string (")\n", -1);
3402 pdl = backtrace_next (pdl);
3405 Vprint_level = old_print_level;
3406 return Qnil;
3409 static union specbinding *
3410 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3412 union specbinding *pdl = backtrace_top ();
3413 register EMACS_INT i;
3415 CHECK_NATNUM (nframes);
3417 if (!NILP (base))
3418 { /* Skip up to `base'. */
3419 base = Findirect_function (base, Qt);
3420 while (backtrace_p (pdl)
3421 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3422 pdl = backtrace_next (pdl);
3425 /* Find the frame requested. */
3426 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3427 pdl = backtrace_next (pdl);
3429 return pdl;
3432 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3433 doc: /* Return the function and arguments NFRAMES up from current execution point.
3434 If that frame has not evaluated the arguments yet (or is a special form),
3435 the value is (nil FUNCTION ARG-FORMS...).
3436 If that frame has evaluated its arguments and called its function already,
3437 the value is (t FUNCTION ARG-VALUES...).
3438 A &rest arg is represented as the tail of the list ARG-VALUES.
3439 FUNCTION is whatever was supplied as car of evaluated list,
3440 or a lambda expression for macro calls.
3441 If NFRAMES is more than the number of frames, the value is nil.
3442 If BASE is non-nil, it should be a function and NFRAMES counts from its
3443 nearest activation frame. */)
3444 (Lisp_Object nframes, Lisp_Object base)
3446 union specbinding *pdl = get_backtrace_frame (nframes, base);
3448 if (!backtrace_p (pdl))
3449 return Qnil;
3450 if (backtrace_nargs (pdl) == UNEVALLED)
3451 return Fcons (Qnil,
3452 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3453 else
3455 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3457 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3461 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3462 the specpdl stack, and then rewind them. We store the pre-unwind values
3463 directly in the pre-existing specpdl elements (i.e. we swap the current
3464 value and the old value stored in the specpdl), kind of like the inplace
3465 pointer-reversal trick. As it turns out, the rewind does the same as the
3466 unwind, except it starts from the other end of the specpdl stack, so we use
3467 the same function for both unwind and rewind. */
3468 static void
3469 backtrace_eval_unrewind (int distance)
3471 union specbinding *tmp = specpdl_ptr;
3472 int step = -1;
3473 if (distance < 0)
3474 { /* It's a rewind rather than unwind. */
3475 tmp += distance - 1;
3476 step = 1;
3477 distance = -distance;
3480 for (; distance > 0; distance--)
3482 tmp += step;
3483 /* */
3484 switch (tmp->kind)
3486 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3487 unwind_protect, but the problem is that we don't know how to
3488 rewind them afterwards. */
3489 case SPECPDL_UNWIND:
3490 case SPECPDL_UNWIND_PTR:
3491 case SPECPDL_UNWIND_INT:
3492 case SPECPDL_UNWIND_VOID:
3493 case SPECPDL_BACKTRACE:
3494 break;
3495 case SPECPDL_LET:
3496 { /* If variable has a trivial value (no forwarding), we can
3497 just set it. No need to check for constant symbols here,
3498 since that was already done by specbind. */
3499 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3500 if (sym->redirect == SYMBOL_PLAINVAL)
3502 Lisp_Object old_value = specpdl_old_value (tmp);
3503 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3504 SET_SYMBOL_VAL (sym, old_value);
3505 break;
3507 else
3508 { /* FALLTHROUGH!!
3509 NOTE: we only ever come here if make_local_foo was used for
3510 the first time on this var within this let. */
3513 case SPECPDL_LET_DEFAULT:
3515 Lisp_Object sym = specpdl_symbol (tmp);
3516 Lisp_Object old_value = specpdl_old_value (tmp);
3517 set_specpdl_old_value (tmp, Fdefault_value (sym));
3518 Fset_default (sym, old_value);
3520 break;
3521 case SPECPDL_LET_LOCAL:
3523 Lisp_Object symbol = specpdl_symbol (tmp);
3524 Lisp_Object where = specpdl_where (tmp);
3525 Lisp_Object old_value = specpdl_old_value (tmp);
3526 eassert (BUFFERP (where));
3528 /* If this was a local binding, reset the value in the appropriate
3529 buffer, but only if that buffer's binding still exists. */
3530 if (!NILP (Flocal_variable_p (symbol, where)))
3532 set_specpdl_old_value
3533 (tmp, Fbuffer_local_value (symbol, where));
3534 set_internal (symbol, old_value, where, 1);
3537 break;
3542 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3543 doc: /* Evaluate EXP in the context of some activation frame.
3544 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3545 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3547 union specbinding *pdl = get_backtrace_frame (nframes, base);
3548 ptrdiff_t count = SPECPDL_INDEX ();
3549 ptrdiff_t distance = specpdl_ptr - pdl;
3550 eassert (distance >= 0);
3552 if (!backtrace_p (pdl))
3553 error ("Activation frame not found!");
3555 backtrace_eval_unrewind (distance);
3556 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3558 /* Use eval_sub rather than Feval since the main motivation behind
3559 backtrace-eval is to be able to get/set the value of lexical variables
3560 from the debugger. */
3561 return unbind_to (count, eval_sub (exp));
3564 void
3565 mark_specpdl (void)
3567 union specbinding *pdl;
3568 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3570 switch (pdl->kind)
3572 case SPECPDL_UNWIND:
3573 mark_object (specpdl_arg (pdl));
3574 break;
3576 case SPECPDL_BACKTRACE:
3578 ptrdiff_t nargs = backtrace_nargs (pdl);
3579 mark_object (backtrace_function (pdl));
3580 if (nargs == UNEVALLED)
3581 nargs = 1;
3582 while (nargs--)
3583 mark_object (backtrace_args (pdl)[nargs]);
3585 break;
3587 case SPECPDL_LET_DEFAULT:
3588 case SPECPDL_LET_LOCAL:
3589 mark_object (specpdl_where (pdl));
3590 /* Fall through. */
3591 case SPECPDL_LET:
3592 mark_object (specpdl_symbol (pdl));
3593 mark_object (specpdl_old_value (pdl));
3594 break;
3599 void
3600 get_backtrace (Lisp_Object array)
3602 union specbinding *pdl = backtrace_next (backtrace_top ());
3603 ptrdiff_t i = 0, asize = ASIZE (array);
3605 /* Copy the backtrace contents into working memory. */
3606 for (; i < asize; i++)
3608 if (backtrace_p (pdl))
3610 ASET (array, i, backtrace_function (pdl));
3611 pdl = backtrace_next (pdl);
3613 else
3614 ASET (array, i, Qnil);
3618 Lisp_Object backtrace_top_function (void)
3620 union specbinding *pdl = backtrace_top ();
3621 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3624 void
3625 syms_of_eval (void)
3627 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3628 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3629 If Lisp code tries to increase the total number past this amount,
3630 an error is signaled.
3631 You can safely use a value considerably larger than the default value,
3632 if that proves inconveniently small. However, if you increase it too far,
3633 Emacs could run out of memory trying to make the stack bigger. */);
3635 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3636 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3638 This limit serves to catch infinite recursions for you before they cause
3639 actual stack overflow in C, which would be fatal for Emacs.
3640 You can safely make it considerably larger than its default value,
3641 if that proves inconveniently small. However, if you increase it too far,
3642 Emacs could overflow the real C stack, and crash. */);
3644 DEFVAR_LISP ("quit-flag", Vquit_flag,
3645 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3646 If the value is t, that means do an ordinary quit.
3647 If the value equals `throw-on-input', that means quit by throwing
3648 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3649 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3650 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3651 Vquit_flag = Qnil;
3653 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3654 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3655 Note that `quit-flag' will still be set by typing C-g,
3656 so a quit will be signaled as soon as `inhibit-quit' is nil.
3657 To prevent this happening, set `quit-flag' to nil
3658 before making `inhibit-quit' nil. */);
3659 Vinhibit_quit = Qnil;
3661 DEFSYM (Qinhibit_quit, "inhibit-quit");
3662 DEFSYM (Qautoload, "autoload");
3663 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3664 DEFSYM (Qmacro, "macro");
3665 DEFSYM (Qdeclare, "declare");
3667 /* Note that the process handling also uses Qexit, but we don't want
3668 to staticpro it twice, so we just do it here. */
3669 DEFSYM (Qexit, "exit");
3671 DEFSYM (Qinteractive, "interactive");
3672 DEFSYM (Qcommandp, "commandp");
3673 DEFSYM (Qand_rest, "&rest");
3674 DEFSYM (Qand_optional, "&optional");
3675 DEFSYM (Qclosure, "closure");
3676 DEFSYM (Qdebug, "debug");
3678 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3679 doc: /* Non-nil means never enter the debugger.
3680 Normally set while the debugger is already active, to avoid recursive
3681 invocations. */);
3682 Vinhibit_debugger = Qnil;
3684 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3685 doc: /* Non-nil means enter debugger if an error is signaled.
3686 Does not apply to errors handled by `condition-case' or those
3687 matched by `debug-ignored-errors'.
3688 If the value is a list, an error only means to enter the debugger
3689 if one of its condition symbols appears in the list.
3690 When you evaluate an expression interactively, this variable
3691 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3692 The command `toggle-debug-on-error' toggles this.
3693 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3694 Vdebug_on_error = Qnil;
3696 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3697 doc: /* List of errors for which the debugger should not be called.
3698 Each element may be a condition-name or a regexp that matches error messages.
3699 If any element applies to a given error, that error skips the debugger
3700 and just returns to top level.
3701 This overrides the variable `debug-on-error'.
3702 It does not apply to errors handled by `condition-case'. */);
3703 Vdebug_ignored_errors = Qnil;
3705 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3706 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3707 Does not apply if quit is handled by a `condition-case'. */);
3708 debug_on_quit = 0;
3710 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3711 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3713 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3714 doc: /* Non-nil means debugger may continue execution.
3715 This is nil when the debugger is called under circumstances where it
3716 might not be safe to continue. */);
3717 debugger_may_continue = 1;
3719 DEFVAR_LISP ("debugger", Vdebugger,
3720 doc: /* Function to call to invoke debugger.
3721 If due to frame exit, args are `exit' and the value being returned;
3722 this function's value will be returned instead of that.
3723 If due to error, args are `error' and a list of the args to `signal'.
3724 If due to `apply' or `funcall' entry, one arg, `lambda'.
3725 If due to `eval' entry, one arg, t. */);
3726 Vdebugger = Qnil;
3728 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3729 doc: /* If non-nil, this is a function for `signal' to call.
3730 It receives the same arguments that `signal' was given.
3731 The Edebug package uses this to regain control. */);
3732 Vsignal_hook_function = Qnil;
3734 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3735 doc: /* Non-nil means call the debugger regardless of condition handlers.
3736 Note that `debug-on-error', `debug-on-quit' and friends
3737 still determine whether to handle the particular condition. */);
3738 Vdebug_on_signal = Qnil;
3740 /* When lexical binding is being used,
3741 Vinternal_interpreter_environment is non-nil, and contains an alist
3742 of lexically-bound variable, or (t), indicating an empty
3743 environment. The lisp name of this variable would be
3744 `internal-interpreter-environment' if it weren't hidden.
3745 Every element of this list can be either a cons (VAR . VAL)
3746 specifying a lexical binding, or a single symbol VAR indicating
3747 that this variable should use dynamic scoping. */
3748 DEFSYM (Qinternal_interpreter_environment,
3749 "internal-interpreter-environment");
3750 DEFVAR_LISP ("internal-interpreter-environment",
3751 Vinternal_interpreter_environment,
3752 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3753 When lexical binding is not being used, this variable is nil.
3754 A value of `(t)' indicates an empty environment, otherwise it is an
3755 alist of active lexical bindings. */);
3756 Vinternal_interpreter_environment = Qnil;
3757 /* Don't export this variable to Elisp, so no one can mess with it
3758 (Just imagine if someone makes it buffer-local). */
3759 Funintern (Qinternal_interpreter_environment, Qnil);
3761 DEFSYM (Vrun_hooks, "run-hooks");
3763 staticpro (&Vautoload_queue);
3764 Vautoload_queue = Qnil;
3765 staticpro (&Vsignaling_function);
3766 Vsignaling_function = Qnil;
3768 inhibit_lisp_code = Qnil;
3770 defsubr (&Sor);
3771 defsubr (&Sand);
3772 defsubr (&Sif);
3773 defsubr (&Scond);
3774 defsubr (&Sprogn);
3775 defsubr (&Sprog1);
3776 defsubr (&Sprog2);
3777 defsubr (&Ssetq);
3778 defsubr (&Squote);
3779 defsubr (&Sfunction);
3780 defsubr (&Sdefault_toplevel_value);
3781 defsubr (&Sset_default_toplevel_value);
3782 defsubr (&Sdefvar);
3783 defsubr (&Sdefvaralias);
3784 defsubr (&Sdefconst);
3785 defsubr (&Smake_var_non_special);
3786 defsubr (&Slet);
3787 defsubr (&SletX);
3788 defsubr (&Swhile);
3789 defsubr (&Smacroexpand);
3790 defsubr (&Scatch);
3791 defsubr (&Sthrow);
3792 defsubr (&Sunwind_protect);
3793 defsubr (&Scondition_case);
3794 defsubr (&Ssignal);
3795 defsubr (&Scommandp);
3796 defsubr (&Sautoload);
3797 defsubr (&Sautoload_do_load);
3798 defsubr (&Seval);
3799 defsubr (&Sapply);
3800 defsubr (&Sfuncall);
3801 defsubr (&Srun_hooks);
3802 defsubr (&Srun_hook_with_args);
3803 defsubr (&Srun_hook_with_args_until_success);
3804 defsubr (&Srun_hook_with_args_until_failure);
3805 defsubr (&Srun_hook_wrapped);
3806 defsubr (&Sfetch_bytecode);
3807 defsubr (&Sbacktrace_debug);
3808 defsubr (&Sbacktrace);
3809 defsubr (&Sbacktrace_frame);
3810 defsubr (&Sbacktrace_eval);
3811 defsubr (&Sspecial_variable_p);
3812 defsubr (&Sfunctionp);