(eww): Don't interpret "org/foo" as an URL.
[emacs.git] / src / eval.c
blob7e4b016b23640c6f0a9d0639f33037e131c91ff1
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 (register Lisp_Object tag, Lisp_Object value)
1168 struct handler *c;
1170 if (!NILP (tag))
1171 for (c = handlerlist; c; c = c->next)
1173 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1174 unwind_to_catch (c, value);
1176 xsignal2 (Qno_catch, tag, value);
1180 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1181 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1182 If BODYFORM completes normally, its value is returned
1183 after executing the UNWINDFORMS.
1184 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1185 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1186 (Lisp_Object args)
1188 Lisp_Object val;
1189 ptrdiff_t count = SPECPDL_INDEX ();
1191 record_unwind_protect (unwind_body, XCDR (args));
1192 val = eval_sub (XCAR (args));
1193 return unbind_to (count, val);
1196 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1197 doc: /* Regain control when an error is signaled.
1198 Executes BODYFORM and returns its value if no error happens.
1199 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1200 where the BODY is made of Lisp expressions.
1202 A handler is applicable to an error
1203 if CONDITION-NAME is one of the error's condition names.
1204 If an error happens, the first applicable handler is run.
1206 The car of a handler may be a list of condition names instead of a
1207 single condition name; then it handles all of them. If the special
1208 condition name `debug' is present in this list, it allows another
1209 condition in the list to run the debugger if `debug-on-error' and the
1210 other usual mechanisms says it should (otherwise, `condition-case'
1211 suppresses the debugger).
1213 When a handler handles an error, control returns to the `condition-case'
1214 and it executes the handler's BODY...
1215 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1216 \(If VAR is nil, the handler can't access that information.)
1217 Then the value of the last BODY form is returned from the `condition-case'
1218 expression.
1220 See also the function `signal' for more info.
1221 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1222 (Lisp_Object args)
1224 Lisp_Object var = XCAR (args);
1225 Lisp_Object bodyform = XCAR (XCDR (args));
1226 Lisp_Object handlers = XCDR (XCDR (args));
1228 return internal_lisp_condition_case (var, bodyform, handlers);
1231 /* Like Fcondition_case, but the args are separate
1232 rather than passed in a list. Used by Fbyte_code. */
1234 Lisp_Object
1235 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1236 Lisp_Object handlers)
1238 Lisp_Object val;
1239 struct handler *c;
1240 struct handler *oldhandlerlist = handlerlist;
1241 int clausenb = 0;
1243 CHECK_SYMBOL (var);
1245 for (val = handlers; CONSP (val); val = XCDR (val))
1247 Lisp_Object tem = XCAR (val);
1248 clausenb++;
1249 if (! (NILP (tem)
1250 || (CONSP (tem)
1251 && (SYMBOLP (XCAR (tem))
1252 || CONSP (XCAR (tem))))))
1253 error ("Invalid condition handler: %s",
1254 SDATA (Fprin1_to_string (tem, Qt)));
1257 { /* The first clause is the one that should be checked first, so it should
1258 be added to handlerlist last. So we build in `clauses' a table that
1259 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1260 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1261 if (MAX_ALLOCA / word_size < clausenb)
1262 memory_full (SIZE_MAX);
1263 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1264 Lisp_Object *volatile clauses_volatile = clauses;
1265 int i = clausenb;
1266 for (val = handlers; CONSP (val); val = XCDR (val))
1267 clauses[--i] = XCAR (val);
1268 for (i = 0; i < clausenb; i++)
1270 Lisp_Object clause = clauses[i];
1271 Lisp_Object condition = XCAR (clause);
1272 if (!CONSP (condition))
1273 condition = Fcons (condition, Qnil);
1274 PUSH_HANDLER (c, condition, CONDITION_CASE);
1275 if (sys_setjmp (c->jmp))
1277 ptrdiff_t count = SPECPDL_INDEX ();
1278 Lisp_Object val = handlerlist->val;
1279 Lisp_Object *chosen_clause = clauses_volatile;
1280 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1281 chosen_clause++;
1282 handlerlist = oldhandlerlist;
1283 if (!NILP (var))
1285 if (!NILP (Vinternal_interpreter_environment))
1286 specbind (Qinternal_interpreter_environment,
1287 Fcons (Fcons (var, val),
1288 Vinternal_interpreter_environment));
1289 else
1290 specbind (var, val);
1292 val = Fprogn (XCDR (*chosen_clause));
1293 /* Note that this just undoes the binding of var; whoever
1294 longjumped to us unwound the stack to c.pdlcount before
1295 throwing. */
1296 if (!NILP (var))
1297 unbind_to (count, Qnil);
1298 return val;
1303 val = eval_sub (bodyform);
1304 handlerlist = oldhandlerlist;
1305 return val;
1308 /* Call the function BFUN with no arguments, catching errors within it
1309 according to HANDLERS. If there is an error, call HFUN with
1310 one argument which is the data that describes the error:
1311 (SIGNALNAME . DATA)
1313 HANDLERS can be a list of conditions to catch.
1314 If HANDLERS is Qt, catch all errors.
1315 If HANDLERS is Qerror, catch all errors
1316 but allow the debugger to run if that is enabled. */
1318 Lisp_Object
1319 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1320 Lisp_Object (*hfun) (Lisp_Object))
1322 Lisp_Object val;
1323 struct handler *c;
1325 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1326 if (sys_setjmp (c->jmp))
1328 Lisp_Object val = handlerlist->val;
1329 clobbered_eassert (handlerlist == c);
1330 handlerlist = handlerlist->next;
1331 return (*hfun) (val);
1334 val = (*bfun) ();
1335 clobbered_eassert (handlerlist == c);
1336 handlerlist = handlerlist->next;
1337 return val;
1340 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1342 Lisp_Object
1343 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1344 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1346 Lisp_Object val;
1347 struct handler *c;
1349 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1350 if (sys_setjmp (c->jmp))
1352 Lisp_Object val = handlerlist->val;
1353 clobbered_eassert (handlerlist == c);
1354 handlerlist = handlerlist->next;
1355 return (*hfun) (val);
1358 val = (*bfun) (arg);
1359 clobbered_eassert (handlerlist == c);
1360 handlerlist = handlerlist->next;
1361 return val;
1364 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1365 its arguments. */
1367 Lisp_Object
1368 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1369 Lisp_Object arg1,
1370 Lisp_Object arg2,
1371 Lisp_Object handlers,
1372 Lisp_Object (*hfun) (Lisp_Object))
1374 Lisp_Object val;
1375 struct handler *c;
1377 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1378 if (sys_setjmp (c->jmp))
1380 Lisp_Object val = handlerlist->val;
1381 clobbered_eassert (handlerlist == c);
1382 handlerlist = handlerlist->next;
1383 return (*hfun) (val);
1386 val = (*bfun) (arg1, arg2);
1387 clobbered_eassert (handlerlist == c);
1388 handlerlist = handlerlist->next;
1389 return val;
1392 /* Like internal_condition_case but call BFUN with NARGS as first,
1393 and ARGS as second argument. */
1395 Lisp_Object
1396 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1397 ptrdiff_t nargs,
1398 Lisp_Object *args,
1399 Lisp_Object handlers,
1400 Lisp_Object (*hfun) (Lisp_Object err,
1401 ptrdiff_t nargs,
1402 Lisp_Object *args))
1404 Lisp_Object val;
1405 struct handler *c;
1407 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1408 if (sys_setjmp (c->jmp))
1410 Lisp_Object val = handlerlist->val;
1411 clobbered_eassert (handlerlist == c);
1412 handlerlist = handlerlist->next;
1413 return (*hfun) (val, nargs, args);
1416 val = (*bfun) (nargs, args);
1417 clobbered_eassert (handlerlist == c);
1418 handlerlist = handlerlist->next;
1419 return val;
1423 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1424 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1425 Lisp_Object data);
1427 void
1428 process_quit_flag (void)
1430 Lisp_Object flag = Vquit_flag;
1431 Vquit_flag = Qnil;
1432 if (EQ (flag, Qkill_emacs))
1433 Fkill_emacs (Qnil);
1434 if (EQ (Vthrow_on_input, flag))
1435 Fthrow (Vthrow_on_input, Qt);
1436 Fsignal (Qquit, Qnil);
1439 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1440 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1441 This function does not return.
1443 An error symbol is a symbol with an `error-conditions' property
1444 that is a list of condition names.
1445 A handler for any of those names will get to handle this signal.
1446 The symbol `error' should normally be one of them.
1448 DATA should be a list. Its elements are printed as part of the error message.
1449 See Info anchor `(elisp)Definition of signal' for some details on how this
1450 error message is constructed.
1451 If the signal is handled, DATA is made available to the handler.
1452 See also the function `condition-case'. */)
1453 (Lisp_Object error_symbol, Lisp_Object data)
1455 /* When memory is full, ERROR-SYMBOL is nil,
1456 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1457 That is a special case--don't do this in other situations. */
1458 Lisp_Object conditions;
1459 Lisp_Object string;
1460 Lisp_Object real_error_symbol
1461 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1462 register Lisp_Object clause = Qnil;
1463 struct handler *h;
1465 immediate_quit = 0;
1466 abort_on_gc = 0;
1467 if (gc_in_progress || waiting_for_input)
1468 emacs_abort ();
1470 #if 0 /* rms: I don't know why this was here,
1471 but it is surely wrong for an error that is handled. */
1472 #ifdef HAVE_WINDOW_SYSTEM
1473 if (display_hourglass_p)
1474 cancel_hourglass ();
1475 #endif
1476 #endif
1478 /* This hook is used by edebug. */
1479 if (! NILP (Vsignal_hook_function)
1480 && ! NILP (error_symbol))
1482 /* Edebug takes care of restoring these variables when it exits. */
1483 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1484 max_lisp_eval_depth = lisp_eval_depth + 20;
1486 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1487 max_specpdl_size = SPECPDL_INDEX () + 40;
1489 call2 (Vsignal_hook_function, error_symbol, data);
1492 conditions = Fget (real_error_symbol, Qerror_conditions);
1494 /* Remember from where signal was called. Skip over the frame for
1495 `signal' itself. If a frame for `error' follows, skip that,
1496 too. Don't do this when ERROR_SYMBOL is nil, because that
1497 is a memory-full error. */
1498 Vsignaling_function = Qnil;
1499 if (!NILP (error_symbol))
1501 union specbinding *pdl = backtrace_next (backtrace_top ());
1502 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1503 pdl = backtrace_next (pdl);
1504 if (backtrace_p (pdl))
1505 Vsignaling_function = backtrace_function (pdl);
1508 for (h = handlerlist; h; h = h->next)
1510 if (h->type != CONDITION_CASE)
1511 continue;
1512 clause = find_handler_clause (h->tag_or_ch, conditions);
1513 if (!NILP (clause))
1514 break;
1517 if (/* Don't run the debugger for a memory-full error.
1518 (There is no room in memory to do that!) */
1519 !NILP (error_symbol)
1520 && (!NILP (Vdebug_on_signal)
1521 /* If no handler is present now, try to run the debugger. */
1522 || NILP (clause)
1523 /* A `debug' symbol in the handler list disables the normal
1524 suppression of the debugger. */
1525 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1526 /* Special handler that means "print a message and run debugger
1527 if requested". */
1528 || EQ (h->tag_or_ch, Qerror)))
1530 bool debugger_called
1531 = maybe_call_debugger (conditions, error_symbol, data);
1532 /* We can't return values to code which signaled an error, but we
1533 can continue code which has signaled a quit. */
1534 if (debugger_called && EQ (real_error_symbol, Qquit))
1535 return Qnil;
1538 if (!NILP (clause))
1540 Lisp_Object unwind_data
1541 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1543 unwind_to_catch (h, unwind_data);
1545 else
1547 if (handlerlist != &handlerlist_sentinel)
1548 /* FIXME: This will come right back here if there's no `top-level'
1549 catcher. A better solution would be to abort here, and instead
1550 add a catch-all condition handler so we never come here. */
1551 Fthrow (Qtop_level, Qt);
1554 if (! NILP (error_symbol))
1555 data = Fcons (error_symbol, data);
1557 string = Ferror_message_string (data);
1558 fatal ("%s", SDATA (string));
1561 /* Internal version of Fsignal that never returns.
1562 Used for anything but Qquit (which can return from Fsignal). */
1564 void
1565 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1567 Fsignal (error_symbol, data);
1568 emacs_abort ();
1571 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1573 void
1574 xsignal0 (Lisp_Object error_symbol)
1576 xsignal (error_symbol, Qnil);
1579 void
1580 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1582 xsignal (error_symbol, list1 (arg));
1585 void
1586 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1588 xsignal (error_symbol, list2 (arg1, arg2));
1591 void
1592 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1594 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1597 /* Signal `error' with message S, and additional arg ARG.
1598 If ARG is not a genuine list, make it a one-element list. */
1600 void
1601 signal_error (const char *s, Lisp_Object arg)
1603 Lisp_Object tortoise, hare;
1605 hare = tortoise = arg;
1606 while (CONSP (hare))
1608 hare = XCDR (hare);
1609 if (!CONSP (hare))
1610 break;
1612 hare = XCDR (hare);
1613 tortoise = XCDR (tortoise);
1615 if (EQ (hare, tortoise))
1616 break;
1619 if (!NILP (hare))
1620 arg = list1 (arg);
1622 xsignal (Qerror, Fcons (build_string (s), arg));
1626 /* Return true if LIST is a non-nil atom or
1627 a list containing one of CONDITIONS. */
1629 static bool
1630 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1632 if (NILP (list))
1633 return 0;
1634 if (! CONSP (list))
1635 return 1;
1637 while (CONSP (conditions))
1639 Lisp_Object this, tail;
1640 this = XCAR (conditions);
1641 for (tail = list; CONSP (tail); tail = XCDR (tail))
1642 if (EQ (XCAR (tail), this))
1643 return 1;
1644 conditions = XCDR (conditions);
1646 return 0;
1649 /* Return true if an error with condition-symbols CONDITIONS,
1650 and described by SIGNAL-DATA, should skip the debugger
1651 according to debugger-ignored-errors. */
1653 static bool
1654 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1656 Lisp_Object tail;
1657 bool first_string = 1;
1658 Lisp_Object error_message;
1660 error_message = Qnil;
1661 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1663 if (STRINGP (XCAR (tail)))
1665 if (first_string)
1667 error_message = Ferror_message_string (data);
1668 first_string = 0;
1671 if (fast_string_match (XCAR (tail), error_message) >= 0)
1672 return 1;
1674 else
1676 Lisp_Object contail;
1678 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1679 if (EQ (XCAR (tail), XCAR (contail)))
1680 return 1;
1684 return 0;
1687 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1688 SIG and DATA describe the signal. There are two ways to pass them:
1689 = SIG is the error symbol, and DATA is the rest of the data.
1690 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1691 This is for memory-full errors only. */
1692 static bool
1693 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1695 Lisp_Object combined_data;
1697 combined_data = Fcons (sig, data);
1699 if (
1700 /* Don't try to run the debugger with interrupts blocked.
1701 The editing loop would return anyway. */
1702 ! input_blocked_p ()
1703 && NILP (Vinhibit_debugger)
1704 /* Does user want to enter debugger for this kind of error? */
1705 && (EQ (sig, Qquit)
1706 ? debug_on_quit
1707 : wants_debugger (Vdebug_on_error, conditions))
1708 && ! skip_debugger (conditions, combined_data)
1709 /* RMS: What's this for? */
1710 && when_entered_debugger < num_nonmacro_input_events)
1712 call_debugger (list2 (Qerror, combined_data));
1713 return 1;
1716 return 0;
1719 static Lisp_Object
1720 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1722 register Lisp_Object h;
1724 /* t is used by handlers for all conditions, set up by C code. */
1725 if (EQ (handlers, Qt))
1726 return Qt;
1728 /* error is used similarly, but means print an error message
1729 and run the debugger if that is enabled. */
1730 if (EQ (handlers, Qerror))
1731 return Qt;
1733 for (h = handlers; CONSP (h); h = XCDR (h))
1735 Lisp_Object handler = XCAR (h);
1736 if (!NILP (Fmemq (handler, conditions)))
1737 return handlers;
1740 return Qnil;
1744 /* Dump an error message; called like vprintf. */
1745 void
1746 verror (const char *m, va_list ap)
1748 char buf[4000];
1749 ptrdiff_t size = sizeof buf;
1750 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1751 char *buffer = buf;
1752 ptrdiff_t used;
1753 Lisp_Object string;
1755 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1756 string = make_string (buffer, used);
1757 if (buffer != buf)
1758 xfree (buffer);
1760 xsignal1 (Qerror, string);
1764 /* Dump an error message; called like printf. */
1766 /* VARARGS 1 */
1767 void
1768 error (const char *m, ...)
1770 va_list ap;
1771 va_start (ap, m);
1772 verror (m, ap);
1775 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1776 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1777 This means it contains a description for how to read arguments to give it.
1778 The value is nil for an invalid function or a symbol with no function
1779 definition.
1781 Interactively callable functions include strings and vectors (treated
1782 as keyboard macros), lambda-expressions that contain a top-level call
1783 to `interactive', autoload definitions made by `autoload' with non-nil
1784 fourth argument, and some of the built-in functions of Lisp.
1786 Also, a symbol satisfies `commandp' if its function definition does so.
1788 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1789 then strings and vectors are not accepted. */)
1790 (Lisp_Object function, Lisp_Object for_call_interactively)
1792 register Lisp_Object fun;
1793 register Lisp_Object funcar;
1794 Lisp_Object if_prop = Qnil;
1796 fun = function;
1798 fun = indirect_function (fun); /* Check cycles. */
1799 if (NILP (fun))
1800 return Qnil;
1802 /* Check an `interactive-form' property if present, analogous to the
1803 function-documentation property. */
1804 fun = function;
1805 while (SYMBOLP (fun))
1807 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1808 if (!NILP (tmp))
1809 if_prop = Qt;
1810 fun = Fsymbol_function (fun);
1813 /* Emacs primitives are interactive if their DEFUN specifies an
1814 interactive spec. */
1815 if (SUBRP (fun))
1816 return XSUBR (fun)->intspec ? Qt : if_prop;
1818 /* Bytecode objects are interactive if they are long enough to
1819 have an element whose index is COMPILED_INTERACTIVE, which is
1820 where the interactive spec is stored. */
1821 else if (COMPILEDP (fun))
1822 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1823 ? Qt : if_prop);
1825 /* Strings and vectors are keyboard macros. */
1826 if (STRINGP (fun) || VECTORP (fun))
1827 return (NILP (for_call_interactively) ? Qt : Qnil);
1829 /* Lists may represent commands. */
1830 if (!CONSP (fun))
1831 return Qnil;
1832 funcar = XCAR (fun);
1833 if (EQ (funcar, Qclosure))
1834 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1835 ? Qt : if_prop);
1836 else if (EQ (funcar, Qlambda))
1837 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1838 else if (EQ (funcar, Qautoload))
1839 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1840 else
1841 return Qnil;
1844 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1845 doc: /* Define FUNCTION to autoload from FILE.
1846 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1847 Third arg DOCSTRING is documentation for the function.
1848 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1849 Fifth arg TYPE indicates the type of the object:
1850 nil or omitted says FUNCTION is a function,
1851 `keymap' says FUNCTION is really a keymap, and
1852 `macro' or t says FUNCTION is really a macro.
1853 Third through fifth args give info about the real definition.
1854 They default to nil.
1855 If FUNCTION is already defined other than as an autoload,
1856 this does nothing and returns nil. */)
1857 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1859 CHECK_SYMBOL (function);
1860 CHECK_STRING (file);
1862 /* If function is defined and not as an autoload, don't override. */
1863 if (!NILP (XSYMBOL (function)->function)
1864 && !AUTOLOADP (XSYMBOL (function)->function))
1865 return Qnil;
1867 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1868 /* `read1' in lread.c has found the docstring starting with "\
1869 and assumed the docstring will be provided by Snarf-documentation, so it
1870 passed us 0 instead. But that leads to accidental sharing in purecopy's
1871 hash-consing, so we use a (hopefully) unique integer instead. */
1872 docstring = make_number (XHASH (function));
1873 return Fdefalias (function,
1874 list5 (Qautoload, file, docstring, interactive, type),
1875 Qnil);
1878 void
1879 un_autoload (Lisp_Object oldqueue)
1881 Lisp_Object queue, first, second;
1883 /* Queue to unwind is current value of Vautoload_queue.
1884 oldqueue is the shadowed value to leave in Vautoload_queue. */
1885 queue = Vautoload_queue;
1886 Vautoload_queue = oldqueue;
1887 while (CONSP (queue))
1889 first = XCAR (queue);
1890 second = Fcdr (first);
1891 first = Fcar (first);
1892 if (EQ (first, make_number (0)))
1893 Vfeatures = second;
1894 else
1895 Ffset (first, second);
1896 queue = XCDR (queue);
1900 /* Load an autoloaded function.
1901 FUNNAME is the symbol which is the function's name.
1902 FUNDEF is the autoload definition (a list). */
1904 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1905 doc: /* Load FUNDEF which should be an autoload.
1906 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1907 in which case the function returns the new autoloaded function value.
1908 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1909 it defines a macro. */)
1910 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1912 ptrdiff_t count = SPECPDL_INDEX ();
1913 struct gcpro gcpro1, gcpro2, gcpro3;
1915 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1916 return fundef;
1918 if (EQ (macro_only, Qmacro))
1920 Lisp_Object kind = Fnth (make_number (4), fundef);
1921 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1922 return fundef;
1925 /* This is to make sure that loadup.el gives a clear picture
1926 of what files are preloaded and when. */
1927 if (! NILP (Vpurify_flag))
1928 error ("Attempt to autoload %s while preparing to dump",
1929 SDATA (SYMBOL_NAME (funname)));
1931 CHECK_SYMBOL (funname);
1932 GCPRO3 (funname, fundef, macro_only);
1934 /* Preserve the match data. */
1935 record_unwind_save_match_data ();
1937 /* If autoloading gets an error (which includes the error of failing
1938 to define the function being called), we use Vautoload_queue
1939 to undo function definitions and `provide' calls made by
1940 the function. We do this in the specific case of autoloading
1941 because autoloading is not an explicit request "load this file",
1942 but rather a request to "call this function".
1944 The value saved here is to be restored into Vautoload_queue. */
1945 record_unwind_protect (un_autoload, Vautoload_queue);
1946 Vautoload_queue = Qt;
1947 /* If `macro_only', assume this autoload to be a "best-effort",
1948 so don't signal an error if autoloading fails. */
1949 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1951 /* Once loading finishes, don't undo it. */
1952 Vautoload_queue = Qt;
1953 unbind_to (count, Qnil);
1955 UNGCPRO;
1957 if (NILP (funname))
1958 return Qnil;
1959 else
1961 Lisp_Object fun = Findirect_function (funname, Qnil);
1963 if (!NILP (Fequal (fun, fundef)))
1964 error ("Autoloading failed to define function %s",
1965 SDATA (SYMBOL_NAME (funname)));
1966 else
1967 return fun;
1972 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1973 doc: /* Evaluate FORM and return its value.
1974 If LEXICAL is t, evaluate using lexical scoping.
1975 LEXICAL can also be an actual lexical environment, in the form of an
1976 alist mapping symbols to their value. */)
1977 (Lisp_Object form, Lisp_Object lexical)
1979 ptrdiff_t count = SPECPDL_INDEX ();
1980 specbind (Qinternal_interpreter_environment,
1981 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1982 return unbind_to (count, eval_sub (form));
1985 /* Grow the specpdl stack by one entry.
1986 The caller should have already initialized the entry.
1987 Signal an error on stack overflow.
1989 Make sure that there is always one unused entry past the top of the
1990 stack, so that the just-initialized entry is safely unwound if
1991 memory exhausted and an error is signaled here. Also, allocate a
1992 never-used entry just before the bottom of the stack; sometimes its
1993 address is taken. */
1995 static void
1996 grow_specpdl (void)
1998 specpdl_ptr++;
2000 if (specpdl_ptr == specpdl + specpdl_size)
2002 ptrdiff_t count = SPECPDL_INDEX ();
2003 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2004 union specbinding *pdlvec = specpdl - 1;
2005 ptrdiff_t pdlvecsize = specpdl_size + 1;
2006 if (max_size <= specpdl_size)
2008 if (max_specpdl_size < 400)
2009 max_size = max_specpdl_size = 400;
2010 if (max_size <= specpdl_size)
2011 signal_error ("Variable binding depth exceeds max-specpdl-size",
2012 Qnil);
2014 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2015 specpdl = pdlvec + 1;
2016 specpdl_size = pdlvecsize - 1;
2017 specpdl_ptr = specpdl + count;
2021 ptrdiff_t
2022 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2024 ptrdiff_t count = SPECPDL_INDEX ();
2026 eassert (nargs >= UNEVALLED);
2027 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2028 specpdl_ptr->bt.debug_on_exit = false;
2029 specpdl_ptr->bt.function = function;
2030 specpdl_ptr->bt.args = args;
2031 specpdl_ptr->bt.nargs = nargs;
2032 grow_specpdl ();
2034 return count;
2037 /* Eval a sub-expression of the current expression (i.e. in the same
2038 lexical scope). */
2039 Lisp_Object
2040 eval_sub (Lisp_Object form)
2042 Lisp_Object fun, val, original_fun, original_args;
2043 Lisp_Object funcar;
2044 struct gcpro gcpro1, gcpro2, gcpro3;
2045 ptrdiff_t count;
2047 if (SYMBOLP (form))
2049 /* Look up its binding in the lexical environment.
2050 We do not pay attention to the declared_special flag here, since we
2051 already did that when let-binding the variable. */
2052 Lisp_Object lex_binding
2053 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2054 ? Fassq (form, Vinternal_interpreter_environment)
2055 : Qnil;
2056 if (CONSP (lex_binding))
2057 return XCDR (lex_binding);
2058 else
2059 return Fsymbol_value (form);
2062 if (!CONSP (form))
2063 return form;
2065 QUIT;
2067 GCPRO1 (form);
2068 maybe_gc ();
2069 UNGCPRO;
2071 if (++lisp_eval_depth > max_lisp_eval_depth)
2073 if (max_lisp_eval_depth < 100)
2074 max_lisp_eval_depth = 100;
2075 if (lisp_eval_depth > max_lisp_eval_depth)
2076 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2079 original_fun = XCAR (form);
2080 original_args = XCDR (form);
2082 /* This also protects them from gc. */
2083 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2085 if (debug_on_next_call)
2086 do_debug_on_call (Qt, count);
2088 /* At this point, only original_fun and original_args
2089 have values that will be used below. */
2090 retry:
2092 /* Optimize for no indirection. */
2093 fun = original_fun;
2094 if (!SYMBOLP (fun))
2095 fun = Ffunction (Fcons (fun, Qnil));
2096 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2097 fun = indirect_function (fun);
2099 if (SUBRP (fun))
2101 Lisp_Object numargs;
2102 Lisp_Object argvals[8];
2103 Lisp_Object args_left;
2104 register int i, maxargs;
2106 args_left = original_args;
2107 numargs = Flength (args_left);
2109 check_cons_list ();
2111 if (XINT (numargs) < XSUBR (fun)->min_args
2112 || (XSUBR (fun)->max_args >= 0
2113 && XSUBR (fun)->max_args < XINT (numargs)))
2114 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2116 else if (XSUBR (fun)->max_args == UNEVALLED)
2117 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2118 else if (XSUBR (fun)->max_args == MANY)
2120 /* Pass a vector of evaluated arguments. */
2121 Lisp_Object *vals;
2122 ptrdiff_t argnum = 0;
2123 USE_SAFE_ALLOCA;
2125 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2127 GCPRO3 (args_left, fun, fun);
2128 gcpro3.var = vals;
2129 gcpro3.nvars = 0;
2131 while (!NILP (args_left))
2133 vals[argnum++] = eval_sub (Fcar (args_left));
2134 args_left = Fcdr (args_left);
2135 gcpro3.nvars = argnum;
2138 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2140 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2141 UNGCPRO;
2142 SAFE_FREE ();
2144 else
2146 GCPRO3 (args_left, fun, fun);
2147 gcpro3.var = argvals;
2148 gcpro3.nvars = 0;
2150 maxargs = XSUBR (fun)->max_args;
2151 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2153 argvals[i] = eval_sub (Fcar (args_left));
2154 gcpro3.nvars = ++i;
2157 UNGCPRO;
2159 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2161 switch (i)
2163 case 0:
2164 val = (XSUBR (fun)->function.a0 ());
2165 break;
2166 case 1:
2167 val = (XSUBR (fun)->function.a1 (argvals[0]));
2168 break;
2169 case 2:
2170 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2171 break;
2172 case 3:
2173 val = (XSUBR (fun)->function.a3
2174 (argvals[0], argvals[1], argvals[2]));
2175 break;
2176 case 4:
2177 val = (XSUBR (fun)->function.a4
2178 (argvals[0], argvals[1], argvals[2], argvals[3]));
2179 break;
2180 case 5:
2181 val = (XSUBR (fun)->function.a5
2182 (argvals[0], argvals[1], argvals[2], argvals[3],
2183 argvals[4]));
2184 break;
2185 case 6:
2186 val = (XSUBR (fun)->function.a6
2187 (argvals[0], argvals[1], argvals[2], argvals[3],
2188 argvals[4], argvals[5]));
2189 break;
2190 case 7:
2191 val = (XSUBR (fun)->function.a7
2192 (argvals[0], argvals[1], argvals[2], argvals[3],
2193 argvals[4], argvals[5], argvals[6]));
2194 break;
2196 case 8:
2197 val = (XSUBR (fun)->function.a8
2198 (argvals[0], argvals[1], argvals[2], argvals[3],
2199 argvals[4], argvals[5], argvals[6], argvals[7]));
2200 break;
2202 default:
2203 /* Someone has created a subr that takes more arguments than
2204 is supported by this code. We need to either rewrite the
2205 subr to use a different argument protocol, or add more
2206 cases to this switch. */
2207 emacs_abort ();
2211 else if (COMPILEDP (fun))
2212 val = apply_lambda (fun, original_args, count);
2213 else
2215 if (NILP (fun))
2216 xsignal1 (Qvoid_function, original_fun);
2217 if (!CONSP (fun))
2218 xsignal1 (Qinvalid_function, original_fun);
2219 funcar = XCAR (fun);
2220 if (!SYMBOLP (funcar))
2221 xsignal1 (Qinvalid_function, original_fun);
2222 if (EQ (funcar, Qautoload))
2224 Fautoload_do_load (fun, original_fun, Qnil);
2225 goto retry;
2227 if (EQ (funcar, Qmacro))
2229 ptrdiff_t count1 = SPECPDL_INDEX ();
2230 Lisp_Object exp;
2231 /* Bind lexical-binding during expansion of the macro, so the
2232 macro can know reliably if the code it outputs will be
2233 interpreted using lexical-binding or not. */
2234 specbind (Qlexical_binding,
2235 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2236 exp = apply1 (Fcdr (fun), original_args);
2237 unbind_to (count1, Qnil);
2238 val = eval_sub (exp);
2240 else if (EQ (funcar, Qlambda)
2241 || EQ (funcar, Qclosure))
2242 val = apply_lambda (fun, original_args, count);
2243 else
2244 xsignal1 (Qinvalid_function, original_fun);
2246 check_cons_list ();
2248 lisp_eval_depth--;
2249 if (backtrace_debug_on_exit (specpdl + count))
2250 val = call_debugger (list2 (Qexit, val));
2251 specpdl_ptr--;
2253 return val;
2256 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2257 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2258 Then return the value FUNCTION returns.
2259 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2260 usage: (apply FUNCTION &rest ARGUMENTS) */)
2261 (ptrdiff_t nargs, Lisp_Object *args)
2263 ptrdiff_t i, numargs, funcall_nargs;
2264 register Lisp_Object *funcall_args = NULL;
2265 register Lisp_Object spread_arg = args[nargs - 1];
2266 Lisp_Object fun = args[0];
2267 Lisp_Object retval;
2268 USE_SAFE_ALLOCA;
2270 CHECK_LIST (spread_arg);
2272 numargs = XINT (Flength (spread_arg));
2274 if (numargs == 0)
2275 return Ffuncall (nargs - 1, args);
2276 else if (numargs == 1)
2278 args [nargs - 1] = XCAR (spread_arg);
2279 return Ffuncall (nargs, args);
2282 numargs += nargs - 2;
2284 /* Optimize for no indirection. */
2285 if (SYMBOLP (fun) && !NILP (fun)
2286 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2288 fun = indirect_function (fun);
2289 if (NILP (fun))
2290 /* Let funcall get the error. */
2291 fun = args[0];
2294 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2295 /* Don't hide an error by adding missing arguments. */
2296 && numargs >= XSUBR (fun)->min_args)
2298 /* Avoid making funcall cons up a yet another new vector of arguments
2299 by explicitly supplying nil's for optional values. */
2300 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2301 for (i = numargs; i < XSUBR (fun)->max_args; /* nothing */)
2302 funcall_args[++i] = Qnil;
2303 funcall_nargs = 1 + XSUBR (fun)->max_args;
2305 else
2306 { /* We add 1 to numargs because funcall_args includes the
2307 function itself as well as its arguments. */
2308 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2309 funcall_nargs = 1 + numargs;
2312 memcpy (funcall_args, args, nargs * word_size);
2313 /* Spread the last arg we got. Its first element goes in
2314 the slot that it used to occupy, hence this value of I. */
2315 i = nargs - 1;
2316 while (!NILP (spread_arg))
2318 funcall_args [i++] = XCAR (spread_arg);
2319 spread_arg = XCDR (spread_arg);
2322 /* Ffuncall gcpro's all of its args. */
2323 retval = Ffuncall (funcall_nargs, funcall_args);
2325 SAFE_FREE ();
2326 return retval;
2329 /* Run hook variables in various ways. */
2331 static Lisp_Object
2332 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2334 Ffuncall (nargs, args);
2335 return Qnil;
2338 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2339 doc: /* Run each hook in HOOKS.
2340 Each argument should be a symbol, a hook variable.
2341 These symbols are processed in the order specified.
2342 If a hook symbol has a non-nil value, that value may be a function
2343 or a list of functions to be called to run the hook.
2344 If the value is a function, it is called with no arguments.
2345 If it is a list, the elements are called, in order, with no arguments.
2347 Major modes should not use this function directly to run their mode
2348 hook; they should use `run-mode-hooks' instead.
2350 Do not use `make-local-variable' to make a hook variable buffer-local.
2351 Instead, use `add-hook' and specify t for the LOCAL argument.
2352 usage: (run-hooks &rest HOOKS) */)
2353 (ptrdiff_t nargs, Lisp_Object *args)
2355 ptrdiff_t i;
2357 for (i = 0; i < nargs; i++)
2358 run_hook (args[i]);
2360 return Qnil;
2363 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2364 Srun_hook_with_args, 1, MANY, 0,
2365 doc: /* Run HOOK with the specified arguments ARGS.
2366 HOOK should be a symbol, a hook variable. The value of HOOK
2367 may be nil, a function, or a list of functions. Call each
2368 function in order with arguments ARGS. The final return value
2369 is unspecified.
2371 Do not use `make-local-variable' to make a hook variable buffer-local.
2372 Instead, use `add-hook' and specify t for the LOCAL argument.
2373 usage: (run-hook-with-args HOOK &rest ARGS) */)
2374 (ptrdiff_t nargs, Lisp_Object *args)
2376 return run_hook_with_args (nargs, args, funcall_nil);
2379 /* NB this one still documents a specific non-nil return value.
2380 (As did run-hook-with-args and run-hook-with-args-until-failure
2381 until they were changed in 24.1.) */
2382 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2383 Srun_hook_with_args_until_success, 1, MANY, 0,
2384 doc: /* Run HOOK with the specified arguments ARGS.
2385 HOOK should be a symbol, a hook variable. The value of HOOK
2386 may be nil, a function, or a list of functions. Call each
2387 function in order with arguments ARGS, stopping at the first
2388 one that returns non-nil, and return that value. Otherwise (if
2389 all functions return nil, or if there are no functions to call),
2390 return nil.
2392 Do not use `make-local-variable' to make a hook variable buffer-local.
2393 Instead, use `add-hook' and specify t for the LOCAL argument.
2394 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2395 (ptrdiff_t nargs, Lisp_Object *args)
2397 return run_hook_with_args (nargs, args, Ffuncall);
2400 static Lisp_Object
2401 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2403 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2406 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2407 Srun_hook_with_args_until_failure, 1, MANY, 0,
2408 doc: /* Run HOOK with the specified arguments ARGS.
2409 HOOK should be a symbol, a hook variable. The value of HOOK
2410 may be nil, a function, or a list of functions. Call each
2411 function in order with arguments ARGS, stopping at the first
2412 one that returns nil, and return nil. Otherwise (if all functions
2413 return non-nil, or if there are no functions to call), return non-nil
2414 \(do not rely on the precise return value in this case).
2416 Do not use `make-local-variable' to make a hook variable buffer-local.
2417 Instead, use `add-hook' and specify t for the LOCAL argument.
2418 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2419 (ptrdiff_t nargs, Lisp_Object *args)
2421 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2424 static Lisp_Object
2425 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2427 Lisp_Object tmp = args[0], ret;
2428 args[0] = args[1];
2429 args[1] = tmp;
2430 ret = Ffuncall (nargs, args);
2431 args[1] = args[0];
2432 args[0] = tmp;
2433 return ret;
2436 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2437 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2438 I.e. instead of calling each function FUN directly with arguments ARGS,
2439 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2440 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2441 aborts and returns that value.
2442 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2443 (ptrdiff_t nargs, Lisp_Object *args)
2445 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2448 /* ARGS[0] should be a hook symbol.
2449 Call each of the functions in the hook value, passing each of them
2450 as arguments all the rest of ARGS (all NARGS - 1 elements).
2451 FUNCALL specifies how to call each function on the hook.
2452 The caller (or its caller, etc) must gcpro all of ARGS,
2453 except that it isn't necessary to gcpro ARGS[0]. */
2455 Lisp_Object
2456 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2457 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2459 Lisp_Object sym, val, ret = Qnil;
2460 struct gcpro gcpro1, gcpro2, gcpro3;
2462 /* If we are dying or still initializing,
2463 don't do anything--it would probably crash if we tried. */
2464 if (NILP (Vrun_hooks))
2465 return Qnil;
2467 sym = args[0];
2468 val = find_symbol_value (sym);
2470 if (EQ (val, Qunbound) || NILP (val))
2471 return ret;
2472 else if (!CONSP (val) || FUNCTIONP (val))
2474 args[0] = val;
2475 return funcall (nargs, args);
2477 else
2479 Lisp_Object global_vals = Qnil;
2480 GCPRO3 (sym, val, global_vals);
2482 for (;
2483 CONSP (val) && NILP (ret);
2484 val = XCDR (val))
2486 if (EQ (XCAR (val), Qt))
2488 /* t indicates this hook has a local binding;
2489 it means to run the global binding too. */
2490 global_vals = Fdefault_value (sym);
2491 if (NILP (global_vals)) continue;
2493 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2495 args[0] = global_vals;
2496 ret = funcall (nargs, args);
2498 else
2500 for (;
2501 CONSP (global_vals) && NILP (ret);
2502 global_vals = XCDR (global_vals))
2504 args[0] = XCAR (global_vals);
2505 /* In a global value, t should not occur. If it does, we
2506 must ignore it to avoid an endless loop. */
2507 if (!EQ (args[0], Qt))
2508 ret = funcall (nargs, args);
2512 else
2514 args[0] = XCAR (val);
2515 ret = funcall (nargs, args);
2519 UNGCPRO;
2520 return ret;
2524 /* Run the hook HOOK, giving each function no args. */
2526 void
2527 run_hook (Lisp_Object hook)
2529 Frun_hook_with_args (1, &hook);
2532 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2534 void
2535 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2537 Frun_hook_with_args (3, ((Lisp_Object []) { hook, arg1, arg2 }));
2540 /* Apply fn to arg. */
2541 Lisp_Object
2542 apply1 (Lisp_Object fn, Lisp_Object arg)
2544 return (NILP (arg) ? Ffuncall (1, &fn)
2545 : Fapply (2, ((Lisp_Object []) { 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 Ffuncall (2, ((Lisp_Object []) { 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 Ffuncall (3, ((Lisp_Object []) { 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 Ffuncall (4, ((Lisp_Object []) { 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 Ffuncall (5, ((Lisp_Object []) { 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 Ffuncall (6, ((Lisp_Object []) { 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 Ffuncall (7, ((Lisp_Object [])
2604 { fn, arg1, arg2, arg3, arg4, arg5, arg6 }));
2607 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2608 /* ARGSUSED */
2609 Lisp_Object
2610 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2611 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2613 return Ffuncall (8, ((Lisp_Object [])
2614 { fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7 }));
2617 /* The caller should GCPRO all the elements of ARGS. */
2619 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2620 doc: /* Non-nil if OBJECT is a function. */)
2621 (Lisp_Object object)
2623 if (FUNCTIONP (object))
2624 return Qt;
2625 return Qnil;
2628 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2629 doc: /* Call first argument as a function, passing remaining arguments to it.
2630 Return the value that function returns.
2631 Thus, (funcall 'cons 'x 'y) returns (x . y).
2632 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2633 (ptrdiff_t nargs, Lisp_Object *args)
2635 Lisp_Object fun, original_fun;
2636 Lisp_Object funcar;
2637 ptrdiff_t numargs = nargs - 1;
2638 Lisp_Object lisp_numargs;
2639 Lisp_Object val;
2640 register Lisp_Object *internal_args;
2641 ptrdiff_t i, count;
2643 QUIT;
2645 if (++lisp_eval_depth > max_lisp_eval_depth)
2647 if (max_lisp_eval_depth < 100)
2648 max_lisp_eval_depth = 100;
2649 if (lisp_eval_depth > max_lisp_eval_depth)
2650 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2653 /* This also GCPROs them. */
2654 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2656 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2657 maybe_gc ();
2659 if (debug_on_next_call)
2660 do_debug_on_call (Qlambda, count);
2662 check_cons_list ();
2664 original_fun = args[0];
2666 retry:
2668 /* Optimize for no indirection. */
2669 fun = original_fun;
2670 if (SYMBOLP (fun) && !NILP (fun)
2671 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2672 fun = indirect_function (fun);
2674 if (SUBRP (fun))
2676 if (numargs < XSUBR (fun)->min_args
2677 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2679 XSETFASTINT (lisp_numargs, numargs);
2680 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2683 else if (XSUBR (fun)->max_args == UNEVALLED)
2684 xsignal1 (Qinvalid_function, original_fun);
2686 else if (XSUBR (fun)->max_args == MANY)
2687 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2688 else
2690 Lisp_Object internal_argbuf[8];
2691 if (XSUBR (fun)->max_args > numargs)
2693 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2694 internal_args = internal_argbuf;
2695 memcpy (internal_args, args + 1, numargs * word_size);
2696 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2697 internal_args[i] = Qnil;
2699 else
2700 internal_args = args + 1;
2701 switch (XSUBR (fun)->max_args)
2703 case 0:
2704 val = (XSUBR (fun)->function.a0 ());
2705 break;
2706 case 1:
2707 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2708 break;
2709 case 2:
2710 val = (XSUBR (fun)->function.a2
2711 (internal_args[0], internal_args[1]));
2712 break;
2713 case 3:
2714 val = (XSUBR (fun)->function.a3
2715 (internal_args[0], internal_args[1], internal_args[2]));
2716 break;
2717 case 4:
2718 val = (XSUBR (fun)->function.a4
2719 (internal_args[0], internal_args[1], internal_args[2],
2720 internal_args[3]));
2721 break;
2722 case 5:
2723 val = (XSUBR (fun)->function.a5
2724 (internal_args[0], internal_args[1], internal_args[2],
2725 internal_args[3], internal_args[4]));
2726 break;
2727 case 6:
2728 val = (XSUBR (fun)->function.a6
2729 (internal_args[0], internal_args[1], internal_args[2],
2730 internal_args[3], internal_args[4], internal_args[5]));
2731 break;
2732 case 7:
2733 val = (XSUBR (fun)->function.a7
2734 (internal_args[0], internal_args[1], internal_args[2],
2735 internal_args[3], internal_args[4], internal_args[5],
2736 internal_args[6]));
2737 break;
2739 case 8:
2740 val = (XSUBR (fun)->function.a8
2741 (internal_args[0], internal_args[1], internal_args[2],
2742 internal_args[3], internal_args[4], internal_args[5],
2743 internal_args[6], internal_args[7]));
2744 break;
2746 default:
2748 /* If a subr takes more than 8 arguments without using MANY
2749 or UNEVALLED, we need to extend this function to support it.
2750 Until this is done, there is no way to call the function. */
2751 emacs_abort ();
2755 else if (COMPILEDP (fun))
2756 val = funcall_lambda (fun, numargs, args + 1);
2757 else
2759 if (NILP (fun))
2760 xsignal1 (Qvoid_function, original_fun);
2761 if (!CONSP (fun))
2762 xsignal1 (Qinvalid_function, original_fun);
2763 funcar = XCAR (fun);
2764 if (!SYMBOLP (funcar))
2765 xsignal1 (Qinvalid_function, original_fun);
2766 if (EQ (funcar, Qlambda)
2767 || EQ (funcar, Qclosure))
2768 val = funcall_lambda (fun, numargs, args + 1);
2769 else if (EQ (funcar, Qautoload))
2771 Fautoload_do_load (fun, original_fun, Qnil);
2772 check_cons_list ();
2773 goto retry;
2775 else
2776 xsignal1 (Qinvalid_function, original_fun);
2778 check_cons_list ();
2779 lisp_eval_depth--;
2780 if (backtrace_debug_on_exit (specpdl + count))
2781 val = call_debugger (list2 (Qexit, val));
2782 specpdl_ptr--;
2783 return val;
2786 static Lisp_Object
2787 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2789 Lisp_Object args_left;
2790 ptrdiff_t i;
2791 EMACS_INT numargs;
2792 register Lisp_Object *arg_vector;
2793 struct gcpro gcpro1, gcpro2, gcpro3;
2794 register Lisp_Object tem;
2795 USE_SAFE_ALLOCA;
2797 numargs = XFASTINT (Flength (args));
2798 SAFE_ALLOCA_LISP (arg_vector, numargs);
2799 args_left = args;
2801 GCPRO3 (*arg_vector, args_left, fun);
2802 gcpro1.nvars = 0;
2804 for (i = 0; i < numargs; )
2806 tem = Fcar (args_left), args_left = Fcdr (args_left);
2807 tem = eval_sub (tem);
2808 arg_vector[i++] = tem;
2809 gcpro1.nvars = i;
2812 UNGCPRO;
2814 set_backtrace_args (specpdl + count, arg_vector, i);
2815 tem = funcall_lambda (fun, numargs, arg_vector);
2817 /* Do the debug-on-exit now, while arg_vector still exists. */
2818 if (backtrace_debug_on_exit (specpdl + count))
2820 /* Don't do it again when we return to eval. */
2821 set_backtrace_debug_on_exit (specpdl + count, false);
2822 tem = call_debugger (list2 (Qexit, tem));
2824 SAFE_FREE ();
2825 return tem;
2828 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2829 and return the result of evaluation.
2830 FUN must be either a lambda-expression or a compiled-code object. */
2832 static Lisp_Object
2833 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2834 register Lisp_Object *arg_vector)
2836 Lisp_Object val, syms_left, next, lexenv;
2837 ptrdiff_t count = SPECPDL_INDEX ();
2838 ptrdiff_t i;
2839 bool optional, rest;
2841 if (CONSP (fun))
2843 if (EQ (XCAR (fun), Qclosure))
2845 fun = XCDR (fun); /* Drop `closure'. */
2846 lexenv = XCAR (fun);
2847 CHECK_LIST_CONS (fun, fun);
2849 else
2850 lexenv = Qnil;
2851 syms_left = XCDR (fun);
2852 if (CONSP (syms_left))
2853 syms_left = XCAR (syms_left);
2854 else
2855 xsignal1 (Qinvalid_function, fun);
2857 else if (COMPILEDP (fun))
2859 syms_left = AREF (fun, COMPILED_ARGLIST);
2860 if (INTEGERP (syms_left))
2861 /* A byte-code object with a non-nil `push args' slot means we
2862 shouldn't bind any arguments, instead just call the byte-code
2863 interpreter directly; it will push arguments as necessary.
2865 Byte-code objects with either a non-existent, or a nil value for
2866 the `push args' slot (the default), have dynamically-bound
2867 arguments, and use the argument-binding code below instead (as do
2868 all interpreted functions, even lexically bound ones). */
2870 /* If we have not actually read the bytecode string
2871 and constants vector yet, fetch them from the file. */
2872 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2873 Ffetch_bytecode (fun);
2874 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2875 AREF (fun, COMPILED_CONSTANTS),
2876 AREF (fun, COMPILED_STACK_DEPTH),
2877 syms_left,
2878 nargs, arg_vector);
2880 lexenv = Qnil;
2882 else
2883 emacs_abort ();
2885 i = optional = rest = 0;
2886 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2888 QUIT;
2890 next = XCAR (syms_left);
2891 if (!SYMBOLP (next))
2892 xsignal1 (Qinvalid_function, fun);
2894 if (EQ (next, Qand_rest))
2895 rest = 1;
2896 else if (EQ (next, Qand_optional))
2897 optional = 1;
2898 else
2900 Lisp_Object arg;
2901 if (rest)
2903 arg = Flist (nargs - i, &arg_vector[i]);
2904 i = nargs;
2906 else if (i < nargs)
2907 arg = arg_vector[i++];
2908 else if (!optional)
2909 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2910 else
2911 arg = Qnil;
2913 /* Bind the argument. */
2914 if (!NILP (lexenv) && SYMBOLP (next))
2915 /* Lexically bind NEXT by adding it to the lexenv alist. */
2916 lexenv = Fcons (Fcons (next, arg), lexenv);
2917 else
2918 /* Dynamically bind NEXT. */
2919 specbind (next, arg);
2923 if (!NILP (syms_left))
2924 xsignal1 (Qinvalid_function, fun);
2925 else if (i < nargs)
2926 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2928 if (!EQ (lexenv, Vinternal_interpreter_environment))
2929 /* Instantiate a new lexical environment. */
2930 specbind (Qinternal_interpreter_environment, lexenv);
2932 if (CONSP (fun))
2933 val = Fprogn (XCDR (XCDR (fun)));
2934 else
2936 /* If we have not actually read the bytecode string
2937 and constants vector yet, fetch them from the file. */
2938 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2939 Ffetch_bytecode (fun);
2940 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2941 AREF (fun, COMPILED_CONSTANTS),
2942 AREF (fun, COMPILED_STACK_DEPTH),
2943 Qnil, 0, 0);
2946 return unbind_to (count, val);
2949 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2950 1, 1, 0,
2951 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2952 (Lisp_Object object)
2954 Lisp_Object tem;
2956 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2958 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2959 if (!CONSP (tem))
2961 tem = AREF (object, COMPILED_BYTECODE);
2962 if (CONSP (tem) && STRINGP (XCAR (tem)))
2963 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2964 else
2965 error ("Invalid byte code");
2967 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2968 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2970 return object;
2973 /* Return true if SYMBOL currently has a let-binding
2974 which was made in the buffer that is now current. */
2976 bool
2977 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2979 union specbinding *p;
2980 Lisp_Object buf = Fcurrent_buffer ();
2982 for (p = specpdl_ptr; p > specpdl; )
2983 if ((--p)->kind > SPECPDL_LET)
2985 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2986 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2987 if (symbol == let_bound_symbol
2988 && EQ (specpdl_where (p), buf))
2989 return 1;
2992 return 0;
2995 bool
2996 let_shadows_global_binding_p (Lisp_Object symbol)
2998 union specbinding *p;
3000 for (p = specpdl_ptr; p > specpdl; )
3001 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3002 return 1;
3004 return 0;
3007 /* `specpdl_ptr' describes which variable is
3008 let-bound, so it can be properly undone when we unbind_to.
3009 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3010 - SYMBOL is the variable being bound. Note that it should not be
3011 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3012 to record V2 here).
3013 - WHERE tells us in which buffer the binding took place.
3014 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3015 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3016 i.e. bindings to the default value of a variable which can be
3017 buffer-local. */
3019 void
3020 specbind (Lisp_Object symbol, Lisp_Object value)
3022 struct Lisp_Symbol *sym;
3024 CHECK_SYMBOL (symbol);
3025 sym = XSYMBOL (symbol);
3027 start:
3028 switch (sym->redirect)
3030 case SYMBOL_VARALIAS:
3031 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3032 case SYMBOL_PLAINVAL:
3033 /* The most common case is that of a non-constant symbol with a
3034 trivial value. Make that as fast as we can. */
3035 specpdl_ptr->let.kind = SPECPDL_LET;
3036 specpdl_ptr->let.symbol = symbol;
3037 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3038 grow_specpdl ();
3039 if (!sym->constant)
3040 SET_SYMBOL_VAL (sym, value);
3041 else
3042 set_internal (symbol, value, Qnil, 1);
3043 break;
3044 case SYMBOL_LOCALIZED:
3045 if (SYMBOL_BLV (sym)->frame_local)
3046 error ("Frame-local vars cannot be let-bound");
3047 case SYMBOL_FORWARDED:
3049 Lisp_Object ovalue = find_symbol_value (symbol);
3050 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3051 specpdl_ptr->let.symbol = symbol;
3052 specpdl_ptr->let.old_value = ovalue;
3053 specpdl_ptr->let.where = Fcurrent_buffer ();
3055 eassert (sym->redirect != SYMBOL_LOCALIZED
3056 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3058 if (sym->redirect == SYMBOL_LOCALIZED)
3060 if (!blv_found (SYMBOL_BLV (sym)))
3061 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3063 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3065 /* If SYMBOL is a per-buffer variable which doesn't have a
3066 buffer-local value here, make the `let' change the global
3067 value by changing the value of SYMBOL in all buffers not
3068 having their own value. This is consistent with what
3069 happens with other buffer-local variables. */
3070 if (NILP (Flocal_variable_p (symbol, Qnil)))
3072 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3073 grow_specpdl ();
3074 Fset_default (symbol, value);
3075 return;
3078 else
3079 specpdl_ptr->let.kind = SPECPDL_LET;
3081 grow_specpdl ();
3082 set_internal (symbol, value, Qnil, 1);
3083 break;
3085 default: emacs_abort ();
3089 /* Push unwind-protect entries of various types. */
3091 void
3092 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3094 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3095 specpdl_ptr->unwind.func = function;
3096 specpdl_ptr->unwind.arg = arg;
3097 grow_specpdl ();
3100 void
3101 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3103 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3104 specpdl_ptr->unwind_ptr.func = function;
3105 specpdl_ptr->unwind_ptr.arg = arg;
3106 grow_specpdl ();
3109 void
3110 record_unwind_protect_int (void (*function) (int), int arg)
3112 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3113 specpdl_ptr->unwind_int.func = function;
3114 specpdl_ptr->unwind_int.arg = arg;
3115 grow_specpdl ();
3118 void
3119 record_unwind_protect_void (void (*function) (void))
3121 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3122 specpdl_ptr->unwind_void.func = function;
3123 grow_specpdl ();
3126 static void
3127 do_nothing (void)
3130 /* Push an unwind-protect entry that does nothing, so that
3131 set_unwind_protect_ptr can overwrite it later. */
3133 void
3134 record_unwind_protect_nothing (void)
3136 record_unwind_protect_void (do_nothing);
3139 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3140 It need not be at the top of the stack. */
3142 void
3143 clear_unwind_protect (ptrdiff_t count)
3145 union specbinding *p = specpdl + count;
3146 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3147 p->unwind_void.func = do_nothing;
3150 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3151 It need not be at the top of the stack. Discard the entry's
3152 previous value without invoking it. */
3154 void
3155 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3156 Lisp_Object arg)
3158 union specbinding *p = specpdl + count;
3159 p->unwind.kind = SPECPDL_UNWIND;
3160 p->unwind.func = func;
3161 p->unwind.arg = arg;
3164 void
3165 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3167 union specbinding *p = specpdl + count;
3168 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3169 p->unwind_ptr.func = func;
3170 p->unwind_ptr.arg = arg;
3173 /* Pop and execute entries from the unwind-protect stack until the
3174 depth COUNT is reached. Return VALUE. */
3176 Lisp_Object
3177 unbind_to (ptrdiff_t count, Lisp_Object value)
3179 Lisp_Object quitf = Vquit_flag;
3180 struct gcpro gcpro1, gcpro2;
3182 GCPRO2 (value, quitf);
3183 Vquit_flag = Qnil;
3185 while (specpdl_ptr != specpdl + count)
3187 /* Decrement specpdl_ptr before we do the work to unbind it, so
3188 that an error in unbinding won't try to unbind the same entry
3189 again. Take care to copy any parts of the binding needed
3190 before invoking any code that can make more bindings. */
3192 specpdl_ptr--;
3194 switch (specpdl_ptr->kind)
3196 case SPECPDL_UNWIND:
3197 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3198 break;
3199 case SPECPDL_UNWIND_PTR:
3200 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3201 break;
3202 case SPECPDL_UNWIND_INT:
3203 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3204 break;
3205 case SPECPDL_UNWIND_VOID:
3206 specpdl_ptr->unwind_void.func ();
3207 break;
3208 case SPECPDL_BACKTRACE:
3209 break;
3210 case SPECPDL_LET:
3211 { /* If variable has a trivial value (no forwarding), we can
3212 just set it. No need to check for constant symbols here,
3213 since that was already done by specbind. */
3214 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3215 if (sym->redirect == SYMBOL_PLAINVAL)
3217 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3218 break;
3220 else
3221 { /* FALLTHROUGH!!
3222 NOTE: we only ever come here if make_local_foo was used for
3223 the first time on this var within this let. */
3226 case SPECPDL_LET_DEFAULT:
3227 Fset_default (specpdl_symbol (specpdl_ptr),
3228 specpdl_old_value (specpdl_ptr));
3229 break;
3230 case SPECPDL_LET_LOCAL:
3232 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3233 Lisp_Object where = specpdl_where (specpdl_ptr);
3234 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3235 eassert (BUFFERP (where));
3237 /* If this was a local binding, reset the value in the appropriate
3238 buffer, but only if that buffer's binding still exists. */
3239 if (!NILP (Flocal_variable_p (symbol, where)))
3240 set_internal (symbol, old_value, where, 1);
3242 break;
3246 if (NILP (Vquit_flag) && !NILP (quitf))
3247 Vquit_flag = quitf;
3249 UNGCPRO;
3250 return value;
3253 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3254 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3255 A special variable is one that will be bound dynamically, even in a
3256 context where binding is lexical by default. */)
3257 (Lisp_Object symbol)
3259 CHECK_SYMBOL (symbol);
3260 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3264 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3265 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3266 The debugger is entered when that frame exits, if the flag is non-nil. */)
3267 (Lisp_Object level, Lisp_Object flag)
3269 union specbinding *pdl = backtrace_top ();
3270 register EMACS_INT i;
3272 CHECK_NUMBER (level);
3274 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3275 pdl = backtrace_next (pdl);
3277 if (backtrace_p (pdl))
3278 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3280 return flag;
3283 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3284 doc: /* Print a trace of Lisp function calls currently active.
3285 Output stream used is value of `standard-output'. */)
3286 (void)
3288 union specbinding *pdl = backtrace_top ();
3289 Lisp_Object tem;
3290 Lisp_Object old_print_level = Vprint_level;
3292 if (NILP (Vprint_level))
3293 XSETFASTINT (Vprint_level, 8);
3295 while (backtrace_p (pdl))
3297 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3298 if (backtrace_nargs (pdl) == UNEVALLED)
3300 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3301 Qnil);
3302 write_string ("\n", -1);
3304 else
3306 tem = backtrace_function (pdl);
3307 Fprin1 (tem, Qnil); /* This can QUIT. */
3308 write_string ("(", -1);
3310 ptrdiff_t i;
3311 for (i = 0; i < backtrace_nargs (pdl); i++)
3313 if (i) write_string (" ", -1);
3314 Fprin1 (backtrace_args (pdl)[i], Qnil);
3317 write_string (")\n", -1);
3319 pdl = backtrace_next (pdl);
3322 Vprint_level = old_print_level;
3323 return Qnil;
3326 static union specbinding *
3327 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3329 union specbinding *pdl = backtrace_top ();
3330 register EMACS_INT i;
3332 CHECK_NATNUM (nframes);
3334 if (!NILP (base))
3335 { /* Skip up to `base'. */
3336 base = Findirect_function (base, Qt);
3337 while (backtrace_p (pdl)
3338 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3339 pdl = backtrace_next (pdl);
3342 /* Find the frame requested. */
3343 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3344 pdl = backtrace_next (pdl);
3346 return pdl;
3349 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3350 doc: /* Return the function and arguments NFRAMES up from current execution point.
3351 If that frame has not evaluated the arguments yet (or is a special form),
3352 the value is (nil FUNCTION ARG-FORMS...).
3353 If that frame has evaluated its arguments and called its function already,
3354 the value is (t FUNCTION ARG-VALUES...).
3355 A &rest arg is represented as the tail of the list ARG-VALUES.
3356 FUNCTION is whatever was supplied as car of evaluated list,
3357 or a lambda expression for macro calls.
3358 If NFRAMES is more than the number of frames, the value is nil.
3359 If BASE is non-nil, it should be a function and NFRAMES counts from its
3360 nearest activation frame. */)
3361 (Lisp_Object nframes, Lisp_Object base)
3363 union specbinding *pdl = get_backtrace_frame (nframes, base);
3365 if (!backtrace_p (pdl))
3366 return Qnil;
3367 if (backtrace_nargs (pdl) == UNEVALLED)
3368 return Fcons (Qnil,
3369 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3370 else
3372 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3374 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3378 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3379 the specpdl stack, and then rewind them. We store the pre-unwind values
3380 directly in the pre-existing specpdl elements (i.e. we swap the current
3381 value and the old value stored in the specpdl), kind of like the inplace
3382 pointer-reversal trick. As it turns out, the rewind does the same as the
3383 unwind, except it starts from the other end of the specpdl stack, so we use
3384 the same function for both unwind and rewind. */
3385 static void
3386 backtrace_eval_unrewind (int distance)
3388 union specbinding *tmp = specpdl_ptr;
3389 int step = -1;
3390 if (distance < 0)
3391 { /* It's a rewind rather than unwind. */
3392 tmp += distance - 1;
3393 step = 1;
3394 distance = -distance;
3397 for (; distance > 0; distance--)
3399 tmp += step;
3400 switch (tmp->kind)
3402 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3403 unwind_protect, but the problem is that we don't know how to
3404 rewind them afterwards. */
3405 case SPECPDL_UNWIND:
3407 Lisp_Object oldarg = tmp->unwind.arg;
3408 if (tmp->unwind.func == set_buffer_if_live)
3409 tmp->unwind.arg = Fcurrent_buffer ();
3410 else if (tmp->unwind.func == save_excursion_restore)
3411 tmp->unwind.arg = save_excursion_save ();
3412 else
3413 break;
3414 tmp->unwind.func (oldarg);
3415 break;
3418 case SPECPDL_UNWIND_PTR:
3419 case SPECPDL_UNWIND_INT:
3420 case SPECPDL_UNWIND_VOID:
3421 case SPECPDL_BACKTRACE:
3422 break;
3423 case SPECPDL_LET:
3424 { /* If variable has a trivial value (no forwarding), we can
3425 just set it. No need to check for constant symbols here,
3426 since that was already done by specbind. */
3427 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3428 if (sym->redirect == SYMBOL_PLAINVAL)
3430 Lisp_Object old_value = specpdl_old_value (tmp);
3431 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3432 SET_SYMBOL_VAL (sym, old_value);
3433 break;
3435 else
3436 { /* FALLTHROUGH!!
3437 NOTE: we only ever come here if make_local_foo was used for
3438 the first time on this var within this let. */
3441 case SPECPDL_LET_DEFAULT:
3443 Lisp_Object sym = specpdl_symbol (tmp);
3444 Lisp_Object old_value = specpdl_old_value (tmp);
3445 set_specpdl_old_value (tmp, Fdefault_value (sym));
3446 Fset_default (sym, old_value);
3448 break;
3449 case SPECPDL_LET_LOCAL:
3451 Lisp_Object symbol = specpdl_symbol (tmp);
3452 Lisp_Object where = specpdl_where (tmp);
3453 Lisp_Object old_value = specpdl_old_value (tmp);
3454 eassert (BUFFERP (where));
3456 /* If this was a local binding, reset the value in the appropriate
3457 buffer, but only if that buffer's binding still exists. */
3458 if (!NILP (Flocal_variable_p (symbol, where)))
3460 set_specpdl_old_value
3461 (tmp, Fbuffer_local_value (symbol, where));
3462 set_internal (symbol, old_value, where, 1);
3465 break;
3470 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3471 doc: /* Evaluate EXP in the context of some activation frame.
3472 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3473 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3475 union specbinding *pdl = get_backtrace_frame (nframes, base);
3476 ptrdiff_t count = SPECPDL_INDEX ();
3477 ptrdiff_t distance = specpdl_ptr - pdl;
3478 eassert (distance >= 0);
3480 if (!backtrace_p (pdl))
3481 error ("Activation frame not found!");
3483 backtrace_eval_unrewind (distance);
3484 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3486 /* Use eval_sub rather than Feval since the main motivation behind
3487 backtrace-eval is to be able to get/set the value of lexical variables
3488 from the debugger. */
3489 return unbind_to (count, eval_sub (exp));
3492 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3493 doc: /* Return names and values of local variables of a stack frame.
3494 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3495 (Lisp_Object nframes, Lisp_Object base)
3497 union specbinding *frame = get_backtrace_frame (nframes, base);
3498 union specbinding *prevframe
3499 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3500 ptrdiff_t distance = specpdl_ptr - frame;
3501 Lisp_Object result = Qnil;
3502 eassert (distance >= 0);
3504 if (!backtrace_p (prevframe))
3505 error ("Activation frame not found!");
3506 if (!backtrace_p (frame))
3507 error ("Activation frame not found!");
3509 /* The specpdl entries normally contain the symbol being bound along with its
3510 `old_value', so it can be restored. The new value to which it is bound is
3511 available in one of two places: either in the current value of the
3512 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3513 next specpdl entry for it.
3514 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3515 and "new value", so we abuse it here, to fetch the new value.
3516 It's ugly (we'd rather not modify global data) and a bit inefficient,
3517 but it does the job for now. */
3518 backtrace_eval_unrewind (distance);
3520 /* Grab values. */
3522 union specbinding *tmp = prevframe;
3523 for (; tmp > frame; tmp--)
3525 switch (tmp->kind)
3527 case SPECPDL_LET:
3528 case SPECPDL_LET_DEFAULT:
3529 case SPECPDL_LET_LOCAL:
3531 Lisp_Object sym = specpdl_symbol (tmp);
3532 Lisp_Object val = specpdl_old_value (tmp);
3533 if (EQ (sym, Qinternal_interpreter_environment))
3535 Lisp_Object env = val;
3536 for (; CONSP (env); env = XCDR (env))
3538 Lisp_Object binding = XCAR (env);
3539 if (CONSP (binding))
3540 result = Fcons (Fcons (XCAR (binding),
3541 XCDR (binding)),
3542 result);
3545 else
3546 result = Fcons (Fcons (sym, val), result);
3552 /* Restore values from specpdl to original place. */
3553 backtrace_eval_unrewind (-distance);
3555 return result;
3559 void
3560 mark_specpdl (void)
3562 union specbinding *pdl;
3563 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3565 switch (pdl->kind)
3567 case SPECPDL_UNWIND:
3568 mark_object (specpdl_arg (pdl));
3569 break;
3571 case SPECPDL_BACKTRACE:
3573 ptrdiff_t nargs = backtrace_nargs (pdl);
3574 mark_object (backtrace_function (pdl));
3575 if (nargs == UNEVALLED)
3576 nargs = 1;
3577 while (nargs--)
3578 mark_object (backtrace_args (pdl)[nargs]);
3580 break;
3582 case SPECPDL_LET_DEFAULT:
3583 case SPECPDL_LET_LOCAL:
3584 mark_object (specpdl_where (pdl));
3585 /* Fall through. */
3586 case SPECPDL_LET:
3587 mark_object (specpdl_symbol (pdl));
3588 mark_object (specpdl_old_value (pdl));
3589 break;
3594 void
3595 get_backtrace (Lisp_Object array)
3597 union specbinding *pdl = backtrace_next (backtrace_top ());
3598 ptrdiff_t i = 0, asize = ASIZE (array);
3600 /* Copy the backtrace contents into working memory. */
3601 for (; i < asize; i++)
3603 if (backtrace_p (pdl))
3605 ASET (array, i, backtrace_function (pdl));
3606 pdl = backtrace_next (pdl);
3608 else
3609 ASET (array, i, Qnil);
3613 Lisp_Object backtrace_top_function (void)
3615 union specbinding *pdl = backtrace_top ();
3616 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3619 void
3620 syms_of_eval (void)
3622 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3623 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3624 If Lisp code tries to increase the total number past this amount,
3625 an error is signaled.
3626 You can safely use a value considerably larger than the default value,
3627 if that proves inconveniently small. However, if you increase it too far,
3628 Emacs could run out of memory trying to make the stack bigger.
3629 Note that this limit may be silently increased by the debugger
3630 if `debug-on-error' or `debug-on-quit' is set. */);
3632 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3633 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3635 This limit serves to catch infinite recursions for you before they cause
3636 actual stack overflow in C, which would be fatal for Emacs.
3637 You can safely make it considerably larger than its default value,
3638 if that proves inconveniently small. However, if you increase it too far,
3639 Emacs could overflow the real C stack, and crash. */);
3641 DEFVAR_LISP ("quit-flag", Vquit_flag,
3642 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3643 If the value is t, that means do an ordinary quit.
3644 If the value equals `throw-on-input', that means quit by throwing
3645 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3646 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3647 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3648 Vquit_flag = Qnil;
3650 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3651 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3652 Note that `quit-flag' will still be set by typing C-g,
3653 so a quit will be signaled as soon as `inhibit-quit' is nil.
3654 To prevent this happening, set `quit-flag' to nil
3655 before making `inhibit-quit' nil. */);
3656 Vinhibit_quit = Qnil;
3658 DEFSYM (Qinhibit_quit, "inhibit-quit");
3659 DEFSYM (Qautoload, "autoload");
3660 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3661 DEFSYM (Qmacro, "macro");
3662 DEFSYM (Qdeclare, "declare");
3664 /* Note that the process handling also uses Qexit, but we don't want
3665 to staticpro it twice, so we just do it here. */
3666 DEFSYM (Qexit, "exit");
3668 DEFSYM (Qinteractive, "interactive");
3669 DEFSYM (Qcommandp, "commandp");
3670 DEFSYM (Qand_rest, "&rest");
3671 DEFSYM (Qand_optional, "&optional");
3672 DEFSYM (Qclosure, "closure");
3673 DEFSYM (Qdebug, "debug");
3675 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3676 doc: /* Non-nil means never enter the debugger.
3677 Normally set while the debugger is already active, to avoid recursive
3678 invocations. */);
3679 Vinhibit_debugger = Qnil;
3681 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3682 doc: /* Non-nil means enter debugger if an error is signaled.
3683 Does not apply to errors handled by `condition-case' or those
3684 matched by `debug-ignored-errors'.
3685 If the value is a list, an error only means to enter the debugger
3686 if one of its condition symbols appears in the list.
3687 When you evaluate an expression interactively, this variable
3688 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3689 The command `toggle-debug-on-error' toggles this.
3690 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3691 Vdebug_on_error = Qnil;
3693 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3694 doc: /* List of errors for which the debugger should not be called.
3695 Each element may be a condition-name or a regexp that matches error messages.
3696 If any element applies to a given error, that error skips the debugger
3697 and just returns to top level.
3698 This overrides the variable `debug-on-error'.
3699 It does not apply to errors handled by `condition-case'. */);
3700 Vdebug_ignored_errors = Qnil;
3702 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3703 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3704 Does not apply if quit is handled by a `condition-case'. */);
3705 debug_on_quit = 0;
3707 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3708 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3710 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3711 doc: /* Non-nil means debugger may continue execution.
3712 This is nil when the debugger is called under circumstances where it
3713 might not be safe to continue. */);
3714 debugger_may_continue = 1;
3716 DEFVAR_LISP ("debugger", Vdebugger,
3717 doc: /* Function to call to invoke debugger.
3718 If due to frame exit, args are `exit' and the value being returned;
3719 this function's value will be returned instead of that.
3720 If due to error, args are `error' and a list of the args to `signal'.
3721 If due to `apply' or `funcall' entry, one arg, `lambda'.
3722 If due to `eval' entry, one arg, t. */);
3723 Vdebugger = Qnil;
3725 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3726 doc: /* If non-nil, this is a function for `signal' to call.
3727 It receives the same arguments that `signal' was given.
3728 The Edebug package uses this to regain control. */);
3729 Vsignal_hook_function = Qnil;
3731 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3732 doc: /* Non-nil means call the debugger regardless of condition handlers.
3733 Note that `debug-on-error', `debug-on-quit' and friends
3734 still determine whether to handle the particular condition. */);
3735 Vdebug_on_signal = Qnil;
3737 /* When lexical binding is being used,
3738 Vinternal_interpreter_environment is non-nil, and contains an alist
3739 of lexically-bound variable, or (t), indicating an empty
3740 environment. The lisp name of this variable would be
3741 `internal-interpreter-environment' if it weren't hidden.
3742 Every element of this list can be either a cons (VAR . VAL)
3743 specifying a lexical binding, or a single symbol VAR indicating
3744 that this variable should use dynamic scoping. */
3745 DEFSYM (Qinternal_interpreter_environment,
3746 "internal-interpreter-environment");
3747 DEFVAR_LISP ("internal-interpreter-environment",
3748 Vinternal_interpreter_environment,
3749 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3750 When lexical binding is not being used, this variable is nil.
3751 A value of `(t)' indicates an empty environment, otherwise it is an
3752 alist of active lexical bindings. */);
3753 Vinternal_interpreter_environment = Qnil;
3754 /* Don't export this variable to Elisp, so no one can mess with it
3755 (Just imagine if someone makes it buffer-local). */
3756 Funintern (Qinternal_interpreter_environment, Qnil);
3758 Vrun_hooks = intern_c_string ("run-hooks");
3759 staticpro (&Vrun_hooks);
3761 staticpro (&Vautoload_queue);
3762 Vautoload_queue = Qnil;
3763 staticpro (&Vsignaling_function);
3764 Vsignaling_function = Qnil;
3766 inhibit_lisp_code = Qnil;
3768 defsubr (&Sor);
3769 defsubr (&Sand);
3770 defsubr (&Sif);
3771 defsubr (&Scond);
3772 defsubr (&Sprogn);
3773 defsubr (&Sprog1);
3774 defsubr (&Sprog2);
3775 defsubr (&Ssetq);
3776 defsubr (&Squote);
3777 defsubr (&Sfunction);
3778 defsubr (&Sdefault_toplevel_value);
3779 defsubr (&Sset_default_toplevel_value);
3780 defsubr (&Sdefvar);
3781 defsubr (&Sdefvaralias);
3782 defsubr (&Sdefconst);
3783 defsubr (&Smake_var_non_special);
3784 defsubr (&Slet);
3785 defsubr (&SletX);
3786 defsubr (&Swhile);
3787 defsubr (&Smacroexpand);
3788 defsubr (&Scatch);
3789 defsubr (&Sthrow);
3790 defsubr (&Sunwind_protect);
3791 defsubr (&Scondition_case);
3792 defsubr (&Ssignal);
3793 defsubr (&Scommandp);
3794 defsubr (&Sautoload);
3795 defsubr (&Sautoload_do_load);
3796 defsubr (&Seval);
3797 defsubr (&Sapply);
3798 defsubr (&Sfuncall);
3799 defsubr (&Srun_hooks);
3800 defsubr (&Srun_hook_with_args);
3801 defsubr (&Srun_hook_with_args_until_success);
3802 defsubr (&Srun_hook_with_args_until_failure);
3803 defsubr (&Srun_hook_wrapped);
3804 defsubr (&Sfetch_bytecode);
3805 defsubr (&Sbacktrace_debug);
3806 defsubr (&Sbacktrace);
3807 defsubr (&Sbacktrace_frame);
3808 defsubr (&Sbacktrace_eval);
3809 defsubr (&Sbacktrace__locals);
3810 defsubr (&Sspecial_variable_p);
3811 defsubr (&Sfunctionp);