Raise an error when detecting old-style backquotes.
[emacs.git] / src / eval.c
blob47c4f17eabc3ab7dcef520958f37d114789e563d
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2017 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 <https://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "lisp.h"
27 #include "blockinput.h"
28 #include "commands.h"
29 #include "keyboard.h"
30 #include "dispextern.h"
31 #include "buffer.h"
33 /* CACHEABLE is ordinarily nothing, except it is 'volatile' if
34 necessary to cajole GCC into not warning incorrectly that a
35 variable should be volatile. */
36 #if defined GCC_LINT || defined lint
37 # define CACHEABLE volatile
38 #else
39 # define CACHEABLE /* empty */
40 #endif
42 /* Chain of condition and catch handlers currently in effect. */
44 /* struct handler *handlerlist; */
46 /* Non-nil means record all fset's and provide's, to be undone
47 if the file being autoloaded is not fully loaded.
48 They are recorded by being consed onto the front of Vautoload_queue:
49 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
51 Lisp_Object Vautoload_queue;
53 /* This holds either the symbol `run-hooks' or nil.
54 It is nil at an early stage of startup, and when Emacs
55 is shutting down. */
56 Lisp_Object Vrun_hooks;
58 /* The commented-out variables below are macros defined in thread.h. */
60 /* Current number of specbindings allocated in specpdl, not counting
61 the dummy entry specpdl[-1]. */
63 /* ptrdiff_t specpdl_size; */
65 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
66 only so that its address can be taken. */
68 /* union specbinding *specpdl; */
70 /* Pointer to first unused element in specpdl. */
72 /* union specbinding *specpdl_ptr; */
74 /* Depth in Lisp evaluations and function calls. */
76 /* static EMACS_INT lisp_eval_depth; */
78 /* The value of num_nonmacro_input_events as of the last time we
79 started to enter the debugger. If we decide to enter the debugger
80 again when this is still equal to num_nonmacro_input_events, then we
81 know that the debugger itself has an error, and we should just
82 signal the error instead of entering an infinite loop of debugger
83 invocations. */
85 static EMACS_INT when_entered_debugger;
87 /* The function from which the last `signal' was called. Set in
88 Fsignal. */
89 /* FIXME: We should probably get rid of this! */
90 Lisp_Object Vsignaling_function;
92 /* If non-nil, Lisp code must not be run since some part of Emacs is in
93 an inconsistent state. Currently unused. */
94 Lisp_Object inhibit_lisp_code;
96 /* These would ordinarily be static, but they need to be visible to GDB. */
97 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
98 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
99 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
100 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
101 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
103 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
104 static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
105 static Lisp_Object lambda_arity (Lisp_Object);
107 static Lisp_Object
108 specpdl_symbol (union specbinding *pdl)
110 eassert (pdl->kind >= SPECPDL_LET);
111 return pdl->let.symbol;
114 static enum specbind_tag
115 specpdl_kind (union specbinding *pdl)
117 eassert (pdl->kind >= SPECPDL_LET);
118 return pdl->let.kind;
121 static Lisp_Object
122 specpdl_old_value (union specbinding *pdl)
124 eassert (pdl->kind >= SPECPDL_LET);
125 return pdl->let.old_value;
128 static void
129 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
131 eassert (pdl->kind >= SPECPDL_LET);
132 pdl->let.old_value = val;
135 static Lisp_Object
136 specpdl_where (union specbinding *pdl)
138 eassert (pdl->kind > SPECPDL_LET);
139 return pdl->let.where;
142 static Lisp_Object
143 specpdl_saved_value (union specbinding *pdl)
145 eassert (pdl->kind >= SPECPDL_LET);
146 return pdl->let.saved_value;
149 static Lisp_Object
150 specpdl_arg (union specbinding *pdl)
152 eassert (pdl->kind == SPECPDL_UNWIND);
153 return pdl->unwind.arg;
156 Lisp_Object
157 backtrace_function (union specbinding *pdl)
159 eassert (pdl->kind == SPECPDL_BACKTRACE);
160 return pdl->bt.function;
163 static ptrdiff_t
164 backtrace_nargs (union specbinding *pdl)
166 eassert (pdl->kind == SPECPDL_BACKTRACE);
167 return pdl->bt.nargs;
170 Lisp_Object *
171 backtrace_args (union specbinding *pdl)
173 eassert (pdl->kind == SPECPDL_BACKTRACE);
174 return pdl->bt.args;
177 static bool
178 backtrace_debug_on_exit (union specbinding *pdl)
180 eassert (pdl->kind == SPECPDL_BACKTRACE);
181 return pdl->bt.debug_on_exit;
184 /* Functions to modify slots of backtrace records. */
186 static void
187 set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
189 eassert (pdl->kind == SPECPDL_BACKTRACE);
190 pdl->bt.args = args;
191 pdl->bt.nargs = nargs;
194 static void
195 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
197 eassert (pdl->kind == SPECPDL_BACKTRACE);
198 pdl->bt.debug_on_exit = doe;
201 /* Helper functions to scan the backtrace. */
203 bool
204 backtrace_p (union specbinding *pdl)
205 { return pdl >= specpdl; }
207 union specbinding *
208 backtrace_top (void)
210 union specbinding *pdl = specpdl_ptr - 1;
211 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
212 pdl--;
213 return pdl;
216 union specbinding *
217 backtrace_next (union specbinding *pdl)
219 pdl--;
220 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
221 pdl--;
222 return pdl;
225 void
226 init_eval_once (void)
228 enum { size = 50 };
229 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
230 specpdl_size = size;
231 specpdl = specpdl_ptr = pdlvec + 1;
232 /* Don't forget to update docs (lispref node "Local Variables"). */
233 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
234 max_lisp_eval_depth = 800;
236 Vrun_hooks = Qnil;
239 /* static struct handler handlerlist_sentinel; */
241 void
242 init_eval (void)
244 specpdl_ptr = specpdl;
245 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
246 This is important since handlerlist->nextfree holds the freelist
247 which would otherwise leak every time we unwind back to top-level. */
248 handlerlist_sentinel = xzalloc (sizeof (struct handler));
249 handlerlist = handlerlist_sentinel->nextfree = handlerlist_sentinel;
250 struct handler *c = push_handler (Qunbound, CATCHER);
251 eassert (c == handlerlist_sentinel);
252 handlerlist_sentinel->nextfree = NULL;
253 handlerlist_sentinel->next = NULL;
255 Vquit_flag = Qnil;
256 debug_on_next_call = 0;
257 lisp_eval_depth = 0;
258 /* This is less than the initial value of num_nonmacro_input_events. */
259 when_entered_debugger = -1;
262 /* Unwind-protect function used by call_debugger. */
264 static void
265 restore_stack_limits (Lisp_Object data)
267 max_specpdl_size = XINT (XCAR (data));
268 max_lisp_eval_depth = XINT (XCDR (data));
271 static void grow_specpdl (void);
273 /* Call the Lisp debugger, giving it argument ARG. */
275 Lisp_Object
276 call_debugger (Lisp_Object arg)
278 bool debug_while_redisplaying;
279 ptrdiff_t count = SPECPDL_INDEX ();
280 Lisp_Object val;
281 EMACS_INT old_depth = max_lisp_eval_depth;
282 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
283 EMACS_INT old_max = max (max_specpdl_size, count);
285 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
286 max_lisp_eval_depth = lisp_eval_depth + 40;
288 /* While debugging Bug#16603, previous value of 100 was found
289 too small to avoid specpdl overflow in the debugger itself. */
290 if (max_specpdl_size - 200 < count)
291 max_specpdl_size = count + 200;
293 if (old_max == count)
295 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
296 specpdl_ptr--;
297 grow_specpdl ();
300 /* Restore limits after leaving the debugger. */
301 record_unwind_protect (restore_stack_limits,
302 Fcons (make_number (old_max),
303 make_number (old_depth)));
305 #ifdef HAVE_WINDOW_SYSTEM
306 if (display_hourglass_p)
307 cancel_hourglass ();
308 #endif
310 debug_on_next_call = 0;
311 when_entered_debugger = num_nonmacro_input_events;
313 /* Resetting redisplaying_p to 0 makes sure that debug output is
314 displayed if the debugger is invoked during redisplay. */
315 debug_while_redisplaying = redisplaying_p;
316 redisplaying_p = 0;
317 specbind (intern ("debugger-may-continue"),
318 debug_while_redisplaying ? Qnil : Qt);
319 specbind (Qinhibit_redisplay, Qnil);
320 specbind (Qinhibit_debugger, Qt);
322 /* If we are debugging an error while `inhibit-changing-match-data'
323 is bound to non-nil (e.g., within a call to `string-match-p'),
324 then make sure debugger code can still use match data. */
325 specbind (Qinhibit_changing_match_data, Qnil);
327 #if 0 /* Binding this prevents execution of Lisp code during
328 redisplay, which necessarily leads to display problems. */
329 specbind (Qinhibit_eval_during_redisplay, Qt);
330 #endif
332 val = apply1 (Vdebugger, arg);
334 /* Interrupting redisplay and resuming it later is not safe under
335 all circumstances. So, when the debugger returns, abort the
336 interrupted redisplay by going back to the top-level. */
337 if (debug_while_redisplaying)
338 Ftop_level ();
340 return unbind_to (count, val);
343 static void
344 do_debug_on_call (Lisp_Object code, ptrdiff_t count)
346 debug_on_next_call = 0;
347 set_backtrace_debug_on_exit (specpdl + count, true);
348 call_debugger (list1 (code));
351 /* NOTE!!! Every function that can call EVAL must protect its args
352 and temporaries from garbage collection while it needs them.
353 The definition of `For' shows what you have to do. */
355 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
356 doc: /* Eval args until one of them yields non-nil, then return that value.
357 The remaining args are not evalled at all.
358 If all args return nil, return nil.
359 usage: (or CONDITIONS...) */)
360 (Lisp_Object args)
362 Lisp_Object val = Qnil;
364 while (CONSP (args))
366 Lisp_Object arg = XCAR (args);
367 args = XCDR (args);
368 val = eval_sub (arg);
369 if (!NILP (val))
370 break;
373 return val;
376 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
377 doc: /* Eval args until one of them yields nil, then return nil.
378 The remaining args are not evalled at all.
379 If no arg yields nil, return the last arg's value.
380 usage: (and CONDITIONS...) */)
381 (Lisp_Object args)
383 Lisp_Object val = Qt;
385 while (CONSP (args))
387 Lisp_Object arg = XCAR (args);
388 args = XCDR (args);
389 val = eval_sub (arg);
390 if (NILP (val))
391 break;
394 return val;
397 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
398 doc: /* If COND yields non-nil, do THEN, else do ELSE...
399 Returns the value of THEN or the value of the last of the ELSE's.
400 THEN must be one expression, but ELSE... can be zero or more expressions.
401 If COND yields nil, and there are no ELSE's, the value is nil.
402 usage: (if COND THEN ELSE...) */)
403 (Lisp_Object args)
405 Lisp_Object cond;
407 cond = eval_sub (XCAR (args));
409 if (!NILP (cond))
410 return eval_sub (Fcar (XCDR (args)));
411 return Fprogn (Fcdr (XCDR (args)));
414 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
415 doc: /* Try each clause until one succeeds.
416 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
417 and, if the value is non-nil, this clause succeeds:
418 then the expressions in BODY are evaluated and the last one's
419 value is the value of the cond-form.
420 If a clause has one element, as in (CONDITION), then the cond-form
421 returns CONDITION's value, if that is non-nil.
422 If no clause succeeds, cond returns nil.
423 usage: (cond CLAUSES...) */)
424 (Lisp_Object args)
426 Lisp_Object val = args;
428 while (CONSP (args))
430 Lisp_Object clause = XCAR (args);
431 val = eval_sub (Fcar (clause));
432 if (!NILP (val))
434 if (!NILP (XCDR (clause)))
435 val = Fprogn (XCDR (clause));
436 break;
438 args = XCDR (args);
441 return val;
444 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
445 doc: /* Eval BODY forms sequentially and return value of last one.
446 usage: (progn BODY...) */)
447 (Lisp_Object body)
449 Lisp_Object val = Qnil;
451 while (CONSP (body))
453 Lisp_Object form = XCAR (body);
454 body = XCDR (body);
455 val = eval_sub (form);
458 return val;
461 /* Evaluate BODY sequentially, discarding its value. */
463 void
464 prog_ignore (Lisp_Object body)
466 Fprogn (body);
469 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
470 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
471 The value of FIRST is saved during the evaluation of the remaining args,
472 whose values are discarded.
473 usage: (prog1 FIRST BODY...) */)
474 (Lisp_Object args)
476 Lisp_Object val = eval_sub (XCAR (args));
477 prog_ignore (XCDR (args));
478 return val;
481 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
482 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
483 The value of FORM2 is saved during the evaluation of the
484 remaining args, whose values are discarded.
485 usage: (prog2 FORM1 FORM2 BODY...) */)
486 (Lisp_Object args)
488 eval_sub (XCAR (args));
489 return Fprog1 (XCDR (args));
492 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
493 doc: /* Set each SYM to the value of its VAL.
494 The symbols SYM are variables; they are literal (not evaluated).
495 The values VAL are expressions; they are evaluated.
496 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
497 The second VAL is not computed until after the first SYM is set, and so on;
498 each VAL can use the new value of variables set earlier in the `setq'.
499 The return value of the `setq' form is the value of the last VAL.
500 usage: (setq [SYM VAL]...) */)
501 (Lisp_Object args)
503 Lisp_Object val = args, tail = args;
505 for (EMACS_INT nargs = 0; CONSP (tail); nargs += 2)
507 Lisp_Object sym = XCAR (tail), lex_binding;
508 tail = XCDR (tail);
509 if (!CONSP (tail))
510 xsignal2 (Qwrong_number_of_arguments, Qsetq, make_number (nargs + 1));
511 Lisp_Object arg = XCAR (tail);
512 tail = XCDR (tail);
513 val = eval_sub (arg);
514 /* Like for eval_sub, we do not check declared_special here since
515 it's been done when let-binding. */
516 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
517 && SYMBOLP (sym)
518 && !NILP (lex_binding
519 = Fassq (sym, Vinternal_interpreter_environment)))
520 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
521 else
522 Fset (sym, val); /* SYM is dynamically bound. */
525 return val;
528 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
529 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
530 Warning: `quote' does not construct its return value, but just returns
531 the value that was pre-constructed by the Lisp reader (see info node
532 `(elisp)Printed Representation').
533 This means that \\='(a . b) is not identical to (cons \\='a \\='b): the former
534 does not cons. Quoting should be reserved for constants that will
535 never be modified by side-effects, unless you like self-modifying code.
536 See the common pitfall in info node `(elisp)Rearrangement' for an example
537 of unexpected results when a quoted object is modified.
538 usage: (quote ARG) */)
539 (Lisp_Object args)
541 if (!NILP (XCDR (args)))
542 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
543 return XCAR (args);
546 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
547 doc: /* Like `quote', but preferred for objects which are functions.
548 In byte compilation, `function' causes its argument to be compiled.
549 `quote' cannot do that.
550 usage: (function ARG) */)
551 (Lisp_Object args)
553 Lisp_Object quoted = XCAR (args);
555 if (!NILP (XCDR (args)))
556 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
558 if (!NILP (Vinternal_interpreter_environment)
559 && CONSP (quoted)
560 && EQ (XCAR (quoted), Qlambda))
561 { /* This is a lambda expression within a lexical environment;
562 return an interpreted closure instead of a simple lambda. */
563 Lisp_Object cdr = XCDR (quoted);
564 Lisp_Object tmp = cdr;
565 if (CONSP (tmp)
566 && (tmp = XCDR (tmp), CONSP (tmp))
567 && (tmp = XCAR (tmp), CONSP (tmp))
568 && (EQ (QCdocumentation, XCAR (tmp))))
569 { /* Handle the special (:documentation <form>) to build the docstring
570 dynamically. */
571 Lisp_Object docstring = eval_sub (Fcar (XCDR (tmp)));
572 CHECK_STRING (docstring);
573 cdr = Fcons (XCAR (cdr), Fcons (docstring, XCDR (XCDR (cdr))));
575 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
576 cdr));
578 else
579 /* Simply quote the argument. */
580 return quoted;
584 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
585 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
586 Aliased variables always have the same value; setting one sets the other.
587 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
588 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
589 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
590 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
591 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
592 The return value is BASE-VARIABLE. */)
593 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
595 struct Lisp_Symbol *sym;
597 CHECK_SYMBOL (new_alias);
598 CHECK_SYMBOL (base_variable);
600 if (SYMBOL_CONSTANT_P (new_alias))
601 /* Making it an alias effectively changes its value. */
602 error ("Cannot make a constant an alias");
604 sym = XSYMBOL (new_alias);
606 switch (sym->u.s.redirect)
608 case SYMBOL_FORWARDED:
609 error ("Cannot make an internal variable an alias");
610 case SYMBOL_LOCALIZED:
611 error ("Don't know how to make a localized variable an alias");
612 case SYMBOL_PLAINVAL:
613 case SYMBOL_VARALIAS:
614 break;
615 default:
616 emacs_abort ();
619 /* https://lists.gnu.org/r/emacs-devel/2008-04/msg00834.html
620 If n_a is bound, but b_v is not, set the value of b_v to n_a,
621 so that old-code that affects n_a before the aliasing is setup
622 still works. */
623 if (NILP (Fboundp (base_variable)))
624 set_internal (base_variable, find_symbol_value (new_alias),
625 Qnil, SET_INTERNAL_BIND);
627 union specbinding *p;
629 for (p = specpdl_ptr; p > specpdl; )
630 if ((--p)->kind >= SPECPDL_LET
631 && (EQ (new_alias, specpdl_symbol (p))))
632 error ("Don't know how to make a let-bound variable an alias");
635 if (sym->u.s.trapped_write == SYMBOL_TRAPPED_WRITE)
636 notify_variable_watchers (new_alias, base_variable, Qdefvaralias, Qnil);
638 sym->u.s.declared_special = true;
639 XSYMBOL (base_variable)->u.s.declared_special = true;
640 sym->u.s.redirect = SYMBOL_VARALIAS;
641 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
642 sym->u.s.trapped_write = XSYMBOL (base_variable)->u.s.trapped_write;
643 LOADHIST_ATTACH (new_alias);
644 /* Even if docstring is nil: remove old docstring. */
645 Fput (new_alias, Qvariable_documentation, docstring);
647 return base_variable;
650 static union specbinding *
651 default_toplevel_binding (Lisp_Object symbol)
653 union specbinding *binding = NULL;
654 union specbinding *pdl = specpdl_ptr;
655 while (pdl > specpdl)
657 switch ((--pdl)->kind)
659 case SPECPDL_LET_DEFAULT:
660 case SPECPDL_LET:
661 if (EQ (specpdl_symbol (pdl), symbol))
662 binding = pdl;
663 break;
665 case SPECPDL_UNWIND:
666 case SPECPDL_UNWIND_PTR:
667 case SPECPDL_UNWIND_INT:
668 case SPECPDL_UNWIND_VOID:
669 case SPECPDL_BACKTRACE:
670 case SPECPDL_LET_LOCAL:
671 break;
673 default:
674 emacs_abort ();
677 return binding;
680 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
681 doc: /* Return SYMBOL's toplevel default value.
682 "Toplevel" means outside of any let binding. */)
683 (Lisp_Object symbol)
685 union specbinding *binding = default_toplevel_binding (symbol);
686 Lisp_Object value
687 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
688 if (!EQ (value, Qunbound))
689 return value;
690 xsignal1 (Qvoid_variable, symbol);
693 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
694 Sset_default_toplevel_value, 2, 2, 0,
695 doc: /* Set SYMBOL's toplevel default value to VALUE.
696 "Toplevel" means outside of any let binding. */)
697 (Lisp_Object symbol, Lisp_Object value)
699 union specbinding *binding = default_toplevel_binding (symbol);
700 if (binding)
701 set_specpdl_old_value (binding, value);
702 else
703 Fset_default (symbol, value);
704 return Qnil;
707 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
708 doc: /* Define SYMBOL as a variable, and return SYMBOL.
709 You are not required to define a variable in order to use it, but
710 defining it lets you supply an initial value and documentation, which
711 can be referred to by the Emacs help facilities and other programming
712 tools. The `defvar' form also declares the variable as \"special\",
713 so that it is always dynamically bound even if `lexical-binding' is t.
715 If SYMBOL's value is void and the optional argument INITVALUE is
716 provided, INITVALUE is evaluated and the result used to set SYMBOL's
717 value. If SYMBOL is buffer-local, its default value is what is set;
718 buffer-local values are not affected. If INITVALUE is missing,
719 SYMBOL's value is not set.
721 If SYMBOL has a local binding, then this form affects the local
722 binding. This is usually not what you want. Thus, if you need to
723 load a file defining variables, with this form or with `defconst' or
724 `defcustom', you should always load that file _outside_ any bindings
725 for these variables. (`defconst' and `defcustom' behave similarly in
726 this respect.)
728 The optional argument DOCSTRING is a documentation string for the
729 variable.
731 To define a user option, use `defcustom' instead of `defvar'.
732 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
733 (Lisp_Object args)
735 Lisp_Object sym, tem, tail;
737 sym = XCAR (args);
738 tail = XCDR (args);
740 if (!NILP (tail))
742 if (!NILP (XCDR (tail)) && !NILP (XCDR (XCDR (tail))))
743 error ("Too many arguments");
745 tem = Fdefault_boundp (sym);
747 /* Do it before evaluating the initial value, for self-references. */
748 XSYMBOL (sym)->u.s.declared_special = true;
750 if (NILP (tem))
751 Fset_default (sym, eval_sub (XCAR (tail)));
752 else
753 { /* Check if there is really a global binding rather than just a let
754 binding that shadows the global unboundness of the var. */
755 union specbinding *binding = default_toplevel_binding (sym);
756 if (binding && EQ (specpdl_old_value (binding), Qunbound))
758 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
761 tail = XCDR (tail);
762 tem = Fcar (tail);
763 if (!NILP (tem))
765 if (!NILP (Vpurify_flag))
766 tem = Fpurecopy (tem);
767 Fput (sym, Qvariable_documentation, tem);
769 LOADHIST_ATTACH (sym);
771 else if (!NILP (Vinternal_interpreter_environment)
772 && !XSYMBOL (sym)->u.s.declared_special)
773 /* A simple (defvar foo) with lexical scoping does "nothing" except
774 declare that var to be dynamically scoped *locally* (i.e. within
775 the current file or let-block). */
776 Vinternal_interpreter_environment
777 = Fcons (sym, Vinternal_interpreter_environment);
778 else
780 /* Simple (defvar <var>) should not count as a definition at all.
781 It could get in the way of other definitions, and unloading this
782 package could try to make the variable unbound. */
785 return sym;
788 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
789 doc: /* Define SYMBOL as a constant variable.
790 This declares that neither programs nor users should ever change the
791 value. This constancy is not actually enforced by Emacs Lisp, but
792 SYMBOL is marked as a special variable so that it is never lexically
793 bound.
795 The `defconst' form always sets the value of SYMBOL to the result of
796 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
797 what is set; buffer-local values are not affected. If SYMBOL has a
798 local binding, then this form sets the local binding's value.
799 However, you should normally not make local bindings for variables
800 defined with this form.
802 The optional DOCSTRING specifies the variable's documentation string.
803 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
804 (Lisp_Object args)
806 Lisp_Object sym, tem;
808 sym = XCAR (args);
809 Lisp_Object docstring = Qnil;
810 if (!NILP (XCDR (XCDR (args))))
812 if (!NILP (XCDR (XCDR (XCDR (args)))))
813 error ("Too many arguments");
814 docstring = XCAR (XCDR (XCDR (args)));
817 tem = eval_sub (XCAR (XCDR (args)));
818 if (!NILP (Vpurify_flag))
819 tem = Fpurecopy (tem);
820 Fset_default (sym, tem);
821 XSYMBOL (sym)->u.s.declared_special = true;
822 if (!NILP (docstring))
824 if (!NILP (Vpurify_flag))
825 docstring = Fpurecopy (docstring);
826 Fput (sym, Qvariable_documentation, docstring);
828 Fput (sym, Qrisky_local_variable, Qt);
829 LOADHIST_ATTACH (sym);
830 return sym;
833 /* Make SYMBOL lexically scoped. */
834 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
835 Smake_var_non_special, 1, 1, 0,
836 doc: /* Internal function. */)
837 (Lisp_Object symbol)
839 CHECK_SYMBOL (symbol);
840 XSYMBOL (symbol)->u.s.declared_special = false;
841 return Qnil;
845 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
846 doc: /* Bind variables according to VARLIST then eval BODY.
847 The value of the last form in BODY is returned.
848 Each element of VARLIST is a symbol (which is bound to nil)
849 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
850 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
851 usage: (let* VARLIST BODY...) */)
852 (Lisp_Object args)
854 Lisp_Object var, val, elt, lexenv;
855 ptrdiff_t count = SPECPDL_INDEX ();
857 lexenv = Vinternal_interpreter_environment;
859 Lisp_Object varlist = XCAR (args);
860 while (CONSP (varlist))
862 maybe_quit ();
864 elt = XCAR (varlist);
865 varlist = XCDR (varlist);
866 if (SYMBOLP (elt))
868 var = elt;
869 val = Qnil;
871 else
873 var = Fcar (elt);
874 if (! NILP (Fcdr (XCDR (elt))))
875 signal_error ("`let' bindings can have only one value-form", elt);
876 val = eval_sub (Fcar (XCDR (elt)));
879 if (!NILP (lexenv) && SYMBOLP (var)
880 && !XSYMBOL (var)->u.s.declared_special
881 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
882 /* Lexically bind VAR by adding it to the interpreter's binding
883 alist. */
885 Lisp_Object newenv
886 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
887 if (EQ (Vinternal_interpreter_environment, lexenv))
888 /* Save the old lexical environment on the specpdl stack,
889 but only for the first lexical binding, since we'll never
890 need to revert to one of the intermediate ones. */
891 specbind (Qinternal_interpreter_environment, newenv);
892 else
893 Vinternal_interpreter_environment = newenv;
895 else
896 specbind (var, val);
898 CHECK_LIST_END (varlist, XCAR (args));
900 val = Fprogn (XCDR (args));
901 return unbind_to (count, val);
904 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
905 doc: /* Bind variables according to VARLIST then eval BODY.
906 The value of the last form in BODY is returned.
907 Each element of VARLIST is a symbol (which is bound to nil)
908 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
909 All the VALUEFORMs are evalled before any symbols are bound.
910 usage: (let VARLIST BODY...) */)
911 (Lisp_Object args)
913 Lisp_Object *temps, tem, lexenv;
914 Lisp_Object elt, varlist;
915 ptrdiff_t count = SPECPDL_INDEX ();
916 ptrdiff_t argnum;
917 USE_SAFE_ALLOCA;
919 varlist = XCAR (args);
920 CHECK_LIST (varlist);
922 /* Make space to hold the values to give the bound variables. */
923 EMACS_INT varlist_len = XFASTINT (Flength (varlist));
924 SAFE_ALLOCA_LISP (temps, varlist_len);
925 ptrdiff_t nvars = varlist_len;
927 /* Compute the values and store them in `temps'. */
929 for (argnum = 0; argnum < nvars && CONSP (varlist); argnum++)
931 maybe_quit ();
932 elt = XCAR (varlist);
933 varlist = XCDR (varlist);
934 if (SYMBOLP (elt))
935 temps[argnum] = Qnil;
936 else if (! NILP (Fcdr (Fcdr (elt))))
937 signal_error ("`let' bindings can have only one value-form", elt);
938 else
939 temps[argnum] = eval_sub (Fcar (Fcdr (elt)));
941 nvars = argnum;
943 lexenv = Vinternal_interpreter_environment;
945 varlist = XCAR (args);
946 for (argnum = 0; argnum < nvars && CONSP (varlist); argnum++)
948 Lisp_Object var;
950 elt = XCAR (varlist);
951 varlist = XCDR (varlist);
952 var = SYMBOLP (elt) ? elt : Fcar (elt);
953 tem = temps[argnum];
955 if (!NILP (lexenv) && SYMBOLP (var)
956 && !XSYMBOL (var)->u.s.declared_special
957 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
958 /* Lexically bind VAR by adding it to the lexenv alist. */
959 lexenv = Fcons (Fcons (var, tem), lexenv);
960 else
961 /* Dynamically bind VAR. */
962 specbind (var, tem);
965 if (!EQ (lexenv, Vinternal_interpreter_environment))
966 /* Instantiate a new lexical environment. */
967 specbind (Qinternal_interpreter_environment, lexenv);
969 elt = Fprogn (XCDR (args));
970 SAFE_FREE ();
971 return unbind_to (count, elt);
974 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
975 doc: /* If TEST yields non-nil, eval BODY... and repeat.
976 The order of execution is thus TEST, BODY, TEST, BODY and so on
977 until TEST returns nil.
978 usage: (while TEST BODY...) */)
979 (Lisp_Object args)
981 Lisp_Object test, body;
983 test = XCAR (args);
984 body = XCDR (args);
985 while (!NILP (eval_sub (test)))
987 maybe_quit ();
988 prog_ignore (body);
991 return Qnil;
994 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
995 doc: /* Return result of expanding macros at top level of FORM.
996 If FORM is not a macro call, it is returned unchanged.
997 Otherwise, the macro is expanded and the expansion is considered
998 in place of FORM. When a non-macro-call results, it is returned.
1000 The second optional arg ENVIRONMENT specifies an environment of macro
1001 definitions to shadow the loaded ones for use in file byte-compilation. */)
1002 (Lisp_Object form, Lisp_Object environment)
1004 /* With cleanups from Hallvard Furuseth. */
1005 register Lisp_Object expander, sym, def, tem;
1007 while (1)
1009 /* Come back here each time we expand a macro call,
1010 in case it expands into another macro call. */
1011 if (!CONSP (form))
1012 break;
1013 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1014 def = sym = XCAR (form);
1015 tem = Qnil;
1016 /* Trace symbols aliases to other symbols
1017 until we get a symbol that is not an alias. */
1018 while (SYMBOLP (def))
1020 maybe_quit ();
1021 sym = def;
1022 tem = Fassq (sym, environment);
1023 if (NILP (tem))
1025 def = XSYMBOL (sym)->u.s.function;
1026 if (!NILP (def))
1027 continue;
1029 break;
1031 /* Right now TEM is the result from SYM in ENVIRONMENT,
1032 and if TEM is nil then DEF is SYM's function definition. */
1033 if (NILP (tem))
1035 /* SYM is not mentioned in ENVIRONMENT.
1036 Look at its function definition. */
1037 def = Fautoload_do_load (def, sym, Qmacro);
1038 if (!CONSP (def))
1039 /* Not defined or definition not suitable. */
1040 break;
1041 if (!EQ (XCAR (def), Qmacro))
1042 break;
1043 else expander = XCDR (def);
1045 else
1047 expander = XCDR (tem);
1048 if (NILP (expander))
1049 break;
1052 Lisp_Object newform = apply1 (expander, XCDR (form));
1053 if (EQ (form, newform))
1054 break;
1055 else
1056 form = newform;
1059 return form;
1062 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1063 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1064 TAG is evalled to get the tag to use; it must not be nil.
1066 Then the BODY is executed.
1067 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1068 If no throw happens, `catch' returns the value of the last BODY form.
1069 If a throw happens, it specifies the value to return from `catch'.
1070 usage: (catch TAG BODY...) */)
1071 (Lisp_Object args)
1073 Lisp_Object tag = eval_sub (XCAR (args));
1074 return internal_catch (tag, Fprogn, XCDR (args));
1077 /* Assert that E is true, but do not evaluate E. Use this instead of
1078 eassert (E) when E contains variables that might be clobbered by a
1079 longjmp. */
1081 #define clobbered_eassert(E) verify (sizeof (E) != 0)
1083 /* Set up a catch, then call C function FUNC on argument ARG.
1084 FUNC should return a Lisp_Object.
1085 This is how catches are done from within C code. */
1087 Lisp_Object
1088 internal_catch (Lisp_Object tag,
1089 Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1091 /* This structure is made part of the chain `catchlist'. */
1092 struct handler *c = push_handler (tag, CATCHER);
1094 /* Call FUNC. */
1095 if (! sys_setjmp (c->jmp))
1097 Lisp_Object val = func (arg);
1098 eassert (handlerlist == c);
1099 handlerlist = c->next;
1100 return val;
1102 else
1103 { /* Throw works by a longjmp that comes right here. */
1104 Lisp_Object val = handlerlist->val;
1105 clobbered_eassert (handlerlist == c);
1106 handlerlist = handlerlist->next;
1107 return val;
1111 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1112 jump to that CATCH, returning VALUE as the value of that catch.
1114 This is the guts of Fthrow and Fsignal; they differ only in the way
1115 they choose the catch tag to throw to. A catch tag for a
1116 condition-case form has a TAG of Qnil.
1118 Before each catch is discarded, unbind all special bindings and
1119 execute all unwind-protect clauses made above that catch. Unwind
1120 the handler stack as we go, so that the proper handlers are in
1121 effect for each unwind-protect clause we run. At the end, restore
1122 some static info saved in CATCH, and longjmp to the location
1123 specified there.
1125 This is used for correct unwinding in Fthrow and Fsignal. */
1127 static _Noreturn void
1128 unwind_to_catch (struct handler *catch, Lisp_Object value)
1130 bool last_time;
1132 eassert (catch->next);
1134 /* Save the value in the tag. */
1135 catch->val = value;
1137 /* Restore certain special C variables. */
1138 set_poll_suppress_count (catch->poll_suppress_count);
1139 unblock_input_to (catch->interrupt_input_blocked);
1143 /* Unwind the specpdl stack, and then restore the proper set of
1144 handlers. */
1145 unbind_to (handlerlist->pdlcount, Qnil);
1146 last_time = handlerlist == catch;
1147 if (! last_time)
1148 handlerlist = handlerlist->next;
1150 while (! last_time);
1152 eassert (handlerlist == catch);
1154 lisp_eval_depth = catch->f_lisp_eval_depth;
1156 sys_longjmp (catch->jmp, 1);
1159 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1160 doc: /* Throw to the catch for TAG and return VALUE from it.
1161 Both TAG and VALUE are evalled. */
1162 attributes: noreturn)
1163 (register Lisp_Object tag, Lisp_Object value)
1165 struct handler *c;
1167 if (!NILP (tag))
1168 for (c = handlerlist; c; c = c->next)
1170 if (c->type == CATCHER_ALL)
1171 unwind_to_catch (c, Fcons (tag, value));
1172 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1173 unwind_to_catch (c, value);
1175 xsignal2 (Qno_catch, tag, value);
1179 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1180 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1181 If BODYFORM completes normally, its value is returned
1182 after executing the UNWINDFORMS.
1183 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1184 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1185 (Lisp_Object args)
1187 Lisp_Object val;
1188 ptrdiff_t count = SPECPDL_INDEX ();
1190 record_unwind_protect (prog_ignore, XCDR (args));
1191 val = eval_sub (XCAR (args));
1192 return unbind_to (count, val);
1195 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1196 doc: /* Regain control when an error is signaled.
1197 Executes BODYFORM and returns its value if no error happens.
1198 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1199 where the BODY is made of Lisp expressions.
1201 A handler is applicable to an error
1202 if CONDITION-NAME is one of the error's condition names.
1203 If an error happens, the first applicable handler is run.
1205 The car of a handler may be a list of condition names instead of a
1206 single condition name; then it handles all of them. If the special
1207 condition name `debug' is present in this list, it allows another
1208 condition in the list to run the debugger if `debug-on-error' and the
1209 other usual mechanisms says it should (otherwise, `condition-case'
1210 suppresses the debugger).
1212 When a handler handles an error, control returns to the `condition-case'
1213 and it executes the handler's BODY...
1214 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1215 \(If VAR is nil, the handler can't access that information.)
1216 Then the value of the last BODY form is returned from the `condition-case'
1217 expression.
1219 See also the function `signal' for more info.
1220 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1221 (Lisp_Object args)
1223 Lisp_Object var = XCAR (args);
1224 Lisp_Object bodyform = XCAR (XCDR (args));
1225 Lisp_Object handlers = XCDR (XCDR (args));
1227 return internal_lisp_condition_case (var, bodyform, handlers);
1230 /* Like Fcondition_case, but the args are separate
1231 rather than passed in a list. Used by Fbyte_code. */
1233 Lisp_Object
1234 internal_lisp_condition_case (Lisp_Object var, Lisp_Object bodyform,
1235 Lisp_Object handlers)
1237 struct handler *oldhandlerlist = handlerlist;
1238 ptrdiff_t CACHEABLE clausenb = 0;
1240 CHECK_SYMBOL (var);
1242 for (Lisp_Object tail = handlers; CONSP (tail); tail = XCDR (tail))
1244 Lisp_Object tem = XCAR (tail);
1245 clausenb++;
1246 if (! (NILP (tem)
1247 || (CONSP (tem)
1248 && (SYMBOLP (XCAR (tem))
1249 || CONSP (XCAR (tem))))))
1250 error ("Invalid condition handler: %s",
1251 SDATA (Fprin1_to_string (tem, Qt)));
1254 /* The first clause is the one that should be checked first, so it
1255 should be added to handlerlist last. So build in CLAUSES a table
1256 that contains HANDLERS but in reverse order. CLAUSES is pointer
1257 to volatile to avoid issues with setjmp and local storage.
1258 SAFE_ALLOCA won't work here due to the setjmp, so impose a
1259 MAX_ALLOCA limit. */
1260 if (MAX_ALLOCA / word_size < clausenb)
1261 memory_full (SIZE_MAX);
1262 Lisp_Object volatile *clauses = alloca (clausenb * sizeof *clauses);
1263 clauses += clausenb;
1264 for (Lisp_Object tail = handlers; CONSP (tail); tail = XCDR (tail))
1265 *--clauses = XCAR (tail);
1266 for (ptrdiff_t i = 0; i < clausenb; i++)
1268 Lisp_Object clause = clauses[i];
1269 Lisp_Object condition = CONSP (clause) ? XCAR (clause) : Qnil;
1270 if (!CONSP (condition))
1271 condition = list1 (condition);
1272 struct handler *c = push_handler (condition, CONDITION_CASE);
1273 if (sys_setjmp (c->jmp))
1275 Lisp_Object val = handlerlist->val;
1276 Lisp_Object volatile *chosen_clause = clauses;
1277 for (struct handler *h = handlerlist->next; h != oldhandlerlist;
1278 h = h->next)
1279 chosen_clause++;
1280 Lisp_Object handler_body = XCDR (*chosen_clause);
1281 handlerlist = oldhandlerlist;
1283 if (NILP (var))
1284 return Fprogn (handler_body);
1286 Lisp_Object handler_var = var;
1287 if (!NILP (Vinternal_interpreter_environment))
1289 val = Fcons (Fcons (var, val),
1290 Vinternal_interpreter_environment);
1291 handler_var = Qinternal_interpreter_environment;
1294 /* Bind HANDLER_VAR to VAL while evaluating HANDLER_BODY.
1295 The unbind_to undoes just this binding; whoever longjumped
1296 to us unwound the stack to C->pdlcount before throwing. */
1297 ptrdiff_t count = SPECPDL_INDEX ();
1298 specbind (handler_var, val);
1299 return unbind_to (count, Fprogn (handler_body));
1303 Lisp_Object result = eval_sub (bodyform);
1304 handlerlist = oldhandlerlist;
1305 return result;
1308 /* Call the function BFUN with no arguments, catching errors within it
1309 according to HANDLERS. If there is an error, call HFUN with
1310 one argument which is the data that describes the error:
1311 (SIGNALNAME . DATA)
1313 HANDLERS can be a list of conditions to catch.
1314 If HANDLERS is Qt, catch all errors.
1315 If HANDLERS is Qerror, catch all errors
1316 but allow the debugger to run if that is enabled. */
1318 Lisp_Object
1319 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1320 Lisp_Object (*hfun) (Lisp_Object))
1322 struct handler *c = push_handler (handlers, CONDITION_CASE);
1323 if (sys_setjmp (c->jmp))
1325 Lisp_Object val = handlerlist->val;
1326 clobbered_eassert (handlerlist == c);
1327 handlerlist = handlerlist->next;
1328 return hfun (val);
1330 else
1332 Lisp_Object val = bfun ();
1333 eassert (handlerlist == c);
1334 handlerlist = c->next;
1335 return val;
1339 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1341 Lisp_Object
1342 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1343 Lisp_Object handlers,
1344 Lisp_Object (*hfun) (Lisp_Object))
1346 struct handler *c = push_handler (handlers, CONDITION_CASE);
1347 if (sys_setjmp (c->jmp))
1349 Lisp_Object val = handlerlist->val;
1350 clobbered_eassert (handlerlist == c);
1351 handlerlist = handlerlist->next;
1352 return hfun (val);
1354 else
1356 Lisp_Object val = bfun (arg);
1357 eassert (handlerlist == c);
1358 handlerlist = c->next;
1359 return val;
1363 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1364 its arguments. */
1366 Lisp_Object
1367 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1368 Lisp_Object arg1,
1369 Lisp_Object arg2,
1370 Lisp_Object handlers,
1371 Lisp_Object (*hfun) (Lisp_Object))
1373 struct handler *c = push_handler (handlers, CONDITION_CASE);
1374 if (sys_setjmp (c->jmp))
1376 Lisp_Object val = handlerlist->val;
1377 clobbered_eassert (handlerlist == c);
1378 handlerlist = handlerlist->next;
1379 return hfun (val);
1381 else
1383 Lisp_Object val = bfun (arg1, arg2);
1384 eassert (handlerlist == c);
1385 handlerlist = c->next;
1386 return val;
1390 /* Like internal_condition_case but call BFUN with NARGS as first,
1391 and ARGS as second argument. */
1393 Lisp_Object
1394 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1395 ptrdiff_t nargs,
1396 Lisp_Object *args,
1397 Lisp_Object handlers,
1398 Lisp_Object (*hfun) (Lisp_Object err,
1399 ptrdiff_t nargs,
1400 Lisp_Object *args))
1402 struct handler *c = push_handler (handlers, CONDITION_CASE);
1403 if (sys_setjmp (c->jmp))
1405 Lisp_Object val = handlerlist->val;
1406 clobbered_eassert (handlerlist == c);
1407 handlerlist = handlerlist->next;
1408 return hfun (val, nargs, args);
1410 else
1412 Lisp_Object val = bfun (nargs, args);
1413 eassert (handlerlist == c);
1414 handlerlist = c->next;
1415 return val;
1419 struct handler *
1420 push_handler (Lisp_Object tag_ch_val, enum handlertype handlertype)
1422 struct handler *c = push_handler_nosignal (tag_ch_val, handlertype);
1423 if (!c)
1424 memory_full (sizeof *c);
1425 return c;
1428 struct handler *
1429 push_handler_nosignal (Lisp_Object tag_ch_val, enum handlertype handlertype)
1431 struct handler *CACHEABLE c = handlerlist->nextfree;
1432 if (!c)
1434 c = malloc (sizeof *c);
1435 if (!c)
1436 return c;
1437 if (profiler_memory_running)
1438 malloc_probe (sizeof *c);
1439 c->nextfree = NULL;
1440 handlerlist->nextfree = c;
1442 c->type = handlertype;
1443 c->tag_or_ch = tag_ch_val;
1444 c->val = Qnil;
1445 c->next = handlerlist;
1446 c->f_lisp_eval_depth = lisp_eval_depth;
1447 c->pdlcount = SPECPDL_INDEX ();
1448 c->poll_suppress_count = poll_suppress_count;
1449 c->interrupt_input_blocked = interrupt_input_blocked;
1450 handlerlist = c;
1451 return c;
1455 static Lisp_Object signal_or_quit (Lisp_Object, Lisp_Object, bool);
1456 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1457 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1458 Lisp_Object data);
1460 static void
1461 process_quit_flag (void)
1463 Lisp_Object flag = Vquit_flag;
1464 Vquit_flag = Qnil;
1465 if (EQ (flag, Qkill_emacs))
1466 Fkill_emacs (Qnil);
1467 if (EQ (Vthrow_on_input, flag))
1468 Fthrow (Vthrow_on_input, Qt);
1469 quit ();
1472 /* Check quit-flag and quit if it is non-nil. Typing C-g does not
1473 directly cause a quit; it only sets Vquit_flag. So the program
1474 needs to call maybe_quit at times when it is safe to quit. Every
1475 loop that might run for a long time or might not exit ought to call
1476 maybe_quit at least once, at a safe place. Unless that is
1477 impossible, of course. But it is very desirable to avoid creating
1478 loops where maybe_quit is impossible.
1480 If quit-flag is set to `kill-emacs' the SIGINT handler has received
1481 a request to exit Emacs when it is safe to do.
1483 When not quitting, process any pending signals.
1485 If you change this function, also adapt module_should_quit in
1486 emacs-module.c. */
1488 void
1489 maybe_quit (void)
1491 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
1492 process_quit_flag ();
1493 else if (pending_signals)
1494 process_pending_signals ();
1497 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1498 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1499 This function does not return.
1501 An error symbol is a symbol with an `error-conditions' property
1502 that is a list of condition names.
1503 A handler for any of those names will get to handle this signal.
1504 The symbol `error' should normally be one of them.
1506 DATA should be a list. Its elements are printed as part of the error message.
1507 See Info anchor `(elisp)Definition of signal' for some details on how this
1508 error message is constructed.
1509 If the signal is handled, DATA is made available to the handler.
1510 See also the function `condition-case'. */
1511 attributes: noreturn)
1512 (Lisp_Object error_symbol, Lisp_Object data)
1514 signal_or_quit (error_symbol, data, false);
1515 eassume (false);
1518 /* Quit, in response to a keyboard quit request. */
1519 Lisp_Object
1520 quit (void)
1522 return signal_or_quit (Qquit, Qnil, true);
1525 /* Signal an error, or quit. ERROR_SYMBOL and DATA are as with Fsignal.
1526 If KEYBOARD_QUIT, this is a quit; ERROR_SYMBOL should be
1527 Qquit and DATA should be Qnil, and this function may return.
1528 Otherwise this function is like Fsignal and does not return. */
1530 static Lisp_Object
1531 signal_or_quit (Lisp_Object error_symbol, Lisp_Object data, bool keyboard_quit)
1533 /* When memory is full, ERROR-SYMBOL is nil,
1534 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1535 That is a special case--don't do this in other situations. */
1536 Lisp_Object conditions;
1537 Lisp_Object string;
1538 Lisp_Object real_error_symbol
1539 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1540 Lisp_Object clause = Qnil;
1541 struct handler *h;
1543 if (gc_in_progress || waiting_for_input)
1544 emacs_abort ();
1546 #if 0 /* rms: I don't know why this was here,
1547 but it is surely wrong for an error that is handled. */
1548 #ifdef HAVE_WINDOW_SYSTEM
1549 if (display_hourglass_p)
1550 cancel_hourglass ();
1551 #endif
1552 #endif
1554 /* This hook is used by edebug. */
1555 if (! NILP (Vsignal_hook_function)
1556 && ! NILP (error_symbol))
1558 /* Edebug takes care of restoring these variables when it exits. */
1559 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1560 max_lisp_eval_depth = lisp_eval_depth + 20;
1562 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1563 max_specpdl_size = SPECPDL_INDEX () + 40;
1565 call2 (Vsignal_hook_function, error_symbol, data);
1568 conditions = Fget (real_error_symbol, Qerror_conditions);
1570 /* Remember from where signal was called. Skip over the frame for
1571 `signal' itself. If a frame for `error' follows, skip that,
1572 too. Don't do this when ERROR_SYMBOL is nil, because that
1573 is a memory-full error. */
1574 Vsignaling_function = Qnil;
1575 if (!NILP (error_symbol))
1577 union specbinding *pdl = backtrace_next (backtrace_top ());
1578 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1579 pdl = backtrace_next (pdl);
1580 if (backtrace_p (pdl))
1581 Vsignaling_function = backtrace_function (pdl);
1584 for (h = handlerlist; h; h = h->next)
1586 if (h->type != CONDITION_CASE)
1587 continue;
1588 clause = find_handler_clause (h->tag_or_ch, conditions);
1589 if (!NILP (clause))
1590 break;
1593 if (/* Don't run the debugger for a memory-full error.
1594 (There is no room in memory to do that!) */
1595 !NILP (error_symbol)
1596 && (!NILP (Vdebug_on_signal)
1597 /* If no handler is present now, try to run the debugger. */
1598 || NILP (clause)
1599 /* A `debug' symbol in the handler list disables the normal
1600 suppression of the debugger. */
1601 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1602 /* Special handler that means "print a message and run debugger
1603 if requested". */
1604 || EQ (h->tag_or_ch, Qerror)))
1606 bool debugger_called
1607 = maybe_call_debugger (conditions, error_symbol, data);
1608 /* We can't return values to code which signaled an error, but we
1609 can continue code which has signaled a quit. */
1610 if (keyboard_quit && debugger_called && EQ (real_error_symbol, Qquit))
1611 return Qnil;
1614 if (!NILP (clause))
1616 Lisp_Object unwind_data
1617 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1619 unwind_to_catch (h, unwind_data);
1621 else
1623 if (handlerlist != handlerlist_sentinel)
1624 /* FIXME: This will come right back here if there's no `top-level'
1625 catcher. A better solution would be to abort here, and instead
1626 add a catch-all condition handler so we never come here. */
1627 Fthrow (Qtop_level, Qt);
1630 if (! NILP (error_symbol))
1631 data = Fcons (error_symbol, data);
1633 string = Ferror_message_string (data);
1634 fatal ("%s", SDATA (string));
1637 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1639 void
1640 xsignal0 (Lisp_Object error_symbol)
1642 xsignal (error_symbol, Qnil);
1645 void
1646 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1648 xsignal (error_symbol, list1 (arg));
1651 void
1652 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1654 xsignal (error_symbol, list2 (arg1, arg2));
1657 void
1658 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1660 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1663 /* Signal `error' with message S, and additional arg ARG.
1664 If ARG is not a genuine list, make it a one-element list. */
1666 void
1667 signal_error (const char *s, Lisp_Object arg)
1669 Lisp_Object tortoise, hare;
1671 hare = tortoise = arg;
1672 while (CONSP (hare))
1674 hare = XCDR (hare);
1675 if (!CONSP (hare))
1676 break;
1678 hare = XCDR (hare);
1679 tortoise = XCDR (tortoise);
1681 if (EQ (hare, tortoise))
1682 break;
1685 if (!NILP (hare))
1686 arg = list1 (arg);
1688 xsignal (Qerror, Fcons (build_string (s), arg));
1692 /* Return true if LIST is a non-nil atom or
1693 a list containing one of CONDITIONS. */
1695 static bool
1696 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1698 if (NILP (list))
1699 return 0;
1700 if (! CONSP (list))
1701 return 1;
1703 while (CONSP (conditions))
1705 Lisp_Object this, tail;
1706 this = XCAR (conditions);
1707 for (tail = list; CONSP (tail); tail = XCDR (tail))
1708 if (EQ (XCAR (tail), this))
1709 return 1;
1710 conditions = XCDR (conditions);
1712 return 0;
1715 /* Return true if an error with condition-symbols CONDITIONS,
1716 and described by SIGNAL-DATA, should skip the debugger
1717 according to debugger-ignored-errors. */
1719 static bool
1720 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1722 Lisp_Object tail;
1723 bool first_string = 1;
1724 Lisp_Object error_message;
1726 error_message = Qnil;
1727 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1729 if (STRINGP (XCAR (tail)))
1731 if (first_string)
1733 error_message = Ferror_message_string (data);
1734 first_string = 0;
1737 if (fast_string_match (XCAR (tail), error_message) >= 0)
1738 return 1;
1740 else
1742 Lisp_Object contail;
1744 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1745 if (EQ (XCAR (tail), XCAR (contail)))
1746 return 1;
1750 return 0;
1753 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1754 SIG and DATA describe the signal. There are two ways to pass them:
1755 = SIG is the error symbol, and DATA is the rest of the data.
1756 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1757 This is for memory-full errors only. */
1758 static bool
1759 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1761 Lisp_Object combined_data;
1763 combined_data = Fcons (sig, data);
1765 if (
1766 /* Don't try to run the debugger with interrupts blocked.
1767 The editing loop would return anyway. */
1768 ! input_blocked_p ()
1769 && NILP (Vinhibit_debugger)
1770 /* Does user want to enter debugger for this kind of error? */
1771 && (EQ (sig, Qquit)
1772 ? debug_on_quit
1773 : wants_debugger (Vdebug_on_error, conditions))
1774 && ! skip_debugger (conditions, combined_data)
1775 /* RMS: What's this for? */
1776 && when_entered_debugger < num_nonmacro_input_events)
1778 call_debugger (list2 (Qerror, combined_data));
1779 return 1;
1782 return 0;
1785 static Lisp_Object
1786 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1788 register Lisp_Object h;
1790 /* t is used by handlers for all conditions, set up by C code. */
1791 if (EQ (handlers, Qt))
1792 return Qt;
1794 /* error is used similarly, but means print an error message
1795 and run the debugger if that is enabled. */
1796 if (EQ (handlers, Qerror))
1797 return Qt;
1799 for (h = handlers; CONSP (h); h = XCDR (h))
1801 Lisp_Object handler = XCAR (h);
1802 if (!NILP (Fmemq (handler, conditions)))
1803 return handlers;
1806 return Qnil;
1810 /* Format and return a string; called like vprintf. */
1811 Lisp_Object
1812 vformat_string (const char *m, va_list ap)
1814 char buf[4000];
1815 ptrdiff_t size = sizeof buf;
1816 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1817 char *buffer = buf;
1818 ptrdiff_t used;
1819 Lisp_Object string;
1821 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1822 string = make_string (buffer, used);
1823 if (buffer != buf)
1824 xfree (buffer);
1826 return string;
1829 /* Dump an error message; called like vprintf. */
1830 void
1831 verror (const char *m, va_list ap)
1833 xsignal1 (Qerror, vformat_string (m, ap));
1837 /* Dump an error message; called like printf. */
1839 /* VARARGS 1 */
1840 void
1841 error (const char *m, ...)
1843 va_list ap;
1844 va_start (ap, m);
1845 verror (m, ap);
1848 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1849 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1850 This means it contains a description for how to read arguments to give it.
1851 The value is nil for an invalid function or a symbol with no function
1852 definition.
1854 Interactively callable functions include strings and vectors (treated
1855 as keyboard macros), lambda-expressions that contain a top-level call
1856 to `interactive', autoload definitions made by `autoload' with non-nil
1857 fourth argument, and some of the built-in functions of Lisp.
1859 Also, a symbol satisfies `commandp' if its function definition does so.
1861 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1862 then strings and vectors are not accepted. */)
1863 (Lisp_Object function, Lisp_Object for_call_interactively)
1865 register Lisp_Object fun;
1866 register Lisp_Object funcar;
1867 Lisp_Object if_prop = Qnil;
1869 fun = function;
1871 fun = indirect_function (fun); /* Check cycles. */
1872 if (NILP (fun))
1873 return Qnil;
1875 /* Check an `interactive-form' property if present, analogous to the
1876 function-documentation property. */
1877 fun = function;
1878 while (SYMBOLP (fun))
1880 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1881 if (!NILP (tmp))
1882 if_prop = Qt;
1883 fun = Fsymbol_function (fun);
1886 /* Emacs primitives are interactive if their DEFUN specifies an
1887 interactive spec. */
1888 if (SUBRP (fun))
1889 return XSUBR (fun)->intspec ? Qt : if_prop;
1891 /* Bytecode objects are interactive if they are long enough to
1892 have an element whose index is COMPILED_INTERACTIVE, which is
1893 where the interactive spec is stored. */
1894 else if (COMPILEDP (fun))
1895 return (PVSIZE (fun) > COMPILED_INTERACTIVE ? Qt : if_prop);
1897 /* Strings and vectors are keyboard macros. */
1898 if (STRINGP (fun) || VECTORP (fun))
1899 return (NILP (for_call_interactively) ? Qt : Qnil);
1901 /* Lists may represent commands. */
1902 if (!CONSP (fun))
1903 return Qnil;
1904 funcar = XCAR (fun);
1905 if (EQ (funcar, Qclosure))
1906 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1907 ? Qt : if_prop);
1908 else if (EQ (funcar, Qlambda))
1909 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1910 else if (EQ (funcar, Qautoload))
1911 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1912 else
1913 return Qnil;
1916 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1917 doc: /* Define FUNCTION to autoload from FILE.
1918 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1919 Third arg DOCSTRING is documentation for the function.
1920 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1921 Fifth arg TYPE indicates the type of the object:
1922 nil or omitted says FUNCTION is a function,
1923 `keymap' says FUNCTION is really a keymap, and
1924 `macro' or t says FUNCTION is really a macro.
1925 Third through fifth args give info about the real definition.
1926 They default to nil.
1927 If FUNCTION is already defined other than as an autoload,
1928 this does nothing and returns nil. */)
1929 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1931 CHECK_SYMBOL (function);
1932 CHECK_STRING (file);
1934 /* If function is defined and not as an autoload, don't override. */
1935 if (!NILP (XSYMBOL (function)->u.s.function)
1936 && !AUTOLOADP (XSYMBOL (function)->u.s.function))
1937 return Qnil;
1939 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1940 /* `read1' in lread.c has found the docstring starting with "\
1941 and assumed the docstring will be provided by Snarf-documentation, so it
1942 passed us 0 instead. But that leads to accidental sharing in purecopy's
1943 hash-consing, so we use a (hopefully) unique integer instead. */
1944 docstring = make_number (XHASH (function));
1945 return Fdefalias (function,
1946 list5 (Qautoload, file, docstring, interactive, type),
1947 Qnil);
1950 void
1951 un_autoload (Lisp_Object oldqueue)
1953 Lisp_Object queue, first, second;
1955 /* Queue to unwind is current value of Vautoload_queue.
1956 oldqueue is the shadowed value to leave in Vautoload_queue. */
1957 queue = Vautoload_queue;
1958 Vautoload_queue = oldqueue;
1959 while (CONSP (queue))
1961 first = XCAR (queue);
1962 second = Fcdr (first);
1963 first = Fcar (first);
1964 if (EQ (first, make_number (0)))
1965 Vfeatures = second;
1966 else
1967 Ffset (first, second);
1968 queue = XCDR (queue);
1972 /* Load an autoloaded function.
1973 FUNNAME is the symbol which is the function's name.
1974 FUNDEF is the autoload definition (a list). */
1976 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1977 doc: /* Load FUNDEF which should be an autoload.
1978 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1979 in which case the function returns the new autoloaded function value.
1980 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1981 it defines a macro. */)
1982 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1984 ptrdiff_t count = SPECPDL_INDEX ();
1986 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1987 return fundef;
1989 Lisp_Object kind = Fnth (make_number (4), fundef);
1990 if (EQ (macro_only, Qmacro)
1991 && !(EQ (kind, Qt) || EQ (kind, Qmacro)))
1992 return fundef;
1994 /* This is to make sure that loadup.el gives a clear picture
1995 of what files are preloaded and when. */
1996 if (! NILP (Vpurify_flag))
1997 error ("Attempt to autoload %s while preparing to dump",
1998 SDATA (SYMBOL_NAME (funname)));
2000 CHECK_SYMBOL (funname);
2002 /* Preserve the match data. */
2003 record_unwind_save_match_data ();
2005 /* If autoloading gets an error (which includes the error of failing
2006 to define the function being called), we use Vautoload_queue
2007 to undo function definitions and `provide' calls made by
2008 the function. We do this in the specific case of autoloading
2009 because autoloading is not an explicit request "load this file",
2010 but rather a request to "call this function".
2012 The value saved here is to be restored into Vautoload_queue. */
2013 record_unwind_protect (un_autoload, Vautoload_queue);
2014 Vautoload_queue = Qt;
2015 /* If `macro_only' is set and fundef isn't a macro, assume this autoload to
2016 be a "best-effort" (e.g. to try and find a compiler macro),
2017 so don't signal an error if autoloading fails. */
2018 Lisp_Object ignore_errors
2019 = (EQ (kind, Qt) || EQ (kind, Qmacro)) ? Qnil : macro_only;
2020 Fload (Fcar (Fcdr (fundef)), ignore_errors, Qt, Qnil, Qt);
2022 /* Once loading finishes, don't undo it. */
2023 Vautoload_queue = Qt;
2024 unbind_to (count, Qnil);
2026 if (NILP (funname) || !NILP (ignore_errors))
2027 return Qnil;
2028 else
2030 Lisp_Object fun = Findirect_function (funname, Qnil);
2032 if (!NILP (Fequal (fun, fundef)))
2033 error ("Autoloading file %s failed to define function %s",
2034 SDATA (Fcar (Fcar (Vload_history))),
2035 SDATA (SYMBOL_NAME (funname)));
2036 else
2037 return fun;
2042 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2043 doc: /* Evaluate FORM and return its value.
2044 If LEXICAL is t, evaluate using lexical scoping.
2045 LEXICAL can also be an actual lexical environment, in the form of an
2046 alist mapping symbols to their value. */)
2047 (Lisp_Object form, Lisp_Object lexical)
2049 ptrdiff_t count = SPECPDL_INDEX ();
2050 specbind (Qinternal_interpreter_environment,
2051 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2052 return unbind_to (count, eval_sub (form));
2055 /* Grow the specpdl stack by one entry.
2056 The caller should have already initialized the entry.
2057 Signal an error on stack overflow.
2059 Make sure that there is always one unused entry past the top of the
2060 stack, so that the just-initialized entry is safely unwound if
2061 memory exhausted and an error is signaled here. Also, allocate a
2062 never-used entry just before the bottom of the stack; sometimes its
2063 address is taken. */
2065 static void
2066 grow_specpdl (void)
2068 specpdl_ptr++;
2070 if (specpdl_ptr == specpdl + specpdl_size)
2072 ptrdiff_t count = SPECPDL_INDEX ();
2073 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2074 union specbinding *pdlvec = specpdl - 1;
2075 ptrdiff_t pdlvecsize = specpdl_size + 1;
2076 if (max_size <= specpdl_size)
2078 if (max_specpdl_size < 400)
2079 max_size = max_specpdl_size = 400;
2080 if (max_size <= specpdl_size)
2081 signal_error ("Variable binding depth exceeds max-specpdl-size",
2082 Qnil);
2084 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2085 specpdl = pdlvec + 1;
2086 specpdl_size = pdlvecsize - 1;
2087 specpdl_ptr = specpdl + count;
2091 ptrdiff_t
2092 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2094 ptrdiff_t count = SPECPDL_INDEX ();
2096 eassert (nargs >= UNEVALLED);
2097 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2098 specpdl_ptr->bt.debug_on_exit = false;
2099 specpdl_ptr->bt.function = function;
2100 current_thread->stack_top = specpdl_ptr->bt.args = args;
2101 specpdl_ptr->bt.nargs = nargs;
2102 grow_specpdl ();
2104 return count;
2107 /* Eval a sub-expression of the current expression (i.e. in the same
2108 lexical scope). */
2109 Lisp_Object
2110 eval_sub (Lisp_Object form)
2112 Lisp_Object fun, val, original_fun, original_args;
2113 Lisp_Object funcar;
2114 ptrdiff_t count;
2116 /* Declare here, as this array may be accessed by call_debugger near
2117 the end of this function. See Bug#21245. */
2118 Lisp_Object argvals[8];
2120 if (SYMBOLP (form))
2122 /* Look up its binding in the lexical environment.
2123 We do not pay attention to the declared_special flag here, since we
2124 already did that when let-binding the variable. */
2125 Lisp_Object lex_binding
2126 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2127 ? Fassq (form, Vinternal_interpreter_environment)
2128 : Qnil;
2129 if (CONSP (lex_binding))
2130 return XCDR (lex_binding);
2131 else
2132 return Fsymbol_value (form);
2135 if (!CONSP (form))
2136 return form;
2138 maybe_quit ();
2140 maybe_gc ();
2142 if (++lisp_eval_depth > max_lisp_eval_depth)
2144 if (max_lisp_eval_depth < 100)
2145 max_lisp_eval_depth = 100;
2146 if (lisp_eval_depth > max_lisp_eval_depth)
2147 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2150 original_fun = XCAR (form);
2151 original_args = XCDR (form);
2152 CHECK_LIST (original_args);
2154 /* This also protects them from gc. */
2155 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2157 if (debug_on_next_call)
2158 do_debug_on_call (Qt, count);
2160 /* At this point, only original_fun and original_args
2161 have values that will be used below. */
2162 retry:
2164 /* Optimize for no indirection. */
2165 fun = original_fun;
2166 if (!SYMBOLP (fun))
2167 fun = Ffunction (Fcons (fun, Qnil));
2168 else if (!NILP (fun) && (fun = XSYMBOL (fun)->u.s.function, SYMBOLP (fun)))
2169 fun = indirect_function (fun);
2171 if (SUBRP (fun))
2173 Lisp_Object args_left = original_args;
2174 Lisp_Object numargs = Flength (args_left);
2176 check_cons_list ();
2178 if (XINT (numargs) < XSUBR (fun)->min_args
2179 || (XSUBR (fun)->max_args >= 0
2180 && XSUBR (fun)->max_args < XINT (numargs)))
2181 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2183 else if (XSUBR (fun)->max_args == UNEVALLED)
2184 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2185 else if (XSUBR (fun)->max_args == MANY)
2187 /* Pass a vector of evaluated arguments. */
2188 Lisp_Object *vals;
2189 ptrdiff_t argnum = 0;
2190 USE_SAFE_ALLOCA;
2192 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2194 while (CONSP (args_left) && argnum < XINT (numargs))
2196 Lisp_Object arg = XCAR (args_left);
2197 args_left = XCDR (args_left);
2198 vals[argnum++] = eval_sub (arg);
2201 set_backtrace_args (specpdl + count, vals, argnum);
2203 val = XSUBR (fun)->function.aMANY (argnum, vals);
2205 check_cons_list ();
2206 lisp_eval_depth--;
2207 /* Do the debug-on-exit now, while VALS still exists. */
2208 if (backtrace_debug_on_exit (specpdl + count))
2209 val = call_debugger (list2 (Qexit, val));
2210 SAFE_FREE ();
2211 specpdl_ptr--;
2212 return val;
2214 else
2216 int i, maxargs = XSUBR (fun)->max_args;
2218 for (i = 0; i < maxargs; i++)
2220 argvals[i] = eval_sub (Fcar (args_left));
2221 args_left = Fcdr (args_left);
2224 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2226 switch (i)
2228 case 0:
2229 val = (XSUBR (fun)->function.a0 ());
2230 break;
2231 case 1:
2232 val = (XSUBR (fun)->function.a1 (argvals[0]));
2233 break;
2234 case 2:
2235 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2236 break;
2237 case 3:
2238 val = (XSUBR (fun)->function.a3
2239 (argvals[0], argvals[1], argvals[2]));
2240 break;
2241 case 4:
2242 val = (XSUBR (fun)->function.a4
2243 (argvals[0], argvals[1], argvals[2], argvals[3]));
2244 break;
2245 case 5:
2246 val = (XSUBR (fun)->function.a5
2247 (argvals[0], argvals[1], argvals[2], argvals[3],
2248 argvals[4]));
2249 break;
2250 case 6:
2251 val = (XSUBR (fun)->function.a6
2252 (argvals[0], argvals[1], argvals[2], argvals[3],
2253 argvals[4], argvals[5]));
2254 break;
2255 case 7:
2256 val = (XSUBR (fun)->function.a7
2257 (argvals[0], argvals[1], argvals[2], argvals[3],
2258 argvals[4], argvals[5], argvals[6]));
2259 break;
2261 case 8:
2262 val = (XSUBR (fun)->function.a8
2263 (argvals[0], argvals[1], argvals[2], argvals[3],
2264 argvals[4], argvals[5], argvals[6], argvals[7]));
2265 break;
2267 default:
2268 /* Someone has created a subr that takes more arguments than
2269 is supported by this code. We need to either rewrite the
2270 subr to use a different argument protocol, or add more
2271 cases to this switch. */
2272 emacs_abort ();
2276 else if (COMPILEDP (fun) || MODULE_FUNCTIONP (fun))
2277 return apply_lambda (fun, original_args, count);
2278 else
2280 if (NILP (fun))
2281 xsignal1 (Qvoid_function, original_fun);
2282 if (!CONSP (fun))
2283 xsignal1 (Qinvalid_function, original_fun);
2284 funcar = XCAR (fun);
2285 if (!SYMBOLP (funcar))
2286 xsignal1 (Qinvalid_function, original_fun);
2287 if (EQ (funcar, Qautoload))
2289 Fautoload_do_load (fun, original_fun, Qnil);
2290 goto retry;
2292 if (EQ (funcar, Qmacro))
2294 ptrdiff_t count1 = SPECPDL_INDEX ();
2295 Lisp_Object exp;
2296 /* Bind lexical-binding during expansion of the macro, so the
2297 macro can know reliably if the code it outputs will be
2298 interpreted using lexical-binding or not. */
2299 specbind (Qlexical_binding,
2300 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2301 exp = apply1 (Fcdr (fun), original_args);
2302 unbind_to (count1, Qnil);
2303 val = eval_sub (exp);
2305 else if (EQ (funcar, Qlambda)
2306 || EQ (funcar, Qclosure))
2307 return apply_lambda (fun, original_args, count);
2308 else
2309 xsignal1 (Qinvalid_function, original_fun);
2311 check_cons_list ();
2313 lisp_eval_depth--;
2314 if (backtrace_debug_on_exit (specpdl + count))
2315 val = call_debugger (list2 (Qexit, val));
2316 specpdl_ptr--;
2318 return val;
2321 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2322 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2323 Then return the value FUNCTION returns.
2324 Thus, (apply \\='+ 1 2 \\='(3 4)) returns 10.
2325 usage: (apply FUNCTION &rest ARGUMENTS) */)
2326 (ptrdiff_t nargs, Lisp_Object *args)
2328 ptrdiff_t i, numargs, funcall_nargs;
2329 register Lisp_Object *funcall_args = NULL;
2330 register Lisp_Object spread_arg = args[nargs - 1];
2331 Lisp_Object fun = args[0];
2332 Lisp_Object retval;
2333 USE_SAFE_ALLOCA;
2335 CHECK_LIST (spread_arg);
2337 numargs = XINT (Flength (spread_arg));
2339 if (numargs == 0)
2340 return Ffuncall (nargs - 1, args);
2341 else if (numargs == 1)
2343 args [nargs - 1] = XCAR (spread_arg);
2344 return Ffuncall (nargs, args);
2347 numargs += nargs - 2;
2349 /* Optimize for no indirection. */
2350 if (SYMBOLP (fun) && !NILP (fun)
2351 && (fun = XSYMBOL (fun)->u.s.function, SYMBOLP (fun)))
2353 fun = indirect_function (fun);
2354 if (NILP (fun))
2355 /* Let funcall get the error. */
2356 fun = args[0];
2359 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2360 /* Don't hide an error by adding missing arguments. */
2361 && numargs >= XSUBR (fun)->min_args)
2363 /* Avoid making funcall cons up a yet another new vector of arguments
2364 by explicitly supplying nil's for optional values. */
2365 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2366 memclear (funcall_args + numargs + 1,
2367 (XSUBR (fun)->max_args - numargs) * word_size);
2368 funcall_nargs = 1 + XSUBR (fun)->max_args;
2370 else
2371 { /* We add 1 to numargs because funcall_args includes the
2372 function itself as well as its arguments. */
2373 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2374 funcall_nargs = 1 + numargs;
2377 memcpy (funcall_args, args, nargs * word_size);
2378 /* Spread the last arg we got. Its first element goes in
2379 the slot that it used to occupy, hence this value of I. */
2380 i = nargs - 1;
2381 while (!NILP (spread_arg))
2383 funcall_args [i++] = XCAR (spread_arg);
2384 spread_arg = XCDR (spread_arg);
2387 retval = Ffuncall (funcall_nargs, funcall_args);
2389 SAFE_FREE ();
2390 return retval;
2393 /* Run hook variables in various ways. */
2395 static Lisp_Object
2396 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2398 Ffuncall (nargs, args);
2399 return Qnil;
2402 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2403 doc: /* Run each hook in HOOKS.
2404 Each argument should be a symbol, a hook variable.
2405 These symbols are processed in the order specified.
2406 If a hook symbol has a non-nil value, that value may be a function
2407 or a list of functions to be called to run the hook.
2408 If the value is a function, it is called with no arguments.
2409 If it is a list, the elements are called, in order, with no arguments.
2411 Major modes should not use this function directly to run their mode
2412 hook; they should use `run-mode-hooks' instead.
2414 Do not use `make-local-variable' to make a hook variable buffer-local.
2415 Instead, use `add-hook' and specify t for the LOCAL argument.
2416 usage: (run-hooks &rest HOOKS) */)
2417 (ptrdiff_t nargs, Lisp_Object *args)
2419 ptrdiff_t i;
2421 for (i = 0; i < nargs; i++)
2422 run_hook (args[i]);
2424 return Qnil;
2427 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2428 Srun_hook_with_args, 1, MANY, 0,
2429 doc: /* Run HOOK with the specified arguments ARGS.
2430 HOOK should be a symbol, a hook variable. The value of HOOK
2431 may be nil, a function, or a list of functions. Call each
2432 function in order with arguments ARGS. The final return value
2433 is unspecified.
2435 Do not use `make-local-variable' to make a hook variable buffer-local.
2436 Instead, use `add-hook' and specify t for the LOCAL argument.
2437 usage: (run-hook-with-args HOOK &rest ARGS) */)
2438 (ptrdiff_t nargs, Lisp_Object *args)
2440 return run_hook_with_args (nargs, args, funcall_nil);
2443 /* NB this one still documents a specific non-nil return value.
2444 (As did run-hook-with-args and run-hook-with-args-until-failure
2445 until they were changed in 24.1.) */
2446 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2447 Srun_hook_with_args_until_success, 1, MANY, 0,
2448 doc: /* Run HOOK with the specified arguments ARGS.
2449 HOOK should be a symbol, a hook variable. The value of HOOK
2450 may be nil, a function, or a list of functions. Call each
2451 function in order with arguments ARGS, stopping at the first
2452 one that returns non-nil, and return that value. Otherwise (if
2453 all functions return nil, or if there are no functions to call),
2454 return nil.
2456 Do not use `make-local-variable' to make a hook variable buffer-local.
2457 Instead, use `add-hook' and specify t for the LOCAL argument.
2458 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2459 (ptrdiff_t nargs, Lisp_Object *args)
2461 return run_hook_with_args (nargs, args, Ffuncall);
2464 static Lisp_Object
2465 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2467 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2470 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2471 Srun_hook_with_args_until_failure, 1, MANY, 0,
2472 doc: /* Run HOOK with the specified arguments ARGS.
2473 HOOK should be a symbol, a hook variable. The value of HOOK
2474 may be nil, a function, or a list of functions. Call each
2475 function in order with arguments ARGS, stopping at the first
2476 one that returns nil, and return nil. Otherwise (if all functions
2477 return non-nil, or if there are no functions to call), return non-nil
2478 \(do not rely on the precise return value in this case).
2480 Do not use `make-local-variable' to make a hook variable buffer-local.
2481 Instead, use `add-hook' and specify t for the LOCAL argument.
2482 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2483 (ptrdiff_t nargs, Lisp_Object *args)
2485 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2488 static Lisp_Object
2489 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2491 Lisp_Object tmp = args[0], ret;
2492 args[0] = args[1];
2493 args[1] = tmp;
2494 ret = Ffuncall (nargs, args);
2495 args[1] = args[0];
2496 args[0] = tmp;
2497 return ret;
2500 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2501 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2502 I.e. instead of calling each function FUN directly with arguments ARGS,
2503 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2504 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2505 aborts and returns that value.
2506 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2507 (ptrdiff_t nargs, Lisp_Object *args)
2509 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2512 /* ARGS[0] should be a hook symbol.
2513 Call each of the functions in the hook value, passing each of them
2514 as arguments all the rest of ARGS (all NARGS - 1 elements).
2515 FUNCALL specifies how to call each function on the hook. */
2517 Lisp_Object
2518 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2519 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2521 Lisp_Object sym, val, ret = Qnil;
2523 /* If we are dying or still initializing,
2524 don't do anything--it would probably crash if we tried. */
2525 if (NILP (Vrun_hooks))
2526 return Qnil;
2528 sym = args[0];
2529 val = find_symbol_value (sym);
2531 if (EQ (val, Qunbound) || NILP (val))
2532 return ret;
2533 else if (!CONSP (val) || FUNCTIONP (val))
2535 args[0] = val;
2536 return funcall (nargs, args);
2538 else
2540 Lisp_Object global_vals = Qnil;
2542 for (;
2543 CONSP (val) && NILP (ret);
2544 val = XCDR (val))
2546 if (EQ (XCAR (val), Qt))
2548 /* t indicates this hook has a local binding;
2549 it means to run the global binding too. */
2550 global_vals = Fdefault_value (sym);
2551 if (NILP (global_vals)) continue;
2553 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2555 args[0] = global_vals;
2556 ret = funcall (nargs, args);
2558 else
2560 for (;
2561 CONSP (global_vals) && NILP (ret);
2562 global_vals = XCDR (global_vals))
2564 args[0] = XCAR (global_vals);
2565 /* In a global value, t should not occur. If it does, we
2566 must ignore it to avoid an endless loop. */
2567 if (!EQ (args[0], Qt))
2568 ret = funcall (nargs, args);
2572 else
2574 args[0] = XCAR (val);
2575 ret = funcall (nargs, args);
2579 return ret;
2583 /* Run the hook HOOK, giving each function no args. */
2585 void
2586 run_hook (Lisp_Object hook)
2588 Frun_hook_with_args (1, &hook);
2591 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2593 void
2594 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2596 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2599 /* Apply fn to arg. */
2600 Lisp_Object
2601 apply1 (Lisp_Object fn, Lisp_Object arg)
2603 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2606 /* Call function fn on no arguments. */
2607 Lisp_Object
2608 call0 (Lisp_Object fn)
2610 return Ffuncall (1, &fn);
2613 /* Call function fn with 1 argument arg1. */
2614 /* ARGSUSED */
2615 Lisp_Object
2616 call1 (Lisp_Object fn, Lisp_Object arg1)
2618 return CALLN (Ffuncall, fn, arg1);
2621 /* Call function fn with 2 arguments arg1, arg2. */
2622 /* ARGSUSED */
2623 Lisp_Object
2624 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2626 return CALLN (Ffuncall, fn, arg1, arg2);
2629 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2630 /* ARGSUSED */
2631 Lisp_Object
2632 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2634 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2637 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2638 /* ARGSUSED */
2639 Lisp_Object
2640 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2641 Lisp_Object arg4)
2643 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2646 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2647 /* ARGSUSED */
2648 Lisp_Object
2649 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2650 Lisp_Object arg4, Lisp_Object arg5)
2652 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2655 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2656 /* ARGSUSED */
2657 Lisp_Object
2658 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2659 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2661 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2664 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2665 /* ARGSUSED */
2666 Lisp_Object
2667 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2668 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2670 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2673 /* Call function fn with 8 arguments arg1, arg2, arg3, arg4, arg5,
2674 arg6, arg7, arg8. */
2675 /* ARGSUSED */
2676 Lisp_Object
2677 call8 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2678 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7,
2679 Lisp_Object arg8)
2681 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
2684 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2685 doc: /* Return t if OBJECT is a function. */)
2686 (Lisp_Object object)
2688 if (FUNCTIONP (object))
2689 return Qt;
2690 return Qnil;
2693 bool
2694 FUNCTIONP (Lisp_Object object)
2696 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2698 object = Findirect_function (object, Qt);
2700 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2702 /* Autoloaded symbols are functions, except if they load
2703 macros or keymaps. */
2704 for (int i = 0; i < 4 && CONSP (object); i++)
2705 object = XCDR (object);
2707 return ! (CONSP (object) && !NILP (XCAR (object)));
2711 if (SUBRP (object))
2712 return XSUBR (object)->max_args != UNEVALLED;
2713 else if (COMPILEDP (object) || MODULE_FUNCTIONP (object))
2714 return true;
2715 else if (CONSP (object))
2717 Lisp_Object car = XCAR (object);
2718 return EQ (car, Qlambda) || EQ (car, Qclosure);
2720 else
2721 return false;
2724 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2725 doc: /* Call first argument as a function, passing remaining arguments to it.
2726 Return the value that function returns.
2727 Thus, (funcall \\='cons \\='x \\='y) returns (x . y).
2728 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2729 (ptrdiff_t nargs, Lisp_Object *args)
2731 Lisp_Object fun, original_fun;
2732 Lisp_Object funcar;
2733 ptrdiff_t numargs = nargs - 1;
2734 Lisp_Object val;
2735 ptrdiff_t count;
2737 maybe_quit ();
2739 if (++lisp_eval_depth > max_lisp_eval_depth)
2741 if (max_lisp_eval_depth < 100)
2742 max_lisp_eval_depth = 100;
2743 if (lisp_eval_depth > max_lisp_eval_depth)
2744 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2747 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2749 maybe_gc ();
2751 if (debug_on_next_call)
2752 do_debug_on_call (Qlambda, count);
2754 check_cons_list ();
2756 original_fun = args[0];
2758 retry:
2760 /* Optimize for no indirection. */
2761 fun = original_fun;
2762 if (SYMBOLP (fun) && !NILP (fun)
2763 && (fun = XSYMBOL (fun)->u.s.function, SYMBOLP (fun)))
2764 fun = indirect_function (fun);
2766 if (SUBRP (fun))
2767 val = funcall_subr (XSUBR (fun), numargs, args + 1);
2768 else if (COMPILEDP (fun) || MODULE_FUNCTIONP (fun))
2769 val = funcall_lambda (fun, numargs, args + 1);
2770 else
2772 if (NILP (fun))
2773 xsignal1 (Qvoid_function, original_fun);
2774 if (!CONSP (fun))
2775 xsignal1 (Qinvalid_function, original_fun);
2776 funcar = XCAR (fun);
2777 if (!SYMBOLP (funcar))
2778 xsignal1 (Qinvalid_function, original_fun);
2779 if (EQ (funcar, Qlambda)
2780 || EQ (funcar, Qclosure))
2781 val = funcall_lambda (fun, numargs, args + 1);
2782 else if (EQ (funcar, Qautoload))
2784 Fautoload_do_load (fun, original_fun, Qnil);
2785 check_cons_list ();
2786 goto retry;
2788 else
2789 xsignal1 (Qinvalid_function, original_fun);
2791 check_cons_list ();
2792 lisp_eval_depth--;
2793 if (backtrace_debug_on_exit (specpdl + count))
2794 val = call_debugger (list2 (Qexit, val));
2795 specpdl_ptr--;
2796 return val;
2800 /* Apply a C subroutine SUBR to the NUMARGS evaluated arguments in ARG_VECTOR
2801 and return the result of evaluation. */
2803 Lisp_Object
2804 funcall_subr (struct Lisp_Subr *subr, ptrdiff_t numargs, Lisp_Object *args)
2806 if (numargs < subr->min_args
2807 || (subr->max_args >= 0 && subr->max_args < numargs))
2809 Lisp_Object fun;
2810 XSETSUBR (fun, subr);
2811 xsignal2 (Qwrong_number_of_arguments, fun, make_number (numargs));
2814 else if (subr->max_args == UNEVALLED)
2816 Lisp_Object fun;
2817 XSETSUBR (fun, subr);
2818 xsignal1 (Qinvalid_function, fun);
2821 else if (subr->max_args == MANY)
2822 return (subr->function.aMANY) (numargs, args);
2823 else
2825 Lisp_Object internal_argbuf[8];
2826 Lisp_Object *internal_args;
2827 if (subr->max_args > numargs)
2829 eassert (subr->max_args <= ARRAYELTS (internal_argbuf));
2830 internal_args = internal_argbuf;
2831 memcpy (internal_args, args, numargs * word_size);
2832 memclear (internal_args + numargs,
2833 (subr->max_args - numargs) * word_size);
2835 else
2836 internal_args = args;
2837 switch (subr->max_args)
2839 case 0:
2840 return (subr->function.a0 ());
2841 case 1:
2842 return (subr->function.a1 (internal_args[0]));
2843 case 2:
2844 return (subr->function.a2
2845 (internal_args[0], internal_args[1]));
2846 case 3:
2847 return (subr->function.a3
2848 (internal_args[0], internal_args[1], internal_args[2]));
2849 case 4:
2850 return (subr->function.a4
2851 (internal_args[0], internal_args[1], internal_args[2],
2852 internal_args[3]));
2853 case 5:
2854 return (subr->function.a5
2855 (internal_args[0], internal_args[1], internal_args[2],
2856 internal_args[3], internal_args[4]));
2857 case 6:
2858 return (subr->function.a6
2859 (internal_args[0], internal_args[1], internal_args[2],
2860 internal_args[3], internal_args[4], internal_args[5]));
2861 case 7:
2862 return (subr->function.a7
2863 (internal_args[0], internal_args[1], internal_args[2],
2864 internal_args[3], internal_args[4], internal_args[5],
2865 internal_args[6]));
2866 case 8:
2867 return (subr->function.a8
2868 (internal_args[0], internal_args[1], internal_args[2],
2869 internal_args[3], internal_args[4], internal_args[5],
2870 internal_args[6], internal_args[7]));
2872 default:
2874 /* If a subr takes more than 8 arguments without using MANY
2875 or UNEVALLED, we need to extend this function to support it.
2876 Until this is done, there is no way to call the function. */
2877 emacs_abort ();
2882 static Lisp_Object
2883 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2885 Lisp_Object args_left;
2886 ptrdiff_t i;
2887 EMACS_INT numargs;
2888 Lisp_Object *arg_vector;
2889 Lisp_Object tem;
2890 USE_SAFE_ALLOCA;
2892 numargs = XFASTINT (Flength (args));
2893 SAFE_ALLOCA_LISP (arg_vector, numargs);
2894 args_left = args;
2896 for (i = 0; i < numargs; )
2898 tem = Fcar (args_left), args_left = Fcdr (args_left);
2899 tem = eval_sub (tem);
2900 arg_vector[i++] = tem;
2903 set_backtrace_args (specpdl + count, arg_vector, i);
2904 tem = funcall_lambda (fun, numargs, arg_vector);
2906 check_cons_list ();
2907 lisp_eval_depth--;
2908 /* Do the debug-on-exit now, while arg_vector still exists. */
2909 if (backtrace_debug_on_exit (specpdl + count))
2910 tem = call_debugger (list2 (Qexit, tem));
2911 SAFE_FREE ();
2912 specpdl_ptr--;
2913 return tem;
2916 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2917 and return the result of evaluation.
2918 FUN must be either a lambda-expression, a compiled-code object,
2919 or a module function. */
2921 static Lisp_Object
2922 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2923 register Lisp_Object *arg_vector)
2925 Lisp_Object val, syms_left, next, lexenv;
2926 ptrdiff_t count = SPECPDL_INDEX ();
2927 ptrdiff_t i;
2928 bool optional, rest;
2930 if (CONSP (fun))
2932 if (EQ (XCAR (fun), Qclosure))
2934 Lisp_Object cdr = XCDR (fun); /* Drop `closure'. */
2935 if (! CONSP (cdr))
2936 xsignal1 (Qinvalid_function, fun);
2937 fun = cdr;
2938 lexenv = XCAR (fun);
2940 else
2941 lexenv = Qnil;
2942 syms_left = XCDR (fun);
2943 if (CONSP (syms_left))
2944 syms_left = XCAR (syms_left);
2945 else
2946 xsignal1 (Qinvalid_function, fun);
2948 else if (COMPILEDP (fun))
2950 ptrdiff_t size = PVSIZE (fun);
2951 if (size <= COMPILED_STACK_DEPTH)
2952 xsignal1 (Qinvalid_function, fun);
2953 syms_left = AREF (fun, COMPILED_ARGLIST);
2954 if (INTEGERP (syms_left))
2955 /* A byte-code object with an integer args template means we
2956 shouldn't bind any arguments, instead just call the byte-code
2957 interpreter directly; it will push arguments as necessary.
2959 Byte-code objects with a nil args template (the default)
2960 have dynamically-bound arguments, and use the
2961 argument-binding code below instead (as do all interpreted
2962 functions, even lexically bound ones). */
2964 /* If we have not actually read the bytecode string
2965 and constants vector yet, fetch them from the file. */
2966 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2967 Ffetch_bytecode (fun);
2968 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2969 AREF (fun, COMPILED_CONSTANTS),
2970 AREF (fun, COMPILED_STACK_DEPTH),
2971 syms_left,
2972 nargs, arg_vector);
2974 lexenv = Qnil;
2976 #ifdef HAVE_MODULES
2977 else if (MODULE_FUNCTIONP (fun))
2978 return funcall_module (fun, nargs, arg_vector);
2979 #endif
2980 else
2981 emacs_abort ();
2983 i = optional = rest = 0;
2984 bool previous_optional_or_rest = false;
2985 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2987 maybe_quit ();
2989 next = XCAR (syms_left);
2990 if (!SYMBOLP (next))
2991 xsignal1 (Qinvalid_function, fun);
2993 if (EQ (next, Qand_rest))
2995 if (rest || previous_optional_or_rest)
2996 xsignal1 (Qinvalid_function, fun);
2997 rest = 1;
2998 previous_optional_or_rest = true;
3000 else if (EQ (next, Qand_optional))
3002 if (optional || rest || previous_optional_or_rest)
3003 xsignal1 (Qinvalid_function, fun);
3004 optional = 1;
3005 previous_optional_or_rest = true;
3007 else
3009 Lisp_Object arg;
3010 if (rest)
3012 arg = Flist (nargs - i, &arg_vector[i]);
3013 i = nargs;
3015 else if (i < nargs)
3016 arg = arg_vector[i++];
3017 else if (!optional)
3018 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3019 else
3020 arg = Qnil;
3022 /* Bind the argument. */
3023 if (!NILP (lexenv) && SYMBOLP (next))
3024 /* Lexically bind NEXT by adding it to the lexenv alist. */
3025 lexenv = Fcons (Fcons (next, arg), lexenv);
3026 else
3027 /* Dynamically bind NEXT. */
3028 specbind (next, arg);
3029 previous_optional_or_rest = false;
3033 if (!NILP (syms_left) || previous_optional_or_rest)
3034 xsignal1 (Qinvalid_function, fun);
3035 else if (i < nargs)
3036 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3038 if (!EQ (lexenv, Vinternal_interpreter_environment))
3039 /* Instantiate a new lexical environment. */
3040 specbind (Qinternal_interpreter_environment, lexenv);
3042 if (CONSP (fun))
3043 val = Fprogn (XCDR (XCDR (fun)));
3044 else
3046 /* If we have not actually read the bytecode string
3047 and constants vector yet, fetch them from the file. */
3048 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3049 Ffetch_bytecode (fun);
3050 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3051 AREF (fun, COMPILED_CONSTANTS),
3052 AREF (fun, COMPILED_STACK_DEPTH),
3053 Qnil, 0, 0);
3056 return unbind_to (count, val);
3059 DEFUN ("func-arity", Ffunc_arity, Sfunc_arity, 1, 1, 0,
3060 doc: /* Return minimum and maximum number of args allowed for FUNCTION.
3061 FUNCTION must be a function of some kind.
3062 The returned value is a cons cell (MIN . MAX). MIN is the minimum number
3063 of args. MAX is the maximum number, or the symbol `many', for a
3064 function with `&rest' args, or `unevalled' for a special form. */)
3065 (Lisp_Object function)
3067 Lisp_Object original;
3068 Lisp_Object funcar;
3069 Lisp_Object result;
3071 original = function;
3073 retry:
3075 /* Optimize for no indirection. */
3076 function = original;
3077 if (SYMBOLP (function) && !NILP (function))
3079 function = XSYMBOL (function)->u.s.function;
3080 if (SYMBOLP (function))
3081 function = indirect_function (function);
3084 if (CONSP (function) && EQ (XCAR (function), Qmacro))
3085 function = XCDR (function);
3087 if (SUBRP (function))
3088 result = Fsubr_arity (function);
3089 else if (COMPILEDP (function))
3090 result = lambda_arity (function);
3091 #ifdef HAVE_MODULES
3092 else if (MODULE_FUNCTIONP (function))
3093 result = module_function_arity (XMODULE_FUNCTION (function));
3094 #endif
3095 else
3097 if (NILP (function))
3098 xsignal1 (Qvoid_function, original);
3099 if (!CONSP (function))
3100 xsignal1 (Qinvalid_function, original);
3101 funcar = XCAR (function);
3102 if (!SYMBOLP (funcar))
3103 xsignal1 (Qinvalid_function, original);
3104 if (EQ (funcar, Qlambda)
3105 || EQ (funcar, Qclosure))
3106 result = lambda_arity (function);
3107 else if (EQ (funcar, Qautoload))
3109 Fautoload_do_load (function, original, Qnil);
3110 goto retry;
3112 else
3113 xsignal1 (Qinvalid_function, original);
3115 return result;
3118 /* FUN must be either a lambda-expression or a compiled-code object. */
3119 static Lisp_Object
3120 lambda_arity (Lisp_Object fun)
3122 Lisp_Object syms_left;
3124 if (CONSP (fun))
3126 if (EQ (XCAR (fun), Qclosure))
3128 fun = XCDR (fun); /* Drop `closure'. */
3129 CHECK_CONS (fun);
3131 syms_left = XCDR (fun);
3132 if (CONSP (syms_left))
3133 syms_left = XCAR (syms_left);
3134 else
3135 xsignal1 (Qinvalid_function, fun);
3137 else if (COMPILEDP (fun))
3139 ptrdiff_t size = PVSIZE (fun);
3140 if (size <= COMPILED_STACK_DEPTH)
3141 xsignal1 (Qinvalid_function, fun);
3142 syms_left = AREF (fun, COMPILED_ARGLIST);
3143 if (INTEGERP (syms_left))
3144 return get_byte_code_arity (syms_left);
3146 else
3147 emacs_abort ();
3149 EMACS_INT minargs = 0, maxargs = 0;
3150 bool optional = false;
3151 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3153 Lisp_Object next = XCAR (syms_left);
3154 if (!SYMBOLP (next))
3155 xsignal1 (Qinvalid_function, fun);
3157 if (EQ (next, Qand_rest))
3158 return Fcons (make_number (minargs), Qmany);
3159 else if (EQ (next, Qand_optional))
3160 optional = true;
3161 else
3163 if (!optional)
3164 minargs++;
3165 maxargs++;
3169 if (!NILP (syms_left))
3170 xsignal1 (Qinvalid_function, fun);
3172 return Fcons (make_number (minargs), make_number (maxargs));
3175 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3176 1, 1, 0,
3177 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3178 (Lisp_Object object)
3180 Lisp_Object tem;
3182 if (COMPILEDP (object))
3184 ptrdiff_t size = PVSIZE (object);
3185 if (size <= COMPILED_STACK_DEPTH)
3186 xsignal1 (Qinvalid_function, object);
3187 if (CONSP (AREF (object, COMPILED_BYTECODE)))
3189 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3190 if (!CONSP (tem))
3192 tem = AREF (object, COMPILED_BYTECODE);
3193 if (CONSP (tem) && STRINGP (XCAR (tem)))
3194 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3195 else
3196 error ("Invalid byte code");
3198 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3199 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3202 return object;
3205 /* Return true if SYMBOL currently has a let-binding
3206 which was made in the buffer that is now current. */
3208 bool
3209 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3211 union specbinding *p;
3212 Lisp_Object buf = Fcurrent_buffer ();
3214 for (p = specpdl_ptr; p > specpdl; )
3215 if ((--p)->kind > SPECPDL_LET)
3217 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3218 eassert (let_bound_symbol->u.s.redirect != SYMBOL_VARALIAS);
3219 if (symbol == let_bound_symbol
3220 && EQ (specpdl_where (p), buf))
3221 return 1;
3224 return 0;
3227 static void
3228 do_specbind (struct Lisp_Symbol *sym, union specbinding *bind,
3229 Lisp_Object value, enum Set_Internal_Bind bindflag)
3231 switch (sym->u.s.redirect)
3233 case SYMBOL_PLAINVAL:
3234 if (!sym->u.s.trapped_write)
3235 SET_SYMBOL_VAL (sym, value);
3236 else
3237 set_internal (specpdl_symbol (bind), value, Qnil, bindflag);
3238 break;
3240 case SYMBOL_FORWARDED:
3241 if (BUFFER_OBJFWDP (SYMBOL_FWD (sym))
3242 && specpdl_kind (bind) == SPECPDL_LET_DEFAULT)
3244 set_default_internal (specpdl_symbol (bind), value, bindflag);
3245 return;
3247 FALLTHROUGH;
3248 case SYMBOL_LOCALIZED:
3249 set_internal (specpdl_symbol (bind), value, Qnil, bindflag);
3250 break;
3252 default:
3253 emacs_abort ();
3257 /* `specpdl_ptr' describes which variable is
3258 let-bound, so it can be properly undone when we unbind_to.
3259 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3260 - SYMBOL is the variable being bound. Note that it should not be
3261 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3262 to record V2 here).
3263 - WHERE tells us in which buffer the binding took place.
3264 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3265 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3266 i.e. bindings to the default value of a variable which can be
3267 buffer-local. */
3269 void
3270 specbind (Lisp_Object symbol, Lisp_Object value)
3272 struct Lisp_Symbol *sym;
3274 CHECK_SYMBOL (symbol);
3275 sym = XSYMBOL (symbol);
3277 start:
3278 switch (sym->u.s.redirect)
3280 case SYMBOL_VARALIAS:
3281 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3282 case SYMBOL_PLAINVAL:
3283 /* The most common case is that of a non-constant symbol with a
3284 trivial value. Make that as fast as we can. */
3285 specpdl_ptr->let.kind = SPECPDL_LET;
3286 specpdl_ptr->let.symbol = symbol;
3287 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3288 specpdl_ptr->let.saved_value = Qnil;
3289 grow_specpdl ();
3290 do_specbind (sym, specpdl_ptr - 1, value, SET_INTERNAL_BIND);
3291 break;
3292 case SYMBOL_LOCALIZED:
3293 case SYMBOL_FORWARDED:
3295 Lisp_Object ovalue = find_symbol_value (symbol);
3296 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3297 specpdl_ptr->let.symbol = symbol;
3298 specpdl_ptr->let.old_value = ovalue;
3299 specpdl_ptr->let.where = Fcurrent_buffer ();
3300 specpdl_ptr->let.saved_value = Qnil;
3302 eassert (sym->u.s.redirect != SYMBOL_LOCALIZED
3303 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3305 if (sym->u.s.redirect == SYMBOL_LOCALIZED)
3307 if (!blv_found (SYMBOL_BLV (sym)))
3308 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3310 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3312 /* If SYMBOL is a per-buffer variable which doesn't have a
3313 buffer-local value here, make the `let' change the global
3314 value by changing the value of SYMBOL in all buffers not
3315 having their own value. This is consistent with what
3316 happens with other buffer-local variables. */
3317 if (NILP (Flocal_variable_p (symbol, Qnil)))
3319 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3320 grow_specpdl ();
3321 do_specbind (sym, specpdl_ptr - 1, value, SET_INTERNAL_BIND);
3322 return;
3325 else
3326 specpdl_ptr->let.kind = SPECPDL_LET;
3328 grow_specpdl ();
3329 do_specbind (sym, specpdl_ptr - 1, value, SET_INTERNAL_BIND);
3330 break;
3332 default: emacs_abort ();
3336 /* Push unwind-protect entries of various types. */
3338 void
3339 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3341 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3342 specpdl_ptr->unwind.func = function;
3343 specpdl_ptr->unwind.arg = arg;
3344 grow_specpdl ();
3347 void
3348 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3350 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3351 specpdl_ptr->unwind_ptr.func = function;
3352 specpdl_ptr->unwind_ptr.arg = arg;
3353 grow_specpdl ();
3356 void
3357 record_unwind_protect_int (void (*function) (int), int arg)
3359 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3360 specpdl_ptr->unwind_int.func = function;
3361 specpdl_ptr->unwind_int.arg = arg;
3362 grow_specpdl ();
3365 void
3366 record_unwind_protect_void (void (*function) (void))
3368 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3369 specpdl_ptr->unwind_void.func = function;
3370 grow_specpdl ();
3373 void
3374 rebind_for_thread_switch (void)
3376 union specbinding *bind;
3378 for (bind = specpdl; bind != specpdl_ptr; ++bind)
3380 if (bind->kind >= SPECPDL_LET)
3382 Lisp_Object value = specpdl_saved_value (bind);
3383 Lisp_Object sym = specpdl_symbol (bind);
3384 bind->let.saved_value = Qnil;
3385 do_specbind (XSYMBOL (sym), bind, value,
3386 SET_INTERNAL_THREAD_SWITCH);
3391 static void
3392 do_one_unbind (union specbinding *this_binding, bool unwinding,
3393 enum Set_Internal_Bind bindflag)
3395 eassert (unwinding || this_binding->kind >= SPECPDL_LET);
3396 switch (this_binding->kind)
3398 case SPECPDL_UNWIND:
3399 this_binding->unwind.func (this_binding->unwind.arg);
3400 break;
3401 case SPECPDL_UNWIND_PTR:
3402 this_binding->unwind_ptr.func (this_binding->unwind_ptr.arg);
3403 break;
3404 case SPECPDL_UNWIND_INT:
3405 this_binding->unwind_int.func (this_binding->unwind_int.arg);
3406 break;
3407 case SPECPDL_UNWIND_VOID:
3408 this_binding->unwind_void.func ();
3409 break;
3410 case SPECPDL_BACKTRACE:
3411 break;
3412 case SPECPDL_LET:
3413 { /* If variable has a trivial value (no forwarding), and isn't
3414 trapped, we can just set it. */
3415 Lisp_Object sym = specpdl_symbol (this_binding);
3416 if (SYMBOLP (sym) && XSYMBOL (sym)->u.s.redirect == SYMBOL_PLAINVAL)
3418 if (XSYMBOL (sym)->u.s.trapped_write == SYMBOL_UNTRAPPED_WRITE)
3419 SET_SYMBOL_VAL (XSYMBOL (sym), specpdl_old_value (this_binding));
3420 else
3421 set_internal (sym, specpdl_old_value (this_binding),
3422 Qnil, bindflag);
3423 break;
3426 /* Come here only if make_local_foo was used for the first time
3427 on this var within this let. */
3428 FALLTHROUGH;
3429 case SPECPDL_LET_DEFAULT:
3430 set_default_internal (specpdl_symbol (this_binding),
3431 specpdl_old_value (this_binding),
3432 bindflag);
3433 break;
3434 case SPECPDL_LET_LOCAL:
3436 Lisp_Object symbol = specpdl_symbol (this_binding);
3437 Lisp_Object where = specpdl_where (this_binding);
3438 Lisp_Object old_value = specpdl_old_value (this_binding);
3439 eassert (BUFFERP (where));
3441 /* If this was a local binding, reset the value in the appropriate
3442 buffer, but only if that buffer's binding still exists. */
3443 if (!NILP (Flocal_variable_p (symbol, where)))
3444 set_internal (symbol, old_value, where, bindflag);
3446 break;
3450 static void
3451 do_nothing (void)
3454 /* Push an unwind-protect entry that does nothing, so that
3455 set_unwind_protect_ptr can overwrite it later. */
3457 void
3458 record_unwind_protect_nothing (void)
3460 record_unwind_protect_void (do_nothing);
3463 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3464 It need not be at the top of the stack. */
3466 void
3467 clear_unwind_protect (ptrdiff_t count)
3469 union specbinding *p = specpdl + count;
3470 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3471 p->unwind_void.func = do_nothing;
3474 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3475 It need not be at the top of the stack. Discard the entry's
3476 previous value without invoking it. */
3478 void
3479 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3480 Lisp_Object arg)
3482 union specbinding *p = specpdl + count;
3483 p->unwind.kind = SPECPDL_UNWIND;
3484 p->unwind.func = func;
3485 p->unwind.arg = arg;
3488 void
3489 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3491 union specbinding *p = specpdl + count;
3492 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3493 p->unwind_ptr.func = func;
3494 p->unwind_ptr.arg = arg;
3497 /* Pop and execute entries from the unwind-protect stack until the
3498 depth COUNT is reached. Return VALUE. */
3500 Lisp_Object
3501 unbind_to (ptrdiff_t count, Lisp_Object value)
3503 Lisp_Object quitf = Vquit_flag;
3505 Vquit_flag = Qnil;
3507 while (specpdl_ptr != specpdl + count)
3509 /* Copy the binding, and decrement specpdl_ptr, before we do
3510 the work to unbind it. We decrement first
3511 so that an error in unbinding won't try to unbind
3512 the same entry again, and we copy the binding first
3513 in case more bindings are made during some of the code we run. */
3515 union specbinding this_binding;
3516 this_binding = *--specpdl_ptr;
3518 do_one_unbind (&this_binding, true, SET_INTERNAL_UNBIND);
3521 if (NILP (Vquit_flag) && !NILP (quitf))
3522 Vquit_flag = quitf;
3524 return value;
3527 void
3528 unbind_for_thread_switch (struct thread_state *thr)
3530 union specbinding *bind;
3532 for (bind = thr->m_specpdl_ptr; bind > thr->m_specpdl;)
3534 if ((--bind)->kind >= SPECPDL_LET)
3536 Lisp_Object sym = specpdl_symbol (bind);
3537 bind->let.saved_value = find_symbol_value (sym);
3538 do_one_unbind (bind, false, SET_INTERNAL_THREAD_SWITCH);
3543 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3544 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3545 A special variable is one that will be bound dynamically, even in a
3546 context where binding is lexical by default. */)
3547 (Lisp_Object symbol)
3549 CHECK_SYMBOL (symbol);
3550 return XSYMBOL (symbol)->u.s.declared_special ? Qt : Qnil;
3554 static union specbinding *
3555 get_backtrace_starting_at (Lisp_Object base)
3557 union specbinding *pdl = backtrace_top ();
3559 if (!NILP (base))
3560 { /* Skip up to `base'. */
3561 base = Findirect_function (base, Qt);
3562 while (backtrace_p (pdl)
3563 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3564 pdl = backtrace_next (pdl);
3567 return pdl;
3570 static union specbinding *
3571 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3573 register EMACS_INT i;
3575 CHECK_NATNUM (nframes);
3576 union specbinding *pdl = get_backtrace_starting_at (base);
3578 /* Find the frame requested. */
3579 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3580 pdl = backtrace_next (pdl);
3582 return pdl;
3585 static Lisp_Object
3586 backtrace_frame_apply (Lisp_Object function, union specbinding *pdl)
3588 if (!backtrace_p (pdl))
3589 return Qnil;
3591 Lisp_Object flags = Qnil;
3592 if (backtrace_debug_on_exit (pdl))
3593 flags = Fcons (QCdebug_on_exit, Fcons (Qt, Qnil));
3595 if (backtrace_nargs (pdl) == UNEVALLED)
3596 return call4 (function, Qnil, backtrace_function (pdl), *backtrace_args (pdl), flags);
3597 else
3599 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3600 return call4 (function, Qt, backtrace_function (pdl), tem, flags);
3604 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3605 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3606 The debugger is entered when that frame exits, if the flag is non-nil. */)
3607 (Lisp_Object level, Lisp_Object flag)
3609 CHECK_NUMBER (level);
3610 union specbinding *pdl = get_backtrace_frame(level, Qnil);
3612 if (backtrace_p (pdl))
3613 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3615 return flag;
3618 DEFUN ("mapbacktrace", Fmapbacktrace, Smapbacktrace, 1, 2, 0,
3619 doc: /* Call FUNCTION for each frame in backtrace.
3620 If BASE is non-nil, it should be a function and iteration will start
3621 from its nearest activation frame.
3622 FUNCTION is called with 4 arguments: EVALD, FUNC, ARGS, and FLAGS. If
3623 a frame has not evaluated its arguments yet or is a special form,
3624 EVALD is nil and ARGS is a list of forms. If a frame has evaluated
3625 its arguments and called its function already, EVALD is t and ARGS is
3626 a list of values.
3627 FLAGS is a plist of properties of the current frame: currently, the
3628 only supported property is :debug-on-exit. `mapbacktrace' always
3629 returns nil. */)
3630 (Lisp_Object function, Lisp_Object base)
3632 union specbinding *pdl = get_backtrace_starting_at (base);
3634 while (backtrace_p (pdl))
3636 ptrdiff_t i = pdl - specpdl;
3637 backtrace_frame_apply (function, pdl);
3638 /* Beware! PDL is no longer valid here because FUNCTION might
3639 have caused grow_specpdl to reallocate pdlvec. We must use
3640 the saved index, cf. Bug#27258. */
3641 pdl = backtrace_next (&specpdl[i]);
3644 return Qnil;
3647 DEFUN ("backtrace-frame--internal", Fbacktrace_frame_internal,
3648 Sbacktrace_frame_internal, 3, 3, NULL,
3649 doc: /* Call FUNCTION on stack frame NFRAMES away from BASE.
3650 Return the result of FUNCTION, or nil if no matching frame could be found. */)
3651 (Lisp_Object function, Lisp_Object nframes, Lisp_Object base)
3653 return backtrace_frame_apply (function, get_backtrace_frame (nframes, base));
3656 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3657 the specpdl stack, and then rewind them. We store the pre-unwind values
3658 directly in the pre-existing specpdl elements (i.e. we swap the current
3659 value and the old value stored in the specpdl), kind of like the inplace
3660 pointer-reversal trick. As it turns out, the rewind does the same as the
3661 unwind, except it starts from the other end of the specpdl stack, so we use
3662 the same function for both unwind and rewind. */
3663 static void
3664 backtrace_eval_unrewind (int distance)
3666 union specbinding *tmp = specpdl_ptr;
3667 int step = -1;
3668 if (distance < 0)
3669 { /* It's a rewind rather than unwind. */
3670 tmp += distance - 1;
3671 step = 1;
3672 distance = -distance;
3675 for (; distance > 0; distance--)
3677 tmp += step;
3678 switch (tmp->kind)
3680 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3681 unwind_protect, but the problem is that we don't know how to
3682 rewind them afterwards. */
3683 case SPECPDL_UNWIND:
3685 Lisp_Object oldarg = tmp->unwind.arg;
3686 if (tmp->unwind.func == set_buffer_if_live)
3687 tmp->unwind.arg = Fcurrent_buffer ();
3688 else if (tmp->unwind.func == save_excursion_restore)
3689 tmp->unwind.arg = save_excursion_save ();
3690 else
3691 break;
3692 tmp->unwind.func (oldarg);
3693 break;
3696 case SPECPDL_UNWIND_PTR:
3697 case SPECPDL_UNWIND_INT:
3698 case SPECPDL_UNWIND_VOID:
3699 case SPECPDL_BACKTRACE:
3700 break;
3701 case SPECPDL_LET:
3702 { /* If variable has a trivial value (no forwarding), we can
3703 just set it. No need to check for constant symbols here,
3704 since that was already done by specbind. */
3705 Lisp_Object sym = specpdl_symbol (tmp);
3706 if (SYMBOLP (sym)
3707 && XSYMBOL (sym)->u.s.redirect == SYMBOL_PLAINVAL)
3709 Lisp_Object old_value = specpdl_old_value (tmp);
3710 set_specpdl_old_value (tmp, SYMBOL_VAL (XSYMBOL (sym)));
3711 SET_SYMBOL_VAL (XSYMBOL (sym), old_value);
3712 break;
3715 /* Come here only if make_local_foo was used for the first
3716 time on this var within this let. */
3717 FALLTHROUGH;
3718 case SPECPDL_LET_DEFAULT:
3720 Lisp_Object sym = specpdl_symbol (tmp);
3721 Lisp_Object old_value = specpdl_old_value (tmp);
3722 set_specpdl_old_value (tmp, Fdefault_value (sym));
3723 Fset_default (sym, old_value);
3725 break;
3726 case SPECPDL_LET_LOCAL:
3728 Lisp_Object symbol = specpdl_symbol (tmp);
3729 Lisp_Object where = specpdl_where (tmp);
3730 Lisp_Object old_value = specpdl_old_value (tmp);
3731 eassert (BUFFERP (where));
3733 /* If this was a local binding, reset the value in the appropriate
3734 buffer, but only if that buffer's binding still exists. */
3735 if (!NILP (Flocal_variable_p (symbol, where)))
3737 set_specpdl_old_value
3738 (tmp, Fbuffer_local_value (symbol, where));
3739 set_internal (symbol, old_value, where, SET_INTERNAL_UNBIND);
3742 break;
3747 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3748 doc: /* Evaluate EXP in the context of some activation frame.
3749 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3750 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3752 union specbinding *pdl = get_backtrace_frame (nframes, base);
3753 ptrdiff_t count = SPECPDL_INDEX ();
3754 ptrdiff_t distance = specpdl_ptr - pdl;
3755 eassert (distance >= 0);
3757 if (!backtrace_p (pdl))
3758 error ("Activation frame not found!");
3760 backtrace_eval_unrewind (distance);
3761 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3763 /* Use eval_sub rather than Feval since the main motivation behind
3764 backtrace-eval is to be able to get/set the value of lexical variables
3765 from the debugger. */
3766 return unbind_to (count, eval_sub (exp));
3769 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3770 doc: /* Return names and values of local variables of a stack frame.
3771 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3772 (Lisp_Object nframes, Lisp_Object base)
3774 union specbinding *frame = get_backtrace_frame (nframes, base);
3775 union specbinding *prevframe
3776 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3777 ptrdiff_t distance = specpdl_ptr - frame;
3778 Lisp_Object result = Qnil;
3779 eassert (distance >= 0);
3781 if (!backtrace_p (prevframe))
3782 error ("Activation frame not found!");
3783 if (!backtrace_p (frame))
3784 error ("Activation frame not found!");
3786 /* The specpdl entries normally contain the symbol being bound along with its
3787 `old_value', so it can be restored. The new value to which it is bound is
3788 available in one of two places: either in the current value of the
3789 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3790 next specpdl entry for it.
3791 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3792 and "new value", so we abuse it here, to fetch the new value.
3793 It's ugly (we'd rather not modify global data) and a bit inefficient,
3794 but it does the job for now. */
3795 backtrace_eval_unrewind (distance);
3797 /* Grab values. */
3799 union specbinding *tmp = prevframe;
3800 for (; tmp > frame; tmp--)
3802 switch (tmp->kind)
3804 case SPECPDL_LET:
3805 case SPECPDL_LET_DEFAULT:
3806 case SPECPDL_LET_LOCAL:
3808 Lisp_Object sym = specpdl_symbol (tmp);
3809 Lisp_Object val = specpdl_old_value (tmp);
3810 if (EQ (sym, Qinternal_interpreter_environment))
3812 Lisp_Object env = val;
3813 for (; CONSP (env); env = XCDR (env))
3815 Lisp_Object binding = XCAR (env);
3816 if (CONSP (binding))
3817 result = Fcons (Fcons (XCAR (binding),
3818 XCDR (binding)),
3819 result);
3822 else
3823 result = Fcons (Fcons (sym, val), result);
3825 break;
3827 case SPECPDL_UNWIND:
3828 case SPECPDL_UNWIND_PTR:
3829 case SPECPDL_UNWIND_INT:
3830 case SPECPDL_UNWIND_VOID:
3831 case SPECPDL_BACKTRACE:
3832 break;
3834 default:
3835 emacs_abort ();
3840 /* Restore values from specpdl to original place. */
3841 backtrace_eval_unrewind (-distance);
3843 return result;
3847 void
3848 mark_specpdl (union specbinding *first, union specbinding *ptr)
3850 union specbinding *pdl;
3851 for (pdl = first; pdl != ptr; pdl++)
3853 switch (pdl->kind)
3855 case SPECPDL_UNWIND:
3856 mark_object (specpdl_arg (pdl));
3857 break;
3859 case SPECPDL_BACKTRACE:
3861 ptrdiff_t nargs = backtrace_nargs (pdl);
3862 mark_object (backtrace_function (pdl));
3863 if (nargs == UNEVALLED)
3864 nargs = 1;
3865 while (nargs--)
3866 mark_object (backtrace_args (pdl)[nargs]);
3868 break;
3870 case SPECPDL_LET_DEFAULT:
3871 case SPECPDL_LET_LOCAL:
3872 mark_object (specpdl_where (pdl));
3873 FALLTHROUGH;
3874 case SPECPDL_LET:
3875 mark_object (specpdl_symbol (pdl));
3876 mark_object (specpdl_old_value (pdl));
3877 mark_object (specpdl_saved_value (pdl));
3878 break;
3880 case SPECPDL_UNWIND_PTR:
3881 case SPECPDL_UNWIND_INT:
3882 case SPECPDL_UNWIND_VOID:
3883 break;
3885 default:
3886 emacs_abort ();
3891 void
3892 get_backtrace (Lisp_Object array)
3894 union specbinding *pdl = backtrace_next (backtrace_top ());
3895 ptrdiff_t i = 0, asize = ASIZE (array);
3897 /* Copy the backtrace contents into working memory. */
3898 for (; i < asize; i++)
3900 if (backtrace_p (pdl))
3902 ASET (array, i, backtrace_function (pdl));
3903 pdl = backtrace_next (pdl);
3905 else
3906 ASET (array, i, Qnil);
3910 Lisp_Object backtrace_top_function (void)
3912 union specbinding *pdl = backtrace_top ();
3913 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3916 void
3917 syms_of_eval (void)
3919 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3920 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3921 If Lisp code tries to increase the total number past this amount,
3922 an error is signaled.
3923 You can safely use a value considerably larger than the default value,
3924 if that proves inconveniently small. However, if you increase it too far,
3925 Emacs could run out of memory trying to make the stack bigger.
3926 Note that this limit may be silently increased by the debugger
3927 if `debug-on-error' or `debug-on-quit' is set. */);
3929 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3930 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3932 This limit serves to catch infinite recursions for you before they cause
3933 actual stack overflow in C, which would be fatal for Emacs.
3934 You can safely make it considerably larger than its default value,
3935 if that proves inconveniently small. However, if you increase it too far,
3936 Emacs could overflow the real C stack, and crash. */);
3938 DEFVAR_LISP ("quit-flag", Vquit_flag,
3939 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3940 If the value is t, that means do an ordinary quit.
3941 If the value equals `throw-on-input', that means quit by throwing
3942 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3943 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3944 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3945 Vquit_flag = Qnil;
3947 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3948 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3949 Note that `quit-flag' will still be set by typing C-g,
3950 so a quit will be signaled as soon as `inhibit-quit' is nil.
3951 To prevent this happening, set `quit-flag' to nil
3952 before making `inhibit-quit' nil. */);
3953 Vinhibit_quit = Qnil;
3955 DEFSYM (Qsetq, "setq");
3956 DEFSYM (Qinhibit_quit, "inhibit-quit");
3957 DEFSYM (Qautoload, "autoload");
3958 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3959 DEFSYM (Qmacro, "macro");
3961 /* Note that the process handling also uses Qexit, but we don't want
3962 to staticpro it twice, so we just do it here. */
3963 DEFSYM (Qexit, "exit");
3965 DEFSYM (Qinteractive, "interactive");
3966 DEFSYM (Qcommandp, "commandp");
3967 DEFSYM (Qand_rest, "&rest");
3968 DEFSYM (Qand_optional, "&optional");
3969 DEFSYM (Qclosure, "closure");
3970 DEFSYM (QCdocumentation, ":documentation");
3971 DEFSYM (Qdebug, "debug");
3973 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3974 doc: /* Non-nil means never enter the debugger.
3975 Normally set while the debugger is already active, to avoid recursive
3976 invocations. */);
3977 Vinhibit_debugger = Qnil;
3979 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3980 doc: /* Non-nil means enter debugger if an error is signaled.
3981 Does not apply to errors handled by `condition-case' or those
3982 matched by `debug-ignored-errors'.
3983 If the value is a list, an error only means to enter the debugger
3984 if one of its condition symbols appears in the list.
3985 When you evaluate an expression interactively, this variable
3986 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3987 The command `toggle-debug-on-error' toggles this.
3988 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3989 Vdebug_on_error = Qnil;
3991 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3992 doc: /* List of errors for which the debugger should not be called.
3993 Each element may be a condition-name or a regexp that matches error messages.
3994 If any element applies to a given error, that error skips the debugger
3995 and just returns to top level.
3996 This overrides the variable `debug-on-error'.
3997 It does not apply to errors handled by `condition-case'. */);
3998 Vdebug_ignored_errors = Qnil;
4000 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
4001 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
4002 Does not apply if quit is handled by a `condition-case'. */);
4003 debug_on_quit = 0;
4005 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
4006 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
4008 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
4009 doc: /* Non-nil means debugger may continue execution.
4010 This is nil when the debugger is called under circumstances where it
4011 might not be safe to continue. */);
4012 debugger_may_continue = 1;
4014 DEFVAR_BOOL ("debugger-stack-frame-as-list", debugger_stack_frame_as_list,
4015 doc: /* Non-nil means display call stack frames as lists. */);
4016 debugger_stack_frame_as_list = 0;
4018 DEFVAR_LISP ("debugger", Vdebugger,
4019 doc: /* Function to call to invoke debugger.
4020 If due to frame exit, args are `exit' and the value being returned;
4021 this function's value will be returned instead of that.
4022 If due to error, args are `error' and a list of the args to `signal'.
4023 If due to `apply' or `funcall' entry, one arg, `lambda'.
4024 If due to `eval' entry, one arg, t. */);
4025 Vdebugger = Qnil;
4027 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
4028 doc: /* If non-nil, this is a function for `signal' to call.
4029 It receives the same arguments that `signal' was given.
4030 The Edebug package uses this to regain control. */);
4031 Vsignal_hook_function = Qnil;
4033 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
4034 doc: /* Non-nil means call the debugger regardless of condition handlers.
4035 Note that `debug-on-error', `debug-on-quit' and friends
4036 still determine whether to handle the particular condition. */);
4037 Vdebug_on_signal = Qnil;
4039 /* When lexical binding is being used,
4040 Vinternal_interpreter_environment is non-nil, and contains an alist
4041 of lexically-bound variable, or (t), indicating an empty
4042 environment. The lisp name of this variable would be
4043 `internal-interpreter-environment' if it weren't hidden.
4044 Every element of this list can be either a cons (VAR . VAL)
4045 specifying a lexical binding, or a single symbol VAR indicating
4046 that this variable should use dynamic scoping. */
4047 DEFSYM (Qinternal_interpreter_environment,
4048 "internal-interpreter-environment");
4049 DEFVAR_LISP ("internal-interpreter-environment",
4050 Vinternal_interpreter_environment,
4051 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
4052 When lexical binding is not being used, this variable is nil.
4053 A value of `(t)' indicates an empty environment, otherwise it is an
4054 alist of active lexical bindings. */);
4055 Vinternal_interpreter_environment = Qnil;
4056 /* Don't export this variable to Elisp, so no one can mess with it
4057 (Just imagine if someone makes it buffer-local). */
4058 Funintern (Qinternal_interpreter_environment, Qnil);
4060 Vrun_hooks = intern_c_string ("run-hooks");
4061 staticpro (&Vrun_hooks);
4063 staticpro (&Vautoload_queue);
4064 Vautoload_queue = Qnil;
4065 staticpro (&Vsignaling_function);
4066 Vsignaling_function = Qnil;
4068 inhibit_lisp_code = Qnil;
4070 defsubr (&Sor);
4071 defsubr (&Sand);
4072 defsubr (&Sif);
4073 defsubr (&Scond);
4074 defsubr (&Sprogn);
4075 defsubr (&Sprog1);
4076 defsubr (&Sprog2);
4077 defsubr (&Ssetq);
4078 defsubr (&Squote);
4079 defsubr (&Sfunction);
4080 defsubr (&Sdefault_toplevel_value);
4081 defsubr (&Sset_default_toplevel_value);
4082 defsubr (&Sdefvar);
4083 defsubr (&Sdefvaralias);
4084 DEFSYM (Qdefvaralias, "defvaralias");
4085 defsubr (&Sdefconst);
4086 defsubr (&Smake_var_non_special);
4087 defsubr (&Slet);
4088 defsubr (&SletX);
4089 defsubr (&Swhile);
4090 defsubr (&Smacroexpand);
4091 defsubr (&Scatch);
4092 defsubr (&Sthrow);
4093 defsubr (&Sunwind_protect);
4094 defsubr (&Scondition_case);
4095 defsubr (&Ssignal);
4096 defsubr (&Scommandp);
4097 defsubr (&Sautoload);
4098 defsubr (&Sautoload_do_load);
4099 defsubr (&Seval);
4100 defsubr (&Sapply);
4101 defsubr (&Sfuncall);
4102 defsubr (&Sfunc_arity);
4103 defsubr (&Srun_hooks);
4104 defsubr (&Srun_hook_with_args);
4105 defsubr (&Srun_hook_with_args_until_success);
4106 defsubr (&Srun_hook_with_args_until_failure);
4107 defsubr (&Srun_hook_wrapped);
4108 defsubr (&Sfetch_bytecode);
4109 defsubr (&Sbacktrace_debug);
4110 DEFSYM (QCdebug_on_exit, ":debug-on-exit");
4111 defsubr (&Smapbacktrace);
4112 defsubr (&Sbacktrace_frame_internal);
4113 defsubr (&Sbacktrace_eval);
4114 defsubr (&Sbacktrace__locals);
4115 defsubr (&Sspecial_variable_p);
4116 defsubr (&Sfunctionp);