emacs-lisp/package.el (package-initialize): Populate `package-selected-packages'.
[emacs.git] / src / eval.c
blobb98b224e622595146ce7724b3d8bdcc789d15de9
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 #ifdef DEBUG_GCPRO
37 /* Count levels of GCPRO to detect failure to UNGCPRO. */
38 int gcpro_level;
39 #endif
41 /* Non-nil means record all fset's and provide's, to be undone
42 if the file being autoloaded is not fully loaded.
43 They are recorded by being consed onto the front of Vautoload_queue:
44 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
46 Lisp_Object Vautoload_queue;
48 /* This holds either the symbol `run-hooks' or nil.
49 It is nil at an early stage of startup, and when Emacs
50 is shutting down. */
51 Lisp_Object Vrun_hooks;
53 /* Current number of specbindings allocated in specpdl, not counting
54 the dummy entry specpdl[-1]. */
56 ptrdiff_t specpdl_size;
58 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
59 only so that its address can be taken. */
61 union specbinding *specpdl;
63 /* Pointer to first unused element in specpdl. */
65 union specbinding *specpdl_ptr;
67 /* Depth in Lisp evaluations and function calls. */
69 EMACS_INT lisp_eval_depth;
71 /* The value of num_nonmacro_input_events as of the last time we
72 started to enter the debugger. If we decide to enter the debugger
73 again when this is still equal to num_nonmacro_input_events, then we
74 know that the debugger itself has an error, and we should just
75 signal the error instead of entering an infinite loop of debugger
76 invocations. */
78 static EMACS_INT when_entered_debugger;
80 /* The function from which the last `signal' was called. Set in
81 Fsignal. */
82 /* FIXME: We should probably get rid of this! */
83 Lisp_Object Vsignaling_function;
85 /* If non-nil, Lisp code must not be run since some part of Emacs is in
86 an inconsistent state. Currently unused. */
87 Lisp_Object inhibit_lisp_code;
89 /* These would ordinarily be static, but they need to be visible to GDB. */
90 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
91 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
92 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
93 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
94 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
96 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
97 static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
99 static Lisp_Object
100 specpdl_symbol (union specbinding *pdl)
102 eassert (pdl->kind >= SPECPDL_LET);
103 return pdl->let.symbol;
106 static Lisp_Object
107 specpdl_old_value (union specbinding *pdl)
109 eassert (pdl->kind >= SPECPDL_LET);
110 return pdl->let.old_value;
113 static void
114 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
116 eassert (pdl->kind >= SPECPDL_LET);
117 pdl->let.old_value = val;
120 static Lisp_Object
121 specpdl_where (union specbinding *pdl)
123 eassert (pdl->kind > SPECPDL_LET);
124 return pdl->let.where;
127 static Lisp_Object
128 specpdl_arg (union specbinding *pdl)
130 eassert (pdl->kind == SPECPDL_UNWIND);
131 return pdl->unwind.arg;
134 Lisp_Object
135 backtrace_function (union specbinding *pdl)
137 eassert (pdl->kind == SPECPDL_BACKTRACE);
138 return pdl->bt.function;
141 static ptrdiff_t
142 backtrace_nargs (union specbinding *pdl)
144 eassert (pdl->kind == SPECPDL_BACKTRACE);
145 return pdl->bt.nargs;
148 Lisp_Object *
149 backtrace_args (union specbinding *pdl)
151 eassert (pdl->kind == SPECPDL_BACKTRACE);
152 return pdl->bt.args;
155 static bool
156 backtrace_debug_on_exit (union specbinding *pdl)
158 eassert (pdl->kind == SPECPDL_BACKTRACE);
159 return pdl->bt.debug_on_exit;
162 /* Functions to modify slots of backtrace records. */
164 static void
165 set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
167 eassert (pdl->kind == SPECPDL_BACKTRACE);
168 pdl->bt.args = args;
169 pdl->bt.nargs = nargs;
172 static void
173 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
175 eassert (pdl->kind == SPECPDL_BACKTRACE);
176 pdl->bt.debug_on_exit = doe;
179 /* Helper functions to scan the backtrace. */
181 bool
182 backtrace_p (union specbinding *pdl)
183 { return pdl >= specpdl; }
185 union specbinding *
186 backtrace_top (void)
188 union specbinding *pdl = specpdl_ptr - 1;
189 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
190 pdl--;
191 return pdl;
194 union specbinding *
195 backtrace_next (union specbinding *pdl)
197 pdl--;
198 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
199 pdl--;
200 return pdl;
204 void
205 init_eval_once (void)
207 enum { size = 50 };
208 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
209 specpdl_size = size;
210 specpdl = specpdl_ptr = pdlvec + 1;
211 /* Don't forget to update docs (lispref node "Local Variables"). */
212 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
213 max_lisp_eval_depth = 600;
215 Vrun_hooks = Qnil;
218 static struct handler handlerlist_sentinel;
220 void
221 init_eval (void)
223 specpdl_ptr = specpdl;
224 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
225 This is important since handlerlist->nextfree holds the freelist
226 which would otherwise leak every time we unwind back to top-level. */
227 struct handler *c;
228 handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
229 PUSH_HANDLER (c, Qunbound, CATCHER);
230 eassert (c == &handlerlist_sentinel);
231 handlerlist_sentinel.nextfree = NULL;
232 handlerlist_sentinel.next = NULL;
234 Vquit_flag = Qnil;
235 debug_on_next_call = 0;
236 lisp_eval_depth = 0;
237 #ifdef DEBUG_GCPRO
238 gcpro_level = 0;
239 #endif
240 /* This is less than the initial value of num_nonmacro_input_events. */
241 when_entered_debugger = -1;
244 /* Unwind-protect function used by call_debugger. */
246 static void
247 restore_stack_limits (Lisp_Object data)
249 max_specpdl_size = XINT (XCAR (data));
250 max_lisp_eval_depth = XINT (XCDR (data));
253 static void grow_specpdl (void);
255 /* Call the Lisp debugger, giving it argument ARG. */
257 Lisp_Object
258 call_debugger (Lisp_Object arg)
260 bool debug_while_redisplaying;
261 ptrdiff_t count = SPECPDL_INDEX ();
262 Lisp_Object val;
263 EMACS_INT old_depth = max_lisp_eval_depth;
264 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
265 EMACS_INT old_max = max (max_specpdl_size, count);
267 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
268 max_lisp_eval_depth = lisp_eval_depth + 40;
270 /* While debugging Bug#16603, previous value of 100 was found
271 too small to avoid specpdl overflow in the debugger itself. */
272 if (max_specpdl_size - 200 < count)
273 max_specpdl_size = count + 200;
275 if (old_max == count)
277 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
278 specpdl_ptr--;
279 grow_specpdl ();
282 /* Restore limits after leaving the debugger. */
283 record_unwind_protect (restore_stack_limits,
284 Fcons (make_number (old_max),
285 make_number (old_depth)));
287 #ifdef HAVE_WINDOW_SYSTEM
288 if (display_hourglass_p)
289 cancel_hourglass ();
290 #endif
292 debug_on_next_call = 0;
293 when_entered_debugger = num_nonmacro_input_events;
295 /* Resetting redisplaying_p to 0 makes sure that debug output is
296 displayed if the debugger is invoked during redisplay. */
297 debug_while_redisplaying = redisplaying_p;
298 redisplaying_p = 0;
299 specbind (intern ("debugger-may-continue"),
300 debug_while_redisplaying ? Qnil : Qt);
301 specbind (Qinhibit_redisplay, Qnil);
302 specbind (Qinhibit_debugger, Qt);
304 #if 0 /* Binding this prevents execution of Lisp code during
305 redisplay, which necessarily leads to display problems. */
306 specbind (Qinhibit_eval_during_redisplay, Qt);
307 #endif
309 val = apply1 (Vdebugger, arg);
311 /* Interrupting redisplay and resuming it later is not safe under
312 all circumstances. So, when the debugger returns, abort the
313 interrupted redisplay by going back to the top-level. */
314 if (debug_while_redisplaying)
315 Ftop_level ();
317 return unbind_to (count, val);
320 static void
321 do_debug_on_call (Lisp_Object code, ptrdiff_t count)
323 debug_on_next_call = 0;
324 set_backtrace_debug_on_exit (specpdl + count, true);
325 call_debugger (list1 (code));
328 /* NOTE!!! Every function that can call EVAL must protect its args
329 and temporaries from garbage collection while it needs them.
330 The definition of `For' shows what you have to do. */
332 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
333 doc: /* Eval args until one of them yields non-nil, then return that value.
334 The remaining args are not evalled at all.
335 If all args return nil, return nil.
336 usage: (or CONDITIONS...) */)
337 (Lisp_Object args)
339 register Lisp_Object val = Qnil;
340 struct gcpro gcpro1;
342 GCPRO1 (args);
344 while (CONSP (args))
346 val = eval_sub (XCAR (args));
347 if (!NILP (val))
348 break;
349 args = XCDR (args);
352 UNGCPRO;
353 return val;
356 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
357 doc: /* Eval args until one of them yields nil, then return nil.
358 The remaining args are not evalled at all.
359 If no arg yields nil, return the last arg's value.
360 usage: (and CONDITIONS...) */)
361 (Lisp_Object args)
363 register Lisp_Object val = Qt;
364 struct gcpro gcpro1;
366 GCPRO1 (args);
368 while (CONSP (args))
370 val = eval_sub (XCAR (args));
371 if (NILP (val))
372 break;
373 args = XCDR (args);
376 UNGCPRO;
377 return val;
380 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
381 doc: /* If COND yields non-nil, do THEN, else do ELSE...
382 Returns the value of THEN or the value of the last of the ELSE's.
383 THEN must be one expression, but ELSE... can be zero or more expressions.
384 If COND yields nil, and there are no ELSE's, the value is nil.
385 usage: (if COND THEN ELSE...) */)
386 (Lisp_Object args)
388 Lisp_Object cond;
389 struct gcpro gcpro1;
391 GCPRO1 (args);
392 cond = eval_sub (XCAR (args));
393 UNGCPRO;
395 if (!NILP (cond))
396 return eval_sub (Fcar (XCDR (args)));
397 return Fprogn (XCDR (XCDR (args)));
400 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
401 doc: /* Try each clause until one succeeds.
402 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
403 and, if the value is non-nil, this clause succeeds:
404 then the expressions in BODY are evaluated and the last one's
405 value is the value of the cond-form.
406 If a clause has one element, as in (CONDITION), then the cond-form
407 returns CONDITION's value, if that is non-nil.
408 If no clause succeeds, cond returns nil.
409 usage: (cond CLAUSES...) */)
410 (Lisp_Object args)
412 Lisp_Object val = args;
413 struct gcpro gcpro1;
415 GCPRO1 (args);
416 while (CONSP (args))
418 Lisp_Object clause = XCAR (args);
419 val = eval_sub (Fcar (clause));
420 if (!NILP (val))
422 if (!NILP (XCDR (clause)))
423 val = Fprogn (XCDR (clause));
424 break;
426 args = XCDR (args);
428 UNGCPRO;
430 return val;
433 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
434 doc: /* Eval BODY forms sequentially and return value of last one.
435 usage: (progn BODY...) */)
436 (Lisp_Object body)
438 Lisp_Object val = Qnil;
439 struct gcpro gcpro1;
441 GCPRO1 (body);
443 while (CONSP (body))
445 val = eval_sub (XCAR (body));
446 body = XCDR (body);
449 UNGCPRO;
450 return val;
453 /* Evaluate BODY sequentially, discarding its value. Suitable for
454 record_unwind_protect. */
456 void
457 unwind_body (Lisp_Object body)
459 Fprogn (body);
462 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
463 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
464 The value of FIRST is saved during the evaluation of the remaining args,
465 whose values are discarded.
466 usage: (prog1 FIRST BODY...) */)
467 (Lisp_Object args)
469 Lisp_Object val;
470 Lisp_Object args_left;
471 struct gcpro gcpro1, gcpro2;
473 args_left = args;
474 val = args;
475 GCPRO2 (args, val);
477 val = eval_sub (XCAR (args_left));
478 while (CONSP (args_left = XCDR (args_left)))
479 eval_sub (XCAR (args_left));
481 UNGCPRO;
482 return val;
485 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
486 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
487 The value of FORM2 is saved during the evaluation of the
488 remaining args, whose values are discarded.
489 usage: (prog2 FORM1 FORM2 BODY...) */)
490 (Lisp_Object args)
492 struct gcpro gcpro1;
494 GCPRO1 (args);
495 eval_sub (XCAR (args));
496 UNGCPRO;
497 return Fprog1 (XCDR (args));
500 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
501 doc: /* Set each SYM to the value of its VAL.
502 The symbols SYM are variables; they are literal (not evaluated).
503 The values VAL are expressions; they are evaluated.
504 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
505 The second VAL is not computed until after the first SYM is set, and so on;
506 each VAL can use the new value of variables set earlier in the `setq'.
507 The return value of the `setq' form is the value of the last VAL.
508 usage: (setq [SYM VAL]...) */)
509 (Lisp_Object args)
511 Lisp_Object val, sym, lex_binding;
513 val = args;
514 if (CONSP (args))
516 Lisp_Object args_left = args;
517 struct gcpro gcpro1;
518 GCPRO1 (args);
522 val = eval_sub (Fcar (XCDR (args_left)));
523 sym = XCAR (args_left);
525 /* Like for eval_sub, we do not check declared_special here since
526 it's been done when let-binding. */
527 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
528 && SYMBOLP (sym)
529 && !NILP (lex_binding
530 = Fassq (sym, Vinternal_interpreter_environment)))
531 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
532 else
533 Fset (sym, val); /* SYM is dynamically bound. */
535 args_left = Fcdr (XCDR (args_left));
537 while (CONSP (args_left));
539 UNGCPRO;
542 return val;
545 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
546 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
547 Warning: `quote' does not construct its return value, but just returns
548 the value that was pre-constructed by the Lisp reader (see info node
549 `(elisp)Printed Representation').
550 This means that '(a . b) is not identical to (cons 'a 'b): the former
551 does not cons. Quoting should be reserved for constants that will
552 never be modified by side-effects, unless you like self-modifying code.
553 See the common pitfall in info node `(elisp)Rearrangement' for an example
554 of unexpected results when a quoted object is modified.
555 usage: (quote ARG) */)
556 (Lisp_Object args)
558 if (CONSP (XCDR (args)))
559 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
560 return XCAR (args);
563 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
564 doc: /* Like `quote', but preferred for objects which are functions.
565 In byte compilation, `function' causes its argument to be compiled.
566 `quote' cannot do that.
567 usage: (function ARG) */)
568 (Lisp_Object args)
570 Lisp_Object quoted = XCAR (args);
572 if (CONSP (XCDR (args)))
573 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
575 if (!NILP (Vinternal_interpreter_environment)
576 && CONSP (quoted)
577 && EQ (XCAR (quoted), Qlambda))
578 /* This is a lambda expression within a lexical environment;
579 return an interpreted closure instead of a simple lambda. */
580 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
581 XCDR (quoted)));
582 else
583 /* Simply quote the argument. */
584 return quoted;
588 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
589 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
590 Aliased variables always have the same value; setting one sets the other.
591 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
592 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
593 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
594 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
595 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
596 The return value is BASE-VARIABLE. */)
597 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
599 struct Lisp_Symbol *sym;
601 CHECK_SYMBOL (new_alias);
602 CHECK_SYMBOL (base_variable);
604 sym = XSYMBOL (new_alias);
606 if (sym->constant)
607 /* Not sure why, but why not? */
608 error ("Cannot make a constant an alias");
610 switch (sym->redirect)
612 case SYMBOL_FORWARDED:
613 error ("Cannot make an internal variable an alias");
614 case SYMBOL_LOCALIZED:
615 error ("Don't know how to make a localized variable an alias");
618 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
619 If n_a is bound, but b_v is not, set the value of b_v to n_a,
620 so that old-code that affects n_a before the aliasing is setup
621 still works. */
622 if (NILP (Fboundp (base_variable)))
623 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
626 union specbinding *p;
628 for (p = specpdl_ptr; p > specpdl; )
629 if ((--p)->kind >= SPECPDL_LET
630 && (EQ (new_alias, specpdl_symbol (p))))
631 error ("Don't know how to make a let-bound variable an alias");
634 sym->declared_special = 1;
635 XSYMBOL (base_variable)->declared_special = 1;
636 sym->redirect = SYMBOL_VARALIAS;
637 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
638 sym->constant = SYMBOL_CONSTANT_P (base_variable);
639 LOADHIST_ATTACH (new_alias);
640 /* Even if docstring is nil: remove old docstring. */
641 Fput (new_alias, Qvariable_documentation, docstring);
643 return base_variable;
646 static union specbinding *
647 default_toplevel_binding (Lisp_Object symbol)
649 union specbinding *binding = NULL;
650 union specbinding *pdl = specpdl_ptr;
651 while (pdl > specpdl)
653 switch ((--pdl)->kind)
655 case SPECPDL_LET_DEFAULT:
656 case SPECPDL_LET:
657 if (EQ (specpdl_symbol (pdl), symbol))
658 binding = pdl;
659 break;
662 return binding;
665 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
666 doc: /* Return SYMBOL's toplevel default value.
667 "Toplevel" means outside of any let binding. */)
668 (Lisp_Object symbol)
670 union specbinding *binding = default_toplevel_binding (symbol);
671 Lisp_Object value
672 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
673 if (!EQ (value, Qunbound))
674 return value;
675 xsignal1 (Qvoid_variable, symbol);
678 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
679 Sset_default_toplevel_value, 2, 2, 0,
680 doc: /* Set SYMBOL's toplevel default value to VALUE.
681 "Toplevel" means outside of any let binding. */)
682 (Lisp_Object symbol, Lisp_Object value)
684 union specbinding *binding = default_toplevel_binding (symbol);
685 if (binding)
686 set_specpdl_old_value (binding, value);
687 else
688 Fset_default (symbol, value);
689 return Qnil;
692 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
693 doc: /* Define SYMBOL as a variable, and return SYMBOL.
694 You are not required to define a variable in order to use it, but
695 defining it lets you supply an initial value and documentation, which
696 can be referred to by the Emacs help facilities and other programming
697 tools. The `defvar' form also declares the variable as \"special\",
698 so that it is always dynamically bound even if `lexical-binding' is t.
700 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
701 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
702 default value is what is set; buffer-local values are not affected.
703 If INITVALUE is missing, SYMBOL's value is not set.
705 If SYMBOL has a local binding, then this form affects the local
706 binding. This is usually not what you want. Thus, if you need to
707 load a file defining variables, with this form or with `defconst' or
708 `defcustom', you should always load that file _outside_ any bindings
709 for these variables. \(`defconst' and `defcustom' behave similarly in
710 this respect.)
712 The optional argument DOCSTRING is a documentation string for the
713 variable.
715 To define a user option, use `defcustom' instead of `defvar'.
716 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
717 (Lisp_Object args)
719 Lisp_Object sym, tem, tail;
721 sym = XCAR (args);
722 tail = XCDR (args);
724 if (CONSP (tail))
726 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
727 error ("Too many arguments");
729 tem = Fdefault_boundp (sym);
731 /* Do it before evaluating the initial value, for self-references. */
732 XSYMBOL (sym)->declared_special = 1;
734 if (NILP (tem))
735 Fset_default (sym, eval_sub (XCAR (tail)));
736 else
737 { /* Check if there is really a global binding rather than just a let
738 binding that shadows the global unboundness of the var. */
739 union specbinding *binding = default_toplevel_binding (sym);
740 if (binding && EQ (specpdl_old_value (binding), Qunbound))
742 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
745 tail = XCDR (tail);
746 tem = Fcar (tail);
747 if (!NILP (tem))
749 if (!NILP (Vpurify_flag))
750 tem = Fpurecopy (tem);
751 Fput (sym, Qvariable_documentation, tem);
753 LOADHIST_ATTACH (sym);
755 else if (!NILP (Vinternal_interpreter_environment)
756 && !XSYMBOL (sym)->declared_special)
757 /* A simple (defvar foo) with lexical scoping does "nothing" except
758 declare that var to be dynamically scoped *locally* (i.e. within
759 the current file or let-block). */
760 Vinternal_interpreter_environment
761 = Fcons (sym, Vinternal_interpreter_environment);
762 else
764 /* Simple (defvar <var>) should not count as a definition at all.
765 It could get in the way of other definitions, and unloading this
766 package could try to make the variable unbound. */
769 return sym;
772 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
773 doc: /* Define SYMBOL as a constant variable.
774 This declares that neither programs nor users should ever change the
775 value. This constancy is not actually enforced by Emacs Lisp, but
776 SYMBOL is marked as a special variable so that it is never lexically
777 bound.
779 The `defconst' form always sets the value of SYMBOL to the result of
780 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
781 what is set; buffer-local values are not affected. If SYMBOL has a
782 local binding, then this form sets the local binding's value.
783 However, you should normally not make local bindings for variables
784 defined with this form.
786 The optional DOCSTRING specifies the variable's documentation string.
787 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
788 (Lisp_Object args)
790 Lisp_Object sym, tem;
792 sym = XCAR (args);
793 if (CONSP (Fcdr (XCDR (XCDR (args)))))
794 error ("Too many arguments");
796 tem = eval_sub (Fcar (XCDR (args)));
797 if (!NILP (Vpurify_flag))
798 tem = Fpurecopy (tem);
799 Fset_default (sym, tem);
800 XSYMBOL (sym)->declared_special = 1;
801 tem = Fcar (XCDR (XCDR (args)));
802 if (!NILP (tem))
804 if (!NILP (Vpurify_flag))
805 tem = Fpurecopy (tem);
806 Fput (sym, Qvariable_documentation, tem);
808 Fput (sym, Qrisky_local_variable, Qt);
809 LOADHIST_ATTACH (sym);
810 return sym;
813 /* Make SYMBOL lexically scoped. */
814 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
815 Smake_var_non_special, 1, 1, 0,
816 doc: /* Internal function. */)
817 (Lisp_Object symbol)
819 CHECK_SYMBOL (symbol);
820 XSYMBOL (symbol)->declared_special = 0;
821 return Qnil;
825 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
826 doc: /* Bind variables according to VARLIST then eval BODY.
827 The value of the last form in BODY is returned.
828 Each element of VARLIST is a symbol (which is bound to nil)
829 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
830 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
831 usage: (let* VARLIST BODY...) */)
832 (Lisp_Object args)
834 Lisp_Object varlist, var, val, elt, lexenv;
835 ptrdiff_t count = SPECPDL_INDEX ();
836 struct gcpro gcpro1, gcpro2, gcpro3;
838 GCPRO3 (args, elt, varlist);
840 lexenv = Vinternal_interpreter_environment;
842 varlist = XCAR (args);
843 while (CONSP (varlist))
845 QUIT;
847 elt = XCAR (varlist);
848 if (SYMBOLP (elt))
850 var = elt;
851 val = Qnil;
853 else if (! NILP (Fcdr (Fcdr (elt))))
854 signal_error ("`let' bindings can have only one value-form", elt);
855 else
857 var = Fcar (elt);
858 val = eval_sub (Fcar (Fcdr (elt)));
861 if (!NILP (lexenv) && SYMBOLP (var)
862 && !XSYMBOL (var)->declared_special
863 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
864 /* Lexically bind VAR by adding it to the interpreter's binding
865 alist. */
867 Lisp_Object newenv
868 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
869 if (EQ (Vinternal_interpreter_environment, lexenv))
870 /* Save the old lexical environment on the specpdl stack,
871 but only for the first lexical binding, since we'll never
872 need to revert to one of the intermediate ones. */
873 specbind (Qinternal_interpreter_environment, newenv);
874 else
875 Vinternal_interpreter_environment = newenv;
877 else
878 specbind (var, val);
880 varlist = XCDR (varlist);
882 UNGCPRO;
883 val = Fprogn (XCDR (args));
884 return unbind_to (count, val);
887 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
888 doc: /* Bind variables according to VARLIST then eval BODY.
889 The value of the last form in BODY is returned.
890 Each element of VARLIST is a symbol (which is bound to nil)
891 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
892 All the VALUEFORMs are evalled before any symbols are bound.
893 usage: (let VARLIST BODY...) */)
894 (Lisp_Object args)
896 Lisp_Object *temps, tem, lexenv;
897 register Lisp_Object elt, varlist;
898 ptrdiff_t count = SPECPDL_INDEX ();
899 ptrdiff_t argnum;
900 struct gcpro gcpro1, gcpro2;
901 USE_SAFE_ALLOCA;
903 varlist = XCAR (args);
905 /* Make space to hold the values to give the bound variables. */
906 elt = Flength (varlist);
907 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
909 /* Compute the values and store them in `temps'. */
911 GCPRO2 (args, *temps);
912 gcpro2.nvars = 0;
914 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
916 QUIT;
917 elt = XCAR (varlist);
918 if (SYMBOLP (elt))
919 temps [argnum++] = Qnil;
920 else if (! NILP (Fcdr (Fcdr (elt))))
921 signal_error ("`let' bindings can have only one value-form", elt);
922 else
923 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
924 gcpro2.nvars = argnum;
926 UNGCPRO;
928 lexenv = Vinternal_interpreter_environment;
930 varlist = XCAR (args);
931 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
933 Lisp_Object var;
935 elt = XCAR (varlist);
936 var = SYMBOLP (elt) ? elt : Fcar (elt);
937 tem = temps[argnum++];
939 if (!NILP (lexenv) && SYMBOLP (var)
940 && !XSYMBOL (var)->declared_special
941 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
942 /* Lexically bind VAR by adding it to the lexenv alist. */
943 lexenv = Fcons (Fcons (var, tem), lexenv);
944 else
945 /* Dynamically bind VAR. */
946 specbind (var, tem);
949 if (!EQ (lexenv, Vinternal_interpreter_environment))
950 /* Instantiate a new lexical environment. */
951 specbind (Qinternal_interpreter_environment, lexenv);
953 elt = Fprogn (XCDR (args));
954 SAFE_FREE ();
955 return unbind_to (count, elt);
958 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
959 doc: /* If TEST yields non-nil, eval BODY... and repeat.
960 The order of execution is thus TEST, BODY, TEST, BODY and so on
961 until TEST returns nil.
962 usage: (while TEST BODY...) */)
963 (Lisp_Object args)
965 Lisp_Object test, body;
966 struct gcpro gcpro1, gcpro2;
968 GCPRO2 (test, body);
970 test = XCAR (args);
971 body = XCDR (args);
972 while (!NILP (eval_sub (test)))
974 QUIT;
975 Fprogn (body);
978 UNGCPRO;
979 return Qnil;
982 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
983 doc: /* Return result of expanding macros at top level of FORM.
984 If FORM is not a macro call, it is returned unchanged.
985 Otherwise, the macro is expanded and the expansion is considered
986 in place of FORM. When a non-macro-call results, it is returned.
988 The second optional arg ENVIRONMENT specifies an environment of macro
989 definitions to shadow the loaded ones for use in file byte-compilation. */)
990 (Lisp_Object form, Lisp_Object environment)
992 /* With cleanups from Hallvard Furuseth. */
993 register Lisp_Object expander, sym, def, tem;
995 while (1)
997 /* Come back here each time we expand a macro call,
998 in case it expands into another macro call. */
999 if (!CONSP (form))
1000 break;
1001 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1002 def = sym = XCAR (form);
1003 tem = Qnil;
1004 /* Trace symbols aliases to other symbols
1005 until we get a symbol that is not an alias. */
1006 while (SYMBOLP (def))
1008 QUIT;
1009 sym = def;
1010 tem = Fassq (sym, environment);
1011 if (NILP (tem))
1013 def = XSYMBOL (sym)->function;
1014 if (!NILP (def))
1015 continue;
1017 break;
1019 /* Right now TEM is the result from SYM in ENVIRONMENT,
1020 and if TEM is nil then DEF is SYM's function definition. */
1021 if (NILP (tem))
1023 /* SYM is not mentioned in ENVIRONMENT.
1024 Look at its function definition. */
1025 struct gcpro gcpro1;
1026 GCPRO1 (form);
1027 def = Fautoload_do_load (def, sym, Qmacro);
1028 UNGCPRO;
1029 if (!CONSP (def))
1030 /* Not defined or definition not suitable. */
1031 break;
1032 if (!EQ (XCAR (def), Qmacro))
1033 break;
1034 else expander = XCDR (def);
1036 else
1038 expander = XCDR (tem);
1039 if (NILP (expander))
1040 break;
1043 Lisp_Object newform = apply1 (expander, XCDR (form));
1044 if (EQ (form, newform))
1045 break;
1046 else
1047 form = newform;
1050 return form;
1053 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1054 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1055 TAG is evalled to get the tag to use; it must not be nil.
1057 Then the BODY is executed.
1058 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1059 If no throw happens, `catch' returns the value of the last BODY form.
1060 If a throw happens, it specifies the value to return from `catch'.
1061 usage: (catch TAG BODY...) */)
1062 (Lisp_Object args)
1064 register Lisp_Object tag;
1065 struct gcpro gcpro1;
1067 GCPRO1 (args);
1068 tag = eval_sub (XCAR (args));
1069 UNGCPRO;
1070 return internal_catch (tag, Fprogn, XCDR (args));
1073 /* Assert that E is true, as a comment only. Use this instead of
1074 eassert (E) when E contains variables that might be clobbered by a
1075 longjmp. */
1077 #define clobbered_eassert(E) ((void) 0)
1079 /* Set up a catch, then call C function FUNC on argument ARG.
1080 FUNC should return a Lisp_Object.
1081 This is how catches are done from within C code. */
1083 Lisp_Object
1084 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1086 /* This structure is made part of the chain `catchlist'. */
1087 struct handler *c;
1089 /* Fill in the components of c, and put it on the list. */
1090 PUSH_HANDLER (c, tag, CATCHER);
1092 /* Call FUNC. */
1093 if (! sys_setjmp (c->jmp))
1095 Lisp_Object val = (*func) (arg);
1096 clobbered_eassert (handlerlist == c);
1097 handlerlist = handlerlist->next;
1098 return val;
1100 else
1101 { /* Throw works by a longjmp that comes right here. */
1102 Lisp_Object val = handlerlist->val;
1103 clobbered_eassert (handlerlist == c);
1104 handlerlist = handlerlist->next;
1105 return val;
1109 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1110 jump to that CATCH, returning VALUE as the value of that catch.
1112 This is the guts of Fthrow and Fsignal; they differ only in the way
1113 they choose the catch tag to throw to. A catch tag for a
1114 condition-case form has a TAG of Qnil.
1116 Before each catch is discarded, unbind all special bindings and
1117 execute all unwind-protect clauses made above that catch. Unwind
1118 the handler stack as we go, so that the proper handlers are in
1119 effect for each unwind-protect clause we run. At the end, restore
1120 some static info saved in CATCH, and longjmp to the location
1121 specified there.
1123 This is used for correct unwinding in Fthrow and Fsignal. */
1125 static _Noreturn void
1126 unwind_to_catch (struct handler *catch, Lisp_Object value)
1128 bool last_time;
1130 eassert (catch->next);
1132 /* Save the value in the tag. */
1133 catch->val = value;
1135 /* Restore certain special C variables. */
1136 set_poll_suppress_count (catch->poll_suppress_count);
1137 unblock_input_to (catch->interrupt_input_blocked);
1138 immediate_quit = 0;
1142 /* Unwind the specpdl stack, and then restore the proper set of
1143 handlers. */
1144 unbind_to (handlerlist->pdlcount, Qnil);
1145 last_time = handlerlist == catch;
1146 if (! last_time)
1147 handlerlist = handlerlist->next;
1149 while (! last_time);
1151 eassert (handlerlist == catch);
1153 byte_stack_list = catch->byte_stack;
1154 gcprolist = catch->gcpro;
1155 #ifdef DEBUG_GCPRO
1156 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1157 #endif
1158 lisp_eval_depth = catch->lisp_eval_depth;
1160 sys_longjmp (catch->jmp, 1);
1163 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1164 doc: /* Throw to the catch for TAG and return VALUE from it.
1165 Both TAG and VALUE are evalled. */
1166 attributes: noreturn)
1167 (register Lisp_Object tag, Lisp_Object value)
1169 struct handler *c;
1171 if (!NILP (tag))
1172 for (c = handlerlist; c; c = c->next)
1174 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1175 unwind_to_catch (c, value);
1177 xsignal2 (Qno_catch, tag, value);
1181 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1182 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1183 If BODYFORM completes normally, its value is returned
1184 after executing the UNWINDFORMS.
1185 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1186 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1187 (Lisp_Object args)
1189 Lisp_Object val;
1190 ptrdiff_t count = SPECPDL_INDEX ();
1192 record_unwind_protect (unwind_body, XCDR (args));
1193 val = eval_sub (XCAR (args));
1194 return unbind_to (count, val);
1197 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1198 doc: /* Regain control when an error is signaled.
1199 Executes BODYFORM and returns its value if no error happens.
1200 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1201 where the BODY is made of Lisp expressions.
1203 A handler is applicable to an error
1204 if CONDITION-NAME is one of the error's condition names.
1205 If an error happens, the first applicable handler is run.
1207 The car of a handler may be a list of condition names instead of a
1208 single condition name; then it handles all of them. If the special
1209 condition name `debug' is present in this list, it allows another
1210 condition in the list to run the debugger if `debug-on-error' and the
1211 other usual mechanisms says it should (otherwise, `condition-case'
1212 suppresses the debugger).
1214 When a handler handles an error, control returns to the `condition-case'
1215 and it executes the handler's BODY...
1216 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1217 \(If VAR is nil, the handler can't access that information.)
1218 Then the value of the last BODY form is returned from the `condition-case'
1219 expression.
1221 See also the function `signal' for more info.
1222 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1223 (Lisp_Object args)
1225 Lisp_Object var = XCAR (args);
1226 Lisp_Object bodyform = XCAR (XCDR (args));
1227 Lisp_Object handlers = XCDR (XCDR (args));
1229 return internal_lisp_condition_case (var, bodyform, handlers);
1232 /* Like Fcondition_case, but the args are separate
1233 rather than passed in a list. Used by Fbyte_code. */
1235 Lisp_Object
1236 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1237 Lisp_Object handlers)
1239 Lisp_Object val;
1240 struct handler *c;
1241 struct handler *oldhandlerlist = handlerlist;
1242 int clausenb = 0;
1244 CHECK_SYMBOL (var);
1246 for (val = handlers; CONSP (val); val = XCDR (val))
1248 Lisp_Object tem = XCAR (val);
1249 clausenb++;
1250 if (! (NILP (tem)
1251 || (CONSP (tem)
1252 && (SYMBOLP (XCAR (tem))
1253 || CONSP (XCAR (tem))))))
1254 error ("Invalid condition handler: %s",
1255 SDATA (Fprin1_to_string (tem, Qt)));
1258 { /* The first clause is the one that should be checked first, so it should
1259 be added to handlerlist last. So we build in `clauses' a table that
1260 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1261 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1262 if (MAX_ALLOCA / word_size < clausenb)
1263 memory_full (SIZE_MAX);
1264 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1265 Lisp_Object *volatile clauses_volatile = clauses;
1266 int i = clausenb;
1267 for (val = handlers; CONSP (val); val = XCDR (val))
1268 clauses[--i] = XCAR (val);
1269 for (i = 0; i < clausenb; i++)
1271 Lisp_Object clause = clauses[i];
1272 Lisp_Object condition = XCAR (clause);
1273 if (!CONSP (condition))
1274 condition = Fcons (condition, Qnil);
1275 PUSH_HANDLER (c, condition, CONDITION_CASE);
1276 if (sys_setjmp (c->jmp))
1278 ptrdiff_t count = SPECPDL_INDEX ();
1279 Lisp_Object val = handlerlist->val;
1280 Lisp_Object *chosen_clause = clauses_volatile;
1281 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1282 chosen_clause++;
1283 handlerlist = oldhandlerlist;
1284 if (!NILP (var))
1286 if (!NILP (Vinternal_interpreter_environment))
1287 specbind (Qinternal_interpreter_environment,
1288 Fcons (Fcons (var, val),
1289 Vinternal_interpreter_environment));
1290 else
1291 specbind (var, val);
1293 val = Fprogn (XCDR (*chosen_clause));
1294 /* Note that this just undoes the binding of var; whoever
1295 longjumped to us unwound the stack to c.pdlcount before
1296 throwing. */
1297 if (!NILP (var))
1298 unbind_to (count, Qnil);
1299 return val;
1304 val = eval_sub (bodyform);
1305 handlerlist = oldhandlerlist;
1306 return val;
1309 /* Call the function BFUN with no arguments, catching errors within it
1310 according to HANDLERS. If there is an error, call HFUN with
1311 one argument which is the data that describes the error:
1312 (SIGNALNAME . DATA)
1314 HANDLERS can be a list of conditions to catch.
1315 If HANDLERS is Qt, catch all errors.
1316 If HANDLERS is Qerror, catch all errors
1317 but allow the debugger to run if that is enabled. */
1319 Lisp_Object
1320 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1321 Lisp_Object (*hfun) (Lisp_Object))
1323 Lisp_Object val;
1324 struct handler *c;
1326 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1327 if (sys_setjmp (c->jmp))
1329 Lisp_Object val = handlerlist->val;
1330 clobbered_eassert (handlerlist == c);
1331 handlerlist = handlerlist->next;
1332 return (*hfun) (val);
1335 val = (*bfun) ();
1336 clobbered_eassert (handlerlist == c);
1337 handlerlist = handlerlist->next;
1338 return val;
1341 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1343 Lisp_Object
1344 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1345 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1347 Lisp_Object val;
1348 struct handler *c;
1350 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1351 if (sys_setjmp (c->jmp))
1353 Lisp_Object val = handlerlist->val;
1354 clobbered_eassert (handlerlist == c);
1355 handlerlist = handlerlist->next;
1356 return (*hfun) (val);
1359 val = (*bfun) (arg);
1360 clobbered_eassert (handlerlist == c);
1361 handlerlist = handlerlist->next;
1362 return val;
1365 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1366 its arguments. */
1368 Lisp_Object
1369 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1370 Lisp_Object arg1,
1371 Lisp_Object arg2,
1372 Lisp_Object handlers,
1373 Lisp_Object (*hfun) (Lisp_Object))
1375 Lisp_Object val;
1376 struct handler *c;
1378 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1379 if (sys_setjmp (c->jmp))
1381 Lisp_Object val = handlerlist->val;
1382 clobbered_eassert (handlerlist == c);
1383 handlerlist = handlerlist->next;
1384 return (*hfun) (val);
1387 val = (*bfun) (arg1, arg2);
1388 clobbered_eassert (handlerlist == c);
1389 handlerlist = handlerlist->next;
1390 return val;
1393 /* Like internal_condition_case but call BFUN with NARGS as first,
1394 and ARGS as second argument. */
1396 Lisp_Object
1397 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1398 ptrdiff_t nargs,
1399 Lisp_Object *args,
1400 Lisp_Object handlers,
1401 Lisp_Object (*hfun) (Lisp_Object err,
1402 ptrdiff_t nargs,
1403 Lisp_Object *args))
1405 Lisp_Object val;
1406 struct handler *c;
1408 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1409 if (sys_setjmp (c->jmp))
1411 Lisp_Object val = handlerlist->val;
1412 clobbered_eassert (handlerlist == c);
1413 handlerlist = handlerlist->next;
1414 return (*hfun) (val, nargs, args);
1417 val = (*bfun) (nargs, args);
1418 clobbered_eassert (handlerlist == c);
1419 handlerlist = handlerlist->next;
1420 return val;
1424 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1425 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1426 Lisp_Object data);
1428 void
1429 process_quit_flag (void)
1431 Lisp_Object flag = Vquit_flag;
1432 Vquit_flag = Qnil;
1433 if (EQ (flag, Qkill_emacs))
1434 Fkill_emacs (Qnil);
1435 if (EQ (Vthrow_on_input, flag))
1436 Fthrow (Vthrow_on_input, Qt);
1437 Fsignal (Qquit, Qnil);
1440 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1441 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1442 This function does not return.
1444 An error symbol is a symbol with an `error-conditions' property
1445 that is a list of condition names.
1446 A handler for any of those names will get to handle this signal.
1447 The symbol `error' should normally be one of them.
1449 DATA should be a list. Its elements are printed as part of the error message.
1450 See Info anchor `(elisp)Definition of signal' for some details on how this
1451 error message is constructed.
1452 If the signal is handled, DATA is made available to the handler.
1453 See also the function `condition-case'. */)
1454 (Lisp_Object error_symbol, Lisp_Object data)
1456 /* When memory is full, ERROR-SYMBOL is nil,
1457 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1458 That is a special case--don't do this in other situations. */
1459 Lisp_Object conditions;
1460 Lisp_Object string;
1461 Lisp_Object real_error_symbol
1462 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1463 register Lisp_Object clause = Qnil;
1464 struct handler *h;
1466 immediate_quit = 0;
1467 abort_on_gc = 0;
1468 if (gc_in_progress || waiting_for_input)
1469 emacs_abort ();
1471 #if 0 /* rms: I don't know why this was here,
1472 but it is surely wrong for an error that is handled. */
1473 #ifdef HAVE_WINDOW_SYSTEM
1474 if (display_hourglass_p)
1475 cancel_hourglass ();
1476 #endif
1477 #endif
1479 /* This hook is used by edebug. */
1480 if (! NILP (Vsignal_hook_function)
1481 && ! NILP (error_symbol))
1483 /* Edebug takes care of restoring these variables when it exits. */
1484 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1485 max_lisp_eval_depth = lisp_eval_depth + 20;
1487 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1488 max_specpdl_size = SPECPDL_INDEX () + 40;
1490 call2 (Vsignal_hook_function, error_symbol, data);
1493 conditions = Fget (real_error_symbol, Qerror_conditions);
1495 /* Remember from where signal was called. Skip over the frame for
1496 `signal' itself. If a frame for `error' follows, skip that,
1497 too. Don't do this when ERROR_SYMBOL is nil, because that
1498 is a memory-full error. */
1499 Vsignaling_function = Qnil;
1500 if (!NILP (error_symbol))
1502 union specbinding *pdl = backtrace_next (backtrace_top ());
1503 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1504 pdl = backtrace_next (pdl);
1505 if (backtrace_p (pdl))
1506 Vsignaling_function = backtrace_function (pdl);
1509 for (h = handlerlist; h; h = h->next)
1511 if (h->type != CONDITION_CASE)
1512 continue;
1513 clause = find_handler_clause (h->tag_or_ch, conditions);
1514 if (!NILP (clause))
1515 break;
1518 if (/* Don't run the debugger for a memory-full error.
1519 (There is no room in memory to do that!) */
1520 !NILP (error_symbol)
1521 && (!NILP (Vdebug_on_signal)
1522 /* If no handler is present now, try to run the debugger. */
1523 || NILP (clause)
1524 /* A `debug' symbol in the handler list disables the normal
1525 suppression of the debugger. */
1526 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1527 /* Special handler that means "print a message and run debugger
1528 if requested". */
1529 || EQ (h->tag_or_ch, Qerror)))
1531 bool debugger_called
1532 = maybe_call_debugger (conditions, error_symbol, data);
1533 /* We can't return values to code which signaled an error, but we
1534 can continue code which has signaled a quit. */
1535 if (debugger_called && EQ (real_error_symbol, Qquit))
1536 return Qnil;
1539 if (!NILP (clause))
1541 Lisp_Object unwind_data
1542 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1544 unwind_to_catch (h, unwind_data);
1546 else
1548 if (handlerlist != &handlerlist_sentinel)
1549 /* FIXME: This will come right back here if there's no `top-level'
1550 catcher. A better solution would be to abort here, and instead
1551 add a catch-all condition handler so we never come here. */
1552 Fthrow (Qtop_level, Qt);
1555 if (! NILP (error_symbol))
1556 data = Fcons (error_symbol, data);
1558 string = Ferror_message_string (data);
1559 fatal ("%s", SDATA (string));
1562 /* Internal version of Fsignal that never returns.
1563 Used for anything but Qquit (which can return from Fsignal). */
1565 void
1566 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1568 Fsignal (error_symbol, data);
1569 emacs_abort ();
1572 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1574 void
1575 xsignal0 (Lisp_Object error_symbol)
1577 xsignal (error_symbol, Qnil);
1580 void
1581 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1583 xsignal (error_symbol, list1 (arg));
1586 void
1587 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1589 xsignal (error_symbol, list2 (arg1, arg2));
1592 void
1593 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1595 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1598 /* Signal `error' with message S, and additional arg ARG.
1599 If ARG is not a genuine list, make it a one-element list. */
1601 void
1602 signal_error (const char *s, Lisp_Object arg)
1604 Lisp_Object tortoise, hare;
1606 hare = tortoise = arg;
1607 while (CONSP (hare))
1609 hare = XCDR (hare);
1610 if (!CONSP (hare))
1611 break;
1613 hare = XCDR (hare);
1614 tortoise = XCDR (tortoise);
1616 if (EQ (hare, tortoise))
1617 break;
1620 if (!NILP (hare))
1621 arg = list1 (arg);
1623 xsignal (Qerror, Fcons (build_string (s), arg));
1627 /* Return true if LIST is a non-nil atom or
1628 a list containing one of CONDITIONS. */
1630 static bool
1631 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1633 if (NILP (list))
1634 return 0;
1635 if (! CONSP (list))
1636 return 1;
1638 while (CONSP (conditions))
1640 Lisp_Object this, tail;
1641 this = XCAR (conditions);
1642 for (tail = list; CONSP (tail); tail = XCDR (tail))
1643 if (EQ (XCAR (tail), this))
1644 return 1;
1645 conditions = XCDR (conditions);
1647 return 0;
1650 /* Return true if an error with condition-symbols CONDITIONS,
1651 and described by SIGNAL-DATA, should skip the debugger
1652 according to debugger-ignored-errors. */
1654 static bool
1655 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1657 Lisp_Object tail;
1658 bool first_string = 1;
1659 Lisp_Object error_message;
1661 error_message = Qnil;
1662 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1664 if (STRINGP (XCAR (tail)))
1666 if (first_string)
1668 error_message = Ferror_message_string (data);
1669 first_string = 0;
1672 if (fast_string_match (XCAR (tail), error_message) >= 0)
1673 return 1;
1675 else
1677 Lisp_Object contail;
1679 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1680 if (EQ (XCAR (tail), XCAR (contail)))
1681 return 1;
1685 return 0;
1688 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1689 SIG and DATA describe the signal. There are two ways to pass them:
1690 = SIG is the error symbol, and DATA is the rest of the data.
1691 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1692 This is for memory-full errors only. */
1693 static bool
1694 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1696 Lisp_Object combined_data;
1698 combined_data = Fcons (sig, data);
1700 if (
1701 /* Don't try to run the debugger with interrupts blocked.
1702 The editing loop would return anyway. */
1703 ! input_blocked_p ()
1704 && NILP (Vinhibit_debugger)
1705 /* Does user want to enter debugger for this kind of error? */
1706 && (EQ (sig, Qquit)
1707 ? debug_on_quit
1708 : wants_debugger (Vdebug_on_error, conditions))
1709 && ! skip_debugger (conditions, combined_data)
1710 /* RMS: What's this for? */
1711 && when_entered_debugger < num_nonmacro_input_events)
1713 call_debugger (list2 (Qerror, combined_data));
1714 return 1;
1717 return 0;
1720 static Lisp_Object
1721 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1723 register Lisp_Object h;
1725 /* t is used by handlers for all conditions, set up by C code. */
1726 if (EQ (handlers, Qt))
1727 return Qt;
1729 /* error is used similarly, but means print an error message
1730 and run the debugger if that is enabled. */
1731 if (EQ (handlers, Qerror))
1732 return Qt;
1734 for (h = handlers; CONSP (h); h = XCDR (h))
1736 Lisp_Object handler = XCAR (h);
1737 if (!NILP (Fmemq (handler, conditions)))
1738 return handlers;
1741 return Qnil;
1745 /* Dump an error message; called like vprintf. */
1746 void
1747 verror (const char *m, va_list ap)
1749 char buf[4000];
1750 ptrdiff_t size = sizeof buf;
1751 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1752 char *buffer = buf;
1753 ptrdiff_t used;
1754 Lisp_Object string;
1756 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1757 string = make_string (buffer, used);
1758 if (buffer != buf)
1759 xfree (buffer);
1761 xsignal1 (Qerror, string);
1765 /* Dump an error message; called like printf. */
1767 /* VARARGS 1 */
1768 void
1769 error (const char *m, ...)
1771 va_list ap;
1772 va_start (ap, m);
1773 verror (m, ap);
1776 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1777 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1778 This means it contains a description for how to read arguments to give it.
1779 The value is nil for an invalid function or a symbol with no function
1780 definition.
1782 Interactively callable functions include strings and vectors (treated
1783 as keyboard macros), lambda-expressions that contain a top-level call
1784 to `interactive', autoload definitions made by `autoload' with non-nil
1785 fourth argument, and some of the built-in functions of Lisp.
1787 Also, a symbol satisfies `commandp' if its function definition does so.
1789 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1790 then strings and vectors are not accepted. */)
1791 (Lisp_Object function, Lisp_Object for_call_interactively)
1793 register Lisp_Object fun;
1794 register Lisp_Object funcar;
1795 Lisp_Object if_prop = Qnil;
1797 fun = function;
1799 fun = indirect_function (fun); /* Check cycles. */
1800 if (NILP (fun))
1801 return Qnil;
1803 /* Check an `interactive-form' property if present, analogous to the
1804 function-documentation property. */
1805 fun = function;
1806 while (SYMBOLP (fun))
1808 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1809 if (!NILP (tmp))
1810 if_prop = Qt;
1811 fun = Fsymbol_function (fun);
1814 /* Emacs primitives are interactive if their DEFUN specifies an
1815 interactive spec. */
1816 if (SUBRP (fun))
1817 return XSUBR (fun)->intspec ? Qt : if_prop;
1819 /* Bytecode objects are interactive if they are long enough to
1820 have an element whose index is COMPILED_INTERACTIVE, which is
1821 where the interactive spec is stored. */
1822 else if (COMPILEDP (fun))
1823 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1824 ? Qt : if_prop);
1826 /* Strings and vectors are keyboard macros. */
1827 if (STRINGP (fun) || VECTORP (fun))
1828 return (NILP (for_call_interactively) ? Qt : Qnil);
1830 /* Lists may represent commands. */
1831 if (!CONSP (fun))
1832 return Qnil;
1833 funcar = XCAR (fun);
1834 if (EQ (funcar, Qclosure))
1835 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1836 ? Qt : if_prop);
1837 else if (EQ (funcar, Qlambda))
1838 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1839 else if (EQ (funcar, Qautoload))
1840 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1841 else
1842 return Qnil;
1845 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1846 doc: /* Define FUNCTION to autoload from FILE.
1847 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1848 Third arg DOCSTRING is documentation for the function.
1849 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1850 Fifth arg TYPE indicates the type of the object:
1851 nil or omitted says FUNCTION is a function,
1852 `keymap' says FUNCTION is really a keymap, and
1853 `macro' or t says FUNCTION is really a macro.
1854 Third through fifth args give info about the real definition.
1855 They default to nil.
1856 If FUNCTION is already defined other than as an autoload,
1857 this does nothing and returns nil. */)
1858 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1860 CHECK_SYMBOL (function);
1861 CHECK_STRING (file);
1863 /* If function is defined and not as an autoload, don't override. */
1864 if (!NILP (XSYMBOL (function)->function)
1865 && !AUTOLOADP (XSYMBOL (function)->function))
1866 return Qnil;
1868 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1869 /* `read1' in lread.c has found the docstring starting with "\
1870 and assumed the docstring will be provided by Snarf-documentation, so it
1871 passed us 0 instead. But that leads to accidental sharing in purecopy's
1872 hash-consing, so we use a (hopefully) unique integer instead. */
1873 docstring = make_number (XHASH (function));
1874 return Fdefalias (function,
1875 list5 (Qautoload, file, docstring, interactive, type),
1876 Qnil);
1879 void
1880 un_autoload (Lisp_Object oldqueue)
1882 Lisp_Object queue, first, second;
1884 /* Queue to unwind is current value of Vautoload_queue.
1885 oldqueue is the shadowed value to leave in Vautoload_queue. */
1886 queue = Vautoload_queue;
1887 Vautoload_queue = oldqueue;
1888 while (CONSP (queue))
1890 first = XCAR (queue);
1891 second = Fcdr (first);
1892 first = Fcar (first);
1893 if (EQ (first, make_number (0)))
1894 Vfeatures = second;
1895 else
1896 Ffset (first, second);
1897 queue = XCDR (queue);
1901 /* Load an autoloaded function.
1902 FUNNAME is the symbol which is the function's name.
1903 FUNDEF is the autoload definition (a list). */
1905 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1906 doc: /* Load FUNDEF which should be an autoload.
1907 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1908 in which case the function returns the new autoloaded function value.
1909 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1910 it defines a macro. */)
1911 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1913 ptrdiff_t count = SPECPDL_INDEX ();
1914 struct gcpro gcpro1, gcpro2, gcpro3;
1916 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1917 return fundef;
1919 if (EQ (macro_only, Qmacro))
1921 Lisp_Object kind = Fnth (make_number (4), fundef);
1922 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1923 return fundef;
1926 /* This is to make sure that loadup.el gives a clear picture
1927 of what files are preloaded and when. */
1928 if (! NILP (Vpurify_flag))
1929 error ("Attempt to autoload %s while preparing to dump",
1930 SDATA (SYMBOL_NAME (funname)));
1932 CHECK_SYMBOL (funname);
1933 GCPRO3 (funname, fundef, macro_only);
1935 /* Preserve the match data. */
1936 record_unwind_save_match_data ();
1938 /* If autoloading gets an error (which includes the error of failing
1939 to define the function being called), we use Vautoload_queue
1940 to undo function definitions and `provide' calls made by
1941 the function. We do this in the specific case of autoloading
1942 because autoloading is not an explicit request "load this file",
1943 but rather a request to "call this function".
1945 The value saved here is to be restored into Vautoload_queue. */
1946 record_unwind_protect (un_autoload, Vautoload_queue);
1947 Vautoload_queue = Qt;
1948 /* If `macro_only', assume this autoload to be a "best-effort",
1949 so don't signal an error if autoloading fails. */
1950 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1952 /* Once loading finishes, don't undo it. */
1953 Vautoload_queue = Qt;
1954 unbind_to (count, Qnil);
1956 UNGCPRO;
1958 if (NILP (funname))
1959 return Qnil;
1960 else
1962 Lisp_Object fun = Findirect_function (funname, Qnil);
1964 if (!NILP (Fequal (fun, fundef)))
1965 error ("Autoloading failed to define function %s",
1966 SDATA (SYMBOL_NAME (funname)));
1967 else
1968 return fun;
1973 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1974 doc: /* Evaluate FORM and return its value.
1975 If LEXICAL is t, evaluate using lexical scoping.
1976 LEXICAL can also be an actual lexical environment, in the form of an
1977 alist mapping symbols to their value. */)
1978 (Lisp_Object form, Lisp_Object lexical)
1980 ptrdiff_t count = SPECPDL_INDEX ();
1981 specbind (Qinternal_interpreter_environment,
1982 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1983 return unbind_to (count, eval_sub (form));
1986 /* Grow the specpdl stack by one entry.
1987 The caller should have already initialized the entry.
1988 Signal an error on stack overflow.
1990 Make sure that there is always one unused entry past the top of the
1991 stack, so that the just-initialized entry is safely unwound if
1992 memory exhausted and an error is signaled here. Also, allocate a
1993 never-used entry just before the bottom of the stack; sometimes its
1994 address is taken. */
1996 static void
1997 grow_specpdl (void)
1999 specpdl_ptr++;
2001 if (specpdl_ptr == specpdl + specpdl_size)
2003 ptrdiff_t count = SPECPDL_INDEX ();
2004 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2005 union specbinding *pdlvec = specpdl - 1;
2006 ptrdiff_t pdlvecsize = specpdl_size + 1;
2007 if (max_size <= specpdl_size)
2009 if (max_specpdl_size < 400)
2010 max_size = max_specpdl_size = 400;
2011 if (max_size <= specpdl_size)
2012 signal_error ("Variable binding depth exceeds max-specpdl-size",
2013 Qnil);
2015 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2016 specpdl = pdlvec + 1;
2017 specpdl_size = pdlvecsize - 1;
2018 specpdl_ptr = specpdl + count;
2022 ptrdiff_t
2023 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2025 ptrdiff_t count = SPECPDL_INDEX ();
2027 eassert (nargs >= UNEVALLED);
2028 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2029 specpdl_ptr->bt.debug_on_exit = false;
2030 specpdl_ptr->bt.function = function;
2031 specpdl_ptr->bt.args = args;
2032 specpdl_ptr->bt.nargs = nargs;
2033 grow_specpdl ();
2035 return count;
2038 /* Eval a sub-expression of the current expression (i.e. in the same
2039 lexical scope). */
2040 Lisp_Object
2041 eval_sub (Lisp_Object form)
2043 Lisp_Object fun, val, original_fun, original_args;
2044 Lisp_Object funcar;
2045 struct gcpro gcpro1, gcpro2, gcpro3;
2046 ptrdiff_t count;
2048 if (SYMBOLP (form))
2050 /* Look up its binding in the lexical environment.
2051 We do not pay attention to the declared_special flag here, since we
2052 already did that when let-binding the variable. */
2053 Lisp_Object lex_binding
2054 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2055 ? Fassq (form, Vinternal_interpreter_environment)
2056 : Qnil;
2057 if (CONSP (lex_binding))
2058 return XCDR (lex_binding);
2059 else
2060 return Fsymbol_value (form);
2063 if (!CONSP (form))
2064 return form;
2066 QUIT;
2068 GCPRO1 (form);
2069 maybe_gc ();
2070 UNGCPRO;
2072 if (++lisp_eval_depth > max_lisp_eval_depth)
2074 if (max_lisp_eval_depth < 100)
2075 max_lisp_eval_depth = 100;
2076 if (lisp_eval_depth > max_lisp_eval_depth)
2077 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2080 original_fun = XCAR (form);
2081 original_args = XCDR (form);
2083 /* This also protects them from gc. */
2084 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2086 if (debug_on_next_call)
2087 do_debug_on_call (Qt, count);
2089 /* At this point, only original_fun and original_args
2090 have values that will be used below. */
2091 retry:
2093 /* Optimize for no indirection. */
2094 fun = original_fun;
2095 if (!SYMBOLP (fun))
2096 fun = Ffunction (Fcons (fun, Qnil));
2097 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2098 fun = indirect_function (fun);
2100 if (SUBRP (fun))
2102 Lisp_Object numargs;
2103 Lisp_Object argvals[8];
2104 Lisp_Object args_left;
2105 register int i, maxargs;
2107 args_left = original_args;
2108 numargs = Flength (args_left);
2110 check_cons_list ();
2112 if (XINT (numargs) < XSUBR (fun)->min_args
2113 || (XSUBR (fun)->max_args >= 0
2114 && XSUBR (fun)->max_args < XINT (numargs)))
2115 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2117 else if (XSUBR (fun)->max_args == UNEVALLED)
2118 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2119 else if (XSUBR (fun)->max_args == MANY)
2121 /* Pass a vector of evaluated arguments. */
2122 Lisp_Object *vals;
2123 ptrdiff_t argnum = 0;
2124 USE_SAFE_ALLOCA;
2126 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2128 GCPRO3 (args_left, fun, fun);
2129 gcpro3.var = vals;
2130 gcpro3.nvars = 0;
2132 while (!NILP (args_left))
2134 vals[argnum++] = eval_sub (Fcar (args_left));
2135 args_left = Fcdr (args_left);
2136 gcpro3.nvars = argnum;
2139 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2141 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2142 UNGCPRO;
2143 SAFE_FREE ();
2145 else
2147 GCPRO3 (args_left, fun, fun);
2148 gcpro3.var = argvals;
2149 gcpro3.nvars = 0;
2151 maxargs = XSUBR (fun)->max_args;
2152 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2154 argvals[i] = eval_sub (Fcar (args_left));
2155 gcpro3.nvars = ++i;
2158 UNGCPRO;
2160 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2162 switch (i)
2164 case 0:
2165 val = (XSUBR (fun)->function.a0 ());
2166 break;
2167 case 1:
2168 val = (XSUBR (fun)->function.a1 (argvals[0]));
2169 break;
2170 case 2:
2171 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2172 break;
2173 case 3:
2174 val = (XSUBR (fun)->function.a3
2175 (argvals[0], argvals[1], argvals[2]));
2176 break;
2177 case 4:
2178 val = (XSUBR (fun)->function.a4
2179 (argvals[0], argvals[1], argvals[2], argvals[3]));
2180 break;
2181 case 5:
2182 val = (XSUBR (fun)->function.a5
2183 (argvals[0], argvals[1], argvals[2], argvals[3],
2184 argvals[4]));
2185 break;
2186 case 6:
2187 val = (XSUBR (fun)->function.a6
2188 (argvals[0], argvals[1], argvals[2], argvals[3],
2189 argvals[4], argvals[5]));
2190 break;
2191 case 7:
2192 val = (XSUBR (fun)->function.a7
2193 (argvals[0], argvals[1], argvals[2], argvals[3],
2194 argvals[4], argvals[5], argvals[6]));
2195 break;
2197 case 8:
2198 val = (XSUBR (fun)->function.a8
2199 (argvals[0], argvals[1], argvals[2], argvals[3],
2200 argvals[4], argvals[5], argvals[6], argvals[7]));
2201 break;
2203 default:
2204 /* Someone has created a subr that takes more arguments than
2205 is supported by this code. We need to either rewrite the
2206 subr to use a different argument protocol, or add more
2207 cases to this switch. */
2208 emacs_abort ();
2212 else if (COMPILEDP (fun))
2213 val = apply_lambda (fun, original_args, count);
2214 else
2216 if (NILP (fun))
2217 xsignal1 (Qvoid_function, original_fun);
2218 if (!CONSP (fun))
2219 xsignal1 (Qinvalid_function, original_fun);
2220 funcar = XCAR (fun);
2221 if (!SYMBOLP (funcar))
2222 xsignal1 (Qinvalid_function, original_fun);
2223 if (EQ (funcar, Qautoload))
2225 Fautoload_do_load (fun, original_fun, Qnil);
2226 goto retry;
2228 if (EQ (funcar, Qmacro))
2230 ptrdiff_t count1 = SPECPDL_INDEX ();
2231 Lisp_Object exp;
2232 /* Bind lexical-binding during expansion of the macro, so the
2233 macro can know reliably if the code it outputs will be
2234 interpreted using lexical-binding or not. */
2235 specbind (Qlexical_binding,
2236 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2237 exp = apply1 (Fcdr (fun), original_args);
2238 unbind_to (count1, Qnil);
2239 val = eval_sub (exp);
2241 else if (EQ (funcar, Qlambda)
2242 || EQ (funcar, Qclosure))
2243 val = apply_lambda (fun, original_args, count);
2244 else
2245 xsignal1 (Qinvalid_function, original_fun);
2247 check_cons_list ();
2249 lisp_eval_depth--;
2250 if (backtrace_debug_on_exit (specpdl + count))
2251 val = call_debugger (list2 (Qexit, val));
2252 specpdl_ptr--;
2254 return val;
2257 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2258 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2259 Then return the value FUNCTION returns.
2260 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2261 usage: (apply FUNCTION &rest ARGUMENTS) */)
2262 (ptrdiff_t nargs, Lisp_Object *args)
2264 ptrdiff_t i, numargs, funcall_nargs;
2265 register Lisp_Object *funcall_args = NULL;
2266 register Lisp_Object spread_arg = args[nargs - 1];
2267 Lisp_Object fun = args[0];
2268 Lisp_Object retval;
2269 USE_SAFE_ALLOCA;
2271 CHECK_LIST (spread_arg);
2273 numargs = XINT (Flength (spread_arg));
2275 if (numargs == 0)
2276 return Ffuncall (nargs - 1, args);
2277 else if (numargs == 1)
2279 args [nargs - 1] = XCAR (spread_arg);
2280 return Ffuncall (nargs, args);
2283 numargs += nargs - 2;
2285 /* Optimize for no indirection. */
2286 if (SYMBOLP (fun) && !NILP (fun)
2287 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2289 fun = indirect_function (fun);
2290 if (NILP (fun))
2291 /* Let funcall get the error. */
2292 fun = args[0];
2295 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2296 /* Don't hide an error by adding missing arguments. */
2297 && numargs >= XSUBR (fun)->min_args)
2299 /* Avoid making funcall cons up a yet another new vector of arguments
2300 by explicitly supplying nil's for optional values. */
2301 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2302 memclear (funcall_args + numargs + 1,
2303 (XSUBR (fun)->max_args - numargs) * word_size);
2304 funcall_nargs = 1 + XSUBR (fun)->max_args;
2306 else
2307 { /* We add 1 to numargs because funcall_args includes the
2308 function itself as well as its arguments. */
2309 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2310 funcall_nargs = 1 + numargs;
2313 memcpy (funcall_args, args, nargs * word_size);
2314 /* Spread the last arg we got. Its first element goes in
2315 the slot that it used to occupy, hence this value of I. */
2316 i = nargs - 1;
2317 while (!NILP (spread_arg))
2319 funcall_args [i++] = XCAR (spread_arg);
2320 spread_arg = XCDR (spread_arg);
2323 /* Ffuncall gcpro's all of its args. */
2324 retval = Ffuncall (funcall_nargs, funcall_args);
2326 SAFE_FREE ();
2327 return retval;
2330 /* Run hook variables in various ways. */
2332 static Lisp_Object
2333 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2335 Ffuncall (nargs, args);
2336 return Qnil;
2339 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2340 doc: /* Run each hook in HOOKS.
2341 Each argument should be a symbol, a hook variable.
2342 These symbols are processed in the order specified.
2343 If a hook symbol has a non-nil value, that value may be a function
2344 or a list of functions to be called to run the hook.
2345 If the value is a function, it is called with no arguments.
2346 If it is a list, the elements are called, in order, with no arguments.
2348 Major modes should not use this function directly to run their mode
2349 hook; they should use `run-mode-hooks' instead.
2351 Do not use `make-local-variable' to make a hook variable buffer-local.
2352 Instead, use `add-hook' and specify t for the LOCAL argument.
2353 usage: (run-hooks &rest HOOKS) */)
2354 (ptrdiff_t nargs, Lisp_Object *args)
2356 ptrdiff_t i;
2358 for (i = 0; i < nargs; i++)
2359 run_hook (args[i]);
2361 return Qnil;
2364 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2365 Srun_hook_with_args, 1, MANY, 0,
2366 doc: /* Run HOOK with the specified arguments ARGS.
2367 HOOK should be a symbol, a hook variable. The value of HOOK
2368 may be nil, a function, or a list of functions. Call each
2369 function in order with arguments ARGS. The final return value
2370 is unspecified.
2372 Do not use `make-local-variable' to make a hook variable buffer-local.
2373 Instead, use `add-hook' and specify t for the LOCAL argument.
2374 usage: (run-hook-with-args HOOK &rest ARGS) */)
2375 (ptrdiff_t nargs, Lisp_Object *args)
2377 return run_hook_with_args (nargs, args, funcall_nil);
2380 /* NB this one still documents a specific non-nil return value.
2381 (As did run-hook-with-args and run-hook-with-args-until-failure
2382 until they were changed in 24.1.) */
2383 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2384 Srun_hook_with_args_until_success, 1, MANY, 0,
2385 doc: /* Run HOOK with the specified arguments ARGS.
2386 HOOK should be a symbol, a hook variable. The value of HOOK
2387 may be nil, a function, or a list of functions. Call each
2388 function in order with arguments ARGS, stopping at the first
2389 one that returns non-nil, and return that value. Otherwise (if
2390 all functions return nil, or if there are no functions to call),
2391 return nil.
2393 Do not use `make-local-variable' to make a hook variable buffer-local.
2394 Instead, use `add-hook' and specify t for the LOCAL argument.
2395 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2396 (ptrdiff_t nargs, Lisp_Object *args)
2398 return run_hook_with_args (nargs, args, Ffuncall);
2401 static Lisp_Object
2402 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2404 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2407 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2408 Srun_hook_with_args_until_failure, 1, MANY, 0,
2409 doc: /* Run HOOK with the specified arguments ARGS.
2410 HOOK should be a symbol, a hook variable. The value of HOOK
2411 may be nil, a function, or a list of functions. Call each
2412 function in order with arguments ARGS, stopping at the first
2413 one that returns nil, and return nil. Otherwise (if all functions
2414 return non-nil, or if there are no functions to call), return non-nil
2415 \(do not rely on the precise return value in this case).
2417 Do not use `make-local-variable' to make a hook variable buffer-local.
2418 Instead, use `add-hook' and specify t for the LOCAL argument.
2419 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2420 (ptrdiff_t nargs, Lisp_Object *args)
2422 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2425 static Lisp_Object
2426 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2428 Lisp_Object tmp = args[0], ret;
2429 args[0] = args[1];
2430 args[1] = tmp;
2431 ret = Ffuncall (nargs, args);
2432 args[1] = args[0];
2433 args[0] = tmp;
2434 return ret;
2437 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2438 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2439 I.e. instead of calling each function FUN directly with arguments ARGS,
2440 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2441 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2442 aborts and returns that value.
2443 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2444 (ptrdiff_t nargs, Lisp_Object *args)
2446 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2449 /* ARGS[0] should be a hook symbol.
2450 Call each of the functions in the hook value, passing each of them
2451 as arguments all the rest of ARGS (all NARGS - 1 elements).
2452 FUNCALL specifies how to call each function on the hook.
2453 The caller (or its caller, etc) must gcpro all of ARGS,
2454 except that it isn't necessary to gcpro ARGS[0]. */
2456 Lisp_Object
2457 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2458 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2460 Lisp_Object sym, val, ret = Qnil;
2461 struct gcpro gcpro1, gcpro2, gcpro3;
2463 /* If we are dying or still initializing,
2464 don't do anything--it would probably crash if we tried. */
2465 if (NILP (Vrun_hooks))
2466 return Qnil;
2468 sym = args[0];
2469 val = find_symbol_value (sym);
2471 if (EQ (val, Qunbound) || NILP (val))
2472 return ret;
2473 else if (!CONSP (val) || FUNCTIONP (val))
2475 args[0] = val;
2476 return funcall (nargs, args);
2478 else
2480 Lisp_Object global_vals = Qnil;
2481 GCPRO3 (sym, val, global_vals);
2483 for (;
2484 CONSP (val) && NILP (ret);
2485 val = XCDR (val))
2487 if (EQ (XCAR (val), Qt))
2489 /* t indicates this hook has a local binding;
2490 it means to run the global binding too. */
2491 global_vals = Fdefault_value (sym);
2492 if (NILP (global_vals)) continue;
2494 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2496 args[0] = global_vals;
2497 ret = funcall (nargs, args);
2499 else
2501 for (;
2502 CONSP (global_vals) && NILP (ret);
2503 global_vals = XCDR (global_vals))
2505 args[0] = XCAR (global_vals);
2506 /* In a global value, t should not occur. If it does, we
2507 must ignore it to avoid an endless loop. */
2508 if (!EQ (args[0], Qt))
2509 ret = funcall (nargs, args);
2513 else
2515 args[0] = XCAR (val);
2516 ret = funcall (nargs, args);
2520 UNGCPRO;
2521 return ret;
2525 /* Run the hook HOOK, giving each function no args. */
2527 void
2528 run_hook (Lisp_Object hook)
2530 Frun_hook_with_args (1, &hook);
2533 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2535 void
2536 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2538 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2541 /* Apply fn to arg. */
2542 Lisp_Object
2543 apply1 (Lisp_Object fn, Lisp_Object arg)
2545 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2548 /* Call function fn on no arguments. */
2549 Lisp_Object
2550 call0 (Lisp_Object fn)
2552 return Ffuncall (1, &fn);
2555 /* Call function fn with 1 argument arg1. */
2556 /* ARGSUSED */
2557 Lisp_Object
2558 call1 (Lisp_Object fn, Lisp_Object arg1)
2560 return CALLN (Ffuncall, fn, arg1);
2563 /* Call function fn with 2 arguments arg1, arg2. */
2564 /* ARGSUSED */
2565 Lisp_Object
2566 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2568 return CALLN (Ffuncall, fn, arg1, arg2);
2571 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2572 /* ARGSUSED */
2573 Lisp_Object
2574 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2576 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2579 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2580 /* ARGSUSED */
2581 Lisp_Object
2582 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2583 Lisp_Object arg4)
2585 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2588 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2589 /* ARGSUSED */
2590 Lisp_Object
2591 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2592 Lisp_Object arg4, Lisp_Object arg5)
2594 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2597 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2598 /* ARGSUSED */
2599 Lisp_Object
2600 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2601 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2603 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2606 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2607 /* ARGSUSED */
2608 Lisp_Object
2609 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2610 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2612 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2615 /* The caller should GCPRO all the elements of ARGS. */
2617 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2618 doc: /* Non-nil if OBJECT is a function. */)
2619 (Lisp_Object object)
2621 if (FUNCTIONP (object))
2622 return Qt;
2623 return Qnil;
2626 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2627 doc: /* Call first argument as a function, passing remaining arguments to it.
2628 Return the value that function returns.
2629 Thus, (funcall 'cons 'x 'y) returns (x . y).
2630 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2631 (ptrdiff_t nargs, Lisp_Object *args)
2633 Lisp_Object fun, original_fun;
2634 Lisp_Object funcar;
2635 ptrdiff_t numargs = nargs - 1;
2636 Lisp_Object lisp_numargs;
2637 Lisp_Object val;
2638 Lisp_Object *internal_args;
2639 ptrdiff_t count;
2641 QUIT;
2643 if (++lisp_eval_depth > max_lisp_eval_depth)
2645 if (max_lisp_eval_depth < 100)
2646 max_lisp_eval_depth = 100;
2647 if (lisp_eval_depth > max_lisp_eval_depth)
2648 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2651 /* This also GCPROs them. */
2652 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2654 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2655 maybe_gc ();
2657 if (debug_on_next_call)
2658 do_debug_on_call (Qlambda, count);
2660 check_cons_list ();
2662 original_fun = args[0];
2664 retry:
2666 /* Optimize for no indirection. */
2667 fun = original_fun;
2668 if (SYMBOLP (fun) && !NILP (fun)
2669 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2670 fun = indirect_function (fun);
2672 if (SUBRP (fun))
2674 if (numargs < XSUBR (fun)->min_args
2675 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2677 XSETFASTINT (lisp_numargs, numargs);
2678 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2681 else if (XSUBR (fun)->max_args == UNEVALLED)
2682 xsignal1 (Qinvalid_function, original_fun);
2684 else if (XSUBR (fun)->max_args == MANY)
2685 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2686 else
2688 Lisp_Object internal_argbuf[8];
2689 if (XSUBR (fun)->max_args > numargs)
2691 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2692 internal_args = internal_argbuf;
2693 memcpy (internal_args, args + 1, numargs * word_size);
2694 memclear (internal_args + numargs,
2695 (XSUBR (fun)->max_args - numargs) * word_size);
2697 else
2698 internal_args = args + 1;
2699 switch (XSUBR (fun)->max_args)
2701 case 0:
2702 val = (XSUBR (fun)->function.a0 ());
2703 break;
2704 case 1:
2705 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2706 break;
2707 case 2:
2708 val = (XSUBR (fun)->function.a2
2709 (internal_args[0], internal_args[1]));
2710 break;
2711 case 3:
2712 val = (XSUBR (fun)->function.a3
2713 (internal_args[0], internal_args[1], internal_args[2]));
2714 break;
2715 case 4:
2716 val = (XSUBR (fun)->function.a4
2717 (internal_args[0], internal_args[1], internal_args[2],
2718 internal_args[3]));
2719 break;
2720 case 5:
2721 val = (XSUBR (fun)->function.a5
2722 (internal_args[0], internal_args[1], internal_args[2],
2723 internal_args[3], internal_args[4]));
2724 break;
2725 case 6:
2726 val = (XSUBR (fun)->function.a6
2727 (internal_args[0], internal_args[1], internal_args[2],
2728 internal_args[3], internal_args[4], internal_args[5]));
2729 break;
2730 case 7:
2731 val = (XSUBR (fun)->function.a7
2732 (internal_args[0], internal_args[1], internal_args[2],
2733 internal_args[3], internal_args[4], internal_args[5],
2734 internal_args[6]));
2735 break;
2737 case 8:
2738 val = (XSUBR (fun)->function.a8
2739 (internal_args[0], internal_args[1], internal_args[2],
2740 internal_args[3], internal_args[4], internal_args[5],
2741 internal_args[6], internal_args[7]));
2742 break;
2744 default:
2746 /* If a subr takes more than 8 arguments without using MANY
2747 or UNEVALLED, we need to extend this function to support it.
2748 Until this is done, there is no way to call the function. */
2749 emacs_abort ();
2753 else if (COMPILEDP (fun))
2754 val = funcall_lambda (fun, numargs, args + 1);
2755 else
2757 if (NILP (fun))
2758 xsignal1 (Qvoid_function, original_fun);
2759 if (!CONSP (fun))
2760 xsignal1 (Qinvalid_function, original_fun);
2761 funcar = XCAR (fun);
2762 if (!SYMBOLP (funcar))
2763 xsignal1 (Qinvalid_function, original_fun);
2764 if (EQ (funcar, Qlambda)
2765 || EQ (funcar, Qclosure))
2766 val = funcall_lambda (fun, numargs, args + 1);
2767 else if (EQ (funcar, Qautoload))
2769 Fautoload_do_load (fun, original_fun, Qnil);
2770 check_cons_list ();
2771 goto retry;
2773 else
2774 xsignal1 (Qinvalid_function, original_fun);
2776 check_cons_list ();
2777 lisp_eval_depth--;
2778 if (backtrace_debug_on_exit (specpdl + count))
2779 val = call_debugger (list2 (Qexit, val));
2780 specpdl_ptr--;
2781 return val;
2784 static Lisp_Object
2785 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2787 Lisp_Object args_left;
2788 ptrdiff_t i;
2789 EMACS_INT numargs;
2790 register Lisp_Object *arg_vector;
2791 struct gcpro gcpro1, gcpro2, gcpro3;
2792 register Lisp_Object tem;
2793 USE_SAFE_ALLOCA;
2795 numargs = XFASTINT (Flength (args));
2796 SAFE_ALLOCA_LISP (arg_vector, numargs);
2797 args_left = args;
2799 GCPRO3 (*arg_vector, args_left, fun);
2800 gcpro1.nvars = 0;
2802 for (i = 0; i < numargs; )
2804 tem = Fcar (args_left), args_left = Fcdr (args_left);
2805 tem = eval_sub (tem);
2806 arg_vector[i++] = tem;
2807 gcpro1.nvars = i;
2810 UNGCPRO;
2812 set_backtrace_args (specpdl + count, arg_vector, i);
2813 tem = funcall_lambda (fun, numargs, arg_vector);
2815 /* Do the debug-on-exit now, while arg_vector still exists. */
2816 if (backtrace_debug_on_exit (specpdl + count))
2818 /* Don't do it again when we return to eval. */
2819 set_backtrace_debug_on_exit (specpdl + count, false);
2820 tem = call_debugger (list2 (Qexit, tem));
2822 SAFE_FREE ();
2823 return tem;
2826 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2827 and return the result of evaluation.
2828 FUN must be either a lambda-expression or a compiled-code object. */
2830 static Lisp_Object
2831 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2832 register Lisp_Object *arg_vector)
2834 Lisp_Object val, syms_left, next, lexenv;
2835 ptrdiff_t count = SPECPDL_INDEX ();
2836 ptrdiff_t i;
2837 bool optional, rest;
2839 if (CONSP (fun))
2841 if (EQ (XCAR (fun), Qclosure))
2843 fun = XCDR (fun); /* Drop `closure'. */
2844 lexenv = XCAR (fun);
2845 CHECK_LIST_CONS (fun, fun);
2847 else
2848 lexenv = Qnil;
2849 syms_left = XCDR (fun);
2850 if (CONSP (syms_left))
2851 syms_left = XCAR (syms_left);
2852 else
2853 xsignal1 (Qinvalid_function, fun);
2855 else if (COMPILEDP (fun))
2857 syms_left = AREF (fun, COMPILED_ARGLIST);
2858 if (INTEGERP (syms_left))
2859 /* A byte-code object with a non-nil `push args' slot means we
2860 shouldn't bind any arguments, instead just call the byte-code
2861 interpreter directly; it will push arguments as necessary.
2863 Byte-code objects with either a non-existent, or a nil value for
2864 the `push args' slot (the default), have dynamically-bound
2865 arguments, and use the argument-binding code below instead (as do
2866 all interpreted functions, even lexically bound ones). */
2868 /* If we have not actually read the bytecode string
2869 and constants vector yet, fetch them from the file. */
2870 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2871 Ffetch_bytecode (fun);
2872 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2873 AREF (fun, COMPILED_CONSTANTS),
2874 AREF (fun, COMPILED_STACK_DEPTH),
2875 syms_left,
2876 nargs, arg_vector);
2878 lexenv = Qnil;
2880 else
2881 emacs_abort ();
2883 i = optional = rest = 0;
2884 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2886 QUIT;
2888 next = XCAR (syms_left);
2889 if (!SYMBOLP (next))
2890 xsignal1 (Qinvalid_function, fun);
2892 if (EQ (next, Qand_rest))
2893 rest = 1;
2894 else if (EQ (next, Qand_optional))
2895 optional = 1;
2896 else
2898 Lisp_Object arg;
2899 if (rest)
2901 arg = Flist (nargs - i, &arg_vector[i]);
2902 i = nargs;
2904 else if (i < nargs)
2905 arg = arg_vector[i++];
2906 else if (!optional)
2907 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2908 else
2909 arg = Qnil;
2911 /* Bind the argument. */
2912 if (!NILP (lexenv) && SYMBOLP (next))
2913 /* Lexically bind NEXT by adding it to the lexenv alist. */
2914 lexenv = Fcons (Fcons (next, arg), lexenv);
2915 else
2916 /* Dynamically bind NEXT. */
2917 specbind (next, arg);
2921 if (!NILP (syms_left))
2922 xsignal1 (Qinvalid_function, fun);
2923 else if (i < nargs)
2924 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2926 if (!EQ (lexenv, Vinternal_interpreter_environment))
2927 /* Instantiate a new lexical environment. */
2928 specbind (Qinternal_interpreter_environment, lexenv);
2930 if (CONSP (fun))
2931 val = Fprogn (XCDR (XCDR (fun)));
2932 else
2934 /* If we have not actually read the bytecode string
2935 and constants vector yet, fetch them from the file. */
2936 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2937 Ffetch_bytecode (fun);
2938 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2939 AREF (fun, COMPILED_CONSTANTS),
2940 AREF (fun, COMPILED_STACK_DEPTH),
2941 Qnil, 0, 0);
2944 return unbind_to (count, val);
2947 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2948 1, 1, 0,
2949 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2950 (Lisp_Object object)
2952 Lisp_Object tem;
2954 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2956 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2957 if (!CONSP (tem))
2959 tem = AREF (object, COMPILED_BYTECODE);
2960 if (CONSP (tem) && STRINGP (XCAR (tem)))
2961 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2962 else
2963 error ("Invalid byte code");
2965 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2966 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2968 return object;
2971 /* Return true if SYMBOL currently has a let-binding
2972 which was made in the buffer that is now current. */
2974 bool
2975 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2977 union specbinding *p;
2978 Lisp_Object buf = Fcurrent_buffer ();
2980 for (p = specpdl_ptr; p > specpdl; )
2981 if ((--p)->kind > SPECPDL_LET)
2983 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2984 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2985 if (symbol == let_bound_symbol
2986 && EQ (specpdl_where (p), buf))
2987 return 1;
2990 return 0;
2993 bool
2994 let_shadows_global_binding_p (Lisp_Object symbol)
2996 union specbinding *p;
2998 for (p = specpdl_ptr; p > specpdl; )
2999 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3000 return 1;
3002 return 0;
3005 /* `specpdl_ptr' describes which variable is
3006 let-bound, so it can be properly undone when we unbind_to.
3007 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3008 - SYMBOL is the variable being bound. Note that it should not be
3009 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3010 to record V2 here).
3011 - WHERE tells us in which buffer the binding took place.
3012 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3013 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3014 i.e. bindings to the default value of a variable which can be
3015 buffer-local. */
3017 void
3018 specbind (Lisp_Object symbol, Lisp_Object value)
3020 struct Lisp_Symbol *sym;
3022 CHECK_SYMBOL (symbol);
3023 sym = XSYMBOL (symbol);
3025 start:
3026 switch (sym->redirect)
3028 case SYMBOL_VARALIAS:
3029 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3030 case SYMBOL_PLAINVAL:
3031 /* The most common case is that of a non-constant symbol with a
3032 trivial value. Make that as fast as we can. */
3033 specpdl_ptr->let.kind = SPECPDL_LET;
3034 specpdl_ptr->let.symbol = symbol;
3035 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3036 grow_specpdl ();
3037 if (!sym->constant)
3038 SET_SYMBOL_VAL (sym, value);
3039 else
3040 set_internal (symbol, value, Qnil, 1);
3041 break;
3042 case SYMBOL_LOCALIZED:
3043 if (SYMBOL_BLV (sym)->frame_local)
3044 error ("Frame-local vars cannot be let-bound");
3045 case SYMBOL_FORWARDED:
3047 Lisp_Object ovalue = find_symbol_value (symbol);
3048 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3049 specpdl_ptr->let.symbol = symbol;
3050 specpdl_ptr->let.old_value = ovalue;
3051 specpdl_ptr->let.where = Fcurrent_buffer ();
3053 eassert (sym->redirect != SYMBOL_LOCALIZED
3054 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3056 if (sym->redirect == SYMBOL_LOCALIZED)
3058 if (!blv_found (SYMBOL_BLV (sym)))
3059 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3061 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3063 /* If SYMBOL is a per-buffer variable which doesn't have a
3064 buffer-local value here, make the `let' change the global
3065 value by changing the value of SYMBOL in all buffers not
3066 having their own value. This is consistent with what
3067 happens with other buffer-local variables. */
3068 if (NILP (Flocal_variable_p (symbol, Qnil)))
3070 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3071 grow_specpdl ();
3072 Fset_default (symbol, value);
3073 return;
3076 else
3077 specpdl_ptr->let.kind = SPECPDL_LET;
3079 grow_specpdl ();
3080 set_internal (symbol, value, Qnil, 1);
3081 break;
3083 default: emacs_abort ();
3087 /* Push unwind-protect entries of various types. */
3089 void
3090 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3092 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3093 specpdl_ptr->unwind.func = function;
3094 specpdl_ptr->unwind.arg = arg;
3095 grow_specpdl ();
3098 void
3099 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3101 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3102 specpdl_ptr->unwind_ptr.func = function;
3103 specpdl_ptr->unwind_ptr.arg = arg;
3104 grow_specpdl ();
3107 void
3108 record_unwind_protect_int (void (*function) (int), int arg)
3110 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3111 specpdl_ptr->unwind_int.func = function;
3112 specpdl_ptr->unwind_int.arg = arg;
3113 grow_specpdl ();
3116 void
3117 record_unwind_protect_void (void (*function) (void))
3119 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3120 specpdl_ptr->unwind_void.func = function;
3121 grow_specpdl ();
3124 static void
3125 do_nothing (void)
3128 /* Push an unwind-protect entry that does nothing, so that
3129 set_unwind_protect_ptr can overwrite it later. */
3131 void
3132 record_unwind_protect_nothing (void)
3134 record_unwind_protect_void (do_nothing);
3137 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3138 It need not be at the top of the stack. */
3140 void
3141 clear_unwind_protect (ptrdiff_t count)
3143 union specbinding *p = specpdl + count;
3144 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3145 p->unwind_void.func = do_nothing;
3148 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3149 It need not be at the top of the stack. Discard the entry's
3150 previous value without invoking it. */
3152 void
3153 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3154 Lisp_Object arg)
3156 union specbinding *p = specpdl + count;
3157 p->unwind.kind = SPECPDL_UNWIND;
3158 p->unwind.func = func;
3159 p->unwind.arg = arg;
3162 void
3163 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3165 union specbinding *p = specpdl + count;
3166 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3167 p->unwind_ptr.func = func;
3168 p->unwind_ptr.arg = arg;
3171 /* Pop and execute entries from the unwind-protect stack until the
3172 depth COUNT is reached. Return VALUE. */
3174 Lisp_Object
3175 unbind_to (ptrdiff_t count, Lisp_Object value)
3177 Lisp_Object quitf = Vquit_flag;
3178 struct gcpro gcpro1, gcpro2;
3180 GCPRO2 (value, quitf);
3181 Vquit_flag = Qnil;
3183 while (specpdl_ptr != specpdl + count)
3185 /* Decrement specpdl_ptr before we do the work to unbind it, so
3186 that an error in unbinding won't try to unbind the same entry
3187 again. Take care to copy any parts of the binding needed
3188 before invoking any code that can make more bindings. */
3190 specpdl_ptr--;
3192 switch (specpdl_ptr->kind)
3194 case SPECPDL_UNWIND:
3195 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3196 break;
3197 case SPECPDL_UNWIND_PTR:
3198 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3199 break;
3200 case SPECPDL_UNWIND_INT:
3201 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3202 break;
3203 case SPECPDL_UNWIND_VOID:
3204 specpdl_ptr->unwind_void.func ();
3205 break;
3206 case SPECPDL_BACKTRACE:
3207 break;
3208 case SPECPDL_LET:
3209 { /* If variable has a trivial value (no forwarding), we can
3210 just set it. No need to check for constant symbols here,
3211 since that was already done by specbind. */
3212 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3213 if (sym->redirect == SYMBOL_PLAINVAL)
3215 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3216 break;
3218 else
3219 { /* FALLTHROUGH!!
3220 NOTE: we only ever come here if make_local_foo was used for
3221 the first time on this var within this let. */
3224 case SPECPDL_LET_DEFAULT:
3225 Fset_default (specpdl_symbol (specpdl_ptr),
3226 specpdl_old_value (specpdl_ptr));
3227 break;
3228 case SPECPDL_LET_LOCAL:
3230 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3231 Lisp_Object where = specpdl_where (specpdl_ptr);
3232 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3233 eassert (BUFFERP (where));
3235 /* If this was a local binding, reset the value in the appropriate
3236 buffer, but only if that buffer's binding still exists. */
3237 if (!NILP (Flocal_variable_p (symbol, where)))
3238 set_internal (symbol, old_value, where, 1);
3240 break;
3244 if (NILP (Vquit_flag) && !NILP (quitf))
3245 Vquit_flag = quitf;
3247 UNGCPRO;
3248 return value;
3251 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3252 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3253 A special variable is one that will be bound dynamically, even in a
3254 context where binding is lexical by default. */)
3255 (Lisp_Object symbol)
3257 CHECK_SYMBOL (symbol);
3258 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3262 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3263 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3264 The debugger is entered when that frame exits, if the flag is non-nil. */)
3265 (Lisp_Object level, Lisp_Object flag)
3267 union specbinding *pdl = backtrace_top ();
3268 register EMACS_INT i;
3270 CHECK_NUMBER (level);
3272 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3273 pdl = backtrace_next (pdl);
3275 if (backtrace_p (pdl))
3276 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3278 return flag;
3281 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3282 doc: /* Print a trace of Lisp function calls currently active.
3283 Output stream used is value of `standard-output'. */)
3284 (void)
3286 union specbinding *pdl = backtrace_top ();
3287 Lisp_Object tem;
3288 Lisp_Object old_print_level = Vprint_level;
3290 if (NILP (Vprint_level))
3291 XSETFASTINT (Vprint_level, 8);
3293 while (backtrace_p (pdl))
3295 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3296 if (backtrace_nargs (pdl) == UNEVALLED)
3298 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3299 Qnil);
3300 write_string ("\n", -1);
3302 else
3304 tem = backtrace_function (pdl);
3305 Fprin1 (tem, Qnil); /* This can QUIT. */
3306 write_string ("(", -1);
3308 ptrdiff_t i;
3309 for (i = 0; i < backtrace_nargs (pdl); i++)
3311 if (i) write_string (" ", -1);
3312 Fprin1 (backtrace_args (pdl)[i], Qnil);
3315 write_string (")\n", -1);
3317 pdl = backtrace_next (pdl);
3320 Vprint_level = old_print_level;
3321 return Qnil;
3324 static union specbinding *
3325 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3327 union specbinding *pdl = backtrace_top ();
3328 register EMACS_INT i;
3330 CHECK_NATNUM (nframes);
3332 if (!NILP (base))
3333 { /* Skip up to `base'. */
3334 base = Findirect_function (base, Qt);
3335 while (backtrace_p (pdl)
3336 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3337 pdl = backtrace_next (pdl);
3340 /* Find the frame requested. */
3341 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3342 pdl = backtrace_next (pdl);
3344 return pdl;
3347 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3348 doc: /* Return the function and arguments NFRAMES up from current execution point.
3349 If that frame has not evaluated the arguments yet (or is a special form),
3350 the value is (nil FUNCTION ARG-FORMS...).
3351 If that frame has evaluated its arguments and called its function already,
3352 the value is (t FUNCTION ARG-VALUES...).
3353 A &rest arg is represented as the tail of the list ARG-VALUES.
3354 FUNCTION is whatever was supplied as car of evaluated list,
3355 or a lambda expression for macro calls.
3356 If NFRAMES is more than the number of frames, the value is nil.
3357 If BASE is non-nil, it should be a function and NFRAMES counts from its
3358 nearest activation frame. */)
3359 (Lisp_Object nframes, Lisp_Object base)
3361 union specbinding *pdl = get_backtrace_frame (nframes, base);
3363 if (!backtrace_p (pdl))
3364 return Qnil;
3365 if (backtrace_nargs (pdl) == UNEVALLED)
3366 return Fcons (Qnil,
3367 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3368 else
3370 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3372 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3376 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3377 the specpdl stack, and then rewind them. We store the pre-unwind values
3378 directly in the pre-existing specpdl elements (i.e. we swap the current
3379 value and the old value stored in the specpdl), kind of like the inplace
3380 pointer-reversal trick. As it turns out, the rewind does the same as the
3381 unwind, except it starts from the other end of the specpdl stack, so we use
3382 the same function for both unwind and rewind. */
3383 static void
3384 backtrace_eval_unrewind (int distance)
3386 union specbinding *tmp = specpdl_ptr;
3387 int step = -1;
3388 if (distance < 0)
3389 { /* It's a rewind rather than unwind. */
3390 tmp += distance - 1;
3391 step = 1;
3392 distance = -distance;
3395 for (; distance > 0; distance--)
3397 tmp += step;
3398 switch (tmp->kind)
3400 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3401 unwind_protect, but the problem is that we don't know how to
3402 rewind them afterwards. */
3403 case SPECPDL_UNWIND:
3405 Lisp_Object oldarg = tmp->unwind.arg;
3406 if (tmp->unwind.func == set_buffer_if_live)
3407 tmp->unwind.arg = Fcurrent_buffer ();
3408 else if (tmp->unwind.func == save_excursion_restore)
3409 tmp->unwind.arg = save_excursion_save ();
3410 else
3411 break;
3412 tmp->unwind.func (oldarg);
3413 break;
3416 case SPECPDL_UNWIND_PTR:
3417 case SPECPDL_UNWIND_INT:
3418 case SPECPDL_UNWIND_VOID:
3419 case SPECPDL_BACKTRACE:
3420 break;
3421 case SPECPDL_LET:
3422 { /* If variable has a trivial value (no forwarding), we can
3423 just set it. No need to check for constant symbols here,
3424 since that was already done by specbind. */
3425 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3426 if (sym->redirect == SYMBOL_PLAINVAL)
3428 Lisp_Object old_value = specpdl_old_value (tmp);
3429 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3430 SET_SYMBOL_VAL (sym, old_value);
3431 break;
3433 else
3434 { /* FALLTHROUGH!!
3435 NOTE: we only ever come here if make_local_foo was used for
3436 the first time on this var within this let. */
3439 case SPECPDL_LET_DEFAULT:
3441 Lisp_Object sym = specpdl_symbol (tmp);
3442 Lisp_Object old_value = specpdl_old_value (tmp);
3443 set_specpdl_old_value (tmp, Fdefault_value (sym));
3444 Fset_default (sym, old_value);
3446 break;
3447 case SPECPDL_LET_LOCAL:
3449 Lisp_Object symbol = specpdl_symbol (tmp);
3450 Lisp_Object where = specpdl_where (tmp);
3451 Lisp_Object old_value = specpdl_old_value (tmp);
3452 eassert (BUFFERP (where));
3454 /* If this was a local binding, reset the value in the appropriate
3455 buffer, but only if that buffer's binding still exists. */
3456 if (!NILP (Flocal_variable_p (symbol, where)))
3458 set_specpdl_old_value
3459 (tmp, Fbuffer_local_value (symbol, where));
3460 set_internal (symbol, old_value, where, 1);
3463 break;
3468 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3469 doc: /* Evaluate EXP in the context of some activation frame.
3470 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3471 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3473 union specbinding *pdl = get_backtrace_frame (nframes, base);
3474 ptrdiff_t count = SPECPDL_INDEX ();
3475 ptrdiff_t distance = specpdl_ptr - pdl;
3476 eassert (distance >= 0);
3478 if (!backtrace_p (pdl))
3479 error ("Activation frame not found!");
3481 backtrace_eval_unrewind (distance);
3482 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3484 /* Use eval_sub rather than Feval since the main motivation behind
3485 backtrace-eval is to be able to get/set the value of lexical variables
3486 from the debugger. */
3487 return unbind_to (count, eval_sub (exp));
3490 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3491 doc: /* Return names and values of local variables of a stack frame.
3492 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3493 (Lisp_Object nframes, Lisp_Object base)
3495 union specbinding *frame = get_backtrace_frame (nframes, base);
3496 union specbinding *prevframe
3497 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3498 ptrdiff_t distance = specpdl_ptr - frame;
3499 Lisp_Object result = Qnil;
3500 eassert (distance >= 0);
3502 if (!backtrace_p (prevframe))
3503 error ("Activation frame not found!");
3504 if (!backtrace_p (frame))
3505 error ("Activation frame not found!");
3507 /* The specpdl entries normally contain the symbol being bound along with its
3508 `old_value', so it can be restored. The new value to which it is bound is
3509 available in one of two places: either in the current value of the
3510 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3511 next specpdl entry for it.
3512 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3513 and "new value", so we abuse it here, to fetch the new value.
3514 It's ugly (we'd rather not modify global data) and a bit inefficient,
3515 but it does the job for now. */
3516 backtrace_eval_unrewind (distance);
3518 /* Grab values. */
3520 union specbinding *tmp = prevframe;
3521 for (; tmp > frame; tmp--)
3523 switch (tmp->kind)
3525 case SPECPDL_LET:
3526 case SPECPDL_LET_DEFAULT:
3527 case SPECPDL_LET_LOCAL:
3529 Lisp_Object sym = specpdl_symbol (tmp);
3530 Lisp_Object val = specpdl_old_value (tmp);
3531 if (EQ (sym, Qinternal_interpreter_environment))
3533 Lisp_Object env = val;
3534 for (; CONSP (env); env = XCDR (env))
3536 Lisp_Object binding = XCAR (env);
3537 if (CONSP (binding))
3538 result = Fcons (Fcons (XCAR (binding),
3539 XCDR (binding)),
3540 result);
3543 else
3544 result = Fcons (Fcons (sym, val), result);
3550 /* Restore values from specpdl to original place. */
3551 backtrace_eval_unrewind (-distance);
3553 return result;
3557 void
3558 mark_specpdl (void)
3560 union specbinding *pdl;
3561 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3563 switch (pdl->kind)
3565 case SPECPDL_UNWIND:
3566 mark_object (specpdl_arg (pdl));
3567 break;
3569 case SPECPDL_BACKTRACE:
3571 ptrdiff_t nargs = backtrace_nargs (pdl);
3572 mark_object (backtrace_function (pdl));
3573 if (nargs == UNEVALLED)
3574 nargs = 1;
3575 while (nargs--)
3576 mark_object (backtrace_args (pdl)[nargs]);
3578 break;
3580 case SPECPDL_LET_DEFAULT:
3581 case SPECPDL_LET_LOCAL:
3582 mark_object (specpdl_where (pdl));
3583 /* Fall through. */
3584 case SPECPDL_LET:
3585 mark_object (specpdl_symbol (pdl));
3586 mark_object (specpdl_old_value (pdl));
3587 break;
3592 void
3593 get_backtrace (Lisp_Object array)
3595 union specbinding *pdl = backtrace_next (backtrace_top ());
3596 ptrdiff_t i = 0, asize = ASIZE (array);
3598 /* Copy the backtrace contents into working memory. */
3599 for (; i < asize; i++)
3601 if (backtrace_p (pdl))
3603 ASET (array, i, backtrace_function (pdl));
3604 pdl = backtrace_next (pdl);
3606 else
3607 ASET (array, i, Qnil);
3611 Lisp_Object backtrace_top_function (void)
3613 union specbinding *pdl = backtrace_top ();
3614 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3617 void
3618 syms_of_eval (void)
3620 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3621 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3622 If Lisp code tries to increase the total number past this amount,
3623 an error is signaled.
3624 You can safely use a value considerably larger than the default value,
3625 if that proves inconveniently small. However, if you increase it too far,
3626 Emacs could run out of memory trying to make the stack bigger.
3627 Note that this limit may be silently increased by the debugger
3628 if `debug-on-error' or `debug-on-quit' is set. */);
3630 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3631 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3633 This limit serves to catch infinite recursions for you before they cause
3634 actual stack overflow in C, which would be fatal for Emacs.
3635 You can safely make it considerably larger than its default value,
3636 if that proves inconveniently small. However, if you increase it too far,
3637 Emacs could overflow the real C stack, and crash. */);
3639 DEFVAR_LISP ("quit-flag", Vquit_flag,
3640 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3641 If the value is t, that means do an ordinary quit.
3642 If the value equals `throw-on-input', that means quit by throwing
3643 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3644 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3645 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3646 Vquit_flag = Qnil;
3648 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3649 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3650 Note that `quit-flag' will still be set by typing C-g,
3651 so a quit will be signaled as soon as `inhibit-quit' is nil.
3652 To prevent this happening, set `quit-flag' to nil
3653 before making `inhibit-quit' nil. */);
3654 Vinhibit_quit = Qnil;
3656 DEFSYM (Qinhibit_quit, "inhibit-quit");
3657 DEFSYM (Qautoload, "autoload");
3658 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3659 DEFSYM (Qmacro, "macro");
3660 DEFSYM (Qdeclare, "declare");
3662 /* Note that the process handling also uses Qexit, but we don't want
3663 to staticpro it twice, so we just do it here. */
3664 DEFSYM (Qexit, "exit");
3666 DEFSYM (Qinteractive, "interactive");
3667 DEFSYM (Qcommandp, "commandp");
3668 DEFSYM (Qand_rest, "&rest");
3669 DEFSYM (Qand_optional, "&optional");
3670 DEFSYM (Qclosure, "closure");
3671 DEFSYM (Qdebug, "debug");
3673 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3674 doc: /* Non-nil means never enter the debugger.
3675 Normally set while the debugger is already active, to avoid recursive
3676 invocations. */);
3677 Vinhibit_debugger = Qnil;
3679 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3680 doc: /* Non-nil means enter debugger if an error is signaled.
3681 Does not apply to errors handled by `condition-case' or those
3682 matched by `debug-ignored-errors'.
3683 If the value is a list, an error only means to enter the debugger
3684 if one of its condition symbols appears in the list.
3685 When you evaluate an expression interactively, this variable
3686 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3687 The command `toggle-debug-on-error' toggles this.
3688 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3689 Vdebug_on_error = Qnil;
3691 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3692 doc: /* List of errors for which the debugger should not be called.
3693 Each element may be a condition-name or a regexp that matches error messages.
3694 If any element applies to a given error, that error skips the debugger
3695 and just returns to top level.
3696 This overrides the variable `debug-on-error'.
3697 It does not apply to errors handled by `condition-case'. */);
3698 Vdebug_ignored_errors = Qnil;
3700 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3701 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3702 Does not apply if quit is handled by a `condition-case'. */);
3703 debug_on_quit = 0;
3705 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3706 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3708 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3709 doc: /* Non-nil means debugger may continue execution.
3710 This is nil when the debugger is called under circumstances where it
3711 might not be safe to continue. */);
3712 debugger_may_continue = 1;
3714 DEFVAR_LISP ("debugger", Vdebugger,
3715 doc: /* Function to call to invoke debugger.
3716 If due to frame exit, args are `exit' and the value being returned;
3717 this function's value will be returned instead of that.
3718 If due to error, args are `error' and a list of the args to `signal'.
3719 If due to `apply' or `funcall' entry, one arg, `lambda'.
3720 If due to `eval' entry, one arg, t. */);
3721 Vdebugger = Qnil;
3723 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3724 doc: /* If non-nil, this is a function for `signal' to call.
3725 It receives the same arguments that `signal' was given.
3726 The Edebug package uses this to regain control. */);
3727 Vsignal_hook_function = Qnil;
3729 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3730 doc: /* Non-nil means call the debugger regardless of condition handlers.
3731 Note that `debug-on-error', `debug-on-quit' and friends
3732 still determine whether to handle the particular condition. */);
3733 Vdebug_on_signal = Qnil;
3735 /* When lexical binding is being used,
3736 Vinternal_interpreter_environment is non-nil, and contains an alist
3737 of lexically-bound variable, or (t), indicating an empty
3738 environment. The lisp name of this variable would be
3739 `internal-interpreter-environment' if it weren't hidden.
3740 Every element of this list can be either a cons (VAR . VAL)
3741 specifying a lexical binding, or a single symbol VAR indicating
3742 that this variable should use dynamic scoping. */
3743 DEFSYM (Qinternal_interpreter_environment,
3744 "internal-interpreter-environment");
3745 DEFVAR_LISP ("internal-interpreter-environment",
3746 Vinternal_interpreter_environment,
3747 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3748 When lexical binding is not being used, this variable is nil.
3749 A value of `(t)' indicates an empty environment, otherwise it is an
3750 alist of active lexical bindings. */);
3751 Vinternal_interpreter_environment = Qnil;
3752 /* Don't export this variable to Elisp, so no one can mess with it
3753 (Just imagine if someone makes it buffer-local). */
3754 Funintern (Qinternal_interpreter_environment, Qnil);
3756 Vrun_hooks = intern_c_string ("run-hooks");
3757 staticpro (&Vrun_hooks);
3759 staticpro (&Vautoload_queue);
3760 Vautoload_queue = Qnil;
3761 staticpro (&Vsignaling_function);
3762 Vsignaling_function = Qnil;
3764 inhibit_lisp_code = Qnil;
3766 defsubr (&Sor);
3767 defsubr (&Sand);
3768 defsubr (&Sif);
3769 defsubr (&Scond);
3770 defsubr (&Sprogn);
3771 defsubr (&Sprog1);
3772 defsubr (&Sprog2);
3773 defsubr (&Ssetq);
3774 defsubr (&Squote);
3775 defsubr (&Sfunction);
3776 defsubr (&Sdefault_toplevel_value);
3777 defsubr (&Sset_default_toplevel_value);
3778 defsubr (&Sdefvar);
3779 defsubr (&Sdefvaralias);
3780 defsubr (&Sdefconst);
3781 defsubr (&Smake_var_non_special);
3782 defsubr (&Slet);
3783 defsubr (&SletX);
3784 defsubr (&Swhile);
3785 defsubr (&Smacroexpand);
3786 defsubr (&Scatch);
3787 defsubr (&Sthrow);
3788 defsubr (&Sunwind_protect);
3789 defsubr (&Scondition_case);
3790 defsubr (&Ssignal);
3791 defsubr (&Scommandp);
3792 defsubr (&Sautoload);
3793 defsubr (&Sautoload_do_load);
3794 defsubr (&Seval);
3795 defsubr (&Sapply);
3796 defsubr (&Sfuncall);
3797 defsubr (&Srun_hooks);
3798 defsubr (&Srun_hook_with_args);
3799 defsubr (&Srun_hook_with_args_until_success);
3800 defsubr (&Srun_hook_with_args_until_failure);
3801 defsubr (&Srun_hook_wrapped);
3802 defsubr (&Sfetch_bytecode);
3803 defsubr (&Sbacktrace_debug);
3804 defsubr (&Sbacktrace);
3805 defsubr (&Sbacktrace_frame);
3806 defsubr (&Sbacktrace_eval);
3807 defsubr (&Sbacktrace__locals);
3808 defsubr (&Sspecial_variable_p);
3809 defsubr (&Sfunctionp);