Fix debugging of string-match-p errors
[emacs.git] / src / eval.c
blob13a41a2ae200c4e7dddf72e8a6c01931f86b4744
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 we are debugging an error while `inhibit-changing-match-data'
303 is bound to non-nil (e.g., within a call to `string-match-p'),
304 then make sure debugger code can still use match data. */
305 specbind (Qinhibit_changing_match_data, Qnil);
307 #if 0 /* Binding this prevents execution of Lisp code during
308 redisplay, which necessarily leads to display problems. */
309 specbind (Qinhibit_eval_during_redisplay, Qt);
310 #endif
312 val = apply1 (Vdebugger, arg);
314 /* Interrupting redisplay and resuming it later is not safe under
315 all circumstances. So, when the debugger returns, abort the
316 interrupted redisplay by going back to the top-level. */
317 if (debug_while_redisplaying)
318 Ftop_level ();
320 return unbind_to (count, val);
323 static void
324 do_debug_on_call (Lisp_Object code, ptrdiff_t count)
326 debug_on_next_call = 0;
327 set_backtrace_debug_on_exit (specpdl + count, true);
328 call_debugger (list1 (code));
331 /* NOTE!!! Every function that can call EVAL must protect its args
332 and temporaries from garbage collection while it needs them.
333 The definition of `For' shows what you have to do. */
335 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
336 doc: /* Eval args until one of them yields non-nil, then return that value.
337 The remaining args are not evalled at all.
338 If all args return nil, return nil.
339 usage: (or CONDITIONS...) */)
340 (Lisp_Object args)
342 Lisp_Object val = Qnil;
344 while (CONSP (args))
346 val = eval_sub (XCAR (args));
347 if (!NILP (val))
348 break;
349 args = XCDR (args);
352 return val;
355 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
356 doc: /* Eval args until one of them yields nil, then return nil.
357 The remaining args are not evalled at all.
358 If no arg yields nil, return the last arg's value.
359 usage: (and CONDITIONS...) */)
360 (Lisp_Object args)
362 Lisp_Object val = Qt;
364 while (CONSP (args))
366 val = eval_sub (XCAR (args));
367 if (NILP (val))
368 break;
369 args = XCDR (args);
372 return val;
375 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
376 doc: /* If COND yields non-nil, do THEN, else do ELSE...
377 Returns the value of THEN or the value of the last of the ELSE's.
378 THEN must be one expression, but ELSE... can be zero or more expressions.
379 If COND yields nil, and there are no ELSE's, the value is nil.
380 usage: (if COND THEN ELSE...) */)
381 (Lisp_Object args)
383 Lisp_Object cond;
385 cond = eval_sub (XCAR (args));
387 if (!NILP (cond))
388 return eval_sub (Fcar (XCDR (args)));
389 return Fprogn (XCDR (XCDR (args)));
392 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
393 doc: /* Try each clause until one succeeds.
394 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
395 and, if the value is non-nil, this clause succeeds:
396 then the expressions in BODY are evaluated and the last one's
397 value is the value of the cond-form.
398 If a clause has one element, as in (CONDITION), then the cond-form
399 returns CONDITION's value, if that is non-nil.
400 If no clause succeeds, cond returns nil.
401 usage: (cond CLAUSES...) */)
402 (Lisp_Object args)
404 Lisp_Object val = args;
406 while (CONSP (args))
408 Lisp_Object clause = XCAR (args);
409 val = eval_sub (Fcar (clause));
410 if (!NILP (val))
412 if (!NILP (XCDR (clause)))
413 val = Fprogn (XCDR (clause));
414 break;
416 args = XCDR (args);
419 return val;
422 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
423 doc: /* Eval BODY forms sequentially and return value of last one.
424 usage: (progn BODY...) */)
425 (Lisp_Object body)
427 Lisp_Object val = Qnil;
429 while (CONSP (body))
431 val = eval_sub (XCAR (body));
432 body = XCDR (body);
435 return val;
438 /* Evaluate BODY sequentially, discarding its value. Suitable for
439 record_unwind_protect. */
441 void
442 unwind_body (Lisp_Object body)
444 Fprogn (body);
447 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
448 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
449 The value of FIRST is saved during the evaluation of the remaining args,
450 whose values are discarded.
451 usage: (prog1 FIRST BODY...) */)
452 (Lisp_Object args)
454 Lisp_Object val;
455 Lisp_Object args_left;
457 args_left = args;
458 val = args;
460 val = eval_sub (XCAR (args_left));
461 while (CONSP (args_left = XCDR (args_left)))
462 eval_sub (XCAR (args_left));
464 return val;
467 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
468 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
469 The value of FORM2 is saved during the evaluation of the
470 remaining args, whose values are discarded.
471 usage: (prog2 FORM1 FORM2 BODY...) */)
472 (Lisp_Object args)
474 eval_sub (XCAR (args));
475 return Fprog1 (XCDR (args));
478 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
479 doc: /* Set each SYM to the value of its VAL.
480 The symbols SYM are variables; they are literal (not evaluated).
481 The values VAL are expressions; they are evaluated.
482 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
483 The second VAL is not computed until after the first SYM is set, and so on;
484 each VAL can use the new value of variables set earlier in the `setq'.
485 The return value of the `setq' form is the value of the last VAL.
486 usage: (setq [SYM VAL]...) */)
487 (Lisp_Object args)
489 Lisp_Object val, sym, lex_binding;
491 val = args;
492 if (CONSP (args))
494 Lisp_Object args_left = args;
495 Lisp_Object numargs = Flength (args);
497 if (XINT (numargs) & 1)
498 xsignal2 (Qwrong_number_of_arguments, Qsetq, numargs);
502 val = eval_sub (Fcar (XCDR (args_left)));
503 sym = XCAR (args_left);
505 /* Like for eval_sub, we do not check declared_special here since
506 it's been done when let-binding. */
507 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
508 && SYMBOLP (sym)
509 && !NILP (lex_binding
510 = Fassq (sym, Vinternal_interpreter_environment)))
511 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
512 else
513 Fset (sym, val); /* SYM is dynamically bound. */
515 args_left = Fcdr (XCDR (args_left));
517 while (CONSP (args_left));
520 return val;
523 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
524 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
525 Warning: `quote' does not construct its return value, but just returns
526 the value that was pre-constructed by the Lisp reader (see info node
527 `(elisp)Printed Representation').
528 This means that \\='(a . b) is not identical to (cons \\='a \\='b): the former
529 does not cons. Quoting should be reserved for constants that will
530 never be modified by side-effects, unless you like self-modifying code.
531 See the common pitfall in info node `(elisp)Rearrangement' for an example
532 of unexpected results when a quoted object is modified.
533 usage: (quote ARG) */)
534 (Lisp_Object args)
536 if (CONSP (XCDR (args)))
537 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
538 return XCAR (args);
541 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
542 doc: /* Like `quote', but preferred for objects which are functions.
543 In byte compilation, `function' causes its argument to be compiled.
544 `quote' cannot do that.
545 usage: (function ARG) */)
546 (Lisp_Object args)
548 Lisp_Object quoted = XCAR (args);
550 if (CONSP (XCDR (args)))
551 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
553 if (!NILP (Vinternal_interpreter_environment)
554 && CONSP (quoted)
555 && EQ (XCAR (quoted), Qlambda))
556 { /* This is a lambda expression within a lexical environment;
557 return an interpreted closure instead of a simple lambda. */
558 Lisp_Object cdr = XCDR (quoted);
559 Lisp_Object tmp = cdr;
560 if (CONSP (tmp)
561 && (tmp = XCDR (tmp), CONSP (tmp))
562 && (tmp = XCAR (tmp), CONSP (tmp))
563 && (EQ (QCdocumentation, XCAR (tmp))))
564 { /* Handle the special (:documentation <form>) to build the docstring
565 dynamically. */
566 Lisp_Object docstring = eval_sub (Fcar (XCDR (tmp)));
567 CHECK_STRING (docstring);
568 cdr = Fcons (XCAR (cdr), Fcons (docstring, XCDR (XCDR (cdr))));
570 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
571 cdr));
573 else
574 /* Simply quote the argument. */
575 return quoted;
579 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
580 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
581 Aliased variables always have the same value; setting one sets the other.
582 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
583 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
584 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
585 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
586 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
587 The return value is BASE-VARIABLE. */)
588 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
590 struct Lisp_Symbol *sym;
592 CHECK_SYMBOL (new_alias);
593 CHECK_SYMBOL (base_variable);
595 sym = XSYMBOL (new_alias);
597 if (sym->constant)
598 /* Not sure why, but why not? */
599 error ("Cannot make a constant an alias");
601 switch (sym->redirect)
603 case SYMBOL_FORWARDED:
604 error ("Cannot make an internal variable an alias");
605 case SYMBOL_LOCALIZED:
606 error ("Don't know how to make a localized variable an alias");
607 case SYMBOL_PLAINVAL:
608 case SYMBOL_VARALIAS:
609 break;
610 default:
611 emacs_abort ();
614 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
615 If n_a is bound, but b_v is not, set the value of b_v to n_a,
616 so that old-code that affects n_a before the aliasing is setup
617 still works. */
618 if (NILP (Fboundp (base_variable)))
619 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
622 union specbinding *p;
624 for (p = specpdl_ptr; p > specpdl; )
625 if ((--p)->kind >= SPECPDL_LET
626 && (EQ (new_alias, specpdl_symbol (p))))
627 error ("Don't know how to make a let-bound variable an alias");
630 sym->declared_special = 1;
631 XSYMBOL (base_variable)->declared_special = 1;
632 sym->redirect = SYMBOL_VARALIAS;
633 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
634 sym->constant = SYMBOL_CONSTANT_P (base_variable);
635 LOADHIST_ATTACH (new_alias);
636 /* Even if docstring is nil: remove old docstring. */
637 Fput (new_alias, Qvariable_documentation, docstring);
639 return base_variable;
642 static union specbinding *
643 default_toplevel_binding (Lisp_Object symbol)
645 union specbinding *binding = NULL;
646 union specbinding *pdl = specpdl_ptr;
647 while (pdl > specpdl)
649 switch ((--pdl)->kind)
651 case SPECPDL_LET_DEFAULT:
652 case SPECPDL_LET:
653 if (EQ (specpdl_symbol (pdl), symbol))
654 binding = pdl;
655 break;
657 case SPECPDL_UNWIND:
658 case SPECPDL_UNWIND_PTR:
659 case SPECPDL_UNWIND_INT:
660 case SPECPDL_UNWIND_VOID:
661 case SPECPDL_BACKTRACE:
662 case SPECPDL_LET_LOCAL:
663 break;
665 default:
666 emacs_abort ();
669 return binding;
672 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
673 doc: /* Return SYMBOL's toplevel default value.
674 "Toplevel" means outside of any let binding. */)
675 (Lisp_Object symbol)
677 union specbinding *binding = default_toplevel_binding (symbol);
678 Lisp_Object value
679 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
680 if (!EQ (value, Qunbound))
681 return value;
682 xsignal1 (Qvoid_variable, symbol);
685 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
686 Sset_default_toplevel_value, 2, 2, 0,
687 doc: /* Set SYMBOL's toplevel default value to VALUE.
688 "Toplevel" means outside of any let binding. */)
689 (Lisp_Object symbol, Lisp_Object value)
691 union specbinding *binding = default_toplevel_binding (symbol);
692 if (binding)
693 set_specpdl_old_value (binding, value);
694 else
695 Fset_default (symbol, value);
696 return Qnil;
699 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
700 doc: /* Define SYMBOL as a variable, and return SYMBOL.
701 You are not required to define a variable in order to use it, but
702 defining it lets you supply an initial value and documentation, which
703 can be referred to by the Emacs help facilities and other programming
704 tools. The `defvar' form also declares the variable as \"special\",
705 so that it is always dynamically bound even if `lexical-binding' is t.
707 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
708 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
709 default value is what is set; buffer-local values are not affected.
710 If INITVALUE is missing, SYMBOL's value is not set.
712 If SYMBOL has a local binding, then this form affects the local
713 binding. This is usually not what you want. Thus, if you need to
714 load a file defining variables, with this form or with `defconst' or
715 `defcustom', you should always load that file _outside_ any bindings
716 for these variables. (`defconst' and `defcustom' behave similarly in
717 this respect.)
719 The optional argument DOCSTRING is a documentation string for the
720 variable.
722 To define a user option, use `defcustom' instead of `defvar'.
723 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
724 (Lisp_Object args)
726 Lisp_Object sym, tem, tail;
728 sym = XCAR (args);
729 tail = XCDR (args);
731 if (CONSP (tail))
733 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
734 error ("Too many arguments");
736 tem = Fdefault_boundp (sym);
738 /* Do it before evaluating the initial value, for self-references. */
739 XSYMBOL (sym)->declared_special = 1;
741 if (NILP (tem))
742 Fset_default (sym, eval_sub (XCAR (tail)));
743 else
744 { /* Check if there is really a global binding rather than just a let
745 binding that shadows the global unboundness of the var. */
746 union specbinding *binding = default_toplevel_binding (sym);
747 if (binding && EQ (specpdl_old_value (binding), Qunbound))
749 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
752 tail = XCDR (tail);
753 tem = Fcar (tail);
754 if (!NILP (tem))
756 if (!NILP (Vpurify_flag))
757 tem = Fpurecopy (tem);
758 Fput (sym, Qvariable_documentation, tem);
760 LOADHIST_ATTACH (sym);
762 else if (!NILP (Vinternal_interpreter_environment)
763 && !XSYMBOL (sym)->declared_special)
764 /* A simple (defvar foo) with lexical scoping does "nothing" except
765 declare that var to be dynamically scoped *locally* (i.e. within
766 the current file or let-block). */
767 Vinternal_interpreter_environment
768 = Fcons (sym, Vinternal_interpreter_environment);
769 else
771 /* Simple (defvar <var>) should not count as a definition at all.
772 It could get in the way of other definitions, and unloading this
773 package could try to make the variable unbound. */
776 return sym;
779 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
780 doc: /* Define SYMBOL as a constant variable.
781 This declares that neither programs nor users should ever change the
782 value. This constancy is not actually enforced by Emacs Lisp, but
783 SYMBOL is marked as a special variable so that it is never lexically
784 bound.
786 The `defconst' form always sets the value of SYMBOL to the result of
787 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
788 what is set; buffer-local values are not affected. If SYMBOL has a
789 local binding, then this form sets the local binding's value.
790 However, you should normally not make local bindings for variables
791 defined with this form.
793 The optional DOCSTRING specifies the variable's documentation string.
794 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
795 (Lisp_Object args)
797 Lisp_Object sym, tem;
799 sym = XCAR (args);
800 if (CONSP (Fcdr (XCDR (XCDR (args)))))
801 error ("Too many arguments");
803 tem = eval_sub (Fcar (XCDR (args)));
804 if (!NILP (Vpurify_flag))
805 tem = Fpurecopy (tem);
806 Fset_default (sym, tem);
807 XSYMBOL (sym)->declared_special = 1;
808 tem = Fcar (XCDR (XCDR (args)));
809 if (!NILP (tem))
811 if (!NILP (Vpurify_flag))
812 tem = Fpurecopy (tem);
813 Fput (sym, Qvariable_documentation, tem);
815 Fput (sym, Qrisky_local_variable, Qt);
816 LOADHIST_ATTACH (sym);
817 return sym;
820 /* Make SYMBOL lexically scoped. */
821 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
822 Smake_var_non_special, 1, 1, 0,
823 doc: /* Internal function. */)
824 (Lisp_Object symbol)
826 CHECK_SYMBOL (symbol);
827 XSYMBOL (symbol)->declared_special = 0;
828 return Qnil;
832 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
833 doc: /* Bind variables according to VARLIST then eval BODY.
834 The value of the last form in BODY is returned.
835 Each element of VARLIST is a symbol (which is bound to nil)
836 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
837 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
838 usage: (let* VARLIST BODY...) */)
839 (Lisp_Object args)
841 Lisp_Object varlist, var, val, elt, lexenv;
842 ptrdiff_t count = SPECPDL_INDEX ();
844 lexenv = Vinternal_interpreter_environment;
846 varlist = XCAR (args);
847 while (CONSP (varlist))
849 QUIT;
851 elt = XCAR (varlist);
852 if (SYMBOLP (elt))
854 var = elt;
855 val = Qnil;
857 else if (! NILP (Fcdr (Fcdr (elt))))
858 signal_error ("`let' bindings can have only one value-form", elt);
859 else
861 var = Fcar (elt);
862 val = eval_sub (Fcar (Fcdr (elt)));
865 if (!NILP (lexenv) && SYMBOLP (var)
866 && !XSYMBOL (var)->declared_special
867 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
868 /* Lexically bind VAR by adding it to the interpreter's binding
869 alist. */
871 Lisp_Object newenv
872 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
873 if (EQ (Vinternal_interpreter_environment, lexenv))
874 /* Save the old lexical environment on the specpdl stack,
875 but only for the first lexical binding, since we'll never
876 need to revert to one of the intermediate ones. */
877 specbind (Qinternal_interpreter_environment, newenv);
878 else
879 Vinternal_interpreter_environment = newenv;
881 else
882 specbind (var, val);
884 varlist = XCDR (varlist);
887 val = Fprogn (XCDR (args));
888 return unbind_to (count, val);
891 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
892 doc: /* Bind variables according to VARLIST then eval BODY.
893 The value of the last form in BODY is returned.
894 Each element of VARLIST is a symbol (which is bound to nil)
895 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
896 All the VALUEFORMs are evalled before any symbols are bound.
897 usage: (let VARLIST BODY...) */)
898 (Lisp_Object args)
900 Lisp_Object *temps, tem, lexenv;
901 Lisp_Object elt, varlist;
902 ptrdiff_t count = SPECPDL_INDEX ();
903 ptrdiff_t argnum;
904 USE_SAFE_ALLOCA;
906 varlist = XCAR (args);
908 /* Make space to hold the values to give the bound variables. */
909 elt = Flength (varlist);
910 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
912 /* Compute the values and store them in `temps'. */
914 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
916 QUIT;
917 elt = XCAR (varlist);
918 if (SYMBOLP (elt))
919 temps [argnum++] = Qnil;
920 else if (! NILP (Fcdr (Fcdr (elt))))
921 signal_error ("`let' bindings can have only one value-form", elt);
922 else
923 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
926 lexenv = Vinternal_interpreter_environment;
928 varlist = XCAR (args);
929 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
931 Lisp_Object var;
933 elt = XCAR (varlist);
934 var = SYMBOLP (elt) ? elt : Fcar (elt);
935 tem = temps[argnum++];
937 if (!NILP (lexenv) && SYMBOLP (var)
938 && !XSYMBOL (var)->declared_special
939 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
940 /* Lexically bind VAR by adding it to the lexenv alist. */
941 lexenv = Fcons (Fcons (var, tem), lexenv);
942 else
943 /* Dynamically bind VAR. */
944 specbind (var, tem);
947 if (!EQ (lexenv, Vinternal_interpreter_environment))
948 /* Instantiate a new lexical environment. */
949 specbind (Qinternal_interpreter_environment, lexenv);
951 elt = Fprogn (XCDR (args));
952 SAFE_FREE ();
953 return unbind_to (count, elt);
956 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
957 doc: /* If TEST yields non-nil, eval BODY... and repeat.
958 The order of execution is thus TEST, BODY, TEST, BODY and so on
959 until TEST returns nil.
960 usage: (while TEST BODY...) */)
961 (Lisp_Object args)
963 Lisp_Object test, body;
965 test = XCAR (args);
966 body = XCDR (args);
967 while (!NILP (eval_sub (test)))
969 QUIT;
970 Fprogn (body);
973 return Qnil;
976 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
977 doc: /* Return result of expanding macros at top level of FORM.
978 If FORM is not a macro call, it is returned unchanged.
979 Otherwise, the macro is expanded and the expansion is considered
980 in place of FORM. When a non-macro-call results, it is returned.
982 The second optional arg ENVIRONMENT specifies an environment of macro
983 definitions to shadow the loaded ones for use in file byte-compilation. */)
984 (Lisp_Object form, Lisp_Object environment)
986 /* With cleanups from Hallvard Furuseth. */
987 register Lisp_Object expander, sym, def, tem;
989 while (1)
991 /* Come back here each time we expand a macro call,
992 in case it expands into another macro call. */
993 if (!CONSP (form))
994 break;
995 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
996 def = sym = XCAR (form);
997 tem = Qnil;
998 /* Trace symbols aliases to other symbols
999 until we get a symbol that is not an alias. */
1000 while (SYMBOLP (def))
1002 QUIT;
1003 sym = def;
1004 tem = Fassq (sym, environment);
1005 if (NILP (tem))
1007 def = XSYMBOL (sym)->function;
1008 if (!NILP (def))
1009 continue;
1011 break;
1013 /* Right now TEM is the result from SYM in ENVIRONMENT,
1014 and if TEM is nil then DEF is SYM's function definition. */
1015 if (NILP (tem))
1017 /* SYM is not mentioned in ENVIRONMENT.
1018 Look at its function definition. */
1019 def = Fautoload_do_load (def, sym, Qmacro);
1020 if (!CONSP (def))
1021 /* Not defined or definition not suitable. */
1022 break;
1023 if (!EQ (XCAR (def), Qmacro))
1024 break;
1025 else expander = XCDR (def);
1027 else
1029 expander = XCDR (tem);
1030 if (NILP (expander))
1031 break;
1034 Lisp_Object newform = apply1 (expander, XCDR (form));
1035 if (EQ (form, newform))
1036 break;
1037 else
1038 form = newform;
1041 return form;
1044 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1045 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1046 TAG is evalled to get the tag to use; it must not be nil.
1048 Then the BODY is executed.
1049 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1050 If no throw happens, `catch' returns the value of the last BODY form.
1051 If a throw happens, it specifies the value to return from `catch'.
1052 usage: (catch TAG BODY...) */)
1053 (Lisp_Object args)
1055 Lisp_Object tag = eval_sub (XCAR (args));
1056 return internal_catch (tag, Fprogn, XCDR (args));
1059 /* Assert that E is true, as a comment only. Use this instead of
1060 eassert (E) when E contains variables that might be clobbered by a
1061 longjmp. */
1063 #define clobbered_eassert(E) ((void) 0)
1065 /* Set up a catch, then call C function FUNC on argument ARG.
1066 FUNC should return a Lisp_Object.
1067 This is how catches are done from within C code. */
1069 Lisp_Object
1070 internal_catch (Lisp_Object tag,
1071 Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1073 /* This structure is made part of the chain `catchlist'. */
1074 struct handler *c = push_handler (tag, CATCHER);
1076 /* Call FUNC. */
1077 if (! sys_setjmp (c->jmp))
1079 Lisp_Object val = func (arg);
1080 clobbered_eassert (handlerlist == c);
1081 handlerlist = handlerlist->next;
1082 return val;
1084 else
1085 { /* Throw works by a longjmp that comes right here. */
1086 Lisp_Object val = handlerlist->val;
1087 clobbered_eassert (handlerlist == c);
1088 handlerlist = handlerlist->next;
1089 return val;
1093 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1094 jump to that CATCH, returning VALUE as the value of that catch.
1096 This is the guts of Fthrow and Fsignal; they differ only in the way
1097 they choose the catch tag to throw to. A catch tag for a
1098 condition-case form has a TAG of Qnil.
1100 Before each catch is discarded, unbind all special bindings and
1101 execute all unwind-protect clauses made above that catch. Unwind
1102 the handler stack as we go, so that the proper handlers are in
1103 effect for each unwind-protect clause we run. At the end, restore
1104 some static info saved in CATCH, and longjmp to the location
1105 specified there.
1107 This is used for correct unwinding in Fthrow and Fsignal. */
1109 static _Noreturn void
1110 unwind_to_catch (struct handler *catch, Lisp_Object value)
1112 bool last_time;
1114 eassert (catch->next);
1116 /* Save the value in the tag. */
1117 catch->val = value;
1119 /* Restore certain special C variables. */
1120 set_poll_suppress_count (catch->poll_suppress_count);
1121 unblock_input_to (catch->interrupt_input_blocked);
1122 immediate_quit = 0;
1126 /* Unwind the specpdl stack, and then restore the proper set of
1127 handlers. */
1128 unbind_to (handlerlist->pdlcount, Qnil);
1129 last_time = handlerlist == catch;
1130 if (! last_time)
1131 handlerlist = handlerlist->next;
1133 while (! last_time);
1135 eassert (handlerlist == catch);
1137 byte_stack_list = catch->byte_stack;
1138 lisp_eval_depth = catch->lisp_eval_depth;
1140 sys_longjmp (catch->jmp, 1);
1143 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1144 doc: /* Throw to the catch for TAG and return VALUE from it.
1145 Both TAG and VALUE are evalled. */
1146 attributes: noreturn)
1147 (register Lisp_Object tag, Lisp_Object value)
1149 struct handler *c;
1151 if (!NILP (tag))
1152 for (c = handlerlist; c; c = c->next)
1154 if (c->type == CATCHER_ALL)
1155 unwind_to_catch (c, Fcons (tag, value));
1156 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1157 unwind_to_catch (c, value);
1159 xsignal2 (Qno_catch, tag, value);
1163 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1164 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1165 If BODYFORM completes normally, its value is returned
1166 after executing the UNWINDFORMS.
1167 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1168 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1169 (Lisp_Object args)
1171 Lisp_Object val;
1172 ptrdiff_t count = SPECPDL_INDEX ();
1174 record_unwind_protect (unwind_body, XCDR (args));
1175 val = eval_sub (XCAR (args));
1176 return unbind_to (count, val);
1179 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1180 doc: /* Regain control when an error is signaled.
1181 Executes BODYFORM and returns its value if no error happens.
1182 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1183 where the BODY is made of Lisp expressions.
1185 A handler is applicable to an error
1186 if CONDITION-NAME is one of the error's condition names.
1187 If an error happens, the first applicable handler is run.
1189 The car of a handler may be a list of condition names instead of a
1190 single condition name; then it handles all of them. If the special
1191 condition name `debug' is present in this list, it allows another
1192 condition in the list to run the debugger if `debug-on-error' and the
1193 other usual mechanisms says it should (otherwise, `condition-case'
1194 suppresses the debugger).
1196 When a handler handles an error, control returns to the `condition-case'
1197 and it executes the handler's BODY...
1198 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1199 \(If VAR is nil, the handler can't access that information.)
1200 Then the value of the last BODY form is returned from the `condition-case'
1201 expression.
1203 See also the function `signal' for more info.
1204 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1205 (Lisp_Object args)
1207 Lisp_Object var = XCAR (args);
1208 Lisp_Object bodyform = XCAR (XCDR (args));
1209 Lisp_Object handlers = XCDR (XCDR (args));
1211 return internal_lisp_condition_case (var, bodyform, handlers);
1214 /* Like Fcondition_case, but the args are separate
1215 rather than passed in a list. Used by Fbyte_code. */
1217 Lisp_Object
1218 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1219 Lisp_Object handlers)
1221 Lisp_Object val;
1222 struct handler *oldhandlerlist = handlerlist;
1223 int clausenb = 0;
1225 CHECK_SYMBOL (var);
1227 for (val = handlers; CONSP (val); val = XCDR (val))
1229 Lisp_Object tem = XCAR (val);
1230 clausenb++;
1231 if (! (NILP (tem)
1232 || (CONSP (tem)
1233 && (SYMBOLP (XCAR (tem))
1234 || CONSP (XCAR (tem))))))
1235 error ("Invalid condition handler: %s",
1236 SDATA (Fprin1_to_string (tem, Qt)));
1239 { /* The first clause is the one that should be checked first, so it should
1240 be added to handlerlist last. So we build in `clauses' a table that
1241 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1242 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1243 if (MAX_ALLOCA / word_size < clausenb)
1244 memory_full (SIZE_MAX);
1245 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1246 Lisp_Object *volatile clauses_volatile = clauses;
1247 int i = clausenb;
1248 for (val = handlers; CONSP (val); val = XCDR (val))
1249 clauses[--i] = XCAR (val);
1250 for (i = 0; i < clausenb; i++)
1252 Lisp_Object clause = clauses[i];
1253 Lisp_Object condition = CONSP (clause) ? XCAR (clause) : Qnil;
1254 if (!CONSP (condition))
1255 condition = Fcons (condition, Qnil);
1256 struct handler *c = push_handler (condition, CONDITION_CASE);
1257 if (sys_setjmp (c->jmp))
1259 ptrdiff_t count = SPECPDL_INDEX ();
1260 Lisp_Object val = handlerlist->val;
1261 Lisp_Object *chosen_clause = clauses_volatile;
1262 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1263 chosen_clause++;
1264 handlerlist = oldhandlerlist;
1265 if (!NILP (var))
1267 if (!NILP (Vinternal_interpreter_environment))
1268 specbind (Qinternal_interpreter_environment,
1269 Fcons (Fcons (var, val),
1270 Vinternal_interpreter_environment));
1271 else
1272 specbind (var, val);
1274 val = Fprogn (XCDR (*chosen_clause));
1275 /* Note that this just undoes the binding of var; whoever
1276 longjumped to us unwound the stack to c.pdlcount before
1277 throwing. */
1278 if (!NILP (var))
1279 unbind_to (count, Qnil);
1280 return val;
1285 val = eval_sub (bodyform);
1286 handlerlist = oldhandlerlist;
1287 return val;
1290 /* Call the function BFUN with no arguments, catching errors within it
1291 according to HANDLERS. If there is an error, call HFUN with
1292 one argument which is the data that describes the error:
1293 (SIGNALNAME . DATA)
1295 HANDLERS can be a list of conditions to catch.
1296 If HANDLERS is Qt, catch all errors.
1297 If HANDLERS is Qerror, catch all errors
1298 but allow the debugger to run if that is enabled. */
1300 Lisp_Object
1301 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1302 Lisp_Object (*hfun) (Lisp_Object))
1304 struct handler *c = push_handler (handlers, CONDITION_CASE);
1305 if (sys_setjmp (c->jmp))
1307 Lisp_Object val = handlerlist->val;
1308 clobbered_eassert (handlerlist == c);
1309 handlerlist = handlerlist->next;
1310 return hfun (val);
1312 else
1314 Lisp_Object val = bfun ();
1315 clobbered_eassert (handlerlist == c);
1316 handlerlist = handlerlist->next;
1317 return val;
1321 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1323 Lisp_Object
1324 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1325 Lisp_Object handlers,
1326 Lisp_Object (*hfun) (Lisp_Object))
1328 struct handler *c = push_handler (handlers, CONDITION_CASE);
1329 if (sys_setjmp (c->jmp))
1331 Lisp_Object val = handlerlist->val;
1332 clobbered_eassert (handlerlist == c);
1333 handlerlist = handlerlist->next;
1334 return hfun (val);
1336 else
1338 Lisp_Object val = bfun (arg);
1339 clobbered_eassert (handlerlist == c);
1340 handlerlist = handlerlist->next;
1341 return val;
1345 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1346 its arguments. */
1348 Lisp_Object
1349 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1350 Lisp_Object arg1,
1351 Lisp_Object arg2,
1352 Lisp_Object handlers,
1353 Lisp_Object (*hfun) (Lisp_Object))
1355 struct handler *c = push_handler (handlers, CONDITION_CASE);
1356 if (sys_setjmp (c->jmp))
1358 Lisp_Object val = handlerlist->val;
1359 clobbered_eassert (handlerlist == c);
1360 handlerlist = handlerlist->next;
1361 return hfun (val);
1363 else
1365 Lisp_Object val = bfun (arg1, arg2);
1366 clobbered_eassert (handlerlist == c);
1367 handlerlist = handlerlist->next;
1368 return val;
1372 /* Like internal_condition_case but call BFUN with NARGS as first,
1373 and ARGS as second argument. */
1375 Lisp_Object
1376 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1377 ptrdiff_t nargs,
1378 Lisp_Object *args,
1379 Lisp_Object handlers,
1380 Lisp_Object (*hfun) (Lisp_Object err,
1381 ptrdiff_t nargs,
1382 Lisp_Object *args))
1384 struct handler *c = push_handler (handlers, CONDITION_CASE);
1385 if (sys_setjmp (c->jmp))
1387 Lisp_Object val = handlerlist->val;
1388 clobbered_eassert (handlerlist == c);
1389 handlerlist = handlerlist->next;
1390 return hfun (val, nargs, args);
1392 else
1394 Lisp_Object val = bfun (nargs, args);
1395 clobbered_eassert (handlerlist == c);
1396 handlerlist = handlerlist->next;
1397 return val;
1401 struct handler *
1402 push_handler (Lisp_Object tag_ch_val, enum handlertype handlertype)
1404 struct handler *c = push_handler_nosignal (tag_ch_val, handlertype);
1405 if (!c)
1406 memory_full (sizeof *c);
1407 return c;
1410 struct handler *
1411 push_handler_nosignal (Lisp_Object tag_ch_val, enum handlertype handlertype)
1413 struct handler *c = handlerlist->nextfree;
1414 if (!c)
1416 c = malloc (sizeof *c);
1417 if (!c)
1418 return c;
1419 if (profiler_memory_running)
1420 malloc_probe (sizeof *c);
1421 c->nextfree = NULL;
1422 handlerlist->nextfree = c;
1424 c->type = handlertype;
1425 c->tag_or_ch = tag_ch_val;
1426 c->val = Qnil;
1427 c->next = handlerlist;
1428 c->lisp_eval_depth = lisp_eval_depth;
1429 c->pdlcount = SPECPDL_INDEX ();
1430 c->poll_suppress_count = poll_suppress_count;
1431 c->interrupt_input_blocked = interrupt_input_blocked;
1432 c->byte_stack = byte_stack_list;
1433 handlerlist = c;
1434 return c;
1438 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1439 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1440 Lisp_Object data);
1442 void
1443 process_quit_flag (void)
1445 Lisp_Object flag = Vquit_flag;
1446 Vquit_flag = Qnil;
1447 if (EQ (flag, Qkill_emacs))
1448 Fkill_emacs (Qnil);
1449 if (EQ (Vthrow_on_input, flag))
1450 Fthrow (Vthrow_on_input, Qt);
1451 Fsignal (Qquit, Qnil);
1454 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1455 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1456 This function does not return.
1458 An error symbol is a symbol with an `error-conditions' property
1459 that is a list of condition names.
1460 A handler for any of those names will get to handle this signal.
1461 The symbol `error' should normally be one of them.
1463 DATA should be a list. Its elements are printed as part of the error message.
1464 See Info anchor `(elisp)Definition of signal' for some details on how this
1465 error message is constructed.
1466 If the signal is handled, DATA is made available to the handler.
1467 See also the function `condition-case'. */)
1468 (Lisp_Object error_symbol, Lisp_Object data)
1470 /* When memory is full, ERROR-SYMBOL is nil,
1471 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1472 That is a special case--don't do this in other situations. */
1473 Lisp_Object conditions;
1474 Lisp_Object string;
1475 Lisp_Object real_error_symbol
1476 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1477 register Lisp_Object clause = Qnil;
1478 struct handler *h;
1480 immediate_quit = 0;
1481 abort_on_gc = 0;
1482 if (gc_in_progress || waiting_for_input)
1483 emacs_abort ();
1485 #if 0 /* rms: I don't know why this was here,
1486 but it is surely wrong for an error that is handled. */
1487 #ifdef HAVE_WINDOW_SYSTEM
1488 if (display_hourglass_p)
1489 cancel_hourglass ();
1490 #endif
1491 #endif
1493 /* This hook is used by edebug. */
1494 if (! NILP (Vsignal_hook_function)
1495 && ! NILP (error_symbol))
1497 /* Edebug takes care of restoring these variables when it exits. */
1498 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1499 max_lisp_eval_depth = lisp_eval_depth + 20;
1501 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1502 max_specpdl_size = SPECPDL_INDEX () + 40;
1504 call2 (Vsignal_hook_function, error_symbol, data);
1507 conditions = Fget (real_error_symbol, Qerror_conditions);
1509 /* Remember from where signal was called. Skip over the frame for
1510 `signal' itself. If a frame for `error' follows, skip that,
1511 too. Don't do this when ERROR_SYMBOL is nil, because that
1512 is a memory-full error. */
1513 Vsignaling_function = Qnil;
1514 if (!NILP (error_symbol))
1516 union specbinding *pdl = backtrace_next (backtrace_top ());
1517 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1518 pdl = backtrace_next (pdl);
1519 if (backtrace_p (pdl))
1520 Vsignaling_function = backtrace_function (pdl);
1523 for (h = handlerlist; h; h = h->next)
1525 if (h->type != CONDITION_CASE)
1526 continue;
1527 clause = find_handler_clause (h->tag_or_ch, conditions);
1528 if (!NILP (clause))
1529 break;
1532 if (/* Don't run the debugger for a memory-full error.
1533 (There is no room in memory to do that!) */
1534 !NILP (error_symbol)
1535 && (!NILP (Vdebug_on_signal)
1536 /* If no handler is present now, try to run the debugger. */
1537 || NILP (clause)
1538 /* A `debug' symbol in the handler list disables the normal
1539 suppression of the debugger. */
1540 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1541 /* Special handler that means "print a message and run debugger
1542 if requested". */
1543 || EQ (h->tag_or_ch, Qerror)))
1545 bool debugger_called
1546 = maybe_call_debugger (conditions, error_symbol, data);
1547 /* We can't return values to code which signaled an error, but we
1548 can continue code which has signaled a quit. */
1549 if (debugger_called && EQ (real_error_symbol, Qquit))
1550 return Qnil;
1553 if (!NILP (clause))
1555 Lisp_Object unwind_data
1556 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1558 unwind_to_catch (h, unwind_data);
1560 else
1562 if (handlerlist != &handlerlist_sentinel)
1563 /* FIXME: This will come right back here if there's no `top-level'
1564 catcher. A better solution would be to abort here, and instead
1565 add a catch-all condition handler so we never come here. */
1566 Fthrow (Qtop_level, Qt);
1569 if (! NILP (error_symbol))
1570 data = Fcons (error_symbol, data);
1572 string = Ferror_message_string (data);
1573 fatal ("%s", SDATA (string));
1576 /* Internal version of Fsignal that never returns.
1577 Used for anything but Qquit (which can return from Fsignal). */
1579 void
1580 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1582 Fsignal (error_symbol, data);
1583 emacs_abort ();
1586 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1588 void
1589 xsignal0 (Lisp_Object error_symbol)
1591 xsignal (error_symbol, Qnil);
1594 void
1595 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1597 xsignal (error_symbol, list1 (arg));
1600 void
1601 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1603 xsignal (error_symbol, list2 (arg1, arg2));
1606 void
1607 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1609 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1612 /* Signal `error' with message S, and additional arg ARG.
1613 If ARG is not a genuine list, make it a one-element list. */
1615 void
1616 signal_error (const char *s, Lisp_Object arg)
1618 Lisp_Object tortoise, hare;
1620 hare = tortoise = arg;
1621 while (CONSP (hare))
1623 hare = XCDR (hare);
1624 if (!CONSP (hare))
1625 break;
1627 hare = XCDR (hare);
1628 tortoise = XCDR (tortoise);
1630 if (EQ (hare, tortoise))
1631 break;
1634 if (!NILP (hare))
1635 arg = list1 (arg);
1637 xsignal (Qerror, Fcons (build_string (s), arg));
1641 /* Return true if LIST is a non-nil atom or
1642 a list containing one of CONDITIONS. */
1644 static bool
1645 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1647 if (NILP (list))
1648 return 0;
1649 if (! CONSP (list))
1650 return 1;
1652 while (CONSP (conditions))
1654 Lisp_Object this, tail;
1655 this = XCAR (conditions);
1656 for (tail = list; CONSP (tail); tail = XCDR (tail))
1657 if (EQ (XCAR (tail), this))
1658 return 1;
1659 conditions = XCDR (conditions);
1661 return 0;
1664 /* Return true if an error with condition-symbols CONDITIONS,
1665 and described by SIGNAL-DATA, should skip the debugger
1666 according to debugger-ignored-errors. */
1668 static bool
1669 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1671 Lisp_Object tail;
1672 bool first_string = 1;
1673 Lisp_Object error_message;
1675 error_message = Qnil;
1676 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1678 if (STRINGP (XCAR (tail)))
1680 if (first_string)
1682 error_message = Ferror_message_string (data);
1683 first_string = 0;
1686 if (fast_string_match (XCAR (tail), error_message) >= 0)
1687 return 1;
1689 else
1691 Lisp_Object contail;
1693 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1694 if (EQ (XCAR (tail), XCAR (contail)))
1695 return 1;
1699 return 0;
1702 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1703 SIG and DATA describe the signal. There are two ways to pass them:
1704 = SIG is the error symbol, and DATA is the rest of the data.
1705 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1706 This is for memory-full errors only. */
1707 static bool
1708 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1710 Lisp_Object combined_data;
1712 combined_data = Fcons (sig, data);
1714 if (
1715 /* Don't try to run the debugger with interrupts blocked.
1716 The editing loop would return anyway. */
1717 ! input_blocked_p ()
1718 && NILP (Vinhibit_debugger)
1719 /* Does user want to enter debugger for this kind of error? */
1720 && (EQ (sig, Qquit)
1721 ? debug_on_quit
1722 : wants_debugger (Vdebug_on_error, conditions))
1723 && ! skip_debugger (conditions, combined_data)
1724 /* RMS: What's this for? */
1725 && when_entered_debugger < num_nonmacro_input_events)
1727 call_debugger (list2 (Qerror, combined_data));
1728 return 1;
1731 return 0;
1734 static Lisp_Object
1735 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1737 register Lisp_Object h;
1739 /* t is used by handlers for all conditions, set up by C code. */
1740 if (EQ (handlers, Qt))
1741 return Qt;
1743 /* error is used similarly, but means print an error message
1744 and run the debugger if that is enabled. */
1745 if (EQ (handlers, Qerror))
1746 return Qt;
1748 for (h = handlers; CONSP (h); h = XCDR (h))
1750 Lisp_Object handler = XCAR (h);
1751 if (!NILP (Fmemq (handler, conditions)))
1752 return handlers;
1755 return Qnil;
1759 /* Dump an error message; called like vprintf. */
1760 void
1761 verror (const char *m, va_list ap)
1763 char buf[4000];
1764 ptrdiff_t size = sizeof buf;
1765 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1766 char *buffer = buf;
1767 ptrdiff_t used;
1768 Lisp_Object string;
1770 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1771 string = make_string (buffer, used);
1772 if (buffer != buf)
1773 xfree (buffer);
1775 xsignal1 (Qerror, string);
1779 /* Dump an error message; called like printf. */
1781 /* VARARGS 1 */
1782 void
1783 error (const char *m, ...)
1785 va_list ap;
1786 va_start (ap, m);
1787 verror (m, ap);
1790 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1791 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1792 This means it contains a description for how to read arguments to give it.
1793 The value is nil for an invalid function or a symbol with no function
1794 definition.
1796 Interactively callable functions include strings and vectors (treated
1797 as keyboard macros), lambda-expressions that contain a top-level call
1798 to `interactive', autoload definitions made by `autoload' with non-nil
1799 fourth argument, and some of the built-in functions of Lisp.
1801 Also, a symbol satisfies `commandp' if its function definition does so.
1803 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1804 then strings and vectors are not accepted. */)
1805 (Lisp_Object function, Lisp_Object for_call_interactively)
1807 register Lisp_Object fun;
1808 register Lisp_Object funcar;
1809 Lisp_Object if_prop = Qnil;
1811 fun = function;
1813 fun = indirect_function (fun); /* Check cycles. */
1814 if (NILP (fun))
1815 return Qnil;
1817 /* Check an `interactive-form' property if present, analogous to the
1818 function-documentation property. */
1819 fun = function;
1820 while (SYMBOLP (fun))
1822 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1823 if (!NILP (tmp))
1824 if_prop = Qt;
1825 fun = Fsymbol_function (fun);
1828 /* Emacs primitives are interactive if their DEFUN specifies an
1829 interactive spec. */
1830 if (SUBRP (fun))
1831 return XSUBR (fun)->intspec ? Qt : if_prop;
1833 /* Bytecode objects are interactive if they are long enough to
1834 have an element whose index is COMPILED_INTERACTIVE, which is
1835 where the interactive spec is stored. */
1836 else if (COMPILEDP (fun))
1837 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1838 ? Qt : if_prop);
1840 /* Strings and vectors are keyboard macros. */
1841 if (STRINGP (fun) || VECTORP (fun))
1842 return (NILP (for_call_interactively) ? Qt : Qnil);
1844 /* Lists may represent commands. */
1845 if (!CONSP (fun))
1846 return Qnil;
1847 funcar = XCAR (fun);
1848 if (EQ (funcar, Qclosure))
1849 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1850 ? Qt : if_prop);
1851 else if (EQ (funcar, Qlambda))
1852 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1853 else if (EQ (funcar, Qautoload))
1854 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1855 else
1856 return Qnil;
1859 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1860 doc: /* Define FUNCTION to autoload from FILE.
1861 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1862 Third arg DOCSTRING is documentation for the function.
1863 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1864 Fifth arg TYPE indicates the type of the object:
1865 nil or omitted says FUNCTION is a function,
1866 `keymap' says FUNCTION is really a keymap, and
1867 `macro' or t says FUNCTION is really a macro.
1868 Third through fifth args give info about the real definition.
1869 They default to nil.
1870 If FUNCTION is already defined other than as an autoload,
1871 this does nothing and returns nil. */)
1872 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1874 CHECK_SYMBOL (function);
1875 CHECK_STRING (file);
1877 /* If function is defined and not as an autoload, don't override. */
1878 if (!NILP (XSYMBOL (function)->function)
1879 && !AUTOLOADP (XSYMBOL (function)->function))
1880 return Qnil;
1882 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1883 /* `read1' in lread.c has found the docstring starting with "\
1884 and assumed the docstring will be provided by Snarf-documentation, so it
1885 passed us 0 instead. But that leads to accidental sharing in purecopy's
1886 hash-consing, so we use a (hopefully) unique integer instead. */
1887 docstring = make_number (XHASH (function));
1888 return Fdefalias (function,
1889 list5 (Qautoload, file, docstring, interactive, type),
1890 Qnil);
1893 void
1894 un_autoload (Lisp_Object oldqueue)
1896 Lisp_Object queue, first, second;
1898 /* Queue to unwind is current value of Vautoload_queue.
1899 oldqueue is the shadowed value to leave in Vautoload_queue. */
1900 queue = Vautoload_queue;
1901 Vautoload_queue = oldqueue;
1902 while (CONSP (queue))
1904 first = XCAR (queue);
1905 second = Fcdr (first);
1906 first = Fcar (first);
1907 if (EQ (first, make_number (0)))
1908 Vfeatures = second;
1909 else
1910 Ffset (first, second);
1911 queue = XCDR (queue);
1915 /* Load an autoloaded function.
1916 FUNNAME is the symbol which is the function's name.
1917 FUNDEF is the autoload definition (a list). */
1919 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1920 doc: /* Load FUNDEF which should be an autoload.
1921 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1922 in which case the function returns the new autoloaded function value.
1923 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1924 it defines a macro. */)
1925 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1927 ptrdiff_t count = SPECPDL_INDEX ();
1929 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1930 return fundef;
1932 if (EQ (macro_only, Qmacro))
1934 Lisp_Object kind = Fnth (make_number (4), fundef);
1935 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1936 return fundef;
1939 /* This is to make sure that loadup.el gives a clear picture
1940 of what files are preloaded and when. */
1941 if (! NILP (Vpurify_flag))
1942 error ("Attempt to autoload %s while preparing to dump",
1943 SDATA (SYMBOL_NAME (funname)));
1945 CHECK_SYMBOL (funname);
1947 /* Preserve the match data. */
1948 record_unwind_save_match_data ();
1950 /* If autoloading gets an error (which includes the error of failing
1951 to define the function being called), we use Vautoload_queue
1952 to undo function definitions and `provide' calls made by
1953 the function. We do this in the specific case of autoloading
1954 because autoloading is not an explicit request "load this file",
1955 but rather a request to "call this function".
1957 The value saved here is to be restored into Vautoload_queue. */
1958 record_unwind_protect (un_autoload, Vautoload_queue);
1959 Vautoload_queue = Qt;
1960 /* If `macro_only', assume this autoload to be a "best-effort",
1961 so don't signal an error if autoloading fails. */
1962 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1964 /* Once loading finishes, don't undo it. */
1965 Vautoload_queue = Qt;
1966 unbind_to (count, Qnil);
1968 if (NILP (funname))
1969 return Qnil;
1970 else
1972 Lisp_Object fun = Findirect_function (funname, Qnil);
1974 if (!NILP (Fequal (fun, fundef)))
1975 error ("Autoloading failed to define function %s",
1976 SDATA (SYMBOL_NAME (funname)));
1977 else
1978 return fun;
1983 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1984 doc: /* Evaluate FORM and return its value.
1985 If LEXICAL is t, evaluate using lexical scoping.
1986 LEXICAL can also be an actual lexical environment, in the form of an
1987 alist mapping symbols to their value. */)
1988 (Lisp_Object form, Lisp_Object lexical)
1990 ptrdiff_t count = SPECPDL_INDEX ();
1991 specbind (Qinternal_interpreter_environment,
1992 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1993 return unbind_to (count, eval_sub (form));
1996 /* Grow the specpdl stack by one entry.
1997 The caller should have already initialized the entry.
1998 Signal an error on stack overflow.
2000 Make sure that there is always one unused entry past the top of the
2001 stack, so that the just-initialized entry is safely unwound if
2002 memory exhausted and an error is signaled here. Also, allocate a
2003 never-used entry just before the bottom of the stack; sometimes its
2004 address is taken. */
2006 static void
2007 grow_specpdl (void)
2009 specpdl_ptr++;
2011 if (specpdl_ptr == specpdl + specpdl_size)
2013 ptrdiff_t count = SPECPDL_INDEX ();
2014 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2015 union specbinding *pdlvec = specpdl - 1;
2016 ptrdiff_t pdlvecsize = specpdl_size + 1;
2017 if (max_size <= specpdl_size)
2019 if (max_specpdl_size < 400)
2020 max_size = max_specpdl_size = 400;
2021 if (max_size <= specpdl_size)
2022 signal_error ("Variable binding depth exceeds max-specpdl-size",
2023 Qnil);
2025 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2026 specpdl = pdlvec + 1;
2027 specpdl_size = pdlvecsize - 1;
2028 specpdl_ptr = specpdl + count;
2032 ptrdiff_t
2033 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2035 ptrdiff_t count = SPECPDL_INDEX ();
2037 eassert (nargs >= UNEVALLED);
2038 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2039 specpdl_ptr->bt.debug_on_exit = false;
2040 specpdl_ptr->bt.function = function;
2041 specpdl_ptr->bt.args = args;
2042 specpdl_ptr->bt.nargs = nargs;
2043 grow_specpdl ();
2045 return count;
2048 /* Eval a sub-expression of the current expression (i.e. in the same
2049 lexical scope). */
2050 Lisp_Object
2051 eval_sub (Lisp_Object form)
2053 Lisp_Object fun, val, original_fun, original_args;
2054 Lisp_Object funcar;
2055 ptrdiff_t count;
2057 /* Declare here, as this array may be accessed by call_debugger near
2058 the end of this function. See Bug#21245. */
2059 Lisp_Object argvals[8];
2061 if (SYMBOLP (form))
2063 /* Look up its binding in the lexical environment.
2064 We do not pay attention to the declared_special flag here, since we
2065 already did that when let-binding the variable. */
2066 Lisp_Object lex_binding
2067 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2068 ? Fassq (form, Vinternal_interpreter_environment)
2069 : Qnil;
2070 if (CONSP (lex_binding))
2071 return XCDR (lex_binding);
2072 else
2073 return Fsymbol_value (form);
2076 if (!CONSP (form))
2077 return form;
2079 QUIT;
2081 maybe_gc ();
2083 if (++lisp_eval_depth > max_lisp_eval_depth)
2085 if (max_lisp_eval_depth < 100)
2086 max_lisp_eval_depth = 100;
2087 if (lisp_eval_depth > max_lisp_eval_depth)
2088 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2091 original_fun = XCAR (form);
2092 original_args = XCDR (form);
2094 /* This also protects them from gc. */
2095 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2097 if (debug_on_next_call)
2098 do_debug_on_call (Qt, count);
2100 /* At this point, only original_fun and original_args
2101 have values that will be used below. */
2102 retry:
2104 /* Optimize for no indirection. */
2105 fun = original_fun;
2106 if (!SYMBOLP (fun))
2107 fun = Ffunction (Fcons (fun, Qnil));
2108 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2109 fun = indirect_function (fun);
2111 if (SUBRP (fun))
2113 Lisp_Object args_left = original_args;
2114 Lisp_Object numargs = Flength (args_left);
2116 check_cons_list ();
2118 if (XINT (numargs) < XSUBR (fun)->min_args
2119 || (XSUBR (fun)->max_args >= 0
2120 && XSUBR (fun)->max_args < XINT (numargs)))
2121 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2123 else if (XSUBR (fun)->max_args == UNEVALLED)
2124 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2125 else if (XSUBR (fun)->max_args == MANY)
2127 /* Pass a vector of evaluated arguments. */
2128 Lisp_Object *vals;
2129 ptrdiff_t argnum = 0;
2130 USE_SAFE_ALLOCA;
2132 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2134 while (!NILP (args_left))
2136 vals[argnum++] = eval_sub (Fcar (args_left));
2137 args_left = Fcdr (args_left);
2140 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2142 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2144 check_cons_list ();
2145 lisp_eval_depth--;
2146 /* Do the debug-on-exit now, while VALS still exists. */
2147 if (backtrace_debug_on_exit (specpdl + count))
2148 val = call_debugger (list2 (Qexit, val));
2149 SAFE_FREE ();
2150 specpdl_ptr--;
2151 return val;
2153 else
2155 int i, maxargs = XSUBR (fun)->max_args;
2157 for (i = 0; i < maxargs; i++)
2159 argvals[i] = eval_sub (Fcar (args_left));
2160 args_left = Fcdr (args_left);
2163 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2165 switch (i)
2167 case 0:
2168 val = (XSUBR (fun)->function.a0 ());
2169 break;
2170 case 1:
2171 val = (XSUBR (fun)->function.a1 (argvals[0]));
2172 break;
2173 case 2:
2174 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2175 break;
2176 case 3:
2177 val = (XSUBR (fun)->function.a3
2178 (argvals[0], argvals[1], argvals[2]));
2179 break;
2180 case 4:
2181 val = (XSUBR (fun)->function.a4
2182 (argvals[0], argvals[1], argvals[2], argvals[3]));
2183 break;
2184 case 5:
2185 val = (XSUBR (fun)->function.a5
2186 (argvals[0], argvals[1], argvals[2], argvals[3],
2187 argvals[4]));
2188 break;
2189 case 6:
2190 val = (XSUBR (fun)->function.a6
2191 (argvals[0], argvals[1], argvals[2], argvals[3],
2192 argvals[4], argvals[5]));
2193 break;
2194 case 7:
2195 val = (XSUBR (fun)->function.a7
2196 (argvals[0], argvals[1], argvals[2], argvals[3],
2197 argvals[4], argvals[5], argvals[6]));
2198 break;
2200 case 8:
2201 val = (XSUBR (fun)->function.a8
2202 (argvals[0], argvals[1], argvals[2], argvals[3],
2203 argvals[4], argvals[5], argvals[6], argvals[7]));
2204 break;
2206 default:
2207 /* Someone has created a subr that takes more arguments than
2208 is supported by this code. We need to either rewrite the
2209 subr to use a different argument protocol, or add more
2210 cases to this switch. */
2211 emacs_abort ();
2215 else if (COMPILEDP (fun))
2216 return apply_lambda (fun, original_args, count);
2217 else
2219 if (NILP (fun))
2220 xsignal1 (Qvoid_function, original_fun);
2221 if (!CONSP (fun))
2222 xsignal1 (Qinvalid_function, original_fun);
2223 funcar = XCAR (fun);
2224 if (!SYMBOLP (funcar))
2225 xsignal1 (Qinvalid_function, original_fun);
2226 if (EQ (funcar, Qautoload))
2228 Fautoload_do_load (fun, original_fun, Qnil);
2229 goto retry;
2231 if (EQ (funcar, Qmacro))
2233 ptrdiff_t count1 = SPECPDL_INDEX ();
2234 Lisp_Object exp;
2235 /* Bind lexical-binding during expansion of the macro, so the
2236 macro can know reliably if the code it outputs will be
2237 interpreted using lexical-binding or not. */
2238 specbind (Qlexical_binding,
2239 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2240 exp = apply1 (Fcdr (fun), original_args);
2241 unbind_to (count1, Qnil);
2242 val = eval_sub (exp);
2244 else if (EQ (funcar, Qlambda)
2245 || EQ (funcar, Qclosure))
2246 return apply_lambda (fun, original_args, count);
2247 else
2248 xsignal1 (Qinvalid_function, original_fun);
2250 check_cons_list ();
2252 lisp_eval_depth--;
2253 if (backtrace_debug_on_exit (specpdl + count))
2254 val = call_debugger (list2 (Qexit, val));
2255 specpdl_ptr--;
2257 return val;
2260 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2261 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2262 Then return the value FUNCTION returns.
2263 Thus, (apply \\='+ 1 2 \\='(3 4)) returns 10.
2264 usage: (apply FUNCTION &rest ARGUMENTS) */)
2265 (ptrdiff_t nargs, Lisp_Object *args)
2267 ptrdiff_t i, numargs, funcall_nargs;
2268 register Lisp_Object *funcall_args = NULL;
2269 register Lisp_Object spread_arg = args[nargs - 1];
2270 Lisp_Object fun = args[0];
2271 Lisp_Object retval;
2272 USE_SAFE_ALLOCA;
2274 CHECK_LIST (spread_arg);
2276 numargs = XINT (Flength (spread_arg));
2278 if (numargs == 0)
2279 return Ffuncall (nargs - 1, args);
2280 else if (numargs == 1)
2282 args [nargs - 1] = XCAR (spread_arg);
2283 return Ffuncall (nargs, args);
2286 numargs += nargs - 2;
2288 /* Optimize for no indirection. */
2289 if (SYMBOLP (fun) && !NILP (fun)
2290 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2292 fun = indirect_function (fun);
2293 if (NILP (fun))
2294 /* Let funcall get the error. */
2295 fun = args[0];
2298 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2299 /* Don't hide an error by adding missing arguments. */
2300 && numargs >= XSUBR (fun)->min_args)
2302 /* Avoid making funcall cons up a yet another new vector of arguments
2303 by explicitly supplying nil's for optional values. */
2304 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2305 memclear (funcall_args + numargs + 1,
2306 (XSUBR (fun)->max_args - numargs) * word_size);
2307 funcall_nargs = 1 + XSUBR (fun)->max_args;
2309 else
2310 { /* We add 1 to numargs because funcall_args includes the
2311 function itself as well as its arguments. */
2312 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2313 funcall_nargs = 1 + numargs;
2316 memcpy (funcall_args, args, nargs * word_size);
2317 /* Spread the last arg we got. Its first element goes in
2318 the slot that it used to occupy, hence this value of I. */
2319 i = nargs - 1;
2320 while (!NILP (spread_arg))
2322 funcall_args [i++] = XCAR (spread_arg);
2323 spread_arg = XCDR (spread_arg);
2326 retval = Ffuncall (funcall_nargs, funcall_args);
2328 SAFE_FREE ();
2329 return retval;
2332 /* Run hook variables in various ways. */
2334 static Lisp_Object
2335 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2337 Ffuncall (nargs, args);
2338 return Qnil;
2341 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2342 doc: /* Run each hook in HOOKS.
2343 Each argument should be a symbol, a hook variable.
2344 These symbols are processed in the order specified.
2345 If a hook symbol has a non-nil value, that value may be a function
2346 or a list of functions to be called to run the hook.
2347 If the value is a function, it is called with no arguments.
2348 If it is a list, the elements are called, in order, with no arguments.
2350 Major modes should not use this function directly to run their mode
2351 hook; they should use `run-mode-hooks' instead.
2353 Do not use `make-local-variable' to make a hook variable buffer-local.
2354 Instead, use `add-hook' and specify t for the LOCAL argument.
2355 usage: (run-hooks &rest HOOKS) */)
2356 (ptrdiff_t nargs, Lisp_Object *args)
2358 ptrdiff_t i;
2360 for (i = 0; i < nargs; i++)
2361 run_hook (args[i]);
2363 return Qnil;
2366 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2367 Srun_hook_with_args, 1, MANY, 0,
2368 doc: /* Run HOOK with the specified arguments ARGS.
2369 HOOK should be a symbol, a hook variable. The value of HOOK
2370 may be nil, a function, or a list of functions. Call each
2371 function in order with arguments ARGS. The final return value
2372 is unspecified.
2374 Do not use `make-local-variable' to make a hook variable buffer-local.
2375 Instead, use `add-hook' and specify t for the LOCAL argument.
2376 usage: (run-hook-with-args HOOK &rest ARGS) */)
2377 (ptrdiff_t nargs, Lisp_Object *args)
2379 return run_hook_with_args (nargs, args, funcall_nil);
2382 /* NB this one still documents a specific non-nil return value.
2383 (As did run-hook-with-args and run-hook-with-args-until-failure
2384 until they were changed in 24.1.) */
2385 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2386 Srun_hook_with_args_until_success, 1, MANY, 0,
2387 doc: /* Run HOOK with the specified arguments ARGS.
2388 HOOK should be a symbol, a hook variable. The value of HOOK
2389 may be nil, a function, or a list of functions. Call each
2390 function in order with arguments ARGS, stopping at the first
2391 one that returns non-nil, and return that value. Otherwise (if
2392 all functions return nil, or if there are no functions to call),
2393 return nil.
2395 Do not use `make-local-variable' to make a hook variable buffer-local.
2396 Instead, use `add-hook' and specify t for the LOCAL argument.
2397 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2398 (ptrdiff_t nargs, Lisp_Object *args)
2400 return run_hook_with_args (nargs, args, Ffuncall);
2403 static Lisp_Object
2404 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2406 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2409 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2410 Srun_hook_with_args_until_failure, 1, MANY, 0,
2411 doc: /* Run HOOK with the specified arguments ARGS.
2412 HOOK should be a symbol, a hook variable. The value of HOOK
2413 may be nil, a function, or a list of functions. Call each
2414 function in order with arguments ARGS, stopping at the first
2415 one that returns nil, and return nil. Otherwise (if all functions
2416 return non-nil, or if there are no functions to call), return non-nil
2417 \(do not rely on the precise return value in this case).
2419 Do not use `make-local-variable' to make a hook variable buffer-local.
2420 Instead, use `add-hook' and specify t for the LOCAL argument.
2421 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2422 (ptrdiff_t nargs, Lisp_Object *args)
2424 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2427 static Lisp_Object
2428 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2430 Lisp_Object tmp = args[0], ret;
2431 args[0] = args[1];
2432 args[1] = tmp;
2433 ret = Ffuncall (nargs, args);
2434 args[1] = args[0];
2435 args[0] = tmp;
2436 return ret;
2439 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2440 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2441 I.e. instead of calling each function FUN directly with arguments ARGS,
2442 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2443 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2444 aborts and returns that value.
2445 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2446 (ptrdiff_t nargs, Lisp_Object *args)
2448 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2451 /* ARGS[0] should be a hook symbol.
2452 Call each of the functions in the hook value, passing each of them
2453 as arguments all the rest of ARGS (all NARGS - 1 elements).
2454 FUNCALL specifies how to call each function on the hook. */
2456 Lisp_Object
2457 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2458 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2460 Lisp_Object sym, val, ret = Qnil;
2462 /* If we are dying or still initializing,
2463 don't do anything--it would probably crash if we tried. */
2464 if (NILP (Vrun_hooks))
2465 return Qnil;
2467 sym = args[0];
2468 val = find_symbol_value (sym);
2470 if (EQ (val, Qunbound) || NILP (val))
2471 return ret;
2472 else if (!CONSP (val) || FUNCTIONP (val))
2474 args[0] = val;
2475 return funcall (nargs, args);
2477 else
2479 Lisp_Object global_vals = Qnil;
2481 for (;
2482 CONSP (val) && NILP (ret);
2483 val = XCDR (val))
2485 if (EQ (XCAR (val), Qt))
2487 /* t indicates this hook has a local binding;
2488 it means to run the global binding too. */
2489 global_vals = Fdefault_value (sym);
2490 if (NILP (global_vals)) continue;
2492 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2494 args[0] = global_vals;
2495 ret = funcall (nargs, args);
2497 else
2499 for (;
2500 CONSP (global_vals) && NILP (ret);
2501 global_vals = XCDR (global_vals))
2503 args[0] = XCAR (global_vals);
2504 /* In a global value, t should not occur. If it does, we
2505 must ignore it to avoid an endless loop. */
2506 if (!EQ (args[0], Qt))
2507 ret = funcall (nargs, args);
2511 else
2513 args[0] = XCAR (val);
2514 ret = funcall (nargs, args);
2518 return ret;
2522 /* Run the hook HOOK, giving each function no args. */
2524 void
2525 run_hook (Lisp_Object hook)
2527 Frun_hook_with_args (1, &hook);
2530 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2532 void
2533 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2535 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2538 /* Apply fn to arg. */
2539 Lisp_Object
2540 apply1 (Lisp_Object fn, Lisp_Object arg)
2542 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2545 /* Call function fn on no arguments. */
2546 Lisp_Object
2547 call0 (Lisp_Object fn)
2549 return Ffuncall (1, &fn);
2552 /* Call function fn with 1 argument arg1. */
2553 /* ARGSUSED */
2554 Lisp_Object
2555 call1 (Lisp_Object fn, Lisp_Object arg1)
2557 return CALLN (Ffuncall, fn, arg1);
2560 /* Call function fn with 2 arguments arg1, arg2. */
2561 /* ARGSUSED */
2562 Lisp_Object
2563 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2565 return CALLN (Ffuncall, fn, arg1, arg2);
2568 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2569 /* ARGSUSED */
2570 Lisp_Object
2571 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2573 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2576 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2577 /* ARGSUSED */
2578 Lisp_Object
2579 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2580 Lisp_Object arg4)
2582 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2585 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2586 /* ARGSUSED */
2587 Lisp_Object
2588 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2589 Lisp_Object arg4, Lisp_Object arg5)
2591 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2594 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2595 /* ARGSUSED */
2596 Lisp_Object
2597 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2598 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2600 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2603 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2604 /* ARGSUSED */
2605 Lisp_Object
2606 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2607 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2609 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2612 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2613 doc: /* Non-nil if OBJECT is a function. */)
2614 (Lisp_Object object)
2616 if (FUNCTIONP (object))
2617 return Qt;
2618 return Qnil;
2621 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2622 doc: /* Call first argument as a function, passing remaining arguments to it.
2623 Return the value that function returns.
2624 Thus, (funcall \\='cons \\='x \\='y) returns (x . y).
2625 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2626 (ptrdiff_t nargs, Lisp_Object *args)
2628 Lisp_Object fun, original_fun;
2629 Lisp_Object funcar;
2630 ptrdiff_t numargs = nargs - 1;
2631 Lisp_Object lisp_numargs;
2632 Lisp_Object val;
2633 Lisp_Object *internal_args;
2634 ptrdiff_t count;
2636 QUIT;
2638 if (++lisp_eval_depth > max_lisp_eval_depth)
2640 if (max_lisp_eval_depth < 100)
2641 max_lisp_eval_depth = 100;
2642 if (lisp_eval_depth > max_lisp_eval_depth)
2643 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2646 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2648 maybe_gc ();
2650 if (debug_on_next_call)
2651 do_debug_on_call (Qlambda, count);
2653 check_cons_list ();
2655 original_fun = args[0];
2657 retry:
2659 /* Optimize for no indirection. */
2660 fun = original_fun;
2661 if (SYMBOLP (fun) && !NILP (fun)
2662 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2663 fun = indirect_function (fun);
2665 if (SUBRP (fun))
2667 if (numargs < XSUBR (fun)->min_args
2668 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2670 XSETFASTINT (lisp_numargs, numargs);
2671 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2674 else if (XSUBR (fun)->max_args == UNEVALLED)
2675 xsignal1 (Qinvalid_function, original_fun);
2677 else if (XSUBR (fun)->max_args == MANY)
2678 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2679 else
2681 Lisp_Object internal_argbuf[8];
2682 if (XSUBR (fun)->max_args > numargs)
2684 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2685 internal_args = internal_argbuf;
2686 memcpy (internal_args, args + 1, numargs * word_size);
2687 memclear (internal_args + numargs,
2688 (XSUBR (fun)->max_args - numargs) * word_size);
2690 else
2691 internal_args = args + 1;
2692 switch (XSUBR (fun)->max_args)
2694 case 0:
2695 val = (XSUBR (fun)->function.a0 ());
2696 break;
2697 case 1:
2698 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2699 break;
2700 case 2:
2701 val = (XSUBR (fun)->function.a2
2702 (internal_args[0], internal_args[1]));
2703 break;
2704 case 3:
2705 val = (XSUBR (fun)->function.a3
2706 (internal_args[0], internal_args[1], internal_args[2]));
2707 break;
2708 case 4:
2709 val = (XSUBR (fun)->function.a4
2710 (internal_args[0], internal_args[1], internal_args[2],
2711 internal_args[3]));
2712 break;
2713 case 5:
2714 val = (XSUBR (fun)->function.a5
2715 (internal_args[0], internal_args[1], internal_args[2],
2716 internal_args[3], internal_args[4]));
2717 break;
2718 case 6:
2719 val = (XSUBR (fun)->function.a6
2720 (internal_args[0], internal_args[1], internal_args[2],
2721 internal_args[3], internal_args[4], internal_args[5]));
2722 break;
2723 case 7:
2724 val = (XSUBR (fun)->function.a7
2725 (internal_args[0], internal_args[1], internal_args[2],
2726 internal_args[3], internal_args[4], internal_args[5],
2727 internal_args[6]));
2728 break;
2730 case 8:
2731 val = (XSUBR (fun)->function.a8
2732 (internal_args[0], internal_args[1], internal_args[2],
2733 internal_args[3], internal_args[4], internal_args[5],
2734 internal_args[6], internal_args[7]));
2735 break;
2737 default:
2739 /* If a subr takes more than 8 arguments without using MANY
2740 or UNEVALLED, we need to extend this function to support it.
2741 Until this is done, there is no way to call the function. */
2742 emacs_abort ();
2746 else if (COMPILEDP (fun))
2747 val = funcall_lambda (fun, numargs, args + 1);
2748 else
2750 if (NILP (fun))
2751 xsignal1 (Qvoid_function, original_fun);
2752 if (!CONSP (fun))
2753 xsignal1 (Qinvalid_function, original_fun);
2754 funcar = XCAR (fun);
2755 if (!SYMBOLP (funcar))
2756 xsignal1 (Qinvalid_function, original_fun);
2757 if (EQ (funcar, Qlambda)
2758 || EQ (funcar, Qclosure))
2759 val = funcall_lambda (fun, numargs, args + 1);
2760 else if (EQ (funcar, Qautoload))
2762 Fautoload_do_load (fun, original_fun, Qnil);
2763 check_cons_list ();
2764 goto retry;
2766 else
2767 xsignal1 (Qinvalid_function, original_fun);
2769 check_cons_list ();
2770 lisp_eval_depth--;
2771 if (backtrace_debug_on_exit (specpdl + count))
2772 val = call_debugger (list2 (Qexit, val));
2773 specpdl_ptr--;
2774 return val;
2777 static Lisp_Object
2778 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2780 Lisp_Object args_left;
2781 ptrdiff_t i;
2782 EMACS_INT numargs;
2783 Lisp_Object *arg_vector;
2784 Lisp_Object tem;
2785 USE_SAFE_ALLOCA;
2787 numargs = XFASTINT (Flength (args));
2788 SAFE_ALLOCA_LISP (arg_vector, numargs);
2789 args_left = args;
2791 for (i = 0; i < numargs; )
2793 tem = Fcar (args_left), args_left = Fcdr (args_left);
2794 tem = eval_sub (tem);
2795 arg_vector[i++] = tem;
2798 set_backtrace_args (specpdl + count, arg_vector, i);
2799 tem = funcall_lambda (fun, numargs, arg_vector);
2801 check_cons_list ();
2802 lisp_eval_depth--;
2803 /* Do the debug-on-exit now, while arg_vector still exists. */
2804 if (backtrace_debug_on_exit (specpdl + count))
2805 tem = call_debugger (list2 (Qexit, tem));
2806 SAFE_FREE ();
2807 specpdl_ptr--;
2808 return tem;
2811 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2812 and return the result of evaluation.
2813 FUN must be either a lambda-expression or a compiled-code object. */
2815 static Lisp_Object
2816 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2817 register Lisp_Object *arg_vector)
2819 Lisp_Object val, syms_left, next, lexenv;
2820 ptrdiff_t count = SPECPDL_INDEX ();
2821 ptrdiff_t i;
2822 bool optional, rest;
2824 if (CONSP (fun))
2826 if (EQ (XCAR (fun), Qclosure))
2828 fun = XCDR (fun); /* Drop `closure'. */
2829 lexenv = XCAR (fun);
2830 CHECK_LIST_CONS (fun, fun);
2832 else
2833 lexenv = Qnil;
2834 syms_left = XCDR (fun);
2835 if (CONSP (syms_left))
2836 syms_left = XCAR (syms_left);
2837 else
2838 xsignal1 (Qinvalid_function, fun);
2840 else if (COMPILEDP (fun))
2842 ptrdiff_t size = ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK;
2843 if (size <= COMPILED_STACK_DEPTH)
2844 xsignal1 (Qinvalid_function, fun);
2845 syms_left = AREF (fun, COMPILED_ARGLIST);
2846 if (INTEGERP (syms_left))
2847 /* A byte-code object with a non-nil `push args' slot means we
2848 shouldn't bind any arguments, instead just call the byte-code
2849 interpreter directly; it will push arguments as necessary.
2851 Byte-code objects with either a non-existent, or a nil value for
2852 the `push args' slot (the default), have dynamically-bound
2853 arguments, and use the argument-binding code below instead (as do
2854 all interpreted functions, even lexically bound ones). */
2856 /* If we have not actually read the bytecode string
2857 and constants vector yet, fetch them from the file. */
2858 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2859 Ffetch_bytecode (fun);
2860 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2861 AREF (fun, COMPILED_CONSTANTS),
2862 AREF (fun, COMPILED_STACK_DEPTH),
2863 syms_left,
2864 nargs, arg_vector);
2866 lexenv = Qnil;
2868 else
2869 emacs_abort ();
2871 i = optional = rest = 0;
2872 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2874 QUIT;
2876 next = XCAR (syms_left);
2877 if (!SYMBOLP (next))
2878 xsignal1 (Qinvalid_function, fun);
2880 if (EQ (next, Qand_rest))
2881 rest = 1;
2882 else if (EQ (next, Qand_optional))
2883 optional = 1;
2884 else
2886 Lisp_Object arg;
2887 if (rest)
2889 arg = Flist (nargs - i, &arg_vector[i]);
2890 i = nargs;
2892 else if (i < nargs)
2893 arg = arg_vector[i++];
2894 else if (!optional)
2895 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2896 else
2897 arg = Qnil;
2899 /* Bind the argument. */
2900 if (!NILP (lexenv) && SYMBOLP (next))
2901 /* Lexically bind NEXT by adding it to the lexenv alist. */
2902 lexenv = Fcons (Fcons (next, arg), lexenv);
2903 else
2904 /* Dynamically bind NEXT. */
2905 specbind (next, arg);
2909 if (!NILP (syms_left))
2910 xsignal1 (Qinvalid_function, fun);
2911 else if (i < nargs)
2912 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2914 if (!EQ (lexenv, Vinternal_interpreter_environment))
2915 /* Instantiate a new lexical environment. */
2916 specbind (Qinternal_interpreter_environment, lexenv);
2918 if (CONSP (fun))
2919 val = Fprogn (XCDR (XCDR (fun)));
2920 else
2922 /* If we have not actually read the bytecode string
2923 and constants vector yet, fetch them from the file. */
2924 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2925 Ffetch_bytecode (fun);
2926 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2927 AREF (fun, COMPILED_CONSTANTS),
2928 AREF (fun, COMPILED_STACK_DEPTH),
2929 Qnil, 0, 0);
2932 return unbind_to (count, val);
2935 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2936 1, 1, 0,
2937 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2938 (Lisp_Object object)
2940 Lisp_Object tem;
2942 if (COMPILEDP (object))
2944 ptrdiff_t size = ASIZE (object) & PSEUDOVECTOR_SIZE_MASK;
2945 if (size <= COMPILED_STACK_DEPTH)
2946 xsignal1 (Qinvalid_function, object);
2947 if (CONSP (AREF (object, COMPILED_BYTECODE)))
2949 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2950 if (!CONSP (tem))
2952 tem = AREF (object, COMPILED_BYTECODE);
2953 if (CONSP (tem) && STRINGP (XCAR (tem)))
2954 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2955 else
2956 error ("Invalid byte code");
2958 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2959 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2962 return object;
2965 /* Return true if SYMBOL currently has a let-binding
2966 which was made in the buffer that is now current. */
2968 bool
2969 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2971 union specbinding *p;
2972 Lisp_Object buf = Fcurrent_buffer ();
2974 for (p = specpdl_ptr; p > specpdl; )
2975 if ((--p)->kind > SPECPDL_LET)
2977 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2978 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2979 if (symbol == let_bound_symbol
2980 && EQ (specpdl_where (p), buf))
2981 return 1;
2984 return 0;
2987 bool
2988 let_shadows_global_binding_p (Lisp_Object symbol)
2990 union specbinding *p;
2992 for (p = specpdl_ptr; p > specpdl; )
2993 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
2994 return 1;
2996 return 0;
2999 /* `specpdl_ptr' describes which variable is
3000 let-bound, so it can be properly undone when we unbind_to.
3001 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3002 - SYMBOL is the variable being bound. Note that it should not be
3003 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3004 to record V2 here).
3005 - WHERE tells us in which buffer the binding took place.
3006 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3007 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3008 i.e. bindings to the default value of a variable which can be
3009 buffer-local. */
3011 void
3012 specbind (Lisp_Object symbol, Lisp_Object value)
3014 struct Lisp_Symbol *sym;
3016 CHECK_SYMBOL (symbol);
3017 sym = XSYMBOL (symbol);
3019 start:
3020 switch (sym->redirect)
3022 case SYMBOL_VARALIAS:
3023 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3024 case SYMBOL_PLAINVAL:
3025 /* The most common case is that of a non-constant symbol with a
3026 trivial value. Make that as fast as we can. */
3027 specpdl_ptr->let.kind = SPECPDL_LET;
3028 specpdl_ptr->let.symbol = symbol;
3029 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3030 grow_specpdl ();
3031 if (!sym->constant)
3032 SET_SYMBOL_VAL (sym, value);
3033 else
3034 set_internal (symbol, value, Qnil, 1);
3035 break;
3036 case SYMBOL_LOCALIZED:
3037 if (SYMBOL_BLV (sym)->frame_local)
3038 error ("Frame-local vars cannot be let-bound");
3039 case SYMBOL_FORWARDED:
3041 Lisp_Object ovalue = find_symbol_value (symbol);
3042 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3043 specpdl_ptr->let.symbol = symbol;
3044 specpdl_ptr->let.old_value = ovalue;
3045 specpdl_ptr->let.where = Fcurrent_buffer ();
3047 eassert (sym->redirect != SYMBOL_LOCALIZED
3048 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3050 if (sym->redirect == SYMBOL_LOCALIZED)
3052 if (!blv_found (SYMBOL_BLV (sym)))
3053 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3055 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3057 /* If SYMBOL is a per-buffer variable which doesn't have a
3058 buffer-local value here, make the `let' change the global
3059 value by changing the value of SYMBOL in all buffers not
3060 having their own value. This is consistent with what
3061 happens with other buffer-local variables. */
3062 if (NILP (Flocal_variable_p (symbol, Qnil)))
3064 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3065 grow_specpdl ();
3066 Fset_default (symbol, value);
3067 return;
3070 else
3071 specpdl_ptr->let.kind = SPECPDL_LET;
3073 grow_specpdl ();
3074 set_internal (symbol, value, Qnil, 1);
3075 break;
3077 default: emacs_abort ();
3081 /* Push unwind-protect entries of various types. */
3083 void
3084 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3086 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3087 specpdl_ptr->unwind.func = function;
3088 specpdl_ptr->unwind.arg = arg;
3089 grow_specpdl ();
3092 void
3093 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3095 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3096 specpdl_ptr->unwind_ptr.func = function;
3097 specpdl_ptr->unwind_ptr.arg = arg;
3098 grow_specpdl ();
3101 void
3102 record_unwind_protect_int (void (*function) (int), int arg)
3104 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3105 specpdl_ptr->unwind_int.func = function;
3106 specpdl_ptr->unwind_int.arg = arg;
3107 grow_specpdl ();
3110 void
3111 record_unwind_protect_void (void (*function) (void))
3113 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3114 specpdl_ptr->unwind_void.func = function;
3115 grow_specpdl ();
3118 static void
3119 do_nothing (void)
3122 /* Push an unwind-protect entry that does nothing, so that
3123 set_unwind_protect_ptr can overwrite it later. */
3125 void
3126 record_unwind_protect_nothing (void)
3128 record_unwind_protect_void (do_nothing);
3131 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3132 It need not be at the top of the stack. */
3134 void
3135 clear_unwind_protect (ptrdiff_t count)
3137 union specbinding *p = specpdl + count;
3138 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3139 p->unwind_void.func = do_nothing;
3142 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3143 It need not be at the top of the stack. Discard the entry's
3144 previous value without invoking it. */
3146 void
3147 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3148 Lisp_Object arg)
3150 union specbinding *p = specpdl + count;
3151 p->unwind.kind = SPECPDL_UNWIND;
3152 p->unwind.func = func;
3153 p->unwind.arg = arg;
3156 void
3157 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3159 union specbinding *p = specpdl + count;
3160 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3161 p->unwind_ptr.func = func;
3162 p->unwind_ptr.arg = arg;
3165 /* Pop and execute entries from the unwind-protect stack until the
3166 depth COUNT is reached. Return VALUE. */
3168 Lisp_Object
3169 unbind_to (ptrdiff_t count, Lisp_Object value)
3171 Lisp_Object quitf = Vquit_flag;
3173 Vquit_flag = Qnil;
3175 while (specpdl_ptr != specpdl + count)
3177 /* Decrement specpdl_ptr before we do the work to unbind it, so
3178 that an error in unbinding won't try to unbind the same entry
3179 again. Take care to copy any parts of the binding needed
3180 before invoking any code that can make more bindings. */
3182 specpdl_ptr--;
3184 switch (specpdl_ptr->kind)
3186 case SPECPDL_UNWIND:
3187 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3188 break;
3189 case SPECPDL_UNWIND_PTR:
3190 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3191 break;
3192 case SPECPDL_UNWIND_INT:
3193 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3194 break;
3195 case SPECPDL_UNWIND_VOID:
3196 specpdl_ptr->unwind_void.func ();
3197 break;
3198 case SPECPDL_BACKTRACE:
3199 break;
3200 case SPECPDL_LET:
3201 { /* If variable has a trivial value (no forwarding), we can
3202 just set it. No need to check for constant symbols here,
3203 since that was already done by specbind. */
3204 Lisp_Object sym = specpdl_symbol (specpdl_ptr);
3205 if (SYMBOLP (sym) && XSYMBOL (sym)->redirect == SYMBOL_PLAINVAL)
3207 SET_SYMBOL_VAL (XSYMBOL (sym),
3208 specpdl_old_value (specpdl_ptr));
3209 break;
3211 else
3212 { /* FALLTHROUGH!!
3213 NOTE: we only ever come here if make_local_foo was used for
3214 the first time on this var within this let. */
3217 case SPECPDL_LET_DEFAULT:
3218 Fset_default (specpdl_symbol (specpdl_ptr),
3219 specpdl_old_value (specpdl_ptr));
3220 break;
3221 case SPECPDL_LET_LOCAL:
3223 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3224 Lisp_Object where = specpdl_where (specpdl_ptr);
3225 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3226 eassert (BUFFERP (where));
3228 /* If this was a local binding, reset the value in the appropriate
3229 buffer, but only if that buffer's binding still exists. */
3230 if (!NILP (Flocal_variable_p (symbol, where)))
3231 set_internal (symbol, old_value, where, 1);
3233 break;
3237 if (NILP (Vquit_flag) && !NILP (quitf))
3238 Vquit_flag = quitf;
3240 return value;
3243 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3244 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3245 A special variable is one that will be bound dynamically, even in a
3246 context where binding is lexical by default. */)
3247 (Lisp_Object symbol)
3249 CHECK_SYMBOL (symbol);
3250 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3254 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3255 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3256 The debugger is entered when that frame exits, if the flag is non-nil. */)
3257 (Lisp_Object level, Lisp_Object flag)
3259 union specbinding *pdl = backtrace_top ();
3260 register EMACS_INT i;
3262 CHECK_NUMBER (level);
3264 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3265 pdl = backtrace_next (pdl);
3267 if (backtrace_p (pdl))
3268 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3270 return flag;
3273 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3274 doc: /* Print a trace of Lisp function calls currently active.
3275 Output stream used is value of `standard-output'. */)
3276 (void)
3278 union specbinding *pdl = backtrace_top ();
3279 Lisp_Object tem;
3280 Lisp_Object old_print_level = Vprint_level;
3282 if (NILP (Vprint_level))
3283 XSETFASTINT (Vprint_level, 8);
3285 while (backtrace_p (pdl))
3287 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ");
3288 if (backtrace_nargs (pdl) == UNEVALLED)
3290 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3291 Qnil);
3292 write_string ("\n");
3294 else
3296 tem = backtrace_function (pdl);
3297 Fprin1 (tem, Qnil); /* This can QUIT. */
3298 write_string ("(");
3300 ptrdiff_t i;
3301 for (i = 0; i < backtrace_nargs (pdl); i++)
3303 if (i) write_string (" ");
3304 Fprin1 (backtrace_args (pdl)[i], Qnil);
3307 write_string (")\n");
3309 pdl = backtrace_next (pdl);
3312 Vprint_level = old_print_level;
3313 return Qnil;
3316 static union specbinding *
3317 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3319 union specbinding *pdl = backtrace_top ();
3320 register EMACS_INT i;
3322 CHECK_NATNUM (nframes);
3324 if (!NILP (base))
3325 { /* Skip up to `base'. */
3326 base = Findirect_function (base, Qt);
3327 while (backtrace_p (pdl)
3328 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3329 pdl = backtrace_next (pdl);
3332 /* Find the frame requested. */
3333 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3334 pdl = backtrace_next (pdl);
3336 return pdl;
3339 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3340 doc: /* Return the function and arguments NFRAMES up from current execution point.
3341 If that frame has not evaluated the arguments yet (or is a special form),
3342 the value is (nil FUNCTION ARG-FORMS...).
3343 If that frame has evaluated its arguments and called its function already,
3344 the value is (t FUNCTION ARG-VALUES...).
3345 A &rest arg is represented as the tail of the list ARG-VALUES.
3346 FUNCTION is whatever was supplied as car of evaluated list,
3347 or a lambda expression for macro calls.
3348 If NFRAMES is more than the number of frames, the value is nil.
3349 If BASE is non-nil, it should be a function and NFRAMES counts from its
3350 nearest activation frame. */)
3351 (Lisp_Object nframes, Lisp_Object base)
3353 union specbinding *pdl = get_backtrace_frame (nframes, base);
3355 if (!backtrace_p (pdl))
3356 return Qnil;
3357 if (backtrace_nargs (pdl) == UNEVALLED)
3358 return Fcons (Qnil,
3359 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3360 else
3362 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3364 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3368 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3369 the specpdl stack, and then rewind them. We store the pre-unwind values
3370 directly in the pre-existing specpdl elements (i.e. we swap the current
3371 value and the old value stored in the specpdl), kind of like the inplace
3372 pointer-reversal trick. As it turns out, the rewind does the same as the
3373 unwind, except it starts from the other end of the specpdl stack, so we use
3374 the same function for both unwind and rewind. */
3375 static void
3376 backtrace_eval_unrewind (int distance)
3378 union specbinding *tmp = specpdl_ptr;
3379 int step = -1;
3380 if (distance < 0)
3381 { /* It's a rewind rather than unwind. */
3382 tmp += distance - 1;
3383 step = 1;
3384 distance = -distance;
3387 for (; distance > 0; distance--)
3389 tmp += step;
3390 switch (tmp->kind)
3392 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3393 unwind_protect, but the problem is that we don't know how to
3394 rewind them afterwards. */
3395 case SPECPDL_UNWIND:
3397 Lisp_Object oldarg = tmp->unwind.arg;
3398 if (tmp->unwind.func == set_buffer_if_live)
3399 tmp->unwind.arg = Fcurrent_buffer ();
3400 else if (tmp->unwind.func == save_excursion_restore)
3401 tmp->unwind.arg = save_excursion_save ();
3402 else
3403 break;
3404 tmp->unwind.func (oldarg);
3405 break;
3408 case SPECPDL_UNWIND_PTR:
3409 case SPECPDL_UNWIND_INT:
3410 case SPECPDL_UNWIND_VOID:
3411 case SPECPDL_BACKTRACE:
3412 break;
3413 case SPECPDL_LET:
3414 { /* If variable has a trivial value (no forwarding), we can
3415 just set it. No need to check for constant symbols here,
3416 since that was already done by specbind. */
3417 Lisp_Object sym = specpdl_symbol (tmp);
3418 if (SYMBOLP (sym) && XSYMBOL (sym)->redirect == SYMBOL_PLAINVAL)
3420 Lisp_Object old_value = specpdl_old_value (tmp);
3421 set_specpdl_old_value (tmp, SYMBOL_VAL (XSYMBOL (sym)));
3422 SET_SYMBOL_VAL (XSYMBOL (sym), old_value);
3423 break;
3425 else
3426 { /* FALLTHROUGH!!
3427 NOTE: we only ever come here if make_local_foo was used for
3428 the first time on this var within this let. */
3431 case SPECPDL_LET_DEFAULT:
3433 Lisp_Object sym = specpdl_symbol (tmp);
3434 Lisp_Object old_value = specpdl_old_value (tmp);
3435 set_specpdl_old_value (tmp, Fdefault_value (sym));
3436 Fset_default (sym, old_value);
3438 break;
3439 case SPECPDL_LET_LOCAL:
3441 Lisp_Object symbol = specpdl_symbol (tmp);
3442 Lisp_Object where = specpdl_where (tmp);
3443 Lisp_Object old_value = specpdl_old_value (tmp);
3444 eassert (BUFFERP (where));
3446 /* If this was a local binding, reset the value in the appropriate
3447 buffer, but only if that buffer's binding still exists. */
3448 if (!NILP (Flocal_variable_p (symbol, where)))
3450 set_specpdl_old_value
3451 (tmp, Fbuffer_local_value (symbol, where));
3452 set_internal (symbol, old_value, where, 1);
3455 break;
3460 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3461 doc: /* Evaluate EXP in the context of some activation frame.
3462 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3463 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3465 union specbinding *pdl = get_backtrace_frame (nframes, base);
3466 ptrdiff_t count = SPECPDL_INDEX ();
3467 ptrdiff_t distance = specpdl_ptr - pdl;
3468 eassert (distance >= 0);
3470 if (!backtrace_p (pdl))
3471 error ("Activation frame not found!");
3473 backtrace_eval_unrewind (distance);
3474 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3476 /* Use eval_sub rather than Feval since the main motivation behind
3477 backtrace-eval is to be able to get/set the value of lexical variables
3478 from the debugger. */
3479 return unbind_to (count, eval_sub (exp));
3482 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3483 doc: /* Return names and values of local variables of a stack frame.
3484 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3485 (Lisp_Object nframes, Lisp_Object base)
3487 union specbinding *frame = get_backtrace_frame (nframes, base);
3488 union specbinding *prevframe
3489 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3490 ptrdiff_t distance = specpdl_ptr - frame;
3491 Lisp_Object result = Qnil;
3492 eassert (distance >= 0);
3494 if (!backtrace_p (prevframe))
3495 error ("Activation frame not found!");
3496 if (!backtrace_p (frame))
3497 error ("Activation frame not found!");
3499 /* The specpdl entries normally contain the symbol being bound along with its
3500 `old_value', so it can be restored. The new value to which it is bound is
3501 available in one of two places: either in the current value of the
3502 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3503 next specpdl entry for it.
3504 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3505 and "new value", so we abuse it here, to fetch the new value.
3506 It's ugly (we'd rather not modify global data) and a bit inefficient,
3507 but it does the job for now. */
3508 backtrace_eval_unrewind (distance);
3510 /* Grab values. */
3512 union specbinding *tmp = prevframe;
3513 for (; tmp > frame; tmp--)
3515 switch (tmp->kind)
3517 case SPECPDL_LET:
3518 case SPECPDL_LET_DEFAULT:
3519 case SPECPDL_LET_LOCAL:
3521 Lisp_Object sym = specpdl_symbol (tmp);
3522 Lisp_Object val = specpdl_old_value (tmp);
3523 if (EQ (sym, Qinternal_interpreter_environment))
3525 Lisp_Object env = val;
3526 for (; CONSP (env); env = XCDR (env))
3528 Lisp_Object binding = XCAR (env);
3529 if (CONSP (binding))
3530 result = Fcons (Fcons (XCAR (binding),
3531 XCDR (binding)),
3532 result);
3535 else
3536 result = Fcons (Fcons (sym, val), result);
3538 break;
3540 case SPECPDL_UNWIND:
3541 case SPECPDL_UNWIND_PTR:
3542 case SPECPDL_UNWIND_INT:
3543 case SPECPDL_UNWIND_VOID:
3544 case SPECPDL_BACKTRACE:
3545 break;
3547 default:
3548 emacs_abort ();
3553 /* Restore values from specpdl to original place. */
3554 backtrace_eval_unrewind (-distance);
3556 return result;
3560 void
3561 mark_specpdl (void)
3563 union specbinding *pdl;
3564 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3566 switch (pdl->kind)
3568 case SPECPDL_UNWIND:
3569 mark_object (specpdl_arg (pdl));
3570 break;
3572 case SPECPDL_BACKTRACE:
3574 ptrdiff_t nargs = backtrace_nargs (pdl);
3575 mark_object (backtrace_function (pdl));
3576 if (nargs == UNEVALLED)
3577 nargs = 1;
3578 while (nargs--)
3579 mark_object (backtrace_args (pdl)[nargs]);
3581 break;
3583 case SPECPDL_LET_DEFAULT:
3584 case SPECPDL_LET_LOCAL:
3585 mark_object (specpdl_where (pdl));
3586 /* Fall through. */
3587 case SPECPDL_LET:
3588 mark_object (specpdl_symbol (pdl));
3589 mark_object (specpdl_old_value (pdl));
3590 break;
3592 case SPECPDL_UNWIND_PTR:
3593 case SPECPDL_UNWIND_INT:
3594 case SPECPDL_UNWIND_VOID:
3595 break;
3597 default:
3598 emacs_abort ();
3603 void
3604 get_backtrace (Lisp_Object array)
3606 union specbinding *pdl = backtrace_next (backtrace_top ());
3607 ptrdiff_t i = 0, asize = ASIZE (array);
3609 /* Copy the backtrace contents into working memory. */
3610 for (; i < asize; i++)
3612 if (backtrace_p (pdl))
3614 ASET (array, i, backtrace_function (pdl));
3615 pdl = backtrace_next (pdl);
3617 else
3618 ASET (array, i, Qnil);
3622 Lisp_Object backtrace_top_function (void)
3624 union specbinding *pdl = backtrace_top ();
3625 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3628 void
3629 syms_of_eval (void)
3631 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3632 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3633 If Lisp code tries to increase the total number past this amount,
3634 an error is signaled.
3635 You can safely use a value considerably larger than the default value,
3636 if that proves inconveniently small. However, if you increase it too far,
3637 Emacs could run out of memory trying to make the stack bigger.
3638 Note that this limit may be silently increased by the debugger
3639 if `debug-on-error' or `debug-on-quit' is set. */);
3641 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3642 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3644 This limit serves to catch infinite recursions for you before they cause
3645 actual stack overflow in C, which would be fatal for Emacs.
3646 You can safely make it considerably larger than its default value,
3647 if that proves inconveniently small. However, if you increase it too far,
3648 Emacs could overflow the real C stack, and crash. */);
3650 DEFVAR_LISP ("quit-flag", Vquit_flag,
3651 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3652 If the value is t, that means do an ordinary quit.
3653 If the value equals `throw-on-input', that means quit by throwing
3654 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3655 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3656 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3657 Vquit_flag = Qnil;
3659 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3660 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3661 Note that `quit-flag' will still be set by typing C-g,
3662 so a quit will be signaled as soon as `inhibit-quit' is nil.
3663 To prevent this happening, set `quit-flag' to nil
3664 before making `inhibit-quit' nil. */);
3665 Vinhibit_quit = Qnil;
3667 DEFSYM (Qsetq, "setq");
3668 DEFSYM (Qinhibit_quit, "inhibit-quit");
3669 DEFSYM (Qautoload, "autoload");
3670 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3671 DEFSYM (Qmacro, "macro");
3673 /* Note that the process handling also uses Qexit, but we don't want
3674 to staticpro it twice, so we just do it here. */
3675 DEFSYM (Qexit, "exit");
3677 DEFSYM (Qinteractive, "interactive");
3678 DEFSYM (Qcommandp, "commandp");
3679 DEFSYM (Qand_rest, "&rest");
3680 DEFSYM (Qand_optional, "&optional");
3681 DEFSYM (Qclosure, "closure");
3682 DEFSYM (QCdocumentation, ":documentation");
3683 DEFSYM (Qdebug, "debug");
3685 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3686 doc: /* Non-nil means never enter the debugger.
3687 Normally set while the debugger is already active, to avoid recursive
3688 invocations. */);
3689 Vinhibit_debugger = Qnil;
3691 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3692 doc: /* Non-nil means enter debugger if an error is signaled.
3693 Does not apply to errors handled by `condition-case' or those
3694 matched by `debug-ignored-errors'.
3695 If the value is a list, an error only means to enter the debugger
3696 if one of its condition symbols appears in the list.
3697 When you evaluate an expression interactively, this variable
3698 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3699 The command `toggle-debug-on-error' toggles this.
3700 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3701 Vdebug_on_error = Qnil;
3703 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3704 doc: /* List of errors for which the debugger should not be called.
3705 Each element may be a condition-name or a regexp that matches error messages.
3706 If any element applies to a given error, that error skips the debugger
3707 and just returns to top level.
3708 This overrides the variable `debug-on-error'.
3709 It does not apply to errors handled by `condition-case'. */);
3710 Vdebug_ignored_errors = Qnil;
3712 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3713 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3714 Does not apply if quit is handled by a `condition-case'. */);
3715 debug_on_quit = 0;
3717 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3718 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3720 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3721 doc: /* Non-nil means debugger may continue execution.
3722 This is nil when the debugger is called under circumstances where it
3723 might not be safe to continue. */);
3724 debugger_may_continue = 1;
3726 DEFVAR_LISP ("debugger", Vdebugger,
3727 doc: /* Function to call to invoke debugger.
3728 If due to frame exit, args are `exit' and the value being returned;
3729 this function's value will be returned instead of that.
3730 If due to error, args are `error' and a list of the args to `signal'.
3731 If due to `apply' or `funcall' entry, one arg, `lambda'.
3732 If due to `eval' entry, one arg, t. */);
3733 Vdebugger = Qnil;
3735 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3736 doc: /* If non-nil, this is a function for `signal' to call.
3737 It receives the same arguments that `signal' was given.
3738 The Edebug package uses this to regain control. */);
3739 Vsignal_hook_function = Qnil;
3741 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3742 doc: /* Non-nil means call the debugger regardless of condition handlers.
3743 Note that `debug-on-error', `debug-on-quit' and friends
3744 still determine whether to handle the particular condition. */);
3745 Vdebug_on_signal = Qnil;
3747 /* When lexical binding is being used,
3748 Vinternal_interpreter_environment is non-nil, and contains an alist
3749 of lexically-bound variable, or (t), indicating an empty
3750 environment. The lisp name of this variable would be
3751 `internal-interpreter-environment' if it weren't hidden.
3752 Every element of this list can be either a cons (VAR . VAL)
3753 specifying a lexical binding, or a single symbol VAR indicating
3754 that this variable should use dynamic scoping. */
3755 DEFSYM (Qinternal_interpreter_environment,
3756 "internal-interpreter-environment");
3757 DEFVAR_LISP ("internal-interpreter-environment",
3758 Vinternal_interpreter_environment,
3759 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3760 When lexical binding is not being used, this variable is nil.
3761 A value of `(t)' indicates an empty environment, otherwise it is an
3762 alist of active lexical bindings. */);
3763 Vinternal_interpreter_environment = Qnil;
3764 /* Don't export this variable to Elisp, so no one can mess with it
3765 (Just imagine if someone makes it buffer-local). */
3766 Funintern (Qinternal_interpreter_environment, Qnil);
3768 Vrun_hooks = intern_c_string ("run-hooks");
3769 staticpro (&Vrun_hooks);
3771 staticpro (&Vautoload_queue);
3772 Vautoload_queue = Qnil;
3773 staticpro (&Vsignaling_function);
3774 Vsignaling_function = Qnil;
3776 inhibit_lisp_code = Qnil;
3778 defsubr (&Sor);
3779 defsubr (&Sand);
3780 defsubr (&Sif);
3781 defsubr (&Scond);
3782 defsubr (&Sprogn);
3783 defsubr (&Sprog1);
3784 defsubr (&Sprog2);
3785 defsubr (&Ssetq);
3786 defsubr (&Squote);
3787 defsubr (&Sfunction);
3788 defsubr (&Sdefault_toplevel_value);
3789 defsubr (&Sset_default_toplevel_value);
3790 defsubr (&Sdefvar);
3791 defsubr (&Sdefvaralias);
3792 defsubr (&Sdefconst);
3793 defsubr (&Smake_var_non_special);
3794 defsubr (&Slet);
3795 defsubr (&SletX);
3796 defsubr (&Swhile);
3797 defsubr (&Smacroexpand);
3798 defsubr (&Scatch);
3799 defsubr (&Sthrow);
3800 defsubr (&Sunwind_protect);
3801 defsubr (&Scondition_case);
3802 defsubr (&Ssignal);
3803 defsubr (&Scommandp);
3804 defsubr (&Sautoload);
3805 defsubr (&Sautoload_do_load);
3806 defsubr (&Seval);
3807 defsubr (&Sapply);
3808 defsubr (&Sfuncall);
3809 defsubr (&Srun_hooks);
3810 defsubr (&Srun_hook_with_args);
3811 defsubr (&Srun_hook_with_args_until_success);
3812 defsubr (&Srun_hook_with_args_until_failure);
3813 defsubr (&Srun_hook_wrapped);
3814 defsubr (&Sfetch_bytecode);
3815 defsubr (&Sbacktrace_debug);
3816 defsubr (&Sbacktrace);
3817 defsubr (&Sbacktrace_frame);
3818 defsubr (&Sbacktrace_eval);
3819 defsubr (&Sbacktrace__locals);
3820 defsubr (&Sspecial_variable_p);
3821 defsubr (&Sfunctionp);