* doc/lispref/frames.texi (Mouse Tracking): Fix typo.
[emacs.git] / src / eval.c
blob77d435acbe6a3da3a3b9cc0e99f9e73b1186d947
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2015 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #include "commands.h"
28 #include "keyboard.h"
29 #include "dispextern.h"
30 #include "buffer.h"
32 /* Chain of condition and catch handlers currently in effect. */
34 struct handler *handlerlist;
36 /* Non-nil means record all fset's and provide's, to be undone
37 if the file being autoloaded is not fully loaded.
38 They are recorded by being consed onto the front of Vautoload_queue:
39 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
41 Lisp_Object Vautoload_queue;
43 /* This holds either the symbol `run-hooks' or nil.
44 It is nil at an early stage of startup, and when Emacs
45 is shutting down. */
46 Lisp_Object Vrun_hooks;
48 /* Current number of specbindings allocated in specpdl, not counting
49 the dummy entry specpdl[-1]. */
51 ptrdiff_t specpdl_size;
53 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
54 only so that its address can be taken. */
56 union specbinding *specpdl;
58 /* Pointer to first unused element in specpdl. */
60 union specbinding *specpdl_ptr;
62 /* Depth in Lisp evaluations and function calls. */
64 EMACS_INT lisp_eval_depth;
66 /* The value of num_nonmacro_input_events as of the last time we
67 started to enter the debugger. If we decide to enter the debugger
68 again when this is still equal to num_nonmacro_input_events, then we
69 know that the debugger itself has an error, and we should just
70 signal the error instead of entering an infinite loop of debugger
71 invocations. */
73 static EMACS_INT when_entered_debugger;
75 /* The function from which the last `signal' was called. Set in
76 Fsignal. */
77 /* FIXME: We should probably get rid of this! */
78 Lisp_Object Vsignaling_function;
80 /* If non-nil, Lisp code must not be run since some part of Emacs is in
81 an inconsistent state. Currently unused. */
82 Lisp_Object inhibit_lisp_code;
84 /* These would ordinarily be static, but they need to be visible to GDB. */
85 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
86 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
87 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
88 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
89 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
91 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
92 static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
94 static Lisp_Object
95 specpdl_symbol (union specbinding *pdl)
97 eassert (pdl->kind >= SPECPDL_LET);
98 return pdl->let.symbol;
101 static Lisp_Object
102 specpdl_old_value (union specbinding *pdl)
104 eassert (pdl->kind >= SPECPDL_LET);
105 return pdl->let.old_value;
108 static void
109 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
111 eassert (pdl->kind >= SPECPDL_LET);
112 pdl->let.old_value = val;
115 static Lisp_Object
116 specpdl_where (union specbinding *pdl)
118 eassert (pdl->kind > SPECPDL_LET);
119 return pdl->let.where;
122 static Lisp_Object
123 specpdl_arg (union specbinding *pdl)
125 eassert (pdl->kind == SPECPDL_UNWIND);
126 return pdl->unwind.arg;
129 Lisp_Object
130 backtrace_function (union specbinding *pdl)
132 eassert (pdl->kind == SPECPDL_BACKTRACE);
133 return pdl->bt.function;
136 static ptrdiff_t
137 backtrace_nargs (union specbinding *pdl)
139 eassert (pdl->kind == SPECPDL_BACKTRACE);
140 return pdl->bt.nargs;
143 Lisp_Object *
144 backtrace_args (union specbinding *pdl)
146 eassert (pdl->kind == SPECPDL_BACKTRACE);
147 return pdl->bt.args;
150 static bool
151 backtrace_debug_on_exit (union specbinding *pdl)
153 eassert (pdl->kind == SPECPDL_BACKTRACE);
154 return pdl->bt.debug_on_exit;
157 /* Functions to modify slots of backtrace records. */
159 static void
160 set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
162 eassert (pdl->kind == SPECPDL_BACKTRACE);
163 pdl->bt.args = args;
164 pdl->bt.nargs = nargs;
167 static void
168 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
170 eassert (pdl->kind == SPECPDL_BACKTRACE);
171 pdl->bt.debug_on_exit = doe;
174 /* Helper functions to scan the backtrace. */
176 bool
177 backtrace_p (union specbinding *pdl)
178 { return pdl >= specpdl; }
180 union specbinding *
181 backtrace_top (void)
183 union specbinding *pdl = specpdl_ptr - 1;
184 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
185 pdl--;
186 return pdl;
189 union specbinding *
190 backtrace_next (union specbinding *pdl)
192 pdl--;
193 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
194 pdl--;
195 return pdl;
198 /* Return a pointer to somewhere near the top of the C stack. */
199 void *
200 near_C_stack_top (void)
202 return backtrace_args (backtrace_top ());
205 void
206 init_eval_once (void)
208 enum { size = 50 };
209 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
210 specpdl_size = size;
211 specpdl = specpdl_ptr = pdlvec + 1;
212 /* Don't forget to update docs (lispref node "Local Variables"). */
213 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
214 max_lisp_eval_depth = 800;
216 Vrun_hooks = Qnil;
219 static struct handler handlerlist_sentinel;
221 void
222 init_eval (void)
224 byte_stack_list = 0;
225 specpdl_ptr = specpdl;
226 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
227 This is important since handlerlist->nextfree holds the freelist
228 which would otherwise leak every time we unwind back to top-level. */
229 struct handler *c;
230 handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
231 PUSH_HANDLER (c, Qunbound, CATCHER);
232 eassert (c == &handlerlist_sentinel);
233 handlerlist_sentinel.nextfree = NULL;
234 handlerlist_sentinel.next = NULL;
236 Vquit_flag = Qnil;
237 debug_on_next_call = 0;
238 lisp_eval_depth = 0;
239 /* This is less than the initial value of num_nonmacro_input_events. */
240 when_entered_debugger = -1;
243 /* Unwind-protect function used by call_debugger. */
245 static void
246 restore_stack_limits (Lisp_Object data)
248 max_specpdl_size = XINT (XCAR (data));
249 max_lisp_eval_depth = XINT (XCDR (data));
252 static void grow_specpdl (void);
254 /* Call the Lisp debugger, giving it argument ARG. */
256 Lisp_Object
257 call_debugger (Lisp_Object arg)
259 bool debug_while_redisplaying;
260 ptrdiff_t count = SPECPDL_INDEX ();
261 Lisp_Object val;
262 EMACS_INT old_depth = max_lisp_eval_depth;
263 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
264 EMACS_INT old_max = max (max_specpdl_size, count);
266 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
267 max_lisp_eval_depth = lisp_eval_depth + 40;
269 /* While debugging Bug#16603, previous value of 100 was found
270 too small to avoid specpdl overflow in the debugger itself. */
271 if (max_specpdl_size - 200 < count)
272 max_specpdl_size = count + 200;
274 if (old_max == count)
276 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
277 specpdl_ptr--;
278 grow_specpdl ();
281 /* Restore limits after leaving the debugger. */
282 record_unwind_protect (restore_stack_limits,
283 Fcons (make_number (old_max),
284 make_number (old_depth)));
286 #ifdef HAVE_WINDOW_SYSTEM
287 if (display_hourglass_p)
288 cancel_hourglass ();
289 #endif
291 debug_on_next_call = 0;
292 when_entered_debugger = num_nonmacro_input_events;
294 /* Resetting redisplaying_p to 0 makes sure that debug output is
295 displayed if the debugger is invoked during redisplay. */
296 debug_while_redisplaying = redisplaying_p;
297 redisplaying_p = 0;
298 specbind (intern ("debugger-may-continue"),
299 debug_while_redisplaying ? Qnil : Qt);
300 specbind (Qinhibit_redisplay, Qnil);
301 specbind (Qinhibit_debugger, Qt);
303 #if 0 /* Binding this prevents execution of Lisp code during
304 redisplay, which necessarily leads to display problems. */
305 specbind (Qinhibit_eval_during_redisplay, Qt);
306 #endif
308 val = apply1 (Vdebugger, arg);
310 /* Interrupting redisplay and resuming it later is not safe under
311 all circumstances. So, when the debugger returns, abort the
312 interrupted redisplay by going back to the top-level. */
313 if (debug_while_redisplaying)
314 Ftop_level ();
316 return unbind_to (count, val);
319 static void
320 do_debug_on_call (Lisp_Object code, ptrdiff_t count)
322 debug_on_next_call = 0;
323 set_backtrace_debug_on_exit (specpdl + count, true);
324 call_debugger (list1 (code));
327 /* NOTE!!! Every function that can call EVAL must protect its args
328 and temporaries from garbage collection while it needs them.
329 The definition of `For' shows what you have to do. */
331 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
332 doc: /* Eval args until one of them yields non-nil, then return that value.
333 The remaining args are not evalled at all.
334 If all args return nil, return nil.
335 usage: (or CONDITIONS...) */)
336 (Lisp_Object args)
338 Lisp_Object val = Qnil;
340 while (CONSP (args))
342 val = eval_sub (XCAR (args));
343 if (!NILP (val))
344 break;
345 args = XCDR (args);
348 return val;
351 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
352 doc: /* Eval args until one of them yields nil, then return nil.
353 The remaining args are not evalled at all.
354 If no arg yields nil, return the last arg's value.
355 usage: (and CONDITIONS...) */)
356 (Lisp_Object args)
358 Lisp_Object val = Qt;
360 while (CONSP (args))
362 val = eval_sub (XCAR (args));
363 if (NILP (val))
364 break;
365 args = XCDR (args);
368 return val;
371 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
372 doc: /* If COND yields non-nil, do THEN, else do ELSE...
373 Returns the value of THEN or the value of the last of the ELSE's.
374 THEN must be one expression, but ELSE... can be zero or more expressions.
375 If COND yields nil, and there are no ELSE's, the value is nil.
376 usage: (if COND THEN ELSE...) */)
377 (Lisp_Object args)
379 Lisp_Object cond;
381 cond = eval_sub (XCAR (args));
383 if (!NILP (cond))
384 return eval_sub (Fcar (XCDR (args)));
385 return Fprogn (XCDR (XCDR (args)));
388 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
389 doc: /* Try each clause until one succeeds.
390 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
391 and, if the value is non-nil, this clause succeeds:
392 then the expressions in BODY are evaluated and the last one's
393 value is the value of the cond-form.
394 If a clause has one element, as in (CONDITION), then the cond-form
395 returns CONDITION's value, if that is non-nil.
396 If no clause succeeds, cond returns nil.
397 usage: (cond CLAUSES...) */)
398 (Lisp_Object args)
400 Lisp_Object val = args;
402 while (CONSP (args))
404 Lisp_Object clause = XCAR (args);
405 val = eval_sub (Fcar (clause));
406 if (!NILP (val))
408 if (!NILP (XCDR (clause)))
409 val = Fprogn (XCDR (clause));
410 break;
412 args = XCDR (args);
415 return val;
418 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
419 doc: /* Eval BODY forms sequentially and return value of last one.
420 usage: (progn BODY...) */)
421 (Lisp_Object body)
423 Lisp_Object val = Qnil;
425 while (CONSP (body))
427 val = eval_sub (XCAR (body));
428 body = XCDR (body);
431 return val;
434 /* Evaluate BODY sequentially, discarding its value. Suitable for
435 record_unwind_protect. */
437 void
438 unwind_body (Lisp_Object body)
440 Fprogn (body);
443 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
444 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
445 The value of FIRST is saved during the evaluation of the remaining args,
446 whose values are discarded.
447 usage: (prog1 FIRST BODY...) */)
448 (Lisp_Object args)
450 Lisp_Object val;
451 Lisp_Object args_left;
453 args_left = args;
454 val = args;
456 val = eval_sub (XCAR (args_left));
457 while (CONSP (args_left = XCDR (args_left)))
458 eval_sub (XCAR (args_left));
460 return val;
463 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
464 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
465 The value of FORM2 is saved during the evaluation of the
466 remaining args, whose values are discarded.
467 usage: (prog2 FORM1 FORM2 BODY...) */)
468 (Lisp_Object args)
470 eval_sub (XCAR (args));
471 return Fprog1 (XCDR (args));
474 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
475 doc: /* Set each SYM to the value of its VAL.
476 The symbols SYM are variables; they are literal (not evaluated).
477 The values VAL are expressions; they are evaluated.
478 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
479 The second VAL is not computed until after the first SYM is set, and so on;
480 each VAL can use the new value of variables set earlier in the `setq'.
481 The return value of the `setq' form is the value of the last VAL.
482 usage: (setq [SYM VAL]...) */)
483 (Lisp_Object args)
485 Lisp_Object val, sym, lex_binding;
487 val = args;
488 if (CONSP (args))
490 Lisp_Object args_left = args;
494 val = eval_sub (Fcar (XCDR (args_left)));
495 sym = XCAR (args_left);
497 /* Like for eval_sub, we do not check declared_special here since
498 it's been done when let-binding. */
499 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
500 && SYMBOLP (sym)
501 && !NILP (lex_binding
502 = Fassq (sym, Vinternal_interpreter_environment)))
503 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
504 else
505 Fset (sym, val); /* SYM is dynamically bound. */
507 args_left = Fcdr (XCDR (args_left));
509 while (CONSP (args_left));
512 return val;
515 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
516 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
517 Warning: `quote' does not construct its return value, but just returns
518 the value that was pre-constructed by the Lisp reader (see info node
519 `(elisp)Printed Representation').
520 This means that \\='(a . b) is not identical to (cons \\='a \\='b): the former
521 does not cons. Quoting should be reserved for constants that will
522 never be modified by side-effects, unless you like self-modifying code.
523 See the common pitfall in info node `(elisp)Rearrangement' for an example
524 of unexpected results when a quoted object is modified.
525 usage: (quote ARG) */)
526 (Lisp_Object args)
528 if (CONSP (XCDR (args)))
529 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
530 return XCAR (args);
533 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
534 doc: /* Like `quote', but preferred for objects which are functions.
535 In byte compilation, `function' causes its argument to be compiled.
536 `quote' cannot do that.
537 usage: (function ARG) */)
538 (Lisp_Object args)
540 Lisp_Object quoted = XCAR (args);
542 if (CONSP (XCDR (args)))
543 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
545 if (!NILP (Vinternal_interpreter_environment)
546 && CONSP (quoted)
547 && EQ (XCAR (quoted), Qlambda))
548 { /* This is a lambda expression within a lexical environment;
549 return an interpreted closure instead of a simple lambda. */
550 Lisp_Object cdr = XCDR (quoted);
551 Lisp_Object tmp = cdr;
552 if (CONSP (tmp)
553 && (tmp = XCDR (tmp), CONSP (tmp))
554 && (tmp = XCAR (tmp), CONSP (tmp))
555 && (EQ (QCdocumentation, XCAR (tmp))))
556 { /* Handle the special (:documentation <form>) to build the docstring
557 dynamically. */
558 Lisp_Object docstring = eval_sub (Fcar (XCDR (tmp)));
559 CHECK_STRING (docstring);
560 cdr = Fcons (XCAR (cdr), Fcons (docstring, XCDR (XCDR (cdr))));
562 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
563 cdr));
565 else
566 /* Simply quote the argument. */
567 return quoted;
571 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
572 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
573 Aliased variables always have the same value; setting one sets the other.
574 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
575 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
576 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
577 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
578 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
579 The return value is BASE-VARIABLE. */)
580 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
582 struct Lisp_Symbol *sym;
584 CHECK_SYMBOL (new_alias);
585 CHECK_SYMBOL (base_variable);
587 sym = XSYMBOL (new_alias);
589 if (sym->constant)
590 /* Not sure why, but why not? */
591 error ("Cannot make a constant an alias");
593 switch (sym->redirect)
595 case SYMBOL_FORWARDED:
596 error ("Cannot make an internal variable an alias");
597 case SYMBOL_LOCALIZED:
598 error ("Don't know how to make a localized variable an alias");
601 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
602 If n_a is bound, but b_v is not, set the value of b_v to n_a,
603 so that old-code that affects n_a before the aliasing is setup
604 still works. */
605 if (NILP (Fboundp (base_variable)))
606 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
609 union specbinding *p;
611 for (p = specpdl_ptr; p > specpdl; )
612 if ((--p)->kind >= SPECPDL_LET
613 && (EQ (new_alias, specpdl_symbol (p))))
614 error ("Don't know how to make a let-bound variable an alias");
617 sym->declared_special = 1;
618 XSYMBOL (base_variable)->declared_special = 1;
619 sym->redirect = SYMBOL_VARALIAS;
620 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
621 sym->constant = SYMBOL_CONSTANT_P (base_variable);
622 LOADHIST_ATTACH (new_alias);
623 /* Even if docstring is nil: remove old docstring. */
624 Fput (new_alias, Qvariable_documentation, docstring);
626 return base_variable;
629 static union specbinding *
630 default_toplevel_binding (Lisp_Object symbol)
632 union specbinding *binding = NULL;
633 union specbinding *pdl = specpdl_ptr;
634 while (pdl > specpdl)
636 switch ((--pdl)->kind)
638 case SPECPDL_LET_DEFAULT:
639 case SPECPDL_LET:
640 if (EQ (specpdl_symbol (pdl), symbol))
641 binding = pdl;
642 break;
645 return binding;
648 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
649 doc: /* Return SYMBOL's toplevel default value.
650 "Toplevel" means outside of any let binding. */)
651 (Lisp_Object symbol)
653 union specbinding *binding = default_toplevel_binding (symbol);
654 Lisp_Object value
655 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
656 if (!EQ (value, Qunbound))
657 return value;
658 xsignal1 (Qvoid_variable, symbol);
661 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
662 Sset_default_toplevel_value, 2, 2, 0,
663 doc: /* Set SYMBOL's toplevel default value to VALUE.
664 "Toplevel" means outside of any let binding. */)
665 (Lisp_Object symbol, Lisp_Object value)
667 union specbinding *binding = default_toplevel_binding (symbol);
668 if (binding)
669 set_specpdl_old_value (binding, value);
670 else
671 Fset_default (symbol, value);
672 return Qnil;
675 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
676 doc: /* Define SYMBOL as a variable, and return SYMBOL.
677 You are not required to define a variable in order to use it, but
678 defining it lets you supply an initial value and documentation, which
679 can be referred to by the Emacs help facilities and other programming
680 tools. The `defvar' form also declares the variable as \"special\",
681 so that it is always dynamically bound even if `lexical-binding' is t.
683 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
684 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
685 default value is what is set; buffer-local values are not affected.
686 If INITVALUE is missing, SYMBOL's value is not set.
688 If SYMBOL has a local binding, then this form affects the local
689 binding. This is usually not what you want. Thus, if you need to
690 load a file defining variables, with this form or with `defconst' or
691 `defcustom', you should always load that file _outside_ any bindings
692 for these variables. \(`defconst' and `defcustom' behave similarly in
693 this respect.)
695 The optional argument DOCSTRING is a documentation string for the
696 variable.
698 To define a user option, use `defcustom' instead of `defvar'.
699 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
700 (Lisp_Object args)
702 Lisp_Object sym, tem, tail;
704 sym = XCAR (args);
705 tail = XCDR (args);
707 if (CONSP (tail))
709 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
710 error ("Too many arguments");
712 tem = Fdefault_boundp (sym);
714 /* Do it before evaluating the initial value, for self-references. */
715 XSYMBOL (sym)->declared_special = 1;
717 if (NILP (tem))
718 Fset_default (sym, eval_sub (XCAR (tail)));
719 else
720 { /* Check if there is really a global binding rather than just a let
721 binding that shadows the global unboundness of the var. */
722 union specbinding *binding = default_toplevel_binding (sym);
723 if (binding && EQ (specpdl_old_value (binding), Qunbound))
725 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
728 tail = XCDR (tail);
729 tem = Fcar (tail);
730 if (!NILP (tem))
732 if (!NILP (Vpurify_flag))
733 tem = Fpurecopy (tem);
734 Fput (sym, Qvariable_documentation, tem);
736 LOADHIST_ATTACH (sym);
738 else if (!NILP (Vinternal_interpreter_environment)
739 && !XSYMBOL (sym)->declared_special)
740 /* A simple (defvar foo) with lexical scoping does "nothing" except
741 declare that var to be dynamically scoped *locally* (i.e. within
742 the current file or let-block). */
743 Vinternal_interpreter_environment
744 = Fcons (sym, Vinternal_interpreter_environment);
745 else
747 /* Simple (defvar <var>) should not count as a definition at all.
748 It could get in the way of other definitions, and unloading this
749 package could try to make the variable unbound. */
752 return sym;
755 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
756 doc: /* Define SYMBOL as a constant variable.
757 This declares that neither programs nor users should ever change the
758 value. This constancy is not actually enforced by Emacs Lisp, but
759 SYMBOL is marked as a special variable so that it is never lexically
760 bound.
762 The `defconst' form always sets the value of SYMBOL to the result of
763 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
764 what is set; buffer-local values are not affected. If SYMBOL has a
765 local binding, then this form sets the local binding's value.
766 However, you should normally not make local bindings for variables
767 defined with this form.
769 The optional DOCSTRING specifies the variable's documentation string.
770 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
771 (Lisp_Object args)
773 Lisp_Object sym, tem;
775 sym = XCAR (args);
776 if (CONSP (Fcdr (XCDR (XCDR (args)))))
777 error ("Too many arguments");
779 tem = eval_sub (Fcar (XCDR (args)));
780 if (!NILP (Vpurify_flag))
781 tem = Fpurecopy (tem);
782 Fset_default (sym, tem);
783 XSYMBOL (sym)->declared_special = 1;
784 tem = Fcar (XCDR (XCDR (args)));
785 if (!NILP (tem))
787 if (!NILP (Vpurify_flag))
788 tem = Fpurecopy (tem);
789 Fput (sym, Qvariable_documentation, tem);
791 Fput (sym, Qrisky_local_variable, Qt);
792 LOADHIST_ATTACH (sym);
793 return sym;
796 /* Make SYMBOL lexically scoped. */
797 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
798 Smake_var_non_special, 1, 1, 0,
799 doc: /* Internal function. */)
800 (Lisp_Object symbol)
802 CHECK_SYMBOL (symbol);
803 XSYMBOL (symbol)->declared_special = 0;
804 return Qnil;
808 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
809 doc: /* Bind variables according to VARLIST then eval BODY.
810 The value of the last form in BODY is returned.
811 Each element of VARLIST is a symbol (which is bound to nil)
812 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
813 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
814 usage: (let* VARLIST BODY...) */)
815 (Lisp_Object args)
817 Lisp_Object varlist, var, val, elt, lexenv;
818 ptrdiff_t count = SPECPDL_INDEX ();
820 lexenv = Vinternal_interpreter_environment;
822 varlist = XCAR (args);
823 while (CONSP (varlist))
825 QUIT;
827 elt = XCAR (varlist);
828 if (SYMBOLP (elt))
830 var = elt;
831 val = Qnil;
833 else if (! NILP (Fcdr (Fcdr (elt))))
834 signal_error ("`let' bindings can have only one value-form", elt);
835 else
837 var = Fcar (elt);
838 val = eval_sub (Fcar (Fcdr (elt)));
841 if (!NILP (lexenv) && SYMBOLP (var)
842 && !XSYMBOL (var)->declared_special
843 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
844 /* Lexically bind VAR by adding it to the interpreter's binding
845 alist. */
847 Lisp_Object newenv
848 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
849 if (EQ (Vinternal_interpreter_environment, lexenv))
850 /* Save the old lexical environment on the specpdl stack,
851 but only for the first lexical binding, since we'll never
852 need to revert to one of the intermediate ones. */
853 specbind (Qinternal_interpreter_environment, newenv);
854 else
855 Vinternal_interpreter_environment = newenv;
857 else
858 specbind (var, val);
860 varlist = XCDR (varlist);
863 val = Fprogn (XCDR (args));
864 return unbind_to (count, val);
867 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
868 doc: /* Bind variables according to VARLIST then eval BODY.
869 The value of the last form in BODY is returned.
870 Each element of VARLIST is a symbol (which is bound to nil)
871 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
872 All the VALUEFORMs are evalled before any symbols are bound.
873 usage: (let VARLIST BODY...) */)
874 (Lisp_Object args)
876 Lisp_Object *temps, tem, lexenv;
877 Lisp_Object elt, varlist;
878 ptrdiff_t count = SPECPDL_INDEX ();
879 ptrdiff_t argnum;
880 USE_SAFE_ALLOCA;
882 varlist = XCAR (args);
884 /* Make space to hold the values to give the bound variables. */
885 elt = Flength (varlist);
886 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
888 /* Compute the values and store them in `temps'. */
890 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
892 QUIT;
893 elt = XCAR (varlist);
894 if (SYMBOLP (elt))
895 temps [argnum++] = Qnil;
896 else if (! NILP (Fcdr (Fcdr (elt))))
897 signal_error ("`let' bindings can have only one value-form", elt);
898 else
899 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
902 lexenv = Vinternal_interpreter_environment;
904 varlist = XCAR (args);
905 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
907 Lisp_Object var;
909 elt = XCAR (varlist);
910 var = SYMBOLP (elt) ? elt : Fcar (elt);
911 tem = temps[argnum++];
913 if (!NILP (lexenv) && SYMBOLP (var)
914 && !XSYMBOL (var)->declared_special
915 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
916 /* Lexically bind VAR by adding it to the lexenv alist. */
917 lexenv = Fcons (Fcons (var, tem), lexenv);
918 else
919 /* Dynamically bind VAR. */
920 specbind (var, tem);
923 if (!EQ (lexenv, Vinternal_interpreter_environment))
924 /* Instantiate a new lexical environment. */
925 specbind (Qinternal_interpreter_environment, lexenv);
927 elt = Fprogn (XCDR (args));
928 SAFE_FREE ();
929 return unbind_to (count, elt);
932 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
933 doc: /* If TEST yields non-nil, eval BODY... and repeat.
934 The order of execution is thus TEST, BODY, TEST, BODY and so on
935 until TEST returns nil.
936 usage: (while TEST BODY...) */)
937 (Lisp_Object args)
939 Lisp_Object test, body;
941 test = XCAR (args);
942 body = XCDR (args);
943 while (!NILP (eval_sub (test)))
945 QUIT;
946 Fprogn (body);
949 return Qnil;
952 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
953 doc: /* Return result of expanding macros at top level of FORM.
954 If FORM is not a macro call, it is returned unchanged.
955 Otherwise, the macro is expanded and the expansion is considered
956 in place of FORM. When a non-macro-call results, it is returned.
958 The second optional arg ENVIRONMENT specifies an environment of macro
959 definitions to shadow the loaded ones for use in file byte-compilation. */)
960 (Lisp_Object form, Lisp_Object environment)
962 /* With cleanups from Hallvard Furuseth. */
963 register Lisp_Object expander, sym, def, tem;
965 while (1)
967 /* Come back here each time we expand a macro call,
968 in case it expands into another macro call. */
969 if (!CONSP (form))
970 break;
971 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
972 def = sym = XCAR (form);
973 tem = Qnil;
974 /* Trace symbols aliases to other symbols
975 until we get a symbol that is not an alias. */
976 while (SYMBOLP (def))
978 QUIT;
979 sym = def;
980 tem = Fassq (sym, environment);
981 if (NILP (tem))
983 def = XSYMBOL (sym)->function;
984 if (!NILP (def))
985 continue;
987 break;
989 /* Right now TEM is the result from SYM in ENVIRONMENT,
990 and if TEM is nil then DEF is SYM's function definition. */
991 if (NILP (tem))
993 /* SYM is not mentioned in ENVIRONMENT.
994 Look at its function definition. */
995 def = Fautoload_do_load (def, sym, Qmacro);
996 if (!CONSP (def))
997 /* Not defined or definition not suitable. */
998 break;
999 if (!EQ (XCAR (def), Qmacro))
1000 break;
1001 else expander = XCDR (def);
1003 else
1005 expander = XCDR (tem);
1006 if (NILP (expander))
1007 break;
1010 Lisp_Object newform = apply1 (expander, XCDR (form));
1011 if (EQ (form, newform))
1012 break;
1013 else
1014 form = newform;
1017 return form;
1020 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1021 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1022 TAG is evalled to get the tag to use; it must not be nil.
1024 Then the BODY is executed.
1025 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1026 If no throw happens, `catch' returns the value of the last BODY form.
1027 If a throw happens, it specifies the value to return from `catch'.
1028 usage: (catch TAG BODY...) */)
1029 (Lisp_Object args)
1031 Lisp_Object tag = eval_sub (XCAR (args));
1032 return internal_catch (tag, Fprogn, XCDR (args));
1035 /* Assert that E is true, as a comment only. Use this instead of
1036 eassert (E) when E contains variables that might be clobbered by a
1037 longjmp. */
1039 #define clobbered_eassert(E) ((void) 0)
1041 /* Set up a catch, then call C function FUNC on argument ARG.
1042 FUNC should return a Lisp_Object.
1043 This is how catches are done from within C code. */
1045 Lisp_Object
1046 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1048 /* This structure is made part of the chain `catchlist'. */
1049 struct handler *c;
1051 /* Fill in the components of c, and put it on the list. */
1052 PUSH_HANDLER (c, tag, CATCHER);
1054 /* Call FUNC. */
1055 if (! sys_setjmp (c->jmp))
1057 Lisp_Object val = (*func) (arg);
1058 clobbered_eassert (handlerlist == c);
1059 handlerlist = handlerlist->next;
1060 return val;
1062 else
1063 { /* Throw works by a longjmp that comes right here. */
1064 Lisp_Object val = handlerlist->val;
1065 clobbered_eassert (handlerlist == c);
1066 handlerlist = handlerlist->next;
1067 return val;
1071 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1072 jump to that CATCH, returning VALUE as the value of that catch.
1074 This is the guts of Fthrow and Fsignal; they differ only in the way
1075 they choose the catch tag to throw to. A catch tag for a
1076 condition-case form has a TAG of Qnil.
1078 Before each catch is discarded, unbind all special bindings and
1079 execute all unwind-protect clauses made above that catch. Unwind
1080 the handler stack as we go, so that the proper handlers are in
1081 effect for each unwind-protect clause we run. At the end, restore
1082 some static info saved in CATCH, and longjmp to the location
1083 specified there.
1085 This is used for correct unwinding in Fthrow and Fsignal. */
1087 static _Noreturn void
1088 unwind_to_catch (struct handler *catch, Lisp_Object value)
1090 bool last_time;
1092 eassert (catch->next);
1094 /* Save the value in the tag. */
1095 catch->val = value;
1097 /* Restore certain special C variables. */
1098 set_poll_suppress_count (catch->poll_suppress_count);
1099 unblock_input_to (catch->interrupt_input_blocked);
1100 immediate_quit = 0;
1104 /* Unwind the specpdl stack, and then restore the proper set of
1105 handlers. */
1106 unbind_to (handlerlist->pdlcount, Qnil);
1107 last_time = handlerlist == catch;
1108 if (! last_time)
1109 handlerlist = handlerlist->next;
1111 while (! last_time);
1113 eassert (handlerlist == catch);
1115 byte_stack_list = catch->byte_stack;
1116 lisp_eval_depth = catch->lisp_eval_depth;
1118 sys_longjmp (catch->jmp, 1);
1121 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1122 doc: /* Throw to the catch for TAG and return VALUE from it.
1123 Both TAG and VALUE are evalled. */
1124 attributes: noreturn)
1125 (register Lisp_Object tag, Lisp_Object value)
1127 struct handler *c;
1129 if (!NILP (tag))
1130 for (c = handlerlist; c; c = c->next)
1132 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1133 unwind_to_catch (c, value);
1135 xsignal2 (Qno_catch, tag, value);
1139 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1140 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1141 If BODYFORM completes normally, its value is returned
1142 after executing the UNWINDFORMS.
1143 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1144 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1145 (Lisp_Object args)
1147 Lisp_Object val;
1148 ptrdiff_t count = SPECPDL_INDEX ();
1150 record_unwind_protect (unwind_body, XCDR (args));
1151 val = eval_sub (XCAR (args));
1152 return unbind_to (count, val);
1155 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1156 doc: /* Regain control when an error is signaled.
1157 Executes BODYFORM and returns its value if no error happens.
1158 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1159 where the BODY is made of Lisp expressions.
1161 A handler is applicable to an error
1162 if CONDITION-NAME is one of the error's condition names.
1163 If an error happens, the first applicable handler is run.
1165 The car of a handler may be a list of condition names instead of a
1166 single condition name; then it handles all of them. If the special
1167 condition name `debug' is present in this list, it allows another
1168 condition in the list to run the debugger if `debug-on-error' and the
1169 other usual mechanisms says it should (otherwise, `condition-case'
1170 suppresses the debugger).
1172 When a handler handles an error, control returns to the `condition-case'
1173 and it executes the handler's BODY...
1174 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1175 \(If VAR is nil, the handler can't access that information.)
1176 Then the value of the last BODY form is returned from the `condition-case'
1177 expression.
1179 See also the function `signal' for more info.
1180 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1181 (Lisp_Object args)
1183 Lisp_Object var = XCAR (args);
1184 Lisp_Object bodyform = XCAR (XCDR (args));
1185 Lisp_Object handlers = XCDR (XCDR (args));
1187 return internal_lisp_condition_case (var, bodyform, handlers);
1190 /* Like Fcondition_case, but the args are separate
1191 rather than passed in a list. Used by Fbyte_code. */
1193 Lisp_Object
1194 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1195 Lisp_Object handlers)
1197 Lisp_Object val;
1198 struct handler *c;
1199 struct handler *oldhandlerlist = handlerlist;
1200 int clausenb = 0;
1202 CHECK_SYMBOL (var);
1204 for (val = handlers; CONSP (val); val = XCDR (val))
1206 Lisp_Object tem = XCAR (val);
1207 clausenb++;
1208 if (! (NILP (tem)
1209 || (CONSP (tem)
1210 && (SYMBOLP (XCAR (tem))
1211 || CONSP (XCAR (tem))))))
1212 error ("Invalid condition handler: %s",
1213 SDATA (Fprin1_to_string (tem, Qt)));
1216 { /* The first clause is the one that should be checked first, so it should
1217 be added to handlerlist last. So we build in `clauses' a table that
1218 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1219 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1220 if (MAX_ALLOCA / word_size < clausenb)
1221 memory_full (SIZE_MAX);
1222 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1223 Lisp_Object *volatile clauses_volatile = clauses;
1224 int i = clausenb;
1225 for (val = handlers; CONSP (val); val = XCDR (val))
1226 clauses[--i] = XCAR (val);
1227 for (i = 0; i < clausenb; i++)
1229 Lisp_Object clause = clauses[i];
1230 Lisp_Object condition = XCAR (clause);
1231 if (!CONSP (condition))
1232 condition = Fcons (condition, Qnil);
1233 PUSH_HANDLER (c, condition, CONDITION_CASE);
1234 if (sys_setjmp (c->jmp))
1236 ptrdiff_t count = SPECPDL_INDEX ();
1237 Lisp_Object val = handlerlist->val;
1238 Lisp_Object *chosen_clause = clauses_volatile;
1239 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1240 chosen_clause++;
1241 handlerlist = oldhandlerlist;
1242 if (!NILP (var))
1244 if (!NILP (Vinternal_interpreter_environment))
1245 specbind (Qinternal_interpreter_environment,
1246 Fcons (Fcons (var, val),
1247 Vinternal_interpreter_environment));
1248 else
1249 specbind (var, val);
1251 val = Fprogn (XCDR (*chosen_clause));
1252 /* Note that this just undoes the binding of var; whoever
1253 longjumped to us unwound the stack to c.pdlcount before
1254 throwing. */
1255 if (!NILP (var))
1256 unbind_to (count, Qnil);
1257 return val;
1262 val = eval_sub (bodyform);
1263 handlerlist = oldhandlerlist;
1264 return val;
1267 /* Call the function BFUN with no arguments, catching errors within it
1268 according to HANDLERS. If there is an error, call HFUN with
1269 one argument which is the data that describes the error:
1270 (SIGNALNAME . DATA)
1272 HANDLERS can be a list of conditions to catch.
1273 If HANDLERS is Qt, catch all errors.
1274 If HANDLERS is Qerror, catch all errors
1275 but allow the debugger to run if that is enabled. */
1277 Lisp_Object
1278 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1279 Lisp_Object (*hfun) (Lisp_Object))
1281 Lisp_Object val;
1282 struct handler *c;
1284 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1285 if (sys_setjmp (c->jmp))
1287 Lisp_Object val = handlerlist->val;
1288 clobbered_eassert (handlerlist == c);
1289 handlerlist = handlerlist->next;
1290 return (*hfun) (val);
1293 val = (*bfun) ();
1294 clobbered_eassert (handlerlist == c);
1295 handlerlist = handlerlist->next;
1296 return val;
1299 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1301 Lisp_Object
1302 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1303 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1305 Lisp_Object val;
1306 struct handler *c;
1308 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1309 if (sys_setjmp (c->jmp))
1311 Lisp_Object val = handlerlist->val;
1312 clobbered_eassert (handlerlist == c);
1313 handlerlist = handlerlist->next;
1314 return (*hfun) (val);
1317 val = (*bfun) (arg);
1318 clobbered_eassert (handlerlist == c);
1319 handlerlist = handlerlist->next;
1320 return val;
1323 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1324 its arguments. */
1326 Lisp_Object
1327 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1328 Lisp_Object arg1,
1329 Lisp_Object arg2,
1330 Lisp_Object handlers,
1331 Lisp_Object (*hfun) (Lisp_Object))
1333 Lisp_Object val;
1334 struct handler *c;
1336 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1337 if (sys_setjmp (c->jmp))
1339 Lisp_Object val = handlerlist->val;
1340 clobbered_eassert (handlerlist == c);
1341 handlerlist = handlerlist->next;
1342 return (*hfun) (val);
1345 val = (*bfun) (arg1, arg2);
1346 clobbered_eassert (handlerlist == c);
1347 handlerlist = handlerlist->next;
1348 return val;
1351 /* Like internal_condition_case but call BFUN with NARGS as first,
1352 and ARGS as second argument. */
1354 Lisp_Object
1355 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1356 ptrdiff_t nargs,
1357 Lisp_Object *args,
1358 Lisp_Object handlers,
1359 Lisp_Object (*hfun) (Lisp_Object err,
1360 ptrdiff_t nargs,
1361 Lisp_Object *args))
1363 Lisp_Object val;
1364 struct handler *c;
1366 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1367 if (sys_setjmp (c->jmp))
1369 Lisp_Object val = handlerlist->val;
1370 clobbered_eassert (handlerlist == c);
1371 handlerlist = handlerlist->next;
1372 return (*hfun) (val, nargs, args);
1375 val = (*bfun) (nargs, args);
1376 clobbered_eassert (handlerlist == c);
1377 handlerlist = handlerlist->next;
1378 return val;
1382 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1383 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1384 Lisp_Object data);
1386 void
1387 process_quit_flag (void)
1389 Lisp_Object flag = Vquit_flag;
1390 Vquit_flag = Qnil;
1391 if (EQ (flag, Qkill_emacs))
1392 Fkill_emacs (Qnil);
1393 if (EQ (Vthrow_on_input, flag))
1394 Fthrow (Vthrow_on_input, Qt);
1395 Fsignal (Qquit, Qnil);
1398 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1399 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1400 This function does not return.
1402 An error symbol is a symbol with an `error-conditions' property
1403 that is a list of condition names.
1404 A handler for any of those names will get to handle this signal.
1405 The symbol `error' should normally be one of them.
1407 DATA should be a list. Its elements are printed as part of the error message.
1408 See Info anchor `(elisp)Definition of signal' for some details on how this
1409 error message is constructed.
1410 If the signal is handled, DATA is made available to the handler.
1411 See also the function `condition-case'. */)
1412 (Lisp_Object error_symbol, Lisp_Object data)
1414 /* When memory is full, ERROR-SYMBOL is nil,
1415 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1416 That is a special case--don't do this in other situations. */
1417 Lisp_Object conditions;
1418 Lisp_Object string;
1419 Lisp_Object real_error_symbol
1420 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1421 register Lisp_Object clause = Qnil;
1422 struct handler *h;
1424 immediate_quit = 0;
1425 abort_on_gc = 0;
1426 if (gc_in_progress || waiting_for_input)
1427 emacs_abort ();
1429 #if 0 /* rms: I don't know why this was here,
1430 but it is surely wrong for an error that is handled. */
1431 #ifdef HAVE_WINDOW_SYSTEM
1432 if (display_hourglass_p)
1433 cancel_hourglass ();
1434 #endif
1435 #endif
1437 /* This hook is used by edebug. */
1438 if (! NILP (Vsignal_hook_function)
1439 && ! NILP (error_symbol))
1441 /* Edebug takes care of restoring these variables when it exits. */
1442 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1443 max_lisp_eval_depth = lisp_eval_depth + 20;
1445 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1446 max_specpdl_size = SPECPDL_INDEX () + 40;
1448 call2 (Vsignal_hook_function, error_symbol, data);
1451 conditions = Fget (real_error_symbol, Qerror_conditions);
1453 /* Remember from where signal was called. Skip over the frame for
1454 `signal' itself. If a frame for `error' follows, skip that,
1455 too. Don't do this when ERROR_SYMBOL is nil, because that
1456 is a memory-full error. */
1457 Vsignaling_function = Qnil;
1458 if (!NILP (error_symbol))
1460 union specbinding *pdl = backtrace_next (backtrace_top ());
1461 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1462 pdl = backtrace_next (pdl);
1463 if (backtrace_p (pdl))
1464 Vsignaling_function = backtrace_function (pdl);
1467 for (h = handlerlist; h; h = h->next)
1469 if (h->type != CONDITION_CASE)
1470 continue;
1471 clause = find_handler_clause (h->tag_or_ch, conditions);
1472 if (!NILP (clause))
1473 break;
1476 if (/* Don't run the debugger for a memory-full error.
1477 (There is no room in memory to do that!) */
1478 !NILP (error_symbol)
1479 && (!NILP (Vdebug_on_signal)
1480 /* If no handler is present now, try to run the debugger. */
1481 || NILP (clause)
1482 /* A `debug' symbol in the handler list disables the normal
1483 suppression of the debugger. */
1484 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1485 /* Special handler that means "print a message and run debugger
1486 if requested". */
1487 || EQ (h->tag_or_ch, Qerror)))
1489 bool debugger_called
1490 = maybe_call_debugger (conditions, error_symbol, data);
1491 /* We can't return values to code which signaled an error, but we
1492 can continue code which has signaled a quit. */
1493 if (debugger_called && EQ (real_error_symbol, Qquit))
1494 return Qnil;
1497 if (!NILP (clause))
1499 Lisp_Object unwind_data
1500 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1502 unwind_to_catch (h, unwind_data);
1504 else
1506 if (handlerlist != &handlerlist_sentinel)
1507 /* FIXME: This will come right back here if there's no `top-level'
1508 catcher. A better solution would be to abort here, and instead
1509 add a catch-all condition handler so we never come here. */
1510 Fthrow (Qtop_level, Qt);
1513 if (! NILP (error_symbol))
1514 data = Fcons (error_symbol, data);
1516 string = Ferror_message_string (data);
1517 fatal ("%s", SDATA (string));
1520 /* Internal version of Fsignal that never returns.
1521 Used for anything but Qquit (which can return from Fsignal). */
1523 void
1524 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1526 Fsignal (error_symbol, data);
1527 emacs_abort ();
1530 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1532 void
1533 xsignal0 (Lisp_Object error_symbol)
1535 xsignal (error_symbol, Qnil);
1538 void
1539 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1541 xsignal (error_symbol, list1 (arg));
1544 void
1545 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1547 xsignal (error_symbol, list2 (arg1, arg2));
1550 void
1551 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1553 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1556 /* Signal `error' with message S, and additional arg ARG.
1557 If ARG is not a genuine list, make it a one-element list. */
1559 void
1560 signal_error (const char *s, Lisp_Object arg)
1562 Lisp_Object tortoise, hare;
1564 hare = tortoise = arg;
1565 while (CONSP (hare))
1567 hare = XCDR (hare);
1568 if (!CONSP (hare))
1569 break;
1571 hare = XCDR (hare);
1572 tortoise = XCDR (tortoise);
1574 if (EQ (hare, tortoise))
1575 break;
1578 if (!NILP (hare))
1579 arg = list1 (arg);
1581 xsignal (Qerror, Fcons (build_string (s), arg));
1585 /* Return true if LIST is a non-nil atom or
1586 a list containing one of CONDITIONS. */
1588 static bool
1589 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1591 if (NILP (list))
1592 return 0;
1593 if (! CONSP (list))
1594 return 1;
1596 while (CONSP (conditions))
1598 Lisp_Object this, tail;
1599 this = XCAR (conditions);
1600 for (tail = list; CONSP (tail); tail = XCDR (tail))
1601 if (EQ (XCAR (tail), this))
1602 return 1;
1603 conditions = XCDR (conditions);
1605 return 0;
1608 /* Return true if an error with condition-symbols CONDITIONS,
1609 and described by SIGNAL-DATA, should skip the debugger
1610 according to debugger-ignored-errors. */
1612 static bool
1613 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1615 Lisp_Object tail;
1616 bool first_string = 1;
1617 Lisp_Object error_message;
1619 error_message = Qnil;
1620 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1622 if (STRINGP (XCAR (tail)))
1624 if (first_string)
1626 error_message = Ferror_message_string (data);
1627 first_string = 0;
1630 if (fast_string_match (XCAR (tail), error_message) >= 0)
1631 return 1;
1633 else
1635 Lisp_Object contail;
1637 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1638 if (EQ (XCAR (tail), XCAR (contail)))
1639 return 1;
1643 return 0;
1646 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1647 SIG and DATA describe the signal. There are two ways to pass them:
1648 = SIG is the error symbol, and DATA is the rest of the data.
1649 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1650 This is for memory-full errors only. */
1651 static bool
1652 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1654 Lisp_Object combined_data;
1656 combined_data = Fcons (sig, data);
1658 if (
1659 /* Don't try to run the debugger with interrupts blocked.
1660 The editing loop would return anyway. */
1661 ! input_blocked_p ()
1662 && NILP (Vinhibit_debugger)
1663 /* Does user want to enter debugger for this kind of error? */
1664 && (EQ (sig, Qquit)
1665 ? debug_on_quit
1666 : wants_debugger (Vdebug_on_error, conditions))
1667 && ! skip_debugger (conditions, combined_data)
1668 /* RMS: What's this for? */
1669 && when_entered_debugger < num_nonmacro_input_events)
1671 call_debugger (list2 (Qerror, combined_data));
1672 return 1;
1675 return 0;
1678 static Lisp_Object
1679 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1681 register Lisp_Object h;
1683 /* t is used by handlers for all conditions, set up by C code. */
1684 if (EQ (handlers, Qt))
1685 return Qt;
1687 /* error is used similarly, but means print an error message
1688 and run the debugger if that is enabled. */
1689 if (EQ (handlers, Qerror))
1690 return Qt;
1692 for (h = handlers; CONSP (h); h = XCDR (h))
1694 Lisp_Object handler = XCAR (h);
1695 if (!NILP (Fmemq (handler, conditions)))
1696 return handlers;
1699 return Qnil;
1703 /* Dump an error message; called like vprintf. */
1704 void
1705 verror (const char *m, va_list ap)
1707 char buf[4000];
1708 ptrdiff_t size = sizeof buf;
1709 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1710 char *buffer = buf;
1711 ptrdiff_t used;
1712 Lisp_Object string;
1714 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1715 string = make_string (buffer, used);
1716 if (buffer != buf)
1717 xfree (buffer);
1719 xsignal1 (Qerror, string);
1723 /* Dump an error message; called like printf. */
1725 /* VARARGS 1 */
1726 void
1727 error (const char *m, ...)
1729 va_list ap;
1730 va_start (ap, m);
1731 verror (m, ap);
1734 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1735 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1736 This means it contains a description for how to read arguments to give it.
1737 The value is nil for an invalid function or a symbol with no function
1738 definition.
1740 Interactively callable functions include strings and vectors (treated
1741 as keyboard macros), lambda-expressions that contain a top-level call
1742 to `interactive', autoload definitions made by `autoload' with non-nil
1743 fourth argument, and some of the built-in functions of Lisp.
1745 Also, a symbol satisfies `commandp' if its function definition does so.
1747 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1748 then strings and vectors are not accepted. */)
1749 (Lisp_Object function, Lisp_Object for_call_interactively)
1751 register Lisp_Object fun;
1752 register Lisp_Object funcar;
1753 Lisp_Object if_prop = Qnil;
1755 fun = function;
1757 fun = indirect_function (fun); /* Check cycles. */
1758 if (NILP (fun))
1759 return Qnil;
1761 /* Check an `interactive-form' property if present, analogous to the
1762 function-documentation property. */
1763 fun = function;
1764 while (SYMBOLP (fun))
1766 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1767 if (!NILP (tmp))
1768 if_prop = Qt;
1769 fun = Fsymbol_function (fun);
1772 /* Emacs primitives are interactive if their DEFUN specifies an
1773 interactive spec. */
1774 if (SUBRP (fun))
1775 return XSUBR (fun)->intspec ? Qt : if_prop;
1777 /* Bytecode objects are interactive if they are long enough to
1778 have an element whose index is COMPILED_INTERACTIVE, which is
1779 where the interactive spec is stored. */
1780 else if (COMPILEDP (fun))
1781 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1782 ? Qt : if_prop);
1784 /* Strings and vectors are keyboard macros. */
1785 if (STRINGP (fun) || VECTORP (fun))
1786 return (NILP (for_call_interactively) ? Qt : Qnil);
1788 /* Lists may represent commands. */
1789 if (!CONSP (fun))
1790 return Qnil;
1791 funcar = XCAR (fun);
1792 if (EQ (funcar, Qclosure))
1793 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1794 ? Qt : if_prop);
1795 else if (EQ (funcar, Qlambda))
1796 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1797 else if (EQ (funcar, Qautoload))
1798 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1799 else
1800 return Qnil;
1803 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1804 doc: /* Define FUNCTION to autoload from FILE.
1805 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1806 Third arg DOCSTRING is documentation for the function.
1807 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1808 Fifth arg TYPE indicates the type of the object:
1809 nil or omitted says FUNCTION is a function,
1810 `keymap' says FUNCTION is really a keymap, and
1811 `macro' or t says FUNCTION is really a macro.
1812 Third through fifth args give info about the real definition.
1813 They default to nil.
1814 If FUNCTION is already defined other than as an autoload,
1815 this does nothing and returns nil. */)
1816 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1818 CHECK_SYMBOL (function);
1819 CHECK_STRING (file);
1821 /* If function is defined and not as an autoload, don't override. */
1822 if (!NILP (XSYMBOL (function)->function)
1823 && !AUTOLOADP (XSYMBOL (function)->function))
1824 return Qnil;
1826 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1827 /* `read1' in lread.c has found the docstring starting with "\
1828 and assumed the docstring will be provided by Snarf-documentation, so it
1829 passed us 0 instead. But that leads to accidental sharing in purecopy's
1830 hash-consing, so we use a (hopefully) unique integer instead. */
1831 docstring = make_number (XHASH (function));
1832 return Fdefalias (function,
1833 list5 (Qautoload, file, docstring, interactive, type),
1834 Qnil);
1837 void
1838 un_autoload (Lisp_Object oldqueue)
1840 Lisp_Object queue, first, second;
1842 /* Queue to unwind is current value of Vautoload_queue.
1843 oldqueue is the shadowed value to leave in Vautoload_queue. */
1844 queue = Vautoload_queue;
1845 Vautoload_queue = oldqueue;
1846 while (CONSP (queue))
1848 first = XCAR (queue);
1849 second = Fcdr (first);
1850 first = Fcar (first);
1851 if (EQ (first, make_number (0)))
1852 Vfeatures = second;
1853 else
1854 Ffset (first, second);
1855 queue = XCDR (queue);
1859 /* Load an autoloaded function.
1860 FUNNAME is the symbol which is the function's name.
1861 FUNDEF is the autoload definition (a list). */
1863 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1864 doc: /* Load FUNDEF which should be an autoload.
1865 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1866 in which case the function returns the new autoloaded function value.
1867 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1868 it defines a macro. */)
1869 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1871 ptrdiff_t count = SPECPDL_INDEX ();
1873 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1874 return fundef;
1876 if (EQ (macro_only, Qmacro))
1878 Lisp_Object kind = Fnth (make_number (4), fundef);
1879 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1880 return fundef;
1883 /* This is to make sure that loadup.el gives a clear picture
1884 of what files are preloaded and when. */
1885 if (! NILP (Vpurify_flag))
1886 error ("Attempt to autoload %s while preparing to dump",
1887 SDATA (SYMBOL_NAME (funname)));
1889 CHECK_SYMBOL (funname);
1891 /* Preserve the match data. */
1892 record_unwind_save_match_data ();
1894 /* If autoloading gets an error (which includes the error of failing
1895 to define the function being called), we use Vautoload_queue
1896 to undo function definitions and `provide' calls made by
1897 the function. We do this in the specific case of autoloading
1898 because autoloading is not an explicit request "load this file",
1899 but rather a request to "call this function".
1901 The value saved here is to be restored into Vautoload_queue. */
1902 record_unwind_protect (un_autoload, Vautoload_queue);
1903 Vautoload_queue = Qt;
1904 /* If `macro_only', assume this autoload to be a "best-effort",
1905 so don't signal an error if autoloading fails. */
1906 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1908 /* Once loading finishes, don't undo it. */
1909 Vautoload_queue = Qt;
1910 unbind_to (count, Qnil);
1912 if (NILP (funname))
1913 return Qnil;
1914 else
1916 Lisp_Object fun = Findirect_function (funname, Qnil);
1918 if (!NILP (Fequal (fun, fundef)))
1919 error ("Autoloading failed to define function %s",
1920 SDATA (SYMBOL_NAME (funname)));
1921 else
1922 return fun;
1927 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1928 doc: /* Evaluate FORM and return its value.
1929 If LEXICAL is t, evaluate using lexical scoping.
1930 LEXICAL can also be an actual lexical environment, in the form of an
1931 alist mapping symbols to their value. */)
1932 (Lisp_Object form, Lisp_Object lexical)
1934 ptrdiff_t count = SPECPDL_INDEX ();
1935 specbind (Qinternal_interpreter_environment,
1936 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1937 return unbind_to (count, eval_sub (form));
1940 /* Grow the specpdl stack by one entry.
1941 The caller should have already initialized the entry.
1942 Signal an error on stack overflow.
1944 Make sure that there is always one unused entry past the top of the
1945 stack, so that the just-initialized entry is safely unwound if
1946 memory exhausted and an error is signaled here. Also, allocate a
1947 never-used entry just before the bottom of the stack; sometimes its
1948 address is taken. */
1950 static void
1951 grow_specpdl (void)
1953 specpdl_ptr++;
1955 if (specpdl_ptr == specpdl + specpdl_size)
1957 ptrdiff_t count = SPECPDL_INDEX ();
1958 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
1959 union specbinding *pdlvec = specpdl - 1;
1960 ptrdiff_t pdlvecsize = specpdl_size + 1;
1961 if (max_size <= specpdl_size)
1963 if (max_specpdl_size < 400)
1964 max_size = max_specpdl_size = 400;
1965 if (max_size <= specpdl_size)
1966 signal_error ("Variable binding depth exceeds max-specpdl-size",
1967 Qnil);
1969 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
1970 specpdl = pdlvec + 1;
1971 specpdl_size = pdlvecsize - 1;
1972 specpdl_ptr = specpdl + count;
1976 ptrdiff_t
1977 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
1979 ptrdiff_t count = SPECPDL_INDEX ();
1981 eassert (nargs >= UNEVALLED);
1982 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
1983 specpdl_ptr->bt.debug_on_exit = false;
1984 specpdl_ptr->bt.function = function;
1985 specpdl_ptr->bt.args = args;
1986 specpdl_ptr->bt.nargs = nargs;
1987 grow_specpdl ();
1989 return count;
1992 /* Eval a sub-expression of the current expression (i.e. in the same
1993 lexical scope). */
1994 Lisp_Object
1995 eval_sub (Lisp_Object form)
1997 Lisp_Object fun, val, original_fun, original_args;
1998 Lisp_Object funcar;
1999 ptrdiff_t count;
2001 if (SYMBOLP (form))
2003 /* Look up its binding in the lexical environment.
2004 We do not pay attention to the declared_special flag here, since we
2005 already did that when let-binding the variable. */
2006 Lisp_Object lex_binding
2007 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2008 ? Fassq (form, Vinternal_interpreter_environment)
2009 : Qnil;
2010 if (CONSP (lex_binding))
2011 return XCDR (lex_binding);
2012 else
2013 return Fsymbol_value (form);
2016 if (!CONSP (form))
2017 return form;
2019 QUIT;
2021 maybe_gc ();
2023 if (++lisp_eval_depth > max_lisp_eval_depth)
2025 if (max_lisp_eval_depth < 100)
2026 max_lisp_eval_depth = 100;
2027 if (lisp_eval_depth > max_lisp_eval_depth)
2028 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2031 original_fun = XCAR (form);
2032 original_args = XCDR (form);
2034 /* This also protects them from gc. */
2035 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2037 if (debug_on_next_call)
2038 do_debug_on_call (Qt, count);
2040 /* At this point, only original_fun and original_args
2041 have values that will be used below. */
2042 retry:
2044 /* Optimize for no indirection. */
2045 fun = original_fun;
2046 if (!SYMBOLP (fun))
2047 fun = Ffunction (Fcons (fun, Qnil));
2048 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2049 fun = indirect_function (fun);
2051 if (SUBRP (fun))
2053 Lisp_Object numargs;
2054 Lisp_Object argvals[8];
2055 Lisp_Object args_left;
2056 register int i, maxargs;
2058 args_left = original_args;
2059 numargs = Flength (args_left);
2061 check_cons_list ();
2063 if (XINT (numargs) < XSUBR (fun)->min_args
2064 || (XSUBR (fun)->max_args >= 0
2065 && XSUBR (fun)->max_args < XINT (numargs)))
2066 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2068 else if (XSUBR (fun)->max_args == UNEVALLED)
2069 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2070 else if (XSUBR (fun)->max_args == MANY)
2072 /* Pass a vector of evaluated arguments. */
2073 Lisp_Object *vals;
2074 ptrdiff_t argnum = 0;
2075 USE_SAFE_ALLOCA;
2077 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2079 while (!NILP (args_left))
2081 vals[argnum++] = eval_sub (Fcar (args_left));
2082 args_left = Fcdr (args_left);
2085 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2087 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2088 SAFE_FREE ();
2090 else
2092 maxargs = XSUBR (fun)->max_args;
2093 for (i = 0; i < maxargs; i++)
2095 argvals[i] = eval_sub (Fcar (args_left));
2096 args_left = Fcdr (args_left);
2099 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2101 switch (i)
2103 case 0:
2104 val = (XSUBR (fun)->function.a0 ());
2105 break;
2106 case 1:
2107 val = (XSUBR (fun)->function.a1 (argvals[0]));
2108 break;
2109 case 2:
2110 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2111 break;
2112 case 3:
2113 val = (XSUBR (fun)->function.a3
2114 (argvals[0], argvals[1], argvals[2]));
2115 break;
2116 case 4:
2117 val = (XSUBR (fun)->function.a4
2118 (argvals[0], argvals[1], argvals[2], argvals[3]));
2119 break;
2120 case 5:
2121 val = (XSUBR (fun)->function.a5
2122 (argvals[0], argvals[1], argvals[2], argvals[3],
2123 argvals[4]));
2124 break;
2125 case 6:
2126 val = (XSUBR (fun)->function.a6
2127 (argvals[0], argvals[1], argvals[2], argvals[3],
2128 argvals[4], argvals[5]));
2129 break;
2130 case 7:
2131 val = (XSUBR (fun)->function.a7
2132 (argvals[0], argvals[1], argvals[2], argvals[3],
2133 argvals[4], argvals[5], argvals[6]));
2134 break;
2136 case 8:
2137 val = (XSUBR (fun)->function.a8
2138 (argvals[0], argvals[1], argvals[2], argvals[3],
2139 argvals[4], argvals[5], argvals[6], argvals[7]));
2140 break;
2142 default:
2143 /* Someone has created a subr that takes more arguments than
2144 is supported by this code. We need to either rewrite the
2145 subr to use a different argument protocol, or add more
2146 cases to this switch. */
2147 emacs_abort ();
2151 else if (COMPILEDP (fun))
2152 val = apply_lambda (fun, original_args, count);
2153 else
2155 if (NILP (fun))
2156 xsignal1 (Qvoid_function, original_fun);
2157 if (!CONSP (fun))
2158 xsignal1 (Qinvalid_function, original_fun);
2159 funcar = XCAR (fun);
2160 if (!SYMBOLP (funcar))
2161 xsignal1 (Qinvalid_function, original_fun);
2162 if (EQ (funcar, Qautoload))
2164 Fautoload_do_load (fun, original_fun, Qnil);
2165 goto retry;
2167 if (EQ (funcar, Qmacro))
2169 ptrdiff_t count1 = SPECPDL_INDEX ();
2170 Lisp_Object exp;
2171 /* Bind lexical-binding during expansion of the macro, so the
2172 macro can know reliably if the code it outputs will be
2173 interpreted using lexical-binding or not. */
2174 specbind (Qlexical_binding,
2175 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2176 exp = apply1 (Fcdr (fun), original_args);
2177 unbind_to (count1, Qnil);
2178 val = eval_sub (exp);
2180 else if (EQ (funcar, Qlambda)
2181 || EQ (funcar, Qclosure))
2182 val = apply_lambda (fun, original_args, count);
2183 else
2184 xsignal1 (Qinvalid_function, original_fun);
2186 check_cons_list ();
2188 lisp_eval_depth--;
2189 if (backtrace_debug_on_exit (specpdl + count))
2190 val = call_debugger (list2 (Qexit, val));
2191 specpdl_ptr--;
2193 return val;
2196 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2197 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2198 Then return the value FUNCTION returns.
2199 Thus, (apply \\='+ 1 2 \\='(3 4)) returns 10.
2200 usage: (apply FUNCTION &rest ARGUMENTS) */)
2201 (ptrdiff_t nargs, Lisp_Object *args)
2203 ptrdiff_t i, numargs, funcall_nargs;
2204 register Lisp_Object *funcall_args = NULL;
2205 register Lisp_Object spread_arg = args[nargs - 1];
2206 Lisp_Object fun = args[0];
2207 Lisp_Object retval;
2208 USE_SAFE_ALLOCA;
2210 CHECK_LIST (spread_arg);
2212 numargs = XINT (Flength (spread_arg));
2214 if (numargs == 0)
2215 return Ffuncall (nargs - 1, args);
2216 else if (numargs == 1)
2218 args [nargs - 1] = XCAR (spread_arg);
2219 return Ffuncall (nargs, args);
2222 numargs += nargs - 2;
2224 /* Optimize for no indirection. */
2225 if (SYMBOLP (fun) && !NILP (fun)
2226 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2228 fun = indirect_function (fun);
2229 if (NILP (fun))
2230 /* Let funcall get the error. */
2231 fun = args[0];
2234 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2235 /* Don't hide an error by adding missing arguments. */
2236 && numargs >= XSUBR (fun)->min_args)
2238 /* Avoid making funcall cons up a yet another new vector of arguments
2239 by explicitly supplying nil's for optional values. */
2240 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2241 memclear (funcall_args + numargs + 1,
2242 (XSUBR (fun)->max_args - numargs) * word_size);
2243 funcall_nargs = 1 + XSUBR (fun)->max_args;
2245 else
2246 { /* We add 1 to numargs because funcall_args includes the
2247 function itself as well as its arguments. */
2248 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2249 funcall_nargs = 1 + numargs;
2252 memcpy (funcall_args, args, nargs * word_size);
2253 /* Spread the last arg we got. Its first element goes in
2254 the slot that it used to occupy, hence this value of I. */
2255 i = nargs - 1;
2256 while (!NILP (spread_arg))
2258 funcall_args [i++] = XCAR (spread_arg);
2259 spread_arg = XCDR (spread_arg);
2262 retval = Ffuncall (funcall_nargs, funcall_args);
2264 SAFE_FREE ();
2265 return retval;
2268 /* Run hook variables in various ways. */
2270 static Lisp_Object
2271 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2273 Ffuncall (nargs, args);
2274 return Qnil;
2277 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2278 doc: /* Run each hook in HOOKS.
2279 Each argument should be a symbol, a hook variable.
2280 These symbols are processed in the order specified.
2281 If a hook symbol has a non-nil value, that value may be a function
2282 or a list of functions to be called to run the hook.
2283 If the value is a function, it is called with no arguments.
2284 If it is a list, the elements are called, in order, with no arguments.
2286 Major modes should not use this function directly to run their mode
2287 hook; they should use `run-mode-hooks' instead.
2289 Do not use `make-local-variable' to make a hook variable buffer-local.
2290 Instead, use `add-hook' and specify t for the LOCAL argument.
2291 usage: (run-hooks &rest HOOKS) */)
2292 (ptrdiff_t nargs, Lisp_Object *args)
2294 ptrdiff_t i;
2296 for (i = 0; i < nargs; i++)
2297 run_hook (args[i]);
2299 return Qnil;
2302 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2303 Srun_hook_with_args, 1, MANY, 0,
2304 doc: /* Run HOOK with the specified arguments ARGS.
2305 HOOK should be a symbol, a hook variable. The value of HOOK
2306 may be nil, a function, or a list of functions. Call each
2307 function in order with arguments ARGS. The final return value
2308 is unspecified.
2310 Do not use `make-local-variable' to make a hook variable buffer-local.
2311 Instead, use `add-hook' and specify t for the LOCAL argument.
2312 usage: (run-hook-with-args HOOK &rest ARGS) */)
2313 (ptrdiff_t nargs, Lisp_Object *args)
2315 return run_hook_with_args (nargs, args, funcall_nil);
2318 /* NB this one still documents a specific non-nil return value.
2319 (As did run-hook-with-args and run-hook-with-args-until-failure
2320 until they were changed in 24.1.) */
2321 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2322 Srun_hook_with_args_until_success, 1, MANY, 0,
2323 doc: /* Run HOOK with the specified arguments ARGS.
2324 HOOK should be a symbol, a hook variable. The value of HOOK
2325 may be nil, a function, or a list of functions. Call each
2326 function in order with arguments ARGS, stopping at the first
2327 one that returns non-nil, and return that value. Otherwise (if
2328 all functions return nil, or if there are no functions to call),
2329 return nil.
2331 Do not use `make-local-variable' to make a hook variable buffer-local.
2332 Instead, use `add-hook' and specify t for the LOCAL argument.
2333 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2334 (ptrdiff_t nargs, Lisp_Object *args)
2336 return run_hook_with_args (nargs, args, Ffuncall);
2339 static Lisp_Object
2340 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2342 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2345 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2346 Srun_hook_with_args_until_failure, 1, MANY, 0,
2347 doc: /* Run HOOK with the specified arguments ARGS.
2348 HOOK should be a symbol, a hook variable. The value of HOOK
2349 may be nil, a function, or a list of functions. Call each
2350 function in order with arguments ARGS, stopping at the first
2351 one that returns nil, and return nil. Otherwise (if all functions
2352 return non-nil, or if there are no functions to call), return non-nil
2353 \(do not rely on the precise return value in this case).
2355 Do not use `make-local-variable' to make a hook variable buffer-local.
2356 Instead, use `add-hook' and specify t for the LOCAL argument.
2357 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2358 (ptrdiff_t nargs, Lisp_Object *args)
2360 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2363 static Lisp_Object
2364 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2366 Lisp_Object tmp = args[0], ret;
2367 args[0] = args[1];
2368 args[1] = tmp;
2369 ret = Ffuncall (nargs, args);
2370 args[1] = args[0];
2371 args[0] = tmp;
2372 return ret;
2375 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2376 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2377 I.e. instead of calling each function FUN directly with arguments ARGS,
2378 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2379 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2380 aborts and returns that value.
2381 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2382 (ptrdiff_t nargs, Lisp_Object *args)
2384 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2387 /* ARGS[0] should be a hook symbol.
2388 Call each of the functions in the hook value, passing each of them
2389 as arguments all the rest of ARGS (all NARGS - 1 elements).
2390 FUNCALL specifies how to call each function on the hook. */
2392 Lisp_Object
2393 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2394 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2396 Lisp_Object sym, val, ret = Qnil;
2398 /* If we are dying or still initializing,
2399 don't do anything--it would probably crash if we tried. */
2400 if (NILP (Vrun_hooks))
2401 return Qnil;
2403 sym = args[0];
2404 val = find_symbol_value (sym);
2406 if (EQ (val, Qunbound) || NILP (val))
2407 return ret;
2408 else if (!CONSP (val) || FUNCTIONP (val))
2410 args[0] = val;
2411 return funcall (nargs, args);
2413 else
2415 Lisp_Object global_vals = Qnil;
2417 for (;
2418 CONSP (val) && NILP (ret);
2419 val = XCDR (val))
2421 if (EQ (XCAR (val), Qt))
2423 /* t indicates this hook has a local binding;
2424 it means to run the global binding too. */
2425 global_vals = Fdefault_value (sym);
2426 if (NILP (global_vals)) continue;
2428 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2430 args[0] = global_vals;
2431 ret = funcall (nargs, args);
2433 else
2435 for (;
2436 CONSP (global_vals) && NILP (ret);
2437 global_vals = XCDR (global_vals))
2439 args[0] = XCAR (global_vals);
2440 /* In a global value, t should not occur. If it does, we
2441 must ignore it to avoid an endless loop. */
2442 if (!EQ (args[0], Qt))
2443 ret = funcall (nargs, args);
2447 else
2449 args[0] = XCAR (val);
2450 ret = funcall (nargs, args);
2454 return ret;
2458 /* Run the hook HOOK, giving each function no args. */
2460 void
2461 run_hook (Lisp_Object hook)
2463 Frun_hook_with_args (1, &hook);
2466 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2468 void
2469 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2471 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2474 /* Apply fn to arg. */
2475 Lisp_Object
2476 apply1 (Lisp_Object fn, Lisp_Object arg)
2478 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2481 /* Call function fn on no arguments. */
2482 Lisp_Object
2483 call0 (Lisp_Object fn)
2485 return Ffuncall (1, &fn);
2488 /* Call function fn with 1 argument arg1. */
2489 /* ARGSUSED */
2490 Lisp_Object
2491 call1 (Lisp_Object fn, Lisp_Object arg1)
2493 return CALLN (Ffuncall, fn, arg1);
2496 /* Call function fn with 2 arguments arg1, arg2. */
2497 /* ARGSUSED */
2498 Lisp_Object
2499 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2501 return CALLN (Ffuncall, fn, arg1, arg2);
2504 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2505 /* ARGSUSED */
2506 Lisp_Object
2507 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2509 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2512 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2513 /* ARGSUSED */
2514 Lisp_Object
2515 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2516 Lisp_Object arg4)
2518 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2521 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2522 /* ARGSUSED */
2523 Lisp_Object
2524 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2525 Lisp_Object arg4, Lisp_Object arg5)
2527 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2530 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2531 /* ARGSUSED */
2532 Lisp_Object
2533 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2534 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2536 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2539 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2540 /* ARGSUSED */
2541 Lisp_Object
2542 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2543 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2545 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2548 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2549 doc: /* Non-nil if OBJECT is a function. */)
2550 (Lisp_Object object)
2552 if (FUNCTIONP (object))
2553 return Qt;
2554 return Qnil;
2557 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2558 doc: /* Call first argument as a function, passing remaining arguments to it.
2559 Return the value that function returns.
2560 Thus, (funcall \\='cons \\='x \\='y) returns (x . y).
2561 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2562 (ptrdiff_t nargs, Lisp_Object *args)
2564 Lisp_Object fun, original_fun;
2565 Lisp_Object funcar;
2566 ptrdiff_t numargs = nargs - 1;
2567 Lisp_Object lisp_numargs;
2568 Lisp_Object val;
2569 Lisp_Object *internal_args;
2570 ptrdiff_t count;
2572 QUIT;
2574 if (++lisp_eval_depth > max_lisp_eval_depth)
2576 if (max_lisp_eval_depth < 100)
2577 max_lisp_eval_depth = 100;
2578 if (lisp_eval_depth > max_lisp_eval_depth)
2579 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2582 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2584 maybe_gc ();
2586 if (debug_on_next_call)
2587 do_debug_on_call (Qlambda, count);
2589 check_cons_list ();
2591 original_fun = args[0];
2593 retry:
2595 /* Optimize for no indirection. */
2596 fun = original_fun;
2597 if (SYMBOLP (fun) && !NILP (fun)
2598 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2599 fun = indirect_function (fun);
2601 if (SUBRP (fun))
2603 if (numargs < XSUBR (fun)->min_args
2604 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2606 XSETFASTINT (lisp_numargs, numargs);
2607 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2610 else if (XSUBR (fun)->max_args == UNEVALLED)
2611 xsignal1 (Qinvalid_function, original_fun);
2613 else if (XSUBR (fun)->max_args == MANY)
2614 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2615 else
2617 Lisp_Object internal_argbuf[8];
2618 if (XSUBR (fun)->max_args > numargs)
2620 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2621 internal_args = internal_argbuf;
2622 memcpy (internal_args, args + 1, numargs * word_size);
2623 memclear (internal_args + numargs,
2624 (XSUBR (fun)->max_args - numargs) * word_size);
2626 else
2627 internal_args = args + 1;
2628 switch (XSUBR (fun)->max_args)
2630 case 0:
2631 val = (XSUBR (fun)->function.a0 ());
2632 break;
2633 case 1:
2634 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2635 break;
2636 case 2:
2637 val = (XSUBR (fun)->function.a2
2638 (internal_args[0], internal_args[1]));
2639 break;
2640 case 3:
2641 val = (XSUBR (fun)->function.a3
2642 (internal_args[0], internal_args[1], internal_args[2]));
2643 break;
2644 case 4:
2645 val = (XSUBR (fun)->function.a4
2646 (internal_args[0], internal_args[1], internal_args[2],
2647 internal_args[3]));
2648 break;
2649 case 5:
2650 val = (XSUBR (fun)->function.a5
2651 (internal_args[0], internal_args[1], internal_args[2],
2652 internal_args[3], internal_args[4]));
2653 break;
2654 case 6:
2655 val = (XSUBR (fun)->function.a6
2656 (internal_args[0], internal_args[1], internal_args[2],
2657 internal_args[3], internal_args[4], internal_args[5]));
2658 break;
2659 case 7:
2660 val = (XSUBR (fun)->function.a7
2661 (internal_args[0], internal_args[1], internal_args[2],
2662 internal_args[3], internal_args[4], internal_args[5],
2663 internal_args[6]));
2664 break;
2666 case 8:
2667 val = (XSUBR (fun)->function.a8
2668 (internal_args[0], internal_args[1], internal_args[2],
2669 internal_args[3], internal_args[4], internal_args[5],
2670 internal_args[6], internal_args[7]));
2671 break;
2673 default:
2675 /* If a subr takes more than 8 arguments without using MANY
2676 or UNEVALLED, we need to extend this function to support it.
2677 Until this is done, there is no way to call the function. */
2678 emacs_abort ();
2682 else if (COMPILEDP (fun))
2683 val = funcall_lambda (fun, numargs, args + 1);
2684 else
2686 if (NILP (fun))
2687 xsignal1 (Qvoid_function, original_fun);
2688 if (!CONSP (fun))
2689 xsignal1 (Qinvalid_function, original_fun);
2690 funcar = XCAR (fun);
2691 if (!SYMBOLP (funcar))
2692 xsignal1 (Qinvalid_function, original_fun);
2693 if (EQ (funcar, Qlambda)
2694 || EQ (funcar, Qclosure))
2695 val = funcall_lambda (fun, numargs, args + 1);
2696 else if (EQ (funcar, Qautoload))
2698 Fautoload_do_load (fun, original_fun, Qnil);
2699 check_cons_list ();
2700 goto retry;
2702 else
2703 xsignal1 (Qinvalid_function, original_fun);
2705 check_cons_list ();
2706 lisp_eval_depth--;
2707 if (backtrace_debug_on_exit (specpdl + count))
2708 val = call_debugger (list2 (Qexit, val));
2709 specpdl_ptr--;
2710 return val;
2713 static Lisp_Object
2714 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2716 Lisp_Object args_left;
2717 ptrdiff_t i;
2718 EMACS_INT numargs;
2719 Lisp_Object *arg_vector;
2720 Lisp_Object tem;
2721 USE_SAFE_ALLOCA;
2723 numargs = XFASTINT (Flength (args));
2724 SAFE_ALLOCA_LISP (arg_vector, numargs);
2725 args_left = args;
2727 for (i = 0; i < numargs; )
2729 tem = Fcar (args_left), args_left = Fcdr (args_left);
2730 tem = eval_sub (tem);
2731 arg_vector[i++] = tem;
2734 set_backtrace_args (specpdl + count, arg_vector, i);
2735 tem = funcall_lambda (fun, numargs, arg_vector);
2737 /* Do the debug-on-exit now, while arg_vector still exists. */
2738 if (backtrace_debug_on_exit (specpdl + count))
2740 /* Don't do it again when we return to eval. */
2741 set_backtrace_debug_on_exit (specpdl + count, false);
2742 tem = call_debugger (list2 (Qexit, tem));
2744 SAFE_FREE ();
2745 return tem;
2748 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2749 and return the result of evaluation.
2750 FUN must be either a lambda-expression or a compiled-code object. */
2752 static Lisp_Object
2753 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2754 register Lisp_Object *arg_vector)
2756 Lisp_Object val, syms_left, next, lexenv;
2757 ptrdiff_t count = SPECPDL_INDEX ();
2758 ptrdiff_t i;
2759 bool optional, rest;
2761 if (CONSP (fun))
2763 if (EQ (XCAR (fun), Qclosure))
2765 fun = XCDR (fun); /* Drop `closure'. */
2766 lexenv = XCAR (fun);
2767 CHECK_LIST_CONS (fun, fun);
2769 else
2770 lexenv = Qnil;
2771 syms_left = XCDR (fun);
2772 if (CONSP (syms_left))
2773 syms_left = XCAR (syms_left);
2774 else
2775 xsignal1 (Qinvalid_function, fun);
2777 else if (COMPILEDP (fun))
2779 syms_left = AREF (fun, COMPILED_ARGLIST);
2780 if (INTEGERP (syms_left))
2781 /* A byte-code object with a non-nil `push args' slot means we
2782 shouldn't bind any arguments, instead just call the byte-code
2783 interpreter directly; it will push arguments as necessary.
2785 Byte-code objects with either a non-existent, or a nil value for
2786 the `push args' slot (the default), have dynamically-bound
2787 arguments, and use the argument-binding code below instead (as do
2788 all interpreted functions, even lexically bound ones). */
2790 /* If we have not actually read the bytecode string
2791 and constants vector yet, fetch them from the file. */
2792 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2793 Ffetch_bytecode (fun);
2794 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2795 AREF (fun, COMPILED_CONSTANTS),
2796 AREF (fun, COMPILED_STACK_DEPTH),
2797 syms_left,
2798 nargs, arg_vector);
2800 lexenv = Qnil;
2802 else
2803 emacs_abort ();
2805 i = optional = rest = 0;
2806 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2808 QUIT;
2810 next = XCAR (syms_left);
2811 if (!SYMBOLP (next))
2812 xsignal1 (Qinvalid_function, fun);
2814 if (EQ (next, Qand_rest))
2815 rest = 1;
2816 else if (EQ (next, Qand_optional))
2817 optional = 1;
2818 else
2820 Lisp_Object arg;
2821 if (rest)
2823 arg = Flist (nargs - i, &arg_vector[i]);
2824 i = nargs;
2826 else if (i < nargs)
2827 arg = arg_vector[i++];
2828 else if (!optional)
2829 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2830 else
2831 arg = Qnil;
2833 /* Bind the argument. */
2834 if (!NILP (lexenv) && SYMBOLP (next))
2835 /* Lexically bind NEXT by adding it to the lexenv alist. */
2836 lexenv = Fcons (Fcons (next, arg), lexenv);
2837 else
2838 /* Dynamically bind NEXT. */
2839 specbind (next, arg);
2843 if (!NILP (syms_left))
2844 xsignal1 (Qinvalid_function, fun);
2845 else if (i < nargs)
2846 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2848 if (!EQ (lexenv, Vinternal_interpreter_environment))
2849 /* Instantiate a new lexical environment. */
2850 specbind (Qinternal_interpreter_environment, lexenv);
2852 if (CONSP (fun))
2853 val = Fprogn (XCDR (XCDR (fun)));
2854 else
2856 /* If we have not actually read the bytecode string
2857 and constants vector yet, fetch them from the file. */
2858 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2859 Ffetch_bytecode (fun);
2860 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2861 AREF (fun, COMPILED_CONSTANTS),
2862 AREF (fun, COMPILED_STACK_DEPTH),
2863 Qnil, 0, 0);
2866 return unbind_to (count, val);
2869 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2870 1, 1, 0,
2871 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2872 (Lisp_Object object)
2874 Lisp_Object tem;
2876 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2878 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2879 if (!CONSP (tem))
2881 tem = AREF (object, COMPILED_BYTECODE);
2882 if (CONSP (tem) && STRINGP (XCAR (tem)))
2883 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2884 else
2885 error ("Invalid byte code");
2887 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2888 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2890 return object;
2893 /* Return true if SYMBOL currently has a let-binding
2894 which was made in the buffer that is now current. */
2896 bool
2897 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2899 union specbinding *p;
2900 Lisp_Object buf = Fcurrent_buffer ();
2902 for (p = specpdl_ptr; p > specpdl; )
2903 if ((--p)->kind > SPECPDL_LET)
2905 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2906 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2907 if (symbol == let_bound_symbol
2908 && EQ (specpdl_where (p), buf))
2909 return 1;
2912 return 0;
2915 bool
2916 let_shadows_global_binding_p (Lisp_Object symbol)
2918 union specbinding *p;
2920 for (p = specpdl_ptr; p > specpdl; )
2921 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
2922 return 1;
2924 return 0;
2927 /* `specpdl_ptr' describes which variable is
2928 let-bound, so it can be properly undone when we unbind_to.
2929 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
2930 - SYMBOL is the variable being bound. Note that it should not be
2931 aliased (i.e. when let-binding V1 that's aliased to V2, we want
2932 to record V2 here).
2933 - WHERE tells us in which buffer the binding took place.
2934 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
2935 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
2936 i.e. bindings to the default value of a variable which can be
2937 buffer-local. */
2939 void
2940 specbind (Lisp_Object symbol, Lisp_Object value)
2942 struct Lisp_Symbol *sym;
2944 CHECK_SYMBOL (symbol);
2945 sym = XSYMBOL (symbol);
2947 start:
2948 switch (sym->redirect)
2950 case SYMBOL_VARALIAS:
2951 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
2952 case SYMBOL_PLAINVAL:
2953 /* The most common case is that of a non-constant symbol with a
2954 trivial value. Make that as fast as we can. */
2955 specpdl_ptr->let.kind = SPECPDL_LET;
2956 specpdl_ptr->let.symbol = symbol;
2957 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
2958 grow_specpdl ();
2959 if (!sym->constant)
2960 SET_SYMBOL_VAL (sym, value);
2961 else
2962 set_internal (symbol, value, Qnil, 1);
2963 break;
2964 case SYMBOL_LOCALIZED:
2965 if (SYMBOL_BLV (sym)->frame_local)
2966 error ("Frame-local vars cannot be let-bound");
2967 case SYMBOL_FORWARDED:
2969 Lisp_Object ovalue = find_symbol_value (symbol);
2970 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
2971 specpdl_ptr->let.symbol = symbol;
2972 specpdl_ptr->let.old_value = ovalue;
2973 specpdl_ptr->let.where = Fcurrent_buffer ();
2975 eassert (sym->redirect != SYMBOL_LOCALIZED
2976 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
2978 if (sym->redirect == SYMBOL_LOCALIZED)
2980 if (!blv_found (SYMBOL_BLV (sym)))
2981 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
2983 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
2985 /* If SYMBOL is a per-buffer variable which doesn't have a
2986 buffer-local value here, make the `let' change the global
2987 value by changing the value of SYMBOL in all buffers not
2988 having their own value. This is consistent with what
2989 happens with other buffer-local variables. */
2990 if (NILP (Flocal_variable_p (symbol, Qnil)))
2992 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
2993 grow_specpdl ();
2994 Fset_default (symbol, value);
2995 return;
2998 else
2999 specpdl_ptr->let.kind = SPECPDL_LET;
3001 grow_specpdl ();
3002 set_internal (symbol, value, Qnil, 1);
3003 break;
3005 default: emacs_abort ();
3009 /* Push unwind-protect entries of various types. */
3011 void
3012 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3014 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3015 specpdl_ptr->unwind.func = function;
3016 specpdl_ptr->unwind.arg = arg;
3017 grow_specpdl ();
3020 void
3021 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3023 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3024 specpdl_ptr->unwind_ptr.func = function;
3025 specpdl_ptr->unwind_ptr.arg = arg;
3026 grow_specpdl ();
3029 void
3030 record_unwind_protect_int (void (*function) (int), int arg)
3032 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3033 specpdl_ptr->unwind_int.func = function;
3034 specpdl_ptr->unwind_int.arg = arg;
3035 grow_specpdl ();
3038 void
3039 record_unwind_protect_void (void (*function) (void))
3041 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3042 specpdl_ptr->unwind_void.func = function;
3043 grow_specpdl ();
3046 static void
3047 do_nothing (void)
3050 /* Push an unwind-protect entry that does nothing, so that
3051 set_unwind_protect_ptr can overwrite it later. */
3053 void
3054 record_unwind_protect_nothing (void)
3056 record_unwind_protect_void (do_nothing);
3059 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3060 It need not be at the top of the stack. */
3062 void
3063 clear_unwind_protect (ptrdiff_t count)
3065 union specbinding *p = specpdl + count;
3066 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3067 p->unwind_void.func = do_nothing;
3070 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3071 It need not be at the top of the stack. Discard the entry's
3072 previous value without invoking it. */
3074 void
3075 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3076 Lisp_Object arg)
3078 union specbinding *p = specpdl + count;
3079 p->unwind.kind = SPECPDL_UNWIND;
3080 p->unwind.func = func;
3081 p->unwind.arg = arg;
3084 void
3085 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3087 union specbinding *p = specpdl + count;
3088 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3089 p->unwind_ptr.func = func;
3090 p->unwind_ptr.arg = arg;
3093 /* Pop and execute entries from the unwind-protect stack until the
3094 depth COUNT is reached. Return VALUE. */
3096 Lisp_Object
3097 unbind_to (ptrdiff_t count, Lisp_Object value)
3099 Lisp_Object quitf = Vquit_flag;
3101 Vquit_flag = Qnil;
3103 while (specpdl_ptr != specpdl + count)
3105 /* Decrement specpdl_ptr before we do the work to unbind it, so
3106 that an error in unbinding won't try to unbind the same entry
3107 again. Take care to copy any parts of the binding needed
3108 before invoking any code that can make more bindings. */
3110 specpdl_ptr--;
3112 switch (specpdl_ptr->kind)
3114 case SPECPDL_UNWIND:
3115 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3116 break;
3117 case SPECPDL_UNWIND_PTR:
3118 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3119 break;
3120 case SPECPDL_UNWIND_INT:
3121 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3122 break;
3123 case SPECPDL_UNWIND_VOID:
3124 specpdl_ptr->unwind_void.func ();
3125 break;
3126 case SPECPDL_BACKTRACE:
3127 break;
3128 case SPECPDL_LET:
3129 { /* If variable has a trivial value (no forwarding), we can
3130 just set it. No need to check for constant symbols here,
3131 since that was already done by specbind. */
3132 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3133 if (sym->redirect == SYMBOL_PLAINVAL)
3135 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3136 break;
3138 else
3139 { /* FALLTHROUGH!!
3140 NOTE: we only ever come here if make_local_foo was used for
3141 the first time on this var within this let. */
3144 case SPECPDL_LET_DEFAULT:
3145 Fset_default (specpdl_symbol (specpdl_ptr),
3146 specpdl_old_value (specpdl_ptr));
3147 break;
3148 case SPECPDL_LET_LOCAL:
3150 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3151 Lisp_Object where = specpdl_where (specpdl_ptr);
3152 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3153 eassert (BUFFERP (where));
3155 /* If this was a local binding, reset the value in the appropriate
3156 buffer, but only if that buffer's binding still exists. */
3157 if (!NILP (Flocal_variable_p (symbol, where)))
3158 set_internal (symbol, old_value, where, 1);
3160 break;
3164 if (NILP (Vquit_flag) && !NILP (quitf))
3165 Vquit_flag = quitf;
3167 return value;
3170 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3171 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3172 A special variable is one that will be bound dynamically, even in a
3173 context where binding is lexical by default. */)
3174 (Lisp_Object symbol)
3176 CHECK_SYMBOL (symbol);
3177 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3181 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3182 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3183 The debugger is entered when that frame exits, if the flag is non-nil. */)
3184 (Lisp_Object level, Lisp_Object flag)
3186 union specbinding *pdl = backtrace_top ();
3187 register EMACS_INT i;
3189 CHECK_NUMBER (level);
3191 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3192 pdl = backtrace_next (pdl);
3194 if (backtrace_p (pdl))
3195 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3197 return flag;
3200 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3201 doc: /* Print a trace of Lisp function calls currently active.
3202 Output stream used is value of `standard-output'. */)
3203 (void)
3205 union specbinding *pdl = backtrace_top ();
3206 Lisp_Object tem;
3207 Lisp_Object old_print_level = Vprint_level;
3209 if (NILP (Vprint_level))
3210 XSETFASTINT (Vprint_level, 8);
3212 while (backtrace_p (pdl))
3214 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ");
3215 if (backtrace_nargs (pdl) == UNEVALLED)
3217 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3218 Qnil);
3219 write_string ("\n");
3221 else
3223 tem = backtrace_function (pdl);
3224 Fprin1 (tem, Qnil); /* This can QUIT. */
3225 write_string ("(");
3227 ptrdiff_t i;
3228 for (i = 0; i < backtrace_nargs (pdl); i++)
3230 if (i) write_string (" ");
3231 Fprin1 (backtrace_args (pdl)[i], Qnil);
3234 write_string (")\n");
3236 pdl = backtrace_next (pdl);
3239 Vprint_level = old_print_level;
3240 return Qnil;
3243 static union specbinding *
3244 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3246 union specbinding *pdl = backtrace_top ();
3247 register EMACS_INT i;
3249 CHECK_NATNUM (nframes);
3251 if (!NILP (base))
3252 { /* Skip up to `base'. */
3253 base = Findirect_function (base, Qt);
3254 while (backtrace_p (pdl)
3255 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3256 pdl = backtrace_next (pdl);
3259 /* Find the frame requested. */
3260 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3261 pdl = backtrace_next (pdl);
3263 return pdl;
3266 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3267 doc: /* Return the function and arguments NFRAMES up from current execution point.
3268 If that frame has not evaluated the arguments yet (or is a special form),
3269 the value is (nil FUNCTION ARG-FORMS...).
3270 If that frame has evaluated its arguments and called its function already,
3271 the value is (t FUNCTION ARG-VALUES...).
3272 A &rest arg is represented as the tail of the list ARG-VALUES.
3273 FUNCTION is whatever was supplied as car of evaluated list,
3274 or a lambda expression for macro calls.
3275 If NFRAMES is more than the number of frames, the value is nil.
3276 If BASE is non-nil, it should be a function and NFRAMES counts from its
3277 nearest activation frame. */)
3278 (Lisp_Object nframes, Lisp_Object base)
3280 union specbinding *pdl = get_backtrace_frame (nframes, base);
3282 if (!backtrace_p (pdl))
3283 return Qnil;
3284 if (backtrace_nargs (pdl) == UNEVALLED)
3285 return Fcons (Qnil,
3286 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3287 else
3289 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3291 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3295 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3296 the specpdl stack, and then rewind them. We store the pre-unwind values
3297 directly in the pre-existing specpdl elements (i.e. we swap the current
3298 value and the old value stored in the specpdl), kind of like the inplace
3299 pointer-reversal trick. As it turns out, the rewind does the same as the
3300 unwind, except it starts from the other end of the specpdl stack, so we use
3301 the same function for both unwind and rewind. */
3302 static void
3303 backtrace_eval_unrewind (int distance)
3305 union specbinding *tmp = specpdl_ptr;
3306 int step = -1;
3307 if (distance < 0)
3308 { /* It's a rewind rather than unwind. */
3309 tmp += distance - 1;
3310 step = 1;
3311 distance = -distance;
3314 for (; distance > 0; distance--)
3316 tmp += step;
3317 switch (tmp->kind)
3319 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3320 unwind_protect, but the problem is that we don't know how to
3321 rewind them afterwards. */
3322 case SPECPDL_UNWIND:
3324 Lisp_Object oldarg = tmp->unwind.arg;
3325 if (tmp->unwind.func == set_buffer_if_live)
3326 tmp->unwind.arg = Fcurrent_buffer ();
3327 else if (tmp->unwind.func == save_excursion_restore)
3328 tmp->unwind.arg = save_excursion_save ();
3329 else
3330 break;
3331 tmp->unwind.func (oldarg);
3332 break;
3335 case SPECPDL_UNWIND_PTR:
3336 case SPECPDL_UNWIND_INT:
3337 case SPECPDL_UNWIND_VOID:
3338 case SPECPDL_BACKTRACE:
3339 break;
3340 case SPECPDL_LET:
3341 { /* If variable has a trivial value (no forwarding), we can
3342 just set it. No need to check for constant symbols here,
3343 since that was already done by specbind. */
3344 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3345 if (sym->redirect == SYMBOL_PLAINVAL)
3347 Lisp_Object old_value = specpdl_old_value (tmp);
3348 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3349 SET_SYMBOL_VAL (sym, old_value);
3350 break;
3352 else
3353 { /* FALLTHROUGH!!
3354 NOTE: we only ever come here if make_local_foo was used for
3355 the first time on this var within this let. */
3358 case SPECPDL_LET_DEFAULT:
3360 Lisp_Object sym = specpdl_symbol (tmp);
3361 Lisp_Object old_value = specpdl_old_value (tmp);
3362 set_specpdl_old_value (tmp, Fdefault_value (sym));
3363 Fset_default (sym, old_value);
3365 break;
3366 case SPECPDL_LET_LOCAL:
3368 Lisp_Object symbol = specpdl_symbol (tmp);
3369 Lisp_Object where = specpdl_where (tmp);
3370 Lisp_Object old_value = specpdl_old_value (tmp);
3371 eassert (BUFFERP (where));
3373 /* If this was a local binding, reset the value in the appropriate
3374 buffer, but only if that buffer's binding still exists. */
3375 if (!NILP (Flocal_variable_p (symbol, where)))
3377 set_specpdl_old_value
3378 (tmp, Fbuffer_local_value (symbol, where));
3379 set_internal (symbol, old_value, where, 1);
3382 break;
3387 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3388 doc: /* Evaluate EXP in the context of some activation frame.
3389 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3390 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3392 union specbinding *pdl = get_backtrace_frame (nframes, base);
3393 ptrdiff_t count = SPECPDL_INDEX ();
3394 ptrdiff_t distance = specpdl_ptr - pdl;
3395 eassert (distance >= 0);
3397 if (!backtrace_p (pdl))
3398 error ("Activation frame not found!");
3400 backtrace_eval_unrewind (distance);
3401 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3403 /* Use eval_sub rather than Feval since the main motivation behind
3404 backtrace-eval is to be able to get/set the value of lexical variables
3405 from the debugger. */
3406 return unbind_to (count, eval_sub (exp));
3409 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3410 doc: /* Return names and values of local variables of a stack frame.
3411 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3412 (Lisp_Object nframes, Lisp_Object base)
3414 union specbinding *frame = get_backtrace_frame (nframes, base);
3415 union specbinding *prevframe
3416 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3417 ptrdiff_t distance = specpdl_ptr - frame;
3418 Lisp_Object result = Qnil;
3419 eassert (distance >= 0);
3421 if (!backtrace_p (prevframe))
3422 error ("Activation frame not found!");
3423 if (!backtrace_p (frame))
3424 error ("Activation frame not found!");
3426 /* The specpdl entries normally contain the symbol being bound along with its
3427 `old_value', so it can be restored. The new value to which it is bound is
3428 available in one of two places: either in the current value of the
3429 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3430 next specpdl entry for it.
3431 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3432 and "new value", so we abuse it here, to fetch the new value.
3433 It's ugly (we'd rather not modify global data) and a bit inefficient,
3434 but it does the job for now. */
3435 backtrace_eval_unrewind (distance);
3437 /* Grab values. */
3439 union specbinding *tmp = prevframe;
3440 for (; tmp > frame; tmp--)
3442 switch (tmp->kind)
3444 case SPECPDL_LET:
3445 case SPECPDL_LET_DEFAULT:
3446 case SPECPDL_LET_LOCAL:
3448 Lisp_Object sym = specpdl_symbol (tmp);
3449 Lisp_Object val = specpdl_old_value (tmp);
3450 if (EQ (sym, Qinternal_interpreter_environment))
3452 Lisp_Object env = val;
3453 for (; CONSP (env); env = XCDR (env))
3455 Lisp_Object binding = XCAR (env);
3456 if (CONSP (binding))
3457 result = Fcons (Fcons (XCAR (binding),
3458 XCDR (binding)),
3459 result);
3462 else
3463 result = Fcons (Fcons (sym, val), result);
3469 /* Restore values from specpdl to original place. */
3470 backtrace_eval_unrewind (-distance);
3472 return result;
3476 void
3477 mark_specpdl (void)
3479 union specbinding *pdl;
3480 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3482 switch (pdl->kind)
3484 case SPECPDL_UNWIND:
3485 mark_object (specpdl_arg (pdl));
3486 break;
3488 case SPECPDL_BACKTRACE:
3490 ptrdiff_t nargs = backtrace_nargs (pdl);
3491 mark_object (backtrace_function (pdl));
3492 if (nargs == UNEVALLED)
3493 nargs = 1;
3494 while (nargs--)
3495 mark_object (backtrace_args (pdl)[nargs]);
3497 break;
3499 case SPECPDL_LET_DEFAULT:
3500 case SPECPDL_LET_LOCAL:
3501 mark_object (specpdl_where (pdl));
3502 /* Fall through. */
3503 case SPECPDL_LET:
3504 mark_object (specpdl_symbol (pdl));
3505 mark_object (specpdl_old_value (pdl));
3506 break;
3511 void
3512 get_backtrace (Lisp_Object array)
3514 union specbinding *pdl = backtrace_next (backtrace_top ());
3515 ptrdiff_t i = 0, asize = ASIZE (array);
3517 /* Copy the backtrace contents into working memory. */
3518 for (; i < asize; i++)
3520 if (backtrace_p (pdl))
3522 ASET (array, i, backtrace_function (pdl));
3523 pdl = backtrace_next (pdl);
3525 else
3526 ASET (array, i, Qnil);
3530 Lisp_Object backtrace_top_function (void)
3532 union specbinding *pdl = backtrace_top ();
3533 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3536 void
3537 syms_of_eval (void)
3539 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3540 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3541 If Lisp code tries to increase the total number past this amount,
3542 an error is signaled.
3543 You can safely use a value considerably larger than the default value,
3544 if that proves inconveniently small. However, if you increase it too far,
3545 Emacs could run out of memory trying to make the stack bigger.
3546 Note that this limit may be silently increased by the debugger
3547 if `debug-on-error' or `debug-on-quit' is set. */);
3549 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3550 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3552 This limit serves to catch infinite recursions for you before they cause
3553 actual stack overflow in C, which would be fatal for Emacs.
3554 You can safely make it considerably larger than its default value,
3555 if that proves inconveniently small. However, if you increase it too far,
3556 Emacs could overflow the real C stack, and crash. */);
3558 DEFVAR_LISP ("quit-flag", Vquit_flag,
3559 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3560 If the value is t, that means do an ordinary quit.
3561 If the value equals `throw-on-input', that means quit by throwing
3562 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3563 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3564 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3565 Vquit_flag = Qnil;
3567 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3568 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3569 Note that `quit-flag' will still be set by typing C-g,
3570 so a quit will be signaled as soon as `inhibit-quit' is nil.
3571 To prevent this happening, set `quit-flag' to nil
3572 before making `inhibit-quit' nil. */);
3573 Vinhibit_quit = Qnil;
3575 DEFSYM (Qinhibit_quit, "inhibit-quit");
3576 DEFSYM (Qautoload, "autoload");
3577 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3578 DEFSYM (Qmacro, "macro");
3580 /* Note that the process handling also uses Qexit, but we don't want
3581 to staticpro it twice, so we just do it here. */
3582 DEFSYM (Qexit, "exit");
3584 DEFSYM (Qinteractive, "interactive");
3585 DEFSYM (Qcommandp, "commandp");
3586 DEFSYM (Qand_rest, "&rest");
3587 DEFSYM (Qand_optional, "&optional");
3588 DEFSYM (Qclosure, "closure");
3589 DEFSYM (QCdocumentation, ":documentation");
3590 DEFSYM (Qdebug, "debug");
3592 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3593 doc: /* Non-nil means never enter the debugger.
3594 Normally set while the debugger is already active, to avoid recursive
3595 invocations. */);
3596 Vinhibit_debugger = Qnil;
3598 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3599 doc: /* Non-nil means enter debugger if an error is signaled.
3600 Does not apply to errors handled by `condition-case' or those
3601 matched by `debug-ignored-errors'.
3602 If the value is a list, an error only means to enter the debugger
3603 if one of its condition symbols appears in the list.
3604 When you evaluate an expression interactively, this variable
3605 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3606 The command `toggle-debug-on-error' toggles this.
3607 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3608 Vdebug_on_error = Qnil;
3610 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3611 doc: /* List of errors for which the debugger should not be called.
3612 Each element may be a condition-name or a regexp that matches error messages.
3613 If any element applies to a given error, that error skips the debugger
3614 and just returns to top level.
3615 This overrides the variable `debug-on-error'.
3616 It does not apply to errors handled by `condition-case'. */);
3617 Vdebug_ignored_errors = Qnil;
3619 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3620 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3621 Does not apply if quit is handled by a `condition-case'. */);
3622 debug_on_quit = 0;
3624 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3625 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3627 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3628 doc: /* Non-nil means debugger may continue execution.
3629 This is nil when the debugger is called under circumstances where it
3630 might not be safe to continue. */);
3631 debugger_may_continue = 1;
3633 DEFVAR_LISP ("debugger", Vdebugger,
3634 doc: /* Function to call to invoke debugger.
3635 If due to frame exit, args are `exit' and the value being returned;
3636 this function's value will be returned instead of that.
3637 If due to error, args are `error' and a list of the args to `signal'.
3638 If due to `apply' or `funcall' entry, one arg, `lambda'.
3639 If due to `eval' entry, one arg, t. */);
3640 Vdebugger = Qnil;
3642 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3643 doc: /* If non-nil, this is a function for `signal' to call.
3644 It receives the same arguments that `signal' was given.
3645 The Edebug package uses this to regain control. */);
3646 Vsignal_hook_function = Qnil;
3648 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3649 doc: /* Non-nil means call the debugger regardless of condition handlers.
3650 Note that `debug-on-error', `debug-on-quit' and friends
3651 still determine whether to handle the particular condition. */);
3652 Vdebug_on_signal = Qnil;
3654 /* When lexical binding is being used,
3655 Vinternal_interpreter_environment is non-nil, and contains an alist
3656 of lexically-bound variable, or (t), indicating an empty
3657 environment. The lisp name of this variable would be
3658 `internal-interpreter-environment' if it weren't hidden.
3659 Every element of this list can be either a cons (VAR . VAL)
3660 specifying a lexical binding, or a single symbol VAR indicating
3661 that this variable should use dynamic scoping. */
3662 DEFSYM (Qinternal_interpreter_environment,
3663 "internal-interpreter-environment");
3664 DEFVAR_LISP ("internal-interpreter-environment",
3665 Vinternal_interpreter_environment,
3666 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3667 When lexical binding is not being used, this variable is nil.
3668 A value of `(t)' indicates an empty environment, otherwise it is an
3669 alist of active lexical bindings. */);
3670 Vinternal_interpreter_environment = Qnil;
3671 /* Don't export this variable to Elisp, so no one can mess with it
3672 (Just imagine if someone makes it buffer-local). */
3673 Funintern (Qinternal_interpreter_environment, Qnil);
3675 Vrun_hooks = intern_c_string ("run-hooks");
3676 staticpro (&Vrun_hooks);
3678 staticpro (&Vautoload_queue);
3679 Vautoload_queue = Qnil;
3680 staticpro (&Vsignaling_function);
3681 Vsignaling_function = Qnil;
3683 inhibit_lisp_code = Qnil;
3685 defsubr (&Sor);
3686 defsubr (&Sand);
3687 defsubr (&Sif);
3688 defsubr (&Scond);
3689 defsubr (&Sprogn);
3690 defsubr (&Sprog1);
3691 defsubr (&Sprog2);
3692 defsubr (&Ssetq);
3693 defsubr (&Squote);
3694 defsubr (&Sfunction);
3695 defsubr (&Sdefault_toplevel_value);
3696 defsubr (&Sset_default_toplevel_value);
3697 defsubr (&Sdefvar);
3698 defsubr (&Sdefvaralias);
3699 defsubr (&Sdefconst);
3700 defsubr (&Smake_var_non_special);
3701 defsubr (&Slet);
3702 defsubr (&SletX);
3703 defsubr (&Swhile);
3704 defsubr (&Smacroexpand);
3705 defsubr (&Scatch);
3706 defsubr (&Sthrow);
3707 defsubr (&Sunwind_protect);
3708 defsubr (&Scondition_case);
3709 defsubr (&Ssignal);
3710 defsubr (&Scommandp);
3711 defsubr (&Sautoload);
3712 defsubr (&Sautoload_do_load);
3713 defsubr (&Seval);
3714 defsubr (&Sapply);
3715 defsubr (&Sfuncall);
3716 defsubr (&Srun_hooks);
3717 defsubr (&Srun_hook_with_args);
3718 defsubr (&Srun_hook_with_args_until_success);
3719 defsubr (&Srun_hook_with_args_until_failure);
3720 defsubr (&Srun_hook_wrapped);
3721 defsubr (&Sfetch_bytecode);
3722 defsubr (&Sbacktrace_debug);
3723 defsubr (&Sbacktrace);
3724 defsubr (&Sbacktrace_frame);
3725 defsubr (&Sbacktrace_eval);
3726 defsubr (&Sbacktrace__locals);
3727 defsubr (&Sspecial_variable_p);
3728 defsubr (&Sfunctionp);