custom-buffer-style doc fix
[emacs.git] / src / eval.c
blobfe6460d53bbac9093c136ea55177f8c47f01868d
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2016 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 (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #include "commands.h"
28 #include "keyboard.h"
29 #include "dispextern.h"
30 #include "buffer.h"
32 /* Chain of condition and catch handlers currently in effect. */
34 struct handler *handlerlist;
36 /* Non-nil means record all fset's and provide's, to be undone
37 if the file being autoloaded is not fully loaded.
38 They are recorded by being consed onto the front of Vautoload_queue:
39 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
41 Lisp_Object Vautoload_queue;
43 /* This holds either the symbol `run-hooks' or nil.
44 It is nil at an early stage of startup, and when Emacs
45 is shutting down. */
46 Lisp_Object Vrun_hooks;
48 /* Current number of specbindings allocated in specpdl, not counting
49 the dummy entry specpdl[-1]. */
51 ptrdiff_t specpdl_size;
53 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
54 only so that its address can be taken. */
56 union specbinding *specpdl;
58 /* Pointer to first unused element in specpdl. */
60 union specbinding *specpdl_ptr;
62 /* Depth in Lisp evaluations and function calls. */
64 static EMACS_INT lisp_eval_depth;
66 /* The value of num_nonmacro_input_events as of the last time we
67 started to enter the debugger. If we decide to enter the debugger
68 again when this is still equal to num_nonmacro_input_events, then we
69 know that the debugger itself has an error, and we should just
70 signal the error instead of entering an infinite loop of debugger
71 invocations. */
73 static EMACS_INT when_entered_debugger;
75 /* The function from which the last `signal' was called. Set in
76 Fsignal. */
77 /* FIXME: We should probably get rid of this! */
78 Lisp_Object Vsignaling_function;
80 /* If non-nil, Lisp code must not be run since some part of Emacs is in
81 an inconsistent state. Currently unused. */
82 Lisp_Object inhibit_lisp_code;
84 /* These would ordinarily be static, but they need to be visible to GDB. */
85 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
86 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
87 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
88 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
89 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
91 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
92 static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
94 static Lisp_Object
95 specpdl_symbol (union specbinding *pdl)
97 eassert (pdl->kind >= SPECPDL_LET);
98 return pdl->let.symbol;
101 static Lisp_Object
102 specpdl_old_value (union specbinding *pdl)
104 eassert (pdl->kind >= SPECPDL_LET);
105 return pdl->let.old_value;
108 static void
109 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
111 eassert (pdl->kind >= SPECPDL_LET);
112 pdl->let.old_value = val;
115 static Lisp_Object
116 specpdl_where (union specbinding *pdl)
118 eassert (pdl->kind > SPECPDL_LET);
119 return pdl->let.where;
122 static Lisp_Object
123 specpdl_arg (union specbinding *pdl)
125 eassert (pdl->kind == SPECPDL_UNWIND);
126 return pdl->unwind.arg;
129 Lisp_Object
130 backtrace_function (union specbinding *pdl)
132 eassert (pdl->kind == SPECPDL_BACKTRACE);
133 return pdl->bt.function;
136 static ptrdiff_t
137 backtrace_nargs (union specbinding *pdl)
139 eassert (pdl->kind == SPECPDL_BACKTRACE);
140 return pdl->bt.nargs;
143 Lisp_Object *
144 backtrace_args (union specbinding *pdl)
146 eassert (pdl->kind == SPECPDL_BACKTRACE);
147 return pdl->bt.args;
150 static bool
151 backtrace_debug_on_exit (union specbinding *pdl)
153 eassert (pdl->kind == SPECPDL_BACKTRACE);
154 return pdl->bt.debug_on_exit;
157 /* Functions to modify slots of backtrace records. */
159 static void
160 set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
162 eassert (pdl->kind == SPECPDL_BACKTRACE);
163 pdl->bt.args = args;
164 pdl->bt.nargs = nargs;
167 static void
168 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
170 eassert (pdl->kind == SPECPDL_BACKTRACE);
171 pdl->bt.debug_on_exit = doe;
174 /* Helper functions to scan the backtrace. */
176 bool
177 backtrace_p (union specbinding *pdl)
178 { return pdl >= specpdl; }
180 union specbinding *
181 backtrace_top (void)
183 union specbinding *pdl = specpdl_ptr - 1;
184 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
185 pdl--;
186 return pdl;
189 union specbinding *
190 backtrace_next (union specbinding *pdl)
192 pdl--;
193 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
194 pdl--;
195 return pdl;
198 /* Return a pointer to somewhere near the top of the C stack. */
199 void *
200 near_C_stack_top (void)
202 return backtrace_args (backtrace_top ());
205 void
206 init_eval_once (void)
208 enum { size = 50 };
209 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
210 specpdl_size = size;
211 specpdl = specpdl_ptr = pdlvec + 1;
212 /* Don't forget to update docs (lispref node "Local Variables"). */
213 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
214 max_lisp_eval_depth = 800;
216 Vrun_hooks = Qnil;
219 static struct handler handlerlist_sentinel;
221 void
222 init_eval (void)
224 byte_stack_list = 0;
225 specpdl_ptr = specpdl;
226 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
227 This is important since handlerlist->nextfree holds the freelist
228 which would otherwise leak every time we unwind back to top-level. */
229 handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
230 struct handler *c = push_handler (Qunbound, CATCHER);
231 eassert (c == &handlerlist_sentinel);
232 handlerlist_sentinel.nextfree = NULL;
233 handlerlist_sentinel.next = NULL;
235 Vquit_flag = Qnil;
236 debug_on_next_call = 0;
237 lisp_eval_depth = 0;
238 /* This is less than the initial value of num_nonmacro_input_events. */
239 when_entered_debugger = -1;
242 /* Unwind-protect function used by call_debugger. */
244 static void
245 restore_stack_limits (Lisp_Object data)
247 max_specpdl_size = XINT (XCAR (data));
248 max_lisp_eval_depth = XINT (XCDR (data));
251 static void grow_specpdl (void);
253 /* Call the Lisp debugger, giving it argument ARG. */
255 Lisp_Object
256 call_debugger (Lisp_Object arg)
258 bool debug_while_redisplaying;
259 ptrdiff_t count = SPECPDL_INDEX ();
260 Lisp_Object val;
261 EMACS_INT old_depth = max_lisp_eval_depth;
262 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
263 EMACS_INT old_max = max (max_specpdl_size, count);
265 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
266 max_lisp_eval_depth = lisp_eval_depth + 40;
268 /* While debugging Bug#16603, previous value of 100 was found
269 too small to avoid specpdl overflow in the debugger itself. */
270 if (max_specpdl_size - 200 < count)
271 max_specpdl_size = count + 200;
273 if (old_max == count)
275 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
276 specpdl_ptr--;
277 grow_specpdl ();
280 /* Restore limits after leaving the debugger. */
281 record_unwind_protect (restore_stack_limits,
282 Fcons (make_number (old_max),
283 make_number (old_depth)));
285 #ifdef HAVE_WINDOW_SYSTEM
286 if (display_hourglass_p)
287 cancel_hourglass ();
288 #endif
290 debug_on_next_call = 0;
291 when_entered_debugger = num_nonmacro_input_events;
293 /* Resetting redisplaying_p to 0 makes sure that debug output is
294 displayed if the debugger is invoked during redisplay. */
295 debug_while_redisplaying = redisplaying_p;
296 redisplaying_p = 0;
297 specbind (intern ("debugger-may-continue"),
298 debug_while_redisplaying ? Qnil : Qt);
299 specbind (Qinhibit_redisplay, Qnil);
300 specbind (Qinhibit_debugger, Qt);
302 #if 0 /* Binding this prevents execution of Lisp code during
303 redisplay, which necessarily leads to display problems. */
304 specbind (Qinhibit_eval_during_redisplay, Qt);
305 #endif
307 val = apply1 (Vdebugger, arg);
309 /* Interrupting redisplay and resuming it later is not safe under
310 all circumstances. So, when the debugger returns, abort the
311 interrupted redisplay by going back to the top-level. */
312 if (debug_while_redisplaying)
313 Ftop_level ();
315 return unbind_to (count, val);
318 static void
319 do_debug_on_call (Lisp_Object code, ptrdiff_t count)
321 debug_on_next_call = 0;
322 set_backtrace_debug_on_exit (specpdl + count, true);
323 call_debugger (list1 (code));
326 /* NOTE!!! Every function that can call EVAL must protect its args
327 and temporaries from garbage collection while it needs them.
328 The definition of `For' shows what you have to do. */
330 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
331 doc: /* Eval args until one of them yields non-nil, then return that value.
332 The remaining args are not evalled at all.
333 If all args return nil, return nil.
334 usage: (or CONDITIONS...) */)
335 (Lisp_Object args)
337 Lisp_Object val = Qnil;
339 while (CONSP (args))
341 val = eval_sub (XCAR (args));
342 if (!NILP (val))
343 break;
344 args = XCDR (args);
347 return val;
350 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
351 doc: /* Eval args until one of them yields nil, then return nil.
352 The remaining args are not evalled at all.
353 If no arg yields nil, return the last arg's value.
354 usage: (and CONDITIONS...) */)
355 (Lisp_Object args)
357 Lisp_Object val = Qt;
359 while (CONSP (args))
361 val = eval_sub (XCAR (args));
362 if (NILP (val))
363 break;
364 args = XCDR (args);
367 return val;
370 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
371 doc: /* If COND yields non-nil, do THEN, else do ELSE...
372 Returns the value of THEN or the value of the last of the ELSE's.
373 THEN must be one expression, but ELSE... can be zero or more expressions.
374 If COND yields nil, and there are no ELSE's, the value is nil.
375 usage: (if COND THEN ELSE...) */)
376 (Lisp_Object args)
378 Lisp_Object cond;
380 cond = eval_sub (XCAR (args));
382 if (!NILP (cond))
383 return eval_sub (Fcar (XCDR (args)));
384 return Fprogn (XCDR (XCDR (args)));
387 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
388 doc: /* Try each clause until one succeeds.
389 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
390 and, if the value is non-nil, this clause succeeds:
391 then the expressions in BODY are evaluated and the last one's
392 value is the value of the cond-form.
393 If a clause has one element, as in (CONDITION), then the cond-form
394 returns CONDITION's value, if that is non-nil.
395 If no clause succeeds, cond returns nil.
396 usage: (cond CLAUSES...) */)
397 (Lisp_Object args)
399 Lisp_Object val = args;
401 while (CONSP (args))
403 Lisp_Object clause = XCAR (args);
404 val = eval_sub (Fcar (clause));
405 if (!NILP (val))
407 if (!NILP (XCDR (clause)))
408 val = Fprogn (XCDR (clause));
409 break;
411 args = XCDR (args);
414 return val;
417 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
418 doc: /* Eval BODY forms sequentially and return value of last one.
419 usage: (progn BODY...) */)
420 (Lisp_Object body)
422 Lisp_Object val = Qnil;
424 while (CONSP (body))
426 val = eval_sub (XCAR (body));
427 body = XCDR (body);
430 return val;
433 /* Evaluate BODY sequentially, discarding its value. Suitable for
434 record_unwind_protect. */
436 void
437 unwind_body (Lisp_Object body)
439 Fprogn (body);
442 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
443 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
444 The value of FIRST is saved during the evaluation of the remaining args,
445 whose values are discarded.
446 usage: (prog1 FIRST BODY...) */)
447 (Lisp_Object args)
449 Lisp_Object val;
450 Lisp_Object args_left;
452 args_left = args;
453 val = args;
455 val = eval_sub (XCAR (args_left));
456 while (CONSP (args_left = XCDR (args_left)))
457 eval_sub (XCAR (args_left));
459 return val;
462 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
463 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
464 The value of FORM2 is saved during the evaluation of the
465 remaining args, whose values are discarded.
466 usage: (prog2 FORM1 FORM2 BODY...) */)
467 (Lisp_Object args)
469 eval_sub (XCAR (args));
470 return Fprog1 (XCDR (args));
473 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
474 doc: /* Set each SYM to the value of its VAL.
475 The symbols SYM are variables; they are literal (not evaluated).
476 The values VAL are expressions; they are evaluated.
477 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
478 The second VAL is not computed until after the first SYM is set, and so on;
479 each VAL can use the new value of variables set earlier in the `setq'.
480 The return value of the `setq' form is the value of the last VAL.
481 usage: (setq [SYM VAL]...) */)
482 (Lisp_Object args)
484 Lisp_Object val, sym, lex_binding;
486 val = args;
487 if (CONSP (args))
489 Lisp_Object args_left = args;
490 Lisp_Object numargs = Flength (args);
492 if (XINT (numargs) & 1)
493 xsignal2 (Qwrong_number_of_arguments, Qsetq, numargs);
497 val = eval_sub (Fcar (XCDR (args_left)));
498 sym = XCAR (args_left);
500 /* Like for eval_sub, we do not check declared_special here since
501 it's been done when let-binding. */
502 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
503 && SYMBOLP (sym)
504 && !NILP (lex_binding
505 = Fassq (sym, Vinternal_interpreter_environment)))
506 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
507 else
508 Fset (sym, val); /* SYM is dynamically bound. */
510 args_left = Fcdr (XCDR (args_left));
512 while (CONSP (args_left));
515 return val;
518 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
519 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
520 Warning: `quote' does not construct its return value, but just returns
521 the value that was pre-constructed by the Lisp reader (see info node
522 `(elisp)Printed Representation').
523 This means that \\='(a . b) is not identical to (cons \\='a \\='b): the former
524 does not cons. Quoting should be reserved for constants that will
525 never be modified by side-effects, unless you like self-modifying code.
526 See the common pitfall in info node `(elisp)Rearrangement' for an example
527 of unexpected results when a quoted object is modified.
528 usage: (quote ARG) */)
529 (Lisp_Object args)
531 if (CONSP (XCDR (args)))
532 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
533 return XCAR (args);
536 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
537 doc: /* Like `quote', but preferred for objects which are functions.
538 In byte compilation, `function' causes its argument to be compiled.
539 `quote' cannot do that.
540 usage: (function ARG) */)
541 (Lisp_Object args)
543 Lisp_Object quoted = XCAR (args);
545 if (CONSP (XCDR (args)))
546 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
548 if (!NILP (Vinternal_interpreter_environment)
549 && CONSP (quoted)
550 && EQ (XCAR (quoted), Qlambda))
551 { /* This is a lambda expression within a lexical environment;
552 return an interpreted closure instead of a simple lambda. */
553 Lisp_Object cdr = XCDR (quoted);
554 Lisp_Object tmp = cdr;
555 if (CONSP (tmp)
556 && (tmp = XCDR (tmp), CONSP (tmp))
557 && (tmp = XCAR (tmp), CONSP (tmp))
558 && (EQ (QCdocumentation, XCAR (tmp))))
559 { /* Handle the special (:documentation <form>) to build the docstring
560 dynamically. */
561 Lisp_Object docstring = eval_sub (Fcar (XCDR (tmp)));
562 CHECK_STRING (docstring);
563 cdr = Fcons (XCAR (cdr), Fcons (docstring, XCDR (XCDR (cdr))));
565 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
566 cdr));
568 else
569 /* Simply quote the argument. */
570 return quoted;
574 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
575 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
576 Aliased variables always have the same value; setting one sets the other.
577 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
578 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
579 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
580 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
581 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
582 The return value is BASE-VARIABLE. */)
583 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
585 struct Lisp_Symbol *sym;
587 CHECK_SYMBOL (new_alias);
588 CHECK_SYMBOL (base_variable);
590 sym = XSYMBOL (new_alias);
592 if (sym->constant)
593 /* Not sure why, but why not? */
594 error ("Cannot make a constant an alias");
596 switch (sym->redirect)
598 case SYMBOL_FORWARDED:
599 error ("Cannot make an internal variable an alias");
600 case SYMBOL_LOCALIZED:
601 error ("Don't know how to make a localized variable an alias");
602 case SYMBOL_PLAINVAL:
603 case SYMBOL_VARALIAS:
604 break;
605 default:
606 emacs_abort ();
609 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
610 If n_a is bound, but b_v is not, set the value of b_v to n_a,
611 so that old-code that affects n_a before the aliasing is setup
612 still works. */
613 if (NILP (Fboundp (base_variable)))
614 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
617 union specbinding *p;
619 for (p = specpdl_ptr; p > specpdl; )
620 if ((--p)->kind >= SPECPDL_LET
621 && (EQ (new_alias, specpdl_symbol (p))))
622 error ("Don't know how to make a let-bound variable an alias");
625 sym->declared_special = 1;
626 XSYMBOL (base_variable)->declared_special = 1;
627 sym->redirect = SYMBOL_VARALIAS;
628 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
629 sym->constant = SYMBOL_CONSTANT_P (base_variable);
630 LOADHIST_ATTACH (new_alias);
631 /* Even if docstring is nil: remove old docstring. */
632 Fput (new_alias, Qvariable_documentation, docstring);
634 return base_variable;
637 static union specbinding *
638 default_toplevel_binding (Lisp_Object symbol)
640 union specbinding *binding = NULL;
641 union specbinding *pdl = specpdl_ptr;
642 while (pdl > specpdl)
644 switch ((--pdl)->kind)
646 case SPECPDL_LET_DEFAULT:
647 case SPECPDL_LET:
648 if (EQ (specpdl_symbol (pdl), symbol))
649 binding = pdl;
650 break;
652 case SPECPDL_UNWIND:
653 case SPECPDL_UNWIND_PTR:
654 case SPECPDL_UNWIND_INT:
655 case SPECPDL_UNWIND_VOID:
656 case SPECPDL_BACKTRACE:
657 case SPECPDL_LET_LOCAL:
658 break;
660 default:
661 emacs_abort ();
664 return binding;
667 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
668 doc: /* Return SYMBOL's toplevel default value.
669 "Toplevel" means outside of any let binding. */)
670 (Lisp_Object symbol)
672 union specbinding *binding = default_toplevel_binding (symbol);
673 Lisp_Object value
674 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
675 if (!EQ (value, Qunbound))
676 return value;
677 xsignal1 (Qvoid_variable, symbol);
680 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
681 Sset_default_toplevel_value, 2, 2, 0,
682 doc: /* Set SYMBOL's toplevel default value to VALUE.
683 "Toplevel" means outside of any let binding. */)
684 (Lisp_Object symbol, Lisp_Object value)
686 union specbinding *binding = default_toplevel_binding (symbol);
687 if (binding)
688 set_specpdl_old_value (binding, value);
689 else
690 Fset_default (symbol, value);
691 return Qnil;
694 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
695 doc: /* Define SYMBOL as a variable, and return SYMBOL.
696 You are not required to define a variable in order to use it, but
697 defining it lets you supply an initial value and documentation, which
698 can be referred to by the Emacs help facilities and other programming
699 tools. The `defvar' form also declares the variable as \"special\",
700 so that it is always dynamically bound even if `lexical-binding' is t.
702 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
703 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
704 default value is what is set; buffer-local values are not affected.
705 If INITVALUE is missing, SYMBOL's value is not set.
707 If SYMBOL has a local binding, then this form affects the local
708 binding. This is usually not what you want. Thus, if you need to
709 load a file defining variables, with this form or with `defconst' or
710 `defcustom', you should always load that file _outside_ any bindings
711 for these variables. (`defconst' and `defcustom' behave similarly in
712 this respect.)
714 The optional argument DOCSTRING is a documentation string for the
715 variable.
717 To define a user option, use `defcustom' instead of `defvar'.
718 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
719 (Lisp_Object args)
721 Lisp_Object sym, tem, tail;
723 sym = XCAR (args);
724 tail = XCDR (args);
726 if (CONSP (tail))
728 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
729 error ("Too many arguments");
731 tem = Fdefault_boundp (sym);
733 /* Do it before evaluating the initial value, for self-references. */
734 XSYMBOL (sym)->declared_special = 1;
736 if (NILP (tem))
737 Fset_default (sym, eval_sub (XCAR (tail)));
738 else
739 { /* Check if there is really a global binding rather than just a let
740 binding that shadows the global unboundness of the var. */
741 union specbinding *binding = default_toplevel_binding (sym);
742 if (binding && EQ (specpdl_old_value (binding), Qunbound))
744 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
747 tail = XCDR (tail);
748 tem = Fcar (tail);
749 if (!NILP (tem))
751 if (!NILP (Vpurify_flag))
752 tem = Fpurecopy (tem);
753 Fput (sym, Qvariable_documentation, tem);
755 LOADHIST_ATTACH (sym);
757 else if (!NILP (Vinternal_interpreter_environment)
758 && !XSYMBOL (sym)->declared_special)
759 /* A simple (defvar foo) with lexical scoping does "nothing" except
760 declare that var to be dynamically scoped *locally* (i.e. within
761 the current file or let-block). */
762 Vinternal_interpreter_environment
763 = Fcons (sym, Vinternal_interpreter_environment);
764 else
766 /* Simple (defvar <var>) should not count as a definition at all.
767 It could get in the way of other definitions, and unloading this
768 package could try to make the variable unbound. */
771 return sym;
774 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
775 doc: /* Define SYMBOL as a constant variable.
776 This declares that neither programs nor users should ever change the
777 value. This constancy is not actually enforced by Emacs Lisp, but
778 SYMBOL is marked as a special variable so that it is never lexically
779 bound.
781 The `defconst' form always sets the value of SYMBOL to the result of
782 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
783 what is set; buffer-local values are not affected. If SYMBOL has a
784 local binding, then this form sets the local binding's value.
785 However, you should normally not make local bindings for variables
786 defined with this form.
788 The optional DOCSTRING specifies the variable's documentation string.
789 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
790 (Lisp_Object args)
792 Lisp_Object sym, tem;
794 sym = XCAR (args);
795 if (CONSP (Fcdr (XCDR (XCDR (args)))))
796 error ("Too many arguments");
798 tem = eval_sub (Fcar (XCDR (args)));
799 if (!NILP (Vpurify_flag))
800 tem = Fpurecopy (tem);
801 Fset_default (sym, tem);
802 XSYMBOL (sym)->declared_special = 1;
803 tem = Fcar (XCDR (XCDR (args)));
804 if (!NILP (tem))
806 if (!NILP (Vpurify_flag))
807 tem = Fpurecopy (tem);
808 Fput (sym, Qvariable_documentation, tem);
810 Fput (sym, Qrisky_local_variable, Qt);
811 LOADHIST_ATTACH (sym);
812 return sym;
815 /* Make SYMBOL lexically scoped. */
816 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
817 Smake_var_non_special, 1, 1, 0,
818 doc: /* Internal function. */)
819 (Lisp_Object symbol)
821 CHECK_SYMBOL (symbol);
822 XSYMBOL (symbol)->declared_special = 0;
823 return Qnil;
827 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
828 doc: /* Bind variables according to VARLIST then eval BODY.
829 The value of the last form in BODY is returned.
830 Each element of VARLIST is a symbol (which is bound to nil)
831 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
832 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
833 usage: (let* VARLIST BODY...) */)
834 (Lisp_Object args)
836 Lisp_Object varlist, var, val, elt, lexenv;
837 ptrdiff_t count = SPECPDL_INDEX ();
839 lexenv = Vinternal_interpreter_environment;
841 varlist = XCAR (args);
842 while (CONSP (varlist))
844 QUIT;
846 elt = XCAR (varlist);
847 if (SYMBOLP (elt))
849 var = elt;
850 val = Qnil;
852 else if (! NILP (Fcdr (Fcdr (elt))))
853 signal_error ("`let' bindings can have only one value-form", elt);
854 else
856 var = Fcar (elt);
857 val = eval_sub (Fcar (Fcdr (elt)));
860 if (!NILP (lexenv) && SYMBOLP (var)
861 && !XSYMBOL (var)->declared_special
862 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
863 /* Lexically bind VAR by adding it to the interpreter's binding
864 alist. */
866 Lisp_Object newenv
867 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
868 if (EQ (Vinternal_interpreter_environment, lexenv))
869 /* Save the old lexical environment on the specpdl stack,
870 but only for the first lexical binding, since we'll never
871 need to revert to one of the intermediate ones. */
872 specbind (Qinternal_interpreter_environment, newenv);
873 else
874 Vinternal_interpreter_environment = newenv;
876 else
877 specbind (var, val);
879 varlist = XCDR (varlist);
882 val = Fprogn (XCDR (args));
883 return unbind_to (count, val);
886 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
887 doc: /* Bind variables according to VARLIST then eval BODY.
888 The value of the last form in BODY is returned.
889 Each element of VARLIST is a symbol (which is bound to nil)
890 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
891 All the VALUEFORMs are evalled before any symbols are bound.
892 usage: (let VARLIST BODY...) */)
893 (Lisp_Object args)
895 Lisp_Object *temps, tem, lexenv;
896 Lisp_Object elt, varlist;
897 ptrdiff_t count = SPECPDL_INDEX ();
898 ptrdiff_t argnum;
899 USE_SAFE_ALLOCA;
901 varlist = XCAR (args);
903 /* Make space to hold the values to give the bound variables. */
904 elt = Flength (varlist);
905 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
907 /* Compute the values and store them in `temps'. */
909 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
911 QUIT;
912 elt = XCAR (varlist);
913 if (SYMBOLP (elt))
914 temps [argnum++] = Qnil;
915 else if (! NILP (Fcdr (Fcdr (elt))))
916 signal_error ("`let' bindings can have only one value-form", elt);
917 else
918 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
921 lexenv = Vinternal_interpreter_environment;
923 varlist = XCAR (args);
924 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
926 Lisp_Object var;
928 elt = XCAR (varlist);
929 var = SYMBOLP (elt) ? elt : Fcar (elt);
930 tem = temps[argnum++];
932 if (!NILP (lexenv) && SYMBOLP (var)
933 && !XSYMBOL (var)->declared_special
934 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
935 /* Lexically bind VAR by adding it to the lexenv alist. */
936 lexenv = Fcons (Fcons (var, tem), lexenv);
937 else
938 /* Dynamically bind VAR. */
939 specbind (var, tem);
942 if (!EQ (lexenv, Vinternal_interpreter_environment))
943 /* Instantiate a new lexical environment. */
944 specbind (Qinternal_interpreter_environment, lexenv);
946 elt = Fprogn (XCDR (args));
947 SAFE_FREE ();
948 return unbind_to (count, elt);
951 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
952 doc: /* If TEST yields non-nil, eval BODY... and repeat.
953 The order of execution is thus TEST, BODY, TEST, BODY and so on
954 until TEST returns nil.
955 usage: (while TEST BODY...) */)
956 (Lisp_Object args)
958 Lisp_Object test, body;
960 test = XCAR (args);
961 body = XCDR (args);
962 while (!NILP (eval_sub (test)))
964 QUIT;
965 Fprogn (body);
968 return Qnil;
971 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
972 doc: /* Return result of expanding macros at top level of FORM.
973 If FORM is not a macro call, it is returned unchanged.
974 Otherwise, the macro is expanded and the expansion is considered
975 in place of FORM. When a non-macro-call results, it is returned.
977 The second optional arg ENVIRONMENT specifies an environment of macro
978 definitions to shadow the loaded ones for use in file byte-compilation. */)
979 (Lisp_Object form, Lisp_Object environment)
981 /* With cleanups from Hallvard Furuseth. */
982 register Lisp_Object expander, sym, def, tem;
984 while (1)
986 /* Come back here each time we expand a macro call,
987 in case it expands into another macro call. */
988 if (!CONSP (form))
989 break;
990 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
991 def = sym = XCAR (form);
992 tem = Qnil;
993 /* Trace symbols aliases to other symbols
994 until we get a symbol that is not an alias. */
995 while (SYMBOLP (def))
997 QUIT;
998 sym = def;
999 tem = Fassq (sym, environment);
1000 if (NILP (tem))
1002 def = XSYMBOL (sym)->function;
1003 if (!NILP (def))
1004 continue;
1006 break;
1008 /* Right now TEM is the result from SYM in ENVIRONMENT,
1009 and if TEM is nil then DEF is SYM's function definition. */
1010 if (NILP (tem))
1012 /* SYM is not mentioned in ENVIRONMENT.
1013 Look at its function definition. */
1014 def = Fautoload_do_load (def, sym, Qmacro);
1015 if (!CONSP (def))
1016 /* Not defined or definition not suitable. */
1017 break;
1018 if (!EQ (XCAR (def), Qmacro))
1019 break;
1020 else expander = XCDR (def);
1022 else
1024 expander = XCDR (tem);
1025 if (NILP (expander))
1026 break;
1029 Lisp_Object newform = apply1 (expander, XCDR (form));
1030 if (EQ (form, newform))
1031 break;
1032 else
1033 form = newform;
1036 return form;
1039 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1040 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1041 TAG is evalled to get the tag to use; it must not be nil.
1043 Then the BODY is executed.
1044 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1045 If no throw happens, `catch' returns the value of the last BODY form.
1046 If a throw happens, it specifies the value to return from `catch'.
1047 usage: (catch TAG BODY...) */)
1048 (Lisp_Object args)
1050 Lisp_Object tag = eval_sub (XCAR (args));
1051 return internal_catch (tag, Fprogn, XCDR (args));
1054 /* Assert that E is true, as a comment only. Use this instead of
1055 eassert (E) when E contains variables that might be clobbered by a
1056 longjmp. */
1058 #define clobbered_eassert(E) ((void) 0)
1060 /* Set up a catch, then call C function FUNC on argument ARG.
1061 FUNC should return a Lisp_Object.
1062 This is how catches are done from within C code. */
1064 Lisp_Object
1065 internal_catch (Lisp_Object tag,
1066 Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1068 /* This structure is made part of the chain `catchlist'. */
1069 struct handler *c = push_handler (tag, CATCHER);
1071 /* Call FUNC. */
1072 if (! sys_setjmp (c->jmp))
1074 Lisp_Object val = func (arg);
1075 clobbered_eassert (handlerlist == c);
1076 handlerlist = handlerlist->next;
1077 return val;
1079 else
1080 { /* Throw works by a longjmp that comes right here. */
1081 Lisp_Object val = handlerlist->val;
1082 clobbered_eassert (handlerlist == c);
1083 handlerlist = handlerlist->next;
1084 return val;
1088 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1089 jump to that CATCH, returning VALUE as the value of that catch.
1091 This is the guts of Fthrow and Fsignal; they differ only in the way
1092 they choose the catch tag to throw to. A catch tag for a
1093 condition-case form has a TAG of Qnil.
1095 Before each catch is discarded, unbind all special bindings and
1096 execute all unwind-protect clauses made above that catch. Unwind
1097 the handler stack as we go, so that the proper handlers are in
1098 effect for each unwind-protect clause we run. At the end, restore
1099 some static info saved in CATCH, and longjmp to the location
1100 specified there.
1102 This is used for correct unwinding in Fthrow and Fsignal. */
1104 static _Noreturn void
1105 unwind_to_catch (struct handler *catch, Lisp_Object value)
1107 bool last_time;
1109 eassert (catch->next);
1111 /* Save the value in the tag. */
1112 catch->val = value;
1114 /* Restore certain special C variables. */
1115 set_poll_suppress_count (catch->poll_suppress_count);
1116 unblock_input_to (catch->interrupt_input_blocked);
1117 immediate_quit = 0;
1121 /* Unwind the specpdl stack, and then restore the proper set of
1122 handlers. */
1123 unbind_to (handlerlist->pdlcount, Qnil);
1124 last_time = handlerlist == catch;
1125 if (! last_time)
1126 handlerlist = handlerlist->next;
1128 while (! last_time);
1130 eassert (handlerlist == catch);
1132 byte_stack_list = catch->byte_stack;
1133 lisp_eval_depth = catch->lisp_eval_depth;
1135 sys_longjmp (catch->jmp, 1);
1138 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1139 doc: /* Throw to the catch for TAG and return VALUE from it.
1140 Both TAG and VALUE are evalled. */
1141 attributes: noreturn)
1142 (register Lisp_Object tag, Lisp_Object value)
1144 struct handler *c;
1146 if (!NILP (tag))
1147 for (c = handlerlist; c; c = c->next)
1149 if (c->type == CATCHER_ALL)
1150 unwind_to_catch (c, Fcons (tag, value));
1151 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1152 unwind_to_catch (c, value);
1154 xsignal2 (Qno_catch, tag, value);
1158 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1159 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1160 If BODYFORM completes normally, its value is returned
1161 after executing the UNWINDFORMS.
1162 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1163 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1164 (Lisp_Object args)
1166 Lisp_Object val;
1167 ptrdiff_t count = SPECPDL_INDEX ();
1169 record_unwind_protect (unwind_body, XCDR (args));
1170 val = eval_sub (XCAR (args));
1171 return unbind_to (count, val);
1174 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1175 doc: /* Regain control when an error is signaled.
1176 Executes BODYFORM and returns its value if no error happens.
1177 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1178 where the BODY is made of Lisp expressions.
1180 A handler is applicable to an error
1181 if CONDITION-NAME is one of the error's condition names.
1182 If an error happens, the first applicable handler is run.
1184 The car of a handler may be a list of condition names instead of a
1185 single condition name; then it handles all of them. If the special
1186 condition name `debug' is present in this list, it allows another
1187 condition in the list to run the debugger if `debug-on-error' and the
1188 other usual mechanisms says it should (otherwise, `condition-case'
1189 suppresses the debugger).
1191 When a handler handles an error, control returns to the `condition-case'
1192 and it executes the handler's BODY...
1193 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1194 \(If VAR is nil, the handler can't access that information.)
1195 Then the value of the last BODY form is returned from the `condition-case'
1196 expression.
1198 See also the function `signal' for more info.
1199 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1200 (Lisp_Object args)
1202 Lisp_Object var = XCAR (args);
1203 Lisp_Object bodyform = XCAR (XCDR (args));
1204 Lisp_Object handlers = XCDR (XCDR (args));
1206 return internal_lisp_condition_case (var, bodyform, handlers);
1209 /* Like Fcondition_case, but the args are separate
1210 rather than passed in a list. Used by Fbyte_code. */
1212 Lisp_Object
1213 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1214 Lisp_Object handlers)
1216 Lisp_Object val;
1217 struct handler *oldhandlerlist = handlerlist;
1218 int clausenb = 0;
1220 CHECK_SYMBOL (var);
1222 for (val = handlers; CONSP (val); val = XCDR (val))
1224 Lisp_Object tem = XCAR (val);
1225 clausenb++;
1226 if (! (NILP (tem)
1227 || (CONSP (tem)
1228 && (SYMBOLP (XCAR (tem))
1229 || CONSP (XCAR (tem))))))
1230 error ("Invalid condition handler: %s",
1231 SDATA (Fprin1_to_string (tem, Qt)));
1234 { /* The first clause is the one that should be checked first, so it should
1235 be added to handlerlist last. So we build in `clauses' a table that
1236 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1237 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1238 if (MAX_ALLOCA / word_size < clausenb)
1239 memory_full (SIZE_MAX);
1240 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1241 Lisp_Object *volatile clauses_volatile = clauses;
1242 int i = clausenb;
1243 for (val = handlers; CONSP (val); val = XCDR (val))
1244 clauses[--i] = XCAR (val);
1245 for (i = 0; i < clausenb; i++)
1247 Lisp_Object clause = clauses[i];
1248 Lisp_Object condition = CONSP (clause) ? XCAR (clause) : Qnil;
1249 if (!CONSP (condition))
1250 condition = Fcons (condition, Qnil);
1251 struct handler *c = push_handler (condition, CONDITION_CASE);
1252 if (sys_setjmp (c->jmp))
1254 ptrdiff_t count = SPECPDL_INDEX ();
1255 Lisp_Object val = handlerlist->val;
1256 Lisp_Object *chosen_clause = clauses_volatile;
1257 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1258 chosen_clause++;
1259 handlerlist = oldhandlerlist;
1260 if (!NILP (var))
1262 if (!NILP (Vinternal_interpreter_environment))
1263 specbind (Qinternal_interpreter_environment,
1264 Fcons (Fcons (var, val),
1265 Vinternal_interpreter_environment));
1266 else
1267 specbind (var, val);
1269 val = Fprogn (XCDR (*chosen_clause));
1270 /* Note that this just undoes the binding of var; whoever
1271 longjumped to us unwound the stack to c.pdlcount before
1272 throwing. */
1273 if (!NILP (var))
1274 unbind_to (count, Qnil);
1275 return val;
1280 val = eval_sub (bodyform);
1281 handlerlist = oldhandlerlist;
1282 return val;
1285 /* Call the function BFUN with no arguments, catching errors within it
1286 according to HANDLERS. If there is an error, call HFUN with
1287 one argument which is the data that describes the error:
1288 (SIGNALNAME . DATA)
1290 HANDLERS can be a list of conditions to catch.
1291 If HANDLERS is Qt, catch all errors.
1292 If HANDLERS is Qerror, catch all errors
1293 but allow the debugger to run if that is enabled. */
1295 Lisp_Object
1296 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1297 Lisp_Object (*hfun) (Lisp_Object))
1299 struct handler *c = push_handler (handlers, CONDITION_CASE);
1300 if (sys_setjmp (c->jmp))
1302 Lisp_Object val = handlerlist->val;
1303 clobbered_eassert (handlerlist == c);
1304 handlerlist = handlerlist->next;
1305 return hfun (val);
1307 else
1309 Lisp_Object val = bfun ();
1310 clobbered_eassert (handlerlist == c);
1311 handlerlist = handlerlist->next;
1312 return val;
1316 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1318 Lisp_Object
1319 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1320 Lisp_Object handlers,
1321 Lisp_Object (*hfun) (Lisp_Object))
1323 struct handler *c = push_handler (handlers, CONDITION_CASE);
1324 if (sys_setjmp (c->jmp))
1326 Lisp_Object val = handlerlist->val;
1327 clobbered_eassert (handlerlist == c);
1328 handlerlist = handlerlist->next;
1329 return hfun (val);
1331 else
1333 Lisp_Object val = bfun (arg);
1334 clobbered_eassert (handlerlist == c);
1335 handlerlist = handlerlist->next;
1336 return val;
1340 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1341 its arguments. */
1343 Lisp_Object
1344 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1345 Lisp_Object arg1,
1346 Lisp_Object arg2,
1347 Lisp_Object handlers,
1348 Lisp_Object (*hfun) (Lisp_Object))
1350 struct handler *c = push_handler (handlers, CONDITION_CASE);
1351 if (sys_setjmp (c->jmp))
1353 Lisp_Object val = handlerlist->val;
1354 clobbered_eassert (handlerlist == c);
1355 handlerlist = handlerlist->next;
1356 return hfun (val);
1358 else
1360 Lisp_Object val = bfun (arg1, arg2);
1361 clobbered_eassert (handlerlist == c);
1362 handlerlist = handlerlist->next;
1363 return val;
1367 /* Like internal_condition_case but call BFUN with NARGS as first,
1368 and ARGS as second argument. */
1370 Lisp_Object
1371 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1372 ptrdiff_t nargs,
1373 Lisp_Object *args,
1374 Lisp_Object handlers,
1375 Lisp_Object (*hfun) (Lisp_Object err,
1376 ptrdiff_t nargs,
1377 Lisp_Object *args))
1379 struct handler *c = push_handler (handlers, CONDITION_CASE);
1380 if (sys_setjmp (c->jmp))
1382 Lisp_Object val = handlerlist->val;
1383 clobbered_eassert (handlerlist == c);
1384 handlerlist = handlerlist->next;
1385 return hfun (val, nargs, args);
1387 else
1389 Lisp_Object val = bfun (nargs, args);
1390 clobbered_eassert (handlerlist == c);
1391 handlerlist = handlerlist->next;
1392 return val;
1396 struct handler *
1397 push_handler (Lisp_Object tag_ch_val, enum handlertype handlertype)
1399 struct handler *c = push_handler_nosignal (tag_ch_val, handlertype);
1400 if (!c)
1401 memory_full (sizeof *c);
1402 return c;
1405 struct handler *
1406 push_handler_nosignal (Lisp_Object tag_ch_val, enum handlertype handlertype)
1408 struct handler *c = handlerlist->nextfree;
1409 if (!c)
1411 c = malloc (sizeof *c);
1412 if (!c)
1413 return c;
1414 if (profiler_memory_running)
1415 malloc_probe (sizeof *c);
1416 c->nextfree = NULL;
1417 handlerlist->nextfree = c;
1419 c->type = handlertype;
1420 c->tag_or_ch = tag_ch_val;
1421 c->val = Qnil;
1422 c->next = handlerlist;
1423 c->lisp_eval_depth = lisp_eval_depth;
1424 c->pdlcount = SPECPDL_INDEX ();
1425 c->poll_suppress_count = poll_suppress_count;
1426 c->interrupt_input_blocked = interrupt_input_blocked;
1427 c->byte_stack = byte_stack_list;
1428 handlerlist = c;
1429 return c;
1433 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1434 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1435 Lisp_Object data);
1437 void
1438 process_quit_flag (void)
1440 Lisp_Object flag = Vquit_flag;
1441 Vquit_flag = Qnil;
1442 if (EQ (flag, Qkill_emacs))
1443 Fkill_emacs (Qnil);
1444 if (EQ (Vthrow_on_input, flag))
1445 Fthrow (Vthrow_on_input, Qt);
1446 Fsignal (Qquit, Qnil);
1449 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1450 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1451 This function does not return.
1453 An error symbol is a symbol with an `error-conditions' property
1454 that is a list of condition names.
1455 A handler for any of those names will get to handle this signal.
1456 The symbol `error' should normally be one of them.
1458 DATA should be a list. Its elements are printed as part of the error message.
1459 See Info anchor `(elisp)Definition of signal' for some details on how this
1460 error message is constructed.
1461 If the signal is handled, DATA is made available to the handler.
1462 See also the function `condition-case'. */)
1463 (Lisp_Object error_symbol, Lisp_Object data)
1465 /* When memory is full, ERROR-SYMBOL is nil,
1466 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1467 That is a special case--don't do this in other situations. */
1468 Lisp_Object conditions;
1469 Lisp_Object string;
1470 Lisp_Object real_error_symbol
1471 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1472 register Lisp_Object clause = Qnil;
1473 struct handler *h;
1475 immediate_quit = 0;
1476 abort_on_gc = 0;
1477 if (gc_in_progress || waiting_for_input)
1478 emacs_abort ();
1480 #if 0 /* rms: I don't know why this was here,
1481 but it is surely wrong for an error that is handled. */
1482 #ifdef HAVE_WINDOW_SYSTEM
1483 if (display_hourglass_p)
1484 cancel_hourglass ();
1485 #endif
1486 #endif
1488 /* This hook is used by edebug. */
1489 if (! NILP (Vsignal_hook_function)
1490 && ! NILP (error_symbol))
1492 /* Edebug takes care of restoring these variables when it exits. */
1493 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1494 max_lisp_eval_depth = lisp_eval_depth + 20;
1496 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1497 max_specpdl_size = SPECPDL_INDEX () + 40;
1499 call2 (Vsignal_hook_function, error_symbol, data);
1502 conditions = Fget (real_error_symbol, Qerror_conditions);
1504 /* Remember from where signal was called. Skip over the frame for
1505 `signal' itself. If a frame for `error' follows, skip that,
1506 too. Don't do this when ERROR_SYMBOL is nil, because that
1507 is a memory-full error. */
1508 Vsignaling_function = Qnil;
1509 if (!NILP (error_symbol))
1511 union specbinding *pdl = backtrace_next (backtrace_top ());
1512 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1513 pdl = backtrace_next (pdl);
1514 if (backtrace_p (pdl))
1515 Vsignaling_function = backtrace_function (pdl);
1518 for (h = handlerlist; h; h = h->next)
1520 if (h->type != CONDITION_CASE)
1521 continue;
1522 clause = find_handler_clause (h->tag_or_ch, conditions);
1523 if (!NILP (clause))
1524 break;
1527 if (/* Don't run the debugger for a memory-full error.
1528 (There is no room in memory to do that!) */
1529 !NILP (error_symbol)
1530 && (!NILP (Vdebug_on_signal)
1531 /* If no handler is present now, try to run the debugger. */
1532 || NILP (clause)
1533 /* A `debug' symbol in the handler list disables the normal
1534 suppression of the debugger. */
1535 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1536 /* Special handler that means "print a message and run debugger
1537 if requested". */
1538 || EQ (h->tag_or_ch, Qerror)))
1540 bool debugger_called
1541 = maybe_call_debugger (conditions, error_symbol, data);
1542 /* We can't return values to code which signaled an error, but we
1543 can continue code which has signaled a quit. */
1544 if (debugger_called && EQ (real_error_symbol, Qquit))
1545 return Qnil;
1548 if (!NILP (clause))
1550 Lisp_Object unwind_data
1551 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1553 unwind_to_catch (h, unwind_data);
1555 else
1557 if (handlerlist != &handlerlist_sentinel)
1558 /* FIXME: This will come right back here if there's no `top-level'
1559 catcher. A better solution would be to abort here, and instead
1560 add a catch-all condition handler so we never come here. */
1561 Fthrow (Qtop_level, Qt);
1564 if (! NILP (error_symbol))
1565 data = Fcons (error_symbol, data);
1567 string = Ferror_message_string (data);
1568 fatal ("%s", SDATA (string));
1571 /* Internal version of Fsignal that never returns.
1572 Used for anything but Qquit (which can return from Fsignal). */
1574 void
1575 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1577 Fsignal (error_symbol, data);
1578 emacs_abort ();
1581 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1583 void
1584 xsignal0 (Lisp_Object error_symbol)
1586 xsignal (error_symbol, Qnil);
1589 void
1590 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1592 xsignal (error_symbol, list1 (arg));
1595 void
1596 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1598 xsignal (error_symbol, list2 (arg1, arg2));
1601 void
1602 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1604 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1607 /* Signal `error' with message S, and additional arg ARG.
1608 If ARG is not a genuine list, make it a one-element list. */
1610 void
1611 signal_error (const char *s, Lisp_Object arg)
1613 Lisp_Object tortoise, hare;
1615 hare = tortoise = arg;
1616 while (CONSP (hare))
1618 hare = XCDR (hare);
1619 if (!CONSP (hare))
1620 break;
1622 hare = XCDR (hare);
1623 tortoise = XCDR (tortoise);
1625 if (EQ (hare, tortoise))
1626 break;
1629 if (!NILP (hare))
1630 arg = list1 (arg);
1632 xsignal (Qerror, Fcons (build_string (s), arg));
1636 /* Return true if LIST is a non-nil atom or
1637 a list containing one of CONDITIONS. */
1639 static bool
1640 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1642 if (NILP (list))
1643 return 0;
1644 if (! CONSP (list))
1645 return 1;
1647 while (CONSP (conditions))
1649 Lisp_Object this, tail;
1650 this = XCAR (conditions);
1651 for (tail = list; CONSP (tail); tail = XCDR (tail))
1652 if (EQ (XCAR (tail), this))
1653 return 1;
1654 conditions = XCDR (conditions);
1656 return 0;
1659 /* Return true if an error with condition-symbols CONDITIONS,
1660 and described by SIGNAL-DATA, should skip the debugger
1661 according to debugger-ignored-errors. */
1663 static bool
1664 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1666 Lisp_Object tail;
1667 bool first_string = 1;
1668 Lisp_Object error_message;
1670 error_message = Qnil;
1671 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1673 if (STRINGP (XCAR (tail)))
1675 if (first_string)
1677 error_message = Ferror_message_string (data);
1678 first_string = 0;
1681 if (fast_string_match (XCAR (tail), error_message) >= 0)
1682 return 1;
1684 else
1686 Lisp_Object contail;
1688 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1689 if (EQ (XCAR (tail), XCAR (contail)))
1690 return 1;
1694 return 0;
1697 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1698 SIG and DATA describe the signal. There are two ways to pass them:
1699 = SIG is the error symbol, and DATA is the rest of the data.
1700 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1701 This is for memory-full errors only. */
1702 static bool
1703 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1705 Lisp_Object combined_data;
1707 combined_data = Fcons (sig, data);
1709 if (
1710 /* Don't try to run the debugger with interrupts blocked.
1711 The editing loop would return anyway. */
1712 ! input_blocked_p ()
1713 && NILP (Vinhibit_debugger)
1714 /* Does user want to enter debugger for this kind of error? */
1715 && (EQ (sig, Qquit)
1716 ? debug_on_quit
1717 : wants_debugger (Vdebug_on_error, conditions))
1718 && ! skip_debugger (conditions, combined_data)
1719 /* RMS: What's this for? */
1720 && when_entered_debugger < num_nonmacro_input_events)
1722 call_debugger (list2 (Qerror, combined_data));
1723 return 1;
1726 return 0;
1729 static Lisp_Object
1730 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1732 register Lisp_Object h;
1734 /* t is used by handlers for all conditions, set up by C code. */
1735 if (EQ (handlers, Qt))
1736 return Qt;
1738 /* error is used similarly, but means print an error message
1739 and run the debugger if that is enabled. */
1740 if (EQ (handlers, Qerror))
1741 return Qt;
1743 for (h = handlers; CONSP (h); h = XCDR (h))
1745 Lisp_Object handler = XCAR (h);
1746 if (!NILP (Fmemq (handler, conditions)))
1747 return handlers;
1750 return Qnil;
1754 /* Dump an error message; called like vprintf. */
1755 void
1756 verror (const char *m, va_list ap)
1758 char buf[4000];
1759 ptrdiff_t size = sizeof buf;
1760 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1761 char *buffer = buf;
1762 ptrdiff_t used;
1763 Lisp_Object string;
1765 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1766 string = make_string (buffer, used);
1767 if (buffer != buf)
1768 xfree (buffer);
1770 xsignal1 (Qerror, string);
1774 /* Dump an error message; called like printf. */
1776 /* VARARGS 1 */
1777 void
1778 error (const char *m, ...)
1780 va_list ap;
1781 va_start (ap, m);
1782 verror (m, ap);
1785 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1786 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1787 This means it contains a description for how to read arguments to give it.
1788 The value is nil for an invalid function or a symbol with no function
1789 definition.
1791 Interactively callable functions include strings and vectors (treated
1792 as keyboard macros), lambda-expressions that contain a top-level call
1793 to `interactive', autoload definitions made by `autoload' with non-nil
1794 fourth argument, and some of the built-in functions of Lisp.
1796 Also, a symbol satisfies `commandp' if its function definition does so.
1798 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1799 then strings and vectors are not accepted. */)
1800 (Lisp_Object function, Lisp_Object for_call_interactively)
1802 register Lisp_Object fun;
1803 register Lisp_Object funcar;
1804 Lisp_Object if_prop = Qnil;
1806 fun = function;
1808 fun = indirect_function (fun); /* Check cycles. */
1809 if (NILP (fun))
1810 return Qnil;
1812 /* Check an `interactive-form' property if present, analogous to the
1813 function-documentation property. */
1814 fun = function;
1815 while (SYMBOLP (fun))
1817 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1818 if (!NILP (tmp))
1819 if_prop = Qt;
1820 fun = Fsymbol_function (fun);
1823 /* Emacs primitives are interactive if their DEFUN specifies an
1824 interactive spec. */
1825 if (SUBRP (fun))
1826 return XSUBR (fun)->intspec ? Qt : if_prop;
1828 /* Bytecode objects are interactive if they are long enough to
1829 have an element whose index is COMPILED_INTERACTIVE, which is
1830 where the interactive spec is stored. */
1831 else if (COMPILEDP (fun))
1832 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1833 ? Qt : if_prop);
1835 /* Strings and vectors are keyboard macros. */
1836 if (STRINGP (fun) || VECTORP (fun))
1837 return (NILP (for_call_interactively) ? Qt : Qnil);
1839 /* Lists may represent commands. */
1840 if (!CONSP (fun))
1841 return Qnil;
1842 funcar = XCAR (fun);
1843 if (EQ (funcar, Qclosure))
1844 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1845 ? Qt : if_prop);
1846 else if (EQ (funcar, Qlambda))
1847 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1848 else if (EQ (funcar, Qautoload))
1849 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1850 else
1851 return Qnil;
1854 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1855 doc: /* Define FUNCTION to autoload from FILE.
1856 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1857 Third arg DOCSTRING is documentation for the function.
1858 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1859 Fifth arg TYPE indicates the type of the object:
1860 nil or omitted says FUNCTION is a function,
1861 `keymap' says FUNCTION is really a keymap, and
1862 `macro' or t says FUNCTION is really a macro.
1863 Third through fifth args give info about the real definition.
1864 They default to nil.
1865 If FUNCTION is already defined other than as an autoload,
1866 this does nothing and returns nil. */)
1867 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1869 CHECK_SYMBOL (function);
1870 CHECK_STRING (file);
1872 /* If function is defined and not as an autoload, don't override. */
1873 if (!NILP (XSYMBOL (function)->function)
1874 && !AUTOLOADP (XSYMBOL (function)->function))
1875 return Qnil;
1877 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1878 /* `read1' in lread.c has found the docstring starting with "\
1879 and assumed the docstring will be provided by Snarf-documentation, so it
1880 passed us 0 instead. But that leads to accidental sharing in purecopy's
1881 hash-consing, so we use a (hopefully) unique integer instead. */
1882 docstring = make_number (XHASH (function));
1883 return Fdefalias (function,
1884 list5 (Qautoload, file, docstring, interactive, type),
1885 Qnil);
1888 void
1889 un_autoload (Lisp_Object oldqueue)
1891 Lisp_Object queue, first, second;
1893 /* Queue to unwind is current value of Vautoload_queue.
1894 oldqueue is the shadowed value to leave in Vautoload_queue. */
1895 queue = Vautoload_queue;
1896 Vautoload_queue = oldqueue;
1897 while (CONSP (queue))
1899 first = XCAR (queue);
1900 second = Fcdr (first);
1901 first = Fcar (first);
1902 if (EQ (first, make_number (0)))
1903 Vfeatures = second;
1904 else
1905 Ffset (first, second);
1906 queue = XCDR (queue);
1910 /* Load an autoloaded function.
1911 FUNNAME is the symbol which is the function's name.
1912 FUNDEF is the autoload definition (a list). */
1914 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1915 doc: /* Load FUNDEF which should be an autoload.
1916 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1917 in which case the function returns the new autoloaded function value.
1918 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1919 it defines a macro. */)
1920 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1922 ptrdiff_t count = SPECPDL_INDEX ();
1924 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1925 return fundef;
1927 if (EQ (macro_only, Qmacro))
1929 Lisp_Object kind = Fnth (make_number (4), fundef);
1930 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1931 return fundef;
1934 /* This is to make sure that loadup.el gives a clear picture
1935 of what files are preloaded and when. */
1936 if (! NILP (Vpurify_flag))
1937 error ("Attempt to autoload %s while preparing to dump",
1938 SDATA (SYMBOL_NAME (funname)));
1940 CHECK_SYMBOL (funname);
1942 /* Preserve the match data. */
1943 record_unwind_save_match_data ();
1945 /* If autoloading gets an error (which includes the error of failing
1946 to define the function being called), we use Vautoload_queue
1947 to undo function definitions and `provide' calls made by
1948 the function. We do this in the specific case of autoloading
1949 because autoloading is not an explicit request "load this file",
1950 but rather a request to "call this function".
1952 The value saved here is to be restored into Vautoload_queue. */
1953 record_unwind_protect (un_autoload, Vautoload_queue);
1954 Vautoload_queue = Qt;
1955 /* If `macro_only', assume this autoload to be a "best-effort",
1956 so don't signal an error if autoloading fails. */
1957 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1959 /* Once loading finishes, don't undo it. */
1960 Vautoload_queue = Qt;
1961 unbind_to (count, Qnil);
1963 if (NILP (funname))
1964 return Qnil;
1965 else
1967 Lisp_Object fun = Findirect_function (funname, Qnil);
1969 if (!NILP (Fequal (fun, fundef)))
1970 error ("Autoloading failed to define function %s",
1971 SDATA (SYMBOL_NAME (funname)));
1972 else
1973 return fun;
1978 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1979 doc: /* Evaluate FORM and return its value.
1980 If LEXICAL is t, evaluate using lexical scoping.
1981 LEXICAL can also be an actual lexical environment, in the form of an
1982 alist mapping symbols to their value. */)
1983 (Lisp_Object form, Lisp_Object lexical)
1985 ptrdiff_t count = SPECPDL_INDEX ();
1986 specbind (Qinternal_interpreter_environment,
1987 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1988 return unbind_to (count, eval_sub (form));
1991 /* Grow the specpdl stack by one entry.
1992 The caller should have already initialized the entry.
1993 Signal an error on stack overflow.
1995 Make sure that there is always one unused entry past the top of the
1996 stack, so that the just-initialized entry is safely unwound if
1997 memory exhausted and an error is signaled here. Also, allocate a
1998 never-used entry just before the bottom of the stack; sometimes its
1999 address is taken. */
2001 static void
2002 grow_specpdl (void)
2004 specpdl_ptr++;
2006 if (specpdl_ptr == specpdl + specpdl_size)
2008 ptrdiff_t count = SPECPDL_INDEX ();
2009 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2010 union specbinding *pdlvec = specpdl - 1;
2011 ptrdiff_t pdlvecsize = specpdl_size + 1;
2012 if (max_size <= specpdl_size)
2014 if (max_specpdl_size < 400)
2015 max_size = max_specpdl_size = 400;
2016 if (max_size <= specpdl_size)
2017 signal_error ("Variable binding depth exceeds max-specpdl-size",
2018 Qnil);
2020 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2021 specpdl = pdlvec + 1;
2022 specpdl_size = pdlvecsize - 1;
2023 specpdl_ptr = specpdl + count;
2027 ptrdiff_t
2028 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2030 ptrdiff_t count = SPECPDL_INDEX ();
2032 eassert (nargs >= UNEVALLED);
2033 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2034 specpdl_ptr->bt.debug_on_exit = false;
2035 specpdl_ptr->bt.function = function;
2036 specpdl_ptr->bt.args = args;
2037 specpdl_ptr->bt.nargs = nargs;
2038 grow_specpdl ();
2040 return count;
2043 /* Eval a sub-expression of the current expression (i.e. in the same
2044 lexical scope). */
2045 Lisp_Object
2046 eval_sub (Lisp_Object form)
2048 Lisp_Object fun, val, original_fun, original_args;
2049 Lisp_Object funcar;
2050 ptrdiff_t count;
2052 /* Declare here, as this array may be accessed by call_debugger near
2053 the end of this function. See Bug#21245. */
2054 Lisp_Object argvals[8];
2056 if (SYMBOLP (form))
2058 /* Look up its binding in the lexical environment.
2059 We do not pay attention to the declared_special flag here, since we
2060 already did that when let-binding the variable. */
2061 Lisp_Object lex_binding
2062 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2063 ? Fassq (form, Vinternal_interpreter_environment)
2064 : Qnil;
2065 if (CONSP (lex_binding))
2066 return XCDR (lex_binding);
2067 else
2068 return Fsymbol_value (form);
2071 if (!CONSP (form))
2072 return form;
2074 QUIT;
2076 maybe_gc ();
2078 if (++lisp_eval_depth > max_lisp_eval_depth)
2080 if (max_lisp_eval_depth < 100)
2081 max_lisp_eval_depth = 100;
2082 if (lisp_eval_depth > max_lisp_eval_depth)
2083 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2086 original_fun = XCAR (form);
2087 original_args = XCDR (form);
2089 /* This also protects them from gc. */
2090 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2092 if (debug_on_next_call)
2093 do_debug_on_call (Qt, count);
2095 /* At this point, only original_fun and original_args
2096 have values that will be used below. */
2097 retry:
2099 /* Optimize for no indirection. */
2100 fun = original_fun;
2101 if (!SYMBOLP (fun))
2102 fun = Ffunction (Fcons (fun, Qnil));
2103 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2104 fun = indirect_function (fun);
2106 if (SUBRP (fun))
2108 Lisp_Object args_left = original_args;
2109 Lisp_Object numargs = Flength (args_left);
2111 check_cons_list ();
2113 if (XINT (numargs) < XSUBR (fun)->min_args
2114 || (XSUBR (fun)->max_args >= 0
2115 && XSUBR (fun)->max_args < XINT (numargs)))
2116 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2118 else if (XSUBR (fun)->max_args == UNEVALLED)
2119 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2120 else if (XSUBR (fun)->max_args == MANY)
2122 /* Pass a vector of evaluated arguments. */
2123 Lisp_Object *vals;
2124 ptrdiff_t argnum = 0;
2125 USE_SAFE_ALLOCA;
2127 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2129 while (!NILP (args_left))
2131 vals[argnum++] = eval_sub (Fcar (args_left));
2132 args_left = Fcdr (args_left);
2135 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2137 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2139 check_cons_list ();
2140 lisp_eval_depth--;
2141 /* Do the debug-on-exit now, while VALS still exists. */
2142 if (backtrace_debug_on_exit (specpdl + count))
2143 val = call_debugger (list2 (Qexit, val));
2144 SAFE_FREE ();
2145 specpdl_ptr--;
2146 return val;
2148 else
2150 int i, maxargs = XSUBR (fun)->max_args;
2152 for (i = 0; i < maxargs; i++)
2154 argvals[i] = eval_sub (Fcar (args_left));
2155 args_left = Fcdr (args_left);
2158 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2160 switch (i)
2162 case 0:
2163 val = (XSUBR (fun)->function.a0 ());
2164 break;
2165 case 1:
2166 val = (XSUBR (fun)->function.a1 (argvals[0]));
2167 break;
2168 case 2:
2169 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2170 break;
2171 case 3:
2172 val = (XSUBR (fun)->function.a3
2173 (argvals[0], argvals[1], argvals[2]));
2174 break;
2175 case 4:
2176 val = (XSUBR (fun)->function.a4
2177 (argvals[0], argvals[1], argvals[2], argvals[3]));
2178 break;
2179 case 5:
2180 val = (XSUBR (fun)->function.a5
2181 (argvals[0], argvals[1], argvals[2], argvals[3],
2182 argvals[4]));
2183 break;
2184 case 6:
2185 val = (XSUBR (fun)->function.a6
2186 (argvals[0], argvals[1], argvals[2], argvals[3],
2187 argvals[4], argvals[5]));
2188 break;
2189 case 7:
2190 val = (XSUBR (fun)->function.a7
2191 (argvals[0], argvals[1], argvals[2], argvals[3],
2192 argvals[4], argvals[5], argvals[6]));
2193 break;
2195 case 8:
2196 val = (XSUBR (fun)->function.a8
2197 (argvals[0], argvals[1], argvals[2], argvals[3],
2198 argvals[4], argvals[5], argvals[6], argvals[7]));
2199 break;
2201 default:
2202 /* Someone has created a subr that takes more arguments than
2203 is supported by this code. We need to either rewrite the
2204 subr to use a different argument protocol, or add more
2205 cases to this switch. */
2206 emacs_abort ();
2210 else if (COMPILEDP (fun))
2211 return apply_lambda (fun, original_args, count);
2212 else
2214 if (NILP (fun))
2215 xsignal1 (Qvoid_function, original_fun);
2216 if (!CONSP (fun))
2217 xsignal1 (Qinvalid_function, original_fun);
2218 funcar = XCAR (fun);
2219 if (!SYMBOLP (funcar))
2220 xsignal1 (Qinvalid_function, original_fun);
2221 if (EQ (funcar, Qautoload))
2223 Fautoload_do_load (fun, original_fun, Qnil);
2224 goto retry;
2226 if (EQ (funcar, Qmacro))
2228 ptrdiff_t count1 = SPECPDL_INDEX ();
2229 Lisp_Object exp;
2230 /* Bind lexical-binding during expansion of the macro, so the
2231 macro can know reliably if the code it outputs will be
2232 interpreted using lexical-binding or not. */
2233 specbind (Qlexical_binding,
2234 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2235 exp = apply1 (Fcdr (fun), original_args);
2236 unbind_to (count1, Qnil);
2237 val = eval_sub (exp);
2239 else if (EQ (funcar, Qlambda)
2240 || EQ (funcar, Qclosure))
2241 return apply_lambda (fun, original_args, count);
2242 else
2243 xsignal1 (Qinvalid_function, original_fun);
2245 check_cons_list ();
2247 lisp_eval_depth--;
2248 if (backtrace_debug_on_exit (specpdl + count))
2249 val = call_debugger (list2 (Qexit, val));
2250 specpdl_ptr--;
2252 return val;
2255 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2256 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2257 Then return the value FUNCTION returns.
2258 Thus, (apply \\='+ 1 2 \\='(3 4)) returns 10.
2259 usage: (apply FUNCTION &rest ARGUMENTS) */)
2260 (ptrdiff_t nargs, Lisp_Object *args)
2262 ptrdiff_t i, numargs, funcall_nargs;
2263 register Lisp_Object *funcall_args = NULL;
2264 register Lisp_Object spread_arg = args[nargs - 1];
2265 Lisp_Object fun = args[0];
2266 Lisp_Object retval;
2267 USE_SAFE_ALLOCA;
2269 CHECK_LIST (spread_arg);
2271 numargs = XINT (Flength (spread_arg));
2273 if (numargs == 0)
2274 return Ffuncall (nargs - 1, args);
2275 else if (numargs == 1)
2277 args [nargs - 1] = XCAR (spread_arg);
2278 return Ffuncall (nargs, args);
2281 numargs += nargs - 2;
2283 /* Optimize for no indirection. */
2284 if (SYMBOLP (fun) && !NILP (fun)
2285 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2287 fun = indirect_function (fun);
2288 if (NILP (fun))
2289 /* Let funcall get the error. */
2290 fun = args[0];
2293 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2294 /* Don't hide an error by adding missing arguments. */
2295 && numargs >= XSUBR (fun)->min_args)
2297 /* Avoid making funcall cons up a yet another new vector of arguments
2298 by explicitly supplying nil's for optional values. */
2299 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2300 memclear (funcall_args + numargs + 1,
2301 (XSUBR (fun)->max_args - numargs) * word_size);
2302 funcall_nargs = 1 + XSUBR (fun)->max_args;
2304 else
2305 { /* We add 1 to numargs because funcall_args includes the
2306 function itself as well as its arguments. */
2307 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2308 funcall_nargs = 1 + numargs;
2311 memcpy (funcall_args, args, nargs * word_size);
2312 /* Spread the last arg we got. Its first element goes in
2313 the slot that it used to occupy, hence this value of I. */
2314 i = nargs - 1;
2315 while (!NILP (spread_arg))
2317 funcall_args [i++] = XCAR (spread_arg);
2318 spread_arg = XCDR (spread_arg);
2321 retval = Ffuncall (funcall_nargs, funcall_args);
2323 SAFE_FREE ();
2324 return retval;
2327 /* Run hook variables in various ways. */
2329 static Lisp_Object
2330 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2332 Ffuncall (nargs, args);
2333 return Qnil;
2336 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2337 doc: /* Run each hook in HOOKS.
2338 Each argument should be a symbol, a hook variable.
2339 These symbols are processed in the order specified.
2340 If a hook symbol has a non-nil value, that value may be a function
2341 or a list of functions to be called to run the hook.
2342 If the value is a function, it is called with no arguments.
2343 If it is a list, the elements are called, in order, with no arguments.
2345 Major modes should not use this function directly to run their mode
2346 hook; they should use `run-mode-hooks' instead.
2348 Do not use `make-local-variable' to make a hook variable buffer-local.
2349 Instead, use `add-hook' and specify t for the LOCAL argument.
2350 usage: (run-hooks &rest HOOKS) */)
2351 (ptrdiff_t nargs, Lisp_Object *args)
2353 ptrdiff_t i;
2355 for (i = 0; i < nargs; i++)
2356 run_hook (args[i]);
2358 return Qnil;
2361 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2362 Srun_hook_with_args, 1, MANY, 0,
2363 doc: /* Run HOOK with the specified arguments ARGS.
2364 HOOK should be a symbol, a hook variable. The value of HOOK
2365 may be nil, a function, or a list of functions. Call each
2366 function in order with arguments ARGS. The final return value
2367 is unspecified.
2369 Do not use `make-local-variable' to make a hook variable buffer-local.
2370 Instead, use `add-hook' and specify t for the LOCAL argument.
2371 usage: (run-hook-with-args HOOK &rest ARGS) */)
2372 (ptrdiff_t nargs, Lisp_Object *args)
2374 return run_hook_with_args (nargs, args, funcall_nil);
2377 /* NB this one still documents a specific non-nil return value.
2378 (As did run-hook-with-args and run-hook-with-args-until-failure
2379 until they were changed in 24.1.) */
2380 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2381 Srun_hook_with_args_until_success, 1, MANY, 0,
2382 doc: /* Run HOOK with the specified arguments ARGS.
2383 HOOK should be a symbol, a hook variable. The value of HOOK
2384 may be nil, a function, or a list of functions. Call each
2385 function in order with arguments ARGS, stopping at the first
2386 one that returns non-nil, and return that value. Otherwise (if
2387 all functions return nil, or if there are no functions to call),
2388 return nil.
2390 Do not use `make-local-variable' to make a hook variable buffer-local.
2391 Instead, use `add-hook' and specify t for the LOCAL argument.
2392 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2393 (ptrdiff_t nargs, Lisp_Object *args)
2395 return run_hook_with_args (nargs, args, Ffuncall);
2398 static Lisp_Object
2399 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2401 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2404 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2405 Srun_hook_with_args_until_failure, 1, MANY, 0,
2406 doc: /* Run HOOK with the specified arguments ARGS.
2407 HOOK should be a symbol, a hook variable. The value of HOOK
2408 may be nil, a function, or a list of functions. Call each
2409 function in order with arguments ARGS, stopping at the first
2410 one that returns nil, and return nil. Otherwise (if all functions
2411 return non-nil, or if there are no functions to call), return non-nil
2412 \(do not rely on the precise return value in this case).
2414 Do not use `make-local-variable' to make a hook variable buffer-local.
2415 Instead, use `add-hook' and specify t for the LOCAL argument.
2416 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2417 (ptrdiff_t nargs, Lisp_Object *args)
2419 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2422 static Lisp_Object
2423 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2425 Lisp_Object tmp = args[0], ret;
2426 args[0] = args[1];
2427 args[1] = tmp;
2428 ret = Ffuncall (nargs, args);
2429 args[1] = args[0];
2430 args[0] = tmp;
2431 return ret;
2434 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2435 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2436 I.e. instead of calling each function FUN directly with arguments ARGS,
2437 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2438 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2439 aborts and returns that value.
2440 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2441 (ptrdiff_t nargs, Lisp_Object *args)
2443 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2446 /* ARGS[0] should be a hook symbol.
2447 Call each of the functions in the hook value, passing each of them
2448 as arguments all the rest of ARGS (all NARGS - 1 elements).
2449 FUNCALL specifies how to call each function on the hook. */
2451 Lisp_Object
2452 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2453 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2455 Lisp_Object sym, val, ret = Qnil;
2457 /* If we are dying or still initializing,
2458 don't do anything--it would probably crash if we tried. */
2459 if (NILP (Vrun_hooks))
2460 return Qnil;
2462 sym = args[0];
2463 val = find_symbol_value (sym);
2465 if (EQ (val, Qunbound) || NILP (val))
2466 return ret;
2467 else if (!CONSP (val) || FUNCTIONP (val))
2469 args[0] = val;
2470 return funcall (nargs, args);
2472 else
2474 Lisp_Object global_vals = Qnil;
2476 for (;
2477 CONSP (val) && NILP (ret);
2478 val = XCDR (val))
2480 if (EQ (XCAR (val), Qt))
2482 /* t indicates this hook has a local binding;
2483 it means to run the global binding too. */
2484 global_vals = Fdefault_value (sym);
2485 if (NILP (global_vals)) continue;
2487 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2489 args[0] = global_vals;
2490 ret = funcall (nargs, args);
2492 else
2494 for (;
2495 CONSP (global_vals) && NILP (ret);
2496 global_vals = XCDR (global_vals))
2498 args[0] = XCAR (global_vals);
2499 /* In a global value, t should not occur. If it does, we
2500 must ignore it to avoid an endless loop. */
2501 if (!EQ (args[0], Qt))
2502 ret = funcall (nargs, args);
2506 else
2508 args[0] = XCAR (val);
2509 ret = funcall (nargs, args);
2513 return ret;
2517 /* Run the hook HOOK, giving each function no args. */
2519 void
2520 run_hook (Lisp_Object hook)
2522 Frun_hook_with_args (1, &hook);
2525 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2527 void
2528 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2530 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2533 /* Apply fn to arg. */
2534 Lisp_Object
2535 apply1 (Lisp_Object fn, Lisp_Object arg)
2537 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2540 /* Call function fn on no arguments. */
2541 Lisp_Object
2542 call0 (Lisp_Object fn)
2544 return Ffuncall (1, &fn);
2547 /* Call function fn with 1 argument arg1. */
2548 /* ARGSUSED */
2549 Lisp_Object
2550 call1 (Lisp_Object fn, Lisp_Object arg1)
2552 return CALLN (Ffuncall, fn, arg1);
2555 /* Call function fn with 2 arguments arg1, arg2. */
2556 /* ARGSUSED */
2557 Lisp_Object
2558 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2560 return CALLN (Ffuncall, fn, arg1, arg2);
2563 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2564 /* ARGSUSED */
2565 Lisp_Object
2566 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2568 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2571 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2572 /* ARGSUSED */
2573 Lisp_Object
2574 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2575 Lisp_Object arg4)
2577 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2580 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2581 /* ARGSUSED */
2582 Lisp_Object
2583 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2584 Lisp_Object arg4, Lisp_Object arg5)
2586 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2589 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2590 /* ARGSUSED */
2591 Lisp_Object
2592 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2593 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2595 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2598 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2599 /* ARGSUSED */
2600 Lisp_Object
2601 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2602 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2604 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2607 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2608 doc: /* Non-nil if OBJECT is a function. */)
2609 (Lisp_Object object)
2611 if (FUNCTIONP (object))
2612 return Qt;
2613 return Qnil;
2616 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2617 doc: /* Call first argument as a function, passing remaining arguments to it.
2618 Return the value that function returns.
2619 Thus, (funcall \\='cons \\='x \\='y) returns (x . y).
2620 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2621 (ptrdiff_t nargs, Lisp_Object *args)
2623 Lisp_Object fun, original_fun;
2624 Lisp_Object funcar;
2625 ptrdiff_t numargs = nargs - 1;
2626 Lisp_Object lisp_numargs;
2627 Lisp_Object val;
2628 Lisp_Object *internal_args;
2629 ptrdiff_t count;
2631 QUIT;
2633 if (++lisp_eval_depth > max_lisp_eval_depth)
2635 if (max_lisp_eval_depth < 100)
2636 max_lisp_eval_depth = 100;
2637 if (lisp_eval_depth > max_lisp_eval_depth)
2638 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2641 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2643 maybe_gc ();
2645 if (debug_on_next_call)
2646 do_debug_on_call (Qlambda, count);
2648 check_cons_list ();
2650 original_fun = args[0];
2652 retry:
2654 /* Optimize for no indirection. */
2655 fun = original_fun;
2656 if (SYMBOLP (fun) && !NILP (fun)
2657 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2658 fun = indirect_function (fun);
2660 if (SUBRP (fun))
2662 if (numargs < XSUBR (fun)->min_args
2663 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2665 XSETFASTINT (lisp_numargs, numargs);
2666 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2669 else if (XSUBR (fun)->max_args == UNEVALLED)
2670 xsignal1 (Qinvalid_function, original_fun);
2672 else if (XSUBR (fun)->max_args == MANY)
2673 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2674 else
2676 Lisp_Object internal_argbuf[8];
2677 if (XSUBR (fun)->max_args > numargs)
2679 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2680 internal_args = internal_argbuf;
2681 memcpy (internal_args, args + 1, numargs * word_size);
2682 memclear (internal_args + numargs,
2683 (XSUBR (fun)->max_args - numargs) * word_size);
2685 else
2686 internal_args = args + 1;
2687 switch (XSUBR (fun)->max_args)
2689 case 0:
2690 val = (XSUBR (fun)->function.a0 ());
2691 break;
2692 case 1:
2693 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2694 break;
2695 case 2:
2696 val = (XSUBR (fun)->function.a2
2697 (internal_args[0], internal_args[1]));
2698 break;
2699 case 3:
2700 val = (XSUBR (fun)->function.a3
2701 (internal_args[0], internal_args[1], internal_args[2]));
2702 break;
2703 case 4:
2704 val = (XSUBR (fun)->function.a4
2705 (internal_args[0], internal_args[1], internal_args[2],
2706 internal_args[3]));
2707 break;
2708 case 5:
2709 val = (XSUBR (fun)->function.a5
2710 (internal_args[0], internal_args[1], internal_args[2],
2711 internal_args[3], internal_args[4]));
2712 break;
2713 case 6:
2714 val = (XSUBR (fun)->function.a6
2715 (internal_args[0], internal_args[1], internal_args[2],
2716 internal_args[3], internal_args[4], internal_args[5]));
2717 break;
2718 case 7:
2719 val = (XSUBR (fun)->function.a7
2720 (internal_args[0], internal_args[1], internal_args[2],
2721 internal_args[3], internal_args[4], internal_args[5],
2722 internal_args[6]));
2723 break;
2725 case 8:
2726 val = (XSUBR (fun)->function.a8
2727 (internal_args[0], internal_args[1], internal_args[2],
2728 internal_args[3], internal_args[4], internal_args[5],
2729 internal_args[6], internal_args[7]));
2730 break;
2732 default:
2734 /* If a subr takes more than 8 arguments without using MANY
2735 or UNEVALLED, we need to extend this function to support it.
2736 Until this is done, there is no way to call the function. */
2737 emacs_abort ();
2741 else if (COMPILEDP (fun))
2742 val = funcall_lambda (fun, numargs, args + 1);
2743 else
2745 if (NILP (fun))
2746 xsignal1 (Qvoid_function, original_fun);
2747 if (!CONSP (fun))
2748 xsignal1 (Qinvalid_function, original_fun);
2749 funcar = XCAR (fun);
2750 if (!SYMBOLP (funcar))
2751 xsignal1 (Qinvalid_function, original_fun);
2752 if (EQ (funcar, Qlambda)
2753 || EQ (funcar, Qclosure))
2754 val = funcall_lambda (fun, numargs, args + 1);
2755 else if (EQ (funcar, Qautoload))
2757 Fautoload_do_load (fun, original_fun, Qnil);
2758 check_cons_list ();
2759 goto retry;
2761 else
2762 xsignal1 (Qinvalid_function, original_fun);
2764 check_cons_list ();
2765 lisp_eval_depth--;
2766 if (backtrace_debug_on_exit (specpdl + count))
2767 val = call_debugger (list2 (Qexit, val));
2768 specpdl_ptr--;
2769 return val;
2772 static Lisp_Object
2773 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2775 Lisp_Object args_left;
2776 ptrdiff_t i;
2777 EMACS_INT numargs;
2778 Lisp_Object *arg_vector;
2779 Lisp_Object tem;
2780 USE_SAFE_ALLOCA;
2782 numargs = XFASTINT (Flength (args));
2783 SAFE_ALLOCA_LISP (arg_vector, numargs);
2784 args_left = args;
2786 for (i = 0; i < numargs; )
2788 tem = Fcar (args_left), args_left = Fcdr (args_left);
2789 tem = eval_sub (tem);
2790 arg_vector[i++] = tem;
2793 set_backtrace_args (specpdl + count, arg_vector, i);
2794 tem = funcall_lambda (fun, numargs, arg_vector);
2796 check_cons_list ();
2797 lisp_eval_depth--;
2798 /* Do the debug-on-exit now, while arg_vector still exists. */
2799 if (backtrace_debug_on_exit (specpdl + count))
2800 tem = call_debugger (list2 (Qexit, tem));
2801 SAFE_FREE ();
2802 specpdl_ptr--;
2803 return tem;
2806 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2807 and return the result of evaluation.
2808 FUN must be either a lambda-expression or a compiled-code object. */
2810 static Lisp_Object
2811 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2812 register Lisp_Object *arg_vector)
2814 Lisp_Object val, syms_left, next, lexenv;
2815 ptrdiff_t count = SPECPDL_INDEX ();
2816 ptrdiff_t i;
2817 bool optional, rest;
2819 if (CONSP (fun))
2821 if (EQ (XCAR (fun), Qclosure))
2823 fun = XCDR (fun); /* Drop `closure'. */
2824 lexenv = XCAR (fun);
2825 CHECK_LIST_CONS (fun, fun);
2827 else
2828 lexenv = Qnil;
2829 syms_left = XCDR (fun);
2830 if (CONSP (syms_left))
2831 syms_left = XCAR (syms_left);
2832 else
2833 xsignal1 (Qinvalid_function, fun);
2835 else if (COMPILEDP (fun))
2837 ptrdiff_t size = ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK;
2838 if (size <= COMPILED_STACK_DEPTH)
2839 xsignal1 (Qinvalid_function, fun);
2840 syms_left = AREF (fun, COMPILED_ARGLIST);
2841 if (INTEGERP (syms_left))
2842 /* A byte-code object with a non-nil `push args' slot means we
2843 shouldn't bind any arguments, instead just call the byte-code
2844 interpreter directly; it will push arguments as necessary.
2846 Byte-code objects with either a non-existent, or a nil value for
2847 the `push args' slot (the default), have dynamically-bound
2848 arguments, and use the argument-binding code below instead (as do
2849 all interpreted functions, even lexically bound ones). */
2851 /* If we have not actually read the bytecode string
2852 and constants vector yet, fetch them from the file. */
2853 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2854 Ffetch_bytecode (fun);
2855 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2856 AREF (fun, COMPILED_CONSTANTS),
2857 AREF (fun, COMPILED_STACK_DEPTH),
2858 syms_left,
2859 nargs, arg_vector);
2861 lexenv = Qnil;
2863 else
2864 emacs_abort ();
2866 i = optional = rest = 0;
2867 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2869 QUIT;
2871 next = XCAR (syms_left);
2872 if (!SYMBOLP (next))
2873 xsignal1 (Qinvalid_function, fun);
2875 if (EQ (next, Qand_rest))
2876 rest = 1;
2877 else if (EQ (next, Qand_optional))
2878 optional = 1;
2879 else
2881 Lisp_Object arg;
2882 if (rest)
2884 arg = Flist (nargs - i, &arg_vector[i]);
2885 i = nargs;
2887 else if (i < nargs)
2888 arg = arg_vector[i++];
2889 else if (!optional)
2890 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2891 else
2892 arg = Qnil;
2894 /* Bind the argument. */
2895 if (!NILP (lexenv) && SYMBOLP (next))
2896 /* Lexically bind NEXT by adding it to the lexenv alist. */
2897 lexenv = Fcons (Fcons (next, arg), lexenv);
2898 else
2899 /* Dynamically bind NEXT. */
2900 specbind (next, arg);
2904 if (!NILP (syms_left))
2905 xsignal1 (Qinvalid_function, fun);
2906 else if (i < nargs)
2907 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2909 if (!EQ (lexenv, Vinternal_interpreter_environment))
2910 /* Instantiate a new lexical environment. */
2911 specbind (Qinternal_interpreter_environment, lexenv);
2913 if (CONSP (fun))
2914 val = Fprogn (XCDR (XCDR (fun)));
2915 else
2917 /* If we have not actually read the bytecode string
2918 and constants vector yet, fetch them from the file. */
2919 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2920 Ffetch_bytecode (fun);
2921 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2922 AREF (fun, COMPILED_CONSTANTS),
2923 AREF (fun, COMPILED_STACK_DEPTH),
2924 Qnil, 0, 0);
2927 return unbind_to (count, val);
2930 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2931 1, 1, 0,
2932 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2933 (Lisp_Object object)
2935 Lisp_Object tem;
2937 if (COMPILEDP (object))
2939 ptrdiff_t size = ASIZE (object) & PSEUDOVECTOR_SIZE_MASK;
2940 if (size <= COMPILED_STACK_DEPTH)
2941 xsignal1 (Qinvalid_function, object);
2942 if (CONSP (AREF (object, COMPILED_BYTECODE)))
2944 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2945 if (!CONSP (tem))
2947 tem = AREF (object, COMPILED_BYTECODE);
2948 if (CONSP (tem) && STRINGP (XCAR (tem)))
2949 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2950 else
2951 error ("Invalid byte code");
2953 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2954 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2957 return object;
2960 /* Return true if SYMBOL currently has a let-binding
2961 which was made in the buffer that is now current. */
2963 bool
2964 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2966 union specbinding *p;
2967 Lisp_Object buf = Fcurrent_buffer ();
2969 for (p = specpdl_ptr; p > specpdl; )
2970 if ((--p)->kind > SPECPDL_LET)
2972 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2973 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2974 if (symbol == let_bound_symbol
2975 && EQ (specpdl_where (p), buf))
2976 return 1;
2979 return 0;
2982 bool
2983 let_shadows_global_binding_p (Lisp_Object symbol)
2985 union specbinding *p;
2987 for (p = specpdl_ptr; p > specpdl; )
2988 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
2989 return 1;
2991 return 0;
2994 /* `specpdl_ptr' describes which variable is
2995 let-bound, so it can be properly undone when we unbind_to.
2996 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
2997 - SYMBOL is the variable being bound. Note that it should not be
2998 aliased (i.e. when let-binding V1 that's aliased to V2, we want
2999 to record V2 here).
3000 - WHERE tells us in which buffer the binding took place.
3001 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3002 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3003 i.e. bindings to the default value of a variable which can be
3004 buffer-local. */
3006 void
3007 specbind (Lisp_Object symbol, Lisp_Object value)
3009 struct Lisp_Symbol *sym;
3011 CHECK_SYMBOL (symbol);
3012 sym = XSYMBOL (symbol);
3014 start:
3015 switch (sym->redirect)
3017 case SYMBOL_VARALIAS:
3018 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3019 case SYMBOL_PLAINVAL:
3020 /* The most common case is that of a non-constant symbol with a
3021 trivial value. Make that as fast as we can. */
3022 specpdl_ptr->let.kind = SPECPDL_LET;
3023 specpdl_ptr->let.symbol = symbol;
3024 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3025 grow_specpdl ();
3026 if (!sym->constant)
3027 SET_SYMBOL_VAL (sym, value);
3028 else
3029 set_internal (symbol, value, Qnil, 1);
3030 break;
3031 case SYMBOL_LOCALIZED:
3032 if (SYMBOL_BLV (sym)->frame_local)
3033 error ("Frame-local vars cannot be let-bound");
3034 case SYMBOL_FORWARDED:
3036 Lisp_Object ovalue = find_symbol_value (symbol);
3037 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3038 specpdl_ptr->let.symbol = symbol;
3039 specpdl_ptr->let.old_value = ovalue;
3040 specpdl_ptr->let.where = Fcurrent_buffer ();
3042 eassert (sym->redirect != SYMBOL_LOCALIZED
3043 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3045 if (sym->redirect == SYMBOL_LOCALIZED)
3047 if (!blv_found (SYMBOL_BLV (sym)))
3048 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3050 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3052 /* If SYMBOL is a per-buffer variable which doesn't have a
3053 buffer-local value here, make the `let' change the global
3054 value by changing the value of SYMBOL in all buffers not
3055 having their own value. This is consistent with what
3056 happens with other buffer-local variables. */
3057 if (NILP (Flocal_variable_p (symbol, Qnil)))
3059 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3060 grow_specpdl ();
3061 Fset_default (symbol, value);
3062 return;
3065 else
3066 specpdl_ptr->let.kind = SPECPDL_LET;
3068 grow_specpdl ();
3069 set_internal (symbol, value, Qnil, 1);
3070 break;
3072 default: emacs_abort ();
3076 /* Push unwind-protect entries of various types. */
3078 void
3079 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3081 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3082 specpdl_ptr->unwind.func = function;
3083 specpdl_ptr->unwind.arg = arg;
3084 grow_specpdl ();
3087 void
3088 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3090 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3091 specpdl_ptr->unwind_ptr.func = function;
3092 specpdl_ptr->unwind_ptr.arg = arg;
3093 grow_specpdl ();
3096 void
3097 record_unwind_protect_int (void (*function) (int), int arg)
3099 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3100 specpdl_ptr->unwind_int.func = function;
3101 specpdl_ptr->unwind_int.arg = arg;
3102 grow_specpdl ();
3105 void
3106 record_unwind_protect_void (void (*function) (void))
3108 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3109 specpdl_ptr->unwind_void.func = function;
3110 grow_specpdl ();
3113 static void
3114 do_nothing (void)
3117 /* Push an unwind-protect entry that does nothing, so that
3118 set_unwind_protect_ptr can overwrite it later. */
3120 void
3121 record_unwind_protect_nothing (void)
3123 record_unwind_protect_void (do_nothing);
3126 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3127 It need not be at the top of the stack. */
3129 void
3130 clear_unwind_protect (ptrdiff_t count)
3132 union specbinding *p = specpdl + count;
3133 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3134 p->unwind_void.func = do_nothing;
3137 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3138 It need not be at the top of the stack. Discard the entry's
3139 previous value without invoking it. */
3141 void
3142 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3143 Lisp_Object arg)
3145 union specbinding *p = specpdl + count;
3146 p->unwind.kind = SPECPDL_UNWIND;
3147 p->unwind.func = func;
3148 p->unwind.arg = arg;
3151 void
3152 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3154 union specbinding *p = specpdl + count;
3155 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3156 p->unwind_ptr.func = func;
3157 p->unwind_ptr.arg = arg;
3160 /* Pop and execute entries from the unwind-protect stack until the
3161 depth COUNT is reached. Return VALUE. */
3163 Lisp_Object
3164 unbind_to (ptrdiff_t count, Lisp_Object value)
3166 Lisp_Object quitf = Vquit_flag;
3168 Vquit_flag = Qnil;
3170 while (specpdl_ptr != specpdl + count)
3172 /* Decrement specpdl_ptr before we do the work to unbind it, so
3173 that an error in unbinding won't try to unbind the same entry
3174 again. Take care to copy any parts of the binding needed
3175 before invoking any code that can make more bindings. */
3177 specpdl_ptr--;
3179 switch (specpdl_ptr->kind)
3181 case SPECPDL_UNWIND:
3182 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3183 break;
3184 case SPECPDL_UNWIND_PTR:
3185 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3186 break;
3187 case SPECPDL_UNWIND_INT:
3188 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3189 break;
3190 case SPECPDL_UNWIND_VOID:
3191 specpdl_ptr->unwind_void.func ();
3192 break;
3193 case SPECPDL_BACKTRACE:
3194 break;
3195 case SPECPDL_LET:
3196 { /* If variable has a trivial value (no forwarding), we can
3197 just set it. No need to check for constant symbols here,
3198 since that was already done by specbind. */
3199 Lisp_Object sym = specpdl_symbol (specpdl_ptr);
3200 if (SYMBOLP (sym) && XSYMBOL (sym)->redirect == SYMBOL_PLAINVAL)
3202 SET_SYMBOL_VAL (XSYMBOL (sym),
3203 specpdl_old_value (specpdl_ptr));
3204 break;
3206 else
3207 { /* FALLTHROUGH!!
3208 NOTE: we only ever come here if make_local_foo was used for
3209 the first time on this var within this let. */
3212 case SPECPDL_LET_DEFAULT:
3213 Fset_default (specpdl_symbol (specpdl_ptr),
3214 specpdl_old_value (specpdl_ptr));
3215 break;
3216 case SPECPDL_LET_LOCAL:
3218 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3219 Lisp_Object where = specpdl_where (specpdl_ptr);
3220 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3221 eassert (BUFFERP (where));
3223 /* If this was a local binding, reset the value in the appropriate
3224 buffer, but only if that buffer's binding still exists. */
3225 if (!NILP (Flocal_variable_p (symbol, where)))
3226 set_internal (symbol, old_value, where, 1);
3228 break;
3232 if (NILP (Vquit_flag) && !NILP (quitf))
3233 Vquit_flag = quitf;
3235 return value;
3238 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3239 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3240 A special variable is one that will be bound dynamically, even in a
3241 context where binding is lexical by default. */)
3242 (Lisp_Object symbol)
3244 CHECK_SYMBOL (symbol);
3245 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3249 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3250 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3251 The debugger is entered when that frame exits, if the flag is non-nil. */)
3252 (Lisp_Object level, Lisp_Object flag)
3254 union specbinding *pdl = backtrace_top ();
3255 register EMACS_INT i;
3257 CHECK_NUMBER (level);
3259 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3260 pdl = backtrace_next (pdl);
3262 if (backtrace_p (pdl))
3263 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3265 return flag;
3268 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3269 doc: /* Print a trace of Lisp function calls currently active.
3270 Output stream used is value of `standard-output'. */)
3271 (void)
3273 union specbinding *pdl = backtrace_top ();
3274 Lisp_Object tem;
3275 Lisp_Object old_print_level = Vprint_level;
3277 if (NILP (Vprint_level))
3278 XSETFASTINT (Vprint_level, 8);
3280 while (backtrace_p (pdl))
3282 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ");
3283 if (backtrace_nargs (pdl) == UNEVALLED)
3285 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3286 Qnil);
3287 write_string ("\n");
3289 else
3291 tem = backtrace_function (pdl);
3292 Fprin1 (tem, Qnil); /* This can QUIT. */
3293 write_string ("(");
3295 ptrdiff_t i;
3296 for (i = 0; i < backtrace_nargs (pdl); i++)
3298 if (i) write_string (" ");
3299 Fprin1 (backtrace_args (pdl)[i], Qnil);
3302 write_string (")\n");
3304 pdl = backtrace_next (pdl);
3307 Vprint_level = old_print_level;
3308 return Qnil;
3311 static union specbinding *
3312 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3314 union specbinding *pdl = backtrace_top ();
3315 register EMACS_INT i;
3317 CHECK_NATNUM (nframes);
3319 if (!NILP (base))
3320 { /* Skip up to `base'. */
3321 base = Findirect_function (base, Qt);
3322 while (backtrace_p (pdl)
3323 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3324 pdl = backtrace_next (pdl);
3327 /* Find the frame requested. */
3328 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3329 pdl = backtrace_next (pdl);
3331 return pdl;
3334 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3335 doc: /* Return the function and arguments NFRAMES up from current execution point.
3336 If that frame has not evaluated the arguments yet (or is a special form),
3337 the value is (nil FUNCTION ARG-FORMS...).
3338 If that frame has evaluated its arguments and called its function already,
3339 the value is (t FUNCTION ARG-VALUES...).
3340 A &rest arg is represented as the tail of the list ARG-VALUES.
3341 FUNCTION is whatever was supplied as car of evaluated list,
3342 or a lambda expression for macro calls.
3343 If NFRAMES is more than the number of frames, the value is nil.
3344 If BASE is non-nil, it should be a function and NFRAMES counts from its
3345 nearest activation frame. */)
3346 (Lisp_Object nframes, Lisp_Object base)
3348 union specbinding *pdl = get_backtrace_frame (nframes, base);
3350 if (!backtrace_p (pdl))
3351 return Qnil;
3352 if (backtrace_nargs (pdl) == UNEVALLED)
3353 return Fcons (Qnil,
3354 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3355 else
3357 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3359 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3363 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3364 the specpdl stack, and then rewind them. We store the pre-unwind values
3365 directly in the pre-existing specpdl elements (i.e. we swap the current
3366 value and the old value stored in the specpdl), kind of like the inplace
3367 pointer-reversal trick. As it turns out, the rewind does the same as the
3368 unwind, except it starts from the other end of the specpdl stack, so we use
3369 the same function for both unwind and rewind. */
3370 static void
3371 backtrace_eval_unrewind (int distance)
3373 union specbinding *tmp = specpdl_ptr;
3374 int step = -1;
3375 if (distance < 0)
3376 { /* It's a rewind rather than unwind. */
3377 tmp += distance - 1;
3378 step = 1;
3379 distance = -distance;
3382 for (; distance > 0; distance--)
3384 tmp += step;
3385 switch (tmp->kind)
3387 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3388 unwind_protect, but the problem is that we don't know how to
3389 rewind them afterwards. */
3390 case SPECPDL_UNWIND:
3392 Lisp_Object oldarg = tmp->unwind.arg;
3393 if (tmp->unwind.func == set_buffer_if_live)
3394 tmp->unwind.arg = Fcurrent_buffer ();
3395 else if (tmp->unwind.func == save_excursion_restore)
3396 tmp->unwind.arg = save_excursion_save ();
3397 else
3398 break;
3399 tmp->unwind.func (oldarg);
3400 break;
3403 case SPECPDL_UNWIND_PTR:
3404 case SPECPDL_UNWIND_INT:
3405 case SPECPDL_UNWIND_VOID:
3406 case SPECPDL_BACKTRACE:
3407 break;
3408 case SPECPDL_LET:
3409 { /* If variable has a trivial value (no forwarding), we can
3410 just set it. No need to check for constant symbols here,
3411 since that was already done by specbind. */
3412 Lisp_Object sym = specpdl_symbol (tmp);
3413 if (SYMBOLP (sym) && XSYMBOL (sym)->redirect == SYMBOL_PLAINVAL)
3415 Lisp_Object old_value = specpdl_old_value (tmp);
3416 set_specpdl_old_value (tmp, SYMBOL_VAL (XSYMBOL (sym)));
3417 SET_SYMBOL_VAL (XSYMBOL (sym), old_value);
3418 break;
3420 else
3421 { /* FALLTHROUGH!!
3422 NOTE: we only ever come here if make_local_foo was used for
3423 the first time on this var within this let. */
3426 case SPECPDL_LET_DEFAULT:
3428 Lisp_Object sym = specpdl_symbol (tmp);
3429 Lisp_Object old_value = specpdl_old_value (tmp);
3430 set_specpdl_old_value (tmp, Fdefault_value (sym));
3431 Fset_default (sym, old_value);
3433 break;
3434 case SPECPDL_LET_LOCAL:
3436 Lisp_Object symbol = specpdl_symbol (tmp);
3437 Lisp_Object where = specpdl_where (tmp);
3438 Lisp_Object old_value = specpdl_old_value (tmp);
3439 eassert (BUFFERP (where));
3441 /* If this was a local binding, reset the value in the appropriate
3442 buffer, but only if that buffer's binding still exists. */
3443 if (!NILP (Flocal_variable_p (symbol, where)))
3445 set_specpdl_old_value
3446 (tmp, Fbuffer_local_value (symbol, where));
3447 set_internal (symbol, old_value, where, 1);
3450 break;
3455 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3456 doc: /* Evaluate EXP in the context of some activation frame.
3457 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3458 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3460 union specbinding *pdl = get_backtrace_frame (nframes, base);
3461 ptrdiff_t count = SPECPDL_INDEX ();
3462 ptrdiff_t distance = specpdl_ptr - pdl;
3463 eassert (distance >= 0);
3465 if (!backtrace_p (pdl))
3466 error ("Activation frame not found!");
3468 backtrace_eval_unrewind (distance);
3469 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3471 /* Use eval_sub rather than Feval since the main motivation behind
3472 backtrace-eval is to be able to get/set the value of lexical variables
3473 from the debugger. */
3474 return unbind_to (count, eval_sub (exp));
3477 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3478 doc: /* Return names and values of local variables of a stack frame.
3479 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3480 (Lisp_Object nframes, Lisp_Object base)
3482 union specbinding *frame = get_backtrace_frame (nframes, base);
3483 union specbinding *prevframe
3484 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3485 ptrdiff_t distance = specpdl_ptr - frame;
3486 Lisp_Object result = Qnil;
3487 eassert (distance >= 0);
3489 if (!backtrace_p (prevframe))
3490 error ("Activation frame not found!");
3491 if (!backtrace_p (frame))
3492 error ("Activation frame not found!");
3494 /* The specpdl entries normally contain the symbol being bound along with its
3495 `old_value', so it can be restored. The new value to which it is bound is
3496 available in one of two places: either in the current value of the
3497 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3498 next specpdl entry for it.
3499 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3500 and "new value", so we abuse it here, to fetch the new value.
3501 It's ugly (we'd rather not modify global data) and a bit inefficient,
3502 but it does the job for now. */
3503 backtrace_eval_unrewind (distance);
3505 /* Grab values. */
3507 union specbinding *tmp = prevframe;
3508 for (; tmp > frame; tmp--)
3510 switch (tmp->kind)
3512 case SPECPDL_LET:
3513 case SPECPDL_LET_DEFAULT:
3514 case SPECPDL_LET_LOCAL:
3516 Lisp_Object sym = specpdl_symbol (tmp);
3517 Lisp_Object val = specpdl_old_value (tmp);
3518 if (EQ (sym, Qinternal_interpreter_environment))
3520 Lisp_Object env = val;
3521 for (; CONSP (env); env = XCDR (env))
3523 Lisp_Object binding = XCAR (env);
3524 if (CONSP (binding))
3525 result = Fcons (Fcons (XCAR (binding),
3526 XCDR (binding)),
3527 result);
3530 else
3531 result = Fcons (Fcons (sym, val), result);
3533 break;
3535 case SPECPDL_UNWIND:
3536 case SPECPDL_UNWIND_PTR:
3537 case SPECPDL_UNWIND_INT:
3538 case SPECPDL_UNWIND_VOID:
3539 case SPECPDL_BACKTRACE:
3540 break;
3542 default:
3543 emacs_abort ();
3548 /* Restore values from specpdl to original place. */
3549 backtrace_eval_unrewind (-distance);
3551 return result;
3555 void
3556 mark_specpdl (void)
3558 union specbinding *pdl;
3559 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3561 switch (pdl->kind)
3563 case SPECPDL_UNWIND:
3564 mark_object (specpdl_arg (pdl));
3565 break;
3567 case SPECPDL_BACKTRACE:
3569 ptrdiff_t nargs = backtrace_nargs (pdl);
3570 mark_object (backtrace_function (pdl));
3571 if (nargs == UNEVALLED)
3572 nargs = 1;
3573 while (nargs--)
3574 mark_object (backtrace_args (pdl)[nargs]);
3576 break;
3578 case SPECPDL_LET_DEFAULT:
3579 case SPECPDL_LET_LOCAL:
3580 mark_object (specpdl_where (pdl));
3581 /* Fall through. */
3582 case SPECPDL_LET:
3583 mark_object (specpdl_symbol (pdl));
3584 mark_object (specpdl_old_value (pdl));
3585 break;
3587 case SPECPDL_UNWIND_PTR:
3588 case SPECPDL_UNWIND_INT:
3589 case SPECPDL_UNWIND_VOID:
3590 break;
3592 default:
3593 emacs_abort ();
3598 void
3599 get_backtrace (Lisp_Object array)
3601 union specbinding *pdl = backtrace_next (backtrace_top ());
3602 ptrdiff_t i = 0, asize = ASIZE (array);
3604 /* Copy the backtrace contents into working memory. */
3605 for (; i < asize; i++)
3607 if (backtrace_p (pdl))
3609 ASET (array, i, backtrace_function (pdl));
3610 pdl = backtrace_next (pdl);
3612 else
3613 ASET (array, i, Qnil);
3617 Lisp_Object backtrace_top_function (void)
3619 union specbinding *pdl = backtrace_top ();
3620 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3623 void
3624 syms_of_eval (void)
3626 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3627 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3628 If Lisp code tries to increase the total number past this amount,
3629 an error is signaled.
3630 You can safely use a value considerably larger than the default value,
3631 if that proves inconveniently small. However, if you increase it too far,
3632 Emacs could run out of memory trying to make the stack bigger.
3633 Note that this limit may be silently increased by the debugger
3634 if `debug-on-error' or `debug-on-quit' is set. */);
3636 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3637 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3639 This limit serves to catch infinite recursions for you before they cause
3640 actual stack overflow in C, which would be fatal for Emacs.
3641 You can safely make it considerably larger than its default value,
3642 if that proves inconveniently small. However, if you increase it too far,
3643 Emacs could overflow the real C stack, and crash. */);
3645 DEFVAR_LISP ("quit-flag", Vquit_flag,
3646 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3647 If the value is t, that means do an ordinary quit.
3648 If the value equals `throw-on-input', that means quit by throwing
3649 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3650 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3651 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3652 Vquit_flag = Qnil;
3654 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3655 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3656 Note that `quit-flag' will still be set by typing C-g,
3657 so a quit will be signaled as soon as `inhibit-quit' is nil.
3658 To prevent this happening, set `quit-flag' to nil
3659 before making `inhibit-quit' nil. */);
3660 Vinhibit_quit = Qnil;
3662 DEFSYM (Qsetq, "setq");
3663 DEFSYM (Qinhibit_quit, "inhibit-quit");
3664 DEFSYM (Qautoload, "autoload");
3665 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3666 DEFSYM (Qmacro, "macro");
3668 /* Note that the process handling also uses Qexit, but we don't want
3669 to staticpro it twice, so we just do it here. */
3670 DEFSYM (Qexit, "exit");
3672 DEFSYM (Qinteractive, "interactive");
3673 DEFSYM (Qcommandp, "commandp");
3674 DEFSYM (Qand_rest, "&rest");
3675 DEFSYM (Qand_optional, "&optional");
3676 DEFSYM (Qclosure, "closure");
3677 DEFSYM (QCdocumentation, ":documentation");
3678 DEFSYM (Qdebug, "debug");
3680 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3681 doc: /* Non-nil means never enter the debugger.
3682 Normally set while the debugger is already active, to avoid recursive
3683 invocations. */);
3684 Vinhibit_debugger = Qnil;
3686 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3687 doc: /* Non-nil means enter debugger if an error is signaled.
3688 Does not apply to errors handled by `condition-case' or those
3689 matched by `debug-ignored-errors'.
3690 If the value is a list, an error only means to enter the debugger
3691 if one of its condition symbols appears in the list.
3692 When you evaluate an expression interactively, this variable
3693 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3694 The command `toggle-debug-on-error' toggles this.
3695 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3696 Vdebug_on_error = Qnil;
3698 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3699 doc: /* List of errors for which the debugger should not be called.
3700 Each element may be a condition-name or a regexp that matches error messages.
3701 If any element applies to a given error, that error skips the debugger
3702 and just returns to top level.
3703 This overrides the variable `debug-on-error'.
3704 It does not apply to errors handled by `condition-case'. */);
3705 Vdebug_ignored_errors = Qnil;
3707 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3708 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3709 Does not apply if quit is handled by a `condition-case'. */);
3710 debug_on_quit = 0;
3712 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3713 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3715 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3716 doc: /* Non-nil means debugger may continue execution.
3717 This is nil when the debugger is called under circumstances where it
3718 might not be safe to continue. */);
3719 debugger_may_continue = 1;
3721 DEFVAR_LISP ("debugger", Vdebugger,
3722 doc: /* Function to call to invoke debugger.
3723 If due to frame exit, args are `exit' and the value being returned;
3724 this function's value will be returned instead of that.
3725 If due to error, args are `error' and a list of the args to `signal'.
3726 If due to `apply' or `funcall' entry, one arg, `lambda'.
3727 If due to `eval' entry, one arg, t. */);
3728 Vdebugger = Qnil;
3730 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3731 doc: /* If non-nil, this is a function for `signal' to call.
3732 It receives the same arguments that `signal' was given.
3733 The Edebug package uses this to regain control. */);
3734 Vsignal_hook_function = Qnil;
3736 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3737 doc: /* Non-nil means call the debugger regardless of condition handlers.
3738 Note that `debug-on-error', `debug-on-quit' and friends
3739 still determine whether to handle the particular condition. */);
3740 Vdebug_on_signal = Qnil;
3742 /* When lexical binding is being used,
3743 Vinternal_interpreter_environment is non-nil, and contains an alist
3744 of lexically-bound variable, or (t), indicating an empty
3745 environment. The lisp name of this variable would be
3746 `internal-interpreter-environment' if it weren't hidden.
3747 Every element of this list can be either a cons (VAR . VAL)
3748 specifying a lexical binding, or a single symbol VAR indicating
3749 that this variable should use dynamic scoping. */
3750 DEFSYM (Qinternal_interpreter_environment,
3751 "internal-interpreter-environment");
3752 DEFVAR_LISP ("internal-interpreter-environment",
3753 Vinternal_interpreter_environment,
3754 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3755 When lexical binding is not being used, this variable is nil.
3756 A value of `(t)' indicates an empty environment, otherwise it is an
3757 alist of active lexical bindings. */);
3758 Vinternal_interpreter_environment = Qnil;
3759 /* Don't export this variable to Elisp, so no one can mess with it
3760 (Just imagine if someone makes it buffer-local). */
3761 Funintern (Qinternal_interpreter_environment, Qnil);
3763 Vrun_hooks = intern_c_string ("run-hooks");
3764 staticpro (&Vrun_hooks);
3766 staticpro (&Vautoload_queue);
3767 Vautoload_queue = Qnil;
3768 staticpro (&Vsignaling_function);
3769 Vsignaling_function = Qnil;
3771 inhibit_lisp_code = Qnil;
3773 defsubr (&Sor);
3774 defsubr (&Sand);
3775 defsubr (&Sif);
3776 defsubr (&Scond);
3777 defsubr (&Sprogn);
3778 defsubr (&Sprog1);
3779 defsubr (&Sprog2);
3780 defsubr (&Ssetq);
3781 defsubr (&Squote);
3782 defsubr (&Sfunction);
3783 defsubr (&Sdefault_toplevel_value);
3784 defsubr (&Sset_default_toplevel_value);
3785 defsubr (&Sdefvar);
3786 defsubr (&Sdefvaralias);
3787 defsubr (&Sdefconst);
3788 defsubr (&Smake_var_non_special);
3789 defsubr (&Slet);
3790 defsubr (&SletX);
3791 defsubr (&Swhile);
3792 defsubr (&Smacroexpand);
3793 defsubr (&Scatch);
3794 defsubr (&Sthrow);
3795 defsubr (&Sunwind_protect);
3796 defsubr (&Scondition_case);
3797 defsubr (&Ssignal);
3798 defsubr (&Scommandp);
3799 defsubr (&Sautoload);
3800 defsubr (&Sautoload_do_load);
3801 defsubr (&Seval);
3802 defsubr (&Sapply);
3803 defsubr (&Sfuncall);
3804 defsubr (&Srun_hooks);
3805 defsubr (&Srun_hook_with_args);
3806 defsubr (&Srun_hook_with_args_until_success);
3807 defsubr (&Srun_hook_with_args_until_failure);
3808 defsubr (&Srun_hook_wrapped);
3809 defsubr (&Sfetch_bytecode);
3810 defsubr (&Sbacktrace_debug);
3811 defsubr (&Sbacktrace);
3812 defsubr (&Sbacktrace_frame);
3813 defsubr (&Sbacktrace_eval);
3814 defsubr (&Sbacktrace__locals);
3815 defsubr (&Sspecial_variable_p);
3816 defsubr (&Sfunctionp);